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