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;
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)
309 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);
329 return 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 || ed_type == 0x03) {
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);
878 target->abandoning = 0;
879 mutex_unlock(&ftdi->u132_lock);
880 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
885 return ftdi->response;
889 static char *have_ed_get_response(struct usb_ftdi *ftdi,
890 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
893 mutex_lock(&ftdi->u132_lock);
894 target->condition_code = TD_DEVNOTRESP;
895 target->actual = (ed_length >> 0) & 0x01FF;
896 target->non_null = (ed_length >> 15) & 0x0001;
897 target->repeat_number = (ed_length >> 11) & 0x000F;
898 mutex_unlock(&ftdi->u132_lock);
900 ftdi_elan_do_callback(ftdi, target, NULL, 0);
901 target->abandoning = 0;
905 return ftdi->response;
910 * The engine tries to empty the FTDI fifo
912 * all responses found in the fifo data are dispatched thus
913 * the response buffer can only ever hold a maximum sized
914 * response from the Uxxx.
917 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
919 u8 *b = ftdi->response + ftdi->received;
921 int retry_on_empty = 1;
922 int retry_on_timeout = 3;
923 int empty_packets = 0;
925 int packet_bytes = 0;
926 int retval = usb_bulk_msg(ftdi->udev,
927 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
928 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
930 char diag[30 *3 + 4];
932 int m = packet_bytes;
933 u8 *c = ftdi->bulk_in_buffer;
934 int s = (sizeof(diag) - 1) / 3;
936 while (s-- > 0 && m-- > 0) {
937 if (s > 0 || m == 0) {
938 d += sprintf(d, " %02X", *c++);
940 d += sprintf(d, " ..");
942 if (packet_bytes > 2) {
943 ftdi->bulk_in_left = packet_bytes - 2;
944 ftdi->bulk_in_last = 1;
946 } else if (retval == -ETIMEDOUT) {
947 if (retry_on_timeout-- > 0) {
948 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
949 packet_bytes, bytes_read, diag);
951 } else if (bytes_read > 0) {
952 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
956 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
957 packet_bytes, bytes_read, diag);
960 } else if (retval == -EILSEQ) {
961 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
962 retval, packet_bytes, bytes_read, diag);
965 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
966 retval, packet_bytes, bytes_read, diag);
968 } else if (packet_bytes == 2) {
969 unsigned char s0 = ftdi->bulk_in_buffer[0];
970 unsigned char s1 = ftdi->bulk_in_buffer[1];
972 if (s0 == 0x31 && s1 == 0x60) {
973 if (retry_on_empty-- > 0) {
977 } else if (s0 == 0x31 && s1 == 0x00) {
978 if (retry_on_empty-- > 0) {
983 if (retry_on_empty-- > 0) {
988 } else if (packet_bytes == 1) {
989 if (retry_on_empty-- > 0) {
994 if (retry_on_empty-- > 0) {
1003 have:if (ftdi->bulk_in_left > 0) {
1004 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1006 ftdi->bulk_in_left -= 1;
1007 if (ftdi->received == 0 && c == 0xFF) {
1011 if (++ftdi->received < ftdi->expected) {
1013 } else if (ftdi->ed_found) {
1014 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1015 u16 ed_length = (ftdi->response[2] << 8) |
1017 struct u132_target *target = &ftdi->target[ed_number];
1018 int payload = (ed_length >> 0) & 0x07FF;
1019 char diag[30 *3 + 4];
1022 u8 *c = 4 + ftdi->response;
1023 int s = (sizeof(diag) - 1) / 3;
1025 while (s-- > 0 && m-- > 0) {
1026 if (s > 0 || m == 0) {
1027 d += sprintf(d, " %02X", *c++);
1029 d += sprintf(d, " ..");
1031 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1038 } else if (ftdi->expected == 8) {
1040 int respond_head = ftdi->respond_head++;
1041 struct u132_respond *respond = &ftdi->respond[
1042 RESPOND_MASK & respond_head];
1043 u32 data = ftdi->response[7];
1045 data |= ftdi->response[6];
1047 data |= ftdi->response[5];
1049 data |= ftdi->response[4];
1050 *respond->value = data;
1051 *respond->result = 0;
1052 complete(&respond->wait_completion);
1057 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1058 if (buscmd == 0x00) {
1059 } else if (buscmd == 0x02) {
1060 } else if (buscmd == 0x06) {
1061 } else if (buscmd == 0x0A) {
1063 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1067 if ((ftdi->response[0] & 0x80) == 0x00) {
1071 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1072 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1073 u16 ed_length = (ftdi->response[2] << 8) |
1075 struct u132_target *target = &ftdi->target[
1077 target->halted = (ftdi->response[0] >> 3) &
1079 target->skipped = (ftdi->response[0] >> 2) &
1081 target->toggle_bits = (ftdi->response[3] >> 6)
1083 target->error_count = (ftdi->response[3] >> 4)
1085 target->condition_code = (ftdi->response[
1087 if ((ftdi->response[0] & 0x10) == 0x00) {
1088 b = have_ed_set_response(ftdi, target,
1089 ed_length, ed_number, ed_type,
1093 b = have_ed_get_response(ftdi, target,
1094 ed_length, ed_number, ed_type,
1106 * create a urb, and a buffer for it, and copy the data to the urb
1109 static ssize_t ftdi_elan_write(struct file *file,
1110 const char __user *user_buffer, size_t count,
1116 struct usb_ftdi *ftdi = file->private_data;
1118 if (ftdi->disconnected > 0) {
1124 urb = usb_alloc_urb(0, GFP_KERNEL);
1129 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1130 &urb->transfer_dma);
1135 if (copy_from_user(buf, user_buffer, count)) {
1139 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1140 ftdi->bulk_out_endpointAddr), buf, count,
1141 ftdi_elan_write_bulk_callback, ftdi);
1142 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1143 retval = usb_submit_urb(urb, GFP_KERNEL);
1145 dev_err(&ftdi->udev->dev,
1146 "failed submitting write urb, error %d\n", retval);
1154 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1161 static const struct file_operations ftdi_elan_fops = {
1162 .owner = THIS_MODULE,
1163 .llseek = no_llseek,
1164 .read = ftdi_elan_read,
1165 .write = ftdi_elan_write,
1166 .open = ftdi_elan_open,
1167 .release = ftdi_elan_release,
1171 * usb class driver info in order to get a minor number from the usb core,
1172 * and to have the device registered with the driver core
1174 static struct usb_class_driver ftdi_elan_jtag_class = {
1175 .name = "ftdi-%d-jtag",
1176 .fops = &ftdi_elan_fops,
1177 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1181 * the following definitions are for the
1182 * ELAN FPGA state machgine processor that
1183 * lies on the other side of the FTDI chip
1185 #define cPCIu132rd 0x0
1186 #define cPCIu132wr 0x1
1187 #define cPCIiord 0x2
1188 #define cPCIiowr 0x3
1189 #define cPCImemrd 0x6
1190 #define cPCImemwr 0x7
1191 #define cPCIcfgrd 0xA
1192 #define cPCIcfgwr 0xB
1193 #define cPCInull 0xF
1194 #define cU132cmd_status 0x0
1195 #define cU132flash 0x1
1196 #define cPIDsetup 0x0
1199 #define cPIDinonce 0x3
1200 #define cCCnoerror 0x0
1202 #define cCCbitstuff 0x2
1203 #define cCCtoggle 0x3
1204 #define cCCstall 0x4
1205 #define cCCnoresp 0x5
1206 #define cCCbadpid1 0x6
1207 #define cCCbadpid2 0x7
1208 #define cCCdataoverrun 0x8
1209 #define cCCdataunderrun 0x9
1210 #define cCCbuffoverrun 0xC
1211 #define cCCbuffunderrun 0xD
1212 #define cCCnotaccessed 0xF
1213 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1215 wait:if (ftdi->disconnected > 0) {
1219 mutex_lock(&ftdi->u132_lock);
1220 command_size = ftdi->command_next - ftdi->command_head;
1221 if (command_size < COMMAND_SIZE) {
1222 struct u132_command *command = &ftdi->command[
1223 COMMAND_MASK & ftdi->command_next];
1224 command->header = 0x00 | cPCIu132wr;
1225 command->length = 0x04;
1226 command->address = 0x00;
1227 command->width = 0x00;
1228 command->follows = 4;
1229 command->value = data;
1230 command->buffer = &command->value;
1231 ftdi->command_next += 1;
1232 ftdi_elan_kick_command_queue(ftdi);
1233 mutex_unlock(&ftdi->u132_lock);
1236 mutex_unlock(&ftdi->u132_lock);
1243 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1246 u8 addressofs = config_offset / 4;
1247 wait:if (ftdi->disconnected > 0) {
1251 mutex_lock(&ftdi->u132_lock);
1252 command_size = ftdi->command_next - ftdi->command_head;
1253 if (command_size < COMMAND_SIZE) {
1254 struct u132_command *command = &ftdi->command[
1255 COMMAND_MASK & ftdi->command_next];
1256 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1257 command->length = 0x04;
1258 command->address = addressofs;
1259 command->width = 0x00 | (width & 0x0F);
1260 command->follows = 4;
1261 command->value = data;
1262 command->buffer = &command->value;
1263 ftdi->command_next += 1;
1264 ftdi_elan_kick_command_queue(ftdi);
1265 mutex_unlock(&ftdi->u132_lock);
1268 mutex_unlock(&ftdi->u132_lock);
1275 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1278 u8 addressofs = mem_offset / 4;
1279 wait:if (ftdi->disconnected > 0) {
1283 mutex_lock(&ftdi->u132_lock);
1284 command_size = ftdi->command_next - ftdi->command_head;
1285 if (command_size < COMMAND_SIZE) {
1286 struct u132_command *command = &ftdi->command[
1287 COMMAND_MASK & ftdi->command_next];
1288 command->header = 0x00 | (cPCImemwr & 0x0F);
1289 command->length = 0x04;
1290 command->address = addressofs;
1291 command->width = 0x00 | (width & 0x0F);
1292 command->follows = 4;
1293 command->value = data;
1294 command->buffer = &command->value;
1295 ftdi->command_next += 1;
1296 ftdi_elan_kick_command_queue(ftdi);
1297 mutex_unlock(&ftdi->u132_lock);
1300 mutex_unlock(&ftdi->u132_lock);
1307 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1310 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1311 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1315 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1316 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1318 wait:if (ftdi->disconnected > 0) {
1323 mutex_lock(&ftdi->u132_lock);
1324 command_size = ftdi->command_next - ftdi->command_head;
1325 respond_size = ftdi->respond_next - ftdi->respond_head;
1326 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1328 struct u132_command *command = &ftdi->command[
1329 COMMAND_MASK & ftdi->command_next];
1330 struct u132_respond *respond = &ftdi->respond[
1331 RESPOND_MASK & ftdi->respond_next];
1332 int result = -ENODEV;
1333 respond->result = &result;
1334 respond->header = command->header = 0x00 | cPCIu132rd;
1335 command->length = 0x04;
1336 respond->address = command->address = cU132cmd_status;
1337 command->width = 0x00;
1338 command->follows = 0;
1340 command->buffer = NULL;
1341 respond->value = data;
1342 init_completion(&respond->wait_completion);
1343 ftdi->command_next += 1;
1344 ftdi->respond_next += 1;
1345 ftdi_elan_kick_command_queue(ftdi);
1346 mutex_unlock(&ftdi->u132_lock);
1347 wait_for_completion(&respond->wait_completion);
1350 mutex_unlock(&ftdi->u132_lock);
1357 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1358 u8 width, u32 *data)
1360 u8 addressofs = config_offset / 4;
1361 wait:if (ftdi->disconnected > 0) {
1366 mutex_lock(&ftdi->u132_lock);
1367 command_size = ftdi->command_next - ftdi->command_head;
1368 respond_size = ftdi->respond_next - ftdi->respond_head;
1369 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1371 struct u132_command *command = &ftdi->command[
1372 COMMAND_MASK & ftdi->command_next];
1373 struct u132_respond *respond = &ftdi->respond[
1374 RESPOND_MASK & ftdi->respond_next];
1375 int result = -ENODEV;
1376 respond->result = &result;
1377 respond->header = command->header = 0x00 | (cPCIcfgrd &
1379 command->length = 0x04;
1380 respond->address = command->address = addressofs;
1381 command->width = 0x00 | (width & 0x0F);
1382 command->follows = 0;
1384 command->buffer = NULL;
1385 respond->value = data;
1386 init_completion(&respond->wait_completion);
1387 ftdi->command_next += 1;
1388 ftdi->respond_next += 1;
1389 ftdi_elan_kick_command_queue(ftdi);
1390 mutex_unlock(&ftdi->u132_lock);
1391 wait_for_completion(&respond->wait_completion);
1394 mutex_unlock(&ftdi->u132_lock);
1401 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1402 u8 width, u32 *data)
1404 u8 addressofs = mem_offset / 4;
1405 wait:if (ftdi->disconnected > 0) {
1410 mutex_lock(&ftdi->u132_lock);
1411 command_size = ftdi->command_next - ftdi->command_head;
1412 respond_size = ftdi->respond_next - ftdi->respond_head;
1413 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1415 struct u132_command *command = &ftdi->command[
1416 COMMAND_MASK & ftdi->command_next];
1417 struct u132_respond *respond = &ftdi->respond[
1418 RESPOND_MASK & ftdi->respond_next];
1419 int result = -ENODEV;
1420 respond->result = &result;
1421 respond->header = command->header = 0x00 | (cPCImemrd &
1423 command->length = 0x04;
1424 respond->address = command->address = addressofs;
1425 command->width = 0x00 | (width & 0x0F);
1426 command->follows = 0;
1428 command->buffer = NULL;
1429 respond->value = data;
1430 init_completion(&respond->wait_completion);
1431 ftdi->command_next += 1;
1432 ftdi->respond_next += 1;
1433 ftdi_elan_kick_command_queue(ftdi);
1434 mutex_unlock(&ftdi->u132_lock);
1435 wait_for_completion(&respond->wait_completion);
1438 mutex_unlock(&ftdi->u132_lock);
1445 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1446 u8 width, u32 *data)
1448 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1449 if (ftdi->initialized == 0) {
1452 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1456 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1457 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1458 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1459 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1460 int toggle_bits, int error_count, int condition_code, int repeat_number,
1461 int halted, int skipped, int actual, int non_null))
1463 u8 ed = ed_number - 1;
1464 wait:if (ftdi->disconnected > 0) {
1466 } else if (ftdi->initialized == 0) {
1470 mutex_lock(&ftdi->u132_lock);
1471 command_size = ftdi->command_next - ftdi->command_head;
1472 if (command_size < COMMAND_SIZE) {
1473 struct u132_target *target = &ftdi->target[ed];
1474 struct u132_command *command = &ftdi->command[
1475 COMMAND_MASK & ftdi->command_next];
1476 command->header = 0x80 | (ed << 5);
1477 command->length = 0x8007;
1478 command->address = (toggle_bits << 6) | (ep_number << 2)
1480 command->width = usb_maxpacket(urb->dev, urb->pipe,
1481 usb_pipeout(urb->pipe));
1482 command->follows = 8;
1484 command->buffer = urb->setup_packet;
1485 target->callback = callback;
1486 target->endp = endp;
1489 ftdi->command_next += 1;
1490 ftdi_elan_kick_command_queue(ftdi);
1491 mutex_unlock(&ftdi->u132_lock);
1494 mutex_unlock(&ftdi->u132_lock);
1501 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1502 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1503 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1504 int toggle_bits, int error_count, int condition_code, int repeat_number,
1505 int halted, int skipped, int actual, int non_null))
1507 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1508 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1509 ep_number, toggle_bits, callback);
1513 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1514 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1515 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1516 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1517 int toggle_bits, int error_count, int condition_code, int repeat_number,
1518 int halted, int skipped, int actual, int non_null))
1520 u8 ed = ed_number - 1;
1521 wait:if (ftdi->disconnected > 0) {
1523 } else if (ftdi->initialized == 0) {
1527 mutex_lock(&ftdi->u132_lock);
1528 command_size = ftdi->command_next - ftdi->command_head;
1529 if (command_size < COMMAND_SIZE) {
1530 struct u132_target *target = &ftdi->target[ed];
1531 struct u132_command *command = &ftdi->command[
1532 COMMAND_MASK & ftdi->command_next];
1533 u32 remaining_length = urb->transfer_buffer_length -
1535 command->header = 0x82 | (ed << 5);
1536 if (remaining_length == 0) {
1537 command->length = 0x0000;
1538 } else if (remaining_length > 1024) {
1539 command->length = 0x8000 | 1023;
1541 command->length = 0x8000 | (remaining_length -
1543 command->address = (toggle_bits << 6) | (ep_number << 2)
1545 command->width = usb_maxpacket(urb->dev, urb->pipe,
1546 usb_pipeout(urb->pipe));
1547 command->follows = 0;
1549 command->buffer = NULL;
1550 target->callback = callback;
1551 target->endp = endp;
1554 ftdi->command_next += 1;
1555 ftdi_elan_kick_command_queue(ftdi);
1556 mutex_unlock(&ftdi->u132_lock);
1559 mutex_unlock(&ftdi->u132_lock);
1566 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1567 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1568 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1569 int toggle_bits, int error_count, int condition_code, int repeat_number,
1570 int halted, int skipped, int actual, int non_null))
1572 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1573 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1574 ep_number, toggle_bits, callback);
1578 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1579 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1580 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1581 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1582 int toggle_bits, int error_count, int condition_code, int repeat_number,
1583 int halted, int skipped, int actual, int non_null))
1585 u8 ed = ed_number - 1;
1586 wait:if (ftdi->disconnected > 0) {
1588 } else if (ftdi->initialized == 0) {
1592 mutex_lock(&ftdi->u132_lock);
1593 command_size = ftdi->command_next - ftdi->command_head;
1594 if (command_size < COMMAND_SIZE) {
1595 struct u132_target *target = &ftdi->target[ed];
1596 struct u132_command *command = &ftdi->command[
1597 COMMAND_MASK & ftdi->command_next];
1598 command->header = 0x81 | (ed << 5);
1599 command->length = 0x0000;
1600 command->address = (toggle_bits << 6) | (ep_number << 2)
1602 command->width = usb_maxpacket(urb->dev, urb->pipe,
1603 usb_pipeout(urb->pipe));
1604 command->follows = 0;
1606 command->buffer = NULL;
1607 target->callback = callback;
1608 target->endp = endp;
1611 ftdi->command_next += 1;
1612 ftdi_elan_kick_command_queue(ftdi);
1613 mutex_unlock(&ftdi->u132_lock);
1616 mutex_unlock(&ftdi->u132_lock);
1623 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1624 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1625 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1626 int toggle_bits, int error_count, int condition_code, int repeat_number,
1627 int halted, int skipped, int actual, int non_null))
1629 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1630 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1631 ep_number, toggle_bits, callback);
1635 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1636 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1637 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1638 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1639 int toggle_bits, int error_count, int condition_code, int repeat_number,
1640 int halted, int skipped, int actual, int non_null))
1642 u8 ed = ed_number - 1;
1643 wait:if (ftdi->disconnected > 0) {
1645 } else if (ftdi->initialized == 0) {
1649 mutex_lock(&ftdi->u132_lock);
1650 command_size = ftdi->command_next - ftdi->command_head;
1651 if (command_size < COMMAND_SIZE) {
1655 char data[30 *3 + 4];
1657 int m = (sizeof(data) - 1) / 3 - 1;
1659 struct u132_target *target = &ftdi->target[ed];
1660 struct u132_command *command = &ftdi->command[
1661 COMMAND_MASK & ftdi->command_next];
1662 command->header = 0x81 | (ed << 5);
1663 command->address = (toggle_bits << 6) | (ep_number << 2)
1665 command->width = usb_maxpacket(urb->dev, urb->pipe,
1666 usb_pipeout(urb->pipe));
1667 command->follows = min_t(u32, 1024,
1668 urb->transfer_buffer_length -
1669 urb->actual_length);
1671 command->buffer = urb->transfer_buffer +
1673 command->length = 0x8000 | (command->follows - 1);
1674 b = command->buffer;
1675 urb_size = command->follows;
1677 while (urb_size-- > 0) {
1679 } else if (i++ < m) {
1680 int w = sprintf(d, " %02X", *b++);
1684 d += sprintf(d, " ..");
1686 target->callback = callback;
1687 target->endp = endp;
1690 ftdi->command_next += 1;
1691 ftdi_elan_kick_command_queue(ftdi);
1692 mutex_unlock(&ftdi->u132_lock);
1695 mutex_unlock(&ftdi->u132_lock);
1702 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1703 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1704 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1705 int toggle_bits, int error_count, int condition_code, int repeat_number,
1706 int halted, int skipped, int actual, int non_null))
1708 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1709 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1710 ep_number, toggle_bits, callback);
1714 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1715 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1716 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1717 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1718 int toggle_bits, int error_count, int condition_code, int repeat_number,
1719 int halted, int skipped, int actual, int non_null))
1721 u8 ed = ed_number - 1;
1722 wait:if (ftdi->disconnected > 0) {
1724 } else if (ftdi->initialized == 0) {
1728 mutex_lock(&ftdi->u132_lock);
1729 command_size = ftdi->command_next - ftdi->command_head;
1730 if (command_size < COMMAND_SIZE) {
1731 u32 remaining_length = urb->transfer_buffer_length -
1733 struct u132_target *target = &ftdi->target[ed];
1734 struct u132_command *command = &ftdi->command[
1735 COMMAND_MASK & ftdi->command_next];
1736 command->header = 0x83 | (ed << 5);
1737 if (remaining_length == 0) {
1738 command->length = 0x0000;
1739 } else if (remaining_length > 1024) {
1740 command->length = 0x8000 | 1023;
1742 command->length = 0x8000 | (remaining_length -
1744 command->address = (toggle_bits << 6) | (ep_number << 2)
1746 command->width = usb_maxpacket(urb->dev, urb->pipe,
1747 usb_pipeout(urb->pipe));
1748 command->follows = 0;
1750 command->buffer = NULL;
1751 target->callback = callback;
1752 target->endp = endp;
1755 ftdi->command_next += 1;
1756 ftdi_elan_kick_command_queue(ftdi);
1757 mutex_unlock(&ftdi->u132_lock);
1760 mutex_unlock(&ftdi->u132_lock);
1767 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1768 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1769 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1770 int toggle_bits, int error_count, int condition_code, int repeat_number,
1771 int halted, int skipped, int actual, int non_null))
1773 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1774 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1775 ep_number, toggle_bits, callback);
1779 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1780 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1783 u8 ed = ed_number - 1;
1784 if (ftdi->disconnected > 0) {
1786 } else if (ftdi->initialized == 0) {
1789 struct u132_target *target = &ftdi->target[ed];
1790 mutex_lock(&ftdi->u132_lock);
1791 if (target->abandoning > 0) {
1792 mutex_unlock(&ftdi->u132_lock);
1795 target->abandoning = 1;
1796 wait_1:if (target->active == 1) {
1797 int command_size = ftdi->command_next -
1799 if (command_size < COMMAND_SIZE) {
1800 struct u132_command *command =
1801 &ftdi->command[COMMAND_MASK &
1802 ftdi->command_next];
1803 command->header = 0x80 | (ed << 5) |
1805 command->length = 0x00;
1806 command->address = 0x00;
1807 command->width = 0x00;
1808 command->follows = 0;
1810 command->buffer = &command->value;
1811 ftdi->command_next += 1;
1812 ftdi_elan_kick_command_queue(ftdi);
1814 mutex_unlock(&ftdi->u132_lock);
1816 mutex_lock(&ftdi->u132_lock);
1820 mutex_unlock(&ftdi->u132_lock);
1826 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1829 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1830 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1834 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1835 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1837 int retry_on_empty = 10;
1838 int retry_on_timeout = 5;
1839 int retry_on_status = 20;
1841 int packet_bytes = 0;
1842 int retval = usb_bulk_msg(ftdi->udev,
1843 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1844 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1845 &packet_bytes, 100);
1846 if (packet_bytes > 2) {
1847 char diag[30 *3 + 4];
1849 int m = (sizeof(diag) - 1) / 3 - 1;
1850 char *b = ftdi->bulk_in_buffer;
1853 while (packet_bytes-- > 0) {
1855 if (bytes_read < m) {
1856 d += sprintf(d, " %02X",
1858 } else if (bytes_read > m) {
1860 d += sprintf(d, " ..");
1865 } else if (packet_bytes > 1) {
1866 char s1 = ftdi->bulk_in_buffer[0];
1867 char s2 = ftdi->bulk_in_buffer[1];
1868 if (s1 == 0x31 && s2 == 0x60) {
1870 } else if (retry_on_status-- > 0) {
1873 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1876 } else if (packet_bytes > 0) {
1877 char b1 = ftdi->bulk_in_buffer[0];
1878 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1880 if (retry_on_status-- > 0) {
1883 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1886 } else if (retval == -ETIMEDOUT) {
1887 if (retry_on_timeout-- > 0) {
1890 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1893 } else if (retval == 0) {
1894 if (retry_on_empty-- > 0) {
1897 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1901 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1910 * send the long flush sequence
1913 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1920 urb = usb_alloc_urb(0, GFP_KERNEL);
1923 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1925 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1931 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1932 ftdi->bulk_out_endpointAddr), buf, i,
1933 ftdi_elan_write_bulk_callback, ftdi);
1934 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1935 retval = usb_submit_urb(urb, GFP_KERNEL);
1937 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1938 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1948 * send the reset sequence
1951 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1958 urb = usb_alloc_urb(0, GFP_KERNEL);
1961 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1963 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1971 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1972 ftdi->bulk_out_endpointAddr), buf, i,
1973 ftdi_elan_write_bulk_callback, ftdi);
1974 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1975 retval = usb_submit_urb(urb, GFP_KERNEL);
1977 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
1978 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1986 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
1990 int retry_on_timeout = 5;
1991 int retry_on_empty = 10;
1993 retval = ftdi_elan_flush_input_fifo(ftdi);
1996 ftdi->bulk_in_left = 0;
1997 ftdi->bulk_in_last = -1;
1998 while (long_stop-- > 0) {
2001 retval = ftdi_elan_synchronize_flush(ftdi);
2004 retval = ftdi_elan_flush_input_fifo(ftdi);
2007 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2013 int packet_bytes = 0;
2014 retval = usb_bulk_msg(ftdi->udev,
2015 usb_rcvbulkpipe(ftdi->udev,
2016 ftdi->bulk_in_endpointAddr),
2017 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2018 &packet_bytes, 500);
2019 if (packet_bytes > 2) {
2020 char diag[30 *3 + 4];
2022 int m = (sizeof(diag) - 1) / 3 - 1;
2023 char *b = ftdi->bulk_in_buffer;
2025 unsigned char c = 0;
2027 while (packet_bytes-- > 0) {
2029 if (bytes_read < m) {
2030 d += sprintf(d, " %02X", c);
2031 } else if (bytes_read > m) {
2033 d += sprintf(d, " ..");
2042 } else if (read_stop-- > 0) {
2045 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2049 } else if (packet_bytes > 1) {
2050 unsigned char s1 = ftdi->bulk_in_buffer[0];
2051 unsigned char s2 = ftdi->bulk_in_buffer[1];
2052 if (s1 == 0x31 && s2 == 0x00) {
2053 if (read_stuck-- > 0) {
2057 } else if (s1 == 0x31 && s2 == 0x60) {
2058 if (read_stop-- > 0) {
2061 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2065 if (read_stop-- > 0) {
2068 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2072 } else if (packet_bytes > 0) {
2073 if (read_stop-- > 0) {
2076 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2079 } else if (retval == -ETIMEDOUT) {
2080 if (retry_on_timeout-- > 0) {
2083 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2086 } else if (retval == 0) {
2087 if (retry_on_empty-- > 0) {
2090 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2095 dev_err(&ftdi->udev->dev, "error = %d\n",
2097 if (read_stop-- > 0) {
2100 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2106 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2110 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2112 int retry_on_empty = 10;
2113 int retry_on_timeout = 5;
2114 int retry_on_status = 50;
2116 int packet_bytes = 0;
2117 int retval = usb_bulk_msg(ftdi->udev,
2118 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2119 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2120 &packet_bytes, 1000);
2121 if (packet_bytes > 2) {
2122 char diag[30 *3 + 4];
2124 int m = (sizeof(diag) - 1) / 3 - 1;
2125 char *b = ftdi->bulk_in_buffer;
2128 while (packet_bytes-- > 0) {
2130 if (bytes_read < m) {
2131 d += sprintf(d, " %02X",
2133 } else if (bytes_read > m) {
2135 d += sprintf(d, " ..");
2140 } else if (packet_bytes > 1) {
2141 char s1 = ftdi->bulk_in_buffer[0];
2142 char s2 = ftdi->bulk_in_buffer[1];
2143 if (s1 == 0x31 && s2 == 0x60) {
2145 } else if (retry_on_status-- > 0) {
2150 } else if (packet_bytes > 0) {
2151 char b1 = ftdi->bulk_in_buffer[0];
2152 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2153 if (retry_on_status-- > 0) {
2157 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2160 } else if (retval == -ETIMEDOUT) {
2161 if (retry_on_timeout-- > 0) {
2164 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2167 } else if (retval == 0) {
2168 if (retry_on_empty-- > 0) {
2171 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2175 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2182 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2184 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2187 if (ftdi->controlreg & 0x00400000) {
2188 if (ftdi->card_ejected) {
2190 ftdi->card_ejected = 1;
2191 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2196 u8 fn = ftdi->function - 1;
2197 int activePCIfn = fn << 8;
2202 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2206 pciVID = pcidata & 0xFFFF;
2207 pciPID = (pcidata >> 16) & 0xFFFF;
2208 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2209 ftdi->platform_data.device) {
2212 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2213 ftdi->platform_data.vendor, pciVID,
2214 ftdi->platform_data.device, pciPID);
2221 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2222 offsetof(struct ohci_regs, member), 0, data);
2223 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2224 offsetof(struct ohci_regs, member), 0, data);
2226 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2227 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2229 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2243 int mask = OHCI_INTR_INIT;
2245 int reset_timeout = 30; /* ... allow extra time */
2247 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2250 retval = ftdi_read_pcimem(ftdi, control, &control);
2253 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2256 num_ports = rh_a & RH_A_NDP;
2257 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2260 hc_fminterval &= 0x3fff;
2261 if (hc_fminterval != FI) {
2263 hc_fminterval |= FSMP(hc_fminterval) << 16;
2264 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2267 switch (hc_control & OHCI_CTRL_HCFS) {
2271 case OHCI_USB_SUSPEND:
2272 case OHCI_USB_RESUME:
2273 hc_control &= OHCI_CTRL_RWC;
2274 hc_control |= OHCI_USB_RESUME;
2278 hc_control &= OHCI_CTRL_RWC;
2279 hc_control |= OHCI_USB_RESET;
2283 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2286 retval = ftdi_read_pcimem(ftdi, control, &control);
2290 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2293 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2294 for (temp = 0; temp < num_ports; temp++) {
2295 retval = ftdi_write_pcimem(ftdi,
2296 roothub.portstatus[temp], RH_PS_LSDA);
2301 retval = ftdi_read_pcimem(ftdi, control, &control);
2304 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2307 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2311 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2314 if (0 != (status & OHCI_HCR)) {
2315 if (--reset_timeout == 0) {
2316 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2324 if (quirk & OHCI_QUIRK_INITRESET) {
2325 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2328 retval = ftdi_read_pcimem(ftdi, control, &control);
2332 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2335 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2338 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2341 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2344 retval = ftdi_write_pcimem(ftdi, fminterval,
2345 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2348 retval = ftdi_write_pcimem(ftdi, periodicstart,
2349 ((9 *hc_fminterval) / 10) & 0x3fff);
2352 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2355 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2358 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2359 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2360 quirk |= OHCI_QUIRK_INITRESET;
2363 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2364 fminterval, periodicstart);
2365 } /* start controller operations */
2366 hc_control &= OHCI_CTRL_RWC;
2367 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2368 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2371 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2374 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2377 retval = ftdi_read_pcimem(ftdi, control, &control);
2380 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2383 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2386 retval = ftdi_write_pcimem(ftdi, intrdisable,
2387 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2388 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2391 return retval; /* handle root hub init quirks ... */
2392 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2395 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2396 if (quirk & OHCI_QUIRK_SUPERIO) {
2397 roothub_a |= RH_A_NOCP;
2398 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2399 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2402 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2403 roothub_a |= RH_A_NPS;
2404 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2408 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2411 retval = ftdi_write_pcimem(ftdi, roothub.b,
2412 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2415 retval = ftdi_read_pcimem(ftdi, control, &control);
2418 mdelay((roothub_a >> 23) & 0x1fe);
2419 for (temp = 0; temp < num_ports; temp++) {
2421 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2431 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2437 int activePCIfn = fn << 8;
2438 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2442 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2446 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2450 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2454 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2459 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2463 latence_timer &= 0xFFFF00FF;
2464 latence_timer |= 0x00001600;
2465 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2469 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2474 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2478 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2482 for (reg = 0; reg <= 0x54; reg += 4) {
2483 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2490 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2496 int activePCIfn = fn << 8;
2497 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2501 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2505 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2509 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2513 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2518 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2522 latence_timer &= 0xFFFF00FF;
2523 latence_timer |= 0x00001600;
2524 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2528 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2533 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2537 return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2540 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2544 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2547 result = ftdi_elan_check_controller(ftdi, quirk);
2548 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2554 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2559 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2562 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2566 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2569 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2572 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2575 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2578 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2582 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2585 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2588 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2591 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2594 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2598 sensebits = (controlreg >> 16) & 0x000F;
2599 if (0x0D == sensebits)
2605 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2611 int activePCIfn = 0;
2612 int max_devices = 0;
2613 int controllers = 0;
2614 int unrecognized = 0;
2616 for (fn = 0; (fn < 4); fn++) {
2620 activePCIfn = fn << 8;
2621 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2625 pciVID = pcidata & 0xFFFF;
2626 pciPID = (pcidata >> 16) & 0xFFFF;
2627 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2628 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2630 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2632 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2634 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2635 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2637 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2639 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2641 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2642 devices = ftdi_elan_found_controller(ftdi, fn,
2645 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2646 devices = ftdi_elan_found_controller(ftdi, fn,
2647 OHCI_QUIRK_ZFMICRO);
2649 } else if (0 == pcidata) {
2652 if (devices > max_devices) {
2653 max_devices = devices;
2654 ftdi->function = fn + 1;
2655 ftdi->platform_data.vendor = pciVID;
2656 ftdi->platform_data.device = pciPID;
2659 if (ftdi->function > 0) {
2660 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2661 } else if (controllers > 0) {
2663 } else if (unrecognized > 0) {
2666 ftdi->enumerated = 0;
2673 * we use only the first bulk-in and bulk-out endpoints
2675 static int ftdi_elan_probe(struct usb_interface *interface,
2676 const struct usb_device_id *id)
2678 struct usb_host_interface *iface_desc;
2679 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
2681 struct usb_ftdi *ftdi;
2683 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2687 mutex_lock(&ftdi_module_lock);
2688 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2689 ftdi->sequence_num = ++ftdi_instances;
2690 mutex_unlock(&ftdi_module_lock);
2691 ftdi_elan_init_kref(ftdi);
2692 sema_init(&ftdi->sw_lock, 1);
2693 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2694 ftdi->interface = interface;
2695 mutex_init(&ftdi->u132_lock);
2698 iface_desc = interface->cur_altsetting;
2699 retval = usb_find_common_endpoints(iface_desc,
2700 &bulk_in, &bulk_out, NULL, NULL);
2702 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2706 ftdi->bulk_in_size = usb_endpoint_maxp(bulk_in);
2707 ftdi->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
2708 ftdi->bulk_in_buffer = kmalloc(ftdi->bulk_in_size, GFP_KERNEL);
2709 if (!ftdi->bulk_in_buffer) {
2714 ftdi->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
2716 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2717 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2718 ftdi->bulk_out_endpointAddr);
2719 usb_set_intfdata(interface, ftdi);
2720 if (iface_desc->desc.bInterfaceNumber == 0 &&
2721 ftdi->bulk_in_endpointAddr == 0x81 &&
2722 ftdi->bulk_out_endpointAddr == 0x02) {
2723 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2725 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2726 usb_set_intfdata(interface, NULL);
2730 ftdi->class = &ftdi_elan_jtag_class;
2731 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2732 ftdi, iface_desc->desc.bInterfaceNumber,
2736 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2737 ftdi->bulk_in_endpointAddr == 0x83 &&
2738 ftdi->bulk_out_endpointAddr == 0x04) {
2740 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2741 ftdi, iface_desc->desc.bInterfaceNumber);
2742 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2743 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2744 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2745 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2748 dev_err(&ftdi->udev->dev,
2749 "Could not find ELAN's U132 device\n");
2754 ftdi_elan_put_kref(ftdi);
2759 static void ftdi_elan_disconnect(struct usb_interface *interface)
2761 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2762 ftdi->disconnected += 1;
2764 int minor = interface->minor;
2765 struct usb_class_driver *class = ftdi->class;
2766 usb_set_intfdata(interface, NULL);
2767 usb_deregister_dev(interface, class);
2768 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2771 ftdi_status_cancel_work(ftdi);
2772 ftdi_command_cancel_work(ftdi);
2773 ftdi_response_cancel_work(ftdi);
2774 ftdi_elan_abandon_completions(ftdi);
2775 ftdi_elan_abandon_targets(ftdi);
2776 if (ftdi->registered) {
2777 platform_device_unregister(&ftdi->platform_dev);
2778 ftdi->synchronized = 0;
2779 ftdi->enumerated = 0;
2780 ftdi->initialized = 0;
2781 ftdi->registered = 0;
2783 ftdi->disconnected += 1;
2784 usb_set_intfdata(interface, NULL);
2785 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2787 ftdi_elan_put_kref(ftdi);
2790 static struct usb_driver ftdi_elan_driver = {
2791 .name = "ftdi-elan",
2792 .probe = ftdi_elan_probe,
2793 .disconnect = ftdi_elan_disconnect,
2794 .id_table = ftdi_elan_table,
2796 static int __init ftdi_elan_init(void)
2799 pr_info("driver %s\n", ftdi_elan_driver.name);
2800 mutex_init(&ftdi_module_lock);
2801 INIT_LIST_HEAD(&ftdi_static_list);
2802 result = usb_register(&ftdi_elan_driver);
2804 pr_err("usb_register failed. Error number %d\n", result);
2810 static void __exit ftdi_elan_exit(void)
2812 struct usb_ftdi *ftdi;
2813 struct usb_ftdi *temp;
2814 usb_deregister(&ftdi_elan_driver);
2815 pr_info("ftdi_u132 driver deregistered\n");
2816 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2817 ftdi_status_cancel_work(ftdi);
2818 ftdi_command_cancel_work(ftdi);
2819 ftdi_response_cancel_work(ftdi);
2824 module_init(ftdi_elan_init);
2825 module_exit(ftdi_elan_exit);