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