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 <linux/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;
65 * ftdi_module_lock exists to protect access to global variables
68 static struct mutex ftdi_module_lock;
69 static int ftdi_instances = 0;
70 static struct list_head ftdi_static_list;
72 * end of the global variables protected by ftdi_module_lock
76 #include <linux/usb/hcd.h>
78 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
79 * If you're going to try stuff like this, you need to split
80 * out shareable stuff (register declarations?) into its own
81 * file, maybe name <linux/usb/ohci.h>
84 #include "../host/ohci.h"
85 /* Define these values to match your devices*/
86 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
87 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
88 /* table of devices that work with this driver*/
89 static const struct usb_device_id ftdi_elan_table[] = {
90 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
91 { /* Terminating entry */ }
94 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
95 /* only the jtag(firmware upgrade device) interface requires
96 * a device file and corresponding minor number, but the
97 * interface is created unconditionally - I suppose it could
98 * be configured or not according to a module parameter.
99 * But since we(now) require one interface per device,
100 * and since it unlikely that a normal installation would
101 * require more than a couple of elan-ftdi devices, 8 seems
102 * like a reasonable limit to have here, and if someone
103 * really requires more than 8 devices, then they can frig the
106 #define USB_FTDI_ELAN_MINOR_BASE 192
107 #define COMMAND_BITS 5
108 #define COMMAND_SIZE (1<<COMMAND_BITS)
109 #define COMMAND_MASK (COMMAND_SIZE-1)
110 struct u132_command {
119 #define RESPOND_BITS 5
120 #define RESPOND_SIZE (1<<RESPOND_BITS)
121 #define RESPOND_MASK (RESPOND_SIZE-1)
122 struct u132_respond {
127 struct completion wait_completion;
142 void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
143 int toggle_bits, int error_count, int condition_code,
144 int repeat_number, int halted, int skipped, int actual,
147 /* Structure to hold all of our device specific stuff*/
149 struct list_head ftdi_list;
150 struct mutex u132_lock;
153 struct u132_command command[COMMAND_SIZE];
156 struct u132_respond respond[RESPOND_SIZE];
157 struct u132_target target[4];
158 char device_name[16];
159 unsigned synchronized:1;
160 unsigned enumerated:1;
161 unsigned registered:1;
162 unsigned initialized:1;
163 unsigned card_ejected:1;
169 int status_queue_delay;
170 struct semaphore sw_lock;
171 struct usb_device *udev;
172 struct usb_interface *interface;
173 struct usb_class_driver *class;
174 struct delayed_work status_work;
175 struct delayed_work command_work;
176 struct delayed_work respond_work;
177 struct u132_platform_data platform_data;
178 struct resource resources[0];
179 struct platform_device platform_dev;
180 unsigned char *bulk_in_buffer;
184 __u8 bulk_in_endpointAddr;
185 __u8 bulk_out_endpointAddr;
188 u8 response[4 + 1024];
193 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
194 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
196 static struct usb_driver ftdi_elan_driver;
197 static void ftdi_elan_delete(struct kref *kref)
199 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
200 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
201 usb_put_dev(ftdi->udev);
202 ftdi->disconnected += 1;
203 mutex_lock(&ftdi_module_lock);
204 list_del_init(&ftdi->ftdi_list);
206 mutex_unlock(&ftdi_module_lock);
207 kfree(ftdi->bulk_in_buffer);
208 ftdi->bulk_in_buffer = NULL;
211 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
213 kref_put(&ftdi->kref, ftdi_elan_delete);
216 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
218 kref_get(&ftdi->kref);
221 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
223 kref_init(&ftdi->kref);
226 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
228 if (!schedule_delayed_work(&ftdi->status_work, delta))
229 kref_put(&ftdi->kref, ftdi_elan_delete);
232 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
234 if (schedule_delayed_work(&ftdi->status_work, delta))
235 kref_get(&ftdi->kref);
238 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
240 if (cancel_delayed_work_sync(&ftdi->status_work))
241 kref_put(&ftdi->kref, ftdi_elan_delete);
244 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
246 if (!schedule_delayed_work(&ftdi->command_work, delta))
247 kref_put(&ftdi->kref, ftdi_elan_delete);
250 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
252 if (schedule_delayed_work(&ftdi->command_work, delta))
253 kref_get(&ftdi->kref);
256 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
258 if (cancel_delayed_work_sync(&ftdi->command_work))
259 kref_put(&ftdi->kref, ftdi_elan_delete);
262 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
265 if (!schedule_delayed_work(&ftdi->respond_work, delta))
266 kref_put(&ftdi->kref, ftdi_elan_delete);
269 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
271 if (schedule_delayed_work(&ftdi->respond_work, delta))
272 kref_get(&ftdi->kref);
275 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
277 if (cancel_delayed_work_sync(&ftdi->respond_work))
278 kref_put(&ftdi->kref, ftdi_elan_delete);
281 void ftdi_elan_gone_away(struct platform_device *pdev)
283 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
284 ftdi->gone_away += 1;
285 ftdi_elan_put_kref(ftdi);
289 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
290 static void ftdi_release_platform_dev(struct device *dev)
295 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
296 struct u132_target *target, u8 *buffer, int length);
297 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
298 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
299 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
300 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
301 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
302 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
303 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
304 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
305 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
306 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
309 if (ftdi->platform_dev.dev.parent)
311 ftdi_elan_get_kref(ftdi);
312 ftdi->platform_data.potpg = 100;
313 ftdi->platform_data.reset = NULL;
314 ftdi->platform_dev.id = ftdi->sequence_num;
315 ftdi->platform_dev.resource = ftdi->resources;
316 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
317 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
318 ftdi->platform_dev.dev.parent = NULL;
319 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
320 ftdi->platform_dev.dev.dma_mask = NULL;
321 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
322 ftdi->platform_dev.name = ftdi->device_name;
323 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
324 request_module("u132_hcd");
325 dev_info(&ftdi->udev->dev, "registering '%s'\n",
326 ftdi->platform_dev.name);
327 result = platform_device_register(&ftdi->platform_dev);
331 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
333 mutex_lock(&ftdi->u132_lock);
334 while (ftdi->respond_next > ftdi->respond_head) {
335 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
336 ftdi->respond_head++];
337 *respond->result = -ESHUTDOWN;
339 complete(&respond->wait_completion);
340 } mutex_unlock(&ftdi->u132_lock);
343 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
346 mutex_lock(&ftdi->u132_lock);
347 while (ed_number-- > 0) {
348 struct u132_target *target = &ftdi->target[ed_number];
349 if (target->active == 1) {
350 target->condition_code = TD_DEVNOTRESP;
351 mutex_unlock(&ftdi->u132_lock);
352 ftdi_elan_do_callback(ftdi, target, NULL, 0);
353 mutex_lock(&ftdi->u132_lock);
359 mutex_unlock(&ftdi->u132_lock);
362 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
365 mutex_lock(&ftdi->u132_lock);
366 while (ed_number-- > 0) {
367 struct u132_target *target = &ftdi->target[ed_number];
368 target->abandoning = 1;
369 wait_1:if (target->active == 1) {
370 int command_size = ftdi->command_next -
372 if (command_size < COMMAND_SIZE) {
373 struct u132_command *command = &ftdi->command[
374 COMMAND_MASK & ftdi->command_next];
375 command->header = 0x80 | (ed_number << 5) | 0x4;
376 command->length = 0x00;
377 command->address = 0x00;
378 command->width = 0x00;
379 command->follows = 0;
381 command->buffer = &command->value;
382 ftdi->command_next += 1;
383 ftdi_elan_kick_command_queue(ftdi);
385 mutex_unlock(&ftdi->u132_lock);
387 mutex_lock(&ftdi->u132_lock);
391 wait_2:if (target->active == 1) {
392 int command_size = ftdi->command_next -
394 if (command_size < COMMAND_SIZE) {
395 struct u132_command *command = &ftdi->command[
396 COMMAND_MASK & ftdi->command_next];
397 command->header = 0x90 | (ed_number << 5);
398 command->length = 0x00;
399 command->address = 0x00;
400 command->width = 0x00;
401 command->follows = 0;
403 command->buffer = &command->value;
404 ftdi->command_next += 1;
405 ftdi_elan_kick_command_queue(ftdi);
407 mutex_unlock(&ftdi->u132_lock);
409 mutex_lock(&ftdi->u132_lock);
417 mutex_unlock(&ftdi->u132_lock);
420 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
423 mutex_lock(&ftdi->u132_lock);
424 while (ed_number-- > 0) {
425 struct u132_target *target = &ftdi->target[ed_number];
426 target->abandoning = 1;
427 wait:if (target->active == 1) {
428 int command_size = ftdi->command_next -
430 if (command_size < COMMAND_SIZE) {
431 struct u132_command *command = &ftdi->command[
432 COMMAND_MASK & ftdi->command_next];
433 command->header = 0x80 | (ed_number << 5) | 0x4;
434 command->length = 0x00;
435 command->address = 0x00;
436 command->width = 0x00;
437 command->follows = 0;
439 command->buffer = &command->value;
440 ftdi->command_next += 1;
441 ftdi_elan_kick_command_queue(ftdi);
443 mutex_unlock(&ftdi->u132_lock);
445 mutex_lock(&ftdi->u132_lock);
453 mutex_unlock(&ftdi->u132_lock);
456 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
458 ftdi_command_queue_work(ftdi, 0);
461 static void ftdi_elan_command_work(struct work_struct *work)
463 struct usb_ftdi *ftdi =
464 container_of(work, struct usb_ftdi, command_work.work);
466 if (ftdi->disconnected > 0) {
467 ftdi_elan_put_kref(ftdi);
470 int retval = ftdi_elan_command_engine(ftdi);
471 if (retval == -ESHUTDOWN) {
472 ftdi->disconnected += 1;
473 } else if (retval == -ENODEV) {
474 ftdi->disconnected += 1;
476 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
477 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
482 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
484 ftdi_respond_queue_work(ftdi, 0);
487 static void ftdi_elan_respond_work(struct work_struct *work)
489 struct usb_ftdi *ftdi =
490 container_of(work, struct usb_ftdi, respond_work.work);
491 if (ftdi->disconnected > 0) {
492 ftdi_elan_put_kref(ftdi);
495 int retval = ftdi_elan_respond_engine(ftdi);
497 } else if (retval == -ESHUTDOWN) {
498 ftdi->disconnected += 1;
499 } else if (retval == -ENODEV) {
500 ftdi->disconnected += 1;
501 } else if (retval == -EILSEQ) {
502 ftdi->disconnected += 1;
504 ftdi->disconnected += 1;
505 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
507 if (ftdi->disconnected > 0) {
508 ftdi_elan_abandon_completions(ftdi);
509 ftdi_elan_abandon_targets(ftdi);
511 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
518 * the sw_lock is initially held and will be freed
519 * after the FTDI has been synchronized
522 static void ftdi_elan_status_work(struct work_struct *work)
524 struct usb_ftdi *ftdi =
525 container_of(work, struct usb_ftdi, status_work.work);
526 int work_delay_in_msec = 0;
527 if (ftdi->disconnected > 0) {
528 ftdi_elan_put_kref(ftdi);
530 } else if (ftdi->synchronized == 0) {
531 down(&ftdi->sw_lock);
532 if (ftdi_elan_synchronize(ftdi) == 0) {
533 ftdi->synchronized = 1;
534 ftdi_command_queue_work(ftdi, 1);
535 ftdi_respond_queue_work(ftdi, 1);
537 work_delay_in_msec = 100;
539 dev_err(&ftdi->udev->dev, "synchronize failed\n");
541 work_delay_in_msec = 10 *1000;
543 } else if (ftdi->stuck_status > 0) {
544 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
545 ftdi->stuck_status = 0;
546 ftdi->synchronized = 0;
547 } else if ((ftdi->stuck_status++ % 60) == 1) {
548 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
550 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
552 work_delay_in_msec = 100;
553 } else if (ftdi->enumerated == 0) {
554 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
555 ftdi->enumerated = 1;
556 work_delay_in_msec = 250;
558 work_delay_in_msec = 1000;
559 } else if (ftdi->initialized == 0) {
560 if (ftdi_elan_setupOHCI(ftdi) == 0) {
561 ftdi->initialized = 1;
562 work_delay_in_msec = 500;
564 dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
565 work_delay_in_msec = 1 *1000;
567 } else if (ftdi->registered == 0) {
568 work_delay_in_msec = 10;
569 if (ftdi_elan_hcd_init(ftdi) == 0) {
570 ftdi->registered = 1;
572 dev_err(&ftdi->udev->dev, "register failed\n");
573 work_delay_in_msec = 250;
575 if (ftdi_elan_checkingPCI(ftdi) == 0) {
576 work_delay_in_msec = 250;
577 } else if (ftdi->controlreg & 0x00400000) {
578 if (ftdi->gone_away > 0) {
579 dev_err(&ftdi->udev->dev, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
580 ftdi->platform_dev.dev.parent,
581 &ftdi->platform_dev.dev);
582 platform_device_unregister(&ftdi->platform_dev);
583 ftdi->platform_dev.dev.parent = NULL;
584 ftdi->registered = 0;
585 ftdi->enumerated = 0;
586 ftdi->card_ejected = 0;
587 ftdi->initialized = 0;
590 ftdi_elan_flush_targets(ftdi);
591 work_delay_in_msec = 250;
593 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n");
594 ftdi_elan_cancel_targets(ftdi);
595 work_delay_in_msec = 500;
596 ftdi->enumerated = 0;
597 ftdi->initialized = 0;
600 if (ftdi->disconnected > 0) {
601 ftdi_elan_put_kref(ftdi);
604 ftdi_status_requeue_work(ftdi,
605 msecs_to_jiffies(work_delay_in_msec));
612 * file_operations for the jtag interface
614 * the usage count for the device is incremented on open()
615 * and decremented on release()
617 static int ftdi_elan_open(struct inode *inode, struct file *file)
620 struct usb_interface *interface;
622 subminor = iminor(inode);
623 interface = usb_find_interface(&ftdi_elan_driver, subminor);
626 pr_err("can't find device for minor %d\n", subminor);
629 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
633 if (down_interruptible(&ftdi->sw_lock)) {
636 ftdi_elan_get_kref(ftdi);
637 file->private_data = ftdi;
644 static int ftdi_elan_release(struct inode *inode, struct file *file)
646 struct usb_ftdi *ftdi = file->private_data;
649 up(&ftdi->sw_lock); /* decrement the count on our device */
650 ftdi_elan_put_kref(ftdi);
657 * blocking bulk reads are used to get data from the device
660 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
661 size_t count, loff_t *ppos)
663 char data[30 *3 + 4];
665 int m = (sizeof(data) - 1) / 3 - 1;
667 int retry_on_empty = 10;
668 int retry_on_timeout = 5;
669 struct usb_ftdi *ftdi = file->private_data;
670 if (ftdi->disconnected > 0) {
674 have:if (ftdi->bulk_in_left > 0) {
676 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
677 ftdi->bulk_in_left -= 1;
678 if (bytes_read < m) {
679 d += sprintf(d, " %02X", 0x000000FF & *p);
680 } else if (bytes_read > m) {
682 d += sprintf(d, " ..");
683 if (copy_to_user(buffer++, p, 1)) {
692 more:if (count > 0) {
693 int packet_bytes = 0;
694 int retval = usb_bulk_msg(ftdi->udev,
695 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
696 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
698 if (packet_bytes > 2) {
699 ftdi->bulk_in_left = packet_bytes - 2;
700 ftdi->bulk_in_last = 1;
702 } else if (retval == -ETIMEDOUT) {
703 if (retry_on_timeout-- > 0) {
705 } else if (bytes_read > 0) {
709 } else if (retval == 0) {
710 if (retry_on_empty-- > 0) {
720 static void ftdi_elan_write_bulk_callback(struct urb *urb)
722 struct usb_ftdi *ftdi = urb->context;
723 int status = urb->status;
725 if (status && !(status == -ENOENT || status == -ECONNRESET ||
726 status == -ESHUTDOWN)) {
727 dev_err(&ftdi->udev->dev,
728 "urb=%p write bulk status received: %d\n", urb, status);
730 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
731 urb->transfer_buffer, urb->transfer_dma);
734 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
735 char *buf, int command_size, int total_size)
739 int I = command_size;
740 int i = ftdi->command_head;
742 struct u132_command *command = &ftdi->command[COMMAND_MASK &
744 int F = command->follows;
745 u8 *f = command->buffer;
746 if (command->header & 0x80) {
747 ed_commands |= 1 << (0x3 & (command->header >> 5));
749 buf[b++] = command->header;
750 buf[b++] = (command->length >> 0) & 0x00FF;
751 buf[b++] = (command->length >> 8) & 0x00FF;
752 buf[b++] = command->address;
753 buf[b++] = command->width;
761 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
764 int I = command_size;
765 int i = ftdi->command_head;
767 struct u132_command *command = &ftdi->command[COMMAND_MASK &
769 total_size += 5 + command->follows;
773 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
780 int command_size = ftdi->command_next - ftdi->command_head;
781 if (command_size == 0)
783 total_size = ftdi_elan_total_command_size(ftdi, command_size);
784 urb = usb_alloc_urb(0, GFP_KERNEL);
787 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
790 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
791 command_size, total_size);
795 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
796 command_size, total_size);
797 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
798 ftdi->bulk_out_endpointAddr), buf, total_size,
799 ftdi_elan_write_bulk_callback, ftdi);
800 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
802 char diag[40 *3 + 4];
806 int s = (sizeof(diag) - 1) / 3;
808 while (s-- > 0 && m-- > 0) {
809 if (s > 0 || m == 0) {
810 d += sprintf(d, " %02X", *c++);
812 d += sprintf(d, " ..");
815 retval = usb_submit_urb(urb, GFP_KERNEL);
817 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
818 retval, urb, command_size, total_size);
819 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
823 usb_free_urb(urb); /* release our reference to this urb,
824 the USB core will eventually free it entirely */
825 ftdi->command_head += command_size;
826 ftdi_elan_kick_respond_queue(ftdi);
830 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
831 struct u132_target *target, u8 *buffer, int length)
833 struct urb *urb = target->urb;
834 int halted = target->halted;
835 int skipped = target->skipped;
836 int actual = target->actual;
837 int non_null = target->non_null;
838 int toggle_bits = target->toggle_bits;
839 int error_count = target->error_count;
840 int condition_code = target->condition_code;
841 int repeat_number = target->repeat_number;
842 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
843 int, int, int, int) = target->callback;
845 target->callback = NULL;
846 (*callback) (target->endp, urb, buffer, length, toggle_bits,
847 error_count, condition_code, repeat_number, halted, skipped,
851 static char *have_ed_set_response(struct usb_ftdi *ftdi,
852 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
855 int payload = (ed_length >> 0) & 0x07FF;
856 mutex_lock(&ftdi->u132_lock);
858 target->non_null = (ed_length >> 15) & 0x0001;
859 target->repeat_number = (ed_length >> 11) & 0x000F;
860 if (ed_type == 0x02) {
861 if (payload == 0 || target->abandoning > 0) {
862 target->abandoning = 0;
863 mutex_unlock(&ftdi->u132_lock);
864 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
869 return ftdi->response;
871 ftdi->expected = 4 + payload;
873 mutex_unlock(&ftdi->u132_lock);
876 } else if (ed_type == 0x03) {
877 if (payload == 0 || target->abandoning > 0) {
878 target->abandoning = 0;
879 mutex_unlock(&ftdi->u132_lock);
880 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
885 return ftdi->response;
887 ftdi->expected = 4 + payload;
889 mutex_unlock(&ftdi->u132_lock);
892 } else if (ed_type == 0x01) {
893 target->abandoning = 0;
894 mutex_unlock(&ftdi->u132_lock);
895 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
900 return ftdi->response;
902 target->abandoning = 0;
903 mutex_unlock(&ftdi->u132_lock);
904 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
909 return ftdi->response;
913 static char *have_ed_get_response(struct usb_ftdi *ftdi,
914 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
917 mutex_lock(&ftdi->u132_lock);
918 target->condition_code = TD_DEVNOTRESP;
919 target->actual = (ed_length >> 0) & 0x01FF;
920 target->non_null = (ed_length >> 15) & 0x0001;
921 target->repeat_number = (ed_length >> 11) & 0x000F;
922 mutex_unlock(&ftdi->u132_lock);
924 ftdi_elan_do_callback(ftdi, target, NULL, 0);
925 target->abandoning = 0;
929 return ftdi->response;
934 * The engine tries to empty the FTDI fifo
936 * all responses found in the fifo data are dispatched thus
937 * the response buffer can only ever hold a maximum sized
938 * response from the Uxxx.
941 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
943 u8 *b = ftdi->response + ftdi->received;
945 int retry_on_empty = 1;
946 int retry_on_timeout = 3;
947 int empty_packets = 0;
949 int packet_bytes = 0;
950 int retval = usb_bulk_msg(ftdi->udev,
951 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
952 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
954 char diag[30 *3 + 4];
956 int m = packet_bytes;
957 u8 *c = ftdi->bulk_in_buffer;
958 int s = (sizeof(diag) - 1) / 3;
960 while (s-- > 0 && m-- > 0) {
961 if (s > 0 || m == 0) {
962 d += sprintf(d, " %02X", *c++);
964 d += sprintf(d, " ..");
966 if (packet_bytes > 2) {
967 ftdi->bulk_in_left = packet_bytes - 2;
968 ftdi->bulk_in_last = 1;
970 } else if (retval == -ETIMEDOUT) {
971 if (retry_on_timeout-- > 0) {
972 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
973 packet_bytes, bytes_read, diag);
975 } else if (bytes_read > 0) {
976 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
980 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
981 packet_bytes, bytes_read, diag);
984 } else if (retval == -EILSEQ) {
985 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
986 retval, packet_bytes, bytes_read, diag);
989 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
990 retval, packet_bytes, bytes_read, diag);
992 } else if (packet_bytes == 2) {
993 unsigned char s0 = ftdi->bulk_in_buffer[0];
994 unsigned char s1 = ftdi->bulk_in_buffer[1];
996 if (s0 == 0x31 && s1 == 0x60) {
997 if (retry_on_empty-- > 0) {
1001 } else if (s0 == 0x31 && s1 == 0x00) {
1002 if (retry_on_empty-- > 0) {
1007 if (retry_on_empty-- > 0) {
1012 } else if (packet_bytes == 1) {
1013 if (retry_on_empty-- > 0) {
1018 if (retry_on_empty-- > 0) {
1027 have:if (ftdi->bulk_in_left > 0) {
1028 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1030 ftdi->bulk_in_left -= 1;
1031 if (ftdi->received == 0 && c == 0xFF) {
1035 if (++ftdi->received < ftdi->expected) {
1037 } else if (ftdi->ed_found) {
1038 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1039 u16 ed_length = (ftdi->response[2] << 8) |
1041 struct u132_target *target = &ftdi->target[ed_number];
1042 int payload = (ed_length >> 0) & 0x07FF;
1043 char diag[30 *3 + 4];
1046 u8 *c = 4 + ftdi->response;
1047 int s = (sizeof(diag) - 1) / 3;
1049 while (s-- > 0 && m-- > 0) {
1050 if (s > 0 || m == 0) {
1051 d += sprintf(d, " %02X", *c++);
1053 d += sprintf(d, " ..");
1055 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1062 } else if (ftdi->expected == 8) {
1064 int respond_head = ftdi->respond_head++;
1065 struct u132_respond *respond = &ftdi->respond[
1066 RESPOND_MASK & respond_head];
1067 u32 data = ftdi->response[7];
1069 data |= ftdi->response[6];
1071 data |= ftdi->response[5];
1073 data |= ftdi->response[4];
1074 *respond->value = data;
1075 *respond->result = 0;
1076 complete(&respond->wait_completion);
1081 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1082 if (buscmd == 0x00) {
1083 } else if (buscmd == 0x02) {
1084 } else if (buscmd == 0x06) {
1085 } else if (buscmd == 0x0A) {
1087 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1091 if ((ftdi->response[0] & 0x80) == 0x00) {
1095 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1096 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1097 u16 ed_length = (ftdi->response[2] << 8) |
1099 struct u132_target *target = &ftdi->target[
1101 target->halted = (ftdi->response[0] >> 3) &
1103 target->skipped = (ftdi->response[0] >> 2) &
1105 target->toggle_bits = (ftdi->response[3] >> 6)
1107 target->error_count = (ftdi->response[3] >> 4)
1109 target->condition_code = (ftdi->response[
1111 if ((ftdi->response[0] & 0x10) == 0x00) {
1112 b = have_ed_set_response(ftdi, target,
1113 ed_length, ed_number, ed_type,
1117 b = have_ed_get_response(ftdi, target,
1118 ed_length, ed_number, ed_type,
1130 * create a urb, and a buffer for it, and copy the data to the urb
1133 static ssize_t ftdi_elan_write(struct file *file,
1134 const char __user *user_buffer, size_t count,
1140 struct usb_ftdi *ftdi = file->private_data;
1142 if (ftdi->disconnected > 0) {
1148 urb = usb_alloc_urb(0, GFP_KERNEL);
1153 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1154 &urb->transfer_dma);
1159 if (copy_from_user(buf, user_buffer, count)) {
1163 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1164 ftdi->bulk_out_endpointAddr), buf, count,
1165 ftdi_elan_write_bulk_callback, ftdi);
1166 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1167 retval = usb_submit_urb(urb, GFP_KERNEL);
1169 dev_err(&ftdi->udev->dev,
1170 "failed submitting write urb, error %d\n", retval);
1178 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1185 static const struct file_operations ftdi_elan_fops = {
1186 .owner = THIS_MODULE,
1187 .llseek = no_llseek,
1188 .read = ftdi_elan_read,
1189 .write = ftdi_elan_write,
1190 .open = ftdi_elan_open,
1191 .release = ftdi_elan_release,
1195 * usb class driver info in order to get a minor number from the usb core,
1196 * and to have the device registered with the driver core
1198 static struct usb_class_driver ftdi_elan_jtag_class = {
1199 .name = "ftdi-%d-jtag",
1200 .fops = &ftdi_elan_fops,
1201 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1205 * the following definitions are for the
1206 * ELAN FPGA state machgine processor that
1207 * lies on the other side of the FTDI chip
1209 #define cPCIu132rd 0x0
1210 #define cPCIu132wr 0x1
1211 #define cPCIiord 0x2
1212 #define cPCIiowr 0x3
1213 #define cPCImemrd 0x6
1214 #define cPCImemwr 0x7
1215 #define cPCIcfgrd 0xA
1216 #define cPCIcfgwr 0xB
1217 #define cPCInull 0xF
1218 #define cU132cmd_status 0x0
1219 #define cU132flash 0x1
1220 #define cPIDsetup 0x0
1223 #define cPIDinonce 0x3
1224 #define cCCnoerror 0x0
1226 #define cCCbitstuff 0x2
1227 #define cCCtoggle 0x3
1228 #define cCCstall 0x4
1229 #define cCCnoresp 0x5
1230 #define cCCbadpid1 0x6
1231 #define cCCbadpid2 0x7
1232 #define cCCdataoverrun 0x8
1233 #define cCCdataunderrun 0x9
1234 #define cCCbuffoverrun 0xC
1235 #define cCCbuffunderrun 0xD
1236 #define cCCnotaccessed 0xF
1237 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1239 wait:if (ftdi->disconnected > 0) {
1243 mutex_lock(&ftdi->u132_lock);
1244 command_size = ftdi->command_next - ftdi->command_head;
1245 if (command_size < COMMAND_SIZE) {
1246 struct u132_command *command = &ftdi->command[
1247 COMMAND_MASK & ftdi->command_next];
1248 command->header = 0x00 | cPCIu132wr;
1249 command->length = 0x04;
1250 command->address = 0x00;
1251 command->width = 0x00;
1252 command->follows = 4;
1253 command->value = data;
1254 command->buffer = &command->value;
1255 ftdi->command_next += 1;
1256 ftdi_elan_kick_command_queue(ftdi);
1257 mutex_unlock(&ftdi->u132_lock);
1260 mutex_unlock(&ftdi->u132_lock);
1267 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1270 u8 addressofs = config_offset / 4;
1271 wait:if (ftdi->disconnected > 0) {
1275 mutex_lock(&ftdi->u132_lock);
1276 command_size = ftdi->command_next - ftdi->command_head;
1277 if (command_size < COMMAND_SIZE) {
1278 struct u132_command *command = &ftdi->command[
1279 COMMAND_MASK & ftdi->command_next];
1280 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1281 command->length = 0x04;
1282 command->address = addressofs;
1283 command->width = 0x00 | (width & 0x0F);
1284 command->follows = 4;
1285 command->value = data;
1286 command->buffer = &command->value;
1287 ftdi->command_next += 1;
1288 ftdi_elan_kick_command_queue(ftdi);
1289 mutex_unlock(&ftdi->u132_lock);
1292 mutex_unlock(&ftdi->u132_lock);
1299 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1302 u8 addressofs = mem_offset / 4;
1303 wait:if (ftdi->disconnected > 0) {
1307 mutex_lock(&ftdi->u132_lock);
1308 command_size = ftdi->command_next - ftdi->command_head;
1309 if (command_size < COMMAND_SIZE) {
1310 struct u132_command *command = &ftdi->command[
1311 COMMAND_MASK & ftdi->command_next];
1312 command->header = 0x00 | (cPCImemwr & 0x0F);
1313 command->length = 0x04;
1314 command->address = addressofs;
1315 command->width = 0x00 | (width & 0x0F);
1316 command->follows = 4;
1317 command->value = data;
1318 command->buffer = &command->value;
1319 ftdi->command_next += 1;
1320 ftdi_elan_kick_command_queue(ftdi);
1321 mutex_unlock(&ftdi->u132_lock);
1324 mutex_unlock(&ftdi->u132_lock);
1331 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1334 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1335 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1339 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1340 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1342 wait:if (ftdi->disconnected > 0) {
1347 mutex_lock(&ftdi->u132_lock);
1348 command_size = ftdi->command_next - ftdi->command_head;
1349 respond_size = ftdi->respond_next - ftdi->respond_head;
1350 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1352 struct u132_command *command = &ftdi->command[
1353 COMMAND_MASK & ftdi->command_next];
1354 struct u132_respond *respond = &ftdi->respond[
1355 RESPOND_MASK & ftdi->respond_next];
1356 int result = -ENODEV;
1357 respond->result = &result;
1358 respond->header = command->header = 0x00 | cPCIu132rd;
1359 command->length = 0x04;
1360 respond->address = command->address = cU132cmd_status;
1361 command->width = 0x00;
1362 command->follows = 0;
1364 command->buffer = NULL;
1365 respond->value = data;
1366 init_completion(&respond->wait_completion);
1367 ftdi->command_next += 1;
1368 ftdi->respond_next += 1;
1369 ftdi_elan_kick_command_queue(ftdi);
1370 mutex_unlock(&ftdi->u132_lock);
1371 wait_for_completion(&respond->wait_completion);
1374 mutex_unlock(&ftdi->u132_lock);
1381 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1382 u8 width, u32 *data)
1384 u8 addressofs = config_offset / 4;
1385 wait:if (ftdi->disconnected > 0) {
1390 mutex_lock(&ftdi->u132_lock);
1391 command_size = ftdi->command_next - ftdi->command_head;
1392 respond_size = ftdi->respond_next - ftdi->respond_head;
1393 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1395 struct u132_command *command = &ftdi->command[
1396 COMMAND_MASK & ftdi->command_next];
1397 struct u132_respond *respond = &ftdi->respond[
1398 RESPOND_MASK & ftdi->respond_next];
1399 int result = -ENODEV;
1400 respond->result = &result;
1401 respond->header = command->header = 0x00 | (cPCIcfgrd &
1403 command->length = 0x04;
1404 respond->address = command->address = addressofs;
1405 command->width = 0x00 | (width & 0x0F);
1406 command->follows = 0;
1408 command->buffer = NULL;
1409 respond->value = data;
1410 init_completion(&respond->wait_completion);
1411 ftdi->command_next += 1;
1412 ftdi->respond_next += 1;
1413 ftdi_elan_kick_command_queue(ftdi);
1414 mutex_unlock(&ftdi->u132_lock);
1415 wait_for_completion(&respond->wait_completion);
1418 mutex_unlock(&ftdi->u132_lock);
1425 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1426 u8 width, u32 *data)
1428 u8 addressofs = mem_offset / 4;
1429 wait:if (ftdi->disconnected > 0) {
1434 mutex_lock(&ftdi->u132_lock);
1435 command_size = ftdi->command_next - ftdi->command_head;
1436 respond_size = ftdi->respond_next - ftdi->respond_head;
1437 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1439 struct u132_command *command = &ftdi->command[
1440 COMMAND_MASK & ftdi->command_next];
1441 struct u132_respond *respond = &ftdi->respond[
1442 RESPOND_MASK & ftdi->respond_next];
1443 int result = -ENODEV;
1444 respond->result = &result;
1445 respond->header = command->header = 0x00 | (cPCImemrd &
1447 command->length = 0x04;
1448 respond->address = command->address = addressofs;
1449 command->width = 0x00 | (width & 0x0F);
1450 command->follows = 0;
1452 command->buffer = NULL;
1453 respond->value = data;
1454 init_completion(&respond->wait_completion);
1455 ftdi->command_next += 1;
1456 ftdi->respond_next += 1;
1457 ftdi_elan_kick_command_queue(ftdi);
1458 mutex_unlock(&ftdi->u132_lock);
1459 wait_for_completion(&respond->wait_completion);
1462 mutex_unlock(&ftdi->u132_lock);
1469 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1470 u8 width, u32 *data)
1472 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1473 if (ftdi->initialized == 0) {
1476 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1480 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1481 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1482 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1483 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1484 int toggle_bits, int error_count, int condition_code, int repeat_number,
1485 int halted, int skipped, int actual, int non_null))
1487 u8 ed = ed_number - 1;
1488 wait:if (ftdi->disconnected > 0) {
1490 } else if (ftdi->initialized == 0) {
1494 mutex_lock(&ftdi->u132_lock);
1495 command_size = ftdi->command_next - ftdi->command_head;
1496 if (command_size < COMMAND_SIZE) {
1497 struct u132_target *target = &ftdi->target[ed];
1498 struct u132_command *command = &ftdi->command[
1499 COMMAND_MASK & ftdi->command_next];
1500 command->header = 0x80 | (ed << 5);
1501 command->length = 0x8007;
1502 command->address = (toggle_bits << 6) | (ep_number << 2)
1504 command->width = usb_maxpacket(urb->dev, urb->pipe,
1505 usb_pipeout(urb->pipe));
1506 command->follows = 8;
1508 command->buffer = urb->setup_packet;
1509 target->callback = callback;
1510 target->endp = endp;
1513 ftdi->command_next += 1;
1514 ftdi_elan_kick_command_queue(ftdi);
1515 mutex_unlock(&ftdi->u132_lock);
1518 mutex_unlock(&ftdi->u132_lock);
1525 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1526 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1527 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1528 int toggle_bits, int error_count, int condition_code, int repeat_number,
1529 int halted, int skipped, int actual, int non_null))
1531 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1532 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1533 ep_number, toggle_bits, callback);
1537 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1538 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1539 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1540 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1541 int toggle_bits, int error_count, int condition_code, int repeat_number,
1542 int halted, int skipped, int actual, int non_null))
1544 u8 ed = ed_number - 1;
1545 wait:if (ftdi->disconnected > 0) {
1547 } else if (ftdi->initialized == 0) {
1551 mutex_lock(&ftdi->u132_lock);
1552 command_size = ftdi->command_next - ftdi->command_head;
1553 if (command_size < COMMAND_SIZE) {
1554 struct u132_target *target = &ftdi->target[ed];
1555 struct u132_command *command = &ftdi->command[
1556 COMMAND_MASK & ftdi->command_next];
1557 u32 remaining_length = urb->transfer_buffer_length -
1559 command->header = 0x82 | (ed << 5);
1560 if (remaining_length == 0) {
1561 command->length = 0x0000;
1562 } else if (remaining_length > 1024) {
1563 command->length = 0x8000 | 1023;
1565 command->length = 0x8000 | (remaining_length -
1567 command->address = (toggle_bits << 6) | (ep_number << 2)
1569 command->width = usb_maxpacket(urb->dev, urb->pipe,
1570 usb_pipeout(urb->pipe));
1571 command->follows = 0;
1573 command->buffer = NULL;
1574 target->callback = callback;
1575 target->endp = endp;
1578 ftdi->command_next += 1;
1579 ftdi_elan_kick_command_queue(ftdi);
1580 mutex_unlock(&ftdi->u132_lock);
1583 mutex_unlock(&ftdi->u132_lock);
1590 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1591 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1592 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1593 int toggle_bits, int error_count, int condition_code, int repeat_number,
1594 int halted, int skipped, int actual, int non_null))
1596 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1597 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1598 ep_number, toggle_bits, callback);
1602 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1603 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1604 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1605 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1606 int toggle_bits, int error_count, int condition_code, int repeat_number,
1607 int halted, int skipped, int actual, int non_null))
1609 u8 ed = ed_number - 1;
1610 wait:if (ftdi->disconnected > 0) {
1612 } else if (ftdi->initialized == 0) {
1616 mutex_lock(&ftdi->u132_lock);
1617 command_size = ftdi->command_next - ftdi->command_head;
1618 if (command_size < COMMAND_SIZE) {
1619 struct u132_target *target = &ftdi->target[ed];
1620 struct u132_command *command = &ftdi->command[
1621 COMMAND_MASK & ftdi->command_next];
1622 command->header = 0x81 | (ed << 5);
1623 command->length = 0x0000;
1624 command->address = (toggle_bits << 6) | (ep_number << 2)
1626 command->width = usb_maxpacket(urb->dev, urb->pipe,
1627 usb_pipeout(urb->pipe));
1628 command->follows = 0;
1630 command->buffer = NULL;
1631 target->callback = callback;
1632 target->endp = endp;
1635 ftdi->command_next += 1;
1636 ftdi_elan_kick_command_queue(ftdi);
1637 mutex_unlock(&ftdi->u132_lock);
1640 mutex_unlock(&ftdi->u132_lock);
1647 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1648 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1649 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1650 int toggle_bits, int error_count, int condition_code, int repeat_number,
1651 int halted, int skipped, int actual, int non_null))
1653 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1654 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1655 ep_number, toggle_bits, callback);
1659 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1660 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1661 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1662 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1663 int toggle_bits, int error_count, int condition_code, int repeat_number,
1664 int halted, int skipped, int actual, int non_null))
1666 u8 ed = ed_number - 1;
1667 wait:if (ftdi->disconnected > 0) {
1669 } else if (ftdi->initialized == 0) {
1673 mutex_lock(&ftdi->u132_lock);
1674 command_size = ftdi->command_next - ftdi->command_head;
1675 if (command_size < COMMAND_SIZE) {
1679 char data[30 *3 + 4];
1681 int m = (sizeof(data) - 1) / 3 - 1;
1683 struct u132_target *target = &ftdi->target[ed];
1684 struct u132_command *command = &ftdi->command[
1685 COMMAND_MASK & ftdi->command_next];
1686 command->header = 0x81 | (ed << 5);
1687 command->address = (toggle_bits << 6) | (ep_number << 2)
1689 command->width = usb_maxpacket(urb->dev, urb->pipe,
1690 usb_pipeout(urb->pipe));
1691 command->follows = min_t(u32, 1024,
1692 urb->transfer_buffer_length -
1693 urb->actual_length);
1695 command->buffer = urb->transfer_buffer +
1697 command->length = 0x8000 | (command->follows - 1);
1698 b = command->buffer;
1699 urb_size = command->follows;
1701 while (urb_size-- > 0) {
1703 } else if (i++ < m) {
1704 int w = sprintf(d, " %02X", *b++);
1708 d += sprintf(d, " ..");
1710 target->callback = callback;
1711 target->endp = endp;
1714 ftdi->command_next += 1;
1715 ftdi_elan_kick_command_queue(ftdi);
1716 mutex_unlock(&ftdi->u132_lock);
1719 mutex_unlock(&ftdi->u132_lock);
1726 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1727 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1728 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1729 int toggle_bits, int error_count, int condition_code, int repeat_number,
1730 int halted, int skipped, int actual, int non_null))
1732 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1733 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1734 ep_number, toggle_bits, callback);
1738 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1739 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1740 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1741 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1742 int toggle_bits, int error_count, int condition_code, int repeat_number,
1743 int halted, int skipped, int actual, int non_null))
1745 u8 ed = ed_number - 1;
1746 wait:if (ftdi->disconnected > 0) {
1748 } else if (ftdi->initialized == 0) {
1752 mutex_lock(&ftdi->u132_lock);
1753 command_size = ftdi->command_next - ftdi->command_head;
1754 if (command_size < COMMAND_SIZE) {
1755 u32 remaining_length = urb->transfer_buffer_length -
1757 struct u132_target *target = &ftdi->target[ed];
1758 struct u132_command *command = &ftdi->command[
1759 COMMAND_MASK & ftdi->command_next];
1760 command->header = 0x83 | (ed << 5);
1761 if (remaining_length == 0) {
1762 command->length = 0x0000;
1763 } else if (remaining_length > 1024) {
1764 command->length = 0x8000 | 1023;
1766 command->length = 0x8000 | (remaining_length -
1768 command->address = (toggle_bits << 6) | (ep_number << 2)
1770 command->width = usb_maxpacket(urb->dev, urb->pipe,
1771 usb_pipeout(urb->pipe));
1772 command->follows = 0;
1774 command->buffer = NULL;
1775 target->callback = callback;
1776 target->endp = endp;
1779 ftdi->command_next += 1;
1780 ftdi_elan_kick_command_queue(ftdi);
1781 mutex_unlock(&ftdi->u132_lock);
1784 mutex_unlock(&ftdi->u132_lock);
1791 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1792 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1793 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1794 int toggle_bits, int error_count, int condition_code, int repeat_number,
1795 int halted, int skipped, int actual, int non_null))
1797 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1798 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1799 ep_number, toggle_bits, callback);
1803 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1804 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1807 u8 ed = ed_number - 1;
1808 if (ftdi->disconnected > 0) {
1810 } else if (ftdi->initialized == 0) {
1813 struct u132_target *target = &ftdi->target[ed];
1814 mutex_lock(&ftdi->u132_lock);
1815 if (target->abandoning > 0) {
1816 mutex_unlock(&ftdi->u132_lock);
1819 target->abandoning = 1;
1820 wait_1:if (target->active == 1) {
1821 int command_size = ftdi->command_next -
1823 if (command_size < COMMAND_SIZE) {
1824 struct u132_command *command =
1825 &ftdi->command[COMMAND_MASK &
1826 ftdi->command_next];
1827 command->header = 0x80 | (ed << 5) |
1829 command->length = 0x00;
1830 command->address = 0x00;
1831 command->width = 0x00;
1832 command->follows = 0;
1834 command->buffer = &command->value;
1835 ftdi->command_next += 1;
1836 ftdi_elan_kick_command_queue(ftdi);
1838 mutex_unlock(&ftdi->u132_lock);
1840 mutex_lock(&ftdi->u132_lock);
1844 mutex_unlock(&ftdi->u132_lock);
1850 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1853 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1854 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1858 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1859 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1861 int retry_on_empty = 10;
1862 int retry_on_timeout = 5;
1863 int retry_on_status = 20;
1865 int packet_bytes = 0;
1866 int retval = usb_bulk_msg(ftdi->udev,
1867 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1868 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1869 &packet_bytes, 100);
1870 if (packet_bytes > 2) {
1871 char diag[30 *3 + 4];
1873 int m = (sizeof(diag) - 1) / 3 - 1;
1874 char *b = ftdi->bulk_in_buffer;
1877 while (packet_bytes-- > 0) {
1879 if (bytes_read < m) {
1880 d += sprintf(d, " %02X",
1882 } else if (bytes_read > m) {
1884 d += sprintf(d, " ..");
1889 } else if (packet_bytes > 1) {
1890 char s1 = ftdi->bulk_in_buffer[0];
1891 char s2 = ftdi->bulk_in_buffer[1];
1892 if (s1 == 0x31 && s2 == 0x60) {
1894 } else if (retry_on_status-- > 0) {
1897 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1900 } else if (packet_bytes > 0) {
1901 char b1 = ftdi->bulk_in_buffer[0];
1902 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1904 if (retry_on_status-- > 0) {
1907 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1910 } else if (retval == -ETIMEDOUT) {
1911 if (retry_on_timeout-- > 0) {
1914 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1917 } else if (retval == 0) {
1918 if (retry_on_empty-- > 0) {
1921 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1925 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1934 * send the long flush sequence
1937 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1944 urb = usb_alloc_urb(0, GFP_KERNEL);
1947 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1949 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1955 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1956 ftdi->bulk_out_endpointAddr), buf, i,
1957 ftdi_elan_write_bulk_callback, ftdi);
1958 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1959 retval = usb_submit_urb(urb, GFP_KERNEL);
1961 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1962 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1972 * send the reset sequence
1975 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1982 urb = usb_alloc_urb(0, GFP_KERNEL);
1985 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1987 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1995 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1996 ftdi->bulk_out_endpointAddr), buf, i,
1997 ftdi_elan_write_bulk_callback, ftdi);
1998 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1999 retval = usb_submit_urb(urb, GFP_KERNEL);
2001 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
2002 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
2010 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2014 int retry_on_timeout = 5;
2015 int retry_on_empty = 10;
2017 retval = ftdi_elan_flush_input_fifo(ftdi);
2020 ftdi->bulk_in_left = 0;
2021 ftdi->bulk_in_last = -1;
2022 while (long_stop-- > 0) {
2025 retval = ftdi_elan_synchronize_flush(ftdi);
2028 retval = ftdi_elan_flush_input_fifo(ftdi);
2031 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2037 int packet_bytes = 0;
2038 retval = usb_bulk_msg(ftdi->udev,
2039 usb_rcvbulkpipe(ftdi->udev,
2040 ftdi->bulk_in_endpointAddr),
2041 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2042 &packet_bytes, 500);
2043 if (packet_bytes > 2) {
2044 char diag[30 *3 + 4];
2046 int m = (sizeof(diag) - 1) / 3 - 1;
2047 char *b = ftdi->bulk_in_buffer;
2049 unsigned char c = 0;
2051 while (packet_bytes-- > 0) {
2053 if (bytes_read < m) {
2054 d += sprintf(d, " %02X", c);
2055 } else if (bytes_read > m) {
2057 d += sprintf(d, " ..");
2066 } else if (read_stop-- > 0) {
2069 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2073 } else if (packet_bytes > 1) {
2074 unsigned char s1 = ftdi->bulk_in_buffer[0];
2075 unsigned char s2 = ftdi->bulk_in_buffer[1];
2076 if (s1 == 0x31 && s2 == 0x00) {
2077 if (read_stuck-- > 0) {
2081 } else if (s1 == 0x31 && s2 == 0x60) {
2082 if (read_stop-- > 0) {
2085 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2089 if (read_stop-- > 0) {
2092 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2096 } else if (packet_bytes > 0) {
2097 if (read_stop-- > 0) {
2100 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2103 } else if (retval == -ETIMEDOUT) {
2104 if (retry_on_timeout-- > 0) {
2107 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2110 } else if (retval == 0) {
2111 if (retry_on_empty-- > 0) {
2114 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2119 dev_err(&ftdi->udev->dev, "error = %d\n",
2121 if (read_stop-- > 0) {
2124 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2130 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2134 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2136 int retry_on_empty = 10;
2137 int retry_on_timeout = 5;
2138 int retry_on_status = 50;
2140 int packet_bytes = 0;
2141 int retval = usb_bulk_msg(ftdi->udev,
2142 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2143 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2144 &packet_bytes, 1000);
2145 if (packet_bytes > 2) {
2146 char diag[30 *3 + 4];
2148 int m = (sizeof(diag) - 1) / 3 - 1;
2149 char *b = ftdi->bulk_in_buffer;
2152 while (packet_bytes-- > 0) {
2154 if (bytes_read < m) {
2155 d += sprintf(d, " %02X",
2157 } else if (bytes_read > m) {
2159 d += sprintf(d, " ..");
2164 } else if (packet_bytes > 1) {
2165 char s1 = ftdi->bulk_in_buffer[0];
2166 char s2 = ftdi->bulk_in_buffer[1];
2167 if (s1 == 0x31 && s2 == 0x60) {
2169 } else if (retry_on_status-- > 0) {
2174 } else if (packet_bytes > 0) {
2175 char b1 = ftdi->bulk_in_buffer[0];
2176 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2177 if (retry_on_status-- > 0) {
2181 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2184 } else if (retval == -ETIMEDOUT) {
2185 if (retry_on_timeout-- > 0) {
2188 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2191 } else if (retval == 0) {
2192 if (retry_on_empty-- > 0) {
2195 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2199 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2206 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2208 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2211 if (ftdi->controlreg & 0x00400000) {
2212 if (ftdi->card_ejected) {
2214 ftdi->card_ejected = 1;
2215 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2220 u8 fn = ftdi->function - 1;
2221 int activePCIfn = fn << 8;
2226 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2230 pciVID = pcidata & 0xFFFF;
2231 pciPID = (pcidata >> 16) & 0xFFFF;
2232 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2233 ftdi->platform_data.device) {
2236 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2237 ftdi->platform_data.vendor, pciVID,
2238 ftdi->platform_data.device, pciPID);
2245 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2246 offsetof(struct ohci_regs, member), 0, data);
2247 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2248 offsetof(struct ohci_regs, member), 0, data);
2250 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2251 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2253 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2267 int mask = OHCI_INTR_INIT;
2269 int reset_timeout = 30; /* ... allow extra time */
2271 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2274 retval = ftdi_read_pcimem(ftdi, control, &control);
2277 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2280 num_ports = rh_a & RH_A_NDP;
2281 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2284 hc_fminterval &= 0x3fff;
2285 if (hc_fminterval != FI) {
2287 hc_fminterval |= FSMP(hc_fminterval) << 16;
2288 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2291 switch (hc_control & OHCI_CTRL_HCFS) {
2295 case OHCI_USB_SUSPEND:
2296 case OHCI_USB_RESUME:
2297 hc_control &= OHCI_CTRL_RWC;
2298 hc_control |= OHCI_USB_RESUME;
2302 hc_control &= OHCI_CTRL_RWC;
2303 hc_control |= OHCI_USB_RESET;
2307 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2310 retval = ftdi_read_pcimem(ftdi, control, &control);
2314 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2317 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2318 for (temp = 0; temp < num_ports; temp++) {
2319 retval = ftdi_write_pcimem(ftdi,
2320 roothub.portstatus[temp], RH_PS_LSDA);
2325 retval = ftdi_read_pcimem(ftdi, control, &control);
2328 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2331 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2335 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2338 if (0 != (status & OHCI_HCR)) {
2339 if (--reset_timeout == 0) {
2340 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2348 if (quirk & OHCI_QUIRK_INITRESET) {
2349 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2352 retval = ftdi_read_pcimem(ftdi, control, &control);
2356 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2359 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2362 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2365 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2368 retval = ftdi_write_pcimem(ftdi, fminterval,
2369 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2372 retval = ftdi_write_pcimem(ftdi, periodicstart,
2373 ((9 *hc_fminterval) / 10) & 0x3fff);
2376 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2379 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2382 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2383 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2384 quirk |= OHCI_QUIRK_INITRESET;
2387 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2388 fminterval, periodicstart);
2389 } /* start controller operations */
2390 hc_control &= OHCI_CTRL_RWC;
2391 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2392 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2395 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2398 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2401 retval = ftdi_read_pcimem(ftdi, control, &control);
2404 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2407 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2410 retval = ftdi_write_pcimem(ftdi, intrdisable,
2411 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2412 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2415 return retval; /* handle root hub init quirks ... */
2416 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2419 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2420 if (quirk & OHCI_QUIRK_SUPERIO) {
2421 roothub_a |= RH_A_NOCP;
2422 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2423 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2426 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2427 roothub_a |= RH_A_NPS;
2428 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2432 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2435 retval = ftdi_write_pcimem(ftdi, roothub.b,
2436 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2439 retval = ftdi_read_pcimem(ftdi, control, &control);
2442 mdelay((roothub_a >> 23) & 0x1fe);
2443 for (temp = 0; temp < num_ports; temp++) {
2445 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2455 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2461 int activePCIfn = fn << 8;
2462 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2466 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2470 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2474 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2478 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2483 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2487 latence_timer &= 0xFFFF00FF;
2488 latence_timer |= 0x00001600;
2489 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2493 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2498 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2502 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2506 for (reg = 0; reg <= 0x54; reg += 4) {
2507 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2514 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2520 int activePCIfn = fn << 8;
2521 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2525 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2529 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2533 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2537 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2542 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2546 latence_timer &= 0xFFFF00FF;
2547 latence_timer |= 0x00001600;
2548 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2552 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2557 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2561 return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2564 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2568 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2571 result = ftdi_elan_check_controller(ftdi, quirk);
2572 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2578 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2583 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2586 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2590 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2593 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2596 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2599 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2602 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2606 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2609 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2612 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2615 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2618 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2622 sensebits = (controlreg >> 16) & 0x000F;
2623 if (0x0D == sensebits)
2629 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2635 int activePCIfn = 0;
2636 int max_devices = 0;
2637 int controllers = 0;
2638 int unrecognized = 0;
2640 for (fn = 0; (fn < 4); fn++) {
2644 activePCIfn = fn << 8;
2645 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2649 pciVID = pcidata & 0xFFFF;
2650 pciPID = (pcidata >> 16) & 0xFFFF;
2651 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2652 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2654 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2656 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2658 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2659 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2661 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2663 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2665 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2666 devices = ftdi_elan_found_controller(ftdi, fn,
2669 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2670 devices = ftdi_elan_found_controller(ftdi, fn,
2671 OHCI_QUIRK_ZFMICRO);
2673 } else if (0 == pcidata) {
2676 if (devices > max_devices) {
2677 max_devices = devices;
2678 ftdi->function = fn + 1;
2679 ftdi->platform_data.vendor = pciVID;
2680 ftdi->platform_data.device = pciPID;
2683 if (ftdi->function > 0) {
2684 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2685 } else if (controllers > 0) {
2687 } else if (unrecognized > 0) {
2690 ftdi->enumerated = 0;
2697 * we use only the first bulk-in and bulk-out endpoints
2699 static int ftdi_elan_probe(struct usb_interface *interface,
2700 const struct usb_device_id *id)
2702 struct usb_host_interface *iface_desc;
2703 struct usb_endpoint_descriptor *endpoint;
2706 int retval = -ENOMEM;
2707 struct usb_ftdi *ftdi;
2709 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2713 mutex_lock(&ftdi_module_lock);
2714 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2715 ftdi->sequence_num = ++ftdi_instances;
2716 mutex_unlock(&ftdi_module_lock);
2717 ftdi_elan_init_kref(ftdi);
2718 sema_init(&ftdi->sw_lock, 1);
2719 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2720 ftdi->interface = interface;
2721 mutex_init(&ftdi->u132_lock);
2723 iface_desc = interface->cur_altsetting;
2724 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2725 endpoint = &iface_desc->endpoint[i].desc;
2726 if (!ftdi->bulk_in_endpointAddr &&
2727 usb_endpoint_is_bulk_in(endpoint)) {
2728 buffer_size = usb_endpoint_maxp(endpoint);
2729 ftdi->bulk_in_size = buffer_size;
2730 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2731 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2732 if (!ftdi->bulk_in_buffer) {
2737 if (!ftdi->bulk_out_endpointAddr &&
2738 usb_endpoint_is_bulk_out(endpoint)) {
2739 ftdi->bulk_out_endpointAddr =
2740 endpoint->bEndpointAddress;
2743 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2744 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2748 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2749 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2750 ftdi->bulk_out_endpointAddr);
2751 usb_set_intfdata(interface, ftdi);
2752 if (iface_desc->desc.bInterfaceNumber == 0 &&
2753 ftdi->bulk_in_endpointAddr == 0x81 &&
2754 ftdi->bulk_out_endpointAddr == 0x02) {
2755 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2757 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2758 usb_set_intfdata(interface, NULL);
2762 ftdi->class = &ftdi_elan_jtag_class;
2763 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2764 ftdi, iface_desc->desc.bInterfaceNumber,
2768 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2769 ftdi->bulk_in_endpointAddr == 0x83 &&
2770 ftdi->bulk_out_endpointAddr == 0x04) {
2772 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2773 ftdi, iface_desc->desc.bInterfaceNumber);
2774 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2775 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2776 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2777 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2780 dev_err(&ftdi->udev->dev,
2781 "Could not find ELAN's U132 device\n");
2786 ftdi_elan_put_kref(ftdi);
2791 static void ftdi_elan_disconnect(struct usb_interface *interface)
2793 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2794 ftdi->disconnected += 1;
2796 int minor = interface->minor;
2797 struct usb_class_driver *class = ftdi->class;
2798 usb_set_intfdata(interface, NULL);
2799 usb_deregister_dev(interface, class);
2800 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2803 ftdi_status_cancel_work(ftdi);
2804 ftdi_command_cancel_work(ftdi);
2805 ftdi_response_cancel_work(ftdi);
2806 ftdi_elan_abandon_completions(ftdi);
2807 ftdi_elan_abandon_targets(ftdi);
2808 if (ftdi->registered) {
2809 platform_device_unregister(&ftdi->platform_dev);
2810 ftdi->synchronized = 0;
2811 ftdi->enumerated = 0;
2812 ftdi->initialized = 0;
2813 ftdi->registered = 0;
2815 ftdi->disconnected += 1;
2816 usb_set_intfdata(interface, NULL);
2817 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2819 ftdi_elan_put_kref(ftdi);
2822 static struct usb_driver ftdi_elan_driver = {
2823 .name = "ftdi-elan",
2824 .probe = ftdi_elan_probe,
2825 .disconnect = ftdi_elan_disconnect,
2826 .id_table = ftdi_elan_table,
2828 static int __init ftdi_elan_init(void)
2831 pr_info("driver %s\n", ftdi_elan_driver.name);
2832 mutex_init(&ftdi_module_lock);
2833 INIT_LIST_HEAD(&ftdi_static_list);
2834 result = usb_register(&ftdi_elan_driver);
2836 pr_err("usb_register failed. Error number %d\n", result);
2842 static void __exit ftdi_elan_exit(void)
2844 struct usb_ftdi *ftdi;
2845 struct usb_ftdi *temp;
2846 usb_deregister(&ftdi_elan_driver);
2847 pr_info("ftdi_u132 driver deregistered\n");
2848 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2849 ftdi_status_cancel_work(ftdi);
2850 ftdi_command_cancel_work(ftdi);
2851 ftdi_response_cancel_work(ftdi);
2856 module_init(ftdi_elan_init);
2857 module_exit(ftdi_elan_exit);