Linux Audio

Check our new training course

Loading...
v3.15
   1/**
   2 * drivers/usb/class/usbtmc.c - USB Test & Measurement 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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23
  24#include <linux/module.h>
  25#include <linux/kernel.h>
  26#include <linux/fs.h>
  27#include <linux/uaccess.h>
  28#include <linux/kref.h>
  29#include <linux/slab.h>
 
  30#include <linux/mutex.h>
  31#include <linux/usb.h>
  32#include <linux/usb/tmc.h>
  33
  34
  35#define RIGOL			1
  36#define USBTMC_HEADER_SIZE	12
  37#define USBTMC_MINOR_BASE	176
  38
  39/*
  40 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
  41 * large as wMaxPacketSize (which is usually 512 bytes).
  42 */
  43#define USBTMC_SIZE_IOBUFFER	2048
  44
  45/* Default USB timeout (in milliseconds) */
  46#define USBTMC_TIMEOUT		5000
  47
  48/*
  49 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
  50 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
  51 * packet is never read.
  52 */
  53#define USBTMC_MAX_READS_TO_CLEAR_BULK_IN	100
  54
  55static const struct usb_device_id usbtmc_devices[] = {
  56	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
  57	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
  58	{ 0, } /* terminating entry */
  59};
  60MODULE_DEVICE_TABLE(usb, usbtmc_devices);
  61
  62/*
  63 * This structure is the capabilities for the device
  64 * See section 4.2.1.8 of the USBTMC specification,
  65 * and section 4.2.2 of the USBTMC usb488 subclass
  66 * specification for details.
  67 */
  68struct usbtmc_dev_capabilities {
  69	__u8 interface_capabilities;
  70	__u8 device_capabilities;
  71	__u8 usb488_interface_capabilities;
  72	__u8 usb488_device_capabilities;
  73};
  74
  75/* This structure holds private data for each USBTMC device. One copy is
  76 * allocated for each USBTMC device in the driver's probe function.
  77 */
  78struct usbtmc_device_data {
  79	const struct usb_device_id *id;
  80	struct usb_device *usb_dev;
  81	struct usb_interface *intf;
  82
  83	unsigned int bulk_in;
  84	unsigned int bulk_out;
  85
  86	u8 bTag;
  87	u8 bTag_last_write;	/* needed for abort */
  88	u8 bTag_last_read;	/* needed for abort */
  89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  90	u8 rigol_quirk;
  91
  92	/* attributes from the USB TMC spec for this device */
  93	u8 TermChar;
  94	bool TermCharEnabled;
  95	bool auto_abort;
  96
  97	bool zombie; /* fd of disconnected device */
  98
  99	struct usbtmc_dev_capabilities	capabilities;
 100	struct kref kref;
 101	struct mutex io_mutex;	/* only one i/o function running at a time */
 
 
 102};
 103#define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
 104
 105struct usbtmc_ID_rigol_quirk {
 106	__u16 idVendor;
 107	__u16 idProduct;
 108};
 109
 110static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
 111	{ 0x1ab1, 0x0588 },
 
 112	{ 0, 0 }
 113};
 114
 115/* Forward declarations */
 116static struct usb_driver usbtmc_driver;
 117
 118static void usbtmc_delete(struct kref *kref)
 119{
 120	struct usbtmc_device_data *data = to_usbtmc_data(kref);
 121
 122	usb_put_dev(data->usb_dev);
 
 123}
 124
 125static int usbtmc_open(struct inode *inode, struct file *filp)
 126{
 127	struct usb_interface *intf;
 128	struct usbtmc_device_data *data;
 129	int retval = 0;
 130
 131	intf = usb_find_interface(&usbtmc_driver, iminor(inode));
 132	if (!intf) {
 133		pr_err("can not find device for minor %d", iminor(inode));
 134		return -ENODEV;
 135	}
 136
 137	data = usb_get_intfdata(intf);
 
 138	kref_get(&data->kref);
 139
 140	/* Store pointer in file structure's private data field */
 141	filp->private_data = data;
 142
 143	return retval;
 144}
 145
 146static int usbtmc_release(struct inode *inode, struct file *file)
 147{
 148	struct usbtmc_device_data *data = file->private_data;
 149
 150	kref_put(&data->kref, usbtmc_delete);
 151	return 0;
 152}
 153
 154static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
 155{
 156	u8 *buffer;
 157	struct device *dev;
 158	int rv;
 159	int n;
 160	int actual;
 161	struct usb_host_interface *current_setting;
 162	int max_size;
 163
 164	dev = &data->intf->dev;
 165	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
 166	if (!buffer)
 167		return -ENOMEM;
 168
 169	rv = usb_control_msg(data->usb_dev,
 170			     usb_rcvctrlpipe(data->usb_dev, 0),
 171			     USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
 172			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
 173			     data->bTag_last_read, data->bulk_in,
 174			     buffer, 2, USBTMC_TIMEOUT);
 175
 176	if (rv < 0) {
 177		dev_err(dev, "usb_control_msg returned %d\n", rv);
 178		goto exit;
 179	}
 180
 181	dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
 182
 183	if (buffer[0] == USBTMC_STATUS_FAILED) {
 184		rv = 0;
 185		goto exit;
 186	}
 187
 188	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
 189		dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
 190			buffer[0]);
 191		rv = -EPERM;
 192		goto exit;
 193	}
 194
 195	max_size = 0;
 196	current_setting = data->intf->cur_altsetting;
 197	for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
 198		if (current_setting->endpoint[n].desc.bEndpointAddress ==
 199			data->bulk_in)
 200			max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
 201
 202	if (max_size == 0) {
 203		dev_err(dev, "Couldn't get wMaxPacketSize\n");
 204		rv = -EPERM;
 205		goto exit;
 206	}
 207
 208	dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
 209
 210	n = 0;
 211
 212	do {
 213		dev_dbg(dev, "Reading from bulk in EP\n");
 214
 215		rv = usb_bulk_msg(data->usb_dev,
 216				  usb_rcvbulkpipe(data->usb_dev,
 217						  data->bulk_in),
 218				  buffer, USBTMC_SIZE_IOBUFFER,
 219				  &actual, USBTMC_TIMEOUT);
 220
 221		n++;
 222
 223		if (rv < 0) {
 224			dev_err(dev, "usb_bulk_msg returned %d\n", rv);
 225			goto exit;
 226		}
 227	} while ((actual == max_size) &&
 228		 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
 229
 230	if (actual == max_size) {
 231		dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
 232			USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
 233		rv = -EPERM;
 234		goto exit;
 235	}
 236
 237	n = 0;
 238
 239usbtmc_abort_bulk_in_status:
 240	rv = usb_control_msg(data->usb_dev,
 241			     usb_rcvctrlpipe(data->usb_dev, 0),
 242			     USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
 243			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
 244			     0, data->bulk_in, buffer, 0x08,
 245			     USBTMC_TIMEOUT);
 246
 247	if (rv < 0) {
 248		dev_err(dev, "usb_control_msg returned %d\n", rv);
 249		goto exit;
 250	}
 251
 252	dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
 253
 254	if (buffer[0] == USBTMC_STATUS_SUCCESS) {
 255		rv = 0;
 256		goto exit;
 257	}
 258
 259	if (buffer[0] != USBTMC_STATUS_PENDING) {
 260		dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
 261		rv = -EPERM;
 262		goto exit;
 263	}
 264
 265	if (buffer[1] == 1)
 266		do {
 267			dev_dbg(dev, "Reading from bulk in EP\n");
 268
 269			rv = usb_bulk_msg(data->usb_dev,
 270					  usb_rcvbulkpipe(data->usb_dev,
 271							  data->bulk_in),
 272					  buffer, USBTMC_SIZE_IOBUFFER,
 273					  &actual, USBTMC_TIMEOUT);
 274
 275			n++;
 276
 277			if (rv < 0) {
 278				dev_err(dev, "usb_bulk_msg returned %d\n", rv);
 279				goto exit;
 280			}
 281		} while ((actual == max_size) &&
 282			 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
 283
 284	if (actual == max_size) {
 285		dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
 286			USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
 287		rv = -EPERM;
 288		goto exit;
 289	}
 290
 291	goto usbtmc_abort_bulk_in_status;
 292
 293exit:
 294	kfree(buffer);
 295	return rv;
 296
 297}
 298
 299static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
 300{
 301	struct device *dev;
 302	u8 *buffer;
 303	int rv;
 304	int n;
 305
 306	dev = &data->intf->dev;
 307
 308	buffer = kmalloc(8, GFP_KERNEL);
 309	if (!buffer)
 310		return -ENOMEM;
 311
 312	rv = usb_control_msg(data->usb_dev,
 313			     usb_rcvctrlpipe(data->usb_dev, 0),
 314			     USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
 315			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
 316			     data->bTag_last_write, data->bulk_out,
 317			     buffer, 2, USBTMC_TIMEOUT);
 318
 319	if (rv < 0) {
 320		dev_err(dev, "usb_control_msg returned %d\n", rv);
 321		goto exit;
 322	}
 323
 324	dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
 325
 326	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
 327		dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
 328			buffer[0]);
 329		rv = -EPERM;
 330		goto exit;
 331	}
 332
 333	n = 0;
 334
 335usbtmc_abort_bulk_out_check_status:
 336	rv = usb_control_msg(data->usb_dev,
 337			     usb_rcvctrlpipe(data->usb_dev, 0),
 338			     USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
 339			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
 340			     0, data->bulk_out, buffer, 0x08,
 341			     USBTMC_TIMEOUT);
 342	n++;
 343	if (rv < 0) {
 344		dev_err(dev, "usb_control_msg returned %d\n", rv);
 345		goto exit;
 346	}
 347
 348	dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
 349
 350	if (buffer[0] == USBTMC_STATUS_SUCCESS)
 351		goto usbtmc_abort_bulk_out_clear_halt;
 352
 353	if ((buffer[0] == USBTMC_STATUS_PENDING) &&
 354	    (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
 355		goto usbtmc_abort_bulk_out_check_status;
 356
 357	rv = -EPERM;
 358	goto exit;
 359
 360usbtmc_abort_bulk_out_clear_halt:
 361	rv = usb_clear_halt(data->usb_dev,
 362			    usb_sndbulkpipe(data->usb_dev, data->bulk_out));
 363
 364	if (rv < 0) {
 365		dev_err(dev, "usb_control_msg returned %d\n", rv);
 366		goto exit;
 367	}
 368	rv = 0;
 369
 370exit:
 371	kfree(buffer);
 372	return rv;
 373}
 374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 375/*
 376 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
 377 * @transfer_size: number of bytes to request from the device.
 378 *
 379 * See the USBTMC specification, Table 4.
 380 *
 381 * Also updates bTag_last_write.
 382 */
 383static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
 384{
 385	int retval;
 386	u8 buffer[USBTMC_HEADER_SIZE];
 387	int actual;
 388
 
 
 
 389	/* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
 390	 * Refer to class specs for details
 391	 */
 392	buffer[0] = 2;
 393	buffer[1] = data->bTag;
 394	buffer[2] = ~data->bTag;
 395	buffer[3] = 0; /* Reserved */
 396	buffer[4] = transfer_size >> 0;
 397	buffer[5] = transfer_size >> 8;
 398	buffer[6] = transfer_size >> 16;
 399	buffer[7] = transfer_size >> 24;
 400	buffer[8] = data->TermCharEnabled * 2;
 401	/* Use term character? */
 402	buffer[9] = data->TermChar;
 403	buffer[10] = 0; /* Reserved */
 404	buffer[11] = 0; /* Reserved */
 405
 406	/* Send bulk URB */
 407	retval = usb_bulk_msg(data->usb_dev,
 408			      usb_sndbulkpipe(data->usb_dev,
 409					      data->bulk_out),
 410			      buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
 411
 412	/* Store bTag (in case we need to abort) */
 413	data->bTag_last_write = data->bTag;
 414
 415	/* Increment bTag -- and increment again if zero */
 416	data->bTag++;
 417	if (!data->bTag)
 418		data->bTag++;
 419
 
 420	if (retval < 0) {
 421		dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
 422		return retval;
 423	}
 424
 425	return 0;
 426}
 427
 428static ssize_t usbtmc_read(struct file *filp, char __user *buf,
 429			   size_t count, loff_t *f_pos)
 430{
 431	struct usbtmc_device_data *data;
 432	struct device *dev;
 433	u32 n_characters;
 434	u8 *buffer;
 435	int actual;
 436	size_t done;
 437	size_t remaining;
 438	int retval;
 439	size_t this_part;
 440
 441	/* Get pointer to private data structure */
 442	data = filp->private_data;
 443	dev = &data->intf->dev;
 444
 445	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
 446	if (!buffer)
 447		return -ENOMEM;
 448
 449	mutex_lock(&data->io_mutex);
 450	if (data->zombie) {
 451		retval = -ENODEV;
 452		goto exit;
 453	}
 454
 455	if (data->rigol_quirk) {
 456		dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
 457
 458		retval = send_request_dev_dep_msg_in(data, count);
 459
 460		if (retval < 0) {
 461			if (data->auto_abort)
 462				usbtmc_ioctl_abort_bulk_out(data);
 463			goto exit;
 464		}
 465	}
 466
 467	/* Loop until we have fetched everything we requested */
 468	remaining = count;
 469	this_part = remaining;
 470	done = 0;
 471
 472	while (remaining > 0) {
 473		if (!data->rigol_quirk) {
 474			dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
 475
 476			if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
 477				this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
 478			else
 479				this_part = remaining;
 480
 481			retval = send_request_dev_dep_msg_in(data, this_part);
 482			if (retval < 0) {
 483			dev_err(dev, "usb_bulk_msg returned %d\n", retval);
 484				if (data->auto_abort)
 485					usbtmc_ioctl_abort_bulk_out(data);
 486				goto exit;
 487			}
 488		}
 489
 490		/* Send bulk URB */
 491		retval = usb_bulk_msg(data->usb_dev,
 492				      usb_rcvbulkpipe(data->usb_dev,
 493						      data->bulk_in),
 494				      buffer, USBTMC_SIZE_IOBUFFER, &actual,
 495				      USBTMC_TIMEOUT);
 496
 497		dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
 498
 499		/* Store bTag (in case we need to abort) */
 500		data->bTag_last_read = data->bTag;
 501
 502		if (retval < 0) {
 503			dev_dbg(dev, "Unable to read data, error %d\n", retval);
 504			if (data->auto_abort)
 505				usbtmc_ioctl_abort_bulk_in(data);
 506			goto exit;
 507		}
 508
 509		/* Parse header in first packet */
 510		if ((done == 0) || !data->rigol_quirk) {
 511			/* Sanity checks for the header */
 512			if (actual < USBTMC_HEADER_SIZE) {
 513				dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
 514				if (data->auto_abort)
 515					usbtmc_ioctl_abort_bulk_in(data);
 516				goto exit;
 517			}
 518
 519			if (buffer[0] != 2) {
 520				dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
 521				if (data->auto_abort)
 522					usbtmc_ioctl_abort_bulk_in(data);
 523				goto exit;
 524			}
 525
 526			if (buffer[1] != data->bTag_last_write) {
 527				dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
 528				if (data->auto_abort)
 529					usbtmc_ioctl_abort_bulk_in(data);
 530				goto exit;
 531			}
 532
 533			/* How many characters did the instrument send? */
 534			n_characters = buffer[4] +
 535				       (buffer[5] << 8) +
 536				       (buffer[6] << 16) +
 537				       (buffer[7] << 24);
 538
 539			if (n_characters > this_part) {
 540				dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
 541				if (data->auto_abort)
 542					usbtmc_ioctl_abort_bulk_in(data);
 543				goto exit;
 544			}
 545
 546			/* Remove the USBTMC header */
 547			actual -= USBTMC_HEADER_SIZE;
 548
 549			/* Check if the message is smaller than requested */
 550			if (data->rigol_quirk) {
 551				if (remaining > n_characters)
 552					remaining = n_characters;
 553				/* Remove padding if it exists */
 554				if (actual > remaining)
 555					actual = remaining;
 556			}
 557			else {
 558				if (this_part > n_characters)
 559					this_part = n_characters;
 560				/* Remove padding if it exists */
 561				if (actual > this_part)
 562					actual = this_part;
 563			}
 564
 565			dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
 566
 567			remaining -= actual;
 568
 569			/* Terminate if end-of-message bit received from device */
 570			if ((buffer[8] & 0x01) && (actual >= n_characters))
 571				remaining = 0;
 572
 573			dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
 574
 575
 576			/* Copy buffer to user space */
 577			if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
 578				/* There must have been an addressing problem */
 579				retval = -EFAULT;
 580				goto exit;
 581			}
 582			done += actual;
 583		}
 584		else  {
 585			if (actual > remaining)
 586				actual = remaining;
 587
 588			remaining -= actual;
 589
 590			dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
 591
 592			/* Copy buffer to user space */
 593			if (copy_to_user(buf + done, buffer, actual)) {
 594				/* There must have been an addressing problem */
 595				retval = -EFAULT;
 596				goto exit;
 597			}
 598			done += actual;
 599		}
 600	}
 601
 602	/* Update file position value */
 603	*f_pos = *f_pos + done;
 604	retval = done;
 605
 606exit:
 607	mutex_unlock(&data->io_mutex);
 608	kfree(buffer);
 609	return retval;
 610}
 611
 612static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
 613			    size_t count, loff_t *f_pos)
 614{
 615	struct usbtmc_device_data *data;
 616	u8 *buffer;
 617	int retval;
 618	int actual;
 619	unsigned long int n_bytes;
 620	int remaining;
 621	int done;
 622	int this_part;
 623
 624	data = filp->private_data;
 625
 626	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
 627	if (!buffer)
 628		return -ENOMEM;
 629
 630	mutex_lock(&data->io_mutex);
 631	if (data->zombie) {
 632		retval = -ENODEV;
 633		goto exit;
 634	}
 635
 636	remaining = count;
 637	done = 0;
 638
 639	while (remaining > 0) {
 640		if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
 641			this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
 642			buffer[8] = 0;
 643		} else {
 644			this_part = remaining;
 645			buffer[8] = 1;
 646		}
 647
 648		/* Setup IO buffer for DEV_DEP_MSG_OUT message */
 649		buffer[0] = 1;
 650		buffer[1] = data->bTag;
 651		buffer[2] = ~data->bTag;
 652		buffer[3] = 0; /* Reserved */
 653		buffer[4] = this_part >> 0;
 654		buffer[5] = this_part >> 8;
 655		buffer[6] = this_part >> 16;
 656		buffer[7] = this_part >> 24;
 657		/* buffer[8] is set above... */
 658		buffer[9] = 0; /* Reserved */
 659		buffer[10] = 0; /* Reserved */
 660		buffer[11] = 0; /* Reserved */
 661
 662		if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
 663			retval = -EFAULT;
 664			goto exit;
 665		}
 666
 667		n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
 668		memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
 669
 670		do {
 671			retval = usb_bulk_msg(data->usb_dev,
 672					      usb_sndbulkpipe(data->usb_dev,
 673							      data->bulk_out),
 674					      buffer, n_bytes,
 675					      &actual, USBTMC_TIMEOUT);
 676			if (retval != 0)
 677				break;
 678			n_bytes -= actual;
 679		} while (n_bytes);
 680
 681		data->bTag_last_write = data->bTag;
 682		data->bTag++;
 683
 684		if (!data->bTag)
 685			data->bTag++;
 686
 687		if (retval < 0) {
 688			dev_err(&data->intf->dev,
 689				"Unable to send data, error %d\n", retval);
 690			if (data->auto_abort)
 691				usbtmc_ioctl_abort_bulk_out(data);
 692			goto exit;
 693		}
 694
 695		remaining -= this_part;
 696		done += this_part;
 697	}
 698
 699	retval = count;
 700exit:
 701	mutex_unlock(&data->io_mutex);
 702	kfree(buffer);
 703	return retval;
 704}
 705
 706static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
 707{
 708	struct usb_host_interface *current_setting;
 709	struct usb_endpoint_descriptor *desc;
 710	struct device *dev;
 711	u8 *buffer;
 712	int rv;
 713	int n;
 714	int actual;
 715	int max_size;
 716
 717	dev = &data->intf->dev;
 718
 719	dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
 720
 721	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
 722	if (!buffer)
 723		return -ENOMEM;
 724
 725	rv = usb_control_msg(data->usb_dev,
 726			     usb_rcvctrlpipe(data->usb_dev, 0),
 727			     USBTMC_REQUEST_INITIATE_CLEAR,
 728			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 729			     0, 0, buffer, 1, USBTMC_TIMEOUT);
 730	if (rv < 0) {
 731		dev_err(dev, "usb_control_msg returned %d\n", rv);
 732		goto exit;
 733	}
 734
 735	dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
 736
 737	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
 738		dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
 739		rv = -EPERM;
 740		goto exit;
 741	}
 742
 743	max_size = 0;
 744	current_setting = data->intf->cur_altsetting;
 745	for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
 746		desc = &current_setting->endpoint[n].desc;
 747		if (desc->bEndpointAddress == data->bulk_in)
 748			max_size = usb_endpoint_maxp(desc);
 749	}
 750
 751	if (max_size == 0) {
 752		dev_err(dev, "Couldn't get wMaxPacketSize\n");
 753		rv = -EPERM;
 754		goto exit;
 755	}
 756
 757	dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
 758
 759	n = 0;
 760
 761usbtmc_clear_check_status:
 762
 763	dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
 764
 765	rv = usb_control_msg(data->usb_dev,
 766			     usb_rcvctrlpipe(data->usb_dev, 0),
 767			     USBTMC_REQUEST_CHECK_CLEAR_STATUS,
 768			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 769			     0, 0, buffer, 2, USBTMC_TIMEOUT);
 770	if (rv < 0) {
 771		dev_err(dev, "usb_control_msg returned %d\n", rv);
 772		goto exit;
 773	}
 774
 775	dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
 776
 777	if (buffer[0] == USBTMC_STATUS_SUCCESS)
 778		goto usbtmc_clear_bulk_out_halt;
 779
 780	if (buffer[0] != USBTMC_STATUS_PENDING) {
 781		dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
 782		rv = -EPERM;
 783		goto exit;
 784	}
 785
 786	if (buffer[1] == 1)
 787		do {
 788			dev_dbg(dev, "Reading from bulk in EP\n");
 789
 790			rv = usb_bulk_msg(data->usb_dev,
 791					  usb_rcvbulkpipe(data->usb_dev,
 792							  data->bulk_in),
 793					  buffer, USBTMC_SIZE_IOBUFFER,
 794					  &actual, USBTMC_TIMEOUT);
 795			n++;
 796
 797			if (rv < 0) {
 798				dev_err(dev, "usb_control_msg returned %d\n",
 799					rv);
 800				goto exit;
 801			}
 802		} while ((actual == max_size) &&
 803			  (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
 804
 805	if (actual == max_size) {
 806		dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
 807			USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
 808		rv = -EPERM;
 809		goto exit;
 810	}
 811
 812	goto usbtmc_clear_check_status;
 813
 814usbtmc_clear_bulk_out_halt:
 815
 816	rv = usb_clear_halt(data->usb_dev,
 817			    usb_sndbulkpipe(data->usb_dev, data->bulk_out));
 818	if (rv < 0) {
 819		dev_err(dev, "usb_control_msg returned %d\n", rv);
 820		goto exit;
 821	}
 822	rv = 0;
 823
 824exit:
 825	kfree(buffer);
 826	return rv;
 827}
 828
 829static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
 830{
 831	int rv;
 832
 833	rv = usb_clear_halt(data->usb_dev,
 834			    usb_sndbulkpipe(data->usb_dev, data->bulk_out));
 835
 836	if (rv < 0) {
 837		dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
 838			rv);
 839		return rv;
 840	}
 841	return 0;
 842}
 843
 844static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
 845{
 846	int rv;
 847
 848	rv = usb_clear_halt(data->usb_dev,
 849			    usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
 850
 851	if (rv < 0) {
 852		dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
 853			rv);
 854		return rv;
 855	}
 856	return 0;
 857}
 858
 859static int get_capabilities(struct usbtmc_device_data *data)
 860{
 861	struct device *dev = &data->usb_dev->dev;
 862	char *buffer;
 863	int rv = 0;
 864
 865	buffer = kmalloc(0x18, GFP_KERNEL);
 866	if (!buffer)
 867		return -ENOMEM;
 868
 869	rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
 870			     USBTMC_REQUEST_GET_CAPABILITIES,
 871			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 872			     0, 0, buffer, 0x18, USBTMC_TIMEOUT);
 873	if (rv < 0) {
 874		dev_err(dev, "usb_control_msg returned %d\n", rv);
 875		goto err_out;
 876	}
 877
 878	dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
 879	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
 880		dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
 881		rv = -EPERM;
 882		goto err_out;
 883	}
 884	dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
 885	dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
 886	dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
 887	dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
 888
 889	data->capabilities.interface_capabilities = buffer[4];
 890	data->capabilities.device_capabilities = buffer[5];
 891	data->capabilities.usb488_interface_capabilities = buffer[14];
 892	data->capabilities.usb488_device_capabilities = buffer[15];
 
 893	rv = 0;
 894
 895err_out:
 896	kfree(buffer);
 897	return rv;
 898}
 899
 900#define capability_attribute(name)					\
 901static ssize_t name##_show(struct device *dev,				\
 902			   struct device_attribute *attr, char *buf)	\
 903{									\
 904	struct usb_interface *intf = to_usb_interface(dev);		\
 905	struct usbtmc_device_data *data = usb_get_intfdata(intf);	\
 906									\
 907	return sprintf(buf, "%d\n", data->capabilities.name);		\
 908}									\
 909static DEVICE_ATTR_RO(name)
 910
 911capability_attribute(interface_capabilities);
 912capability_attribute(device_capabilities);
 913capability_attribute(usb488_interface_capabilities);
 914capability_attribute(usb488_device_capabilities);
 915
 916static struct attribute *capability_attrs[] = {
 917	&dev_attr_interface_capabilities.attr,
 918	&dev_attr_device_capabilities.attr,
 919	&dev_attr_usb488_interface_capabilities.attr,
 920	&dev_attr_usb488_device_capabilities.attr,
 921	NULL,
 922};
 923
 924static struct attribute_group capability_attr_grp = {
 925	.attrs = capability_attrs,
 926};
 927
 928static ssize_t TermChar_show(struct device *dev,
 929			     struct device_attribute *attr, char *buf)
 930{
 931	struct usb_interface *intf = to_usb_interface(dev);
 932	struct usbtmc_device_data *data = usb_get_intfdata(intf);
 933
 934	return sprintf(buf, "%c\n", data->TermChar);
 935}
 936
 937static ssize_t TermChar_store(struct device *dev,
 938			      struct device_attribute *attr,
 939			      const char *buf, size_t count)
 940{
 941	struct usb_interface *intf = to_usb_interface(dev);
 942	struct usbtmc_device_data *data = usb_get_intfdata(intf);
 943
 944	if (count < 1)
 945		return -EINVAL;
 946	data->TermChar = buf[0];
 947	return count;
 948}
 949static DEVICE_ATTR_RW(TermChar);
 950
 951#define data_attribute(name)						\
 952static ssize_t name##_show(struct device *dev,				\
 953			   struct device_attribute *attr, char *buf)	\
 954{									\
 955	struct usb_interface *intf = to_usb_interface(dev);		\
 956	struct usbtmc_device_data *data = usb_get_intfdata(intf);	\
 957									\
 958	return sprintf(buf, "%d\n", data->name);			\
 959}									\
 960static ssize_t name##_store(struct device *dev,				\
 961			    struct device_attribute *attr,		\
 962			    const char *buf, size_t count)		\
 963{									\
 964	struct usb_interface *intf = to_usb_interface(dev);		\
 965	struct usbtmc_device_data *data = usb_get_intfdata(intf);	\
 966	ssize_t result;							\
 967	unsigned val;							\
 968									\
 969	result = sscanf(buf, "%u\n", &val);				\
 970	if (result != 1)						\
 971		result = -EINVAL;					\
 972	data->name = val;						\
 973	if (result < 0)							\
 974		return result;						\
 975	else								\
 976		return count;						\
 977}									\
 978static DEVICE_ATTR_RW(name)
 979
 980data_attribute(TermCharEnabled);
 981data_attribute(auto_abort);
 982
 983static struct attribute *data_attrs[] = {
 984	&dev_attr_TermChar.attr,
 985	&dev_attr_TermCharEnabled.attr,
 986	&dev_attr_auto_abort.attr,
 987	NULL,
 988};
 989
 990static struct attribute_group data_attr_grp = {
 991	.attrs = data_attrs,
 992};
 993
 994static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
 995{
 996	struct device *dev;
 997	u8 *buffer;
 998	int rv;
 999
1000	dev = &data->intf->dev;
1001
1002	buffer = kmalloc(2, GFP_KERNEL);
1003	if (!buffer)
1004		return -ENOMEM;
1005
1006	rv = usb_control_msg(data->usb_dev,
1007			     usb_rcvctrlpipe(data->usb_dev, 0),
1008			     USBTMC_REQUEST_INDICATOR_PULSE,
1009			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1010			     0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1011
1012	if (rv < 0) {
1013		dev_err(dev, "usb_control_msg returned %d\n", rv);
1014		goto exit;
1015	}
1016
1017	dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1018
1019	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1020		dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1021		rv = -EPERM;
1022		goto exit;
1023	}
1024	rv = 0;
1025
1026exit:
1027	kfree(buffer);
1028	return rv;
1029}
1030
1031static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1032{
1033	struct usbtmc_device_data *data;
1034	int retval = -EBADRQC;
1035
1036	data = file->private_data;
1037	mutex_lock(&data->io_mutex);
1038	if (data->zombie) {
1039		retval = -ENODEV;
1040		goto skip_io_on_zombie;
1041	}
1042
1043	switch (cmd) {
1044	case USBTMC_IOCTL_CLEAR_OUT_HALT:
1045		retval = usbtmc_ioctl_clear_out_halt(data);
1046		break;
1047
1048	case USBTMC_IOCTL_CLEAR_IN_HALT:
1049		retval = usbtmc_ioctl_clear_in_halt(data);
1050		break;
1051
1052	case USBTMC_IOCTL_INDICATOR_PULSE:
1053		retval = usbtmc_ioctl_indicator_pulse(data);
1054		break;
1055
1056	case USBTMC_IOCTL_CLEAR:
1057		retval = usbtmc_ioctl_clear(data);
1058		break;
1059
1060	case USBTMC_IOCTL_ABORT_BULK_OUT:
1061		retval = usbtmc_ioctl_abort_bulk_out(data);
1062		break;
1063
1064	case USBTMC_IOCTL_ABORT_BULK_IN:
1065		retval = usbtmc_ioctl_abort_bulk_in(data);
1066		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1067	}
1068
1069skip_io_on_zombie:
1070	mutex_unlock(&data->io_mutex);
1071	return retval;
1072}
1073
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1074static const struct file_operations fops = {
1075	.owner		= THIS_MODULE,
1076	.read		= usbtmc_read,
1077	.write		= usbtmc_write,
1078	.open		= usbtmc_open,
1079	.release	= usbtmc_release,
1080	.unlocked_ioctl	= usbtmc_ioctl,
 
 
1081	.llseek		= default_llseek,
1082};
1083
1084static struct usb_class_driver usbtmc_class = {
1085	.name =		"usbtmc%d",
1086	.fops =		&fops,
1087	.minor_base =	USBTMC_MINOR_BASE,
1088};
1089
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1090
1091static int usbtmc_probe(struct usb_interface *intf,
1092			const struct usb_device_id *id)
1093{
1094	struct usbtmc_device_data *data;
1095	struct usb_host_interface *iface_desc;
1096	struct usb_endpoint_descriptor *endpoint;
1097	int n;
1098	int retcode;
1099
1100	dev_dbg(&intf->dev, "%s called\n", __func__);
1101
1102	data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
1103	if (!data) {
1104		dev_err(&intf->dev, "Unable to allocate kernel memory\n");
1105		return -ENOMEM;
1106	}
1107
1108	data->intf = intf;
1109	data->id = id;
1110	data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1111	usb_set_intfdata(intf, data);
1112	kref_init(&data->kref);
1113	mutex_init(&data->io_mutex);
 
 
 
1114	data->zombie = 0;
1115
1116	/* Determine if it is a Rigol or not */
1117	data->rigol_quirk = 0;
1118	dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1119		le16_to_cpu(data->usb_dev->descriptor.idVendor),
1120		le16_to_cpu(data->usb_dev->descriptor.idProduct));
1121	for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
1122		if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) &&
1123		    (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) {
1124			dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1125			data->rigol_quirk = 1;
1126			break;
1127		}
1128	}
1129
1130	/* Initialize USBTMC bTag and other fields */
1131	data->bTag	= 1;
1132	data->TermCharEnabled = 0;
1133	data->TermChar = '\n';
 
 
1134
1135	/* USBTMC devices have only one setting, so use that */
1136	iface_desc = data->intf->cur_altsetting;
 
