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