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.
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/kernel.h>
42 #include <linux/errno.h>
43 #include <linux/init.h>
44 #include <linux/list.h>
45 #include <linux/ioctl.h>
46 #include <linux/pci_ids.h>
47 #include <linux/slab.h>
48 #include <linux/module.h>
49 #include <linux/kref.h>
50 #include <linux/mutex.h>
51 #include <asm/uaccess.h>
52 #include <linux/usb.h>
53 #include <linux/workqueue.h>
54 #include <linux/platform_device.h>
55 MODULE_AUTHOR("Tony Olech");
56 MODULE_DESCRIPTION("FTDI ELAN driver");
57 MODULE_LICENSE("GPL");
58 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
59 static bool distrust_firmware = 1;
60 module_param(distrust_firmware, bool, 0);
61 MODULE_PARM_DESC(distrust_firmware,
62 "true to distrust firmware power/overcurrent setup");
63 extern struct platform_driver u132_platform_driver;
64 static struct workqueue_struct *status_queue;
65 static struct workqueue_struct *command_queue;
66 static struct workqueue_struct *respond_queue;
68 * ftdi_module_lock exists to protect access to global variables
71 static struct mutex ftdi_module_lock;
72 static int ftdi_instances = 0;
73 static struct list_head ftdi_static_list;
75 * end of the global variables protected by ftdi_module_lock
79 #include <linux/usb/hcd.h>
81 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
82 * If you're going to try stuff like this, you need to split
83 * out shareable stuff (register declarations?) into its own
84 * file, maybe name <linux/usb/ohci.h>
87 #include "../host/ohci.h"
88 /* Define these values to match your devices*/
89 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
90 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
91 /* table of devices that work with this driver*/
92 static const struct usb_device_id ftdi_elan_table[] = {
93 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
94 { /* Terminating entry */ }
97 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
98 /* only the jtag(firmware upgrade device) interface requires
99 * a device file and corresponding minor number, but the
100 * interface is created unconditionally - I suppose it could
101 * be configured or not according to a module parameter.
102 * But since we(now) require one interface per device,
103 * and since it unlikely that a normal installation would
104 * require more than a couple of elan-ftdi devices, 8 seems
105 * like a reasonable limit to have here, and if someone
106 * really requires more than 8 devices, then they can frig the
109 #define USB_FTDI_ELAN_MINOR_BASE 192
110 #define COMMAND_BITS 5
111 #define COMMAND_SIZE (1<<COMMAND_BITS)
112 #define COMMAND_MASK (COMMAND_SIZE-1)
113 struct u132_command {
122 #define RESPOND_BITS 5
123 #define RESPOND_SIZE (1<<RESPOND_BITS)
124 #define RESPOND_MASK (RESPOND_SIZE-1)
125 struct u132_respond {
130 struct completion wait_completion;
145 void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
146 int toggle_bits, int error_count, int condition_code,
147 int repeat_number, int halted, int skipped, int actual,
150 /* Structure to hold all of our device specific stuff*/
152 struct list_head ftdi_list;
153 struct mutex u132_lock;
156 struct u132_command command[COMMAND_SIZE];
159 struct u132_respond respond[RESPOND_SIZE];
160 struct u132_target target[4];
161 char device_name[16];
162 unsigned synchronized:1;
163 unsigned enumerated:1;
164 unsigned registered:1;
165 unsigned initialized:1;
166 unsigned card_ejected:1;
172 int status_queue_delay;
173 struct semaphore sw_lock;
174 struct usb_device *udev;
175 struct usb_interface *interface;
176 struct usb_class_driver *class;
177 struct delayed_work status_work;
178 struct delayed_work command_work;
179 struct delayed_work respond_work;
180 struct u132_platform_data platform_data;
181 struct resource resources[0];
182 struct platform_device platform_dev;
183 unsigned char *bulk_in_buffer;
187 __u8 bulk_in_endpointAddr;
188 __u8 bulk_out_endpointAddr;
191 u8 response[4 + 1024];
196 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
197 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
199 static struct usb_driver ftdi_elan_driver;
200 static void ftdi_elan_delete(struct kref *kref)
202 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
203 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
204 usb_put_dev(ftdi->udev);
205 ftdi->disconnected += 1;
206 mutex_lock(&ftdi_module_lock);
207 list_del_init(&ftdi->ftdi_list);
209 mutex_unlock(&ftdi_module_lock);
210 kfree(ftdi->bulk_in_buffer);
211 ftdi->bulk_in_buffer = NULL;
214 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
216 kref_put(&ftdi->kref, ftdi_elan_delete);
219 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
221 kref_get(&ftdi->kref);
224 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
226 kref_init(&ftdi->kref);
229 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
231 if (!queue_delayed_work(status_queue, &ftdi->status_work, delta))
232 kref_put(&ftdi->kref, ftdi_elan_delete);
235 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
237 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
238 kref_get(&ftdi->kref);
241 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
243 if (cancel_delayed_work(&ftdi->status_work))
244 kref_put(&ftdi->kref, ftdi_elan_delete);
247 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
249 if (!queue_delayed_work(command_queue, &ftdi->command_work, delta))
250 kref_put(&ftdi->kref, ftdi_elan_delete);
253 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
255 if (queue_delayed_work(command_queue, &ftdi->command_work, delta))
256 kref_get(&ftdi->kref);
259 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
261 if (cancel_delayed_work(&ftdi->command_work))
262 kref_put(&ftdi->kref, ftdi_elan_delete);
265 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
268 if (!queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
269 kref_put(&ftdi->kref, ftdi_elan_delete);
272 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
274 if (queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
275 kref_get(&ftdi->kref);
278 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
280 if (cancel_delayed_work(&ftdi->respond_work))
281 kref_put(&ftdi->kref, ftdi_elan_delete);
284 void ftdi_elan_gone_away(struct platform_device *pdev)
286 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
287 ftdi->gone_away += 1;
288 ftdi_elan_put_kref(ftdi);
292 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
293 static void ftdi_release_platform_dev(struct device *dev)
298 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
299 struct u132_target *target, u8 *buffer, int length);
300 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
301 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
302 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
303 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
304 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
305 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
306 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
307 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
308 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
309 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
312 if (ftdi->platform_dev.dev.parent)
314 ftdi_elan_get_kref(ftdi);
315 ftdi->platform_data.potpg = 100;
316 ftdi->platform_data.reset = NULL;
317 ftdi->platform_dev.id = ftdi->sequence_num;
318 ftdi->platform_dev.resource = ftdi->resources;
319 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
320 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
321 ftdi->platform_dev.dev.parent = NULL;
322 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
323 ftdi->platform_dev.dev.dma_mask = NULL;
324 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
325 ftdi->platform_dev.name = ftdi->device_name;
326 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
327 request_module("u132_hcd");
328 dev_info(&ftdi->udev->dev, "registering '%s'\n",
329 ftdi->platform_dev.name);
330 result = platform_device_register(&ftdi->platform_dev);
334 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
336 mutex_lock(&ftdi->u132_lock);
337 while (ftdi->respond_next > ftdi->respond_head) {
338 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
339 ftdi->respond_head++];
340 *respond->result = -ESHUTDOWN;
342 complete(&respond->wait_completion);
343 } mutex_unlock(&ftdi->u132_lock);
346 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
349 mutex_lock(&ftdi->u132_lock);
350 while (ed_number-- > 0) {
351 struct u132_target *target = &ftdi->target[ed_number];
352 if (target->active == 1) {
353 target->condition_code = TD_DEVNOTRESP;
354 mutex_unlock(&ftdi->u132_lock);
355 ftdi_elan_do_callback(ftdi, target, NULL, 0);
356 mutex_lock(&ftdi->u132_lock);
362 mutex_unlock(&ftdi->u132_lock);
365 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
368 mutex_lock(&ftdi->u132_lock);
369 while (ed_number-- > 0) {
370 struct u132_target *target = &ftdi->target[ed_number];
371 target->abandoning = 1;
372 wait_1:if (target->active == 1) {
373 int command_size = ftdi->command_next -
375 if (command_size < COMMAND_SIZE) {
376 struct u132_command *command = &ftdi->command[
377 COMMAND_MASK & ftdi->command_next];
378 command->header = 0x80 | (ed_number << 5) | 0x4;
379 command->length = 0x00;
380 command->address = 0x00;
381 command->width = 0x00;
382 command->follows = 0;
384 command->buffer = &command->value;
385 ftdi->command_next += 1;
386 ftdi_elan_kick_command_queue(ftdi);
388 mutex_unlock(&ftdi->u132_lock);
390 mutex_lock(&ftdi->u132_lock);
394 wait_2:if (target->active == 1) {
395 int command_size = ftdi->command_next -
397 if (command_size < COMMAND_SIZE) {
398 struct u132_command *command = &ftdi->command[
399 COMMAND_MASK & ftdi->command_next];
400 command->header = 0x90 | (ed_number << 5);
401 command->length = 0x00;
402 command->address = 0x00;
403 command->width = 0x00;
404 command->follows = 0;
406 command->buffer = &command->value;
407 ftdi->command_next += 1;
408 ftdi_elan_kick_command_queue(ftdi);
410 mutex_unlock(&ftdi->u132_lock);
412 mutex_lock(&ftdi->u132_lock);
420 mutex_unlock(&ftdi->u132_lock);
423 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
426 mutex_lock(&ftdi->u132_lock);
427 while (ed_number-- > 0) {
428 struct u132_target *target = &ftdi->target[ed_number];
429 target->abandoning = 1;
430 wait:if (target->active == 1) {
431 int command_size = ftdi->command_next -
433 if (command_size < COMMAND_SIZE) {
434 struct u132_command *command = &ftdi->command[
435 COMMAND_MASK & ftdi->command_next];
436 command->header = 0x80 | (ed_number << 5) | 0x4;
437 command->length = 0x00;
438 command->address = 0x00;
439 command->width = 0x00;
440 command->follows = 0;
442 command->buffer = &command->value;
443 ftdi->command_next += 1;
444 ftdi_elan_kick_command_queue(ftdi);
446 mutex_unlock(&ftdi->u132_lock);
448 mutex_lock(&ftdi->u132_lock);
456 mutex_unlock(&ftdi->u132_lock);
459 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
461 ftdi_command_queue_work(ftdi, 0);
464 static void ftdi_elan_command_work(struct work_struct *work)
466 struct usb_ftdi *ftdi =
467 container_of(work, struct usb_ftdi, command_work.work);
469 if (ftdi->disconnected > 0) {
470 ftdi_elan_put_kref(ftdi);
473 int retval = ftdi_elan_command_engine(ftdi);
474 if (retval == -ESHUTDOWN) {
475 ftdi->disconnected += 1;
476 } else if (retval == -ENODEV) {
477 ftdi->disconnected += 1;
479 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
480 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
485 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
487 ftdi_respond_queue_work(ftdi, 0);
490 static void ftdi_elan_respond_work(struct work_struct *work)
492 struct usb_ftdi *ftdi =
493 container_of(work, struct usb_ftdi, respond_work.work);
494 if (ftdi->disconnected > 0) {
495 ftdi_elan_put_kref(ftdi);
498 int retval = ftdi_elan_respond_engine(ftdi);
500 } else if (retval == -ESHUTDOWN) {
501 ftdi->disconnected += 1;
502 } else if (retval == -ENODEV) {
503 ftdi->disconnected += 1;
504 } else if (retval == -EILSEQ) {
505 ftdi->disconnected += 1;
507 ftdi->disconnected += 1;
508 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
510 if (ftdi->disconnected > 0) {
511 ftdi_elan_abandon_completions(ftdi);
512 ftdi_elan_abandon_targets(ftdi);
514 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
521 * the sw_lock is initially held and will be freed
522 * after the FTDI has been synchronized
525 static void ftdi_elan_status_work(struct work_struct *work)
527 struct usb_ftdi *ftdi =
528 container_of(work, struct usb_ftdi, status_work.work);
529 int work_delay_in_msec = 0;
530 if (ftdi->disconnected > 0) {
531 ftdi_elan_put_kref(ftdi);
533 } else if (ftdi->synchronized == 0) {
534 down(&ftdi->sw_lock);
535 if (ftdi_elan_synchronize(ftdi) == 0) {
536 ftdi->synchronized = 1;
537 ftdi_command_queue_work(ftdi, 1);
538 ftdi_respond_queue_work(ftdi, 1);
540 work_delay_in_msec = 100;
542 dev_err(&ftdi->udev->dev, "synchronize failed\n");
544 work_delay_in_msec = 10 *1000;
546 } else if (ftdi->stuck_status > 0) {
547 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
548 ftdi->stuck_status = 0;
549 ftdi->synchronized = 0;
550 } else if ((ftdi->stuck_status++ % 60) == 1) {
551 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
553 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
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;
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;
567 dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
568 work_delay_in_msec = 1 *1000;
570 } else if (ftdi->registered == 0) {
571 work_delay_in_msec = 10;
572 if (ftdi_elan_hcd_init(ftdi) == 0) {
573 ftdi->registered = 1;
575 dev_err(&ftdi->udev->dev, "register failed\n");
576 work_delay_in_msec = 250;
578 if (ftdi_elan_checkingPCI(ftdi) == 0) {
579 work_delay_in_msec = 250;
580 } else if (ftdi->controlreg & 0x00400000) {
581 if (ftdi->gone_away > 0) {
582 dev_err(&ftdi->udev->dev, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
583 ftdi->platform_dev.dev.parent,
584 &ftdi->platform_dev.dev);
585 platform_device_unregister(&ftdi->platform_dev);
586 ftdi->platform_dev.dev.parent = NULL;
587 ftdi->registered = 0;
588 ftdi->enumerated = 0;
589 ftdi->card_ejected = 0;
590 ftdi->initialized = 0;
593 ftdi_elan_flush_targets(ftdi);
594 work_delay_in_msec = 250;
596 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n");
597 ftdi_elan_cancel_targets(ftdi);
598 work_delay_in_msec = 500;
599 ftdi->enumerated = 0;
600 ftdi->initialized = 0;
603 if (ftdi->disconnected > 0) {
604 ftdi_elan_put_kref(ftdi);
607 ftdi_status_requeue_work(ftdi,
608 msecs_to_jiffies(work_delay_in_msec));
615 * file_operations for the jtag interface
617 * the usage count for the device is incremented on open()
618 * and decremented on release()
620 static int ftdi_elan_open(struct inode *inode, struct file *file)
623 struct usb_interface *interface;
625 subminor = iminor(inode);
626 interface = usb_find_interface(&ftdi_elan_driver, subminor);
629 pr_err("can't find device for minor %d\n", subminor);
632 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
636 if (down_interruptible(&ftdi->sw_lock)) {
639 ftdi_elan_get_kref(ftdi);
640 file->private_data = ftdi;
647 static int ftdi_elan_release(struct inode *inode, struct file *file)
649 struct usb_ftdi *ftdi = file->private_data;
652 up(&ftdi->sw_lock); /* decrement the count on our device */
653 ftdi_elan_put_kref(ftdi);
660 * blocking bulk reads are used to get data from the device
663 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
664 size_t count, loff_t *ppos)
666 char data[30 *3 + 4];
668 int m = (sizeof(data) - 1) / 3;
670 int retry_on_empty = 10;
671 int retry_on_timeout = 5;
672 struct usb_ftdi *ftdi = file->private_data;
673 if (ftdi->disconnected > 0) {
677 have:if (ftdi->bulk_in_left > 0) {
679 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
680 ftdi->bulk_in_left -= 1;
681 if (bytes_read < m) {
682 d += sprintf(d, " %02X", 0x000000FF & *p);
683 } else if (bytes_read > m) {
685 d += sprintf(d, " ..");
686 if (copy_to_user(buffer++, p, 1)) {
695 more:if (count > 0) {
696 int packet_bytes = 0;
697 int retval = usb_bulk_msg(ftdi->udev,
698 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
699 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
701 if (packet_bytes > 2) {
702 ftdi->bulk_in_left = packet_bytes - 2;
703 ftdi->bulk_in_last = 1;
705 } else if (retval == -ETIMEDOUT) {
706 if (retry_on_timeout-- > 0) {
708 } else if (bytes_read > 0) {
712 } else if (retval == 0) {
713 if (retry_on_empty-- > 0) {
723 static void ftdi_elan_write_bulk_callback(struct urb *urb)
725 struct usb_ftdi *ftdi = urb->context;
726 int status = urb->status;
728 if (status && !(status == -ENOENT || status == -ECONNRESET ||
729 status == -ESHUTDOWN)) {
730 dev_err(&ftdi->udev->dev,
731 "urb=%p write bulk status received: %d\n", urb, status);
733 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
734 urb->transfer_buffer, urb->transfer_dma);
737 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
738 char *buf, int command_size, int total_size)
742 int I = command_size;
743 int i = ftdi->command_head;
745 struct u132_command *command = &ftdi->command[COMMAND_MASK &
747 int F = command->follows;
748 u8 *f = command->buffer;
749 if (command->header & 0x80) {
750 ed_commands |= 1 << (0x3 & (command->header >> 5));
752 buf[b++] = command->header;
753 buf[b++] = (command->length >> 0) & 0x00FF;
754 buf[b++] = (command->length >> 8) & 0x00FF;
755 buf[b++] = command->address;
756 buf[b++] = command->width;
764 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
767 int I = command_size;
768 int i = ftdi->command_head;
770 struct u132_command *command = &ftdi->command[COMMAND_MASK &
772 total_size += 5 + command->follows;
776 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
783 int command_size = ftdi->command_next - ftdi->command_head;
784 if (command_size == 0)
786 total_size = ftdi_elan_total_command_size(ftdi, command_size);
787 urb = usb_alloc_urb(0, GFP_KERNEL);
789 dev_err(&ftdi->udev->dev, "could not get a urb to write %d commands totaling %d bytes to the Uxxx\n",
790 command_size, total_size);
793 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
796 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
797 command_size, total_size);
801 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
802 command_size, total_size);
803 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
804 ftdi->bulk_out_endpointAddr), buf, total_size,
805 ftdi_elan_write_bulk_callback, ftdi);
806 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
808 char diag[40 *3 + 4];
812 int s = (sizeof(diag) - 1) / 3;
814 while (s-- > 0 && m-- > 0) {
815 if (s > 0 || m == 0) {
816 d += sprintf(d, " %02X", *c++);
818 d += sprintf(d, " ..");
821 retval = usb_submit_urb(urb, GFP_KERNEL);
823 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
824 retval, urb, command_size, total_size);
825 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
829 usb_free_urb(urb); /* release our reference to this urb,
830 the USB core will eventually free it entirely */
831 ftdi->command_head += command_size;
832 ftdi_elan_kick_respond_queue(ftdi);
836 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
837 struct u132_target *target, u8 *buffer, int length)
839 struct urb *urb = target->urb;
840 int halted = target->halted;
841 int skipped = target->skipped;
842 int actual = target->actual;
843 int non_null = target->non_null;
844 int toggle_bits = target->toggle_bits;
845 int error_count = target->error_count;
846 int condition_code = target->condition_code;
847 int repeat_number = target->repeat_number;
848 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
849 int, int, int, int) = target->callback;
851 target->callback = NULL;
852 (*callback) (target->endp, urb, buffer, length, toggle_bits,
853 error_count, condition_code, repeat_number, halted, skipped,
857 static char *have_ed_set_response(struct usb_ftdi *ftdi,
858 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
861 int payload = (ed_length >> 0) & 0x07FF;
862 mutex_lock(&ftdi->u132_lock);
864 target->non_null = (ed_length >> 15) & 0x0001;
865 target->repeat_number = (ed_length >> 11) & 0x000F;
866 if (ed_type == 0x02) {
867 if (payload == 0 || target->abandoning > 0) {
868 target->abandoning = 0;
869 mutex_unlock(&ftdi->u132_lock);
870 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
875 return ftdi->response;
877 ftdi->expected = 4 + payload;
879 mutex_unlock(&ftdi->u132_lock);
882 } else if (ed_type == 0x03) {
883 if (payload == 0 || target->abandoning > 0) {
884 target->abandoning = 0;
885 mutex_unlock(&ftdi->u132_lock);
886 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
891 return ftdi->response;
893 ftdi->expected = 4 + payload;
895 mutex_unlock(&ftdi->u132_lock);
898 } else if (ed_type == 0x01) {
899 target->abandoning = 0;
900 mutex_unlock(&ftdi->u132_lock);
901 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
906 return ftdi->response;
908 target->abandoning = 0;
909 mutex_unlock(&ftdi->u132_lock);
910 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
915 return ftdi->response;
919 static char *have_ed_get_response(struct usb_ftdi *ftdi,
920 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
923 mutex_lock(&ftdi->u132_lock);
924 target->condition_code = TD_DEVNOTRESP;
925 target->actual = (ed_length >> 0) & 0x01FF;
926 target->non_null = (ed_length >> 15) & 0x0001;
927 target->repeat_number = (ed_length >> 11) & 0x000F;
928 mutex_unlock(&ftdi->u132_lock);
930 ftdi_elan_do_callback(ftdi, target, NULL, 0);
931 target->abandoning = 0;
935 return ftdi->response;
940 * The engine tries to empty the FTDI fifo
942 * all responses found in the fifo data are dispatched thus
943 * the response buffer can only ever hold a maximum sized
944 * response from the Uxxx.
947 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
949 u8 *b = ftdi->response + ftdi->received;
951 int retry_on_empty = 1;
952 int retry_on_timeout = 3;
953 int empty_packets = 0;
955 int packet_bytes = 0;
956 int retval = usb_bulk_msg(ftdi->udev,
957 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
958 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
960 char diag[30 *3 + 4];
962 int m = packet_bytes;
963 u8 *c = ftdi->bulk_in_buffer;
964 int s = (sizeof(diag) - 1) / 3;
966 while (s-- > 0 && m-- > 0) {
967 if (s > 0 || m == 0) {
968 d += sprintf(d, " %02X", *c++);
970 d += sprintf(d, " ..");
972 if (packet_bytes > 2) {
973 ftdi->bulk_in_left = packet_bytes - 2;
974 ftdi->bulk_in_last = 1;
976 } else if (retval == -ETIMEDOUT) {
977 if (retry_on_timeout-- > 0) {
978 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
979 packet_bytes, bytes_read, diag);
981 } else if (bytes_read > 0) {
982 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
986 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
987 packet_bytes, bytes_read, diag);
990 } else if (retval == -EILSEQ) {
991 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
992 retval, packet_bytes, bytes_read, diag);
995 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
996 retval, packet_bytes, bytes_read, diag);
998 } else if (packet_bytes == 2) {
999 unsigned char s0 = ftdi->bulk_in_buffer[0];
1000 unsigned char s1 = ftdi->bulk_in_buffer[1];
1002 if (s0 == 0x31 && s1 == 0x60) {
1003 if (retry_on_empty-- > 0) {
1007 } else if (s0 == 0x31 && s1 == 0x00) {
1008 if (retry_on_empty-- > 0) {
1013 if (retry_on_empty-- > 0) {
1018 } else if (packet_bytes == 1) {
1019 if (retry_on_empty-- > 0) {
1024 if (retry_on_empty-- > 0) {
1033 have:if (ftdi->bulk_in_left > 0) {
1034 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1036 ftdi->bulk_in_left -= 1;
1037 if (ftdi->received == 0 && c == 0xFF) {
1041 if (++ftdi->received < ftdi->expected) {
1043 } else if (ftdi->ed_found) {
1044 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1045 u16 ed_length = (ftdi->response[2] << 8) |
1047 struct u132_target *target = &ftdi->target[ed_number];
1048 int payload = (ed_length >> 0) & 0x07FF;
1049 char diag[30 *3 + 4];
1052 u8 *c = 4 + ftdi->response;
1053 int s = (sizeof(diag) - 1) / 3;
1055 while (s-- > 0 && m-- > 0) {
1056 if (s > 0 || m == 0) {
1057 d += sprintf(d, " %02X", *c++);
1059 d += sprintf(d, " ..");
1061 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1068 } else if (ftdi->expected == 8) {
1070 int respond_head = ftdi->respond_head++;
1071 struct u132_respond *respond = &ftdi->respond[
1072 RESPOND_MASK & respond_head];
1073 u32 data = ftdi->response[7];
1075 data |= ftdi->response[6];
1077 data |= ftdi->response[5];
1079 data |= ftdi->response[4];
1080 *respond->value = data;
1081 *respond->result = 0;
1082 complete(&respond->wait_completion);
1087 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1088 if (buscmd == 0x00) {
1089 } else if (buscmd == 0x02) {
1090 } else if (buscmd == 0x06) {
1091 } else if (buscmd == 0x0A) {
1093 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1097 if ((ftdi->response[0] & 0x80) == 0x00) {
1101 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1102 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1103 u16 ed_length = (ftdi->response[2] << 8) |
1105 struct u132_target *target = &ftdi->target[
1107 target->halted = (ftdi->response[0] >> 3) &
1109 target->skipped = (ftdi->response[0] >> 2) &
1111 target->toggle_bits = (ftdi->response[3] >> 6)
1113 target->error_count = (ftdi->response[3] >> 4)
1115 target->condition_code = (ftdi->response[
1117 if ((ftdi->response[0] & 0x10) == 0x00) {
1118 b = have_ed_set_response(ftdi, target,
1119 ed_length, ed_number, ed_type,
1123 b = have_ed_get_response(ftdi, target,
1124 ed_length, ed_number, ed_type,
1136 * create a urb, and a buffer for it, and copy the data to the urb
1139 static ssize_t ftdi_elan_write(struct file *file,
1140 const char __user *user_buffer, size_t count,
1146 struct usb_ftdi *ftdi = file->private_data;
1148 if (ftdi->disconnected > 0) {
1154 urb = usb_alloc_urb(0, GFP_KERNEL);
1159 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1160 &urb->transfer_dma);
1165 if (copy_from_user(buf, user_buffer, count)) {
1169 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1170 ftdi->bulk_out_endpointAddr), buf, count,
1171 ftdi_elan_write_bulk_callback, ftdi);
1172 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1173 retval = usb_submit_urb(urb, GFP_KERNEL);
1175 dev_err(&ftdi->udev->dev,
1176 "failed submitting write urb, error %d\n", retval);
1184 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1191 static const struct file_operations ftdi_elan_fops = {
1192 .owner = THIS_MODULE,
1193 .llseek = no_llseek,
1194 .read = ftdi_elan_read,
1195 .write = ftdi_elan_write,
1196 .open = ftdi_elan_open,
1197 .release = ftdi_elan_release,
1201 * usb class driver info in order to get a minor number from the usb core,
1202 * and to have the device registered with the driver core
1204 static struct usb_class_driver ftdi_elan_jtag_class = {
1205 .name = "ftdi-%d-jtag",
1206 .fops = &ftdi_elan_fops,
1207 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1211 * the following definitions are for the
1212 * ELAN FPGA state machgine processor that
1213 * lies on the other side of the FTDI chip
1215 #define cPCIu132rd 0x0
1216 #define cPCIu132wr 0x1
1217 #define cPCIiord 0x2
1218 #define cPCIiowr 0x3
1219 #define cPCImemrd 0x6
1220 #define cPCImemwr 0x7
1221 #define cPCIcfgrd 0xA
1222 #define cPCIcfgwr 0xB
1223 #define cPCInull 0xF
1224 #define cU132cmd_status 0x0
1225 #define cU132flash 0x1
1226 #define cPIDsetup 0x0
1229 #define cPIDinonce 0x3
1230 #define cCCnoerror 0x0
1232 #define cCCbitstuff 0x2
1233 #define cCCtoggle 0x3
1234 #define cCCstall 0x4
1235 #define cCCnoresp 0x5
1236 #define cCCbadpid1 0x6
1237 #define cCCbadpid2 0x7
1238 #define cCCdataoverrun 0x8
1239 #define cCCdataunderrun 0x9
1240 #define cCCbuffoverrun 0xC
1241 #define cCCbuffunderrun 0xD
1242 #define cCCnotaccessed 0xF
1243 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1245 wait:if (ftdi->disconnected > 0) {
1249 mutex_lock(&ftdi->u132_lock);
1250 command_size = ftdi->command_next - ftdi->command_head;
1251 if (command_size < COMMAND_SIZE) {
1252 struct u132_command *command = &ftdi->command[
1253 COMMAND_MASK & ftdi->command_next];
1254 command->header = 0x00 | cPCIu132wr;
1255 command->length = 0x04;
1256 command->address = 0x00;
1257 command->width = 0x00;
1258 command->follows = 4;
1259 command->value = data;
1260 command->buffer = &command->value;
1261 ftdi->command_next += 1;
1262 ftdi_elan_kick_command_queue(ftdi);
1263 mutex_unlock(&ftdi->u132_lock);
1266 mutex_unlock(&ftdi->u132_lock);
1273 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1276 u8 addressofs = config_offset / 4;
1277 wait:if (ftdi->disconnected > 0) {
1281 mutex_lock(&ftdi->u132_lock);
1282 command_size = ftdi->command_next - ftdi->command_head;
1283 if (command_size < COMMAND_SIZE) {
1284 struct u132_command *command = &ftdi->command[
1285 COMMAND_MASK & ftdi->command_next];
1286 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1287 command->length = 0x04;
1288 command->address = addressofs;
1289 command->width = 0x00 | (width & 0x0F);
1290 command->follows = 4;
1291 command->value = data;
1292 command->buffer = &command->value;
1293 ftdi->command_next += 1;
1294 ftdi_elan_kick_command_queue(ftdi);
1295 mutex_unlock(&ftdi->u132_lock);
1298 mutex_unlock(&ftdi->u132_lock);
1305 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1308 u8 addressofs = mem_offset / 4;
1309 wait:if (ftdi->disconnected > 0) {
1313 mutex_lock(&ftdi->u132_lock);
1314 command_size = ftdi->command_next - ftdi->command_head;
1315 if (command_size < COMMAND_SIZE) {
1316 struct u132_command *command = &ftdi->command[
1317 COMMAND_MASK & ftdi->command_next];
1318 command->header = 0x00 | (cPCImemwr & 0x0F);
1319 command->length = 0x04;
1320 command->address = addressofs;
1321 command->width = 0x00 | (width & 0x0F);
1322 command->follows = 4;
1323 command->value = data;
1324 command->buffer = &command->value;
1325 ftdi->command_next += 1;
1326 ftdi_elan_kick_command_queue(ftdi);
1327 mutex_unlock(&ftdi->u132_lock);
1330 mutex_unlock(&ftdi->u132_lock);
1337 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1340 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1341 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1345 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1346 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1348 wait:if (ftdi->disconnected > 0) {
1353 mutex_lock(&ftdi->u132_lock);
1354 command_size = ftdi->command_next - ftdi->command_head;
1355 respond_size = ftdi->respond_next - ftdi->respond_head;
1356 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1358 struct u132_command *command = &ftdi->command[
1359 COMMAND_MASK & ftdi->command_next];
1360 struct u132_respond *respond = &ftdi->respond[
1361 RESPOND_MASK & ftdi->respond_next];
1362 int result = -ENODEV;
1363 respond->result = &result;
1364 respond->header = command->header = 0x00 | cPCIu132rd;
1365 command->length = 0x04;
1366 respond->address = command->address = cU132cmd_status;
1367 command->width = 0x00;
1368 command->follows = 0;
1370 command->buffer = NULL;
1371 respond->value = data;
1372 init_completion(&respond->wait_completion);
1373 ftdi->command_next += 1;
1374 ftdi->respond_next += 1;
1375 ftdi_elan_kick_command_queue(ftdi);
1376 mutex_unlock(&ftdi->u132_lock);
1377 wait_for_completion(&respond->wait_completion);
1380 mutex_unlock(&ftdi->u132_lock);
1387 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1388 u8 width, u32 *data)
1390 u8 addressofs = config_offset / 4;
1391 wait:if (ftdi->disconnected > 0) {
1396 mutex_lock(&ftdi->u132_lock);
1397 command_size = ftdi->command_next - ftdi->command_head;
1398 respond_size = ftdi->respond_next - ftdi->respond_head;
1399 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1401 struct u132_command *command = &ftdi->command[
1402 COMMAND_MASK & ftdi->command_next];
1403 struct u132_respond *respond = &ftdi->respond[
1404 RESPOND_MASK & ftdi->respond_next];
1405 int result = -ENODEV;
1406 respond->result = &result;
1407 respond->header = command->header = 0x00 | (cPCIcfgrd &
1409 command->length = 0x04;
1410 respond->address = command->address = addressofs;
1411 command->width = 0x00 | (width & 0x0F);
1412 command->follows = 0;
1414 command->buffer = NULL;
1415 respond->value = data;
1416 init_completion(&respond->wait_completion);
1417 ftdi->command_next += 1;
1418 ftdi->respond_next += 1;
1419 ftdi_elan_kick_command_queue(ftdi);
1420 mutex_unlock(&ftdi->u132_lock);
1421 wait_for_completion(&respond->wait_completion);
1424 mutex_unlock(&ftdi->u132_lock);
1431 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1432 u8 width, u32 *data)
1434 u8 addressofs = mem_offset / 4;
1435 wait:if (ftdi->disconnected > 0) {
1440 mutex_lock(&ftdi->u132_lock);
1441 command_size = ftdi->command_next - ftdi->command_head;
1442 respond_size = ftdi->respond_next - ftdi->respond_head;
1443 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1445 struct u132_command *command = &ftdi->command[
1446 COMMAND_MASK & ftdi->command_next];
1447 struct u132_respond *respond = &ftdi->respond[
1448 RESPOND_MASK & ftdi->respond_next];
1449 int result = -ENODEV;
1450 respond->result = &result;
1451 respond->header = command->header = 0x00 | (cPCImemrd &
1453 command->length = 0x04;
1454 respond->address = command->address = addressofs;
1455 command->width = 0x00 | (width & 0x0F);
1456 command->follows = 0;
1458 command->buffer = NULL;
1459 respond->value = data;
1460 init_completion(&respond->wait_completion);
1461 ftdi->command_next += 1;
1462 ftdi->respond_next += 1;
1463 ftdi_elan_kick_command_queue(ftdi);
1464 mutex_unlock(&ftdi->u132_lock);
1465 wait_for_completion(&respond->wait_completion);
1468 mutex_unlock(&ftdi->u132_lock);
1475 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1476 u8 width, u32 *data)
1478 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1479 if (ftdi->initialized == 0) {
1482 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1486 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1487 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1488 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1489 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1490 int toggle_bits, int error_count, int condition_code, int repeat_number,
1491 int halted, int skipped, int actual, int non_null))
1493 u8 ed = ed_number - 1;
1494 wait:if (ftdi->disconnected > 0) {
1496 } else if (ftdi->initialized == 0) {
1500 mutex_lock(&ftdi->u132_lock);
1501 command_size = ftdi->command_next - ftdi->command_head;
1502 if (command_size < COMMAND_SIZE) {
1503 struct u132_target *target = &ftdi->target[ed];
1504 struct u132_command *command = &ftdi->command[
1505 COMMAND_MASK & ftdi->command_next];
1506 command->header = 0x80 | (ed << 5);
1507 command->length = 0x8007;
1508 command->address = (toggle_bits << 6) | (ep_number << 2)
1510 command->width = usb_maxpacket(urb->dev, urb->pipe,
1511 usb_pipeout(urb->pipe));
1512 command->follows = 8;
1514 command->buffer = urb->setup_packet;
1515 target->callback = callback;
1516 target->endp = endp;
1519 ftdi->command_next += 1;
1520 ftdi_elan_kick_command_queue(ftdi);
1521 mutex_unlock(&ftdi->u132_lock);
1524 mutex_unlock(&ftdi->u132_lock);
1531 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1532 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1533 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1534 int toggle_bits, int error_count, int condition_code, int repeat_number,
1535 int halted, int skipped, int actual, int non_null))
1537 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1538 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1539 ep_number, toggle_bits, callback);
1543 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1544 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1545 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1546 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1547 int toggle_bits, int error_count, int condition_code, int repeat_number,
1548 int halted, int skipped, int actual, int non_null))
1550 u8 ed = ed_number - 1;
1551 wait:if (ftdi->disconnected > 0) {
1553 } else if (ftdi->initialized == 0) {
1557 mutex_lock(&ftdi->u132_lock);
1558 command_size = ftdi->command_next - ftdi->command_head;
1559 if (command_size < COMMAND_SIZE) {
1560 struct u132_target *target = &ftdi->target[ed];
1561 struct u132_command *command = &ftdi->command[
1562 COMMAND_MASK & ftdi->command_next];
1563 u32 remaining_length = urb->transfer_buffer_length -
1565 command->header = 0x82 | (ed << 5);
1566 if (remaining_length == 0) {
1567 command->length = 0x0000;
1568 } else if (remaining_length > 1024) {
1569 command->length = 0x8000 | 1023;
1571 command->length = 0x8000 | (remaining_length -
1573 command->address = (toggle_bits << 6) | (ep_number << 2)
1575 command->width = usb_maxpacket(urb->dev, urb->pipe,
1576 usb_pipeout(urb->pipe));
1577 command->follows = 0;
1579 command->buffer = NULL;
1580 target->callback = callback;
1581 target->endp = endp;
1584 ftdi->command_next += 1;
1585 ftdi_elan_kick_command_queue(ftdi);
1586 mutex_unlock(&ftdi->u132_lock);
1589 mutex_unlock(&ftdi->u132_lock);
1596 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1597 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1598 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1599 int toggle_bits, int error_count, int condition_code, int repeat_number,
1600 int halted, int skipped, int actual, int non_null))
1602 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1603 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1604 ep_number, toggle_bits, callback);
1608 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1609 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1610 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1611 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1612 int toggle_bits, int error_count, int condition_code, int repeat_number,
1613 int halted, int skipped, int actual, int non_null))
1615 u8 ed = ed_number - 1;
1616 wait:if (ftdi->disconnected > 0) {
1618 } else if (ftdi->initialized == 0) {
1622 mutex_lock(&ftdi->u132_lock);
1623 command_size = ftdi->command_next - ftdi->command_head;
1624 if (command_size < COMMAND_SIZE) {
1625 struct u132_target *target = &ftdi->target[ed];
1626 struct u132_command *command = &ftdi->command[
1627 COMMAND_MASK & ftdi->command_next];
1628 command->header = 0x81 | (ed << 5);
1629 command->length = 0x0000;
1630 command->address = (toggle_bits << 6) | (ep_number << 2)
1632 command->width = usb_maxpacket(urb->dev, urb->pipe,
1633 usb_pipeout(urb->pipe));
1634 command->follows = 0;
1636 command->buffer = NULL;
1637 target->callback = callback;
1638 target->endp = endp;
1641 ftdi->command_next += 1;
1642 ftdi_elan_kick_command_queue(ftdi);
1643 mutex_unlock(&ftdi->u132_lock);
1646 mutex_unlock(&ftdi->u132_lock);
1653 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1654 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1655 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1656 int toggle_bits, int error_count, int condition_code, int repeat_number,
1657 int halted, int skipped, int actual, int non_null))
1659 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1660 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1661 ep_number, toggle_bits, callback);
1665 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1666 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1667 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1668 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1669 int toggle_bits, int error_count, int condition_code, int repeat_number,
1670 int halted, int skipped, int actual, int non_null))
1672 u8 ed = ed_number - 1;
1673 wait:if (ftdi->disconnected > 0) {
1675 } else if (ftdi->initialized == 0) {
1679 mutex_lock(&ftdi->u132_lock);
1680 command_size = ftdi->command_next - ftdi->command_head;
1681 if (command_size < COMMAND_SIZE) {
1685 char data[30 *3 + 4];
1687 int m = (sizeof(data) - 1) / 3;
1689 struct u132_target *target = &ftdi->target[ed];
1690 struct u132_command *command = &ftdi->command[
1691 COMMAND_MASK & ftdi->command_next];
1692 command->header = 0x81 | (ed << 5);
1693 command->address = (toggle_bits << 6) | (ep_number << 2)
1695 command->width = usb_maxpacket(urb->dev, urb->pipe,
1696 usb_pipeout(urb->pipe));
1697 command->follows = min_t(u32, 1024,
1698 urb->transfer_buffer_length -
1699 urb->actual_length);
1701 command->buffer = urb->transfer_buffer +
1703 command->length = 0x8000 | (command->follows - 1);
1704 b = command->buffer;
1705 urb_size = command->follows;
1707 while (urb_size-- > 0) {
1709 } else if (i++ < m) {
1710 int w = sprintf(d, " %02X", *b++);
1714 d += sprintf(d, " ..");
1716 target->callback = callback;
1717 target->endp = endp;
1720 ftdi->command_next += 1;
1721 ftdi_elan_kick_command_queue(ftdi);
1722 mutex_unlock(&ftdi->u132_lock);
1725 mutex_unlock(&ftdi->u132_lock);
1732 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1733 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1734 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1735 int toggle_bits, int error_count, int condition_code, int repeat_number,
1736 int halted, int skipped, int actual, int non_null))
1738 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1739 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1740 ep_number, toggle_bits, callback);
1744 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1745 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1746 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1747 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1748 int toggle_bits, int error_count, int condition_code, int repeat_number,
1749 int halted, int skipped, int actual, int non_null))
1751 u8 ed = ed_number - 1;
1752 wait:if (ftdi->disconnected > 0) {
1754 } else if (ftdi->initialized == 0) {
1758 mutex_lock(&ftdi->u132_lock);
1759 command_size = ftdi->command_next - ftdi->command_head;
1760 if (command_size < COMMAND_SIZE) {
1761 u32 remaining_length = urb->transfer_buffer_length -
1763 struct u132_target *target = &ftdi->target[ed];
1764 struct u132_command *command = &ftdi->command[
1765 COMMAND_MASK & ftdi->command_next];
1766 command->header = 0x83 | (ed << 5);
1767 if (remaining_length == 0) {
1768 command->length = 0x0000;
1769 } else if (remaining_length > 1024) {
1770 command->length = 0x8000 | 1023;
1772 command->length = 0x8000 | (remaining_length -
1774 command->address = (toggle_bits << 6) | (ep_number << 2)
1776 command->width = usb_maxpacket(urb->dev, urb->pipe,
1777 usb_pipeout(urb->pipe));
1778 command->follows = 0;
1780 command->buffer = NULL;
1781 target->callback = callback;
1782 target->endp = endp;
1785 ftdi->command_next += 1;
1786 ftdi_elan_kick_command_queue(ftdi);
1787 mutex_unlock(&ftdi->u132_lock);
1790 mutex_unlock(&ftdi->u132_lock);
1797 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1798 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1799 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1800 int toggle_bits, int error_count, int condition_code, int repeat_number,
1801 int halted, int skipped, int actual, int non_null))
1803 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1804 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1805 ep_number, toggle_bits, callback);
1809 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1810 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1813 u8 ed = ed_number - 1;
1814 if (ftdi->disconnected > 0) {
1816 } else if (ftdi->initialized == 0) {
1819 struct u132_target *target = &ftdi->target[ed];
1820 mutex_lock(&ftdi->u132_lock);
1821 if (target->abandoning > 0) {
1822 mutex_unlock(&ftdi->u132_lock);
1825 target->abandoning = 1;
1826 wait_1:if (target->active == 1) {
1827 int command_size = ftdi->command_next -
1829 if (command_size < COMMAND_SIZE) {
1830 struct u132_command *command =
1831 &ftdi->command[COMMAND_MASK &
1832 ftdi->command_next];
1833 command->header = 0x80 | (ed << 5) |
1835 command->length = 0x00;
1836 command->address = 0x00;
1837 command->width = 0x00;
1838 command->follows = 0;
1840 command->buffer = &command->value;
1841 ftdi->command_next += 1;
1842 ftdi_elan_kick_command_queue(ftdi);
1844 mutex_unlock(&ftdi->u132_lock);
1846 mutex_lock(&ftdi->u132_lock);
1850 mutex_unlock(&ftdi->u132_lock);
1856 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1859 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1860 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1864 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1865 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1867 int retry_on_empty = 10;
1868 int retry_on_timeout = 5;
1869 int retry_on_status = 20;
1871 int packet_bytes = 0;
1872 int retval = usb_bulk_msg(ftdi->udev,
1873 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1874 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1875 &packet_bytes, 100);
1876 if (packet_bytes > 2) {
1877 char diag[30 *3 + 4];
1879 int m = (sizeof(diag) - 1) / 3;
1880 char *b = ftdi->bulk_in_buffer;
1883 while (packet_bytes-- > 0) {
1885 if (bytes_read < m) {
1886 d += sprintf(d, " %02X",
1888 } else if (bytes_read > m) {
1890 d += sprintf(d, " ..");
1895 } else if (packet_bytes > 1) {
1896 char s1 = ftdi->bulk_in_buffer[0];
1897 char s2 = ftdi->bulk_in_buffer[1];
1898 if (s1 == 0x31 && s2 == 0x60) {
1900 } else if (retry_on_status-- > 0) {
1903 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1906 } else if (packet_bytes > 0) {
1907 char b1 = ftdi->bulk_in_buffer[0];
1908 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1910 if (retry_on_status-- > 0) {
1913 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1916 } else if (retval == -ETIMEDOUT) {
1917 if (retry_on_timeout-- > 0) {
1920 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1923 } else if (retval == 0) {
1924 if (retry_on_empty-- > 0) {
1927 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1931 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1940 * send the long flush sequence
1943 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1950 urb = usb_alloc_urb(0, GFP_KERNEL);
1952 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequence\n");
1955 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1957 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1963 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1964 ftdi->bulk_out_endpointAddr), buf, i,
1965 ftdi_elan_write_bulk_callback, ftdi);
1966 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1967 retval = usb_submit_urb(urb, GFP_KERNEL);
1969 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1970 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1980 * send the reset sequence
1983 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1990 urb = usb_alloc_urb(0, GFP_KERNEL);
1992 dev_err(&ftdi->udev->dev, "could not get a urb for the reset sequence\n");
1995 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1997 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
2005 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2006 ftdi->bulk_out_endpointAddr), buf, i,
2007 ftdi_elan_write_bulk_callback, ftdi);
2008 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2009 retval = usb_submit_urb(urb, GFP_KERNEL);
2011 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
2012 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
2020 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2024 int retry_on_timeout = 5;
2025 int retry_on_empty = 10;
2027 retval = ftdi_elan_flush_input_fifo(ftdi);
2030 ftdi->bulk_in_left = 0;
2031 ftdi->bulk_in_last = -1;
2032 while (long_stop-- > 0) {
2035 retval = ftdi_elan_synchronize_flush(ftdi);
2038 retval = ftdi_elan_flush_input_fifo(ftdi);
2041 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2047 int packet_bytes = 0;
2048 retval = usb_bulk_msg(ftdi->udev,
2049 usb_rcvbulkpipe(ftdi->udev,
2050 ftdi->bulk_in_endpointAddr),
2051 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2052 &packet_bytes, 500);
2053 if (packet_bytes > 2) {
2054 char diag[30 *3 + 4];
2056 int m = (sizeof(diag) - 1) / 3;
2057 char *b = ftdi->bulk_in_buffer;
2059 unsigned char c = 0;
2061 while (packet_bytes-- > 0) {
2063 if (bytes_read < m) {
2064 d += sprintf(d, " %02X", c);
2065 } else if (bytes_read > m) {
2067 d += sprintf(d, " ..");
2076 } else if (read_stop-- > 0) {
2079 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2083 } else if (packet_bytes > 1) {
2084 unsigned char s1 = ftdi->bulk_in_buffer[0];
2085 unsigned char s2 = ftdi->bulk_in_buffer[1];
2086 if (s1 == 0x31 && s2 == 0x00) {
2087 if (read_stuck-- > 0) {
2091 } else if (s1 == 0x31 && s2 == 0x60) {
2092 if (read_stop-- > 0) {
2095 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2099 if (read_stop-- > 0) {
2102 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2106 } else if (packet_bytes > 0) {
2107 if (read_stop-- > 0) {
2110 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2113 } else if (retval == -ETIMEDOUT) {
2114 if (retry_on_timeout-- > 0) {
2117 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2120 } else if (retval == 0) {
2121 if (retry_on_empty-- > 0) {
2124 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2129 dev_err(&ftdi->udev->dev, "error = %d\n",
2131 if (read_stop-- > 0) {
2134 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2140 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2144 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2146 int retry_on_empty = 10;
2147 int retry_on_timeout = 5;
2148 int retry_on_status = 50;
2150 int packet_bytes = 0;
2151 int retval = usb_bulk_msg(ftdi->udev,
2152 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2153 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2154 &packet_bytes, 1000);
2155 if (packet_bytes > 2) {
2156 char diag[30 *3 + 4];
2158 int m = (sizeof(diag) - 1) / 3;
2159 char *b = ftdi->bulk_in_buffer;
2162 while (packet_bytes-- > 0) {
2164 if (bytes_read < m) {
2165 d += sprintf(d, " %02X",
2167 } else if (bytes_read > m) {
2169 d += sprintf(d, " ..");
2174 } else if (packet_bytes > 1) {
2175 char s1 = ftdi->bulk_in_buffer[0];
2176 char s2 = ftdi->bulk_in_buffer[1];
2177 if (s1 == 0x31 && s2 == 0x60) {
2179 } else if (retry_on_status-- > 0) {
2184 } else if (packet_bytes > 0) {
2185 char b1 = ftdi->bulk_in_buffer[0];
2186 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2187 if (retry_on_status-- > 0) {
2191 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2194 } else if (retval == -ETIMEDOUT) {
2195 if (retry_on_timeout-- > 0) {
2198 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2201 } else if (retval == 0) {
2202 if (retry_on_empty-- > 0) {
2205 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2209 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2216 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2218 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2221 if (ftdi->controlreg & 0x00400000) {
2222 if (ftdi->card_ejected) {
2224 ftdi->card_ejected = 1;
2225 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2230 u8 fn = ftdi->function - 1;
2231 int activePCIfn = fn << 8;
2236 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2240 pciVID = pcidata & 0xFFFF;
2241 pciPID = (pcidata >> 16) & 0xFFFF;
2242 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2243 ftdi->platform_data.device) {
2246 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2247 ftdi->platform_data.vendor, pciVID,
2248 ftdi->platform_data.device, pciPID);
2255 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2256 offsetof(struct ohci_regs, member), 0, data);
2257 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2258 offsetof(struct ohci_regs, member), 0, data);
2260 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2261 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2263 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2277 int mask = OHCI_INTR_INIT;
2279 int reset_timeout = 30; /* ... allow extra time */
2281 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2284 retval = ftdi_read_pcimem(ftdi, control, &control);
2287 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2290 num_ports = rh_a & RH_A_NDP;
2291 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2294 hc_fminterval &= 0x3fff;
2295 if (hc_fminterval != FI) {
2297 hc_fminterval |= FSMP(hc_fminterval) << 16;
2298 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2301 switch (hc_control & OHCI_CTRL_HCFS) {
2305 case OHCI_USB_SUSPEND:
2306 case OHCI_USB_RESUME:
2307 hc_control &= OHCI_CTRL_RWC;
2308 hc_control |= OHCI_USB_RESUME;
2312 hc_control &= OHCI_CTRL_RWC;
2313 hc_control |= OHCI_USB_RESET;
2317 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2320 retval = ftdi_read_pcimem(ftdi, control, &control);
2324 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2327 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2328 for (temp = 0; temp < num_ports; temp++) {
2329 retval = ftdi_write_pcimem(ftdi,
2330 roothub.portstatus[temp], RH_PS_LSDA);
2335 retval = ftdi_read_pcimem(ftdi, control, &control);
2338 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2341 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2345 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2348 if (0 != (status & OHCI_HCR)) {
2349 if (--reset_timeout == 0) {
2350 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2358 if (quirk & OHCI_QUIRK_INITRESET) {
2359 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2362 retval = ftdi_read_pcimem(ftdi, control, &control);
2366 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2369 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2372 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2375 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2378 retval = ftdi_write_pcimem(ftdi, fminterval,
2379 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2382 retval = ftdi_write_pcimem(ftdi, periodicstart,
2383 ((9 *hc_fminterval) / 10) & 0x3fff);
2386 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2389 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2392 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2393 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2394 quirk |= OHCI_QUIRK_INITRESET;
2397 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2398 fminterval, periodicstart);
2399 } /* start controller operations */
2400 hc_control &= OHCI_CTRL_RWC;
2401 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2402 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2405 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2408 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2411 retval = ftdi_read_pcimem(ftdi, control, &control);
2414 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2417 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2420 retval = ftdi_write_pcimem(ftdi, intrdisable,
2421 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2422 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2425 return retval; /* handle root hub init quirks ... */
2426 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2429 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2430 if (quirk & OHCI_QUIRK_SUPERIO) {
2431 roothub_a |= RH_A_NOCP;
2432 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2433 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2436 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2437 roothub_a |= RH_A_NPS;
2438 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2442 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2445 retval = ftdi_write_pcimem(ftdi, roothub.b,
2446 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2449 retval = ftdi_read_pcimem(ftdi, control, &control);
2452 mdelay((roothub_a >> 23) & 0x1fe);
2453 for (temp = 0; temp < num_ports; temp++) {
2455 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2465 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2471 int activePCIfn = fn << 8;
2472 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2476 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2480 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2484 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2488 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2493 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2497 latence_timer &= 0xFFFF00FF;
2498 latence_timer |= 0x00001600;
2499 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2503 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2508 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2512 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2516 for (reg = 0; reg <= 0x54; reg += 4) {
2517 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2524 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2530 int activePCIfn = fn << 8;
2531 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2535 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2539 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2543 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2547 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2552 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2556 latence_timer &= 0xFFFF00FF;
2557 latence_timer |= 0x00001600;
2558 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2562 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2567 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2571 return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2574 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2578 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2581 result = ftdi_elan_check_controller(ftdi, quirk);
2582 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2588 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2593 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2596 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2600 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2603 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2606 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2609 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2612 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2616 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2619 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2622 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2625 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2628 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2632 sensebits = (controlreg >> 16) & 0x000F;
2633 if (0x0D == sensebits)
2639 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2645 int activePCIfn = 0;
2646 int max_devices = 0;
2647 int controllers = 0;
2648 int unrecognized = 0;
2650 for (fn = 0; (fn < 4); fn++) {
2654 activePCIfn = fn << 8;
2655 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2659 pciVID = pcidata & 0xFFFF;
2660 pciPID = (pcidata >> 16) & 0xFFFF;
2661 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2662 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2664 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2666 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2668 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2669 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2671 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2673 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2675 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2676 devices = ftdi_elan_found_controller(ftdi, fn,
2679 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2680 devices = ftdi_elan_found_controller(ftdi, fn,
2681 OHCI_QUIRK_ZFMICRO);
2683 } else if (0 == pcidata) {
2686 if (devices > max_devices) {
2687 max_devices = devices;
2688 ftdi->function = fn + 1;
2689 ftdi->platform_data.vendor = pciVID;
2690 ftdi->platform_data.device = pciPID;
2693 if (ftdi->function > 0) {
2694 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2695 } else if (controllers > 0) {
2697 } else if (unrecognized > 0) {
2700 ftdi->enumerated = 0;
2707 * we use only the first bulk-in and bulk-out endpoints
2709 static int ftdi_elan_probe(struct usb_interface *interface,
2710 const struct usb_device_id *id)
2712 struct usb_host_interface *iface_desc;
2713 struct usb_endpoint_descriptor *endpoint;
2716 int retval = -ENOMEM;
2717 struct usb_ftdi *ftdi;
2719 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2723 mutex_lock(&ftdi_module_lock);
2724 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2725 ftdi->sequence_num = ++ftdi_instances;
2726 mutex_unlock(&ftdi_module_lock);
2727 ftdi_elan_init_kref(ftdi);
2728 sema_init(&ftdi->sw_lock, 1);
2729 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2730 ftdi->interface = interface;
2731 mutex_init(&ftdi->u132_lock);
2733 iface_desc = interface->cur_altsetting;
2734 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2735 endpoint = &iface_desc->endpoint[i].desc;
2736 if (!ftdi->bulk_in_endpointAddr &&
2737 usb_endpoint_is_bulk_in(endpoint)) {
2738 buffer_size = usb_endpoint_maxp(endpoint);
2739 ftdi->bulk_in_size = buffer_size;
2740 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2741 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2742 if (!ftdi->bulk_in_buffer) {
2743 dev_err(&ftdi->udev->dev, "Could not allocate bulk_in_buffer\n");
2748 if (!ftdi->bulk_out_endpointAddr &&
2749 usb_endpoint_is_bulk_out(endpoint)) {
2750 ftdi->bulk_out_endpointAddr =
2751 endpoint->bEndpointAddress;
2754 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2755 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2759 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2760 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2761 ftdi->bulk_out_endpointAddr);
2762 usb_set_intfdata(interface, ftdi);
2763 if (iface_desc->desc.bInterfaceNumber == 0 &&
2764 ftdi->bulk_in_endpointAddr == 0x81 &&
2765 ftdi->bulk_out_endpointAddr == 0x02) {
2766 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2768 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2769 usb_set_intfdata(interface, NULL);
2773 ftdi->class = &ftdi_elan_jtag_class;
2774 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2775 ftdi, iface_desc->desc.bInterfaceNumber,
2779 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2780 ftdi->bulk_in_endpointAddr == 0x83 &&
2781 ftdi->bulk_out_endpointAddr == 0x04) {
2783 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2784 ftdi, iface_desc->desc.bInterfaceNumber);
2785 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2786 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2787 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2788 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2791 dev_err(&ftdi->udev->dev,
2792 "Could not find ELAN's U132 device\n");
2797 ftdi_elan_put_kref(ftdi);
2802 static void ftdi_elan_disconnect(struct usb_interface *interface)
2804 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2805 ftdi->disconnected += 1;
2807 int minor = interface->minor;
2808 struct usb_class_driver *class = ftdi->class;
2809 usb_set_intfdata(interface, NULL);
2810 usb_deregister_dev(interface, class);
2811 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2814 ftdi_status_cancel_work(ftdi);
2815 ftdi_command_cancel_work(ftdi);
2816 ftdi_response_cancel_work(ftdi);
2817 ftdi_elan_abandon_completions(ftdi);
2818 ftdi_elan_abandon_targets(ftdi);
2819 if (ftdi->registered) {
2820 platform_device_unregister(&ftdi->platform_dev);
2821 ftdi->synchronized = 0;
2822 ftdi->enumerated = 0;
2823 ftdi->initialized = 0;
2824 ftdi->registered = 0;
2826 flush_workqueue(status_queue);
2827 flush_workqueue(command_queue);
2828 flush_workqueue(respond_queue);
2829 ftdi->disconnected += 1;
2830 usb_set_intfdata(interface, NULL);
2831 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2833 ftdi_elan_put_kref(ftdi);
2836 static struct usb_driver ftdi_elan_driver = {
2837 .name = "ftdi-elan",
2838 .probe = ftdi_elan_probe,
2839 .disconnect = ftdi_elan_disconnect,
2840 .id_table = ftdi_elan_table,
2842 static int __init ftdi_elan_init(void)
2845 pr_info("driver %s\n", ftdi_elan_driver.name);
2846 mutex_init(&ftdi_module_lock);
2847 INIT_LIST_HEAD(&ftdi_static_list);
2848 status_queue = create_singlethread_workqueue("ftdi-status-control");
2850 goto err_status_queue;
2851 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2853 goto err_command_queue;
2854 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2856 goto err_respond_queue;
2857 result = usb_register(&ftdi_elan_driver);
2859 destroy_workqueue(status_queue);
2860 destroy_workqueue(command_queue);
2861 destroy_workqueue(respond_queue);
2862 pr_err("usb_register failed. Error number %d\n", result);
2867 destroy_workqueue(command_queue);
2869 destroy_workqueue(status_queue);
2871 pr_err("%s couldn't create workqueue\n", ftdi_elan_driver.name);
2875 static void __exit ftdi_elan_exit(void)
2877 struct usb_ftdi *ftdi;
2878 struct usb_ftdi *temp;
2879 usb_deregister(&ftdi_elan_driver);
2880 pr_info("ftdi_u132 driver deregistered\n");
2881 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2882 ftdi_status_cancel_work(ftdi);
2883 ftdi_command_cancel_work(ftdi);
2884 ftdi_response_cancel_work(ftdi);
2885 } flush_workqueue(status_queue);
2886 destroy_workqueue(status_queue);
2887 status_queue = NULL;
2888 flush_workqueue(command_queue);
2889 destroy_workqueue(command_queue);
2890 command_queue = NULL;
2891 flush_workqueue(respond_queue);
2892 destroy_workqueue(respond_queue);
2893 respond_queue = NULL;
2897 module_init(ftdi_elan_init);
2898 module_exit(ftdi_elan_exit);