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