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