• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
3  *
4  * Copyright(C) 2006 Elan Digital Systems Limited
5  * http://www.elandigitalsystems.com
6  *
7  * Author and Maintainer - Tony Olech - Elan Digital Systems
8  * tony.olech@elandigitalsystems.com
9  *
10  * This program is free software;you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation, version 2.
13  *
14  *
15  * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
16  * based on various USB client drivers in the 2.6.15 linux kernel
17  * with constant reference to the 3rd Edition of Linux Device Drivers
18  * published by O'Reilly
19  *
20  * The U132 adapter is a USB to CardBus adapter specifically designed
21  * for PC cards that contain an OHCI host controller. Typical PC cards
22  * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
23  *
24  * The U132 adapter will *NOT *work with PC cards that do not contain
25  * an OHCI controller. A simple way to test whether a PC card has an
26  * OHCI controller as an interface is to insert the PC card directly
27  * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
28  * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
29  * then there is a good chance that the U132 adapter will support the
30  * PC card.(you also need the specific client driver for the PC card)
31  *
32  * Please inform the Author and Maintainer about any PC cards that
33  * contain OHCI Host Controller and work when directly connected to
34  * an embedded CardBus slot but do not work when they are connected
35  * via an ELAN U132 adapter.
36  *
37  */
38 
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40 
41 #include <linux/kernel.h>
42 #include <linux/errno.h>
43 #include <linux/init.h>
44 #include <linux/list.h>
45 #include <linux/ioctl.h>
46 #include <linux/pci_ids.h>
47 #include <linux/slab.h>
48 #include <linux/module.h>
49 #include <linux/kref.h>
50 #include <linux/mutex.h>
51 #include <asm/uaccess.h>
52 #include <linux/usb.h>
53 #include <linux/workqueue.h>
54 #include <linux/platform_device.h>
55 MODULE_AUTHOR("Tony Olech");
56 MODULE_DESCRIPTION("FTDI ELAN driver");
57 MODULE_LICENSE("GPL");
58 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
59 static bool distrust_firmware = 1;
60 module_param(distrust_firmware, bool, 0);
61 MODULE_PARM_DESC(distrust_firmware,
62 		 "true to distrust firmware power/overcurrent setup");
63 extern struct platform_driver u132_platform_driver;
64 static struct workqueue_struct *status_queue;
65 static struct workqueue_struct *command_queue;
66 static struct workqueue_struct *respond_queue;
67 /*
68  * ftdi_module_lock exists to protect access to global variables
69  *
70  */
71 static struct mutex ftdi_module_lock;
72 static int ftdi_instances = 0;
73 static struct list_head ftdi_static_list;
74 /*
75  * end of the global variables protected by ftdi_module_lock
76  */
77 #include "usb_u132.h"
78 #include <asm/io.h>
79 #include <linux/usb/hcd.h>
80 
81 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
82  * If you're going to try stuff like this, you need to split
83  * out shareable stuff (register declarations?) into its own
84  * file, maybe name <linux/usb/ohci.h>
85  */
86 
87 #include "../host/ohci.h"
88 /* Define these values to match your devices*/
89 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
90 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
91 /* table of devices that work with this driver*/
92 static const struct usb_device_id ftdi_elan_table[] = {
93 	{USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
94 	{ /* Terminating entry */ }
95 };
96 
97 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
98 /* only the jtag(firmware upgrade device) interface requires
99  * a device file and corresponding minor number, but the
100  * interface is created unconditionally - I suppose it could
101  * be configured or not according to a module parameter.
102  * But since we(now) require one interface per device,
103  * and since it unlikely that a normal installation would
104  * require more than a couple of elan-ftdi devices, 8 seems
105  * like a reasonable limit to have here, and if someone
106  * really requires more than 8 devices, then they can frig the
107  * code and recompile
108  */
109 #define USB_FTDI_ELAN_MINOR_BASE 192
110 #define COMMAND_BITS 5
111 #define COMMAND_SIZE (1<<COMMAND_BITS)
112 #define COMMAND_MASK (COMMAND_SIZE-1)
113 struct u132_command {
114 	u8 header;
115 	u16 length;
116 	u8 address;
117 	u8 width;
118 	u32 value;
119 	int follows;
120 	void *buffer;
121 };
122 #define RESPOND_BITS 5
123 #define RESPOND_SIZE (1<<RESPOND_BITS)
124 #define RESPOND_MASK (RESPOND_SIZE-1)
125 struct u132_respond {
126 	u8 header;
127 	u8 address;
128 	u32 *value;
129 	int *result;
130 	struct completion wait_completion;
131 };
132 struct u132_target {
133 	void *endp;
134 	struct urb *urb;
135 	int toggle_bits;
136 	int error_count;
137 	int condition_code;
138 	int repeat_number;
139 	int halted;
140 	int skipped;
141 	int actual;
142 	int non_null;
143 	int active;
144 	int abandoning;
145 	void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
146 			 int toggle_bits, int error_count, int condition_code,
147 			 int repeat_number, int halted, int skipped, int actual,
148 			 int non_null);
149 };
150 /* Structure to hold all of our device specific stuff*/
151 struct usb_ftdi {
152 	struct list_head ftdi_list;
153 	struct mutex u132_lock;
154 	int command_next;
155 	int command_head;
156 	struct u132_command command[COMMAND_SIZE];
157 	int respond_next;
158 	int respond_head;
159 	struct u132_respond respond[RESPOND_SIZE];
160 	struct u132_target target[4];
161 	char device_name[16];
162 	unsigned synchronized:1;
163 	unsigned enumerated:1;
164 	unsigned registered:1;
165 	unsigned initialized:1;
166 	unsigned card_ejected:1;
167 	int function;
168 	int sequence_num;
169 	int disconnected;
170 	int gone_away;
171 	int stuck_status;
172 	int status_queue_delay;
173 	struct semaphore sw_lock;
174 	struct usb_device *udev;
175 	struct usb_interface *interface;
176 	struct usb_class_driver *class;
177 	struct delayed_work status_work;
178 	struct delayed_work command_work;
179 	struct delayed_work respond_work;
180 	struct u132_platform_data platform_data;
181 	struct resource resources[0];
182 	struct platform_device platform_dev;
183 	unsigned char *bulk_in_buffer;
184 	size_t bulk_in_size;
185 	size_t bulk_in_last;
186 	size_t bulk_in_left;
187 	__u8 bulk_in_endpointAddr;
188 	__u8 bulk_out_endpointAddr;
189 	struct kref kref;
190 	u32 controlreg;
191 	u8 response[4 + 1024];
192 	int expected;
193 	int received;
194 	int ed_found;
195 };
196 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
197 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
198 						    platform_dev)
199 static struct usb_driver ftdi_elan_driver;
ftdi_elan_delete(struct kref * kref)200 static void ftdi_elan_delete(struct kref *kref)
201 {
202 	struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
203 	dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
204 	usb_put_dev(ftdi->udev);
205 	ftdi->disconnected += 1;
206 	mutex_lock(&ftdi_module_lock);
207 	list_del_init(&ftdi->ftdi_list);
208 	ftdi_instances -= 1;
209 	mutex_unlock(&ftdi_module_lock);
210 	kfree(ftdi->bulk_in_buffer);
211 	ftdi->bulk_in_buffer = NULL;
212 	kfree(ftdi);
213 }
214 
ftdi_elan_put_kref(struct usb_ftdi * ftdi)215 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
216 {
217 	kref_put(&ftdi->kref, ftdi_elan_delete);
218 }
219 
ftdi_elan_get_kref(struct usb_ftdi * ftdi)220 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
221 {
222 	kref_get(&ftdi->kref);
223 }
224 
ftdi_elan_init_kref(struct usb_ftdi * ftdi)225 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
226 {
227 	kref_init(&ftdi->kref);
228 }
229 
ftdi_status_requeue_work(struct usb_ftdi * ftdi,unsigned int delta)230 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
231 {
232 	if (!queue_delayed_work(status_queue, &ftdi->status_work, delta))
233 		kref_put(&ftdi->kref, ftdi_elan_delete);
234 }
235 
ftdi_status_queue_work(struct usb_ftdi * ftdi,unsigned int delta)236 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
237 {
238 	if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
239 		kref_get(&ftdi->kref);
240 }
241 
ftdi_status_cancel_work(struct usb_ftdi * ftdi)242 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
243 {
244 	if (cancel_delayed_work(&ftdi->status_work))
245 		kref_put(&ftdi->kref, ftdi_elan_delete);
246 }
247 
ftdi_command_requeue_work(struct usb_ftdi * ftdi,unsigned int delta)248 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
249 {
250 	if (!queue_delayed_work(command_queue, &ftdi->command_work, delta))
251 		kref_put(&ftdi->kref, ftdi_elan_delete);
252 }
253 
ftdi_command_queue_work(struct usb_ftdi * ftdi,unsigned int delta)254 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
255 {
256 	if (queue_delayed_work(command_queue, &ftdi->command_work, delta))
257 		kref_get(&ftdi->kref);
258 }
259 
ftdi_command_cancel_work(struct usb_ftdi * ftdi)260 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
261 {
262 	if (cancel_delayed_work(&ftdi->command_work))
263 		kref_put(&ftdi->kref, ftdi_elan_delete);
264 }
265 
ftdi_response_requeue_work(struct usb_ftdi * ftdi,unsigned int delta)266 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
267 				       unsigned int delta)
268 {
269 	if (!queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
270 		kref_put(&ftdi->kref, ftdi_elan_delete);
271 }
272 
ftdi_respond_queue_work(struct usb_ftdi * ftdi,unsigned int delta)273 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
274 {
275 	if (queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
276 		kref_get(&ftdi->kref);
277 }
278 
ftdi_response_cancel_work(struct usb_ftdi * ftdi)279 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
280 {
281 	if (cancel_delayed_work(&ftdi->respond_work))
282 		kref_put(&ftdi->kref, ftdi_elan_delete);
283 }
284 
ftdi_elan_gone_away(struct platform_device * pdev)285 void ftdi_elan_gone_away(struct platform_device *pdev)
286 {
287 	struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
288 	ftdi->gone_away += 1;
289 	ftdi_elan_put_kref(ftdi);
290 }
291 
292 
293 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
ftdi_release_platform_dev(struct device * dev)294 static void ftdi_release_platform_dev(struct device *dev)
295 {
296 	dev->parent = NULL;
297 }
298 
299 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
300 				  struct u132_target *target, u8 *buffer, int length);
301 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
302 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
303 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
304 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
305 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
306 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
307 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
308 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
309 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
ftdi_elan_hcd_init(struct usb_ftdi * ftdi)310 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
311 {
312 	int result;
313 	if (ftdi->platform_dev.dev.parent)
314 		return -EBUSY;
315 	ftdi_elan_get_kref(ftdi);
316 	ftdi->platform_data.potpg = 100;
317 	ftdi->platform_data.reset = NULL;
318 	ftdi->platform_dev.id = ftdi->sequence_num;
319 	ftdi->platform_dev.resource = ftdi->resources;
320 	ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
321 	ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
322 	ftdi->platform_dev.dev.parent = NULL;
323 	ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
324 	ftdi->platform_dev.dev.dma_mask = NULL;
325 	snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
326 	ftdi->platform_dev.name = ftdi->device_name;
327 	dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
328 	request_module("u132_hcd");
329 	dev_info(&ftdi->udev->dev, "registering '%s'\n",
330 		 ftdi->platform_dev.name);
331 	result = platform_device_register(&ftdi->platform_dev);
332 	return result;
333 }
334 
ftdi_elan_abandon_completions(struct usb_ftdi * ftdi)335 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
336 {
337 	mutex_lock(&ftdi->u132_lock);
338 	while (ftdi->respond_next > ftdi->respond_head) {
339 		struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
340 							      ftdi->respond_head++];
341 		*respond->result = -ESHUTDOWN;
342 		*respond->value = 0;
343 		complete(&respond->wait_completion);
344 	} mutex_unlock(&ftdi->u132_lock);
345 }
346 
ftdi_elan_abandon_targets(struct usb_ftdi * ftdi)347 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
348 {
349 	int ed_number = 4;
350 	mutex_lock(&ftdi->u132_lock);
351 	while (ed_number-- > 0) {
352 		struct u132_target *target = &ftdi->target[ed_number];
353 		if (target->active == 1) {
354 			target->condition_code = TD_DEVNOTRESP;
355 			mutex_unlock(&ftdi->u132_lock);
356 			ftdi_elan_do_callback(ftdi, target, NULL, 0);
357 			mutex_lock(&ftdi->u132_lock);
358 		}
359 	}
360 	ftdi->received = 0;
361 	ftdi->expected = 4;
362 	ftdi->ed_found = 0;
363 	mutex_unlock(&ftdi->u132_lock);
364 }
365 
ftdi_elan_flush_targets(struct usb_ftdi * ftdi)366 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
367 {
368 	int ed_number = 4;
369 	mutex_lock(&ftdi->u132_lock);
370 	while (ed_number-- > 0) {
371 		struct u132_target *target = &ftdi->target[ed_number];
372 		target->abandoning = 1;
373 	wait_1:if (target->active == 1) {
374 			int command_size = ftdi->command_next -
375 				ftdi->command_head;
376 			if (command_size < COMMAND_SIZE) {
377 				struct u132_command *command = &ftdi->command[
378 					COMMAND_MASK & ftdi->command_next];
379 				command->header = 0x80 | (ed_number << 5) | 0x4;
380 				command->length = 0x00;
381 				command->address = 0x00;
382 				command->width = 0x00;
383 				command->follows = 0;
384 				command->value = 0;
385 				command->buffer = &command->value;
386 				ftdi->command_next += 1;
387 				ftdi_elan_kick_command_queue(ftdi);
388 			} else {
389 				mutex_unlock(&ftdi->u132_lock);
390 				msleep(100);
391 				mutex_lock(&ftdi->u132_lock);
392 				goto wait_1;
393 			}
394 		}
395 	wait_2:if (target->active == 1) {
396 			int command_size = ftdi->command_next -
397 				ftdi->command_head;
398 			if (command_size < COMMAND_SIZE) {
399 				struct u132_command *command = &ftdi->command[
400 					COMMAND_MASK & ftdi->command_next];
401 				command->header = 0x90 | (ed_number << 5);
402 				command->length = 0x00;
403 				command->address = 0x00;
404 				command->width = 0x00;
405 				command->follows = 0;
406 				command->value = 0;
407 				command->buffer = &command->value;
408 				ftdi->command_next += 1;
409 				ftdi_elan_kick_command_queue(ftdi);
410 			} else {
411 				mutex_unlock(&ftdi->u132_lock);
412 				msleep(100);
413 				mutex_lock(&ftdi->u132_lock);
414 				goto wait_2;
415 			}
416 		}
417 	}
418 	ftdi->received = 0;
419 	ftdi->expected = 4;
420 	ftdi->ed_found = 0;
421 	mutex_unlock(&ftdi->u132_lock);
422 }
423 
ftdi_elan_cancel_targets(struct usb_ftdi * ftdi)424 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
425 {
426 	int ed_number = 4;
427 	mutex_lock(&ftdi->u132_lock);
428 	while (ed_number-- > 0) {
429 		struct u132_target *target = &ftdi->target[ed_number];
430 		target->abandoning = 1;
431 	wait:if (target->active == 1) {
432 			int command_size = ftdi->command_next -
433 				ftdi->command_head;
434 			if (command_size < COMMAND_SIZE) {
435 				struct u132_command *command = &ftdi->command[
436 					COMMAND_MASK & ftdi->command_next];
437 				command->header = 0x80 | (ed_number << 5) | 0x4;
438 				command->length = 0x00;
439 				command->address = 0x00;
440 				command->width = 0x00;
441 				command->follows = 0;
442 				command->value = 0;
443 				command->buffer = &command->value;
444 				ftdi->command_next += 1;
445 				ftdi_elan_kick_command_queue(ftdi);
446 			} else {
447 				mutex_unlock(&ftdi->u132_lock);
448 				msleep(100);
449 				mutex_lock(&ftdi->u132_lock);
450 				goto wait;
451 			}
452 		}
453 	}
454 	ftdi->received = 0;
455 	ftdi->expected = 4;
456 	ftdi->ed_found = 0;
457 	mutex_unlock(&ftdi->u132_lock);
458 }
459 
ftdi_elan_kick_command_queue(struct usb_ftdi * ftdi)460 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
461 {
462 	ftdi_command_queue_work(ftdi, 0);
463 }
464 
ftdi_elan_command_work(struct work_struct * work)465 static void ftdi_elan_command_work(struct work_struct *work)
466 {
467 	struct usb_ftdi *ftdi =
468 		container_of(work, struct usb_ftdi, command_work.work);
469 
470 	if (ftdi->disconnected > 0) {
471 		ftdi_elan_put_kref(ftdi);
472 		return;
473 	} else {
474 		int retval = ftdi_elan_command_engine(ftdi);
475 		if (retval == -ESHUTDOWN) {
476 			ftdi->disconnected += 1;
477 		} else if (retval == -ENODEV) {
478 			ftdi->disconnected += 1;
479 		} else if (retval)
480 			dev_err(&ftdi->udev->dev, "command error %d\n", retval);
481 		ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
482 		return;
483 	}
484 }
485 
ftdi_elan_kick_respond_queue(struct usb_ftdi * ftdi)486 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
487 {
488 	ftdi_respond_queue_work(ftdi, 0);
489 }
490 
ftdi_elan_respond_work(struct work_struct * work)491 static void ftdi_elan_respond_work(struct work_struct *work)
492 {
493 	struct usb_ftdi *ftdi =
494 		container_of(work, struct usb_ftdi, respond_work.work);
495 	if (ftdi->disconnected > 0) {
496 		ftdi_elan_put_kref(ftdi);
497 		return;
498 	} else {
499 		int retval = ftdi_elan_respond_engine(ftdi);
500 		if (retval == 0) {
501 		} else if (retval == -ESHUTDOWN) {
502 			ftdi->disconnected += 1;
503 		} else if (retval == -ENODEV) {
504 			ftdi->disconnected += 1;
505 		} else if (retval == -EILSEQ) {
506 			ftdi->disconnected += 1;
507 		} else {
508 			ftdi->disconnected += 1;
509 			dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
510 		}
511 		if (ftdi->disconnected > 0) {
512 			ftdi_elan_abandon_completions(ftdi);
513 			ftdi_elan_abandon_targets(ftdi);
514 		}
515 		ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
516 		return;
517 	}
518 }
519 
520 
521 /*
522  * the sw_lock is initially held and will be freed
523  * after the FTDI has been synchronized
524  *
525  */
ftdi_elan_status_work(struct work_struct * work)526 static void ftdi_elan_status_work(struct work_struct *work)
527 {
528 	struct usb_ftdi *ftdi =
529 		container_of(work, struct usb_ftdi, status_work.work);
530 	int work_delay_in_msec = 0;
531 	if (ftdi->disconnected > 0) {
532 		ftdi_elan_put_kref(ftdi);
533 		return;
534 	} else if (ftdi->synchronized == 0) {
535 		down(&ftdi->sw_lock);
536 		if (ftdi_elan_synchronize(ftdi) == 0) {
537 			ftdi->synchronized = 1;
538 			ftdi_command_queue_work(ftdi, 1);
539 			ftdi_respond_queue_work(ftdi, 1);
540 			up(&ftdi->sw_lock);
541 			work_delay_in_msec = 100;
542 		} else {
543 			dev_err(&ftdi->udev->dev, "synchronize failed\n");
544 			up(&ftdi->sw_lock);
545 			work_delay_in_msec = 10 *1000;
546 		}
547 	} else if (ftdi->stuck_status > 0) {
548 		if (ftdi_elan_stuck_waiting(ftdi) == 0) {
549 			ftdi->stuck_status = 0;
550 			ftdi->synchronized = 0;
551 		} else if ((ftdi->stuck_status++ % 60) == 1) {
552 			dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
553 		} else
554 			dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
555 				ftdi->stuck_status);
556 		work_delay_in_msec = 100;
557 	} else if (ftdi->enumerated == 0) {
558 		if (ftdi_elan_enumeratePCI(ftdi) == 0) {
559 			ftdi->enumerated = 1;
560 			work_delay_in_msec = 250;
561 		} else
562 			work_delay_in_msec = 1000;
563 	} else if (ftdi->initialized == 0) {
564 		if (ftdi_elan_setupOHCI(ftdi) == 0) {
565 			ftdi->initialized = 1;
566 			work_delay_in_msec = 500;
567 		} else {
568 			dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
569 			work_delay_in_msec = 1 *1000;
570 		}
571 	} else if (ftdi->registered == 0) {
572 		work_delay_in_msec = 10;
573 		if (ftdi_elan_hcd_init(ftdi) == 0) {
574 			ftdi->registered = 1;
575 		} else
576 			dev_err(&ftdi->udev->dev, "register failed\n");
577 		work_delay_in_msec = 250;
578 	} else {
579 		if (ftdi_elan_checkingPCI(ftdi) == 0) {
580 			work_delay_in_msec = 250;
581 		} else if (ftdi->controlreg & 0x00400000) {
582 			if (ftdi->gone_away > 0) {
583 				dev_err(&ftdi->udev->dev, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
584 					ftdi->platform_dev.dev.parent,
585 					&ftdi->platform_dev.dev);
586 				platform_device_unregister(&ftdi->platform_dev);
587 				ftdi->platform_dev.dev.parent = NULL;
588 				ftdi->registered = 0;
589 				ftdi->enumerated = 0;
590 				ftdi->card_ejected = 0;
591 				ftdi->initialized = 0;
592 				ftdi->gone_away = 0;
593 			} else
594 				ftdi_elan_flush_targets(ftdi);
595 			work_delay_in_msec = 250;
596 		} else {
597 			dev_err(&ftdi->udev->dev, "PCI device has disappeared\n");
598 			ftdi_elan_cancel_targets(ftdi);
599 			work_delay_in_msec = 500;
600 			ftdi->enumerated = 0;
601 			ftdi->initialized = 0;
602 		}
603 	}
604 	if (ftdi->disconnected > 0) {
605 		ftdi_elan_put_kref(ftdi);
606 		return;
607 	} else {
608 		ftdi_status_requeue_work(ftdi,
609 					 msecs_to_jiffies(work_delay_in_msec));
610 		return;
611 	}
612 }
613 
614 
615 /*
616  * file_operations for the jtag interface
617  *
618  * the usage count for the device is incremented on open()
619  * and decremented on release()
620  */
ftdi_elan_open(struct inode * inode,struct file * file)621 static int ftdi_elan_open(struct inode *inode, struct file *file)
622 {
623 	int subminor;
624 	struct usb_interface *interface;
625 
626 	subminor = iminor(inode);
627 	interface = usb_find_interface(&ftdi_elan_driver, subminor);
628 
629 	if (!interface) {
630 		pr_err("can't find device for minor %d\n", subminor);
631 		return -ENODEV;
632 	} else {
633 		struct usb_ftdi *ftdi = usb_get_intfdata(interface);
634 		if (!ftdi) {
635 			return -ENODEV;
636 		} else {
637 			if (down_interruptible(&ftdi->sw_lock)) {
638 				return -EINTR;
639 			} else {
640 				ftdi_elan_get_kref(ftdi);
641 				file->private_data = ftdi;
642 				return 0;
643 			}
644 		}
645 	}
646 }
647 
ftdi_elan_release(struct inode * inode,struct file * file)648 static int ftdi_elan_release(struct inode *inode, struct file *file)
649 {
650 	struct usb_ftdi *ftdi = file->private_data;
651 	if (ftdi == NULL)
652 		return -ENODEV;
653 	up(&ftdi->sw_lock);        /* decrement the count on our device */
654 	ftdi_elan_put_kref(ftdi);
655 	return 0;
656 }
657 
658 
659 /*
660  *
661  * blocking bulk reads are used to get data from the device
662  *
663  */
ftdi_elan_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)664 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
665 			      size_t count, loff_t *ppos)
666 {
667 	char data[30 *3 + 4];
668 	char *d = data;
669 	int m = (sizeof(data) - 1) / 3;
670 	int bytes_read = 0;
671 	int retry_on_empty = 10;
672 	int retry_on_timeout = 5;
673 	struct usb_ftdi *ftdi = file->private_data;
674 	if (ftdi->disconnected > 0) {
675 		return -ENODEV;
676 	}
677 	data[0] = 0;
678 have:if (ftdi->bulk_in_left > 0) {
679 		if (count-- > 0) {
680 			char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
681 			ftdi->bulk_in_left -= 1;
682 			if (bytes_read < m) {
683 				d += sprintf(d, " %02X", 0x000000FF & *p);
684 			} else if (bytes_read > m) {
685 			} else
686 				d += sprintf(d, " ..");
687 			if (copy_to_user(buffer++, p, 1)) {
688 				return -EFAULT;
689 			} else {
690 				bytes_read += 1;
691 				goto have;
692 			}
693 		} else
694 			return bytes_read;
695 	}
696 more:if (count > 0) {
697 		int packet_bytes = 0;
698 		int retval = usb_bulk_msg(ftdi->udev,
699 					  usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
700 					  ftdi->bulk_in_buffer, ftdi->bulk_in_size,
701 					  &packet_bytes, 50);
702 		if (packet_bytes > 2) {
703 			ftdi->bulk_in_left = packet_bytes - 2;
704 			ftdi->bulk_in_last = 1;
705 			goto have;
706 		} else if (retval == -ETIMEDOUT) {
707 			if (retry_on_timeout-- > 0) {
708 				goto more;
709 			} else if (bytes_read > 0) {
710 				return bytes_read;
711 			} else
712 				return retval;
713 		} else if (retval == 0) {
714 			if (retry_on_empty-- > 0) {
715 				goto more;
716 			} else
717 				return bytes_read;
718 		} else
719 			return retval;
720 	} else
721 		return bytes_read;
722 }
723 
ftdi_elan_write_bulk_callback(struct urb * urb)724 static void ftdi_elan_write_bulk_callback(struct urb *urb)
725 {
726 	struct usb_ftdi *ftdi = urb->context;
727 	int status = urb->status;
728 
729 	if (status && !(status == -ENOENT || status == -ECONNRESET ||
730 			status == -ESHUTDOWN)) {
731 		dev_err(&ftdi->udev->dev,
732 			"urb=%p write bulk status received: %d\n", urb, status);
733 	}
734 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
735 			  urb->transfer_buffer, urb->transfer_dma);
736 }
737 
fill_buffer_with_all_queued_commands(struct usb_ftdi * ftdi,char * buf,int command_size,int total_size)738 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
739 						char *buf, int command_size, int total_size)
740 {
741 	int ed_commands = 0;
742 	int b = 0;
743 	int I = command_size;
744 	int i = ftdi->command_head;
745 	while (I-- > 0) {
746 		struct u132_command *command = &ftdi->command[COMMAND_MASK &
747 							      i++];
748 		int F = command->follows;
749 		u8 *f = command->buffer;
750 		if (command->header & 0x80) {
751 			ed_commands |= 1 << (0x3 & (command->header >> 5));
752 		}
753 		buf[b++] = command->header;
754 		buf[b++] = (command->length >> 0) & 0x00FF;
755 		buf[b++] = (command->length >> 8) & 0x00FF;
756 		buf[b++] = command->address;
757 		buf[b++] = command->width;
758 		while (F-- > 0) {
759 			buf[b++] = *f++;
760 		}
761 	}
762 	return ed_commands;
763 }
764 
ftdi_elan_total_command_size(struct usb_ftdi * ftdi,int command_size)765 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
766 {
767 	int total_size = 0;
768 	int I = command_size;
769 	int i = ftdi->command_head;
770 	while (I-- > 0) {
771 		struct u132_command *command = &ftdi->command[COMMAND_MASK &
772 							      i++];
773 		total_size += 5 + command->follows;
774 	} return total_size;
775 }
776 
ftdi_elan_command_engine(struct usb_ftdi * ftdi)777 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
778 {
779 	int retval;
780 	char *buf;
781 	int ed_commands;
782 	int total_size;
783 	struct urb *urb;
784 	int command_size = ftdi->command_next - ftdi->command_head;
785 	if (command_size == 0)
786 		return 0;
787 	total_size = ftdi_elan_total_command_size(ftdi, command_size);
788 	urb = usb_alloc_urb(0, GFP_KERNEL);
789 	if (!urb) {
790 		dev_err(&ftdi->udev->dev, "could not get a urb to write %d commands totaling %d bytes to the Uxxx\n",
791 			command_size, total_size);
792 		return -ENOMEM;
793 	}
794 	buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
795 				 &urb->transfer_dma);
796 	if (!buf) {
797 		dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
798 			command_size, total_size);
799 		usb_free_urb(urb);
800 		return -ENOMEM;
801 	}
802 	ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
803 							   command_size, total_size);
804 	usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
805 							   ftdi->bulk_out_endpointAddr), buf, total_size,
806 			  ftdi_elan_write_bulk_callback, ftdi);
807 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
808 	if (ed_commands) {
809 		char diag[40 *3 + 4];
810 		char *d = diag;
811 		int m = total_size;
812 		u8 *c = buf;
813 		int s = (sizeof(diag) - 1) / 3;
814 		diag[0] = 0;
815 		while (s-- > 0 && m-- > 0) {
816 			if (s > 0 || m == 0) {
817 				d += sprintf(d, " %02X", *c++);
818 			} else
819 				d += sprintf(d, " ..");
820 		}
821 	}
822 	retval = usb_submit_urb(urb, GFP_KERNEL);
823 	if (retval) {
824 		dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
825 			retval, urb, command_size, total_size);
826 		usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
827 		usb_free_urb(urb);
828 		return retval;
829 	}
830 	usb_free_urb(urb);        /* release our reference to this urb,
831 				     the USB core will eventually free it entirely */
832 	ftdi->command_head += command_size;
833 	ftdi_elan_kick_respond_queue(ftdi);
834 	return 0;
835 }
836 
ftdi_elan_do_callback(struct usb_ftdi * ftdi,struct u132_target * target,u8 * buffer,int length)837 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
838 				  struct u132_target *target, u8 *buffer, int length)
839 {
840 	struct urb *urb = target->urb;
841 	int halted = target->halted;
842 	int skipped = target->skipped;
843 	int actual = target->actual;
844 	int non_null = target->non_null;
845 	int toggle_bits = target->toggle_bits;
846 	int error_count = target->error_count;
847 	int condition_code = target->condition_code;
848 	int repeat_number = target->repeat_number;
849 	void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
850 			  int, int, int, int) = target->callback;
851 	target->active -= 1;
852 	target->callback = NULL;
853 	(*callback) (target->endp, urb, buffer, length, toggle_bits,
854 		     error_count, condition_code, repeat_number, halted, skipped,
855 		     actual, non_null);
856 }
857 
have_ed_set_response(struct usb_ftdi * ftdi,struct u132_target * target,u16 ed_length,int ed_number,int ed_type,char * b)858 static char *have_ed_set_response(struct usb_ftdi *ftdi,
859 				  struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
860 				  char *b)
861 {
862 	int payload = (ed_length >> 0) & 0x07FF;
863 	mutex_lock(&ftdi->u132_lock);
864 	target->actual = 0;
865 	target->non_null = (ed_length >> 15) & 0x0001;
866 	target->repeat_number = (ed_length >> 11) & 0x000F;
867 	if (ed_type == 0x02) {
868 		if (payload == 0 || target->abandoning > 0) {
869 			target->abandoning = 0;
870 			mutex_unlock(&ftdi->u132_lock);
871 			ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
872 					      payload);
873 			ftdi->received = 0;
874 			ftdi->expected = 4;
875 			ftdi->ed_found = 0;
876 			return ftdi->response;
877 		} else {
878 			ftdi->expected = 4 + payload;
879 			ftdi->ed_found = 1;
880 			mutex_unlock(&ftdi->u132_lock);
881 			return b;
882 		}
883 	} else if (ed_type == 0x03) {
884 		if (payload == 0 || target->abandoning > 0) {
885 			target->abandoning = 0;
886 			mutex_unlock(&ftdi->u132_lock);
887 			ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
888 					      payload);
889 			ftdi->received = 0;
890 			ftdi->expected = 4;
891 			ftdi->ed_found = 0;
892 			return ftdi->response;
893 		} else {
894 			ftdi->expected = 4 + payload;
895 			ftdi->ed_found = 1;
896 			mutex_unlock(&ftdi->u132_lock);
897 			return b;
898 		}
899 	} else if (ed_type == 0x01) {
900 		target->abandoning = 0;
901 		mutex_unlock(&ftdi->u132_lock);
902 		ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
903 				      payload);
904 		ftdi->received = 0;
905 		ftdi->expected = 4;
906 		ftdi->ed_found = 0;
907 		return ftdi->response;
908 	} else {
909 		target->abandoning = 0;
910 		mutex_unlock(&ftdi->u132_lock);
911 		ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
912 				      payload);
913 		ftdi->received = 0;
914 		ftdi->expected = 4;
915 		ftdi->ed_found = 0;
916 		return ftdi->response;
917 	}
918 }
919 
have_ed_get_response(struct usb_ftdi * ftdi,struct u132_target * target,u16 ed_length,int ed_number,int ed_type,char * b)920 static char *have_ed_get_response(struct usb_ftdi *ftdi,
921 				  struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
922 				  char *b)
923 {
924 	mutex_lock(&ftdi->u132_lock);
925 	target->condition_code = TD_DEVNOTRESP;
926 	target->actual = (ed_length >> 0) & 0x01FF;
927 	target->non_null = (ed_length >> 15) & 0x0001;
928 	target->repeat_number = (ed_length >> 11) & 0x000F;
929 	mutex_unlock(&ftdi->u132_lock);
930 	if (target->active)
931 		ftdi_elan_do_callback(ftdi, target, NULL, 0);
932 	target->abandoning = 0;
933 	ftdi->received = 0;
934 	ftdi->expected = 4;
935 	ftdi->ed_found = 0;
936 	return ftdi->response;
937 }
938 
939 
940 /*
941  * The engine tries to empty the FTDI fifo
942  *
943  * all responses found in the fifo data are dispatched thus
944  * the response buffer can only ever hold a maximum sized
945  * response from the Uxxx.
946  *
947  */
ftdi_elan_respond_engine(struct usb_ftdi * ftdi)948 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
949 {
950 	u8 *b = ftdi->response + ftdi->received;
951 	int bytes_read = 0;
952 	int retry_on_empty = 1;
953 	int retry_on_timeout = 3;
954 	int empty_packets = 0;
955 read:{
956 		int packet_bytes = 0;
957 		int retval = usb_bulk_msg(ftdi->udev,
958 					  usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
959 					  ftdi->bulk_in_buffer, ftdi->bulk_in_size,
960 					  &packet_bytes, 500);
961 		char diag[30 *3 + 4];
962 		char *d = diag;
963 		int m = packet_bytes;
964 		u8 *c = ftdi->bulk_in_buffer;
965 		int s = (sizeof(diag) - 1) / 3;
966 		diag[0] = 0;
967 		while (s-- > 0 && m-- > 0) {
968 			if (s > 0 || m == 0) {
969 				d += sprintf(d, " %02X", *c++);
970 			} else
971 				d += sprintf(d, " ..");
972 		}
973 		if (packet_bytes > 2) {
974 			ftdi->bulk_in_left = packet_bytes - 2;
975 			ftdi->bulk_in_last = 1;
976 			goto have;
977 		} else if (retval == -ETIMEDOUT) {
978 			if (retry_on_timeout-- > 0) {
979 				dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
980 					packet_bytes, bytes_read, diag);
981 				goto more;
982 			} else if (bytes_read > 0) {
983 				dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
984 					bytes_read, diag);
985 				return -ENOMEM;
986 			} else {
987 				dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
988 					packet_bytes, bytes_read, diag);
989 				return -ENOMEM;
990 			}
991 		} else if (retval == -EILSEQ) {
992 			dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
993 				retval, packet_bytes, bytes_read, diag);
994 			return retval;
995 		} else if (retval) {
996 			dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
997 				retval, packet_bytes, bytes_read, diag);
998 			return retval;
999 		} else if (packet_bytes == 2) {
1000 			unsigned char s0 = ftdi->bulk_in_buffer[0];
1001 			unsigned char s1 = ftdi->bulk_in_buffer[1];
1002 			empty_packets += 1;
1003 			if (s0 == 0x31 && s1 == 0x60) {
1004 				if (retry_on_empty-- > 0) {
1005 					goto more;
1006 				} else
1007 					return 0;
1008 			} else if (s0 == 0x31 && s1 == 0x00) {
1009 				if (retry_on_empty-- > 0) {
1010 					goto more;
1011 				} else
1012 					return 0;
1013 			} else {
1014 				if (retry_on_empty-- > 0) {
1015 					goto more;
1016 				} else
1017 					return 0;
1018 			}
1019 		} else if (packet_bytes == 1) {
1020 			if (retry_on_empty-- > 0) {
1021 				goto more;
1022 			} else
1023 				return 0;
1024 		} else {
1025 			if (retry_on_empty-- > 0) {
1026 				goto more;
1027 			} else
1028 				return 0;
1029 		}
1030 	}
1031 more:{
1032 		goto read;
1033 	}
1034 have:if (ftdi->bulk_in_left > 0) {
1035 		u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1036 		bytes_read += 1;
1037 		ftdi->bulk_in_left -= 1;
1038 		if (ftdi->received == 0 && c == 0xFF) {
1039 			goto have;
1040 		} else
1041 			*b++ = c;
1042 		if (++ftdi->received < ftdi->expected) {
1043 			goto have;
1044 		} else if (ftdi->ed_found) {
1045 			int ed_number = (ftdi->response[0] >> 5) & 0x03;
1046 			u16 ed_length = (ftdi->response[2] << 8) |
1047 				ftdi->response[1];
1048 			struct u132_target *target = &ftdi->target[ed_number];
1049 			int payload = (ed_length >> 0) & 0x07FF;
1050 			char diag[30 *3 + 4];
1051 			char *d = diag;
1052 			int m = payload;
1053 			u8 *c = 4 + ftdi->response;
1054 			int s = (sizeof(diag) - 1) / 3;
1055 			diag[0] = 0;
1056 			while (s-- > 0 && m-- > 0) {
1057 				if (s > 0 || m == 0) {
1058 					d += sprintf(d, " %02X", *c++);
1059 				} else
1060 					d += sprintf(d, " ..");
1061 			}
1062 			ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1063 					      payload);
1064 			ftdi->received = 0;
1065 			ftdi->expected = 4;
1066 			ftdi->ed_found = 0;
1067 			b = ftdi->response;
1068 			goto have;
1069 		} else if (ftdi->expected == 8) {
1070 			u8 buscmd;
1071 			int respond_head = ftdi->respond_head++;
1072 			struct u132_respond *respond = &ftdi->respond[
1073 				RESPOND_MASK & respond_head];
1074 			u32 data = ftdi->response[7];
1075 			data <<= 8;
1076 			data |= ftdi->response[6];
1077 			data <<= 8;
1078 			data |= ftdi->response[5];
1079 			data <<= 8;
1080 			data |= ftdi->response[4];
1081 			*respond->value = data;
1082 			*respond->result = 0;
1083 			complete(&respond->wait_completion);
1084 			ftdi->received = 0;
1085 			ftdi->expected = 4;
1086 			ftdi->ed_found = 0;
1087 			b = ftdi->response;
1088 			buscmd = (ftdi->response[0] >> 0) & 0x0F;
1089 			if (buscmd == 0x00) {
1090 			} else if (buscmd == 0x02) {
1091 			} else if (buscmd == 0x06) {
1092 			} else if (buscmd == 0x0A) {
1093 			} else
1094 				dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1095 					buscmd, data);
1096 			goto have;
1097 		} else {
1098 			if ((ftdi->response[0] & 0x80) == 0x00) {
1099 				ftdi->expected = 8;
1100 				goto have;
1101 			} else {
1102 				int ed_number = (ftdi->response[0] >> 5) & 0x03;
1103 				int ed_type = (ftdi->response[0] >> 0) & 0x03;
1104 				u16 ed_length = (ftdi->response[2] << 8) |
1105 					ftdi->response[1];
1106 				struct u132_target *target = &ftdi->target[
1107 					ed_number];
1108 				target->halted = (ftdi->response[0] >> 3) &
1109 					0x01;
1110 				target->skipped = (ftdi->response[0] >> 2) &
1111 					0x01;
1112 				target->toggle_bits = (ftdi->response[3] >> 6)
1113 					& 0x03;
1114 				target->error_count = (ftdi->response[3] >> 4)
1115 					& 0x03;
1116 				target->condition_code = (ftdi->response[
1117 								  3] >> 0) & 0x0F;
1118 				if ((ftdi->response[0] & 0x10) == 0x00) {
1119 					b = have_ed_set_response(ftdi, target,
1120 								 ed_length, ed_number, ed_type,
1121 								 b);
1122 					goto have;
1123 				} else {
1124 					b = have_ed_get_response(ftdi, target,
1125 								 ed_length, ed_number, ed_type,
1126 								 b);
1127 					goto have;
1128 				}
1129 			}
1130 		}
1131 	} else
1132 		goto more;
1133 }
1134 
1135 
1136 /*
1137  * create a urb, and a buffer for it, and copy the data to the urb
1138  *
1139  */
ftdi_elan_write(struct file * file,const char __user * user_buffer,size_t count,loff_t * ppos)1140 static ssize_t ftdi_elan_write(struct file *file,
1141 			       const char __user *user_buffer, size_t count,
1142 			       loff_t *ppos)
1143 {
1144 	int retval = 0;
1145 	struct urb *urb;
1146 	char *buf;
1147 	struct usb_ftdi *ftdi = file->private_data;
1148 
1149 	if (ftdi->disconnected > 0) {
1150 		return -ENODEV;
1151 	}
1152 	if (count == 0) {
1153 		goto exit;
1154 	}
1155 	urb = usb_alloc_urb(0, GFP_KERNEL);
1156 	if (!urb) {
1157 		retval = -ENOMEM;
1158 		goto error_1;
1159 	}
1160 	buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1161 				 &urb->transfer_dma);
1162 	if (!buf) {
1163 		retval = -ENOMEM;
1164 		goto error_2;
1165 	}
1166 	if (copy_from_user(buf, user_buffer, count)) {
1167 		retval = -EFAULT;
1168 		goto error_3;
1169 	}
1170 	usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1171 							   ftdi->bulk_out_endpointAddr), buf, count,
1172 			  ftdi_elan_write_bulk_callback, ftdi);
1173 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1174 	retval = usb_submit_urb(urb, GFP_KERNEL);
1175 	if (retval) {
1176 		dev_err(&ftdi->udev->dev,
1177 			"failed submitting write urb, error %d\n", retval);
1178 		goto error_3;
1179 	}
1180 	usb_free_urb(urb);
1181 
1182 exit:
1183 	return count;
1184 error_3:
1185 	usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1186 error_2:
1187 	usb_free_urb(urb);
1188 error_1:
1189 	return retval;
1190 }
1191 
1192 static const struct file_operations ftdi_elan_fops = {
1193 	.owner = THIS_MODULE,
1194 	.llseek = no_llseek,
1195 	.read = ftdi_elan_read,
1196 	.write = ftdi_elan_write,
1197 	.open = ftdi_elan_open,
1198 	.release = ftdi_elan_release,
1199 };
1200 
1201 /*
1202  * usb class driver info in order to get a minor number from the usb core,
1203  * and to have the device registered with the driver core
1204  */
1205 static struct usb_class_driver ftdi_elan_jtag_class = {
1206 	.name = "ftdi-%d-jtag",
1207 	.fops = &ftdi_elan_fops,
1208 	.minor_base = USB_FTDI_ELAN_MINOR_BASE,
1209 };
1210 
1211 /*
1212  * the following definitions are for the
1213  * ELAN FPGA state machgine processor that
1214  * lies on the other side of the FTDI chip
1215  */
1216 #define cPCIu132rd 0x0
1217 #define cPCIu132wr 0x1
1218 #define cPCIiord 0x2
1219 #define cPCIiowr 0x3
1220 #define cPCImemrd 0x6
1221 #define cPCImemwr 0x7
1222 #define cPCIcfgrd 0xA
1223 #define cPCIcfgwr 0xB
1224 #define cPCInull 0xF
1225 #define cU132cmd_status 0x0
1226 #define cU132flash 0x1
1227 #define cPIDsetup 0x0
1228 #define cPIDout 0x1
1229 #define cPIDin 0x2
1230 #define cPIDinonce 0x3
1231 #define cCCnoerror 0x0
1232 #define cCCcrc 0x1
1233 #define cCCbitstuff 0x2
1234 #define cCCtoggle 0x3
1235 #define cCCstall 0x4
1236 #define cCCnoresp 0x5
1237 #define cCCbadpid1 0x6
1238 #define cCCbadpid2 0x7
1239 #define cCCdataoverrun 0x8
1240 #define cCCdataunderrun 0x9
1241 #define cCCbuffoverrun 0xC
1242 #define cCCbuffunderrun 0xD
1243 #define cCCnotaccessed 0xF
ftdi_elan_write_reg(struct usb_ftdi * ftdi,u32 data)1244 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1245 {
1246 wait:if (ftdi->disconnected > 0) {
1247 		return -ENODEV;
1248 	} else {
1249 		int command_size;
1250 		mutex_lock(&ftdi->u132_lock);
1251 		command_size = ftdi->command_next - ftdi->command_head;
1252 		if (command_size < COMMAND_SIZE) {
1253 			struct u132_command *command = &ftdi->command[
1254 				COMMAND_MASK & ftdi->command_next];
1255 			command->header = 0x00 | cPCIu132wr;
1256 			command->length = 0x04;
1257 			command->address = 0x00;
1258 			command->width = 0x00;
1259 			command->follows = 4;
1260 			command->value = data;
1261 			command->buffer = &command->value;
1262 			ftdi->command_next += 1;
1263 			ftdi_elan_kick_command_queue(ftdi);
1264 			mutex_unlock(&ftdi->u132_lock);
1265 			return 0;
1266 		} else {
1267 			mutex_unlock(&ftdi->u132_lock);
1268 			msleep(100);
1269 			goto wait;
1270 		}
1271 	}
1272 }
1273 
ftdi_elan_write_config(struct usb_ftdi * ftdi,int config_offset,u8 width,u32 data)1274 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1275 				  u8 width, u32 data)
1276 {
1277 	u8 addressofs = config_offset / 4;
1278 wait:if (ftdi->disconnected > 0) {
1279 		return -ENODEV;
1280 	} else {
1281 		int command_size;
1282 		mutex_lock(&ftdi->u132_lock);
1283 		command_size = ftdi->command_next - ftdi->command_head;
1284 		if (command_size < COMMAND_SIZE) {
1285 			struct u132_command *command = &ftdi->command[
1286 				COMMAND_MASK & ftdi->command_next];
1287 			command->header = 0x00 | (cPCIcfgwr & 0x0F);
1288 			command->length = 0x04;
1289 			command->address = addressofs;
1290 			command->width = 0x00 | (width & 0x0F);
1291 			command->follows = 4;
1292 			command->value = data;
1293 			command->buffer = &command->value;
1294 			ftdi->command_next += 1;
1295 			ftdi_elan_kick_command_queue(ftdi);
1296 			mutex_unlock(&ftdi->u132_lock);
1297 			return 0;
1298 		} else {
1299 			mutex_unlock(&ftdi->u132_lock);
1300 			msleep(100);
1301 			goto wait;
1302 		}
1303 	}
1304 }
1305 
ftdi_elan_write_pcimem(struct usb_ftdi * ftdi,int mem_offset,u8 width,u32 data)1306 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1307 				  u8 width, u32 data)
1308 {
1309 	u8 addressofs = mem_offset / 4;
1310 wait:if (ftdi->disconnected > 0) {
1311 		return -ENODEV;
1312 	} else {
1313 		int command_size;
1314 		mutex_lock(&ftdi->u132_lock);
1315 		command_size = ftdi->command_next - ftdi->command_head;
1316 		if (command_size < COMMAND_SIZE) {
1317 			struct u132_command *command = &ftdi->command[
1318 				COMMAND_MASK & ftdi->command_next];
1319 			command->header = 0x00 | (cPCImemwr & 0x0F);
1320 			command->length = 0x04;
1321 			command->address = addressofs;
1322 			command->width = 0x00 | (width & 0x0F);
1323 			command->follows = 4;
1324 			command->value = data;
1325 			command->buffer = &command->value;
1326 			ftdi->command_next += 1;
1327 			ftdi_elan_kick_command_queue(ftdi);
1328 			mutex_unlock(&ftdi->u132_lock);
1329 			return 0;
1330 		} else {
1331 			mutex_unlock(&ftdi->u132_lock);
1332 			msleep(100);
1333 			goto wait;
1334 		}
1335 	}
1336 }
1337 
usb_ftdi_elan_write_pcimem(struct platform_device * pdev,int mem_offset,u8 width,u32 data)1338 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1339 			       u8 width, u32 data)
1340 {
1341 	struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1342 	return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1343 }
1344 
1345 
1346 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
ftdi_elan_read_reg(struct usb_ftdi * ftdi,u32 * data)1347 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1348 {
1349 wait:if (ftdi->disconnected > 0) {
1350 		return -ENODEV;
1351 	} else {
1352 		int command_size;
1353 		int respond_size;
1354 		mutex_lock(&ftdi->u132_lock);
1355 		command_size = ftdi->command_next - ftdi->command_head;
1356 		respond_size = ftdi->respond_next - ftdi->respond_head;
1357 		if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1358 		{
1359 			struct u132_command *command = &ftdi->command[
1360 				COMMAND_MASK & ftdi->command_next];
1361 			struct u132_respond *respond = &ftdi->respond[
1362 				RESPOND_MASK & ftdi->respond_next];
1363 			int result = -ENODEV;
1364 			respond->result = &result;
1365 			respond->header = command->header = 0x00 | cPCIu132rd;
1366 			command->length = 0x04;
1367 			respond->address = command->address = cU132cmd_status;
1368 			command->width = 0x00;
1369 			command->follows = 0;
1370 			command->value = 0;
1371 			command->buffer = NULL;
1372 			respond->value = data;
1373 			init_completion(&respond->wait_completion);
1374 			ftdi->command_next += 1;
1375 			ftdi->respond_next += 1;
1376 			ftdi_elan_kick_command_queue(ftdi);
1377 			mutex_unlock(&ftdi->u132_lock);
1378 			wait_for_completion(&respond->wait_completion);
1379 			return result;
1380 		} else {
1381 			mutex_unlock(&ftdi->u132_lock);
1382 			msleep(100);
1383 			goto wait;
1384 		}
1385 	}
1386 }
1387 
ftdi_elan_read_config(struct usb_ftdi * ftdi,int config_offset,u8 width,u32 * data)1388 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1389 				 u8 width, u32 *data)
1390 {
1391 	u8 addressofs = config_offset / 4;
1392 wait:if (ftdi->disconnected > 0) {
1393 		return -ENODEV;
1394 	} else {
1395 		int command_size;
1396 		int respond_size;
1397 		mutex_lock(&ftdi->u132_lock);
1398 		command_size = ftdi->command_next - ftdi->command_head;
1399 		respond_size = ftdi->respond_next - ftdi->respond_head;
1400 		if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1401 		{
1402 			struct u132_command *command = &ftdi->command[
1403 				COMMAND_MASK & ftdi->command_next];
1404 			struct u132_respond *respond = &ftdi->respond[
1405 				RESPOND_MASK & ftdi->respond_next];
1406 			int result = -ENODEV;
1407 			respond->result = &result;
1408 			respond->header = command->header = 0x00 | (cPCIcfgrd &
1409 								    0x0F);
1410 			command->length = 0x04;
1411 			respond->address = command->address = addressofs;
1412 			command->width = 0x00 | (width & 0x0F);
1413 			command->follows = 0;
1414 			command->value = 0;
1415 			command->buffer = NULL;
1416 			respond->value = data;
1417 			init_completion(&respond->wait_completion);
1418 			ftdi->command_next += 1;
1419 			ftdi->respond_next += 1;
1420 			ftdi_elan_kick_command_queue(ftdi);
1421 			mutex_unlock(&ftdi->u132_lock);
1422 			wait_for_completion(&respond->wait_completion);
1423 			return result;
1424 		} else {
1425 			mutex_unlock(&ftdi->u132_lock);
1426 			msleep(100);
1427 			goto wait;
1428 		}
1429 	}
1430 }
1431 
ftdi_elan_read_pcimem(struct usb_ftdi * ftdi,int mem_offset,u8 width,u32 * data)1432 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1433 				 u8 width, u32 *data)
1434 {
1435 	u8 addressofs = mem_offset / 4;
1436 wait:if (ftdi->disconnected > 0) {
1437 		return -ENODEV;
1438 	} else {
1439 		int command_size;
1440 		int respond_size;
1441 		mutex_lock(&ftdi->u132_lock);
1442 		command_size = ftdi->command_next - ftdi->command_head;
1443 		respond_size = ftdi->respond_next - ftdi->respond_head;
1444 		if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1445 		{
1446 			struct u132_command *command = &ftdi->command[
1447 				COMMAND_MASK & ftdi->command_next];
1448 			struct u132_respond *respond = &ftdi->respond[
1449 				RESPOND_MASK & ftdi->respond_next];
1450 			int result = -ENODEV;
1451 			respond->result = &result;
1452 			respond->header = command->header = 0x00 | (cPCImemrd &
1453 								    0x0F);
1454 			command->length = 0x04;
1455 			respond->address = command->address = addressofs;
1456 			command->width = 0x00 | (width & 0x0F);
1457 			command->follows = 0;
1458 			command->value = 0;
1459 			command->buffer = NULL;
1460 			respond->value = data;
1461 			init_completion(&respond->wait_completion);
1462 			ftdi->command_next += 1;
1463 			ftdi->respond_next += 1;
1464 			ftdi_elan_kick_command_queue(ftdi);
1465 			mutex_unlock(&ftdi->u132_lock);
1466 			wait_for_completion(&respond->wait_completion);
1467 			return result;
1468 		} else {
1469 			mutex_unlock(&ftdi->u132_lock);
1470 			msleep(100);
1471 			goto wait;
1472 		}
1473 	}
1474 }
1475 
usb_ftdi_elan_read_pcimem(struct platform_device * pdev,int mem_offset,u8 width,u32 * data)1476 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1477 			      u8 width, u32 *data)
1478 {
1479 	struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1480 	if (ftdi->initialized == 0) {
1481 		return -ENODEV;
1482 	} else
1483 		return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1484 }
1485 
1486 
1487 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
ftdi_elan_edset_setup(struct usb_ftdi * ftdi,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,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))1488 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1489 				 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1490 				 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1491 						   int toggle_bits, int error_count, int condition_code, int repeat_number,
1492 						   int halted, int skipped, int actual, int non_null))
1493 {
1494 	u8 ed = ed_number - 1;
1495 wait:if (ftdi->disconnected > 0) {
1496 		return -ENODEV;
1497 	} else if (ftdi->initialized == 0) {
1498 		return -ENODEV;
1499 	} else {
1500 		int command_size;
1501 		mutex_lock(&ftdi->u132_lock);
1502 		command_size = ftdi->command_next - ftdi->command_head;
1503 		if (command_size < COMMAND_SIZE) {
1504 			struct u132_target *target = &ftdi->target[ed];
1505 			struct u132_command *command = &ftdi->command[
1506 				COMMAND_MASK & ftdi->command_next];
1507 			command->header = 0x80 | (ed << 5);
1508 			command->length = 0x8007;
1509 			command->address = (toggle_bits << 6) | (ep_number << 2)
1510 				| (address << 0);
1511 			command->width = usb_maxpacket(urb->dev, urb->pipe,
1512 						       usb_pipeout(urb->pipe));
1513 			command->follows = 8;
1514 			command->value = 0;
1515 			command->buffer = urb->setup_packet;
1516 			target->callback = callback;
1517 			target->endp = endp;
1518 			target->urb = urb;
1519 			target->active = 1;
1520 			ftdi->command_next += 1;
1521 			ftdi_elan_kick_command_queue(ftdi);
1522 			mutex_unlock(&ftdi->u132_lock);
1523 			return 0;
1524 		} else {
1525 			mutex_unlock(&ftdi->u132_lock);
1526 			msleep(100);
1527 			goto wait;
1528 		}
1529 	}
1530 }
1531 
usb_ftdi_elan_edset_setup(struct platform_device * pdev,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,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))1532 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1533 			      void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1534 			      void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1535 						int toggle_bits, int error_count, int condition_code, int repeat_number,
1536 						int halted, int skipped, int actual, int non_null))
1537 {
1538 	struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1539 	return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1540 				     ep_number, toggle_bits, callback);
1541 }
1542 
1543 
1544 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
ftdi_elan_edset_input(struct usb_ftdi * ftdi,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,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))1545 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1546 				 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1547 				 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1548 						   int toggle_bits, int error_count, int condition_code, int repeat_number,
1549 						   int halted, int skipped, int actual, int non_null))
1550 {
1551 	u8 ed = ed_number - 1;
1552 wait:if (ftdi->disconnected > 0) {
1553 		return -ENODEV;
1554 	} else if (ftdi->initialized == 0) {
1555 		return -ENODEV;
1556 	} else {
1557 		int command_size;
1558 		mutex_lock(&ftdi->u132_lock);
1559 		command_size = ftdi->command_next - ftdi->command_head;
1560 		if (command_size < COMMAND_SIZE) {
1561 			struct u132_target *target = &ftdi->target[ed];
1562 			struct u132_command *command = &ftdi->command[
1563 				COMMAND_MASK & ftdi->command_next];
1564 			u32 remaining_length = urb->transfer_buffer_length -
1565 				urb->actual_length;
1566 			command->header = 0x82 | (ed << 5);
1567 			if (remaining_length == 0) {
1568 				command->length = 0x0000;
1569 			} else if (remaining_length > 1024) {
1570 				command->length = 0x8000 | 1023;
1571 			} else
1572 				command->length = 0x8000 | (remaining_length -
1573 							    1);
1574 			command->address = (toggle_bits << 6) | (ep_number << 2)
1575 				| (address << 0);
1576 			command->width = usb_maxpacket(urb->dev, urb->pipe,
1577 						       usb_pipeout(urb->pipe));
1578 			command->follows = 0;
1579 			command->value = 0;
1580 			command->buffer = NULL;
1581 			target->callback = callback;
1582 			target->endp = endp;
1583 			target->urb = urb;
1584 			target->active = 1;
1585 			ftdi->command_next += 1;
1586 			ftdi_elan_kick_command_queue(ftdi);
1587 			mutex_unlock(&ftdi->u132_lock);
1588 			return 0;
1589 		} else {
1590 			mutex_unlock(&ftdi->u132_lock);
1591 			msleep(100);
1592 			goto wait;
1593 		}
1594 	}
1595 }
1596 
usb_ftdi_elan_edset_input(struct platform_device * pdev,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,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))1597 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1598 			      void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1599 			      void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1600 						int toggle_bits, int error_count, int condition_code, int repeat_number,
1601 						int halted, int skipped, int actual, int non_null))
1602 {
1603 	struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1604 	return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1605 				     ep_number, toggle_bits, callback);
1606 }
1607 
1608 
1609 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
ftdi_elan_edset_empty(struct usb_ftdi * ftdi,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,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))1610 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1611 				 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1612 				 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1613 						   int toggle_bits, int error_count, int condition_code, int repeat_number,
1614 						   int halted, int skipped, int actual, int non_null))
1615 {
1616 	u8 ed = ed_number - 1;
1617 wait:if (ftdi->disconnected > 0) {
1618 		return -ENODEV;
1619 	} else if (ftdi->initialized == 0) {
1620 		return -ENODEV;
1621 	} else {
1622 		int command_size;
1623 		mutex_lock(&ftdi->u132_lock);
1624 		command_size = ftdi->command_next - ftdi->command_head;
1625 		if (command_size < COMMAND_SIZE) {
1626 			struct u132_target *target = &ftdi->target[ed];
1627 			struct u132_command *command = &ftdi->command[
1628 				COMMAND_MASK & ftdi->command_next];
1629 			command->header = 0x81 | (ed << 5);
1630 			command->length = 0x0000;
1631 			command->address = (toggle_bits << 6) | (ep_number << 2)
1632 				| (address << 0);
1633 			command->width = usb_maxpacket(urb->dev, urb->pipe,
1634 						       usb_pipeout(urb->pipe));
1635 			command->follows = 0;
1636 			command->value = 0;
1637 			command->buffer = NULL;
1638 			target->callback = callback;
1639 			target->endp = endp;
1640 			target->urb = urb;
1641 			target->active = 1;
1642 			ftdi->command_next += 1;
1643 			ftdi_elan_kick_command_queue(ftdi);
1644 			mutex_unlock(&ftdi->u132_lock);
1645 			return 0;
1646 		} else {
1647 			mutex_unlock(&ftdi->u132_lock);
1648 			msleep(100);
1649 			goto wait;
1650 		}
1651 	}
1652 }
1653 
usb_ftdi_elan_edset_empty(struct platform_device * pdev,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,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))1654 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1655 			      void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1656 			      void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1657 						int toggle_bits, int error_count, int condition_code, int repeat_number,
1658 						int halted, int skipped, int actual, int non_null))
1659 {
1660 	struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1661 	return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1662 				     ep_number, toggle_bits, callback);
1663 }
1664 
1665 
1666 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
ftdi_elan_edset_output(struct usb_ftdi * ftdi,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,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))1667 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1668 				  void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1669 				  void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1670 						    int toggle_bits, int error_count, int condition_code, int repeat_number,
1671 						    int halted, int skipped, int actual, int non_null))
1672 {
1673 	u8 ed = ed_number - 1;
1674 wait:if (ftdi->disconnected > 0) {
1675 		return -ENODEV;
1676 	} else if (ftdi->initialized == 0) {
1677 		return -ENODEV;
1678 	} else {
1679 		int command_size;
1680 		mutex_lock(&ftdi->u132_lock);
1681 		command_size = ftdi->command_next - ftdi->command_head;
1682 		if (command_size < COMMAND_SIZE) {
1683 			u8 *b;
1684 			u16 urb_size;
1685 			int i = 0;
1686 			char data[30 *3 + 4];
1687 			char *d = data;
1688 			int m = (sizeof(data) - 1) / 3;
1689 			int l = 0;
1690 			struct u132_target *target = &ftdi->target[ed];
1691 			struct u132_command *command = &ftdi->command[
1692 				COMMAND_MASK & ftdi->command_next];
1693 			command->header = 0x81 | (ed << 5);
1694 			command->address = (toggle_bits << 6) | (ep_number << 2)
1695 				| (address << 0);
1696 			command->width = usb_maxpacket(urb->dev, urb->pipe,
1697 						       usb_pipeout(urb->pipe));
1698 			command->follows = min_t(u32, 1024,
1699 						 urb->transfer_buffer_length -
1700 						 urb->actual_length);
1701 			command->value = 0;
1702 			command->buffer = urb->transfer_buffer +
1703 				urb->actual_length;
1704 			command->length = 0x8000 | (command->follows - 1);
1705 			b = command->buffer;
1706 			urb_size = command->follows;
1707 			data[0] = 0;
1708 			while (urb_size-- > 0) {
1709 				if (i > m) {
1710 				} else if (i++ < m) {
1711 					int w = sprintf(d, " %02X", *b++);
1712 					d += w;
1713 					l += w;
1714 				} else
1715 					d += sprintf(d, " ..");
1716 			}
1717 			target->callback = callback;
1718 			target->endp = endp;
1719 			target->urb = urb;
1720 			target->active = 1;
1721 			ftdi->command_next += 1;
1722 			ftdi_elan_kick_command_queue(ftdi);
1723 			mutex_unlock(&ftdi->u132_lock);
1724 			return 0;
1725 		} else {
1726 			mutex_unlock(&ftdi->u132_lock);
1727 			msleep(100);
1728 			goto wait;
1729 		}
1730 	}
1731 }
1732 
usb_ftdi_elan_edset_output(struct platform_device * pdev,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,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))1733 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1734 			       void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1735 			       void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1736 						 int toggle_bits, int error_count, int condition_code, int repeat_number,
1737 						 int halted, int skipped, int actual, int non_null))
1738 {
1739 	struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1740 	return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1741 				      ep_number, toggle_bits, callback);
1742 }
1743 
1744 
1745 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
ftdi_elan_edset_single(struct usb_ftdi * ftdi,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,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))1746 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1747 				  void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1748 				  void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1749 						    int toggle_bits, int error_count, int condition_code, int repeat_number,
1750 						    int halted, int skipped, int actual, int non_null))
1751 {
1752 	u8 ed = ed_number - 1;
1753 wait:if (ftdi->disconnected > 0) {
1754 		return -ENODEV;
1755 	} else if (ftdi->initialized == 0) {
1756 		return -ENODEV;
1757 	} else {
1758 		int command_size;
1759 		mutex_lock(&ftdi->u132_lock);
1760 		command_size = ftdi->command_next - ftdi->command_head;
1761 		if (command_size < COMMAND_SIZE) {
1762 			u32 remaining_length = urb->transfer_buffer_length -
1763 				urb->actual_length;
1764 			struct u132_target *target = &ftdi->target[ed];
1765 			struct u132_command *command = &ftdi->command[
1766 				COMMAND_MASK & ftdi->command_next];
1767 			command->header = 0x83 | (ed << 5);
1768 			if (remaining_length == 0) {
1769 				command->length = 0x0000;
1770 			} else if (remaining_length > 1024) {
1771 				command->length = 0x8000 | 1023;
1772 			} else
1773 				command->length = 0x8000 | (remaining_length -
1774 							    1);
1775 			command->address = (toggle_bits << 6) | (ep_number << 2)
1776 				| (address << 0);
1777 			command->width = usb_maxpacket(urb->dev, urb->pipe,
1778 						       usb_pipeout(urb->pipe));
1779 			command->follows = 0;
1780 			command->value = 0;
1781 			command->buffer = NULL;
1782 			target->callback = callback;
1783 			target->endp = endp;
1784 			target->urb = urb;
1785 			target->active = 1;
1786 			ftdi->command_next += 1;
1787 			ftdi_elan_kick_command_queue(ftdi);
1788 			mutex_unlock(&ftdi->u132_lock);
1789 			return 0;
1790 		} else {
1791 			mutex_unlock(&ftdi->u132_lock);
1792 			msleep(100);
1793 			goto wait;
1794 		}
1795 	}
1796 }
1797 
usb_ftdi_elan_edset_single(struct platform_device * pdev,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,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))1798 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1799 			       void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1800 			       void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1801 						 int toggle_bits, int error_count, int condition_code, int repeat_number,
1802 						 int halted, int skipped, int actual, int non_null))
1803 {
1804 	struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1805 	return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1806 				      ep_number, toggle_bits, callback);
1807 }
1808 
1809 
1810 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
ftdi_elan_edset_flush(struct usb_ftdi * ftdi,u8 ed_number,void * endp)1811 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1812 				 void *endp)
1813 {
1814 	u8 ed = ed_number - 1;
1815 	if (ftdi->disconnected > 0) {
1816 		return -ENODEV;
1817 	} else if (ftdi->initialized == 0) {
1818 		return -ENODEV;
1819 	} else {
1820 		struct u132_target *target = &ftdi->target[ed];
1821 		mutex_lock(&ftdi->u132_lock);
1822 		if (target->abandoning > 0) {
1823 			mutex_unlock(&ftdi->u132_lock);
1824 			return 0;
1825 		} else {
1826 			target->abandoning = 1;
1827 		wait_1:if (target->active == 1) {
1828 				int command_size = ftdi->command_next -
1829 					ftdi->command_head;
1830 				if (command_size < COMMAND_SIZE) {
1831 					struct u132_command *command =
1832 						&ftdi->command[COMMAND_MASK &
1833 							       ftdi->command_next];
1834 					command->header = 0x80 | (ed << 5) |
1835 						0x4;
1836 					command->length = 0x00;
1837 					command->address = 0x00;
1838 					command->width = 0x00;
1839 					command->follows = 0;
1840 					command->value = 0;
1841 					command->buffer = &command->value;
1842 					ftdi->command_next += 1;
1843 					ftdi_elan_kick_command_queue(ftdi);
1844 				} else {
1845 					mutex_unlock(&ftdi->u132_lock);
1846 					msleep(100);
1847 					mutex_lock(&ftdi->u132_lock);
1848 					goto wait_1;
1849 				}
1850 			}
1851 			mutex_unlock(&ftdi->u132_lock);
1852 			return 0;
1853 		}
1854 	}
1855 }
1856 
usb_ftdi_elan_edset_flush(struct platform_device * pdev,u8 ed_number,void * endp)1857 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1858 			      void *endp)
1859 {
1860 	struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1861 	return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1862 }
1863 
1864 
1865 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
ftdi_elan_flush_input_fifo(struct usb_ftdi * ftdi)1866 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1867 {
1868 	int retry_on_empty = 10;
1869 	int retry_on_timeout = 5;
1870 	int retry_on_status = 20;
1871 more:{
1872 		int packet_bytes = 0;
1873 		int retval = usb_bulk_msg(ftdi->udev,
1874 					  usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1875 					  ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1876 					  &packet_bytes, 100);
1877 		if (packet_bytes > 2) {
1878 			char diag[30 *3 + 4];
1879 			char *d = diag;
1880 			int m = (sizeof(diag) - 1) / 3;
1881 			char *b = ftdi->bulk_in_buffer;
1882 			int bytes_read = 0;
1883 			diag[0] = 0;
1884 			while (packet_bytes-- > 0) {
1885 				char c = *b++;
1886 				if (bytes_read < m) {
1887 					d += sprintf(d, " %02X",
1888 						     0x000000FF & c);
1889 				} else if (bytes_read > m) {
1890 				} else
1891 					d += sprintf(d, " ..");
1892 				bytes_read += 1;
1893 				continue;
1894 			}
1895 			goto more;
1896 		} else if (packet_bytes > 1) {
1897 			char s1 = ftdi->bulk_in_buffer[0];
1898 			char s2 = ftdi->bulk_in_buffer[1];
1899 			if (s1 == 0x31 && s2 == 0x60) {
1900 				return 0;
1901 			} else if (retry_on_status-- > 0) {
1902 				goto more;
1903 			} else {
1904 				dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1905 				return -EFAULT;
1906 			}
1907 		} else if (packet_bytes > 0) {
1908 			char b1 = ftdi->bulk_in_buffer[0];
1909 			dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1910 				b1);
1911 			if (retry_on_status-- > 0) {
1912 				goto more;
1913 			} else {
1914 				dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1915 				return -EFAULT;
1916 			}
1917 		} else if (retval == -ETIMEDOUT) {
1918 			if (retry_on_timeout-- > 0) {
1919 				goto more;
1920 			} else {
1921 				dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1922 				return -ENOMEM;
1923 			}
1924 		} else if (retval == 0) {
1925 			if (retry_on_empty-- > 0) {
1926 				goto more;
1927 			} else {
1928 				dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1929 				return -ENOMEM;
1930 			}
1931 		} else {
1932 			dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1933 			return retval;
1934 		}
1935 	}
1936 	return -1;
1937 }
1938 
1939 
1940 /*
1941  * send the long flush sequence
1942  *
1943  */
ftdi_elan_synchronize_flush(struct usb_ftdi * ftdi)1944 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1945 {
1946 	int retval;
1947 	struct urb *urb;
1948 	char *buf;
1949 	int I = 257;
1950 	int i = 0;
1951 	urb = usb_alloc_urb(0, GFP_KERNEL);
1952 	if (!urb) {
1953 		dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequence\n");
1954 		return -ENOMEM;
1955 	}
1956 	buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1957 	if (!buf) {
1958 		dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1959 		usb_free_urb(urb);
1960 		return -ENOMEM;
1961 	}
1962 	while (I-- > 0)
1963 		buf[i++] = 0x55;
1964 	usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1965 							   ftdi->bulk_out_endpointAddr), buf, i,
1966 			  ftdi_elan_write_bulk_callback, ftdi);
1967 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1968 	retval = usb_submit_urb(urb, GFP_KERNEL);
1969 	if (retval) {
1970 		dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1971 		usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1972 		usb_free_urb(urb);
1973 		return -ENOMEM;
1974 	}
1975 	usb_free_urb(urb);
1976 	return 0;
1977 }
1978 
1979 
1980 /*
1981  * send the reset sequence
1982  *
1983  */
ftdi_elan_synchronize_reset(struct usb_ftdi * ftdi)1984 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1985 {
1986 	int retval;
1987 	struct urb *urb;
1988 	char *buf;
1989 	int I = 4;
1990 	int i = 0;
1991 	urb = usb_alloc_urb(0, GFP_KERNEL);
1992 	if (!urb) {
1993 		dev_err(&ftdi->udev->dev, "could not get a urb for the reset sequence\n");
1994 		return -ENOMEM;
1995 	}
1996 	buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1997 	if (!buf) {
1998 		dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1999 		usb_free_urb(urb);
2000 		return -ENOMEM;
2001 	}
2002 	buf[i++] = 0x55;
2003 	buf[i++] = 0xAA;
2004 	buf[i++] = 0x5A;
2005 	buf[i++] = 0xA5;
2006 	usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2007 							   ftdi->bulk_out_endpointAddr), buf, i,
2008 			  ftdi_elan_write_bulk_callback, ftdi);
2009 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2010 	retval = usb_submit_urb(urb, GFP_KERNEL);
2011 	if (retval) {
2012 		dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
2013 		usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
2014 		usb_free_urb(urb);
2015 		return -ENOMEM;
2016 	}
2017 	usb_free_urb(urb);
2018 	return 0;
2019 }
2020 
ftdi_elan_synchronize(struct usb_ftdi * ftdi)2021 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2022 {
2023 	int retval;
2024 	int long_stop = 10;
2025 	int retry_on_timeout = 5;
2026 	int retry_on_empty = 10;
2027 	int err_count = 0;
2028 	retval = ftdi_elan_flush_input_fifo(ftdi);
2029 	if (retval)
2030 		return retval;
2031 	ftdi->bulk_in_left = 0;
2032 	ftdi->bulk_in_last = -1;
2033 	while (long_stop-- > 0) {
2034 		int read_stop;
2035 		int read_stuck;
2036 		retval = ftdi_elan_synchronize_flush(ftdi);
2037 		if (retval)
2038 			return retval;
2039 		retval = ftdi_elan_flush_input_fifo(ftdi);
2040 		if (retval)
2041 			return retval;
2042 	reset:retval = ftdi_elan_synchronize_reset(ftdi);
2043 		if (retval)
2044 			return retval;
2045 		read_stop = 100;
2046 		read_stuck = 10;
2047 	read:{
2048 			int packet_bytes = 0;
2049 			retval = usb_bulk_msg(ftdi->udev,
2050 					      usb_rcvbulkpipe(ftdi->udev,
2051 							      ftdi->bulk_in_endpointAddr),
2052 					      ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2053 					      &packet_bytes, 500);
2054 			if (packet_bytes > 2) {
2055 				char diag[30 *3 + 4];
2056 				char *d = diag;
2057 				int m = (sizeof(diag) - 1) / 3;
2058 				char *b = ftdi->bulk_in_buffer;
2059 				int bytes_read = 0;
2060 				unsigned char c = 0;
2061 				diag[0] = 0;
2062 				while (packet_bytes-- > 0) {
2063 					c = *b++;
2064 					if (bytes_read < m) {
2065 						d += sprintf(d, " %02X", c);
2066 					} else if (bytes_read > m) {
2067 					} else
2068 						d += sprintf(d, " ..");
2069 					bytes_read += 1;
2070 					continue;
2071 				}
2072 				if (c == 0x7E) {
2073 					return 0;
2074 				} else {
2075 					if (c == 0x55) {
2076 						goto read;
2077 					} else if (read_stop-- > 0) {
2078 						goto read;
2079 					} else {
2080 						dev_err(&ftdi->udev->dev, "retry limit reached\n");
2081 						continue;
2082 					}
2083 				}
2084 			} else if (packet_bytes > 1) {
2085 				unsigned char s1 = ftdi->bulk_in_buffer[0];
2086 				unsigned char s2 = ftdi->bulk_in_buffer[1];
2087 				if (s1 == 0x31 && s2 == 0x00) {
2088 					if (read_stuck-- > 0) {
2089 						goto read;
2090 					} else
2091 						goto reset;
2092 				} else if (s1 == 0x31 && s2 == 0x60) {
2093 					if (read_stop-- > 0) {
2094 						goto read;
2095 					} else {
2096 						dev_err(&ftdi->udev->dev, "retry limit reached\n");
2097 						continue;
2098 					}
2099 				} else {
2100 					if (read_stop-- > 0) {
2101 						goto read;
2102 					} else {
2103 						dev_err(&ftdi->udev->dev, "retry limit reached\n");
2104 						continue;
2105 					}
2106 				}
2107 			} else if (packet_bytes > 0) {
2108 				if (read_stop-- > 0) {
2109 					goto read;
2110 				} else {
2111 					dev_err(&ftdi->udev->dev, "retry limit reached\n");
2112 					continue;
2113 				}
2114 			} else if (retval == -ETIMEDOUT) {
2115 				if (retry_on_timeout-- > 0) {
2116 					goto read;
2117 				} else {
2118 					dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2119 					continue;
2120 				}
2121 			} else if (retval == 0) {
2122 				if (retry_on_empty-- > 0) {
2123 					goto read;
2124 				} else {
2125 					dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2126 					continue;
2127 				}
2128 			} else {
2129 				err_count += 1;
2130 				dev_err(&ftdi->udev->dev, "error = %d\n",
2131 					retval);
2132 				if (read_stop-- > 0) {
2133 					goto read;
2134 				} else {
2135 					dev_err(&ftdi->udev->dev, "retry limit reached\n");
2136 					continue;
2137 				}
2138 			}
2139 		}
2140 	}
2141 	dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2142 	return -EFAULT;
2143 }
2144 
ftdi_elan_stuck_waiting(struct usb_ftdi * ftdi)2145 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2146 {
2147 	int retry_on_empty = 10;
2148 	int retry_on_timeout = 5;
2149 	int retry_on_status = 50;
2150 more:{
2151 		int packet_bytes = 0;
2152 		int retval = usb_bulk_msg(ftdi->udev,
2153 					  usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2154 					  ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2155 					  &packet_bytes, 1000);
2156 		if (packet_bytes > 2) {
2157 			char diag[30 *3 + 4];
2158 			char *d = diag;
2159 			int m = (sizeof(diag) - 1) / 3;
2160 			char *b = ftdi->bulk_in_buffer;
2161 			int bytes_read = 0;
2162 			diag[0] = 0;
2163 			while (packet_bytes-- > 0) {
2164 				char c = *b++;
2165 				if (bytes_read < m) {
2166 					d += sprintf(d, " %02X",
2167 						     0x000000FF & c);
2168 				} else if (bytes_read > m) {
2169 				} else
2170 					d += sprintf(d, " ..");
2171 				bytes_read += 1;
2172 				continue;
2173 			}
2174 			goto more;
2175 		} else if (packet_bytes > 1) {
2176 			char s1 = ftdi->bulk_in_buffer[0];
2177 			char s2 = ftdi->bulk_in_buffer[1];
2178 			if (s1 == 0x31 && s2 == 0x60) {
2179 				return 0;
2180 			} else if (retry_on_status-- > 0) {
2181 				msleep(5);
2182 				goto more;
2183 			} else
2184 				return -EFAULT;
2185 		} else if (packet_bytes > 0) {
2186 			char b1 = ftdi->bulk_in_buffer[0];
2187 			dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2188 			if (retry_on_status-- > 0) {
2189 				msleep(5);
2190 				goto more;
2191 			} else {
2192 				dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2193 				return -EFAULT;
2194 			}
2195 		} else if (retval == -ETIMEDOUT) {
2196 			if (retry_on_timeout-- > 0) {
2197 				goto more;
2198 			} else {
2199 				dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2200 				return -ENOMEM;
2201 			}
2202 		} else if (retval == 0) {
2203 			if (retry_on_empty-- > 0) {
2204 				goto more;
2205 			} else {
2206 				dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2207 				return -ENOMEM;
2208 			}
2209 		} else {
2210 			dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2211 			return -ENOMEM;
2212 		}
2213 	}
2214 	return -1;
2215 }
2216 
ftdi_elan_checkingPCI(struct usb_ftdi * ftdi)2217 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2218 {
2219 	int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2220 	if (UxxxStatus)
2221 		return UxxxStatus;
2222 	if (ftdi->controlreg & 0x00400000) {
2223 		if (ftdi->card_ejected) {
2224 		} else {
2225 			ftdi->card_ejected = 1;
2226 			dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2227 				ftdi->controlreg);
2228 		}
2229 		return -ENODEV;
2230 	} else {
2231 		u8 fn = ftdi->function - 1;
2232 		int activePCIfn = fn << 8;
2233 		u32 pcidata;
2234 		u32 pciVID;
2235 		u32 pciPID;
2236 		int reg = 0;
2237 		UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2238 						   &pcidata);
2239 		if (UxxxStatus)
2240 			return UxxxStatus;
2241 		pciVID = pcidata & 0xFFFF;
2242 		pciPID = (pcidata >> 16) & 0xFFFF;
2243 		if (pciVID == ftdi->platform_data.vendor && pciPID ==
2244 		    ftdi->platform_data.device) {
2245 			return 0;
2246 		} else {
2247 			dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2248 				ftdi->platform_data.vendor, pciVID,
2249 				ftdi->platform_data.device, pciPID);
2250 			return -ENODEV;
2251 		}
2252 	}
2253 }
2254 
2255 
2256 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2257 								   offsetof(struct ohci_regs, member), 0, data);
2258 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2259 								     offsetof(struct ohci_regs, member), 0, data);
2260 
2261 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2262 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD |	\
2263 			OHCI_INTR_WDH)
ftdi_elan_check_controller(struct usb_ftdi * ftdi,int quirk)2264 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2265 {
2266 	int devices = 0;
2267 	int retval;
2268 	u32 hc_control;
2269 	int num_ports;
2270 	u32 control;
2271 	u32 rh_a = -1;
2272 	u32 status;
2273 	u32 fminterval;
2274 	u32 hc_fminterval;
2275 	u32 periodicstart;
2276 	u32 cmdstatus;
2277 	u32 roothub_a;
2278 	int mask = OHCI_INTR_INIT;
2279 	int sleep_time = 0;
2280 	int reset_timeout = 30;        /* ... allow extra time */
2281 	int temp;
2282 	retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2283 	if (retval)
2284 		return retval;
2285 	retval = ftdi_read_pcimem(ftdi, control, &control);
2286 	if (retval)
2287 		return retval;
2288 	retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2289 	if (retval)
2290 		return retval;
2291 	num_ports = rh_a & RH_A_NDP;
2292 	retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2293 	if (retval)
2294 		return retval;
2295 	hc_fminterval &= 0x3fff;
2296 	if (hc_fminterval != FI) {
2297 	}
2298 	hc_fminterval |= FSMP(hc_fminterval) << 16;
2299 	retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2300 	if (retval)
2301 		return retval;
2302 	switch (hc_control & OHCI_CTRL_HCFS) {
2303 	case OHCI_USB_OPER:
2304 		sleep_time = 0;
2305 		break;
2306 	case OHCI_USB_SUSPEND:
2307 	case OHCI_USB_RESUME:
2308 		hc_control &= OHCI_CTRL_RWC;
2309 		hc_control |= OHCI_USB_RESUME;
2310 		sleep_time = 10;
2311 		break;
2312 	default:
2313 		hc_control &= OHCI_CTRL_RWC;
2314 		hc_control |= OHCI_USB_RESET;
2315 		sleep_time = 50;
2316 		break;
2317 	}
2318 	retval = ftdi_write_pcimem(ftdi, control, hc_control);
2319 	if (retval)
2320 		return retval;
2321 	retval = ftdi_read_pcimem(ftdi, control, &control);
2322 	if (retval)
2323 		return retval;
2324 	msleep(sleep_time);
2325 	retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2326 	if (retval)
2327 		return retval;
2328 	if (!(roothub_a & RH_A_NPS)) {        /* power down each port */
2329 		for (temp = 0; temp < num_ports; temp++) {
2330 			retval = ftdi_write_pcimem(ftdi,
2331 						   roothub.portstatus[temp], RH_PS_LSDA);
2332 			if (retval)
2333 				return retval;
2334 		}
2335 	}
2336 	retval = ftdi_read_pcimem(ftdi, control, &control);
2337 	if (retval)
2338 		return retval;
2339 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2340 	if (retval)
2341 		return retval;
2342 	retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2343 	if (retval)
2344 		return retval;
2345 extra:{
2346 		retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2347 		if (retval)
2348 			return retval;
2349 		if (0 != (status & OHCI_HCR)) {
2350 			if (--reset_timeout == 0) {
2351 				dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2352 				return -ENODEV;
2353 			} else {
2354 				msleep(5);
2355 				goto extra;
2356 			}
2357 		}
2358 	}
2359 	if (quirk & OHCI_QUIRK_INITRESET) {
2360 		retval = ftdi_write_pcimem(ftdi, control, hc_control);
2361 		if (retval)
2362 			return retval;
2363 		retval = ftdi_read_pcimem(ftdi, control, &control);
2364 		if (retval)
2365 			return retval;
2366 	}
2367 	retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2368 	if (retval)
2369 		return retval;
2370 	retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2371 	if (retval)
2372 		return retval;
2373 	retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2374 	if (retval)
2375 		return retval;
2376 	retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2377 	if (retval)
2378 		return retval;
2379 	retval = ftdi_write_pcimem(ftdi, fminterval,
2380 				   ((fminterval & FIT) ^ FIT) | hc_fminterval);
2381 	if (retval)
2382 		return retval;
2383 	retval = ftdi_write_pcimem(ftdi, periodicstart,
2384 				   ((9 *hc_fminterval) / 10) & 0x3fff);
2385 	if (retval)
2386 		return retval;
2387 	retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2388 	if (retval)
2389 		return retval;
2390 	retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2391 	if (retval)
2392 		return retval;
2393 	if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2394 		if (!(quirk & OHCI_QUIRK_INITRESET)) {
2395 			quirk |= OHCI_QUIRK_INITRESET;
2396 			goto retry;
2397 		} else
2398 			dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2399 				fminterval, periodicstart);
2400 	}                        /* start controller operations */
2401 	hc_control &= OHCI_CTRL_RWC;
2402 	hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2403 	retval = ftdi_write_pcimem(ftdi, control, hc_control);
2404 	if (retval)
2405 		return retval;
2406 	retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2407 	if (retval)
2408 		return retval;
2409 	retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2410 	if (retval)
2411 		return retval;
2412 	retval = ftdi_read_pcimem(ftdi, control, &control);
2413 	if (retval)
2414 		return retval;
2415 	retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2416 	if (retval)
2417 		return retval;
2418 	retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2419 	if (retval)
2420 		return retval;
2421 	retval = ftdi_write_pcimem(ftdi, intrdisable,
2422 				   OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2423 				   OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2424 				   OHCI_INTR_SO);
2425 	if (retval)
2426 		return retval;        /* handle root hub init quirks ... */
2427 	retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2428 	if (retval)
2429 		return retval;
2430 	roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2431 	if (quirk & OHCI_QUIRK_SUPERIO) {
2432 		roothub_a |= RH_A_NOCP;
2433 		roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2434 		retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2435 		if (retval)
2436 			return retval;
2437 	} else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2438 		roothub_a |= RH_A_NPS;
2439 		retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2440 		if (retval)
2441 			return retval;
2442 	}
2443 	retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2444 	if (retval)
2445 		return retval;
2446 	retval = ftdi_write_pcimem(ftdi, roothub.b,
2447 				   (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2448 	if (retval)
2449 		return retval;
2450 	retval = ftdi_read_pcimem(ftdi, control, &control);
2451 	if (retval)
2452 		return retval;
2453 	mdelay((roothub_a >> 23) & 0x1fe);
2454 	for (temp = 0; temp < num_ports; temp++) {
2455 		u32 portstatus;
2456 		retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2457 					  &portstatus);
2458 		if (retval)
2459 			return retval;
2460 		if (1 & portstatus)
2461 			devices += 1;
2462 	}
2463 	return devices;
2464 }
2465 
ftdi_elan_setup_controller(struct usb_ftdi * ftdi,int fn)2466 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2467 {
2468 	u32 latence_timer;
2469 	int UxxxStatus;
2470 	u32 pcidata;
2471 	int reg = 0;
2472 	int activePCIfn = fn << 8;
2473 	UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2474 	if (UxxxStatus)
2475 		return UxxxStatus;
2476 	reg = 16;
2477 	UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2478 					    0xFFFFFFFF);
2479 	if (UxxxStatus)
2480 		return UxxxStatus;
2481 	UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2482 					   &pcidata);
2483 	if (UxxxStatus)
2484 		return UxxxStatus;
2485 	UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2486 					    0xF0000000);
2487 	if (UxxxStatus)
2488 		return UxxxStatus;
2489 	UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2490 					   &pcidata);
2491 	if (UxxxStatus)
2492 		return UxxxStatus;
2493 	reg = 12;
2494 	UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2495 					   &latence_timer);
2496 	if (UxxxStatus)
2497 		return UxxxStatus;
2498 	latence_timer &= 0xFFFF00FF;
2499 	latence_timer |= 0x00001600;
2500 	UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2501 					    latence_timer);
2502 	if (UxxxStatus)
2503 		return UxxxStatus;
2504 	UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2505 					   &pcidata);
2506 	if (UxxxStatus)
2507 		return UxxxStatus;
2508 	reg = 4;
2509 	UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2510 					    0x06);
2511 	if (UxxxStatus)
2512 		return UxxxStatus;
2513 	UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2514 					   &pcidata);
2515 	if (UxxxStatus)
2516 		return UxxxStatus;
2517 	for (reg = 0; reg <= 0x54; reg += 4) {
2518 		UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2519 		if (UxxxStatus)
2520 			return UxxxStatus;
2521 	}
2522 	return 0;
2523 }
2524 
ftdi_elan_close_controller(struct usb_ftdi * ftdi,int fn)2525 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2526 {
2527 	u32 latence_timer;
2528 	int UxxxStatus;
2529 	u32 pcidata;
2530 	int reg = 0;
2531 	int activePCIfn = fn << 8;
2532 	UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2533 	if (UxxxStatus)
2534 		return UxxxStatus;
2535 	reg = 16;
2536 	UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2537 					    0xFFFFFFFF);
2538 	if (UxxxStatus)
2539 		return UxxxStatus;
2540 	UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2541 					   &pcidata);
2542 	if (UxxxStatus)
2543 		return UxxxStatus;
2544 	UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2545 					    0x00000000);
2546 	if (UxxxStatus)
2547 		return UxxxStatus;
2548 	UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2549 					   &pcidata);
2550 	if (UxxxStatus)
2551 		return UxxxStatus;
2552 	reg = 12;
2553 	UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2554 					   &latence_timer);
2555 	if (UxxxStatus)
2556 		return UxxxStatus;
2557 	latence_timer &= 0xFFFF00FF;
2558 	latence_timer |= 0x00001600;
2559 	UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2560 					    latence_timer);
2561 	if (UxxxStatus)
2562 		return UxxxStatus;
2563 	UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2564 					   &pcidata);
2565 	if (UxxxStatus)
2566 		return UxxxStatus;
2567 	reg = 4;
2568 	UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2569 					    0x00);
2570 	if (UxxxStatus)
2571 		return UxxxStatus;
2572 	return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2573 }
2574 
ftdi_elan_found_controller(struct usb_ftdi * ftdi,int fn,int quirk)2575 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2576 {
2577 	int result;
2578 	int UxxxStatus;
2579 	UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2580 	if (UxxxStatus)
2581 		return UxxxStatus;
2582 	result = ftdi_elan_check_controller(ftdi, quirk);
2583 	UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2584 	if (UxxxStatus)
2585 		return UxxxStatus;
2586 	return result;
2587 }
2588 
ftdi_elan_enumeratePCI(struct usb_ftdi * ftdi)2589 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2590 {
2591 	u32 controlreg;
2592 	u8 sensebits;
2593 	int UxxxStatus;
2594 	UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2595 	if (UxxxStatus)
2596 		return UxxxStatus;
2597 	UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2598 	if (UxxxStatus)
2599 		return UxxxStatus;
2600 	msleep(750);
2601 	UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2602 	if (UxxxStatus)
2603 		return UxxxStatus;
2604 	UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2605 	if (UxxxStatus)
2606 		return UxxxStatus;
2607 	UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2608 	if (UxxxStatus)
2609 		return UxxxStatus;
2610 	UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2611 	if (UxxxStatus)
2612 		return UxxxStatus;
2613 	UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2614 	if (UxxxStatus)
2615 		return UxxxStatus;
2616 	msleep(250);
2617 	UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2618 	if (UxxxStatus)
2619 		return UxxxStatus;
2620 	UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2621 	if (UxxxStatus)
2622 		return UxxxStatus;
2623 	UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2624 	if (UxxxStatus)
2625 		return UxxxStatus;
2626 	UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2627 	if (UxxxStatus)
2628 		return UxxxStatus;
2629 	UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2630 	if (UxxxStatus)
2631 		return UxxxStatus;
2632 	msleep(1000);
2633 	sensebits = (controlreg >> 16) & 0x000F;
2634 	if (0x0D == sensebits)
2635 		return 0;
2636 	else
2637 		return - ENXIO;
2638 }
2639 
ftdi_elan_setupOHCI(struct usb_ftdi * ftdi)2640 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2641 {
2642 	int UxxxStatus;
2643 	u32 pcidata;
2644 	int reg = 0;
2645 	u8 fn;
2646 	int activePCIfn = 0;
2647 	int max_devices = 0;
2648 	int controllers = 0;
2649 	int unrecognized = 0;
2650 	ftdi->function = 0;
2651 	for (fn = 0; (fn < 4); fn++) {
2652 		u32 pciVID = 0;
2653 		u32 pciPID = 0;
2654 		int devices = 0;
2655 		activePCIfn = fn << 8;
2656 		UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2657 						   &pcidata);
2658 		if (UxxxStatus)
2659 			return UxxxStatus;
2660 		pciVID = pcidata & 0xFFFF;
2661 		pciPID = (pcidata >> 16) & 0xFFFF;
2662 		if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2663 			devices = ftdi_elan_found_controller(ftdi, fn, 0);
2664 			controllers += 1;
2665 		} else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2666 		{
2667 			devices = ftdi_elan_found_controller(ftdi, fn, 0);
2668 			controllers += 1;
2669 		} else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2670 			devices = ftdi_elan_found_controller(ftdi, fn, 0);
2671 			controllers += 1;
2672 		} else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2673 		{
2674 			devices = ftdi_elan_found_controller(ftdi, fn, 0);
2675 			controllers += 1;
2676 		} else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2677 			devices = ftdi_elan_found_controller(ftdi, fn,
2678 							     OHCI_QUIRK_AMD756);
2679 			controllers += 1;
2680 		} else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2681 			devices = ftdi_elan_found_controller(ftdi, fn,
2682 							     OHCI_QUIRK_ZFMICRO);
2683 			controllers += 1;
2684 		} else if (0 == pcidata) {
2685 		} else
2686 			unrecognized += 1;
2687 		if (devices > max_devices) {
2688 			max_devices = devices;
2689 			ftdi->function = fn + 1;
2690 			ftdi->platform_data.vendor = pciVID;
2691 			ftdi->platform_data.device = pciPID;
2692 		}
2693 	}
2694 	if (ftdi->function > 0) {
2695 		return ftdi_elan_setup_controller(ftdi,	ftdi->function - 1);
2696 	} else if (controllers > 0) {
2697 		return -ENXIO;
2698 	} else if (unrecognized > 0) {
2699 		return -ENXIO;
2700 	} else {
2701 		ftdi->enumerated = 0;
2702 		return -ENXIO;
2703 	}
2704 }
2705 
2706 
2707 /*
2708  * we use only the first bulk-in and bulk-out endpoints
2709  */
ftdi_elan_probe(struct usb_interface * interface,const struct usb_device_id * id)2710 static int ftdi_elan_probe(struct usb_interface *interface,
2711 			   const struct usb_device_id *id)
2712 {
2713 	struct usb_host_interface *iface_desc;
2714 	struct usb_endpoint_descriptor *endpoint;
2715 	size_t buffer_size;
2716 	int i;
2717 	int retval = -ENOMEM;
2718 	struct usb_ftdi *ftdi;
2719 
2720 	ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2721 	if (!ftdi)
2722 		return -ENOMEM;
2723 
2724 	mutex_lock(&ftdi_module_lock);
2725 	list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2726 	ftdi->sequence_num = ++ftdi_instances;
2727 	mutex_unlock(&ftdi_module_lock);
2728 	ftdi_elan_init_kref(ftdi);
2729 	sema_init(&ftdi->sw_lock, 1);
2730 	ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2731 	ftdi->interface = interface;
2732 	mutex_init(&ftdi->u132_lock);
2733 	ftdi->expected = 4;
2734 	iface_desc = interface->cur_altsetting;
2735 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2736 		endpoint = &iface_desc->endpoint[i].desc;
2737 		if (!ftdi->bulk_in_endpointAddr &&
2738 		    usb_endpoint_is_bulk_in(endpoint)) {
2739 			buffer_size = usb_endpoint_maxp(endpoint);
2740 			ftdi->bulk_in_size = buffer_size;
2741 			ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2742 			ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2743 			if (!ftdi->bulk_in_buffer) {
2744 				dev_err(&ftdi->udev->dev, "Could not allocate bulk_in_buffer\n");
2745 				retval = -ENOMEM;
2746 				goto error;
2747 			}
2748 		}
2749 		if (!ftdi->bulk_out_endpointAddr &&
2750 		    usb_endpoint_is_bulk_out(endpoint)) {
2751 			ftdi->bulk_out_endpointAddr =
2752 				endpoint->bEndpointAddress;
2753 		}
2754 	}
2755 	if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2756 		dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2757 		retval = -ENODEV;
2758 		goto error;
2759 	}
2760 	dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2761 		 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2762 		 ftdi->bulk_out_endpointAddr);
2763 	usb_set_intfdata(interface, ftdi);
2764 	if (iface_desc->desc.bInterfaceNumber == 0 &&
2765 	    ftdi->bulk_in_endpointAddr == 0x81 &&
2766 	    ftdi->bulk_out_endpointAddr == 0x02) {
2767 		retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2768 		if (retval) {
2769 			dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2770 			usb_set_intfdata(interface, NULL);
2771 			retval = -ENOMEM;
2772 			goto error;
2773 		} else {
2774 			ftdi->class = &ftdi_elan_jtag_class;
2775 			dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2776 				 ftdi, iface_desc->desc.bInterfaceNumber,
2777 				 interface->minor);
2778 			return 0;
2779 		}
2780 	} else if (iface_desc->desc.bInterfaceNumber == 1 &&
2781 		   ftdi->bulk_in_endpointAddr == 0x83 &&
2782 		   ftdi->bulk_out_endpointAddr == 0x04) {
2783 		ftdi->class = NULL;
2784 		dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2785 			 ftdi, iface_desc->desc.bInterfaceNumber);
2786 		INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2787 		INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2788 		INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2789 		ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2790 		return 0;
2791 	} else {
2792 		dev_err(&ftdi->udev->dev,
2793 			"Could not find ELAN's U132 device\n");
2794 		retval = -ENODEV;
2795 		goto error;
2796 	}
2797 error:if (ftdi) {
2798 		ftdi_elan_put_kref(ftdi);
2799 	}
2800 	return retval;
2801 }
2802 
ftdi_elan_disconnect(struct usb_interface * interface)2803 static void ftdi_elan_disconnect(struct usb_interface *interface)
2804 {
2805 	struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2806 	ftdi->disconnected += 1;
2807 	if (ftdi->class) {
2808 		int minor = interface->minor;
2809 		struct usb_class_driver *class = ftdi->class;
2810 		usb_set_intfdata(interface, NULL);
2811 		usb_deregister_dev(interface, class);
2812 		dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2813 			 minor);
2814 	} else {
2815 		ftdi_status_cancel_work(ftdi);
2816 		ftdi_command_cancel_work(ftdi);
2817 		ftdi_response_cancel_work(ftdi);
2818 		ftdi_elan_abandon_completions(ftdi);
2819 		ftdi_elan_abandon_targets(ftdi);
2820 		if (ftdi->registered) {
2821 			platform_device_unregister(&ftdi->platform_dev);
2822 			ftdi->synchronized = 0;
2823 			ftdi->enumerated = 0;
2824 			ftdi->initialized = 0;
2825 			ftdi->registered = 0;
2826 		}
2827 		flush_workqueue(status_queue);
2828 		flush_workqueue(command_queue);
2829 		flush_workqueue(respond_queue);
2830 		ftdi->disconnected += 1;
2831 		usb_set_intfdata(interface, NULL);
2832 		dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2833 	}
2834 	ftdi_elan_put_kref(ftdi);
2835 }
2836 
2837 static struct usb_driver ftdi_elan_driver = {
2838 	.name = "ftdi-elan",
2839 	.probe = ftdi_elan_probe,
2840 	.disconnect = ftdi_elan_disconnect,
2841 	.id_table = ftdi_elan_table,
2842 };
ftdi_elan_init(void)2843 static int __init ftdi_elan_init(void)
2844 {
2845 	int result;
2846 	pr_info("driver %s\n", ftdi_elan_driver.name);
2847 	mutex_init(&ftdi_module_lock);
2848 	INIT_LIST_HEAD(&ftdi_static_list);
2849 	status_queue = create_singlethread_workqueue("ftdi-status-control");
2850 	if (!status_queue)
2851 		goto err_status_queue;
2852 	command_queue = create_singlethread_workqueue("ftdi-command-engine");
2853 	if (!command_queue)
2854 		goto err_command_queue;
2855 	respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2856 	if (!respond_queue)
2857 		goto err_respond_queue;
2858 	result = usb_register(&ftdi_elan_driver);
2859 	if (result) {
2860 		destroy_workqueue(status_queue);
2861 		destroy_workqueue(command_queue);
2862 		destroy_workqueue(respond_queue);
2863 		pr_err("usb_register failed. Error number %d\n", result);
2864 	}
2865 	return result;
2866 
2867 err_respond_queue:
2868 	destroy_workqueue(command_queue);
2869 err_command_queue:
2870 	destroy_workqueue(status_queue);
2871 err_status_queue:
2872 	pr_err("%s couldn't create workqueue\n", ftdi_elan_driver.name);
2873 	return -ENOMEM;
2874 }
2875 
ftdi_elan_exit(void)2876 static void __exit ftdi_elan_exit(void)
2877 {
2878 	struct usb_ftdi *ftdi;
2879 	struct usb_ftdi *temp;
2880 	usb_deregister(&ftdi_elan_driver);
2881 	pr_info("ftdi_u132 driver deregistered\n");
2882 	list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2883 		ftdi_status_cancel_work(ftdi);
2884 		ftdi_command_cancel_work(ftdi);
2885 		ftdi_response_cancel_work(ftdi);
2886 	} flush_workqueue(status_queue);
2887 	destroy_workqueue(status_queue);
2888 	status_queue = NULL;
2889 	flush_workqueue(command_queue);
2890 	destroy_workqueue(command_queue);
2891 	command_queue = NULL;
2892 	flush_workqueue(respond_queue);
2893 	destroy_workqueue(respond_queue);
2894 	respond_queue = NULL;
2895 }
2896 
2897 
2898 module_init(ftdi_elan_init);
2899 module_exit(ftdi_elan_exit);
2900