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