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