blob: 917a55c4480dc4de6e46ab1bc6cf5f9a759aae21 [file] [log] [blame]
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002 * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07003 *
4 * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
5 * Copyright (C) 2008 Novell, Inc.
6 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * The GNU General Public License is available at
19 * http://www.gnu.org/copyleft/gpl.html.
20 */
21
Andy Shevchenkof4d844c2013-07-15 11:59:42 +030022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070024#include <linux/module.h>
Ilpo Järvinen857cc4d2008-10-30 13:56:47 +020025#include <linux/kernel.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070026#include <linux/fs.h>
27#include <linux/uaccess.h>
28#include <linux/kref.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Dave Penklereb6b92e2016-01-27 19:19:14 +010030#include <linux/poll.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070031#include <linux/mutex.h>
32#include <linux/usb.h>
33#include <linux/usb/tmc.h>
34
35
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -050036#define RIGOL 1
37#define USBTMC_HEADER_SIZE 12
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070038#define USBTMC_MINOR_BASE 176
39
40/*
41 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
42 * large as wMaxPacketSize (which is usually 512 bytes).
43 */
44#define USBTMC_SIZE_IOBUFFER 2048
45
46/* Default USB timeout (in milliseconds) */
Gergely Imreh35f76e892009-09-15 16:03:31 +080047#define USBTMC_TIMEOUT 5000
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070048
49/*
50 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
51 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
52 * packet is never read.
53 */
54#define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
55
Németh Márton6ef48522010-01-10 15:33:45 +010056static const struct usb_device_id usbtmc_devices[] = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070057 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
Greg Kroah-Hartman228dd052009-03-11 13:51:42 -070058 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070059 { 0, } /* terminating entry */
60};
Greg Kroah-Hartman5413aa42008-12-03 16:33:09 -080061MODULE_DEVICE_TABLE(usb, usbtmc_devices);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070062
63/*
64 * This structure is the capabilities for the device
Gergely Imrehd0a38362009-09-07 10:47:01 +080065 * See section 4.2.1.8 of the USBTMC specification,
66 * and section 4.2.2 of the USBTMC usb488 subclass
67 * specification for details.
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070068 */
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
Dave Penklerdbf3e7f2016-01-27 19:09:24 +010091 /* 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;
Dave Penkler82ed3382016-01-27 19:15:15 +0100103 atomic_t srq_asserted;
Dave Penklerdbf3e7f2016-01-27 19:09:24 +0100104
Dave Penkler29779d892016-01-27 19:22:28 +0100105 /* coalesced usb488_caps from usbtmc_dev_capabilities */
106 __u8 usb488_caps;
107
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -0500108 u8 rigol_quirk;
109
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700110 /* attributes from the USB TMC spec for this device */
111 u8 TermChar;
112 bool TermCharEnabled;
113 bool auto_abort;
114
Oliver Neukum86286882009-07-02 11:36:30 +0200115 bool zombie; /* fd of disconnected device */
116
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700117 struct usbtmc_dev_capabilities capabilities;
118 struct kref kref;
119 struct mutex io_mutex; /* only one i/o function running at a time */
Dave Penklerdbf3e7f2016-01-27 19:09:24 +0100120 wait_queue_head_t waitq;
Dave Penkler82ed3382016-01-27 19:15:15 +0100121 struct fasync_struct *fasync;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700122};
123#define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
124
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -0500125struct 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 },
Teunis van Beelenf5042022015-05-31 09:36:22 +0200132 { 0x1ab1, 0x04b0 },
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -0500133 { 0, 0 }
134};
135
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700136/* 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);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700144}
145
146static int usbtmc_open(struct inode *inode, struct file *filp)
147{
148 struct usb_interface *intf;
149 struct usbtmc_device_data *data;
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700150 int retval = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700151
152 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
153 if (!intf) {
Andy Shevchenkof4d844c2013-07-15 11:59:42 +0300154 pr_err("can not find device for minor %d", iminor(inode));
155 return -ENODEV;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700156 }
157
158 data = usb_get_intfdata(intf);
159 kref_get(&data->kref);
160
161 /* Store pointer in file structure's private data field */
162 filp->private_data = data;
163
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700164 return retval;
165}
166
167static int usbtmc_release(struct inode *inode, struct file *file)
168{
169 struct usbtmc_device_data *data = file->private_data;
170
171 kref_put(&data->kref, usbtmc_delete);
172 return 0;
173}
174
175static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
176{
Chris Malleyb361a6e2008-10-25 22:07:32 +0100177 u8 *buffer;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700178 struct device *dev;
179 int rv;
180 int n;
181 int actual;
182 struct usb_host_interface *current_setting;
183 int max_size;
184
185 dev = &data->intf->dev;
186 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
187 if (!buffer)
188 return -ENOMEM;
189
190 rv = usb_control_msg(data->usb_dev,
191 usb_rcvctrlpipe(data->usb_dev, 0),
192 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
193 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
194 data->bTag_last_read, data->bulk_in,
195 buffer, 2, USBTMC_TIMEOUT);
196
197 if (rv < 0) {
198 dev_err(dev, "usb_control_msg returned %d\n", rv);
199 goto exit;
200 }
201
202 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
203
204 if (buffer[0] == USBTMC_STATUS_FAILED) {
205 rv = 0;
206 goto exit;
207 }
208
209 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
210 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
211 buffer[0]);
212 rv = -EPERM;
213 goto exit;
214 }
215
216 max_size = 0;
217 current_setting = data->intf->cur_altsetting;
218 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
219 if (current_setting->endpoint[n].desc.bEndpointAddress ==
220 data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700221 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700222
223 if (max_size == 0) {
224 dev_err(dev, "Couldn't get wMaxPacketSize\n");
225 rv = -EPERM;
226 goto exit;
227 }
228
229 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
230
231 n = 0;
232
233 do {
234 dev_dbg(dev, "Reading from bulk in EP\n");
235
236 rv = usb_bulk_msg(data->usb_dev,
237 usb_rcvbulkpipe(data->usb_dev,
238 data->bulk_in),
239 buffer, USBTMC_SIZE_IOBUFFER,
240 &actual, USBTMC_TIMEOUT);
241
242 n++;
243
244 if (rv < 0) {
245 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
246 goto exit;
247 }
248 } while ((actual == max_size) &&
249 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
250
251 if (actual == max_size) {
252 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
253 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
254 rv = -EPERM;
255 goto exit;
256 }
257
258 n = 0;
259
260usbtmc_abort_bulk_in_status:
261 rv = usb_control_msg(data->usb_dev,
262 usb_rcvctrlpipe(data->usb_dev, 0),
263 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
264 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
265 0, data->bulk_in, buffer, 0x08,
266 USBTMC_TIMEOUT);
267
268 if (rv < 0) {
269 dev_err(dev, "usb_control_msg returned %d\n", rv);
270 goto exit;
271 }
272
273 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
274
275 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
276 rv = 0;
277 goto exit;
278 }
279
280 if (buffer[0] != USBTMC_STATUS_PENDING) {
281 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
282 rv = -EPERM;
283 goto exit;
284 }
285
286 if (buffer[1] == 1)
287 do {
288 dev_dbg(dev, "Reading from bulk in EP\n");
289
290 rv = usb_bulk_msg(data->usb_dev,
291 usb_rcvbulkpipe(data->usb_dev,
292 data->bulk_in),
293 buffer, USBTMC_SIZE_IOBUFFER,
294 &actual, USBTMC_TIMEOUT);
295
296 n++;
297
298 if (rv < 0) {
299 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
300 goto exit;
301 }
Maxim Nikulin4f1a7a32011-07-09 23:44:44 +0700302 } while ((actual == max_size) &&
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700303 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
304
305 if (actual == max_size) {
306 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
307 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
308 rv = -EPERM;
309 goto exit;
310 }
311
312 goto usbtmc_abort_bulk_in_status;
313
314exit:
315 kfree(buffer);
316 return rv;
317
318}
319
320static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
321{
322 struct device *dev;
323 u8 *buffer;
324 int rv;
325 int n;
326
327 dev = &data->intf->dev;
328
329 buffer = kmalloc(8, GFP_KERNEL);
330 if (!buffer)
331 return -ENOMEM;
332
333 rv = usb_control_msg(data->usb_dev,
334 usb_rcvctrlpipe(data->usb_dev, 0),
335 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
336 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
337 data->bTag_last_write, data->bulk_out,
338 buffer, 2, USBTMC_TIMEOUT);
339
340 if (rv < 0) {
341 dev_err(dev, "usb_control_msg returned %d\n", rv);
342 goto exit;
343 }
344
345 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
346
347 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
348 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
349 buffer[0]);
350 rv = -EPERM;
351 goto exit;
352 }
353
354 n = 0;
355
356usbtmc_abort_bulk_out_check_status:
357 rv = usb_control_msg(data->usb_dev,
358 usb_rcvctrlpipe(data->usb_dev, 0),
359 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
360 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
361 0, data->bulk_out, buffer, 0x08,
362 USBTMC_TIMEOUT);
363 n++;
364 if (rv < 0) {
365 dev_err(dev, "usb_control_msg returned %d\n", rv);
366 goto exit;
367 }
368
369 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
370
371 if (buffer[0] == USBTMC_STATUS_SUCCESS)
372 goto usbtmc_abort_bulk_out_clear_halt;
373
374 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
375 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
376 goto usbtmc_abort_bulk_out_check_status;
377
378 rv = -EPERM;
379 goto exit;
380
381usbtmc_abort_bulk_out_clear_halt:
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800382 rv = usb_clear_halt(data->usb_dev,
383 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700384
385 if (rv < 0) {
386 dev_err(dev, "usb_control_msg returned %d\n", rv);
387 goto exit;
388 }
389 rv = 0;
390
391exit:
392 kfree(buffer);
393 return rv;
394}
395
Dave Penklerdbf3e7f2016-01-27 19:09:24 +0100396static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data *data,
397 void __user *arg)
398{
399 struct device *dev = &data->intf->dev;
400 u8 *buffer;
401 u8 tag;
402 __u8 stb;
403 int rv;
404
405 dev_dbg(dev, "Enter ioctl_read_stb iin_ep_present: %d\n",
406 data->iin_ep_present);
407
408 buffer = kmalloc(8, GFP_KERNEL);
409 if (!buffer)
410 return -ENOMEM;
411
412 atomic_set(&data->iin_data_valid, 0);
413
Dave Penkler82ed3382016-01-27 19:15:15 +0100414 /* must issue read_stb before using poll or select */
415 atomic_set(&data->srq_asserted, 0);
416
Dave Penklerdbf3e7f2016-01-27 19:09:24 +0100417 rv = usb_control_msg(data->usb_dev,
418 usb_rcvctrlpipe(data->usb_dev, 0),
419 USBTMC488_REQUEST_READ_STATUS_BYTE,
420 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
421 data->iin_bTag,
422 data->ifnum,
423 buffer, 0x03, USBTMC_TIMEOUT);
424 if (rv < 0) {
425 dev_err(dev, "stb usb_control_msg returned %d\n", rv);
426 goto exit;
427 }
428
429 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
430 dev_err(dev, "control status returned %x\n", buffer[0]);
431 rv = -EIO;
432 goto exit;
433 }
434
435 if (data->iin_ep_present) {
436 rv = wait_event_interruptible_timeout(
437 data->waitq,
438 atomic_read(&data->iin_data_valid) != 0,
439 USBTMC_TIMEOUT);
440 if (rv < 0) {
441 dev_dbg(dev, "wait interrupted %d\n", rv);
442 goto exit;
443 }
444
445 if (rv == 0) {
446 dev_dbg(dev, "wait timed out\n");
447 rv = -ETIME;
448 goto exit;
449 }
450
451 tag = data->bNotify1 & 0x7f;
452 if (tag != data->iin_bTag) {
453 dev_err(dev, "expected bTag %x got %x\n",
454 data->iin_bTag, tag);
455 }
456
457 stb = data->bNotify2;
458 } else {
459 stb = buffer[2];
460 }
461
462 rv = copy_to_user(arg, &stb, sizeof(stb));
463 if (rv)
464 rv = -EFAULT;
465
466 exit:
467 /* bump interrupt bTag */
468 data->iin_bTag += 1;
469 if (data->iin_bTag > 127)
470 /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
471 data->iin_bTag = 2;
472
473 kfree(buffer);
474 return rv;
475}
476
Dave Penkler379d3d32016-01-27 19:25:24 +0100477static int usbtmc488_ioctl_simple(struct usbtmc_device_data *data,
478 void __user *arg, unsigned int cmd)
479{
480 struct device *dev = &data->intf->dev;
481 __u8 val;
482 u8 *buffer;
483 u16 wValue;
484 int rv;
485
486 if (!(data->usb488_caps & USBTMC488_CAPABILITY_SIMPLE))
487 return -EINVAL;
488
489 buffer = kmalloc(8, GFP_KERNEL);
490 if (!buffer)
491 return -ENOMEM;
492
493 if (cmd == USBTMC488_REQUEST_REN_CONTROL) {
494 rv = copy_from_user(&val, arg, sizeof(val));
495 if (rv) {
496 rv = -EFAULT;
497 goto exit;
498 }
499 wValue = val ? 1 : 0;
500 } else {
501 wValue = 0;
502 }
503
504 rv = usb_control_msg(data->usb_dev,
505 usb_rcvctrlpipe(data->usb_dev, 0),
506 cmd,
507 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
508 wValue,
509 data->ifnum,
510 buffer, 0x01, USBTMC_TIMEOUT);
511 if (rv < 0) {
512 dev_err(dev, "simple usb_control_msg failed %d\n", rv);
513 goto exit;
514 } else if (rv != 1) {
515 dev_warn(dev, "simple usb_control_msg returned %d\n", rv);
516 rv = -EIO;
517 goto exit;
518 }
519
520 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
521 dev_err(dev, "simple control status returned %x\n", buffer[0]);
522 rv = -EIO;
523 goto exit;
524 }
525 rv = 0;
526
527 exit:
528 kfree(buffer);
529 return rv;
530}
531
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500532/*
533 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
534 * @transfer_size: number of bytes to request from the device.
535 *
536 * See the USBTMC specification, Table 4.
537 *
538 * Also updates bTag_last_write.
539 */
540static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
541{
542 int retval;
Oliver Neukumd846b762014-05-19 13:54:57 +0200543 u8 *buffer;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500544 int actual;
545
Oliver Neukumd846b762014-05-19 13:54:57 +0200546 buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
547 if (!buffer)
548 return -ENOMEM;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500549 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
550 * Refer to class specs for details
551 */
552 buffer[0] = 2;
553 buffer[1] = data->bTag;
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300554 buffer[2] = ~data->bTag;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500555 buffer[3] = 0; /* Reserved */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300556 buffer[4] = transfer_size >> 0;
557 buffer[5] = transfer_size >> 8;
558 buffer[6] = transfer_size >> 16;
559 buffer[7] = transfer_size >> 24;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500560 buffer[8] = data->TermCharEnabled * 2;
561 /* Use term character? */
562 buffer[9] = data->TermChar;
563 buffer[10] = 0; /* Reserved */
564 buffer[11] = 0; /* Reserved */
565
566 /* Send bulk URB */
567 retval = usb_bulk_msg(data->usb_dev,
568 usb_sndbulkpipe(data->usb_dev,
569 data->bulk_out),
570 buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
571
572 /* Store bTag (in case we need to abort) */
573 data->bTag_last_write = data->bTag;
574
575 /* Increment bTag -- and increment again if zero */
576 data->bTag++;
577 if (!data->bTag)
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300578 data->bTag++;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500579
Oliver Neukumd846b762014-05-19 13:54:57 +0200580 kfree(buffer);
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500581 if (retval < 0) {
582 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
583 return retval;
584 }
585
586 return 0;
587}
588
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700589static ssize_t usbtmc_read(struct file *filp, char __user *buf,
590 size_t count, loff_t *f_pos)
591{
592 struct usbtmc_device_data *data;
593 struct device *dev;
Guus Sliepen665d7662009-07-22 17:39:42 +0200594 u32 n_characters;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700595 u8 *buffer;
596 int actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200597 size_t done;
598 size_t remaining;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700599 int retval;
Guus Sliepen665d7662009-07-22 17:39:42 +0200600 size_t this_part;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700601
602 /* Get pointer to private data structure */
603 data = filp->private_data;
604 dev = &data->intf->dev;
605
606 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
607 if (!buffer)
608 return -ENOMEM;
609
610 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200611 if (data->zombie) {
612 retval = -ENODEV;
613 goto exit;
614 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700615
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500616 if (data->rigol_quirk) {
617 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700618
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500619 retval = send_request_dev_dep_msg_in(data, count);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700620
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700621 if (retval < 0) {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700622 if (data->auto_abort)
623 usbtmc_ioctl_abort_bulk_out(data);
624 goto exit;
625 }
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500626 }
627
628 /* Loop until we have fetched everything we requested */
629 remaining = count;
630 this_part = remaining;
631 done = 0;
632
633 while (remaining > 0) {
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300634 if (!data->rigol_quirk) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500635 dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
636
637 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
638 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
639 else
640 this_part = remaining;
641
642 retval = send_request_dev_dep_msg_in(data, this_part);
643 if (retval < 0) {
644 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
645 if (data->auto_abort)
646 usbtmc_ioctl_abort_bulk_out(data);
647 goto exit;
648 }
649 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700650
651 /* Send bulk URB */
652 retval = usb_bulk_msg(data->usb_dev,
653 usb_rcvbulkpipe(data->usb_dev,
654 data->bulk_in),
655 buffer, USBTMC_SIZE_IOBUFFER, &actual,
656 USBTMC_TIMEOUT);
657
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500658 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
659
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700660 /* Store bTag (in case we need to abort) */
661 data->bTag_last_read = data->bTag;
662
663 if (retval < 0) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500664 dev_dbg(dev, "Unable to read data, error %d\n", retval);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700665 if (data->auto_abort)
666 usbtmc_ioctl_abort_bulk_in(data);
667 goto exit;
668 }
669
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500670 /* Parse header in first packet */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300671 if ((done == 0) || !data->rigol_quirk) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500672 /* Sanity checks for the header */
673 if (actual < USBTMC_HEADER_SIZE) {
674 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
675 if (data->auto_abort)
676 usbtmc_ioctl_abort_bulk_in(data);
677 goto exit;
678 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700679
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500680 if (buffer[0] != 2) {
681 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
682 if (data->auto_abort)
683 usbtmc_ioctl_abort_bulk_in(data);
684 goto exit;
685 }
686
687 if (buffer[1] != data->bTag_last_write) {
688 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
689 if (data->auto_abort)
690 usbtmc_ioctl_abort_bulk_in(data);
691 goto exit;
692 }
693
694 /* How many characters did the instrument send? */
695 n_characters = buffer[4] +
696 (buffer[5] << 8) +
697 (buffer[6] << 16) +
698 (buffer[7] << 24);
699
700 if (n_characters > this_part) {
701 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
702 if (data->auto_abort)
703 usbtmc_ioctl_abort_bulk_in(data);
704 goto exit;
705 }
706
707 /* Remove the USBTMC header */
708 actual -= USBTMC_HEADER_SIZE;
709
710 /* Check if the message is smaller than requested */
711 if (data->rigol_quirk) {
712 if (remaining > n_characters)
713 remaining = n_characters;
714 /* Remove padding if it exists */
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300715 if (actual > remaining)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500716 actual = remaining;
717 }
718 else {
719 if (this_part > n_characters)
720 this_part = n_characters;
721 /* Remove padding if it exists */
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300722 if (actual > this_part)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500723 actual = this_part;
724 }
725
726 dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
727
728 remaining -= actual;
729
730 /* Terminate if end-of-message bit received from device */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300731 if ((buffer[8] & 0x01) && (actual >= n_characters))
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500732 remaining = 0;
733
734 dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
735
736
737 /* Copy buffer to user space */
738 if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
739 /* There must have been an addressing problem */
740 retval = -EFAULT;
741 goto exit;
742 }
743 done += actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200744 }
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500745 else {
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300746 if (actual > remaining)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500747 actual = remaining;
Guus Sliepen665d7662009-07-22 17:39:42 +0200748
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500749 remaining -= actual;
750
751 dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
752
753 /* Copy buffer to user space */
754 if (copy_to_user(buf + done, buffer, actual)) {
755 /* There must have been an addressing problem */
756 retval = -EFAULT;
757 goto exit;
758 }
759 done += actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200760 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700761 }
762
763 /* Update file position value */
764 *f_pos = *f_pos + done;
765 retval = done;
766
767exit:
768 mutex_unlock(&data->io_mutex);
769 kfree(buffer);
770 return retval;
771}
772
773static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
774 size_t count, loff_t *f_pos)
775{
776 struct usbtmc_device_data *data;
777 u8 *buffer;
778 int retval;
779 int actual;
780 unsigned long int n_bytes;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700781 int remaining;
782 int done;
783 int this_part;
784
785 data = filp->private_data;
786
787 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
788 if (!buffer)
789 return -ENOMEM;
790
791 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200792 if (data->zombie) {
793 retval = -ENODEV;
794 goto exit;
795 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700796
797 remaining = count;
798 done = 0;
799
800 while (remaining > 0) {
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500801 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
802 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700803 buffer[8] = 0;
804 } else {
805 this_part = remaining;
806 buffer[8] = 1;
807 }
808
809 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
810 buffer[0] = 1;
811 buffer[1] = data->bTag;
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300812 buffer[2] = ~data->bTag;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700813 buffer[3] = 0; /* Reserved */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300814 buffer[4] = this_part >> 0;
815 buffer[5] = this_part >> 8;
816 buffer[6] = this_part >> 16;
817 buffer[7] = this_part >> 24;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700818 /* buffer[8] is set above... */
819 buffer[9] = 0; /* Reserved */
820 buffer[10] = 0; /* Reserved */
821 buffer[11] = 0; /* Reserved */
822
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500823 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700824 retval = -EFAULT;
825 goto exit;
826 }
827
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500828 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
829 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700830
Andre Hermsec412b92009-11-19 18:14:49 +0100831 do {
832 retval = usb_bulk_msg(data->usb_dev,
833 usb_sndbulkpipe(data->usb_dev,
834 data->bulk_out),
835 buffer, n_bytes,
836 &actual, USBTMC_TIMEOUT);
837 if (retval != 0)
838 break;
839 n_bytes -= actual;
840 } while (n_bytes);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700841
842 data->bTag_last_write = data->bTag;
843 data->bTag++;
844
845 if (!data->bTag)
846 data->bTag++;
847
848 if (retval < 0) {
849 dev_err(&data->intf->dev,
850 "Unable to send data, error %d\n", retval);
851 if (data->auto_abort)
852 usbtmc_ioctl_abort_bulk_out(data);
853 goto exit;
854 }
855
856 remaining -= this_part;
857 done += this_part;
858 }
859
860 retval = count;
861exit:
862 mutex_unlock(&data->io_mutex);
863 kfree(buffer);
864 return retval;
865}
866
867static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
868{
869 struct usb_host_interface *current_setting;
870 struct usb_endpoint_descriptor *desc;
871 struct device *dev;
872 u8 *buffer;
873 int rv;
874 int n;
Rickard Strandqvistab53eb92014-06-01 15:43:19 +0200875 int actual = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700876 int max_size;
877
878 dev = &data->intf->dev;
879
880 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
881
882 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
883 if (!buffer)
884 return -ENOMEM;
885
886 rv = usb_control_msg(data->usb_dev,
887 usb_rcvctrlpipe(data->usb_dev, 0),
888 USBTMC_REQUEST_INITIATE_CLEAR,
889 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
890 0, 0, buffer, 1, USBTMC_TIMEOUT);
891 if (rv < 0) {
892 dev_err(dev, "usb_control_msg returned %d\n", rv);
893 goto exit;
894 }
895
896 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
897
898 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
899 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
900 rv = -EPERM;
901 goto exit;
902 }
903
904 max_size = 0;
905 current_setting = data->intf->cur_altsetting;
906 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
907 desc = &current_setting->endpoint[n].desc;
908 if (desc->bEndpointAddress == data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700909 max_size = usb_endpoint_maxp(desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700910 }
911
912 if (max_size == 0) {
913 dev_err(dev, "Couldn't get wMaxPacketSize\n");
914 rv = -EPERM;
915 goto exit;
916 }
917
918 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
919
920 n = 0;
921
922usbtmc_clear_check_status:
923
924 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
925
926 rv = usb_control_msg(data->usb_dev,
927 usb_rcvctrlpipe(data->usb_dev, 0),
928 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
929 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
930 0, 0, buffer, 2, USBTMC_TIMEOUT);
931 if (rv < 0) {
932 dev_err(dev, "usb_control_msg returned %d\n", rv);
933 goto exit;
934 }
935
936 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
937
938 if (buffer[0] == USBTMC_STATUS_SUCCESS)
939 goto usbtmc_clear_bulk_out_halt;
940
941 if (buffer[0] != USBTMC_STATUS_PENDING) {
942 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
943 rv = -EPERM;
944 goto exit;
945 }
946
947 if (buffer[1] == 1)
948 do {
949 dev_dbg(dev, "Reading from bulk in EP\n");
950
951 rv = usb_bulk_msg(data->usb_dev,
952 usb_rcvbulkpipe(data->usb_dev,
953 data->bulk_in),
954 buffer, USBTMC_SIZE_IOBUFFER,
955 &actual, USBTMC_TIMEOUT);
956 n++;
957
958 if (rv < 0) {
959 dev_err(dev, "usb_control_msg returned %d\n",
960 rv);
961 goto exit;
962 }
963 } while ((actual == max_size) &&
964 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
965
966 if (actual == max_size) {
967 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
968 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
969 rv = -EPERM;
970 goto exit;
971 }
972
973 goto usbtmc_clear_check_status;
974
975usbtmc_clear_bulk_out_halt:
976
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800977 rv = usb_clear_halt(data->usb_dev,
978 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700979 if (rv < 0) {
980 dev_err(dev, "usb_control_msg returned %d\n", rv);
981 goto exit;
982 }
983 rv = 0;
984
985exit:
986 kfree(buffer);
987 return rv;
988}
989
990static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
991{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700992 int rv;
993
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800994 rv = usb_clear_halt(data->usb_dev,
995 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700996
997 if (rv < 0) {
998 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
999 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +08001000 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001001 }
Ming Leiac9e59c2013-04-18 12:17:38 +08001002 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001003}
1004
1005static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
1006{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001007 int rv;
1008
Sarah Sharp3342ecd2009-12-03 11:35:59 -08001009 rv = usb_clear_halt(data->usb_dev,
1010 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001011
1012 if (rv < 0) {
1013 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
1014 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +08001015 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001016 }
Ming Leiac9e59c2013-04-18 12:17:38 +08001017 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001018}
1019
1020static int get_capabilities(struct usbtmc_device_data *data)
1021{
1022 struct device *dev = &data->usb_dev->dev;
1023 char *buffer;
Oliver Neukumca157c42009-07-02 16:41:39 +02001024 int rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001025
1026 buffer = kmalloc(0x18, GFP_KERNEL);
1027 if (!buffer)
1028 return -ENOMEM;
1029
1030 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
1031 USBTMC_REQUEST_GET_CAPABILITIES,
1032 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1033 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
1034 if (rv < 0) {
1035 dev_err(dev, "usb_control_msg returned %d\n", rv);
Oliver Neukumca157c42009-07-02 16:41:39 +02001036 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001037 }
1038
1039 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001040 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1041 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Oliver Neukumca157c42009-07-02 16:41:39 +02001042 rv = -EPERM;
1043 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001044 }
Gergely Imrehd0a38362009-09-07 10:47:01 +08001045 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
1046 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
1047 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
1048 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001049
1050 data->capabilities.interface_capabilities = buffer[4];
1051 data->capabilities.device_capabilities = buffer[5];
1052 data->capabilities.usb488_interface_capabilities = buffer[14];
1053 data->capabilities.usb488_device_capabilities = buffer[15];
Dave Penkler29779d892016-01-27 19:22:28 +01001054 data->usb488_caps = (buffer[14] & 0x07) | ((buffer[15] & 0x0f) << 4);
Gergely Imrehd0a38362009-09-07 10:47:01 +08001055 rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001056
Oliver Neukumca157c42009-07-02 16:41:39 +02001057err_out:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001058 kfree(buffer);
Oliver Neukumca157c42009-07-02 16:41:39 +02001059 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001060}
1061
1062#define capability_attribute(name) \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001063static ssize_t name##_show(struct device *dev, \
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001064 struct device_attribute *attr, char *buf) \
1065{ \
1066 struct usb_interface *intf = to_usb_interface(dev); \
1067 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1068 \
1069 return sprintf(buf, "%d\n", data->capabilities.name); \
1070} \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001071static DEVICE_ATTR_RO(name)
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001072
1073capability_attribute(interface_capabilities);
1074capability_attribute(device_capabilities);
1075capability_attribute(usb488_interface_capabilities);
1076capability_attribute(usb488_device_capabilities);
1077
1078static struct attribute *capability_attrs[] = {
1079 &dev_attr_interface_capabilities.attr,
1080 &dev_attr_device_capabilities.attr,
1081 &dev_attr_usb488_interface_capabilities.attr,
1082 &dev_attr_usb488_device_capabilities.attr,
1083 NULL,
1084};
1085
1086static struct attribute_group capability_attr_grp = {
1087 .attrs = capability_attrs,
1088};
1089
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001090static ssize_t TermChar_show(struct device *dev,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001091 struct device_attribute *attr, char *buf)
1092{
1093 struct usb_interface *intf = to_usb_interface(dev);
1094 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1095
1096 return sprintf(buf, "%c\n", data->TermChar);
1097}
1098
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001099static ssize_t TermChar_store(struct device *dev,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001100 struct device_attribute *attr,
1101 const char *buf, size_t count)
1102{
1103 struct usb_interface *intf = to_usb_interface(dev);
1104 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1105
1106 if (count < 1)
1107 return -EINVAL;
1108 data->TermChar = buf[0];
1109 return count;
1110}
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001111static DEVICE_ATTR_RW(TermChar);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001112
1113#define data_attribute(name) \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001114static ssize_t name##_show(struct device *dev, \
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001115 struct device_attribute *attr, char *buf) \
1116{ \
1117 struct usb_interface *intf = to_usb_interface(dev); \
1118 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1119 \
1120 return sprintf(buf, "%d\n", data->name); \
1121} \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001122static ssize_t name##_store(struct device *dev, \
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001123 struct device_attribute *attr, \
1124 const char *buf, size_t count) \
1125{ \
1126 struct usb_interface *intf = to_usb_interface(dev); \
1127 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1128 ssize_t result; \
1129 unsigned val; \
1130 \
1131 result = sscanf(buf, "%u\n", &val); \
1132 if (result != 1) \
1133 result = -EINVAL; \
1134 data->name = val; \
1135 if (result < 0) \
1136 return result; \
1137 else \
1138 return count; \
1139} \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001140static DEVICE_ATTR_RW(name)
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001141
1142data_attribute(TermCharEnabled);
1143data_attribute(auto_abort);
1144
1145static struct attribute *data_attrs[] = {
1146 &dev_attr_TermChar.attr,
1147 &dev_attr_TermCharEnabled.attr,
1148 &dev_attr_auto_abort.attr,
1149 NULL,
1150};
1151
1152static struct attribute_group data_attr_grp = {
1153 .attrs = data_attrs,
1154};
1155
1156static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
1157{
1158 struct device *dev;
1159 u8 *buffer;
1160 int rv;
1161
1162 dev = &data->intf->dev;
1163
1164 buffer = kmalloc(2, GFP_KERNEL);
1165 if (!buffer)
1166 return -ENOMEM;
1167
1168 rv = usb_control_msg(data->usb_dev,
1169 usb_rcvctrlpipe(data->usb_dev, 0),
1170 USBTMC_REQUEST_INDICATOR_PULSE,
1171 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1172 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1173
1174 if (rv < 0) {
1175 dev_err(dev, "usb_control_msg returned %d\n", rv);
1176 goto exit;
1177 }
1178
1179 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1180
1181 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1182 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1183 rv = -EPERM;
1184 goto exit;
1185 }
1186 rv = 0;
1187
1188exit:
1189 kfree(buffer);
1190 return rv;
1191}
1192
1193static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1194{
1195 struct usbtmc_device_data *data;
1196 int retval = -EBADRQC;
1197
1198 data = file->private_data;
1199 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +02001200 if (data->zombie) {
1201 retval = -ENODEV;
1202 goto skip_io_on_zombie;
1203 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001204
1205 switch (cmd) {
1206 case USBTMC_IOCTL_CLEAR_OUT_HALT:
1207 retval = usbtmc_ioctl_clear_out_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001208 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001209
1210 case USBTMC_IOCTL_CLEAR_IN_HALT:
1211 retval = usbtmc_ioctl_clear_in_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001212 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001213
1214 case USBTMC_IOCTL_INDICATOR_PULSE:
1215 retval = usbtmc_ioctl_indicator_pulse(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001216 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001217
1218 case USBTMC_IOCTL_CLEAR:
1219 retval = usbtmc_ioctl_clear(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001220 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001221
1222 case USBTMC_IOCTL_ABORT_BULK_OUT:
1223 retval = usbtmc_ioctl_abort_bulk_out(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001224 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001225
1226 case USBTMC_IOCTL_ABORT_BULK_IN:
1227 retval = usbtmc_ioctl_abort_bulk_in(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001228 break;
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001229
Dave Penkler29779d892016-01-27 19:22:28 +01001230 case USBTMC488_IOCTL_GET_CAPS:
1231 retval = copy_to_user((void __user *)arg,
1232 &data->usb488_caps,
1233 sizeof(data->usb488_caps));
1234 if (retval)
1235 retval = -EFAULT;
1236 break;
1237
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001238 case USBTMC488_IOCTL_READ_STB:
1239 retval = usbtmc488_ioctl_read_stb(data, (void __user *)arg);
1240 break;
Dave Penkler379d3d32016-01-27 19:25:24 +01001241
1242 case USBTMC488_IOCTL_REN_CONTROL:
1243 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1244 USBTMC488_REQUEST_REN_CONTROL);
1245 break;
1246
1247 case USBTMC488_IOCTL_GOTO_LOCAL:
1248 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1249 USBTMC488_REQUEST_GOTO_LOCAL);
1250 break;
1251
1252 case USBTMC488_IOCTL_LOCAL_LOCKOUT:
1253 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1254 USBTMC488_REQUEST_LOCAL_LOCKOUT);
1255 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001256 }
1257
Oliver Neukum86286882009-07-02 11:36:30 +02001258skip_io_on_zombie:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001259 mutex_unlock(&data->io_mutex);
1260 return retval;
1261}
1262
Dave Penkler82ed3382016-01-27 19:15:15 +01001263static int usbtmc_fasync(int fd, struct file *file, int on)
1264{
1265 struct usbtmc_device_data *data = file->private_data;
1266
1267 return fasync_helper(fd, file, on, &data->fasync);
1268}
1269
Dave Penklereb6b92e2016-01-27 19:19:14 +01001270static unsigned int usbtmc_poll(struct file *file, poll_table *wait)
1271{
1272 struct usbtmc_device_data *data = file->private_data;
1273 unsigned int mask;
1274
1275 mutex_lock(&data->io_mutex);
1276
1277 if (data->zombie) {
1278 mask = POLLHUP | POLLERR;
1279 goto no_poll;
1280 }
1281
1282 poll_wait(file, &data->waitq, wait);
1283
1284 mask = (atomic_read(&data->srq_asserted)) ? POLLIN | POLLRDNORM : 0;
1285
1286no_poll:
1287 mutex_unlock(&data->io_mutex);
1288 return mask;
1289}
1290
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001291static const struct file_operations fops = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001292 .owner = THIS_MODULE,
1293 .read = usbtmc_read,
1294 .write = usbtmc_write,
1295 .open = usbtmc_open,
1296 .release = usbtmc_release,
1297 .unlocked_ioctl = usbtmc_ioctl,
Dave Penkler82ed3382016-01-27 19:15:15 +01001298 .fasync = usbtmc_fasync,
Dave Penklereb6b92e2016-01-27 19:19:14 +01001299 .poll = usbtmc_poll,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001300 .llseek = default_llseek,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001301};
1302
1303static struct usb_class_driver usbtmc_class = {
1304 .name = "usbtmc%d",
1305 .fops = &fops,
1306 .minor_base = USBTMC_MINOR_BASE,
1307};
1308
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001309static void usbtmc_interrupt(struct urb *urb)
1310{
1311 struct usbtmc_device_data *data = urb->context;
1312 struct device *dev = &data->intf->dev;
1313 int status = urb->status;
1314 int rv;
1315
1316 dev_dbg(&data->intf->dev, "int status: %d len %d\n",
1317 status, urb->actual_length);
1318
1319 switch (status) {
1320 case 0: /* SUCCESS */
1321 /* check for valid STB notification */
1322 if (data->iin_buffer[0] > 0x81) {
1323 data->bNotify1 = data->iin_buffer[0];
1324 data->bNotify2 = data->iin_buffer[1];
1325 atomic_set(&data->iin_data_valid, 1);
1326 wake_up_interruptible(&data->waitq);
1327 goto exit;
1328 }
Dave Penkler82ed3382016-01-27 19:15:15 +01001329 /* check for SRQ notification */
1330 if (data->iin_buffer[0] == 0x81) {
1331 if (data->fasync)
1332 kill_fasync(&data->fasync,
1333 SIGIO, POLL_IN);
1334
1335 atomic_set(&data->srq_asserted, 1);
1336 wake_up_interruptible(&data->waitq);
1337 goto exit;
1338 }
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001339 dev_warn(dev, "invalid notification: %x\n", data->iin_buffer[0]);
1340 break;
1341 case -EOVERFLOW:
1342 dev_err(dev, "overflow with length %d, actual length is %d\n",
1343 data->iin_wMaxPacketSize, urb->actual_length);
1344 case -ECONNRESET:
1345 case -ENOENT:
1346 case -ESHUTDOWN:
1347 case -EILSEQ:
1348 case -ETIME:
1349 /* urb terminated, clean up */
1350 dev_dbg(dev, "urb terminated, status: %d\n", status);
1351 return;
1352 default:
1353 dev_err(dev, "unknown status received: %d\n", status);
1354 }
1355exit:
1356 rv = usb_submit_urb(urb, GFP_ATOMIC);
1357 if (rv)
1358 dev_err(dev, "usb_submit_urb failed: %d\n", rv);
1359}
1360
1361static void usbtmc_free_int(struct usbtmc_device_data *data)
1362{
1363 if (!data->iin_ep_present || !data->iin_urb)
1364 return;
1365 usb_kill_urb(data->iin_urb);
1366 kfree(data->iin_buffer);
1367 usb_free_urb(data->iin_urb);
1368 kref_put(&data->kref, usbtmc_delete);
1369}
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001370
1371static int usbtmc_probe(struct usb_interface *intf,
1372 const struct usb_device_id *id)
1373{
1374 struct usbtmc_device_data *data;
1375 struct usb_host_interface *iface_desc;
1376 struct usb_endpoint_descriptor *endpoint;
1377 int n;
1378 int retcode;
1379
1380 dev_dbg(&intf->dev, "%s called\n", __func__);
1381
Andy Shevchenkoe6c7efd2013-07-15 11:59:41 +03001382 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Peter Chenb8f28542014-10-14 15:55:56 +08001383 if (!data)
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001384 return -ENOMEM;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001385
1386 data->intf = intf;
1387 data->id = id;
1388 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1389 usb_set_intfdata(intf, data);
1390 kref_init(&data->kref);
1391 mutex_init(&data->io_mutex);
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001392 init_waitqueue_head(&data->waitq);
1393 atomic_set(&data->iin_data_valid, 0);
Dave Penkler82ed3382016-01-27 19:15:15 +01001394 atomic_set(&data->srq_asserted, 0);
Oliver Neukum86286882009-07-02 11:36:30 +02001395 data->zombie = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001396
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001397 /* Determine if it is a Rigol or not */
1398 data->rigol_quirk = 0;
1399 dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
Johan Hovoldc7861382013-08-11 16:49:21 +02001400 le16_to_cpu(data->usb_dev->descriptor.idVendor),
1401 le16_to_cpu(data->usb_dev->descriptor.idProduct));
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001402 for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
Johan Hovoldc7861382013-08-11 16:49:21 +02001403 if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) &&
1404 (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) {
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001405 dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1406 data->rigol_quirk = 1;
1407 break;
1408 }
1409 }
1410
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001411 /* Initialize USBTMC bTag and other fields */
1412 data->bTag = 1;
1413 data->TermCharEnabled = 0;
1414 data->TermChar = '\n';
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001415 /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */
1416 data->iin_bTag = 2;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001417
1418 /* USBTMC devices have only one setting, so use that */
1419 iface_desc = data->intf->cur_altsetting;
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001420 data->ifnum = iface_desc->desc.bInterfaceNumber;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001421
1422 /* Find bulk in endpoint */
1423 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1424 endpoint = &iface_desc->endpoint[n].desc;
1425
1426 if (usb_endpoint_is_bulk_in(endpoint)) {
1427 data->bulk_in = endpoint->bEndpointAddress;
1428 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1429 data->bulk_in);
1430 break;
1431 }
1432 }
1433
1434 /* Find bulk out endpoint */
1435 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1436 endpoint = &iface_desc->endpoint[n].desc;
1437
1438 if (usb_endpoint_is_bulk_out(endpoint)) {
1439 data->bulk_out = endpoint->bEndpointAddress;
1440 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1441 data->bulk_out);
1442 break;
1443 }
1444 }
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001445 /* Find int endpoint */
1446 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1447 endpoint = &iface_desc->endpoint[n].desc;
1448
1449 if (usb_endpoint_is_int_in(endpoint)) {
1450 data->iin_ep_present = 1;
1451 data->iin_ep = endpoint->bEndpointAddress;
1452 data->iin_wMaxPacketSize = usb_endpoint_maxp(endpoint);
1453 data->iin_interval = endpoint->bInterval;
1454 dev_dbg(&intf->dev, "Found Int in endpoint at %u\n",
1455 data->iin_ep);
1456 break;
1457 }
1458 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001459
1460 retcode = get_capabilities(data);
1461 if (retcode)
1462 dev_err(&intf->dev, "can't read capabilities\n");
1463 else
1464 retcode = sysfs_create_group(&intf->dev.kobj,
1465 &capability_attr_grp);
1466
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001467 if (data->iin_ep_present) {
1468 /* allocate int urb */
1469 data->iin_urb = usb_alloc_urb(0, GFP_KERNEL);
1470 if (!data->iin_urb) {
1471 dev_err(&intf->dev, "Failed to allocate int urb\n");
1472 goto error_register;
1473 }
1474
1475 /* will reference data in int urb */
1476 kref_get(&data->kref);
1477
1478 /* allocate buffer for interrupt in */
1479 data->iin_buffer = kmalloc(data->iin_wMaxPacketSize,
1480 GFP_KERNEL);
1481 if (!data->iin_buffer) {
1482 dev_err(&intf->dev, "Failed to allocate int buf\n");
1483 goto error_register;
1484 }
1485
1486 /* fill interrupt urb */
1487 usb_fill_int_urb(data->iin_urb, data->usb_dev,
1488 usb_rcvintpipe(data->usb_dev, data->iin_ep),
1489 data->iin_buffer, data->iin_wMaxPacketSize,
1490 usbtmc_interrupt,
1491 data, data->iin_interval);
1492
1493 retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL);
1494 if (retcode) {
1495 dev_err(&intf->dev, "Failed to submit iin_urb\n");
1496 goto error_register;
1497 }
1498 }
1499
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001500 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1501
1502 retcode = usb_register_dev(intf, &usbtmc_class);
1503 if (retcode) {
1504 dev_err(&intf->dev, "Not able to get a minor"
1505 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1506 retcode);
1507 goto error_register;
1508 }
1509 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1510
1511 return 0;
1512
1513error_register:
1514 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1515 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001516 usbtmc_free_int(data);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001517 kref_put(&data->kref, usbtmc_delete);
1518 return retcode;
1519}
1520
1521static void usbtmc_disconnect(struct usb_interface *intf)
1522{
1523 struct usbtmc_device_data *data;
1524
1525 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1526
1527 data = usb_get_intfdata(intf);
1528 usb_deregister_dev(intf, &usbtmc_class);
1529 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1530 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
Oliver Neukum86286882009-07-02 11:36:30 +02001531 mutex_lock(&data->io_mutex);
1532 data->zombie = 1;
Dave Penklerf9cfabc2016-02-18 10:03:00 +01001533 wake_up_all(&data->waitq);
Oliver Neukum86286882009-07-02 11:36:30 +02001534 mutex_unlock(&data->io_mutex);
Dave Penklerf9cfabc2016-02-18 10:03:00 +01001535 usbtmc_free_int(data);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001536 kref_put(&data->kref, usbtmc_delete);
1537}
1538
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001539static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
Oliver Neukuma4708102009-07-02 11:44:33 +02001540{
1541 /* this driver does not have pending URBs */
1542 return 0;
1543}
1544
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001545static int usbtmc_resume(struct usb_interface *intf)
Oliver Neukuma4708102009-07-02 11:44:33 +02001546{
1547 return 0;
1548}
1549
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001550static struct usb_driver usbtmc_driver = {
1551 .name = "usbtmc",
1552 .id_table = usbtmc_devices,
1553 .probe = usbtmc_probe,
Oliver Neukuma4708102009-07-02 11:44:33 +02001554 .disconnect = usbtmc_disconnect,
1555 .suspend = usbtmc_suspend,
1556 .resume = usbtmc_resume,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001557};
1558
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001559module_usb_driver(usbtmc_driver);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001560
1561MODULE_LICENSE("GPL");