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