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