2 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
4 * Copyright(C) 2006 Elan Digital Systems Limited
5 * http://www.elandigitalsystems.com
7 * Author and Maintainer - Tony Olech - Elan Digital Systems
8 * tony.olech@elandigitalsystems.com
10 * This program is free software;you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2.
15 * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
16 * based on various USB client drivers in the 2.6.15 linux kernel
17 * with constant reference to the 3rd Edition of Linux Device Drivers
18 * published by O'Reilly
20 * The U132 adapter is a USB to CardBus adapter specifically designed
21 * for PC cards that contain an OHCI host controller. Typical PC cards
22 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
24 * The U132 adapter will *NOT *work with PC cards that do not contain
25 * an OHCI controller. A simple way to test whether a PC card has an
26 * OHCI controller as an interface is to insert the PC card directly
27 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
28 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
29 * then there is a good chance that the U132 adapter will support the
30 * PC card.(you also need the specific client driver for the PC card)
32 * Please inform the Author and Maintainer about any PC cards that
33 * contain OHCI Host Controller and work when directly connected to
34 * an embedded CardBus slot but do not work when they are connected
35 * via an ELAN U132 adapter.
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/kernel.h>
42 #include <linux/errno.h>
43 #include <linux/init.h>
44 #include <linux/list.h>
45 #include <linux/ioctl.h>
46 #include <linux/pci_ids.h>
47 #include <linux/slab.h>
48 #include <linux/module.h>
49 #include <linux/kref.h>
50 #include <linux/mutex.h>
51 #include <asm/uaccess.h>
52 #include <linux/usb.h>
53 #include <linux/workqueue.h>
54 #include <linux/platform_device.h>
55 MODULE_AUTHOR("Tony Olech");
56 MODULE_DESCRIPTION("FTDI ELAN driver");
57 MODULE_LICENSE("GPL");
58 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
59 static bool distrust_firmware = 1;
60 module_param(distrust_firmware, bool, 0);
61 MODULE_PARM_DESC(distrust_firmware,
62 "true to distrust firmware power/overcurrent setup");
63 extern struct platform_driver u132_platform_driver;
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;
212 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
214 kref_put(&ftdi->kref, ftdi_elan_delete);
217 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
219 kref_get(&ftdi->kref);
222 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
224 kref_init(&ftdi->kref);
227 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
229 if (!schedule_delayed_work(&ftdi->status_work, delta))
230 kref_put(&ftdi->kref, ftdi_elan_delete);
233 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
235 if (schedule_delayed_work(&ftdi->status_work, delta))
236 kref_get(&ftdi->kref);
239 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
241 if (cancel_delayed_work_sync(&ftdi->status_work))
242 kref_put(&ftdi->kref, ftdi_elan_delete);
245 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
247 if (!schedule_delayed_work(&ftdi->command_work, delta))
248 kref_put(&ftdi->kref, ftdi_elan_delete);
251 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
253 if (schedule_delayed_work(&ftdi->command_work, delta))
254 kref_get(&ftdi->kref);
257 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
259 if (cancel_delayed_work_sync(&ftdi->command_work))
260 kref_put(&ftdi->kref, ftdi_elan_delete);
263 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
266 if (!schedule_delayed_work(&ftdi->respond_work, delta))
267 kref_put(&ftdi->kref, ftdi_elan_delete);
270 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
272 if (schedule_delayed_work(&ftdi->respond_work, delta))
273 kref_get(&ftdi->kref);
276 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
278 if (cancel_delayed_work_sync(&ftdi->respond_work))
279 kref_put(&ftdi->kref, ftdi_elan_delete);
282 void ftdi_elan_gone_away(struct platform_device *pdev)
284 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
285 ftdi->gone_away += 1;
286 ftdi_elan_put_kref(ftdi);
290 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
291 static void ftdi_release_platform_dev(struct device *dev)
296 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
297 struct u132_target *target, u8 *buffer, int length);
298 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
299 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
300 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
301 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
302 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
303 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
304 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
305 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
306 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
307 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
310 if (ftdi->platform_dev.dev.parent)
312 ftdi_elan_get_kref(ftdi);
313 ftdi->platform_data.potpg = 100;
314 ftdi->platform_data.reset = NULL;
315 ftdi->platform_dev.id = ftdi->sequence_num;
316 ftdi->platform_dev.resource = ftdi->resources;
317 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
318 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
319 ftdi->platform_dev.dev.parent = NULL;
320 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
321 ftdi->platform_dev.dev.dma_mask = NULL;
322 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
323 ftdi->platform_dev.name = ftdi->device_name;
324 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
325 request_module("u132_hcd");
326 dev_info(&ftdi->udev->dev, "registering '%s'\n",
327 ftdi->platform_dev.name);
328 result = platform_device_register(&ftdi->platform_dev);
332 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
334 mutex_lock(&ftdi->u132_lock);
335 while (ftdi->respond_next > ftdi->respond_head) {
336 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
337 ftdi->respond_head++];
338 *respond->result = -ESHUTDOWN;
340 complete(&respond->wait_completion);
341 } mutex_unlock(&ftdi->u132_lock);
344 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
347 mutex_lock(&ftdi->u132_lock);
348 while (ed_number-- > 0) {
349 struct u132_target *target = &ftdi->target[ed_number];
350 if (target->active == 1) {
351 target->condition_code = TD_DEVNOTRESP;
352 mutex_unlock(&ftdi->u132_lock);
353 ftdi_elan_do_callback(ftdi, target, NULL, 0);
354 mutex_lock(&ftdi->u132_lock);
360 mutex_unlock(&ftdi->u132_lock);
363 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
366 mutex_lock(&ftdi->u132_lock);
367 while (ed_number-- > 0) {
368 struct u132_target *target = &ftdi->target[ed_number];
369 target->abandoning = 1;
370 wait_1:if (target->active == 1) {
371 int command_size = ftdi->command_next -
373 if (command_size < COMMAND_SIZE) {
374 struct u132_command *command = &ftdi->command[
375 COMMAND_MASK & ftdi->command_next];
376 command->header = 0x80 | (ed_number << 5) | 0x4;
377 command->length = 0x00;
378 command->address = 0x00;
379 command->width = 0x00;
380 command->follows = 0;
382 command->buffer = &command->value;
383 ftdi->command_next += 1;
384 ftdi_elan_kick_command_queue(ftdi);
386 mutex_unlock(&ftdi->u132_lock);
388 mutex_lock(&ftdi->u132_lock);
392 wait_2:if (target->active == 1) {
393 int command_size = ftdi->command_next -
395 if (command_size < COMMAND_SIZE) {
396 struct u132_command *command = &ftdi->command[
397 COMMAND_MASK & ftdi->command_next];
398 command->header = 0x90 | (ed_number << 5);
399 command->length = 0x00;
400 command->address = 0x00;
401 command->width = 0x00;
402 command->follows = 0;
404 command->buffer = &command->value;
405 ftdi->command_next += 1;
406 ftdi_elan_kick_command_queue(ftdi);
408 mutex_unlock(&ftdi->u132_lock);
410 mutex_lock(&ftdi->u132_lock);
418 mutex_unlock(&ftdi->u132_lock);
421 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
424 mutex_lock(&ftdi->u132_lock);
425 while (ed_number-- > 0) {
426 struct u132_target *target = &ftdi->target[ed_number];
427 target->abandoning = 1;
428 wait:if (target->active == 1) {
429 int command_size = ftdi->command_next -
431 if (command_size < COMMAND_SIZE) {
432 struct u132_command *command = &ftdi->command[
433 COMMAND_MASK & ftdi->command_next];
434 command->header = 0x80 | (ed_number << 5) | 0x4;
435 command->length = 0x00;
436 command->address = 0x00;
437 command->width = 0x00;
438 command->follows = 0;
440 command->buffer = &command->value;
441 ftdi->command_next += 1;
442 ftdi_elan_kick_command_queue(ftdi);
444 mutex_unlock(&ftdi->u132_lock);
446 mutex_lock(&ftdi->u132_lock);
454 mutex_unlock(&ftdi->u132_lock);
457 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
459 ftdi_command_queue_work(ftdi, 0);
462 static void ftdi_elan_command_work(struct work_struct *work)
464 struct usb_ftdi *ftdi =
465 container_of(work, struct usb_ftdi, command_work.work);
467 if (ftdi->disconnected > 0) {
468 ftdi_elan_put_kref(ftdi);
471 int retval = ftdi_elan_command_engine(ftdi);
472 if (retval == -ESHUTDOWN) {
473 ftdi->disconnected += 1;
474 } else if (retval == -ENODEV) {
475 ftdi->disconnected += 1;
477 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
478 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
483 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
485 ftdi_respond_queue_work(ftdi, 0);
488 static void ftdi_elan_respond_work(struct work_struct *work)
490 struct usb_ftdi *ftdi =
491 container_of(work, struct usb_ftdi, respond_work.work);
492 if (ftdi->disconnected > 0) {
493 ftdi_elan_put_kref(ftdi);
496 int retval = ftdi_elan_respond_engine(ftdi);
498 } else if (retval == -ESHUTDOWN) {
499 ftdi->disconnected += 1;
500 } else if (retval == -ENODEV) {
501 ftdi->disconnected += 1;
502 } else if (retval == -EILSEQ) {
503 ftdi->disconnected += 1;
505 ftdi->disconnected += 1;
506 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
508 if (ftdi->disconnected > 0) {
509 ftdi_elan_abandon_completions(ftdi);
510 ftdi_elan_abandon_targets(ftdi);
512 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
519 * the sw_lock is initially held and will be freed
520 * after the FTDI has been synchronized
523 static void ftdi_elan_status_work(struct work_struct *work)
525 struct usb_ftdi *ftdi =
526 container_of(work, struct usb_ftdi, status_work.work);
527 int work_delay_in_msec = 0;
528 if (ftdi->disconnected > 0) {
529 ftdi_elan_put_kref(ftdi);
531 } else if (ftdi->synchronized == 0) {
532 down(&ftdi->sw_lock);
533 if (ftdi_elan_synchronize(ftdi) == 0) {
534 ftdi->synchronized = 1;
535 ftdi_command_queue_work(ftdi, 1);
536 ftdi_respond_queue_work(ftdi, 1);
538 work_delay_in_msec = 100;
540 dev_err(&ftdi->udev->dev, "synchronize failed\n");
542 work_delay_in_msec = 10 *1000;
544 } else if (ftdi->stuck_status > 0) {
545 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
546 ftdi->stuck_status = 0;
547 ftdi->synchronized = 0;
548 } else if ((ftdi->stuck_status++ % 60) == 1) {
549 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
551 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
553 work_delay_in_msec = 100;
554 } else if (ftdi->enumerated == 0) {
555 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
556 ftdi->enumerated = 1;
557 work_delay_in_msec = 250;
559 work_delay_in_msec = 1000;
560 } else if (ftdi->initialized == 0) {
561 if (ftdi_elan_setupOHCI(ftdi) == 0) {
562 ftdi->initialized = 1;
563 work_delay_in_msec = 500;
565 dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
566 work_delay_in_msec = 1 *1000;
568 } else if (ftdi->registered == 0) {
569 work_delay_in_msec = 10;
570 if (ftdi_elan_hcd_init(ftdi) == 0) {
571 ftdi->registered = 1;
573 dev_err(&ftdi->udev->dev, "register failed\n");
574 work_delay_in_msec = 250;
576 if (ftdi_elan_checkingPCI(ftdi) == 0) {
577 work_delay_in_msec = 250;
578 } else if (ftdi->controlreg & 0x00400000) {
579 if (ftdi->gone_away > 0) {
580 dev_err(&ftdi->udev->dev, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
581 ftdi->platform_dev.dev.parent,
582 &ftdi->platform_dev.dev);
583 platform_device_unregister(&ftdi->platform_dev);
584 ftdi->platform_dev.dev.parent = NULL;
585 ftdi->registered = 0;
586 ftdi->enumerated = 0;
587 ftdi->card_ejected = 0;
588 ftdi->initialized = 0;
591 ftdi_elan_flush_targets(ftdi);
592 work_delay_in_msec = 250;
594 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n");
595 ftdi_elan_cancel_targets(ftdi);
596 work_delay_in_msec = 500;
597 ftdi->enumerated = 0;
598 ftdi->initialized = 0;
601 if (ftdi->disconnected > 0) {
602 ftdi_elan_put_kref(ftdi);
605 ftdi_status_requeue_work(ftdi,
606 msecs_to_jiffies(work_delay_in_msec));
613 * file_operations for the jtag interface
615 * the usage count for the device is incremented on open()
616 * and decremented on release()
618 static int ftdi_elan_open(struct inode *inode, struct file *file)
621 struct usb_interface *interface;
623 subminor = iminor(inode);
624 interface = usb_find_interface(&ftdi_elan_driver, subminor);
627 pr_err("can't find device for minor %d\n", subminor);
630 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
634 if (down_interruptible(&ftdi->sw_lock)) {
637 ftdi_elan_get_kref(ftdi);
638 file->private_data = ftdi;
645 static int ftdi_elan_release(struct inode *inode, struct file *file)
647 struct usb_ftdi *ftdi = file->private_data;
650 up(&ftdi->sw_lock); /* decrement the count on our device */
651 ftdi_elan_put_kref(ftdi);
658 * blocking bulk reads are used to get data from the device
661 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
662 size_t count, loff_t *ppos)
664 char data[30 *3 + 4];
666 int m = (sizeof(data) - 1) / 3 - 1;
668 int retry_on_empty = 10;
669 int retry_on_timeout = 5;
670 struct usb_ftdi *ftdi = file->private_data;
671 if (ftdi->disconnected > 0) {
675 have:if (ftdi->bulk_in_left > 0) {
677 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
678 ftdi->bulk_in_left -= 1;
679 if (bytes_read < m) {
680 d += sprintf(d, " %02X", 0x000000FF & *p);
681 } else if (bytes_read > m) {
683 d += sprintf(d, " ..");
684 if (copy_to_user(buffer++, p, 1)) {
693 more:if (count > 0) {
694 int packet_bytes = 0;
695 int retval = usb_bulk_msg(ftdi->udev,
696 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
697 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
699 if (packet_bytes > 2) {
700 ftdi->bulk_in_left = packet_bytes - 2;
701 ftdi->bulk_in_last = 1;
703 } else if (retval == -ETIMEDOUT) {
704 if (retry_on_timeout-- > 0) {
706 } else if (bytes_read > 0) {
710 } else if (retval == 0) {
711 if (retry_on_empty-- > 0) {
721 static void ftdi_elan_write_bulk_callback(struct urb *urb)
723 struct usb_ftdi *ftdi = urb->context;
724 int status = urb->status;
726 if (status && !(status == -ENOENT || status == -ECONNRESET ||
727 status == -ESHUTDOWN)) {
728 dev_err(&ftdi->udev->dev,
729 "urb=%p write bulk status received: %d\n", urb, status);
731 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
732 urb->transfer_buffer, urb->transfer_dma);
735 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
736 char *buf, int command_size, int total_size)
740 int I = command_size;
741 int i = ftdi->command_head;
743 struct u132_command *command = &ftdi->command[COMMAND_MASK &
745 int F = command->follows;
746 u8 *f = command->buffer;
747 if (command->header & 0x80) {
748 ed_commands |= 1 << (0x3 & (command->header >> 5));
750 buf[b++] = command->header;
751 buf[b++] = (command->length >> 0) & 0x00FF;
752 buf[b++] = (command->length >> 8) & 0x00FF;
753 buf[b++] = command->address;
754 buf[b++] = command->width;
762 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
765 int I = command_size;
766 int i = ftdi->command_head;
768 struct u132_command *command = &ftdi->command[COMMAND_MASK &
770 total_size += 5 + command->follows;
774 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
781 int command_size = ftdi->command_next - ftdi->command_head;
782 if (command_size == 0)
784 total_size = ftdi_elan_total_command_size(ftdi, command_size);
785 urb = usb_alloc_urb(0, GFP_KERNEL);
788 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
791 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
792 command_size, total_size);
796 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
797 command_size, total_size);
798 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
799 ftdi->bulk_out_endpointAddr), buf, total_size,
800 ftdi_elan_write_bulk_callback, ftdi);
801 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
803 char diag[40 *3 + 4];
807 int s = (sizeof(diag) - 1) / 3;
809 while (s-- > 0 && m-- > 0) {
810 if (s > 0 || m == 0) {
811 d += sprintf(d, " %02X", *c++);
813 d += sprintf(d, " ..");
816 retval = usb_submit_urb(urb, GFP_KERNEL);
818 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
819 retval, urb, command_size, total_size);
820 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
824 usb_free_urb(urb); /* release our reference to this urb,
825 the USB core will eventually free it entirely */
826 ftdi->command_head += command_size;
827 ftdi_elan_kick_respond_queue(ftdi);
831 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
832 struct u132_target *target, u8 *buffer, int length)
834 struct urb *urb = target->urb;
835 int halted = target->halted;
836 int skipped = target->skipped;
837 int actual = target->actual;
838 int non_null = target->non_null;
839 int toggle_bits = target->toggle_bits;
840 int error_count = target->error_count;
841 int condition_code = target->condition_code;
842 int repeat_number = target->repeat_number;
843 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
844 int, int, int, int) = target->callback;
846 target->callback = NULL;
847 (*callback) (target->endp, urb, buffer, length, toggle_bits,
848 error_count, condition_code, repeat_number, halted, skipped,
852 static char *have_ed_set_response(struct usb_ftdi *ftdi,
853 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
856 int payload = (ed_length >> 0) & 0x07FF;
857 mutex_lock(&ftdi->u132_lock);
859 target->non_null = (ed_length >> 15) & 0x0001;
860 target->repeat_number = (ed_length >> 11) & 0x000F;
861 if (ed_type == 0x02) {
862 if (payload == 0 || target->abandoning > 0) {
863 target->abandoning = 0;
864 mutex_unlock(&ftdi->u132_lock);
865 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
870 return ftdi->response;
872 ftdi->expected = 4 + payload;
874 mutex_unlock(&ftdi->u132_lock);
877 } else if (ed_type == 0x03) {
878 if (payload == 0 || target->abandoning > 0) {
879 target->abandoning = 0;
880 mutex_unlock(&ftdi->u132_lock);
881 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
886 return ftdi->response;
888 ftdi->expected = 4 + payload;
890 mutex_unlock(&ftdi->u132_lock);
893 } else if (ed_type == 0x01) {
894 target->abandoning = 0;
895 mutex_unlock(&ftdi->u132_lock);
896 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
901 return ftdi->response;
903 target->abandoning = 0;
904 mutex_unlock(&ftdi->u132_lock);
905 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
910 return ftdi->response;
914 static char *have_ed_get_response(struct usb_ftdi *ftdi,
915 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
918 mutex_lock(&ftdi->u132_lock);
919 target->condition_code = TD_DEVNOTRESP;
920 target->actual = (ed_length >> 0) & 0x01FF;
921 target->non_null = (ed_length >> 15) & 0x0001;
922 target->repeat_number = (ed_length >> 11) & 0x000F;
923 mutex_unlock(&ftdi->u132_lock);
925 ftdi_elan_do_callback(ftdi, target, NULL, 0);
926 target->abandoning = 0;
930 return ftdi->response;
935 * The engine tries to empty the FTDI fifo
937 * all responses found in the fifo data are dispatched thus
938 * the response buffer can only ever hold a maximum sized
939 * response from the Uxxx.
942 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
944 u8 *b = ftdi->response + ftdi->received;
946 int retry_on_empty = 1;
947 int retry_on_timeout = 3;
948 int empty_packets = 0;
950 int packet_bytes = 0;
951 int retval = usb_bulk_msg(ftdi->udev,
952 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
953 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
955 char diag[30 *3 + 4];
957 int m = packet_bytes;
958 u8 *c = ftdi->bulk_in_buffer;
959 int s = (sizeof(diag) - 1) / 3;
961 while (s-- > 0 && m-- > 0) {
962 if (s > 0 || m == 0) {
963 d += sprintf(d, " %02X", *c++);
965 d += sprintf(d, " ..");
967 if (packet_bytes > 2) {
968 ftdi->bulk_in_left = packet_bytes - 2;
969 ftdi->bulk_in_last = 1;
971 } else if (retval == -ETIMEDOUT) {
972 if (retry_on_timeout-- > 0) {
973 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
974 packet_bytes, bytes_read, diag);
976 } else if (bytes_read > 0) {
977 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
981 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
982 packet_bytes, bytes_read, diag);
985 } else if (retval == -EILSEQ) {
986 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
987 retval, packet_bytes, bytes_read, diag);
990 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
991 retval, packet_bytes, bytes_read, diag);
993 } else if (packet_bytes == 2) {
994 unsigned char s0 = ftdi->bulk_in_buffer[0];
995 unsigned char s1 = ftdi->bulk_in_buffer[1];
997 if (s0 == 0x31 && s1 == 0x60) {
998 if (retry_on_empty-- > 0) {
1002 } else if (s0 == 0x31 && s1 == 0x00) {
1003 if (retry_on_empty-- > 0) {
1008 if (retry_on_empty-- > 0) {
1013 } else if (packet_bytes == 1) {
1014 if (retry_on_empty-- > 0) {
1019 if (retry_on_empty-- > 0) {
1028 have:if (ftdi->bulk_in_left > 0) {
1029 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1031 ftdi->bulk_in_left -= 1;
1032 if (ftdi->received == 0 && c == 0xFF) {
1036 if (++ftdi->received < ftdi->expected) {
1038 } else if (ftdi->ed_found) {
1039 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1040 u16 ed_length = (ftdi->response[2] << 8) |
1042 struct u132_target *target = &ftdi->target[ed_number];
1043 int payload = (ed_length >> 0) & 0x07FF;
1044 char diag[30 *3 + 4];
1047 u8 *c = 4 + ftdi->response;
1048 int s = (sizeof(diag) - 1) / 3;
1050 while (s-- > 0 && m-- > 0) {
1051 if (s > 0 || m == 0) {
1052 d += sprintf(d, " %02X", *c++);
1054 d += sprintf(d, " ..");
1056 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1063 } else if (ftdi->expected == 8) {
1065 int respond_head = ftdi->respond_head++;
1066 struct u132_respond *respond = &ftdi->respond[
1067 RESPOND_MASK & respond_head];
1068 u32 data = ftdi->response[7];
1070 data |= ftdi->response[6];
1072 data |= ftdi->response[5];
1074 data |= ftdi->response[4];
1075 *respond->value = data;
1076 *respond->result = 0;
1077 complete(&respond->wait_completion);
1082 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1083 if (buscmd == 0x00) {
1084 } else if (buscmd == 0x02) {
1085 } else if (buscmd == 0x06) {
1086 } else if (buscmd == 0x0A) {
1088 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1092 if ((ftdi->response[0] & 0x80) == 0x00) {
1096 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1097 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1098 u16 ed_length = (ftdi->response[2] << 8) |
1100 struct u132_target *target = &ftdi->target[
1102 target->halted = (ftdi->response[0] >> 3) &
1104 target->skipped = (ftdi->response[0] >> 2) &
1106 target->toggle_bits = (ftdi->response[3] >> 6)
1108 target->error_count = (ftdi->response[3] >> 4)
1110 target->condition_code = (ftdi->response[
1112 if ((ftdi->response[0] & 0x10) == 0x00) {
1113 b = have_ed_set_response(ftdi, target,
1114 ed_length, ed_number, ed_type,
1118 b = have_ed_get_response(ftdi, target,
1119 ed_length, ed_number, ed_type,
1131 * create a urb, and a buffer for it, and copy the data to the urb
1134 static ssize_t ftdi_elan_write(struct file *file,
1135 const char __user *user_buffer, size_t count,
1141 struct usb_ftdi *ftdi = file->private_data;
1143 if (ftdi->disconnected > 0) {
1149 urb = usb_alloc_urb(0, GFP_KERNEL);
1154 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1155 &urb->transfer_dma);
1160 if (copy_from_user(buf, user_buffer, count)) {
1164 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1165 ftdi->bulk_out_endpointAddr), buf, count,
1166 ftdi_elan_write_bulk_callback, ftdi);
1167 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1168 retval = usb_submit_urb(urb, GFP_KERNEL);
1170 dev_err(&ftdi->udev->dev,
1171 "failed submitting write urb, error %d\n", retval);
1179 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1186 static const struct file_operations ftdi_elan_fops = {
1187 .owner = THIS_MODULE,
1188 .llseek = no_llseek,
1189 .read = ftdi_elan_read,
1190 .write = ftdi_elan_write,
1191 .open = ftdi_elan_open,
1192 .release = ftdi_elan_release,
1196 * usb class driver info in order to get a minor number from the usb core,
1197 * and to have the device registered with the driver core
1199 static struct usb_class_driver ftdi_elan_jtag_class = {
1200 .name = "ftdi-%d-jtag",
1201 .fops = &ftdi_elan_fops,
1202 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1206 * the following definitions are for the
1207 * ELAN FPGA state machgine processor that
1208 * lies on the other side of the FTDI chip
1210 #define cPCIu132rd 0x0
1211 #define cPCIu132wr 0x1
1212 #define cPCIiord 0x2
1213 #define cPCIiowr 0x3
1214 #define cPCImemrd 0x6
1215 #define cPCImemwr 0x7
1216 #define cPCIcfgrd 0xA
1217 #define cPCIcfgwr 0xB
1218 #define cPCInull 0xF
1219 #define cU132cmd_status 0x0
1220 #define cU132flash 0x1
1221 #define cPIDsetup 0x0
1224 #define cPIDinonce 0x3
1225 #define cCCnoerror 0x0
1227 #define cCCbitstuff 0x2
1228 #define cCCtoggle 0x3
1229 #define cCCstall 0x4
1230 #define cCCnoresp 0x5
1231 #define cCCbadpid1 0x6
1232 #define cCCbadpid2 0x7
1233 #define cCCdataoverrun 0x8
1234 #define cCCdataunderrun 0x9
1235 #define cCCbuffoverrun 0xC
1236 #define cCCbuffunderrun 0xD
1237 #define cCCnotaccessed 0xF
1238 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1240 wait:if (ftdi->disconnected > 0) {
1244 mutex_lock(&ftdi->u132_lock);
1245 command_size = ftdi->command_next - ftdi->command_head;
1246 if (command_size < COMMAND_SIZE) {
1247 struct u132_command *command = &ftdi->command[
1248 COMMAND_MASK & ftdi->command_next];
1249 command->header = 0x00 | cPCIu132wr;
1250 command->length = 0x04;
1251 command->address = 0x00;
1252 command->width = 0x00;
1253 command->follows = 4;
1254 command->value = data;
1255 command->buffer = &command->value;
1256 ftdi->command_next += 1;
1257 ftdi_elan_kick_command_queue(ftdi);
1258 mutex_unlock(&ftdi->u132_lock);
1261 mutex_unlock(&ftdi->u132_lock);
1268 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1271 u8 addressofs = config_offset / 4;
1272 wait:if (ftdi->disconnected > 0) {
1276 mutex_lock(&ftdi->u132_lock);
1277 command_size = ftdi->command_next - ftdi->command_head;
1278 if (command_size < COMMAND_SIZE) {
1279 struct u132_command *command = &ftdi->command[
1280 COMMAND_MASK & ftdi->command_next];
1281 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1282 command->length = 0x04;
1283 command->address = addressofs;
1284 command->width = 0x00 | (width & 0x0F);
1285 command->follows = 4;
1286 command->value = data;
1287 command->buffer = &command->value;
1288 ftdi->command_next += 1;
1289 ftdi_elan_kick_command_queue(ftdi);
1290 mutex_unlock(&ftdi->u132_lock);
1293 mutex_unlock(&ftdi->u132_lock);
1300 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1303 u8 addressofs = mem_offset / 4;
1304 wait:if (ftdi->disconnected > 0) {
1308 mutex_lock(&ftdi->u132_lock);
1309 command_size = ftdi->command_next - ftdi->command_head;
1310 if (command_size < COMMAND_SIZE) {
1311 struct u132_command *command = &ftdi->command[
1312 COMMAND_MASK & ftdi->command_next];
1313 command->header = 0x00 | (cPCImemwr & 0x0F);
1314 command->length = 0x04;
1315 command->address = addressofs;
1316 command->width = 0x00 | (width & 0x0F);
1317 command->follows = 4;
1318 command->value = data;
1319 command->buffer = &command->value;
1320 ftdi->command_next += 1;
1321 ftdi_elan_kick_command_queue(ftdi);
1322 mutex_unlock(&ftdi->u132_lock);
1325 mutex_unlock(&ftdi->u132_lock);
1332 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1335 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1336 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1340 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1341 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1343 wait:if (ftdi->disconnected > 0) {
1348 mutex_lock(&ftdi->u132_lock);
1349 command_size = ftdi->command_next - ftdi->command_head;
1350 respond_size = ftdi->respond_next - ftdi->respond_head;
1351 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1353 struct u132_command *command = &ftdi->command[
1354 COMMAND_MASK & ftdi->command_next];
1355 struct u132_respond *respond = &ftdi->respond[
1356 RESPOND_MASK & ftdi->respond_next];
1357 int result = -ENODEV;
1358 respond->result = &result;
1359 respond->header = command->header = 0x00 | cPCIu132rd;
1360 command->length = 0x04;
1361 respond->address = command->address = cU132cmd_status;
1362 command->width = 0x00;
1363 command->follows = 0;
1365 command->buffer = NULL;
1366 respond->value = data;
1367 init_completion(&respond->wait_completion);
1368 ftdi->command_next += 1;
1369 ftdi->respond_next += 1;
1370 ftdi_elan_kick_command_queue(ftdi);
1371 mutex_unlock(&ftdi->u132_lock);
1372 wait_for_completion(&respond->wait_completion);
1375 mutex_unlock(&ftdi->u132_lock);
1382 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1383 u8 width, u32 *data)
1385 u8 addressofs = config_offset / 4;
1386 wait:if (ftdi->disconnected > 0) {
1391 mutex_lock(&ftdi->u132_lock);
1392 command_size = ftdi->command_next - ftdi->command_head;
1393 respond_size = ftdi->respond_next - ftdi->respond_head;
1394 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1396 struct u132_command *command = &ftdi->command[
1397 COMMAND_MASK & ftdi->command_next];
1398 struct u132_respond *respond = &ftdi->respond[
1399 RESPOND_MASK & ftdi->respond_next];
1400 int result = -ENODEV;
1401 respond->result = &result;
1402 respond->header = command->header = 0x00 | (cPCIcfgrd &
1404 command->length = 0x04;
1405 respond->address = command->address = addressofs;
1406 command->width = 0x00 | (width & 0x0F);
1407 command->follows = 0;
1409 command->buffer = NULL;
1410 respond->value = data;
1411 init_completion(&respond->wait_completion);
1412 ftdi->command_next += 1;
1413 ftdi->respond_next += 1;
1414 ftdi_elan_kick_command_queue(ftdi);
1415 mutex_unlock(&ftdi->u132_lock);
1416 wait_for_completion(&respond->wait_completion);
1419 mutex_unlock(&ftdi->u132_lock);
1426 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1427 u8 width, u32 *data)
1429 u8 addressofs = mem_offset / 4;
1430 wait:if (ftdi->disconnected > 0) {
1435 mutex_lock(&ftdi->u132_lock);
1436 command_size = ftdi->command_next - ftdi->command_head;
1437 respond_size = ftdi->respond_next - ftdi->respond_head;
1438 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1440 struct u132_command *command = &ftdi->command[
1441 COMMAND_MASK & ftdi->command_next];
1442 struct u132_respond *respond = &ftdi->respond[
1443 RESPOND_MASK & ftdi->respond_next];
1444 int result = -ENODEV;
1445 respond->result = &result;
1446 respond->header = command->header = 0x00 | (cPCImemrd &
1448 command->length = 0x04;
1449 respond->address = command->address = addressofs;
1450 command->width = 0x00 | (width & 0x0F);
1451 command->follows = 0;
1453 command->buffer = NULL;
1454 respond->value = data;
1455 init_completion(&respond->wait_completion);
1456 ftdi->command_next += 1;
1457 ftdi->respond_next += 1;
1458 ftdi_elan_kick_command_queue(ftdi);
1459 mutex_unlock(&ftdi->u132_lock);
1460 wait_for_completion(&respond->wait_completion);
1463 mutex_unlock(&ftdi->u132_lock);
1470 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1471 u8 width, u32 *data)
1473 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1474 if (ftdi->initialized == 0) {
1477 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1481 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1482 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1483 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1484 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1485 int toggle_bits, int error_count, int condition_code, int repeat_number,
1486 int halted, int skipped, int actual, int non_null))
1488 u8 ed = ed_number - 1;
1489 wait:if (ftdi->disconnected > 0) {
1491 } else if (ftdi->initialized == 0) {
1495 mutex_lock(&ftdi->u132_lock);
1496 command_size = ftdi->command_next - ftdi->command_head;
1497 if (command_size < COMMAND_SIZE) {
1498 struct u132_target *target = &ftdi->target[ed];
1499 struct u132_command *command = &ftdi->command[
1500 COMMAND_MASK & ftdi->command_next];
1501 command->header = 0x80 | (ed << 5);
1502 command->length = 0x8007;
1503 command->address = (toggle_bits << 6) | (ep_number << 2)
1505 command->width = usb_maxpacket(urb->dev, urb->pipe,
1506 usb_pipeout(urb->pipe));
1507 command->follows = 8;
1509 command->buffer = urb->setup_packet;
1510 target->callback = callback;
1511 target->endp = endp;
1514 ftdi->command_next += 1;
1515 ftdi_elan_kick_command_queue(ftdi);
1516 mutex_unlock(&ftdi->u132_lock);
1519 mutex_unlock(&ftdi->u132_lock);
1526 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1527 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1528 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1529 int toggle_bits, int error_count, int condition_code, int repeat_number,
1530 int halted, int skipped, int actual, int non_null))
1532 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1533 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1534 ep_number, toggle_bits, callback);
1538 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1539 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1540 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1541 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1542 int toggle_bits, int error_count, int condition_code, int repeat_number,
1543 int halted, int skipped, int actual, int non_null))
1545 u8 ed = ed_number - 1;
1546 wait:if (ftdi->disconnected > 0) {
1548 } else if (ftdi->initialized == 0) {
1552 mutex_lock(&ftdi->u132_lock);
1553 command_size = ftdi->command_next - ftdi->command_head;
1554 if (command_size < COMMAND_SIZE) {
1555 struct u132_target *target = &ftdi->target[ed];
1556 struct u132_command *command = &ftdi->command[
1557 COMMAND_MASK & ftdi->command_next];
1558 u32 remaining_length = urb->transfer_buffer_length -
1560 command->header = 0x82 | (ed << 5);
1561 if (remaining_length == 0) {
1562 command->length = 0x0000;
1563 } else if (remaining_length > 1024) {
1564 command->length = 0x8000 | 1023;
1566 command->length = 0x8000 | (remaining_length -
1568 command->address = (toggle_bits << 6) | (ep_number << 2)
1570 command->width = usb_maxpacket(urb->dev, urb->pipe,
1571 usb_pipeout(urb->pipe));
1572 command->follows = 0;
1574 command->buffer = NULL;
1575 target->callback = callback;
1576 target->endp = endp;
1579 ftdi->command_next += 1;
1580 ftdi_elan_kick_command_queue(ftdi);
1581 mutex_unlock(&ftdi->u132_lock);
1584 mutex_unlock(&ftdi->u132_lock);
1591 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1592 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1593 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1594 int toggle_bits, int error_count, int condition_code, int repeat_number,
1595 int halted, int skipped, int actual, int non_null))
1597 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1598 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1599 ep_number, toggle_bits, callback);
1603 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1604 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1605 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1606 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1607 int toggle_bits, int error_count, int condition_code, int repeat_number,
1608 int halted, int skipped, int actual, int non_null))
1610 u8 ed = ed_number - 1;
1611 wait:if (ftdi->disconnected > 0) {
1613 } else if (ftdi->initialized == 0) {
1617 mutex_lock(&ftdi->u132_lock);
1618 command_size = ftdi->command_next - ftdi->command_head;
1619 if (command_size < COMMAND_SIZE) {
1620 struct u132_target *target = &ftdi->target[ed];
1621 struct u132_command *command = &ftdi->command[
1622 COMMAND_MASK & ftdi->command_next];
1623 command->header = 0x81 | (ed << 5);
1624 command->length = 0x0000;
1625 command->address = (toggle_bits << 6) | (ep_number << 2)
1627 command->width = usb_maxpacket(urb->dev, urb->pipe,
1628 usb_pipeout(urb->pipe));
1629 command->follows = 0;
1631 command->buffer = NULL;
1632 target->callback = callback;
1633 target->endp = endp;
1636 ftdi->command_next += 1;
1637 ftdi_elan_kick_command_queue(ftdi);
1638 mutex_unlock(&ftdi->u132_lock);
1641 mutex_unlock(&ftdi->u132_lock);
1648 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1649 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1650 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1651 int toggle_bits, int error_count, int condition_code, int repeat_number,
1652 int halted, int skipped, int actual, int non_null))
1654 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1655 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1656 ep_number, toggle_bits, callback);
1660 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1661 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1662 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1663 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1664 int toggle_bits, int error_count, int condition_code, int repeat_number,
1665 int halted, int skipped, int actual, int non_null))
1667 u8 ed = ed_number - 1;
1668 wait:if (ftdi->disconnected > 0) {
1670 } else if (ftdi->initialized == 0) {
1674 mutex_lock(&ftdi->u132_lock);
1675 command_size = ftdi->command_next - ftdi->command_head;
1676 if (command_size < COMMAND_SIZE) {
1680 char data[30 *3 + 4];
1682 int m = (sizeof(data) - 1) / 3 - 1;
1684 struct u132_target *target = &ftdi->target[ed];
1685 struct u132_command *command = &ftdi->command[
1686 COMMAND_MASK & ftdi->command_next];
1687 command->header = 0x81 | (ed << 5);
1688 command->address = (toggle_bits << 6) | (ep_number << 2)
1690 command->width = usb_maxpacket(urb->dev, urb->pipe,
1691 usb_pipeout(urb->pipe));
1692 command->follows = min_t(u32, 1024,
1693 urb->transfer_buffer_length -
1694 urb->actual_length);
1696 command->buffer = urb->transfer_buffer +
1698 command->length = 0x8000 | (command->follows - 1);
1699 b = command->buffer;
1700 urb_size = command->follows;
1702 while (urb_size-- > 0) {
1704 } else if (i++ < m) {
1705 int w = sprintf(d, " %02X", *b++);
1709 d += sprintf(d, " ..");
1711 target->callback = callback;
1712 target->endp = endp;
1715 ftdi->command_next += 1;
1716 ftdi_elan_kick_command_queue(ftdi);
1717 mutex_unlock(&ftdi->u132_lock);
1720 mutex_unlock(&ftdi->u132_lock);
1727 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1728 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1729 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1730 int toggle_bits, int error_count, int condition_code, int repeat_number,
1731 int halted, int skipped, int actual, int non_null))
1733 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1734 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1735 ep_number, toggle_bits, callback);
1739 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1740 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1741 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1742 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1743 int toggle_bits, int error_count, int condition_code, int repeat_number,
1744 int halted, int skipped, int actual, int non_null))
1746 u8 ed = ed_number - 1;
1747 wait:if (ftdi->disconnected > 0) {
1749 } else if (ftdi->initialized == 0) {
1753 mutex_lock(&ftdi->u132_lock);
1754 command_size = ftdi->command_next - ftdi->command_head;
1755 if (command_size < COMMAND_SIZE) {
1756 u32 remaining_length = urb->transfer_buffer_length -
1758 struct u132_target *target = &ftdi->target[ed];
1759 struct u132_command *command = &ftdi->command[
1760 COMMAND_MASK & ftdi->command_next];
1761 command->header = 0x83 | (ed << 5);
1762 if (remaining_length == 0) {
1763 command->length = 0x0000;
1764 } else if (remaining_length > 1024) {
1765 command->length = 0x8000 | 1023;
1767 command->length = 0x8000 | (remaining_length -
1769 command->address = (toggle_bits << 6) | (ep_number << 2)
1771 command->width = usb_maxpacket(urb->dev, urb->pipe,
1772 usb_pipeout(urb->pipe));
1773 command->follows = 0;
1775 command->buffer = NULL;
1776 target->callback = callback;
1777 target->endp = endp;
1780 ftdi->command_next += 1;
1781 ftdi_elan_kick_command_queue(ftdi);
1782 mutex_unlock(&ftdi->u132_lock);
1785 mutex_unlock(&ftdi->u132_lock);
1792 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1793 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1794 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1795 int toggle_bits, int error_count, int condition_code, int repeat_number,
1796 int halted, int skipped, int actual, int non_null))
1798 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1799 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1800 ep_number, toggle_bits, callback);
1804 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1805 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1808 u8 ed = ed_number - 1;
1809 if (ftdi->disconnected > 0) {
1811 } else if (ftdi->initialized == 0) {
1814 struct u132_target *target = &ftdi->target[ed];
1815 mutex_lock(&ftdi->u132_lock);
1816 if (target->abandoning > 0) {
1817 mutex_unlock(&ftdi->u132_lock);
1820 target->abandoning = 1;
1821 wait_1:if (target->active == 1) {
1822 int command_size = ftdi->command_next -
1824 if (command_size < COMMAND_SIZE) {
1825 struct u132_command *command =
1826 &ftdi->command[COMMAND_MASK &
1827 ftdi->command_next];
1828 command->header = 0x80 | (ed << 5) |
1830 command->length = 0x00;
1831 command->address = 0x00;
1832 command->width = 0x00;
1833 command->follows = 0;
1835 command->buffer = &command->value;
1836 ftdi->command_next += 1;
1837 ftdi_elan_kick_command_queue(ftdi);
1839 mutex_unlock(&ftdi->u132_lock);
1841 mutex_lock(&ftdi->u132_lock);
1845 mutex_unlock(&ftdi->u132_lock);
1851 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1854 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1855 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1859 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1860 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1862 int retry_on_empty = 10;
1863 int retry_on_timeout = 5;
1864 int retry_on_status = 20;
1866 int packet_bytes = 0;
1867 int retval = usb_bulk_msg(ftdi->udev,
1868 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1869 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1870 &packet_bytes, 100);
1871 if (packet_bytes > 2) {
1872 char diag[30 *3 + 4];
1874 int m = (sizeof(diag) - 1) / 3 - 1;
1875 char *b = ftdi->bulk_in_buffer;
1878 while (packet_bytes-- > 0) {
1880 if (bytes_read < m) {
1881 d += sprintf(d, " %02X",
1883 } else if (bytes_read > m) {
1885 d += sprintf(d, " ..");
1890 } else if (packet_bytes > 1) {
1891 char s1 = ftdi->bulk_in_buffer[0];
1892 char s2 = ftdi->bulk_in_buffer[1];
1893 if (s1 == 0x31 && s2 == 0x60) {
1895 } else if (retry_on_status-- > 0) {
1898 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1901 } else if (packet_bytes > 0) {
1902 char b1 = ftdi->bulk_in_buffer[0];
1903 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1905 if (retry_on_status-- > 0) {
1908 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1911 } else if (retval == -ETIMEDOUT) {
1912 if (retry_on_timeout-- > 0) {
1915 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1918 } else if (retval == 0) {
1919 if (retry_on_empty-- > 0) {
1922 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1926 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1935 * send the long flush sequence
1938 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1945 urb = usb_alloc_urb(0, GFP_KERNEL);
1948 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1950 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1956 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1957 ftdi->bulk_out_endpointAddr), buf, i,
1958 ftdi_elan_write_bulk_callback, ftdi);
1959 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1960 retval = usb_submit_urb(urb, GFP_KERNEL);
1962 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1963 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1973 * send the reset sequence
1976 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1983 urb = usb_alloc_urb(0, GFP_KERNEL);
1986 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1988 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1996 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1997 ftdi->bulk_out_endpointAddr), buf, i,
1998 ftdi_elan_write_bulk_callback, ftdi);
1999 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2000 retval = usb_submit_urb(urb, GFP_KERNEL);
2002 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
2003 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
2011 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2015 int retry_on_timeout = 5;
2016 int retry_on_empty = 10;
2018 retval = ftdi_elan_flush_input_fifo(ftdi);
2021 ftdi->bulk_in_left = 0;
2022 ftdi->bulk_in_last = -1;
2023 while (long_stop-- > 0) {
2026 retval = ftdi_elan_synchronize_flush(ftdi);
2029 retval = ftdi_elan_flush_input_fifo(ftdi);
2032 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2038 int packet_bytes = 0;
2039 retval = usb_bulk_msg(ftdi->udev,
2040 usb_rcvbulkpipe(ftdi->udev,
2041 ftdi->bulk_in_endpointAddr),
2042 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2043 &packet_bytes, 500);
2044 if (packet_bytes > 2) {
2045 char diag[30 *3 + 4];
2047 int m = (sizeof(diag) - 1) / 3 - 1;
2048 char *b = ftdi->bulk_in_buffer;
2050 unsigned char c = 0;
2052 while (packet_bytes-- > 0) {
2054 if (bytes_read < m) {
2055 d += sprintf(d, " %02X", c);
2056 } else if (bytes_read > m) {
2058 d += sprintf(d, " ..");
2067 } else if (read_stop-- > 0) {
2070 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2074 } else if (packet_bytes > 1) {
2075 unsigned char s1 = ftdi->bulk_in_buffer[0];
2076 unsigned char s2 = ftdi->bulk_in_buffer[1];
2077 if (s1 == 0x31 && s2 == 0x00) {
2078 if (read_stuck-- > 0) {
2082 } else if (s1 == 0x31 && s2 == 0x60) {
2083 if (read_stop-- > 0) {
2086 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2090 if (read_stop-- > 0) {
2093 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2097 } else if (packet_bytes > 0) {
2098 if (read_stop-- > 0) {
2101 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2104 } else if (retval == -ETIMEDOUT) {
2105 if (retry_on_timeout-- > 0) {
2108 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2111 } else if (retval == 0) {
2112 if (retry_on_empty-- > 0) {
2115 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2120 dev_err(&ftdi->udev->dev, "error = %d\n",
2122 if (read_stop-- > 0) {
2125 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2131 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2135 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2137 int retry_on_empty = 10;
2138 int retry_on_timeout = 5;
2139 int retry_on_status = 50;
2141 int packet_bytes = 0;
2142 int retval = usb_bulk_msg(ftdi->udev,
2143 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2144 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2145 &packet_bytes, 1000);
2146 if (packet_bytes > 2) {
2147 char diag[30 *3 + 4];
2149 int m = (sizeof(diag) - 1) / 3 - 1;
2150 char *b = ftdi->bulk_in_buffer;
2153 while (packet_bytes-- > 0) {
2155 if (bytes_read < m) {
2156 d += sprintf(d, " %02X",
2158 } else if (bytes_read > m) {
2160 d += sprintf(d, " ..");
2165 } else if (packet_bytes > 1) {
2166 char s1 = ftdi->bulk_in_buffer[0];
2167 char s2 = ftdi->bulk_in_buffer[1];
2168 if (s1 == 0x31 && s2 == 0x60) {
2170 } else if (retry_on_status-- > 0) {
2175 } else if (packet_bytes > 0) {
2176 char b1 = ftdi->bulk_in_buffer[0];
2177 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2178 if (retry_on_status-- > 0) {
2182 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2185 } else if (retval == -ETIMEDOUT) {
2186 if (retry_on_timeout-- > 0) {
2189 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2192 } else if (retval == 0) {
2193 if (retry_on_empty-- > 0) {
2196 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2200 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2207 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2209 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2212 if (ftdi->controlreg & 0x00400000) {
2213 if (ftdi->card_ejected) {
2215 ftdi->card_ejected = 1;
2216 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2221 u8 fn = ftdi->function - 1;
2222 int activePCIfn = fn << 8;
2227 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2231 pciVID = pcidata & 0xFFFF;
2232 pciPID = (pcidata >> 16) & 0xFFFF;
2233 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2234 ftdi->platform_data.device) {
2237 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2238 ftdi->platform_data.vendor, pciVID,
2239 ftdi->platform_data.device, pciPID);
2246 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2247 offsetof(struct ohci_regs, member), 0, data);
2248 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2249 offsetof(struct ohci_regs, member), 0, data);
2251 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2252 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2254 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2268 int mask = OHCI_INTR_INIT;
2270 int reset_timeout = 30; /* ... allow extra time */
2272 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2275 retval = ftdi_read_pcimem(ftdi, control, &control);
2278 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2281 num_ports = rh_a & RH_A_NDP;
2282 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2285 hc_fminterval &= 0x3fff;
2286 if (hc_fminterval != FI) {
2288 hc_fminterval |= FSMP(hc_fminterval) << 16;
2289 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2292 switch (hc_control & OHCI_CTRL_HCFS) {
2296 case OHCI_USB_SUSPEND:
2297 case OHCI_USB_RESUME:
2298 hc_control &= OHCI_CTRL_RWC;
2299 hc_control |= OHCI_USB_RESUME;
2303 hc_control &= OHCI_CTRL_RWC;
2304 hc_control |= OHCI_USB_RESET;
2308 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2311 retval = ftdi_read_pcimem(ftdi, control, &control);
2315 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2318 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2319 for (temp = 0; temp < num_ports; temp++) {
2320 retval = ftdi_write_pcimem(ftdi,
2321 roothub.portstatus[temp], RH_PS_LSDA);
2326 retval = ftdi_read_pcimem(ftdi, control, &control);
2329 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2332 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2336 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2339 if (0 != (status & OHCI_HCR)) {
2340 if (--reset_timeout == 0) {
2341 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2349 if (quirk & OHCI_QUIRK_INITRESET) {
2350 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2353 retval = ftdi_read_pcimem(ftdi, control, &control);
2357 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2360 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2363 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2366 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2369 retval = ftdi_write_pcimem(ftdi, fminterval,
2370 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2373 retval = ftdi_write_pcimem(ftdi, periodicstart,
2374 ((9 *hc_fminterval) / 10) & 0x3fff);
2377 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2380 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2383 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2384 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2385 quirk |= OHCI_QUIRK_INITRESET;
2388 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2389 fminterval, periodicstart);
2390 } /* start controller operations */
2391 hc_control &= OHCI_CTRL_RWC;
2392 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2393 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2396 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2399 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2402 retval = ftdi_read_pcimem(ftdi, control, &control);
2405 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2408 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2411 retval = ftdi_write_pcimem(ftdi, intrdisable,
2412 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2413 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2416 return retval; /* handle root hub init quirks ... */
2417 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2420 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2421 if (quirk & OHCI_QUIRK_SUPERIO) {
2422 roothub_a |= RH_A_NOCP;
2423 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2424 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2427 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2428 roothub_a |= RH_A_NPS;
2429 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2433 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2436 retval = ftdi_write_pcimem(ftdi, roothub.b,
2437 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2440 retval = ftdi_read_pcimem(ftdi, control, &control);
2443 mdelay((roothub_a >> 23) & 0x1fe);
2444 for (temp = 0; temp < num_ports; temp++) {
2446 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2456 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2462 int activePCIfn = fn << 8;
2463 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2467 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2471 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2475 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2479 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2484 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2488 latence_timer &= 0xFFFF00FF;
2489 latence_timer |= 0x00001600;
2490 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2494 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2499 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2503 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2507 for (reg = 0; reg <= 0x54; reg += 4) {
2508 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2515 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2521 int activePCIfn = fn << 8;
2522 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2526 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2530 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2534 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2538 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2543 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2547 latence_timer &= 0xFFFF00FF;
2548 latence_timer |= 0x00001600;
2549 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2553 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2558 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2562 return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2565 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2569 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2572 result = ftdi_elan_check_controller(ftdi, quirk);
2573 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2579 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2584 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2587 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2591 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2594 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2597 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2600 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2603 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2607 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2610 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2613 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2616 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2619 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2623 sensebits = (controlreg >> 16) & 0x000F;
2624 if (0x0D == sensebits)
2630 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2636 int activePCIfn = 0;
2637 int max_devices = 0;
2638 int controllers = 0;
2639 int unrecognized = 0;
2641 for (fn = 0; (fn < 4); fn++) {
2645 activePCIfn = fn << 8;
2646 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2650 pciVID = pcidata & 0xFFFF;
2651 pciPID = (pcidata >> 16) & 0xFFFF;
2652 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2653 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2655 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2657 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2659 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2660 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2662 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2664 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2666 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2667 devices = ftdi_elan_found_controller(ftdi, fn,
2670 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2671 devices = ftdi_elan_found_controller(ftdi, fn,
2672 OHCI_QUIRK_ZFMICRO);
2674 } else if (0 == pcidata) {
2677 if (devices > max_devices) {
2678 max_devices = devices;
2679 ftdi->function = fn + 1;
2680 ftdi->platform_data.vendor = pciVID;
2681 ftdi->platform_data.device = pciPID;
2684 if (ftdi->function > 0) {
2685 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2686 } else if (controllers > 0) {
2688 } else if (unrecognized > 0) {
2691 ftdi->enumerated = 0;
2698 * we use only the first bulk-in and bulk-out endpoints
2700 static int ftdi_elan_probe(struct usb_interface *interface,
2701 const struct usb_device_id *id)
2703 struct usb_host_interface *iface_desc;
2704 struct usb_endpoint_descriptor *endpoint;
2707 int retval = -ENOMEM;
2708 struct usb_ftdi *ftdi;
2710 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2714 mutex_lock(&ftdi_module_lock);
2715 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2716 ftdi->sequence_num = ++ftdi_instances;
2717 mutex_unlock(&ftdi_module_lock);
2718 ftdi_elan_init_kref(ftdi);
2719 sema_init(&ftdi->sw_lock, 1);
2720 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2721 ftdi->interface = interface;
2722 mutex_init(&ftdi->u132_lock);
2724 iface_desc = interface->cur_altsetting;
2725 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2726 endpoint = &iface_desc->endpoint[i].desc;
2727 if (!ftdi->bulk_in_endpointAddr &&
2728 usb_endpoint_is_bulk_in(endpoint)) {
2729 buffer_size = usb_endpoint_maxp(endpoint);
2730 ftdi->bulk_in_size = buffer_size;
2731 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2732 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2733 if (!ftdi->bulk_in_buffer) {
2738 if (!ftdi->bulk_out_endpointAddr &&
2739 usb_endpoint_is_bulk_out(endpoint)) {
2740 ftdi->bulk_out_endpointAddr =
2741 endpoint->bEndpointAddress;
2744 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2745 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2749 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2750 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2751 ftdi->bulk_out_endpointAddr);
2752 usb_set_intfdata(interface, ftdi);
2753 if (iface_desc->desc.bInterfaceNumber == 0 &&
2754 ftdi->bulk_in_endpointAddr == 0x81 &&
2755 ftdi->bulk_out_endpointAddr == 0x02) {
2756 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2758 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2759 usb_set_intfdata(interface, NULL);
2763 ftdi->class = &ftdi_elan_jtag_class;
2764 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2765 ftdi, iface_desc->desc.bInterfaceNumber,
2769 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2770 ftdi->bulk_in_endpointAddr == 0x83 &&
2771 ftdi->bulk_out_endpointAddr == 0x04) {
2773 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2774 ftdi, iface_desc->desc.bInterfaceNumber);
2775 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2776 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2777 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2778 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2781 dev_err(&ftdi->udev->dev,
2782 "Could not find ELAN's U132 device\n");
2787 ftdi_elan_put_kref(ftdi);
2792 static void ftdi_elan_disconnect(struct usb_interface *interface)
2794 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2795 ftdi->disconnected += 1;
2797 int minor = interface->minor;
2798 struct usb_class_driver *class = ftdi->class;
2799 usb_set_intfdata(interface, NULL);
2800 usb_deregister_dev(interface, class);
2801 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2804 ftdi_status_cancel_work(ftdi);
2805 ftdi_command_cancel_work(ftdi);
2806 ftdi_response_cancel_work(ftdi);
2807 ftdi_elan_abandon_completions(ftdi);
2808 ftdi_elan_abandon_targets(ftdi);
2809 if (ftdi->registered) {
2810 platform_device_unregister(&ftdi->platform_dev);
2811 ftdi->synchronized = 0;
2812 ftdi->enumerated = 0;
2813 ftdi->initialized = 0;
2814 ftdi->registered = 0;
2816 ftdi->disconnected += 1;
2817 usb_set_intfdata(interface, NULL);
2818 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2820 ftdi_elan_put_kref(ftdi);
2823 static struct usb_driver ftdi_elan_driver = {
2824 .name = "ftdi-elan",
2825 .probe = ftdi_elan_probe,
2826 .disconnect = ftdi_elan_disconnect,
2827 .id_table = ftdi_elan_table,
2829 static int __init ftdi_elan_init(void)
2832 pr_info("driver %s\n", ftdi_elan_driver.name);
2833 mutex_init(&ftdi_module_lock);
2834 INIT_LIST_HEAD(&ftdi_static_list);
2835 result = usb_register(&ftdi_elan_driver);
2837 pr_err("usb_register failed. Error number %d\n", result);
2843 static void __exit ftdi_elan_exit(void)
2845 struct usb_ftdi *ftdi;
2846 struct usb_ftdi *temp;
2847 usb_deregister(&ftdi_elan_driver);
2848 pr_info("ftdi_u132 driver deregistered\n");
2849 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2850 ftdi_status_cancel_work(ftdi);
2851 ftdi_command_cancel_work(ftdi);
2852 ftdi_response_cancel_work(ftdi);
2857 module_init(ftdi_elan_init);
2858 module_exit(ftdi_elan_exit);