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