Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfashe...
[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 static 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 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1430         u8 width, u32 *data)
1431 {
1432         u8 addressofs = config_offset / 4;
1433       wait:if (ftdi->disconnected > 0) {
1434                 return -ENODEV;
1435         } else {
1436                 int command_size;
1437                 int respond_size;
1438                 down(&ftdi->u132_lock);
1439                 command_size = ftdi->command_next - ftdi->command_head;
1440                 respond_size = ftdi->respond_next - ftdi->respond_head;
1441                 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1442                         {
1443                         struct u132_command *command = &ftdi->command[
1444                                 COMMAND_MASK & ftdi->command_next];
1445                         struct u132_respond *respond = &ftdi->respond[
1446                                 RESPOND_MASK & ftdi->respond_next];
1447                         int result = -ENODEV;
1448                         respond->result = &result;
1449                         respond->header = command->header = 0x00 | (cPCIcfgrd &
1450                                 0x0F);
1451                         command->length = 0x04;
1452                         respond->address = command->address = addressofs;
1453                         command->width = 0x00 | (width & 0x0F);
1454                         command->follows = 0;
1455                         command->value = 0;
1456                         command->buffer = NULL;
1457                         respond->value = data;
1458                         init_completion(&respond->wait_completion);
1459                         ftdi->command_next += 1;
1460                         ftdi->respond_next += 1;
1461                         ftdi_elan_kick_command_queue(ftdi);
1462                         up(&ftdi->u132_lock);
1463                         wait_for_completion(&respond->wait_completion);
1464                         return result;
1465                 } else {
1466                         up(&ftdi->u132_lock);
1467                         msleep(100);
1468                         goto wait;
1469                 }
1470         }
1471 }
1472
1473 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1474         u8 width, u32 *data)
1475 {
1476         u8 addressofs = mem_offset / 4;
1477       wait:if (ftdi->disconnected > 0) {
1478                 return -ENODEV;
1479         } else {
1480                 int command_size;
1481                 int respond_size;
1482                 down(&ftdi->u132_lock);
1483                 command_size = ftdi->command_next - ftdi->command_head;
1484                 respond_size = ftdi->respond_next - ftdi->respond_head;
1485                 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1486                         {
1487                         struct u132_command *command = &ftdi->command[
1488                                 COMMAND_MASK & ftdi->command_next];
1489                         struct u132_respond *respond = &ftdi->respond[
1490                                 RESPOND_MASK & ftdi->respond_next];
1491                         int result = -ENODEV;
1492                         respond->result = &result;
1493                         respond->header = command->header = 0x00 | (cPCImemrd &
1494                                 0x0F);
1495                         command->length = 0x04;
1496                         respond->address = command->address = addressofs;
1497                         command->width = 0x00 | (width & 0x0F);
1498                         command->follows = 0;
1499                         command->value = 0;
1500                         command->buffer = NULL;
1501                         respond->value = data;
1502                         init_completion(&respond->wait_completion);
1503                         ftdi->command_next += 1;
1504                         ftdi->respond_next += 1;
1505                         ftdi_elan_kick_command_queue(ftdi);
1506                         up(&ftdi->u132_lock);
1507                         wait_for_completion(&respond->wait_completion);
1508                         return result;
1509                 } else {
1510                         up(&ftdi->u132_lock);
1511                         msleep(100);
1512                         goto wait;
1513                 }
1514         }
1515 }
1516
1517 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1518         u8 width, u32 *data)
1519 {
1520         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1521         if (ftdi->initialized == 0) {
1522                 return -ENODEV;
1523         } else
1524                 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1525 }
1526
1527
1528 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1529 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1530         void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1531         void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1532         int toggle_bits, int error_count, int condition_code, int repeat_number,
1533          int halted, int skipped, int actual, int non_null))
1534 {
1535         u8 ed = ed_number - 1;
1536       wait:if (ftdi->disconnected > 0) {
1537                 return -ENODEV;
1538         } else if (ftdi->initialized == 0) {
1539                 return -ENODEV;
1540         } else {
1541                 int command_size;
1542                 down(&ftdi->u132_lock);
1543                 command_size = ftdi->command_next - ftdi->command_head;
1544                 if (command_size < COMMAND_SIZE) {
1545                         struct u132_target *target = &ftdi->target[ed];
1546                         struct u132_command *command = &ftdi->command[
1547                                 COMMAND_MASK & ftdi->command_next];
1548                         command->header = 0x80 | (ed << 5);
1549                         command->length = 0x8007;
1550                         command->address = (toggle_bits << 6) | (ep_number << 2)
1551                                 | (address << 0);
1552                         command->width = usb_maxpacket(urb->dev, urb->pipe,
1553                                 usb_pipeout(urb->pipe));
1554                         command->follows = 8;
1555                         command->value = 0;
1556                         command->buffer = urb->setup_packet;
1557                         target->callback = callback;
1558                         target->endp = endp;
1559                         target->urb = urb;
1560                         target->active = 1;
1561                         ftdi->command_next += 1;
1562                         ftdi_elan_kick_command_queue(ftdi);
1563                         up(&ftdi->u132_lock);
1564                         return 0;
1565                 } else {
1566                         up(&ftdi->u132_lock);
1567                         msleep(100);
1568                         goto wait;
1569                 }
1570         }
1571 }
1572
1573 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1574         void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1575         void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1576         int toggle_bits, int error_count, int condition_code, int repeat_number,
1577          int halted, int skipped, int actual, int non_null))
1578 {
1579         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1580         return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1581                 ep_number, toggle_bits, callback);
1582 }
1583
1584
1585 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1586 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1587         void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1588         void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1589         int toggle_bits, int error_count, int condition_code, int repeat_number,
1590          int halted, int skipped, int actual, int non_null))
1591 {
1592         u8 ed = ed_number - 1;
1593       wait:if (ftdi->disconnected > 0) {
1594                 return -ENODEV;
1595         } else if (ftdi->initialized == 0) {
1596                 return -ENODEV;
1597         } else {
1598                 int command_size;
1599                 down(&ftdi->u132_lock);
1600                 command_size = ftdi->command_next - ftdi->command_head;
1601                 if (command_size < COMMAND_SIZE) {
1602                         struct u132_target *target = &ftdi->target[ed];
1603                         struct u132_command *command = &ftdi->command[
1604                                 COMMAND_MASK & ftdi->command_next];
1605                         int remaining_length = urb->transfer_buffer_length -
1606                                 urb->actual_length;
1607                         command->header = 0x82 | (ed << 5);
1608                         if (remaining_length == 0) {
1609                                 command->length = 0x0000;
1610                         } else if (remaining_length > 1024) {
1611                                 command->length = 0x8000 | 1023;
1612                         } else
1613                                 command->length = 0x8000 | (remaining_length -
1614                                         1);
1615                         command->address = (toggle_bits << 6) | (ep_number << 2)
1616                                 | (address << 0);
1617                         command->width = usb_maxpacket(urb->dev, urb->pipe,
1618                                 usb_pipeout(urb->pipe));
1619                         command->follows = 0;
1620                         command->value = 0;
1621                         command->buffer = NULL;
1622                         target->callback = callback;
1623                         target->endp = endp;
1624                         target->urb = urb;
1625                         target->active = 1;
1626                         ftdi->command_next += 1;
1627                         ftdi_elan_kick_command_queue(ftdi);
1628                         up(&ftdi->u132_lock);
1629                         return 0;
1630                 } else {
1631                         up(&ftdi->u132_lock);
1632                         msleep(100);
1633                         goto wait;
1634                 }
1635         }
1636 }
1637
1638 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1639         void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1640         void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1641         int toggle_bits, int error_count, int condition_code, int repeat_number,
1642          int halted, int skipped, int actual, int non_null))
1643 {
1644         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1645         return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1646                 ep_number, toggle_bits, callback);
1647 }
1648
1649
1650 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1651 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1652         void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1653         void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1654         int toggle_bits, int error_count, int condition_code, int repeat_number,
1655          int halted, int skipped, int actual, int non_null))
1656 {
1657         u8 ed = ed_number - 1;
1658       wait:if (ftdi->disconnected > 0) {
1659                 return -ENODEV;
1660         } else if (ftdi->initialized == 0) {
1661                 return -ENODEV;
1662         } else {
1663                 int command_size;
1664                 down(&ftdi->u132_lock);
1665                 command_size = ftdi->command_next - ftdi->command_head;
1666                 if (command_size < COMMAND_SIZE) {
1667                         struct u132_target *target = &ftdi->target[ed];
1668                         struct u132_command *command = &ftdi->command[
1669                                 COMMAND_MASK & ftdi->command_next];
1670                         command->header = 0x81 | (ed << 5);
1671                         command->length = 0x0000;
1672                         command->address = (toggle_bits << 6) | (ep_number << 2)
1673                                 | (address << 0);
1674                         command->width = usb_maxpacket(urb->dev, urb->pipe,
1675                                 usb_pipeout(urb->pipe));
1676                         command->follows = 0;
1677                         command->value = 0;
1678                         command->buffer = NULL;
1679                         target->callback = callback;
1680                         target->endp = endp;
1681                         target->urb = urb;
1682                         target->active = 1;
1683                         ftdi->command_next += 1;
1684                         ftdi_elan_kick_command_queue(ftdi);
1685                         up(&ftdi->u132_lock);
1686                         return 0;
1687                 } else {
1688                         up(&ftdi->u132_lock);
1689                         msleep(100);
1690                         goto wait;
1691                 }
1692         }
1693 }
1694
1695 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1696         void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1697         void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1698         int toggle_bits, int error_count, int condition_code, int repeat_number,
1699          int halted, int skipped, int actual, int non_null))
1700 {
1701         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1702         return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1703                 ep_number, toggle_bits, callback);
1704 }
1705
1706
1707 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1708 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1709         void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1710         void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1711         int toggle_bits, int error_count, int condition_code, int repeat_number,
1712          int halted, int skipped, int actual, int non_null))
1713 {
1714         u8 ed = ed_number - 1;
1715       wait:if (ftdi->disconnected > 0) {
1716                 return -ENODEV;
1717         } else if (ftdi->initialized == 0) {
1718                 return -ENODEV;
1719         } else {
1720                 int command_size;
1721                 down(&ftdi->u132_lock);
1722                 command_size = ftdi->command_next - ftdi->command_head;
1723                 if (command_size < COMMAND_SIZE) {
1724                         u8 *b;
1725                         u16 urb_size;
1726                         int i = 0;
1727                         char data[30 *3 + 4];
1728                         char *d = data;
1729                         int m = (sizeof(data) - 1) / 3;
1730                         int l = 0;
1731                         struct u132_target *target = &ftdi->target[ed];
1732                         struct u132_command *command = &ftdi->command[
1733                                 COMMAND_MASK & ftdi->command_next];
1734                         command->header = 0x81 | (ed << 5);
1735                         command->address = (toggle_bits << 6) | (ep_number << 2)
1736                                 | (address << 0);
1737                         command->width = usb_maxpacket(urb->dev, urb->pipe,
1738                                 usb_pipeout(urb->pipe));
1739                         command->follows = min(1024,
1740                                 urb->transfer_buffer_length -
1741                                 urb->actual_length);
1742                         command->value = 0;
1743                         command->buffer = urb->transfer_buffer +
1744                                 urb->actual_length;
1745                         command->length = 0x8000 | (command->follows - 1);
1746                         b = command->buffer;
1747                         urb_size = command->follows;
1748                         data[0] = 0;
1749                         while (urb_size-- > 0) {
1750                                 if (i > m) {
1751                                 } else if (i++ < m) {
1752                                         int w = sprintf(d, " %02X", *b++);
1753                                         d += w;
1754                                         l += w;
1755                                 } else
1756                                         d += sprintf(d, " ..");
1757                         }
1758                         target->callback = callback;
1759                         target->endp = endp;
1760                         target->urb = urb;
1761                         target->active = 1;
1762                         ftdi->command_next += 1;
1763                         ftdi_elan_kick_command_queue(ftdi);
1764                         up(&ftdi->u132_lock);
1765                         return 0;
1766                 } else {
1767                         up(&ftdi->u132_lock);
1768                         msleep(100);
1769                         goto wait;
1770                 }
1771         }
1772 }
1773
1774 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1775         void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1776         void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1777         int toggle_bits, int error_count, int condition_code, int repeat_number,
1778          int halted, int skipped, int actual, int non_null))
1779 {
1780         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1781         return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1782                 ep_number, toggle_bits, callback);
1783 }
1784
1785
1786 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1787 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1788         void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1789         void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1790         int toggle_bits, int error_count, int condition_code, int repeat_number,
1791          int halted, int skipped, int actual, int non_null))
1792 {
1793         u8 ed = ed_number - 1;
1794       wait:if (ftdi->disconnected > 0) {
1795                 return -ENODEV;
1796         } else if (ftdi->initialized == 0) {
1797                 return -ENODEV;
1798         } else {
1799                 int command_size;
1800                 down(&ftdi->u132_lock);
1801                 command_size = ftdi->command_next - ftdi->command_head;
1802                 if (command_size < COMMAND_SIZE) {
1803                         int remaining_length = urb->transfer_buffer_length -
1804                                 urb->actual_length;
1805                         struct u132_target *target = &ftdi->target[ed];
1806                         struct u132_command *command = &ftdi->command[
1807                                 COMMAND_MASK & ftdi->command_next];
1808                         command->header = 0x83 | (ed << 5);
1809                         if (remaining_length == 0) {
1810                                 command->length = 0x0000;
1811                         } else if (remaining_length > 1024) {
1812                                 command->length = 0x8000 | 1023;
1813                         } else
1814                                 command->length = 0x8000 | (remaining_length -
1815                                         1);
1816                         command->address = (toggle_bits << 6) | (ep_number << 2)
1817                                 | (address << 0);
1818                         command->width = usb_maxpacket(urb->dev, urb->pipe,
1819                                 usb_pipeout(urb->pipe));
1820                         command->follows = 0;
1821                         command->value = 0;
1822                         command->buffer = NULL;
1823                         target->callback = callback;
1824                         target->endp = endp;
1825                         target->urb = urb;
1826                         target->active = 1;
1827                         ftdi->command_next += 1;
1828                         ftdi_elan_kick_command_queue(ftdi);
1829                         up(&ftdi->u132_lock);
1830                         return 0;
1831                 } else {
1832                         up(&ftdi->u132_lock);
1833                         msleep(100);
1834                         goto wait;
1835                 }
1836         }
1837 }
1838
1839 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1840         void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1841         void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1842         int toggle_bits, int error_count, int condition_code, int repeat_number,
1843          int halted, int skipped, int actual, int non_null))
1844 {
1845         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1846         return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1847                 ep_number, toggle_bits, callback);
1848 }
1849
1850
1851 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1852 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1853         void *endp)
1854 {
1855         u8 ed = ed_number - 1;
1856         if (ftdi->disconnected > 0) {
1857                 return -ENODEV;
1858         } else if (ftdi->initialized == 0) {
1859                 return -ENODEV;
1860         } else {
1861                 struct u132_target *target = &ftdi->target[ed];
1862                 down(&ftdi->u132_lock);
1863                 if (target->abandoning > 0) {
1864                         up(&ftdi->u132_lock);
1865                         return 0;
1866                 } else {
1867                         target->abandoning = 1;
1868                       wait_1:if (target->active == 1) {
1869                                 int command_size = ftdi->command_next -
1870                                         ftdi->command_head;
1871                                 if (command_size < COMMAND_SIZE) {
1872                                         struct u132_command *command =
1873                                                 &ftdi->command[COMMAND_MASK &
1874                                                 ftdi->command_next];
1875                                         command->header = 0x80 | (ed << 5) |
1876                                                 0x4;
1877                                         command->length = 0x00;
1878                                         command->address = 0x00;
1879                                         command->width = 0x00;
1880                                         command->follows = 0;
1881                                         command->value = 0;
1882                                         command->buffer = &command->value;
1883                                         ftdi->command_next += 1;
1884                                         ftdi_elan_kick_command_queue(ftdi);
1885                                 } else {
1886                                         up(&ftdi->u132_lock);
1887                                         msleep(100);
1888                                         down(&ftdi->u132_lock);
1889                                         goto wait_1;
1890                                 }
1891                         }
1892                         up(&ftdi->u132_lock);
1893                         return 0;
1894                 }
1895         }
1896 }
1897
1898 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1899         void *endp)
1900 {
1901         struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1902         return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1903 }
1904
1905
1906 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1907 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1908 {
1909         int retry_on_empty = 10;
1910         int retry_on_timeout = 5;
1911         int retry_on_status = 20;
1912       more:{
1913                 int packet_bytes = 0;
1914                 int retval = usb_bulk_msg(ftdi->udev,
1915                         usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1916                          ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1917                         &packet_bytes, msecs_to_jiffies(100));
1918                 if (packet_bytes > 2) {
1919                         char diag[30 *3 + 4];
1920                         char *d = diag;
1921                         int m = (sizeof(diag) - 1) / 3;
1922                         char *b = ftdi->bulk_in_buffer;
1923                         int bytes_read = 0;
1924                         diag[0] = 0;
1925                         while (packet_bytes-- > 0) {
1926                                 char c = *b++;
1927                                 if (bytes_read < m) {
1928                                         d += sprintf(d, " %02X",
1929                                                 0x000000FF & c);
1930                                 } else if (bytes_read > m) {
1931                                 } else
1932                                         d += sprintf(d, " ..");
1933                                 bytes_read += 1;
1934                                 continue;
1935                         }
1936                         goto more;
1937                 } else if (packet_bytes > 1) {
1938                         char s1 = ftdi->bulk_in_buffer[0];
1939                         char s2 = ftdi->bulk_in_buffer[1];
1940                         if (s1 == 0x31 && s2 == 0x60) {
1941                                 return 0;
1942                         } else if (retry_on_status-- > 0) {
1943                                 goto more;
1944                         } else {
1945                                 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1946                                         "imit reached\n");
1947                                 return -EFAULT;
1948                         }
1949                 } else if (packet_bytes > 0) {
1950                         char b1 = ftdi->bulk_in_buffer[0];
1951                         dev_err(&ftdi->udev->dev, "only one byte flushed from F"
1952                                 "TDI = %02X\n", b1);
1953                         if (retry_on_status-- > 0) {
1954                                 goto more;
1955                         } else {
1956                                 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1957                                         "imit reached\n");
1958                                 return -EFAULT;
1959                         }
1960                 } else if (retval == -ETIMEDOUT) {
1961                         if (retry_on_timeout-- > 0) {
1962                                 goto more;
1963                         } else {
1964                                 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
1965                                         "t reached\n");
1966                                 return -ENOMEM;
1967                         }
1968                 } else if (retval == 0) {
1969                         if (retry_on_empty-- > 0) {
1970                                 goto more;
1971                         } else {
1972                                 dev_err(&ftdi->udev->dev, "empty packet retry l"
1973                                         "imit reached\n");
1974                                 return -ENOMEM;
1975                         }
1976                 } else {
1977                         dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1978                         return retval;
1979                 }
1980         }
1981         return -1;
1982 }
1983
1984
1985 /*
1986 * send the long flush sequence
1987 *
1988 */
1989 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1990 {
1991         int retval;
1992         struct urb *urb;
1993         char *buf;
1994         int I = 257;
1995         int i = 0;
1996         urb = usb_alloc_urb(0, GFP_KERNEL);
1997         if (!urb) {
1998                 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
1999                         "ence\n");
2000                 return -ENOMEM;
2001         }
2002         buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2003         if (!buf) {
2004                 dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
2005                         "uence\n");
2006                 usb_free_urb(urb);
2007                 return -ENOMEM;
2008         }
2009         while (I-- > 0)
2010                 buf[i++] = 0x55;
2011         usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2012                 ftdi->bulk_out_endpointAddr), buf, i,
2013                 ftdi_elan_write_bulk_callback, ftdi);
2014         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2015         retval = usb_submit_urb(urb, GFP_KERNEL);
2016         if (retval) {
2017                 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2018                         "flush sequence\n");
2019                 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2020                 usb_free_urb(urb);
2021                 return -ENOMEM;
2022         }
2023         usb_free_urb(urb);
2024         return 0;
2025 }
2026
2027
2028 /*
2029 * send the reset sequence
2030 *
2031 */
2032 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
2033 {
2034         int retval;
2035         struct urb *urb;
2036         char *buf;
2037         int I = 4;
2038         int i = 0;
2039         urb = usb_alloc_urb(0, GFP_KERNEL);
2040         if (!urb) {
2041                 dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
2042                         "quence\n");
2043                 return -ENOMEM;
2044         }
2045         buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2046         if (!buf) {
2047                 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
2048                         " sequence\n");
2049                 usb_free_urb(urb);
2050                 return -ENOMEM;
2051         }
2052         buf[i++] = 0x55;
2053         buf[i++] = 0xAA;
2054         buf[i++] = 0x5A;
2055         buf[i++] = 0xA5;
2056         usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2057                 ftdi->bulk_out_endpointAddr), buf, i,
2058                 ftdi_elan_write_bulk_callback, ftdi);
2059         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2060         retval = usb_submit_urb(urb, GFP_KERNEL);
2061         if (retval) {
2062                 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2063                         "reset sequence\n");
2064                 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2065                 usb_free_urb(urb);
2066                 return -ENOMEM;
2067         }
2068         usb_free_urb(urb);
2069         return 0;
2070 }
2071
2072 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2073 {
2074         int retval;
2075         int long_stop = 10;
2076         int retry_on_timeout = 5;
2077         int retry_on_empty = 10;
2078         int err_count = 0;
2079         retval = ftdi_elan_flush_input_fifo(ftdi);
2080         if (retval)
2081                 return retval;
2082         ftdi->bulk_in_left = 0;
2083         ftdi->bulk_in_last = -1;
2084         while (long_stop-- > 0) {
2085                 int read_stop;
2086                 int read_stuck;
2087                 retval = ftdi_elan_synchronize_flush(ftdi);
2088                 if (retval)
2089                         return retval;
2090                 retval = ftdi_elan_flush_input_fifo(ftdi);
2091                 if (retval)
2092                         return retval;
2093               reset:retval = ftdi_elan_synchronize_reset(ftdi);
2094                 if (retval)
2095                         return retval;
2096                 read_stop = 100;
2097                 read_stuck = 10;
2098               read:{
2099                         int packet_bytes = 0;
2100                         retval = usb_bulk_msg(ftdi->udev,
2101                                 usb_rcvbulkpipe(ftdi->udev,
2102                                 ftdi->bulk_in_endpointAddr),
2103                                 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2104                                 &packet_bytes, msecs_to_jiffies(500));
2105                         if (packet_bytes > 2) {
2106                                 char diag[30 *3 + 4];
2107                                 char *d = diag;
2108                                 int m = (sizeof(diag) - 1) / 3;
2109                                 char *b = ftdi->bulk_in_buffer;
2110                                 int bytes_read = 0;
2111                                 unsigned char c = 0;
2112                                 diag[0] = 0;
2113                                 while (packet_bytes-- > 0) {
2114                                         c = *b++;
2115                                         if (bytes_read < m) {
2116                                                 d += sprintf(d, " %02X", c);
2117                                         } else if (bytes_read > m) {
2118                                         } else
2119                                                 d += sprintf(d, " ..");
2120                                         bytes_read += 1;
2121                                         continue;
2122                                 }
2123                                 if (c == 0x7E) {
2124                                         return 0;
2125                                 } else {
2126                                         if (c == 0x55) {
2127                                                 goto read;
2128                                         } else if (read_stop-- > 0) {
2129                                                 goto read;
2130                                         } else {
2131                                                 dev_err(&ftdi->udev->dev, "retr"
2132                                                         "y limit reached\n");
2133                                                 continue;
2134                                         }
2135                                 }
2136                         } else if (packet_bytes > 1) {
2137                                 unsigned char s1 = ftdi->bulk_in_buffer[0];
2138                                 unsigned char s2 = ftdi->bulk_in_buffer[1];
2139                                 if (s1 == 0x31 && s2 == 0x00) {
2140                                         if (read_stuck-- > 0) {
2141                                                 goto read;
2142                                         } else
2143                                                 goto reset;
2144                                 } else if (s1 == 0x31 && s2 == 0x60) {
2145                                         if (read_stop-- > 0) {
2146                                                 goto read;
2147                                         } else {
2148                                                 dev_err(&ftdi->udev->dev, "retr"
2149                                                         "y limit reached\n");
2150                                                 continue;
2151                                         }
2152                                 } else {
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                                 }
2161                         } else if (packet_bytes > 0) {
2162                                 if (read_stop-- > 0) {
2163                                         goto read;
2164                                 } else {
2165                                         dev_err(&ftdi->udev->dev, "retry limit "
2166                                                 "reached\n");
2167                                         continue;
2168                                 }
2169                         } else if (retval == -ETIMEDOUT) {
2170                                 if (retry_on_timeout-- > 0) {
2171                                         goto read;
2172                                 } else {
2173                                         dev_err(&ftdi->udev->dev, "TIMED OUT re"
2174                                                 "try limit reached\n");
2175                                         continue;
2176                                 }
2177                         } else if (retval == 0) {
2178                                 if (retry_on_empty-- > 0) {
2179                                         goto read;
2180                                 } else {
2181                                         dev_err(&ftdi->udev->dev, "empty packet"
2182                                                 " retry limit reached\n");
2183                                         continue;
2184                                 }
2185                         } else {
2186                                 err_count += 1;
2187                                 dev_err(&ftdi->udev->dev, "error = %d\n",
2188                                         retval);
2189                                 if (read_stop-- > 0) {
2190                                         goto read;
2191                                 } else {
2192                                         dev_err(&ftdi->udev->dev, "retry limit "
2193                                                 "reached\n");
2194                                         continue;
2195                                 }
2196                         }
2197                 }
2198         }
2199         dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2200         return -EFAULT;
2201 }
2202
2203 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2204 {
2205         int retry_on_empty = 10;
2206         int retry_on_timeout = 5;
2207         int retry_on_status = 50;
2208       more:{
2209                 int packet_bytes = 0;
2210                 int retval = usb_bulk_msg(ftdi->udev,
2211                         usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2212                          ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2213                         &packet_bytes, msecs_to_jiffies(1000));
2214                 if (packet_bytes > 2) {
2215                         char diag[30 *3 + 4];
2216                         char *d = diag;
2217                         int m = (sizeof(diag) - 1) / 3;
2218                         char *b = ftdi->bulk_in_buffer;
2219                         int bytes_read = 0;
2220                         diag[0] = 0;
2221                         while (packet_bytes-- > 0) {
2222                                 char c = *b++;
2223                                 if (bytes_read < m) {
2224                                         d += sprintf(d, " %02X",
2225                                                 0x000000FF & c);
2226                                 } else if (bytes_read > m) {
2227                                 } else
2228                                         d += sprintf(d, " ..");
2229                                 bytes_read += 1;
2230                                 continue;
2231                         }
2232                         goto more;
2233                 } else if (packet_bytes > 1) {
2234                         char s1 = ftdi->bulk_in_buffer[0];
2235                         char s2 = ftdi->bulk_in_buffer[1];
2236                         if (s1 == 0x31 && s2 == 0x60) {
2237                                 return 0;
2238                         } else if (retry_on_status-- > 0) {
2239                                 msleep(5);
2240                                 goto more;
2241                         } else
2242                                 return -EFAULT;
2243                 } else if (packet_bytes > 0) {
2244                         char b1 = ftdi->bulk_in_buffer[0];
2245                         dev_err(&ftdi->udev->dev, "only one byte flushed from F"
2246                                 "TDI = %02X\n", b1);
2247                         if (retry_on_status-- > 0) {
2248                                 msleep(5);
2249                                 goto more;
2250                         } else {
2251                                 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
2252                                         "imit reached\n");
2253                                 return -EFAULT;
2254                         }
2255                 } else if (retval == -ETIMEDOUT) {
2256                         if (retry_on_timeout-- > 0) {
2257                                 goto more;
2258                         } else {
2259                                 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
2260                                         "t reached\n");
2261                                 return -ENOMEM;
2262                         }
2263                 } else if (retval == 0) {
2264                         if (retry_on_empty-- > 0) {
2265                                 goto more;
2266                         } else {
2267                                 dev_err(&ftdi->udev->dev, "empty packet retry l"
2268                                         "imit reached\n");
2269                                 return -ENOMEM;
2270                         }
2271                 } else {
2272                         dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2273                         return -ENOMEM;
2274                 }
2275         }
2276         return -1;
2277 }
2278
2279 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2280 {
2281         int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2282         if (UxxxStatus)
2283                 return UxxxStatus;
2284         if (ftdi->controlreg & 0x00400000) {
2285                 if (ftdi->card_ejected) {
2286                 } else {
2287                         ftdi->card_ejected = 1;
2288                         dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
2289                                 "%08X\n", ftdi->controlreg);
2290                 }
2291                 return -ENODEV;
2292         } else {
2293                 u8 fn = ftdi->function - 1;
2294                 int activePCIfn = fn << 8;
2295                 u32 pcidata;
2296                 u32 pciVID;
2297                 u32 pciPID;
2298                 int reg = 0;
2299                 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2300                         &pcidata);
2301                 if (UxxxStatus)
2302                         return UxxxStatus;
2303                 pciVID = pcidata & 0xFFFF;
2304                 pciPID = (pcidata >> 16) & 0xFFFF;
2305                 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2306                         ftdi->platform_data.device) {
2307                         return 0;
2308                 } else {
2309                         dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X devi"
2310                                 "ce=%04X pciPID=%04X\n",
2311                                 ftdi->platform_data.vendor, pciVID,
2312                                 ftdi->platform_data.device, pciPID);
2313                         return -ENODEV;
2314                 }
2315         }
2316 }
2317
2318 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2319 {
2320         u32 latence_timer;
2321         u32 controlreg;
2322         int UxxxStatus;
2323         u32 pcidata;
2324         int reg = 0;
2325         int foundOHCI = 0;
2326         u8 fn;
2327         int activePCIfn = 0;
2328         u32 pciVID = 0;
2329         u32 pciPID = 0;
2330         UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2331         if (UxxxStatus)
2332                 return UxxxStatus;
2333         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2334         if (UxxxStatus)
2335                 return UxxxStatus;
2336         msleep(750);
2337         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2338         if (UxxxStatus)
2339                 return UxxxStatus;
2340         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2341         if (UxxxStatus)
2342                 return UxxxStatus;
2343         UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2344         if (UxxxStatus)
2345                 return UxxxStatus;
2346         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2347         if (UxxxStatus)
2348                 return UxxxStatus;
2349         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2350         if (UxxxStatus)
2351                 return UxxxStatus;
2352         msleep(250);
2353         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2354         if (UxxxStatus)
2355                 return UxxxStatus;
2356         UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2357         if (UxxxStatus)
2358                 return UxxxStatus;
2359         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2360         if (UxxxStatus)
2361                 return UxxxStatus;
2362         UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2363         if (UxxxStatus)
2364                 return UxxxStatus;
2365         UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2366         if (UxxxStatus)
2367                 return UxxxStatus;
2368         msleep(1000);
2369         for (fn = 0; (fn < 4) && (!foundOHCI); fn++) {
2370                 activePCIfn = fn << 8;
2371                 ftdi->function = fn + 1;
2372                 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2373                         &pcidata);
2374                 if (UxxxStatus)
2375                         return UxxxStatus;
2376                 pciVID = pcidata & 0xFFFF;
2377                 pciPID = (pcidata >> 16) & 0xFFFF;
2378                 if ((pciVID == 0x1045) && (pciPID == 0xc861)) {
2379                         foundOHCI = 1;
2380                 } else if ((pciVID == 0x1033) && (pciPID == 0x0035)) {
2381                         foundOHCI = 1;
2382                 } else if ((pciVID == 0x10b9) && (pciPID == 0x5237)) {
2383                         foundOHCI = 1;
2384                 } else if ((pciVID == 0x11c1) && (pciPID == 0x5802)) {
2385                         foundOHCI = 1;
2386                 } else if ((pciVID == 0x11AB) && (pciPID == 0x1FA6)) {
2387                 }
2388         }
2389         if (foundOHCI == 0) {
2390                 return -ENXIO;
2391         }
2392         ftdi->platform_data.vendor = pciVID;
2393         ftdi->platform_data.device = pciPID;
2394         UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2395         if (UxxxStatus)
2396                 return UxxxStatus;
2397         reg = 16;
2398         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2399                 0xFFFFFFFF);
2400         if (UxxxStatus)
2401                 return UxxxStatus;
2402         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2403                 &pcidata);
2404         if (UxxxStatus)
2405                 return UxxxStatus;
2406         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2407                 0xF0000000);
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         reg = 12;
2415         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2416                 &latence_timer);
2417         if (UxxxStatus)
2418                 return UxxxStatus;
2419         latence_timer &= 0xFFFF00FF;
2420         latence_timer |= 0x00001600;
2421         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2422                 latence_timer);
2423         if (UxxxStatus)
2424                 return UxxxStatus;
2425         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2426                 &pcidata);
2427         if (UxxxStatus)
2428                 return UxxxStatus;
2429         reg = 4;
2430         UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2431                 0x06);
2432         if (UxxxStatus)
2433                 return UxxxStatus;
2434         UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2435                 &pcidata);
2436         if (UxxxStatus)
2437                 return UxxxStatus;
2438         return 0;
2439 }
2440
2441 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2442 {
2443         u32 pcidata;
2444         int U132Status;
2445         int reg;
2446         int reset_repeat = 0;
2447       do_reset:reg = 8;
2448         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x01);
2449         if (U132Status)
2450                 return U132Status;
2451       reset_check:{
2452                 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2453                 if (U132Status)
2454                         return U132Status;
2455                 if (pcidata & 1) {
2456                         msleep(500);
2457                         if (reset_repeat++ > 100) {
2458                                 reset_repeat = 0;
2459                                 goto do_reset;
2460                         } else
2461                                 goto reset_check;
2462                 }
2463         }
2464         goto dump_regs;
2465         msleep(500);
2466         reg = 0x28;
2467         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x11000000);
2468         if (U132Status)
2469                 return U132Status;
2470         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2471         if (U132Status)
2472                 return U132Status;
2473         reg = 0x40;
2474         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf);
2475         if (U132Status)
2476                 return U132Status;
2477         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2478         if (U132Status)
2479                 return U132Status;
2480         reg = 0x34;
2481         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf2edf);
2482         if (U132Status)
2483                 return U132Status;
2484         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2485         if (U132Status)
2486                 return U132Status;
2487         reg = 4;
2488         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0xA0);
2489         if (U132Status)
2490                 return U132Status;
2491         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2492         if (U132Status)
2493                 return U132Status;
2494         msleep(250);
2495         reg = 8;
2496         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x04);
2497         if (U132Status)
2498                 return U132Status;
2499         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2500         if (U132Status)
2501                 return U132Status;
2502         reg = 0x28;
2503         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2504         if (U132Status)
2505                 return U132Status;
2506         reg = 8;
2507         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2508         if (U132Status)
2509                 return U132Status;
2510         reg = 0x48;
2511         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x00001200);
2512         if (U132Status)
2513                 return U132Status;
2514         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2515         if (U132Status)
2516                 return U132Status;
2517         reg = 0x54;
2518         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2519         if (U132Status)
2520                 return U132Status;
2521         reg = 0x58;
2522         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2523         if (U132Status)
2524                 return U132Status;
2525         reg = 0x34;
2526         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x28002edf);
2527         if (U132Status)
2528                 return U132Status;
2529         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2530         if (U132Status)
2531                 return U132Status;
2532         msleep(100);
2533         reg = 0x50;
2534         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10000);
2535         if (U132Status)
2536                 return U132Status;
2537         reg = 0x54;
2538       power_check:U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2539         if (U132Status)
2540                 return U132Status;
2541         if (!(pcidata & 1)) {
2542                 msleep(500);
2543                 goto power_check;
2544         }
2545         msleep(3000);
2546         reg = 0x54;
2547         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2548         if (U132Status)
2549                 return U132Status;
2550         reg = 0x58;
2551         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2552         if (U132Status)
2553                 return U132Status;
2554         reg = 0x54;
2555         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2556         if (U132Status)
2557                 return U132Status;
2558         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2559         if (U132Status)
2560                 return U132Status;
2561         reg = 0x54;
2562         U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10);
2563         if (U132Status)
2564                 return U132Status;
2565         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2566         if (U132Status)
2567                 return U132Status;
2568         msleep(750);
2569         reg = 0x54;
2570         if (0) {
2571                 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2572                 if (U132Status)
2573                         return U132Status;
2574         }
2575         if (0) {
2576                 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2577                 if (U132Status)
2578                         return U132Status;
2579         }
2580         reg = 0x54;
2581         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2582         if (U132Status)
2583                 return U132Status;
2584         reg = 0x58;
2585         U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2586         if (U132Status)
2587                 return U132Status;
2588       dump_regs:for (reg = 0; reg <= 0x54; reg += 4) {
2589                 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2590                 if (U132Status)
2591                         return U132Status;
2592         }
2593         return 0;
2594 }
2595
2596
2597 /*
2598 * we use only the first bulk-in and bulk-out endpoints
2599 */
2600 static int ftdi_elan_probe(struct usb_interface *interface,
2601         const struct usb_device_id *id)
2602 {
2603         struct usb_host_interface *iface_desc;
2604         struct usb_endpoint_descriptor *endpoint;
2605         size_t buffer_size;
2606         int i;
2607         int retval = -ENOMEM;
2608         struct usb_ftdi *ftdi = kmalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2609         if (ftdi == NULL) {
2610                 printk(KERN_ERR "Out of memory\n");
2611                 return -ENOMEM;
2612         }
2613         memset(ftdi, 0x00, sizeof(struct usb_ftdi));
2614         down(&ftdi_module_lock);
2615         list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2616         ftdi->sequence_num = ++ftdi_instances;
2617         up(&ftdi_module_lock);
2618         ftdi_elan_init_kref(ftdi);
2619         init_MUTEX(&ftdi->sw_lock);
2620         ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2621         ftdi->interface = interface;
2622         init_MUTEX(&ftdi->u132_lock);
2623         ftdi->expected = 4;
2624         iface_desc = interface->cur_altsetting;
2625         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2626                 endpoint = &iface_desc->endpoint[i].desc;
2627                 if (!ftdi->bulk_in_endpointAddr &&
2628                     usb_endpoint_is_bulk_in(endpoint)) {
2629                         buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2630                         ftdi->bulk_in_size = buffer_size;
2631                         ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2632                         ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2633                         if (!ftdi->bulk_in_buffer) {
2634                                 dev_err(&ftdi->udev->dev, "Could not allocate b"
2635                                         "ulk_in_buffer\n");
2636                                 retval = -ENOMEM;
2637                                 goto error;
2638                         }
2639                 }
2640                 if (!ftdi->bulk_out_endpointAddr &&
2641                     usb_endpoint_is_bulk_out(endpoint)) {
2642                         ftdi->bulk_out_endpointAddr =
2643                                 endpoint->bEndpointAddress;
2644                 }
2645         }
2646         if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2647                 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk"
2648                         "-out endpoints\n");
2649                 retval = -ENODEV;
2650                 goto error;
2651         }
2652         dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2653                 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2654                 ftdi->bulk_out_endpointAddr);
2655         usb_set_intfdata(interface, ftdi);
2656         if (iface_desc->desc.bInterfaceNumber == 0 &&
2657                 ftdi->bulk_in_endpointAddr == 0x81 &&
2658                 ftdi->bulk_out_endpointAddr == 0x02) {
2659                 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2660                 if (retval) {
2661                         dev_err(&ftdi->udev->dev, "Not able to get a minor for "
2662                                 "this device.\n");
2663                         usb_set_intfdata(interface, NULL);
2664                         retval = -ENOMEM;
2665                         goto error;
2666                 } else {
2667                         ftdi->class = &ftdi_elan_jtag_class;
2668                         dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface "
2669                                 "%d now attached to ftdi%d\n", ftdi,
2670                                 iface_desc->desc.bInterfaceNumber,
2671                                 interface->minor);
2672                         return 0;
2673                 }
2674         } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2675                 ftdi->bulk_in_endpointAddr == 0x83 &&
2676                 ftdi->bulk_out_endpointAddr == 0x04) {
2677                 ftdi->class = NULL;
2678                 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now a"
2679                         "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
2680                 INIT_WORK(&ftdi->status_work, ftdi_elan_status_work,
2681                         (void *)ftdi);
2682                 INIT_WORK(&ftdi->command_work, ftdi_elan_command_work,
2683                         (void *)ftdi);
2684                 INIT_WORK(&ftdi->respond_work, ftdi_elan_respond_work,
2685                         (void *)ftdi);
2686                 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2687                 return 0;
2688         } else {
2689                 dev_err(&ftdi->udev->dev,
2690                         "Could not find ELAN's U132 device\n");
2691                 retval = -ENODEV;
2692                 goto error;
2693         }
2694       error:if (ftdi) {
2695                 ftdi_elan_put_kref(ftdi);
2696         }
2697         return retval;
2698 }
2699
2700 static void ftdi_elan_disconnect(struct usb_interface *interface)
2701 {
2702         struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2703         ftdi->disconnected += 1;
2704         if (ftdi->class) {
2705                 int minor = interface->minor;
2706                 struct usb_class_driver *class = ftdi->class;
2707                 usb_set_intfdata(interface, NULL);
2708                 usb_deregister_dev(interface, class);
2709                 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on min"
2710                         "or %d now disconnected\n", minor);
2711         } else {
2712                 ftdi_status_cancel_work(ftdi);
2713                 ftdi_command_cancel_work(ftdi);
2714                 ftdi_response_cancel_work(ftdi);
2715                 ftdi_elan_abandon_completions(ftdi);
2716                 ftdi_elan_abandon_targets(ftdi);
2717                 if (ftdi->registered) {
2718                         platform_device_unregister(&ftdi->platform_dev);
2719                         ftdi->synchronized = 0;
2720                         ftdi->enumerated = 0;
2721                         ftdi->registered = 0;
2722                 }
2723                 flush_workqueue(status_queue);
2724                 flush_workqueue(command_queue);
2725                 flush_workqueue(respond_queue);
2726                 ftdi->disconnected += 1;
2727                 usb_set_intfdata(interface, NULL);
2728                 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller inter"
2729                         "face now disconnected\n");
2730         }
2731         ftdi_elan_put_kref(ftdi);
2732 }
2733
2734 static struct usb_driver ftdi_elan_driver = {
2735         .name = "ftdi-elan",
2736         .probe = ftdi_elan_probe,
2737         .disconnect = ftdi_elan_disconnect,
2738         .id_table = ftdi_elan_table,
2739 };
2740 static int __init ftdi_elan_init(void)
2741 {
2742         int result;
2743         printk(KERN_INFO "driver %s built at %s on %s\n", ftdi_elan_driver.name,
2744                  __TIME__, __DATE__);
2745         init_MUTEX(&ftdi_module_lock);
2746         INIT_LIST_HEAD(&ftdi_static_list);
2747         status_queue = create_singlethread_workqueue("ftdi-status-control");
2748         command_queue = create_singlethread_workqueue("ftdi-command-engine");
2749         respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2750         result = usb_register(&ftdi_elan_driver);
2751         if (result)
2752                 printk(KERN_ERR "usb_register failed. Error number %d\n",
2753                         result);
2754         return result;
2755 }
2756
2757 static void __exit ftdi_elan_exit(void)
2758 {
2759         struct usb_ftdi *ftdi;
2760         struct usb_ftdi *temp;
2761         usb_deregister(&ftdi_elan_driver);
2762         printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2763         list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2764                 ftdi_status_cancel_work(ftdi);
2765                 ftdi_command_cancel_work(ftdi);
2766                 ftdi_response_cancel_work(ftdi);
2767         } flush_workqueue(status_queue);
2768         destroy_workqueue(status_queue);
2769         status_queue = NULL;
2770         flush_workqueue(command_queue);
2771         destroy_workqueue(command_queue);
2772         command_queue = NULL;
2773         flush_workqueue(respond_queue);
2774         destroy_workqueue(respond_queue);
2775         respond_queue = NULL;
2776 }
2777
2778
2779 module_init(ftdi_elan_init);
2780 module_exit(ftdi_elan_exit);