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