blob: 8cda8814a3eff6a5231b9e298cac5ed7b0629272 [file] [log] [blame]
Tony Olecha5c66e42006-09-13 11:26:04 +01001/*
Joe Perches8dae6932014-04-04 15:16:04 -07002 * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
3 *
4 * Copyright(C) 2006 Elan Digital Systems Limited
5 * http://www.elandigitalsystems.com
6 *
7 * Author and Maintainer - Tony Olech - Elan Digital Systems
8 * tony.olech@elandigitalsystems.com
9 *
10 * This program is free software;you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2.
13 *
14 *
15 * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
16 * based on various USB client drivers in the 2.6.15 linux kernel
17 * with constant reference to the 3rd Edition of Linux Device Drivers
18 * published by O'Reilly
19 *
20 * The U132 adapter is a USB to CardBus adapter specifically designed
21 * for PC cards that contain an OHCI host controller. Typical PC cards
22 * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
23 *
24 * The U132 adapter will *NOT *work with PC cards that do not contain
25 * an OHCI controller. A simple way to test whether a PC card has an
26 * OHCI controller as an interface is to insert the PC card directly
27 * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
28 * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
29 * then there is a good chance that the U132 adapter will support the
30 * PC card.(you also need the specific client driver for the PC card)
31 *
32 * Please inform the Author and Maintainer about any PC cards that
33 * contain OHCI Host Controller and work when directly connected to
34 * an embedded CardBus slot but do not work when they are connected
35 * via an ELAN U132 adapter.
36 *
37 */
Tony Olecha5c66e42006-09-13 11:26:04 +010038#include <linux/kernel.h>
39#include <linux/errno.h>
40#include <linux/init.h>
41#include <linux/list.h>
42#include <linux/ioctl.h>
Tony Olech4b873612006-12-06 13:16:22 +000043#include <linux/pci_ids.h>
Tony Olecha5c66e42006-09-13 11:26:04 +010044#include <linux/slab.h>
45#include <linux/module.h>
46#include <linux/kref.h>
Matthias Kaehlckeeb33cae2007-07-13 21:29:46 +020047#include <linux/mutex.h>
Tony Olecha5c66e42006-09-13 11:26:04 +010048#include <asm/uaccess.h>
49#include <linux/usb.h>
50#include <linux/workqueue.h>
51#include <linux/platform_device.h>
52MODULE_AUTHOR("Tony Olech");
53MODULE_DESCRIPTION("FTDI ELAN driver");
54MODULE_LICENSE("GPL");
55#define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
Rusty Russell90ab5ee2012-01-13 09:32:20 +103056static bool distrust_firmware = 1;
Tony Olech4b873612006-12-06 13:16:22 +000057module_param(distrust_firmware, bool, 0);
58MODULE_PARM_DESC(distrust_firmware, "true to distrust firmware power/overcurren"
Joe Perches8dae6932014-04-04 15:16:04 -070059 "t setup");
Tony Olecha5c66e42006-09-13 11:26:04 +010060extern struct platform_driver u132_platform_driver;
61static struct workqueue_struct *status_queue;
62static struct workqueue_struct *command_queue;
63static struct workqueue_struct *respond_queue;
64/*
Joe Perches8dae6932014-04-04 15:16:04 -070065 * ftdi_module_lock exists to protect access to global variables
66 *
67 */
Matthias Kaehlckeeb33cae2007-07-13 21:29:46 +020068static struct mutex ftdi_module_lock;
Tony Olecha5c66e42006-09-13 11:26:04 +010069static int ftdi_instances = 0;
70static struct list_head ftdi_static_list;
71/*
Joe Perches8dae6932014-04-04 15:16:04 -070072 * end of the global variables protected by ftdi_module_lock
73 */
Tony Olecha5c66e42006-09-13 11:26:04 +010074#include "usb_u132.h"
Tony Olech4b873612006-12-06 13:16:22 +000075#include <asm/io.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020076#include <linux/usb/hcd.h>
David Brownell47f84682007-04-29 10:21:14 -070077
Joe Perches8dae6932014-04-04 15:16:04 -070078/* FIXME ohci.h is ONLY for internal use by the OHCI driver.
79 * If you're going to try stuff like this, you need to split
80 * out shareable stuff (register declarations?) into its own
81 * file, maybe name <linux/usb/ohci.h>
82 */
David Brownell47f84682007-04-29 10:21:14 -070083
Tony Olech4b873612006-12-06 13:16:22 +000084#include "../host/ohci.h"
Tony Olecha5c66e42006-09-13 11:26:04 +010085/* Define these values to match your devices*/
86#define USB_FTDI_ELAN_VENDOR_ID 0x0403
87#define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
88/* table of devices that work with this driver*/
Németh Márton33b9e162010-01-10 15:34:45 +010089static const struct usb_device_id ftdi_elan_table[] = {
Joe Perches8dae6932014-04-04 15:16:04 -070090 {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
91 { /* Terminating entry */ }
Tony Olecha5c66e42006-09-13 11:26:04 +010092};
93
94MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
95/* only the jtag(firmware upgrade device) interface requires
Joe Perches8dae6932014-04-04 15:16:04 -070096 * a device file and corresponding minor number, but the
97 * interface is created unconditionally - I suppose it could
98 * be configured or not according to a module parameter.
99 * But since we(now) require one interface per device,
100 * and since it unlikely that a normal installation would
101 * require more than a couple of elan-ftdi devices, 8 seems
102 * like a reasonable limit to have here, and if someone
103 * really requires more than 8 devices, then they can frig the
104 * code and recompile
105 */
Tony Olecha5c66e42006-09-13 11:26:04 +0100106#define USB_FTDI_ELAN_MINOR_BASE 192
107#define COMMAND_BITS 5
108#define COMMAND_SIZE (1<<COMMAND_BITS)
109#define COMMAND_MASK (COMMAND_SIZE-1)
110struct u132_command {
Joe Perches8dae6932014-04-04 15:16:04 -0700111 u8 header;
112 u16 length;
113 u8 address;
114 u8 width;
115 u32 value;
116 int follows;
117 void *buffer;
Tony Olecha5c66e42006-09-13 11:26:04 +0100118};
119#define RESPOND_BITS 5
120#define RESPOND_SIZE (1<<RESPOND_BITS)
121#define RESPOND_MASK (RESPOND_SIZE-1)
122struct u132_respond {
Joe Perches8dae6932014-04-04 15:16:04 -0700123 u8 header;
124 u8 address;
125 u32 *value;
126 int *result;
127 struct completion wait_completion;
Tony Olecha5c66e42006-09-13 11:26:04 +0100128};
129struct u132_target {
Joe Perches8dae6932014-04-04 15:16:04 -0700130 void *endp;
131 struct urb *urb;
132 int toggle_bits;
133 int error_count;
134 int condition_code;
135 int repeat_number;
136 int halted;
137 int skipped;
138 int actual;
139 int non_null;
140 int active;
141 int abandoning;
142 void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
143 int toggle_bits, int error_count, int condition_code,
144 int repeat_number, int halted, int skipped, int actual,
145 int non_null);
Tony Olecha5c66e42006-09-13 11:26:04 +0100146};
147/* Structure to hold all of our device specific stuff*/
148struct usb_ftdi {
Joe Perches8dae6932014-04-04 15:16:04 -0700149 struct list_head ftdi_list;
150 struct mutex u132_lock;
151 int command_next;
152 int command_head;
153 struct u132_command command[COMMAND_SIZE];
154 int respond_next;
155 int respond_head;
156 struct u132_respond respond[RESPOND_SIZE];
157 struct u132_target target[4];
158 char device_name[16];
159 unsigned synchronized:1;
160 unsigned enumerated:1;
161 unsigned registered:1;
162 unsigned initialized:1;
163 unsigned card_ejected:1;
164 int function;
165 int sequence_num;
166 int disconnected;
167 int gone_away;
168 int stuck_status;
169 int status_queue_delay;
170 struct semaphore sw_lock;
171 struct usb_device *udev;
172 struct usb_interface *interface;
173 struct usb_class_driver *class;
174 struct delayed_work status_work;
175 struct delayed_work command_work;
176 struct delayed_work respond_work;
177 struct u132_platform_data platform_data;
178 struct resource resources[0];
179 struct platform_device platform_dev;
180 unsigned char *bulk_in_buffer;
181 size_t bulk_in_size;
182 size_t bulk_in_last;
183 size_t bulk_in_left;
184 __u8 bulk_in_endpointAddr;
185 __u8 bulk_out_endpointAddr;
186 struct kref kref;
187 u32 controlreg;
188 u8 response[4 + 1024];
189 int expected;
190 int received;
191 int ed_found;
Tony Olecha5c66e42006-09-13 11:26:04 +0100192};
193#define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
194#define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
Joe Perches8dae6932014-04-04 15:16:04 -0700195 platform_dev)
Tony Olecha5c66e42006-09-13 11:26:04 +0100196static struct usb_driver ftdi_elan_driver;
197static void ftdi_elan_delete(struct kref *kref)
198{
Joe Perches8dae6932014-04-04 15:16:04 -0700199 struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
200 dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
201 usb_put_dev(ftdi->udev);
202 ftdi->disconnected += 1;
203 mutex_lock(&ftdi_module_lock);
204 list_del_init(&ftdi->ftdi_list);
205 ftdi_instances -= 1;
206 mutex_unlock(&ftdi_module_lock);
207 kfree(ftdi->bulk_in_buffer);
208 ftdi->bulk_in_buffer = NULL;
Tony Olecha5c66e42006-09-13 11:26:04 +0100209}
210
211static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
212{
Joe Perches8dae6932014-04-04 15:16:04 -0700213 kref_put(&ftdi->kref, ftdi_elan_delete);
Tony Olecha5c66e42006-09-13 11:26:04 +0100214}
215
216static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
217{
Joe Perches8dae6932014-04-04 15:16:04 -0700218 kref_get(&ftdi->kref);
Tony Olecha5c66e42006-09-13 11:26:04 +0100219}
220
221static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
222{
Joe Perches8dae6932014-04-04 15:16:04 -0700223 kref_init(&ftdi->kref);
Tony Olecha5c66e42006-09-13 11:26:04 +0100224}
225
226static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
227{
David Howellsc4028952006-11-22 14:57:56 +0000228 if (!queue_delayed_work(status_queue, &ftdi->status_work, delta))
229 kref_put(&ftdi->kref, ftdi_elan_delete);
Tony Olecha5c66e42006-09-13 11:26:04 +0100230}
231
232static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
233{
David Howellsc4028952006-11-22 14:57:56 +0000234 if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
235 kref_get(&ftdi->kref);
Tony Olecha5c66e42006-09-13 11:26:04 +0100236}
237
238static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
239{
Joe Perches8dae6932014-04-04 15:16:04 -0700240 if (cancel_delayed_work(&ftdi->status_work))
241 kref_put(&ftdi->kref, ftdi_elan_delete);
Tony Olecha5c66e42006-09-13 11:26:04 +0100242}
243
244static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
245{
David Howellsc4028952006-11-22 14:57:56 +0000246 if (!queue_delayed_work(command_queue, &ftdi->command_work, delta))
247 kref_put(&ftdi->kref, ftdi_elan_delete);
Tony Olecha5c66e42006-09-13 11:26:04 +0100248}
249
250static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
251{
David Howellsc4028952006-11-22 14:57:56 +0000252 if (queue_delayed_work(command_queue, &ftdi->command_work, delta))
253 kref_get(&ftdi->kref);
Tony Olecha5c66e42006-09-13 11:26:04 +0100254}
255
256static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
257{
Joe Perches8dae6932014-04-04 15:16:04 -0700258 if (cancel_delayed_work(&ftdi->command_work))
259 kref_put(&ftdi->kref, ftdi_elan_delete);
Tony Olecha5c66e42006-09-13 11:26:04 +0100260}
261
262static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
Joe Perches8dae6932014-04-04 15:16:04 -0700263 unsigned int delta)
Tony Olecha5c66e42006-09-13 11:26:04 +0100264{
David Howellsc4028952006-11-22 14:57:56 +0000265 if (!queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
266 kref_put(&ftdi->kref, ftdi_elan_delete);
Tony Olecha5c66e42006-09-13 11:26:04 +0100267}
268
269static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
270{
David Howellsc4028952006-11-22 14:57:56 +0000271 if (queue_delayed_work(respond_queue, &ftdi->respond_work, delta))
272 kref_get(&ftdi->kref);
Tony Olecha5c66e42006-09-13 11:26:04 +0100273}
274
275static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
276{
Joe Perches8dae6932014-04-04 15:16:04 -0700277 if (cancel_delayed_work(&ftdi->respond_work))
278 kref_put(&ftdi->kref, ftdi_elan_delete);
Tony Olecha5c66e42006-09-13 11:26:04 +0100279}
280
281void ftdi_elan_gone_away(struct platform_device *pdev)
282{
Joe Perches8dae6932014-04-04 15:16:04 -0700283 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
284 ftdi->gone_away += 1;
285 ftdi_elan_put_kref(ftdi);
Tony Olecha5c66e42006-09-13 11:26:04 +0100286}
287
288
289EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
Adrian Bunk9ce85402006-11-20 03:24:44 +0100290static void ftdi_release_platform_dev(struct device *dev)
Tony Olecha5c66e42006-09-13 11:26:04 +0100291{
Joe Perches8dae6932014-04-04 15:16:04 -0700292 dev->parent = NULL;
Tony Olecha5c66e42006-09-13 11:26:04 +0100293}
294
295static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
Joe Perches8dae6932014-04-04 15:16:04 -0700296 struct u132_target *target, u8 *buffer, int length);
Tony Olecha5c66e42006-09-13 11:26:04 +0100297static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
298static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
299static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
300static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
301static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
302static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
303static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
304static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
305static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
306static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
307{
Joe Perches8dae6932014-04-04 15:16:04 -0700308 int result;
309 if (ftdi->platform_dev.dev.parent)
310 return -EBUSY;
311 ftdi_elan_get_kref(ftdi);
312 ftdi->platform_data.potpg = 100;
313 ftdi->platform_data.reset = NULL;
314 ftdi->platform_dev.id = ftdi->sequence_num;
315 ftdi->platform_dev.resource = ftdi->resources;
316 ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
317 ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
318 ftdi->platform_dev.dev.parent = NULL;
319 ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
320 ftdi->platform_dev.dev.dma_mask = NULL;
321 snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
322 ftdi->platform_dev.name = ftdi->device_name;
323 dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
324 request_module("u132_hcd");
325 dev_info(&ftdi->udev->dev, "registering '%s'\n",
326 ftdi->platform_dev.name);
327 result = platform_device_register(&ftdi->platform_dev);
328 return result;
Tony Olecha5c66e42006-09-13 11:26:04 +0100329}
330
331static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
332{
Joe Perches8dae6932014-04-04 15:16:04 -0700333 mutex_lock(&ftdi->u132_lock);
334 while (ftdi->respond_next > ftdi->respond_head) {
335 struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
336 ftdi->respond_head++];
337 *respond->result = -ESHUTDOWN;
338 *respond->value = 0;
339 complete(&respond->wait_completion);
340 } mutex_unlock(&ftdi->u132_lock);
Tony Olecha5c66e42006-09-13 11:26:04 +0100341}
342
343static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
344{
Joe Perches8dae6932014-04-04 15:16:04 -0700345 int ed_number = 4;
346 mutex_lock(&ftdi->u132_lock);
347 while (ed_number-- > 0) {
348 struct u132_target *target = &ftdi->target[ed_number];
349 if (target->active == 1) {
350 target->condition_code = TD_DEVNOTRESP;
351 mutex_unlock(&ftdi->u132_lock);
352 ftdi_elan_do_callback(ftdi, target, NULL, 0);
353 mutex_lock(&ftdi->u132_lock);
354 }
355 }
356 ftdi->received = 0;
357 ftdi->expected = 4;
358 ftdi->ed_found = 0;
359 mutex_unlock(&ftdi->u132_lock);
Tony Olecha5c66e42006-09-13 11:26:04 +0100360}
361
362static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
363{
Joe Perches8dae6932014-04-04 15:16:04 -0700364 int ed_number = 4;
365 mutex_lock(&ftdi->u132_lock);
366 while (ed_number-- > 0) {
367 struct u132_target *target = &ftdi->target[ed_number];
368 target->abandoning = 1;
369 wait_1:if (target->active == 1) {
370 int command_size = ftdi->command_next -
371 ftdi->command_head;
372 if (command_size < COMMAND_SIZE) {
373 struct u132_command *command = &ftdi->command[
374 COMMAND_MASK & ftdi->command_next];
375 command->header = 0x80 | (ed_number << 5) | 0x4;
376 command->length = 0x00;
377 command->address = 0x00;
378 command->width = 0x00;
379 command->follows = 0;
380 command->value = 0;
381 command->buffer = &command->value;
382 ftdi->command_next += 1;
383 ftdi_elan_kick_command_queue(ftdi);
384 } else {
385 mutex_unlock(&ftdi->u132_lock);
386 msleep(100);
387 mutex_lock(&ftdi->u132_lock);
388 goto wait_1;
389 }
390 }
391 wait_2:if (target->active == 1) {
392 int command_size = ftdi->command_next -
393 ftdi->command_head;
394 if (command_size < COMMAND_SIZE) {
395 struct u132_command *command = &ftdi->command[
396 COMMAND_MASK & ftdi->command_next];
397 command->header = 0x90 | (ed_number << 5);
398 command->length = 0x00;
399 command->address = 0x00;
400 command->width = 0x00;
401 command->follows = 0;
402 command->value = 0;
403 command->buffer = &command->value;
404 ftdi->command_next += 1;
405 ftdi_elan_kick_command_queue(ftdi);
406 } else {
407 mutex_unlock(&ftdi->u132_lock);
408 msleep(100);
409 mutex_lock(&ftdi->u132_lock);
410 goto wait_2;
411 }
412 }
413 }
414 ftdi->received = 0;
415 ftdi->expected = 4;
416 ftdi->ed_found = 0;
417 mutex_unlock(&ftdi->u132_lock);
Tony Olecha5c66e42006-09-13 11:26:04 +0100418}
419
420static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
421{
Joe Perches8dae6932014-04-04 15:16:04 -0700422 int ed_number = 4;
423 mutex_lock(&ftdi->u132_lock);
424 while (ed_number-- > 0) {
425 struct u132_target *target = &ftdi->target[ed_number];
426 target->abandoning = 1;
427 wait:if (target->active == 1) {
428 int command_size = ftdi->command_next -
429 ftdi->command_head;
430 if (command_size < COMMAND_SIZE) {
431 struct u132_command *command = &ftdi->command[
432 COMMAND_MASK & ftdi->command_next];
433 command->header = 0x80 | (ed_number << 5) | 0x4;
434 command->length = 0x00;
435 command->address = 0x00;
436 command->width = 0x00;
437 command->follows = 0;
438 command->value = 0;
439 command->buffer = &command->value;
440 ftdi->command_next += 1;
441 ftdi_elan_kick_command_queue(ftdi);
442 } else {
443 mutex_unlock(&ftdi->u132_lock);
444 msleep(100);
445 mutex_lock(&ftdi->u132_lock);
446 goto wait;
447 }
448 }
449 }
450 ftdi->received = 0;
451 ftdi->expected = 4;
452 ftdi->ed_found = 0;
453 mutex_unlock(&ftdi->u132_lock);
Tony Olecha5c66e42006-09-13 11:26:04 +0100454}
455
456static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
457{
Joe Perches8dae6932014-04-04 15:16:04 -0700458 ftdi_command_queue_work(ftdi, 0);
Tony Olecha5c66e42006-09-13 11:26:04 +0100459}
460
David Howellsc4028952006-11-22 14:57:56 +0000461static void ftdi_elan_command_work(struct work_struct *work)
Tony Olecha5c66e42006-09-13 11:26:04 +0100462{
Joe Perches8dae6932014-04-04 15:16:04 -0700463 struct usb_ftdi *ftdi =
David Howellsc4028952006-11-22 14:57:56 +0000464 container_of(work, struct usb_ftdi, command_work.work);
465
Joe Perches8dae6932014-04-04 15:16:04 -0700466 if (ftdi->disconnected > 0) {
467 ftdi_elan_put_kref(ftdi);
468 return;
469 } else {
470 int retval = ftdi_elan_command_engine(ftdi);
471 if (retval == -ESHUTDOWN) {
472 ftdi->disconnected += 1;
473 } else if (retval == -ENODEV) {
474 ftdi->disconnected += 1;
475 } else if (retval)
476 dev_err(&ftdi->udev->dev, "command error %d\n", retval);
477 ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
478 return;
479 }
Tony Olecha5c66e42006-09-13 11:26:04 +0100480}
481
482static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
483{
Joe Perches8dae6932014-04-04 15:16:04 -0700484 ftdi_respond_queue_work(ftdi, 0);
Tony Olecha5c66e42006-09-13 11:26:04 +0100485}
486
David Howellsc4028952006-11-22 14:57:56 +0000487static void ftdi_elan_respond_work(struct work_struct *work)
Tony Olecha5c66e42006-09-13 11:26:04 +0100488{
Joe Perches8dae6932014-04-04 15:16:04 -0700489 struct usb_ftdi *ftdi =
David Howellsc4028952006-11-22 14:57:56 +0000490 container_of(work, struct usb_ftdi, respond_work.work);
Joe Perches8dae6932014-04-04 15:16:04 -0700491 if (ftdi->disconnected > 0) {
492 ftdi_elan_put_kref(ftdi);
493 return;
494 } else {
495 int retval = ftdi_elan_respond_engine(ftdi);
496 if (retval == 0) {
497 } else if (retval == -ESHUTDOWN) {
498 ftdi->disconnected += 1;
499 } else if (retval == -ENODEV) {
500 ftdi->disconnected += 1;
501 } else if (retval == -EILSEQ) {
502 ftdi->disconnected += 1;
503 } else {
504 ftdi->disconnected += 1;
505 dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
506 }
507 if (ftdi->disconnected > 0) {
508 ftdi_elan_abandon_completions(ftdi);
509 ftdi_elan_abandon_targets(ftdi);
510 }
511 ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
512 return;
513 }
Tony Olecha5c66e42006-09-13 11:26:04 +0100514}
515
516
517/*
Joe Perches8dae6932014-04-04 15:16:04 -0700518 * the sw_lock is initially held and will be freed
519 * after the FTDI has been synchronized
520 *
521 */
David Howellsc4028952006-11-22 14:57:56 +0000522static void ftdi_elan_status_work(struct work_struct *work)
Tony Olecha5c66e42006-09-13 11:26:04 +0100523{
Joe Perches8dae6932014-04-04 15:16:04 -0700524 struct usb_ftdi *ftdi =
David Howellsc4028952006-11-22 14:57:56 +0000525 container_of(work, struct usb_ftdi, status_work.work);
Joe Perches8dae6932014-04-04 15:16:04 -0700526 int work_delay_in_msec = 0;
527 if (ftdi->disconnected > 0) {
528 ftdi_elan_put_kref(ftdi);
529 return;
530 } else if (ftdi->synchronized == 0) {
531 down(&ftdi->sw_lock);
532 if (ftdi_elan_synchronize(ftdi) == 0) {
533 ftdi->synchronized = 1;
534 ftdi_command_queue_work(ftdi, 1);
535 ftdi_respond_queue_work(ftdi, 1);
536 up(&ftdi->sw_lock);
537 work_delay_in_msec = 100;
538 } else {
539 dev_err(&ftdi->udev->dev, "synchronize failed\n");
540 up(&ftdi->sw_lock);
541 work_delay_in_msec = 10 *1000;
542 }
543 } else if (ftdi->stuck_status > 0) {
544 if (ftdi_elan_stuck_waiting(ftdi) == 0) {
545 ftdi->stuck_status = 0;
546 ftdi->synchronized = 0;
547 } else if ((ftdi->stuck_status++ % 60) == 1) {
Joe Perches5acc6e42014-04-04 15:16:05 -0700548 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
Joe Perches8dae6932014-04-04 15:16:04 -0700549 } else
Joe Perches5acc6e42014-04-04 15:16:05 -0700550 dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
551 ftdi->stuck_status);
Joe Perches8dae6932014-04-04 15:16:04 -0700552 work_delay_in_msec = 100;
553 } else if (ftdi->enumerated == 0) {
554 if (ftdi_elan_enumeratePCI(ftdi) == 0) {
555 ftdi->enumerated = 1;
556 work_delay_in_msec = 250;
557 } else
558 work_delay_in_msec = 1000;
559 } else if (ftdi->initialized == 0) {
560 if (ftdi_elan_setupOHCI(ftdi) == 0) {
561 ftdi->initialized = 1;
562 work_delay_in_msec = 500;
563 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -0700564 dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
Joe Perches8dae6932014-04-04 15:16:04 -0700565 work_delay_in_msec = 1 *1000;
566 }
567 } else if (ftdi->registered == 0) {
568 work_delay_in_msec = 10;
569 if (ftdi_elan_hcd_init(ftdi) == 0) {
570 ftdi->registered = 1;
571 } else
572 dev_err(&ftdi->udev->dev, "register failed\n");
573 work_delay_in_msec = 250;
574 } else {
575 if (ftdi_elan_checkingPCI(ftdi) == 0) {
576 work_delay_in_msec = 250;
577 } else if (ftdi->controlreg & 0x00400000) {
578 if (ftdi->gone_away > 0) {
Joe Perches5acc6e42014-04-04 15:16:05 -0700579 dev_err(&ftdi->udev->dev, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
Joe Perches8dae6932014-04-04 15:16:04 -0700580 ftdi->platform_dev.dev.parent,
581 &ftdi->platform_dev.dev);
582 platform_device_unregister(&ftdi->platform_dev);
583 ftdi->platform_dev.dev.parent = NULL;
584 ftdi->registered = 0;
585 ftdi->enumerated = 0;
586 ftdi->card_ejected = 0;
587 ftdi->initialized = 0;
588 ftdi->gone_away = 0;
589 } else
590 ftdi_elan_flush_targets(ftdi);
591 work_delay_in_msec = 250;
592 } else {
593 dev_err(&ftdi->udev->dev, "PCI device has disappeared\n"
594 );
595 ftdi_elan_cancel_targets(ftdi);
596 work_delay_in_msec = 500;
597 ftdi->enumerated = 0;
598 ftdi->initialized = 0;
599 }
600 }
601 if (ftdi->disconnected > 0) {
602 ftdi_elan_put_kref(ftdi);
603 return;
604 } else {
605 ftdi_status_requeue_work(ftdi,
606 msecs_to_jiffies(work_delay_in_msec));
607 return;
608 }
Tony Olecha5c66e42006-09-13 11:26:04 +0100609}
610
611
612/*
Joe Perches8dae6932014-04-04 15:16:04 -0700613 * file_operations for the jtag interface
614 *
615 * the usage count for the device is incremented on open()
616 * and decremented on release()
617 */
Tony Olecha5c66e42006-09-13 11:26:04 +0100618static int ftdi_elan_open(struct inode *inode, struct file *file)
619{
Oliver Neukum86266452010-01-13 15:33:15 +0100620 int subminor;
621 struct usb_interface *interface;
622
Joe Perches8dae6932014-04-04 15:16:04 -0700623 subminor = iminor(inode);
624 interface = usb_find_interface(&ftdi_elan_driver, subminor);
Oliver Neukum86266452010-01-13 15:33:15 +0100625
Joe Perches8dae6932014-04-04 15:16:04 -0700626 if (!interface) {
627 printk(KERN_ERR "can't find device for minor %d\n", subminor);
628 return -ENODEV;
629 } else {
630 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
631 if (!ftdi) {
632 return -ENODEV;
633 } else {
634 if (down_interruptible(&ftdi->sw_lock)) {
635 return -EINTR;
636 } else {
637 ftdi_elan_get_kref(ftdi);
638 file->private_data = ftdi;
639 return 0;
640 }
641 }
642 }
Tony Olecha5c66e42006-09-13 11:26:04 +0100643}
644
645static int ftdi_elan_release(struct inode *inode, struct file *file)
646{
Joe Perches8dae6932014-04-04 15:16:04 -0700647 struct usb_ftdi *ftdi = file->private_data;
648 if (ftdi == NULL)
649 return -ENODEV;
650 up(&ftdi->sw_lock); /* decrement the count on our device */
651 ftdi_elan_put_kref(ftdi);
652 return 0;
Tony Olecha5c66e42006-09-13 11:26:04 +0100653}
654
655
Tony Olecha5c66e42006-09-13 11:26:04 +0100656/*
Joe Perches8dae6932014-04-04 15:16:04 -0700657 *
658 * blocking bulk reads are used to get data from the device
659 *
660 */
Tony Olecha5c66e42006-09-13 11:26:04 +0100661static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
662 size_t count, loff_t *ppos)
663{
Joe Perches8dae6932014-04-04 15:16:04 -0700664 char data[30 *3 + 4];
665 char *d = data;
666 int m = (sizeof(data) - 1) / 3;
667 int bytes_read = 0;
668 int retry_on_empty = 10;
669 int retry_on_timeout = 5;
670 struct usb_ftdi *ftdi = file->private_data;
671 if (ftdi->disconnected > 0) {
672 return -ENODEV;
673 }
674 data[0] = 0;
675have:if (ftdi->bulk_in_left > 0) {
676 if (count-- > 0) {
677 char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
678 ftdi->bulk_in_left -= 1;
679 if (bytes_read < m) {
680 d += sprintf(d, " %02X", 0x000000FF & *p);
681 } else if (bytes_read > m) {
682 } else
683 d += sprintf(d, " ..");
684 if (copy_to_user(buffer++, p, 1)) {
685 return -EFAULT;
686 } else {
687 bytes_read += 1;
688 goto have;
689 }
690 } else
691 return bytes_read;
692 }
693more:if (count > 0) {
694 int packet_bytes = 0;
695 int retval = usb_bulk_msg(ftdi->udev,
696 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
697 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
698 &packet_bytes, 50);
699 if (packet_bytes > 2) {
700 ftdi->bulk_in_left = packet_bytes - 2;
701 ftdi->bulk_in_last = 1;
702 goto have;
703 } else if (retval == -ETIMEDOUT) {
704 if (retry_on_timeout-- > 0) {
705 goto more;
706 } else if (bytes_read > 0) {
707 return bytes_read;
708 } else
709 return retval;
710 } else if (retval == 0) {
711 if (retry_on_empty-- > 0) {
712 goto more;
713 } else
714 return bytes_read;
715 } else
716 return retval;
717 } else
718 return bytes_read;
Tony Olecha5c66e42006-09-13 11:26:04 +0100719}
720
David Howells7d12e782006-10-05 14:55:46 +0100721static void ftdi_elan_write_bulk_callback(struct urb *urb)
Tony Olecha5c66e42006-09-13 11:26:04 +0100722{
Ming Leicdc97792008-02-24 18:41:47 +0800723 struct usb_ftdi *ftdi = urb->context;
Greg Kroah-Hartman84346262007-07-18 10:58:02 -0700724 int status = urb->status;
725
726 if (status && !(status == -ENOENT || status == -ECONNRESET ||
Joe Perches8dae6932014-04-04 15:16:04 -0700727 status == -ESHUTDOWN)) {
Joe Perches90ba4f72014-04-04 15:16:03 -0700728 dev_err(&ftdi->udev->dev,
729 "urb=%p write bulk status received: %d\n", urb, status);
Joe Perches8dae6932014-04-04 15:16:04 -0700730 }
731 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
732 urb->transfer_buffer, urb->transfer_dma);
Tony Olecha5c66e42006-09-13 11:26:04 +0100733}
734
735static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
Joe Perches8dae6932014-04-04 15:16:04 -0700736 char *buf, int command_size, int total_size)
Tony Olecha5c66e42006-09-13 11:26:04 +0100737{
Joe Perches8dae6932014-04-04 15:16:04 -0700738 int ed_commands = 0;
739 int b = 0;
740 int I = command_size;
741 int i = ftdi->command_head;
742 while (I-- > 0) {
743 struct u132_command *command = &ftdi->command[COMMAND_MASK &
744 i++];
745 int F = command->follows;
746 u8 *f = command->buffer;
747 if (command->header & 0x80) {
748 ed_commands |= 1 << (0x3 & (command->header >> 5));
749 }
750 buf[b++] = command->header;
751 buf[b++] = (command->length >> 0) & 0x00FF;
752 buf[b++] = (command->length >> 8) & 0x00FF;
753 buf[b++] = command->address;
754 buf[b++] = command->width;
755 while (F-- > 0) {
756 buf[b++] = *f++;
757 }
758 }
759 return ed_commands;
Tony Olecha5c66e42006-09-13 11:26:04 +0100760}
761
762static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
763{
Joe Perches8dae6932014-04-04 15:16:04 -0700764 int total_size = 0;
765 int I = command_size;
766 int i = ftdi->command_head;
767 while (I-- > 0) {
768 struct u132_command *command = &ftdi->command[COMMAND_MASK &
769 i++];
770 total_size += 5 + command->follows;
771 } return total_size;
Tony Olecha5c66e42006-09-13 11:26:04 +0100772}
773
774static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
775{
Joe Perches8dae6932014-04-04 15:16:04 -0700776 int retval;
777 char *buf;
778 int ed_commands;
779 int total_size;
780 struct urb *urb;
781 int command_size = ftdi->command_next - ftdi->command_head;
782 if (command_size == 0)
783 return 0;
784 total_size = ftdi_elan_total_command_size(ftdi, command_size);
785 urb = usb_alloc_urb(0, GFP_KERNEL);
786 if (!urb) {
Joe Perches5acc6e42014-04-04 15:16:05 -0700787 dev_err(&ftdi->udev->dev, "could not get a urb to write %d commands totaling %d bytes to the Uxxx\n",
788 command_size, total_size);
Joe Perches8dae6932014-04-04 15:16:04 -0700789 return -ENOMEM;
790 }
791 buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
792 &urb->transfer_dma);
793 if (!buf) {
Joe Perches5acc6e42014-04-04 15:16:05 -0700794 dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
795 command_size, total_size);
Joe Perches8dae6932014-04-04 15:16:04 -0700796 usb_free_urb(urb);
797 return -ENOMEM;
798 }
799 ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
800 command_size, total_size);
801 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
802 ftdi->bulk_out_endpointAddr), buf, total_size,
803 ftdi_elan_write_bulk_callback, ftdi);
804 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
805 if (ed_commands) {
806 char diag[40 *3 + 4];
807 char *d = diag;
808 int m = total_size;
809 u8 *c = buf;
810 int s = (sizeof(diag) - 1) / 3;
811 diag[0] = 0;
812 while (s-- > 0 && m-- > 0) {
813 if (s > 0 || m == 0) {
814 d += sprintf(d, " %02X", *c++);
815 } else
816 d += sprintf(d, " ..");
817 }
818 }
819 retval = usb_submit_urb(urb, GFP_KERNEL);
820 if (retval) {
Joe Perches5acc6e42014-04-04 15:16:05 -0700821 dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
822 retval, urb, command_size, total_size);
Joe Perches8dae6932014-04-04 15:16:04 -0700823 usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
824 usb_free_urb(urb);
825 return retval;
826 }
827 usb_free_urb(urb); /* release our reference to this urb,
828 the USB core will eventually free it entirely */
829 ftdi->command_head += command_size;
830 ftdi_elan_kick_respond_queue(ftdi);
831 return 0;
Tony Olecha5c66e42006-09-13 11:26:04 +0100832}
833
834static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
Joe Perches8dae6932014-04-04 15:16:04 -0700835 struct u132_target *target, u8 *buffer, int length)
Tony Olecha5c66e42006-09-13 11:26:04 +0100836{
Joe Perches8dae6932014-04-04 15:16:04 -0700837 struct urb *urb = target->urb;
838 int halted = target->halted;
839 int skipped = target->skipped;
840 int actual = target->actual;
841 int non_null = target->non_null;
842 int toggle_bits = target->toggle_bits;
843 int error_count = target->error_count;
844 int condition_code = target->condition_code;
845 int repeat_number = target->repeat_number;
846 void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
847 int, int, int, int) = target->callback;
848 target->active -= 1;
849 target->callback = NULL;
850 (*callback) (target->endp, urb, buffer, length, toggle_bits,
851 error_count, condition_code, repeat_number, halted, skipped,
852 actual, non_null);
Tony Olecha5c66e42006-09-13 11:26:04 +0100853}
854
855static char *have_ed_set_response(struct usb_ftdi *ftdi,
Joe Perches8dae6932014-04-04 15:16:04 -0700856 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
857 char *b)
Tony Olecha5c66e42006-09-13 11:26:04 +0100858{
Joe Perches8dae6932014-04-04 15:16:04 -0700859 int payload = (ed_length >> 0) & 0x07FF;
860 mutex_lock(&ftdi->u132_lock);
861 target->actual = 0;
862 target->non_null = (ed_length >> 15) & 0x0001;
863 target->repeat_number = (ed_length >> 11) & 0x000F;
864 if (ed_type == 0x02) {
865 if (payload == 0 || target->abandoning > 0) {
866 target->abandoning = 0;
867 mutex_unlock(&ftdi->u132_lock);
868 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
869 payload);
870 ftdi->received = 0;
871 ftdi->expected = 4;
872 ftdi->ed_found = 0;
873 return ftdi->response;
874 } else {
875 ftdi->expected = 4 + payload;
876 ftdi->ed_found = 1;
877 mutex_unlock(&ftdi->u132_lock);
878 return b;
879 }
880 } else if (ed_type == 0x03) {
881 if (payload == 0 || target->abandoning > 0) {
882 target->abandoning = 0;
883 mutex_unlock(&ftdi->u132_lock);
884 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
885 payload);
886 ftdi->received = 0;
887 ftdi->expected = 4;
888 ftdi->ed_found = 0;
889 return ftdi->response;
890 } else {
891 ftdi->expected = 4 + payload;
892 ftdi->ed_found = 1;
893 mutex_unlock(&ftdi->u132_lock);
894 return b;
895 }
896 } else if (ed_type == 0x01) {
897 target->abandoning = 0;
898 mutex_unlock(&ftdi->u132_lock);
899 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
900 payload);
901 ftdi->received = 0;
902 ftdi->expected = 4;
903 ftdi->ed_found = 0;
904 return ftdi->response;
905 } else {
906 target->abandoning = 0;
907 mutex_unlock(&ftdi->u132_lock);
908 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
909 payload);
910 ftdi->received = 0;
911 ftdi->expected = 4;
912 ftdi->ed_found = 0;
913 return ftdi->response;
914 }
Tony Olecha5c66e42006-09-13 11:26:04 +0100915}
916
917static char *have_ed_get_response(struct usb_ftdi *ftdi,
Joe Perches8dae6932014-04-04 15:16:04 -0700918 struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
919 char *b)
Tony Olecha5c66e42006-09-13 11:26:04 +0100920{
Joe Perches8dae6932014-04-04 15:16:04 -0700921 mutex_lock(&ftdi->u132_lock);
922 target->condition_code = TD_DEVNOTRESP;
923 target->actual = (ed_length >> 0) & 0x01FF;
924 target->non_null = (ed_length >> 15) & 0x0001;
925 target->repeat_number = (ed_length >> 11) & 0x000F;
926 mutex_unlock(&ftdi->u132_lock);
927 if (target->active)
928 ftdi_elan_do_callback(ftdi, target, NULL, 0);
929 target->abandoning = 0;
930 ftdi->received = 0;
931 ftdi->expected = 4;
932 ftdi->ed_found = 0;
933 return ftdi->response;
Tony Olecha5c66e42006-09-13 11:26:04 +0100934}
935
936
937/*
Joe Perches8dae6932014-04-04 15:16:04 -0700938 * The engine tries to empty the FTDI fifo
939 *
940 * all responses found in the fifo data are dispatched thus
941 * the response buffer can only ever hold a maximum sized
942 * response from the Uxxx.
943 *
944 */
Tony Olecha5c66e42006-09-13 11:26:04 +0100945static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
946{
Joe Perches8dae6932014-04-04 15:16:04 -0700947 u8 *b = ftdi->response + ftdi->received;
948 int bytes_read = 0;
949 int retry_on_empty = 1;
950 int retry_on_timeout = 3;
951 int empty_packets = 0;
952read:{
953 int packet_bytes = 0;
954 int retval = usb_bulk_msg(ftdi->udev,
955 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
956 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
957 &packet_bytes, 500);
958 char diag[30 *3 + 4];
959 char *d = diag;
960 int m = packet_bytes;
961 u8 *c = ftdi->bulk_in_buffer;
962 int s = (sizeof(diag) - 1) / 3;
963 diag[0] = 0;
964 while (s-- > 0 && m-- > 0) {
965 if (s > 0 || m == 0) {
966 d += sprintf(d, " %02X", *c++);
967 } else
968 d += sprintf(d, " ..");
969 }
970 if (packet_bytes > 2) {
971 ftdi->bulk_in_left = packet_bytes - 2;
972 ftdi->bulk_in_last = 1;
973 goto have;
974 } else if (retval == -ETIMEDOUT) {
975 if (retry_on_timeout-- > 0) {
Joe Perches5acc6e42014-04-04 15:16:05 -0700976 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
Joe Perches8dae6932014-04-04 15:16:04 -0700977 packet_bytes, bytes_read, diag);
978 goto more;
979 } else if (bytes_read > 0) {
980 dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
981 bytes_read, diag);
982 return -ENOMEM;
983 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -0700984 dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
Joe Perches8dae6932014-04-04 15:16:04 -0700985 packet_bytes, bytes_read, diag);
986 return -ENOMEM;
987 }
988 } else if (retval == -EILSEQ) {
Joe Perches5acc6e42014-04-04 15:16:05 -0700989 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
990 retval, packet_bytes, bytes_read, diag);
Joe Perches8dae6932014-04-04 15:16:04 -0700991 return retval;
992 } else if (retval) {
Joe Perches5acc6e42014-04-04 15:16:05 -0700993 dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
994 retval, packet_bytes, bytes_read, diag);
Joe Perches8dae6932014-04-04 15:16:04 -0700995 return retval;
996 } else if (packet_bytes == 2) {
997 unsigned char s0 = ftdi->bulk_in_buffer[0];
998 unsigned char s1 = ftdi->bulk_in_buffer[1];
999 empty_packets += 1;
1000 if (s0 == 0x31 && s1 == 0x60) {
1001 if (retry_on_empty-- > 0) {
1002 goto more;
1003 } else
1004 return 0;
1005 } else if (s0 == 0x31 && s1 == 0x00) {
1006 if (retry_on_empty-- > 0) {
1007 goto more;
1008 } else
1009 return 0;
1010 } else {
1011 if (retry_on_empty-- > 0) {
1012 goto more;
1013 } else
1014 return 0;
1015 }
1016 } else if (packet_bytes == 1) {
1017 if (retry_on_empty-- > 0) {
1018 goto more;
1019 } else
1020 return 0;
1021 } else {
1022 if (retry_on_empty-- > 0) {
1023 goto more;
1024 } else
1025 return 0;
1026 }
1027 }
1028more:{
1029 goto read;
1030 }
1031have:if (ftdi->bulk_in_left > 0) {
1032 u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
1033 bytes_read += 1;
1034 ftdi->bulk_in_left -= 1;
1035 if (ftdi->received == 0 && c == 0xFF) {
1036 goto have;
1037 } else
1038 *b++ = c;
1039 if (++ftdi->received < ftdi->expected) {
1040 goto have;
1041 } else if (ftdi->ed_found) {
1042 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1043 u16 ed_length = (ftdi->response[2] << 8) |
1044 ftdi->response[1];
1045 struct u132_target *target = &ftdi->target[ed_number];
1046 int payload = (ed_length >> 0) & 0x07FF;
1047 char diag[30 *3 + 4];
1048 char *d = diag;
1049 int m = payload;
1050 u8 *c = 4 + ftdi->response;
1051 int s = (sizeof(diag) - 1) / 3;
1052 diag[0] = 0;
1053 while (s-- > 0 && m-- > 0) {
1054 if (s > 0 || m == 0) {
1055 d += sprintf(d, " %02X", *c++);
1056 } else
1057 d += sprintf(d, " ..");
1058 }
1059 ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
1060 payload);
1061 ftdi->received = 0;
1062 ftdi->expected = 4;
1063 ftdi->ed_found = 0;
1064 b = ftdi->response;
1065 goto have;
1066 } else if (ftdi->expected == 8) {
1067 u8 buscmd;
1068 int respond_head = ftdi->respond_head++;
1069 struct u132_respond *respond = &ftdi->respond[
1070 RESPOND_MASK & respond_head];
1071 u32 data = ftdi->response[7];
1072 data <<= 8;
1073 data |= ftdi->response[6];
1074 data <<= 8;
1075 data |= ftdi->response[5];
1076 data <<= 8;
1077 data |= ftdi->response[4];
1078 *respond->value = data;
1079 *respond->result = 0;
1080 complete(&respond->wait_completion);
1081 ftdi->received = 0;
1082 ftdi->expected = 4;
1083 ftdi->ed_found = 0;
1084 b = ftdi->response;
1085 buscmd = (ftdi->response[0] >> 0) & 0x0F;
1086 if (buscmd == 0x00) {
1087 } else if (buscmd == 0x02) {
1088 } else if (buscmd == 0x06) {
1089 } else if (buscmd == 0x0A) {
1090 } else
Joe Perches5acc6e42014-04-04 15:16:05 -07001091 dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
1092 buscmd, data);
Joe Perches8dae6932014-04-04 15:16:04 -07001093 goto have;
1094 } else {
1095 if ((ftdi->response[0] & 0x80) == 0x00) {
1096 ftdi->expected = 8;
1097 goto have;
1098 } else {
1099 int ed_number = (ftdi->response[0] >> 5) & 0x03;
1100 int ed_type = (ftdi->response[0] >> 0) & 0x03;
1101 u16 ed_length = (ftdi->response[2] << 8) |
1102 ftdi->response[1];
1103 struct u132_target *target = &ftdi->target[
1104 ed_number];
1105 target->halted = (ftdi->response[0] >> 3) &
1106 0x01;
1107 target->skipped = (ftdi->response[0] >> 2) &
1108 0x01;
1109 target->toggle_bits = (ftdi->response[3] >> 6)
1110 & 0x03;
1111 target->error_count = (ftdi->response[3] >> 4)
1112 & 0x03;
1113 target->condition_code = (ftdi->response[
1114 3] >> 0) & 0x0F;
1115 if ((ftdi->response[0] & 0x10) == 0x00) {
1116 b = have_ed_set_response(ftdi, target,
1117 ed_length, ed_number, ed_type,
1118 b);
1119 goto have;
1120 } else {
1121 b = have_ed_get_response(ftdi, target,
1122 ed_length, ed_number, ed_type,
1123 b);
1124 goto have;
1125 }
1126 }
1127 }
1128 } else
1129 goto more;
Tony Olecha5c66e42006-09-13 11:26:04 +01001130}
1131
1132
1133/*
Joe Perches8dae6932014-04-04 15:16:04 -07001134 * create a urb, and a buffer for it, and copy the data to the urb
1135 *
1136 */
Tony Olecha5c66e42006-09-13 11:26:04 +01001137static ssize_t ftdi_elan_write(struct file *file,
1138 const char __user *user_buffer, size_t count,
1139 loff_t *ppos)
1140{
Joe Perches8dae6932014-04-04 15:16:04 -07001141 int retval = 0;
1142 struct urb *urb;
1143 char *buf;
1144 struct usb_ftdi *ftdi = file->private_data;
Greg Kroah-Hartman96a51892006-10-09 12:24:49 -07001145
Joe Perches8dae6932014-04-04 15:16:04 -07001146 if (ftdi->disconnected > 0) {
1147 return -ENODEV;
1148 }
1149 if (count == 0) {
1150 goto exit;
1151 }
1152 urb = usb_alloc_urb(0, GFP_KERNEL);
1153 if (!urb) {
1154 retval = -ENOMEM;
1155 goto error_1;
1156 }
1157 buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
1158 &urb->transfer_dma);
1159 if (!buf) {
1160 retval = -ENOMEM;
1161 goto error_2;
1162 }
1163 if (copy_from_user(buf, user_buffer, count)) {
1164 retval = -EFAULT;
1165 goto error_3;
1166 }
1167 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1168 ftdi->bulk_out_endpointAddr), buf, count,
1169 ftdi_elan_write_bulk_callback, ftdi);
1170 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1171 retval = usb_submit_urb(urb, GFP_KERNEL);
1172 if (retval) {
Joe Perches90ba4f72014-04-04 15:16:03 -07001173 dev_err(&ftdi->udev->dev,
1174 "failed submitting write urb, error %d\n", retval);
Joe Perches8dae6932014-04-04 15:16:04 -07001175 goto error_3;
1176 }
1177 usb_free_urb(urb);
Greg Kroah-Hartman96a51892006-10-09 12:24:49 -07001178
1179exit:
Joe Perches8dae6932014-04-04 15:16:04 -07001180 return count;
Greg Kroah-Hartman96a51892006-10-09 12:24:49 -07001181error_3:
Daniel Mack997ea582010-04-12 13:17:25 +02001182 usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
Greg Kroah-Hartman96a51892006-10-09 12:24:49 -07001183error_2:
1184 usb_free_urb(urb);
1185error_1:
1186 return retval;
Tony Olecha5c66e42006-09-13 11:26:04 +01001187}
1188
Arjan van de Ven00977a52007-02-12 00:55:34 -08001189static const struct file_operations ftdi_elan_fops = {
Joe Perches8dae6932014-04-04 15:16:04 -07001190 .owner = THIS_MODULE,
1191 .llseek = no_llseek,
1192 .read = ftdi_elan_read,
1193 .write = ftdi_elan_write,
1194 .open = ftdi_elan_open,
1195 .release = ftdi_elan_release,
Tony Olecha5c66e42006-09-13 11:26:04 +01001196};
1197
1198/*
Joe Perches8dae6932014-04-04 15:16:04 -07001199 * usb class driver info in order to get a minor number from the usb core,
1200 * and to have the device registered with the driver core
1201 */
Tony Olecha5c66e42006-09-13 11:26:04 +01001202static struct usb_class_driver ftdi_elan_jtag_class = {
Joe Perches8dae6932014-04-04 15:16:04 -07001203 .name = "ftdi-%d-jtag",
1204 .fops = &ftdi_elan_fops,
1205 .minor_base = USB_FTDI_ELAN_MINOR_BASE,
Tony Olecha5c66e42006-09-13 11:26:04 +01001206};
1207
1208/*
Joe Perches8dae6932014-04-04 15:16:04 -07001209 * the following definitions are for the
1210 * ELAN FPGA state machgine processor that
1211 * lies on the other side of the FTDI chip
1212 */
Tony Olecha5c66e42006-09-13 11:26:04 +01001213#define cPCIu132rd 0x0
1214#define cPCIu132wr 0x1
1215#define cPCIiord 0x2
1216#define cPCIiowr 0x3
1217#define cPCImemrd 0x6
1218#define cPCImemwr 0x7
1219#define cPCIcfgrd 0xA
1220#define cPCIcfgwr 0xB
1221#define cPCInull 0xF
1222#define cU132cmd_status 0x0
1223#define cU132flash 0x1
1224#define cPIDsetup 0x0
1225#define cPIDout 0x1
1226#define cPIDin 0x2
1227#define cPIDinonce 0x3
1228#define cCCnoerror 0x0
1229#define cCCcrc 0x1
1230#define cCCbitstuff 0x2
1231#define cCCtoggle 0x3
1232#define cCCstall 0x4
1233#define cCCnoresp 0x5
1234#define cCCbadpid1 0x6
1235#define cCCbadpid2 0x7
1236#define cCCdataoverrun 0x8
1237#define cCCdataunderrun 0x9
1238#define cCCbuffoverrun 0xC
1239#define cCCbuffunderrun 0xD
1240#define cCCnotaccessed 0xF
1241static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
1242{
Joe Perches8dae6932014-04-04 15:16:04 -07001243wait:if (ftdi->disconnected > 0) {
1244 return -ENODEV;
1245 } else {
1246 int command_size;
1247 mutex_lock(&ftdi->u132_lock);
1248 command_size = ftdi->command_next - ftdi->command_head;
1249 if (command_size < COMMAND_SIZE) {
1250 struct u132_command *command = &ftdi->command[
1251 COMMAND_MASK & ftdi->command_next];
1252 command->header = 0x00 | cPCIu132wr;
1253 command->length = 0x04;
1254 command->address = 0x00;
1255 command->width = 0x00;
1256 command->follows = 4;
1257 command->value = data;
1258 command->buffer = &command->value;
1259 ftdi->command_next += 1;
1260 ftdi_elan_kick_command_queue(ftdi);
1261 mutex_unlock(&ftdi->u132_lock);
1262 return 0;
1263 } else {
1264 mutex_unlock(&ftdi->u132_lock);
1265 msleep(100);
1266 goto wait;
1267 }
1268 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001269}
1270
1271static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
Joe Perches8dae6932014-04-04 15:16:04 -07001272 u8 width, u32 data)
Tony Olecha5c66e42006-09-13 11:26:04 +01001273{
Joe Perches8dae6932014-04-04 15:16:04 -07001274 u8 addressofs = config_offset / 4;
1275wait:if (ftdi->disconnected > 0) {
1276 return -ENODEV;
1277 } else {
1278 int command_size;
1279 mutex_lock(&ftdi->u132_lock);
1280 command_size = ftdi->command_next - ftdi->command_head;
1281 if (command_size < COMMAND_SIZE) {
1282 struct u132_command *command = &ftdi->command[
1283 COMMAND_MASK & ftdi->command_next];
1284 command->header = 0x00 | (cPCIcfgwr & 0x0F);
1285 command->length = 0x04;
1286 command->address = addressofs;
1287 command->width = 0x00 | (width & 0x0F);
1288 command->follows = 4;
1289 command->value = data;
1290 command->buffer = &command->value;
1291 ftdi->command_next += 1;
1292 ftdi_elan_kick_command_queue(ftdi);
1293 mutex_unlock(&ftdi->u132_lock);
1294 return 0;
1295 } else {
1296 mutex_unlock(&ftdi->u132_lock);
1297 msleep(100);
1298 goto wait;
1299 }
1300 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001301}
1302
1303static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
Joe Perches8dae6932014-04-04 15:16:04 -07001304 u8 width, u32 data)
Tony Olecha5c66e42006-09-13 11:26:04 +01001305{
Joe Perches8dae6932014-04-04 15:16:04 -07001306 u8 addressofs = mem_offset / 4;
1307wait:if (ftdi->disconnected > 0) {
1308 return -ENODEV;
1309 } else {
1310 int command_size;
1311 mutex_lock(&ftdi->u132_lock);
1312 command_size = ftdi->command_next - ftdi->command_head;
1313 if (command_size < COMMAND_SIZE) {
1314 struct u132_command *command = &ftdi->command[
1315 COMMAND_MASK & ftdi->command_next];
1316 command->header = 0x00 | (cPCImemwr & 0x0F);
1317 command->length = 0x04;
1318 command->address = addressofs;
1319 command->width = 0x00 | (width & 0x0F);
1320 command->follows = 4;
1321 command->value = data;
1322 command->buffer = &command->value;
1323 ftdi->command_next += 1;
1324 ftdi_elan_kick_command_queue(ftdi);
1325 mutex_unlock(&ftdi->u132_lock);
1326 return 0;
1327 } else {
1328 mutex_unlock(&ftdi->u132_lock);
1329 msleep(100);
1330 goto wait;
1331 }
1332 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001333}
1334
1335int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
Joe Perches8dae6932014-04-04 15:16:04 -07001336 u8 width, u32 data)
Tony Olecha5c66e42006-09-13 11:26:04 +01001337{
Joe Perches8dae6932014-04-04 15:16:04 -07001338 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1339 return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
Tony Olecha5c66e42006-09-13 11:26:04 +01001340}
1341
1342
1343EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
1344static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
1345{
Joe Perches8dae6932014-04-04 15:16:04 -07001346wait:if (ftdi->disconnected > 0) {
1347 return -ENODEV;
1348 } else {
1349 int command_size;
1350 int respond_size;
1351 mutex_lock(&ftdi->u132_lock);
1352 command_size = ftdi->command_next - ftdi->command_head;
1353 respond_size = ftdi->respond_next - ftdi->respond_head;
1354 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1355 {
1356 struct u132_command *command = &ftdi->command[
1357 COMMAND_MASK & ftdi->command_next];
1358 struct u132_respond *respond = &ftdi->respond[
1359 RESPOND_MASK & ftdi->respond_next];
1360 int result = -ENODEV;
1361 respond->result = &result;
1362 respond->header = command->header = 0x00 | cPCIu132rd;
1363 command->length = 0x04;
1364 respond->address = command->address = cU132cmd_status;
1365 command->width = 0x00;
1366 command->follows = 0;
1367 command->value = 0;
1368 command->buffer = NULL;
1369 respond->value = data;
1370 init_completion(&respond->wait_completion);
1371 ftdi->command_next += 1;
1372 ftdi->respond_next += 1;
1373 ftdi_elan_kick_command_queue(ftdi);
1374 mutex_unlock(&ftdi->u132_lock);
1375 wait_for_completion(&respond->wait_completion);
1376 return result;
1377 } else {
1378 mutex_unlock(&ftdi->u132_lock);
1379 msleep(100);
1380 goto wait;
1381 }
1382 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001383}
1384
Tony Olecha5c66e42006-09-13 11:26:04 +01001385static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
Joe Perches8dae6932014-04-04 15:16:04 -07001386 u8 width, u32 *data)
Tony Olecha5c66e42006-09-13 11:26:04 +01001387{
Joe Perches8dae6932014-04-04 15:16:04 -07001388 u8 addressofs = config_offset / 4;
1389wait:if (ftdi->disconnected > 0) {
1390 return -ENODEV;
1391 } else {
1392 int command_size;
1393 int respond_size;
1394 mutex_lock(&ftdi->u132_lock);
1395 command_size = ftdi->command_next - ftdi->command_head;
1396 respond_size = ftdi->respond_next - ftdi->respond_head;
1397 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1398 {
1399 struct u132_command *command = &ftdi->command[
1400 COMMAND_MASK & ftdi->command_next];
1401 struct u132_respond *respond = &ftdi->respond[
1402 RESPOND_MASK & ftdi->respond_next];
1403 int result = -ENODEV;
1404 respond->result = &result;
1405 respond->header = command->header = 0x00 | (cPCIcfgrd &
1406 0x0F);
1407 command->length = 0x04;
1408 respond->address = command->address = addressofs;
1409 command->width = 0x00 | (width & 0x0F);
1410 command->follows = 0;
1411 command->value = 0;
1412 command->buffer = NULL;
1413 respond->value = data;
1414 init_completion(&respond->wait_completion);
1415 ftdi->command_next += 1;
1416 ftdi->respond_next += 1;
1417 ftdi_elan_kick_command_queue(ftdi);
1418 mutex_unlock(&ftdi->u132_lock);
1419 wait_for_completion(&respond->wait_completion);
1420 return result;
1421 } else {
1422 mutex_unlock(&ftdi->u132_lock);
1423 msleep(100);
1424 goto wait;
1425 }
1426 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001427}
1428
1429static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
Joe Perches8dae6932014-04-04 15:16:04 -07001430 u8 width, u32 *data)
Tony Olecha5c66e42006-09-13 11:26:04 +01001431{
Joe Perches8dae6932014-04-04 15:16:04 -07001432 u8 addressofs = mem_offset / 4;
1433wait:if (ftdi->disconnected > 0) {
1434 return -ENODEV;
1435 } else {
1436 int command_size;
1437 int respond_size;
1438 mutex_lock(&ftdi->u132_lock);
1439 command_size = ftdi->command_next - ftdi->command_head;
1440 respond_size = ftdi->respond_next - ftdi->respond_head;
1441 if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
1442 {
1443 struct u132_command *command = &ftdi->command[
1444 COMMAND_MASK & ftdi->command_next];
1445 struct u132_respond *respond = &ftdi->respond[
1446 RESPOND_MASK & ftdi->respond_next];
1447 int result = -ENODEV;
1448 respond->result = &result;
1449 respond->header = command->header = 0x00 | (cPCImemrd &
1450 0x0F);
1451 command->length = 0x04;
1452 respond->address = command->address = addressofs;
1453 command->width = 0x00 | (width & 0x0F);
1454 command->follows = 0;
1455 command->value = 0;
1456 command->buffer = NULL;
1457 respond->value = data;
1458 init_completion(&respond->wait_completion);
1459 ftdi->command_next += 1;
1460 ftdi->respond_next += 1;
1461 ftdi_elan_kick_command_queue(ftdi);
1462 mutex_unlock(&ftdi->u132_lock);
1463 wait_for_completion(&respond->wait_completion);
1464 return result;
1465 } else {
1466 mutex_unlock(&ftdi->u132_lock);
1467 msleep(100);
1468 goto wait;
1469 }
1470 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001471}
1472
1473int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
Joe Perches8dae6932014-04-04 15:16:04 -07001474 u8 width, u32 *data)
Tony Olecha5c66e42006-09-13 11:26:04 +01001475{
Joe Perches8dae6932014-04-04 15:16:04 -07001476 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1477 if (ftdi->initialized == 0) {
1478 return -ENODEV;
1479 } else
1480 return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
Tony Olecha5c66e42006-09-13 11:26:04 +01001481}
1482
1483
1484EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
1485static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001486 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1487 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1488 int toggle_bits, int error_count, int condition_code, int repeat_number,
1489 int halted, int skipped, int actual, int non_null))
Tony Olecha5c66e42006-09-13 11:26:04 +01001490{
Joe Perches8dae6932014-04-04 15:16:04 -07001491 u8 ed = ed_number - 1;
1492wait:if (ftdi->disconnected > 0) {
1493 return -ENODEV;
1494 } else if (ftdi->initialized == 0) {
1495 return -ENODEV;
1496 } else {
1497 int command_size;
1498 mutex_lock(&ftdi->u132_lock);
1499 command_size = ftdi->command_next - ftdi->command_head;
1500 if (command_size < COMMAND_SIZE) {
1501 struct u132_target *target = &ftdi->target[ed];
1502 struct u132_command *command = &ftdi->command[
1503 COMMAND_MASK & ftdi->command_next];
1504 command->header = 0x80 | (ed << 5);
1505 command->length = 0x8007;
1506 command->address = (toggle_bits << 6) | (ep_number << 2)
1507 | (address << 0);
1508 command->width = usb_maxpacket(urb->dev, urb->pipe,
1509 usb_pipeout(urb->pipe));
1510 command->follows = 8;
1511 command->value = 0;
1512 command->buffer = urb->setup_packet;
1513 target->callback = callback;
1514 target->endp = endp;
1515 target->urb = urb;
1516 target->active = 1;
1517 ftdi->command_next += 1;
1518 ftdi_elan_kick_command_queue(ftdi);
1519 mutex_unlock(&ftdi->u132_lock);
1520 return 0;
1521 } else {
1522 mutex_unlock(&ftdi->u132_lock);
1523 msleep(100);
1524 goto wait;
1525 }
1526 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001527}
1528
1529int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001530 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1531 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1532 int toggle_bits, int error_count, int condition_code, int repeat_number,
1533 int halted, int skipped, int actual, int non_null))
Tony Olecha5c66e42006-09-13 11:26:04 +01001534{
Joe Perches8dae6932014-04-04 15:16:04 -07001535 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1536 return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
1537 ep_number, toggle_bits, callback);
Tony Olecha5c66e42006-09-13 11:26:04 +01001538}
1539
1540
1541EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
1542static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001543 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1544 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1545 int toggle_bits, int error_count, int condition_code, int repeat_number,
1546 int halted, int skipped, int actual, int non_null))
Tony Olecha5c66e42006-09-13 11:26:04 +01001547{
Joe Perches8dae6932014-04-04 15:16:04 -07001548 u8 ed = ed_number - 1;
1549wait:if (ftdi->disconnected > 0) {
1550 return -ENODEV;
1551 } else if (ftdi->initialized == 0) {
1552 return -ENODEV;
1553 } else {
1554 int command_size;
1555 mutex_lock(&ftdi->u132_lock);
1556 command_size = ftdi->command_next - ftdi->command_head;
1557 if (command_size < COMMAND_SIZE) {
1558 struct u132_target *target = &ftdi->target[ed];
1559 struct u132_command *command = &ftdi->command[
1560 COMMAND_MASK & ftdi->command_next];
1561 u32 remaining_length = urb->transfer_buffer_length -
1562 urb->actual_length;
1563 command->header = 0x82 | (ed << 5);
1564 if (remaining_length == 0) {
1565 command->length = 0x0000;
1566 } else if (remaining_length > 1024) {
1567 command->length = 0x8000 | 1023;
1568 } else
1569 command->length = 0x8000 | (remaining_length -
1570 1);
1571 command->address = (toggle_bits << 6) | (ep_number << 2)
1572 | (address << 0);
1573 command->width = usb_maxpacket(urb->dev, urb->pipe,
1574 usb_pipeout(urb->pipe));
1575 command->follows = 0;
1576 command->value = 0;
1577 command->buffer = NULL;
1578 target->callback = callback;
1579 target->endp = endp;
1580 target->urb = urb;
1581 target->active = 1;
1582 ftdi->command_next += 1;
1583 ftdi_elan_kick_command_queue(ftdi);
1584 mutex_unlock(&ftdi->u132_lock);
1585 return 0;
1586 } else {
1587 mutex_unlock(&ftdi->u132_lock);
1588 msleep(100);
1589 goto wait;
1590 }
1591 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001592}
1593
1594int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001595 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1596 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1597 int toggle_bits, int error_count, int condition_code, int repeat_number,
1598 int halted, int skipped, int actual, int non_null))
Tony Olecha5c66e42006-09-13 11:26:04 +01001599{
Joe Perches8dae6932014-04-04 15:16:04 -07001600 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1601 return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
1602 ep_number, toggle_bits, callback);
Tony Olecha5c66e42006-09-13 11:26:04 +01001603}
1604
1605
1606EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
1607static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001608 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1609 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1610 int toggle_bits, int error_count, int condition_code, int repeat_number,
1611 int halted, int skipped, int actual, int non_null))
Tony Olecha5c66e42006-09-13 11:26:04 +01001612{
Joe Perches8dae6932014-04-04 15:16:04 -07001613 u8 ed = ed_number - 1;
1614wait:if (ftdi->disconnected > 0) {
1615 return -ENODEV;
1616 } else if (ftdi->initialized == 0) {
1617 return -ENODEV;
1618 } else {
1619 int command_size;
1620 mutex_lock(&ftdi->u132_lock);
1621 command_size = ftdi->command_next - ftdi->command_head;
1622 if (command_size < COMMAND_SIZE) {
1623 struct u132_target *target = &ftdi->target[ed];
1624 struct u132_command *command = &ftdi->command[
1625 COMMAND_MASK & ftdi->command_next];
1626 command->header = 0x81 | (ed << 5);
1627 command->length = 0x0000;
1628 command->address = (toggle_bits << 6) | (ep_number << 2)
1629 | (address << 0);
1630 command->width = usb_maxpacket(urb->dev, urb->pipe,
1631 usb_pipeout(urb->pipe));
1632 command->follows = 0;
1633 command->value = 0;
1634 command->buffer = NULL;
1635 target->callback = callback;
1636 target->endp = endp;
1637 target->urb = urb;
1638 target->active = 1;
1639 ftdi->command_next += 1;
1640 ftdi_elan_kick_command_queue(ftdi);
1641 mutex_unlock(&ftdi->u132_lock);
1642 return 0;
1643 } else {
1644 mutex_unlock(&ftdi->u132_lock);
1645 msleep(100);
1646 goto wait;
1647 }
1648 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001649}
1650
1651int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001652 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1653 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1654 int toggle_bits, int error_count, int condition_code, int repeat_number,
1655 int halted, int skipped, int actual, int non_null))
Tony Olecha5c66e42006-09-13 11:26:04 +01001656{
Joe Perches8dae6932014-04-04 15:16:04 -07001657 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1658 return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
1659 ep_number, toggle_bits, callback);
Tony Olecha5c66e42006-09-13 11:26:04 +01001660}
1661
1662
1663EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
1664static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001665 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1666 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1667 int toggle_bits, int error_count, int condition_code, int repeat_number,
1668 int halted, int skipped, int actual, int non_null))
Tony Olecha5c66e42006-09-13 11:26:04 +01001669{
Joe Perches8dae6932014-04-04 15:16:04 -07001670 u8 ed = ed_number - 1;
1671wait:if (ftdi->disconnected > 0) {
1672 return -ENODEV;
1673 } else if (ftdi->initialized == 0) {
1674 return -ENODEV;
1675 } else {
1676 int command_size;
1677 mutex_lock(&ftdi->u132_lock);
1678 command_size = ftdi->command_next - ftdi->command_head;
1679 if (command_size < COMMAND_SIZE) {
1680 u8 *b;
1681 u16 urb_size;
1682 int i = 0;
1683 char data[30 *3 + 4];
1684 char *d = data;
1685 int m = (sizeof(data) - 1) / 3;
1686 int l = 0;
1687 struct u132_target *target = &ftdi->target[ed];
1688 struct u132_command *command = &ftdi->command[
1689 COMMAND_MASK & ftdi->command_next];
1690 command->header = 0x81 | (ed << 5);
1691 command->address = (toggle_bits << 6) | (ep_number << 2)
1692 | (address << 0);
1693 command->width = usb_maxpacket(urb->dev, urb->pipe,
1694 usb_pipeout(urb->pipe));
1695 command->follows = min_t(u32, 1024,
1696 urb->transfer_buffer_length -
1697 urb->actual_length);
1698 command->value = 0;
1699 command->buffer = urb->transfer_buffer +
1700 urb->actual_length;
1701 command->length = 0x8000 | (command->follows - 1);
1702 b = command->buffer;
1703 urb_size = command->follows;
1704 data[0] = 0;
1705 while (urb_size-- > 0) {
1706 if (i > m) {
1707 } else if (i++ < m) {
1708 int w = sprintf(d, " %02X", *b++);
1709 d += w;
1710 l += w;
1711 } else
1712 d += sprintf(d, " ..");
1713 }
1714 target->callback = callback;
1715 target->endp = endp;
1716 target->urb = urb;
1717 target->active = 1;
1718 ftdi->command_next += 1;
1719 ftdi_elan_kick_command_queue(ftdi);
1720 mutex_unlock(&ftdi->u132_lock);
1721 return 0;
1722 } else {
1723 mutex_unlock(&ftdi->u132_lock);
1724 msleep(100);
1725 goto wait;
1726 }
1727 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001728}
1729
1730int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001731 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1732 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1733 int toggle_bits, int error_count, int condition_code, int repeat_number,
1734 int halted, int skipped, int actual, int non_null))
Tony Olecha5c66e42006-09-13 11:26:04 +01001735{
Joe Perches8dae6932014-04-04 15:16:04 -07001736 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1737 return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
1738 ep_number, toggle_bits, callback);
Tony Olecha5c66e42006-09-13 11:26:04 +01001739}
1740
1741
1742EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
1743static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001744 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1745 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1746 int toggle_bits, int error_count, int condition_code, int repeat_number,
1747 int halted, int skipped, int actual, int non_null))
Tony Olecha5c66e42006-09-13 11:26:04 +01001748{
Joe Perches8dae6932014-04-04 15:16:04 -07001749 u8 ed = ed_number - 1;
1750wait:if (ftdi->disconnected > 0) {
1751 return -ENODEV;
1752 } else if (ftdi->initialized == 0) {
1753 return -ENODEV;
1754 } else {
1755 int command_size;
1756 mutex_lock(&ftdi->u132_lock);
1757 command_size = ftdi->command_next - ftdi->command_head;
1758 if (command_size < COMMAND_SIZE) {
1759 u32 remaining_length = urb->transfer_buffer_length -
1760 urb->actual_length;
1761 struct u132_target *target = &ftdi->target[ed];
1762 struct u132_command *command = &ftdi->command[
1763 COMMAND_MASK & ftdi->command_next];
1764 command->header = 0x83 | (ed << 5);
1765 if (remaining_length == 0) {
1766 command->length = 0x0000;
1767 } else if (remaining_length > 1024) {
1768 command->length = 0x8000 | 1023;
1769 } else
1770 command->length = 0x8000 | (remaining_length -
1771 1);
1772 command->address = (toggle_bits << 6) | (ep_number << 2)
1773 | (address << 0);
1774 command->width = usb_maxpacket(urb->dev, urb->pipe,
1775 usb_pipeout(urb->pipe));
1776 command->follows = 0;
1777 command->value = 0;
1778 command->buffer = NULL;
1779 target->callback = callback;
1780 target->endp = endp;
1781 target->urb = urb;
1782 target->active = 1;
1783 ftdi->command_next += 1;
1784 ftdi_elan_kick_command_queue(ftdi);
1785 mutex_unlock(&ftdi->u132_lock);
1786 return 0;
1787 } else {
1788 mutex_unlock(&ftdi->u132_lock);
1789 msleep(100);
1790 goto wait;
1791 }
1792 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001793}
1794
1795int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001796 void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
1797 void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
1798 int toggle_bits, int error_count, int condition_code, int repeat_number,
1799 int halted, int skipped, int actual, int non_null))
Tony Olecha5c66e42006-09-13 11:26:04 +01001800{
Joe Perches8dae6932014-04-04 15:16:04 -07001801 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1802 return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
1803 ep_number, toggle_bits, callback);
Tony Olecha5c66e42006-09-13 11:26:04 +01001804}
1805
1806
1807EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
1808static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001809 void *endp)
Tony Olecha5c66e42006-09-13 11:26:04 +01001810{
Joe Perches8dae6932014-04-04 15:16:04 -07001811 u8 ed = ed_number - 1;
1812 if (ftdi->disconnected > 0) {
1813 return -ENODEV;
1814 } else if (ftdi->initialized == 0) {
1815 return -ENODEV;
1816 } else {
1817 struct u132_target *target = &ftdi->target[ed];
1818 mutex_lock(&ftdi->u132_lock);
1819 if (target->abandoning > 0) {
1820 mutex_unlock(&ftdi->u132_lock);
1821 return 0;
1822 } else {
1823 target->abandoning = 1;
1824 wait_1:if (target->active == 1) {
1825 int command_size = ftdi->command_next -
1826 ftdi->command_head;
1827 if (command_size < COMMAND_SIZE) {
1828 struct u132_command *command =
1829 &ftdi->command[COMMAND_MASK &
1830 ftdi->command_next];
1831 command->header = 0x80 | (ed << 5) |
1832 0x4;
1833 command->length = 0x00;
1834 command->address = 0x00;
1835 command->width = 0x00;
1836 command->follows = 0;
1837 command->value = 0;
1838 command->buffer = &command->value;
1839 ftdi->command_next += 1;
1840 ftdi_elan_kick_command_queue(ftdi);
1841 } else {
1842 mutex_unlock(&ftdi->u132_lock);
1843 msleep(100);
1844 mutex_lock(&ftdi->u132_lock);
1845 goto wait_1;
1846 }
1847 }
1848 mutex_unlock(&ftdi->u132_lock);
1849 return 0;
1850 }
1851 }
Tony Olecha5c66e42006-09-13 11:26:04 +01001852}
1853
1854int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
Joe Perches8dae6932014-04-04 15:16:04 -07001855 void *endp)
Tony Olecha5c66e42006-09-13 11:26:04 +01001856{
Joe Perches8dae6932014-04-04 15:16:04 -07001857 struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
1858 return ftdi_elan_edset_flush(ftdi, ed_number, endp);
Tony Olecha5c66e42006-09-13 11:26:04 +01001859}
1860
1861
1862EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
1863static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
1864{
Joe Perches8dae6932014-04-04 15:16:04 -07001865 int retry_on_empty = 10;
1866 int retry_on_timeout = 5;
1867 int retry_on_status = 20;
1868more:{
1869 int packet_bytes = 0;
1870 int retval = usb_bulk_msg(ftdi->udev,
1871 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
1872 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
1873 &packet_bytes, 100);
1874 if (packet_bytes > 2) {
1875 char diag[30 *3 + 4];
1876 char *d = diag;
1877 int m = (sizeof(diag) - 1) / 3;
1878 char *b = ftdi->bulk_in_buffer;
1879 int bytes_read = 0;
1880 diag[0] = 0;
1881 while (packet_bytes-- > 0) {
1882 char c = *b++;
1883 if (bytes_read < m) {
1884 d += sprintf(d, " %02X",
1885 0x000000FF & c);
1886 } else if (bytes_read > m) {
1887 } else
1888 d += sprintf(d, " ..");
1889 bytes_read += 1;
1890 continue;
1891 }
1892 goto more;
1893 } else if (packet_bytes > 1) {
1894 char s1 = ftdi->bulk_in_buffer[0];
1895 char s2 = ftdi->bulk_in_buffer[1];
1896 if (s1 == 0x31 && s2 == 0x60) {
1897 return 0;
1898 } else if (retry_on_status-- > 0) {
1899 goto more;
1900 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07001901 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07001902 return -EFAULT;
1903 }
1904 } else if (packet_bytes > 0) {
1905 char b1 = ftdi->bulk_in_buffer[0];
Joe Perches5acc6e42014-04-04 15:16:05 -07001906 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
1907 b1);
Joe Perches8dae6932014-04-04 15:16:04 -07001908 if (retry_on_status-- > 0) {
1909 goto more;
1910 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07001911 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07001912 return -EFAULT;
1913 }
1914 } else if (retval == -ETIMEDOUT) {
1915 if (retry_on_timeout-- > 0) {
1916 goto more;
1917 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07001918 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07001919 return -ENOMEM;
1920 }
1921 } else if (retval == 0) {
1922 if (retry_on_empty-- > 0) {
1923 goto more;
1924 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07001925 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07001926 return -ENOMEM;
1927 }
1928 } else {
1929 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
1930 return retval;
1931 }
1932 }
1933 return -1;
Tony Olecha5c66e42006-09-13 11:26:04 +01001934}
1935
1936
1937/*
Joe Perches8dae6932014-04-04 15:16:04 -07001938 * send the long flush sequence
1939 *
1940 */
Tony Olecha5c66e42006-09-13 11:26:04 +01001941static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
1942{
Joe Perches8dae6932014-04-04 15:16:04 -07001943 int retval;
1944 struct urb *urb;
1945 char *buf;
1946 int I = 257;
1947 int i = 0;
1948 urb = usb_alloc_urb(0, GFP_KERNEL);
1949 if (!urb) {
Joe Perches5acc6e42014-04-04 15:16:05 -07001950 dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequence\n");
Joe Perches8dae6932014-04-04 15:16:04 -07001951 return -ENOMEM;
1952 }
1953 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1954 if (!buf) {
Joe Perches5acc6e42014-04-04 15:16:05 -07001955 dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
Joe Perches8dae6932014-04-04 15:16:04 -07001956 usb_free_urb(urb);
1957 return -ENOMEM;
1958 }
1959 while (I-- > 0)
1960 buf[i++] = 0x55;
1961 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
1962 ftdi->bulk_out_endpointAddr), buf, i,
1963 ftdi_elan_write_bulk_callback, ftdi);
1964 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1965 retval = usb_submit_urb(urb, GFP_KERNEL);
1966 if (retval) {
Joe Perches5acc6e42014-04-04 15:16:05 -07001967 dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
Joe Perches8dae6932014-04-04 15:16:04 -07001968 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
1969 usb_free_urb(urb);
1970 return -ENOMEM;
1971 }
1972 usb_free_urb(urb);
1973 return 0;
Tony Olecha5c66e42006-09-13 11:26:04 +01001974}
1975
1976
1977/*
Joe Perches8dae6932014-04-04 15:16:04 -07001978 * send the reset sequence
1979 *
1980 */
Tony Olecha5c66e42006-09-13 11:26:04 +01001981static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
1982{
Joe Perches8dae6932014-04-04 15:16:04 -07001983 int retval;
1984 struct urb *urb;
1985 char *buf;
1986 int I = 4;
1987 int i = 0;
1988 urb = usb_alloc_urb(0, GFP_KERNEL);
1989 if (!urb) {
Joe Perches5acc6e42014-04-04 15:16:05 -07001990 dev_err(&ftdi->udev->dev, "could not get a urb for the reset sequence\n");
Joe Perches8dae6932014-04-04 15:16:04 -07001991 return -ENOMEM;
1992 }
1993 buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
1994 if (!buf) {
Joe Perches5acc6e42014-04-04 15:16:05 -07001995 dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
Joe Perches8dae6932014-04-04 15:16:04 -07001996 usb_free_urb(urb);
1997 return -ENOMEM;
1998 }
1999 buf[i++] = 0x55;
2000 buf[i++] = 0xAA;
2001 buf[i++] = 0x5A;
2002 buf[i++] = 0xA5;
2003 usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
2004 ftdi->bulk_out_endpointAddr), buf, i,
2005 ftdi_elan_write_bulk_callback, ftdi);
2006 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2007 retval = usb_submit_urb(urb, GFP_KERNEL);
2008 if (retval) {
Joe Perches5acc6e42014-04-04 15:16:05 -07002009 dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002010 usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
2011 usb_free_urb(urb);
2012 return -ENOMEM;
2013 }
2014 usb_free_urb(urb);
2015 return 0;
Tony Olecha5c66e42006-09-13 11:26:04 +01002016}
2017
2018static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
2019{
Joe Perches8dae6932014-04-04 15:16:04 -07002020 int retval;
2021 int long_stop = 10;
2022 int retry_on_timeout = 5;
2023 int retry_on_empty = 10;
2024 int err_count = 0;
2025 retval = ftdi_elan_flush_input_fifo(ftdi);
2026 if (retval)
2027 return retval;
2028 ftdi->bulk_in_left = 0;
2029 ftdi->bulk_in_last = -1;
2030 while (long_stop-- > 0) {
2031 int read_stop;
2032 int read_stuck;
2033 retval = ftdi_elan_synchronize_flush(ftdi);
2034 if (retval)
2035 return retval;
2036 retval = ftdi_elan_flush_input_fifo(ftdi);
2037 if (retval)
2038 return retval;
2039 reset:retval = ftdi_elan_synchronize_reset(ftdi);
2040 if (retval)
2041 return retval;
2042 read_stop = 100;
2043 read_stuck = 10;
2044 read:{
2045 int packet_bytes = 0;
2046 retval = usb_bulk_msg(ftdi->udev,
2047 usb_rcvbulkpipe(ftdi->udev,
2048 ftdi->bulk_in_endpointAddr),
2049 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2050 &packet_bytes, 500);
2051 if (packet_bytes > 2) {
2052 char diag[30 *3 + 4];
2053 char *d = diag;
2054 int m = (sizeof(diag) - 1) / 3;
2055 char *b = ftdi->bulk_in_buffer;
2056 int bytes_read = 0;
2057 unsigned char c = 0;
2058 diag[0] = 0;
2059 while (packet_bytes-- > 0) {
2060 c = *b++;
2061 if (bytes_read < m) {
2062 d += sprintf(d, " %02X", c);
2063 } else if (bytes_read > m) {
2064 } else
2065 d += sprintf(d, " ..");
2066 bytes_read += 1;
2067 continue;
2068 }
2069 if (c == 0x7E) {
2070 return 0;
2071 } else {
2072 if (c == 0x55) {
2073 goto read;
2074 } else if (read_stop-- > 0) {
2075 goto read;
2076 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002077 dev_err(&ftdi->udev->dev, "retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002078 continue;
2079 }
2080 }
2081 } else if (packet_bytes > 1) {
2082 unsigned char s1 = ftdi->bulk_in_buffer[0];
2083 unsigned char s2 = ftdi->bulk_in_buffer[1];
2084 if (s1 == 0x31 && s2 == 0x00) {
2085 if (read_stuck-- > 0) {
2086 goto read;
2087 } else
2088 goto reset;
2089 } else if (s1 == 0x31 && s2 == 0x60) {
2090 if (read_stop-- > 0) {
2091 goto read;
2092 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002093 dev_err(&ftdi->udev->dev, "retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002094 continue;
2095 }
2096 } else {
2097 if (read_stop-- > 0) {
2098 goto read;
2099 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002100 dev_err(&ftdi->udev->dev, "retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002101 continue;
2102 }
2103 }
2104 } else if (packet_bytes > 0) {
2105 if (read_stop-- > 0) {
2106 goto read;
2107 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002108 dev_err(&ftdi->udev->dev, "retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002109 continue;
2110 }
2111 } else if (retval == -ETIMEDOUT) {
2112 if (retry_on_timeout-- > 0) {
2113 goto read;
2114 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002115 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002116 continue;
2117 }
2118 } else if (retval == 0) {
2119 if (retry_on_empty-- > 0) {
2120 goto read;
2121 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002122 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002123 continue;
2124 }
2125 } else {
2126 err_count += 1;
2127 dev_err(&ftdi->udev->dev, "error = %d\n",
2128 retval);
2129 if (read_stop-- > 0) {
2130 goto read;
2131 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002132 dev_err(&ftdi->udev->dev, "retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002133 continue;
2134 }
2135 }
2136 }
2137 }
2138 dev_err(&ftdi->udev->dev, "failed to synchronize\n");
2139 return -EFAULT;
Tony Olecha5c66e42006-09-13 11:26:04 +01002140}
2141
2142static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
2143{
Joe Perches8dae6932014-04-04 15:16:04 -07002144 int retry_on_empty = 10;
2145 int retry_on_timeout = 5;
2146 int retry_on_status = 50;
2147more:{
2148 int packet_bytes = 0;
2149 int retval = usb_bulk_msg(ftdi->udev,
2150 usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
2151 ftdi->bulk_in_buffer, ftdi->bulk_in_size,
2152 &packet_bytes, 1000);
2153 if (packet_bytes > 2) {
2154 char diag[30 *3 + 4];
2155 char *d = diag;
2156 int m = (sizeof(diag) - 1) / 3;
2157 char *b = ftdi->bulk_in_buffer;
2158 int bytes_read = 0;
2159 diag[0] = 0;
2160 while (packet_bytes-- > 0) {
2161 char c = *b++;
2162 if (bytes_read < m) {
2163 d += sprintf(d, " %02X",
2164 0x000000FF & c);
2165 } else if (bytes_read > m) {
2166 } else
2167 d += sprintf(d, " ..");
2168 bytes_read += 1;
2169 continue;
2170 }
2171 goto more;
2172 } else if (packet_bytes > 1) {
2173 char s1 = ftdi->bulk_in_buffer[0];
2174 char s2 = ftdi->bulk_in_buffer[1];
2175 if (s1 == 0x31 && s2 == 0x60) {
2176 return 0;
2177 } else if (retry_on_status-- > 0) {
2178 msleep(5);
2179 goto more;
2180 } else
2181 return -EFAULT;
2182 } else if (packet_bytes > 0) {
2183 char b1 = ftdi->bulk_in_buffer[0];
Joe Perches5acc6e42014-04-04 15:16:05 -07002184 dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
Joe Perches8dae6932014-04-04 15:16:04 -07002185 if (retry_on_status-- > 0) {
2186 msleep(5);
2187 goto more;
2188 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002189 dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002190 return -EFAULT;
2191 }
2192 } else if (retval == -ETIMEDOUT) {
2193 if (retry_on_timeout-- > 0) {
2194 goto more;
2195 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002196 dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002197 return -ENOMEM;
2198 }
2199 } else if (retval == 0) {
2200 if (retry_on_empty-- > 0) {
2201 goto more;
2202 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002203 dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002204 return -ENOMEM;
2205 }
2206 } else {
2207 dev_err(&ftdi->udev->dev, "error = %d\n", retval);
2208 return -ENOMEM;
2209 }
2210 }
2211 return -1;
Tony Olecha5c66e42006-09-13 11:26:04 +01002212}
2213
2214static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
2215{
Joe Perches8dae6932014-04-04 15:16:04 -07002216 int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
2217 if (UxxxStatus)
2218 return UxxxStatus;
2219 if (ftdi->controlreg & 0x00400000) {
2220 if (ftdi->card_ejected) {
2221 } else {
2222 ftdi->card_ejected = 1;
Joe Perches5acc6e42014-04-04 15:16:05 -07002223 dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
2224 ftdi->controlreg);
Joe Perches8dae6932014-04-04 15:16:04 -07002225 }
2226 return -ENODEV;
2227 } else {
2228 u8 fn = ftdi->function - 1;
2229 int activePCIfn = fn << 8;
2230 u32 pcidata;
2231 u32 pciVID;
2232 u32 pciPID;
2233 int reg = 0;
2234 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2235 &pcidata);
2236 if (UxxxStatus)
2237 return UxxxStatus;
2238 pciVID = pcidata & 0xFFFF;
2239 pciPID = (pcidata >> 16) & 0xFFFF;
2240 if (pciVID == ftdi->platform_data.vendor && pciPID ==
2241 ftdi->platform_data.device) {
2242 return 0;
2243 } else {
Joe Perches5acc6e42014-04-04 15:16:05 -07002244 dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
Joe Perches8dae6932014-04-04 15:16:04 -07002245 ftdi->platform_data.vendor, pciVID,
2246 ftdi->platform_data.device, pciPID);
2247 return -ENODEV;
2248 }
2249 }
Tony Olecha5c66e42006-09-13 11:26:04 +01002250}
2251
Tony Olech4b873612006-12-06 13:16:22 +00002252
2253#define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
Joe Perches8dae6932014-04-04 15:16:04 -07002254 offsetof(struct ohci_regs, member), 0, data);
Tony Olech4b873612006-12-06 13:16:22 +00002255#define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
Joe Perches8dae6932014-04-04 15:16:04 -07002256 offsetof(struct ohci_regs, member), 0, data);
David Brownell47f84682007-04-29 10:21:14 -07002257
Tony Olech4b873612006-12-06 13:16:22 +00002258#define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
Joe Perches8dae6932014-04-04 15:16:04 -07002259#define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
2260 OHCI_INTR_WDH)
Tony Olech4b873612006-12-06 13:16:22 +00002261static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
2262{
Joe Perches8dae6932014-04-04 15:16:04 -07002263 int devices = 0;
2264 int retval;
2265 u32 hc_control;
2266 int num_ports;
2267 u32 control;
2268 u32 rh_a = -1;
2269 u32 status;
2270 u32 fminterval;
2271 u32 hc_fminterval;
2272 u32 periodicstart;
2273 u32 cmdstatus;
2274 u32 roothub_a;
2275 int mask = OHCI_INTR_INIT;
2276 int sleep_time = 0;
2277 int reset_timeout = 30; /* ... allow extra time */
2278 int temp;
2279 retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
2280 if (retval)
2281 return retval;
2282 retval = ftdi_read_pcimem(ftdi, control, &control);
2283 if (retval)
2284 return retval;
2285 retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
2286 if (retval)
2287 return retval;
2288 num_ports = rh_a & RH_A_NDP;
2289 retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
2290 if (retval)
2291 return retval;
2292 hc_fminterval &= 0x3fff;
2293 if (hc_fminterval != FI) {
2294 }
2295 hc_fminterval |= FSMP(hc_fminterval) << 16;
2296 retval = ftdi_read_pcimem(ftdi, control, &hc_control);
2297 if (retval)
2298 return retval;
2299 switch (hc_control & OHCI_CTRL_HCFS) {
2300 case OHCI_USB_OPER:
2301 sleep_time = 0;
2302 break;
2303 case OHCI_USB_SUSPEND:
2304 case OHCI_USB_RESUME:
2305 hc_control &= OHCI_CTRL_RWC;
2306 hc_control |= OHCI_USB_RESUME;
2307 sleep_time = 10;
2308 break;
2309 default:
2310 hc_control &= OHCI_CTRL_RWC;
2311 hc_control |= OHCI_USB_RESET;
2312 sleep_time = 50;
2313 break;
2314 }
2315 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2316 if (retval)
2317 return retval;
2318 retval = ftdi_read_pcimem(ftdi, control, &control);
2319 if (retval)
2320 return retval;
2321 msleep(sleep_time);
2322 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2323 if (retval)
2324 return retval;
2325 if (!(roothub_a & RH_A_NPS)) { /* power down each port */
2326 for (temp = 0; temp < num_ports; temp++) {
2327 retval = ftdi_write_pcimem(ftdi,
2328 roothub.portstatus[temp], RH_PS_LSDA);
2329 if (retval)
2330 return retval;
2331 }
2332 }
2333 retval = ftdi_read_pcimem(ftdi, control, &control);
2334 if (retval)
2335 return retval;
2336retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2337 if (retval)
2338 return retval;
2339 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
2340 if (retval)
2341 return retval;
2342extra:{
2343 retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
2344 if (retval)
2345 return retval;
2346 if (0 != (status & OHCI_HCR)) {
2347 if (--reset_timeout == 0) {
Joe Perches5acc6e42014-04-04 15:16:05 -07002348 dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002349 return -ENODEV;
2350 } else {
2351 msleep(5);
2352 goto extra;
2353 }
2354 }
2355 }
2356 if (quirk & OHCI_QUIRK_INITRESET) {
2357 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2358 if (retval)
2359 return retval;
2360 retval = ftdi_read_pcimem(ftdi, control, &control);
2361 if (retval)
2362 return retval;
2363 }
2364 retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
2365 if (retval)
2366 return retval;
2367 retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
2368 if (retval)
2369 return retval;
2370 retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
2371 if (retval)
2372 return retval;
2373 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2374 if (retval)
2375 return retval;
2376 retval = ftdi_write_pcimem(ftdi, fminterval,
2377 ((fminterval & FIT) ^ FIT) | hc_fminterval);
2378 if (retval)
2379 return retval;
2380 retval = ftdi_write_pcimem(ftdi, periodicstart,
2381 ((9 *hc_fminterval) / 10) & 0x3fff);
2382 if (retval)
2383 return retval;
2384 retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
2385 if (retval)
2386 return retval;
2387 retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
2388 if (retval)
2389 return retval;
2390 if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
2391 if (!(quirk & OHCI_QUIRK_INITRESET)) {
2392 quirk |= OHCI_QUIRK_INITRESET;
2393 goto retry;
2394 } else
2395 dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
2396 fminterval, periodicstart);
2397 } /* start controller operations */
2398 hc_control &= OHCI_CTRL_RWC;
2399 hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
2400 retval = ftdi_write_pcimem(ftdi, control, hc_control);
2401 if (retval)
2402 return retval;
2403 retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
2404 if (retval)
2405 return retval;
2406 retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
2407 if (retval)
2408 return retval;
2409 retval = ftdi_read_pcimem(ftdi, control, &control);
2410 if (retval)
2411 return retval;
2412 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
2413 if (retval)
2414 return retval;
2415 retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
2416 if (retval)
2417 return retval;
2418 retval = ftdi_write_pcimem(ftdi, intrdisable,
2419 OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
2420 OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
2421 OHCI_INTR_SO);
2422 if (retval)
2423 return retval; /* handle root hub init quirks ... */
2424 retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
2425 if (retval)
2426 return retval;
2427 roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
2428 if (quirk & OHCI_QUIRK_SUPERIO) {
2429 roothub_a |= RH_A_NOCP;
2430 roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
2431 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2432 if (retval)
2433 return retval;
2434 } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
2435 roothub_a |= RH_A_NPS;
2436 retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
2437 if (retval)
2438 return retval;
2439 }
2440 retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
2441 if (retval)
2442 return retval;
2443 retval = ftdi_write_pcimem(ftdi, roothub.b,
2444 (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
2445 if (retval)
2446 return retval;
2447 retval = ftdi_read_pcimem(ftdi, control, &control);
2448 if (retval)
2449 return retval;
2450 mdelay((roothub_a >> 23) & 0x1fe);
2451 for (temp = 0; temp < num_ports; temp++) {
2452 u32 portstatus;
2453 retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
2454 &portstatus);
2455 if (retval)
2456 return retval;
2457 if (1 & portstatus)
2458 devices += 1;
2459 }
2460 return devices;
Tony Olech4b873612006-12-06 13:16:22 +00002461}
2462
2463static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
Tony Olecha5c66e42006-09-13 11:26:04 +01002464{
Joe Perches8dae6932014-04-04 15:16:04 -07002465 u32 latence_timer;
2466 int UxxxStatus;
2467 u32 pcidata;
2468 int reg = 0;
2469 int activePCIfn = fn << 8;
2470 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2471 if (UxxxStatus)
2472 return UxxxStatus;
2473 reg = 16;
2474 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2475 0xFFFFFFFF);
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 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2483 0xF0000000);
2484 if (UxxxStatus)
2485 return UxxxStatus;
2486 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2487 &pcidata);
2488 if (UxxxStatus)
2489 return UxxxStatus;
2490 reg = 12;
2491 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2492 &latence_timer);
2493 if (UxxxStatus)
2494 return UxxxStatus;
2495 latence_timer &= 0xFFFF00FF;
2496 latence_timer |= 0x00001600;
2497 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2498 latence_timer);
2499 if (UxxxStatus)
2500 return UxxxStatus;
2501 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2502 &pcidata);
2503 if (UxxxStatus)
2504 return UxxxStatus;
2505 reg = 4;
2506 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2507 0x06);
2508 if (UxxxStatus)
2509 return UxxxStatus;
2510 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2511 &pcidata);
2512 if (UxxxStatus)
2513 return UxxxStatus;
2514 for (reg = 0; reg <= 0x54; reg += 4) {
2515 UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
2516 if (UxxxStatus)
2517 return UxxxStatus;
2518 }
2519 return 0;
Tony Olecha5c66e42006-09-13 11:26:04 +01002520}
2521
Tony Olech4b873612006-12-06 13:16:22 +00002522static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
2523{
Joe Perches8dae6932014-04-04 15:16:04 -07002524 u32 latence_timer;
2525 int UxxxStatus;
2526 u32 pcidata;
2527 int reg = 0;
2528 int activePCIfn = fn << 8;
2529 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
2530 if (UxxxStatus)
2531 return UxxxStatus;
2532 reg = 16;
2533 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2534 0xFFFFFFFF);
2535 if (UxxxStatus)
2536 return UxxxStatus;
2537 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2538 &pcidata);
2539 if (UxxxStatus)
2540 return UxxxStatus;
2541 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
2542 0x00000000);
2543 if (UxxxStatus)
2544 return UxxxStatus;
2545 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2546 &pcidata);
2547 if (UxxxStatus)
2548 return UxxxStatus;
2549 reg = 12;
2550 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2551 &latence_timer);
2552 if (UxxxStatus)
2553 return UxxxStatus;
2554 latence_timer &= 0xFFFF00FF;
2555 latence_timer |= 0x00001600;
2556 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2557 latence_timer);
2558 if (UxxxStatus)
2559 return UxxxStatus;
2560 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2561 &pcidata);
2562 if (UxxxStatus)
2563 return UxxxStatus;
2564 reg = 4;
2565 UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
2566 0x00);
2567 if (UxxxStatus)
2568 return UxxxStatus;
2569 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2570 &pcidata);
2571 if (UxxxStatus)
2572 return UxxxStatus;
2573 return 0;
Tony Olech4b873612006-12-06 13:16:22 +00002574}
2575
2576static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
2577{
Joe Perches8dae6932014-04-04 15:16:04 -07002578 int result;
2579 int UxxxStatus;
2580 UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
2581 if (UxxxStatus)
2582 return UxxxStatus;
2583 result = ftdi_elan_check_controller(ftdi, quirk);
2584 UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
2585 if (UxxxStatus)
2586 return UxxxStatus;
2587 return result;
Tony Olech4b873612006-12-06 13:16:22 +00002588}
2589
2590static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
2591{
Joe Perches8dae6932014-04-04 15:16:04 -07002592 u32 controlreg;
2593 u8 sensebits;
2594 int UxxxStatus;
2595 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2596 if (UxxxStatus)
2597 return UxxxStatus;
2598 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
2599 if (UxxxStatus)
2600 return UxxxStatus;
2601 msleep(750);
2602 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
2603 if (UxxxStatus)
2604 return UxxxStatus;
2605 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
2606 if (UxxxStatus)
2607 return UxxxStatus;
2608 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2609 if (UxxxStatus)
2610 return UxxxStatus;
2611 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
2612 if (UxxxStatus)
2613 return UxxxStatus;
2614 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
2615 if (UxxxStatus)
2616 return UxxxStatus;
2617 msleep(250);
2618 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
2619 if (UxxxStatus)
2620 return UxxxStatus;
2621 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2622 if (UxxxStatus)
2623 return UxxxStatus;
2624 UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
2625 if (UxxxStatus)
2626 return UxxxStatus;
2627 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2628 if (UxxxStatus)
2629 return UxxxStatus;
2630 UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
2631 if (UxxxStatus)
2632 return UxxxStatus;
2633 msleep(1000);
2634 sensebits = (controlreg >> 16) & 0x000F;
2635 if (0x0D == sensebits)
2636 return 0;
2637 else
Tony Olech4b873612006-12-06 13:16:22 +00002638 return - ENXIO;
2639}
2640
Tony Olecha5c66e42006-09-13 11:26:04 +01002641static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
2642{
Joe Perches8dae6932014-04-04 15:16:04 -07002643 int UxxxStatus;
2644 u32 pcidata;
2645 int reg = 0;
2646 u8 fn;
2647 int activePCIfn = 0;
2648 int max_devices = 0;
2649 int controllers = 0;
2650 int unrecognized = 0;
2651 ftdi->function = 0;
2652 for (fn = 0; (fn < 4); fn++) {
2653 u32 pciVID = 0;
2654 u32 pciPID = 0;
2655 int devices = 0;
2656 activePCIfn = fn << 8;
2657 UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
2658 &pcidata);
2659 if (UxxxStatus)
2660 return UxxxStatus;
2661 pciVID = pcidata & 0xFFFF;
2662 pciPID = (pcidata >> 16) & 0xFFFF;
2663 if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
2664 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2665 controllers += 1;
2666 } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
2667 {
2668 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2669 controllers += 1;
2670 } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
2671 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2672 controllers += 1;
2673 } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
2674 {
2675 devices = ftdi_elan_found_controller(ftdi, fn, 0);
2676 controllers += 1;
2677 } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
2678 devices = ftdi_elan_found_controller(ftdi, fn,
2679 OHCI_QUIRK_AMD756);
2680 controllers += 1;
2681 } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
2682 devices = ftdi_elan_found_controller(ftdi, fn,
2683 OHCI_QUIRK_ZFMICRO);
2684 controllers += 1;
2685 } else if (0 == pcidata) {
2686 } else
2687 unrecognized += 1;
2688 if (devices > max_devices) {
2689 max_devices = devices;
2690 ftdi->function = fn + 1;
2691 ftdi->platform_data.vendor = pciVID;
2692 ftdi->platform_data.device = pciPID;
2693 }
2694 }
2695 if (ftdi->function > 0) {
2696 UxxxStatus = ftdi_elan_setup_controller(ftdi,
2697 ftdi->function - 1);
2698 if (UxxxStatus)
2699 return UxxxStatus;
2700 return 0;
2701 } else if (controllers > 0) {
2702 return -ENXIO;
2703 } else if (unrecognized > 0) {
2704 return -ENXIO;
2705 } else {
2706 ftdi->enumerated = 0;
2707 return -ENXIO;
2708 }
Tony Olecha5c66e42006-09-13 11:26:04 +01002709}
2710
2711
2712/*
Joe Perches8dae6932014-04-04 15:16:04 -07002713 * we use only the first bulk-in and bulk-out endpoints
2714 */
Tony Olecha5c66e42006-09-13 11:26:04 +01002715static int ftdi_elan_probe(struct usb_interface *interface,
Joe Perches8dae6932014-04-04 15:16:04 -07002716 const struct usb_device_id *id)
Tony Olecha5c66e42006-09-13 11:26:04 +01002717{
Joe Perches8dae6932014-04-04 15:16:04 -07002718 struct usb_host_interface *iface_desc;
2719 struct usb_endpoint_descriptor *endpoint;
2720 size_t buffer_size;
2721 int i;
2722 int retval = -ENOMEM;
2723 struct usb_ftdi *ftdi;
Mariusz Kozlowski5280d602007-08-10 14:53:35 -07002724
2725 ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
2726 if (!ftdi) {
Joe Perches8dae6932014-04-04 15:16:04 -07002727 printk(KERN_ERR "Out of memory\n");
2728 return -ENOMEM;
2729 }
Mariusz Kozlowski5280d602007-08-10 14:53:35 -07002730
Joe Perches8dae6932014-04-04 15:16:04 -07002731 mutex_lock(&ftdi_module_lock);
2732 list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
2733 ftdi->sequence_num = ++ftdi_instances;
2734 mutex_unlock(&ftdi_module_lock);
2735 ftdi_elan_init_kref(ftdi);
Thomas Gleixner5014b5e32010-09-07 14:32:43 +00002736 sema_init(&ftdi->sw_lock, 1);
Joe Perches8dae6932014-04-04 15:16:04 -07002737 ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
2738 ftdi->interface = interface;
2739 mutex_init(&ftdi->u132_lock);
2740 ftdi->expected = 4;
2741 iface_desc = interface->cur_altsetting;
2742 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2743 endpoint = &iface_desc->endpoint[i].desc;
2744 if (!ftdi->bulk_in_endpointAddr &&
Luiz Fernando N. Capitulino2ae77452006-10-26 13:02:50 -03002745 usb_endpoint_is_bulk_in(endpoint)) {
Joe Perches8dae6932014-04-04 15:16:04 -07002746 buffer_size = usb_endpoint_maxp(endpoint);
2747 ftdi->bulk_in_size = buffer_size;
2748 ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
2749 ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
2750 if (!ftdi->bulk_in_buffer) {
Joe Perches5acc6e42014-04-04 15:16:05 -07002751 dev_err(&ftdi->udev->dev, "Could not allocate bulk_in_buffer\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002752 retval = -ENOMEM;
2753 goto error;
2754 }
2755 }
2756 if (!ftdi->bulk_out_endpointAddr &&
Luiz Fernando N. Capitulino2ae77452006-10-26 13:02:50 -03002757 usb_endpoint_is_bulk_out(endpoint)) {
Joe Perches8dae6932014-04-04 15:16:04 -07002758 ftdi->bulk_out_endpointAddr =
2759 endpoint->bEndpointAddress;
2760 }
2761 }
2762 if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
Joe Perches5acc6e42014-04-04 15:16:05 -07002763 dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002764 retval = -ENODEV;
2765 goto error;
2766 }
2767 dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
2768 iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
2769 ftdi->bulk_out_endpointAddr);
2770 usb_set_intfdata(interface, ftdi);
2771 if (iface_desc->desc.bInterfaceNumber == 0 &&
2772 ftdi->bulk_in_endpointAddr == 0x81 &&
2773 ftdi->bulk_out_endpointAddr == 0x02) {
2774 retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
2775 if (retval) {
Joe Perches5acc6e42014-04-04 15:16:05 -07002776 dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002777 usb_set_intfdata(interface, NULL);
2778 retval = -ENOMEM;
2779 goto error;
2780 } else {
2781 ftdi->class = &ftdi_elan_jtag_class;
Joe Perches5acc6e42014-04-04 15:16:05 -07002782 dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
2783 ftdi, iface_desc->desc.bInterfaceNumber,
Joe Perches8dae6932014-04-04 15:16:04 -07002784 interface->minor);
2785 return 0;
2786 }
2787 } else if (iface_desc->desc.bInterfaceNumber == 1 &&
2788 ftdi->bulk_in_endpointAddr == 0x83 &&
2789 ftdi->bulk_out_endpointAddr == 0x04) {
2790 ftdi->class = NULL;
Joe Perches5acc6e42014-04-04 15:16:05 -07002791 dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
2792 ftdi, iface_desc->desc.bInterfaceNumber);
Joe Perches8dae6932014-04-04 15:16:04 -07002793 INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
2794 INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
2795 INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
2796 ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
2797 return 0;
2798 } else {
2799 dev_err(&ftdi->udev->dev,
2800 "Could not find ELAN's U132 device\n");
2801 retval = -ENODEV;
2802 goto error;
2803 }
2804error:if (ftdi) {
2805 ftdi_elan_put_kref(ftdi);
2806 }
2807 return retval;
Tony Olecha5c66e42006-09-13 11:26:04 +01002808}
2809
2810static void ftdi_elan_disconnect(struct usb_interface *interface)
2811{
Joe Perches8dae6932014-04-04 15:16:04 -07002812 struct usb_ftdi *ftdi = usb_get_intfdata(interface);
2813 ftdi->disconnected += 1;
2814 if (ftdi->class) {
2815 int minor = interface->minor;
2816 struct usb_class_driver *class = ftdi->class;
2817 usb_set_intfdata(interface, NULL);
2818 usb_deregister_dev(interface, class);
Joe Perches5acc6e42014-04-04 15:16:05 -07002819 dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
2820 minor);
Joe Perches8dae6932014-04-04 15:16:04 -07002821 } else {
2822 ftdi_status_cancel_work(ftdi);
2823 ftdi_command_cancel_work(ftdi);
2824 ftdi_response_cancel_work(ftdi);
2825 ftdi_elan_abandon_completions(ftdi);
2826 ftdi_elan_abandon_targets(ftdi);
2827 if (ftdi->registered) {
2828 platform_device_unregister(&ftdi->platform_dev);
2829 ftdi->synchronized = 0;
2830 ftdi->enumerated = 0;
2831 ftdi->initialized = 0;
2832 ftdi->registered = 0;
2833 }
2834 flush_workqueue(status_queue);
2835 flush_workqueue(command_queue);
2836 flush_workqueue(respond_queue);
2837 ftdi->disconnected += 1;
2838 usb_set_intfdata(interface, NULL);
Joe Perches5acc6e42014-04-04 15:16:05 -07002839 dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
Joe Perches8dae6932014-04-04 15:16:04 -07002840 }
2841 ftdi_elan_put_kref(ftdi);
Tony Olecha5c66e42006-09-13 11:26:04 +01002842}
2843
2844static struct usb_driver ftdi_elan_driver = {
Joe Perches8dae6932014-04-04 15:16:04 -07002845 .name = "ftdi-elan",
2846 .probe = ftdi_elan_probe,
2847 .disconnect = ftdi_elan_disconnect,
2848 .id_table = ftdi_elan_table,
Tony Olecha5c66e42006-09-13 11:26:04 +01002849};
2850static int __init ftdi_elan_init(void)
2851{
Joe Perches8dae6932014-04-04 15:16:04 -07002852 int result;
2853 printk(KERN_INFO "driver %s\n", ftdi_elan_driver.name);
2854 mutex_init(&ftdi_module_lock);
2855 INIT_LIST_HEAD(&ftdi_static_list);
2856 status_queue = create_singlethread_workqueue("ftdi-status-control");
Cyrill Gorcunovee17b282007-03-06 02:47:44 -08002857 if (!status_queue)
Cyrill Gorcunov893a3422007-04-26 00:38:00 -07002858 goto err_status_queue;
Joe Perches8dae6932014-04-04 15:16:04 -07002859 command_queue = create_singlethread_workqueue("ftdi-command-engine");
Cyrill Gorcunovee17b282007-03-06 02:47:44 -08002860 if (!command_queue)
Cyrill Gorcunov893a3422007-04-26 00:38:00 -07002861 goto err_command_queue;
Joe Perches8dae6932014-04-04 15:16:04 -07002862 respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
Cyrill Gorcunovee17b282007-03-06 02:47:44 -08002863 if (!respond_queue)
Cyrill Gorcunov893a3422007-04-26 00:38:00 -07002864 goto err_respond_queue;
Joe Perches8dae6932014-04-04 15:16:04 -07002865 result = usb_register(&ftdi_elan_driver);
2866 if (result) {
Cyrill Gorcunov893a3422007-04-26 00:38:00 -07002867 destroy_workqueue(status_queue);
2868 destroy_workqueue(command_queue);
2869 destroy_workqueue(respond_queue);
Joe Perches8dae6932014-04-04 15:16:04 -07002870 printk(KERN_ERR "usb_register failed. Error number %d\n",
Cyrill Gorcunovee17b282007-03-06 02:47:44 -08002871 result);
Cyrill Gorcunov893a3422007-04-26 00:38:00 -07002872 }
Joe Perches8dae6932014-04-04 15:16:04 -07002873 return result;
Cyrill Gorcunovee17b282007-03-06 02:47:44 -08002874
Joe Perches8dae6932014-04-04 15:16:04 -07002875err_respond_queue:
Cyrill Gorcunovee17b282007-03-06 02:47:44 -08002876 destroy_workqueue(command_queue);
Joe Perches8dae6932014-04-04 15:16:04 -07002877err_command_queue:
Cyrill Gorcunovee17b282007-03-06 02:47:44 -08002878 destroy_workqueue(status_queue);
Joe Perches8dae6932014-04-04 15:16:04 -07002879err_status_queue:
Cyrill Gorcunovee17b282007-03-06 02:47:44 -08002880 printk(KERN_ERR "%s couldn't create workqueue\n", ftdi_elan_driver.name);
2881 return -ENOMEM;
Tony Olecha5c66e42006-09-13 11:26:04 +01002882}
2883
2884static void __exit ftdi_elan_exit(void)
2885{
Joe Perches8dae6932014-04-04 15:16:04 -07002886 struct usb_ftdi *ftdi;
2887 struct usb_ftdi *temp;
2888 usb_deregister(&ftdi_elan_driver);
2889 printk(KERN_INFO "ftdi_u132 driver deregistered\n");
2890 list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
2891 ftdi_status_cancel_work(ftdi);
2892 ftdi_command_cancel_work(ftdi);
2893 ftdi_response_cancel_work(ftdi);
2894 } flush_workqueue(status_queue);
2895 destroy_workqueue(status_queue);
2896 status_queue = NULL;
2897 flush_workqueue(command_queue);
2898 destroy_workqueue(command_queue);
2899 command_queue = NULL;
2900 flush_workqueue(respond_queue);
2901 destroy_workqueue(respond_queue);
2902 respond_queue = NULL;
Tony Olecha5c66e42006-09-13 11:26:04 +01002903}
2904
2905
2906module_init(ftdi_elan_init);
2907module_exit(ftdi_elan_exit);