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