Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1 | /** |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2 | * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany |
| 5 | * Copyright (C) 2008 Novell, Inc. |
| 6 | * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version 2 |
| 11 | * of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * The GNU General Public License is available at |
| 19 | * http://www.gnu.org/copyleft/gpl.html. |
| 20 | */ |
| 21 | |
Andy Shevchenko | f4d844c | 2013-07-15 11:59:42 +0300 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/module.h> |
Ilpo Järvinen | 857cc4d | 2008-10-30 13:56:47 +0200 | [diff] [blame] | 26 | #include <linux/kernel.h> |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 27 | #include <linux/fs.h> |
| 28 | #include <linux/uaccess.h> |
| 29 | #include <linux/kref.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 31 | #include <linux/mutex.h> |
| 32 | #include <linux/usb.h> |
| 33 | #include <linux/usb/tmc.h> |
| 34 | |
| 35 | |
Alexandre Peixoto Ferreira | 8402db5 | 2013-04-30 00:51:50 -0500 | [diff] [blame] | 36 | #define RIGOL 1 |
| 37 | #define USBTMC_HEADER_SIZE 12 |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 38 | #define USBTMC_MINOR_BASE 176 |
| 39 | |
| 40 | /* |
| 41 | * Size of driver internal IO buffer. Must be multiple of 4 and at least as |
| 42 | * large as wMaxPacketSize (which is usually 512 bytes). |
| 43 | */ |
| 44 | #define USBTMC_SIZE_IOBUFFER 2048 |
| 45 | |
| 46 | /* Default USB timeout (in milliseconds) */ |
Gergely Imreh | 35f76e89 | 2009-09-15 16:03:31 +0800 | [diff] [blame] | 47 | #define USBTMC_TIMEOUT 5000 |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Maximum number of read cycles to empty bulk in endpoint during CLEAR and |
| 51 | * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short |
| 52 | * packet is never read. |
| 53 | */ |
| 54 | #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100 |
| 55 | |
Németh Márton | 6ef4852 | 2010-01-10 15:33:45 +0100 | [diff] [blame] | 56 | static const struct usb_device_id usbtmc_devices[] = { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 57 | { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), }, |
Greg Kroah-Hartman | 228dd05 | 2009-03-11 13:51:42 -0700 | [diff] [blame] | 58 | { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), }, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 59 | { 0, } /* terminating entry */ |
| 60 | }; |
Greg Kroah-Hartman | 5413aa4 | 2008-12-03 16:33:09 -0800 | [diff] [blame] | 61 | MODULE_DEVICE_TABLE(usb, usbtmc_devices); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * This structure is the capabilities for the device |
Gergely Imreh | d0a3836 | 2009-09-07 10:47:01 +0800 | [diff] [blame] | 65 | * See section 4.2.1.8 of the USBTMC specification, |
| 66 | * and section 4.2.2 of the USBTMC usb488 subclass |
| 67 | * specification for details. |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 68 | */ |
| 69 | struct usbtmc_dev_capabilities { |
| 70 | __u8 interface_capabilities; |
| 71 | __u8 device_capabilities; |
| 72 | __u8 usb488_interface_capabilities; |
| 73 | __u8 usb488_device_capabilities; |
| 74 | }; |
| 75 | |
| 76 | /* This structure holds private data for each USBTMC device. One copy is |
| 77 | * allocated for each USBTMC device in the driver's probe function. |
| 78 | */ |
| 79 | struct usbtmc_device_data { |
| 80 | const struct usb_device_id *id; |
| 81 | struct usb_device *usb_dev; |
| 82 | struct usb_interface *intf; |
| 83 | |
| 84 | unsigned int bulk_in; |
| 85 | unsigned int bulk_out; |
| 86 | |
| 87 | u8 bTag; |
| 88 | u8 bTag_last_write; /* needed for abort */ |
| 89 | u8 bTag_last_read; /* needed for abort */ |
| 90 | |
Alexandre Peixoto Ferreira | 8402db5 | 2013-04-30 00:51:50 -0500 | [diff] [blame] | 91 | u8 rigol_quirk; |
| 92 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 93 | /* attributes from the USB TMC spec for this device */ |
| 94 | u8 TermChar; |
| 95 | bool TermCharEnabled; |
| 96 | bool auto_abort; |
| 97 | |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 98 | bool zombie; /* fd of disconnected device */ |
| 99 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 100 | struct usbtmc_dev_capabilities capabilities; |
| 101 | struct kref kref; |
| 102 | struct mutex io_mutex; /* only one i/o function running at a time */ |
| 103 | }; |
| 104 | #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref) |
| 105 | |
Alexandre Peixoto Ferreira | 8402db5 | 2013-04-30 00:51:50 -0500 | [diff] [blame] | 106 | struct usbtmc_ID_rigol_quirk { |
| 107 | __u16 idVendor; |
| 108 | __u16 idProduct; |
| 109 | }; |
| 110 | |
| 111 | static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = { |
| 112 | { 0x1ab1, 0x0588 }, |
| 113 | { 0, 0 } |
| 114 | }; |
| 115 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 116 | /* Forward declarations */ |
| 117 | static struct usb_driver usbtmc_driver; |
| 118 | |
| 119 | static void usbtmc_delete(struct kref *kref) |
| 120 | { |
| 121 | struct usbtmc_device_data *data = to_usbtmc_data(kref); |
| 122 | |
| 123 | usb_put_dev(data->usb_dev); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int usbtmc_open(struct inode *inode, struct file *filp) |
| 127 | { |
| 128 | struct usb_interface *intf; |
| 129 | struct usbtmc_device_data *data; |
Greg Kroah-Hartman | 5b10916 | 2009-03-10 20:42:55 -0700 | [diff] [blame] | 130 | int retval = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 131 | |
| 132 | intf = usb_find_interface(&usbtmc_driver, iminor(inode)); |
| 133 | if (!intf) { |
Andy Shevchenko | f4d844c | 2013-07-15 11:59:42 +0300 | [diff] [blame] | 134 | pr_err("can not find device for minor %d", iminor(inode)); |
| 135 | return -ENODEV; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | data = usb_get_intfdata(intf); |
| 139 | kref_get(&data->kref); |
| 140 | |
| 141 | /* Store pointer in file structure's private data field */ |
| 142 | filp->private_data = data; |
| 143 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 144 | return retval; |
| 145 | } |
| 146 | |
| 147 | static int usbtmc_release(struct inode *inode, struct file *file) |
| 148 | { |
| 149 | struct usbtmc_device_data *data = file->private_data; |
| 150 | |
| 151 | kref_put(&data->kref, usbtmc_delete); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data) |
| 156 | { |
Chris Malley | b361a6e | 2008-10-25 22:07:32 +0100 | [diff] [blame] | 157 | u8 *buffer; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 158 | struct device *dev; |
| 159 | int rv; |
| 160 | int n; |
| 161 | int actual; |
| 162 | struct usb_host_interface *current_setting; |
| 163 | int max_size; |
| 164 | |
| 165 | dev = &data->intf->dev; |
| 166 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 167 | if (!buffer) |
| 168 | return -ENOMEM; |
| 169 | |
| 170 | rv = usb_control_msg(data->usb_dev, |
| 171 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 172 | USBTMC_REQUEST_INITIATE_ABORT_BULK_IN, |
| 173 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 174 | data->bTag_last_read, data->bulk_in, |
| 175 | buffer, 2, USBTMC_TIMEOUT); |
| 176 | |
| 177 | if (rv < 0) { |
| 178 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 179 | goto exit; |
| 180 | } |
| 181 | |
| 182 | dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]); |
| 183 | |
| 184 | if (buffer[0] == USBTMC_STATUS_FAILED) { |
| 185 | rv = 0; |
| 186 | goto exit; |
| 187 | } |
| 188 | |
| 189 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 190 | dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", |
| 191 | buffer[0]); |
| 192 | rv = -EPERM; |
| 193 | goto exit; |
| 194 | } |
| 195 | |
| 196 | max_size = 0; |
| 197 | current_setting = data->intf->cur_altsetting; |
| 198 | for (n = 0; n < current_setting->desc.bNumEndpoints; n++) |
| 199 | if (current_setting->endpoint[n].desc.bEndpointAddress == |
| 200 | data->bulk_in) |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 201 | max_size = usb_endpoint_maxp(¤t_setting->endpoint[n].desc); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 202 | |
| 203 | if (max_size == 0) { |
| 204 | dev_err(dev, "Couldn't get wMaxPacketSize\n"); |
| 205 | rv = -EPERM; |
| 206 | goto exit; |
| 207 | } |
| 208 | |
| 209 | dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size); |
| 210 | |
| 211 | n = 0; |
| 212 | |
| 213 | do { |
| 214 | dev_dbg(dev, "Reading from bulk in EP\n"); |
| 215 | |
| 216 | rv = usb_bulk_msg(data->usb_dev, |
| 217 | usb_rcvbulkpipe(data->usb_dev, |
| 218 | data->bulk_in), |
| 219 | buffer, USBTMC_SIZE_IOBUFFER, |
| 220 | &actual, USBTMC_TIMEOUT); |
| 221 | |
| 222 | n++; |
| 223 | |
| 224 | if (rv < 0) { |
| 225 | dev_err(dev, "usb_bulk_msg returned %d\n", rv); |
| 226 | goto exit; |
| 227 | } |
| 228 | } while ((actual == max_size) && |
| 229 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)); |
| 230 | |
| 231 | if (actual == max_size) { |
| 232 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", |
| 233 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); |
| 234 | rv = -EPERM; |
| 235 | goto exit; |
| 236 | } |
| 237 | |
| 238 | n = 0; |
| 239 | |
| 240 | usbtmc_abort_bulk_in_status: |
| 241 | rv = usb_control_msg(data->usb_dev, |
| 242 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 243 | USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS, |
| 244 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 245 | 0, data->bulk_in, buffer, 0x08, |
| 246 | USBTMC_TIMEOUT); |
| 247 | |
| 248 | if (rv < 0) { |
| 249 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 250 | goto exit; |
| 251 | } |
| 252 | |
| 253 | dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]); |
| 254 | |
| 255 | if (buffer[0] == USBTMC_STATUS_SUCCESS) { |
| 256 | rv = 0; |
| 257 | goto exit; |
| 258 | } |
| 259 | |
| 260 | if (buffer[0] != USBTMC_STATUS_PENDING) { |
| 261 | dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]); |
| 262 | rv = -EPERM; |
| 263 | goto exit; |
| 264 | } |
| 265 | |
| 266 | if (buffer[1] == 1) |
| 267 | do { |
| 268 | dev_dbg(dev, "Reading from bulk in EP\n"); |
| 269 | |
| 270 | rv = usb_bulk_msg(data->usb_dev, |
| 271 | usb_rcvbulkpipe(data->usb_dev, |
| 272 | data->bulk_in), |
| 273 | buffer, USBTMC_SIZE_IOBUFFER, |
| 274 | &actual, USBTMC_TIMEOUT); |
| 275 | |
| 276 | n++; |
| 277 | |
| 278 | if (rv < 0) { |
| 279 | dev_err(dev, "usb_bulk_msg returned %d\n", rv); |
| 280 | goto exit; |
| 281 | } |
Maxim Nikulin | 4f1a7a3 | 2011-07-09 23:44:44 +0700 | [diff] [blame] | 282 | } while ((actual == max_size) && |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 283 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)); |
| 284 | |
| 285 | if (actual == max_size) { |
| 286 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", |
| 287 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); |
| 288 | rv = -EPERM; |
| 289 | goto exit; |
| 290 | } |
| 291 | |
| 292 | goto usbtmc_abort_bulk_in_status; |
| 293 | |
| 294 | exit: |
| 295 | kfree(buffer); |
| 296 | return rv; |
| 297 | |
| 298 | } |
| 299 | |
| 300 | static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data) |
| 301 | { |
| 302 | struct device *dev; |
| 303 | u8 *buffer; |
| 304 | int rv; |
| 305 | int n; |
| 306 | |
| 307 | dev = &data->intf->dev; |
| 308 | |
| 309 | buffer = kmalloc(8, GFP_KERNEL); |
| 310 | if (!buffer) |
| 311 | return -ENOMEM; |
| 312 | |
| 313 | rv = usb_control_msg(data->usb_dev, |
| 314 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 315 | USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT, |
| 316 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 317 | data->bTag_last_write, data->bulk_out, |
| 318 | buffer, 2, USBTMC_TIMEOUT); |
| 319 | |
| 320 | if (rv < 0) { |
| 321 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 322 | goto exit; |
| 323 | } |
| 324 | |
| 325 | dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]); |
| 326 | |
| 327 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 328 | dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", |
| 329 | buffer[0]); |
| 330 | rv = -EPERM; |
| 331 | goto exit; |
| 332 | } |
| 333 | |
| 334 | n = 0; |
| 335 | |
| 336 | usbtmc_abort_bulk_out_check_status: |
| 337 | rv = usb_control_msg(data->usb_dev, |
| 338 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 339 | USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS, |
| 340 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 341 | 0, data->bulk_out, buffer, 0x08, |
| 342 | USBTMC_TIMEOUT); |
| 343 | n++; |
| 344 | if (rv < 0) { |
| 345 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 346 | goto exit; |
| 347 | } |
| 348 | |
| 349 | dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]); |
| 350 | |
| 351 | if (buffer[0] == USBTMC_STATUS_SUCCESS) |
| 352 | goto usbtmc_abort_bulk_out_clear_halt; |
| 353 | |
| 354 | if ((buffer[0] == USBTMC_STATUS_PENDING) && |
| 355 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)) |
| 356 | goto usbtmc_abort_bulk_out_check_status; |
| 357 | |
| 358 | rv = -EPERM; |
| 359 | goto exit; |
| 360 | |
| 361 | usbtmc_abort_bulk_out_clear_halt: |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 362 | rv = usb_clear_halt(data->usb_dev, |
| 363 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 364 | |
| 365 | if (rv < 0) { |
| 366 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 367 | goto exit; |
| 368 | } |
| 369 | rv = 0; |
| 370 | |
| 371 | exit: |
| 372 | kfree(buffer); |
| 373 | return rv; |
| 374 | } |
| 375 | |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 376 | /* |
| 377 | * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint. |
| 378 | * @transfer_size: number of bytes to request from the device. |
| 379 | * |
| 380 | * See the USBTMC specification, Table 4. |
| 381 | * |
| 382 | * Also updates bTag_last_write. |
| 383 | */ |
| 384 | static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size) |
| 385 | { |
| 386 | int retval; |
| 387 | u8 buffer[USBTMC_HEADER_SIZE]; |
| 388 | int actual; |
| 389 | |
| 390 | /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message |
| 391 | * Refer to class specs for details |
| 392 | */ |
| 393 | buffer[0] = 2; |
| 394 | buffer[1] = data->bTag; |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 395 | buffer[2] = ~data->bTag; |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 396 | buffer[3] = 0; /* Reserved */ |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 397 | buffer[4] = transfer_size >> 0; |
| 398 | buffer[5] = transfer_size >> 8; |
| 399 | buffer[6] = transfer_size >> 16; |
| 400 | buffer[7] = transfer_size >> 24; |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 401 | buffer[8] = data->TermCharEnabled * 2; |
| 402 | /* Use term character? */ |
| 403 | buffer[9] = data->TermChar; |
| 404 | buffer[10] = 0; /* Reserved */ |
| 405 | buffer[11] = 0; /* Reserved */ |
| 406 | |
| 407 | /* Send bulk URB */ |
| 408 | retval = usb_bulk_msg(data->usb_dev, |
| 409 | usb_sndbulkpipe(data->usb_dev, |
| 410 | data->bulk_out), |
| 411 | buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT); |
| 412 | |
| 413 | /* Store bTag (in case we need to abort) */ |
| 414 | data->bTag_last_write = data->bTag; |
| 415 | |
| 416 | /* Increment bTag -- and increment again if zero */ |
| 417 | data->bTag++; |
| 418 | if (!data->bTag) |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 419 | data->bTag++; |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 420 | |
| 421 | if (retval < 0) { |
| 422 | dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval); |
| 423 | return retval; |
| 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 429 | static ssize_t usbtmc_read(struct file *filp, char __user *buf, |
| 430 | size_t count, loff_t *f_pos) |
| 431 | { |
| 432 | struct usbtmc_device_data *data; |
| 433 | struct device *dev; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 434 | u32 n_characters; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 435 | u8 *buffer; |
| 436 | int actual; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 437 | size_t done; |
| 438 | size_t remaining; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 439 | int retval; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 440 | size_t this_part; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 441 | |
| 442 | /* Get pointer to private data structure */ |
| 443 | data = filp->private_data; |
| 444 | dev = &data->intf->dev; |
| 445 | |
| 446 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 447 | if (!buffer) |
| 448 | return -ENOMEM; |
| 449 | |
| 450 | mutex_lock(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 451 | if (data->zombie) { |
| 452 | retval = -ENODEV; |
| 453 | goto exit; |
| 454 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 455 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 456 | if (data->rigol_quirk) { |
| 457 | dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 458 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 459 | retval = send_request_dev_dep_msg_in(data, count); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 460 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 461 | if (retval < 0) { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 462 | if (data->auto_abort) |
| 463 | usbtmc_ioctl_abort_bulk_out(data); |
| 464 | goto exit; |
| 465 | } |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* Loop until we have fetched everything we requested */ |
| 469 | remaining = count; |
| 470 | this_part = remaining; |
| 471 | done = 0; |
| 472 | |
| 473 | while (remaining > 0) { |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 474 | if (!data->rigol_quirk) { |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 475 | dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count); |
| 476 | |
| 477 | if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3) |
| 478 | this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3; |
| 479 | else |
| 480 | this_part = remaining; |
| 481 | |
| 482 | retval = send_request_dev_dep_msg_in(data, this_part); |
| 483 | if (retval < 0) { |
| 484 | dev_err(dev, "usb_bulk_msg returned %d\n", retval); |
| 485 | if (data->auto_abort) |
| 486 | usbtmc_ioctl_abort_bulk_out(data); |
| 487 | goto exit; |
| 488 | } |
| 489 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 490 | |
| 491 | /* Send bulk URB */ |
| 492 | retval = usb_bulk_msg(data->usb_dev, |
| 493 | usb_rcvbulkpipe(data->usb_dev, |
| 494 | data->bulk_in), |
| 495 | buffer, USBTMC_SIZE_IOBUFFER, &actual, |
| 496 | USBTMC_TIMEOUT); |
| 497 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 498 | dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual); |
| 499 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 500 | /* Store bTag (in case we need to abort) */ |
| 501 | data->bTag_last_read = data->bTag; |
| 502 | |
| 503 | if (retval < 0) { |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 504 | dev_dbg(dev, "Unable to read data, error %d\n", retval); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 505 | if (data->auto_abort) |
| 506 | usbtmc_ioctl_abort_bulk_in(data); |
| 507 | goto exit; |
| 508 | } |
| 509 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 510 | /* Parse header in first packet */ |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 511 | if ((done == 0) || !data->rigol_quirk) { |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 512 | /* Sanity checks for the header */ |
| 513 | if (actual < USBTMC_HEADER_SIZE) { |
| 514 | dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE); |
| 515 | if (data->auto_abort) |
| 516 | usbtmc_ioctl_abort_bulk_in(data); |
| 517 | goto exit; |
| 518 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 519 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 520 | if (buffer[0] != 2) { |
| 521 | dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]); |
| 522 | if (data->auto_abort) |
| 523 | usbtmc_ioctl_abort_bulk_in(data); |
| 524 | goto exit; |
| 525 | } |
| 526 | |
| 527 | if (buffer[1] != data->bTag_last_write) { |
| 528 | dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write); |
| 529 | if (data->auto_abort) |
| 530 | usbtmc_ioctl_abort_bulk_in(data); |
| 531 | goto exit; |
| 532 | } |
| 533 | |
| 534 | /* How many characters did the instrument send? */ |
| 535 | n_characters = buffer[4] + |
| 536 | (buffer[5] << 8) + |
| 537 | (buffer[6] << 16) + |
| 538 | (buffer[7] << 24); |
| 539 | |
| 540 | if (n_characters > this_part) { |
| 541 | dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count); |
| 542 | if (data->auto_abort) |
| 543 | usbtmc_ioctl_abort_bulk_in(data); |
| 544 | goto exit; |
| 545 | } |
| 546 | |
| 547 | /* Remove the USBTMC header */ |
| 548 | actual -= USBTMC_HEADER_SIZE; |
| 549 | |
| 550 | /* Check if the message is smaller than requested */ |
| 551 | if (data->rigol_quirk) { |
| 552 | if (remaining > n_characters) |
| 553 | remaining = n_characters; |
| 554 | /* Remove padding if it exists */ |
Andy Shevchenko | 92f78dd | 2013-07-15 11:59:40 +0300 | [diff] [blame] | 555 | if (actual > remaining) |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 556 | actual = remaining; |
| 557 | } |
| 558 | else { |
| 559 | if (this_part > n_characters) |
| 560 | this_part = n_characters; |
| 561 | /* Remove padding if it exists */ |
Andy Shevchenko | 92f78dd | 2013-07-15 11:59:40 +0300 | [diff] [blame] | 562 | if (actual > this_part) |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 563 | actual = this_part; |
| 564 | } |
| 565 | |
| 566 | dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]); |
| 567 | |
| 568 | remaining -= actual; |
| 569 | |
| 570 | /* Terminate if end-of-message bit received from device */ |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 571 | if ((buffer[8] & 0x01) && (actual >= n_characters)) |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 572 | remaining = 0; |
| 573 | |
| 574 | dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done); |
| 575 | |
| 576 | |
| 577 | /* Copy buffer to user space */ |
| 578 | if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) { |
| 579 | /* There must have been an addressing problem */ |
| 580 | retval = -EFAULT; |
| 581 | goto exit; |
| 582 | } |
| 583 | done += actual; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 584 | } |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 585 | else { |
Andy Shevchenko | 92f78dd | 2013-07-15 11:59:40 +0300 | [diff] [blame] | 586 | if (actual > remaining) |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 587 | actual = remaining; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 588 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 589 | remaining -= actual; |
| 590 | |
| 591 | dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer); |
| 592 | |
| 593 | /* Copy buffer to user space */ |
| 594 | if (copy_to_user(buf + done, buffer, actual)) { |
| 595 | /* There must have been an addressing problem */ |
| 596 | retval = -EFAULT; |
| 597 | goto exit; |
| 598 | } |
| 599 | done += actual; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 600 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* Update file position value */ |
| 604 | *f_pos = *f_pos + done; |
| 605 | retval = done; |
| 606 | |
| 607 | exit: |
| 608 | mutex_unlock(&data->io_mutex); |
| 609 | kfree(buffer); |
| 610 | return retval; |
| 611 | } |
| 612 | |
| 613 | static ssize_t usbtmc_write(struct file *filp, const char __user *buf, |
| 614 | size_t count, loff_t *f_pos) |
| 615 | { |
| 616 | struct usbtmc_device_data *data; |
| 617 | u8 *buffer; |
| 618 | int retval; |
| 619 | int actual; |
| 620 | unsigned long int n_bytes; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 621 | int remaining; |
| 622 | int done; |
| 623 | int this_part; |
| 624 | |
| 625 | data = filp->private_data; |
| 626 | |
| 627 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 628 | if (!buffer) |
| 629 | return -ENOMEM; |
| 630 | |
| 631 | mutex_lock(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 632 | if (data->zombie) { |
| 633 | retval = -ENODEV; |
| 634 | goto exit; |
| 635 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 636 | |
| 637 | remaining = count; |
| 638 | done = 0; |
| 639 | |
| 640 | while (remaining > 0) { |
Alexandre Peixoto Ferreira | 50c9ba3 | 2013-04-30 00:51:54 -0500 | [diff] [blame] | 641 | if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) { |
| 642 | this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 643 | buffer[8] = 0; |
| 644 | } else { |
| 645 | this_part = remaining; |
| 646 | buffer[8] = 1; |
| 647 | } |
| 648 | |
| 649 | /* Setup IO buffer for DEV_DEP_MSG_OUT message */ |
| 650 | buffer[0] = 1; |
| 651 | buffer[1] = data->bTag; |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 652 | buffer[2] = ~data->bTag; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 653 | buffer[3] = 0; /* Reserved */ |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 654 | buffer[4] = this_part >> 0; |
| 655 | buffer[5] = this_part >> 8; |
| 656 | buffer[6] = this_part >> 16; |
| 657 | buffer[7] = this_part >> 24; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 658 | /* buffer[8] is set above... */ |
| 659 | buffer[9] = 0; /* Reserved */ |
| 660 | buffer[10] = 0; /* Reserved */ |
| 661 | buffer[11] = 0; /* Reserved */ |
| 662 | |
Alexandre Peixoto Ferreira | 50c9ba3 | 2013-04-30 00:51:54 -0500 | [diff] [blame] | 663 | if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 664 | retval = -EFAULT; |
| 665 | goto exit; |
| 666 | } |
| 667 | |
Alexandre Peixoto Ferreira | 50c9ba3 | 2013-04-30 00:51:54 -0500 | [diff] [blame] | 668 | n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4); |
| 669 | memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 670 | |
Andre Herms | ec412b9 | 2009-11-19 18:14:49 +0100 | [diff] [blame] | 671 | do { |
| 672 | retval = usb_bulk_msg(data->usb_dev, |
| 673 | usb_sndbulkpipe(data->usb_dev, |
| 674 | data->bulk_out), |
| 675 | buffer, n_bytes, |
| 676 | &actual, USBTMC_TIMEOUT); |
| 677 | if (retval != 0) |
| 678 | break; |
| 679 | n_bytes -= actual; |
| 680 | } while (n_bytes); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 681 | |
| 682 | data->bTag_last_write = data->bTag; |
| 683 | data->bTag++; |
| 684 | |
| 685 | if (!data->bTag) |
| 686 | data->bTag++; |
| 687 | |
| 688 | if (retval < 0) { |
| 689 | dev_err(&data->intf->dev, |
| 690 | "Unable to send data, error %d\n", retval); |
| 691 | if (data->auto_abort) |
| 692 | usbtmc_ioctl_abort_bulk_out(data); |
| 693 | goto exit; |
| 694 | } |
| 695 | |
| 696 | remaining -= this_part; |
| 697 | done += this_part; |
| 698 | } |
| 699 | |
| 700 | retval = count; |
| 701 | exit: |
| 702 | mutex_unlock(&data->io_mutex); |
| 703 | kfree(buffer); |
| 704 | return retval; |
| 705 | } |
| 706 | |
| 707 | static int usbtmc_ioctl_clear(struct usbtmc_device_data *data) |
| 708 | { |
| 709 | struct usb_host_interface *current_setting; |
| 710 | struct usb_endpoint_descriptor *desc; |
| 711 | struct device *dev; |
| 712 | u8 *buffer; |
| 713 | int rv; |
| 714 | int n; |
| 715 | int actual; |
| 716 | int max_size; |
| 717 | |
| 718 | dev = &data->intf->dev; |
| 719 | |
| 720 | dev_dbg(dev, "Sending INITIATE_CLEAR request\n"); |
| 721 | |
| 722 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 723 | if (!buffer) |
| 724 | return -ENOMEM; |
| 725 | |
| 726 | rv = usb_control_msg(data->usb_dev, |
| 727 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 728 | USBTMC_REQUEST_INITIATE_CLEAR, |
| 729 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 730 | 0, 0, buffer, 1, USBTMC_TIMEOUT); |
| 731 | if (rv < 0) { |
| 732 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 733 | goto exit; |
| 734 | } |
| 735 | |
| 736 | dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]); |
| 737 | |
| 738 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 739 | dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]); |
| 740 | rv = -EPERM; |
| 741 | goto exit; |
| 742 | } |
| 743 | |
| 744 | max_size = 0; |
| 745 | current_setting = data->intf->cur_altsetting; |
| 746 | for (n = 0; n < current_setting->desc.bNumEndpoints; n++) { |
| 747 | desc = ¤t_setting->endpoint[n].desc; |
| 748 | if (desc->bEndpointAddress == data->bulk_in) |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 749 | max_size = usb_endpoint_maxp(desc); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | if (max_size == 0) { |
| 753 | dev_err(dev, "Couldn't get wMaxPacketSize\n"); |
| 754 | rv = -EPERM; |
| 755 | goto exit; |
| 756 | } |
| 757 | |
| 758 | dev_dbg(dev, "wMaxPacketSize is %d\n", max_size); |
| 759 | |
| 760 | n = 0; |
| 761 | |
| 762 | usbtmc_clear_check_status: |
| 763 | |
| 764 | dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n"); |
| 765 | |
| 766 | rv = usb_control_msg(data->usb_dev, |
| 767 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 768 | USBTMC_REQUEST_CHECK_CLEAR_STATUS, |
| 769 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 770 | 0, 0, buffer, 2, USBTMC_TIMEOUT); |
| 771 | if (rv < 0) { |
| 772 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 773 | goto exit; |
| 774 | } |
| 775 | |
| 776 | dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]); |
| 777 | |
| 778 | if (buffer[0] == USBTMC_STATUS_SUCCESS) |
| 779 | goto usbtmc_clear_bulk_out_halt; |
| 780 | |
| 781 | if (buffer[0] != USBTMC_STATUS_PENDING) { |
| 782 | dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]); |
| 783 | rv = -EPERM; |
| 784 | goto exit; |
| 785 | } |
| 786 | |
| 787 | if (buffer[1] == 1) |
| 788 | do { |
| 789 | dev_dbg(dev, "Reading from bulk in EP\n"); |
| 790 | |
| 791 | rv = usb_bulk_msg(data->usb_dev, |
| 792 | usb_rcvbulkpipe(data->usb_dev, |
| 793 | data->bulk_in), |
| 794 | buffer, USBTMC_SIZE_IOBUFFER, |
| 795 | &actual, USBTMC_TIMEOUT); |
| 796 | n++; |
| 797 | |
| 798 | if (rv < 0) { |
| 799 | dev_err(dev, "usb_control_msg returned %d\n", |
| 800 | rv); |
| 801 | goto exit; |
| 802 | } |
| 803 | } while ((actual == max_size) && |
| 804 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)); |
| 805 | |
| 806 | if (actual == max_size) { |
| 807 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", |
| 808 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); |
| 809 | rv = -EPERM; |
| 810 | goto exit; |
| 811 | } |
| 812 | |
| 813 | goto usbtmc_clear_check_status; |
| 814 | |
| 815 | usbtmc_clear_bulk_out_halt: |
| 816 | |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 817 | rv = usb_clear_halt(data->usb_dev, |
| 818 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 819 | if (rv < 0) { |
| 820 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 821 | goto exit; |
| 822 | } |
| 823 | rv = 0; |
| 824 | |
| 825 | exit: |
| 826 | kfree(buffer); |
| 827 | return rv; |
| 828 | } |
| 829 | |
| 830 | static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data) |
| 831 | { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 832 | int rv; |
| 833 | |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 834 | rv = usb_clear_halt(data->usb_dev, |
| 835 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 836 | |
| 837 | if (rv < 0) { |
| 838 | dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n", |
| 839 | rv); |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 840 | return rv; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 841 | } |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 842 | return 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data) |
| 846 | { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 847 | int rv; |
| 848 | |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 849 | rv = usb_clear_halt(data->usb_dev, |
| 850 | usb_rcvbulkpipe(data->usb_dev, data->bulk_in)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 851 | |
| 852 | if (rv < 0) { |
| 853 | dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n", |
| 854 | rv); |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 855 | return rv; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 856 | } |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 857 | return 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | static int get_capabilities(struct usbtmc_device_data *data) |
| 861 | { |
| 862 | struct device *dev = &data->usb_dev->dev; |
| 863 | char *buffer; |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 864 | int rv = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 865 | |
| 866 | buffer = kmalloc(0x18, GFP_KERNEL); |
| 867 | if (!buffer) |
| 868 | return -ENOMEM; |
| 869 | |
| 870 | rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0), |
| 871 | USBTMC_REQUEST_GET_CAPABILITIES, |
| 872 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 873 | 0, 0, buffer, 0x18, USBTMC_TIMEOUT); |
| 874 | if (rv < 0) { |
| 875 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 876 | goto err_out; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 880 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 881 | dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]); |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 882 | rv = -EPERM; |
| 883 | goto err_out; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 884 | } |
Gergely Imreh | d0a3836 | 2009-09-07 10:47:01 +0800 | [diff] [blame] | 885 | dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]); |
| 886 | dev_dbg(dev, "Device capabilities are %x\n", buffer[5]); |
| 887 | dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]); |
| 888 | dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 889 | |
| 890 | data->capabilities.interface_capabilities = buffer[4]; |
| 891 | data->capabilities.device_capabilities = buffer[5]; |
| 892 | data->capabilities.usb488_interface_capabilities = buffer[14]; |
| 893 | data->capabilities.usb488_device_capabilities = buffer[15]; |
Gergely Imreh | d0a3836 | 2009-09-07 10:47:01 +0800 | [diff] [blame] | 894 | rv = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 895 | |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 896 | err_out: |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 897 | kfree(buffer); |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 898 | return rv; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | #define capability_attribute(name) \ |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 902 | static ssize_t name##_show(struct device *dev, \ |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 903 | struct device_attribute *attr, char *buf) \ |
| 904 | { \ |
| 905 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 906 | struct usbtmc_device_data *data = usb_get_intfdata(intf); \ |
| 907 | \ |
| 908 | return sprintf(buf, "%d\n", data->capabilities.name); \ |
| 909 | } \ |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 910 | static DEVICE_ATTR_RO(name) |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 911 | |
| 912 | capability_attribute(interface_capabilities); |
| 913 | capability_attribute(device_capabilities); |
| 914 | capability_attribute(usb488_interface_capabilities); |
| 915 | capability_attribute(usb488_device_capabilities); |
| 916 | |
| 917 | static struct attribute *capability_attrs[] = { |
| 918 | &dev_attr_interface_capabilities.attr, |
| 919 | &dev_attr_device_capabilities.attr, |
| 920 | &dev_attr_usb488_interface_capabilities.attr, |
| 921 | &dev_attr_usb488_device_capabilities.attr, |
| 922 | NULL, |
| 923 | }; |
| 924 | |
| 925 | static struct attribute_group capability_attr_grp = { |
| 926 | .attrs = capability_attrs, |
| 927 | }; |
| 928 | |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 929 | static ssize_t TermChar_show(struct device *dev, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 930 | struct device_attribute *attr, char *buf) |
| 931 | { |
| 932 | struct usb_interface *intf = to_usb_interface(dev); |
| 933 | struct usbtmc_device_data *data = usb_get_intfdata(intf); |
| 934 | |
| 935 | return sprintf(buf, "%c\n", data->TermChar); |
| 936 | } |
| 937 | |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 938 | static ssize_t TermChar_store(struct device *dev, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 939 | struct device_attribute *attr, |
| 940 | const char *buf, size_t count) |
| 941 | { |
| 942 | struct usb_interface *intf = to_usb_interface(dev); |
| 943 | struct usbtmc_device_data *data = usb_get_intfdata(intf); |
| 944 | |
| 945 | if (count < 1) |
| 946 | return -EINVAL; |
| 947 | data->TermChar = buf[0]; |
| 948 | return count; |
| 949 | } |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 950 | static DEVICE_ATTR_RW(TermChar); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 951 | |
| 952 | #define data_attribute(name) \ |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 953 | static ssize_t name##_show(struct device *dev, \ |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 954 | struct device_attribute *attr, char *buf) \ |
| 955 | { \ |
| 956 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 957 | struct usbtmc_device_data *data = usb_get_intfdata(intf); \ |
| 958 | \ |
| 959 | return sprintf(buf, "%d\n", data->name); \ |
| 960 | } \ |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 961 | static ssize_t name##_store(struct device *dev, \ |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 962 | struct device_attribute *attr, \ |
| 963 | const char *buf, size_t count) \ |
| 964 | { \ |
| 965 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 966 | struct usbtmc_device_data *data = usb_get_intfdata(intf); \ |
| 967 | ssize_t result; \ |
| 968 | unsigned val; \ |
| 969 | \ |
| 970 | result = sscanf(buf, "%u\n", &val); \ |
| 971 | if (result != 1) \ |
| 972 | result = -EINVAL; \ |
| 973 | data->name = val; \ |
| 974 | if (result < 0) \ |
| 975 | return result; \ |
| 976 | else \ |
| 977 | return count; \ |
| 978 | } \ |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 979 | static DEVICE_ATTR_RW(name) |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 980 | |
| 981 | data_attribute(TermCharEnabled); |
| 982 | data_attribute(auto_abort); |
| 983 | |
| 984 | static struct attribute *data_attrs[] = { |
| 985 | &dev_attr_TermChar.attr, |
| 986 | &dev_attr_TermCharEnabled.attr, |
| 987 | &dev_attr_auto_abort.attr, |
| 988 | NULL, |
| 989 | }; |
| 990 | |
| 991 | static struct attribute_group data_attr_grp = { |
| 992 | .attrs = data_attrs, |
| 993 | }; |
| 994 | |
| 995 | static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data) |
| 996 | { |
| 997 | struct device *dev; |
| 998 | u8 *buffer; |
| 999 | int rv; |
| 1000 | |
| 1001 | dev = &data->intf->dev; |
| 1002 | |
| 1003 | buffer = kmalloc(2, GFP_KERNEL); |
| 1004 | if (!buffer) |
| 1005 | return -ENOMEM; |
| 1006 | |
| 1007 | rv = usb_control_msg(data->usb_dev, |
| 1008 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 1009 | USBTMC_REQUEST_INDICATOR_PULSE, |
| 1010 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 1011 | 0, 0, buffer, 0x01, USBTMC_TIMEOUT); |
| 1012 | |
| 1013 | if (rv < 0) { |
| 1014 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 1015 | goto exit; |
| 1016 | } |
| 1017 | |
| 1018 | dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]); |
| 1019 | |
| 1020 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 1021 | dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]); |
| 1022 | rv = -EPERM; |
| 1023 | goto exit; |
| 1024 | } |
| 1025 | rv = 0; |
| 1026 | |
| 1027 | exit: |
| 1028 | kfree(buffer); |
| 1029 | return rv; |
| 1030 | } |
| 1031 | |
| 1032 | static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1033 | { |
| 1034 | struct usbtmc_device_data *data; |
| 1035 | int retval = -EBADRQC; |
| 1036 | |
| 1037 | data = file->private_data; |
| 1038 | mutex_lock(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 1039 | if (data->zombie) { |
| 1040 | retval = -ENODEV; |
| 1041 | goto skip_io_on_zombie; |
| 1042 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1043 | |
| 1044 | switch (cmd) { |
| 1045 | case USBTMC_IOCTL_CLEAR_OUT_HALT: |
| 1046 | retval = usbtmc_ioctl_clear_out_halt(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1047 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1048 | |
| 1049 | case USBTMC_IOCTL_CLEAR_IN_HALT: |
| 1050 | retval = usbtmc_ioctl_clear_in_halt(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1051 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1052 | |
| 1053 | case USBTMC_IOCTL_INDICATOR_PULSE: |
| 1054 | retval = usbtmc_ioctl_indicator_pulse(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1055 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1056 | |
| 1057 | case USBTMC_IOCTL_CLEAR: |
| 1058 | retval = usbtmc_ioctl_clear(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1059 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1060 | |
| 1061 | case USBTMC_IOCTL_ABORT_BULK_OUT: |
| 1062 | retval = usbtmc_ioctl_abort_bulk_out(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1063 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1064 | |
| 1065 | case USBTMC_IOCTL_ABORT_BULK_IN: |
| 1066 | retval = usbtmc_ioctl_abort_bulk_in(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1067 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 1070 | skip_io_on_zombie: |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1071 | mutex_unlock(&data->io_mutex); |
| 1072 | return retval; |
| 1073 | } |
| 1074 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1075 | static const struct file_operations fops = { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1076 | .owner = THIS_MODULE, |
| 1077 | .read = usbtmc_read, |
| 1078 | .write = usbtmc_write, |
| 1079 | .open = usbtmc_open, |
| 1080 | .release = usbtmc_release, |
| 1081 | .unlocked_ioctl = usbtmc_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1082 | .llseek = default_llseek, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1083 | }; |
| 1084 | |
| 1085 | static struct usb_class_driver usbtmc_class = { |
| 1086 | .name = "usbtmc%d", |
| 1087 | .fops = &fops, |
| 1088 | .minor_base = USBTMC_MINOR_BASE, |
| 1089 | }; |
| 1090 | |
| 1091 | |
| 1092 | static int usbtmc_probe(struct usb_interface *intf, |
| 1093 | const struct usb_device_id *id) |
| 1094 | { |
| 1095 | struct usbtmc_device_data *data; |
| 1096 | struct usb_host_interface *iface_desc; |
| 1097 | struct usb_endpoint_descriptor *endpoint; |
| 1098 | int n; |
| 1099 | int retcode; |
| 1100 | |
| 1101 | dev_dbg(&intf->dev, "%s called\n", __func__); |
| 1102 | |
Andy Shevchenko | e6c7efdc | 2013-07-15 11:59:41 +0300 | [diff] [blame] | 1103 | data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1104 | if (!data) { |
| 1105 | dev_err(&intf->dev, "Unable to allocate kernel memory\n"); |
| 1106 | return -ENOMEM; |
| 1107 | } |
| 1108 | |
| 1109 | data->intf = intf; |
| 1110 | data->id = id; |
| 1111 | data->usb_dev = usb_get_dev(interface_to_usbdev(intf)); |
| 1112 | usb_set_intfdata(intf, data); |
| 1113 | kref_init(&data->kref); |
| 1114 | mutex_init(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 1115 | data->zombie = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1116 | |
Alexandre Peixoto Ferreira | c2e3148 | 2013-04-30 00:51:52 -0500 | [diff] [blame] | 1117 | /* Determine if it is a Rigol or not */ |
| 1118 | data->rigol_quirk = 0; |
| 1119 | dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n", |
Johan Hovold | c786138 | 2013-08-11 16:49:21 +0200 | [diff] [blame] | 1120 | le16_to_cpu(data->usb_dev->descriptor.idVendor), |
| 1121 | le16_to_cpu(data->usb_dev->descriptor.idProduct)); |
Alexandre Peixoto Ferreira | c2e3148 | 2013-04-30 00:51:52 -0500 | [diff] [blame] | 1122 | for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) { |
Johan Hovold | c786138 | 2013-08-11 16:49:21 +0200 | [diff] [blame] | 1123 | if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) && |
| 1124 | (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) { |
Alexandre Peixoto Ferreira | c2e3148 | 2013-04-30 00:51:52 -0500 | [diff] [blame] | 1125 | dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n"); |
| 1126 | data->rigol_quirk = 1; |
| 1127 | break; |
| 1128 | } |
| 1129 | } |
| 1130 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1131 | /* Initialize USBTMC bTag and other fields */ |
| 1132 | data->bTag = 1; |
| 1133 | data->TermCharEnabled = 0; |
| 1134 | data->TermChar = '\n'; |
| 1135 | |
| 1136 | /* USBTMC devices have only one setting, so use that */ |
| 1137 | iface_desc = data->intf->cur_altsetting; |
| 1138 | |
| 1139 | /* Find bulk in endpoint */ |
| 1140 | for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) { |
| 1141 | endpoint = &iface_desc->endpoint[n].desc; |
| 1142 | |
| 1143 | if (usb_endpoint_is_bulk_in(endpoint)) { |
| 1144 | data->bulk_in = endpoint->bEndpointAddress; |
| 1145 | dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n", |
| 1146 | data->bulk_in); |
| 1147 | break; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | /* Find bulk out endpoint */ |
| 1152 | for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) { |
| 1153 | endpoint = &iface_desc->endpoint[n].desc; |
| 1154 | |
| 1155 | if (usb_endpoint_is_bulk_out(endpoint)) { |
| 1156 | data->bulk_out = endpoint->bEndpointAddress; |
| 1157 | dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n", |
| 1158 | data->bulk_out); |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | retcode = get_capabilities(data); |
| 1164 | if (retcode) |
| 1165 | dev_err(&intf->dev, "can't read capabilities\n"); |
| 1166 | else |
| 1167 | retcode = sysfs_create_group(&intf->dev.kobj, |
| 1168 | &capability_attr_grp); |
| 1169 | |
| 1170 | retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp); |
| 1171 | |
| 1172 | retcode = usb_register_dev(intf, &usbtmc_class); |
| 1173 | if (retcode) { |
| 1174 | dev_err(&intf->dev, "Not able to get a minor" |
| 1175 | " (base %u, slice default): %d\n", USBTMC_MINOR_BASE, |
| 1176 | retcode); |
| 1177 | goto error_register; |
| 1178 | } |
| 1179 | dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor); |
| 1180 | |
| 1181 | return 0; |
| 1182 | |
| 1183 | error_register: |
| 1184 | sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp); |
| 1185 | sysfs_remove_group(&intf->dev.kobj, &data_attr_grp); |
| 1186 | kref_put(&data->kref, usbtmc_delete); |
| 1187 | return retcode; |
| 1188 | } |
| 1189 | |
| 1190 | static void usbtmc_disconnect(struct usb_interface *intf) |
| 1191 | { |
| 1192 | struct usbtmc_device_data *data; |
| 1193 | |
| 1194 | dev_dbg(&intf->dev, "usbtmc_disconnect called\n"); |
| 1195 | |
| 1196 | data = usb_get_intfdata(intf); |
| 1197 | usb_deregister_dev(intf, &usbtmc_class); |
| 1198 | sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp); |
| 1199 | sysfs_remove_group(&intf->dev.kobj, &data_attr_grp); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 1200 | mutex_lock(&data->io_mutex); |
| 1201 | data->zombie = 1; |
| 1202 | mutex_unlock(&data->io_mutex); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1203 | kref_put(&data->kref, usbtmc_delete); |
| 1204 | } |
| 1205 | |
Oliver Neukum | dca8cd0 | 2009-09-24 00:33:45 +0200 | [diff] [blame] | 1206 | static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message) |
Oliver Neukum | a4708103 | 2009-07-02 11:44:33 +0200 | [diff] [blame] | 1207 | { |
| 1208 | /* this driver does not have pending URBs */ |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
Oliver Neukum | dca8cd0 | 2009-09-24 00:33:45 +0200 | [diff] [blame] | 1212 | static int usbtmc_resume(struct usb_interface *intf) |
Oliver Neukum | a4708103 | 2009-07-02 11:44:33 +0200 | [diff] [blame] | 1213 | { |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1217 | static struct usb_driver usbtmc_driver = { |
| 1218 | .name = "usbtmc", |
| 1219 | .id_table = usbtmc_devices, |
| 1220 | .probe = usbtmc_probe, |
Oliver Neukum | a4708103 | 2009-07-02 11:44:33 +0200 | [diff] [blame] | 1221 | .disconnect = usbtmc_disconnect, |
| 1222 | .suspend = usbtmc_suspend, |
| 1223 | .resume = usbtmc_resume, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1224 | }; |
| 1225 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 1226 | module_usb_driver(usbtmc_driver); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1227 | |
| 1228 | MODULE_LICENSE("GPL"); |