Merge remote-tracking branches 'asoc/topic/prefix' and 'asoc/topic/rcar' into asoc...
[deliverable/linux.git] / drivers / staging / ozwpan / ozhcd.c
1 /* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 *
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.
9 * Note:
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
13 * the driver.
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 * -----------------------------------------------------------------------------
26 */
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>
33 #include "ozdbg.h"
34 #include "ozusbif.h"
35 #include "ozurbparanoia.h"
36 #include "ozhcd.h"
37
38 /*
39 * Number of units of buffering to capture for an isochronous IN endpoint before
40 * allowing data to be indicated up.
41 */
42 #define OZ_IN_BUFFERING_UNITS 100
43
44 /* Name of our platform device.
45 */
46 #define OZ_PLAT_DEV_NAME "ozwpan"
47
48 /* Maximum number of free urb links that can be kept in the pool.
49 */
50 #define OZ_MAX_LINK_POOL_SIZE 16
51
52 /* Get endpoint object from the containing link.
53 */
54 #define ep_from_link(__e) container_of((__e), struct oz_endpoint, link)
55
56 /*EP0 timeout before ep0 request is again added to TX queue. (13*8 = 98mSec)
57 */
58 #define EP0_TIMEOUT_COUNTER 13
59
60 /* Debounce time HCD driver should wait before unregistering.
61 */
62 #define OZ_HUB_DEBOUNCE_TIMEOUT 1500
63
64 /*
65 * Used to link urbs together and also store some status information for each
66 * urb.
67 * A cache of these are kept in a pool to reduce number of calls to kmalloc.
68 */
69 struct oz_urb_link {
70 struct list_head link;
71 struct urb *urb;
72 struct oz_port *port;
73 u8 req_id;
74 u8 ep_num;
75 unsigned submit_counter;
76 };
77
78 /* Holds state information about a USB endpoint.
79 */
80 #define OZ_EP_BUFFER_SIZE_ISOC (1024 * 24)
81 #define OZ_EP_BUFFER_SIZE_INT 512
82 struct oz_endpoint {
83 struct list_head urb_list; /* List of oz_urb_link items. */
84 struct list_head link; /* For isoc ep, links in to isoc
85 lists of oz_port. */
86 struct timespec timestamp;
87 int credit;
88 int credit_ceiling;
89 u8 ep_num;
90 u8 attrib;
91 u8 *buffer;
92 int buffer_size;
93 int in_ix;
94 int out_ix;
95 int buffered_units;
96 unsigned flags;
97 int start_frame;
98 };
99
100 /* Bits in the flags field. */
101 #define OZ_F_EP_BUFFERING 0x1
102 #define OZ_F_EP_HAVE_STREAM 0x2
103
104 /* Holds state information about a USB interface.
105 */
106 struct oz_interface {
107 unsigned ep_mask;
108 u8 alt;
109 };
110
111 /* Holds state information about an hcd port.
112 */
113 #define OZ_NB_ENDPOINTS 16
114 struct oz_port {
115 unsigned flags;
116 unsigned status;
117 void *hpd;
118 struct oz_hcd *ozhcd;
119 spinlock_t port_lock;
120 u8 bus_addr;
121 u8 next_req_id;
122 u8 config_num;
123 int num_iface;
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;
129 };
130
131 #define OZ_PORT_F_PRESENT 0x1
132 #define OZ_PORT_F_CHANGED 0x2
133 #define OZ_PORT_F_DYING 0x4
134
135 /* Data structure in the private context area of struct usb_hcd.
136 */
137 #define OZ_NB_PORTS 8
138 struct oz_hcd {
139 spinlock_t hcd_lock;
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];
145 uint flags;
146 struct usb_hcd *hcd;
147 };
148
149 /* Bits in flags field.
150 */
151 #define OZ_HDC_F_SUSPENDED 0x1
152
153 /*
154 * Static function prototypes.
155 */
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,
160 gfp_t mem_flags);
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,
180 gfp_t mem_flags);
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,
189 gfp_t mem_flags);
190 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
191 struct urb *urb);
192 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status);
193
194 /*
195 * Static external variables.
196 */
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),
213 .flags = HCD_USB11,
214 .start = oz_hcd_start,
215 .stop = oz_hcd_stop,
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,
226 };
227
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,
234 .driver = {
235 .name = OZ_PLAT_DEV_NAME,
236 .owner = THIS_MODULE,
237 },
238 };
239
240 /*
241 * Gets our private context area (which is of type struct oz_hcd) from the
242 * usb_hcd structure.
243 * Context: any
244 */
245 static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
246 {
247 return (struct oz_hcd *)hcd->hcd_priv;
248 }
249
250 /*
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.
254 * Context: any
255 */
256 static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
257 {
258 int i;
259
260 for (i = 0; i < OZ_NB_PORTS; i++) {
261 if (ozhcd->ports[i].bus_addr == bus_addr)
262 return i;
263 }
264 return ozhcd->conn_port;
265 }
266
267 /*
268 * Allocates an urb link, first trying the pool but going to heap if empty.
269 * Context: any
270 */
271 static struct oz_urb_link *oz_alloc_urb_link(void)
272 {
273 struct oz_urb_link *urbl = NULL;
274 unsigned long irq_state;
275
276 spin_lock_irqsave(&g_link_lock, irq_state);
277 if (g_link_pool) {
278 urbl = container_of(g_link_pool, struct oz_urb_link, link);
279 g_link_pool = urbl->link.next;
280 --g_link_pool_size;
281 }
282 spin_unlock_irqrestore(&g_link_lock, irq_state);
283 if (urbl == NULL)
284 urbl = kmalloc(sizeof(struct oz_urb_link), GFP_ATOMIC);
285 return urbl;
286 }
287
288 /*
289 * Frees an urb link by putting it in the pool if there is enough space or
290 * deallocating it to heap otherwise.
291 * Context: any
292 */
293 static void oz_free_urb_link(struct oz_urb_link *urbl)
294 {
295 if (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;
301 urbl = NULL;
302 g_link_pool_size++;
303 }
304 spin_unlock_irqrestore(&g_link_lock, irq_state);
305 kfree(urbl);
306 }
307 }
308
309 /*
310 * Deallocates all the urb links in the pool.
311 * Context: unknown
312 */
313 static void oz_empty_link_pool(void)
314 {
315 struct list_head *e;
316 unsigned long irq_state;
317
318 spin_lock_irqsave(&g_link_lock, irq_state);
319 e = g_link_pool;
320 g_link_pool = NULL;
321 g_link_pool_size = 0;
322 spin_unlock_irqrestore(&g_link_lock, irq_state);
323 while (e) {
324 struct oz_urb_link *urbl =
325 container_of(e, struct oz_urb_link, link);
326 e = e->next;
327 kfree(urbl);
328 }
329 }
330
331 /*
332 * Allocates endpoint structure and optionally a buffer. If a buffer is
333 * allocated it immediately follows the endpoint structure.
334 * Context: softirq
335 */
336 static struct oz_endpoint *oz_ep_alloc(int buffer_size, gfp_t mem_flags)
337 {
338 struct oz_endpoint *ep =
339 kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
340 if (ep) {
341 INIT_LIST_HEAD(&ep->urb_list);
342 INIT_LIST_HEAD(&ep->link);
343 ep->credit = -1;
344 if (buffer_size) {
345 ep->buffer_size = buffer_size;
346 ep->buffer = (u8 *)(ep+1);
347 }
348 }
349 return ep;
350 }
351
352 /*
353 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
354 * disabled.
355 * Context: softirq or process
356 */
357 static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd, struct urb *urb)
358 {
359 struct oz_urb_link *urbl;
360 struct list_head *e;
361
362 list_for_each(e, &ozhcd->urb_cancel_list) {
363 urbl = container_of(e, struct oz_urb_link, link);
364 if (urb == urbl->urb) {
365 list_del_init(e);
366 return urbl;
367 }
368 }
369 return NULL;
370 }
371
372 /*
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
376 */
377 static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
378 int status)
379 {
380 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
381 unsigned long irq_state;
382 struct oz_urb_link *cancel_urbl;
383
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.
388 */
389 urb->hcpriv = NULL;
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.
393 */
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.
402 */
403 spin_unlock(&g_tasklet_lock);
404 if (oz_forget_urb(urb)) {
405 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
406 } else {
407 atomic_dec(&g_pending_urbs);
408 usb_hcd_giveback_urb(hcd, urb, status);
409 }
410 spin_lock(&g_tasklet_lock);
411 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
412 if (cancel_urbl)
413 oz_free_urb_link(cancel_urbl);
414 }
415
416 /*
417 * Deallocates an endpoint including deallocating any associated stream and
418 * returning any queued urbs to the core.
419 * Context: softirq
420 */
421 static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
422 {
423 if (port) {
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);
436 }
437 oz_dbg(ON, "Freeing endpoint memory\n");
438 kfree(ep);
439 }
440
441 /*
442 * Context: softirq
443 */
444 static void oz_complete_buffered_urb(struct oz_port *port,
445 struct oz_endpoint *ep,
446 struct urb *urb)
447 {
448 int data_len, available_space, copy_len;
449
450 data_len = ep->buffer[ep->out_ix];
451 if (data_len <= urb->transfer_buffer_length)
452 available_space = data_len;
453 else
454 available_space = urb->transfer_buffer_length;
455
456 if (++ep->out_ix == ep->buffer_size)
457 ep->out_ix = 0;
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);
462
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;
467 } else {
468 ep->out_ix += copy_len;
469 }
470 urb->actual_length = available_space;
471 if (ep->out_ix == ep->buffer_size)
472 ep->out_ix = 0;
473
474 ep->buffered_units--;
475 oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
476 available_space);
477 oz_complete_urb(port->ozhcd->hcd, urb, 0);
478 }
479
480 /*
481 * Context: softirq
482 */
483 static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
484 struct urb *urb, u8 req_id)
485 {
486 struct oz_urb_link *urbl;
487 struct oz_endpoint *ep = NULL;
488 int err = 0;
489
490 if (ep_addr >= OZ_NB_ENDPOINTS) {
491 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
492 return -EINVAL;
493 }
494 urbl = oz_alloc_urb_link();
495 if (!urbl)
496 return -ENOMEM;
497 urbl->submit_counter = 0;
498 urbl->urb = urb;
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.
503 */
504 spin_lock_bh(&port->ozhcd->hcd_lock);
505 /* If the urb has been unlinked while out of any list then
506 * complete it now.
507 */
508 if (urb->unlinked) {
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);
513 return 0;
514 }
515
516 if (in_dir)
517 ep = port->in_ep[ep_addr];
518 else
519 ep = port->out_ep[ep_addr];
520 if (!ep) {
521 err = -ENOMEM;
522 goto out;
523 }
524
525 /*For interrupt endpoint check for buffered data
526 * & complete urb
527 */
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);
533 return 0;
534 }
535
536 if (port->hpd) {
537 list_add_tail(&urbl->link, &ep->urb_list);
538 if (!in_dir && ep_addr && (ep->credit < 0)) {
539 getrawmonotonic(&ep->timestamp);
540 ep->credit = 0;
541 }
542 } else {
543 err = -EPIPE;
544 }
545 out:
546 spin_unlock_bh(&port->ozhcd->hcd_lock);
547 if (err)
548 oz_free_urb_link(urbl);
549 return err;
550 }
551
552 /*
553 * Removes an urb from the queue in the endpoint.
554 * Returns 0 if it is found and -EIDRM otherwise.
555 * Context: softirq
556 */
557 static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
558 struct urb *urb)
559 {
560 struct oz_urb_link *urbl = NULL;
561 struct oz_endpoint *ep;
562
563 spin_lock_bh(&port->ozhcd->hcd_lock);
564 if (in_dir)
565 ep = port->in_ep[ep_addr];
566 else
567 ep = port->out_ep[ep_addr];
568 if (ep) {
569 struct list_head *e;
570 list_for_each(e, &ep->urb_list) {
571 urbl = container_of(e, struct oz_urb_link, link);
572 if (urbl->urb == urb) {
573 list_del_init(e);
574 break;
575 }
576 urbl = NULL;
577 }
578 }
579 spin_unlock_bh(&port->ozhcd->hcd_lock);
580 if (urbl)
581 oz_free_urb_link(urbl);
582 return urbl ? 0 : -EIDRM;
583 }
584
585 /*
586 * Finds an urb given its request id.
587 * Context: softirq
588 */
589 static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
590 u8 req_id)
591 {
592 struct oz_hcd *ozhcd = port->ozhcd;
593 struct urb *urb = NULL;
594 struct oz_urb_link *urbl;
595 struct oz_endpoint *ep;
596
597 spin_lock_bh(&ozhcd->hcd_lock);
598 ep = port->out_ep[ep_ix];
599 if (ep) {
600 struct list_head *e;
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) {
604 urb = urbl->urb;
605 list_del_init(e);
606 break;
607 }
608 }
609 }
610 spin_unlock_bh(&ozhcd->hcd_lock);
611 /* If urb is non-zero then we we must have an urb link to delete.
612 */
613 if (urb)
614 oz_free_urb_link(urbl);
615 return urb;
616 }
617
618 /*
619 * Pre-condition: Port lock must be held.
620 * Context: softirq
621 */
622 static void oz_acquire_port(struct oz_port *port, void *hpd)
623 {
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);
629 oz_usb_get(hpd);
630 port->hpd = hpd;
631 }
632
633 /*
634 * Context: softirq
635 */
636 static struct oz_hcd *oz_hcd_claim(void)
637 {
638 struct oz_hcd *ozhcd;
639
640 spin_lock_bh(&g_hcdlock);
641 ozhcd = g_ozhcd;
642 if (ozhcd)
643 usb_get_hcd(ozhcd->hcd);
644 spin_unlock_bh(&g_hcdlock);
645 return ozhcd;
646 }
647
648 /*
649 * Context: softirq
650 */
651 static inline void oz_hcd_put(struct oz_hcd *ozhcd)
652 {
653 if (ozhcd)
654 usb_put_hcd(ozhcd->hcd);
655 }
656
657 /*
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
662 * we fail.
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.
667 * Context: softirq
668 */
669 struct oz_port *oz_hcd_pd_arrived(void *hpd)
670 {
671 int i;
672 struct oz_port *hport;
673 struct oz_hcd *ozhcd;
674 struct oz_endpoint *ep;
675
676 ozhcd = oz_hcd_claim();
677 if (!ozhcd)
678 return NULL;
679 /* Allocate an endpoint object in advance (before holding hcd lock) to
680 * use for out endpoint 0.
681 */
682 ep = oz_ep_alloc(0, GFP_ATOMIC);
683 if (!ep)
684 goto err_put;
685
686 spin_lock_bh(&ozhcd->hcd_lock);
687 if (ozhcd->conn_port >= 0)
688 goto err_unlock;
689
690 for (i = 0; i < OZ_NB_PORTS; i++) {
691 struct oz_port *port = &ozhcd->ports[i];
692
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);
697 break;
698 }
699 spin_unlock(&port->port_lock);
700 }
701 if (i == OZ_NB_PORTS)
702 goto err_unlock;
703
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);
711 oz_hcd_put(ozhcd);
712
713 return hport;
714
715 err_unlock:
716 spin_unlock_bh(&ozhcd->hcd_lock);
717 oz_ep_free(NULL, ep);
718 err_put:
719 oz_hcd_put(ozhcd);
720 return NULL;
721 }
722
723 /*
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.
727 * Context: softirq
728 */
729 void oz_hcd_pd_departed(struct oz_port *port)
730 {
731 struct oz_hcd *ozhcd;
732 void *hpd;
733 struct oz_endpoint *ep = NULL;
734
735 if (port == NULL) {
736 oz_dbg(ON, "%s: port = 0\n", __func__);
737 return;
738 }
739 ozhcd = port->ozhcd;
740 if (ozhcd == NULL)
741 return;
742 /* Check if this is the connection port - if so clear it.
743 */
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;
749 }
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);
754
755 oz_clean_endpoints_for_config(ozhcd->hcd, port);
756 spin_lock_bh(&port->port_lock);
757 hpd = port->hpd;
758 port->hpd = NULL;
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.
767 */
768 if (port->out_ep[0]) {
769 ep = port->out_ep[0];
770 port->out_ep[0] = NULL;
771 }
772 spin_unlock_bh(&port->port_lock);
773 if (ep)
774 oz_ep_free(port, ep);
775 usb_hcd_poll_rh_status(ozhcd->hcd);
776 oz_usb_put(hpd);
777 }
778
779 /*
780 * Context: softirq
781 */
782 void oz_hcd_pd_reset(void *hpd, void *hport)
783 {
784 /* Cleanup the current configuration and report reset to the core.
785 */
786 struct oz_port *port = (struct oz_port *)hport;
787 struct oz_hcd *ozhcd = port->ozhcd;
788
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);
797 }
798
799 /*
800 * Context: softirq
801 */
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)
804 {
805 struct oz_port *port = (struct oz_port *)hport;
806 struct urb *urb;
807 int err = 0;
808
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);
812 if (!urb)
813 return;
814 if (status == 0) {
815 int copy_len;
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)
821 copy_len = length;
822 memcpy(urb->transfer_buffer+offset, desc, copy_len);
823 offset += 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))
829 err = -ENOMEM;
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);
835 err = -ENOMEM;
836 }
837 if (err == 0)
838 return;
839 }
840 }
841 urb->actual_length = total_size;
842 oz_complete_urb(port->ozhcd->hcd, urb, 0);
843 }
844
845 /*
846 * Context: softirq
847 */
848 static void oz_display_conf_type(u8 t)
849 {
850 switch (t) {
851 case USB_REQ_GET_STATUS:
852 oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
853 break;
854 case USB_REQ_CLEAR_FEATURE:
855 oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
856 break;
857 case USB_REQ_SET_FEATURE:
858 oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
859 break;
860 case USB_REQ_SET_ADDRESS:
861 oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
862 break;
863 case USB_REQ_GET_DESCRIPTOR:
864 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
865 break;
866 case USB_REQ_SET_DESCRIPTOR:
867 oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
868 break;
869 case USB_REQ_GET_CONFIGURATION:
870 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
871 break;
872 case USB_REQ_SET_CONFIGURATION:
873 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
874 break;
875 case USB_REQ_GET_INTERFACE:
876 oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
877 break;
878 case USB_REQ_SET_INTERFACE:
879 oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
880 break;
881 case USB_REQ_SYNCH_FRAME:
882 oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
883 break;
884 }
885 }
886
887 /*
888 * Context: softirq
889 */
890 static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
891 u8 rcode, u8 config_num)
892 {
893 int rc = 0;
894 struct usb_hcd *hcd = port->ozhcd->hcd;
895
896 if (rcode == 0) {
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)) {
901 rc = -ENOMEM;
902 }
903 } else {
904 rc = -ENOMEM;
905 }
906 oz_complete_urb(hcd, urb, rc);
907 }
908
909 /*
910 * Context: softirq
911 */
912 static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
913 u8 rcode, u8 if_num, u8 alt)
914 {
915 struct usb_hcd *hcd = port->ozhcd->hcd;
916 int rc = 0;
917
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,
926 GFP_ATOMIC))
927 rc = -ENOMEM;
928 else
929 port->iface[if_num].alt = alt;
930 } else {
931 rc = -ENOMEM;
932 }
933 oz_complete_urb(hcd, urb, rc);
934 }
935
936 /*
937 * Context: softirq
938 */
939 void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
940 int data_len)
941 {
942 struct oz_port *port = (struct oz_port *)hport;
943 struct urb *urb;
944 struct usb_ctrlrequest *setup;
945 struct usb_hcd *hcd = port->ozhcd->hcd;
946 unsigned windex;
947 unsigned wvalue;
948
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);
951 if (!urb) {
952 oz_dbg(ON, "URB not found\n");
953 return;
954 }
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,
964 (u8)wvalue);
965 break;
966 case USB_REQ_SET_INTERFACE:
967 oz_hcd_complete_set_interface(port, urb, rcode,
968 (u8)windex, (u8)wvalue);
969 break;
970 default:
971 oz_complete_urb(hcd, urb, 0);
972 }
973
974 } else {
975 int copy_len;
976 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
977 if (data_len) {
978 if (data_len <= urb->transfer_buffer_length)
979 copy_len = data_len;
980 else
981 copy_len = urb->transfer_buffer_length;
982 memcpy(urb->transfer_buffer, data, copy_len);
983 urb->actual_length = copy_len;
984 }
985 oz_complete_urb(hcd, urb, 0);
986 }
987 }
988
989 /*
990 * Context: softirq-serialized
991 */
992 static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
993 int data_len)
994 {
995 int space;
996 int copy_len;
997
998 if (!ep->buffer)
999 return -1;
1000 space = ep->out_ix-ep->in_ix-1;
1001 if (space < 0)
1002 space += ep->buffer_size;
1003 if (space < (data_len+1)) {
1004 oz_dbg(ON, "Buffer full\n");
1005 return -1;
1006 }
1007 ep->buffer[ep->in_ix] = (u8)data_len;
1008 if (++ep->in_ix == ep->buffer_size)
1009 ep->in_ix = 0;
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);
1014
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;
1018 } else {
1019 ep->in_ix += copy_len;
1020 }
1021 if (ep->in_ix == ep->buffer_size)
1022 ep->in_ix = 0;
1023 ep->buffered_units++;
1024 return 0;
1025 }
1026
1027 /*
1028 * Context: softirq-serialized
1029 */
1030 void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
1031 {
1032 struct oz_port *port = (struct oz_port *)hport;
1033 struct oz_endpoint *ep;
1034 struct oz_hcd *ozhcd = port->ozhcd;
1035
1036 spin_lock_bh(&ozhcd->hcd_lock);
1037 ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
1038 if (ep == NULL)
1039 goto done;
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);
1047 struct urb *urb;
1048 int copy_len;
1049 list_del_init(&urbl->link);
1050 spin_unlock_bh(&ozhcd->hcd_lock);
1051 urb = urbl->urb;
1052 oz_free_urb_link(urbl);
1053 if (data_len <= urb->transfer_buffer_length)
1054 copy_len = data_len;
1055 else
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);
1060 return;
1061 } else {
1062 oz_dbg(ON, "buffering frame as URB is not available\n");
1063 oz_hcd_buffer_data(ep, data, data_len);
1064 }
1065 break;
1066 case USB_ENDPOINT_XFER_ISOC:
1067 oz_hcd_buffer_data(ep, data, data_len);
1068 break;
1069 }
1070 done:
1071 spin_unlock_bh(&ozhcd->hcd_lock);
1072 }
1073
1074 /*
1075 * Context: unknown
1076 */
1077 static inline int oz_usb_get_frame_number(void)
1078 {
1079 return atomic_inc_return(&g_usb_frame_number);
1080 }
1081
1082 /*
1083 * Context: softirq
1084 */
1085 int oz_hcd_heartbeat(void *hport)
1086 {
1087 int rc = 0;
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;
1094 struct urb *urb;
1095 struct oz_endpoint *ep;
1096 struct timespec ts, delta;
1097
1098 getrawmonotonic(&ts);
1099 INIT_LIST_HEAD(&xfr_list);
1100 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1101 */
1102 spin_lock_bh(&ozhcd->hcd_lock);
1103 list_for_each(e, &port->isoc_out_ep) {
1104 ep = ep_from_link(e);
1105 if (ep->credit < 0)
1106 continue;
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;
1111 ep->timestamp = ts;
1112 while (ep->credit && !list_empty(&ep->urb_list)) {
1113 urbl = list_first_entry(&ep->urb_list,
1114 struct oz_urb_link, link);
1115 urb = urbl->urb;
1116 if ((ep->credit + 1) < urb->number_of_packets)
1117 break;
1118 ep->credit -= urb->number_of_packets;
1119 if (ep->credit < 0)
1120 ep->credit = 0;
1121 list_move_tail(&urbl->link, &xfr_list);
1122 }
1123 }
1124 spin_unlock_bh(&ozhcd->hcd_lock);
1125 /* Send to PD and complete URBs.
1126 */
1127 list_for_each_safe(e, n, &xfr_list) {
1128 urbl = container_of(e, struct oz_urb_link, link);
1129 urb = urbl->urb;
1130 list_del_init(e);
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);
1136 }
1137 /* Check the IN isoc endpoints to see if any URBs can be completed.
1138 */
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;
1145 ep->credit = 0;
1146 ep->timestamp = ts;
1147 ep->start_frame = 0;
1148 }
1149 continue;
1150 }
1151 delta = timespec_sub(ts, ep->timestamp);
1152 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
1153 ep->timestamp = ts;
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;
1159 int len = 0;
1160 int copy_len;
1161 int i;
1162 if (ep->credit < urb->number_of_packets)
1163 break;
1164 if (ep->buffered_units < urb->number_of_packets)
1165 break;
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)
1170 ep->out_ix = 0;
1171 copy_len = ep->buffer_size - ep->out_ix;
1172 if (copy_len > len)
1173 copy_len = len;
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;
1180 } else
1181 ep->out_ix += copy_len;
1182 if (ep->out_ix == ep->buffer_size)
1183 ep->out_ix = 0;
1184 urb->iso_frame_desc[i].offset =
1185 urb->actual_length;
1186 urb->actual_length += len;
1187 urb->iso_frame_desc[i].actual_length = len;
1188 urb->iso_frame_desc[i].status = 0;
1189 }
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;
1196 }
1197 }
1198 if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1199 rc = 1;
1200 spin_unlock_bh(&ozhcd->hcd_lock);
1201 /* Complete the filled URBs.
1202 */
1203 list_for_each_safe(e, n, &xfr_list) {
1204 urbl = container_of(e, struct oz_urb_link, link);
1205 urb = urbl->urb;
1206 list_del_init(e);
1207 oz_free_urb_link(urbl);
1208 oz_complete_urb(port->ozhcd->hcd, urb, 0);
1209 }
1210 /* Check if there are any ep0 requests that have timed out.
1211 * If so resent to PD.
1212 */
1213 ep = port->out_ep[0];
1214 if (ep) {
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;
1224 } else {
1225 urbl->submit_counter++;
1226 }
1227 }
1228 if (!list_empty(&ep->urb_list))
1229 rc = 1;
1230 spin_unlock_bh(&ozhcd->hcd_lock);
1231 e = xfr_list.next;
1232 while (e != &xfr_list) {
1233 urbl = container_of(e, struct oz_urb_link, link);
1234 e = e->next;
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);
1238 }
1239 }
1240 return rc;
1241 }
1242
1243 /*
1244 * Context: softirq
1245 */
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)
1249 {
1250 struct oz_hcd *ozhcd = port->ozhcd;
1251 int i;
1252 int if_ix = intf->desc.bInterfaceNumber;
1253 int request_heartbeat = 0;
1254
1255 oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
1256 if (if_ix >= port->num_iface || port->iface == NULL)
1257 return -ENOMEM;
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;
1264
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;
1271 break;
1272 case USB_ENDPOINT_XFER_INT:
1273 buffer_size = OZ_EP_BUFFER_SIZE_INT;
1274 break;
1275 }
1276 }
1277
1278 ep = oz_ep_alloc(buffer_size, mem_flags);
1279 if (!ep) {
1280 oz_clean_endpoints_for_interface(hcd, port, if_ix);
1281 return -ENOMEM;
1282 }
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;
1292 } else {
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;
1296 }
1297 }
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;
1307 }
1308 } else {
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;
1315 }
1316 }
1317 spin_unlock_bh(&ozhcd->hcd_lock);
1318 if (request_heartbeat && port->hpd)
1319 oz_usb_request_heartbeat(port->hpd);
1320 }
1321 return 0;
1322 }
1323
1324 /*
1325 * Context: softirq
1326 */
1327 static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1328 struct oz_port *port, int if_ix)
1329 {
1330 struct oz_hcd *ozhcd = port->ozhcd;
1331 unsigned mask;
1332 int i;
1333 struct list_head ep_list;
1334
1335 oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
1336 if (if_ix >= port->num_iface)
1337 return;
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.
1345 */
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.
1350 */
1351 list_move_tail(e, &ep_list);
1352 }
1353 /* Gather IN endpoints.
1354 */
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);
1359 }
1360 }
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);
1367 }
1368 }
1369
1370 /*
1371 * Context: softirq
1372 */
1373 static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1374 struct oz_port *port, struct usb_host_config *config,
1375 gfp_t mem_flags)
1376 {
1377 struct oz_hcd *ozhcd = port->ozhcd;
1378 int i;
1379 int num_iface = config->desc.bNumInterfaces;
1380
1381 if (num_iface) {
1382 struct oz_interface *iface;
1383
1384 iface = kmalloc(num_iface*sizeof(struct oz_interface),
1385 mem_flags | __GFP_ZERO);
1386 if (!iface)
1387 return -ENOMEM;
1388 spin_lock_bh(&ozhcd->hcd_lock);
1389 port->iface = iface;
1390 port->num_iface = num_iface;
1391 spin_unlock_bh(&ozhcd->hcd_lock);
1392 }
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,
1397 mem_flags))
1398 goto fail;
1399 }
1400 return 0;
1401 fail:
1402 oz_clean_endpoints_for_config(hcd, port);
1403 return -1;
1404 }
1405
1406 /*
1407 * Context: softirq
1408 */
1409 static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1410 struct oz_port *port)
1411 {
1412 struct oz_hcd *ozhcd = port->ozhcd;
1413 int i;
1414
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);
1419 if (port->iface) {
1420 oz_dbg(ON, "Freeing interfaces object\n");
1421 kfree(port->iface);
1422 port->iface = NULL;
1423 }
1424 port->num_iface = 0;
1425 spin_unlock_bh(&ozhcd->hcd_lock);
1426 }
1427
1428 /*
1429 * Context: tasklet
1430 */
1431 static void *oz_claim_hpd(struct oz_port *port)
1432 {
1433 void *hpd;
1434 struct oz_hcd *ozhcd = port->ozhcd;
1435
1436 spin_lock_bh(&ozhcd->hcd_lock);
1437 hpd = port->hpd;
1438 if (hpd)
1439 oz_usb_get(hpd);
1440 spin_unlock_bh(&ozhcd->hcd_lock);
1441 return hpd;
1442 }
1443
1444 /*
1445 * Context: tasklet
1446 */
1447 static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1448 gfp_t mem_flags)
1449 {
1450 struct usb_ctrlrequest *setup;
1451 unsigned windex;
1452 unsigned wvalue;
1453 unsigned wlength;
1454 void *hpd;
1455 u8 req_id;
1456 int rc = 0;
1457 unsigned complete = 0;
1458
1459 int port_ix = -1;
1460 struct oz_port *port = NULL;
1461
1462 oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
1463 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1464 if (port_ix < 0) {
1465 rc = -EPIPE;
1466 goto out;
1467 }
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);
1473 rc = -EPIPE;
1474 goto out;
1475 }
1476 /* Store port in private context data.
1477 */
1478 urb->hcpriv = port;
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);
1488
1489 req_id = port->next_req_id++;
1490 hpd = oz_claim_hpd(port);
1491 if (hpd == NULL) {
1492 oz_dbg(ON, "Cannot claim port\n");
1493 rc = -EPIPE;
1494 goto out;
1495 }
1496
1497 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1498 /* Standard requests
1499 */
1500 switch (setup->bRequest) {
1501 case USB_REQ_GET_DESCRIPTOR:
1502 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
1503 break;
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",
1507 ozhcd->conn_port,
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;
1515 }
1516 spin_unlock_bh(&ozhcd->hcd_lock);
1517 complete = 1;
1518 break;
1519 case USB_REQ_SET_CONFIGURATION:
1520 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
1521 break;
1522 case USB_REQ_GET_CONFIGURATION:
1523 /* We short circuit this case and reply directly since
1524 * we have the selected configuration number cached.
1525 */
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) =
1530 port->config_num;
1531 complete = 1;
1532 } else {
1533 rc = -EPIPE;
1534 }
1535 break;
1536 case USB_REQ_GET_INTERFACE:
1537 /* We short circuit this case and reply directly since
1538 * we have the selected interface alternative cached.
1539 */
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);
1547 complete = 1;
1548 } else {
1549 rc = -EPIPE;
1550 }
1551 break;
1552 case USB_REQ_SET_INTERFACE:
1553 oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
1554 break;
1555 }
1556 }
1557 if (!rc && !complete) {
1558 int data_len = 0;
1559 if ((setup->bRequestType & USB_DIR_IN) == 0)
1560 data_len = wlength;
1561 urb->actual_length = data_len;
1562 if (oz_usb_control_req(port->hpd, req_id, setup,
1563 urb->transfer_buffer, data_len)) {
1564 rc = -ENOMEM;
1565 } else {
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.
1576 */
1577 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1578 rc = -ENOMEM;
1579 }
1580 }
1581 oz_usb_put(hpd);
1582 out:
1583 if (rc || complete) {
1584 oz_dbg(ON, "Completing request locally\n");
1585 oz_complete_urb(ozhcd->hcd, urb, rc);
1586 } else {
1587 oz_usb_request_heartbeat(port->hpd);
1588 }
1589 }
1590
1591 /*
1592 * Context: tasklet
1593 */
1594 static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1595 {
1596 int rc = 0;
1597 struct oz_port *port = urb->hcpriv;
1598 u8 ep_addr;
1599
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.
1603 */
1604 oz_remember_urb(urb);
1605 /* Check buffer is valid.
1606 */
1607 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1608 return -EINVAL;
1609 /* Check if there is a device at the port - refuse if not.
1610 */
1611 if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1612 return -EPIPE;
1613 ep_addr = usb_pipeendpoint(urb->pipe);
1614 if (ep_addr) {
1615 /* If the request is not for EP0 then queue it.
1616 */
1617 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1618 urb, 0))
1619 rc = -EPIPE;
1620 } else {
1621 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1622 }
1623 return rc;
1624 }
1625
1626 /*
1627 * Context: tasklet
1628 */
1629 static void oz_urb_process_tasklet(unsigned long unused)
1630 {
1631 unsigned long irq_state;
1632 struct urb *urb;
1633 struct oz_hcd *ozhcd = oz_hcd_claim();
1634 int rc = 0;
1635
1636 if (ozhcd == NULL)
1637 return;
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.
1641 */
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);
1649 urb = urbl->urb;
1650 oz_free_urb_link(urbl);
1651 rc = oz_urb_process(ozhcd, urb);
1652 if (rc)
1653 oz_complete_urb(ozhcd->hcd, urb, rc);
1654 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1655 }
1656 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1657 oz_hcd_put(ozhcd);
1658 }
1659
1660 /*
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.
1667 * Context: tasklet
1668 */
1669 static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1670 {
1671 struct oz_urb_link *urbl = NULL;
1672 struct list_head *e;
1673 struct oz_hcd *ozhcd;
1674 unsigned long irq_state;
1675 u8 ix;
1676
1677 if (port == NULL) {
1678 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
1679 return;
1680 }
1681 ozhcd = port->ozhcd;
1682 if (ozhcd == NULL) {
1683 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
1684 return;
1685 }
1686
1687 /* Look in the tasklet queue.
1688 */
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) {
1693 list_del_init(e);
1694 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1695 goto out2;
1696 }
1697 }
1698 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1699 urbl = NULL;
1700
1701 /* Look in the orphanage.
1702 */
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) {
1707 list_del(e);
1708 oz_dbg(ON, "Found urb in orphanage\n");
1709 goto out;
1710 }
1711 }
1712 ix = (ep_num & 0xf);
1713 urbl = NULL;
1714 if ((ep_num & USB_DIR_IN) && ix)
1715 urbl = oz_remove_urb(port->in_ep[ix], urb);
1716 else
1717 urbl = oz_remove_urb(port->out_ep[ix], urb);
1718 out:
1719 spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1720 out2:
1721 if (urbl) {
1722 urb->actual_length = 0;
1723 oz_free_urb_link(urbl);
1724 oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
1725 }
1726 }
1727
1728 /*
1729 * Context: tasklet
1730 */
1731 static void oz_urb_cancel_tasklet(unsigned long unused)
1732 {
1733 unsigned long irq_state;
1734 struct urb *urb;
1735 struct oz_hcd *ozhcd = oz_hcd_claim();
1736
1737 if (ozhcd == NULL)
1738 return;
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);
1746 urb = urbl->urb;
1747 if (urb->unlinked)
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);
1751 }
1752 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1753 oz_hcd_put(ozhcd);
1754 }
1755
1756 /*
1757 * Context: unknown
1758 */
1759 static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1760 {
1761 if (ozhcd) {
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);
1769 }
1770 }
1771 }
1772
1773 /*
1774 * Context: unknown
1775 */
1776 static int oz_hcd_start(struct usb_hcd *hcd)
1777 {
1778 hcd->power_budget = 200;
1779 hcd->state = HC_STATE_RUNNING;
1780 hcd->uses_new_polling = 1;
1781 return 0;
1782 }
1783
1784 /*
1785 * Context: unknown
1786 */
1787 static void oz_hcd_stop(struct usb_hcd *hcd)
1788 {
1789 }
1790
1791 /*
1792 * Context: unknown
1793 */
1794 static void oz_hcd_shutdown(struct usb_hcd *hcd)
1795 {
1796 }
1797
1798 /*
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().
1802 * Context: any
1803 */
1804 static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1805 gfp_t mem_flags)
1806 {
1807 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1808 int rc;
1809 int port_ix;
1810 struct oz_port *port;
1811 unsigned long irq_state;
1812 struct oz_urb_link *urbl;
1813
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);
1817 return -EPIPE;
1818 }
1819 if (unlikely(hcd->state != HC_STATE_RUNNING)) {
1820 oz_dbg(URB, "Refused urb(%p) not running\n", urb);
1821 return -EPIPE;
1822 }
1823 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1824 if (port_ix < 0)
1825 return -EPIPE;
1826 port = &ozhcd->ports[port_ix];
1827 if (port == NULL)
1828 return -EPIPE;
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);
1833 return -EPIPE;
1834 }
1835 urb->hcpriv = port;
1836 /* Put request in queue for processing by tasklet.
1837 */
1838 urbl = oz_alloc_urb_link();
1839 if (unlikely(urbl == NULL))
1840 return -ENOMEM;
1841 urbl->urb = urb;
1842 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1843 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1844 if (unlikely(rc)) {
1845 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1846 oz_free_urb_link(urbl);
1847 return rc;
1848 }
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);
1853 return 0;
1854 }
1855
1856 /*
1857 * Context: tasklet
1858 */
1859 static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1860 struct urb *urb)
1861 {
1862 struct oz_urb_link *urbl;
1863 struct list_head *e;
1864
1865 if (unlikely(ep == NULL))
1866 return NULL;
1867 list_for_each(e, &ep->urb_list) {
1868 urbl = container_of(e, struct oz_urb_link, link);
1869 if (urbl->urb == urb) {
1870 list_del_init(e);
1871 if (usb_pipeisoc(urb->pipe)) {
1872 ep->credit -= urb->number_of_packets;
1873 if (ep->credit < 0)
1874 ep->credit = 0;
1875 }
1876 return urbl;
1877 }
1878 }
1879 return NULL;
1880 }
1881
1882 /*
1883 * Called to dequeue a previously submitted urb for the device.
1884 * Context: any
1885 */
1886 static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1887 {
1888 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1889 struct oz_urb_link *urbl;
1890 int rc;
1891 unsigned long irq_state;
1892
1893 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
1894 urbl = oz_alloc_urb_link();
1895 if (unlikely(urbl == NULL))
1896 return -ENOMEM;
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.
1902 */
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.
1907 */
1908 if ((rc == 0) && urb->hcpriv) {
1909 urbl->urb = urb;
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);
1917 } else {
1918 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1919 oz_free_urb_link(urbl);
1920 }
1921 return rc;
1922 }
1923
1924 /*
1925 * Context: unknown
1926 */
1927 static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1928 struct usb_host_endpoint *ep)
1929 {
1930 }
1931
1932 /*
1933 * Context: unknown
1934 */
1935 static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1936 struct usb_host_endpoint *ep)
1937 {
1938 }
1939
1940 /*
1941 * Context: unknown
1942 */
1943 static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1944 {
1945 oz_dbg(ON, "oz_hcd_get_frame_number\n");
1946 return oz_usb_get_frame_number();
1947 }
1948
1949 /*
1950 * Context: softirq
1951 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1952 * always do that in softirq context.
1953 */
1954 static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1955 {
1956 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1957 int i;
1958
1959 buf[0] = 0;
1960 buf[1] = 0;
1961
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;
1967 if (i < 7)
1968 buf[0] |= 1 << (i + 1);
1969 else
1970 buf[1] |= 1 << (i - 7);
1971 }
1972 }
1973 spin_unlock_bh(&ozhcd->hcd_lock);
1974 if (buf[0] != 0 || buf[1] != 0)
1975 return 2;
1976 else
1977 return 0;
1978 }
1979
1980 /*
1981 * Context: process
1982 */
1983 static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1984 struct usb_hub_descriptor *desc)
1985 {
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;
1992 }
1993
1994 /*
1995 * Context: process
1996 */
1997 static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1998 {
1999 struct oz_port *port;
2000 int err = 0;
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;
2005
2006 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2007 return -EPIPE;
2008 port = &ozhcd->ports[port_id-1];
2009 switch (wvalue) {
2010 case USB_PORT_FEAT_CONNECTION:
2011 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
2012 break;
2013 case USB_PORT_FEAT_ENABLE:
2014 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
2015 break;
2016 case USB_PORT_FEAT_SUSPEND:
2017 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
2018 break;
2019 case USB_PORT_FEAT_OVER_CURRENT:
2020 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2021 break;
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;
2027 break;
2028 case USB_PORT_FEAT_POWER:
2029 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2030 set_bits |= USB_PORT_STAT_POWER;
2031 break;
2032 case USB_PORT_FEAT_LOWSPEED:
2033 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2034 break;
2035 case USB_PORT_FEAT_C_CONNECTION:
2036 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2037 break;
2038 case USB_PORT_FEAT_C_ENABLE:
2039 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
2040 break;
2041 case USB_PORT_FEAT_C_SUSPEND:
2042 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2043 break;
2044 case USB_PORT_FEAT_C_OVER_CURRENT:
2045 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2046 break;
2047 case USB_PORT_FEAT_C_RESET:
2048 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
2049 break;
2050 case USB_PORT_FEAT_TEST:
2051 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2052 break;
2053 case USB_PORT_FEAT_INDICATOR:
2054 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2055 break;
2056 default:
2057 oz_dbg(HUB, "Other %d\n", wvalue);
2058 break;
2059 }
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);
2065 }
2066 oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
2067 return err;
2068 }
2069
2070 /*
2071 * Context: process
2072 */
2073 static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
2074 {
2075 struct oz_port *port;
2076 int err = 0;
2077 u8 port_id = (u8)windex;
2078 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2079 unsigned clear_bits = 0;
2080
2081 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2082 return -EPIPE;
2083 port = &ozhcd->ports[port_id-1];
2084 switch (wvalue) {
2085 case USB_PORT_FEAT_CONNECTION:
2086 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
2087 break;
2088 case USB_PORT_FEAT_ENABLE:
2089 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
2090 clear_bits = USB_PORT_STAT_ENABLE;
2091 break;
2092 case USB_PORT_FEAT_SUSPEND:
2093 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
2094 break;
2095 case USB_PORT_FEAT_OVER_CURRENT:
2096 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2097 break;
2098 case USB_PORT_FEAT_RESET:
2099 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
2100 break;
2101 case USB_PORT_FEAT_POWER:
2102 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
2103 clear_bits |= USB_PORT_STAT_POWER;
2104 break;
2105 case USB_PORT_FEAT_LOWSPEED:
2106 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
2107 break;
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);
2111 break;
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);
2115 break;
2116 case USB_PORT_FEAT_C_SUSPEND:
2117 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2118 break;
2119 case USB_PORT_FEAT_C_OVER_CURRENT:
2120 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2121 break;
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);
2125 break;
2126 case USB_PORT_FEAT_TEST:
2127 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
2128 break;
2129 case USB_PORT_FEAT_INDICATOR:
2130 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
2131 break;
2132 default:
2133 oz_dbg(HUB, "Other %d\n", wvalue);
2134 break;
2135 }
2136 if (clear_bits) {
2137 spin_lock_bh(&port->port_lock);
2138 port->status &= ~clear_bits;
2139 spin_unlock_bh(&port->port_lock);
2140 }
2141 oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2142 port_id, ozhcd->ports[port_id-1].status);
2143 return err;
2144 }
2145
2146 /*
2147 * Context: process
2148 */
2149 static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2150 {
2151 struct oz_hcd *ozhcd;
2152 u32 status;
2153
2154 if ((windex < 1) || (windex > OZ_NB_PORTS))
2155 return -EPIPE;
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);
2161 return 0;
2162 }
2163
2164 /*
2165 * Context: process
2166 */
2167 static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2168 u16 windex, char *buf, u16 wlength)
2169 {
2170 int err = 0;
2171
2172 switch (req_type) {
2173 case ClearHubFeature:
2174 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
2175 break;
2176 case ClearPortFeature:
2177 err = oz_clear_port_feature(hcd, wvalue, windex);
2178 break;
2179 case GetHubDescriptor:
2180 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2181 break;
2182 case GetHubStatus:
2183 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
2184 put_unaligned(__constant_cpu_to_le32(0), (__le32 *)buf);
2185 break;
2186 case GetPortStatus:
2187 err = oz_get_port_status(hcd, windex, buf);
2188 break;
2189 case SetHubFeature:
2190 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
2191 break;
2192 case SetPortFeature:
2193 err = oz_set_port_feature(hcd, wvalue, windex);
2194 break;
2195 default:
2196 oz_dbg(HUB, "Other: %d\n", req_type);
2197 break;
2198 }
2199 return err;
2200 }
2201
2202 /*
2203 * Context: process
2204 */
2205 static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2206 {
2207 struct oz_hcd *ozhcd;
2208
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);
2214 return 0;
2215 }
2216
2217 /*
2218 * Context: process
2219 */
2220 static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2221 {
2222 struct oz_hcd *ozhcd;
2223
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);
2229 return 0;
2230 }
2231
2232 static void oz_plat_shutdown(struct platform_device *dev)
2233 {
2234 }
2235
2236 /*
2237 * Context: process
2238 */
2239 static int oz_plat_probe(struct platform_device *dev)
2240 {
2241 int i;
2242 int err;
2243 struct usb_hcd *hcd;
2244 struct oz_hcd *ozhcd;
2245
2246 hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
2247 if (hcd == NULL) {
2248 oz_dbg(ON, "Failed to created hcd object OK\n");
2249 return -ENOMEM;
2250 }
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);
2256 ozhcd->hcd = hcd;
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;
2262 port->flags = 0;
2263 port->status = 0;
2264 port->bus_addr = 0xff;
2265 spin_lock_init(&port->port_lock);
2266 }
2267 err = usb_add_hcd(hcd, 0, 0);
2268 if (err) {
2269 oz_dbg(ON, "Failed to add hcd object OK\n");
2270 usb_put_hcd(hcd);
2271 return -1;
2272 }
2273 device_wakeup_enable(hcd->self.controller);
2274
2275 spin_lock_bh(&g_hcdlock);
2276 g_ozhcd = ozhcd;
2277 spin_unlock_bh(&g_hcdlock);
2278 return 0;
2279 }
2280
2281 /*
2282 * Context: unknown
2283 */
2284 static int oz_plat_remove(struct platform_device *dev)
2285 {
2286 struct usb_hcd *hcd = platform_get_drvdata(dev);
2287 struct oz_hcd *ozhcd;
2288
2289 if (hcd == NULL)
2290 return -1;
2291 ozhcd = oz_hcd_private(hcd);
2292 spin_lock_bh(&g_hcdlock);
2293 if (ozhcd == g_ozhcd)
2294 g_ozhcd = NULL;
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);
2300 usb_put_hcd(hcd);
2301 oz_empty_link_pool();
2302 return 0;
2303 }
2304
2305 /*
2306 * Context: unknown
2307 */
2308 static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2309 {
2310 return 0;
2311 }
2312
2313
2314 /*
2315 * Context: unknown
2316 */
2317 static int oz_plat_resume(struct platform_device *dev)
2318 {
2319 return 0;
2320 }
2321
2322 /*
2323 * Context: process
2324 */
2325 int oz_hcd_init(void)
2326 {
2327 int err;
2328
2329 if (usb_disabled())
2330 return -ENODEV;
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);
2335 if (err)
2336 goto error;
2337 g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
2338 if (g_plat_dev == NULL) {
2339 err = -ENOMEM;
2340 goto error1;
2341 }
2342 oz_dbg(ON, "platform_device_alloc() succeeded\n");
2343 err = platform_device_add(g_plat_dev);
2344 if (err)
2345 goto error2;
2346 oz_dbg(ON, "platform_device_add() succeeded\n");
2347 return 0;
2348 error2:
2349 platform_device_put(g_plat_dev);
2350 error1:
2351 platform_driver_unregister(&g_oz_plat_drv);
2352 error:
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);
2356 return err;
2357 }
2358
2359 /*
2360 * Context: process
2361 */
2362 void oz_hcd_term(void)
2363 {
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));
2370 }
This page took 0.082813 seconds and 5 git commands to generate.