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
, struct urb
*urb
)
359 struct oz_urb_link
*urbl
;
362 list_for_each(e
, &ozhcd
->urb_cancel_list
) {
363 urbl
= container_of(e
, struct oz_urb_link
, link
);
364 if (urb
== urbl
->urb
) {
373 * This is called when we have finished processing an urb. It unlinks it from
374 * the ep and returns it to the core.
375 * Context: softirq or process
377 static void oz_complete_urb(struct usb_hcd
*hcd
, struct urb
*urb
,
380 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
381 unsigned long irq_state
;
382 struct oz_urb_link
*cancel_urbl
;
384 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
385 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
386 /* Clear hcpriv which will prevent it being put in the cancel list
387 * in the event that an attempt is made to cancel it.
390 /* Walk the cancel list in case the urb is already sitting there.
391 * Since we process the cancel list in a tasklet rather than in
392 * the dequeue function this could happen.
394 cancel_urbl
= oz_uncancel_urb(ozhcd
, urb
);
395 /* Note: we release lock but do not enable local irqs.
396 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
397 * or at least other host controllers disable interrupts at this point
398 * so we do the same. We must, however, release the lock otherwise a
399 * deadlock will occur if an urb is submitted to our driver in the urb
400 * completion function. Because we disable interrupts it is possible
401 * that the urb_enqueue function can be called with them disabled.
403 spin_unlock(&g_tasklet_lock
);
404 if (oz_forget_urb(urb
)) {
405 oz_dbg(ON
, "ERROR Unknown URB %p\n", urb
);
407 atomic_dec(&g_pending_urbs
);
408 usb_hcd_giveback_urb(hcd
, urb
, status
);
410 spin_lock(&g_tasklet_lock
);
411 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
413 oz_free_urb_link(cancel_urbl
);
417 * Deallocates an endpoint including deallocating any associated stream and
418 * returning any queued urbs to the core.
421 static void oz_ep_free(struct oz_port
*port
, struct oz_endpoint
*ep
)
424 struct list_head list
;
425 struct oz_hcd
*ozhcd
= port
->ozhcd
;
426 INIT_LIST_HEAD(&list
);
427 if (ep
->flags
& OZ_F_EP_HAVE_STREAM
)
428 oz_usb_stream_delete(port
->hpd
, ep
->ep_num
);
429 /* Transfer URBs to the orphanage while we hold the lock. */
430 spin_lock_bh(&ozhcd
->hcd_lock
);
431 /* Note: this works even if ep->urb_list is empty.*/
432 list_replace_init(&ep
->urb_list
, &list
);
433 /* Put the URBs in the orphanage. */
434 list_splice_tail(&list
, &ozhcd
->orphanage
);
435 spin_unlock_bh(&ozhcd
->hcd_lock
);
437 oz_dbg(ON
, "Freeing endpoint memory\n");
444 static void oz_complete_buffered_urb(struct oz_port
*port
,
445 struct oz_endpoint
*ep
,
448 int data_len
, available_space
, copy_len
;
450 data_len
= ep
->buffer
[ep
->out_ix
];
451 if (data_len
<= urb
->transfer_buffer_length
)
452 available_space
= data_len
;
454 available_space
= urb
->transfer_buffer_length
;
456 if (++ep
->out_ix
== ep
->buffer_size
)
458 copy_len
= ep
->buffer_size
- ep
->out_ix
;
459 if (copy_len
>= available_space
)
460 copy_len
= available_space
;
461 memcpy(urb
->transfer_buffer
, &ep
->buffer
[ep
->out_ix
], copy_len
);
463 if (copy_len
< available_space
) {
464 memcpy((urb
->transfer_buffer
+ copy_len
), ep
->buffer
,
465 (available_space
- copy_len
));
466 ep
->out_ix
= available_space
- copy_len
;
468 ep
->out_ix
+= copy_len
;
470 urb
->actual_length
= available_space
;
471 if (ep
->out_ix
== ep
->buffer_size
)
474 ep
->buffered_units
--;
475 oz_dbg(ON
, "Trying to give back buffered frame of size=%d\n",
477 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
483 static int oz_enqueue_ep_urb(struct oz_port
*port
, u8 ep_addr
, int in_dir
,
484 struct urb
*urb
, u8 req_id
)
486 struct oz_urb_link
*urbl
;
487 struct oz_endpoint
*ep
= NULL
;
490 if (ep_addr
>= OZ_NB_ENDPOINTS
) {
491 oz_dbg(ON
, "%s: Invalid endpoint number\n", __func__
);
494 urbl
= oz_alloc_urb_link();
497 urbl
->submit_counter
= 0;
499 urbl
->req_id
= req_id
;
500 urbl
->ep_num
= ep_addr
;
501 /* Hold lock while we insert the URB into the list within the
502 * endpoint structure.
504 spin_lock_bh(&port
->ozhcd
->hcd_lock
);
505 /* If the urb has been unlinked while out of any list then
509 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
510 oz_dbg(ON
, "urb %p unlinked so complete immediately\n", urb
);
511 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
512 oz_free_urb_link(urbl
);
517 ep
= port
->in_ep
[ep_addr
];
519 ep
= port
->out_ep
[ep_addr
];
525 /*For interrupt endpoint check for buffered data
528 if (((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
529 && ep
->buffered_units
> 0) {
530 oz_free_urb_link(urbl
);
531 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
532 oz_complete_buffered_urb(port
, ep
, urb
);
537 list_add_tail(&urbl
->link
, &ep
->urb_list
);
538 if (!in_dir
&& ep_addr
&& (ep
->credit
< 0)) {
539 getrawmonotonic(&ep
->timestamp
);
546 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
548 oz_free_urb_link(urbl
);
553 * Removes an urb from the queue in the endpoint.
554 * Returns 0 if it is found and -EIDRM otherwise.
557 static int oz_dequeue_ep_urb(struct oz_port
*port
, u8 ep_addr
, int in_dir
,
560 struct oz_urb_link
*urbl
= NULL
;
561 struct oz_endpoint
*ep
;
563 spin_lock_bh(&port
->ozhcd
->hcd_lock
);
565 ep
= port
->in_ep
[ep_addr
];
567 ep
= port
->out_ep
[ep_addr
];
570 list_for_each(e
, &ep
->urb_list
) {
571 urbl
= container_of(e
, struct oz_urb_link
, link
);
572 if (urbl
->urb
== urb
) {
579 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
581 oz_free_urb_link(urbl
);
582 return urbl
? 0 : -EIDRM
;
586 * Finds an urb given its request id.
589 static struct urb
*oz_find_urb_by_id(struct oz_port
*port
, int ep_ix
,
592 struct oz_hcd
*ozhcd
= port
->ozhcd
;
593 struct urb
*urb
= NULL
;
594 struct oz_urb_link
*urbl
;
595 struct oz_endpoint
*ep
;
597 spin_lock_bh(&ozhcd
->hcd_lock
);
598 ep
= port
->out_ep
[ep_ix
];
601 list_for_each(e
, &ep
->urb_list
) {
602 urbl
= container_of(e
, struct oz_urb_link
, link
);
603 if (urbl
->req_id
== req_id
) {
610 spin_unlock_bh(&ozhcd
->hcd_lock
);
611 /* If urb is non-zero then we we must have an urb link to delete.
614 oz_free_urb_link(urbl
);
619 * Pre-condition: Port lock must be held.
622 static void oz_acquire_port(struct oz_port
*port
, void *hpd
)
624 INIT_LIST_HEAD(&port
->isoc_out_ep
);
625 INIT_LIST_HEAD(&port
->isoc_in_ep
);
626 port
->flags
|= OZ_PORT_F_PRESENT
| OZ_PORT_F_CHANGED
;
627 port
->status
|= USB_PORT_STAT_CONNECTION
|
628 (USB_PORT_STAT_C_CONNECTION
<< 16);
636 static struct oz_hcd
*oz_hcd_claim(void)
638 struct oz_hcd
*ozhcd
;
640 spin_lock_bh(&g_hcdlock
);
643 usb_get_hcd(ozhcd
->hcd
);
644 spin_unlock_bh(&g_hcdlock
);
651 static inline void oz_hcd_put(struct oz_hcd
*ozhcd
)
654 usb_put_hcd(ozhcd
->hcd
);
658 * This is called by the protocol handler to notify that a PD has arrived.
659 * We allocate a port to associate with the PD and create a structure for
660 * endpoint 0. This port is made the connection port.
661 * In the event that one of the other port is already a connection port then
663 * TODO We should be able to do better than fail and should be able remember
664 * that this port needs configuring and make it the connection port once the
665 * current connection port has been assigned an address. Collisions here are
666 * probably very rare indeed.
669 struct oz_port
*oz_hcd_pd_arrived(void *hpd
)
672 struct oz_port
*hport
;
673 struct oz_hcd
*ozhcd
;
674 struct oz_endpoint
*ep
;
676 ozhcd
= oz_hcd_claim();
679 /* Allocate an endpoint object in advance (before holding hcd lock) to
680 * use for out endpoint 0.
682 ep
= oz_ep_alloc(0, GFP_ATOMIC
);
686 spin_lock_bh(&ozhcd
->hcd_lock
);
687 if (ozhcd
->conn_port
>= 0)
690 for (i
= 0; i
< OZ_NB_PORTS
; i
++) {
691 struct oz_port
*port
= &ozhcd
->ports
[i
];
693 spin_lock(&port
->port_lock
);
694 if (!(port
->flags
& (OZ_PORT_F_PRESENT
| OZ_PORT_F_CHANGED
))) {
695 oz_acquire_port(port
, hpd
);
696 spin_unlock(&port
->port_lock
);
699 spin_unlock(&port
->port_lock
);
701 if (i
== OZ_NB_PORTS
)
704 ozhcd
->conn_port
= i
;
705 hport
= &ozhcd
->ports
[i
];
706 hport
->out_ep
[0] = ep
;
707 spin_unlock_bh(&ozhcd
->hcd_lock
);
708 if (ozhcd
->flags
& OZ_HDC_F_SUSPENDED
)
709 usb_hcd_resume_root_hub(ozhcd
->hcd
);
710 usb_hcd_poll_rh_status(ozhcd
->hcd
);
716 spin_unlock_bh(&ozhcd
->hcd_lock
);
717 oz_ep_free(NULL
, ep
);
724 * This is called by the protocol handler to notify that the PD has gone away.
725 * We need to deallocate all resources and then request that the root hub is
726 * polled. We release the reference we hold on the PD.
729 void oz_hcd_pd_departed(struct oz_port
*port
)
731 struct oz_hcd
*ozhcd
;
733 struct oz_endpoint
*ep
= NULL
;
736 oz_dbg(ON
, "%s: port = 0\n", __func__
);
742 /* Check if this is the connection port - if so clear it.
744 spin_lock_bh(&ozhcd
->hcd_lock
);
745 if ((ozhcd
->conn_port
>= 0) &&
746 (port
== &ozhcd
->ports
[ozhcd
->conn_port
])) {
747 oz_dbg(ON
, "Clearing conn_port\n");
748 ozhcd
->conn_port
= -1;
750 spin_lock(&port
->port_lock
);
751 port
->flags
|= OZ_PORT_F_DYING
;
752 spin_unlock(&port
->port_lock
);
753 spin_unlock_bh(&ozhcd
->hcd_lock
);
755 oz_clean_endpoints_for_config(ozhcd
->hcd
, port
);
756 spin_lock_bh(&port
->port_lock
);
759 port
->bus_addr
= 0xff;
760 port
->config_num
= 0;
761 port
->flags
&= ~(OZ_PORT_F_PRESENT
| OZ_PORT_F_DYING
);
762 port
->flags
|= OZ_PORT_F_CHANGED
;
763 port
->status
&= ~(USB_PORT_STAT_CONNECTION
| USB_PORT_STAT_ENABLE
);
764 port
->status
|= (USB_PORT_STAT_C_CONNECTION
<< 16);
765 /* If there is an endpont 0 then clear the pointer while we hold
766 * the spinlock be we deallocate it after releasing the lock.
768 if (port
->out_ep
[0]) {
769 ep
= port
->out_ep
[0];
770 port
->out_ep
[0] = NULL
;
772 spin_unlock_bh(&port
->port_lock
);
774 oz_ep_free(port
, ep
);
775 usb_hcd_poll_rh_status(ozhcd
->hcd
);
782 void oz_hcd_pd_reset(void *hpd
, void *hport
)
784 /* Cleanup the current configuration and report reset to the core.
786 struct oz_port
*port
= (struct oz_port
*)hport
;
787 struct oz_hcd
*ozhcd
= port
->ozhcd
;
789 oz_dbg(ON
, "PD Reset\n");
790 spin_lock_bh(&port
->port_lock
);
791 port
->flags
|= OZ_PORT_F_CHANGED
;
792 port
->status
|= USB_PORT_STAT_RESET
;
793 port
->status
|= (USB_PORT_STAT_C_RESET
<< 16);
794 spin_unlock_bh(&port
->port_lock
);
795 oz_clean_endpoints_for_config(ozhcd
->hcd
, port
);
796 usb_hcd_poll_rh_status(ozhcd
->hcd
);
802 void oz_hcd_get_desc_cnf(void *hport
, u8 req_id
, int status
, const u8
*desc
,
803 int length
, int offset
, int total_size
)
805 struct oz_port
*port
= (struct oz_port
*)hport
;
809 oz_dbg(ON
, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
810 length
, offset
, total_size
);
811 urb
= oz_find_urb_by_id(port
, 0, req_id
);
816 int required_size
= urb
->transfer_buffer_length
;
817 if (required_size
> total_size
)
818 required_size
= total_size
;
819 copy_len
= required_size
-offset
;
820 if (length
<= copy_len
)
822 memcpy(urb
->transfer_buffer
+offset
, desc
, copy_len
);
824 if (offset
< required_size
) {
825 struct usb_ctrlrequest
*setup
=
826 (struct usb_ctrlrequest
*)urb
->setup_packet
;
827 unsigned wvalue
= le16_to_cpu(setup
->wValue
);
828 if (oz_enqueue_ep_urb(port
, 0, 0, urb
, req_id
))
830 else if (oz_usb_get_desc_req(port
->hpd
, req_id
,
831 setup
->bRequestType
, (u8
)(wvalue
>>8),
832 (u8
)wvalue
, setup
->wIndex
, offset
,
833 required_size
-offset
)) {
834 oz_dequeue_ep_urb(port
, 0, 0, urb
);
841 urb
->actual_length
= total_size
;
842 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
848 static void oz_display_conf_type(u8 t
)
851 case USB_REQ_GET_STATUS
:
852 oz_dbg(ON
, "USB_REQ_GET_STATUS - cnf\n");
854 case USB_REQ_CLEAR_FEATURE
:
855 oz_dbg(ON
, "USB_REQ_CLEAR_FEATURE - cnf\n");
857 case USB_REQ_SET_FEATURE
:
858 oz_dbg(ON
, "USB_REQ_SET_FEATURE - cnf\n");
860 case USB_REQ_SET_ADDRESS
:
861 oz_dbg(ON
, "USB_REQ_SET_ADDRESS - cnf\n");
863 case USB_REQ_GET_DESCRIPTOR
:
864 oz_dbg(ON
, "USB_REQ_GET_DESCRIPTOR - cnf\n");
866 case USB_REQ_SET_DESCRIPTOR
:
867 oz_dbg(ON
, "USB_REQ_SET_DESCRIPTOR - cnf\n");
869 case USB_REQ_GET_CONFIGURATION
:
870 oz_dbg(ON
, "USB_REQ_GET_CONFIGURATION - cnf\n");
872 case USB_REQ_SET_CONFIGURATION
:
873 oz_dbg(ON
, "USB_REQ_SET_CONFIGURATION - cnf\n");
875 case USB_REQ_GET_INTERFACE
:
876 oz_dbg(ON
, "USB_REQ_GET_INTERFACE - cnf\n");
878 case USB_REQ_SET_INTERFACE
:
879 oz_dbg(ON
, "USB_REQ_SET_INTERFACE - cnf\n");
881 case USB_REQ_SYNCH_FRAME
:
882 oz_dbg(ON
, "USB_REQ_SYNCH_FRAME - cnf\n");
890 static void oz_hcd_complete_set_config(struct oz_port
*port
, struct urb
*urb
,
891 u8 rcode
, u8 config_num
)
894 struct usb_hcd
*hcd
= port
->ozhcd
->hcd
;
897 port
->config_num
= config_num
;
898 oz_clean_endpoints_for_config(hcd
, port
);
899 if (oz_build_endpoints_for_config(hcd
, port
,
900 &urb
->dev
->config
[port
->config_num
-1], GFP_ATOMIC
)) {
906 oz_complete_urb(hcd
, urb
, rc
);
912 static void oz_hcd_complete_set_interface(struct oz_port
*port
, struct urb
*urb
,
913 u8 rcode
, u8 if_num
, u8 alt
)
915 struct usb_hcd
*hcd
= port
->ozhcd
->hcd
;
918 if ((rcode
== 0) && (port
->config_num
> 0)) {
919 struct usb_host_config
*config
;
920 struct usb_host_interface
*intf
;
921 oz_dbg(ON
, "Set interface %d alt %d\n", if_num
, alt
);
922 oz_clean_endpoints_for_interface(hcd
, port
, if_num
);
923 config
= &urb
->dev
->config
[port
->config_num
-1];
924 intf
= &config
->intf_cache
[if_num
]->altsetting
[alt
];
925 if (oz_build_endpoints_for_interface(hcd
, port
, intf
,
929 port
->iface
[if_num
].alt
= alt
;
933 oz_complete_urb(hcd
, urb
, rc
);
939 void oz_hcd_control_cnf(void *hport
, u8 req_id
, u8 rcode
, const u8
*data
,
942 struct oz_port
*port
= (struct oz_port
*)hport
;
944 struct usb_ctrlrequest
*setup
;
945 struct usb_hcd
*hcd
= port
->ozhcd
->hcd
;
949 oz_dbg(ON
, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode
, data_len
);
950 urb
= oz_find_urb_by_id(port
, 0, req_id
);
952 oz_dbg(ON
, "URB not found\n");
955 setup
= (struct usb_ctrlrequest
*)urb
->setup_packet
;
956 windex
= le16_to_cpu(setup
->wIndex
);
957 wvalue
= le16_to_cpu(setup
->wValue
);
958 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
959 /* Standard requests */
960 oz_display_conf_type(setup
->bRequest
);
961 switch (setup
->bRequest
) {
962 case USB_REQ_SET_CONFIGURATION
:
963 oz_hcd_complete_set_config(port
, urb
, rcode
,
966 case USB_REQ_SET_INTERFACE
:
967 oz_hcd_complete_set_interface(port
, urb
, rcode
,
968 (u8
)windex
, (u8
)wvalue
);
971 oz_complete_urb(hcd
, urb
, 0);
976 oz_dbg(ON
, "VENDOR-CLASS - cnf\n");
978 if (data_len
<= urb
->transfer_buffer_length
)
981 copy_len
= urb
->transfer_buffer_length
;
982 memcpy(urb
->transfer_buffer
, data
, copy_len
);
983 urb
->actual_length
= copy_len
;
985 oz_complete_urb(hcd
, urb
, 0);
990 * Context: softirq-serialized
992 static int oz_hcd_buffer_data(struct oz_endpoint
*ep
, const u8
*data
,
1000 space
= ep
->out_ix
-ep
->in_ix
-1;
1002 space
+= ep
->buffer_size
;
1003 if (space
< (data_len
+1)) {
1004 oz_dbg(ON
, "Buffer full\n");
1007 ep
->buffer
[ep
->in_ix
] = (u8
)data_len
;
1008 if (++ep
->in_ix
== ep
->buffer_size
)
1010 copy_len
= ep
->buffer_size
- ep
->in_ix
;
1011 if (copy_len
> data_len
)
1012 copy_len
= data_len
;
1013 memcpy(&ep
->buffer
[ep
->in_ix
], data
, copy_len
);
1015 if (copy_len
< data_len
) {
1016 memcpy(ep
->buffer
, data
+copy_len
, data_len
-copy_len
);
1017 ep
->in_ix
= data_len
-copy_len
;
1019 ep
->in_ix
+= copy_len
;
1021 if (ep
->in_ix
== ep
->buffer_size
)
1023 ep
->buffered_units
++;
1028 * Context: softirq-serialized
1030 void oz_hcd_data_ind(void *hport
, u8 endpoint
, const u8
*data
, int data_len
)
1032 struct oz_port
*port
= (struct oz_port
*)hport
;
1033 struct oz_endpoint
*ep
;
1034 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1036 spin_lock_bh(&ozhcd
->hcd_lock
);
1037 ep
= port
->in_ep
[endpoint
& USB_ENDPOINT_NUMBER_MASK
];
1040 switch (ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
) {
1041 case USB_ENDPOINT_XFER_INT
:
1042 case USB_ENDPOINT_XFER_BULK
:
1043 if (!list_empty(&ep
->urb_list
)) {
1044 struct oz_urb_link
*urbl
=
1045 list_first_entry(&ep
->urb_list
,
1046 struct oz_urb_link
, link
);
1049 list_del_init(&urbl
->link
);
1050 spin_unlock_bh(&ozhcd
->hcd_lock
);
1052 oz_free_urb_link(urbl
);
1053 if (data_len
<= urb
->transfer_buffer_length
)
1054 copy_len
= data_len
;
1056 copy_len
= urb
->transfer_buffer_length
;
1057 memcpy(urb
->transfer_buffer
, data
, copy_len
);
1058 urb
->actual_length
= copy_len
;
1059 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
1062 oz_dbg(ON
, "buffering frame as URB is not available\n");
1063 oz_hcd_buffer_data(ep
, data
, data_len
);
1066 case USB_ENDPOINT_XFER_ISOC
:
1067 oz_hcd_buffer_data(ep
, data
, data_len
);
1071 spin_unlock_bh(&ozhcd
->hcd_lock
);
1077 static inline int oz_usb_get_frame_number(void)
1079 return atomic_inc_return(&g_usb_frame_number
);
1085 int oz_hcd_heartbeat(void *hport
)
1088 struct oz_port
*port
= (struct oz_port
*)hport
;
1089 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1090 struct oz_urb_link
*urbl
;
1091 struct list_head xfr_list
;
1092 struct list_head
*e
;
1093 struct list_head
*n
;
1095 struct oz_endpoint
*ep
;
1096 struct timespec ts
, delta
;
1098 getrawmonotonic(&ts
);
1099 INIT_LIST_HEAD(&xfr_list
);
1100 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1102 spin_lock_bh(&ozhcd
->hcd_lock
);
1103 list_for_each(e
, &port
->isoc_out_ep
) {
1104 ep
= ep_from_link(e
);
1107 delta
= timespec_sub(ts
, ep
->timestamp
);
1108 ep
->credit
+= div_u64(timespec_to_ns(&delta
), NSEC_PER_MSEC
);
1109 if (ep
->credit
> ep
->credit_ceiling
)
1110 ep
->credit
= ep
->credit_ceiling
;
1112 while (ep
->credit
&& !list_empty(&ep
->urb_list
)) {
1113 urbl
= list_first_entry(&ep
->urb_list
,
1114 struct oz_urb_link
, link
);
1116 if ((ep
->credit
+ 1) < urb
->number_of_packets
)
1118 ep
->credit
-= urb
->number_of_packets
;
1121 list_move_tail(&urbl
->link
, &xfr_list
);
1124 spin_unlock_bh(&ozhcd
->hcd_lock
);
1125 /* Send to PD and complete URBs.
1127 list_for_each_safe(e
, n
, &xfr_list
) {
1128 urbl
= container_of(e
, struct oz_urb_link
, link
);
1131 urb
->error_count
= 0;
1132 urb
->start_frame
= oz_usb_get_frame_number();
1133 oz_usb_send_isoc(port
->hpd
, urbl
->ep_num
, urb
);
1134 oz_free_urb_link(urbl
);
1135 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
1137 /* Check the IN isoc endpoints to see if any URBs can be completed.
1139 spin_lock_bh(&ozhcd
->hcd_lock
);
1140 list_for_each(e
, &port
->isoc_in_ep
) {
1141 struct oz_endpoint
*ep
= ep_from_link(e
);
1142 if (ep
->flags
& OZ_F_EP_BUFFERING
) {
1143 if (ep
->buffered_units
>= OZ_IN_BUFFERING_UNITS
) {
1144 ep
->flags
&= ~OZ_F_EP_BUFFERING
;
1147 ep
->start_frame
= 0;
1151 delta
= timespec_sub(ts
, ep
->timestamp
);
1152 ep
->credit
+= div_u64(timespec_to_ns(&delta
), NSEC_PER_MSEC
);
1154 while (!list_empty(&ep
->urb_list
)) {
1155 struct oz_urb_link
*urbl
=
1156 list_first_entry(&ep
->urb_list
,
1157 struct oz_urb_link
, link
);
1158 struct urb
*urb
= urbl
->urb
;
1162 if (ep
->credit
< urb
->number_of_packets
)
1164 if (ep
->buffered_units
< urb
->number_of_packets
)
1166 urb
->actual_length
= 0;
1167 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1168 len
= ep
->buffer
[ep
->out_ix
];
1169 if (++ep
->out_ix
== ep
->buffer_size
)
1171 copy_len
= ep
->buffer_size
- ep
->out_ix
;
1174 memcpy(urb
->transfer_buffer
,
1175 &ep
->buffer
[ep
->out_ix
], copy_len
);
1176 if (copy_len
< len
) {
1177 memcpy(urb
->transfer_buffer
+copy_len
,
1178 ep
->buffer
, len
-copy_len
);
1179 ep
->out_ix
= len
-copy_len
;
1181 ep
->out_ix
+= copy_len
;
1182 if (ep
->out_ix
== ep
->buffer_size
)
1184 urb
->iso_frame_desc
[i
].offset
=
1186 urb
->actual_length
+= len
;
1187 urb
->iso_frame_desc
[i
].actual_length
= len
;
1188 urb
->iso_frame_desc
[i
].status
= 0;
1190 ep
->buffered_units
-= urb
->number_of_packets
;
1191 urb
->error_count
= 0;
1192 urb
->start_frame
= ep
->start_frame
;
1193 ep
->start_frame
+= urb
->number_of_packets
;
1194 list_move_tail(&urbl
->link
, &xfr_list
);
1195 ep
->credit
-= urb
->number_of_packets
;
1198 if (!list_empty(&port
->isoc_out_ep
) || !list_empty(&port
->isoc_in_ep
))
1200 spin_unlock_bh(&ozhcd
->hcd_lock
);
1201 /* Complete the filled URBs.
1203 list_for_each_safe(e
, n
, &xfr_list
) {
1204 urbl
= container_of(e
, struct oz_urb_link
, link
);
1207 oz_free_urb_link(urbl
);
1208 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0);
1210 /* Check if there are any ep0 requests that have timed out.
1211 * If so resent to PD.
1213 ep
= port
->out_ep
[0];
1215 struct list_head
*e
;
1216 struct list_head
*n
;
1217 spin_lock_bh(&ozhcd
->hcd_lock
);
1218 list_for_each_safe(e
, n
, &ep
->urb_list
) {
1219 urbl
= container_of(e
, struct oz_urb_link
, link
);
1220 if (urbl
->submit_counter
> EP0_TIMEOUT_COUNTER
) {
1221 oz_dbg(ON
, "Request 0x%p timeout\n", urbl
->urb
);
1222 list_move_tail(e
, &xfr_list
);
1223 urbl
->submit_counter
= 0;
1225 urbl
->submit_counter
++;
1228 if (!list_empty(&ep
->urb_list
))
1230 spin_unlock_bh(&ozhcd
->hcd_lock
);
1232 while (e
!= &xfr_list
) {
1233 urbl
= container_of(e
, struct oz_urb_link
, link
);
1235 oz_dbg(ON
, "Resending request to PD\n");
1236 oz_process_ep0_urb(ozhcd
, urbl
->urb
, GFP_ATOMIC
);
1237 oz_free_urb_link(urbl
);
1246 static int oz_build_endpoints_for_interface(struct usb_hcd
*hcd
,
1247 struct oz_port
*port
,
1248 struct usb_host_interface
*intf
, gfp_t mem_flags
)
1250 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1252 int if_ix
= intf
->desc
.bInterfaceNumber
;
1253 int request_heartbeat
= 0;
1255 oz_dbg(ON
, "interface[%d] = %p\n", if_ix
, intf
);
1256 if (if_ix
>= port
->num_iface
|| port
->iface
== NULL
)
1258 for (i
= 0; i
< intf
->desc
.bNumEndpoints
; i
++) {
1259 struct usb_host_endpoint
*hep
= &intf
->endpoint
[i
];
1260 u8 ep_addr
= hep
->desc
.bEndpointAddress
;
1261 u8 ep_num
= ep_addr
& USB_ENDPOINT_NUMBER_MASK
;
1262 struct oz_endpoint
*ep
;
1263 int buffer_size
= 0;
1265 oz_dbg(ON
, "%d bEndpointAddress = %x\n", i
, ep_addr
);
1266 if (ep_addr
& USB_ENDPOINT_DIR_MASK
) {
1267 switch (hep
->desc
.bmAttributes
&
1268 USB_ENDPOINT_XFERTYPE_MASK
) {
1269 case USB_ENDPOINT_XFER_ISOC
:
1270 buffer_size
= OZ_EP_BUFFER_SIZE_ISOC
;
1272 case USB_ENDPOINT_XFER_INT
:
1273 buffer_size
= OZ_EP_BUFFER_SIZE_INT
;
1278 ep
= oz_ep_alloc(buffer_size
, mem_flags
);
1280 oz_clean_endpoints_for_interface(hcd
, port
, if_ix
);
1283 ep
->attrib
= hep
->desc
.bmAttributes
;
1284 ep
->ep_num
= ep_num
;
1285 if ((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
)
1286 == USB_ENDPOINT_XFER_ISOC
) {
1287 oz_dbg(ON
, "wMaxPacketSize = %d\n",
1288 usb_endpoint_maxp(&hep
->desc
));
1289 ep
->credit_ceiling
= 200;
1290 if (ep_addr
& USB_ENDPOINT_DIR_MASK
) {
1291 ep
->flags
|= OZ_F_EP_BUFFERING
;
1293 ep
->flags
|= OZ_F_EP_HAVE_STREAM
;
1294 if (oz_usb_stream_create(port
->hpd
, ep_num
))
1295 ep
->flags
&= ~OZ_F_EP_HAVE_STREAM
;
1298 spin_lock_bh(&ozhcd
->hcd_lock
);
1299 if (ep_addr
& USB_ENDPOINT_DIR_MASK
) {
1300 port
->in_ep
[ep_num
] = ep
;
1301 port
->iface
[if_ix
].ep_mask
|=
1302 (1<<(ep_num
+OZ_NB_ENDPOINTS
));
1303 if ((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
)
1304 == USB_ENDPOINT_XFER_ISOC
) {
1305 list_add_tail(&ep
->link
, &port
->isoc_in_ep
);
1306 request_heartbeat
= 1;
1309 port
->out_ep
[ep_num
] = ep
;
1310 port
->iface
[if_ix
].ep_mask
|= (1<<ep_num
);
1311 if ((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
)
1312 == USB_ENDPOINT_XFER_ISOC
) {
1313 list_add_tail(&ep
->link
, &port
->isoc_out_ep
);
1314 request_heartbeat
= 1;
1317 spin_unlock_bh(&ozhcd
->hcd_lock
);
1318 if (request_heartbeat
&& port
->hpd
)
1319 oz_usb_request_heartbeat(port
->hpd
);
1327 static void oz_clean_endpoints_for_interface(struct usb_hcd
*hcd
,
1328 struct oz_port
*port
, int if_ix
)
1330 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1333 struct list_head ep_list
;
1335 oz_dbg(ON
, "Deleting endpoints for interface %d\n", if_ix
);
1336 if (if_ix
>= port
->num_iface
)
1338 INIT_LIST_HEAD(&ep_list
);
1339 spin_lock_bh(&ozhcd
->hcd_lock
);
1340 mask
= port
->iface
[if_ix
].ep_mask
;
1341 port
->iface
[if_ix
].ep_mask
= 0;
1342 for (i
= 0; i
< OZ_NB_ENDPOINTS
; i
++) {
1343 struct list_head
*e
;
1344 /* Gather OUT endpoints.
1346 if ((mask
& (1<<i
)) && port
->out_ep
[i
]) {
1347 e
= &port
->out_ep
[i
]->link
;
1348 port
->out_ep
[i
] = NULL
;
1349 /* Remove from isoc list if present.
1351 list_move_tail(e
, &ep_list
);
1353 /* Gather IN endpoints.
1355 if ((mask
& (1<<(i
+OZ_NB_ENDPOINTS
))) && port
->in_ep
[i
]) {
1356 e
= &port
->in_ep
[i
]->link
;
1357 port
->in_ep
[i
] = NULL
;
1358 list_move_tail(e
, &ep_list
);
1361 spin_unlock_bh(&ozhcd
->hcd_lock
);
1362 while (!list_empty(&ep_list
)) {
1363 struct oz_endpoint
*ep
=
1364 list_first_entry(&ep_list
, struct oz_endpoint
, link
);
1365 list_del_init(&ep
->link
);
1366 oz_ep_free(port
, ep
);
1373 static int oz_build_endpoints_for_config(struct usb_hcd
*hcd
,
1374 struct oz_port
*port
, struct usb_host_config
*config
,
1377 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1379 int num_iface
= config
->desc
.bNumInterfaces
;
1382 struct oz_interface
*iface
;
1384 iface
= kmalloc(num_iface
*sizeof(struct oz_interface
),
1385 mem_flags
| __GFP_ZERO
);
1388 spin_lock_bh(&ozhcd
->hcd_lock
);
1389 port
->iface
= iface
;
1390 port
->num_iface
= num_iface
;
1391 spin_unlock_bh(&ozhcd
->hcd_lock
);
1393 for (i
= 0; i
< num_iface
; i
++) {
1394 struct usb_host_interface
*intf
=
1395 &config
->intf_cache
[i
]->altsetting
[0];
1396 if (oz_build_endpoints_for_interface(hcd
, port
, intf
,
1402 oz_clean_endpoints_for_config(hcd
, port
);
1409 static void oz_clean_endpoints_for_config(struct usb_hcd
*hcd
,
1410 struct oz_port
*port
)
1412 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1415 oz_dbg(ON
, "Deleting endpoints for configuration\n");
1416 for (i
= 0; i
< port
->num_iface
; i
++)
1417 oz_clean_endpoints_for_interface(hcd
, port
, i
);
1418 spin_lock_bh(&ozhcd
->hcd_lock
);
1420 oz_dbg(ON
, "Freeing interfaces object\n");
1424 port
->num_iface
= 0;
1425 spin_unlock_bh(&ozhcd
->hcd_lock
);
1431 static void *oz_claim_hpd(struct oz_port
*port
)
1434 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1436 spin_lock_bh(&ozhcd
->hcd_lock
);
1440 spin_unlock_bh(&ozhcd
->hcd_lock
);
1447 static void oz_process_ep0_urb(struct oz_hcd
*ozhcd
, struct urb
*urb
,
1450 struct usb_ctrlrequest
*setup
;
1457 unsigned complete
= 0;
1460 struct oz_port
*port
= NULL
;
1462 oz_dbg(URB
, "[%s]:(%p)\n", __func__
, urb
);
1463 port_ix
= oz_get_port_from_addr(ozhcd
, urb
->dev
->devnum
);
1468 port
= &ozhcd
->ports
[port_ix
];
1469 if (((port
->flags
& OZ_PORT_F_PRESENT
) == 0)
1470 || (port
->flags
& OZ_PORT_F_DYING
)) {
1471 oz_dbg(ON
, "Refusing URB port_ix = %d devnum = %d\n",
1472 port_ix
, urb
->dev
->devnum
);
1476 /* Store port in private context data.
1479 setup
= (struct usb_ctrlrequest
*)urb
->setup_packet
;
1480 windex
= le16_to_cpu(setup
->wIndex
);
1481 wvalue
= le16_to_cpu(setup
->wValue
);
1482 wlength
= le16_to_cpu(setup
->wLength
);
1483 oz_dbg(CTRL_DETAIL
, "bRequestType = %x\n", setup
->bRequestType
);
1484 oz_dbg(CTRL_DETAIL
, "bRequest = %x\n", setup
->bRequest
);
1485 oz_dbg(CTRL_DETAIL
, "wValue = %x\n", wvalue
);
1486 oz_dbg(CTRL_DETAIL
, "wIndex = %x\n", windex
);
1487 oz_dbg(CTRL_DETAIL
, "wLength = %x\n", wlength
);
1489 req_id
= port
->next_req_id
++;
1490 hpd
= oz_claim_hpd(port
);
1492 oz_dbg(ON
, "Cannot claim port\n");
1497 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1498 /* Standard requests
1500 switch (setup
->bRequest
) {
1501 case USB_REQ_GET_DESCRIPTOR
:
1502 oz_dbg(ON
, "USB_REQ_GET_DESCRIPTOR - req\n");
1504 case USB_REQ_SET_ADDRESS
:
1505 oz_dbg(ON
, "USB_REQ_SET_ADDRESS - req\n");
1506 oz_dbg(ON
, "Port %d address is 0x%x\n",
1508 (u8
)le16_to_cpu(setup
->wValue
));
1509 spin_lock_bh(&ozhcd
->hcd_lock
);
1510 if (ozhcd
->conn_port
>= 0) {
1511 ozhcd
->ports
[ozhcd
->conn_port
].bus_addr
=
1512 (u8
)le16_to_cpu(setup
->wValue
);
1513 oz_dbg(ON
, "Clearing conn_port\n");
1514 ozhcd
->conn_port
= -1;
1516 spin_unlock_bh(&ozhcd
->hcd_lock
);
1519 case USB_REQ_SET_CONFIGURATION
:
1520 oz_dbg(ON
, "USB_REQ_SET_CONFIGURATION - req\n");
1522 case USB_REQ_GET_CONFIGURATION
:
1523 /* We short circuit this case and reply directly since
1524 * we have the selected configuration number cached.
1526 oz_dbg(ON
, "USB_REQ_GET_CONFIGURATION - reply now\n");
1527 if (urb
->transfer_buffer_length
>= 1) {
1528 urb
->actual_length
= 1;
1529 *((u8
*)urb
->transfer_buffer
) =
1536 case USB_REQ_GET_INTERFACE
:
1537 /* We short circuit this case and reply directly since
1538 * we have the selected interface alternative cached.
1540 oz_dbg(ON
, "USB_REQ_GET_INTERFACE - reply now\n");
1541 if (urb
->transfer_buffer_length
>= 1) {
1542 urb
->actual_length
= 1;
1543 *((u8
*)urb
->transfer_buffer
) =
1544 port
->iface
[(u8
)windex
].alt
;
1545 oz_dbg(ON
, "interface = %d alt = %d\n",
1546 windex
, port
->iface
[(u8
)windex
].alt
);
1552 case USB_REQ_SET_INTERFACE
:
1553 oz_dbg(ON
, "USB_REQ_SET_INTERFACE - req\n");
1557 if (!rc
&& !complete
) {
1559 if ((setup
->bRequestType
& USB_DIR_IN
) == 0)
1561 urb
->actual_length
= data_len
;
1562 if (oz_usb_control_req(port
->hpd
, req_id
, setup
,
1563 urb
->transfer_buffer
, data_len
)) {
1566 /* Note: we are queuing the request after we have
1567 * submitted it to be transmitted. If the request were
1568 * to complete before we queued it then it would not
1569 * be found in the queue. It seems impossible for
1570 * this to happen but if it did the request would
1571 * be resubmitted so the problem would hopefully
1572 * resolve itself. Putting the request into the
1573 * queue before it has been sent is worse since the
1574 * urb could be cancelled while we are using it
1575 * to build the request.
1577 if (oz_enqueue_ep_urb(port
, 0, 0, urb
, req_id
))
1583 if (rc
|| complete
) {
1584 oz_dbg(ON
, "Completing request locally\n");
1585 oz_complete_urb(ozhcd
->hcd
, urb
, rc
);
1587 oz_usb_request_heartbeat(port
->hpd
);
1594 static int oz_urb_process(struct oz_hcd
*ozhcd
, struct urb
*urb
)
1597 struct oz_port
*port
= urb
->hcpriv
;
1600 /* When we are paranoid we keep a list of urbs which we check against
1601 * before handing one back. This is just for debugging during
1602 * development and should be turned off in the released driver.
1604 oz_remember_urb(urb
);
1605 /* Check buffer is valid.
1607 if (!urb
->transfer_buffer
&& urb
->transfer_buffer_length
)
1609 /* Check if there is a device at the port - refuse if not.
1611 if ((port
->flags
& OZ_PORT_F_PRESENT
) == 0)
1613 ep_addr
= usb_pipeendpoint(urb
->pipe
);
1615 /* If the request is not for EP0 then queue it.
1617 if (oz_enqueue_ep_urb(port
, ep_addr
, usb_pipein(urb
->pipe
),
1621 oz_process_ep0_urb(ozhcd
, urb
, GFP_ATOMIC
);
1629 static void oz_urb_process_tasklet(unsigned long unused
)
1631 unsigned long irq_state
;
1633 struct oz_hcd
*ozhcd
= oz_hcd_claim();
1638 /* This is called from a tasklet so is in softirq context but the urb
1639 * list is filled from any context so we need to lock
1640 * appropriately while removing urbs.
1642 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1643 while (!list_empty(&ozhcd
->urb_pending_list
)) {
1644 struct oz_urb_link
*urbl
=
1645 list_first_entry(&ozhcd
->urb_pending_list
,
1646 struct oz_urb_link
, link
);
1647 list_del_init(&urbl
->link
);
1648 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1650 oz_free_urb_link(urbl
);
1651 rc
= oz_urb_process(ozhcd
, urb
);
1653 oz_complete_urb(ozhcd
->hcd
, urb
, rc
);
1654 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1656 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1661 * This function searches for the urb in any of the lists it could be in.
1662 * If it is found it is removed from the list and completed. If the urb is
1663 * being processed then it won't be in a list so won't be found. However, the
1664 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1665 * to a non-zero value. When an attempt is made to put the urb back in a list
1666 * the unlinked field will be checked and the urb will then be completed.
1669 static void oz_urb_cancel(struct oz_port
*port
, u8 ep_num
, struct urb
*urb
)
1671 struct oz_urb_link
*urbl
= NULL
;
1672 struct list_head
*e
;
1673 struct oz_hcd
*ozhcd
;
1674 unsigned long irq_state
;
1678 oz_dbg(ON
, "%s: ERROR: (%p) port is null\n", __func__
, urb
);
1681 ozhcd
= port
->ozhcd
;
1682 if (ozhcd
== NULL
) {
1683 oz_dbg(ON
, "%s; ERROR: (%p) ozhcd is null\n", __func__
, urb
);
1687 /* Look in the tasklet queue.
1689 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1690 list_for_each(e
, &ozhcd
->urb_cancel_list
) {
1691 urbl
= container_of(e
, struct oz_urb_link
, link
);
1692 if (urb
== urbl
->urb
) {
1694 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1698 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1701 /* Look in the orphanage.
1703 spin_lock_irqsave(&ozhcd
->hcd_lock
, irq_state
);
1704 list_for_each(e
, &ozhcd
->orphanage
) {
1705 urbl
= container_of(e
, struct oz_urb_link
, link
);
1706 if (urbl
->urb
== urb
) {
1708 oz_dbg(ON
, "Found urb in orphanage\n");
1712 ix
= (ep_num
& 0xf);
1714 if ((ep_num
& USB_DIR_IN
) && ix
)
1715 urbl
= oz_remove_urb(port
->in_ep
[ix
], urb
);
1717 urbl
= oz_remove_urb(port
->out_ep
[ix
], urb
);
1719 spin_unlock_irqrestore(&ozhcd
->hcd_lock
, irq_state
);
1722 urb
->actual_length
= 0;
1723 oz_free_urb_link(urbl
);
1724 oz_complete_urb(ozhcd
->hcd
, urb
, -EPIPE
);
1731 static void oz_urb_cancel_tasklet(unsigned long unused
)
1733 unsigned long irq_state
;
1735 struct oz_hcd
*ozhcd
= oz_hcd_claim();
1739 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1740 while (!list_empty(&ozhcd
->urb_cancel_list
)) {
1741 struct oz_urb_link
*urbl
=
1742 list_first_entry(&ozhcd
->urb_cancel_list
,
1743 struct oz_urb_link
, link
);
1744 list_del_init(&urbl
->link
);
1745 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1748 oz_urb_cancel(urbl
->port
, urbl
->ep_num
, urb
);
1749 oz_free_urb_link(urbl
);
1750 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1752 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1759 static void oz_hcd_clear_orphanage(struct oz_hcd
*ozhcd
, int status
)
1762 struct oz_urb_link
*urbl
;
1763 while (!list_empty(&ozhcd
->orphanage
)) {
1764 urbl
= list_first_entry(&ozhcd
->orphanage
,
1765 struct oz_urb_link
, link
);
1766 list_del(&urbl
->link
);
1767 oz_complete_urb(ozhcd
->hcd
, urbl
->urb
, status
);
1768 oz_free_urb_link(urbl
);
1776 static int oz_hcd_start(struct usb_hcd
*hcd
)
1778 hcd
->power_budget
= 200;
1779 hcd
->state
= HC_STATE_RUNNING
;
1780 hcd
->uses_new_polling
= 1;
1787 static void oz_hcd_stop(struct usb_hcd
*hcd
)
1794 static void oz_hcd_shutdown(struct usb_hcd
*hcd
)
1799 * Called to queue an urb for the device.
1800 * This function should return a non-zero error code if it fails the urb but
1801 * should not call usb_hcd_giveback_urb().
1804 static int oz_hcd_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
1807 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
1810 struct oz_port
*port
;
1811 unsigned long irq_state
;
1812 struct oz_urb_link
*urbl
;
1814 oz_dbg(URB
, "%s: (%p)\n", __func__
, urb
);
1815 if (unlikely(ozhcd
== NULL
)) {
1816 oz_dbg(URB
, "Refused urb(%p) not ozhcd\n", urb
);
1819 if (unlikely(hcd
->state
!= HC_STATE_RUNNING
)) {
1820 oz_dbg(URB
, "Refused urb(%p) not running\n", urb
);
1823 port_ix
= oz_get_port_from_addr(ozhcd
, urb
->dev
->devnum
);
1826 port
= &ozhcd
->ports
[port_ix
];
1829 if (!(port
->flags
& OZ_PORT_F_PRESENT
) ||
1830 (port
->flags
& OZ_PORT_F_CHANGED
)) {
1831 oz_dbg(ON
, "Refusing URB port_ix = %d devnum = %d\n",
1832 port_ix
, urb
->dev
->devnum
);
1836 /* Put request in queue for processing by tasklet.
1838 urbl
= oz_alloc_urb_link();
1839 if (unlikely(urbl
== NULL
))
1842 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1843 rc
= usb_hcd_link_urb_to_ep(hcd
, urb
);
1845 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1846 oz_free_urb_link(urbl
);
1849 list_add_tail(&urbl
->link
, &ozhcd
->urb_pending_list
);
1850 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1851 tasklet_schedule(&g_urb_process_tasklet
);
1852 atomic_inc(&g_pending_urbs
);
1859 static struct oz_urb_link
*oz_remove_urb(struct oz_endpoint
*ep
,
1862 struct oz_urb_link
*urbl
;
1863 struct list_head
*e
;
1865 if (unlikely(ep
== NULL
))
1867 list_for_each(e
, &ep
->urb_list
) {
1868 urbl
= container_of(e
, struct oz_urb_link
, link
);
1869 if (urbl
->urb
== urb
) {
1871 if (usb_pipeisoc(urb
->pipe
)) {
1872 ep
->credit
-= urb
->number_of_packets
;
1883 * Called to dequeue a previously submitted urb for the device.
1886 static int oz_hcd_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1888 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
1889 struct oz_urb_link
*urbl
;
1891 unsigned long irq_state
;
1893 oz_dbg(URB
, "%s: (%p)\n", __func__
, urb
);
1894 urbl
= oz_alloc_urb_link();
1895 if (unlikely(urbl
== NULL
))
1897 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1898 /* The following function checks the urb is still in the queue
1899 * maintained by the core and that the unlinked field is zero.
1900 * If both are true the function sets the unlinked field and returns
1901 * zero. Otherwise it returns an error.
1903 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1904 /* We have to check we haven't completed the urb or are about
1905 * to complete it. When we do we set hcpriv to 0 so if this has
1906 * already happened we don't put the urb in the cancel queue.
1908 if ((rc
== 0) && urb
->hcpriv
) {
1910 urbl
->port
= (struct oz_port
*)urb
->hcpriv
;
1911 urbl
->ep_num
= usb_pipeendpoint(urb
->pipe
);
1912 if (usb_pipein(urb
->pipe
))
1913 urbl
->ep_num
|= USB_DIR_IN
;
1914 list_add_tail(&urbl
->link
, &ozhcd
->urb_cancel_list
);
1915 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1916 tasklet_schedule(&g_urb_cancel_tasklet
);
1918 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1919 oz_free_urb_link(urbl
);
1927 static void oz_hcd_endpoint_disable(struct usb_hcd
*hcd
,
1928 struct usb_host_endpoint
*ep
)
1935 static void oz_hcd_endpoint_reset(struct usb_hcd
*hcd
,
1936 struct usb_host_endpoint
*ep
)
1943 static int oz_hcd_get_frame_number(struct usb_hcd
*hcd
)
1945 oz_dbg(ON
, "oz_hcd_get_frame_number\n");
1946 return oz_usb_get_frame_number();
1951 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1952 * always do that in softirq context.
1954 static int oz_hcd_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
1956 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
1962 spin_lock_bh(&ozhcd
->hcd_lock
);
1963 for (i
= 0; i
< OZ_NB_PORTS
; i
++) {
1964 if (ozhcd
->ports
[i
].flags
& OZ_PORT_F_CHANGED
) {
1965 oz_dbg(HUB
, "Port %d changed\n", i
);
1966 ozhcd
->ports
[i
].flags
&= ~OZ_PORT_F_CHANGED
;
1968 buf
[0] |= 1 << (i
+ 1);
1970 buf
[1] |= 1 << (i
- 7);
1973 spin_unlock_bh(&ozhcd
->hcd_lock
);
1974 if (buf
[0] != 0 || buf
[1] != 0)
1983 static void oz_get_hub_descriptor(struct usb_hcd
*hcd
,
1984 struct usb_hub_descriptor
*desc
)
1986 memset(desc
, 0, sizeof(*desc
));
1987 desc
->bDescriptorType
= 0x29;
1988 desc
->bDescLength
= 9;
1989 desc
->wHubCharacteristics
= (__force __u16
)
1990 __constant_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(__constant_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
));