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