header cleaning: don't include smp_lock.h when not used
[usb.git] / drivers / usb / host / u132-hcd.c
blobff0dba01f1c7d2129a9830cae777fa3e1f85b938
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 <asm/io.h>
56 #include <asm/irq.h>
57 #include <asm/system.h>
58 #include <asm/byteorder.h>
59 #include "../core/hcd.h"
60 #include "ohci.h"
61 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
62 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
63 OHCI_INTR_WDH)
64 MODULE_AUTHOR("Tony Olech - Elan Digital Systems Limited");
65 MODULE_DESCRIPTION("U132 USB Host Controller Driver");
66 MODULE_LICENSE("GPL");
67 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
68 INT_MODULE_PARM(testing, 0);
69 /* Some boards misreport power switching/overcurrent*/
70 static int distrust_firmware = 1;
71 module_param(distrust_firmware, bool, 0);
72 MODULE_PARM_DESC(distrust_firmware, "true to distrust firmware power/overcurren"
73 "t setup");
74 static DECLARE_WAIT_QUEUE_HEAD(u132_hcd_wait);
76 * u132_module_lock exists to protect access to global variables
79 static struct semaphore u132_module_lock;
80 static int u132_exiting = 0;
81 static int u132_instances = 0;
82 static struct list_head u132_static_list;
84 * end of the global variables protected by u132_module_lock
86 static struct workqueue_struct *workqueue;
87 #define MAX_U132_PORTS 7
88 #define MAX_U132_ADDRS 128
89 #define MAX_U132_UDEVS 4
90 #define MAX_U132_ENDPS 100
91 #define MAX_U132_RINGS 4
92 static const char *cc_to_text[16] = {
93 "No Error ",
94 "CRC Error ",
95 "Bit Stuff ",
96 "Data Togg ",
97 "Stall ",
98 "DevNotResp ",
99 "PIDCheck ",
100 "UnExpPID ",
101 "DataOver ",
102 "DataUnder ",
103 "(for hw) ",
104 "(for hw) ",
105 "BufferOver ",
106 "BuffUnder ",
107 "(for HCD) ",
108 "(for HCD) "
110 struct u132_port {
111 struct u132 *u132;
112 int reset;
113 int enable;
114 int power;
115 int Status;
117 struct u132_addr {
118 u8 address;
120 struct u132_udev {
121 struct kref kref;
122 struct usb_device *usb_device;
123 u8 enumeration;
124 u8 udev_number;
125 u8 usb_addr;
126 u8 portnumber;
127 u8 endp_number_in[16];
128 u8 endp_number_out[16];
130 #define ENDP_QUEUE_SHIFT 3
131 #define ENDP_QUEUE_SIZE (1<<ENDP_QUEUE_SHIFT)
132 #define ENDP_QUEUE_MASK (ENDP_QUEUE_SIZE-1)
133 struct u132_urbq {
134 struct list_head urb_more;
135 struct urb *urb;
137 struct u132_spin {
138 spinlock_t slock;
140 struct u132_endp {
141 struct kref kref;
142 u8 udev_number;
143 u8 endp_number;
144 u8 usb_addr;
145 u8 usb_endp;
146 struct u132 *u132;
147 struct list_head endp_ring;
148 struct u132_ring *ring;
149 unsigned toggle_bits:2;
150 unsigned active:1;
151 unsigned delayed:1;
152 unsigned input:1;
153 unsigned output:1;
154 unsigned pipetype:2;
155 unsigned dequeueing:1;
156 unsigned edset_flush:1;
157 unsigned spare_bits:14;
158 unsigned long jiffies;
159 struct usb_host_endpoint *hep;
160 struct u132_spin queue_lock;
161 u16 queue_size;
162 u16 queue_last;
163 u16 queue_next;
164 struct urb *urb_list[ENDP_QUEUE_SIZE];
165 struct list_head urb_more;
166 struct delayed_work scheduler;
168 struct u132_ring {
169 unsigned in_use:1;
170 unsigned length:7;
171 u8 number;
172 struct u132 *u132;
173 struct u132_endp *curr_endp;
174 struct delayed_work scheduler;
176 #define OHCI_QUIRK_AMD756 0x01
177 #define OHCI_QUIRK_SUPERIO 0x02
178 #define OHCI_QUIRK_INITRESET 0x04
179 #define OHCI_BIG_ENDIAN 0x08
180 #define OHCI_QUIRK_ZFMICRO 0x10
181 struct u132 {
182 struct kref kref;
183 struct list_head u132_list;
184 struct semaphore sw_lock;
185 struct semaphore scheduler_lock;
186 struct u132_platform_data *board;
187 struct platform_device *platform_dev;
188 struct u132_ring ring[MAX_U132_RINGS];
189 int sequence_num;
190 int going;
191 int power;
192 int reset;
193 int num_ports;
194 u32 hc_control;
195 u32 hc_fminterval;
196 u32 hc_roothub_status;
197 u32 hc_roothub_a;
198 u32 hc_roothub_portstatus[MAX_ROOT_PORTS];
199 int flags;
200 unsigned long next_statechange;
201 struct delayed_work monitor;
202 int num_endpoints;
203 struct u132_addr addr[MAX_U132_ADDRS];
204 struct u132_udev udev[MAX_U132_UDEVS];
205 struct u132_port port[MAX_U132_PORTS];
206 struct u132_endp *endp[MAX_U132_ENDPS];
210 * these cannot be inlines because we need the structure offset!!
211 * Does anyone have a better way?????
213 #define ftdi_read_pcimem(pdev, member, data) usb_ftdi_elan_read_pcimem(pdev, \
214 offsetof(struct ohci_regs, member), 0, data);
215 #define ftdi_write_pcimem(pdev, member, data) usb_ftdi_elan_write_pcimem(pdev, \
216 offsetof(struct ohci_regs, member), 0, data);
217 #define u132_read_pcimem(u132, member, data) \
218 usb_ftdi_elan_read_pcimem(u132->platform_dev, offsetof(struct \
219 ohci_regs, member), 0, data);
220 #define u132_write_pcimem(u132, member, data) \
221 usb_ftdi_elan_write_pcimem(u132->platform_dev, offsetof(struct \
222 ohci_regs, member), 0, data);
223 static inline struct u132 *udev_to_u132(struct u132_udev *udev)
225 u8 udev_number = udev->udev_number;
226 return container_of(udev, struct u132, udev[udev_number]);
229 static inline struct u132 *hcd_to_u132(struct usb_hcd *hcd)
231 return (struct u132 *)(hcd->hcd_priv);
234 static inline struct usb_hcd *u132_to_hcd(struct u132 *u132)
236 return container_of((void *)u132, struct usb_hcd, hcd_priv);
239 static inline void u132_disable(struct u132 *u132)
241 u132_to_hcd(u132)->state = HC_STATE_HALT;
245 #define kref_to_u132(d) container_of(d, struct u132, kref)
246 #define kref_to_u132_endp(d) container_of(d, struct u132_endp, kref)
247 #define kref_to_u132_udev(d) container_of(d, struct u132_udev, kref)
248 #include "../misc/usb_u132.h"
249 static const char hcd_name[] = "u132_hcd";
250 #define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE | \
251 USB_PORT_STAT_C_SUSPEND | USB_PORT_STAT_C_OVERCURRENT | \
252 USB_PORT_STAT_C_RESET) << 16)
253 static void u132_hcd_delete(struct kref *kref)
255 struct u132 *u132 = kref_to_u132(kref);
256 struct platform_device *pdev = u132->platform_dev;
257 struct usb_hcd *hcd = u132_to_hcd(u132);
258 u132->going += 1;
259 down(&u132_module_lock);
260 list_del_init(&u132->u132_list);
261 u132_instances -= 1;
262 up(&u132_module_lock);
263 dev_warn(&u132->platform_dev->dev, "FREEING the hcd=%p and thus the u13"
264 "2=%p going=%d pdev=%p\n", hcd, u132, u132->going, pdev);
265 usb_put_hcd(hcd);
268 static inline void u132_u132_put_kref(struct u132 *u132)
270 kref_put(&u132->kref, u132_hcd_delete);
273 static inline void u132_u132_init_kref(struct u132 *u132)
275 kref_init(&u132->kref);
278 static void u132_udev_delete(struct kref *kref)
280 struct u132_udev *udev = kref_to_u132_udev(kref);
281 udev->udev_number = 0;
282 udev->usb_device = NULL;
283 udev->usb_addr = 0;
284 udev->enumeration = 0;
287 static inline void u132_udev_put_kref(struct u132 *u132, struct u132_udev *udev)
289 kref_put(&udev->kref, u132_udev_delete);
292 static inline void u132_udev_get_kref(struct u132 *u132, struct u132_udev *udev)
294 kref_get(&udev->kref);
297 static inline void u132_udev_init_kref(struct u132 *u132,
298 struct u132_udev *udev)
300 kref_init(&udev->kref);
303 static inline void u132_ring_put_kref(struct u132 *u132, struct u132_ring *ring)
305 kref_put(&u132->kref, u132_hcd_delete);
308 static void u132_ring_requeue_work(struct u132 *u132, struct u132_ring *ring,
309 unsigned int delta)
311 if (delta > 0) {
312 if (queue_delayed_work(workqueue, &ring->scheduler, delta))
313 return;
314 } else if (queue_delayed_work(workqueue, &ring->scheduler, 0))
315 return;
316 kref_put(&u132->kref, u132_hcd_delete);
317 return;
320 static void u132_ring_queue_work(struct u132 *u132, struct u132_ring *ring,
321 unsigned int delta)
323 kref_get(&u132->kref);
324 u132_ring_requeue_work(u132, ring, delta);
325 return;
328 static void u132_ring_cancel_work(struct u132 *u132, struct u132_ring *ring)
330 if (cancel_delayed_work(&ring->scheduler)) {
331 kref_put(&u132->kref, u132_hcd_delete);
335 static void u132_endp_delete(struct kref *kref)
337 struct u132_endp *endp = kref_to_u132_endp(kref);
338 struct u132 *u132 = endp->u132;
339 u8 usb_addr = endp->usb_addr;
340 u8 usb_endp = endp->usb_endp;
341 u8 address = u132->addr[usb_addr].address;
342 struct u132_udev *udev = &u132->udev[address];
343 u8 endp_number = endp->endp_number;
344 struct usb_host_endpoint *hep = endp->hep;
345 struct u132_ring *ring = endp->ring;
346 struct list_head *head = &endp->endp_ring;
347 ring->length -= 1;
348 if (endp == ring->curr_endp) {
349 if (list_empty(head)) {
350 ring->curr_endp = NULL;
351 list_del(head);
352 } else {
353 struct u132_endp *next_endp = list_entry(head->next,
354 struct u132_endp, endp_ring);
355 ring->curr_endp = next_endp;
356 list_del(head);
357 }} else
358 list_del(head);
359 if (endp->input) {
360 udev->endp_number_in[usb_endp] = 0;
361 u132_udev_put_kref(u132, udev);
363 if (endp->output) {
364 udev->endp_number_out[usb_endp] = 0;
365 u132_udev_put_kref(u132, udev);
367 u132->endp[endp_number - 1] = NULL;
368 hep->hcpriv = NULL;
369 kfree(endp);
370 u132_u132_put_kref(u132);
373 static inline void u132_endp_put_kref(struct u132 *u132, struct u132_endp *endp)
375 kref_put(&endp->kref, u132_endp_delete);
378 static inline void u132_endp_get_kref(struct u132 *u132, struct u132_endp *endp)
380 kref_get(&endp->kref);
383 static inline void u132_endp_init_kref(struct u132 *u132,
384 struct u132_endp *endp)
386 kref_init(&endp->kref);
387 kref_get(&u132->kref);
390 static void u132_endp_queue_work(struct u132 *u132, struct u132_endp *endp,
391 unsigned int delta)
393 if (queue_delayed_work(workqueue, &endp->scheduler, delta))
394 kref_get(&endp->kref);
397 static void u132_endp_cancel_work(struct u132 *u132, struct u132_endp *endp)
399 if (cancel_delayed_work(&endp->scheduler))
400 kref_put(&endp->kref, u132_endp_delete);
403 static inline void u132_monitor_put_kref(struct u132 *u132)
405 kref_put(&u132->kref, u132_hcd_delete);
408 static void u132_monitor_queue_work(struct u132 *u132, unsigned int delta)
410 if (queue_delayed_work(workqueue, &u132->monitor, delta))
411 kref_get(&u132->kref);
414 static void u132_monitor_requeue_work(struct u132 *u132, unsigned int delta)
416 if (!queue_delayed_work(workqueue, &u132->monitor, delta))
417 kref_put(&u132->kref, u132_hcd_delete);
420 static void u132_monitor_cancel_work(struct u132 *u132)
422 if (cancel_delayed_work(&u132->monitor))
423 kref_put(&u132->kref, u132_hcd_delete);
426 static int read_roothub_info(struct u132 *u132)
428 u32 revision;
429 int retval;
430 retval = u132_read_pcimem(u132, revision, &revision);
431 if (retval) {
432 dev_err(&u132->platform_dev->dev, "error %d accessing device co"
433 "ntrol\n", retval);
434 return retval;
435 } else if ((revision & 0xFF) == 0x10) {
436 } else if ((revision & 0xFF) == 0x11) {
437 } else {
438 dev_err(&u132->platform_dev->dev, "device revision is not valid"
439 " %08X\n", revision);
440 return -ENODEV;
442 retval = u132_read_pcimem(u132, control, &u132->hc_control);
443 if (retval) {
444 dev_err(&u132->platform_dev->dev, "error %d accessing device co"
445 "ntrol\n", retval);
446 return retval;
448 retval = u132_read_pcimem(u132, roothub.status,
449 &u132->hc_roothub_status);
450 if (retval) {
451 dev_err(&u132->platform_dev->dev, "error %d accessing device re"
452 "g roothub.status\n", retval);
453 return retval;
455 retval = u132_read_pcimem(u132, roothub.a, &u132->hc_roothub_a);
456 if (retval) {
457 dev_err(&u132->platform_dev->dev, "error %d accessing device re"
458 "g roothub.a\n", retval);
459 return retval;
462 int I = u132->num_ports;
463 int i = 0;
464 while (I-- > 0) {
465 retval = u132_read_pcimem(u132, roothub.portstatus[i],
466 &u132->hc_roothub_portstatus[i]);
467 if (retval) {
468 dev_err(&u132->platform_dev->dev, "error %d acc"
469 "essing device roothub.portstatus[%d]\n"
470 , retval, i);
471 return retval;
472 } else
473 i += 1;
476 return 0;
479 static void u132_hcd_monitor_work(struct work_struct *work)
481 struct u132 *u132 = container_of(work, struct u132, monitor.work);
482 if (u132->going > 1) {
483 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
484 , u132->going);
485 u132_monitor_put_kref(u132);
486 return;
487 } else if (u132->going > 0) {
488 dev_err(&u132->platform_dev->dev, "device is being removed\n");
489 u132_monitor_put_kref(u132);
490 return;
491 } else {
492 int retval;
493 down(&u132->sw_lock);
494 retval = read_roothub_info(u132);
495 if (retval) {
496 struct usb_hcd *hcd = u132_to_hcd(u132);
497 u132_disable(u132);
498 u132->going = 1;
499 up(&u132->sw_lock);
500 usb_hc_died(hcd);
501 ftdi_elan_gone_away(u132->platform_dev);
502 u132_monitor_put_kref(u132);
503 return;
504 } else {
505 u132_monitor_requeue_work(u132, 500);
506 up(&u132->sw_lock);
507 return;
512 static void u132_hcd_giveback_urb(struct u132 *u132, struct u132_endp *endp,
513 struct urb *urb, int status)
515 struct u132_ring *ring;
516 unsigned long irqs;
517 struct usb_hcd *hcd = u132_to_hcd(u132);
518 urb->error_count = 0;
519 urb->status = status;
520 urb->hcpriv = NULL;
521 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
522 endp->queue_next += 1;
523 if (ENDP_QUEUE_SIZE > --endp->queue_size) {
524 endp->active = 0;
525 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
526 } else {
527 struct list_head *next = endp->urb_more.next;
528 struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
529 urb_more);
530 list_del(next);
531 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
532 urbq->urb;
533 endp->active = 0;
534 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
535 kfree(urbq);
536 } down(&u132->scheduler_lock);
537 ring = endp->ring;
538 ring->in_use = 0;
539 u132_ring_cancel_work(u132, ring);
540 u132_ring_queue_work(u132, ring, 0);
541 up(&u132->scheduler_lock);
542 u132_endp_put_kref(u132, endp);
543 usb_hcd_giveback_urb(hcd, urb);
544 return;
547 static void u132_hcd_forget_urb(struct u132 *u132, struct u132_endp *endp,
548 struct urb *urb, int status)
550 u132_endp_put_kref(u132, endp);
553 static void u132_hcd_abandon_urb(struct u132 *u132, struct u132_endp *endp,
554 struct urb *urb, int status)
556 unsigned long irqs;
557 struct usb_hcd *hcd = u132_to_hcd(u132);
558 urb->error_count = 0;
559 urb->status = status;
560 urb->hcpriv = NULL;
561 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
562 endp->queue_next += 1;
563 if (ENDP_QUEUE_SIZE > --endp->queue_size) {
564 endp->active = 0;
565 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
566 } else {
567 struct list_head *next = endp->urb_more.next;
568 struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
569 urb_more);
570 list_del(next);
571 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
572 urbq->urb;
573 endp->active = 0;
574 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
575 kfree(urbq);
576 } usb_hcd_giveback_urb(hcd, urb);
577 return;
580 static inline int edset_input(struct u132 *u132, struct u132_ring *ring,
581 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
582 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
583 int toggle_bits, int error_count, int condition_code, int repeat_number,
584 int halted, int skipped, int actual, int non_null))
586 return usb_ftdi_elan_edset_input(u132->platform_dev, ring->number, endp,
587 urb, address, endp->usb_endp, toggle_bits, callback);
590 static inline int edset_setup(struct u132 *u132, struct u132_ring *ring,
591 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
592 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
593 int toggle_bits, int error_count, int condition_code, int repeat_number,
594 int halted, int skipped, int actual, int non_null))
596 return usb_ftdi_elan_edset_setup(u132->platform_dev, ring->number, endp,
597 urb, address, endp->usb_endp, toggle_bits, callback);
600 static inline int edset_single(struct u132 *u132, struct u132_ring *ring,
601 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
602 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
603 int toggle_bits, int error_count, int condition_code, int repeat_number,
604 int halted, int skipped, int actual, int non_null))
606 return usb_ftdi_elan_edset_single(u132->platform_dev, ring->number,
607 endp, urb, address, endp->usb_endp, toggle_bits, callback);
610 static inline int edset_output(struct u132 *u132, struct u132_ring *ring,
611 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
612 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
613 int toggle_bits, int error_count, int condition_code, int repeat_number,
614 int halted, int skipped, int actual, int non_null))
616 return usb_ftdi_elan_edset_output(u132->platform_dev, ring->number,
617 endp, urb, address, endp->usb_endp, toggle_bits, callback);
622 * must not LOCK sw_lock
625 static void u132_hcd_interrupt_recv(void *data, struct urb *urb, u8 *buf,
626 int len, int toggle_bits, int error_count, int condition_code,
627 int repeat_number, int halted, int skipped, int actual, int non_null)
629 struct u132_endp *endp = data;
630 struct u132 *u132 = endp->u132;
631 u8 address = u132->addr[endp->usb_addr].address;
632 struct u132_udev *udev = &u132->udev[address];
633 down(&u132->scheduler_lock);
634 if (u132->going > 1) {
635 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
636 , u132->going);
637 up(&u132->scheduler_lock);
638 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
639 return;
640 } else if (endp->dequeueing) {
641 endp->dequeueing = 0;
642 up(&u132->scheduler_lock);
643 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
644 return;
645 } else if (u132->going > 0) {
646 dev_err(&u132->platform_dev->dev, "device is being removed urb="
647 "%p status=%d\n", urb, urb->status);
648 up(&u132->scheduler_lock);
649 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
650 return;
651 } else if (urb->status == -EINPROGRESS) {
652 struct u132_ring *ring = endp->ring;
653 u8 *u = urb->transfer_buffer + urb->actual_length;
654 u8 *b = buf;
655 int L = len;
656 while (L-- > 0) {
657 *u++ = *b++;
659 urb->actual_length += len;
660 if ((condition_code == TD_CC_NOERROR) &&
661 (urb->transfer_buffer_length > urb->actual_length)) {
662 endp->toggle_bits = toggle_bits;
663 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
664 1 & toggle_bits);
665 if (urb->actual_length > 0) {
666 int retval;
667 up(&u132->scheduler_lock);
668 retval = edset_single(u132, ring, endp, urb,
669 address, endp->toggle_bits,
670 u132_hcd_interrupt_recv);
671 if (retval == 0) {
672 } else
673 u132_hcd_giveback_urb(u132, endp, urb,
674 retval);
675 } else {
676 ring->in_use = 0;
677 endp->active = 0;
678 endp->jiffies = jiffies +
679 msecs_to_jiffies(urb->interval);
680 u132_ring_cancel_work(u132, ring);
681 u132_ring_queue_work(u132, ring, 0);
682 up(&u132->scheduler_lock);
683 u132_endp_put_kref(u132, endp);
685 return;
686 } else if ((condition_code == TD_DATAUNDERRUN) &&
687 ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
688 endp->toggle_bits = toggle_bits;
689 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
690 1 & toggle_bits);
691 up(&u132->scheduler_lock);
692 u132_hcd_giveback_urb(u132, endp, urb, 0);
693 return;
694 } else {
695 if (condition_code == TD_CC_NOERROR) {
696 endp->toggle_bits = toggle_bits;
697 usb_settoggle(udev->usb_device, endp->usb_endp,
698 0, 1 & toggle_bits);
699 } else if (condition_code == TD_CC_STALL) {
700 endp->toggle_bits = 0x2;
701 usb_settoggle(udev->usb_device, endp->usb_endp,
702 0, 0);
703 } else {
704 endp->toggle_bits = 0x2;
705 usb_settoggle(udev->usb_device, endp->usb_endp,
706 0, 0);
707 dev_err(&u132->platform_dev->dev, "urb=%p givin"
708 "g back INTERRUPT %s\n", urb,
709 cc_to_text[condition_code]);
711 up(&u132->scheduler_lock);
712 u132_hcd_giveback_urb(u132, endp, urb,
713 cc_to_error[condition_code]);
714 return;
716 } else {
717 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
718 "s=%d\n", urb, urb->status);
719 up(&u132->scheduler_lock);
720 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
721 return;
725 static void u132_hcd_bulk_output_sent(void *data, struct urb *urb, u8 *buf,
726 int len, int toggle_bits, int error_count, int condition_code,
727 int repeat_number, int halted, int skipped, int actual, int non_null)
729 struct u132_endp *endp = data;
730 struct u132 *u132 = endp->u132;
731 u8 address = u132->addr[endp->usb_addr].address;
732 down(&u132->scheduler_lock);
733 if (u132->going > 1) {
734 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
735 , u132->going);
736 up(&u132->scheduler_lock);
737 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
738 return;
739 } else if (endp->dequeueing) {
740 endp->dequeueing = 0;
741 up(&u132->scheduler_lock);
742 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
743 return;
744 } else if (u132->going > 0) {
745 dev_err(&u132->platform_dev->dev, "device is being removed urb="
746 "%p status=%d\n", urb, urb->status);
747 up(&u132->scheduler_lock);
748 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
749 return;
750 } else if (urb->status == -EINPROGRESS) {
751 struct u132_ring *ring = endp->ring;
752 urb->actual_length += len;
753 endp->toggle_bits = toggle_bits;
754 if (urb->transfer_buffer_length > urb->actual_length) {
755 int retval;
756 up(&u132->scheduler_lock);
757 retval = edset_output(u132, ring, endp, urb, address,
758 endp->toggle_bits, u132_hcd_bulk_output_sent);
759 if (retval == 0) {
760 } else
761 u132_hcd_giveback_urb(u132, endp, urb, retval);
762 return;
763 } else {
764 up(&u132->scheduler_lock);
765 u132_hcd_giveback_urb(u132, endp, urb, 0);
766 return;
768 } else {
769 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
770 "s=%d\n", urb, urb->status);
771 up(&u132->scheduler_lock);
772 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
773 return;
777 static void u132_hcd_bulk_input_recv(void *data, struct urb *urb, u8 *buf,
778 int len, int toggle_bits, int error_count, int condition_code,
779 int repeat_number, int halted, int skipped, int actual, int non_null)
781 struct u132_endp *endp = data;
782 struct u132 *u132 = endp->u132;
783 u8 address = u132->addr[endp->usb_addr].address;
784 struct u132_udev *udev = &u132->udev[address];
785 down(&u132->scheduler_lock);
786 if (u132->going > 1) {
787 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
788 , u132->going);
789 up(&u132->scheduler_lock);
790 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
791 return;
792 } else if (endp->dequeueing) {
793 endp->dequeueing = 0;
794 up(&u132->scheduler_lock);
795 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
796 return;
797 } else if (u132->going > 0) {
798 dev_err(&u132->platform_dev->dev, "device is being removed urb="
799 "%p status=%d\n", urb, urb->status);
800 up(&u132->scheduler_lock);
801 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
802 return;
803 } else if (urb->status == -EINPROGRESS) {
804 struct u132_ring *ring = endp->ring;
805 u8 *u = urb->transfer_buffer + urb->actual_length;
806 u8 *b = buf;
807 int L = len;
808 while (L-- > 0) {
809 *u++ = *b++;
811 urb->actual_length += len;
812 if ((condition_code == TD_CC_NOERROR) &&
813 (urb->transfer_buffer_length > urb->actual_length)) {
814 int retval;
815 endp->toggle_bits = toggle_bits;
816 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
817 1 & toggle_bits);
818 up(&u132->scheduler_lock);
819 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
820 ring->number, endp, urb, address,
821 endp->usb_endp, endp->toggle_bits,
822 u132_hcd_bulk_input_recv);
823 if (retval == 0) {
824 } else
825 u132_hcd_giveback_urb(u132, endp, urb, retval);
826 return;
827 } else if (condition_code == TD_CC_NOERROR) {
828 endp->toggle_bits = toggle_bits;
829 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
830 1 & toggle_bits);
831 up(&u132->scheduler_lock);
832 u132_hcd_giveback_urb(u132, endp, urb,
833 cc_to_error[condition_code]);
834 return;
835 } else if ((condition_code == TD_DATAUNDERRUN) &&
836 ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
837 endp->toggle_bits = toggle_bits;
838 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
839 1 & toggle_bits);
840 up(&u132->scheduler_lock);
841 u132_hcd_giveback_urb(u132, endp, urb, 0);
842 return;
843 } else if (condition_code == TD_DATAUNDERRUN) {
844 endp->toggle_bits = toggle_bits;
845 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
846 1 & toggle_bits);
847 dev_warn(&u132->platform_dev->dev, "urb=%p(SHORT NOT OK"
848 ") giving back BULK IN %s\n", urb,
849 cc_to_text[condition_code]);
850 up(&u132->scheduler_lock);
851 u132_hcd_giveback_urb(u132, endp, urb, 0);
852 return;
853 } else if (condition_code == TD_CC_STALL) {
854 endp->toggle_bits = 0x2;
855 usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
856 up(&u132->scheduler_lock);
857 u132_hcd_giveback_urb(u132, endp, urb,
858 cc_to_error[condition_code]);
859 return;
860 } else {
861 endp->toggle_bits = 0x2;
862 usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
863 dev_err(&u132->platform_dev->dev, "urb=%p giving back B"
864 "ULK IN code=%d %s\n", urb, condition_code,
865 cc_to_text[condition_code]);
866 up(&u132->scheduler_lock);
867 u132_hcd_giveback_urb(u132, endp, urb,
868 cc_to_error[condition_code]);
869 return;
871 } else {
872 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
873 "s=%d\n", urb, urb->status);
874 up(&u132->scheduler_lock);
875 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
876 return;
880 static void u132_hcd_configure_empty_sent(void *data, struct urb *urb, u8 *buf,
881 int len, int toggle_bits, int error_count, int condition_code,
882 int repeat_number, int halted, int skipped, int actual, int non_null)
884 struct u132_endp *endp = data;
885 struct u132 *u132 = endp->u132;
886 down(&u132->scheduler_lock);
887 if (u132->going > 1) {
888 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
889 , u132->going);
890 up(&u132->scheduler_lock);
891 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
892 return;
893 } else if (endp->dequeueing) {
894 endp->dequeueing = 0;
895 up(&u132->scheduler_lock);
896 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
897 return;
898 } else if (u132->going > 0) {
899 dev_err(&u132->platform_dev->dev, "device is being removed urb="
900 "%p status=%d\n", urb, urb->status);
901 up(&u132->scheduler_lock);
902 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
903 return;
904 } else if (urb->status == -EINPROGRESS) {
905 up(&u132->scheduler_lock);
906 u132_hcd_giveback_urb(u132, endp, urb, 0);
907 return;
908 } else {
909 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
910 "s=%d\n", urb, urb->status);
911 up(&u132->scheduler_lock);
912 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
913 return;
917 static void u132_hcd_configure_input_recv(void *data, struct urb *urb, u8 *buf,
918 int len, int toggle_bits, int error_count, int condition_code,
919 int repeat_number, int halted, int skipped, int actual, int non_null)
921 struct u132_endp *endp = data;
922 struct u132 *u132 = endp->u132;
923 u8 address = u132->addr[endp->usb_addr].address;
924 down(&u132->scheduler_lock);
925 if (u132->going > 1) {
926 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
927 , u132->going);
928 up(&u132->scheduler_lock);
929 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
930 return;
931 } else if (endp->dequeueing) {
932 endp->dequeueing = 0;
933 up(&u132->scheduler_lock);
934 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
935 return;
936 } else if (u132->going > 0) {
937 dev_err(&u132->platform_dev->dev, "device is being removed urb="
938 "%p status=%d\n", urb, urb->status);
939 up(&u132->scheduler_lock);
940 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
941 return;
942 } else if (urb->status == -EINPROGRESS) {
943 struct u132_ring *ring = endp->ring;
944 u8 *u = urb->transfer_buffer;
945 u8 *b = buf;
946 int L = len;
947 while (L-- > 0) {
948 *u++ = *b++;
950 urb->actual_length = len;
951 if ((condition_code == TD_CC_NOERROR) || ((condition_code ==
952 TD_DATAUNDERRUN) && ((urb->transfer_flags &
953 URB_SHORT_NOT_OK) == 0))) {
954 int retval;
955 up(&u132->scheduler_lock);
956 retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
957 ring->number, endp, urb, address,
958 endp->usb_endp, 0x3,
959 u132_hcd_configure_empty_sent);
960 if (retval == 0) {
961 } else
962 u132_hcd_giveback_urb(u132, endp, urb, retval);
963 return;
964 } else if (condition_code == TD_CC_STALL) {
965 up(&u132->scheduler_lock);
966 dev_warn(&u132->platform_dev->dev, "giving back SETUP I"
967 "NPUT STALL urb %p\n", urb);
968 u132_hcd_giveback_urb(u132, endp, urb,
969 cc_to_error[condition_code]);
970 return;
971 } else {
972 up(&u132->scheduler_lock);
973 dev_err(&u132->platform_dev->dev, "giving back SETUP IN"
974 "PUT %s urb %p\n", cc_to_text[condition_code],
975 urb);
976 u132_hcd_giveback_urb(u132, endp, urb,
977 cc_to_error[condition_code]);
978 return;
980 } else {
981 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
982 "s=%d\n", urb, urb->status);
983 up(&u132->scheduler_lock);
984 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
985 return;
989 static void u132_hcd_configure_empty_recv(void *data, struct urb *urb, u8 *buf,
990 int len, int toggle_bits, int error_count, int condition_code,
991 int repeat_number, int halted, int skipped, int actual, int non_null)
993 struct u132_endp *endp = data;
994 struct u132 *u132 = endp->u132;
995 down(&u132->scheduler_lock);
996 if (u132->going > 1) {
997 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
998 , u132->going);
999 up(&u132->scheduler_lock);
1000 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1001 return;
1002 } else if (endp->dequeueing) {
1003 endp->dequeueing = 0;
1004 up(&u132->scheduler_lock);
1005 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1006 return;
1007 } else if (u132->going > 0) {
1008 dev_err(&u132->platform_dev->dev, "device is being removed urb="
1009 "%p status=%d\n", urb, urb->status);
1010 up(&u132->scheduler_lock);
1011 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1012 return;
1013 } else if (urb->status == -EINPROGRESS) {
1014 up(&u132->scheduler_lock);
1015 u132_hcd_giveback_urb(u132, endp, urb, 0);
1016 return;
1017 } else {
1018 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
1019 "s=%d\n", urb, urb->status);
1020 up(&u132->scheduler_lock);
1021 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
1022 return;
1026 static void u132_hcd_configure_setup_sent(void *data, struct urb *urb, u8 *buf,
1027 int len, int toggle_bits, int error_count, int condition_code,
1028 int repeat_number, int halted, int skipped, int actual, int non_null)
1030 struct u132_endp *endp = data;
1031 struct u132 *u132 = endp->u132;
1032 u8 address = u132->addr[endp->usb_addr].address;
1033 down(&u132->scheduler_lock);
1034 if (u132->going > 1) {
1035 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1036 , u132->going);
1037 up(&u132->scheduler_lock);
1038 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1039 return;
1040 } else if (endp->dequeueing) {
1041 endp->dequeueing = 0;
1042 up(&u132->scheduler_lock);
1043 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1044 return;
1045 } else if (u132->going > 0) {
1046 dev_err(&u132->platform_dev->dev, "device is being removed urb="
1047 "%p status=%d\n", urb, urb->status);
1048 up(&u132->scheduler_lock);
1049 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1050 return;
1051 } else if (urb->status == -EINPROGRESS) {
1052 if (usb_pipein(urb->pipe)) {
1053 int retval;
1054 struct u132_ring *ring = endp->ring;
1055 up(&u132->scheduler_lock);
1056 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1057 ring->number, endp, urb, address,
1058 endp->usb_endp, 0,
1059 u132_hcd_configure_input_recv);
1060 if (retval == 0) {
1061 } else
1062 u132_hcd_giveback_urb(u132, endp, urb, retval);
1063 return;
1064 } else {
1065 int retval;
1066 struct u132_ring *ring = endp->ring;
1067 up(&u132->scheduler_lock);
1068 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1069 ring->number, endp, urb, address,
1070 endp->usb_endp, 0,
1071 u132_hcd_configure_empty_recv);
1072 if (retval == 0) {
1073 } else
1074 u132_hcd_giveback_urb(u132, endp, urb, retval);
1075 return;
1077 } else {
1078 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
1079 "s=%d\n", urb, urb->status);
1080 up(&u132->scheduler_lock);
1081 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
1082 return;
1086 static void u132_hcd_enumeration_empty_recv(void *data, struct urb *urb,
1087 u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
1088 int repeat_number, int halted, int skipped, int actual, int non_null)
1090 struct u132_endp *endp = data;
1091 struct u132 *u132 = endp->u132;
1092 u8 address = u132->addr[endp->usb_addr].address;
1093 struct u132_udev *udev = &u132->udev[address];
1094 down(&u132->scheduler_lock);
1095 if (u132->going > 1) {
1096 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1097 , u132->going);
1098 up(&u132->scheduler_lock);
1099 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1100 return;
1101 } else if (endp->dequeueing) {
1102 endp->dequeueing = 0;
1103 up(&u132->scheduler_lock);
1104 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1105 return;
1106 } else if (u132->going > 0) {
1107 dev_err(&u132->platform_dev->dev, "device is being removed urb="
1108 "%p status=%d\n", urb, urb->status);
1109 up(&u132->scheduler_lock);
1110 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1111 return;
1112 } else if (urb->status == -EINPROGRESS) {
1113 u132->addr[0].address = 0;
1114 endp->usb_addr = udev->usb_addr;
1115 up(&u132->scheduler_lock);
1116 u132_hcd_giveback_urb(u132, endp, urb, 0);
1117 return;
1118 } else {
1119 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
1120 "s=%d\n", urb, urb->status);
1121 up(&u132->scheduler_lock);
1122 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
1123 return;
1127 static void u132_hcd_enumeration_address_sent(void *data, struct urb *urb,
1128 u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
1129 int repeat_number, int halted, int skipped, int actual, int non_null)
1131 struct u132_endp *endp = data;
1132 struct u132 *u132 = endp->u132;
1133 down(&u132->scheduler_lock);
1134 if (u132->going > 1) {
1135 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1136 , u132->going);
1137 up(&u132->scheduler_lock);
1138 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1139 return;
1140 } else if (endp->dequeueing) {
1141 endp->dequeueing = 0;
1142 up(&u132->scheduler_lock);
1143 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1144 return;
1145 } else if (u132->going > 0) {
1146 dev_err(&u132->platform_dev->dev, "device is being removed urb="
1147 "%p status=%d\n", urb, urb->status);
1148 up(&u132->scheduler_lock);
1149 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1150 return;
1151 } else if (urb->status == -EINPROGRESS) {
1152 int retval;
1153 struct u132_ring *ring = endp->ring;
1154 up(&u132->scheduler_lock);
1155 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1156 ring->number, endp, urb, 0, endp->usb_endp, 0,
1157 u132_hcd_enumeration_empty_recv);
1158 if (retval == 0) {
1159 } else
1160 u132_hcd_giveback_urb(u132, endp, urb, retval);
1161 return;
1162 } else {
1163 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
1164 "s=%d\n", urb, urb->status);
1165 up(&u132->scheduler_lock);
1166 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
1167 return;
1171 static void u132_hcd_initial_empty_sent(void *data, struct urb *urb, u8 *buf,
1172 int len, int toggle_bits, int error_count, int condition_code,
1173 int repeat_number, int halted, int skipped, int actual, int non_null)
1175 struct u132_endp *endp = data;
1176 struct u132 *u132 = endp->u132;
1177 down(&u132->scheduler_lock);
1178 if (u132->going > 1) {
1179 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1180 , u132->going);
1181 up(&u132->scheduler_lock);
1182 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1183 return;
1184 } else if (endp->dequeueing) {
1185 endp->dequeueing = 0;
1186 up(&u132->scheduler_lock);
1187 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1188 return;
1189 } else if (u132->going > 0) {
1190 dev_err(&u132->platform_dev->dev, "device is being removed urb="
1191 "%p status=%d\n", urb, urb->status);
1192 up(&u132->scheduler_lock);
1193 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1194 return;
1195 } else if (urb->status == -EINPROGRESS) {
1196 up(&u132->scheduler_lock);
1197 u132_hcd_giveback_urb(u132, endp, urb, 0);
1198 return;
1199 } else {
1200 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
1201 "s=%d\n", urb, urb->status);
1202 up(&u132->scheduler_lock);
1203 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
1204 return;
1208 static void u132_hcd_initial_input_recv(void *data, struct urb *urb, u8 *buf,
1209 int len, int toggle_bits, int error_count, int condition_code,
1210 int repeat_number, int halted, int skipped, int actual, int non_null)
1212 struct u132_endp *endp = data;
1213 struct u132 *u132 = endp->u132;
1214 u8 address = u132->addr[endp->usb_addr].address;
1215 down(&u132->scheduler_lock);
1216 if (u132->going > 1) {
1217 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1218 , u132->going);
1219 up(&u132->scheduler_lock);
1220 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1221 return;
1222 } else if (endp->dequeueing) {
1223 endp->dequeueing = 0;
1224 up(&u132->scheduler_lock);
1225 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1226 return;
1227 } else if (u132->going > 0) {
1228 dev_err(&u132->platform_dev->dev, "device is being removed urb="
1229 "%p status=%d\n", urb, urb->status);
1230 up(&u132->scheduler_lock);
1231 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1232 return;
1233 } else if (urb->status == -EINPROGRESS) {
1234 int retval;
1235 struct u132_ring *ring = endp->ring;
1236 u8 *u = urb->transfer_buffer;
1237 u8 *b = buf;
1238 int L = len;
1239 while (L-- > 0) {
1240 *u++ = *b++;
1242 urb->actual_length = len;
1243 up(&u132->scheduler_lock);
1244 retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
1245 ring->number, endp, urb, address, endp->usb_endp, 0x3,
1246 u132_hcd_initial_empty_sent);
1247 if (retval == 0) {
1248 } else
1249 u132_hcd_giveback_urb(u132, endp, urb, retval);
1250 return;
1251 } else {
1252 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
1253 "s=%d\n", urb, urb->status);
1254 up(&u132->scheduler_lock);
1255 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
1256 return;
1260 static void u132_hcd_initial_setup_sent(void *data, struct urb *urb, u8 *buf,
1261 int len, int toggle_bits, int error_count, int condition_code,
1262 int repeat_number, int halted, int skipped, int actual, int non_null)
1264 struct u132_endp *endp = data;
1265 struct u132 *u132 = endp->u132;
1266 u8 address = u132->addr[endp->usb_addr].address;
1267 down(&u132->scheduler_lock);
1268 if (u132->going > 1) {
1269 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1270 , u132->going);
1271 up(&u132->scheduler_lock);
1272 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1273 return;
1274 } else if (endp->dequeueing) {
1275 endp->dequeueing = 0;
1276 up(&u132->scheduler_lock);
1277 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1278 return;
1279 } else if (u132->going > 0) {
1280 dev_err(&u132->platform_dev->dev, "device is being removed urb="
1281 "%p status=%d\n", urb, urb->status);
1282 up(&u132->scheduler_lock);
1283 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1284 return;
1285 } else if (urb->status == -EINPROGRESS) {
1286 int retval;
1287 struct u132_ring *ring = endp->ring;
1288 up(&u132->scheduler_lock);
1289 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1290 ring->number, endp, urb, address, endp->usb_endp, 0,
1291 u132_hcd_initial_input_recv);
1292 if (retval == 0) {
1293 } else
1294 u132_hcd_giveback_urb(u132, endp, urb, retval);
1295 return;
1296 } else {
1297 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p statu"
1298 "s=%d\n", urb, urb->status);
1299 up(&u132->scheduler_lock);
1300 u132_hcd_giveback_urb(u132, endp, urb, urb->status);
1301 return;
1306 * this work function is only executed from the work queue
1309 static void u132_hcd_ring_work_scheduler(struct work_struct *work)
1311 struct u132_ring *ring =
1312 container_of(work, struct u132_ring, scheduler.work);
1313 struct u132 *u132 = ring->u132;
1314 down(&u132->scheduler_lock);
1315 if (ring->in_use) {
1316 up(&u132->scheduler_lock);
1317 u132_ring_put_kref(u132, ring);
1318 return;
1319 } else if (ring->curr_endp) {
1320 struct u132_endp *last_endp = ring->curr_endp;
1321 struct list_head *scan;
1322 struct list_head *head = &last_endp->endp_ring;
1323 unsigned long wakeup = 0;
1324 list_for_each(scan, head) {
1325 struct u132_endp *endp = list_entry(scan,
1326 struct u132_endp, endp_ring);
1327 if (endp->queue_next == endp->queue_last) {
1328 } else if ((endp->delayed == 0)
1329 || time_after_eq(jiffies, endp->jiffies)) {
1330 ring->curr_endp = endp;
1331 u132_endp_cancel_work(u132, last_endp);
1332 u132_endp_queue_work(u132, last_endp, 0);
1333 up(&u132->scheduler_lock);
1334 u132_ring_put_kref(u132, ring);
1335 return;
1336 } else {
1337 unsigned long delta = endp->jiffies - jiffies;
1338 if (delta > wakeup)
1339 wakeup = delta;
1342 if (last_endp->queue_next == last_endp->queue_last) {
1343 } else if ((last_endp->delayed == 0) || time_after_eq(jiffies,
1344 last_endp->jiffies)) {
1345 u132_endp_cancel_work(u132, last_endp);
1346 u132_endp_queue_work(u132, last_endp, 0);
1347 up(&u132->scheduler_lock);
1348 u132_ring_put_kref(u132, ring);
1349 return;
1350 } else {
1351 unsigned long delta = last_endp->jiffies - jiffies;
1352 if (delta > wakeup)
1353 wakeup = delta;
1355 if (wakeup > 0) {
1356 u132_ring_requeue_work(u132, ring, wakeup);
1357 up(&u132->scheduler_lock);
1358 return;
1359 } else {
1360 up(&u132->scheduler_lock);
1361 u132_ring_put_kref(u132, ring);
1362 return;
1364 } else {
1365 up(&u132->scheduler_lock);
1366 u132_ring_put_kref(u132, ring);
1367 return;
1371 static void u132_hcd_endp_work_scheduler(struct work_struct *work)
1373 struct u132_ring *ring;
1374 struct u132_endp *endp =
1375 container_of(work, struct u132_endp, scheduler.work);
1376 struct u132 *u132 = endp->u132;
1377 down(&u132->scheduler_lock);
1378 ring = endp->ring;
1379 if (endp->edset_flush) {
1380 endp->edset_flush = 0;
1381 if (endp->dequeueing)
1382 usb_ftdi_elan_edset_flush(u132->platform_dev,
1383 ring->number, endp);
1384 up(&u132->scheduler_lock);
1385 u132_endp_put_kref(u132, endp);
1386 return;
1387 } else if (endp->active) {
1388 up(&u132->scheduler_lock);
1389 u132_endp_put_kref(u132, endp);
1390 return;
1391 } else if (ring->in_use) {
1392 up(&u132->scheduler_lock);
1393 u132_endp_put_kref(u132, endp);
1394 return;
1395 } else if (endp->queue_next == endp->queue_last) {
1396 up(&u132->scheduler_lock);
1397 u132_endp_put_kref(u132, endp);
1398 return;
1399 } else if (endp->pipetype == PIPE_INTERRUPT) {
1400 u8 address = u132->addr[endp->usb_addr].address;
1401 if (ring->in_use) {
1402 up(&u132->scheduler_lock);
1403 u132_endp_put_kref(u132, endp);
1404 return;
1405 } else {
1406 int retval;
1407 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1408 endp->queue_next];
1409 endp->active = 1;
1410 ring->curr_endp = endp;
1411 ring->in_use = 1;
1412 up(&u132->scheduler_lock);
1413 retval = edset_single(u132, ring, endp, urb, address,
1414 endp->toggle_bits, u132_hcd_interrupt_recv);
1415 if (retval == 0) {
1416 } else
1417 u132_hcd_giveback_urb(u132, endp, urb, retval);
1418 return;
1420 } else if (endp->pipetype == PIPE_CONTROL) {
1421 u8 address = u132->addr[endp->usb_addr].address;
1422 if (ring->in_use) {
1423 up(&u132->scheduler_lock);
1424 u132_endp_put_kref(u132, endp);
1425 return;
1426 } else if (address == 0) {
1427 int retval;
1428 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1429 endp->queue_next];
1430 endp->active = 1;
1431 ring->curr_endp = endp;
1432 ring->in_use = 1;
1433 up(&u132->scheduler_lock);
1434 retval = edset_setup(u132, ring, endp, urb, address,
1435 0x2, u132_hcd_initial_setup_sent);
1436 if (retval == 0) {
1437 } else
1438 u132_hcd_giveback_urb(u132, endp, urb, retval);
1439 return;
1440 } else if (endp->usb_addr == 0) {
1441 int retval;
1442 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1443 endp->queue_next];
1444 endp->active = 1;
1445 ring->curr_endp = endp;
1446 ring->in_use = 1;
1447 up(&u132->scheduler_lock);
1448 retval = edset_setup(u132, ring, endp, urb, 0, 0x2,
1449 u132_hcd_enumeration_address_sent);
1450 if (retval == 0) {
1451 } else
1452 u132_hcd_giveback_urb(u132, endp, urb, retval);
1453 return;
1454 } else {
1455 int retval;
1456 u8 address = u132->addr[endp->usb_addr].address;
1457 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1458 endp->queue_next];
1459 endp->active = 1;
1460 ring->curr_endp = endp;
1461 ring->in_use = 1;
1462 up(&u132->scheduler_lock);
1463 retval = edset_setup(u132, ring, endp, urb, address,
1464 0x2, u132_hcd_configure_setup_sent);
1465 if (retval == 0) {
1466 } else
1467 u132_hcd_giveback_urb(u132, endp, urb, retval);
1468 return;
1470 } else {
1471 if (endp->input) {
1472 u8 address = u132->addr[endp->usb_addr].address;
1473 if (ring->in_use) {
1474 up(&u132->scheduler_lock);
1475 u132_endp_put_kref(u132, endp);
1476 return;
1477 } else {
1478 int retval;
1479 struct urb *urb = endp->urb_list[
1480 ENDP_QUEUE_MASK & endp->queue_next];
1481 endp->active = 1;
1482 ring->curr_endp = endp;
1483 ring->in_use = 1;
1484 up(&u132->scheduler_lock);
1485 retval = edset_input(u132, ring, endp, urb,
1486 address, endp->toggle_bits,
1487 u132_hcd_bulk_input_recv);
1488 if (retval == 0) {
1489 } else
1490 u132_hcd_giveback_urb(u132, endp, urb,
1491 retval);
1492 return;
1494 } else { /* output pipe */
1495 u8 address = u132->addr[endp->usb_addr].address;
1496 if (ring->in_use) {
1497 up(&u132->scheduler_lock);
1498 u132_endp_put_kref(u132, endp);
1499 return;
1500 } else {
1501 int retval;
1502 struct urb *urb = endp->urb_list[
1503 ENDP_QUEUE_MASK & endp->queue_next];
1504 endp->active = 1;
1505 ring->curr_endp = endp;
1506 ring->in_use = 1;
1507 up(&u132->scheduler_lock);
1508 retval = edset_output(u132, ring, endp, urb,
1509 address, endp->toggle_bits,
1510 u132_hcd_bulk_output_sent);
1511 if (retval == 0) {
1512 } else
1513 u132_hcd_giveback_urb(u132, endp, urb,
1514 retval);
1515 return;
1521 static void port_power(struct u132 *u132, int pn, int is_on)
1523 u132->port[pn].power = is_on;
1526 static void u132_power(struct u132 *u132, int is_on)
1528 struct usb_hcd *hcd = u132_to_hcd(u132)
1529 ; /* hub is inactive unless the port is powered */
1530 if (is_on) {
1531 if (u132->power)
1532 return;
1533 u132->power = 1;
1534 hcd->self.controller->power.power_state = PMSG_ON;
1535 } else {
1536 u132->power = 0;
1537 hcd->state = HC_STATE_HALT;
1538 hcd->self.controller->power.power_state = PMSG_SUSPEND;
1542 static int u132_periodic_reinit(struct u132 *u132)
1544 int retval;
1545 u32 fi = u132->hc_fminterval & 0x03fff;
1546 u32 fit;
1547 u32 fminterval;
1548 retval = u132_read_pcimem(u132, fminterval, &fminterval);
1549 if (retval)
1550 return retval;
1551 fit = fminterval & FIT;
1552 retval = u132_write_pcimem(u132, fminterval,
1553 (fit ^ FIT) | u132->hc_fminterval);
1554 if (retval)
1555 return retval;
1556 retval = u132_write_pcimem(u132, periodicstart,
1557 ((9 *fi) / 10) & 0x3fff);
1558 if (retval)
1559 return retval;
1560 return 0;
1563 static char *hcfs2string(int state)
1565 switch (state) {
1566 case OHCI_USB_RESET:
1567 return "reset";
1568 case OHCI_USB_RESUME:
1569 return "resume";
1570 case OHCI_USB_OPER:
1571 return "operational";
1572 case OHCI_USB_SUSPEND:
1573 return "suspend";
1575 return "?";
1578 static int u132_init(struct u132 *u132)
1580 int retval;
1581 u32 control;
1582 u132_disable(u132);
1583 u132->next_statechange = jiffies;
1584 retval = u132_write_pcimem(u132, intrdisable, OHCI_INTR_MIE);
1585 if (retval)
1586 return retval;
1587 retval = u132_read_pcimem(u132, control, &control);
1588 if (retval)
1589 return retval;
1590 if (u132->num_ports == 0) {
1591 u32 rh_a = -1;
1592 retval = u132_read_pcimem(u132, roothub.a, &rh_a);
1593 if (retval)
1594 return retval;
1595 u132->num_ports = rh_a & RH_A_NDP;
1596 retval = read_roothub_info(u132);
1597 if (retval)
1598 return retval;
1600 if (u132->num_ports > MAX_U132_PORTS) {
1601 return -EINVAL;
1603 return 0;
1607 /* Start an OHCI controller, set the BUS operational
1608 * resets USB and controller
1609 * enable interrupts
1611 static int u132_run(struct u132 *u132)
1613 int retval;
1614 u32 control;
1615 u32 status;
1616 u32 fminterval;
1617 u32 periodicstart;
1618 u32 cmdstatus;
1619 u32 roothub_a;
1620 int mask = OHCI_INTR_INIT;
1621 int first = u132->hc_fminterval == 0;
1622 int sleep_time = 0;
1623 int reset_timeout = 30; /* ... allow extra time */
1624 u132_disable(u132);
1625 if (first) {
1626 u32 temp;
1627 retval = u132_read_pcimem(u132, fminterval, &temp);
1628 if (retval)
1629 return retval;
1630 u132->hc_fminterval = temp & 0x3fff;
1631 if (u132->hc_fminterval != FI) {
1633 u132->hc_fminterval |= FSMP(u132->hc_fminterval) << 16;
1635 retval = u132_read_pcimem(u132, control, &u132->hc_control);
1636 if (retval)
1637 return retval;
1638 dev_info(&u132->platform_dev->dev, "resetting from state '%s', control "
1639 "= %08X\n", hcfs2string(u132->hc_control & OHCI_CTRL_HCFS),
1640 u132->hc_control);
1641 switch (u132->hc_control & OHCI_CTRL_HCFS) {
1642 case OHCI_USB_OPER:
1643 sleep_time = 0;
1644 break;
1645 case OHCI_USB_SUSPEND:
1646 case OHCI_USB_RESUME:
1647 u132->hc_control &= OHCI_CTRL_RWC;
1648 u132->hc_control |= OHCI_USB_RESUME;
1649 sleep_time = 10;
1650 break;
1651 default:
1652 u132->hc_control &= OHCI_CTRL_RWC;
1653 u132->hc_control |= OHCI_USB_RESET;
1654 sleep_time = 50;
1655 break;
1657 retval = u132_write_pcimem(u132, control, u132->hc_control);
1658 if (retval)
1659 return retval;
1660 retval = u132_read_pcimem(u132, control, &control);
1661 if (retval)
1662 return retval;
1663 msleep(sleep_time);
1664 retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1665 if (retval)
1666 return retval;
1667 if (!(roothub_a & RH_A_NPS)) {
1668 int temp; /* power down each port */
1669 for (temp = 0; temp < u132->num_ports; temp++) {
1670 retval = u132_write_pcimem(u132,
1671 roothub.portstatus[temp], RH_PS_LSDA);
1672 if (retval)
1673 return retval;
1676 retval = u132_read_pcimem(u132, control, &control);
1677 if (retval)
1678 return retval;
1679 retry:retval = u132_read_pcimem(u132, cmdstatus, &status);
1680 if (retval)
1681 return retval;
1682 retval = u132_write_pcimem(u132, cmdstatus, OHCI_HCR);
1683 if (retval)
1684 return retval;
1685 extra:{
1686 retval = u132_read_pcimem(u132, cmdstatus, &status);
1687 if (retval)
1688 return retval;
1689 if (0 != (status & OHCI_HCR)) {
1690 if (--reset_timeout == 0) {
1691 dev_err(&u132->platform_dev->dev, "USB HC reset"
1692 " timed out!\n");
1693 return -ENODEV;
1694 } else {
1695 msleep(5);
1696 goto extra;
1700 if (u132->flags & OHCI_QUIRK_INITRESET) {
1701 retval = u132_write_pcimem(u132, control, u132->hc_control);
1702 if (retval)
1703 return retval;
1704 retval = u132_read_pcimem(u132, control, &control);
1705 if (retval)
1706 return retval;
1708 retval = u132_write_pcimem(u132, ed_controlhead, 0x00000000);
1709 if (retval)
1710 return retval;
1711 retval = u132_write_pcimem(u132, ed_bulkhead, 0x11000000);
1712 if (retval)
1713 return retval;
1714 retval = u132_write_pcimem(u132, hcca, 0x00000000);
1715 if (retval)
1716 return retval;
1717 retval = u132_periodic_reinit(u132);
1718 if (retval)
1719 return retval;
1720 retval = u132_read_pcimem(u132, fminterval, &fminterval);
1721 if (retval)
1722 return retval;
1723 retval = u132_read_pcimem(u132, periodicstart, &periodicstart);
1724 if (retval)
1725 return retval;
1726 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
1727 if (!(u132->flags & OHCI_QUIRK_INITRESET)) {
1728 u132->flags |= OHCI_QUIRK_INITRESET;
1729 goto retry;
1730 } else
1731 dev_err(&u132->platform_dev->dev, "init err(%08x %04x)"
1732 "\n", fminterval, periodicstart);
1733 } /* start controller operations */
1734 u132->hc_control &= OHCI_CTRL_RWC;
1735 u132->hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
1736 retval = u132_write_pcimem(u132, control, u132->hc_control);
1737 if (retval)
1738 return retval;
1739 retval = u132_write_pcimem(u132, cmdstatus, OHCI_BLF);
1740 if (retval)
1741 return retval;
1742 retval = u132_read_pcimem(u132, cmdstatus, &cmdstatus);
1743 if (retval)
1744 return retval;
1745 retval = u132_read_pcimem(u132, control, &control);
1746 if (retval)
1747 return retval;
1748 u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1749 retval = u132_write_pcimem(u132, roothub.status, RH_HS_DRWE);
1750 if (retval)
1751 return retval;
1752 retval = u132_write_pcimem(u132, intrstatus, mask);
1753 if (retval)
1754 return retval;
1755 retval = u132_write_pcimem(u132, intrdisable,
1756 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
1757 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
1758 OHCI_INTR_SO);
1759 if (retval)
1760 return retval; /* handle root hub init quirks ... */
1761 retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1762 if (retval)
1763 return retval;
1764 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
1765 if (u132->flags & OHCI_QUIRK_SUPERIO) {
1766 roothub_a |= RH_A_NOCP;
1767 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
1768 retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1769 if (retval)
1770 return retval;
1771 } else if ((u132->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
1772 roothub_a |= RH_A_NPS;
1773 retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1774 if (retval)
1775 return retval;
1777 retval = u132_write_pcimem(u132, roothub.status, RH_HS_LPSC);
1778 if (retval)
1779 return retval;
1780 retval = u132_write_pcimem(u132, roothub.b,
1781 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
1782 if (retval)
1783 return retval;
1784 retval = u132_read_pcimem(u132, control, &control);
1785 if (retval)
1786 return retval;
1787 mdelay((roothub_a >> 23) & 0x1fe);
1788 u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1789 return 0;
1792 static void u132_hcd_stop(struct usb_hcd *hcd)
1794 struct u132 *u132 = hcd_to_u132(hcd);
1795 if (u132->going > 1) {
1796 dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p) has b"
1797 "een removed %d\n", u132, hcd, u132->going);
1798 } else if (u132->going > 0) {
1799 dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
1800 "ed\n", hcd);
1801 } else {
1802 down(&u132->sw_lock);
1803 msleep(100);
1804 u132_power(u132, 0);
1805 up(&u132->sw_lock);
1809 static int u132_hcd_start(struct usb_hcd *hcd)
1811 struct u132 *u132 = hcd_to_u132(hcd);
1812 if (u132->going > 1) {
1813 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1814 , u132->going);
1815 return -ENODEV;
1816 } else if (u132->going > 0) {
1817 dev_err(&u132->platform_dev->dev, "device is being removed\n");
1818 return -ESHUTDOWN;
1819 } else if (hcd->self.controller) {
1820 int retval;
1821 struct platform_device *pdev =
1822 to_platform_device(hcd->self.controller);
1823 u16 vendor = ((struct u132_platform_data *)
1824 (pdev->dev.platform_data))->vendor;
1825 u16 device = ((struct u132_platform_data *)
1826 (pdev->dev.platform_data))->device;
1827 down(&u132->sw_lock);
1828 msleep(10);
1829 if (vendor == PCI_VENDOR_ID_AMD && device == 0x740c) {
1830 u132->flags = OHCI_QUIRK_AMD756;
1831 } else if (vendor == PCI_VENDOR_ID_OPTI && device == 0xc861) {
1832 dev_err(&u132->platform_dev->dev, "WARNING: OPTi workar"
1833 "ounds unavailable\n");
1834 } else if (vendor == PCI_VENDOR_ID_COMPAQ && device == 0xa0f8)
1835 u132->flags |= OHCI_QUIRK_ZFMICRO;
1836 retval = u132_run(u132);
1837 if (retval) {
1838 u132_disable(u132);
1839 u132->going = 1;
1841 msleep(100);
1842 up(&u132->sw_lock);
1843 return retval;
1844 } else {
1845 dev_err(&u132->platform_dev->dev, "platform_device missing\n");
1846 return -ENODEV;
1850 static int u132_hcd_reset(struct usb_hcd *hcd)
1852 struct u132 *u132 = hcd_to_u132(hcd);
1853 if (u132->going > 1) {
1854 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1855 , u132->going);
1856 return -ENODEV;
1857 } else if (u132->going > 0) {
1858 dev_err(&u132->platform_dev->dev, "device is being removed\n");
1859 return -ESHUTDOWN;
1860 } else {
1861 int retval;
1862 down(&u132->sw_lock);
1863 retval = u132_init(u132);
1864 if (retval) {
1865 u132_disable(u132);
1866 u132->going = 1;
1868 up(&u132->sw_lock);
1869 return retval;
1873 static int create_endpoint_and_queue_int(struct u132 *u132,
1874 struct u132_udev *udev, struct usb_host_endpoint *hep, struct urb *urb,
1875 struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1876 gfp_t mem_flags)
1878 struct u132_ring *ring;
1879 unsigned long irqs;
1880 u8 endp_number = ++u132->num_endpoints;
1881 struct u132_endp *endp = hep->hcpriv = u132->endp[endp_number - 1] =
1882 kmalloc(sizeof(struct u132_endp), mem_flags);
1883 if (!endp) {
1884 return -ENOMEM;
1886 INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1887 spin_lock_init(&endp->queue_lock.slock);
1888 INIT_LIST_HEAD(&endp->urb_more);
1889 ring = endp->ring = &u132->ring[0];
1890 if (ring->curr_endp) {
1891 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
1892 } else {
1893 INIT_LIST_HEAD(&endp->endp_ring);
1894 ring->curr_endp = endp;
1896 ring->length += 1;
1897 endp->dequeueing = 0;
1898 endp->edset_flush = 0;
1899 endp->active = 0;
1900 endp->delayed = 0;
1901 endp->endp_number = endp_number;
1902 endp->u132 = u132;
1903 endp->hep = hep;
1904 endp->pipetype = usb_pipetype(urb->pipe);
1905 u132_endp_init_kref(u132, endp);
1906 if (usb_pipein(urb->pipe)) {
1907 endp->toggle_bits = 0x2;
1908 usb_settoggle(udev->usb_device, usb_endp, 0, 0);
1909 endp->input = 1;
1910 endp->output = 0;
1911 udev->endp_number_in[usb_endp] = endp_number;
1912 u132_udev_get_kref(u132, udev);
1913 } else {
1914 endp->toggle_bits = 0x2;
1915 usb_settoggle(udev->usb_device, usb_endp, 1, 0);
1916 endp->input = 0;
1917 endp->output = 1;
1918 udev->endp_number_out[usb_endp] = endp_number;
1919 u132_udev_get_kref(u132, udev);
1921 urb->hcpriv = u132;
1922 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
1923 endp->delayed = 1;
1924 endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1925 endp->udev_number = address;
1926 endp->usb_addr = usb_addr;
1927 endp->usb_endp = usb_endp;
1928 endp->queue_size = 1;
1929 endp->queue_last = 0;
1930 endp->queue_next = 0;
1931 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1932 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1933 u132_endp_queue_work(u132, endp, msecs_to_jiffies(urb->interval));
1934 return 0;
1937 static int queue_int_on_old_endpoint(struct u132 *u132, struct u132_udev *udev,
1938 struct usb_host_endpoint *hep, struct urb *urb,
1939 struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
1940 u8 usb_endp, u8 address)
1942 urb->hcpriv = u132;
1943 endp->delayed = 1;
1944 endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1945 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
1946 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1947 } else {
1948 struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
1949 GFP_ATOMIC);
1950 if (urbq == NULL) {
1951 endp->queue_size -= 1;
1952 return -ENOMEM;
1953 } else {
1954 list_add_tail(&urbq->urb_more, &endp->urb_more);
1955 urbq->urb = urb;
1958 return 0;
1961 static int create_endpoint_and_queue_bulk(struct u132 *u132,
1962 struct u132_udev *udev, struct usb_host_endpoint *hep, struct urb *urb,
1963 struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1964 gfp_t mem_flags)
1966 int ring_number;
1967 struct u132_ring *ring;
1968 unsigned long irqs;
1969 u8 endp_number = ++u132->num_endpoints;
1970 struct u132_endp *endp = hep->hcpriv = u132->endp[endp_number - 1] =
1971 kmalloc(sizeof(struct u132_endp), mem_flags);
1972 if (!endp) {
1973 return -ENOMEM;
1975 INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1976 spin_lock_init(&endp->queue_lock.slock);
1977 INIT_LIST_HEAD(&endp->urb_more);
1978 endp->dequeueing = 0;
1979 endp->edset_flush = 0;
1980 endp->active = 0;
1981 endp->delayed = 0;
1982 endp->endp_number = endp_number;
1983 endp->u132 = u132;
1984 endp->hep = hep;
1985 endp->pipetype = usb_pipetype(urb->pipe);
1986 u132_endp_init_kref(u132, endp);
1987 if (usb_pipein(urb->pipe)) {
1988 endp->toggle_bits = 0x2;
1989 usb_settoggle(udev->usb_device, usb_endp, 0, 0);
1990 ring_number = 3;
1991 endp->input = 1;
1992 endp->output = 0;
1993 udev->endp_number_in[usb_endp] = endp_number;
1994 u132_udev_get_kref(u132, udev);
1995 } else {
1996 endp->toggle_bits = 0x2;
1997 usb_settoggle(udev->usb_device, usb_endp, 1, 0);
1998 ring_number = 2;
1999 endp->input = 0;
2000 endp->output = 1;
2001 udev->endp_number_out[usb_endp] = endp_number;
2002 u132_udev_get_kref(u132, udev);
2004 ring = endp->ring = &u132->ring[ring_number - 1];
2005 if (ring->curr_endp) {
2006 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2007 } else {
2008 INIT_LIST_HEAD(&endp->endp_ring);
2009 ring->curr_endp = endp;
2011 ring->length += 1;
2012 urb->hcpriv = u132;
2013 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2014 endp->udev_number = address;
2015 endp->usb_addr = usb_addr;
2016 endp->usb_endp = usb_endp;
2017 endp->queue_size = 1;
2018 endp->queue_last = 0;
2019 endp->queue_next = 0;
2020 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2021 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2022 u132_endp_queue_work(u132, endp, 0);
2023 return 0;
2026 static int queue_bulk_on_old_endpoint(struct u132 *u132, struct u132_udev *udev,
2027 struct usb_host_endpoint *hep, struct urb *urb,
2028 struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2029 u8 usb_endp, u8 address)
2031 urb->hcpriv = u132;
2032 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2033 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2034 } else {
2035 struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
2036 GFP_ATOMIC);
2037 if (urbq == NULL) {
2038 endp->queue_size -= 1;
2039 return -ENOMEM;
2040 } else {
2041 list_add_tail(&urbq->urb_more, &endp->urb_more);
2042 urbq->urb = urb;
2045 return 0;
2048 static int create_endpoint_and_queue_control(struct u132 *u132,
2049 struct usb_host_endpoint *hep, struct urb *urb,
2050 struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp,
2051 gfp_t mem_flags)
2053 struct u132_ring *ring;
2054 u8 endp_number = ++u132->num_endpoints;
2055 struct u132_endp *endp = hep->hcpriv = u132->endp[endp_number - 1] =
2056 kmalloc(sizeof(struct u132_endp), mem_flags);
2057 if (!endp) {
2058 return -ENOMEM;
2060 INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
2061 spin_lock_init(&endp->queue_lock.slock);
2062 INIT_LIST_HEAD(&endp->urb_more);
2063 ring = endp->ring = &u132->ring[0];
2064 if (ring->curr_endp) {
2065 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2066 } else {
2067 INIT_LIST_HEAD(&endp->endp_ring);
2068 ring->curr_endp = endp;
2070 ring->length += 1;
2071 endp->dequeueing = 0;
2072 endp->edset_flush = 0;
2073 endp->active = 0;
2074 endp->delayed = 0;
2075 endp->endp_number = endp_number;
2076 endp->u132 = u132;
2077 endp->hep = hep;
2078 u132_endp_init_kref(u132, endp);
2079 u132_endp_get_kref(u132, endp);
2080 if (usb_addr == 0) {
2081 unsigned long irqs;
2082 u8 address = u132->addr[usb_addr].address;
2083 struct u132_udev *udev = &u132->udev[address];
2084 endp->udev_number = address;
2085 endp->usb_addr = usb_addr;
2086 endp->usb_endp = usb_endp;
2087 endp->input = 1;
2088 endp->output = 1;
2089 endp->pipetype = usb_pipetype(urb->pipe);
2090 u132_udev_init_kref(u132, udev);
2091 u132_udev_get_kref(u132, udev);
2092 udev->endp_number_in[usb_endp] = endp_number;
2093 udev->endp_number_out[usb_endp] = endp_number;
2094 urb->hcpriv = u132;
2095 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2096 endp->queue_size = 1;
2097 endp->queue_last = 0;
2098 endp->queue_next = 0;
2099 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2100 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2101 u132_endp_queue_work(u132, endp, 0);
2102 return 0;
2103 } else { /*(usb_addr > 0) */
2104 unsigned long irqs;
2105 u8 address = u132->addr[usb_addr].address;
2106 struct u132_udev *udev = &u132->udev[address];
2107 endp->udev_number = address;
2108 endp->usb_addr = usb_addr;
2109 endp->usb_endp = usb_endp;
2110 endp->input = 1;
2111 endp->output = 1;
2112 endp->pipetype = usb_pipetype(urb->pipe);
2113 u132_udev_get_kref(u132, udev);
2114 udev->enumeration = 2;
2115 udev->endp_number_in[usb_endp] = endp_number;
2116 udev->endp_number_out[usb_endp] = endp_number;
2117 urb->hcpriv = u132;
2118 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2119 endp->queue_size = 1;
2120 endp->queue_last = 0;
2121 endp->queue_next = 0;
2122 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2123 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2124 u132_endp_queue_work(u132, endp, 0);
2125 return 0;
2129 static int queue_control_on_old_endpoint(struct u132 *u132,
2130 struct usb_host_endpoint *hep, struct urb *urb,
2131 struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2132 u8 usb_endp)
2134 if (usb_addr == 0) {
2135 if (usb_pipein(urb->pipe)) {
2136 urb->hcpriv = u132;
2137 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2138 endp->urb_list[ENDP_QUEUE_MASK &
2139 endp->queue_last++] = urb;
2140 } else {
2141 struct u132_urbq *urbq =
2142 kmalloc(sizeof(struct u132_urbq),
2143 GFP_ATOMIC);
2144 if (urbq == NULL) {
2145 endp->queue_size -= 1;
2146 return -ENOMEM;
2147 } else {
2148 list_add_tail(&urbq->urb_more,
2149 &endp->urb_more);
2150 urbq->urb = urb;
2153 return 0;
2154 } else { /* usb_pipeout(urb->pipe) */
2155 struct u132_addr *addr = &u132->addr[usb_dev->devnum];
2156 int I = MAX_U132_UDEVS;
2157 int i = 0;
2158 while (--I > 0) {
2159 struct u132_udev *udev = &u132->udev[++i];
2160 if (udev->usb_device) {
2161 continue;
2162 } else {
2163 udev->enumeration = 1;
2164 u132->addr[0].address = i;
2165 endp->udev_number = i;
2166 udev->udev_number = i;
2167 udev->usb_addr = usb_dev->devnum;
2168 u132_udev_init_kref(u132, udev);
2169 udev->endp_number_in[usb_endp] =
2170 endp->endp_number;
2171 u132_udev_get_kref(u132, udev);
2172 udev->endp_number_out[usb_endp] =
2173 endp->endp_number;
2174 udev->usb_device = usb_dev;
2175 ((u8 *) (urb->setup_packet))[2] =
2176 addr->address = i;
2177 u132_udev_get_kref(u132, udev);
2178 break;
2181 if (I == 0) {
2182 dev_err(&u132->platform_dev->dev, "run out of d"
2183 "evice space\n");
2184 return -EINVAL;
2186 urb->hcpriv = u132;
2187 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2188 endp->urb_list[ENDP_QUEUE_MASK &
2189 endp->queue_last++] = urb;
2190 } else {
2191 struct u132_urbq *urbq =
2192 kmalloc(sizeof(struct u132_urbq),
2193 GFP_ATOMIC);
2194 if (urbq == NULL) {
2195 endp->queue_size -= 1;
2196 return -ENOMEM;
2197 } else {
2198 list_add_tail(&urbq->urb_more,
2199 &endp->urb_more);
2200 urbq->urb = urb;
2203 return 0;
2205 } else { /*(usb_addr > 0) */
2206 u8 address = u132->addr[usb_addr].address;
2207 struct u132_udev *udev = &u132->udev[address];
2208 urb->hcpriv = u132;
2209 if (udev->enumeration == 2) {
2210 } else
2211 udev->enumeration = 2;
2212 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2213 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
2214 urb;
2215 } else {
2216 struct u132_urbq *urbq =
2217 kmalloc(sizeof(struct u132_urbq), GFP_ATOMIC);
2218 if (urbq == NULL) {
2219 endp->queue_size -= 1;
2220 return -ENOMEM;
2221 } else {
2222 list_add_tail(&urbq->urb_more, &endp->urb_more);
2223 urbq->urb = urb;
2226 return 0;
2230 static int u132_urb_enqueue(struct usb_hcd *hcd, struct usb_host_endpoint *hep,
2231 struct urb *urb, gfp_t mem_flags)
2233 struct u132 *u132 = hcd_to_u132(hcd);
2234 if (irqs_disabled()) {
2235 if (__GFP_WAIT & mem_flags) {
2236 printk(KERN_ERR "invalid context for function that migh"
2237 "t sleep\n");
2238 return -EINVAL;
2241 if (u132->going > 1) {
2242 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2243 , u132->going);
2244 return -ENODEV;
2245 } else if (u132->going > 0) {
2246 dev_err(&u132->platform_dev->dev, "device is being removed urb="
2247 "%p status=%d\n", urb, urb->status);
2248 return -ESHUTDOWN;
2249 } else {
2250 u8 usb_addr = usb_pipedevice(urb->pipe);
2251 u8 usb_endp = usb_pipeendpoint(urb->pipe);
2252 struct usb_device *usb_dev = urb->dev;
2253 if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2254 u8 address = u132->addr[usb_addr].address;
2255 struct u132_udev *udev = &u132->udev[address];
2256 struct u132_endp *endp = hep->hcpriv;
2257 urb->actual_length = 0;
2258 if (endp) {
2259 unsigned long irqs;
2260 int retval;
2261 spin_lock_irqsave(&endp->queue_lock.slock,
2262 irqs);
2263 retval = queue_int_on_old_endpoint(u132, udev,
2264 hep, urb, usb_dev, endp, usb_addr,
2265 usb_endp, address);
2266 spin_unlock_irqrestore(&endp->queue_lock.slock,
2267 irqs);
2268 if (retval) {
2269 return retval;
2270 } else {
2271 u132_endp_queue_work(u132, endp,
2272 msecs_to_jiffies(urb->interval))
2274 return 0;
2276 } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2277 return -EINVAL;
2278 } else { /*(endp == NULL) */
2279 return create_endpoint_and_queue_int(u132, udev,
2280 hep, urb, usb_dev, usb_addr, usb_endp,
2281 address, mem_flags);
2283 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2284 dev_err(&u132->platform_dev->dev, "the hardware does no"
2285 "t support PIPE_ISOCHRONOUS\n");
2286 return -EINVAL;
2287 } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
2288 u8 address = u132->addr[usb_addr].address;
2289 struct u132_udev *udev = &u132->udev[address];
2290 struct u132_endp *endp = hep->hcpriv;
2291 urb->actual_length = 0;
2292 if (endp) {
2293 unsigned long irqs;
2294 int retval;
2295 spin_lock_irqsave(&endp->queue_lock.slock,
2296 irqs);
2297 retval = queue_bulk_on_old_endpoint(u132, udev,
2298 hep, urb, usb_dev, endp, usb_addr,
2299 usb_endp, address);
2300 spin_unlock_irqrestore(&endp->queue_lock.slock,
2301 irqs);
2302 if (retval) {
2303 return retval;
2304 } else {
2305 u132_endp_queue_work(u132, endp, 0);
2306 return 0;
2308 } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2309 return -EINVAL;
2310 } else
2311 return create_endpoint_and_queue_bulk(u132,
2312 udev, hep, urb, usb_dev, usb_addr,
2313 usb_endp, address, mem_flags);
2314 } else {
2315 struct u132_endp *endp = hep->hcpriv;
2316 u16 urb_size = 8;
2317 u8 *b = urb->setup_packet;
2318 int i = 0;
2319 char data[30 *3 + 4];
2320 char *d = data;
2321 int m = (sizeof(data) - 1) / 3;
2322 int l = 0;
2323 data[0] = 0;
2324 while (urb_size-- > 0) {
2325 if (i > m) {
2326 } else if (i++ < m) {
2327 int w = sprintf(d, " %02X", *b++);
2328 d += w;
2329 l += w;
2330 } else
2331 d += sprintf(d, " ..");
2333 if (endp) {
2334 unsigned long irqs;
2335 int retval;
2336 spin_lock_irqsave(&endp->queue_lock.slock,
2337 irqs);
2338 retval = queue_control_on_old_endpoint(u132,
2339 hep, urb, usb_dev, endp, usb_addr,
2340 usb_endp);
2341 spin_unlock_irqrestore(&endp->queue_lock.slock,
2342 irqs);
2343 if (retval) {
2344 return retval;
2345 } else {
2346 u132_endp_queue_work(u132, endp, 0);
2347 return 0;
2349 } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2350 return -EINVAL;
2351 } else
2352 return create_endpoint_and_queue_control(u132,
2353 hep, urb, usb_dev, usb_addr, usb_endp,
2354 mem_flags);
2359 static int dequeue_from_overflow_chain(struct u132 *u132,
2360 struct u132_endp *endp, struct urb *urb)
2362 struct list_head *scan;
2363 struct list_head *head = &endp->urb_more;
2364 list_for_each(scan, head) {
2365 struct u132_urbq *urbq = list_entry(scan, struct u132_urbq,
2366 urb_more);
2367 if (urbq->urb == urb) {
2368 struct usb_hcd *hcd = u132_to_hcd(u132);
2369 list_del(scan);
2370 endp->queue_size -= 1;
2371 urb->error_count = 0;
2372 urb->hcpriv = NULL;
2373 usb_hcd_giveback_urb(hcd, urb);
2374 return 0;
2375 } else
2376 continue;
2378 dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]=%p ring"
2379 "[%d] %c%c usb_endp=%d usb_addr=%d size=%d next=%04X last=%04X"
2380 "\n", urb, endp->endp_number, endp, endp->ring->number,
2381 endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2382 endp->usb_endp, endp->usb_addr, endp->queue_size,
2383 endp->queue_next, endp->queue_last);
2384 return -EINVAL;
2387 static int u132_endp_urb_dequeue(struct u132 *u132, struct u132_endp *endp,
2388 struct urb *urb)
2390 unsigned long irqs;
2391 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2392 if (endp->queue_size == 0) {
2393 dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]"
2394 "=%p ring[%d] %c%c usb_endp=%d usb_addr=%d\n", urb,
2395 endp->endp_number, endp, endp->ring->number,
2396 endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2397 endp->usb_endp, endp->usb_addr);
2398 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2399 return -EINVAL;
2401 if (urb == endp->urb_list[ENDP_QUEUE_MASK & endp->queue_next]) {
2402 if (endp->active) {
2403 endp->dequeueing = 1;
2404 endp->edset_flush = 1;
2405 u132_endp_queue_work(u132, endp, 0);
2406 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2407 urb->hcpriv = NULL;
2408 return 0;
2409 } else {
2410 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2411 u132_hcd_abandon_urb(u132, endp, urb, urb->status);
2412 return 0;
2414 } else {
2415 u16 queue_list = 0;
2416 u16 queue_size = endp->queue_size;
2417 u16 queue_scan = endp->queue_next;
2418 struct urb **urb_slot = NULL;
2419 while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2420 if (urb == endp->urb_list[ENDP_QUEUE_MASK &
2421 ++queue_scan]) {
2422 urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2423 queue_scan];
2424 break;
2425 } else
2426 continue;
2428 while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2429 *urb_slot = endp->urb_list[ENDP_QUEUE_MASK &
2430 ++queue_scan];
2431 urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2432 queue_scan];
2434 if (urb_slot) {
2435 struct usb_hcd *hcd = u132_to_hcd(u132);
2436 endp->queue_size -= 1;
2437 if (list_empty(&endp->urb_more)) {
2438 spin_unlock_irqrestore(&endp->queue_lock.slock,
2439 irqs);
2440 } else {
2441 struct list_head *next = endp->urb_more.next;
2442 struct u132_urbq *urbq = list_entry(next,
2443 struct u132_urbq, urb_more);
2444 list_del(next);
2445 *urb_slot = urbq->urb;
2446 spin_unlock_irqrestore(&endp->queue_lock.slock,
2447 irqs);
2448 kfree(urbq);
2449 } urb->error_count = 0;
2450 urb->hcpriv = NULL;
2451 usb_hcd_giveback_urb(hcd, urb);
2452 return 0;
2453 } else if (list_empty(&endp->urb_more)) {
2454 dev_err(&u132->platform_dev->dev, "urb=%p not found in "
2455 "endp[%d]=%p ring[%d] %c%c usb_endp=%d usb_addr"
2456 "=%d size=%d next=%04X last=%04X\n", urb,
2457 endp->endp_number, endp, endp->ring->number,
2458 endp->input ? 'I' : ' ',
2459 endp->output ? 'O' : ' ', endp->usb_endp,
2460 endp->usb_addr, endp->queue_size,
2461 endp->queue_next, endp->queue_last);
2462 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2463 return -EINVAL;
2464 } else {
2465 int retval = dequeue_from_overflow_chain(u132, endp,
2466 urb);
2467 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2468 return retval;
2473 static int u132_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
2475 struct u132 *u132 = hcd_to_u132(hcd);
2476 if (u132->going > 2) {
2477 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2478 , u132->going);
2479 return -ENODEV;
2480 } else {
2481 u8 usb_addr = usb_pipedevice(urb->pipe);
2482 u8 usb_endp = usb_pipeendpoint(urb->pipe);
2483 u8 address = u132->addr[usb_addr].address;
2484 struct u132_udev *udev = &u132->udev[address];
2485 if (usb_pipein(urb->pipe)) {
2486 u8 endp_number = udev->endp_number_in[usb_endp];
2487 struct u132_endp *endp = u132->endp[endp_number - 1];
2488 return u132_endp_urb_dequeue(u132, endp, urb);
2489 } else {
2490 u8 endp_number = udev->endp_number_out[usb_endp];
2491 struct u132_endp *endp = u132->endp[endp_number - 1];
2492 return u132_endp_urb_dequeue(u132, endp, urb);
2497 static void u132_endpoint_disable(struct usb_hcd *hcd,
2498 struct usb_host_endpoint *hep)
2500 struct u132 *u132 = hcd_to_u132(hcd);
2501 if (u132->going > 2) {
2502 dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p hep=%p"
2503 ") has been removed %d\n", u132, hcd, hep,
2504 u132->going);
2505 } else {
2506 struct u132_endp *endp = hep->hcpriv;
2507 if (endp)
2508 u132_endp_put_kref(u132, endp);
2512 static int u132_get_frame(struct usb_hcd *hcd)
2514 struct u132 *u132 = hcd_to_u132(hcd);
2515 if (u132->going > 1) {
2516 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2517 , u132->going);
2518 return -ENODEV;
2519 } else if (u132->going > 0) {
2520 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2521 return -ESHUTDOWN;
2522 } else {
2523 int frame = 0;
2524 dev_err(&u132->platform_dev->dev, "TODO: u132_get_frame\n");
2525 msleep(100);
2526 return frame;
2530 static int u132_roothub_descriptor(struct u132 *u132,
2531 struct usb_hub_descriptor *desc)
2533 int retval;
2534 u16 temp;
2535 u32 rh_a = -1;
2536 u32 rh_b = -1;
2537 retval = u132_read_pcimem(u132, roothub.a, &rh_a);
2538 if (retval)
2539 return retval;
2540 desc->bDescriptorType = 0x29;
2541 desc->bPwrOn2PwrGood = (rh_a & RH_A_POTPGT) >> 24;
2542 desc->bHubContrCurrent = 0;
2543 desc->bNbrPorts = u132->num_ports;
2544 temp = 1 + (u132->num_ports / 8);
2545 desc->bDescLength = 7 + 2 *temp;
2546 temp = 0;
2547 if (rh_a & RH_A_NPS)
2548 temp |= 0x0002;
2549 if (rh_a & RH_A_PSM)
2550 temp |= 0x0001;
2551 if (rh_a & RH_A_NOCP) {
2552 temp |= 0x0010;
2553 } else if (rh_a & RH_A_OCPM)
2554 temp |= 0x0008;
2555 desc->wHubCharacteristics = cpu_to_le16(temp);
2556 retval = u132_read_pcimem(u132, roothub.b, &rh_b);
2557 if (retval)
2558 return retval;
2559 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
2560 desc->bitmap[0] = rh_b & RH_B_DR;
2561 if (u132->num_ports > 7) {
2562 desc->bitmap[1] = (rh_b & RH_B_DR) >> 8;
2563 desc->bitmap[2] = 0xff;
2564 } else
2565 desc->bitmap[1] = 0xff;
2566 return 0;
2569 static int u132_roothub_status(struct u132 *u132, __le32 *desc)
2571 u32 rh_status = -1;
2572 int ret_status = u132_read_pcimem(u132, roothub.status, &rh_status);
2573 *desc = cpu_to_le32(rh_status);
2574 return ret_status;
2577 static int u132_roothub_portstatus(struct u132 *u132, __le32 *desc, u16 wIndex)
2579 if (wIndex == 0 || wIndex > u132->num_ports) {
2580 return -EINVAL;
2581 } else {
2582 int port = wIndex - 1;
2583 u32 rh_portstatus = -1;
2584 int ret_portstatus = u132_read_pcimem(u132,
2585 roothub.portstatus[port], &rh_portstatus);
2586 *desc = cpu_to_le32(rh_portstatus);
2587 if (*(u16 *) (desc + 2)) {
2588 dev_info(&u132->platform_dev->dev, "Port %d Status Chan"
2589 "ge = %08X\n", port, *desc);
2591 return ret_portstatus;
2596 /* this timer value might be vendor-specific ... */
2597 #define PORT_RESET_HW_MSEC 10
2598 #define PORT_RESET_MSEC 10
2599 /* wrap-aware logic morphed from <linux/jiffies.h> */
2600 #define tick_before(t1, t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
2601 static int u132_roothub_portreset(struct u132 *u132, int port_index)
2603 int retval;
2604 u32 fmnumber;
2605 u16 now;
2606 u16 reset_done;
2607 retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2608 if (retval)
2609 return retval;
2610 now = fmnumber;
2611 reset_done = now + PORT_RESET_MSEC;
2612 do {
2613 u32 portstat;
2614 do {
2615 retval = u132_read_pcimem(u132,
2616 roothub.portstatus[port_index], &portstat);
2617 if (retval)
2618 return retval;
2619 if (RH_PS_PRS & portstat) {
2620 continue;
2621 } else
2622 break;
2623 } while (tick_before(now, reset_done));
2624 if (RH_PS_PRS & portstat)
2625 return -ENODEV;
2626 if (RH_PS_CCS & portstat) {
2627 if (RH_PS_PRSC & portstat) {
2628 retval = u132_write_pcimem(u132,
2629 roothub.portstatus[port_index],
2630 RH_PS_PRSC);
2631 if (retval)
2632 return retval;
2634 } else
2635 break; /* start the next reset,
2636 sleep till it's probably done */
2637 retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2638 RH_PS_PRS);
2639 if (retval)
2640 return retval;
2641 msleep(PORT_RESET_HW_MSEC);
2642 retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2643 if (retval)
2644 return retval;
2645 now = fmnumber;
2646 } while (tick_before(now, reset_done));
2647 return 0;
2650 static int u132_roothub_setportfeature(struct u132 *u132, u16 wValue,
2651 u16 wIndex)
2653 if (wIndex == 0 || wIndex > u132->num_ports) {
2654 return -EINVAL;
2655 } else {
2656 int retval;
2657 int port_index = wIndex - 1;
2658 struct u132_port *port = &u132->port[port_index];
2659 port->Status &= ~(1 << wValue);
2660 switch (wValue) {
2661 case USB_PORT_FEAT_SUSPEND:
2662 retval = u132_write_pcimem(u132,
2663 roothub.portstatus[port_index], RH_PS_PSS);
2664 if (retval)
2665 return retval;
2666 return 0;
2667 case USB_PORT_FEAT_POWER:
2668 retval = u132_write_pcimem(u132,
2669 roothub.portstatus[port_index], RH_PS_PPS);
2670 if (retval)
2671 return retval;
2672 return 0;
2673 case USB_PORT_FEAT_RESET:
2674 retval = u132_roothub_portreset(u132, port_index);
2675 if (retval)
2676 return retval;
2677 return 0;
2678 default:
2679 return -EPIPE;
2684 static int u132_roothub_clearportfeature(struct u132 *u132, u16 wValue,
2685 u16 wIndex)
2687 if (wIndex == 0 || wIndex > u132->num_ports) {
2688 return -EINVAL;
2689 } else {
2690 int port_index = wIndex - 1;
2691 u32 temp;
2692 int retval;
2693 struct u132_port *port = &u132->port[port_index];
2694 port->Status &= ~(1 << wValue);
2695 switch (wValue) {
2696 case USB_PORT_FEAT_ENABLE:
2697 temp = RH_PS_CCS;
2698 break;
2699 case USB_PORT_FEAT_C_ENABLE:
2700 temp = RH_PS_PESC;
2701 break;
2702 case USB_PORT_FEAT_SUSPEND:
2703 temp = RH_PS_POCI;
2704 if ((u132->hc_control & OHCI_CTRL_HCFS)
2705 != OHCI_USB_OPER) {
2706 dev_err(&u132->platform_dev->dev, "TODO resume_"
2707 "root_hub\n");
2709 break;
2710 case USB_PORT_FEAT_C_SUSPEND:
2711 temp = RH_PS_PSSC;
2712 break;
2713 case USB_PORT_FEAT_POWER:
2714 temp = RH_PS_LSDA;
2715 break;
2716 case USB_PORT_FEAT_C_CONNECTION:
2717 temp = RH_PS_CSC;
2718 break;
2719 case USB_PORT_FEAT_C_OVER_CURRENT:
2720 temp = RH_PS_OCIC;
2721 break;
2722 case USB_PORT_FEAT_C_RESET:
2723 temp = RH_PS_PRSC;
2724 break;
2725 default:
2726 return -EPIPE;
2728 retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2729 temp);
2730 if (retval)
2731 return retval;
2732 return 0;
2737 /* the virtual root hub timer IRQ checks for hub status*/
2738 static int u132_hub_status_data(struct usb_hcd *hcd, char *buf)
2740 struct u132 *u132 = hcd_to_u132(hcd);
2741 if (u132->going > 1) {
2742 dev_err(&u132->platform_dev->dev, "device hcd=%p has been remov"
2743 "ed %d\n", hcd, u132->going);
2744 return -ENODEV;
2745 } else if (u132->going > 0) {
2746 dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
2747 "ed\n", hcd);
2748 return -ESHUTDOWN;
2749 } else {
2750 int i, changed = 0, length = 1;
2751 if (u132->flags & OHCI_QUIRK_AMD756) {
2752 if ((u132->hc_roothub_a & RH_A_NDP) > MAX_ROOT_PORTS) {
2753 dev_err(&u132->platform_dev->dev, "bogus NDP, r"
2754 "ereads as NDP=%d\n",
2755 u132->hc_roothub_a & RH_A_NDP);
2756 goto done;
2759 if (u132->hc_roothub_status & (RH_HS_LPSC | RH_HS_OCIC)) {
2760 buf[0] = changed = 1;
2761 } else
2762 buf[0] = 0;
2763 if (u132->num_ports > 7) {
2764 buf[1] = 0;
2765 length++;
2767 for (i = 0; i < u132->num_ports; i++) {
2768 if (u132->hc_roothub_portstatus[i] & (RH_PS_CSC |
2769 RH_PS_PESC | RH_PS_PSSC | RH_PS_OCIC |
2770 RH_PS_PRSC)) {
2771 changed = 1;
2772 if (i < 7) {
2773 buf[0] |= 1 << (i + 1);
2774 } else
2775 buf[1] |= 1 << (i - 7);
2776 continue;
2778 if (!(u132->hc_roothub_portstatus[i] & RH_PS_CCS)) {
2779 continue;
2781 if ((u132->hc_roothub_portstatus[i] & RH_PS_PSS)) {
2782 continue;
2785 done:return changed ? length : 0;
2789 static int u132_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2790 u16 wIndex, char *buf, u16 wLength)
2792 struct u132 *u132 = hcd_to_u132(hcd);
2793 if (u132->going > 1) {
2794 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2795 , u132->going);
2796 return -ENODEV;
2797 } else if (u132->going > 0) {
2798 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2799 return -ESHUTDOWN;
2800 } else {
2801 int retval = 0;
2802 down(&u132->sw_lock);
2803 switch (typeReq) {
2804 case ClearHubFeature:
2805 switch (wValue) {
2806 case C_HUB_OVER_CURRENT:
2807 case C_HUB_LOCAL_POWER:
2808 break;
2809 default:
2810 goto stall;
2812 break;
2813 case SetHubFeature:
2814 switch (wValue) {
2815 case C_HUB_OVER_CURRENT:
2816 case C_HUB_LOCAL_POWER:
2817 break;
2818 default:
2819 goto stall;
2821 break;
2822 case ClearPortFeature:{
2823 retval = u132_roothub_clearportfeature(u132,
2824 wValue, wIndex);
2825 if (retval)
2826 goto error;
2827 break;
2829 case GetHubDescriptor:{
2830 retval = u132_roothub_descriptor(u132,
2831 (struct usb_hub_descriptor *)buf);
2832 if (retval)
2833 goto error;
2834 break;
2836 case GetHubStatus:{
2837 retval = u132_roothub_status(u132,
2838 (__le32 *) buf);
2839 if (retval)
2840 goto error;
2841 break;
2843 case GetPortStatus:{
2844 retval = u132_roothub_portstatus(u132,
2845 (__le32 *) buf, wIndex);
2846 if (retval)
2847 goto error;
2848 break;
2850 case SetPortFeature:{
2851 retval = u132_roothub_setportfeature(u132,
2852 wValue, wIndex);
2853 if (retval)
2854 goto error;
2855 break;
2857 default:
2858 goto stall;
2859 error:u132_disable(u132);
2860 u132->going = 1;
2861 break;
2862 stall:retval = -EPIPE;
2863 break;
2865 up(&u132->sw_lock);
2866 return retval;
2870 static int u132_start_port_reset(struct usb_hcd *hcd, unsigned port_num)
2872 struct u132 *u132 = hcd_to_u132(hcd);
2873 if (u132->going > 1) {
2874 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2875 , u132->going);
2876 return -ENODEV;
2877 } else if (u132->going > 0) {
2878 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2879 return -ESHUTDOWN;
2880 } else
2881 return 0;
2884 static void u132_hub_irq_enable(struct usb_hcd *hcd)
2886 struct u132 *u132 = hcd_to_u132(hcd);
2887 if (u132->going > 1) {
2888 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2889 , u132->going);
2890 } else if (u132->going > 0)
2891 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2895 #ifdef CONFIG_PM
2896 static int u132_hcd_suspend(struct usb_hcd *hcd, pm_message_t message)
2898 struct u132 *u132 = hcd_to_u132(hcd);
2899 if (u132->going > 1) {
2900 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2901 , u132->going);
2902 return -ENODEV;
2903 } else if (u132->going > 0) {
2904 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2905 return -ESHUTDOWN;
2906 } else
2907 return 0;
2910 static int u132_hcd_resume(struct usb_hcd *hcd)
2912 struct u132 *u132 = hcd_to_u132(hcd);
2913 if (u132->going > 1) {
2914 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2915 , u132->going);
2916 return -ENODEV;
2917 } else if (u132->going > 0) {
2918 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2919 return -ESHUTDOWN;
2920 } else
2921 return 0;
2924 static int u132_bus_suspend(struct usb_hcd *hcd)
2926 struct u132 *u132 = hcd_to_u132(hcd);
2927 if (u132->going > 1) {
2928 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2929 , u132->going);
2930 return -ENODEV;
2931 } else if (u132->going > 0) {
2932 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2933 return -ESHUTDOWN;
2934 } else
2935 return 0;
2938 static int u132_bus_resume(struct usb_hcd *hcd)
2940 struct u132 *u132 = hcd_to_u132(hcd);
2941 if (u132->going > 1) {
2942 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2943 , u132->going);
2944 return -ENODEV;
2945 } else if (u132->going > 0) {
2946 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2947 return -ESHUTDOWN;
2948 } else
2949 return 0;
2952 #else
2953 #define u132_hcd_suspend NULL
2954 #define u132_hcd_resume NULL
2955 #define u132_bus_suspend NULL
2956 #define u132_bus_resume NULL
2957 #endif
2958 static struct hc_driver u132_hc_driver = {
2959 .description = hcd_name,
2960 .hcd_priv_size = sizeof(struct u132),
2961 .irq = NULL,
2962 .flags = HCD_USB11 | HCD_MEMORY,
2963 .reset = u132_hcd_reset,
2964 .start = u132_hcd_start,
2965 .suspend = u132_hcd_suspend,
2966 .resume = u132_hcd_resume,
2967 .stop = u132_hcd_stop,
2968 .urb_enqueue = u132_urb_enqueue,
2969 .urb_dequeue = u132_urb_dequeue,
2970 .endpoint_disable = u132_endpoint_disable,
2971 .get_frame_number = u132_get_frame,
2972 .hub_status_data = u132_hub_status_data,
2973 .hub_control = u132_hub_control,
2974 .bus_suspend = u132_bus_suspend,
2975 .bus_resume = u132_bus_resume,
2976 .start_port_reset = u132_start_port_reset,
2977 .hub_irq_enable = u132_hub_irq_enable,
2981 * This function may be called by the USB core whilst the "usb_all_devices_rwsem"
2982 * is held for writing, thus this module must not call usb_remove_hcd()
2983 * synchronously - but instead should immediately stop activity to the
2984 * device and asynchronously call usb_remove_hcd()
2986 static int __devexit u132_remove(struct platform_device *pdev)
2988 struct usb_hcd *hcd = platform_get_drvdata(pdev);
2989 if (hcd) {
2990 struct u132 *u132 = hcd_to_u132(hcd);
2991 if (u132->going++ > 1) {
2992 dev_err(&u132->platform_dev->dev, "already being remove"
2993 "d\n");
2994 return -ENODEV;
2995 } else {
2996 int rings = MAX_U132_RINGS;
2997 int endps = MAX_U132_ENDPS;
2998 dev_err(&u132->platform_dev->dev, "removing device u132"
2999 ".%d\n", u132->sequence_num);
3000 msleep(100);
3001 down(&u132->sw_lock);
3002 u132_monitor_cancel_work(u132);
3003 while (rings-- > 0) {
3004 struct u132_ring *ring = &u132->ring[rings];
3005 u132_ring_cancel_work(u132, ring);
3006 } while (endps-- > 0) {
3007 struct u132_endp *endp = u132->endp[endps];
3008 if (endp)
3009 u132_endp_cancel_work(u132, endp);
3011 u132->going += 1;
3012 printk(KERN_INFO "removing device u132.%d\n",
3013 u132->sequence_num);
3014 up(&u132->sw_lock);
3015 usb_remove_hcd(hcd);
3016 u132_u132_put_kref(u132);
3017 return 0;
3019 } else
3020 return 0;
3023 static void u132_initialise(struct u132 *u132, struct platform_device *pdev)
3025 int rings = MAX_U132_RINGS;
3026 int ports = MAX_U132_PORTS;
3027 int addrs = MAX_U132_ADDRS;
3028 int udevs = MAX_U132_UDEVS;
3029 int endps = MAX_U132_ENDPS;
3030 u132->board = pdev->dev.platform_data;
3031 u132->platform_dev = pdev;
3032 u132->power = 0;
3033 u132->reset = 0;
3034 init_MUTEX(&u132->sw_lock);
3035 init_MUTEX(&u132->scheduler_lock);
3036 while (rings-- > 0) {
3037 struct u132_ring *ring = &u132->ring[rings];
3038 ring->u132 = u132;
3039 ring->number = rings + 1;
3040 ring->length = 0;
3041 ring->curr_endp = NULL;
3042 INIT_DELAYED_WORK(&ring->scheduler,
3043 u132_hcd_ring_work_scheduler);
3044 } down(&u132->sw_lock);
3045 INIT_DELAYED_WORK(&u132->monitor, u132_hcd_monitor_work);
3046 while (ports-- > 0) {
3047 struct u132_port *port = &u132->port[ports];
3048 port->u132 = u132;
3049 port->reset = 0;
3050 port->enable = 0;
3051 port->power = 0;
3052 port->Status = 0;
3053 } while (addrs-- > 0) {
3054 struct u132_addr *addr = &u132->addr[addrs];
3055 addr->address = 0;
3056 } while (udevs-- > 0) {
3057 struct u132_udev *udev = &u132->udev[udevs];
3058 int i = ARRAY_SIZE(udev->endp_number_in);
3059 int o = ARRAY_SIZE(udev->endp_number_out);
3060 udev->usb_device = NULL;
3061 udev->udev_number = 0;
3062 udev->usb_addr = 0;
3063 udev->portnumber = 0;
3064 while (i-- > 0) {
3065 udev->endp_number_in[i] = 0;
3067 while (o-- > 0) {
3068 udev->endp_number_out[o] = 0;
3071 while (endps-- > 0) {
3072 u132->endp[endps] = NULL;
3074 up(&u132->sw_lock);
3075 return;
3078 static int __devinit u132_probe(struct platform_device *pdev)
3080 struct usb_hcd *hcd;
3081 int retval;
3082 u32 control;
3083 u32 rh_a = -1;
3084 u32 num_ports;
3085 msleep(100);
3086 if (u132_exiting > 0) {
3087 return -ENODEV;
3089 retval = ftdi_write_pcimem(pdev, intrdisable, OHCI_INTR_MIE);
3090 if (retval)
3091 return retval;
3092 retval = ftdi_read_pcimem(pdev, control, &control);
3093 if (retval)
3094 return retval;
3095 retval = ftdi_read_pcimem(pdev, roothub.a, &rh_a);
3096 if (retval)
3097 return retval;
3098 num_ports = rh_a & RH_A_NDP; /* refuse to confuse usbcore */
3099 if (pdev->dev.dma_mask) {
3100 return -EINVAL;
3102 hcd = usb_create_hcd(&u132_hc_driver, &pdev->dev, pdev->dev.bus_id);
3103 if (!hcd) {
3104 printk(KERN_ERR "failed to create the usb hcd struct for U132\n"
3106 ftdi_elan_gone_away(pdev);
3107 return -ENOMEM;
3108 } else {
3109 int retval = 0;
3110 struct u132 *u132 = hcd_to_u132(hcd);
3111 hcd->rsrc_start = 0;
3112 down(&u132_module_lock);
3113 list_add_tail(&u132->u132_list, &u132_static_list);
3114 u132->sequence_num = ++u132_instances;
3115 up(&u132_module_lock);
3116 u132_u132_init_kref(u132);
3117 u132_initialise(u132, pdev);
3118 hcd->product_desc = "ELAN U132 Host Controller";
3119 retval = usb_add_hcd(hcd, 0, 0);
3120 if (retval != 0) {
3121 dev_err(&u132->platform_dev->dev, "init error %d\n",
3122 retval);
3123 u132_u132_put_kref(u132);
3124 return retval;
3125 } else {
3126 u132_monitor_queue_work(u132, 100);
3127 return 0;
3133 #ifdef CONFIG_PM
3134 /* for this device there's no useful distinction between the controller
3135 * and its root hub, except that the root hub only gets direct PM calls
3136 * when CONFIG_USB_SUSPEND is enabled.
3138 static int u132_suspend(struct platform_device *pdev, pm_message_t state)
3140 struct usb_hcd *hcd = platform_get_drvdata(pdev);
3141 struct u132 *u132 = hcd_to_u132(hcd);
3142 if (u132->going > 1) {
3143 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3144 , u132->going);
3145 return -ENODEV;
3146 } else if (u132->going > 0) {
3147 dev_err(&u132->platform_dev->dev, "device is being removed\n");
3148 return -ESHUTDOWN;
3149 } else {
3150 int retval = 0;
3151 if (state.event == PM_EVENT_FREEZE) {
3152 retval = u132_bus_suspend(hcd);
3153 } else if (state.event == PM_EVENT_SUSPEND) {
3154 int ports = MAX_U132_PORTS;
3155 while (ports-- > 0) {
3156 port_power(u132, ports, 0);
3159 if (retval == 0)
3160 pdev->dev.power.power_state = state;
3161 return retval;
3165 static int u132_resume(struct platform_device *pdev)
3167 struct usb_hcd *hcd = platform_get_drvdata(pdev);
3168 struct u132 *u132 = hcd_to_u132(hcd);
3169 if (u132->going > 1) {
3170 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3171 , u132->going);
3172 return -ENODEV;
3173 } else if (u132->going > 0) {
3174 dev_err(&u132->platform_dev->dev, "device is being removed\n");
3175 return -ESHUTDOWN;
3176 } else {
3177 int retval = 0;
3178 if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
3179 int ports = MAX_U132_PORTS;
3180 while (ports-- > 0) {
3181 port_power(u132, ports, 1);
3183 retval = 0;
3184 } else {
3185 pdev->dev.power.power_state = PMSG_ON;
3186 retval = u132_bus_resume(hcd);
3188 return retval;
3192 #else
3193 #define u132_suspend NULL
3194 #define u132_resume NULL
3195 #endif
3197 * this driver is loaded explicitly by ftdi_u132
3199 * the platform_driver struct is static because it is per type of module
3201 static struct platform_driver u132_platform_driver = {
3202 .probe = u132_probe,
3203 .remove = __devexit_p(u132_remove),
3204 .suspend = u132_suspend,
3205 .resume = u132_resume,
3206 .driver = {
3207 .name = (char *)hcd_name,
3208 .owner = THIS_MODULE,
3211 static int __init u132_hcd_init(void)
3213 int retval;
3214 INIT_LIST_HEAD(&u132_static_list);
3215 u132_instances = 0;
3216 u132_exiting = 0;
3217 init_MUTEX(&u132_module_lock);
3218 if (usb_disabled())
3219 return -ENODEV;
3220 printk(KERN_INFO "driver %s built at %s on %s\n", hcd_name, __TIME__,
3221 __DATE__);
3222 workqueue = create_singlethread_workqueue("u132");
3223 retval = platform_driver_register(&u132_platform_driver);
3224 return retval;
3228 module_init(u132_hcd_init);
3229 static void __exit u132_hcd_exit(void)
3231 struct u132 *u132;
3232 struct u132 *temp;
3233 down(&u132_module_lock);
3234 u132_exiting += 1;
3235 up(&u132_module_lock);
3236 list_for_each_entry_safe(u132, temp, &u132_static_list, u132_list) {
3237 platform_device_unregister(u132->platform_dev);
3238 } platform_driver_unregister(&u132_platform_driver);
3239 printk(KERN_INFO "u132-hcd driver deregistered\n");
3240 wait_event(u132_hcd_wait, u132_instances == 0);
3241 flush_workqueue(workqueue);
3242 destroy_workqueue(workqueue);
3246 module_exit(u132_hcd_exit);
3247 MODULE_LICENSE("GPL");