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