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