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