Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 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

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

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