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/jiffies.h>
30 #include <linux/slab.h>
31 #include <linux/export.h>
32 #include "linux/usb/hcd.h"
33 #include <asm/unaligned.h>
38 #include "ozurbparanoia.h"
40 /*------------------------------------------------------------------------------
41 * Number of units of buffering to capture for an isochronous IN endpoint before
42 * allowing data to be indicated up.
44 #define OZ_IN_BUFFERING_UNITS 50
45 /* Name of our platform device.
47 #define OZ_PLAT_DEV_NAME "ozwpan"
48 /* Maximum number of free urb links that can be kept in the pool.
50 #define OZ_MAX_LINK_POOL_SIZE 16
51 /* Get endpoint object from the containing link.
53 #define ep_from_link(__e) container_of((__e), struct oz_endpoint, link)
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 long submit_jiffies;
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 unsigned long last_jiffies;
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 const struct hc_driver g_oz_hc_drv = {
193 .description = g_hcd_name,
194 .product_desc = "Ozmo Devices WPAN",
195 .hcd_priv_size = sizeof(struct oz_hcd),
197 .start = oz_hcd_start,
199 .shutdown = oz_hcd_shutdown,
200 .urb_enqueue = oz_hcd_urb_enqueue,
201 .urb_dequeue = oz_hcd_urb_dequeue,
202 .endpoint_disable = oz_hcd_endpoint_disable,
203 .endpoint_reset = oz_hcd_endpoint_reset,
204 .get_frame_number = oz_hcd_get_frame_number,
205 .hub_status_data = oz_hcd_hub_status_data,
206 .hub_control = oz_hcd_hub_control,
207 .bus_suspend = oz_hcd_bus_suspend,
208 .bus_resume = oz_hcd_bus_resume,
211 static struct platform_driver g_oz_plat_drv = {
212 .probe = oz_plat_probe,
213 .remove = oz_plat_remove,
214 .shutdown = oz_plat_shutdown,
215 .suspend = oz_plat_suspend,
216 .resume = oz_plat_resume,
218 .name = OZ_PLAT_DEV_NAME,
219 .owner = THIS_MODULE,
222 /*------------------------------------------------------------------------------
223 * Gets our private context area (which is of type struct oz_hcd) from the
227 static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
229 return (struct oz_hcd *)hcd->hcd_priv;
231 /*------------------------------------------------------------------------------
232 * Searches list of ports to find the index of the one with a specified USB
233 * bus address. If none of the ports has the bus address then the connection
234 * port is returned, if there is one or -1 otherwise.
237 static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
240 for (i = 0; i < OZ_NB_PORTS; i++) {
241 if (ozhcd->ports[i].bus_addr == bus_addr)
244 return ozhcd->conn_port;
246 /*------------------------------------------------------------------------------
247 * Allocates an urb link, first trying the pool but going to heap if empty.
250 static struct oz_urb_link *oz_alloc_urb_link(void)
252 struct oz_urb_link *urbl = NULL;
253 unsigned long irq_state;
254 spin_lock_irqsave(&g_link_lock, irq_state);
256 urbl = container_of(g_link_pool, struct oz_urb_link, link);
257 g_link_pool = urbl->link.next;
260 spin_unlock_irqrestore(&g_link_lock, irq_state);
262 urbl = kmalloc(sizeof(struct oz_urb_link), GFP_ATOMIC);
265 /*------------------------------------------------------------------------------
266 * Frees an urb link by putting it in the pool if there is enough space or
267 * deallocating it to heap otherwise.
270 static void oz_free_urb_link(struct oz_urb_link *urbl)
273 unsigned long irq_state;
274 spin_lock_irqsave(&g_link_lock, irq_state);
275 if (g_link_pool_size < OZ_MAX_LINK_POOL_SIZE) {
276 urbl->link.next = g_link_pool;
277 g_link_pool = &urbl->link;
281 spin_unlock_irqrestore(&g_link_lock, irq_state);
285 /*------------------------------------------------------------------------------
286 * Deallocates all the urb links in the pool.
289 static void oz_empty_link_pool(void)
292 unsigned long irq_state;
293 spin_lock_irqsave(&g_link_lock, irq_state);
296 g_link_pool_size = 0;
297 spin_unlock_irqrestore(&g_link_lock, irq_state);
299 struct oz_urb_link *urbl =
300 container_of(e, struct oz_urb_link, link);
305 /*------------------------------------------------------------------------------
306 * Allocates endpoint structure and optionally a buffer. If a buffer is
307 * allocated it immediately follows the endpoint structure.
310 static struct oz_endpoint *oz_ep_alloc(gfp_t mem_flags, int buffer_size)
312 struct oz_endpoint *ep =
313 kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
315 INIT_LIST_HEAD(&ep->urb_list);
316 INIT_LIST_HEAD(&ep->link);
319 ep->buffer_size = buffer_size;
320 ep->buffer = (u8 *)(ep+1);
325 /*------------------------------------------------------------------------------
326 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
328 * Context: softirq or process
330 static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd, struct urb *urb)
332 struct oz_urb_link *urbl;
334 list_for_each(e, &ozhcd->urb_cancel_list) {
335 urbl = container_of(e, struct oz_urb_link, link);
336 if (urb == urbl->urb) {
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
348 static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
349 int status, unsigned long submit_jiffies)
351 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
352 unsigned long irq_state;
353 struct oz_urb_link *cancel_urbl = NULL;
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.
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.
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.
373 spin_unlock(&g_tasklet_lock);
374 if (oz_forget_urb(urb)) {
375 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
377 static unsigned long last_time;
378 atomic_dec(&g_pending_urbs);
379 oz_dbg(URB, "%lu: giveback_urb(%p,%x) %lu %lu pending:%d\n",
380 jiffies, urb, status, jiffies-submit_jiffies,
381 jiffies-last_time, atomic_read(&g_pending_urbs));
383 usb_hcd_giveback_urb(hcd, urb, status);
385 spin_lock(&g_tasklet_lock);
386 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
388 oz_free_urb_link(cancel_urbl);
390 /*------------------------------------------------------------------------------
391 * Deallocates an endpoint including deallocating any associated stream and
392 * returning any queued urbs to the core.
395 static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
398 struct list_head list;
399 struct oz_hcd *ozhcd = port->ozhcd;
400 INIT_LIST_HEAD(&list);
401 if (ep->flags & OZ_F_EP_HAVE_STREAM)
402 oz_usb_stream_delete(port->hpd, ep->ep_num);
403 /* Transfer URBs to the orphanage while we hold the lock. */
404 spin_lock_bh(&ozhcd->hcd_lock);
405 /* Note: this works even if ep->urb_list is empty.*/
406 list_replace_init(&ep->urb_list, &list);
407 /* Put the URBs in the orphanage. */
408 list_splice_tail(&list, &ozhcd->orphanage);
409 spin_unlock_bh(&ozhcd->hcd_lock);
411 oz_dbg(ON, "Freeing endpoint memory\n");
414 /*------------------------------------------------------------------------------
417 static void oz_complete_buffered_urb(struct oz_port *port,
418 struct oz_endpoint *ep,
421 u8 data_len, available_space, copy_len;
423 memcpy(&data_len, &ep->buffer[ep->out_ix], sizeof(u8));
424 if (data_len <= urb->transfer_buffer_length)
425 available_space = data_len;
427 available_space = urb->transfer_buffer_length;
429 if (++ep->out_ix == ep->buffer_size)
431 copy_len = ep->buffer_size - ep->out_ix;
432 if (copy_len >= available_space)
433 copy_len = available_space;
434 memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
436 if (copy_len < available_space) {
437 memcpy((urb->transfer_buffer + copy_len), ep->buffer,
438 (available_space - copy_len));
439 ep->out_ix = available_space - copy_len;
441 ep->out_ix += copy_len;
443 urb->actual_length = available_space;
444 if (ep->out_ix == ep->buffer_size)
447 ep->buffered_units--;
448 oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
450 oz_complete_urb(port->ozhcd->hcd, urb, 0, 0);
453 /*------------------------------------------------------------------------------
456 static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
457 struct urb *urb, u8 req_id)
459 struct oz_urb_link *urbl;
460 struct oz_endpoint *ep;
462 if (ep_addr >= OZ_NB_ENDPOINTS) {
463 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
466 urbl = oz_alloc_urb_link();
469 urbl->submit_jiffies = jiffies;
471 urbl->req_id = req_id;
472 urbl->ep_num = ep_addr;
473 /* Hold lock while we insert the URB into the list within the
474 * endpoint structure.
476 spin_lock_bh(&port->ozhcd->hcd_lock);
477 /* If the urb has been unlinked while out of any list then
481 spin_unlock_bh(&port->ozhcd->hcd_lock);
482 oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
483 oz_complete_urb(port->ozhcd->hcd, urb, 0, 0);
484 oz_free_urb_link(urbl);
488 ep = port->in_ep[ep_addr];
490 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 ep->last_jiffies = jiffies;
512 spin_unlock_bh(&port->ozhcd->hcd_lock);
514 oz_free_urb_link(urbl);
517 /*------------------------------------------------------------------------------
518 * Removes an urb from the queue in the endpoint.
519 * Returns 0 if it is found and -EIDRM otherwise.
522 static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
525 struct oz_urb_link *urbl = NULL;
526 struct oz_endpoint *ep;
527 spin_lock_bh(&port->ozhcd->hcd_lock);
529 ep = port->in_ep[ep_addr];
531 ep = port->out_ep[ep_addr];
534 list_for_each(e, &ep->urb_list) {
535 urbl = container_of(e, struct oz_urb_link, link);
536 if (urbl->urb == urb) {
543 spin_unlock_bh(&port->ozhcd->hcd_lock);
545 oz_free_urb_link(urbl);
546 return urbl ? 0 : -EIDRM;
548 /*------------------------------------------------------------------------------
549 * Finds an urb given its request id.
552 static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
555 struct oz_hcd *ozhcd = port->ozhcd;
556 struct urb *urb = NULL;
557 struct oz_urb_link *urbl = NULL;
558 struct oz_endpoint *ep;
560 spin_lock_bh(&ozhcd->hcd_lock);
561 ep = port->out_ep[ep_ix];
564 list_for_each(e, &ep->urb_list) {
565 urbl = container_of(e, struct oz_urb_link, link);
566 if (urbl->req_id == req_id) {
573 spin_unlock_bh(&ozhcd->hcd_lock);
574 /* If urb is non-zero then we we must have an urb link to delete.
577 oz_free_urb_link(urbl);
580 /*------------------------------------------------------------------------------
581 * Pre-condition: Port lock must be held.
584 static void oz_acquire_port(struct oz_port *port, void *hpd)
586 INIT_LIST_HEAD(&port->isoc_out_ep);
587 INIT_LIST_HEAD(&port->isoc_in_ep);
588 port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
589 port->status |= USB_PORT_STAT_CONNECTION |
590 (USB_PORT_STAT_C_CONNECTION << 16);
594 /*------------------------------------------------------------------------------
597 static struct oz_hcd *oz_hcd_claim(void)
599 struct oz_hcd *ozhcd;
600 spin_lock_bh(&g_hcdlock);
603 usb_get_hcd(ozhcd->hcd);
604 spin_unlock_bh(&g_hcdlock);
607 /*------------------------------------------------------------------------------
610 static inline void oz_hcd_put(struct oz_hcd *ozhcd)
613 usb_put_hcd(ozhcd->hcd);
615 /*------------------------------------------------------------------------------
616 * This is called by the protocol handler to notify that a PD has arrived.
617 * We allocate a port to associate with the PD and create a structure for
618 * endpoint 0. This port is made the connection port.
619 * In the event that one of the other port is already a connection port then
621 * TODO We should be able to do better than fail and should be able remember
622 * that this port needs configuring and make it the connection port once the
623 * current connection port has been assigned an address. Collisions here are
624 * probably very rare indeed.
627 void *oz_hcd_pd_arrived(void *hpd)
631 struct oz_hcd *ozhcd = NULL;
632 struct oz_endpoint *ep;
633 ozhcd = oz_hcd_claim();
636 /* Allocate an endpoint object in advance (before holding hcd lock) to
637 * use for out endpoint 0.
639 ep = oz_ep_alloc(GFP_ATOMIC, 0);
640 spin_lock_bh(&ozhcd->hcd_lock);
641 if (ozhcd->conn_port >= 0) {
642 spin_unlock_bh(&ozhcd->hcd_lock);
643 oz_dbg(ON, "conn_port >= 0\n");
646 for (i = 0; i < OZ_NB_PORTS; i++) {
647 struct oz_port *port = &ozhcd->ports[i];
648 spin_lock(&port->port_lock);
649 if ((port->flags & OZ_PORT_F_PRESENT) == 0) {
650 oz_acquire_port(port, hpd);
651 spin_unlock(&port->port_lock);
654 spin_unlock(&port->port_lock);
656 if (i < OZ_NB_PORTS) {
657 oz_dbg(ON, "Setting conn_port = %d\n", i);
658 ozhcd->conn_port = i;
659 /* Attach out endpoint 0.
661 ozhcd->ports[i].out_ep[0] = ep;
663 hport = &ozhcd->ports[i];
664 spin_unlock_bh(&ozhcd->hcd_lock);
665 if (ozhcd->flags & OZ_HDC_F_SUSPENDED) {
666 oz_dbg(ON, "Resuming root hub\n");
667 usb_hcd_resume_root_hub(ozhcd->hcd);
669 usb_hcd_poll_rh_status(ozhcd->hcd);
671 spin_unlock_bh(&ozhcd->hcd_lock);
674 if (ep) /* ep is non-null if not used. */
675 oz_ep_free(NULL, ep);
679 /*------------------------------------------------------------------------------
680 * This is called by the protocol handler to notify that the PD has gone away.
681 * We need to deallocate all resources and then request that the root hub is
682 * polled. We release the reference we hold on the PD.
685 void oz_hcd_pd_departed(void *hport)
687 struct oz_port *port = (struct oz_port *)hport;
688 struct oz_hcd *ozhcd;
690 struct oz_endpoint *ep = NULL;
693 oz_dbg(ON, "%s: port = 0\n", __func__);
699 /* Check if this is the connection port - if so clear it.
701 spin_lock_bh(&ozhcd->hcd_lock);
702 if ((ozhcd->conn_port >= 0) &&
703 (port == &ozhcd->ports[ozhcd->conn_port])) {
704 oz_dbg(ON, "Clearing conn_port\n");
705 ozhcd->conn_port = -1;
707 spin_lock(&port->port_lock);
708 port->flags |= OZ_PORT_F_DYING;
709 spin_unlock(&port->port_lock);
710 spin_unlock_bh(&ozhcd->hcd_lock);
712 oz_clean_endpoints_for_config(ozhcd->hcd, port);
713 spin_lock_bh(&port->port_lock);
716 port->bus_addr = 0xff;
717 port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
718 port->flags |= OZ_PORT_F_CHANGED;
719 port->status &= ~USB_PORT_STAT_CONNECTION;
720 port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
721 /* If there is an endpont 0 then clear the pointer while we hold
722 * the spinlock be we deallocate it after releasing the lock.
724 if (port->out_ep[0]) {
725 ep = port->out_ep[0];
726 port->out_ep[0] = NULL;
728 spin_unlock_bh(&port->port_lock);
730 oz_ep_free(port, ep);
731 usb_hcd_poll_rh_status(ozhcd->hcd);
734 /*------------------------------------------------------------------------------
737 void oz_hcd_pd_reset(void *hpd, void *hport)
739 /* Cleanup the current configuration and report reset to the core.
741 struct oz_port *port = (struct oz_port *)hport;
742 struct oz_hcd *ozhcd = port->ozhcd;
743 oz_dbg(ON, "PD Reset\n");
744 spin_lock_bh(&port->port_lock);
745 port->flags |= OZ_PORT_F_CHANGED;
746 port->status |= USB_PORT_STAT_RESET;
747 port->status |= (USB_PORT_STAT_C_RESET << 16);
748 spin_unlock_bh(&port->port_lock);
749 oz_clean_endpoints_for_config(ozhcd->hcd, port);
750 usb_hcd_poll_rh_status(ozhcd->hcd);
752 /*------------------------------------------------------------------------------
755 void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
756 int length, int offset, int total_size)
758 struct oz_port *port = (struct oz_port *)hport;
762 oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
763 length, offset, total_size);
764 urb = oz_find_urb_by_id(port, 0, req_id);
769 int required_size = urb->transfer_buffer_length;
770 if (required_size > total_size)
771 required_size = total_size;
772 copy_len = required_size-offset;
773 if (length <= copy_len)
775 memcpy(urb->transfer_buffer+offset, desc, copy_len);
777 if (offset < required_size) {
778 struct usb_ctrlrequest *setup =
779 (struct usb_ctrlrequest *)urb->setup_packet;
780 unsigned wvalue = le16_to_cpu(setup->wValue);
781 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
783 else if (oz_usb_get_desc_req(port->hpd, req_id,
784 setup->bRequestType, (u8)(wvalue>>8),
785 (u8)wvalue, setup->wIndex, offset,
786 required_size-offset)) {
787 oz_dequeue_ep_urb(port, 0, 0, urb);
794 urb->actual_length = total_size;
795 oz_complete_urb(port->ozhcd->hcd, urb, 0, 0);
797 /*------------------------------------------------------------------------------
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 #define oz_display_conf_type(__x)
841 #endif /* WANT_TRACE */
842 /*------------------------------------------------------------------------------
845 static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
846 u8 rcode, u8 config_num)
849 struct usb_hcd *hcd = port->ozhcd->hcd;
851 port->config_num = config_num;
852 oz_clean_endpoints_for_config(hcd, port);
853 if (oz_build_endpoints_for_config(hcd, port,
854 &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
860 oz_complete_urb(hcd, urb, rc, 0);
862 /*------------------------------------------------------------------------------
865 static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
866 u8 rcode, u8 if_num, u8 alt)
868 struct usb_hcd *hcd = port->ozhcd->hcd;
871 struct usb_host_config *config;
872 struct usb_host_interface *intf;
873 oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
874 oz_clean_endpoints_for_interface(hcd, port, if_num);
875 config = &urb->dev->config[port->config_num-1];
876 intf = &config->intf_cache[if_num]->altsetting[alt];
877 if (oz_build_endpoints_for_interface(hcd, port, intf,
881 port->iface[if_num].alt = alt;
885 oz_complete_urb(hcd, urb, rc, 0);
887 /*------------------------------------------------------------------------------
890 void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
893 struct oz_port *port = (struct oz_port *)hport;
895 struct usb_ctrlrequest *setup;
896 struct usb_hcd *hcd = port->ozhcd->hcd;
900 oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
901 urb = oz_find_urb_by_id(port, 0, req_id);
903 oz_dbg(ON, "URB not found\n");
906 setup = (struct usb_ctrlrequest *)urb->setup_packet;
907 windex = le16_to_cpu(setup->wIndex);
908 wvalue = le16_to_cpu(setup->wValue);
909 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
910 /* Standard requests */
911 oz_display_conf_type(setup->bRequest);
912 switch (setup->bRequest) {
913 case USB_REQ_SET_CONFIGURATION:
914 oz_hcd_complete_set_config(port, urb, rcode,
917 case USB_REQ_SET_INTERFACE:
918 oz_hcd_complete_set_interface(port, urb, rcode,
919 (u8)windex, (u8)wvalue);
922 oz_complete_urb(hcd, urb, 0, 0);
927 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
929 if (data_len <= urb->transfer_buffer_length)
932 copy_len = urb->transfer_buffer_length;
933 memcpy(urb->transfer_buffer, data, copy_len);
934 urb->actual_length = copy_len;
936 oz_complete_urb(hcd, urb, 0, 0);
939 /*------------------------------------------------------------------------------
940 * Context: softirq-serialized
942 static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
949 space = ep->out_ix-ep->in_ix-1;
951 space += ep->buffer_size;
952 if (space < (data_len+1)) {
953 oz_dbg(ON, "Buffer full\n");
956 ep->buffer[ep->in_ix] = (u8)data_len;
957 if (++ep->in_ix == ep->buffer_size)
959 copy_len = ep->buffer_size - ep->in_ix;
960 if (copy_len > data_len)
962 memcpy(&ep->buffer[ep->in_ix], data, copy_len);
964 if (copy_len < data_len) {
965 memcpy(ep->buffer, data+copy_len, data_len-copy_len);
966 ep->in_ix = data_len-copy_len;
968 ep->in_ix += copy_len;
970 if (ep->in_ix == ep->buffer_size)
972 ep->buffered_units++;
975 /*------------------------------------------------------------------------------
976 * Context: softirq-serialized
978 void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
980 struct oz_port *port = (struct oz_port *)hport;
981 struct oz_endpoint *ep;
982 struct oz_hcd *ozhcd = port->ozhcd;
983 spin_lock_bh(&ozhcd->hcd_lock);
984 ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
987 switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
988 case USB_ENDPOINT_XFER_INT:
989 case USB_ENDPOINT_XFER_BULK:
990 if (!list_empty(&ep->urb_list)) {
991 struct oz_urb_link *urbl =
992 list_first_entry(&ep->urb_list,
993 struct oz_urb_link, link);
996 list_del_init(&urbl->link);
997 spin_unlock_bh(&ozhcd->hcd_lock);
999 oz_free_urb_link(urbl);
1000 if (data_len <= urb->transfer_buffer_length)
1001 copy_len = data_len;
1003 copy_len = urb->transfer_buffer_length;
1004 memcpy(urb->transfer_buffer, data, copy_len);
1005 urb->actual_length = copy_len;
1006 oz_complete_urb(port->ozhcd->hcd, urb, 0, 0);
1009 oz_dbg(ON, "buffering frame as URB is not available\n");
1010 oz_hcd_buffer_data(ep, data, data_len);
1013 case USB_ENDPOINT_XFER_ISOC:
1014 oz_hcd_buffer_data(ep, data, data_len);
1018 spin_unlock_bh(&ozhcd->hcd_lock);
1020 /*------------------------------------------------------------------------------
1023 static inline int oz_usb_get_frame_number(void)
1025 return jiffies_to_msecs(get_jiffies_64());
1027 /*------------------------------------------------------------------------------
1030 int oz_hcd_heartbeat(void *hport)
1033 struct oz_port *port = (struct oz_port *)hport;
1034 struct oz_hcd *ozhcd = port->ozhcd;
1035 struct oz_urb_link *urbl;
1036 struct list_head xfr_list;
1037 struct list_head *e;
1038 struct list_head *n;
1040 struct oz_endpoint *ep;
1041 unsigned long now = jiffies;
1042 INIT_LIST_HEAD(&xfr_list);
1043 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1045 spin_lock_bh(&ozhcd->hcd_lock);
1046 list_for_each(e, &port->isoc_out_ep) {
1047 ep = ep_from_link(e);
1050 ep->credit += jiffies_to_msecs(now - ep->last_jiffies);
1051 if (ep->credit > ep->credit_ceiling)
1052 ep->credit = ep->credit_ceiling;
1053 ep->last_jiffies = now;
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;
1061 list_move_tail(&urbl->link, &xfr_list);
1064 spin_unlock_bh(&ozhcd->hcd_lock);
1065 /* Send to PD and complete URBs.
1067 list_for_each_safe(e, n, &xfr_list) {
1069 urbl = container_of(e, struct oz_urb_link, link);
1071 t = urbl->submit_jiffies;
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, t);
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;
1088 ep->last_jiffies = now;
1089 ep->start_frame = 0;
1093 ep->credit += jiffies_to_msecs(now - ep->last_jiffies);
1094 ep->last_jiffies = now;
1095 while (!list_empty(&ep->urb_list)) {
1096 struct oz_urb_link *urbl =
1097 list_first_entry(&ep->urb_list,
1098 struct oz_urb_link, link);
1099 struct urb *urb = urbl->urb;
1103 if ((ep->credit + 1) < urb->number_of_packets)
1105 if (ep->buffered_units < urb->number_of_packets)
1107 urb->actual_length = 0;
1108 for (i = 0; i < urb->number_of_packets; i++) {
1109 len = ep->buffer[ep->out_ix];
1110 if (++ep->out_ix == ep->buffer_size)
1112 copy_len = ep->buffer_size - ep->out_ix;
1115 memcpy(urb->transfer_buffer,
1116 &ep->buffer[ep->out_ix], copy_len);
1117 if (copy_len < len) {
1118 memcpy(urb->transfer_buffer+copy_len,
1119 ep->buffer, len-copy_len);
1120 ep->out_ix = len-copy_len;
1122 ep->out_ix += copy_len;
1123 if (ep->out_ix == ep->buffer_size)
1125 urb->iso_frame_desc[i].offset =
1127 urb->actual_length += len;
1128 urb->iso_frame_desc[i].actual_length = len;
1129 urb->iso_frame_desc[i].status = 0;
1131 ep->buffered_units -= urb->number_of_packets;
1132 urb->error_count = 0;
1133 urb->start_frame = ep->start_frame;
1134 ep->start_frame += urb->number_of_packets;
1135 list_move_tail(&urbl->link, &xfr_list);
1136 ep->credit -= urb->number_of_packets;
1139 if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1141 spin_unlock_bh(&ozhcd->hcd_lock);
1142 /* Complete the filled URBs.
1144 list_for_each_safe(e, n, &xfr_list) {
1145 urbl = container_of(e, struct oz_urb_link, link);
1148 oz_free_urb_link(urbl);
1149 oz_complete_urb(port->ozhcd->hcd, urb, 0, 0);
1151 /* Check if there are any ep0 requests that have timed out.
1152 * If so resent to PD.
1154 ep = port->out_ep[0];
1156 struct list_head *e;
1157 struct list_head *n;
1158 spin_lock_bh(&ozhcd->hcd_lock);
1159 list_for_each_safe(e, n, &ep->urb_list) {
1160 urbl = container_of(e, struct oz_urb_link, link);
1161 if (time_after(now, urbl->submit_jiffies+HZ/2)) {
1162 oz_dbg(ON, "%ld: Request 0x%p timeout\n",
1164 urbl->submit_jiffies = now;
1165 list_move_tail(e, &xfr_list);
1168 if (!list_empty(&ep->urb_list))
1170 spin_unlock_bh(&ozhcd->hcd_lock);
1172 while (e != &xfr_list) {
1173 urbl = container_of(e, struct oz_urb_link, link);
1175 oz_dbg(ON, "Resending request to PD\n");
1176 oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1177 oz_free_urb_link(urbl);
1182 /*------------------------------------------------------------------------------
1185 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1186 struct oz_port *port,
1187 struct usb_host_interface *intf, gfp_t mem_flags)
1189 struct oz_hcd *ozhcd = port->ozhcd;
1191 int if_ix = intf->desc.bInterfaceNumber;
1192 int request_heartbeat = 0;
1193 oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
1194 for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1195 struct usb_host_endpoint *hep = &intf->endpoint[i];
1196 u8 ep_addr = hep->desc.bEndpointAddress;
1197 u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1198 struct oz_endpoint *ep;
1199 int buffer_size = 0;
1201 oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
1202 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1203 switch (hep->desc.bmAttributes &
1204 USB_ENDPOINT_XFERTYPE_MASK) {
1205 case USB_ENDPOINT_XFER_ISOC:
1206 buffer_size = 24*1024;
1208 case USB_ENDPOINT_XFER_INT:
1214 ep = oz_ep_alloc(mem_flags, buffer_size);
1216 oz_clean_endpoints_for_interface(hcd, port, if_ix);
1219 ep->attrib = hep->desc.bmAttributes;
1220 ep->ep_num = ep_num;
1221 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1222 == USB_ENDPOINT_XFER_ISOC) {
1223 oz_dbg(ON, "wMaxPacketSize = %d\n",
1224 usb_endpoint_maxp(&hep->desc));
1225 ep->credit_ceiling = 200;
1226 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1227 ep->flags |= OZ_F_EP_BUFFERING;
1229 ep->flags |= OZ_F_EP_HAVE_STREAM;
1230 if (oz_usb_stream_create(port->hpd, ep_num))
1231 ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1234 spin_lock_bh(&ozhcd->hcd_lock);
1235 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1236 port->in_ep[ep_num] = ep;
1237 port->iface[if_ix].ep_mask |=
1238 (1<<(ep_num+OZ_NB_ENDPOINTS));
1239 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1240 == USB_ENDPOINT_XFER_ISOC) {
1241 list_add_tail(&ep->link, &port->isoc_in_ep);
1242 request_heartbeat = 1;
1245 port->out_ep[ep_num] = ep;
1246 port->iface[if_ix].ep_mask |= (1<<ep_num);
1247 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1248 == USB_ENDPOINT_XFER_ISOC) {
1249 list_add_tail(&ep->link, &port->isoc_out_ep);
1250 request_heartbeat = 1;
1253 spin_unlock_bh(&ozhcd->hcd_lock);
1254 if (request_heartbeat && port->hpd)
1255 oz_usb_request_heartbeat(port->hpd);
1259 /*------------------------------------------------------------------------------
1262 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1263 struct oz_port *port, int if_ix)
1265 struct oz_hcd *ozhcd = port->ozhcd;
1268 struct list_head ep_list;
1270 oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
1271 if (if_ix >= port->num_iface)
1273 INIT_LIST_HEAD(&ep_list);
1274 spin_lock_bh(&ozhcd->hcd_lock);
1275 mask = port->iface[if_ix].ep_mask;
1276 port->iface[if_ix].ep_mask = 0;
1277 for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1278 struct list_head *e;
1279 /* Gather OUT endpoints.
1281 if ((mask & (1<<i)) && port->out_ep[i]) {
1282 e = &port->out_ep[i]->link;
1283 port->out_ep[i] = NULL;
1284 /* Remove from isoc list if present.
1286 list_move_tail(e, &ep_list);
1288 /* Gather IN endpoints.
1290 if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1291 e = &port->in_ep[i]->link;
1292 port->in_ep[i] = NULL;
1293 list_move_tail(e, &ep_list);
1296 spin_unlock_bh(&ozhcd->hcd_lock);
1297 while (!list_empty(&ep_list)) {
1298 struct oz_endpoint *ep =
1299 list_first_entry(&ep_list, struct oz_endpoint, link);
1300 list_del_init(&ep->link);
1301 oz_ep_free(port, ep);
1304 /*------------------------------------------------------------------------------
1307 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1308 struct oz_port *port, struct usb_host_config *config,
1311 struct oz_hcd *ozhcd = port->ozhcd;
1313 int num_iface = config->desc.bNumInterfaces;
1315 struct oz_interface *iface;
1317 iface = kmalloc(num_iface*sizeof(struct oz_interface),
1318 mem_flags | __GFP_ZERO);
1321 spin_lock_bh(&ozhcd->hcd_lock);
1322 port->iface = iface;
1323 port->num_iface = num_iface;
1324 spin_unlock_bh(&ozhcd->hcd_lock);
1326 for (i = 0; i < num_iface; i++) {
1327 struct usb_host_interface *intf =
1328 &config->intf_cache[i]->altsetting[0];
1329 if (oz_build_endpoints_for_interface(hcd, port, intf,
1335 oz_clean_endpoints_for_config(hcd, port);
1338 /*------------------------------------------------------------------------------
1341 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1342 struct oz_port *port)
1344 struct oz_hcd *ozhcd = port->ozhcd;
1346 oz_dbg(ON, "Deleting endpoints for configuration\n");
1347 for (i = 0; i < port->num_iface; i++)
1348 oz_clean_endpoints_for_interface(hcd, port, i);
1349 spin_lock_bh(&ozhcd->hcd_lock);
1351 oz_dbg(ON, "Freeing interfaces object\n");
1355 port->num_iface = 0;
1356 spin_unlock_bh(&ozhcd->hcd_lock);
1358 /*------------------------------------------------------------------------------
1361 static void *oz_claim_hpd(struct oz_port *port)
1364 struct oz_hcd *ozhcd = port->ozhcd;
1365 spin_lock_bh(&ozhcd->hcd_lock);
1369 spin_unlock_bh(&ozhcd->hcd_lock);
1372 /*------------------------------------------------------------------------------
1375 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1378 struct usb_ctrlrequest *setup;
1385 unsigned complete = 0;
1388 struct oz_port *port = NULL;
1390 oz_dbg(URB, "%lu: %s: (%p)\n", jiffies, __func__, urb);
1391 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1396 port = &ozhcd->ports[port_ix];
1397 if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1398 || (port->flags & OZ_PORT_F_DYING)) {
1399 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1400 port_ix, urb->dev->devnum);
1404 /* Store port in private context data.
1407 setup = (struct usb_ctrlrequest *)urb->setup_packet;
1408 windex = le16_to_cpu(setup->wIndex);
1409 wvalue = le16_to_cpu(setup->wValue);
1410 wlength = le16_to_cpu(setup->wLength);
1411 oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
1412 oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1413 oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
1414 oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
1415 oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
1417 req_id = port->next_req_id++;
1418 hpd = oz_claim_hpd(port);
1420 oz_dbg(ON, "Cannot claim port\n");
1425 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1426 /* Standard requests
1428 switch (setup->bRequest) {
1429 case USB_REQ_GET_DESCRIPTOR:
1430 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
1432 case USB_REQ_SET_ADDRESS:
1433 oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
1434 oz_dbg(ON, "Port %d address is 0x%x\n",
1436 (u8)le16_to_cpu(setup->wValue));
1437 spin_lock_bh(&ozhcd->hcd_lock);
1438 if (ozhcd->conn_port >= 0) {
1439 ozhcd->ports[ozhcd->conn_port].bus_addr =
1440 (u8)le16_to_cpu(setup->wValue);
1441 oz_dbg(ON, "Clearing conn_port\n");
1442 ozhcd->conn_port = -1;
1444 spin_unlock_bh(&ozhcd->hcd_lock);
1447 case USB_REQ_SET_CONFIGURATION:
1448 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
1450 case USB_REQ_GET_CONFIGURATION:
1451 /* We short circuit this case and reply directly since
1452 * we have the selected configuration number cached.
1454 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
1455 if (urb->transfer_buffer_length >= 1) {
1456 urb->actual_length = 1;
1457 *((u8 *)urb->transfer_buffer) =
1464 case USB_REQ_GET_INTERFACE:
1465 /* We short circuit this case and reply directly since
1466 * we have the selected interface alternative cached.
1468 oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
1469 if (urb->transfer_buffer_length >= 1) {
1470 urb->actual_length = 1;
1471 *((u8 *)urb->transfer_buffer) =
1472 port->iface[(u8)windex].alt;
1473 oz_dbg(ON, "interface = %d alt = %d\n",
1474 windex, port->iface[(u8)windex].alt);
1480 case USB_REQ_SET_INTERFACE:
1481 oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
1485 if (!rc && !complete) {
1487 if ((setup->bRequestType & USB_DIR_IN) == 0)
1489 urb->actual_length = data_len;
1490 if (oz_usb_control_req(port->hpd, req_id, setup,
1491 urb->transfer_buffer, data_len)) {
1494 /* Note: we are queuing the request after we have
1495 * submitted it to be transmitted. If the request were
1496 * to complete before we queued it then it would not
1497 * be found in the queue. It seems impossible for
1498 * this to happen but if it did the request would
1499 * be resubmitted so the problem would hopefully
1500 * resolve itself. Putting the request into the
1501 * queue before it has been sent is worse since the
1502 * urb could be cancelled while we are using it
1503 * to build the request.
1505 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1511 if (rc || complete) {
1512 oz_dbg(ON, "Completing request locally\n");
1513 oz_complete_urb(ozhcd->hcd, urb, rc, 0);
1515 oz_usb_request_heartbeat(port->hpd);
1518 /*------------------------------------------------------------------------------
1521 static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1524 struct oz_port *port = urb->hcpriv;
1526 /* When we are paranoid we keep a list of urbs which we check against
1527 * before handing one back. This is just for debugging during
1528 * development and should be turned off in the released driver.
1530 oz_remember_urb(urb);
1531 /* Check buffer is valid.
1533 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1535 /* Check if there is a device at the port - refuse if not.
1537 if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1539 ep_addr = usb_pipeendpoint(urb->pipe);
1541 /* If the request is not for EP0 then queue it.
1543 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1547 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1551 /*------------------------------------------------------------------------------
1554 static void oz_urb_process_tasklet(unsigned long unused)
1556 unsigned long irq_state;
1558 struct oz_hcd *ozhcd = oz_hcd_claim();
1562 /* This is called from a tasklet so is in softirq context but the urb
1563 * list is filled from any context so we need to lock
1564 * appropriately while removing urbs.
1566 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1567 while (!list_empty(&ozhcd->urb_pending_list)) {
1568 struct oz_urb_link *urbl =
1569 list_first_entry(&ozhcd->urb_pending_list,
1570 struct oz_urb_link, link);
1571 list_del_init(&urbl->link);
1572 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1574 oz_free_urb_link(urbl);
1575 rc = oz_urb_process(ozhcd, urb);
1577 oz_complete_urb(ozhcd->hcd, urb, rc, 0);
1578 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1580 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1583 /*------------------------------------------------------------------------------
1584 * This function searches for the urb in any of the lists it could be in.
1585 * If it is found it is removed from the list and completed. If the urb is
1586 * being processed then it won't be in a list so won't be found. However, the
1587 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1588 * to a non-zero value. When an attempt is made to put the urb back in a list
1589 * the unlinked field will be checked and the urb will then be completed.
1592 static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1594 struct oz_urb_link *urbl = NULL;
1595 struct list_head *e;
1596 struct oz_hcd *ozhcd;
1597 unsigned long irq_state;
1600 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
1603 ozhcd = port->ozhcd;
1604 if (ozhcd == NULL) {
1605 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
1609 /* Look in the tasklet queue.
1611 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1612 list_for_each(e, &ozhcd->urb_cancel_list) {
1613 urbl = container_of(e, struct oz_urb_link, link);
1614 if (urb == urbl->urb) {
1616 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1620 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1623 /* Look in the orphanage.
1625 spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1626 list_for_each(e, &ozhcd->orphanage) {
1627 urbl = container_of(e, struct oz_urb_link, link);
1628 if (urbl->urb == urb) {
1630 oz_dbg(ON, "Found urb in orphanage\n");
1634 ix = (ep_num & 0xf);
1636 if ((ep_num & USB_DIR_IN) && ix)
1637 urbl = oz_remove_urb(port->in_ep[ix], urb);
1639 urbl = oz_remove_urb(port->out_ep[ix], urb);
1641 spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1644 urb->actual_length = 0;
1645 oz_free_urb_link(urbl);
1646 oz_complete_urb(ozhcd->hcd, urb, -EPIPE, 0);
1649 /*------------------------------------------------------------------------------
1652 static void oz_urb_cancel_tasklet(unsigned long unused)
1654 unsigned long irq_state;
1656 struct oz_hcd *ozhcd = oz_hcd_claim();
1659 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1660 while (!list_empty(&ozhcd->urb_cancel_list)) {
1661 struct oz_urb_link *urbl =
1662 list_first_entry(&ozhcd->urb_cancel_list,
1663 struct oz_urb_link, link);
1664 list_del_init(&urbl->link);
1665 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1668 oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1669 oz_free_urb_link(urbl);
1670 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1672 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1675 /*------------------------------------------------------------------------------
1678 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1681 struct oz_urb_link *urbl;
1682 while (!list_empty(&ozhcd->orphanage)) {
1683 urbl = list_first_entry(&ozhcd->orphanage,
1684 struct oz_urb_link, link);
1685 list_del(&urbl->link);
1686 oz_complete_urb(ozhcd->hcd, urbl->urb, status, 0);
1687 oz_free_urb_link(urbl);
1691 /*------------------------------------------------------------------------------
1694 static int oz_hcd_start(struct usb_hcd *hcd)
1696 hcd->power_budget = 200;
1697 hcd->state = HC_STATE_RUNNING;
1698 hcd->uses_new_polling = 1;
1701 /*------------------------------------------------------------------------------
1704 static void oz_hcd_stop(struct usb_hcd *hcd)
1707 /*------------------------------------------------------------------------------
1710 static void oz_hcd_shutdown(struct usb_hcd *hcd)
1713 /*------------------------------------------------------------------------------
1714 * Called to queue an urb for the device.
1715 * This function should return a non-zero error code if it fails the urb but
1716 * should not call usb_hcd_giveback_urb().
1719 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1722 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1725 struct oz_port *port;
1726 unsigned long irq_state;
1727 struct oz_urb_link *urbl;
1728 oz_dbg(URB, "%lu: %s: (%p)\n", jiffies, __func__, urb);
1729 if (unlikely(ozhcd == NULL)) {
1730 oz_dbg(URB, "%lu: Refused urb(%p) not ozhcd\n", jiffies, urb);
1733 if (unlikely(hcd->state != HC_STATE_RUNNING)) {
1734 oz_dbg(URB, "%lu: Refused urb(%p) not running\n",
1738 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1741 port = &ozhcd->ports[port_ix];
1744 if ((port->flags & OZ_PORT_F_PRESENT) == 0) {
1745 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1746 port_ix, urb->dev->devnum);
1750 /* Put request in queue for processing by tasklet.
1752 urbl = oz_alloc_urb_link();
1753 if (unlikely(urbl == NULL))
1756 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1757 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1759 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1760 oz_free_urb_link(urbl);
1763 list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1764 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1765 tasklet_schedule(&g_urb_process_tasklet);
1766 atomic_inc(&g_pending_urbs);
1769 /*------------------------------------------------------------------------------
1772 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1775 struct oz_urb_link *urbl = NULL;
1776 struct list_head *e;
1777 if (unlikely(ep == NULL))
1779 list_for_each(e, &ep->urb_list) {
1780 urbl = container_of(e, struct oz_urb_link, link);
1781 if (urbl->urb == urb) {
1783 if (usb_pipeisoc(urb->pipe)) {
1784 ep->credit -= urb->number_of_packets;
1793 /*------------------------------------------------------------------------------
1794 * Called to dequeue a previously submitted urb for the device.
1797 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1799 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1800 struct oz_urb_link *urbl = NULL;
1802 unsigned long irq_state;
1803 oz_dbg(URB, "%lu: %s: (%p)\n", jiffies, __func__, urb);
1804 urbl = oz_alloc_urb_link();
1805 if (unlikely(urbl == NULL))
1807 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1808 /* The following function checks the urb is still in the queue
1809 * maintained by the core and that the unlinked field is zero.
1810 * If both are true the function sets the unlinked field and returns
1811 * zero. Otherwise it returns an error.
1813 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1814 /* We have to check we haven't completed the urb or are about
1815 * to complete it. When we do we set hcpriv to 0 so if this has
1816 * already happened we don't put the urb in the cancel queue.
1818 if ((rc == 0) && urb->hcpriv) {
1820 urbl->port = (struct oz_port *)urb->hcpriv;
1821 urbl->ep_num = usb_pipeendpoint(urb->pipe);
1822 if (usb_pipein(urb->pipe))
1823 urbl->ep_num |= USB_DIR_IN;
1824 list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1825 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1826 tasklet_schedule(&g_urb_cancel_tasklet);
1828 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1829 oz_free_urb_link(urbl);
1833 /*------------------------------------------------------------------------------
1836 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1837 struct usb_host_endpoint *ep)
1840 /*------------------------------------------------------------------------------
1843 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1844 struct usb_host_endpoint *ep)
1847 /*------------------------------------------------------------------------------
1850 static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1852 oz_dbg(ON, "oz_hcd_get_frame_number\n");
1853 return oz_usb_get_frame_number();
1855 /*------------------------------------------------------------------------------
1857 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1858 * always do that in softirq context.
1860 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1862 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1867 spin_lock_bh(&ozhcd->hcd_lock);
1868 for (i = 0; i < OZ_NB_PORTS; i++) {
1869 if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
1870 oz_dbg(HUB, "Port %d changed\n", i);
1871 ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
1875 spin_unlock_bh(&ozhcd->hcd_lock);
1876 return buf[0] ? 1 : 0;
1878 /*------------------------------------------------------------------------------
1881 static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1882 struct usb_hub_descriptor *desc)
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;
1891 /*------------------------------------------------------------------------------
1894 static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1896 struct oz_port *port;
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;
1903 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
1905 port = &ozhcd->ports[port_id-1];
1907 case USB_PORT_FEAT_CONNECTION:
1908 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
1910 case USB_PORT_FEAT_ENABLE:
1911 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
1913 case USB_PORT_FEAT_SUSPEND:
1914 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
1916 case USB_PORT_FEAT_OVER_CURRENT:
1917 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
1919 case USB_PORT_FEAT_RESET:
1920 oz_dbg(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;
1925 case USB_PORT_FEAT_POWER:
1926 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
1927 set_bits |= USB_PORT_STAT_POWER;
1929 case USB_PORT_FEAT_LOWSPEED:
1930 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
1932 case USB_PORT_FEAT_C_CONNECTION:
1933 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
1935 case USB_PORT_FEAT_C_ENABLE:
1936 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
1938 case USB_PORT_FEAT_C_SUSPEND:
1939 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
1941 case USB_PORT_FEAT_C_OVER_CURRENT:
1942 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
1944 case USB_PORT_FEAT_C_RESET:
1945 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
1947 case USB_PORT_FEAT_TEST:
1948 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
1950 case USB_PORT_FEAT_INDICATOR:
1951 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
1954 oz_dbg(HUB, "Other %d\n", wvalue);
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);
1963 oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
1966 /*------------------------------------------------------------------------------
1969 static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1971 struct oz_port *port;
1973 u8 port_id = (u8)windex;
1974 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1975 unsigned clear_bits = 0;
1977 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
1979 port = &ozhcd->ports[port_id-1];
1981 case USB_PORT_FEAT_CONNECTION:
1982 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
1984 case USB_PORT_FEAT_ENABLE:
1985 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
1986 clear_bits = USB_PORT_STAT_ENABLE;
1988 case USB_PORT_FEAT_SUSPEND:
1989 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
1991 case USB_PORT_FEAT_OVER_CURRENT:
1992 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
1994 case USB_PORT_FEAT_RESET:
1995 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
1997 case USB_PORT_FEAT_POWER:
1998 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
1999 clear_bits |= USB_PORT_STAT_POWER;
2001 case USB_PORT_FEAT_LOWSPEED:
2002 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2004 case USB_PORT_FEAT_C_CONNECTION:
2005 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2006 clear_bits = (USB_PORT_STAT_C_CONNECTION << 16);
2008 case USB_PORT_FEAT_C_ENABLE:
2009 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2010 clear_bits = (USB_PORT_STAT_C_ENABLE << 16);
2012 case USB_PORT_FEAT_C_SUSPEND:
2013 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2015 case USB_PORT_FEAT_C_OVER_CURRENT:
2016 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2018 case USB_PORT_FEAT_C_RESET:
2019 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2020 clear_bits = (USB_PORT_FEAT_C_RESET << 16);
2022 case USB_PORT_FEAT_TEST:
2023 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2025 case USB_PORT_FEAT_INDICATOR:
2026 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2029 oz_dbg(HUB, "Other %d\n", wvalue);
2033 spin_lock_bh(&port->port_lock);
2034 port->status &= ~clear_bits;
2035 spin_unlock_bh(&port->port_lock);
2037 oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2038 port_id, ozhcd->ports[port_id-1].status);
2041 /*------------------------------------------------------------------------------
2044 static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2046 struct oz_hcd *ozhcd;
2048 if ((windex < 1) || (windex > OZ_NB_PORTS))
2050 ozhcd = oz_hcd_private(hcd);
2051 oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
2052 status = ozhcd->ports[windex-1].status;
2053 put_unaligned(cpu_to_le32(status), (__le32 *)buf);
2054 oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
2057 /*------------------------------------------------------------------------------
2060 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2061 u16 windex, char *buf, u16 wlength)
2066 case ClearHubFeature:
2067 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
2069 case ClearPortFeature:
2070 err = oz_clear_port_feature(hcd, wvalue, windex);
2072 case GetHubDescriptor:
2073 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2076 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
2077 put_unaligned(__constant_cpu_to_le32(0), (__le32 *)buf);
2080 err = oz_get_port_status(hcd, windex, buf);
2083 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
2085 case SetPortFeature:
2086 err = oz_set_port_feature(hcd, wvalue, windex);
2089 oz_dbg(HUB, "Other: %d\n", req_type);
2094 /*------------------------------------------------------------------------------
2097 static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2099 struct oz_hcd *ozhcd;
2101 ozhcd = oz_hcd_private(hcd);
2102 spin_lock_bh(&ozhcd->hcd_lock);
2103 hcd->state = HC_STATE_SUSPENDED;
2104 ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2105 spin_unlock_bh(&ozhcd->hcd_lock);
2108 /*------------------------------------------------------------------------------
2111 static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2113 struct oz_hcd *ozhcd;
2115 ozhcd = oz_hcd_private(hcd);
2116 spin_lock_bh(&ozhcd->hcd_lock);
2117 ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2118 hcd->state = HC_STATE_RUNNING;
2119 spin_unlock_bh(&ozhcd->hcd_lock);
2122 /*------------------------------------------------------------------------------
2124 static void oz_plat_shutdown(struct platform_device *dev)
2128 /*------------------------------------------------------------------------------
2131 static int oz_plat_probe(struct platform_device *dev)
2135 struct usb_hcd *hcd;
2136 struct oz_hcd *ozhcd;
2138 hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
2140 oz_dbg(ON, "Failed to created hcd object OK\n");
2143 ozhcd = oz_hcd_private(hcd);
2144 memset(ozhcd, 0, sizeof(*ozhcd));
2145 INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2146 INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2147 INIT_LIST_HEAD(&ozhcd->orphanage);
2149 ozhcd->conn_port = -1;
2150 spin_lock_init(&ozhcd->hcd_lock);
2151 for (i = 0; i < OZ_NB_PORTS; i++) {
2152 struct oz_port *port = &ozhcd->ports[i];
2153 port->ozhcd = ozhcd;
2156 port->bus_addr = 0xff;
2157 spin_lock_init(&port->port_lock);
2159 err = usb_add_hcd(hcd, 0, 0);
2161 oz_dbg(ON, "Failed to add hcd object OK\n");
2165 spin_lock_bh(&g_hcdlock);
2167 spin_unlock_bh(&g_hcdlock);
2170 /*------------------------------------------------------------------------------
2173 static int oz_plat_remove(struct platform_device *dev)
2175 struct usb_hcd *hcd = platform_get_drvdata(dev);
2176 struct oz_hcd *ozhcd;
2180 ozhcd = oz_hcd_private(hcd);
2181 spin_lock_bh(&g_hcdlock);
2182 if (ozhcd == g_ozhcd)
2184 spin_unlock_bh(&g_hcdlock);
2185 oz_dbg(ON, "Clearing orphanage\n");
2186 oz_hcd_clear_orphanage(ozhcd, -EPIPE);
2187 oz_dbg(ON, "Removing hcd\n");
2188 usb_remove_hcd(hcd);
2190 oz_empty_link_pool();
2194 /*------------------------------------------------------------------------------
2197 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2202 /*------------------------------------------------------------------------------
2205 static int oz_plat_resume(struct platform_device *dev)
2210 /*------------------------------------------------------------------------------
2213 int oz_hcd_init(void)
2218 tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
2219 tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
2220 err = platform_driver_register(&g_oz_plat_drv);
2221 oz_dbg(ON, "platform_driver_register() returned %d\n", err);
2224 g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
2225 if (g_plat_dev == NULL) {
2229 oz_dbg(ON, "platform_device_alloc() succeeded\n");
2230 err = platform_device_add(g_plat_dev);
2233 oz_dbg(ON, "platform_device_add() succeeded\n");
2236 platform_device_put(g_plat_dev);
2238 platform_driver_unregister(&g_oz_plat_drv);
2240 tasklet_disable(&g_urb_process_tasklet);
2241 tasklet_disable(&g_urb_cancel_tasklet);
2242 oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
2245 /*------------------------------------------------------------------------------
2248 void oz_hcd_term(void)
2250 tasklet_kill(&g_urb_process_tasklet);
2251 tasklet_kill(&g_urb_cancel_tasklet);
2252 platform_device_unregister(g_plat_dev);
2253 platform_driver_unregister(&g_oz_plat_drv);
2254 oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));