1 /* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
5 * This file provides the implementation of a USB host controller device that
6 * does not have any associated hardware. Instead the virtual device is
7 * connected to the WiFi network and emulates the operation of a USB hcd by
8 * receiving and sending network frames.
10 * We take great pains to reduce the amount of code where interrupts need to be
11 * disabled and in this respect we are different from standard HCD's. In
12 * particular we don't want in_irq() code bleeding over to the protocol side of
14 * The troublesome functions are the urb enqueue and dequeue functions both of
15 * which can be called in_irq(). So for these functions we put the urbs into a
16 * queue and request a tasklet to process them. This means that a spinlock with
17 * interrupts disabled must be held for insertion and removal but most code is
18 * is in tasklet or soft irq context. The lock that protects this list is called
19 * the tasklet lock and serves the purpose of the 'HCD lock' which must be held
20 * when calling the following functions.
21 * usb_hcd_link_urb_to_ep()
22 * usb_hcd_unlink_urb_from_ep()
23 * usb_hcd_flush_endpoint()
24 * usb_hcd_check_unlink_urb()
25 * -----------------------------------------------------------------------------
27 #include <linux/platform_device.h>
28 #include <linux/usb.h>
29 #include <linux/slab.h>
30 #include <linux/export.h>
31 #include "linux/usb/hcd.h"
32 #include <asm/unaligned.h>
35 #include "ozurbparanoia.h"
39 * Number of units of buffering to capture for an isochronous IN endpoint before
40 * allowing data to be indicated up.
42 #define OZ_IN_BUFFERING_UNITS 100
44 /* Name of our platform device.
46 #define OZ_PLAT_DEV_NAME "ozwpan"
48 /* Maximum number of free urb links that can be kept in the pool.
50 #define OZ_MAX_LINK_POOL_SIZE 16
52 /* Get endpoint object from the containing link.
54 #define ep_from_link(__e) container_of((__e), struct oz_endpoint, link)
56 /*EP0 timeout before ep0 request is again added to TX queue. (13*8 = 98mSec)
58 #define EP0_TIMEOUT_COUNTER 13
60 /* Debounce time HCD driver should wait before unregistering.
62 #define OZ_HUB_DEBOUNCE_TIMEOUT 1500
65 * Used to link urbs together and also store some status information for each
67 * A cache of these are kept in a pool to reduce number of calls to kmalloc.
70 struct list_head link
;
75 unsigned submit_counter
;
78 /* Holds state information about a USB endpoint.
80 #define OZ_EP_BUFFER_SIZE_ISOC (1024 * 24)
81 #define OZ_EP_BUFFER_SIZE_INT 512
83 struct list_head urb_list
; /* List of oz_urb_link items. */
84 struct list_head link
; /* For isoc ep, links in to isoc
86 struct timespec timestamp
;
100 /* Bits in the flags field. */
101 #define OZ_F_EP_BUFFERING 0x1
102 #define OZ_F_EP_HAVE_STREAM 0x2
104 /* Holds state information about a USB interface.
106 struct oz_interface
{
111 /* Holds state information about an hcd port.
113 #define OZ_NB_ENDPOINTS 16
118 struct oz_hcd
*ozhcd
;
119 spinlock_t port_lock
;
124 struct oz_interface
*iface
;
125 struct oz_endpoint
*out_ep
[OZ_NB_ENDPOINTS
];
126 struct oz_endpoint
*in_ep
[OZ_NB_ENDPOINTS
];
127 struct list_head isoc_out_ep
;
128 struct list_head isoc_in_ep
;
131 #define OZ_PORT_F_PRESENT 0x1
132 #define OZ_PORT_F_CHANGED 0x2
133 #define OZ_PORT_F_DYING 0x4
135 /* Data structure in the private context area of struct usb_hcd.
137 #define OZ_NB_PORTS 8
140 struct list_head urb_pending_list
;
141 struct list_head urb_cancel_list
;
142 struct list_head orphanage
;
143 int conn_port
; /* Port that is currently connecting, -1 if none.*/
144 struct oz_port ports
[OZ_NB_PORTS
];
149 /* Bits in flags field.
151 #define OZ_HDC_F_SUSPENDED 0x1
154 * Static function prototypes.
156 static int oz_hcd_start(struct usb_hcd
*hcd
);
157 static void oz_hcd_stop(struct usb_hcd
*hcd
);
158 static void oz_hcd_shutdown(struct usb_hcd
*hcd
);
159 static int oz_hcd_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
161 static int oz_hcd_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
);
162 static void oz_hcd_endpoint_disable(struct usb_hcd
*hcd
,
163 struct usb_host_endpoint
*ep
);
164 static void oz_hcd_endpoint_reset(struct usb_hcd
*hcd
,
165 struct usb_host_endpoint
*ep
);
166 static int oz_hcd_get_frame_number(struct usb_hcd
*hcd
);
167 static int oz_hcd_hub_status_data(struct usb_hcd
*hcd
, char *buf
);
168 static int oz_hcd_hub_control(struct usb_hcd
*hcd
, u16 req_type
, u16 wvalue
,
169 u16 windex
, char *buf
, u16 wlength
);
170 static int oz_hcd_bus_suspend(struct usb_hcd
*hcd
);
171 static int oz_hcd_bus_resume(struct usb_hcd
*hcd
);
172 static int oz_plat_probe(struct platform_device
*dev
);
173 static int oz_plat_remove(struct platform_device
*dev
);
174 static void oz_plat_shutdown(struct platform_device
*dev
);
175 static int oz_plat_suspend(struct platform_device
*dev
, pm_message_t msg
);
176 static int oz_plat_resume(struct platform_device
*dev
);
177 static void oz_urb_process_tasklet(unsigned long unused
);
178 static int oz_build_endpoints_for_config(struct usb_hcd
*hcd
,
179 struct oz_port
*port
, struct usb_host_config
*config
,
181 static void oz_clean_endpoints_for_config(struct usb_hcd
*hcd
,
182 struct oz_port
*port
);
183 static int oz_build_endpoints_for_interface(struct usb_hcd
*hcd
,
184 struct oz_port
*port
,
185 struct usb_host_interface
*intf
, gfp_t mem_flags
);
186 static void oz_clean_endpoints_for_interface(struct usb_hcd
*hcd
,
187 struct oz_port
*port
, int if_ix
);
188 static void oz_process_ep0_urb(struct oz_hcd
*ozhcd
, struct urb
*urb
,
190 static struct oz_urb_link
*oz_remove_urb(struct oz_endpoint
*ep
,
192 static void oz_hcd_clear_orphanage(struct oz_hcd
*ozhcd
, int status
);
195 * Static external variables.
197 static struct platform_device
*g_plat_dev
;
198 static struct oz_hcd
*g_ozhcd
;
199 static DEFINE_SPINLOCK(g_hcdlock
); /* Guards g_ozhcd. */
200 static const char g_hcd_name
[] = "Ozmo WPAN";
201 static struct list_head
*g_link_pool
;
202 static int g_link_pool_size
;
203 static DEFINE_SPINLOCK(g_link_lock
);
204 static DEFINE_SPINLOCK(g_tasklet_lock
);
205 static struct tasklet_struct g_urb_process_tasklet
;
206 static struct tasklet_struct g_urb_cancel_tasklet
;
207 static atomic_t g_pending_urbs
= ATOMIC_INIT(0);
208 static atomic_t g_usb_frame_number
= ATOMIC_INIT(0);
209 static const struct hc_driver g_oz_hc_drv
= {
210 .description
= g_hcd_name
,
211 .product_desc
= "Ozmo Devices WPAN",
212 .hcd_priv_size
= sizeof(struct oz_hcd
),
214 .start
= oz_hcd_start
,
216 .shutdown
= oz_hcd_shutdown
,
217 .urb_enqueue
= oz_hcd_urb_enqueue
,
218 .urb_dequeue
= oz_hcd_urb_dequeue
,
219 .endpoint_disable
= oz_hcd_endpoint_disable
,
220 .endpoint_reset
= oz_hcd_endpoint_reset
,
221 .get_frame_number
= oz_hcd_get_frame_number
,
222 .hub_status_data
= oz_hcd_hub_status_data
,
223 .hub_control
= oz_hcd_hub_control
,
224 .bus_suspend
= oz_hcd_bus_suspend
,
225 .bus_resume
= oz_hcd_bus_resume
,
228 static struct platform_driver g_oz_plat_drv
= {
229 .probe
= oz_plat_probe
,
230 .remove
= oz_plat_remove
,
231 .shutdown
= oz_plat_shutdown
,
232 .suspend
= oz_plat_suspend
,
233 .resume
= oz_plat_resume
,
235 .name
= OZ_PLAT_DEV_NAME
,
236 .owner
= THIS_MODULE
,
241 * Gets our private context area (which is of type struct oz_hcd) from the
245 static inline struct oz_hcd
*oz_hcd_private(struct usb_hcd
*hcd
)
247 return (struct oz_hcd
*)hcd
->hcd_priv
;
251 * Searches list of ports to find the index of the one with a specified USB
252 * bus address. If none of the ports has the bus address then the connection
253 * port is returned, if there is one or -1 otherwise.
256 static int oz_get_port_from_addr(struct oz_hcd
*ozhcd
, u8 bus_addr
)
260 for (i
= 0; i
< OZ_NB_PORTS
; i
++) {
261 if (ozhcd
->ports
[i
].bus_addr
== bus_addr
)
264 return ozhcd
->conn_port
;
268 * Allocates an urb link, first trying the pool but going to heap if empty.
271 static struct oz_urb_link
*oz_alloc_urb_link(void)
273 struct oz_urb_link
*urbl
= NULL
;
274 unsigned long irq_state
;
276 spin_lock_irqsave(&g_link_lock
, irq_state
);
278 urbl
= container_of(g_link_pool
, struct oz_urb_link
, link
);
279 g_link_pool
= urbl
->link
.next
;
282 spin_unlock_irqrestore(&g_link_lock
, irq_state
);
284 urbl
= kmalloc(sizeof(struct oz_urb_link
), GFP_ATOMIC
);
289 * Frees an urb link by putting it in the pool if there is enough space or
290 * deallocating it to heap otherwise.
293 static void oz_free_urb_link(struct oz_urb_link
*urbl
)
296 unsigned long irq_state
;
297 spin_lock_irqsave(&g_link_lock
, irq_state
);
298 if (g_link_pool_size
< OZ_MAX_LINK_POOL_SIZE
) {
299 urbl
->link
.next
= g_link_pool
;
300 g_link_pool
= &urbl
->link
;
304 spin_unlock_irqrestore(&g_link_lock
, irq_state
);
310 * Deallocates all the urb links in the pool.
313 static void oz_empty_link_pool(void)
316 unsigned long irq_state
;
318 spin_lock_irqsave(&g_link_lock
, irq_state
);
321 g_link_pool_size
= 0;
322 spin_unlock_irqrestore(&g_link_lock
, irq_state
);
324 struct oz_urb_link
*urbl
=
325 container_of(e
, struct oz_urb_link
, link
);
332 * Allocates endpoint structure and optionally a buffer. If a buffer is
333 * allocated it immediately follows the endpoint structure.
336 static struct oz_endpoint
*oz_ep_alloc(int buffer_size
, gfp_t mem_flags
)
338 struct oz_endpoint
*ep
=
339 kzalloc(sizeof(struct oz_endpoint
)+buffer_size
, mem_flags
);
341 INIT_LIST_HEAD(&ep
->urb_list
);
342 INIT_LIST_HEAD(&ep
->link
);
345 ep
->buffer_size
= buffer_size
;
346 ep
->buffer
= (u8
*)(ep
+1);
353 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
355 * Context: softirq or process
357 static struct oz_urb_link
*oz_uncancel_urb(struct oz_hcd
*ozhcd
,
360 struct oz_urb_link
*urbl
;
363 list_for_each(e
, &ozhcd
->urb_cancel_list
) {
364 urbl
= container_of(e
, struct oz_urb_link
, link
);
365 if (urb
== urbl
->urb
) {
374 * This is called when we have finished processing an urb. It unlinks it from
375 * the ep and returns it to the core.
376 * Context: softirq or process
378 static void oz_complete_urb(struct usb_hcd
*hcd
, struct urb
*urb
,
381 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
382 unsigned long irq_state
;
383 struct oz_urb_link
*cancel_urbl
;
385 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
386 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
387 /* Clear hcpriv which will prevent it being put in the cancel list
388 * in the event that an attempt is made to cancel it.
391 /* Walk the cancel list in case the urb is already sitting there.
392 * Since we process the cancel list in a tasklet rather than in
393 * the dequeue function this could happen.
395 cancel_urbl
= oz_uncancel_urb(ozhcd
, urb
);
396 /* Note: we release lock but do not enable local irqs.
397 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
398 * or at least other host controllers disable interrupts at this point
399 * so we do the same. We must, however, release the lock otherwise a
400 * deadlock will occur if an urb is submitted to our driver in the urb
401 * completion function. Because we disable interrupts it is possible
402 * that the urb_enqueue function can be called with them disabled.
404 spin_unlock(&g_tasklet_lock
);
405 if (oz_forget_urb(urb
)) {
406 oz_dbg(ON
, "ERROR Unknown URB %p\n", urb
);
408 atomic_dec(&g_pending_urbs
);
409 usb_hcd_giveback_urb(hcd
, urb
, status
);
411 spin_lock(&g_tasklet_lock
);
412 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
414 oz_free_urb_link(cancel_urbl
);
418 * Deallocates an endpoint including deallocating any associated stream and
419 * returning any queued urbs to the core.
422 static void oz_ep_free(struct oz_port
*port
, struct oz_endpoint
*ep
)
425 struct list_head list
;
426 struct oz_hcd
*ozhcd
= port
->ozhcd
;
427 INIT_LIST_HEAD(&list
);
428 if (ep
->flags
& OZ_F_EP_HAVE_STREAM
)
429 oz_usb_stream_delete(port
->hpd
, ep
->ep_num
);
430 /* Transfer URBs to the orphanage while we hold the lock. */
431 spin_lock_bh(&ozhcd
->hcd_lock
);
432 /* Note: this works even if ep->urb_list is empty.*/
433 list_replace_init(&ep
->urb_list
, &list
);
434 /* Put the URBs in the orphanage. */
435 list_splice_tail(&list
, &ozhcd
->orphanage
);
436 spin_unlock_bh(&ozhcd
->hcd_lock
);
438 oz_dbg(ON
, "Freeing endpoint memory\n");
445 static void oz_complete_buffered_urb(struct oz_port
*port
,
446 struct oz_endpoint
*ep
,
449 int data_len
, available_space
, copy_len
;
451 data_len
= ep
->buffer
[ep
->out_ix
];
452 if (data_len
<= urb
->transfer_buffer_length
)
453 available_space
= data_len
;
455 available_space
= urb
->transfer_buffer_length
;
457 if (++ep
->out_ix
== ep
->buffer_size
)
459 copy_len
= ep
->buffer_size
- ep
->out_ix
;
460 if (copy_len
>= available_space
)
461 copy_len
= available_space
;
462 memcpy(urb
->transfer_buffer
, &ep
->buffer
[ep
->out_ix
], copy_len
);
464 if (copy_len
< available_space
) {
465 memcpy((urb
->transfer_buffer
+ copy_len
), ep
->buffer
,
466 (available_space
- copy_len
));
467 ep
->out_ix
= available_space
- copy_len
;
469 ep
->out_ix
+= copy_len
;
471 urb
->actual_length
= available_space
;
472 if (ep
->out_ix
== ep
->buffer_size
)
475 ep
->buffered_units
--;
476 oz_dbg(ON
, "Trying to give back buffered frame of size=%d\n",
478 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
484 static int oz_enqueue_ep_urb(struct oz_port
*port
, u8 ep_addr
, int in_dir
,
485 struct urb
*urb
, u8 req_id
)
487 struct oz_urb_link
*urbl
;
488 struct oz_endpoint
*ep
= NULL
;
491 if (ep_addr
>= OZ_NB_ENDPOINTS
) {
492 oz_dbg(ON
, "%s: Invalid endpoint number\n", __func__
);
495 urbl
= oz_alloc_urb_link();
498 urbl
->submit_counter
= 0;
500 urbl
->req_id
= req_id
;
501 urbl
->ep_num
= ep_addr
;
502 /* Hold lock while we insert the URB into the list within the
503 * endpoint structure.
505 spin_lock_bh(&port
->ozhcd
->hcd_lock
);
506 /* If the urb has been unlinked while out of any list then
510 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
511 oz_dbg(ON
, "urb %p unlinked so complete immediately\n", urb
);
512 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
513 oz_free_urb_link(urbl
);
518 ep
= port
->in_ep
[ep_addr
];
520 ep
= port
->out_ep
[ep_addr
];
526 /*For interrupt endpoint check for buffered data
529 if (((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
530 && ep
->buffered_units
> 0) {
531 oz_free_urb_link(urbl
);
532 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
533 oz_complete_buffered_urb(port
, ep
, urb
);
538 list_add_tail(&urbl
->link
, &ep
->urb_list
);
539 if (!in_dir
&& ep_addr
&& (ep
->credit
< 0)) {
540 getrawmonotonic(&ep
->timestamp
);
547 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
549 oz_free_urb_link(urbl
);
554 * Removes an urb from the queue in the endpoint.
555 * Returns 0 if it is found and -EIDRM otherwise.
558 static int oz_dequeue_ep_urb(struct oz_port
*port
, u8 ep_addr
, int in_dir
,
561 struct oz_urb_link
*urbl
= NULL
;
562 struct oz_endpoint
*ep
;
564 spin_lock_bh(&port
->ozhcd
->hcd_lock
);
566 ep
= port
->in_ep
[ep_addr
];
568 ep
= port
->out_ep
[ep_addr
];
571 list_for_each(e
, &ep
->urb_list
) {
572 urbl
= container_of(e
, struct oz_urb_link
, link
);
573 if (urbl
->urb
== urb
) {
580 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
582 oz_free_urb_link(urbl
);
583 return urbl
? 0 : -EIDRM
;
587 * Finds an urb given its request id.
590 static struct urb
*oz_find_urb_by_id(struct oz_port
*port
, int ep_ix
,
593 struct oz_hcd
*ozhcd
= port
->ozhcd
;
594 struct urb
*urb
= NULL
;
595 struct oz_urb_link
*urbl
;
596 struct oz_endpoint
*ep
;
598 spin_lock_bh(&ozhcd
->hcd_lock
);
599 ep
= port
->out_ep
[ep_ix
];
602 list_for_each(e
, &ep
->urb_list
) {
603 urbl
= container_of(e
, struct oz_urb_link
, link
);
604 if (urbl
->req_id
== req_id
) {
611 spin_unlock_bh(&ozhcd
->hcd_lock
);
612 /* If urb is non-zero then we we must have an urb link to delete.
615 oz_free_urb_link(urbl
);
620 * Pre-condition: Port lock must be held.
623 static void oz_acquire_port(struct oz_port
*port
, void *hpd
)
625 INIT_LIST_HEAD(&port
->isoc_out_ep
);
626 INIT_LIST_HEAD(&port
->isoc_in_ep
);
627 port
->flags
|= OZ_PORT_F_PRESENT
| OZ_PORT_F_CHANGED
;
628 port
->status
|= USB_PORT_STAT_CONNECTION
|
629 (USB_PORT_STAT_C_CONNECTION
<< 16);
637 static struct oz_hcd
*oz_hcd_claim(void)
639 struct oz_hcd
*ozhcd
;
641 spin_lock_bh(&g_hcdlock
);
644 usb_get_hcd(ozhcd
->hcd
);
645 spin_unlock_bh(&g_hcdlock
);
652 static inline void oz_hcd_put(struct oz_hcd
*ozhcd
)
655 usb_put_hcd(ozhcd
->hcd
);
659 * This is called by the protocol handler to notify that a PD has arrived.
660 * We allocate a port to associate with the PD and create a structure for
661 * endpoint 0. This port is made the connection port.
662 * In the event that one of the other port is already a connection port then
664 * TODO We should be able to do better than fail and should be able remember
665 * that this port needs configuring and make it the connection port once the
666 * current connection port has been assigned an address. Collisions here are
667 * probably very rare indeed.
670 struct oz_port
*oz_hcd_pd_arrived(void *hpd
)
673 struct oz_port
*hport
;
674 struct oz_hcd
*ozhcd
;
675 struct oz_endpoint
*ep
;
677 ozhcd
= oz_hcd_claim();
680 /* Allocate an endpoint object in advance (before holding hcd lock) to
681 * use for out endpoint 0.
683 ep
= oz_ep_alloc(0, GFP_ATOMIC
);
687 spin_lock_bh(&ozhcd
->hcd_lock
);
688 if (ozhcd
->conn_port
>= 0)
691 for (i
= 0; i
< OZ_NB_PORTS
; i
++) {
692 struct oz_port
*port
= &ozhcd
->ports
[i
];
694 spin_lock(&port
->port_lock
);
695 if (!(port
->flags
& (OZ_PORT_F_PRESENT
| OZ_PORT_F_CHANGED
))) {
696 oz_acquire_port(port
, hpd
);
697 spin_unlock(&port
->port_lock
);
700 spin_unlock(&port
->port_lock
);
702 if (i
== OZ_NB_PORTS
)
705 ozhcd
->conn_port
= i
;
706 hport
= &ozhcd
->ports
[i
];
707 hport
->out_ep
[0] = ep
;
708 spin_unlock_bh(&ozhcd
->hcd_lock
);
709 if (ozhcd
->flags
& OZ_HDC_F_SUSPENDED
)
710 usb_hcd_resume_root_hub(ozhcd
->hcd
);
711 usb_hcd_poll_rh_status(ozhcd
->hcd
);
717 spin_unlock_bh(&ozhcd
->hcd_lock
);
718 oz_ep_free(NULL
, ep
);
725 * This is called by the protocol handler to notify that the PD has gone away.
726 * We need to deallocate all resources and then request that the root hub is
727 * polled. We release the reference we hold on the PD.
730 void oz_hcd_pd_departed(struct oz_port
*port
)
732 struct oz_hcd
*ozhcd
;
734 struct oz_endpoint
*ep
= NULL
;
737 oz_dbg(ON
, "%s: port = 0\n", __func__
);
743 /* Check if this is the connection port - if so clear it.
745 spin_lock_bh(&ozhcd
->hcd_lock
);
746 if ((ozhcd
->conn_port
>= 0) &&
747 (port
== &ozhcd
->ports
[ozhcd
->conn_port
])) {
748 oz_dbg(ON
, "Clearing conn_port\n");
749 ozhcd
->conn_port
= -1;
751 spin_lock(&port
->port_lock
);
752 port
->flags
|= OZ_PORT_F_DYING
;
753 spin_unlock(&port
->port_lock
);
754 spin_unlock_bh(&ozhcd
->hcd_lock
);
756 oz_clean_endpoints_for_config(ozhcd
->hcd
, port
);
757 spin_lock_bh(&port
->port_lock
);
760 port
->bus_addr
= 0xff;
761 port
->config_num
= 0;
762 port
->flags
&= ~(OZ_PORT_F_PRESENT
| OZ_PORT_F_DYING
);
763 port
->flags
|= OZ_PORT_F_CHANGED
;
764 port
->status
&= ~(USB_PORT_STAT_CONNECTION
| USB_PORT_STAT_ENABLE
);
765 port
->status
|= (USB_PORT_STAT_C_CONNECTION
<< 16);
766 /* If there is an endpont 0 then clear the pointer while we hold
767 * the spinlock be we deallocate it after releasing the lock.
769 if (port
->out_ep
[0]) {
770 ep
= port
->out_ep
[0];
771 port
->out_ep
[0] = NULL
;
773 spin_unlock_bh(&port
->port_lock
);
775 oz_ep_free(port
, ep
);
776 usb_hcd_poll_rh_status(ozhcd
->hcd
);
783 void oz_hcd_pd_reset(void *hpd
, void *hport
)
785 /* Cleanup the current configuration and report reset to the core.
787 struct oz_port
*port
= (struct oz_port
*)hport
;
788 struct oz_hcd
*ozhcd
= port
->ozhcd
;
790 oz_dbg(ON
, "PD Reset\n");
791 spin_lock_bh(&port
->port_lock
);
792 port
->flags
|= OZ_PORT_F_CHANGED
;
793 port
->status
|= USB_PORT_STAT_RESET
;
794 port
->status
|= (USB_PORT_STAT_C_RESET
<< 16);
795 spin_unlock_bh(&port
->port_lock
);
796 oz_clean_endpoints_for_config(ozhcd
->hcd
, port
);
797 usb_hcd_poll_rh_status(ozhcd
->hcd
);
803 void oz_hcd_get_desc_cnf(void *hport
, u8 req_id
, int status
, const u8
*desc
,
804 int length
, int offset
, int total_size
)
806 struct oz_port
*port
= (struct oz_port
*)hport
;
810 oz_dbg(ON
, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
811 length
, offset
, total_size
);
812 urb
= oz_find_urb_by_id(port
, 0, req_id
);
817 int required_size
= urb
->transfer_buffer_length
;
818 if (required_size
> total_size
)
819 required_size
= total_size
;
820 copy_len
= required_size
-offset
;
821 if (length
<= copy_len
)
823 memcpy(urb
->transfer_buffer
+offset
, desc
, copy_len
);
825 if (offset
< required_size
) {
826 struct usb_ctrlrequest
*setup
=
827 (struct usb_ctrlrequest
*)urb
->setup_packet
;
828 unsigned wvalue
= le16_to_cpu(setup
->wValue
);
829 if (oz_enqueue_ep_urb(port
, 0, 0, urb
, req_id
))
831 else if (oz_usb_get_desc_req(port
->hpd
, req_id
,
832 setup
->bRequestType
, (u8
)(wvalue
>>8),
833 (u8
)wvalue
, setup
->wIndex
, offset
,
834 required_size
-offset
)) {
835 oz_dequeue_ep_urb(port
, 0, 0, urb
);
842 urb
->actual_length
= total_size
;
843 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
849 static void oz_display_conf_type(u8 t
)
852 case USB_REQ_GET_STATUS
:
853 oz_dbg(ON
, "USB_REQ_GET_STATUS - cnf\n");
855 case USB_REQ_CLEAR_FEATURE
:
856 oz_dbg(ON
, "USB_REQ_CLEAR_FEATURE - cnf\n");
858 case USB_REQ_SET_FEATURE
:
859 oz_dbg(ON
, "USB_REQ_SET_FEATURE - cnf\n");
861 case USB_REQ_SET_ADDRESS
:
862 oz_dbg(ON
, "USB_REQ_SET_ADDRESS - cnf\n");
864 case USB_REQ_GET_DESCRIPTOR
:
865 oz_dbg(ON
, "USB_REQ_GET_DESCRIPTOR - cnf\n");
867 case USB_REQ_SET_DESCRIPTOR
:
868 oz_dbg(ON
, "USB_REQ_SET_DESCRIPTOR - cnf\n");
870 case USB_REQ_GET_CONFIGURATION
:
871 oz_dbg(ON
, "USB_REQ_GET_CONFIGURATION - cnf\n");
873 case USB_REQ_SET_CONFIGURATION
:
874 oz_dbg(ON
, "USB_REQ_SET_CONFIGURATION - cnf\n");
876 case USB_REQ_GET_INTERFACE
:
877 oz_dbg(ON
, "USB_REQ_GET_INTERFACE - cnf\n");
879 case USB_REQ_SET_INTERFACE
:
880 oz_dbg(ON
, "USB_REQ_SET_INTERFACE - cnf\n");
882 case USB_REQ_SYNCH_FRAME
:
883 oz_dbg(ON
, "USB_REQ_SYNCH_FRAME - cnf\n");
891 static void oz_hcd_complete_set_config(struct oz_port
*port
, struct urb
*urb
,
892 u8 rcode
, u8 config_num
)
895 struct usb_hcd
*hcd
= port
->ozhcd
->hcd
;
898 port
->config_num
= config_num
;
899 oz_clean_endpoints_for_config(hcd
, port
);
900 if (oz_build_endpoints_for_config(hcd
, port
,
901 &urb
->dev
->config
[port
->config_num
-1], GFP_ATOMIC
)) {
907 oz_complete_urb(hcd
, urb
, rc
);
913 static void oz_hcd_complete_set_interface(struct oz_port
*port
, struct urb
*urb
,
914 u8 rcode
, u8 if_num
, u8 alt
)
916 struct usb_hcd
*hcd
= port
->ozhcd
->hcd
;
919 if ((rcode
== 0) && (port
->config_num
> 0)) {
920 struct usb_host_config
*config
;
921 struct usb_host_interface
*intf
;
922 oz_dbg(ON
, "Set interface %d alt %d\n", if_num
, alt
);
923 oz_clean_endpoints_for_interface(hcd
, port
, if_num
);
924 config
= &urb
->dev
->config
[port
->config_num
-1];
925 intf
= &config
->intf_cache
[if_num
]->altsetting
[alt
];
926 if (oz_build_endpoints_for_interface(hcd
, port
, intf
,
930 port
->iface
[if_num
].alt
= alt
;
934 oz_complete_urb(hcd
, urb
, rc
);
940 void oz_hcd_control_cnf(void *hport
, u8 req_id
, u8 rcode
, const u8
*data
,
943 struct oz_port
*port
= (struct oz_port
*)hport
;
945 struct usb_ctrlrequest
*setup
;
946 struct usb_hcd
*hcd
= port
->ozhcd
->hcd
;
950 oz_dbg(ON
, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode
, data_len
);
951 urb
= oz_find_urb_by_id(port
, 0, req_id
);
953 oz_dbg(ON
, "URB not found\n");
956 setup
= (struct usb_ctrlrequest
*)urb
->setup_packet
;
957 windex
= le16_to_cpu(setup
->wIndex
);
958 wvalue
= le16_to_cpu(setup
->wValue
);
959 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
960 /* Standard requests */
961 oz_display_conf_type(setup
->bRequest
);
962 switch (setup
->bRequest
) {
963 case USB_REQ_SET_CONFIGURATION
:
964 oz_hcd_complete_set_config(port
, urb
, rcode
,
967 case USB_REQ_SET_INTERFACE
:
968 oz_hcd_complete_set_interface(port
, urb
, rcode
,
969 (u8
)windex
, (u8
)wvalue
);
972 oz_complete_urb(hcd
, urb
, 0);
977 oz_dbg(ON
, "VENDOR-CLASS - cnf\n");
979 if (data_len
<= urb
->transfer_buffer_length
)
982 copy_len
= urb
->transfer_buffer_length
;
983 memcpy(urb
->transfer_buffer
, data
, copy_len
);
984 urb
->actual_length
= copy_len
;
986 oz_complete_urb(hcd
, urb
, 0);
991 * Context: softirq-serialized
993 static int oz_hcd_buffer_data(struct oz_endpoint
*ep
, const u8
*data
,
1001 space
= ep
->out_ix
-ep
->in_ix
-1;
1003 space
+= ep
->buffer_size
;
1004 if (space
< (data_len
+1)) {
1005 oz_dbg(ON
, "Buffer full\n");
1008 ep
->buffer
[ep
->in_ix
] = (u8
)data_len
;
1009 if (++ep
->in_ix
== ep
->buffer_size
)
1011 copy_len
= ep
->buffer_size
- ep
->in_ix
;
1012 if (copy_len
> data_len
)
1013 copy_len
= data_len
;
1014 memcpy(&ep
->buffer
[ep
->in_ix
], data
, copy_len
);
1016 if (copy_len
< data_len
) {
1017 memcpy(ep
->buffer
, data
+copy_len
, data_len
-copy_len
);
1018 ep
->in_ix
= data_len
-copy_len
;
1020 ep
->in_ix
+= copy_len
;
1022 if (ep
->in_ix
== ep
->buffer_size
)
1024 ep
->buffered_units
++;
1029 * Context: softirq-serialized
1031 void oz_hcd_data_ind(void *hport
, u8 endpoint
, const u8
*data
, int data_len
)
1033 struct oz_port
*port
= (struct oz_port
*)hport
;
1034 struct oz_endpoint
*ep
;
1035 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1037 spin_lock_bh(&ozhcd
->hcd_lock
);
1038 ep
= port
->in_ep
[endpoint
& USB_ENDPOINT_NUMBER_MASK
];
1041 switch (ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
) {
1042 case USB_ENDPOINT_XFER_INT
:
1043 case USB_ENDPOINT_XFER_BULK
:
1044 if (!list_empty(&ep
->urb_list
)) {
1045 struct oz_urb_link
*urbl
=
1046 list_first_entry(&ep
->urb_list
,
1047 struct oz_urb_link
, link
);
1050 list_del_init(&urbl
->link
);
1051 spin_unlock_bh(&ozhcd
->hcd_lock
);
1053 oz_free_urb_link(urbl
);
1054 if (data_len
<= urb
->transfer_buffer_length
)
1055 copy_len
= data_len
;
1057 copy_len
= urb
->transfer_buffer_length
;
1058 memcpy(urb
->transfer_buffer
, data
, copy_len
);
1059 urb
->actual_length
= copy_len
;
1060 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
1063 oz_dbg(ON
, "buffering frame as URB is not available\n");
1064 oz_hcd_buffer_data(ep
, data
, data_len
);
1067 case USB_ENDPOINT_XFER_ISOC
:
1068 oz_hcd_buffer_data(ep
, data
, data_len
);
1072 spin_unlock_bh(&ozhcd
->hcd_lock
);
1078 static inline int oz_usb_get_frame_number(void)
1080 return atomic_inc_return(&g_usb_frame_number
);
1086 int oz_hcd_heartbeat(void *hport
)
1089 struct oz_port
*port
= (struct oz_port
*)hport
;
1090 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1091 struct oz_urb_link
*urbl
;
1092 struct list_head xfr_list
;
1093 struct list_head
*e
;
1094 struct list_head
*n
;
1096 struct oz_endpoint
*ep
;
1097 struct timespec ts
, delta
;
1099 getrawmonotonic(&ts
);
1100 INIT_LIST_HEAD(&xfr_list
);
1101 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1103 spin_lock_bh(&ozhcd
->hcd_lock
);
1104 list_for_each(e
, &port
->isoc_out_ep
) {
1105 ep
= ep_from_link(e
);
1108 delta
= timespec_sub(ts
, ep
->timestamp
);
1109 ep
->credit
+= div_u64(timespec_to_ns(&delta
), NSEC_PER_MSEC
);
1110 if (ep
->credit
> ep
->credit_ceiling
)
1111 ep
->credit
= ep
->credit_ceiling
;
1113 while (ep
->credit
&& !list_empty(&ep
->urb_list
)) {
1114 urbl
= list_first_entry(&ep
->urb_list
,
1115 struct oz_urb_link
, link
);
1117 if ((ep
->credit
+ 1) < urb
->number_of_packets
)
1119 ep
->credit
-= urb
->number_of_packets
;
1122 list_move_tail(&urbl
->link
, &xfr_list
);
1125 spin_unlock_bh(&ozhcd
->hcd_lock
);
1126 /* Send to PD and complete URBs.
1128 list_for_each_safe(e
, n
, &xfr_list
) {
1129 urbl
= container_of(e
, struct oz_urb_link
, link
);
1132 urb
->error_count
= 0;
1133 urb
->start_frame
= oz_usb_get_frame_number();
1134 oz_usb_send_isoc(port
->hpd
, urbl
->ep_num
, urb
);
1135 oz_free_urb_link(urbl
);
1136 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
1138 /* Check the IN isoc endpoints to see if any URBs can be completed.
1140 spin_lock_bh(&ozhcd
->hcd_lock
);
1141 list_for_each(e
, &port
->isoc_in_ep
) {
1142 struct oz_endpoint
*ep
= ep_from_link(e
);
1143 if (ep
->flags
& OZ_F_EP_BUFFERING
) {
1144 if (ep
->buffered_units
>= OZ_IN_BUFFERING_UNITS
) {
1145 ep
->flags
&= ~OZ_F_EP_BUFFERING
;
1148 ep
->start_frame
= 0;
1152 delta
= timespec_sub(ts
, ep
->timestamp
);
1153 ep
->credit
+= div_u64(timespec_to_ns(&delta
), NSEC_PER_MSEC
);
1155 while (!list_empty(&ep
->urb_list
)) {
1156 struct oz_urb_link
*urbl
=
1157 list_first_entry(&ep
->urb_list
,
1158 struct oz_urb_link
, link
);
1159 struct urb
*urb
= urbl
->urb
;
1163 if (ep
->credit
< urb
->number_of_packets
)
1165 if (ep
->buffered_units
< urb
->number_of_packets
)
1167 urb
->actual_length
= 0;
1168 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1169 len
= ep
->buffer
[ep
->out_ix
];
1170 if (++ep
->out_ix
== ep
->buffer_size
)
1172 copy_len
= ep
->buffer_size
- ep
->out_ix
;
1175 memcpy(urb
->transfer_buffer
,
1176 &ep
->buffer
[ep
->out_ix
], copy_len
);
1177 if (copy_len
< len
) {
1178 memcpy(urb
->transfer_buffer
+copy_len
,
1179 ep
->buffer
, len
-copy_len
);
1180 ep
->out_ix
= len
-copy_len
;
1182 ep
->out_ix
+= copy_len
;
1183 if (ep
->out_ix
== ep
->buffer_size
)
1185 urb
->iso_frame_desc
[i
].offset
=
1187 urb
->actual_length
+= len
;
1188 urb
->iso_frame_desc
[i
].actual_length
= len
;
1189 urb
->iso_frame_desc
[i
].status
= 0;
1191 ep
->buffered_units
-= urb
->number_of_packets
;
1192 urb
->error_count
= 0;
1193 urb
->start_frame
= ep
->start_frame
;
1194 ep
->start_frame
+= urb
->number_of_packets
;
1195 list_move_tail(&urbl
->link
, &xfr_list
);
1196 ep
->credit
-= urb
->number_of_packets
;
1199 if (!list_empty(&port
->isoc_out_ep
) || !list_empty(&port
->isoc_in_ep
))
1201 spin_unlock_bh(&ozhcd
->hcd_lock
);
1202 /* Complete the filled URBs.
1204 list_for_each_safe(e
, n
, &xfr_list
) {
1205 urbl
= container_of(e
, struct oz_urb_link
, link
);
1208 oz_free_urb_link(urbl
);
1209 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
1211 /* Check if there are any ep0 requests that have timed out.
1212 * If so resent to PD.
1214 ep
= port
->out_ep
[0];
1216 struct list_head
*e
;
1217 struct list_head
*n
;
1218 spin_lock_bh(&ozhcd
->hcd_lock
);
1219 list_for_each_safe(e
, n
, &ep
->urb_list
) {
1220 urbl
= container_of(e
, struct oz_urb_link
, link
);
1221 if (urbl
->submit_counter
> EP0_TIMEOUT_COUNTER
) {
1222 oz_dbg(ON
, "Request 0x%p timeout\n", urbl
->urb
);
1223 list_move_tail(e
, &xfr_list
);
1224 urbl
->submit_counter
= 0;
1226 urbl
->submit_counter
++;
1229 if (!list_empty(&ep
->urb_list
))
1231 spin_unlock_bh(&ozhcd
->hcd_lock
);
1233 while (e
!= &xfr_list
) {
1234 urbl
= container_of(e
, struct oz_urb_link
, link
);
1236 oz_dbg(ON
, "Resending request to PD\n");
1237 oz_process_ep0_urb(ozhcd
, urbl
->urb
, GFP_ATOMIC
);
1238 oz_free_urb_link(urbl
);
1247 static int oz_build_endpoints_for_interface(struct usb_hcd
*hcd
,
1248 struct oz_port
*port
,
1249 struct usb_host_interface
*intf
, gfp_t mem_flags
)
1251 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1253 int if_ix
= intf
->desc
.bInterfaceNumber
;
1254 int request_heartbeat
= 0;
1256 oz_dbg(ON
, "interface[%d] = %p\n", if_ix
, intf
);
1257 if (if_ix
>= port
->num_iface
|| port
->iface
== NULL
)
1259 for (i
= 0; i
< intf
->desc
.bNumEndpoints
; i
++) {
1260 struct usb_host_endpoint
*hep
= &intf
->endpoint
[i
];
1261 u8 ep_addr
= hep
->desc
.bEndpointAddress
;
1262 u8 ep_num
= ep_addr
& USB_ENDPOINT_NUMBER_MASK
;
1263 struct oz_endpoint
*ep
;
1264 int buffer_size
= 0;
1266 oz_dbg(ON
, "%d bEndpointAddress = %x\n", i
, ep_addr
);
1267 if (ep_addr
& USB_ENDPOINT_DIR_MASK
) {
1268 switch (hep
->desc
.bmAttributes
&
1269 USB_ENDPOINT_XFERTYPE_MASK
) {
1270 case USB_ENDPOINT_XFER_ISOC
:
1271 buffer_size
= OZ_EP_BUFFER_SIZE_ISOC
;
1273 case USB_ENDPOINT_XFER_INT
:
1274 buffer_size
= OZ_EP_BUFFER_SIZE_INT
;
1279 ep
= oz_ep_alloc(buffer_size
, mem_flags
);
1281 oz_clean_endpoints_for_interface(hcd
, port
, if_ix
);
1284 ep
->attrib
= hep
->desc
.bmAttributes
;
1285 ep
->ep_num
= ep_num
;
1286 if ((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
)
1287 == USB_ENDPOINT_XFER_ISOC
) {
1288 oz_dbg(ON
, "wMaxPacketSize = %d\n",
1289 usb_endpoint_maxp(&hep
->desc
));
1290 ep
->credit_ceiling
= 200;
1291 if (ep_addr
& USB_ENDPOINT_DIR_MASK
) {
1292 ep
->flags
|= OZ_F_EP_BUFFERING
;
1294 ep
->flags
|= OZ_F_EP_HAVE_STREAM
;
1295 if (oz_usb_stream_create(port
->hpd
, ep_num
))
1296 ep
->flags
&= ~OZ_F_EP_HAVE_STREAM
;
1299 spin_lock_bh(&ozhcd
->hcd_lock
);
1300 if (ep_addr
& USB_ENDPOINT_DIR_MASK
) {
1301 port
->in_ep
[ep_num
] = ep
;
1302 port
->iface
[if_ix
].ep_mask
|=
1303 (1<<(ep_num
+OZ_NB_ENDPOINTS
));
1304 if ((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
)
1305 == USB_ENDPOINT_XFER_ISOC
) {
1306 list_add_tail(&ep
->link
, &port
->isoc_in_ep
);
1307 request_heartbeat
= 1;
1310 port
->out_ep
[ep_num
] = ep
;
1311 port
->iface
[if_ix
].ep_mask
|= (1<<ep_num
);
1312 if ((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
)
1313 == USB_ENDPOINT_XFER_ISOC
) {
1314 list_add_tail(&ep
->link
, &port
->isoc_out_ep
);
1315 request_heartbeat
= 1;
1318 spin_unlock_bh(&ozhcd
->hcd_lock
);
1319 if (request_heartbeat
&& port
->hpd
)
1320 oz_usb_request_heartbeat(port
->hpd
);
1328 static void oz_clean_endpoints_for_interface(struct usb_hcd
*hcd
,
1329 struct oz_port
*port
, int if_ix
)
1331 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1334 struct list_head ep_list
;
1336 oz_dbg(ON
, "Deleting endpoints for interface %d\n", if_ix
);
1337 if (if_ix
>= port
->num_iface
)
1339 INIT_LIST_HEAD(&ep_list
);
1340 spin_lock_bh(&ozhcd
->hcd_lock
);
1341 mask
= port
->iface
[if_ix
].ep_mask
;
1342 port
->iface
[if_ix
].ep_mask
= 0;
1343 for (i
= 0; i
< OZ_NB_ENDPOINTS
; i
++) {
1344 struct list_head
*e
;
1345 /* Gather OUT endpoints.
1347 if ((mask
& (1<<i
)) && port
->out_ep
[i
]) {
1348 e
= &port
->out_ep
[i
]->link
;
1349 port
->out_ep
[i
] = NULL
;
1350 /* Remove from isoc list if present.
1352 list_move_tail(e
, &ep_list
);
1354 /* Gather IN endpoints.
1356 if ((mask
& (1<<(i
+OZ_NB_ENDPOINTS
))) && port
->in_ep
[i
]) {
1357 e
= &port
->in_ep
[i
]->link
;
1358 port
->in_ep
[i
] = NULL
;
1359 list_move_tail(e
, &ep_list
);
1362 spin_unlock_bh(&ozhcd
->hcd_lock
);
1363 while (!list_empty(&ep_list
)) {
1364 struct oz_endpoint
*ep
=
1365 list_first_entry(&ep_list
, struct oz_endpoint
, link
);
1366 list_del_init(&ep
->link
);
1367 oz_ep_free(port
, ep
);
1374 static int oz_build_endpoints_for_config(struct usb_hcd
*hcd
,
1375 struct oz_port
*port
, struct usb_host_config
*config
,
1378 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1380 int num_iface
= config
->desc
.bNumInterfaces
;
1383 struct oz_interface
*iface
;
1385 iface
= kmalloc(num_iface
*sizeof(struct oz_interface
),
1386 mem_flags
| __GFP_ZERO
);
1389 spin_lock_bh(&ozhcd
->hcd_lock
);
1390 port
->iface
= iface
;
1391 port
->num_iface
= num_iface
;
1392 spin_unlock_bh(&ozhcd
->hcd_lock
);
1394 for (i
= 0; i
< num_iface
; i
++) {
1395 struct usb_host_interface
*intf
=
1396 &config
->intf_cache
[i
]->altsetting
[0];
1397 if (oz_build_endpoints_for_interface(hcd
, port
, intf
,
1403 oz_clean_endpoints_for_config(hcd
, port
);
1410 static void oz_clean_endpoints_for_config(struct usb_hcd
*hcd
,
1411 struct oz_port
*port
)
1413 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1416 oz_dbg(ON
, "Deleting endpoints for configuration\n");
1417 for (i
= 0; i
< port
->num_iface
; i
++)
1418 oz_clean_endpoints_for_interface(hcd
, port
, i
);
1419 spin_lock_bh(&ozhcd
->hcd_lock
);
1421 oz_dbg(ON
, "Freeing interfaces object\n");
1425 port
->num_iface
= 0;
1426 spin_unlock_bh(&ozhcd
->hcd_lock
);
1432 static void *oz_claim_hpd(struct oz_port
*port
)
1435 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1437 spin_lock_bh(&ozhcd
->hcd_lock
);
1441 spin_unlock_bh(&ozhcd
->hcd_lock
);
1448 static void oz_process_ep0_urb(struct oz_hcd
*ozhcd
, struct urb
*urb
,
1451 struct usb_ctrlrequest
*setup
;
1458 unsigned complete
= 0;
1461 struct oz_port
*port
= NULL
;
1463 oz_dbg(URB
, "[%s]:(%p)\n", __func__
, urb
);
1464 port_ix
= oz_get_port_from_addr(ozhcd
, urb
->dev
->devnum
);
1469 port
= &ozhcd
->ports
[port_ix
];
1470 if (((port
->flags
& OZ_PORT_F_PRESENT
) == 0)
1471 || (port
->flags
& OZ_PORT_F_DYING
)) {
1472 oz_dbg(ON
, "Refusing URB port_ix = %d devnum = %d\n",
1473 port_ix
, urb
->dev
->devnum
);
1477 /* Store port in private context data.
1480 setup
= (struct usb_ctrlrequest
*)urb
->setup_packet
;
1481 windex
= le16_to_cpu(setup
->wIndex
);
1482 wvalue
= le16_to_cpu(setup
->wValue
);
1483 wlength
= le16_to_cpu(setup
->wLength
);
1484 oz_dbg(CTRL_DETAIL
, "bRequestType = %x\n", setup
->bRequestType
);
1485 oz_dbg(CTRL_DETAIL
, "bRequest = %x\n", setup
->bRequest
);
1486 oz_dbg(CTRL_DETAIL
, "wValue = %x\n", wvalue
);
1487 oz_dbg(CTRL_DETAIL
, "wIndex = %x\n", windex
);
1488 oz_dbg(CTRL_DETAIL
, "wLength = %x\n", wlength
);
1490 req_id
= port
->next_req_id
++;
1491 hpd
= oz_claim_hpd(port
);
1493 oz_dbg(ON
, "Cannot claim port\n");
1498 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1499 /* Standard requests
1501 switch (setup
->bRequest
) {
1502 case USB_REQ_GET_DESCRIPTOR
:
1503 oz_dbg(ON
, "USB_REQ_GET_DESCRIPTOR - req\n");
1505 case USB_REQ_SET_ADDRESS
:
1506 oz_dbg(ON
, "USB_REQ_SET_ADDRESS - req\n");
1507 oz_dbg(ON
, "Port %d address is 0x%x\n",
1509 (u8
)le16_to_cpu(setup
->wValue
));
1510 spin_lock_bh(&ozhcd
->hcd_lock
);
1511 if (ozhcd
->conn_port
>= 0) {
1512 ozhcd
->ports
[ozhcd
->conn_port
].bus_addr
=
1513 (u8
)le16_to_cpu(setup
->wValue
);
1514 oz_dbg(ON
, "Clearing conn_port\n");
1515 ozhcd
->conn_port
= -1;
1517 spin_unlock_bh(&ozhcd
->hcd_lock
);
1520 case USB_REQ_SET_CONFIGURATION
:
1521 oz_dbg(ON
, "USB_REQ_SET_CONFIGURATION - req\n");
1523 case USB_REQ_GET_CONFIGURATION
:
1524 /* We short circuit this case and reply directly since
1525 * we have the selected configuration number cached.
1527 oz_dbg(ON
, "USB_REQ_GET_CONFIGURATION - reply now\n");
1528 if (urb
->transfer_buffer_length
>= 1) {
1529 urb
->actual_length
= 1;
1530 *((u8
*)urb
->transfer_buffer
) =
1537 case USB_REQ_GET_INTERFACE
:
1538 /* We short circuit this case and reply directly since
1539 * we have the selected interface alternative cached.
1541 oz_dbg(ON
, "USB_REQ_GET_INTERFACE - reply now\n");
1542 if (urb
->transfer_buffer_length
>= 1) {
1543 urb
->actual_length
= 1;
1544 *((u8
*)urb
->transfer_buffer
) =
1545 port
->iface
[(u8
)windex
].alt
;
1546 oz_dbg(ON
, "interface = %d alt = %d\n",
1547 windex
, port
->iface
[(u8
)windex
].alt
);
1553 case USB_REQ_SET_INTERFACE
:
1554 oz_dbg(ON
, "USB_REQ_SET_INTERFACE - req\n");
1558 if (!rc
&& !complete
) {
1560 if ((setup
->bRequestType
& USB_DIR_IN
) == 0)
1562 urb
->actual_length
= data_len
;
1563 if (oz_usb_control_req(port
->hpd
, req_id
, setup
,
1564 urb
->transfer_buffer
, data_len
)) {
1567 /* Note: we are queuing the request after we have
1568 * submitted it to be transmitted. If the request were
1569 * to complete before we queued it then it would not
1570 * be found in the queue. It seems impossible for
1571 * this to happen but if it did the request would
1572 * be resubmitted so the problem would hopefully
1573 * resolve itself. Putting the request into the
1574 * queue before it has been sent is worse since the
1575 * urb could be cancelled while we are using it
1576 * to build the request.
1578 if (oz_enqueue_ep_urb(port
, 0, 0, urb
, req_id
))
1584 if (rc
|| complete
) {
1585 oz_dbg(ON
, "Completing request locally\n");
1586 oz_complete_urb(ozhcd
->hcd
, urb
, rc
);
1588 oz_usb_request_heartbeat(port
->hpd
);
1595 static int oz_urb_process(struct oz_hcd
*ozhcd
, struct urb
*urb
)
1598 struct oz_port
*port
= urb
->hcpriv
;
1601 /* When we are paranoid we keep a list of urbs which we check against
1602 * before handing one back. This is just for debugging during
1603 * development and should be turned off in the released driver.
1605 oz_remember_urb(urb
);
1606 /* Check buffer is valid.
1608 if (!urb
->transfer_buffer
&& urb
->transfer_buffer_length
)
1610 /* Check if there is a device at the port - refuse if not.
1612 if ((port
->flags
& OZ_PORT_F_PRESENT
) == 0)
1614 ep_addr
= usb_pipeendpoint(urb
->pipe
);
1616 /* If the request is not for EP0 then queue it.
1618 if (oz_enqueue_ep_urb(port
, ep_addr
, usb_pipein(urb
->pipe
),
1622 oz_process_ep0_urb(ozhcd
, urb
, GFP_ATOMIC
);
1630 static void oz_urb_process_tasklet(unsigned long unused
)
1632 unsigned long irq_state
;
1634 struct oz_hcd
*ozhcd
= oz_hcd_claim();
1639 /* This is called from a tasklet so is in softirq context but the urb
1640 * list is filled from any context so we need to lock
1641 * appropriately while removing urbs.
1643 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1644 while (!list_empty(&ozhcd
->urb_pending_list
)) {
1645 struct oz_urb_link
*urbl
=
1646 list_first_entry(&ozhcd
->urb_pending_list
,
1647 struct oz_urb_link
, link
);
1648 list_del_init(&urbl
->link
);
1649 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1651 oz_free_urb_link(urbl
);
1652 rc
= oz_urb_process(ozhcd
, urb
);
1654 oz_complete_urb(ozhcd
->hcd
, urb
, rc
);
1655 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1657 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1662 * This function searches for the urb in any of the lists it could be in.
1663 * If it is found it is removed from the list and completed. If the urb is
1664 * being processed then it won't be in a list so won't be found. However, the
1665 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1666 * to a non-zero value. When an attempt is made to put the urb back in a list
1667 * the unlinked field will be checked and the urb will then be completed.
1670 static void oz_urb_cancel(struct oz_port
*port
, u8 ep_num
, struct urb
*urb
)
1672 struct oz_urb_link
*urbl
= NULL
;
1673 struct list_head
*e
;
1674 struct oz_hcd
*ozhcd
;
1675 unsigned long irq_state
;
1679 oz_dbg(ON
, "%s: ERROR: (%p) port is null\n", __func__
, urb
);
1682 ozhcd
= port
->ozhcd
;
1683 if (ozhcd
== NULL
) {
1684 oz_dbg(ON
, "%s; ERROR: (%p) ozhcd is null\n", __func__
, urb
);
1688 /* Look in the tasklet queue.
1690 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1691 list_for_each(e
, &ozhcd
->urb_cancel_list
) {
1692 urbl
= container_of(e
, struct oz_urb_link
, link
);
1693 if (urb
== urbl
->urb
) {
1695 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1699 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1702 /* Look in the orphanage.
1704 spin_lock_irqsave(&ozhcd
->hcd_lock
, irq_state
);
1705 list_for_each(e
, &ozhcd
->orphanage
) {
1706 urbl
= container_of(e
, struct oz_urb_link
, link
);
1707 if (urbl
->urb
== urb
) {
1709 oz_dbg(ON
, "Found urb in orphanage\n");
1713 ix
= (ep_num
& 0xf);
1715 if ((ep_num
& USB_DIR_IN
) && ix
)
1716 urbl
= oz_remove_urb(port
->in_ep
[ix
], urb
);
1718 urbl
= oz_remove_urb(port
->out_ep
[ix
], urb
);
1720 spin_unlock_irqrestore(&ozhcd
->hcd_lock
, irq_state
);
1723 urb
->actual_length
= 0;
1724 oz_free_urb_link(urbl
);
1725 oz_complete_urb(ozhcd
->hcd
, urb
, -EPIPE
);
1732 static void oz_urb_cancel_tasklet(unsigned long unused
)
1734 unsigned long irq_state
;
1736 struct oz_hcd
*ozhcd
= oz_hcd_claim();
1740 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1741 while (!list_empty(&ozhcd
->urb_cancel_list
)) {
1742 struct oz_urb_link
*urbl
=
1743 list_first_entry(&ozhcd
->urb_cancel_list
,
1744 struct oz_urb_link
, link
);
1745 list_del_init(&urbl
->link
);
1746 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1749 oz_urb_cancel(urbl
->port
, urbl
->ep_num
, urb
);
1750 oz_free_urb_link(urbl
);
1751 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1753 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1760 static void oz_hcd_clear_orphanage(struct oz_hcd
*ozhcd
, int status
)
1763 struct oz_urb_link
*urbl
;
1764 while (!list_empty(&ozhcd
->orphanage
)) {
1765 urbl
= list_first_entry(&ozhcd
->orphanage
,
1766 struct oz_urb_link
, link
);
1767 list_del(&urbl
->link
);
1768 oz_complete_urb(ozhcd
->hcd
, urbl
->urb
, status
);
1769 oz_free_urb_link(urbl
);
1777 static int oz_hcd_start(struct usb_hcd
*hcd
)
1779 hcd
->power_budget
= 200;
1780 hcd
->state
= HC_STATE_RUNNING
;
1781 hcd
->uses_new_polling
= 1;
1788 static void oz_hcd_stop(struct usb_hcd
*hcd
)
1795 static void oz_hcd_shutdown(struct usb_hcd
*hcd
)
1800 * Called to queue an urb for the device.
1801 * This function should return a non-zero error code if it fails the urb but
1802 * should not call usb_hcd_giveback_urb().
1805 static int oz_hcd_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
1808 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
1811 struct oz_port
*port
;
1812 unsigned long irq_state
;
1813 struct oz_urb_link
*urbl
;
1815 oz_dbg(URB
, "%s: (%p)\n", __func__
, urb
);
1816 if (unlikely(ozhcd
== NULL
)) {
1817 oz_dbg(URB
, "Refused urb(%p) not ozhcd\n", urb
);
1820 if (unlikely(hcd
->state
!= HC_STATE_RUNNING
)) {
1821 oz_dbg(URB
, "Refused urb(%p) not running\n", urb
);
1824 port_ix
= oz_get_port_from_addr(ozhcd
, urb
->dev
->devnum
);
1827 port
= &ozhcd
->ports
[port_ix
];
1830 if (!(port
->flags
& OZ_PORT_F_PRESENT
) ||
1831 (port
->flags
& OZ_PORT_F_CHANGED
)) {
1832 oz_dbg(ON
, "Refusing URB port_ix = %d devnum = %d\n",
1833 port_ix
, urb
->dev
->devnum
);
1837 /* Put request in queue for processing by tasklet.
1839 urbl
= oz_alloc_urb_link();
1840 if (unlikely(urbl
== NULL
))
1843 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1844 rc
= usb_hcd_link_urb_to_ep(hcd
, urb
);
1846 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1847 oz_free_urb_link(urbl
);
1850 list_add_tail(&urbl
->link
, &ozhcd
->urb_pending_list
);
1851 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1852 tasklet_schedule(&g_urb_process_tasklet
);
1853 atomic_inc(&g_pending_urbs
);
1860 static struct oz_urb_link
*oz_remove_urb(struct oz_endpoint
*ep
,
1863 struct oz_urb_link
*urbl
;
1864 struct list_head
*e
;
1866 if (unlikely(ep
== NULL
))
1868 list_for_each(e
, &ep
->urb_list
) {
1869 urbl
= container_of(e
, struct oz_urb_link
, link
);
1870 if (urbl
->urb
== urb
) {
1872 if (usb_pipeisoc(urb
->pipe
)) {
1873 ep
->credit
-= urb
->number_of_packets
;
1884 * Called to dequeue a previously submitted urb for the device.
1887 static int oz_hcd_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1889 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
1890 struct oz_urb_link
*urbl
;
1892 unsigned long irq_state
;
1894 oz_dbg(URB
, "%s: (%p)\n", __func__
, urb
);
1895 urbl
= oz_alloc_urb_link();
1896 if (unlikely(urbl
== NULL
))
1898 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1899 /* The following function checks the urb is still in the queue
1900 * maintained by the core and that the unlinked field is zero.
1901 * If both are true the function sets the unlinked field and returns
1902 * zero. Otherwise it returns an error.
1904 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1905 /* We have to check we haven't completed the urb or are about
1906 * to complete it. When we do we set hcpriv to 0 so if this has
1907 * already happened we don't put the urb in the cancel queue.
1909 if ((rc
== 0) && urb
->hcpriv
) {
1911 urbl
->port
= (struct oz_port
*)urb
->hcpriv
;
1912 urbl
->ep_num
= usb_pipeendpoint(urb
->pipe
);
1913 if (usb_pipein(urb
->pipe
))
1914 urbl
->ep_num
|= USB_DIR_IN
;
1915 list_add_tail(&urbl
->link
, &ozhcd
->urb_cancel_list
);
1916 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1917 tasklet_schedule(&g_urb_cancel_tasklet
);
1919 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1920 oz_free_urb_link(urbl
);
1928 static void oz_hcd_endpoint_disable(struct usb_hcd
*hcd
,
1929 struct usb_host_endpoint
*ep
)
1936 static void oz_hcd_endpoint_reset(struct usb_hcd
*hcd
,
1937 struct usb_host_endpoint
*ep
)
1944 static int oz_hcd_get_frame_number(struct usb_hcd
*hcd
)
1946 oz_dbg(ON
, "oz_hcd_get_frame_number\n");
1947 return oz_usb_get_frame_number();
1952 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1953 * always do that in softirq context.
1955 static int oz_hcd_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
1957 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
1963 spin_lock_bh(&ozhcd
->hcd_lock
);
1964 for (i
= 0; i
< OZ_NB_PORTS
; i
++) {
1965 if (ozhcd
->ports
[i
].flags
& OZ_PORT_F_CHANGED
) {
1966 oz_dbg(HUB
, "Port %d changed\n", i
);
1967 ozhcd
->ports
[i
].flags
&= ~OZ_PORT_F_CHANGED
;
1969 buf
[0] |= 1 << (i
+ 1);
1971 buf
[1] |= 1 << (i
- 7);
1974 spin_unlock_bh(&ozhcd
->hcd_lock
);
1975 if (buf
[0] != 0 || buf
[1] != 0)
1984 static void oz_get_hub_descriptor(struct usb_hcd
*hcd
,
1985 struct usb_hub_descriptor
*desc
)
1987 memset(desc
, 0, sizeof(*desc
));
1988 desc
->bDescriptorType
= 0x29;
1989 desc
->bDescLength
= 9;
1990 desc
->wHubCharacteristics
= (__force __u16
)cpu_to_le16(0x0001);
1991 desc
->bNbrPorts
= OZ_NB_PORTS
;
1997 static int oz_set_port_feature(struct usb_hcd
*hcd
, u16 wvalue
, u16 windex
)
1999 struct oz_port
*port
;
2001 u8 port_id
= (u8
)windex
;
2002 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
2003 unsigned set_bits
= 0;
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");
2016 case USB_PORT_FEAT_SUSPEND
:
2017 oz_dbg(HUB
, "USB_PORT_FEAT_SUSPEND\n");
2019 case USB_PORT_FEAT_OVER_CURRENT
:
2020 oz_dbg(HUB
, "USB_PORT_FEAT_OVER_CURRENT\n");
2022 case USB_PORT_FEAT_RESET
:
2023 oz_dbg(HUB
, "USB_PORT_FEAT_RESET\n");
2024 set_bits
= USB_PORT_STAT_ENABLE
| (USB_PORT_STAT_C_RESET
<<16);
2025 clear_bits
= USB_PORT_STAT_RESET
;
2026 ozhcd
->ports
[port_id
-1].bus_addr
= 0;
2028 case USB_PORT_FEAT_POWER
:
2029 oz_dbg(HUB
, "USB_PORT_FEAT_POWER\n");
2030 set_bits
|= USB_PORT_STAT_POWER
;
2032 case USB_PORT_FEAT_LOWSPEED
:
2033 oz_dbg(HUB
, "USB_PORT_FEAT_LOWSPEED\n");
2035 case USB_PORT_FEAT_C_CONNECTION
:
2036 oz_dbg(HUB
, "USB_PORT_FEAT_C_CONNECTION\n");
2038 case USB_PORT_FEAT_C_ENABLE
:
2039 oz_dbg(HUB
, "USB_PORT_FEAT_C_ENABLE\n");
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");
2050 case USB_PORT_FEAT_TEST
:
2051 oz_dbg(HUB
, "USB_PORT_FEAT_TEST\n");
2053 case USB_PORT_FEAT_INDICATOR
:
2054 oz_dbg(HUB
, "USB_PORT_FEAT_INDICATOR\n");
2057 oz_dbg(HUB
, "Other %d\n", wvalue
);
2060 if (set_bits
|| clear_bits
) {
2061 spin_lock_bh(&port
->port_lock
);
2062 port
->status
&= ~clear_bits
;
2063 port
->status
|= set_bits
;
2064 spin_unlock_bh(&port
->port_lock
);
2066 oz_dbg(HUB
, "Port[%d] status = 0x%x\n", port_id
, port
->status
);
2073 static int oz_clear_port_feature(struct usb_hcd
*hcd
, u16 wvalue
, u16 windex
)
2075 struct oz_port
*port
;
2077 u8 port_id
= (u8
)windex
;
2078 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
2079 unsigned clear_bits
= 0;
2081 if ((port_id
< 1) || (port_id
> OZ_NB_PORTS
))
2083 port
= &ozhcd
->ports
[port_id
-1];
2085 case USB_PORT_FEAT_CONNECTION
:
2086 oz_dbg(HUB
, "USB_PORT_FEAT_CONNECTION\n");
2088 case USB_PORT_FEAT_ENABLE
:
2089 oz_dbg(HUB
, "USB_PORT_FEAT_ENABLE\n");
2090 clear_bits
= USB_PORT_STAT_ENABLE
;
2092 case USB_PORT_FEAT_SUSPEND
:
2093 oz_dbg(HUB
, "USB_PORT_FEAT_SUSPEND\n");
2095 case USB_PORT_FEAT_OVER_CURRENT
:
2096 oz_dbg(HUB
, "USB_PORT_FEAT_OVER_CURRENT\n");
2098 case USB_PORT_FEAT_RESET
:
2099 oz_dbg(HUB
, "USB_PORT_FEAT_RESET\n");
2101 case USB_PORT_FEAT_POWER
:
2102 oz_dbg(HUB
, "USB_PORT_FEAT_POWER\n");
2103 clear_bits
|= USB_PORT_STAT_POWER
;
2105 case USB_PORT_FEAT_LOWSPEED
:
2106 oz_dbg(HUB
, "USB_PORT_FEAT_LOWSPEED\n");
2108 case USB_PORT_FEAT_C_CONNECTION
:
2109 oz_dbg(HUB
, "USB_PORT_FEAT_C_CONNECTION\n");
2110 clear_bits
= (USB_PORT_STAT_C_CONNECTION
<< 16);
2112 case USB_PORT_FEAT_C_ENABLE
:
2113 oz_dbg(HUB
, "USB_PORT_FEAT_C_ENABLE\n");
2114 clear_bits
= (USB_PORT_STAT_C_ENABLE
<< 16);
2116 case USB_PORT_FEAT_C_SUSPEND
:
2117 oz_dbg(HUB
, "USB_PORT_FEAT_C_SUSPEND\n");
2119 case USB_PORT_FEAT_C_OVER_CURRENT
:
2120 oz_dbg(HUB
, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2122 case USB_PORT_FEAT_C_RESET
:
2123 oz_dbg(HUB
, "USB_PORT_FEAT_C_RESET\n");
2124 clear_bits
= (USB_PORT_FEAT_C_RESET
<< 16);
2126 case USB_PORT_FEAT_TEST
:
2127 oz_dbg(HUB
, "USB_PORT_FEAT_TEST\n");
2129 case USB_PORT_FEAT_INDICATOR
:
2130 oz_dbg(HUB
, "USB_PORT_FEAT_INDICATOR\n");
2133 oz_dbg(HUB
, "Other %d\n", wvalue
);
2137 spin_lock_bh(&port
->port_lock
);
2138 port
->status
&= ~clear_bits
;
2139 spin_unlock_bh(&port
->port_lock
);
2141 oz_dbg(HUB
, "Port[%d] status = 0x%x\n",
2142 port_id
, ozhcd
->ports
[port_id
-1].status
);
2149 static int oz_get_port_status(struct usb_hcd
*hcd
, u16 windex
, char *buf
)
2151 struct oz_hcd
*ozhcd
;
2154 if ((windex
< 1) || (windex
> OZ_NB_PORTS
))
2156 ozhcd
= oz_hcd_private(hcd
);
2157 oz_dbg(HUB
, "GetPortStatus windex = %d\n", windex
);
2158 status
= ozhcd
->ports
[windex
-1].status
;
2159 put_unaligned(cpu_to_le32(status
), (__le32
*)buf
);
2160 oz_dbg(HUB
, "Port[%d] status = %x\n", windex
, status
);
2167 static int oz_hcd_hub_control(struct usb_hcd
*hcd
, u16 req_type
, u16 wvalue
,
2168 u16 windex
, char *buf
, u16 wlength
)
2173 case ClearHubFeature
:
2174 oz_dbg(HUB
, "ClearHubFeature: %d\n", req_type
);
2176 case ClearPortFeature
:
2177 err
= oz_clear_port_feature(hcd
, wvalue
, windex
);
2179 case GetHubDescriptor
:
2180 oz_get_hub_descriptor(hcd
, (struct usb_hub_descriptor
*)buf
);
2183 oz_dbg(HUB
, "GetHubStatus: req_type = 0x%x\n", req_type
);
2184 put_unaligned(cpu_to_le32(0), (__le32
*)buf
);
2187 err
= oz_get_port_status(hcd
, windex
, buf
);
2190 oz_dbg(HUB
, "SetHubFeature: %d\n", req_type
);
2192 case SetPortFeature
:
2193 err
= oz_set_port_feature(hcd
, wvalue
, windex
);
2196 oz_dbg(HUB
, "Other: %d\n", req_type
);
2205 static int oz_hcd_bus_suspend(struct usb_hcd
*hcd
)
2207 struct oz_hcd
*ozhcd
;
2209 ozhcd
= oz_hcd_private(hcd
);
2210 spin_lock_bh(&ozhcd
->hcd_lock
);
2211 hcd
->state
= HC_STATE_SUSPENDED
;
2212 ozhcd
->flags
|= OZ_HDC_F_SUSPENDED
;
2213 spin_unlock_bh(&ozhcd
->hcd_lock
);
2220 static int oz_hcd_bus_resume(struct usb_hcd
*hcd
)
2222 struct oz_hcd
*ozhcd
;
2224 ozhcd
= oz_hcd_private(hcd
);
2225 spin_lock_bh(&ozhcd
->hcd_lock
);
2226 ozhcd
->flags
&= ~OZ_HDC_F_SUSPENDED
;
2227 hcd
->state
= HC_STATE_RUNNING
;
2228 spin_unlock_bh(&ozhcd
->hcd_lock
);
2232 static void oz_plat_shutdown(struct platform_device
*dev
)
2239 static int oz_plat_probe(struct platform_device
*dev
)
2243 struct usb_hcd
*hcd
;
2244 struct oz_hcd
*ozhcd
;
2246 hcd
= usb_create_hcd(&g_oz_hc_drv
, &dev
->dev
, dev_name(&dev
->dev
));
2248 oz_dbg(ON
, "Failed to created hcd object OK\n");
2251 ozhcd
= oz_hcd_private(hcd
);
2252 memset(ozhcd
, 0, sizeof(*ozhcd
));
2253 INIT_LIST_HEAD(&ozhcd
->urb_pending_list
);
2254 INIT_LIST_HEAD(&ozhcd
->urb_cancel_list
);
2255 INIT_LIST_HEAD(&ozhcd
->orphanage
);
2257 ozhcd
->conn_port
= -1;
2258 spin_lock_init(&ozhcd
->hcd_lock
);
2259 for (i
= 0; i
< OZ_NB_PORTS
; i
++) {
2260 struct oz_port
*port
= &ozhcd
->ports
[i
];
2261 port
->ozhcd
= ozhcd
;
2264 port
->bus_addr
= 0xff;
2265 spin_lock_init(&port
->port_lock
);
2267 err
= usb_add_hcd(hcd
, 0, 0);
2269 oz_dbg(ON
, "Failed to add hcd object OK\n");
2273 device_wakeup_enable(hcd
->self
.controller
);
2275 spin_lock_bh(&g_hcdlock
);
2277 spin_unlock_bh(&g_hcdlock
);
2284 static int oz_plat_remove(struct platform_device
*dev
)
2286 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
2287 struct oz_hcd
*ozhcd
;
2291 ozhcd
= oz_hcd_private(hcd
);
2292 spin_lock_bh(&g_hcdlock
);
2293 if (ozhcd
== g_ozhcd
)
2295 spin_unlock_bh(&g_hcdlock
);
2296 oz_dbg(ON
, "Clearing orphanage\n");
2297 oz_hcd_clear_orphanage(ozhcd
, -EPIPE
);
2298 oz_dbg(ON
, "Removing hcd\n");
2299 usb_remove_hcd(hcd
);
2301 oz_empty_link_pool();
2308 static int oz_plat_suspend(struct platform_device
*dev
, pm_message_t msg
)
2317 static int oz_plat_resume(struct platform_device
*dev
)
2325 int oz_hcd_init(void)
2331 tasklet_init(&g_urb_process_tasklet
, oz_urb_process_tasklet
, 0);
2332 tasklet_init(&g_urb_cancel_tasklet
, oz_urb_cancel_tasklet
, 0);
2333 err
= platform_driver_register(&g_oz_plat_drv
);
2334 oz_dbg(ON
, "platform_driver_register() returned %d\n", err
);
2337 g_plat_dev
= platform_device_alloc(OZ_PLAT_DEV_NAME
, -1);
2338 if (g_plat_dev
== NULL
) {
2342 oz_dbg(ON
, "platform_device_alloc() succeeded\n");
2343 err
= platform_device_add(g_plat_dev
);
2346 oz_dbg(ON
, "platform_device_add() succeeded\n");
2349 platform_device_put(g_plat_dev
);
2351 platform_driver_unregister(&g_oz_plat_drv
);
2353 tasklet_disable(&g_urb_process_tasklet
);
2354 tasklet_disable(&g_urb_cancel_tasklet
);
2355 oz_dbg(ON
, "oz_hcd_init() failed %d\n", err
);
2362 void oz_hcd_term(void)
2364 msleep(OZ_HUB_DEBOUNCE_TIMEOUT
);
2365 tasklet_kill(&g_urb_process_tasklet
);
2366 tasklet_kill(&g_urb_cancel_tasklet
);
2367 platform_device_unregister(g_plat_dev
);
2368 platform_driver_unregister(&g_oz_plat_drv
);
2369 oz_dbg(ON
, "Pending urbs:%d\n", atomic_read(&g_pending_urbs
));