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