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.
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/init.h>
41 #include <linux/list.h>
42 #include <linux/ioctl.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/kref.h>
46 #include <asm/uaccess.h>
47 #include <linux/usb.h>
48 #include <linux/workqueue.h>
49 #include <linux/platform_device.h>
50 MODULE_AUTHOR("Tony Olech");
51 MODULE_DESCRIPTION("FTDI ELAN driver");
52 MODULE_LICENSE("GPL");
53 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
54 extern struct platform_driver u132_platform_driver;
55 static struct workqueue_struct *status_queue;
56 static struct workqueue_struct *command_queue;
57 static struct workqueue_struct *respond_queue;
59 * ftdi_module_lock exists to protect access to global variables
62 static struct semaphore ftdi_module_lock;
63 static int ftdi_instances = 0;
64 static struct list_head ftdi_static_list;
66 * end of the global variables protected by ftdi_module_lock
69 #define TD_DEVNOTRESP 5
70 /* Define these values to match your devices*/
71 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
72 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
73 /* table of devices that work with this driver*/
74 static struct usb_device_id ftdi_elan_table[] = {
75 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
76 { /* Terminating entry */ }
79 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
80 /* only the jtag(firmware upgrade device) interface requires
81 * a device file and corresponding minor number, but the
82 * interface is created unconditionally - I suppose it could
83 * be configured or not according to a module parameter.
84 * But since we(now) require one interface per device,
85 * and since it unlikely that a normal installation would
86 * require more than a couple of elan-ftdi devices, 8 seems
87 * like a reasonable limit to have here, and if someone
88 * really requires more than 8 devices, then they can frig the
91 #define USB_FTDI_ELAN_MINOR_BASE 192
92 #define COMMAND_BITS 5
93 #define COMMAND_SIZE (1<<COMMAND_BITS)
94 #define COMMAND_MASK (COMMAND_SIZE-1)
104 #define RESPOND_BITS 5
105 #define RESPOND_SIZE (1<<RESPOND_BITS)
106 #define RESPOND_MASK (RESPOND_SIZE-1)
107 struct u132_respond {
112 struct completion wait_completion;
127 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
128 int toggle_bits, int error_count, int condition_code,
129 int repeat_number, int halted, int skipped, int actual,
132 /* Structure to hold all of our device specific stuff*/
134 struct list_head ftdi_list;
135 struct semaphore u132_lock;
138 struct u132_command command[COMMAND_SIZE];
141 struct u132_respond respond[RESPOND_SIZE];
142 struct u132_target target[4];
143 char device_name[16];
144 unsigned synchronized:1;
145 unsigned enumerated:1;
146 unsigned registered:1;
147 unsigned initialized:1;
148 unsigned card_ejected:1;
154 int status_queue_delay;
155 struct semaphore sw_lock;
156 struct usb_device *udev;
157 struct usb_interface *interface;
158 struct usb_class_driver *class;
159 struct work_struct status_work;
160 struct work_struct command_work;
161 struct work_struct respond_work;
162 struct u132_platform_data platform_data;
163 struct resource resources[0];
164 struct platform_device platform_dev;
165 unsigned char *bulk_in_buffer;
169 __u8 bulk_in_endpointAddr;
170 __u8 bulk_out_endpointAddr;
173 u8 response[4 + 1024];
178 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
179 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
181 static struct usb_driver ftdi_elan_driver;
182 static void ftdi_elan_delete(struct kref *kref)
184 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
185 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
186 usb_put_dev(ftdi->udev);
187 ftdi->disconnected += 1;
188 down(&ftdi_module_lock);
189 list_del_init(&ftdi->ftdi_list);
191 up(&ftdi_module_lock);
192 kfree(ftdi->bulk_in_buffer);
193 ftdi->bulk_in_buffer = NULL;
196 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
198 kref_put(&ftdi->kref, ftdi_elan_delete);
201 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
203 kref_get(&ftdi->kref);
206 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
208 kref_init(&ftdi->kref);
211 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
214 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
216 } else if (queue_work(status_queue, &ftdi->status_work))
218 kref_put(&ftdi->kref, ftdi_elan_delete);
222 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
225 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
226 kref_get(&ftdi->kref);
227 } else if (queue_work(status_queue, &ftdi->status_work))
228 kref_get(&ftdi->kref);
232 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
234 if (cancel_delayed_work(&ftdi->status_work))
235 kref_put(&ftdi->kref, ftdi_elan_delete);
238 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
241 if (queue_delayed_work(command_queue, &ftdi->command_work,
244 } else if (queue_work(command_queue, &ftdi->command_work))
246 kref_put(&ftdi->kref, ftdi_elan_delete);
250 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
253 if (queue_delayed_work(command_queue, &ftdi->command_work,
255 kref_get(&ftdi->kref);
256 } else if (queue_work(command_queue, &ftdi->command_work))
257 kref_get(&ftdi->kref);
261 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
263 if (cancel_delayed_work(&ftdi->command_work))
264 kref_put(&ftdi->kref, ftdi_elan_delete);
267 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
271 if (queue_delayed_work(respond_queue, &ftdi->respond_work,
274 } else if (queue_work(respond_queue, &ftdi->respond_work))
276 kref_put(&ftdi->kref, ftdi_elan_delete);
280 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
283 if (queue_delayed_work(respond_queue, &ftdi->respond_work,
285 kref_get(&ftdi->kref);
286 } else if (queue_work(respond_queue, &ftdi->respond_work))
287 kref_get(&ftdi->kref);
291 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
293 if (cancel_delayed_work(&ftdi->respond_work))
294 kref_put(&ftdi->kref, ftdi_elan_delete);
297 void ftdi_elan_gone_away(struct platform_device *pdev)
299 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
300 ftdi->gone_away += 1;
301 ftdi_elan_put_kref(ftdi);
305 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
306 void ftdi_release_platform_dev(struct device *dev)
311 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
312 struct u132_target *target, u8 *buffer, int length);
313 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
314 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
315 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
316 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
317 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
318 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
319 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
320 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
321 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
322 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
325 if (ftdi->platform_dev.dev.parent)
327 ftdi_elan_get_kref(ftdi);
328 ftdi->platform_data.potpg = 100;
329 ftdi->platform_data.reset = NULL;
330 ftdi->platform_dev.id = ftdi->sequence_num;
331 ftdi->platform_dev.resource = ftdi->resources;
332 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
333 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
334 ftdi->platform_dev.dev.parent = NULL;
335 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
336 ftdi->platform_dev.dev.dma_mask = NULL;
337 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
338 ftdi->platform_dev.name = ftdi->device_name;
339 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
340 request_module("u132_hcd");
341 dev_info(&ftdi->udev->dev, "registering '%s'\n",
342 ftdi->platform_dev.name);
343 result = platform_device_register(&ftdi->platform_dev);
347 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
349 down(&ftdi->u132_lock);
350 while (ftdi->respond_next > ftdi->respond_head) {
351 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
352 ftdi->respond_head++];
353 *respond->result = -ESHUTDOWN;
355 complete(&respond->wait_completion);
356 } up(&ftdi->u132_lock);
359 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
362 down(&ftdi->u132_lock);
363 while (ed_number-- > 0) {
364 struct u132_target *target = &ftdi->target[ed_number];
365 if (target->active == 1) {
366 target->condition_code = TD_DEVNOTRESP;
367 up(&ftdi->u132_lock);
368 ftdi_elan_do_callback(ftdi, target, NULL, 0);
369 down(&ftdi->u132_lock);
375 up(&ftdi->u132_lock);
378 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
381 down(&ftdi->u132_lock);
382 while (ed_number-- > 0) {
383 struct u132_target *target = &ftdi->target[ed_number];
384 target->abandoning = 1;
385 wait_1:if (target->active == 1) {
386 int command_size = ftdi->command_next -
388 if (command_size < COMMAND_SIZE) {
389 struct u132_command *command = &ftdi->command[
390 COMMAND_MASK & ftdi->command_next];
391 command->header = 0x80 | (ed_number << 5) | 0x4;
392 command->length = 0x00;
393 command->address = 0x00;
394 command->width = 0x00;
395 command->follows = 0;
397 command->buffer = &command->value;
398 ftdi->command_next += 1;
399 ftdi_elan_kick_command_queue(ftdi);
401 up(&ftdi->u132_lock);
403 down(&ftdi->u132_lock);
407 wait_2:if (target->active == 1) {
408 int command_size = ftdi->command_next -
410 if (command_size < COMMAND_SIZE) {
411 struct u132_command *command = &ftdi->command[
412 COMMAND_MASK & ftdi->command_next];
413 command->header = 0x90 | (ed_number << 5);
414 command->length = 0x00;
415 command->address = 0x00;
416 command->width = 0x00;
417 command->follows = 0;
419 command->buffer = &command->value;
420 ftdi->command_next += 1;
421 ftdi_elan_kick_command_queue(ftdi);
423 up(&ftdi->u132_lock);
425 down(&ftdi->u132_lock);
433 up(&ftdi->u132_lock);
436 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
439 down(&ftdi->u132_lock);
440 while (ed_number-- > 0) {
441 struct u132_target *target = &ftdi->target[ed_number];
442 target->abandoning = 1;
443 wait:if (target->active == 1) {
444 int command_size = ftdi->command_next -
446 if (command_size < COMMAND_SIZE) {
447 struct u132_command *command = &ftdi->command[
448 COMMAND_MASK & ftdi->command_next];
449 command->header = 0x80 | (ed_number << 5) | 0x4;
450 command->length = 0x00;
451 command->address = 0x00;
452 command->width = 0x00;
453 command->follows = 0;
455 command->buffer = &command->value;
456 ftdi->command_next += 1;
457 ftdi_elan_kick_command_queue(ftdi);
459 up(&ftdi->u132_lock);
461 down(&ftdi->u132_lock);
469 up(&ftdi->u132_lock);
472 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
474 ftdi_command_queue_work(ftdi, 0);
478 static void ftdi_elan_command_work(void *data)
480 struct usb_ftdi *ftdi = data;
481 if (ftdi->disconnected > 0) {
482 ftdi_elan_put_kref(ftdi);
485 int retval = ftdi_elan_command_engine(ftdi);
486 if (retval == -ESHUTDOWN) {
487 ftdi->disconnected += 1;
488 } else if (retval == -ENODEV) {
489 ftdi->disconnected += 1;
491 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
492 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
497 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
499 ftdi_respond_queue_work(ftdi, 0);
503 static void ftdi_elan_respond_work(void *data)
505 struct usb_ftdi *ftdi = data;
506 if (ftdi->disconnected > 0) {
507 ftdi_elan_put_kref(ftdi);
510 int retval = ftdi_elan_respond_engine(ftdi);
512 } else if (retval == -ESHUTDOWN) {
513 ftdi->disconnected += 1;
514 } else if (retval == -ENODEV) {
515 ftdi->disconnected += 1;
516 } else if (retval == -ENODEV) {
517 ftdi->disconnected += 1;
518 } else if (retval == -EILSEQ) {
519 ftdi->disconnected += 1;
521 ftdi->disconnected += 1;
522 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
524 if (ftdi->disconnected > 0) {
525 ftdi_elan_abandon_completions(ftdi);
526 ftdi_elan_abandon_targets(ftdi);
528 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
535 * the sw_lock is initially held and will be freed
536 * after the FTDI has been synchronized
539 static void ftdi_elan_status_work(void *data)
541 struct usb_ftdi *ftdi = data;
542 int work_delay_in_msec = 0;
543 if (ftdi->disconnected > 0) {
544 ftdi_elan_put_kref(ftdi);
546 } else if (ftdi->synchronized == 0) {
547 down(&ftdi->sw_lock);
548 if (ftdi_elan_synchronize(ftdi) == 0) {
549 ftdi->synchronized = 1;
550 ftdi_command_queue_work(ftdi, 1);
551 ftdi_respond_queue_work(ftdi, 1);
553 work_delay_in_msec = 100;
555 dev_err(&ftdi->udev->dev, "synchronize failed\n");
557 work_delay_in_msec = 10 *1000;
559 } else if (ftdi->stuck_status > 0) {
560 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
561 ftdi->stuck_status = 0;
562 ftdi->synchronized = 0;
563 } else if ((ftdi->stuck_status++ % 60) == 1) {
564 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
565 "- please remove\n");
567 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
568 "- checked %d times\n", ftdi->stuck_status);
569 work_delay_in_msec = 100;
570 } else if (ftdi->enumerated == 0) {
571 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
572 ftdi->enumerated = 1;
573 work_delay_in_msec = 250;
575 work_delay_in_msec = 1000;
576 } else if (ftdi->initialized == 0) {
577 if (ftdi_elan_setupOHCI(ftdi) == 0) {
578 ftdi->initialized = 1;
579 work_delay_in_msec = 500;
581 dev_err(&ftdi->udev->dev, "initialized failed - trying "
582 "again in 10 seconds\n");
583 work_delay_in_msec = 10 *1000;
585 } else if (ftdi->registered == 0) {
586 work_delay_in_msec = 10;
587 if (ftdi_elan_hcd_init(ftdi) == 0) {
588 ftdi->registered = 1;
590 dev_err(&ftdi->udev->dev, "register failed\n");
591 work_delay_in_msec = 250;
593 if (ftdi_elan_checkingPCI(ftdi) == 0) {
594 work_delay_in_msec = 250;
595 } else if (ftdi->controlreg & 0x00400000) {
596 if (ftdi->gone_away > 0) {
597 dev_err(&ftdi->udev->dev, "PCI device eject con"
598 "firmed platform_dev.dev.parent=%p plat"
600 ftdi->platform_dev.dev.parent,
601 &ftdi->platform_dev.dev);
602 platform_device_unregister(&ftdi->platform_dev);
603 ftdi->platform_dev.dev.parent = NULL;
604 ftdi->registered = 0;
605 ftdi->enumerated = 0;
606 ftdi->card_ejected = 0;
607 ftdi->initialized = 0;
610 ftdi_elan_flush_targets(ftdi);
611 work_delay_in_msec = 250;
613 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n"
615 ftdi_elan_cancel_targets(ftdi);
616 work_delay_in_msec = 500;
617 ftdi->enumerated = 0;
618 ftdi->initialized = 0;
621 if (ftdi->disconnected > 0) {
622 ftdi_elan_put_kref(ftdi);
625 ftdi_status_requeue_work(ftdi,
626 msecs_to_jiffies(work_delay_in_msec));
633 * file_operations for the jtag interface
635 * the usage count for the device is incremented on open()
636 * and decremented on release()
638 static int ftdi_elan_open(struct inode *inode, struct file *file)
640 int subminor = iminor(inode);
641 struct usb_interface *interface = usb_find_interface(&ftdi_elan_driver,
644 printk(KERN_ERR "can't find device for minor %d\n", subminor);
647 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
651 if (down_interruptible(&ftdi->sw_lock)) {
654 ftdi_elan_get_kref(ftdi);
655 file->private_data = ftdi;
662 static int ftdi_elan_release(struct inode *inode, struct file *file)
664 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
667 up(&ftdi->sw_lock); /* decrement the count on our device */
668 ftdi_elan_put_kref(ftdi);
673 #define FTDI_ELAN_IOC_MAGIC 0xA1
674 #define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
675 static int ftdi_elan_ioctl(struct inode *inode, struct file *file,
676 unsigned int cmd, unsigned long arg)
679 case FTDI_ELAN_IOCDEBUG:{
681 int size = strncpy_from_user(line,
682 (const char __user *)arg, sizeof(line));
686 printk(KERN_ERR "TODO: ioctl %s\n", line);
698 * blocking bulk reads are used to get data from the device
701 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
702 size_t count, loff_t *ppos)
704 char data[30 *3 + 4];
706 int m = (sizeof(data) - 1) / 3;
708 int retry_on_empty = 10;
709 int retry_on_timeout = 5;
710 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
711 if (ftdi->disconnected > 0) {
715 have:if (ftdi->bulk_in_left > 0) {
717 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
718 ftdi->bulk_in_left -= 1;
719 if (bytes_read < m) {
720 d += sprintf(d, " %02X", 0x000000FF & *p);
721 } else if (bytes_read > m) {
723 d += sprintf(d, " ..");
724 if (copy_to_user(buffer++, p, 1)) {
733 more:if (count > 0) {
734 int packet_bytes = 0;
735 int retval = usb_bulk_msg(ftdi->udev,
736 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
737 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
738 &packet_bytes, msecs_to_jiffies(50));
739 if (packet_bytes > 2) {
740 ftdi->bulk_in_left = packet_bytes - 2;
741 ftdi->bulk_in_last = 1;
743 } else if (retval == -ETIMEDOUT) {
744 if (retry_on_timeout-- > 0) {
746 } else if (bytes_read > 0) {
750 } else if (retval == 0) {
751 if (retry_on_empty-- > 0) {
761 static void ftdi_elan_write_bulk_callback(struct urb *urb)
763 struct usb_ftdi *ftdi = (struct usb_ftdi *)urb->context;
764 if (urb->status && !(urb->status == -ENOENT || urb->status ==
765 -ECONNRESET || urb->status == -ESHUTDOWN)) {
766 dev_err(&ftdi->udev->dev, "urb=%p write bulk status received: %"
767 "d\n", urb, urb->status);
769 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
770 urb->transfer_buffer, urb->transfer_dma);
773 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
774 char *buf, int command_size, int total_size)
778 int I = command_size;
779 int i = ftdi->command_head;
781 struct u132_command *command = &ftdi->command[COMMAND_MASK &
783 int F = command->follows;
784 u8 *f = command->buffer;
785 if (command->header & 0x80) {
786 ed_commands |= 1 << (0x3 & (command->header >> 5));
788 buf[b++] = command->header;
789 buf[b++] = (command->length >> 0) & 0x00FF;
790 buf[b++] = (command->length >> 8) & 0x00FF;
791 buf[b++] = command->address;
792 buf[b++] = command->width;
800 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
803 int I = command_size;
804 int i = ftdi->command_head;
806 struct u132_command *command = &ftdi->command[COMMAND_MASK &
808 total_size += 5 + command->follows;
812 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
819 int command_size = ftdi->command_next - ftdi->command_head;
820 if (command_size == 0)
822 total_size = ftdi_elan_total_command_size(ftdi, command_size);
823 urb = usb_alloc_urb(0, GFP_KERNEL);
825 dev_err(&ftdi->udev->dev, "could not get a urb to write %d comm"
826 "ands totaling %d bytes to the Uxxx\n", command_size,
830 buf = usb_buffer_alloc(ftdi->udev, total_size, GFP_KERNEL,
833 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d c"
834 "ommands totaling %d bytes to the Uxxx\n", command_size,
839 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
840 command_size, total_size);
841 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
842 ftdi->bulk_out_endpointAddr), buf, total_size,
843 ftdi_elan_write_bulk_callback, ftdi);
844 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
846 char diag[40 *3 + 4];
850 int s = (sizeof(diag) - 1) / 3;
852 while (s-- > 0 && m-- > 0) {
853 if (s > 0 || m == 0) {
854 d += sprintf(d, " %02X", *c++);
856 d += sprintf(d, " ..");
859 retval = usb_submit_urb(urb, GFP_KERNEL);
861 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write "
862 "%d commands totaling %d bytes to the Uxxx\n", retval,
863 urb, command_size, total_size);
864 usb_buffer_free(ftdi->udev, total_size, buf, urb->transfer_dma);
868 usb_free_urb(urb); /* release our reference to this urb,
869 the USB core will eventually free it entirely */
870 ftdi->command_head += command_size;
871 ftdi_elan_kick_respond_queue(ftdi);
875 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
876 struct u132_target *target, u8 *buffer, int length)
878 struct urb *urb = target->urb;
879 int halted = target->halted;
880 int skipped = target->skipped;
881 int actual = target->actual;
882 int non_null = target->non_null;
883 int toggle_bits = target->toggle_bits;
884 int error_count = target->error_count;
885 int condition_code = target->condition_code;
886 int repeat_number = target->repeat_number;
887 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
888 int, int, int, int) = target->callback;
890 target->callback = NULL;
891 (*callback) (target->endp, urb, buffer, length, toggle_bits,
892 error_count, condition_code, repeat_number, halted, skipped,
896 static char *have_ed_set_response(struct usb_ftdi *ftdi,
897 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
900 int payload = (ed_length >> 0) & 0x07FF;
901 down(&ftdi->u132_lock);
903 target->non_null = (ed_length >> 15) & 0x0001;
904 target->repeat_number = (ed_length >> 11) & 0x000F;
905 if (ed_type == 0x02) {
906 if (payload == 0 || target->abandoning > 0) {
907 target->abandoning = 0;
908 up(&ftdi->u132_lock);
909 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
914 return ftdi->response;
916 ftdi->expected = 4 + payload;
918 up(&ftdi->u132_lock);
921 } else if (ed_type == 0x03) {
922 if (payload == 0 || target->abandoning > 0) {
923 target->abandoning = 0;
924 up(&ftdi->u132_lock);
925 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
930 return ftdi->response;
932 ftdi->expected = 4 + payload;
934 up(&ftdi->u132_lock);
937 } else if (ed_type == 0x01) {
938 target->abandoning = 0;
939 up(&ftdi->u132_lock);
940 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
945 return ftdi->response;
947 target->abandoning = 0;
948 up(&ftdi->u132_lock);
949 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
954 return ftdi->response;
958 static char *have_ed_get_response(struct usb_ftdi *ftdi,
959 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
962 down(&ftdi->u132_lock);
963 target->condition_code = TD_DEVNOTRESP;
964 target->actual = (ed_length >> 0) & 0x01FF;
965 target->non_null = (ed_length >> 15) & 0x0001;
966 target->repeat_number = (ed_length >> 11) & 0x000F;
967 up(&ftdi->u132_lock);
969 ftdi_elan_do_callback(ftdi, target, NULL, 0);
970 target->abandoning = 0;
974 return ftdi->response;
979 * The engine tries to empty the FTDI fifo
981 * all responses found in the fifo data are dispatched thus
982 * the response buffer can only ever hold a maximum sized
983 * response from the Uxxx.
986 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
988 u8 *b = ftdi->response + ftdi->recieved;
990 int retry_on_empty = 1;
991 int retry_on_timeout = 3;
992 int empty_packets = 0;
994 int packet_bytes = 0;
995 int retval = usb_bulk_msg(ftdi->udev,
996 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
997 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
998 &packet_bytes, msecs_to_jiffies(500));
999 char diag[30 *3 + 4];
1001 int m = packet_bytes;
1002 u8 *c = ftdi->bulk_in_buffer;
1003 int s = (sizeof(diag) - 1) / 3;
1005 while (s-- > 0 && m-- > 0) {
1006 if (s > 0 || m == 0) {
1007 d += sprintf(d, " %02X", *c++);
1009 d += sprintf(d, " ..");
1011 if (packet_bytes > 2) {
1012 ftdi->bulk_in_left = packet_bytes - 2;
1013 ftdi->bulk_in_last = 1;
1015 } else if (retval == -ETIMEDOUT) {
1016 if (retry_on_timeout-- > 0) {
1017 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
1018 "t_bytes = %d with total %d bytes%s\n",
1019 packet_bytes, bytes_read, diag);
1021 } else if (bytes_read > 0) {
1022 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
1026 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
1027 "t_bytes = %d with total %d bytes%s\n",
1028 packet_bytes, bytes_read, diag);
1031 } else if (retval == -EILSEQ) {
1032 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1033 " = %d with total %d bytes%s\n", retval,
1034 packet_bytes, bytes_read, diag);
1036 } else if (retval) {
1037 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1038 " = %d with total %d bytes%s\n", retval,
1039 packet_bytes, bytes_read, diag);
1041 } else if (packet_bytes == 2) {
1042 unsigned char s0 = ftdi->bulk_in_buffer[0];
1043 unsigned char s1 = ftdi->bulk_in_buffer[1];
1045 if (s0 == 0x31 && s1 == 0x60) {
1046 if (retry_on_empty-- > 0) {
1050 } else if (s0 == 0x31 && s1 == 0x00) {
1051 if (retry_on_empty-- > 0) {
1056 if (retry_on_empty-- > 0) {
1061 } else if (packet_bytes == 1) {
1062 if (retry_on_empty-- > 0) {
1067 if (retry_on_empty-- > 0) {
1076 have:if (ftdi->bulk_in_left > 0) {
1077 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1079 ftdi->bulk_in_left -= 1;
1080 if (ftdi->recieved == 0 && c == 0xFF) {
1084 if (++ftdi->recieved < ftdi->expected) {
1086 } else if (ftdi->ed_found) {
1087 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1088 u16 ed_length = (ftdi->response[2] << 8) |
1090 struct u132_target *target = &ftdi->target[ed_number];
1091 int payload = (ed_length >> 0) & 0x07FF;
1092 char diag[30 *3 + 4];
1095 u8 *c = 4 + ftdi->response;
1096 int s = (sizeof(diag) - 1) / 3;
1098 while (s-- > 0 && m-- > 0) {
1099 if (s > 0 || m == 0) {
1100 d += sprintf(d, " %02X", *c++);
1102 d += sprintf(d, " ..");
1104 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1111 } else if (ftdi->expected == 8) {
1113 int respond_head = ftdi->respond_head++;
1114 struct u132_respond *respond = &ftdi->respond[
1115 RESPOND_MASK & respond_head];
1116 u32 data = ftdi->response[7];
1118 data |= ftdi->response[6];
1120 data |= ftdi->response[5];
1122 data |= ftdi->response[4];
1123 *respond->value = data;
1124 *respond->result = 0;
1125 complete(&respond->wait_completion);
1130 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1131 if (buscmd == 0x00) {
1132 } else if (buscmd == 0x02) {
1133 } else if (buscmd == 0x06) {
1134 } else if (buscmd == 0x0A) {
1136 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) va"
1137 "lue = %08X\n", buscmd, data);
1140 if ((ftdi->response[0] & 0x80) == 0x00) {
1144 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1145 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1146 u16 ed_length = (ftdi->response[2] << 8) |
1148 struct u132_target *target = &ftdi->target[
1150 target->halted = (ftdi->response[0] >> 3) &
1152 target->skipped = (ftdi->response[0] >> 2) &
1154 target->toggle_bits = (ftdi->response[3] >> 6)
1156 target->error_count = (ftdi->response[3] >> 4)
1158 target->condition_code = (ftdi->response[
1160 if ((ftdi->response[0] & 0x10) == 0x00) {
1161 b = have_ed_set_response(ftdi, target,
1162 ed_length, ed_number, ed_type,
1166 b = have_ed_get_response(ftdi, target,
1167 ed_length, ed_number, ed_type,
1179 * create a urb, and a buffer for it, and copy the data to the urb
1182 static ssize_t ftdi_elan_write(struct file *file,
1183 const char __user *user_buffer, size_t count,
1189 char data[30 *3 + 4];
1191 const char __user *s = user_buffer;
1192 int m = (sizeof(data) - 1) / 3;
1193 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
1194 if (ftdi->disconnected > 0) {
1200 urb = usb_alloc_urb(0, GFP_KERNEL);
1205 buf = usb_buffer_alloc(ftdi->udev, count, GFP_KERNEL,
1206 &urb->transfer_dma);
1211 if (copy_from_user(buf, user_buffer, count)) {
1215 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1216 ftdi->bulk_out_endpointAddr), buf, count,
1217 ftdi_elan_write_bulk_callback, ftdi);
1218 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1219 retval = usb_submit_urb(urb, GFP_KERNEL);
1221 dev_err(&ftdi->udev->dev, "failed submitting write urb, error %"
1230 d += sprintf(d, " %02X", 0x000000FF & *s++);
1232 d += sprintf(d, " ..");
1236 d += sprintf(d, " %02X", 0x000000FF & *s++);
1240 error_4: error_3:usb_buffer_free(ftdi->udev, count, buf,
1242 error_2:usb_free_urb(urb);
1243 error_1:return retval;
1246 static struct file_operations ftdi_elan_fops = {
1247 .owner = THIS_MODULE,
1248 .llseek = no_llseek,
1249 .ioctl = ftdi_elan_ioctl,
1250 .read = ftdi_elan_read,
1251 .write = ftdi_elan_write,
1252 .open = ftdi_elan_open,
1253 .release = ftdi_elan_release,
1257 * usb class driver info in order to get a minor number from the usb core,
1258 * and to have the device registered with the driver core
1260 static struct usb_class_driver ftdi_elan_jtag_class = {
1261 .name = "ftdi-%d-jtag",
1262 .fops = &ftdi_elan_fops,
1263 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1267 * the following definitions are for the
1268 * ELAN FPGA state machgine processor that
1269 * lies on the other side of the FTDI chip
1271 #define cPCIu132rd 0x0
1272 #define cPCIu132wr 0x1
1273 #define cPCIiord 0x2
1274 #define cPCIiowr 0x3
1275 #define cPCImemrd 0x6
1276 #define cPCImemwr 0x7
1277 #define cPCIcfgrd 0xA
1278 #define cPCIcfgwr 0xB
1279 #define cPCInull 0xF
1280 #define cU132cmd_status 0x0
1281 #define cU132flash 0x1
1282 #define cPIDsetup 0x0
1285 #define cPIDinonce 0x3
1286 #define cCCnoerror 0x0
1288 #define cCCbitstuff 0x2
1289 #define cCCtoggle 0x3
1290 #define cCCstall 0x4
1291 #define cCCnoresp 0x5
1292 #define cCCbadpid1 0x6
1293 #define cCCbadpid2 0x7
1294 #define cCCdataoverrun 0x8
1295 #define cCCdataunderrun 0x9
1296 #define cCCbuffoverrun 0xC
1297 #define cCCbuffunderrun 0xD
1298 #define cCCnotaccessed 0xF
1299 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1301 wait:if (ftdi->disconnected > 0) {
1305 down(&ftdi->u132_lock);
1306 command_size = ftdi->command_next - ftdi->command_head;
1307 if (command_size < COMMAND_SIZE) {
1308 struct u132_command *command = &ftdi->command[
1309 COMMAND_MASK & ftdi->command_next];
1310 command->header = 0x00 | cPCIu132wr;
1311 command->length = 0x04;
1312 command->address = 0x00;
1313 command->width = 0x00;
1314 command->follows = 4;
1315 command->value = data;
1316 command->buffer = &command->value;
1317 ftdi->command_next += 1;
1318 ftdi_elan_kick_command_queue(ftdi);
1319 up(&ftdi->u132_lock);
1322 up(&ftdi->u132_lock);
1329 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1332 u8 addressofs = config_offset / 4;
1333 wait:if (ftdi->disconnected > 0) {
1337 down(&ftdi->u132_lock);
1338 command_size = ftdi->command_next - ftdi->command_head;
1339 if (command_size < COMMAND_SIZE) {
1340 struct u132_command *command = &ftdi->command[
1341 COMMAND_MASK & ftdi->command_next];
1342 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1343 command->length = 0x04;
1344 command->address = addressofs;
1345 command->width = 0x00 | (width & 0x0F);
1346 command->follows = 4;
1347 command->value = data;
1348 command->buffer = &command->value;
1349 ftdi->command_next += 1;
1350 ftdi_elan_kick_command_queue(ftdi);
1351 up(&ftdi->u132_lock);
1354 up(&ftdi->u132_lock);
1361 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1364 u8 addressofs = mem_offset / 4;
1365 wait:if (ftdi->disconnected > 0) {
1369 down(&ftdi->u132_lock);
1370 command_size = ftdi->command_next - ftdi->command_head;
1371 if (command_size < COMMAND_SIZE) {
1372 struct u132_command *command = &ftdi->command[
1373 COMMAND_MASK & ftdi->command_next];
1374 command->header = 0x00 | (cPCImemwr & 0x0F);
1375 command->length = 0x04;
1376 command->address = addressofs;
1377 command->width = 0x00 | (width & 0x0F);
1378 command->follows = 4;
1379 command->value = data;
1380 command->buffer = &command->value;
1381 ftdi->command_next += 1;
1382 ftdi_elan_kick_command_queue(ftdi);
1383 up(&ftdi->u132_lock);
1386 up(&ftdi->u132_lock);
1393 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1396 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1397 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1401 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1402 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1404 wait:if (ftdi->disconnected > 0) {
1409 down(&ftdi->u132_lock);
1410 command_size = ftdi->command_next - ftdi->command_head;
1411 respond_size = ftdi->respond_next - ftdi->respond_head;
1412 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1414 struct u132_command *command = &ftdi->command[
1415 COMMAND_MASK & ftdi->command_next];
1416 struct u132_respond *respond = &ftdi->respond[
1417 RESPOND_MASK & ftdi->respond_next];
1418 int result = -ENODEV;
1419 respond->result = &result;
1420 respond->header = command->header = 0x00 | cPCIu132rd;
1421 command->length = 0x04;
1422 respond->address = command->address = cU132cmd_status;
1423 command->width = 0x00;
1424 command->follows = 0;
1426 command->buffer = NULL;
1427 respond->value = data;
1428 init_completion(&respond->wait_completion);
1429 ftdi->command_next += 1;
1430 ftdi->respond_next += 1;
1431 ftdi_elan_kick_command_queue(ftdi);
1432 up(&ftdi->u132_lock);
1433 wait_for_completion(&respond->wait_completion);
1436 up(&ftdi->u132_lock);
1443 int usb_ftdi_elan_read_reg(struct platform_device *pdev, u32 *data)
1445 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1446 return ftdi_elan_read_reg(ftdi, data);
1450 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_reg);
1451 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1452 u8 width, u32 *data)
1454 u8 addressofs = config_offset / 4;
1455 wait:if (ftdi->disconnected > 0) {
1460 down(&ftdi->u132_lock);
1461 command_size = ftdi->command_next - ftdi->command_head;
1462 respond_size = ftdi->respond_next - ftdi->respond_head;
1463 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1465 struct u132_command *command = &ftdi->command[
1466 COMMAND_MASK & ftdi->command_next];
1467 struct u132_respond *respond = &ftdi->respond[
1468 RESPOND_MASK & ftdi->respond_next];
1469 int result = -ENODEV;
1470 respond->result = &result;
1471 respond->header = command->header = 0x00 | (cPCIcfgrd &
1473 command->length = 0x04;
1474 respond->address = command->address = addressofs;
1475 command->width = 0x00 | (width & 0x0F);
1476 command->follows = 0;
1478 command->buffer = NULL;
1479 respond->value = data;
1480 init_completion(&respond->wait_completion);
1481 ftdi->command_next += 1;
1482 ftdi->respond_next += 1;
1483 ftdi_elan_kick_command_queue(ftdi);
1484 up(&ftdi->u132_lock);
1485 wait_for_completion(&respond->wait_completion);
1488 up(&ftdi->u132_lock);
1495 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1496 u8 width, u32 *data)
1498 u8 addressofs = mem_offset / 4;
1499 wait:if (ftdi->disconnected > 0) {
1504 down(&ftdi->u132_lock);
1505 command_size = ftdi->command_next - ftdi->command_head;
1506 respond_size = ftdi->respond_next - ftdi->respond_head;
1507 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1509 struct u132_command *command = &ftdi->command[
1510 COMMAND_MASK & ftdi->command_next];
1511 struct u132_respond *respond = &ftdi->respond[
1512 RESPOND_MASK & ftdi->respond_next];
1513 int result = -ENODEV;
1514 respond->result = &result;
1515 respond->header = command->header = 0x00 | (cPCImemrd &
1517 command->length = 0x04;
1518 respond->address = command->address = addressofs;
1519 command->width = 0x00 | (width & 0x0F);
1520 command->follows = 0;
1522 command->buffer = NULL;
1523 respond->value = data;
1524 init_completion(&respond->wait_completion);
1525 ftdi->command_next += 1;
1526 ftdi->respond_next += 1;
1527 ftdi_elan_kick_command_queue(ftdi);
1528 up(&ftdi->u132_lock);
1529 wait_for_completion(&respond->wait_completion);
1532 up(&ftdi->u132_lock);
1539 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1540 u8 width, u32 *data)
1542 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1543 if (ftdi->initialized == 0) {
1546 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1550 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1551 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1552 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1553 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1554 int toggle_bits, int error_count, int condition_code, int repeat_number,
1555 int halted, int skipped, int actual, int non_null))
1557 u8 ed = ed_number - 1;
1558 wait:if (ftdi->disconnected > 0) {
1560 } else if (ftdi->initialized == 0) {
1564 down(&ftdi->u132_lock);
1565 command_size = ftdi->command_next - ftdi->command_head;
1566 if (command_size < COMMAND_SIZE) {
1567 struct u132_target *target = &ftdi->target[ed];
1568 struct u132_command *command = &ftdi->command[
1569 COMMAND_MASK & ftdi->command_next];
1570 command->header = 0x80 | (ed << 5);
1571 command->length = 0x8007;
1572 command->address = (toggle_bits << 6) | (ep_number << 2)
1574 command->width = usb_maxpacket(urb->dev, urb->pipe,
1575 usb_pipeout(urb->pipe));
1576 command->follows = 8;
1578 command->buffer = urb->setup_packet;
1579 target->callback = callback;
1580 target->endp = endp;
1583 ftdi->command_next += 1;
1584 ftdi_elan_kick_command_queue(ftdi);
1585 up(&ftdi->u132_lock);
1588 up(&ftdi->u132_lock);
1595 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1596 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1597 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1598 int toggle_bits, int error_count, int condition_code, int repeat_number,
1599 int halted, int skipped, int actual, int non_null))
1601 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1602 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1603 ep_number, toggle_bits, callback);
1607 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1608 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1609 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1610 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1611 int toggle_bits, int error_count, int condition_code, int repeat_number,
1612 int halted, int skipped, int actual, int non_null))
1614 u8 ed = ed_number - 1;
1615 wait:if (ftdi->disconnected > 0) {
1617 } else if (ftdi->initialized == 0) {
1621 down(&ftdi->u132_lock);
1622 command_size = ftdi->command_next - ftdi->command_head;
1623 if (command_size < COMMAND_SIZE) {
1624 struct u132_target *target = &ftdi->target[ed];
1625 struct u132_command *command = &ftdi->command[
1626 COMMAND_MASK & ftdi->command_next];
1627 int remaining_length = urb->transfer_buffer_length -
1629 command->header = 0x82 | (ed << 5);
1630 if (remaining_length == 0) {
1631 command->length = 0x0000;
1632 } else if (remaining_length > 1024) {
1633 command->length = 0x8000 | 1023;
1635 command->length = 0x8000 | (remaining_length -
1637 command->address = (toggle_bits << 6) | (ep_number << 2)
1639 command->width = usb_maxpacket(urb->dev, urb->pipe,
1640 usb_pipeout(urb->pipe));
1641 command->follows = 0;
1643 command->buffer = NULL;
1644 target->callback = callback;
1645 target->endp = endp;
1648 ftdi->command_next += 1;
1649 ftdi_elan_kick_command_queue(ftdi);
1650 up(&ftdi->u132_lock);
1653 up(&ftdi->u132_lock);
1660 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1661 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1662 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1663 int toggle_bits, int error_count, int condition_code, int repeat_number,
1664 int halted, int skipped, int actual, int non_null))
1666 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1667 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1668 ep_number, toggle_bits, callback);
1672 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1673 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1674 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1675 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1676 int toggle_bits, int error_count, int condition_code, int repeat_number,
1677 int halted, int skipped, int actual, int non_null))
1679 u8 ed = ed_number - 1;
1680 wait:if (ftdi->disconnected > 0) {
1682 } else if (ftdi->initialized == 0) {
1686 down(&ftdi->u132_lock);
1687 command_size = ftdi->command_next - ftdi->command_head;
1688 if (command_size < COMMAND_SIZE) {
1689 struct u132_target *target = &ftdi->target[ed];
1690 struct u132_command *command = &ftdi->command[
1691 COMMAND_MASK & ftdi->command_next];
1692 command->header = 0x81 | (ed << 5);
1693 command->length = 0x0000;
1694 command->address = (toggle_bits << 6) | (ep_number << 2)
1696 command->width = usb_maxpacket(urb->dev, urb->pipe,
1697 usb_pipeout(urb->pipe));
1698 command->follows = 0;
1700 command->buffer = NULL;
1701 target->callback = callback;
1702 target->endp = endp;
1705 ftdi->command_next += 1;
1706 ftdi_elan_kick_command_queue(ftdi);
1707 up(&ftdi->u132_lock);
1710 up(&ftdi->u132_lock);
1717 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1718 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1719 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1720 int toggle_bits, int error_count, int condition_code, int repeat_number,
1721 int halted, int skipped, int actual, int non_null))
1723 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1724 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1725 ep_number, toggle_bits, callback);
1729 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1730 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1731 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1732 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1733 int toggle_bits, int error_count, int condition_code, int repeat_number,
1734 int halted, int skipped, int actual, int non_null))
1736 u8 ed = ed_number - 1;
1737 wait:if (ftdi->disconnected > 0) {
1739 } else if (ftdi->initialized == 0) {
1743 down(&ftdi->u132_lock);
1744 command_size = ftdi->command_next - ftdi->command_head;
1745 if (command_size < COMMAND_SIZE) {
1749 char data[30 *3 + 4];
1751 int m = (sizeof(data) - 1) / 3;
1753 struct u132_target *target = &ftdi->target[ed];
1754 struct u132_command *command = &ftdi->command[
1755 COMMAND_MASK & ftdi->command_next];
1756 command->header = 0x81 | (ed << 5);
1757 command->address = (toggle_bits << 6) | (ep_number << 2)
1759 command->width = usb_maxpacket(urb->dev, urb->pipe,
1760 usb_pipeout(urb->pipe));
1761 command->follows = min(1024,
1762 urb->transfer_buffer_length -
1763 urb->actual_length);
1765 command->buffer = urb->transfer_buffer +
1767 command->length = 0x8000 | (command->follows - 1);
1768 b = command->buffer;
1769 urb_size = command->follows;
1771 while (urb_size-- > 0) {
1773 } else if (i++ < m) {
1774 int w = sprintf(d, " %02X", *b++);
1778 d += sprintf(d, " ..");
1780 target->callback = callback;
1781 target->endp = endp;
1784 ftdi->command_next += 1;
1785 ftdi_elan_kick_command_queue(ftdi);
1786 up(&ftdi->u132_lock);
1789 up(&ftdi->u132_lock);
1796 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1797 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1798 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1799 int toggle_bits, int error_count, int condition_code, int repeat_number,
1800 int halted, int skipped, int actual, int non_null))
1802 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1803 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1804 ep_number, toggle_bits, callback);
1808 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1809 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1810 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1811 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1812 int toggle_bits, int error_count, int condition_code, int repeat_number,
1813 int halted, int skipped, int actual, int non_null))
1815 u8 ed = ed_number - 1;
1816 wait:if (ftdi->disconnected > 0) {
1818 } else if (ftdi->initialized == 0) {
1822 down(&ftdi->u132_lock);
1823 command_size = ftdi->command_next - ftdi->command_head;
1824 if (command_size < COMMAND_SIZE) {
1825 int remaining_length = urb->transfer_buffer_length -
1827 struct u132_target *target = &ftdi->target[ed];
1828 struct u132_command *command = &ftdi->command[
1829 COMMAND_MASK & ftdi->command_next];
1830 command->header = 0x83 | (ed << 5);
1831 if (remaining_length == 0) {
1832 command->length = 0x0000;
1833 } else if (remaining_length > 1024) {
1834 command->length = 0x8000 | 1023;
1836 command->length = 0x8000 | (remaining_length -
1838 command->address = (toggle_bits << 6) | (ep_number << 2)
1840 command->width = usb_maxpacket(urb->dev, urb->pipe,
1841 usb_pipeout(urb->pipe));
1842 command->follows = 0;
1844 command->buffer = NULL;
1845 target->callback = callback;
1846 target->endp = endp;
1849 ftdi->command_next += 1;
1850 ftdi_elan_kick_command_queue(ftdi);
1851 up(&ftdi->u132_lock);
1854 up(&ftdi->u132_lock);
1861 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1862 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1863 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1864 int toggle_bits, int error_count, int condition_code, int repeat_number,
1865 int halted, int skipped, int actual, int non_null))
1867 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1868 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1869 ep_number, toggle_bits, callback);
1873 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1874 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1877 u8 ed = ed_number - 1;
1878 if (ftdi->disconnected > 0) {
1880 } else if (ftdi->initialized == 0) {
1883 struct u132_target *target = &ftdi->target[ed];
1884 down(&ftdi->u132_lock);
1885 if (target->abandoning > 0) {
1886 up(&ftdi->u132_lock);
1889 target->abandoning = 1;
1890 wait_1:if (target->active == 1) {
1891 int command_size = ftdi->command_next -
1893 if (command_size < COMMAND_SIZE) {
1894 struct u132_command *command =
1895 &ftdi->command[COMMAND_MASK &
1896 ftdi->command_next];
1897 command->header = 0x80 | (ed << 5) |
1899 command->length = 0x00;
1900 command->address = 0x00;
1901 command->width = 0x00;
1902 command->follows = 0;
1904 command->buffer = &command->value;
1905 ftdi->command_next += 1;
1906 ftdi_elan_kick_command_queue(ftdi);
1908 up(&ftdi->u132_lock);
1910 down(&ftdi->u132_lock);
1914 up(&ftdi->u132_lock);
1920 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1923 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1924 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1928 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1929 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1931 int retry_on_empty = 10;
1932 int retry_on_timeout = 5;
1933 int retry_on_status = 20;
1935 int packet_bytes = 0;
1936 int retval = usb_bulk_msg(ftdi->udev,
1937 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1938 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1939 &packet_bytes, msecs_to_jiffies(100));
1940 if (packet_bytes > 2) {
1941 char diag[30 *3 + 4];
1943 int m = (sizeof(diag) - 1) / 3;
1944 char *b = ftdi->bulk_in_buffer;
1947 while (packet_bytes-- > 0) {
1949 if (bytes_read < m) {
1950 d += sprintf(d, " %02X",
1952 } else if (bytes_read > m) {
1954 d += sprintf(d, " ..");
1959 } else if (packet_bytes > 1) {
1960 char s1 = ftdi->bulk_in_buffer[0];
1961 char s2 = ftdi->bulk_in_buffer[1];
1962 if (s1 == 0x31 && s2 == 0x60) {
1964 } else if (retry_on_status-- > 0) {
1967 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1971 } else if (packet_bytes > 0) {
1972 char b1 = ftdi->bulk_in_buffer[0];
1973 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
1974 "TDI = %02X\n", b1);
1975 if (retry_on_status-- > 0) {
1978 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1982 } else if (retval == -ETIMEDOUT) {
1983 if (retry_on_timeout-- > 0) {
1986 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
1990 } else if (retval == 0) {
1991 if (retry_on_empty-- > 0) {
1994 dev_err(&ftdi->udev->dev, "empty packet retry l"
1999 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2008 * send the long flush sequence
2011 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
2018 urb = usb_alloc_urb(0, GFP_KERNEL);
2020 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
2024 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2026 dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
2033 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2034 ftdi->bulk_out_endpointAddr), buf, i,
2035 ftdi_elan_write_bulk_callback, ftdi);
2036 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2037 retval = usb_submit_urb(urb, GFP_KERNEL);
2039 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2040 "flush sequence\n");
2041 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2051 * send the reset sequence
2054 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
2061 urb = usb_alloc_urb(0, GFP_KERNEL);
2063 dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
2067 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2069 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
2078 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2079 ftdi->bulk_out_endpointAddr), buf, i,
2080 ftdi_elan_write_bulk_callback, ftdi);
2081 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2082 retval = usb_submit_urb(urb, GFP_KERNEL);
2084 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2085 "reset sequence\n");
2086 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2094 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2098 int retry_on_timeout = 5;
2099 int retry_on_empty = 10;
2101 retval = ftdi_elan_flush_input_fifo(ftdi);
2104 ftdi->bulk_in_left = 0;
2105 ftdi->bulk_in_last = -1;
2106 while (long_stop-- > 0) {
2109 retval = ftdi_elan_synchronize_flush(ftdi);
2112 retval = ftdi_elan_flush_input_fifo(ftdi);
2115 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2121 int packet_bytes = 0;
2122 retval = usb_bulk_msg(ftdi->udev,
2123 usb_rcvbulkpipe(ftdi->udev,
2124 ftdi->bulk_in_endpointAddr),
2125 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2126 &packet_bytes, msecs_to_jiffies(500));
2127 if (packet_bytes > 2) {
2128 char diag[30 *3 + 4];
2130 int m = (sizeof(diag) - 1) / 3;
2131 char *b = ftdi->bulk_in_buffer;
2133 unsigned char c = 0;
2135 while (packet_bytes-- > 0) {
2137 if (bytes_read < m) {
2138 d += sprintf(d, " %02X", c);
2139 } else if (bytes_read > m) {
2141 d += sprintf(d, " ..");
2150 } else if (read_stop-- > 0) {
2153 dev_err(&ftdi->udev->dev, "retr"
2154 "y limit reached\n");
2158 } else if (packet_bytes > 1) {
2159 unsigned char s1 = ftdi->bulk_in_buffer[0];
2160 unsigned char s2 = ftdi->bulk_in_buffer[1];
2161 if (s1 == 0x31 && s2 == 0x00) {
2162 if (read_stuck-- > 0) {
2166 } else if (s1 == 0x31 && s2 == 0x60) {
2167 if (read_stop-- > 0) {
2170 dev_err(&ftdi->udev->dev, "retr"
2171 "y limit reached\n");
2175 if (read_stop-- > 0) {
2178 dev_err(&ftdi->udev->dev, "retr"
2179 "y limit reached\n");
2183 } else if (packet_bytes > 0) {
2184 if (read_stop-- > 0) {
2187 dev_err(&ftdi->udev->dev, "retry limit "
2191 } else if (retval == -ETIMEDOUT) {
2192 if (retry_on_timeout-- > 0) {
2195 dev_err(&ftdi->udev->dev, "TIMED OUT re"
2196 "try limit reached\n");
2199 } else if (retval == 0) {
2200 if (retry_on_empty-- > 0) {
2203 dev_err(&ftdi->udev->dev, "empty packet"
2204 " retry limit reached\n");
2209 dev_err(&ftdi->udev->dev, "error = %d\n",
2211 if (read_stop-- > 0) {
2214 dev_err(&ftdi->udev->dev, "retry limit "
2221 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2225 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2227 int retry_on_empty = 10;
2228 int retry_on_timeout = 5;
2229 int retry_on_status = 50;
2231 int packet_bytes = 0;
2232 int retval = usb_bulk_msg(ftdi->udev,
2233 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2234 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2235 &packet_bytes, msecs_to_jiffies(1000));
2236 if (packet_bytes > 2) {
2237 char diag[30 *3 + 4];
2239 int m = (sizeof(diag) - 1) / 3;
2240 char *b = ftdi->bulk_in_buffer;
2243 while (packet_bytes-- > 0) {
2245 if (bytes_read < m) {
2246 d += sprintf(d, " %02X",
2248 } else if (bytes_read > m) {
2250 d += sprintf(d, " ..");
2255 } else if (packet_bytes > 1) {
2256 char s1 = ftdi->bulk_in_buffer[0];
2257 char s2 = ftdi->bulk_in_buffer[1];
2258 if (s1 == 0x31 && s2 == 0x60) {
2260 } else if (retry_on_status-- > 0) {
2265 } else if (packet_bytes > 0) {
2266 char b1 = ftdi->bulk_in_buffer[0];
2267 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
2268 "TDI = %02X\n", b1);
2269 if (retry_on_status-- > 0) {
2273 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
2277 } else if (retval == -ETIMEDOUT) {
2278 if (retry_on_timeout-- > 0) {
2281 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
2285 } else if (retval == 0) {
2286 if (retry_on_empty-- > 0) {
2289 dev_err(&ftdi->udev->dev, "empty packet retry l"
2294 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2301 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2303 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2306 if (ftdi->controlreg & 0x00400000) {
2307 if (ftdi->card_ejected) {
2309 ftdi->card_ejected = 1;
2310 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
2311 "%08X\n", ftdi->controlreg);
2315 u8 fn = ftdi->function - 1;
2316 int activePCIfn = fn << 8;
2321 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2325 pciVID = pcidata & 0xFFFF;
2326 pciPID = (pcidata >> 16) & 0xFFFF;
2327 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2328 ftdi->platform_data.device) {
2331 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X devi"
2332 "ce=%04X pciPID=%04X\n",
2333 ftdi->platform_data.vendor, pciVID,
2334 ftdi->platform_data.device, pciPID);
2340 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2349 int activePCIfn = 0;
2352 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2355 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2359 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2362 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2365 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2368 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2371 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2375 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2378 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2381 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2384 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2387 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2391 for (fn = 0; (fn < 4) && (!foundOHCI); fn++) {
2392 activePCIfn = fn << 8;
2393 ftdi->function = fn + 1;
2394 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2398 pciVID = pcidata & 0xFFFF;
2399 pciPID = (pcidata >> 16) & 0xFFFF;
2400 if ((pciVID == 0x1045) && (pciPID == 0xc861)) {
2402 } else if ((pciVID == 0x1033) && (pciPID == 0x0035)) {
2404 } else if ((pciVID == 0x10b9) && (pciPID == 0x5237)) {
2406 } else if ((pciVID == 0x11c1) && (pciPID == 0x5802)) {
2408 } else if ((pciVID == 0x11AB) && (pciPID == 0x1FA6)) {
2411 if (foundOHCI == 0) {
2414 ftdi->platform_data.vendor = pciVID;
2415 ftdi->platform_data.device = pciPID;
2416 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2420 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2424 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2428 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2432 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2437 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2441 latence_timer &= 0xFFFF00FF;
2442 latence_timer |= 0x00001600;
2443 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2447 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2452 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2456 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2463 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2468 int reset_repeat = 0;
2470 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x01);
2474 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2479 if (reset_repeat++ > 100) {
2489 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x11000000);
2492 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2496 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf);
2499 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2503 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf2edf);
2506 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2510 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0xA0);
2513 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2518 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x04);
2521 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2525 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2529 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2533 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x00001200);
2536 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2540 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2544 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2548 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x28002edf);
2551 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2556 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10000);
2560 power_check:U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2563 if (!(pcidata & 1)) {
2569 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2573 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2577 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2580 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2584 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10);
2587 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2593 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2598 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2603 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2607 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2610 dump_regs:for (reg = 0; reg <= 0x54; reg += 4) {
2611 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2620 * we use only the first bulk-in and bulk-out endpoints
2622 static int ftdi_elan_probe(struct usb_interface *interface,
2623 const struct usb_device_id *id)
2625 struct usb_host_interface *iface_desc;
2626 struct usb_endpoint_descriptor *endpoint;
2629 int retval = -ENOMEM;
2630 struct usb_ftdi *ftdi = kmalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2632 printk(KERN_ERR "Out of memory\n");
2635 memset(ftdi, 0x00, sizeof(struct usb_ftdi));
2636 down(&ftdi_module_lock);
2637 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2638 ftdi->sequence_num = ++ftdi_instances;
2639 up(&ftdi_module_lock);
2640 ftdi_elan_init_kref(ftdi);
2641 init_MUTEX(&ftdi->sw_lock);
2642 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2643 ftdi->interface = interface;
2644 init_MUTEX(&ftdi->u132_lock);
2646 iface_desc = interface->cur_altsetting;
2647 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2648 endpoint = &iface_desc->endpoint[i].desc;
2649 if (!ftdi->bulk_in_endpointAddr &&
2650 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
2651 == USB_DIR_IN) && ((endpoint->bmAttributes &
2652 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK))
2654 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2655 ftdi->bulk_in_size = buffer_size;
2656 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2657 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2658 if (!ftdi->bulk_in_buffer) {
2659 dev_err(&ftdi->udev->dev, "Could not allocate b"
2665 if (!ftdi->bulk_out_endpointAddr &&
2666 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
2667 == USB_DIR_OUT) && ((endpoint->bmAttributes &
2668 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK))
2670 ftdi->bulk_out_endpointAddr =
2671 endpoint->bEndpointAddress;
2674 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2675 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk"
2676 "-out endpoints\n");
2680 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2681 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2682 ftdi->bulk_out_endpointAddr);
2683 usb_set_intfdata(interface, ftdi);
2684 if (iface_desc->desc.bInterfaceNumber == 0 &&
2685 ftdi->bulk_in_endpointAddr == 0x81 &&
2686 ftdi->bulk_out_endpointAddr == 0x02) {
2687 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2689 dev_err(&ftdi->udev->dev, "Not able to get a minor for "
2691 usb_set_intfdata(interface, NULL);
2695 ftdi->class = &ftdi_elan_jtag_class;
2696 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface "
2697 "%d now attached to ftdi%d\n", ftdi,
2698 iface_desc->desc.bInterfaceNumber,
2702 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2703 ftdi->bulk_in_endpointAddr == 0x83 &&
2704 ftdi->bulk_out_endpointAddr == 0x04) {
2706 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now a"
2707 "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
2708 INIT_WORK(&ftdi->status_work, ftdi_elan_status_work,
2710 INIT_WORK(&ftdi->command_work, ftdi_elan_command_work,
2712 INIT_WORK(&ftdi->respond_work, ftdi_elan_respond_work,
2714 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2717 dev_err(&ftdi->udev->dev,
2718 "Could not find ELAN's U132 device\n");
2723 ftdi_elan_put_kref(ftdi);
2728 static void ftdi_elan_disconnect(struct usb_interface *interface)
2730 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2731 ftdi->disconnected += 1;
2733 int minor = interface->minor;
2734 struct usb_class_driver *class = ftdi->class;
2735 usb_set_intfdata(interface, NULL);
2736 usb_deregister_dev(interface, class);
2737 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on min"
2738 "or %d now disconnected\n", minor);
2740 ftdi_status_cancel_work(ftdi);
2741 ftdi_command_cancel_work(ftdi);
2742 ftdi_response_cancel_work(ftdi);
2743 ftdi_elan_abandon_completions(ftdi);
2744 ftdi_elan_abandon_targets(ftdi);
2745 if (ftdi->registered) {
2746 platform_device_unregister(&ftdi->platform_dev);
2747 ftdi->synchronized = 0;
2748 ftdi->enumerated = 0;
2749 ftdi->registered = 0;
2751 flush_workqueue(status_queue);
2752 flush_workqueue(command_queue);
2753 flush_workqueue(respond_queue);
2754 ftdi->disconnected += 1;
2755 usb_set_intfdata(interface, NULL);
2756 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller inter"
2757 "face now disconnected\n");
2759 ftdi_elan_put_kref(ftdi);
2762 static struct usb_driver ftdi_elan_driver = {
2763 .name = "ftdi-elan",
2764 .probe = ftdi_elan_probe,
2765 .disconnect = ftdi_elan_disconnect,
2766 .id_table = ftdi_elan_table,
2768 static int __init ftdi_elan_init(void)
2771 printk(KERN_INFO "driver %s built at %s on %s\n", ftdi_elan_driver.name,
2772 __TIME__, __DATE__);
2773 init_MUTEX(&ftdi_module_lock);
2774 INIT_LIST_HEAD(&ftdi_static_list);
2775 status_queue = create_singlethread_workqueue("ftdi-status-control");
2776 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2777 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2778 result = usb_register(&ftdi_elan_driver);
2780 printk(KERN_ERR "usb_register failed. Error number %d\n",
2785 static void __exit ftdi_elan_exit(void)
2787 struct usb_ftdi *ftdi;
2788 struct usb_ftdi *temp;
2789 usb_deregister(&ftdi_elan_driver);
2790 printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2791 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2792 ftdi_status_cancel_work(ftdi);
2793 ftdi_command_cancel_work(ftdi);
2794 ftdi_response_cancel_work(ftdi);
2795 } flush_workqueue(status_queue);
2796 destroy_workqueue(status_queue);
2797 status_queue = NULL;
2798 flush_workqueue(command_queue);
2799 destroy_workqueue(command_queue);
2800 command_queue = NULL;
2801 flush_workqueue(respond_queue);
2802 destroy_workqueue(respond_queue);
2803 respond_queue = NULL;
2807 module_init(ftdi_elan_init);
2808 module_exit(ftdi_elan_exit);