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"
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 100
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 /* Debounce time HCD driver should wait before unregistering.
62 #define OZ_HUB_DEBOUNCE_TIMEOUT 1500
65 * Used to link urbs together and also store some status information for each
67 * A cache of these are kept in a pool to reduce number of calls to kmalloc.
70 struct list_head link;
75 unsigned submit_counter;
78 /* Holds state information about a USB endpoint.
80 #define OZ_EP_BUFFER_SIZE_ISOC (1024 * 24)
81 #define OZ_EP_BUFFER_SIZE_INT 512
83 struct list_head urb_list; /* List of oz_urb_link items. */
84 struct list_head link; /* For isoc ep, links in to isoc
86 struct timespec timestamp;
100 /* Bits in the flags field. */
101 #define OZ_F_EP_BUFFERING 0x1
102 #define OZ_F_EP_HAVE_STREAM 0x2
104 /* Holds state information about a USB interface.
106 struct oz_interface {
111 /* Holds state information about an hcd port.
113 #define OZ_NB_ENDPOINTS 16
118 struct oz_hcd *ozhcd;
119 spinlock_t port_lock;
124 struct oz_interface *iface;
125 struct oz_endpoint *out_ep[OZ_NB_ENDPOINTS];
126 struct oz_endpoint *in_ep[OZ_NB_ENDPOINTS];
127 struct list_head isoc_out_ep;
128 struct list_head isoc_in_ep;
131 #define OZ_PORT_F_PRESENT 0x1
132 #define OZ_PORT_F_CHANGED 0x2
133 #define OZ_PORT_F_DYING 0x4
135 /* Data structure in the private context area of struct usb_hcd.
137 #define OZ_NB_PORTS 8
140 struct list_head urb_pending_list;
141 struct list_head urb_cancel_list;
142 struct list_head orphanage;
143 int conn_port; /* Port that is currently connecting, -1 if none.*/
144 struct oz_port ports[OZ_NB_PORTS];
149 /* Bits in flags field.
151 #define OZ_HDC_F_SUSPENDED 0x1
154 * Static function prototypes.
156 static int oz_hcd_start(struct usb_hcd *hcd);
157 static void oz_hcd_stop(struct usb_hcd *hcd);
158 static void oz_hcd_shutdown(struct usb_hcd *hcd);
159 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
161 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
162 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
163 struct usb_host_endpoint *ep);
164 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
165 struct usb_host_endpoint *ep);
166 static int oz_hcd_get_frame_number(struct usb_hcd *hcd);
167 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf);
168 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
169 u16 windex, char *buf, u16 wlength);
170 static int oz_hcd_bus_suspend(struct usb_hcd *hcd);
171 static int oz_hcd_bus_resume(struct usb_hcd *hcd);
172 static int oz_plat_probe(struct platform_device *dev);
173 static int oz_plat_remove(struct platform_device *dev);
174 static void oz_plat_shutdown(struct platform_device *dev);
175 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg);
176 static int oz_plat_resume(struct platform_device *dev);
177 static void oz_urb_process_tasklet(unsigned long unused);
178 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
179 struct oz_port *port, struct usb_host_config *config,
181 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
182 struct oz_port *port);
183 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
184 struct oz_port *port,
185 struct usb_host_interface *intf, gfp_t mem_flags);
186 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
187 struct oz_port *port, int if_ix);
188 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
190 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
192 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status);
195 * Static external variables.
197 static struct platform_device *g_plat_dev;
198 static struct oz_hcd *g_ozhcd;
199 static DEFINE_SPINLOCK(g_hcdlock); /* Guards g_ozhcd. */
200 static const char g_hcd_name[] = "Ozmo WPAN";
201 static struct list_head *g_link_pool;
202 static int g_link_pool_size;
203 static DEFINE_SPINLOCK(g_link_lock);
204 static DEFINE_SPINLOCK(g_tasklet_lock);
205 static struct tasklet_struct g_urb_process_tasklet;
206 static struct tasklet_struct g_urb_cancel_tasklet;
207 static atomic_t g_pending_urbs = ATOMIC_INIT(0);
208 static atomic_t g_usb_frame_number = ATOMIC_INIT(0);
209 static const struct hc_driver g_oz_hc_drv = {
210 .description = g_hcd_name,
211 .product_desc = "Ozmo Devices WPAN",
212 .hcd_priv_size = sizeof(struct oz_hcd),
214 .start = oz_hcd_start,
216 .shutdown = oz_hcd_shutdown,
217 .urb_enqueue = oz_hcd_urb_enqueue,
218 .urb_dequeue = oz_hcd_urb_dequeue,
219 .endpoint_disable = oz_hcd_endpoint_disable,
220 .endpoint_reset = oz_hcd_endpoint_reset,
221 .get_frame_number = oz_hcd_get_frame_number,
222 .hub_status_data = oz_hcd_hub_status_data,
223 .hub_control = oz_hcd_hub_control,
224 .bus_suspend = oz_hcd_bus_suspend,
225 .bus_resume = oz_hcd_bus_resume,
228 static struct platform_driver g_oz_plat_drv = {
229 .probe = oz_plat_probe,
230 .remove = oz_plat_remove,
231 .shutdown = oz_plat_shutdown,
232 .suspend = oz_plat_suspend,
233 .resume = oz_plat_resume,
235 .name = OZ_PLAT_DEV_NAME,
236 .owner = THIS_MODULE,
241 * Gets our private context area (which is of type struct oz_hcd) from the
245 static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
247 return (struct oz_hcd *)hcd->hcd_priv;
251 * Searches list of ports to find the index of the one with a specified USB
252 * bus address. If none of the ports has the bus address then the connection
253 * port is returned, if there is one or -1 otherwise.
256 static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
260 for (i = 0; i < OZ_NB_PORTS; i++) {
261 if (ozhcd->ports[i].bus_addr == bus_addr)
264 return ozhcd->conn_port;
268 * Allocates an urb link, first trying the pool but going to heap if empty.
271 static struct oz_urb_link *oz_alloc_urb_link(void)
273 struct oz_urb_link *urbl = NULL;
274 unsigned long irq_state;
276 spin_lock_irqsave(&g_link_lock, irq_state);
278 urbl = container_of(g_link_pool, struct oz_urb_link, link);
279 g_link_pool = urbl->link.next;
282 spin_unlock_irqrestore(&g_link_lock, irq_state);
284 urbl = kmalloc(sizeof(struct oz_urb_link), GFP_ATOMIC);
289 * Frees an urb link by putting it in the pool if there is enough space or
290 * deallocating it to heap otherwise.
293 static void oz_free_urb_link(struct oz_urb_link *urbl)
296 unsigned long irq_state;
298 spin_lock_irqsave(&g_link_lock, irq_state);
299 if (g_link_pool_size < OZ_MAX_LINK_POOL_SIZE) {
300 urbl->link.next = g_link_pool;
301 g_link_pool = &urbl->link;
305 spin_unlock_irqrestore(&g_link_lock, irq_state);
311 * Deallocates all the urb links in the pool.
314 static void oz_empty_link_pool(void)
317 unsigned long irq_state;
319 spin_lock_irqsave(&g_link_lock, irq_state);
322 g_link_pool_size = 0;
323 spin_unlock_irqrestore(&g_link_lock, irq_state);
325 struct oz_urb_link *urbl =
326 container_of(e, struct oz_urb_link, link);
333 * Allocates endpoint structure and optionally a buffer. If a buffer is
334 * allocated it immediately follows the endpoint structure.
337 static struct oz_endpoint *oz_ep_alloc(int buffer_size, gfp_t mem_flags)
339 struct oz_endpoint *ep =
340 kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
342 INIT_LIST_HEAD(&ep->urb_list);
343 INIT_LIST_HEAD(&ep->link);
346 ep->buffer_size = buffer_size;
347 ep->buffer = (u8 *)(ep+1);
354 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
356 * Context: softirq or process
358 static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd,
361 struct oz_urb_link *urbl;
364 list_for_each(e, &ozhcd->urb_cancel_list) {
365 urbl = container_of(e, struct oz_urb_link, link);
366 if (urb == urbl->urb) {
375 * This is called when we have finished processing an urb. It unlinks it from
376 * the ep and returns it to the core.
377 * Context: softirq or process
379 static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
382 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
383 unsigned long irq_state;
384 struct oz_urb_link *cancel_urbl;
386 spin_lock_irqsave(&g_tasklet_lock, irq_state);
387 usb_hcd_unlink_urb_from_ep(hcd, urb);
388 /* Clear hcpriv which will prevent it being put in the cancel list
389 * in the event that an attempt is made to cancel it.
392 /* Walk the cancel list in case the urb is already sitting there.
393 * Since we process the cancel list in a tasklet rather than in
394 * the dequeue function this could happen.
396 cancel_urbl = oz_uncancel_urb(ozhcd, urb);
397 /* Note: we release lock but do not enable local irqs.
398 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
399 * or at least other host controllers disable interrupts at this point
400 * so we do the same. We must, however, release the lock otherwise a
401 * deadlock will occur if an urb is submitted to our driver in the urb
402 * completion function. Because we disable interrupts it is possible
403 * that the urb_enqueue function can be called with them disabled.
405 spin_unlock(&g_tasklet_lock);
406 if (oz_forget_urb(urb)) {
407 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
409 atomic_dec(&g_pending_urbs);
410 usb_hcd_giveback_urb(hcd, urb, status);
412 spin_lock(&g_tasklet_lock);
413 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
415 oz_free_urb_link(cancel_urbl);
419 * Deallocates an endpoint including deallocating any associated stream and
420 * returning any queued urbs to the core.
423 static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
426 struct list_head list;
427 struct oz_hcd *ozhcd = port->ozhcd;
429 INIT_LIST_HEAD(&list);
430 if (ep->flags & OZ_F_EP_HAVE_STREAM)
431 oz_usb_stream_delete(port->hpd, ep->ep_num);
432 /* Transfer URBs to the orphanage while we hold the lock. */
433 spin_lock_bh(&ozhcd->hcd_lock);
434 /* Note: this works even if ep->urb_list is empty.*/
435 list_replace_init(&ep->urb_list, &list);
436 /* Put the URBs in the orphanage. */
437 list_splice_tail(&list, &ozhcd->orphanage);
438 spin_unlock_bh(&ozhcd->hcd_lock);
440 oz_dbg(ON, "Freeing endpoint memory\n");
447 static void oz_complete_buffered_urb(struct oz_port *port,
448 struct oz_endpoint *ep,
451 int data_len, available_space, copy_len;
453 data_len = ep->buffer[ep->out_ix];
454 if (data_len <= urb->transfer_buffer_length)
455 available_space = data_len;
457 available_space = urb->transfer_buffer_length;
459 if (++ep->out_ix == ep->buffer_size)
461 copy_len = ep->buffer_size - ep->out_ix;
462 if (copy_len >= available_space)
463 copy_len = available_space;
464 memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
466 if (copy_len < available_space) {
467 memcpy((urb->transfer_buffer + copy_len), ep->buffer,
468 (available_space - copy_len));
469 ep->out_ix = available_space - copy_len;
471 ep->out_ix += copy_len;
473 urb->actual_length = available_space;
474 if (ep->out_ix == ep->buffer_size)
477 ep->buffered_units--;
478 oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
480 oz_complete_urb(port->ozhcd->hcd, urb, 0);
486 static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
487 struct urb *urb, u8 req_id)
489 struct oz_urb_link *urbl;
490 struct oz_endpoint *ep = NULL;
493 if (ep_addr >= OZ_NB_ENDPOINTS) {
494 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
497 urbl = oz_alloc_urb_link();
500 urbl->submit_counter = 0;
502 urbl->req_id = req_id;
503 urbl->ep_num = ep_addr;
504 /* Hold lock while we insert the URB into the list within the
505 * endpoint structure.
507 spin_lock_bh(&port->ozhcd->hcd_lock);
508 /* If the urb has been unlinked while out of any list then
512 spin_unlock_bh(&port->ozhcd->hcd_lock);
513 oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
514 oz_complete_urb(port->ozhcd->hcd, urb, 0);
515 oz_free_urb_link(urbl);
520 ep = port->in_ep[ep_addr];
522 ep = port->out_ep[ep_addr];
528 /*For interrupt endpoint check for buffered data
531 if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
532 && ep->buffered_units > 0) {
533 oz_free_urb_link(urbl);
534 spin_unlock_bh(&port->ozhcd->hcd_lock);
535 oz_complete_buffered_urb(port, ep, urb);
540 list_add_tail(&urbl->link, &ep->urb_list);
541 if (!in_dir && ep_addr && (ep->credit < 0)) {
542 getrawmonotonic(&ep->timestamp);
549 spin_unlock_bh(&port->ozhcd->hcd_lock);
551 oz_free_urb_link(urbl);
556 * Removes an urb from the queue in the endpoint.
557 * Returns 0 if it is found and -EIDRM otherwise.
560 static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
563 struct oz_urb_link *urbl = NULL;
564 struct oz_endpoint *ep;
566 spin_lock_bh(&port->ozhcd->hcd_lock);
568 ep = port->in_ep[ep_addr];
570 ep = port->out_ep[ep_addr];
574 list_for_each(e, &ep->urb_list) {
575 urbl = container_of(e, struct oz_urb_link, link);
576 if (urbl->urb == urb) {
583 spin_unlock_bh(&port->ozhcd->hcd_lock);
585 oz_free_urb_link(urbl);
586 return urbl ? 0 : -EIDRM;
590 * Finds an urb given its request id.
593 static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
596 struct oz_hcd *ozhcd = port->ozhcd;
597 struct urb *urb = NULL;
598 struct oz_urb_link *urbl;
599 struct oz_endpoint *ep;
601 spin_lock_bh(&ozhcd->hcd_lock);
602 ep = port->out_ep[ep_ix];
606 list_for_each(e, &ep->urb_list) {
607 urbl = container_of(e, struct oz_urb_link, link);
608 if (urbl->req_id == req_id) {
615 spin_unlock_bh(&ozhcd->hcd_lock);
616 /* If urb is non-zero then we we must have an urb link to delete.
619 oz_free_urb_link(urbl);
624 * Pre-condition: Port lock must be held.
627 static void oz_acquire_port(struct oz_port *port, void *hpd)
629 INIT_LIST_HEAD(&port->isoc_out_ep);
630 INIT_LIST_HEAD(&port->isoc_in_ep);
631 port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
632 port->status |= USB_PORT_STAT_CONNECTION |
633 (USB_PORT_STAT_C_CONNECTION << 16);
641 static struct oz_hcd *oz_hcd_claim(void)
643 struct oz_hcd *ozhcd;
645 spin_lock_bh(&g_hcdlock);
648 usb_get_hcd(ozhcd->hcd);
649 spin_unlock_bh(&g_hcdlock);
656 static inline void oz_hcd_put(struct oz_hcd *ozhcd)
659 usb_put_hcd(ozhcd->hcd);
663 * This is called by the protocol handler to notify that a PD has arrived.
664 * We allocate a port to associate with the PD and create a structure for
665 * endpoint 0. This port is made the connection port.
666 * In the event that one of the other port is already a connection port then
668 * TODO We should be able to do better than fail and should be able remember
669 * that this port needs configuring and make it the connection port once the
670 * current connection port has been assigned an address. Collisions here are
671 * probably very rare indeed.
674 struct oz_port *oz_hcd_pd_arrived(void *hpd)
677 struct oz_port *hport;
678 struct oz_hcd *ozhcd;
679 struct oz_endpoint *ep;
681 ozhcd = oz_hcd_claim();
684 /* Allocate an endpoint object in advance (before holding hcd lock) to
685 * use for out endpoint 0.
687 ep = oz_ep_alloc(0, GFP_ATOMIC);
691 spin_lock_bh(&ozhcd->hcd_lock);
692 if (ozhcd->conn_port >= 0)
695 for (i = 0; i < OZ_NB_PORTS; i++) {
696 struct oz_port *port = &ozhcd->ports[i];
698 spin_lock(&port->port_lock);
699 if (!(port->flags & (OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED))) {
700 oz_acquire_port(port, hpd);
701 spin_unlock(&port->port_lock);
704 spin_unlock(&port->port_lock);
706 if (i == OZ_NB_PORTS)
709 ozhcd->conn_port = i;
710 hport = &ozhcd->ports[i];
711 hport->out_ep[0] = ep;
712 spin_unlock_bh(&ozhcd->hcd_lock);
713 if (ozhcd->flags & OZ_HDC_F_SUSPENDED)
714 usb_hcd_resume_root_hub(ozhcd->hcd);
715 usb_hcd_poll_rh_status(ozhcd->hcd);
721 spin_unlock_bh(&ozhcd->hcd_lock);
722 oz_ep_free(NULL, ep);
729 * This is called by the protocol handler to notify that the PD has gone away.
730 * We need to deallocate all resources and then request that the root hub is
731 * polled. We release the reference we hold on the PD.
734 void oz_hcd_pd_departed(struct oz_port *port)
736 struct oz_hcd *ozhcd;
738 struct oz_endpoint *ep = NULL;
741 oz_dbg(ON, "%s: port = 0\n", __func__);
747 /* Check if this is the connection port - if so clear it.
749 spin_lock_bh(&ozhcd->hcd_lock);
750 if ((ozhcd->conn_port >= 0) &&
751 (port == &ozhcd->ports[ozhcd->conn_port])) {
752 oz_dbg(ON, "Clearing conn_port\n");
753 ozhcd->conn_port = -1;
755 spin_lock(&port->port_lock);
756 port->flags |= OZ_PORT_F_DYING;
757 spin_unlock(&port->port_lock);
758 spin_unlock_bh(&ozhcd->hcd_lock);
760 oz_clean_endpoints_for_config(ozhcd->hcd, port);
761 spin_lock_bh(&port->port_lock);
764 port->bus_addr = 0xff;
765 port->config_num = 0;
766 port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
767 port->flags |= OZ_PORT_F_CHANGED;
768 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
769 port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
770 /* If there is an endpont 0 then clear the pointer while we hold
771 * the spinlock be we deallocate it after releasing the lock.
773 if (port->out_ep[0]) {
774 ep = port->out_ep[0];
775 port->out_ep[0] = NULL;
777 spin_unlock_bh(&port->port_lock);
779 oz_ep_free(port, ep);
780 usb_hcd_poll_rh_status(ozhcd->hcd);
787 void oz_hcd_pd_reset(void *hpd, void *hport)
789 /* Cleanup the current configuration and report reset to the core.
791 struct oz_port *port = (struct oz_port *)hport;
792 struct oz_hcd *ozhcd = port->ozhcd;
794 oz_dbg(ON, "PD Reset\n");
795 spin_lock_bh(&port->port_lock);
796 port->flags |= OZ_PORT_F_CHANGED;
797 port->status |= USB_PORT_STAT_RESET;
798 port->status |= (USB_PORT_STAT_C_RESET << 16);
799 spin_unlock_bh(&port->port_lock);
800 oz_clean_endpoints_for_config(ozhcd->hcd, port);
801 usb_hcd_poll_rh_status(ozhcd->hcd);
807 void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
808 int length, int offset, int total_size)
810 struct oz_port *port = (struct oz_port *)hport;
814 oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
815 length, offset, total_size);
816 urb = oz_find_urb_by_id(port, 0, req_id);
821 int required_size = urb->transfer_buffer_length;
823 if (required_size > total_size)
824 required_size = total_size;
825 copy_len = required_size-offset;
826 if (length <= copy_len)
828 memcpy(urb->transfer_buffer+offset, desc, copy_len);
830 if (offset < required_size) {
831 struct usb_ctrlrequest *setup =
832 (struct usb_ctrlrequest *)urb->setup_packet;
833 unsigned wvalue = le16_to_cpu(setup->wValue);
835 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
837 else if (oz_usb_get_desc_req(port->hpd, req_id,
838 setup->bRequestType, (u8)(wvalue>>8),
839 (u8)wvalue, setup->wIndex, offset,
840 required_size-offset)) {
841 oz_dequeue_ep_urb(port, 0, 0, urb);
848 urb->actual_length = total_size;
849 oz_complete_urb(port->ozhcd->hcd, urb, 0);
855 static void oz_display_conf_type(u8 t)
858 case USB_REQ_GET_STATUS:
859 oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
861 case USB_REQ_CLEAR_FEATURE:
862 oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
864 case USB_REQ_SET_FEATURE:
865 oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
867 case USB_REQ_SET_ADDRESS:
868 oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
870 case USB_REQ_GET_DESCRIPTOR:
871 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
873 case USB_REQ_SET_DESCRIPTOR:
874 oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
876 case USB_REQ_GET_CONFIGURATION:
877 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
879 case USB_REQ_SET_CONFIGURATION:
880 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
882 case USB_REQ_GET_INTERFACE:
883 oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
885 case USB_REQ_SET_INTERFACE:
886 oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
888 case USB_REQ_SYNCH_FRAME:
889 oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
897 static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
898 u8 rcode, u8 config_num)
901 struct usb_hcd *hcd = port->ozhcd->hcd;
904 port->config_num = config_num;
905 oz_clean_endpoints_for_config(hcd, port);
906 if (oz_build_endpoints_for_config(hcd, port,
907 &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
913 oz_complete_urb(hcd, urb, rc);
919 static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
920 u8 rcode, u8 if_num, u8 alt)
922 struct usb_hcd *hcd = port->ozhcd->hcd;
925 if ((rcode == 0) && (port->config_num > 0)) {
926 struct usb_host_config *config;
927 struct usb_host_interface *intf;
929 oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
930 oz_clean_endpoints_for_interface(hcd, port, if_num);
931 config = &urb->dev->config[port->config_num-1];
932 intf = &config->intf_cache[if_num]->altsetting[alt];
933 if (oz_build_endpoints_for_interface(hcd, port, intf,
937 port->iface[if_num].alt = alt;
941 oz_complete_urb(hcd, urb, rc);
947 void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
950 struct oz_port *port = (struct oz_port *)hport;
952 struct usb_ctrlrequest *setup;
953 struct usb_hcd *hcd = port->ozhcd->hcd;
957 oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
958 urb = oz_find_urb_by_id(port, 0, req_id);
960 oz_dbg(ON, "URB not found\n");
963 setup = (struct usb_ctrlrequest *)urb->setup_packet;
964 windex = le16_to_cpu(setup->wIndex);
965 wvalue = le16_to_cpu(setup->wValue);
966 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
967 /* Standard requests */
968 oz_display_conf_type(setup->bRequest);
969 switch (setup->bRequest) {
970 case USB_REQ_SET_CONFIGURATION:
971 oz_hcd_complete_set_config(port, urb, rcode,
974 case USB_REQ_SET_INTERFACE:
975 oz_hcd_complete_set_interface(port, urb, rcode,
976 (u8)windex, (u8)wvalue);
979 oz_complete_urb(hcd, urb, 0);
985 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
987 if (data_len <= urb->transfer_buffer_length)
990 copy_len = urb->transfer_buffer_length;
991 memcpy(urb->transfer_buffer, data, copy_len);
992 urb->actual_length = copy_len;
994 oz_complete_urb(hcd, urb, 0);
999 * Context: softirq-serialized
1001 static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
1009 space = ep->out_ix-ep->in_ix-1;
1011 space += ep->buffer_size;
1012 if (space < (data_len+1)) {
1013 oz_dbg(ON, "Buffer full\n");
1016 ep->buffer[ep->in_ix] = (u8)data_len;
1017 if (++ep->in_ix == ep->buffer_size)
1019 copy_len = ep->buffer_size - ep->in_ix;
1020 if (copy_len > data_len)
1021 copy_len = data_len;
1022 memcpy(&ep->buffer[ep->in_ix], data, copy_len);
1024 if (copy_len < data_len) {
1025 memcpy(ep->buffer, data+copy_len, data_len-copy_len);
1026 ep->in_ix = data_len-copy_len;
1028 ep->in_ix += copy_len;
1030 if (ep->in_ix == ep->buffer_size)
1032 ep->buffered_units++;
1037 * Context: softirq-serialized
1039 void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
1041 struct oz_port *port = (struct oz_port *)hport;
1042 struct oz_endpoint *ep;
1043 struct oz_hcd *ozhcd = port->ozhcd;
1045 spin_lock_bh(&ozhcd->hcd_lock);
1046 ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
1049 switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
1050 case USB_ENDPOINT_XFER_INT:
1051 case USB_ENDPOINT_XFER_BULK:
1052 if (!list_empty(&ep->urb_list)) {
1053 struct oz_urb_link *urbl =
1054 list_first_entry(&ep->urb_list,
1055 struct oz_urb_link, link);
1059 list_del_init(&urbl->link);
1060 spin_unlock_bh(&ozhcd->hcd_lock);
1062 oz_free_urb_link(urbl);
1063 if (data_len <= urb->transfer_buffer_length)
1064 copy_len = data_len;
1066 copy_len = urb->transfer_buffer_length;
1067 memcpy(urb->transfer_buffer, data, copy_len);
1068 urb->actual_length = copy_len;
1069 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1072 oz_dbg(ON, "buffering frame as URB is not available\n");
1073 oz_hcd_buffer_data(ep, data, data_len);
1076 case USB_ENDPOINT_XFER_ISOC:
1077 oz_hcd_buffer_data(ep, data, data_len);
1081 spin_unlock_bh(&ozhcd->hcd_lock);
1087 static inline int oz_usb_get_frame_number(void)
1089 return atomic_inc_return(&g_usb_frame_number);
1095 int oz_hcd_heartbeat(void *hport)
1098 struct oz_port *port = (struct oz_port *)hport;
1099 struct oz_hcd *ozhcd = port->ozhcd;
1100 struct oz_urb_link *urbl;
1101 struct list_head xfr_list;
1102 struct list_head *e;
1103 struct list_head *n;
1105 struct oz_endpoint *ep;
1106 struct timespec ts, delta;
1108 getrawmonotonic(&ts);
1109 INIT_LIST_HEAD(&xfr_list);
1110 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1112 spin_lock_bh(&ozhcd->hcd_lock);
1113 list_for_each(e, &port->isoc_out_ep) {
1114 ep = ep_from_link(e);
1117 delta = timespec_sub(ts, ep->timestamp);
1118 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1119 if (ep->credit > ep->credit_ceiling)
1120 ep->credit = ep->credit_ceiling;
1122 while (ep->credit && !list_empty(&ep->urb_list)) {
1123 urbl = list_first_entry(&ep->urb_list,
1124 struct oz_urb_link, link);
1126 if ((ep->credit + 1) < urb->number_of_packets)
1128 ep->credit -= urb->number_of_packets;
1131 list_move_tail(&urbl->link, &xfr_list);
1134 spin_unlock_bh(&ozhcd->hcd_lock);
1135 /* Send to PD and complete URBs.
1137 list_for_each_safe(e, n, &xfr_list) {
1138 urbl = container_of(e, struct oz_urb_link, link);
1141 urb->error_count = 0;
1142 urb->start_frame = oz_usb_get_frame_number();
1143 oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
1144 oz_free_urb_link(urbl);
1145 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1147 /* Check the IN isoc endpoints to see if any URBs can be completed.
1149 spin_lock_bh(&ozhcd->hcd_lock);
1150 list_for_each(e, &port->isoc_in_ep) {
1151 struct oz_endpoint *ep = ep_from_link(e);
1153 if (ep->flags & OZ_F_EP_BUFFERING) {
1154 if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
1155 ep->flags &= ~OZ_F_EP_BUFFERING;
1158 ep->start_frame = 0;
1162 delta = timespec_sub(ts, ep->timestamp);
1163 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1165 while (!list_empty(&ep->urb_list)) {
1166 struct oz_urb_link *urbl =
1167 list_first_entry(&ep->urb_list,
1168 struct oz_urb_link, link);
1169 struct urb *urb = urbl->urb;
1174 if (ep->credit < urb->number_of_packets)
1176 if (ep->buffered_units < urb->number_of_packets)
1178 urb->actual_length = 0;
1179 for (i = 0; i < urb->number_of_packets; i++) {
1180 len = ep->buffer[ep->out_ix];
1181 if (++ep->out_ix == ep->buffer_size)
1183 copy_len = ep->buffer_size - ep->out_ix;
1186 memcpy(urb->transfer_buffer,
1187 &ep->buffer[ep->out_ix], copy_len);
1188 if (copy_len < len) {
1189 memcpy(urb->transfer_buffer+copy_len,
1190 ep->buffer, len-copy_len);
1191 ep->out_ix = len-copy_len;
1193 ep->out_ix += copy_len;
1194 if (ep->out_ix == ep->buffer_size)
1196 urb->iso_frame_desc[i].offset =
1198 urb->actual_length += len;
1199 urb->iso_frame_desc[i].actual_length = len;
1200 urb->iso_frame_desc[i].status = 0;
1202 ep->buffered_units -= urb->number_of_packets;
1203 urb->error_count = 0;
1204 urb->start_frame = ep->start_frame;
1205 ep->start_frame += urb->number_of_packets;
1206 list_move_tail(&urbl->link, &xfr_list);
1207 ep->credit -= urb->number_of_packets;
1210 if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1212 spin_unlock_bh(&ozhcd->hcd_lock);
1213 /* Complete the filled URBs.
1215 list_for_each_safe(e, n, &xfr_list) {
1216 urbl = container_of(e, struct oz_urb_link, link);
1219 oz_free_urb_link(urbl);
1220 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1222 /* Check if there are any ep0 requests that have timed out.
1223 * If so resent to PD.
1225 ep = port->out_ep[0];
1227 struct list_head *e;
1228 struct list_head *n;
1230 spin_lock_bh(&ozhcd->hcd_lock);
1231 list_for_each_safe(e, n, &ep->urb_list) {
1232 urbl = container_of(e, struct oz_urb_link, link);
1233 if (urbl->submit_counter > EP0_TIMEOUT_COUNTER) {
1234 oz_dbg(ON, "Request 0x%p timeout\n", urbl->urb);
1235 list_move_tail(e, &xfr_list);
1236 urbl->submit_counter = 0;
1238 urbl->submit_counter++;
1241 if (!list_empty(&ep->urb_list))
1243 spin_unlock_bh(&ozhcd->hcd_lock);
1245 while (e != &xfr_list) {
1246 urbl = container_of(e, struct oz_urb_link, link);
1248 oz_dbg(ON, "Resending request to PD\n");
1249 oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1250 oz_free_urb_link(urbl);
1259 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1260 struct oz_port *port,
1261 struct usb_host_interface *intf, gfp_t mem_flags)
1263 struct oz_hcd *ozhcd = port->ozhcd;
1265 int if_ix = intf->desc.bInterfaceNumber;
1266 int request_heartbeat = 0;
1268 oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
1269 if (if_ix >= port->num_iface || port->iface == NULL)
1271 for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1272 struct usb_host_endpoint *hep = &intf->endpoint[i];
1273 u8 ep_addr = hep->desc.bEndpointAddress;
1274 u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1275 struct oz_endpoint *ep;
1276 int buffer_size = 0;
1278 oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
1279 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1280 switch (hep->desc.bmAttributes &
1281 USB_ENDPOINT_XFERTYPE_MASK) {
1282 case USB_ENDPOINT_XFER_ISOC:
1283 buffer_size = OZ_EP_BUFFER_SIZE_ISOC;
1285 case USB_ENDPOINT_XFER_INT:
1286 buffer_size = OZ_EP_BUFFER_SIZE_INT;
1291 ep = oz_ep_alloc(buffer_size, mem_flags);
1293 oz_clean_endpoints_for_interface(hcd, port, if_ix);
1296 ep->attrib = hep->desc.bmAttributes;
1297 ep->ep_num = ep_num;
1298 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1299 == USB_ENDPOINT_XFER_ISOC) {
1300 oz_dbg(ON, "wMaxPacketSize = %d\n",
1301 usb_endpoint_maxp(&hep->desc));
1302 ep->credit_ceiling = 200;
1303 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1304 ep->flags |= OZ_F_EP_BUFFERING;
1306 ep->flags |= OZ_F_EP_HAVE_STREAM;
1307 if (oz_usb_stream_create(port->hpd, ep_num))
1308 ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1311 spin_lock_bh(&ozhcd->hcd_lock);
1312 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1313 port->in_ep[ep_num] = ep;
1314 port->iface[if_ix].ep_mask |=
1315 (1<<(ep_num+OZ_NB_ENDPOINTS));
1316 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1317 == USB_ENDPOINT_XFER_ISOC) {
1318 list_add_tail(&ep->link, &port->isoc_in_ep);
1319 request_heartbeat = 1;
1322 port->out_ep[ep_num] = ep;
1323 port->iface[if_ix].ep_mask |= (1<<ep_num);
1324 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1325 == USB_ENDPOINT_XFER_ISOC) {
1326 list_add_tail(&ep->link, &port->isoc_out_ep);
1327 request_heartbeat = 1;
1330 spin_unlock_bh(&ozhcd->hcd_lock);
1331 if (request_heartbeat && port->hpd)
1332 oz_usb_request_heartbeat(port->hpd);
1340 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1341 struct oz_port *port, int if_ix)
1343 struct oz_hcd *ozhcd = port->ozhcd;
1346 struct list_head ep_list;
1348 oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
1349 if (if_ix >= port->num_iface)
1351 INIT_LIST_HEAD(&ep_list);
1352 spin_lock_bh(&ozhcd->hcd_lock);
1353 mask = port->iface[if_ix].ep_mask;
1354 port->iface[if_ix].ep_mask = 0;
1355 for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1356 struct list_head *e;
1357 /* Gather OUT endpoints.
1359 if ((mask & (1<<i)) && port->out_ep[i]) {
1360 e = &port->out_ep[i]->link;
1361 port->out_ep[i] = NULL;
1362 /* Remove from isoc list if present.
1364 list_move_tail(e, &ep_list);
1366 /* Gather IN endpoints.
1368 if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1369 e = &port->in_ep[i]->link;
1370 port->in_ep[i] = NULL;
1371 list_move_tail(e, &ep_list);
1374 spin_unlock_bh(&ozhcd->hcd_lock);
1375 while (!list_empty(&ep_list)) {
1376 struct oz_endpoint *ep =
1377 list_first_entry(&ep_list, struct oz_endpoint, link);
1378 list_del_init(&ep->link);
1379 oz_ep_free(port, ep);
1386 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1387 struct oz_port *port, struct usb_host_config *config,
1390 struct oz_hcd *ozhcd = port->ozhcd;
1392 int num_iface = config->desc.bNumInterfaces;
1395 struct oz_interface *iface;
1397 iface = kmalloc(num_iface*sizeof(struct oz_interface),
1398 mem_flags | __GFP_ZERO);
1401 spin_lock_bh(&ozhcd->hcd_lock);
1402 port->iface = iface;
1403 port->num_iface = num_iface;
1404 spin_unlock_bh(&ozhcd->hcd_lock);
1406 for (i = 0; i < num_iface; i++) {
1407 struct usb_host_interface *intf =
1408 &config->intf_cache[i]->altsetting[0];
1409 if (oz_build_endpoints_for_interface(hcd, port, intf,
1415 oz_clean_endpoints_for_config(hcd, port);
1422 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1423 struct oz_port *port)
1425 struct oz_hcd *ozhcd = port->ozhcd;
1428 oz_dbg(ON, "Deleting endpoints for configuration\n");
1429 for (i = 0; i < port->num_iface; i++)
1430 oz_clean_endpoints_for_interface(hcd, port, i);
1431 spin_lock_bh(&ozhcd->hcd_lock);
1433 oz_dbg(ON, "Freeing interfaces object\n");
1437 port->num_iface = 0;
1438 spin_unlock_bh(&ozhcd->hcd_lock);
1444 static void *oz_claim_hpd(struct oz_port *port)
1447 struct oz_hcd *ozhcd = port->ozhcd;
1449 spin_lock_bh(&ozhcd->hcd_lock);
1453 spin_unlock_bh(&ozhcd->hcd_lock);
1460 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1463 struct usb_ctrlrequest *setup;
1470 unsigned complete = 0;
1473 struct oz_port *port = NULL;
1475 oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
1476 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1481 port = &ozhcd->ports[port_ix];
1482 if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1483 || (port->flags & OZ_PORT_F_DYING)) {
1484 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1485 port_ix, urb->dev->devnum);
1489 /* Store port in private context data.
1492 setup = (struct usb_ctrlrequest *)urb->setup_packet;
1493 windex = le16_to_cpu(setup->wIndex);
1494 wvalue = le16_to_cpu(setup->wValue);
1495 wlength = le16_to_cpu(setup->wLength);
1496 oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
1497 oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1498 oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
1499 oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
1500 oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
1502 req_id = port->next_req_id++;
1503 hpd = oz_claim_hpd(port);
1505 oz_dbg(ON, "Cannot claim port\n");
1510 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1511 /* Standard requests
1513 switch (setup->bRequest) {
1514 case USB_REQ_GET_DESCRIPTOR:
1515 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
1517 case USB_REQ_SET_ADDRESS:
1518 oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
1519 oz_dbg(ON, "Port %d address is 0x%x\n",
1521 (u8)le16_to_cpu(setup->wValue));
1522 spin_lock_bh(&ozhcd->hcd_lock);
1523 if (ozhcd->conn_port >= 0) {
1524 ozhcd->ports[ozhcd->conn_port].bus_addr =
1525 (u8)le16_to_cpu(setup->wValue);
1526 oz_dbg(ON, "Clearing conn_port\n");
1527 ozhcd->conn_port = -1;
1529 spin_unlock_bh(&ozhcd->hcd_lock);
1532 case USB_REQ_SET_CONFIGURATION:
1533 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
1535 case USB_REQ_GET_CONFIGURATION:
1536 /* We short circuit this case and reply directly since
1537 * we have the selected configuration number cached.
1539 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
1540 if (urb->transfer_buffer_length >= 1) {
1541 urb->actual_length = 1;
1542 *((u8 *)urb->transfer_buffer) =
1549 case USB_REQ_GET_INTERFACE:
1550 /* We short circuit this case and reply directly since
1551 * we have the selected interface alternative cached.
1553 oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
1554 if (urb->transfer_buffer_length >= 1) {
1555 urb->actual_length = 1;
1556 *((u8 *)urb->transfer_buffer) =
1557 port->iface[(u8)windex].alt;
1558 oz_dbg(ON, "interface = %d alt = %d\n",
1559 windex, port->iface[(u8)windex].alt);
1565 case USB_REQ_SET_INTERFACE:
1566 oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
1570 if (!rc && !complete) {
1573 if ((setup->bRequestType & USB_DIR_IN) == 0)
1575 urb->actual_length = data_len;
1576 if (oz_usb_control_req(port->hpd, req_id, setup,
1577 urb->transfer_buffer, data_len)) {
1580 /* Note: we are queuing the request after we have
1581 * submitted it to be transmitted. If the request were
1582 * to complete before we queued it then it would not
1583 * be found in the queue. It seems impossible for
1584 * this to happen but if it did the request would
1585 * be resubmitted so the problem would hopefully
1586 * resolve itself. Putting the request into the
1587 * queue before it has been sent is worse since the
1588 * urb could be cancelled while we are using it
1589 * to build the request.
1591 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1597 if (rc || complete) {
1598 oz_dbg(ON, "Completing request locally\n");
1599 oz_complete_urb(ozhcd->hcd, urb, rc);
1601 oz_usb_request_heartbeat(port->hpd);
1608 static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1611 struct oz_port *port = urb->hcpriv;
1614 /* When we are paranoid we keep a list of urbs which we check against
1615 * before handing one back. This is just for debugging during
1616 * development and should be turned off in the released driver.
1618 oz_remember_urb(urb);
1619 /* Check buffer is valid.
1621 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1623 /* Check if there is a device at the port - refuse if not.
1625 if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1627 ep_addr = usb_pipeendpoint(urb->pipe);
1629 /* If the request is not for EP0 then queue it.
1631 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1635 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1643 static void oz_urb_process_tasklet(unsigned long unused)
1645 unsigned long irq_state;
1647 struct oz_hcd *ozhcd = oz_hcd_claim();
1652 /* This is called from a tasklet so is in softirq context but the urb
1653 * list is filled from any context so we need to lock
1654 * appropriately while removing urbs.
1656 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1657 while (!list_empty(&ozhcd->urb_pending_list)) {
1658 struct oz_urb_link *urbl =
1659 list_first_entry(&ozhcd->urb_pending_list,
1660 struct oz_urb_link, link);
1661 list_del_init(&urbl->link);
1662 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1664 oz_free_urb_link(urbl);
1665 rc = oz_urb_process(ozhcd, urb);
1667 oz_complete_urb(ozhcd->hcd, urb, rc);
1668 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1670 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1675 * This function searches for the urb in any of the lists it could be in.
1676 * If it is found it is removed from the list and completed. If the urb is
1677 * being processed then it won't be in a list so won't be found. However, the
1678 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1679 * to a non-zero value. When an attempt is made to put the urb back in a list
1680 * the unlinked field will be checked and the urb will then be completed.
1683 static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1685 struct oz_urb_link *urbl = NULL;
1686 struct list_head *e;
1687 struct oz_hcd *ozhcd;
1688 unsigned long irq_state;
1692 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
1695 ozhcd = port->ozhcd;
1696 if (ozhcd == NULL) {
1697 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
1701 /* Look in the tasklet queue.
1703 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1704 list_for_each(e, &ozhcd->urb_cancel_list) {
1705 urbl = container_of(e, struct oz_urb_link, link);
1706 if (urb == urbl->urb) {
1708 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1712 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1715 /* Look in the orphanage.
1717 spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1718 list_for_each(e, &ozhcd->orphanage) {
1719 urbl = container_of(e, struct oz_urb_link, link);
1720 if (urbl->urb == urb) {
1722 oz_dbg(ON, "Found urb in orphanage\n");
1726 ix = (ep_num & 0xf);
1728 if ((ep_num & USB_DIR_IN) && ix)
1729 urbl = oz_remove_urb(port->in_ep[ix], urb);
1731 urbl = oz_remove_urb(port->out_ep[ix], urb);
1733 spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1736 urb->actual_length = 0;
1737 oz_free_urb_link(urbl);
1738 oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
1745 static void oz_urb_cancel_tasklet(unsigned long unused)
1747 unsigned long irq_state;
1749 struct oz_hcd *ozhcd = oz_hcd_claim();
1753 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1754 while (!list_empty(&ozhcd->urb_cancel_list)) {
1755 struct oz_urb_link *urbl =
1756 list_first_entry(&ozhcd->urb_cancel_list,
1757 struct oz_urb_link, link);
1758 list_del_init(&urbl->link);
1759 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1762 oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1763 oz_free_urb_link(urbl);
1764 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1766 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1773 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1776 struct oz_urb_link *urbl;
1778 while (!list_empty(&ozhcd->orphanage)) {
1779 urbl = list_first_entry(&ozhcd->orphanage,
1780 struct oz_urb_link, link);
1781 list_del(&urbl->link);
1782 oz_complete_urb(ozhcd->hcd, urbl->urb, status);
1783 oz_free_urb_link(urbl);
1791 static int oz_hcd_start(struct usb_hcd *hcd)
1793 hcd->power_budget = 200;
1794 hcd->state = HC_STATE_RUNNING;
1795 hcd->uses_new_polling = 1;
1802 static void oz_hcd_stop(struct usb_hcd *hcd)
1809 static void oz_hcd_shutdown(struct usb_hcd *hcd)
1814 * Called to queue an urb for the device.
1815 * This function should return a non-zero error code if it fails the urb but
1816 * should not call usb_hcd_giveback_urb().
1819 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1822 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1825 struct oz_port *port;
1826 unsigned long irq_state;
1827 struct oz_urb_link *urbl;
1829 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1830 if (unlikely(ozhcd == NULL)) {
1831 oz_dbg(URB, "Refused urb(%p) not ozhcd\n", urb);
1834 if (unlikely(hcd->state != HC_STATE_RUNNING)) {
1835 oz_dbg(URB, "Refused urb(%p) not running\n", urb);
1838 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1841 port = &ozhcd->ports[port_ix];
1844 if (!(port->flags & OZ_PORT_F_PRESENT) ||
1845 (port->flags & OZ_PORT_F_CHANGED)) {
1846 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1847 port_ix, urb->dev->devnum);
1851 /* Put request in queue for processing by tasklet.
1853 urbl = oz_alloc_urb_link();
1854 if (unlikely(urbl == NULL))
1857 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1858 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1860 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1861 oz_free_urb_link(urbl);
1864 list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1865 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1866 tasklet_schedule(&g_urb_process_tasklet);
1867 atomic_inc(&g_pending_urbs);
1874 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1877 struct oz_urb_link *urbl;
1878 struct list_head *e;
1880 if (unlikely(ep == NULL))
1882 list_for_each(e, &ep->urb_list) {
1883 urbl = container_of(e, struct oz_urb_link, link);
1884 if (urbl->urb == urb) {
1886 if (usb_pipeisoc(urb->pipe)) {
1887 ep->credit -= urb->number_of_packets;
1898 * Called to dequeue a previously submitted urb for the device.
1901 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1903 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1904 struct oz_urb_link *urbl;
1906 unsigned long irq_state;
1908 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1909 urbl = oz_alloc_urb_link();
1910 if (unlikely(urbl == NULL))
1912 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1913 /* The following function checks the urb is still in the queue
1914 * maintained by the core and that the unlinked field is zero.
1915 * If both are true the function sets the unlinked field and returns
1916 * zero. Otherwise it returns an error.
1918 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1919 /* We have to check we haven't completed the urb or are about
1920 * to complete it. When we do we set hcpriv to 0 so if this has
1921 * already happened we don't put the urb in the cancel queue.
1923 if ((rc == 0) && urb->hcpriv) {
1925 urbl->port = (struct oz_port *)urb->hcpriv;
1926 urbl->ep_num = usb_pipeendpoint(urb->pipe);
1927 if (usb_pipein(urb->pipe))
1928 urbl->ep_num |= USB_DIR_IN;
1929 list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1930 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1931 tasklet_schedule(&g_urb_cancel_tasklet);
1933 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1934 oz_free_urb_link(urbl);
1942 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1943 struct usb_host_endpoint *ep)
1950 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1951 struct usb_host_endpoint *ep)
1958 static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1960 oz_dbg(ON, "oz_hcd_get_frame_number\n");
1961 return oz_usb_get_frame_number();
1966 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1967 * always do that in softirq context.
1969 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1971 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1977 spin_lock_bh(&ozhcd->hcd_lock);
1978 for (i = 0; i < OZ_NB_PORTS; i++) {
1979 if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
1980 oz_dbg(HUB, "Port %d changed\n", i);
1981 ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
1983 buf[0] |= 1 << (i + 1);
1985 buf[1] |= 1 << (i - 7);
1988 spin_unlock_bh(&ozhcd->hcd_lock);
1989 if (buf[0] != 0 || buf[1] != 0)
1998 static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1999 struct usb_hub_descriptor *desc)
2001 memset(desc, 0, sizeof(*desc));
2002 desc->bDescriptorType = 0x29;
2003 desc->bDescLength = 9;
2004 desc->wHubCharacteristics = (__force __u16)cpu_to_le16(0x0001);
2005 desc->bNbrPorts = OZ_NB_PORTS;
2011 static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
2013 struct oz_port *port;
2014 u8 port_id = (u8)windex;
2015 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2016 unsigned set_bits = 0;
2017 unsigned clear_bits = 0;
2019 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2021 port = &ozhcd->ports[port_id-1];
2023 case USB_PORT_FEAT_CONNECTION:
2024 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
2026 case USB_PORT_FEAT_ENABLE:
2027 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
2029 case USB_PORT_FEAT_SUSPEND:
2030 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
2032 case USB_PORT_FEAT_OVER_CURRENT:
2033 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2035 case USB_PORT_FEAT_RESET:
2036 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
2037 set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
2038 clear_bits = USB_PORT_STAT_RESET;
2039 ozhcd->ports[port_id-1].bus_addr = 0;
2041 case USB_PORT_FEAT_POWER:
2042 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2043 set_bits |= USB_PORT_STAT_POWER;
2045 case USB_PORT_FEAT_LOWSPEED:
2046 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2048 case USB_PORT_FEAT_C_CONNECTION:
2049 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2051 case USB_PORT_FEAT_C_ENABLE:
2052 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2054 case USB_PORT_FEAT_C_SUSPEND:
2055 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2057 case USB_PORT_FEAT_C_OVER_CURRENT:
2058 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2060 case USB_PORT_FEAT_C_RESET:
2061 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2063 case USB_PORT_FEAT_TEST:
2064 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2066 case USB_PORT_FEAT_INDICATOR:
2067 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2070 oz_dbg(HUB, "Other %d\n", wvalue);
2073 if (set_bits || clear_bits) {
2074 spin_lock_bh(&port->port_lock);
2075 port->status &= ~clear_bits;
2076 port->status |= set_bits;
2077 spin_unlock_bh(&port->port_lock);
2079 oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
2086 static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
2088 struct oz_port *port;
2089 u8 port_id = (u8)windex;
2090 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2091 unsigned clear_bits = 0;
2093 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2095 port = &ozhcd->ports[port_id-1];
2097 case USB_PORT_FEAT_CONNECTION:
2098 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
2100 case USB_PORT_FEAT_ENABLE:
2101 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
2102 clear_bits = USB_PORT_STAT_ENABLE;
2104 case USB_PORT_FEAT_SUSPEND:
2105 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
2107 case USB_PORT_FEAT_OVER_CURRENT:
2108 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2110 case USB_PORT_FEAT_RESET:
2111 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
2113 case USB_PORT_FEAT_POWER:
2114 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2115 clear_bits |= USB_PORT_STAT_POWER;
2117 case USB_PORT_FEAT_LOWSPEED:
2118 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2120 case USB_PORT_FEAT_C_CONNECTION:
2121 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2122 clear_bits = (USB_PORT_STAT_C_CONNECTION << 16);
2124 case USB_PORT_FEAT_C_ENABLE:
2125 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2126 clear_bits = (USB_PORT_STAT_C_ENABLE << 16);
2128 case USB_PORT_FEAT_C_SUSPEND:
2129 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2131 case USB_PORT_FEAT_C_OVER_CURRENT:
2132 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2134 case USB_PORT_FEAT_C_RESET:
2135 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2136 clear_bits = (USB_PORT_FEAT_C_RESET << 16);
2138 case USB_PORT_FEAT_TEST:
2139 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2141 case USB_PORT_FEAT_INDICATOR:
2142 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2145 oz_dbg(HUB, "Other %d\n", wvalue);
2149 spin_lock_bh(&port->port_lock);
2150 port->status &= ~clear_bits;
2151 spin_unlock_bh(&port->port_lock);
2153 oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2154 port_id, ozhcd->ports[port_id-1].status);
2161 static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2163 struct oz_hcd *ozhcd;
2166 if ((windex < 1) || (windex > OZ_NB_PORTS))
2168 ozhcd = oz_hcd_private(hcd);
2169 oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
2170 status = ozhcd->ports[windex-1].status;
2171 put_unaligned(cpu_to_le32(status), (__le32 *)buf);
2172 oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
2179 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2180 u16 windex, char *buf, u16 wlength)
2185 case ClearHubFeature:
2186 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
2188 case ClearPortFeature:
2189 err = oz_clear_port_feature(hcd, wvalue, windex);
2191 case GetHubDescriptor:
2192 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2195 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
2196 put_unaligned(cpu_to_le32(0), (__le32 *)buf);
2199 err = oz_get_port_status(hcd, windex, buf);
2202 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
2204 case SetPortFeature:
2205 err = oz_set_port_feature(hcd, wvalue, windex);
2208 oz_dbg(HUB, "Other: %d\n", req_type);
2217 static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2219 struct oz_hcd *ozhcd;
2221 ozhcd = oz_hcd_private(hcd);
2222 spin_lock_bh(&ozhcd->hcd_lock);
2223 hcd->state = HC_STATE_SUSPENDED;
2224 ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2225 spin_unlock_bh(&ozhcd->hcd_lock);
2232 static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2234 struct oz_hcd *ozhcd;
2236 ozhcd = oz_hcd_private(hcd);
2237 spin_lock_bh(&ozhcd->hcd_lock);
2238 ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2239 hcd->state = HC_STATE_RUNNING;
2240 spin_unlock_bh(&ozhcd->hcd_lock);
2244 static void oz_plat_shutdown(struct platform_device *dev)
2251 static int oz_plat_probe(struct platform_device *dev)
2255 struct usb_hcd *hcd;
2256 struct oz_hcd *ozhcd;
2258 hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
2260 oz_dbg(ON, "Failed to created hcd object OK\n");
2263 ozhcd = oz_hcd_private(hcd);
2264 memset(ozhcd, 0, sizeof(*ozhcd));
2265 INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2266 INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2267 INIT_LIST_HEAD(&ozhcd->orphanage);
2269 ozhcd->conn_port = -1;
2270 spin_lock_init(&ozhcd->hcd_lock);
2271 for (i = 0; i < OZ_NB_PORTS; i++) {
2272 struct oz_port *port = &ozhcd->ports[i];
2274 port->ozhcd = ozhcd;
2277 port->bus_addr = 0xff;
2278 spin_lock_init(&port->port_lock);
2280 err = usb_add_hcd(hcd, 0, 0);
2282 oz_dbg(ON, "Failed to add hcd object OK\n");
2286 device_wakeup_enable(hcd->self.controller);
2288 spin_lock_bh(&g_hcdlock);
2290 spin_unlock_bh(&g_hcdlock);
2297 static int oz_plat_remove(struct platform_device *dev)
2299 struct usb_hcd *hcd = platform_get_drvdata(dev);
2300 struct oz_hcd *ozhcd;
2304 ozhcd = oz_hcd_private(hcd);
2305 spin_lock_bh(&g_hcdlock);
2306 if (ozhcd == g_ozhcd)
2308 spin_unlock_bh(&g_hcdlock);
2309 oz_dbg(ON, "Clearing orphanage\n");
2310 oz_hcd_clear_orphanage(ozhcd, -EPIPE);
2311 oz_dbg(ON, "Removing hcd\n");
2312 usb_remove_hcd(hcd);
2314 oz_empty_link_pool();
2321 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2330 static int oz_plat_resume(struct platform_device *dev)
2338 int oz_hcd_init(void)
2344 tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
2345 tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
2346 err = platform_driver_register(&g_oz_plat_drv);
2347 oz_dbg(ON, "platform_driver_register() returned %d\n", err);
2350 g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
2351 if (g_plat_dev == NULL) {
2355 oz_dbg(ON, "platform_device_alloc() succeeded\n");
2356 err = platform_device_add(g_plat_dev);
2359 oz_dbg(ON, "platform_device_add() succeeded\n");
2362 platform_device_put(g_plat_dev);
2364 platform_driver_unregister(&g_oz_plat_drv);
2366 tasklet_disable(&g_urb_process_tasklet);
2367 tasklet_disable(&g_urb_cancel_tasklet);
2368 oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
2375 void oz_hcd_term(void)
2377 msleep(OZ_HUB_DEBOUNCE_TIMEOUT);
2378 tasklet_kill(&g_urb_process_tasklet);
2379 tasklet_kill(&g_urb_cancel_tasklet);
2380 platform_device_unregister(g_plat_dev);
2381 platform_driver_unregister(&g_oz_plat_drv);
2382 oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));