RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / usb / host / u132-hcd.c
blob4e39bd240e3f2f277f5e0a6ffe2a395f7e148a5f
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/usb/hcd.h>
53 #include <linux/workqueue.h>
54 #include <linux/platform_device.h>
55 #include <linux/mutex.h>
56 #include <asm/io.h>
57 #include <asm/irq.h>
58 #include <asm/system.h>
59 #include <asm/byteorder.h>
62 #include "ohci.h"
63 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
64 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
65 OHCI_INTR_WDH)
66 MODULE_AUTHOR("Tony Olech - Elan Digital Systems Limited");
67 MODULE_DESCRIPTION("U132 USB Host Controller Driver");
68 MODULE_LICENSE("GPL");
69 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
70 INT_MODULE_PARM(testing, 0);
71 /* Some boards misreport power switching/overcurrent*/
72 static int distrust_firmware = 1;
73 module_param(distrust_firmware, bool, 0);
74 MODULE_PARM_DESC(distrust_firmware, "true to distrust firmware power/overcurren"
75 "t setup");
76 static DECLARE_WAIT_QUEUE_HEAD(u132_hcd_wait);
78 * u132_module_lock exists to protect access to global variables
81 static struct mutex u132_module_lock;
82 static int u132_exiting;
83 static int u132_instances;
84 static struct list_head u132_static_list;
86 * end of the global variables protected by u132_module_lock
88 static struct workqueue_struct *workqueue;
89 #define MAX_U132_PORTS 7
90 #define MAX_U132_ADDRS 128
91 #define MAX_U132_UDEVS 4
92 #define MAX_U132_ENDPS 100
93 #define MAX_U132_RINGS 4
94 static const char *cc_to_text[16] = {
95 "No Error ",
96 "CRC Error ",
97 "Bit Stuff ",
98 "Data Togg ",
99 "Stall ",
100 "DevNotResp ",
101 "PIDCheck ",
102 "UnExpPID ",
103 "DataOver ",
104 "DataUnder ",
105 "(for hw) ",
106 "(for hw) ",
107 "BufferOver ",
108 "BuffUnder ",
109 "(for HCD) ",
110 "(for HCD) "
112 struct u132_port {
113 struct u132 *u132;
114 int reset;
115 int enable;
116 int power;
117 int Status;
119 struct u132_addr {
120 u8 address;
122 struct u132_udev {
123 struct kref kref;
124 struct usb_device *usb_device;
125 u8 enumeration;
126 u8 udev_number;
127 u8 usb_addr;
128 u8 portnumber;
129 u8 endp_number_in[16];
130 u8 endp_number_out[16];
132 #define ENDP_QUEUE_SHIFT 3
133 #define ENDP_QUEUE_SIZE (1<<ENDP_QUEUE_SHIFT)
134 #define ENDP_QUEUE_MASK (ENDP_QUEUE_SIZE-1)
135 struct u132_urbq {
136 struct list_head urb_more;
137 struct urb *urb;
139 struct u132_spin {
140 spinlock_t slock;
142 struct u132_endp {
143 struct kref kref;
144 u8 udev_number;
145 u8 endp_number;
146 u8 usb_addr;
147 u8 usb_endp;
148 struct u132 *u132;
149 struct list_head endp_ring;
150 struct u132_ring *ring;
151 unsigned toggle_bits:2;
152 unsigned active:1;
153 unsigned delayed:1;
154 unsigned input:1;
155 unsigned output:1;
156 unsigned pipetype:2;
157 unsigned dequeueing:1;
158 unsigned edset_flush:1;
159 unsigned spare_bits:14;
160 unsigned long jiffies;
161 struct usb_host_endpoint *hep;
162 struct u132_spin queue_lock;
163 u16 queue_size;
164 u16 queue_last;
165 u16 queue_next;
166 struct urb *urb_list[ENDP_QUEUE_SIZE];
167 struct list_head urb_more;
168 struct delayed_work scheduler;
170 struct u132_ring {
171 unsigned in_use:1;
172 unsigned length:7;
173 u8 number;
174 struct u132 *u132;
175 struct u132_endp *curr_endp;
176 struct delayed_work scheduler;
178 struct u132 {
179 struct kref kref;
180 struct list_head u132_list;
181 struct mutex sw_lock;
182 struct mutex scheduler_lock;
183 struct u132_platform_data *board;
184 struct platform_device *platform_dev;
185 struct u132_ring ring[MAX_U132_RINGS];
186 int sequence_num;
187 int going;
188 int power;
189 int reset;
190 int num_ports;
191 u32 hc_control;
192 u32 hc_fminterval;
193 u32 hc_roothub_status;
194 u32 hc_roothub_a;
195 u32 hc_roothub_portstatus[MAX_ROOT_PORTS];
196 int flags;
197 unsigned long next_statechange;
198 struct delayed_work monitor;
199 int num_endpoints;
200 struct u132_addr addr[MAX_U132_ADDRS];
201 struct u132_udev udev[MAX_U132_UDEVS];
202 struct u132_port port[MAX_U132_PORTS];
203 struct u132_endp *endp[MAX_U132_ENDPS];
207 * these cannot be inlines because we need the structure offset!!
208 * Does anyone have a better way?????
210 #define ftdi_read_pcimem(pdev, member, data) usb_ftdi_elan_read_pcimem(pdev, \
211 offsetof(struct ohci_regs, member), 0, data);
212 #define ftdi_write_pcimem(pdev, member, data) usb_ftdi_elan_write_pcimem(pdev, \
213 offsetof(struct ohci_regs, member), 0, data);
214 #define u132_read_pcimem(u132, member, data) \
215 usb_ftdi_elan_read_pcimem(u132->platform_dev, offsetof(struct \
216 ohci_regs, member), 0, data);
217 #define u132_write_pcimem(u132, member, data) \
218 usb_ftdi_elan_write_pcimem(u132->platform_dev, offsetof(struct \
219 ohci_regs, member), 0, data);
220 static inline struct u132 *udev_to_u132(struct u132_udev *udev)
222 u8 udev_number = udev->udev_number;
223 return container_of(udev, struct u132, udev[udev_number]);
226 static inline struct u132 *hcd_to_u132(struct usb_hcd *hcd)
228 return (struct u132 *)(hcd->hcd_priv);
231 static inline struct usb_hcd *u132_to_hcd(struct u132 *u132)
233 return container_of((void *)u132, struct usb_hcd, hcd_priv);
236 static inline void u132_disable(struct u132 *u132)
238 u132_to_hcd(u132)->state = HC_STATE_HALT;
242 #define kref_to_u132(d) container_of(d, struct u132, kref)
243 #define kref_to_u132_endp(d) container_of(d, struct u132_endp, kref)
244 #define kref_to_u132_udev(d) container_of(d, struct u132_udev, kref)
245 #include "../misc/usb_u132.h"
246 static const char hcd_name[] = "u132_hcd";
247 #define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE | \
248 USB_PORT_STAT_C_SUSPEND | USB_PORT_STAT_C_OVERCURRENT | \
249 USB_PORT_STAT_C_RESET) << 16)
250 static void u132_hcd_delete(struct kref *kref)
252 struct u132 *u132 = kref_to_u132(kref);
253 struct platform_device *pdev = u132->platform_dev;
254 struct usb_hcd *hcd = u132_to_hcd(u132);
255 u132->going += 1;
256 mutex_lock(&u132_module_lock);
257 list_del_init(&u132->u132_list);
258 u132_instances -= 1;
259 mutex_unlock(&u132_module_lock);
260 dev_warn(&u132->platform_dev->dev, "FREEING the hcd=%p and thus the u13"
261 "2=%p going=%d pdev=%p\n", hcd, u132, u132->going, pdev);
262 usb_put_hcd(hcd);
265 static inline void u132_u132_put_kref(struct u132 *u132)
267 kref_put(&u132->kref, u132_hcd_delete);
270 static inline void u132_u132_init_kref(struct u132 *u132)
272 kref_init(&u132->kref);
275 static void u132_udev_delete(struct kref *kref)
277 struct u132_udev *udev = kref_to_u132_udev(kref);
278 udev->udev_number = 0;
279 udev->usb_device = NULL;
280 udev->usb_addr = 0;
281 udev->enumeration = 0;
284 static inline void u132_udev_put_kref(struct u132 *u132, struct u132_udev *udev)
286 kref_put(&udev->kref, u132_udev_delete);
289 static inline void u132_udev_get_kref(struct u132 *u132, struct u132_udev *udev)
291 kref_get(&udev->kref);
294 static inline void u132_udev_init_kref(struct u132 *u132,
295 struct u132_udev *udev)
297 kref_init(&udev->kref);
300 static inline void u132_ring_put_kref(struct u132 *u132, struct u132_ring *ring)
302 kref_put(&u132->kref, u132_hcd_delete);
305 static void u132_ring_requeue_work(struct u132 *u132, struct u132_ring *ring,
306 unsigned int delta)
308 if (delta > 0) {
309 if (queue_delayed_work(workqueue, &ring->scheduler, delta))
310 return;
311 } else if (queue_delayed_work(workqueue, &ring->scheduler, 0))
312 return;
313 kref_put(&u132->kref, u132_hcd_delete);
314 return;
317 static void u132_ring_queue_work(struct u132 *u132, struct u132_ring *ring,
318 unsigned int delta)
320 kref_get(&u132->kref);
321 u132_ring_requeue_work(u132, ring, delta);
322 return;
325 static void u132_ring_cancel_work(struct u132 *u132, struct u132_ring *ring)
327 if (cancel_delayed_work(&ring->scheduler))
328 kref_put(&u132->kref, u132_hcd_delete);
331 static void u132_endp_delete(struct kref *kref)
333 struct u132_endp *endp = kref_to_u132_endp(kref);
334 struct u132 *u132 = endp->u132;
335 u8 usb_addr = endp->usb_addr;
336 u8 usb_endp = endp->usb_endp;
337 u8 address = u132->addr[usb_addr].address;
338 struct u132_udev *udev = &u132->udev[address];
339 u8 endp_number = endp->endp_number;
340 struct usb_host_endpoint *hep = endp->hep;
341 struct u132_ring *ring = endp->ring;
342 struct list_head *head = &endp->endp_ring;
343 ring->length -= 1;
344 if (endp == ring->curr_endp) {
345 if (list_empty(head)) {
346 ring->curr_endp = NULL;
347 list_del(head);
348 } else {
349 struct u132_endp *next_endp = list_entry(head->next,
350 struct u132_endp, endp_ring);
351 ring->curr_endp = next_endp;
352 list_del(head);
354 } else
355 list_del(head);
356 if (endp->input) {
357 udev->endp_number_in[usb_endp] = 0;
358 u132_udev_put_kref(u132, udev);
360 if (endp->output) {
361 udev->endp_number_out[usb_endp] = 0;
362 u132_udev_put_kref(u132, udev);
364 u132->endp[endp_number - 1] = NULL;
365 hep->hcpriv = NULL;
366 kfree(endp);
367 u132_u132_put_kref(u132);
370 static inline void u132_endp_put_kref(struct u132 *u132, struct u132_endp *endp)
372 kref_put(&endp->kref, u132_endp_delete);
375 static inline void u132_endp_get_kref(struct u132 *u132, struct u132_endp *endp)
377 kref_get(&endp->kref);
380 static inline void u132_endp_init_kref(struct u132 *u132,
381 struct u132_endp *endp)
383 kref_init(&endp->kref);
384 kref_get(&u132->kref);
387 static void u132_endp_queue_work(struct u132 *u132, struct u132_endp *endp,
388 unsigned int delta)
390 if (queue_delayed_work(workqueue, &endp->scheduler, delta))
391 kref_get(&endp->kref);
394 static void u132_endp_cancel_work(struct u132 *u132, struct u132_endp *endp)
396 if (cancel_delayed_work(&endp->scheduler))
397 kref_put(&endp->kref, u132_endp_delete);
400 static inline void u132_monitor_put_kref(struct u132 *u132)
402 kref_put(&u132->kref, u132_hcd_delete);
405 static void u132_monitor_queue_work(struct u132 *u132, unsigned int delta)
407 if (queue_delayed_work(workqueue, &u132->monitor, delta))
408 kref_get(&u132->kref);
411 static void u132_monitor_requeue_work(struct u132 *u132, unsigned int delta)
413 if (!queue_delayed_work(workqueue, &u132->monitor, delta))
414 kref_put(&u132->kref, u132_hcd_delete);
417 static void u132_monitor_cancel_work(struct u132 *u132)
419 if (cancel_delayed_work(&u132->monitor))
420 kref_put(&u132->kref, u132_hcd_delete);
423 static int read_roothub_info(struct u132 *u132)
425 u32 revision;
426 int retval;
427 retval = u132_read_pcimem(u132, revision, &revision);
428 if (retval) {
429 dev_err(&u132->platform_dev->dev, "error %d accessing device co"
430 "ntrol\n", retval);
431 return retval;
432 } else if ((revision & 0xFF) == 0x10) {
433 } else if ((revision & 0xFF) == 0x11) {
434 } else {
435 dev_err(&u132->platform_dev->dev, "device revision is not valid"
436 " %08X\n", revision);
437 return -ENODEV;
439 retval = u132_read_pcimem(u132, control, &u132->hc_control);
440 if (retval) {
441 dev_err(&u132->platform_dev->dev, "error %d accessing device co"
442 "ntrol\n", retval);
443 return retval;
445 retval = u132_read_pcimem(u132, roothub.status,
446 &u132->hc_roothub_status);
447 if (retval) {
448 dev_err(&u132->platform_dev->dev, "error %d accessing device re"
449 "g roothub.status\n", retval);
450 return retval;
452 retval = u132_read_pcimem(u132, roothub.a, &u132->hc_roothub_a);
453 if (retval) {
454 dev_err(&u132->platform_dev->dev, "error %d accessing device re"
455 "g roothub.a\n", retval);
456 return retval;
459 int I = u132->num_ports;
460 int i = 0;
461 while (I-- > 0) {
462 retval = u132_read_pcimem(u132, roothub.portstatus[i],
463 &u132->hc_roothub_portstatus[i]);
464 if (retval) {
465 dev_err(&u132->platform_dev->dev, "error %d acc"
466 "essing device roothub.portstatus[%d]\n"
467 , retval, i);
468 return retval;
469 } else
470 i += 1;
473 return 0;
476 static void u132_hcd_monitor_work(struct work_struct *work)
478 struct u132 *u132 = container_of(work, struct u132, monitor.work);
479 if (u132->going > 1) {
480 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
481 , u132->going);
482 u132_monitor_put_kref(u132);
483 return;
484 } else if (u132->going > 0) {
485 dev_err(&u132->platform_dev->dev, "device is being removed\n");
486 u132_monitor_put_kref(u132);
487 return;
488 } else {
489 int retval;
490 mutex_lock(&u132->sw_lock);
491 retval = read_roothub_info(u132);
492 if (retval) {
493 struct usb_hcd *hcd = u132_to_hcd(u132);
494 u132_disable(u132);
495 u132->going = 1;
496 mutex_unlock(&u132->sw_lock);
497 usb_hc_died(hcd);
498 ftdi_elan_gone_away(u132->platform_dev);
499 u132_monitor_put_kref(u132);
500 return;
501 } else {
502 u132_monitor_requeue_work(u132, 500);
503 mutex_unlock(&u132->sw_lock);
504 return;
509 static void u132_hcd_giveback_urb(struct u132 *u132, struct u132_endp *endp,
510 struct urb *urb, int status)
512 struct u132_ring *ring;
513 unsigned long irqs;
514 struct usb_hcd *hcd = u132_to_hcd(u132);
515 urb->error_count = 0;
516 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
517 usb_hcd_unlink_urb_from_ep(hcd, urb);
518 endp->queue_next += 1;
519 if (ENDP_QUEUE_SIZE > --endp->queue_size) {
520 endp->active = 0;
521 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
522 } else {
523 struct list_head *next = endp->urb_more.next;
524 struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
525 urb_more);
526 list_del(next);
527 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
528 urbq->urb;
529 endp->active = 0;
530 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
531 kfree(urbq);
533 mutex_lock(&u132->scheduler_lock);
534 ring = endp->ring;
535 ring->in_use = 0;
536 u132_ring_cancel_work(u132, ring);
537 u132_ring_queue_work(u132, ring, 0);
538 mutex_unlock(&u132->scheduler_lock);
539 u132_endp_put_kref(u132, endp);
540 usb_hcd_giveback_urb(hcd, urb, status);
541 return;
544 static void u132_hcd_forget_urb(struct u132 *u132, struct u132_endp *endp,
545 struct urb *urb, int status)
547 u132_endp_put_kref(u132, endp);
550 static void u132_hcd_abandon_urb(struct u132 *u132, struct u132_endp *endp,
551 struct urb *urb, int status)
553 unsigned long irqs;
554 struct usb_hcd *hcd = u132_to_hcd(u132);
555 urb->error_count = 0;
556 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
557 usb_hcd_unlink_urb_from_ep(hcd, urb);
558 endp->queue_next += 1;
559 if (ENDP_QUEUE_SIZE > --endp->queue_size) {
560 endp->active = 0;
561 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
562 } else {
563 struct list_head *next = endp->urb_more.next;
564 struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
565 urb_more);
566 list_del(next);
567 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
568 urbq->urb;
569 endp->active = 0;
570 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
571 kfree(urbq);
572 } usb_hcd_giveback_urb(hcd, urb, status);
573 return;
576 static inline int edset_input(struct u132 *u132, struct u132_ring *ring,
577 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
578 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
579 int toggle_bits, int error_count, int condition_code, int repeat_number,
580 int halted, int skipped, int actual, int non_null))
582 return usb_ftdi_elan_edset_input(u132->platform_dev, ring->number, endp,
583 urb, address, endp->usb_endp, toggle_bits, callback);
586 static inline int edset_setup(struct u132 *u132, struct u132_ring *ring,
587 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
588 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
589 int toggle_bits, int error_count, int condition_code, int repeat_number,
590 int halted, int skipped, int actual, int non_null))
592 return usb_ftdi_elan_edset_setup(u132->platform_dev, ring->number, endp,
593 urb, address, endp->usb_endp, toggle_bits, callback);
596 static inline int edset_single(struct u132 *u132, struct u132_ring *ring,
597 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
598 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
599 int toggle_bits, int error_count, int condition_code, int repeat_number,
600 int halted, int skipped, int actual, int non_null))
602 return usb_ftdi_elan_edset_single(u132->platform_dev, ring->number,
603 endp, urb, address, endp->usb_endp, toggle_bits, callback);
606 static inline int edset_output(struct u132 *u132, struct u132_ring *ring,
607 struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
608 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
609 int toggle_bits, int error_count, int condition_code, int repeat_number,
610 int halted, int skipped, int actual, int non_null))
612 return usb_ftdi_elan_edset_output(u132->platform_dev, ring->number,
613 endp, urb, address, endp->usb_endp, toggle_bits, callback);
618 * must not LOCK sw_lock
621 static void u132_hcd_interrupt_recv(void *data, struct urb *urb, u8 *buf,
622 int len, int toggle_bits, int error_count, int condition_code,
623 int repeat_number, int halted, int skipped, int actual, int non_null)
625 struct u132_endp *endp = data;
626 struct u132 *u132 = endp->u132;
627 u8 address = u132->addr[endp->usb_addr].address;
628 struct u132_udev *udev = &u132->udev[address];
629 mutex_lock(&u132->scheduler_lock);
630 if (u132->going > 1) {
631 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
632 , u132->going);
633 mutex_unlock(&u132->scheduler_lock);
634 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
635 return;
636 } else if (endp->dequeueing) {
637 endp->dequeueing = 0;
638 mutex_unlock(&u132->scheduler_lock);
639 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
640 return;
641 } else if (u132->going > 0) {
642 dev_err(&u132->platform_dev->dev, "device is being removed "
643 "urb=%p\n", urb);
644 mutex_unlock(&u132->scheduler_lock);
645 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
646 return;
647 } else if (!urb->unlinked) {
648 struct u132_ring *ring = endp->ring;
649 u8 *u = urb->transfer_buffer + urb->actual_length;
650 u8 *b = buf;
651 int L = len;
653 while (L-- > 0)
654 *u++ = *b++;
656 urb->actual_length += len;
657 if ((condition_code == TD_CC_NOERROR) &&
658 (urb->transfer_buffer_length > urb->actual_length)) {
659 endp->toggle_bits = toggle_bits;
660 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
661 1 & toggle_bits);
662 if (urb->actual_length > 0) {
663 int retval;
664 mutex_unlock(&u132->scheduler_lock);
665 retval = edset_single(u132, ring, endp, urb,
666 address, endp->toggle_bits,
667 u132_hcd_interrupt_recv);
668 if (retval != 0)
669 u132_hcd_giveback_urb(u132, endp, urb,
670 retval);
671 } else {
672 ring->in_use = 0;
673 endp->active = 0;
674 endp->jiffies = jiffies +
675 msecs_to_jiffies(urb->interval);
676 u132_ring_cancel_work(u132, ring);
677 u132_ring_queue_work(u132, ring, 0);
678 mutex_unlock(&u132->scheduler_lock);
679 u132_endp_put_kref(u132, endp);
681 return;
682 } else if ((condition_code == TD_DATAUNDERRUN) &&
683 ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
684 endp->toggle_bits = toggle_bits;
685 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
686 1 & toggle_bits);
687 mutex_unlock(&u132->scheduler_lock);
688 u132_hcd_giveback_urb(u132, endp, urb, 0);
689 return;
690 } else {
691 if (condition_code == TD_CC_NOERROR) {
692 endp->toggle_bits = toggle_bits;
693 usb_settoggle(udev->usb_device, endp->usb_endp,
694 0, 1 & toggle_bits);
695 } else if (condition_code == TD_CC_STALL) {
696 endp->toggle_bits = 0x2;
697 usb_settoggle(udev->usb_device, endp->usb_endp,
698 0, 0);
699 } else {
700 endp->toggle_bits = 0x2;
701 usb_settoggle(udev->usb_device, endp->usb_endp,
702 0, 0);
703 dev_err(&u132->platform_dev->dev, "urb=%p givin"
704 "g back INTERRUPT %s\n", urb,
705 cc_to_text[condition_code]);
707 mutex_unlock(&u132->scheduler_lock);
708 u132_hcd_giveback_urb(u132, endp, urb,
709 cc_to_error[condition_code]);
710 return;
712 } else {
713 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
714 "unlinked=%d\n", urb, urb->unlinked);
715 mutex_unlock(&u132->scheduler_lock);
716 u132_hcd_giveback_urb(u132, endp, urb, 0);
717 return;
721 static void u132_hcd_bulk_output_sent(void *data, struct urb *urb, u8 *buf,
722 int len, int toggle_bits, int error_count, int condition_code,
723 int repeat_number, int halted, int skipped, int actual, int non_null)
725 struct u132_endp *endp = data;
726 struct u132 *u132 = endp->u132;
727 u8 address = u132->addr[endp->usb_addr].address;
728 mutex_lock(&u132->scheduler_lock);
729 if (u132->going > 1) {
730 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
731 , u132->going);
732 mutex_unlock(&u132->scheduler_lock);
733 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
734 return;
735 } else if (endp->dequeueing) {
736 endp->dequeueing = 0;
737 mutex_unlock(&u132->scheduler_lock);
738 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
739 return;
740 } else if (u132->going > 0) {
741 dev_err(&u132->platform_dev->dev, "device is being removed "
742 "urb=%p\n", urb);
743 mutex_unlock(&u132->scheduler_lock);
744 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
745 return;
746 } else if (!urb->unlinked) {
747 struct u132_ring *ring = endp->ring;
748 urb->actual_length += len;
749 endp->toggle_bits = toggle_bits;
750 if (urb->transfer_buffer_length > urb->actual_length) {
751 int retval;
752 mutex_unlock(&u132->scheduler_lock);
753 retval = edset_output(u132, ring, endp, urb, address,
754 endp->toggle_bits, u132_hcd_bulk_output_sent);
755 if (retval != 0)
756 u132_hcd_giveback_urb(u132, endp, urb, retval);
757 return;
758 } else {
759 mutex_unlock(&u132->scheduler_lock);
760 u132_hcd_giveback_urb(u132, endp, urb, 0);
761 return;
763 } else {
764 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
765 "unlinked=%d\n", urb, urb->unlinked);
766 mutex_unlock(&u132->scheduler_lock);
767 u132_hcd_giveback_urb(u132, endp, urb, 0);
768 return;
772 static void u132_hcd_bulk_input_recv(void *data, struct urb *urb, u8 *buf,
773 int len, int toggle_bits, int error_count, int condition_code,
774 int repeat_number, int halted, int skipped, int actual, int non_null)
776 struct u132_endp *endp = data;
777 struct u132 *u132 = endp->u132;
778 u8 address = u132->addr[endp->usb_addr].address;
779 struct u132_udev *udev = &u132->udev[address];
780 mutex_lock(&u132->scheduler_lock);
781 if (u132->going > 1) {
782 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
783 , u132->going);
784 mutex_unlock(&u132->scheduler_lock);
785 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
786 return;
787 } else if (endp->dequeueing) {
788 endp->dequeueing = 0;
789 mutex_unlock(&u132->scheduler_lock);
790 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
791 return;
792 } else if (u132->going > 0) {
793 dev_err(&u132->platform_dev->dev, "device is being removed "
794 "urb=%p\n", urb);
795 mutex_unlock(&u132->scheduler_lock);
796 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
797 return;
798 } else if (!urb->unlinked) {
799 struct u132_ring *ring = endp->ring;
800 u8 *u = urb->transfer_buffer + urb->actual_length;
801 u8 *b = buf;
802 int L = len;
804 while (L-- > 0)
805 *u++ = *b++;
807 urb->actual_length += len;
808 if ((condition_code == TD_CC_NOERROR) &&
809 (urb->transfer_buffer_length > urb->actual_length)) {
810 int retval;
811 endp->toggle_bits = toggle_bits;
812 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
813 1 & toggle_bits);
814 mutex_unlock(&u132->scheduler_lock);
815 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
816 ring->number, endp, urb, address,
817 endp->usb_endp, endp->toggle_bits,
818 u132_hcd_bulk_input_recv);
819 if (retval != 0)
820 u132_hcd_giveback_urb(u132, endp, urb, retval);
821 return;
822 } else if (condition_code == TD_CC_NOERROR) {
823 endp->toggle_bits = toggle_bits;
824 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
825 1 & toggle_bits);
826 mutex_unlock(&u132->scheduler_lock);
827 u132_hcd_giveback_urb(u132, endp, urb,
828 cc_to_error[condition_code]);
829 return;
830 } else if ((condition_code == TD_DATAUNDERRUN) &&
831 ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
832 endp->toggle_bits = toggle_bits;
833 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
834 1 & toggle_bits);
835 mutex_unlock(&u132->scheduler_lock);
836 u132_hcd_giveback_urb(u132, endp, urb, 0);
837 return;
838 } else if (condition_code == TD_DATAUNDERRUN) {
839 endp->toggle_bits = toggle_bits;
840 usb_settoggle(udev->usb_device, endp->usb_endp, 0,
841 1 & toggle_bits);
842 dev_warn(&u132->platform_dev->dev, "urb=%p(SHORT NOT OK"
843 ") giving back BULK IN %s\n", urb,
844 cc_to_text[condition_code]);
845 mutex_unlock(&u132->scheduler_lock);
846 u132_hcd_giveback_urb(u132, endp, urb, 0);
847 return;
848 } else if (condition_code == TD_CC_STALL) {
849 endp->toggle_bits = 0x2;
850 usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
851 mutex_unlock(&u132->scheduler_lock);
852 u132_hcd_giveback_urb(u132, endp, urb,
853 cc_to_error[condition_code]);
854 return;
855 } else {
856 endp->toggle_bits = 0x2;
857 usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
858 dev_err(&u132->platform_dev->dev, "urb=%p giving back B"
859 "ULK IN code=%d %s\n", urb, condition_code,
860 cc_to_text[condition_code]);
861 mutex_unlock(&u132->scheduler_lock);
862 u132_hcd_giveback_urb(u132, endp, urb,
863 cc_to_error[condition_code]);
864 return;
866 } else {
867 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
868 "unlinked=%d\n", urb, urb->unlinked);
869 mutex_unlock(&u132->scheduler_lock);
870 u132_hcd_giveback_urb(u132, endp, urb, 0);
871 return;
875 static void u132_hcd_configure_empty_sent(void *data, struct urb *urb, u8 *buf,
876 int len, int toggle_bits, int error_count, int condition_code,
877 int repeat_number, int halted, int skipped, int actual, int non_null)
879 struct u132_endp *endp = data;
880 struct u132 *u132 = endp->u132;
881 mutex_lock(&u132->scheduler_lock);
882 if (u132->going > 1) {
883 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
884 , u132->going);
885 mutex_unlock(&u132->scheduler_lock);
886 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
887 return;
888 } else if (endp->dequeueing) {
889 endp->dequeueing = 0;
890 mutex_unlock(&u132->scheduler_lock);
891 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
892 return;
893 } else if (u132->going > 0) {
894 dev_err(&u132->platform_dev->dev, "device is being removed "
895 "urb=%p\n", urb);
896 mutex_unlock(&u132->scheduler_lock);
897 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
898 return;
899 } else if (!urb->unlinked) {
900 mutex_unlock(&u132->scheduler_lock);
901 u132_hcd_giveback_urb(u132, endp, urb, 0);
902 return;
903 } else {
904 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
905 "unlinked=%d\n", urb, urb->unlinked);
906 mutex_unlock(&u132->scheduler_lock);
907 u132_hcd_giveback_urb(u132, endp, urb, 0);
908 return;
912 static void u132_hcd_configure_input_recv(void *data, struct urb *urb, u8 *buf,
913 int len, int toggle_bits, int error_count, int condition_code,
914 int repeat_number, int halted, int skipped, int actual, int non_null)
916 struct u132_endp *endp = data;
917 struct u132 *u132 = endp->u132;
918 u8 address = u132->addr[endp->usb_addr].address;
919 mutex_lock(&u132->scheduler_lock);
920 if (u132->going > 1) {
921 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
922 , u132->going);
923 mutex_unlock(&u132->scheduler_lock);
924 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
925 return;
926 } else if (endp->dequeueing) {
927 endp->dequeueing = 0;
928 mutex_unlock(&u132->scheduler_lock);
929 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
930 return;
931 } else if (u132->going > 0) {
932 dev_err(&u132->platform_dev->dev, "device is being removed "
933 "urb=%p\n", urb);
934 mutex_unlock(&u132->scheduler_lock);
935 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
936 return;
937 } else if (!urb->unlinked) {
938 struct u132_ring *ring = endp->ring;
939 u8 *u = urb->transfer_buffer;
940 u8 *b = buf;
941 int L = len;
943 while (L-- > 0)
944 *u++ = *b++;
946 urb->actual_length = len;
947 if ((condition_code == TD_CC_NOERROR) || ((condition_code ==
948 TD_DATAUNDERRUN) && ((urb->transfer_flags &
949 URB_SHORT_NOT_OK) == 0))) {
950 int retval;
951 mutex_unlock(&u132->scheduler_lock);
952 retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
953 ring->number, endp, urb, address,
954 endp->usb_endp, 0x3,
955 u132_hcd_configure_empty_sent);
956 if (retval != 0)
957 u132_hcd_giveback_urb(u132, endp, urb, retval);
958 return;
959 } else if (condition_code == TD_CC_STALL) {
960 mutex_unlock(&u132->scheduler_lock);
961 dev_warn(&u132->platform_dev->dev, "giving back SETUP I"
962 "NPUT STALL urb %p\n", urb);
963 u132_hcd_giveback_urb(u132, endp, urb,
964 cc_to_error[condition_code]);
965 return;
966 } else {
967 mutex_unlock(&u132->scheduler_lock);
968 dev_err(&u132->platform_dev->dev, "giving back SETUP IN"
969 "PUT %s urb %p\n", cc_to_text[condition_code],
970 urb);
971 u132_hcd_giveback_urb(u132, endp, urb,
972 cc_to_error[condition_code]);
973 return;
975 } else {
976 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
977 "unlinked=%d\n", urb, urb->unlinked);
978 mutex_unlock(&u132->scheduler_lock);
979 u132_hcd_giveback_urb(u132, endp, urb, 0);
980 return;
984 static void u132_hcd_configure_empty_recv(void *data, struct urb *urb, u8 *buf,
985 int len, int toggle_bits, int error_count, int condition_code,
986 int repeat_number, int halted, int skipped, int actual, int non_null)
988 struct u132_endp *endp = data;
989 struct u132 *u132 = endp->u132;
990 mutex_lock(&u132->scheduler_lock);
991 if (u132->going > 1) {
992 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
993 , u132->going);
994 mutex_unlock(&u132->scheduler_lock);
995 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
996 return;
997 } else if (endp->dequeueing) {
998 endp->dequeueing = 0;
999 mutex_unlock(&u132->scheduler_lock);
1000 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1001 return;
1002 } else if (u132->going > 0) {
1003 dev_err(&u132->platform_dev->dev, "device is being removed "
1004 "urb=%p\n", urb);
1005 mutex_unlock(&u132->scheduler_lock);
1006 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1007 return;
1008 } else if (!urb->unlinked) {
1009 mutex_unlock(&u132->scheduler_lock);
1010 u132_hcd_giveback_urb(u132, endp, urb, 0);
1011 return;
1012 } else {
1013 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1014 "unlinked=%d\n", urb, urb->unlinked);
1015 mutex_unlock(&u132->scheduler_lock);
1016 u132_hcd_giveback_urb(u132, endp, urb, 0);
1017 return;
1021 static void u132_hcd_configure_setup_sent(void *data, struct urb *urb, u8 *buf,
1022 int len, int toggle_bits, int error_count, int condition_code,
1023 int repeat_number, int halted, int skipped, int actual, int non_null)
1025 struct u132_endp *endp = data;
1026 struct u132 *u132 = endp->u132;
1027 u8 address = u132->addr[endp->usb_addr].address;
1028 mutex_lock(&u132->scheduler_lock);
1029 if (u132->going > 1) {
1030 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1031 , u132->going);
1032 mutex_unlock(&u132->scheduler_lock);
1033 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1034 return;
1035 } else if (endp->dequeueing) {
1036 endp->dequeueing = 0;
1037 mutex_unlock(&u132->scheduler_lock);
1038 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1039 return;
1040 } else if (u132->going > 0) {
1041 dev_err(&u132->platform_dev->dev, "device is being removed "
1042 "urb=%p\n", urb);
1043 mutex_unlock(&u132->scheduler_lock);
1044 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1045 return;
1046 } else if (!urb->unlinked) {
1047 if (usb_pipein(urb->pipe)) {
1048 int retval;
1049 struct u132_ring *ring = endp->ring;
1050 mutex_unlock(&u132->scheduler_lock);
1051 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1052 ring->number, endp, urb, address,
1053 endp->usb_endp, 0,
1054 u132_hcd_configure_input_recv);
1055 if (retval != 0)
1056 u132_hcd_giveback_urb(u132, endp, urb, retval);
1057 return;
1058 } else {
1059 int retval;
1060 struct u132_ring *ring = endp->ring;
1061 mutex_unlock(&u132->scheduler_lock);
1062 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1063 ring->number, endp, urb, address,
1064 endp->usb_endp, 0,
1065 u132_hcd_configure_empty_recv);
1066 if (retval != 0)
1067 u132_hcd_giveback_urb(u132, endp, urb, retval);
1068 return;
1070 } else {
1071 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1072 "unlinked=%d\n", urb, urb->unlinked);
1073 mutex_unlock(&u132->scheduler_lock);
1074 u132_hcd_giveback_urb(u132, endp, urb, 0);
1075 return;
1079 static void u132_hcd_enumeration_empty_recv(void *data, struct urb *urb,
1080 u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
1081 int repeat_number, int halted, int skipped, int actual, int non_null)
1083 struct u132_endp *endp = data;
1084 struct u132 *u132 = endp->u132;
1085 u8 address = u132->addr[endp->usb_addr].address;
1086 struct u132_udev *udev = &u132->udev[address];
1087 mutex_lock(&u132->scheduler_lock);
1088 if (u132->going > 1) {
1089 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1090 , u132->going);
1091 mutex_unlock(&u132->scheduler_lock);
1092 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1093 return;
1094 } else if (endp->dequeueing) {
1095 endp->dequeueing = 0;
1096 mutex_unlock(&u132->scheduler_lock);
1097 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1098 return;
1099 } else if (u132->going > 0) {
1100 dev_err(&u132->platform_dev->dev, "device is being removed "
1101 "urb=%p\n", urb);
1102 mutex_unlock(&u132->scheduler_lock);
1103 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1104 return;
1105 } else if (!urb->unlinked) {
1106 u132->addr[0].address = 0;
1107 endp->usb_addr = udev->usb_addr;
1108 mutex_unlock(&u132->scheduler_lock);
1109 u132_hcd_giveback_urb(u132, endp, urb, 0);
1110 return;
1111 } else {
1112 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1113 "unlinked=%d\n", urb, urb->unlinked);
1114 mutex_unlock(&u132->scheduler_lock);
1115 u132_hcd_giveback_urb(u132, endp, urb, 0);
1116 return;
1120 static void u132_hcd_enumeration_address_sent(void *data, struct urb *urb,
1121 u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
1122 int repeat_number, int halted, int skipped, int actual, int non_null)
1124 struct u132_endp *endp = data;
1125 struct u132 *u132 = endp->u132;
1126 mutex_lock(&u132->scheduler_lock);
1127 if (u132->going > 1) {
1128 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1129 , u132->going);
1130 mutex_unlock(&u132->scheduler_lock);
1131 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1132 return;
1133 } else if (endp->dequeueing) {
1134 endp->dequeueing = 0;
1135 mutex_unlock(&u132->scheduler_lock);
1136 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1137 return;
1138 } else if (u132->going > 0) {
1139 dev_err(&u132->platform_dev->dev, "device is being removed "
1140 "urb=%p\n", urb);
1141 mutex_unlock(&u132->scheduler_lock);
1142 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1143 return;
1144 } else if (!urb->unlinked) {
1145 int retval;
1146 struct u132_ring *ring = endp->ring;
1147 mutex_unlock(&u132->scheduler_lock);
1148 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1149 ring->number, endp, urb, 0, endp->usb_endp, 0,
1150 u132_hcd_enumeration_empty_recv);
1151 if (retval != 0)
1152 u132_hcd_giveback_urb(u132, endp, urb, retval);
1153 return;
1154 } else {
1155 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1156 "unlinked=%d\n", urb, urb->unlinked);
1157 mutex_unlock(&u132->scheduler_lock);
1158 u132_hcd_giveback_urb(u132, endp, urb, 0);
1159 return;
1163 static void u132_hcd_initial_empty_sent(void *data, struct urb *urb, u8 *buf,
1164 int len, int toggle_bits, int error_count, int condition_code,
1165 int repeat_number, int halted, int skipped, int actual, int non_null)
1167 struct u132_endp *endp = data;
1168 struct u132 *u132 = endp->u132;
1169 mutex_lock(&u132->scheduler_lock);
1170 if (u132->going > 1) {
1171 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1172 , u132->going);
1173 mutex_unlock(&u132->scheduler_lock);
1174 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1175 return;
1176 } else if (endp->dequeueing) {
1177 endp->dequeueing = 0;
1178 mutex_unlock(&u132->scheduler_lock);
1179 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1180 return;
1181 } else if (u132->going > 0) {
1182 dev_err(&u132->platform_dev->dev, "device is being removed "
1183 "urb=%p\n", urb);
1184 mutex_unlock(&u132->scheduler_lock);
1185 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1186 return;
1187 } else if (!urb->unlinked) {
1188 mutex_unlock(&u132->scheduler_lock);
1189 u132_hcd_giveback_urb(u132, endp, urb, 0);
1190 return;
1191 } else {
1192 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1193 "unlinked=%d\n", urb, urb->unlinked);
1194 mutex_unlock(&u132->scheduler_lock);
1195 u132_hcd_giveback_urb(u132, endp, urb, 0);
1196 return;
1200 static void u132_hcd_initial_input_recv(void *data, struct urb *urb, u8 *buf,
1201 int len, int toggle_bits, int error_count, int condition_code,
1202 int repeat_number, int halted, int skipped, int actual, int non_null)
1204 struct u132_endp *endp = data;
1205 struct u132 *u132 = endp->u132;
1206 u8 address = u132->addr[endp->usb_addr].address;
1207 mutex_lock(&u132->scheduler_lock);
1208 if (u132->going > 1) {
1209 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1210 , u132->going);
1211 mutex_unlock(&u132->scheduler_lock);
1212 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1213 return;
1214 } else if (endp->dequeueing) {
1215 endp->dequeueing = 0;
1216 mutex_unlock(&u132->scheduler_lock);
1217 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1218 return;
1219 } else if (u132->going > 0) {
1220 dev_err(&u132->platform_dev->dev, "device is being removed "
1221 "urb=%p\n", urb);
1222 mutex_unlock(&u132->scheduler_lock);
1223 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1224 return;
1225 } else if (!urb->unlinked) {
1226 int retval;
1227 struct u132_ring *ring = endp->ring;
1228 u8 *u = urb->transfer_buffer;
1229 u8 *b = buf;
1230 int L = len;
1232 while (L-- > 0)
1233 *u++ = *b++;
1235 urb->actual_length = len;
1236 mutex_unlock(&u132->scheduler_lock);
1237 retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
1238 ring->number, endp, urb, address, endp->usb_endp, 0x3,
1239 u132_hcd_initial_empty_sent);
1240 if (retval != 0)
1241 u132_hcd_giveback_urb(u132, endp, urb, retval);
1242 return;
1243 } else {
1244 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1245 "unlinked=%d\n", urb, urb->unlinked);
1246 mutex_unlock(&u132->scheduler_lock);
1247 u132_hcd_giveback_urb(u132, endp, urb, 0);
1248 return;
1252 static void u132_hcd_initial_setup_sent(void *data, struct urb *urb, u8 *buf,
1253 int len, int toggle_bits, int error_count, int condition_code,
1254 int repeat_number, int halted, int skipped, int actual, int non_null)
1256 struct u132_endp *endp = data;
1257 struct u132 *u132 = endp->u132;
1258 u8 address = u132->addr[endp->usb_addr].address;
1259 mutex_lock(&u132->scheduler_lock);
1260 if (u132->going > 1) {
1261 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1262 , u132->going);
1263 mutex_unlock(&u132->scheduler_lock);
1264 u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
1265 return;
1266 } else if (endp->dequeueing) {
1267 endp->dequeueing = 0;
1268 mutex_unlock(&u132->scheduler_lock);
1269 u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
1270 return;
1271 } else if (u132->going > 0) {
1272 dev_err(&u132->platform_dev->dev, "device is being removed "
1273 "urb=%p\n", urb);
1274 mutex_unlock(&u132->scheduler_lock);
1275 u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
1276 return;
1277 } else if (!urb->unlinked) {
1278 int retval;
1279 struct u132_ring *ring = endp->ring;
1280 mutex_unlock(&u132->scheduler_lock);
1281 retval = usb_ftdi_elan_edset_input(u132->platform_dev,
1282 ring->number, endp, urb, address, endp->usb_endp, 0,
1283 u132_hcd_initial_input_recv);
1284 if (retval != 0)
1285 u132_hcd_giveback_urb(u132, endp, urb, retval);
1286 return;
1287 } else {
1288 dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
1289 "unlinked=%d\n", urb, urb->unlinked);
1290 mutex_unlock(&u132->scheduler_lock);
1291 u132_hcd_giveback_urb(u132, endp, urb, 0);
1292 return;
1297 * this work function is only executed from the work queue
1300 static void u132_hcd_ring_work_scheduler(struct work_struct *work)
1302 struct u132_ring *ring =
1303 container_of(work, struct u132_ring, scheduler.work);
1304 struct u132 *u132 = ring->u132;
1305 mutex_lock(&u132->scheduler_lock);
1306 if (ring->in_use) {
1307 mutex_unlock(&u132->scheduler_lock);
1308 u132_ring_put_kref(u132, ring);
1309 return;
1310 } else if (ring->curr_endp) {
1311 struct u132_endp *last_endp = ring->curr_endp;
1312 struct list_head *scan;
1313 struct list_head *head = &last_endp->endp_ring;
1314 unsigned long wakeup = 0;
1315 list_for_each(scan, head) {
1316 struct u132_endp *endp = list_entry(scan,
1317 struct u132_endp, endp_ring);
1318 if (endp->queue_next == endp->queue_last) {
1319 } else if ((endp->delayed == 0)
1320 || time_after_eq(jiffies, endp->jiffies)) {
1321 ring->curr_endp = endp;
1322 u132_endp_cancel_work(u132, last_endp);
1323 u132_endp_queue_work(u132, last_endp, 0);
1324 mutex_unlock(&u132->scheduler_lock);
1325 u132_ring_put_kref(u132, ring);
1326 return;
1327 } else {
1328 unsigned long delta = endp->jiffies - jiffies;
1329 if (delta > wakeup)
1330 wakeup = delta;
1333 if (last_endp->queue_next == last_endp->queue_last) {
1334 } else if ((last_endp->delayed == 0) || time_after_eq(jiffies,
1335 last_endp->jiffies)) {
1336 u132_endp_cancel_work(u132, last_endp);
1337 u132_endp_queue_work(u132, last_endp, 0);
1338 mutex_unlock(&u132->scheduler_lock);
1339 u132_ring_put_kref(u132, ring);
1340 return;
1341 } else {
1342 unsigned long delta = last_endp->jiffies - jiffies;
1343 if (delta > wakeup)
1344 wakeup = delta;
1346 if (wakeup > 0) {
1347 u132_ring_requeue_work(u132, ring, wakeup);
1348 mutex_unlock(&u132->scheduler_lock);
1349 return;
1350 } else {
1351 mutex_unlock(&u132->scheduler_lock);
1352 u132_ring_put_kref(u132, ring);
1353 return;
1355 } else {
1356 mutex_unlock(&u132->scheduler_lock);
1357 u132_ring_put_kref(u132, ring);
1358 return;
1362 static void u132_hcd_endp_work_scheduler(struct work_struct *work)
1364 struct u132_ring *ring;
1365 struct u132_endp *endp =
1366 container_of(work, struct u132_endp, scheduler.work);
1367 struct u132 *u132 = endp->u132;
1368 mutex_lock(&u132->scheduler_lock);
1369 ring = endp->ring;
1370 if (endp->edset_flush) {
1371 endp->edset_flush = 0;
1372 if (endp->dequeueing)
1373 usb_ftdi_elan_edset_flush(u132->platform_dev,
1374 ring->number, endp);
1375 mutex_unlock(&u132->scheduler_lock);
1376 u132_endp_put_kref(u132, endp);
1377 return;
1378 } else if (endp->active) {
1379 mutex_unlock(&u132->scheduler_lock);
1380 u132_endp_put_kref(u132, endp);
1381 return;
1382 } else if (ring->in_use) {
1383 mutex_unlock(&u132->scheduler_lock);
1384 u132_endp_put_kref(u132, endp);
1385 return;
1386 } else if (endp->queue_next == endp->queue_last) {
1387 mutex_unlock(&u132->scheduler_lock);
1388 u132_endp_put_kref(u132, endp);
1389 return;
1390 } else if (endp->pipetype == PIPE_INTERRUPT) {
1391 u8 address = u132->addr[endp->usb_addr].address;
1392 if (ring->in_use) {
1393 mutex_unlock(&u132->scheduler_lock);
1394 u132_endp_put_kref(u132, endp);
1395 return;
1396 } else {
1397 int retval;
1398 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1399 endp->queue_next];
1400 endp->active = 1;
1401 ring->curr_endp = endp;
1402 ring->in_use = 1;
1403 mutex_unlock(&u132->scheduler_lock);
1404 retval = edset_single(u132, ring, endp, urb, address,
1405 endp->toggle_bits, u132_hcd_interrupt_recv);
1406 if (retval != 0)
1407 u132_hcd_giveback_urb(u132, endp, urb, retval);
1408 return;
1410 } else if (endp->pipetype == PIPE_CONTROL) {
1411 u8 address = u132->addr[endp->usb_addr].address;
1412 if (ring->in_use) {
1413 mutex_unlock(&u132->scheduler_lock);
1414 u132_endp_put_kref(u132, endp);
1415 return;
1416 } else if (address == 0) {
1417 int retval;
1418 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1419 endp->queue_next];
1420 endp->active = 1;
1421 ring->curr_endp = endp;
1422 ring->in_use = 1;
1423 mutex_unlock(&u132->scheduler_lock);
1424 retval = edset_setup(u132, ring, endp, urb, address,
1425 0x2, u132_hcd_initial_setup_sent);
1426 if (retval != 0)
1427 u132_hcd_giveback_urb(u132, endp, urb, retval);
1428 return;
1429 } else if (endp->usb_addr == 0) {
1430 int retval;
1431 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1432 endp->queue_next];
1433 endp->active = 1;
1434 ring->curr_endp = endp;
1435 ring->in_use = 1;
1436 mutex_unlock(&u132->scheduler_lock);
1437 retval = edset_setup(u132, ring, endp, urb, 0, 0x2,
1438 u132_hcd_enumeration_address_sent);
1439 if (retval != 0)
1440 u132_hcd_giveback_urb(u132, endp, urb, retval);
1441 return;
1442 } else {
1443 int retval;
1444 struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
1445 endp->queue_next];
1446 address = u132->addr[endp->usb_addr].address;
1447 endp->active = 1;
1448 ring->curr_endp = endp;
1449 ring->in_use = 1;
1450 mutex_unlock(&u132->scheduler_lock);
1451 retval = edset_setup(u132, ring, endp, urb, address,
1452 0x2, u132_hcd_configure_setup_sent);
1453 if (retval != 0)
1454 u132_hcd_giveback_urb(u132, endp, urb, retval);
1455 return;
1457 } else {
1458 if (endp->input) {
1459 u8 address = u132->addr[endp->usb_addr].address;
1460 if (ring->in_use) {
1461 mutex_unlock(&u132->scheduler_lock);
1462 u132_endp_put_kref(u132, endp);
1463 return;
1464 } else {
1465 int retval;
1466 struct urb *urb = endp->urb_list[
1467 ENDP_QUEUE_MASK & endp->queue_next];
1468 endp->active = 1;
1469 ring->curr_endp = endp;
1470 ring->in_use = 1;
1471 mutex_unlock(&u132->scheduler_lock);
1472 retval = edset_input(u132, ring, endp, urb,
1473 address, endp->toggle_bits,
1474 u132_hcd_bulk_input_recv);
1475 if (retval == 0) {
1476 } else
1477 u132_hcd_giveback_urb(u132, endp, urb,
1478 retval);
1479 return;
1481 } else { /* output pipe */
1482 u8 address = u132->addr[endp->usb_addr].address;
1483 if (ring->in_use) {
1484 mutex_unlock(&u132->scheduler_lock);
1485 u132_endp_put_kref(u132, endp);
1486 return;
1487 } else {
1488 int retval;
1489 struct urb *urb = endp->urb_list[
1490 ENDP_QUEUE_MASK & endp->queue_next];
1491 endp->active = 1;
1492 ring->curr_endp = endp;
1493 ring->in_use = 1;
1494 mutex_unlock(&u132->scheduler_lock);
1495 retval = edset_output(u132, ring, endp, urb,
1496 address, endp->toggle_bits,
1497 u132_hcd_bulk_output_sent);
1498 if (retval == 0) {
1499 } else
1500 u132_hcd_giveback_urb(u132, endp, urb,
1501 retval);
1502 return;
1507 #ifdef CONFIG_PM
1509 static void port_power(struct u132 *u132, int pn, int is_on)
1511 u132->port[pn].power = is_on;
1514 #endif
1516 static void u132_power(struct u132 *u132, int is_on)
1518 struct usb_hcd *hcd = u132_to_hcd(u132)
1519 ; /* hub is inactive unless the port is powered */
1520 if (is_on) {
1521 if (u132->power)
1522 return;
1523 u132->power = 1;
1524 } else {
1525 u132->power = 0;
1526 hcd->state = HC_STATE_HALT;
1530 static int u132_periodic_reinit(struct u132 *u132)
1532 int retval;
1533 u32 fi = u132->hc_fminterval & 0x03fff;
1534 u32 fit;
1535 u32 fminterval;
1536 retval = u132_read_pcimem(u132, fminterval, &fminterval);
1537 if (retval)
1538 return retval;
1539 fit = fminterval & FIT;
1540 retval = u132_write_pcimem(u132, fminterval,
1541 (fit ^ FIT) | u132->hc_fminterval);
1542 if (retval)
1543 return retval;
1544 retval = u132_write_pcimem(u132, periodicstart,
1545 ((9 * fi) / 10) & 0x3fff);
1546 if (retval)
1547 return retval;
1548 return 0;
1551 static char *hcfs2string(int state)
1553 switch (state) {
1554 case OHCI_USB_RESET:
1555 return "reset";
1556 case OHCI_USB_RESUME:
1557 return "resume";
1558 case OHCI_USB_OPER:
1559 return "operational";
1560 case OHCI_USB_SUSPEND:
1561 return "suspend";
1563 return "?";
1566 static int u132_init(struct u132 *u132)
1568 int retval;
1569 u32 control;
1570 u132_disable(u132);
1571 u132->next_statechange = jiffies;
1572 retval = u132_write_pcimem(u132, intrdisable, OHCI_INTR_MIE);
1573 if (retval)
1574 return retval;
1575 retval = u132_read_pcimem(u132, control, &control);
1576 if (retval)
1577 return retval;
1578 if (u132->num_ports == 0) {
1579 u32 rh_a = -1;
1580 retval = u132_read_pcimem(u132, roothub.a, &rh_a);
1581 if (retval)
1582 return retval;
1583 u132->num_ports = rh_a & RH_A_NDP;
1584 retval = read_roothub_info(u132);
1585 if (retval)
1586 return retval;
1588 if (u132->num_ports > MAX_U132_PORTS)
1589 return -EINVAL;
1591 return 0;
1595 /* Start an OHCI controller, set the BUS operational
1596 * resets USB and controller
1597 * enable interrupts
1599 static int u132_run(struct u132 *u132)
1601 int retval;
1602 u32 control;
1603 u32 status;
1604 u32 fminterval;
1605 u32 periodicstart;
1606 u32 cmdstatus;
1607 u32 roothub_a;
1608 int mask = OHCI_INTR_INIT;
1609 int first = u132->hc_fminterval == 0;
1610 int sleep_time = 0;
1611 int reset_timeout = 30; /* ... allow extra time */
1612 u132_disable(u132);
1613 if (first) {
1614 u32 temp;
1615 retval = u132_read_pcimem(u132, fminterval, &temp);
1616 if (retval)
1617 return retval;
1618 u132->hc_fminterval = temp & 0x3fff;
1619 u132->hc_fminterval |= FSMP(u132->hc_fminterval) << 16;
1621 retval = u132_read_pcimem(u132, control, &u132->hc_control);
1622 if (retval)
1623 return retval;
1624 dev_info(&u132->platform_dev->dev, "resetting from state '%s', control "
1625 "= %08X\n", hcfs2string(u132->hc_control & OHCI_CTRL_HCFS),
1626 u132->hc_control);
1627 switch (u132->hc_control & OHCI_CTRL_HCFS) {
1628 case OHCI_USB_OPER:
1629 sleep_time = 0;
1630 break;
1631 case OHCI_USB_SUSPEND:
1632 case OHCI_USB_RESUME:
1633 u132->hc_control &= OHCI_CTRL_RWC;
1634 u132->hc_control |= OHCI_USB_RESUME;
1635 sleep_time = 10;
1636 break;
1637 default:
1638 u132->hc_control &= OHCI_CTRL_RWC;
1639 u132->hc_control |= OHCI_USB_RESET;
1640 sleep_time = 50;
1641 break;
1643 retval = u132_write_pcimem(u132, control, u132->hc_control);
1644 if (retval)
1645 return retval;
1646 retval = u132_read_pcimem(u132, control, &control);
1647 if (retval)
1648 return retval;
1649 msleep(sleep_time);
1650 retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1651 if (retval)
1652 return retval;
1653 if (!(roothub_a & RH_A_NPS)) {
1654 int temp; /* power down each port */
1655 for (temp = 0; temp < u132->num_ports; temp++) {
1656 retval = u132_write_pcimem(u132,
1657 roothub.portstatus[temp], RH_PS_LSDA);
1658 if (retval)
1659 return retval;
1662 retval = u132_read_pcimem(u132, control, &control);
1663 if (retval)
1664 return retval;
1665 retry:
1666 retval = u132_read_pcimem(u132, cmdstatus, &status);
1667 if (retval)
1668 return retval;
1669 retval = u132_write_pcimem(u132, cmdstatus, OHCI_HCR);
1670 if (retval)
1671 return retval;
1672 extra: {
1673 retval = u132_read_pcimem(u132, cmdstatus, &status);
1674 if (retval)
1675 return retval;
1676 if (0 != (status & OHCI_HCR)) {
1677 if (--reset_timeout == 0) {
1678 dev_err(&u132->platform_dev->dev, "USB HC reset"
1679 " timed out!\n");
1680 return -ENODEV;
1681 } else {
1682 msleep(5);
1683 goto extra;
1687 if (u132->flags & OHCI_QUIRK_INITRESET) {
1688 retval = u132_write_pcimem(u132, control, u132->hc_control);
1689 if (retval)
1690 return retval;
1691 retval = u132_read_pcimem(u132, control, &control);
1692 if (retval)
1693 return retval;
1695 retval = u132_write_pcimem(u132, ed_controlhead, 0x00000000);
1696 if (retval)
1697 return retval;
1698 retval = u132_write_pcimem(u132, ed_bulkhead, 0x11000000);
1699 if (retval)
1700 return retval;
1701 retval = u132_write_pcimem(u132, hcca, 0x00000000);
1702 if (retval)
1703 return retval;
1704 retval = u132_periodic_reinit(u132);
1705 if (retval)
1706 return retval;
1707 retval = u132_read_pcimem(u132, fminterval, &fminterval);
1708 if (retval)
1709 return retval;
1710 retval = u132_read_pcimem(u132, periodicstart, &periodicstart);
1711 if (retval)
1712 return retval;
1713 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
1714 if (!(u132->flags & OHCI_QUIRK_INITRESET)) {
1715 u132->flags |= OHCI_QUIRK_INITRESET;
1716 goto retry;
1717 } else
1718 dev_err(&u132->platform_dev->dev, "init err(%08x %04x)"
1719 "\n", fminterval, periodicstart);
1720 } /* start controller operations */
1721 u132->hc_control &= OHCI_CTRL_RWC;
1722 u132->hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
1723 retval = u132_write_pcimem(u132, control, u132->hc_control);
1724 if (retval)
1725 return retval;
1726 retval = u132_write_pcimem(u132, cmdstatus, OHCI_BLF);
1727 if (retval)
1728 return retval;
1729 retval = u132_read_pcimem(u132, cmdstatus, &cmdstatus);
1730 if (retval)
1731 return retval;
1732 retval = u132_read_pcimem(u132, control, &control);
1733 if (retval)
1734 return retval;
1735 u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1736 retval = u132_write_pcimem(u132, roothub.status, RH_HS_DRWE);
1737 if (retval)
1738 return retval;
1739 retval = u132_write_pcimem(u132, intrstatus, mask);
1740 if (retval)
1741 return retval;
1742 retval = u132_write_pcimem(u132, intrdisable,
1743 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
1744 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
1745 OHCI_INTR_SO);
1746 if (retval)
1747 return retval; /* handle root hub init quirks ... */
1748 retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1749 if (retval)
1750 return retval;
1751 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
1752 if (u132->flags & OHCI_QUIRK_SUPERIO) {
1753 roothub_a |= RH_A_NOCP;
1754 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
1755 retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1756 if (retval)
1757 return retval;
1758 } else if ((u132->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
1759 roothub_a |= RH_A_NPS;
1760 retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1761 if (retval)
1762 return retval;
1764 retval = u132_write_pcimem(u132, roothub.status, RH_HS_LPSC);
1765 if (retval)
1766 return retval;
1767 retval = u132_write_pcimem(u132, roothub.b,
1768 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
1769 if (retval)
1770 return retval;
1771 retval = u132_read_pcimem(u132, control, &control);
1772 if (retval)
1773 return retval;
1774 mdelay((roothub_a >> 23) & 0x1fe);
1775 u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1776 return 0;
1779 static void u132_hcd_stop(struct usb_hcd *hcd)
1781 struct u132 *u132 = hcd_to_u132(hcd);
1782 if (u132->going > 1) {
1783 dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p) has b"
1784 "een removed %d\n", u132, hcd, u132->going);
1785 } else if (u132->going > 0) {
1786 dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
1787 "ed\n", hcd);
1788 } else {
1789 mutex_lock(&u132->sw_lock);
1790 msleep(100);
1791 u132_power(u132, 0);
1792 mutex_unlock(&u132->sw_lock);
1796 static int u132_hcd_start(struct usb_hcd *hcd)
1798 struct u132 *u132 = hcd_to_u132(hcd);
1799 if (u132->going > 1) {
1800 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1801 , u132->going);
1802 return -ENODEV;
1803 } else if (u132->going > 0) {
1804 dev_err(&u132->platform_dev->dev, "device is being removed\n");
1805 return -ESHUTDOWN;
1806 } else if (hcd->self.controller) {
1807 int retval;
1808 struct platform_device *pdev =
1809 to_platform_device(hcd->self.controller);
1810 u16 vendor = ((struct u132_platform_data *)
1811 (pdev->dev.platform_data))->vendor;
1812 u16 device = ((struct u132_platform_data *)
1813 (pdev->dev.platform_data))->device;
1814 mutex_lock(&u132->sw_lock);
1815 msleep(10);
1816 if (vendor == PCI_VENDOR_ID_AMD && device == 0x740c) {
1817 u132->flags = OHCI_QUIRK_AMD756;
1818 } else if (vendor == PCI_VENDOR_ID_OPTI && device == 0xc861) {
1819 dev_err(&u132->platform_dev->dev, "WARNING: OPTi workar"
1820 "ounds unavailable\n");
1821 } else if (vendor == PCI_VENDOR_ID_COMPAQ && device == 0xa0f8)
1822 u132->flags |= OHCI_QUIRK_ZFMICRO;
1823 retval = u132_run(u132);
1824 if (retval) {
1825 u132_disable(u132);
1826 u132->going = 1;
1828 msleep(100);
1829 mutex_unlock(&u132->sw_lock);
1830 return retval;
1831 } else {
1832 dev_err(&u132->platform_dev->dev, "platform_device missing\n");
1833 return -ENODEV;
1837 static int u132_hcd_reset(struct usb_hcd *hcd)
1839 struct u132 *u132 = hcd_to_u132(hcd);
1840 if (u132->going > 1) {
1841 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1842 , u132->going);
1843 return -ENODEV;
1844 } else if (u132->going > 0) {
1845 dev_err(&u132->platform_dev->dev, "device is being removed\n");
1846 return -ESHUTDOWN;
1847 } else {
1848 int retval;
1849 mutex_lock(&u132->sw_lock);
1850 retval = u132_init(u132);
1851 if (retval) {
1852 u132_disable(u132);
1853 u132->going = 1;
1855 mutex_unlock(&u132->sw_lock);
1856 return retval;
1860 static int create_endpoint_and_queue_int(struct u132 *u132,
1861 struct u132_udev *udev, struct urb *urb,
1862 struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1863 gfp_t mem_flags)
1865 struct u132_ring *ring;
1866 unsigned long irqs;
1867 int rc;
1868 u8 endp_number;
1869 struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
1871 if (!endp)
1872 return -ENOMEM;
1874 spin_lock_init(&endp->queue_lock.slock);
1875 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
1876 rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
1877 if (rc) {
1878 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1879 kfree(endp);
1880 return rc;
1883 endp_number = ++u132->num_endpoints;
1884 urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
1885 INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1886 INIT_LIST_HEAD(&endp->urb_more);
1887 ring = endp->ring = &u132->ring[0];
1888 if (ring->curr_endp) {
1889 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
1890 } else {
1891 INIT_LIST_HEAD(&endp->endp_ring);
1892 ring->curr_endp = endp;
1894 ring->length += 1;
1895 endp->dequeueing = 0;
1896 endp->edset_flush = 0;
1897 endp->active = 0;
1898 endp->delayed = 0;
1899 endp->endp_number = endp_number;
1900 endp->u132 = u132;
1901 endp->hep = urb->ep;
1902 endp->pipetype = usb_pipetype(urb->pipe);
1903 u132_endp_init_kref(u132, endp);
1904 if (usb_pipein(urb->pipe)) {
1905 endp->toggle_bits = 0x2;
1906 usb_settoggle(udev->usb_device, usb_endp, 0, 0);
1907 endp->input = 1;
1908 endp->output = 0;
1909 udev->endp_number_in[usb_endp] = endp_number;
1910 u132_udev_get_kref(u132, udev);
1911 } else {
1912 endp->toggle_bits = 0x2;
1913 usb_settoggle(udev->usb_device, usb_endp, 1, 0);
1914 endp->input = 0;
1915 endp->output = 1;
1916 udev->endp_number_out[usb_endp] = endp_number;
1917 u132_udev_get_kref(u132, udev);
1919 urb->hcpriv = u132;
1920 endp->delayed = 1;
1921 endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1922 endp->udev_number = address;
1923 endp->usb_addr = usb_addr;
1924 endp->usb_endp = usb_endp;
1925 endp->queue_size = 1;
1926 endp->queue_last = 0;
1927 endp->queue_next = 0;
1928 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1929 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1930 u132_endp_queue_work(u132, endp, msecs_to_jiffies(urb->interval));
1931 return 0;
1934 static int queue_int_on_old_endpoint(struct u132 *u132,
1935 struct u132_udev *udev, struct urb *urb,
1936 struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
1937 u8 usb_endp, u8 address)
1939 urb->hcpriv = u132;
1940 endp->delayed = 1;
1941 endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1942 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
1943 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1944 } else {
1945 struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
1946 GFP_ATOMIC);
1947 if (urbq == NULL) {
1948 endp->queue_size -= 1;
1949 return -ENOMEM;
1950 } else {
1951 list_add_tail(&urbq->urb_more, &endp->urb_more);
1952 urbq->urb = urb;
1955 return 0;
1958 static int create_endpoint_and_queue_bulk(struct u132 *u132,
1959 struct u132_udev *udev, struct urb *urb,
1960 struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1961 gfp_t mem_flags)
1963 int ring_number;
1964 struct u132_ring *ring;
1965 unsigned long irqs;
1966 int rc;
1967 u8 endp_number;
1968 struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
1970 if (!endp)
1971 return -ENOMEM;
1973 spin_lock_init(&endp->queue_lock.slock);
1974 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
1975 rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
1976 if (rc) {
1977 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1978 kfree(endp);
1979 return rc;
1982 endp_number = ++u132->num_endpoints;
1983 urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
1984 INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1985 INIT_LIST_HEAD(&endp->urb_more);
1986 endp->dequeueing = 0;
1987 endp->edset_flush = 0;
1988 endp->active = 0;
1989 endp->delayed = 0;
1990 endp->endp_number = endp_number;
1991 endp->u132 = u132;
1992 endp->hep = urb->ep;
1993 endp->pipetype = usb_pipetype(urb->pipe);
1994 u132_endp_init_kref(u132, endp);
1995 if (usb_pipein(urb->pipe)) {
1996 endp->toggle_bits = 0x2;
1997 usb_settoggle(udev->usb_device, usb_endp, 0, 0);
1998 ring_number = 3;
1999 endp->input = 1;
2000 endp->output = 0;
2001 udev->endp_number_in[usb_endp] = endp_number;
2002 u132_udev_get_kref(u132, udev);
2003 } else {
2004 endp->toggle_bits = 0x2;
2005 usb_settoggle(udev->usb_device, usb_endp, 1, 0);
2006 ring_number = 2;
2007 endp->input = 0;
2008 endp->output = 1;
2009 udev->endp_number_out[usb_endp] = endp_number;
2010 u132_udev_get_kref(u132, udev);
2012 ring = endp->ring = &u132->ring[ring_number - 1];
2013 if (ring->curr_endp) {
2014 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2015 } else {
2016 INIT_LIST_HEAD(&endp->endp_ring);
2017 ring->curr_endp = endp;
2019 ring->length += 1;
2020 urb->hcpriv = u132;
2021 endp->udev_number = address;
2022 endp->usb_addr = usb_addr;
2023 endp->usb_endp = usb_endp;
2024 endp->queue_size = 1;
2025 endp->queue_last = 0;
2026 endp->queue_next = 0;
2027 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2028 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2029 u132_endp_queue_work(u132, endp, 0);
2030 return 0;
2033 static int queue_bulk_on_old_endpoint(struct u132 *u132, struct u132_udev *udev,
2034 struct urb *urb,
2035 struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2036 u8 usb_endp, u8 address)
2038 urb->hcpriv = u132;
2039 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2040 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2041 } else {
2042 struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
2043 GFP_ATOMIC);
2044 if (urbq == NULL) {
2045 endp->queue_size -= 1;
2046 return -ENOMEM;
2047 } else {
2048 list_add_tail(&urbq->urb_more, &endp->urb_more);
2049 urbq->urb = urb;
2052 return 0;
2055 static int create_endpoint_and_queue_control(struct u132 *u132,
2056 struct urb *urb,
2057 struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp,
2058 gfp_t mem_flags)
2060 struct u132_ring *ring;
2061 unsigned long irqs;
2062 int rc;
2063 u8 endp_number;
2064 struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
2066 if (!endp)
2067 return -ENOMEM;
2069 spin_lock_init(&endp->queue_lock.slock);
2070 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2071 rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
2072 if (rc) {
2073 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2074 kfree(endp);
2075 return rc;
2078 endp_number = ++u132->num_endpoints;
2079 urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
2080 INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
2081 INIT_LIST_HEAD(&endp->urb_more);
2082 ring = endp->ring = &u132->ring[0];
2083 if (ring->curr_endp) {
2084 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2085 } else {
2086 INIT_LIST_HEAD(&endp->endp_ring);
2087 ring->curr_endp = endp;
2089 ring->length += 1;
2090 endp->dequeueing = 0;
2091 endp->edset_flush = 0;
2092 endp->active = 0;
2093 endp->delayed = 0;
2094 endp->endp_number = endp_number;
2095 endp->u132 = u132;
2096 endp->hep = urb->ep;
2097 u132_endp_init_kref(u132, endp);
2098 u132_endp_get_kref(u132, endp);
2099 if (usb_addr == 0) {
2100 u8 address = u132->addr[usb_addr].address;
2101 struct u132_udev *udev = &u132->udev[address];
2102 endp->udev_number = address;
2103 endp->usb_addr = usb_addr;
2104 endp->usb_endp = usb_endp;
2105 endp->input = 1;
2106 endp->output = 1;
2107 endp->pipetype = usb_pipetype(urb->pipe);
2108 u132_udev_init_kref(u132, udev);
2109 u132_udev_get_kref(u132, udev);
2110 udev->endp_number_in[usb_endp] = endp_number;
2111 udev->endp_number_out[usb_endp] = endp_number;
2112 urb->hcpriv = u132;
2113 endp->queue_size = 1;
2114 endp->queue_last = 0;
2115 endp->queue_next = 0;
2116 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2117 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2118 u132_endp_queue_work(u132, endp, 0);
2119 return 0;
2120 } else { /*(usb_addr > 0) */
2121 u8 address = u132->addr[usb_addr].address;
2122 struct u132_udev *udev = &u132->udev[address];
2123 endp->udev_number = address;
2124 endp->usb_addr = usb_addr;
2125 endp->usb_endp = usb_endp;
2126 endp->input = 1;
2127 endp->output = 1;
2128 endp->pipetype = usb_pipetype(urb->pipe);
2129 u132_udev_get_kref(u132, udev);
2130 udev->enumeration = 2;
2131 udev->endp_number_in[usb_endp] = endp_number;
2132 udev->endp_number_out[usb_endp] = endp_number;
2133 urb->hcpriv = u132;
2134 endp->queue_size = 1;
2135 endp->queue_last = 0;
2136 endp->queue_next = 0;
2137 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2138 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2139 u132_endp_queue_work(u132, endp, 0);
2140 return 0;
2144 static int queue_control_on_old_endpoint(struct u132 *u132,
2145 struct urb *urb,
2146 struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2147 u8 usb_endp)
2149 if (usb_addr == 0) {
2150 if (usb_pipein(urb->pipe)) {
2151 urb->hcpriv = u132;
2152 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2153 endp->urb_list[ENDP_QUEUE_MASK &
2154 endp->queue_last++] = urb;
2155 } else {
2156 struct u132_urbq *urbq =
2157 kmalloc(sizeof(struct u132_urbq),
2158 GFP_ATOMIC);
2159 if (urbq == NULL) {
2160 endp->queue_size -= 1;
2161 return -ENOMEM;
2162 } else {
2163 list_add_tail(&urbq->urb_more,
2164 &endp->urb_more);
2165 urbq->urb = urb;
2168 return 0;
2169 } else { /* usb_pipeout(urb->pipe) */
2170 struct u132_addr *addr = &u132->addr[usb_dev->devnum];
2171 int I = MAX_U132_UDEVS;
2172 int i = 0;
2173 while (--I > 0) {
2174 struct u132_udev *udev = &u132->udev[++i];
2175 if (udev->usb_device) {
2176 continue;
2177 } else {
2178 udev->enumeration = 1;
2179 u132->addr[0].address = i;
2180 endp->udev_number = i;
2181 udev->udev_number = i;
2182 udev->usb_addr = usb_dev->devnum;
2183 u132_udev_init_kref(u132, udev);
2184 udev->endp_number_in[usb_endp] =
2185 endp->endp_number;
2186 u132_udev_get_kref(u132, udev);
2187 udev->endp_number_out[usb_endp] =
2188 endp->endp_number;
2189 udev->usb_device = usb_dev;
2190 ((u8 *) (urb->setup_packet))[2] =
2191 addr->address = i;
2192 u132_udev_get_kref(u132, udev);
2193 break;
2196 if (I == 0) {
2197 dev_err(&u132->platform_dev->dev, "run out of d"
2198 "evice space\n");
2199 return -EINVAL;
2201 urb->hcpriv = u132;
2202 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2203 endp->urb_list[ENDP_QUEUE_MASK &
2204 endp->queue_last++] = urb;
2205 } else {
2206 struct u132_urbq *urbq =
2207 kmalloc(sizeof(struct u132_urbq),
2208 GFP_ATOMIC);
2209 if (urbq == NULL) {
2210 endp->queue_size -= 1;
2211 return -ENOMEM;
2212 } else {
2213 list_add_tail(&urbq->urb_more,
2214 &endp->urb_more);
2215 urbq->urb = urb;
2218 return 0;
2220 } else { /*(usb_addr > 0) */
2221 u8 address = u132->addr[usb_addr].address;
2222 struct u132_udev *udev = &u132->udev[address];
2223 urb->hcpriv = u132;
2224 if (udev->enumeration != 2)
2225 udev->enumeration = 2;
2226 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2227 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
2228 urb;
2229 } else {
2230 struct u132_urbq *urbq =
2231 kmalloc(sizeof(struct u132_urbq), GFP_ATOMIC);
2232 if (urbq == NULL) {
2233 endp->queue_size -= 1;
2234 return -ENOMEM;
2235 } else {
2236 list_add_tail(&urbq->urb_more, &endp->urb_more);
2237 urbq->urb = urb;
2240 return 0;
2244 static int u132_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
2245 gfp_t mem_flags)
2247 struct u132 *u132 = hcd_to_u132(hcd);
2248 if (irqs_disabled()) {
2249 if (__GFP_WAIT & mem_flags) {
2250 printk(KERN_ERR "invalid context for function that migh"
2251 "t sleep\n");
2252 return -EINVAL;
2255 if (u132->going > 1) {
2256 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2257 , u132->going);
2258 return -ENODEV;
2259 } else if (u132->going > 0) {
2260 dev_err(&u132->platform_dev->dev, "device is being removed "
2261 "urb=%p\n", urb);
2262 return -ESHUTDOWN;
2263 } else {
2264 u8 usb_addr = usb_pipedevice(urb->pipe);
2265 u8 usb_endp = usb_pipeendpoint(urb->pipe);
2266 struct usb_device *usb_dev = urb->dev;
2267 if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2268 u8 address = u132->addr[usb_addr].address;
2269 struct u132_udev *udev = &u132->udev[address];
2270 struct u132_endp *endp = urb->ep->hcpriv;
2271 urb->actual_length = 0;
2272 if (endp) {
2273 unsigned long irqs;
2274 int retval;
2275 spin_lock_irqsave(&endp->queue_lock.slock,
2276 irqs);
2277 retval = usb_hcd_link_urb_to_ep(hcd, urb);
2278 if (retval == 0) {
2279 retval = queue_int_on_old_endpoint(
2280 u132, udev, urb,
2281 usb_dev, endp,
2282 usb_addr, usb_endp,
2283 address);
2284 if (retval)
2285 usb_hcd_unlink_urb_from_ep(
2286 hcd, urb);
2288 spin_unlock_irqrestore(&endp->queue_lock.slock,
2289 irqs);
2290 if (retval) {
2291 return retval;
2292 } else {
2293 u132_endp_queue_work(u132, endp,
2294 msecs_to_jiffies(urb->interval))
2296 return 0;
2298 } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2299 return -EINVAL;
2300 } else { /*(endp == NULL) */
2301 return create_endpoint_and_queue_int(u132, udev,
2302 urb, usb_dev, usb_addr,
2303 usb_endp, address, mem_flags);
2305 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2306 dev_err(&u132->platform_dev->dev, "the hardware does no"
2307 "t support PIPE_ISOCHRONOUS\n");
2308 return -EINVAL;
2309 } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
2310 u8 address = u132->addr[usb_addr].address;
2311 struct u132_udev *udev = &u132->udev[address];
2312 struct u132_endp *endp = urb->ep->hcpriv;
2313 urb->actual_length = 0;
2314 if (endp) {
2315 unsigned long irqs;
2316 int retval;
2317 spin_lock_irqsave(&endp->queue_lock.slock,
2318 irqs);
2319 retval = usb_hcd_link_urb_to_ep(hcd, urb);
2320 if (retval == 0) {
2321 retval = queue_bulk_on_old_endpoint(
2322 u132, udev, urb,
2323 usb_dev, endp,
2324 usb_addr, usb_endp,
2325 address);
2326 if (retval)
2327 usb_hcd_unlink_urb_from_ep(
2328 hcd, urb);
2330 spin_unlock_irqrestore(&endp->queue_lock.slock,
2331 irqs);
2332 if (retval) {
2333 return retval;
2334 } else {
2335 u132_endp_queue_work(u132, endp, 0);
2336 return 0;
2338 } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2339 return -EINVAL;
2340 } else
2341 return create_endpoint_and_queue_bulk(u132,
2342 udev, urb, usb_dev, usb_addr,
2343 usb_endp, address, mem_flags);
2344 } else {
2345 struct u132_endp *endp = urb->ep->hcpriv;
2346 u16 urb_size = 8;
2347 u8 *b = urb->setup_packet;
2348 int i = 0;
2349 char data[30 * 3 + 4];
2350 char *d = data;
2351 int m = (sizeof(data) - 1) / 3;
2352 int l = 0;
2353 data[0] = 0;
2354 while (urb_size-- > 0) {
2355 if (i > m) {
2356 } else if (i++ < m) {
2357 int w = sprintf(d, " %02X", *b++);
2358 d += w;
2359 l += w;
2360 } else
2361 d += sprintf(d, " ..");
2363 if (endp) {
2364 unsigned long irqs;
2365 int retval;
2366 spin_lock_irqsave(&endp->queue_lock.slock,
2367 irqs);
2368 retval = usb_hcd_link_urb_to_ep(hcd, urb);
2369 if (retval == 0) {
2370 retval = queue_control_on_old_endpoint(
2371 u132, urb, usb_dev,
2372 endp, usb_addr,
2373 usb_endp);
2374 if (retval)
2375 usb_hcd_unlink_urb_from_ep(
2376 hcd, urb);
2378 spin_unlock_irqrestore(&endp->queue_lock.slock,
2379 irqs);
2380 if (retval) {
2381 return retval;
2382 } else {
2383 u132_endp_queue_work(u132, endp, 0);
2384 return 0;
2386 } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2387 return -EINVAL;
2388 } else
2389 return create_endpoint_and_queue_control(u132,
2390 urb, usb_dev, usb_addr, usb_endp,
2391 mem_flags);
2396 static int dequeue_from_overflow_chain(struct u132 *u132,
2397 struct u132_endp *endp, struct urb *urb)
2399 struct list_head *scan;
2400 struct list_head *head = &endp->urb_more;
2401 list_for_each(scan, head) {
2402 struct u132_urbq *urbq = list_entry(scan, struct u132_urbq,
2403 urb_more);
2404 if (urbq->urb == urb) {
2405 struct usb_hcd *hcd = u132_to_hcd(u132);
2406 list_del(scan);
2407 endp->queue_size -= 1;
2408 urb->error_count = 0;
2409 usb_hcd_giveback_urb(hcd, urb, 0);
2410 return 0;
2411 } else
2412 continue;
2414 dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]=%p ring"
2415 "[%d] %c%c usb_endp=%d usb_addr=%d size=%d next=%04X last=%04X"
2416 "\n", urb, endp->endp_number, endp, endp->ring->number,
2417 endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2418 endp->usb_endp, endp->usb_addr, endp->queue_size,
2419 endp->queue_next, endp->queue_last);
2420 return -EINVAL;
2423 static int u132_endp_urb_dequeue(struct u132 *u132, struct u132_endp *endp,
2424 struct urb *urb, int status)
2426 unsigned long irqs;
2427 int rc;
2429 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2430 rc = usb_hcd_check_unlink_urb(u132_to_hcd(u132), urb, status);
2431 if (rc) {
2432 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2433 return rc;
2435 if (endp->queue_size == 0) {
2436 dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]"
2437 "=%p ring[%d] %c%c usb_endp=%d usb_addr=%d\n", urb,
2438 endp->endp_number, endp, endp->ring->number,
2439 endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2440 endp->usb_endp, endp->usb_addr);
2441 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2442 return -EINVAL;
2444 if (urb == endp->urb_list[ENDP_QUEUE_MASK & endp->queue_next]) {
2445 if (endp->active) {
2446 endp->dequeueing = 1;
2447 endp->edset_flush = 1;
2448 u132_endp_queue_work(u132, endp, 0);
2449 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2450 return 0;
2451 } else {
2452 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2453 u132_hcd_abandon_urb(u132, endp, urb, status);
2454 return 0;
2456 } else {
2457 u16 queue_list = 0;
2458 u16 queue_size = endp->queue_size;
2459 u16 queue_scan = endp->queue_next;
2460 struct urb **urb_slot = NULL;
2461 while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2462 if (urb == endp->urb_list[ENDP_QUEUE_MASK &
2463 ++queue_scan]) {
2464 urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2465 queue_scan];
2466 break;
2467 } else
2468 continue;
2470 while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2471 *urb_slot = endp->urb_list[ENDP_QUEUE_MASK &
2472 ++queue_scan];
2473 urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2474 queue_scan];
2476 if (urb_slot) {
2477 struct usb_hcd *hcd = u132_to_hcd(u132);
2479 usb_hcd_unlink_urb_from_ep(hcd, urb);
2480 endp->queue_size -= 1;
2481 if (list_empty(&endp->urb_more)) {
2482 spin_unlock_irqrestore(&endp->queue_lock.slock,
2483 irqs);
2484 } else {
2485 struct list_head *next = endp->urb_more.next;
2486 struct u132_urbq *urbq = list_entry(next,
2487 struct u132_urbq, urb_more);
2488 list_del(next);
2489 *urb_slot = urbq->urb;
2490 spin_unlock_irqrestore(&endp->queue_lock.slock,
2491 irqs);
2492 kfree(urbq);
2493 } urb->error_count = 0;
2494 usb_hcd_giveback_urb(hcd, urb, status);
2495 return 0;
2496 } else if (list_empty(&endp->urb_more)) {
2497 dev_err(&u132->platform_dev->dev, "urb=%p not found in "
2498 "endp[%d]=%p ring[%d] %c%c usb_endp=%d usb_addr"
2499 "=%d size=%d next=%04X last=%04X\n", urb,
2500 endp->endp_number, endp, endp->ring->number,
2501 endp->input ? 'I' : ' ',
2502 endp->output ? 'O' : ' ', endp->usb_endp,
2503 endp->usb_addr, endp->queue_size,
2504 endp->queue_next, endp->queue_last);
2505 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2506 return -EINVAL;
2507 } else {
2508 int retval;
2510 usb_hcd_unlink_urb_from_ep(u132_to_hcd(u132), urb);
2511 retval = dequeue_from_overflow_chain(u132, endp,
2512 urb);
2513 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2514 return retval;
2519 static int u132_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2521 struct u132 *u132 = hcd_to_u132(hcd);
2522 if (u132->going > 2) {
2523 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2524 , u132->going);
2525 return -ENODEV;
2526 } else {
2527 u8 usb_addr = usb_pipedevice(urb->pipe);
2528 u8 usb_endp = usb_pipeendpoint(urb->pipe);
2529 u8 address = u132->addr[usb_addr].address;
2530 struct u132_udev *udev = &u132->udev[address];
2531 if (usb_pipein(urb->pipe)) {
2532 u8 endp_number = udev->endp_number_in[usb_endp];
2533 struct u132_endp *endp = u132->endp[endp_number - 1];
2534 return u132_endp_urb_dequeue(u132, endp, urb, status);
2535 } else {
2536 u8 endp_number = udev->endp_number_out[usb_endp];
2537 struct u132_endp *endp = u132->endp[endp_number - 1];
2538 return u132_endp_urb_dequeue(u132, endp, urb, status);
2543 static void u132_endpoint_disable(struct usb_hcd *hcd,
2544 struct usb_host_endpoint *hep)
2546 struct u132 *u132 = hcd_to_u132(hcd);
2547 if (u132->going > 2) {
2548 dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p hep=%p"
2549 ") has been removed %d\n", u132, hcd, hep,
2550 u132->going);
2551 } else {
2552 struct u132_endp *endp = hep->hcpriv;
2553 if (endp)
2554 u132_endp_put_kref(u132, endp);
2558 static int u132_get_frame(struct usb_hcd *hcd)
2560 struct u132 *u132 = hcd_to_u132(hcd);
2561 if (u132->going > 1) {
2562 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2563 , u132->going);
2564 return -ENODEV;
2565 } else if (u132->going > 0) {
2566 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2567 return -ESHUTDOWN;
2568 } else {
2569 int frame = 0;
2570 dev_err(&u132->platform_dev->dev, "TODO: u132_get_frame\n");
2571 msleep(100);
2572 return frame;
2576 static int u132_roothub_descriptor(struct u132 *u132,
2577 struct usb_hub_descriptor *desc)
2579 int retval;
2580 u16 temp;
2581 u32 rh_a = -1;
2582 u32 rh_b = -1;
2583 retval = u132_read_pcimem(u132, roothub.a, &rh_a);
2584 if (retval)
2585 return retval;
2586 desc->bDescriptorType = 0x29;
2587 desc->bPwrOn2PwrGood = (rh_a & RH_A_POTPGT) >> 24;
2588 desc->bHubContrCurrent = 0;
2589 desc->bNbrPorts = u132->num_ports;
2590 temp = 1 + (u132->num_ports / 8);
2591 desc->bDescLength = 7 + 2 * temp;
2592 temp = 0;
2593 if (rh_a & RH_A_NPS)
2594 temp |= 0x0002;
2595 if (rh_a & RH_A_PSM)
2596 temp |= 0x0001;
2597 if (rh_a & RH_A_NOCP)
2598 temp |= 0x0010;
2599 else if (rh_a & RH_A_OCPM)
2600 temp |= 0x0008;
2601 desc->wHubCharacteristics = cpu_to_le16(temp);
2602 retval = u132_read_pcimem(u132, roothub.b, &rh_b);
2603 if (retval)
2604 return retval;
2605 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
2606 desc->bitmap[0] = rh_b & RH_B_DR;
2607 if (u132->num_ports > 7) {
2608 desc->bitmap[1] = (rh_b & RH_B_DR) >> 8;
2609 desc->bitmap[2] = 0xff;
2610 } else
2611 desc->bitmap[1] = 0xff;
2612 return 0;
2615 static int u132_roothub_status(struct u132 *u132, __le32 *desc)
2617 u32 rh_status = -1;
2618 int ret_status = u132_read_pcimem(u132, roothub.status, &rh_status);
2619 *desc = cpu_to_le32(rh_status);
2620 return ret_status;
2623 static int u132_roothub_portstatus(struct u132 *u132, __le32 *desc, u16 wIndex)
2625 if (wIndex == 0 || wIndex > u132->num_ports) {
2626 return -EINVAL;
2627 } else {
2628 int port = wIndex - 1;
2629 u32 rh_portstatus = -1;
2630 int ret_portstatus = u132_read_pcimem(u132,
2631 roothub.portstatus[port], &rh_portstatus);
2632 *desc = cpu_to_le32(rh_portstatus);
2633 if (*(u16 *) (desc + 2)) {
2634 dev_info(&u132->platform_dev->dev, "Port %d Status Chan"
2635 "ge = %08X\n", port, *desc);
2637 return ret_portstatus;
2642 /* this timer value might be vendor-specific ... */
2643 #define PORT_RESET_HW_MSEC 10
2644 #define PORT_RESET_MSEC 10
2645 /* wrap-aware logic morphed from <linux/jiffies.h> */
2646 #define tick_before(t1, t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
2647 static int u132_roothub_portreset(struct u132 *u132, int port_index)
2649 int retval;
2650 u32 fmnumber;
2651 u16 now;
2652 u16 reset_done;
2653 retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2654 if (retval)
2655 return retval;
2656 now = fmnumber;
2657 reset_done = now + PORT_RESET_MSEC;
2658 do {
2659 u32 portstat;
2660 do {
2661 retval = u132_read_pcimem(u132,
2662 roothub.portstatus[port_index], &portstat);
2663 if (retval)
2664 return retval;
2665 if (RH_PS_PRS & portstat)
2666 continue;
2667 else
2668 break;
2669 } while (tick_before(now, reset_done));
2670 if (RH_PS_PRS & portstat)
2671 return -ENODEV;
2672 if (RH_PS_CCS & portstat) {
2673 if (RH_PS_PRSC & portstat) {
2674 retval = u132_write_pcimem(u132,
2675 roothub.portstatus[port_index],
2676 RH_PS_PRSC);
2677 if (retval)
2678 return retval;
2680 } else
2681 break; /* start the next reset,
2682 sleep till it's probably done */
2683 retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2684 RH_PS_PRS);
2685 if (retval)
2686 return retval;
2687 msleep(PORT_RESET_HW_MSEC);
2688 retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2689 if (retval)
2690 return retval;
2691 now = fmnumber;
2692 } while (tick_before(now, reset_done));
2693 return 0;
2696 static int u132_roothub_setportfeature(struct u132 *u132, u16 wValue,
2697 u16 wIndex)
2699 if (wIndex == 0 || wIndex > u132->num_ports) {
2700 return -EINVAL;
2701 } else {
2702 int retval;
2703 int port_index = wIndex - 1;
2704 struct u132_port *port = &u132->port[port_index];
2705 port->Status &= ~(1 << wValue);
2706 switch (wValue) {
2707 case USB_PORT_FEAT_SUSPEND:
2708 retval = u132_write_pcimem(u132,
2709 roothub.portstatus[port_index], RH_PS_PSS);
2710 if (retval)
2711 return retval;
2712 return 0;
2713 case USB_PORT_FEAT_POWER:
2714 retval = u132_write_pcimem(u132,
2715 roothub.portstatus[port_index], RH_PS_PPS);
2716 if (retval)
2717 return retval;
2718 return 0;
2719 case USB_PORT_FEAT_RESET:
2720 retval = u132_roothub_portreset(u132, port_index);
2721 if (retval)
2722 return retval;
2723 return 0;
2724 default:
2725 return -EPIPE;
2730 static int u132_roothub_clearportfeature(struct u132 *u132, u16 wValue,
2731 u16 wIndex)
2733 if (wIndex == 0 || wIndex > u132->num_ports) {
2734 return -EINVAL;
2735 } else {
2736 int port_index = wIndex - 1;
2737 u32 temp;
2738 int retval;
2739 struct u132_port *port = &u132->port[port_index];
2740 port->Status &= ~(1 << wValue);
2741 switch (wValue) {
2742 case USB_PORT_FEAT_ENABLE:
2743 temp = RH_PS_CCS;
2744 break;
2745 case USB_PORT_FEAT_C_ENABLE:
2746 temp = RH_PS_PESC;
2747 break;
2748 case USB_PORT_FEAT_SUSPEND:
2749 temp = RH_PS_POCI;
2750 if ((u132->hc_control & OHCI_CTRL_HCFS)
2751 != OHCI_USB_OPER) {
2752 dev_err(&u132->platform_dev->dev, "TODO resume_"
2753 "root_hub\n");
2755 break;
2756 case USB_PORT_FEAT_C_SUSPEND:
2757 temp = RH_PS_PSSC;
2758 break;
2759 case USB_PORT_FEAT_POWER:
2760 temp = RH_PS_LSDA;
2761 break;
2762 case USB_PORT_FEAT_C_CONNECTION:
2763 temp = RH_PS_CSC;
2764 break;
2765 case USB_PORT_FEAT_C_OVER_CURRENT:
2766 temp = RH_PS_OCIC;
2767 break;
2768 case USB_PORT_FEAT_C_RESET:
2769 temp = RH_PS_PRSC;
2770 break;
2771 default:
2772 return -EPIPE;
2774 retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2775 temp);
2776 if (retval)
2777 return retval;
2778 return 0;
2783 /* the virtual root hub timer IRQ checks for hub status*/
2784 static int u132_hub_status_data(struct usb_hcd *hcd, char *buf)
2786 struct u132 *u132 = hcd_to_u132(hcd);
2787 if (u132->going > 1) {
2788 dev_err(&u132->platform_dev->dev, "device hcd=%p has been remov"
2789 "ed %d\n", hcd, u132->going);
2790 return -ENODEV;
2791 } else if (u132->going > 0) {
2792 dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
2793 "ed\n", hcd);
2794 return -ESHUTDOWN;
2795 } else {
2796 int i, changed = 0, length = 1;
2797 if (u132->flags & OHCI_QUIRK_AMD756) {
2798 if ((u132->hc_roothub_a & RH_A_NDP) > MAX_ROOT_PORTS) {
2799 dev_err(&u132->platform_dev->dev, "bogus NDP, r"
2800 "ereads as NDP=%d\n",
2801 u132->hc_roothub_a & RH_A_NDP);
2802 goto done;
2805 if (u132->hc_roothub_status & (RH_HS_LPSC | RH_HS_OCIC))
2806 buf[0] = changed = 1;
2807 else
2808 buf[0] = 0;
2809 if (u132->num_ports > 7) {
2810 buf[1] = 0;
2811 length++;
2813 for (i = 0; i < u132->num_ports; i++) {
2814 if (u132->hc_roothub_portstatus[i] & (RH_PS_CSC |
2815 RH_PS_PESC | RH_PS_PSSC | RH_PS_OCIC |
2816 RH_PS_PRSC)) {
2817 changed = 1;
2818 if (i < 7)
2819 buf[0] |= 1 << (i + 1);
2820 else
2821 buf[1] |= 1 << (i - 7);
2822 continue;
2824 if (!(u132->hc_roothub_portstatus[i] & RH_PS_CCS))
2825 continue;
2827 if ((u132->hc_roothub_portstatus[i] & RH_PS_PSS))
2828 continue;
2830 done:
2831 return changed ? length : 0;
2835 static int u132_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2836 u16 wIndex, char *buf, u16 wLength)
2838 struct u132 *u132 = hcd_to_u132(hcd);
2839 if (u132->going > 1) {
2840 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2841 , u132->going);
2842 return -ENODEV;
2843 } else if (u132->going > 0) {
2844 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2845 return -ESHUTDOWN;
2846 } else {
2847 int retval = 0;
2848 mutex_lock(&u132->sw_lock);
2849 switch (typeReq) {
2850 case ClearHubFeature:
2851 switch (wValue) {
2852 case C_HUB_OVER_CURRENT:
2853 case C_HUB_LOCAL_POWER:
2854 break;
2855 default:
2856 goto stall;
2858 break;
2859 case SetHubFeature:
2860 switch (wValue) {
2861 case C_HUB_OVER_CURRENT:
2862 case C_HUB_LOCAL_POWER:
2863 break;
2864 default:
2865 goto stall;
2867 break;
2868 case ClearPortFeature:{
2869 retval = u132_roothub_clearportfeature(u132,
2870 wValue, wIndex);
2871 if (retval)
2872 goto error;
2873 break;
2875 case GetHubDescriptor:{
2876 retval = u132_roothub_descriptor(u132,
2877 (struct usb_hub_descriptor *)buf);
2878 if (retval)
2879 goto error;
2880 break;
2882 case GetHubStatus:{
2883 retval = u132_roothub_status(u132,
2884 (__le32 *) buf);
2885 if (retval)
2886 goto error;
2887 break;
2889 case GetPortStatus:{
2890 retval = u132_roothub_portstatus(u132,
2891 (__le32 *) buf, wIndex);
2892 if (retval)
2893 goto error;
2894 break;
2896 case SetPortFeature:{
2897 retval = u132_roothub_setportfeature(u132,
2898 wValue, wIndex);
2899 if (retval)
2900 goto error;
2901 break;
2903 default:
2904 goto stall;
2905 error:
2906 u132_disable(u132);
2907 u132->going = 1;
2908 break;
2909 stall:
2910 retval = -EPIPE;
2911 break;
2913 mutex_unlock(&u132->sw_lock);
2914 return retval;
2918 static int u132_start_port_reset(struct usb_hcd *hcd, unsigned port_num)
2920 struct u132 *u132 = hcd_to_u132(hcd);
2921 if (u132->going > 1) {
2922 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2923 , u132->going);
2924 return -ENODEV;
2925 } else if (u132->going > 0) {
2926 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2927 return -ESHUTDOWN;
2928 } else
2929 return 0;
2933 #ifdef CONFIG_PM
2934 static int u132_bus_suspend(struct usb_hcd *hcd)
2936 struct u132 *u132 = hcd_to_u132(hcd);
2937 if (u132->going > 1) {
2938 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2939 , u132->going);
2940 return -ENODEV;
2941 } else if (u132->going > 0) {
2942 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2943 return -ESHUTDOWN;
2944 } else
2945 return 0;
2948 static int u132_bus_resume(struct usb_hcd *hcd)
2950 struct u132 *u132 = hcd_to_u132(hcd);
2951 if (u132->going > 1) {
2952 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2953 , u132->going);
2954 return -ENODEV;
2955 } else if (u132->going > 0) {
2956 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2957 return -ESHUTDOWN;
2958 } else
2959 return 0;
2962 #else
2963 #define u132_bus_suspend NULL
2964 #define u132_bus_resume NULL
2965 #endif
2966 static struct hc_driver u132_hc_driver = {
2967 .description = hcd_name,
2968 .hcd_priv_size = sizeof(struct u132),
2969 .irq = NULL,
2970 .flags = HCD_USB11 | HCD_MEMORY,
2971 .reset = u132_hcd_reset,
2972 .start = u132_hcd_start,
2973 .stop = u132_hcd_stop,
2974 .urb_enqueue = u132_urb_enqueue,
2975 .urb_dequeue = u132_urb_dequeue,
2976 .endpoint_disable = u132_endpoint_disable,
2977 .get_frame_number = u132_get_frame,
2978 .hub_status_data = u132_hub_status_data,
2979 .hub_control = u132_hub_control,
2980 .bus_suspend = u132_bus_suspend,
2981 .bus_resume = u132_bus_resume,
2982 .start_port_reset = u132_start_port_reset,
2986 * This function may be called by the USB core whilst the "usb_all_devices_rwsem"
2987 * is held for writing, thus this module must not call usb_remove_hcd()
2988 * synchronously - but instead should immediately stop activity to the
2989 * device and asynchronously call usb_remove_hcd()
2991 static int __devexit u132_remove(struct platform_device *pdev)
2993 struct usb_hcd *hcd = platform_get_drvdata(pdev);
2994 if (hcd) {
2995 struct u132 *u132 = hcd_to_u132(hcd);
2996 if (u132->going++ > 1) {
2997 dev_err(&u132->platform_dev->dev, "already being remove"
2998 "d\n");
2999 return -ENODEV;
3000 } else {
3001 int rings = MAX_U132_RINGS;
3002 int endps = MAX_U132_ENDPS;
3003 dev_err(&u132->platform_dev->dev, "removing device u132"
3004 ".%d\n", u132->sequence_num);
3005 msleep(100);
3006 mutex_lock(&u132->sw_lock);
3007 u132_monitor_cancel_work(u132);
3008 while (rings-- > 0) {
3009 struct u132_ring *ring = &u132->ring[rings];
3010 u132_ring_cancel_work(u132, ring);
3011 } while (endps-- > 0) {
3012 struct u132_endp *endp = u132->endp[endps];
3013 if (endp)
3014 u132_endp_cancel_work(u132, endp);
3016 u132->going += 1;
3017 printk(KERN_INFO "removing device u132.%d\n",
3018 u132->sequence_num);
3019 mutex_unlock(&u132->sw_lock);
3020 usb_remove_hcd(hcd);
3021 u132_u132_put_kref(u132);
3022 return 0;
3024 } else
3025 return 0;
3028 static void u132_initialise(struct u132 *u132, struct platform_device *pdev)
3030 int rings = MAX_U132_RINGS;
3031 int ports = MAX_U132_PORTS;
3032 int addrs = MAX_U132_ADDRS;
3033 int udevs = MAX_U132_UDEVS;
3034 int endps = MAX_U132_ENDPS;
3035 u132->board = pdev->dev.platform_data;
3036 u132->platform_dev = pdev;
3037 u132->power = 0;
3038 u132->reset = 0;
3039 mutex_init(&u132->sw_lock);
3040 mutex_init(&u132->scheduler_lock);
3041 while (rings-- > 0) {
3042 struct u132_ring *ring = &u132->ring[rings];
3043 ring->u132 = u132;
3044 ring->number = rings + 1;
3045 ring->length = 0;
3046 ring->curr_endp = NULL;
3047 INIT_DELAYED_WORK(&ring->scheduler,
3048 u132_hcd_ring_work_scheduler);
3050 mutex_lock(&u132->sw_lock);
3051 INIT_DELAYED_WORK(&u132->monitor, u132_hcd_monitor_work);
3052 while (ports-- > 0) {
3053 struct u132_port *port = &u132->port[ports];
3054 port->u132 = u132;
3055 port->reset = 0;
3056 port->enable = 0;
3057 port->power = 0;
3058 port->Status = 0;
3060 while (addrs-- > 0) {
3061 struct u132_addr *addr = &u132->addr[addrs];
3062 addr->address = 0;
3064 while (udevs-- > 0) {
3065 struct u132_udev *udev = &u132->udev[udevs];
3066 int i = ARRAY_SIZE(udev->endp_number_in);
3067 int o = ARRAY_SIZE(udev->endp_number_out);
3068 udev->usb_device = NULL;
3069 udev->udev_number = 0;
3070 udev->usb_addr = 0;
3071 udev->portnumber = 0;
3072 while (i-- > 0)
3073 udev->endp_number_in[i] = 0;
3075 while (o-- > 0)
3076 udev->endp_number_out[o] = 0;
3079 while (endps-- > 0)
3080 u132->endp[endps] = NULL;
3082 mutex_unlock(&u132->sw_lock);
3083 return;
3086 static int __devinit u132_probe(struct platform_device *pdev)
3088 struct usb_hcd *hcd;
3089 int retval;
3090 u32 control;
3091 u32 rh_a = -1;
3092 u32 num_ports;
3094 msleep(100);
3095 if (u132_exiting > 0)
3096 return -ENODEV;
3098 retval = ftdi_write_pcimem(pdev, intrdisable, OHCI_INTR_MIE);
3099 if (retval)
3100 return retval;
3101 retval = ftdi_read_pcimem(pdev, control, &control);
3102 if (retval)
3103 return retval;
3104 retval = ftdi_read_pcimem(pdev, roothub.a, &rh_a);
3105 if (retval)
3106 return retval;
3107 num_ports = rh_a & RH_A_NDP; /* refuse to confuse usbcore */
3108 if (pdev->dev.dma_mask)
3109 return -EINVAL;
3111 hcd = usb_create_hcd(&u132_hc_driver, &pdev->dev, dev_name(&pdev->dev));
3112 if (!hcd) {
3113 printk(KERN_ERR "failed to create the usb hcd struct for U132\n"
3115 ftdi_elan_gone_away(pdev);
3116 return -ENOMEM;
3117 } else {
3118 struct u132 *u132 = hcd_to_u132(hcd);
3119 retval = 0;
3120 hcd->rsrc_start = 0;
3121 mutex_lock(&u132_module_lock);
3122 list_add_tail(&u132->u132_list, &u132_static_list);
3123 u132->sequence_num = ++u132_instances;
3124 mutex_unlock(&u132_module_lock);
3125 u132_u132_init_kref(u132);
3126 u132_initialise(u132, pdev);
3127 hcd->product_desc = "ELAN U132 Host Controller";
3128 retval = usb_add_hcd(hcd, 0, 0);
3129 if (retval != 0) {
3130 dev_err(&u132->platform_dev->dev, "init error %d\n",
3131 retval);
3132 u132_u132_put_kref(u132);
3133 return retval;
3134 } else {
3135 u132_monitor_queue_work(u132, 100);
3136 return 0;
3142 #ifdef CONFIG_PM
3143 /* for this device there's no useful distinction between the controller
3144 * and its root hub, except that the root hub only gets direct PM calls
3145 * when CONFIG_USB_SUSPEND is enabled.
3147 static int u132_suspend(struct platform_device *pdev, pm_message_t state)
3149 struct usb_hcd *hcd = platform_get_drvdata(pdev);
3150 struct u132 *u132 = hcd_to_u132(hcd);
3151 if (u132->going > 1) {
3152 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3153 , u132->going);
3154 return -ENODEV;
3155 } else if (u132->going > 0) {
3156 dev_err(&u132->platform_dev->dev, "device is being removed\n");
3157 return -ESHUTDOWN;
3158 } else {
3159 int retval = 0, ports;
3161 switch (state.event) {
3162 case PM_EVENT_FREEZE:
3163 retval = u132_bus_suspend(hcd);
3164 break;
3165 case PM_EVENT_SUSPEND:
3166 case PM_EVENT_HIBERNATE:
3167 ports = MAX_U132_PORTS;
3168 while (ports-- > 0) {
3169 port_power(u132, ports, 0);
3171 break;
3173 return retval;
3177 static int u132_resume(struct platform_device *pdev)
3179 struct usb_hcd *hcd = platform_get_drvdata(pdev);
3180 struct u132 *u132 = hcd_to_u132(hcd);
3181 if (u132->going > 1) {
3182 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3183 , u132->going);
3184 return -ENODEV;
3185 } else if (u132->going > 0) {
3186 dev_err(&u132->platform_dev->dev, "device is being removed\n");
3187 return -ESHUTDOWN;
3188 } else {
3189 int retval = 0;
3190 if (!u132->port[0].power) {
3191 int ports = MAX_U132_PORTS;
3192 while (ports-- > 0) {
3193 port_power(u132, ports, 1);
3195 retval = 0;
3196 } else {
3197 retval = u132_bus_resume(hcd);
3199 return retval;
3203 #else
3204 #define u132_suspend NULL
3205 #define u132_resume NULL
3206 #endif
3208 * this driver is loaded explicitly by ftdi_u132
3210 * the platform_driver struct is static because it is per type of module
3212 static struct platform_driver u132_platform_driver = {
3213 .probe = u132_probe,
3214 .remove = __devexit_p(u132_remove),
3215 .suspend = u132_suspend,
3216 .resume = u132_resume,
3217 .driver = {
3218 .name = (char *)hcd_name,
3219 .owner = THIS_MODULE,
3222 static int __init u132_hcd_init(void)
3224 int retval;
3225 INIT_LIST_HEAD(&u132_static_list);
3226 u132_instances = 0;
3227 u132_exiting = 0;
3228 mutex_init(&u132_module_lock);
3229 if (usb_disabled())
3230 return -ENODEV;
3231 printk(KERN_INFO "driver %s built at %s on %s\n", hcd_name, __TIME__,
3232 __DATE__);
3233 workqueue = create_singlethread_workqueue("u132");
3234 retval = platform_driver_register(&u132_platform_driver);
3235 return retval;
3239 module_init(u132_hcd_init);
3240 static void __exit u132_hcd_exit(void)
3242 struct u132 *u132;
3243 struct u132 *temp;
3244 mutex_lock(&u132_module_lock);
3245 u132_exiting += 1;
3246 mutex_unlock(&u132_module_lock);
3247 list_for_each_entry_safe(u132, temp, &u132_static_list, u132_list) {
3248 platform_device_unregister(u132->platform_dev);
3250 platform_driver_unregister(&u132_platform_driver);
3251 printk(KERN_INFO "u132-hcd driver deregistered\n");
3252 wait_event(u132_hcd_wait, u132_instances == 0);
3253 flush_workqueue(workqueue);
3254 destroy_workqueue(workqueue);
3258 module_exit(u132_hcd_exit);
3259 MODULE_LICENSE("GPL");
3260 MODULE_ALIAS("platform:u132_hcd");