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