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