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