Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/drzeus/mmc
[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 #ifdef CONFIG_PM
1524
1525 static void port_power(struct u132 *u132, int pn, int is_on)
1526 {
1527         u132->port[pn].power = is_on;
1528 }
1529
1530 #endif
1531
1532 static void u132_power(struct u132 *u132, int is_on)
1533 {
1534         struct usb_hcd *hcd = u132_to_hcd(u132)
1535                 ;        /* hub is inactive unless the port is powered */
1536         if (is_on) {
1537                 if (u132->power)
1538                         return;
1539                 u132->power = 1;
1540                 hcd->self.controller->power.power_state = PMSG_ON;
1541         } else {
1542                 u132->power = 0;
1543                 hcd->state = HC_STATE_HALT;
1544                 hcd->self.controller->power.power_state = PMSG_SUSPEND;
1545         }
1546 }
1547
1548 static int u132_periodic_reinit(struct u132 *u132)
1549 {
1550         int retval;
1551         u32 fi = u132->hc_fminterval & 0x03fff;
1552         u32 fit;
1553         u32 fminterval;
1554         retval = u132_read_pcimem(u132, fminterval, &fminterval);
1555         if (retval)
1556                 return retval;
1557         fit = fminterval & FIT;
1558         retval = u132_write_pcimem(u132, fminterval,
1559                 (fit ^ FIT) | u132->hc_fminterval);
1560         if (retval)
1561                 return retval;
1562         retval = u132_write_pcimem(u132, periodicstart,
1563                 ((9 *fi) / 10) & 0x3fff);
1564         if (retval)
1565                 return retval;
1566         return 0;
1567 }
1568
1569 static char *hcfs2string(int state)
1570 {
1571         switch (state) {
1572         case OHCI_USB_RESET:
1573                 return "reset";
1574         case OHCI_USB_RESUME:
1575                 return "resume";
1576         case OHCI_USB_OPER:
1577                 return "operational";
1578         case OHCI_USB_SUSPEND:
1579                 return "suspend";
1580         }
1581         return "?";
1582 }
1583
1584 static int u132_init(struct u132 *u132)
1585 {
1586         int retval;
1587         u32 control;
1588         u132_disable(u132);
1589         u132->next_statechange = jiffies;
1590         retval = u132_write_pcimem(u132, intrdisable, OHCI_INTR_MIE);
1591         if (retval)
1592                 return retval;
1593         retval = u132_read_pcimem(u132, control, &control);
1594         if (retval)
1595                 return retval;
1596         if (u132->num_ports == 0) {
1597                 u32 rh_a = -1;
1598                 retval = u132_read_pcimem(u132, roothub.a, &rh_a);
1599                 if (retval)
1600                         return retval;
1601                 u132->num_ports = rh_a & RH_A_NDP;
1602                 retval = read_roothub_info(u132);
1603                 if (retval)
1604                         return retval;
1605         }
1606         if (u132->num_ports > MAX_U132_PORTS) {
1607                 return -EINVAL;
1608         }
1609         return 0;
1610 }
1611
1612
1613 /* Start an OHCI controller, set the BUS operational
1614 * resets USB and controller
1615 * enable interrupts
1616 */
1617 static int u132_run(struct u132 *u132)
1618 {
1619         int retval;
1620         u32 control;
1621         u32 status;
1622         u32 fminterval;
1623         u32 periodicstart;
1624         u32 cmdstatus;
1625         u32 roothub_a;
1626         int mask = OHCI_INTR_INIT;
1627         int first = u132->hc_fminterval == 0;
1628         int sleep_time = 0;
1629         int reset_timeout = 30;        /* ... allow extra time */
1630         u132_disable(u132);
1631         if (first) {
1632                 u32 temp;
1633                 retval = u132_read_pcimem(u132, fminterval, &temp);
1634                 if (retval)
1635                         return retval;
1636                 u132->hc_fminterval = temp & 0x3fff;
1637                 if (u132->hc_fminterval != FI) {
1638                 }
1639                 u132->hc_fminterval |= FSMP(u132->hc_fminterval) << 16;
1640         }
1641         retval = u132_read_pcimem(u132, control, &u132->hc_control);
1642         if (retval)
1643                 return retval;
1644         dev_info(&u132->platform_dev->dev, "resetting from state '%s', control "
1645                 "= %08X\n", hcfs2string(u132->hc_control & OHCI_CTRL_HCFS),
1646                 u132->hc_control);
1647         switch (u132->hc_control & OHCI_CTRL_HCFS) {
1648         case OHCI_USB_OPER:
1649                 sleep_time = 0;
1650                 break;
1651         case OHCI_USB_SUSPEND:
1652         case OHCI_USB_RESUME:
1653                 u132->hc_control &= OHCI_CTRL_RWC;
1654                 u132->hc_control |= OHCI_USB_RESUME;
1655                 sleep_time = 10;
1656                 break;
1657         default:
1658                 u132->hc_control &= OHCI_CTRL_RWC;
1659                 u132->hc_control |= OHCI_USB_RESET;
1660                 sleep_time = 50;
1661                 break;
1662         }
1663         retval = u132_write_pcimem(u132, control, u132->hc_control);
1664         if (retval)
1665                 return retval;
1666         retval = u132_read_pcimem(u132, control, &control);
1667         if (retval)
1668                 return retval;
1669         msleep(sleep_time);
1670         retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1671         if (retval)
1672                 return retval;
1673         if (!(roothub_a & RH_A_NPS)) {
1674                 int temp;        /* power down each port */
1675                 for (temp = 0; temp < u132->num_ports; temp++) {
1676                         retval = u132_write_pcimem(u132,
1677                                 roothub.portstatus[temp], RH_PS_LSDA);
1678                         if (retval)
1679                                 return retval;
1680                 }
1681         }
1682         retval = u132_read_pcimem(u132, control, &control);
1683         if (retval)
1684                 return retval;
1685       retry:retval = u132_read_pcimem(u132, cmdstatus, &status);
1686         if (retval)
1687                 return retval;
1688         retval = u132_write_pcimem(u132, cmdstatus, OHCI_HCR);
1689         if (retval)
1690                 return retval;
1691       extra:{
1692                 retval = u132_read_pcimem(u132, cmdstatus, &status);
1693                 if (retval)
1694                         return retval;
1695                 if (0 != (status & OHCI_HCR)) {
1696                         if (--reset_timeout == 0) {
1697                                 dev_err(&u132->platform_dev->dev, "USB HC reset"
1698                                         " timed out!\n");
1699                                 return -ENODEV;
1700                         } else {
1701                                 msleep(5);
1702                                 goto extra;
1703                         }
1704                 }
1705         }
1706         if (u132->flags & OHCI_QUIRK_INITRESET) {
1707                 retval = u132_write_pcimem(u132, control, u132->hc_control);
1708                 if (retval)
1709                         return retval;
1710                 retval = u132_read_pcimem(u132, control, &control);
1711                 if (retval)
1712                         return retval;
1713         }
1714         retval = u132_write_pcimem(u132, ed_controlhead, 0x00000000);
1715         if (retval)
1716                 return retval;
1717         retval = u132_write_pcimem(u132, ed_bulkhead, 0x11000000);
1718         if (retval)
1719                 return retval;
1720         retval = u132_write_pcimem(u132, hcca, 0x00000000);
1721         if (retval)
1722                 return retval;
1723         retval = u132_periodic_reinit(u132);
1724         if (retval)
1725                 return retval;
1726         retval = u132_read_pcimem(u132, fminterval, &fminterval);
1727         if (retval)
1728                 return retval;
1729         retval = u132_read_pcimem(u132, periodicstart, &periodicstart);
1730         if (retval)
1731                 return retval;
1732         if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
1733                 if (!(u132->flags & OHCI_QUIRK_INITRESET)) {
1734                         u132->flags |= OHCI_QUIRK_INITRESET;
1735                         goto retry;
1736                 } else
1737                         dev_err(&u132->platform_dev->dev, "init err(%08x %04x)"
1738                                 "\n", fminterval, periodicstart);
1739         }                        /* start controller operations */
1740         u132->hc_control &= OHCI_CTRL_RWC;
1741         u132->hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
1742         retval = u132_write_pcimem(u132, control, u132->hc_control);
1743         if (retval)
1744                 return retval;
1745         retval = u132_write_pcimem(u132, cmdstatus, OHCI_BLF);
1746         if (retval)
1747                 return retval;
1748         retval = u132_read_pcimem(u132, cmdstatus, &cmdstatus);
1749         if (retval)
1750                 return retval;
1751         retval = u132_read_pcimem(u132, control, &control);
1752         if (retval)
1753                 return retval;
1754         u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1755         retval = u132_write_pcimem(u132, roothub.status, RH_HS_DRWE);
1756         if (retval)
1757                 return retval;
1758         retval = u132_write_pcimem(u132, intrstatus, mask);
1759         if (retval)
1760                 return retval;
1761         retval = u132_write_pcimem(u132, intrdisable,
1762                 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
1763                 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
1764                 OHCI_INTR_SO);
1765         if (retval)
1766                 return retval;        /* handle root hub init quirks ... */
1767         retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
1768         if (retval)
1769                 return retval;
1770         roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
1771         if (u132->flags & OHCI_QUIRK_SUPERIO) {
1772                 roothub_a |= RH_A_NOCP;
1773                 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
1774                 retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1775                 if (retval)
1776                         return retval;
1777         } else if ((u132->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
1778                 roothub_a |= RH_A_NPS;
1779                 retval = u132_write_pcimem(u132, roothub.a, roothub_a);
1780                 if (retval)
1781                         return retval;
1782         }
1783         retval = u132_write_pcimem(u132, roothub.status, RH_HS_LPSC);
1784         if (retval)
1785                 return retval;
1786         retval = u132_write_pcimem(u132, roothub.b,
1787                 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
1788         if (retval)
1789                 return retval;
1790         retval = u132_read_pcimem(u132, control, &control);
1791         if (retval)
1792                 return retval;
1793         mdelay((roothub_a >> 23) & 0x1fe);
1794         u132_to_hcd(u132)->state = HC_STATE_RUNNING;
1795         return 0;
1796 }
1797
1798 static void u132_hcd_stop(struct usb_hcd *hcd)
1799 {
1800         struct u132 *u132 = hcd_to_u132(hcd);
1801         if (u132->going > 1) {
1802                 dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p) has b"
1803                         "een removed %d\n", u132, hcd, u132->going);
1804         } else if (u132->going > 0) {
1805                 dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
1806                         "ed\n", hcd);
1807         } else {
1808                 down(&u132->sw_lock);
1809                 msleep(100);
1810                 u132_power(u132, 0);
1811                 up(&u132->sw_lock);
1812         }
1813 }
1814
1815 static int u132_hcd_start(struct usb_hcd *hcd)
1816 {
1817         struct u132 *u132 = hcd_to_u132(hcd);
1818         if (u132->going > 1) {
1819                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1820                         , u132->going);
1821                 return -ENODEV;
1822         } else if (u132->going > 0) {
1823                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
1824                 return -ESHUTDOWN;
1825         } else if (hcd->self.controller) {
1826                 int retval;
1827                 struct platform_device *pdev =
1828                         to_platform_device(hcd->self.controller);
1829                 u16 vendor = ((struct u132_platform_data *)
1830                         (pdev->dev.platform_data))->vendor;
1831                 u16 device = ((struct u132_platform_data *)
1832                         (pdev->dev.platform_data))->device;
1833                 down(&u132->sw_lock);
1834                 msleep(10);
1835                 if (vendor == PCI_VENDOR_ID_AMD && device == 0x740c) {
1836                         u132->flags = OHCI_QUIRK_AMD756;
1837                 } else if (vendor == PCI_VENDOR_ID_OPTI && device == 0xc861) {
1838                         dev_err(&u132->platform_dev->dev, "WARNING: OPTi workar"
1839                                 "ounds unavailable\n");
1840                 } else if (vendor == PCI_VENDOR_ID_COMPAQ && device == 0xa0f8)
1841                         u132->flags |= OHCI_QUIRK_ZFMICRO;
1842                 retval = u132_run(u132);
1843                 if (retval) {
1844                         u132_disable(u132);
1845                         u132->going = 1;
1846                 }
1847                 msleep(100);
1848                 up(&u132->sw_lock);
1849                 return retval;
1850         } else {
1851                 dev_err(&u132->platform_dev->dev, "platform_device missing\n");
1852                 return -ENODEV;
1853         }
1854 }
1855
1856 static int u132_hcd_reset(struct usb_hcd *hcd)
1857 {
1858         struct u132 *u132 = hcd_to_u132(hcd);
1859         if (u132->going > 1) {
1860                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
1861                         , u132->going);
1862                 return -ENODEV;
1863         } else if (u132->going > 0) {
1864                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
1865                 return -ESHUTDOWN;
1866         } else {
1867                 int retval;
1868                 down(&u132->sw_lock);
1869                 retval = u132_init(u132);
1870                 if (retval) {
1871                         u132_disable(u132);
1872                         u132->going = 1;
1873                 }
1874                 up(&u132->sw_lock);
1875                 return retval;
1876         }
1877 }
1878
1879 static int create_endpoint_and_queue_int(struct u132 *u132,
1880         struct u132_udev *udev, struct usb_host_endpoint *hep, struct urb *urb,
1881         struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1882         gfp_t mem_flags)
1883 {
1884         struct u132_ring *ring;
1885         unsigned long irqs;
1886         u8 endp_number = ++u132->num_endpoints;
1887         struct u132_endp *endp = hep->hcpriv = u132->endp[endp_number - 1] =
1888                 kmalloc(sizeof(struct u132_endp), mem_flags);
1889         if (!endp) {
1890                 return -ENOMEM;
1891         }
1892         INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1893         spin_lock_init(&endp->queue_lock.slock);
1894         INIT_LIST_HEAD(&endp->urb_more);
1895         ring = endp->ring = &u132->ring[0];
1896         if (ring->curr_endp) {
1897                 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
1898         } else {
1899                 INIT_LIST_HEAD(&endp->endp_ring);
1900                 ring->curr_endp = endp;
1901         }
1902         ring->length += 1;
1903         endp->dequeueing = 0;
1904         endp->edset_flush = 0;
1905         endp->active = 0;
1906         endp->delayed = 0;
1907         endp->endp_number = endp_number;
1908         endp->u132 = u132;
1909         endp->hep = hep;
1910         endp->pipetype = usb_pipetype(urb->pipe);
1911         u132_endp_init_kref(u132, endp);
1912         if (usb_pipein(urb->pipe)) {
1913                 endp->toggle_bits = 0x2;
1914                 usb_settoggle(udev->usb_device, usb_endp, 0, 0);
1915                 endp->input = 1;
1916                 endp->output = 0;
1917                 udev->endp_number_in[usb_endp] = endp_number;
1918                 u132_udev_get_kref(u132, udev);
1919         } else {
1920                 endp->toggle_bits = 0x2;
1921                 usb_settoggle(udev->usb_device, usb_endp, 1, 0);
1922                 endp->input = 0;
1923                 endp->output = 1;
1924                 udev->endp_number_out[usb_endp] = endp_number;
1925                 u132_udev_get_kref(u132, udev);
1926         }
1927         urb->hcpriv = u132;
1928         spin_lock_irqsave(&endp->queue_lock.slock, irqs);
1929         endp->delayed = 1;
1930         endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1931         endp->udev_number = address;
1932         endp->usb_addr = usb_addr;
1933         endp->usb_endp = usb_endp;
1934         endp->queue_size = 1;
1935         endp->queue_last = 0;
1936         endp->queue_next = 0;
1937         endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1938         spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
1939         u132_endp_queue_work(u132, endp, msecs_to_jiffies(urb->interval));
1940         return 0;
1941 }
1942
1943 static int queue_int_on_old_endpoint(struct u132 *u132, struct u132_udev *udev,
1944         struct usb_host_endpoint *hep, struct urb *urb,
1945         struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
1946         u8 usb_endp, u8 address)
1947 {
1948         urb->hcpriv = u132;
1949         endp->delayed = 1;
1950         endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
1951         if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
1952                 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
1953         } else {
1954                 struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
1955                         GFP_ATOMIC);
1956                 if (urbq == NULL) {
1957                         endp->queue_size -= 1;
1958                         return -ENOMEM;
1959                 } else {
1960                         list_add_tail(&urbq->urb_more, &endp->urb_more);
1961                         urbq->urb = urb;
1962                 }
1963         }
1964         return 0;
1965 }
1966
1967 static int create_endpoint_and_queue_bulk(struct u132 *u132,
1968         struct u132_udev *udev, struct usb_host_endpoint *hep, struct urb *urb,
1969         struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
1970         gfp_t mem_flags)
1971 {
1972         int ring_number;
1973         struct u132_ring *ring;
1974         unsigned long irqs;
1975         u8 endp_number = ++u132->num_endpoints;
1976         struct u132_endp *endp = hep->hcpriv = u132->endp[endp_number - 1] =
1977                 kmalloc(sizeof(struct u132_endp), mem_flags);
1978         if (!endp) {
1979                 return -ENOMEM;
1980         }
1981         INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
1982         spin_lock_init(&endp->queue_lock.slock);
1983         INIT_LIST_HEAD(&endp->urb_more);
1984         endp->dequeueing = 0;
1985         endp->edset_flush = 0;
1986         endp->active = 0;
1987         endp->delayed = 0;
1988         endp->endp_number = endp_number;
1989         endp->u132 = u132;
1990         endp->hep = hep;
1991         endp->pipetype = usb_pipetype(urb->pipe);
1992         u132_endp_init_kref(u132, endp);
1993         if (usb_pipein(urb->pipe)) {
1994                 endp->toggle_bits = 0x2;
1995                 usb_settoggle(udev->usb_device, usb_endp, 0, 0);
1996                 ring_number = 3;
1997                 endp->input = 1;
1998                 endp->output = 0;
1999                 udev->endp_number_in[usb_endp] = endp_number;
2000                 u132_udev_get_kref(u132, udev);
2001         } else {
2002                 endp->toggle_bits = 0x2;
2003                 usb_settoggle(udev->usb_device, usb_endp, 1, 0);
2004                 ring_number = 2;
2005                 endp->input = 0;
2006                 endp->output = 1;
2007                 udev->endp_number_out[usb_endp] = endp_number;
2008                 u132_udev_get_kref(u132, udev);
2009         }
2010         ring = endp->ring = &u132->ring[ring_number - 1];
2011         if (ring->curr_endp) {
2012                 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2013         } else {
2014                 INIT_LIST_HEAD(&endp->endp_ring);
2015                 ring->curr_endp = endp;
2016         }
2017         ring->length += 1;
2018         urb->hcpriv = u132;
2019         spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2020         endp->udev_number = address;
2021         endp->usb_addr = usb_addr;
2022         endp->usb_endp = usb_endp;
2023         endp->queue_size = 1;
2024         endp->queue_last = 0;
2025         endp->queue_next = 0;
2026         endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2027         spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2028         u132_endp_queue_work(u132, endp, 0);
2029         return 0;
2030 }
2031
2032 static int queue_bulk_on_old_endpoint(struct u132 *u132, struct u132_udev *udev,
2033          struct usb_host_endpoint *hep, struct urb *urb,
2034         struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2035         u8 usb_endp, u8 address)
2036 {
2037         urb->hcpriv = u132;
2038         if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2039                 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2040         } else {
2041                 struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
2042                         GFP_ATOMIC);
2043                 if (urbq == NULL) {
2044                         endp->queue_size -= 1;
2045                         return -ENOMEM;
2046                 } else {
2047                         list_add_tail(&urbq->urb_more, &endp->urb_more);
2048                         urbq->urb = urb;
2049                 }
2050         }
2051         return 0;
2052 }
2053
2054 static int create_endpoint_and_queue_control(struct u132 *u132,
2055         struct usb_host_endpoint *hep, struct urb *urb,
2056         struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp,
2057         gfp_t mem_flags)
2058 {
2059         struct u132_ring *ring;
2060         u8 endp_number = ++u132->num_endpoints;
2061         struct u132_endp *endp = hep->hcpriv = u132->endp[endp_number - 1] =
2062                 kmalloc(sizeof(struct u132_endp), mem_flags);
2063         if (!endp) {
2064                 return -ENOMEM;
2065         }
2066         INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
2067         spin_lock_init(&endp->queue_lock.slock);
2068         INIT_LIST_HEAD(&endp->urb_more);
2069         ring = endp->ring = &u132->ring[0];
2070         if (ring->curr_endp) {
2071                 list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
2072         } else {
2073                 INIT_LIST_HEAD(&endp->endp_ring);
2074                 ring->curr_endp = endp;
2075         }
2076         ring->length += 1;
2077         endp->dequeueing = 0;
2078         endp->edset_flush = 0;
2079         endp->active = 0;
2080         endp->delayed = 0;
2081         endp->endp_number = endp_number;
2082         endp->u132 = u132;
2083         endp->hep = hep;
2084         u132_endp_init_kref(u132, endp);
2085         u132_endp_get_kref(u132, endp);
2086         if (usb_addr == 0) {
2087                 unsigned long irqs;
2088                 u8 address = u132->addr[usb_addr].address;
2089                 struct u132_udev *udev = &u132->udev[address];
2090                 endp->udev_number = address;
2091                 endp->usb_addr = usb_addr;
2092                 endp->usb_endp = usb_endp;
2093                 endp->input = 1;
2094                 endp->output = 1;
2095                 endp->pipetype = usb_pipetype(urb->pipe);
2096                 u132_udev_init_kref(u132, udev);
2097                 u132_udev_get_kref(u132, udev);
2098                 udev->endp_number_in[usb_endp] = endp_number;
2099                 udev->endp_number_out[usb_endp] = endp_number;
2100                 urb->hcpriv = u132;
2101                 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2102                 endp->queue_size = 1;
2103                 endp->queue_last = 0;
2104                 endp->queue_next = 0;
2105                 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2106                 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2107                 u132_endp_queue_work(u132, endp, 0);
2108                 return 0;
2109         } else {                /*(usb_addr > 0) */
2110                 unsigned long irqs;
2111                 u8 address = u132->addr[usb_addr].address;
2112                 struct u132_udev *udev = &u132->udev[address];
2113                 endp->udev_number = address;
2114                 endp->usb_addr = usb_addr;
2115                 endp->usb_endp = usb_endp;
2116                 endp->input = 1;
2117                 endp->output = 1;
2118                 endp->pipetype = usb_pipetype(urb->pipe);
2119                 u132_udev_get_kref(u132, udev);
2120                 udev->enumeration = 2;
2121                 udev->endp_number_in[usb_endp] = endp_number;
2122                 udev->endp_number_out[usb_endp] = endp_number;
2123                 urb->hcpriv = u132;
2124                 spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2125                 endp->queue_size = 1;
2126                 endp->queue_last = 0;
2127                 endp->queue_next = 0;
2128                 endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
2129                 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2130                 u132_endp_queue_work(u132, endp, 0);
2131                 return 0;
2132         }
2133 }
2134
2135 static int queue_control_on_old_endpoint(struct u132 *u132,
2136         struct usb_host_endpoint *hep, struct urb *urb,
2137         struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
2138         u8 usb_endp)
2139 {
2140         if (usb_addr == 0) {
2141                 if (usb_pipein(urb->pipe)) {
2142                         urb->hcpriv = u132;
2143                         if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2144                                 endp->urb_list[ENDP_QUEUE_MASK &
2145                                         endp->queue_last++] = urb;
2146                         } else {
2147                                 struct u132_urbq *urbq =
2148                                         kmalloc(sizeof(struct u132_urbq),
2149                                         GFP_ATOMIC);
2150                                 if (urbq == NULL) {
2151                                         endp->queue_size -= 1;
2152                                         return -ENOMEM;
2153                                 } else {
2154                                         list_add_tail(&urbq->urb_more,
2155                                                 &endp->urb_more);
2156                                         urbq->urb = urb;
2157                                 }
2158                         }
2159                         return 0;
2160                 } else {        /* usb_pipeout(urb->pipe) */
2161                         struct u132_addr *addr = &u132->addr[usb_dev->devnum];
2162                         int I = MAX_U132_UDEVS;
2163                         int i = 0;
2164                         while (--I > 0) {
2165                                 struct u132_udev *udev = &u132->udev[++i];
2166                                 if (udev->usb_device) {
2167                                         continue;
2168                                 } else {
2169                                         udev->enumeration = 1;
2170                                         u132->addr[0].address = i;
2171                                         endp->udev_number = i;
2172                                         udev->udev_number = i;
2173                                         udev->usb_addr = usb_dev->devnum;
2174                                         u132_udev_init_kref(u132, udev);
2175                                         udev->endp_number_in[usb_endp] =
2176                                                 endp->endp_number;
2177                                         u132_udev_get_kref(u132, udev);
2178                                         udev->endp_number_out[usb_endp] =
2179                                                 endp->endp_number;
2180                                         udev->usb_device = usb_dev;
2181                                         ((u8 *) (urb->setup_packet))[2] =
2182                                                 addr->address = i;
2183                                         u132_udev_get_kref(u132, udev);
2184                                         break;
2185                                 }
2186                         }
2187                         if (I == 0) {
2188                                 dev_err(&u132->platform_dev->dev, "run out of d"
2189                                         "evice space\n");
2190                                 return -EINVAL;
2191                         }
2192                         urb->hcpriv = u132;
2193                         if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2194                                 endp->urb_list[ENDP_QUEUE_MASK &
2195                                         endp->queue_last++] = urb;
2196                         } else {
2197                                 struct u132_urbq *urbq =
2198                                         kmalloc(sizeof(struct u132_urbq),
2199                                         GFP_ATOMIC);
2200                                 if (urbq == NULL) {
2201                                         endp->queue_size -= 1;
2202                                         return -ENOMEM;
2203                                 } else {
2204                                         list_add_tail(&urbq->urb_more,
2205                                                 &endp->urb_more);
2206                                         urbq->urb = urb;
2207                                 }
2208                         }
2209                         return 0;
2210                 }
2211         } else {                /*(usb_addr > 0) */
2212                 u8 address = u132->addr[usb_addr].address;
2213                 struct u132_udev *udev = &u132->udev[address];
2214                 urb->hcpriv = u132;
2215                 if (udev->enumeration == 2) {
2216                 } else
2217                         udev->enumeration = 2;
2218                 if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
2219                         endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
2220                                 urb;
2221                 } else {
2222                         struct u132_urbq *urbq =
2223                                 kmalloc(sizeof(struct u132_urbq), GFP_ATOMIC);
2224                         if (urbq == NULL) {
2225                                 endp->queue_size -= 1;
2226                                 return -ENOMEM;
2227                         } else {
2228                                 list_add_tail(&urbq->urb_more, &endp->urb_more);
2229                                 urbq->urb = urb;
2230                         }
2231                 }
2232                 return 0;
2233         }
2234 }
2235
2236 static int u132_urb_enqueue(struct usb_hcd *hcd, struct usb_host_endpoint *hep,
2237         struct urb *urb, gfp_t mem_flags)
2238 {
2239         struct u132 *u132 = hcd_to_u132(hcd);
2240         if (irqs_disabled()) {
2241                 if (__GFP_WAIT & mem_flags) {
2242                         printk(KERN_ERR "invalid context for function that migh"
2243                                 "t sleep\n");
2244                         return -EINVAL;
2245                 }
2246         }
2247         if (u132->going > 1) {
2248                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2249                         , u132->going);
2250                 return -ENODEV;
2251         } else if (u132->going > 0) {
2252                 dev_err(&u132->platform_dev->dev, "device is being removed urb="
2253                         "%p status=%d\n", urb, urb->status);
2254                 return -ESHUTDOWN;
2255         } else {
2256                 u8 usb_addr = usb_pipedevice(urb->pipe);
2257                 u8 usb_endp = usb_pipeendpoint(urb->pipe);
2258                 struct usb_device *usb_dev = urb->dev;
2259                 if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2260                         u8 address = u132->addr[usb_addr].address;
2261                         struct u132_udev *udev = &u132->udev[address];
2262                         struct u132_endp *endp = hep->hcpriv;
2263                         urb->actual_length = 0;
2264                         if (endp) {
2265                                 unsigned long irqs;
2266                                 int retval;
2267                                 spin_lock_irqsave(&endp->queue_lock.slock,
2268                                         irqs);
2269                                 retval = queue_int_on_old_endpoint(u132, udev,
2270                                         hep, urb, usb_dev, endp, usb_addr,
2271                                         usb_endp, address);
2272                                 spin_unlock_irqrestore(&endp->queue_lock.slock,
2273                                         irqs);
2274                                 if (retval) {
2275                                         return retval;
2276                                 } else {
2277                                         u132_endp_queue_work(u132, endp,
2278                                                 msecs_to_jiffies(urb->interval))
2279                                                 ;
2280                                         return 0;
2281                                 }
2282                         } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2283                                 return -EINVAL;
2284                         } else {        /*(endp == NULL) */
2285                                 return create_endpoint_and_queue_int(u132, udev,
2286                                          hep, urb, usb_dev, usb_addr, usb_endp,
2287                                         address, mem_flags);
2288                         }
2289                 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2290                         dev_err(&u132->platform_dev->dev, "the hardware does no"
2291                                 "t support PIPE_ISOCHRONOUS\n");
2292                         return -EINVAL;
2293                 } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
2294                         u8 address = u132->addr[usb_addr].address;
2295                         struct u132_udev *udev = &u132->udev[address];
2296                         struct u132_endp *endp = hep->hcpriv;
2297                         urb->actual_length = 0;
2298                         if (endp) {
2299                                 unsigned long irqs;
2300                                 int retval;
2301                                 spin_lock_irqsave(&endp->queue_lock.slock,
2302                                         irqs);
2303                                 retval = queue_bulk_on_old_endpoint(u132, udev,
2304                                         hep, urb, usb_dev, endp, usb_addr,
2305                                         usb_endp, address);
2306                                 spin_unlock_irqrestore(&endp->queue_lock.slock,
2307                                         irqs);
2308                                 if (retval) {
2309                                         return retval;
2310                                 } else {
2311                                         u132_endp_queue_work(u132, endp, 0);
2312                                         return 0;
2313                                 }
2314                         } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2315                                 return -EINVAL;
2316                         } else
2317                                 return create_endpoint_and_queue_bulk(u132,
2318                                         udev, hep, urb, usb_dev, usb_addr,
2319                                         usb_endp, address, mem_flags);
2320                 } else {
2321                         struct u132_endp *endp = hep->hcpriv;
2322                         u16 urb_size = 8;
2323                         u8 *b = urb->setup_packet;
2324                         int i = 0;
2325                         char data[30 *3 + 4];
2326                         char *d = data;
2327                         int m = (sizeof(data) - 1) / 3;
2328                         int l = 0;
2329                         data[0] = 0;
2330                         while (urb_size-- > 0) {
2331                                 if (i > m) {
2332                                 } else if (i++ < m) {
2333                                         int w = sprintf(d, " %02X", *b++);
2334                                         d += w;
2335                                         l += w;
2336                                 } else
2337                                         d += sprintf(d, " ..");
2338                         }
2339                         if (endp) {
2340                                 unsigned long irqs;
2341                                 int retval;
2342                                 spin_lock_irqsave(&endp->queue_lock.slock,
2343                                         irqs);
2344                                 retval = queue_control_on_old_endpoint(u132,
2345                                         hep, urb, usb_dev, endp, usb_addr,
2346                                         usb_endp);
2347                                 spin_unlock_irqrestore(&endp->queue_lock.slock,
2348                                         irqs);
2349                                 if (retval) {
2350                                         return retval;
2351                                 } else {
2352                                         u132_endp_queue_work(u132, endp, 0);
2353                                         return 0;
2354                                 }
2355                         } else if (u132->num_endpoints == MAX_U132_ENDPS) {
2356                                 return -EINVAL;
2357                         } else
2358                                 return create_endpoint_and_queue_control(u132,
2359                                         hep, urb, usb_dev, usb_addr, usb_endp,
2360                                         mem_flags);
2361                 }
2362         }
2363 }
2364
2365 static int dequeue_from_overflow_chain(struct u132 *u132,
2366         struct u132_endp *endp, struct urb *urb)
2367 {
2368         struct list_head *scan;
2369         struct list_head *head = &endp->urb_more;
2370         list_for_each(scan, head) {
2371                 struct u132_urbq *urbq = list_entry(scan, struct u132_urbq,
2372                         urb_more);
2373                 if (urbq->urb == urb) {
2374                         struct usb_hcd *hcd = u132_to_hcd(u132);
2375                         list_del(scan);
2376                         endp->queue_size -= 1;
2377                         urb->error_count = 0;
2378                         urb->hcpriv = NULL;
2379                         usb_hcd_giveback_urb(hcd, urb);
2380                         return 0;
2381                 } else
2382                         continue;
2383         }
2384         dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]=%p ring"
2385                 "[%d] %c%c usb_endp=%d usb_addr=%d size=%d next=%04X last=%04X"
2386                 "\n", urb, endp->endp_number, endp, endp->ring->number,
2387                 endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2388                 endp->usb_endp, endp->usb_addr, endp->queue_size,
2389                 endp->queue_next, endp->queue_last);
2390         return -EINVAL;
2391 }
2392
2393 static int u132_endp_urb_dequeue(struct u132 *u132, struct u132_endp *endp,
2394         struct urb *urb)
2395 {
2396         unsigned long irqs;
2397         spin_lock_irqsave(&endp->queue_lock.slock, irqs);
2398         if (endp->queue_size == 0) {
2399                 dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]"
2400                         "=%p ring[%d] %c%c usb_endp=%d usb_addr=%d\n", urb,
2401                         endp->endp_number, endp, endp->ring->number,
2402                         endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
2403                         endp->usb_endp, endp->usb_addr);
2404                 spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2405                 return -EINVAL;
2406         }
2407         if (urb == endp->urb_list[ENDP_QUEUE_MASK & endp->queue_next]) {
2408                 if (endp->active) {
2409                         endp->dequeueing = 1;
2410                         endp->edset_flush = 1;
2411                         u132_endp_queue_work(u132, endp, 0);
2412                         spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2413                         urb->hcpriv = NULL;
2414                         return 0;
2415                 } else {
2416                         spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2417                         u132_hcd_abandon_urb(u132, endp, urb, urb->status);
2418                         return 0;
2419                 }
2420         } else {
2421                 u16 queue_list = 0;
2422                 u16 queue_size = endp->queue_size;
2423                 u16 queue_scan = endp->queue_next;
2424                 struct urb **urb_slot = NULL;
2425                 while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2426                         if (urb == endp->urb_list[ENDP_QUEUE_MASK &
2427                                 ++queue_scan]) {
2428                                 urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2429                                         queue_scan];
2430                                 break;
2431                         } else
2432                                 continue;
2433                 }
2434                 while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
2435                         *urb_slot = endp->urb_list[ENDP_QUEUE_MASK &
2436                                 ++queue_scan];
2437                         urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
2438                                 queue_scan];
2439                 }
2440                 if (urb_slot) {
2441                         struct usb_hcd *hcd = u132_to_hcd(u132);
2442                         endp->queue_size -= 1;
2443                         if (list_empty(&endp->urb_more)) {
2444                                 spin_unlock_irqrestore(&endp->queue_lock.slock,
2445                                         irqs);
2446                         } else {
2447                                 struct list_head *next = endp->urb_more.next;
2448                                 struct u132_urbq *urbq = list_entry(next,
2449                                         struct u132_urbq, urb_more);
2450                                 list_del(next);
2451                                 *urb_slot = urbq->urb;
2452                                 spin_unlock_irqrestore(&endp->queue_lock.slock,
2453                                         irqs);
2454                                 kfree(urbq);
2455                         } urb->error_count = 0;
2456                         urb->hcpriv = NULL;
2457                         usb_hcd_giveback_urb(hcd, urb);
2458                         return 0;
2459                 } else if (list_empty(&endp->urb_more)) {
2460                         dev_err(&u132->platform_dev->dev, "urb=%p not found in "
2461                                 "endp[%d]=%p ring[%d] %c%c usb_endp=%d usb_addr"
2462                                 "=%d size=%d next=%04X last=%04X\n", urb,
2463                                 endp->endp_number, endp, endp->ring->number,
2464                                 endp->input ? 'I' : ' ',
2465                                 endp->output ? 'O' : ' ', endp->usb_endp,
2466                                 endp->usb_addr, endp->queue_size,
2467                                 endp->queue_next, endp->queue_last);
2468                         spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2469                         return -EINVAL;
2470                 } else {
2471                         int retval = dequeue_from_overflow_chain(u132, endp,
2472                                 urb);
2473                         spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
2474                         return retval;
2475                 }
2476         }
2477 }
2478
2479 static int u132_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
2480 {
2481         struct u132 *u132 = hcd_to_u132(hcd);
2482         if (u132->going > 2) {
2483                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2484                         , u132->going);
2485                 return -ENODEV;
2486         } else {
2487                 u8 usb_addr = usb_pipedevice(urb->pipe);
2488                 u8 usb_endp = usb_pipeendpoint(urb->pipe);
2489                 u8 address = u132->addr[usb_addr].address;
2490                 struct u132_udev *udev = &u132->udev[address];
2491                 if (usb_pipein(urb->pipe)) {
2492                         u8 endp_number = udev->endp_number_in[usb_endp];
2493                         struct u132_endp *endp = u132->endp[endp_number - 1];
2494                         return u132_endp_urb_dequeue(u132, endp, urb);
2495                 } else {
2496                         u8 endp_number = udev->endp_number_out[usb_endp];
2497                         struct u132_endp *endp = u132->endp[endp_number - 1];
2498                         return u132_endp_urb_dequeue(u132, endp, urb);
2499                 }
2500         }
2501 }
2502
2503 static void u132_endpoint_disable(struct usb_hcd *hcd,
2504         struct usb_host_endpoint *hep)
2505 {
2506         struct u132 *u132 = hcd_to_u132(hcd);
2507         if (u132->going > 2) {
2508                 dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p hep=%p"
2509                         ") has been removed %d\n", u132, hcd, hep,
2510                         u132->going);
2511         } else {
2512                 struct u132_endp *endp = hep->hcpriv;
2513                 if (endp)
2514                         u132_endp_put_kref(u132, endp);
2515         }
2516 }
2517
2518 static int u132_get_frame(struct usb_hcd *hcd)
2519 {
2520         struct u132 *u132 = hcd_to_u132(hcd);
2521         if (u132->going > 1) {
2522                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2523                         , u132->going);
2524                 return -ENODEV;
2525         } else if (u132->going > 0) {
2526                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2527                 return -ESHUTDOWN;
2528         } else {
2529                 int frame = 0;
2530                 dev_err(&u132->platform_dev->dev, "TODO: u132_get_frame\n");
2531                 msleep(100);
2532                 return frame;
2533         }
2534 }
2535
2536 static int u132_roothub_descriptor(struct u132 *u132,
2537         struct usb_hub_descriptor *desc)
2538 {
2539         int retval;
2540         u16 temp;
2541         u32 rh_a = -1;
2542         u32 rh_b = -1;
2543         retval = u132_read_pcimem(u132, roothub.a, &rh_a);
2544         if (retval)
2545                 return retval;
2546         desc->bDescriptorType = 0x29;
2547         desc->bPwrOn2PwrGood = (rh_a & RH_A_POTPGT) >> 24;
2548         desc->bHubContrCurrent = 0;
2549         desc->bNbrPorts = u132->num_ports;
2550         temp = 1 + (u132->num_ports / 8);
2551         desc->bDescLength = 7 + 2 *temp;
2552         temp = 0;
2553         if (rh_a & RH_A_NPS)
2554                 temp |= 0x0002;
2555         if (rh_a & RH_A_PSM)
2556                 temp |= 0x0001;
2557         if (rh_a & RH_A_NOCP) {
2558                 temp |= 0x0010;
2559         } else if (rh_a & RH_A_OCPM)
2560                 temp |= 0x0008;
2561         desc->wHubCharacteristics = cpu_to_le16(temp);
2562         retval = u132_read_pcimem(u132, roothub.b, &rh_b);
2563         if (retval)
2564                 return retval;
2565         memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
2566         desc->bitmap[0] = rh_b & RH_B_DR;
2567         if (u132->num_ports > 7) {
2568                 desc->bitmap[1] = (rh_b & RH_B_DR) >> 8;
2569                 desc->bitmap[2] = 0xff;
2570         } else
2571                 desc->bitmap[1] = 0xff;
2572         return 0;
2573 }
2574
2575 static int u132_roothub_status(struct u132 *u132, __le32 *desc)
2576 {
2577         u32 rh_status = -1;
2578         int ret_status = u132_read_pcimem(u132, roothub.status, &rh_status);
2579         *desc = cpu_to_le32(rh_status);
2580         return ret_status;
2581 }
2582
2583 static int u132_roothub_portstatus(struct u132 *u132, __le32 *desc, u16 wIndex)
2584 {
2585         if (wIndex == 0 || wIndex > u132->num_ports) {
2586                 return -EINVAL;
2587         } else {
2588                 int port = wIndex - 1;
2589                 u32 rh_portstatus = -1;
2590                 int ret_portstatus = u132_read_pcimem(u132,
2591                         roothub.portstatus[port], &rh_portstatus);
2592                 *desc = cpu_to_le32(rh_portstatus);
2593                 if (*(u16 *) (desc + 2)) {
2594                         dev_info(&u132->platform_dev->dev, "Port %d Status Chan"
2595                                 "ge = %08X\n", port, *desc);
2596                 }
2597                 return ret_portstatus;
2598         }
2599 }
2600
2601
2602 /* this timer value might be vendor-specific ... */
2603 #define PORT_RESET_HW_MSEC 10
2604 #define PORT_RESET_MSEC 10
2605 /* wrap-aware logic morphed from <linux/jiffies.h> */
2606 #define tick_before(t1, t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
2607 static int u132_roothub_portreset(struct u132 *u132, int port_index)
2608 {
2609         int retval;
2610         u32 fmnumber;
2611         u16 now;
2612         u16 reset_done;
2613         retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2614         if (retval)
2615                 return retval;
2616         now = fmnumber;
2617         reset_done = now + PORT_RESET_MSEC;
2618         do {
2619                 u32 portstat;
2620                 do {
2621                         retval = u132_read_pcimem(u132,
2622                                 roothub.portstatus[port_index], &portstat);
2623                         if (retval)
2624                                 return retval;
2625                         if (RH_PS_PRS & portstat) {
2626                                 continue;
2627                         } else
2628                                 break;
2629                 } while (tick_before(now, reset_done));
2630                 if (RH_PS_PRS & portstat)
2631                         return -ENODEV;
2632                 if (RH_PS_CCS & portstat) {
2633                         if (RH_PS_PRSC & portstat) {
2634                                 retval = u132_write_pcimem(u132,
2635                                         roothub.portstatus[port_index],
2636                                         RH_PS_PRSC);
2637                                 if (retval)
2638                                         return retval;
2639                         }
2640                 } else
2641                         break;        /* start the next reset,
2642                                 sleep till it's probably done */
2643                 retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2644                          RH_PS_PRS);
2645                 if (retval)
2646                         return retval;
2647                 msleep(PORT_RESET_HW_MSEC);
2648                 retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
2649                 if (retval)
2650                         return retval;
2651                 now = fmnumber;
2652         } while (tick_before(now, reset_done));
2653         return 0;
2654 }
2655
2656 static int u132_roothub_setportfeature(struct u132 *u132, u16 wValue,
2657         u16 wIndex)
2658 {
2659         if (wIndex == 0 || wIndex > u132->num_ports) {
2660                 return -EINVAL;
2661         } else {
2662                 int retval;
2663                 int port_index = wIndex - 1;
2664                 struct u132_port *port = &u132->port[port_index];
2665                 port->Status &= ~(1 << wValue);
2666                 switch (wValue) {
2667                 case USB_PORT_FEAT_SUSPEND:
2668                         retval = u132_write_pcimem(u132,
2669                                 roothub.portstatus[port_index], RH_PS_PSS);
2670                         if (retval)
2671                                 return retval;
2672                         return 0;
2673                 case USB_PORT_FEAT_POWER:
2674                         retval = u132_write_pcimem(u132,
2675                                 roothub.portstatus[port_index], RH_PS_PPS);
2676                         if (retval)
2677                                 return retval;
2678                         return 0;
2679                 case USB_PORT_FEAT_RESET:
2680                         retval = u132_roothub_portreset(u132, port_index);
2681                         if (retval)
2682                                 return retval;
2683                         return 0;
2684                 default:
2685                         return -EPIPE;
2686                 }
2687         }
2688 }
2689
2690 static int u132_roothub_clearportfeature(struct u132 *u132, u16 wValue,
2691         u16 wIndex)
2692 {
2693         if (wIndex == 0 || wIndex > u132->num_ports) {
2694                 return -EINVAL;
2695         } else {
2696                 int port_index = wIndex - 1;
2697                 u32 temp;
2698                 int retval;
2699                 struct u132_port *port = &u132->port[port_index];
2700                 port->Status &= ~(1 << wValue);
2701                 switch (wValue) {
2702                 case USB_PORT_FEAT_ENABLE:
2703                         temp = RH_PS_CCS;
2704                         break;
2705                 case USB_PORT_FEAT_C_ENABLE:
2706                         temp = RH_PS_PESC;
2707                         break;
2708                 case USB_PORT_FEAT_SUSPEND:
2709                         temp = RH_PS_POCI;
2710                         if ((u132->hc_control & OHCI_CTRL_HCFS)
2711                                 != OHCI_USB_OPER) {
2712                                 dev_err(&u132->platform_dev->dev, "TODO resume_"
2713                                         "root_hub\n");
2714                         }
2715                         break;
2716                 case USB_PORT_FEAT_C_SUSPEND:
2717                         temp = RH_PS_PSSC;
2718                         break;
2719                 case USB_PORT_FEAT_POWER:
2720                         temp = RH_PS_LSDA;
2721                         break;
2722                 case USB_PORT_FEAT_C_CONNECTION:
2723                         temp = RH_PS_CSC;
2724                         break;
2725                 case USB_PORT_FEAT_C_OVER_CURRENT:
2726                         temp = RH_PS_OCIC;
2727                         break;
2728                 case USB_PORT_FEAT_C_RESET:
2729                         temp = RH_PS_PRSC;
2730                         break;
2731                 default:
2732                         return -EPIPE;
2733                 }
2734                 retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
2735                          temp);
2736                 if (retval)
2737                         return retval;
2738                 return 0;
2739         }
2740 }
2741
2742
2743 /* the virtual root hub timer IRQ checks for hub status*/
2744 static int u132_hub_status_data(struct usb_hcd *hcd, char *buf)
2745 {
2746         struct u132 *u132 = hcd_to_u132(hcd);
2747         if (u132->going > 1) {
2748                 dev_err(&u132->platform_dev->dev, "device hcd=%p has been remov"
2749                         "ed %d\n", hcd, u132->going);
2750                 return -ENODEV;
2751         } else if (u132->going > 0) {
2752                 dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
2753                         "ed\n", hcd);
2754                 return -ESHUTDOWN;
2755         } else {
2756                 int i, changed = 0, length = 1;
2757                 if (u132->flags & OHCI_QUIRK_AMD756) {
2758                         if ((u132->hc_roothub_a & RH_A_NDP) > MAX_ROOT_PORTS) {
2759                                 dev_err(&u132->platform_dev->dev, "bogus NDP, r"
2760                                         "ereads as NDP=%d\n",
2761                                         u132->hc_roothub_a & RH_A_NDP);
2762                                 goto done;
2763                         }
2764                 }
2765                 if (u132->hc_roothub_status & (RH_HS_LPSC | RH_HS_OCIC)) {
2766                         buf[0] = changed = 1;
2767                 } else
2768                         buf[0] = 0;
2769                 if (u132->num_ports > 7) {
2770                         buf[1] = 0;
2771                         length++;
2772                 }
2773                 for (i = 0; i < u132->num_ports; i++) {
2774                         if (u132->hc_roothub_portstatus[i] & (RH_PS_CSC |
2775                                 RH_PS_PESC | RH_PS_PSSC | RH_PS_OCIC |
2776                                 RH_PS_PRSC)) {
2777                                 changed = 1;
2778                                 if (i < 7) {
2779                                         buf[0] |= 1 << (i + 1);
2780                                 } else
2781                                         buf[1] |= 1 << (i - 7);
2782                                 continue;
2783                         }
2784                         if (!(u132->hc_roothub_portstatus[i] & RH_PS_CCS)) {
2785                                 continue;
2786                         }
2787                         if ((u132->hc_roothub_portstatus[i] & RH_PS_PSS)) {
2788                                 continue;
2789                         }
2790                 }
2791               done:return changed ? length : 0;
2792         }
2793 }
2794
2795 static int u132_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2796         u16 wIndex, char *buf, u16 wLength)
2797 {
2798         struct u132 *u132 = hcd_to_u132(hcd);
2799         if (u132->going > 1) {
2800                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2801                         , u132->going);
2802                 return -ENODEV;
2803         } else if (u132->going > 0) {
2804                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2805                 return -ESHUTDOWN;
2806         } else {
2807                 int retval = 0;
2808                 down(&u132->sw_lock);
2809                 switch (typeReq) {
2810                 case ClearHubFeature:
2811                         switch (wValue) {
2812                         case C_HUB_OVER_CURRENT:
2813                         case C_HUB_LOCAL_POWER:
2814                                 break;
2815                         default:
2816                                 goto stall;
2817                         }
2818                         break;
2819                 case SetHubFeature:
2820                         switch (wValue) {
2821                         case C_HUB_OVER_CURRENT:
2822                         case C_HUB_LOCAL_POWER:
2823                                 break;
2824                         default:
2825                                 goto stall;
2826                         }
2827                         break;
2828                 case ClearPortFeature:{
2829                                 retval = u132_roothub_clearportfeature(u132,
2830                                         wValue, wIndex);
2831                                 if (retval)
2832                                         goto error;
2833                                 break;
2834                         }
2835                 case GetHubDescriptor:{
2836                                 retval = u132_roothub_descriptor(u132,
2837                                         (struct usb_hub_descriptor *)buf);
2838                                 if (retval)
2839                                         goto error;
2840                                 break;
2841                         }
2842                 case GetHubStatus:{
2843                                 retval = u132_roothub_status(u132,
2844                                         (__le32 *) buf);
2845                                 if (retval)
2846                                         goto error;
2847                                 break;
2848                         }
2849                 case GetPortStatus:{
2850                                 retval = u132_roothub_portstatus(u132,
2851                                         (__le32 *) buf, wIndex);
2852                                 if (retval)
2853                                         goto error;
2854                                 break;
2855                         }
2856                 case SetPortFeature:{
2857                                 retval = u132_roothub_setportfeature(u132,
2858                                         wValue, wIndex);
2859                                 if (retval)
2860                                         goto error;
2861                                 break;
2862                         }
2863                 default:
2864                         goto stall;
2865                       error:u132_disable(u132);
2866                         u132->going = 1;
2867                         break;
2868                       stall:retval = -EPIPE;
2869                         break;
2870                 }
2871                 up(&u132->sw_lock);
2872                 return retval;
2873         }
2874 }
2875
2876 static int u132_start_port_reset(struct usb_hcd *hcd, unsigned port_num)
2877 {
2878         struct u132 *u132 = hcd_to_u132(hcd);
2879         if (u132->going > 1) {
2880                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2881                         , u132->going);
2882                 return -ENODEV;
2883         } else if (u132->going > 0) {
2884                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2885                 return -ESHUTDOWN;
2886         } else
2887                 return 0;
2888 }
2889
2890 static void u132_hub_irq_enable(struct usb_hcd *hcd)
2891 {
2892         struct u132 *u132 = hcd_to_u132(hcd);
2893         if (u132->going > 1) {
2894                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2895                         , u132->going);
2896         } else if (u132->going > 0)
2897                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2898 }
2899
2900
2901 #ifdef CONFIG_PM
2902 static int u132_hcd_suspend(struct usb_hcd *hcd, pm_message_t message)
2903 {
2904         struct u132 *u132 = hcd_to_u132(hcd);
2905         if (u132->going > 1) {
2906                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2907                         , u132->going);
2908                 return -ENODEV;
2909         } else if (u132->going > 0) {
2910                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2911                 return -ESHUTDOWN;
2912         } else
2913                 return 0;
2914 }
2915
2916 static int u132_hcd_resume(struct usb_hcd *hcd)
2917 {
2918         struct u132 *u132 = hcd_to_u132(hcd);
2919         if (u132->going > 1) {
2920                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2921                         , u132->going);
2922                 return -ENODEV;
2923         } else if (u132->going > 0) {
2924                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2925                 return -ESHUTDOWN;
2926         } else
2927                 return 0;
2928 }
2929
2930 static int u132_bus_suspend(struct usb_hcd *hcd)
2931 {
2932         struct u132 *u132 = hcd_to_u132(hcd);
2933         if (u132->going > 1) {
2934                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2935                         , u132->going);
2936                 return -ENODEV;
2937         } else if (u132->going > 0) {
2938                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2939                 return -ESHUTDOWN;
2940         } else
2941                 return 0;
2942 }
2943
2944 static int u132_bus_resume(struct usb_hcd *hcd)
2945 {
2946         struct u132 *u132 = hcd_to_u132(hcd);
2947         if (u132->going > 1) {
2948                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
2949                         , u132->going);
2950                 return -ENODEV;
2951         } else if (u132->going > 0) {
2952                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
2953                 return -ESHUTDOWN;
2954         } else
2955                 return 0;
2956 }
2957
2958 #else
2959 #define u132_hcd_suspend NULL
2960 #define u132_hcd_resume NULL
2961 #define u132_bus_suspend NULL
2962 #define u132_bus_resume NULL
2963 #endif
2964 static struct hc_driver u132_hc_driver = {
2965         .description = hcd_name,
2966         .hcd_priv_size = sizeof(struct u132),
2967         .irq = NULL,
2968         .flags = HCD_USB11 | HCD_MEMORY,
2969         .reset = u132_hcd_reset,
2970         .start = u132_hcd_start,
2971         .suspend = u132_hcd_suspend,
2972         .resume = u132_hcd_resume,
2973         .stop = u132_hcd_stop,
2974         .urb_enqueue = u132_urb_enqueue,
2975         .urb_dequeue = u132_urb_dequeue,
2976         .endpoint_disable = u132_endpoint_disable,
2977         .get_frame_number = u132_get_frame,
2978         .hub_status_data = u132_hub_status_data,
2979         .hub_control = u132_hub_control,
2980         .bus_suspend = u132_bus_suspend,
2981         .bus_resume = u132_bus_resume,
2982         .start_port_reset = u132_start_port_reset,
2983         .hub_irq_enable = u132_hub_irq_enable,
2984 };
2985
2986 /*
2987 * This function may be called by the USB core whilst the "usb_all_devices_rwsem"
2988 * is held for writing, thus this module must not call usb_remove_hcd()
2989 * synchronously - but instead should immediately stop activity to the
2990 * device and asynchronously call usb_remove_hcd()
2991 */
2992 static int __devexit u132_remove(struct platform_device *pdev)
2993 {
2994         struct usb_hcd *hcd = platform_get_drvdata(pdev);
2995         if (hcd) {
2996                 struct u132 *u132 = hcd_to_u132(hcd);
2997                 if (u132->going++ > 1) {
2998                         dev_err(&u132->platform_dev->dev, "already being remove"
2999                                 "d\n");
3000                         return -ENODEV;
3001                 } else {
3002                         int rings = MAX_U132_RINGS;
3003                         int endps = MAX_U132_ENDPS;
3004                         dev_err(&u132->platform_dev->dev, "removing device u132"
3005                                 ".%d\n", u132->sequence_num);
3006                         msleep(100);
3007                         down(&u132->sw_lock);
3008                         u132_monitor_cancel_work(u132);
3009                         while (rings-- > 0) {
3010                                 struct u132_ring *ring = &u132->ring[rings];
3011                                 u132_ring_cancel_work(u132, ring);
3012                         } while (endps-- > 0) {
3013                                 struct u132_endp *endp = u132->endp[endps];
3014                                 if (endp)
3015                                         u132_endp_cancel_work(u132, endp);
3016                         }
3017                         u132->going += 1;
3018                         printk(KERN_INFO "removing device u132.%d\n",
3019                                 u132->sequence_num);
3020                         up(&u132->sw_lock);
3021                         usb_remove_hcd(hcd);
3022                         u132_u132_put_kref(u132);
3023                         return 0;
3024                 }
3025         } else
3026                 return 0;
3027 }
3028
3029 static void u132_initialise(struct u132 *u132, struct platform_device *pdev)
3030 {
3031         int rings = MAX_U132_RINGS;
3032         int ports = MAX_U132_PORTS;
3033         int addrs = MAX_U132_ADDRS;
3034         int udevs = MAX_U132_UDEVS;
3035         int endps = MAX_U132_ENDPS;
3036         u132->board = pdev->dev.platform_data;
3037         u132->platform_dev = pdev;
3038         u132->power = 0;
3039         u132->reset = 0;
3040         init_MUTEX(&u132->sw_lock);
3041         init_MUTEX(&u132->scheduler_lock);
3042         while (rings-- > 0) {
3043                 struct u132_ring *ring = &u132->ring[rings];
3044                 ring->u132 = u132;
3045                 ring->number = rings + 1;
3046                 ring->length = 0;
3047                 ring->curr_endp = NULL;
3048                 INIT_DELAYED_WORK(&ring->scheduler,
3049                                   u132_hcd_ring_work_scheduler);
3050         } down(&u132->sw_lock);
3051         INIT_DELAYED_WORK(&u132->monitor, u132_hcd_monitor_work);
3052         while (ports-- > 0) {
3053                 struct u132_port *port = &u132->port[ports];
3054                 port->u132 = u132;
3055                 port->reset = 0;
3056                 port->enable = 0;
3057                 port->power = 0;
3058                 port->Status = 0;
3059         } while (addrs-- > 0) {
3060                 struct u132_addr *addr = &u132->addr[addrs];
3061                 addr->address = 0;
3062         } while (udevs-- > 0) {
3063                 struct u132_udev *udev = &u132->udev[udevs];
3064                 int i = ARRAY_SIZE(udev->endp_number_in);
3065                 int o = ARRAY_SIZE(udev->endp_number_out);
3066                 udev->usb_device = NULL;
3067                 udev->udev_number = 0;
3068                 udev->usb_addr = 0;
3069                 udev->portnumber = 0;
3070                 while (i-- > 0) {
3071                         udev->endp_number_in[i] = 0;
3072                 }
3073                 while (o-- > 0) {
3074                         udev->endp_number_out[o] = 0;
3075                 }
3076         }
3077         while (endps-- > 0) {
3078                 u132->endp[endps] = NULL;
3079         }
3080         up(&u132->sw_lock);
3081         return;
3082 }
3083
3084 static int __devinit u132_probe(struct platform_device *pdev)
3085 {
3086         struct usb_hcd *hcd;
3087         int retval;
3088         u32 control;
3089         u32 rh_a = -1;
3090         u32 num_ports;
3091         msleep(100);
3092         if (u132_exiting > 0) {
3093                 return -ENODEV;
3094         }
3095         retval = ftdi_write_pcimem(pdev, intrdisable, OHCI_INTR_MIE);
3096         if (retval)
3097                 return retval;
3098         retval = ftdi_read_pcimem(pdev, control, &control);
3099         if (retval)
3100                 return retval;
3101         retval = ftdi_read_pcimem(pdev, roothub.a, &rh_a);
3102         if (retval)
3103                 return retval;
3104         num_ports = rh_a & RH_A_NDP;        /* refuse to confuse usbcore */
3105         if (pdev->dev.dma_mask) {
3106                 return -EINVAL;
3107         }
3108         hcd = usb_create_hcd(&u132_hc_driver, &pdev->dev, pdev->dev.bus_id);
3109         if (!hcd) {
3110                 printk(KERN_ERR "failed to create the usb hcd struct for U132\n"
3111                         );
3112                 ftdi_elan_gone_away(pdev);
3113                 return -ENOMEM;
3114         } else {
3115                 int retval = 0;
3116                 struct u132 *u132 = hcd_to_u132(hcd);
3117                 hcd->rsrc_start = 0;
3118                 mutex_lock(&u132_module_lock);
3119                 list_add_tail(&u132->u132_list, &u132_static_list);
3120                 u132->sequence_num = ++u132_instances;
3121                 mutex_unlock(&u132_module_lock);
3122                 u132_u132_init_kref(u132);
3123                 u132_initialise(u132, pdev);
3124                 hcd->product_desc = "ELAN U132 Host Controller";
3125                 retval = usb_add_hcd(hcd, 0, 0);
3126                 if (retval != 0) {
3127                         dev_err(&u132->platform_dev->dev, "init error %d\n",
3128                                 retval);
3129                         u132_u132_put_kref(u132);
3130                         return retval;
3131                 } else {
3132                         u132_monitor_queue_work(u132, 100);
3133                         return 0;
3134                 }
3135         }
3136 }
3137
3138
3139 #ifdef CONFIG_PM
3140 /* for this device there's no useful distinction between the controller
3141 * and its root hub, except that the root hub only gets direct PM calls
3142 * when CONFIG_USB_SUSPEND is enabled.
3143 */
3144 static int u132_suspend(struct platform_device *pdev, pm_message_t state)
3145 {
3146         struct usb_hcd *hcd = platform_get_drvdata(pdev);
3147         struct u132 *u132 = hcd_to_u132(hcd);
3148         if (u132->going > 1) {
3149                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3150                         , u132->going);
3151                 return -ENODEV;
3152         } else if (u132->going > 0) {
3153                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
3154                 return -ESHUTDOWN;
3155         } else {
3156                 int retval = 0;
3157                 if (state.event == PM_EVENT_FREEZE) {
3158                         retval = u132_bus_suspend(hcd);
3159                 } else if (state.event == PM_EVENT_SUSPEND) {
3160                         int ports = MAX_U132_PORTS;
3161                         while (ports-- > 0) {
3162                                 port_power(u132, ports, 0);
3163                         }
3164                 }
3165                 if (retval == 0)
3166                         pdev->dev.power.power_state = state;
3167                 return retval;
3168         }
3169 }
3170
3171 static int u132_resume(struct platform_device *pdev)
3172 {
3173         struct usb_hcd *hcd = platform_get_drvdata(pdev);
3174         struct u132 *u132 = hcd_to_u132(hcd);
3175         if (u132->going > 1) {
3176                 dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
3177                         , u132->going);
3178                 return -ENODEV;
3179         } else if (u132->going > 0) {
3180                 dev_err(&u132->platform_dev->dev, "device is being removed\n");
3181                 return -ESHUTDOWN;
3182         } else {
3183                 int retval = 0;
3184                 if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
3185                         int ports = MAX_U132_PORTS;
3186                         while (ports-- > 0) {
3187                                 port_power(u132, ports, 1);
3188                         }
3189                         retval = 0;
3190                 } else {
3191                         pdev->dev.power.power_state = PMSG_ON;
3192                         retval = u132_bus_resume(hcd);
3193                 }
3194                 return retval;
3195         }
3196 }
3197
3198 #else
3199 #define u132_suspend NULL
3200 #define u132_resume NULL
3201 #endif
3202 /*
3203 * this driver is loaded explicitly by ftdi_u132
3204 *
3205 * the platform_driver struct is static because it is per type of module
3206 */
3207 static struct platform_driver u132_platform_driver = {
3208         .probe = u132_probe,
3209         .remove = __devexit_p(u132_remove),
3210         .suspend = u132_suspend,
3211         .resume = u132_resume,
3212         .driver = {
3213                    .name = (char *)hcd_name,
3214                    .owner = THIS_MODULE,
3215                    },
3216 };
3217 static int __init u132_hcd_init(void)
3218 {
3219         int retval;
3220         INIT_LIST_HEAD(&u132_static_list);
3221         u132_instances = 0;
3222         u132_exiting = 0;
3223         mutex_init(&u132_module_lock);
3224         if (usb_disabled())
3225                 return -ENODEV;
3226         printk(KERN_INFO "driver %s built at %s on %s\n", hcd_name, __TIME__,
3227                 __DATE__);
3228         workqueue = create_singlethread_workqueue("u132");
3229         retval = platform_driver_register(&u132_platform_driver);
3230         return retval;
3231 }
3232
3233
3234 module_init(u132_hcd_init);
3235 static void __exit u132_hcd_exit(void)
3236 {
3237         struct u132 *u132;
3238         struct u132 *temp;
3239         mutex_lock(&u132_module_lock);
3240         u132_exiting += 1;
3241         mutex_unlock(&u132_module_lock);
3242         list_for_each_entry_safe(u132, temp, &u132_static_list, u132_list) {
3243                 platform_device_unregister(u132->platform_dev);
3244         } platform_driver_unregister(&u132_platform_driver);
3245         printk(KERN_INFO "u132-hcd driver deregistered\n");
3246         wait_event(u132_hcd_wait, u132_instances == 0);
3247         flush_workqueue(workqueue);
3248         destroy_workqueue(workqueue);
3249 }
3250
3251
3252 module_exit(u132_hcd_exit);
3253 MODULE_LICENSE("GPL");