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