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 /*EP0 timeout before ep0 request is again added to TX queue. (13*8 = 98mSec)
50 #define EP0_TIMEOUT_COUNTER 13
52 /* Debounce time HCD driver should wait before unregistering.
54 #define OZ_HUB_DEBOUNCE_TIMEOUT 1500
57 * Used to link urbs together and also store some status information for each
59 * A cache of these are kept in a pool to reduce number of calls to kmalloc.
62 struct list_head link;
67 unsigned submit_counter;
70 static struct kmem_cache *oz_urb_link_cache;
72 /* Holds state information about a USB endpoint.
74 #define OZ_EP_BUFFER_SIZE_ISOC (1024 * 24)
75 #define OZ_EP_BUFFER_SIZE_INT 512
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
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);
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 DEFINE_SPINLOCK(g_tasklet_lock);
196 static struct tasklet_struct g_urb_process_tasklet;
197 static struct tasklet_struct g_urb_cancel_tasklet;
198 static atomic_t g_pending_urbs = ATOMIC_INIT(0);
199 static atomic_t g_usb_frame_number = ATOMIC_INIT(0);
200 static const struct hc_driver g_oz_hc_drv = {
201 .description = g_hcd_name,
202 .product_desc = "Ozmo Devices WPAN",
203 .hcd_priv_size = sizeof(struct oz_hcd),
205 .start = oz_hcd_start,
207 .shutdown = oz_hcd_shutdown,
208 .urb_enqueue = oz_hcd_urb_enqueue,
209 .urb_dequeue = oz_hcd_urb_dequeue,
210 .endpoint_disable = oz_hcd_endpoint_disable,
211 .endpoint_reset = oz_hcd_endpoint_reset,
212 .get_frame_number = oz_hcd_get_frame_number,
213 .hub_status_data = oz_hcd_hub_status_data,
214 .hub_control = oz_hcd_hub_control,
215 .bus_suspend = oz_hcd_bus_suspend,
216 .bus_resume = oz_hcd_bus_resume,
219 static struct platform_driver g_oz_plat_drv = {
220 .probe = oz_plat_probe,
221 .remove = oz_plat_remove,
222 .shutdown = oz_plat_shutdown,
223 .suspend = oz_plat_suspend,
224 .resume = oz_plat_resume,
226 .name = OZ_PLAT_DEV_NAME,
231 * Gets our private context area (which is of type struct oz_hcd) from the
235 static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
237 return (struct oz_hcd *)hcd->hcd_priv;
241 * Searches list of ports to find the index of the one with a specified USB
242 * bus address. If none of the ports has the bus address then the connection
243 * port is returned, if there is one or -1 otherwise.
246 static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
250 for (i = 0; i < OZ_NB_PORTS; i++) {
251 if (ozhcd->ports[i].bus_addr == bus_addr)
254 return ozhcd->conn_port;
260 static struct oz_urb_link *oz_alloc_urb_link(void)
262 return kmem_cache_alloc(oz_urb_link_cache, GFP_ATOMIC);
268 static void oz_free_urb_link(struct oz_urb_link *urbl)
273 kmem_cache_free(oz_urb_link_cache, urbl);
277 * Allocates endpoint structure and optionally a buffer. If a buffer is
278 * allocated it immediately follows the endpoint structure.
281 static struct oz_endpoint *oz_ep_alloc(int buffer_size, gfp_t mem_flags)
283 struct oz_endpoint *ep =
284 kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
286 INIT_LIST_HEAD(&ep->urb_list);
287 INIT_LIST_HEAD(&ep->link);
290 ep->buffer_size = buffer_size;
291 ep->buffer = (u8 *)(ep+1);
298 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
300 * Context: softirq or process
302 static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd,
305 struct oz_urb_link *urbl;
307 list_for_each_entry(urbl, &ozhcd->urb_cancel_list, link) {
308 if (urb == urbl->urb) {
309 list_del_init(&urbl->link);
317 * This is called when we have finished processing an urb. It unlinks it from
318 * the ep and returns it to the core.
319 * Context: softirq or process
321 static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
324 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
325 unsigned long irq_state;
326 struct oz_urb_link *cancel_urbl;
328 spin_lock_irqsave(&g_tasklet_lock, irq_state);
329 usb_hcd_unlink_urb_from_ep(hcd, urb);
330 /* Clear hcpriv which will prevent it being put in the cancel list
331 * in the event that an attempt is made to cancel it.
334 /* Walk the cancel list in case the urb is already sitting there.
335 * Since we process the cancel list in a tasklet rather than in
336 * the dequeue function this could happen.
338 cancel_urbl = oz_uncancel_urb(ozhcd, urb);
339 /* Note: we release lock but do not enable local irqs.
340 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
341 * or at least other host controllers disable interrupts at this point
342 * so we do the same. We must, however, release the lock otherwise a
343 * deadlock will occur if an urb is submitted to our driver in the urb
344 * completion function. Because we disable interrupts it is possible
345 * that the urb_enqueue function can be called with them disabled.
347 spin_unlock(&g_tasklet_lock);
348 if (oz_forget_urb(urb)) {
349 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
351 atomic_dec(&g_pending_urbs);
352 usb_hcd_giveback_urb(hcd, urb, status);
354 spin_lock(&g_tasklet_lock);
355 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
356 oz_free_urb_link(cancel_urbl);
360 * Deallocates an endpoint including deallocating any associated stream and
361 * returning any queued urbs to the core.
364 static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
368 struct oz_hcd *ozhcd = port->ozhcd;
370 if (ep->flags & OZ_F_EP_HAVE_STREAM)
371 oz_usb_stream_delete(port->hpd, ep->ep_num);
372 /* Transfer URBs to the orphanage while we hold the lock. */
373 spin_lock_bh(&ozhcd->hcd_lock);
374 /* Note: this works even if ep->urb_list is empty.*/
375 list_replace_init(&ep->urb_list, &list);
376 /* Put the URBs in the orphanage. */
377 list_splice_tail(&list, &ozhcd->orphanage);
378 spin_unlock_bh(&ozhcd->hcd_lock);
380 oz_dbg(ON, "Freeing endpoint memory\n");
387 static void oz_complete_buffered_urb(struct oz_port *port,
388 struct oz_endpoint *ep,
391 int data_len, available_space, copy_len;
393 data_len = ep->buffer[ep->out_ix];
394 if (data_len <= urb->transfer_buffer_length)
395 available_space = data_len;
397 available_space = urb->transfer_buffer_length;
399 if (++ep->out_ix == ep->buffer_size)
401 copy_len = ep->buffer_size - ep->out_ix;
402 if (copy_len >= available_space)
403 copy_len = available_space;
404 memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
406 if (copy_len < available_space) {
407 memcpy((urb->transfer_buffer + copy_len), ep->buffer,
408 (available_space - copy_len));
409 ep->out_ix = available_space - copy_len;
411 ep->out_ix += copy_len;
413 urb->actual_length = available_space;
414 if (ep->out_ix == ep->buffer_size)
417 ep->buffered_units--;
418 oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
420 oz_complete_urb(port->ozhcd->hcd, urb, 0);
426 static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
427 struct urb *urb, u8 req_id)
429 struct oz_urb_link *urbl;
430 struct oz_endpoint *ep = NULL;
433 if (ep_addr >= OZ_NB_ENDPOINTS) {
434 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
437 urbl = oz_alloc_urb_link();
440 urbl->submit_counter = 0;
442 urbl->req_id = req_id;
443 urbl->ep_num = ep_addr;
444 /* Hold lock while we insert the URB into the list within the
445 * endpoint structure.
447 spin_lock_bh(&port->ozhcd->hcd_lock);
448 /* If the urb has been unlinked while out of any list then
452 spin_unlock_bh(&port->ozhcd->hcd_lock);
453 oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
454 oz_complete_urb(port->ozhcd->hcd, urb, 0);
455 oz_free_urb_link(urbl);
460 ep = port->in_ep[ep_addr];
462 ep = port->out_ep[ep_addr];
468 /*For interrupt endpoint check for buffered data
471 if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
472 && ep->buffered_units > 0) {
473 oz_free_urb_link(urbl);
474 spin_unlock_bh(&port->ozhcd->hcd_lock);
475 oz_complete_buffered_urb(port, ep, urb);
480 list_add_tail(&urbl->link, &ep->urb_list);
481 if (!in_dir && ep_addr && (ep->credit < 0)) {
482 getrawmonotonic(&ep->timestamp);
489 spin_unlock_bh(&port->ozhcd->hcd_lock);
491 oz_free_urb_link(urbl);
496 * Removes an urb from the queue in the endpoint.
497 * Returns 0 if it is found and -EIDRM otherwise.
500 static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
503 struct oz_urb_link *urbl = NULL;
504 struct oz_endpoint *ep;
506 spin_lock_bh(&port->ozhcd->hcd_lock);
508 ep = port->in_ep[ep_addr];
510 ep = port->out_ep[ep_addr];
514 list_for_each(e, &ep->urb_list) {
515 urbl = list_entry(e, struct oz_urb_link, link);
516 if (urbl->urb == urb) {
523 spin_unlock_bh(&port->ozhcd->hcd_lock);
524 oz_free_urb_link(urbl);
525 return urbl ? 0 : -EIDRM;
529 * Finds an urb given its request id.
532 static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
535 struct oz_hcd *ozhcd = port->ozhcd;
536 struct urb *urb = NULL;
537 struct oz_urb_link *urbl;
538 struct oz_endpoint *ep;
540 spin_lock_bh(&ozhcd->hcd_lock);
541 ep = port->out_ep[ep_ix];
545 list_for_each(e, &ep->urb_list) {
546 urbl = list_entry(e, struct oz_urb_link, link);
547 if (urbl->req_id == req_id) {
554 spin_unlock_bh(&ozhcd->hcd_lock);
555 /* If urb is non-zero then we we must have an urb link to delete.
558 oz_free_urb_link(urbl);
563 * Pre-condition: Port lock must be held.
566 static void oz_acquire_port(struct oz_port *port, void *hpd)
568 INIT_LIST_HEAD(&port->isoc_out_ep);
569 INIT_LIST_HEAD(&port->isoc_in_ep);
570 port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
571 port->status |= USB_PORT_STAT_CONNECTION |
572 (USB_PORT_STAT_C_CONNECTION << 16);
580 static struct oz_hcd *oz_hcd_claim(void)
582 struct oz_hcd *ozhcd;
584 spin_lock_bh(&g_hcdlock);
587 usb_get_hcd(ozhcd->hcd);
588 spin_unlock_bh(&g_hcdlock);
595 static inline void oz_hcd_put(struct oz_hcd *ozhcd)
598 usb_put_hcd(ozhcd->hcd);
602 * This is called by the protocol handler to notify that a PD has arrived.
603 * We allocate a port to associate with the PD and create a structure for
604 * endpoint 0. This port is made the connection port.
605 * In the event that one of the other port is already a connection port then
607 * TODO We should be able to do better than fail and should be able remember
608 * that this port needs configuring and make it the connection port once the
609 * current connection port has been assigned an address. Collisions here are
610 * probably very rare indeed.
613 struct oz_port *oz_hcd_pd_arrived(void *hpd)
616 struct oz_port *hport;
617 struct oz_hcd *ozhcd;
618 struct oz_endpoint *ep;
620 ozhcd = oz_hcd_claim();
623 /* Allocate an endpoint object in advance (before holding hcd lock) to
624 * use for out endpoint 0.
626 ep = oz_ep_alloc(0, GFP_ATOMIC);
630 spin_lock_bh(&ozhcd->hcd_lock);
631 if (ozhcd->conn_port >= 0)
634 for (i = 0; i < OZ_NB_PORTS; i++) {
635 struct oz_port *port = &ozhcd->ports[i];
637 spin_lock(&port->port_lock);
638 if (!(port->flags & (OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED))) {
639 oz_acquire_port(port, hpd);
640 spin_unlock(&port->port_lock);
643 spin_unlock(&port->port_lock);
645 if (i == OZ_NB_PORTS)
648 ozhcd->conn_port = i;
649 hport = &ozhcd->ports[i];
650 hport->out_ep[0] = ep;
651 spin_unlock_bh(&ozhcd->hcd_lock);
652 if (ozhcd->flags & OZ_HDC_F_SUSPENDED)
653 usb_hcd_resume_root_hub(ozhcd->hcd);
654 usb_hcd_poll_rh_status(ozhcd->hcd);
660 spin_unlock_bh(&ozhcd->hcd_lock);
661 oz_ep_free(NULL, ep);
668 * This is called by the protocol handler to notify that the PD has gone away.
669 * We need to deallocate all resources and then request that the root hub is
670 * polled. We release the reference we hold on the PD.
673 void oz_hcd_pd_departed(struct oz_port *port)
675 struct oz_hcd *ozhcd;
677 struct oz_endpoint *ep = NULL;
680 oz_dbg(ON, "%s: port = 0\n", __func__);
686 /* Check if this is the connection port - if so clear it.
688 spin_lock_bh(&ozhcd->hcd_lock);
689 if ((ozhcd->conn_port >= 0) &&
690 (port == &ozhcd->ports[ozhcd->conn_port])) {
691 oz_dbg(ON, "Clearing conn_port\n");
692 ozhcd->conn_port = -1;
694 spin_lock(&port->port_lock);
695 port->flags |= OZ_PORT_F_DYING;
696 spin_unlock(&port->port_lock);
697 spin_unlock_bh(&ozhcd->hcd_lock);
699 oz_clean_endpoints_for_config(ozhcd->hcd, port);
700 spin_lock_bh(&port->port_lock);
703 port->bus_addr = 0xff;
704 port->config_num = 0;
705 port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
706 port->flags |= OZ_PORT_F_CHANGED;
707 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
708 port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
709 /* If there is an endpont 0 then clear the pointer while we hold
710 * the spinlock be we deallocate it after releasing the lock.
712 if (port->out_ep[0]) {
713 ep = port->out_ep[0];
714 port->out_ep[0] = NULL;
716 spin_unlock_bh(&port->port_lock);
718 oz_ep_free(port, ep);
719 usb_hcd_poll_rh_status(ozhcd->hcd);
726 void oz_hcd_pd_reset(void *hpd, void *hport)
728 /* Cleanup the current configuration and report reset to the core.
730 struct oz_port *port = hport;
731 struct oz_hcd *ozhcd = port->ozhcd;
733 oz_dbg(ON, "PD Reset\n");
734 spin_lock_bh(&port->port_lock);
735 port->flags |= OZ_PORT_F_CHANGED;
736 port->status |= USB_PORT_STAT_RESET;
737 port->status |= (USB_PORT_STAT_C_RESET << 16);
738 spin_unlock_bh(&port->port_lock);
739 oz_clean_endpoints_for_config(ozhcd->hcd, port);
740 usb_hcd_poll_rh_status(ozhcd->hcd);
746 void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
747 int length, int offset, int total_size)
749 struct oz_port *port = hport;
753 oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
754 length, offset, total_size);
755 urb = oz_find_urb_by_id(port, 0, req_id);
760 int required_size = urb->transfer_buffer_length;
762 if (required_size > total_size)
763 required_size = total_size;
764 copy_len = required_size-offset;
765 if (length <= copy_len)
767 memcpy(urb->transfer_buffer+offset, desc, copy_len);
769 if (offset < required_size) {
770 struct usb_ctrlrequest *setup =
771 (struct usb_ctrlrequest *)urb->setup_packet;
772 unsigned wvalue = le16_to_cpu(setup->wValue);
774 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
776 else if (oz_usb_get_desc_req(port->hpd, req_id,
777 setup->bRequestType, (u8)(wvalue>>8),
778 (u8)wvalue, setup->wIndex, offset,
779 required_size-offset)) {
780 oz_dequeue_ep_urb(port, 0, 0, urb);
787 urb->actual_length = total_size;
788 oz_complete_urb(port->ozhcd->hcd, urb, 0);
794 static void oz_display_conf_type(u8 t)
797 case USB_REQ_GET_STATUS:
798 oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
800 case USB_REQ_CLEAR_FEATURE:
801 oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
803 case USB_REQ_SET_FEATURE:
804 oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
806 case USB_REQ_SET_ADDRESS:
807 oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
809 case USB_REQ_GET_DESCRIPTOR:
810 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
812 case USB_REQ_SET_DESCRIPTOR:
813 oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
815 case USB_REQ_GET_CONFIGURATION:
816 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
818 case USB_REQ_SET_CONFIGURATION:
819 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
821 case USB_REQ_GET_INTERFACE:
822 oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
824 case USB_REQ_SET_INTERFACE:
825 oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
827 case USB_REQ_SYNCH_FRAME:
828 oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
836 static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
837 u8 rcode, u8 config_num)
840 struct usb_hcd *hcd = port->ozhcd->hcd;
843 port->config_num = config_num;
844 oz_clean_endpoints_for_config(hcd, port);
845 if (oz_build_endpoints_for_config(hcd, port,
846 &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
852 oz_complete_urb(hcd, urb, rc);
858 static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
859 u8 rcode, u8 if_num, u8 alt)
861 struct usb_hcd *hcd = port->ozhcd->hcd;
864 if ((rcode == 0) && (port->config_num > 0)) {
865 struct usb_host_config *config;
866 struct usb_host_interface *intf;
868 oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
869 oz_clean_endpoints_for_interface(hcd, port, if_num);
870 config = &urb->dev->config[port->config_num-1];
871 intf = &config->intf_cache[if_num]->altsetting[alt];
872 if (oz_build_endpoints_for_interface(hcd, port, intf,
876 port->iface[if_num].alt = alt;
880 oz_complete_urb(hcd, urb, rc);
886 void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
889 struct oz_port *port = hport;
891 struct usb_ctrlrequest *setup;
892 struct usb_hcd *hcd = port->ozhcd->hcd;
896 oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
897 urb = oz_find_urb_by_id(port, 0, req_id);
899 oz_dbg(ON, "URB not found\n");
902 setup = (struct usb_ctrlrequest *)urb->setup_packet;
903 windex = le16_to_cpu(setup->wIndex);
904 wvalue = le16_to_cpu(setup->wValue);
905 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
906 /* Standard requests */
907 oz_display_conf_type(setup->bRequest);
908 switch (setup->bRequest) {
909 case USB_REQ_SET_CONFIGURATION:
910 oz_hcd_complete_set_config(port, urb, rcode,
913 case USB_REQ_SET_INTERFACE:
914 oz_hcd_complete_set_interface(port, urb, rcode,
915 (u8)windex, (u8)wvalue);
918 oz_complete_urb(hcd, urb, 0);
924 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
926 if (data_len <= urb->transfer_buffer_length)
929 copy_len = urb->transfer_buffer_length;
930 memcpy(urb->transfer_buffer, data, copy_len);
931 urb->actual_length = copy_len;
933 oz_complete_urb(hcd, urb, 0);
938 * Context: softirq-serialized
940 static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
948 space = ep->out_ix-ep->in_ix-1;
950 space += ep->buffer_size;
951 if (space < (data_len+1)) {
952 oz_dbg(ON, "Buffer full\n");
955 ep->buffer[ep->in_ix] = (u8)data_len;
956 if (++ep->in_ix == ep->buffer_size)
958 copy_len = ep->buffer_size - ep->in_ix;
959 if (copy_len > data_len)
961 memcpy(&ep->buffer[ep->in_ix], data, copy_len);
963 if (copy_len < data_len) {
964 memcpy(ep->buffer, data+copy_len, data_len-copy_len);
965 ep->in_ix = data_len-copy_len;
967 ep->in_ix += copy_len;
969 if (ep->in_ix == ep->buffer_size)
971 ep->buffered_units++;
976 * Context: softirq-serialized
978 void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
980 struct oz_port *port = (struct oz_port *)hport;
981 struct oz_endpoint *ep;
982 struct oz_hcd *ozhcd = port->ozhcd;
984 spin_lock_bh(&ozhcd->hcd_lock);
985 ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
988 switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
989 case USB_ENDPOINT_XFER_INT:
990 case USB_ENDPOINT_XFER_BULK:
991 if (!list_empty(&ep->urb_list)) {
992 struct oz_urb_link *urbl =
993 list_first_entry(&ep->urb_list,
994 struct oz_urb_link, link);
998 list_del_init(&urbl->link);
999 spin_unlock_bh(&ozhcd->hcd_lock);
1001 oz_free_urb_link(urbl);
1002 if (data_len <= urb->transfer_buffer_length)
1003 copy_len = data_len;
1005 copy_len = urb->transfer_buffer_length;
1006 memcpy(urb->transfer_buffer, data, copy_len);
1007 urb->actual_length = copy_len;
1008 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1011 oz_dbg(ON, "buffering frame as URB is not available\n");
1012 oz_hcd_buffer_data(ep, data, data_len);
1014 case USB_ENDPOINT_XFER_ISOC:
1015 oz_hcd_buffer_data(ep, data, data_len);
1019 spin_unlock_bh(&ozhcd->hcd_lock);
1025 static inline int oz_usb_get_frame_number(void)
1027 return atomic_inc_return(&g_usb_frame_number);
1033 int oz_hcd_heartbeat(void *hport)
1036 struct oz_port *port = hport;
1037 struct oz_hcd *ozhcd = port->ozhcd;
1038 struct oz_urb_link *urbl, *n;
1039 LIST_HEAD(xfr_list);
1041 struct oz_endpoint *ep;
1042 struct timespec ts, delta;
1044 getrawmonotonic(&ts);
1045 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1047 spin_lock_bh(&ozhcd->hcd_lock);
1048 list_for_each_entry(ep, &port->isoc_out_ep, link) {
1051 delta = timespec_sub(ts, ep->timestamp);
1052 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1053 if (ep->credit > ep->credit_ceiling)
1054 ep->credit = ep->credit_ceiling;
1056 while (ep->credit && !list_empty(&ep->urb_list)) {
1057 urbl = list_first_entry(&ep->urb_list,
1058 struct oz_urb_link, link);
1060 if ((ep->credit + 1) < urb->number_of_packets)
1062 ep->credit -= urb->number_of_packets;
1065 list_move_tail(&urbl->link, &xfr_list);
1068 spin_unlock_bh(&ozhcd->hcd_lock);
1069 /* Send to PD and complete URBs.
1071 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
1073 list_del_init(&urbl->link);
1074 urb->error_count = 0;
1075 urb->start_frame = oz_usb_get_frame_number();
1076 oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
1077 oz_free_urb_link(urbl);
1078 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1080 /* Check the IN isoc endpoints to see if any URBs can be completed.
1082 spin_lock_bh(&ozhcd->hcd_lock);
1083 list_for_each_entry(ep, &port->isoc_in_ep, link) {
1084 if (ep->flags & OZ_F_EP_BUFFERING) {
1085 if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
1086 ep->flags &= ~OZ_F_EP_BUFFERING;
1089 ep->start_frame = 0;
1093 delta = timespec_sub(ts, ep->timestamp);
1094 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1096 list_for_each_entry_safe(urbl, n, &ep->urb_list, link) {
1097 struct urb *urb = urbl->urb;
1102 if (ep->credit < urb->number_of_packets)
1104 if (ep->buffered_units < urb->number_of_packets)
1106 urb->actual_length = 0;
1107 for (i = 0; i < urb->number_of_packets; i++) {
1108 len = ep->buffer[ep->out_ix];
1109 if (++ep->out_ix == ep->buffer_size)
1111 copy_len = ep->buffer_size - ep->out_ix;
1114 memcpy(urb->transfer_buffer,
1115 &ep->buffer[ep->out_ix], copy_len);
1116 if (copy_len < len) {
1117 memcpy(urb->transfer_buffer+copy_len,
1118 ep->buffer, len-copy_len);
1119 ep->out_ix = len-copy_len;
1121 ep->out_ix += copy_len;
1122 if (ep->out_ix == ep->buffer_size)
1124 urb->iso_frame_desc[i].offset =
1126 urb->actual_length += len;
1127 urb->iso_frame_desc[i].actual_length = len;
1128 urb->iso_frame_desc[i].status = 0;
1130 ep->buffered_units -= urb->number_of_packets;
1131 urb->error_count = 0;
1132 urb->start_frame = ep->start_frame;
1133 ep->start_frame += urb->number_of_packets;
1134 list_move_tail(&urbl->link, &xfr_list);
1135 ep->credit -= urb->number_of_packets;
1138 if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1140 spin_unlock_bh(&ozhcd->hcd_lock);
1141 /* Complete the filled URBs.
1143 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
1145 list_del_init(&urbl->link);
1146 oz_free_urb_link(urbl);
1147 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1149 /* Check if there are any ep0 requests that have timed out.
1150 * If so resent to PD.
1152 ep = port->out_ep[0];
1154 spin_lock_bh(&ozhcd->hcd_lock);
1155 list_for_each_entry_safe(urbl, n, &ep->urb_list, link) {
1156 if (urbl->submit_counter > EP0_TIMEOUT_COUNTER) {
1157 oz_dbg(ON, "Request 0x%p timeout\n", urbl->urb);
1158 list_move_tail(&urbl->link, &xfr_list);
1159 urbl->submit_counter = 0;
1161 urbl->submit_counter++;
1164 if (!list_empty(&ep->urb_list))
1166 spin_unlock_bh(&ozhcd->hcd_lock);
1167 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
1168 oz_dbg(ON, "Resending request to PD\n");
1169 oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1170 oz_free_urb_link(urbl);
1179 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1180 struct oz_port *port,
1181 struct usb_host_interface *intf, gfp_t mem_flags)
1183 struct oz_hcd *ozhcd = port->ozhcd;
1185 int if_ix = intf->desc.bInterfaceNumber;
1186 int request_heartbeat = 0;
1188 oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
1189 if (if_ix >= port->num_iface || port->iface == NULL)
1191 for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1192 struct usb_host_endpoint *hep = &intf->endpoint[i];
1193 u8 ep_addr = hep->desc.bEndpointAddress;
1194 u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1195 struct oz_endpoint *ep;
1196 int buffer_size = 0;
1198 oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
1199 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1200 switch (hep->desc.bmAttributes &
1201 USB_ENDPOINT_XFERTYPE_MASK) {
1202 case USB_ENDPOINT_XFER_ISOC:
1203 buffer_size = OZ_EP_BUFFER_SIZE_ISOC;
1205 case USB_ENDPOINT_XFER_INT:
1206 buffer_size = OZ_EP_BUFFER_SIZE_INT;
1211 ep = oz_ep_alloc(buffer_size, mem_flags);
1213 oz_clean_endpoints_for_interface(hcd, port, if_ix);
1216 ep->attrib = hep->desc.bmAttributes;
1217 ep->ep_num = ep_num;
1218 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1219 == USB_ENDPOINT_XFER_ISOC) {
1220 oz_dbg(ON, "wMaxPacketSize = %d\n",
1221 usb_endpoint_maxp(&hep->desc));
1222 ep->credit_ceiling = 200;
1223 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1224 ep->flags |= OZ_F_EP_BUFFERING;
1226 ep->flags |= OZ_F_EP_HAVE_STREAM;
1227 if (oz_usb_stream_create(port->hpd, ep_num))
1228 ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1231 spin_lock_bh(&ozhcd->hcd_lock);
1232 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1233 port->in_ep[ep_num] = ep;
1234 port->iface[if_ix].ep_mask |=
1235 (1<<(ep_num+OZ_NB_ENDPOINTS));
1236 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1237 == USB_ENDPOINT_XFER_ISOC) {
1238 list_add_tail(&ep->link, &port->isoc_in_ep);
1239 request_heartbeat = 1;
1242 port->out_ep[ep_num] = ep;
1243 port->iface[if_ix].ep_mask |= (1<<ep_num);
1244 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1245 == USB_ENDPOINT_XFER_ISOC) {
1246 list_add_tail(&ep->link, &port->isoc_out_ep);
1247 request_heartbeat = 1;
1250 spin_unlock_bh(&ozhcd->hcd_lock);
1251 if (request_heartbeat && port->hpd)
1252 oz_usb_request_heartbeat(port->hpd);
1260 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1261 struct oz_port *port, int if_ix)
1263 struct oz_hcd *ozhcd = port->ozhcd;
1267 struct oz_endpoint *ep, *n;
1269 oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
1270 if (if_ix >= port->num_iface)
1272 spin_lock_bh(&ozhcd->hcd_lock);
1273 mask = port->iface[if_ix].ep_mask;
1274 port->iface[if_ix].ep_mask = 0;
1275 for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1276 struct list_head *e;
1277 /* Gather OUT endpoints.
1279 if ((mask & (1<<i)) && port->out_ep[i]) {
1280 e = &port->out_ep[i]->link;
1281 port->out_ep[i] = NULL;
1282 /* Remove from isoc list if present.
1284 list_move_tail(e, &ep_list);
1286 /* Gather IN endpoints.
1288 if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1289 e = &port->in_ep[i]->link;
1290 port->in_ep[i] = NULL;
1291 list_move_tail(e, &ep_list);
1294 spin_unlock_bh(&ozhcd->hcd_lock);
1295 list_for_each_entry_safe(ep, n, &ep_list, link) {
1296 list_del_init(&ep->link);
1297 oz_ep_free(port, ep);
1304 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1305 struct oz_port *port, struct usb_host_config *config,
1308 struct oz_hcd *ozhcd = port->ozhcd;
1310 int num_iface = config->desc.bNumInterfaces;
1313 struct oz_interface *iface;
1315 iface = kmalloc_array(num_iface, sizeof(struct oz_interface),
1316 mem_flags | __GFP_ZERO);
1319 spin_lock_bh(&ozhcd->hcd_lock);
1320 port->iface = iface;
1321 port->num_iface = num_iface;
1322 spin_unlock_bh(&ozhcd->hcd_lock);
1324 for (i = 0; i < num_iface; i++) {
1325 struct usb_host_interface *intf =
1326 &config->intf_cache[i]->altsetting[0];
1327 if (oz_build_endpoints_for_interface(hcd, port, intf,
1333 oz_clean_endpoints_for_config(hcd, port);
1340 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1341 struct oz_port *port)
1343 struct oz_hcd *ozhcd = port->ozhcd;
1346 oz_dbg(ON, "Deleting endpoints for configuration\n");
1347 for (i = 0; i < port->num_iface; i++)
1348 oz_clean_endpoints_for_interface(hcd, port, i);
1349 spin_lock_bh(&ozhcd->hcd_lock);
1351 oz_dbg(ON, "Freeing interfaces object\n");
1355 port->num_iface = 0;
1356 spin_unlock_bh(&ozhcd->hcd_lock);
1362 static void *oz_claim_hpd(struct oz_port *port)
1365 struct oz_hcd *ozhcd = port->ozhcd;
1367 spin_lock_bh(&ozhcd->hcd_lock);
1371 spin_unlock_bh(&ozhcd->hcd_lock);
1378 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1381 struct usb_ctrlrequest *setup;
1388 unsigned complete = 0;
1391 struct oz_port *port = NULL;
1393 oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
1394 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1399 port = &ozhcd->ports[port_ix];
1400 if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1401 || (port->flags & OZ_PORT_F_DYING)) {
1402 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1403 port_ix, urb->dev->devnum);
1407 /* Store port in private context data.
1410 setup = (struct usb_ctrlrequest *)urb->setup_packet;
1411 windex = le16_to_cpu(setup->wIndex);
1412 wvalue = le16_to_cpu(setup->wValue);
1413 wlength = le16_to_cpu(setup->wLength);
1414 oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
1415 oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1416 oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
1417 oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
1418 oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
1420 req_id = port->next_req_id++;
1421 hpd = oz_claim_hpd(port);
1423 oz_dbg(ON, "Cannot claim port\n");
1428 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1429 /* Standard requests
1431 switch (setup->bRequest) {
1432 case USB_REQ_GET_DESCRIPTOR:
1433 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
1435 case USB_REQ_SET_ADDRESS:
1436 oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
1437 oz_dbg(ON, "Port %d address is 0x%x\n",
1439 (u8)le16_to_cpu(setup->wValue));
1440 spin_lock_bh(&ozhcd->hcd_lock);
1441 if (ozhcd->conn_port >= 0) {
1442 ozhcd->ports[ozhcd->conn_port].bus_addr =
1443 (u8)le16_to_cpu(setup->wValue);
1444 oz_dbg(ON, "Clearing conn_port\n");
1445 ozhcd->conn_port = -1;
1447 spin_unlock_bh(&ozhcd->hcd_lock);
1450 case USB_REQ_SET_CONFIGURATION:
1451 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
1453 case USB_REQ_GET_CONFIGURATION:
1454 /* We short circuit this case and reply directly since
1455 * we have the selected configuration number cached.
1457 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
1458 if (urb->transfer_buffer_length >= 1) {
1459 urb->actual_length = 1;
1460 *((u8 *)urb->transfer_buffer) =
1467 case USB_REQ_GET_INTERFACE:
1468 /* We short circuit this case and reply directly since
1469 * we have the selected interface alternative cached.
1471 oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
1472 if (urb->transfer_buffer_length >= 1) {
1473 urb->actual_length = 1;
1474 *((u8 *)urb->transfer_buffer) =
1475 port->iface[(u8)windex].alt;
1476 oz_dbg(ON, "interface = %d alt = %d\n",
1477 windex, port->iface[(u8)windex].alt);
1483 case USB_REQ_SET_INTERFACE:
1484 oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
1488 if (!rc && !complete) {
1491 if ((setup->bRequestType & USB_DIR_IN) == 0)
1493 urb->actual_length = data_len;
1494 if (oz_usb_control_req(port->hpd, req_id, setup,
1495 urb->transfer_buffer, data_len)) {
1498 /* Note: we are queuing the request after we have
1499 * submitted it to be transmitted. If the request were
1500 * to complete before we queued it then it would not
1501 * be found in the queue. It seems impossible for
1502 * this to happen but if it did the request would
1503 * be resubmitted so the problem would hopefully
1504 * resolve itself. Putting the request into the
1505 * queue before it has been sent is worse since the
1506 * urb could be cancelled while we are using it
1507 * to build the request.
1509 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1515 if (rc || complete) {
1516 oz_dbg(ON, "Completing request locally\n");
1517 oz_complete_urb(ozhcd->hcd, urb, rc);
1519 oz_usb_request_heartbeat(port->hpd);
1526 static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1529 struct oz_port *port = urb->hcpriv;
1532 /* When we are paranoid we keep a list of urbs which we check against
1533 * before handing one back. This is just for debugging during
1534 * development and should be turned off in the released driver.
1536 oz_remember_urb(urb);
1537 /* Check buffer is valid.
1539 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1541 /* Check if there is a device at the port - refuse if not.
1543 if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1545 ep_addr = usb_pipeendpoint(urb->pipe);
1547 /* If the request is not for EP0 then queue it.
1549 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1553 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1561 static void oz_urb_process_tasklet(unsigned long unused)
1563 unsigned long irq_state;
1565 struct oz_hcd *ozhcd = oz_hcd_claim();
1566 struct oz_urb_link *urbl, *n;
1571 /* This is called from a tasklet so is in softirq context but the urb
1572 * list is filled from any context so we need to lock
1573 * appropriately while removing urbs.
1575 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1576 list_for_each_entry_safe(urbl, n, &ozhcd->urb_pending_list, link) {
1577 list_del_init(&urbl->link);
1578 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1580 oz_free_urb_link(urbl);
1581 rc = oz_urb_process(ozhcd, urb);
1583 oz_complete_urb(ozhcd->hcd, urb, rc);
1584 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1586 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1591 * This function searches for the urb in any of the lists it could be in.
1592 * If it is found it is removed from the list and completed. If the urb is
1593 * being processed then it won't be in a list so won't be found. However, the
1594 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1595 * to a non-zero value. When an attempt is made to put the urb back in a list
1596 * the unlinked field will be checked and the urb will then be completed.
1599 static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1601 struct oz_urb_link *urbl = NULL;
1602 struct list_head *e;
1603 struct oz_hcd *ozhcd;
1604 unsigned long irq_state;
1608 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
1611 ozhcd = port->ozhcd;
1612 if (ozhcd == NULL) {
1613 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
1617 /* Look in the tasklet queue.
1619 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1620 list_for_each(e, &ozhcd->urb_cancel_list) {
1621 urbl = list_entry(e, struct oz_urb_link, link);
1622 if (urb == urbl->urb) {
1624 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1628 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1631 /* Look in the orphanage.
1633 spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1634 list_for_each(e, &ozhcd->orphanage) {
1635 urbl = list_entry(e, struct oz_urb_link, link);
1636 if (urbl->urb == urb) {
1638 oz_dbg(ON, "Found urb in orphanage\n");
1642 ix = (ep_num & 0xf);
1644 if ((ep_num & USB_DIR_IN) && ix)
1645 urbl = oz_remove_urb(port->in_ep[ix], urb);
1647 urbl = oz_remove_urb(port->out_ep[ix], urb);
1649 spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1652 urb->actual_length = 0;
1653 oz_free_urb_link(urbl);
1654 oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
1661 static void oz_urb_cancel_tasklet(unsigned long unused)
1663 unsigned long irq_state;
1665 struct oz_urb_link *urbl, *n;
1666 struct oz_hcd *ozhcd = oz_hcd_claim();
1670 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1671 list_for_each_entry_safe(urbl, n, &ozhcd->urb_cancel_list, link) {
1672 list_del_init(&urbl->link);
1673 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1676 oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1677 oz_free_urb_link(urbl);
1678 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1680 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1687 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1690 struct oz_urb_link *urbl, *n;
1692 list_for_each_entry_safe(urbl, n, &ozhcd->orphanage, link) {
1693 list_del(&urbl->link);
1694 oz_complete_urb(ozhcd->hcd, urbl->urb, status);
1695 oz_free_urb_link(urbl);
1703 static int oz_hcd_start(struct usb_hcd *hcd)
1705 hcd->power_budget = 200;
1706 hcd->state = HC_STATE_RUNNING;
1707 hcd->uses_new_polling = 1;
1714 static void oz_hcd_stop(struct usb_hcd *hcd)
1721 static void oz_hcd_shutdown(struct usb_hcd *hcd)
1726 * Called to queue an urb for the device.
1727 * This function should return a non-zero error code if it fails the urb but
1728 * should not call usb_hcd_giveback_urb().
1731 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1734 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1737 struct oz_port *port;
1738 unsigned long irq_state;
1739 struct oz_urb_link *urbl;
1741 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1742 if (unlikely(ozhcd == NULL)) {
1743 oz_dbg(URB, "Refused urb(%p) not ozhcd\n", urb);
1746 if (unlikely(hcd->state != HC_STATE_RUNNING)) {
1747 oz_dbg(URB, "Refused urb(%p) not running\n", urb);
1750 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1753 port = &ozhcd->ports[port_ix];
1756 if (!(port->flags & OZ_PORT_F_PRESENT) ||
1757 (port->flags & OZ_PORT_F_CHANGED)) {
1758 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1759 port_ix, urb->dev->devnum);
1763 /* Put request in queue for processing by tasklet.
1765 urbl = oz_alloc_urb_link();
1766 if (unlikely(urbl == NULL))
1769 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1770 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1772 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1773 oz_free_urb_link(urbl);
1776 list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1777 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1778 tasklet_schedule(&g_urb_process_tasklet);
1779 atomic_inc(&g_pending_urbs);
1786 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1789 struct oz_urb_link *urbl;
1791 if (unlikely(ep == NULL))
1794 list_for_each_entry(urbl, &ep->urb_list, link) {
1795 if (urbl->urb == urb) {
1796 list_del_init(&urbl->link);
1797 if (usb_pipeisoc(urb->pipe)) {
1798 ep->credit -= urb->number_of_packets;
1809 * Called to dequeue a previously submitted urb for the device.
1812 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1814 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1815 struct oz_urb_link *urbl;
1817 unsigned long irq_state;
1819 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1820 urbl = oz_alloc_urb_link();
1821 if (unlikely(urbl == NULL))
1823 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1824 /* The following function checks the urb is still in the queue
1825 * maintained by the core and that the unlinked field is zero.
1826 * If both are true the function sets the unlinked field and returns
1827 * zero. Otherwise it returns an error.
1829 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1830 /* We have to check we haven't completed the urb or are about
1831 * to complete it. When we do we set hcpriv to 0 so if this has
1832 * already happened we don't put the urb in the cancel queue.
1834 if ((rc == 0) && urb->hcpriv) {
1836 urbl->port = (struct oz_port *)urb->hcpriv;
1837 urbl->ep_num = usb_pipeendpoint(urb->pipe);
1838 if (usb_pipein(urb->pipe))
1839 urbl->ep_num |= USB_DIR_IN;
1840 list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1841 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1842 tasklet_schedule(&g_urb_cancel_tasklet);
1844 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1845 oz_free_urb_link(urbl);
1853 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1854 struct usb_host_endpoint *ep)
1861 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1862 struct usb_host_endpoint *ep)
1869 static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1871 oz_dbg(ON, "oz_hcd_get_frame_number\n");
1872 return oz_usb_get_frame_number();
1877 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1878 * always do that in softirq context.
1880 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1882 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1888 spin_lock_bh(&ozhcd->hcd_lock);
1889 for (i = 0; i < OZ_NB_PORTS; i++) {
1890 if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
1891 oz_dbg(HUB, "Port %d changed\n", i);
1892 ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
1894 buf[0] |= 1 << (i + 1);
1896 buf[1] |= 1 << (i - 7);
1899 spin_unlock_bh(&ozhcd->hcd_lock);
1900 if (buf[0] != 0 || buf[1] != 0)
1908 static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1909 struct usb_hub_descriptor *desc)
1911 memset(desc, 0, sizeof(*desc));
1912 desc->bDescriptorType = 0x29;
1913 desc->bDescLength = 9;
1914 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1915 desc->bNbrPorts = OZ_NB_PORTS;
1921 static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1923 struct oz_port *port;
1924 u8 port_id = (u8)windex;
1925 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1926 unsigned set_bits = 0;
1927 unsigned clear_bits = 0;
1929 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
1931 port = &ozhcd->ports[port_id-1];
1933 case USB_PORT_FEAT_CONNECTION:
1934 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
1936 case USB_PORT_FEAT_ENABLE:
1937 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
1939 case USB_PORT_FEAT_SUSPEND:
1940 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
1942 case USB_PORT_FEAT_OVER_CURRENT:
1943 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
1945 case USB_PORT_FEAT_RESET:
1946 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
1947 set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
1948 clear_bits = USB_PORT_STAT_RESET;
1949 ozhcd->ports[port_id-1].bus_addr = 0;
1951 case USB_PORT_FEAT_POWER:
1952 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
1953 set_bits |= USB_PORT_STAT_POWER;
1955 case USB_PORT_FEAT_LOWSPEED:
1956 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
1958 case USB_PORT_FEAT_C_CONNECTION:
1959 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
1961 case USB_PORT_FEAT_C_ENABLE:
1962 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
1964 case USB_PORT_FEAT_C_SUSPEND:
1965 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
1967 case USB_PORT_FEAT_C_OVER_CURRENT:
1968 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
1970 case USB_PORT_FEAT_C_RESET:
1971 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
1973 case USB_PORT_FEAT_TEST:
1974 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
1976 case USB_PORT_FEAT_INDICATOR:
1977 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
1980 oz_dbg(HUB, "Other %d\n", wvalue);
1983 if (set_bits || clear_bits) {
1984 spin_lock_bh(&port->port_lock);
1985 port->status &= ~clear_bits;
1986 port->status |= set_bits;
1987 spin_unlock_bh(&port->port_lock);
1989 oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
1996 static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1998 struct oz_port *port;
1999 u8 port_id = (u8)windex;
2000 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2001 unsigned clear_bits = 0;
2003 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2005 port = &ozhcd->ports[port_id-1];
2007 case USB_PORT_FEAT_CONNECTION:
2008 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
2010 case USB_PORT_FEAT_ENABLE:
2011 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
2012 clear_bits = USB_PORT_STAT_ENABLE;
2014 case USB_PORT_FEAT_SUSPEND:
2015 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
2017 case USB_PORT_FEAT_OVER_CURRENT:
2018 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2020 case USB_PORT_FEAT_RESET:
2021 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
2023 case USB_PORT_FEAT_POWER:
2024 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2025 clear_bits |= USB_PORT_STAT_POWER;
2027 case USB_PORT_FEAT_LOWSPEED:
2028 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2030 case USB_PORT_FEAT_C_CONNECTION:
2031 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2032 clear_bits = USB_PORT_STAT_C_CONNECTION << 16;
2034 case USB_PORT_FEAT_C_ENABLE:
2035 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2036 clear_bits = USB_PORT_STAT_C_ENABLE << 16;
2038 case USB_PORT_FEAT_C_SUSPEND:
2039 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2041 case USB_PORT_FEAT_C_OVER_CURRENT:
2042 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2044 case USB_PORT_FEAT_C_RESET:
2045 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2046 clear_bits = USB_PORT_FEAT_C_RESET << 16;
2048 case USB_PORT_FEAT_TEST:
2049 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2051 case USB_PORT_FEAT_INDICATOR:
2052 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2055 oz_dbg(HUB, "Other %d\n", wvalue);
2059 spin_lock_bh(&port->port_lock);
2060 port->status &= ~clear_bits;
2061 spin_unlock_bh(&port->port_lock);
2063 oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2064 port_id, ozhcd->ports[port_id-1].status);
2071 static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2073 struct oz_hcd *ozhcd;
2076 if ((windex < 1) || (windex > OZ_NB_PORTS))
2078 ozhcd = oz_hcd_private(hcd);
2079 oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
2080 status = ozhcd->ports[windex-1].status;
2081 put_unaligned(cpu_to_le32(status), (__le32 *)buf);
2082 oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
2089 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2090 u16 windex, char *buf, u16 wlength)
2095 case ClearHubFeature:
2096 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
2098 case ClearPortFeature:
2099 err = oz_clear_port_feature(hcd, wvalue, windex);
2101 case GetHubDescriptor:
2102 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2105 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
2106 put_unaligned(cpu_to_le32(0), (__le32 *)buf);
2109 err = oz_get_port_status(hcd, windex, buf);
2112 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
2114 case SetPortFeature:
2115 err = oz_set_port_feature(hcd, wvalue, windex);
2118 oz_dbg(HUB, "Other: %d\n", req_type);
2127 static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2129 struct oz_hcd *ozhcd;
2131 ozhcd = oz_hcd_private(hcd);
2132 spin_lock_bh(&ozhcd->hcd_lock);
2133 hcd->state = HC_STATE_SUSPENDED;
2134 ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2135 spin_unlock_bh(&ozhcd->hcd_lock);
2142 static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2144 struct oz_hcd *ozhcd;
2146 ozhcd = oz_hcd_private(hcd);
2147 spin_lock_bh(&ozhcd->hcd_lock);
2148 ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2149 hcd->state = HC_STATE_RUNNING;
2150 spin_unlock_bh(&ozhcd->hcd_lock);
2154 static void oz_plat_shutdown(struct platform_device *dev)
2161 static int oz_plat_probe(struct platform_device *dev)
2165 struct usb_hcd *hcd;
2166 struct oz_hcd *ozhcd;
2168 hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
2170 oz_dbg(ON, "Failed to created hcd object OK\n");
2173 ozhcd = oz_hcd_private(hcd);
2174 memset(ozhcd, 0, sizeof(*ozhcd));
2175 INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2176 INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2177 INIT_LIST_HEAD(&ozhcd->orphanage);
2179 ozhcd->conn_port = -1;
2180 spin_lock_init(&ozhcd->hcd_lock);
2181 for (i = 0; i < OZ_NB_PORTS; i++) {
2182 struct oz_port *port = &ozhcd->ports[i];
2184 port->ozhcd = ozhcd;
2187 port->bus_addr = 0xff;
2188 spin_lock_init(&port->port_lock);
2190 err = usb_add_hcd(hcd, 0, 0);
2192 oz_dbg(ON, "Failed to add hcd object OK\n");
2196 device_wakeup_enable(hcd->self.controller);
2198 spin_lock_bh(&g_hcdlock);
2200 spin_unlock_bh(&g_hcdlock);
2207 static int oz_plat_remove(struct platform_device *dev)
2209 struct usb_hcd *hcd = platform_get_drvdata(dev);
2210 struct oz_hcd *ozhcd;
2214 ozhcd = oz_hcd_private(hcd);
2215 spin_lock_bh(&g_hcdlock);
2216 if (ozhcd == g_ozhcd)
2218 spin_unlock_bh(&g_hcdlock);
2219 oz_dbg(ON, "Clearing orphanage\n");
2220 oz_hcd_clear_orphanage(ozhcd, -EPIPE);
2221 oz_dbg(ON, "Removing hcd\n");
2222 usb_remove_hcd(hcd);
2230 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2239 static int oz_plat_resume(struct platform_device *dev)
2247 int oz_hcd_init(void)
2254 oz_urb_link_cache = KMEM_CACHE(oz_urb_link, 0);
2255 if (!oz_urb_link_cache)
2258 tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
2259 tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
2260 err = platform_driver_register(&g_oz_plat_drv);
2261 oz_dbg(ON, "platform_driver_register() returned %d\n", err);
2264 g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
2265 if (g_plat_dev == NULL) {
2269 oz_dbg(ON, "platform_device_alloc() succeeded\n");
2270 err = platform_device_add(g_plat_dev);
2273 oz_dbg(ON, "platform_device_add() succeeded\n");
2276 platform_device_put(g_plat_dev);
2278 platform_driver_unregister(&g_oz_plat_drv);
2280 tasklet_disable(&g_urb_process_tasklet);
2281 tasklet_disable(&g_urb_cancel_tasklet);
2282 oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
2289 void oz_hcd_term(void)
2291 msleep(OZ_HUB_DEBOUNCE_TIMEOUT);
2292 tasklet_kill(&g_urb_process_tasklet);
2293 tasklet_kill(&g_urb_cancel_tasklet);
2294 platform_device_unregister(g_plat_dev);
2295 platform_driver_unregister(&g_oz_plat_drv);
2296 oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));
2297 kmem_cache_destroy(oz_urb_link_cache);