GNU Linux-libre 4.14.290-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 <linux/uaccess.h>
52 #include <linux/usb.h>
53 #include <linux/workqueue.h>
54 #include <linux/platform_device.h>
55 MODULE_AUTHOR("Tony Olech");
56 MODULE_DESCRIPTION("FTDI ELAN driver");
57 MODULE_LICENSE("GPL");
58 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
59 static bool distrust_firmware = 1;
60 module_param(distrust_firmware, bool, 0);
61 MODULE_PARM_DESC(distrust_firmware,
62                  "true to distrust firmware power/overcurrent setup");
63 extern struct platform_driver u132_platform_driver;
64 /*
65  * ftdi_module_lock exists to protect access to global variables
66  *
67  */
68 static struct mutex ftdi_module_lock;
69 static int ftdi_instances = 0;
70 static struct list_head ftdi_static_list;
71 /*
72  * end of the global variables protected by ftdi_module_lock
73  */
74 #include "usb_u132.h"
75 #include <asm/io.h>
76 #include <linux/usb/hcd.h>
77
78 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
79  * If you're going to try stuff like this, you need to split
80  * out shareable stuff (register declarations?) into its own
81  * file, maybe name <linux/usb/ohci.h>
82  */
83
84 #include "../host/ohci.h"
85 /* Define these values to match your devices*/
86 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
87 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
88 /* table of devices that work with this driver*/
89 static const struct usb_device_id ftdi_elan_table[] = {
90         {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
91         { /* Terminating entry */ }
92 };
93
94 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
95 /* only the jtag(firmware upgrade device) interface requires
96  * a device file and corresponding minor number, but the
97  * interface is created unconditionally - I suppose it could
98  * be configured or not according to a module parameter.
99  * But since we(now) require one interface per device,
100  * and since it unlikely that a normal installation would
101  * require more than a couple of elan-ftdi devices, 8 seems
102  * like a reasonable limit to have here, and if someone
103  * really requires more than 8 devices, then they can frig the
104  * code and recompile
105  */
106 #define USB_FTDI_ELAN_MINOR_BASE 192
107 #define COMMAND_BITS 5
108 #define COMMAND_SIZE (1<<COMMAND_BITS)
109 #define COMMAND_MASK (COMMAND_SIZE-1)
110 struct u132_command {
111         u8 header;
112         u16 length;
113         u8 address;
114         u8 width;
115         u32 value;
116         int follows;
117         void *buffer;
118 };
119 #define RESPOND_BITS 5
120 #define RESPOND_SIZE (1<<RESPOND_BITS)
121 #define RESPOND_MASK (RESPOND_SIZE-1)
122 struct u132_respond {
123         u8 header;
124         u8 address;
125         u32 *value;
126         int *result;
127         struct completion wait_completion;
128 };
129 struct u132_target {
130         void *endp;
131         struct urb *urb;
132         int toggle_bits;
133         int error_count;
134         int condition_code;
135         int repeat_number;
136         int halted;
137         int skipped;
138         int actual;
139         int non_null;
140         int active;
141         int abandoning;
142         void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
143                          int toggle_bits, int error_count, int condition_code,
144                          int repeat_number, int halted, int skipped, int actual,
145                          int non_null);
146 };
147 /* Structure to hold all of our device specific stuff*/
148 struct usb_ftdi {
149         struct list_head ftdi_list;
150         struct mutex u132_lock;
151         int command_next;
152         int command_head;
153         struct u132_command command[COMMAND_SIZE];
154         int respond_next;
155         int respond_head;
156         struct u132_respond respond[RESPOND_SIZE];
157         struct u132_target target[4];
158         char device_name[16];
159         unsigned synchronized:1;
160         unsigned enumerated:1;
161         unsigned registered:1;
162         unsigned initialized:1;
163         unsigned card_ejected:1;
164         int function;
165         int sequence_num;
166         int disconnected;
167         int gone_away;
168         int stuck_status;
169         int status_queue_delay;
170         struct semaphore sw_lock;
171         struct usb_device *udev;
172         struct usb_interface *interface;
173         struct usb_class_driver *class;
174         struct delayed_work status_work;
175         struct delayed_work command_work;
176         struct delayed_work respond_work;
177         struct u132_platform_data platform_data;
178         struct resource resources[0];
179         struct platform_device platform_dev;
180         unsigned char *bulk_in_buffer;
181         size_t bulk_in_size;
182         size_t bulk_in_last;
183         size_t bulk_in_left;
184         __u8 bulk_in_endpointAddr;
185         __u8 bulk_out_endpointAddr;
186         struct kref kref;
187         u32 controlreg;
188         u8 response[4 + 1024];
189         int expected;
190         int received;
191         int ed_found;
192 };
193 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
194 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
195                                                     platform_dev)
196 static struct usb_driver ftdi_elan_driver;
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         if (ftdi->platform_dev.dev.parent)
310                 return -EBUSY;
311
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
329         return platform_device_register(&ftdi->platform_dev);
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 || ed_type == 0x03) {
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 {
878                 target->abandoning = 0;
879                 mutex_unlock(&ftdi->u132_lock);
880                 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
881                                       payload);
882                 ftdi->received = 0;
883                 ftdi->expected = 4;
884                 ftdi->ed_found = 0;
885                 return ftdi->response;
886         }
887 }
888
889 static char *have_ed_get_response(struct usb_ftdi *ftdi,
890                                   struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
891                                   char *b)
892 {
893         mutex_lock(&ftdi->u132_lock);
894         target->condition_code = TD_DEVNOTRESP;
895         target->actual = (ed_length >> 0) & 0x01FF;
896         target->non_null = (ed_length >> 15) & 0x0001;
897         target->repeat_number = (ed_length >> 11) & 0x000F;
898         mutex_unlock(&ftdi->u132_lock);
899         if (target->active)
900                 ftdi_elan_do_callback(ftdi, target, NULL, 0);
901         target->abandoning = 0;
902         ftdi->received = 0;
903         ftdi->expected = 4;
904         ftdi->ed_found = 0;
905         return ftdi->response;
906 }
907
908
909 /*
910  * The engine tries to empty the FTDI fifo
911  *
912  * all responses found in the fifo data are dispatched thus
913  * the response buffer can only ever hold a maximum sized
914  * response from the Uxxx.
915  *
916  */
917 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
918 {
919         u8 *b = ftdi->response + ftdi->received;
920         int bytes_read = 0;
921         int retry_on_empty = 1;
922         int retry_on_timeout = 3;
923         int empty_packets = 0;
924 read:{
925                 int packet_bytes = 0;
926                 int retval = usb_bulk_msg(ftdi->udev,
927                                           usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
928                                           ftdi->bulk_in_buffer, ftdi->bulk_in_size,
929                                           &packet_bytes, 500);
930                 char diag[30 *3 + 4];
931                 char *d = diag;
932                 int m = packet_bytes;
933                 u8 *c = ftdi->bulk_in_buffer;
934                 int s = (sizeof(diag) - 1) / 3;
935                 diag[0] = 0;
936                 while (s-- > 0 && m-- > 0) {
937                         if (s > 0 || m == 0) {
938                                 d += sprintf(d, " %02X", *c++);
939                         } else
940                                 d += sprintf(d, " ..");
941                 }
942                 if (packet_bytes > 2) {
943                         ftdi->bulk_in_left = packet_bytes - 2;
944                         ftdi->bulk_in_last = 1;
945                         goto have;
946                 } else if (retval == -ETIMEDOUT) {
947                         if (retry_on_timeout-- > 0) {
948                                 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
949                                         packet_bytes, bytes_read, diag);
950                                 goto more;
951                         } else if (bytes_read > 0) {
952                                 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
953                                         bytes_read, diag);
954                                 return -ENOMEM;
955                         } else {
956                                 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
957                                         packet_bytes, bytes_read, diag);
958                                 return -ENOMEM;
959                         }
960                 } else if (retval == -EILSEQ) {
961                         dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
962                                 retval, packet_bytes, bytes_read, diag);
963                         return retval;
964                 } else if (retval) {
965                         dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
966                                 retval, packet_bytes, bytes_read, diag);
967                         return retval;
968                 } else if (packet_bytes == 2) {
969                         unsigned char s0 = ftdi->bulk_in_buffer[0];
970                         unsigned char s1 = ftdi->bulk_in_buffer[1];
971                         empty_packets += 1;
972                         if (s0 == 0x31 && s1 == 0x60) {
973                                 if (retry_on_empty-- > 0) {
974                                         goto more;
975                                 } else
976                                         return 0;
977                         } else if (s0 == 0x31 && s1 == 0x00) {
978                                 if (retry_on_empty-- > 0) {
979                                         goto more;
980                                 } else
981                                         return 0;
982                         } else {
983                                 if (retry_on_empty-- > 0) {
984                                         goto more;
985                                 } else
986                                         return 0;
987                         }
988                 } else if (packet_bytes == 1) {
989                         if (retry_on_empty-- > 0) {
990                                 goto more;
991                         } else
992                                 return 0;
993                 } else {
994                         if (retry_on_empty-- > 0) {
995                                 goto more;
996                         } else
997                                 return 0;
998                 }
999         }
1000 more:{
1001                 goto read;
1002         }
1003 have:if (ftdi->bulk_in_left > 0) {
1004                 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1005                 bytes_read += 1;
1006                 ftdi->bulk_in_left -= 1;
1007                 if (ftdi->received == 0 && c == 0xFF) {
1008                         goto have;
1009                 } else
1010                         *b++ = c;
1011                 if (++ftdi->received < ftdi->expected) {
1012                         goto have;
1013                 } else if (ftdi->ed_found) {
1014                         int ed_number = (ftdi->response[0] >> 5) & 0x03;
1015                         u16 ed_length = (ftdi->response[2] << 8) |
1016                                 ftdi->response[1];
1017                         struct u132_target *target = &ftdi->target[ed_number];
1018                         int payload = (ed_length >> 0) & 0x07FF;
1019                         char diag[30 *3 + 4];
1020                         char *d = diag;
1021                         int m = payload;
1022                         u8 *c = 4 + ftdi->response;
1023                         int s = (sizeof(diag) - 1) / 3;
1024                         diag[0] = 0;
1025                         while (s-- > 0 && m-- > 0) {
1026                                 if (s > 0 || m == 0) {
1027                                         d += sprintf(d, " %02X", *c++);
1028                                 } else
1029                                         d += sprintf(d, " ..");
1030                         }
1031                         ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1032                                               payload);
1033                         ftdi->received = 0;
1034                         ftdi->expected = 4;
1035                         ftdi->ed_found = 0;
1036                         b = ftdi->response;
1037                         goto have;
1038                 } else if (ftdi->expected == 8) {
1039                         u8 buscmd;
1040                         int respond_head = ftdi->respond_head++;
1041                         struct u132_respond *respond = &ftdi->respond[
1042                                 RESPOND_MASK & respond_head];
1043                         u32 data = ftdi->response[7];
1044                         data <<= 8;
1045                         data |= ftdi->response[6];
1046                         data <<= 8;
1047                         data |= ftdi->response[5];
1048                         data <<= 8;
1049                         data |= ftdi->response[4];
1050                         *respond->value = data;
1051                         *respond->result = 0;
1052                         complete(&respond->wait_completion);
1053                         ftdi->received = 0;
1054                         ftdi->expected = 4;
1055                         ftdi->ed_found = 0;
1056                         b = ftdi->response;
1057                         buscmd = (ftdi->response[0] >> 0) & 0x0F;
1058                         if (buscmd == 0x00) {
1059                         } else if (buscmd == 0x02) {
1060                         } else if (buscmd == 0x06) {
1061                         } else if (buscmd == 0x0A) {
1062                         } else
1063                                 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1064                                         buscmd, data);
1065                         goto have;
1066                 } else {
1067                         if ((ftdi->response[0] & 0x80) == 0x00) {
1068                                 ftdi->expected = 8;
1069                                 goto have;
1070                         } else {
1071                                 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1072                                 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1073                                 u16 ed_length = (ftdi->response[2] << 8) |
1074                                         ftdi->response[1];
1075                                 struct u132_target *target = &ftdi->target[
1076                                         ed_number];
1077                                 target->halted = (ftdi->response[0] >> 3) &
1078                                         0x01;
1079                                 target->skipped = (ftdi->response[0] >> 2) &
1080                                         0x01;
1081                                 target->toggle_bits = (ftdi->response[3] >> 6)
1082                                         & 0x03;
1083                                 target->error_count = (ftdi->response[3] >> 4)
1084                                         & 0x03;
1085                                 target->condition_code = (ftdi->response[
1086                                                                   3] >> 0) & 0x0F;
1087                                 if ((ftdi->response[0] & 0x10) == 0x00) {
1088                                         b = have_ed_set_response(ftdi, target,
1089                                                                  ed_length, ed_number, ed_type,
1090                                                                  b);
1091                                         goto have;
1092                                 } else {
1093                                         b = have_ed_get_response(ftdi, target,
1094                                                                  ed_length, ed_number, ed_type,
1095                                                                  b);
1096                                         goto have;
1097                                 }
1098                         }
1099                 }
1100         } else
1101                 goto more;
1102 }
1103
1104
1105 /*
1106  * create a urb, and a buffer for it, and copy the data to the urb
1107  *
1108  */
1109 static ssize_t ftdi_elan_write(struct file *file,
1110                                const char __user *user_buffer, size_t count,
1111                                loff_t *ppos)
1112 {
1113         int retval = 0;
1114         struct urb *urb;
1115         char *buf;
1116         struct usb_ftdi *ftdi = file->private_data;
1117
1118         if (ftdi->disconnected > 0) {
1119                 return -ENODEV;
1120         }
1121         if (count == 0) {
1122                 goto exit;
1123         }
1124         urb = usb_alloc_urb(0, GFP_KERNEL);
1125         if (!urb) {
1126                 retval = -ENOMEM;
1127                 goto error_1;
1128         }
1129         buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1130                                  &urb->transfer_dma);
1131         if (!buf) {
1132                 retval = -ENOMEM;
1133                 goto error_2;
1134         }
1135         if (copy_from_user(buf, user_buffer, count)) {
1136                 retval = -EFAULT;
1137                 goto error_3;
1138         }
1139         usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1140                                                            ftdi->bulk_out_endpointAddr), buf, count,
1141                           ftdi_elan_write_bulk_callback, ftdi);
1142         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1143         retval = usb_submit_urb(urb, GFP_KERNEL);
1144         if (retval) {
1145                 dev_err(&ftdi->udev->dev,
1146                         "failed submitting write urb, error %d\n", retval);
1147                 goto error_3;
1148         }
1149         usb_free_urb(urb);
1150
1151 exit:
1152         return count;
1153 error_3:
1154         usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1155 error_2:
1156         usb_free_urb(urb);
1157 error_1:
1158         return retval;
1159 }
1160
1161 static const struct file_operations ftdi_elan_fops = {
1162         .owner = THIS_MODULE,
1163         .llseek = no_llseek,
1164         .read = ftdi_elan_read,
1165         .write = ftdi_elan_write,
1166         .open = ftdi_elan_open,
1167         .release = ftdi_elan_release,
1168 };
1169
1170 /*
1171  * usb class driver info in order to get a minor number from the usb core,
1172  * and to have the device registered with the driver core
1173  */
1174 static struct usb_class_driver ftdi_elan_jtag_class = {
1175         .name = "ftdi-%d-jtag",
1176         .fops = &ftdi_elan_fops,
1177         .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1178 };
1179
1180 /*
1181  * the following definitions are for the
1182  * ELAN FPGA state machgine processor that
1183  * lies on the other side of the FTDI chip
1184  */
1185 #define cPCIu132rd 0x0
1186 #define cPCIu132wr 0x1
1187 #define cPCIiord 0x2
1188 #define cPCIiowr 0x3
1189 #define cPCImemrd 0x6
1190 #define cPCImemwr 0x7
1191 #define cPCIcfgrd 0xA
1192 #define cPCIcfgwr 0xB
1193 #define cPCInull 0xF
1194 #define cU132cmd_status 0x0
1195 #define cU132flash 0x1
1196 #define cPIDsetup 0x0
1197 #define cPIDout 0x1
1198 #define cPIDin 0x2
1199 #define cPIDinonce 0x3
1200 #define cCCnoerror 0x0
1201 #define cCCcrc 0x1
1202 #define cCCbitstuff 0x2
1203 #define cCCtoggle 0x3
1204 #define cCCstall 0x4
1205 #define cCCnoresp 0x5
1206 #define cCCbadpid1 0x6
1207 #define cCCbadpid2 0x7
1208 #define cCCdataoverrun 0x8
1209 #define cCCdataunderrun 0x9
1210 #define cCCbuffoverrun 0xC
1211 #define cCCbuffunderrun 0xD
1212 #define cCCnotaccessed 0xF
1213 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1214 {
1215 wait:if (ftdi->disconnected > 0) {
1216                 return -ENODEV;
1217         } else {
1218                 int command_size;
1219                 mutex_lock(&ftdi->u132_lock);
1220                 command_size = ftdi->command_next - ftdi->command_head;
1221                 if (command_size < COMMAND_SIZE) {
1222                         struct u132_command *command = &ftdi->command[
1223                                 COMMAND_MASK & ftdi->command_next];
1224                         command->header = 0x00 | cPCIu132wr;
1225                         command->length = 0x04;
1226                         command->address = 0x00;
1227                         command->width = 0x00;
1228                         command->follows = 4;
1229                         command->value = data;
1230                         command->buffer = &command->value;
1231                         ftdi->command_next += 1;
1232                         ftdi_elan_kick_command_queue(ftdi);
1233                         mutex_unlock(&ftdi->u132_lock);
1234                         return 0;
1235                 } else {
1236                         mutex_unlock(&ftdi->u132_lock);
1237                         msleep(100);
1238                         goto wait;
1239                 }
1240         }
1241 }
1242
1243 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1244                                   u8 width, u32 data)
1245 {
1246         u8 addressofs = config_offset / 4;
1247 wait:if (ftdi->disconnected > 0) {
1248                 return -ENODEV;
1249         } else {
1250                 int command_size;
1251                 mutex_lock(&ftdi->u132_lock);
1252                 command_size = ftdi->command_next - ftdi->command_head;
1253                 if (command_size < COMMAND_SIZE) {
1254                         struct u132_command *command = &ftdi->command[
1255                                 COMMAND_MASK & ftdi->command_next];
1256                         command->header = 0x00 | (cPCIcfgwr & 0x0F);
1257                         command->length = 0x04;
1258                         command->address = addressofs;
1259                         command->width = 0x00 | (width & 0x0F);
1260                         command->follows = 4;
1261                         command->value = data;
1262                         command->buffer = &command->value;
1263                         ftdi->command_next += 1;
1264                         ftdi_elan_kick_command_queue(ftdi);
1265                         mutex_unlock(&ftdi->u132_lock);
1266                         return 0;
1267                 } else {
1268                         mutex_unlock(&ftdi->u132_lock);
1269                         msleep(100);
1270                         goto wait;
1271                 }
1272         }
1273 }
1274
1275 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1276                                   u8 width, u32 data)
1277 {
1278         u8 addressofs = mem_offset / 4;
1279 wait:if (ftdi->disconnected > 0) {
1280                 return -ENODEV;
1281         } else {
1282                 int command_size;
1283                 mutex_lock(&ftdi->u132_lock);
1284                 command_size = ftdi->command_next - ftdi->command_head;
1285                 if (command_size < COMMAND_SIZE) {
1286                         struct u132_command *command = &ftdi->command[
1287                                 COMMAND_MASK & ftdi->command_next];
1288                         command->header = 0x00 | (cPCImemwr & 0x0F);
1289                         command->length = 0x04;
1290                         command->address = addressofs;
1291                         command->width = 0x00 | (width & 0x0F);
1292                         command->follows = 4;
1293                         command->value = data;
1294                         command->buffer = &command->value;
1295                         ftdi->command_next += 1;
1296                         ftdi_elan_kick_command_queue(ftdi);
1297                         mutex_unlock(&ftdi->u132_lock);
1298                         return 0;
1299                 } else {
1300                         mutex_unlock(&ftdi->u132_lock);
1301                         msleep(100);
1302                         goto wait;
1303                 }
1304         }
1305 }
1306
1307 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1308                                u8 width, u32 data)
1309 {
1310         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1311         return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1312 }
1313
1314
1315 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1316 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1317 {
1318 wait:if (ftdi->disconnected > 0) {
1319                 return -ENODEV;
1320         } else {
1321                 int command_size;
1322                 int respond_size;
1323                 mutex_lock(&ftdi->u132_lock);
1324                 command_size = ftdi->command_next - ftdi->command_head;
1325                 respond_size = ftdi->respond_next - ftdi->respond_head;
1326                 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1327                 {
1328                         struct u132_command *command = &ftdi->command[
1329                                 COMMAND_MASK & ftdi->command_next];
1330                         struct u132_respond *respond = &ftdi->respond[
1331                                 RESPOND_MASK & ftdi->respond_next];
1332                         int result = -ENODEV;
1333                         respond->result = &result;
1334                         respond->header = command->header = 0x00 | cPCIu132rd;
1335                         command->length = 0x04;
1336                         respond->address = command->address = cU132cmd_status;
1337                         command->width = 0x00;
1338                         command->follows = 0;
1339                         command->value = 0;
1340                         command->buffer = NULL;
1341                         respond->value = data;
1342                         init_completion(&respond->wait_completion);
1343                         ftdi->command_next += 1;
1344                         ftdi->respond_next += 1;
1345                         ftdi_elan_kick_command_queue(ftdi);
1346                         mutex_unlock(&ftdi->u132_lock);
1347                         wait_for_completion(&respond->wait_completion);
1348                         return result;
1349                 } else {
1350                         mutex_unlock(&ftdi->u132_lock);
1351                         msleep(100);
1352                         goto wait;
1353                 }
1354         }
1355 }
1356
1357 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1358                                  u8 width, u32 *data)
1359 {
1360         u8 addressofs = config_offset / 4;
1361 wait:if (ftdi->disconnected > 0) {
1362                 return -ENODEV;
1363         } else {
1364                 int command_size;
1365                 int respond_size;
1366                 mutex_lock(&ftdi->u132_lock);
1367                 command_size = ftdi->command_next - ftdi->command_head;
1368                 respond_size = ftdi->respond_next - ftdi->respond_head;
1369                 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1370                 {
1371                         struct u132_command *command = &ftdi->command[
1372                                 COMMAND_MASK & ftdi->command_next];
1373                         struct u132_respond *respond = &ftdi->respond[
1374                                 RESPOND_MASK & ftdi->respond_next];
1375                         int result = -ENODEV;
1376                         respond->result = &result;
1377                         respond->header = command->header = 0x00 | (cPCIcfgrd &
1378                                                                     0x0F);
1379                         command->length = 0x04;
1380                         respond->address = command->address = addressofs;
1381                         command->width = 0x00 | (width & 0x0F);
1382                         command->follows = 0;
1383                         command->value = 0;
1384                         command->buffer = NULL;
1385                         respond->value = data;
1386                         init_completion(&respond->wait_completion);
1387                         ftdi->command_next += 1;
1388                         ftdi->respond_next += 1;
1389                         ftdi_elan_kick_command_queue(ftdi);
1390                         mutex_unlock(&ftdi->u132_lock);
1391                         wait_for_completion(&respond->wait_completion);
1392                         return result;
1393                 } else {
1394                         mutex_unlock(&ftdi->u132_lock);
1395                         msleep(100);
1396                         goto wait;
1397                 }
1398         }
1399 }
1400
1401 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1402                                  u8 width, u32 *data)
1403 {
1404         u8 addressofs = mem_offset / 4;
1405 wait:if (ftdi->disconnected > 0) {
1406                 return -ENODEV;
1407         } else {
1408                 int command_size;
1409                 int respond_size;
1410                 mutex_lock(&ftdi->u132_lock);
1411                 command_size = ftdi->command_next - ftdi->command_head;
1412                 respond_size = ftdi->respond_next - ftdi->respond_head;
1413                 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1414                 {
1415                         struct u132_command *command = &ftdi->command[
1416                                 COMMAND_MASK & ftdi->command_next];
1417                         struct u132_respond *respond = &ftdi->respond[
1418                                 RESPOND_MASK & ftdi->respond_next];
1419                         int result = -ENODEV;
1420                         respond->result = &result;
1421                         respond->header = command->header = 0x00 | (cPCImemrd &
1422                                                                     0x0F);
1423                         command->length = 0x04;
1424                         respond->address = command->address = addressofs;
1425                         command->width = 0x00 | (width & 0x0F);
1426                         command->follows = 0;
1427                         command->value = 0;
1428                         command->buffer = NULL;
1429                         respond->value = data;
1430                         init_completion(&respond->wait_completion);
1431                         ftdi->command_next += 1;
1432                         ftdi->respond_next += 1;
1433                         ftdi_elan_kick_command_queue(ftdi);
1434                         mutex_unlock(&ftdi->u132_lock);
1435                         wait_for_completion(&respond->wait_completion);
1436                         return result;
1437                 } else {
1438                         mutex_unlock(&ftdi->u132_lock);
1439                         msleep(100);
1440                         goto wait;
1441                 }
1442         }
1443 }
1444
1445 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1446                               u8 width, u32 *data)
1447 {
1448         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1449         if (ftdi->initialized == 0) {
1450                 return -ENODEV;
1451         } else
1452                 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1453 }
1454
1455
1456 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1457 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1458                                  void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1459                                  void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1460                                                    int toggle_bits, int error_count, int condition_code, int repeat_number,
1461                                                    int halted, int skipped, int actual, int non_null))
1462 {
1463         u8 ed = ed_number - 1;
1464 wait:if (ftdi->disconnected > 0) {
1465                 return -ENODEV;
1466         } else if (ftdi->initialized == 0) {
1467                 return -ENODEV;
1468         } else {
1469                 int command_size;
1470                 mutex_lock(&ftdi->u132_lock);
1471                 command_size = ftdi->command_next - ftdi->command_head;
1472                 if (command_size < COMMAND_SIZE) {
1473                         struct u132_target *target = &ftdi->target[ed];
1474                         struct u132_command *command = &ftdi->command[
1475                                 COMMAND_MASK & ftdi->command_next];
1476                         command->header = 0x80 | (ed << 5);
1477                         command->length = 0x8007;
1478                         command->address = (toggle_bits << 6) | (ep_number << 2)
1479                                 | (address << 0);
1480                         command->width = usb_maxpacket(urb->dev, urb->pipe,
1481                                                        usb_pipeout(urb->pipe));
1482                         command->follows = 8;
1483                         command->value = 0;
1484                         command->buffer = urb->setup_packet;
1485                         target->callback = callback;
1486                         target->endp = endp;
1487                         target->urb = urb;
1488                         target->active = 1;
1489                         ftdi->command_next += 1;
1490                         ftdi_elan_kick_command_queue(ftdi);
1491                         mutex_unlock(&ftdi->u132_lock);
1492                         return 0;
1493                 } else {
1494                         mutex_unlock(&ftdi->u132_lock);
1495                         msleep(100);
1496                         goto wait;
1497                 }
1498         }
1499 }
1500
1501 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1502                               void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1503                               void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1504                                                 int toggle_bits, int error_count, int condition_code, int repeat_number,
1505                                                 int halted, int skipped, int actual, int non_null))
1506 {
1507         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1508         return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1509                                      ep_number, toggle_bits, callback);
1510 }
1511
1512
1513 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1514 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1515                                  void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1516                                  void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1517                                                    int toggle_bits, int error_count, int condition_code, int repeat_number,
1518                                                    int halted, int skipped, int actual, int non_null))
1519 {
1520         u8 ed = ed_number - 1;
1521 wait:if (ftdi->disconnected > 0) {
1522                 return -ENODEV;
1523         } else if (ftdi->initialized == 0) {
1524                 return -ENODEV;
1525         } else {
1526                 int command_size;
1527                 mutex_lock(&ftdi->u132_lock);
1528                 command_size = ftdi->command_next - ftdi->command_head;
1529                 if (command_size < COMMAND_SIZE) {
1530                         struct u132_target *target = &ftdi->target[ed];
1531                         struct u132_command *command = &ftdi->command[
1532                                 COMMAND_MASK & ftdi->command_next];
1533                         u32 remaining_length = urb->transfer_buffer_length -
1534                                 urb->actual_length;
1535                         command->header = 0x82 | (ed << 5);
1536                         if (remaining_length == 0) {
1537                                 command->length = 0x0000;
1538                         } else if (remaining_length > 1024) {
1539                                 command->length = 0x8000 | 1023;
1540                         } else
1541                                 command->length = 0x8000 | (remaining_length -
1542                                                             1);
1543                         command->address = (toggle_bits << 6) | (ep_number << 2)
1544                                 | (address << 0);
1545                         command->width = usb_maxpacket(urb->dev, urb->pipe,
1546                                                        usb_pipeout(urb->pipe));
1547                         command->follows = 0;
1548                         command->value = 0;
1549                         command->buffer = NULL;
1550                         target->callback = callback;
1551                         target->endp = endp;
1552                         target->urb = urb;
1553                         target->active = 1;
1554                         ftdi->command_next += 1;
1555                         ftdi_elan_kick_command_queue(ftdi);
1556                         mutex_unlock(&ftdi->u132_lock);
1557                         return 0;
1558                 } else {
1559                         mutex_unlock(&ftdi->u132_lock);
1560                         msleep(100);
1561                         goto wait;
1562                 }
1563         }
1564 }
1565
1566 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1567                               void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1568                               void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1569                                                 int toggle_bits, int error_count, int condition_code, int repeat_number,
1570                                                 int halted, int skipped, int actual, int non_null))
1571 {
1572         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1573         return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1574                                      ep_number, toggle_bits, callback);
1575 }
1576
1577
1578 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1579 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1580                                  void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1581                                  void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1582                                                    int toggle_bits, int error_count, int condition_code, int repeat_number,
1583                                                    int halted, int skipped, int actual, int non_null))
1584 {
1585         u8 ed = ed_number - 1;
1586 wait:if (ftdi->disconnected > 0) {
1587                 return -ENODEV;
1588         } else if (ftdi->initialized == 0) {
1589                 return -ENODEV;
1590         } else {
1591                 int command_size;
1592                 mutex_lock(&ftdi->u132_lock);
1593                 command_size = ftdi->command_next - ftdi->command_head;
1594                 if (command_size < COMMAND_SIZE) {
1595                         struct u132_target *target = &ftdi->target[ed];
1596                         struct u132_command *command = &ftdi->command[
1597                                 COMMAND_MASK & ftdi->command_next];
1598                         command->header = 0x81 | (ed << 5);
1599                         command->length = 0x0000;
1600                         command->address = (toggle_bits << 6) | (ep_number << 2)
1601                                 | (address << 0);
1602                         command->width = usb_maxpacket(urb->dev, urb->pipe,
1603                                                        usb_pipeout(urb->pipe));
1604                         command->follows = 0;
1605                         command->value = 0;
1606                         command->buffer = NULL;
1607                         target->callback = callback;
1608                         target->endp = endp;
1609                         target->urb = urb;
1610                         target->active = 1;
1611                         ftdi->command_next += 1;
1612                         ftdi_elan_kick_command_queue(ftdi);
1613                         mutex_unlock(&ftdi->u132_lock);
1614                         return 0;
1615                 } else {
1616                         mutex_unlock(&ftdi->u132_lock);
1617                         msleep(100);
1618                         goto wait;
1619                 }
1620         }
1621 }
1622
1623 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1624                               void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1625                               void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1626                                                 int toggle_bits, int error_count, int condition_code, int repeat_number,
1627                                                 int halted, int skipped, int actual, int non_null))
1628 {
1629         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1630         return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1631                                      ep_number, toggle_bits, callback);
1632 }
1633
1634
1635 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1636 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1637                                   void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1638                                   void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1639                                                     int toggle_bits, int error_count, int condition_code, int repeat_number,
1640                                                     int halted, int skipped, int actual, int non_null))
1641 {
1642         u8 ed = ed_number - 1;
1643 wait:if (ftdi->disconnected > 0) {
1644                 return -ENODEV;
1645         } else if (ftdi->initialized == 0) {
1646                 return -ENODEV;
1647         } else {
1648                 int command_size;
1649                 mutex_lock(&ftdi->u132_lock);
1650                 command_size = ftdi->command_next - ftdi->command_head;
1651                 if (command_size < COMMAND_SIZE) {
1652                         u8 *b;
1653                         u16 urb_size;
1654                         int i = 0;
1655                         char data[30 *3 + 4];
1656                         char *d = data;
1657                         int m = (sizeof(data) - 1) / 3 - 1;
1658                         int l = 0;
1659                         struct u132_target *target = &ftdi->target[ed];
1660                         struct u132_command *command = &ftdi->command[
1661                                 COMMAND_MASK & ftdi->command_next];
1662                         command->header = 0x81 | (ed << 5);
1663                         command->address = (toggle_bits << 6) | (ep_number << 2)
1664                                 | (address << 0);
1665                         command->width = usb_maxpacket(urb->dev, urb->pipe,
1666                                                        usb_pipeout(urb->pipe));
1667                         command->follows = min_t(u32, 1024,
1668                                                  urb->transfer_buffer_length -
1669                                                  urb->actual_length);
1670                         command->value = 0;
1671                         command->buffer = urb->transfer_buffer +
1672                                 urb->actual_length;
1673                         command->length = 0x8000 | (command->follows - 1);
1674                         b = command->buffer;
1675                         urb_size = command->follows;
1676                         data[0] = 0;
1677                         while (urb_size-- > 0) {
1678                                 if (i > m) {
1679                                 } else if (i++ < m) {
1680                                         int w = sprintf(d, " %02X", *b++);
1681                                         d += w;
1682                                         l += w;
1683                                 } else
1684                                         d += sprintf(d, " ..");
1685                         }
1686                         target->callback = callback;
1687                         target->endp = endp;
1688                         target->urb = urb;
1689                         target->active = 1;
1690                         ftdi->command_next += 1;
1691                         ftdi_elan_kick_command_queue(ftdi);
1692                         mutex_unlock(&ftdi->u132_lock);
1693                         return 0;
1694                 } else {
1695                         mutex_unlock(&ftdi->u132_lock);
1696                         msleep(100);
1697                         goto wait;
1698                 }
1699         }
1700 }
1701
1702 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1703                                void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1704                                void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1705                                                  int toggle_bits, int error_count, int condition_code, int repeat_number,
1706                                                  int halted, int skipped, int actual, int non_null))
1707 {
1708         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1709         return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1710                                       ep_number, toggle_bits, callback);
1711 }
1712
1713
1714 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1715 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1716                                   void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1717                                   void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1718                                                     int toggle_bits, int error_count, int condition_code, int repeat_number,
1719                                                     int halted, int skipped, int actual, int non_null))
1720 {
1721         u8 ed = ed_number - 1;
1722 wait:if (ftdi->disconnected > 0) {
1723                 return -ENODEV;
1724         } else if (ftdi->initialized == 0) {
1725                 return -ENODEV;
1726         } else {
1727                 int command_size;
1728                 mutex_lock(&ftdi->u132_lock);
1729                 command_size = ftdi->command_next - ftdi->command_head;
1730                 if (command_size < COMMAND_SIZE) {
1731                         u32 remaining_length = urb->transfer_buffer_length -
1732                                 urb->actual_length;
1733                         struct u132_target *target = &ftdi->target[ed];
1734                         struct u132_command *command = &ftdi->command[
1735                                 COMMAND_MASK & ftdi->command_next];
1736                         command->header = 0x83 | (ed << 5);
1737                         if (remaining_length == 0) {
1738                                 command->length = 0x0000;
1739                         } else if (remaining_length > 1024) {
1740                                 command->length = 0x8000 | 1023;
1741                         } else
1742                                 command->length = 0x8000 | (remaining_length -
1743                                                             1);
1744                         command->address = (toggle_bits << 6) | (ep_number << 2)
1745                                 | (address << 0);
1746                         command->width = usb_maxpacket(urb->dev, urb->pipe,
1747                                                        usb_pipeout(urb->pipe));
1748                         command->follows = 0;
1749                         command->value = 0;
1750                         command->buffer = NULL;
1751                         target->callback = callback;
1752                         target->endp = endp;
1753                         target->urb = urb;
1754                         target->active = 1;
1755                         ftdi->command_next += 1;
1756                         ftdi_elan_kick_command_queue(ftdi);
1757                         mutex_unlock(&ftdi->u132_lock);
1758                         return 0;
1759                 } else {
1760                         mutex_unlock(&ftdi->u132_lock);
1761                         msleep(100);
1762                         goto wait;
1763                 }
1764         }
1765 }
1766
1767 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1768                                void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1769                                void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1770                                                  int toggle_bits, int error_count, int condition_code, int repeat_number,
1771                                                  int halted, int skipped, int actual, int non_null))
1772 {
1773         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1774         return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1775                                       ep_number, toggle_bits, callback);
1776 }
1777
1778
1779 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1780 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1781                                  void *endp)
1782 {
1783         u8 ed = ed_number - 1;
1784         if (ftdi->disconnected > 0) {
1785                 return -ENODEV;
1786         } else if (ftdi->initialized == 0) {
1787                 return -ENODEV;
1788         } else {
1789                 struct u132_target *target = &ftdi->target[ed];
1790                 mutex_lock(&ftdi->u132_lock);
1791                 if (target->abandoning > 0) {
1792                         mutex_unlock(&ftdi->u132_lock);
1793                         return 0;
1794                 } else {
1795                         target->abandoning = 1;
1796                 wait_1:if (target->active == 1) {
1797                                 int command_size = ftdi->command_next -
1798                                         ftdi->command_head;
1799                                 if (command_size < COMMAND_SIZE) {
1800                                         struct u132_command *command =
1801                                                 &ftdi->command[COMMAND_MASK &
1802                                                                ftdi->command_next];
1803                                         command->header = 0x80 | (ed << 5) |
1804                                                 0x4;
1805                                         command->length = 0x00;
1806                                         command->address = 0x00;
1807                                         command->width = 0x00;
1808                                         command->follows = 0;
1809                                         command->value = 0;
1810                                         command->buffer = &command->value;
1811                                         ftdi->command_next += 1;
1812                                         ftdi_elan_kick_command_queue(ftdi);
1813                                 } else {
1814                                         mutex_unlock(&ftdi->u132_lock);
1815                                         msleep(100);
1816                                         mutex_lock(&ftdi->u132_lock);
1817                                         goto wait_1;
1818                                 }
1819                         }
1820                         mutex_unlock(&ftdi->u132_lock);
1821                         return 0;
1822                 }
1823         }
1824 }
1825
1826 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1827                               void *endp)
1828 {
1829         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1830         return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1831 }
1832
1833
1834 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1835 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1836 {
1837         int retry_on_empty = 10;
1838         int retry_on_timeout = 5;
1839         int retry_on_status = 20;
1840 more:{
1841                 int packet_bytes = 0;
1842                 int retval = usb_bulk_msg(ftdi->udev,
1843                                           usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1844                                           ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1845                                           &packet_bytes, 100);
1846                 if (packet_bytes > 2) {
1847                         char diag[30 *3 + 4];
1848                         char *d = diag;
1849                         int m = (sizeof(diag) - 1) / 3 - 1;
1850                         char *b = ftdi->bulk_in_buffer;
1851                         int bytes_read = 0;
1852                         diag[0] = 0;
1853                         while (packet_bytes-- > 0) {
1854                                 char c = *b++;
1855                                 if (bytes_read < m) {
1856                                         d += sprintf(d, " %02X",
1857                                                      0x000000FF & c);
1858                                 } else if (bytes_read > m) {
1859                                 } else
1860                                         d += sprintf(d, " ..");
1861                                 bytes_read += 1;
1862                                 continue;
1863                         }
1864                         goto more;
1865                 } else if (packet_bytes > 1) {
1866                         char s1 = ftdi->bulk_in_buffer[0];
1867                         char s2 = ftdi->bulk_in_buffer[1];
1868                         if (s1 == 0x31 && s2 == 0x60) {
1869                                 return 0;
1870                         } else if (retry_on_status-- > 0) {
1871                                 goto more;
1872                         } else {
1873                                 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1874                                 return -EFAULT;
1875                         }
1876                 } else if (packet_bytes > 0) {
1877                         char b1 = ftdi->bulk_in_buffer[0];
1878                         dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1879                                 b1);
1880                         if (retry_on_status-- > 0) {
1881                                 goto more;
1882                         } else {
1883                                 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1884                                 return -EFAULT;
1885                         }
1886                 } else if (retval == -ETIMEDOUT) {
1887                         if (retry_on_timeout-- > 0) {
1888                                 goto more;
1889                         } else {
1890                                 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1891                                 return -ENOMEM;
1892                         }
1893                 } else if (retval == 0) {
1894                         if (retry_on_empty-- > 0) {
1895                                 goto more;
1896                         } else {
1897                                 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1898                                 return -ENOMEM;
1899                         }
1900                 } else {
1901                         dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1902                         return retval;
1903                 }
1904         }
1905         return -1;
1906 }
1907
1908
1909 /*
1910  * send the long flush sequence
1911  *
1912  */
1913 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1914 {
1915         int retval;
1916         struct urb *urb;
1917         char *buf;
1918         int I = 257;
1919         int i = 0;
1920         urb = usb_alloc_urb(0, GFP_KERNEL);
1921         if (!urb)
1922                 return -ENOMEM;
1923         buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1924         if (!buf) {
1925                 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1926                 usb_free_urb(urb);
1927                 return -ENOMEM;
1928         }
1929         while (I-- > 0)
1930                 buf[i++] = 0x55;
1931         usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1932                                                            ftdi->bulk_out_endpointAddr), buf, i,
1933                           ftdi_elan_write_bulk_callback, ftdi);
1934         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1935         retval = usb_submit_urb(urb, GFP_KERNEL);
1936         if (retval) {
1937                 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1938                 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1939                 usb_free_urb(urb);
1940                 return -ENOMEM;
1941         }
1942         usb_free_urb(urb);
1943         return 0;
1944 }
1945
1946
1947 /*
1948  * send the reset sequence
1949  *
1950  */
1951 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1952 {
1953         int retval;
1954         struct urb *urb;
1955         char *buf;
1956         int I = 4;
1957         int i = 0;
1958         urb = usb_alloc_urb(0, GFP_KERNEL);
1959         if (!urb)
1960                 return -ENOMEM;
1961         buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1962         if (!buf) {
1963                 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1964                 usb_free_urb(urb);
1965                 return -ENOMEM;
1966         }
1967         buf[i++] = 0x55;
1968         buf[i++] = 0xAA;
1969         buf[i++] = 0x5A;
1970         buf[i++] = 0xA5;
1971         usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1972                                                            ftdi->bulk_out_endpointAddr), buf, i,
1973                           ftdi_elan_write_bulk_callback, ftdi);
1974         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1975         retval = usb_submit_urb(urb, GFP_KERNEL);
1976         if (retval) {
1977                 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
1978                 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1979                 usb_free_urb(urb);
1980                 return -ENOMEM;
1981         }
1982         usb_free_urb(urb);
1983         return 0;
1984 }
1985
1986 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
1987 {
1988         int retval;
1989         int long_stop = 10;
1990         int retry_on_timeout = 5;
1991         int retry_on_empty = 10;
1992         int err_count = 0;
1993         retval = ftdi_elan_flush_input_fifo(ftdi);
1994         if (retval)
1995                 return retval;
1996         ftdi->bulk_in_left = 0;
1997         ftdi->bulk_in_last = -1;
1998         while (long_stop-- > 0) {
1999                 int read_stop;
2000                 int read_stuck;
2001                 retval = ftdi_elan_synchronize_flush(ftdi);
2002                 if (retval)
2003                         return retval;
2004                 retval = ftdi_elan_flush_input_fifo(ftdi);
2005                 if (retval)
2006                         return retval;
2007         reset:retval = ftdi_elan_synchronize_reset(ftdi);
2008                 if (retval)
2009                         return retval;
2010                 read_stop = 100;
2011                 read_stuck = 10;
2012         read:{
2013                         int packet_bytes = 0;
2014                         retval = usb_bulk_msg(ftdi->udev,
2015                                               usb_rcvbulkpipe(ftdi->udev,
2016                                                               ftdi->bulk_in_endpointAddr),
2017                                               ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2018                                               &packet_bytes, 500);
2019                         if (packet_bytes > 2) {
2020                                 char diag[30 *3 + 4];
2021                                 char *d = diag;
2022                                 int m = (sizeof(diag) - 1) / 3 - 1;
2023                                 char *b = ftdi->bulk_in_buffer;
2024                                 int bytes_read = 0;
2025                                 unsigned char c = 0;
2026                                 diag[0] = 0;
2027                                 while (packet_bytes-- > 0) {
2028                                         c = *b++;
2029                                         if (bytes_read < m) {
2030                                                 d += sprintf(d, " %02X", c);
2031                                         } else if (bytes_read > m) {
2032                                         } else
2033                                                 d += sprintf(d, " ..");
2034                                         bytes_read += 1;
2035                                         continue;
2036                                 }
2037                                 if (c == 0x7E) {
2038                                         return 0;
2039                                 } else {
2040                                         if (c == 0x55) {
2041                                                 goto read;
2042                                         } else if (read_stop-- > 0) {
2043                                                 goto read;
2044                                         } else {
2045                                                 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2046                                                 continue;
2047                                         }
2048                                 }
2049                         } else if (packet_bytes > 1) {
2050                                 unsigned char s1 = ftdi->bulk_in_buffer[0];
2051                                 unsigned char s2 = ftdi->bulk_in_buffer[1];
2052                                 if (s1 == 0x31 && s2 == 0x00) {
2053                                         if (read_stuck-- > 0) {
2054                                                 goto read;
2055                                         } else
2056                                                 goto reset;
2057                                 } else if (s1 == 0x31 && s2 == 0x60) {
2058                                         if (read_stop-- > 0) {
2059                                                 goto read;
2060                                         } else {
2061                                                 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2062                                                 continue;
2063                                         }
2064                                 } else {
2065                                         if (read_stop-- > 0) {
2066                                                 goto read;
2067                                         } else {
2068                                                 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2069                                                 continue;
2070                                         }
2071                                 }
2072                         } else if (packet_bytes > 0) {
2073                                 if (read_stop-- > 0) {
2074                                         goto read;
2075                                 } else {
2076                                         dev_err(&ftdi->udev->dev, "retry limit reached\n");
2077                                         continue;
2078                                 }
2079                         } else if (retval == -ETIMEDOUT) {
2080                                 if (retry_on_timeout-- > 0) {
2081                                         goto read;
2082                                 } else {
2083                                         dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2084                                         continue;
2085                                 }
2086                         } else if (retval == 0) {
2087                                 if (retry_on_empty-- > 0) {
2088                                         goto read;
2089                                 } else {
2090                                         dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2091                                         continue;
2092                                 }
2093                         } else {
2094                                 err_count += 1;
2095                                 dev_err(&ftdi->udev->dev, "error = %d\n",
2096                                         retval);
2097                                 if (read_stop-- > 0) {
2098                                         goto read;
2099                                 } else {
2100                                         dev_err(&ftdi->udev->dev, "retry limit reached\n");
2101                                         continue;
2102                                 }
2103                         }
2104                 }
2105         }
2106         dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2107         return -EFAULT;
2108 }
2109
2110 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2111 {
2112         int retry_on_empty = 10;
2113         int retry_on_timeout = 5;
2114         int retry_on_status = 50;
2115 more:{
2116                 int packet_bytes = 0;
2117                 int retval = usb_bulk_msg(ftdi->udev,
2118                                           usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2119                                           ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2120                                           &packet_bytes, 1000);
2121                 if (packet_bytes > 2) {
2122                         char diag[30 *3 + 4];
2123                         char *d = diag;
2124                         int m = (sizeof(diag) - 1) / 3 - 1;
2125                         char *b = ftdi->bulk_in_buffer;
2126                         int bytes_read = 0;
2127                         diag[0] = 0;
2128                         while (packet_bytes-- > 0) {
2129                                 char c = *b++;
2130                                 if (bytes_read < m) {
2131                                         d += sprintf(d, " %02X",
2132                                                      0x000000FF & c);
2133                                 } else if (bytes_read > m) {
2134                                 } else
2135                                         d += sprintf(d, " ..");
2136                                 bytes_read += 1;
2137                                 continue;
2138                         }
2139                         goto more;
2140                 } else if (packet_bytes > 1) {
2141                         char s1 = ftdi->bulk_in_buffer[0];
2142                         char s2 = ftdi->bulk_in_buffer[1];
2143                         if (s1 == 0x31 && s2 == 0x60) {
2144                                 return 0;
2145                         } else if (retry_on_status-- > 0) {
2146                                 msleep(5);
2147                                 goto more;
2148                         } else
2149                                 return -EFAULT;
2150                 } else if (packet_bytes > 0) {
2151                         char b1 = ftdi->bulk_in_buffer[0];
2152                         dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2153                         if (retry_on_status-- > 0) {
2154                                 msleep(5);
2155                                 goto more;
2156                         } else {
2157                                 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2158                                 return -EFAULT;
2159                         }
2160                 } else if (retval == -ETIMEDOUT) {
2161                         if (retry_on_timeout-- > 0) {
2162                                 goto more;
2163                         } else {
2164                                 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2165                                 return -ENOMEM;
2166                         }
2167                 } else if (retval == 0) {
2168                         if (retry_on_empty-- > 0) {
2169                                 goto more;
2170                         } else {
2171                                 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2172                                 return -ENOMEM;
2173                         }
2174                 } else {
2175                         dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2176                         return -ENOMEM;
2177                 }
2178         }
2179         return -1;
2180 }
2181
2182 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2183 {
2184         int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2185         if (UxxxStatus)
2186                 return UxxxStatus;
2187         if (ftdi->controlreg & 0x00400000) {
2188                 if (ftdi->card_ejected) {
2189                 } else {
2190                         ftdi->card_ejected = 1;
2191                         dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2192                                 ftdi->controlreg);
2193                 }
2194                 return -ENODEV;
2195         } else {
2196                 u8 fn = ftdi->function - 1;
2197                 int activePCIfn = fn << 8;
2198                 u32 pcidata;
2199                 u32 pciVID;
2200                 u32 pciPID;
2201                 int reg = 0;
2202                 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2203                                                    &pcidata);
2204                 if (UxxxStatus)
2205                         return UxxxStatus;
2206                 pciVID = pcidata & 0xFFFF;
2207                 pciPID = (pcidata >> 16) & 0xFFFF;
2208                 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2209                     ftdi->platform_data.device) {
2210                         return 0;
2211                 } else {
2212                         dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2213                                 ftdi->platform_data.vendor, pciVID,
2214                                 ftdi->platform_data.device, pciPID);
2215                         return -ENODEV;
2216                 }
2217         }
2218 }
2219
2220
2221 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2222                                                                    offsetof(struct ohci_regs, member), 0, data);
2223 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2224                                                                      offsetof(struct ohci_regs, member), 0, data);
2225
2226 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2227 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD |   \
2228                         OHCI_INTR_WDH)
2229 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2230 {
2231         int devices = 0;
2232         int retval;
2233         u32 hc_control;
2234         int num_ports;
2235         u32 control;
2236         u32 rh_a = -1;
2237         u32 status;
2238         u32 fminterval;
2239         u32 hc_fminterval;
2240         u32 periodicstart;
2241         u32 cmdstatus;
2242         u32 roothub_a;
2243         int mask = OHCI_INTR_INIT;
2244         int sleep_time = 0;
2245         int reset_timeout = 30;        /* ... allow extra time */
2246         int temp;
2247         retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2248         if (retval)
2249                 return retval;
2250         retval = ftdi_read_pcimem(ftdi, control, &control);
2251         if (retval)
2252                 return retval;
2253         retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2254         if (retval)
2255                 return retval;
2256         num_ports = rh_a & RH_A_NDP;
2257         retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2258         if (retval)
2259                 return retval;
2260         hc_fminterval &= 0x3fff;
2261         if (hc_fminterval != FI) {
2262         }
2263         hc_fminterval |= FSMP(hc_fminterval) << 16;
2264         retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2265         if (retval)
2266                 return retval;
2267         switch (hc_control & OHCI_CTRL_HCFS) {
2268         case OHCI_USB_OPER:
2269                 sleep_time = 0;
2270                 break;
2271         case OHCI_USB_SUSPEND:
2272         case OHCI_USB_RESUME:
2273                 hc_control &= OHCI_CTRL_RWC;
2274                 hc_control |= OHCI_USB_RESUME;
2275                 sleep_time = 10;
2276                 break;
2277         default:
2278                 hc_control &= OHCI_CTRL_RWC;
2279                 hc_control |= OHCI_USB_RESET;
2280                 sleep_time = 50;
2281                 break;
2282         }
2283         retval = ftdi_write_pcimem(ftdi, control, hc_control);
2284         if (retval)
2285                 return retval;
2286         retval = ftdi_read_pcimem(ftdi, control, &control);
2287         if (retval)
2288                 return retval;
2289         msleep(sleep_time);
2290         retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2291         if (retval)
2292                 return retval;
2293         if (!(roothub_a & RH_A_NPS)) {        /* power down each port */
2294                 for (temp = 0; temp < num_ports; temp++) {
2295                         retval = ftdi_write_pcimem(ftdi,
2296                                                    roothub.portstatus[temp], RH_PS_LSDA);
2297                         if (retval)
2298                                 return retval;
2299                 }
2300         }
2301         retval = ftdi_read_pcimem(ftdi, control, &control);
2302         if (retval)
2303                 return retval;
2304 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2305         if (retval)
2306                 return retval;
2307         retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2308         if (retval)
2309                 return retval;
2310 extra:{
2311                 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2312                 if (retval)
2313                         return retval;
2314                 if (0 != (status & OHCI_HCR)) {
2315                         if (--reset_timeout == 0) {
2316                                 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2317                                 return -ENODEV;
2318                         } else {
2319                                 msleep(5);
2320                                 goto extra;
2321                         }
2322                 }
2323         }
2324         if (quirk & OHCI_QUIRK_INITRESET) {
2325                 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2326                 if (retval)
2327                         return retval;
2328                 retval = ftdi_read_pcimem(ftdi, control, &control);
2329                 if (retval)
2330                         return retval;
2331         }
2332         retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2333         if (retval)
2334                 return retval;
2335         retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2336         if (retval)
2337                 return retval;
2338         retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2339         if (retval)
2340                 return retval;
2341         retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2342         if (retval)
2343                 return retval;
2344         retval = ftdi_write_pcimem(ftdi, fminterval,
2345                                    ((fminterval & FIT) ^ FIT) | hc_fminterval);
2346         if (retval)
2347                 return retval;
2348         retval = ftdi_write_pcimem(ftdi, periodicstart,
2349                                    ((9 *hc_fminterval) / 10) & 0x3fff);
2350         if (retval)
2351                 return retval;
2352         retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2353         if (retval)
2354                 return retval;
2355         retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2356         if (retval)
2357                 return retval;
2358         if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2359                 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2360                         quirk |= OHCI_QUIRK_INITRESET;
2361                         goto retry;
2362                 } else
2363                         dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2364                                 fminterval, periodicstart);
2365         }                        /* start controller operations */
2366         hc_control &= OHCI_CTRL_RWC;
2367         hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2368         retval = ftdi_write_pcimem(ftdi, control, hc_control);
2369         if (retval)
2370                 return retval;
2371         retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2372         if (retval)
2373                 return retval;
2374         retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2375         if (retval)
2376                 return retval;
2377         retval = ftdi_read_pcimem(ftdi, control, &control);
2378         if (retval)
2379                 return retval;
2380         retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2381         if (retval)
2382                 return retval;
2383         retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2384         if (retval)
2385                 return retval;
2386         retval = ftdi_write_pcimem(ftdi, intrdisable,
2387                                    OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2388                                    OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2389                                    OHCI_INTR_SO);
2390         if (retval)
2391                 return retval;        /* handle root hub init quirks ... */
2392         retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2393         if (retval)
2394                 return retval;
2395         roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2396         if (quirk & OHCI_QUIRK_SUPERIO) {
2397                 roothub_a |= RH_A_NOCP;
2398                 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2399                 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2400                 if (retval)
2401                         return retval;
2402         } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2403                 roothub_a |= RH_A_NPS;
2404                 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2405                 if (retval)
2406                         return retval;
2407         }
2408         retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2409         if (retval)
2410                 return retval;
2411         retval = ftdi_write_pcimem(ftdi, roothub.b,
2412                                    (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2413         if (retval)
2414                 return retval;
2415         retval = ftdi_read_pcimem(ftdi, control, &control);
2416         if (retval)
2417                 return retval;
2418         mdelay((roothub_a >> 23) & 0x1fe);
2419         for (temp = 0; temp < num_ports; temp++) {
2420                 u32 portstatus;
2421                 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2422                                           &portstatus);
2423                 if (retval)
2424                         return retval;
2425                 if (1 & portstatus)
2426                         devices += 1;
2427         }
2428         return devices;
2429 }
2430
2431 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2432 {
2433         u32 latence_timer;
2434         int UxxxStatus;
2435         u32 pcidata;
2436         int reg = 0;
2437         int activePCIfn = fn << 8;
2438         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2439         if (UxxxStatus)
2440                 return UxxxStatus;
2441         reg = 16;
2442         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2443                                             0xFFFFFFFF);
2444         if (UxxxStatus)
2445                 return UxxxStatus;
2446         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2447                                            &pcidata);
2448         if (UxxxStatus)
2449                 return UxxxStatus;
2450         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2451                                             0xF0000000);
2452         if (UxxxStatus)
2453                 return UxxxStatus;
2454         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2455                                            &pcidata);
2456         if (UxxxStatus)
2457                 return UxxxStatus;
2458         reg = 12;
2459         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2460                                            &latence_timer);
2461         if (UxxxStatus)
2462                 return UxxxStatus;
2463         latence_timer &= 0xFFFF00FF;
2464         latence_timer |= 0x00001600;
2465         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2466                                             latence_timer);
2467         if (UxxxStatus)
2468                 return UxxxStatus;
2469         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2470                                            &pcidata);
2471         if (UxxxStatus)
2472                 return UxxxStatus;
2473         reg = 4;
2474         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2475                                             0x06);
2476         if (UxxxStatus)
2477                 return UxxxStatus;
2478         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2479                                            &pcidata);
2480         if (UxxxStatus)
2481                 return UxxxStatus;
2482         for (reg = 0; reg <= 0x54; reg += 4) {
2483                 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2484                 if (UxxxStatus)
2485                         return UxxxStatus;
2486         }
2487         return 0;
2488 }
2489
2490 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2491 {
2492         u32 latence_timer;
2493         int UxxxStatus;
2494         u32 pcidata;
2495         int reg = 0;
2496         int activePCIfn = fn << 8;
2497         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2498         if (UxxxStatus)
2499                 return UxxxStatus;
2500         reg = 16;
2501         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2502                                             0xFFFFFFFF);
2503         if (UxxxStatus)
2504                 return UxxxStatus;
2505         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2506                                            &pcidata);
2507         if (UxxxStatus)
2508                 return UxxxStatus;
2509         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2510                                             0x00000000);
2511         if (UxxxStatus)
2512                 return UxxxStatus;
2513         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2514                                            &pcidata);
2515         if (UxxxStatus)
2516                 return UxxxStatus;
2517         reg = 12;
2518         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2519                                            &latence_timer);
2520         if (UxxxStatus)
2521                 return UxxxStatus;
2522         latence_timer &= 0xFFFF00FF;
2523         latence_timer |= 0x00001600;
2524         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2525                                             latence_timer);
2526         if (UxxxStatus)
2527                 return UxxxStatus;
2528         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2529                                            &pcidata);
2530         if (UxxxStatus)
2531                 return UxxxStatus;
2532         reg = 4;
2533         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2534                                             0x00);
2535         if (UxxxStatus)
2536                 return UxxxStatus;
2537         return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2538 }
2539
2540 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2541 {
2542         int result;
2543         int UxxxStatus;
2544         UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2545         if (UxxxStatus)
2546                 return UxxxStatus;
2547         result = ftdi_elan_check_controller(ftdi, quirk);
2548         UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2549         if (UxxxStatus)
2550                 return UxxxStatus;
2551         return result;
2552 }
2553
2554 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2555 {
2556         u32 controlreg;
2557         u8 sensebits;
2558         int UxxxStatus;
2559         UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2560         if (UxxxStatus)
2561                 return UxxxStatus;
2562         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2563         if (UxxxStatus)
2564                 return UxxxStatus;
2565         msleep(750);
2566         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2567         if (UxxxStatus)
2568                 return UxxxStatus;
2569         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2570         if (UxxxStatus)
2571                 return UxxxStatus;
2572         UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2573         if (UxxxStatus)
2574                 return UxxxStatus;
2575         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2576         if (UxxxStatus)
2577                 return UxxxStatus;
2578         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2579         if (UxxxStatus)
2580                 return UxxxStatus;
2581         msleep(250);
2582         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2583         if (UxxxStatus)
2584                 return UxxxStatus;
2585         UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2586         if (UxxxStatus)
2587                 return UxxxStatus;
2588         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2589         if (UxxxStatus)
2590                 return UxxxStatus;
2591         UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2592         if (UxxxStatus)
2593                 return UxxxStatus;
2594         UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2595         if (UxxxStatus)
2596                 return UxxxStatus;
2597         msleep(1000);
2598         sensebits = (controlreg >> 16) & 0x000F;
2599         if (0x0D == sensebits)
2600                 return 0;
2601         else
2602                 return - ENXIO;
2603 }
2604
2605 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2606 {
2607         int UxxxStatus;
2608         u32 pcidata;
2609         int reg = 0;
2610         u8 fn;
2611         int activePCIfn = 0;
2612         int max_devices = 0;
2613         int controllers = 0;
2614         int unrecognized = 0;
2615         ftdi->function = 0;
2616         for (fn = 0; (fn < 4); fn++) {
2617                 u32 pciVID = 0;
2618                 u32 pciPID = 0;
2619                 int devices = 0;
2620                 activePCIfn = fn << 8;
2621                 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2622                                                    &pcidata);
2623                 if (UxxxStatus)
2624                         return UxxxStatus;
2625                 pciVID = pcidata & 0xFFFF;
2626                 pciPID = (pcidata >> 16) & 0xFFFF;
2627                 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2628                         devices = ftdi_elan_found_controller(ftdi, fn, 0);
2629                         controllers += 1;
2630                 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2631                 {
2632                         devices = ftdi_elan_found_controller(ftdi, fn, 0);
2633                         controllers += 1;
2634                 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2635                         devices = ftdi_elan_found_controller(ftdi, fn, 0);
2636                         controllers += 1;
2637                 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2638                 {
2639                         devices = ftdi_elan_found_controller(ftdi, fn, 0);
2640                         controllers += 1;
2641                 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2642                         devices = ftdi_elan_found_controller(ftdi, fn,
2643                                                              OHCI_QUIRK_AMD756);
2644                         controllers += 1;
2645                 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2646                         devices = ftdi_elan_found_controller(ftdi, fn,
2647                                                              OHCI_QUIRK_ZFMICRO);
2648                         controllers += 1;
2649                 } else if (0 == pcidata) {
2650                 } else
2651                         unrecognized += 1;
2652                 if (devices > max_devices) {
2653                         max_devices = devices;
2654                         ftdi->function = fn + 1;
2655                         ftdi->platform_data.vendor = pciVID;
2656                         ftdi->platform_data.device = pciPID;
2657                 }
2658         }
2659         if (ftdi->function > 0) {
2660                 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2661         } else if (controllers > 0) {
2662                 return -ENXIO;
2663         } else if (unrecognized > 0) {
2664                 return -ENXIO;
2665         } else {
2666                 ftdi->enumerated = 0;
2667                 return -ENXIO;
2668         }
2669 }
2670
2671
2672 /*
2673  * we use only the first bulk-in and bulk-out endpoints
2674  */
2675 static int ftdi_elan_probe(struct usb_interface *interface,
2676                            const struct usb_device_id *id)
2677 {
2678         struct usb_host_interface *iface_desc;
2679         struct usb_endpoint_descriptor *bulk_in, *bulk_out;
2680         int retval;
2681         struct usb_ftdi *ftdi;
2682
2683         ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2684         if (!ftdi)
2685                 return -ENOMEM;
2686
2687         mutex_lock(&ftdi_module_lock);
2688         list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2689         ftdi->sequence_num = ++ftdi_instances;
2690         mutex_unlock(&ftdi_module_lock);
2691         ftdi_elan_init_kref(ftdi);
2692         sema_init(&ftdi->sw_lock, 1);
2693         ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2694         ftdi->interface = interface;
2695         mutex_init(&ftdi->u132_lock);
2696         ftdi->expected = 4;
2697
2698         iface_desc = interface->cur_altsetting;
2699         retval = usb_find_common_endpoints(iface_desc,
2700                         &bulk_in, &bulk_out, NULL, NULL);
2701         if (retval) {
2702                 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2703                 goto error;
2704         }
2705
2706         ftdi->bulk_in_size = usb_endpoint_maxp(bulk_in);
2707         ftdi->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
2708         ftdi->bulk_in_buffer = kmalloc(ftdi->bulk_in_size, GFP_KERNEL);
2709         if (!ftdi->bulk_in_buffer) {
2710                 retval = -ENOMEM;
2711                 goto error;
2712         }
2713
2714         ftdi->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
2715
2716         dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2717                  iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2718                  ftdi->bulk_out_endpointAddr);
2719         usb_set_intfdata(interface, ftdi);
2720         if (iface_desc->desc.bInterfaceNumber == 0 &&
2721             ftdi->bulk_in_endpointAddr == 0x81 &&
2722             ftdi->bulk_out_endpointAddr == 0x02) {
2723                 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2724                 if (retval) {
2725                         dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2726                         usb_set_intfdata(interface, NULL);
2727                         retval = -ENOMEM;
2728                         goto error;
2729                 } else {
2730                         ftdi->class = &ftdi_elan_jtag_class;
2731                         dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2732                                  ftdi, iface_desc->desc.bInterfaceNumber,
2733                                  interface->minor);
2734                         return 0;
2735                 }
2736         } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2737                    ftdi->bulk_in_endpointAddr == 0x83 &&
2738                    ftdi->bulk_out_endpointAddr == 0x04) {
2739                 ftdi->class = NULL;
2740                 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2741                          ftdi, iface_desc->desc.bInterfaceNumber);
2742                 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2743                 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2744                 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2745                 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2746                 return 0;
2747         } else {
2748                 dev_err(&ftdi->udev->dev,
2749                         "Could not find ELAN's U132 device\n");
2750                 retval = -ENODEV;
2751                 goto error;
2752         }
2753 error:if (ftdi) {
2754                 ftdi_elan_put_kref(ftdi);
2755         }
2756         return retval;
2757 }
2758
2759 static void ftdi_elan_disconnect(struct usb_interface *interface)
2760 {
2761         struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2762         ftdi->disconnected += 1;
2763         if (ftdi->class) {
2764                 int minor = interface->minor;
2765                 struct usb_class_driver *class = ftdi->class;
2766                 usb_set_intfdata(interface, NULL);
2767                 usb_deregister_dev(interface, class);
2768                 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2769                          minor);
2770         } else {
2771                 ftdi_status_cancel_work(ftdi);
2772                 ftdi_command_cancel_work(ftdi);
2773                 ftdi_response_cancel_work(ftdi);
2774                 ftdi_elan_abandon_completions(ftdi);
2775                 ftdi_elan_abandon_targets(ftdi);
2776                 if (ftdi->registered) {
2777                         platform_device_unregister(&ftdi->platform_dev);
2778                         ftdi->synchronized = 0;
2779                         ftdi->enumerated = 0;
2780                         ftdi->initialized = 0;
2781                         ftdi->registered = 0;
2782                 }
2783                 ftdi->disconnected += 1;
2784                 usb_set_intfdata(interface, NULL);
2785                 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2786         }
2787         ftdi_elan_put_kref(ftdi);
2788 }
2789
2790 static struct usb_driver ftdi_elan_driver = {
2791         .name = "ftdi-elan",
2792         .probe = ftdi_elan_probe,
2793         .disconnect = ftdi_elan_disconnect,
2794         .id_table = ftdi_elan_table,
2795 };
2796 static int __init ftdi_elan_init(void)
2797 {
2798         int result;
2799         pr_info("driver %s\n", ftdi_elan_driver.name);
2800         mutex_init(&ftdi_module_lock);
2801         INIT_LIST_HEAD(&ftdi_static_list);
2802         result = usb_register(&ftdi_elan_driver);
2803         if (result) {
2804                 pr_err("usb_register failed. Error number %d\n", result);
2805         }
2806         return result;
2807
2808 }
2809
2810 static void __exit ftdi_elan_exit(void)
2811 {
2812         struct usb_ftdi *ftdi;
2813         struct usb_ftdi *temp;
2814         usb_deregister(&ftdi_elan_driver);
2815         pr_info("ftdi_u132 driver deregistered\n");
2816         list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2817                 ftdi_status_cancel_work(ftdi);
2818                 ftdi_command_cancel_work(ftdi);
2819                 ftdi_response_cancel_work(ftdi);
2820         }
2821 }
2822
2823
2824 module_init(ftdi_elan_init);
2825 module_exit(ftdi_elan_exit);