1137
1138	/* Find bulk in endpoint */
1139	for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1140		endpoint = &iface_desc->endpoint[n].desc;
1141
1142		if (usb_endpoint_is_bulk_in(endpoint)) {
1143			data->bulk_in = endpoint->bEndpointAddress;
1144			dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1145				data->bulk_in);
1146			break;
1147		}
1148	}
1149
1150	/* Find bulk out endpoint */
1151	for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1152		endpoint = &iface_desc->endpoint[n].desc;
1153
1154		if (usb_endpoint_is_bulk_out(endpoint)) {
1155			data->bulk_out = endpoint->bEndpointAddress;
1156			dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1157				data->bulk_out);
1158			break;
1159		}
1160	}
1161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1162	retcode = get_capabilities(data);
1163	if (retcode)
1164		dev_err(&intf->dev, "can't read capabilities\n");
1165	else
1166		retcode = sysfs_create_group(&intf->dev.kobj,
1167					     &capability_attr_grp);
1168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1169	retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1170
1171	retcode = usb_register_dev(intf, &usbtmc_class);
1172	if (retcode) {
1173		dev_err(&intf->dev, "Not able to get a minor"
1174			" (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1175			retcode);
1176		goto error_register;
1177	}
1178	dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1179
1180	return 0;
1181
1182error_register:
1183	sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1184	sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
 
 
1185	kref_put(&data->kref, usbtmc_delete);
1186	return retcode;
1187}
1188
1189static void usbtmc_disconnect(struct usb_interface *intf)
1190{
1191	struct usbtmc_device_data *data;
1192
1193	dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1194
1195	data = usb_get_intfdata(intf);
1196	usb_deregister_dev(intf, &usbtmc_class);
1197	sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1198	sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1199	mutex_lock(&data->io_mutex);
1200	data->zombie = 1;
 
1201	mutex_unlock(&data->io_mutex);
 
1202	kref_put(&data->kref, usbtmc_delete);
1203}
1204
1205static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
1206{
1207	/* this driver does not have pending URBs */
1208	return 0;
1209}
1210
1211static int usbtmc_resume(struct usb_interface *intf)
1212{
1213	return 0;
1214}
1215
1216static struct usb_driver usbtmc_driver = {
1217	.name		= "usbtmc",
1218	.id_table	= usbtmc_devices,
1219	.probe		= usbtmc_probe,
1220	.disconnect	= usbtmc_disconnect,
1221	.suspend	= usbtmc_suspend,
1222	.resume		= usbtmc_resume,
1223};
1224
1225module_usb_driver(usbtmc_driver);
1226
1227MODULE_LICENSE("GPL");
v4.10.11
   1/**
   2 * drivers/usb/class/usbtmc.c - USB Test & Measurement 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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23
  24#include <linux/module.h>
  25#include <linux/kernel.h>
  26#include <linux/fs.h>
  27#include <linux/uaccess.h>
  28#include <linux/kref.h>
  29#include <linux/slab.h>
  30#include <linux/poll.h>
  31#include <linux/mutex.h>
  32#include <linux/usb.h>
  33#include <linux/usb/tmc.h>
  34
  35
  36#define RIGOL			1
  37#define USBTMC_HEADER_SIZE	12
  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) */
  47#define USBTMC_TIMEOUT		5000
  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
  56static const struct usb_device_id usbtmc_devices[] = {
  57	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
  58	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
  59	{ 0, } /* terminating entry */
  60};
  61MODULE_DEVICE_TABLE(usb, usbtmc_devices);
  62
  63/*
  64 * This structure is the capabilities for the device
  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.
  68 */
  69struct 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 */
  79struct 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
  91	/* data for interrupt in endpoint handling */
  92	u8             bNotify1;
  93	u8             bNotify2;
  94	u16            ifnum;
  95	u8             iin_bTag;
  96	u8            *iin_buffer;
  97	atomic_t       iin_data_valid;
  98	unsigned int   iin_ep;
  99	int            iin_ep_present;
 100	int            iin_interval;
 101	struct urb    *iin_urb;
 102	u16            iin_wMaxPacketSize;
 103	atomic_t       srq_asserted;
 104
 105	/* coalesced usb488_caps from usbtmc_dev_capabilities */
 106	__u8 usb488_caps;
 107
 108	u8 rigol_quirk;
 109
 110	/* attributes from the USB TMC spec for this device */
 111	u8 TermChar;
 112	bool TermCharEnabled;
 113	bool auto_abort;
 114
 115	bool zombie; /* fd of disconnected device */
 116
 117	struct usbtmc_dev_capabilities	capabilities;
 118	struct kref kref;
 119	struct mutex io_mutex;	/* only one i/o function running at a time */
 120	wait_queue_head_t waitq;
 121	struct fasync_struct *fasync;
 122};
 123#define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
 124
 125struct usbtmc_ID_rigol_quirk {
 126	__u16 idVendor;
 127	__u16 idProduct;
 128};
 129
 130static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
 131	{ 0x1ab1, 0x0588 },
 132	{ 0x1ab1, 0x04b0 },
 133	{ 0, 0 }
 134};
 135
 136/* Forward declarations */
 137static struct usb_driver usbtmc_driver;
 138
 139static void usbtmc_delete(struct kref *kref)
 140{
 141	struct usbtmc_device_data *data = to_usbtmc_data(kref);
 142
 143	usb_put_dev(data->usb_dev);
 144	kfree(data);
 145}
 146
 147static int usbtmc_open(struct inode *inode, struct file *filp)
 148{
 149	struct usb_interface *intf;
 150	struct usbtmc_device_data *data;
 151	int retval = 0;
 152
 153	intf = usb_find_interface(&usbtmc_driver, iminor(inode));
 154	if (!intf) {
 155		pr_err("can not find device for minor %d", iminor(inode));
 156		return -ENODEV;
 157	}
 158
 159	data = usb_get_intfdata(intf);
 160	/* Protect reference to data from file structure until release */
 161	kref_get(&data->kref);
 162
 163	/* Store pointer in file structure's private data field */
 164	filp->private_data = data;
 165
 166	return retval;
 167}
 168
 169static int usbtmc_release(struct inode *inode, struct file *file)
 170{
 171	struct usbtmc_device_data *data = file->private_data;
 172
 173	kref_put(&data->kref, usbtmc_delete);
 174	return 0;
 175}
 176
 177static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
 178{
 179	u8 *buffer;
 180	struct device *dev;
 181	int rv;
 182	int n;
 183	int actual;
 184	struct usb_host_interface *current_setting;
 185	int max_size;
 186
 187	dev = &data->intf->dev;
 188	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
 189	if (!buffer)
 190		return -ENOMEM;
 191
 192	rv = usb_control_msg(data->usb_dev,
 193			     usb_rcvctrlpipe(data->usb_dev, 0),
 194			     USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
 195			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
 196			     data->bTag_last_read, data->bulk_in,
 197			     buffer, 2, USBTMC_TIMEOUT);
 198
 199	if (rv < 0) {
 200		dev_err(dev, "usb_control_msg returned %d\n", rv);
 201		goto exit;
 202	}
 203
 204	dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
 205
 206	if (buffer[0] == USBTMC_STATUS_FAILED) {
 207		rv = 0;
 208		goto exit;
 209	}
 210
 211	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
 212		dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
 213			buffer[0]);
 214		rv = -EPERM;
 215		goto exit;
 216	}
 217
 218	max_size = 0;
 219	current_setting = data->intf->cur_altsetting;
 220	for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
 221		if (current_setting->endpoint[n].desc.bEndpointAddress ==
 222			data->bulk_in)
 223			max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
 224
 225	if (max_size == 0) {
 226		dev_err(dev, "Couldn't get wMaxPacketSize\n");
 227		rv = -EPERM;
 228		goto exit;
 229	}
 230
 231	dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
 232
 233	n = 0;
 234
 235	do {
 236		dev_dbg(dev, "Reading from bulk in EP\n");
 237
 238		rv = usb_bulk_msg(data->usb_dev,
 239				  usb_rcvbulkpipe(data->usb_dev,
 240						  data->bulk_in),
 241				  buffer, USBTMC_SIZE_IOBUFFER,
 242				  &actual, USBTMC_TIMEOUT);
 243
 244		n++;
 245
 246		if (rv < 0) {
 247			dev_err(dev, "usb_bulk_msg returned %d\n", rv);
 248			goto exit;
 249		}
 250	} while ((actual == max_size) &&
 251		 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
 252
 253	if (actual == max_size) {
 254		dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
 255			USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
 256		rv = -EPERM;
 257		goto exit;
 258	}
 259
 260	n = 0;
 261
 262usbtmc_abort_bulk_in_status:
 263	rv = usb_control_msg(data->usb_dev,
 264			     usb_rcvctrlpipe(data->usb_dev, 0),
 265			     USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
 266			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
 267			     0, data->bulk_in, buffer, 0x08,
 268			     USBTMC_TIMEOUT);
 269
 270	if (rv < 0) {
 271		dev_err(dev, "usb_control_msg returned %d\n", rv);
 272		goto exit;
 273	}
 274
 275	dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
 276
 277	if (buffer[0] == USBTMC_STATUS_SUCCESS) {
 278		rv = 0;
 279		goto exit;
 280	}
 281
 282	if (buffer[0] != USBTMC_STATUS_PENDING) {
 283		dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
 284		rv = -EPERM;
 285		goto exit;
 286	}
 287
 288	if (buffer[1] == 1)
 289		do {
 290			dev_dbg(dev, "Reading from bulk in EP\n");
 291
 292			rv = usb_bulk_msg(data->usb_dev,
 293					  usb_rcvbulkpipe(data->usb_dev,
 294							  data->bulk_in),
 295					  buffer, USBTMC_SIZE_IOBUFFER,
 296					  &actual, USBTMC_TIMEOUT);
 297
 298			n++;
 299
 300			if (rv < 0) {
 301				dev_err(dev, "usb_bulk_msg returned %d\n", rv);
 302				goto exit;
 303			}
 304		} while ((actual == max_size) &&
 305			 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
 306
 307	if (actual == max_size) {
 308		dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
 309			USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
 310		rv = -EPERM;
 311		goto exit;
 312	}
 313
 314	goto usbtmc_abort_bulk_in_status;
 315
 316exit:
 317	kfree(buffer);
 318	return rv;
 319
 320}
 321
 322static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
 323{
 324	struct device *dev;
 325	u8 *buffer;
 326	int rv;
 327	int n;
 328
 329	dev = &data->intf->dev;
 330
 331	buffer = kmalloc(8, GFP_KERNEL);
 332	if (!buffer)
 333		return -ENOMEM;
 334
 335	rv = usb_control_msg(data->usb_dev,
 336			     usb_rcvctrlpipe(data->usb_dev, 0),
 337			     USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
 338			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
 339			     data->bTag_last_write, data->bulk_out,
 340			     buffer, 2, USBTMC_TIMEOUT);
 341
 342	if (rv < 0) {
 343		dev_err(dev, "usb_control_msg returned %d\n", rv);
 344		goto exit;
 345	}
 346
 347	dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
 348
 349	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
 350		dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
 351			buffer[0]);
 352		rv = -EPERM;
 353		goto exit;
 354	}
 355
 356	n = 0;
 357
 358usbtmc_abort_bulk_out_check_status:
 359	rv = usb_control_msg(data->usb_dev,
 360			     usb_rcvctrlpipe(data->usb_dev, 0),
 361			     USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
 362			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
 363			     0, data->bulk_out, buffer, 0x08,
 364			     USBTMC_TIMEOUT);
 365	n++;
 366	if (rv < 0) {
 367		dev_err(dev, "usb_control_msg returned %d\n", rv);
 368		goto exit;
 369	}
 370
 371	dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
 372
 373	if (buffer[0] == USBTMC_STATUS_SUCCESS)
 374		goto usbtmc_abort_bulk_out_clear_halt;
 375
 376	if ((buffer[0] == USBTMC_STATUS_PENDING) &&
 377	    (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
 378		goto usbtmc_abort_bulk_out_check_status;
 379
 380	rv = -EPERM;
 381	goto exit;
 382
 383usbtmc_abort_bulk_out_clear_halt:
 384	rv = usb_clear_halt(data->usb_dev,
 385			    usb_sndbulkpipe(data->usb_dev, data->bulk_out));
 386
 387	if (rv < 0) {
 388		dev_err(dev, "usb_control_msg returned %d\n", rv);
 389		goto exit;
 390	}
 391	rv = 0;
 392
 393exit:
 394	kfree(buffer);
 395	return rv;
 396}
 397
 398static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data *data,
 399				void __user *arg)
 400{
 401	struct device *dev = &data->intf->dev;
 402	u8 *buffer;
 403	u8 tag;
 404	__u8 stb;
 405	int rv;
 406
 407	dev_dbg(dev, "Enter ioctl_read_stb iin_ep_present: %d\n",
 408		data->iin_ep_present);
 409
 410	buffer = kmalloc(8, GFP_KERNEL);
 411	if (!buffer)
 412		return -ENOMEM;
 413
 414	atomic_set(&data->iin_data_valid, 0);
 415
 416	/* must issue read_stb before using poll or select */
 417	atomic_set(&data->srq_asserted, 0);
 418
 419	rv = usb_control_msg(data->usb_dev,
 420			usb_rcvctrlpipe(data->usb_dev, 0),
 421			USBTMC488_REQUEST_READ_STATUS_BYTE,
 422			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 423			data->iin_bTag,
 424			data->ifnum,
 425			buffer, 0x03, USBTMC_TIMEOUT);
 426	if (rv < 0) {
 427		dev_err(dev, "stb usb_control_msg returned %d\n", rv);
 428		goto exit;
 429	}
 430
 431	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
 432		dev_err(dev, "control status returned %x\n", buffer[0]);
 433		rv = -EIO;
 434		goto exit;
 435	}
 436
 437	if (data->iin_ep_present) {
 438		rv = wait_event_interruptible_timeout(
 439			data->waitq,
 440			atomic_read(&data->iin_data_valid) != 0,
 441			USBTMC_TIMEOUT);
 442		if (rv < 0) {
 443			dev_dbg(dev, "wait interrupted %d\n", rv);
 444			goto exit;
 445		}
 446
 447		if (rv == 0) {
 448			dev_dbg(dev, "wait timed out\n");
 449			rv = -ETIME;
 450			goto exit;
 451		}
 452
 453		tag = data->bNotify1 & 0x7f;
 454		if (tag != data->iin_bTag) {
 455			dev_err(dev, "expected bTag %x got %x\n",
 456				data->iin_bTag, tag);
 457		}
 458
 459		stb = data->bNotify2;
 460	} else {
 461		stb = buffer[2];
 462	}
 463
 464	rv = copy_to_user(arg, &stb, sizeof(stb));
 465	if (rv)
 466		rv = -EFAULT;
 467
 468 exit:
 469	/* bump interrupt bTag */
 470	data->iin_bTag += 1;
 471	if (data->iin_bTag > 127)
 472		/* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
 473		data->iin_bTag = 2;
 474
 475	kfree(buffer);
 476	return rv;
 477}
 478
 479static int usbtmc488_ioctl_simple(struct usbtmc_device_data *data,
 480				void __user *arg, unsigned int cmd)
 481{
 482	struct device *dev = &data->intf->dev;
 483	__u8 val;
 484	u8 *buffer;
 485	u16 wValue;
 486	int rv;
 487
 488	if (!(data->usb488_caps & USBTMC488_CAPABILITY_SIMPLE))
 489		return -EINVAL;
 490
 491	buffer = kmalloc(8, GFP_KERNEL);
 492	if (!buffer)
 493		return -ENOMEM;
 494
 495	if (cmd == USBTMC488_REQUEST_REN_CONTROL) {
 496		rv = copy_from_user(&val, arg, sizeof(val));
 497		if (rv) {
 498			rv = -EFAULT;
 499			goto exit;
 500		}
 501		wValue = val ? 1 : 0;
 502	} else {
 503		wValue = 0;
 504	}
 505
 506	rv = usb_control_msg(data->usb_dev,
 507			usb_rcvctrlpipe(data->usb_dev, 0),
 508			cmd,
 509			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 510			wValue,
 511			data->ifnum,
 512			buffer, 0x01, USBTMC_TIMEOUT);
 513	if (rv < 0) {
 514		dev_err(dev, "simple usb_control_msg failed %d\n", rv);
 515		goto exit;
 516	} else if (rv != 1) {
 517		dev_warn(dev, "simple usb_control_msg returned %d\n", rv);
 518		rv = -EIO;
 519		goto exit;
 520	}
 521
 522	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
 523		dev_err(dev, "simple control status returned %x\n", buffer[0]);
 524		rv = -EIO;
 525		goto exit;
 526	}
 527	rv = 0;
 528
 529 exit:
 530	kfree(buffer);
 531	return rv;
 532}
 533
 534/*
 535 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-OUT endpoint.
 536 * @transfer_size: number of bytes to request from the device.
 537 *
 538 * See the USBTMC specification, Table 4.
 539 *
 540 * Also updates bTag_last_write.
 541 */
 542static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
 543{
 544	int retval;
 545	u8 *buffer;
 546	int actual;
 547
 548	buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
 549	if (!buffer)
 550		return -ENOMEM;
 551	/* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
 552	 * Refer to class specs for details
 553	 */
 554	buffer[0] = 2;
 555	buffer[1] = data->bTag;
 556	buffer[2] = ~data->bTag;
 557	buffer[3] = 0; /* Reserved */
 558	buffer[4] = transfer_size >> 0;
 559	buffer[5] = transfer_size >> 8;
 560	buffer[6] = transfer_size >> 16;
 561	buffer[7] = transfer_size >> 24;
 562	buffer[8] = data->TermCharEnabled * 2;
 563	/* Use term character? */
 564	buffer[9] = data->TermChar;
 565	buffer[10] = 0; /* Reserved */
 566	buffer[11] = 0; /* Reserved */
 567
 568	/* Send bulk URB */
 569	retval = usb_bulk_msg(data->usb_dev,
 570			      usb_sndbulkpipe(data->usb_dev,
 571					      data->bulk_out),
 572			      buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
 573
 574	/* Store bTag (in case we need to abort) */
 575	data->bTag_last_write = data->bTag;
 576
 577	/* Increment bTag -- and increment again if zero */
 578	data->bTag++;
 579	if (!data->bTag)
 580		data->bTag++;
 581
 582	kfree(buffer);
 583	if (retval < 0) {
 584		dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
 585		return retval;
 586	}
 587
 588	return 0;
 589}
 590
 591static ssize_t usbtmc_read(struct file *filp, char __user *buf,
 592			   size_t count, loff_t *f_pos)
 593{
 594	struct usbtmc_device_data *data;
 595	struct device *dev;
 596	u32 n_characters;
 597	u8 *buffer;
 598	int actual;
 599	size_t done;
 600	size_t remaining;
 601	int retval;
 602	size_t this_part;
 603
 604	/* Get pointer to private data structure */
 605	data = filp->private_data;
 606	dev = &data->intf->dev;
 607
 608	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
 609	if (!buffer)
 610		return -ENOMEM;
 611
 612	mutex_lock(&data->io_mutex);
 613	if (data->zombie) {
 614		retval = -ENODEV;
 615		goto exit;
 616	}
 617
 618	if (data->rigol_quirk) {
 619		dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
 620
 621		retval = send_request_dev_dep_msg_in(data, count);
 622
 623		if (retval < 0) {
 624			if (data->auto_abort)
 625				usbtmc_ioctl_abort_bulk_out(data);
 626			goto exit;
 627		}
 628	}
 629
 630	/* Loop until we have fetched everything we requested */
 631	remaining = count;
 632	this_part = remaining;
 633	done = 0;
 634
 635	while (remaining > 0) {
 636		if (!data->rigol_quirk) {
 637			dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
 638
 639			if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
 640				this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
 641			else
 642				this_part = remaining;
 643
 644			retval = send_request_dev_dep_msg_in(data, this_part);
 645			if (retval < 0) {
 646			dev_err(dev, "usb_bulk_msg returned %d\n", retval);
 647				if (data->auto_abort)
 648					usbtmc_ioctl_abort_bulk_out(data);
 649				goto exit;
 650			}
 651		}
 652
 653		/* Send bulk URB */
 654		retval = usb_bulk_msg(data->usb_dev,
 655				      usb_rcvbulkpipe(data->usb_dev,
 656						      data->bulk_in),
 657				      buffer, USBTMC_SIZE_IOBUFFER, &actual,
 658				      USBTMC_TIMEOUT);
 659
 660		dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
 661
 662		/* Store bTag (in case we need to abort) */
 663		data->bTag_last_read = data->bTag;
 664
 665		if (retval < 0) {
 666			dev_dbg(dev, "Unable to read data, error %d\n", retval);
 667			if (data->auto_abort)
 668				usbtmc_ioctl_abort_bulk_in(data);
 669			goto exit;
 670		}
 671
 672		/* Parse header in first packet */
 673		if ((done == 0) || !data->rigol_quirk) {
 674			/* Sanity checks for the header */
 675			if (actual < USBTMC_HEADER_SIZE) {
 676				dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
 677				if (data->auto_abort)
 678					usbtmc_ioctl_abort_bulk_in(data);
 679				goto exit;
 680			}
 681
 682			if (buffer[0] != 2) {
 683				dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
 684				if (data->auto_abort)
 685					usbtmc_ioctl_abort_bulk_in(data);
 686				goto exit;
 687			}
 688
 689			if (buffer[1] != data->bTag_last_write) {
 690				dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
 691				if (data->auto_abort)
 692					usbtmc_ioctl_abort_bulk_in(data);
 693				goto exit;
 694			}
 695
 696			/* How many characters did the instrument send? */
 697			n_characters = buffer[4] +
 698				       (buffer[5] << 8) +
 699				       (buffer[6] << 16) +
 700				       (buffer[7] << 24);
 701
 702			if (n_characters > this_part) {
 703				dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
 704				if (data->auto_abort)
 705					usbtmc_ioctl_abort_bulk_in(data);
 706				goto exit;
 707			}
 708
 709			/* Remove the USBTMC header */
 710			actual -= USBTMC_HEADER_SIZE;
 711
 712			/* Check if the message is smaller than requested */
 713			if (data->rigol_quirk) {
 714				if (remaining > n_characters)
 715					remaining = n_characters;
 716				/* Remove padding if it exists */
 717				if (actual > remaining)
 718					actual = remaining;
 719			}
 720			else {
 721				if (this_part > n_characters)
 722					this_part = n_characters;
 723				/* Remove padding if it exists */
 724				if (actual > this_part)
 725					actual = this_part;
 726			}
 727
 728			dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
 729
 730			remaining -= actual;
 731
 732			/* Terminate if end-of-message bit received from device */
 733			if ((buffer[8] & 0x01) && (actual >= n_characters))
 734				remaining = 0;
 735
 736			dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
 737
 738
 739			/* Copy buffer to user space */
 740			if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
 741				/* There must have been an addressing problem */
 742				retval = -EFAULT;
 743				goto exit;
 744			}
 745			done += actual;
 746		}
 747		else  {
 748			if (actual > remaining)
 749				actual = remaining;
 750
 751			remaining -= actual;
 752
 753			dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
 754
 755			/* Copy buffer to user space */
 756			if (copy_to_user(buf + done, buffer, actual)) {
 757				/* There must have been an addressing problem */
 758				retval = -EFAULT;
 759				goto exit;
 760			}
 761			done += actual;
 762		}
 763	}
 764
 765	/* Update file position value */
 766	*f_pos = *f_pos + done;
 767	retval = done;
 768
 769exit:
 770	mutex_unlock(&data->io_mutex);
 771	kfree(buffer);
 772	return retval;
 773}
 774
 775static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
 776			    size_t count, loff_t *f_pos)
 777{
 778	struct usbtmc_device_data *data;
 779	u8 *buffer;
 780	int retval;
 781	int actual;
 782	unsigned long int n_bytes;
 783	int remaining;
 784	int done;
 785	int this_part;
 786
 787	data = filp->private_data;
 788
 789	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
 790	if (!buffer)
 791		return -ENOMEM;
 792
 793	mutex_lock(&data->io_mutex);
 794	if (data->zombie) {
 795		retval = -ENODEV;
 796		goto exit;
 797	}
 798
 799	remaining = count;
 800	done = 0;
 801
 802	while (remaining > 0) {
 803		if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
 804			this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
 805			buffer[8] = 0;
 806		} else {
 807			this_part = remaining;
 808			buffer[8] = 1;
 809		}
 810
 811		/* Setup IO buffer for DEV_DEP_MSG_OUT message */
 812		buffer[0] = 1;
 813		buffer[1] = data->bTag;
 814		buffer[2] = ~data->bTag;
 815		buffer[3] = 0; /* Reserved */
 816		buffer[4] = this_part >> 0;
 817		buffer[5] = this_part >> 8;
 818		buffer[6] = this_part >> 16;
 819		buffer[7] = this_part >> 24;
 820		/* buffer[8] is set above... */
 821		buffer[9] = 0; /* Reserved */
 822		buffer[10] = 0; /* Reserved */
 823		buffer[11] = 0; /* Reserved */
 824
 825		if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
 826			retval = -EFAULT;
 827			goto exit;
 828		}
 829
 830		n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
 831		memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
 832
 833		do {
 834			retval = usb_bulk_msg(data->usb_dev,
 835					      usb_sndbulkpipe(data->usb_dev,
 836							      data->bulk_out),
 837					      buffer, n_bytes,
 838					      &actual, USBTMC_TIMEOUT);
 839			if (retval != 0)
 840				break;
 841			n_bytes -= actual;
 842		} while (n_bytes);
 843
 844		data->bTag_last_write = data->bTag;
 845		data->bTag++;
 846
 847		if (!data->bTag)
 848			data->bTag++;
 849
 850		if (retval < 0) {
 851			dev_err(&data->intf->dev,
 852				"Unable to send data, error %d\n", retval);
 853			if (data->auto_abort)
 854				usbtmc_ioctl_abort_bulk_out(data);
 855			goto exit;
 856		}
 857
 858		remaining -= this_part;
 859		done += this_part;
 860	}
 861
 862	retval = count;
 863exit:
 864	mutex_unlock(&data->io_mutex);
 865	kfree(buffer);
 866	return retval;
 867}
 868
 869static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
 870{
 871	struct usb_host_interface *current_setting;
 872	struct usb_endpoint_descriptor *desc;
 873	struct device *dev;
 874	u8 *buffer;
 875	int rv;
 876	int n;
 877	int actual = 0;
 878	int max_size;
 879
 880	dev = &data->intf->dev;
 881
 882	dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
 883
 884	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
 885	if (!buffer)
 886		return -ENOMEM;
 887
 888	rv = usb_control_msg(data->usb_dev,
 889			     usb_rcvctrlpipe(data->usb_dev, 0),
 890			     USBTMC_REQUEST_INITIATE_CLEAR,
 891			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 892			     0, 0, buffer, 1, USBTMC_TIMEOUT);
 893	if (rv < 0) {
 894		dev_err(dev, "usb_control_msg returned %d\n", rv);
 895		goto exit;
 896	}
 897
 898	dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
 899
 900	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
 901		dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
 902		rv = -EPERM;
 903		goto exit;
 904	}
 905
 906	max_size = 0;
 907	current_setting = data->intf->cur_altsetting;
 908	for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
 909		desc = &current_setting->endpoint[n].desc;
 910		if (desc->bEndpointAddress == data->bulk_in)
 911			max_size = usb_endpoint_maxp(desc);
 912	}
 913
 914	if (max_size == 0) {
 915		dev_err(dev, "Couldn't get wMaxPacketSize\n");
 916		rv = -EPERM;
 917		goto exit;
 918	}
 919
 920	dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
 921
 922	n = 0;
 923
 924usbtmc_clear_check_status:
 925
 926	dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
 927
 928	rv = usb_control_msg(data->usb_dev,
 929			     usb_rcvctrlpipe(data->usb_dev, 0),
 930			     USBTMC_REQUEST_CHECK_CLEAR_STATUS,
 931			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 932			     0, 0, buffer, 2, USBTMC_TIMEOUT);
 933	if (rv < 0) {
 934		dev_err(dev, "usb_control_msg returned %d\n", rv);
 935		goto exit;
 936	}
 937
 938	dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
 939
 940	if (buffer[0] == USBTMC_STATUS_SUCCESS)
 941		goto usbtmc_clear_bulk_out_halt;
 942
 943	if (buffer[0] != USBTMC_STATUS_PENDING) {
 944		dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
 945		rv = -EPERM;
 946		goto exit;
 947	}
 948
 949	if (buffer[1] == 1)
 950		do {
 951			dev_dbg(dev, "Reading from bulk in EP\n");
 952
 953			rv = usb_bulk_msg(data->usb_dev,
 954					  usb_rcvbulkpipe(data->usb_dev,
 955							  data->bulk_in),
 956					  buffer, USBTMC_SIZE_IOBUFFER,
 957					  &actual, USBTMC_TIMEOUT);
 958			n++;
 959
 960			if (rv < 0) {
 961				dev_err(dev, "usb_control_msg returned %d\n",
 962					rv);
 963				goto exit;
 964			}
 965		} while ((actual == max_size) &&
 966			  (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
 967
 968	if (actual == max_size) {
 969		dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
 970			USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
 971		rv = -EPERM;
 972		goto exit;
 973	}
 974
 975	goto usbtmc_clear_check_status;
 976
 977usbtmc_clear_bulk_out_halt:
 978
 979	rv = usb_clear_halt(data->usb_dev,
 980			    usb_sndbulkpipe(data->usb_dev, data->bulk_out));
 981	if (rv < 0) {
 982		dev_err(dev, "usb_control_msg returned %d\n", rv);
 983		goto exit;
 984	}
 985	rv = 0;
 986
 987exit:
 988	kfree(buffer);
 989	return rv;
 990}
 991
 992static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
 993{
 994	int rv;
 995
 996	rv = usb_clear_halt(data->usb_dev,
 997			    usb_sndbulkpipe(data->usb_dev, data->bulk_out));
 998
 999	if (rv < 0) {
1000		dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
1001			rv);
1002		return rv;
1003	}
1004	return 0;
1005}
1006
1007static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
1008{
1009	int rv;
1010
1011	rv = usb_clear_halt(data->usb_dev,
1012			    usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
1013
1014	if (rv < 0) {
1015		dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
1016			rv);
1017		return rv;
1018	}
1019	return 0;
1020}
1021
1022static int get_capabilities(struct usbtmc_device_data *data)
1023{
1024	struct device *dev = &data->usb_dev->dev;
1025	char *buffer;
1026	int rv = 0;
1027
1028	buffer = kmalloc(0x18, GFP_KERNEL);
1029	if (!buffer)
1030		return -ENOMEM;
1031
1032	rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
1033			     USBTMC_REQUEST_GET_CAPABILITIES,
1034			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1035			     0, 0, buffer, 0x18, USBTMC_TIMEOUT);
1036	if (rv < 0) {
1037		dev_err(dev, "usb_control_msg returned %d\n", rv);
1038		goto err_out;
1039	}
1040
1041	dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
1042	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1043		dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
1044		rv = -EPERM;
1045		goto err_out;
1046	}
1047	dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
1048	dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
1049	dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
1050	dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
1051
1052	data->capabilities.interface_capabilities = buffer[4];
1053	data->capabilities.device_capabilities = buffer[5];
1054	data->capabilities.usb488_interface_capabilities = buffer[14];
1055	data->capabilities.usb488_device_capabilities = buffer[15];
1056	data->usb488_caps = (buffer[14] & 0x07) | ((buffer[15] & 0x0f) << 4);
1057	rv = 0;
1058
1059err_out:
1060	kfree(buffer);
1061	return rv;
1062}
1063
1064#define capability_attribute(name)					\
1065static ssize_t name##_show(struct device *dev,				\
1066			   struct device_attribute *attr, char *buf)	\
1067{									\
1068	struct usb_interface *intf = to_usb_interface(dev);		\
1069	struct usbtmc_device_data *data = usb_get_intfdata(intf);	\
1070									\
1071	return sprintf(buf, "%d\n", data->capabilities.name);		\
1072}									\
1073static DEVICE_ATTR_RO(name)
1074
1075capability_attribute(interface_capabilities);
1076capability_attribute(device_capabilities);
1077capability_attribute(usb488_interface_capabilities);
1078capability_attribute(usb488_device_capabilities);
1079
1080static struct attribute *capability_attrs[] = {
1081	&dev_attr_interface_capabilities.attr,
1082	&dev_attr_device_capabilities.attr,
1083	&dev_attr_usb488_interface_capabilities.attr,
1084	&dev_attr_usb488_device_capabilities.attr,
1085	NULL,
1086};
1087
1088static struct attribute_group capability_attr_grp = {
1089	.attrs = capability_attrs,
1090};
1091
1092static ssize_t TermChar_show(struct device *dev,
1093			     struct device_attribute *attr, char *buf)
1094{
1095	struct usb_interface *intf = to_usb_interface(dev);
1096	struct usbtmc_device_data *data = usb_get_intfdata(intf);
1097
1098	return sprintf(buf, "%c\n", data->TermChar);
1099}
1100
1101static ssize_t TermChar_store(struct device *dev,
1102			      struct device_attribute *attr,
1103			      const char *buf, size_t count)
1104{
1105	struct usb_interface *intf = to_usb_interface(dev);
1106	struct usbtmc_device_data *data = usb_get_intfdata(intf);
1107
1108	if (count < 1)
1109		return -EINVAL;
1110	data->TermChar = buf[0];
1111	return count;
1112}
1113static DEVICE_ATTR_RW(TermChar);
1114
1115#define data_attribute(name)						\
1116static ssize_t name##_show(struct device *dev,				\
1117			   struct device_attribute *attr, char *buf)	\
1118{									\
1119	struct usb_interface *intf = to_usb_interface(dev);		\
1120	struct usbtmc_device_data *data = usb_get_intfdata(intf);	\
1121									\
1122	return sprintf(buf, "%d\n", data->name);			\
1123}									\
1124static ssize_t name##_store(struct device *dev,				\
1125			    struct device_attribute *attr,		\
1126			    const char *buf, size_t count)		\
1127{									\
1128	struct usb_interface *intf = to_usb_interface(dev);		\
1129	struct usbtmc_device_data *data = usb_get_intfdata(intf);	\
1130	ssize_t result;							\
1131	unsigned val;							\
1132									\
1133	result = sscanf(buf, "%u\n", &val);				\
1134	if (result != 1)						\
1135		result = -EINVAL;					\
1136	data->name = val;						\
1137	if (result < 0)							\
1138		return result;						\
1139	else								\
1140		return count;						\
1141}									\
1142static DEVICE_ATTR_RW(name)
1143
1144data_attribute(TermCharEnabled);
1145data_attribute(auto_abort);
1146
1147static struct attribute *data_attrs[] = {
1148	&dev_attr_TermChar.attr,
1149	&dev_attr_TermCharEnabled.attr,
1150	&dev_attr_auto_abort.attr,
1151	NULL,
1152};
1153
1154static struct attribute_group data_attr_grp = {
1155	.attrs = data_attrs,
1156};
1157
1158static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
1159{
1160	struct device *dev;
1161	u8 *buffer;
1162	int rv;
1163
1164	dev = &data->intf->dev;
1165
1166	buffer = kmalloc(2, GFP_KERNEL);
1167	if (!buffer)
1168		return -ENOMEM;
1169
1170	rv = usb_control_msg(data->usb_dev,
1171			     usb_rcvctrlpipe(data->usb_dev, 0),
1172			     USBTMC_REQUEST_INDICATOR_PULSE,
1173			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1174			     0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1175
1176	if (rv < 0) {
1177		dev_err(dev, "usb_control_msg returned %d\n", rv);
1178		goto exit;
1179	}
1180
1181	dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1182
1183	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1184		dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1185		rv = -EPERM;
1186		goto exit;
1187	}
1188	rv = 0;
1189
1190exit:
1191	kfree(buffer);
1192	return rv;
1193}
1194
1195static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1196{
1197	struct usbtmc_device_data *data;
1198	int retval = -EBADRQC;
1199
1200	data = file->private_data;
1201	mutex_lock(&data->io_mutex);
1202	if (data->zombie) {
1203		retval = -ENODEV;
1204		goto skip_io_on_zombie;
1205	}
1206
1207	switch (cmd) {
1208	case USBTMC_IOCTL_CLEAR_OUT_HALT:
1209		retval = usbtmc_ioctl_clear_out_halt(data);
1210		break;
1211
1212	case USBTMC_IOCTL_CLEAR_IN_HALT:
1213		retval = usbtmc_ioctl_clear_in_halt(data);
1214		break;
1215
1216	case USBTMC_IOCTL_INDICATOR_PULSE:
1217		retval = usbtmc_ioctl_indicator_pulse(data);
1218		break;
1219
1220	case USBTMC_IOCTL_CLEAR:
1221		retval = usbtmc_ioctl_clear(data);
1222		break;
1223
1224	case USBTMC_IOCTL_ABORT_BULK_OUT:
1225		retval = usbtmc_ioctl_abort_bulk_out(data);
1226		break;
1227
1228	case USBTMC_IOCTL_ABORT_BULK_IN:
1229		retval = usbtmc_ioctl_abort_bulk_in(data);
1230		break;
1231
1232	case USBTMC488_IOCTL_GET_CAPS:
1233		retval = copy_to_user((void __user *)arg,
1234				&data->usb488_caps,
1235				sizeof(data->usb488_caps));
1236		if (retval)
1237			retval = -EFAULT;
1238		break;
1239
1240	case USBTMC488_IOCTL_READ_STB:
1241		retval = usbtmc488_ioctl_read_stb(data, (void __user *)arg);
1242		break;
1243
1244	case USBTMC488_IOCTL_REN_CONTROL:
1245		retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1246						USBTMC488_REQUEST_REN_CONTROL);
1247		break;
1248
1249	case USBTMC488_IOCTL_GOTO_LOCAL:
1250		retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1251						USBTMC488_REQUEST_GOTO_LOCAL);
1252		break;
1253
1254	case USBTMC488_IOCTL_LOCAL_LOCKOUT:
1255		retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1256						USBTMC488_REQUEST_LOCAL_LOCKOUT);
1257		break;
1258	}
1259
1260skip_io_on_zombie:
1261	mutex_unlock(&data->io_mutex);
1262	return retval;
1263}
1264
1265static int usbtmc_fasync(int fd, struct file *file, int on)
1266{
1267	struct usbtmc_device_data *data = file->private_data;
1268
1269	return fasync_helper(fd, file, on, &data->fasync);
1270}
1271
1272static unsigned int usbtmc_poll(struct file *file, poll_table *wait)
1273{
1274	struct usbtmc_device_data *data = file->private_data;
1275	unsigned int mask;
1276
1277	mutex_lock(&data->io_mutex);
1278
1279	if (data->zombie) {
1280		mask = POLLHUP | POLLERR;
1281		goto no_poll;
1282	}
1283
1284	poll_wait(file, &data->waitq, wait);
1285
1286	mask = (atomic_read(&data->srq_asserted)) ? POLLIN | POLLRDNORM : 0;
1287
1288no_poll:
1289	mutex_unlock(&data->io_mutex);
1290	return mask;
1291}
1292
1293static const struct file_operations fops = {
1294	.owner		= THIS_MODULE,
1295	.read		= usbtmc_read,
1296	.write		= usbtmc_write,
1297	.open		= usbtmc_open,
1298	.release	= usbtmc_release,
1299	.unlocked_ioctl	= usbtmc_ioctl,
1300	.fasync         = usbtmc_fasync,
1301	.poll           = usbtmc_poll,
1302	.llseek		= default_llseek,
1303};
1304
1305static struct usb_class_driver usbtmc_class = {
1306	.name =		"usbtmc%d",
1307	.fops =		&fops,
1308	.minor_base =	USBTMC_MINOR_BASE,
1309};
1310
1311static void usbtmc_interrupt(struct urb *urb)
1312{
1313	struct usbtmc_device_data *data = urb->context;
1314	struct device *dev = &data->intf->dev;
1315	int status = urb->status;
1316	int rv;
1317
1318	dev_dbg(&data->intf->dev, "int status: %d len %d\n",
1319		status, urb->actual_length);
1320
1321	switch (status) {
1322	case 0: /* SUCCESS */
1323		/* check for valid STB notification */
1324		if (data->iin_buffer[0] > 0x81) {
1325			data->bNotify1 = data->iin_buffer[0];
1326			data->bNotify2 = data->iin_buffer[1];
1327			atomic_set(&data->iin_data_valid, 1);
1328			wake_up_interruptible(&data->waitq);
1329			goto exit;
1330		}
1331		/* check for SRQ notification */
1332		if (data->iin_buffer[0] == 0x81) {
1333			if (data->fasync)
1334				kill_fasync(&data->fasync,
1335					SIGIO, POLL_IN);
1336
1337			atomic_set(&data->srq_asserted, 1);
1338			wake_up_interruptible(&data->waitq);
1339			goto exit;
1340		}
1341		dev_warn(dev, "invalid notification: %x\n", data->iin_buffer[0]);
1342		break;
1343	case -EOVERFLOW:
1344		dev_err(dev, "overflow with length %d, actual length is %d\n",
1345			data->iin_wMaxPacketSize, urb->actual_length);
1346	case -ECONNRESET:
1347	case -ENOENT:
1348	case -ESHUTDOWN:
1349	case -EILSEQ:
1350	case -ETIME:
1351		/* urb terminated, clean up */
1352		dev_dbg(dev, "urb terminated, status: %d\n", status);
1353		return;
1354	default:
1355		dev_err(dev, "unknown status received: %d\n", status);
1356	}
1357exit:
1358	rv = usb_submit_urb(urb, GFP_ATOMIC);
1359	if (rv)
1360		dev_err(dev, "usb_submit_urb failed: %d\n", rv);
1361}
1362
1363static void usbtmc_free_int(struct usbtmc_device_data *data)
1364{
1365	if (!data->iin_ep_present || !data->iin_urb)
1366		return;
1367	usb_kill_urb(data->iin_urb);
1368	kfree(data->iin_buffer);
1369	usb_free_urb(data->iin_urb);
1370	kref_put(&data->kref, usbtmc_delete);
1371}
1372
1373static int usbtmc_probe(struct usb_interface *intf,
1374			const struct usb_device_id *id)
1375{
1376	struct usbtmc_device_data *data;
1377	struct usb_host_interface *iface_desc;
1378	struct usb_endpoint_descriptor *endpoint;
1379	int n;
1380	int retcode;
1381
1382	dev_dbg(&intf->dev, "%s called\n", __func__);
1383
1384	data = kzalloc(sizeof(*data), GFP_KERNEL);
1385	if (!data)
 
1386		return -ENOMEM;
 
1387
1388	data->intf = intf;
1389	data->id = id;
1390	data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1391	usb_set_intfdata(intf, data);
1392	kref_init(&data->kref);
1393	mutex_init(&data->io_mutex);
1394	init_waitqueue_head(&data->waitq);
1395	atomic_set(&data->iin_data_valid, 0);
1396	atomic_set(&data->srq_asserted, 0);
1397	data->zombie = 0;
1398
1399	/* Determine if it is a Rigol or not */
1400	data->rigol_quirk = 0;
1401	dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1402		le16_to_cpu(data->usb_dev->descriptor.idVendor),
1403		le16_to_cpu(data->usb_dev->descriptor.idProduct));
1404	for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
1405		if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) &&
1406		    (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) {
1407			dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1408			data->rigol_quirk = 1;
1409			break;
1410		}
1411	}
1412
1413	/* Initialize USBTMC bTag and other fields */
1414	data->bTag	= 1;
1415	data->TermCharEnabled = 0;
1416	data->TermChar = '\n';
1417	/*  2 <= bTag <= 127   USBTMC-USB488 subclass specification 4.3.1 */
1418	data->iin_bTag = 2;
1419
1420	/* USBTMC devices have only one setting, so use that */
1421	iface_desc = data->intf->cur_altsetting;
1422	data->ifnum = iface_desc->desc.bInterfaceNumber;
1423
1424	/* Find bulk in endpoint */
1425	for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1426		endpoint = &iface_desc->endpoint[n].desc;
1427
1428		if (usb_endpoint_is_bulk_in(endpoint)) {
1429			data->bulk_in = endpoint->bEndpointAddress;
1430			dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1431				data->bulk_in);
1432			break;
1433		}
1434	}
1435
1436	/* Find bulk out endpoint */
1437	for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1438		endpoint = &iface_desc->endpoint[n].desc;
1439
1440		if (usb_endpoint_is_bulk_out(endpoint)) {
1441			data->bulk_out = endpoint->bEndpointAddress;
1442			dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1443				data->bulk_out);
1444			break;
1445		}
1446	}
1447
1448	if (!data->bulk_out || !data->bulk_in) {
1449		dev_err(&intf->dev, "bulk endpoints not found\n");
1450		retcode = -ENODEV;
1451		goto err_put;
1452	}
1453
1454	/* Find int endpoint */
1455	for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1456		endpoint = &iface_desc->endpoint[n].desc;
1457
1458		if (usb_endpoint_is_int_in(endpoint)) {
1459			data->iin_ep_present = 1;
1460			data->iin_ep = endpoint->bEndpointAddress;
1461			data->iin_wMaxPacketSize = usb_endpoint_maxp(endpoint);
1462			data->iin_interval = endpoint->bInterval;
1463			dev_dbg(&intf->dev, "Found Int in endpoint at %u\n",
1464				data->iin_ep);
1465			break;
1466		}
1467	}
1468
1469	retcode = get_capabilities(data);
1470	if (retcode)
1471		dev_err(&intf->dev, "can't read capabilities\n");
1472	else
1473		retcode = sysfs_create_group(&intf->dev.kobj,
1474					     &capability_attr_grp);
1475
1476	if (data->iin_ep_present) {
1477		/* allocate int urb */
1478		data->iin_urb = usb_alloc_urb(0, GFP_KERNEL);
1479		if (!data->iin_urb) {
1480			retcode = -ENOMEM;
1481			goto error_register;
1482		}
1483
1484		/* Protect interrupt in endpoint data until iin_urb is freed */
1485		kref_get(&data->kref);
1486
1487		/* allocate buffer for interrupt in */
1488		data->iin_buffer = kmalloc(data->iin_wMaxPacketSize,
1489					GFP_KERNEL);
1490		if (!data->iin_buffer) {
1491			retcode = -ENOMEM;
1492			goto error_register;
1493		}
1494
1495		/* fill interrupt urb */
1496		usb_fill_int_urb(data->iin_urb, data->usb_dev,
1497				usb_rcvintpipe(data->usb_dev, data->iin_ep),
1498				data->iin_buffer, data->iin_wMaxPacketSize,
1499				usbtmc_interrupt,
1500				data, data->iin_interval);
1501
1502		retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL);
1503		if (retcode) {
1504			dev_err(&intf->dev, "Failed to submit iin_urb\n");
1505			goto error_register;
1506		}
1507	}
1508
1509	retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1510
1511	retcode = usb_register_dev(intf, &usbtmc_class);
1512	if (retcode) {
1513		dev_err(&intf->dev, "Not able to get a minor"
1514			" (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1515			retcode);
1516		goto error_register;
1517	}
1518	dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1519
1520	return 0;
1521
1522error_register:
1523	sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1524	sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1525	usbtmc_free_int(data);
1526err_put:
1527	kref_put(&data->kref, usbtmc_delete);
1528	return retcode;
1529}
1530
1531static void usbtmc_disconnect(struct usb_interface *intf)
1532{
1533	struct usbtmc_device_data *data;
1534
1535	dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1536
1537	data = usb_get_intfdata(intf);
1538	usb_deregister_dev(intf, &usbtmc_class);
1539	sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1540	sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1541	mutex_lock(&data->io_mutex);
1542	data->zombie = 1;
1543	wake_up_all(&data->waitq);
1544	mutex_unlock(&data->io_mutex);
1545	usbtmc_free_int(data);
1546	kref_put(&data->kref, usbtmc_delete);
1547}
1548
1549static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
1550{
1551	/* this driver does not have pending URBs */
1552	return 0;
1553}
1554
1555static int usbtmc_resume(struct usb_interface *intf)
1556{
1557	return 0;
1558}
1559
1560static struct usb_driver usbtmc_driver = {
1561	.name		= "usbtmc",
1562	.id_table	= usbtmc_devices,
1563	.probe		= usbtmc_probe,
1564	.disconnect	= usbtmc_disconnect,
1565	.suspend	= usbtmc_suspend,
1566	.resume		= usbtmc_resume,
1567};
1568
1569module_usb_driver(usbtmc_driver);
1570
1571MODULE_LICENSE("GPL");