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