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