Version:  2.0.40 2.2.26 2.4.37 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0

Linux/drivers/staging/ozwpan/ozhcd.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us