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"
38 /*------------------------------------------------------------------------------
39 * Number of units of buffering to capture for an isochronous IN endpoint before
40 * allowing data to be indicated up.
42 #define OZ_IN_BUFFERING_UNITS 50
44 /* Name of our platform device.
46 #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
52 /* Get endpoint object from the containing link.
54 #define ep_from_link(__e) container_of((__e), struct oz_endpoint, link)
56 /*EP0 timeout before ep0 request is again added to TX queue. (13*8 = 98mSec)
58 #define EP0_TIMEOUT_COUNTER 13
60 /*------------------------------------------------------------------------------
61 * Used to link urbs together and also store some status information for each
63 * A cache of these are kept in a pool to reduce number of calls to kmalloc.
66 struct list_head link;
71 unsigned submit_counter;
74 /* Holds state information about a USB endpoint.
77 struct list_head urb_list; /* List of oz_urb_link items. */
78 struct list_head link; /* For isoc ep, links in to isoc
80 struct timespec timestamp;
94 /* Bits in the flags field. */
95 #define OZ_F_EP_BUFFERING 0x1
96 #define OZ_F_EP_HAVE_STREAM 0x2
98 /* Holds state information about a USB interface.
100 struct oz_interface {
105 /* Holds state information about an hcd port.
107 #define OZ_NB_ENDPOINTS 16
112 struct oz_hcd *ozhcd;
113 spinlock_t port_lock;
118 struct oz_interface *iface;
119 struct oz_endpoint *out_ep[OZ_NB_ENDPOINTS];
120 struct oz_endpoint *in_ep[OZ_NB_ENDPOINTS];
121 struct list_head isoc_out_ep;
122 struct list_head isoc_in_ep;
125 #define OZ_PORT_F_PRESENT 0x1
126 #define OZ_PORT_F_CHANGED 0x2
127 #define OZ_PORT_F_DYING 0x4
129 /* Data structure in the private context area of struct usb_hcd.
131 #define OZ_NB_PORTS 8
134 struct list_head urb_pending_list;
135 struct list_head urb_cancel_list;
136 struct list_head orphanage;
137 int conn_port; /* Port that is currently connecting, -1 if none.*/
138 struct oz_port ports[OZ_NB_PORTS];
143 /* Bits in flags field.
145 #define OZ_HDC_F_SUSPENDED 0x1
147 /*------------------------------------------------------------------------------
148 * Static function prototypes.
150 static int oz_hcd_start(struct usb_hcd *hcd);
151 static void oz_hcd_stop(struct usb_hcd *hcd);
152 static void oz_hcd_shutdown(struct usb_hcd *hcd);
153 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
155 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
156 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
157 struct usb_host_endpoint *ep);
158 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
159 struct usb_host_endpoint *ep);
160 static int oz_hcd_get_frame_number(struct usb_hcd *hcd);
161 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf);
162 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
163 u16 windex, char *buf, u16 wlength);
164 static int oz_hcd_bus_suspend(struct usb_hcd *hcd);
165 static int oz_hcd_bus_resume(struct usb_hcd *hcd);
166 static int oz_plat_probe(struct platform_device *dev);
167 static int oz_plat_remove(struct platform_device *dev);
168 static void oz_plat_shutdown(struct platform_device *dev);
169 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg);
170 static int oz_plat_resume(struct platform_device *dev);
171 static void oz_urb_process_tasklet(unsigned long unused);
172 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
173 struct oz_port *port, struct usb_host_config *config,
175 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
176 struct oz_port *port);
177 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
178 struct oz_port *port,
179 struct usb_host_interface *intf, gfp_t mem_flags);
180 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
181 struct oz_port *port, int if_ix);
182 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
184 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
186 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status);
188 /*------------------------------------------------------------------------------
189 * Static external variables.
191 static struct platform_device *g_plat_dev;
192 static struct oz_hcd *g_ozhcd;
193 static DEFINE_SPINLOCK(g_hcdlock); /* Guards g_ozhcd. */
194 static const char g_hcd_name[] = "Ozmo WPAN";
195 static struct list_head *g_link_pool;
196 static int g_link_pool_size;
197 static DEFINE_SPINLOCK(g_link_lock);
198 static DEFINE_SPINLOCK(g_tasklet_lock);
199 static struct tasklet_struct g_urb_process_tasklet;
200 static struct tasklet_struct g_urb_cancel_tasklet;
201 static atomic_t g_pending_urbs = ATOMIC_INIT(0);
202 static atomic_t g_usb_frame_number = ATOMIC_INIT(0);
203 static const struct hc_driver g_oz_hc_drv = {
204 .description = g_hcd_name,
205 .product_desc = "Ozmo Devices WPAN",
206 .hcd_priv_size = sizeof(struct oz_hcd),
208 .start = oz_hcd_start,
210 .shutdown = oz_hcd_shutdown,
211 .urb_enqueue = oz_hcd_urb_enqueue,
212 .urb_dequeue = oz_hcd_urb_dequeue,
213 .endpoint_disable = oz_hcd_endpoint_disable,
214 .endpoint_reset = oz_hcd_endpoint_reset,
215 .get_frame_number = oz_hcd_get_frame_number,
216 .hub_status_data = oz_hcd_hub_status_data,
217 .hub_control = oz_hcd_hub_control,
218 .bus_suspend = oz_hcd_bus_suspend,
219 .bus_resume = oz_hcd_bus_resume,
222 static struct platform_driver g_oz_plat_drv = {
223 .probe = oz_plat_probe,
224 .remove = oz_plat_remove,
225 .shutdown = oz_plat_shutdown,
226 .suspend = oz_plat_suspend,
227 .resume = oz_plat_resume,
229 .name = OZ_PLAT_DEV_NAME,
230 .owner = THIS_MODULE,
234 /*------------------------------------------------------------------------------
235 * Gets our private context area (which is of type struct oz_hcd) from the
239 static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
241 return (struct oz_hcd *)hcd->hcd_priv;
244 /*------------------------------------------------------------------------------
245 * Searches list of ports to find the index of the one with a specified USB
246 * bus address. If none of the ports has the bus address then the connection
247 * port is returned, if there is one or -1 otherwise.
250 static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
254 for (i = 0; i < OZ_NB_PORTS; i++) {
255 if (ozhcd->ports[i].bus_addr == bus_addr)
258 return ozhcd->conn_port;
261 /*------------------------------------------------------------------------------
262 * Allocates an urb link, first trying the pool but going to heap if empty.
265 static struct oz_urb_link *oz_alloc_urb_link(void)
267 struct oz_urb_link *urbl = NULL;
268 unsigned long irq_state;
270 spin_lock_irqsave(&g_link_lock, irq_state);
272 urbl = container_of(g_link_pool, struct oz_urb_link, link);
273 g_link_pool = urbl->link.next;
276 spin_unlock_irqrestore(&g_link_lock, irq_state);
278 urbl = kmalloc(sizeof(struct oz_urb_link), GFP_ATOMIC);
282 /*------------------------------------------------------------------------------
283 * Frees an urb link by putting it in the pool if there is enough space or
284 * deallocating it to heap otherwise.
287 static void oz_free_urb_link(struct oz_urb_link *urbl)
290 unsigned long irq_state;
291 spin_lock_irqsave(&g_link_lock, irq_state);
292 if (g_link_pool_size < OZ_MAX_LINK_POOL_SIZE) {
293 urbl->link.next = g_link_pool;
294 g_link_pool = &urbl->link;
298 spin_unlock_irqrestore(&g_link_lock, irq_state);
303 /*------------------------------------------------------------------------------
304 * Deallocates all the urb links in the pool.
307 static void oz_empty_link_pool(void)
310 unsigned long irq_state;
312 spin_lock_irqsave(&g_link_lock, irq_state);
315 g_link_pool_size = 0;
316 spin_unlock_irqrestore(&g_link_lock, irq_state);
318 struct oz_urb_link *urbl =
319 container_of(e, struct oz_urb_link, link);
325 /*------------------------------------------------------------------------------
326 * Allocates endpoint structure and optionally a buffer. If a buffer is
327 * allocated it immediately follows the endpoint structure.
330 static struct oz_endpoint *oz_ep_alloc(gfp_t mem_flags, int buffer_size)
332 struct oz_endpoint *ep =
333 kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
335 INIT_LIST_HEAD(&ep->urb_list);
336 INIT_LIST_HEAD(&ep->link);
339 ep->buffer_size = buffer_size;
340 ep->buffer = (u8 *)(ep+1);
346 /*------------------------------------------------------------------------------
347 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
349 * Context: softirq or process
351 static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd, struct urb *urb)
353 struct oz_urb_link *urbl;
356 list_for_each(e, &ozhcd->urb_cancel_list) {
357 urbl = container_of(e, struct oz_urb_link, link);
358 if (urb == urbl->urb) {
366 /*------------------------------------------------------------------------------
367 * This is called when we have finished processing an urb. It unlinks it from
368 * the ep and returns it to the core.
369 * Context: softirq or process
371 static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
374 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
375 unsigned long irq_state;
376 struct oz_urb_link *cancel_urbl = NULL;
378 spin_lock_irqsave(&g_tasklet_lock, irq_state);
379 usb_hcd_unlink_urb_from_ep(hcd, urb);
380 /* Clear hcpriv which will prevent it being put in the cancel list
381 * in the event that an attempt is made to cancel it.
384 /* Walk the cancel list in case the urb is already sitting there.
385 * Since we process the cancel list in a tasklet rather than in
386 * the dequeue function this could happen.
388 cancel_urbl = oz_uncancel_urb(ozhcd, urb);
389 /* Note: we release lock but do not enable local irqs.
390 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
391 * or at least other host controllers disable interrupts at this point
392 * so we do the same. We must, however, release the lock otherwise a
393 * deadlock will occur if an urb is submitted to our driver in the urb
394 * completion function. Because we disable interrupts it is possible
395 * that the urb_enqueue function can be called with them disabled.
397 spin_unlock(&g_tasklet_lock);
398 if (oz_forget_urb(urb)) {
399 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
401 atomic_dec(&g_pending_urbs);
402 usb_hcd_giveback_urb(hcd, urb, status);
404 spin_lock(&g_tasklet_lock);
405 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
407 oz_free_urb_link(cancel_urbl);
410 /*------------------------------------------------------------------------------
411 * Deallocates an endpoint including deallocating any associated stream and
412 * returning any queued urbs to the core.
415 static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
418 struct list_head list;
419 struct oz_hcd *ozhcd = port->ozhcd;
420 INIT_LIST_HEAD(&list);
421 if (ep->flags & OZ_F_EP_HAVE_STREAM)
422 oz_usb_stream_delete(port->hpd, ep->ep_num);
423 /* Transfer URBs to the orphanage while we hold the lock. */
424 spin_lock_bh(&ozhcd->hcd_lock);
425 /* Note: this works even if ep->urb_list is empty.*/
426 list_replace_init(&ep->urb_list, &list);
427 /* Put the URBs in the orphanage. */
428 list_splice_tail(&list, &ozhcd->orphanage);
429 spin_unlock_bh(&ozhcd->hcd_lock);
431 oz_dbg(ON, "Freeing endpoint memory\n");
435 /*------------------------------------------------------------------------------
438 static void oz_complete_buffered_urb(struct oz_port *port,
439 struct oz_endpoint *ep,
442 u8 data_len, available_space, copy_len;
444 memcpy(&data_len, &ep->buffer[ep->out_ix], sizeof(u8));
445 if (data_len <= urb->transfer_buffer_length)
446 available_space = data_len;
448 available_space = urb->transfer_buffer_length;
450 if (++ep->out_ix == ep->buffer_size)
452 copy_len = ep->buffer_size - ep->out_ix;
453 if (copy_len >= available_space)
454 copy_len = available_space;
455 memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
457 if (copy_len < available_space) {
458 memcpy((urb->transfer_buffer + copy_len), ep->buffer,
459 (available_space - copy_len));
460 ep->out_ix = available_space - copy_len;
462 ep->out_ix += copy_len;
464 urb->actual_length = available_space;
465 if (ep->out_ix == ep->buffer_size)
468 ep->buffered_units--;
469 oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
471 oz_complete_urb(port->ozhcd->hcd, urb, 0);
474 /*------------------------------------------------------------------------------
477 static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
478 struct urb *urb, u8 req_id)
480 struct oz_urb_link *urbl;
481 struct oz_endpoint *ep = NULL;
484 if (ep_addr >= OZ_NB_ENDPOINTS) {
485 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
488 urbl = oz_alloc_urb_link();
491 urbl->submit_counter = 0;
493 urbl->req_id = req_id;
494 urbl->ep_num = ep_addr;
495 /* Hold lock while we insert the URB into the list within the
496 * endpoint structure.
498 spin_lock_bh(&port->ozhcd->hcd_lock);
499 /* If the urb has been unlinked while out of any list then
503 spin_unlock_bh(&port->ozhcd->hcd_lock);
504 oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
505 oz_complete_urb(port->ozhcd->hcd, urb, 0);
506 oz_free_urb_link(urbl);
511 ep = port->in_ep[ep_addr];
513 ep = port->out_ep[ep_addr];
519 /*For interrupt endpoint check for buffered data
522 if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
523 && ep->buffered_units > 0) {
524 oz_free_urb_link(urbl);
525 spin_unlock_bh(&port->ozhcd->hcd_lock);
526 oz_complete_buffered_urb(port, ep, urb);
530 if (ep && port->hpd) {
531 list_add_tail(&urbl->link, &ep->urb_list);
532 if (!in_dir && ep_addr && (ep->credit < 0)) {
533 getrawmonotonic(&ep->timestamp);
540 spin_unlock_bh(&port->ozhcd->hcd_lock);
542 oz_free_urb_link(urbl);
546 /*------------------------------------------------------------------------------
547 * Removes an urb from the queue in the endpoint.
548 * Returns 0 if it is found and -EIDRM otherwise.
551 static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
554 struct oz_urb_link *urbl = NULL;
555 struct oz_endpoint *ep;
557 spin_lock_bh(&port->ozhcd->hcd_lock);
559 ep = port->in_ep[ep_addr];
561 ep = port->out_ep[ep_addr];
564 list_for_each(e, &ep->urb_list) {
565 urbl = container_of(e, struct oz_urb_link, link);
566 if (urbl->urb == urb) {
573 spin_unlock_bh(&port->ozhcd->hcd_lock);
575 oz_free_urb_link(urbl);
576 return urbl ? 0 : -EIDRM;
579 /*------------------------------------------------------------------------------
580 * Finds an urb given its request id.
583 static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
586 struct oz_hcd *ozhcd = port->ozhcd;
587 struct urb *urb = NULL;
588 struct oz_urb_link *urbl = NULL;
589 struct oz_endpoint *ep;
591 spin_lock_bh(&ozhcd->hcd_lock);
592 ep = port->out_ep[ep_ix];
595 list_for_each(e, &ep->urb_list) {
596 urbl = container_of(e, struct oz_urb_link, link);
597 if (urbl->req_id == req_id) {
604 spin_unlock_bh(&ozhcd->hcd_lock);
605 /* If urb is non-zero then we we must have an urb link to delete.
608 oz_free_urb_link(urbl);
612 /*------------------------------------------------------------------------------
613 * Pre-condition: Port lock must be held.
616 static void oz_acquire_port(struct oz_port *port, void *hpd)
618 INIT_LIST_HEAD(&port->isoc_out_ep);
619 INIT_LIST_HEAD(&port->isoc_in_ep);
620 port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
621 port->status |= USB_PORT_STAT_CONNECTION |
622 (USB_PORT_STAT_C_CONNECTION << 16);
627 /*------------------------------------------------------------------------------
630 static struct oz_hcd *oz_hcd_claim(void)
632 struct oz_hcd *ozhcd;
634 spin_lock_bh(&g_hcdlock);
637 usb_get_hcd(ozhcd->hcd);
638 spin_unlock_bh(&g_hcdlock);
642 /*------------------------------------------------------------------------------
645 static inline void oz_hcd_put(struct oz_hcd *ozhcd)
648 usb_put_hcd(ozhcd->hcd);
651 /*------------------------------------------------------------------------------
652 * This is called by the protocol handler to notify that a PD has arrived.
653 * We allocate a port to associate with the PD and create a structure for
654 * endpoint 0. This port is made the connection port.
655 * In the event that one of the other port is already a connection port then
657 * TODO We should be able to do better than fail and should be able remember
658 * that this port needs configuring and make it the connection port once the
659 * current connection port has been assigned an address. Collisions here are
660 * probably very rare indeed.
663 void *oz_hcd_pd_arrived(void *hpd)
667 struct oz_hcd *ozhcd = NULL;
668 struct oz_endpoint *ep;
670 ozhcd = oz_hcd_claim();
673 /* Allocate an endpoint object in advance (before holding hcd lock) to
674 * use for out endpoint 0.
676 ep = oz_ep_alloc(GFP_ATOMIC, 0);
677 spin_lock_bh(&ozhcd->hcd_lock);
678 if (ozhcd->conn_port >= 0) {
679 spin_unlock_bh(&ozhcd->hcd_lock);
680 oz_dbg(ON, "conn_port >= 0\n");
683 for (i = 0; i < OZ_NB_PORTS; i++) {
684 struct oz_port *port = &ozhcd->ports[i];
685 spin_lock(&port->port_lock);
686 if ((port->flags & OZ_PORT_F_PRESENT) == 0) {
687 oz_acquire_port(port, hpd);
688 spin_unlock(&port->port_lock);
691 spin_unlock(&port->port_lock);
693 if (i < OZ_NB_PORTS) {
694 oz_dbg(ON, "Setting conn_port = %d\n", i);
695 ozhcd->conn_port = i;
696 /* Attach out endpoint 0.
698 ozhcd->ports[i].out_ep[0] = ep;
700 hport = &ozhcd->ports[i];
701 spin_unlock_bh(&ozhcd->hcd_lock);
702 if (ozhcd->flags & OZ_HDC_F_SUSPENDED) {
703 oz_dbg(ON, "Resuming root hub\n");
704 usb_hcd_resume_root_hub(ozhcd->hcd);
706 usb_hcd_poll_rh_status(ozhcd->hcd);
708 spin_unlock_bh(&ozhcd->hcd_lock);
711 if (ep) /* ep is non-null if not used. */
712 oz_ep_free(NULL, ep);
717 /*------------------------------------------------------------------------------
718 * This is called by the protocol handler to notify that the PD has gone away.
719 * We need to deallocate all resources and then request that the root hub is
720 * polled. We release the reference we hold on the PD.
723 void oz_hcd_pd_departed(void *hport)
725 struct oz_port *port = (struct oz_port *)hport;
726 struct oz_hcd *ozhcd;
728 struct oz_endpoint *ep = NULL;
731 oz_dbg(ON, "%s: port = 0\n", __func__);
737 /* Check if this is the connection port - if so clear it.
739 spin_lock_bh(&ozhcd->hcd_lock);
740 if ((ozhcd->conn_port >= 0) &&
741 (port == &ozhcd->ports[ozhcd->conn_port])) {
742 oz_dbg(ON, "Clearing conn_port\n");
743 ozhcd->conn_port = -1;
745 spin_lock(&port->port_lock);
746 port->flags |= OZ_PORT_F_DYING;
747 spin_unlock(&port->port_lock);
748 spin_unlock_bh(&ozhcd->hcd_lock);
750 oz_clean_endpoints_for_config(ozhcd->hcd, port);
751 spin_lock_bh(&port->port_lock);
754 port->bus_addr = 0xff;
755 port->config_num = 0;
756 port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
757 port->flags |= OZ_PORT_F_CHANGED;
758 port->status &= ~USB_PORT_STAT_CONNECTION;
759 port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
760 /* If there is an endpont 0 then clear the pointer while we hold
761 * the spinlock be we deallocate it after releasing the lock.
763 if (port->out_ep[0]) {
764 ep = port->out_ep[0];
765 port->out_ep[0] = NULL;
767 spin_unlock_bh(&port->port_lock);
769 oz_ep_free(port, ep);
770 usb_hcd_poll_rh_status(ozhcd->hcd);
774 /*------------------------------------------------------------------------------
777 void oz_hcd_pd_reset(void *hpd, void *hport)
779 /* Cleanup the current configuration and report reset to the core.
781 struct oz_port *port = (struct oz_port *)hport;
782 struct oz_hcd *ozhcd = port->ozhcd;
784 oz_dbg(ON, "PD Reset\n");
785 spin_lock_bh(&port->port_lock);
786 port->flags |= OZ_PORT_F_CHANGED;
787 port->status |= USB_PORT_STAT_RESET;
788 port->status |= (USB_PORT_STAT_C_RESET << 16);
789 spin_unlock_bh(&port->port_lock);
790 oz_clean_endpoints_for_config(ozhcd->hcd, port);
791 usb_hcd_poll_rh_status(ozhcd->hcd);
794 /*------------------------------------------------------------------------------
797 void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
798 int length, int offset, int total_size)
800 struct oz_port *port = (struct oz_port *)hport;
804 oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
805 length, offset, total_size);
806 urb = oz_find_urb_by_id(port, 0, req_id);
811 int required_size = urb->transfer_buffer_length;
812 if (required_size > total_size)
813 required_size = total_size;
814 copy_len = required_size-offset;
815 if (length <= copy_len)
817 memcpy(urb->transfer_buffer+offset, desc, copy_len);
819 if (offset < required_size) {
820 struct usb_ctrlrequest *setup =
821 (struct usb_ctrlrequest *)urb->setup_packet;
822 unsigned wvalue = le16_to_cpu(setup->wValue);
823 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
825 else if (oz_usb_get_desc_req(port->hpd, req_id,
826 setup->bRequestType, (u8)(wvalue>>8),
827 (u8)wvalue, setup->wIndex, offset,
828 required_size-offset)) {
829 oz_dequeue_ep_urb(port, 0, 0, urb);
836 urb->actual_length = total_size;
837 oz_complete_urb(port->ozhcd->hcd, urb, 0);
840 /*------------------------------------------------------------------------------
843 static void oz_display_conf_type(u8 t)
846 case USB_REQ_GET_STATUS:
847 oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
849 case USB_REQ_CLEAR_FEATURE:
850 oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
852 case USB_REQ_SET_FEATURE:
853 oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
855 case USB_REQ_SET_ADDRESS:
856 oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
858 case USB_REQ_GET_DESCRIPTOR:
859 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
861 case USB_REQ_SET_DESCRIPTOR:
862 oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
864 case USB_REQ_GET_CONFIGURATION:
865 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
867 case USB_REQ_SET_CONFIGURATION:
868 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
870 case USB_REQ_GET_INTERFACE:
871 oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
873 case USB_REQ_SET_INTERFACE:
874 oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
876 case USB_REQ_SYNCH_FRAME:
877 oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
882 /*------------------------------------------------------------------------------
885 static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
886 u8 rcode, u8 config_num)
889 struct usb_hcd *hcd = port->ozhcd->hcd;
892 port->config_num = config_num;
893 oz_clean_endpoints_for_config(hcd, port);
894 if (oz_build_endpoints_for_config(hcd, port,
895 &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
901 oz_complete_urb(hcd, urb, rc);
904 /*------------------------------------------------------------------------------
907 static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
908 u8 rcode, u8 if_num, u8 alt)
910 struct usb_hcd *hcd = port->ozhcd->hcd;
914 struct usb_host_config *config;
915 struct usb_host_interface *intf;
916 oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
917 oz_clean_endpoints_for_interface(hcd, port, if_num);
918 config = &urb->dev->config[port->config_num-1];
919 intf = &config->intf_cache[if_num]->altsetting[alt];
920 if (oz_build_endpoints_for_interface(hcd, port, intf,
924 port->iface[if_num].alt = alt;
928 oz_complete_urb(hcd, urb, rc);
931 /*------------------------------------------------------------------------------
934 void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
937 struct oz_port *port = (struct oz_port *)hport;
939 struct usb_ctrlrequest *setup;
940 struct usb_hcd *hcd = port->ozhcd->hcd;
944 oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
945 urb = oz_find_urb_by_id(port, 0, req_id);
947 oz_dbg(ON, "URB not found\n");
950 setup = (struct usb_ctrlrequest *)urb->setup_packet;
951 windex = le16_to_cpu(setup->wIndex);
952 wvalue = le16_to_cpu(setup->wValue);
953 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
954 /* Standard requests */
955 oz_display_conf_type(setup->bRequest);
956 switch (setup->bRequest) {
957 case USB_REQ_SET_CONFIGURATION:
958 oz_hcd_complete_set_config(port, urb, rcode,
961 case USB_REQ_SET_INTERFACE:
962 oz_hcd_complete_set_interface(port, urb, rcode,
963 (u8)windex, (u8)wvalue);
966 oz_complete_urb(hcd, urb, 0);
971 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
973 if (data_len <= urb->transfer_buffer_length)
976 copy_len = urb->transfer_buffer_length;
977 memcpy(urb->transfer_buffer, data, copy_len);
978 urb->actual_length = copy_len;
980 oz_complete_urb(hcd, urb, 0);
984 /*------------------------------------------------------------------------------
985 * Context: softirq-serialized
987 static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
995 space = ep->out_ix-ep->in_ix-1;
997 space += ep->buffer_size;
998 if (space < (data_len+1)) {
999 oz_dbg(ON, "Buffer full\n");
1002 ep->buffer[ep->in_ix] = (u8)data_len;
1003 if (++ep->in_ix == ep->buffer_size)
1005 copy_len = ep->buffer_size - ep->in_ix;
1006 if (copy_len > data_len)
1007 copy_len = data_len;
1008 memcpy(&ep->buffer[ep->in_ix], data, copy_len);
1010 if (copy_len < data_len) {
1011 memcpy(ep->buffer, data+copy_len, data_len-copy_len);
1012 ep->in_ix = data_len-copy_len;
1014 ep->in_ix += copy_len;
1016 if (ep->in_ix == ep->buffer_size)
1018 ep->buffered_units++;
1022 /*------------------------------------------------------------------------------
1023 * Context: softirq-serialized
1025 void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
1027 struct oz_port *port = (struct oz_port *)hport;
1028 struct oz_endpoint *ep;
1029 struct oz_hcd *ozhcd = port->ozhcd;
1031 spin_lock_bh(&ozhcd->hcd_lock);
1032 ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
1035 switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
1036 case USB_ENDPOINT_XFER_INT:
1037 case USB_ENDPOINT_XFER_BULK:
1038 if (!list_empty(&ep->urb_list)) {
1039 struct oz_urb_link *urbl =
1040 list_first_entry(&ep->urb_list,
1041 struct oz_urb_link, link);
1044 list_del_init(&urbl->link);
1045 spin_unlock_bh(&ozhcd->hcd_lock);
1047 oz_free_urb_link(urbl);
1048 if (data_len <= urb->transfer_buffer_length)
1049 copy_len = data_len;
1051 copy_len = urb->transfer_buffer_length;
1052 memcpy(urb->transfer_buffer, data, copy_len);
1053 urb->actual_length = copy_len;
1054 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1057 oz_dbg(ON, "buffering frame as URB is not available\n");
1058 oz_hcd_buffer_data(ep, data, data_len);
1061 case USB_ENDPOINT_XFER_ISOC:
1062 oz_hcd_buffer_data(ep, data, data_len);
1066 spin_unlock_bh(&ozhcd->hcd_lock);
1069 /*------------------------------------------------------------------------------
1072 static inline int oz_usb_get_frame_number(void)
1074 return atomic_inc_return(&g_usb_frame_number);
1077 /*------------------------------------------------------------------------------
1080 int oz_hcd_heartbeat(void *hport)
1083 struct oz_port *port = (struct oz_port *)hport;
1084 struct oz_hcd *ozhcd = port->ozhcd;
1085 struct oz_urb_link *urbl;
1086 struct list_head xfr_list;
1087 struct list_head *e;
1088 struct list_head *n;
1090 struct oz_endpoint *ep;
1091 struct timespec ts, delta;
1093 getrawmonotonic(&ts);
1094 INIT_LIST_HEAD(&xfr_list);
1095 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1097 spin_lock_bh(&ozhcd->hcd_lock);
1098 list_for_each(e, &port->isoc_out_ep) {
1099 ep = ep_from_link(e);
1102 delta = timespec_sub(ts, ep->timestamp);
1103 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1104 if (ep->credit > ep->credit_ceiling)
1105 ep->credit = ep->credit_ceiling;
1107 while (ep->credit && !list_empty(&ep->urb_list)) {
1108 urbl = list_first_entry(&ep->urb_list,
1109 struct oz_urb_link, link);
1111 if ((ep->credit + 1) < urb->number_of_packets)
1113 ep->credit -= urb->number_of_packets;
1116 list_move_tail(&urbl->link, &xfr_list);
1119 spin_unlock_bh(&ozhcd->hcd_lock);
1120 /* Send to PD and complete URBs.
1122 list_for_each_safe(e, n, &xfr_list) {
1123 urbl = container_of(e, struct oz_urb_link, link);
1126 urb->error_count = 0;
1127 urb->start_frame = oz_usb_get_frame_number();
1128 oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
1129 oz_free_urb_link(urbl);
1130 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1132 /* Check the IN isoc endpoints to see if any URBs can be completed.
1134 spin_lock_bh(&ozhcd->hcd_lock);
1135 list_for_each(e, &port->isoc_in_ep) {
1136 struct oz_endpoint *ep = ep_from_link(e);
1137 if (ep->flags & OZ_F_EP_BUFFERING) {
1138 if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
1139 ep->flags &= ~OZ_F_EP_BUFFERING;
1142 ep->start_frame = 0;
1146 delta = timespec_sub(ts, ep->timestamp);
1147 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1149 while (!list_empty(&ep->urb_list)) {
1150 struct oz_urb_link *urbl =
1151 list_first_entry(&ep->urb_list,
1152 struct oz_urb_link, link);
1153 struct urb *urb = urbl->urb;
1157 if (ep->credit < urb->number_of_packets)
1159 if (ep->buffered_units < urb->number_of_packets)
1161 urb->actual_length = 0;
1162 for (i = 0; i < urb->number_of_packets; i++) {
1163 len = ep->buffer[ep->out_ix];
1164 if (++ep->out_ix == ep->buffer_size)
1166 copy_len = ep->buffer_size - ep->out_ix;
1169 memcpy(urb->transfer_buffer,
1170 &ep->buffer[ep->out_ix], copy_len);
1171 if (copy_len < len) {
1172 memcpy(urb->transfer_buffer+copy_len,
1173 ep->buffer, len-copy_len);
1174 ep->out_ix = len-copy_len;
1176 ep->out_ix += copy_len;
1177 if (ep->out_ix == ep->buffer_size)
1179 urb->iso_frame_desc[i].offset =
1181 urb->actual_length += len;
1182 urb->iso_frame_desc[i].actual_length = len;
1183 urb->iso_frame_desc[i].status = 0;
1185 ep->buffered_units -= urb->number_of_packets;
1186 urb->error_count = 0;
1187 urb->start_frame = ep->start_frame;
1188 ep->start_frame += urb->number_of_packets;
1189 list_move_tail(&urbl->link, &xfr_list);
1190 ep->credit -= urb->number_of_packets;
1193 if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1195 spin_unlock_bh(&ozhcd->hcd_lock);
1196 /* Complete the filled URBs.
1198 list_for_each_safe(e, n, &xfr_list) {
1199 urbl = container_of(e, struct oz_urb_link, link);
1202 oz_free_urb_link(urbl);
1203 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1205 /* Check if there are any ep0 requests that have timed out.
1206 * If so resent to PD.
1208 ep = port->out_ep[0];
1210 struct list_head *e;
1211 struct list_head *n;
1212 spin_lock_bh(&ozhcd->hcd_lock);
1213 list_for_each_safe(e, n, &ep->urb_list) {
1214 urbl = container_of(e, struct oz_urb_link, link);
1215 if (urbl->submit_counter > EP0_TIMEOUT_COUNTER) {
1216 oz_dbg(ON, "Request 0x%p timeout\n", urbl->urb);
1217 list_move_tail(e, &xfr_list);
1218 urbl->submit_counter = 0;
1220 urbl->submit_counter++;
1223 if (!list_empty(&ep->urb_list))
1225 spin_unlock_bh(&ozhcd->hcd_lock);
1227 while (e != &xfr_list) {
1228 urbl = container_of(e, struct oz_urb_link, link);
1230 oz_dbg(ON, "Resending request to PD\n");
1231 oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1232 oz_free_urb_link(urbl);
1238 /*------------------------------------------------------------------------------
1241 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1242 struct oz_port *port,
1243 struct usb_host_interface *intf, gfp_t mem_flags)
1245 struct oz_hcd *ozhcd = port->ozhcd;
1247 int if_ix = intf->desc.bInterfaceNumber;
1248 int request_heartbeat = 0;
1250 oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
1251 for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1252 struct usb_host_endpoint *hep = &intf->endpoint[i];
1253 u8 ep_addr = hep->desc.bEndpointAddress;
1254 u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1255 struct oz_endpoint *ep;
1256 int buffer_size = 0;
1258 oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
1259 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1260 switch (hep->desc.bmAttributes &
1261 USB_ENDPOINT_XFERTYPE_MASK) {
1262 case USB_ENDPOINT_XFER_ISOC:
1263 buffer_size = 24*1024;
1265 case USB_ENDPOINT_XFER_INT:
1271 ep = oz_ep_alloc(mem_flags, buffer_size);
1273 oz_clean_endpoints_for_interface(hcd, port, if_ix);
1276 ep->attrib = hep->desc.bmAttributes;
1277 ep->ep_num = ep_num;
1278 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1279 == USB_ENDPOINT_XFER_ISOC) {
1280 oz_dbg(ON, "wMaxPacketSize = %d\n",
1281 usb_endpoint_maxp(&hep->desc));
1282 ep->credit_ceiling = 200;
1283 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1284 ep->flags |= OZ_F_EP_BUFFERING;
1286 ep->flags |= OZ_F_EP_HAVE_STREAM;
1287 if (oz_usb_stream_create(port->hpd, ep_num))
1288 ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1291 spin_lock_bh(&ozhcd->hcd_lock);
1292 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1293 port->in_ep[ep_num] = ep;
1294 port->iface[if_ix].ep_mask |=
1295 (1<<(ep_num+OZ_NB_ENDPOINTS));
1296 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1297 == USB_ENDPOINT_XFER_ISOC) {
1298 list_add_tail(&ep->link, &port->isoc_in_ep);
1299 request_heartbeat = 1;
1302 port->out_ep[ep_num] = ep;
1303 port->iface[if_ix].ep_mask |= (1<<ep_num);
1304 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1305 == USB_ENDPOINT_XFER_ISOC) {
1306 list_add_tail(&ep->link, &port->isoc_out_ep);
1307 request_heartbeat = 1;
1310 spin_unlock_bh(&ozhcd->hcd_lock);
1311 if (request_heartbeat && port->hpd)
1312 oz_usb_request_heartbeat(port->hpd);
1317 /*------------------------------------------------------------------------------
1320 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1321 struct oz_port *port, int if_ix)
1323 struct oz_hcd *ozhcd = port->ozhcd;
1326 struct list_head ep_list;
1328 oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
1329 if (if_ix >= port->num_iface)
1331 INIT_LIST_HEAD(&ep_list);
1332 spin_lock_bh(&ozhcd->hcd_lock);
1333 mask = port->iface[if_ix].ep_mask;
1334 port->iface[if_ix].ep_mask = 0;
1335 for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1336 struct list_head *e;
1337 /* Gather OUT endpoints.
1339 if ((mask & (1<<i)) && port->out_ep[i]) {
1340 e = &port->out_ep[i]->link;
1341 port->out_ep[i] = NULL;
1342 /* Remove from isoc list if present.
1344 list_move_tail(e, &ep_list);
1346 /* Gather IN endpoints.
1348 if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1349 e = &port->in_ep[i]->link;
1350 port->in_ep[i] = NULL;
1351 list_move_tail(e, &ep_list);
1354 spin_unlock_bh(&ozhcd->hcd_lock);
1355 while (!list_empty(&ep_list)) {
1356 struct oz_endpoint *ep =
1357 list_first_entry(&ep_list, struct oz_endpoint, link);
1358 list_del_init(&ep->link);
1359 oz_ep_free(port, ep);
1363 /*------------------------------------------------------------------------------
1366 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1367 struct oz_port *port, struct usb_host_config *config,
1370 struct oz_hcd *ozhcd = port->ozhcd;
1372 int num_iface = config->desc.bNumInterfaces;
1375 struct oz_interface *iface;
1377 iface = kmalloc(num_iface*sizeof(struct oz_interface),
1378 mem_flags | __GFP_ZERO);
1381 spin_lock_bh(&ozhcd->hcd_lock);
1382 port->iface = iface;
1383 port->num_iface = num_iface;
1384 spin_unlock_bh(&ozhcd->hcd_lock);
1386 for (i = 0; i < num_iface; i++) {
1387 struct usb_host_interface *intf =
1388 &config->intf_cache[i]->altsetting[0];
1389 if (oz_build_endpoints_for_interface(hcd, port, intf,
1395 oz_clean_endpoints_for_config(hcd, port);
1399 /*------------------------------------------------------------------------------
1402 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1403 struct oz_port *port)
1405 struct oz_hcd *ozhcd = port->ozhcd;
1408 oz_dbg(ON, "Deleting endpoints for configuration\n");
1409 for (i = 0; i < port->num_iface; i++)
1410 oz_clean_endpoints_for_interface(hcd, port, i);
1411 spin_lock_bh(&ozhcd->hcd_lock);
1413 oz_dbg(ON, "Freeing interfaces object\n");
1417 port->num_iface = 0;
1418 spin_unlock_bh(&ozhcd->hcd_lock);
1421 /*------------------------------------------------------------------------------
1424 static void *oz_claim_hpd(struct oz_port *port)
1427 struct oz_hcd *ozhcd = port->ozhcd;
1429 spin_lock_bh(&ozhcd->hcd_lock);
1433 spin_unlock_bh(&ozhcd->hcd_lock);
1437 /*------------------------------------------------------------------------------
1440 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1443 struct usb_ctrlrequest *setup;
1450 unsigned complete = 0;
1453 struct oz_port *port = NULL;
1455 oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
1456 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1461 port = &ozhcd->ports[port_ix];
1462 if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1463 || (port->flags & OZ_PORT_F_DYING)) {
1464 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1465 port_ix, urb->dev->devnum);
1469 /* Store port in private context data.
1472 setup = (struct usb_ctrlrequest *)urb->setup_packet;
1473 windex = le16_to_cpu(setup->wIndex);
1474 wvalue = le16_to_cpu(setup->wValue);
1475 wlength = le16_to_cpu(setup->wLength);
1476 oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
1477 oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1478 oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
1479 oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
1480 oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
1482 req_id = port->next_req_id++;
1483 hpd = oz_claim_hpd(port);
1485 oz_dbg(ON, "Cannot claim port\n");
1490 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1491 /* Standard requests
1493 switch (setup->bRequest) {
1494 case USB_REQ_GET_DESCRIPTOR:
1495 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
1497 case USB_REQ_SET_ADDRESS:
1498 oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
1499 oz_dbg(ON, "Port %d address is 0x%x\n",
1501 (u8)le16_to_cpu(setup->wValue));
1502 spin_lock_bh(&ozhcd->hcd_lock);
1503 if (ozhcd->conn_port >= 0) {
1504 ozhcd->ports[ozhcd->conn_port].bus_addr =
1505 (u8)le16_to_cpu(setup->wValue);
1506 oz_dbg(ON, "Clearing conn_port\n");
1507 ozhcd->conn_port = -1;
1509 spin_unlock_bh(&ozhcd->hcd_lock);
1512 case USB_REQ_SET_CONFIGURATION:
1513 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
1515 case USB_REQ_GET_CONFIGURATION:
1516 /* We short circuit this case and reply directly since
1517 * we have the selected configuration number cached.
1519 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
1520 if (urb->transfer_buffer_length >= 1) {
1521 urb->actual_length = 1;
1522 *((u8 *)urb->transfer_buffer) =
1529 case USB_REQ_GET_INTERFACE:
1530 /* We short circuit this case and reply directly since
1531 * we have the selected interface alternative cached.
1533 oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
1534 if (urb->transfer_buffer_length >= 1) {
1535 urb->actual_length = 1;
1536 *((u8 *)urb->transfer_buffer) =
1537 port->iface[(u8)windex].alt;
1538 oz_dbg(ON, "interface = %d alt = %d\n",
1539 windex, port->iface[(u8)windex].alt);
1545 case USB_REQ_SET_INTERFACE:
1546 oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
1550 if (!rc && !complete) {
1552 if ((setup->bRequestType & USB_DIR_IN) == 0)
1554 urb->actual_length = data_len;
1555 if (oz_usb_control_req(port->hpd, req_id, setup,
1556 urb->transfer_buffer, data_len)) {
1559 /* Note: we are queuing the request after we have
1560 * submitted it to be transmitted. If the request were
1561 * to complete before we queued it then it would not
1562 * be found in the queue. It seems impossible for
1563 * this to happen but if it did the request would
1564 * be resubmitted so the problem would hopefully
1565 * resolve itself. Putting the request into the
1566 * queue before it has been sent is worse since the
1567 * urb could be cancelled while we are using it
1568 * to build the request.
1570 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1576 if (rc || complete) {
1577 oz_dbg(ON, "Completing request locally\n");
1578 oz_complete_urb(ozhcd->hcd, urb, rc);
1580 oz_usb_request_heartbeat(port->hpd);
1584 /*------------------------------------------------------------------------------
1587 static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1590 struct oz_port *port = urb->hcpriv;
1593 /* When we are paranoid we keep a list of urbs which we check against
1594 * before handing one back. This is just for debugging during
1595 * development and should be turned off in the released driver.
1597 oz_remember_urb(urb);
1598 /* Check buffer is valid.
1600 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1602 /* Check if there is a device at the port - refuse if not.
1604 if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1606 ep_addr = usb_pipeendpoint(urb->pipe);
1608 /* If the request is not for EP0 then queue it.
1610 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1614 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1619 /*------------------------------------------------------------------------------
1622 static void oz_urb_process_tasklet(unsigned long unused)
1624 unsigned long irq_state;
1626 struct oz_hcd *ozhcd = oz_hcd_claim();
1631 /* This is called from a tasklet so is in softirq context but the urb
1632 * list is filled from any context so we need to lock
1633 * appropriately while removing urbs.
1635 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1636 while (!list_empty(&ozhcd->urb_pending_list)) {
1637 struct oz_urb_link *urbl =
1638 list_first_entry(&ozhcd->urb_pending_list,
1639 struct oz_urb_link, link);
1640 list_del_init(&urbl->link);
1641 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1643 oz_free_urb_link(urbl);
1644 rc = oz_urb_process(ozhcd, urb);
1646 oz_complete_urb(ozhcd->hcd, urb, rc);
1647 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1649 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1653 /*------------------------------------------------------------------------------
1654 * This function searches for the urb in any of the lists it could be in.
1655 * If it is found it is removed from the list and completed. If the urb is
1656 * being processed then it won't be in a list so won't be found. However, the
1657 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1658 * to a non-zero value. When an attempt is made to put the urb back in a list
1659 * the unlinked field will be checked and the urb will then be completed.
1662 static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1664 struct oz_urb_link *urbl = NULL;
1665 struct list_head *e;
1666 struct oz_hcd *ozhcd;
1667 unsigned long irq_state;
1671 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
1674 ozhcd = port->ozhcd;
1675 if (ozhcd == NULL) {
1676 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
1680 /* Look in the tasklet queue.
1682 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1683 list_for_each(e, &ozhcd->urb_cancel_list) {
1684 urbl = container_of(e, struct oz_urb_link, link);
1685 if (urb == urbl->urb) {
1687 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1691 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1694 /* Look in the orphanage.
1696 spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1697 list_for_each(e, &ozhcd->orphanage) {
1698 urbl = container_of(e, struct oz_urb_link, link);
1699 if (urbl->urb == urb) {
1701 oz_dbg(ON, "Found urb in orphanage\n");
1705 ix = (ep_num & 0xf);
1707 if ((ep_num & USB_DIR_IN) && ix)
1708 urbl = oz_remove_urb(port->in_ep[ix], urb);
1710 urbl = oz_remove_urb(port->out_ep[ix], urb);
1712 spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1715 urb->actual_length = 0;
1716 oz_free_urb_link(urbl);
1717 oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
1721 /*------------------------------------------------------------------------------
1724 static void oz_urb_cancel_tasklet(unsigned long unused)
1726 unsigned long irq_state;
1728 struct oz_hcd *ozhcd = oz_hcd_claim();
1732 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1733 while (!list_empty(&ozhcd->urb_cancel_list)) {
1734 struct oz_urb_link *urbl =
1735 list_first_entry(&ozhcd->urb_cancel_list,
1736 struct oz_urb_link, link);
1737 list_del_init(&urbl->link);
1738 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1741 oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1742 oz_free_urb_link(urbl);
1743 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1745 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1749 /*------------------------------------------------------------------------------
1752 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1755 struct oz_urb_link *urbl;
1756 while (!list_empty(&ozhcd->orphanage)) {
1757 urbl = list_first_entry(&ozhcd->orphanage,
1758 struct oz_urb_link, link);
1759 list_del(&urbl->link);
1760 oz_complete_urb(ozhcd->hcd, urbl->urb, status);
1761 oz_free_urb_link(urbl);
1766 /*------------------------------------------------------------------------------
1769 static int oz_hcd_start(struct usb_hcd *hcd)
1771 hcd->power_budget = 200;
1772 hcd->state = HC_STATE_RUNNING;
1773 hcd->uses_new_polling = 1;
1777 /*------------------------------------------------------------------------------
1780 static void oz_hcd_stop(struct usb_hcd *hcd)
1784 /*------------------------------------------------------------------------------
1787 static void oz_hcd_shutdown(struct usb_hcd *hcd)
1791 /*------------------------------------------------------------------------------
1792 * Called to queue an urb for the device.
1793 * This function should return a non-zero error code if it fails the urb but
1794 * should not call usb_hcd_giveback_urb().
1797 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1800 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1803 struct oz_port *port;
1804 unsigned long irq_state;
1805 struct oz_urb_link *urbl;
1807 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1808 if (unlikely(ozhcd == NULL)) {
1809 oz_dbg(URB, "Refused urb(%p) not ozhcd\n", urb);
1812 if (unlikely(hcd->state != HC_STATE_RUNNING)) {
1813 oz_dbg(URB, "Refused urb(%p) not running\n", urb);
1816 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1819 port = &ozhcd->ports[port_ix];
1822 if ((port->flags & OZ_PORT_F_PRESENT) == 0) {
1823 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1824 port_ix, urb->dev->devnum);
1828 /* Put request in queue for processing by tasklet.
1830 urbl = oz_alloc_urb_link();
1831 if (unlikely(urbl == NULL))
1834 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1835 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1837 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1838 oz_free_urb_link(urbl);
1841 list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1842 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1843 tasklet_schedule(&g_urb_process_tasklet);
1844 atomic_inc(&g_pending_urbs);
1848 /*------------------------------------------------------------------------------
1851 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1854 struct oz_urb_link *urbl = NULL;
1855 struct list_head *e;
1857 if (unlikely(ep == NULL))
1859 list_for_each(e, &ep->urb_list) {
1860 urbl = container_of(e, struct oz_urb_link, link);
1861 if (urbl->urb == urb) {
1863 if (usb_pipeisoc(urb->pipe)) {
1864 ep->credit -= urb->number_of_packets;
1874 /*------------------------------------------------------------------------------
1875 * Called to dequeue a previously submitted urb for the device.
1878 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1880 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1881 struct oz_urb_link *urbl = NULL;
1883 unsigned long irq_state;
1885 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1886 urbl = oz_alloc_urb_link();
1887 if (unlikely(urbl == NULL))
1889 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1890 /* The following function checks the urb is still in the queue
1891 * maintained by the core and that the unlinked field is zero.
1892 * If both are true the function sets the unlinked field and returns
1893 * zero. Otherwise it returns an error.
1895 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1896 /* We have to check we haven't completed the urb or are about
1897 * to complete it. When we do we set hcpriv to 0 so if this has
1898 * already happened we don't put the urb in the cancel queue.
1900 if ((rc == 0) && urb->hcpriv) {
1902 urbl->port = (struct oz_port *)urb->hcpriv;
1903 urbl->ep_num = usb_pipeendpoint(urb->pipe);
1904 if (usb_pipein(urb->pipe))
1905 urbl->ep_num |= USB_DIR_IN;
1906 list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1907 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1908 tasklet_schedule(&g_urb_cancel_tasklet);
1910 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1911 oz_free_urb_link(urbl);
1916 /*------------------------------------------------------------------------------
1919 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1920 struct usb_host_endpoint *ep)
1924 /*------------------------------------------------------------------------------
1927 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1928 struct usb_host_endpoint *ep)
1932 /*------------------------------------------------------------------------------
1935 static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1937 oz_dbg(ON, "oz_hcd_get_frame_number\n");
1938 return oz_usb_get_frame_number();
1941 /*------------------------------------------------------------------------------
1943 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1944 * always do that in softirq context.
1946 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1948 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1954 spin_lock_bh(&ozhcd->hcd_lock);
1955 for (i = 0; i < OZ_NB_PORTS; i++) {
1956 if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
1957 oz_dbg(HUB, "Port %d changed\n", i);
1958 ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
1960 buf[0] |= 1 << (i + 1);
1962 buf[1] |= 1 << (i - 7);
1965 spin_unlock_bh(&ozhcd->hcd_lock);
1966 if (buf[0] != 0 || buf[1] != 0)
1972 /*------------------------------------------------------------------------------
1975 static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1976 struct usb_hub_descriptor *desc)
1978 memset(desc, 0, sizeof(*desc));
1979 desc->bDescriptorType = 0x29;
1980 desc->bDescLength = 9;
1981 desc->wHubCharacteristics = (__force __u16)
1982 __constant_cpu_to_le16(0x0001);
1983 desc->bNbrPorts = OZ_NB_PORTS;
1986 /*------------------------------------------------------------------------------
1989 static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1991 struct oz_port *port;
1993 u8 port_id = (u8)windex;
1994 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1995 unsigned set_bits = 0;
1996 unsigned clear_bits = 0;
1998 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2000 port = &ozhcd->ports[port_id-1];
2002 case USB_PORT_FEAT_CONNECTION:
2003 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
2005 case USB_PORT_FEAT_ENABLE:
2006 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
2008 case USB_PORT_FEAT_SUSPEND:
2009 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
2011 case USB_PORT_FEAT_OVER_CURRENT:
2012 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2014 case USB_PORT_FEAT_RESET:
2015 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
2016 set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
2017 clear_bits = USB_PORT_STAT_RESET;
2018 ozhcd->ports[port_id-1].bus_addr = 0;
2020 case USB_PORT_FEAT_POWER:
2021 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2022 set_bits |= USB_PORT_STAT_POWER;
2024 case USB_PORT_FEAT_LOWSPEED:
2025 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2027 case USB_PORT_FEAT_C_CONNECTION:
2028 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2030 case USB_PORT_FEAT_C_ENABLE:
2031 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2033 case USB_PORT_FEAT_C_SUSPEND:
2034 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2036 case USB_PORT_FEAT_C_OVER_CURRENT:
2037 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2039 case USB_PORT_FEAT_C_RESET:
2040 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2042 case USB_PORT_FEAT_TEST:
2043 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2045 case USB_PORT_FEAT_INDICATOR:
2046 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2049 oz_dbg(HUB, "Other %d\n", wvalue);
2052 if (set_bits || clear_bits) {
2053 spin_lock_bh(&port->port_lock);
2054 port->status &= ~clear_bits;
2055 port->status |= set_bits;
2056 spin_unlock_bh(&port->port_lock);
2058 oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
2062 /*------------------------------------------------------------------------------
2065 static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
2067 struct oz_port *port;
2069 u8 port_id = (u8)windex;
2070 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2071 unsigned clear_bits = 0;
2073 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2075 port = &ozhcd->ports[port_id-1];
2077 case USB_PORT_FEAT_CONNECTION:
2078 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
2080 case USB_PORT_FEAT_ENABLE:
2081 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
2082 clear_bits = USB_PORT_STAT_ENABLE;
2084 case USB_PORT_FEAT_SUSPEND:
2085 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
2087 case USB_PORT_FEAT_OVER_CURRENT:
2088 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2090 case USB_PORT_FEAT_RESET:
2091 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
2093 case USB_PORT_FEAT_POWER:
2094 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2095 clear_bits |= USB_PORT_STAT_POWER;
2097 case USB_PORT_FEAT_LOWSPEED:
2098 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2100 case USB_PORT_FEAT_C_CONNECTION:
2101 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2102 clear_bits = (USB_PORT_STAT_C_CONNECTION << 16);
2104 case USB_PORT_FEAT_C_ENABLE:
2105 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2106 clear_bits = (USB_PORT_STAT_C_ENABLE << 16);
2108 case USB_PORT_FEAT_C_SUSPEND:
2109 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2111 case USB_PORT_FEAT_C_OVER_CURRENT:
2112 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2114 case USB_PORT_FEAT_C_RESET:
2115 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2116 clear_bits = (USB_PORT_FEAT_C_RESET << 16);
2118 case USB_PORT_FEAT_TEST:
2119 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2121 case USB_PORT_FEAT_INDICATOR:
2122 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2125 oz_dbg(HUB, "Other %d\n", wvalue);
2129 spin_lock_bh(&port->port_lock);
2130 port->status &= ~clear_bits;
2131 spin_unlock_bh(&port->port_lock);
2133 oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2134 port_id, ozhcd->ports[port_id-1].status);
2138 /*------------------------------------------------------------------------------
2141 static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2143 struct oz_hcd *ozhcd;
2146 if ((windex < 1) || (windex > OZ_NB_PORTS))
2148 ozhcd = oz_hcd_private(hcd);
2149 oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
2150 status = ozhcd->ports[windex-1].status;
2151 put_unaligned(cpu_to_le32(status), (__le32 *)buf);
2152 oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
2156 /*------------------------------------------------------------------------------
2159 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2160 u16 windex, char *buf, u16 wlength)
2165 case ClearHubFeature:
2166 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
2168 case ClearPortFeature:
2169 err = oz_clear_port_feature(hcd, wvalue, windex);
2171 case GetHubDescriptor:
2172 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2175 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
2176 put_unaligned(__constant_cpu_to_le32(0), (__le32 *)buf);
2179 err = oz_get_port_status(hcd, windex, buf);
2182 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
2184 case SetPortFeature:
2185 err = oz_set_port_feature(hcd, wvalue, windex);
2188 oz_dbg(HUB, "Other: %d\n", req_type);
2194 /*------------------------------------------------------------------------------
2197 static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2199 struct oz_hcd *ozhcd;
2201 ozhcd = oz_hcd_private(hcd);
2202 spin_lock_bh(&ozhcd->hcd_lock);
2203 hcd->state = HC_STATE_SUSPENDED;
2204 ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2205 spin_unlock_bh(&ozhcd->hcd_lock);
2209 /*------------------------------------------------------------------------------
2212 static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2214 struct oz_hcd *ozhcd;
2216 ozhcd = oz_hcd_private(hcd);
2217 spin_lock_bh(&ozhcd->hcd_lock);
2218 ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2219 hcd->state = HC_STATE_RUNNING;
2220 spin_unlock_bh(&ozhcd->hcd_lock);
2224 /*------------------------------------------------------------------------------
2226 static void oz_plat_shutdown(struct platform_device *dev)
2230 /*------------------------------------------------------------------------------
2233 static int oz_plat_probe(struct platform_device *dev)
2237 struct usb_hcd *hcd;
2238 struct oz_hcd *ozhcd;
2240 hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
2242 oz_dbg(ON, "Failed to created hcd object OK\n");
2245 ozhcd = oz_hcd_private(hcd);
2246 memset(ozhcd, 0, sizeof(*ozhcd));
2247 INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2248 INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2249 INIT_LIST_HEAD(&ozhcd->orphanage);
2251 ozhcd->conn_port = -1;
2252 spin_lock_init(&ozhcd->hcd_lock);
2253 for (i = 0; i < OZ_NB_PORTS; i++) {
2254 struct oz_port *port = &ozhcd->ports[i];
2255 port->ozhcd = ozhcd;
2258 port->bus_addr = 0xff;
2259 spin_lock_init(&port->port_lock);
2261 err = usb_add_hcd(hcd, 0, 0);
2263 oz_dbg(ON, "Failed to add hcd object OK\n");
2267 spin_lock_bh(&g_hcdlock);
2269 spin_unlock_bh(&g_hcdlock);
2273 /*------------------------------------------------------------------------------
2276 static int oz_plat_remove(struct platform_device *dev)
2278 struct usb_hcd *hcd = platform_get_drvdata(dev);
2279 struct oz_hcd *ozhcd;
2283 ozhcd = oz_hcd_private(hcd);
2284 spin_lock_bh(&g_hcdlock);
2285 if (ozhcd == g_ozhcd)
2287 spin_unlock_bh(&g_hcdlock);
2288 oz_dbg(ON, "Clearing orphanage\n");
2289 oz_hcd_clear_orphanage(ozhcd, -EPIPE);
2290 oz_dbg(ON, "Removing hcd\n");
2291 usb_remove_hcd(hcd);
2293 oz_empty_link_pool();
2297 /*------------------------------------------------------------------------------
2300 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2306 /*------------------------------------------------------------------------------
2309 static int oz_plat_resume(struct platform_device *dev)
2314 /*------------------------------------------------------------------------------
2317 int oz_hcd_init(void)
2323 tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
2324 tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
2325 err = platform_driver_register(&g_oz_plat_drv);
2326 oz_dbg(ON, "platform_driver_register() returned %d\n", err);
2329 g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
2330 if (g_plat_dev == NULL) {
2334 oz_dbg(ON, "platform_device_alloc() succeeded\n");
2335 err = platform_device_add(g_plat_dev);
2338 oz_dbg(ON, "platform_device_add() succeeded\n");
2341 platform_device_put(g_plat_dev);
2343 platform_driver_unregister(&g_oz_plat_drv);
2345 tasklet_disable(&g_urb_process_tasklet);
2346 tasklet_disable(&g_urb_cancel_tasklet);
2347 oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
2351 /*------------------------------------------------------------------------------
2354 void oz_hcd_term(void)
2356 tasklet_kill(&g_urb_process_tasklet);
2357 tasklet_kill(&g_urb_cancel_tasklet);
2358 platform_device_unregister(g_plat_dev);
2359 platform_driver_unregister(&g_oz_plat_drv);
2360 oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));