RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / usb / host / u132-hcd.c
blob318e082ab5703c860039dd9bac071213f7615633
1 /*
2 * Host Controller Driver for the Elan Digital Systems U132 adapter
4 * Copyright(C) 2006 Elan Digital Systems Limited
5 * http://www.elandigitalsystems.com
7 * Author and Maintainer - Tony Olech - Elan Digital Systems
8 * tony.olech@elandigitalsystems.com
10 * This program is free software;you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2.
15 * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
16 * based on various USB host drivers in the 2.6.15 linux kernel
17 * with constant reference to the 3rd Edition of Linux Device Drivers
18 * published by O'Reilly
20 * The U132 adapter is a USB to CardBus adapter specifically designed
21 * for PC cards that contain an OHCI host controller. Typical PC cards
22 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
24 * The U132 adapter will *NOT *work with PC cards that do not contain
25 * an OHCI controller. A simple way to test whether a PC card has an
26 * OHCI controller as an interface is to insert the PC card directly
27 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
28 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
29 * then there is a good chance that the U132 adapter will support the
30 * PC card.(you also need the specific client driver for the PC card)
32 * Please inform the Author and Maintainer about any PC cards that
33 * contain OHCI Host Controller and work when directly connected to
34 * an embedded CardBus slot but do not work when they are connected
35 * via an ELAN U132 adapter.
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
41 #include <linux/delay.h>
42 #include <linux/ioport.h>
43 #include <linux/pci_ids.h>
44 #include <linux/sched.h>
45 #include <linux/slab.h>
46 #include <linux/errno.h>
47 #include <linux/init.h>
48 #include <linux/timer.h>
49 #include <linux/list.h>
50 #include <linux/interrupt.h>
51 #include <linux/usb.h>
52 #include <linux/workqueue.h>
53 #include <linux/platform_device.h>
54 #include <linux/pci_ids.h>
55 #include <linux/mutex.h>
56 #include <asm/io.h>
57 #include <asm/irq.h>
58 #include <asm/system.h>
59 #include <asm/byteorder.h>
60 #include "../core/hcd.h"
62 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
63 * If you're going to try stuff like this, you need to split
64 * out shareable stuff (register declarations?) into its own
65 * file, maybe name <linux/usb/ohci.h>
68 #include "ohci.h"
69 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
70 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
71 OHCI_INTR_WDH)
72 MODULE_AUTHOR("Tony Olech - Elan Digital Systems Limited");
73 MODULE_DESCRIPTION("U132 USB Host Controller Driver");
74 MODULE_LICENSE("GPL");
75 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
76 INT_MODULE_PARM(testing, 0);
77 /* Some boards misreport power switching/overcurrent*/
78 static int distrust_firmware = 1;
79 module_param(distrust_firmware, bool, 0);
80 MODULE_PARM_DESC(distrust_firmware, "true to distrust firmware power/overcurren"
81 "t setup");
82 static DECLARE_WAIT_QUEUE_HEAD(u132_hcd_wait);
84 * u132_module_lock exists to protect access to global variables
87 static struct mutex u132_module_lock;
88 static int u132_exiting = 0;
89 static int u132_instances = 0;
90 static struct list_head u132_static_list;
92 * end of the global variables protected by u132_module_lock
94 static struct workqueue_struct *workqueue;
95 #define MAX_U132_PORTS 7
96 #define MAX_U132_ADDRS 128
97 #define MAX_U132_UDEVS 4
98 #define MAX_U132_ENDPS 100
99 #define MAX_U132_RINGS 4
100 static const char *cc_to_text[16] = {
101 "No Error ",
102 "CRC Error ",
103 "Bit Stuff ",
104 "Data Togg ",
105 "Stall ",
106 "DevNotResp ",
107 "PIDCheck ",
108 "UnExpPID ",
109 "DataOver ",
110 "DataUnder ",
111 "(for hw) ",
112 "(for hw) ",
113 "BufferOver ",
114 "BuffUnder ",
115 "(for HCD) ",
116 "(for HCD) "
118 struct u132_port {
119 struct u132 *u132;
120 int reset;
121 int enable;
122 int power;
123 int Status;
125 struct u132_addr {
126 u8 address;
128 struct u132_udev {
129 struct kref kref;
130 struct usb_device *usb_device;
131 u8 enumeration;
132 u8 udev_number;
133 u8 usb_addr;
134 u8 portnumber;
135 u8 endp_number_in[16];
136 u8 endp_number_out[16];
138 #define ENDP_QUEUE_SHIFT 3
139 #define ENDP_QUEUE_SIZE (1<<ENDP_QUEUE_SHIFT)
140 #define ENDP_QUEUE_MASK (ENDP_QUEUE_SIZE-1)
141 struct u132_urbq {
142 struct list_head urb_more;
143 struct urb *urb;
145 struct u132_spin {
146 spinlock_t slock;
148 struct u132_endp {
149 struct kref kref;
150 u8 udev_number;
151 u8 endp_number;
152 u8 usb_addr;
153 u8 usb_endp;
154 struct u132 *u132;
155 struct list_head endp_ring;
156 struct u132_ring *ring;
157 unsigned toggle_bits:2;
158 unsigned active:1;
159 unsigned delayed:1;
160 unsigned input:1;
161 unsigned output:1;
162 unsigned pipetype:2;
163 unsigned dequeueing:1;
164 unsigned edset_flush:1;
165 unsigned spare_bits:14;
166 unsigned long jiffies;
167 struct usb_host_endpoint *hep;
168 struct u132_spin queue_lock;
169 u16 queue_size;
170 u16 queue_last;
171 u16 queue_next;
172 struct urb *urb_list[ENDP_QUEUE_SIZE];
173 struct list_head urb_more;
174 struct delayed_work scheduler;
176 struct u132_ring {
177 unsigned in_use:1;
178 unsigned length:7;
179 u8 number;
180 struct u132 *u132;
181 struct u132_endp *curr_endp;
182 struct delayed_work scheduler;
184 struct u132 {
185 struct kref kref;
186 struct list_head u132_list;
187 struct semaphore sw_lock;
188 struct semaphore scheduler_lock;
189 struct u132_platform_data *board;
190 struct platform_device *platform_dev;
191 struct u132_ring ring[MAX_U132_RINGS];
192 int sequence_num;
193 int going;
194 int power;
195 int reset;
196 int num_ports;
197 u32 hc_control;
198 u32 hc_fminterval;
199 u32 hc_roothub_status;
200 u32 hc_roothub_a;
201 u32 hc_roothub_portstatus[MAX_ROOT_PORTS];
202 int flags;
203 unsigned long next_statechange;
204 struct delayed_work monitor;
205 int num_endpoints;
206 struct u132_addr addr[MAX_U132_ADDRS];
207 struct u132_udev udev[MAX_U132_UDEVS];
208 struct u132_port port[MAX_U132_PORTS];
209 struct u132_endp *endp[MAX_U132_ENDPS];
213 * these cannot be inlines because we need the structure offset!!
214 * Does anyone have a better way?????
216 #define ftdi_read_pcimem(pdev, member, data) usb_ftdi_elan_read_pcimem(pdev, \
217 offsetof(struct ohci_regs, member), 0, data);
218 #define ftdi_write_pcimem(pdev, member, data) usb_ftdi_elan_write_pcimem(pdev, \
219 offsetof(struct ohci_regs, member), 0, data);
220 #define u132_read_pcimem(u132, member, data) \
221 usb_ftdi_elan_read_pcimem(u132->platform_dev, offsetof(struct \
222 ohci_regs, member), 0, data);
223 #define u132_write_pcimem(u132, member, data) \
224 usb_ftdi_elan_write_pcimem(u132->platform_dev, offsetof(struct \
225 ohci_regs, member), 0, data);
226 static inline struct u132 *udev_to_u132(struct u132_udev *udev)
228 u8 udev_number = udev->udev_number;
229 return container_of(udev, struct u132, udev[udev_number]);
232 static inline struct u132 *hcd_to_u132(struct usb_hcd *hcd)
234 return (struct u132 *)(hcd->hcd_priv);
237 static inline struct usb_hcd *u132_to_hcd(struct u132 *u132)
239 return container_of((void *)u132, struct usb_hcd, hcd_priv);
242 static inline void u132_disable(struct u132 *u132)
244 u132_to_hcd(u132)->state = HC_STATE_HALT;
248 #define kref_to_u132(d) container_of(d, struct u132, kref)
249 #define kref_to_u132_endp(d) container_of(d, struct u132_endp, kref)
250 #define kref_to_u132_udev(d) container_of(d, struct u132_udev, kref)
251 #include "../misc/usb_u132.h"
252 static const char hcd_name[] = "u132_hcd";
253 #define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE | \
254 USB_PORT_STAT_C_SUSPEND | USB_PORT_STAT_C_OVERCURRENT | \
255 USB_PORT_STAT_C_RESET) << 16)
256 static void u132_hcd_delete(struct kref *kref)
258 struct u132 *u132 = kref_to_u132(kref);
259 struct platform_device *pdev = u132->platform_dev;
260 struct usb_hcd *hcd = u132_to_hcd(u132);
261 u132->going += 1;
262 mutex_lock(&u132_module_lock);
263 list_del_init(&u132->u132_list);
264 u132_instances -= 1;
265 mutex_unlock(&u132_module_lock);
266 dev_warn(&u132->platform_dev->dev, "FREEING the hcd=%p and thus the u13"
267 "2=%p going=%d pdev=%p\n", hcd, u132, u132->going, pdev);
268 usb_put_hcd(hcd);
271 static inline void u132_u132_put_kref(struct u132 *u132)
273 kref_put(&u132->kref, u132_hcd_delete);
276 static inline void u132_u132_init_kref(struct u132 *u132)
278 kref_init(&u132->kref);
281 static void u132_udev_delete(struct kref *kref)
283 struct u132_udev *udev = kref_to_u132_udev(kref);
284 udev->udev_number = 0;
285 udev->usb_device = NULL;
286 udev->usb_addr = 0;
287 udev->enumeration = 0;
290 static inline void u132_udev_put_kref(struct u132 *u132, struct u132_udev *udev)
292 kref_put(&udev->kref, u132_udev_delete);
295 static inline void u132_udev_get_kref(struct u132 *u132, struct u132_udev *udev)
297 kref_get(&udev->kref);
300 static inline void u132_udev_init_kref(struct u132 *u132,
301 struct u132_udev *udev)
303 kref_init(&udev->kref);
306 static inline void u132_ring_put_kref(struct u132 *u132, struct u132_ring *ring)
308 kref_put(&u132->kref, u132_hcd_delete);
311 static void u132_ring_requeue_work(struct u132 *u132, struct u132_ring *ring,
312 unsigned int delta)
314 if (delta > 0) {
315 if (queue_delayed_work(workqueue, &ring->scheduler, delta))
316 return;
317 } else if (queue_delayed_work(workqueue, &ring->scheduler, 0))
318 return;
319 kref_put(&u132->kref, u132_hcd_delete);
320 return;
323 static void u132_ring_queue_work(struct u132 *u132, struct u132_ring *ring,
324 unsigned int delta)
326 kref_get(&u132->kref);
327 u132_ring_requeue_work(u132, ring, delta);
328 return;
331 static void u132_ring_cancel_work(struct u132 *u132, struct u132_ring *ring)
333 if (cancel_delayed_work(&ring->scheduler)) {
334 kref_put(&u132->kref, u132_hcd_delete);
338 static void u132_endp_delete(struct kref *kref)
340 struct u132_endp *endp = kref_to_u132_endp(kref);
341 struct u132 *u132 = endp->u132;
342 u8 usb_addr = endp->usb_addr;
343 u8 usb_endp = endp->usb_endp;
344 u8 address = u132->addr[usb_addr].address;
345 struct u132_udev *udev = &u132->udev[address];
346 u8 endp_number = endp->endp_number;
347 struct usb_host_endpoint *hep = endp->hep;
348 struct u132_ring *ring = endp->ring;
349 struct list_head *head = &endp->endp_ring;
350 ring->length -= 1;
351 if (endp == ring->curr_endp) {
352 if (list_empty(head)) {
353 ring->curr_endp = NULL;
354 list_del(head);
355 } else {
356 struct u132_endp *next_endp = list_entry(head->next,
357 struct u132_endp, endp_ring);
358 ring->curr_endp = next_endp;
359 list_del(head);
360 }} else
361 list_del(head);
362 if (endp->input) {
363 udev->endp_number_in[usb_endp] = 0;
364 u132_udev_put_kref(u132, udev);
366 if (endp->output) {
367 udev->endp_number_out[usb_endp] = 0;
368 u132_udev_put_kref(u132, udev);
370 u132->endp[endp_number - 1] = NULL;
371 hep->hcpriv = NULL;
372 kfree(endp);
373 u132_u132_put_kref(u132);
376 static inline void u132_endp_put_kref(struct u132 *u132, struct u132_endp *endp)
378 kref_put(&endp->kref, u132_endp_delete);
381 static inline void u132_endp_get_kref(struct u132 *u132, struct u132_endp *endp)
383 kref_get(&endp->kref);
386 static inline void u132_endp_init_kref(struct u132 *u132,
387 struct u132_endp *endp)
389 kref_init(&endp->kref);
390 kref_get(&u132->kref);
393 static void u132_endp_queue_work(struct u132 *u132, struct u132_endp *endp,
394 unsigned int delta)
396 if (queue_delayed_work(workqueue, &endp->scheduler, delta))
397 kref_get(&endp->kref);
400 static void u132_endp_cancel_work(struct u132 *u132, struct u132_endp *endp)
402 if (cancel_delayed_work(&endp->scheduler))
403 kref_put(&endp->kref, u132_endp_delete);
406 static inline void u132_monitor_put_kref(struct u132 *u132)
408 kref_put(&u132->kref, u132_hcd_delete);
411 static void u132_monitor_queue_work(struct u132 *u132, unsigned int delta)
413 if (queue_delayed_work(workqueue, &u132->monitor, delta))
414 kref_get(&u132->kref);
417 static void u132_monitor_requeue_work(struct u132 *u132, unsigned int delta)
419 if (!queue_delayed_work(workqueue, &u132->monitor, delta))
420 kref_put(&u132->kref, u132_hcd_delete);
423 static void u132_monitor_cancel_work(struct u132 *u132)
425 if (cancel_delayed_work(&u132->monitor))
426 kref_put(&u132->kref, u132_hcd_delete);
429 static int read_roothub_info(struct u132 *u132)
431 u32 revision;
432 int retval;
433 retval = u132_read_pcimem(u132, revision, &revision);
434 if (retval) {
435 dev_err(&u132->platform_dev->dev, "error %d accessing device co"
436 "ntrol\n", retval);
437 return retval;
438 } else if ((revision & 0xFF) == 0x10) {
439 } else if ((revision & 0xFF) == 0x11) {
440 } else {
441 dev_err(&u132->platform_dev->dev, "device revision is not valid"
442 " %08X\n", revision);
443 return -ENODEV;
445 retval = u132_read_pcimem(u132, control, &u132->hc_control);
446 if (retval) {
447 dev_err(&u132->platform_dev->dev, "error %d accessing device co"
448 "ntrol\n", retval);
449 return retval;
451 retval = u132_read_pcimem(u132, roothub.status,
452 &u132->hc_roothub_status);
453 if (retval) {
454 dev_err(&u132->platform_dev->dev, "error %d accessing device re"
455 "g roothub.status\n", retval);
456 return retval;
458 retval = u132_read_pcimem(u132, roothub.a, &u132->hc_roothub_a);
459 if (retval) {
460 dev_err(&u132->platform_dev->dev, "error %d accessing device re"
461 "g roothub.a\n", retval);
462 return retval;
465 int I = u132->num_ports;
466 int i = 0;
467 while (I-- > 0) {
468 retval = u132_read_pcimem(u132, roothub.portstatus[i],
469 &u132->hc_roothub_portstatus[i]);
470 if (retval) {
471 dev_err(&u132->platform_dev->dev, "error %d acc"
472 "essing device roothub.portstatus[%d]\n"
473 , retval, i);
474 return retval;
475 } else
476 i += 1;
479 return 0;
482 static void u132_hcd_monitor_work(struct work_struct *work)
484 struct u132 *u132 = container_of(work, struct u132, monitor.work);
485 if (u132->going > 1) {
486 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
487 , u132->going);
488 u132_monitor_put_kref(u132);
489 return;
490 } else if (u132->going > 0) {
491 dev_err(&u132->platform_dev->dev, "device is being removed\n");
492 u132_monitor_put_kref(u132);
493 return;
494 } else {
495 int retval;
496 down(&u132->sw_lock);
497 retval = read_roothub_info(u132);
498 if (retval) {
499 struct usb_hcd *hcd = u132_to_hcd(u132);
500 u132_disable(u132);
501 u132->going = 1;
502 up(&u132->sw_lock);
503 usb_hc_died(hcd);
504 ftdi_elan_gone_away(u132->platform_dev);
505 u132_monitor_put_kref(u132);
506 return;
507 } else {
508 u132_monitor_requeue_work(u132, 500);
509 up(&u132->sw_lock);
510 return;
515 static void u132_hcd_giveback_urb(struct u132 *u132, struct u132_endp *endp,
516 struct urb *urb, int status)
518 struct u132_ring *ring;
519 unsigned long irqs;
520 struct usb_hcd *hcd = u132_to_hcd(u132);
521 urb->error_count = 0;
522 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
523 usb_hcd_unlink_urb_from_ep(hcd, urb);
524 endp->queue_next += 1;
525 if (ENDP_QUEUE_SIZE > --endp->queue_size) {
526 endp->active = 0;
527 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
528 } else {
529 struct list_head *next = endp->urb_more.next;
530 struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
531 urb_more);
532 list_del(next);
533 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
534 urbq->urb;
535 endp->active = 0;
536 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
537 kfree(urbq);
538 } down(&u132->scheduler_lock);
539 ring = endp->ring;
540 ring->in_use = 0;
541 u132_ring_cancel_work(u132, ring);
542 u132_ring_queue_work(u132, ring, 0);
543 up(&u132->scheduler_lock);
544 u132_endp_put_kref(u132, endp);
545 usb_hcd_giveback_urb(hcd, urb, status);
546 return;
549 static void u132_hcd_forget_urb(struct u132 *u132, struct u132_endp *endp,
550 struct urb *urb, int status)
552 u132_endp_put_kref(u132, endp);
555 static void u132_hcd_abandon_urb(struct u132 *u132, struct u132_endp *endp,
556 struct urb *urb, int status)
558 unsigned long irqs;
559 struct usb_hcd *hcd = u132_to_hcd(u132);
560 urb->error_count = 0;
561 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
562 usb_hcd_unlink_urb_from_ep(hcd, urb);
563 endp->queue_next += 1;
564 if (ENDP_QUEUE_SIZE > --endp->queue_size) {
565 endp->active = 0;
566 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
567 } else {
568 struct list_head *next = endp->urb_more.next;
569 struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
570 urb_more);
571 list_del(next);
572 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
573 urbq->urb;
574 endp->active = 0;
575 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
576 kfree(urbq);
577 } usb_hcd_giveback_urb(hcd, urb, status);
578 return;
581 static inline int edset_input(struct u132 *u132, struct u132_ring *ring,
582 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
583 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
584 int toggle_bits, int error_count, int condition_code, int repeat_number,
585 int halted, int skipped, int actual, int non_null))
587 return usb_ftdi_elan_edset_input(u132->platform_dev, ring->number, endp,
588 urb, address, endp->usb_endp, toggle_bits, callback);
591 static inline int edset_setup(struct u132 *u132, struct u132_ring *ring,
592 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
593 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
594 int toggle_bits, int error_count, int condition_code, int repeat_number,
595 int halted, int skipped, int actual, int non_null))
597 return usb_ftdi_elan_edset_setup(u132->platform_dev, ring->number, endp,
598 urb, address, endp->usb_endp, toggle_bits, callback);
601 static inline int edset_single(struct u132 *u132, struct u132_ring *ring,
602 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
603 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
604 int toggle_bits, int error_count, int condition_code, int repeat_number,
605 int halted, int skipped, int actual, int non_null))
607 return usb_ftdi_elan_edset_single(u132->platform_dev, ring->number,
608 endp, urb, address, endp->usb_endp, toggle_bits, callback);
611 static inline int edset_output(struct u132 *u132, struct u132_ring *ring,
612 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
613 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
614 int toggle_bits, int error_count, int condition_code, int repeat_number,
615 int halted, int skipped, int actual, int non_null))
617 return usb_ftdi_elan_edset_output(u132->platform_dev, ring->number,
618 endp, urb, address, endp->usb_endp, toggle_bits, callback);
623 * must not LOCK sw_lock
626 static void u132_hcd_interrupt_recv(void *data, struct urb *urb, u8 *buf,
627 int len, int toggle_bits, int error_count, int condition_code,
628 int repeat_number, int halted, int skipped, int actual, int non_null)
630 struct u132_endp *endp = data;
631 struct u132 *u132 = endp->u132;
632 u8 address = u132->addr[endp->usb_addr].address;
633 struct u132_udev *udev = &u132->udev[address];
634 down(&u132->scheduler_lock);
635 if (u132->going > 1) {
636 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
637 , u132->going);
638 up(&u132->scheduler_lock);
639 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
640 return;
641 } else if (endp->dequeueing) {
642 endp->dequeueing = 0;
643 up(&u132->scheduler_lock);
644 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
645 return;
646 } else if (u132->going > 0) {
647 dev_err(&u132->platform_dev->dev, "device is being removed "
648 "urb=%p\n", urb);
649 up(&u132->scheduler_lock);
650 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
651 return;
652 } else if (!urb->unlinked) {
653 struct u132_ring *ring = endp->ring;
654 u8 *u = urb->transfer_buffer + urb->actual_length;
655 u8 *b = buf;
656 int L = len;
657 while (L-- > 0) {
658 *u++ = *b++;
660 urb->actual_length += len;
661 if ((condition_code == TD_CC_NOERROR) &&
662 (urb->transfer_buffer_length > urb->actual_length)) {
663 endp->toggle_bits = toggle_bits;
664 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
665 1 & toggle_bits);
666 if (urb->actual_length > 0) {
667 int retval;
668 up(&u132->scheduler_lock);
669 retval = edset_single(u132, ring, endp, urb,
670 address, endp->toggle_bits,
671 u132_hcd_interrupt_recv);
672 if (retval == 0) {
673 } else
674 u132_hcd_giveback_urb(u132, endp, urb,
675 retval);
676 } else {
677 ring->in_use = 0;
678 endp->active = 0;
679 endp->jiffies = jiffies +
680 msecs_to_jiffies(urb->interval);
681 u132_ring_cancel_work(u132, ring);
682 u132_ring_queue_work(u132, ring, 0);
683 up(&u132->scheduler_lock);
684 u132_endp_put_kref(u132, endp);
686 return;
687 } else if ((condition_code == TD_DATAUNDERRUN) &&
688 ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
689 endp->toggle_bits = toggle_bits;
690 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
691 1 & toggle_bits);
692 up(&u132->scheduler_lock);
693 u132_hcd_giveback_urb(u132, endp, urb, 0);
694 return;
695 } else {
696 if (condition_code == TD_CC_NOERROR) {
697 endp->toggle_bits = toggle_bits;
698 usb_settoggle(udev->usb_device, endp->usb_endp,
699 0, 1 & toggle_bits);
700 } else if (condition_code == TD_CC_STALL) {
701 endp->toggle_bits = 0x2;
702 usb_settoggle(udev->usb_device, endp->usb_endp,
703 0, 0);
704 } else {
705 endp->toggle_bits = 0x2;
706 usb_settoggle(udev->usb_device, endp->usb_endp,
707 0, 0);
708 dev_err(&u132->platform_dev->dev, "urb=%p givin"
709 "g back INTERRUPT %s\n", urb,
710 cc_to_text[condition_code]);
712 up(&u132->scheduler_lock);
713 u132_hcd_giveback_urb(u132, endp, urb,
714 cc_to_error[condition_code]);
715 return;
717 } else {
718 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
719 "unlinked=%d\n", urb, urb->unlinked);
720 up(&u132->scheduler_lock);
721 u132_hcd_giveback_urb(u132, endp, urb, 0);
722 return;
726 static void u132_hcd_bulk_output_sent(void *data, struct urb *urb, u8 *buf,
727 int len, int toggle_bits, int error_count, int condition_code,
728 int repeat_number, int halted, int skipped, int actual, int non_null)
730 struct u132_endp *endp = data;
731 struct u132 *u132 = endp->u132;
732 u8 address = u132->addr[endp->usb_addr].address;
733 down(&u132->scheduler_lock);
734 if (u132->going > 1) {
735 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
736 , u132->going);
737 up(&u132->scheduler_lock);
738 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
739 return;
740 } else if (endp->dequeueing) {
741 endp->dequeueing = 0;
742 up(&u132->scheduler_lock);
743 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
744 return;
745 } else if (u132->going > 0) {
746 dev_err(&u132->platform_dev->dev, "device is being removed "
747 "urb=%p\n", urb);
748 up(&u132->scheduler_lock);
749 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
750 return;
751 } else if (!urb->unlinked) {
752 struct u132_ring *ring = endp->ring;
753 urb->actual_length += len;
754 endp->toggle_bits = toggle_bits;
755 if (urb->transfer_buffer_length > urb->actual_length) {
756 int retval;
757 up(&u132->scheduler_lock);
758 retval = edset_output(u132, ring, endp, urb, address,
759 endp->toggle_bits, u132_hcd_bulk_output_sent);
760 if (retval == 0) {
761 } else
762 u132_hcd_giveback_urb(u132, endp, urb, retval);
763 return;
764 } else {
765 up(&u132->scheduler_lock);
766 u132_hcd_giveback_urb(u132, endp, urb, 0);
767 return;
769 } else {
770 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
771 "unlinked=%d\n", urb, urb->unlinked);
772 up(&u132->scheduler_lock);
773 u132_hcd_giveback_urb(u132, endp, urb, 0);
774 return;
778 static void u132_hcd_bulk_input_recv(void *data, struct urb *urb, u8 *buf,
779 int len, int toggle_bits, int error_count, int condition_code,
780 int repeat_number, int halted, int skipped, int actual, int non_null)
782 struct u132_endp *endp = data;
783 struct u132 *u132 = endp->u132;
784 u8 address = u132->addr[endp->usb_addr].address;
785 struct u132_udev *udev = &u132->udev[address];
786 down(&u132->scheduler_lock);
787 if (u132->going > 1) {
788 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
789 , u132->going);
790 up(&u132->scheduler_lock);
791 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
792 return;
793 } else if (endp->dequeueing) {
794 endp->dequeueing = 0;
795 up(&u132->scheduler_lock);
796 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
797 return;
798 } else if (u132->going > 0) {
799 dev_err(&u132->platform_dev->dev, "device is being removed "
800 "urb=%p\n", urb);
801 up(&u132->scheduler_lock);
802 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
803 return;
804 } else if (!urb->unlinked) {
805 struct u132_ring *ring = endp->ring;
806 u8 *u = urb->transfer_buffer + urb->actual_length;
807 u8 *b = buf;
808 int L = len;
809 while (L-- > 0) {
810 *u++ = *b++;
812 urb->actual_length += len;
813 if ((condition_code == TD_CC_NOERROR) &&
814 (urb->transfer_buffer_length > urb->actual_length)) {
815 int retval;
816 endp->toggle_bits = toggle_bits;
817 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
818 1 & toggle_bits);
819 up(&u132->scheduler_lock);
820 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
821 ring->number, endp, urb, address,
822 endp->usb_endp, endp->toggle_bits,
823 u132_hcd_bulk_input_recv);
824 if (retval == 0) {
825 } else
826 u132_hcd_giveback_urb(u132, endp, urb, retval);
827 return;
828 } else if (condition_code == TD_CC_NOERROR) {
829 endp->toggle_bits = toggle_bits;
830 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
831 1 & toggle_bits);
832 up(&u132->scheduler_lock);
833 u132_hcd_giveback_urb(u132, endp, urb,
834 cc_to_error[condition_code]);
835 return;
836 } else if ((condition_code == TD_DATAUNDERRUN) &&
837 ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
838 endp->toggle_bits = toggle_bits;
839 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
840 1 & toggle_bits);
841 up(&u132->scheduler_lock);
842 u132_hcd_giveback_urb(u132, endp, urb, 0);
843 return;
844 } else if (condition_code == TD_DATAUNDERRUN) {
845 endp->toggle_bits = toggle_bits;
846 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
847 1 & toggle_bits);
848 dev_warn(&u132->platform_dev->dev, "urb=%p(SHORT NOT OK"
849 ") giving back BULK IN %s\n", urb,
850 cc_to_text[condition_code]);
851 up(&u132->scheduler_lock);
852 u132_hcd_giveback_urb(u132, endp, urb, 0);
853 return;
854 } else if (condition_code == TD_CC_STALL) {
855 endp->toggle_bits = 0x2;
856 usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
857 up(&u132->scheduler_lock);
858 u132_hcd_giveback_urb(u132, endp, urb,
859 cc_to_error[condition_code]);
860 return;
861 } else {
862 endp->toggle_bits = 0x2;
863 usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
864 dev_err(&u132->platform_dev->dev, "urb=%p giving back B"
865 "ULK IN code=%d %s\n", urb, condition_code,
866 cc_to_text[condition_code]);
867 up(&u132->scheduler_lock);
868 u132_hcd_giveback_urb(u132, endp, urb,
869 cc_to_error[condition_code]);
870 return;
872 } else {
873 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
874 "unlinked=%d\n", urb, urb->unlinked);
875 up(&u132->scheduler_lock);
876 u132_hcd_giveback_urb(u132, endp, urb, 0);
877 return;
881 static void u132_hcd_configure_empty_sent(void *data, struct urb *urb, u8 *buf,
882 int len, int toggle_bits, int error_count, int condition_code,
883 int repeat_number, int halted, int skipped, int actual, int non_null)
885 struct u132_endp *endp = data;
886 struct u132 *u132 = endp->u132;
887 down(&u132->scheduler_lock);
888 if (u132->going > 1) {
889 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
890 , u132->going);
891 up(&u132->scheduler_lock);
892 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
893 return;
894 } else if (endp->dequeueing) {
895 endp->dequeueing = 0;
896 up(&u132->scheduler_lock);
897 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
898 return;
899 } else if (u132->going > 0) {
900 dev_err(&u132->platform_dev->dev, "device is being removed "
901 "urb=%p\n", urb);
902 up(&u132->scheduler_lock);
903 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
904 return;
905 } else if (!urb->unlinked) {
906 up(&u132->scheduler_lock);
907 u132_hcd_giveback_urb(u132, endp, urb, 0);
908 return;
909 } else {
910 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
911 "unlinked=%d\n", urb, urb->unlinked);
912 up(&u132->scheduler_lock);
913 u132_hcd_giveback_urb(u132, endp, urb, 0);
914 return;
918 static void u132_hcd_configure_input_recv(void *data, struct urb *urb, u8 *buf,
919 int len, int toggle_bits, int error_count, int condition_code,
920 int repeat_number, int halted, int skipped, int actual, int non_null)
922 struct u132_endp *endp = data;
923 struct u132 *u132 = endp->u132;
924 u8 address = u132->addr[endp->usb_addr].address;
925 down(&u132->scheduler_lock);
926 if (u132->going > 1) {
927 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
928 , u132->going);
929 up(&u132->scheduler_lock);
930 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
931 return;
932 } else if (endp->dequeueing) {
933 endp->dequeueing = 0;
934 up(&u132->scheduler_lock);
935 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
936 return;
937 } else if (u132->going > 0) {
938 dev_err(&u132->platform_dev->dev, "device is being removed "
939 "urb=%p\n", urb);
940 up(&u132->scheduler_lock);
941 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
942 return;
943 } else if (!urb->unlinked) {
944 struct u132_ring *ring = endp->ring;
945 u8 *u = urb->transfer_buffer;
946 u8 *b = buf;
947 int L = len;
948 while (L-- > 0) {
949 *u++ = *b++;
951 urb->actual_length = len;
952 if ((condition_code == TD_CC_NOERROR) || ((condition_code ==
953 TD_DATAUNDERRUN) && ((urb->transfer_flags &
954 URB_SHORT_NOT_OK) == 0))) {
955 int retval;
956 up(&u132->scheduler_lock);
957 retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
958 ring->number, endp, urb, address,
959 endp->usb_endp, 0x3,
960 u132_hcd_configure_empty_sent);
961 if (retval == 0) {
962 } else
963 u132_hcd_giveback_urb(u132, endp, urb, retval);
964 return;
965 } else if (condition_code == TD_CC_STALL) {
966 up(&u132->scheduler_lock);
967 dev_warn(&u132->platform_dev->dev, "giving back SETUP I"
968 "NPUT STALL urb %p\n", urb);
969 u132_hcd_giveback_urb(u132, endp, urb,
970 cc_to_error[condition_code]);
971 return;
972 } else {
973 up(&u132->scheduler_lock);
974 dev_err(&u132->platform_dev->dev, "giving back SETUP IN"
975 "PUT %s urb %p\n", cc_to_text[condition_code],
976 urb);
977 u132_hcd_giveback_urb(u132, endp, urb,
978 cc_to_error[condition_code]);
979 return;
981 } else {
982 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
983 "unlinked=%d\n", urb, urb->unlinked);
984 up(&u132->scheduler_lock);
985 u132_hcd_giveback_urb(u132, endp, urb, 0);
986 return;
990 static void u132_hcd_configure_empty_recv(void *data, struct urb *urb, u8 *buf,
991 int len, int toggle_bits, int error_count, int condition_code,
992 int repeat_number, int halted, int skipped, int actual, int non_null)
994 struct u132_endp *endp = data;
995 struct u132 *u132 = endp->u132;
996 down(&u132->scheduler_lock);
997 if (u132->going > 1) {
998 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
999 , u132->going);
1000 up(&u132->scheduler_lock);
1001 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1002 return;
1003 } else if (endp->dequeueing) {
1004 endp->dequeueing = 0;
1005 up(&u132->scheduler_lock);
1006 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1007 return;
1008 } else if (u132->going > 0) {
1009 dev_err(&u132->platform_dev->dev, "device is being removed "
1010 "urb=%p\n", urb);
1011 up(&u132->scheduler_lock);
1012 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1013 return;
1014 } else if (!urb->unlinked) {
1015 up(&u132->scheduler_lock);
1016 u132_hcd_giveback_urb(u132, endp, urb, 0);
1017 return;
1018 } else {
1019 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1020 "unlinked=%d\n", urb, urb->unlinked);
1021 up(&u132->scheduler_lock);
1022 u132_hcd_giveback_urb(u132, endp, urb, 0);
1023 return;
1027 static void u132_hcd_configure_setup_sent(void *data, struct urb *urb, u8 *buf,
1028 int len, int toggle_bits, int error_count, int condition_code,
1029 int repeat_number, int halted, int skipped, int actual, int non_null)
1031 struct u132_endp *endp = data;
1032 struct u132 *u132 = endp->u132;
1033 u8 address = u132->addr[endp->usb_addr].address;
1034 down(&u132->scheduler_lock);
1035 if (u132->going > 1) {
1036 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1037 , u132->going);
1038 up(&u132->scheduler_lock);
1039 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1040 return;
1041 } else if (endp->dequeueing) {
1042 endp->dequeueing = 0;
1043 up(&u132->scheduler_lock);
1044 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1045 return;
1046 } else if (u132->going > 0) {
1047 dev_err(&u132->platform_dev->dev, "device is being removed "
1048 "urb=%p\n", urb);
1049 up(&u132->scheduler_lock);
1050 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1051 return;
1052 } else if (!urb->unlinked) {
1053 if (usb_pipein(urb->pipe)) {
1054 int retval;
1055 struct u132_ring *ring = endp->ring;
1056 up(&u132->scheduler_lock);
1057 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1058 ring->number, endp, urb, address,
1059 endp->usb_endp, 0,
1060 u132_hcd_configure_input_recv);
1061 if (retval == 0) {
1062 } else
1063 u132_hcd_giveback_urb(u132, endp, urb, retval);
1064 return;
1065 } else {
1066 int retval;
1067 struct u132_ring *ring = endp->ring;
1068 up(&u132->scheduler_lock);
1069 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1070 ring->number, endp, urb, address,
1071 endp->usb_endp, 0,
1072 u132_hcd_configure_empty_recv);
1073 if (retval == 0) {
1074 } else
1075 u132_hcd_giveback_urb(u132, endp, urb, retval);
1076 return;
1078 } else {
1079 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1080 "unlinked=%d\n", urb, urb->unlinked);
1081 up(&u132->scheduler_lock);
1082 u132_hcd_giveback_urb(u132, endp, urb, 0);
1083 return;
1087 static void u132_hcd_enumeration_empty_recv(void *data, struct urb *urb,
1088 u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
1089 int repeat_number, int halted, int skipped, int actual, int non_null)
1091 struct u132_endp *endp = data;
1092 struct u132 *u132 = endp->u132;
1093 u8 address = u132->addr[endp->usb_addr].address;
1094 struct u132_udev *udev = &u132->udev[address];
1095 down(&u132->scheduler_lock);
1096 if (u132->going > 1) {
1097 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1098 , u132->going);
1099 up(&u132->scheduler_lock);
1100 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1101 return;
1102 } else if (endp->dequeueing) {
1103 endp->dequeueing = 0;
1104 up(&u132->scheduler_lock);
1105 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1106 return;
1107 } else if (u132->going > 0) {
1108 dev_err(&u132->platform_dev->dev, "device is being removed "
1109 "urb=%p\n", urb);
1110 up(&u132->scheduler_lock);
1111 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1112 return;
1113 } else if (!urb->unlinked) {
1114 u132->addr[0].address = 0;
1115 endp->usb_addr = udev->usb_addr;
1116 up(&u132->scheduler_lock);
1117 u132_hcd_giveback_urb(u132, endp, urb, 0);
1118 return;
1119 } else {
1120 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1121 "unlinked=%d\n", urb, urb->unlinked);
1122 up(&u132->scheduler_lock);
1123 u132_hcd_giveback_urb(u132, endp, urb, 0);
1124 return;
1128 static void u132_hcd_enumeration_address_sent(void *data, struct urb *urb,
1129 u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
1130 int repeat_number, int halted, int skipped, int actual, int non_null)
1132 struct u132_endp *endp = data;
1133 struct u132 *u132 = endp->u132;
1134 down(&u132->scheduler_lock);
1135 if (u132->going > 1) {
1136 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1137 , u132->going);
1138 up(&u132->scheduler_lock);
1139 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1140 return;
1141 } else if (endp->dequeueing) {
1142 endp->dequeueing = 0;
1143 up(&u132->scheduler_lock);
1144 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1145 return;
1146 } else if (u132->going > 0) {
1147 dev_err(&u132->platform_dev->dev, "device is being removed "
1148 "urb=%p\n", urb);
1149 up(&u132->scheduler_lock);
1150 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1151 return;
1152 } else if (!urb->unlinked) {
1153 int retval;
1154 struct u132_ring *ring = endp->ring;
1155 up(&u132->scheduler_lock);
1156 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1157 ring->number, endp, urb, 0, endp->usb_endp, 0,
1158 u132_hcd_enumeration_empty_recv);
1159 if (retval == 0) {
1160 } else
1161 u132_hcd_giveback_urb(u132, endp, urb, retval);
1162 return;
1163 } else {
1164 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1165 "unlinked=%d\n", urb, urb->unlinked);
1166 up(&u132->scheduler_lock);
1167 u132_hcd_giveback_urb(u132, endp, urb, 0);
1168 return;
1172 static void u132_hcd_initial_empty_sent(void *data, struct urb *urb, u8 *buf,
1173 int len, int toggle_bits, int error_count, int condition_code,
1174 int repeat_number, int halted, int skipped, int actual, int non_null)
1176 struct u132_endp *endp = data;
1177 struct u132 *u132 = endp->u132;
1178 down(&u132->scheduler_lock);
1179 if (u132->going > 1) {
1180 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1181 , u132->going);
1182 up(&u132->scheduler_lock);
1183 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1184 return;
1185 } else if (endp->dequeueing) {
1186 endp->dequeueing = 0;
1187 up(&u132->scheduler_lock);
1188 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1189 return;
1190 } else if (u132->going > 0) {
1191 dev_err(&u132->platform_dev->dev, "device is being removed "
1192 "urb=%p\n", urb);
1193 up(&u132->scheduler_lock);
1194 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1195 return;
1196 } else if (!urb->unlinked) {
1197 up(&u132->scheduler_lock);
1198 u132_hcd_giveback_urb(u132, endp, urb, 0);
1199 return;
1200 } else {
1201 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1202 "unlinked=%d\n", urb, urb->unlinked);
1203 up(&u132->scheduler_lock);
1204 u132_hcd_giveback_urb(u132, endp, urb, 0);
1205 return;
1209 static void u132_hcd_initial_input_recv(void *data, struct urb *urb, u8 *buf,
1210 int len, int toggle_bits, int error_count, int condition_code,
1211 int repeat_number, int halted, int skipped, int actual, int non_null)
1213 struct u132_endp *endp = data;
1214 struct u132 *u132 = endp->u132;
1215 u8 address = u132->addr[endp->usb_addr].address;
1216 down(&u132->scheduler_lock);
1217 if (u132->going > 1) {
1218 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1219 , u132->going);
1220 up(&u132->scheduler_lock);
1221 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1222 return;
1223 } else if (endp->dequeueing) {
1224 endp->dequeueing = 0;
1225 up(&u132->scheduler_lock);
1226 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1227 return;
1228 } else if (u132->going > 0) {
1229 dev_err(&u132->platform_dev->dev, "device is being removed "
1230 "urb=%p\n", urb);
1231 up(&u132->scheduler_lock);
1232 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1233 return;
1234 } else if (!urb->unlinked) {
1235 int retval;
1236 struct u132_ring *ring = endp->ring;
1237 u8 *u = urb->transfer_buffer;
1238 u8 *b = buf;
1239 int L = len;
1240 while (L-- > 0) {
1241 *u++ = *b++;
1243 urb->actual_length = len;
1244 up(&u132->scheduler_lock);
1245 retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
1246 ring->number, endp, urb, address, endp->usb_endp, 0x3,
1247 u132_hcd_initial_empty_sent);
1248 if (retval == 0) {
1249 } else
1250 u132_hcd_giveback_urb(u132, endp, urb, retval);
1251 return;
1252 } else {
1253 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1254 "unlinked=%d\n", urb, urb->unlinked);
1255 up(&u132->scheduler_lock);
1256 u132_hcd_giveback_urb(u132, endp, urb, 0);
1257 return;
1261 static void u132_hcd_initial_setup_sent(void *data, struct urb *urb, u8 *buf,
1262 int len, int toggle_bits, int error_count, int condition_code,
1263 int repeat_number, int halted, int skipped, int actual, int non_null)
1265 struct u132_endp *endp = data;
1266 struct u132 *u132 = endp->u132;
1267 u8 address = u132->addr[endp->usb_addr].address;
1268 down(&u132->scheduler_lock);
1269 if (u132->going > 1) {
1270 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1271 , u132->going);
1272 up(&u132->scheduler_lock);
1273 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1274 return;
1275 } else if (endp->dequeueing) {
1276 endp->dequeueing = 0;
1277 up(&u132->scheduler_lock);
1278 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1279 return;
1280 } else if (u132->going > 0) {
1281 dev_err(&u132->platform_dev->dev, "device is being removed "
1282 "urb=%p\n", urb);
1283 up(&u132->scheduler_lock);
1284 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1285 return;
1286 } else if (!urb->unlinked) {
1287 int retval;
1288 struct u132_ring *ring = endp->ring;
1289 up(&u132->scheduler_lock);
1290 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1291 ring->number, endp, urb, address, endp->usb_endp, 0,
1292 u132_hcd_initial_input_recv);
1293 if (retval == 0) {
1294 } else
1295 u132_hcd_giveback_urb(u132, endp, urb, retval);
1296 return;
1297 } else {
1298 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1299 "unlinked=%d\n", urb, urb->unlinked);
1300 up(&u132->scheduler_lock);
1301 u132_hcd_giveback_urb(u132, endp, urb, 0);
1302 return;
1307 * this work function is only executed from the work queue
1310 static void u132_hcd_ring_work_scheduler(struct work_struct *work)
1312 struct u132_ring *ring =
1313 container_of(work, struct u132_ring, scheduler.work);
1314 struct u132 *u132 = ring->u132;
1315 down(&u132->scheduler_lock);
1316 if (ring->in_use) {
1317 up(&u132->scheduler_lock);
1318 u132_ring_put_kref(u132, ring);
1319 return;
1320 } else if (ring->curr_endp) {
1321 struct u132_endp *last_endp = ring->curr_endp;
1322 struct list_head *scan;
1323 struct list_head *head = &last_endp->endp_ring;
1324 unsigned long wakeup = 0;
1325 list_for_each(scan, head) {
1326 struct u132_endp *endp = list_entry(scan,
1327 struct u132_endp, endp_ring);
1328 if (endp->queue_next == endp->queue_last) {
1329 } else if ((endp->delayed == 0)
1330 || time_after_eq(jiffies, endp->jiffies)) {
1331 ring->curr_endp = endp;
1332 u132_endp_cancel_work(u132, last_endp);
1333 u132_endp_queue_work(u132, last_endp, 0);
1334 up(&u132->scheduler_lock);
1335 u132_ring_put_kref(u132, ring);
1336 return;
1337 } else {
1338 unsigned long delta = endp->jiffies - jiffies;
1339 if (delta > wakeup)
1340 wakeup = delta;
1343 if (last_endp->queue_next == last_endp->queue_last) {
1344 } else if ((last_endp->delayed == 0) || time_after_eq(jiffies,
1345 last_endp->jiffies)) {
1346 u132_endp_cancel_work(u132, last_endp);
1347 u132_endp_queue_work(u132, last_endp, 0);
1348 up(&u132->scheduler_lock);
1349 u132_ring_put_kref(u132, ring);
1350 return;
1351 } else {
1352 unsigned long delta = last_endp->jiffies - jiffies;
1353 if (delta > wakeup)
1354 wakeup = delta;
1356 if (wakeup > 0) {
1357 u132_ring_requeue_work(u132, ring, wakeup);
1358 up(&u132->scheduler_lock);
1359 return;
1360 } else {
1361 up(&u132->scheduler_lock);
1362 u132_ring_put_kref(u132, ring);
1363 return;
1365 } else {
1366 up(&u132->scheduler_lock);
1367 u132_ring_put_kref(u132, ring);
1368 return;
1372 static void u132_hcd_endp_work_scheduler(struct work_struct *work)
1374 struct u132_ring *ring;
1375 struct u132_endp *endp =
1376 container_of(work, struct u132_endp, scheduler.work);
1377 struct u132 *u132 = endp->u132;
1378 down(&u132->scheduler_lock);
1379 ring = endp->ring;
1380 if (endp->edset_flush) {
1381 endp->edset_flush = 0;
1382 if (endp->dequeueing)
1383 usb_ftdi_elan_edset_flush(u132->platform_dev,
1384 ring->number, endp);
1385 up(&u132->scheduler_lock);
1386 u132_endp_put_kref(u132, endp);
1387 return;
1388 } else if (endp->active) {
1389 up(&u132->scheduler_lock);
1390 u132_endp_put_kref(u132, endp);
1391 return;
1392 } else if (ring->in_use) {
1393 up(&u132->scheduler_lock);
1394 u132_endp_put_kref(u132, endp);
1395 return;
1396 } else if (endp->queue_next == endp->queue_last) {
1397 up(&u132->scheduler_lock);
1398 u132_endp_put_kref(u132, endp);
1399 return;
1400 } else if (endp->pipetype == PIPE_INTERRUPT) {
1401 u8 address = u132->addr[endp->usb_addr].address;
1402 if (ring->in_use) {
1403 up(&u132->scheduler_lock);
1404 u132_endp_put_kref(u132, endp);
1405 return;
1406 } else {
1407 int retval;
1408 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1409 endp->queue_next];
1410 endp->active = 1;
1411 ring->curr_endp = endp;
1412 ring->in_use = 1;
1413 up(&u132->scheduler_lock);
1414 retval = edset_single(u132, ring, endp, urb, address,
1415 endp->toggle_bits, u132_hcd_interrupt_recv);
1416 if (retval == 0) {
1417 } else
1418 u132_hcd_giveback_urb(u132, endp, urb, retval);
1419 return;
1421 } else if (endp->pipetype == PIPE_CONTROL) {
1422 u8 address = u132->addr[endp->usb_addr].address;
1423 if (ring->in_use) {
1424 up(&u132->scheduler_lock);
1425 u132_endp_put_kref(u132, endp);
1426 return;
1427 } else if (address == 0) {
1428 int retval;
1429 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1430 endp->queue_next];
1431 endp->active = 1;
1432 ring->curr_endp = endp;
1433 ring->in_use = 1;
1434 up(&u132->scheduler_lock);
1435 retval = edset_setup(u132, ring, endp, urb, address,
1436 0x2, u132_hcd_initial_setup_sent);
1437 if (retval == 0) {
1438 } else
1439 u132_hcd_giveback_urb(u132, endp, urb, retval);
1440 return;
1441 } else if (endp->usb_addr == 0) {
1442 int retval;
1443 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1444 endp->queue_next];
1445 endp->active = 1;
1446 ring->curr_endp = endp;
1447 ring->in_use = 1;
1448 up(&u132->scheduler_lock);
1449 retval = edset_setup(u132, ring, endp, urb, 0, 0x2,
1450 u132_hcd_enumeration_address_sent);
1451 if (retval == 0) {
1452 } else
1453 u132_hcd_giveback_urb(u132, endp, urb, retval);
1454 return;
1455 } else {
1456 int retval;
1457 u8 address = u132->addr[endp->usb_addr].address;
1458 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1459 endp->queue_next];
1460 endp->active = 1;
1461 ring->curr_endp = endp;
1462 ring->in_use = 1;
1463 up(&u132->scheduler_lock);
1464 retval = edset_setup(u132, ring, endp, urb, address,
1465 0x2, u132_hcd_configure_setup_sent);
1466 if (retval == 0) {
1467 } else
1468 u132_hcd_giveback_urb(u132, endp, urb, retval);
1469 return;
1471 } else {
1472 if (endp->input) {
1473 u8 address = u132->addr[endp->usb_addr].address;
1474 if (ring->in_use) {
1475 up(&u132->scheduler_lock);
1476 u132_endp_put_kref(u132, endp);
1477 return;
1478 } else {
1479 int retval;
1480 struct urb *urb = endp->urb_list[
1481 ENDP_QUEUE_MASK & endp->queue_next];
1482 endp->active = 1;
1483 ring->curr_endp = endp;
1484 ring->in_use = 1;
1485 up(&u132->scheduler_lock);
1486 retval = edset_input(u132, ring, endp, urb,
1487 address, endp->toggle_bits,
1488 u132_hcd_bulk_input_recv);
1489 if (retval == 0) {
1490 } else
1491 u132_hcd_giveback_urb(u132, endp, urb,
1492 retval);
1493 return;
1495 } else { /* output pipe */
1496 u8 address = u132->addr[endp->usb_addr].address;
1497 if (ring->in_use) {
1498 up(&u132->scheduler_lock);
1499 u132_endp_put_kref(u132, endp);
1500 return;
1501 } else {
1502 int retval;
1503 struct urb *urb = endp->urb_list[
1504 ENDP_QUEUE_MASK & endp->queue_next];
1505 endp->active = 1;
1506 ring->curr_endp = endp;
1507 ring->in_use = 1;
1508 up(&u132->scheduler_lock);
1509 retval = edset_output(u132, ring, endp, urb,
1510 address, endp->toggle_bits,
1511 u132_hcd_bulk_output_sent);
1512 if (retval == 0) {
1513 } else
1514 u132_hcd_giveback_urb(u132, endp, urb,
1515 retval);
1516 return;
1521 #ifdef CONFIG_PM
1523 static void port_power(struct u132 *u132, int pn, int is_on)
1525 u132->port[pn].power = is_on;
1528 #endif
1530 static void u132_power(struct u132 *u132, int is_on)
1532 struct usb_hcd *hcd = u132_to_hcd(u132)
1533 ; /* hub is inactive unless the port is powered */
1534 if (is_on) {
1535 if (u132->power)
1536 return;
1537 u132->power = 1;
1538 } else {
1539 u132->power = 0;
1540 hcd->state = HC_STATE_HALT;
1544 static int u132_periodic_reinit(struct u132 *u132)
1546 int retval;
1547 u32 fi = u132->hc_fminterval & 0x03fff;
1548 u32 fit;
1549 u32 fminterval;
1550 retval = u132_read_pcimem(u132, fminterval, &fminterval);
1551 if (retval)
1552 return retval;
1553 fit = fminterval & FIT;
1554 retval = u132_write_pcimem(u132, fminterval,
1555 (fit ^ FIT) | u132->hc_fminterval);
1556 if (retval)
1557 return retval;
1558 retval = u132_write_pcimem(u132, periodicstart,
1559 ((9 *fi) / 10) & 0x3fff);
1560 if (retval)
1561 return retval;
1562 return 0;
1565 static char *hcfs2string(int state)
1567 switch (state) {
1568 case OHCI_USB_RESET:
1569 return "reset";
1570 case OHCI_USB_RESUME:
1571 return "resume";
1572 case OHCI_USB_OPER:
1573 return "operational";
1574 case OHCI_USB_SUSPEND:
1575 return "suspend";
1577 return "?";
1580 static int u132_init(struct u132 *u132)
1582 int retval;
1583 u32 control;
1584 u132_disable(u132);
1585 u132->next_statechange = jiffies;
1586 retval = u132_write_pcimem(u132, intrdisable, OHCI_INTR_MIE);
1587 if (retval)
1588 return retval;
1589 retval = u132_read_pcimem(u132, control, &control);
1590 if (retval)
1591 return retval;
1592 if (u132->num_ports == 0) {
1593 u32 rh_a = -1;
1594 retval = u132_read_pcimem(u132, roothub.a, &rh_a);
1595 if (retval)
1596 return retval;
1597 u132->num_ports = rh_a & RH_A_NDP;
1598 retval = read_roothub_info(u132);
1599 if (retval)
1600 return retval;
1602 if (u132->num_ports > MAX_U132_PORTS) {
1603 return -EINVAL;
1605 return 0;
1609 /* Start an OHCI controller, set the BUS operational
1610 * resets USB and controller
1611 * enable interrupts
1613 static int u132_run(struct u132 *u132)
1615 int retval;
1616 u32 control;
1617 u32 status;
1618 u32 fminterval;
1619 u32 periodicstart;
1620 u32 cmdstatus;
1621 u32 roothub_a;
1622 int mask = OHCI_INTR_INIT;
1623 int first = u132->hc_fminterval == 0;
1624 int sleep_time = 0;
1625 int reset_timeout = 30; /* ... allow extra time */
1626 u132_disable(u132);
1627 if (first) {
1628 u32 temp;
1629 retval = u132_read_pcimem(u132, fminterval, &temp);
1630 if (retval)
1631 return retval;
1632 u132->hc_fminterval = temp & 0x3fff;
1633 if (u132->hc_fminterval != FI) {
1635 u132->hc_fminterval |= FSMP(u132->hc_fminterval) << 16;
1637 retval = u132_read_pcimem(u132, control, &u132->hc_control);
1638 if (retval)
1639 return retval;
1640 dev_info(&u132->platform_dev->dev, "resetting from state '%s', control "
1641 "= %08X\n", hcfs2string(u132->hc_control & OHCI_CTRL_HCFS),
1642 u132->hc_control);
1643 switch (u132->hc_control & OHCI_CTRL_HCFS) {
1644 case OHCI_USB_OPER:
1645 sleep_time = 0;
1646 break;
1647 case OHCI_USB_SUSPEND:
1648 case OHCI_USB_RESUME:
1649 u132->hc_control &= OHCI_CTRL_RWC;
1650 u132->hc_control |= OHCI_USB_RESUME;
1651 sleep_time = 10;
1652 break;
1653 default:
1654 u132->hc_control &= OHCI_CTRL_RWC;
1655 u132->hc_control |= OHCI_USB_RESET;
1656 sleep_time = 50;
1657 break;
1659 retval = u132_write_pcimem(u132, control, u132->hc_control);
1660 if (retval)
1661 return retval;
1662 retval = u132_read_pcimem(u132, control, &control);
1663 if (retval)
1664 return retval;
1665 msleep(sleep_time);
1666 retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1667 if (retval)
1668 return retval;
1669 if (!(roothub_a & RH_A_NPS)) {
1670 int temp; /* power down each port */
1671 for (temp = 0; temp < u132->num_ports; temp++) {
1672 retval = u132_write_pcimem(u132,
1673 roothub.portstatus[temp], RH_PS_LSDA);
1674 if (retval)
1675 return retval;
1678 retval = u132_read_pcimem(u132, control, &control);
1679 if (retval)
1680 return retval;
1681 retry:retval = u132_read_pcimem(u132, cmdstatus, &status);
1682 if (retval)
1683 return retval;
1684 retval = u132_write_pcimem(u132, cmdstatus, OHCI_HCR);
1685 if (retval)
1686 return retval;
1687 extra:{
1688 retval = u132_read_pcimem(u132, cmdstatus, &status);
1689 if (retval)
1690 return retval;
1691 if (0 != (status & OHCI_HCR)) {
1692 if (--reset_timeout == 0) {
1693 dev_err(&u132->platform_dev->dev, "USB HC reset"
1694 " timed out!\n");
1695 return -ENODEV;
1696 } else {
1697 msleep(5);
1698 goto extra;
1702 if (u132->flags & OHCI_QUIRK_INITRESET) {
1703 retval = u132_write_pcimem(u132, control, u132->hc_control);
1704 if (retval)
1705 return retval;
1706 retval = u132_read_pcimem(u132, control, &control);
1707 if (retval)
1708 return retval;
1710 retval = u132_write_pcimem(u132, ed_controlhead, 0x00000000);
1711 if (retval)
1712 return retval;
1713 retval = u132_write_pcimem(u132, ed_bulkhead, 0x11000000);
1714 if (retval)
1715 return retval;
1716 retval = u132_write_pcimem(u132, hcca, 0x00000000);
1717 if (retval)
1718 return retval;
1719 retval = u132_periodic_reinit(u132);
1720 if (retval)
1721 return retval;
1722 retval = u132_read_pcimem(u132, fminterval, &fminterval);
1723 if (retval)
1724 return retval;
1725 retval = u132_read_pcimem(u132, periodicstart, &periodicstart);
1726 if (retval)
1727 return retval;
1728 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
1729 if (!(u132->flags & OHCI_QUIRK_INITRESET)) {
1730 u132->flags |= OHCI_QUIRK_INITRESET;
1731 goto retry;
1732 } else
1733 dev_err(&u132->platform_dev->dev, "init err(%08x %04x)"
1734 "\n", fminterval, periodicstart);
1735 } /* start controller operations */
1736 u132->hc_control &= OHCI_CTRL_RWC;
1737 u132->hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
1738 retval = u132_write_pcimem(u132, control, u132->hc_control);
1739 if (retval)
1740 return retval;
1741 retval = u132_write_pcimem(u132, cmdstatus, OHCI_BLF);
1742 if (retval)
1743 return retval;
1744 retval = u132_read_pcimem(u132, cmdstatus, &cmdstatus);
1745 if (retval)
1746 return retval;
1747 retval = u132_read_pcimem(u132, control, &control);
1748 if (retval)
1749 return retval;
1750 u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1751 retval = u132_write_pcimem(u132, roothub.status, RH_HS_DRWE);
1752 if (retval)
1753 return retval;
1754 retval = u132_write_pcimem(u132, intrstatus, mask);
1755 if (retval)
1756 return retval;
1757 retval = u132_write_pcimem(u132, intrdisable,
1758 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
1759 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
1760 OHCI_INTR_SO);
1761 if (retval)
1762 return retval; /* handle root hub init quirks ... */
1763 retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1764 if (retval)
1765 return retval;
1766 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
1767 if (u132->flags & OHCI_QUIRK_SUPERIO) {
1768 roothub_a |= RH_A_NOCP;
1769 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
1770 retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1771 if (retval)
1772 return retval;
1773 } else if ((u132->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
1774 roothub_a |= RH_A_NPS;
1775 retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1776 if (retval)
1777 return retval;
1779 retval = u132_write_pcimem(u132, roothub.status, RH_HS_LPSC);
1780 if (retval)
1781 return retval;
1782 retval = u132_write_pcimem(u132, roothub.b,
1783 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
1784 if (retval)
1785 return retval;
1786 retval = u132_read_pcimem(u132, control, &control);
1787 if (retval)
1788 return retval;
1789 mdelay((roothub_a >> 23) & 0x1fe);
1790 u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1791 return 0;
1794 static void u132_hcd_stop(struct usb_hcd *hcd)
1796 struct u132 *u132 = hcd_to_u132(hcd);
1797 if (u132->going > 1) {
1798 dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p) has b"
1799 "een removed %d\n", u132, hcd, u132->going);
1800 } else if (u132->going > 0) {
1801 dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
1802 "ed\n", hcd);
1803 } else {
1804 down(&u132->sw_lock);
1805 msleep(100);
1806 u132_power(u132, 0);
1807 up(&u132->sw_lock);
1811 static int u132_hcd_start(struct usb_hcd *hcd)
1813 struct u132 *u132 = hcd_to_u132(hcd);
1814 if (u132->going > 1) {
1815 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1816 , u132->going);
1817 return -ENODEV;
1818 } else if (u132->going > 0) {
1819 dev_err(&u132->platform_dev->dev, "device is being removed\n");
1820 return -ESHUTDOWN;
1821 } else if (hcd->self.controller) {
1822 int retval;
1823 struct platform_device *pdev =
1824 to_platform_device(hcd->self.controller);
1825 u16 vendor = ((struct u132_platform_data *)
1826 (pdev->dev.platform_data))->vendor;
1827 u16 device = ((struct u132_platform_data *)
1828 (pdev->dev.platform_data))->device;
1829 down(&u132->sw_lock);
1830 msleep(10);
1831 if (vendor == PCI_VENDOR_ID_AMD && device == 0x740c) {
1832 u132->flags = OHCI_QUIRK_AMD756;
1833 } else if (vendor == PCI_VENDOR_ID_OPTI && device == 0xc861) {
1834 dev_err(&u132->platform_dev->dev, "WARNING: OPTi workar"
1835 "ounds unavailable\n");
1836 } else if (vendor == PCI_VENDOR_ID_COMPAQ && device == 0xa0f8)
1837 u132->flags |= OHCI_QUIRK_ZFMICRO;
1838 retval = u132_run(u132);
1839 if (retval) {
1840 u132_disable(u132);
1841 u132->going = 1;
1843 msleep(100);
1844 up(&u132->sw_lock);
1845 return retval;
1846 } else {
1847 dev_err(&u132->platform_dev->dev, "platform_device missing\n");
1848 return -ENODEV;
1852 static int u132_hcd_reset(struct usb_hcd *hcd)
1854 struct u132 *u132 = hcd_to_u132(hcd);
1855 if (u132->going > 1) {
1856 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1857 , u132->going);
1858 return -ENODEV;
1859 } else if (u132->going > 0) {
1860 dev_err(&u132->platform_dev->dev, "device is being removed\n");
1861 return -ESHUTDOWN;
1862 } else {
1863 int retval;
1864 down(&u132->sw_lock);
1865 retval = u132_init(u132);
1866 if (retval) {
1867 u132_disable(u132);
1868 u132->going = 1;
1870 up(&u132->sw_lock);
1871 return retval;
1875 static int create_endpoint_and_queue_int(struct u132 *u132,
1876 struct u132_udev *udev, struct urb *urb,
1877 struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1878 gfp_t mem_flags)
1880 struct u132_ring *ring;
1881 unsigned long irqs;
1882 int rc;
1883 u8 endp_number;
1884 struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
1886 if (!endp) {
1887 return -ENOMEM;
1890 spin_lock_init(&endp->queue_lock.slock);
1891 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
1892 rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
1893 if (rc) {
1894 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1895 kfree(endp);
1896 return rc;
1899 endp_number = ++u132->num_endpoints;
1900 urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
1901 INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1902 INIT_LIST_HEAD(&endp->urb_more);
1903 ring = endp->ring = &u132->ring[0];
1904 if (ring->curr_endp) {
1905 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
1906 } else {
1907 INIT_LIST_HEAD(&endp->endp_ring);
1908 ring->curr_endp = endp;
1910 ring->length += 1;
1911 endp->dequeueing = 0;
1912 endp->edset_flush = 0;
1913 endp->active = 0;
1914 endp->delayed = 0;
1915 endp->endp_number = endp_number;
1916 endp->u132 = u132;
1917 endp->hep = urb->ep;
1918 endp->pipetype = usb_pipetype(urb->pipe);
1919 u132_endp_init_kref(u132, endp);
1920 if (usb_pipein(urb->pipe)) {
1921 endp->toggle_bits = 0x2;
1922 usb_settoggle(udev->usb_device, usb_endp, 0, 0);
1923 endp->input = 1;
1924 endp->output = 0;
1925 udev->endp_number_in[usb_endp] = endp_number;
1926 u132_udev_get_kref(u132, udev);
1927 } else {
1928 endp->toggle_bits = 0x2;
1929 usb_settoggle(udev->usb_device, usb_endp, 1, 0);
1930 endp->input = 0;
1931 endp->output = 1;
1932 udev->endp_number_out[usb_endp] = endp_number;
1933 u132_udev_get_kref(u132, udev);
1935 urb->hcpriv = u132;
1936 endp->delayed = 1;
1937 endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1938 endp->udev_number = address;
1939 endp->usb_addr = usb_addr;
1940 endp->usb_endp = usb_endp;
1941 endp->queue_size = 1;
1942 endp->queue_last = 0;
1943 endp->queue_next = 0;
1944 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1945 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1946 u132_endp_queue_work(u132, endp, msecs_to_jiffies(urb->interval));
1947 return 0;
1950 static int queue_int_on_old_endpoint(struct u132 *u132,
1951 struct u132_udev *udev, struct urb *urb,
1952 struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
1953 u8 usb_endp, u8 address)
1955 urb->hcpriv = u132;
1956 endp->delayed = 1;
1957 endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1958 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
1959 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1960 } else {
1961 struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
1962 GFP_ATOMIC);
1963 if (urbq == NULL) {
1964 endp->queue_size -= 1;
1965 return -ENOMEM;
1966 } else {
1967 list_add_tail(&urbq->urb_more, &endp->urb_more);
1968 urbq->urb = urb;
1971 return 0;
1974 static int create_endpoint_and_queue_bulk(struct u132 *u132,
1975 struct u132_udev *udev, struct urb *urb,
1976 struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1977 gfp_t mem_flags)
1979 int ring_number;
1980 struct u132_ring *ring;
1981 unsigned long irqs;
1982 int rc;
1983 u8 endp_number;
1984 struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
1986 if (!endp) {
1987 return -ENOMEM;
1990 spin_lock_init(&endp->queue_lock.slock);
1991 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
1992 rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
1993 if (rc) {
1994 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1995 kfree(endp);
1996 return rc;
1999 endp_number = ++u132->num_endpoints;
2000 urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
2001 INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
2002 INIT_LIST_HEAD(&endp->urb_more);
2003 endp->dequeueing = 0;
2004 endp->edset_flush = 0;
2005 endp->active = 0;
2006 endp->delayed = 0;
2007 endp->endp_number = endp_number;
2008 endp->u132 = u132;
2009 endp->hep = urb->ep;
2010 endp->pipetype = usb_pipetype(urb->pipe);
2011 u132_endp_init_kref(u132, endp);
2012 if (usb_pipein(urb->pipe)) {
2013 endp->toggle_bits = 0x2;
2014 usb_settoggle(udev->usb_device, usb_endp, 0, 0);
2015 ring_number = 3;
2016 endp->input = 1;
2017 endp->output = 0;
2018 udev->endp_number_in[usb_endp] = endp_number;
2019 u132_udev_get_kref(u132, udev);
2020 } else {
2021 endp->toggle_bits = 0x2;
2022 usb_settoggle(udev->usb_device, usb_endp, 1, 0);
2023 ring_number = 2;
2024 endp->input = 0;
2025 endp->output = 1;
2026 udev->endp_number_out[usb_endp] = endp_number;
2027 u132_udev_get_kref(u132, udev);
2029 ring = endp->ring = &u132->ring[ring_number - 1];
2030 if (ring->curr_endp) {
2031 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2032 } else {
2033 INIT_LIST_HEAD(&endp->endp_ring);
2034 ring->curr_endp = endp;
2036 ring->length += 1;
2037 urb->hcpriv = u132;
2038 endp->udev_number = address;
2039 endp->usb_addr = usb_addr;
2040 endp->usb_endp = usb_endp;
2041 endp->queue_size = 1;
2042 endp->queue_last = 0;
2043 endp->queue_next = 0;
2044 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2045 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2046 u132_endp_queue_work(u132, endp, 0);
2047 return 0;
2050 static int queue_bulk_on_old_endpoint(struct u132 *u132, struct u132_udev *udev,
2051 struct urb *urb,
2052 struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2053 u8 usb_endp, u8 address)
2055 urb->hcpriv = u132;
2056 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2057 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2058 } else {
2059 struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
2060 GFP_ATOMIC);
2061 if (urbq == NULL) {
2062 endp->queue_size -= 1;
2063 return -ENOMEM;
2064 } else {
2065 list_add_tail(&urbq->urb_more, &endp->urb_more);
2066 urbq->urb = urb;
2069 return 0;
2072 static int create_endpoint_and_queue_control(struct u132 *u132,
2073 struct urb *urb,
2074 struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp,
2075 gfp_t mem_flags)
2077 struct u132_ring *ring;
2078 unsigned long irqs;
2079 int rc;
2080 u8 endp_number;
2081 struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
2083 if (!endp) {
2084 return -ENOMEM;
2087 spin_lock_init(&endp->queue_lock.slock);
2088 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2089 rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
2090 if (rc) {
2091 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2092 kfree(endp);
2093 return rc;
2096 endp_number = ++u132->num_endpoints;
2097 urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
2098 INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
2099 INIT_LIST_HEAD(&endp->urb_more);
2100 ring = endp->ring = &u132->ring[0];
2101 if (ring->curr_endp) {
2102 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2103 } else {
2104 INIT_LIST_HEAD(&endp->endp_ring);
2105 ring->curr_endp = endp;
2107 ring->length += 1;
2108 endp->dequeueing = 0;
2109 endp->edset_flush = 0;
2110 endp->active = 0;
2111 endp->delayed = 0;
2112 endp->endp_number = endp_number;
2113 endp->u132 = u132;
2114 endp->hep = urb->ep;
2115 u132_endp_init_kref(u132, endp);
2116 u132_endp_get_kref(u132, endp);
2117 if (usb_addr == 0) {
2118 u8 address = u132->addr[usb_addr].address;
2119 struct u132_udev *udev = &u132->udev[address];
2120 endp->udev_number = address;
2121 endp->usb_addr = usb_addr;
2122 endp->usb_endp = usb_endp;
2123 endp->input = 1;
2124 endp->output = 1;
2125 endp->pipetype = usb_pipetype(urb->pipe);
2126 u132_udev_init_kref(u132, udev);
2127 u132_udev_get_kref(u132, udev);
2128 udev->endp_number_in[usb_endp] = endp_number;
2129 udev->endp_number_out[usb_endp] = endp_number;
2130 urb->hcpriv = u132;
2131 endp->queue_size = 1;
2132 endp->queue_last = 0;
2133 endp->queue_next = 0;
2134 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2135 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2136 u132_endp_queue_work(u132, endp, 0);
2137 return 0;
2138 } else { /*(usb_addr > 0) */
2139 u8 address = u132->addr[usb_addr].address;
2140 struct u132_udev *udev = &u132->udev[address];
2141 endp->udev_number = address;
2142 endp->usb_addr = usb_addr;
2143 endp->usb_endp = usb_endp;
2144 endp->input = 1;
2145 endp->output = 1;
2146 endp->pipetype = usb_pipetype(urb->pipe);
2147 u132_udev_get_kref(u132, udev);
2148 udev->enumeration = 2;
2149 udev->endp_number_in[usb_endp] = endp_number;
2150 udev->endp_number_out[usb_endp] = endp_number;
2151 urb->hcpriv = u132;
2152 endp->queue_size = 1;
2153 endp->queue_last = 0;
2154 endp->queue_next = 0;
2155 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2156 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2157 u132_endp_queue_work(u132, endp, 0);
2158 return 0;
2162 static int queue_control_on_old_endpoint(struct u132 *u132,
2163 struct urb *urb,
2164 struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2165 u8 usb_endp)
2167 if (usb_addr == 0) {
2168 if (usb_pipein(urb->pipe)) {
2169 urb->hcpriv = u132;
2170 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2171 endp->urb_list[ENDP_QUEUE_MASK &
2172 endp->queue_last++] = urb;
2173 } else {
2174 struct u132_urbq *urbq =
2175 kmalloc(sizeof(struct u132_urbq),
2176 GFP_ATOMIC);
2177 if (urbq == NULL) {
2178 endp->queue_size -= 1;
2179 return -ENOMEM;
2180 } else {
2181 list_add_tail(&urbq->urb_more,
2182 &endp->urb_more);
2183 urbq->urb = urb;
2186 return 0;
2187 } else { /* usb_pipeout(urb->pipe) */
2188 struct u132_addr *addr = &u132->addr[usb_dev->devnum];
2189 int I = MAX_U132_UDEVS;
2190 int i = 0;
2191 while (--I > 0) {
2192 struct u132_udev *udev = &u132->udev[++i];
2193 if (udev->usb_device) {
2194 continue;
2195 } else {
2196 udev->enumeration = 1;
2197 u132->addr[0].address = i;
2198 endp->udev_number = i;
2199 udev->udev_number = i;
2200 udev->usb_addr = usb_dev->devnum;
2201 u132_udev_init_kref(u132, udev);
2202 udev->endp_number_in[usb_endp] =
2203 endp->endp_number;
2204 u132_udev_get_kref(u132, udev);
2205 udev->endp_number_out[usb_endp] =
2206 endp->endp_number;
2207 udev->usb_device = usb_dev;
2208 ((u8 *) (urb->setup_packet))[2] =
2209 addr->address = i;
2210 u132_udev_get_kref(u132, udev);
2211 break;
2214 if (I == 0) {
2215 dev_err(&u132->platform_dev->dev, "run out of d"
2216 "evice space\n");
2217 return -EINVAL;
2219 urb->hcpriv = u132;
2220 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2221 endp->urb_list[ENDP_QUEUE_MASK &
2222 endp->queue_last++] = urb;
2223 } else {
2224 struct u132_urbq *urbq =
2225 kmalloc(sizeof(struct u132_urbq),
2226 GFP_ATOMIC);
2227 if (urbq == NULL) {
2228 endp->queue_size -= 1;
2229 return -ENOMEM;
2230 } else {
2231 list_add_tail(&urbq->urb_more,
2232 &endp->urb_more);
2233 urbq->urb = urb;
2236 return 0;
2238 } else { /*(usb_addr > 0) */
2239 u8 address = u132->addr[usb_addr].address;
2240 struct u132_udev *udev = &u132->udev[address];
2241 urb->hcpriv = u132;
2242 if (udev->enumeration == 2) {
2243 } else
2244 udev->enumeration = 2;
2245 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2246 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
2247 urb;
2248 } else {
2249 struct u132_urbq *urbq =
2250 kmalloc(sizeof(struct u132_urbq), GFP_ATOMIC);
2251 if (urbq == NULL) {
2252 endp->queue_size -= 1;
2253 return -ENOMEM;
2254 } else {
2255 list_add_tail(&urbq->urb_more, &endp->urb_more);
2256 urbq->urb = urb;
2259 return 0;
2263 static int u132_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
2264 gfp_t mem_flags)
2266 struct u132 *u132 = hcd_to_u132(hcd);
2267 if (irqs_disabled()) {
2268 if (__GFP_WAIT & mem_flags) {
2269 printk(KERN_ERR "invalid context for function that migh"
2270 "t sleep\n");
2271 return -EINVAL;
2274 if (u132->going > 1) {
2275 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2276 , u132->going);
2277 return -ENODEV;
2278 } else if (u132->going > 0) {
2279 dev_err(&u132->platform_dev->dev, "device is being removed "
2280 "urb=%p\n", urb);
2281 return -ESHUTDOWN;
2282 } else {
2283 u8 usb_addr = usb_pipedevice(urb->pipe);
2284 u8 usb_endp = usb_pipeendpoint(urb->pipe);
2285 struct usb_device *usb_dev = urb->dev;
2286 if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2287 u8 address = u132->addr[usb_addr].address;
2288 struct u132_udev *udev = &u132->udev[address];
2289 struct u132_endp *endp = urb->ep->hcpriv;
2290 urb->actual_length = 0;
2291 if (endp) {
2292 unsigned long irqs;
2293 int retval;
2294 spin_lock_irqsave(&endp->queue_lock.slock,
2295 irqs);
2296 retval = usb_hcd_link_urb_to_ep(hcd, urb);
2297 if (retval == 0) {
2298 retval = queue_int_on_old_endpoint(
2299 u132, udev, urb,
2300 usb_dev, endp,
2301 usb_addr, usb_endp,
2302 address);
2303 if (retval)
2304 usb_hcd_unlink_urb_from_ep(
2305 hcd, urb);
2307 spin_unlock_irqrestore(&endp->queue_lock.slock,
2308 irqs);
2309 if (retval) {
2310 return retval;
2311 } else {
2312 u132_endp_queue_work(u132, endp,
2313 msecs_to_jiffies(urb->interval))
2315 return 0;
2317 } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2318 return -EINVAL;
2319 } else { /*(endp == NULL) */
2320 return create_endpoint_and_queue_int(u132, udev,
2321 urb, usb_dev, usb_addr,
2322 usb_endp, address, mem_flags);
2324 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2325 dev_err(&u132->platform_dev->dev, "the hardware does no"
2326 "t support PIPE_ISOCHRONOUS\n");
2327 return -EINVAL;
2328 } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
2329 u8 address = u132->addr[usb_addr].address;
2330 struct u132_udev *udev = &u132->udev[address];
2331 struct u132_endp *endp = urb->ep->hcpriv;
2332 urb->actual_length = 0;
2333 if (endp) {
2334 unsigned long irqs;
2335 int retval;
2336 spin_lock_irqsave(&endp->queue_lock.slock,
2337 irqs);
2338 retval = usb_hcd_link_urb_to_ep(hcd, urb);
2339 if (retval == 0) {
2340 retval = queue_bulk_on_old_endpoint(
2341 u132, udev, urb,
2342 usb_dev, endp,
2343 usb_addr, usb_endp,
2344 address);
2345 if (retval)
2346 usb_hcd_unlink_urb_from_ep(
2347 hcd, urb);
2349 spin_unlock_irqrestore(&endp->queue_lock.slock,
2350 irqs);
2351 if (retval) {
2352 return retval;
2353 } else {
2354 u132_endp_queue_work(u132, endp, 0);
2355 return 0;
2357 } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2358 return -EINVAL;
2359 } else
2360 return create_endpoint_and_queue_bulk(u132,
2361 udev, urb, usb_dev, usb_addr,
2362 usb_endp, address, mem_flags);
2363 } else {
2364 struct u132_endp *endp = urb->ep->hcpriv;
2365 u16 urb_size = 8;
2366 u8 *b = urb->setup_packet;
2367 int i = 0;
2368 char data[30 *3 + 4];
2369 char *d = data;
2370 int m = (sizeof(data) - 1) / 3;
2371 int l = 0;
2372 data[0] = 0;
2373 while (urb_size-- > 0) {
2374 if (i > m) {
2375 } else if (i++ < m) {
2376 int w = sprintf(d, " %02X", *b++);
2377 d += w;
2378 l += w;
2379 } else
2380 d += sprintf(d, " ..");
2382 if (endp) {
2383 unsigned long irqs;
2384 int retval;
2385 spin_lock_irqsave(&endp->queue_lock.slock,
2386 irqs);
2387 retval = usb_hcd_link_urb_to_ep(hcd, urb);
2388 if (retval == 0) {
2389 retval = queue_control_on_old_endpoint(
2390 u132, urb, usb_dev,
2391 endp, usb_addr,
2392 usb_endp);
2393 if (retval)
2394 usb_hcd_unlink_urb_from_ep(
2395 hcd, urb);
2397 spin_unlock_irqrestore(&endp->queue_lock.slock,
2398 irqs);
2399 if (retval) {
2400 return retval;
2401 } else {
2402 u132_endp_queue_work(u132, endp, 0);
2403 return 0;
2405 } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2406 return -EINVAL;
2407 } else
2408 return create_endpoint_and_queue_control(u132,
2409 urb, usb_dev, usb_addr, usb_endp,
2410 mem_flags);
2415 static int dequeue_from_overflow_chain(struct u132 *u132,
2416 struct u132_endp *endp, struct urb *urb)
2418 struct list_head *scan;
2419 struct list_head *head = &endp->urb_more;
2420 list_for_each(scan, head) {
2421 struct u132_urbq *urbq = list_entry(scan, struct u132_urbq,
2422 urb_more);
2423 if (urbq->urb == urb) {
2424 struct usb_hcd *hcd = u132_to_hcd(u132);
2425 list_del(scan);
2426 endp->queue_size -= 1;
2427 urb->error_count = 0;
2428 usb_hcd_giveback_urb(hcd, urb, 0);
2429 return 0;
2430 } else
2431 continue;
2433 dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]=%p ring"
2434 "[%d] %c%c usb_endp=%d usb_addr=%d size=%d next=%04X last=%04X"
2435 "\n", urb, endp->endp_number, endp, endp->ring->number,
2436 endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2437 endp->usb_endp, endp->usb_addr, endp->queue_size,
2438 endp->queue_next, endp->queue_last);
2439 return -EINVAL;
2442 static int u132_endp_urb_dequeue(struct u132 *u132, struct u132_endp *endp,
2443 struct urb *urb, int status)
2445 unsigned long irqs;
2446 int rc;
2448 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2449 rc = usb_hcd_check_unlink_urb(u132_to_hcd(u132), urb, status);
2450 if (rc) {
2451 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2452 return rc;
2454 if (endp->queue_size == 0) {
2455 dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]"
2456 "=%p ring[%d] %c%c usb_endp=%d usb_addr=%d\n", urb,
2457 endp->endp_number, endp, endp->ring->number,
2458 endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2459 endp->usb_endp, endp->usb_addr);
2460 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2461 return -EINVAL;
2463 if (urb == endp->urb_list[ENDP_QUEUE_MASK & endp->queue_next]) {
2464 if (endp->active) {
2465 endp->dequeueing = 1;
2466 endp->edset_flush = 1;
2467 u132_endp_queue_work(u132, endp, 0);
2468 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2469 return 0;
2470 } else {
2471 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2472 u132_hcd_abandon_urb(u132, endp, urb, status);
2473 return 0;
2475 } else {
2476 u16 queue_list = 0;
2477 u16 queue_size = endp->queue_size;
2478 u16 queue_scan = endp->queue_next;
2479 struct urb **urb_slot = NULL;
2480 while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2481 if (urb == endp->urb_list[ENDP_QUEUE_MASK &
2482 ++queue_scan]) {
2483 urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2484 queue_scan];
2485 break;
2486 } else
2487 continue;
2489 while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2490 *urb_slot = endp->urb_list[ENDP_QUEUE_MASK &
2491 ++queue_scan];
2492 urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2493 queue_scan];
2495 if (urb_slot) {
2496 struct usb_hcd *hcd = u132_to_hcd(u132);
2498 usb_hcd_unlink_urb_from_ep(hcd, urb);
2499 endp->queue_size -= 1;
2500 if (list_empty(&endp->urb_more)) {
2501 spin_unlock_irqrestore(&endp->queue_lock.slock,
2502 irqs);
2503 } else {
2504 struct list_head *next = endp->urb_more.next;
2505 struct u132_urbq *urbq = list_entry(next,
2506 struct u132_urbq, urb_more);
2507 list_del(next);
2508 *urb_slot = urbq->urb;
2509 spin_unlock_irqrestore(&endp->queue_lock.slock,
2510 irqs);
2511 kfree(urbq);
2512 } urb->error_count = 0;
2513 usb_hcd_giveback_urb(hcd, urb, status);
2514 return 0;
2515 } else if (list_empty(&endp->urb_more)) {
2516 dev_err(&u132->platform_dev->dev, "urb=%p not found in "
2517 "endp[%d]=%p ring[%d] %c%c usb_endp=%d usb_addr"
2518 "=%d size=%d next=%04X last=%04X\n", urb,
2519 endp->endp_number, endp, endp->ring->number,
2520 endp->input ? 'I' : ' ',
2521 endp->output ? 'O' : ' ', endp->usb_endp,
2522 endp->usb_addr, endp->queue_size,
2523 endp->queue_next, endp->queue_last);
2524 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2525 return -EINVAL;
2526 } else {
2527 int retval;
2529 usb_hcd_unlink_urb_from_ep(u132_to_hcd(u132), urb);
2530 retval = dequeue_from_overflow_chain(u132, endp,
2531 urb);
2532 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2533 return retval;
2538 static int u132_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2540 struct u132 *u132 = hcd_to_u132(hcd);
2541 if (u132->going > 2) {
2542 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2543 , u132->going);
2544 return -ENODEV;
2545 } else {
2546 u8 usb_addr = usb_pipedevice(urb->pipe);
2547 u8 usb_endp = usb_pipeendpoint(urb->pipe);
2548 u8 address = u132->addr[usb_addr].address;
2549 struct u132_udev *udev = &u132->udev[address];
2550 if (usb_pipein(urb->pipe)) {
2551 u8 endp_number = udev->endp_number_in[usb_endp];
2552 struct u132_endp *endp = u132->endp[endp_number - 1];
2553 return u132_endp_urb_dequeue(u132, endp, urb, status);
2554 } else {
2555 u8 endp_number = udev->endp_number_out[usb_endp];
2556 struct u132_endp *endp = u132->endp[endp_number - 1];
2557 return u132_endp_urb_dequeue(u132, endp, urb, status);
2562 static void u132_endpoint_disable(struct usb_hcd *hcd,
2563 struct usb_host_endpoint *hep)
2565 struct u132 *u132 = hcd_to_u132(hcd);
2566 if (u132->going > 2) {
2567 dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p hep=%p"
2568 ") has been removed %d\n", u132, hcd, hep,
2569 u132->going);
2570 } else {
2571 struct u132_endp *endp = hep->hcpriv;
2572 if (endp)
2573 u132_endp_put_kref(u132, endp);
2577 static int u132_get_frame(struct usb_hcd *hcd)
2579 struct u132 *u132 = hcd_to_u132(hcd);
2580 if (u132->going > 1) {
2581 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2582 , u132->going);
2583 return -ENODEV;
2584 } else if (u132->going > 0) {
2585 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2586 return -ESHUTDOWN;
2587 } else {
2588 int frame = 0;
2589 dev_err(&u132->platform_dev->dev, "TODO: u132_get_frame\n");
2590 msleep(100);
2591 return frame;
2595 static int u132_roothub_descriptor(struct u132 *u132,
2596 struct usb_hub_descriptor *desc)
2598 int retval;
2599 u16 temp;
2600 u32 rh_a = -1;
2601 u32 rh_b = -1;
2602 retval = u132_read_pcimem(u132, roothub.a, &rh_a);
2603 if (retval)
2604 return retval;
2605 desc->bDescriptorType = 0x29;
2606 desc->bPwrOn2PwrGood = (rh_a & RH_A_POTPGT) >> 24;
2607 desc->bHubContrCurrent = 0;
2608 desc->bNbrPorts = u132->num_ports;
2609 temp = 1 + (u132->num_ports / 8);
2610 desc->bDescLength = 7 + 2 *temp;
2611 temp = 0;
2612 if (rh_a & RH_A_NPS)
2613 temp |= 0x0002;
2614 if (rh_a & RH_A_PSM)
2615 temp |= 0x0001;
2616 if (rh_a & RH_A_NOCP) {
2617 temp |= 0x0010;
2618 } else if (rh_a & RH_A_OCPM)
2619 temp |= 0x0008;
2620 desc->wHubCharacteristics = cpu_to_le16(temp);
2621 retval = u132_read_pcimem(u132, roothub.b, &rh_b);
2622 if (retval)
2623 return retval;
2624 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
2625 desc->bitmap[0] = rh_b & RH_B_DR;
2626 if (u132->num_ports > 7) {
2627 desc->bitmap[1] = (rh_b & RH_B_DR) >> 8;
2628 desc->bitmap[2] = 0xff;
2629 } else
2630 desc->bitmap[1] = 0xff;
2631 return 0;
2634 static int u132_roothub_status(struct u132 *u132, __le32 *desc)
2636 u32 rh_status = -1;
2637 int ret_status = u132_read_pcimem(u132, roothub.status, &rh_status);
2638 *desc = cpu_to_le32(rh_status);
2639 return ret_status;
2642 static int u132_roothub_portstatus(struct u132 *u132, __le32 *desc, u16 wIndex)
2644 if (wIndex == 0 || wIndex > u132->num_ports) {
2645 return -EINVAL;
2646 } else {
2647 int port = wIndex - 1;
2648 u32 rh_portstatus = -1;
2649 int ret_portstatus = u132_read_pcimem(u132,
2650 roothub.portstatus[port], &rh_portstatus);
2651 *desc = cpu_to_le32(rh_portstatus);
2652 if (*(u16 *) (desc + 2)) {
2653 dev_info(&u132->platform_dev->dev, "Port %d Status Chan"
2654 "ge = %08X\n", port, *desc);
2656 return ret_portstatus;
2661 /* this timer value might be vendor-specific ... */
2662 #define PORT_RESET_HW_MSEC 10
2663 #define PORT_RESET_MSEC 10
2664 /* wrap-aware logic morphed from <linux/jiffies.h> */
2665 #define tick_before(t1, t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
2666 static int u132_roothub_portreset(struct u132 *u132, int port_index)
2668 int retval;
2669 u32 fmnumber;
2670 u16 now;
2671 u16 reset_done;
2672 retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2673 if (retval)
2674 return retval;
2675 now = fmnumber;
2676 reset_done = now + PORT_RESET_MSEC;
2677 do {
2678 u32 portstat;
2679 do {
2680 retval = u132_read_pcimem(u132,
2681 roothub.portstatus[port_index], &portstat);
2682 if (retval)
2683 return retval;
2684 if (RH_PS_PRS & portstat) {
2685 continue;
2686 } else
2687 break;
2688 } while (tick_before(now, reset_done));
2689 if (RH_PS_PRS & portstat)
2690 return -ENODEV;
2691 if (RH_PS_CCS & portstat) {
2692 if (RH_PS_PRSC & portstat) {
2693 retval = u132_write_pcimem(u132,
2694 roothub.portstatus[port_index],
2695 RH_PS_PRSC);
2696 if (retval)
2697 return retval;
2699 } else
2700 break; /* start the next reset,
2701 sleep till it's probably done */
2702 retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2703 RH_PS_PRS);
2704 if (retval)
2705 return retval;
2706 msleep(PORT_RESET_HW_MSEC);
2707 retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2708 if (retval)
2709 return retval;
2710 now = fmnumber;
2711 } while (tick_before(now, reset_done));
2712 return 0;
2715 static int u132_roothub_setportfeature(struct u132 *u132, u16 wValue,
2716 u16 wIndex)
2718 if (wIndex == 0 || wIndex > u132->num_ports) {
2719 return -EINVAL;
2720 } else {
2721 int retval;
2722 int port_index = wIndex - 1;
2723 struct u132_port *port = &u132->port[port_index];
2724 port->Status &= ~(1 << wValue);
2725 switch (wValue) {
2726 case USB_PORT_FEAT_SUSPEND:
2727 retval = u132_write_pcimem(u132,
2728 roothub.portstatus[port_index], RH_PS_PSS);
2729 if (retval)
2730 return retval;
2731 return 0;
2732 case USB_PORT_FEAT_POWER:
2733 retval = u132_write_pcimem(u132,
2734 roothub.portstatus[port_index], RH_PS_PPS);
2735 if (retval)
2736 return retval;
2737 return 0;
2738 case USB_PORT_FEAT_RESET:
2739 retval = u132_roothub_portreset(u132, port_index);
2740 if (retval)
2741 return retval;
2742 return 0;
2743 default:
2744 return -EPIPE;
2749 static int u132_roothub_clearportfeature(struct u132 *u132, u16 wValue,
2750 u16 wIndex)
2752 if (wIndex == 0 || wIndex > u132->num_ports) {
2753 return -EINVAL;
2754 } else {
2755 int port_index = wIndex - 1;
2756 u32 temp;
2757 int retval;
2758 struct u132_port *port = &u132->port[port_index];
2759 port->Status &= ~(1 << wValue);
2760 switch (wValue) {
2761 case USB_PORT_FEAT_ENABLE:
2762 temp = RH_PS_CCS;
2763 break;
2764 case USB_PORT_FEAT_C_ENABLE:
2765 temp = RH_PS_PESC;
2766 break;
2767 case USB_PORT_FEAT_SUSPEND:
2768 temp = RH_PS_POCI;
2769 if ((u132->hc_control & OHCI_CTRL_HCFS)
2770 != OHCI_USB_OPER) {
2771 dev_err(&u132->platform_dev->dev, "TODO resume_"
2772 "root_hub\n");
2774 break;
2775 case USB_PORT_FEAT_C_SUSPEND:
2776 temp = RH_PS_PSSC;
2777 break;
2778 case USB_PORT_FEAT_POWER:
2779 temp = RH_PS_LSDA;
2780 break;
2781 case USB_PORT_FEAT_C_CONNECTION:
2782 temp = RH_PS_CSC;
2783 break;
2784 case USB_PORT_FEAT_C_OVER_CURRENT:
2785 temp = RH_PS_OCIC;
2786 break;
2787 case USB_PORT_FEAT_C_RESET:
2788 temp = RH_PS_PRSC;
2789 break;
2790 default:
2791 return -EPIPE;
2793 retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2794 temp);
2795 if (retval)
2796 return retval;
2797 return 0;
2802 /* the virtual root hub timer IRQ checks for hub status*/
2803 static int u132_hub_status_data(struct usb_hcd *hcd, char *buf)
2805 struct u132 *u132 = hcd_to_u132(hcd);
2806 if (u132->going > 1) {
2807 dev_err(&u132->platform_dev->dev, "device hcd=%p has been remov"
2808 "ed %d\n", hcd, u132->going);
2809 return -ENODEV;
2810 } else if (u132->going > 0) {
2811 dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
2812 "ed\n", hcd);
2813 return -ESHUTDOWN;
2814 } else {
2815 int i, changed = 0, length = 1;
2816 if (u132->flags & OHCI_QUIRK_AMD756) {
2817 if ((u132->hc_roothub_a & RH_A_NDP) > MAX_ROOT_PORTS) {
2818 dev_err(&u132->platform_dev->dev, "bogus NDP, r"
2819 "ereads as NDP=%d\n",
2820 u132->hc_roothub_a & RH_A_NDP);
2821 goto done;
2824 if (u132->hc_roothub_status & (RH_HS_LPSC | RH_HS_OCIC)) {
2825 buf[0] = changed = 1;
2826 } else
2827 buf[0] = 0;
2828 if (u132->num_ports > 7) {
2829 buf[1] = 0;
2830 length++;
2832 for (i = 0; i < u132->num_ports; i++) {
2833 if (u132->hc_roothub_portstatus[i] & (RH_PS_CSC |
2834 RH_PS_PESC | RH_PS_PSSC | RH_PS_OCIC |
2835 RH_PS_PRSC)) {
2836 changed = 1;
2837 if (i < 7) {
2838 buf[0] |= 1 << (i + 1);
2839 } else
2840 buf[1] |= 1 << (i - 7);
2841 continue;
2843 if (!(u132->hc_roothub_portstatus[i] & RH_PS_CCS)) {
2844 continue;
2846 if ((u132->hc_roothub_portstatus[i] & RH_PS_PSS)) {
2847 continue;
2850 done:return changed ? length : 0;
2854 static int u132_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2855 u16 wIndex, char *buf, u16 wLength)
2857 struct u132 *u132 = hcd_to_u132(hcd);
2858 if (u132->going > 1) {
2859 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2860 , u132->going);
2861 return -ENODEV;
2862 } else if (u132->going > 0) {
2863 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2864 return -ESHUTDOWN;
2865 } else {
2866 int retval = 0;
2867 down(&u132->sw_lock);
2868 switch (typeReq) {
2869 case ClearHubFeature:
2870 switch (wValue) {
2871 case C_HUB_OVER_CURRENT:
2872 case C_HUB_LOCAL_POWER:
2873 break;
2874 default:
2875 goto stall;
2877 break;
2878 case SetHubFeature:
2879 switch (wValue) {
2880 case C_HUB_OVER_CURRENT:
2881 case C_HUB_LOCAL_POWER:
2882 break;
2883 default:
2884 goto stall;
2886 break;
2887 case ClearPortFeature:{
2888 retval = u132_roothub_clearportfeature(u132,
2889 wValue, wIndex);
2890 if (retval)
2891 goto error;
2892 break;
2894 case GetHubDescriptor:{
2895 retval = u132_roothub_descriptor(u132,
2896 (struct usb_hub_descriptor *)buf);
2897 if (retval)
2898 goto error;
2899 break;
2901 case GetHubStatus:{
2902 retval = u132_roothub_status(u132,
2903 (__le32 *) buf);
2904 if (retval)
2905 goto error;
2906 break;
2908 case GetPortStatus:{
2909 retval = u132_roothub_portstatus(u132,
2910 (__le32 *) buf, wIndex);
2911 if (retval)
2912 goto error;
2913 break;
2915 case SetPortFeature:{
2916 retval = u132_roothub_setportfeature(u132,
2917 wValue, wIndex);
2918 if (retval)
2919 goto error;
2920 break;
2922 default:
2923 goto stall;
2924 error:u132_disable(u132);
2925 u132->going = 1;
2926 break;
2927 stall:retval = -EPIPE;
2928 break;
2930 up(&u132->sw_lock);
2931 return retval;
2935 static int u132_start_port_reset(struct usb_hcd *hcd, unsigned port_num)
2937 struct u132 *u132 = hcd_to_u132(hcd);
2938 if (u132->going > 1) {
2939 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2940 , u132->going);
2941 return -ENODEV;
2942 } else if (u132->going > 0) {
2943 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2944 return -ESHUTDOWN;
2945 } else
2946 return 0;
2949 static void u132_hub_irq_enable(struct usb_hcd *hcd)
2951 struct u132 *u132 = hcd_to_u132(hcd);
2952 if (u132->going > 1) {
2953 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2954 , u132->going);
2955 } else if (u132->going > 0)
2956 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2960 #ifdef CONFIG_PM
2961 static int u132_bus_suspend(struct usb_hcd *hcd)
2963 struct u132 *u132 = hcd_to_u132(hcd);
2964 if (u132->going > 1) {
2965 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2966 , u132->going);
2967 return -ENODEV;
2968 } else if (u132->going > 0) {
2969 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2970 return -ESHUTDOWN;
2971 } else
2972 return 0;
2975 static int u132_bus_resume(struct usb_hcd *hcd)
2977 struct u132 *u132 = hcd_to_u132(hcd);
2978 if (u132->going > 1) {
2979 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2980 , u132->going);
2981 return -ENODEV;
2982 } else if (u132->going > 0) {
2983 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2984 return -ESHUTDOWN;
2985 } else
2986 return 0;
2989 #else
2990 #define u132_bus_suspend NULL
2991 #define u132_bus_resume NULL
2992 #endif
2993 static struct hc_driver u132_hc_driver = {
2994 .description = hcd_name,
2995 .hcd_priv_size = sizeof(struct u132),
2996 .irq = NULL,
2997 .flags = HCD_USB11 | HCD_MEMORY,
2998 .reset = u132_hcd_reset,
2999 .start = u132_hcd_start,
3000 .stop = u132_hcd_stop,
3001 .urb_enqueue = u132_urb_enqueue,
3002 .urb_dequeue = u132_urb_dequeue,
3003 .endpoint_disable = u132_endpoint_disable,
3004 .get_frame_number = u132_get_frame,
3005 .hub_status_data = u132_hub_status_data,
3006 .hub_control = u132_hub_control,
3007 .bus_suspend = u132_bus_suspend,
3008 .bus_resume = u132_bus_resume,
3009 .start_port_reset = u132_start_port_reset,
3010 .hub_irq_enable = u132_hub_irq_enable,
3014 * This function may be called by the USB core whilst the "usb_all_devices_rwsem"
3015 * is held for writing, thus this module must not call usb_remove_hcd()
3016 * synchronously - but instead should immediately stop activity to the
3017 * device and asynchronously call usb_remove_hcd()
3019 static int __devexit u132_remove(struct platform_device *pdev)
3021 struct usb_hcd *hcd = platform_get_drvdata(pdev);
3022 if (hcd) {
3023 struct u132 *u132 = hcd_to_u132(hcd);
3024 if (u132->going++ > 1) {
3025 dev_err(&u132->platform_dev->dev, "already being remove"
3026 "d\n");
3027 return -ENODEV;
3028 } else {
3029 int rings = MAX_U132_RINGS;
3030 int endps = MAX_U132_ENDPS;
3031 dev_err(&u132->platform_dev->dev, "removing device u132"
3032 ".%d\n", u132->sequence_num);
3033 msleep(100);
3034 down(&u132->sw_lock);
3035 u132_monitor_cancel_work(u132);
3036 while (rings-- > 0) {
3037 struct u132_ring *ring = &u132->ring[rings];
3038 u132_ring_cancel_work(u132, ring);
3039 } while (endps-- > 0) {
3040 struct u132_endp *endp = u132->endp[endps];
3041 if (endp)
3042 u132_endp_cancel_work(u132, endp);
3044 u132->going += 1;
3045 printk(KERN_INFO "removing device u132.%d\n",
3046 u132->sequence_num);
3047 up(&u132->sw_lock);
3048 usb_remove_hcd(hcd);
3049 u132_u132_put_kref(u132);
3050 return 0;
3052 } else
3053 return 0;
3056 static void u132_initialise(struct u132 *u132, struct platform_device *pdev)
3058 int rings = MAX_U132_RINGS;
3059 int ports = MAX_U132_PORTS;
3060 int addrs = MAX_U132_ADDRS;
3061 int udevs = MAX_U132_UDEVS;
3062 int endps = MAX_U132_ENDPS;
3063 u132->board = pdev->dev.platform_data;
3064 u132->platform_dev = pdev;
3065 u132->power = 0;
3066 u132->reset = 0;
3067 init_MUTEX(&u132->sw_lock);
3068 init_MUTEX(&u132->scheduler_lock);
3069 while (rings-- > 0) {
3070 struct u132_ring *ring = &u132->ring[rings];
3071 ring->u132 = u132;
3072 ring->number = rings + 1;
3073 ring->length = 0;
3074 ring->curr_endp = NULL;
3075 INIT_DELAYED_WORK(&ring->scheduler,
3076 u132_hcd_ring_work_scheduler);
3077 } down(&u132->sw_lock);
3078 INIT_DELAYED_WORK(&u132->monitor, u132_hcd_monitor_work);
3079 while (ports-- > 0) {
3080 struct u132_port *port = &u132->port[ports];
3081 port->u132 = u132;
3082 port->reset = 0;
3083 port->enable = 0;
3084 port->power = 0;
3085 port->Status = 0;
3086 } while (addrs-- > 0) {
3087 struct u132_addr *addr = &u132->addr[addrs];
3088 addr->address = 0;
3089 } while (udevs-- > 0) {
3090 struct u132_udev *udev = &u132->udev[udevs];
3091 int i = ARRAY_SIZE(udev->endp_number_in);
3092 int o = ARRAY_SIZE(udev->endp_number_out);
3093 udev->usb_device = NULL;
3094 udev->udev_number = 0;
3095 udev->usb_addr = 0;
3096 udev->portnumber = 0;
3097 while (i-- > 0) {
3098 udev->endp_number_in[i] = 0;
3100 while (o-- > 0) {
3101 udev->endp_number_out[o] = 0;
3104 while (endps-- > 0) {
3105 u132->endp[endps] = NULL;
3107 up(&u132->sw_lock);
3108 return;
3111 static int __devinit u132_probe(struct platform_device *pdev)
3113 struct usb_hcd *hcd;
3114 int retval;
3115 u32 control;
3116 u32 rh_a = -1;
3117 u32 num_ports;
3118 msleep(100);
3119 if (u132_exiting > 0) {
3120 return -ENODEV;
3122 retval = ftdi_write_pcimem(pdev, intrdisable, OHCI_INTR_MIE);
3123 if (retval)
3124 return retval;
3125 retval = ftdi_read_pcimem(pdev, control, &control);
3126 if (retval)
3127 return retval;
3128 retval = ftdi_read_pcimem(pdev, roothub.a, &rh_a);
3129 if (retval)
3130 return retval;
3131 num_ports = rh_a & RH_A_NDP; /* refuse to confuse usbcore */
3132 if (pdev->dev.dma_mask) {
3133 return -EINVAL;
3135 hcd = usb_create_hcd(&u132_hc_driver, &pdev->dev, pdev->dev.bus_id);
3136 if (!hcd) {
3137 printk(KERN_ERR "failed to create the usb hcd struct for U132\n"
3139 ftdi_elan_gone_away(pdev);
3140 return -ENOMEM;
3141 } else {
3142 int retval = 0;
3143 struct u132 *u132 = hcd_to_u132(hcd);
3144 hcd->rsrc_start = 0;
3145 mutex_lock(&u132_module_lock);
3146 list_add_tail(&u132->u132_list, &u132_static_list);
3147 u132->sequence_num = ++u132_instances;
3148 mutex_unlock(&u132_module_lock);
3149 u132_u132_init_kref(u132);
3150 u132_initialise(u132, pdev);
3151 hcd->product_desc = "ELAN U132 Host Controller";
3152 retval = usb_add_hcd(hcd, 0, 0);
3153 if (retval != 0) {
3154 dev_err(&u132->platform_dev->dev, "init error %d\n",
3155 retval);
3156 u132_u132_put_kref(u132);
3157 return retval;
3158 } else {
3159 u132_monitor_queue_work(u132, 100);
3160 return 0;
3166 #ifdef CONFIG_PM
3167 /* for this device there's no useful distinction between the controller
3168 * and its root hub, except that the root hub only gets direct PM calls
3169 * when CONFIG_USB_SUSPEND is enabled.
3171 static int u132_suspend(struct platform_device *pdev, pm_message_t state)
3173 struct usb_hcd *hcd = platform_get_drvdata(pdev);
3174 struct u132 *u132 = hcd_to_u132(hcd);
3175 if (u132->going > 1) {
3176 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3177 , u132->going);
3178 return -ENODEV;
3179 } else if (u132->going > 0) {
3180 dev_err(&u132->platform_dev->dev, "device is being removed\n");
3181 return -ESHUTDOWN;
3182 } else {
3183 int retval = 0;
3184 if (state.event == PM_EVENT_FREEZE) {
3185 retval = u132_bus_suspend(hcd);
3186 } else if (state.event == PM_EVENT_SUSPEND) {
3187 int ports = MAX_U132_PORTS;
3188 while (ports-- > 0) {
3189 port_power(u132, ports, 0);
3192 return retval;
3196 static int u132_resume(struct platform_device *pdev)
3198 struct usb_hcd *hcd = platform_get_drvdata(pdev);
3199 struct u132 *u132 = hcd_to_u132(hcd);
3200 if (u132->going > 1) {
3201 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3202 , u132->going);
3203 return -ENODEV;
3204 } else if (u132->going > 0) {
3205 dev_err(&u132->platform_dev->dev, "device is being removed\n");
3206 return -ESHUTDOWN;
3207 } else {
3208 int retval = 0;
3209 if (!u132->port[0].power) {
3210 int ports = MAX_U132_PORTS;
3211 while (ports-- > 0) {
3212 port_power(u132, ports, 1);
3214 retval = 0;
3215 } else {
3216 retval = u132_bus_resume(hcd);
3218 return retval;
3222 #else
3223 #define u132_suspend NULL
3224 #define u132_resume NULL
3225 #endif
3227 * this driver is loaded explicitly by ftdi_u132
3229 * the platform_driver struct is static because it is per type of module
3231 static struct platform_driver u132_platform_driver = {
3232 .probe = u132_probe,
3233 .remove = __devexit_p(u132_remove),
3234 .suspend = u132_suspend,
3235 .resume = u132_resume,
3236 .driver = {
3237 .name = (char *)hcd_name,
3238 .owner = THIS_MODULE,
3241 static int __init u132_hcd_init(void)
3243 int retval;
3244 INIT_LIST_HEAD(&u132_static_list);
3245 u132_instances = 0;
3246 u132_exiting = 0;
3247 mutex_init(&u132_module_lock);
3248 if (usb_disabled())
3249 return -ENODEV;
3250 printk(KERN_INFO "driver %s built at %s on %s\n", hcd_name, __TIME__,
3251 __DATE__);
3252 workqueue = create_singlethread_workqueue("u132");
3253 retval = platform_driver_register(&u132_platform_driver);
3254 return retval;
3258 module_init(u132_hcd_init);
3259 static void __exit u132_hcd_exit(void)
3261 struct u132 *u132;
3262 struct u132 *temp;
3263 mutex_lock(&u132_module_lock);
3264 u132_exiting += 1;
3265 mutex_unlock(&u132_module_lock);
3266 list_for_each_entry_safe(u132, temp, &u132_static_list, u132_list) {
3267 platform_device_unregister(u132->platform_dev);
3268 } platform_driver_unregister(&u132_platform_driver);
3269 printk(KERN_INFO "u132-hcd driver deregistered\n");
3270 wait_event(u132_hcd_wait, u132_instances == 0);
3271 flush_workqueue(workqueue);
3272 destroy_workqueue(workqueue);
3276 module_exit(u132_hcd_exit);
3277 MODULE_LICENSE("GPL");