38 #include <linux/kernel.h>
39 #include <linux/errno.h>
41 #include <linux/list.h>
44 #include <linux/slab.h>
45 #include <linux/module.h>
48 #include <asm/uaccess.h>
55 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
56 static bool distrust_firmware = 1;
58 MODULE_PARM_DESC(distrust_firmware,
"true to distrust firmware power/overcurren"
68 static struct mutex ftdi_module_lock;
69 static int ftdi_instances = 0;
84 #include "../host/ohci.h"
86 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
87 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
106 #define USB_FTDI_ELAN_MINOR_BASE 192
107 #define COMMAND_BITS 5
108 #define COMMAND_SIZE (1<<COMMAND_BITS)
109 #define COMMAND_MASK (COMMAND_SIZE-1)
119 #define RESPOND_BITS 5
120 #define RESPOND_SIZE (1<<RESPOND_BITS)
121 #define RESPOND_MASK (RESPOND_SIZE-1)
159 unsigned synchronized:1;
173 struct usb_class_driver *
class;
193 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
194 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
196 static struct usb_driver ftdi_elan_driver;
197 static void ftdi_elan_delete(
struct kref *
kref)
211 static void ftdi_elan_put_kref(
struct usb_ftdi *ftdi)
213 kref_put(&ftdi->
kref, ftdi_elan_delete);
216 static void ftdi_elan_get_kref(
struct usb_ftdi *ftdi)
218 kref_get(&ftdi->
kref);
221 static void ftdi_elan_init_kref(
struct usb_ftdi *ftdi)
223 kref_init(&ftdi->
kref);
226 static void ftdi_status_requeue_work(
struct usb_ftdi *ftdi,
unsigned int delta)
229 kref_put(&ftdi->
kref, ftdi_elan_delete);
232 static void ftdi_status_queue_work(
struct usb_ftdi *ftdi,
unsigned int delta)
235 kref_get(&ftdi->
kref);
238 static void ftdi_status_cancel_work(
struct usb_ftdi *ftdi)
241 kref_put(&ftdi->
kref, ftdi_elan_delete);
244 static void ftdi_command_requeue_work(
struct usb_ftdi *ftdi,
unsigned int delta)
247 kref_put(&ftdi->
kref, ftdi_elan_delete);
250 static void ftdi_command_queue_work(
struct usb_ftdi *ftdi,
unsigned int delta)
253 kref_get(&ftdi->
kref);
256 static void ftdi_command_cancel_work(
struct usb_ftdi *ftdi)
259 kref_put(&ftdi->
kref, ftdi_elan_delete);
262 static void ftdi_response_requeue_work(
struct usb_ftdi *ftdi,
266 kref_put(&ftdi->
kref, ftdi_elan_delete);
269 static void ftdi_respond_queue_work(
struct usb_ftdi *ftdi,
unsigned int delta)
272 kref_get(&ftdi->
kref);
275 static void ftdi_response_cancel_work(
struct usb_ftdi *ftdi)
278 kref_put(&ftdi->
kref, ftdi_elan_delete);
285 ftdi_elan_put_kref(ftdi);
290 static void ftdi_release_platform_dev(
struct device *
dev)
295 static void ftdi_elan_do_callback(
struct usb_ftdi *ftdi,
297 static void ftdi_elan_kick_command_queue(
struct usb_ftdi *ftdi);
298 static void ftdi_elan_kick_respond_queue(
struct usb_ftdi *ftdi);
299 static int ftdi_elan_setupOHCI(
struct usb_ftdi *ftdi);
300 static int ftdi_elan_checkingPCI(
struct usb_ftdi *ftdi);
301 static int ftdi_elan_enumeratePCI(
struct usb_ftdi *ftdi);
302 static int ftdi_elan_synchronize(
struct usb_ftdi *ftdi);
303 static int ftdi_elan_stuck_waiting(
struct usb_ftdi *ftdi);
304 static int ftdi_elan_command_engine(
struct usb_ftdi *ftdi);
305 static int ftdi_elan_respond_engine(
struct usb_ftdi *ftdi);
306 static int ftdi_elan_hcd_init(
struct usb_ftdi *ftdi)
311 ftdi_elan_get_kref(ftdi);
319 ftdi->
platform_dev.dev.release = ftdi_release_platform_dev;
323 dev_info(&ftdi->
udev->dev,
"requesting module '%s'\n",
"u132_hcd");
324 request_module(
"u132_hcd");
331 static void ftdi_elan_abandon_completions(
struct usb_ftdi *ftdi)
343 static void ftdi_elan_abandon_targets(
struct usb_ftdi *ftdi)
347 while (ed_number-- > 0) {
349 if (target->
active == 1) {
352 ftdi_elan_do_callback(ftdi, target,
NULL, 0);
362 static void ftdi_elan_flush_targets(
struct usb_ftdi *ftdi)
366 while (ed_number-- > 0) {
369 wait_1:
if (target->
active == 1) {
375 command->
header = 0x80 | (ed_number << 5) | 0x4;
378 command->
width = 0x00;
383 ftdi_elan_kick_command_queue(ftdi);
391 wait_2:
if (target->
active == 1) {
397 command->
header = 0x90 | (ed_number << 5);
400 command->
width = 0x00;
405 ftdi_elan_kick_command_queue(ftdi);
420 static void ftdi_elan_cancel_targets(
struct usb_ftdi *ftdi)
424 while (ed_number-- > 0) {
433 command->
header = 0x80 | (ed_number << 5) | 0x4;
436 command->
width = 0x00;
441 ftdi_elan_kick_command_queue(ftdi);
456 static void ftdi_elan_kick_command_queue(
struct usb_ftdi *ftdi)
458 ftdi_command_queue_work(ftdi, 0);
467 ftdi_elan_put_kref(ftdi);
470 int retval = ftdi_elan_command_engine(ftdi);
473 }
else if (retval == -
ENODEV) {
476 dev_err(&ftdi->
udev->dev,
"command error %d\n", retval);
482 static void ftdi_elan_kick_respond_queue(
struct usb_ftdi *ftdi)
484 ftdi_respond_queue_work(ftdi, 0);
487 static void ftdi_elan_respond_work(
struct work_struct *work)
492 ftdi_elan_put_kref(ftdi);
495 int retval = ftdi_elan_respond_engine(ftdi);
499 }
else if (retval == -
ENODEV) {
501 }
else if (retval == -
EILSEQ) {
505 dev_err(&ftdi->
udev->dev,
"respond error %d\n", retval);
508 ftdi_elan_abandon_completions(ftdi);
509 ftdi_elan_abandon_targets(ftdi);
522 static void ftdi_elan_status_work(
struct work_struct *work)
526 int work_delay_in_msec = 0;
528 ftdi_elan_put_kref(ftdi);
532 if (ftdi_elan_synchronize(ftdi) == 0) {
534 ftdi_command_queue_work(ftdi, 1);
535 ftdi_respond_queue_work(ftdi, 1);
537 work_delay_in_msec = 100;
541 work_delay_in_msec = 10 *1000;
544 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
548 dev_err(&ftdi->
udev->dev,
"WRONG type of card inserted "
549 "- please remove\n");
551 dev_err(&ftdi->
udev->dev,
"WRONG type of card inserted "
553 work_delay_in_msec = 100;
555 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
557 work_delay_in_msec = 250;
559 work_delay_in_msec = 1000;
561 if (ftdi_elan_setupOHCI(ftdi) == 0) {
563 work_delay_in_msec = 500;
565 dev_err(&ftdi->
udev->dev,
"initialized failed - trying "
566 "again in 10 seconds\n");
567 work_delay_in_msec = 1 *1000;
570 work_delay_in_msec = 10;
571 if (ftdi_elan_hcd_init(ftdi) == 0) {
575 work_delay_in_msec = 250;
577 if (ftdi_elan_checkingPCI(ftdi) == 0) {
578 work_delay_in_msec = 250;
582 "firmed platform_dev.dev.parent=%p plat"
594 ftdi_elan_flush_targets(ftdi);
595 work_delay_in_msec = 250;
597 dev_err(&ftdi->
udev->dev,
"PCI device has disappeared\n"
599 ftdi_elan_cancel_targets(ftdi);
600 work_delay_in_msec = 500;
606 ftdi_elan_put_kref(ftdi);
609 ftdi_status_requeue_work(ftdi,
627 subminor = iminor(inode);
634 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
641 ftdi_elan_get_kref(ftdi);
649 static int ftdi_elan_release(
struct inode *inode,
struct file *file)
655 ftdi_elan_put_kref(ftdi);
665 static ssize_t ftdi_elan_read(
struct file *file,
char __user *
buffer,
666 size_t count, loff_t *ppos)
668 char data[30 *3 + 4];
670 int m = (
sizeof(
data) - 1) / 3;
672 int retry_on_empty = 10;
673 int retry_on_timeout = 5;
683 if (bytes_read < m) {
684 d +=
sprintf(d,
" %02X", 0x000000FF & *p);
685 }
else if (bytes_read > m) {
697 more:
if (count > 0) {
698 int packet_bytes = 0;
703 if (packet_bytes > 2) {
708 if (retry_on_timeout-- > 0) {
710 }
else if (bytes_read > 0) {
714 }
else if (retval == 0) {
715 if (retry_on_empty-- > 0) {
725 static void ftdi_elan_write_bulk_callback(
struct urb *
urb)
727 struct usb_ftdi *ftdi = urb->context;
732 dev_err(&ftdi->
udev->dev,
"urb=%p write bulk status received: %"
736 urb->transfer_buffer, urb->transfer_dma);
739 static int fill_buffer_with_all_queued_commands(
struct usb_ftdi *ftdi,
744 int I = command_size;
751 if (command->
header & 0x80) {
752 ed_commands |= 1 << (0x3 & (command->
header >> 5));
754 buf[b++] = command->
header;
755 buf[b++] = (command->
length >> 0) & 0x00FF;
756 buf[b++] = (command->
length >> 8) & 0x00FF;
758 buf[b++] = command->
width;
766 static int ftdi_elan_total_command_size(
struct usb_ftdi *ftdi,
int command_size)
769 int I = command_size;
774 total_size += 5 + command->
follows;
778 static int ftdi_elan_command_engine(
struct usb_ftdi *ftdi)
786 if (command_size == 0)
788 total_size = ftdi_elan_total_command_size(ftdi, command_size);
791 dev_err(&ftdi->
udev->dev,
"could not get a urb to write %d comm"
792 "ands totaling %d bytes to the Uxxx\n", command_size,
799 dev_err(&ftdi->
udev->dev,
"could not get a buffer to write %d c"
800 "ommands totaling %d bytes to the Uxxx\n", command_size,
805 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
806 command_size, total_size);
807 usb_fill_bulk_urb(urb, ftdi->
udev, usb_sndbulkpipe(ftdi->
udev,
809 ftdi_elan_write_bulk_callback, ftdi);
810 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
812 char diag[40 *3 + 4];
816 int s = (
sizeof(
diag) - 1) / 3;
818 while (s-- > 0 && m-- > 0) {
819 if (s > 0 || m == 0) {
820 d +=
sprintf(d,
" %02X", *c++);
827 dev_err(&ftdi->
udev->dev,
"failed %d to submit urb %p to write "
828 "%d commands totaling %d bytes to the Uxxx\n", retval,
829 urb, command_size, total_size);
837 ftdi_elan_kick_respond_queue(ftdi);
841 static void ftdi_elan_do_callback(
struct usb_ftdi *ftdi,
844 struct urb *urb = target->
urb;
845 int halted = target->
halted;
847 int actual = target->
actual;
858 error_count, condition_code, repeat_number, halted, skipped,
862 static char *have_ed_set_response(
struct usb_ftdi *ftdi,
863 struct u132_target *target,
u16 ed_length,
int ed_number,
int ed_type,
866 int payload = (ed_length >> 0) & 0x07FF;
869 target->
non_null = (ed_length >> 15) & 0x0001;
871 if (ed_type == 0x02) {
875 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->
response,
887 }
else if (ed_type == 0x03) {
891 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->
response,
903 }
else if (ed_type == 0x01) {
906 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->
response,
915 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->
response,
924 static char *have_ed_get_response(
struct usb_ftdi *ftdi,
925 struct u132_target *target,
u16 ed_length,
int ed_number,
int ed_type,
930 target->
actual = (ed_length >> 0) & 0x01FF;
931 target->
non_null = (ed_length >> 15) & 0x0001;
935 ftdi_elan_do_callback(ftdi, target,
NULL, 0);
952 static int ftdi_elan_respond_engine(
struct usb_ftdi *ftdi)
956 int retry_on_empty = 1;
957 int retry_on_timeout = 3;
958 int empty_packets = 0;
960 int packet_bytes = 0;
965 char diag[30 *3 + 4];
967 int m = packet_bytes;
969 int s = (
sizeof(
diag) - 1) / 3;
971 while (s-- > 0 && m-- > 0) {
972 if (s > 0 || m == 0) {
973 d +=
sprintf(d,
" %02X", *c++);
977 if (packet_bytes > 2) {
982 if (retry_on_timeout-- > 0) {
984 "t_bytes = %d with total %d bytes%s\n",
985 packet_bytes, bytes_read, diag);
987 }
else if (bytes_read > 0) {
993 "t_bytes = %d with total %d bytes%s\n",
994 packet_bytes, bytes_read, diag);
997 }
else if (retval == -
EILSEQ) {
998 dev_err(&ftdi->
udev->dev,
"error = %d with packet_bytes"
999 " = %d with total %d bytes%s\n", retval,
1000 packet_bytes, bytes_read, diag);
1002 }
else if (retval) {
1003 dev_err(&ftdi->
udev->dev,
"error = %d with packet_bytes"
1004 " = %d with total %d bytes%s\n", retval,
1005 packet_bytes, bytes_read, diag);
1007 }
else if (packet_bytes == 2) {
1011 if (s0 == 0x31 && s1 == 0x60) {
1012 if (retry_on_empty-- > 0) {
1016 }
else if (s0 == 0x31 && s1 == 0x00) {
1017 if (retry_on_empty-- > 0) {
1022 if (retry_on_empty-- > 0) {
1027 }
else if (packet_bytes == 1) {
1028 if (retry_on_empty-- > 0) {
1033 if (retry_on_empty-- > 0) {
1046 if (ftdi->
received == 0 && c == 0xFF) {
1053 int ed_number = (ftdi->
response[0] >> 5) & 0x03;
1057 int payload = (ed_length >> 0) & 0x07FF;
1058 char diag[30 *3 + 4];
1062 int s = (
sizeof(
diag) - 1) / 3;
1064 while (s-- > 0 && m-- > 0) {
1065 if (s > 0 || m == 0) {
1066 d +=
sprintf(d,
" %02X", *c++);
1070 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->
response,
1096 buscmd = (ftdi->
response[0] >> 0) & 0x0F;
1097 if (buscmd == 0x00) {
1098 }
else if (buscmd == 0x02) {
1099 }
else if (buscmd == 0x06) {
1100 }
else if (buscmd == 0x0A) {
1103 "lue = %08X\n", buscmd, data);
1106 if ((ftdi->
response[0] & 0x80) == 0x00) {
1110 int ed_number = (ftdi->
response[0] >> 5) & 0x03;
1111 int ed_type = (ftdi->
response[0] >> 0) & 0x03;
1126 if ((ftdi->
response[0] & 0x10) == 0x00) {
1127 b = have_ed_set_response(ftdi, target,
1128 ed_length, ed_number, ed_type,
1132 b = have_ed_get_response(ftdi, target,
1133 ed_length, ed_number, ed_type,
1148 static ssize_t ftdi_elan_write(
struct file *file,
1149 const char __user *user_buffer,
size_t count,
1169 &urb->transfer_dma);
1178 usb_fill_bulk_urb(urb, ftdi->
udev, usb_sndbulkpipe(ftdi->
udev,
1180 ftdi_elan_write_bulk_callback, ftdi);
1181 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1184 dev_err(&ftdi->
udev->dev,
"failed submitting write urb, error %"
1203 .read = ftdi_elan_read,
1204 .write = ftdi_elan_write,
1205 .open = ftdi_elan_open,
1206 .release = ftdi_elan_release,
1213 static struct usb_class_driver ftdi_elan_jtag_class = {
1214 .name =
"ftdi-%d-jtag",
1215 .fops = &ftdi_elan_fops,
1224 #define cPCIu132rd 0x0
1225 #define cPCIu132wr 0x1
1226 #define cPCIiord 0x2
1227 #define cPCIiowr 0x3
1228 #define cPCImemrd 0x6
1229 #define cPCImemwr 0x7
1230 #define cPCIcfgrd 0xA
1231 #define cPCIcfgwr 0xB
1232 #define cPCInull 0xF
1233 #define cU132cmd_status 0x0
1234 #define cU132flash 0x1
1235 #define cPIDsetup 0x0
1238 #define cPIDinonce 0x3
1239 #define cCCnoerror 0x0
1241 #define cCCbitstuff 0x2
1242 #define cCCtoggle 0x3
1243 #define cCCstall 0x4
1244 #define cCCnoresp 0x5
1245 #define cCCbadpid1 0x6
1246 #define cCCbadpid2 0x7
1247 #define cCCdataoverrun 0x8
1248 #define cCCdataunderrun 0x9
1249 #define cCCbuffoverrun 0xC
1250 #define cCCbuffunderrun 0xD
1251 #define cCCnotaccessed 0xF
1252 static int ftdi_elan_write_reg(
struct usb_ftdi *ftdi,
u32 data)
1266 command->
width = 0x00;
1271 ftdi_elan_kick_command_queue(ftdi);
1285 u8 addressofs = config_offset / 4;
1297 command->
address = addressofs;
1298 command->
width = 0x00 | (width & 0x0F);
1303 ftdi_elan_kick_command_queue(ftdi);
1314 static int ftdi_elan_write_pcimem(
struct usb_ftdi *ftdi,
int mem_offset,
1317 u8 addressofs = mem_offset / 4;
1329 command->
address = addressofs;
1330 command->
width = 0x00 | (width & 0x0F);
1335 ftdi_elan_kick_command_queue(ftdi);
1350 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1355 static int ftdi_elan_read_reg(
struct usb_ftdi *ftdi,
u32 *data)
1376 command->
width = 0x00;
1384 ftdi_elan_kick_command_queue(ftdi);
1396 static int ftdi_elan_read_config(
struct usb_ftdi *ftdi,
int config_offset,
1397 u8 width,
u32 *data)
1399 u8 addressofs = config_offset / 4;
1420 command->
width = 0x00 | (width & 0x0F);
1428 ftdi_elan_kick_command_queue(ftdi);
1440 static int ftdi_elan_read_pcimem(
struct usb_ftdi *ftdi,
int mem_offset,
1441 u8 width,
u32 *data)
1443 u8 addressofs = mem_offset / 4;
1464 command->
width = 0x00 | (width & 0x0F);
1472 ftdi_elan_kick_command_queue(ftdi);
1485 u8 width,
u32 *data)
1491 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1496 static int ftdi_elan_edset_setup(
struct usb_ftdi *ftdi,
u8 ed_number,
1497 void *endp,
struct urb *urb,
u8 address,
u8 ep_number,
u8 toggle_bits,
1498 void (*
callback) (
void *endp,
struct urb *urb,
u8 *buf,
int len,
1499 int toggle_bits,
int error_count,
int condition_code,
int repeat_number,
1500 int halted,
int skipped,
int actual,
int non_null))
1502 u8 ed = ed_number - 1;
1515 command->
header = 0x80 | (ed << 5);
1516 command->
length = 0x8007;
1517 command->
address = (toggle_bits << 6) | (ep_number << 2)
1519 command->
width = usb_maxpacket(urb->dev, urb->pipe,
1520 usb_pipeout(urb->pipe));
1523 command->
buffer = urb->setup_packet;
1525 target->
endp = endp;
1529 ftdi_elan_kick_command_queue(ftdi);
1541 void *endp,
struct urb *urb,
u8 address,
u8 ep_number,
u8 toggle_bits,
1542 void (*
callback) (
void *endp,
struct urb *urb,
u8 *buf,
int len,
1543 int toggle_bits,
int error_count,
int condition_code,
int repeat_number,
1544 int halted,
int skipped,
int actual,
int non_null))
1547 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1553 static int ftdi_elan_edset_input(
struct usb_ftdi *ftdi,
u8 ed_number,
1554 void *endp,
struct urb *urb,
u8 address,
u8 ep_number,
u8 toggle_bits,
1555 void (*
callback) (
void *endp,
struct urb *urb,
u8 *buf,
int len,
1556 int toggle_bits,
int error_count,
int condition_code,
int repeat_number,
1557 int halted,
int skipped,
int actual,
int non_null))
1559 u8 ed = ed_number - 1;
1572 u32 remaining_length = urb->transfer_buffer_length -
1574 command->
header = 0x82 | (ed << 5);
1575 if (remaining_length == 0) {
1576 command->
length = 0x0000;
1577 }
else if (remaining_length > 1024) {
1578 command->
length = 0x8000 | 1023;
1580 command->
length = 0x8000 | (remaining_length -
1582 command->
address = (toggle_bits << 6) | (ep_number << 2)
1584 command->
width = usb_maxpacket(urb->dev, urb->pipe,
1585 usb_pipeout(urb->pipe));
1590 target->
endp = endp;
1594 ftdi_elan_kick_command_queue(ftdi);
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))
1612 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1618 static int ftdi_elan_edset_empty(
struct usb_ftdi *ftdi,
u8 ed_number,
1619 void *endp,
struct urb *urb,
u8 address,
u8 ep_number,
u8 toggle_bits,
1620 void (*
callback) (
void *endp,
struct urb *urb,
u8 *buf,
int len,
1621 int toggle_bits,
int error_count,
int condition_code,
int repeat_number,
1622 int halted,
int skipped,
int actual,
int non_null))
1624 u8 ed = ed_number - 1;
1637 command->
header = 0x81 | (ed << 5);
1638 command->
length = 0x0000;
1639 command->
address = (toggle_bits << 6) | (ep_number << 2)
1641 command->
width = usb_maxpacket(urb->dev, urb->pipe,
1642 usb_pipeout(urb->pipe));
1647 target->
endp = endp;
1651 ftdi_elan_kick_command_queue(ftdi);
1663 void *endp,
struct urb *urb,
u8 address,
u8 ep_number,
u8 toggle_bits,
1664 void (*
callback) (
void *endp,
struct urb *urb,
u8 *buf,
int len,
1665 int toggle_bits,
int error_count,
int condition_code,
int repeat_number,
1666 int halted,
int skipped,
int actual,
int non_null))
1669 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1675 static int ftdi_elan_edset_output(
struct usb_ftdi *ftdi,
u8 ed_number,
1676 void *endp,
struct urb *urb,
u8 address,
u8 ep_number,
u8 toggle_bits,
1677 void (*
callback) (
void *endp,
struct urb *urb,
u8 *buf,
int len,
1678 int toggle_bits,
int error_count,
int condition_code,
int repeat_number,
1679 int halted,
int skipped,
int actual,
int non_null))
1681 u8 ed = ed_number - 1;
1694 char data[30 *3 + 4];
1696 int m = (
sizeof(
data) - 1) / 3;
1701 command->
header = 0x81 | (ed << 5);
1702 command->
address = (toggle_bits << 6) | (ep_number << 2)
1704 command->
width = usb_maxpacket(urb->dev, urb->pipe,
1705 usb_pipeout(urb->pipe));
1707 urb->transfer_buffer_length -
1708 urb->actual_length);
1710 command->
buffer = urb->transfer_buffer +
1716 while (urb_size-- > 0) {
1718 }
else if (i++ < m) {
1726 target->
endp = endp;
1730 ftdi_elan_kick_command_queue(ftdi);
1742 void *endp,
struct urb *urb,
u8 address,
u8 ep_number,
u8 toggle_bits,
1743 void (*
callback) (
void *endp,
struct urb *urb,
u8 *buf,
int len,
1744 int toggle_bits,
int error_count,
int condition_code,
int repeat_number,
1745 int halted,
int skipped,
int actual,
int non_null))
1748 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1754 static int ftdi_elan_edset_single(
struct usb_ftdi *ftdi,
u8 ed_number,
1755 void *endp,
struct urb *urb,
u8 address,
u8 ep_number,
u8 toggle_bits,
1756 void (*
callback) (
void *endp,
struct urb *urb,
u8 *buf,
int len,
1757 int toggle_bits,
int error_count,
int condition_code,
int repeat_number,
1758 int halted,
int skipped,
int actual,
int non_null))
1760 u8 ed = ed_number - 1;
1770 u32 remaining_length = urb->transfer_buffer_length -
1775 command->
header = 0x83 | (ed << 5);
1776 if (remaining_length == 0) {
1777 command->
length = 0x0000;
1778 }
else if (remaining_length > 1024) {
1779 command->
length = 0x8000 | 1023;
1781 command->
length = 0x8000 | (remaining_length -
1783 command->
address = (toggle_bits << 6) | (ep_number << 2)
1785 command->
width = usb_maxpacket(urb->dev, urb->pipe,
1786 usb_pipeout(urb->pipe));
1791 target->
endp = endp;
1795 ftdi_elan_kick_command_queue(ftdi);
1807 void *endp,
struct urb *urb,
u8 address,
u8 ep_number,
u8 toggle_bits,
1808 void (*
callback) (
void *endp,
struct urb *urb,
u8 *buf,
int len,
1809 int toggle_bits,
int error_count,
int condition_code,
int repeat_number,
1810 int halted,
int skipped,
int actual,
int non_null))
1813 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1819 static int ftdi_elan_edset_flush(
struct usb_ftdi *ftdi,
u8 ed_number,
1822 u8 ed = ed_number - 1;
1835 wait_1:
if (target->
active == 1) {
1842 command->
header = 0x80 | (ed << 5) |
1846 command->
width = 0x00;
1851 ftdi_elan_kick_command_queue(ftdi);
1869 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1874 static int ftdi_elan_flush_input_fifo(
struct usb_ftdi *ftdi)
1876 int retry_on_empty = 10;
1877 int retry_on_timeout = 5;
1878 int retry_on_status = 20;
1880 int packet_bytes = 0;
1884 &packet_bytes, 100);
1885 if (packet_bytes > 2) {
1886 char diag[30 *3 + 4];
1888 int m = (
sizeof(
diag) - 1) / 3;
1892 while (packet_bytes-- > 0) {
1894 if (bytes_read < m) {
1897 }
else if (bytes_read > m) {
1904 }
else if (packet_bytes > 1) {
1907 if (s1 == 0x31 && s2 == 0x60) {
1909 }
else if (retry_on_status-- > 0) {
1916 }
else if (packet_bytes > 0) {
1918 dev_err(&ftdi->
udev->dev,
"only one byte flushed from F"
1919 "TDI = %02X\n", b1);
1920 if (retry_on_status-- > 0) {
1928 if (retry_on_timeout-- > 0) {
1935 }
else if (retval == 0) {
1936 if (retry_on_empty-- > 0) {
1944 dev_err(&ftdi->
udev->dev,
"error = %d\n", retval);
1956 static int ftdi_elan_synchronize_flush(
struct usb_ftdi *ftdi)
1965 dev_err(&ftdi->
udev->dev,
"could not alloc a urb for flush sequ"
1971 dev_err(&ftdi->
udev->dev,
"could not get a buffer for flush seq"
1978 usb_fill_bulk_urb(urb, ftdi->
udev, usb_sndbulkpipe(ftdi->
udev,
1980 ftdi_elan_write_bulk_callback, ftdi);
1981 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1984 dev_err(&ftdi->
udev->dev,
"failed to submit urb containing the "
1985 "flush sequence\n");
1999 static int ftdi_elan_synchronize_reset(
struct usb_ftdi *ftdi)
2008 dev_err(&ftdi->
udev->dev,
"could not get a urb for the reset se"
2014 dev_err(&ftdi->
udev->dev,
"could not get a buffer for the reset"
2023 usb_fill_bulk_urb(urb, ftdi->
udev, usb_sndbulkpipe(ftdi->
udev,
2025 ftdi_elan_write_bulk_callback, ftdi);
2026 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2029 dev_err(&ftdi->
udev->dev,
"failed to submit urb containing the "
2030 "reset sequence\n");
2039 static int ftdi_elan_synchronize(
struct usb_ftdi *ftdi)
2043 int retry_on_timeout = 5;
2044 int retry_on_empty = 10;
2046 retval = ftdi_elan_flush_input_fifo(ftdi);
2051 while (long_stop-- > 0) {
2054 retval = ftdi_elan_synchronize_flush(ftdi);
2057 retval = ftdi_elan_flush_input_fifo(ftdi);
2060 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2066 int packet_bytes = 0;
2068 usb_rcvbulkpipe(ftdi->
udev,
2071 &packet_bytes, 500);
2072 if (packet_bytes > 2) {
2073 char diag[30 *3 + 4];
2075 int m = (
sizeof(
diag) - 1) / 3;
2078 unsigned char c = 0;
2080 while (packet_bytes-- > 0) {
2082 if (bytes_read < m) {
2084 }
else if (bytes_read > m) {
2095 }
else if (read_stop-- > 0) {
2099 "y limit reached\n");
2103 }
else if (packet_bytes > 1) {
2106 if (s1 == 0x31 && s2 == 0x00) {
2107 if (read_stuck-- > 0) {
2111 }
else if (s1 == 0x31 && s2 == 0x60) {
2112 if (read_stop-- > 0) {
2116 "y limit reached\n");
2120 if (read_stop-- > 0) {
2124 "y limit reached\n");
2128 }
else if (packet_bytes > 0) {
2129 if (read_stop-- > 0) {
2137 if (retry_on_timeout-- > 0) {
2141 "try limit reached\n");
2144 }
else if (retval == 0) {
2145 if (retry_on_empty-- > 0) {
2149 " retry limit reached\n");
2156 if (read_stop-- > 0) {
2166 dev_err(&ftdi->
udev->dev,
"failed to synchronize\n");
2170 static int ftdi_elan_stuck_waiting(
struct usb_ftdi *ftdi)
2172 int retry_on_empty = 10;
2173 int retry_on_timeout = 5;
2174 int retry_on_status = 50;
2176 int packet_bytes = 0;
2180 &packet_bytes, 1000);
2181 if (packet_bytes > 2) {
2182 char diag[30 *3 + 4];
2184 int m = (
sizeof(
diag) - 1) / 3;
2188 while (packet_bytes-- > 0) {
2190 if (bytes_read < m) {
2193 }
else if (bytes_read > m) {
2200 }
else if (packet_bytes > 1) {
2203 if (s1 == 0x31 && s2 == 0x60) {
2205 }
else if (retry_on_status-- > 0) {
2210 }
else if (packet_bytes > 0) {
2212 dev_err(&ftdi->
udev->dev,
"only one byte flushed from F"
2213 "TDI = %02X\n", b1);
2214 if (retry_on_status-- > 0) {
2223 if (retry_on_timeout-- > 0) {
2230 }
else if (retval == 0) {
2231 if (retry_on_empty-- > 0) {
2239 dev_err(&ftdi->
udev->dev,
"error = %d\n", retval);
2246 static int ftdi_elan_checkingPCI(
struct usb_ftdi *ftdi)
2248 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->
controlreg);
2255 dev_err(&ftdi->
udev->dev,
"CARD EJECTED - controlreg = "
2261 int activePCIfn = fn << 8;
2266 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2270 pciVID = pcidata & 0xFFFF;
2271 pciPID = (pcidata >> 16) & 0xFFFF;
2276 dev_err(&ftdi->
udev->dev,
"vendor=%04X pciVID=%04X devi"
2277 "ce=%04X pciPID=%04X\n",
2286 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2287 offsetof(struct ohci_regs, member), 0, data);
2288 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2289 offsetof(struct ohci_regs, member), 0, data);
2291 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2292 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2294 static int ftdi_elan_check_controller(
struct usb_ftdi *ftdi,
int quirk)
2310 int reset_timeout = 30;
2325 hc_fminterval &= 0x3fff;
2326 if (hc_fminterval !=
FI) {
2328 hc_fminterval |=
FSMP(hc_fminterval) << 16;
2359 for (temp = 0; temp < num_ports; temp++) {
2380 if (--reset_timeout == 0) {
2411 ((fminterval &
FIT) ^ FIT) | hc_fminterval);
2415 ((9 *hc_fminterval) / 10) & 0x3fff);
2424 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2425 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2429 dev_err(&ftdi->
udev->dev,
"init err(%08x %04x)\n",
2430 fminterval, periodicstart);
2478 (roothub_a & RH_A_NPS) ? 0 :
RH_B_PPCM);
2484 mdelay((roothub_a >> 23) & 0x1fe);
2485 for (temp = 0; temp < num_ports; temp++) {
2497 static int ftdi_elan_setup_controller(
struct usb_ftdi *ftdi,
int fn)
2503 int activePCIfn = fn << 8;
2504 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2508 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2512 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2516 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2520 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2525 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2529 latence_timer &= 0xFFFF00FF;
2530 latence_timer |= 0x00001600;
2531 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2535 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2540 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2544 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2548 for (reg = 0; reg <= 0x54; reg += 4) {
2549 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2556 static int ftdi_elan_close_controller(
struct usb_ftdi *ftdi,
int fn)
2562 int activePCIfn = fn << 8;
2563 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2567 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2571 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2575 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2579 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2584 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2588 latence_timer &= 0xFFFF00FF;
2589 latence_timer |= 0x00001600;
2590 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2594 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2599 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2603 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2610 static int ftdi_elan_found_controller(
struct usb_ftdi *ftdi,
int fn,
int quirk)
2614 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2617 result = ftdi_elan_check_controller(ftdi, quirk);
2618 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2624 static int ftdi_elan_enumeratePCI(
struct usb_ftdi *ftdi)
2629 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2632 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2636 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2639 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2642 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2645 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2648 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2652 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2655 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2658 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2661 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2664 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2668 sensebits = (controlreg >> 16) & 0x000F;
2669 if (0x0D == sensebits)
2675 static int ftdi_elan_setupOHCI(
struct usb_ftdi *ftdi)
2681 int activePCIfn = 0;
2682 int max_devices = 0;
2683 int controllers = 0;
2684 int unrecognized = 0;
2686 for (fn = 0; (fn < 4); fn++) {
2690 activePCIfn = fn << 8;
2691 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2695 pciVID = pcidata & 0xFFFF;
2696 pciPID = (pcidata >> 16) & 0xFFFF;
2698 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2702 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2705 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2709 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2712 devices = ftdi_elan_found_controller(ftdi, fn,
2716 devices = ftdi_elan_found_controller(ftdi, fn,
2719 }
else if (0 == pcidata) {
2722 if (devices > max_devices) {
2723 max_devices = devices;
2730 UxxxStatus = ftdi_elan_setup_controller(ftdi,
2735 }
else if (controllers > 0) {
2737 }
else if (unrecognized > 0) {
2752 struct usb_host_interface *iface_desc;
2769 ftdi_elan_init_kref(ftdi);
2775 iface_desc = interface->cur_altsetting;
2776 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++
i) {
2777 endpoint = &iface_desc->endpoint[
i].desc;
2779 usb_endpoint_is_bulk_in(endpoint)) {
2780 buffer_size = usb_endpoint_maxp(endpoint);
2792 usb_endpoint_is_bulk_out(endpoint)) {
2798 dev_err(&ftdi->
udev->dev,
"Could not find both bulk-in and bulk"
2799 "-out endpoints\n");
2803 dev_info(&ftdi->
udev->dev,
"interface %d has I=%02X O=%02X\n",
2806 usb_set_intfdata(interface, ftdi);
2807 if (iface_desc->desc.bInterfaceNumber == 0 &&
2812 dev_err(&ftdi->
udev->dev,
"Not able to get a minor for "
2814 usb_set_intfdata(interface,
NULL);
2818 ftdi->
class = &ftdi_elan_jtag_class;
2819 dev_info(&ftdi->
udev->dev,
"USB FDTI=%p JTAG interface "
2820 "%d now attached to ftdi%d\n", ftdi,
2821 iface_desc->desc.bInterfaceNumber,
2825 }
else if (iface_desc->desc.bInterfaceNumber == 1 &&
2829 dev_info(&ftdi->
udev->dev,
"USB FDTI=%p ELAN interface %d now a"
2830 "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
2838 "Could not find ELAN's U132 device\n");
2843 ftdi_elan_put_kref(ftdi);
2848 static void ftdi_elan_disconnect(
struct usb_interface *interface)
2850 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2853 int minor = interface->minor;
2854 struct usb_class_driver *
class = ftdi->
class;
2855 usb_set_intfdata(interface,
NULL);
2857 dev_info(&ftdi->
udev->dev,
"USB FTDI U132 jtag interface on min"
2858 "or %d now disconnected\n", minor);
2860 ftdi_status_cancel_work(ftdi);
2861 ftdi_command_cancel_work(ftdi);
2862 ftdi_response_cancel_work(ftdi);
2863 ftdi_elan_abandon_completions(ftdi);
2864 ftdi_elan_abandon_targets(ftdi);
2876 usb_set_intfdata(interface,
NULL);
2877 dev_info(&ftdi->
udev->dev,
"USB FTDI U132 host controller inter"
2878 "face now disconnected\n");
2880 ftdi_elan_put_kref(ftdi);
2883 static struct usb_driver ftdi_elan_driver = {
2884 .name =
"ftdi-elan",
2885 .probe = ftdi_elan_probe,
2886 .disconnect = ftdi_elan_disconnect,
2887 .id_table = ftdi_elan_table,
2889 static int __init ftdi_elan_init(
void)
2894 INIT_LIST_HEAD(&ftdi_static_list);
2897 goto err_status_queue;
2900 goto err_command_queue;
2903 goto err_respond_queue;
2904 result = usb_register(&ftdi_elan_driver);
2919 printk(
KERN_ERR "%s couldn't create workqueue\n", ftdi_elan_driver.name);
2923 static void __exit ftdi_elan_exit(
void)
2930 ftdi_status_cancel_work(ftdi);
2931 ftdi_command_cancel_work(ftdi);
2932 ftdi_response_cancel_work(ftdi);
2935 status_queue =
NULL;
2938 command_queue =
NULL;
2941 respond_queue =
NULL;