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