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