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