1 /* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
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.
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
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 * -----------------------------------------------------------------------------
27 #include <linux/platform_device.h>
28 #include <linux/usb.h>
29 #include <linux/slab.h>
30 #include <linux/export.h>
31 #include "linux/usb/hcd.h"
32 #include <asm/unaligned.h>
35 #include "ozurbparanoia.h"
37 /*------------------------------------------------------------------------------
38 * Number of units of buffering to capture for an isochronous IN endpoint before
39 * allowing data to be indicated up.
41 #define OZ_IN_BUFFERING_UNITS 50
42 /* Name of our platform device.
44 #define OZ_PLAT_DEV_NAME "ozwpan"
45 /* Maximum number of free urb links that can be kept in the pool.
47 #define OZ_MAX_LINK_POOL_SIZE 16
48 /* Get endpoint object from the containing link.
50 #define ep_from_link(__e) container_of((__e), struct oz_endpoint, link)
51 /*EP0 timeout before ep0 request is again added to TX queue. (13*8 = 98mSec)
53 #define EP0_TIMEOUT_COUNTER 13
54 /*------------------------------------------------------------------------------
55 * Used to link urbs together and also store some status information for each
57 * A cache of these are kept in a pool to reduce number of calls to kmalloc.
60 struct list_head link;
65 unsigned submit_counter;
68 /* Holds state information about a USB endpoint.
71 struct list_head urb_list; /* List of oz_urb_link items. */
72 struct list_head link; /* For isoc ep, links in to isoc
74 struct timespec timestamp;
87 /* Bits in the flags field. */
88 #define OZ_F_EP_BUFFERING 0x1
89 #define OZ_F_EP_HAVE_STREAM 0x2
91 /* Holds state information about a USB interface.
98 /* Holds state information about an hcd port.
100 #define OZ_NB_ENDPOINTS 16
105 struct oz_hcd *ozhcd;
106 spinlock_t port_lock;
111 struct oz_interface *iface;
112 struct oz_endpoint *out_ep[OZ_NB_ENDPOINTS];
113 struct oz_endpoint *in_ep[OZ_NB_ENDPOINTS];
114 struct list_head isoc_out_ep;
115 struct list_head isoc_in_ep;
117 #define OZ_PORT_F_PRESENT 0x1
118 #define OZ_PORT_F_CHANGED 0x2
119 #define OZ_PORT_F_DYING 0x4
121 /* Data structure in the private context area of struct usb_hcd.
123 #define OZ_NB_PORTS 8
126 struct list_head urb_pending_list;
127 struct list_head urb_cancel_list;
128 struct list_head orphanage;
129 int conn_port; /* Port that is currently connecting, -1 if none.*/
130 struct oz_port ports[OZ_NB_PORTS];
134 /* Bits in flags field.
136 #define OZ_HDC_F_SUSPENDED 0x1
138 /*------------------------------------------------------------------------------
139 * Static function prototypes.
141 static int oz_hcd_start(struct usb_hcd *hcd);
142 static void oz_hcd_stop(struct usb_hcd *hcd);
143 static void oz_hcd_shutdown(struct usb_hcd *hcd);
144 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
146 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
147 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
148 struct usb_host_endpoint *ep);
149 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
150 struct usb_host_endpoint *ep);
151 static int oz_hcd_get_frame_number(struct usb_hcd *hcd);
152 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf);
153 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
154 u16 windex, char *buf, u16 wlength);
155 static int oz_hcd_bus_suspend(struct usb_hcd *hcd);
156 static int oz_hcd_bus_resume(struct usb_hcd *hcd);
157 static int oz_plat_probe(struct platform_device *dev);
158 static int oz_plat_remove(struct platform_device *dev);
159 static void oz_plat_shutdown(struct platform_device *dev);
160 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg);
161 static int oz_plat_resume(struct platform_device *dev);
162 static void oz_urb_process_tasklet(unsigned long unused);
163 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
164 struct oz_port *port, struct usb_host_config *config,
166 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
167 struct oz_port *port);
168 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
169 struct oz_port *port,
170 struct usb_host_interface *intf, gfp_t mem_flags);
171 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
172 struct oz_port *port, int if_ix);
173 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
175 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
177 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status);
178 /*------------------------------------------------------------------------------
179 * Static external variables.
181 static struct platform_device *g_plat_dev;
182 static struct oz_hcd *g_ozhcd;
183 static DEFINE_SPINLOCK(g_hcdlock); /* Guards g_ozhcd. */
184 static const char g_hcd_name[] = "Ozmo WPAN";
185 static struct list_head *g_link_pool;
186 static int g_link_pool_size;
187 static DEFINE_SPINLOCK(g_link_lock);
188 static DEFINE_SPINLOCK(g_tasklet_lock);
189 static struct tasklet_struct g_urb_process_tasklet;
190 static struct tasklet_struct g_urb_cancel_tasklet;
191 static atomic_t g_pending_urbs = ATOMIC_INIT(0);
192 static atomic_t g_usb_frame_number = ATOMIC_INIT(0);
193 static const struct hc_driver g_oz_hc_drv = {
194 .description = g_hcd_name,
195 .product_desc = "Ozmo Devices WPAN",
196 .hcd_priv_size = sizeof(struct oz_hcd),
198 .start = oz_hcd_start,
200 .shutdown = oz_hcd_shutdown,
201 .urb_enqueue = oz_hcd_urb_enqueue,
202 .urb_dequeue = oz_hcd_urb_dequeue,
203 .endpoint_disable = oz_hcd_endpoint_disable,
204 .endpoint_reset = oz_hcd_endpoint_reset,
205 .get_frame_number = oz_hcd_get_frame_number,
206 .hub_status_data = oz_hcd_hub_status_data,
207 .hub_control = oz_hcd_hub_control,
208 .bus_suspend = oz_hcd_bus_suspend,
209 .bus_resume = oz_hcd_bus_resume,
212 static struct platform_driver g_oz_plat_drv = {
213 .probe = oz_plat_probe,
214 .remove = oz_plat_remove,
215 .shutdown = oz_plat_shutdown,
216 .suspend = oz_plat_suspend,
217 .resume = oz_plat_resume,
219 .name = OZ_PLAT_DEV_NAME,
220 .owner = THIS_MODULE,
223 /*------------------------------------------------------------------------------
224 * Gets our private context area (which is of type struct oz_hcd) from the
228 static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
230 return (struct oz_hcd *)hcd->hcd_priv;
232 /*------------------------------------------------------------------------------
233 * Searches list of ports to find the index of the one with a specified USB
234 * bus address. If none of the ports has the bus address then the connection
235 * port is returned, if there is one or -1 otherwise.
238 static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
241 for (i = 0; i < OZ_NB_PORTS; i++) {
242 if (ozhcd->ports[i].bus_addr == bus_addr)
245 return ozhcd->conn_port;
247 /*------------------------------------------------------------------------------
248 * Allocates an urb link, first trying the pool but going to heap if empty.
251 static struct oz_urb_link *oz_alloc_urb_link(void)
253 struct oz_urb_link *urbl = NULL;
254 unsigned long irq_state;
255 spin_lock_irqsave(&g_link_lock, irq_state);
257 urbl = container_of(g_link_pool, struct oz_urb_link, link);
258 g_link_pool = urbl->link.next;
261 spin_unlock_irqrestore(&g_link_lock, irq_state);
263 urbl = kmalloc(sizeof(struct oz_urb_link), GFP_ATOMIC);
266 /*------------------------------------------------------------------------------
267 * Frees an urb link by putting it in the pool if there is enough space or
268 * deallocating it to heap otherwise.
271 static void oz_free_urb_link(struct oz_urb_link *urbl)
274 unsigned long irq_state;
275 spin_lock_irqsave(&g_link_lock, irq_state);
276 if (g_link_pool_size < OZ_MAX_LINK_POOL_SIZE) {
277 urbl->link.next = g_link_pool;
278 g_link_pool = &urbl->link;
282 spin_unlock_irqrestore(&g_link_lock, irq_state);
286 /*------------------------------------------------------------------------------
287 * Deallocates all the urb links in the pool.
290 static void oz_empty_link_pool(void)
293 unsigned long irq_state;
294 spin_lock_irqsave(&g_link_lock, irq_state);
297 g_link_pool_size = 0;
298 spin_unlock_irqrestore(&g_link_lock, irq_state);
300 struct oz_urb_link *urbl =
301 container_of(e, struct oz_urb_link, link);
306 /*------------------------------------------------------------------------------
307 * Allocates endpoint structure and optionally a buffer. If a buffer is
308 * allocated it immediately follows the endpoint structure.
311 static struct oz_endpoint *oz_ep_alloc(gfp_t mem_flags, int buffer_size)
313 struct oz_endpoint *ep =
314 kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
316 INIT_LIST_HEAD(&ep->urb_list);
317 INIT_LIST_HEAD(&ep->link);
320 ep->buffer_size = buffer_size;
321 ep->buffer = (u8 *)(ep+1);
326 /*------------------------------------------------------------------------------
327 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
329 * Context: softirq or process
331 static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd, struct urb *urb)
333 struct oz_urb_link *urbl;
335 list_for_each(e, &ozhcd->urb_cancel_list) {
336 urbl = container_of(e, struct oz_urb_link, link);
337 if (urb == urbl->urb) {
344 /*------------------------------------------------------------------------------
345 * This is called when we have finished processing an urb. It unlinks it from
346 * the ep and returns it to the core.
347 * Context: softirq or process
349 static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
352 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
353 unsigned long irq_state;
354 struct oz_urb_link *cancel_urbl = NULL;
355 spin_lock_irqsave(&g_tasklet_lock, irq_state);
356 usb_hcd_unlink_urb_from_ep(hcd, urb);
357 /* Clear hcpriv which will prevent it being put in the cancel list
358 * in the event that an attempt is made to cancel it.
361 /* Walk the cancel list in case the urb is already sitting there.
362 * Since we process the cancel list in a tasklet rather than in
363 * the dequeue function this could happen.
365 cancel_urbl = oz_uncancel_urb(ozhcd, urb);
366 /* Note: we release lock but do not enable local irqs.
367 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
368 * or at least other host controllers disable interrupts at this point
369 * so we do the same. We must, however, release the lock otherwise a
370 * deadlock will occur if an urb is submitted to our driver in the urb
371 * completion function. Because we disable interrupts it is possible
372 * that the urb_enqueue function can be called with them disabled.
374 spin_unlock(&g_tasklet_lock);
375 if (oz_forget_urb(urb)) {
376 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
378 atomic_dec(&g_pending_urbs);
379 usb_hcd_giveback_urb(hcd, urb, status);
381 spin_lock(&g_tasklet_lock);
382 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
384 oz_free_urb_link(cancel_urbl);
386 /*------------------------------------------------------------------------------
387 * Deallocates an endpoint including deallocating any associated stream and
388 * returning any queued urbs to the core.
391 static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
394 struct list_head list;
395 struct oz_hcd *ozhcd = port->ozhcd;
396 INIT_LIST_HEAD(&list);
397 if (ep->flags & OZ_F_EP_HAVE_STREAM)
398 oz_usb_stream_delete(port->hpd, ep->ep_num);
399 /* Transfer URBs to the orphanage while we hold the lock. */
400 spin_lock_bh(&ozhcd->hcd_lock);
401 /* Note: this works even if ep->urb_list is empty.*/
402 list_replace_init(&ep->urb_list, &list);
403 /* Put the URBs in the orphanage. */
404 list_splice_tail(&list, &ozhcd->orphanage);
405 spin_unlock_bh(&ozhcd->hcd_lock);
407 oz_dbg(ON, "Freeing endpoint memory\n");
410 /*------------------------------------------------------------------------------
413 static void oz_complete_buffered_urb(struct oz_port *port,
414 struct oz_endpoint *ep,
417 u8 data_len, available_space, copy_len;
419 memcpy(&data_len, &ep->buffer[ep->out_ix], sizeof(u8));
420 if (data_len <= urb->transfer_buffer_length)
421 available_space = data_len;
423 available_space = urb->transfer_buffer_length;
425 if (++ep->out_ix == ep->buffer_size)
427 copy_len = ep->buffer_size - ep->out_ix;
428 if (copy_len >= available_space)
429 copy_len = available_space;
430 memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
432 if (copy_len < available_space) {
433 memcpy((urb->transfer_buffer + copy_len), ep->buffer,
434 (available_space - copy_len));
435 ep->out_ix = available_space - copy_len;
437 ep->out_ix += copy_len;
439 urb->actual_length = available_space;
440 if (ep->out_ix == ep->buffer_size)
443 ep->buffered_units--;
444 oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
446 oz_complete_urb(port->ozhcd->hcd, urb, 0);
449 /*------------------------------------------------------------------------------
452 static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
453 struct urb *urb, u8 req_id)
455 struct oz_urb_link *urbl;
456 struct oz_endpoint *ep;
458 if (ep_addr >= OZ_NB_ENDPOINTS) {
459 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
462 urbl = oz_alloc_urb_link();
465 urbl->submit_counter = 0;
467 urbl->req_id = req_id;
468 urbl->ep_num = ep_addr;
469 /* Hold lock while we insert the URB into the list within the
470 * endpoint structure.
472 spin_lock_bh(&port->ozhcd->hcd_lock);
473 /* If the urb has been unlinked while out of any list then
477 spin_unlock_bh(&port->ozhcd->hcd_lock);
478 oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
479 oz_complete_urb(port->ozhcd->hcd, urb, 0);
480 oz_free_urb_link(urbl);
483 if (in_dir && port->in_ep[ep_addr])
484 ep = port->in_ep[ep_addr];
485 else if (!in_dir && port->out_ep[ep_addr])
486 ep = port->out_ep[ep_addr];
492 /*For interrupt endpoint check for buffered data
495 if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
496 && ep->buffered_units > 0) {
497 oz_free_urb_link(urbl);
498 spin_unlock_bh(&port->ozhcd->hcd_lock);
499 oz_complete_buffered_urb(port, ep, urb);
503 if (ep && port->hpd) {
504 list_add_tail(&urbl->link, &ep->urb_list);
505 if (!in_dir && ep_addr && (ep->credit < 0)) {
506 getrawmonotonic(&ep->timestamp);
513 spin_unlock_bh(&port->ozhcd->hcd_lock);
515 oz_free_urb_link(urbl);
518 /*------------------------------------------------------------------------------
519 * Removes an urb from the queue in the endpoint.
520 * Returns 0 if it is found and -EIDRM otherwise.
523 static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
526 struct oz_urb_link *urbl = NULL;
527 struct oz_endpoint *ep;
528 spin_lock_bh(&port->ozhcd->hcd_lock);
530 ep = port->in_ep[ep_addr];
532 ep = port->out_ep[ep_addr];
535 list_for_each(e, &ep->urb_list) {
536 urbl = container_of(e, struct oz_urb_link, link);
537 if (urbl->urb == urb) {
544 spin_unlock_bh(&port->ozhcd->hcd_lock);
546 oz_free_urb_link(urbl);
547 return urbl ? 0 : -EIDRM;
549 /*------------------------------------------------------------------------------
550 * Finds an urb given its request id.
553 static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
556 struct oz_hcd *ozhcd = port->ozhcd;
557 struct urb *urb = NULL;
558 struct oz_urb_link *urbl = NULL;
559 struct oz_endpoint *ep;
561 spin_lock_bh(&ozhcd->hcd_lock);
562 ep = port->out_ep[ep_ix];
565 list_for_each(e, &ep->urb_list) {
566 urbl = container_of(e, struct oz_urb_link, link);
567 if (urbl->req_id == req_id) {
574 spin_unlock_bh(&ozhcd->hcd_lock);
575 /* If urb is non-zero then we we must have an urb link to delete.
578 oz_free_urb_link(urbl);
581 /*------------------------------------------------------------------------------
582 * Pre-condition: Port lock must be held.
585 static void oz_acquire_port(struct oz_port *port, void *hpd)
587 INIT_LIST_HEAD(&port->isoc_out_ep);
588 INIT_LIST_HEAD(&port->isoc_in_ep);
589 port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
590 port->status |= USB_PORT_STAT_CONNECTION |
591 (USB_PORT_STAT_C_CONNECTION << 16);
595 /*------------------------------------------------------------------------------
598 static struct oz_hcd *oz_hcd_claim(void)
600 struct oz_hcd *ozhcd;
601 spin_lock_bh(&g_hcdlock);
604 usb_get_hcd(ozhcd->hcd);
605 spin_unlock_bh(&g_hcdlock);
608 /*------------------------------------------------------------------------------
611 static inline void oz_hcd_put(struct oz_hcd *ozhcd)
614 usb_put_hcd(ozhcd->hcd);
616 /*------------------------------------------------------------------------------
617 * This is called by the protocol handler to notify that a PD has arrived.
618 * We allocate a port to associate with the PD and create a structure for
619 * endpoint 0. This port is made the connection port.
620 * In the event that one of the other port is already a connection port then
622 * TODO We should be able to do better than fail and should be able remember
623 * that this port needs configuring and make it the connection port once the
624 * current connection port has been assigned an address. Collisions here are
625 * probably very rare indeed.
628 void *oz_hcd_pd_arrived(void *hpd)
632 struct oz_hcd *ozhcd = NULL;
633 struct oz_endpoint *ep;
634 ozhcd = oz_hcd_claim();
637 /* Allocate an endpoint object in advance (before holding hcd lock) to
638 * use for out endpoint 0.
640 ep = oz_ep_alloc(GFP_ATOMIC, 0);
641 spin_lock_bh(&ozhcd->hcd_lock);
642 if (ozhcd->conn_port >= 0) {
643 spin_unlock_bh(&ozhcd->hcd_lock);
644 oz_dbg(ON, "conn_port >= 0\n");
647 for (i = 0; i < OZ_NB_PORTS; i++) {
648 struct oz_port *port = &ozhcd->ports[i];
649 spin_lock(&port->port_lock);
650 if ((port->flags & OZ_PORT_F_PRESENT) == 0) {
651 oz_acquire_port(port, hpd);
652 spin_unlock(&port->port_lock);
655 spin_unlock(&port->port_lock);
657 if (i < OZ_NB_PORTS) {
658 oz_dbg(ON, "Setting conn_port = %d\n", i);
659 ozhcd->conn_port = i;
660 /* Attach out endpoint 0.
662 ozhcd->ports[i].out_ep[0] = ep;
664 hport = &ozhcd->ports[i];
665 spin_unlock_bh(&ozhcd->hcd_lock);
666 if (ozhcd->flags & OZ_HDC_F_SUSPENDED) {
667 oz_dbg(ON, "Resuming root hub\n");
668 usb_hcd_resume_root_hub(ozhcd->hcd);
670 usb_hcd_poll_rh_status(ozhcd->hcd);
672 spin_unlock_bh(&ozhcd->hcd_lock);
675 if (ep) /* ep is non-null if not used. */
676 oz_ep_free(NULL, ep);
680 /*------------------------------------------------------------------------------
681 * This is called by the protocol handler to notify that the PD has gone away.
682 * We need to deallocate all resources and then request that the root hub is
683 * polled. We release the reference we hold on the PD.
686 void oz_hcd_pd_departed(void *hport)
688 struct oz_port *port = (struct oz_port *)hport;
689 struct oz_hcd *ozhcd;
691 struct oz_endpoint *ep = NULL;
694 oz_dbg(ON, "%s: port = 0\n", __func__);
700 /* Check if this is the connection port - if so clear it.
702 spin_lock_bh(&ozhcd->hcd_lock);
703 if ((ozhcd->conn_port >= 0) &&
704 (port == &ozhcd->ports[ozhcd->conn_port])) {
705 oz_dbg(ON, "Clearing conn_port\n");
706 ozhcd->conn_port = -1;
708 spin_lock(&port->port_lock);
709 port->flags |= OZ_PORT_F_DYING;
710 spin_unlock(&port->port_lock);
711 spin_unlock_bh(&ozhcd->hcd_lock);
713 oz_clean_endpoints_for_config(ozhcd->hcd, port);
714 spin_lock_bh(&port->port_lock);
717 port->bus_addr = 0xff;
718 port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
719 port->flags |= OZ_PORT_F_CHANGED;
720 port->status &= ~USB_PORT_STAT_CONNECTION;
721 port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
722 /* If there is an endpont 0 then clear the pointer while we hold
723 * the spinlock be we deallocate it after releasing the lock.
725 if (port->out_ep[0]) {
726 ep = port->out_ep[0];
727 port->out_ep[0] = NULL;
729 spin_unlock_bh(&port->port_lock);
731 oz_ep_free(port, ep);
732 usb_hcd_poll_rh_status(ozhcd->hcd);
735 /*------------------------------------------------------------------------------
738 void oz_hcd_pd_reset(void *hpd, void *hport)
740 /* Cleanup the current configuration and report reset to the core.
742 struct oz_port *port = (struct oz_port *)hport;
743 struct oz_hcd *ozhcd = port->ozhcd;
744 oz_dbg(ON, "PD Reset\n");
745 spin_lock_bh(&port->port_lock);
746 port->flags |= OZ_PORT_F_CHANGED;
747 port->status |= USB_PORT_STAT_RESET;
748 port->status |= (USB_PORT_STAT_C_RESET << 16);
749 spin_unlock_bh(&port->port_lock);
750 oz_clean_endpoints_for_config(ozhcd->hcd, port);
751 usb_hcd_poll_rh_status(ozhcd->hcd);
753 /*------------------------------------------------------------------------------
756 void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
757 int length, int offset, int total_size)
759 struct oz_port *port = (struct oz_port *)hport;
763 oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
764 length, offset, total_size);
765 urb = oz_find_urb_by_id(port, 0, req_id);
770 int required_size = urb->transfer_buffer_length;
771 if (required_size > total_size)
772 required_size = total_size;
773 copy_len = required_size-offset;
774 if (length <= copy_len)
776 memcpy(urb->transfer_buffer+offset, desc, copy_len);
778 if (offset < required_size) {
779 struct usb_ctrlrequest *setup =
780 (struct usb_ctrlrequest *)urb->setup_packet;
781 unsigned wvalue = le16_to_cpu(setup->wValue);
782 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
784 else if (oz_usb_get_desc_req(port->hpd, req_id,
785 setup->bRequestType, (u8)(wvalue>>8),
786 (u8)wvalue, setup->wIndex, offset,
787 required_size-offset)) {
788 oz_dequeue_ep_urb(port, 0, 0, urb);
795 urb->actual_length = total_size;
796 oz_complete_urb(port->ozhcd->hcd, urb, 0);
798 /*------------------------------------------------------------------------------
801 static void oz_display_conf_type(u8 t)
804 case USB_REQ_GET_STATUS:
805 oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
807 case USB_REQ_CLEAR_FEATURE:
808 oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
810 case USB_REQ_SET_FEATURE:
811 oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
813 case USB_REQ_SET_ADDRESS:
814 oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
816 case USB_REQ_GET_DESCRIPTOR:
817 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
819 case USB_REQ_SET_DESCRIPTOR:
820 oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
822 case USB_REQ_GET_CONFIGURATION:
823 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
825 case USB_REQ_SET_CONFIGURATION:
826 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
828 case USB_REQ_GET_INTERFACE:
829 oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
831 case USB_REQ_SET_INTERFACE:
832 oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
834 case USB_REQ_SYNCH_FRAME:
835 oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
840 /*------------------------------------------------------------------------------
843 static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
844 u8 rcode, u8 config_num)
847 struct usb_hcd *hcd = port->ozhcd->hcd;
849 port->config_num = config_num;
850 oz_clean_endpoints_for_config(hcd, port);
851 if (oz_build_endpoints_for_config(hcd, port,
852 &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
858 oz_complete_urb(hcd, urb, rc);
860 /*------------------------------------------------------------------------------
863 static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
864 u8 rcode, u8 if_num, u8 alt)
866 struct usb_hcd *hcd = port->ozhcd->hcd;
869 struct usb_host_config *config;
870 struct usb_host_interface *intf;
871 oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
872 oz_clean_endpoints_for_interface(hcd, port, if_num);
873 config = &urb->dev->config[port->config_num-1];
874 intf = &config->intf_cache[if_num]->altsetting[alt];
875 if (oz_build_endpoints_for_interface(hcd, port, intf,
879 port->iface[if_num].alt = alt;
883 oz_complete_urb(hcd, urb, rc);
885 /*------------------------------------------------------------------------------
888 void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
891 struct oz_port *port = (struct oz_port *)hport;
893 struct usb_ctrlrequest *setup;
894 struct usb_hcd *hcd = port->ozhcd->hcd;
898 oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
899 urb = oz_find_urb_by_id(port, 0, req_id);
901 oz_dbg(ON, "URB not found\n");
904 setup = (struct usb_ctrlrequest *)urb->setup_packet;
905 windex = le16_to_cpu(setup->wIndex);
906 wvalue = le16_to_cpu(setup->wValue);
907 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
908 /* Standard requests */
909 oz_display_conf_type(setup->bRequest);
910 switch (setup->bRequest) {
911 case USB_REQ_SET_CONFIGURATION:
912 oz_hcd_complete_set_config(port, urb, rcode,
915 case USB_REQ_SET_INTERFACE:
916 oz_hcd_complete_set_interface(port, urb, rcode,
917 (u8)windex, (u8)wvalue);
920 oz_complete_urb(hcd, urb, 0);
925 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
927 if (data_len <= urb->transfer_buffer_length)
930 copy_len = urb->transfer_buffer_length;
931 memcpy(urb->transfer_buffer, data, copy_len);
932 urb->actual_length = copy_len;
934 oz_complete_urb(hcd, urb, 0);
937 /*------------------------------------------------------------------------------
938 * Context: softirq-serialized
940 static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
947 space = ep->out_ix-ep->in_ix-1;
949 space += ep->buffer_size;
950 if (space < (data_len+1)) {
951 oz_dbg(ON, "Buffer full\n");
954 ep->buffer[ep->in_ix] = (u8)data_len;
955 if (++ep->in_ix == ep->buffer_size)
957 copy_len = ep->buffer_size - ep->in_ix;
958 if (copy_len > data_len)
960 memcpy(&ep->buffer[ep->in_ix], data, copy_len);
962 if (copy_len < data_len) {
963 memcpy(ep->buffer, data+copy_len, data_len-copy_len);
964 ep->in_ix = data_len-copy_len;
966 ep->in_ix += copy_len;
968 if (ep->in_ix == ep->buffer_size)
970 ep->buffered_units++;
973 /*------------------------------------------------------------------------------
974 * Context: softirq-serialized
976 void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
978 struct oz_port *port = (struct oz_port *)hport;
979 struct oz_endpoint *ep;
980 struct oz_hcd *ozhcd = port->ozhcd;
981 spin_lock_bh(&ozhcd->hcd_lock);
982 ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
985 switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
986 case USB_ENDPOINT_XFER_INT:
987 case USB_ENDPOINT_XFER_BULK:
988 if (!list_empty(&ep->urb_list)) {
989 struct oz_urb_link *urbl =
990 list_first_entry(&ep->urb_list,
991 struct oz_urb_link, link);
994 list_del_init(&urbl->link);
995 spin_unlock_bh(&ozhcd->hcd_lock);
997 oz_free_urb_link(urbl);
998 if (data_len <= urb->transfer_buffer_length)
1001 copy_len = urb->transfer_buffer_length;
1002 memcpy(urb->transfer_buffer, data, copy_len);
1003 urb->actual_length = copy_len;
1004 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1007 oz_dbg(ON, "buffering frame as URB is not available\n");
1008 oz_hcd_buffer_data(ep, data, data_len);
1011 case USB_ENDPOINT_XFER_ISOC:
1012 oz_hcd_buffer_data(ep, data, data_len);
1016 spin_unlock_bh(&ozhcd->hcd_lock);
1018 /*------------------------------------------------------------------------------
1021 static inline int oz_usb_get_frame_number(void)
1023 return atomic_inc_return(&g_usb_frame_number);
1025 /*------------------------------------------------------------------------------
1028 int oz_hcd_heartbeat(void *hport)
1031 struct oz_port *port = (struct oz_port *)hport;
1032 struct oz_hcd *ozhcd = port->ozhcd;
1033 struct oz_urb_link *urbl;
1034 struct list_head xfr_list;
1035 struct list_head *e;
1036 struct list_head *n;
1038 struct oz_endpoint *ep;
1039 struct timespec ts, delta;
1040 getrawmonotonic(&ts);
1041 INIT_LIST_HEAD(&xfr_list);
1042 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1044 spin_lock_bh(&ozhcd->hcd_lock);
1045 list_for_each(e, &port->isoc_out_ep) {
1046 ep = ep_from_link(e);
1049 delta = timespec_sub(ts, ep->timestamp);
1050 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1051 if (ep->credit > ep->credit_ceiling)
1052 ep->credit = ep->credit_ceiling;
1054 while (ep->credit && !list_empty(&ep->urb_list)) {
1055 urbl = list_first_entry(&ep->urb_list,
1056 struct oz_urb_link, link);
1058 if ((ep->credit + 1) < urb->number_of_packets)
1060 ep->credit -= urb->number_of_packets;
1063 list_move_tail(&urbl->link, &xfr_list);
1066 spin_unlock_bh(&ozhcd->hcd_lock);
1067 /* Send to PD and complete URBs.
1069 list_for_each_safe(e, n, &xfr_list) {
1070 urbl = container_of(e, struct oz_urb_link, link);
1073 urb->error_count = 0;
1074 urb->start_frame = oz_usb_get_frame_number();
1075 oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
1076 oz_free_urb_link(urbl);
1077 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1079 /* Check the IN isoc endpoints to see if any URBs can be completed.
1081 spin_lock_bh(&ozhcd->hcd_lock);
1082 list_for_each(e, &port->isoc_in_ep) {
1083 struct oz_endpoint *ep = ep_from_link(e);
1084 if (ep->flags & OZ_F_EP_BUFFERING) {
1085 if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
1086 ep->flags &= ~OZ_F_EP_BUFFERING;
1089 ep->start_frame = 0;
1093 delta = timespec_sub(ts, ep->timestamp);
1094 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1096 while (!list_empty(&ep->urb_list)) {
1097 struct oz_urb_link *urbl =
1098 list_first_entry(&ep->urb_list,
1099 struct oz_urb_link, link);
1100 struct urb *urb = urbl->urb;
1104 if (ep->credit < urb->number_of_packets)
1106 if (ep->buffered_units < urb->number_of_packets)
1108 urb->actual_length = 0;
1109 for (i = 0; i < urb->number_of_packets; i++) {
1110 len = ep->buffer[ep->out_ix];
1111 if (++ep->out_ix == ep->buffer_size)
1113 copy_len = ep->buffer_size - ep->out_ix;
1116 memcpy(urb->transfer_buffer,
1117 &ep->buffer[ep->out_ix], copy_len);
1118 if (copy_len < len) {
1119 memcpy(urb->transfer_buffer+copy_len,
1120 ep->buffer, len-copy_len);
1121 ep->out_ix = len-copy_len;
1123 ep->out_ix += copy_len;
1124 if (ep->out_ix == ep->buffer_size)
1126 urb->iso_frame_desc[i].offset =
1128 urb->actual_length += len;
1129 urb->iso_frame_desc[i].actual_length = len;
1130 urb->iso_frame_desc[i].status = 0;
1132 ep->buffered_units -= urb->number_of_packets;
1133 urb->error_count = 0;
1134 urb->start_frame = ep->start_frame;
1135 ep->start_frame += urb->number_of_packets;
1136 list_move_tail(&urbl->link, &xfr_list);
1137 ep->credit -= urb->number_of_packets;
1140 if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1142 spin_unlock_bh(&ozhcd->hcd_lock);
1143 /* Complete the filled URBs.
1145 list_for_each_safe(e, n, &xfr_list) {
1146 urbl = container_of(e, struct oz_urb_link, link);
1149 oz_free_urb_link(urbl);
1150 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1152 /* Check if there are any ep0 requests that have timed out.
1153 * If so resent to PD.
1155 ep = port->out_ep[0];
1157 struct list_head *e;
1158 struct list_head *n;
1159 spin_lock_bh(&ozhcd->hcd_lock);
1160 list_for_each_safe(e, n, &ep->urb_list) {
1161 urbl = container_of(e, struct oz_urb_link, link);
1162 if (urbl->submit_counter > EP0_TIMEOUT_COUNTER) {
1163 oz_dbg(ON, "Request 0x%p timeout\n", urbl->urb);
1164 list_move_tail(e, &xfr_list);
1165 urbl->submit_counter = 0;
1167 urbl->submit_counter++;
1170 if (!list_empty(&ep->urb_list))
1172 spin_unlock_bh(&ozhcd->hcd_lock);
1174 while (e != &xfr_list) {
1175 urbl = container_of(e, struct oz_urb_link, link);
1177 oz_dbg(ON, "Resending request to PD\n");
1178 oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1179 oz_free_urb_link(urbl);
1184 /*------------------------------------------------------------------------------
1187 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1188 struct oz_port *port,
1189 struct usb_host_interface *intf, gfp_t mem_flags)
1191 struct oz_hcd *ozhcd = port->ozhcd;
1193 int if_ix = intf->desc.bInterfaceNumber;
1194 int request_heartbeat = 0;
1195 oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
1196 for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1197 struct usb_host_endpoint *hep = &intf->endpoint[i];
1198 u8 ep_addr = hep->desc.bEndpointAddress;
1199 u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1200 struct oz_endpoint *ep;
1201 int buffer_size = 0;
1203 oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
1204 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1205 switch (hep->desc.bmAttributes &
1206 USB_ENDPOINT_XFERTYPE_MASK) {
1207 case USB_ENDPOINT_XFER_ISOC:
1208 buffer_size = 24*1024;
1210 case USB_ENDPOINT_XFER_INT:
1216 ep = oz_ep_alloc(mem_flags, buffer_size);
1218 oz_clean_endpoints_for_interface(hcd, port, if_ix);
1221 ep->attrib = hep->desc.bmAttributes;
1222 ep->ep_num = ep_num;
1223 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1224 == USB_ENDPOINT_XFER_ISOC) {
1225 oz_dbg(ON, "wMaxPacketSize = %d\n",
1226 usb_endpoint_maxp(&hep->desc));
1227 ep->credit_ceiling = 200;
1228 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1229 ep->flags |= OZ_F_EP_BUFFERING;
1231 ep->flags |= OZ_F_EP_HAVE_STREAM;
1232 if (oz_usb_stream_create(port->hpd, ep_num))
1233 ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1236 spin_lock_bh(&ozhcd->hcd_lock);
1237 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1238 port->in_ep[ep_num] = ep;
1239 port->iface[if_ix].ep_mask |=
1240 (1<<(ep_num+OZ_NB_ENDPOINTS));
1241 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1242 == USB_ENDPOINT_XFER_ISOC) {
1243 list_add_tail(&ep->link, &port->isoc_in_ep);
1244 request_heartbeat = 1;
1247 port->out_ep[ep_num] = ep;
1248 port->iface[if_ix].ep_mask |= (1<<ep_num);
1249 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1250 == USB_ENDPOINT_XFER_ISOC) {
1251 list_add_tail(&ep->link, &port->isoc_out_ep);
1252 request_heartbeat = 1;
1255 spin_unlock_bh(&ozhcd->hcd_lock);
1256 if (request_heartbeat && port->hpd)
1257 oz_usb_request_heartbeat(port->hpd);
1261 /*------------------------------------------------------------------------------
1264 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1265 struct oz_port *port, int if_ix)
1267 struct oz_hcd *ozhcd = port->ozhcd;
1270 struct list_head ep_list;
1272 oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
1273 if (if_ix >= port->num_iface)
1275 INIT_LIST_HEAD(&ep_list);
1276 spin_lock_bh(&ozhcd->hcd_lock);
1277 mask = port->iface[if_ix].ep_mask;
1278 port->iface[if_ix].ep_mask = 0;
1279 for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1280 struct list_head *e;
1281 /* Gather OUT endpoints.
1283 if ((mask & (1<<i)) && port->out_ep[i]) {
1284 e = &port->out_ep[i]->link;
1285 port->out_ep[i] = NULL;
1286 /* Remove from isoc list if present.
1288 list_move_tail(e, &ep_list);
1290 /* Gather IN endpoints.
1292 if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1293 e = &port->in_ep[i]->link;
1294 port->in_ep[i] = NULL;
1295 list_move_tail(e, &ep_list);
1298 spin_unlock_bh(&ozhcd->hcd_lock);
1299 while (!list_empty(&ep_list)) {
1300 struct oz_endpoint *ep =
1301 list_first_entry(&ep_list, struct oz_endpoint, link);
1302 list_del_init(&ep->link);
1303 oz_ep_free(port, ep);
1306 /*------------------------------------------------------------------------------
1309 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1310 struct oz_port *port, struct usb_host_config *config,
1313 struct oz_hcd *ozhcd = port->ozhcd;
1315 int num_iface = config->desc.bNumInterfaces;
1317 struct oz_interface *iface;
1319 iface = kmalloc(num_iface*sizeof(struct oz_interface),
1320 mem_flags | __GFP_ZERO);
1323 spin_lock_bh(&ozhcd->hcd_lock);
1324 port->iface = iface;
1325 port->num_iface = num_iface;
1326 spin_unlock_bh(&ozhcd->hcd_lock);
1328 for (i = 0; i < num_iface; i++) {
1329 struct usb_host_interface *intf =
1330 &config->intf_cache[i]->altsetting[0];
1331 if (oz_build_endpoints_for_interface(hcd, port, intf,
1337 oz_clean_endpoints_for_config(hcd, port);
1340 /*------------------------------------------------------------------------------
1343 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1344 struct oz_port *port)
1346 struct oz_hcd *ozhcd = port->ozhcd;
1348 oz_dbg(ON, "Deleting endpoints for configuration\n");
1349 for (i = 0; i < port->num_iface; i++)
1350 oz_clean_endpoints_for_interface(hcd, port, i);
1351 spin_lock_bh(&ozhcd->hcd_lock);
1353 oz_dbg(ON, "Freeing interfaces object\n");
1357 port->num_iface = 0;
1358 spin_unlock_bh(&ozhcd->hcd_lock);
1360 /*------------------------------------------------------------------------------
1363 static void *oz_claim_hpd(struct oz_port *port)
1366 struct oz_hcd *ozhcd = port->ozhcd;
1367 spin_lock_bh(&ozhcd->hcd_lock);
1371 spin_unlock_bh(&ozhcd->hcd_lock);
1374 /*------------------------------------------------------------------------------
1377 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1380 struct usb_ctrlrequest *setup;
1387 unsigned complete = 0;
1390 struct oz_port *port = NULL;
1392 oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
1393 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1398 port = &ozhcd->ports[port_ix];
1399 if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1400 || (port->flags & OZ_PORT_F_DYING)) {
1401 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1402 port_ix, urb->dev->devnum);
1406 /* Store port in private context data.
1409 setup = (struct usb_ctrlrequest *)urb->setup_packet;
1410 windex = le16_to_cpu(setup->wIndex);
1411 wvalue = le16_to_cpu(setup->wValue);
1412 wlength = le16_to_cpu(setup->wLength);
1413 oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
1414 oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1415 oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
1416 oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
1417 oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
1419 req_id = port->next_req_id++;
1420 hpd = oz_claim_hpd(port);
1422 oz_dbg(ON, "Cannot claim port\n");
1427 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1428 /* Standard requests
1430 switch (setup->bRequest) {
1431 case USB_REQ_GET_DESCRIPTOR:
1432 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
1434 case USB_REQ_SET_ADDRESS:
1435 oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
1436 oz_dbg(ON, "Port %d address is 0x%x\n",
1438 (u8)le16_to_cpu(setup->wValue));
1439 spin_lock_bh(&ozhcd->hcd_lock);
1440 if (ozhcd->conn_port >= 0) {
1441 ozhcd->ports[ozhcd->conn_port].bus_addr =
1442 (u8)le16_to_cpu(setup->wValue);
1443 oz_dbg(ON, "Clearing conn_port\n");
1444 ozhcd->conn_port = -1;
1446 spin_unlock_bh(&ozhcd->hcd_lock);
1449 case USB_REQ_SET_CONFIGURATION:
1450 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
1452 case USB_REQ_GET_CONFIGURATION:
1453 /* We short circuit this case and reply directly since
1454 * we have the selected configuration number cached.
1456 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
1457 if (urb->transfer_buffer_length >= 1) {
1458 urb->actual_length = 1;
1459 *((u8 *)urb->transfer_buffer) =
1466 case USB_REQ_GET_INTERFACE:
1467 /* We short circuit this case and reply directly since
1468 * we have the selected interface alternative cached.
1470 oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
1471 if (urb->transfer_buffer_length >= 1) {
1472 urb->actual_length = 1;
1473 *((u8 *)urb->transfer_buffer) =
1474 port->iface[(u8)windex].alt;
1475 oz_dbg(ON, "interface = %d alt = %d\n",
1476 windex, port->iface[(u8)windex].alt);
1482 case USB_REQ_SET_INTERFACE:
1483 oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
1487 if (!rc && !complete) {
1489 if ((setup->bRequestType & USB_DIR_IN) == 0)
1491 urb->actual_length = data_len;
1492 if (oz_usb_control_req(port->hpd, req_id, setup,
1493 urb->transfer_buffer, data_len)) {
1496 /* Note: we are queuing the request after we have
1497 * submitted it to be transmitted. If the request were
1498 * to complete before we queued it then it would not
1499 * be found in the queue. It seems impossible for
1500 * this to happen but if it did the request would
1501 * be resubmitted so the problem would hopefully
1502 * resolve itself. Putting the request into the
1503 * queue before it has been sent is worse since the
1504 * urb could be cancelled while we are using it
1505 * to build the request.
1507 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1513 if (rc || complete) {
1514 oz_dbg(ON, "Completing request locally\n");
1515 oz_complete_urb(ozhcd->hcd, urb, rc);
1517 oz_usb_request_heartbeat(port->hpd);
1520 /*------------------------------------------------------------------------------
1523 static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1526 struct oz_port *port = urb->hcpriv;
1528 /* When we are paranoid we keep a list of urbs which we check against
1529 * before handing one back. This is just for debugging during
1530 * development and should be turned off in the released driver.
1532 oz_remember_urb(urb);
1533 /* Check buffer is valid.
1535 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1537 /* Check if there is a device at the port - refuse if not.
1539 if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1541 ep_addr = usb_pipeendpoint(urb->pipe);
1543 /* If the request is not for EP0 then queue it.
1545 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1549 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1553 /*------------------------------------------------------------------------------
1556 static void oz_urb_process_tasklet(unsigned long unused)
1558 unsigned long irq_state;
1560 struct oz_hcd *ozhcd = oz_hcd_claim();
1564 /* This is called from a tasklet so is in softirq context but the urb
1565 * list is filled from any context so we need to lock
1566 * appropriately while removing urbs.
1568 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1569 while (!list_empty(&ozhcd->urb_pending_list)) {
1570 struct oz_urb_link *urbl =
1571 list_first_entry(&ozhcd->urb_pending_list,
1572 struct oz_urb_link, link);
1573 list_del_init(&urbl->link);
1574 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1576 oz_free_urb_link(urbl);
1577 rc = oz_urb_process(ozhcd, urb);
1579 oz_complete_urb(ozhcd->hcd, urb, rc);
1580 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1582 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1585 /*------------------------------------------------------------------------------
1586 * This function searches for the urb in any of the lists it could be in.
1587 * If it is found it is removed from the list and completed. If the urb is
1588 * being processed then it won't be in a list so won't be found. However, the
1589 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1590 * to a non-zero value. When an attempt is made to put the urb back in a list
1591 * the unlinked field will be checked and the urb will then be completed.
1594 static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1596 struct oz_urb_link *urbl = NULL;
1597 struct list_head *e;
1598 struct oz_hcd *ozhcd;
1599 unsigned long irq_state;
1602 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
1605 ozhcd = port->ozhcd;
1606 if (ozhcd == NULL) {
1607 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
1611 /* Look in the tasklet queue.
1613 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1614 list_for_each(e, &ozhcd->urb_cancel_list) {
1615 urbl = container_of(e, struct oz_urb_link, link);
1616 if (urb == urbl->urb) {
1618 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1622 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1625 /* Look in the orphanage.
1627 spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1628 list_for_each(e, &ozhcd->orphanage) {
1629 urbl = container_of(e, struct oz_urb_link, link);
1630 if (urbl->urb == urb) {
1632 oz_dbg(ON, "Found urb in orphanage\n");
1636 ix = (ep_num & 0xf);
1638 if ((ep_num & USB_DIR_IN) && ix)
1639 urbl = oz_remove_urb(port->in_ep[ix], urb);
1641 urbl = oz_remove_urb(port->out_ep[ix], urb);
1643 spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1646 urb->actual_length = 0;
1647 oz_free_urb_link(urbl);
1648 oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
1651 /*------------------------------------------------------------------------------
1654 static void oz_urb_cancel_tasklet(unsigned long unused)
1656 unsigned long irq_state;
1658 struct oz_hcd *ozhcd = oz_hcd_claim();
1661 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1662 while (!list_empty(&ozhcd->urb_cancel_list)) {
1663 struct oz_urb_link *urbl =
1664 list_first_entry(&ozhcd->urb_cancel_list,
1665 struct oz_urb_link, link);
1666 list_del_init(&urbl->link);
1667 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1670 oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1671 oz_free_urb_link(urbl);
1672 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1674 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1677 /*------------------------------------------------------------------------------
1680 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1683 struct oz_urb_link *urbl;
1684 while (!list_empty(&ozhcd->orphanage)) {
1685 urbl = list_first_entry(&ozhcd->orphanage,
1686 struct oz_urb_link, link);
1687 list_del(&urbl->link);
1688 oz_complete_urb(ozhcd->hcd, urbl->urb, status);
1689 oz_free_urb_link(urbl);
1693 /*------------------------------------------------------------------------------
1696 static int oz_hcd_start(struct usb_hcd *hcd)
1698 hcd->power_budget = 200;
1699 hcd->state = HC_STATE_RUNNING;
1700 hcd->uses_new_polling = 1;
1703 /*------------------------------------------------------------------------------
1706 static void oz_hcd_stop(struct usb_hcd *hcd)
1709 /*------------------------------------------------------------------------------
1712 static void oz_hcd_shutdown(struct usb_hcd *hcd)
1715 /*------------------------------------------------------------------------------
1716 * Called to queue an urb for the device.
1717 * This function should return a non-zero error code if it fails the urb but
1718 * should not call usb_hcd_giveback_urb().
1721 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1724 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1727 struct oz_port *port;
1728 unsigned long irq_state;
1729 struct oz_urb_link *urbl;
1730 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1731 if (unlikely(ozhcd == NULL)) {
1732 oz_dbg(URB, "Refused urb(%p) not ozhcd\n", urb);
1735 if (unlikely(hcd->state != HC_STATE_RUNNING)) {
1736 oz_dbg(URB, "Refused urb(%p) not running\n", urb);
1739 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1742 port = &ozhcd->ports[port_ix];
1745 if ((port->flags & OZ_PORT_F_PRESENT) == 0) {
1746 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1747 port_ix, urb->dev->devnum);
1751 /* Put request in queue for processing by tasklet.
1753 urbl = oz_alloc_urb_link();
1754 if (unlikely(urbl == NULL))
1757 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1758 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1760 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1761 oz_free_urb_link(urbl);
1764 list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1765 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1766 tasklet_schedule(&g_urb_process_tasklet);
1767 atomic_inc(&g_pending_urbs);
1770 /*------------------------------------------------------------------------------
1773 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1776 struct oz_urb_link *urbl = NULL;
1777 struct list_head *e;
1778 if (unlikely(ep == NULL))
1780 list_for_each(e, &ep->urb_list) {
1781 urbl = container_of(e, struct oz_urb_link, link);
1782 if (urbl->urb == urb) {
1784 if (usb_pipeisoc(urb->pipe)) {
1785 ep->credit -= urb->number_of_packets;
1794 /*------------------------------------------------------------------------------
1795 * Called to dequeue a previously submitted urb for the device.
1798 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1800 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1801 struct oz_urb_link *urbl = NULL;
1803 unsigned long irq_state;
1804 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1805 urbl = oz_alloc_urb_link();
1806 if (unlikely(urbl == NULL))
1808 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1809 /* The following function checks the urb is still in the queue
1810 * maintained by the core and that the unlinked field is zero.
1811 * If both are true the function sets the unlinked field and returns
1812 * zero. Otherwise it returns an error.
1814 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1815 /* We have to check we haven't completed the urb or are about
1816 * to complete it. When we do we set hcpriv to 0 so if this has
1817 * already happened we don't put the urb in the cancel queue.
1819 if ((rc == 0) && urb->hcpriv) {
1821 urbl->port = (struct oz_port *)urb->hcpriv;
1822 urbl->ep_num = usb_pipeendpoint(urb->pipe);
1823 if (usb_pipein(urb->pipe))
1824 urbl->ep_num |= USB_DIR_IN;
1825 list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1826 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1827 tasklet_schedule(&g_urb_cancel_tasklet);
1829 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1830 oz_free_urb_link(urbl);
1834 /*------------------------------------------------------------------------------
1837 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1838 struct usb_host_endpoint *ep)
1841 /*------------------------------------------------------------------------------
1844 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1845 struct usb_host_endpoint *ep)
1848 /*------------------------------------------------------------------------------
1851 static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1853 oz_dbg(ON, "oz_hcd_get_frame_number\n");
1854 return oz_usb_get_frame_number();
1856 /*------------------------------------------------------------------------------
1858 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1859 * always do that in softirq context.
1861 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1863 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1868 spin_lock_bh(&ozhcd->hcd_lock);
1869 for (i = 0; i < OZ_NB_PORTS; i++) {
1870 if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
1871 oz_dbg(HUB, "Port %d changed\n", i);
1872 ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
1876 spin_unlock_bh(&ozhcd->hcd_lock);
1877 return buf[0] ? 1 : 0;
1879 /*------------------------------------------------------------------------------
1882 static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1883 struct usb_hub_descriptor *desc)
1885 memset(desc, 0, sizeof(*desc));
1886 desc->bDescriptorType = 0x29;
1887 desc->bDescLength = 9;
1888 desc->wHubCharacteristics = (__force __u16)
1889 __constant_cpu_to_le16(0x0001);
1890 desc->bNbrPorts = OZ_NB_PORTS;
1892 /*------------------------------------------------------------------------------
1895 static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1897 struct oz_port *port;
1899 u8 port_id = (u8)windex;
1900 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1901 unsigned set_bits = 0;
1902 unsigned clear_bits = 0;
1904 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
1906 port = &ozhcd->ports[port_id-1];
1908 case USB_PORT_FEAT_CONNECTION:
1909 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
1911 case USB_PORT_FEAT_ENABLE:
1912 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
1914 case USB_PORT_FEAT_SUSPEND:
1915 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
1917 case USB_PORT_FEAT_OVER_CURRENT:
1918 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
1920 case USB_PORT_FEAT_RESET:
1921 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
1922 set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
1923 clear_bits = USB_PORT_STAT_RESET;
1924 ozhcd->ports[port_id-1].bus_addr = 0;
1926 case USB_PORT_FEAT_POWER:
1927 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
1928 set_bits |= USB_PORT_STAT_POWER;
1930 case USB_PORT_FEAT_LOWSPEED:
1931 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
1933 case USB_PORT_FEAT_C_CONNECTION:
1934 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
1936 case USB_PORT_FEAT_C_ENABLE:
1937 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
1939 case USB_PORT_FEAT_C_SUSPEND:
1940 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
1942 case USB_PORT_FEAT_C_OVER_CURRENT:
1943 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
1945 case USB_PORT_FEAT_C_RESET:
1946 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
1948 case USB_PORT_FEAT_TEST:
1949 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
1951 case USB_PORT_FEAT_INDICATOR:
1952 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
1955 oz_dbg(HUB, "Other %d\n", wvalue);
1958 if (set_bits || clear_bits) {
1959 spin_lock_bh(&port->port_lock);
1960 port->status &= ~clear_bits;
1961 port->status |= set_bits;
1962 spin_unlock_bh(&port->port_lock);
1964 oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
1967 /*------------------------------------------------------------------------------
1970 static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1972 struct oz_port *port;
1974 u8 port_id = (u8)windex;
1975 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1976 unsigned clear_bits = 0;
1978 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
1980 port = &ozhcd->ports[port_id-1];
1982 case USB_PORT_FEAT_CONNECTION:
1983 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
1985 case USB_PORT_FEAT_ENABLE:
1986 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
1987 clear_bits = USB_PORT_STAT_ENABLE;
1989 case USB_PORT_FEAT_SUSPEND:
1990 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
1992 case USB_PORT_FEAT_OVER_CURRENT:
1993 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
1995 case USB_PORT_FEAT_RESET:
1996 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
1998 case USB_PORT_FEAT_POWER:
1999 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2000 clear_bits |= USB_PORT_STAT_POWER;
2002 case USB_PORT_FEAT_LOWSPEED:
2003 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2005 case USB_PORT_FEAT_C_CONNECTION:
2006 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2007 clear_bits = (USB_PORT_STAT_C_CONNECTION << 16);
2009 case USB_PORT_FEAT_C_ENABLE:
2010 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2011 clear_bits = (USB_PORT_STAT_C_ENABLE << 16);
2013 case USB_PORT_FEAT_C_SUSPEND:
2014 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2016 case USB_PORT_FEAT_C_OVER_CURRENT:
2017 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2019 case USB_PORT_FEAT_C_RESET:
2020 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2021 clear_bits = (USB_PORT_FEAT_C_RESET << 16);
2023 case USB_PORT_FEAT_TEST:
2024 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2026 case USB_PORT_FEAT_INDICATOR:
2027 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2030 oz_dbg(HUB, "Other %d\n", wvalue);
2034 spin_lock_bh(&port->port_lock);
2035 port->status &= ~clear_bits;
2036 spin_unlock_bh(&port->port_lock);
2038 oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2039 port_id, ozhcd->ports[port_id-1].status);
2042 /*------------------------------------------------------------------------------
2045 static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2047 struct oz_hcd *ozhcd;
2049 if ((windex < 1) || (windex > OZ_NB_PORTS))
2051 ozhcd = oz_hcd_private(hcd);
2052 oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
2053 status = ozhcd->ports[windex-1].status;
2054 put_unaligned(cpu_to_le32(status), (__le32 *)buf);
2055 oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
2058 /*------------------------------------------------------------------------------
2061 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2062 u16 windex, char *buf, u16 wlength)
2067 case ClearHubFeature:
2068 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
2070 case ClearPortFeature:
2071 err = oz_clear_port_feature(hcd, wvalue, windex);
2073 case GetHubDescriptor:
2074 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2077 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
2078 put_unaligned(__constant_cpu_to_le32(0), (__le32 *)buf);
2081 err = oz_get_port_status(hcd, windex, buf);
2084 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
2086 case SetPortFeature:
2087 err = oz_set_port_feature(hcd, wvalue, windex);
2090 oz_dbg(HUB, "Other: %d\n", req_type);
2095 /*------------------------------------------------------------------------------
2098 static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2100 struct oz_hcd *ozhcd;
2102 ozhcd = oz_hcd_private(hcd);
2103 spin_lock_bh(&ozhcd->hcd_lock);
2104 hcd->state = HC_STATE_SUSPENDED;
2105 ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2106 spin_unlock_bh(&ozhcd->hcd_lock);
2109 /*------------------------------------------------------------------------------
2112 static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2114 struct oz_hcd *ozhcd;
2116 ozhcd = oz_hcd_private(hcd);
2117 spin_lock_bh(&ozhcd->hcd_lock);
2118 ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2119 hcd->state = HC_STATE_RUNNING;
2120 spin_unlock_bh(&ozhcd->hcd_lock);
2123 /*------------------------------------------------------------------------------
2125 static void oz_plat_shutdown(struct platform_device *dev)
2129 /*------------------------------------------------------------------------------
2132 static int oz_plat_probe(struct platform_device *dev)
2136 struct usb_hcd *hcd;
2137 struct oz_hcd *ozhcd;
2139 hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
2141 oz_dbg(ON, "Failed to created hcd object OK\n");
2144 ozhcd = oz_hcd_private(hcd);
2145 memset(ozhcd, 0, sizeof(*ozhcd));
2146 INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2147 INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2148 INIT_LIST_HEAD(&ozhcd->orphanage);
2150 ozhcd->conn_port = -1;
2151 spin_lock_init(&ozhcd->hcd_lock);
2152 for (i = 0; i < OZ_NB_PORTS; i++) {
2153 struct oz_port *port = &ozhcd->ports[i];
2154 port->ozhcd = ozhcd;
2157 port->bus_addr = 0xff;
2158 spin_lock_init(&port->port_lock);
2160 err = usb_add_hcd(hcd, 0, 0);
2162 oz_dbg(ON, "Failed to add hcd object OK\n");
2166 spin_lock_bh(&g_hcdlock);
2168 spin_unlock_bh(&g_hcdlock);
2171 /*------------------------------------------------------------------------------
2174 static int oz_plat_remove(struct platform_device *dev)
2176 struct usb_hcd *hcd = platform_get_drvdata(dev);
2177 struct oz_hcd *ozhcd;
2181 ozhcd = oz_hcd_private(hcd);
2182 spin_lock_bh(&g_hcdlock);
2183 if (ozhcd == g_ozhcd)
2185 spin_unlock_bh(&g_hcdlock);
2186 oz_dbg(ON, "Clearing orphanage\n");
2187 oz_hcd_clear_orphanage(ozhcd, -EPIPE);
2188 oz_dbg(ON, "Removing hcd\n");
2189 usb_remove_hcd(hcd);
2191 oz_empty_link_pool();
2195 /*------------------------------------------------------------------------------
2198 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2203 /*------------------------------------------------------------------------------
2206 static int oz_plat_resume(struct platform_device *dev)
2211 /*------------------------------------------------------------------------------
2214 int oz_hcd_init(void)
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_dbg(ON, "platform_driver_register() returned %d\n", err);
2225 g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
2226 if (g_plat_dev == NULL) {
2230 oz_dbg(ON, "platform_device_alloc() succeeded\n");
2231 err = platform_device_add(g_plat_dev);
2234 oz_dbg(ON, "platform_device_add() succeeded\n");
2237 platform_device_put(g_plat_dev);
2239 platform_driver_unregister(&g_oz_plat_drv);
2241 tasklet_disable(&g_urb_process_tasklet);
2242 tasklet_disable(&g_urb_cancel_tasklet);
2243 oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
2246 /*------------------------------------------------------------------------------
2249 void oz_hcd_term(void)
2251 tasklet_kill(&g_urb_process_tasklet);
2252 tasklet_kill(&g_urb_cancel_tasklet);
2253 platform_device_unregister(g_plat_dev);
2254 platform_driver_unregister(&g_oz_plat_drv);
2255 oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));