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