1 // SPDX-License-Identifier: GPL-2.0
3 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
5 * Copyright(C) 2006 Elan Digital Systems Limited
6 * http://www.elandigitalsystems.com
8 * Author and Maintainer - Tony Olech - Elan Digital Systems
9 * tony.olech@elandigitalsystems.com
11 * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
12 * based on various USB client drivers in the 2.6.15 linux kernel
13 * with constant reference to the 3rd Edition of Linux Device Drivers
14 * published by O'Reilly
16 * The U132 adapter is a USB to CardBus adapter specifically designed
17 * for PC cards that contain an OHCI host controller. Typical PC cards
18 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
20 * The U132 adapter will *NOT *work with PC cards that do not contain
21 * an OHCI controller. A simple way to test whether a PC card has an
22 * OHCI controller as an interface is to insert the PC card directly
23 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
24 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
25 * then there is a good chance that the U132 adapter will support the
26 * PC card.(you also need the specific client driver for the PC card)
28 * Please inform the Author and Maintainer about any PC cards that
29 * contain OHCI Host Controller and work when directly connected to
30 * an embedded CardBus slot but do not work when they are connected
31 * via an ELAN U132 adapter.
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <linux/ioctl.h>
42 #include <linux/pci_ids.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/kref.h>
46 #include <linux/mutex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/workqueue.h>
50 #include <linux/platform_device.h>
51 MODULE_AUTHOR("Tony Olech");
52 MODULE_DESCRIPTION("FTDI ELAN driver");
53 MODULE_LICENSE("GPL");
54 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
55 static bool distrust_firmware = 1;
56 module_param(distrust_firmware, bool, 0);
57 MODULE_PARM_DESC(distrust_firmware,
58 "true to distrust firmware power/overcurrent setup");
59 extern struct platform_driver u132_platform_driver;
61 * ftdi_module_lock exists to protect access to global variables
64 static struct mutex ftdi_module_lock;
65 static int ftdi_instances = 0;
66 static struct list_head ftdi_static_list;
68 * end of the global variables protected by ftdi_module_lock
72 #include <linux/usb/hcd.h>
74 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
75 * If you're going to try stuff like this, you need to split
76 * out shareable stuff (register declarations?) into its own
77 * file, maybe name <linux/usb/ohci.h>
80 #include "../host/ohci.h"
81 /* Define these values to match your devices*/
82 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
83 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
84 /* table of devices that work with this driver*/
85 static const struct usb_device_id ftdi_elan_table[] = {
86 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
87 { /* Terminating entry */ }
90 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
91 /* only the jtag(firmware upgrade device) interface requires
92 * a device file and corresponding minor number, but the
93 * interface is created unconditionally - I suppose it could
94 * be configured or not according to a module parameter.
95 * But since we(now) require one interface per device,
96 * and since it unlikely that a normal installation would
97 * require more than a couple of elan-ftdi devices, 8 seems
98 * like a reasonable limit to have here, and if someone
99 * really requires more than 8 devices, then they can frig the
102 #define USB_FTDI_ELAN_MINOR_BASE 192
103 #define COMMAND_BITS 5
104 #define COMMAND_SIZE (1<<COMMAND_BITS)
105 #define COMMAND_MASK (COMMAND_SIZE-1)
106 struct u132_command {
115 #define RESPOND_BITS 5
116 #define RESPOND_SIZE (1<<RESPOND_BITS)
117 #define RESPOND_MASK (RESPOND_SIZE-1)
118 struct u132_respond {
123 struct completion wait_completion;
138 void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
139 int toggle_bits, int error_count, int condition_code,
140 int repeat_number, int halted, int skipped, int actual,
143 /* Structure to hold all of our device specific stuff*/
145 struct list_head ftdi_list;
146 struct mutex u132_lock;
149 struct u132_command command[COMMAND_SIZE];
152 struct u132_respond respond[RESPOND_SIZE];
153 struct u132_target target[4];
154 char device_name[16];
155 unsigned synchronized:1;
156 unsigned enumerated:1;
157 unsigned registered:1;
158 unsigned initialized:1;
159 unsigned card_ejected:1;
165 int status_queue_delay;
166 struct semaphore sw_lock;
167 struct usb_device *udev;
168 struct usb_interface *interface;
169 struct usb_class_driver *class;
170 struct delayed_work status_work;
171 struct delayed_work command_work;
172 struct delayed_work respond_work;
173 struct u132_platform_data platform_data;
174 struct resource resources[0];
175 struct platform_device platform_dev;
176 unsigned char *bulk_in_buffer;
180 __u8 bulk_in_endpointAddr;
181 __u8 bulk_out_endpointAddr;
184 u8 response[4 + 1024];
189 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
190 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
192 static struct usb_driver ftdi_elan_driver;
193 static void ftdi_elan_delete(struct kref *kref)
195 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
196 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
197 usb_put_dev(ftdi->udev);
198 ftdi->disconnected += 1;
199 mutex_lock(&ftdi_module_lock);
200 list_del_init(&ftdi->ftdi_list);
202 mutex_unlock(&ftdi_module_lock);
203 kfree(ftdi->bulk_in_buffer);
204 ftdi->bulk_in_buffer = NULL;
208 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
210 kref_put(&ftdi->kref, ftdi_elan_delete);
213 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
215 kref_get(&ftdi->kref);
218 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
220 kref_init(&ftdi->kref);
223 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
225 if (!schedule_delayed_work(&ftdi->status_work, delta))
226 kref_put(&ftdi->kref, ftdi_elan_delete);
229 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
231 if (schedule_delayed_work(&ftdi->status_work, delta))
232 kref_get(&ftdi->kref);
235 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
237 if (cancel_delayed_work_sync(&ftdi->status_work))
238 kref_put(&ftdi->kref, ftdi_elan_delete);
241 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
243 if (!schedule_delayed_work(&ftdi->command_work, delta))
244 kref_put(&ftdi->kref, ftdi_elan_delete);
247 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
249 if (schedule_delayed_work(&ftdi->command_work, delta))
250 kref_get(&ftdi->kref);
253 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
255 if (cancel_delayed_work_sync(&ftdi->command_work))
256 kref_put(&ftdi->kref, ftdi_elan_delete);
259 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
262 if (!schedule_delayed_work(&ftdi->respond_work, delta))
263 kref_put(&ftdi->kref, ftdi_elan_delete);
266 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
268 if (schedule_delayed_work(&ftdi->respond_work, delta))
269 kref_get(&ftdi->kref);
272 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
274 if (cancel_delayed_work_sync(&ftdi->respond_work))
275 kref_put(&ftdi->kref, ftdi_elan_delete);
278 void ftdi_elan_gone_away(struct platform_device *pdev)
280 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
281 ftdi->gone_away += 1;
282 ftdi_elan_put_kref(ftdi);
286 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
287 static void ftdi_release_platform_dev(struct device *dev)
292 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
293 struct u132_target *target, u8 *buffer, int length);
294 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
295 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
296 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
297 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
298 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
299 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
300 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
301 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
302 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
303 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
305 if (ftdi->platform_dev.dev.parent)
308 ftdi_elan_get_kref(ftdi);
309 ftdi->platform_data.potpg = 100;
310 ftdi->platform_data.reset = NULL;
311 ftdi->platform_dev.id = ftdi->sequence_num;
312 ftdi->platform_dev.resource = ftdi->resources;
313 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
314 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
315 ftdi->platform_dev.dev.parent = NULL;
316 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
317 ftdi->platform_dev.dev.dma_mask = NULL;
318 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
319 ftdi->platform_dev.name = ftdi->device_name;
320 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
321 request_module("u132_hcd");
322 dev_info(&ftdi->udev->dev, "registering '%s'\n",
323 ftdi->platform_dev.name);
325 return platform_device_register(&ftdi->platform_dev);
328 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
330 mutex_lock(&ftdi->u132_lock);
331 while (ftdi->respond_next > ftdi->respond_head) {
332 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
333 ftdi->respond_head++];
334 *respond->result = -ESHUTDOWN;
336 complete(&respond->wait_completion);
337 } mutex_unlock(&ftdi->u132_lock);
340 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
343 mutex_lock(&ftdi->u132_lock);
344 while (ed_number-- > 0) {
345 struct u132_target *target = &ftdi->target[ed_number];
346 if (target->active == 1) {
347 target->condition_code = TD_DEVNOTRESP;
348 mutex_unlock(&ftdi->u132_lock);
349 ftdi_elan_do_callback(ftdi, target, NULL, 0);
350 mutex_lock(&ftdi->u132_lock);
356 mutex_unlock(&ftdi->u132_lock);
359 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
362 mutex_lock(&ftdi->u132_lock);
363 while (ed_number-- > 0) {
364 struct u132_target *target = &ftdi->target[ed_number];
365 target->abandoning = 1;
366 wait_1:if (target->active == 1) {
367 int command_size = ftdi->command_next -
369 if (command_size < COMMAND_SIZE) {
370 struct u132_command *command = &ftdi->command[
371 COMMAND_MASK & ftdi->command_next];
372 command->header = 0x80 | (ed_number << 5) | 0x4;
373 command->length = 0x00;
374 command->address = 0x00;
375 command->width = 0x00;
376 command->follows = 0;
378 command->buffer = &command->value;
379 ftdi->command_next += 1;
380 ftdi_elan_kick_command_queue(ftdi);
382 mutex_unlock(&ftdi->u132_lock);
384 mutex_lock(&ftdi->u132_lock);
388 wait_2:if (target->active == 1) {
389 int command_size = ftdi->command_next -
391 if (command_size < COMMAND_SIZE) {
392 struct u132_command *command = &ftdi->command[
393 COMMAND_MASK & ftdi->command_next];
394 command->header = 0x90 | (ed_number << 5);
395 command->length = 0x00;
396 command->address = 0x00;
397 command->width = 0x00;
398 command->follows = 0;
400 command->buffer = &command->value;
401 ftdi->command_next += 1;
402 ftdi_elan_kick_command_queue(ftdi);
404 mutex_unlock(&ftdi->u132_lock);
406 mutex_lock(&ftdi->u132_lock);
414 mutex_unlock(&ftdi->u132_lock);
417 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
420 mutex_lock(&ftdi->u132_lock);
421 while (ed_number-- > 0) {
422 struct u132_target *target = &ftdi->target[ed_number];
423 target->abandoning = 1;
424 wait:if (target->active == 1) {
425 int command_size = ftdi->command_next -
427 if (command_size < COMMAND_SIZE) {
428 struct u132_command *command = &ftdi->command[
429 COMMAND_MASK & ftdi->command_next];
430 command->header = 0x80 | (ed_number << 5) | 0x4;
431 command->length = 0x00;
432 command->address = 0x00;
433 command->width = 0x00;
434 command->follows = 0;
436 command->buffer = &command->value;
437 ftdi->command_next += 1;
438 ftdi_elan_kick_command_queue(ftdi);
440 mutex_unlock(&ftdi->u132_lock);
442 mutex_lock(&ftdi->u132_lock);
450 mutex_unlock(&ftdi->u132_lock);
453 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
455 ftdi_command_queue_work(ftdi, 0);
458 static void ftdi_elan_command_work(struct work_struct *work)
460 struct usb_ftdi *ftdi =
461 container_of(work, struct usb_ftdi, command_work.work);
463 if (ftdi->disconnected > 0) {
464 ftdi_elan_put_kref(ftdi);
467 int retval = ftdi_elan_command_engine(ftdi);
468 if (retval == -ESHUTDOWN) {
469 ftdi->disconnected += 1;
470 } else if (retval == -ENODEV) {
471 ftdi->disconnected += 1;
473 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
474 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
479 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
481 ftdi_respond_queue_work(ftdi, 0);
484 static void ftdi_elan_respond_work(struct work_struct *work)
486 struct usb_ftdi *ftdi =
487 container_of(work, struct usb_ftdi, respond_work.work);
488 if (ftdi->disconnected > 0) {
489 ftdi_elan_put_kref(ftdi);
492 int retval = ftdi_elan_respond_engine(ftdi);
494 } else if (retval == -ESHUTDOWN) {
495 ftdi->disconnected += 1;
496 } else if (retval == -ENODEV) {
497 ftdi->disconnected += 1;
498 } else if (retval == -EILSEQ) {
499 ftdi->disconnected += 1;
501 ftdi->disconnected += 1;
502 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
504 if (ftdi->disconnected > 0) {
505 ftdi_elan_abandon_completions(ftdi);
506 ftdi_elan_abandon_targets(ftdi);
508 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
515 * the sw_lock is initially held and will be freed
516 * after the FTDI has been synchronized
519 static void ftdi_elan_status_work(struct work_struct *work)
521 struct usb_ftdi *ftdi =
522 container_of(work, struct usb_ftdi, status_work.work);
523 int work_delay_in_msec = 0;
524 if (ftdi->disconnected > 0) {
525 ftdi_elan_put_kref(ftdi);
527 } else if (ftdi->synchronized == 0) {
528 down(&ftdi->sw_lock);
529 if (ftdi_elan_synchronize(ftdi) == 0) {
530 ftdi->synchronized = 1;
531 ftdi_command_queue_work(ftdi, 1);
532 ftdi_respond_queue_work(ftdi, 1);
534 work_delay_in_msec = 100;
536 dev_err(&ftdi->udev->dev, "synchronize failed\n");
538 work_delay_in_msec = 10 *1000;
540 } else if (ftdi->stuck_status > 0) {
541 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
542 ftdi->stuck_status = 0;
543 ftdi->synchronized = 0;
544 } else if ((ftdi->stuck_status++ % 60) == 1) {
545 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
547 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
549 work_delay_in_msec = 100;
550 } else if (ftdi->enumerated == 0) {
551 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
552 ftdi->enumerated = 1;
553 work_delay_in_msec = 250;
555 work_delay_in_msec = 1000;
556 } else if (ftdi->initialized == 0) {
557 if (ftdi_elan_setupOHCI(ftdi) == 0) {
558 ftdi->initialized = 1;
559 work_delay_in_msec = 500;
561 dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
562 work_delay_in_msec = 1 *1000;
564 } else if (ftdi->registered == 0) {
565 work_delay_in_msec = 10;
566 if (ftdi_elan_hcd_init(ftdi) == 0) {
567 ftdi->registered = 1;
569 dev_err(&ftdi->udev->dev, "register failed\n");
570 work_delay_in_msec = 250;
572 if (ftdi_elan_checkingPCI(ftdi) == 0) {
573 work_delay_in_msec = 250;
574 } else if (ftdi->controlreg & 0x00400000) {
575 if (ftdi->gone_away > 0) {
576 dev_err(&ftdi->udev->dev, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
577 ftdi->platform_dev.dev.parent,
578 &ftdi->platform_dev.dev);
579 platform_device_unregister(&ftdi->platform_dev);
580 ftdi->platform_dev.dev.parent = NULL;
581 ftdi->registered = 0;
582 ftdi->enumerated = 0;
583 ftdi->card_ejected = 0;
584 ftdi->initialized = 0;
587 ftdi_elan_flush_targets(ftdi);
588 work_delay_in_msec = 250;
590 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n");
591 ftdi_elan_cancel_targets(ftdi);
592 work_delay_in_msec = 500;
593 ftdi->enumerated = 0;
594 ftdi->initialized = 0;
597 if (ftdi->disconnected > 0) {
598 ftdi_elan_put_kref(ftdi);
601 ftdi_status_requeue_work(ftdi,
602 msecs_to_jiffies(work_delay_in_msec));
609 * file_operations for the jtag interface
611 * the usage count for the device is incremented on open()
612 * and decremented on release()
614 static int ftdi_elan_open(struct inode *inode, struct file *file)
617 struct usb_interface *interface;
619 subminor = iminor(inode);
620 interface = usb_find_interface(&ftdi_elan_driver, subminor);
623 pr_err("can't find device for minor %d\n", subminor);
626 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
630 if (down_interruptible(&ftdi->sw_lock)) {
633 ftdi_elan_get_kref(ftdi);
634 file->private_data = ftdi;
641 static int ftdi_elan_release(struct inode *inode, struct file *file)
643 struct usb_ftdi *ftdi = file->private_data;
646 up(&ftdi->sw_lock); /* decrement the count on our device */
647 ftdi_elan_put_kref(ftdi);
654 * blocking bulk reads are used to get data from the device
657 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
658 size_t count, loff_t *ppos)
660 char data[30 *3 + 4];
662 int m = (sizeof(data) - 1) / 3 - 1;
664 int retry_on_empty = 10;
665 int retry_on_timeout = 5;
666 struct usb_ftdi *ftdi = file->private_data;
667 if (ftdi->disconnected > 0) {
671 have:if (ftdi->bulk_in_left > 0) {
673 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
674 ftdi->bulk_in_left -= 1;
675 if (bytes_read < m) {
676 d += sprintf(d, " %02X", 0x000000FF & *p);
677 } else if (bytes_read > m) {
679 d += sprintf(d, " ..");
680 if (copy_to_user(buffer++, p, 1)) {
689 more:if (count > 0) {
690 int packet_bytes = 0;
691 int retval = usb_bulk_msg(ftdi->udev,
692 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
693 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
695 if (packet_bytes > 2) {
696 ftdi->bulk_in_left = packet_bytes - 2;
697 ftdi->bulk_in_last = 1;
699 } else if (retval == -ETIMEDOUT) {
700 if (retry_on_timeout-- > 0) {
702 } else if (bytes_read > 0) {
706 } else if (retval == 0) {
707 if (retry_on_empty-- > 0) {
717 static void ftdi_elan_write_bulk_callback(struct urb *urb)
719 struct usb_ftdi *ftdi = urb->context;
720 int status = urb->status;
722 if (status && !(status == -ENOENT || status == -ECONNRESET ||
723 status == -ESHUTDOWN)) {
724 dev_err(&ftdi->udev->dev,
725 "urb=%p write bulk status received: %d\n", urb, status);
727 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
728 urb->transfer_buffer, urb->transfer_dma);
731 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
732 char *buf, int command_size, int total_size)
736 int I = command_size;
737 int i = ftdi->command_head;
739 struct u132_command *command = &ftdi->command[COMMAND_MASK &
741 int F = command->follows;
742 u8 *f = command->buffer;
743 if (command->header & 0x80) {
744 ed_commands |= 1 << (0x3 & (command->header >> 5));
746 buf[b++] = command->header;
747 buf[b++] = (command->length >> 0) & 0x00FF;
748 buf[b++] = (command->length >> 8) & 0x00FF;
749 buf[b++] = command->address;
750 buf[b++] = command->width;
758 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
761 int I = command_size;
762 int i = ftdi->command_head;
764 struct u132_command *command = &ftdi->command[COMMAND_MASK &
766 total_size += 5 + command->follows;
770 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
777 int command_size = ftdi->command_next - ftdi->command_head;
778 if (command_size == 0)
780 total_size = ftdi_elan_total_command_size(ftdi, command_size);
781 urb = usb_alloc_urb(0, GFP_KERNEL);
784 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
787 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
788 command_size, total_size);
792 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
793 command_size, total_size);
794 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
795 ftdi->bulk_out_endpointAddr), buf, total_size,
796 ftdi_elan_write_bulk_callback, ftdi);
797 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
799 char diag[40 *3 + 4];
803 int s = (sizeof(diag) - 1) / 3;
805 while (s-- > 0 && m-- > 0) {
806 if (s > 0 || m == 0) {
807 d += sprintf(d, " %02X", *c++);
809 d += sprintf(d, " ..");
812 retval = usb_submit_urb(urb, GFP_KERNEL);
814 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
815 retval, urb, command_size, total_size);
816 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
820 usb_free_urb(urb); /* release our reference to this urb,
821 the USB core will eventually free it entirely */
822 ftdi->command_head += command_size;
823 ftdi_elan_kick_respond_queue(ftdi);
827 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
828 struct u132_target *target, u8 *buffer, int length)
830 struct urb *urb = target->urb;
831 int halted = target->halted;
832 int skipped = target->skipped;
833 int actual = target->actual;
834 int non_null = target->non_null;
835 int toggle_bits = target->toggle_bits;
836 int error_count = target->error_count;
837 int condition_code = target->condition_code;
838 int repeat_number = target->repeat_number;
839 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
840 int, int, int, int) = target->callback;
842 target->callback = NULL;
843 (*callback) (target->endp, urb, buffer, length, toggle_bits,
844 error_count, condition_code, repeat_number, halted, skipped,
848 static char *have_ed_set_response(struct usb_ftdi *ftdi,
849 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
852 int payload = (ed_length >> 0) & 0x07FF;
853 mutex_lock(&ftdi->u132_lock);
855 target->non_null = (ed_length >> 15) & 0x0001;
856 target->repeat_number = (ed_length >> 11) & 0x000F;
857 if (ed_type == 0x02 || ed_type == 0x03) {
858 if (payload == 0 || target->abandoning > 0) {
859 target->abandoning = 0;
860 mutex_unlock(&ftdi->u132_lock);
861 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
866 return ftdi->response;
868 ftdi->expected = 4 + payload;
870 mutex_unlock(&ftdi->u132_lock);
874 target->abandoning = 0;
875 mutex_unlock(&ftdi->u132_lock);
876 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
881 return ftdi->response;
885 static char *have_ed_get_response(struct usb_ftdi *ftdi,
886 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
889 mutex_lock(&ftdi->u132_lock);
890 target->condition_code = TD_DEVNOTRESP;
891 target->actual = (ed_length >> 0) & 0x01FF;
892 target->non_null = (ed_length >> 15) & 0x0001;
893 target->repeat_number = (ed_length >> 11) & 0x000F;
894 mutex_unlock(&ftdi->u132_lock);
896 ftdi_elan_do_callback(ftdi, target, NULL, 0);
897 target->abandoning = 0;
901 return ftdi->response;
906 * The engine tries to empty the FTDI fifo
908 * all responses found in the fifo data are dispatched thus
909 * the response buffer can only ever hold a maximum sized
910 * response from the Uxxx.
913 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
915 u8 *b = ftdi->response + ftdi->received;
917 int retry_on_empty = 1;
918 int retry_on_timeout = 3;
919 int empty_packets = 0;
921 int packet_bytes = 0;
922 int retval = usb_bulk_msg(ftdi->udev,
923 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
924 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
926 char diag[30 *3 + 4];
928 int m = packet_bytes;
929 u8 *c = ftdi->bulk_in_buffer;
930 int s = (sizeof(diag) - 1) / 3;
932 while (s-- > 0 && m-- > 0) {
933 if (s > 0 || m == 0) {
934 d += sprintf(d, " %02X", *c++);
936 d += sprintf(d, " ..");
938 if (packet_bytes > 2) {
939 ftdi->bulk_in_left = packet_bytes - 2;
940 ftdi->bulk_in_last = 1;
942 } else if (retval == -ETIMEDOUT) {
943 if (retry_on_timeout-- > 0) {
944 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
945 packet_bytes, bytes_read, diag);
947 } else if (bytes_read > 0) {
948 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
952 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
953 packet_bytes, bytes_read, diag);
956 } else if (retval == -EILSEQ) {
957 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
958 retval, packet_bytes, bytes_read, diag);
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);
964 } else if (packet_bytes == 2) {
965 unsigned char s0 = ftdi->bulk_in_buffer[0];
966 unsigned char s1 = ftdi->bulk_in_buffer[1];
968 if (s0 == 0x31 && s1 == 0x60) {
969 if (retry_on_empty-- > 0) {
973 } else if (s0 == 0x31 && s1 == 0x00) {
974 if (retry_on_empty-- > 0) {
979 if (retry_on_empty-- > 0) {
984 } else if (packet_bytes == 1) {
985 if (retry_on_empty-- > 0) {
990 if (retry_on_empty-- > 0) {
999 have:if (ftdi->bulk_in_left > 0) {
1000 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1002 ftdi->bulk_in_left -= 1;
1003 if (ftdi->received == 0 && c == 0xFF) {
1007 if (++ftdi->received < ftdi->expected) {
1009 } else if (ftdi->ed_found) {
1010 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1011 u16 ed_length = (ftdi->response[2] << 8) |
1013 struct u132_target *target = &ftdi->target[ed_number];
1014 int payload = (ed_length >> 0) & 0x07FF;
1015 char diag[30 *3 + 4];
1018 u8 *c = 4 + ftdi->response;
1019 int s = (sizeof(diag) - 1) / 3;
1021 while (s-- > 0 && m-- > 0) {
1022 if (s > 0 || m == 0) {
1023 d += sprintf(d, " %02X", *c++);
1025 d += sprintf(d, " ..");
1027 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1034 } else if (ftdi->expected == 8) {
1036 int respond_head = ftdi->respond_head++;
1037 struct u132_respond *respond = &ftdi->respond[
1038 RESPOND_MASK & respond_head];
1039 u32 data = ftdi->response[7];
1041 data |= ftdi->response[6];
1043 data |= ftdi->response[5];
1045 data |= ftdi->response[4];
1046 *respond->value = data;
1047 *respond->result = 0;
1048 complete(&respond->wait_completion);
1053 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1054 if (buscmd == 0x00) {
1055 } else if (buscmd == 0x02) {
1056 } else if (buscmd == 0x06) {
1057 } else if (buscmd == 0x0A) {
1059 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1063 if ((ftdi->response[0] & 0x80) == 0x00) {
1067 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1068 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1069 u16 ed_length = (ftdi->response[2] << 8) |
1071 struct u132_target *target = &ftdi->target[
1073 target->halted = (ftdi->response[0] >> 3) &
1075 target->skipped = (ftdi->response[0] >> 2) &
1077 target->toggle_bits = (ftdi->response[3] >> 6)
1079 target->error_count = (ftdi->response[3] >> 4)
1081 target->condition_code = (ftdi->response[
1083 if ((ftdi->response[0] & 0x10) == 0x00) {
1084 b = have_ed_set_response(ftdi, target,
1085 ed_length, ed_number, ed_type,
1089 b = have_ed_get_response(ftdi, target,
1090 ed_length, ed_number, ed_type,
1102 * create a urb, and a buffer for it, and copy the data to the urb
1105 static ssize_t ftdi_elan_write(struct file *file,
1106 const char __user *user_buffer, size_t count,
1112 struct usb_ftdi *ftdi = file->private_data;
1114 if (ftdi->disconnected > 0) {
1120 urb = usb_alloc_urb(0, GFP_KERNEL);
1125 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1126 &urb->transfer_dma);
1131 if (copy_from_user(buf, user_buffer, count)) {
1135 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1136 ftdi->bulk_out_endpointAddr), buf, count,
1137 ftdi_elan_write_bulk_callback, ftdi);
1138 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1139 retval = usb_submit_urb(urb, GFP_KERNEL);
1141 dev_err(&ftdi->udev->dev,
1142 "failed submitting write urb, error %d\n", retval);
1150 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1157 static const struct file_operations ftdi_elan_fops = {
1158 .owner = THIS_MODULE,
1159 .llseek = no_llseek,
1160 .read = ftdi_elan_read,
1161 .write = ftdi_elan_write,
1162 .open = ftdi_elan_open,
1163 .release = ftdi_elan_release,
1167 * usb class driver info in order to get a minor number from the usb core,
1168 * and to have the device registered with the driver core
1170 static struct usb_class_driver ftdi_elan_jtag_class = {
1171 .name = "ftdi-%d-jtag",
1172 .fops = &ftdi_elan_fops,
1173 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1177 * the following definitions are for the
1178 * ELAN FPGA state machgine processor that
1179 * lies on the other side of the FTDI chip
1181 #define cPCIu132rd 0x0
1182 #define cPCIu132wr 0x1
1183 #define cPCIiord 0x2
1184 #define cPCIiowr 0x3
1185 #define cPCImemrd 0x6
1186 #define cPCImemwr 0x7
1187 #define cPCIcfgrd 0xA
1188 #define cPCIcfgwr 0xB
1189 #define cPCInull 0xF
1190 #define cU132cmd_status 0x0
1191 #define cU132flash 0x1
1192 #define cPIDsetup 0x0
1195 #define cPIDinonce 0x3
1196 #define cCCnoerror 0x0
1198 #define cCCbitstuff 0x2
1199 #define cCCtoggle 0x3
1200 #define cCCstall 0x4
1201 #define cCCnoresp 0x5
1202 #define cCCbadpid1 0x6
1203 #define cCCbadpid2 0x7
1204 #define cCCdataoverrun 0x8
1205 #define cCCdataunderrun 0x9
1206 #define cCCbuffoverrun 0xC
1207 #define cCCbuffunderrun 0xD
1208 #define cCCnotaccessed 0xF
1209 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1211 wait:if (ftdi->disconnected > 0) {
1215 mutex_lock(&ftdi->u132_lock);
1216 command_size = ftdi->command_next - ftdi->command_head;
1217 if (command_size < COMMAND_SIZE) {
1218 struct u132_command *command = &ftdi->command[
1219 COMMAND_MASK & ftdi->command_next];
1220 command->header = 0x00 | cPCIu132wr;
1221 command->length = 0x04;
1222 command->address = 0x00;
1223 command->width = 0x00;
1224 command->follows = 4;
1225 command->value = data;
1226 command->buffer = &command->value;
1227 ftdi->command_next += 1;
1228 ftdi_elan_kick_command_queue(ftdi);
1229 mutex_unlock(&ftdi->u132_lock);
1232 mutex_unlock(&ftdi->u132_lock);
1239 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1242 u8 addressofs = config_offset / 4;
1243 wait:if (ftdi->disconnected > 0) {
1247 mutex_lock(&ftdi->u132_lock);
1248 command_size = ftdi->command_next - ftdi->command_head;
1249 if (command_size < COMMAND_SIZE) {
1250 struct u132_command *command = &ftdi->command[
1251 COMMAND_MASK & ftdi->command_next];
1252 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1253 command->length = 0x04;
1254 command->address = addressofs;
1255 command->width = 0x00 | (width & 0x0F);
1256 command->follows = 4;
1257 command->value = data;
1258 command->buffer = &command->value;
1259 ftdi->command_next += 1;
1260 ftdi_elan_kick_command_queue(ftdi);
1261 mutex_unlock(&ftdi->u132_lock);
1264 mutex_unlock(&ftdi->u132_lock);
1271 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1274 u8 addressofs = mem_offset / 4;
1275 wait:if (ftdi->disconnected > 0) {
1279 mutex_lock(&ftdi->u132_lock);
1280 command_size = ftdi->command_next - ftdi->command_head;
1281 if (command_size < COMMAND_SIZE) {
1282 struct u132_command *command = &ftdi->command[
1283 COMMAND_MASK & ftdi->command_next];
1284 command->header = 0x00 | (cPCImemwr & 0x0F);
1285 command->length = 0x04;
1286 command->address = addressofs;
1287 command->width = 0x00 | (width & 0x0F);
1288 command->follows = 4;
1289 command->value = data;
1290 command->buffer = &command->value;
1291 ftdi->command_next += 1;
1292 ftdi_elan_kick_command_queue(ftdi);
1293 mutex_unlock(&ftdi->u132_lock);
1296 mutex_unlock(&ftdi->u132_lock);
1303 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1306 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1307 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1311 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1312 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1314 wait:if (ftdi->disconnected > 0) {
1319 mutex_lock(&ftdi->u132_lock);
1320 command_size = ftdi->command_next - ftdi->command_head;
1321 respond_size = ftdi->respond_next - ftdi->respond_head;
1322 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1324 struct u132_command *command = &ftdi->command[
1325 COMMAND_MASK & ftdi->command_next];
1326 struct u132_respond *respond = &ftdi->respond[
1327 RESPOND_MASK & ftdi->respond_next];
1328 int result = -ENODEV;
1329 respond->result = &result;
1330 respond->header = command->header = 0x00 | cPCIu132rd;
1331 command->length = 0x04;
1332 respond->address = command->address = cU132cmd_status;
1333 command->width = 0x00;
1334 command->follows = 0;
1336 command->buffer = NULL;
1337 respond->value = data;
1338 init_completion(&respond->wait_completion);
1339 ftdi->command_next += 1;
1340 ftdi->respond_next += 1;
1341 ftdi_elan_kick_command_queue(ftdi);
1342 mutex_unlock(&ftdi->u132_lock);
1343 wait_for_completion(&respond->wait_completion);
1346 mutex_unlock(&ftdi->u132_lock);
1353 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1354 u8 width, u32 *data)
1356 u8 addressofs = config_offset / 4;
1357 wait:if (ftdi->disconnected > 0) {
1362 mutex_lock(&ftdi->u132_lock);
1363 command_size = ftdi->command_next - ftdi->command_head;
1364 respond_size = ftdi->respond_next - ftdi->respond_head;
1365 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1367 struct u132_command *command = &ftdi->command[
1368 COMMAND_MASK & ftdi->command_next];
1369 struct u132_respond *respond = &ftdi->respond[
1370 RESPOND_MASK & ftdi->respond_next];
1371 int result = -ENODEV;
1372 respond->result = &result;
1373 respond->header = command->header = 0x00 | (cPCIcfgrd &
1375 command->length = 0x04;
1376 respond->address = command->address = addressofs;
1377 command->width = 0x00 | (width & 0x0F);
1378 command->follows = 0;
1380 command->buffer = NULL;
1381 respond->value = data;
1382 init_completion(&respond->wait_completion);
1383 ftdi->command_next += 1;
1384 ftdi->respond_next += 1;
1385 ftdi_elan_kick_command_queue(ftdi);
1386 mutex_unlock(&ftdi->u132_lock);
1387 wait_for_completion(&respond->wait_completion);
1390 mutex_unlock(&ftdi->u132_lock);
1397 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1398 u8 width, u32 *data)
1400 u8 addressofs = mem_offset / 4;
1401 wait:if (ftdi->disconnected > 0) {
1406 mutex_lock(&ftdi->u132_lock);
1407 command_size = ftdi->command_next - ftdi->command_head;
1408 respond_size = ftdi->respond_next - ftdi->respond_head;
1409 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1411 struct u132_command *command = &ftdi->command[
1412 COMMAND_MASK & ftdi->command_next];
1413 struct u132_respond *respond = &ftdi->respond[
1414 RESPOND_MASK & ftdi->respond_next];
1415 int result = -ENODEV;
1416 respond->result = &result;
1417 respond->header = command->header = 0x00 | (cPCImemrd &
1419 command->length = 0x04;
1420 respond->address = command->address = addressofs;
1421 command->width = 0x00 | (width & 0x0F);
1422 command->follows = 0;
1424 command->buffer = NULL;
1425 respond->value = data;
1426 init_completion(&respond->wait_completion);
1427 ftdi->command_next += 1;
1428 ftdi->respond_next += 1;
1429 ftdi_elan_kick_command_queue(ftdi);
1430 mutex_unlock(&ftdi->u132_lock);
1431 wait_for_completion(&respond->wait_completion);
1434 mutex_unlock(&ftdi->u132_lock);
1441 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1442 u8 width, u32 *data)
1444 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1445 if (ftdi->initialized == 0) {
1448 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1452 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1453 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1454 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1455 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1456 int toggle_bits, int error_count, int condition_code, int repeat_number,
1457 int halted, int skipped, int actual, int non_null))
1459 u8 ed = ed_number - 1;
1460 wait:if (ftdi->disconnected > 0) {
1462 } else if (ftdi->initialized == 0) {
1466 mutex_lock(&ftdi->u132_lock);
1467 command_size = ftdi->command_next - ftdi->command_head;
1468 if (command_size < COMMAND_SIZE) {
1469 struct u132_target *target = &ftdi->target[ed];
1470 struct u132_command *command = &ftdi->command[
1471 COMMAND_MASK & ftdi->command_next];
1472 command->header = 0x80 | (ed << 5);
1473 command->length = 0x8007;
1474 command->address = (toggle_bits << 6) | (ep_number << 2)
1476 command->width = usb_maxpacket(urb->dev, urb->pipe,
1477 usb_pipeout(urb->pipe));
1478 command->follows = 8;
1480 command->buffer = urb->setup_packet;
1481 target->callback = callback;
1482 target->endp = endp;
1485 ftdi->command_next += 1;
1486 ftdi_elan_kick_command_queue(ftdi);
1487 mutex_unlock(&ftdi->u132_lock);
1490 mutex_unlock(&ftdi->u132_lock);
1497 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1498 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1499 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1500 int toggle_bits, int error_count, int condition_code, int repeat_number,
1501 int halted, int skipped, int actual, int non_null))
1503 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1504 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1505 ep_number, toggle_bits, callback);
1509 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1510 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1511 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1512 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1513 int toggle_bits, int error_count, int condition_code, int repeat_number,
1514 int halted, int skipped, int actual, int non_null))
1516 u8 ed = ed_number - 1;
1517 wait:if (ftdi->disconnected > 0) {
1519 } else if (ftdi->initialized == 0) {
1523 mutex_lock(&ftdi->u132_lock);
1524 command_size = ftdi->command_next - ftdi->command_head;
1525 if (command_size < COMMAND_SIZE) {
1526 struct u132_target *target = &ftdi->target[ed];
1527 struct u132_command *command = &ftdi->command[
1528 COMMAND_MASK & ftdi->command_next];
1529 u32 remaining_length = urb->transfer_buffer_length -
1531 command->header = 0x82 | (ed << 5);
1532 if (remaining_length == 0) {
1533 command->length = 0x0000;
1534 } else if (remaining_length > 1024) {
1535 command->length = 0x8000 | 1023;
1537 command->length = 0x8000 | (remaining_length -
1539 command->address = (toggle_bits << 6) | (ep_number << 2)
1541 command->width = usb_maxpacket(urb->dev, urb->pipe,
1542 usb_pipeout(urb->pipe));
1543 command->follows = 0;
1545 command->buffer = NULL;
1546 target->callback = callback;
1547 target->endp = endp;
1550 ftdi->command_next += 1;
1551 ftdi_elan_kick_command_queue(ftdi);
1552 mutex_unlock(&ftdi->u132_lock);
1555 mutex_unlock(&ftdi->u132_lock);
1562 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1563 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1564 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1565 int toggle_bits, int error_count, int condition_code, int repeat_number,
1566 int halted, int skipped, int actual, int non_null))
1568 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1569 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1570 ep_number, toggle_bits, callback);
1574 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1575 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1576 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1577 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1578 int toggle_bits, int error_count, int condition_code, int repeat_number,
1579 int halted, int skipped, int actual, int non_null))
1581 u8 ed = ed_number - 1;
1582 wait:if (ftdi->disconnected > 0) {
1584 } else if (ftdi->initialized == 0) {
1588 mutex_lock(&ftdi->u132_lock);
1589 command_size = ftdi->command_next - ftdi->command_head;
1590 if (command_size < COMMAND_SIZE) {
1591 struct u132_target *target = &ftdi->target[ed];
1592 struct u132_command *command = &ftdi->command[
1593 COMMAND_MASK & ftdi->command_next];
1594 command->header = 0x81 | (ed << 5);
1595 command->length = 0x0000;
1596 command->address = (toggle_bits << 6) | (ep_number << 2)
1598 command->width = usb_maxpacket(urb->dev, urb->pipe,
1599 usb_pipeout(urb->pipe));
1600 command->follows = 0;
1602 command->buffer = NULL;
1603 target->callback = callback;
1604 target->endp = endp;
1607 ftdi->command_next += 1;
1608 ftdi_elan_kick_command_queue(ftdi);
1609 mutex_unlock(&ftdi->u132_lock);
1612 mutex_unlock(&ftdi->u132_lock);
1619 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1620 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1621 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1622 int toggle_bits, int error_count, int condition_code, int repeat_number,
1623 int halted, int skipped, int actual, int non_null))
1625 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1626 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1627 ep_number, toggle_bits, callback);
1631 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1632 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1633 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1634 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1635 int toggle_bits, int error_count, int condition_code, int repeat_number,
1636 int halted, int skipped, int actual, int non_null))
1638 u8 ed = ed_number - 1;
1639 wait:if (ftdi->disconnected > 0) {
1641 } else if (ftdi->initialized == 0) {
1645 mutex_lock(&ftdi->u132_lock);
1646 command_size = ftdi->command_next - ftdi->command_head;
1647 if (command_size < COMMAND_SIZE) {
1651 char data[30 *3 + 4];
1653 int m = (sizeof(data) - 1) / 3 - 1;
1655 struct u132_target *target = &ftdi->target[ed];
1656 struct u132_command *command = &ftdi->command[
1657 COMMAND_MASK & ftdi->command_next];
1658 command->header = 0x81 | (ed << 5);
1659 command->address = (toggle_bits << 6) | (ep_number << 2)
1661 command->width = usb_maxpacket(urb->dev, urb->pipe,
1662 usb_pipeout(urb->pipe));
1663 command->follows = min_t(u32, 1024,
1664 urb->transfer_buffer_length -
1665 urb->actual_length);
1667 command->buffer = urb->transfer_buffer +
1669 command->length = 0x8000 | (command->follows - 1);
1670 b = command->buffer;
1671 urb_size = command->follows;
1673 while (urb_size-- > 0) {
1675 } else if (i++ < m) {
1676 int w = sprintf(d, " %02X", *b++);
1680 d += sprintf(d, " ..");
1682 target->callback = callback;
1683 target->endp = endp;
1686 ftdi->command_next += 1;
1687 ftdi_elan_kick_command_queue(ftdi);
1688 mutex_unlock(&ftdi->u132_lock);
1691 mutex_unlock(&ftdi->u132_lock);
1698 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1699 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1700 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1701 int toggle_bits, int error_count, int condition_code, int repeat_number,
1702 int halted, int skipped, int actual, int non_null))
1704 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1705 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1706 ep_number, toggle_bits, callback);
1710 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1711 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1712 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1713 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1714 int toggle_bits, int error_count, int condition_code, int repeat_number,
1715 int halted, int skipped, int actual, int non_null))
1717 u8 ed = ed_number - 1;
1718 wait:if (ftdi->disconnected > 0) {
1720 } else if (ftdi->initialized == 0) {
1724 mutex_lock(&ftdi->u132_lock);
1725 command_size = ftdi->command_next - ftdi->command_head;
1726 if (command_size < COMMAND_SIZE) {
1727 u32 remaining_length = urb->transfer_buffer_length -
1729 struct u132_target *target = &ftdi->target[ed];
1730 struct u132_command *command = &ftdi->command[
1731 COMMAND_MASK & ftdi->command_next];
1732 command->header = 0x83 | (ed << 5);
1733 if (remaining_length == 0) {
1734 command->length = 0x0000;
1735 } else if (remaining_length > 1024) {
1736 command->length = 0x8000 | 1023;
1738 command->length = 0x8000 | (remaining_length -
1740 command->address = (toggle_bits << 6) | (ep_number << 2)
1742 command->width = usb_maxpacket(urb->dev, urb->pipe,
1743 usb_pipeout(urb->pipe));
1744 command->follows = 0;
1746 command->buffer = NULL;
1747 target->callback = callback;
1748 target->endp = endp;
1751 ftdi->command_next += 1;
1752 ftdi_elan_kick_command_queue(ftdi);
1753 mutex_unlock(&ftdi->u132_lock);
1756 mutex_unlock(&ftdi->u132_lock);
1763 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1764 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1765 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1766 int toggle_bits, int error_count, int condition_code, int repeat_number,
1767 int halted, int skipped, int actual, int non_null))
1769 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1770 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1771 ep_number, toggle_bits, callback);
1775 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1776 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1779 u8 ed = ed_number - 1;
1780 if (ftdi->disconnected > 0) {
1782 } else if (ftdi->initialized == 0) {
1785 struct u132_target *target = &ftdi->target[ed];
1786 mutex_lock(&ftdi->u132_lock);
1787 if (target->abandoning > 0) {
1788 mutex_unlock(&ftdi->u132_lock);
1791 target->abandoning = 1;
1792 wait_1:if (target->active == 1) {
1793 int command_size = ftdi->command_next -
1795 if (command_size < COMMAND_SIZE) {
1796 struct u132_command *command =
1797 &ftdi->command[COMMAND_MASK &
1798 ftdi->command_next];
1799 command->header = 0x80 | (ed << 5) |
1801 command->length = 0x00;
1802 command->address = 0x00;
1803 command->width = 0x00;
1804 command->follows = 0;
1806 command->buffer = &command->value;
1807 ftdi->command_next += 1;
1808 ftdi_elan_kick_command_queue(ftdi);
1810 mutex_unlock(&ftdi->u132_lock);
1812 mutex_lock(&ftdi->u132_lock);
1816 mutex_unlock(&ftdi->u132_lock);
1822 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1825 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1826 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1830 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1831 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1833 int retry_on_empty = 10;
1834 int retry_on_timeout = 5;
1835 int retry_on_status = 20;
1837 int packet_bytes = 0;
1838 int retval = usb_bulk_msg(ftdi->udev,
1839 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1840 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1841 &packet_bytes, 100);
1842 if (packet_bytes > 2) {
1843 char diag[30 *3 + 4];
1845 int m = (sizeof(diag) - 1) / 3 - 1;
1846 char *b = ftdi->bulk_in_buffer;
1849 while (packet_bytes-- > 0) {
1851 if (bytes_read < m) {
1852 d += sprintf(d, " %02X",
1854 } else if (bytes_read > m) {
1856 d += sprintf(d, " ..");
1861 } else if (packet_bytes > 1) {
1862 char s1 = ftdi->bulk_in_buffer[0];
1863 char s2 = ftdi->bulk_in_buffer[1];
1864 if (s1 == 0x31 && s2 == 0x60) {
1866 } else if (retry_on_status-- > 0) {
1869 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1872 } else if (packet_bytes > 0) {
1873 char b1 = ftdi->bulk_in_buffer[0];
1874 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1876 if (retry_on_status-- > 0) {
1879 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1882 } else if (retval == -ETIMEDOUT) {
1883 if (retry_on_timeout-- > 0) {
1886 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1889 } else if (retval == 0) {
1890 if (retry_on_empty-- > 0) {
1893 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1897 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1906 * send the long flush sequence
1909 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1916 urb = usb_alloc_urb(0, GFP_KERNEL);
1919 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1921 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1927 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1928 ftdi->bulk_out_endpointAddr), buf, i,
1929 ftdi_elan_write_bulk_callback, ftdi);
1930 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1931 retval = usb_submit_urb(urb, GFP_KERNEL);
1933 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1934 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1944 * send the reset sequence
1947 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1954 urb = usb_alloc_urb(0, GFP_KERNEL);
1957 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1959 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1967 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1968 ftdi->bulk_out_endpointAddr), buf, i,
1969 ftdi_elan_write_bulk_callback, ftdi);
1970 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1971 retval = usb_submit_urb(urb, GFP_KERNEL);
1973 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
1974 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1982 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
1986 int retry_on_timeout = 5;
1987 int retry_on_empty = 10;
1989 retval = ftdi_elan_flush_input_fifo(ftdi);
1992 ftdi->bulk_in_left = 0;
1993 ftdi->bulk_in_last = -1;
1994 while (long_stop-- > 0) {
1997 retval = ftdi_elan_synchronize_flush(ftdi);
2000 retval = ftdi_elan_flush_input_fifo(ftdi);
2003 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2009 int packet_bytes = 0;
2010 retval = usb_bulk_msg(ftdi->udev,
2011 usb_rcvbulkpipe(ftdi->udev,
2012 ftdi->bulk_in_endpointAddr),
2013 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2014 &packet_bytes, 500);
2015 if (packet_bytes > 2) {
2016 char diag[30 *3 + 4];
2018 int m = (sizeof(diag) - 1) / 3 - 1;
2019 char *b = ftdi->bulk_in_buffer;
2021 unsigned char c = 0;
2023 while (packet_bytes-- > 0) {
2025 if (bytes_read < m) {
2026 d += sprintf(d, " %02X", c);
2027 } else if (bytes_read > m) {
2029 d += sprintf(d, " ..");
2038 } else if (read_stop-- > 0) {
2041 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2045 } else if (packet_bytes > 1) {
2046 unsigned char s1 = ftdi->bulk_in_buffer[0];
2047 unsigned char s2 = ftdi->bulk_in_buffer[1];
2048 if (s1 == 0x31 && s2 == 0x00) {
2049 if (read_stuck-- > 0) {
2053 } else if (s1 == 0x31 && s2 == 0x60) {
2054 if (read_stop-- > 0) {
2057 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2061 if (read_stop-- > 0) {
2064 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2068 } else if (packet_bytes > 0) {
2069 if (read_stop-- > 0) {
2072 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2075 } else if (retval == -ETIMEDOUT) {
2076 if (retry_on_timeout-- > 0) {
2079 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2082 } else if (retval == 0) {
2083 if (retry_on_empty-- > 0) {
2086 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2091 dev_err(&ftdi->udev->dev, "error = %d\n",
2093 if (read_stop-- > 0) {
2096 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2102 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2106 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2108 int retry_on_empty = 10;
2109 int retry_on_timeout = 5;
2110 int retry_on_status = 50;
2112 int packet_bytes = 0;
2113 int retval = usb_bulk_msg(ftdi->udev,
2114 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2115 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2116 &packet_bytes, 1000);
2117 if (packet_bytes > 2) {
2118 char diag[30 *3 + 4];
2120 int m = (sizeof(diag) - 1) / 3 - 1;
2121 char *b = ftdi->bulk_in_buffer;
2124 while (packet_bytes-- > 0) {
2126 if (bytes_read < m) {
2127 d += sprintf(d, " %02X",
2129 } else if (bytes_read > m) {
2131 d += sprintf(d, " ..");
2136 } else if (packet_bytes > 1) {
2137 char s1 = ftdi->bulk_in_buffer[0];
2138 char s2 = ftdi->bulk_in_buffer[1];
2139 if (s1 == 0x31 && s2 == 0x60) {
2141 } else if (retry_on_status-- > 0) {
2146 } else if (packet_bytes > 0) {
2147 char b1 = ftdi->bulk_in_buffer[0];
2148 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2149 if (retry_on_status-- > 0) {
2153 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2156 } else if (retval == -ETIMEDOUT) {
2157 if (retry_on_timeout-- > 0) {
2160 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2163 } else if (retval == 0) {
2164 if (retry_on_empty-- > 0) {
2167 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2171 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2178 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2180 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2183 if (ftdi->controlreg & 0x00400000) {
2184 if (ftdi->card_ejected) {
2186 ftdi->card_ejected = 1;
2187 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2192 u8 fn = ftdi->function - 1;
2193 int activePCIfn = fn << 8;
2198 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2202 pciVID = pcidata & 0xFFFF;
2203 pciPID = (pcidata >> 16) & 0xFFFF;
2204 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2205 ftdi->platform_data.device) {
2208 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2209 ftdi->platform_data.vendor, pciVID,
2210 ftdi->platform_data.device, pciPID);
2217 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2218 offsetof(struct ohci_regs, member), 0, data);
2219 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2220 offsetof(struct ohci_regs, member), 0, data);
2222 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2223 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2225 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2239 int mask = OHCI_INTR_INIT;
2241 int reset_timeout = 30; /* ... allow extra time */
2243 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2246 retval = ftdi_read_pcimem(ftdi, control, &control);
2249 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2252 num_ports = rh_a & RH_A_NDP;
2253 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2256 hc_fminterval &= 0x3fff;
2257 if (hc_fminterval != FI) {
2259 hc_fminterval |= FSMP(hc_fminterval) << 16;
2260 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2263 switch (hc_control & OHCI_CTRL_HCFS) {
2267 case OHCI_USB_SUSPEND:
2268 case OHCI_USB_RESUME:
2269 hc_control &= OHCI_CTRL_RWC;
2270 hc_control |= OHCI_USB_RESUME;
2274 hc_control &= OHCI_CTRL_RWC;
2275 hc_control |= OHCI_USB_RESET;
2279 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2282 retval = ftdi_read_pcimem(ftdi, control, &control);
2286 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2289 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2290 for (temp = 0; temp < num_ports; temp++) {
2291 retval = ftdi_write_pcimem(ftdi,
2292 roothub.portstatus[temp], RH_PS_LSDA);
2297 retval = ftdi_read_pcimem(ftdi, control, &control);
2300 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2303 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2307 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2310 if (0 != (status & OHCI_HCR)) {
2311 if (--reset_timeout == 0) {
2312 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2320 if (quirk & OHCI_QUIRK_INITRESET) {
2321 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2324 retval = ftdi_read_pcimem(ftdi, control, &control);
2328 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2331 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2334 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2337 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2340 retval = ftdi_write_pcimem(ftdi, fminterval,
2341 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2344 retval = ftdi_write_pcimem(ftdi, periodicstart,
2345 ((9 *hc_fminterval) / 10) & 0x3fff);
2348 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2351 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2354 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2355 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2356 quirk |= OHCI_QUIRK_INITRESET;
2359 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2360 fminterval, periodicstart);
2361 } /* start controller operations */
2362 hc_control &= OHCI_CTRL_RWC;
2363 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2364 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2367 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2370 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2373 retval = ftdi_read_pcimem(ftdi, control, &control);
2376 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2379 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2382 retval = ftdi_write_pcimem(ftdi, intrdisable,
2383 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2384 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2387 return retval; /* handle root hub init quirks ... */
2388 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2391 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2392 if (quirk & OHCI_QUIRK_SUPERIO) {
2393 roothub_a |= RH_A_NOCP;
2394 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2395 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2398 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2399 roothub_a |= RH_A_NPS;
2400 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2404 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2407 retval = ftdi_write_pcimem(ftdi, roothub.b,
2408 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2411 retval = ftdi_read_pcimem(ftdi, control, &control);
2414 mdelay((roothub_a >> 23) & 0x1fe);
2415 for (temp = 0; temp < num_ports; temp++) {
2417 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2427 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2433 int activePCIfn = fn << 8;
2434 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2438 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2442 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2446 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2450 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2455 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2459 latence_timer &= 0xFFFF00FF;
2460 latence_timer |= 0x00001600;
2461 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2465 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2470 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2474 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2478 for (reg = 0; reg <= 0x54; reg += 4) {
2479 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2486 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2492 int activePCIfn = fn << 8;
2493 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2497 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2501 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2505 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2509 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2514 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2518 latence_timer &= 0xFFFF00FF;
2519 latence_timer |= 0x00001600;
2520 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2524 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2529 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2533 return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2536 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2540 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2543 result = ftdi_elan_check_controller(ftdi, quirk);
2544 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2550 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2555 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2558 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2562 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2565 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2568 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2571 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2574 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2578 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2581 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2584 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2587 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2590 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2594 sensebits = (controlreg >> 16) & 0x000F;
2595 if (0x0D == sensebits)
2601 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2607 int activePCIfn = 0;
2608 int max_devices = 0;
2609 int controllers = 0;
2610 int unrecognized = 0;
2612 for (fn = 0; (fn < 4); fn++) {
2616 activePCIfn = fn << 8;
2617 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2621 pciVID = pcidata & 0xFFFF;
2622 pciPID = (pcidata >> 16) & 0xFFFF;
2623 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2624 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2626 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2628 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2630 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2631 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2633 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2635 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2637 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2638 devices = ftdi_elan_found_controller(ftdi, fn,
2641 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2642 devices = ftdi_elan_found_controller(ftdi, fn,
2643 OHCI_QUIRK_ZFMICRO);
2645 } else if (0 == pcidata) {
2648 if (devices > max_devices) {
2649 max_devices = devices;
2650 ftdi->function = fn + 1;
2651 ftdi->platform_data.vendor = pciVID;
2652 ftdi->platform_data.device = pciPID;
2655 if (ftdi->function > 0) {
2656 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2657 } else if (controllers > 0) {
2659 } else if (unrecognized > 0) {
2662 ftdi->enumerated = 0;
2669 * we use only the first bulk-in and bulk-out endpoints
2671 static int ftdi_elan_probe(struct usb_interface *interface,
2672 const struct usb_device_id *id)
2674 struct usb_host_interface *iface_desc;
2675 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
2677 struct usb_ftdi *ftdi;
2679 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2683 mutex_lock(&ftdi_module_lock);
2684 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2685 ftdi->sequence_num = ++ftdi_instances;
2686 mutex_unlock(&ftdi_module_lock);
2687 ftdi_elan_init_kref(ftdi);
2688 sema_init(&ftdi->sw_lock, 1);
2689 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2690 ftdi->interface = interface;
2691 mutex_init(&ftdi->u132_lock);
2694 iface_desc = interface->cur_altsetting;
2695 retval = usb_find_common_endpoints(iface_desc,
2696 &bulk_in, &bulk_out, NULL, NULL);
2698 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2702 ftdi->bulk_in_size = usb_endpoint_maxp(bulk_in);
2703 ftdi->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
2704 ftdi->bulk_in_buffer = kmalloc(ftdi->bulk_in_size, GFP_KERNEL);
2705 if (!ftdi->bulk_in_buffer) {
2710 ftdi->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
2712 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2713 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2714 ftdi->bulk_out_endpointAddr);
2715 usb_set_intfdata(interface, ftdi);
2716 if (iface_desc->desc.bInterfaceNumber == 0 &&
2717 ftdi->bulk_in_endpointAddr == 0x81 &&
2718 ftdi->bulk_out_endpointAddr == 0x02) {
2719 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2721 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2722 usb_set_intfdata(interface, NULL);
2726 ftdi->class = &ftdi_elan_jtag_class;
2727 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2728 ftdi, iface_desc->desc.bInterfaceNumber,
2732 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2733 ftdi->bulk_in_endpointAddr == 0x83 &&
2734 ftdi->bulk_out_endpointAddr == 0x04) {
2736 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2737 ftdi, iface_desc->desc.bInterfaceNumber);
2738 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2739 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2740 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2741 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2744 dev_err(&ftdi->udev->dev,
2745 "Could not find ELAN's U132 device\n");
2750 ftdi_elan_put_kref(ftdi);
2755 static void ftdi_elan_disconnect(struct usb_interface *interface)
2757 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2758 ftdi->disconnected += 1;
2760 int minor = interface->minor;
2761 struct usb_class_driver *class = ftdi->class;
2762 usb_set_intfdata(interface, NULL);
2763 usb_deregister_dev(interface, class);
2764 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2767 ftdi_status_cancel_work(ftdi);
2768 ftdi_command_cancel_work(ftdi);
2769 ftdi_response_cancel_work(ftdi);
2770 ftdi_elan_abandon_completions(ftdi);
2771 ftdi_elan_abandon_targets(ftdi);
2772 if (ftdi->registered) {
2773 platform_device_unregister(&ftdi->platform_dev);
2774 ftdi->synchronized = 0;
2775 ftdi->enumerated = 0;
2776 ftdi->initialized = 0;
2777 ftdi->registered = 0;
2779 ftdi->disconnected += 1;
2780 usb_set_intfdata(interface, NULL);
2781 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2783 ftdi_elan_put_kref(ftdi);
2786 static struct usb_driver ftdi_elan_driver = {
2787 .name = "ftdi-elan",
2788 .probe = ftdi_elan_probe,
2789 .disconnect = ftdi_elan_disconnect,
2790 .id_table = ftdi_elan_table,
2792 static int __init ftdi_elan_init(void)
2795 pr_info("driver %s\n", ftdi_elan_driver.name);
2796 mutex_init(&ftdi_module_lock);
2797 INIT_LIST_HEAD(&ftdi_static_list);
2798 result = usb_register(&ftdi_elan_driver);
2800 pr_err("usb_register failed. Error number %d\n", result);
2806 static void __exit ftdi_elan_exit(void)
2808 struct usb_ftdi *ftdi;
2809 struct usb_ftdi *temp;
2810 usb_deregister(&ftdi_elan_driver);
2811 pr_info("ftdi_u132 driver deregistered\n");
2812 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2813 ftdi_status_cancel_work(ftdi);
2814 ftdi_command_cancel_work(ftdi);
2815 ftdi_response_cancel_work(ftdi);
2820 module_init(ftdi_elan_init);
2821 module_exit(ftdi_elan_exit);