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;
285 ep = kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
289 INIT_LIST_HEAD(&ep->urb_list);
290 INIT_LIST_HEAD(&ep->link);
293 ep->buffer_size = buffer_size;
294 ep->buffer = (u8 *)(ep+1);
301 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
303 * Context: softirq or process
305 static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd,
308 struct oz_urb_link *urbl;
310 list_for_each_entry(urbl, &ozhcd->urb_cancel_list, link) {
311 if (urb == urbl->urb) {
312 list_del_init(&urbl->link);
320 * This is called when we have finished processing an urb. It unlinks it from
321 * the ep and returns it to the core.
322 * Context: softirq or process
324 static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
327 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
328 unsigned long irq_state;
329 struct oz_urb_link *cancel_urbl;
331 spin_lock_irqsave(&g_tasklet_lock, irq_state);
332 usb_hcd_unlink_urb_from_ep(hcd, urb);
333 /* Clear hcpriv which will prevent it being put in the cancel list
334 * in the event that an attempt is made to cancel it.
337 /* Walk the cancel list in case the urb is already sitting there.
338 * Since we process the cancel list in a tasklet rather than in
339 * the dequeue function this could happen.
341 cancel_urbl = oz_uncancel_urb(ozhcd, urb);
342 /* Note: we release lock but do not enable local irqs.
343 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
344 * or at least other host controllers disable interrupts at this point
345 * so we do the same. We must, however, release the lock otherwise a
346 * deadlock will occur if an urb is submitted to our driver in the urb
347 * completion function. Because we disable interrupts it is possible
348 * that the urb_enqueue function can be called with them disabled.
350 spin_unlock(&g_tasklet_lock);
351 if (oz_forget_urb(urb)) {
352 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
354 atomic_dec(&g_pending_urbs);
355 usb_hcd_giveback_urb(hcd, urb, status);
357 spin_lock(&g_tasklet_lock);
358 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
359 oz_free_urb_link(cancel_urbl);
363 * Deallocates an endpoint including deallocating any associated stream and
364 * returning any queued urbs to the core.
367 static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
371 struct oz_hcd *ozhcd = port->ozhcd;
373 if (ep->flags & OZ_F_EP_HAVE_STREAM)
374 oz_usb_stream_delete(port->hpd, ep->ep_num);
375 /* Transfer URBs to the orphanage while we hold the lock. */
376 spin_lock_bh(&ozhcd->hcd_lock);
377 /* Note: this works even if ep->urb_list is empty.*/
378 list_replace_init(&ep->urb_list, &list);
379 /* Put the URBs in the orphanage. */
380 list_splice_tail(&list, &ozhcd->orphanage);
381 spin_unlock_bh(&ozhcd->hcd_lock);
383 oz_dbg(ON, "Freeing endpoint memory\n");
390 static void oz_complete_buffered_urb(struct oz_port *port,
391 struct oz_endpoint *ep,
394 int data_len, available_space, copy_len;
396 data_len = ep->buffer[ep->out_ix];
397 if (data_len <= urb->transfer_buffer_length)
398 available_space = data_len;
400 available_space = urb->transfer_buffer_length;
402 if (++ep->out_ix == ep->buffer_size)
404 copy_len = ep->buffer_size - ep->out_ix;
405 if (copy_len >= available_space)
406 copy_len = available_space;
407 memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
409 if (copy_len < available_space) {
410 memcpy((urb->transfer_buffer + copy_len), ep->buffer,
411 (available_space - copy_len));
412 ep->out_ix = available_space - copy_len;
414 ep->out_ix += copy_len;
416 urb->actual_length = available_space;
417 if (ep->out_ix == ep->buffer_size)
420 ep->buffered_units--;
421 oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
423 oz_complete_urb(port->ozhcd->hcd, urb, 0);
429 static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
430 struct urb *urb, u8 req_id)
432 struct oz_urb_link *urbl;
433 struct oz_endpoint *ep = NULL;
436 if (ep_addr >= OZ_NB_ENDPOINTS) {
437 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
440 urbl = oz_alloc_urb_link();
443 urbl->submit_counter = 0;
445 urbl->req_id = req_id;
446 urbl->ep_num = ep_addr;
447 /* Hold lock while we insert the URB into the list within the
448 * endpoint structure.
450 spin_lock_bh(&port->ozhcd->hcd_lock);
451 /* If the urb has been unlinked while out of any list then
455 spin_unlock_bh(&port->ozhcd->hcd_lock);
456 oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
457 oz_complete_urb(port->ozhcd->hcd, urb, 0);
458 oz_free_urb_link(urbl);
463 ep = port->in_ep[ep_addr];
465 ep = port->out_ep[ep_addr];
471 /*For interrupt endpoint check for buffered data
474 if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
475 && ep->buffered_units > 0) {
476 oz_free_urb_link(urbl);
477 spin_unlock_bh(&port->ozhcd->hcd_lock);
478 oz_complete_buffered_urb(port, ep, urb);
483 list_add_tail(&urbl->link, &ep->urb_list);
484 if (!in_dir && ep_addr && (ep->credit < 0)) {
485 getrawmonotonic(&ep->timestamp);
492 spin_unlock_bh(&port->ozhcd->hcd_lock);
494 oz_free_urb_link(urbl);
499 * Removes an urb from the queue in the endpoint.
500 * Returns 0 if it is found and -EIDRM otherwise.
503 static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
506 struct oz_urb_link *urbl = NULL;
507 struct oz_endpoint *ep;
509 spin_lock_bh(&port->ozhcd->hcd_lock);
511 ep = port->in_ep[ep_addr];
513 ep = port->out_ep[ep_addr];
517 list_for_each(e, &ep->urb_list) {
518 urbl = list_entry(e, struct oz_urb_link, link);
519 if (urbl->urb == urb) {
526 spin_unlock_bh(&port->ozhcd->hcd_lock);
527 oz_free_urb_link(urbl);
528 return urbl ? 0 : -EIDRM;
532 * Finds an urb given its request id.
535 static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
538 struct oz_hcd *ozhcd = port->ozhcd;
539 struct urb *urb = NULL;
540 struct oz_urb_link *urbl;
541 struct oz_endpoint *ep;
543 spin_lock_bh(&ozhcd->hcd_lock);
544 ep = port->out_ep[ep_ix];
548 list_for_each(e, &ep->urb_list) {
549 urbl = list_entry(e, struct oz_urb_link, link);
550 if (urbl->req_id == req_id) {
557 spin_unlock_bh(&ozhcd->hcd_lock);
558 /* If urb is non-zero then we we must have an urb link to delete.
561 oz_free_urb_link(urbl);
566 * Pre-condition: Port lock must be held.
569 static void oz_acquire_port(struct oz_port *port, void *hpd)
571 INIT_LIST_HEAD(&port->isoc_out_ep);
572 INIT_LIST_HEAD(&port->isoc_in_ep);
573 port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
574 port->status |= USB_PORT_STAT_CONNECTION |
575 (USB_PORT_STAT_C_CONNECTION << 16);
583 static struct oz_hcd *oz_hcd_claim(void)
585 struct oz_hcd *ozhcd;
587 spin_lock_bh(&g_hcdlock);
590 usb_get_hcd(ozhcd->hcd);
591 spin_unlock_bh(&g_hcdlock);
598 static inline void oz_hcd_put(struct oz_hcd *ozhcd)
601 usb_put_hcd(ozhcd->hcd);
605 * This is called by the protocol handler to notify that a PD has arrived.
606 * We allocate a port to associate with the PD and create a structure for
607 * endpoint 0. This port is made the connection port.
608 * In the event that one of the other port is already a connection port then
610 * TODO We should be able to do better than fail and should be able remember
611 * that this port needs configuring and make it the connection port once the
612 * current connection port has been assigned an address. Collisions here are
613 * probably very rare indeed.
616 struct oz_port *oz_hcd_pd_arrived(void *hpd)
619 struct oz_port *hport;
620 struct oz_hcd *ozhcd;
621 struct oz_endpoint *ep;
623 ozhcd = oz_hcd_claim();
626 /* Allocate an endpoint object in advance (before holding hcd lock) to
627 * use for out endpoint 0.
629 ep = oz_ep_alloc(0, GFP_ATOMIC);
633 spin_lock_bh(&ozhcd->hcd_lock);
634 if (ozhcd->conn_port >= 0)
637 for (i = 0; i < OZ_NB_PORTS; i++) {
638 struct oz_port *port = &ozhcd->ports[i];
640 spin_lock(&port->port_lock);
641 if (!(port->flags & (OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED))) {
642 oz_acquire_port(port, hpd);
643 spin_unlock(&port->port_lock);
646 spin_unlock(&port->port_lock);
648 if (i == OZ_NB_PORTS)
651 ozhcd->conn_port = i;
652 hport = &ozhcd->ports[i];
653 hport->out_ep[0] = ep;
654 spin_unlock_bh(&ozhcd->hcd_lock);
655 if (ozhcd->flags & OZ_HDC_F_SUSPENDED)
656 usb_hcd_resume_root_hub(ozhcd->hcd);
657 usb_hcd_poll_rh_status(ozhcd->hcd);
663 spin_unlock_bh(&ozhcd->hcd_lock);
664 oz_ep_free(NULL, ep);
671 * This is called by the protocol handler to notify that the PD has gone away.
672 * We need to deallocate all resources and then request that the root hub is
673 * polled. We release the reference we hold on the PD.
676 void oz_hcd_pd_departed(struct oz_port *port)
678 struct oz_hcd *ozhcd;
680 struct oz_endpoint *ep = NULL;
683 oz_dbg(ON, "%s: port = 0\n", __func__);
689 /* Check if this is the connection port - if so clear it.
691 spin_lock_bh(&ozhcd->hcd_lock);
692 if ((ozhcd->conn_port >= 0) &&
693 (port == &ozhcd->ports[ozhcd->conn_port])) {
694 oz_dbg(ON, "Clearing conn_port\n");
695 ozhcd->conn_port = -1;
697 spin_lock(&port->port_lock);
698 port->flags |= OZ_PORT_F_DYING;
699 spin_unlock(&port->port_lock);
700 spin_unlock_bh(&ozhcd->hcd_lock);
702 oz_clean_endpoints_for_config(ozhcd->hcd, port);
703 spin_lock_bh(&port->port_lock);
706 port->bus_addr = 0xff;
707 port->config_num = 0;
708 port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
709 port->flags |= OZ_PORT_F_CHANGED;
710 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
711 port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
712 /* If there is an endpont 0 then clear the pointer while we hold
713 * the spinlock be we deallocate it after releasing the lock.
715 if (port->out_ep[0]) {
716 ep = port->out_ep[0];
717 port->out_ep[0] = NULL;
719 spin_unlock_bh(&port->port_lock);
721 oz_ep_free(port, ep);
722 usb_hcd_poll_rh_status(ozhcd->hcd);
729 void oz_hcd_pd_reset(void *hpd, void *hport)
731 /* Cleanup the current configuration and report reset to the core.
733 struct oz_port *port = hport;
734 struct oz_hcd *ozhcd = port->ozhcd;
736 oz_dbg(ON, "PD Reset\n");
737 spin_lock_bh(&port->port_lock);
738 port->flags |= OZ_PORT_F_CHANGED;
739 port->status |= USB_PORT_STAT_RESET;
740 port->status |= (USB_PORT_STAT_C_RESET << 16);
741 spin_unlock_bh(&port->port_lock);
742 oz_clean_endpoints_for_config(ozhcd->hcd, port);
743 usb_hcd_poll_rh_status(ozhcd->hcd);
749 void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
750 int length, int offset, int total_size)
752 struct oz_port *port = hport;
756 oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
757 length, offset, total_size);
758 urb = oz_find_urb_by_id(port, 0, req_id);
763 int required_size = urb->transfer_buffer_length;
765 if (required_size > total_size)
766 required_size = total_size;
767 copy_len = required_size-offset;
768 if (length <= copy_len)
770 memcpy(urb->transfer_buffer+offset, desc, copy_len);
772 if (offset < required_size) {
773 struct usb_ctrlrequest *setup =
774 (struct usb_ctrlrequest *)urb->setup_packet;
775 unsigned wvalue = le16_to_cpu(setup->wValue);
777 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
779 else if (oz_usb_get_desc_req(port->hpd, req_id,
780 setup->bRequestType, (u8)(wvalue>>8),
781 (u8)wvalue, setup->wIndex, offset,
782 required_size-offset)) {
783 oz_dequeue_ep_urb(port, 0, 0, urb);
790 urb->actual_length = total_size;
791 oz_complete_urb(port->ozhcd->hcd, urb, 0);
797 static void oz_display_conf_type(u8 t)
800 case USB_REQ_GET_STATUS:
801 oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
803 case USB_REQ_CLEAR_FEATURE:
804 oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
806 case USB_REQ_SET_FEATURE:
807 oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
809 case USB_REQ_SET_ADDRESS:
810 oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
812 case USB_REQ_GET_DESCRIPTOR:
813 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
815 case USB_REQ_SET_DESCRIPTOR:
816 oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
818 case USB_REQ_GET_CONFIGURATION:
819 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
821 case USB_REQ_SET_CONFIGURATION:
822 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
824 case USB_REQ_GET_INTERFACE:
825 oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
827 case USB_REQ_SET_INTERFACE:
828 oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
830 case USB_REQ_SYNCH_FRAME:
831 oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
839 static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
840 u8 rcode, u8 config_num)
843 struct usb_hcd *hcd = port->ozhcd->hcd;
846 port->config_num = config_num;
847 oz_clean_endpoints_for_config(hcd, port);
848 if (oz_build_endpoints_for_config(hcd, port,
849 &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
855 oz_complete_urb(hcd, urb, rc);
861 static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
862 u8 rcode, u8 if_num, u8 alt)
864 struct usb_hcd *hcd = port->ozhcd->hcd;
867 if ((rcode == 0) && (port->config_num > 0)) {
868 struct usb_host_config *config;
869 struct usb_host_interface *intf;
871 oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
872 oz_clean_endpoints_for_interface(hcd, port, if_num);
873 config = &urb->dev->config[port->config_num-1];
874 intf = &config->intf_cache[if_num]->altsetting[alt];
875 if (oz_build_endpoints_for_interface(hcd, port, intf,
879 port->iface[if_num].alt = alt;
883 oz_complete_urb(hcd, urb, rc);
889 void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
892 struct oz_port *port = hport;
894 struct usb_ctrlrequest *setup;
895 struct usb_hcd *hcd = port->ozhcd->hcd;
899 oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
900 urb = oz_find_urb_by_id(port, 0, req_id);
902 oz_dbg(ON, "URB not found\n");
905 setup = (struct usb_ctrlrequest *)urb->setup_packet;
906 windex = le16_to_cpu(setup->wIndex);
907 wvalue = le16_to_cpu(setup->wValue);
908 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
909 /* Standard requests */
910 oz_display_conf_type(setup->bRequest);
911 switch (setup->bRequest) {
912 case USB_REQ_SET_CONFIGURATION:
913 oz_hcd_complete_set_config(port, urb, rcode,
916 case USB_REQ_SET_INTERFACE:
917 oz_hcd_complete_set_interface(port, urb, rcode,
918 (u8)windex, (u8)wvalue);
921 oz_complete_urb(hcd, urb, 0);
927 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
929 if (data_len <= urb->transfer_buffer_length)
932 copy_len = urb->transfer_buffer_length;
933 memcpy(urb->transfer_buffer, data, copy_len);
934 urb->actual_length = copy_len;
936 oz_complete_urb(hcd, urb, 0);
941 * Context: softirq-serialized
943 static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
951 space = ep->out_ix-ep->in_ix-1;
953 space += ep->buffer_size;
954 if (space < (data_len+1)) {
955 oz_dbg(ON, "Buffer full\n");
958 ep->buffer[ep->in_ix] = (u8)data_len;
959 if (++ep->in_ix == ep->buffer_size)
961 copy_len = ep->buffer_size - ep->in_ix;
962 if (copy_len > data_len)
964 memcpy(&ep->buffer[ep->in_ix], data, copy_len);
966 if (copy_len < data_len) {
967 memcpy(ep->buffer, data+copy_len, data_len-copy_len);
968 ep->in_ix = data_len-copy_len;
970 ep->in_ix += copy_len;
972 if (ep->in_ix == ep->buffer_size)
974 ep->buffered_units++;
979 * Context: softirq-serialized
981 void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
983 struct oz_port *port = (struct oz_port *)hport;
984 struct oz_endpoint *ep;
985 struct oz_hcd *ozhcd = port->ozhcd;
987 spin_lock_bh(&ozhcd->hcd_lock);
988 ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
991 switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
992 case USB_ENDPOINT_XFER_INT:
993 case USB_ENDPOINT_XFER_BULK:
994 if (!list_empty(&ep->urb_list)) {
995 struct oz_urb_link *urbl =
996 list_first_entry(&ep->urb_list,
997 struct oz_urb_link, link);
1001 list_del_init(&urbl->link);
1002 spin_unlock_bh(&ozhcd->hcd_lock);
1004 oz_free_urb_link(urbl);
1005 if (data_len <= urb->transfer_buffer_length)
1006 copy_len = data_len;
1008 copy_len = urb->transfer_buffer_length;
1009 memcpy(urb->transfer_buffer, data, copy_len);
1010 urb->actual_length = copy_len;
1011 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1014 oz_dbg(ON, "buffering frame as URB is not available\n");
1015 oz_hcd_buffer_data(ep, data, data_len);
1017 case USB_ENDPOINT_XFER_ISOC:
1018 oz_hcd_buffer_data(ep, data, data_len);
1022 spin_unlock_bh(&ozhcd->hcd_lock);
1028 static inline int oz_usb_get_frame_number(void)
1030 return atomic_inc_return(&g_usb_frame_number);
1036 int oz_hcd_heartbeat(void *hport)
1039 struct oz_port *port = hport;
1040 struct oz_hcd *ozhcd = port->ozhcd;
1041 struct oz_urb_link *urbl, *n;
1042 LIST_HEAD(xfr_list);
1044 struct oz_endpoint *ep;
1045 struct timespec ts, delta;
1047 getrawmonotonic(&ts);
1048 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1050 spin_lock_bh(&ozhcd->hcd_lock);
1051 list_for_each_entry(ep, &port->isoc_out_ep, link) {
1054 delta = timespec_sub(ts, ep->timestamp);
1055 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1056 if (ep->credit > ep->credit_ceiling)
1057 ep->credit = ep->credit_ceiling;
1059 while (ep->credit && !list_empty(&ep->urb_list)) {
1060 urbl = list_first_entry(&ep->urb_list,
1061 struct oz_urb_link, link);
1063 if ((ep->credit + 1) < urb->number_of_packets)
1065 ep->credit -= urb->number_of_packets;
1068 list_move_tail(&urbl->link, &xfr_list);
1071 spin_unlock_bh(&ozhcd->hcd_lock);
1072 /* Send to PD and complete URBs.
1074 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
1076 list_del_init(&urbl->link);
1077 urb->error_count = 0;
1078 urb->start_frame = oz_usb_get_frame_number();
1079 oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
1080 oz_free_urb_link(urbl);
1081 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1083 /* Check the IN isoc endpoints to see if any URBs can be completed.
1085 spin_lock_bh(&ozhcd->hcd_lock);
1086 list_for_each_entry(ep, &port->isoc_in_ep, link) {
1087 if (ep->flags & OZ_F_EP_BUFFERING) {
1088 if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
1089 ep->flags &= ~OZ_F_EP_BUFFERING;
1092 ep->start_frame = 0;
1096 delta = timespec_sub(ts, ep->timestamp);
1097 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1099 list_for_each_entry_safe(urbl, n, &ep->urb_list, link) {
1100 struct urb *urb = urbl->urb;
1105 if (ep->credit < urb->number_of_packets)
1107 if (ep->buffered_units < urb->number_of_packets)
1109 urb->actual_length = 0;
1110 for (i = 0; i < urb->number_of_packets; i++) {
1111 len = ep->buffer[ep->out_ix];
1112 if (++ep->out_ix == ep->buffer_size)
1114 copy_len = ep->buffer_size - ep->out_ix;
1117 memcpy(urb->transfer_buffer,
1118 &ep->buffer[ep->out_ix], copy_len);
1119 if (copy_len < len) {
1120 memcpy(urb->transfer_buffer+copy_len,
1121 ep->buffer, len-copy_len);
1122 ep->out_ix = len-copy_len;
1124 ep->out_ix += copy_len;
1125 if (ep->out_ix == ep->buffer_size)
1127 urb->iso_frame_desc[i].offset =
1129 urb->actual_length += len;
1130 urb->iso_frame_desc[i].actual_length = len;
1131 urb->iso_frame_desc[i].status = 0;
1133 ep->buffered_units -= urb->number_of_packets;
1134 urb->error_count = 0;
1135 urb->start_frame = ep->start_frame;
1136 ep->start_frame += urb->number_of_packets;
1137 list_move_tail(&urbl->link, &xfr_list);
1138 ep->credit -= urb->number_of_packets;
1141 if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1143 spin_unlock_bh(&ozhcd->hcd_lock);
1144 /* Complete the filled URBs.
1146 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
1148 list_del_init(&urbl->link);
1149 oz_free_urb_link(urbl);
1150 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1152 /* Check if there are any ep0 requests that have timed out.
1153 * If so resent to PD.
1155 ep = port->out_ep[0];
1157 spin_lock_bh(&ozhcd->hcd_lock);
1158 list_for_each_entry_safe(urbl, n, &ep->urb_list, link) {
1159 if (urbl->submit_counter > EP0_TIMEOUT_COUNTER) {
1160 oz_dbg(ON, "Request 0x%p timeout\n", urbl->urb);
1161 list_move_tail(&urbl->link, &xfr_list);
1162 urbl->submit_counter = 0;
1164 urbl->submit_counter++;
1167 if (!list_empty(&ep->urb_list))
1169 spin_unlock_bh(&ozhcd->hcd_lock);
1170 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
1171 oz_dbg(ON, "Resending request to PD\n");
1172 oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1173 oz_free_urb_link(urbl);
1182 static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1183 struct oz_port *port,
1184 struct usb_host_interface *intf, gfp_t mem_flags)
1186 struct oz_hcd *ozhcd = port->ozhcd;
1188 int if_ix = intf->desc.bInterfaceNumber;
1189 int request_heartbeat = 0;
1191 oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
1192 if (if_ix >= port->num_iface || port->iface == NULL)
1194 for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1195 struct usb_host_endpoint *hep = &intf->endpoint[i];
1196 u8 ep_addr = hep->desc.bEndpointAddress;
1197 u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1198 struct oz_endpoint *ep;
1199 int buffer_size = 0;
1201 oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
1202 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1203 switch (hep->desc.bmAttributes &
1204 USB_ENDPOINT_XFERTYPE_MASK) {
1205 case USB_ENDPOINT_XFER_ISOC:
1206 buffer_size = OZ_EP_BUFFER_SIZE_ISOC;
1208 case USB_ENDPOINT_XFER_INT:
1209 buffer_size = OZ_EP_BUFFER_SIZE_INT;
1214 ep = oz_ep_alloc(buffer_size, mem_flags);
1216 oz_clean_endpoints_for_interface(hcd, port, if_ix);
1219 ep->attrib = hep->desc.bmAttributes;
1220 ep->ep_num = ep_num;
1221 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1222 == USB_ENDPOINT_XFER_ISOC) {
1223 oz_dbg(ON, "wMaxPacketSize = %d\n",
1224 usb_endpoint_maxp(&hep->desc));
1225 ep->credit_ceiling = 200;
1226 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1227 ep->flags |= OZ_F_EP_BUFFERING;
1229 ep->flags |= OZ_F_EP_HAVE_STREAM;
1230 if (oz_usb_stream_create(port->hpd, ep_num))
1231 ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1234 spin_lock_bh(&ozhcd->hcd_lock);
1235 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1236 port->in_ep[ep_num] = ep;
1237 port->iface[if_ix].ep_mask |=
1238 (1<<(ep_num+OZ_NB_ENDPOINTS));
1239 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1240 == USB_ENDPOINT_XFER_ISOC) {
1241 list_add_tail(&ep->link, &port->isoc_in_ep);
1242 request_heartbeat = 1;
1245 port->out_ep[ep_num] = ep;
1246 port->iface[if_ix].ep_mask |= (1<<ep_num);
1247 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1248 == USB_ENDPOINT_XFER_ISOC) {
1249 list_add_tail(&ep->link, &port->isoc_out_ep);
1250 request_heartbeat = 1;
1253 spin_unlock_bh(&ozhcd->hcd_lock);
1254 if (request_heartbeat && port->hpd)
1255 oz_usb_request_heartbeat(port->hpd);
1263 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1264 struct oz_port *port, int if_ix)
1266 struct oz_hcd *ozhcd = port->ozhcd;
1270 struct oz_endpoint *ep, *n;
1272 oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
1273 if (if_ix >= port->num_iface)
1275 spin_lock_bh(&ozhcd->hcd_lock);
1276 mask = port->iface[if_ix].ep_mask;
1277 port->iface[if_ix].ep_mask = 0;
1278 for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1279 struct list_head *e;
1280 /* Gather OUT endpoints.
1282 if ((mask & (1<<i)) && port->out_ep[i]) {
1283 e = &port->out_ep[i]->link;
1284 port->out_ep[i] = NULL;
1285 /* Remove from isoc list if present.
1287 list_move_tail(e, &ep_list);
1289 /* Gather IN endpoints.
1291 if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1292 e = &port->in_ep[i]->link;
1293 port->in_ep[i] = NULL;
1294 list_move_tail(e, &ep_list);
1297 spin_unlock_bh(&ozhcd->hcd_lock);
1298 list_for_each_entry_safe(ep, n, &ep_list, link) {
1299 list_del_init(&ep->link);
1300 oz_ep_free(port, ep);
1307 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1308 struct oz_port *port, struct usb_host_config *config,
1311 struct oz_hcd *ozhcd = port->ozhcd;
1313 int num_iface = config->desc.bNumInterfaces;
1316 struct oz_interface *iface;
1318 iface = kmalloc_array(num_iface, sizeof(struct oz_interface),
1319 mem_flags | __GFP_ZERO);
1322 spin_lock_bh(&ozhcd->hcd_lock);
1323 port->iface = iface;
1324 port->num_iface = num_iface;
1325 spin_unlock_bh(&ozhcd->hcd_lock);
1327 for (i = 0; i < num_iface; i++) {
1328 struct usb_host_interface *intf =
1329 &config->intf_cache[i]->altsetting[0];
1330 if (oz_build_endpoints_for_interface(hcd, port, intf,
1336 oz_clean_endpoints_for_config(hcd, port);
1343 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1344 struct oz_port *port)
1346 struct oz_hcd *ozhcd = port->ozhcd;
1349 oz_dbg(ON, "Deleting endpoints for configuration\n");
1350 for (i = 0; i < port->num_iface; i++)
1351 oz_clean_endpoints_for_interface(hcd, port, i);
1352 spin_lock_bh(&ozhcd->hcd_lock);
1354 oz_dbg(ON, "Freeing interfaces object\n");
1358 port->num_iface = 0;
1359 spin_unlock_bh(&ozhcd->hcd_lock);
1365 static void *oz_claim_hpd(struct oz_port *port)
1368 struct oz_hcd *ozhcd = port->ozhcd;
1370 spin_lock_bh(&ozhcd->hcd_lock);
1374 spin_unlock_bh(&ozhcd->hcd_lock);
1381 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1384 struct usb_ctrlrequest *setup;
1391 unsigned complete = 0;
1394 struct oz_port *port = NULL;
1396 oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
1397 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1402 port = &ozhcd->ports[port_ix];
1403 if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1404 || (port->flags & OZ_PORT_F_DYING)) {
1405 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1406 port_ix, urb->dev->devnum);
1410 /* Store port in private context data.
1413 setup = (struct usb_ctrlrequest *)urb->setup_packet;
1414 windex = le16_to_cpu(setup->wIndex);
1415 wvalue = le16_to_cpu(setup->wValue);
1416 wlength = le16_to_cpu(setup->wLength);
1417 oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
1418 oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1419 oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
1420 oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
1421 oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
1423 req_id = port->next_req_id++;
1424 hpd = oz_claim_hpd(port);
1426 oz_dbg(ON, "Cannot claim port\n");
1431 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1432 /* Standard requests
1434 switch (setup->bRequest) {
1435 case USB_REQ_GET_DESCRIPTOR:
1436 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
1438 case USB_REQ_SET_ADDRESS:
1439 oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
1440 oz_dbg(ON, "Port %d address is 0x%x\n",
1442 (u8)le16_to_cpu(setup->wValue));
1443 spin_lock_bh(&ozhcd->hcd_lock);
1444 if (ozhcd->conn_port >= 0) {
1445 ozhcd->ports[ozhcd->conn_port].bus_addr =
1446 (u8)le16_to_cpu(setup->wValue);
1447 oz_dbg(ON, "Clearing conn_port\n");
1448 ozhcd->conn_port = -1;
1450 spin_unlock_bh(&ozhcd->hcd_lock);
1453 case USB_REQ_SET_CONFIGURATION:
1454 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
1456 case USB_REQ_GET_CONFIGURATION:
1457 /* We short circuit this case and reply directly since
1458 * we have the selected configuration number cached.
1460 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
1461 if (urb->transfer_buffer_length >= 1) {
1462 urb->actual_length = 1;
1463 *((u8 *)urb->transfer_buffer) =
1470 case USB_REQ_GET_INTERFACE:
1471 /* We short circuit this case and reply directly since
1472 * we have the selected interface alternative cached.
1474 oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
1475 if (urb->transfer_buffer_length >= 1) {
1476 urb->actual_length = 1;
1477 *((u8 *)urb->transfer_buffer) =
1478 port->iface[(u8)windex].alt;
1479 oz_dbg(ON, "interface = %d alt = %d\n",
1480 windex, port->iface[(u8)windex].alt);
1486 case USB_REQ_SET_INTERFACE:
1487 oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
1491 if (!rc && !complete) {
1494 if ((setup->bRequestType & USB_DIR_IN) == 0)
1496 urb->actual_length = data_len;
1497 if (oz_usb_control_req(port->hpd, req_id, setup,
1498 urb->transfer_buffer, data_len)) {
1501 /* Note: we are queuing the request after we have
1502 * submitted it to be transmitted. If the request were
1503 * to complete before we queued it then it would not
1504 * be found in the queue. It seems impossible for
1505 * this to happen but if it did the request would
1506 * be resubmitted so the problem would hopefully
1507 * resolve itself. Putting the request into the
1508 * queue before it has been sent is worse since the
1509 * urb could be cancelled while we are using it
1510 * to build the request.
1512 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1518 if (rc || complete) {
1519 oz_dbg(ON, "Completing request locally\n");
1520 oz_complete_urb(ozhcd->hcd, urb, rc);
1522 oz_usb_request_heartbeat(port->hpd);
1529 static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1532 struct oz_port *port = urb->hcpriv;
1535 /* When we are paranoid we keep a list of urbs which we check against
1536 * before handing one back. This is just for debugging during
1537 * development and should be turned off in the released driver.
1539 oz_remember_urb(urb);
1540 /* Check buffer is valid.
1542 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1544 /* Check if there is a device at the port - refuse if not.
1546 if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1548 ep_addr = usb_pipeendpoint(urb->pipe);
1550 /* If the request is not for EP0 then queue it.
1552 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1556 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1564 static void oz_urb_process_tasklet(unsigned long unused)
1566 unsigned long irq_state;
1568 struct oz_hcd *ozhcd = oz_hcd_claim();
1569 struct oz_urb_link *urbl, *n;
1574 /* This is called from a tasklet so is in softirq context but the urb
1575 * list is filled from any context so we need to lock
1576 * appropriately while removing urbs.
1578 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1579 list_for_each_entry_safe(urbl, n, &ozhcd->urb_pending_list, link) {
1580 list_del_init(&urbl->link);
1581 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1583 oz_free_urb_link(urbl);
1584 rc = oz_urb_process(ozhcd, urb);
1586 oz_complete_urb(ozhcd->hcd, urb, rc);
1587 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1589 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1594 * This function searches for the urb in any of the lists it could be in.
1595 * If it is found it is removed from the list and completed. If the urb is
1596 * being processed then it won't be in a list so won't be found. However, the
1597 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1598 * to a non-zero value. When an attempt is made to put the urb back in a list
1599 * the unlinked field will be checked and the urb will then be completed.
1602 static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1604 struct oz_urb_link *urbl = NULL;
1605 struct list_head *e;
1606 struct oz_hcd *ozhcd;
1607 unsigned long irq_state;
1611 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
1614 ozhcd = port->ozhcd;
1615 if (ozhcd == NULL) {
1616 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
1620 /* Look in the tasklet queue.
1622 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1623 list_for_each(e, &ozhcd->urb_cancel_list) {
1624 urbl = list_entry(e, struct oz_urb_link, link);
1625 if (urb == urbl->urb) {
1627 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1631 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1634 /* Look in the orphanage.
1636 spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1637 list_for_each(e, &ozhcd->orphanage) {
1638 urbl = list_entry(e, struct oz_urb_link, link);
1639 if (urbl->urb == urb) {
1641 oz_dbg(ON, "Found urb in orphanage\n");
1645 ix = (ep_num & 0xf);
1647 if ((ep_num & USB_DIR_IN) && ix)
1648 urbl = oz_remove_urb(port->in_ep[ix], urb);
1650 urbl = oz_remove_urb(port->out_ep[ix], urb);
1652 spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1655 urb->actual_length = 0;
1656 oz_free_urb_link(urbl);
1657 oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
1664 static void oz_urb_cancel_tasklet(unsigned long unused)
1666 unsigned long irq_state;
1668 struct oz_urb_link *urbl, *n;
1669 struct oz_hcd *ozhcd = oz_hcd_claim();
1673 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1674 list_for_each_entry_safe(urbl, n, &ozhcd->urb_cancel_list, link) {
1675 list_del_init(&urbl->link);
1676 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1679 oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1680 oz_free_urb_link(urbl);
1681 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1683 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1690 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1693 struct oz_urb_link *urbl, *n;
1695 list_for_each_entry_safe(urbl, n, &ozhcd->orphanage, link) {
1696 list_del(&urbl->link);
1697 oz_complete_urb(ozhcd->hcd, urbl->urb, status);
1698 oz_free_urb_link(urbl);
1706 static int oz_hcd_start(struct usb_hcd *hcd)
1708 hcd->power_budget = 200;
1709 hcd->state = HC_STATE_RUNNING;
1710 hcd->uses_new_polling = 1;
1717 static void oz_hcd_stop(struct usb_hcd *hcd)
1724 static void oz_hcd_shutdown(struct usb_hcd *hcd)
1729 * Called to queue an urb for the device.
1730 * This function should return a non-zero error code if it fails the urb but
1731 * should not call usb_hcd_giveback_urb().
1734 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1737 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1740 struct oz_port *port;
1741 unsigned long irq_state;
1742 struct oz_urb_link *urbl;
1744 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1745 if (unlikely(ozhcd == NULL)) {
1746 oz_dbg(URB, "Refused urb(%p) not ozhcd\n", urb);
1749 if (unlikely(hcd->state != HC_STATE_RUNNING)) {
1750 oz_dbg(URB, "Refused urb(%p) not running\n", urb);
1753 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1756 port = &ozhcd->ports[port_ix];
1759 if (!(port->flags & OZ_PORT_F_PRESENT) ||
1760 (port->flags & OZ_PORT_F_CHANGED)) {
1761 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1762 port_ix, urb->dev->devnum);
1766 /* Put request in queue for processing by tasklet.
1768 urbl = oz_alloc_urb_link();
1769 if (unlikely(urbl == NULL))
1772 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1773 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1775 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1776 oz_free_urb_link(urbl);
1779 list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1780 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1781 tasklet_schedule(&g_urb_process_tasklet);
1782 atomic_inc(&g_pending_urbs);
1789 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1792 struct oz_urb_link *urbl;
1794 if (unlikely(ep == NULL))
1797 list_for_each_entry(urbl, &ep->urb_list, link) {
1798 if (urbl->urb == urb) {
1799 list_del_init(&urbl->link);
1800 if (usb_pipeisoc(urb->pipe)) {
1801 ep->credit -= urb->number_of_packets;
1812 * Called to dequeue a previously submitted urb for the device.
1815 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1817 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1818 struct oz_urb_link *urbl;
1820 unsigned long irq_state;
1822 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1823 urbl = oz_alloc_urb_link();
1824 if (unlikely(urbl == NULL))
1826 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1827 /* The following function checks the urb is still in the queue
1828 * maintained by the core and that the unlinked field is zero.
1829 * If both are true the function sets the unlinked field and returns
1830 * zero. Otherwise it returns an error.
1832 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1833 /* We have to check we haven't completed the urb or are about
1834 * to complete it. When we do we set hcpriv to 0 so if this has
1835 * already happened we don't put the urb in the cancel queue.
1837 if ((rc == 0) && urb->hcpriv) {
1839 urbl->port = (struct oz_port *)urb->hcpriv;
1840 urbl->ep_num = usb_pipeendpoint(urb->pipe);
1841 if (usb_pipein(urb->pipe))
1842 urbl->ep_num |= USB_DIR_IN;
1843 list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1844 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1845 tasklet_schedule(&g_urb_cancel_tasklet);
1847 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1848 oz_free_urb_link(urbl);
1856 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1857 struct usb_host_endpoint *ep)
1864 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1865 struct usb_host_endpoint *ep)
1872 static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1874 oz_dbg(ON, "oz_hcd_get_frame_number\n");
1875 return oz_usb_get_frame_number();
1880 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1881 * always do that in softirq context.
1883 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1885 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1891 spin_lock_bh(&ozhcd->hcd_lock);
1892 for (i = 0; i < OZ_NB_PORTS; i++) {
1893 if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
1894 oz_dbg(HUB, "Port %d changed\n", i);
1895 ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
1897 buf[0] |= 1 << (i + 1);
1899 buf[1] |= 1 << (i - 7);
1902 spin_unlock_bh(&ozhcd->hcd_lock);
1903 if (buf[0] != 0 || buf[1] != 0)
1911 static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1912 struct usb_hub_descriptor *desc)
1914 memset(desc, 0, sizeof(*desc));
1915 desc->bDescriptorType = 0x29;
1916 desc->bDescLength = 9;
1917 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1918 desc->bNbrPorts = OZ_NB_PORTS;
1924 static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1926 struct oz_port *port;
1927 u8 port_id = (u8)windex;
1928 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1929 unsigned set_bits = 0;
1930 unsigned clear_bits = 0;
1932 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
1934 port = &ozhcd->ports[port_id-1];
1936 case USB_PORT_FEAT_CONNECTION:
1937 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
1939 case USB_PORT_FEAT_ENABLE:
1940 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
1942 case USB_PORT_FEAT_SUSPEND:
1943 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
1945 case USB_PORT_FEAT_OVER_CURRENT:
1946 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
1948 case USB_PORT_FEAT_RESET:
1949 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
1950 set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
1951 clear_bits = USB_PORT_STAT_RESET;
1952 ozhcd->ports[port_id-1].bus_addr = 0;
1954 case USB_PORT_FEAT_POWER:
1955 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
1956 set_bits |= USB_PORT_STAT_POWER;
1958 case USB_PORT_FEAT_LOWSPEED:
1959 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
1961 case USB_PORT_FEAT_C_CONNECTION:
1962 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
1964 case USB_PORT_FEAT_C_ENABLE:
1965 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
1967 case USB_PORT_FEAT_C_SUSPEND:
1968 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
1970 case USB_PORT_FEAT_C_OVER_CURRENT:
1971 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
1973 case USB_PORT_FEAT_C_RESET:
1974 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
1976 case USB_PORT_FEAT_TEST:
1977 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
1979 case USB_PORT_FEAT_INDICATOR:
1980 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
1983 oz_dbg(HUB, "Other %d\n", wvalue);
1986 if (set_bits || clear_bits) {
1987 spin_lock_bh(&port->port_lock);
1988 port->status &= ~clear_bits;
1989 port->status |= set_bits;
1990 spin_unlock_bh(&port->port_lock);
1992 oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
1999 static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
2001 struct oz_port *port;
2002 u8 port_id = (u8)windex;
2003 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2004 unsigned clear_bits = 0;
2006 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2008 port = &ozhcd->ports[port_id-1];
2010 case USB_PORT_FEAT_CONNECTION:
2011 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
2013 case USB_PORT_FEAT_ENABLE:
2014 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
2015 clear_bits = USB_PORT_STAT_ENABLE;
2017 case USB_PORT_FEAT_SUSPEND:
2018 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
2020 case USB_PORT_FEAT_OVER_CURRENT:
2021 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2023 case USB_PORT_FEAT_RESET:
2024 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
2026 case USB_PORT_FEAT_POWER:
2027 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2028 clear_bits |= USB_PORT_STAT_POWER;
2030 case USB_PORT_FEAT_LOWSPEED:
2031 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2033 case USB_PORT_FEAT_C_CONNECTION:
2034 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2035 clear_bits = USB_PORT_STAT_C_CONNECTION << 16;
2037 case USB_PORT_FEAT_C_ENABLE:
2038 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2039 clear_bits = USB_PORT_STAT_C_ENABLE << 16;
2041 case USB_PORT_FEAT_C_SUSPEND:
2042 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2044 case USB_PORT_FEAT_C_OVER_CURRENT:
2045 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2047 case USB_PORT_FEAT_C_RESET:
2048 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2049 clear_bits = USB_PORT_FEAT_C_RESET << 16;
2051 case USB_PORT_FEAT_TEST:
2052 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2054 case USB_PORT_FEAT_INDICATOR:
2055 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2058 oz_dbg(HUB, "Other %d\n", wvalue);
2062 spin_lock_bh(&port->port_lock);
2063 port->status &= ~clear_bits;
2064 spin_unlock_bh(&port->port_lock);
2066 oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2067 port_id, ozhcd->ports[port_id-1].status);
2074 static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2076 struct oz_hcd *ozhcd;
2079 if ((windex < 1) || (windex > OZ_NB_PORTS))
2081 ozhcd = oz_hcd_private(hcd);
2082 oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
2083 status = ozhcd->ports[windex-1].status;
2084 put_unaligned(cpu_to_le32(status), (__le32 *)buf);
2085 oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
2092 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2093 u16 windex, char *buf, u16 wlength)
2098 case ClearHubFeature:
2099 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
2101 case ClearPortFeature:
2102 err = oz_clear_port_feature(hcd, wvalue, windex);
2104 case GetHubDescriptor:
2105 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2108 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
2109 put_unaligned(cpu_to_le32(0), (__le32 *)buf);
2112 err = oz_get_port_status(hcd, windex, buf);
2115 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
2117 case SetPortFeature:
2118 err = oz_set_port_feature(hcd, wvalue, windex);
2121 oz_dbg(HUB, "Other: %d\n", req_type);
2130 static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2132 struct oz_hcd *ozhcd;
2134 ozhcd = oz_hcd_private(hcd);
2135 spin_lock_bh(&ozhcd->hcd_lock);
2136 hcd->state = HC_STATE_SUSPENDED;
2137 ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2138 spin_unlock_bh(&ozhcd->hcd_lock);
2145 static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2147 struct oz_hcd *ozhcd;
2149 ozhcd = oz_hcd_private(hcd);
2150 spin_lock_bh(&ozhcd->hcd_lock);
2151 ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2152 hcd->state = HC_STATE_RUNNING;
2153 spin_unlock_bh(&ozhcd->hcd_lock);
2157 static void oz_plat_shutdown(struct platform_device *dev)
2164 static int oz_plat_probe(struct platform_device *dev)
2168 struct usb_hcd *hcd;
2169 struct oz_hcd *ozhcd;
2171 hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
2173 oz_dbg(ON, "Failed to created hcd object OK\n");
2176 ozhcd = oz_hcd_private(hcd);
2177 memset(ozhcd, 0, sizeof(*ozhcd));
2178 INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2179 INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2180 INIT_LIST_HEAD(&ozhcd->orphanage);
2182 ozhcd->conn_port = -1;
2183 spin_lock_init(&ozhcd->hcd_lock);
2184 for (i = 0; i < OZ_NB_PORTS; i++) {
2185 struct oz_port *port = &ozhcd->ports[i];
2187 port->ozhcd = ozhcd;
2190 port->bus_addr = 0xff;
2191 spin_lock_init(&port->port_lock);
2193 err = usb_add_hcd(hcd, 0, 0);
2195 oz_dbg(ON, "Failed to add hcd object OK\n");
2199 device_wakeup_enable(hcd->self.controller);
2201 spin_lock_bh(&g_hcdlock);
2203 spin_unlock_bh(&g_hcdlock);
2210 static int oz_plat_remove(struct platform_device *dev)
2212 struct usb_hcd *hcd = platform_get_drvdata(dev);
2213 struct oz_hcd *ozhcd;
2217 ozhcd = oz_hcd_private(hcd);
2218 spin_lock_bh(&g_hcdlock);
2219 if (ozhcd == g_ozhcd)
2221 spin_unlock_bh(&g_hcdlock);
2222 oz_dbg(ON, "Clearing orphanage\n");
2223 oz_hcd_clear_orphanage(ozhcd, -EPIPE);
2224 oz_dbg(ON, "Removing hcd\n");
2225 usb_remove_hcd(hcd);
2233 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2242 static int oz_plat_resume(struct platform_device *dev)
2250 int oz_hcd_init(void)
2257 oz_urb_link_cache = KMEM_CACHE(oz_urb_link, 0);
2258 if (!oz_urb_link_cache)
2261 tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
2262 tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
2263 err = platform_driver_register(&g_oz_plat_drv);
2264 oz_dbg(ON, "platform_driver_register() returned %d\n", err);
2267 g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
2268 if (g_plat_dev == NULL) {
2272 oz_dbg(ON, "platform_device_alloc() succeeded\n");
2273 err = platform_device_add(g_plat_dev);
2276 oz_dbg(ON, "platform_device_add() succeeded\n");
2279 platform_device_put(g_plat_dev);
2281 platform_driver_unregister(&g_oz_plat_drv);
2283 tasklet_disable(&g_urb_process_tasklet);
2284 tasklet_disable(&g_urb_cancel_tasklet);
2285 oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
2292 void oz_hcd_term(void)
2294 msleep(OZ_HUB_DEBOUNCE_TIMEOUT);
2295 tasklet_kill(&g_urb_process_tasklet);
2296 tasklet_kill(&g_urb_cancel_tasklet);
2297 platform_device_unregister(g_plat_dev);
2298 platform_driver_unregister(&g_oz_plat_drv);
2299 oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));
2300 kmem_cache_destroy(oz_urb_link_cache);