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
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.
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 bool distrust_firmware = 1;
57 module_param(distrust_firmware, bool, 0);
58 MODULE_PARM_DESC(distrust_firmware, "true to distrust firmware power/overcurren"
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
76 #include <linux/usb/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
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 {
119 #define RESPOND_BITS 5
120 #define RESPOND_SIZE (1<<RESPOND_BITS)
121 #define RESPOND_MASK (RESPOND_SIZE-1)
122 struct u132_respond {
127 struct completion wait_completion;
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,
147 /* Structure to hold all of our device specific stuff*/
149 struct list_head ftdi_list;
150 struct mutex u132_lock;
153 struct u132_command command[COMMAND_SIZE];
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;
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;
184 __u8 bulk_in_endpointAddr;
185 __u8 bulk_out_endpointAddr;
188 u8 response[4 + 1024];
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, \
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);
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,
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)
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)
309 if (ftdi->platform_dev.dev.parent)
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);
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;
339 complete(&respond->wait_completion);
340 } mutex_unlock(&ftdi->u132_lock);
343 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
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);
359 mutex_unlock(&ftdi->u132_lock);
362 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
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 -
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;
381 command->buffer = &command->value;
382 ftdi->command_next += 1;
383 ftdi_elan_kick_command_queue(ftdi);
385 mutex_unlock(&ftdi->u132_lock);
387 mutex_lock(&ftdi->u132_lock);
391 wait_2:if (target->active == 1) {
392 int command_size = ftdi->command_next -
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;
403 command->buffer = &command->value;
404 ftdi->command_next += 1;
405 ftdi_elan_kick_command_queue(ftdi);
407 mutex_unlock(&ftdi->u132_lock);
409 mutex_lock(&ftdi->u132_lock);
417 mutex_unlock(&ftdi->u132_lock);
420 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
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 -
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;
439 command->buffer = &command->value;
440 ftdi->command_next += 1;
441 ftdi_elan_kick_command_queue(ftdi);
443 mutex_unlock(&ftdi->u132_lock);
445 mutex_lock(&ftdi->u132_lock);
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);
461 static void ftdi_elan_command_work(struct work_struct *work)
463 struct usb_ftdi *ftdi =
464 container_of(work, struct usb_ftdi, command_work.work);
466 if (ftdi->disconnected > 0) {
467 ftdi_elan_put_kref(ftdi);
470 int retval = ftdi_elan_command_engine(ftdi);
471 if (retval == -ESHUTDOWN) {
472 ftdi->disconnected += 1;
473 } else if (retval == -ENODEV) {
474 ftdi->disconnected += 1;
476 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
477 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
482 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
484 ftdi_respond_queue_work(ftdi, 0);
487 static void ftdi_elan_respond_work(struct work_struct *work)
489 struct usb_ftdi *ftdi =
490 container_of(work, struct usb_ftdi, respond_work.work);
491 if (ftdi->disconnected > 0) {
492 ftdi_elan_put_kref(ftdi);
495 int retval = ftdi_elan_respond_engine(ftdi);
497 } else if (retval == -ESHUTDOWN) {
498 ftdi->disconnected += 1;
499 } else if (retval == -ENODEV) {
500 ftdi->disconnected += 1;
501 } else if (retval == -EILSEQ) {
502 ftdi->disconnected += 1;
504 ftdi->disconnected += 1;
505 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
507 if (ftdi->disconnected > 0) {
508 ftdi_elan_abandon_completions(ftdi);
509 ftdi_elan_abandon_targets(ftdi);
511 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
518 * the sw_lock is initially held and will be freed
519 * after the FTDI has been synchronized
522 static void ftdi_elan_status_work(struct work_struct *work)
524 struct usb_ftdi *ftdi =
525 container_of(work, struct usb_ftdi, status_work.work);
526 int work_delay_in_msec = 0;
527 if (ftdi->disconnected > 0) {
528 ftdi_elan_put_kref(ftdi);
530 } else if (ftdi->synchronized == 0) {
531 down(&ftdi->sw_lock);
532 if (ftdi_elan_synchronize(ftdi) == 0) {
533 ftdi->synchronized = 1;
534 ftdi_command_queue_work(ftdi, 1);
535 ftdi_respond_queue_work(ftdi, 1);
537 work_delay_in_msec = 100;
539 dev_err(&ftdi->udev->dev, "synchronize failed\n");
541 work_delay_in_msec = 10 *1000;
543 } else if (ftdi->stuck_status > 0) {
544 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
545 ftdi->stuck_status = 0;
546 ftdi->synchronized = 0;
547 } else if ((ftdi->stuck_status++ % 60) == 1) {
548 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
549 "- please remove\n");
551 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
552 "- checked %d times\n", ftdi->stuck_status);
553 work_delay_in_msec = 100;
554 } else if (ftdi->enumerated == 0) {
555 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
556 ftdi->enumerated = 1;
557 work_delay_in_msec = 250;
559 work_delay_in_msec = 1000;
560 } else if (ftdi->initialized == 0) {
561 if (ftdi_elan_setupOHCI(ftdi) == 0) {
562 ftdi->initialized = 1;
563 work_delay_in_msec = 500;
565 dev_err(&ftdi->udev->dev, "initialized failed - trying "
566 "again in 10 seconds\n");
567 work_delay_in_msec = 1 *1000;
569 } else if (ftdi->registered == 0) {
570 work_delay_in_msec = 10;
571 if (ftdi_elan_hcd_init(ftdi) == 0) {
572 ftdi->registered = 1;
574 dev_err(&ftdi->udev->dev, "register failed\n");
575 work_delay_in_msec = 250;
577 if (ftdi_elan_checkingPCI(ftdi) == 0) {
578 work_delay_in_msec = 250;
579 } else if (ftdi->controlreg & 0x00400000) {
580 if (ftdi->gone_away > 0) {
581 dev_err(&ftdi->udev->dev, "PCI device eject con"
582 "firmed platform_dev.dev.parent=%p plat"
584 ftdi->platform_dev.dev.parent,
585 &ftdi->platform_dev.dev);
586 platform_device_unregister(&ftdi->platform_dev);
587 ftdi->platform_dev.dev.parent = NULL;
588 ftdi->registered = 0;
589 ftdi->enumerated = 0;
590 ftdi->card_ejected = 0;
591 ftdi->initialized = 0;
594 ftdi_elan_flush_targets(ftdi);
595 work_delay_in_msec = 250;
597 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n"
599 ftdi_elan_cancel_targets(ftdi);
600 work_delay_in_msec = 500;
601 ftdi->enumerated = 0;
602 ftdi->initialized = 0;
605 if (ftdi->disconnected > 0) {
606 ftdi_elan_put_kref(ftdi);
609 ftdi_status_requeue_work(ftdi,
610 msecs_to_jiffies(work_delay_in_msec));
617 * file_operations for the jtag interface
619 * the usage count for the device is incremented on open()
620 * and decremented on release()
622 static int ftdi_elan_open(struct inode *inode, struct file *file)
625 struct usb_interface *interface;
627 subminor = iminor(inode);
628 interface = usb_find_interface(&ftdi_elan_driver, subminor);
631 printk(KERN_ERR "can't find device for minor %d\n", subminor);
634 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
638 if (down_interruptible(&ftdi->sw_lock)) {
641 ftdi_elan_get_kref(ftdi);
642 file->private_data = ftdi;
649 static int ftdi_elan_release(struct inode *inode, struct file *file)
651 struct usb_ftdi *ftdi = file->private_data;
654 up(&ftdi->sw_lock); /* decrement the count on our device */
655 ftdi_elan_put_kref(ftdi);
662 * blocking bulk reads are used to get data from the device
665 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
666 size_t count, loff_t *ppos)
668 char data[30 *3 + 4];
670 int m = (sizeof(data) - 1) / 3;
672 int retry_on_empty = 10;
673 int retry_on_timeout = 5;
674 struct usb_ftdi *ftdi = file->private_data;
675 if (ftdi->disconnected > 0) {
679 have:if (ftdi->bulk_in_left > 0) {
681 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
682 ftdi->bulk_in_left -= 1;
683 if (bytes_read < m) {
684 d += sprintf(d, " %02X", 0x000000FF & *p);
685 } else if (bytes_read > m) {
687 d += sprintf(d, " ..");
688 if (copy_to_user(buffer++, p, 1)) {
697 more:if (count > 0) {
698 int packet_bytes = 0;
699 int retval = usb_bulk_msg(ftdi->udev,
700 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
701 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
703 if (packet_bytes > 2) {
704 ftdi->bulk_in_left = packet_bytes - 2;
705 ftdi->bulk_in_last = 1;
707 } else if (retval == -ETIMEDOUT) {
708 if (retry_on_timeout-- > 0) {
710 } else if (bytes_read > 0) {
714 } else if (retval == 0) {
715 if (retry_on_empty-- > 0) {
725 static void ftdi_elan_write_bulk_callback(struct urb *urb)
727 struct usb_ftdi *ftdi = urb->context;
728 int status = urb->status;
730 if (status && !(status == -ENOENT || status == -ECONNRESET ||
731 status == -ESHUTDOWN)) {
732 dev_err(&ftdi->udev->dev, "urb=%p write bulk status received: %"
735 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
736 urb->transfer_buffer, urb->transfer_dma);
739 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
740 char *buf, int command_size, int total_size)
744 int I = command_size;
745 int i = ftdi->command_head;
747 struct u132_command *command = &ftdi->command[COMMAND_MASK &
749 int F = command->follows;
750 u8 *f = command->buffer;
751 if (command->header & 0x80) {
752 ed_commands |= 1 << (0x3 & (command->header >> 5));
754 buf[b++] = command->header;
755 buf[b++] = (command->length >> 0) & 0x00FF;
756 buf[b++] = (command->length >> 8) & 0x00FF;
757 buf[b++] = command->address;
758 buf[b++] = command->width;
766 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
769 int I = command_size;
770 int i = ftdi->command_head;
772 struct u132_command *command = &ftdi->command[COMMAND_MASK &
774 total_size += 5 + command->follows;
778 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
785 int command_size = ftdi->command_next - ftdi->command_head;
786 if (command_size == 0)
788 total_size = ftdi_elan_total_command_size(ftdi, command_size);
789 urb = usb_alloc_urb(0, GFP_KERNEL);
791 dev_err(&ftdi->udev->dev, "could not get a urb to write %d comm"
792 "ands totaling %d bytes to the Uxxx\n", command_size,
796 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
799 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d c"
800 "ommands totaling %d bytes to the Uxxx\n", command_size,
805 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
806 command_size, total_size);
807 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
808 ftdi->bulk_out_endpointAddr), buf, total_size,
809 ftdi_elan_write_bulk_callback, ftdi);
810 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
812 char diag[40 *3 + 4];
816 int s = (sizeof(diag) - 1) / 3;
818 while (s-- > 0 && m-- > 0) {
819 if (s > 0 || m == 0) {
820 d += sprintf(d, " %02X", *c++);
822 d += sprintf(d, " ..");
825 retval = usb_submit_urb(urb, GFP_KERNEL);
827 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write "
828 "%d commands totaling %d bytes to the Uxxx\n", retval,
829 urb, command_size, total_size);
830 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
834 usb_free_urb(urb); /* release our reference to this urb,
835 the USB core will eventually free it entirely */
836 ftdi->command_head += command_size;
837 ftdi_elan_kick_respond_queue(ftdi);
841 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
842 struct u132_target *target, u8 *buffer, int length)
844 struct urb *urb = target->urb;
845 int halted = target->halted;
846 int skipped = target->skipped;
847 int actual = target->actual;
848 int non_null = target->non_null;
849 int toggle_bits = target->toggle_bits;
850 int error_count = target->error_count;
851 int condition_code = target->condition_code;
852 int repeat_number = target->repeat_number;
853 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
854 int, int, int, int) = target->callback;
856 target->callback = NULL;
857 (*callback) (target->endp, urb, buffer, length, toggle_bits,
858 error_count, condition_code, repeat_number, halted, skipped,
862 static char *have_ed_set_response(struct usb_ftdi *ftdi,
863 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
866 int payload = (ed_length >> 0) & 0x07FF;
867 mutex_lock(&ftdi->u132_lock);
869 target->non_null = (ed_length >> 15) & 0x0001;
870 target->repeat_number = (ed_length >> 11) & 0x000F;
871 if (ed_type == 0x02) {
872 if (payload == 0 || target->abandoning > 0) {
873 target->abandoning = 0;
874 mutex_unlock(&ftdi->u132_lock);
875 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
880 return ftdi->response;
882 ftdi->expected = 4 + payload;
884 mutex_unlock(&ftdi->u132_lock);
887 } else if (ed_type == 0x03) {
888 if (payload == 0 || target->abandoning > 0) {
889 target->abandoning = 0;
890 mutex_unlock(&ftdi->u132_lock);
891 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
896 return ftdi->response;
898 ftdi->expected = 4 + payload;
900 mutex_unlock(&ftdi->u132_lock);
903 } else if (ed_type == 0x01) {
904 target->abandoning = 0;
905 mutex_unlock(&ftdi->u132_lock);
906 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
911 return ftdi->response;
913 target->abandoning = 0;
914 mutex_unlock(&ftdi->u132_lock);
915 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
920 return ftdi->response;
924 static char *have_ed_get_response(struct usb_ftdi *ftdi,
925 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
928 mutex_lock(&ftdi->u132_lock);
929 target->condition_code = TD_DEVNOTRESP;
930 target->actual = (ed_length >> 0) & 0x01FF;
931 target->non_null = (ed_length >> 15) & 0x0001;
932 target->repeat_number = (ed_length >> 11) & 0x000F;
933 mutex_unlock(&ftdi->u132_lock);
935 ftdi_elan_do_callback(ftdi, target, NULL, 0);
936 target->abandoning = 0;
940 return ftdi->response;
945 * The engine tries to empty the FTDI fifo
947 * all responses found in the fifo data are dispatched thus
948 * the response buffer can only ever hold a maximum sized
949 * response from the Uxxx.
952 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
954 u8 *b = ftdi->response + ftdi->received;
956 int retry_on_empty = 1;
957 int retry_on_timeout = 3;
958 int empty_packets = 0;
960 int packet_bytes = 0;
961 int retval = usb_bulk_msg(ftdi->udev,
962 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
963 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
965 char diag[30 *3 + 4];
967 int m = packet_bytes;
968 u8 *c = ftdi->bulk_in_buffer;
969 int s = (sizeof(diag) - 1) / 3;
971 while (s-- > 0 && m-- > 0) {
972 if (s > 0 || m == 0) {
973 d += sprintf(d, " %02X", *c++);
975 d += sprintf(d, " ..");
977 if (packet_bytes > 2) {
978 ftdi->bulk_in_left = packet_bytes - 2;
979 ftdi->bulk_in_last = 1;
981 } else if (retval == -ETIMEDOUT) {
982 if (retry_on_timeout-- > 0) {
983 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
984 "t_bytes = %d with total %d bytes%s\n",
985 packet_bytes, bytes_read, diag);
987 } else if (bytes_read > 0) {
988 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
992 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
993 "t_bytes = %d with total %d bytes%s\n",
994 packet_bytes, bytes_read, diag);
997 } else if (retval == -EILSEQ) {
998 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
999 " = %d with total %d bytes%s\n", retval,
1000 packet_bytes, bytes_read, diag);
1002 } else if (retval) {
1003 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1004 " = %d with total %d bytes%s\n", retval,
1005 packet_bytes, bytes_read, diag);
1007 } else if (packet_bytes == 2) {
1008 unsigned char s0 = ftdi->bulk_in_buffer[0];
1009 unsigned char s1 = ftdi->bulk_in_buffer[1];
1011 if (s0 == 0x31 && s1 == 0x60) {
1012 if (retry_on_empty-- > 0) {
1016 } else if (s0 == 0x31 && s1 == 0x00) {
1017 if (retry_on_empty-- > 0) {
1022 if (retry_on_empty-- > 0) {
1027 } else if (packet_bytes == 1) {
1028 if (retry_on_empty-- > 0) {
1033 if (retry_on_empty-- > 0) {
1042 have:if (ftdi->bulk_in_left > 0) {
1043 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1045 ftdi->bulk_in_left -= 1;
1046 if (ftdi->received == 0 && c == 0xFF) {
1050 if (++ftdi->received < ftdi->expected) {
1052 } else if (ftdi->ed_found) {
1053 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1054 u16 ed_length = (ftdi->response[2] << 8) |
1056 struct u132_target *target = &ftdi->target[ed_number];
1057 int payload = (ed_length >> 0) & 0x07FF;
1058 char diag[30 *3 + 4];
1061 u8 *c = 4 + ftdi->response;
1062 int s = (sizeof(diag) - 1) / 3;
1064 while (s-- > 0 && m-- > 0) {
1065 if (s > 0 || m == 0) {
1066 d += sprintf(d, " %02X", *c++);
1068 d += sprintf(d, " ..");
1070 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1077 } else if (ftdi->expected == 8) {
1079 int respond_head = ftdi->respond_head++;
1080 struct u132_respond *respond = &ftdi->respond[
1081 RESPOND_MASK & respond_head];
1082 u32 data = ftdi->response[7];
1084 data |= ftdi->response[6];
1086 data |= ftdi->response[5];
1088 data |= ftdi->response[4];
1089 *respond->value = data;
1090 *respond->result = 0;
1091 complete(&respond->wait_completion);
1096 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1097 if (buscmd == 0x00) {
1098 } else if (buscmd == 0x02) {
1099 } else if (buscmd == 0x06) {
1100 } else if (buscmd == 0x0A) {
1102 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) va"
1103 "lue = %08X\n", buscmd, data);
1106 if ((ftdi->response[0] & 0x80) == 0x00) {
1110 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1111 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1112 u16 ed_length = (ftdi->response[2] << 8) |
1114 struct u132_target *target = &ftdi->target[
1116 target->halted = (ftdi->response[0] >> 3) &
1118 target->skipped = (ftdi->response[0] >> 2) &
1120 target->toggle_bits = (ftdi->response[3] >> 6)
1122 target->error_count = (ftdi->response[3] >> 4)
1124 target->condition_code = (ftdi->response[
1126 if ((ftdi->response[0] & 0x10) == 0x00) {
1127 b = have_ed_set_response(ftdi, target,
1128 ed_length, ed_number, ed_type,
1132 b = have_ed_get_response(ftdi, target,
1133 ed_length, ed_number, ed_type,
1145 * create a urb, and a buffer for it, and copy the data to the urb
1148 static ssize_t ftdi_elan_write(struct file *file,
1149 const char __user *user_buffer, size_t count,
1155 struct usb_ftdi *ftdi = file->private_data;
1157 if (ftdi->disconnected > 0) {
1163 urb = usb_alloc_urb(0, GFP_KERNEL);
1168 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1169 &urb->transfer_dma);
1174 if (copy_from_user(buf, user_buffer, count)) {
1178 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1179 ftdi->bulk_out_endpointAddr), buf, count,
1180 ftdi_elan_write_bulk_callback, ftdi);
1181 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1182 retval = usb_submit_urb(urb, GFP_KERNEL);
1184 dev_err(&ftdi->udev->dev, "failed submitting write urb, error %"
1193 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1200 static const struct file_operations ftdi_elan_fops = {
1201 .owner = THIS_MODULE,
1202 .llseek = no_llseek,
1203 .read = ftdi_elan_read,
1204 .write = ftdi_elan_write,
1205 .open = ftdi_elan_open,
1206 .release = ftdi_elan_release,
1210 * usb class driver info in order to get a minor number from the usb core,
1211 * and to have the device registered with the driver core
1213 static struct usb_class_driver ftdi_elan_jtag_class = {
1214 .name = "ftdi-%d-jtag",
1215 .fops = &ftdi_elan_fops,
1216 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1220 * the following definitions are for the
1221 * ELAN FPGA state machgine processor that
1222 * lies on the other side of the FTDI chip
1224 #define cPCIu132rd 0x0
1225 #define cPCIu132wr 0x1
1226 #define cPCIiord 0x2
1227 #define cPCIiowr 0x3
1228 #define cPCImemrd 0x6
1229 #define cPCImemwr 0x7
1230 #define cPCIcfgrd 0xA
1231 #define cPCIcfgwr 0xB
1232 #define cPCInull 0xF
1233 #define cU132cmd_status 0x0
1234 #define cU132flash 0x1
1235 #define cPIDsetup 0x0
1238 #define cPIDinonce 0x3
1239 #define cCCnoerror 0x0
1241 #define cCCbitstuff 0x2
1242 #define cCCtoggle 0x3
1243 #define cCCstall 0x4
1244 #define cCCnoresp 0x5
1245 #define cCCbadpid1 0x6
1246 #define cCCbadpid2 0x7
1247 #define cCCdataoverrun 0x8
1248 #define cCCdataunderrun 0x9
1249 #define cCCbuffoverrun 0xC
1250 #define cCCbuffunderrun 0xD
1251 #define cCCnotaccessed 0xF
1252 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1254 wait:if (ftdi->disconnected > 0) {
1258 mutex_lock(&ftdi->u132_lock);
1259 command_size = ftdi->command_next - ftdi->command_head;
1260 if (command_size < COMMAND_SIZE) {
1261 struct u132_command *command = &ftdi->command[
1262 COMMAND_MASK & ftdi->command_next];
1263 command->header = 0x00 | cPCIu132wr;
1264 command->length = 0x04;
1265 command->address = 0x00;
1266 command->width = 0x00;
1267 command->follows = 4;
1268 command->value = data;
1269 command->buffer = &command->value;
1270 ftdi->command_next += 1;
1271 ftdi_elan_kick_command_queue(ftdi);
1272 mutex_unlock(&ftdi->u132_lock);
1275 mutex_unlock(&ftdi->u132_lock);
1282 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1285 u8 addressofs = config_offset / 4;
1286 wait:if (ftdi->disconnected > 0) {
1290 mutex_lock(&ftdi->u132_lock);
1291 command_size = ftdi->command_next - ftdi->command_head;
1292 if (command_size < COMMAND_SIZE) {
1293 struct u132_command *command = &ftdi->command[
1294 COMMAND_MASK & ftdi->command_next];
1295 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1296 command->length = 0x04;
1297 command->address = addressofs;
1298 command->width = 0x00 | (width & 0x0F);
1299 command->follows = 4;
1300 command->value = data;
1301 command->buffer = &command->value;
1302 ftdi->command_next += 1;
1303 ftdi_elan_kick_command_queue(ftdi);
1304 mutex_unlock(&ftdi->u132_lock);
1307 mutex_unlock(&ftdi->u132_lock);
1314 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1317 u8 addressofs = mem_offset / 4;
1318 wait:if (ftdi->disconnected > 0) {
1322 mutex_lock(&ftdi->u132_lock);
1323 command_size = ftdi->command_next - ftdi->command_head;
1324 if (command_size < COMMAND_SIZE) {
1325 struct u132_command *command = &ftdi->command[
1326 COMMAND_MASK & ftdi->command_next];
1327 command->header = 0x00 | (cPCImemwr & 0x0F);
1328 command->length = 0x04;
1329 command->address = addressofs;
1330 command->width = 0x00 | (width & 0x0F);
1331 command->follows = 4;
1332 command->value = data;
1333 command->buffer = &command->value;
1334 ftdi->command_next += 1;
1335 ftdi_elan_kick_command_queue(ftdi);
1336 mutex_unlock(&ftdi->u132_lock);
1339 mutex_unlock(&ftdi->u132_lock);
1346 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1349 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1350 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1354 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1355 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1357 wait:if (ftdi->disconnected > 0) {
1362 mutex_lock(&ftdi->u132_lock);
1363 command_size = ftdi->command_next - ftdi->command_head;
1364 respond_size = ftdi->respond_next - ftdi->respond_head;
1365 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1367 struct u132_command *command = &ftdi->command[
1368 COMMAND_MASK & ftdi->command_next];
1369 struct u132_respond *respond = &ftdi->respond[
1370 RESPOND_MASK & ftdi->respond_next];
1371 int result = -ENODEV;
1372 respond->result = &result;
1373 respond->header = command->header = 0x00 | cPCIu132rd;
1374 command->length = 0x04;
1375 respond->address = command->address = cU132cmd_status;
1376 command->width = 0x00;
1377 command->follows = 0;
1379 command->buffer = NULL;
1380 respond->value = data;
1381 init_completion(&respond->wait_completion);
1382 ftdi->command_next += 1;
1383 ftdi->respond_next += 1;
1384 ftdi_elan_kick_command_queue(ftdi);
1385 mutex_unlock(&ftdi->u132_lock);
1386 wait_for_completion(&respond->wait_completion);
1389 mutex_unlock(&ftdi->u132_lock);
1396 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1397 u8 width, u32 *data)
1399 u8 addressofs = config_offset / 4;
1400 wait:if (ftdi->disconnected > 0) {
1405 mutex_lock(&ftdi->u132_lock);
1406 command_size = ftdi->command_next - ftdi->command_head;
1407 respond_size = ftdi->respond_next - ftdi->respond_head;
1408 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1410 struct u132_command *command = &ftdi->command[
1411 COMMAND_MASK & ftdi->command_next];
1412 struct u132_respond *respond = &ftdi->respond[
1413 RESPOND_MASK & ftdi->respond_next];
1414 int result = -ENODEV;
1415 respond->result = &result;
1416 respond->header = command->header = 0x00 | (cPCIcfgrd &
1418 command->length = 0x04;
1419 respond->address = command->address = addressofs;
1420 command->width = 0x00 | (width & 0x0F);
1421 command->follows = 0;
1423 command->buffer = NULL;
1424 respond->value = data;
1425 init_completion(&respond->wait_completion);
1426 ftdi->command_next += 1;
1427 ftdi->respond_next += 1;
1428 ftdi_elan_kick_command_queue(ftdi);
1429 mutex_unlock(&ftdi->u132_lock);
1430 wait_for_completion(&respond->wait_completion);
1433 mutex_unlock(&ftdi->u132_lock);
1440 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1441 u8 width, u32 *data)
1443 u8 addressofs = mem_offset / 4;
1444 wait:if (ftdi->disconnected > 0) {
1449 mutex_lock(&ftdi->u132_lock);
1450 command_size = ftdi->command_next - ftdi->command_head;
1451 respond_size = ftdi->respond_next - ftdi->respond_head;
1452 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1454 struct u132_command *command = &ftdi->command[
1455 COMMAND_MASK & ftdi->command_next];
1456 struct u132_respond *respond = &ftdi->respond[
1457 RESPOND_MASK & ftdi->respond_next];
1458 int result = -ENODEV;
1459 respond->result = &result;
1460 respond->header = command->header = 0x00 | (cPCImemrd &
1462 command->length = 0x04;
1463 respond->address = command->address = addressofs;
1464 command->width = 0x00 | (width & 0x0F);
1465 command->follows = 0;
1467 command->buffer = NULL;
1468 respond->value = data;
1469 init_completion(&respond->wait_completion);
1470 ftdi->command_next += 1;
1471 ftdi->respond_next += 1;
1472 ftdi_elan_kick_command_queue(ftdi);
1473 mutex_unlock(&ftdi->u132_lock);
1474 wait_for_completion(&respond->wait_completion);
1477 mutex_unlock(&ftdi->u132_lock);
1484 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1485 u8 width, u32 *data)
1487 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1488 if (ftdi->initialized == 0) {
1491 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1495 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1496 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1497 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1498 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1499 int toggle_bits, int error_count, int condition_code, int repeat_number,
1500 int halted, int skipped, int actual, int non_null))
1502 u8 ed = ed_number - 1;
1503 wait:if (ftdi->disconnected > 0) {
1505 } else if (ftdi->initialized == 0) {
1509 mutex_lock(&ftdi->u132_lock);
1510 command_size = ftdi->command_next - ftdi->command_head;
1511 if (command_size < COMMAND_SIZE) {
1512 struct u132_target *target = &ftdi->target[ed];
1513 struct u132_command *command = &ftdi->command[
1514 COMMAND_MASK & ftdi->command_next];
1515 command->header = 0x80 | (ed << 5);
1516 command->length = 0x8007;
1517 command->address = (toggle_bits << 6) | (ep_number << 2)
1519 command->width = usb_maxpacket(urb->dev, urb->pipe,
1520 usb_pipeout(urb->pipe));
1521 command->follows = 8;
1523 command->buffer = urb->setup_packet;
1524 target->callback = callback;
1525 target->endp = endp;
1528 ftdi->command_next += 1;
1529 ftdi_elan_kick_command_queue(ftdi);
1530 mutex_unlock(&ftdi->u132_lock);
1533 mutex_unlock(&ftdi->u132_lock);
1540 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1541 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1542 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1543 int toggle_bits, int error_count, int condition_code, int repeat_number,
1544 int halted, int skipped, int actual, int non_null))
1546 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1547 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1548 ep_number, toggle_bits, callback);
1552 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1553 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1554 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1555 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1556 int toggle_bits, int error_count, int condition_code, int repeat_number,
1557 int halted, int skipped, int actual, int non_null))
1559 u8 ed = ed_number - 1;
1560 wait:if (ftdi->disconnected > 0) {
1562 } else if (ftdi->initialized == 0) {
1566 mutex_lock(&ftdi->u132_lock);
1567 command_size = ftdi->command_next - ftdi->command_head;
1568 if (command_size < COMMAND_SIZE) {
1569 struct u132_target *target = &ftdi->target[ed];
1570 struct u132_command *command = &ftdi->command[
1571 COMMAND_MASK & ftdi->command_next];
1572 u32 remaining_length = urb->transfer_buffer_length -
1574 command->header = 0x82 | (ed << 5);
1575 if (remaining_length == 0) {
1576 command->length = 0x0000;
1577 } else if (remaining_length > 1024) {
1578 command->length = 0x8000 | 1023;
1580 command->length = 0x8000 | (remaining_length -
1582 command->address = (toggle_bits << 6) | (ep_number << 2)
1584 command->width = usb_maxpacket(urb->dev, urb->pipe,
1585 usb_pipeout(urb->pipe));
1586 command->follows = 0;
1588 command->buffer = NULL;
1589 target->callback = callback;
1590 target->endp = endp;
1593 ftdi->command_next += 1;
1594 ftdi_elan_kick_command_queue(ftdi);
1595 mutex_unlock(&ftdi->u132_lock);
1598 mutex_unlock(&ftdi->u132_lock);
1605 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1606 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1607 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1608 int toggle_bits, int error_count, int condition_code, int repeat_number,
1609 int halted, int skipped, int actual, int non_null))
1611 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1612 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1613 ep_number, toggle_bits, callback);
1617 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1618 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1619 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1620 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1621 int toggle_bits, int error_count, int condition_code, int repeat_number,
1622 int halted, int skipped, int actual, int non_null))
1624 u8 ed = ed_number - 1;
1625 wait:if (ftdi->disconnected > 0) {
1627 } else if (ftdi->initialized == 0) {
1631 mutex_lock(&ftdi->u132_lock);
1632 command_size = ftdi->command_next - ftdi->command_head;
1633 if (command_size < COMMAND_SIZE) {
1634 struct u132_target *target = &ftdi->target[ed];
1635 struct u132_command *command = &ftdi->command[
1636 COMMAND_MASK & ftdi->command_next];
1637 command->header = 0x81 | (ed << 5);
1638 command->length = 0x0000;
1639 command->address = (toggle_bits << 6) | (ep_number << 2)
1641 command->width = usb_maxpacket(urb->dev, urb->pipe,
1642 usb_pipeout(urb->pipe));
1643 command->follows = 0;
1645 command->buffer = NULL;
1646 target->callback = callback;
1647 target->endp = endp;
1650 ftdi->command_next += 1;
1651 ftdi_elan_kick_command_queue(ftdi);
1652 mutex_unlock(&ftdi->u132_lock);
1655 mutex_unlock(&ftdi->u132_lock);
1662 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1663 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1664 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1665 int toggle_bits, int error_count, int condition_code, int repeat_number,
1666 int halted, int skipped, int actual, int non_null))
1668 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1669 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1670 ep_number, toggle_bits, callback);
1674 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1675 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1676 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1677 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1678 int toggle_bits, int error_count, int condition_code, int repeat_number,
1679 int halted, int skipped, int actual, int non_null))
1681 u8 ed = ed_number - 1;
1682 wait:if (ftdi->disconnected > 0) {
1684 } else if (ftdi->initialized == 0) {
1688 mutex_lock(&ftdi->u132_lock);
1689 command_size = ftdi->command_next - ftdi->command_head;
1690 if (command_size < COMMAND_SIZE) {
1694 char data[30 *3 + 4];
1696 int m = (sizeof(data) - 1) / 3;
1698 struct u132_target *target = &ftdi->target[ed];
1699 struct u132_command *command = &ftdi->command[
1700 COMMAND_MASK & ftdi->command_next];
1701 command->header = 0x81 | (ed << 5);
1702 command->address = (toggle_bits << 6) | (ep_number << 2)
1704 command->width = usb_maxpacket(urb->dev, urb->pipe,
1705 usb_pipeout(urb->pipe));
1706 command->follows = min_t(u32, 1024,
1707 urb->transfer_buffer_length -
1708 urb->actual_length);
1710 command->buffer = urb->transfer_buffer +
1712 command->length = 0x8000 | (command->follows - 1);
1713 b = command->buffer;
1714 urb_size = command->follows;
1716 while (urb_size-- > 0) {
1718 } else if (i++ < m) {
1719 int w = sprintf(d, " %02X", *b++);
1723 d += sprintf(d, " ..");
1725 target->callback = callback;
1726 target->endp = endp;
1729 ftdi->command_next += 1;
1730 ftdi_elan_kick_command_queue(ftdi);
1731 mutex_unlock(&ftdi->u132_lock);
1734 mutex_unlock(&ftdi->u132_lock);
1741 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1742 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1743 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1744 int toggle_bits, int error_count, int condition_code, int repeat_number,
1745 int halted, int skipped, int actual, int non_null))
1747 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1748 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1749 ep_number, toggle_bits, callback);
1753 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1754 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1755 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1756 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1757 int toggle_bits, int error_count, int condition_code, int repeat_number,
1758 int halted, int skipped, int actual, int non_null))
1760 u8 ed = ed_number - 1;
1761 wait:if (ftdi->disconnected > 0) {
1763 } else if (ftdi->initialized == 0) {
1767 mutex_lock(&ftdi->u132_lock);
1768 command_size = ftdi->command_next - ftdi->command_head;
1769 if (command_size < COMMAND_SIZE) {
1770 u32 remaining_length = urb->transfer_buffer_length -
1772 struct u132_target *target = &ftdi->target[ed];
1773 struct u132_command *command = &ftdi->command[
1774 COMMAND_MASK & ftdi->command_next];
1775 command->header = 0x83 | (ed << 5);
1776 if (remaining_length == 0) {
1777 command->length = 0x0000;
1778 } else if (remaining_length > 1024) {
1779 command->length = 0x8000 | 1023;
1781 command->length = 0x8000 | (remaining_length -
1783 command->address = (toggle_bits << 6) | (ep_number << 2)
1785 command->width = usb_maxpacket(urb->dev, urb->pipe,
1786 usb_pipeout(urb->pipe));
1787 command->follows = 0;
1789 command->buffer = NULL;
1790 target->callback = callback;
1791 target->endp = endp;
1794 ftdi->command_next += 1;
1795 ftdi_elan_kick_command_queue(ftdi);
1796 mutex_unlock(&ftdi->u132_lock);
1799 mutex_unlock(&ftdi->u132_lock);
1806 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1807 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1808 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1809 int toggle_bits, int error_count, int condition_code, int repeat_number,
1810 int halted, int skipped, int actual, int non_null))
1812 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1813 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1814 ep_number, toggle_bits, callback);
1818 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1819 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1822 u8 ed = ed_number - 1;
1823 if (ftdi->disconnected > 0) {
1825 } else if (ftdi->initialized == 0) {
1828 struct u132_target *target = &ftdi->target[ed];
1829 mutex_lock(&ftdi->u132_lock);
1830 if (target->abandoning > 0) {
1831 mutex_unlock(&ftdi->u132_lock);
1834 target->abandoning = 1;
1835 wait_1:if (target->active == 1) {
1836 int command_size = ftdi->command_next -
1838 if (command_size < COMMAND_SIZE) {
1839 struct u132_command *command =
1840 &ftdi->command[COMMAND_MASK &
1841 ftdi->command_next];
1842 command->header = 0x80 | (ed << 5) |
1844 command->length = 0x00;
1845 command->address = 0x00;
1846 command->width = 0x00;
1847 command->follows = 0;
1849 command->buffer = &command->value;
1850 ftdi->command_next += 1;
1851 ftdi_elan_kick_command_queue(ftdi);
1853 mutex_unlock(&ftdi->u132_lock);
1855 mutex_lock(&ftdi->u132_lock);
1859 mutex_unlock(&ftdi->u132_lock);
1865 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1868 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1869 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1873 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1874 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1876 int retry_on_empty = 10;
1877 int retry_on_timeout = 5;
1878 int retry_on_status = 20;
1880 int packet_bytes = 0;
1881 int retval = usb_bulk_msg(ftdi->udev,
1882 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1883 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1884 &packet_bytes, 100);
1885 if (packet_bytes > 2) {
1886 char diag[30 *3 + 4];
1888 int m = (sizeof(diag) - 1) / 3;
1889 char *b = ftdi->bulk_in_buffer;
1892 while (packet_bytes-- > 0) {
1894 if (bytes_read < m) {
1895 d += sprintf(d, " %02X",
1897 } else if (bytes_read > m) {
1899 d += sprintf(d, " ..");
1904 } else if (packet_bytes > 1) {
1905 char s1 = ftdi->bulk_in_buffer[0];
1906 char s2 = ftdi->bulk_in_buffer[1];
1907 if (s1 == 0x31 && s2 == 0x60) {
1909 } else if (retry_on_status-- > 0) {
1912 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1916 } else if (packet_bytes > 0) {
1917 char b1 = ftdi->bulk_in_buffer[0];
1918 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
1919 "TDI = %02X\n", b1);
1920 if (retry_on_status-- > 0) {
1923 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1927 } else if (retval == -ETIMEDOUT) {
1928 if (retry_on_timeout-- > 0) {
1931 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
1935 } else if (retval == 0) {
1936 if (retry_on_empty-- > 0) {
1939 dev_err(&ftdi->udev->dev, "empty packet retry l"
1944 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1953 * send the long flush sequence
1956 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1963 urb = usb_alloc_urb(0, GFP_KERNEL);
1965 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
1969 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1971 dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
1978 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1979 ftdi->bulk_out_endpointAddr), buf, i,
1980 ftdi_elan_write_bulk_callback, ftdi);
1981 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1982 retval = usb_submit_urb(urb, GFP_KERNEL);
1984 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
1985 "flush sequence\n");
1986 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1996 * send the reset sequence
1999 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
2006 urb = usb_alloc_urb(0, GFP_KERNEL);
2008 dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
2012 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2014 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
2023 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2024 ftdi->bulk_out_endpointAddr), buf, i,
2025 ftdi_elan_write_bulk_callback, ftdi);
2026 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2027 retval = usb_submit_urb(urb, GFP_KERNEL);
2029 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2030 "reset sequence\n");
2031 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
2039 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2043 int retry_on_timeout = 5;
2044 int retry_on_empty = 10;
2046 retval = ftdi_elan_flush_input_fifo(ftdi);
2049 ftdi->bulk_in_left = 0;
2050 ftdi->bulk_in_last = -1;
2051 while (long_stop-- > 0) {
2054 retval = ftdi_elan_synchronize_flush(ftdi);
2057 retval = ftdi_elan_flush_input_fifo(ftdi);
2060 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2066 int packet_bytes = 0;
2067 retval = usb_bulk_msg(ftdi->udev,
2068 usb_rcvbulkpipe(ftdi->udev,
2069 ftdi->bulk_in_endpointAddr),
2070 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2071 &packet_bytes, 500);
2072 if (packet_bytes > 2) {
2073 char diag[30 *3 + 4];
2075 int m = (sizeof(diag) - 1) / 3;
2076 char *b = ftdi->bulk_in_buffer;
2078 unsigned char c = 0;
2080 while (packet_bytes-- > 0) {
2082 if (bytes_read < m) {
2083 d += sprintf(d, " %02X", c);
2084 } else if (bytes_read > m) {
2086 d += sprintf(d, " ..");
2095 } else if (read_stop-- > 0) {
2098 dev_err(&ftdi->udev->dev, "retr"
2099 "y limit reached\n");
2103 } else if (packet_bytes > 1) {
2104 unsigned char s1 = ftdi->bulk_in_buffer[0];
2105 unsigned char s2 = ftdi->bulk_in_buffer[1];
2106 if (s1 == 0x31 && s2 == 0x00) {
2107 if (read_stuck-- > 0) {
2111 } else if (s1 == 0x31 && s2 == 0x60) {
2112 if (read_stop-- > 0) {
2115 dev_err(&ftdi->udev->dev, "retr"
2116 "y limit reached\n");
2120 if (read_stop-- > 0) {
2123 dev_err(&ftdi->udev->dev, "retr"
2124 "y limit reached\n");
2128 } else if (packet_bytes > 0) {
2129 if (read_stop-- > 0) {
2132 dev_err(&ftdi->udev->dev, "retry limit "
2136 } else if (retval == -ETIMEDOUT) {
2137 if (retry_on_timeout-- > 0) {
2140 dev_err(&ftdi->udev->dev, "TIMED OUT re"
2141 "try limit reached\n");
2144 } else if (retval == 0) {
2145 if (retry_on_empty-- > 0) {
2148 dev_err(&ftdi->udev->dev, "empty packet"
2149 " retry limit reached\n");
2154 dev_err(&ftdi->udev->dev, "error = %d\n",
2156 if (read_stop-- > 0) {
2159 dev_err(&ftdi->udev->dev, "retry limit "
2166 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2170 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2172 int retry_on_empty = 10;
2173 int retry_on_timeout = 5;
2174 int retry_on_status = 50;
2176 int packet_bytes = 0;
2177 int retval = usb_bulk_msg(ftdi->udev,
2178 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2179 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2180 &packet_bytes, 1000);
2181 if (packet_bytes > 2) {
2182 char diag[30 *3 + 4];
2184 int m = (sizeof(diag) - 1) / 3;
2185 char *b = ftdi->bulk_in_buffer;
2188 while (packet_bytes-- > 0) {
2190 if (bytes_read < m) {
2191 d += sprintf(d, " %02X",
2193 } else if (bytes_read > m) {
2195 d += sprintf(d, " ..");
2200 } else if (packet_bytes > 1) {
2201 char s1 = ftdi->bulk_in_buffer[0];
2202 char s2 = ftdi->bulk_in_buffer[1];
2203 if (s1 == 0x31 && s2 == 0x60) {
2205 } else if (retry_on_status-- > 0) {
2210 } else if (packet_bytes > 0) {
2211 char b1 = ftdi->bulk_in_buffer[0];
2212 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
2213 "TDI = %02X\n", b1);
2214 if (retry_on_status-- > 0) {
2218 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
2222 } else if (retval == -ETIMEDOUT) {
2223 if (retry_on_timeout-- > 0) {
2226 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
2230 } else if (retval == 0) {
2231 if (retry_on_empty-- > 0) {
2234 dev_err(&ftdi->udev->dev, "empty packet retry l"
2239 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2246 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2248 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2251 if (ftdi->controlreg & 0x00400000) {
2252 if (ftdi->card_ejected) {
2254 ftdi->card_ejected = 1;
2255 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
2256 "%08X\n", ftdi->controlreg);
2260 u8 fn = ftdi->function - 1;
2261 int activePCIfn = fn << 8;
2266 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2270 pciVID = pcidata & 0xFFFF;
2271 pciPID = (pcidata >> 16) & 0xFFFF;
2272 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2273 ftdi->platform_data.device) {
2276 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X devi"
2277 "ce=%04X pciPID=%04X\n",
2278 ftdi->platform_data.vendor, pciVID,
2279 ftdi->platform_data.device, pciPID);
2286 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2287 offsetof(struct ohci_regs, member), 0, data);
2288 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2289 offsetof(struct ohci_regs, member), 0, data);
2291 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2292 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2294 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2308 int mask = OHCI_INTR_INIT;
2310 int reset_timeout = 30; /* ... allow extra time */
2312 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2315 retval = ftdi_read_pcimem(ftdi, control, &control);
2318 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2321 num_ports = rh_a & RH_A_NDP;
2322 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2325 hc_fminterval &= 0x3fff;
2326 if (hc_fminterval != FI) {
2328 hc_fminterval |= FSMP(hc_fminterval) << 16;
2329 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2332 switch (hc_control & OHCI_CTRL_HCFS) {
2336 case OHCI_USB_SUSPEND:
2337 case OHCI_USB_RESUME:
2338 hc_control &= OHCI_CTRL_RWC;
2339 hc_control |= OHCI_USB_RESUME;
2343 hc_control &= OHCI_CTRL_RWC;
2344 hc_control |= OHCI_USB_RESET;
2348 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2351 retval = ftdi_read_pcimem(ftdi, control, &control);
2355 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2358 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2359 for (temp = 0; temp < num_ports; temp++) {
2360 retval = ftdi_write_pcimem(ftdi,
2361 roothub.portstatus[temp], RH_PS_LSDA);
2366 retval = ftdi_read_pcimem(ftdi, control, &control);
2369 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2372 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2376 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2379 if (0 != (status & OHCI_HCR)) {
2380 if (--reset_timeout == 0) {
2381 dev_err(&ftdi->udev->dev, "USB HC reset timed o"
2390 if (quirk & OHCI_QUIRK_INITRESET) {
2391 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2394 retval = ftdi_read_pcimem(ftdi, control, &control);
2398 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2401 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2404 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2407 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2410 retval = ftdi_write_pcimem(ftdi, fminterval,
2411 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2414 retval = ftdi_write_pcimem(ftdi, periodicstart,
2415 ((9 *hc_fminterval) / 10) & 0x3fff);
2418 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2421 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2424 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2425 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2426 quirk |= OHCI_QUIRK_INITRESET;
2429 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2430 fminterval, periodicstart);
2431 } /* start controller operations */
2432 hc_control &= OHCI_CTRL_RWC;
2433 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2434 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2437 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2440 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2443 retval = ftdi_read_pcimem(ftdi, control, &control);
2446 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2449 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2452 retval = ftdi_write_pcimem(ftdi, intrdisable,
2453 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2454 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2457 return retval; /* handle root hub init quirks ... */
2458 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2461 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2462 if (quirk & OHCI_QUIRK_SUPERIO) {
2463 roothub_a |= RH_A_NOCP;
2464 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2465 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2468 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2469 roothub_a |= RH_A_NPS;
2470 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2474 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2477 retval = ftdi_write_pcimem(ftdi, roothub.b,
2478 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2481 retval = ftdi_read_pcimem(ftdi, control, &control);
2484 mdelay((roothub_a >> 23) & 0x1fe);
2485 for (temp = 0; temp < num_ports; temp++) {
2487 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2497 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2503 int activePCIfn = fn << 8;
2504 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2508 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2512 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2516 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2520 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2525 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2529 latence_timer &= 0xFFFF00FF;
2530 latence_timer |= 0x00001600;
2531 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2535 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2540 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2544 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2548 for (reg = 0; reg <= 0x54; reg += 4) {
2549 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2556 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2562 int activePCIfn = fn << 8;
2563 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2567 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2571 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2575 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2579 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2584 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2588 latence_timer &= 0xFFFF00FF;
2589 latence_timer |= 0x00001600;
2590 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2594 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2599 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2603 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2610 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2614 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2617 result = ftdi_elan_check_controller(ftdi, quirk);
2618 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2624 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2629 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2632 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2636 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2639 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2642 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2645 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2648 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2652 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2655 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2658 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2661 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2664 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2668 sensebits = (controlreg >> 16) & 0x000F;
2669 if (0x0D == sensebits)
2675 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2681 int activePCIfn = 0;
2682 int max_devices = 0;
2683 int controllers = 0;
2684 int unrecognized = 0;
2686 for (fn = 0; (fn < 4); fn++) {
2690 activePCIfn = fn << 8;
2691 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2695 pciVID = pcidata & 0xFFFF;
2696 pciPID = (pcidata >> 16) & 0xFFFF;
2697 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2698 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2700 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2702 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2704 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2705 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2707 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2709 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2711 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2712 devices = ftdi_elan_found_controller(ftdi, fn,
2715 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2716 devices = ftdi_elan_found_controller(ftdi, fn,
2717 OHCI_QUIRK_ZFMICRO);
2719 } else if (0 == pcidata) {
2722 if (devices > max_devices) {
2723 max_devices = devices;
2724 ftdi->function = fn + 1;
2725 ftdi->platform_data.vendor = pciVID;
2726 ftdi->platform_data.device = pciPID;
2729 if (ftdi->function > 0) {
2730 UxxxStatus = ftdi_elan_setup_controller(ftdi,
2731 ftdi->function - 1);
2735 } else if (controllers > 0) {
2737 } else if (unrecognized > 0) {
2740 ftdi->enumerated = 0;
2747 * we use only the first bulk-in and bulk-out endpoints
2749 static int ftdi_elan_probe(struct usb_interface *interface,
2750 const struct usb_device_id *id)
2752 struct usb_host_interface *iface_desc;
2753 struct usb_endpoint_descriptor *endpoint;
2756 int retval = -ENOMEM;
2757 struct usb_ftdi *ftdi;
2759 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2761 printk(KERN_ERR "Out of memory\n");
2765 mutex_lock(&ftdi_module_lock);
2766 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2767 ftdi->sequence_num = ++ftdi_instances;
2768 mutex_unlock(&ftdi_module_lock);
2769 ftdi_elan_init_kref(ftdi);
2770 sema_init(&ftdi->sw_lock, 1);
2771 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2772 ftdi->interface = interface;
2773 mutex_init(&ftdi->u132_lock);
2775 iface_desc = interface->cur_altsetting;
2776 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2777 endpoint = &iface_desc->endpoint[i].desc;
2778 if (!ftdi->bulk_in_endpointAddr &&
2779 usb_endpoint_is_bulk_in(endpoint)) {
2780 buffer_size = usb_endpoint_maxp(endpoint);
2781 ftdi->bulk_in_size = buffer_size;
2782 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2783 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2784 if (!ftdi->bulk_in_buffer) {
2785 dev_err(&ftdi->udev->dev, "Could not allocate b"
2791 if (!ftdi->bulk_out_endpointAddr &&
2792 usb_endpoint_is_bulk_out(endpoint)) {
2793 ftdi->bulk_out_endpointAddr =
2794 endpoint->bEndpointAddress;
2797 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2798 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk"
2799 "-out endpoints\n");
2803 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2804 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2805 ftdi->bulk_out_endpointAddr);
2806 usb_set_intfdata(interface, ftdi);
2807 if (iface_desc->desc.bInterfaceNumber == 0 &&
2808 ftdi->bulk_in_endpointAddr == 0x81 &&
2809 ftdi->bulk_out_endpointAddr == 0x02) {
2810 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2812 dev_err(&ftdi->udev->dev, "Not able to get a minor for "
2814 usb_set_intfdata(interface, NULL);
2818 ftdi->class = &ftdi_elan_jtag_class;
2819 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface "
2820 "%d now attached to ftdi%d\n", ftdi,
2821 iface_desc->desc.bInterfaceNumber,
2825 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2826 ftdi->bulk_in_endpointAddr == 0x83 &&
2827 ftdi->bulk_out_endpointAddr == 0x04) {
2829 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now a"
2830 "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
2831 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2832 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2833 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2834 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2837 dev_err(&ftdi->udev->dev,
2838 "Could not find ELAN's U132 device\n");
2843 ftdi_elan_put_kref(ftdi);
2848 static void ftdi_elan_disconnect(struct usb_interface *interface)
2850 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2851 ftdi->disconnected += 1;
2853 int minor = interface->minor;
2854 struct usb_class_driver *class = ftdi->class;
2855 usb_set_intfdata(interface, NULL);
2856 usb_deregister_dev(interface, class);
2857 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on min"
2858 "or %d now disconnected\n", minor);
2860 ftdi_status_cancel_work(ftdi);
2861 ftdi_command_cancel_work(ftdi);
2862 ftdi_response_cancel_work(ftdi);
2863 ftdi_elan_abandon_completions(ftdi);
2864 ftdi_elan_abandon_targets(ftdi);
2865 if (ftdi->registered) {
2866 platform_device_unregister(&ftdi->platform_dev);
2867 ftdi->synchronized = 0;
2868 ftdi->enumerated = 0;
2869 ftdi->initialized = 0;
2870 ftdi->registered = 0;
2872 flush_workqueue(status_queue);
2873 flush_workqueue(command_queue);
2874 flush_workqueue(respond_queue);
2875 ftdi->disconnected += 1;
2876 usb_set_intfdata(interface, NULL);
2877 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller inter"
2878 "face now disconnected\n");
2880 ftdi_elan_put_kref(ftdi);
2883 static struct usb_driver ftdi_elan_driver = {
2884 .name = "ftdi-elan",
2885 .probe = ftdi_elan_probe,
2886 .disconnect = ftdi_elan_disconnect,
2887 .id_table = ftdi_elan_table,
2889 static int __init ftdi_elan_init(void)
2892 printk(KERN_INFO "driver %s\n", ftdi_elan_driver.name);
2893 mutex_init(&ftdi_module_lock);
2894 INIT_LIST_HEAD(&ftdi_static_list);
2895 status_queue = create_singlethread_workqueue("ftdi-status-control");
2897 goto err_status_queue;
2898 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2900 goto err_command_queue;
2901 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2903 goto err_respond_queue;
2904 result = usb_register(&ftdi_elan_driver);
2906 destroy_workqueue(status_queue);
2907 destroy_workqueue(command_queue);
2908 destroy_workqueue(respond_queue);
2909 printk(KERN_ERR "usb_register failed. Error number %d\n",
2915 destroy_workqueue(command_queue);
2917 destroy_workqueue(status_queue);
2919 printk(KERN_ERR "%s couldn't create workqueue\n", ftdi_elan_driver.name);
2923 static void __exit ftdi_elan_exit(void)
2925 struct usb_ftdi *ftdi;
2926 struct usb_ftdi *temp;
2927 usb_deregister(&ftdi_elan_driver);
2928 printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2929 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2930 ftdi_status_cancel_work(ftdi);
2931 ftdi_command_cancel_work(ftdi);
2932 ftdi_response_cancel_work(ftdi);
2933 } flush_workqueue(status_queue);
2934 destroy_workqueue(status_queue);
2935 status_queue = NULL;
2936 flush_workqueue(command_queue);
2937 destroy_workqueue(command_queue);
2938 command_queue = NULL;
2939 flush_workqueue(respond_queue);
2940 destroy_workqueue(respond_queue);
2941 respond_queue = NULL;
2945 module_init(ftdi_elan_init);
2946 module_exit(ftdi_elan_exit);