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
== -EILSEQ
) {
517 ftdi
->disconnected
+= 1;
519 ftdi
->disconnected
+= 1;
520 dev_err(&ftdi
->udev
->dev
, "respond error %d\n", retval
);
522 if (ftdi
->disconnected
> 0) {
523 ftdi_elan_abandon_completions(ftdi
);
524 ftdi_elan_abandon_targets(ftdi
);
526 ftdi_response_requeue_work(ftdi
, msecs_to_jiffies(10));
533 * the sw_lock is initially held and will be freed
534 * after the FTDI has been synchronized
537 static void ftdi_elan_status_work(void *data
)
539 struct usb_ftdi
*ftdi
= data
;
540 int work_delay_in_msec
= 0;
541 if (ftdi
->disconnected
> 0) {
542 ftdi_elan_put_kref(ftdi
);
544 } else if (ftdi
->synchronized
== 0) {
545 down(&ftdi
->sw_lock
);
546 if (ftdi_elan_synchronize(ftdi
) == 0) {
547 ftdi
->synchronized
= 1;
548 ftdi_command_queue_work(ftdi
, 1);
549 ftdi_respond_queue_work(ftdi
, 1);
551 work_delay_in_msec
= 100;
553 dev_err(&ftdi
->udev
->dev
, "synchronize failed\n");
555 work_delay_in_msec
= 10 *1000;
557 } else if (ftdi
->stuck_status
> 0) {
558 if (ftdi_elan_stuck_waiting(ftdi
) == 0) {
559 ftdi
->stuck_status
= 0;
560 ftdi
->synchronized
= 0;
561 } else if ((ftdi
->stuck_status
++ % 60) == 1) {
562 dev_err(&ftdi
->udev
->dev
, "WRONG type of card inserted "
563 "- please remove\n");
565 dev_err(&ftdi
->udev
->dev
, "WRONG type of card inserted "
566 "- checked %d times\n", ftdi
->stuck_status
);
567 work_delay_in_msec
= 100;
568 } else if (ftdi
->enumerated
== 0) {
569 if (ftdi_elan_enumeratePCI(ftdi
) == 0) {
570 ftdi
->enumerated
= 1;
571 work_delay_in_msec
= 250;
573 work_delay_in_msec
= 1000;
574 } else if (ftdi
->initialized
== 0) {
575 if (ftdi_elan_setupOHCI(ftdi
) == 0) {
576 ftdi
->initialized
= 1;
577 work_delay_in_msec
= 500;
579 dev_err(&ftdi
->udev
->dev
, "initialized failed - trying "
580 "again in 10 seconds\n");
581 work_delay_in_msec
= 10 *1000;
583 } else if (ftdi
->registered
== 0) {
584 work_delay_in_msec
= 10;
585 if (ftdi_elan_hcd_init(ftdi
) == 0) {
586 ftdi
->registered
= 1;
588 dev_err(&ftdi
->udev
->dev
, "register failed\n");
589 work_delay_in_msec
= 250;
591 if (ftdi_elan_checkingPCI(ftdi
) == 0) {
592 work_delay_in_msec
= 250;
593 } else if (ftdi
->controlreg
& 0x00400000) {
594 if (ftdi
->gone_away
> 0) {
595 dev_err(&ftdi
->udev
->dev
, "PCI device eject con"
596 "firmed platform_dev.dev.parent=%p plat"
598 ftdi
->platform_dev
.dev
.parent
,
599 &ftdi
->platform_dev
.dev
);
600 platform_device_unregister(&ftdi
->platform_dev
);
601 ftdi
->platform_dev
.dev
.parent
= NULL
;
602 ftdi
->registered
= 0;
603 ftdi
->enumerated
= 0;
604 ftdi
->card_ejected
= 0;
605 ftdi
->initialized
= 0;
608 ftdi_elan_flush_targets(ftdi
);
609 work_delay_in_msec
= 250;
611 dev_err(&ftdi
->udev
->dev
, "PCI device has disappeared\n"
613 ftdi_elan_cancel_targets(ftdi
);
614 work_delay_in_msec
= 500;
615 ftdi
->enumerated
= 0;
616 ftdi
->initialized
= 0;
619 if (ftdi
->disconnected
> 0) {
620 ftdi_elan_put_kref(ftdi
);
623 ftdi_status_requeue_work(ftdi
,
624 msecs_to_jiffies(work_delay_in_msec
));
631 * file_operations for the jtag interface
633 * the usage count for the device is incremented on open()
634 * and decremented on release()
636 static int ftdi_elan_open(struct inode
*inode
, struct file
*file
)
638 int subminor
= iminor(inode
);
639 struct usb_interface
*interface
= usb_find_interface(&ftdi_elan_driver
,
642 printk(KERN_ERR
"can't find device for minor %d\n", subminor
);
645 struct usb_ftdi
*ftdi
= usb_get_intfdata(interface
);
649 if (down_interruptible(&ftdi
->sw_lock
)) {
652 ftdi_elan_get_kref(ftdi
);
653 file
->private_data
= ftdi
;
660 static int ftdi_elan_release(struct inode
*inode
, struct file
*file
)
662 struct usb_ftdi
*ftdi
= (struct usb_ftdi
*)file
->private_data
;
665 up(&ftdi
->sw_lock
); /* decrement the count on our device */
666 ftdi_elan_put_kref(ftdi
);
671 #define FTDI_ELAN_IOC_MAGIC 0xA1
672 #define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
673 static int ftdi_elan_ioctl(struct inode
*inode
, struct file
*file
,
674 unsigned int cmd
, unsigned long arg
)
677 case FTDI_ELAN_IOCDEBUG
:{
679 int size
= strncpy_from_user(line
,
680 (const char __user
*)arg
, sizeof(line
));
684 printk(KERN_ERR
"TODO: ioctl %s\n", line
);
696 * blocking bulk reads are used to get data from the device
699 static ssize_t
ftdi_elan_read(struct file
*file
, char __user
*buffer
,
700 size_t count
, loff_t
*ppos
)
702 char data
[30 *3 + 4];
704 int m
= (sizeof(data
) - 1) / 3;
706 int retry_on_empty
= 10;
707 int retry_on_timeout
= 5;
708 struct usb_ftdi
*ftdi
= (struct usb_ftdi
*)file
->private_data
;
709 if (ftdi
->disconnected
> 0) {
713 have
:if (ftdi
->bulk_in_left
> 0) {
715 char *p
= ++ftdi
->bulk_in_last
+ ftdi
->bulk_in_buffer
;
716 ftdi
->bulk_in_left
-= 1;
717 if (bytes_read
< m
) {
718 d
+= sprintf(d
, " %02X", 0x000000FF & *p
);
719 } else if (bytes_read
> m
) {
721 d
+= sprintf(d
, " ..");
722 if (copy_to_user(buffer
++, p
, 1)) {
731 more
:if (count
> 0) {
732 int packet_bytes
= 0;
733 int retval
= usb_bulk_msg(ftdi
->udev
,
734 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
735 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
736 &packet_bytes
, msecs_to_jiffies(50));
737 if (packet_bytes
> 2) {
738 ftdi
->bulk_in_left
= packet_bytes
- 2;
739 ftdi
->bulk_in_last
= 1;
741 } else if (retval
== -ETIMEDOUT
) {
742 if (retry_on_timeout
-- > 0) {
744 } else if (bytes_read
> 0) {
748 } else if (retval
== 0) {
749 if (retry_on_empty
-- > 0) {
759 static void ftdi_elan_write_bulk_callback(struct urb
*urb
)
761 struct usb_ftdi
*ftdi
= (struct usb_ftdi
*)urb
->context
;
762 if (urb
->status
&& !(urb
->status
== -ENOENT
|| urb
->status
==
763 -ECONNRESET
|| urb
->status
== -ESHUTDOWN
)) {
764 dev_err(&ftdi
->udev
->dev
, "urb=%p write bulk status received: %"
765 "d\n", urb
, urb
->status
);
767 usb_buffer_free(urb
->dev
, urb
->transfer_buffer_length
,
768 urb
->transfer_buffer
, urb
->transfer_dma
);
771 static int fill_buffer_with_all_queued_commands(struct usb_ftdi
*ftdi
,
772 char *buf
, int command_size
, int total_size
)
776 int I
= command_size
;
777 int i
= ftdi
->command_head
;
779 struct u132_command
*command
= &ftdi
->command
[COMMAND_MASK
&
781 int F
= command
->follows
;
782 u8
*f
= command
->buffer
;
783 if (command
->header
& 0x80) {
784 ed_commands
|= 1 << (0x3 & (command
->header
>> 5));
786 buf
[b
++] = command
->header
;
787 buf
[b
++] = (command
->length
>> 0) & 0x00FF;
788 buf
[b
++] = (command
->length
>> 8) & 0x00FF;
789 buf
[b
++] = command
->address
;
790 buf
[b
++] = command
->width
;
798 static int ftdi_elan_total_command_size(struct usb_ftdi
*ftdi
, int command_size
)
801 int I
= command_size
;
802 int i
= ftdi
->command_head
;
804 struct u132_command
*command
= &ftdi
->command
[COMMAND_MASK
&
806 total_size
+= 5 + command
->follows
;
810 static int ftdi_elan_command_engine(struct usb_ftdi
*ftdi
)
817 int command_size
= ftdi
->command_next
- ftdi
->command_head
;
818 if (command_size
== 0)
820 total_size
= ftdi_elan_total_command_size(ftdi
, command_size
);
821 urb
= usb_alloc_urb(0, GFP_KERNEL
);
823 dev_err(&ftdi
->udev
->dev
, "could not get a urb to write %d comm"
824 "ands totaling %d bytes to the Uxxx\n", command_size
,
828 buf
= usb_buffer_alloc(ftdi
->udev
, total_size
, GFP_KERNEL
,
831 dev_err(&ftdi
->udev
->dev
, "could not get a buffer to write %d c"
832 "ommands totaling %d bytes to the Uxxx\n", command_size
,
837 ed_commands
= fill_buffer_with_all_queued_commands(ftdi
, buf
,
838 command_size
, total_size
);
839 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
840 ftdi
->bulk_out_endpointAddr
), buf
, total_size
,
841 ftdi_elan_write_bulk_callback
, ftdi
);
842 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
844 char diag
[40 *3 + 4];
848 int s
= (sizeof(diag
) - 1) / 3;
850 while (s
-- > 0 && m
-- > 0) {
851 if (s
> 0 || m
== 0) {
852 d
+= sprintf(d
, " %02X", *c
++);
854 d
+= sprintf(d
, " ..");
857 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
859 dev_err(&ftdi
->udev
->dev
, "failed %d to submit urb %p to write "
860 "%d commands totaling %d bytes to the Uxxx\n", retval
,
861 urb
, command_size
, total_size
);
862 usb_buffer_free(ftdi
->udev
, total_size
, buf
, urb
->transfer_dma
);
866 usb_free_urb(urb
); /* release our reference to this urb,
867 the USB core will eventually free it entirely */
868 ftdi
->command_head
+= command_size
;
869 ftdi_elan_kick_respond_queue(ftdi
);
873 static void ftdi_elan_do_callback(struct usb_ftdi
*ftdi
,
874 struct u132_target
*target
, u8
*buffer
, int length
)
876 struct urb
*urb
= target
->urb
;
877 int halted
= target
->halted
;
878 int skipped
= target
->skipped
;
879 int actual
= target
->actual
;
880 int non_null
= target
->non_null
;
881 int toggle_bits
= target
->toggle_bits
;
882 int error_count
= target
->error_count
;
883 int condition_code
= target
->condition_code
;
884 int repeat_number
= target
->repeat_number
;
885 void (*callback
) (void *, struct urb
*, u8
*, int, int, int, int, int,
886 int, int, int, int) = target
->callback
;
888 target
->callback
= NULL
;
889 (*callback
) (target
->endp
, urb
, buffer
, length
, toggle_bits
,
890 error_count
, condition_code
, repeat_number
, halted
, skipped
,
894 static char *have_ed_set_response(struct usb_ftdi
*ftdi
,
895 struct u132_target
*target
, u16 ed_length
, int ed_number
, int ed_type
,
898 int payload
= (ed_length
>> 0) & 0x07FF;
899 down(&ftdi
->u132_lock
);
901 target
->non_null
= (ed_length
>> 15) & 0x0001;
902 target
->repeat_number
= (ed_length
>> 11) & 0x000F;
903 if (ed_type
== 0x02) {
904 if (payload
== 0 || target
->abandoning
> 0) {
905 target
->abandoning
= 0;
906 up(&ftdi
->u132_lock
);
907 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
912 return ftdi
->response
;
914 ftdi
->expected
= 4 + payload
;
916 up(&ftdi
->u132_lock
);
919 } else if (ed_type
== 0x03) {
920 if (payload
== 0 || target
->abandoning
> 0) {
921 target
->abandoning
= 0;
922 up(&ftdi
->u132_lock
);
923 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
928 return ftdi
->response
;
930 ftdi
->expected
= 4 + payload
;
932 up(&ftdi
->u132_lock
);
935 } else if (ed_type
== 0x01) {
936 target
->abandoning
= 0;
937 up(&ftdi
->u132_lock
);
938 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
943 return ftdi
->response
;
945 target
->abandoning
= 0;
946 up(&ftdi
->u132_lock
);
947 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
952 return ftdi
->response
;
956 static char *have_ed_get_response(struct usb_ftdi
*ftdi
,
957 struct u132_target
*target
, u16 ed_length
, int ed_number
, int ed_type
,
960 down(&ftdi
->u132_lock
);
961 target
->condition_code
= TD_DEVNOTRESP
;
962 target
->actual
= (ed_length
>> 0) & 0x01FF;
963 target
->non_null
= (ed_length
>> 15) & 0x0001;
964 target
->repeat_number
= (ed_length
>> 11) & 0x000F;
965 up(&ftdi
->u132_lock
);
967 ftdi_elan_do_callback(ftdi
, target
, NULL
, 0);
968 target
->abandoning
= 0;
972 return ftdi
->response
;
977 * The engine tries to empty the FTDI fifo
979 * all responses found in the fifo data are dispatched thus
980 * the response buffer can only ever hold a maximum sized
981 * response from the Uxxx.
984 static int ftdi_elan_respond_engine(struct usb_ftdi
*ftdi
)
986 u8
*b
= ftdi
->response
+ ftdi
->recieved
;
988 int retry_on_empty
= 1;
989 int retry_on_timeout
= 3;
990 int empty_packets
= 0;
992 int packet_bytes
= 0;
993 int retval
= usb_bulk_msg(ftdi
->udev
,
994 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
995 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
996 &packet_bytes
, msecs_to_jiffies(500));
997 char diag
[30 *3 + 4];
999 int m
= packet_bytes
;
1000 u8
*c
= ftdi
->bulk_in_buffer
;
1001 int s
= (sizeof(diag
) - 1) / 3;
1003 while (s
-- > 0 && m
-- > 0) {
1004 if (s
> 0 || m
== 0) {
1005 d
+= sprintf(d
, " %02X", *c
++);
1007 d
+= sprintf(d
, " ..");
1009 if (packet_bytes
> 2) {
1010 ftdi
->bulk_in_left
= packet_bytes
- 2;
1011 ftdi
->bulk_in_last
= 1;
1013 } else if (retval
== -ETIMEDOUT
) {
1014 if (retry_on_timeout
-- > 0) {
1015 dev_err(&ftdi
->udev
->dev
, "TIMED OUT with packe"
1016 "t_bytes = %d with total %d bytes%s\n",
1017 packet_bytes
, bytes_read
, diag
);
1019 } else if (bytes_read
> 0) {
1020 dev_err(&ftdi
->udev
->dev
, "ONLY %d bytes%s\n",
1024 dev_err(&ftdi
->udev
->dev
, "TIMED OUT with packe"
1025 "t_bytes = %d with total %d bytes%s\n",
1026 packet_bytes
, bytes_read
, diag
);
1029 } else if (retval
== -EILSEQ
) {
1030 dev_err(&ftdi
->udev
->dev
, "error = %d with packet_bytes"
1031 " = %d with total %d bytes%s\n", retval
,
1032 packet_bytes
, bytes_read
, diag
);
1034 } else if (retval
) {
1035 dev_err(&ftdi
->udev
->dev
, "error = %d with packet_bytes"
1036 " = %d with total %d bytes%s\n", retval
,
1037 packet_bytes
, bytes_read
, diag
);
1039 } else if (packet_bytes
== 2) {
1040 unsigned char s0
= ftdi
->bulk_in_buffer
[0];
1041 unsigned char s1
= ftdi
->bulk_in_buffer
[1];
1043 if (s0
== 0x31 && s1
== 0x60) {
1044 if (retry_on_empty
-- > 0) {
1048 } else if (s0
== 0x31 && s1
== 0x00) {
1049 if (retry_on_empty
-- > 0) {
1054 if (retry_on_empty
-- > 0) {
1059 } else if (packet_bytes
== 1) {
1060 if (retry_on_empty
-- > 0) {
1065 if (retry_on_empty
-- > 0) {
1074 have
:if (ftdi
->bulk_in_left
> 0) {
1075 u8 c
= ftdi
->bulk_in_buffer
[++ftdi
->bulk_in_last
];
1077 ftdi
->bulk_in_left
-= 1;
1078 if (ftdi
->recieved
== 0 && c
== 0xFF) {
1082 if (++ftdi
->recieved
< ftdi
->expected
) {
1084 } else if (ftdi
->ed_found
) {
1085 int ed_number
= (ftdi
->response
[0] >> 5) & 0x03;
1086 u16 ed_length
= (ftdi
->response
[2] << 8) |
1088 struct u132_target
*target
= &ftdi
->target
[ed_number
];
1089 int payload
= (ed_length
>> 0) & 0x07FF;
1090 char diag
[30 *3 + 4];
1093 u8
*c
= 4 + ftdi
->response
;
1094 int s
= (sizeof(diag
) - 1) / 3;
1096 while (s
-- > 0 && m
-- > 0) {
1097 if (s
> 0 || m
== 0) {
1098 d
+= sprintf(d
, " %02X", *c
++);
1100 d
+= sprintf(d
, " ..");
1102 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
1109 } else if (ftdi
->expected
== 8) {
1111 int respond_head
= ftdi
->respond_head
++;
1112 struct u132_respond
*respond
= &ftdi
->respond
[
1113 RESPOND_MASK
& respond_head
];
1114 u32 data
= ftdi
->response
[7];
1116 data
|= ftdi
->response
[6];
1118 data
|= ftdi
->response
[5];
1120 data
|= ftdi
->response
[4];
1121 *respond
->value
= data
;
1122 *respond
->result
= 0;
1123 complete(&respond
->wait_completion
);
1128 buscmd
= (ftdi
->response
[0] >> 0) & 0x0F;
1129 if (buscmd
== 0x00) {
1130 } else if (buscmd
== 0x02) {
1131 } else if (buscmd
== 0x06) {
1132 } else if (buscmd
== 0x0A) {
1134 dev_err(&ftdi
->udev
->dev
, "Uxxx unknown(%0X) va"
1135 "lue = %08X\n", buscmd
, data
);
1138 if ((ftdi
->response
[0] & 0x80) == 0x00) {
1142 int ed_number
= (ftdi
->response
[0] >> 5) & 0x03;
1143 int ed_type
= (ftdi
->response
[0] >> 0) & 0x03;
1144 u16 ed_length
= (ftdi
->response
[2] << 8) |
1146 struct u132_target
*target
= &ftdi
->target
[
1148 target
->halted
= (ftdi
->response
[0] >> 3) &
1150 target
->skipped
= (ftdi
->response
[0] >> 2) &
1152 target
->toggle_bits
= (ftdi
->response
[3] >> 6)
1154 target
->error_count
= (ftdi
->response
[3] >> 4)
1156 target
->condition_code
= (ftdi
->response
[
1158 if ((ftdi
->response
[0] & 0x10) == 0x00) {
1159 b
= have_ed_set_response(ftdi
, target
,
1160 ed_length
, ed_number
, ed_type
,
1164 b
= have_ed_get_response(ftdi
, target
,
1165 ed_length
, ed_number
, ed_type
,
1177 * create a urb, and a buffer for it, and copy the data to the urb
1180 static ssize_t
ftdi_elan_write(struct file
*file
,
1181 const char __user
*user_buffer
, size_t count
,
1187 struct usb_ftdi
*ftdi
= file
->private_data
;
1189 if (ftdi
->disconnected
> 0) {
1195 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1200 buf
= usb_buffer_alloc(ftdi
->udev
, count
, GFP_KERNEL
,
1201 &urb
->transfer_dma
);
1206 if (copy_from_user(buf
, user_buffer
, count
)) {
1210 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
1211 ftdi
->bulk_out_endpointAddr
), buf
, count
,
1212 ftdi_elan_write_bulk_callback
, ftdi
);
1213 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1214 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
1216 dev_err(&ftdi
->udev
->dev
, "failed submitting write urb, error %"
1225 usb_buffer_free(ftdi
->udev
, count
, buf
, urb
->transfer_dma
);
1232 static struct file_operations ftdi_elan_fops
= {
1233 .owner
= THIS_MODULE
,
1234 .llseek
= no_llseek
,
1235 .ioctl
= ftdi_elan_ioctl
,
1236 .read
= ftdi_elan_read
,
1237 .write
= ftdi_elan_write
,
1238 .open
= ftdi_elan_open
,
1239 .release
= ftdi_elan_release
,
1243 * usb class driver info in order to get a minor number from the usb core,
1244 * and to have the device registered with the driver core
1246 static struct usb_class_driver ftdi_elan_jtag_class
= {
1247 .name
= "ftdi-%d-jtag",
1248 .fops
= &ftdi_elan_fops
,
1249 .minor_base
= USB_FTDI_ELAN_MINOR_BASE
,
1253 * the following definitions are for the
1254 * ELAN FPGA state machgine processor that
1255 * lies on the other side of the FTDI chip
1257 #define cPCIu132rd 0x0
1258 #define cPCIu132wr 0x1
1259 #define cPCIiord 0x2
1260 #define cPCIiowr 0x3
1261 #define cPCImemrd 0x6
1262 #define cPCImemwr 0x7
1263 #define cPCIcfgrd 0xA
1264 #define cPCIcfgwr 0xB
1265 #define cPCInull 0xF
1266 #define cU132cmd_status 0x0
1267 #define cU132flash 0x1
1268 #define cPIDsetup 0x0
1271 #define cPIDinonce 0x3
1272 #define cCCnoerror 0x0
1274 #define cCCbitstuff 0x2
1275 #define cCCtoggle 0x3
1276 #define cCCstall 0x4
1277 #define cCCnoresp 0x5
1278 #define cCCbadpid1 0x6
1279 #define cCCbadpid2 0x7
1280 #define cCCdataoverrun 0x8
1281 #define cCCdataunderrun 0x9
1282 #define cCCbuffoverrun 0xC
1283 #define cCCbuffunderrun 0xD
1284 #define cCCnotaccessed 0xF
1285 static int ftdi_elan_write_reg(struct usb_ftdi
*ftdi
, u32 data
)
1287 wait
:if (ftdi
->disconnected
> 0) {
1291 down(&ftdi
->u132_lock
);
1292 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1293 if (command_size
< COMMAND_SIZE
) {
1294 struct u132_command
*command
= &ftdi
->command
[
1295 COMMAND_MASK
& ftdi
->command_next
];
1296 command
->header
= 0x00 | cPCIu132wr
;
1297 command
->length
= 0x04;
1298 command
->address
= 0x00;
1299 command
->width
= 0x00;
1300 command
->follows
= 4;
1301 command
->value
= data
;
1302 command
->buffer
= &command
->value
;
1303 ftdi
->command_next
+= 1;
1304 ftdi_elan_kick_command_queue(ftdi
);
1305 up(&ftdi
->u132_lock
);
1308 up(&ftdi
->u132_lock
);
1315 static int ftdi_elan_write_config(struct usb_ftdi
*ftdi
, int config_offset
,
1318 u8 addressofs
= config_offset
/ 4;
1319 wait
:if (ftdi
->disconnected
> 0) {
1323 down(&ftdi
->u132_lock
);
1324 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1325 if (command_size
< COMMAND_SIZE
) {
1326 struct u132_command
*command
= &ftdi
->command
[
1327 COMMAND_MASK
& ftdi
->command_next
];
1328 command
->header
= 0x00 | (cPCIcfgwr
& 0x0F);
1329 command
->length
= 0x04;
1330 command
->address
= addressofs
;
1331 command
->width
= 0x00 | (width
& 0x0F);
1332 command
->follows
= 4;
1333 command
->value
= data
;
1334 command
->buffer
= &command
->value
;
1335 ftdi
->command_next
+= 1;
1336 ftdi_elan_kick_command_queue(ftdi
);
1337 up(&ftdi
->u132_lock
);
1340 up(&ftdi
->u132_lock
);
1347 static int ftdi_elan_write_pcimem(struct usb_ftdi
*ftdi
, int mem_offset
,
1350 u8 addressofs
= mem_offset
/ 4;
1351 wait
:if (ftdi
->disconnected
> 0) {
1355 down(&ftdi
->u132_lock
);
1356 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1357 if (command_size
< COMMAND_SIZE
) {
1358 struct u132_command
*command
= &ftdi
->command
[
1359 COMMAND_MASK
& ftdi
->command_next
];
1360 command
->header
= 0x00 | (cPCImemwr
& 0x0F);
1361 command
->length
= 0x04;
1362 command
->address
= addressofs
;
1363 command
->width
= 0x00 | (width
& 0x0F);
1364 command
->follows
= 4;
1365 command
->value
= data
;
1366 command
->buffer
= &command
->value
;
1367 ftdi
->command_next
+= 1;
1368 ftdi_elan_kick_command_queue(ftdi
);
1369 up(&ftdi
->u132_lock
);
1372 up(&ftdi
->u132_lock
);
1379 int usb_ftdi_elan_write_pcimem(struct platform_device
*pdev
, int mem_offset
,
1382 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1383 return ftdi_elan_write_pcimem(ftdi
, mem_offset
, width
, data
);
1387 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem
);
1388 static int ftdi_elan_read_reg(struct usb_ftdi
*ftdi
, u32
*data
)
1390 wait
:if (ftdi
->disconnected
> 0) {
1395 down(&ftdi
->u132_lock
);
1396 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1397 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1398 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1400 struct u132_command
*command
= &ftdi
->command
[
1401 COMMAND_MASK
& ftdi
->command_next
];
1402 struct u132_respond
*respond
= &ftdi
->respond
[
1403 RESPOND_MASK
& ftdi
->respond_next
];
1404 int result
= -ENODEV
;
1405 respond
->result
= &result
;
1406 respond
->header
= command
->header
= 0x00 | cPCIu132rd
;
1407 command
->length
= 0x04;
1408 respond
->address
= command
->address
= cU132cmd_status
;
1409 command
->width
= 0x00;
1410 command
->follows
= 0;
1412 command
->buffer
= NULL
;
1413 respond
->value
= data
;
1414 init_completion(&respond
->wait_completion
);
1415 ftdi
->command_next
+= 1;
1416 ftdi
->respond_next
+= 1;
1417 ftdi_elan_kick_command_queue(ftdi
);
1418 up(&ftdi
->u132_lock
);
1419 wait_for_completion(&respond
->wait_completion
);
1422 up(&ftdi
->u132_lock
);
1429 int usb_ftdi_elan_read_reg(struct platform_device
*pdev
, u32
*data
)
1431 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1432 return ftdi_elan_read_reg(ftdi
, data
);
1436 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_reg
);
1437 static int ftdi_elan_read_config(struct usb_ftdi
*ftdi
, int config_offset
,
1438 u8 width
, u32
*data
)
1440 u8 addressofs
= config_offset
/ 4;
1441 wait
:if (ftdi
->disconnected
> 0) {
1446 down(&ftdi
->u132_lock
);
1447 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1448 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1449 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1451 struct u132_command
*command
= &ftdi
->command
[
1452 COMMAND_MASK
& ftdi
->command_next
];
1453 struct u132_respond
*respond
= &ftdi
->respond
[
1454 RESPOND_MASK
& ftdi
->respond_next
];
1455 int result
= -ENODEV
;
1456 respond
->result
= &result
;
1457 respond
->header
= command
->header
= 0x00 | (cPCIcfgrd
&
1459 command
->length
= 0x04;
1460 respond
->address
= command
->address
= addressofs
;
1461 command
->width
= 0x00 | (width
& 0x0F);
1462 command
->follows
= 0;
1464 command
->buffer
= NULL
;
1465 respond
->value
= data
;
1466 init_completion(&respond
->wait_completion
);
1467 ftdi
->command_next
+= 1;
1468 ftdi
->respond_next
+= 1;
1469 ftdi_elan_kick_command_queue(ftdi
);
1470 up(&ftdi
->u132_lock
);
1471 wait_for_completion(&respond
->wait_completion
);
1474 up(&ftdi
->u132_lock
);
1481 static int ftdi_elan_read_pcimem(struct usb_ftdi
*ftdi
, int mem_offset
,
1482 u8 width
, u32
*data
)
1484 u8 addressofs
= mem_offset
/ 4;
1485 wait
:if (ftdi
->disconnected
> 0) {
1490 down(&ftdi
->u132_lock
);
1491 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1492 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1493 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1495 struct u132_command
*command
= &ftdi
->command
[
1496 COMMAND_MASK
& ftdi
->command_next
];
1497 struct u132_respond
*respond
= &ftdi
->respond
[
1498 RESPOND_MASK
& ftdi
->respond_next
];
1499 int result
= -ENODEV
;
1500 respond
->result
= &result
;
1501 respond
->header
= command
->header
= 0x00 | (cPCImemrd
&
1503 command
->length
= 0x04;
1504 respond
->address
= command
->address
= addressofs
;
1505 command
->width
= 0x00 | (width
& 0x0F);
1506 command
->follows
= 0;
1508 command
->buffer
= NULL
;
1509 respond
->value
= data
;
1510 init_completion(&respond
->wait_completion
);
1511 ftdi
->command_next
+= 1;
1512 ftdi
->respond_next
+= 1;
1513 ftdi_elan_kick_command_queue(ftdi
);
1514 up(&ftdi
->u132_lock
);
1515 wait_for_completion(&respond
->wait_completion
);
1518 up(&ftdi
->u132_lock
);
1525 int usb_ftdi_elan_read_pcimem(struct platform_device
*pdev
, int mem_offset
,
1526 u8 width
, u32
*data
)
1528 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1529 if (ftdi
->initialized
== 0) {
1532 return ftdi_elan_read_pcimem(ftdi
, mem_offset
, width
, data
);
1536 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem
);
1537 static int ftdi_elan_edset_setup(struct usb_ftdi
*ftdi
, u8 ed_number
,
1538 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1539 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1540 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1541 int halted
, int skipped
, int actual
, int non_null
))
1543 u8 ed
= ed_number
- 1;
1544 wait
:if (ftdi
->disconnected
> 0) {
1546 } else if (ftdi
->initialized
== 0) {
1550 down(&ftdi
->u132_lock
);
1551 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1552 if (command_size
< COMMAND_SIZE
) {
1553 struct u132_target
*target
= &ftdi
->target
[ed
];
1554 struct u132_command
*command
= &ftdi
->command
[
1555 COMMAND_MASK
& ftdi
->command_next
];
1556 command
->header
= 0x80 | (ed
<< 5);
1557 command
->length
= 0x8007;
1558 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1560 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1561 usb_pipeout(urb
->pipe
));
1562 command
->follows
= 8;
1564 command
->buffer
= urb
->setup_packet
;
1565 target
->callback
= callback
;
1566 target
->endp
= endp
;
1569 ftdi
->command_next
+= 1;
1570 ftdi_elan_kick_command_queue(ftdi
);
1571 up(&ftdi
->u132_lock
);
1574 up(&ftdi
->u132_lock
);
1581 int usb_ftdi_elan_edset_setup(struct platform_device
*pdev
, u8 ed_number
,
1582 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1583 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1584 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1585 int halted
, int skipped
, int actual
, int non_null
))
1587 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1588 return ftdi_elan_edset_setup(ftdi
, ed_number
, endp
, urb
, address
,
1589 ep_number
, toggle_bits
, callback
);
1593 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup
);
1594 static int ftdi_elan_edset_input(struct usb_ftdi
*ftdi
, u8 ed_number
,
1595 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1596 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1597 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1598 int halted
, int skipped
, int actual
, int non_null
))
1600 u8 ed
= ed_number
- 1;
1601 wait
:if (ftdi
->disconnected
> 0) {
1603 } else if (ftdi
->initialized
== 0) {
1607 down(&ftdi
->u132_lock
);
1608 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1609 if (command_size
< COMMAND_SIZE
) {
1610 struct u132_target
*target
= &ftdi
->target
[ed
];
1611 struct u132_command
*command
= &ftdi
->command
[
1612 COMMAND_MASK
& ftdi
->command_next
];
1613 int remaining_length
= urb
->transfer_buffer_length
-
1615 command
->header
= 0x82 | (ed
<< 5);
1616 if (remaining_length
== 0) {
1617 command
->length
= 0x0000;
1618 } else if (remaining_length
> 1024) {
1619 command
->length
= 0x8000 | 1023;
1621 command
->length
= 0x8000 | (remaining_length
-
1623 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1625 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1626 usb_pipeout(urb
->pipe
));
1627 command
->follows
= 0;
1629 command
->buffer
= NULL
;
1630 target
->callback
= callback
;
1631 target
->endp
= endp
;
1634 ftdi
->command_next
+= 1;
1635 ftdi_elan_kick_command_queue(ftdi
);
1636 up(&ftdi
->u132_lock
);
1639 up(&ftdi
->u132_lock
);
1646 int usb_ftdi_elan_edset_input(struct platform_device
*pdev
, u8 ed_number
,
1647 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1648 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1649 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1650 int halted
, int skipped
, int actual
, int non_null
))
1652 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1653 return ftdi_elan_edset_input(ftdi
, ed_number
, endp
, urb
, address
,
1654 ep_number
, toggle_bits
, callback
);
1658 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input
);
1659 static int ftdi_elan_edset_empty(struct usb_ftdi
*ftdi
, u8 ed_number
,
1660 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1661 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1662 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1663 int halted
, int skipped
, int actual
, int non_null
))
1665 u8 ed
= ed_number
- 1;
1666 wait
:if (ftdi
->disconnected
> 0) {
1668 } else if (ftdi
->initialized
== 0) {
1672 down(&ftdi
->u132_lock
);
1673 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1674 if (command_size
< COMMAND_SIZE
) {
1675 struct u132_target
*target
= &ftdi
->target
[ed
];
1676 struct u132_command
*command
= &ftdi
->command
[
1677 COMMAND_MASK
& ftdi
->command_next
];
1678 command
->header
= 0x81 | (ed
<< 5);
1679 command
->length
= 0x0000;
1680 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1682 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1683 usb_pipeout(urb
->pipe
));
1684 command
->follows
= 0;
1686 command
->buffer
= NULL
;
1687 target
->callback
= callback
;
1688 target
->endp
= endp
;
1691 ftdi
->command_next
+= 1;
1692 ftdi_elan_kick_command_queue(ftdi
);
1693 up(&ftdi
->u132_lock
);
1696 up(&ftdi
->u132_lock
);
1703 int usb_ftdi_elan_edset_empty(struct platform_device
*pdev
, u8 ed_number
,
1704 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1705 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1706 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1707 int halted
, int skipped
, int actual
, int non_null
))
1709 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1710 return ftdi_elan_edset_empty(ftdi
, ed_number
, endp
, urb
, address
,
1711 ep_number
, toggle_bits
, callback
);
1715 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty
);
1716 static int ftdi_elan_edset_output(struct usb_ftdi
*ftdi
, u8 ed_number
,
1717 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1718 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1719 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1720 int halted
, int skipped
, int actual
, int non_null
))
1722 u8 ed
= ed_number
- 1;
1723 wait
:if (ftdi
->disconnected
> 0) {
1725 } else if (ftdi
->initialized
== 0) {
1729 down(&ftdi
->u132_lock
);
1730 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1731 if (command_size
< COMMAND_SIZE
) {
1735 char data
[30 *3 + 4];
1737 int m
= (sizeof(data
) - 1) / 3;
1739 struct u132_target
*target
= &ftdi
->target
[ed
];
1740 struct u132_command
*command
= &ftdi
->command
[
1741 COMMAND_MASK
& ftdi
->command_next
];
1742 command
->header
= 0x81 | (ed
<< 5);
1743 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1745 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1746 usb_pipeout(urb
->pipe
));
1747 command
->follows
= min(1024,
1748 urb
->transfer_buffer_length
-
1749 urb
->actual_length
);
1751 command
->buffer
= urb
->transfer_buffer
+
1753 command
->length
= 0x8000 | (command
->follows
- 1);
1754 b
= command
->buffer
;
1755 urb_size
= command
->follows
;
1757 while (urb_size
-- > 0) {
1759 } else if (i
++ < m
) {
1760 int w
= sprintf(d
, " %02X", *b
++);
1764 d
+= sprintf(d
, " ..");
1766 target
->callback
= callback
;
1767 target
->endp
= endp
;
1770 ftdi
->command_next
+= 1;
1771 ftdi_elan_kick_command_queue(ftdi
);
1772 up(&ftdi
->u132_lock
);
1775 up(&ftdi
->u132_lock
);
1782 int usb_ftdi_elan_edset_output(struct platform_device
*pdev
, u8 ed_number
,
1783 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1784 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1785 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1786 int halted
, int skipped
, int actual
, int non_null
))
1788 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1789 return ftdi_elan_edset_output(ftdi
, ed_number
, endp
, urb
, address
,
1790 ep_number
, toggle_bits
, callback
);
1794 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output
);
1795 static int ftdi_elan_edset_single(struct usb_ftdi
*ftdi
, u8 ed_number
,
1796 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1797 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1798 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1799 int halted
, int skipped
, int actual
, int non_null
))
1801 u8 ed
= ed_number
- 1;
1802 wait
:if (ftdi
->disconnected
> 0) {
1804 } else if (ftdi
->initialized
== 0) {
1808 down(&ftdi
->u132_lock
);
1809 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1810 if (command_size
< COMMAND_SIZE
) {
1811 int remaining_length
= urb
->transfer_buffer_length
-
1813 struct u132_target
*target
= &ftdi
->target
[ed
];
1814 struct u132_command
*command
= &ftdi
->command
[
1815 COMMAND_MASK
& ftdi
->command_next
];
1816 command
->header
= 0x83 | (ed
<< 5);
1817 if (remaining_length
== 0) {
1818 command
->length
= 0x0000;
1819 } else if (remaining_length
> 1024) {
1820 command
->length
= 0x8000 | 1023;
1822 command
->length
= 0x8000 | (remaining_length
-
1824 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1826 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1827 usb_pipeout(urb
->pipe
));
1828 command
->follows
= 0;
1830 command
->buffer
= NULL
;
1831 target
->callback
= callback
;
1832 target
->endp
= endp
;
1835 ftdi
->command_next
+= 1;
1836 ftdi_elan_kick_command_queue(ftdi
);
1837 up(&ftdi
->u132_lock
);
1840 up(&ftdi
->u132_lock
);
1847 int usb_ftdi_elan_edset_single(struct platform_device
*pdev
, u8 ed_number
,
1848 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1849 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1850 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1851 int halted
, int skipped
, int actual
, int non_null
))
1853 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1854 return ftdi_elan_edset_single(ftdi
, ed_number
, endp
, urb
, address
,
1855 ep_number
, toggle_bits
, callback
);
1859 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single
);
1860 static int ftdi_elan_edset_flush(struct usb_ftdi
*ftdi
, u8 ed_number
,
1863 u8 ed
= ed_number
- 1;
1864 if (ftdi
->disconnected
> 0) {
1866 } else if (ftdi
->initialized
== 0) {
1869 struct u132_target
*target
= &ftdi
->target
[ed
];
1870 down(&ftdi
->u132_lock
);
1871 if (target
->abandoning
> 0) {
1872 up(&ftdi
->u132_lock
);
1875 target
->abandoning
= 1;
1876 wait_1
:if (target
->active
== 1) {
1877 int command_size
= ftdi
->command_next
-
1879 if (command_size
< COMMAND_SIZE
) {
1880 struct u132_command
*command
=
1881 &ftdi
->command
[COMMAND_MASK
&
1882 ftdi
->command_next
];
1883 command
->header
= 0x80 | (ed
<< 5) |
1885 command
->length
= 0x00;
1886 command
->address
= 0x00;
1887 command
->width
= 0x00;
1888 command
->follows
= 0;
1890 command
->buffer
= &command
->value
;
1891 ftdi
->command_next
+= 1;
1892 ftdi_elan_kick_command_queue(ftdi
);
1894 up(&ftdi
->u132_lock
);
1896 down(&ftdi
->u132_lock
);
1900 up(&ftdi
->u132_lock
);
1906 int usb_ftdi_elan_edset_flush(struct platform_device
*pdev
, u8 ed_number
,
1909 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1910 return ftdi_elan_edset_flush(ftdi
, ed_number
, endp
);
1914 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush
);
1915 static int ftdi_elan_flush_input_fifo(struct usb_ftdi
*ftdi
)
1917 int retry_on_empty
= 10;
1918 int retry_on_timeout
= 5;
1919 int retry_on_status
= 20;
1921 int packet_bytes
= 0;
1922 int retval
= usb_bulk_msg(ftdi
->udev
,
1923 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
1924 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
1925 &packet_bytes
, msecs_to_jiffies(100));
1926 if (packet_bytes
> 2) {
1927 char diag
[30 *3 + 4];
1929 int m
= (sizeof(diag
) - 1) / 3;
1930 char *b
= ftdi
->bulk_in_buffer
;
1933 while (packet_bytes
-- > 0) {
1935 if (bytes_read
< m
) {
1936 d
+= sprintf(d
, " %02X",
1938 } else if (bytes_read
> m
) {
1940 d
+= sprintf(d
, " ..");
1945 } else if (packet_bytes
> 1) {
1946 char s1
= ftdi
->bulk_in_buffer
[0];
1947 char s2
= ftdi
->bulk_in_buffer
[1];
1948 if (s1
== 0x31 && s2
== 0x60) {
1950 } else if (retry_on_status
-- > 0) {
1953 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry l"
1957 } else if (packet_bytes
> 0) {
1958 char b1
= ftdi
->bulk_in_buffer
[0];
1959 dev_err(&ftdi
->udev
->dev
, "only one byte flushed from F"
1960 "TDI = %02X\n", b1
);
1961 if (retry_on_status
-- > 0) {
1964 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry l"
1968 } else if (retval
== -ETIMEDOUT
) {
1969 if (retry_on_timeout
-- > 0) {
1972 dev_err(&ftdi
->udev
->dev
, "TIMED OUT retry limi"
1976 } else if (retval
== 0) {
1977 if (retry_on_empty
-- > 0) {
1980 dev_err(&ftdi
->udev
->dev
, "empty packet retry l"
1985 dev_err(&ftdi
->udev
->dev
, "error = %d\n", retval
);
1994 * send the long flush sequence
1997 static int ftdi_elan_synchronize_flush(struct usb_ftdi
*ftdi
)
2004 urb
= usb_alloc_urb(0, GFP_KERNEL
);
2006 dev_err(&ftdi
->udev
->dev
, "could not alloc a urb for flush sequ"
2010 buf
= usb_buffer_alloc(ftdi
->udev
, I
, GFP_KERNEL
, &urb
->transfer_dma
);
2012 dev_err(&ftdi
->udev
->dev
, "could not get a buffer for flush seq"
2019 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
2020 ftdi
->bulk_out_endpointAddr
), buf
, i
,
2021 ftdi_elan_write_bulk_callback
, ftdi
);
2022 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
2023 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
2025 dev_err(&ftdi
->udev
->dev
, "failed to submit urb containing the "
2026 "flush sequence\n");
2027 usb_buffer_free(ftdi
->udev
, i
, buf
, urb
->transfer_dma
);
2037 * send the reset sequence
2040 static int ftdi_elan_synchronize_reset(struct usb_ftdi
*ftdi
)
2047 urb
= usb_alloc_urb(0, GFP_KERNEL
);
2049 dev_err(&ftdi
->udev
->dev
, "could not get a urb for the reset se"
2053 buf
= usb_buffer_alloc(ftdi
->udev
, I
, GFP_KERNEL
, &urb
->transfer_dma
);
2055 dev_err(&ftdi
->udev
->dev
, "could not get a buffer for the reset"
2064 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
2065 ftdi
->bulk_out_endpointAddr
), buf
, i
,
2066 ftdi_elan_write_bulk_callback
, ftdi
);
2067 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
2068 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
2070 dev_err(&ftdi
->udev
->dev
, "failed to submit urb containing the "
2071 "reset sequence\n");
2072 usb_buffer_free(ftdi
->udev
, i
, buf
, urb
->transfer_dma
);
2080 static int ftdi_elan_synchronize(struct usb_ftdi
*ftdi
)
2084 int retry_on_timeout
= 5;
2085 int retry_on_empty
= 10;
2087 retval
= ftdi_elan_flush_input_fifo(ftdi
);
2090 ftdi
->bulk_in_left
= 0;
2091 ftdi
->bulk_in_last
= -1;
2092 while (long_stop
-- > 0) {
2095 retval
= ftdi_elan_synchronize_flush(ftdi
);
2098 retval
= ftdi_elan_flush_input_fifo(ftdi
);
2101 reset
:retval
= ftdi_elan_synchronize_reset(ftdi
);
2107 int packet_bytes
= 0;
2108 retval
= usb_bulk_msg(ftdi
->udev
,
2109 usb_rcvbulkpipe(ftdi
->udev
,
2110 ftdi
->bulk_in_endpointAddr
),
2111 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
2112 &packet_bytes
, msecs_to_jiffies(500));
2113 if (packet_bytes
> 2) {
2114 char diag
[30 *3 + 4];
2116 int m
= (sizeof(diag
) - 1) / 3;
2117 char *b
= ftdi
->bulk_in_buffer
;
2119 unsigned char c
= 0;
2121 while (packet_bytes
-- > 0) {
2123 if (bytes_read
< m
) {
2124 d
+= sprintf(d
, " %02X", c
);
2125 } else if (bytes_read
> m
) {
2127 d
+= sprintf(d
, " ..");
2136 } else if (read_stop
-- > 0) {
2139 dev_err(&ftdi
->udev
->dev
, "retr"
2140 "y limit reached\n");
2144 } else if (packet_bytes
> 1) {
2145 unsigned char s1
= ftdi
->bulk_in_buffer
[0];
2146 unsigned char s2
= ftdi
->bulk_in_buffer
[1];
2147 if (s1
== 0x31 && s2
== 0x00) {
2148 if (read_stuck
-- > 0) {
2152 } else if (s1
== 0x31 && s2
== 0x60) {
2153 if (read_stop
-- > 0) {
2156 dev_err(&ftdi
->udev
->dev
, "retr"
2157 "y limit reached\n");
2161 if (read_stop
-- > 0) {
2164 dev_err(&ftdi
->udev
->dev
, "retr"
2165 "y limit reached\n");
2169 } else if (packet_bytes
> 0) {
2170 if (read_stop
-- > 0) {
2173 dev_err(&ftdi
->udev
->dev
, "retry limit "
2177 } else if (retval
== -ETIMEDOUT
) {
2178 if (retry_on_timeout
-- > 0) {
2181 dev_err(&ftdi
->udev
->dev
, "TIMED OUT re"
2182 "try limit reached\n");
2185 } else if (retval
== 0) {
2186 if (retry_on_empty
-- > 0) {
2189 dev_err(&ftdi
->udev
->dev
, "empty packet"
2190 " retry limit reached\n");
2195 dev_err(&ftdi
->udev
->dev
, "error = %d\n",
2197 if (read_stop
-- > 0) {
2200 dev_err(&ftdi
->udev
->dev
, "retry limit "
2207 dev_err(&ftdi
->udev
->dev
, "failed to synchronize\n");
2211 static int ftdi_elan_stuck_waiting(struct usb_ftdi
*ftdi
)
2213 int retry_on_empty
= 10;
2214 int retry_on_timeout
= 5;
2215 int retry_on_status
= 50;
2217 int packet_bytes
= 0;
2218 int retval
= usb_bulk_msg(ftdi
->udev
,
2219 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
2220 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
2221 &packet_bytes
, msecs_to_jiffies(1000));
2222 if (packet_bytes
> 2) {
2223 char diag
[30 *3 + 4];
2225 int m
= (sizeof(diag
) - 1) / 3;
2226 char *b
= ftdi
->bulk_in_buffer
;
2229 while (packet_bytes
-- > 0) {
2231 if (bytes_read
< m
) {
2232 d
+= sprintf(d
, " %02X",
2234 } else if (bytes_read
> m
) {
2236 d
+= sprintf(d
, " ..");
2241 } else if (packet_bytes
> 1) {
2242 char s1
= ftdi
->bulk_in_buffer
[0];
2243 char s2
= ftdi
->bulk_in_buffer
[1];
2244 if (s1
== 0x31 && s2
== 0x60) {
2246 } else if (retry_on_status
-- > 0) {
2251 } else if (packet_bytes
> 0) {
2252 char b1
= ftdi
->bulk_in_buffer
[0];
2253 dev_err(&ftdi
->udev
->dev
, "only one byte flushed from F"
2254 "TDI = %02X\n", b1
);
2255 if (retry_on_status
-- > 0) {
2259 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry l"
2263 } else if (retval
== -ETIMEDOUT
) {
2264 if (retry_on_timeout
-- > 0) {
2267 dev_err(&ftdi
->udev
->dev
, "TIMED OUT retry limi"
2271 } else if (retval
== 0) {
2272 if (retry_on_empty
-- > 0) {
2275 dev_err(&ftdi
->udev
->dev
, "empty packet retry l"
2280 dev_err(&ftdi
->udev
->dev
, "error = %d\n", retval
);
2287 static int ftdi_elan_checkingPCI(struct usb_ftdi
*ftdi
)
2289 int UxxxStatus
= ftdi_elan_read_reg(ftdi
, &ftdi
->controlreg
);
2292 if (ftdi
->controlreg
& 0x00400000) {
2293 if (ftdi
->card_ejected
) {
2295 ftdi
->card_ejected
= 1;
2296 dev_err(&ftdi
->udev
->dev
, "CARD EJECTED - controlreg = "
2297 "%08X\n", ftdi
->controlreg
);
2301 u8 fn
= ftdi
->function
- 1;
2302 int activePCIfn
= fn
<< 8;
2307 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2311 pciVID
= pcidata
& 0xFFFF;
2312 pciPID
= (pcidata
>> 16) & 0xFFFF;
2313 if (pciVID
== ftdi
->platform_data
.vendor
&& pciPID
==
2314 ftdi
->platform_data
.device
) {
2317 dev_err(&ftdi
->udev
->dev
, "vendor=%04X pciVID=%04X devi"
2318 "ce=%04X pciPID=%04X\n",
2319 ftdi
->platform_data
.vendor
, pciVID
,
2320 ftdi
->platform_data
.device
, pciPID
);
2326 static int ftdi_elan_enumeratePCI(struct usb_ftdi
*ftdi
)
2335 int activePCIfn
= 0;
2338 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2341 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000000L
);
2345 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000200L
| 0x100);
2348 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000200L
| 0x500);
2351 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2354 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020CL
| 0x000);
2357 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020DL
| 0x000);
2361 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020FL
| 0x000);
2364 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2367 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000025FL
| 0x800);
2370 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2373 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2377 for (fn
= 0; (fn
< 4) && (!foundOHCI
); fn
++) {
2378 activePCIfn
= fn
<< 8;
2379 ftdi
->function
= fn
+ 1;
2380 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2384 pciVID
= pcidata
& 0xFFFF;
2385 pciPID
= (pcidata
>> 16) & 0xFFFF;
2386 if ((pciVID
== 0x1045) && (pciPID
== 0xc861)) {
2388 } else if ((pciVID
== 0x1033) && (pciPID
== 0x0035)) {
2390 } else if ((pciVID
== 0x10b9) && (pciPID
== 0x5237)) {
2392 } else if ((pciVID
== 0x11c1) && (pciPID
== 0x5802)) {
2394 } else if ((pciVID
== 0x11AB) && (pciPID
== 0x1FA6)) {
2397 if (foundOHCI
== 0) {
2400 ftdi
->platform_data
.vendor
= pciVID
;
2401 ftdi
->platform_data
.device
= pciPID
;
2402 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000025FL
| 0x2800);
2406 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2410 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2414 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2418 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2423 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2427 latence_timer
&= 0xFFFF00FF;
2428 latence_timer
|= 0x00001600;
2429 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2433 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2438 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2442 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2449 static int ftdi_elan_setupOHCI(struct usb_ftdi
*ftdi
)
2454 int reset_repeat
= 0;
2456 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x0e, 0x01);
2460 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2465 if (reset_repeat
++ > 100) {
2475 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x11000000);
2478 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2482 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x2edf);
2485 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2489 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x2edf2edf);
2492 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2496 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0xA0);
2499 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2504 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x0e, 0x04);
2507 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2511 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2515 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2519 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x00001200);
2522 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2526 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2530 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2534 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x28002edf);
2537 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2542 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x10000);
2546 power_check
:U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2549 if (!(pcidata
& 1)) {
2555 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2559 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2563 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x02);
2566 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2570 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x10);
2573 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2579 U132Status
= ftdi_elan_write_pcimem(ftdi
, reg
, 0x00, 0x02);
2584 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2589 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2593 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2596 dump_regs
:for (reg
= 0; reg
<= 0x54; reg
+= 4) {
2597 U132Status
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2606 * we use only the first bulk-in and bulk-out endpoints
2608 static int ftdi_elan_probe(struct usb_interface
*interface
,
2609 const struct usb_device_id
*id
)
2611 struct usb_host_interface
*iface_desc
;
2612 struct usb_endpoint_descriptor
*endpoint
;
2615 int retval
= -ENOMEM
;
2616 struct usb_ftdi
*ftdi
= kmalloc(sizeof(struct usb_ftdi
), GFP_KERNEL
);
2618 printk(KERN_ERR
"Out of memory\n");
2621 memset(ftdi
, 0x00, sizeof(struct usb_ftdi
));
2622 down(&ftdi_module_lock
);
2623 list_add_tail(&ftdi
->ftdi_list
, &ftdi_static_list
);
2624 ftdi
->sequence_num
= ++ftdi_instances
;
2625 up(&ftdi_module_lock
);
2626 ftdi_elan_init_kref(ftdi
);
2627 init_MUTEX(&ftdi
->sw_lock
);
2628 ftdi
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2629 ftdi
->interface
= interface
;
2630 init_MUTEX(&ftdi
->u132_lock
);
2632 iface_desc
= interface
->cur_altsetting
;
2633 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2634 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2635 if (!ftdi
->bulk_in_endpointAddr
&&
2636 ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
2637 == USB_DIR_IN
) && ((endpoint
->bmAttributes
&
2638 USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_BULK
))
2640 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
2641 ftdi
->bulk_in_size
= buffer_size
;
2642 ftdi
->bulk_in_endpointAddr
= endpoint
->bEndpointAddress
;
2643 ftdi
->bulk_in_buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
2644 if (!ftdi
->bulk_in_buffer
) {
2645 dev_err(&ftdi
->udev
->dev
, "Could not allocate b"
2651 if (!ftdi
->bulk_out_endpointAddr
&&
2652 ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
2653 == USB_DIR_OUT
) && ((endpoint
->bmAttributes
&
2654 USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_BULK
))
2656 ftdi
->bulk_out_endpointAddr
=
2657 endpoint
->bEndpointAddress
;
2660 if (!(ftdi
->bulk_in_endpointAddr
&& ftdi
->bulk_out_endpointAddr
)) {
2661 dev_err(&ftdi
->udev
->dev
, "Could not find both bulk-in and bulk"
2662 "-out endpoints\n");
2666 dev_info(&ftdi
->udev
->dev
, "interface %d has I=%02X O=%02X\n",
2667 iface_desc
->desc
.bInterfaceNumber
, ftdi
->bulk_in_endpointAddr
,
2668 ftdi
->bulk_out_endpointAddr
);
2669 usb_set_intfdata(interface
, ftdi
);
2670 if (iface_desc
->desc
.bInterfaceNumber
== 0 &&
2671 ftdi
->bulk_in_endpointAddr
== 0x81 &&
2672 ftdi
->bulk_out_endpointAddr
== 0x02) {
2673 retval
= usb_register_dev(interface
, &ftdi_elan_jtag_class
);
2675 dev_err(&ftdi
->udev
->dev
, "Not able to get a minor for "
2677 usb_set_intfdata(interface
, NULL
);
2681 ftdi
->class = &ftdi_elan_jtag_class
;
2682 dev_info(&ftdi
->udev
->dev
, "USB FDTI=%p JTAG interface "
2683 "%d now attached to ftdi%d\n", ftdi
,
2684 iface_desc
->desc
.bInterfaceNumber
,
2688 } else if (iface_desc
->desc
.bInterfaceNumber
== 1 &&
2689 ftdi
->bulk_in_endpointAddr
== 0x83 &&
2690 ftdi
->bulk_out_endpointAddr
== 0x04) {
2692 dev_info(&ftdi
->udev
->dev
, "USB FDTI=%p ELAN interface %d now a"
2693 "ctivated\n", ftdi
, iface_desc
->desc
.bInterfaceNumber
);
2694 INIT_WORK(&ftdi
->status_work
, ftdi_elan_status_work
,
2696 INIT_WORK(&ftdi
->command_work
, ftdi_elan_command_work
,
2698 INIT_WORK(&ftdi
->respond_work
, ftdi_elan_respond_work
,
2700 ftdi_status_queue_work(ftdi
, msecs_to_jiffies(3 *1000));
2703 dev_err(&ftdi
->udev
->dev
,
2704 "Could not find ELAN's U132 device\n");
2709 ftdi_elan_put_kref(ftdi
);
2714 static void ftdi_elan_disconnect(struct usb_interface
*interface
)
2716 struct usb_ftdi
*ftdi
= usb_get_intfdata(interface
);
2717 ftdi
->disconnected
+= 1;
2719 int minor
= interface
->minor
;
2720 struct usb_class_driver
*class = ftdi
->class;
2721 usb_set_intfdata(interface
, NULL
);
2722 usb_deregister_dev(interface
, class);
2723 dev_info(&ftdi
->udev
->dev
, "USB FTDI U132 jtag interface on min"
2724 "or %d now disconnected\n", minor
);
2726 ftdi_status_cancel_work(ftdi
);
2727 ftdi_command_cancel_work(ftdi
);
2728 ftdi_response_cancel_work(ftdi
);
2729 ftdi_elan_abandon_completions(ftdi
);
2730 ftdi_elan_abandon_targets(ftdi
);
2731 if (ftdi
->registered
) {
2732 platform_device_unregister(&ftdi
->platform_dev
);
2733 ftdi
->synchronized
= 0;
2734 ftdi
->enumerated
= 0;
2735 ftdi
->registered
= 0;
2737 flush_workqueue(status_queue
);
2738 flush_workqueue(command_queue
);
2739 flush_workqueue(respond_queue
);
2740 ftdi
->disconnected
+= 1;
2741 usb_set_intfdata(interface
, NULL
);
2742 dev_info(&ftdi
->udev
->dev
, "USB FTDI U132 host controller inter"
2743 "face now disconnected\n");
2745 ftdi_elan_put_kref(ftdi
);
2748 static struct usb_driver ftdi_elan_driver
= {
2749 .name
= "ftdi-elan",
2750 .probe
= ftdi_elan_probe
,
2751 .disconnect
= ftdi_elan_disconnect
,
2752 .id_table
= ftdi_elan_table
,
2754 static int __init
ftdi_elan_init(void)
2757 printk(KERN_INFO
"driver %s built at %s on %s\n", ftdi_elan_driver
.name
,
2758 __TIME__
, __DATE__
);
2759 init_MUTEX(&ftdi_module_lock
);
2760 INIT_LIST_HEAD(&ftdi_static_list
);
2761 status_queue
= create_singlethread_workqueue("ftdi-status-control");
2762 command_queue
= create_singlethread_workqueue("ftdi-command-engine");
2763 respond_queue
= create_singlethread_workqueue("ftdi-respond-engine");
2764 result
= usb_register(&ftdi_elan_driver
);
2766 printk(KERN_ERR
"usb_register failed. Error number %d\n",
2771 static void __exit
ftdi_elan_exit(void)
2773 struct usb_ftdi
*ftdi
;
2774 struct usb_ftdi
*temp
;
2775 usb_deregister(&ftdi_elan_driver
);
2776 printk(KERN_INFO
"ftdi_u132 driver deregistered\n");
2777 list_for_each_entry_safe(ftdi
, temp
, &ftdi_static_list
, ftdi_list
) {
2778 ftdi_status_cancel_work(ftdi
);
2779 ftdi_command_cancel_work(ftdi
);
2780 ftdi_response_cancel_work(ftdi
);
2781 } flush_workqueue(status_queue
);
2782 destroy_workqueue(status_queue
);
2783 status_queue
= NULL
;
2784 flush_workqueue(command_queue
);
2785 destroy_workqueue(command_queue
);
2786 command_queue
= NULL
;
2787 flush_workqueue(respond_queue
);
2788 destroy_workqueue(respond_queue
);
2789 respond_queue
= NULL
;
2793 module_init(ftdi_elan_init
);
2794 module_exit(ftdi_elan_exit
);