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