Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

Linux/drivers/staging/ozwpan/ozhcd.c

  1 /* -----------------------------------------------------------------------------
  2  * Copyright (c) 2011 Ozmo Inc
  3  * Released under the GNU General Public License Version 2 (GPLv2).
  4  *
  5  * This file provides the implementation of a USB host controller device that
  6  * does not have any associated hardware. Instead the virtual device is
  7  * connected to the WiFi network and emulates the operation of a USB hcd by
  8  * receiving and sending network frames.
  9  * Note:
 10  * We take great pains to reduce the amount of code where interrupts need to be
 11  * disabled and in this respect we are different from standard HCD's. In
 12  * particular we don't want in_irq() code bleeding over to the protocol side of
 13  * the driver.
 14  * The troublesome functions are the urb enqueue and dequeue functions both of
 15  * which can be called in_irq(). So for these functions we put the urbs into a
 16  * queue and request a tasklet to process them. This means that a spinlock with
 17  * interrupts disabled must be held for insertion and removal but most code is
 18  * is in tasklet or soft irq context. The lock that protects this list is called
 19  * the tasklet lock and serves the purpose of the 'HCD lock' which must be held
 20  * when calling the following functions.
 21  *   usb_hcd_link_urb_to_ep()
 22  *   usb_hcd_unlink_urb_from_ep()
 23  *   usb_hcd_flush_endpoint()
 24  *   usb_hcd_check_unlink_urb()
 25  * -----------------------------------------------------------------------------
 26  */
 27 #include <linux/platform_device.h>
 28 #include <linux/usb.h>
 29 #include <linux/slab.h>
 30 #include <linux/export.h>
 31 #include "linux/usb/hcd.h"
 32 #include <asm/unaligned.h>
 33 #include "ozdbg.h"
 34 #include "ozusbif.h"
 35 #include "ozurbparanoia.h"
 36 #include "ozhcd.h"
 37 
 38 /*
 39  * Number of units of buffering to capture for an isochronous IN endpoint before
 40  * allowing data to be indicated up.
 41  */
 42 #define OZ_IN_BUFFERING_UNITS   100
 43 
 44 /* Name of our platform device.
 45  */
 46 #define OZ_PLAT_DEV_NAME        "ozwpan"
 47 
 48 /* Maximum number of free urb links that can be kept in the pool.
 49  */
 50 #define OZ_MAX_LINK_POOL_SIZE   16
 51 
 52 /* Get endpoint object from the containing link.
 53  */
 54 #define ep_from_link(__e) container_of((__e), struct oz_endpoint, link)
 55 
 56 /*EP0 timeout before ep0 request is again added to TX queue. (13*8 = 98mSec)
 57  */
 58 #define EP0_TIMEOUT_COUNTER 13
 59 
 60 /* Debounce time HCD driver should wait before unregistering.
 61  */
 62 #define OZ_HUB_DEBOUNCE_TIMEOUT 1500
 63 
 64 /*
 65  * Used to link urbs together and also store some status information for each
 66  * urb.
 67  * A cache of these are kept in a pool to reduce number of calls to kmalloc.
 68  */
 69 struct oz_urb_link {
 70         struct list_head link;
 71         struct urb *urb;
 72         struct oz_port *port;
 73         u8 req_id;
 74         u8 ep_num;
 75         unsigned submit_counter;
 76 };
 77 
 78 /* Holds state information about a USB endpoint.
 79  */
 80 #define OZ_EP_BUFFER_SIZE_ISOC  (1024 * 24)
 81 #define OZ_EP_BUFFER_SIZE_INT   512
 82 struct oz_endpoint {
 83         struct list_head urb_list;      /* List of oz_urb_link items. */
 84         struct list_head link;          /* For isoc ep, links in to isoc
 85                                            lists of oz_port. */
 86         struct timespec timestamp;
 87         int credit;
 88         int credit_ceiling;
 89         u8 ep_num;
 90         u8 attrib;
 91         u8 *buffer;
 92         int buffer_size;
 93         int in_ix;
 94         int out_ix;
 95         int buffered_units;
 96         unsigned flags;
 97         int start_frame;
 98 };
 99 
