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