1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
4 *
5 * Copyright(C) 2006 Elan Digital Systems Limited
6 * http://www.elandigitalsystems.com
7 *
8 * Author and Maintainer - Tony Olech - Elan Digital Systems
9 * tony.olech@elandigitalsystems.com
10 *
11 * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
12 * based on various USB client drivers in the 2.6.15 linux kernel
13 * with constant reference to the 3rd Edition of Linux Device Drivers
14 * published by O'Reilly
15 *
16 * The U132 adapter is a USB to CardBus adapter specifically designed
17 * for PC cards that contain an OHCI host controller. Typical PC cards
18 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
19 *
20 * The U132 adapter will *NOT *work with PC cards that do not contain
21 * an OHCI controller. A simple way to test whether a PC card has an
22 * OHCI controller as an interface is to insert the PC card directly
23 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
24 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
25 * then there is a good chance that the U132 adapter will support the
26 * PC card.(you also need the specific client driver for the PC card)
27 *
28 * Please inform the Author and Maintainer about any PC cards that
29 * contain OHCI Host Controller and work when directly connected to
30 * an embedded CardBus slot but do not work when they are connected
31 * via an ELAN U132 adapter.
32 *
33 */
34
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <linux/ioctl.h>
42 #include <linux/pci_ids.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/kref.h>
46 #include <linux/mutex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/workqueue.h>
50 #include <linux/platform_device.h>
51 MODULE_AUTHOR("Tony Olech");
52 MODULE_DESCRIPTION("FTDI ELAN driver");
53 MODULE_LICENSE("GPL");
54 #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
55 static bool distrust_firmware = 1;
56 module_param(distrust_firmware, bool, 0);
57 MODULE_PARM_DESC(distrust_firmware,
58 "true to distrust firmware power/overcurrent setup");
59 extern struct platform_driver u132_platform_driver;
60 /*
61 * ftdi_module_lock exists to protect access to global variables
62 *
63 */
64 static struct mutex ftdi_module_lock;
65 static int ftdi_instances = 0;
66 static struct list_head ftdi_static_list;
67 /*
68 * end of the global variables protected by ftdi_module_lock
69 */
70 #include "usb_u132.h"
71 #include <asm/io.h>
72 #include <linux/usb/hcd.h>
73
74 /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
75 * If you're going to try stuff like this, you need to split
76 * out shareable stuff (register declarations?) into its own
77 * file, maybe name <linux/usb/ohci.h>
78 */
79
80 #include "../host/ohci.h"
81 /* Define these values to match your devices*/
82 #define USB_FTDI_ELAN_VENDOR_ID 0x0403
83 #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
84 /* table of devices that work with this driver*/
85 static const struct usb_device_id ftdi_elan_table[] = {
86 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
87 { /* Terminating entry */ }
88 };
89
90 MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
91 /* only the jtag(firmware upgrade device) interface requires
92 * a device file and corresponding minor number, but the
93 * interface is created unconditionally - I suppose it could
94 * be configured or not according to a module parameter.
95 * But since we(now) require one interface per device,
96 * and since it unlikely that a normal installation would
97 * require more than a couple of elan-ftdi devices, 8 seems
98 * like a reasonable limit to have here, and if someone
99 * really requires more than 8 devices, then they can frig the
100 * code and recompile
101 */
102 #define USB_FTDI_ELAN_MINOR_BASE 192
103 #define COMMAND_BITS 5
104 #define COMMAND_SIZE (1<<COMMAND_BITS)
105 #define COMMAND_MASK (COMMAND_SIZE-1)
106 struct u132_command {
107 u8 header;
108 u16 length;
109 u8 address;
110 u8 width;
111 u32 value;
112 int follows;
113 void *buffer;
114 };
115 #define RESPOND_BITS 5
116 #define RESPOND_SIZE (1<<RESPOND_BITS)
117 #define RESPOND_MASK (RESPOND_SIZE-1)
118 struct u132_respond {
119 u8 header;
120 u8 address;
121 u32 *value;
122 int *result;
123 struct completion wait_completion;
124 };
125 struct u132_target {
126 void *endp;
127 struct urb *urb;
128 int toggle_bits;
129 int error_count;
130 int condition_code;
131 int repeat_number;
132 int halted;
133 int skipped;
134 int actual;
135 int non_null;
136 int active;
137 int abandoning;
138 void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
139 int toggle_bits, int error_count, int condition_code,
140 int repeat_number, int halted, int skipped, int actual,
141 int non_null);
142 };
143 /* Structure to hold all of our device specific stuff*/
144 struct usb_ftdi {
145 struct list_head ftdi_list;
146 struct mutex u132_lock;
147 int command_next;
148 int command_head;
149 struct u132_command command[COMMAND_SIZE];
150 int respond_next;
151 int respond_head;
152 struct u132_respond respond[RESPOND_SIZE];
153 struct u132_target target[4];
154 char device_name[16];
155 unsigned synchronized:1;
156 unsigned enumerated:1;
157 unsigned registered:1;
158 unsigned initialized:1;
159 unsigned card_ejected:1;
160 int function;
161 int sequence_num;
162 int disconnected;
163 int gone_away;
164 int stuck_status;
165 int status_queue_delay;
166 struct semaphore sw_lock;
167 struct usb_device *udev;
168 struct usb_interface *interface;
169 struct usb_class_driver *class;
170 struct delayed_work status_work;
171 struct delayed_work command_work;
172 struct delayed_work respond_work;
173 struct u132_platform_data platform_data;
174 struct resource resources[0];
175 struct platform_device platform_dev;
176 unsigned char *bulk_in_buffer;
177 size_t bulk_in_size;
178 size_t bulk_in_last;
179 size_t bulk_in_left;
180 __u8 bulk_in_endpointAddr;
181 __u8 bulk_out_endpointAddr;
182 struct kref kref;
183 u32 controlreg;
184 u8 response[4 + 1024];
185 int expected;
186 int received;
187 int ed_found;
188 };
189 #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
190 #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
191 platform_dev)
192 static struct usb_driver ftdi_elan_driver;
ftdi_elan_delete(struct kref * kref)193 static void ftdi_elan_delete(struct kref *kref)
194 {
195 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
196 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
197 usb_put_dev(ftdi->udev);
198 ftdi->disconnected += 1;
199 mutex_lock(&ftdi_module_lock);
200 list_del_init(&ftdi->ftdi_list);
201 ftdi_instances -= 1;
202 mutex_unlock(&ftdi_module_lock);
203 kfree(ftdi->bulk_in_buffer);
204 ftdi->bulk_in_buffer = NULL;
205 kfree(ftdi);
206 }
207
ftdi_elan_put_kref(struct usb_ftdi * ftdi)208 static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
209 {
210 kref_put(&ftdi->kref, ftdi_elan_delete);
211 }
212
ftdi_elan_get_kref(struct usb_ftdi * ftdi)213 static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
214 {
215 kref_get(&ftdi->kref);
216 }
217
ftdi_elan_init_kref(struct usb_ftdi * ftdi)218 static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
219 {
220 kref_init(&ftdi->kref);
221 }
222
ftdi_status_requeue_work(struct usb_ftdi * ftdi,unsigned int delta)223 static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
224 {
225 if (!schedule_delayed_work(&ftdi->status_work, delta))
226 kref_put(&ftdi->kref, ftdi_elan_delete);
227 }
228
ftdi_status_queue_work(struct usb_ftdi * ftdi,unsigned int delta)229 static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
230 {
231 if (schedule_delayed_work(&ftdi->status_work, delta))
232 kref_get(&ftdi->kref);
233 }
234
ftdi_status_cancel_work(struct usb_ftdi * ftdi)235 static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
236 {
237 if (cancel_delayed_work_sync(&ftdi->status_work))
238 kref_put(&ftdi->kref, ftdi_elan_delete);
239 }
240
ftdi_command_requeue_work(struct usb_ftdi * ftdi,unsigned int delta)241 static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
242 {
243 if (!schedule_delayed_work(&ftdi->command_work, delta))
244 kref_put(&ftdi->kref, ftdi_elan_delete);
245 }
246
ftdi_command_queue_work(struct usb_ftdi * ftdi,unsigned int delta)247 static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
248 {
249 if (schedule_delayed_work(&ftdi->command_work, delta))
250 kref_get(&ftdi->kref);
251 }
252
ftdi_command_cancel_work(struct usb_ftdi * ftdi)253 static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
254 {
255 if (cancel_delayed_work_sync(&ftdi->command_work))
256 kref_put(&ftdi->kref, ftdi_elan_delete);
257 }
258
ftdi_response_requeue_work(struct usb_ftdi * ftdi,unsigned int delta)259 static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
260 unsigned int delta)
261 {
262 if (!schedule_delayed_work(&ftdi->respond_work, delta))
263 kref_put(&ftdi->kref, ftdi_elan_delete);
264 }
265
ftdi_respond_queue_work(struct usb_ftdi * ftdi,unsigned int delta)266 static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
267 {
268 if (schedule_delayed_work(&ftdi->respond_work, delta))
269 kref_get(&ftdi->kref);
270 }
271
ftdi_response_cancel_work(struct usb_ftdi * ftdi)272 static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
273 {
274 if (cancel_delayed_work_sync(&ftdi->respond_work))
275 kref_put(&ftdi->kref, ftdi_elan_delete);
276 }
277
ftdi_elan_gone_away(struct platform_device * pdev)278 void ftdi_elan_gone_away(struct platform_device *pdev)
279 {
280 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
281 ftdi->gone_away += 1;
282 ftdi_elan_put_kref(ftdi);
283 }
284
285
286 EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
ftdi_release_platform_dev(struct device * dev)287 static void ftdi_release_platform_dev(struct device *dev)
288 {
289 dev->parent = NULL;
290 }
291
292 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
293 struct u132_target *target, u8 *buffer, int length);
294 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
295 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
296 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
297 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
298 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
299 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
300 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
301 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
302 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
ftdi_elan_hcd_init(struct usb_ftdi * ftdi)303 static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
304 {
305 if (ftdi->platform_dev.dev.parent)
306 return -EBUSY;
307
308 ftdi_elan_get_kref(ftdi);
309 ftdi->platform_data.potpg = 100;
310 ftdi->platform_data.reset = NULL;
311 ftdi->platform_dev.id = ftdi->sequence_num;
312 ftdi->platform_dev.resource = ftdi->resources;
313 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
314 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
315 ftdi->platform_dev.dev.parent = NULL;
316 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
317 ftdi->platform_dev.dev.dma_mask = NULL;
318 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
319 ftdi->platform_dev.name = ftdi->device_name;
320 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
321 request_module("u132_hcd");
322 dev_info(&ftdi->udev->dev, "registering '%s'\n",
323 ftdi->platform_dev.name);
324
325 return platform_device_register(&ftdi->platform_dev);
326 }
327
ftdi_elan_abandon_completions(struct usb_ftdi * ftdi)328 static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
329 {
330 mutex_lock(&ftdi->u132_lock);
331 while (ftdi->respond_next > ftdi->respond_head) {
332 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
333 ftdi->respond_head++];
334 *respond->result = -ESHUTDOWN;
335 *respond->value = 0;
336 complete(&respond->wait_completion);
337 }
338 mutex_unlock(&ftdi->u132_lock);
339 }
340
ftdi_elan_abandon_targets(struct usb_ftdi * ftdi)341 static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
342 {
343 int ed_number = 4;
344 mutex_lock(&ftdi->u132_lock);
345 while (ed_number-- > 0) {
346 struct u132_target *target = &ftdi->target[ed_number];
347 if (target->active == 1) {
348 target->condition_code = TD_DEVNOTRESP;
349 mutex_unlock(&ftdi->u132_lock);
350 ftdi_elan_do_callback(ftdi, target, NULL, 0);
351 mutex_lock(&ftdi->u132_lock);
352 }
353 }
354 ftdi->received = 0;
355 ftdi->expected = 4;
356 ftdi->ed_found = 0;
357 mutex_unlock(&ftdi->u132_lock);
358 }
359
ftdi_elan_flush_targets(struct usb_ftdi * ftdi)360 static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
361 {
362 int ed_number = 4;
363 mutex_lock(&ftdi->u132_lock);
364 while (ed_number-- > 0) {
365 struct u132_target *target = &ftdi->target[ed_number];
366 target->abandoning = 1;
367 wait_1:if (target->active == 1) {
368 int command_size = ftdi->command_next -
369 ftdi->command_head;
370 if (command_size < COMMAND_SIZE) {
371 struct u132_command *command = &ftdi->command[
372 COMMAND_MASK & ftdi->command_next];
373 command->header = 0x80 | (ed_number << 5) | 0x4;
374 command->length = 0x00;
375 command->address = 0x00;
376 command->width = 0x00;
377 command->follows = 0;
378 command->value = 0;
379 command->buffer = &command->value;
380 ftdi->command_next += 1;
381 ftdi_elan_kick_command_queue(ftdi);
382 } else {
383 mutex_unlock(&ftdi->u132_lock);
384 msleep(100);
385 mutex_lock(&ftdi->u132_lock);
386 goto wait_1;
387 }
388 }
389 wait_2:if (target->active == 1) {
390 int command_size = ftdi->command_next -
391 ftdi->command_head;
392 if (command_size < COMMAND_SIZE) {
393 struct u132_command *command = &ftdi->command[
394 COMMAND_MASK & ftdi->command_next];
395 command->header = 0x90 | (ed_number << 5);
396 command->length = 0x00;
397 command->address = 0x00;
398 command->width = 0x00;
399 command->follows = 0;
400 command->value = 0;
401 command->buffer = &command->value;
402 ftdi->command_next += 1;
403 ftdi_elan_kick_command_queue(ftdi);
404 } else {
405 mutex_unlock(&ftdi->u132_lock);
406 msleep(100);
407 mutex_lock(&ftdi->u132_lock);
408 goto wait_2;
409 }
410 }
411 }
412 ftdi->received = 0;
413 ftdi->expected = 4;
414 ftdi->ed_found = 0;
415 mutex_unlock(&ftdi->u132_lock);
416 }
417
ftdi_elan_cancel_targets(struct usb_ftdi * ftdi)418 static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
419 {
420 int ed_number = 4;
421 mutex_lock(&ftdi->u132_lock);
422 while (ed_number-- > 0) {
423 struct u132_target *target = &ftdi->target[ed_number];
424 target->abandoning = 1;
425 wait:if (target->active == 1) {
426 int command_size = ftdi->command_next -
427 ftdi->command_head;
428 if (command_size < COMMAND_SIZE) {
429 struct u132_command *command = &ftdi->command[
430 COMMAND_MASK & ftdi->command_next];
431 command->header = 0x80 | (ed_number << 5) | 0x4;
432 command->length = 0x00;
433 command->address = 0x00;
434 command->width = 0x00;
435 command->follows = 0;
436 command->value = 0;
437 command->buffer = &command->value;
438 ftdi->command_next += 1;
439 ftdi_elan_kick_command_queue(ftdi);
440 } else {
441 mutex_unlock(&ftdi->u132_lock);
442 msleep(100);
443 mutex_lock(&ftdi->u132_lock);
444 goto wait;
445 }
446 }
447 }
448 ftdi->received = 0;
449 ftdi->expected = 4;
450 ftdi->ed_found = 0;
451 mutex_unlock(&ftdi->u132_lock);
452 }
453
ftdi_elan_kick_command_queue(struct usb_ftdi * ftdi)454 static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
455 {
456 ftdi_command_queue_work(ftdi, 0);
457 }
458
ftdi_elan_command_work(struct work_struct * work)459 static void ftdi_elan_command_work(struct work_struct *work)
460 {
461 struct usb_ftdi *ftdi =
462 container_of(work, struct usb_ftdi, command_work.work);
463
464 if (ftdi->disconnected > 0) {
465 ftdi_elan_put_kref(ftdi);
466 return;
467 } else {
468 int retval = ftdi_elan_command_engine(ftdi);
469 if (retval == -ESHUTDOWN) {
470 ftdi->disconnected += 1;
471 } else if (retval == -ENODEV) {
472 ftdi->disconnected += 1;
473 } else if (retval)
474 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
475 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
476 return;
477 }
478 }
479
ftdi_elan_kick_respond_queue(struct usb_ftdi * ftdi)480 static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
481 {
482 ftdi_respond_queue_work(ftdi, 0);
483 }
484
ftdi_elan_respond_work(struct work_struct * work)485 static void ftdi_elan_respond_work(struct work_struct *work)
486 {
487 struct usb_ftdi *ftdi =
488 container_of(work, struct usb_ftdi, respond_work.work);
489 if (ftdi->disconnected > 0) {
490 ftdi_elan_put_kref(ftdi);
491 return;
492 } else {
493 int retval = ftdi_elan_respond_engine(ftdi);
494 if (retval == 0) {
495 } else if (retval == -ESHUTDOWN) {
496 ftdi->disconnected += 1;
497 } else if (retval == -ENODEV) {
498 ftdi->disconnected += 1;
499 } else if (retval == -EILSEQ) {
500 ftdi->disconnected += 1;
501 } else {
502 ftdi->disconnected += 1;
503 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
504 }
505 if (ftdi->disconnected > 0) {
506 ftdi_elan_abandon_completions(ftdi);
507 ftdi_elan_abandon_targets(ftdi);
508 }
509 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
510 return;
511 }
512 }
513
514
515 /*
516 * the sw_lock is initially held and will be freed
517 * after the FTDI has been synchronized
518 *
519 */
ftdi_elan_status_work(struct work_struct * work)520 static void ftdi_elan_status_work(struct work_struct *work)
521 {
522 struct usb_ftdi *ftdi =
523 container_of(work, struct usb_ftdi, status_work.work);
524 int work_delay_in_msec = 0;
525 if (ftdi->disconnected > 0) {
526 ftdi_elan_put_kref(ftdi);
527 return;
528 } else if (ftdi->synchronized == 0) {
529 down(&ftdi->sw_lock);
530 if (ftdi_elan_synchronize(ftdi) == 0) {
531 ftdi->synchronized = 1;
532 ftdi_command_queue_work(ftdi, 1);
533 ftdi_respond_queue_work(ftdi, 1);
534 up(&ftdi->sw_lock);
535 work_delay_in_msec = 100;
536 } else {
537 dev_err(&ftdi->udev->dev, "synchronize failed\n");
538 up(&ftdi->sw_lock);
539 work_delay_in_msec = 10 *1000;
540 }
541 } else if (ftdi->stuck_status > 0) {
542 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
543 ftdi->stuck_status = 0;
544 ftdi->synchronized = 0;
545 } else if ((ftdi->stuck_status++ % 60) == 1) {
546 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
547 } else
548 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
549 ftdi->stuck_status);
550 work_delay_in_msec = 100;
551 } else if (ftdi->enumerated == 0) {
552 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
553 ftdi->enumerated = 1;
554 work_delay_in_msec = 250;
555 } else
556 work_delay_in_msec = 1000;
557 } else if (ftdi->initialized == 0) {
558 if (ftdi_elan_setupOHCI(ftdi) == 0) {
559 ftdi->initialized = 1;
560 work_delay_in_msec = 500;
561 } else {
562 dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
563 work_delay_in_msec = 1 *1000;
564 }
565 } else if (ftdi->registered == 0) {
566 work_delay_in_msec = 10;
567 if (ftdi_elan_hcd_init(ftdi) == 0) {
568 ftdi->registered = 1;
569 } else
570 dev_err(&ftdi->udev->dev, "register failed\n");
571 work_delay_in_msec = 250;
572 } else {
573 if (ftdi_elan_checkingPCI(ftdi) == 0) {
574 work_delay_in_msec = 250;
575 } else if (ftdi->controlreg & 0x00400000) {
576 if (ftdi->gone_away > 0) {
577 dev_err(&ftdi->udev->dev, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
578 ftdi->platform_dev.dev.parent,
579 &ftdi->platform_dev.dev);
580 platform_device_unregister(&ftdi->platform_dev);
581 ftdi->platform_dev.dev.parent = NULL;
582 ftdi->registered = 0;
583 ftdi->enumerated = 0;
584 ftdi->card_ejected = 0;
585 ftdi->initialized = 0;
586 ftdi->gone_away = 0;
587 } else
588 ftdi_elan_flush_targets(ftdi);
589 work_delay_in_msec = 250;
590 } else {
591 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n");
592 ftdi_elan_cancel_targets(ftdi);
593 work_delay_in_msec = 500;
594 ftdi->enumerated = 0;
595 ftdi->initialized = 0;
596 }
597 }
598 if (ftdi->disconnected > 0) {
599 ftdi_elan_put_kref(ftdi);
600 return;
601 } else {
602 ftdi_status_requeue_work(ftdi,
603 msecs_to_jiffies(work_delay_in_msec));
604 return;
605 }
606 }
607
608
609 /*
610 * file_operations for the jtag interface
611 *
612 * the usage count for the device is incremented on open()
613 * and decremented on release()
614 */
ftdi_elan_open(struct inode * inode,struct file * file)615 static int ftdi_elan_open(struct inode *inode, struct file *file)
616 {
617 int subminor;
618 struct usb_interface *interface;
619
620 subminor = iminor(inode);
621 interface = usb_find_interface(&ftdi_elan_driver, subminor);
622
623 if (!interface) {
624 pr_err("can't find device for minor %d\n", subminor);
625 return -ENODEV;
626 } else {
627 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
628 if (!ftdi) {
629 return -ENODEV;
630 } else {
631 if (down_interruptible(&ftdi->sw_lock)) {
632 return -EINTR;
633 } else {
634 ftdi_elan_get_kref(ftdi);
635 file->private_data = ftdi;
636 return 0;
637 }
638 }
639 }
640 }
641
ftdi_elan_release(struct inode * inode,struct file * file)642 static int ftdi_elan_release(struct inode *inode, struct file *file)
643 {
644 struct usb_ftdi *ftdi = file->private_data;
645 if (ftdi == NULL)
646 return -ENODEV;
647 up(&ftdi->sw_lock); /* decrement the count on our device */
648 ftdi_elan_put_kref(ftdi);
649 return 0;
650 }
651
652
653 /*
654 *
655 * blocking bulk reads are used to get data from the device
656 *
657 */
ftdi_elan_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)658 static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
659 size_t count, loff_t *ppos)
660 {
661 char data[30 *3 + 4];
662 char *d = data;
663 int m = (sizeof(data) - 1) / 3 - 1;
664 int bytes_read = 0;
665 int retry_on_empty = 10;
666 int retry_on_timeout = 5;
667 struct usb_ftdi *ftdi = file->private_data;
668 if (ftdi->disconnected > 0) {
669 return -ENODEV;
670 }
671 data[0] = 0;
672 have:if (ftdi->bulk_in_left > 0) {
673 if (count-- > 0) {
674 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
675 ftdi->bulk_in_left -= 1;
676 if (bytes_read < m) {
677 d += sprintf(d, " %02X", 0x000000FF & *p);
678 } else if (bytes_read > m) {
679 } else
680 d += sprintf(d, " ..");
681 if (copy_to_user(buffer++, p, 1)) {
682 return -EFAULT;
683 } else {
684 bytes_read += 1;
685 goto have;
686 }
687 } else
688 return bytes_read;
689 }
690 more:if (count > 0) {
691 int packet_bytes = 0;
692 int retval = usb_bulk_msg(ftdi->udev,
693 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
694 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
695 &packet_bytes, 50);
696 if (packet_bytes > 2) {
697 ftdi->bulk_in_left = packet_bytes - 2;
698 ftdi->bulk_in_last = 1;
699 goto have;
700 } else if (retval == -ETIMEDOUT) {
701 if (retry_on_timeout-- > 0) {
702 goto more;
703 } else if (bytes_read > 0) {
704 return bytes_read;
705 } else
706 return retval;
707 } else if (retval == 0) {
708 if (retry_on_empty-- > 0) {
709 goto more;
710 } else
711 return bytes_read;
712 } else
713 return retval;
714 } else
715 return bytes_read;
716 }
717
ftdi_elan_write_bulk_callback(struct urb * urb)718 static void ftdi_elan_write_bulk_callback(struct urb *urb)
719 {
720 struct usb_ftdi *ftdi = urb->context;
721 int status = urb->status;
722
723 if (status && !(status == -ENOENT || status == -ECONNRESET ||
724 status == -ESHUTDOWN)) {
725 dev_err(&ftdi->udev->dev,
726 "urb=%p write bulk status received: %d\n", urb, status);
727 }
728 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
729 urb->transfer_buffer, urb->transfer_dma);
730 }
731
fill_buffer_with_all_queued_commands(struct usb_ftdi * ftdi,char * buf,int command_size,int total_size)732 static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
733 char *buf, int command_size, int total_size)
734 {
735 int ed_commands = 0;
736 int b = 0;
737 int I = command_size;
738 int i = ftdi->command_head;
739 while (I-- > 0) {
740 struct u132_command *command = &ftdi->command[COMMAND_MASK &
741 i++];
742 int F = command->follows;
743 u8 *f = command->buffer;
744 if (command->header & 0x80) {
745 ed_commands |= 1 << (0x3 & (command->header >> 5));
746 }
747 buf[b++] = command->header;
748 buf[b++] = (command->length >> 0) & 0x00FF;
749 buf[b++] = (command->length >> 8) & 0x00FF;
750 buf[b++] = command->address;
751 buf[b++] = command->width;
752 while (F-- > 0) {
753 buf[b++] = *f++;
754 }
755 }
756 return ed_commands;
757 }
758
ftdi_elan_total_command_size(struct usb_ftdi * ftdi,int command_size)759 static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
760 {
761 int total_size = 0;
762 int I = command_size;
763 int i = ftdi->command_head;
764 while (I-- > 0) {
765 struct u132_command *command = &ftdi->command[COMMAND_MASK &
766 i++];
767 total_size += 5 + command->follows;
768 }
769 return total_size;
770 }
771
ftdi_elan_command_engine(struct usb_ftdi * ftdi)772 static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
773 {
774 int retval;
775 char *buf;
776 int ed_commands;
777 int total_size;
778 struct urb *urb;
779 int command_size = ftdi->command_next - ftdi->command_head;
780 if (command_size == 0)
781 return 0;
782 total_size = ftdi_elan_total_command_size(ftdi, command_size);
783 urb = usb_alloc_urb(0, GFP_KERNEL);
784 if (!urb)
785 return -ENOMEM;
786 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
787 &urb->transfer_dma);
788 if (!buf) {
789 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
790 command_size, total_size);
791 usb_free_urb(urb);
792 return -ENOMEM;
793 }
794 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
795 command_size, total_size);
796 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
797 ftdi->bulk_out_endpointAddr), buf, total_size,
798 ftdi_elan_write_bulk_callback, ftdi);
799 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
800 if (ed_commands) {
801 char diag[40 *3 + 4];
802 char *d = diag;
803 int m = total_size;
804 u8 *c = buf;
805 int s = (sizeof(diag) - 1) / 3;
806 diag[0] = 0;
807 while (s-- > 0 && m-- > 0) {
808 if (s > 0 || m == 0) {
809 d += sprintf(d, " %02X", *c++);
810 } else
811 d += sprintf(d, " ..");
812 }
813 }
814 retval = usb_submit_urb(urb, GFP_KERNEL);
815 if (retval) {
816 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
817 retval, urb, command_size, total_size);
818 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
819 usb_free_urb(urb);
820 return retval;
821 }
822 usb_free_urb(urb); /* release our reference to this urb,
823 the USB core will eventually free it entirely */
824 ftdi->command_head += command_size;
825 ftdi_elan_kick_respond_queue(ftdi);
826 return 0;
827 }
828
ftdi_elan_do_callback(struct usb_ftdi * ftdi,struct u132_target * target,u8 * buffer,int length)829 static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
830 struct u132_target *target, u8 *buffer, int length)
831 {
832 struct urb *urb = target->urb;
833 int halted = target->halted;
834 int skipped = target->skipped;
835 int actual = target->actual;
836 int non_null = target->non_null;
837 int toggle_bits = target->toggle_bits;
838 int error_count = target->error_count;
839 int condition_code = target->condition_code;
840 int repeat_number = target->repeat_number;
841 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
842 int, int, int, int) = target->callback;
843 target->active -= 1;
844 target->callback = NULL;
845 (*callback) (target->endp, urb, buffer, length, toggle_bits,
846 error_count, condition_code, repeat_number, halted, skipped,
847 actual, non_null);
848 }
849
have_ed_set_response(struct usb_ftdi * ftdi,struct u132_target * target,u16 ed_length,int ed_number,int ed_type,char * b)850 static char *have_ed_set_response(struct usb_ftdi *ftdi,
851 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
852 char *b)
853 {
854 int payload = (ed_length >> 0) & 0x07FF;
855 mutex_lock(&ftdi->u132_lock);
856 target->actual = 0;
857 target->non_null = (ed_length >> 15) & 0x0001;
858 target->repeat_number = (ed_length >> 11) & 0x000F;
859 if (ed_type == 0x02 || ed_type == 0x03) {
860 if (payload == 0 || target->abandoning > 0) {
861 target->abandoning = 0;
862 mutex_unlock(&ftdi->u132_lock);
863 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
864 payload);
865 ftdi->received = 0;
866 ftdi->expected = 4;
867 ftdi->ed_found = 0;
868 return ftdi->response;
869 } else {
870 ftdi->expected = 4 + payload;
871 ftdi->ed_found = 1;
872 mutex_unlock(&ftdi->u132_lock);
873 return b;
874 }
875 } else {
876 target->abandoning = 0;
877 mutex_unlock(&ftdi->u132_lock);
878 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
879 payload);
880 ftdi->received = 0;
881 ftdi->expected = 4;
882 ftdi->ed_found = 0;
883 return ftdi->response;
884 }
885 }
886
have_ed_get_response(struct usb_ftdi * ftdi,struct u132_target * target,u16 ed_length,int ed_number,int ed_type,char * b)887 static char *have_ed_get_response(struct usb_ftdi *ftdi,
888 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
889 char *b)
890 {
891 mutex_lock(&ftdi->u132_lock);
892 target->condition_code = TD_DEVNOTRESP;
893 target->actual = (ed_length >> 0) & 0x01FF;
894 target->non_null = (ed_length >> 15) & 0x0001;
895 target->repeat_number = (ed_length >> 11) & 0x000F;
896 mutex_unlock(&ftdi->u132_lock);
897 if (target->active)
898 ftdi_elan_do_callback(ftdi, target, NULL, 0);
899 target->abandoning = 0;
900 ftdi->received = 0;
901 ftdi->expected = 4;
902 ftdi->ed_found = 0;
903 return ftdi->response;
904 }
905
906
907 /*
908 * The engine tries to empty the FTDI fifo
909 *
910 * all responses found in the fifo data are dispatched thus
911 * the response buffer can only ever hold a maximum sized
912 * response from the Uxxx.
913 *
914 */
ftdi_elan_respond_engine(struct usb_ftdi * ftdi)915 static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
916 {
917 u8 *b = ftdi->response + ftdi->received;
918 int bytes_read = 0;
919 int retry_on_empty = 1;
920 int retry_on_timeout = 3;
921 read:{
922 int packet_bytes = 0;
923 int retval = usb_bulk_msg(ftdi->udev,
924 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
925 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
926 &packet_bytes, 500);
927 char diag[30 *3 + 4];
928 char *d = diag;
929 int m = packet_bytes;
930 u8 *c = ftdi->bulk_in_buffer;
931 int s = (sizeof(diag) - 1) / 3;
932 diag[0] = 0;
933 while (s-- > 0 && m-- > 0) {
934 if (s > 0 || m == 0) {
935 d += sprintf(d, " %02X", *c++);
936 } else
937 d += sprintf(d, " ..");
938 }
939 if (packet_bytes > 2) {
940 ftdi->bulk_in_left = packet_bytes - 2;
941 ftdi->bulk_in_last = 1;
942 goto have;
943 } else if (retval == -ETIMEDOUT) {
944 if (retry_on_timeout-- > 0) {
945 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
946 packet_bytes, bytes_read, diag);
947 goto more;
948 } else if (bytes_read > 0) {
949 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
950 bytes_read, diag);
951 return -ENOMEM;
952 } else {
953 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
954 packet_bytes, bytes_read, diag);
955 return -ENOMEM;
956 }
957 } else if (retval == -EILSEQ) {
958 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
959 retval, packet_bytes, bytes_read, diag);
960 return retval;
961 } else if (retval) {
962 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
963 retval, packet_bytes, bytes_read, diag);
964 return retval;
965 } else {
966 if (retry_on_empty-- > 0) {
967 goto more;
968 } else
969 return 0;
970 }
971 }
972 more:{
973 goto read;
974 }
975 have:if (ftdi->bulk_in_left > 0) {
976 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
977 bytes_read += 1;
978 ftdi->bulk_in_left -= 1;
979 if (ftdi->received == 0 && c == 0xFF) {
980 goto have;
981 } else
982 *b++ = c;
983 if (++ftdi->received < ftdi->expected) {
984 goto have;
985 } else if (ftdi->ed_found) {
986 int ed_number = (ftdi->response[0] >> 5) & 0x03;
987 u16 ed_length = (ftdi->response[2] << 8) |
988 ftdi->response[1];
989 struct u132_target *target = &ftdi->target[ed_number];
990 int payload = (ed_length >> 0) & 0x07FF;
991 char diag[30 *3 + 4];
992 char *d = diag;
993 int m = payload;
994 u8 *c = 4 + ftdi->response;
995 int s = (sizeof(diag) - 1) / 3;
996 diag[0] = 0;
997 while (s-- > 0 && m-- > 0) {
998 if (s > 0 || m == 0) {
999 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 * create a urb, and a buffer for it, and copy the data to the urb
1079 *
1080 */
ftdi_elan_write(struct file * file,const char __user * user_buffer,size_t count,loff_t * ppos)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 * usb class driver info in order to get a minor number from the usb core,
1144 * and to have the device registered with the driver core
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 * the following definitions are for the
1154 * ELAN FPGA state machgine processor that
1155 * lies on the other side of the FTDI chip
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
ftdi_elan_write_reg(struct usb_ftdi * ftdi,u32 data)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
ftdi_elan_write_config(struct usb_ftdi * ftdi,int config_offset,u8 width,u32 data)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
ftdi_elan_write_pcimem(struct usb_ftdi * ftdi,int mem_offset,u8 width,u32 data)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
usb_ftdi_elan_write_pcimem(struct platform_device * pdev,int mem_offset,u8 width,u32 data)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);
ftdi_elan_read_reg(struct usb_ftdi * ftdi,u32 * data)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
ftdi_elan_read_config(struct usb_ftdi * ftdi,int config_offset,u8 width,u32 * data)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
ftdi_elan_read_pcimem(struct usb_ftdi * ftdi,int mem_offset,u8 width,u32 * data)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
usb_ftdi_elan_read_pcimem(struct platform_device * pdev,int mem_offset,u8 width,u32 * data)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);
ftdi_elan_edset_setup(struct usb_ftdi * ftdi,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,u8 toggle_bits,void (* callback)(void * endp,struct urb * urb,u8 * buf,int len,int toggle_bits,int error_count,int condition_code,int repeat_number,int halted,int skipped,int actual,int non_null))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 usb_pipeout(urb->pipe));
1454 command->follows = 8;
1455 command->value = 0;
1456 command->buffer = urb->setup_packet;
1457 target->callback = callback;
1458 target->endp = endp;
1459 target->urb = urb;
1460 target->active = 1;
1461 ftdi->command_next += 1;
1462 ftdi_elan_kick_command_queue(ftdi);
1463 mutex_unlock(&ftdi->u132_lock);
1464 return 0;
1465 } else {
1466 mutex_unlock(&ftdi->u132_lock);
1467 msleep(100);
1468 goto wait;
1469 }
1470 }
1471 }
1472
usb_ftdi_elan_edset_setup(struct platform_device * pdev,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,u8 toggle_bits,void (* callback)(void * endp,struct urb * urb,u8 * buf,int len,int toggle_bits,int error_count,int condition_code,int repeat_number,int halted,int skipped,int actual,int non_null))1473 int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
1474 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1475 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1476 int toggle_bits, int error_count, int condition_code, int repeat_number,
1477 int halted, int skipped, int actual, int non_null))
1478 {
1479 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1480 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1481 ep_number, toggle_bits, callback);
1482 }
1483
1484
1485 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
ftdi_elan_edset_input(struct usb_ftdi * ftdi,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,u8 toggle_bits,void (* callback)(void * endp,struct urb * urb,u8 * buf,int len,int toggle_bits,int error_count,int condition_code,int repeat_number,int halted,int skipped,int actual,int non_null))1486 static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
1487 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1488 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1489 int toggle_bits, int error_count, int condition_code, int repeat_number,
1490 int halted, int skipped, int actual, int non_null))
1491 {
1492 u8 ed = ed_number - 1;
1493 wait:if (ftdi->disconnected > 0) {
1494 return -ENODEV;
1495 } else if (ftdi->initialized == 0) {
1496 return -ENODEV;
1497 } else {
1498 int command_size;
1499 mutex_lock(&ftdi->u132_lock);
1500 command_size = ftdi->command_next - ftdi->command_head;
1501 if (command_size < COMMAND_SIZE) {
1502 struct u132_target *target = &ftdi->target[ed];
1503 struct u132_command *command = &ftdi->command[
1504 COMMAND_MASK & ftdi->command_next];
1505 u32 remaining_length = urb->transfer_buffer_length -
1506 urb->actual_length;
1507 command->header = 0x82 | (ed << 5);
1508 if (remaining_length == 0) {
1509 command->length = 0x0000;
1510 } else if (remaining_length > 1024) {
1511 command->length = 0x8000 | 1023;
1512 } else
1513 command->length = 0x8000 | (remaining_length -
1514 1);
1515 command->address = (toggle_bits << 6) | (ep_number << 2)
1516 | (address << 0);
1517 command->width = usb_maxpacket(urb->dev, urb->pipe,
1518 usb_pipeout(urb->pipe));
1519 command->follows = 0;
1520 command->value = 0;
1521 command->buffer = NULL;
1522 target->callback = callback;
1523 target->endp = endp;
1524 target->urb = urb;
1525 target->active = 1;
1526 ftdi->command_next += 1;
1527 ftdi_elan_kick_command_queue(ftdi);
1528 mutex_unlock(&ftdi->u132_lock);
1529 return 0;
1530 } else {
1531 mutex_unlock(&ftdi->u132_lock);
1532 msleep(100);
1533 goto wait;
1534 }
1535 }
1536 }
1537
usb_ftdi_elan_edset_input(struct platform_device * pdev,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,u8 toggle_bits,void (* callback)(void * endp,struct urb * urb,u8 * buf,int len,int toggle_bits,int error_count,int condition_code,int repeat_number,int halted,int skipped,int actual,int non_null))1538 int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
1539 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1540 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1541 int toggle_bits, int error_count, int condition_code, int repeat_number,
1542 int halted, int skipped, int actual, int non_null))
1543 {
1544 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1545 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1546 ep_number, toggle_bits, callback);
1547 }
1548
1549
1550 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
ftdi_elan_edset_empty(struct usb_ftdi * ftdi,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,u8 toggle_bits,void (* callback)(void * endp,struct urb * urb,u8 * buf,int len,int toggle_bits,int error_count,int condition_code,int repeat_number,int halted,int skipped,int actual,int non_null))1551 static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
1552 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1553 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1554 int toggle_bits, int error_count, int condition_code, int repeat_number,
1555 int halted, int skipped, int actual, int non_null))
1556 {
1557 u8 ed = ed_number - 1;
1558 wait:if (ftdi->disconnected > 0) {
1559 return -ENODEV;
1560 } else if (ftdi->initialized == 0) {
1561 return -ENODEV;
1562 } else {
1563 int command_size;
1564 mutex_lock(&ftdi->u132_lock);
1565 command_size = ftdi->command_next - ftdi->command_head;
1566 if (command_size < COMMAND_SIZE) {
1567 struct u132_target *target = &ftdi->target[ed];
1568 struct u132_command *command = &ftdi->command[
1569 COMMAND_MASK & ftdi->command_next];
1570 command->header = 0x81 | (ed << 5);
1571 command->length = 0x0000;
1572 command->address = (toggle_bits << 6) | (ep_number << 2)
1573 | (address << 0);
1574 command->width = usb_maxpacket(urb->dev, urb->pipe,
1575 usb_pipeout(urb->pipe));
1576 command->follows = 0;
1577 command->value = 0;
1578 command->buffer = NULL;
1579 target->callback = callback;
1580 target->endp = endp;
1581 target->urb = urb;
1582 target->active = 1;
1583 ftdi->command_next += 1;
1584 ftdi_elan_kick_command_queue(ftdi);
1585 mutex_unlock(&ftdi->u132_lock);
1586 return 0;
1587 } else {
1588 mutex_unlock(&ftdi->u132_lock);
1589 msleep(100);
1590 goto wait;
1591 }
1592 }
1593 }
1594
usb_ftdi_elan_edset_empty(struct platform_device * pdev,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,u8 toggle_bits,void (* callback)(void * endp,struct urb * urb,u8 * buf,int len,int toggle_bits,int error_count,int condition_code,int repeat_number,int halted,int skipped,int actual,int non_null))1595 int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
1596 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1597 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1598 int toggle_bits, int error_count, int condition_code, int repeat_number,
1599 int halted, int skipped, int actual, int non_null))
1600 {
1601 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1602 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1603 ep_number, toggle_bits, callback);
1604 }
1605
1606
1607 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
ftdi_elan_edset_output(struct usb_ftdi * ftdi,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,u8 toggle_bits,void (* callback)(void * endp,struct urb * urb,u8 * buf,int len,int toggle_bits,int error_count,int condition_code,int repeat_number,int halted,int skipped,int actual,int non_null))1608 static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
1609 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1610 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1611 int toggle_bits, int error_count, int condition_code, int repeat_number,
1612 int halted, int skipped, int actual, int non_null))
1613 {
1614 u8 ed = ed_number - 1;
1615 wait:if (ftdi->disconnected > 0) {
1616 return -ENODEV;
1617 } else if (ftdi->initialized == 0) {
1618 return -ENODEV;
1619 } else {
1620 int command_size;
1621 mutex_lock(&ftdi->u132_lock);
1622 command_size = ftdi->command_next - ftdi->command_head;
1623 if (command_size < COMMAND_SIZE) {
1624 u8 *b;
1625 u16 urb_size;
1626 int i = 0;
1627 char data[30 *3 + 4];
1628 char *d = data;
1629 int m = (sizeof(data) - 1) / 3 - 1;
1630 int l = 0;
1631 struct u132_target *target = &ftdi->target[ed];
1632 struct u132_command *command = &ftdi->command[
1633 COMMAND_MASK & ftdi->command_next];
1634 command->header = 0x81 | (ed << 5);
1635 command->address = (toggle_bits << 6) | (ep_number << 2)
1636 | (address << 0);
1637 command->width = usb_maxpacket(urb->dev, urb->pipe,
1638 usb_pipeout(urb->pipe));
1639 command->follows = min_t(u32, 1024,
1640 urb->transfer_buffer_length -
1641 urb->actual_length);
1642 command->value = 0;
1643 command->buffer = urb->transfer_buffer +
1644 urb->actual_length;
1645 command->length = 0x8000 | (command->follows - 1);
1646 b = command->buffer;
1647 urb_size = command->follows;
1648 data[0] = 0;
1649 while (urb_size-- > 0) {
1650 if (i > m) {
1651 } else if (i++ < m) {
1652 int w = sprintf(d, " %02X", *b++);
1653 d += w;
1654 l += w;
1655 } else
1656 d += sprintf(d, " ..");
1657 }
1658 target->callback = callback;
1659 target->endp = endp;
1660 target->urb = urb;
1661 target->active = 1;
1662 ftdi->command_next += 1;
1663 ftdi_elan_kick_command_queue(ftdi);
1664 mutex_unlock(&ftdi->u132_lock);
1665 return 0;
1666 } else {
1667 mutex_unlock(&ftdi->u132_lock);
1668 msleep(100);
1669 goto wait;
1670 }
1671 }
1672 }
1673
usb_ftdi_elan_edset_output(struct platform_device * pdev,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,u8 toggle_bits,void (* callback)(void * endp,struct urb * urb,u8 * buf,int len,int toggle_bits,int error_count,int condition_code,int repeat_number,int halted,int skipped,int actual,int non_null))1674 int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
1675 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1676 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1677 int toggle_bits, int error_count, int condition_code, int repeat_number,
1678 int halted, int skipped, int actual, int non_null))
1679 {
1680 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1681 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1682 ep_number, toggle_bits, callback);
1683 }
1684
1685
1686 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
ftdi_elan_edset_single(struct usb_ftdi * ftdi,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,u8 toggle_bits,void (* callback)(void * endp,struct urb * urb,u8 * buf,int len,int toggle_bits,int error_count,int condition_code,int repeat_number,int halted,int skipped,int actual,int non_null))1687 static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
1688 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1689 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1690 int toggle_bits, int error_count, int condition_code, int repeat_number,
1691 int halted, int skipped, int actual, int non_null))
1692 {
1693 u8 ed = ed_number - 1;
1694 wait:if (ftdi->disconnected > 0) {
1695 return -ENODEV;
1696 } else if (ftdi->initialized == 0) {
1697 return -ENODEV;
1698 } else {
1699 int command_size;
1700 mutex_lock(&ftdi->u132_lock);
1701 command_size = ftdi->command_next - ftdi->command_head;
1702 if (command_size < COMMAND_SIZE) {
1703 u32 remaining_length = urb->transfer_buffer_length -
1704 urb->actual_length;
1705 struct u132_target *target = &ftdi->target[ed];
1706 struct u132_command *command = &ftdi->command[
1707 COMMAND_MASK & ftdi->command_next];
1708 command->header = 0x83 | (ed << 5);
1709 if (remaining_length == 0) {
1710 command->length = 0x0000;
1711 } else if (remaining_length > 1024) {
1712 command->length = 0x8000 | 1023;
1713 } else
1714 command->length = 0x8000 | (remaining_length -
1715 1);
1716 command->address = (toggle_bits << 6) | (ep_number << 2)
1717 | (address << 0);
1718 command->width = usb_maxpacket(urb->dev, urb->pipe,
1719 usb_pipeout(urb->pipe));
1720 command->follows = 0;
1721 command->value = 0;
1722 command->buffer = NULL;
1723 target->callback = callback;
1724 target->endp = endp;
1725 target->urb = urb;
1726 target->active = 1;
1727 ftdi->command_next += 1;
1728 ftdi_elan_kick_command_queue(ftdi);
1729 mutex_unlock(&ftdi->u132_lock);
1730 return 0;
1731 } else {
1732 mutex_unlock(&ftdi->u132_lock);
1733 msleep(100);
1734 goto wait;
1735 }
1736 }
1737 }
1738
usb_ftdi_elan_edset_single(struct platform_device * pdev,u8 ed_number,void * endp,struct urb * urb,u8 address,u8 ep_number,u8 toggle_bits,void (* callback)(void * endp,struct urb * urb,u8 * buf,int len,int toggle_bits,int error_count,int condition_code,int repeat_number,int halted,int skipped,int actual,int non_null))1739 int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
1740 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1741 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1742 int toggle_bits, int error_count, int condition_code, int repeat_number,
1743 int halted, int skipped, int actual, int non_null))
1744 {
1745 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1746 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1747 ep_number, toggle_bits, callback);
1748 }
1749
1750
1751 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
ftdi_elan_edset_flush(struct usb_ftdi * ftdi,u8 ed_number,void * endp)1752 static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
1753 void *endp)
1754 {
1755 u8 ed = ed_number - 1;
1756 if (ftdi->disconnected > 0) {
1757 return -ENODEV;
1758 } else if (ftdi->initialized == 0) {
1759 return -ENODEV;
1760 } else {
1761 struct u132_target *target = &ftdi->target[ed];
1762 mutex_lock(&ftdi->u132_lock);
1763 if (target->abandoning > 0) {
1764 mutex_unlock(&ftdi->u132_lock);
1765 return 0;
1766 } else {
1767 target->abandoning = 1;
1768 wait_1:if (target->active == 1) {
1769 int command_size = ftdi->command_next -
1770 ftdi->command_head;
1771 if (command_size < COMMAND_SIZE) {
1772 struct u132_command *command =
1773 &ftdi->command[COMMAND_MASK &
1774 ftdi->command_next];
1775 command->header = 0x80 | (ed << 5) |
1776 0x4;
1777 command->length = 0x00;
1778 command->address = 0x00;
1779 command->width = 0x00;
1780 command->follows = 0;
1781 command->value = 0;
1782 command->buffer = &command->value;
1783 ftdi->command_next += 1;
1784 ftdi_elan_kick_command_queue(ftdi);
1785 } else {
1786 mutex_unlock(&ftdi->u132_lock);
1787 msleep(100);
1788 mutex_lock(&ftdi->u132_lock);
1789 goto wait_1;
1790 }
1791 }
1792 mutex_unlock(&ftdi->u132_lock);
1793 return 0;
1794 }
1795 }
1796 }
1797
usb_ftdi_elan_edset_flush(struct platform_device * pdev,u8 ed_number,void * endp)1798 int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
1799 void *endp)
1800 {
1801 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1802 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
1803 }
1804
1805
1806 EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
ftdi_elan_flush_input_fifo(struct usb_ftdi * ftdi)1807 static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1808 {
1809 int retry_on_empty = 10;
1810 int retry_on_timeout = 5;
1811 int retry_on_status = 20;
1812 more:{
1813 int packet_bytes = 0;
1814 int retval = usb_bulk_msg(ftdi->udev,
1815 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1816 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1817 &packet_bytes, 100);
1818 if (packet_bytes > 2) {
1819 char diag[30 *3 + 4];
1820 char *d = diag;
1821 int m = (sizeof(diag) - 1) / 3 - 1;
1822 char *b = ftdi->bulk_in_buffer;
1823 int bytes_read = 0;
1824 diag[0] = 0;
1825 while (packet_bytes-- > 0) {
1826 char c = *b++;
1827 if (bytes_read < m) {
1828 d += sprintf(d, " %02X",
1829 0x000000FF & c);
1830 } else if (bytes_read > m) {
1831 } else
1832 d += sprintf(d, " ..");
1833 bytes_read += 1;
1834 continue;
1835 }
1836 goto more;
1837 } else if (packet_bytes > 1) {
1838 char s1 = ftdi->bulk_in_buffer[0];
1839 char s2 = ftdi->bulk_in_buffer[1];
1840 if (s1 == 0x31 && s2 == 0x60) {
1841 return 0;
1842 } else if (retry_on_status-- > 0) {
1843 goto more;
1844 } else {
1845 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1846 return -EFAULT;
1847 }
1848 } else if (packet_bytes > 0) {
1849 char b1 = ftdi->bulk_in_buffer[0];
1850 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1851 b1);
1852 if (retry_on_status-- > 0) {
1853 goto more;
1854 } else {
1855 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
1856 return -EFAULT;
1857 }
1858 } else if (retval == -ETIMEDOUT) {
1859 if (retry_on_timeout-- > 0) {
1860 goto more;
1861 } else {
1862 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
1863 return -ENOMEM;
1864 }
1865 } else if (retval == 0) {
1866 if (retry_on_empty-- > 0) {
1867 goto more;
1868 } else {
1869 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
1870 return -ENOMEM;
1871 }
1872 } else {
1873 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1874 return retval;
1875 }
1876 }
1877 return -1;
1878 }
1879
1880
1881 /*
1882 * send the long flush sequence
1883 *
1884 */
ftdi_elan_synchronize_flush(struct usb_ftdi * ftdi)1885 static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1886 {
1887 int retval;
1888 struct urb *urb;
1889 char *buf;
1890 int I = 257;
1891 int i = 0;
1892 urb = usb_alloc_urb(0, GFP_KERNEL);
1893 if (!urb)
1894 return -ENOMEM;
1895 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1896 if (!buf) {
1897 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
1898 usb_free_urb(urb);
1899 return -ENOMEM;
1900 }
1901 while (I-- > 0)
1902 buf[i++] = 0x55;
1903 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1904 ftdi->bulk_out_endpointAddr), buf, i,
1905 ftdi_elan_write_bulk_callback, ftdi);
1906 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1907 retval = usb_submit_urb(urb, GFP_KERNEL);
1908 if (retval) {
1909 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
1910 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1911 usb_free_urb(urb);
1912 return -ENOMEM;
1913 }
1914 usb_free_urb(urb);
1915 return 0;
1916 }
1917
1918
1919 /*
1920 * send the reset sequence
1921 *
1922 */
ftdi_elan_synchronize_reset(struct usb_ftdi * ftdi)1923 static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1924 {
1925 int retval;
1926 struct urb *urb;
1927 char *buf;
1928 int I = 4;
1929 int i = 0;
1930 urb = usb_alloc_urb(0, GFP_KERNEL);
1931 if (!urb)
1932 return -ENOMEM;
1933 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1934 if (!buf) {
1935 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
1936 usb_free_urb(urb);
1937 return -ENOMEM;
1938 }
1939 buf[i++] = 0x55;
1940 buf[i++] = 0xAA;
1941 buf[i++] = 0x5A;
1942 buf[i++] = 0xA5;
1943 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1944 ftdi->bulk_out_endpointAddr), buf, i,
1945 ftdi_elan_write_bulk_callback, ftdi);
1946 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1947 retval = usb_submit_urb(urb, GFP_KERNEL);
1948 if (retval) {
1949 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
1950 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1951 usb_free_urb(urb);
1952 return -ENOMEM;
1953 }
1954 usb_free_urb(urb);
1955 return 0;
1956 }
1957
ftdi_elan_synchronize(struct usb_ftdi * ftdi)1958 static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
1959 {
1960 int retval;
1961 int long_stop = 10;
1962 int retry_on_timeout = 5;
1963 int retry_on_empty = 10;
1964 int err_count = 0;
1965 retval = ftdi_elan_flush_input_fifo(ftdi);
1966 if (retval)
1967 return retval;
1968 ftdi->bulk_in_left = 0;
1969 ftdi->bulk_in_last = -1;
1970 while (long_stop-- > 0) {
1971 int read_stop;
1972 int read_stuck;
1973 retval = ftdi_elan_synchronize_flush(ftdi);
1974 if (retval)
1975 return retval;
1976 retval = ftdi_elan_flush_input_fifo(ftdi);
1977 if (retval)
1978 return retval;
1979 reset:retval = ftdi_elan_synchronize_reset(ftdi);
1980 if (retval)
1981 return retval;
1982 read_stop = 100;
1983 read_stuck = 10;
1984 read:{
1985 int packet_bytes = 0;
1986 retval = usb_bulk_msg(ftdi->udev,
1987 usb_rcvbulkpipe(ftdi->udev,
1988 ftdi->bulk_in_endpointAddr),
1989 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1990 &packet_bytes, 500);
1991 if (packet_bytes > 2) {
1992 char diag[30 *3 + 4];
1993 char *d = diag;
1994 int m = (sizeof(diag) - 1) / 3 - 1;
1995 char *b = ftdi->bulk_in_buffer;
1996 int bytes_read = 0;
1997 unsigned char c = 0;
1998 diag[0] = 0;
1999 while (packet_bytes-- > 0) {
2000 c = *b++;
2001 if (bytes_read < m) {
2002 d += sprintf(d, " %02X", c);
2003 } else if (bytes_read > m) {
2004 } else
2005 d += sprintf(d, " ..");
2006 bytes_read += 1;
2007 continue;
2008 }
2009 if (c == 0x7E) {
2010 return 0;
2011 } else {
2012 if (c == 0x55) {
2013 goto read;
2014 } else if (read_stop-- > 0) {
2015 goto read;
2016 } else {
2017 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2018 continue;
2019 }
2020 }
2021 } else if (packet_bytes > 1) {
2022 unsigned char s1 = ftdi->bulk_in_buffer[0];
2023 unsigned char s2 = ftdi->bulk_in_buffer[1];
2024 if (s1 == 0x31 && s2 == 0x00) {
2025 if (read_stuck-- > 0) {
2026 goto read;
2027 } else
2028 goto reset;
2029 } else {
2030 if (read_stop-- > 0) {
2031 goto read;
2032 } else {
2033 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2034 continue;
2035 }
2036 }
2037 } else if (packet_bytes > 0) {
2038 if (read_stop-- > 0) {
2039 goto read;
2040 } else {
2041 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2042 continue;
2043 }
2044 } else if (retval == -ETIMEDOUT) {
2045 if (retry_on_timeout-- > 0) {
2046 goto read;
2047 } else {
2048 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2049 continue;
2050 }
2051 } else if (retval == 0) {
2052 if (retry_on_empty-- > 0) {
2053 goto read;
2054 } else {
2055 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2056 continue;
2057 }
2058 } else {
2059 err_count += 1;
2060 dev_err(&ftdi->udev->dev, "error = %d\n",
2061 retval);
2062 if (read_stop-- > 0) {
2063 goto read;
2064 } else {
2065 dev_err(&ftdi->udev->dev, "retry limit reached\n");
2066 continue;
2067 }
2068 }
2069 }
2070 }
2071 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2072 return -EFAULT;
2073 }
2074
ftdi_elan_stuck_waiting(struct usb_ftdi * ftdi)2075 static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2076 {
2077 int retry_on_empty = 10;
2078 int retry_on_timeout = 5;
2079 int retry_on_status = 50;
2080 more:{
2081 int packet_bytes = 0;
2082 int retval = usb_bulk_msg(ftdi->udev,
2083 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2084 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2085 &packet_bytes, 1000);
2086 if (packet_bytes > 2) {
2087 char diag[30 *3 + 4];
2088 char *d = diag;
2089 int m = (sizeof(diag) - 1) / 3 - 1;
2090 char *b = ftdi->bulk_in_buffer;
2091 int bytes_read = 0;
2092 diag[0] = 0;
2093 while (packet_bytes-- > 0) {
2094 char c = *b++;
2095 if (bytes_read < m) {
2096 d += sprintf(d, " %02X",
2097 0x000000FF & c);
2098 } else if (bytes_read > m) {
2099 } else
2100 d += sprintf(d, " ..");
2101 bytes_read += 1;
2102 continue;
2103 }
2104 goto more;
2105 } else if (packet_bytes > 1) {
2106 char s1 = ftdi->bulk_in_buffer[0];
2107 char s2 = ftdi->bulk_in_buffer[1];
2108 if (s1 == 0x31 && s2 == 0x60) {
2109 return 0;
2110 } else if (retry_on_status-- > 0) {
2111 msleep(5);
2112 goto more;
2113 } else
2114 return -EFAULT;
2115 } else if (packet_bytes > 0) {
2116 char b1 = ftdi->bulk_in_buffer[0];
2117 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
2118 if (retry_on_status-- > 0) {
2119 msleep(5);
2120 goto more;
2121 } else {
2122 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
2123 return -EFAULT;
2124 }
2125 } else if (retval == -ETIMEDOUT) {
2126 if (retry_on_timeout-- > 0) {
2127 goto more;
2128 } else {
2129 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
2130 return -ENOMEM;
2131 }
2132 } else if (retval == 0) {
2133 if (retry_on_empty-- > 0) {
2134 goto more;
2135 } else {
2136 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
2137 return -ENOMEM;
2138 }
2139 } else {
2140 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2141 return -ENOMEM;
2142 }
2143 }
2144 return -1;
2145 }
2146
ftdi_elan_checkingPCI(struct usb_ftdi * ftdi)2147 static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2148 {
2149 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2150 if (UxxxStatus)
2151 return UxxxStatus;
2152 if (ftdi->controlreg & 0x00400000) {
2153 if (ftdi->card_ejected) {
2154 } else {
2155 ftdi->card_ejected = 1;
2156 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2157 ftdi->controlreg);
2158 }
2159 return -ENODEV;
2160 } else {
2161 u8 fn = ftdi->function - 1;
2162 int activePCIfn = fn << 8;
2163 u32 pcidata;
2164 u32 pciVID;
2165 u32 pciPID;
2166 int reg = 0;
2167 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2168 &pcidata);
2169 if (UxxxStatus)
2170 return UxxxStatus;
2171 pciVID = pcidata & 0xFFFF;
2172 pciPID = (pcidata >> 16) & 0xFFFF;
2173 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2174 ftdi->platform_data.device) {
2175 return 0;
2176 } else {
2177 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
2178 ftdi->platform_data.vendor, pciVID,
2179 ftdi->platform_data.device, pciPID);
2180 return -ENODEV;
2181 }
2182 }
2183 }
2184
2185
2186 #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
2187 offsetof(struct ohci_regs, member), 0, data);
2188 #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
2189 offsetof(struct ohci_regs, member), 0, data);
2190
2191 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
2192 #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2193 OHCI_INTR_WDH)
ftdi_elan_check_controller(struct usb_ftdi * ftdi,int quirk)2194 static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2195 {
2196 int devices = 0;
2197 int retval;
2198 u32 hc_control;
2199 int num_ports;
2200 u32 control;
2201 u32 rh_a = -1;
2202 u32 status;
2203 u32 fminterval;
2204 u32 hc_fminterval;
2205 u32 periodicstart;
2206 u32 cmdstatus;
2207 u32 roothub_a;
2208 int mask = OHCI_INTR_INIT;
2209 int sleep_time = 0;
2210 int reset_timeout = 30; /* ... allow extra time */
2211 int temp;
2212 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2213 if (retval)
2214 return retval;
2215 retval = ftdi_read_pcimem(ftdi, control, &control);
2216 if (retval)
2217 return retval;
2218 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2219 if (retval)
2220 return retval;
2221 num_ports = rh_a & RH_A_NDP;
2222 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2223 if (retval)
2224 return retval;
2225 hc_fminterval &= 0x3fff;
2226 if (hc_fminterval != FI) {
2227 }
2228 hc_fminterval |= FSMP(hc_fminterval) << 16;
2229 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2230 if (retval)
2231 return retval;
2232 switch (hc_control & OHCI_CTRL_HCFS) {
2233 case OHCI_USB_OPER:
2234 sleep_time = 0;
2235 break;
2236 case OHCI_USB_SUSPEND:
2237 case OHCI_USB_RESUME:
2238 hc_control &= OHCI_CTRL_RWC;
2239 hc_control |= OHCI_USB_RESUME;
2240 sleep_time = 10;
2241 break;
2242 default:
2243 hc_control &= OHCI_CTRL_RWC;
2244 hc_control |= OHCI_USB_RESET;
2245 sleep_time = 50;
2246 break;
2247 }
2248 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2249 if (retval)
2250 return retval;
2251 retval = ftdi_read_pcimem(ftdi, control, &control);
2252 if (retval)
2253 return retval;
2254 msleep(sleep_time);
2255 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2256 if (retval)
2257 return retval;
2258 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2259 for (temp = 0; temp < num_ports; temp++) {
2260 retval = ftdi_write_pcimem(ftdi,
2261 roothub.portstatus[temp], RH_PS_LSDA);
2262 if (retval)
2263 return retval;
2264 }
2265 }
2266 retval = ftdi_read_pcimem(ftdi, control, &control);
2267 if (retval)
2268 return retval;
2269 retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2270 if (retval)
2271 return retval;
2272 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2273 if (retval)
2274 return retval;
2275 extra:{
2276 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2277 if (retval)
2278 return retval;
2279 if (0 != (status & OHCI_HCR)) {
2280 if (--reset_timeout == 0) {
2281 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
2282 return -ENODEV;
2283 } else {
2284 msleep(5);
2285 goto extra;
2286 }
2287 }
2288 }
2289 if (quirk & OHCI_QUIRK_INITRESET) {
2290 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2291 if (retval)
2292 return retval;
2293 retval = ftdi_read_pcimem(ftdi, control, &control);
2294 if (retval)
2295 return retval;
2296 }
2297 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2298 if (retval)
2299 return retval;
2300 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2301 if (retval)
2302 return retval;
2303 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2304 if (retval)
2305 return retval;
2306 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2307 if (retval)
2308 return retval;
2309 retval = ftdi_write_pcimem(ftdi, fminterval,
2310 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2311 if (retval)
2312 return retval;
2313 retval = ftdi_write_pcimem(ftdi, periodicstart,
2314 ((9 *hc_fminterval) / 10) & 0x3fff);
2315 if (retval)
2316 return retval;
2317 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2318 if (retval)
2319 return retval;
2320 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2321 if (retval)
2322 return retval;
2323 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2324 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2325 quirk |= OHCI_QUIRK_INITRESET;
2326 goto retry;
2327 } else
2328 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2329 fminterval, periodicstart);
2330 } /* start controller operations */
2331 hc_control &= OHCI_CTRL_RWC;
2332 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2333 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2334 if (retval)
2335 return retval;
2336 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2337 if (retval)
2338 return retval;
2339 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2340 if (retval)
2341 return retval;
2342 retval = ftdi_read_pcimem(ftdi, control, &control);
2343 if (retval)
2344 return retval;
2345 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2346 if (retval)
2347 return retval;
2348 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2349 if (retval)
2350 return retval;
2351 retval = ftdi_write_pcimem(ftdi, intrdisable,
2352 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2353 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2354 OHCI_INTR_SO);
2355 if (retval)
2356 return retval; /* handle root hub init quirks ... */
2357 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2358 if (retval)
2359 return retval;
2360 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2361 if (quirk & OHCI_QUIRK_SUPERIO) {
2362 roothub_a |= RH_A_NOCP;
2363 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2364 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2365 if (retval)
2366 return retval;
2367 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2368 roothub_a |= RH_A_NPS;
2369 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2370 if (retval)
2371 return retval;
2372 }
2373 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2374 if (retval)
2375 return retval;
2376 retval = ftdi_write_pcimem(ftdi, roothub.b,
2377 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2378 if (retval)
2379 return retval;
2380 retval = ftdi_read_pcimem(ftdi, control, &control);
2381 if (retval)
2382 return retval;
2383 mdelay((roothub_a >> 23) & 0x1fe);
2384 for (temp = 0; temp < num_ports; temp++) {
2385 u32 portstatus;
2386 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2387 &portstatus);
2388 if (retval)
2389 return retval;
2390 if (1 & portstatus)
2391 devices += 1;
2392 }
2393 return devices;
2394 }
2395
ftdi_elan_setup_controller(struct usb_ftdi * ftdi,int fn)2396 static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
2397 {
2398 u32 latence_timer;
2399 int UxxxStatus;
2400 u32 pcidata;
2401 int reg = 0;
2402 int activePCIfn = fn << 8;
2403 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2404 if (UxxxStatus)
2405 return UxxxStatus;
2406 reg = 16;
2407 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2408 0xFFFFFFFF);
2409 if (UxxxStatus)
2410 return UxxxStatus;
2411 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2412 &pcidata);
2413 if (UxxxStatus)
2414 return UxxxStatus;
2415 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2416 0xF0000000);
2417 if (UxxxStatus)
2418 return UxxxStatus;
2419 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2420 &pcidata);
2421 if (UxxxStatus)
2422 return UxxxStatus;
2423 reg = 12;
2424 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2425 &latence_timer);
2426 if (UxxxStatus)
2427 return UxxxStatus;
2428 latence_timer &= 0xFFFF00FF;
2429 latence_timer |= 0x00001600;
2430 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2431 latence_timer);
2432 if (UxxxStatus)
2433 return UxxxStatus;
2434 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2435 &pcidata);
2436 if (UxxxStatus)
2437 return UxxxStatus;
2438 reg = 4;
2439 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2440 0x06);
2441 if (UxxxStatus)
2442 return UxxxStatus;
2443 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2444 &pcidata);
2445 if (UxxxStatus)
2446 return UxxxStatus;
2447 for (reg = 0; reg <= 0x54; reg += 4) {
2448 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2449 if (UxxxStatus)
2450 return UxxxStatus;
2451 }
2452 return 0;
2453 }
2454
ftdi_elan_close_controller(struct usb_ftdi * ftdi,int fn)2455 static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2456 {
2457 u32 latence_timer;
2458 int UxxxStatus;
2459 u32 pcidata;
2460 int reg = 0;
2461 int activePCIfn = fn << 8;
2462 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2463 if (UxxxStatus)
2464 return UxxxStatus;
2465 reg = 16;
2466 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2467 0xFFFFFFFF);
2468 if (UxxxStatus)
2469 return UxxxStatus;
2470 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2471 &pcidata);
2472 if (UxxxStatus)
2473 return UxxxStatus;
2474 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2475 0x00000000);
2476 if (UxxxStatus)
2477 return UxxxStatus;
2478 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2479 &pcidata);
2480 if (UxxxStatus)
2481 return UxxxStatus;
2482 reg = 12;
2483 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2484 &latence_timer);
2485 if (UxxxStatus)
2486 return UxxxStatus;
2487 latence_timer &= 0xFFFF00FF;
2488 latence_timer |= 0x00001600;
2489 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2490 latence_timer);
2491 if (UxxxStatus)
2492 return UxxxStatus;
2493 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2494 &pcidata);
2495 if (UxxxStatus)
2496 return UxxxStatus;
2497 reg = 4;
2498 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2499 0x00);
2500 if (UxxxStatus)
2501 return UxxxStatus;
2502 return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
2503 }
2504
ftdi_elan_found_controller(struct usb_ftdi * ftdi,int fn,int quirk)2505 static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2506 {
2507 int result;
2508 int UxxxStatus;
2509 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2510 if (UxxxStatus)
2511 return UxxxStatus;
2512 result = ftdi_elan_check_controller(ftdi, quirk);
2513 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2514 if (UxxxStatus)
2515 return UxxxStatus;
2516 return result;
2517 }
2518
ftdi_elan_enumeratePCI(struct usb_ftdi * ftdi)2519 static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2520 {
2521 u32 controlreg;
2522 u8 sensebits;
2523 int UxxxStatus;
2524 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2525 if (UxxxStatus)
2526 return UxxxStatus;
2527 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2528 if (UxxxStatus)
2529 return UxxxStatus;
2530 msleep(750);
2531 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2532 if (UxxxStatus)
2533 return UxxxStatus;
2534 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2535 if (UxxxStatus)
2536 return UxxxStatus;
2537 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2538 if (UxxxStatus)
2539 return UxxxStatus;
2540 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2541 if (UxxxStatus)
2542 return UxxxStatus;
2543 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2544 if (UxxxStatus)
2545 return UxxxStatus;
2546 msleep(250);
2547 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2548 if (UxxxStatus)
2549 return UxxxStatus;
2550 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2551 if (UxxxStatus)
2552 return UxxxStatus;
2553 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2554 if (UxxxStatus)
2555 return UxxxStatus;
2556 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2557 if (UxxxStatus)
2558 return UxxxStatus;
2559 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2560 if (UxxxStatus)
2561 return UxxxStatus;
2562 msleep(1000);
2563 sensebits = (controlreg >> 16) & 0x000F;
2564 if (0x0D == sensebits)
2565 return 0;
2566 else
2567 return - ENXIO;
2568 }
2569
ftdi_elan_setupOHCI(struct usb_ftdi * ftdi)2570 static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2571 {
2572 int UxxxStatus;
2573 u32 pcidata;
2574 int reg = 0;
2575 u8 fn;
2576 int activePCIfn = 0;
2577 int max_devices = 0;
2578 int controllers = 0;
2579 int unrecognized = 0;
2580 ftdi->function = 0;
2581 for (fn = 0; (fn < 4); fn++) {
2582 u32 pciVID = 0;
2583 u32 pciPID = 0;
2584 int devices = 0;
2585 activePCIfn = fn << 8;
2586 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2587 &pcidata);
2588 if (UxxxStatus)
2589 return UxxxStatus;
2590 pciVID = pcidata & 0xFFFF;
2591 pciPID = (pcidata >> 16) & 0xFFFF;
2592 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2593 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2594 controllers += 1;
2595 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2596 {
2597 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2598 controllers += 1;
2599 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2600 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2601 controllers += 1;
2602 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2603 {
2604 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2605 controllers += 1;
2606 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2607 devices = ftdi_elan_found_controller(ftdi, fn,
2608 OHCI_QUIRK_AMD756);
2609 controllers += 1;
2610 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2611 devices = ftdi_elan_found_controller(ftdi, fn,
2612 OHCI_QUIRK_ZFMICRO);
2613 controllers += 1;
2614 } else if (0 == pcidata) {
2615 } else
2616 unrecognized += 1;
2617 if (devices > max_devices) {
2618 max_devices = devices;
2619 ftdi->function = fn + 1;
2620 ftdi->platform_data.vendor = pciVID;
2621 ftdi->platform_data.device = pciPID;
2622 }
2623 }
2624 if (ftdi->function > 0) {
2625 return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
2626 } else if (controllers > 0) {
2627 return -ENXIO;
2628 } else if (unrecognized > 0) {
2629 return -ENXIO;
2630 } else {
2631 ftdi->enumerated = 0;
2632 return -ENXIO;
2633 }
2634 }
2635
2636
2637 /*
2638 * we use only the first bulk-in and bulk-out endpoints
2639 */
ftdi_elan_probe(struct usb_interface * interface,const struct usb_device_id * id)2640 static int ftdi_elan_probe(struct usb_interface *interface,
2641 const struct usb_device_id *id)
2642 {
2643 struct usb_host_interface *iface_desc;
2644 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
2645 int retval;
2646 struct usb_ftdi *ftdi;
2647
2648 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2649 if (!ftdi)
2650 return -ENOMEM;
2651
2652 mutex_lock(&ftdi_module_lock);
2653 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2654 ftdi->sequence_num = ++ftdi_instances;
2655 mutex_unlock(&ftdi_module_lock);
2656 ftdi_elan_init_kref(ftdi);
2657 sema_init(&ftdi->sw_lock, 1);
2658 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2659 ftdi->interface = interface;
2660 mutex_init(&ftdi->u132_lock);
2661 ftdi->expected = 4;
2662
2663 iface_desc = interface->cur_altsetting;
2664 retval = usb_find_common_endpoints(iface_desc,
2665 &bulk_in, &bulk_out, NULL, NULL);
2666 if (retval) {
2667 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
2668 goto error;
2669 }
2670
2671 ftdi->bulk_in_size = usb_endpoint_maxp(bulk_in);
2672 ftdi->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
2673 ftdi->bulk_in_buffer = kmalloc(ftdi->bulk_in_size, GFP_KERNEL);
2674 if (!ftdi->bulk_in_buffer) {
2675 retval = -ENOMEM;
2676 goto error;
2677 }
2678
2679 ftdi->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
2680
2681 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2682 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2683 ftdi->bulk_out_endpointAddr);
2684 usb_set_intfdata(interface, ftdi);
2685 if (iface_desc->desc.bInterfaceNumber == 0 &&
2686 ftdi->bulk_in_endpointAddr == 0x81 &&
2687 ftdi->bulk_out_endpointAddr == 0x02) {
2688 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2689 if (retval) {
2690 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
2691 usb_set_intfdata(interface, NULL);
2692 retval = -ENOMEM;
2693 goto error;
2694 } else {
2695 ftdi->class = &ftdi_elan_jtag_class;
2696 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2697 ftdi, iface_desc->desc.bInterfaceNumber,
2698 interface->minor);
2699 return 0;
2700 }
2701 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2702 ftdi->bulk_in_endpointAddr == 0x83 &&
2703 ftdi->bulk_out_endpointAddr == 0x04) {
2704 ftdi->class = NULL;
2705 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2706 ftdi, iface_desc->desc.bInterfaceNumber);
2707 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2708 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2709 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2710 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2711 return 0;
2712 } else {
2713 dev_err(&ftdi->udev->dev,
2714 "Could not find ELAN's U132 device\n");
2715 retval = -ENODEV;
2716 goto error;
2717 }
2718 error:if (ftdi) {
2719 ftdi_elan_put_kref(ftdi);
2720 }
2721 return retval;
2722 }
2723
ftdi_elan_disconnect(struct usb_interface * interface)2724 static void ftdi_elan_disconnect(struct usb_interface *interface)
2725 {
2726 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2727 ftdi->disconnected += 1;
2728 if (ftdi->class) {
2729 int minor = interface->minor;
2730 struct usb_class_driver *class = ftdi->class;
2731 usb_set_intfdata(interface, NULL);
2732 usb_deregister_dev(interface, class);
2733 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2734 minor);
2735 } else {
2736 ftdi_status_cancel_work(ftdi);
2737 ftdi_command_cancel_work(ftdi);
2738 ftdi_response_cancel_work(ftdi);
2739 ftdi_elan_abandon_completions(ftdi);
2740 ftdi_elan_abandon_targets(ftdi);
2741 if (ftdi->registered) {
2742 platform_device_unregister(&ftdi->platform_dev);
2743 ftdi->synchronized = 0;
2744 ftdi->enumerated = 0;
2745 ftdi->initialized = 0;
2746 ftdi->registered = 0;
2747 }
2748 ftdi->disconnected += 1;
2749 usb_set_intfdata(interface, NULL);
2750 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
2751 }
2752 ftdi_elan_put_kref(ftdi);
2753 }
2754
2755 static struct usb_driver ftdi_elan_driver = {
2756 .name = "ftdi-elan",
2757 .probe = ftdi_elan_probe,
2758 .disconnect = ftdi_elan_disconnect,
2759 .id_table = ftdi_elan_table,
2760 };
ftdi_elan_init(void)2761 static int __init ftdi_elan_init(void)
2762 {
2763 int result;
2764 pr_info("driver %s\n", ftdi_elan_driver.name);
2765 mutex_init(&ftdi_module_lock);
2766 INIT_LIST_HEAD(&ftdi_static_list);
2767 result = usb_register(&ftdi_elan_driver);
2768 if (result) {
2769 pr_err("usb_register failed. Error number %d\n", result);
2770 }
2771 return result;
2772
2773 }
2774
ftdi_elan_exit(void)2775 static void __exit ftdi_elan_exit(void)
2776 {
2777 struct usb_ftdi *ftdi;
2778 struct usb_ftdi *temp;
2779 usb_deregister(&ftdi_elan_driver);
2780 pr_info("ftdi_u132 driver deregistered\n");
2781 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2782 ftdi_status_cancel_work(ftdi);
2783 ftdi_command_cancel_work(ftdi);
2784 ftdi_response_cancel_work(ftdi);
2785 }
2786 }
2787
2788
2789 module_init(ftdi_elan_init);
2790 module_exit(ftdi_elan_exit);
2791