100 /* Bits in the flags field. */
101 #define OZ_F_EP_BUFFERING       0x1
102 #define OZ_F_EP_HAVE_STREAM     0x2
103 
104 /* Holds state information about a USB interface.
105  */
106 struct oz_interface {
107         unsigned ep_mask;
108         u8 alt;
109 };
110 
111 /* Holds state information about an hcd port.
112  */
113 #define OZ_NB_ENDPOINTS 16
114 struct oz_port {
115         unsigned flags;
116         unsigned status;
117         void *hpd;
118         struct oz_hcd *ozhcd;
119         spinlock_t port_lock;
120         u8 bus_addr;
121         u8 next_req_id;
122         u8 config_num;
123         int num_iface;
124         struct oz_interface *iface;
125         struct oz_endpoint *out_ep[OZ_NB_ENDPOINTS];
126         struct oz_endpoint *in_ep[OZ_NB_ENDPOINTS];
127         struct list_head isoc_out_ep;
128         struct list_head isoc_in_ep;
129 };
130 
131 #define OZ_PORT_F_PRESENT       0x1
132 #define OZ_PORT_F_CHANGED       0x2
133 #define OZ_PORT_F_DYING         0x4
134 
135 /* Data structure in the private context area of struct usb_hcd.
136  */
137 #define OZ_NB_PORTS     8
138 struct oz_hcd {
139         spinlock_t hcd_lock;
140         struct list_head urb_pending_list;
141         struct list_head urb_cancel_list;
142         struct list_head orphanage;
143         int conn_port; /* Port that is currently connecting, -1 if none.*/
144         struct oz_port ports[OZ_NB_PORTS];
145         uint flags;
146         struct usb_hcd *hcd;
147 };
148 
149 /* Bits in flags field.
150  */
151 #define OZ_HDC_F_SUSPENDED      0x1
152 
153 /*
154  * Static function prototypes.
155  */
156 static int oz_hcd_start(struct usb_hcd *hcd);
157 static void oz_hcd_stop(struct usb_hcd *hcd);
158 static void oz_hcd_shutdown(struct usb_hcd *hcd);
159 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
160                                 gfp_t mem_flags);
161 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
162 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
163                                 struct usb_host_endpoint *ep);
164 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
165                                 struct usb_host_endpoint *ep);
166 static int oz_hcd_get_frame_number(struct usb_hcd *hcd);
167 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf);
168 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
169                                 u16 windex, char *buf, u16 wlength);
170 static int oz_hcd_bus_suspend(struct usb_hcd *hcd);
171 static int oz_hcd_bus_resume(struct usb_hcd *hcd);
172 static int oz_plat_probe(struct platform_device *dev);
173 static int oz_plat_remove(struct platform_device *dev);
174 static void oz_plat_shutdown(struct platform_device *dev);
175 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg);
176 static int oz_plat_resume(struct platform_device *dev);
177 static void oz_urb_process_tasklet(unsigned long unused);
178 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
179                 struct oz_port *port, struct usb_host_config *config,
180                 gfp_t mem_flags);
181 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
182                                 struct oz_port *port);
183 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
184                         struct oz_port *port,
185                         struct usb_host_interface *intf, gfp_t mem_flags);
186 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
187                         struct oz_port *port, int if_ix);
188 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
189                 gfp_t mem_flags);
190 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
191                 struct urb *urb);
192 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status);
193 
194 /*
195  * Static external variables.
196  */
197 static struct platform_device *g_plat_dev;
198 static struct oz_hcd *g_ozhcd;
199 static DEFINE_SPINLOCK(g_hcdlock);      /* Guards g_ozhcd. */
200 static const char g_hcd_name[] = "Ozmo WPAN";
201 static struct list_head *g_link_pool;
202 static int g_link_pool_size;
203 static DEFINE_SPINLOCK(g_link_lock);
204 static DEFINE_SPINLOCK(g_tasklet_lock);
205 static struct tasklet_struct g_urb_process_tasklet;
206 static struct tasklet_struct g_urb_cancel_tasklet;
207 static atomic_t g_pending_urbs = ATOMIC_INIT(0);
208 static atomic_t g_usb_frame_number = ATOMIC_INIT(0);
209 static const struct hc_driver g_oz_hc_drv = {
210         .description =          g_hcd_name,
211         .product_desc =         "Ozmo Devices WPAN",
212         .hcd_priv_size =        sizeof(struct oz_hcd),
213         .flags =                HCD_USB11,
214         .start =                oz_hcd_start,
215         .stop =                 oz_hcd_stop,
216         .shutdown =             oz_hcd_shutdown,
217         .urb_enqueue =          oz_hcd_urb_enqueue,
218         .urb_dequeue =          oz_hcd_urb_dequeue,
219         .endpoint_disable =     oz_hcd_endpoint_disable,
220         .endpoint_reset =       oz_hcd_endpoint_reset,
221         .get_frame_number =     oz_hcd_get_frame_number,
222         .hub_status_data =      oz_hcd_hub_status_data,
223         .hub_control =          oz_hcd_hub_control,
224         .bus_suspend =          oz_hcd_bus_suspend,
225         .bus_resume =           oz_hcd_bus_resume,
226 };
227 
228 static struct platform_driver g_oz_plat_drv = {
229         .probe = oz_plat_probe,
230         .remove = oz_plat_remove,
231         .shutdown = oz_plat_shutdown,
232         .suspend = oz_plat_suspend,
233         .resume = oz_plat_resume,
234         .driver = {
235                 .name = OZ_PLAT_DEV_NAME,
236                 .owner = THIS_MODULE,
237         },
238 };
239 
240 /*
241  * Gets our private context area (which is of type struct oz_hcd) from the
242  * usb_hcd structure.
243  * Context: any
244  */
245 static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
246 {
247         return (struct oz_hcd *)hcd->hcd_priv;
248 }
249 
250 /*
251  * Searches list of ports to find the index of the one with a specified  USB
252  * bus address. If none of the ports has the bus address then the connection
253  * port is returned, if there is one or -1 otherwise.
254  * Context: any
255  */
256 static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
257 {
258         int i;
259 
260         for (i = 0; i < OZ_NB_PORTS; i++) {
261                 if (ozhcd->ports[i].bus_addr == bus_addr)
262                         return i;
263         }
264         return ozhcd->conn_port;
265 }
266 
267 /*
268  * Allocates an urb link, first trying the pool but going to heap if empty.
269  * Context: any
270  */
271 static struct oz_urb_link *oz_alloc_urb_link(void)
272 {
273         struct oz_urb_link *urbl = NULL;
274         unsigned long irq_state;
275 
276         spin_lock_irqsave(&g_link_lock, irq_state);
277         if (g_link_pool) {
278                 urbl = container_of(g_link_pool, struct oz_urb_link, link);
279                 g_link_pool = urbl->link.next;
280                 --g_link_pool_size;
281         }
282         spin_unlock_irqrestore(&g_link_lock, irq_state);
283         if (urbl == NULL)
284                 urbl = kmalloc(sizeof(struct oz_urb_link), GFP_ATOMIC);
285         return urbl;
286 }
287 
288 /*
289  * Frees an urb link by putting it in the pool if there is enough space or
290  * deallocating it to heap otherwise.
291  * Context: any
292  */
293 static void oz_free_urb_link(struct oz_urb_link *urbl)
294 {
295         if (urbl) {
296                 unsigned long irq_state;
297                 spin_lock_irqsave(&g_link_lock, irq_state);
298                 if (g_link_pool_size < OZ_MAX_LINK_POOL_SIZE) {
299                         urbl->link.next = g_link_pool;
300                         g_link_pool = &urbl->link;
301                         urbl = NULL;
302                         g_link_pool_size++;
303                 }
304                 spin_unlock_irqrestore(&g_link_lock, irq_state);
305                 kfree(urbl);
306         }
307 }
308 
309 /*
310  * Deallocates all the urb links in the pool.
311  * Context: unknown
312  */
313 static void oz_empty_link_pool(void)
314 {
315         struct list_head *e;
316         unsigned long irq_state;
317 
318         spin_lock_irqsave(&g_link_lock, irq_state);
319         e = g_link_pool;
320         g_link_pool = NULL;
321         g_link_pool_size = 0;
322         spin_unlock_irqrestore(&g_link_lock, irq_state);
323         while (e) {
324                 struct oz_urb_link *urbl =
325                         container_of(e, struct oz_urb_link, link);
326                 e = e->next;
327                 kfree(urbl);
328         }
329 }
330 
331 /*
332  * Allocates endpoint structure and optionally a buffer. If a buffer is
333  * allocated it immediately follows the endpoint structure.
334  * Context: softirq
335  */
336 static struct oz_endpoint *oz_ep_alloc(int buffer_size, gfp_t mem_flags)
337 {
338         struct oz_endpoint *ep =
339                 kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
340         if (ep) {
341                 INIT_LIST_HEAD(&ep->urb_list);
342                 INIT_LIST_HEAD(&ep->link);
343                 ep->credit = -1;
344                 if (buffer_size) {
345                         ep->buffer_size = buffer_size;
346                         ep->buffer = (u8 *)(ep+1);
347                 }
348         }
349         return ep;
350 }
351 
352 /*
353  * Pre-condition: Must be called with g_tasklet_lock held and interrupts
354  * disabled.
355  * Context: softirq or process
356  */
357 static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd,
358                 struct urb *urb)
359 {
360         struct oz_urb_link *urbl;
361         struct list_head *e;
362 
363         list_for_each(e, &ozhcd->urb_cancel_list) {
364                 urbl = container_of(e, struct oz_urb_link, link);
365                 if (urb == urbl->urb) {
366                         list_del_init(e);
367                         return urbl;
368                 }
369         }
370         return NULL;
371 }
372 
373 /*
374  * This is called when we have finished processing an urb. It unlinks it from
375  * the ep and returns it to the core.
376  * Context: softirq or process
377  */
378 static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
379                 int status)
380 {
381         struct oz_hcd *ozhcd = oz_hcd_private(hcd);
382         unsigned long irq_state;
383         struct oz_urb_link *cancel_urbl;
384 
385         spin_lock_irqsave(&g_tasklet_lock, irq_state);
386         usb_hcd_unlink_urb_from_ep(hcd, urb);
387         /* Clear hcpriv which will prevent it being put in the cancel list
388          * in the event that an attempt is made to cancel it.
389          */
390         urb->hcpriv = NULL;
391         /* Walk the cancel list in case the urb is already sitting there.
392          * Since we process the cancel list in a tasklet rather than in
393          * the dequeue function this could happen.
394          */
395         cancel_urbl = oz_uncancel_urb(ozhcd, urb);
396         /* Note: we release lock but do not enable local irqs.
397          * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
398          * or at least other host controllers disable interrupts at this point
399          * so we do the same. We must, however, release the lock otherwise a
400          * deadlock will occur if an urb is submitted to our driver in the urb
401          * completion function. Because we disable interrupts it is possible
402          * that the urb_enqueue function can be called with them disabled.
403          */
404         spin_unlock(&g_tasklet_lock);
405         if (oz_forget_urb(urb)) {
406                 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
407         } else {
408                 atomic_dec(&g_pending_urbs);
409                 usb_hcd_giveback_urb(hcd, urb, status);
410         }
411         spin_lock(&g_tasklet_lock);
412         spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
413         if (cancel_urbl)
414                 oz_free_urb_link(cancel_urbl);
415 }
416 
417 /*
418  * Deallocates an endpoint including deallocating any associated stream and
419  * returning any queued urbs to the core.
420  * Context: softirq
421  */
422 static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
423 {
424         if (port) {
425                 struct list_head list;
426                 struct oz_hcd *ozhcd = port->ozhcd;
427                 INIT_LIST_HEAD(&list);
428                 if (ep->flags & OZ_F_EP_HAVE_STREAM)
429                         oz_usb_stream_delete(port->hpd, ep->ep_num);
430                 /* Transfer URBs to the orphanage while we hold the lock. */
431                 spin_lock_bh(&ozhcd->hcd_lock);
432                 /* Note: this works even if ep->urb_list is empty.*/
433                 list_replace_init(&ep->urb_list, &list);
434                 /* Put the URBs in the orphanage. */
435                 list_splice_tail(&list, &ozhcd->orphanage);
436                 spin_unlock_bh(&ozhcd->hcd_lock);
437         }
438         oz_dbg(ON, "Freeing endpoint memory\n");
439         kfree(ep);
440 }
441 
442 /*
443  * Context: softirq
444  */
445 static void oz_complete_buffered_urb(struct oz_port *port,
446                         struct oz_endpoint *ep,
447                         struct urb *urb)
448 {
449         int data_len, available_space, copy_len;
450 
451         data_len = ep->buffer[ep->out_ix];
452         if (data_len <= urb->transfer_buffer_length)
453                 available_space = data_len;
454         else
455                 available_space = urb->transfer_buffer_length;
456 
457         if (++ep->out_ix == ep->buffer_size)
458                 ep->out_ix = 0;
459         copy_len = ep->buffer_size - ep->out_ix;
460         if (copy_len >= available_space)
461                 copy_len = available_space;
462         memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
463 
464         if (copy_len < available_space) {
465                 memcpy((urb->transfer_buffer + copy_len), ep->buffer,
466                                                 (available_space - copy_len));
467                 ep->out_ix = available_space - copy_len;
468         } else {
469                 ep->out_ix += copy_len;
470         }
471         urb->actual_length = available_space;
472         if (ep->out_ix == ep->buffer_size)
473                 ep->out_ix = 0;
474 
475         ep->buffered_units--;
476         oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
477                available_space);
478         oz_complete_urb(port->ozhcd->hcd, urb, 0);
479 }
480 
481 /*
482  * Context: softirq
483  */
484 static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
485                         struct urb *urb, u8 req_id)
486 {
487         struct oz_urb_link *urbl;
488         struct oz_endpoint *ep = NULL;
489         int err = 0;
490 
491         if (ep_addr >= OZ_NB_ENDPOINTS) {
492                 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
493                 return -EINVAL;
494         }
495         urbl = oz_alloc_urb_link();
496         if (!urbl)
497                 return -ENOMEM;
498         urbl->submit_counter = 0;
499         urbl->urb = urb;
500         urbl->req_id = req_id;
501         urbl->ep_num = ep_addr;
502         /* Hold lock while we insert the URB into the list within the
503          * endpoint structure.
504          */
505         spin_lock_bh(&port->ozhcd->hcd_lock);
506         /* If the urb has been unlinked while out of any list then
507          * complete it now.
508          */
509         if (urb->unlinked) {
510                 spin_unlock_bh(&port->ozhcd->hcd_lock);
511                 oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
512                 oz_complete_urb(port->ozhcd->hcd, urb, 0);
513                 oz_free_urb_link(urbl);
514                 return 0;
515         }
516 
517         if (in_dir)
518                 ep = port->in_ep[ep_addr];
519         else
520                 ep = port->out_ep[ep_addr];
521         if (!ep) {
522                 err = -ENOMEM;
523                 goto out;
524         }
525 
526         /*For interrupt endpoint check for buffered data
527         * & complete urb
528         */
529         if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
530                                                  && ep->buffered_units > 0) {
531                 oz_free_urb_link(urbl);
532                 spin_unlock_bh(&port->ozhcd->hcd_lock);
533                 oz_complete_buffered_urb(port, ep, urb);
534                 return 0;
535         }
536 
537         if (port->hpd) {
538                 list_add_tail(&urbl->link, &ep->urb_list);
539                 if (!in_dir && ep_addr && (ep->credit < 0)) {
540                         getrawmonotonic(&ep->timestamp);
541                         ep->credit = 0;
542                 }
543         } else {
544                 err = -EPIPE;
545         }
546 out:
547         spin_unlock_bh(&port->ozhcd->hcd_lock);
548         if (err)
549                 oz_free_urb_link(urbl);
550         return err;
551 }
552 
553 /*
554  * Removes an urb from the queue in the endpoint.
555  * Returns 0 if it is found and -EIDRM otherwise.
556  * Context: softirq
557  */
558 static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
559                         struct urb *urb)
560 {
561         struct oz_urb_link *urbl = NULL;
562         struct oz_endpoint *ep;
563 
564         spin_lock_bh(&port->ozhcd->hcd_lock);
565         if (in_dir)
566                 ep = port->in_ep[ep_addr];
567         else
568                 ep = port->out_ep[ep_addr];
569         if (ep) {
570                 struct list_head *e;
571                 list_for_each(e, &ep->urb_list) {
572                         urbl = container_of(e, struct oz_urb_link, link);
573                         if (urbl->urb == urb) {
574                                 list_del_init(e);
575                                 break;
576                         }
577                         urbl = NULL;
578                 }
579         }
580         spin_unlock_bh(&port->ozhcd->hcd_lock);
581         if (urbl)
582                 oz_free_urb_link(urbl);
583         return urbl ? 0 : -EIDRM;
584 }
585 
586 /*
587  * Finds an urb given its request id.
588  * Context: softirq
589  */
590 static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
591                 u8 req_id)
592 {
593         struct oz_hcd *ozhcd = port->ozhcd;
594         struct urb *urb = NULL;
595         struct oz_urb_link *urbl;
596         struct oz_endpoint *ep;
597 
598         spin_lock_bh(&ozhcd->hcd_lock);
599         ep = port->out_ep[ep_ix];
600         if (ep) {
601                 struct list_head *e;
602                 list_for_each(e, &ep->urb_list) {
603                         urbl = container_of(e, struct oz_urb_link, link);
604                         if (urbl->req_id == req_id) {
605                                 urb = urbl->urb;
606                                 list_del_init(e);
607                                 break;
608                         }
609                 }
610         }
611         spin_unlock_bh(&ozhcd->hcd_lock);
612         /* If urb is non-zero then we we must have an urb link to delete.
613          */
614         if (urb)
615                 oz_free_urb_link(urbl);
616         return urb;
617 }
618 
619 /*
620  * Pre-condition: Port lock must be held.
621  * Context: softirq
622  */
623 static void oz_acquire_port(struct oz_port *port, void *hpd)
624 {
625         INIT_LIST_HEAD(&port->isoc_out_ep);
626         INIT_LIST_HEAD(&port->isoc_in_ep);
627         port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
628         port->status |= USB_PORT_STAT_CONNECTION |
629                         (USB_PORT_STAT_C_CONNECTION << 16);
630         oz_usb_get(hpd);
631         port->hpd = hpd;
632 }
633 
634 /*
635  * Context: softirq
636  */
637 static struct oz_hcd *oz_hcd_claim(void)
638 {
639         struct oz_hcd *ozhcd;
640 
641         spin_lock_bh(&g_hcdlock);
642         ozhcd = g_ozhcd;
643         if (ozhcd)
644                 usb_get_hcd(ozhcd->hcd);
645         spin_unlock_bh(&g_hcdlock);
646         return ozhcd;
647 }
648 
649 /*
650  * Context: softirq
651  */
652 static inline void oz_hcd_put(struct oz_hcd *ozhcd)
653 {
654         if (ozhcd)
655                 usb_put_hcd(ozhcd->hcd);
656 }
657 
658 /*
659  * This is called by the protocol handler to notify that a PD has arrived.
660  * We allocate a port to associate with the PD and create a structure for
661  * endpoint 0. This port is made the connection port.
662  * In the event that one of the other port is already a connection port then
663  * we fail.
664  * TODO We should be able to do better than fail and should be able remember
665  * that this port needs configuring and make it the connection port once the
666  * current connection port has been assigned an address. Collisions here are
667  * probably very rare indeed.
668  * Context: softirq
669  */
670 struct oz_port *oz_hcd_pd_arrived(void *hpd)
671 {
672         int i;
673         struct oz_port *hport;
674         struct oz_hcd *ozhcd;
675         struct oz_endpoint *ep;
676 
677         ozhcd = oz_hcd_claim();
678         if (!ozhcd)
679                 return NULL;
680         /* Allocate an endpoint object in advance (before holding hcd lock) to
681          * use for out endpoint 0.
682          */
683         ep = oz_ep_alloc(0, GFP_ATOMIC);
684         if (!ep)
685                 goto err_put;
686 
687         spin_lock_bh(&ozhcd->hcd_lock);
688         if (ozhcd->conn_port >= 0)
689                 goto err_unlock;
690 
691         for (i = 0; i < OZ_NB_PORTS; i++) {
692                 struct oz_port *port = &ozhcd->ports[i];
693 
694                 spin_lock(&port->port_lock);
695                 if (!(port->flags & (OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED))) {
696                         oz_acquire_port(port, hpd);
697                         spin_unlock(&port->port_lock);
698                         break;
699                 }
700                 spin_unlock(&port->port_lock);
701         }
702         if (i == OZ_NB_PORTS)
703                 goto err_unlock;
704 
705         ozhcd->conn_port = i;
706         hport = &ozhcd->ports[i];
707         hport->out_ep[0] = ep;
708         spin_unlock_bh(&ozhcd->hcd_lock);
709         if (ozhcd->flags & OZ_HDC_F_SUSPENDED)
710                 usb_hcd_resume_root_hub(ozhcd->hcd);
711         usb_hcd_poll_rh_status(ozhcd->hcd);
712         oz_hcd_put(ozhcd);
713 
714         return hport;
715 
716 err_unlock:
717         spin_unlock_bh(&ozhcd->hcd_lock);
718         oz_ep_free(NULL, ep);
719 err_put:
720         oz_hcd_put(ozhcd);
721         return NULL;
722 }
723 
724 /*
725  * This is called by the protocol handler to notify that the PD has gone away.
726  * We need to deallocate all resources and then request that the root hub is
727  * polled. We release the reference we hold on the PD.
728  * Context: softirq
729  */
730 void oz_hcd_pd_departed(struct oz_port *port)
731 {
732         struct oz_hcd *ozhcd;
733         void *hpd;
734         struct oz_endpoint *ep = NULL;
735 
736         if (port == NULL) {
737                 oz_dbg(ON, "%s: port = 0\n", __func__);
738                 return;
739         }
740         ozhcd = port->ozhcd;
741         if (ozhcd == NULL)
742                 return;
743         /* Check if this is the connection port - if so clear it.
744          */
745         spin_lock_bh(&ozhcd->hcd_lock);
746         if ((ozhcd->conn_port >= 0) &&
747                 (port == &ozhcd->ports[ozhcd->conn_port])) {
748                 oz_dbg(ON, "Clearing conn_port\n");
749                 ozhcd->conn_port = -1;
750         }
751         spin_lock(&port->port_lock);
752         port->flags |= OZ_PORT_F_DYING;
753         spin_unlock(&port->port_lock);
754         spin_unlock_bh(&ozhcd->hcd_lock);
755 
756         oz_clean_endpoints_for_config(ozhcd->hcd, port);
757         spin_lock_bh(&port->port_lock);
758         hpd = port->hpd;
759         port->hpd = NULL;
760         port->bus_addr = 0xff;
761         port->config_num = 0;
762         port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
763         port->flags |= OZ_PORT_F_CHANGED;
764         port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
765         port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
766         /* If there is an endpont 0 then clear the pointer while we hold
767          * the spinlock be we deallocate it after releasing the lock.
768          */
769         if (port->out_ep[0]) {
770                 ep = port->out_ep[0];
771                 port->out_ep[0] = NULL;
772         }
773         spin_unlock_bh(&port->port_lock);
774         if (ep)
775                 oz_ep_free(port, ep);
776         usb_hcd_poll_rh_status(ozhcd->hcd);
777         oz_usb_put(hpd);
778 }
779 
780 /*
781  * Context: softirq
782  */
783 void oz_hcd_pd_reset(void *hpd, void *hport)
784 {
785         /* Cleanup the current configuration and report reset to the core.
786          */
787         struct oz_port *port = (struct oz_port *)hport;
788         struct oz_hcd *ozhcd = port->ozhcd;
789 
790         oz_dbg(ON, "PD Reset\n");
791         spin_lock_bh(&port->port_lock);
792         port->flags |= OZ_PORT_F_CHANGED;
793         port->status |= USB_PORT_STAT_RESET;
794         port->status |= (USB_PORT_STAT_C_RESET << 16);
795         spin_unlock_bh(&port->port_lock);
796         oz_clean_endpoints_for_config(ozhcd->hcd, port);
797         usb_hcd_poll_rh_status(ozhcd->hcd);
798 }
799 
800 /*
801  * Context: softirq
802  */
803 void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
804                         int length, int offset, int total_size)
805 {
806         struct oz_port *port = (struct oz_port *)hport;
807         struct urb *urb;
808         int err = 0;
809 
810         oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
811                length, offset, total_size);
812         urb = oz_find_urb_by_id(port, 0, req_id);
813         if (!urb)
814                 return;
815         if (status == 0) {
816                 int copy_len;
817                 int required_size = urb->transfer_buffer_length;
818                 if (required_size > total_size)
819                         required_size = total_size;
820                 copy_len = required_size-offset;
821                 if (length <= copy_len)
822                         copy_len = length;
823                 memcpy(urb->transfer_buffer+offset, desc, copy_len);
824                 offset += copy_len;
825                 if (offset < required_size) {
826                         struct usb_ctrlrequest *setup =
827                                 (struct usb_ctrlrequest *)urb->setup_packet;
828                         unsigned wvalue = le16_to_cpu(setup->wValue);
829                         if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
830                                 err = -ENOMEM;
831                         else if (oz_usb_get_desc_req(port->hpd, req_id,
832                                         setup->bRequestType, (u8)(wvalue>>8),
833                                         (u8)wvalue, setup->wIndex, offset,
834                                         required_size-offset)) {
835                                 oz_dequeue_ep_urb(port, 0, 0, urb);
836                                 err = -ENOMEM;
837                         }
838                         if (err == 0)
839                                 return;
840                 }
841         }
842         urb->actual_length = total_size;
843         oz_complete_urb(port->ozhcd->hcd, urb, 0);
844 }
845 
846 /*
847  * Context: softirq
848  */
849 static void oz_display_conf_type(u8 t)
850 {
851         switch (t) {
852         case USB_REQ_GET_STATUS:
853                 oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
854                 break;
855         case USB_REQ_CLEAR_FEATURE:
856                 oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
857                 break;
858         case USB_REQ_SET_FEATURE:
859                 oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
860                 break;
861         case USB_REQ_SET_ADDRESS:
862                 oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
863                 break;
864         case USB_REQ_GET_DESCRIPTOR:
865                 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
866                 break;
867         case USB_REQ_SET_DESCRIPTOR:
868                 oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
869                 break;
870         case USB_REQ_GET_CONFIGURATION:
871                 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
872                 break;
873         case USB_REQ_SET_CONFIGURATION:
874                 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
875                 break;
876         case USB_REQ_GET_INTERFACE:
877                 oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
878                 break;
879         case USB_REQ_SET_INTERFACE:
880                 oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
881                 break;
882         case USB_REQ_SYNCH_FRAME:
883                 oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
884                 break;
885         }
886 }
887 
888 /*
889  * Context: softirq
890  */
891 static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
892                 u8 rcode, u8 config_num)
893 {
894         int rc = 0;
895         struct usb_hcd *hcd = port->ozhcd->hcd;
896 
897         if (rcode == 0) {
898                 port->config_num = config_num;
899                 oz_clean_endpoints_for_config(hcd, port);
900                 if (oz_build_endpoints_for_config(hcd, port,
901                         &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
902                         rc = -ENOMEM;
903                 }
904         } else {
905                 rc = -ENOMEM;
906         }
907         oz_complete_urb(hcd, urb, rc);
908 }
909 
910 /*
911  * Context: softirq
912  */
913 static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
914                 u8 rcode, u8 if_num, u8 alt)
915 {
916         struct usb_hcd *hcd = port->ozhcd->hcd;
917         int rc = 0;
918 
919         if ((rcode == 0) && (port->config_num > 0)) {
920                 struct usb_host_config *config;
921                 struct usb_host_interface *intf;
922                 oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
923                 oz_clean_endpoints_for_interface(hcd, port, if_num);
924                 config = &urb->dev->config[port->config_num-1];
925                 intf = &config->intf_cache[if_num]->altsetting[alt];
926                 if (oz_build_endpoints_for_interface(hcd, port, intf,
927                         GFP_ATOMIC))
928                         rc = -ENOMEM;
929                 else
930                         port->iface[if_num].alt = alt;
931         } else {
932                 rc = -ENOMEM;
933         }
934         oz_complete_urb(hcd, urb, rc);
935 }
936 
937 /*
938  * Context: softirq
939  */
940 void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
941         int data_len)
942 {
943         struct oz_port *port = (struct oz_port *)hport;
944         struct urb *urb;
945         struct usb_ctrlrequest *setup;
946         struct usb_hcd *hcd = port->ozhcd->hcd;
947         unsigned windex;
948         unsigned wvalue;
949 
950         oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
951         urb = oz_find_urb_by_id(port, 0, req_id);
952         if (!urb) {
953                 oz_dbg(ON, "URB not found\n");
954                 return;
955         }
956         setup = (struct usb_ctrlrequest *)urb->setup_packet;
957         windex = le16_to_cpu(setup->wIndex);
958         wvalue = le16_to_cpu(setup->wValue);
959         if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
960                 /* Standard requests */
961                 oz_display_conf_type(setup->bRequest);
962                 switch (setup->bRequest) {
963                 case USB_REQ_SET_CONFIGURATION:
964                         oz_hcd_complete_set_config(port, urb, rcode,
965                                 (u8)wvalue);
966                         break;
967                 case USB_REQ_SET_INTERFACE:
968                         oz_hcd_complete_set_interface(port, urb, rcode,
969                                 (u8)windex, (u8)wvalue);
970                         break;
971                 default:
972                         oz_complete_urb(hcd, urb, 0);
973                 }
974 
975         } else {
976                 int copy_len;
977                 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
978                 if (data_len) {
979                         if (data_len <= urb->transfer_buffer_length)
980                                 copy_len = data_len;
981                         else
982                                 copy_len = urb->transfer_buffer_length;
983                         memcpy(urb->transfer_buffer, data, copy_len);
984                         urb->actual_length = copy_len;
985                 }
986                 oz_complete_urb(hcd, urb, 0);
987         }
988 }
989 
990 /*
991  * Context: softirq-serialized
992  */
993 static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
994                               int data_len)
995 {
996         int space;
997         int copy_len;
998 
999         if (!ep->buffer)
1000                 return -1;
1001         space = ep->out_ix-ep->in_ix-1;
1002         if (space < 0)
1003                 space += ep->buffer_size;
1004         if (space < (data_len+1)) {
1005                 oz_dbg(ON, "Buffer full\n");
1006                 return -1;
1007         }
1008         ep->buffer[ep->in_ix] = (u8)data_len;
1009         if (++ep->in_ix == ep->buffer_size)
1010                 ep->in_ix = 0;
1011         copy_len = ep->buffer_size - ep->in_ix;
1012         if (copy_len > data_len)
1013                 copy_len = data_len;
1014         memcpy(&ep->buffer[ep->in_ix], data, copy_len);
1015 
1016         if (copy_len < data_len) {
1017                 memcpy(ep->buffer, data+copy_len, data_len-copy_len);
1018                 ep->in_ix = data_len-copy_len;
1019         } else {
1020                 ep->in_ix += copy_len;
1021         }
1022         if (ep->in_ix == ep->buffer_size)
1023                 ep->in_ix = 0;
1024         ep->buffered_units++;
1025         return 0;
1026 }
1027 
1028 /*
1029  * Context: softirq-serialized
1030  */
1031 void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
1032 {
1033         struct oz_port *port = (struct oz_port *)hport;
1034         struct oz_endpoint *ep;
1035         struct oz_hcd *ozhcd = port->ozhcd;
1036 
1037         spin_lock_bh(&ozhcd->hcd_lock);
1038         ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
1039         if (ep == NULL)
1040                 goto done;
1041         switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
1042         case USB_ENDPOINT_XFER_INT:
1043         case USB_ENDPOINT_XFER_BULK:
1044                 if (!list_empty(&ep->urb_list)) {
1045                         struct oz_urb_link *urbl =
1046                                 list_first_entry(&ep->urb_list,
1047                                         struct oz_urb_link, link);
1048                         struct urb *urb;
1049                         int copy_len;
1050                         list_del_init(&urbl->link);
1051                         spin_unlock_bh(&ozhcd->hcd_lock);
1052                         urb = urbl->urb;
1053                         oz_free_urb_link(urbl);
1054                         if (data_len <= urb->transfer_buffer_length)
1055                                 copy_len = data_len;
1056                         else
1057                                 copy_len = urb->transfer_buffer_length;
1058                         memcpy(urb->transfer_buffer, data, copy_len);
1059                         urb->actual_length = copy_len;
1060                         oz_complete_urb(port->ozhcd->hcd, urb, 0);
1061                         return;
1062                 } else {
1063                         oz_dbg(ON, "buffering frame as URB is not available\n");
1064                         oz_hcd_buffer_data(ep, data, data_len);
1065                 }
1066                 break;
1067         case USB_ENDPOINT_XFER_ISOC:
1068                 oz_hcd_buffer_data(ep, data, data_len);
1069                 break;
1070         }
1071 done:
1072         spin_unlock_bh(&ozhcd->hcd_lock);
1073 }
1074 
1075 /*
1076  * Context: unknown
1077  */
1078 static inline int oz_usb_get_frame_number(void)
1079 {
1080         return atomic_inc_return(&g_usb_frame_number);
1081 }
1082 
1083 /*
1084  * Context: softirq
1085  */
1086 int oz_hcd_heartbeat(void *hport)
1087 {
1088         int rc = 0;
1089         struct oz_port *port = (struct oz_port *)hport;
1090         struct oz_hcd *ozhcd = port->ozhcd;
1091         struct oz_urb_link *urbl;
1092         struct list_head xfr_list;
1093         struct list_head *e;
1094         struct list_head *n;
1095         struct urb *urb;
1096         struct oz_endpoint *ep;
1097         struct timespec ts, delta;
1098 
1099         getrawmonotonic(&ts);
1100         INIT_LIST_HEAD(&xfr_list);
1101         /* Check the OUT isoc endpoints to see if any URB data can be sent.
1102          */
1103         spin_lock_bh(&ozhcd->hcd_lock);
1104         list_for_each(e, &port->isoc_out_ep) {
1105                 ep = ep_from_link(e);
1106                 if (ep->credit < 0)
1107                         continue;
1108                 delta = timespec_sub(ts, ep->timestamp);
1109                 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1110                 if (ep->credit > ep->credit_ceiling)
1111                         ep->credit = ep->credit_ceiling;
1112                 ep->timestamp = ts;
1113                 while (ep->credit && !list_empty(&ep->urb_list)) {
1114                         urbl = list_first_entry(&ep->urb_list,
1115                                 struct oz_urb_link, link);
1116                         urb = urbl->urb;
1117                         if ((ep->credit + 1) < urb->number_of_packets)
1118                                 break;
1119                         ep->credit -= urb->number_of_packets;
1120                         if (ep->credit < 0)
1121                                 ep->credit = 0;
1122                         list_move_tail(&urbl->link, &xfr_list);
1123                 }
1124         }
1125         spin_unlock_bh(&ozhcd->hcd_lock);
1126         /* Send to PD and complete URBs.
1127          */
1128         list_for_each_safe(e, n, &xfr_list) {
1129                 urbl = container_of(e, struct oz_urb_link, link);
1130                 urb = urbl->urb;
1131                 list_del_init(e);
1132                 urb->error_count = 0;
1133                 urb->start_frame = oz_usb_get_frame_number();
1134                 oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
1135                 oz_free_urb_link(urbl);
1136                 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1137         }
1138         /* Check the IN isoc endpoints to see if any URBs can be completed.
1139          */
1140         spin_lock_bh(&ozhcd->hcd_lock);
1141         list_for_each(e, &port->isoc_in_ep) {
1142                 struct oz_endpoint *ep = ep_from_link(e);
1143                 if (ep->flags & OZ_F_EP_BUFFERING) {
1144                         if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
1145                                 ep->flags &= ~OZ_F_EP_BUFFERING;
1146                                 ep->credit = 0;
1147                                 ep->timestamp = ts;
1148                                 ep->start_frame = 0;
1149                         }
1150                         continue;
1151                 }
1152                 delta = timespec_sub(ts, ep->timestamp);
1153                 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1154                 ep->timestamp = ts;
1155                 while (!list_empty(&ep->urb_list)) {
1156                         struct oz_urb_link *urbl =
1157                                 list_first_entry(&ep->urb_list,
1158                                         struct oz_urb_link, link);
1159                         struct urb *urb = urbl->urb;
1160                         int len = 0;
1161                         int copy_len;
1162                         int i;
1163                         if (ep->credit  < urb->number_of_packets)
1164                                 break;
1165                         if (ep->buffered_units < urb->number_of_packets)
1166                                 break;
1167                         urb->actual_length = 0;
1168                         for (i = 0; i < urb->number_of_packets; i++) {
1169                                 len = ep->buffer[ep->out_ix];
1170                                 if (++ep->out_ix == ep->buffer_size)
1171                                         ep->out_ix = 0;
1172                                 copy_len = ep->buffer_size - ep->out_ix;
1173                                 if (copy_len > len)
1174                                         copy_len = len;
1175                                 memcpy(urb->transfer_buffer,
1176                                         &ep->buffer[ep->out_ix], copy_len);
1177                                 if (copy_len < len) {
1178                                         memcpy(urb->transfer_buffer+copy_len,
1179                                                 ep->buffer, len-copy_len);
1180                                         ep->out_ix = len-copy_len;
1181                                 } else
1182                                         ep->out_ix += copy_len;
1183                                 if (ep->out_ix == ep->buffer_size)
1184                                         ep->out_ix = 0;
1185                                 urb->iso_frame_desc[i].offset =
1186                                         urb->actual_length;
1187                                 urb->actual_length += len;
1188                                 urb->iso_frame_desc[i].actual_length = len;
1189                                 urb->iso_frame_desc[i].status = 0;
1190                         }
1191                         ep->buffered_units -= urb->number_of_packets;
1192                         urb->error_count = 0;
1193                         urb->start_frame = ep->start_frame;
1194                         ep->start_frame += urb->number_of_packets;
1195                         list_move_tail(&urbl->link, &xfr_list);
1196                         ep->credit -= urb->number_of_packets;
1197                 }
1198         }
1199         if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1200                 rc = 1;
1201         spin_unlock_bh(&ozhcd->hcd_lock);
1202         /* Complete the filled URBs.
1203          */
1204         list_for_each_safe(e, n, &xfr_list) {
1205                 urbl = container_of(e, struct oz_urb_link, link);
1206                 urb = urbl->urb;
1207                 list_del_init(e);
1208                 oz_free_urb_link(urbl);
1209                 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1210         }
1211         /* Check if there are any ep0 requests that have timed out.
1212          * If so resent to PD.
1213          */
1214         ep = port->out_ep[0];
1215         if (ep) {
1216                 struct list_head *e;
1217                 struct list_head *n;
1218                 spin_lock_bh(&ozhcd->hcd_lock);
1219                 list_for_each_safe(e, n, &ep->urb_list) {
1220                         urbl = container_of(e, struct oz_urb_link, link);
1221                         if (urbl->submit_counter > EP0_TIMEOUT_COUNTER) {
1222                                 oz_dbg(ON, "Request 0x%p timeout\n", urbl->urb);
1223                                 list_move_tail(e, &xfr_list);
1224                                 urbl->submit_counter = 0;
1225                         } else {
1226                                 urbl->submit_counter++;
1227                         }
1228                 }
1229                 if (!list_empty(&ep->urb_list))
1230                         rc = 1;
1231                 spin_unlock_bh(&ozhcd->hcd_lock);
1232                 e = xfr_list.next;
1233                 while (e != &xfr_list) {
1234                         urbl = container_of(e, struct oz_urb_link, link);
1235                         e = e->next;
1236                         oz_dbg(ON, "Resending request to PD\n");
1237                         oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1238                         oz_free_urb_link(urbl);
1239                 }
1240         }
1241         return rc;
1242 }
1243 
1244 /*
1245  * Context: softirq
1246  */
1247 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1248                 struct oz_port *port,
1249                 struct usb_host_interface *intf, gfp_t mem_flags)
1250 {
1251         struct oz_hcd *ozhcd = port->ozhcd;
1252         int i;
1253         int if_ix = intf->desc.bInterfaceNumber;
1254         int request_heartbeat = 0;
1255 
1256         oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
1257         if (if_ix >= port->num_iface || port->iface == NULL)
1258                 return -ENOMEM;
1259         for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1260                 struct usb_host_endpoint *hep = &intf->endpoint[i];
1261                 u8 ep_addr = hep->desc.bEndpointAddress;
1262                 u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1263                 struct oz_endpoint *ep;
1264                 int buffer_size = 0;
1265 
1266                 oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
1267                 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1268                         switch (hep->desc.bmAttributes &
1269                                                 USB_ENDPOINT_XFERTYPE_MASK) {
1270                         case USB_ENDPOINT_XFER_ISOC:
1271                                 buffer_size = OZ_EP_BUFFER_SIZE_ISOC;
1272                                 break;
1273                         case USB_ENDPOINT_XFER_INT:
1274                                 buffer_size = OZ_EP_BUFFER_SIZE_INT;
1275                                 break;
1276                         }
1277                 }
1278 
1279                 ep = oz_ep_alloc(buffer_size, mem_flags);
1280                 if (!ep) {
1281                         oz_clean_endpoints_for_interface(hcd, port, if_ix);
1282                         return -ENOMEM;
1283                 }
1284                 ep->attrib = hep->desc.bmAttributes;
1285                 ep->ep_num = ep_num;
1286                 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1287                         == USB_ENDPOINT_XFER_ISOC) {
1288                         oz_dbg(ON, "wMaxPacketSize = %d\n",
1289                                usb_endpoint_maxp(&hep->desc));
1290                         ep->credit_ceiling = 200;
1291                         if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1292                                 ep->flags |= OZ_F_EP_BUFFERING;
1293                         } else {
1294                                 ep->flags |= OZ_F_EP_HAVE_STREAM;
1295                                 if (oz_usb_stream_create(port->hpd, ep_num))
1296                                         ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1297                         }
1298                 }
1299                 spin_lock_bh(&ozhcd->hcd_lock);
1300                 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1301                         port->in_ep[ep_num] = ep;
1302                         port->iface[if_ix].ep_mask |=
1303                                 (1<<(ep_num+OZ_NB_ENDPOINTS));
1304                         if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1305                                  == USB_ENDPOINT_XFER_ISOC) {
1306                                 list_add_tail(&ep->link, &port->isoc_in_ep);
1307                                 request_heartbeat = 1;
1308                         }
1309                 } else {
1310                         port->out_ep[ep_num] = ep;
1311                         port->iface[if_ix].ep_mask |= (1<<ep_num);
1312                         if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1313                                 == USB_ENDPOINT_XFER_ISOC) {
1314                                 list_add_tail(&ep->link, &port->isoc_out_ep);
1315                                 request_heartbeat = 1;
1316                         }
1317                 }
1318                 spin_unlock_bh(&ozhcd->hcd_lock);
1319                 if (request_heartbeat && port->hpd)
1320                         oz_usb_request_heartbeat(port->hpd);
1321         }
1322         return 0;
1323 }
1324 
1325 /*
1326  * Context: softirq
1327  */
1328 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1329                         struct oz_port *port, int if_ix)
1330 {
1331         struct oz_hcd *ozhcd = port->ozhcd;
1332         unsigned mask;
1333         int i;
1334         struct list_head ep_list;
1335 
1336         oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
1337         if (if_ix >= port->num_iface)
1338                 return;
1339         INIT_LIST_HEAD(&ep_list);
1340         spin_lock_bh(&ozhcd->hcd_lock);
1341         mask = port->iface[if_ix].ep_mask;
1342         port->iface[if_ix].ep_mask = 0;
1343         for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1344                 struct list_head *e;
1345                 /* Gather OUT endpoints.
1346                  */
1347                 if ((mask & (1<<i)) && port->out_ep[i]) {
1348                         e = &port->out_ep[i]->link;
1349                         port->out_ep[i] = NULL;
1350                         /* Remove from isoc list if present.
1351                          */
1352                         list_move_tail(e, &ep_list);
1353                 }
1354                 /* Gather IN endpoints.
1355                  */
1356                 if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1357                         e = &port->in_ep[i]->link;
1358                         port->in_ep[i] = NULL;
1359                         list_move_tail(e, &ep_list);
1360                 }
1361         }
1362         spin_unlock_bh(&ozhcd->hcd_lock);
1363         while (!list_empty(&ep_list)) {
1364                 struct oz_endpoint *ep =
1365                         list_first_entry(&ep_list, struct oz_endpoint, link);
1366                 list_del_init(&ep->link);
1367                 oz_ep_free(port, ep);
1368         }
1369 }
1370 
1371 /*
1372  * Context: softirq
1373  */
1374 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1375                 struct oz_port *port, struct usb_host_config *config,
1376                 gfp_t mem_flags)
1377 {
1378         struct oz_hcd *ozhcd = port->ozhcd;
1379         int i;
1380         int num_iface = config->desc.bNumInterfaces;
1381 
1382         if (num_iface) {
1383                 struct oz_interface *iface;
1384 
1385                 iface = kmalloc(num_iface*sizeof(struct oz_interface),
1386                                 mem_flags | __GFP_ZERO);
1387                 if (!iface)
1388                         return -ENOMEM;
1389                 spin_lock_bh(&ozhcd->hcd_lock);
1390                 port->iface = iface;
1391                 port->num_iface = num_iface;
1392                 spin_unlock_bh(&ozhcd->hcd_lock);
1393         }
1394         for (i = 0; i < num_iface; i++) {
1395                 struct usb_host_interface *intf =
1396                         &config->intf_cache[i]->altsetting[0];
1397                 if (oz_build_endpoints_for_interface(hcd, port, intf,
1398                         mem_flags))
1399                         goto fail;
1400         }
1401         return 0;
1402 fail:
1403         oz_clean_endpoints_for_config(hcd, port);
1404         return -1;
1405 }
1406 
1407 /*
1408  * Context: softirq
1409  */
1410 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1411                         struct oz_port *port)
1412 {
1413         struct oz_hcd *ozhcd = port->ozhcd;
1414         int i;
1415 
1416         oz_dbg(ON, "Deleting endpoints for configuration\n");
1417         for (i = 0; i < port->num_iface; i++)
1418                 oz_clean_endpoints_for_interface(hcd, port, i);
1419         spin_lock_bh(&ozhcd->hcd_lock);
1420         if (port->iface) {
1421                 oz_dbg(ON, "Freeing interfaces object\n");
1422                 kfree(port->iface);
1423                 port->iface = NULL;
1424         }
1425         port->num_iface = 0;
1426         spin_unlock_bh(&ozhcd->hcd_lock);
1427 }
1428 
1429 /*
1430  * Context: tasklet
1431  */
1432 static void *oz_claim_hpd(struct oz_port *port)
1433 {
1434         void *hpd;
1435         struct oz_hcd *ozhcd = port->ozhcd;
1436 
1437         spin_lock_bh(&ozhcd->hcd_lock);
1438         hpd = port->hpd;
1439         if (hpd)
1440                 oz_usb_get(hpd);
1441         spin_unlock_bh(&ozhcd->hcd_lock);
1442         return hpd;
1443 }
1444 
1445 /*
1446  * Context: tasklet
1447  */
1448 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1449                 gfp_t mem_flags)
1450 {
1451         struct usb_ctrlrequest *setup;
1452         unsigned windex;
1453         unsigned wvalue;
1454         unsigned wlength;
1455         void *hpd;
1456         u8 req_id;
1457         int rc = 0;
1458         unsigned complete = 0;
1459 
1460         int port_ix = -1;
1461         struct oz_port *port = NULL;
1462 
1463         oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
1464         port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1465         if (port_ix < 0) {
1466                 rc = -EPIPE;
1467                 goto out;
1468         }
1469         port =  &ozhcd->ports[port_ix];
1470         if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1471                 || (port->flags & OZ_PORT_F_DYING)) {
1472                 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1473                        port_ix, urb->dev->devnum);
1474                 rc = -EPIPE;
1475                 goto out;
1476         }
1477         /* Store port in private context data.
1478          */
1479         urb->hcpriv = port;
1480         setup = (struct usb_ctrlrequest *)urb->setup_packet;
1481         windex = le16_to_cpu(setup->wIndex);
1482         wvalue = le16_to_cpu(setup->wValue);
1483         wlength = le16_to_cpu(setup->wLength);
1484         oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
1485         oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1486         oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
1487         oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
1488         oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
1489 
1490         req_id = port->next_req_id++;
1491         hpd = oz_claim_hpd(port);
1492         if (hpd == NULL) {
1493                 oz_dbg(ON, "Cannot claim port\n");
1494                 rc = -EPIPE;
1495                 goto out;
1496         }
1497 
1498         if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1499                 /* Standard requests
1500                  */
1501                 switch (setup->bRequest) {
1502                 case USB_REQ_GET_DESCRIPTOR:
1503                         oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
1504                         break;
1505                 case USB_REQ_SET_ADDRESS:
1506                         oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
1507                         oz_dbg(ON, "Port %d address is 0x%x\n",
1508                                ozhcd->conn_port,
1509                                (u8)le16_to_cpu(setup->wValue));
1510                         spin_lock_bh(&ozhcd->hcd_lock);
1511                         if (ozhcd->conn_port >= 0) {
1512                                 ozhcd->ports[ozhcd->conn_port].bus_addr =
1513                                         (u8)le16_to_cpu(setup->wValue);
1514                                 oz_dbg(ON, "Clearing conn_port\n");
1515                                 ozhcd->conn_port = -1;
1516                         }
1517                         spin_unlock_bh(&ozhcd->hcd_lock);
1518                         complete = 1;
1519                         break;
1520                 case USB_REQ_SET_CONFIGURATION:
1521                         oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
1522                         break;
1523                 case USB_REQ_GET_CONFIGURATION:
1524                         /* We short circuit this case and reply directly since
1525                          * we have the selected configuration number cached.
1526                          */
1527                         oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
1528                         if (urb->transfer_buffer_length >= 1) {
1529                                 urb->actual_length = 1;
1530                                 *((u8 *)urb->transfer_buffer) =
1531                                         port->config_num;
1532                                 complete = 1;
1533                         } else {
1534                                 rc = -EPIPE;
1535                         }
1536                         break;
1537                 case USB_REQ_GET_INTERFACE:
1538                         /* We short circuit this case and reply directly since
1539                          * we have the selected interface alternative cached.
1540                          */
1541                         oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
1542                         if (urb->transfer_buffer_length >= 1) {
1543                                 urb->actual_length = 1;
1544                                 *((u8 *)urb->transfer_buffer) =
1545                                         port->iface[(u8)windex].alt;
1546                                 oz_dbg(ON, "interface = %d alt = %d\n",
1547                                        windex, port->iface[(u8)windex].alt);
1548                                 complete = 1;
1549                         } else {
1550                                 rc = -EPIPE;
1551                         }
1552                         break;
1553                 case USB_REQ_SET_INTERFACE:
1554                         oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
1555                         break;
1556                 }
1557         }
1558         if (!rc && !complete) {
1559                 int data_len = 0;
1560                 if ((setup->bRequestType & USB_DIR_IN) == 0)
1561                         data_len = wlength;
1562                 urb->actual_length = data_len;
1563                 if (oz_usb_control_req(port->hpd, req_id, setup,
1564                                 urb->transfer_buffer, data_len)) {
1565                         rc = -ENOMEM;
1566                 } else {
1567                         /* Note: we are queuing the request after we have
1568                          * submitted it to be transmitted. If the request were
1569                          * to complete before we queued it then it would not
1570                          * be found in the queue. It seems impossible for
1571                          * this to happen but if it did the request would
1572                          * be resubmitted so the problem would hopefully
1573                          * resolve itself. Putting the request into the
1574                          * queue before it has been sent is worse since the
1575                          * urb could be cancelled while we are using it
1576                          * to build the request.
1577                          */
1578                         if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1579                                 rc = -ENOMEM;
1580                 }
1581         }
1582         oz_usb_put(hpd);
1583 out:
1584         if (rc || complete) {
1585                 oz_dbg(ON, "Completing request locally\n");
1586                 oz_complete_urb(ozhcd->hcd, urb, rc);
1587         } else {
1588                 oz_usb_request_heartbeat(port->hpd);
1589         }
1590 }
1591 
1592 /*
1593  * Context: tasklet
1594  */
1595 static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1596 {
1597         int rc = 0;
1598         struct oz_port *port = urb->hcpriv;
1599         u8 ep_addr;
1600 
1601         /* When we are paranoid we keep a list of urbs which we check against
1602          * before handing one back. This is just for debugging during
1603          * development and should be turned off in the released driver.
1604          */
1605         oz_remember_urb(urb);
1606         /* Check buffer is valid.
1607          */
1608         if (!urb->transfer_buffer && urb->transfer_buffer_length)
1609                 return -EINVAL;
1610         /* Check if there is a device at the port - refuse if not.
1611          */
1612         if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1613                 return -EPIPE;
1614         ep_addr = usb_pipeendpoint(urb->pipe);
1615         if (ep_addr) {
1616                 /* If the request is not for EP0 then queue it.
1617                  */
1618                 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1619                         urb, 0))
1620                         rc = -EPIPE;
1621         } else {
1622                 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1623         }
1624         return rc;
1625 }
1626 
1627 /*
1628  * Context: tasklet
1629  */
1630 static void oz_urb_process_tasklet(unsigned long unused)
1631 {
1632         unsigned long irq_state;
1633         struct urb *urb;
1634         struct oz_hcd *ozhcd = oz_hcd_claim();
1635         int rc = 0;
1636 
1637         if (ozhcd == NULL)
1638                 return;
1639         /* This is called from a tasklet so is in softirq context but the urb
1640          * list is filled from any context so we need to lock
1641          * appropriately while removing urbs.
1642          */
1643         spin_lock_irqsave(&g_tasklet_lock, irq_state);
1644         while (!list_empty(&ozhcd->urb_pending_list)) {
1645                 struct oz_urb_link *urbl =
1646                         list_first_entry(&ozhcd->urb_pending_list,
1647                                 struct oz_urb_link, link);
1648                 list_del_init(&urbl->link);
1649                 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1650                 urb = urbl->urb;
1651                 oz_free_urb_link(urbl);
1652                 rc = oz_urb_process(ozhcd, urb);
1653                 if (rc)
1654                         oz_complete_urb(ozhcd->hcd, urb, rc);
1655                 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1656         }
1657         spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1658         oz_hcd_put(ozhcd);
1659 }
1660 
1661 /*
1662  * This function searches for the urb in any of the lists it could be in.
1663  * If it is found it is removed from the list and completed. If the urb is
1664  * being processed then it won't be in a list so won't be found. However, the
1665  * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1666  * to a non-zero value. When an attempt is made to put the urb back in a list
1667  * the unlinked field will be checked and the urb will then be completed.
1668  * Context: tasklet
1669  */
1670 static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1671 {
1672         struct oz_urb_link *urbl = NULL;
1673         struct list_head *e;
1674         struct oz_hcd *ozhcd;
1675         unsigned long irq_state;
1676         u8 ix;
1677 
1678         if (port == NULL) {
1679                 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
1680                 return;
1681         }
1682         ozhcd = port->ozhcd;
1683         if (ozhcd == NULL) {
1684                 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
1685                 return;
1686         }
1687 
1688         /* Look in the tasklet queue.
1689          */
1690         spin_lock_irqsave(&g_tasklet_lock, irq_state);
1691         list_for_each(e, &ozhcd->urb_cancel_list) {
1692                 urbl = container_of(e, struct oz_urb_link, link);
1693                 if (urb == urbl->urb) {
1694                         list_del_init(e);
1695                         spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1696                         goto out2;
1697                 }
1698         }
1699         spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1700         urbl = NULL;
1701 
1702         /* Look in the orphanage.
1703          */
1704         spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1705         list_for_each(e, &ozhcd->orphanage) {
1706                 urbl = container_of(e, struct oz_urb_link, link);
1707                 if (urbl->urb == urb) {
1708                         list_del(e);
1709                         oz_dbg(ON, "Found urb in orphanage\n");
1710                         goto out;
1711                 }
1712         }
1713         ix = (ep_num & 0xf);
1714         urbl = NULL;
1715         if ((ep_num & USB_DIR_IN) && ix)
1716                 urbl = oz_remove_urb(port->in_ep[ix], urb);
1717         else
1718                 urbl = oz_remove_urb(port->out_ep[ix], urb);
1719 out:
1720         spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1721 out2:
1722         if (urbl) {
1723                 urb->actual_length = 0;
1724                 oz_free_urb_link(urbl);
1725                 oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
1726         }
1727 }
1728 
1729 /*
1730  * Context: tasklet
1731  */
1732 static void oz_urb_cancel_tasklet(unsigned long unused)
1733 {
1734         unsigned long irq_state;
1735         struct urb *urb;
1736         struct oz_hcd *ozhcd = oz_hcd_claim();
1737 
1738         if (ozhcd == NULL)
1739                 return;
1740         spin_lock_irqsave(&g_tasklet_lock, irq_state);
1741         while (!list_empty(&ozhcd->urb_cancel_list)) {
1742                 struct oz_urb_link *urbl =
1743                         list_first_entry(&ozhcd->urb_cancel_list,
1744                                 struct oz_urb_link, link);
1745                 list_del_init(&urbl->link);
1746                 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1747                 urb = urbl->urb;
1748                 if (urb->unlinked)
1749                         oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1750                 oz_free_urb_link(urbl);
1751                 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1752         }
1753         spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1754         oz_hcd_put(ozhcd);
1755 }
1756 
1757 /*
1758  * Context: unknown
1759  */
1760 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1761 {
1762         if (ozhcd) {
1763                 struct oz_urb_link *urbl;
1764                 while (!list_empty(&ozhcd->orphanage)) {
1765                         urbl = list_first_entry(&ozhcd->orphanage,
1766                                 struct oz_urb_link, link);
1767                         list_del(&urbl->link);
1768                         oz_complete_urb(ozhcd->hcd, urbl->urb, status);
1769                         oz_free_urb_link(urbl);
1770                 }
1771         }
1772 }
1773 
1774 /*
1775  * Context: unknown
1776  */
1777 static int oz_hcd_start(struct usb_hcd *hcd)
1778 {
1779         hcd->power_budget = 200;
1780         hcd->state = HC_STATE_RUNNING;
1781         hcd->uses_new_polling = 1;
1782         return 0;
1783 }
1784 
1785 /*
1786  * Context: unknown
1787  */
1788 static void oz_hcd_stop(struct usb_hcd *hcd)
1789 {
1790 }
1791 
1792 /*
1793  * Context: unknown
1794  */
1795 static void oz_hcd_shutdown(struct usb_hcd *hcd)
1796 {
1797 }
1798 
1799 /*
1800  * Called to queue an urb for the device.
1801  * This function should return a non-zero error code if it fails the urb but
1802  * should not call usb_hcd_giveback_urb().
1803  * Context: any
1804  */
1805 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1806                                 gfp_t mem_flags)
1807 {
1808         struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1809         int rc;
1810         int port_ix;
1811         struct oz_port *port;
1812         unsigned long irq_state;
1813         struct oz_urb_link *urbl;
1814 
1815         oz_dbg(URB, "%s: (%p)\n",  __func__, urb);
1816         if (unlikely(ozhcd == NULL)) {
1817                 oz_dbg(URB, "Refused urb(%p) not ozhcd\n", urb);
1818                 return -EPIPE;
1819         }
1820         if (unlikely(hcd->state != HC_STATE_RUNNING)) {
1821                 oz_dbg(URB, "Refused urb(%p) not running\n", urb);
1822                 return -EPIPE;
1823         }
1824         port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1825         if (port_ix < 0)
1826                 return -EPIPE;
1827         port =  &ozhcd->ports[port_ix];
1828         if (port == NULL)
1829                 return -EPIPE;
1830         if (!(port->flags & OZ_PORT_F_PRESENT) ||
1831                                 (port->flags & OZ_PORT_F_CHANGED)) {
1832                 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1833                        port_ix, urb->dev->devnum);
1834                 return -EPIPE;
1835         }
1836         urb->hcpriv = port;
1837         /* Put request in queue for processing by tasklet.
1838          */
1839         urbl = oz_alloc_urb_link();
1840         if (unlikely(urbl == NULL))
1841                 return -ENOMEM;
1842         urbl->urb = urb;
1843         spin_lock_irqsave(&g_tasklet_lock, irq_state);
1844         rc = usb_hcd_link_urb_to_ep(hcd, urb);
1845         if (unlikely(rc)) {
1846                 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1847                 oz_free_urb_link(urbl);
1848                 return rc;
1849         }
1850         list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1851         spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1852         tasklet_schedule(&g_urb_process_tasklet);
1853         atomic_inc(&g_pending_urbs);
1854         return 0;
1855 }
1856 
1857 /*
1858  * Context: tasklet
1859  */
1860 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1861                                 struct urb *urb)
1862 {
1863         struct oz_urb_link *urbl;
1864         struct list_head *e;
1865 
1866         if (unlikely(ep == NULL))
1867                 return NULL;
1868         list_for_each(e, &ep->urb_list) {
1869                 urbl = container_of(e, struct oz_urb_link, link);
1870                 if (urbl->urb == urb) {
1871                         list_del_init(e);
1872                         if (usb_pipeisoc(urb->pipe)) {
1873                                 ep->credit -= urb->number_of_packets;
1874                                 if (ep->credit < 0)
1875                                         ep->credit = 0;
1876                         }
1877                         return urbl;
1878                 }
1879         }
1880         return NULL;
1881 }
1882 
1883 /*
1884  * Called to dequeue a previously submitted urb for the device.
1885  * Context: any
1886  */
1887 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1888 {
1889         struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1890         struct oz_urb_link *urbl;
1891         int rc;
1892         unsigned long irq_state;
1893 
1894         oz_dbg(URB, "%s: (%p)\n",  __func__, urb);
1895         urbl = oz_alloc_urb_link();
1896         if (unlikely(urbl == NULL))
1897                 return -ENOMEM;
1898         spin_lock_irqsave(&g_tasklet_lock, irq_state);
1899         /* The following function checks the urb is still in the queue
1900          * maintained by the core and that the unlinked field is zero.
1901          * If both are true the function sets the unlinked field and returns
1902          * zero. Otherwise it returns an error.
1903          */
1904         rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1905         /* We have to check we haven't completed the urb or are about
1906          * to complete it. When we do we set hcpriv to 0 so if this has
1907          * already happened we don't put the urb in the cancel queue.
1908          */
1909         if ((rc == 0) && urb->hcpriv) {
1910                 urbl->urb = urb;
1911                 urbl->port = (struct oz_port *)urb->hcpriv;
1912                 urbl->ep_num = usb_pipeendpoint(urb->pipe);
1913                 if (usb_pipein(urb->pipe))
1914                         urbl->ep_num |= USB_DIR_IN;
1915                 list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1916                 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1917                 tasklet_schedule(&g_urb_cancel_tasklet);
1918         } else {
1919                 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1920                 oz_free_urb_link(urbl);
1921         }
1922         return rc;
1923 }
1924 
1925 /*
1926  * Context: unknown
1927  */
1928 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1929                                 struct usb_host_endpoint *ep)
1930 {
1931 }
1932 
1933 /*
1934  * Context: unknown
1935  */
1936 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1937                                 struct usb_host_endpoint *ep)
1938 {
1939 }
1940 
1941 /*
1942  * Context: unknown
1943  */
1944 static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1945 {
1946         oz_dbg(ON, "oz_hcd_get_frame_number\n");
1947         return oz_usb_get_frame_number();
1948 }
1949 
1950 /*
1951  * Context: softirq
1952  * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1953  * always do that in softirq context.
1954  */
1955 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1956 {
1957         struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1958         int i;
1959 
1960         buf[0] = 0;
1961         buf[1] = 0;
1962 
1963         spin_lock_bh(&ozhcd->hcd_lock);
1964         for (i = 0; i < OZ_NB_PORTS; i++) {
1965                 if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
1966                         oz_dbg(HUB, "Port %d changed\n", i);
1967                         ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
1968                         if (i < 7)
1969                                 buf[0] |= 1 << (i + 1);
1970                         else
1971                                 buf[1] |= 1 << (i - 7);
1972                 }
1973         }
1974         spin_unlock_bh(&ozhcd->hcd_lock);
1975         if (buf[0] != 0 || buf[1] != 0)
1976                 return 2;
1977         else
1978                 return 0;
1979 }
1980 
1981 /*
1982  * Context: process
1983  */
1984 static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1985                                 struct usb_hub_descriptor *desc)
1986 {
1987         memset(desc, 0, sizeof(*desc));
1988         desc->bDescriptorType = 0x29;
1989         desc->bDescLength = 9;
1990         desc->wHubCharacteristics = (__force __u16)cpu_to_le16(0x0001);
1991         desc->bNbrPorts = OZ_NB_PORTS;
1992 }
1993 
1994 /*
1995  * Context: process
1996  */
1997 static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1998 {
1999         struct oz_port *port;
2000         int err = 0;
2001         u8 port_id = (u8)windex;
2002         struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2003         unsigned set_bits = 0;
2004         unsigned clear_bits = 0;
2005 
2006         if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2007                 return -EPIPE;
2008         port = &ozhcd->ports[port_id-1];
2009         switch (wvalue) {
2010         case USB_PORT_FEAT_CONNECTION:
2011                 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
2012                 break;
2013         case USB_PORT_FEAT_ENABLE:
2014                 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
2015                 break;
2016         case USB_PORT_FEAT_SUSPEND:
2017                 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
2018                 break;
2019         case USB_PORT_FEAT_OVER_CURRENT:
2020                 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2021                 break;
2022         case USB_PORT_FEAT_RESET:
2023                 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
2024                 set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
2025                 clear_bits = USB_PORT_STAT_RESET;
2026                 ozhcd->ports[port_id-1].bus_addr = 0;
2027                 break;
2028         case USB_PORT_FEAT_POWER:
2029                 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2030                 set_bits |= USB_PORT_STAT_POWER;
2031                 break;
2032         case USB_PORT_FEAT_LOWSPEED:
2033                 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2034                 break;
2035         case USB_PORT_FEAT_C_CONNECTION:
2036                 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2037                 break;
2038         case USB_PORT_FEAT_C_ENABLE:
2039                 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2040                 break;
2041         case USB_PORT_FEAT_C_SUSPEND:
2042                 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2043                 break;
2044         case USB_PORT_FEAT_C_OVER_CURRENT:
2045                 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2046                 break;
2047         case USB_PORT_FEAT_C_RESET:
2048                 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2049                 break;
2050         case USB_PORT_FEAT_TEST:
2051                 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2052                 break;
2053         case USB_PORT_FEAT_INDICATOR:
2054                 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2055                 break;
2056         default:
2057                 oz_dbg(HUB, "Other %d\n", wvalue);
2058                 break;
2059         }
2060         if (set_bits || clear_bits) {
2061                 spin_lock_bh(&port->port_lock);
2062                 port->status &= ~clear_bits;
2063                 port->status |= set_bits;
2064                 spin_unlock_bh(&port->port_lock);
2065         }
2066         oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
2067         return err;
2068 }
2069 
2070 /*
2071  * Context: process
2072  */
2073 static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
2074 {
2075         struct oz_port *port;
2076         int err = 0;
2077         u8 port_id = (u8)windex;
2078         struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2079         unsigned clear_bits = 0;
2080 
2081         if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2082                 return -EPIPE;
2083         port = &ozhcd->ports[port_id-1];
2084         switch (wvalue) {
2085         case USB_PORT_FEAT_CONNECTION:
2086                 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
2087                 break;
2088         case USB_PORT_FEAT_ENABLE:
2089                 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
2090                 clear_bits = USB_PORT_STAT_ENABLE;
2091                 break;
2092         case USB_PORT_FEAT_SUSPEND:
2093                 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
2094                 break;
2095         case USB_PORT_FEAT_OVER_CURRENT:
2096                 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2097                 break;
2098         case USB_PORT_FEAT_RESET:
2099                 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
2100                 break;
2101         case USB_PORT_FEAT_POWER:
2102                 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2103                 clear_bits |= USB_PORT_STAT_POWER;
2104                 break;
2105         case USB_PORT_FEAT_LOWSPEED:
2106                 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2107                 break;
2108         case USB_PORT_FEAT_C_CONNECTION:
2109                 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2110                 clear_bits = (USB_PORT_STAT_C_CONNECTION << 16);
2111                 break;
2112         case USB_PORT_FEAT_C_ENABLE:
2113                 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2114                 clear_bits = (USB_PORT_STAT_C_ENABLE << 16);
2115                 break;
2116         case USB_PORT_FEAT_C_SUSPEND:
2117                 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2118                 break;
2119         case USB_PORT_FEAT_C_OVER_CURRENT:
2120                 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2121                 break;
2122         case USB_PORT_FEAT_C_RESET:
2123                 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2124                 clear_bits = (USB_PORT_FEAT_C_RESET << 16);
2125                 break;
2126         case USB_PORT_FEAT_TEST:
2127                 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2128                 break;
2129         case USB_PORT_FEAT_INDICATOR:
2130                 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2131                 break;
2132         default:
2133                 oz_dbg(HUB, "Other %d\n", wvalue);
2134                 break;
2135         }
2136         if (clear_bits) {
2137                 spin_lock_bh(&port->port_lock);
2138                 port->status &= ~clear_bits;
2139                 spin_unlock_bh(&port->port_lock);
2140         }
2141         oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2142                port_id, ozhcd->ports[port_id-1].status);
2143         return err;
2144 }
2145 
2146 /*
2147  * Context: process
2148  */
2149 static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2150 {
2151         struct oz_hcd *ozhcd;
2152         u32 status;
2153 
2154         if ((windex < 1) || (windex > OZ_NB_PORTS))
2155                 return -EPIPE;
2156         ozhcd = oz_hcd_private(hcd);
2157         oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
2158         status = ozhcd->ports[windex-1].status;
2159         put_unaligned(cpu_to_le32(status), (__le32 *)buf);
2160         oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
2161         return 0;
2162 }
2163 
2164 /*
2165  * Context: process
2166  */
2167 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2168                                 u16 windex, char *buf, u16 wlength)
2169 {
2170         int err = 0;
2171 
2172         switch (req_type) {
2173         case ClearHubFeature:
2174                 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
2175                 break;
2176         case ClearPortFeature:
2177                 err = oz_clear_port_feature(hcd, wvalue, windex);
2178                 break;
2179         case GetHubDescriptor:
2180                 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2181                 break;
2182         case GetHubStatus:
2183                 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
2184                 put_unaligned(cpu_to_le32(0), (__le32 *)buf);
2185                 break;
2186         case GetPortStatus:
2187                 err = oz_get_port_status(hcd, windex, buf);
2188                 break;
2189         case SetHubFeature:
2190                 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
2191                 break;
2192         case SetPortFeature:
2193                 err = oz_set_port_feature(hcd, wvalue, windex);
2194                 break;
2195         default:
2196                 oz_dbg(HUB, "Other: %d\n", req_type);
2197                 break;
2198         }
2199         return err;
2200 }
2201 
2202 /*
2203  * Context: process
2204  */
2205 static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2206 {
2207         struct oz_hcd *ozhcd;
2208 
2209         ozhcd = oz_hcd_private(hcd);
2210         spin_lock_bh(&ozhcd->hcd_lock);
2211         hcd->state = HC_STATE_SUSPENDED;
2212         ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2213         spin_unlock_bh(&ozhcd->hcd_lock);
2214         return 0;
2215 }
2216 
2217 /*
2218  * Context: process
2219  */
2220 static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2221 {
2222         struct oz_hcd *ozhcd;
2223 
2224         ozhcd = oz_hcd_private(hcd);
2225         spin_lock_bh(&ozhcd->hcd_lock);
2226         ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2227         hcd->state = HC_STATE_RUNNING;
2228         spin_unlock_bh(&ozhcd->hcd_lock);
2229         return 0;
2230 }
2231 
2232 static void oz_plat_shutdown(struct platform_device *dev)
2233 {
2234 }
2235 
2236 /*
2237  * Context: process
2238  */
2239 static int oz_plat_probe(struct platform_device *dev)
2240 {
2241         int i;
2242         int err;
2243         struct usb_hcd *hcd;
2244         struct oz_hcd *ozhcd;
2245 
2246         hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
2247         if (hcd == NULL) {
2248                 oz_dbg(ON, "Failed to created hcd object OK\n");
2249                 return -ENOMEM;
2250         }
2251         ozhcd = oz_hcd_private(hcd);
2252         memset(ozhcd, 0, sizeof(*ozhcd));
2253         INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2254         INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2255         INIT_LIST_HEAD(&ozhcd->orphanage);
2256         ozhcd->hcd = hcd;
2257         ozhcd->conn_port = -1;
2258         spin_lock_init(&ozhcd->hcd_lock);
2259         for (i = 0; i < OZ_NB_PORTS; i++) {
2260                 struct oz_port *port = &ozhcd->ports[i];
2261                 port->ozhcd = ozhcd;
2262                 port->flags = 0;
2263                 port->status = 0;
2264                 port->bus_addr = 0xff;
2265                 spin_lock_init(&port->port_lock);
2266         }
2267         err = usb_add_hcd(hcd, 0, 0);
2268         if (err) {
2269                 oz_dbg(ON, "Failed to add hcd object OK\n");
2270                 usb_put_hcd(hcd);
2271                 return -1;
2272         }
2273         device_wakeup_enable(hcd->self.controller);
2274 
2275         spin_lock_bh(&g_hcdlock);
2276         g_ozhcd = ozhcd;
2277         spin_unlock_bh(&g_hcdlock);
2278         return 0;
2279 }
2280 
2281 /*
2282  * Context: unknown
2283  */
2284 static int oz_plat_remove(struct platform_device *dev)
2285 {
2286         struct usb_hcd *hcd = platform_get_drvdata(dev);
2287         struct oz_hcd *ozhcd;
2288 
2289         if (hcd == NULL)
2290                 return -1;
2291         ozhcd = oz_hcd_private(hcd);
2292         spin_lock_bh(&g_hcdlock);
2293         if (ozhcd == g_ozhcd)
2294                 g_ozhcd = NULL;
2295         spin_unlock_bh(&g_hcdlock);
2296         oz_dbg(ON, "Clearing orphanage\n");
2297         oz_hcd_clear_orphanage(ozhcd, -EPIPE);
2298         oz_dbg(ON, "Removing hcd\n");
2299         usb_remove_hcd(hcd);
2300         usb_put_hcd(hcd);
2301         oz_empty_link_pool();
2302         return 0;
2303 }
2304 
2305 /*
2306  * Context: unknown
2307  */
2308 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2309 {
2310         return 0;
2311 }
2312 
2313 
2314 /*
2315  * Context: unknown
2316  */
2317 static int oz_plat_resume(struct platform_device *dev)
2318 {
2319         return 0;
2320 }
2321 
2322 /*
2323  * Context: process
2324  */
2325 int oz_hcd_init(void)
2326 {
2327         int err;
2328 
2329         if (usb_disabled())
2330                 return -ENODEV;
2331         tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
2332         tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
2333         err = platform_driver_register(&g_oz_plat_drv);
2334         oz_dbg(ON, "platform_driver_register() returned %d\n", err);
2335         if (err)
2336                 goto error;
2337         g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
2338         if (g_plat_dev == NULL) {
2339                 err = -ENOMEM;
2340                 goto error1;
2341         }
2342         oz_dbg(ON, "platform_device_alloc() succeeded\n");
2343         err = platform_device_add(g_plat_dev);
2344         if (err)
2345                 goto error2;
2346         oz_dbg(ON, "platform_device_add() succeeded\n");
2347         return 0;
2348 error2:
2349         platform_device_put(g_plat_dev);
2350 error1:
2351         platform_driver_unregister(&g_oz_plat_drv);
2352 error:
2353         tasklet_disable(&g_urb_process_tasklet);
2354         tasklet_disable(&g_urb_cancel_tasklet);
2355         oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
2356         return err;
2357 }
2358 
2359 /*
2360  * Context: process
2361  */
2362 void oz_hcd_term(void)
2363 {
2364         msleep(OZ_HUB_DEBOUNCE_TIMEOUT);
2365         tasklet_kill(&g_urb_process_tasklet);
2366         tasklet_kill(&g_urb_cancel_tasklet);
2367         platform_device_unregister(g_plat_dev);
2368         platform_driver_unregister(&g_oz_plat_drv);
2369         oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));
2370 }
2371 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us