1 // SPDX-License-Identifier: GPL-2.0
3 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
5 * Copyright(C) 2006 Elan Digital Systems Limited
6 * http://www.elandigitalsystems.com
8 * Author and Maintainer - Tony Olech - Elan Digital Systems
9 * tony.olech@elandigitalsystems.com
11 * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
12 * based on various USB client drivers in the 2.6.15 linux kernel
13 * with constant reference to the 3rd Edition of Linux Device Drivers
14 * published by O'Reilly
16 * The U132 adapter is a USB to CardBus adapter specifically designed
17 * for PC cards that contain an OHCI host controller. Typical PC cards
18 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
20 * The U132 adapter will *NOT *work with PC cards that do not contain
21 * an OHCI controller. A simple way to test whether a PC card has an
22 * OHCI controller as an interface is to insert the PC card directly
23 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
24 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
25 * then there is a good chance that the U132 adapter will support the
26 * PC card.(you also need the specific client driver for the PC card)
28 * Please inform the Author and Maintainer about any PC cards that
29 * contain OHCI Host Controller and work when directly connected to
30 * an embedded CardBus slot but do not work when they are connected
31 * via an ELAN U132 adapter.
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <linux/ioctl.h>
42 #include <linux/pci_ids.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/kref.h>
46 #include <linux/mutex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/workqueue.h>
50 #include <linux/platform_device.h>
51 MODULE_AUTHOR("Tony Olech");
52 MODULE_DESCRIPTION("FTDI ELAN driver");
53 MODULE_LICENSE("GPL");
54 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
55 static bool distrust_firmware
= 1;
56 module_param(distrust_firmware
, bool, 0);
57 MODULE_PARM_DESC(distrust_firmware
,
58 "true to distrust firmware power/overcurrent setup");
59 extern struct platform_driver u132_platform_driver
;
61 * ftdi_module_lock exists to protect access to global variables
64 static struct mutex ftdi_module_lock
;
65 static int ftdi_instances
= 0;
66 static struct list_head ftdi_static_list
;
68 * end of the global variables protected by ftdi_module_lock
72 #include <linux/usb/hcd.h>
74 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
75 * If you're going to try stuff like this, you need to split
76 * out shareable stuff (register declarations?) into its own
77 * file, maybe name <linux/usb/ohci.h>
80 #include "../host/ohci.h"
81 /* Define these values to match your devices*/
82 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
83 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
84 /* table of devices that work with this driver*/
85 static const struct usb_device_id ftdi_elan_table
[] = {
86 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID
, USB_FTDI_ELAN_PRODUCT_ID
)},
87 { /* Terminating entry */ }
90 MODULE_DEVICE_TABLE(usb
, ftdi_elan_table
);
91 /* only the jtag(firmware upgrade device) interface requires
92 * a device file and corresponding minor number, but the
93 * interface is created unconditionally - I suppose it could
94 * be configured or not according to a module parameter.
95 * But since we(now) require one interface per device,
96 * and since it unlikely that a normal installation would
97 * require more than a couple of elan-ftdi devices, 8 seems
98 * like a reasonable limit to have here, and if someone
99 * really requires more than 8 devices, then they can frig the
102 #define USB_FTDI_ELAN_MINOR_BASE 192
103 #define COMMAND_BITS 5
104 #define COMMAND_SIZE (1<<COMMAND_BITS)
105 #define COMMAND_MASK (COMMAND_SIZE-1)
106 struct u132_command
{
115 #define RESPOND_BITS 5
116 #define RESPOND_SIZE (1<<RESPOND_BITS)
117 #define RESPOND_MASK (RESPOND_SIZE-1)
118 struct u132_respond
{
123 struct completion wait_completion
;
138 void (*callback
)(void *endp
, struct urb
*urb
, u8
*buf
, int len
,
139 int toggle_bits
, int error_count
, int condition_code
,
140 int repeat_number
, int halted
, int skipped
, int actual
,
143 /* Structure to hold all of our device specific stuff*/
145 struct list_head ftdi_list
;
146 struct mutex u132_lock
;
149 struct u132_command command
[COMMAND_SIZE
];
152 struct u132_respond respond
[RESPOND_SIZE
];
153 struct u132_target target
[4];
154 char device_name
[16];
155 unsigned synchronized
:1;
156 unsigned enumerated
:1;
157 unsigned registered
:1;
158 unsigned initialized
:1;
159 unsigned card_ejected
:1;
165 int status_queue_delay
;
166 struct semaphore sw_lock
;
167 struct usb_device
*udev
;
168 struct usb_interface
*interface
;
169 struct usb_class_driver
*class;
170 struct delayed_work status_work
;
171 struct delayed_work command_work
;
172 struct delayed_work respond_work
;
173 struct u132_platform_data platform_data
;
174 struct resource resources
[0];
175 struct platform_device platform_dev
;
176 unsigned char *bulk_in_buffer
;
180 __u8 bulk_in_endpointAddr
;
181 __u8 bulk_out_endpointAddr
;
184 u8 response
[4 + 1024];
189 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
190 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
192 static struct usb_driver ftdi_elan_driver
;
193 static void ftdi_elan_delete(struct kref
*kref
)
195 struct usb_ftdi
*ftdi
= kref_to_usb_ftdi(kref
);
196 dev_warn(&ftdi
->udev
->dev
, "FREEING ftdi=%p\n", ftdi
);
197 usb_put_dev(ftdi
->udev
);
198 ftdi
->disconnected
+= 1;
199 mutex_lock(&ftdi_module_lock
);
200 list_del_init(&ftdi
->ftdi_list
);
202 mutex_unlock(&ftdi_module_lock
);
203 kfree(ftdi
->bulk_in_buffer
);
204 ftdi
->bulk_in_buffer
= NULL
;
207 static void ftdi_elan_put_kref(struct usb_ftdi
*ftdi
)
209 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
212 static void ftdi_elan_get_kref(struct usb_ftdi
*ftdi
)
214 kref_get(&ftdi
->kref
);
217 static void ftdi_elan_init_kref(struct usb_ftdi
*ftdi
)
219 kref_init(&ftdi
->kref
);
222 static void ftdi_status_requeue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
224 if (!schedule_delayed_work(&ftdi
->status_work
, delta
))
225 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
228 static void ftdi_status_queue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
230 if (schedule_delayed_work(&ftdi
->status_work
, delta
))
231 kref_get(&ftdi
->kref
);
234 static void ftdi_status_cancel_work(struct usb_ftdi
*ftdi
)
236 if (cancel_delayed_work_sync(&ftdi
->status_work
))
237 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
240 static void ftdi_command_requeue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
242 if (!schedule_delayed_work(&ftdi
->command_work
, delta
))
243 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
246 static void ftdi_command_queue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
248 if (schedule_delayed_work(&ftdi
->command_work
, delta
))
249 kref_get(&ftdi
->kref
);
252 static void ftdi_command_cancel_work(struct usb_ftdi
*ftdi
)
254 if (cancel_delayed_work_sync(&ftdi
->command_work
))
255 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
258 static void ftdi_response_requeue_work(struct usb_ftdi
*ftdi
,
261 if (!schedule_delayed_work(&ftdi
->respond_work
, delta
))
262 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
265 static void ftdi_respond_queue_work(struct usb_ftdi
*ftdi
, unsigned int delta
)
267 if (schedule_delayed_work(&ftdi
->respond_work
, delta
))
268 kref_get(&ftdi
->kref
);
271 static void ftdi_response_cancel_work(struct usb_ftdi
*ftdi
)
273 if (cancel_delayed_work_sync(&ftdi
->respond_work
))
274 kref_put(&ftdi
->kref
, ftdi_elan_delete
);
277 void ftdi_elan_gone_away(struct platform_device
*pdev
)
279 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
280 ftdi
->gone_away
+= 1;
281 ftdi_elan_put_kref(ftdi
);
285 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away
);
286 static void ftdi_release_platform_dev(struct device
*dev
)
291 static void ftdi_elan_do_callback(struct usb_ftdi
*ftdi
,
292 struct u132_target
*target
, u8
*buffer
, int length
);
293 static void ftdi_elan_kick_command_queue(struct usb_ftdi
*ftdi
);
294 static void ftdi_elan_kick_respond_queue(struct usb_ftdi
*ftdi
);
295 static int ftdi_elan_setupOHCI(struct usb_ftdi
*ftdi
);
296 static int ftdi_elan_checkingPCI(struct usb_ftdi
*ftdi
);
297 static int ftdi_elan_enumeratePCI(struct usb_ftdi
*ftdi
);
298 static int ftdi_elan_synchronize(struct usb_ftdi
*ftdi
);
299 static int ftdi_elan_stuck_waiting(struct usb_ftdi
*ftdi
);
300 static int ftdi_elan_command_engine(struct usb_ftdi
*ftdi
);
301 static int ftdi_elan_respond_engine(struct usb_ftdi
*ftdi
);
302 static int ftdi_elan_hcd_init(struct usb_ftdi
*ftdi
)
304 if (ftdi
->platform_dev
.dev
.parent
)
307 ftdi_elan_get_kref(ftdi
);
308 ftdi
->platform_data
.potpg
= 100;
309 ftdi
->platform_data
.reset
= NULL
;
310 ftdi
->platform_dev
.id
= ftdi
->sequence_num
;
311 ftdi
->platform_dev
.resource
= ftdi
->resources
;
312 ftdi
->platform_dev
.num_resources
= ARRAY_SIZE(ftdi
->resources
);
313 ftdi
->platform_dev
.dev
.platform_data
= &ftdi
->platform_data
;
314 ftdi
->platform_dev
.dev
.parent
= NULL
;
315 ftdi
->platform_dev
.dev
.release
= ftdi_release_platform_dev
;
316 ftdi
->platform_dev
.dev
.dma_mask
= NULL
;
317 snprintf(ftdi
->device_name
, sizeof(ftdi
->device_name
), "u132_hcd");
318 ftdi
->platform_dev
.name
= ftdi
->device_name
;
319 dev_info(&ftdi
->udev
->dev
, "requesting module '%s'\n", "u132_hcd");
320 request_module("u132_hcd");
321 dev_info(&ftdi
->udev
->dev
, "registering '%s'\n",
322 ftdi
->platform_dev
.name
);
324 return platform_device_register(&ftdi
->platform_dev
);
327 static void ftdi_elan_abandon_completions(struct usb_ftdi
*ftdi
)
329 mutex_lock(&ftdi
->u132_lock
);
330 while (ftdi
->respond_next
> ftdi
->respond_head
) {
331 struct u132_respond
*respond
= &ftdi
->respond
[RESPOND_MASK
&
332 ftdi
->respond_head
++];
333 *respond
->result
= -ESHUTDOWN
;
335 complete(&respond
->wait_completion
);
336 } mutex_unlock(&ftdi
->u132_lock
);
339 static void ftdi_elan_abandon_targets(struct usb_ftdi
*ftdi
)
342 mutex_lock(&ftdi
->u132_lock
);
343 while (ed_number
-- > 0) {
344 struct u132_target
*target
= &ftdi
->target
[ed_number
];
345 if (target
->active
== 1) {
346 target
->condition_code
= TD_DEVNOTRESP
;
347 mutex_unlock(&ftdi
->u132_lock
);
348 ftdi_elan_do_callback(ftdi
, target
, NULL
, 0);
349 mutex_lock(&ftdi
->u132_lock
);
355 mutex_unlock(&ftdi
->u132_lock
);
358 static void ftdi_elan_flush_targets(struct usb_ftdi
*ftdi
)
361 mutex_lock(&ftdi
->u132_lock
);
362 while (ed_number
-- > 0) {
363 struct u132_target
*target
= &ftdi
->target
[ed_number
];
364 target
->abandoning
= 1;
365 wait_1
:if (target
->active
== 1) {
366 int command_size
= ftdi
->command_next
-
368 if (command_size
< COMMAND_SIZE
) {
369 struct u132_command
*command
= &ftdi
->command
[
370 COMMAND_MASK
& ftdi
->command_next
];
371 command
->header
= 0x80 | (ed_number
<< 5) | 0x4;
372 command
->length
= 0x00;
373 command
->address
= 0x00;
374 command
->width
= 0x00;
375 command
->follows
= 0;
377 command
->buffer
= &command
->value
;
378 ftdi
->command_next
+= 1;
379 ftdi_elan_kick_command_queue(ftdi
);
381 mutex_unlock(&ftdi
->u132_lock
);
383 mutex_lock(&ftdi
->u132_lock
);
387 wait_2
:if (target
->active
== 1) {
388 int command_size
= ftdi
->command_next
-
390 if (command_size
< COMMAND_SIZE
) {
391 struct u132_command
*command
= &ftdi
->command
[
392 COMMAND_MASK
& ftdi
->command_next
];
393 command
->header
= 0x90 | (ed_number
<< 5);
394 command
->length
= 0x00;
395 command
->address
= 0x00;
396 command
->width
= 0x00;
397 command
->follows
= 0;
399 command
->buffer
= &command
->value
;
400 ftdi
->command_next
+= 1;
401 ftdi_elan_kick_command_queue(ftdi
);
403 mutex_unlock(&ftdi
->u132_lock
);
405 mutex_lock(&ftdi
->u132_lock
);
413 mutex_unlock(&ftdi
->u132_lock
);
416 static void ftdi_elan_cancel_targets(struct usb_ftdi
*ftdi
)
419 mutex_lock(&ftdi
->u132_lock
);
420 while (ed_number
-- > 0) {
421 struct u132_target
*target
= &ftdi
->target
[ed_number
];
422 target
->abandoning
= 1;
423 wait
:if (target
->active
== 1) {
424 int command_size
= ftdi
->command_next
-
426 if (command_size
< COMMAND_SIZE
) {
427 struct u132_command
*command
= &ftdi
->command
[
428 COMMAND_MASK
& ftdi
->command_next
];
429 command
->header
= 0x80 | (ed_number
<< 5) | 0x4;
430 command
->length
= 0x00;
431 command
->address
= 0x00;
432 command
->width
= 0x00;
433 command
->follows
= 0;
435 command
->buffer
= &command
->value
;
436 ftdi
->command_next
+= 1;
437 ftdi_elan_kick_command_queue(ftdi
);
439 mutex_unlock(&ftdi
->u132_lock
);
441 mutex_lock(&ftdi
->u132_lock
);
449 mutex_unlock(&ftdi
->u132_lock
);
452 static void ftdi_elan_kick_command_queue(struct usb_ftdi
*ftdi
)
454 ftdi_command_queue_work(ftdi
, 0);
457 static void ftdi_elan_command_work(struct work_struct
*work
)
459 struct usb_ftdi
*ftdi
=
460 container_of(work
, struct usb_ftdi
, command_work
.work
);
462 if (ftdi
->disconnected
> 0) {
463 ftdi_elan_put_kref(ftdi
);
466 int retval
= ftdi_elan_command_engine(ftdi
);
467 if (retval
== -ESHUTDOWN
) {
468 ftdi
->disconnected
+= 1;
469 } else if (retval
== -ENODEV
) {
470 ftdi
->disconnected
+= 1;
472 dev_err(&ftdi
->udev
->dev
, "command error %d\n", retval
);
473 ftdi_command_requeue_work(ftdi
, msecs_to_jiffies(10));
478 static void ftdi_elan_kick_respond_queue(struct usb_ftdi
*ftdi
)
480 ftdi_respond_queue_work(ftdi
, 0);
483 static void ftdi_elan_respond_work(struct work_struct
*work
)
485 struct usb_ftdi
*ftdi
=
486 container_of(work
, struct usb_ftdi
, respond_work
.work
);
487 if (ftdi
->disconnected
> 0) {
488 ftdi_elan_put_kref(ftdi
);
491 int retval
= ftdi_elan_respond_engine(ftdi
);
493 } else if (retval
== -ESHUTDOWN
) {
494 ftdi
->disconnected
+= 1;
495 } else if (retval
== -ENODEV
) {
496 ftdi
->disconnected
+= 1;
497 } else if (retval
== -EILSEQ
) {
498 ftdi
->disconnected
+= 1;
500 ftdi
->disconnected
+= 1;
501 dev_err(&ftdi
->udev
->dev
, "respond error %d\n", retval
);
503 if (ftdi
->disconnected
> 0) {
504 ftdi_elan_abandon_completions(ftdi
);
505 ftdi_elan_abandon_targets(ftdi
);
507 ftdi_response_requeue_work(ftdi
, msecs_to_jiffies(10));
514 * the sw_lock is initially held and will be freed
515 * after the FTDI has been synchronized
518 static void ftdi_elan_status_work(struct work_struct
*work
)
520 struct usb_ftdi
*ftdi
=
521 container_of(work
, struct usb_ftdi
, status_work
.work
);
522 int work_delay_in_msec
= 0;
523 if (ftdi
->disconnected
> 0) {
524 ftdi_elan_put_kref(ftdi
);
526 } else if (ftdi
->synchronized
== 0) {
527 down(&ftdi
->sw_lock
);
528 if (ftdi_elan_synchronize(ftdi
) == 0) {
529 ftdi
->synchronized
= 1;
530 ftdi_command_queue_work(ftdi
, 1);
531 ftdi_respond_queue_work(ftdi
, 1);
533 work_delay_in_msec
= 100;
535 dev_err(&ftdi
->udev
->dev
, "synchronize failed\n");
537 work_delay_in_msec
= 10 *1000;
539 } else if (ftdi
->stuck_status
> 0) {
540 if (ftdi_elan_stuck_waiting(ftdi
) == 0) {
541 ftdi
->stuck_status
= 0;
542 ftdi
->synchronized
= 0;
543 } else if ((ftdi
->stuck_status
++ % 60) == 1) {
544 dev_err(&ftdi
->udev
->dev
, "WRONG type of card inserted - please remove\n");
546 dev_err(&ftdi
->udev
->dev
, "WRONG type of card inserted - checked %d times\n",
548 work_delay_in_msec
= 100;
549 } else if (ftdi
->enumerated
== 0) {
550 if (ftdi_elan_enumeratePCI(ftdi
) == 0) {
551 ftdi
->enumerated
= 1;
552 work_delay_in_msec
= 250;
554 work_delay_in_msec
= 1000;
555 } else if (ftdi
->initialized
== 0) {
556 if (ftdi_elan_setupOHCI(ftdi
) == 0) {
557 ftdi
->initialized
= 1;
558 work_delay_in_msec
= 500;
560 dev_err(&ftdi
->udev
->dev
, "initialized failed - trying again in 10 seconds\n");
561 work_delay_in_msec
= 1 *1000;
563 } else if (ftdi
->registered
== 0) {
564 work_delay_in_msec
= 10;
565 if (ftdi_elan_hcd_init(ftdi
) == 0) {
566 ftdi
->registered
= 1;
568 dev_err(&ftdi
->udev
->dev
, "register failed\n");
569 work_delay_in_msec
= 250;
571 if (ftdi_elan_checkingPCI(ftdi
) == 0) {
572 work_delay_in_msec
= 250;
573 } else if (ftdi
->controlreg
& 0x00400000) {
574 if (ftdi
->gone_away
> 0) {
575 dev_err(&ftdi
->udev
->dev
, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
576 ftdi
->platform_dev
.dev
.parent
,
577 &ftdi
->platform_dev
.dev
);
578 platform_device_unregister(&ftdi
->platform_dev
);
579 ftdi
->platform_dev
.dev
.parent
= NULL
;
580 ftdi
->registered
= 0;
581 ftdi
->enumerated
= 0;
582 ftdi
->card_ejected
= 0;
583 ftdi
->initialized
= 0;
586 ftdi_elan_flush_targets(ftdi
);
587 work_delay_in_msec
= 250;
589 dev_err(&ftdi
->udev
->dev
, "PCI device has disappeared\n");
590 ftdi_elan_cancel_targets(ftdi
);
591 work_delay_in_msec
= 500;
592 ftdi
->enumerated
= 0;
593 ftdi
->initialized
= 0;
596 if (ftdi
->disconnected
> 0) {
597 ftdi_elan_put_kref(ftdi
);
600 ftdi_status_requeue_work(ftdi
,
601 msecs_to_jiffies(work_delay_in_msec
));
608 * file_operations for the jtag interface
610 * the usage count for the device is incremented on open()
611 * and decremented on release()
613 static int ftdi_elan_open(struct inode
*inode
, struct file
*file
)
616 struct usb_interface
*interface
;
618 subminor
= iminor(inode
);
619 interface
= usb_find_interface(&ftdi_elan_driver
, subminor
);
622 pr_err("can't find device for minor %d\n", subminor
);
625 struct usb_ftdi
*ftdi
= usb_get_intfdata(interface
);
629 if (down_interruptible(&ftdi
->sw_lock
)) {
632 ftdi_elan_get_kref(ftdi
);
633 file
->private_data
= ftdi
;
640 static int ftdi_elan_release(struct inode
*inode
, struct file
*file
)
642 struct usb_ftdi
*ftdi
= file
->private_data
;
645 up(&ftdi
->sw_lock
); /* decrement the count on our device */
646 ftdi_elan_put_kref(ftdi
);
653 * blocking bulk reads are used to get data from the device
656 static ssize_t
ftdi_elan_read(struct file
*file
, char __user
*buffer
,
657 size_t count
, loff_t
*ppos
)
659 char data
[30 *3 + 4];
661 int m
= (sizeof(data
) - 1) / 3 - 1;
663 int retry_on_empty
= 10;
664 int retry_on_timeout
= 5;
665 struct usb_ftdi
*ftdi
= file
->private_data
;
666 if (ftdi
->disconnected
> 0) {
670 have
:if (ftdi
->bulk_in_left
> 0) {
672 char *p
= ++ftdi
->bulk_in_last
+ ftdi
->bulk_in_buffer
;
673 ftdi
->bulk_in_left
-= 1;
674 if (bytes_read
< m
) {
675 d
+= sprintf(d
, " %02X", 0x000000FF & *p
);
676 } else if (bytes_read
> m
) {
678 d
+= sprintf(d
, " ..");
679 if (copy_to_user(buffer
++, p
, 1)) {
688 more
:if (count
> 0) {
689 int packet_bytes
= 0;
690 int retval
= usb_bulk_msg(ftdi
->udev
,
691 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
692 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
694 if (packet_bytes
> 2) {
695 ftdi
->bulk_in_left
= packet_bytes
- 2;
696 ftdi
->bulk_in_last
= 1;
698 } else if (retval
== -ETIMEDOUT
) {
699 if (retry_on_timeout
-- > 0) {
701 } else if (bytes_read
> 0) {
705 } else if (retval
== 0) {
706 if (retry_on_empty
-- > 0) {
716 static void ftdi_elan_write_bulk_callback(struct urb
*urb
)
718 struct usb_ftdi
*ftdi
= urb
->context
;
719 int status
= urb
->status
;
721 if (status
&& !(status
== -ENOENT
|| status
== -ECONNRESET
||
722 status
== -ESHUTDOWN
)) {
723 dev_err(&ftdi
->udev
->dev
,
724 "urb=%p write bulk status received: %d\n", urb
, status
);
726 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
727 urb
->transfer_buffer
, urb
->transfer_dma
);
730 static int fill_buffer_with_all_queued_commands(struct usb_ftdi
*ftdi
,
731 char *buf
, int command_size
, int total_size
)
735 int I
= command_size
;
736 int i
= ftdi
->command_head
;
738 struct u132_command
*command
= &ftdi
->command
[COMMAND_MASK
&
740 int F
= command
->follows
;
741 u8
*f
= command
->buffer
;
742 if (command
->header
& 0x80) {
743 ed_commands
|= 1 << (0x3 & (command
->header
>> 5));
745 buf
[b
++] = command
->header
;
746 buf
[b
++] = (command
->length
>> 0) & 0x00FF;
747 buf
[b
++] = (command
->length
>> 8) & 0x00FF;
748 buf
[b
++] = command
->address
;
749 buf
[b
++] = command
->width
;
757 static int ftdi_elan_total_command_size(struct usb_ftdi
*ftdi
, int command_size
)
760 int I
= command_size
;
761 int i
= ftdi
->command_head
;
763 struct u132_command
*command
= &ftdi
->command
[COMMAND_MASK
&
765 total_size
+= 5 + command
->follows
;
769 static int ftdi_elan_command_engine(struct usb_ftdi
*ftdi
)
776 int command_size
= ftdi
->command_next
- ftdi
->command_head
;
777 if (command_size
== 0)
779 total_size
= ftdi_elan_total_command_size(ftdi
, command_size
);
780 urb
= usb_alloc_urb(0, GFP_KERNEL
);
783 buf
= usb_alloc_coherent(ftdi
->udev
, total_size
, GFP_KERNEL
,
786 dev_err(&ftdi
->udev
->dev
, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
787 command_size
, total_size
);
791 ed_commands
= fill_buffer_with_all_queued_commands(ftdi
, buf
,
792 command_size
, total_size
);
793 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
794 ftdi
->bulk_out_endpointAddr
), buf
, total_size
,
795 ftdi_elan_write_bulk_callback
, ftdi
);
796 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
798 char diag
[40 *3 + 4];
802 int s
= (sizeof(diag
) - 1) / 3;
804 while (s
-- > 0 && m
-- > 0) {
805 if (s
> 0 || m
== 0) {
806 d
+= sprintf(d
, " %02X", *c
++);
808 d
+= sprintf(d
, " ..");
811 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
813 dev_err(&ftdi
->udev
->dev
, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
814 retval
, urb
, command_size
, total_size
);
815 usb_free_coherent(ftdi
->udev
, total_size
, buf
, urb
->transfer_dma
);
819 usb_free_urb(urb
); /* release our reference to this urb,
820 the USB core will eventually free it entirely */
821 ftdi
->command_head
+= command_size
;
822 ftdi_elan_kick_respond_queue(ftdi
);
826 static void ftdi_elan_do_callback(struct usb_ftdi
*ftdi
,
827 struct u132_target
*target
, u8
*buffer
, int length
)
829 struct urb
*urb
= target
->urb
;
830 int halted
= target
->halted
;
831 int skipped
= target
->skipped
;
832 int actual
= target
->actual
;
833 int non_null
= target
->non_null
;
834 int toggle_bits
= target
->toggle_bits
;
835 int error_count
= target
->error_count
;
836 int condition_code
= target
->condition_code
;
837 int repeat_number
= target
->repeat_number
;
838 void (*callback
) (void *, struct urb
*, u8
*, int, int, int, int, int,
839 int, int, int, int) = target
->callback
;
841 target
->callback
= NULL
;
842 (*callback
) (target
->endp
, urb
, buffer
, length
, toggle_bits
,
843 error_count
, condition_code
, repeat_number
, halted
, skipped
,
847 static char *have_ed_set_response(struct usb_ftdi
*ftdi
,
848 struct u132_target
*target
, u16 ed_length
, int ed_number
, int ed_type
,
851 int payload
= (ed_length
>> 0) & 0x07FF;
852 mutex_lock(&ftdi
->u132_lock
);
854 target
->non_null
= (ed_length
>> 15) & 0x0001;
855 target
->repeat_number
= (ed_length
>> 11) & 0x000F;
856 if (ed_type
== 0x02 || ed_type
== 0x03) {
857 if (payload
== 0 || target
->abandoning
> 0) {
858 target
->abandoning
= 0;
859 mutex_unlock(&ftdi
->u132_lock
);
860 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
865 return ftdi
->response
;
867 ftdi
->expected
= 4 + payload
;
869 mutex_unlock(&ftdi
->u132_lock
);
873 target
->abandoning
= 0;
874 mutex_unlock(&ftdi
->u132_lock
);
875 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
880 return ftdi
->response
;
884 static char *have_ed_get_response(struct usb_ftdi
*ftdi
,
885 struct u132_target
*target
, u16 ed_length
, int ed_number
, int ed_type
,
888 mutex_lock(&ftdi
->u132_lock
);
889 target
->condition_code
= TD_DEVNOTRESP
;
890 target
->actual
= (ed_length
>> 0) & 0x01FF;
891 target
->non_null
= (ed_length
>> 15) & 0x0001;
892 target
->repeat_number
= (ed_length
>> 11) & 0x000F;
893 mutex_unlock(&ftdi
->u132_lock
);
895 ftdi_elan_do_callback(ftdi
, target
, NULL
, 0);
896 target
->abandoning
= 0;
900 return ftdi
->response
;
905 * The engine tries to empty the FTDI fifo
907 * all responses found in the fifo data are dispatched thus
908 * the response buffer can only ever hold a maximum sized
909 * response from the Uxxx.
912 static int ftdi_elan_respond_engine(struct usb_ftdi
*ftdi
)
914 u8
*b
= ftdi
->response
+ ftdi
->received
;
916 int retry_on_empty
= 1;
917 int retry_on_timeout
= 3;
919 int packet_bytes
= 0;
920 int retval
= usb_bulk_msg(ftdi
->udev
,
921 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
922 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
924 char diag
[30 *3 + 4];
926 int m
= packet_bytes
;
927 u8
*c
= ftdi
->bulk_in_buffer
;
928 int s
= (sizeof(diag
) - 1) / 3;
930 while (s
-- > 0 && m
-- > 0) {
931 if (s
> 0 || m
== 0) {
932 d
+= sprintf(d
, " %02X", *c
++);
934 d
+= sprintf(d
, " ..");
936 if (packet_bytes
> 2) {
937 ftdi
->bulk_in_left
= packet_bytes
- 2;
938 ftdi
->bulk_in_last
= 1;
940 } else if (retval
== -ETIMEDOUT
) {
941 if (retry_on_timeout
-- > 0) {
942 dev_err(&ftdi
->udev
->dev
, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
943 packet_bytes
, bytes_read
, diag
);
945 } else if (bytes_read
> 0) {
946 dev_err(&ftdi
->udev
->dev
, "ONLY %d bytes%s\n",
950 dev_err(&ftdi
->udev
->dev
, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
951 packet_bytes
, bytes_read
, diag
);
954 } else if (retval
== -EILSEQ
) {
955 dev_err(&ftdi
->udev
->dev
, "error = %d with packet_bytes = %d with total %d bytes%s\n",
956 retval
, packet_bytes
, bytes_read
, diag
);
959 dev_err(&ftdi
->udev
->dev
, "error = %d with packet_bytes = %d with total %d bytes%s\n",
960 retval
, packet_bytes
, bytes_read
, diag
);
963 if (retry_on_empty
-- > 0) {
972 have
:if (ftdi
->bulk_in_left
> 0) {
973 u8 c
= ftdi
->bulk_in_buffer
[++ftdi
->bulk_in_last
];
975 ftdi
->bulk_in_left
-= 1;
976 if (ftdi
->received
== 0 && c
== 0xFF) {
980 if (++ftdi
->received
< ftdi
->expected
) {
982 } else if (ftdi
->ed_found
) {
983 int ed_number
= (ftdi
->response
[0] >> 5) & 0x03;
984 u16 ed_length
= (ftdi
->response
[2] << 8) |
986 struct u132_target
*target
= &ftdi
->target
[ed_number
];
987 int payload
= (ed_length
>> 0) & 0x07FF;
988 char diag
[30 *3 + 4];
991 u8
*c
= 4 + ftdi
->response
;
992 int s
= (sizeof(diag
) - 1) / 3;
994 while (s
-- > 0 && m
-- > 0) {
995 if (s
> 0 || m
== 0) {
996 d
+= sprintf(d
, " %02X", *c
++);
998 d
+= sprintf(d
, " ..");
1000 ftdi_elan_do_callback(ftdi
, target
, 4 + ftdi
->response
,
1007 } else if (ftdi
->expected
== 8) {
1009 int respond_head
= ftdi
->respond_head
++;
1010 struct u132_respond
*respond
= &ftdi
->respond
[
1011 RESPOND_MASK
& respond_head
];
1012 u32 data
= ftdi
->response
[7];
1014 data
|= ftdi
->response
[6];
1016 data
|= ftdi
->response
[5];
1018 data
|= ftdi
->response
[4];
1019 *respond
->value
= data
;
1020 *respond
->result
= 0;
1021 complete(&respond
->wait_completion
);
1026 buscmd
= (ftdi
->response
[0] >> 0) & 0x0F;
1027 if (buscmd
== 0x00) {
1028 } else if (buscmd
== 0x02) {
1029 } else if (buscmd
== 0x06) {
1030 } else if (buscmd
== 0x0A) {
1032 dev_err(&ftdi
->udev
->dev
, "Uxxx unknown(%0X) value = %08X\n",
1036 if ((ftdi
->response
[0] & 0x80) == 0x00) {
1040 int ed_number
= (ftdi
->response
[0] >> 5) & 0x03;
1041 int ed_type
= (ftdi
->response
[0] >> 0) & 0x03;
1042 u16 ed_length
= (ftdi
->response
[2] << 8) |
1044 struct u132_target
*target
= &ftdi
->target
[
1046 target
->halted
= (ftdi
->response
[0] >> 3) &
1048 target
->skipped
= (ftdi
->response
[0] >> 2) &
1050 target
->toggle_bits
= (ftdi
->response
[3] >> 6)
1052 target
->error_count
= (ftdi
->response
[3] >> 4)
1054 target
->condition_code
= (ftdi
->response
[
1056 if ((ftdi
->response
[0] & 0x10) == 0x00) {
1057 b
= have_ed_set_response(ftdi
, target
,
1058 ed_length
, ed_number
, ed_type
,
1062 b
= have_ed_get_response(ftdi
, target
,
1063 ed_length
, ed_number
, ed_type
,
1075 * create a urb, and a buffer for it, and copy the data to the urb
1078 static ssize_t
ftdi_elan_write(struct file
*file
,
1079 const char __user
*user_buffer
, size_t count
,
1085 struct usb_ftdi
*ftdi
= file
->private_data
;
1087 if (ftdi
->disconnected
> 0) {
1093 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1098 buf
= usb_alloc_coherent(ftdi
->udev
, count
, GFP_KERNEL
,
1099 &urb
->transfer_dma
);
1104 if (copy_from_user(buf
, user_buffer
, count
)) {
1108 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
1109 ftdi
->bulk_out_endpointAddr
), buf
, count
,
1110 ftdi_elan_write_bulk_callback
, ftdi
);
1111 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1112 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
1114 dev_err(&ftdi
->udev
->dev
,
1115 "failed submitting write urb, error %d\n", retval
);
1123 usb_free_coherent(ftdi
->udev
, count
, buf
, urb
->transfer_dma
);
1130 static const struct file_operations ftdi_elan_fops
= {
1131 .owner
= THIS_MODULE
,
1132 .llseek
= no_llseek
,
1133 .read
= ftdi_elan_read
,
1134 .write
= ftdi_elan_write
,
1135 .open
= ftdi_elan_open
,
1136 .release
= ftdi_elan_release
,
1140 * usb class driver info in order to get a minor number from the usb core,
1141 * and to have the device registered with the driver core
1143 static struct usb_class_driver ftdi_elan_jtag_class
= {
1144 .name
= "ftdi-%d-jtag",
1145 .fops
= &ftdi_elan_fops
,
1146 .minor_base
= USB_FTDI_ELAN_MINOR_BASE
,
1150 * the following definitions are for the
1151 * ELAN FPGA state machgine processor that
1152 * lies on the other side of the FTDI chip
1154 #define cPCIu132rd 0x0
1155 #define cPCIu132wr 0x1
1156 #define cPCIiord 0x2
1157 #define cPCIiowr 0x3
1158 #define cPCImemrd 0x6
1159 #define cPCImemwr 0x7
1160 #define cPCIcfgrd 0xA
1161 #define cPCIcfgwr 0xB
1162 #define cPCInull 0xF
1163 #define cU132cmd_status 0x0
1164 #define cU132flash 0x1
1165 #define cPIDsetup 0x0
1168 #define cPIDinonce 0x3
1169 #define cCCnoerror 0x0
1171 #define cCCbitstuff 0x2
1172 #define cCCtoggle 0x3
1173 #define cCCstall 0x4
1174 #define cCCnoresp 0x5
1175 #define cCCbadpid1 0x6
1176 #define cCCbadpid2 0x7
1177 #define cCCdataoverrun 0x8
1178 #define cCCdataunderrun 0x9
1179 #define cCCbuffoverrun 0xC
1180 #define cCCbuffunderrun 0xD
1181 #define cCCnotaccessed 0xF
1182 static int ftdi_elan_write_reg(struct usb_ftdi
*ftdi
, u32 data
)
1184 wait
:if (ftdi
->disconnected
> 0) {
1188 mutex_lock(&ftdi
->u132_lock
);
1189 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1190 if (command_size
< COMMAND_SIZE
) {
1191 struct u132_command
*command
= &ftdi
->command
[
1192 COMMAND_MASK
& ftdi
->command_next
];
1193 command
->header
= 0x00 | cPCIu132wr
;
1194 command
->length
= 0x04;
1195 command
->address
= 0x00;
1196 command
->width
= 0x00;
1197 command
->follows
= 4;
1198 command
->value
= data
;
1199 command
->buffer
= &command
->value
;
1200 ftdi
->command_next
+= 1;
1201 ftdi_elan_kick_command_queue(ftdi
);
1202 mutex_unlock(&ftdi
->u132_lock
);
1205 mutex_unlock(&ftdi
->u132_lock
);
1212 static int ftdi_elan_write_config(struct usb_ftdi
*ftdi
, int config_offset
,
1215 u8 addressofs
= config_offset
/ 4;
1216 wait
:if (ftdi
->disconnected
> 0) {
1220 mutex_lock(&ftdi
->u132_lock
);
1221 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1222 if (command_size
< COMMAND_SIZE
) {
1223 struct u132_command
*command
= &ftdi
->command
[
1224 COMMAND_MASK
& ftdi
->command_next
];
1225 command
->header
= 0x00 | (cPCIcfgwr
& 0x0F);
1226 command
->length
= 0x04;
1227 command
->address
= addressofs
;
1228 command
->width
= 0x00 | (width
& 0x0F);
1229 command
->follows
= 4;
1230 command
->value
= data
;
1231 command
->buffer
= &command
->value
;
1232 ftdi
->command_next
+= 1;
1233 ftdi_elan_kick_command_queue(ftdi
);
1234 mutex_unlock(&ftdi
->u132_lock
);
1237 mutex_unlock(&ftdi
->u132_lock
);
1244 static int ftdi_elan_write_pcimem(struct usb_ftdi
*ftdi
, int mem_offset
,
1247 u8 addressofs
= mem_offset
/ 4;
1248 wait
:if (ftdi
->disconnected
> 0) {
1252 mutex_lock(&ftdi
->u132_lock
);
1253 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1254 if (command_size
< COMMAND_SIZE
) {
1255 struct u132_command
*command
= &ftdi
->command
[
1256 COMMAND_MASK
& ftdi
->command_next
];
1257 command
->header
= 0x00 | (cPCImemwr
& 0x0F);
1258 command
->length
= 0x04;
1259 command
->address
= addressofs
;
1260 command
->width
= 0x00 | (width
& 0x0F);
1261 command
->follows
= 4;
1262 command
->value
= data
;
1263 command
->buffer
= &command
->value
;
1264 ftdi
->command_next
+= 1;
1265 ftdi_elan_kick_command_queue(ftdi
);
1266 mutex_unlock(&ftdi
->u132_lock
);
1269 mutex_unlock(&ftdi
->u132_lock
);
1276 int usb_ftdi_elan_write_pcimem(struct platform_device
*pdev
, int mem_offset
,
1279 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1280 return ftdi_elan_write_pcimem(ftdi
, mem_offset
, width
, data
);
1284 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem
);
1285 static int ftdi_elan_read_reg(struct usb_ftdi
*ftdi
, u32
*data
)
1287 wait
:if (ftdi
->disconnected
> 0) {
1292 mutex_lock(&ftdi
->u132_lock
);
1293 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1294 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1295 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1297 struct u132_command
*command
= &ftdi
->command
[
1298 COMMAND_MASK
& ftdi
->command_next
];
1299 struct u132_respond
*respond
= &ftdi
->respond
[
1300 RESPOND_MASK
& ftdi
->respond_next
];
1301 int result
= -ENODEV
;
1302 respond
->result
= &result
;
1303 respond
->header
= command
->header
= 0x00 | cPCIu132rd
;
1304 command
->length
= 0x04;
1305 respond
->address
= command
->address
= cU132cmd_status
;
1306 command
->width
= 0x00;
1307 command
->follows
= 0;
1309 command
->buffer
= NULL
;
1310 respond
->value
= data
;
1311 init_completion(&respond
->wait_completion
);
1312 ftdi
->command_next
+= 1;
1313 ftdi
->respond_next
+= 1;
1314 ftdi_elan_kick_command_queue(ftdi
);
1315 mutex_unlock(&ftdi
->u132_lock
);
1316 wait_for_completion(&respond
->wait_completion
);
1319 mutex_unlock(&ftdi
->u132_lock
);
1326 static int ftdi_elan_read_config(struct usb_ftdi
*ftdi
, int config_offset
,
1327 u8 width
, u32
*data
)
1329 u8 addressofs
= config_offset
/ 4;
1330 wait
:if (ftdi
->disconnected
> 0) {
1335 mutex_lock(&ftdi
->u132_lock
);
1336 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1337 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1338 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1340 struct u132_command
*command
= &ftdi
->command
[
1341 COMMAND_MASK
& ftdi
->command_next
];
1342 struct u132_respond
*respond
= &ftdi
->respond
[
1343 RESPOND_MASK
& ftdi
->respond_next
];
1344 int result
= -ENODEV
;
1345 respond
->result
= &result
;
1346 respond
->header
= command
->header
= 0x00 | (cPCIcfgrd
&
1348 command
->length
= 0x04;
1349 respond
->address
= command
->address
= addressofs
;
1350 command
->width
= 0x00 | (width
& 0x0F);
1351 command
->follows
= 0;
1353 command
->buffer
= NULL
;
1354 respond
->value
= data
;
1355 init_completion(&respond
->wait_completion
);
1356 ftdi
->command_next
+= 1;
1357 ftdi
->respond_next
+= 1;
1358 ftdi_elan_kick_command_queue(ftdi
);
1359 mutex_unlock(&ftdi
->u132_lock
);
1360 wait_for_completion(&respond
->wait_completion
);
1363 mutex_unlock(&ftdi
->u132_lock
);
1370 static int ftdi_elan_read_pcimem(struct usb_ftdi
*ftdi
, int mem_offset
,
1371 u8 width
, u32
*data
)
1373 u8 addressofs
= mem_offset
/ 4;
1374 wait
:if (ftdi
->disconnected
> 0) {
1379 mutex_lock(&ftdi
->u132_lock
);
1380 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1381 respond_size
= ftdi
->respond_next
- ftdi
->respond_head
;
1382 if (command_size
< COMMAND_SIZE
&& respond_size
< RESPOND_SIZE
)
1384 struct u132_command
*command
= &ftdi
->command
[
1385 COMMAND_MASK
& ftdi
->command_next
];
1386 struct u132_respond
*respond
= &ftdi
->respond
[
1387 RESPOND_MASK
& ftdi
->respond_next
];
1388 int result
= -ENODEV
;
1389 respond
->result
= &result
;
1390 respond
->header
= command
->header
= 0x00 | (cPCImemrd
&
1392 command
->length
= 0x04;
1393 respond
->address
= command
->address
= addressofs
;
1394 command
->width
= 0x00 | (width
& 0x0F);
1395 command
->follows
= 0;
1397 command
->buffer
= NULL
;
1398 respond
->value
= data
;
1399 init_completion(&respond
->wait_completion
);
1400 ftdi
->command_next
+= 1;
1401 ftdi
->respond_next
+= 1;
1402 ftdi_elan_kick_command_queue(ftdi
);
1403 mutex_unlock(&ftdi
->u132_lock
);
1404 wait_for_completion(&respond
->wait_completion
);
1407 mutex_unlock(&ftdi
->u132_lock
);
1414 int usb_ftdi_elan_read_pcimem(struct platform_device
*pdev
, int mem_offset
,
1415 u8 width
, u32
*data
)
1417 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1418 if (ftdi
->initialized
== 0) {
1421 return ftdi_elan_read_pcimem(ftdi
, mem_offset
, width
, data
);
1425 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem
);
1426 static int ftdi_elan_edset_setup(struct usb_ftdi
*ftdi
, u8 ed_number
,
1427 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1428 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1429 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1430 int halted
, int skipped
, int actual
, int non_null
))
1432 u8 ed
= ed_number
- 1;
1433 wait
:if (ftdi
->disconnected
> 0) {
1435 } else if (ftdi
->initialized
== 0) {
1439 mutex_lock(&ftdi
->u132_lock
);
1440 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1441 if (command_size
< COMMAND_SIZE
) {
1442 struct u132_target
*target
= &ftdi
->target
[ed
];
1443 struct u132_command
*command
= &ftdi
->command
[
1444 COMMAND_MASK
& ftdi
->command_next
];
1445 command
->header
= 0x80 | (ed
<< 5);
1446 command
->length
= 0x8007;
1447 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1449 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1450 usb_pipeout(urb
->pipe
));
1451 command
->follows
= 8;
1453 command
->buffer
= urb
->setup_packet
;
1454 target
->callback
= callback
;
1455 target
->endp
= endp
;
1458 ftdi
->command_next
+= 1;
1459 ftdi_elan_kick_command_queue(ftdi
);
1460 mutex_unlock(&ftdi
->u132_lock
);
1463 mutex_unlock(&ftdi
->u132_lock
);
1470 int usb_ftdi_elan_edset_setup(struct platform_device
*pdev
, u8 ed_number
,
1471 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1472 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1473 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1474 int halted
, int skipped
, int actual
, int non_null
))
1476 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1477 return ftdi_elan_edset_setup(ftdi
, ed_number
, endp
, urb
, address
,
1478 ep_number
, toggle_bits
, callback
);
1482 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup
);
1483 static int ftdi_elan_edset_input(struct usb_ftdi
*ftdi
, u8 ed_number
,
1484 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1485 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1486 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1487 int halted
, int skipped
, int actual
, int non_null
))
1489 u8 ed
= ed_number
- 1;
1490 wait
:if (ftdi
->disconnected
> 0) {
1492 } else if (ftdi
->initialized
== 0) {
1496 mutex_lock(&ftdi
->u132_lock
);
1497 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1498 if (command_size
< COMMAND_SIZE
) {
1499 struct u132_target
*target
= &ftdi
->target
[ed
];
1500 struct u132_command
*command
= &ftdi
->command
[
1501 COMMAND_MASK
& ftdi
->command_next
];
1502 u32 remaining_length
= urb
->transfer_buffer_length
-
1504 command
->header
= 0x82 | (ed
<< 5);
1505 if (remaining_length
== 0) {
1506 command
->length
= 0x0000;
1507 } else if (remaining_length
> 1024) {
1508 command
->length
= 0x8000 | 1023;
1510 command
->length
= 0x8000 | (remaining_length
-
1512 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1514 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1515 usb_pipeout(urb
->pipe
));
1516 command
->follows
= 0;
1518 command
->buffer
= NULL
;
1519 target
->callback
= callback
;
1520 target
->endp
= endp
;
1523 ftdi
->command_next
+= 1;
1524 ftdi_elan_kick_command_queue(ftdi
);
1525 mutex_unlock(&ftdi
->u132_lock
);
1528 mutex_unlock(&ftdi
->u132_lock
);
1535 int usb_ftdi_elan_edset_input(struct platform_device
*pdev
, u8 ed_number
,
1536 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1537 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1538 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1539 int halted
, int skipped
, int actual
, int non_null
))
1541 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1542 return ftdi_elan_edset_input(ftdi
, ed_number
, endp
, urb
, address
,
1543 ep_number
, toggle_bits
, callback
);
1547 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input
);
1548 static int ftdi_elan_edset_empty(struct usb_ftdi
*ftdi
, u8 ed_number
,
1549 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1550 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1551 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1552 int halted
, int skipped
, int actual
, int non_null
))
1554 u8 ed
= ed_number
- 1;
1555 wait
:if (ftdi
->disconnected
> 0) {
1557 } else if (ftdi
->initialized
== 0) {
1561 mutex_lock(&ftdi
->u132_lock
);
1562 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1563 if (command_size
< COMMAND_SIZE
) {
1564 struct u132_target
*target
= &ftdi
->target
[ed
];
1565 struct u132_command
*command
= &ftdi
->command
[
1566 COMMAND_MASK
& ftdi
->command_next
];
1567 command
->header
= 0x81 | (ed
<< 5);
1568 command
->length
= 0x0000;
1569 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1571 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1572 usb_pipeout(urb
->pipe
));
1573 command
->follows
= 0;
1575 command
->buffer
= NULL
;
1576 target
->callback
= callback
;
1577 target
->endp
= endp
;
1580 ftdi
->command_next
+= 1;
1581 ftdi_elan_kick_command_queue(ftdi
);
1582 mutex_unlock(&ftdi
->u132_lock
);
1585 mutex_unlock(&ftdi
->u132_lock
);
1592 int usb_ftdi_elan_edset_empty(struct platform_device
*pdev
, u8 ed_number
,
1593 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1594 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1595 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1596 int halted
, int skipped
, int actual
, int non_null
))
1598 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1599 return ftdi_elan_edset_empty(ftdi
, ed_number
, endp
, urb
, address
,
1600 ep_number
, toggle_bits
, callback
);
1604 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty
);
1605 static int ftdi_elan_edset_output(struct usb_ftdi
*ftdi
, u8 ed_number
,
1606 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1607 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1608 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1609 int halted
, int skipped
, int actual
, int non_null
))
1611 u8 ed
= ed_number
- 1;
1612 wait
:if (ftdi
->disconnected
> 0) {
1614 } else if (ftdi
->initialized
== 0) {
1618 mutex_lock(&ftdi
->u132_lock
);
1619 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1620 if (command_size
< COMMAND_SIZE
) {
1624 char data
[30 *3 + 4];
1626 int m
= (sizeof(data
) - 1) / 3 - 1;
1628 struct u132_target
*target
= &ftdi
->target
[ed
];
1629 struct u132_command
*command
= &ftdi
->command
[
1630 COMMAND_MASK
& ftdi
->command_next
];
1631 command
->header
= 0x81 | (ed
<< 5);
1632 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1634 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1635 usb_pipeout(urb
->pipe
));
1636 command
->follows
= min_t(u32
, 1024,
1637 urb
->transfer_buffer_length
-
1638 urb
->actual_length
);
1640 command
->buffer
= urb
->transfer_buffer
+
1642 command
->length
= 0x8000 | (command
->follows
- 1);
1643 b
= command
->buffer
;
1644 urb_size
= command
->follows
;
1646 while (urb_size
-- > 0) {
1648 } else if (i
++ < m
) {
1649 int w
= sprintf(d
, " %02X", *b
++);
1653 d
+= sprintf(d
, " ..");
1655 target
->callback
= callback
;
1656 target
->endp
= endp
;
1659 ftdi
->command_next
+= 1;
1660 ftdi_elan_kick_command_queue(ftdi
);
1661 mutex_unlock(&ftdi
->u132_lock
);
1664 mutex_unlock(&ftdi
->u132_lock
);
1671 int usb_ftdi_elan_edset_output(struct platform_device
*pdev
, u8 ed_number
,
1672 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1673 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1674 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1675 int halted
, int skipped
, int actual
, int non_null
))
1677 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1678 return ftdi_elan_edset_output(ftdi
, ed_number
, endp
, urb
, address
,
1679 ep_number
, toggle_bits
, callback
);
1683 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output
);
1684 static int ftdi_elan_edset_single(struct usb_ftdi
*ftdi
, u8 ed_number
,
1685 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1686 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1687 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1688 int halted
, int skipped
, int actual
, int non_null
))
1690 u8 ed
= ed_number
- 1;
1691 wait
:if (ftdi
->disconnected
> 0) {
1693 } else if (ftdi
->initialized
== 0) {
1697 mutex_lock(&ftdi
->u132_lock
);
1698 command_size
= ftdi
->command_next
- ftdi
->command_head
;
1699 if (command_size
< COMMAND_SIZE
) {
1700 u32 remaining_length
= urb
->transfer_buffer_length
-
1702 struct u132_target
*target
= &ftdi
->target
[ed
];
1703 struct u132_command
*command
= &ftdi
->command
[
1704 COMMAND_MASK
& ftdi
->command_next
];
1705 command
->header
= 0x83 | (ed
<< 5);
1706 if (remaining_length
== 0) {
1707 command
->length
= 0x0000;
1708 } else if (remaining_length
> 1024) {
1709 command
->length
= 0x8000 | 1023;
1711 command
->length
= 0x8000 | (remaining_length
-
1713 command
->address
= (toggle_bits
<< 6) | (ep_number
<< 2)
1715 command
->width
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1716 usb_pipeout(urb
->pipe
));
1717 command
->follows
= 0;
1719 command
->buffer
= NULL
;
1720 target
->callback
= callback
;
1721 target
->endp
= endp
;
1724 ftdi
->command_next
+= 1;
1725 ftdi_elan_kick_command_queue(ftdi
);
1726 mutex_unlock(&ftdi
->u132_lock
);
1729 mutex_unlock(&ftdi
->u132_lock
);
1736 int usb_ftdi_elan_edset_single(struct platform_device
*pdev
, u8 ed_number
,
1737 void *endp
, struct urb
*urb
, u8 address
, u8 ep_number
, u8 toggle_bits
,
1738 void (*callback
) (void *endp
, struct urb
*urb
, u8
*buf
, int len
,
1739 int toggle_bits
, int error_count
, int condition_code
, int repeat_number
,
1740 int halted
, int skipped
, int actual
, int non_null
))
1742 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1743 return ftdi_elan_edset_single(ftdi
, ed_number
, endp
, urb
, address
,
1744 ep_number
, toggle_bits
, callback
);
1748 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single
);
1749 static int ftdi_elan_edset_flush(struct usb_ftdi
*ftdi
, u8 ed_number
,
1752 u8 ed
= ed_number
- 1;
1753 if (ftdi
->disconnected
> 0) {
1755 } else if (ftdi
->initialized
== 0) {
1758 struct u132_target
*target
= &ftdi
->target
[ed
];
1759 mutex_lock(&ftdi
->u132_lock
);
1760 if (target
->abandoning
> 0) {
1761 mutex_unlock(&ftdi
->u132_lock
);
1764 target
->abandoning
= 1;
1765 wait_1
:if (target
->active
== 1) {
1766 int command_size
= ftdi
->command_next
-
1768 if (command_size
< COMMAND_SIZE
) {
1769 struct u132_command
*command
=
1770 &ftdi
->command
[COMMAND_MASK
&
1771 ftdi
->command_next
];
1772 command
->header
= 0x80 | (ed
<< 5) |
1774 command
->length
= 0x00;
1775 command
->address
= 0x00;
1776 command
->width
= 0x00;
1777 command
->follows
= 0;
1779 command
->buffer
= &command
->value
;
1780 ftdi
->command_next
+= 1;
1781 ftdi_elan_kick_command_queue(ftdi
);
1783 mutex_unlock(&ftdi
->u132_lock
);
1785 mutex_lock(&ftdi
->u132_lock
);
1789 mutex_unlock(&ftdi
->u132_lock
);
1795 int usb_ftdi_elan_edset_flush(struct platform_device
*pdev
, u8 ed_number
,
1798 struct usb_ftdi
*ftdi
= platform_device_to_usb_ftdi(pdev
);
1799 return ftdi_elan_edset_flush(ftdi
, ed_number
, endp
);
1803 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush
);
1804 static int ftdi_elan_flush_input_fifo(struct usb_ftdi
*ftdi
)
1806 int retry_on_empty
= 10;
1807 int retry_on_timeout
= 5;
1808 int retry_on_status
= 20;
1810 int packet_bytes
= 0;
1811 int retval
= usb_bulk_msg(ftdi
->udev
,
1812 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
1813 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
1814 &packet_bytes
, 100);
1815 if (packet_bytes
> 2) {
1816 char diag
[30 *3 + 4];
1818 int m
= (sizeof(diag
) - 1) / 3 - 1;
1819 char *b
= ftdi
->bulk_in_buffer
;
1822 while (packet_bytes
-- > 0) {
1824 if (bytes_read
< m
) {
1825 d
+= sprintf(d
, " %02X",
1827 } else if (bytes_read
> m
) {
1829 d
+= sprintf(d
, " ..");
1834 } else if (packet_bytes
> 1) {
1835 char s1
= ftdi
->bulk_in_buffer
[0];
1836 char s2
= ftdi
->bulk_in_buffer
[1];
1837 if (s1
== 0x31 && s2
== 0x60) {
1839 } else if (retry_on_status
-- > 0) {
1842 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry limit reached\n");
1845 } else if (packet_bytes
> 0) {
1846 char b1
= ftdi
->bulk_in_buffer
[0];
1847 dev_err(&ftdi
->udev
->dev
, "only one byte flushed from FTDI = %02X\n",
1849 if (retry_on_status
-- > 0) {
1852 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry limit reached\n");
1855 } else if (retval
== -ETIMEDOUT
) {
1856 if (retry_on_timeout
-- > 0) {
1859 dev_err(&ftdi
->udev
->dev
, "TIMED OUT retry limit reached\n");
1862 } else if (retval
== 0) {
1863 if (retry_on_empty
-- > 0) {
1866 dev_err(&ftdi
->udev
->dev
, "empty packet retry limit reached\n");
1870 dev_err(&ftdi
->udev
->dev
, "error = %d\n", retval
);
1879 * send the long flush sequence
1882 static int ftdi_elan_synchronize_flush(struct usb_ftdi
*ftdi
)
1889 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1892 buf
= usb_alloc_coherent(ftdi
->udev
, I
, GFP_KERNEL
, &urb
->transfer_dma
);
1894 dev_err(&ftdi
->udev
->dev
, "could not get a buffer for flush sequence\n");
1900 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
1901 ftdi
->bulk_out_endpointAddr
), buf
, i
,
1902 ftdi_elan_write_bulk_callback
, ftdi
);
1903 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1904 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
1906 dev_err(&ftdi
->udev
->dev
, "failed to submit urb containing the flush sequence\n");
1907 usb_free_coherent(ftdi
->udev
, i
, buf
, urb
->transfer_dma
);
1917 * send the reset sequence
1920 static int ftdi_elan_synchronize_reset(struct usb_ftdi
*ftdi
)
1927 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1930 buf
= usb_alloc_coherent(ftdi
->udev
, I
, GFP_KERNEL
, &urb
->transfer_dma
);
1932 dev_err(&ftdi
->udev
->dev
, "could not get a buffer for the reset sequence\n");
1940 usb_fill_bulk_urb(urb
, ftdi
->udev
, usb_sndbulkpipe(ftdi
->udev
,
1941 ftdi
->bulk_out_endpointAddr
), buf
, i
,
1942 ftdi_elan_write_bulk_callback
, ftdi
);
1943 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1944 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
1946 dev_err(&ftdi
->udev
->dev
, "failed to submit urb containing the reset sequence\n");
1947 usb_free_coherent(ftdi
->udev
, i
, buf
, urb
->transfer_dma
);
1955 static int ftdi_elan_synchronize(struct usb_ftdi
*ftdi
)
1959 int retry_on_timeout
= 5;
1960 int retry_on_empty
= 10;
1962 retval
= ftdi_elan_flush_input_fifo(ftdi
);
1965 ftdi
->bulk_in_left
= 0;
1966 ftdi
->bulk_in_last
= -1;
1967 while (long_stop
-- > 0) {
1970 retval
= ftdi_elan_synchronize_flush(ftdi
);
1973 retval
= ftdi_elan_flush_input_fifo(ftdi
);
1976 reset
:retval
= ftdi_elan_synchronize_reset(ftdi
);
1982 int packet_bytes
= 0;
1983 retval
= usb_bulk_msg(ftdi
->udev
,
1984 usb_rcvbulkpipe(ftdi
->udev
,
1985 ftdi
->bulk_in_endpointAddr
),
1986 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
1987 &packet_bytes
, 500);
1988 if (packet_bytes
> 2) {
1989 char diag
[30 *3 + 4];
1991 int m
= (sizeof(diag
) - 1) / 3 - 1;
1992 char *b
= ftdi
->bulk_in_buffer
;
1994 unsigned char c
= 0;
1996 while (packet_bytes
-- > 0) {
1998 if (bytes_read
< m
) {
1999 d
+= sprintf(d
, " %02X", c
);
2000 } else if (bytes_read
> m
) {
2002 d
+= sprintf(d
, " ..");
2011 } else if (read_stop
-- > 0) {
2014 dev_err(&ftdi
->udev
->dev
, "retry limit reached\n");
2018 } else if (packet_bytes
> 1) {
2019 unsigned char s1
= ftdi
->bulk_in_buffer
[0];
2020 unsigned char s2
= ftdi
->bulk_in_buffer
[1];
2021 if (s1
== 0x31 && s2
== 0x00) {
2022 if (read_stuck
-- > 0) {
2027 if (read_stop
-- > 0) {
2030 dev_err(&ftdi
->udev
->dev
, "retry limit reached\n");
2034 } else if (packet_bytes
> 0) {
2035 if (read_stop
-- > 0) {
2038 dev_err(&ftdi
->udev
->dev
, "retry limit reached\n");
2041 } else if (retval
== -ETIMEDOUT
) {
2042 if (retry_on_timeout
-- > 0) {
2045 dev_err(&ftdi
->udev
->dev
, "TIMED OUT retry limit reached\n");
2048 } else if (retval
== 0) {
2049 if (retry_on_empty
-- > 0) {
2052 dev_err(&ftdi
->udev
->dev
, "empty packet retry limit reached\n");
2057 dev_err(&ftdi
->udev
->dev
, "error = %d\n",
2059 if (read_stop
-- > 0) {
2062 dev_err(&ftdi
->udev
->dev
, "retry limit reached\n");
2068 dev_err(&ftdi
->udev
->dev
, "failed to synchronize\n");
2072 static int ftdi_elan_stuck_waiting(struct usb_ftdi
*ftdi
)
2074 int retry_on_empty
= 10;
2075 int retry_on_timeout
= 5;
2076 int retry_on_status
= 50;
2078 int packet_bytes
= 0;
2079 int retval
= usb_bulk_msg(ftdi
->udev
,
2080 usb_rcvbulkpipe(ftdi
->udev
, ftdi
->bulk_in_endpointAddr
),
2081 ftdi
->bulk_in_buffer
, ftdi
->bulk_in_size
,
2082 &packet_bytes
, 1000);
2083 if (packet_bytes
> 2) {
2084 char diag
[30 *3 + 4];
2086 int m
= (sizeof(diag
) - 1) / 3 - 1;
2087 char *b
= ftdi
->bulk_in_buffer
;
2090 while (packet_bytes
-- > 0) {
2092 if (bytes_read
< m
) {
2093 d
+= sprintf(d
, " %02X",
2095 } else if (bytes_read
> m
) {
2097 d
+= sprintf(d
, " ..");
2102 } else if (packet_bytes
> 1) {
2103 char s1
= ftdi
->bulk_in_buffer
[0];
2104 char s2
= ftdi
->bulk_in_buffer
[1];
2105 if (s1
== 0x31 && s2
== 0x60) {
2107 } else if (retry_on_status
-- > 0) {
2112 } else if (packet_bytes
> 0) {
2113 char b1
= ftdi
->bulk_in_buffer
[0];
2114 dev_err(&ftdi
->udev
->dev
, "only one byte flushed from FTDI = %02X\n", b1
);
2115 if (retry_on_status
-- > 0) {
2119 dev_err(&ftdi
->udev
->dev
, "STATUS ERROR retry limit reached\n");
2122 } else if (retval
== -ETIMEDOUT
) {
2123 if (retry_on_timeout
-- > 0) {
2126 dev_err(&ftdi
->udev
->dev
, "TIMED OUT retry limit reached\n");
2129 } else if (retval
== 0) {
2130 if (retry_on_empty
-- > 0) {
2133 dev_err(&ftdi
->udev
->dev
, "empty packet retry limit reached\n");
2137 dev_err(&ftdi
->udev
->dev
, "error = %d\n", retval
);
2144 static int ftdi_elan_checkingPCI(struct usb_ftdi
*ftdi
)
2146 int UxxxStatus
= ftdi_elan_read_reg(ftdi
, &ftdi
->controlreg
);
2149 if (ftdi
->controlreg
& 0x00400000) {
2150 if (ftdi
->card_ejected
) {
2152 ftdi
->card_ejected
= 1;
2153 dev_err(&ftdi
->udev
->dev
, "CARD EJECTED - controlreg = %08X\n",
2158 u8 fn
= ftdi
->function
- 1;
2159 int activePCIfn
= fn
<< 8;
2164 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2168 pciVID
= pcidata
& 0xFFFF;
2169 pciPID
= (pcidata
>> 16) & 0xFFFF;
2170 if (pciVID
== ftdi
->platform_data
.vendor
&& pciPID
==
2171 ftdi
->platform_data
.device
) {
2174 dev_err(&ftdi
->udev
->dev
, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2175 ftdi
->platform_data
.vendor
, pciVID
,
2176 ftdi
->platform_data
.device
, pciPID
);
2183 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2184 offsetof(struct ohci_regs, member), 0, data);
2185 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2186 offsetof(struct ohci_regs, member), 0, data);
2188 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2189 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2191 static int ftdi_elan_check_controller(struct usb_ftdi
*ftdi
, int quirk
)
2205 int mask
= OHCI_INTR_INIT
;
2207 int reset_timeout
= 30; /* ... allow extra time */
2209 retval
= ftdi_write_pcimem(ftdi
, intrdisable
, OHCI_INTR_MIE
);
2212 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2215 retval
= ftdi_read_pcimem(ftdi
, roothub
.a
, &rh_a
);
2218 num_ports
= rh_a
& RH_A_NDP
;
2219 retval
= ftdi_read_pcimem(ftdi
, fminterval
, &hc_fminterval
);
2222 hc_fminterval
&= 0x3fff;
2223 if (hc_fminterval
!= FI
) {
2225 hc_fminterval
|= FSMP(hc_fminterval
) << 16;
2226 retval
= ftdi_read_pcimem(ftdi
, control
, &hc_control
);
2229 switch (hc_control
& OHCI_CTRL_HCFS
) {
2233 case OHCI_USB_SUSPEND
:
2234 case OHCI_USB_RESUME
:
2235 hc_control
&= OHCI_CTRL_RWC
;
2236 hc_control
|= OHCI_USB_RESUME
;
2240 hc_control
&= OHCI_CTRL_RWC
;
2241 hc_control
|= OHCI_USB_RESET
;
2245 retval
= ftdi_write_pcimem(ftdi
, control
, hc_control
);
2248 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2252 retval
= ftdi_read_pcimem(ftdi
, roothub
.a
, &roothub_a
);
2255 if (!(roothub_a
& RH_A_NPS
)) { /* power down each port */
2256 for (temp
= 0; temp
< num_ports
; temp
++) {
2257 retval
= ftdi_write_pcimem(ftdi
,
2258 roothub
.portstatus
[temp
], RH_PS_LSDA
);
2263 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2266 retry
:retval
= ftdi_read_pcimem(ftdi
, cmdstatus
, &status
);
2269 retval
= ftdi_write_pcimem(ftdi
, cmdstatus
, OHCI_HCR
);
2273 retval
= ftdi_read_pcimem(ftdi
, cmdstatus
, &status
);
2276 if (0 != (status
& OHCI_HCR
)) {
2277 if (--reset_timeout
== 0) {
2278 dev_err(&ftdi
->udev
->dev
, "USB HC reset timed out!\n");
2286 if (quirk
& OHCI_QUIRK_INITRESET
) {
2287 retval
= ftdi_write_pcimem(ftdi
, control
, hc_control
);
2290 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2294 retval
= ftdi_write_pcimem(ftdi
, ed_controlhead
, 0x00000000);
2297 retval
= ftdi_write_pcimem(ftdi
, ed_bulkhead
, 0x11000000);
2300 retval
= ftdi_write_pcimem(ftdi
, hcca
, 0x00000000);
2303 retval
= ftdi_read_pcimem(ftdi
, fminterval
, &fminterval
);
2306 retval
= ftdi_write_pcimem(ftdi
, fminterval
,
2307 ((fminterval
& FIT
) ^ FIT
) | hc_fminterval
);
2310 retval
= ftdi_write_pcimem(ftdi
, periodicstart
,
2311 ((9 *hc_fminterval
) / 10) & 0x3fff);
2314 retval
= ftdi_read_pcimem(ftdi
, fminterval
, &fminterval
);
2317 retval
= ftdi_read_pcimem(ftdi
, periodicstart
, &periodicstart
);
2320 if (0 == (fminterval
& 0x3fff0000) || 0 == periodicstart
) {
2321 if (!(quirk
& OHCI_QUIRK_INITRESET
)) {
2322 quirk
|= OHCI_QUIRK_INITRESET
;
2325 dev_err(&ftdi
->udev
->dev
, "init err(%08x %04x)\n",
2326 fminterval
, periodicstart
);
2327 } /* start controller operations */
2328 hc_control
&= OHCI_CTRL_RWC
;
2329 hc_control
|= OHCI_CONTROL_INIT
| OHCI_CTRL_BLE
| OHCI_USB_OPER
;
2330 retval
= ftdi_write_pcimem(ftdi
, control
, hc_control
);
2333 retval
= ftdi_write_pcimem(ftdi
, cmdstatus
, OHCI_BLF
);
2336 retval
= ftdi_read_pcimem(ftdi
, cmdstatus
, &cmdstatus
);
2339 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2342 retval
= ftdi_write_pcimem(ftdi
, roothub
.status
, RH_HS_DRWE
);
2345 retval
= ftdi_write_pcimem(ftdi
, intrstatus
, mask
);
2348 retval
= ftdi_write_pcimem(ftdi
, intrdisable
,
2349 OHCI_INTR_MIE
| OHCI_INTR_OC
| OHCI_INTR_RHSC
| OHCI_INTR_FNO
|
2350 OHCI_INTR_UE
| OHCI_INTR_RD
| OHCI_INTR_SF
| OHCI_INTR_WDH
|
2353 return retval
; /* handle root hub init quirks ... */
2354 retval
= ftdi_read_pcimem(ftdi
, roothub
.a
, &roothub_a
);
2357 roothub_a
&= ~(RH_A_PSM
| RH_A_OCPM
);
2358 if (quirk
& OHCI_QUIRK_SUPERIO
) {
2359 roothub_a
|= RH_A_NOCP
;
2360 roothub_a
&= ~(RH_A_POTPGT
| RH_A_NPS
);
2361 retval
= ftdi_write_pcimem(ftdi
, roothub
.a
, roothub_a
);
2364 } else if ((quirk
& OHCI_QUIRK_AMD756
) || distrust_firmware
) {
2365 roothub_a
|= RH_A_NPS
;
2366 retval
= ftdi_write_pcimem(ftdi
, roothub
.a
, roothub_a
);
2370 retval
= ftdi_write_pcimem(ftdi
, roothub
.status
, RH_HS_LPSC
);
2373 retval
= ftdi_write_pcimem(ftdi
, roothub
.b
,
2374 (roothub_a
& RH_A_NPS
) ? 0 : RH_B_PPCM
);
2377 retval
= ftdi_read_pcimem(ftdi
, control
, &control
);
2380 mdelay((roothub_a
>> 23) & 0x1fe);
2381 for (temp
= 0; temp
< num_ports
; temp
++) {
2383 retval
= ftdi_read_pcimem(ftdi
, roothub
.portstatus
[temp
],
2393 static int ftdi_elan_setup_controller(struct usb_ftdi
*ftdi
, int fn
)
2399 int activePCIfn
= fn
<< 8;
2400 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000025FL
| 0x2800);
2404 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2408 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2412 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2416 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2421 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2425 latence_timer
&= 0xFFFF00FF;
2426 latence_timer
|= 0x00001600;
2427 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2431 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2436 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2440 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2444 for (reg
= 0; reg
<= 0x54; reg
+= 4) {
2445 UxxxStatus
= ftdi_elan_read_pcimem(ftdi
, reg
, 0, &pcidata
);
2452 static int ftdi_elan_close_controller(struct usb_ftdi
*ftdi
, int fn
)
2458 int activePCIfn
= fn
<< 8;
2459 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000025FL
| 0x2800);
2463 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2467 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2471 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0,
2475 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2480 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2484 latence_timer
&= 0xFFFF00FF;
2485 latence_timer
|= 0x00001600;
2486 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2490 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2495 UxxxStatus
= ftdi_elan_write_config(ftdi
, activePCIfn
| reg
, 0x00,
2499 return ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0, &pcidata
);
2502 static int ftdi_elan_found_controller(struct usb_ftdi
*ftdi
, int fn
, int quirk
)
2506 UxxxStatus
= ftdi_elan_setup_controller(ftdi
, fn
);
2509 result
= ftdi_elan_check_controller(ftdi
, quirk
);
2510 UxxxStatus
= ftdi_elan_close_controller(ftdi
, fn
);
2516 static int ftdi_elan_enumeratePCI(struct usb_ftdi
*ftdi
)
2521 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2524 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000000L
);
2528 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000200L
| 0x100);
2531 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x00000200L
| 0x500);
2534 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2537 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020CL
| 0x000);
2540 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020DL
| 0x000);
2544 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000020FL
| 0x000);
2547 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2550 UxxxStatus
= ftdi_elan_write_reg(ftdi
, 0x0000025FL
| 0x800);
2553 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2556 UxxxStatus
= ftdi_elan_read_reg(ftdi
, &controlreg
);
2560 sensebits
= (controlreg
>> 16) & 0x000F;
2561 if (0x0D == sensebits
)
2567 static int ftdi_elan_setupOHCI(struct usb_ftdi
*ftdi
)
2573 int activePCIfn
= 0;
2574 int max_devices
= 0;
2575 int controllers
= 0;
2576 int unrecognized
= 0;
2578 for (fn
= 0; (fn
< 4); fn
++) {
2582 activePCIfn
= fn
<< 8;
2583 UxxxStatus
= ftdi_elan_read_config(ftdi
, activePCIfn
| reg
, 0,
2587 pciVID
= pcidata
& 0xFFFF;
2588 pciPID
= (pcidata
>> 16) & 0xFFFF;
2589 if ((pciVID
== PCI_VENDOR_ID_OPTI
) && (pciPID
== 0xc861)) {
2590 devices
= ftdi_elan_found_controller(ftdi
, fn
, 0);
2592 } else if ((pciVID
== PCI_VENDOR_ID_NEC
) && (pciPID
== 0x0035))
2594 devices
= ftdi_elan_found_controller(ftdi
, fn
, 0);
2596 } else if ((pciVID
== PCI_VENDOR_ID_AL
) && (pciPID
== 0x5237)) {
2597 devices
= ftdi_elan_found_controller(ftdi
, fn
, 0);
2599 } else if ((pciVID
== PCI_VENDOR_ID_ATT
) && (pciPID
== 0x5802))
2601 devices
= ftdi_elan_found_controller(ftdi
, fn
, 0);
2603 } else if (pciVID
== PCI_VENDOR_ID_AMD
&& pciPID
== 0x740c) {
2604 devices
= ftdi_elan_found_controller(ftdi
, fn
,
2607 } else if (pciVID
== PCI_VENDOR_ID_COMPAQ
&& pciPID
== 0xa0f8) {
2608 devices
= ftdi_elan_found_controller(ftdi
, fn
,
2609 OHCI_QUIRK_ZFMICRO
);
2611 } else if (0 == pcidata
) {
2614 if (devices
> max_devices
) {
2615 max_devices
= devices
;
2616 ftdi
->function
= fn
+ 1;
2617 ftdi
->platform_data
.vendor
= pciVID
;
2618 ftdi
->platform_data
.device
= pciPID
;
2621 if (ftdi
->function
> 0) {
2622 return ftdi_elan_setup_controller(ftdi
, ftdi
->function
- 1);
2623 } else if (controllers
> 0) {
2625 } else if (unrecognized
> 0) {
2628 ftdi
->enumerated
= 0;
2635 * we use only the first bulk-in and bulk-out endpoints
2637 static int ftdi_elan_probe(struct usb_interface
*interface
,
2638 const struct usb_device_id
*id
)
2640 struct usb_host_interface
*iface_desc
;
2641 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
;
2643 struct usb_ftdi
*ftdi
;
2645 ftdi
= kzalloc(sizeof(struct usb_ftdi
), GFP_KERNEL
);
2649 mutex_lock(&ftdi_module_lock
);
2650 list_add_tail(&ftdi
->ftdi_list
, &ftdi_static_list
);
2651 ftdi
->sequence_num
= ++ftdi_instances
;
2652 mutex_unlock(&ftdi_module_lock
);
2653 ftdi_elan_init_kref(ftdi
);
2654 sema_init(&ftdi
->sw_lock
, 1);
2655 ftdi
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2656 ftdi
->interface
= interface
;
2657 mutex_init(&ftdi
->u132_lock
);
2660 iface_desc
= interface
->cur_altsetting
;
2661 retval
= usb_find_common_endpoints(iface_desc
,
2662 &bulk_in
, &bulk_out
, NULL
, NULL
);
2664 dev_err(&ftdi
->udev
->dev
, "Could not find both bulk-in and bulk-out endpoints\n");
2668 ftdi
->bulk_in_size
= usb_endpoint_maxp(bulk_in
);
2669 ftdi
->bulk_in_endpointAddr
= bulk_in
->bEndpointAddress
;
2670 ftdi
->bulk_in_buffer
= kmalloc(ftdi
->bulk_in_size
, GFP_KERNEL
);
2671 if (!ftdi
->bulk_in_buffer
) {
2676 ftdi
->bulk_out_endpointAddr
= bulk_out
->bEndpointAddress
;
2678 dev_info(&ftdi
->udev
->dev
, "interface %d has I=%02X O=%02X\n",
2679 iface_desc
->desc
.bInterfaceNumber
, ftdi
->bulk_in_endpointAddr
,
2680 ftdi
->bulk_out_endpointAddr
);
2681 usb_set_intfdata(interface
, ftdi
);
2682 if (iface_desc
->desc
.bInterfaceNumber
== 0 &&
2683 ftdi
->bulk_in_endpointAddr
== 0x81 &&
2684 ftdi
->bulk_out_endpointAddr
== 0x02) {
2685 retval
= usb_register_dev(interface
, &ftdi_elan_jtag_class
);
2687 dev_err(&ftdi
->udev
->dev
, "Not able to get a minor for this device\n");
2688 usb_set_intfdata(interface
, NULL
);
2692 ftdi
->class = &ftdi_elan_jtag_class
;
2693 dev_info(&ftdi
->udev
->dev
, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2694 ftdi
, iface_desc
->desc
.bInterfaceNumber
,
2698 } else if (iface_desc
->desc
.bInterfaceNumber
== 1 &&
2699 ftdi
->bulk_in_endpointAddr
== 0x83 &&
2700 ftdi
->bulk_out_endpointAddr
== 0x04) {
2702 dev_info(&ftdi
->udev
->dev
, "USB FDTI=%p ELAN interface %d now activated\n",
2703 ftdi
, iface_desc
->desc
.bInterfaceNumber
);
2704 INIT_DELAYED_WORK(&ftdi
->status_work
, ftdi_elan_status_work
);
2705 INIT_DELAYED_WORK(&ftdi
->command_work
, ftdi_elan_command_work
);
2706 INIT_DELAYED_WORK(&ftdi
->respond_work
, ftdi_elan_respond_work
);
2707 ftdi_status_queue_work(ftdi
, msecs_to_jiffies(3 *1000));
2710 dev_err(&ftdi
->udev
->dev
,
2711 "Could not find ELAN's U132 device\n");
2716 ftdi_elan_put_kref(ftdi
);
2721 static void ftdi_elan_disconnect(struct usb_interface
*interface
)
2723 struct usb_ftdi
*ftdi
= usb_get_intfdata(interface
);
2724 ftdi
->disconnected
+= 1;
2726 int minor
= interface
->minor
;
2727 struct usb_class_driver
*class = ftdi
->class;
2728 usb_set_intfdata(interface
, NULL
);
2729 usb_deregister_dev(interface
, class);
2730 dev_info(&ftdi
->udev
->dev
, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2733 ftdi_status_cancel_work(ftdi
);
2734 ftdi_command_cancel_work(ftdi
);
2735 ftdi_response_cancel_work(ftdi
);
2736 ftdi_elan_abandon_completions(ftdi
);
2737 ftdi_elan_abandon_targets(ftdi
);
2738 if (ftdi
->registered
) {
2739 platform_device_unregister(&ftdi
->platform_dev
);
2740 ftdi
->synchronized
= 0;
2741 ftdi
->enumerated
= 0;
2742 ftdi
->initialized
= 0;
2743 ftdi
->registered
= 0;
2745 ftdi
->disconnected
+= 1;
2746 usb_set_intfdata(interface
, NULL
);
2747 dev_info(&ftdi
->udev
->dev
, "USB FTDI U132 host controller interface now disconnected\n");
2749 ftdi_elan_put_kref(ftdi
);
2752 static struct usb_driver ftdi_elan_driver
= {
2753 .name
= "ftdi-elan",
2754 .probe
= ftdi_elan_probe
,
2755 .disconnect
= ftdi_elan_disconnect
,
2756 .id_table
= ftdi_elan_table
,
2758 static int __init
ftdi_elan_init(void)
2761 pr_info("driver %s\n", ftdi_elan_driver
.name
);
2762 mutex_init(&ftdi_module_lock
);
2763 INIT_LIST_HEAD(&ftdi_static_list
);
2764 result
= usb_register(&ftdi_elan_driver
);
2766 pr_err("usb_register failed. Error number %d\n", result
);
2772 static void __exit
ftdi_elan_exit(void)
2774 struct usb_ftdi
*ftdi
;
2775 struct usb_ftdi
*temp
;
2776 usb_deregister(&ftdi_elan_driver
);
2777 pr_info("ftdi_u132 driver deregistered\n");
2778 list_for_each_entry_safe(ftdi
, temp
, &ftdi_static_list
, ftdi_list
) {
2779 ftdi_status_cancel_work(ftdi
);
2780 ftdi_command_cancel_work(ftdi
);
2781 ftdi_response_cancel_work(ftdi
);
2786 module_init(ftdi_elan_init
);
2787 module_exit(ftdi_elan_exit
);