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 delayed_work status_work;
160 struct delayed_work command_work;
161 struct delayed_work 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)
213 if (!queue_delayed_work(status_queue, &ftdi->status_work, delta))
214 kref_put(&ftdi->kref, ftdi_elan_delete);
217 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
219 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
220 kref_get(&ftdi->kref);
223 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
225 if (cancel_delayed_work(&ftdi->status_work))
226 kref_put(&ftdi->kref, ftdi_elan_delete);
229 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
231 if (!queue_delayed_work(command_queue, &ftdi->command_work, delta))
232 kref_put(&ftdi->kref, ftdi_elan_delete);
235 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
237 if (queue_delayed_work(command_queue, &ftdi->command_work, delta))
238 kref_get(&ftdi->kref);
241 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
243 if (cancel_delayed_work(&ftdi->command_work))
244 kref_put(&ftdi->kref, ftdi_elan_delete);
247 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
250 if (!queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
251 kref_put(&ftdi->kref, ftdi_elan_delete);
254 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
256 if (queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
257 kref_get(&ftdi->kref);
260 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
262 if (cancel_delayed_work(&ftdi->respond_work))
263 kref_put(&ftdi->kref, ftdi_elan_delete);
266 void ftdi_elan_gone_away(struct platform_device *pdev)
268 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
269 ftdi->gone_away += 1;
270 ftdi_elan_put_kref(ftdi);
274 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
275 void ftdi_release_platform_dev(struct device *dev)
280 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
281 struct u132_target *target, u8 *buffer, int length);
282 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
283 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
284 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
285 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
286 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
287 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
288 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
289 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
290 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
291 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
294 if (ftdi->platform_dev.dev.parent)
296 ftdi_elan_get_kref(ftdi);
297 ftdi->platform_data.potpg = 100;
298 ftdi->platform_data.reset = NULL;
299 ftdi->platform_dev.id = ftdi->sequence_num;
300 ftdi->platform_dev.resource = ftdi->resources;
301 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
302 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
303 ftdi->platform_dev.dev.parent = NULL;
304 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
305 ftdi->platform_dev.dev.dma_mask = NULL;
306 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
307 ftdi->platform_dev.name = ftdi->device_name;
308 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
309 request_module("u132_hcd");
310 dev_info(&ftdi->udev->dev, "registering '%s'\n",
311 ftdi->platform_dev.name);
312 result = platform_device_register(&ftdi->platform_dev);
316 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
318 down(&ftdi->u132_lock);
319 while (ftdi->respond_next > ftdi->respond_head) {
320 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
321 ftdi->respond_head++];
322 *respond->result = -ESHUTDOWN;
324 complete(&respond->wait_completion);
325 } up(&ftdi->u132_lock);
328 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
331 down(&ftdi->u132_lock);
332 while (ed_number-- > 0) {
333 struct u132_target *target = &ftdi->target[ed_number];
334 if (target->active == 1) {
335 target->condition_code = TD_DEVNOTRESP;
336 up(&ftdi->u132_lock);
337 ftdi_elan_do_callback(ftdi, target, NULL, 0);
338 down(&ftdi->u132_lock);
344 up(&ftdi->u132_lock);
347 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
350 down(&ftdi->u132_lock);
351 while (ed_number-- > 0) {
352 struct u132_target *target = &ftdi->target[ed_number];
353 target->abandoning = 1;
354 wait_1:if (target->active == 1) {
355 int command_size = ftdi->command_next -
357 if (command_size < COMMAND_SIZE) {
358 struct u132_command *command = &ftdi->command[
359 COMMAND_MASK & ftdi->command_next];
360 command->header = 0x80 | (ed_number << 5) | 0x4;
361 command->length = 0x00;
362 command->address = 0x00;
363 command->width = 0x00;
364 command->follows = 0;
366 command->buffer = &command->value;
367 ftdi->command_next += 1;
368 ftdi_elan_kick_command_queue(ftdi);
370 up(&ftdi->u132_lock);
372 down(&ftdi->u132_lock);
376 wait_2:if (target->active == 1) {
377 int command_size = ftdi->command_next -
379 if (command_size < COMMAND_SIZE) {
380 struct u132_command *command = &ftdi->command[
381 COMMAND_MASK & ftdi->command_next];
382 command->header = 0x90 | (ed_number << 5);
383 command->length = 0x00;
384 command->address = 0x00;
385 command->width = 0x00;
386 command->follows = 0;
388 command->buffer = &command->value;
389 ftdi->command_next += 1;
390 ftdi_elan_kick_command_queue(ftdi);
392 up(&ftdi->u132_lock);
394 down(&ftdi->u132_lock);
402 up(&ftdi->u132_lock);
405 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
408 down(&ftdi->u132_lock);
409 while (ed_number-- > 0) {
410 struct u132_target *target = &ftdi->target[ed_number];
411 target->abandoning = 1;
412 wait:if (target->active == 1) {
413 int command_size = ftdi->command_next -
415 if (command_size < COMMAND_SIZE) {
416 struct u132_command *command = &ftdi->command[
417 COMMAND_MASK & ftdi->command_next];
418 command->header = 0x80 | (ed_number << 5) | 0x4;
419 command->length = 0x00;
420 command->address = 0x00;
421 command->width = 0x00;
422 command->follows = 0;
424 command->buffer = &command->value;
425 ftdi->command_next += 1;
426 ftdi_elan_kick_command_queue(ftdi);
428 up(&ftdi->u132_lock);
430 down(&ftdi->u132_lock);
438 up(&ftdi->u132_lock);
441 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
443 ftdi_command_queue_work(ftdi, 0);
447 static void ftdi_elan_command_work(struct work_struct *work)
449 struct usb_ftdi *ftdi =
450 container_of(work, struct usb_ftdi, command_work.work);
452 if (ftdi->disconnected > 0) {
453 ftdi_elan_put_kref(ftdi);
456 int retval = ftdi_elan_command_engine(ftdi);
457 if (retval == -ESHUTDOWN) {
458 ftdi->disconnected += 1;
459 } else if (retval == -ENODEV) {
460 ftdi->disconnected += 1;
462 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
463 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
468 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
470 ftdi_respond_queue_work(ftdi, 0);
474 static void ftdi_elan_respond_work(struct work_struct *work)
476 struct usb_ftdi *ftdi =
477 container_of(work, struct usb_ftdi, respond_work.work);
478 if (ftdi->disconnected > 0) {
479 ftdi_elan_put_kref(ftdi);
482 int retval = ftdi_elan_respond_engine(ftdi);
484 } else if (retval == -ESHUTDOWN) {
485 ftdi->disconnected += 1;
486 } else if (retval == -ENODEV) {
487 ftdi->disconnected += 1;
488 } else if (retval == -EILSEQ) {
489 ftdi->disconnected += 1;
491 ftdi->disconnected += 1;
492 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
494 if (ftdi->disconnected > 0) {
495 ftdi_elan_abandon_completions(ftdi);
496 ftdi_elan_abandon_targets(ftdi);
498 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
505 * the sw_lock is initially held and will be freed
506 * after the FTDI has been synchronized
509 static void ftdi_elan_status_work(struct work_struct *work)
511 struct usb_ftdi *ftdi =
512 container_of(work, struct usb_ftdi, status_work.work);
513 int work_delay_in_msec = 0;
514 if (ftdi->disconnected > 0) {
515 ftdi_elan_put_kref(ftdi);
517 } else if (ftdi->synchronized == 0) {
518 down(&ftdi->sw_lock);
519 if (ftdi_elan_synchronize(ftdi) == 0) {
520 ftdi->synchronized = 1;
521 ftdi_command_queue_work(ftdi, 1);
522 ftdi_respond_queue_work(ftdi, 1);
524 work_delay_in_msec = 100;
526 dev_err(&ftdi->udev->dev, "synchronize failed\n");
528 work_delay_in_msec = 10 *1000;
530 } else if (ftdi->stuck_status > 0) {
531 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
532 ftdi->stuck_status = 0;
533 ftdi->synchronized = 0;
534 } else if ((ftdi->stuck_status++ % 60) == 1) {
535 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
536 "- please remove\n");
538 dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
539 "- checked %d times\n", ftdi->stuck_status);
540 work_delay_in_msec = 100;
541 } else if (ftdi->enumerated == 0) {
542 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
543 ftdi->enumerated = 1;
544 work_delay_in_msec = 250;
546 work_delay_in_msec = 1000;
547 } else if (ftdi->initialized == 0) {
548 if (ftdi_elan_setupOHCI(ftdi) == 0) {
549 ftdi->initialized = 1;
550 work_delay_in_msec = 500;
552 dev_err(&ftdi->udev->dev, "initialized failed - trying "
553 "again in 10 seconds\n");
554 work_delay_in_msec = 10 *1000;
556 } else if (ftdi->registered == 0) {
557 work_delay_in_msec = 10;
558 if (ftdi_elan_hcd_init(ftdi) == 0) {
559 ftdi->registered = 1;
561 dev_err(&ftdi->udev->dev, "register failed\n");
562 work_delay_in_msec = 250;
564 if (ftdi_elan_checkingPCI(ftdi) == 0) {
565 work_delay_in_msec = 250;
566 } else if (ftdi->controlreg & 0x00400000) {
567 if (ftdi->gone_away > 0) {
568 dev_err(&ftdi->udev->dev, "PCI device eject con"
569 "firmed platform_dev.dev.parent=%p plat"
571 ftdi->platform_dev.dev.parent,
572 &ftdi->platform_dev.dev);
573 platform_device_unregister(&ftdi->platform_dev);
574 ftdi->platform_dev.dev.parent = NULL;
575 ftdi->registered = 0;
576 ftdi->enumerated = 0;
577 ftdi->card_ejected = 0;
578 ftdi->initialized = 0;
581 ftdi_elan_flush_targets(ftdi);
582 work_delay_in_msec = 250;
584 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n"
586 ftdi_elan_cancel_targets(ftdi);
587 work_delay_in_msec = 500;
588 ftdi->enumerated = 0;
589 ftdi->initialized = 0;
592 if (ftdi->disconnected > 0) {
593 ftdi_elan_put_kref(ftdi);
596 ftdi_status_requeue_work(ftdi,
597 msecs_to_jiffies(work_delay_in_msec));
604 * file_operations for the jtag interface
606 * the usage count for the device is incremented on open()
607 * and decremented on release()
609 static int ftdi_elan_open(struct inode *inode, struct file *file)
611 int subminor = iminor(inode);
612 struct usb_interface *interface = usb_find_interface(&ftdi_elan_driver,
615 printk(KERN_ERR "can't find device for minor %d\n", subminor);
618 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
622 if (down_interruptible(&ftdi->sw_lock)) {
625 ftdi_elan_get_kref(ftdi);
626 file->private_data = ftdi;
633 static int ftdi_elan_release(struct inode *inode, struct file *file)
635 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
638 up(&ftdi->sw_lock); /* decrement the count on our device */
639 ftdi_elan_put_kref(ftdi);
644 #define FTDI_ELAN_IOC_MAGIC 0xA1
645 #define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
646 static int ftdi_elan_ioctl(struct inode *inode, struct file *file,
647 unsigned int cmd, unsigned long arg)
650 case FTDI_ELAN_IOCDEBUG:{
652 int size = strncpy_from_user(line,
653 (const char __user *)arg, sizeof(line));
657 printk(KERN_ERR "TODO: ioctl %s\n", line);
669 * blocking bulk reads are used to get data from the device
672 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
673 size_t count, loff_t *ppos)
675 char data[30 *3 + 4];
677 int m = (sizeof(data) - 1) / 3;
679 int retry_on_empty = 10;
680 int retry_on_timeout = 5;
681 struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
682 if (ftdi->disconnected > 0) {
686 have:if (ftdi->bulk_in_left > 0) {
688 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
689 ftdi->bulk_in_left -= 1;
690 if (bytes_read < m) {
691 d += sprintf(d, " %02X", 0x000000FF & *p);
692 } else if (bytes_read > m) {
694 d += sprintf(d, " ..");
695 if (copy_to_user(buffer++, p, 1)) {
704 more:if (count > 0) {
705 int packet_bytes = 0;
706 int retval = usb_bulk_msg(ftdi->udev,
707 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
708 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
709 &packet_bytes, msecs_to_jiffies(50));
710 if (packet_bytes > 2) {
711 ftdi->bulk_in_left = packet_bytes - 2;
712 ftdi->bulk_in_last = 1;
714 } else if (retval == -ETIMEDOUT) {
715 if (retry_on_timeout-- > 0) {
717 } else if (bytes_read > 0) {
721 } else if (retval == 0) {
722 if (retry_on_empty-- > 0) {
732 static void ftdi_elan_write_bulk_callback(struct urb *urb)
734 struct usb_ftdi *ftdi = (struct usb_ftdi *)urb->context;
735 if (urb->status && !(urb->status == -ENOENT || urb->status ==
736 -ECONNRESET || urb->status == -ESHUTDOWN)) {
737 dev_err(&ftdi->udev->dev, "urb=%p write bulk status received: %"
738 "d\n", urb, urb->status);
740 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
741 urb->transfer_buffer, urb->transfer_dma);
744 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
745 char *buf, int command_size, int total_size)
749 int I = command_size;
750 int i = ftdi->command_head;
752 struct u132_command *command = &ftdi->command[COMMAND_MASK &
754 int F = command->follows;
755 u8 *f = command->buffer;
756 if (command->header & 0x80) {
757 ed_commands |= 1 << (0x3 & (command->header >> 5));
759 buf[b++] = command->header;
760 buf[b++] = (command->length >> 0) & 0x00FF;
761 buf[b++] = (command->length >> 8) & 0x00FF;
762 buf[b++] = command->address;
763 buf[b++] = command->width;
771 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
774 int I = command_size;
775 int i = ftdi->command_head;
777 struct u132_command *command = &ftdi->command[COMMAND_MASK &
779 total_size += 5 + command->follows;
783 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
790 int command_size = ftdi->command_next - ftdi->command_head;
791 if (command_size == 0)
793 total_size = ftdi_elan_total_command_size(ftdi, command_size);
794 urb = usb_alloc_urb(0, GFP_KERNEL);
796 dev_err(&ftdi->udev->dev, "could not get a urb to write %d comm"
797 "ands totaling %d bytes to the Uxxx\n", command_size,
801 buf = usb_buffer_alloc(ftdi->udev, total_size, GFP_KERNEL,
804 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d c"
805 "ommands totaling %d bytes to the Uxxx\n", command_size,
810 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
811 command_size, total_size);
812 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
813 ftdi->bulk_out_endpointAddr), buf, total_size,
814 ftdi_elan_write_bulk_callback, ftdi);
815 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
817 char diag[40 *3 + 4];
821 int s = (sizeof(diag) - 1) / 3;
823 while (s-- > 0 && m-- > 0) {
824 if (s > 0 || m == 0) {
825 d += sprintf(d, " %02X", *c++);
827 d += sprintf(d, " ..");
830 retval = usb_submit_urb(urb, GFP_KERNEL);
832 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write "
833 "%d commands totaling %d bytes to the Uxxx\n", retval,
834 urb, command_size, total_size);
835 usb_buffer_free(ftdi->udev, total_size, buf, urb->transfer_dma);
839 usb_free_urb(urb); /* release our reference to this urb,
840 the USB core will eventually free it entirely */
841 ftdi->command_head += command_size;
842 ftdi_elan_kick_respond_queue(ftdi);
846 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
847 struct u132_target *target, u8 *buffer, int length)
849 struct urb *urb = target->urb;
850 int halted = target->halted;
851 int skipped = target->skipped;
852 int actual = target->actual;
853 int non_null = target->non_null;
854 int toggle_bits = target->toggle_bits;
855 int error_count = target->error_count;
856 int condition_code = target->condition_code;
857 int repeat_number = target->repeat_number;
858 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
859 int, int, int, int) = target->callback;
861 target->callback = NULL;
862 (*callback) (target->endp, urb, buffer, length, toggle_bits,
863 error_count, condition_code, repeat_number, halted, skipped,
867 static char *have_ed_set_response(struct usb_ftdi *ftdi,
868 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
871 int payload = (ed_length >> 0) & 0x07FF;
872 down(&ftdi->u132_lock);
874 target->non_null = (ed_length >> 15) & 0x0001;
875 target->repeat_number = (ed_length >> 11) & 0x000F;
876 if (ed_type == 0x02) {
877 if (payload == 0 || target->abandoning > 0) {
878 target->abandoning = 0;
879 up(&ftdi->u132_lock);
880 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
885 return ftdi->response;
887 ftdi->expected = 4 + payload;
889 up(&ftdi->u132_lock);
892 } else if (ed_type == 0x03) {
893 if (payload == 0 || target->abandoning > 0) {
894 target->abandoning = 0;
895 up(&ftdi->u132_lock);
896 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
901 return ftdi->response;
903 ftdi->expected = 4 + payload;
905 up(&ftdi->u132_lock);
908 } else if (ed_type == 0x01) {
909 target->abandoning = 0;
910 up(&ftdi->u132_lock);
911 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
916 return ftdi->response;
918 target->abandoning = 0;
919 up(&ftdi->u132_lock);
920 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
925 return ftdi->response;
929 static char *have_ed_get_response(struct usb_ftdi *ftdi,
930 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
933 down(&ftdi->u132_lock);
934 target->condition_code = TD_DEVNOTRESP;
935 target->actual = (ed_length >> 0) & 0x01FF;
936 target->non_null = (ed_length >> 15) & 0x0001;
937 target->repeat_number = (ed_length >> 11) & 0x000F;
938 up(&ftdi->u132_lock);
940 ftdi_elan_do_callback(ftdi, target, NULL, 0);
941 target->abandoning = 0;
945 return ftdi->response;
950 * The engine tries to empty the FTDI fifo
952 * all responses found in the fifo data are dispatched thus
953 * the response buffer can only ever hold a maximum sized
954 * response from the Uxxx.
957 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
959 u8 *b = ftdi->response + ftdi->recieved;
961 int retry_on_empty = 1;
962 int retry_on_timeout = 3;
963 int empty_packets = 0;
965 int packet_bytes = 0;
966 int retval = usb_bulk_msg(ftdi->udev,
967 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
968 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
969 &packet_bytes, msecs_to_jiffies(500));
970 char diag[30 *3 + 4];
972 int m = packet_bytes;
973 u8 *c = ftdi->bulk_in_buffer;
974 int s = (sizeof(diag) - 1) / 3;
976 while (s-- > 0 && m-- > 0) {
977 if (s > 0 || m == 0) {
978 d += sprintf(d, " %02X", *c++);
980 d += sprintf(d, " ..");
982 if (packet_bytes > 2) {
983 ftdi->bulk_in_left = packet_bytes - 2;
984 ftdi->bulk_in_last = 1;
986 } else if (retval == -ETIMEDOUT) {
987 if (retry_on_timeout-- > 0) {
988 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
989 "t_bytes = %d with total %d bytes%s\n",
990 packet_bytes, bytes_read, diag);
992 } else if (bytes_read > 0) {
993 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
997 dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
998 "t_bytes = %d with total %d bytes%s\n",
999 packet_bytes, bytes_read, diag);
1002 } else if (retval == -EILSEQ) {
1003 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1004 " = %d with total %d bytes%s\n", retval,
1005 packet_bytes, bytes_read, diag);
1007 } else if (retval) {
1008 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
1009 " = %d with total %d bytes%s\n", retval,
1010 packet_bytes, bytes_read, diag);
1012 } else if (packet_bytes == 2) {
1013 unsigned char s0 = ftdi->bulk_in_buffer[0];
1014 unsigned char s1 = ftdi->bulk_in_buffer[1];
1016 if (s0 == 0x31 && s1 == 0x60) {
1017 if (retry_on_empty-- > 0) {
1021 } else if (s0 == 0x31 && s1 == 0x00) {
1022 if (retry_on_empty-- > 0) {
1027 if (retry_on_empty-- > 0) {
1032 } else if (packet_bytes == 1) {
1033 if (retry_on_empty-- > 0) {
1038 if (retry_on_empty-- > 0) {
1047 have:if (ftdi->bulk_in_left > 0) {
1048 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1050 ftdi->bulk_in_left -= 1;
1051 if (ftdi->recieved == 0 && c == 0xFF) {
1055 if (++ftdi->recieved < ftdi->expected) {
1057 } else if (ftdi->ed_found) {
1058 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1059 u16 ed_length = (ftdi->response[2] << 8) |
1061 struct u132_target *target = &ftdi->target[ed_number];
1062 int payload = (ed_length >> 0) & 0x07FF;
1063 char diag[30 *3 + 4];
1066 u8 *c = 4 + ftdi->response;
1067 int s = (sizeof(diag) - 1) / 3;
1069 while (s-- > 0 && m-- > 0) {
1070 if (s > 0 || m == 0) {
1071 d += sprintf(d, " %02X", *c++);
1073 d += sprintf(d, " ..");
1075 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1082 } else if (ftdi->expected == 8) {
1084 int respond_head = ftdi->respond_head++;
1085 struct u132_respond *respond = &ftdi->respond[
1086 RESPOND_MASK & respond_head];
1087 u32 data = ftdi->response[7];
1089 data |= ftdi->response[6];
1091 data |= ftdi->response[5];
1093 data |= ftdi->response[4];
1094 *respond->value = data;
1095 *respond->result = 0;
1096 complete(&respond->wait_completion);
1101 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1102 if (buscmd == 0x00) {
1103 } else if (buscmd == 0x02) {
1104 } else if (buscmd == 0x06) {
1105 } else if (buscmd == 0x0A) {
1107 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) va"
1108 "lue = %08X\n", buscmd, data);
1111 if ((ftdi->response[0] & 0x80) == 0x00) {
1115 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1116 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1117 u16 ed_length = (ftdi->response[2] << 8) |
1119 struct u132_target *target = &ftdi->target[
1121 target->halted = (ftdi->response[0] >> 3) &
1123 target->skipped = (ftdi->response[0] >> 2) &
1125 target->toggle_bits = (ftdi->response[3] >> 6)
1127 target->error_count = (ftdi->response[3] >> 4)
1129 target->condition_code = (ftdi->response[
1131 if ((ftdi->response[0] & 0x10) == 0x00) {
1132 b = have_ed_set_response(ftdi, target,
1133 ed_length, ed_number, ed_type,
1137 b = have_ed_get_response(ftdi, target,
1138 ed_length, ed_number, ed_type,
1150 * create a urb, and a buffer for it, and copy the data to the urb
1153 static ssize_t ftdi_elan_write(struct file *file,
1154 const char __user *user_buffer, size_t count,
1160 struct usb_ftdi *ftdi = file->private_data;
1162 if (ftdi->disconnected > 0) {
1168 urb = usb_alloc_urb(0, GFP_KERNEL);
1173 buf = usb_buffer_alloc(ftdi->udev, count, GFP_KERNEL,
1174 &urb->transfer_dma);
1179 if (copy_from_user(buf, user_buffer, count)) {
1183 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1184 ftdi->bulk_out_endpointAddr), buf, count,
1185 ftdi_elan_write_bulk_callback, ftdi);
1186 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1187 retval = usb_submit_urb(urb, GFP_KERNEL);
1189 dev_err(&ftdi->udev->dev, "failed submitting write urb, error %"
1198 usb_buffer_free(ftdi->udev, count, buf, urb->transfer_dma);
1205 static struct file_operations ftdi_elan_fops = {
1206 .owner = THIS_MODULE,
1207 .llseek = no_llseek,
1208 .ioctl = ftdi_elan_ioctl,
1209 .read = ftdi_elan_read,
1210 .write = ftdi_elan_write,
1211 .open = ftdi_elan_open,
1212 .release = ftdi_elan_release,
1216 * usb class driver info in order to get a minor number from the usb core,
1217 * and to have the device registered with the driver core
1219 static struct usb_class_driver ftdi_elan_jtag_class = {
1220 .name = "ftdi-%d-jtag",
1221 .fops = &ftdi_elan_fops,
1222 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1226 * the following definitions are for the
1227 * ELAN FPGA state machgine processor that
1228 * lies on the other side of the FTDI chip
1230 #define cPCIu132rd 0x0
1231 #define cPCIu132wr 0x1
1232 #define cPCIiord 0x2
1233 #define cPCIiowr 0x3
1234 #define cPCImemrd 0x6
1235 #define cPCImemwr 0x7
1236 #define cPCIcfgrd 0xA
1237 #define cPCIcfgwr 0xB
1238 #define cPCInull 0xF
1239 #define cU132cmd_status 0x0
1240 #define cU132flash 0x1
1241 #define cPIDsetup 0x0
1244 #define cPIDinonce 0x3
1245 #define cCCnoerror 0x0
1247 #define cCCbitstuff 0x2
1248 #define cCCtoggle 0x3
1249 #define cCCstall 0x4
1250 #define cCCnoresp 0x5
1251 #define cCCbadpid1 0x6
1252 #define cCCbadpid2 0x7
1253 #define cCCdataoverrun 0x8
1254 #define cCCdataunderrun 0x9
1255 #define cCCbuffoverrun 0xC
1256 #define cCCbuffunderrun 0xD
1257 #define cCCnotaccessed 0xF
1258 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1260 wait:if (ftdi->disconnected > 0) {
1264 down(&ftdi->u132_lock);
1265 command_size = ftdi->command_next - ftdi->command_head;
1266 if (command_size < COMMAND_SIZE) {
1267 struct u132_command *command = &ftdi->command[
1268 COMMAND_MASK & ftdi->command_next];
1269 command->header = 0x00 | cPCIu132wr;
1270 command->length = 0x04;
1271 command->address = 0x00;
1272 command->width = 0x00;
1273 command->follows = 4;
1274 command->value = data;
1275 command->buffer = &command->value;
1276 ftdi->command_next += 1;
1277 ftdi_elan_kick_command_queue(ftdi);
1278 up(&ftdi->u132_lock);
1281 up(&ftdi->u132_lock);
1288 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1291 u8 addressofs = config_offset / 4;
1292 wait:if (ftdi->disconnected > 0) {
1296 down(&ftdi->u132_lock);
1297 command_size = ftdi->command_next - ftdi->command_head;
1298 if (command_size < COMMAND_SIZE) {
1299 struct u132_command *command = &ftdi->command[
1300 COMMAND_MASK & ftdi->command_next];
1301 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1302 command->length = 0x04;
1303 command->address = addressofs;
1304 command->width = 0x00 | (width & 0x0F);
1305 command->follows = 4;
1306 command->value = data;
1307 command->buffer = &command->value;
1308 ftdi->command_next += 1;
1309 ftdi_elan_kick_command_queue(ftdi);
1310 up(&ftdi->u132_lock);
1313 up(&ftdi->u132_lock);
1320 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1323 u8 addressofs = mem_offset / 4;
1324 wait:if (ftdi->disconnected > 0) {
1328 down(&ftdi->u132_lock);
1329 command_size = ftdi->command_next - ftdi->command_head;
1330 if (command_size < COMMAND_SIZE) {
1331 struct u132_command *command = &ftdi->command[
1332 COMMAND_MASK & ftdi->command_next];
1333 command->header = 0x00 | (cPCImemwr & 0x0F);
1334 command->length = 0x04;
1335 command->address = addressofs;
1336 command->width = 0x00 | (width & 0x0F);
1337 command->follows = 4;
1338 command->value = data;
1339 command->buffer = &command->value;
1340 ftdi->command_next += 1;
1341 ftdi_elan_kick_command_queue(ftdi);
1342 up(&ftdi->u132_lock);
1345 up(&ftdi->u132_lock);
1352 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1355 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1356 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1360 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1361 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1363 wait:if (ftdi->disconnected > 0) {
1368 down(&ftdi->u132_lock);
1369 command_size = ftdi->command_next - ftdi->command_head;
1370 respond_size = ftdi->respond_next - ftdi->respond_head;
1371 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1373 struct u132_command *command = &ftdi->command[
1374 COMMAND_MASK & ftdi->command_next];
1375 struct u132_respond *respond = &ftdi->respond[
1376 RESPOND_MASK & ftdi->respond_next];
1377 int result = -ENODEV;
1378 respond->result = &result;
1379 respond->header = command->header = 0x00 | cPCIu132rd;
1380 command->length = 0x04;
1381 respond->address = command->address = cU132cmd_status;
1382 command->width = 0x00;
1383 command->follows = 0;
1385 command->buffer = NULL;
1386 respond->value = data;
1387 init_completion(&respond->wait_completion);
1388 ftdi->command_next += 1;
1389 ftdi->respond_next += 1;
1390 ftdi_elan_kick_command_queue(ftdi);
1391 up(&ftdi->u132_lock);
1392 wait_for_completion(&respond->wait_completion);
1395 up(&ftdi->u132_lock);
1402 int usb_ftdi_elan_read_reg(struct platform_device *pdev, u32 *data)
1404 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1405 return ftdi_elan_read_reg(ftdi, data);
1409 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_reg);
1410 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1411 u8 width, u32 *data)
1413 u8 addressofs = config_offset / 4;
1414 wait:if (ftdi->disconnected > 0) {
1419 down(&ftdi->u132_lock);
1420 command_size = ftdi->command_next - ftdi->command_head;
1421 respond_size = ftdi->respond_next - ftdi->respond_head;
1422 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1424 struct u132_command *command = &ftdi->command[
1425 COMMAND_MASK & ftdi->command_next];
1426 struct u132_respond *respond = &ftdi->respond[
1427 RESPOND_MASK & ftdi->respond_next];
1428 int result = -ENODEV;
1429 respond->result = &result;
1430 respond->header = command->header = 0x00 | (cPCIcfgrd &
1432 command->length = 0x04;
1433 respond->address = command->address = addressofs;
1434 command->width = 0x00 | (width & 0x0F);
1435 command->follows = 0;
1437 command->buffer = NULL;
1438 respond->value = data;
1439 init_completion(&respond->wait_completion);
1440 ftdi->command_next += 1;
1441 ftdi->respond_next += 1;
1442 ftdi_elan_kick_command_queue(ftdi);
1443 up(&ftdi->u132_lock);
1444 wait_for_completion(&respond->wait_completion);
1447 up(&ftdi->u132_lock);
1454 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1455 u8 width, u32 *data)
1457 u8 addressofs = mem_offset / 4;
1458 wait:if (ftdi->disconnected > 0) {
1463 down(&ftdi->u132_lock);
1464 command_size = ftdi->command_next - ftdi->command_head;
1465 respond_size = ftdi->respond_next - ftdi->respond_head;
1466 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1468 struct u132_command *command = &ftdi->command[
1469 COMMAND_MASK & ftdi->command_next];
1470 struct u132_respond *respond = &ftdi->respond[
1471 RESPOND_MASK & ftdi->respond_next];
1472 int result = -ENODEV;
1473 respond->result = &result;
1474 respond->header = command->header = 0x00 | (cPCImemrd &
1476 command->length = 0x04;
1477 respond->address = command->address = addressofs;
1478 command->width = 0x00 | (width & 0x0F);
1479 command->follows = 0;
1481 command->buffer = NULL;
1482 respond->value = data;
1483 init_completion(&respond->wait_completion);
1484 ftdi->command_next += 1;
1485 ftdi->respond_next += 1;
1486 ftdi_elan_kick_command_queue(ftdi);
1487 up(&ftdi->u132_lock);
1488 wait_for_completion(&respond->wait_completion);
1491 up(&ftdi->u132_lock);
1498 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1499 u8 width, u32 *data)
1501 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1502 if (ftdi->initialized == 0) {
1505 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1509 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1510 static int ftdi_elan_edset_setup(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 down(&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 command->header = 0x80 | (ed << 5);
1530 command->length = 0x8007;
1531 command->address = (toggle_bits << 6) | (ep_number << 2)
1533 command->width = usb_maxpacket(urb->dev, urb->pipe,
1534 usb_pipeout(urb->pipe));
1535 command->follows = 8;
1537 command->buffer = urb->setup_packet;
1538 target->callback = callback;
1539 target->endp = endp;
1542 ftdi->command_next += 1;
1543 ftdi_elan_kick_command_queue(ftdi);
1544 up(&ftdi->u132_lock);
1547 up(&ftdi->u132_lock);
1554 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1555 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1556 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1557 int toggle_bits, int error_count, int condition_code, int repeat_number,
1558 int halted, int skipped, int actual, int non_null))
1560 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1561 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1562 ep_number, toggle_bits, callback);
1566 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1567 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1568 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1569 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1570 int toggle_bits, int error_count, int condition_code, int repeat_number,
1571 int halted, int skipped, int actual, int non_null))
1573 u8 ed = ed_number - 1;
1574 wait:if (ftdi->disconnected > 0) {
1576 } else if (ftdi->initialized == 0) {
1580 down(&ftdi->u132_lock);
1581 command_size = ftdi->command_next - ftdi->command_head;
1582 if (command_size < COMMAND_SIZE) {
1583 struct u132_target *target = &ftdi->target[ed];
1584 struct u132_command *command = &ftdi->command[
1585 COMMAND_MASK & ftdi->command_next];
1586 int remaining_length = urb->transfer_buffer_length -
1588 command->header = 0x82 | (ed << 5);
1589 if (remaining_length == 0) {
1590 command->length = 0x0000;
1591 } else if (remaining_length > 1024) {
1592 command->length = 0x8000 | 1023;
1594 command->length = 0x8000 | (remaining_length -
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 up(&ftdi->u132_lock);
1612 up(&ftdi->u132_lock);
1619 int usb_ftdi_elan_edset_input(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_input(ftdi, ed_number, endp, urb, address,
1627 ep_number, toggle_bits, callback);
1631 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1632 static int ftdi_elan_edset_empty(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 down(&ftdi->u132_lock);
1646 command_size = ftdi->command_next - ftdi->command_head;
1647 if (command_size < COMMAND_SIZE) {
1648 struct u132_target *target = &ftdi->target[ed];
1649 struct u132_command *command = &ftdi->command[
1650 COMMAND_MASK & ftdi->command_next];
1651 command->header = 0x81 | (ed << 5);
1652 command->length = 0x0000;
1653 command->address = (toggle_bits << 6) | (ep_number << 2)
1655 command->width = usb_maxpacket(urb->dev, urb->pipe,
1656 usb_pipeout(urb->pipe));
1657 command->follows = 0;
1659 command->buffer = NULL;
1660 target->callback = callback;
1661 target->endp = endp;
1664 ftdi->command_next += 1;
1665 ftdi_elan_kick_command_queue(ftdi);
1666 up(&ftdi->u132_lock);
1669 up(&ftdi->u132_lock);
1676 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1677 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1678 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1679 int toggle_bits, int error_count, int condition_code, int repeat_number,
1680 int halted, int skipped, int actual, int non_null))
1682 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1683 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1684 ep_number, toggle_bits, callback);
1688 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1689 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1690 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1691 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1692 int toggle_bits, int error_count, int condition_code, int repeat_number,
1693 int halted, int skipped, int actual, int non_null))
1695 u8 ed = ed_number - 1;
1696 wait:if (ftdi->disconnected > 0) {
1698 } else if (ftdi->initialized == 0) {
1702 down(&ftdi->u132_lock);
1703 command_size = ftdi->command_next - ftdi->command_head;
1704 if (command_size < COMMAND_SIZE) {
1708 char data[30 *3 + 4];
1710 int m = (sizeof(data) - 1) / 3;
1712 struct u132_target *target = &ftdi->target[ed];
1713 struct u132_command *command = &ftdi->command[
1714 COMMAND_MASK & ftdi->command_next];
1715 command->header = 0x81 | (ed << 5);
1716 command->address = (toggle_bits << 6) | (ep_number << 2)
1718 command->width = usb_maxpacket(urb->dev, urb->pipe,
1719 usb_pipeout(urb->pipe));
1720 command->follows = min(1024,
1721 urb->transfer_buffer_length -
1722 urb->actual_length);
1724 command->buffer = urb->transfer_buffer +
1726 command->length = 0x8000 | (command->follows - 1);
1727 b = command->buffer;
1728 urb_size = command->follows;
1730 while (urb_size-- > 0) {
1732 } else if (i++ < m) {
1733 int w = sprintf(d, " %02X", *b++);
1737 d += sprintf(d, " ..");
1739 target->callback = callback;
1740 target->endp = endp;
1743 ftdi->command_next += 1;
1744 ftdi_elan_kick_command_queue(ftdi);
1745 up(&ftdi->u132_lock);
1748 up(&ftdi->u132_lock);
1755 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1756 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1757 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1758 int toggle_bits, int error_count, int condition_code, int repeat_number,
1759 int halted, int skipped, int actual, int non_null))
1761 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1762 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1763 ep_number, toggle_bits, callback);
1767 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1768 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1769 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1770 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1771 int toggle_bits, int error_count, int condition_code, int repeat_number,
1772 int halted, int skipped, int actual, int non_null))
1774 u8 ed = ed_number - 1;
1775 wait:if (ftdi->disconnected > 0) {
1777 } else if (ftdi->initialized == 0) {
1781 down(&ftdi->u132_lock);
1782 command_size = ftdi->command_next - ftdi->command_head;
1783 if (command_size < COMMAND_SIZE) {
1784 int remaining_length = urb->transfer_buffer_length -
1786 struct u132_target *target = &ftdi->target[ed];
1787 struct u132_command *command = &ftdi->command[
1788 COMMAND_MASK & ftdi->command_next];
1789 command->header = 0x83 | (ed << 5);
1790 if (remaining_length == 0) {
1791 command->length = 0x0000;
1792 } else if (remaining_length > 1024) {
1793 command->length = 0x8000 | 1023;
1795 command->length = 0x8000 | (remaining_length -
1797 command->address = (toggle_bits << 6) | (ep_number << 2)
1799 command->width = usb_maxpacket(urb->dev, urb->pipe,
1800 usb_pipeout(urb->pipe));
1801 command->follows = 0;
1803 command->buffer = NULL;
1804 target->callback = callback;
1805 target->endp = endp;
1808 ftdi->command_next += 1;
1809 ftdi_elan_kick_command_queue(ftdi);
1810 up(&ftdi->u132_lock);
1813 up(&ftdi->u132_lock);
1820 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1821 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1822 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1823 int toggle_bits, int error_count, int condition_code, int repeat_number,
1824 int halted, int skipped, int actual, int non_null))
1826 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1827 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1828 ep_number, toggle_bits, callback);
1832 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1833 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1836 u8 ed = ed_number - 1;
1837 if (ftdi->disconnected > 0) {
1839 } else if (ftdi->initialized == 0) {
1842 struct u132_target *target = &ftdi->target[ed];
1843 down(&ftdi->u132_lock);
1844 if (target->abandoning > 0) {
1845 up(&ftdi->u132_lock);
1848 target->abandoning = 1;
1849 wait_1:if (target->active == 1) {
1850 int command_size = ftdi->command_next -
1852 if (command_size < COMMAND_SIZE) {
1853 struct u132_command *command =
1854 &ftdi->command[COMMAND_MASK &
1855 ftdi->command_next];
1856 command->header = 0x80 | (ed << 5) |
1858 command->length = 0x00;
1859 command->address = 0x00;
1860 command->width = 0x00;
1861 command->follows = 0;
1863 command->buffer = &command->value;
1864 ftdi->command_next += 1;
1865 ftdi_elan_kick_command_queue(ftdi);
1867 up(&ftdi->u132_lock);
1869 down(&ftdi->u132_lock);
1873 up(&ftdi->u132_lock);
1879 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1882 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1883 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1887 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1888 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1890 int retry_on_empty = 10;
1891 int retry_on_timeout = 5;
1892 int retry_on_status = 20;
1894 int packet_bytes = 0;
1895 int retval = usb_bulk_msg(ftdi->udev,
1896 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1897 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1898 &packet_bytes, msecs_to_jiffies(100));
1899 if (packet_bytes > 2) {
1900 char diag[30 *3 + 4];
1902 int m = (sizeof(diag) - 1) / 3;
1903 char *b = ftdi->bulk_in_buffer;
1906 while (packet_bytes-- > 0) {
1908 if (bytes_read < m) {
1909 d += sprintf(d, " %02X",
1911 } else if (bytes_read > m) {
1913 d += sprintf(d, " ..");
1918 } else if (packet_bytes > 1) {
1919 char s1 = ftdi->bulk_in_buffer[0];
1920 char s2 = ftdi->bulk_in_buffer[1];
1921 if (s1 == 0x31 && s2 == 0x60) {
1923 } else if (retry_on_status-- > 0) {
1926 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1930 } else if (packet_bytes > 0) {
1931 char b1 = ftdi->bulk_in_buffer[0];
1932 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
1933 "TDI = %02X\n", b1);
1934 if (retry_on_status-- > 0) {
1937 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
1941 } else if (retval == -ETIMEDOUT) {
1942 if (retry_on_timeout-- > 0) {
1945 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
1949 } else if (retval == 0) {
1950 if (retry_on_empty-- > 0) {
1953 dev_err(&ftdi->udev->dev, "empty packet retry l"
1958 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1967 * send the long flush sequence
1970 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1977 urb = usb_alloc_urb(0, GFP_KERNEL);
1979 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
1983 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1985 dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
1992 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1993 ftdi->bulk_out_endpointAddr), buf, i,
1994 ftdi_elan_write_bulk_callback, ftdi);
1995 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1996 retval = usb_submit_urb(urb, GFP_KERNEL);
1998 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
1999 "flush sequence\n");
2000 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2010 * send the reset sequence
2013 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
2020 urb = usb_alloc_urb(0, GFP_KERNEL);
2022 dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
2026 buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
2028 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
2037 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2038 ftdi->bulk_out_endpointAddr), buf, i,
2039 ftdi_elan_write_bulk_callback, ftdi);
2040 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2041 retval = usb_submit_urb(urb, GFP_KERNEL);
2043 dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
2044 "reset sequence\n");
2045 usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
2053 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2057 int retry_on_timeout = 5;
2058 int retry_on_empty = 10;
2060 retval = ftdi_elan_flush_input_fifo(ftdi);
2063 ftdi->bulk_in_left = 0;
2064 ftdi->bulk_in_last = -1;
2065 while (long_stop-- > 0) {
2068 retval = ftdi_elan_synchronize_flush(ftdi);
2071 retval = ftdi_elan_flush_input_fifo(ftdi);
2074 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2080 int packet_bytes = 0;
2081 retval = usb_bulk_msg(ftdi->udev,
2082 usb_rcvbulkpipe(ftdi->udev,
2083 ftdi->bulk_in_endpointAddr),
2084 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2085 &packet_bytes, msecs_to_jiffies(500));
2086 if (packet_bytes > 2) {
2087 char diag[30 *3 + 4];
2089 int m = (sizeof(diag) - 1) / 3;
2090 char *b = ftdi->bulk_in_buffer;
2092 unsigned char c = 0;
2094 while (packet_bytes-- > 0) {
2096 if (bytes_read < m) {
2097 d += sprintf(d, " %02X", c);
2098 } else if (bytes_read > m) {
2100 d += sprintf(d, " ..");
2109 } else if (read_stop-- > 0) {
2112 dev_err(&ftdi->udev->dev, "retr"
2113 "y limit reached\n");
2117 } else if (packet_bytes > 1) {
2118 unsigned char s1 = ftdi->bulk_in_buffer[0];
2119 unsigned char s2 = ftdi->bulk_in_buffer[1];
2120 if (s1 == 0x31 && s2 == 0x00) {
2121 if (read_stuck-- > 0) {
2125 } else if (s1 == 0x31 && s2 == 0x60) {
2126 if (read_stop-- > 0) {
2129 dev_err(&ftdi->udev->dev, "retr"
2130 "y limit reached\n");
2134 if (read_stop-- > 0) {
2137 dev_err(&ftdi->udev->dev, "retr"
2138 "y limit reached\n");
2142 } else if (packet_bytes > 0) {
2143 if (read_stop-- > 0) {
2146 dev_err(&ftdi->udev->dev, "retry limit "
2150 } else if (retval == -ETIMEDOUT) {
2151 if (retry_on_timeout-- > 0) {
2154 dev_err(&ftdi->udev->dev, "TIMED OUT re"
2155 "try limit reached\n");
2158 } else if (retval == 0) {
2159 if (retry_on_empty-- > 0) {
2162 dev_err(&ftdi->udev->dev, "empty packet"
2163 " retry limit reached\n");
2168 dev_err(&ftdi->udev->dev, "error = %d\n",
2170 if (read_stop-- > 0) {
2173 dev_err(&ftdi->udev->dev, "retry limit "
2180 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2184 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2186 int retry_on_empty = 10;
2187 int retry_on_timeout = 5;
2188 int retry_on_status = 50;
2190 int packet_bytes = 0;
2191 int retval = usb_bulk_msg(ftdi->udev,
2192 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2193 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2194 &packet_bytes, msecs_to_jiffies(1000));
2195 if (packet_bytes > 2) {
2196 char diag[30 *3 + 4];
2198 int m = (sizeof(diag) - 1) / 3;
2199 char *b = ftdi->bulk_in_buffer;
2202 while (packet_bytes-- > 0) {
2204 if (bytes_read < m) {
2205 d += sprintf(d, " %02X",
2207 } else if (bytes_read > m) {
2209 d += sprintf(d, " ..");
2214 } else if (packet_bytes > 1) {
2215 char s1 = ftdi->bulk_in_buffer[0];
2216 char s2 = ftdi->bulk_in_buffer[1];
2217 if (s1 == 0x31 && s2 == 0x60) {
2219 } else if (retry_on_status-- > 0) {
2224 } else if (packet_bytes > 0) {
2225 char b1 = ftdi->bulk_in_buffer[0];
2226 dev_err(&ftdi->udev->dev, "only one byte flushed from F"
2227 "TDI = %02X\n", b1);
2228 if (retry_on_status-- > 0) {
2232 dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
2236 } else if (retval == -ETIMEDOUT) {
2237 if (retry_on_timeout-- > 0) {
2240 dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
2244 } else if (retval == 0) {
2245 if (retry_on_empty-- > 0) {
2248 dev_err(&ftdi->udev->dev, "empty packet retry l"
2253 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2260 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2262 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2265 if (ftdi->controlreg & 0x00400000) {
2266 if (ftdi->card_ejected) {
2268 ftdi->card_ejected = 1;
2269 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
2270 "%08X\n", ftdi->controlreg);
2274 u8 fn = ftdi->function - 1;
2275 int activePCIfn = fn << 8;
2280 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2284 pciVID = pcidata & 0xFFFF;
2285 pciPID = (pcidata >> 16) & 0xFFFF;
2286 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2287 ftdi->platform_data.device) {
2290 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X devi"
2291 "ce=%04X pciPID=%04X\n",
2292 ftdi->platform_data.vendor, pciVID,
2293 ftdi->platform_data.device, pciPID);
2299 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2308 int activePCIfn = 0;
2311 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2314 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2318 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2321 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2324 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2327 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2330 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2334 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2337 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2340 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2343 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2346 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2350 for (fn = 0; (fn < 4) && (!foundOHCI); fn++) {
2351 activePCIfn = fn << 8;
2352 ftdi->function = fn + 1;
2353 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2357 pciVID = pcidata & 0xFFFF;
2358 pciPID = (pcidata >> 16) & 0xFFFF;
2359 if ((pciVID == 0x1045) && (pciPID == 0xc861)) {
2361 } else if ((pciVID == 0x1033) && (pciPID == 0x0035)) {
2363 } else if ((pciVID == 0x10b9) && (pciPID == 0x5237)) {
2365 } else if ((pciVID == 0x11c1) && (pciPID == 0x5802)) {
2367 } else if ((pciVID == 0x11AB) && (pciPID == 0x1FA6)) {
2370 if (foundOHCI == 0) {
2373 ftdi->platform_data.vendor = pciVID;
2374 ftdi->platform_data.device = pciPID;
2375 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2379 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2383 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2387 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2391 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2396 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2400 latence_timer &= 0xFFFF00FF;
2401 latence_timer |= 0x00001600;
2402 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2406 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2411 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2415 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2422 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2427 int reset_repeat = 0;
2429 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x01);
2433 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2438 if (reset_repeat++ > 100) {
2448 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x11000000);
2451 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2455 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf);
2458 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2462 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf2edf);
2465 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2469 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0xA0);
2472 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2477 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x04);
2480 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2484 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2488 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2492 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x00001200);
2495 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2499 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2503 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2507 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x28002edf);
2510 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2515 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10000);
2519 power_check:U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2522 if (!(pcidata & 1)) {
2528 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2532 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2536 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2539 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2543 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10);
2546 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2552 U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
2557 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2562 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2566 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2569 dump_regs:for (reg = 0; reg <= 0x54; reg += 4) {
2570 U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2579 * we use only the first bulk-in and bulk-out endpoints
2581 static int ftdi_elan_probe(struct usb_interface *interface,
2582 const struct usb_device_id *id)
2584 struct usb_host_interface *iface_desc;
2585 struct usb_endpoint_descriptor *endpoint;
2588 int retval = -ENOMEM;
2589 struct usb_ftdi *ftdi = kmalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2591 printk(KERN_ERR "Out of memory\n");
2594 memset(ftdi, 0x00, sizeof(struct usb_ftdi));
2595 down(&ftdi_module_lock);
2596 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2597 ftdi->sequence_num = ++ftdi_instances;
2598 up(&ftdi_module_lock);
2599 ftdi_elan_init_kref(ftdi);
2600 init_MUTEX(&ftdi->sw_lock);
2601 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2602 ftdi->interface = interface;
2603 init_MUTEX(&ftdi->u132_lock);
2605 iface_desc = interface->cur_altsetting;
2606 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2607 endpoint = &iface_desc->endpoint[i].desc;
2608 if (!ftdi->bulk_in_endpointAddr &&
2609 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
2610 == USB_DIR_IN) && ((endpoint->bmAttributes &
2611 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK))
2613 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2614 ftdi->bulk_in_size = buffer_size;
2615 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2616 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2617 if (!ftdi->bulk_in_buffer) {
2618 dev_err(&ftdi->udev->dev, "Could not allocate b"
2624 if (!ftdi->bulk_out_endpointAddr &&
2625 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
2626 == USB_DIR_OUT) && ((endpoint->bmAttributes &
2627 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK))
2629 ftdi->bulk_out_endpointAddr =
2630 endpoint->bEndpointAddress;
2633 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
2634 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk"
2635 "-out endpoints\n");
2639 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2640 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2641 ftdi->bulk_out_endpointAddr);
2642 usb_set_intfdata(interface, ftdi);
2643 if (iface_desc->desc.bInterfaceNumber == 0 &&
2644 ftdi->bulk_in_endpointAddr == 0x81 &&
2645 ftdi->bulk_out_endpointAddr == 0x02) {
2646 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2648 dev_err(&ftdi->udev->dev, "Not able to get a minor for "
2650 usb_set_intfdata(interface, NULL);
2654 ftdi->class = &ftdi_elan_jtag_class;
2655 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface "
2656 "%d now attached to ftdi%d\n", ftdi,
2657 iface_desc->desc.bInterfaceNumber,
2661 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2662 ftdi->bulk_in_endpointAddr == 0x83 &&
2663 ftdi->bulk_out_endpointAddr == 0x04) {
2665 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now a"
2666 "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
2667 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2668 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2669 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2670 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2673 dev_err(&ftdi->udev->dev,
2674 "Could not find ELAN's U132 device\n");
2679 ftdi_elan_put_kref(ftdi);
2684 static void ftdi_elan_disconnect(struct usb_interface *interface)
2686 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2687 ftdi->disconnected += 1;
2689 int minor = interface->minor;
2690 struct usb_class_driver *class = ftdi->class;
2691 usb_set_intfdata(interface, NULL);
2692 usb_deregister_dev(interface, class);
2693 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on min"
2694 "or %d now disconnected\n", minor);
2696 ftdi_status_cancel_work(ftdi);
2697 ftdi_command_cancel_work(ftdi);
2698 ftdi_response_cancel_work(ftdi);
2699 ftdi_elan_abandon_completions(ftdi);
2700 ftdi_elan_abandon_targets(ftdi);
2701 if (ftdi->registered) {
2702 platform_device_unregister(&ftdi->platform_dev);
2703 ftdi->synchronized = 0;
2704 ftdi->enumerated = 0;
2705 ftdi->registered = 0;
2707 flush_workqueue(status_queue);
2708 flush_workqueue(command_queue);
2709 flush_workqueue(respond_queue);
2710 ftdi->disconnected += 1;
2711 usb_set_intfdata(interface, NULL);
2712 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller inter"
2713 "face now disconnected\n");
2715 ftdi_elan_put_kref(ftdi);
2718 static struct usb_driver ftdi_elan_driver = {
2719 .name = "ftdi-elan",
2720 .probe = ftdi_elan_probe,
2721 .disconnect = ftdi_elan_disconnect,
2722 .id_table = ftdi_elan_table,
2724 static int __init ftdi_elan_init(void)
2727 printk(KERN_INFO "driver %s built at %s on %s\n", ftdi_elan_driver.name,
2728 __TIME__, __DATE__);
2729 init_MUTEX(&ftdi_module_lock);
2730 INIT_LIST_HEAD(&ftdi_static_list);
2731 status_queue = create_singlethread_workqueue("ftdi-status-control");
2732 command_queue = create_singlethread_workqueue("ftdi-command-engine");
2733 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
2734 result = usb_register(&ftdi_elan_driver);
2736 printk(KERN_ERR "usb_register failed. Error number %d\n",
2741 static void __exit ftdi_elan_exit(void)
2743 struct usb_ftdi *ftdi;
2744 struct usb_ftdi *temp;
2745 usb_deregister(&ftdi_elan_driver);
2746 printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2747 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2748 ftdi_status_cancel_work(ftdi);
2749 ftdi_command_cancel_work(ftdi);
2750 ftdi_response_cancel_work(ftdi);
2751 } flush_workqueue(status_queue);
2752 destroy_workqueue(status_queue);
2753 status_queue = NULL;
2754 flush_workqueue(command_queue);
2755 destroy_workqueue(command_queue);
2756 command_queue = NULL;
2757 flush_workqueue(respond_queue);
2758 destroy_workqueue(respond_queue);
2759 respond_queue = NULL;
2763 module_init(ftdi_elan_init);
2764 module_exit(ftdi_elan_exit);