0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0036
0037 #include <linux/kernel.h>
0038 #include <linux/errno.h>
0039 #include <linux/init.h>
0040 #include <linux/list.h>
0041 #include <linux/ioctl.h>
0042 #include <linux/pci_ids.h>
0043 #include <linux/slab.h>
0044 #include <linux/module.h>
0045 #include <linux/kref.h>
0046 #include <linux/mutex.h>
0047 #include <linux/uaccess.h>
0048 #include <linux/usb.h>
0049 #include <linux/workqueue.h>
0050 #include <linux/platform_device.h>
0051 MODULE_AUTHOR("Tony Olech");
0052 MODULE_DESCRIPTION("FTDI ELAN driver");
0053 MODULE_LICENSE("GPL");
0054 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
0055 static bool distrust_firmware = 1;
0056 module_param(distrust_firmware, bool, 0);
0057 MODULE_PARM_DESC(distrust_firmware,
0058 "true to distrust firmware power/overcurrent setup");
0059 extern struct platform_driver u132_platform_driver;
0060
0061
0062
0063
0064 static struct mutex ftdi_module_lock;
0065 static int ftdi_instances = 0;
0066 static struct list_head ftdi_static_list;
0067
0068
0069
0070 #include "usb_u132.h"
0071 #include <asm/io.h>
0072 #include <linux/usb/hcd.h>
0073
0074
0075
0076
0077
0078
0079
0080 #include "../host/ohci.h"
0081
0082 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
0083 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
0084
0085 static const struct usb_device_id ftdi_elan_table[] = {
0086 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
0087 { }
0088 };
0089
0090 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 #define USB_FTDI_ELAN_MINOR_BASE 192
0103 #define COMMAND_BITS 5
0104 #define COMMAND_SIZE (1<<COMMAND_BITS)
0105 #define COMMAND_MASK (COMMAND_SIZE-1)
0106 struct u132_command {
0107 u8 header;
0108 u16 length;
0109 u8 address;
0110 u8 width;
0111 u32 value;
0112 int follows;
0113 void *buffer;
0114 };
0115 #define RESPOND_BITS 5
0116 #define RESPOND_SIZE (1<<RESPOND_BITS)
0117 #define RESPOND_MASK (RESPOND_SIZE-1)
0118 struct u132_respond {
0119 u8 header;
0120 u8 address;
0121 u32 *value;
0122 int *result;
0123 struct completion wait_completion;
0124 };
0125 struct u132_target {
0126 void *endp;
0127 struct urb *urb;
0128 int toggle_bits;
0129 int error_count;
0130 int condition_code;
0131 int repeat_number;
0132 int halted;
0133 int skipped;
0134 int actual;
0135 int non_null;
0136 int active;
0137 int abandoning;
0138 void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
0139 int toggle_bits, int error_count, int condition_code,
0140 int repeat_number, int halted, int skipped, int actual,
0141 int non_null);
0142 };
0143
0144 struct usb_ftdi {
0145 struct list_head ftdi_list;
0146 struct mutex u132_lock;
0147 int command_next;
0148 int command_head;
0149 struct u132_command command[COMMAND_SIZE];
0150 int respond_next;
0151 int respond_head;
0152 struct u132_respond respond[RESPOND_SIZE];
0153 struct u132_target target[4];
0154 char device_name[16];
0155 unsigned synchronized:1;
0156 unsigned enumerated:1;
0157 unsigned registered:1;
0158 unsigned initialized:1;
0159 unsigned card_ejected:1;
0160 int function;
0161 int sequence_num;
0162 int disconnected;
0163 int gone_away;
0164 int stuck_status;
0165 int status_queue_delay;
0166 struct semaphore sw_lock;
0167 struct usb_device *udev;
0168 struct usb_interface *interface;
0169 struct usb_class_driver *class;
0170 struct delayed_work status_work;
0171 struct delayed_work command_work;
0172 struct delayed_work respond_work;
0173 struct u132_platform_data platform_data;
0174 struct resource resources[0];
0175 struct platform_device platform_dev;
0176 unsigned char *bulk_in_buffer;
0177 size_t bulk_in_size;
0178 size_t bulk_in_last;
0179 size_t bulk_in_left;
0180 __u8 bulk_in_endpointAddr;
0181 __u8 bulk_out_endpointAddr;
0182 struct kref kref;
0183 u32 controlreg;
0184 u8 response[4 + 1024];
0185 int expected;
0186 int received;
0187 int ed_found;
0188 };
0189 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
0190 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
0191 platform_dev)
0192 static struct usb_driver ftdi_elan_driver;
0193 static void ftdi_elan_delete(struct kref *kref)
0194 {
0195 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
0196 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
0197 usb_put_dev(ftdi->udev);
0198 ftdi->disconnected += 1;
0199 mutex_lock(&ftdi_module_lock);
0200 list_del_init(&ftdi->ftdi_list);
0201 ftdi_instances -= 1;
0202 mutex_unlock(&ftdi_module_lock);
0203 kfree(ftdi->bulk_in_buffer);
0204 ftdi->bulk_in_buffer = NULL;
0205 kfree(ftdi);
0206 }
0207
0208 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
0209 {
0210 kref_put(&ftdi->kref, ftdi_elan_delete);
0211 }
0212
0213 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
0214 {
0215 kref_get(&ftdi->kref);
0216 }
0217
0218 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
0219 {
0220 kref_init(&ftdi->kref);
0221 }
0222
0223 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
0224 {
0225 if (!schedule_delayed_work(&ftdi->status_work, delta))
0226 kref_put(&ftdi->kref, ftdi_elan_delete);
0227 }
0228
0229 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
0230 {
0231 if (schedule_delayed_work(&ftdi->status_work, delta))
0232 kref_get(&ftdi->kref);
0233 }
0234
0235 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
0236 {
0237 if (cancel_delayed_work_sync(&ftdi->status_work))
0238 kref_put(&ftdi->kref, ftdi_elan_delete);
0239 }
0240
0241 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
0242 {
0243 if (!schedule_delayed_work(&ftdi->command_work, delta))
0244 kref_put(&ftdi->kref, ftdi_elan_delete);
0245 }
0246
0247 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
0248 {
0249 if (schedule_delayed_work(&ftdi->command_work, delta))
0250 kref_get(&ftdi->kref);
0251 }
0252
0253 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
0254 {
0255 if (cancel_delayed_work_sync(&ftdi->command_work))
0256 kref_put(&ftdi->kref, ftdi_elan_delete);
0257 }
0258
0259 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
0260 unsigned int delta)
0261 {
0262 if (!schedule_delayed_work(&ftdi->respond_work, delta))
0263 kref_put(&ftdi->kref, ftdi_elan_delete);
0264 }
0265
0266 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
0267 {
0268 if (schedule_delayed_work(&ftdi->respond_work, delta))
0269 kref_get(&ftdi->kref);
0270 }
0271
0272 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
0273 {
0274 if (cancel_delayed_work_sync(&ftdi->respond_work))
0275 kref_put(&ftdi->kref, ftdi_elan_delete);
0276 }
0277
0278 void ftdi_elan_gone_away(struct platform_device *pdev)
0279 {
0280 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
0281 ftdi->gone_away += 1;
0282 ftdi_elan_put_kref(ftdi);
0283 }
0284
0285
0286 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
0287 static void ftdi_release_platform_dev(struct device *dev)
0288 {
0289 dev->parent = NULL;
0290 }
0291
0292 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
0293 struct u132_target *target, u8 *buffer, int length);
0294 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
0295 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
0296 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
0297 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
0298 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
0299 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
0300 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
0301 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
0302 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
0303 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
0304 {
0305 if (ftdi->platform_dev.dev.parent)
0306 return -EBUSY;
0307
0308 ftdi_elan_get_kref(ftdi);
0309 ftdi->platform_data.potpg = 100;
0310 ftdi->platform_data.reset = NULL;
0311 ftdi->platform_dev.id = ftdi->sequence_num;
0312 ftdi->platform_dev.resource = ftdi->resources;
0313 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
0314 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
0315 ftdi->platform_dev.dev.parent = NULL;
0316 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
0317 ftdi->platform_dev.dev.dma_mask = NULL;
0318 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
0319 ftdi->platform_dev.name = ftdi->device_name;
0320 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
0321 request_module("u132_hcd");
0322 dev_info(&ftdi->udev->dev, "registering '%s'\n",
0323 ftdi->platform_dev.name);
0324
0325 return platform_device_register(&ftdi->platform_dev);
0326 }
0327
0328 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
0329 {
0330 mutex_lock(&ftdi->u132_lock);
0331 while (ftdi->respond_next > ftdi->respond_head) {
0332 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
0333 ftdi->respond_head++];
0334 *respond->result = -ESHUTDOWN;
0335 *respond->value = 0;
0336 complete(&respond->wait_completion);
0337 }
0338 mutex_unlock(&ftdi->u132_lock);
0339 }
0340
0341 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
0342 {
0343 int ed_number = 4;
0344 mutex_lock(&ftdi->u132_lock);
0345 while (ed_number-- > 0) {
0346 struct u132_target *target = &ftdi->target[ed_number];
0347 if (target->active == 1) {
0348 target->condition_code = TD_DEVNOTRESP;
0349 mutex_unlock(&ftdi->u132_lock);
0350 ftdi_elan_do_callback(ftdi, target, NULL, 0);
0351 mutex_lock(&ftdi->u132_lock);
0352 }
0353 }
0354 ftdi->received = 0;
0355 ftdi->expected = 4;
0356 ftdi->ed_found = 0;
0357 mutex_unlock(&ftdi->u132_lock);
0358 }
0359
0360 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
0361 {
0362 int ed_number = 4;
0363 mutex_lock(&ftdi->u132_lock);
0364 while (ed_number-- > 0) {
0365 struct u132_target *target = &ftdi->target[ed_number];
0366 target->abandoning = 1;
0367 wait_1:if (target->active == 1) {
0368 int command_size = ftdi->command_next -
0369 ftdi->command_head;
0370 if (command_size < COMMAND_SIZE) {
0371 struct u132_command *command = &ftdi->command[
0372 COMMAND_MASK & ftdi->command_next];
0373 command->header = 0x80 | (ed_number << 5) | 0x4;
0374 command->length = 0x00;
0375 command->address = 0x00;
0376 command->width = 0x00;
0377 command->follows = 0;
0378 command->value = 0;
0379 command->buffer = &command->value;
0380 ftdi->command_next += 1;
0381 ftdi_elan_kick_command_queue(ftdi);
0382 } else {
0383 mutex_unlock(&ftdi->u132_lock);
0384 msleep(100);
0385 mutex_lock(&ftdi->u132_lock);
0386 goto wait_1;
0387 }
0388 }
0389 wait_2:if (target->active == 1) {
0390 int command_size = ftdi->command_next -
0391 ftdi->command_head;
0392 if (command_size < COMMAND_SIZE) {
0393 struct u132_command *command = &ftdi->command[
0394 COMMAND_MASK & ftdi->command_next];
0395 command->header = 0x90 | (ed_number << 5);
0396 command->length = 0x00;
0397 command->address = 0x00;
0398 command->width = 0x00;
0399 command->follows = 0;
0400 command->value = 0;
0401 command->buffer = &command->value;
0402 ftdi->command_next += 1;
0403 ftdi_elan_kick_command_queue(ftdi);
0404 } else {
0405 mutex_unlock(&ftdi->u132_lock);
0406 msleep(100);
0407 mutex_lock(&ftdi->u132_lock);
0408 goto wait_2;
0409 }
0410 }
0411 }
0412 ftdi->received = 0;
0413 ftdi->expected = 4;
0414 ftdi->ed_found = 0;
0415 mutex_unlock(&ftdi->u132_lock);
0416 }
0417
0418 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
0419 {
0420 int ed_number = 4;
0421 mutex_lock(&ftdi->u132_lock);
0422 while (ed_number-- > 0) {
0423 struct u132_target *target = &ftdi->target[ed_number];
0424 target->abandoning = 1;
0425 wait:if (target->active == 1) {
0426 int command_size = ftdi->command_next -
0427 ftdi->command_head;
0428 if (command_size < COMMAND_SIZE) {
0429 struct u132_command *command = &ftdi->command[
0430 COMMAND_MASK & ftdi->command_next];
0431 command->header = 0x80 | (ed_number << 5) | 0x4;
0432 command->length = 0x00;
0433 command->address = 0x00;
0434 command->width = 0x00;
0435 command->follows = 0;
0436 command->value = 0;
0437 command->buffer = &command->value;
0438 ftdi->command_next += 1;
0439 ftdi_elan_kick_command_queue(ftdi);
0440 } else {
0441 mutex_unlock(&ftdi->u132_lock);
0442 msleep(100);
0443 mutex_lock(&ftdi->u132_lock);
0444 goto wait;
0445 }
0446 }
0447 }
0448 ftdi->received = 0;
0449 ftdi->expected = 4;
0450 ftdi->ed_found = 0;
0451 mutex_unlock(&ftdi->u132_lock);
0452 }
0453
0454 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
0455 {
0456 ftdi_command_queue_work(ftdi, 0);
0457 }
0458
0459 static void ftdi_elan_command_work(struct work_struct *work)
0460 {
0461 struct usb_ftdi *ftdi =
0462 container_of(work, struct usb_ftdi, command_work.work);
0463
0464 if (ftdi->disconnected > 0) {
0465 ftdi_elan_put_kref(ftdi);
0466 return;
0467 } else {
0468 int retval = ftdi_elan_command_engine(ftdi);
0469 if (retval == -ESHUTDOWN) {
0470 ftdi->disconnected += 1;
0471 } else if (retval == -ENODEV) {
0472 ftdi->disconnected += 1;
0473 } else if (retval)
0474 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
0475 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
0476 return;
0477 }
0478 }
0479
0480 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
0481 {
0482 ftdi_respond_queue_work(ftdi, 0);
0483 }
0484
0485 static void ftdi_elan_respond_work(struct work_struct *work)
0486 {
0487 struct usb_ftdi *ftdi =
0488 container_of(work, struct usb_ftdi, respond_work.work);
0489 if (ftdi->disconnected > 0) {
0490 ftdi_elan_put_kref(ftdi);
0491 return;
0492 } else {
0493 int retval = ftdi_elan_respond_engine(ftdi);
0494 if (retval == 0) {
0495 } else if (retval == -ESHUTDOWN) {
0496 ftdi->disconnected += 1;
0497 } else if (retval == -ENODEV) {
0498 ftdi->disconnected += 1;
0499 } else if (retval == -EILSEQ) {
0500 ftdi->disconnected += 1;
0501 } else {
0502 ftdi->disconnected += 1;
0503 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
0504 }
0505 if (ftdi->disconnected > 0) {
0506 ftdi_elan_abandon_completions(ftdi);
0507 ftdi_elan_abandon_targets(ftdi);
0508 }
0509 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
0510 return;
0511 }
0512 }
0513
0514
0515
0516
0517
0518
0519
0520 static void ftdi_elan_status_work(struct work_struct *work)
0521 {
0522 struct usb_ftdi *ftdi =
0523 container_of(work, struct usb_ftdi, status_work.work);
0524 int work_delay_in_msec = 0;
0525 if (ftdi->disconnected > 0) {
0526 ftdi_elan_put_kref(ftdi);
0527 return;
0528 } else if (ftdi->synchronized == 0) {
0529 down(&ftdi->sw_lock);
0530 if (ftdi_elan_synchronize(ftdi) == 0) {
0531 ftdi->synchronized = 1;
0532 ftdi_command_queue_work(ftdi, 1);
0533 ftdi_respond_queue_work(ftdi, 1);
0534 up(&ftdi->sw_lock);
0535 work_delay_in_msec = 100;
0536 } else {
0537 dev_err(&ftdi->udev->dev, "synchronize failed\n");
0538 up(&ftdi->sw_lock);
0539 work_delay_in_msec = 10 *1000;
0540 }
0541 } else if (ftdi->stuck_status > 0) {
0542 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
0543 ftdi->stuck_status = 0;
0544 ftdi->synchronized = 0;
0545 } else if ((ftdi->stuck_status++ % 60) == 1) {
0546 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
0547 } else
0548 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
0549 ftdi->stuck_status);
0550 work_delay_in_msec = 100;
0551 } else if (ftdi->enumerated == 0) {
0552 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
0553 ftdi->enumerated = 1;
0554 work_delay_in_msec = 250;
0555 } else
0556 work_delay_in_msec = 1000;
0557 } else if (ftdi->initialized == 0) {
0558 if (ftdi_elan_setupOHCI(ftdi) == 0) {
0559 ftdi->initialized = 1;
0560 work_delay_in_msec = 500;
0561 } else {
0562 dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
0563 work_delay_in_msec = 1 *1000;
0564 }
0565 } else if (ftdi->registered == 0) {
0566 work_delay_in_msec = 10;
0567 if (ftdi_elan_hcd_init(ftdi) == 0) {
0568 ftdi->registered = 1;
0569 } else
0570 dev_err(&ftdi->udev->dev, "register failed\n");
0571 work_delay_in_msec = 250;
0572 } else {
0573 if (ftdi_elan_checkingPCI(ftdi) == 0) {
0574 work_delay_in_msec = 250;
0575 } else if (ftdi->controlreg & 0x00400000) {
0576 if (ftdi->gone_away > 0) {
0577 dev_err(&ftdi->udev->dev, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
0578 ftdi->platform_dev.dev.parent,
0579 &ftdi->platform_dev.dev);
0580 platform_device_unregister(&ftdi->platform_dev);
0581 ftdi->platform_dev.dev.parent = NULL;
0582 ftdi->registered = 0;
0583 ftdi->enumerated = 0;
0584 ftdi->card_ejected = 0;
0585 ftdi->initialized = 0;
0586 ftdi->gone_away = 0;
0587 } else
0588 ftdi_elan_flush_targets(ftdi);
0589 work_delay_in_msec = 250;
0590 } else {
0591 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n");
0592 ftdi_elan_cancel_targets(ftdi);
0593 work_delay_in_msec = 500;
0594 ftdi->enumerated = 0;
0595 ftdi->initialized = 0;
0596 }
0597 }
0598 if (ftdi->disconnected > 0) {
0599 ftdi_elan_put_kref(ftdi);
0600 return;
0601 } else {
0602 ftdi_status_requeue_work(ftdi,
0603 msecs_to_jiffies(work_delay_in_msec));
0604 return;
0605 }
0606 }
0607
0608
0609
0610
0611
0612
0613
0614
0615 static int ftdi_elan_open(struct inode *inode, struct file *file)
0616 {
0617 int subminor;
0618 struct usb_interface *interface;
0619
0620 subminor = iminor(inode);
0621 interface = usb_find_interface(&ftdi_elan_driver, subminor);
0622
0623 if (!interface) {
0624 pr_err("can't find device for minor %d\n", subminor);
0625 return -ENODEV;
0626 } else {
0627 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
0628 if (!ftdi) {
0629 return -ENODEV;
0630 } else {
0631 if (down_interruptible(&ftdi->sw_lock)) {
0632 return -EINTR;
0633 } else {
0634 ftdi_elan_get_kref(ftdi);
0635 file->private_data = ftdi;
0636 return 0;
0637 }
0638 }
0639 }
0640 }
0641
0642 static int ftdi_elan_release(struct inode *inode, struct file *file)
0643 {
0644 struct usb_ftdi *ftdi = file->private_data;
0645 if (ftdi == NULL)
0646 return -ENODEV;
0647 up(&ftdi->sw_lock);
0648 ftdi_elan_put_kref(ftdi);
0649 return 0;
0650 }
0651
0652
0653
0654
0655
0656
0657
0658 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
0659 size_t count, loff_t *ppos)
0660 {
0661 char data[30 *3 + 4];
0662 char *d = data;
0663 int m = (sizeof(data) - 1) / 3 - 1;
0664 int bytes_read = 0;
0665 int retry_on_empty = 10;
0666 int retry_on_timeout = 5;
0667 struct usb_ftdi *ftdi = file->private_data;
0668 if (ftdi->disconnected > 0) {
0669 return -ENODEV;
0670 }
0671 data[0] = 0;
0672 have:if (ftdi->bulk_in_left > 0) {
0673 if (count-- > 0) {
0674 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
0675 ftdi->bulk_in_left -= 1;
0676 if (bytes_read < m) {
0677 d += sprintf(d, " %02X", 0x000000FF & *p);
0678 } else if (bytes_read > m) {
0679 } else
0680 d += sprintf(d, " ..");
0681 if (copy_to_user(buffer++, p, 1)) {
0682 return -EFAULT;
0683 } else {
0684 bytes_read += 1;
0685 goto have;
0686 }
0687 } else
0688 return bytes_read;
0689 }
0690 more:if (count > 0) {
0691 int packet_bytes = 0;
0692 int retval = usb_bulk_msg(ftdi->udev,
0693 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
0694 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
0695 &packet_bytes, 50);
0696 if (packet_bytes > 2) {
0697 ftdi->bulk_in_left = packet_bytes - 2;
0698 ftdi->bulk_in_last = 1;
0699 goto have;
0700 } else if (retval == -ETIMEDOUT) {
0701 if (retry_on_timeout-- > 0) {
0702 goto more;
0703 } else if (bytes_read > 0) {
0704 return bytes_read;
0705 } else
0706 return retval;
0707 } else if (retval == 0) {
0708 if (retry_on_empty-- > 0) {
0709 goto more;
0710 } else
0711 return bytes_read;
0712 } else
0713 return retval;
0714 } else
0715 return bytes_read;
0716 }
0717
0718 static void ftdi_elan_write_bulk_callback(struct urb *urb)
0719 {
0720 struct usb_ftdi *ftdi = urb->context;
0721 int status = urb->status;
0722
0723 if (status && !(status == -ENOENT || status == -ECONNRESET ||
0724 status == -ESHUTDOWN)) {
0725 dev_err(&ftdi->udev->dev,
0726 "urb=%p write bulk status received: %d\n", urb, status);
0727 }
0728 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
0729 urb->transfer_buffer, urb->transfer_dma);
0730 }
0731
0732 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
0733 char *buf, int command_size, int total_size)
0734 {
0735 int ed_commands = 0;
0736 int b = 0;
0737 int I = command_size;
0738 int i = ftdi->command_head;
0739 while (I-- > 0) {
0740 struct u132_command *command = &ftdi->command[COMMAND_MASK &
0741 i++];
0742 int F = command->follows;
0743 u8 *f = command->buffer;
0744 if (command->header & 0x80) {
0745 ed_commands |= 1 << (0x3 & (command->header >> 5));
0746 }
0747 buf[b++] = command->header;
0748 buf[b++] = (command->length >> 0) & 0x00FF;
0749 buf[b++] = (command->length >> 8) & 0x00FF;
0750 buf[b++] = command->address;
0751 buf[b++] = command->width;
0752 while (F-- > 0) {
0753 buf[b++] = *f++;
0754 }
0755 }
0756 return ed_commands;
0757 }
0758
0759 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
0760 {
0761 int total_size = 0;
0762 int I = command_size;
0763 int i = ftdi->command_head;
0764 while (I-- > 0) {
0765 struct u132_command *command = &ftdi->command[COMMAND_MASK &
0766 i++];
0767 total_size += 5 + command->follows;
0768 }
0769 return total_size;
0770 }
0771
0772 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
0773 {
0774 int retval;
0775 char *buf;
0776 int ed_commands;
0777 int total_size;
0778 struct urb *urb;
0779 int command_size = ftdi->command_next - ftdi->command_head;
0780 if (command_size == 0)
0781 return 0;
0782 total_size = ftdi_elan_total_command_size(ftdi, command_size);
0783 urb = usb_alloc_urb(0, GFP_KERNEL);
0784 if (!urb)
0785 return -ENOMEM;
0786 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
0787 &urb->transfer_dma);
0788 if (!buf) {
0789 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
0790 command_size, total_size);
0791 usb_free_urb(urb);
0792 return -ENOMEM;
0793 }
0794 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
0795 command_size, total_size);
0796 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
0797 ftdi->bulk_out_endpointAddr), buf, total_size,
0798 ftdi_elan_write_bulk_callback, ftdi);
0799 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0800 if (ed_commands) {
0801 char diag[40 *3 + 4];
0802 char *d = diag;
0803 int m = total_size;
0804 u8 *c = buf;
0805 int s = (sizeof(diag) - 1) / 3;
0806 diag[0] = 0;
0807 while (s-- > 0 && m-- > 0) {
0808 if (s > 0 || m == 0) {
0809 d += sprintf(d, " %02X", *c++);
0810 } else
0811 d += sprintf(d, " ..");
0812 }
0813 }
0814 retval = usb_submit_urb(urb, GFP_KERNEL);
0815 if (retval) {
0816 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
0817 retval, urb, command_size, total_size);
0818 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
0819 usb_free_urb(urb);
0820 return retval;
0821 }
0822 usb_free_urb(urb);
0823
0824 ftdi->command_head += command_size;
0825 ftdi_elan_kick_respond_queue(ftdi);
0826 return 0;
0827 }
0828
0829 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
0830 struct u132_target *target, u8 *buffer, int length)
0831 {
0832 struct urb *urb = target->urb;
0833 int halted = target->halted;
0834 int skipped = target->skipped;
0835 int actual = target->actual;
0836 int non_null = target->non_null;
0837 int toggle_bits = target->toggle_bits;
0838 int error_count = target->error_count;
0839 int condition_code = target->condition_code;
0840 int repeat_number = target->repeat_number;
0841 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
0842 int, int, int, int) = target->callback;
0843 target->active -= 1;
0844 target->callback = NULL;
0845 (*callback) (target->endp, urb, buffer, length, toggle_bits,
0846 error_count, condition_code, repeat_number, halted, skipped,
0847 actual, non_null);
0848 }
0849
0850 static char *have_ed_set_response(struct usb_ftdi *ftdi,
0851 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
0852 char *b)
0853 {
0854 int payload = (ed_length >> 0) & 0x07FF;
0855 mutex_lock(&ftdi->u132_lock);
0856 target->actual = 0;
0857 target->non_null = (ed_length >> 15) & 0x0001;
0858 target->repeat_number = (ed_length >> 11) & 0x000F;
0859 if (ed_type == 0x02 || ed_type == 0x03) {
0860 if (payload == 0 || target->abandoning > 0) {
0861 target->abandoning = 0;
0862 mutex_unlock(&ftdi->u132_lock);
0863 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
0864 payload);
0865 ftdi->received = 0;
0866 ftdi->expected = 4;
0867 ftdi->ed_found = 0;
0868 return ftdi->response;
0869 } else {
0870 ftdi->expected = 4 + payload;
0871 ftdi->ed_found = 1;
0872 mutex_unlock(&ftdi->u132_lock);
0873 return b;
0874 }
0875 } else {
0876 target->abandoning = 0;
0877 mutex_unlock(&ftdi->u132_lock);
0878 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
0879 payload);
0880 ftdi->received = 0;
0881 ftdi->expected = 4;
0882 ftdi->ed_found = 0;
0883 return ftdi->response;
0884 }
0885 }
0886
0887 static char *have_ed_get_response(struct usb_ftdi *ftdi,
0888 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
0889 char *b)
0890 {
0891 mutex_lock(&ftdi->u132_lock);
0892 target->condition_code = TD_DEVNOTRESP;
0893 target->actual = (ed_length >> 0) & 0x01FF;
0894 target->non_null = (ed_length >> 15) & 0x0001;
0895 target->repeat_number = (ed_length >> 11) & 0x000F;
0896 mutex_unlock(&ftdi->u132_lock);
0897 if (target->active)
0898 ftdi_elan_do_callback(ftdi, target, NULL, 0);
0899 target->abandoning = 0;
0900 ftdi->received = 0;
0901 ftdi->expected = 4;
0902 ftdi->ed_found = 0;
0903 return ftdi->response;
0904 }
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
0916 {
0917 u8 *b = ftdi->response + ftdi->received;
0918 int bytes_read = 0;
0919 int retry_on_empty = 1;
0920 int retry_on_timeout = 3;
0921 read:{
0922 int packet_bytes = 0;
0923 int retval = usb_bulk_msg(ftdi->udev,
0924 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
0925 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
0926 &packet_bytes, 500);
0927 char diag[30 *3 + 4];
0928 char *d = diag;
0929 int m = packet_bytes;
0930 u8 *c = ftdi->bulk_in_buffer;
0931 int s = (sizeof(diag) - 1) / 3;
0932 diag[0] = 0;
0933 while (s-- > 0 && m-- > 0) {
0934 if (s > 0 || m == 0) {
0935 d += sprintf(d, " %02X", *c++);
0936 } else
0937 d += sprintf(d, " ..");
0938 }
0939 if (packet_bytes > 2) {
0940 ftdi->bulk_in_left = packet_bytes - 2;
0941 ftdi->bulk_in_last = 1;
0942 goto have;
0943 } else if (retval == -ETIMEDOUT) {
0944 if (retry_on_timeout-- > 0) {
0945 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
0946 packet_bytes, bytes_read, diag);
0947 goto more;
0948 } else if (bytes_read > 0) {
0949 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
0950 bytes_read, diag);
0951 return -ENOMEM;
0952 } else {
0953 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
0954 packet_bytes, bytes_read, diag);
0955 return -ENOMEM;
0956 }
0957 } else if (retval == -EILSEQ) {
0958 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
0959 retval, packet_bytes, bytes_read, diag);
0960 return retval;
0961 } else if (retval) {
0962 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
0963 retval, packet_bytes, bytes_read, diag);
0964 return retval;
0965 } else {
0966 if (retry_on_empty-- > 0) {
0967 goto more;
0968 } else
0969 return 0;
0970 }
0971 }
0972 more:{
0973 goto read;
0974 }
0975 have:if (ftdi->bulk_in_left > 0) {
0976 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
0977 bytes_read += 1;
0978 ftdi->bulk_in_left -= 1;
0979 if (ftdi->received == 0 && c == 0xFF) {
0980 goto have;
0981 } else
0982 *b++ = c;
0983 if (++ftdi->received < ftdi->expected) {
0984 goto have;
0985 } else if (ftdi->ed_found) {
0986 int ed_number = (ftdi->response[0] >> 5) & 0x03;
0987 u16 ed_length = (ftdi->response[2] << 8) |
0988 ftdi->response[1];
0989 struct u132_target *target = &ftdi->target[ed_number];
0990 int payload = (ed_length >> 0) & 0x07FF;
0991 char diag[30 *3 + 4];
0992 char *d = diag;
0993 int m = payload;
0994 u8 *c = 4 + ftdi->response;
0995 int s = (sizeof(diag) - 1) / 3;
0996 diag[0] = 0;
0997 while (s-- > 0 && m-- > 0) {
0998 if (s > 0 || m == 0) {
0999 d += sprintf(d, " %02X", *c++);
1000 } else
1001 d += sprintf(d, " ..");
1002 }
1003 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1004 payload);
1005 ftdi->received = 0;
1006 ftdi->expected = 4;
1007 ftdi->ed_found = 0;
1008 b = ftdi->response;
1009 goto have;
1010 } else if (ftdi->expected == 8) {
1011 u8 buscmd;
1012 int respond_head = ftdi->respond_head++;
1013 struct u132_respond *respond = &ftdi->respond[
1014 RESPOND_MASK & respond_head];
1015 u32 data = ftdi->response[7];
1016 data <<= 8;
1017 data |= ftdi->response[6];
1018 data <<= 8;
1019 data |= ftdi->response[5];
1020 data <<= 8;
1021 data |= ftdi->response[4];
1022 *respond->value = data;
1023 *respond->result = 0;
1024 complete(&respond->wait_completion);
1025 ftdi->received = 0;
1026 ftdi->expected = 4;
1027 ftdi->ed_found = 0;
1028 b = ftdi->response;
1029 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1030 if (buscmd == 0x00) {
1031 } else if (buscmd == 0x02) {
1032 } else if (buscmd == 0x06) {
1033 } else if (buscmd == 0x0A) {
1034 } else
1035 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1036 buscmd, data);
1037 goto have;
1038 } else {
1039 if ((ftdi->response[0] & 0x80) == 0x00) {
1040 ftdi->expected = 8;
1041 goto have;
1042 } else {
1043 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1044 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1045 u16 ed_length = (ftdi->response[2] << 8) |
1046 ftdi->response[1];
1047 struct u132_target *target = &ftdi->target[
1048 ed_number];
1049 target->halted = (ftdi->response[0] >> 3) &
1050 0x01;
1051 target->skipped = (ftdi->response[0] >> 2) &
1052 0x01;
1053 target->toggle_bits = (ftdi->response[3] >> 6)
1054 & 0x03;
1055 target->error_count = (ftdi->response[3] >> 4)
1056 & 0x03;
1057 target->condition_code = (ftdi->response[
1058 3] >> 0) & 0x0F;
1059 if ((ftdi->response[0] & 0x10) == 0x00) {
1060 b = have_ed_set_response(ftdi, target,
1061 ed_length, ed_number, ed_type,
1062 b);
1063 goto have;
1064 } else {
1065 b = have_ed_get_response(ftdi, target,
1066 ed_length, ed_number, ed_type,
1067 b);
1068 goto have;
1069 }
1070 }
1071 }
1072 } else
1073 goto more;
1074 }
1075
1076
1077
1078
1079
1080
1081 static ssize_t ftdi_elan_write(struct file *file,
1082 const char __user *user_buffer, size_t count,
1083 loff_t *ppos)
1084 {
1085 int retval = 0;
1086 struct urb *urb;
1087 char *buf;
1088 struct usb_ftdi *ftdi = file->private_data;
1089
1090 if (ftdi->disconnected > 0) {
1091 return -ENODEV;
1092 }
1093 if (count == 0) {
1094 goto exit;
1095 }
1096 urb = usb_alloc_urb(0, GFP_KERNEL);
1097 if (!urb) {
1098 retval = -ENOMEM;
1099 goto error_1;
1100 }
1101 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1102 &urb->transfer_dma);
1103 if (!buf) {
1104 retval = -ENOMEM;
1105 goto error_2;
1106 }
1107 if (copy_from_user(buf, user_buffer, count)) {
1108 retval = -EFAULT;
1109 goto error_3;
1110 }
1111 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1112 ftdi->bulk_out_endpointAddr), buf, count,
1113 ftdi_elan_write_bulk_callback, ftdi);
1114 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1115 retval = usb_submit_urb(urb, GFP_KERNEL);
1116 if (retval) {
1117 dev_err(&ftdi->udev->dev,
1118 "failed submitting write urb, error %d\n", retval);
1119 goto error_3;
1120 }
1121 usb_free_urb(urb);
1122
1123 exit:
1124 return count;
1125 error_3:
1126 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
1127 error_2:
1128 usb_free_urb(urb);
1129 error_1:
1130 return retval;
1131 }
1132
1133 static const struct file_operations ftdi_elan_fops = {
1134 .owner = THIS_MODULE,
1135 .llseek = no_llseek,
1136 .read = ftdi_elan_read,
1137 .write = ftdi_elan_write,
1138 .open = ftdi_elan_open,
1139 .release = ftdi_elan_release,
1140 };
1141
1142
1143
1144
1145
1146 static struct usb_class_driver ftdi_elan_jtag_class = {
1147 .name = "ftdi-%d-jtag",
1148 .fops = &ftdi_elan_fops,
1149 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
1150 };
1151
1152
1153
1154
1155
1156
1157 #define cPCIu132rd 0x0
1158 #define cPCIu132wr 0x1
1159 #define cPCIiord 0x2
1160 #define cPCIiowr 0x3
1161 #define cPCImemrd 0x6
1162 #define cPCImemwr 0x7
1163 #define cPCIcfgrd 0xA
1164 #define cPCIcfgwr 0xB
1165 #define cPCInull 0xF
1166 #define cU132cmd_status 0x0
1167 #define cU132flash 0x1
1168 #define cPIDsetup 0x0
1169 #define cPIDout 0x1
1170 #define cPIDin 0x2
1171 #define cPIDinonce 0x3
1172 #define cCCnoerror 0x0
1173 #define cCCcrc 0x1
1174 #define cCCbitstuff 0x2
1175 #define cCCtoggle 0x3
1176 #define cCCstall 0x4
1177 #define cCCnoresp 0x5
1178 #define cCCbadpid1 0x6
1179 #define cCCbadpid2 0x7
1180 #define cCCdataoverrun 0x8
1181 #define cCCdataunderrun 0x9
1182 #define cCCbuffoverrun 0xC
1183 #define cCCbuffunderrun 0xD
1184 #define cCCnotaccessed 0xF
1185 static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1186 {
1187 wait:if (ftdi->disconnected > 0) {
1188 return -ENODEV;
1189 } else {
1190 int command_size;
1191 mutex_lock(&ftdi->u132_lock);
1192 command_size = ftdi->command_next - ftdi->command_head;
1193 if (command_size < COMMAND_SIZE) {
1194 struct u132_command *command = &ftdi->command[
1195 COMMAND_MASK & ftdi->command_next];
1196 command->header = 0x00 | cPCIu132wr;
1197 command->length = 0x04;
1198 command->address = 0x00;
1199 command->width = 0x00;
1200 command->follows = 4;
1201 command->value = data;
1202 command->buffer = &command->value;
1203 ftdi->command_next += 1;
1204 ftdi_elan_kick_command_queue(ftdi);
1205 mutex_unlock(&ftdi->u132_lock);
1206 return 0;
1207 } else {
1208 mutex_unlock(&ftdi->u132_lock);
1209 msleep(100);
1210 goto wait;
1211 }
1212 }
1213 }
1214
1215 static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
1216 u8 width, u32 data)
1217 {
1218 u8 addressofs = config_offset / 4;
1219 wait:if (ftdi->disconnected > 0) {
1220 return -ENODEV;
1221 } else {
1222 int command_size;
1223 mutex_lock(&ftdi->u132_lock);
1224 command_size = ftdi->command_next - ftdi->command_head;
1225 if (command_size < COMMAND_SIZE) {
1226 struct u132_command *command = &ftdi->command[
1227 COMMAND_MASK & ftdi->command_next];
1228 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1229 command->length = 0x04;
1230 command->address = addressofs;
1231 command->width = 0x00 | (width & 0x0F);
1232 command->follows = 4;
1233 command->value = data;
1234 command->buffer = &command->value;
1235 ftdi->command_next += 1;
1236 ftdi_elan_kick_command_queue(ftdi);
1237 mutex_unlock(&ftdi->u132_lock);
1238 return 0;
1239 } else {
1240 mutex_unlock(&ftdi->u132_lock);
1241 msleep(100);
1242 goto wait;
1243 }
1244 }
1245 }
1246
1247 static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1248 u8 width, u32 data)
1249 {
1250 u8 addressofs = mem_offset / 4;
1251 wait:if (ftdi->disconnected > 0) {
1252 return -ENODEV;
1253 } else {
1254 int command_size;
1255 mutex_lock(&ftdi->u132_lock);
1256 command_size = ftdi->command_next - ftdi->command_head;
1257 if (command_size < COMMAND_SIZE) {
1258 struct u132_command *command = &ftdi->command[
1259 COMMAND_MASK & ftdi->command_next];
1260 command->header = 0x00 | (cPCImemwr & 0x0F);
1261 command->length = 0x04;
1262 command->address = addressofs;
1263 command->width = 0x00 | (width & 0x0F);
1264 command->follows = 4;
1265 command->value = data;
1266 command->buffer = &command->value;
1267 ftdi->command_next += 1;
1268 ftdi_elan_kick_command_queue(ftdi);
1269 mutex_unlock(&ftdi->u132_lock);
1270 return 0;
1271 } else {
1272 mutex_unlock(&ftdi->u132_lock);
1273 msleep(100);
1274 goto wait;
1275 }
1276 }
1277 }
1278
1279 int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
1280 u8 width, u32 data)
1281 {
1282 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1283 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
1284 }
1285
1286
1287 EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1288 static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1289 {
1290 wait:if (ftdi->disconnected > 0) {
1291 return -ENODEV;
1292 } else {
1293 int command_size;
1294 int respond_size;
1295 mutex_lock(&ftdi->u132_lock);
1296 command_size = ftdi->command_next - ftdi->command_head;
1297 respond_size = ftdi->respond_next - ftdi->respond_head;
1298 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1299 {
1300 struct u132_command *command = &ftdi->command[
1301 COMMAND_MASK & ftdi->command_next];
1302 struct u132_respond *respond = &ftdi->respond[
1303 RESPOND_MASK & ftdi->respond_next];
1304 int result = -ENODEV;
1305 respond->result = &result;
1306 respond->header = command->header = 0x00 | cPCIu132rd;
1307 command->length = 0x04;
1308 respond->address = command->address = cU132cmd_status;
1309 command->width = 0x00;
1310 command->follows = 0;
1311 command->value = 0;
1312 command->buffer = NULL;
1313 respond->value = data;
1314 init_completion(&respond->wait_completion);
1315 ftdi->command_next += 1;
1316 ftdi->respond_next += 1;
1317 ftdi_elan_kick_command_queue(ftdi);
1318 mutex_unlock(&ftdi->u132_lock);
1319 wait_for_completion(&respond->wait_completion);
1320 return result;
1321 } else {
1322 mutex_unlock(&ftdi->u132_lock);
1323 msleep(100);
1324 goto wait;
1325 }
1326 }
1327 }
1328
1329 static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
1330 u8 width, u32 *data)
1331 {
1332 u8 addressofs = config_offset / 4;
1333 wait:if (ftdi->disconnected > 0) {
1334 return -ENODEV;
1335 } else {
1336 int command_size;
1337 int respond_size;
1338 mutex_lock(&ftdi->u132_lock);
1339 command_size = ftdi->command_next - ftdi->command_head;
1340 respond_size = ftdi->respond_next - ftdi->respond_head;
1341 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1342 {
1343 struct u132_command *command = &ftdi->command[
1344 COMMAND_MASK & ftdi->command_next];
1345 struct u132_respond *respond = &ftdi->respond[
1346 RESPOND_MASK & ftdi->respond_next];
1347 int result = -ENODEV;
1348 respond->result = &result;
1349 respond->header = command->header = 0x00 | (cPCIcfgrd &
1350 0x0F);
1351 command->length = 0x04;
1352 respond->address = command->address = addressofs;
1353 command->width = 0x00 | (width & 0x0F);
1354 command->follows = 0;
1355 command->value = 0;
1356 command->buffer = NULL;
1357 respond->value = data;
1358 init_completion(&respond->wait_completion);
1359 ftdi->command_next += 1;
1360 ftdi->respond_next += 1;
1361 ftdi_elan_kick_command_queue(ftdi);
1362 mutex_unlock(&ftdi->u132_lock);
1363 wait_for_completion(&respond->wait_completion);
1364 return result;
1365 } else {
1366 mutex_unlock(&ftdi->u132_lock);
1367 msleep(100);
1368 goto wait;
1369 }
1370 }
1371 }
1372
1373 static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
1374 u8 width, u32 *data)
1375 {
1376 u8 addressofs = mem_offset / 4;
1377 wait:if (ftdi->disconnected > 0) {
1378 return -ENODEV;
1379 } else {
1380 int command_size;
1381 int respond_size;
1382 mutex_lock(&ftdi->u132_lock);
1383 command_size = ftdi->command_next - ftdi->command_head;
1384 respond_size = ftdi->respond_next - ftdi->respond_head;
1385 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1386 {
1387 struct u132_command *command = &ftdi->command[
1388 COMMAND_MASK & ftdi->command_next];
1389 struct u132_respond *respond = &ftdi->respond[
1390 RESPOND_MASK & ftdi->respond_next];
1391 int result = -ENODEV;
1392 respond->result = &result;
1393 respond->header = command->header = 0x00 | (cPCImemrd &
1394 0x0F);
1395 command->length = 0x04;
1396 respond->address = command->address = addressofs;
1397 command->width = 0x00 | (width & 0x0F);
1398 command->follows = 0;
1399 command->value = 0;
1400 command->buffer = NULL;
1401 respond->value = data;
1402 init_completion(&respond->wait_completion);
1403 ftdi->command_next += 1;
1404 ftdi->respond_next += 1;
1405 ftdi_elan_kick_command_queue(ftdi);
1406 mutex_unlock(&ftdi->u132_lock);
1407 wait_for_completion(&respond->wait_completion);
1408 return result;
1409 } else {
1410 mutex_unlock(&ftdi->u132_lock);
1411 msleep(100);
1412 goto wait;
1413 }
1414 }
1415 }
1416
1417 int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
1418 u8 width, u32 *data)
1419 {
1420 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1421 if (ftdi->initialized == 0) {
1422 return -ENODEV;
1423 } else
1424 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
1425 }
1426
1427
1428 EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1429 static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
1430 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1431 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1432 int toggle_bits, int error_count, int condition_code, int repeat_number,
1433 int halted, int skipped, int actual, int non_null))
1434 {
1435 u8 ed = ed_number - 1;
1436 wait:if (ftdi->disconnected > 0) {
1437 return -ENODEV;
1438 } else if (ftdi->initialized == 0) {
1439 return -ENODEV;
1440 } else {
1441 int command_size;
1442 mutex_lock(&ftdi->u132_lock);
1443 command_size = ftdi->command_next - ftdi->command_head;
1444 if (command_size < COMMAND_SIZE) {
1445 struct u132_target *target = &ftdi->target[ed];
1446 struct u132_command *command = &ftdi->command[
1447 COMMAND_MASK & ftdi->command_next];
1448 command->header = 0x80 | (ed << 5);
1449 command->length = 0x8007;
1450 command->address = (toggle_bits << 6) | (ep_number << 2)
1451 | (address << 0);
1452 command->width = usb_maxpacket(urb->dev, urb->pipe);
1453 command->follows = 8;
1454 command->value = 0;
1455 command->buffer = urb->setup_packet;
1456 target->callback = callback;
1457 target->endp = endp;
1458 target->urb = urb;
1459 target->active = 1;
1460 ftdi->command_next += 1;
1461 ftdi_elan_kick_command_queue(ftdi);
1462 mutex_unlock(&ftdi->u132_lock);
1463 return 0;
1464 } else {
1465 mutex_unlock(&ftdi->u132_lock);
1466 msleep(100);
1467 goto wait;
1468 }
1469 }
1470 }
1471
1472 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1473 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1474 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1475 int toggle_bits, int error_count, int condition_code, int repeat_number,
1476 int halted, int skipped, int actual, int non_null))
1477 {
1478 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1479 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1480 ep_number, toggle_bits, callback);
1481 }
1482
1483
1484 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1485 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1486 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1487 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1488 int toggle_bits, int error_count, int condition_code, int repeat_number,
1489 int halted, int skipped, int actual, int non_null))
1490 {
1491 u8 ed = ed_number - 1;
1492 wait:if (ftdi->disconnected > 0) {
1493 return -ENODEV;
1494 } else if (ftdi->initialized == 0) {
1495 return -ENODEV;
1496 } else {
1497 int command_size;
1498 mutex_lock(&ftdi->u132_lock);
1499 command_size = ftdi->command_next - ftdi->command_head;
1500 if (command_size < COMMAND_SIZE) {
1501 struct u132_target *target = &ftdi->target[ed];
1502 struct u132_command *command = &ftdi->command[
1503 COMMAND_MASK & ftdi->command_next];
1504 u32 remaining_length = urb->transfer_buffer_length -
1505 urb->actual_length;
1506 command->header = 0x82 | (ed << 5);
1507 if (remaining_length == 0) {
1508 command->length = 0x0000;
1509 } else if (remaining_length > 1024) {
1510 command->length = 0x8000 | 1023;
1511 } else
1512 command->length = 0x8000 | (remaining_length -
1513 1);
1514 command->address = (toggle_bits << 6) | (ep_number << 2)
1515 | (address << 0);
1516 command->width = usb_maxpacket(urb->dev, urb->pipe);
1517 command->follows = 0;
1518 command->value = 0;
1519 command->buffer = NULL;
1520 target->callback = callback;
1521 target->endp = endp;
1522 target->urb = urb;
1523 target->active = 1;
1524 ftdi->command_next += 1;
1525 ftdi_elan_kick_command_queue(ftdi);
1526 mutex_unlock(&ftdi->u132_lock);
1527 return 0;
1528 } else {
1529 mutex_unlock(&ftdi->u132_lock);
1530 msleep(100);
1531 goto wait;
1532 }
1533 }
1534 }
1535
1536 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1537 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1538 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1539 int toggle_bits, int error_count, int condition_code, int repeat_number,
1540 int halted, int skipped, int actual, int non_null))
1541 {
1542 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1543 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1544 ep_number, toggle_bits, callback);
1545 }
1546
1547
1548 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1549 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1550 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1551 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1552 int toggle_bits, int error_count, int condition_code, int repeat_number,
1553 int halted, int skipped, int actual, int non_null))
1554 {
1555 u8 ed = ed_number - 1;
1556 wait:if (ftdi->disconnected > 0) {
1557 return -ENODEV;
1558 } else if (ftdi->initialized == 0) {
1559 return -ENODEV;
1560 } else {
1561 int command_size;
1562 mutex_lock(&ftdi->u132_lock);
1563 command_size = ftdi->command_next - ftdi->command_head;
1564 if (command_size < COMMAND_SIZE) {
1565 struct u132_target *target = &ftdi->target[ed];
1566 struct u132_command *command = &ftdi->command[
1567 COMMAND_MASK & ftdi->command_next];
1568 command->header = 0x81 | (ed << 5);
1569 command->length = 0x0000;
1570 command->address = (toggle_bits << 6) | (ep_number << 2)
1571 | (address << 0);
1572 command->width = usb_maxpacket(urb->dev, urb->pipe);
1573 command->follows = 0;
1574 command->value = 0;
1575 command->buffer = NULL;
1576 target->callback = callback;
1577 target->endp = endp;
1578 target->urb = urb;
1579 target->active = 1;
1580 ftdi->command_next += 1;
1581 ftdi_elan_kick_command_queue(ftdi);
1582 mutex_unlock(&ftdi->u132_lock);
1583 return 0;
1584 } else {
1585 mutex_unlock(&ftdi->u132_lock);
1586 msleep(100);
1587 goto wait;
1588 }
1589 }
1590 }
1591
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))
1597 {
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);
1601 }
1602
1603
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))
1610 {
1611 u8 ed = ed_number - 1;
1612 wait:if (ftdi->disconnected > 0) {
1613 return -ENODEV;
1614 } else if (ftdi->initialized == 0) {
1615 return -ENODEV;
1616 } else {
1617 int command_size;
1618 mutex_lock(&ftdi->u132_lock);
1619 command_size = ftdi->command_next - ftdi->command_head;
1620 if (command_size < COMMAND_SIZE) {
1621 u8 *b;
1622 u16 urb_size;
1623 int i = 0;
1624 char data[30 *3 + 4];
1625 char *d = data;
1626 int m = (sizeof(data) - 1) / 3 - 1;
1627 int l = 0;
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)
1633 | (address << 0);
1634 command->width = usb_maxpacket(urb->dev, urb->pipe);
1635 command->follows = min_t(u32, 1024,
1636 urb->transfer_buffer_length -
1637 urb->actual_length);
1638 command->value = 0;
1639 command->buffer = urb->transfer_buffer +
1640 urb->actual_length;
1641 command->length = 0x8000 | (command->follows - 1);
1642 b = command->buffer;
1643 urb_size = command->follows;
1644 data[0] = 0;
1645 while (urb_size-- > 0) {
1646 if (i > m) {
1647 } else if (i++ < m) {
1648 int w = sprintf(d, " %02X", *b++);
1649 d += w;
1650 l += w;
1651 } else
1652 d += sprintf(d, " ..");
1653 }
1654 target->callback = callback;
1655 target->endp = endp;
1656 target->urb = urb;
1657 target->active = 1;
1658 ftdi->command_next += 1;
1659 ftdi_elan_kick_command_queue(ftdi);
1660 mutex_unlock(&ftdi->u132_lock);
1661 return 0;
1662 } else {
1663 mutex_unlock(&ftdi->u132_lock);
1664 msleep(100);
1665 goto wait;
1666 }
1667 }
1668 }
1669
1670 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1671 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1672 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1673 int toggle_bits, int error_count, int condition_code, int repeat_number,
1674 int halted, int skipped, int actual, int non_null))
1675 {
1676 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1677 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1678 ep_number, toggle_bits, callback);
1679 }
1680
1681
1682 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1683 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1684 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1685 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1686 int toggle_bits, int error_count, int condition_code, int repeat_number,
1687 int halted, int skipped, int actual, int non_null))
1688 {
1689 u8 ed = ed_number - 1;
1690 wait:if (ftdi->disconnected > 0) {
1691 return -ENODEV;
1692 } else if (ftdi->initialized == 0) {
1693 return -ENODEV;
1694 } else {
1695 int command_size;
1696 mutex_lock(&ftdi->u132_lock);
1697 command_size = ftdi->command_next - ftdi->command_head;
1698 if (command_size < COMMAND_SIZE) {
1699 u32 remaining_length = urb->transfer_buffer_length -
1700 urb->actual_length;
1701 struct u132_target *target = &ftdi->target[ed];
1702 struct u132_command *command = &ftdi->command[
1703 COMMAND_MASK & ftdi->command_next];
1704 command->header = 0x83 | (ed << 5);
1705 if (remaining_length == 0) {
1706 command->length = 0x0000;
1707 } else if (remaining_length > 1024) {
1708 command->length = 0x8000 | 1023;
1709 } else
1710 command->length = 0x8000 | (remaining_length -
1711 1);
1712 command->address = (toggle_bits << 6) | (ep_number << 2)
1713 | (address << 0);
1714 command->width = usb_maxpacket(urb->dev, urb->pipe);
1715 command->follows = 0;
1716 command->value = 0;
1717 command->buffer = NULL;
1718 target->callback = callback;
1719 target->endp = endp;
1720 target->urb = urb;
1721 target->active = 1;
1722 ftdi->command_next += 1;
1723 ftdi_elan_kick_command_queue(ftdi);
1724 mutex_unlock(&ftdi->u132_lock);
1725 return 0;
1726 } else {
1727 mutex_unlock(&ftdi->u132_lock);
1728 msleep(100);
1729 goto wait;
1730 }
1731 }
1732 }
1733
1734 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1735 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1736 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1737 int toggle_bits, int error_count, int condition_code, int repeat_number,
1738 int halted, int skipped, int actual, int non_null))
1739 {
1740 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1741 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1742 ep_number, toggle_bits, callback);
1743 }
1744
1745
1746 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1747 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1748 void *endp)
1749 {
1750 u8 ed = ed_number - 1;
1751 if (ftdi->disconnected > 0) {
1752 return -ENODEV;
1753 } else if (ftdi->initialized == 0) {
1754 return -ENODEV;
1755 } else {
1756 struct u132_target *target = &ftdi->target[ed];
1757 mutex_lock(&ftdi->u132_lock);
1758 if (target->abandoning > 0) {
1759 mutex_unlock(&ftdi->u132_lock);
1760 return 0;
1761 } else {
1762 target->abandoning = 1;
1763 wait_1:if (target->active == 1) {
1764 int command_size = ftdi->command_next -
1765 ftdi->command_head;
1766 if (command_size < COMMAND_SIZE) {
1767 struct u132_command *command =
1768 &ftdi->command[COMMAND_MASK &
1769 ftdi->command_next];
1770 command->header = 0x80 | (ed << 5) |
1771 0x4;
1772 command->length = 0x00;
1773 command->address = 0x00;
1774 command->width = 0x00;
1775 command->follows = 0;
1776 command->value = 0;
1777 command->buffer = &command->value;
1778 ftdi->command_next += 1;
1779 ftdi_elan_kick_command_queue(ftdi);
1780 } else {
1781 mutex_unlock(&ftdi->u132_lock);
1782 msleep(100);
1783 mutex_lock(&ftdi->u132_lock);
1784 goto wait_1;
1785 }
1786 }
1787 mutex_unlock(&ftdi->u132_lock);
1788 return 0;
1789 }
1790 }
1791 }
1792
1793 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1794 void *endp)
1795 {
1796 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1797 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1798 }
1799
1800
1801 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1802 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1803 {
1804 int retry_on_empty = 10;
1805 int retry_on_timeout = 5;
1806 int retry_on_status = 20;
1807 more:{
1808 int packet_bytes = 0;
1809 int retval = usb_bulk_msg(ftdi->udev,
1810 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1811 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1812 &packet_bytes, 100);
1813 if (packet_bytes > 2) {
1814 char diag[30 *3 + 4];
1815 char *d = diag;
1816 int m = (sizeof(diag) - 1) / 3 - 1;
1817 char *b = ftdi->bulk_in_buffer;
1818 int bytes_read = 0;
1819 diag[0] = 0;
1820 while (packet_bytes-- > 0) {
1821 char c = *b++;
1822 if (bytes_read < m) {
1823 d += sprintf(d, " %02X",
1824 0x000000FF & c);
1825 } else if (bytes_read > m) {
1826 } else
1827 d += sprintf(d, " ..");
1828 bytes_read += 1;
1829 continue;
1830 }
1831 goto more;
1832 } else if (packet_bytes > 1) {
1833 char s1 = ftdi->bulk_in_buffer[0];
1834 char s2 = ftdi->bulk_in_buffer[1];
1835 if (s1 == 0x31 && s2 == 0x60) {
1836 return 0;
1837 } else if (retry_on_status-- > 0) {
1838 goto more;
1839 } else {
1840 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1841 return -EFAULT;
1842 }
1843 } else if (packet_bytes > 0) {
1844 char b1 = ftdi->bulk_in_buffer[0];
1845 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1846 b1);
1847 if (retry_on_status-- > 0) {
1848 goto more;
1849 } else {
1850 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1851 return -EFAULT;
1852 }
1853 } else if (retval == -ETIMEDOUT) {
1854 if (retry_on_timeout-- > 0) {
1855 goto more;
1856 } else {
1857 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1858 return -ENOMEM;
1859 }
1860 } else if (retval == 0) {
1861 if (retry_on_empty-- > 0) {
1862 goto more;
1863 } else {
1864 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1865 return -ENOMEM;
1866 }
1867 } else {
1868 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1869 return retval;
1870 }
1871 }
1872 return -1;
1873 }
1874
1875
1876
1877
1878
1879
1880 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1881 {
1882 int retval;
1883 struct urb *urb;
1884 char *buf;
1885 int I = 257;
1886 int i = 0;
1887 urb = usb_alloc_urb(0, GFP_KERNEL);
1888 if (!urb)
1889 return -ENOMEM;
1890 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1891 if (!buf) {
1892 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1893 usb_free_urb(urb);
1894 return -ENOMEM;
1895 }
1896 while (I-- > 0)
1897 buf[i++] = 0x55;
1898 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1899 ftdi->bulk_out_endpointAddr), buf, i,
1900 ftdi_elan_write_bulk_callback, ftdi);
1901 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1902 retval = usb_submit_urb(urb, GFP_KERNEL);
1903 if (retval) {
1904 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1905 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1906 usb_free_urb(urb);
1907 return -ENOMEM;
1908 }
1909 usb_free_urb(urb);
1910 return 0;
1911 }
1912
1913
1914
1915
1916
1917
1918 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1919 {
1920 int retval;
1921 struct urb *urb;
1922 char *buf;
1923 int I = 4;
1924 int i = 0;
1925 urb = usb_alloc_urb(0, GFP_KERNEL);
1926 if (!urb)
1927 return -ENOMEM;
1928 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1929 if (!buf) {
1930 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1931 usb_free_urb(urb);
1932 return -ENOMEM;
1933 }
1934 buf[i++] = 0x55;
1935 buf[i++] = 0xAA;
1936 buf[i++] = 0x5A;
1937 buf[i++] = 0xA5;
1938 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1939 ftdi->bulk_out_endpointAddr), buf, i,
1940 ftdi_elan_write_bulk_callback, ftdi);
1941 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1942 retval = usb_submit_urb(urb, GFP_KERNEL);
1943 if (retval) {
1944 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
1945 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1946 usb_free_urb(urb);
1947 return -ENOMEM;
1948 }
1949 usb_free_urb(urb);
1950 return 0;
1951 }
1952
1953 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
1954 {
1955 int retval;
1956 int long_stop = 10;
1957 int retry_on_timeout = 5;
1958 int retry_on_empty = 10;
1959 int err_count = 0;
1960 retval = ftdi_elan_flush_input_fifo(ftdi);
1961 if (retval)
1962 return retval;
1963 ftdi->bulk_in_left = 0;
1964 ftdi->bulk_in_last = -1;
1965 while (long_stop-- > 0) {
1966 int read_stop;
1967 int read_stuck;
1968 retval = ftdi_elan_synchronize_flush(ftdi);
1969 if (retval)
1970 return retval;
1971 retval = ftdi_elan_flush_input_fifo(ftdi);
1972 if (retval)
1973 return retval;
1974 reset:retval = ftdi_elan_synchronize_reset(ftdi);
1975 if (retval)
1976 return retval;
1977 read_stop = 100;
1978 read_stuck = 10;
1979 read:{
1980 int packet_bytes = 0;
1981 retval = usb_bulk_msg(ftdi->udev,
1982 usb_rcvbulkpipe(ftdi->udev,
1983 ftdi->bulk_in_endpointAddr),
1984 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1985 &packet_bytes, 500);
1986 if (packet_bytes > 2) {
1987 char diag[30 *3 + 4];
1988 char *d = diag;
1989 int m = (sizeof(diag) - 1) / 3 - 1;
1990 char *b = ftdi->bulk_in_buffer;
1991 int bytes_read = 0;
1992 unsigned char c = 0;
1993 diag[0] = 0;
1994 while (packet_bytes-- > 0) {
1995 c = *b++;
1996 if (bytes_read < m) {
1997 d += sprintf(d, " %02X", c);
1998 } else if (bytes_read > m) {
1999 } else
2000 d += sprintf(d, " ..");
2001 bytes_read += 1;
2002 continue;
2003 }
2004 if (c == 0x7E) {
2005 return 0;
2006 } else {
2007 if (c == 0x55) {
2008 goto read;
2009 } else if (read_stop-- > 0) {
2010 goto read;
2011 } else {
2012 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2013 continue;
2014 }
2015 }
2016 } else if (packet_bytes > 1) {
2017 unsigned char s1 = ftdi->bulk_in_buffer[0];
2018 unsigned char s2 = ftdi->bulk_in_buffer[1];
2019 if (s1 == 0x31 && s2 == 0x00) {
2020 if (read_stuck-- > 0) {
2021 goto read;
2022 } else
2023 goto reset;
2024 } else {
2025 if (read_stop-- > 0) {
2026 goto read;
2027 } else {
2028 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2029 continue;
2030 }
2031 }
2032 } else if (packet_bytes > 0) {
2033 if (read_stop-- > 0) {
2034 goto read;
2035 } else {
2036 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2037 continue;
2038 }
2039 } else if (retval == -ETIMEDOUT) {
2040 if (retry_on_timeout-- > 0) {
2041 goto read;
2042 } else {
2043 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2044 continue;
2045 }
2046 } else if (retval == 0) {
2047 if (retry_on_empty-- > 0) {
2048 goto read;
2049 } else {
2050 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2051 continue;
2052 }
2053 } else {
2054 err_count += 1;
2055 dev_err(&ftdi->udev->dev, "error = %d\n",
2056 retval);
2057 if (read_stop-- > 0) {
2058 goto read;
2059 } else {
2060 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2061 continue;
2062 }
2063 }
2064 }
2065 }
2066 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2067 return -EFAULT;
2068 }
2069
2070 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2071 {
2072 int retry_on_empty = 10;
2073 int retry_on_timeout = 5;
2074 int retry_on_status = 50;
2075 more:{
2076 int packet_bytes = 0;
2077 int retval = usb_bulk_msg(ftdi->udev,
2078 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2079 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2080 &packet_bytes, 1000);
2081 if (packet_bytes > 2) {
2082 char diag[30 *3 + 4];
2083 char *d = diag;
2084 int m = (sizeof(diag) - 1) / 3 - 1;
2085 char *b = ftdi->bulk_in_buffer;
2086 int bytes_read = 0;
2087 diag[0] = 0;
2088 while (packet_bytes-- > 0) {
2089 char c = *b++;
2090 if (bytes_read < m) {
2091 d += sprintf(d, " %02X",
2092 0x000000FF & c);
2093 } else if (bytes_read > m) {
2094 } else
2095 d += sprintf(d, " ..");
2096 bytes_read += 1;
2097 }
2098 goto more;
2099 } else if (packet_bytes > 1) {
2100 char s1 = ftdi->bulk_in_buffer[0];
2101 char s2 = ftdi->bulk_in_buffer[1];
2102 if (s1 == 0x31 && s2 == 0x60) {
2103 return 0;
2104 } else if (retry_on_status-- > 0) {
2105 msleep(5);
2106 goto more;
2107 } else
2108 return -EFAULT;
2109 } else if (packet_bytes > 0) {
2110 char b1 = ftdi->bulk_in_buffer[0];
2111 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2112 if (retry_on_status-- > 0) {
2113 msleep(5);
2114 goto more;
2115 } else {
2116 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2117 return -EFAULT;
2118 }
2119 } else if (retval == -ETIMEDOUT) {
2120 if (retry_on_timeout-- > 0) {
2121 goto more;
2122 } else {
2123 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2124 return -ENOMEM;
2125 }
2126 } else if (retval == 0) {
2127 if (retry_on_empty-- > 0) {
2128 goto more;
2129 } else {
2130 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2131 return -ENOMEM;
2132 }
2133 } else {
2134 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2135 return -ENOMEM;
2136 }
2137 }
2138 return -1;
2139 }
2140
2141 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2142 {
2143 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2144 if (UxxxStatus)
2145 return UxxxStatus;
2146 if (ftdi->controlreg & 0x00400000) {
2147 if (ftdi->card_ejected) {
2148 } else {
2149 ftdi->card_ejected = 1;
2150 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2151 ftdi->controlreg);
2152 }
2153 return -ENODEV;
2154 } else {
2155 u8 fn = ftdi->function - 1;
2156 int activePCIfn = fn << 8;
2157 u32 pcidata;
2158 u32 pciVID;
2159 u32 pciPID;
2160 int reg = 0;
2161 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2162 &pcidata);
2163 if (UxxxStatus)
2164 return UxxxStatus;
2165 pciVID = pcidata & 0xFFFF;
2166 pciPID = (pcidata >> 16) & 0xFFFF;
2167 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2168 ftdi->platform_data.device) {
2169 return 0;
2170 } else {
2171 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2172 ftdi->platform_data.vendor, pciVID,
2173 ftdi->platform_data.device, pciPID);
2174 return -ENODEV;
2175 }
2176 }
2177 }
2178
2179
2180 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2181 offsetof(struct ohci_regs, member), 0, data);
2182 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2183 offsetof(struct ohci_regs, member), 0, data);
2184
2185 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2186 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2187 OHCI_INTR_WDH)
2188 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2189 {
2190 int devices = 0;
2191 int retval;
2192 u32 hc_control;
2193 int num_ports;
2194 u32 control;
2195 u32 rh_a = -1;
2196 u32 status;
2197 u32 fminterval;
2198 u32 hc_fminterval;
2199 u32 periodicstart;
2200 u32 cmdstatus;
2201 u32 roothub_a;
2202 int mask = OHCI_INTR_INIT;
2203 int sleep_time = 0;
2204 int reset_timeout = 30;
2205 int temp;
2206 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2207 if (retval)
2208 return retval;
2209 retval = ftdi_read_pcimem(ftdi, control, &control);
2210 if (retval)
2211 return retval;
2212 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2213 if (retval)
2214 return retval;
2215 num_ports = rh_a & RH_A_NDP;
2216 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2217 if (retval)
2218 return retval;
2219 hc_fminterval &= 0x3fff;
2220 if (hc_fminterval != FI) {
2221 }
2222 hc_fminterval |= FSMP(hc_fminterval) << 16;
2223 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2224 if (retval)
2225 return retval;
2226 switch (hc_control & OHCI_CTRL_HCFS) {
2227 case OHCI_USB_OPER:
2228 sleep_time = 0;
2229 break;
2230 case OHCI_USB_SUSPEND:
2231 case OHCI_USB_RESUME:
2232 hc_control &= OHCI_CTRL_RWC;
2233 hc_control |= OHCI_USB_RESUME;
2234 sleep_time = 10;
2235 break;
2236 default:
2237 hc_control &= OHCI_CTRL_RWC;
2238 hc_control |= OHCI_USB_RESET;
2239 sleep_time = 50;
2240 break;
2241 }
2242 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2243 if (retval)
2244 return retval;
2245 retval = ftdi_read_pcimem(ftdi, control, &control);
2246 if (retval)
2247 return retval;
2248 msleep(sleep_time);
2249 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2250 if (retval)
2251 return retval;
2252 if (!(roothub_a & RH_A_NPS)) {
2253 for (temp = 0; temp < num_ports; temp++) {
2254 retval = ftdi_write_pcimem(ftdi,
2255 roothub.portstatus[temp], RH_PS_LSDA);
2256 if (retval)
2257 return retval;
2258 }
2259 }
2260 retval = ftdi_read_pcimem(ftdi, control, &control);
2261 if (retval)
2262 return retval;
2263 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2264 if (retval)
2265 return retval;
2266 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2267 if (retval)
2268 return retval;
2269 extra:{
2270 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2271 if (retval)
2272 return retval;
2273 if (0 != (status & OHCI_HCR)) {
2274 if (--reset_timeout == 0) {
2275 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2276 return -ENODEV;
2277 } else {
2278 msleep(5);
2279 goto extra;
2280 }
2281 }
2282 }
2283 if (quirk & OHCI_QUIRK_INITRESET) {
2284 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2285 if (retval)
2286 return retval;
2287 retval = ftdi_read_pcimem(ftdi, control, &control);
2288 if (retval)
2289 return retval;
2290 }
2291 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2292 if (retval)
2293 return retval;
2294 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2295 if (retval)
2296 return retval;
2297 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2298 if (retval)
2299 return retval;
2300 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2301 if (retval)
2302 return retval;
2303 retval = ftdi_write_pcimem(ftdi, fminterval,
2304 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2305 if (retval)
2306 return retval;
2307 retval = ftdi_write_pcimem(ftdi, periodicstart,
2308 ((9 *hc_fminterval) / 10) & 0x3fff);
2309 if (retval)
2310 return retval;
2311 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2312 if (retval)
2313 return retval;
2314 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2315 if (retval)
2316 return retval;
2317 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2318 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2319 quirk |= OHCI_QUIRK_INITRESET;
2320 goto retry;
2321 } else
2322 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2323 fminterval, periodicstart);
2324 }
2325 hc_control &= OHCI_CTRL_RWC;
2326 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2327 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2328 if (retval)
2329 return retval;
2330 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2331 if (retval)
2332 return retval;
2333 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2334 if (retval)
2335 return retval;
2336 retval = ftdi_read_pcimem(ftdi, control, &control);
2337 if (retval)
2338 return retval;
2339 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2340 if (retval)
2341 return retval;
2342 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2343 if (retval)
2344 return retval;
2345 retval = ftdi_write_pcimem(ftdi, intrdisable,
2346 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2347 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2348 OHCI_INTR_SO);
2349 if (retval)
2350 return retval;
2351 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2352 if (retval)
2353 return retval;
2354 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2355 if (quirk & OHCI_QUIRK_SUPERIO) {
2356 roothub_a |= RH_A_NOCP;
2357 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2358 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2359 if (retval)
2360 return retval;
2361 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2362 roothub_a |= RH_A_NPS;
2363 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2364 if (retval)
2365 return retval;
2366 }
2367 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2368 if (retval)
2369 return retval;
2370 retval = ftdi_write_pcimem(ftdi, roothub.b,
2371 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2372 if (retval)
2373 return retval;
2374 retval = ftdi_read_pcimem(ftdi, control, &control);
2375 if (retval)
2376 return retval;
2377 mdelay((roothub_a >> 23) & 0x1fe);
2378 for (temp = 0; temp < num_ports; temp++) {
2379 u32 portstatus;
2380 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2381 &portstatus);
2382 if (retval)
2383 return retval;
2384 if (1 & portstatus)
2385 devices += 1;
2386 }
2387 return devices;
2388 }
2389
2390 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2391 {
2392 u32 latence_timer;
2393 int UxxxStatus;
2394 u32 pcidata;
2395 int reg = 0;
2396 int activePCIfn = fn << 8;
2397 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2398 if (UxxxStatus)
2399 return UxxxStatus;
2400 reg = 16;
2401 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2402 0xFFFFFFFF);
2403 if (UxxxStatus)
2404 return UxxxStatus;
2405 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2406 &pcidata);
2407 if (UxxxStatus)
2408 return UxxxStatus;
2409 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2410 0xF0000000);
2411 if (UxxxStatus)
2412 return UxxxStatus;
2413 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2414 &pcidata);
2415 if (UxxxStatus)
2416 return UxxxStatus;
2417 reg = 12;
2418 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2419 &latence_timer);
2420 if (UxxxStatus)
2421 return UxxxStatus;
2422 latence_timer &= 0xFFFF00FF;
2423 latence_timer |= 0x00001600;
2424 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2425 latence_timer);
2426 if (UxxxStatus)
2427 return UxxxStatus;
2428 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2429 &pcidata);
2430 if (UxxxStatus)
2431 return UxxxStatus;
2432 reg = 4;
2433 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2434 0x06);
2435 if (UxxxStatus)
2436 return UxxxStatus;
2437 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2438 &pcidata);
2439 if (UxxxStatus)
2440 return UxxxStatus;
2441 for (reg = 0; reg <= 0x54; reg += 4) {
2442 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2443 if (UxxxStatus)
2444 return UxxxStatus;
2445 }
2446 return 0;
2447 }
2448
2449 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2450 {
2451 u32 latence_timer;
2452 int UxxxStatus;
2453 u32 pcidata;
2454 int reg = 0;
2455 int activePCIfn = fn << 8;
2456 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2457 if (UxxxStatus)
2458 return UxxxStatus;
2459 reg = 16;
2460 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2461 0xFFFFFFFF);
2462 if (UxxxStatus)
2463 return UxxxStatus;
2464 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2465 &pcidata);
2466 if (UxxxStatus)
2467 return UxxxStatus;
2468 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2469 0x00000000);
2470 if (UxxxStatus)
2471 return UxxxStatus;
2472 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2473 &pcidata);
2474 if (UxxxStatus)
2475 return UxxxStatus;
2476 reg = 12;
2477 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2478 &latence_timer);
2479 if (UxxxStatus)
2480 return UxxxStatus;
2481 latence_timer &= 0xFFFF00FF;
2482 latence_timer |= 0x00001600;
2483 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2484 latence_timer);
2485 if (UxxxStatus)
2486 return UxxxStatus;
2487 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2488 &pcidata);
2489 if (UxxxStatus)
2490 return UxxxStatus;
2491 reg = 4;
2492 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2493 0x00);
2494 if (UxxxStatus)
2495 return UxxxStatus;
2496 return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2497 }
2498
2499 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2500 {
2501 int result;
2502 int UxxxStatus;
2503 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2504 if (UxxxStatus)
2505 return UxxxStatus;
2506 result = ftdi_elan_check_controller(ftdi, quirk);
2507 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2508 if (UxxxStatus)
2509 return UxxxStatus;
2510 return result;
2511 }
2512
2513 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2514 {
2515 u32 controlreg;
2516 u8 sensebits;
2517 int UxxxStatus;
2518 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2519 if (UxxxStatus)
2520 return UxxxStatus;
2521 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2522 if (UxxxStatus)
2523 return UxxxStatus;
2524 msleep(750);
2525 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2526 if (UxxxStatus)
2527 return UxxxStatus;
2528 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2529 if (UxxxStatus)
2530 return UxxxStatus;
2531 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2532 if (UxxxStatus)
2533 return UxxxStatus;
2534 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2535 if (UxxxStatus)
2536 return UxxxStatus;
2537 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2538 if (UxxxStatus)
2539 return UxxxStatus;
2540 msleep(250);
2541 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2542 if (UxxxStatus)
2543 return UxxxStatus;
2544 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2545 if (UxxxStatus)
2546 return UxxxStatus;
2547 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2548 if (UxxxStatus)
2549 return UxxxStatus;
2550 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2551 if (UxxxStatus)
2552 return UxxxStatus;
2553 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2554 if (UxxxStatus)
2555 return UxxxStatus;
2556 msleep(1000);
2557 sensebits = (controlreg >> 16) & 0x000F;
2558 if (0x0D == sensebits)
2559 return 0;
2560 else
2561 return - ENXIO;
2562 }
2563
2564 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2565 {
2566 int UxxxStatus;
2567 u32 pcidata;
2568 int reg = 0;
2569 u8 fn;
2570 int activePCIfn = 0;
2571 int max_devices = 0;
2572 int controllers = 0;
2573 int unrecognized = 0;
2574 ftdi->function = 0;
2575 for (fn = 0; (fn < 4); fn++) {
2576 u32 pciVID = 0;
2577 u32 pciPID = 0;
2578 int devices = 0;
2579 activePCIfn = fn << 8;
2580 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2581 &pcidata);
2582 if (UxxxStatus)
2583 return UxxxStatus;
2584 pciVID = pcidata & 0xFFFF;
2585 pciPID = (pcidata >> 16) & 0xFFFF;
2586 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2587 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2588 controllers += 1;
2589 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2590 {
2591 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2592 controllers += 1;
2593 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2594 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2595 controllers += 1;
2596 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2597 {
2598 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2599 controllers += 1;
2600 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2601 devices = ftdi_elan_found_controller(ftdi, fn,
2602 OHCI_QUIRK_AMD756);
2603 controllers += 1;
2604 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2605 devices = ftdi_elan_found_controller(ftdi, fn,
2606 OHCI_QUIRK_ZFMICRO);
2607 controllers += 1;
2608 } else if (0 == pcidata) {
2609 } else
2610 unrecognized += 1;
2611 if (devices > max_devices) {
2612 max_devices = devices;
2613 ftdi->function = fn + 1;
2614 ftdi->platform_data.vendor = pciVID;
2615 ftdi->platform_data.device = pciPID;
2616 }
2617 }
2618 if (ftdi->function > 0) {
2619 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2620 } else if (controllers > 0) {
2621 return -ENXIO;
2622 } else if (unrecognized > 0) {
2623 return -ENXIO;
2624 } else {
2625 ftdi->enumerated = 0;
2626 return -ENXIO;
2627 }
2628 }
2629
2630
2631
2632
2633
2634 static int ftdi_elan_probe(struct usb_interface *interface,
2635 const struct usb_device_id *id)
2636 {
2637 struct usb_host_interface *iface_desc;
2638 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
2639 int retval;
2640 struct usb_ftdi *ftdi;
2641
2642 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2643 if (!ftdi)
2644 return -ENOMEM;
2645
2646 mutex_lock(&ftdi_module_lock);
2647 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2648 ftdi->sequence_num = ++ftdi_instances;
2649 mutex_unlock(&ftdi_module_lock);
2650 ftdi_elan_init_kref(ftdi);
2651 sema_init(&ftdi->sw_lock, 1);
2652 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2653 ftdi->interface = interface;
2654 mutex_init(&ftdi->u132_lock);
2655 ftdi->expected = 4;
2656
2657 iface_desc = interface->cur_altsetting;
2658 retval = usb_find_common_endpoints(iface_desc,
2659 &bulk_in, &bulk_out, NULL, NULL);
2660 if (retval) {
2661 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2662 goto error;
2663 }
2664
2665 ftdi->bulk_in_size = usb_endpoint_maxp(bulk_in);
2666 ftdi->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
2667 ftdi->bulk_in_buffer = kmalloc(ftdi->bulk_in_size, GFP_KERNEL);
2668 if (!ftdi->bulk_in_buffer) {
2669 retval = -ENOMEM;
2670 goto error;
2671 }
2672
2673 ftdi->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
2674
2675 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2676 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2677 ftdi->bulk_out_endpointAddr);
2678 usb_set_intfdata(interface, ftdi);
2679 if (iface_desc->desc.bInterfaceNumber == 0 &&
2680 ftdi->bulk_in_endpointAddr == 0x81 &&
2681 ftdi->bulk_out_endpointAddr == 0x02) {
2682 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2683 if (retval) {
2684 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2685 usb_set_intfdata(interface, NULL);
2686 retval = -ENOMEM;
2687 goto error;
2688 } else {
2689 ftdi->class = &ftdi_elan_jtag_class;
2690 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2691 ftdi, iface_desc->desc.bInterfaceNumber,
2692 interface->minor);
2693 return 0;
2694 }
2695 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2696 ftdi->bulk_in_endpointAddr == 0x83 &&
2697 ftdi->bulk_out_endpointAddr == 0x04) {
2698 ftdi->class = NULL;
2699 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2700 ftdi, iface_desc->desc.bInterfaceNumber);
2701 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2702 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2703 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2704 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2705 return 0;
2706 } else {
2707 dev_err(&ftdi->udev->dev,
2708 "Could not find ELAN's U132 device\n");
2709 retval = -ENODEV;
2710 goto error;
2711 }
2712 error:if (ftdi) {
2713 ftdi_elan_put_kref(ftdi);
2714 }
2715 return retval;
2716 }
2717
2718 static void ftdi_elan_disconnect(struct usb_interface *interface)
2719 {
2720 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2721 ftdi->disconnected += 1;
2722 if (ftdi->class) {
2723 int minor = interface->minor;
2724 struct usb_class_driver *class = ftdi->class;
2725 usb_set_intfdata(interface, NULL);
2726 usb_deregister_dev(interface, class);
2727 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2728 minor);
2729 } else {
2730 ftdi_status_cancel_work(ftdi);
2731 ftdi_command_cancel_work(ftdi);
2732 ftdi_response_cancel_work(ftdi);
2733 ftdi_elan_abandon_completions(ftdi);
2734 ftdi_elan_abandon_targets(ftdi);
2735 if (ftdi->registered) {
2736 platform_device_unregister(&ftdi->platform_dev);
2737 ftdi->synchronized = 0;
2738 ftdi->enumerated = 0;
2739 ftdi->initialized = 0;
2740 ftdi->registered = 0;
2741 }
2742 ftdi->disconnected += 1;
2743 usb_set_intfdata(interface, NULL);
2744 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2745 }
2746 ftdi_elan_put_kref(ftdi);
2747 }
2748
2749 static struct usb_driver ftdi_elan_driver = {
2750 .name = "ftdi-elan",
2751 .probe = ftdi_elan_probe,
2752 .disconnect = ftdi_elan_disconnect,
2753 .id_table = ftdi_elan_table,
2754 };
2755 static int __init ftdi_elan_init(void)
2756 {
2757 int result;
2758 pr_info("driver %s\n", ftdi_elan_driver.name);
2759 mutex_init(&ftdi_module_lock);
2760 INIT_LIST_HEAD(&ftdi_static_list);
2761 result = usb_register(&ftdi_elan_driver);
2762 if (result) {
2763 pr_err("usb_register failed. Error number %d\n", result);
2764 }
2765 return result;
2766
2767 }
2768
2769 static void __exit ftdi_elan_exit(void)
2770 {
2771 struct usb_ftdi *ftdi;
2772 struct usb_ftdi *temp;
2773 usb_deregister(&ftdi_elan_driver);
2774 pr_info("ftdi_u132 driver deregistered\n");
2775 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2776 ftdi_status_cancel_work(ftdi);
2777 ftdi_command_cancel_work(ftdi);
2778 ftdi_response_cancel_work(ftdi);
2779 }
2780 }
2781
2782
2783 module_init(ftdi_elan_init);
2784 module_exit(ftdi_elan_exit);