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/jiffies.h>
30 #include <linux/slab.h>
31 #include <linux/export.h>
32 #include "linux/usb/hcd.h"
33 #include <asm/unaligned.h>
37 #include "ozurbparanoia.h"
39 /*------------------------------------------------------------------------------
40 * Number of units of buffering to capture for an isochronous IN endpoint before
41 * allowing data to be indicated up.
43 #define OZ_IN_BUFFERING_UNITS 50
44 /* Name of our platform device.
46 #define OZ_PLAT_DEV_NAME "ozwpan"
47 /* Maximum number of free urb links that can be kept in the pool.
49 #define OZ_MAX_LINK_POOL_SIZE 16
50 /* Get endpoint object from the containing link.
52 #define ep_from_link(__e) container_of((__e), struct oz_endpoint, link)
53 /*------------------------------------------------------------------------------
54 * Used to link urbs together and also store some status information for each
56 * A cache of these are kept in a pool to reduce number of calls to kmalloc.
59 struct list_head link
;
64 unsigned long submit_jiffies
;
67 /* Holds state information about a USB endpoint.
70 struct list_head urb_list
; /* List of oz_urb_link items. */
71 struct list_head link
; /* For isoc ep, links in to isoc
73 unsigned long last_jiffies
;
86 /* Bits in the flags field. */
87 #define OZ_F_EP_BUFFERING 0x1
88 #define OZ_F_EP_HAVE_STREAM 0x2
90 /* Holds state information about a USB interface.
97 /* Holds state information about an hcd port.
99 #define OZ_NB_ENDPOINTS 16
104 struct oz_hcd
*ozhcd
;
105 spinlock_t port_lock
;
110 struct oz_interface
*iface
;
111 struct oz_endpoint
*out_ep
[OZ_NB_ENDPOINTS
];
112 struct oz_endpoint
*in_ep
[OZ_NB_ENDPOINTS
];
113 struct list_head isoc_out_ep
;
114 struct list_head isoc_in_ep
;
116 #define OZ_PORT_F_PRESENT 0x1
117 #define OZ_PORT_F_CHANGED 0x2
118 #define OZ_PORT_F_DYING 0x4
120 /* Data structure in the private context area of struct usb_hcd.
122 #define OZ_NB_PORTS 8
125 struct list_head urb_pending_list
;
126 struct list_head urb_cancel_list
;
127 struct list_head orphanage
;
128 int conn_port
; /* Port that is currently connecting, -1 if none.*/
129 struct oz_port ports
[OZ_NB_PORTS
];
133 /* Bits in flags field.
135 #define OZ_HDC_F_SUSPENDED 0x1
137 /*------------------------------------------------------------------------------
138 * Static function prototypes.
140 static int oz_hcd_start(struct usb_hcd
*hcd
);
141 static void oz_hcd_stop(struct usb_hcd
*hcd
);
142 static void oz_hcd_shutdown(struct usb_hcd
*hcd
);
143 static int oz_hcd_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
145 static int oz_hcd_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
);
146 static void oz_hcd_endpoint_disable(struct usb_hcd
*hcd
,
147 struct usb_host_endpoint
*ep
);
148 static void oz_hcd_endpoint_reset(struct usb_hcd
*hcd
,
149 struct usb_host_endpoint
*ep
);
150 static int oz_hcd_get_frame_number(struct usb_hcd
*hcd
);
151 static int oz_hcd_hub_status_data(struct usb_hcd
*hcd
, char *buf
);
152 static int oz_hcd_hub_control(struct usb_hcd
*hcd
, u16 req_type
, u16 wvalue
,
153 u16 windex
, char *buf
, u16 wlength
);
154 static int oz_hcd_bus_suspend(struct usb_hcd
*hcd
);
155 static int oz_hcd_bus_resume(struct usb_hcd
*hcd
);
156 static int oz_plat_probe(struct platform_device
*dev
);
157 static int oz_plat_remove(struct platform_device
*dev
);
158 static void oz_plat_shutdown(struct platform_device
*dev
);
159 static int oz_plat_suspend(struct platform_device
*dev
, pm_message_t msg
);
160 static int oz_plat_resume(struct platform_device
*dev
);
161 static void oz_urb_process_tasklet(unsigned long unused
);
162 static int oz_build_endpoints_for_config(struct usb_hcd
*hcd
,
163 struct oz_port
*port
, struct usb_host_config
*config
,
165 static void oz_clean_endpoints_for_config(struct usb_hcd
*hcd
,
166 struct oz_port
*port
);
167 static int oz_build_endpoints_for_interface(struct usb_hcd
*hcd
,
168 struct oz_port
*port
,
169 struct usb_host_interface
*intf
, gfp_t mem_flags
);
170 static void oz_clean_endpoints_for_interface(struct usb_hcd
*hcd
,
171 struct oz_port
*port
, int if_ix
);
172 static void oz_process_ep0_urb(struct oz_hcd
*ozhcd
, struct urb
*urb
,
174 static struct oz_urb_link
*oz_remove_urb(struct oz_endpoint
*ep
,
176 static void oz_hcd_clear_orphanage(struct oz_hcd
*ozhcd
, int status
);
177 /*------------------------------------------------------------------------------
178 * Static external variables.
180 static struct platform_device
*g_plat_dev
;
181 static struct oz_hcd
*g_ozhcd
;
182 static DEFINE_SPINLOCK(g_hcdlock
); /* Guards g_ozhcd. */
183 static const char g_hcd_name
[] = "Ozmo WPAN";
184 static struct list_head
*g_link_pool
;
185 static int g_link_pool_size
;
186 static DEFINE_SPINLOCK(g_link_lock
);
187 static DEFINE_SPINLOCK(g_tasklet_lock
);
188 static struct tasklet_struct g_urb_process_tasklet
;
189 static struct tasklet_struct g_urb_cancel_tasklet
;
190 static atomic_t g_pending_urbs
= ATOMIC_INIT(0);
191 static const struct hc_driver g_oz_hc_drv
= {
192 .description
= g_hcd_name
,
193 .product_desc
= "Ozmo Devices WPAN",
194 .hcd_priv_size
= sizeof(struct oz_hcd
),
196 .start
= oz_hcd_start
,
198 .shutdown
= oz_hcd_shutdown
,
199 .urb_enqueue
= oz_hcd_urb_enqueue
,
200 .urb_dequeue
= oz_hcd_urb_dequeue
,
201 .endpoint_disable
= oz_hcd_endpoint_disable
,
202 .endpoint_reset
= oz_hcd_endpoint_reset
,
203 .get_frame_number
= oz_hcd_get_frame_number
,
204 .hub_status_data
= oz_hcd_hub_status_data
,
205 .hub_control
= oz_hcd_hub_control
,
206 .bus_suspend
= oz_hcd_bus_suspend
,
207 .bus_resume
= oz_hcd_bus_resume
,
210 static struct platform_driver g_oz_plat_drv
= {
211 .probe
= oz_plat_probe
,
212 .remove
= oz_plat_remove
,
213 .shutdown
= oz_plat_shutdown
,
214 .suspend
= oz_plat_suspend
,
215 .resume
= oz_plat_resume
,
217 .name
= OZ_PLAT_DEV_NAME
,
218 .owner
= THIS_MODULE
,
221 /*------------------------------------------------------------------------------
222 * Gets our private context area (which is of type struct oz_hcd) from the
226 static inline struct oz_hcd
*oz_hcd_private(struct usb_hcd
*hcd
)
228 return (struct oz_hcd
*)hcd
->hcd_priv
;
230 /*------------------------------------------------------------------------------
231 * Searches list of ports to find the index of the one with a specified USB
232 * bus address. If none of the ports has the bus address then the connection
233 * port is returned, if there is one or -1 otherwise.
236 static int oz_get_port_from_addr(struct oz_hcd
*ozhcd
, u8 bus_addr
)
239 for (i
= 0; i
< OZ_NB_PORTS
; i
++) {
240 if (ozhcd
->ports
[i
].bus_addr
== bus_addr
)
243 return ozhcd
->conn_port
;
245 /*------------------------------------------------------------------------------
246 * Allocates an urb link, first trying the pool but going to heap if empty.
249 static struct oz_urb_link
*oz_alloc_urb_link(void)
251 struct oz_urb_link
*urbl
= 0;
252 unsigned long irq_state
;
253 spin_lock_irqsave(&g_link_lock
, irq_state
);
255 urbl
= container_of(g_link_pool
, struct oz_urb_link
, link
);
256 g_link_pool
= urbl
->link
.next
;
259 spin_unlock_irqrestore(&g_link_lock
, irq_state
);
261 urbl
= kmalloc(sizeof(struct oz_urb_link
), GFP_ATOMIC
);
264 /*------------------------------------------------------------------------------
265 * Frees an urb link by putting it in the pool if there is enough space or
266 * deallocating it to heap otherwise.
269 static void oz_free_urb_link(struct oz_urb_link
*urbl
)
272 unsigned long irq_state
;
273 spin_lock_irqsave(&g_link_lock
, irq_state
);
274 if (g_link_pool_size
< OZ_MAX_LINK_POOL_SIZE
) {
275 urbl
->link
.next
= g_link_pool
;
276 g_link_pool
= &urbl
->link
;
280 spin_unlock_irqrestore(&g_link_lock
, irq_state
);
285 /*------------------------------------------------------------------------------
286 * Deallocates all the urb links in the pool.
289 static void oz_empty_link_pool(void)
292 unsigned long irq_state
;
293 spin_lock_irqsave(&g_link_lock
, irq_state
);
296 g_link_pool_size
= 0;
297 spin_unlock_irqrestore(&g_link_lock
, irq_state
);
299 struct oz_urb_link
*urbl
=
300 container_of(e
, struct oz_urb_link
, link
);
305 /*------------------------------------------------------------------------------
306 * Allocates endpoint structure and optionally a buffer. If a buffer is
307 * allocated it immediately follows the endpoint structure.
310 static struct oz_endpoint
*oz_ep_alloc(gfp_t mem_flags
, int buffer_size
)
312 struct oz_endpoint
*ep
=
313 kzalloc(sizeof(struct oz_endpoint
)+buffer_size
, mem_flags
);
315 INIT_LIST_HEAD(&ep
->urb_list
);
316 INIT_LIST_HEAD(&ep
->link
);
319 ep
->buffer_size
= buffer_size
;
320 ep
->buffer
= (u8
*)(ep
+1);
325 /*------------------------------------------------------------------------------
326 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
328 * Context: softirq or process
330 struct oz_urb_link
*oz_uncancel_urb(struct oz_hcd
*ozhcd
, struct urb
*urb
)
332 struct oz_urb_link
*urbl
;
334 list_for_each(e
, &ozhcd
->urb_cancel_list
) {
335 urbl
= container_of(e
, struct oz_urb_link
, link
);
336 if (urb
== urbl
->urb
) {
343 /*------------------------------------------------------------------------------
344 * This is called when we have finished processing an urb. It unlinks it from
345 * the ep and returns it to the core.
346 * Context: softirq or process
348 static void oz_complete_urb(struct usb_hcd
*hcd
, struct urb
*urb
,
349 int status
, unsigned long submit_jiffies
)
351 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
352 unsigned long irq_state
;
353 struct oz_urb_link
*cancel_urbl
= 0;
354 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
355 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
356 /* Clear hcpriv which will prevent it being put in the cancel list
357 * in the event that an attempt is made to cancel it.
360 /* Walk the cancel list in case the urb is already sitting there.
361 * Since we process the cancel list in a tasklet rather than in
362 * the dequeue function this could happen.
364 cancel_urbl
= oz_uncancel_urb(ozhcd
, urb
);
365 /* Note: we release lock but do not enable local irqs.
366 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
367 * or at least other host controllers disable interrupts at this point
368 * so we do the same. We must, however, release the lock otherwise a
369 * deadlock will occur if an urb is submitted to our driver in the urb
370 * completion function. Because we disable interrupts it is possible
371 * that the urb_enqueue function can be called with them disabled.
373 spin_unlock(&g_tasklet_lock
);
374 if (oz_forget_urb(urb
)) {
375 oz_trace("OZWPAN: ERROR Unknown URB %p\n", urb
);
377 static unsigned long last_time
;
378 atomic_dec(&g_pending_urbs
);
379 oz_trace2(OZ_TRACE_URB
,
380 "%lu: giveback_urb(%p,%x) %lu %lu pending:%d\n",
381 jiffies
, urb
, status
, jiffies
-submit_jiffies
,
382 jiffies
-last_time
, atomic_read(&g_pending_urbs
));
384 oz_event_log(OZ_EVT_URB_DONE
, 0, 0, urb
, status
);
385 usb_hcd_giveback_urb(hcd
, urb
, status
);
387 spin_lock(&g_tasklet_lock
);
388 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
390 oz_free_urb_link(cancel_urbl
);
392 /*------------------------------------------------------------------------------
393 * Deallocates an endpoint including deallocating any associated stream and
394 * returning any queued urbs to the core.
397 static void oz_ep_free(struct oz_port
*port
, struct oz_endpoint
*ep
)
399 oz_trace("oz_ep_free()\n");
401 struct list_head list
;
402 struct oz_hcd
*ozhcd
= port
->ozhcd
;
403 INIT_LIST_HEAD(&list
);
404 if (ep
->flags
& OZ_F_EP_HAVE_STREAM
)
405 oz_usb_stream_delete(port
->hpd
, ep
->ep_num
);
406 /* Transfer URBs to the orphanage while we hold the lock. */
407 spin_lock_bh(&ozhcd
->hcd_lock
);
408 /* Note: this works even if ep->urb_list is empty.*/
409 list_replace_init(&ep
->urb_list
, &list
);
410 /* Put the URBs in the orphanage. */
411 list_splice_tail(&list
, &ozhcd
->orphanage
);
412 spin_unlock_bh(&ozhcd
->hcd_lock
);
414 oz_trace("Freeing endpoint memory\n");
417 /*------------------------------------------------------------------------------
420 void oz_complete_buffered_urb(struct oz_port
*port
, struct oz_endpoint
*ep
,
423 u8 data_len
, available_space
, copy_len
;
425 memcpy(&data_len
, &ep
->buffer
[ep
->out_ix
], sizeof(u8
));
426 if (data_len
<= urb
->transfer_buffer_length
)
427 available_space
= data_len
;
429 available_space
= urb
->transfer_buffer_length
;
431 if (++ep
->out_ix
== ep
->buffer_size
)
433 copy_len
= ep
->buffer_size
- ep
->out_ix
;
434 if (copy_len
>= available_space
)
435 copy_len
= available_space
;
436 memcpy(urb
->transfer_buffer
, &ep
->buffer
[ep
->out_ix
], copy_len
);
438 if (copy_len
< available_space
) {
439 memcpy((urb
->transfer_buffer
+ copy_len
), ep
->buffer
,
440 (available_space
- copy_len
));
441 ep
->out_ix
= available_space
- copy_len
;
443 ep
->out_ix
+= copy_len
;
445 urb
->actual_length
= available_space
;
446 if (ep
->out_ix
== ep
->buffer_size
)
449 ep
->buffered_units
--;
450 oz_trace("Trying to give back buffered frame of size=%d\n",
452 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0, 0);
455 /*------------------------------------------------------------------------------
458 static int oz_enqueue_ep_urb(struct oz_port
*port
, u8 ep_addr
, int in_dir
,
459 struct urb
*urb
, u8 req_id
)
461 struct oz_urb_link
*urbl
;
462 struct oz_endpoint
*ep
;
464 if (ep_addr
>= OZ_NB_ENDPOINTS
) {
465 oz_trace("Invalid endpoint number in oz_enqueue_ep_urb().\n");
468 urbl
= oz_alloc_urb_link();
471 urbl
->submit_jiffies
= jiffies
;
473 urbl
->req_id
= req_id
;
474 urbl
->ep_num
= ep_addr
;
475 /* Hold lock while we insert the URB into the list within the
476 * endpoint structure.
478 spin_lock_bh(&port
->ozhcd
->hcd_lock
);
479 /* If the urb has been unlinked while out of any list then
483 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
484 oz_trace("urb %p unlinked so complete immediately\n", urb
);
485 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0, 0);
486 oz_free_urb_link(urbl
);
490 ep
= port
->in_ep
[ep_addr
];
492 ep
= port
->out_ep
[ep_addr
];
494 /*For interrupt endpoint check for buffered data
497 if (((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
498 && ep
->buffered_units
> 0) {
499 oz_free_urb_link(urbl
);
500 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
501 oz_complete_buffered_urb(port
, ep
, urb
);
505 if (ep
&& port
->hpd
) {
506 list_add_tail(&urbl
->link
, &ep
->urb_list
);
507 if (!in_dir
&& ep_addr
&& (ep
->credit
< 0)) {
508 ep
->last_jiffies
= jiffies
;
510 oz_event_log(OZ_EVT_EP_CREDIT
, ep
->ep_num
,
516 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
518 oz_free_urb_link(urbl
);
521 /*------------------------------------------------------------------------------
522 * Removes an urb from the queue in the endpoint.
523 * Returns 0 if it is found and -EIDRM otherwise.
526 static int oz_dequeue_ep_urb(struct oz_port
*port
, u8 ep_addr
, int in_dir
,
529 struct oz_urb_link
*urbl
= 0;
530 struct oz_endpoint
*ep
;
531 spin_lock_bh(&port
->ozhcd
->hcd_lock
);
533 ep
= port
->in_ep
[ep_addr
];
535 ep
= port
->out_ep
[ep_addr
];
538 list_for_each(e
, &ep
->urb_list
) {
539 urbl
= container_of(e
, struct oz_urb_link
, link
);
540 if (urbl
->urb
== urb
) {
547 spin_unlock_bh(&port
->ozhcd
->hcd_lock
);
549 oz_free_urb_link(urbl
);
550 return urbl
? 0 : -EIDRM
;
552 /*------------------------------------------------------------------------------
553 * Finds an urb given its request id.
556 static struct urb
*oz_find_urb_by_id(struct oz_port
*port
, int ep_ix
,
559 struct oz_hcd
*ozhcd
= port
->ozhcd
;
561 struct oz_urb_link
*urbl
= 0;
562 struct oz_endpoint
*ep
;
564 spin_lock_bh(&ozhcd
->hcd_lock
);
565 ep
= port
->out_ep
[ep_ix
];
568 list_for_each(e
, &ep
->urb_list
) {
569 urbl
= container_of(e
, struct oz_urb_link
, link
);
570 if (urbl
->req_id
== req_id
) {
577 spin_unlock_bh(&ozhcd
->hcd_lock
);
578 /* If urb is non-zero then we we must have an urb link to delete.
581 oz_free_urb_link(urbl
);
584 /*------------------------------------------------------------------------------
585 * Pre-condition: Port lock must be held.
588 static void oz_acquire_port(struct oz_port
*port
, void *hpd
)
590 INIT_LIST_HEAD(&port
->isoc_out_ep
);
591 INIT_LIST_HEAD(&port
->isoc_in_ep
);
592 port
->flags
|= OZ_PORT_F_PRESENT
| OZ_PORT_F_CHANGED
;
593 port
->status
|= USB_PORT_STAT_CONNECTION
|
594 (USB_PORT_STAT_C_CONNECTION
<< 16);
598 /*------------------------------------------------------------------------------
601 static struct oz_hcd
*oz_hcd_claim(void)
603 struct oz_hcd
*ozhcd
;
604 spin_lock_bh(&g_hcdlock
);
607 usb_get_hcd(ozhcd
->hcd
);
608 spin_unlock_bh(&g_hcdlock
);
611 /*------------------------------------------------------------------------------
614 static inline void oz_hcd_put(struct oz_hcd
*ozhcd
)
617 usb_put_hcd(ozhcd
->hcd
);
619 /*------------------------------------------------------------------------------
620 * This is called by the protocol handler to notify that a PD has arrived.
621 * We allocate a port to associate with the PD and create a structure for
622 * endpoint 0. This port is made the connection port.
623 * In the event that one of the other port is already a connection port then
625 * TODO We should be able to do better than fail and should be able remember
626 * that this port needs configuring and make it the connection port once the
627 * current connection port has been assigned an address. Collisions here are
628 * probably very rare indeed.
631 void *oz_hcd_pd_arrived(void *hpd
)
635 struct oz_hcd
*ozhcd
= 0;
636 struct oz_endpoint
*ep
;
637 oz_trace("oz_hcd_pd_arrived()\n");
638 ozhcd
= oz_hcd_claim();
641 /* Allocate an endpoint object in advance (before holding hcd lock) to
642 * use for out endpoint 0.
644 ep
= oz_ep_alloc(GFP_ATOMIC
, 0);
645 spin_lock_bh(&ozhcd
->hcd_lock
);
646 if (ozhcd
->conn_port
>= 0) {
647 spin_unlock_bh(&ozhcd
->hcd_lock
);
648 oz_trace("conn_port >= 0\n");
651 for (i
= 0; i
< OZ_NB_PORTS
; i
++) {
652 struct oz_port
*port
= &ozhcd
->ports
[i
];
653 spin_lock(&port
->port_lock
);
654 if ((port
->flags
& OZ_PORT_F_PRESENT
) == 0) {
655 oz_acquire_port(port
, hpd
);
656 spin_unlock(&port
->port_lock
);
659 spin_unlock(&port
->port_lock
);
661 if (i
< OZ_NB_PORTS
) {
662 oz_trace("Setting conn_port = %d\n", i
);
663 ozhcd
->conn_port
= i
;
664 /* Attach out endpoint 0.
666 ozhcd
->ports
[i
].out_ep
[0] = ep
;
668 hport
= &ozhcd
->ports
[i
];
669 spin_unlock_bh(&ozhcd
->hcd_lock
);
670 if (ozhcd
->flags
& OZ_HDC_F_SUSPENDED
) {
671 oz_trace("Resuming root hub\n");
672 usb_hcd_resume_root_hub(ozhcd
->hcd
);
674 usb_hcd_poll_rh_status(ozhcd
->hcd
);
676 spin_unlock_bh(&ozhcd
->hcd_lock
);
679 if (ep
) /* ep is non-null if not used. */
684 /*------------------------------------------------------------------------------
685 * This is called by the protocol handler to notify that the PD has gone away.
686 * We need to deallocate all resources and then request that the root hub is
687 * polled. We release the reference we hold on the PD.
690 void oz_hcd_pd_departed(void *hport
)
692 struct oz_port
*port
= (struct oz_port
*)hport
;
693 struct oz_hcd
*ozhcd
;
695 struct oz_endpoint
*ep
= 0;
697 oz_trace("oz_hcd_pd_departed()\n");
699 oz_trace("oz_hcd_pd_departed() port = 0\n");
705 /* Check if this is the connection port - if so clear it.
707 spin_lock_bh(&ozhcd
->hcd_lock
);
708 if ((ozhcd
->conn_port
>= 0) &&
709 (port
== &ozhcd
->ports
[ozhcd
->conn_port
])) {
710 oz_trace("Clearing conn_port\n");
711 ozhcd
->conn_port
= -1;
713 spin_lock(&port
->port_lock
);
714 port
->flags
|= OZ_PORT_F_DYING
;
715 spin_unlock(&port
->port_lock
);
716 spin_unlock_bh(&ozhcd
->hcd_lock
);
718 oz_clean_endpoints_for_config(ozhcd
->hcd
, port
);
719 spin_lock_bh(&port
->port_lock
);
722 port
->bus_addr
= 0xff;
723 port
->flags
&= ~(OZ_PORT_F_PRESENT
| OZ_PORT_F_DYING
);
724 port
->flags
|= OZ_PORT_F_CHANGED
;
725 port
->status
&= ~USB_PORT_STAT_CONNECTION
;
726 port
->status
|= (USB_PORT_STAT_C_CONNECTION
<< 16);
727 /* If there is an endpont 0 then clear the pointer while we hold
728 * the spinlock be we deallocate it after releasing the lock.
730 if (port
->out_ep
[0]) {
731 ep
= port
->out_ep
[0];
734 spin_unlock_bh(&port
->port_lock
);
736 oz_ep_free(port
, ep
);
737 usb_hcd_poll_rh_status(ozhcd
->hcd
);
740 /*------------------------------------------------------------------------------
743 void oz_hcd_pd_reset(void *hpd
, void *hport
)
745 /* Cleanup the current configuration and report reset to the core.
747 struct oz_port
*port
= (struct oz_port
*)hport
;
748 struct oz_hcd
*ozhcd
= port
->ozhcd
;
749 oz_trace("PD Reset\n");
750 spin_lock_bh(&port
->port_lock
);
751 port
->flags
|= OZ_PORT_F_CHANGED
;
752 port
->status
|= USB_PORT_STAT_RESET
;
753 port
->status
|= (USB_PORT_STAT_C_RESET
<< 16);
754 spin_unlock_bh(&port
->port_lock
);
755 oz_clean_endpoints_for_config(ozhcd
->hcd
, port
);
756 usb_hcd_poll_rh_status(ozhcd
->hcd
);
758 /*------------------------------------------------------------------------------
761 void oz_hcd_get_desc_cnf(void *hport
, u8 req_id
, int status
, u8
*desc
,
762 int length
, int offset
, int total_size
)
764 struct oz_port
*port
= (struct oz_port
*)hport
;
768 oz_event_log(OZ_EVT_CTRL_CNF
, 0, req_id
, 0, status
);
769 oz_trace("oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
770 length
, offset
, total_size
);
771 urb
= oz_find_urb_by_id(port
, 0, req_id
);
776 int required_size
= urb
->transfer_buffer_length
;
777 if (required_size
> total_size
)
778 required_size
= total_size
;
779 copy_len
= required_size
-offset
;
780 if (length
<= copy_len
)
782 memcpy(urb
->transfer_buffer
+offset
, desc
, copy_len
);
784 if (offset
< required_size
) {
785 struct usb_ctrlrequest
*setup
=
786 (struct usb_ctrlrequest
*)urb
->setup_packet
;
787 unsigned wvalue
= le16_to_cpu(setup
->wValue
);
788 if (oz_enqueue_ep_urb(port
, 0, 0, urb
, req_id
))
790 else if (oz_usb_get_desc_req(port
->hpd
, req_id
,
791 setup
->bRequestType
, (u8
)(wvalue
>>8),
792 (u8
)wvalue
, setup
->wIndex
, offset
,
793 required_size
-offset
)) {
794 oz_dequeue_ep_urb(port
, 0, 0, urb
);
801 urb
->actual_length
= total_size
;
802 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0, 0);
804 /*------------------------------------------------------------------------------
808 static void oz_display_conf_type(u8 t
)
811 case USB_REQ_GET_STATUS
:
812 oz_trace("USB_REQ_GET_STATUS - cnf\n");
814 case USB_REQ_CLEAR_FEATURE
:
815 oz_trace("USB_REQ_CLEAR_FEATURE - cnf\n");
817 case USB_REQ_SET_FEATURE
:
818 oz_trace("USB_REQ_SET_FEATURE - cnf\n");
820 case USB_REQ_SET_ADDRESS
:
821 oz_trace("USB_REQ_SET_ADDRESS - cnf\n");
823 case USB_REQ_GET_DESCRIPTOR
:
824 oz_trace("USB_REQ_GET_DESCRIPTOR - cnf\n");
826 case USB_REQ_SET_DESCRIPTOR
:
827 oz_trace("USB_REQ_SET_DESCRIPTOR - cnf\n");
829 case USB_REQ_GET_CONFIGURATION
:
830 oz_trace("USB_REQ_GET_CONFIGURATION - cnf\n");
832 case USB_REQ_SET_CONFIGURATION
:
833 oz_trace("USB_REQ_SET_CONFIGURATION - cnf\n");
835 case USB_REQ_GET_INTERFACE
:
836 oz_trace("USB_REQ_GET_INTERFACE - cnf\n");
838 case USB_REQ_SET_INTERFACE
:
839 oz_trace("USB_REQ_SET_INTERFACE - cnf\n");
841 case USB_REQ_SYNCH_FRAME
:
842 oz_trace("USB_REQ_SYNCH_FRAME - cnf\n");
847 #define oz_display_conf_type(__x)
848 #endif /* WANT_TRACE */
849 /*------------------------------------------------------------------------------
852 static void oz_hcd_complete_set_config(struct oz_port
*port
, struct urb
*urb
,
853 u8 rcode
, u8 config_num
)
856 struct usb_hcd
*hcd
= port
->ozhcd
->hcd
;
858 port
->config_num
= config_num
;
859 oz_clean_endpoints_for_config(hcd
, port
);
860 if (oz_build_endpoints_for_config(hcd
, port
,
861 &urb
->dev
->config
[port
->config_num
-1], GFP_ATOMIC
)) {
867 oz_complete_urb(hcd
, urb
, rc
, 0);
869 /*------------------------------------------------------------------------------
872 static void oz_hcd_complete_set_interface(struct oz_port
*port
, struct urb
*urb
,
873 u8 rcode
, u8 if_num
, u8 alt
)
875 struct usb_hcd
*hcd
= port
->ozhcd
->hcd
;
878 struct usb_host_config
*config
;
879 struct usb_host_interface
*intf
;
880 oz_trace("Set interface %d alt %d\n", if_num
, alt
);
881 oz_clean_endpoints_for_interface(hcd
, port
, if_num
);
882 config
= &urb
->dev
->config
[port
->config_num
-1];
883 intf
= &config
->intf_cache
[if_num
]->altsetting
[alt
];
884 if (oz_build_endpoints_for_interface(hcd
, port
, intf
,
888 port
->iface
[if_num
].alt
= alt
;
892 oz_complete_urb(hcd
, urb
, rc
, 0);
894 /*------------------------------------------------------------------------------
897 void oz_hcd_control_cnf(void *hport
, u8 req_id
, u8 rcode
, u8
*data
,
900 struct oz_port
*port
= (struct oz_port
*)hport
;
902 struct usb_ctrlrequest
*setup
;
903 struct usb_hcd
*hcd
= port
->ozhcd
->hcd
;
907 oz_event_log(OZ_EVT_CTRL_CNF
, 0, req_id
, 0, rcode
);
908 oz_trace("oz_hcd_control_cnf rcode=%u len=%d\n", rcode
, data_len
);
909 urb
= oz_find_urb_by_id(port
, 0, req_id
);
911 oz_trace("URB not found\n");
914 setup
= (struct usb_ctrlrequest
*)urb
->setup_packet
;
915 windex
= le16_to_cpu(setup
->wIndex
);
916 wvalue
= le16_to_cpu(setup
->wValue
);
917 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
918 /* Standard requests */
919 oz_display_conf_type(setup
->bRequest
);
920 switch (setup
->bRequest
) {
921 case USB_REQ_SET_CONFIGURATION
:
922 oz_hcd_complete_set_config(port
, urb
, rcode
,
925 case USB_REQ_SET_INTERFACE
:
926 oz_hcd_complete_set_interface(port
, urb
, rcode
,
927 (u8
)windex
, (u8
)wvalue
);
930 oz_complete_urb(hcd
, urb
, 0, 0);
935 oz_trace("VENDOR-CLASS - cnf\n");
937 if (data_len
<= urb
->transfer_buffer_length
)
940 copy_len
= urb
->transfer_buffer_length
;
941 memcpy(urb
->transfer_buffer
, data
, copy_len
);
942 urb
->actual_length
= copy_len
;
944 oz_complete_urb(hcd
, urb
, 0, 0);
947 /*------------------------------------------------------------------------------
948 * Context: softirq-serialized
950 static int oz_hcd_buffer_data(struct oz_endpoint
*ep
, u8
*data
, int data_len
)
956 space
= ep
->out_ix
-ep
->in_ix
-1;
958 space
+= ep
->buffer_size
;
959 if (space
< (data_len
+1)) {
960 oz_trace("Buffer full\n");
963 ep
->buffer
[ep
->in_ix
] = (u8
)data_len
;
964 if (++ep
->in_ix
== ep
->buffer_size
)
966 copy_len
= ep
->buffer_size
- ep
->in_ix
;
967 if (copy_len
> data_len
)
969 memcpy(&ep
->buffer
[ep
->in_ix
], data
, copy_len
);
971 if (copy_len
< data_len
) {
972 memcpy(ep
->buffer
, data
+copy_len
, data_len
-copy_len
);
973 ep
->in_ix
= data_len
-copy_len
;
975 ep
->in_ix
+= copy_len
;
977 if (ep
->in_ix
== ep
->buffer_size
)
979 ep
->buffered_units
++;
982 /*------------------------------------------------------------------------------
983 * Context: softirq-serialized
985 void oz_hcd_data_ind(void *hport
, u8 endpoint
, u8
*data
, int data_len
)
987 struct oz_port
*port
= (struct oz_port
*)hport
;
988 struct oz_endpoint
*ep
;
989 struct oz_hcd
*ozhcd
= port
->ozhcd
;
990 spin_lock_bh(&ozhcd
->hcd_lock
);
991 ep
= port
->in_ep
[endpoint
& USB_ENDPOINT_NUMBER_MASK
];
994 switch (ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
) {
995 case USB_ENDPOINT_XFER_INT
:
996 case USB_ENDPOINT_XFER_BULK
:
997 if (!list_empty(&ep
->urb_list
)) {
998 struct oz_urb_link
*urbl
=
999 list_first_entry(&ep
->urb_list
,
1000 struct oz_urb_link
, link
);
1003 list_del_init(&urbl
->link
);
1004 spin_unlock_bh(&ozhcd
->hcd_lock
);
1006 oz_free_urb_link(urbl
);
1007 if (data_len
<= urb
->transfer_buffer_length
)
1008 copy_len
= data_len
;
1010 copy_len
= urb
->transfer_buffer_length
;
1011 memcpy(urb
->transfer_buffer
, data
, copy_len
);
1012 urb
->actual_length
= copy_len
;
1013 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0, 0);
1016 oz_trace("buffering frame as URB is not available\n");
1017 oz_hcd_buffer_data(ep
, data
, data_len
);
1020 case USB_ENDPOINT_XFER_ISOC
:
1021 oz_hcd_buffer_data(ep
, data
, data_len
);
1025 spin_unlock_bh(&ozhcd
->hcd_lock
);
1027 /*------------------------------------------------------------------------------
1030 static inline int oz_usb_get_frame_number(void)
1032 return jiffies_to_msecs(get_jiffies_64());
1034 /*------------------------------------------------------------------------------
1037 int oz_hcd_heartbeat(void *hport
)
1040 struct oz_port
*port
= (struct oz_port
*)hport
;
1041 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1042 struct oz_urb_link
*urbl
;
1043 struct list_head xfr_list
;
1044 struct list_head
*e
;
1045 struct list_head
*n
;
1047 struct oz_endpoint
*ep
;
1048 unsigned long now
= jiffies
;
1049 INIT_LIST_HEAD(&xfr_list
);
1050 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1052 spin_lock_bh(&ozhcd
->hcd_lock
);
1053 list_for_each(e
, &port
->isoc_out_ep
) {
1054 ep
= ep_from_link(e
);
1057 ep
->credit
+= jiffies_to_msecs(now
- ep
->last_jiffies
);
1058 if (ep
->credit
> ep
->credit_ceiling
)
1059 ep
->credit
= ep
->credit_ceiling
;
1060 oz_event_log(OZ_EVT_EP_CREDIT
, ep
->ep_num
, 0, 0, ep
->credit
);
1061 ep
->last_jiffies
= now
;
1062 while (ep
->credit
&& !list_empty(&ep
->urb_list
)) {
1063 urbl
= list_first_entry(&ep
->urb_list
,
1064 struct oz_urb_link
, link
);
1066 if ((ep
->credit
+ 1) < urb
->number_of_packets
)
1068 ep
->credit
-= urb
->number_of_packets
;
1069 oz_event_log(OZ_EVT_EP_CREDIT
, ep
->ep_num
, 0, 0,
1071 list_move_tail(&urbl
->link
, &xfr_list
);
1074 spin_unlock_bh(&ozhcd
->hcd_lock
);
1075 /* Send to PD and complete URBs.
1077 list_for_each_safe(e
, n
, &xfr_list
) {
1079 urbl
= container_of(e
, struct oz_urb_link
, link
);
1081 t
= urbl
->submit_jiffies
;
1083 urb
->error_count
= 0;
1084 urb
->start_frame
= oz_usb_get_frame_number();
1085 oz_usb_send_isoc(port
->hpd
, urbl
->ep_num
, urb
);
1086 oz_free_urb_link(urbl
);
1087 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0, t
);
1089 /* Check the IN isoc endpoints to see if any URBs can be completed.
1091 spin_lock_bh(&ozhcd
->hcd_lock
);
1092 list_for_each(e
, &port
->isoc_in_ep
) {
1093 struct oz_endpoint
*ep
= ep_from_link(e
);
1094 if (ep
->flags
& OZ_F_EP_BUFFERING
) {
1095 if (ep
->buffered_units
* OZ_IN_BUFFERING_UNITS
) {
1096 ep
->flags
&= ~OZ_F_EP_BUFFERING
;
1098 oz_event_log(OZ_EVT_EP_CREDIT
,
1099 ep
->ep_num
| USB_DIR_IN
,
1101 ep
->last_jiffies
= now
;
1102 ep
->start_frame
= 0;
1103 oz_event_log(OZ_EVT_EP_BUFFERING
,
1104 ep
->ep_num
| USB_DIR_IN
, 0, 0, 0);
1108 ep
->credit
+= jiffies_to_msecs(now
- ep
->last_jiffies
);
1109 oz_event_log(OZ_EVT_EP_CREDIT
, ep
->ep_num
| USB_DIR_IN
,
1111 ep
->last_jiffies
= now
;
1112 while (!list_empty(&ep
->urb_list
)) {
1113 struct oz_urb_link
*urbl
=
1114 list_first_entry(&ep
->urb_list
,
1115 struct oz_urb_link
, link
);
1116 struct urb
*urb
= urbl
->urb
;
1120 if ((ep
->credit
+ 1) < urb
->number_of_packets
)
1122 if (ep
->buffered_units
< urb
->number_of_packets
)
1124 urb
->actual_length
= 0;
1125 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1126 len
= ep
->buffer
[ep
->out_ix
];
1127 if (++ep
->out_ix
== ep
->buffer_size
)
1129 copy_len
= ep
->buffer_size
- ep
->out_ix
;
1132 memcpy(urb
->transfer_buffer
,
1133 &ep
->buffer
[ep
->out_ix
], copy_len
);
1134 if (copy_len
< len
) {
1135 memcpy(urb
->transfer_buffer
+copy_len
,
1136 ep
->buffer
, len
-copy_len
);
1137 ep
->out_ix
= len
-copy_len
;
1139 ep
->out_ix
+= copy_len
;
1140 if (ep
->out_ix
== ep
->buffer_size
)
1142 urb
->iso_frame_desc
[i
].offset
=
1144 urb
->actual_length
+= len
;
1145 urb
->iso_frame_desc
[i
].actual_length
= len
;
1146 urb
->iso_frame_desc
[i
].status
= 0;
1148 ep
->buffered_units
-= urb
->number_of_packets
;
1149 urb
->error_count
= 0;
1150 urb
->start_frame
= ep
->start_frame
;
1151 ep
->start_frame
+= urb
->number_of_packets
;
1152 list_move_tail(&urbl
->link
, &xfr_list
);
1153 ep
->credit
-= urb
->number_of_packets
;
1154 oz_event_log(OZ_EVT_EP_CREDIT
, ep
->ep_num
| USB_DIR_IN
,
1158 if (!list_empty(&port
->isoc_out_ep
) || !list_empty(&port
->isoc_in_ep
))
1160 spin_unlock_bh(&ozhcd
->hcd_lock
);
1161 /* Complete the filled URBs.
1163 list_for_each_safe(e
, n
, &xfr_list
) {
1164 urbl
= container_of(e
, struct oz_urb_link
, link
);
1167 oz_free_urb_link(urbl
);
1168 oz_complete_urb(port
->ozhcd
->hcd
, urb
, 0, 0);
1170 /* Check if there are any ep0 requests that have timed out.
1171 * If so resent to PD.
1173 ep
= port
->out_ep
[0];
1175 struct list_head
*e
;
1176 struct list_head
*n
;
1177 spin_lock_bh(&ozhcd
->hcd_lock
);
1178 list_for_each_safe(e
, n
, &ep
->urb_list
) {
1179 urbl
= container_of(e
, struct oz_urb_link
, link
);
1180 if (time_after(now
, urbl
->submit_jiffies
+HZ
/2)) {
1181 oz_trace("%ld: Request 0x%p timeout\n",
1183 urbl
->submit_jiffies
= now
;
1184 list_move_tail(e
, &xfr_list
);
1187 if (!list_empty(&ep
->urb_list
))
1189 spin_unlock_bh(&ozhcd
->hcd_lock
);
1191 while (e
!= &xfr_list
) {
1192 urbl
= container_of(e
, struct oz_urb_link
, link
);
1194 oz_trace("Resending request to PD.\n");
1195 oz_process_ep0_urb(ozhcd
, urbl
->urb
, GFP_ATOMIC
);
1196 oz_free_urb_link(urbl
);
1201 /*------------------------------------------------------------------------------
1204 static int oz_build_endpoints_for_interface(struct usb_hcd
*hcd
,
1205 struct oz_port
*port
,
1206 struct usb_host_interface
*intf
, gfp_t mem_flags
)
1208 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1210 int if_ix
= intf
->desc
.bInterfaceNumber
;
1211 int request_heartbeat
= 0;
1212 oz_trace("interface[%d] = %p\n", if_ix
, intf
);
1213 for (i
= 0; i
< intf
->desc
.bNumEndpoints
; i
++) {
1214 struct usb_host_endpoint
*hep
= &intf
->endpoint
[i
];
1215 u8 ep_addr
= hep
->desc
.bEndpointAddress
;
1216 u8 ep_num
= ep_addr
& USB_ENDPOINT_NUMBER_MASK
;
1217 struct oz_endpoint
*ep
;
1218 int buffer_size
= 0;
1220 oz_trace("%d bEndpointAddress = %x\n", i
, ep_addr
);
1221 if (ep_addr
& USB_ENDPOINT_DIR_MASK
) {
1222 switch (hep
->desc
.bmAttributes
&
1223 USB_ENDPOINT_XFERTYPE_MASK
) {
1224 case USB_ENDPOINT_XFER_ISOC
:
1225 buffer_size
= 24*1024;
1227 case USB_ENDPOINT_XFER_INT
:
1233 ep
= oz_ep_alloc(mem_flags
, buffer_size
);
1235 oz_clean_endpoints_for_interface(hcd
, port
, if_ix
);
1238 ep
->attrib
= hep
->desc
.bmAttributes
;
1239 ep
->ep_num
= ep_num
;
1240 if ((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
)
1241 == USB_ENDPOINT_XFER_ISOC
) {
1242 oz_trace("wMaxPacketSize = %d\n",
1243 hep
->desc
.wMaxPacketSize
);
1244 ep
->credit_ceiling
= 200;
1245 if (ep_addr
& USB_ENDPOINT_DIR_MASK
) {
1246 ep
->flags
|= OZ_F_EP_BUFFERING
;
1247 oz_event_log(OZ_EVT_EP_BUFFERING
,
1248 ep
->ep_num
| USB_DIR_IN
, 1, 0, 0);
1250 ep
->flags
|= OZ_F_EP_HAVE_STREAM
;
1251 if (oz_usb_stream_create(port
->hpd
, ep_num
))
1252 ep
->flags
&= ~OZ_F_EP_HAVE_STREAM
;
1255 spin_lock_bh(&ozhcd
->hcd_lock
);
1256 if (ep_addr
& USB_ENDPOINT_DIR_MASK
) {
1257 port
->in_ep
[ep_num
] = ep
;
1258 port
->iface
[if_ix
].ep_mask
|=
1259 (1<<(ep_num
+OZ_NB_ENDPOINTS
));
1260 if ((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
)
1261 == USB_ENDPOINT_XFER_ISOC
) {
1262 list_add_tail(&ep
->link
, &port
->isoc_in_ep
);
1263 request_heartbeat
= 1;
1266 port
->out_ep
[ep_num
] = ep
;
1267 port
->iface
[if_ix
].ep_mask
|= (1<<ep_num
);
1268 if ((ep
->attrib
& USB_ENDPOINT_XFERTYPE_MASK
)
1269 == USB_ENDPOINT_XFER_ISOC
) {
1270 list_add_tail(&ep
->link
, &port
->isoc_out_ep
);
1271 request_heartbeat
= 1;
1274 spin_unlock_bh(&ozhcd
->hcd_lock
);
1275 if (request_heartbeat
&& port
->hpd
)
1276 oz_usb_request_heartbeat(port
->hpd
);
1280 /*------------------------------------------------------------------------------
1283 static void oz_clean_endpoints_for_interface(struct usb_hcd
*hcd
,
1284 struct oz_port
*port
, int if_ix
)
1286 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1289 struct list_head ep_list
;
1291 oz_trace("Deleting endpoints for interface %d\n", if_ix
);
1292 if (if_ix
>= port
->num_iface
)
1294 INIT_LIST_HEAD(&ep_list
);
1295 spin_lock_bh(&ozhcd
->hcd_lock
);
1296 mask
= port
->iface
[if_ix
].ep_mask
;
1297 port
->iface
[if_ix
].ep_mask
= 0;
1298 for (i
= 0; i
< OZ_NB_ENDPOINTS
; i
++) {
1299 struct list_head
*e
;
1300 /* Gather OUT endpoints.
1302 if ((mask
& (1<<i
)) && port
->out_ep
[i
]) {
1303 e
= &port
->out_ep
[i
]->link
;
1304 port
->out_ep
[i
] = 0;
1305 /* Remove from isoc list if present.
1307 list_move_tail(e
, &ep_list
);
1309 /* Gather IN endpoints.
1311 if ((mask
& (1<<(i
+OZ_NB_ENDPOINTS
))) && port
->in_ep
[i
]) {
1312 e
= &port
->in_ep
[i
]->link
;
1314 list_move_tail(e
, &ep_list
);
1317 spin_unlock_bh(&ozhcd
->hcd_lock
);
1318 while (!list_empty(&ep_list
)) {
1319 struct oz_endpoint
*ep
=
1320 list_first_entry(&ep_list
, struct oz_endpoint
, link
);
1321 list_del_init(&ep
->link
);
1322 oz_ep_free(port
, ep
);
1325 /*------------------------------------------------------------------------------
1328 static int oz_build_endpoints_for_config(struct usb_hcd
*hcd
,
1329 struct oz_port
*port
, struct usb_host_config
*config
,
1332 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1334 int num_iface
= config
->desc
.bNumInterfaces
;
1336 struct oz_interface
*iface
;
1338 iface
= kmalloc(num_iface
*sizeof(struct oz_interface
),
1339 mem_flags
| __GFP_ZERO
);
1342 spin_lock_bh(&ozhcd
->hcd_lock
);
1343 port
->iface
= iface
;
1344 port
->num_iface
= num_iface
;
1345 spin_unlock_bh(&ozhcd
->hcd_lock
);
1347 for (i
= 0; i
< num_iface
; i
++) {
1348 struct usb_host_interface
*intf
=
1349 &config
->intf_cache
[i
]->altsetting
[0];
1350 if (oz_build_endpoints_for_interface(hcd
, port
, intf
,
1356 oz_clean_endpoints_for_config(hcd
, port
);
1359 /*------------------------------------------------------------------------------
1362 static void oz_clean_endpoints_for_config(struct usb_hcd
*hcd
,
1363 struct oz_port
*port
)
1365 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1367 oz_trace("Deleting endpoints for configuration.\n");
1368 for (i
= 0; i
< port
->num_iface
; i
++)
1369 oz_clean_endpoints_for_interface(hcd
, port
, i
);
1370 spin_lock_bh(&ozhcd
->hcd_lock
);
1372 oz_trace("Freeing interfaces object.\n");
1376 port
->num_iface
= 0;
1377 spin_unlock_bh(&ozhcd
->hcd_lock
);
1379 /*------------------------------------------------------------------------------
1382 static void *oz_claim_hpd(struct oz_port
*port
)
1385 struct oz_hcd
*ozhcd
= port
->ozhcd
;
1386 spin_lock_bh(&ozhcd
->hcd_lock
);
1390 spin_unlock_bh(&ozhcd
->hcd_lock
);
1393 /*------------------------------------------------------------------------------
1396 static void oz_process_ep0_urb(struct oz_hcd
*ozhcd
, struct urb
*urb
,
1399 struct usb_ctrlrequest
*setup
;
1406 unsigned complete
= 0;
1409 struct oz_port
*port
= 0;
1411 oz_trace2(OZ_TRACE_URB
, "%lu: oz_process_ep0_urb(%p)\n", jiffies
, urb
);
1412 port_ix
= oz_get_port_from_addr(ozhcd
, urb
->dev
->devnum
);
1417 port
= &ozhcd
->ports
[port_ix
];
1418 if (((port
->flags
& OZ_PORT_F_PRESENT
) == 0)
1419 || (port
->flags
& OZ_PORT_F_DYING
)) {
1420 oz_trace("Refusing URB port_ix = %d devnum = %d\n",
1421 port_ix
, urb
->dev
->devnum
);
1425 /* Store port in private context data.
1428 setup
= (struct usb_ctrlrequest
*)urb
->setup_packet
;
1429 windex
= le16_to_cpu(setup
->wIndex
);
1430 wvalue
= le16_to_cpu(setup
->wValue
);
1431 wlength
= le16_to_cpu(setup
->wLength
);
1432 oz_trace2(OZ_TRACE_CTRL_DETAIL
, "bRequestType = %x\n",
1433 setup
->bRequestType
);
1434 oz_trace2(OZ_TRACE_CTRL_DETAIL
, "bRequest = %x\n", setup
->bRequest
);
1435 oz_trace2(OZ_TRACE_CTRL_DETAIL
, "wValue = %x\n", wvalue
);
1436 oz_trace2(OZ_TRACE_CTRL_DETAIL
, "wIndex = %x\n", windex
);
1437 oz_trace2(OZ_TRACE_CTRL_DETAIL
, "wLength = %x\n", wlength
);
1439 req_id
= port
->next_req_id
++;
1440 hpd
= oz_claim_hpd(port
);
1442 oz_trace("Cannot claim port\n");
1447 if ((setup
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1448 /* Standard requests
1450 switch (setup
->bRequest
) {
1451 case USB_REQ_GET_DESCRIPTOR
:
1452 oz_trace("USB_REQ_GET_DESCRIPTOR - req\n");
1454 case USB_REQ_SET_ADDRESS
:
1455 oz_event_log(OZ_EVT_CTRL_LOCAL
, setup
->bRequest
,
1456 0, 0, setup
->bRequestType
);
1457 oz_trace("USB_REQ_SET_ADDRESS - req\n");
1458 oz_trace("Port %d address is 0x%x\n", ozhcd
->conn_port
,
1459 (u8
)le16_to_cpu(setup
->wValue
));
1460 spin_lock_bh(&ozhcd
->hcd_lock
);
1461 if (ozhcd
->conn_port
>= 0) {
1462 ozhcd
->ports
[ozhcd
->conn_port
].bus_addr
=
1463 (u8
)le16_to_cpu(setup
->wValue
);
1464 oz_trace("Clearing conn_port\n");
1465 ozhcd
->conn_port
= -1;
1467 spin_unlock_bh(&ozhcd
->hcd_lock
);
1470 case USB_REQ_SET_CONFIGURATION
:
1471 oz_trace("USB_REQ_SET_CONFIGURATION - req\n");
1473 case USB_REQ_GET_CONFIGURATION
:
1474 /* We short circuit this case and reply directly since
1475 * we have the selected configuration number cached.
1477 oz_event_log(OZ_EVT_CTRL_LOCAL
, setup
->bRequest
, 0, 0,
1478 setup
->bRequestType
);
1479 oz_trace("USB_REQ_GET_CONFIGURATION - reply now\n");
1480 if (urb
->transfer_buffer_length
>= 1) {
1481 urb
->actual_length
= 1;
1482 *((u8
*)urb
->transfer_buffer
) =
1489 case USB_REQ_GET_INTERFACE
:
1490 /* We short circuit this case and reply directly since
1491 * we have the selected interface alternative cached.
1493 oz_event_log(OZ_EVT_CTRL_LOCAL
, setup
->bRequest
, 0, 0,
1494 setup
->bRequestType
);
1495 oz_trace("USB_REQ_GET_INTERFACE - reply now\n");
1496 if (urb
->transfer_buffer_length
>= 1) {
1497 urb
->actual_length
= 1;
1498 *((u8
*)urb
->transfer_buffer
) =
1499 port
->iface
[(u8
)windex
].alt
;
1500 oz_trace("interface = %d alt = %d\n",
1501 windex
, port
->iface
[(u8
)windex
].alt
);
1507 case USB_REQ_SET_INTERFACE
:
1508 oz_trace("USB_REQ_SET_INTERFACE - req\n");
1512 if (!rc
&& !complete
) {
1514 if ((setup
->bRequestType
& USB_DIR_IN
) == 0)
1516 urb
->actual_length
= data_len
;
1517 if (oz_usb_control_req(port
->hpd
, req_id
, setup
,
1518 urb
->transfer_buffer
, data_len
)) {
1521 /* Note: we are queuing the request after we have
1522 * submitted it to be transmitted. If the request were
1523 * to complete before we queued it then it would not
1524 * be found in the queue. It seems impossible for
1525 * this to happen but if it did the request would
1526 * be resubmitted so the problem would hopefully
1527 * resolve itself. Putting the request into the
1528 * queue before it has been sent is worse since the
1529 * urb could be cancelled while we are using it
1530 * to build the request.
1532 if (oz_enqueue_ep_urb(port
, 0, 0, urb
, req_id
))
1538 if (rc
|| complete
) {
1539 oz_trace("Completing request locally\n");
1540 oz_complete_urb(ozhcd
->hcd
, urb
, rc
, 0);
1542 oz_usb_request_heartbeat(port
->hpd
);
1545 /*------------------------------------------------------------------------------
1548 static int oz_urb_process(struct oz_hcd
*ozhcd
, struct urb
*urb
)
1551 struct oz_port
*port
= urb
->hcpriv
;
1553 /* When we are paranoid we keep a list of urbs which we check against
1554 * before handing one back. This is just for debugging during
1555 * development and should be turned off in the released driver.
1557 oz_remember_urb(urb
);
1558 /* Check buffer is valid.
1560 if (!urb
->transfer_buffer
&& urb
->transfer_buffer_length
)
1562 /* Check if there is a device at the port - refuse if not.
1564 if ((port
->flags
& OZ_PORT_F_PRESENT
) == 0)
1566 ep_addr
= usb_pipeendpoint(urb
->pipe
);
1568 /* If the request is not for EP0 then queue it.
1570 if (oz_enqueue_ep_urb(port
, ep_addr
, usb_pipein(urb
->pipe
),
1574 oz_process_ep0_urb(ozhcd
, urb
, GFP_ATOMIC
);
1578 /*------------------------------------------------------------------------------
1581 static void oz_urb_process_tasklet(unsigned long unused
)
1583 unsigned long irq_state
;
1585 struct oz_hcd
*ozhcd
= oz_hcd_claim();
1589 /* This is called from a tasklet so is in softirq context but the urb
1590 * list is filled from any context so we need to lock
1591 * appropriately while removing urbs.
1593 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1594 while (!list_empty(&ozhcd
->urb_pending_list
)) {
1595 struct oz_urb_link
*urbl
=
1596 list_first_entry(&ozhcd
->urb_pending_list
,
1597 struct oz_urb_link
, link
);
1598 list_del_init(&urbl
->link
);
1599 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1601 oz_free_urb_link(urbl
);
1602 rc
= oz_urb_process(ozhcd
, urb
);
1604 oz_complete_urb(ozhcd
->hcd
, urb
, rc
, 0);
1605 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1607 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1610 /*------------------------------------------------------------------------------
1611 * This function searches for the urb in any of the lists it could be in.
1612 * If it is found it is removed from the list and completed. If the urb is
1613 * being processed then it won't be in a list so won't be found. However, the
1614 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1615 * to a non-zero value. When an attempt is made to put the urb back in a list
1616 * the unlinked field will be checked and the urb will then be completed.
1619 static void oz_urb_cancel(struct oz_port
*port
, u8 ep_num
, struct urb
*urb
)
1621 struct oz_urb_link
*urbl
= 0;
1622 struct list_head
*e
;
1623 struct oz_hcd
*ozhcd
;
1624 unsigned long irq_state
;
1627 oz_trace("ERRORERROR: oz_urb_cancel(%p) port is null\n", urb
);
1630 ozhcd
= port
->ozhcd
;
1632 oz_trace("ERRORERROR: oz_urb_cancel(%p) ozhcd is null\n", urb
);
1636 /* Look in the tasklet queue.
1638 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1639 list_for_each(e
, &ozhcd
->urb_cancel_list
) {
1640 urbl
= container_of(e
, struct oz_urb_link
, link
);
1641 if (urb
== urbl
->urb
) {
1643 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1647 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1650 /* Look in the orphanage.
1652 spin_lock_irqsave(&ozhcd
->hcd_lock
, irq_state
);
1653 list_for_each(e
, &ozhcd
->orphanage
) {
1654 urbl
= container_of(e
, struct oz_urb_link
, link
);
1655 if (urbl
->urb
== urb
) {
1657 oz_trace("Found urb in orphanage\n");
1661 ix
= (ep_num
& 0xf);
1663 if ((ep_num
& USB_DIR_IN
) && ix
)
1664 urbl
= oz_remove_urb(port
->in_ep
[ix
], urb
);
1666 urbl
= oz_remove_urb(port
->out_ep
[ix
], urb
);
1668 spin_unlock_irqrestore(&ozhcd
->hcd_lock
, irq_state
);
1671 urb
->actual_length
= 0;
1672 oz_free_urb_link(urbl
);
1673 oz_complete_urb(ozhcd
->hcd
, urb
, -EPIPE
, 0);
1676 /*------------------------------------------------------------------------------
1679 static void oz_urb_cancel_tasklet(unsigned long unused
)
1681 unsigned long irq_state
;
1683 struct oz_hcd
*ozhcd
= oz_hcd_claim();
1686 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1687 while (!list_empty(&ozhcd
->urb_cancel_list
)) {
1688 struct oz_urb_link
*urbl
=
1689 list_first_entry(&ozhcd
->urb_cancel_list
,
1690 struct oz_urb_link
, link
);
1691 list_del_init(&urbl
->link
);
1692 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1695 oz_urb_cancel(urbl
->port
, urbl
->ep_num
, urb
);
1696 oz_free_urb_link(urbl
);
1697 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1699 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1702 /*------------------------------------------------------------------------------
1705 static void oz_hcd_clear_orphanage(struct oz_hcd
*ozhcd
, int status
)
1708 struct oz_urb_link
*urbl
;
1709 while (!list_empty(&ozhcd
->orphanage
)) {
1710 urbl
= list_first_entry(&ozhcd
->orphanage
,
1711 struct oz_urb_link
, link
);
1712 list_del(&urbl
->link
);
1713 oz_complete_urb(ozhcd
->hcd
, urbl
->urb
, status
, 0);
1714 oz_free_urb_link(urbl
);
1718 /*------------------------------------------------------------------------------
1721 static int oz_hcd_start(struct usb_hcd
*hcd
)
1723 oz_trace("oz_hcd_start()\n");
1724 hcd
->power_budget
= 200;
1725 hcd
->state
= HC_STATE_RUNNING
;
1726 hcd
->uses_new_polling
= 1;
1729 /*------------------------------------------------------------------------------
1732 static void oz_hcd_stop(struct usb_hcd
*hcd
)
1734 oz_trace("oz_hcd_stop()\n");
1736 /*------------------------------------------------------------------------------
1739 static void oz_hcd_shutdown(struct usb_hcd
*hcd
)
1741 oz_trace("oz_hcd_shutdown()\n");
1743 /*------------------------------------------------------------------------------
1746 #ifdef WANT_EVENT_TRACE
1747 static u8
oz_get_irq_ctx(void)
1756 #endif /* WANT_EVENT_TRACE */
1757 /*------------------------------------------------------------------------------
1758 * Called to queue an urb for the device.
1759 * This function should return a non-zero error code if it fails the urb but
1760 * should not call usb_hcd_giveback_urb().
1763 static int oz_hcd_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
1766 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
1769 struct oz_port
*port
;
1770 unsigned long irq_state
;
1771 struct oz_urb_link
*urbl
;
1772 oz_trace2(OZ_TRACE_URB
, "%lu: oz_hcd_urb_enqueue(%p)\n",
1774 oz_event_log(OZ_EVT_URB_SUBMIT
, oz_get_irq_ctx(),
1775 (u16
)urb
->number_of_packets
, urb
, urb
->pipe
);
1776 if (unlikely(ozhcd
== 0)) {
1777 oz_trace2(OZ_TRACE_URB
, "%lu: Refused urb(%p) not ozhcd.\n",
1781 if (unlikely(hcd
->state
!= HC_STATE_RUNNING
)) {
1782 oz_trace2(OZ_TRACE_URB
, "%lu: Refused urb(%p) not running.\n",
1786 port_ix
= oz_get_port_from_addr(ozhcd
, urb
->dev
->devnum
);
1789 port
= &ozhcd
->ports
[port_ix
];
1792 if ((port
->flags
& OZ_PORT_F_PRESENT
) == 0) {
1793 oz_trace("Refusing URB port_ix = %d devnum = %d\n",
1794 port_ix
, urb
->dev
->devnum
);
1798 /* Put request in queue for processing by tasklet.
1800 urbl
= oz_alloc_urb_link();
1801 if (unlikely(urbl
== 0))
1804 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1805 rc
= usb_hcd_link_urb_to_ep(hcd
, urb
);
1807 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1808 oz_free_urb_link(urbl
);
1811 list_add_tail(&urbl
->link
, &ozhcd
->urb_pending_list
);
1812 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1813 tasklet_schedule(&g_urb_process_tasklet
);
1814 atomic_inc(&g_pending_urbs
);
1817 /*------------------------------------------------------------------------------
1820 static struct oz_urb_link
*oz_remove_urb(struct oz_endpoint
*ep
,
1823 struct oz_urb_link
*urbl
= 0;
1824 struct list_head
*e
;
1825 if (unlikely(ep
== 0))
1827 list_for_each(e
, &ep
->urb_list
) {
1828 urbl
= container_of(e
, struct oz_urb_link
, link
);
1829 if (urbl
->urb
== urb
) {
1831 if (usb_pipeisoc(urb
->pipe
)) {
1832 ep
->credit
-= urb
->number_of_packets
;
1835 oz_event_log(OZ_EVT_EP_CREDIT
,
1836 usb_pipein(urb
->pipe
) ?
1837 (ep
->ep_num
| USB_DIR_IN
) : ep
->ep_num
,
1845 /*------------------------------------------------------------------------------
1846 * Called to dequeue a previously submitted urb for the device.
1849 static int oz_hcd_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1851 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
1852 struct oz_urb_link
*urbl
= 0;
1854 unsigned long irq_state
;
1855 oz_trace2(OZ_TRACE_URB
, "%lu: oz_hcd_urb_dequeue(%p)\n", jiffies
, urb
);
1856 urbl
= oz_alloc_urb_link();
1857 if (unlikely(urbl
== 0))
1859 spin_lock_irqsave(&g_tasklet_lock
, irq_state
);
1860 /* The following function checks the urb is still in the queue
1861 * maintained by the core and that the unlinked field is zero.
1862 * If both are true the function sets the unlinked field and returns
1863 * zero. Otherwise it returns an error.
1865 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1866 /* We have to check we haven't completed the urb or are about
1867 * to complete it. When we do we set hcpriv to 0 so if this has
1868 * already happened we don't put the urb in the cancel queue.
1870 if ((rc
== 0) && urb
->hcpriv
) {
1872 urbl
->port
= (struct oz_port
*)urb
->hcpriv
;
1873 urbl
->ep_num
= usb_pipeendpoint(urb
->pipe
);
1874 if (usb_pipein(urb
->pipe
))
1875 urbl
->ep_num
|= USB_DIR_IN
;
1876 list_add_tail(&urbl
->link
, &ozhcd
->urb_cancel_list
);
1877 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1878 tasklet_schedule(&g_urb_cancel_tasklet
);
1880 spin_unlock_irqrestore(&g_tasklet_lock
, irq_state
);
1881 oz_free_urb_link(urbl
);
1885 /*------------------------------------------------------------------------------
1888 static void oz_hcd_endpoint_disable(struct usb_hcd
*hcd
,
1889 struct usb_host_endpoint
*ep
)
1891 oz_trace("oz_hcd_endpoint_disable\n");
1893 /*------------------------------------------------------------------------------
1896 static void oz_hcd_endpoint_reset(struct usb_hcd
*hcd
,
1897 struct usb_host_endpoint
*ep
)
1899 oz_trace("oz_hcd_endpoint_reset\n");
1901 /*------------------------------------------------------------------------------
1904 static int oz_hcd_get_frame_number(struct usb_hcd
*hcd
)
1906 oz_trace("oz_hcd_get_frame_number\n");
1907 return oz_usb_get_frame_number();
1909 /*------------------------------------------------------------------------------
1911 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1912 * always do that in softirq context.
1914 static int oz_hcd_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
1916 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
1919 oz_trace2(OZ_TRACE_HUB
, "oz_hcd_hub_status_data()\n");
1922 spin_lock_bh(&ozhcd
->hcd_lock
);
1923 for (i
= 0; i
< OZ_NB_PORTS
; i
++) {
1924 if (ozhcd
->ports
[i
].flags
& OZ_PORT_F_CHANGED
) {
1925 oz_trace2(OZ_TRACE_HUB
, "Port %d changed\n", i
);
1926 ozhcd
->ports
[i
].flags
&= ~OZ_PORT_F_CHANGED
;
1930 spin_unlock_bh(&ozhcd
->hcd_lock
);
1931 return buf
[0] ? 1 : 0;
1933 /*------------------------------------------------------------------------------
1936 static void oz_get_hub_descriptor(struct usb_hcd
*hcd
,
1937 struct usb_hub_descriptor
*desc
)
1939 oz_trace2(OZ_TRACE_HUB
, "GetHubDescriptor\n");
1940 memset(desc
, 0, sizeof(*desc
));
1941 desc
->bDescriptorType
= 0x29;
1942 desc
->bDescLength
= 9;
1943 desc
->wHubCharacteristics
= (__force __u16
)
1944 __constant_cpu_to_le16(0x0001);
1945 desc
->bNbrPorts
= OZ_NB_PORTS
;
1947 /*------------------------------------------------------------------------------
1950 static int oz_set_port_feature(struct usb_hcd
*hcd
, u16 wvalue
, u16 windex
)
1952 struct oz_port
*port
;
1954 u8 port_id
= (u8
)windex
;
1955 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
1956 unsigned set_bits
= 0;
1957 unsigned clear_bits
= 0;
1958 oz_trace2(OZ_TRACE_HUB
, "SetPortFeature\n");
1959 if ((port_id
< 1) || (port_id
> OZ_NB_PORTS
))
1961 port
= &ozhcd
->ports
[port_id
-1];
1963 case USB_PORT_FEAT_CONNECTION
:
1964 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_CONNECTION\n");
1966 case USB_PORT_FEAT_ENABLE
:
1967 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_ENABLE\n");
1969 case USB_PORT_FEAT_SUSPEND
:
1970 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_SUSPEND\n");
1972 case USB_PORT_FEAT_OVER_CURRENT
:
1973 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_OVER_CURRENT\n");
1975 case USB_PORT_FEAT_RESET
:
1976 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_RESET\n");
1977 set_bits
= USB_PORT_STAT_ENABLE
| (USB_PORT_STAT_C_RESET
<<16);
1978 clear_bits
= USB_PORT_STAT_RESET
;
1979 ozhcd
->ports
[port_id
-1].bus_addr
= 0;
1981 case USB_PORT_FEAT_POWER
:
1982 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_POWER\n");
1983 set_bits
|= USB_PORT_STAT_POWER
;
1985 case USB_PORT_FEAT_LOWSPEED
:
1986 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_LOWSPEED\n");
1988 case USB_PORT_FEAT_C_CONNECTION
:
1989 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_C_CONNECTION\n");
1991 case USB_PORT_FEAT_C_ENABLE
:
1992 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_C_ENABLE\n");
1994 case USB_PORT_FEAT_C_SUSPEND
:
1995 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_C_SUSPEND\n");
1997 case USB_PORT_FEAT_C_OVER_CURRENT
:
1998 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2000 case USB_PORT_FEAT_C_RESET
:
2001 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_C_RESET\n");
2003 case USB_PORT_FEAT_TEST
:
2004 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_TEST\n");
2006 case USB_PORT_FEAT_INDICATOR
:
2007 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_INDICATOR\n");
2010 oz_trace2(OZ_TRACE_HUB
, "Other %d\n", wvalue
);
2013 if (set_bits
|| clear_bits
) {
2014 spin_lock_bh(&port
->port_lock
);
2015 port
->status
&= ~clear_bits
;
2016 port
->status
|= set_bits
;
2017 spin_unlock_bh(&port
->port_lock
);
2019 oz_trace2(OZ_TRACE_HUB
, "Port[%d] status = 0x%x\n", port_id
,
2023 /*------------------------------------------------------------------------------
2026 static int oz_clear_port_feature(struct usb_hcd
*hcd
, u16 wvalue
, u16 windex
)
2028 struct oz_port
*port
;
2030 u8 port_id
= (u8
)windex
;
2031 struct oz_hcd
*ozhcd
= oz_hcd_private(hcd
);
2032 unsigned clear_bits
= 0;
2033 oz_trace2(OZ_TRACE_HUB
, "ClearPortFeature\n");
2034 if ((port_id
< 1) || (port_id
> OZ_NB_PORTS
))
2036 port
= &ozhcd
->ports
[port_id
-1];
2038 case USB_PORT_FEAT_CONNECTION
:
2039 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_CONNECTION\n");
2041 case USB_PORT_FEAT_ENABLE
:
2042 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_ENABLE\n");
2043 clear_bits
= USB_PORT_STAT_ENABLE
;
2045 case USB_PORT_FEAT_SUSPEND
:
2046 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_SUSPEND\n");
2048 case USB_PORT_FEAT_OVER_CURRENT
:
2049 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_OVER_CURRENT\n");
2051 case USB_PORT_FEAT_RESET
:
2052 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_RESET\n");
2054 case USB_PORT_FEAT_POWER
:
2055 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_POWER\n");
2056 clear_bits
|= USB_PORT_STAT_POWER
;
2058 case USB_PORT_FEAT_LOWSPEED
:
2059 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_LOWSPEED\n");
2061 case USB_PORT_FEAT_C_CONNECTION
:
2062 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_C_CONNECTION\n");
2063 clear_bits
= (USB_PORT_STAT_C_CONNECTION
<< 16);
2065 case USB_PORT_FEAT_C_ENABLE
:
2066 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_C_ENABLE\n");
2067 clear_bits
= (USB_PORT_STAT_C_ENABLE
<< 16);
2069 case USB_PORT_FEAT_C_SUSPEND
:
2070 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_C_SUSPEND\n");
2072 case USB_PORT_FEAT_C_OVER_CURRENT
:
2073 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2075 case USB_PORT_FEAT_C_RESET
:
2076 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_C_RESET\n");
2077 clear_bits
= (USB_PORT_FEAT_C_RESET
<< 16);
2079 case USB_PORT_FEAT_TEST
:
2080 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_TEST\n");
2082 case USB_PORT_FEAT_INDICATOR
:
2083 oz_trace2(OZ_TRACE_HUB
, "USB_PORT_FEAT_INDICATOR\n");
2086 oz_trace2(OZ_TRACE_HUB
, "Other %d\n", wvalue
);
2090 spin_lock_bh(&port
->port_lock
);
2091 port
->status
&= ~clear_bits
;
2092 spin_unlock_bh(&port
->port_lock
);
2094 oz_trace2(OZ_TRACE_HUB
, "Port[%d] status = 0x%x\n", port_id
,
2095 ozhcd
->ports
[port_id
-1].status
);
2098 /*------------------------------------------------------------------------------
2101 static int oz_get_port_status(struct usb_hcd
*hcd
, u16 windex
, char *buf
)
2103 struct oz_hcd
*ozhcd
;
2105 if ((windex
< 1) || (windex
> OZ_NB_PORTS
))
2107 ozhcd
= oz_hcd_private(hcd
);
2108 oz_trace2(OZ_TRACE_HUB
, "GetPortStatus windex = %d\n", windex
);
2109 status
= ozhcd
->ports
[windex
-1].status
;
2110 put_unaligned(cpu_to_le32(status
), (__le32
*)buf
);
2111 oz_trace2(OZ_TRACE_HUB
, "Port[%d] status = %x\n", windex
, status
);
2114 /*------------------------------------------------------------------------------
2117 static int oz_hcd_hub_control(struct usb_hcd
*hcd
, u16 req_type
, u16 wvalue
,
2118 u16 windex
, char *buf
, u16 wlength
)
2121 oz_trace2(OZ_TRACE_HUB
, "oz_hcd_hub_control()\n");
2123 case ClearHubFeature
:
2124 oz_trace2(OZ_TRACE_HUB
, "ClearHubFeature: %d\n", req_type
);
2126 case ClearPortFeature
:
2127 err
= oz_clear_port_feature(hcd
, wvalue
, windex
);
2129 case GetHubDescriptor
:
2130 oz_get_hub_descriptor(hcd
, (struct usb_hub_descriptor
*)buf
);
2133 oz_trace2(OZ_TRACE_HUB
, "GetHubStatus: req_type = 0x%x\n",
2135 put_unaligned(__constant_cpu_to_le32(0), (__le32
*)buf
);
2138 err
= oz_get_port_status(hcd
, windex
, buf
);
2141 oz_trace2(OZ_TRACE_HUB
, "SetHubFeature: %d\n", req_type
);
2143 case SetPortFeature
:
2144 err
= oz_set_port_feature(hcd
, wvalue
, windex
);
2147 oz_trace2(OZ_TRACE_HUB
, "Other: %d\n", req_type
);
2152 /*------------------------------------------------------------------------------
2155 static int oz_hcd_bus_suspend(struct usb_hcd
*hcd
)
2157 struct oz_hcd
*ozhcd
;
2158 oz_trace2(OZ_TRACE_HUB
, "oz_hcd_hub_suspend()\n");
2159 ozhcd
= oz_hcd_private(hcd
);
2160 spin_lock_bh(&ozhcd
->hcd_lock
);
2161 hcd
->state
= HC_STATE_SUSPENDED
;
2162 ozhcd
->flags
|= OZ_HDC_F_SUSPENDED
;
2163 spin_unlock_bh(&ozhcd
->hcd_lock
);
2166 /*------------------------------------------------------------------------------
2169 static int oz_hcd_bus_resume(struct usb_hcd
*hcd
)
2171 struct oz_hcd
*ozhcd
;
2172 oz_trace2(OZ_TRACE_HUB
, "oz_hcd_hub_resume()\n");
2173 ozhcd
= oz_hcd_private(hcd
);
2174 spin_lock_bh(&ozhcd
->hcd_lock
);
2175 ozhcd
->flags
&= ~OZ_HDC_F_SUSPENDED
;
2176 hcd
->state
= HC_STATE_RUNNING
;
2177 spin_unlock_bh(&ozhcd
->hcd_lock
);
2180 /*------------------------------------------------------------------------------
2182 static void oz_plat_shutdown(struct platform_device
*dev
)
2184 oz_trace("oz_plat_shutdown()\n");
2186 /*------------------------------------------------------------------------------
2189 static int oz_plat_probe(struct platform_device
*dev
)
2193 struct usb_hcd
*hcd
;
2194 struct oz_hcd
*ozhcd
;
2195 oz_trace("oz_plat_probe()\n");
2196 hcd
= usb_create_hcd(&g_oz_hc_drv
, &dev
->dev
, dev_name(&dev
->dev
));
2198 oz_trace("Failed to created hcd object OK\n");
2201 ozhcd
= oz_hcd_private(hcd
);
2202 memset(ozhcd
, 0, sizeof(*ozhcd
));
2203 INIT_LIST_HEAD(&ozhcd
->urb_pending_list
);
2204 INIT_LIST_HEAD(&ozhcd
->urb_cancel_list
);
2205 INIT_LIST_HEAD(&ozhcd
->orphanage
);
2207 ozhcd
->conn_port
= -1;
2208 spin_lock_init(&ozhcd
->hcd_lock
);
2209 for (i
= 0; i
< OZ_NB_PORTS
; i
++) {
2210 struct oz_port
*port
= &ozhcd
->ports
[i
];
2211 port
->ozhcd
= ozhcd
;
2214 port
->bus_addr
= 0xff;
2215 spin_lock_init(&port
->port_lock
);
2217 err
= usb_add_hcd(hcd
, 0, 0);
2219 oz_trace("Failed to add hcd object OK\n");
2223 spin_lock_bh(&g_hcdlock
);
2225 spin_unlock_bh(&g_hcdlock
);
2228 /*------------------------------------------------------------------------------
2231 static int oz_plat_remove(struct platform_device
*dev
)
2233 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
2234 struct oz_hcd
*ozhcd
;
2235 oz_trace("oz_plat_remove()\n");
2238 ozhcd
= oz_hcd_private(hcd
);
2239 spin_lock_bh(&g_hcdlock
);
2240 if (ozhcd
== g_ozhcd
)
2242 spin_unlock_bh(&g_hcdlock
);
2243 oz_trace("Clearing orphanage\n");
2244 oz_hcd_clear_orphanage(ozhcd
, -EPIPE
);
2245 oz_trace("Removing hcd\n");
2246 usb_remove_hcd(hcd
);
2248 oz_empty_link_pool();
2251 /*------------------------------------------------------------------------------
2254 static int oz_plat_suspend(struct platform_device
*dev
, pm_message_t msg
)
2256 oz_trace("oz_plat_suspend()\n");
2259 /*------------------------------------------------------------------------------
2262 static int oz_plat_resume(struct platform_device
*dev
)
2264 oz_trace("oz_plat_resume()\n");
2267 /*------------------------------------------------------------------------------
2270 int oz_hcd_init(void)
2275 tasklet_init(&g_urb_process_tasklet
, oz_urb_process_tasklet
, 0);
2276 tasklet_init(&g_urb_cancel_tasklet
, oz_urb_cancel_tasklet
, 0);
2277 err
= platform_driver_register(&g_oz_plat_drv
);
2278 oz_trace("platform_driver_register() returned %d\n", err
);
2281 g_plat_dev
= platform_device_alloc(OZ_PLAT_DEV_NAME
, -1);
2282 if (g_plat_dev
== 0) {
2286 oz_trace("platform_device_alloc() succeeded\n");
2287 err
= platform_device_add(g_plat_dev
);
2290 oz_trace("platform_device_add() succeeded\n");
2293 platform_device_put(g_plat_dev
);
2295 platform_driver_unregister(&g_oz_plat_drv
);
2297 tasklet_disable(&g_urb_process_tasklet
);
2298 tasklet_disable(&g_urb_cancel_tasklet
);
2299 oz_trace("oz_hcd_init() failed %d\n", err
);
2302 /*------------------------------------------------------------------------------
2305 void oz_hcd_term(void)
2307 tasklet_disable(&g_urb_process_tasklet
);
2308 tasklet_disable(&g_urb_cancel_tasklet
);
2309 platform_device_unregister(g_plat_dev
);
2310 platform_driver_unregister(&g_oz_plat_drv
);
2311 oz_trace("Pending urbs:%d\n", atomic_read(&g_pending_urbs
));