1 // SPDX-License-Identifier: GPL-2.0
3 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
5 * Copyright(C) 2006 Elan Digital Systems Limited
6 * http://www.elandigitalsystems.com
8 * Author and Maintainer - Tony Olech - Elan Digital Systems
12 * based on various USB client drivers in the 2.6.15 linux kernel
13 * with constant reference to the 3rd Edition of Linux Device Drivers
14 * published by O'Reilly
16 * The U132 adapter is a USB to CardBus adapter specifically designed
17 * for PC cards that contain an OHCI host controller. Typical PC cards
18 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
20 * The U132 adapter will *NOT *work with PC cards that do not contain
21 * an OHCI controller. A simple way to test whether a PC card has an
22 * OHCI controller as an interface is to insert the PC card directly
23 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
24 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
25 * then there is a good chance that the U132 adapter will support the
26 * PC card.(you also need the specific client driver for the PC card)
28 * Please inform the Author and Maintainer about any PC cards that
29 * contain OHCI Host Controller and work when directly connected to
30 * an embedded CardBus slot but do not work when they are connected
31 * via an ELAN U132 adapter.
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <linux/ioctl.h>
42 #include <linux/pci_ids.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/kref.h>
46 #include <linux/mutex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/workqueue.h>
50 #include <linux/platform_device.h>
51 MODULE_AUTHOR("Tony Olech");
52 MODULE_DESCRIPTION("FTDI ELAN driver");
53 MODULE_LICENSE("GPL");
54 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
55 static bool distrust_firmware = 1;
56 module_param(distrust_firmware, bool, 0);
57 MODULE_PARM_DESC(distrust_firmware,
58 "true to distrust firmware power/overcurrent setup");
59 extern struct platform_driver u132_platform_driver;
61 * ftdi_module_lock exists to protect access to global variables
64 static struct mutex ftdi_module_lock;
65 static int ftdi_instances = 0;
66 static struct list_head ftdi_static_list;
68 * end of the global variables protected by ftdi_module_lock
72 #include <linux/usb/hcd.h>
74 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
75 * If you're going to try stuff like this, you need to split
76 * out shareable stuff (register declarations?) into its own
77 * file, maybe name <linux/usb/ohci.h>
80 #include "../host/ohci.h"
81 /* Define these values to match your devices*/
82 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
83 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
84 /* table of devices that work with this driver*/
85 static const struct usb_device_id ftdi_elan_table[] = {
86 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
87 { /* Terminating entry */ }
90 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
91 /* only the jtag(firmware upgrade device) interface requires
92 * a device file and corresponding minor number, but the
93 * interface is created unconditionally - I suppose it could
94 * be configured or not according to a module parameter.
95 * But since we(now) require one interface per device,
96 * and since it unlikely that a normal installation would
97 * require more than a couple of elan-ftdi devices, 8 seems
98 * like a reasonable limit to have here, and if someone
99 * really requires more than 8 devices, then they can frig the
102 #define USB_FTDI_ELAN_MINOR_BASE 192
103 #define COMMAND_BITS 5
104 #define COMMAND_SIZE (1<<COMMAND_BITS)
105 #define COMMAND_MASK (COMMAND_SIZE-1)
106 struct u132_command {
115 #define RESPOND_BITS 5
116 #define RESPOND_SIZE (1<<RESPOND_BITS)
117 #define RESPOND_MASK (RESPOND_SIZE-1)
118 struct u132_respond {
123 struct completion wait_completion;
138 void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
139 int toggle_bits, int error_count, int condition_code,
140 int repeat_number, int halted, int skipped, int actual,
143 /* Structure to hold all of our device specific stuff*/
145 struct list_head ftdi_list;
146 struct mutex u132_lock;
149 struct u132_command command[COMMAND_SIZE];
152 struct u132_respond respond[RESPOND_SIZE];
153 struct u132_target target[4];
154 char device_name[16];
155 unsigned synchronized:1;
156 unsigned enumerated:1;
157 unsigned registered:1;
158 unsigned initialized:1;
159 unsigned card_ejected:1;
165 int status_queue_delay;
166 struct semaphore sw_lock;
167 struct usb_device *udev;
168 struct usb_interface *interface;
169 struct usb_class_driver *class;
170 struct delayed_work status_work;
171 struct delayed_work command_work;
172 struct delayed_work respond_work;
173 struct u132_platform_data platform_data;
174 struct resource resources[0];
175 struct platform_device platform_dev;
176 unsigned char *bulk_in_buffer;
180 __u8 bulk_in_endpointAddr;
181 __u8 bulk_out_endpointAddr;
184 u8 response[4 + 1024];
189 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
190 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
192 static struct usb_driver ftdi_elan_driver;
193 static void ftdi_elan_delete(struct kref *kref)
195 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
196 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
197 usb_put_dev(ftdi->udev);
198 ftdi->disconnected += 1;
199 mutex_lock(&ftdi_module_lock);
200 list_del_init(&ftdi->ftdi_list);
202 mutex_unlock(&ftdi_module_lock);
203 kfree(ftdi->bulk_in_buffer);
204 ftdi->bulk_in_buffer = NULL;
208 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
210 kref_put(&ftdi->kref, ftdi_elan_delete);
213 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
215 kref_get(&ftdi->kref);
218 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
220 kref_init(&ftdi->kref);
223 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
225 if (!schedule_delayed_work(&ftdi->status_work, delta))
226 kref_put(&ftdi->kref, ftdi_elan_delete);
229 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
231 if (schedule_delayed_work(&ftdi->status_work, delta))
232 kref_get(&ftdi->kref);
235 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
237 if (cancel_delayed_work_sync(&ftdi->status_work))
238 kref_put(&ftdi->kref, ftdi_elan_delete);
241 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
243 if (!schedule_delayed_work(&ftdi->command_work, delta))
244 kref_put(&ftdi->kref, ftdi_elan_delete);
247 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
249 if (schedule_delayed_work(&ftdi->command_work, delta))
250 kref_get(&ftdi->kref);
253 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
255 if (cancel_delayed_work_sync(&ftdi->command_work))
256 kref_put(&ftdi->kref, ftdi_elan_delete);
259 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
262 if (!schedule_delayed_work(&ftdi->respond_work, delta))
263 kref_put(&ftdi->kref, ftdi_elan_delete);
266 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
268 if (schedule_delayed_work(&ftdi->respond_work, delta))
269 kref_get(&ftdi->kref);
272 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
274 if (cancel_delayed_work_sync(&ftdi->respond_work))
275 kref_put(&ftdi->kref, ftdi_elan_delete);
278 void ftdi_elan_gone_away(struct platform_device *pdev)
280 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
281 ftdi->gone_away += 1;
282 ftdi_elan_put_kref(ftdi);
286 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
287 static void ftdi_release_platform_dev(struct device *dev)
292 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
293 struct u132_target *target, u8 *buffer, int length);
294 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
295 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
296 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
297 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
298 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
299 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
300 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
301 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
302 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
303 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
305 if (ftdi->platform_dev.dev.parent)
308 ftdi_elan_get_kref(ftdi);
309 ftdi->platform_data.potpg = 100;
310 ftdi->platform_data.reset = NULL;
311 ftdi->platform_dev.id = ftdi->sequence_num;
312 ftdi->platform_dev.resource = ftdi->resources;
313 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
314 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
315 ftdi->platform_dev.dev.parent = NULL;
316 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
317 ftdi->platform_dev.dev.dma_mask = NULL;
318 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
319 ftdi->platform_dev.name = ftdi->device_name;
320 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
321 request_module("u132_hcd");
322 dev_info(&ftdi->udev->dev, "registering '%s'\n",
323 ftdi->platform_dev.name);
325 return platform_device_register(&ftdi->platform_dev);
328 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
330 mutex_lock(&ftdi->u132_lock);
331 while (ftdi->respond_next > ftdi->respond_head) {
332 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
333 ftdi->respond_head++];
334 *respond->result = -ESHUTDOWN;
336 complete(&respond->wait_completion);
338 mutex_unlock(&ftdi->u132_lock);
341 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
344 mutex_lock(&ftdi->u132_lock);
345 while (ed_number-- > 0) {
346 struct u132_target *target = &ftdi->target[ed_number];
347 if (target->active == 1) {
348 target->condition_code = TD_DEVNOTRESP;
349 mutex_unlock(&ftdi->u132_lock);
350 ftdi_elan_do_callback(ftdi, target, NULL, 0);
351 mutex_lock(&ftdi->u132_lock);
357 mutex_unlock(&ftdi->u132_lock);
360 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
363 mutex_lock(&ftdi->u132_lock);
364 while (ed_number-- > 0) {
365 struct u132_target *target = &ftdi->target[ed_number];
366 target->abandoning = 1;
367 wait_1:if (target->active == 1) {
368 int command_size = ftdi->command_next -
370 if (command_size < COMMAND_SIZE) {
371 struct u132_command *command = &ftdi->command[
372 COMMAND_MASK & ftdi->command_next];
373 command->header = 0x80 | (ed_number << 5) | 0x4;
374 command->length = 0x00;
375 command->address = 0x00;
376 command->width = 0x00;
377 command->follows = 0;
379 command->buffer = &command->value;
380 ftdi->command_next += 1;
381 ftdi_elan_kick_command_queue(ftdi);
383 mutex_unlock(&ftdi->u132_lock);
385 mutex_lock(&ftdi->u132_lock);
389 wait_2:if (target->active == 1) {
390 int command_size = ftdi->command_next -
392 if (command_size < COMMAND_SIZE) {
393 struct u132_command *command = &ftdi->command[
394 COMMAND_MASK & ftdi->command_next];
395 command->header = 0x90 | (ed_number << 5);
396 command->length = 0x00;
397 command->address = 0x00;
398 command->width = 0x00;
399 command->follows = 0;
401 command->buffer = &command->value;
402 ftdi->command_next += 1;
403 ftdi_elan_kick_command_queue(ftdi);
405 mutex_unlock(&ftdi->u132_lock);
407 mutex_lock(&ftdi->u132_lock);
415 mutex_unlock(&ftdi->u132_lock);
418 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
421 mutex_lock(&ftdi->u132_lock);
422 while (ed_number-- > 0) {
423 struct u132_target *target = &ftdi->target[ed_number];
424 target->abandoning = 1;
425 wait:if (target->active == 1) {
426 int command_size = ftdi->command_next -
428 if (command_size < COMMAND_SIZE) {
429 struct u132_command *command = &ftdi->command[
430 COMMAND_MASK & ftdi->command_next];
431 command->header = 0x80 | (ed_number << 5) | 0x4;
432 command->length = 0x00;
433 command->address = 0x00;
434 command->width = 0x00;
435 command->follows = 0;
437 command->buffer = &command->value;
438 ftdi->command_next += 1;
439 ftdi_elan_kick_command_queue(ftdi);
441 mutex_unlock(&ftdi->u132_lock);
443 mutex_lock(&ftdi->u132_lock);
451 mutex_unlock(&ftdi->u132_lock);
454 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
456 ftdi_command_queue_work(ftdi, 0);
459 static void ftdi_elan_command_work(struct work_struct *work)
461 struct usb_ftdi *ftdi =
462 container_of(work, struct usb_ftdi, command_work.work);
464 if (ftdi->disconnected > 0) {
465 ftdi_elan_put_kref(ftdi);
468 int retval = ftdi_elan_command_engine(ftdi);
469 if (retval == -ESHUTDOWN) {
470 ftdi->disconnected += 1;
471 } else if (retval == -ENODEV) {
472 ftdi->disconnected += 1;
474 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
475 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
480 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
482 ftdi_respond_queue_work(ftdi, 0);
485 static void ftdi_elan_respond_work(struct work_struct *work)
487 struct usb_ftdi *ftdi =
488 container_of(work, struct usb_ftdi, respond_work.work);
489 if (ftdi->disconnected > 0) {
490 ftdi_elan_put_kref(ftdi);
493 int retval = ftdi_elan_respond_engine(ftdi);
495 } else if (retval == -ESHUTDOWN) {
496 ftdi->disconnected += 1;
497 } else if (retval == -ENODEV) {
498 ftdi->disconnected += 1;
499 } else if (retval == -EILSEQ) {
500 ftdi->disconnected += 1;
502 ftdi->disconnected += 1;
503 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
505 if (ftdi->disconnected > 0) {
506 ftdi_elan_abandon_completions(ftdi);
507 ftdi_elan_abandon_targets(ftdi);
509 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
516 * the sw_lock is initially held and will be freed
517 * after the FTDI has been synchronized
520 static void ftdi_elan_status_work(struct work_struct *work)
522 struct usb_ftdi *ftdi =
523 container_of(work, struct usb_ftdi, status_work.work);
524 int work_delay_in_msec = 0;
525 if (ftdi->disconnected > 0) {
526 ftdi_elan_put_kref(ftdi);
528 } else if (ftdi->synchronized == 0) {
529 down(&ftdi->sw_lock);
530 if (ftdi_elan_synchronize(ftdi) == 0) {
531 ftdi->synchronized = 1;
532 ftdi_command_queue_work(ftdi, 1);
533 ftdi_respond_queue_work(ftdi, 1);
535 work_delay_in_msec = 100;
537 dev_err(&ftdi->udev->dev, "synchronize failed\n");
539 work_delay_in_msec = 10 *1000;
541 } else if (ftdi->stuck_status > 0) {
542 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
543 ftdi->stuck_status = 0;
544 ftdi->synchronized = 0;
545 } else if ((ftdi->stuck_status++ % 60) == 1) {
546 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
548 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
550 work_delay_in_msec = 100;
551 } else if (ftdi->enumerated == 0) {
552 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
553 ftdi->enumerated = 1;
554 work_delay_in_msec = 250;
556 work_delay_in_msec = 1000;
557 } else if (ftdi->initialized == 0) {
558 if (ftdi_elan_setupOHCI(ftdi) == 0) {
559 ftdi->initialized = 1;
560 work_delay_in_msec = 500;
562 dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
563 work_delay_in_msec = 1 *1000;
565 } else if (ftdi->registered == 0) {
566 work_delay_in_msec = 10;
567 if (ftdi_elan_hcd_init(ftdi) == 0) {
568 ftdi->registered = 1;
570 dev_err(&ftdi->udev->dev, "register failed\n");
571 work_delay_in_msec = 250;
573 if (ftdi_elan_checkingPCI(ftdi) == 0) {
574 work_delay_in_msec = 250;
575 } else if (ftdi->controlreg & 0x00400000) {
576 if (ftdi->gone_away > 0) {
577 dev_err(&ftdi->udev->dev, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
578 ftdi->platform_dev.dev.parent,
579 &ftdi->platform_dev.dev);
580 platform_device_unregister(&ftdi->platform_dev);
581 ftdi->platform_dev.dev.parent = NULL;
582 ftdi->registered = 0;
583 ftdi->enumerated = 0;
584 ftdi->card_ejected = 0;
585 ftdi->initialized = 0;
588 ftdi_elan_flush_targets(ftdi);
589 work_delay_in_msec = 250;
591 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n");
592 ftdi_elan_cancel_targets(ftdi);
593 work_delay_in_msec = 500;
594 ftdi->enumerated = 0;
595 ftdi->initialized = 0;
598 if (ftdi->disconnected > 0) {
599 ftdi_elan_put_kref(ftdi);
602 ftdi_status_requeue_work(ftdi,
603 msecs_to_jiffies(work_delay_in_msec));
610 * file_operations for the jtag interface
612 * the usage count for the device is incremented on open()
613 * and decremented on release()
615 static int ftdi_elan_open(struct inode *inode, struct file *file)
618 struct usb_interface *interface;
620 subminor = iminor(inode);
621 interface = usb_find_interface(&ftdi_elan_driver, subminor);
624 pr_err("can't find device for minor %d\n", subminor);
627 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
631 if (down_interruptible(&ftdi->sw_lock)) {
634 ftdi_elan_get_kref(ftdi);
635 file->private_data = ftdi;
642 static int ftdi_elan_release(struct inode *inode, struct file *file)
644 struct usb_ftdi *ftdi = file->private_data;
647 up(&ftdi->sw_lock); /* decrement the count on our device */
648 ftdi_elan_put_kref(ftdi);
655 * blocking bulk reads are used to get data from the device
658 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
659 size_t count, loff_t *ppos)
661 char data[30 *3 + 4];
663 int m = (sizeof(data) - 1) / 3 - 1;
665 int retry_on_empty = 10;
666 int retry_on_timeout = 5;
667 struct usb_ftdi *ftdi = file->private_data;
668 if (ftdi->disconnected > 0) {
672 have:if (ftdi->bulk_in_left > 0) {
674 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
675 ftdi->bulk_in_left -= 1;
676 if (bytes_read < m) {
677 d += sprintf(d, " %02X", 0x000000FF & *p);
678 } else if (bytes_read > m) {
680 d += sprintf(d, " ..");
681 if (copy_to_user(buffer++, p, 1)) {
690 more:if (count > 0) {
691 int packet_bytes = 0;
692 int retval = usb_bulk_msg(ftdi->udev,
693 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
694 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
696 if (packet_bytes > 2) {
697 ftdi->bulk_in_left = packet_bytes - 2;
698 ftdi->bulk_in_last = 1;
700 } else if (retval == -ETIMEDOUT) {
701 if (retry_on_timeout-- > 0) {
703 } else if (bytes_read > 0) {
707 } else if (retval == 0) {
708 if (retry_on_empty-- > 0) {
718 static void ftdi_elan_write_bulk_callback(struct urb *urb)
720 struct usb_ftdi *ftdi = urb->context;
721 int status = urb->status;
723 if (status && !(status == -ENOENT || status == -ECONNRESET ||
724 status == -ESHUTDOWN)) {
725 dev_err(&ftdi->udev->dev,
726 "urb=%p write bulk status received: %d\n", urb, status);
728 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
729 urb->transfer_buffer, urb->transfer_dma);
732 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
733 char *buf, int command_size, int total_size)
737 int I = command_size;
738 int i = ftdi->command_head;
740 struct u132_command *command = &ftdi->command[COMMAND_MASK &
742 int F = command->follows;
743 u8 *f = command->buffer;
744 if (command->header & 0x80) {
745 ed_commands |= 1 << (0x3 & (command->header >> 5));
747 buf[b++] = command->header;
748 buf[b++] = (command->length >> 0) & 0x00FF;
749 buf[b++] = (command->length >> 8) & 0x00FF;
750 buf[b++] = command->address;
751 buf[b++] = command->width;
759 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
762 int I = command_size;
763 int i = ftdi->command_head;
765 struct u132_command *command = &ftdi->command[COMMAND_MASK &
767 total_size += 5 + command->follows;
772 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
779 int command_size = ftdi->command_next - ftdi->command_head;
780 if (command_size == 0)
782 total_size = ftdi_elan_total_command_size(ftdi, command_size);
783 urb = usb_alloc_urb(0, GFP_KERNEL);
786 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
789 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
790 command_size, total_size);
794 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
795 command_size, total_size);
796 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
797 ftdi->bulk_out_endpointAddr), buf, total_size,
798 ftdi_elan_write_bulk_callback, ftdi);
799 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
801 char diag[40 *3 + 4];
805 int s = (sizeof(diag) - 1) / 3;
807 while (s-- > 0 && m-- > 0) {
808 if (s > 0 || m == 0) {
809 d += sprintf(d, " %02X", *c++);
811 d += sprintf(d, " ..");
814 retval = usb_submit_urb(urb, GFP_KERNEL);
816 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
817 retval, urb, command_size, total_size);
818 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
822 usb_free_urb(urb); /* release our reference to this urb,
823 the USB core will eventually free it entirely */
824 ftdi->command_head += command_size;
825 ftdi_elan_kick_respond_queue(ftdi);
829 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
830 struct u132_target *target, u8 *buffer, int length)
832 struct urb *urb = target->urb;
833 int halted = target->halted;
834 int skipped = target->skipped;
835 int actual = target->actual;
836 int non_null = target->non_null;
837 int toggle_bits = target->toggle_bits;
838 int error_count = target->error_count;
839 int condition_code = target->condition_code;
840 int repeat_number = target->repeat_number;
841 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
842 int, int, int, int) = target->callback;
844 target->callback = NULL;
845 (*callback) (target->endp, urb, buffer, length, toggle_bits,
846 error_count, condition_code, repeat_number, halted, skipped,
850 static char *have_ed_set_response(struct usb_ftdi *ftdi,
851 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
854 int payload = (ed_length >> 0) & 0x07FF;
855 mutex_lock(&ftdi->u132_lock);
857 target->non_null = (ed_length >> 15) & 0x0001;
858 target->repeat_number = (ed_length >> 11) & 0x000F;
859 if (ed_type == 0x02 || ed_type == 0x03) {
860 if (payload == 0 || target->abandoning > 0) {
861 target->abandoning = 0;
862 mutex_unlock(&ftdi->u132_lock);
863 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
868 return ftdi->response;
870 ftdi->expected = 4 + payload;
872 mutex_unlock(&ftdi->u132_lock);
876 target->abandoning = 0;
877 mutex_unlock(&ftdi->u132_lock);
878 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
883 return ftdi->response;
887 static char *have_ed_get_response(struct usb_ftdi *ftdi,
888 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
891 mutex_lock(&ftdi->u132_lock);
892 target->condition_code = TD_DEVNOTRESP;
893 target->actual = (ed_length >> 0) & 0x01FF;
894 target->non_null = (ed_length >> 15) & 0x0001;
895 target->repeat_number = (ed_length >> 11) & 0x000F;
896 mutex_unlock(&ftdi->u132_lock);
898 ftdi_elan_do_callback(ftdi, target, NULL, 0);
899 target->abandoning = 0;
903 return ftdi->response;
908 * The engine tries to empty the FTDI fifo
910 * all responses found in the fifo data are dispatched thus
911 * the response buffer can only ever hold a maximum sized
912 * response from the Uxxx.
915 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
917 u8 *b = ftdi->response + ftdi->received;
919 int retry_on_empty = 1;
920 int retry_on_timeout = 3;
922 int packet_bytes = 0;
923 int retval = usb_bulk_msg(ftdi->udev,
924 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
925 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
927 char diag[30 *3 + 4];
929 int m = packet_bytes;
930 u8 *c = ftdi->bulk_in_buffer;
931 int s = (sizeof(diag) - 1) / 3;
933 while (s-- > 0 && m-- > 0) {
934 if (s > 0 || m == 0) {
935 d += sprintf(d, " %02X", *c++);
937 d += sprintf(d, " ..");
939 if (packet_bytes > 2) {
940 ftdi->bulk_in_left = packet_bytes - 2;
941 ftdi->bulk_in_last = 1;
943 } else if (retval == -ETIMEDOUT) {
944 if (retry_on_timeout-- > 0) {
945 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
946 packet_bytes, bytes_read, diag);
948 } else if (bytes_read > 0) {
949 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
953 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
954 packet_bytes, bytes_read, diag);
957 } else if (retval == -EILSEQ) {
958 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
959 retval, packet_bytes, bytes_read, diag);
962 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
963 retval, packet_bytes, bytes_read, diag);
966 if (retry_on_empty-- > 0) {
975 have:if (ftdi->bulk_in_left > 0) {
976 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
978 ftdi->bulk_in_left -= 1;
979 if (ftdi->received == 0 && c == 0xFF) {
983 if (++ftdi->received < ftdi->expected) {
985 } else if (ftdi->ed_found) {
986 int ed_number = (ftdi->response[0] >> 5) & 0x03;
987 u16 ed_length = (ftdi->response[2] << 8) |
989 struct u132_target *target = &ftdi->target[ed_number];
990 int payload = (ed_length >> 0) & 0x07FF;
991 char diag[30 *3 + 4];
994 u8 *c = 4 + ftdi->response;
995 int s = (sizeof(diag) - 1) / 3;
997 while (s-- > 0 && m-- > 0) {
998 if (s > 0 || m == 0) {
999 d += sprintf(d, " %02X", *c++);
1001 d += sprintf(d, " ..");
1003 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1010 } else if (ftdi->expected == 8) {
1012 int respond_head = ftdi->respond_head++;
1013 struct u132_respond *respond = &ftdi->respond[
1014 RESPOND_MASK & respond_head];
1015 u32 data = ftdi->response[7];
1017 data |= ftdi->response[6];
1019 data |= ftdi->response[5];
1021 data |= ftdi->response[4];
1022 *respond->value = data;
1023 *respond->result = 0;
1024 complete(&respond->wait_completion);
1029 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1030 if (buscmd == 0x00) {
1031 } else if (buscmd == 0x02) {
1032 } else if (buscmd == 0x06) {
1033 } else if (buscmd == 0x0A) {
1035 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1039 if ((ftdi->response[0] & 0x80) == 0x00) {
1043 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1044 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1045 u16 ed_length = (ftdi->response[2] << 8) |
1047 struct u132_target *target = &ftdi->target[
1049 target->halted = (ftdi->response[0] >> 3) &
1051 target->skipped = (ftdi->response[0] >> 2) &
1053 target->toggle_bits = (ftdi->response[3] >> 6)
1055 target->error_count = (ftdi->response[3] >> 4)
1057 target->condition_code = (ftdi->response[
1059 if ((ftdi->response[0] & 0x10) == 0x00) {
1060 b = have_ed_set_response(ftdi, target,
1061 ed_length, ed_number, ed_type,
1065 b = have_ed_get_response(ftdi, target,
1066 ed_length, ed_number, ed_type,
1078 * create a urb, and a buffer for it, and copy the data to the urb
1081 static ssize_t ftdi_elan_write(struct file *file,
1082 const char __user *user_buffer, size_t count,
1088 struct usb_ftdi *ftdi = file->private_data;
1090 if (ftdi->disconnected > 0) {
1096 urb = usb_alloc_urb(0, GFP_KERNEL);
1101 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1102 &urb->transfer_dma);
1107 if (copy_from_user(buf, user_buffer, count)) {
1111 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1112 ftdi->bulk_out_endpointAddr), buf, count,
1113 ftdi_elan_write_bulk_callback, ftdi);
1114 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1115 retval = usb_submit_urb(urb, GFP_KERNEL);
1117 dev_err(&ftdi->udev->dev,
1118 "failed submitting write urb, error %d\n", retval);
1126 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1133 static const struct file_operations ftdi_elan_fops = {
1134 .owner = THIS_MODULE,
1135 .llseek = no_llseek,
1136 .read = ftdi_elan_read,
1137 .write = ftdi_elan_write,
1138 .open = ftdi_elan_open,
1139 .release = ftdi_elan_release,
1143 * usb class driver info in order to get a minor number from the usb core,
1144 * and to have the device registered with the driver core
1146 static struct usb_class_driver ftdi_elan_jtag_class = {
1147 .name = "ftdi-%d-jtag",
1148 .fops = &ftdi_elan_fops,
1149 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1153 * the following definitions are for the
1154 * ELAN FPGA state machgine processor that
1155 * lies on the other side of the FTDI chip
1157 #define cPCIu132rd 0x0
1158 #define cPCIu132wr 0x1
1159 #define cPCIiord 0x2
1160 #define cPCIiowr 0x3
1161 #define cPCImemrd 0x6
1162 #define cPCImemwr 0x7
1163 #define cPCIcfgrd 0xA
1164 #define cPCIcfgwr 0xB
1165 #define cPCInull 0xF
1166 #define cU132cmd_status 0x0
1167 #define cU132flash 0x1
1168 #define cPIDsetup 0x0
1171 #define cPIDinonce 0x3
1172 #define cCCnoerror 0x0
1174 #define cCCbitstuff 0x2
1175 #define cCCtoggle 0x3
1176 #define cCCstall 0x4
1177 #define cCCnoresp 0x5
1178 #define cCCbadpid1 0x6
1179 #define cCCbadpid2 0x7
1180 #define cCCdataoverrun 0x8
1181 #define cCCdataunderrun 0x9
1182 #define cCCbuffoverrun 0xC
1183 #define cCCbuffunderrun 0xD
1184 #define cCCnotaccessed 0xF
1185 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1187 wait:if (ftdi->disconnected > 0) {
1191 mutex_lock(&ftdi->u132_lock);
1192 command_size = ftdi->command_next - ftdi->command_head;
1193 if (command_size < COMMAND_SIZE) {
1194 struct u132_command *command = &ftdi->command[
1195 COMMAND_MASK & ftdi->command_next];
1196 command->header = 0x00 | cPCIu132wr;
1197 command->length = 0x04;
1198 command->address = 0x00;
1199 command->width = 0x00;
1200 command->follows = 4;
1201 command->value = data;
1202 command->buffer = &command->value;
1203 ftdi->command_next += 1;
1204 ftdi_elan_kick_command_queue(ftdi);
1205 mutex_unlock(&ftdi->u132_lock);
1208 mutex_unlock(&ftdi->u132_lock);
1215 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1218 u8 addressofs = config_offset / 4;
1219 wait:if (ftdi->disconnected > 0) {
1223 mutex_lock(&ftdi->u132_lock);
1224 command_size = ftdi->command_next - ftdi->command_head;
1225 if (command_size < COMMAND_SIZE) {
1226 struct u132_command *command = &ftdi->command[
1227 COMMAND_MASK & ftdi->command_next];
1228 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1229 command->length = 0x04;
1230 command->address = addressofs;
1231 command->width = 0x00 | (width & 0x0F);
1232 command->follows = 4;
1233 command->value = data;
1234 command->buffer = &command->value;
1235 ftdi->command_next += 1;
1236 ftdi_elan_kick_command_queue(ftdi);
1237 mutex_unlock(&ftdi->u132_lock);
1240 mutex_unlock(&ftdi->u132_lock);
1247 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1250 u8 addressofs = mem_offset / 4;
1251 wait:if (ftdi->disconnected > 0) {
1255 mutex_lock(&ftdi->u132_lock);
1256 command_size = ftdi->command_next - ftdi->command_head;
1257 if (command_size < COMMAND_SIZE) {
1258 struct u132_command *command = &ftdi->command[
1259 COMMAND_MASK & ftdi->command_next];
1260 command->header = 0x00 | (cPCImemwr & 0x0F);
1261 command->length = 0x04;
1262 command->address = addressofs;
1263 command->width = 0x00 | (width & 0x0F);
1264 command->follows = 4;
1265 command->value = data;
1266 command->buffer = &command->value;
1267 ftdi->command_next += 1;
1268 ftdi_elan_kick_command_queue(ftdi);
1269 mutex_unlock(&ftdi->u132_lock);
1272 mutex_unlock(&ftdi->u132_lock);
1279 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1282 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1283 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1287 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1288 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1290 wait:if (ftdi->disconnected > 0) {
1295 mutex_lock(&ftdi->u132_lock);
1296 command_size = ftdi->command_next - ftdi->command_head;
1297 respond_size = ftdi->respond_next - ftdi->respond_head;
1298 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1300 struct u132_command *command = &ftdi->command[
1301 COMMAND_MASK & ftdi->command_next];
1302 struct u132_respond *respond = &ftdi->respond[
1303 RESPOND_MASK & ftdi->respond_next];
1304 int result = -ENODEV;
1305 respond->result = &result;
1306 respond->header = command->header = 0x00 | cPCIu132rd;
1307 command->length = 0x04;
1308 respond->address = command->address = cU132cmd_status;
1309 command->width = 0x00;
1310 command->follows = 0;
1312 command->buffer = NULL;
1313 respond->value = data;
1314 init_completion(&respond->wait_completion);
1315 ftdi->command_next += 1;
1316 ftdi->respond_next += 1;
1317 ftdi_elan_kick_command_queue(ftdi);
1318 mutex_unlock(&ftdi->u132_lock);
1319 wait_for_completion(&respond->wait_completion);
1322 mutex_unlock(&ftdi->u132_lock);
1329 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1330 u8 width, u32 *data)
1332 u8 addressofs = config_offset / 4;
1333 wait:if (ftdi->disconnected > 0) {
1338 mutex_lock(&ftdi->u132_lock);
1339 command_size = ftdi->command_next - ftdi->command_head;
1340 respond_size = ftdi->respond_next - ftdi->respond_head;
1341 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1343 struct u132_command *command = &ftdi->command[
1344 COMMAND_MASK & ftdi->command_next];
1345 struct u132_respond *respond = &ftdi->respond[
1346 RESPOND_MASK & ftdi->respond_next];
1347 int result = -ENODEV;
1348 respond->result = &result;
1349 respond->header = command->header = 0x00 | (cPCIcfgrd &
1351 command->length = 0x04;
1352 respond->address = command->address = addressofs;
1353 command->width = 0x00 | (width & 0x0F);
1354 command->follows = 0;
1356 command->buffer = NULL;
1357 respond->value = data;
1358 init_completion(&respond->wait_completion);
1359 ftdi->command_next += 1;
1360 ftdi->respond_next += 1;
1361 ftdi_elan_kick_command_queue(ftdi);
1362 mutex_unlock(&ftdi->u132_lock);
1363 wait_for_completion(&respond->wait_completion);
1366 mutex_unlock(&ftdi->u132_lock);
1373 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1374 u8 width, u32 *data)
1376 u8 addressofs = mem_offset / 4;
1377 wait:if (ftdi->disconnected > 0) {
1382 mutex_lock(&ftdi->u132_lock);
1383 command_size = ftdi->command_next - ftdi->command_head;
1384 respond_size = ftdi->respond_next - ftdi->respond_head;
1385 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1387 struct u132_command *command = &ftdi->command[
1388 COMMAND_MASK & ftdi->command_next];
1389 struct u132_respond *respond = &ftdi->respond[
1390 RESPOND_MASK & ftdi->respond_next];
1391 int result = -ENODEV;
1392 respond->result = &result;
1393 respond->header = command->header = 0x00 | (cPCImemrd &
1395 command->length = 0x04;
1396 respond->address = command->address = addressofs;
1397 command->width = 0x00 | (width & 0x0F);
1398 command->follows = 0;
1400 command->buffer = NULL;
1401 respond->value = data;
1402 init_completion(&respond->wait_completion);
1403 ftdi->command_next += 1;
1404 ftdi->respond_next += 1;
1405 ftdi_elan_kick_command_queue(ftdi);
1406 mutex_unlock(&ftdi->u132_lock);
1407 wait_for_completion(&respond->wait_completion);
1410 mutex_unlock(&ftdi->u132_lock);
1417 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1418 u8 width, u32 *data)
1420 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1421 if (ftdi->initialized == 0) {
1424 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1428 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1429 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1430 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1431 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1432 int toggle_bits, int error_count, int condition_code, int repeat_number,
1433 int halted, int skipped, int actual, int non_null))
1435 u8 ed = ed_number - 1;
1436 wait:if (ftdi->disconnected > 0) {
1438 } else if (ftdi->initialized == 0) {
1442 mutex_lock(&ftdi->u132_lock);
1443 command_size = ftdi->command_next - ftdi->command_head;
1444 if (command_size < COMMAND_SIZE) {
1445 struct u132_target *target = &ftdi->target[ed];
1446 struct u132_command *command = &ftdi->command[
1447 COMMAND_MASK & ftdi->command_next];
1448 command->header = 0x80 | (ed << 5);
1449 command->length = 0x8007;
1450 command->address = (toggle_bits << 6) | (ep_number << 2)
1452 command->width = usb_maxpacket(urb->dev, urb->pipe);
1453 command->follows = 8;
1455 command->buffer = urb->setup_packet;
1456 target->callback = callback;
1457 target->endp = endp;
1460 ftdi->command_next += 1;
1461 ftdi_elan_kick_command_queue(ftdi);
1462 mutex_unlock(&ftdi->u132_lock);
1465 mutex_unlock(&ftdi->u132_lock);
1472 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1473 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1474 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1475 int toggle_bits, int error_count, int condition_code, int repeat_number,
1476 int halted, int skipped, int actual, int non_null))
1478 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1479 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1480 ep_number, toggle_bits, callback);
1484 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1485 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1486 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1487 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1488 int toggle_bits, int error_count, int condition_code, int repeat_number,
1489 int halted, int skipped, int actual, int non_null))
1491 u8 ed = ed_number - 1;
1492 wait:if (ftdi->disconnected > 0) {
1494 } else if (ftdi->initialized == 0) {
1498 mutex_lock(&ftdi->u132_lock);
1499 command_size = ftdi->command_next - ftdi->command_head;
1500 if (command_size < COMMAND_SIZE) {
1501 struct u132_target *target = &ftdi->target[ed];
1502 struct u132_command *command = &ftdi->command[
1503 COMMAND_MASK & ftdi->command_next];
1504 u32 remaining_length = urb->transfer_buffer_length -
1506 command->header = 0x82 | (ed << 5);
1507 if (remaining_length == 0) {
1508 command->length = 0x0000;
1509 } else if (remaining_length > 1024) {
1510 command->length = 0x8000 | 1023;
1512 command->length = 0x8000 | (remaining_length -
1514 command->address = (toggle_bits << 6) | (ep_number << 2)
1516 command->width = usb_maxpacket(urb->dev, urb->pipe);
1517 command->follows = 0;
1519 command->buffer = NULL;
1520 target->callback = callback;
1521 target->endp = endp;
1524 ftdi->command_next += 1;
1525 ftdi_elan_kick_command_queue(ftdi);
1526 mutex_unlock(&ftdi->u132_lock);
1529 mutex_unlock(&ftdi->u132_lock);
1536 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1537 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1538 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1539 int toggle_bits, int error_count, int condition_code, int repeat_number,
1540 int halted, int skipped, int actual, int non_null))
1542 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1543 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1544 ep_number, toggle_bits, callback);
1548 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1549 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1550 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1551 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1552 int toggle_bits, int error_count, int condition_code, int repeat_number,
1553 int halted, int skipped, int actual, int non_null))
1555 u8 ed = ed_number - 1;
1556 wait:if (ftdi->disconnected > 0) {
1558 } else if (ftdi->initialized == 0) {
1562 mutex_lock(&ftdi->u132_lock);
1563 command_size = ftdi->command_next - ftdi->command_head;
1564 if (command_size < COMMAND_SIZE) {
1565 struct u132_target *target = &ftdi->target[ed];
1566 struct u132_command *command = &ftdi->command[
1567 COMMAND_MASK & ftdi->command_next];
1568 command->header = 0x81 | (ed << 5);
1569 command->length = 0x0000;
1570 command->address = (toggle_bits << 6) | (ep_number << 2)
1572 command->width = usb_maxpacket(urb->dev, urb->pipe);
1573 command->follows = 0;
1575 command->buffer = NULL;
1576 target->callback = callback;
1577 target->endp = endp;
1580 ftdi->command_next += 1;
1581 ftdi_elan_kick_command_queue(ftdi);
1582 mutex_unlock(&ftdi->u132_lock);
1585 mutex_unlock(&ftdi->u132_lock);
1592 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1593 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1594 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1595 int toggle_bits, int error_count, int condition_code, int repeat_number,
1596 int halted, int skipped, int actual, int non_null))
1598 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1599 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1600 ep_number, toggle_bits, callback);
1604 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1605 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, 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 u8 ed = ed_number - 1;
1612 wait:if (ftdi->disconnected > 0) {
1614 } else if (ftdi->initialized == 0) {
1618 mutex_lock(&ftdi->u132_lock);
1619 command_size = ftdi->command_next - ftdi->command_head;
1620 if (command_size < COMMAND_SIZE) {
1624 char data[30 *3 + 4];
1626 int m = (sizeof(data) - 1) / 3 - 1;
1627 struct u132_target *target = &ftdi->target[ed];
1628 struct u132_command *command = &ftdi->command[
1629 COMMAND_MASK & ftdi->command_next];
1630 command->header = 0x81 | (ed << 5);
1631 command->address = (toggle_bits << 6) | (ep_number << 2)
1633 command->width = usb_maxpacket(urb->dev, urb->pipe);
1634 command->follows = min_t(u32, 1024,
1635 urb->transfer_buffer_length -
1636 urb->actual_length);
1638 command->buffer = urb->transfer_buffer +
1640 command->length = 0x8000 | (command->follows - 1);
1641 b = command->buffer;
1642 urb_size = command->follows;
1644 while (urb_size-- > 0) {
1646 } else if (i++ < m) {
1647 int w = sprintf(d, " %02X", *b++);
1650 d += sprintf(d, " ..");
1652 target->callback = callback;
1653 target->endp = endp;
1656 ftdi->command_next += 1;
1657 ftdi_elan_kick_command_queue(ftdi);
1658 mutex_unlock(&ftdi->u132_lock);
1661 mutex_unlock(&ftdi->u132_lock);
1668 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1669 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1670 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1671 int toggle_bits, int error_count, int condition_code, int repeat_number,
1672 int halted, int skipped, int actual, int non_null))
1674 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1675 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1676 ep_number, toggle_bits, callback);
1680 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1681 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1682 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1683 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1684 int toggle_bits, int error_count, int condition_code, int repeat_number,
1685 int halted, int skipped, int actual, int non_null))
1687 u8 ed = ed_number - 1;
1688 wait:if (ftdi->disconnected > 0) {
1690 } else if (ftdi->initialized == 0) {
1694 mutex_lock(&ftdi->u132_lock);
1695 command_size = ftdi->command_next - ftdi->command_head;
1696 if (command_size < COMMAND_SIZE) {
1697 u32 remaining_length = urb->transfer_buffer_length -
1699 struct u132_target *target = &ftdi->target[ed];
1700 struct u132_command *command = &ftdi->command[
1701 COMMAND_MASK & ftdi->command_next];
1702 command->header = 0x83 | (ed << 5);
1703 if (remaining_length == 0) {
1704 command->length = 0x0000;
1705 } else if (remaining_length > 1024) {
1706 command->length = 0x8000 | 1023;
1708 command->length = 0x8000 | (remaining_length -
1710 command->address = (toggle_bits << 6) | (ep_number << 2)
1712 command->width = usb_maxpacket(urb->dev, urb->pipe);
1713 command->follows = 0;
1715 command->buffer = NULL;
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_single(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_single(ftdi, ed_number, endp, urb, address,
1740 ep_number, toggle_bits, callback);
1744 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1745 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1748 u8 ed = ed_number - 1;
1749 if (ftdi->disconnected > 0) {
1751 } else if (ftdi->initialized == 0) {
1754 struct u132_target *target = &ftdi->target[ed];
1755 mutex_lock(&ftdi->u132_lock);
1756 if (target->abandoning > 0) {
1757 mutex_unlock(&ftdi->u132_lock);
1760 target->abandoning = 1;
1761 wait_1:if (target->active == 1) {
1762 int command_size = ftdi->command_next -
1764 if (command_size < COMMAND_SIZE) {
1765 struct u132_command *command =
1766 &ftdi->command[COMMAND_MASK &
1767 ftdi->command_next];
1768 command->header = 0x80 | (ed << 5) |
1770 command->length = 0x00;
1771 command->address = 0x00;
1772 command->width = 0x00;
1773 command->follows = 0;
1775 command->buffer = &command->value;
1776 ftdi->command_next += 1;
1777 ftdi_elan_kick_command_queue(ftdi);
1779 mutex_unlock(&ftdi->u132_lock);
1781 mutex_lock(&ftdi->u132_lock);
1785 mutex_unlock(&ftdi->u132_lock);
1791 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1794 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1795 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1799 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1800 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1802 int retry_on_empty = 10;
1803 int retry_on_timeout = 5;
1804 int retry_on_status = 20;
1806 int packet_bytes = 0;
1807 int retval = usb_bulk_msg(ftdi->udev,
1808 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1809 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1810 &packet_bytes, 100);
1811 if (packet_bytes > 2) {
1812 char diag[30 *3 + 4];
1814 int m = (sizeof(diag) - 1) / 3 - 1;
1815 char *b = ftdi->bulk_in_buffer;
1818 while (packet_bytes-- > 0) {
1820 if (bytes_read < m) {
1821 d += sprintf(d, " %02X",
1823 } else if (bytes_read > m) {
1825 d += sprintf(d, " ..");
1830 } else if (packet_bytes > 1) {
1831 char s1 = ftdi->bulk_in_buffer[0];
1832 char s2 = ftdi->bulk_in_buffer[1];
1833 if (s1 == 0x31 && s2 == 0x60) {
1835 } else if (retry_on_status-- > 0) {
1838 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1841 } else if (packet_bytes > 0) {
1842 char b1 = ftdi->bulk_in_buffer[0];
1843 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1845 if (retry_on_status-- > 0) {
1848 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1851 } else if (retval == -ETIMEDOUT) {
1852 if (retry_on_timeout-- > 0) {
1855 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1858 } else if (retval == 0) {
1859 if (retry_on_empty-- > 0) {
1862 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1866 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1875 * send the long flush sequence
1878 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1885 urb = usb_alloc_urb(0, GFP_KERNEL);
1888 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1890 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1896 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1897 ftdi->bulk_out_endpointAddr), buf, i,
1898 ftdi_elan_write_bulk_callback, ftdi);
1899 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1900 retval = usb_submit_urb(urb, GFP_KERNEL);
1902 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1903 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1913 * send the reset sequence
1916 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1923 urb = usb_alloc_urb(0, GFP_KERNEL);
1926 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1928 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1936 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1937 ftdi->bulk_out_endpointAddr), buf, i,
1938 ftdi_elan_write_bulk_callback, ftdi);
1939 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1940 retval = usb_submit_urb(urb, GFP_KERNEL);
1942 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
1943 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1951 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
1955 int retry_on_timeout = 5;
1956 int retry_on_empty = 10;
1957 retval = ftdi_elan_flush_input_fifo(ftdi);
1960 ftdi->bulk_in_left = 0;
1961 ftdi->bulk_in_last = -1;
1962 while (long_stop-- > 0) {
1965 retval = ftdi_elan_synchronize_flush(ftdi);
1968 retval = ftdi_elan_flush_input_fifo(ftdi);
1971 reset:retval = ftdi_elan_synchronize_reset(ftdi);
1977 int packet_bytes = 0;
1978 retval = usb_bulk_msg(ftdi->udev,
1979 usb_rcvbulkpipe(ftdi->udev,
1980 ftdi->bulk_in_endpointAddr),
1981 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1982 &packet_bytes, 500);
1983 if (packet_bytes > 2) {
1984 char diag[30 *3 + 4];
1986 int m = (sizeof(diag) - 1) / 3 - 1;
1987 char *b = ftdi->bulk_in_buffer;
1989 unsigned char c = 0;
1991 while (packet_bytes-- > 0) {
1993 if (bytes_read < m) {
1994 d += sprintf(d, " %02X", c);
1995 } else if (bytes_read > m) {
1997 d += sprintf(d, " ..");
2006 } else if (read_stop-- > 0) {
2009 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2013 } else if (packet_bytes > 1) {
2014 unsigned char s1 = ftdi->bulk_in_buffer[0];
2015 unsigned char s2 = ftdi->bulk_in_buffer[1];
2016 if (s1 == 0x31 && s2 == 0x00) {
2017 if (read_stuck-- > 0) {
2022 if (read_stop-- > 0) {
2025 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2029 } else if (packet_bytes > 0) {
2030 if (read_stop-- > 0) {
2033 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2036 } else if (retval == -ETIMEDOUT) {
2037 if (retry_on_timeout-- > 0) {
2040 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2043 } else if (retval == 0) {
2044 if (retry_on_empty-- > 0) {
2047 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2051 dev_err(&ftdi->udev->dev, "error = %d\n",
2053 if (read_stop-- > 0) {
2056 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2062 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2066 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2068 int retry_on_empty = 10;
2069 int retry_on_timeout = 5;
2070 int retry_on_status = 50;
2072 int packet_bytes = 0;
2073 int retval = usb_bulk_msg(ftdi->udev,
2074 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2075 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2076 &packet_bytes, 1000);
2077 if (packet_bytes > 2) {
2078 char diag[30 *3 + 4];
2080 int m = (sizeof(diag) - 1) / 3 - 1;
2081 char *b = ftdi->bulk_in_buffer;
2084 while (packet_bytes-- > 0) {
2086 if (bytes_read < m) {
2087 d += sprintf(d, " %02X",
2089 } else if (bytes_read > m) {
2091 d += sprintf(d, " ..");
2095 } else if (packet_bytes > 1) {
2096 char s1 = ftdi->bulk_in_buffer[0];
2097 char s2 = ftdi->bulk_in_buffer[1];
2098 if (s1 == 0x31 && s2 == 0x60) {
2100 } else if (retry_on_status-- > 0) {
2105 } else if (packet_bytes > 0) {
2106 char b1 = ftdi->bulk_in_buffer[0];
2107 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2108 if (retry_on_status-- > 0) {
2112 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2115 } else if (retval == -ETIMEDOUT) {
2116 if (retry_on_timeout-- > 0) {
2119 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2122 } else if (retval == 0) {
2123 if (retry_on_empty-- > 0) {
2126 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2130 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2137 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2139 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2142 if (ftdi->controlreg & 0x00400000) {
2143 if (ftdi->card_ejected) {
2145 ftdi->card_ejected = 1;
2146 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2151 u8 fn = ftdi->function - 1;
2152 int activePCIfn = fn << 8;
2157 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2161 pciVID = pcidata & 0xFFFF;
2162 pciPID = (pcidata >> 16) & 0xFFFF;
2163 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2164 ftdi->platform_data.device) {
2167 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2168 ftdi->platform_data.vendor, pciVID,
2169 ftdi->platform_data.device, pciPID);
2176 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2177 offsetof(struct ohci_regs, member), 0, data);
2178 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2179 offsetof(struct ohci_regs, member), 0, data);
2181 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2182 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2184 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2198 int mask = OHCI_INTR_INIT;
2200 int reset_timeout = 30; /* ... allow extra time */
2202 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2205 retval = ftdi_read_pcimem(ftdi, control, &control);
2208 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2211 num_ports = rh_a & RH_A_NDP;
2212 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2215 hc_fminterval &= 0x3fff;
2216 if (hc_fminterval != FI) {
2218 hc_fminterval |= FSMP(hc_fminterval) << 16;
2219 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2222 switch (hc_control & OHCI_CTRL_HCFS) {
2226 case OHCI_USB_SUSPEND:
2227 case OHCI_USB_RESUME:
2228 hc_control &= OHCI_CTRL_RWC;
2229 hc_control |= OHCI_USB_RESUME;
2233 hc_control &= OHCI_CTRL_RWC;
2234 hc_control |= OHCI_USB_RESET;
2238 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2241 retval = ftdi_read_pcimem(ftdi, control, &control);
2245 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2248 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2249 for (temp = 0; temp < num_ports; temp++) {
2250 retval = ftdi_write_pcimem(ftdi,
2251 roothub.portstatus[temp], RH_PS_LSDA);
2256 retval = ftdi_read_pcimem(ftdi, control, &control);
2259 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2262 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2266 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2269 if (0 != (status & OHCI_HCR)) {
2270 if (--reset_timeout == 0) {
2271 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2279 if (quirk & OHCI_QUIRK_INITRESET) {
2280 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2283 retval = ftdi_read_pcimem(ftdi, control, &control);
2287 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2290 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2293 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2296 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2299 retval = ftdi_write_pcimem(ftdi, fminterval,
2300 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2303 retval = ftdi_write_pcimem(ftdi, periodicstart,
2304 ((9 *hc_fminterval) / 10) & 0x3fff);
2307 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2310 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2313 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2314 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2315 quirk |= OHCI_QUIRK_INITRESET;
2318 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2319 fminterval, periodicstart);
2320 } /* start controller operations */
2321 hc_control &= OHCI_CTRL_RWC;
2322 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2323 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2326 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2329 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2332 retval = ftdi_read_pcimem(ftdi, control, &control);
2335 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2338 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2341 retval = ftdi_write_pcimem(ftdi, intrdisable,
2342 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2343 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2346 return retval; /* handle root hub init quirks ... */
2347 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2350 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2351 if (quirk & OHCI_QUIRK_SUPERIO) {
2352 roothub_a |= RH_A_NOCP;
2353 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2354 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2357 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2358 roothub_a |= RH_A_NPS;
2359 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2363 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2366 retval = ftdi_write_pcimem(ftdi, roothub.b,
2367 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2370 retval = ftdi_read_pcimem(ftdi, control, &control);
2373 mdelay((roothub_a >> 23) & 0x1fe);
2374 for (temp = 0; temp < num_ports; temp++) {
2376 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2386 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2392 int activePCIfn = fn << 8;
2393 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2397 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2401 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2405 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2409 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2414 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2418 latence_timer &= 0xFFFF00FF;
2419 latence_timer |= 0x00001600;
2420 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2424 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2429 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2433 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2437 for (reg = 0; reg <= 0x54; reg += 4) {
2438 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2445 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2451 int activePCIfn = fn << 8;
2452 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2456 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2460 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2464 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2468 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2473 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2477 latence_timer &= 0xFFFF00FF;
2478 latence_timer |= 0x00001600;
2479 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2483 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2488 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2492 return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2495 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2499 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2502 result = ftdi_elan_check_controller(ftdi, quirk);
2503 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2509 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2514 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2517 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2521 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2524 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2527 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2530 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2533 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2537 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2540 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2543 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2546 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2549 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2553 sensebits = (controlreg >> 16) & 0x000F;
2554 if (0x0D == sensebits)
2560 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2566 int activePCIfn = 0;
2567 int max_devices = 0;
2568 int controllers = 0;
2569 int unrecognized = 0;
2571 for (fn = 0; (fn < 4); fn++) {
2575 activePCIfn = fn << 8;
2576 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2580 pciVID = pcidata & 0xFFFF;
2581 pciPID = (pcidata >> 16) & 0xFFFF;
2582 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2583 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2585 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2587 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2589 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2590 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2592 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2594 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2596 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2597 devices = ftdi_elan_found_controller(ftdi, fn,
2600 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2601 devices = ftdi_elan_found_controller(ftdi, fn,
2602 OHCI_QUIRK_ZFMICRO);
2604 } else if (0 == pcidata) {
2607 if (devices > max_devices) {
2608 max_devices = devices;
2609 ftdi->function = fn + 1;
2610 ftdi->platform_data.vendor = pciVID;
2611 ftdi->platform_data.device = pciPID;
2614 if (ftdi->function > 0) {
2615 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2616 } else if (controllers > 0) {
2618 } else if (unrecognized > 0) {
2621 ftdi->enumerated = 0;
2628 * we use only the first bulk-in and bulk-out endpoints
2630 static int ftdi_elan_probe(struct usb_interface *interface,
2631 const struct usb_device_id *id)
2633 struct usb_host_interface *iface_desc;
2634 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
2636 struct usb_ftdi *ftdi;
2638 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2642 mutex_lock(&ftdi_module_lock);
2643 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2644 ftdi->sequence_num = ++ftdi_instances;
2645 mutex_unlock(&ftdi_module_lock);
2646 ftdi_elan_init_kref(ftdi);
2647 sema_init(&ftdi->sw_lock, 1);
2648 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2649 ftdi->interface = interface;
2650 mutex_init(&ftdi->u132_lock);
2653 iface_desc = interface->cur_altsetting;
2654 retval = usb_find_common_endpoints(iface_desc,
2655 &bulk_in, &bulk_out, NULL, NULL);
2657 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2661 ftdi->bulk_in_size = usb_endpoint_maxp(bulk_in);
2662 ftdi->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
2663 ftdi->bulk_in_buffer = kmalloc(ftdi->bulk_in_size, GFP_KERNEL);
2664 if (!ftdi->bulk_in_buffer) {
2669 ftdi->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
2671 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2672 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2673 ftdi->bulk_out_endpointAddr);
2674 usb_set_intfdata(interface, ftdi);
2675 if (iface_desc->desc.bInterfaceNumber == 0 &&
2676 ftdi->bulk_in_endpointAddr == 0x81 &&
2677 ftdi->bulk_out_endpointAddr == 0x02) {
2678 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2680 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2681 usb_set_intfdata(interface, NULL);
2685 ftdi->class = &ftdi_elan_jtag_class;
2686 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2687 ftdi, iface_desc->desc.bInterfaceNumber,
2691 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2692 ftdi->bulk_in_endpointAddr == 0x83 &&
2693 ftdi->bulk_out_endpointAddr == 0x04) {
2695 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2696 ftdi, iface_desc->desc.bInterfaceNumber);
2697 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2698 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2699 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2700 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2703 dev_err(&ftdi->udev->dev,
2704 "Could not find ELAN's U132 device\n");
2709 ftdi_elan_put_kref(ftdi);
2714 static void ftdi_elan_disconnect(struct usb_interface *interface)
2716 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2717 ftdi->disconnected += 1;
2719 int minor = interface->minor;
2720 struct usb_class_driver *class = ftdi->class;
2721 usb_set_intfdata(interface, NULL);
2722 usb_deregister_dev(interface, class);
2723 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2726 ftdi_status_cancel_work(ftdi);
2727 ftdi_command_cancel_work(ftdi);
2728 ftdi_response_cancel_work(ftdi);
2729 ftdi_elan_abandon_completions(ftdi);
2730 ftdi_elan_abandon_targets(ftdi);
2731 if (ftdi->registered) {
2732 platform_device_unregister(&ftdi->platform_dev);
2733 ftdi->synchronized = 0;
2734 ftdi->enumerated = 0;
2735 ftdi->initialized = 0;
2736 ftdi->registered = 0;
2738 ftdi->disconnected += 1;
2739 usb_set_intfdata(interface, NULL);
2740 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2742 ftdi_elan_put_kref(ftdi);
2745 static struct usb_driver ftdi_elan_driver = {
2746 .name = "ftdi-elan",
2747 .probe = ftdi_elan_probe,
2748 .disconnect = ftdi_elan_disconnect,
2749 .id_table = ftdi_elan_table,
2751 static int __init ftdi_elan_init(void)
2754 pr_info("driver %s\n", ftdi_elan_driver.name);
2755 mutex_init(&ftdi_module_lock);
2756 INIT_LIST_HEAD(&ftdi_static_list);
2757 result = usb_register(&ftdi_elan_driver);
2759 pr_err("usb_register failed. Error number %d\n", result);
2765 static void __exit ftdi_elan_exit(void)
2767 struct usb_ftdi *ftdi;
2768 struct usb_ftdi *temp;
2769 usb_deregister(&ftdi_elan_driver);
2770 pr_info("ftdi_u132 driver deregistered\n");
2771 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2772 ftdi_status_cancel_work(ftdi);
2773 ftdi_command_cancel_work(ftdi);
2774 ftdi_response_cancel_work(ftdi);
2779 module_init(ftdi_elan_init);
2780 module_exit(ftdi_elan_exit);