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