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