xref: /linux/drivers/gpib/lpvo_usb_gpib/lpvo_usb_gpib.c (revision cb4eb6771c0f8fd1c52a8f6fdec7762fb087380a)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /***************************************************************************
4  *  This code has been developed at the Department of Physics (University  *
5  *  of Florence, Italy) to support in linux-gpib the open usb-gpib adapter *
6  *  implemented at the University of Ljubljana (lpvo.fe.uni-lj.si/gpib)	   *
7  *									   *
8  *  copyright		 : (C) 2011 Marcello Carla'			   *
9  ***************************************************************************/
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #define dev_fmt pr_fmt
13 #define NAME KBUILD_MODNAME
14 
15 /* base module includes */
16 
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/tty.h>
22 #include <linux/types.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
25 #include <linux/vmalloc.h>
26 #include <linux/spinlock.h>
27 #include <linux/file.h>
28 #include <linux/timer.h>
29 #include <linux/delay.h>
30 #include <linux/sched/signal.h>
31 #include <linux/usb.h>
32 
33 #include "gpibP.h"
34 
35 MODULE_LICENSE("GPL");
36 MODULE_DESCRIPTION("GPIB driver for LPVO usb devices");
37 
38 /*
39  * Table of devices that work with this driver.
40  *
41  * Currently, only one device is known to be used in the lpvo_usb_gpib
42  * adapter (FTDI 0403:6001) but as this device id is already handled by the
43  * ftdi_sio USB serial driver the LPVO driver must not bind to it by default.
44  *
45  * If your adapter uses a different chip, insert a line
46  * in the following table with proper <Vendor-id>, <Product-id>.
47  *
48  * To have your chip automatically handled by the driver,
49  * update files "/usr/local/etc/modprobe.d/lpvo_usb_gpib.conf"
50  * and /usr/local/etc/udev/rules.d/99-lpvo_usb_gpib.rules.
51  *
52  */
53 
54 static const struct usb_device_id lpvo_table[] = {
55 	{ }					   /* Terminating entry */
56 };
57 MODULE_DEVICE_TABLE(usb, lpvo_table);
58 
59 /*
60  *   ***  Diagnostics and Debug  ***
61  * To enable the diagnostic and debug messages either compile with DEBUG set
62  * or control via the dynamic debug mechanisms.
63  * The module parameter "debug" controls the sending of debug messages to
64  * syslog. By default it is set to 0
65  * debug = 0: only attach/detach messages are sent
66  *         1: every action is logged
67  *         2: extended logging; each single exchanged byte is documented
68  *	(about twice the log volume of [1])
69  * To switch debug level:
70  *         At module loading:  modprobe lpvo_usb_gpib debug={0,1,2}
71  *         On the fly: echo {0,1,2} > /sys/modules/lpvo_usb_gpib/parameters/debug
72  */
73 
74 static int debug;
75 module_param(debug, int, 0644);
76 
77 #define DIA_LOG(level, format, ...)			   \
78 	do { if (debug >= (level))					\
79 			dev_dbg(board->gpib_dev, format, ## __VA_ARGS__); } \
80 	while (0)
81 
82 #define WQT wait_queue_entry_t
83 #define WQH head
84 #define WQE entry
85 
86 /* standard and extended command sets of the usb-gpib adapter */
87 
88 #define USB_GPIB_ON	 "\nIB\n"
89 #define USB_GPIB_OFF	 "\nIBO\n"
90 #define USB_GPIB_IBm0	 "\nIBm0\n"   /* do not assert REN with IFC */
91 #define USB_GPIB_IBm1	 "\nIBm1\n"   /* assert REN with IFC */
92 #define USB_GPIB_IBCL	 "\nIBZ\n"
93 #define USB_GPIB_STATUS	 "\nIBS\n"
94 #define USB_GPIB_READ	 "\nIB?\n"
95 #define USB_GPIB_READ_1	 "\nIBB\n"
96 #define USB_GPIB_EOI	 "\nIBe0\n"
97 #define USB_GPIB_FTMO	 "\nIBf0\n"    /* disable first byte timeout */
98 #define USB_GPIB_TTMOZ	 "\nIBt0\n"    /* disable byte timeout */
99 
100 /* incomplete commands */
101 
102 #define USB_GPIB_BTMO	 "\nIBt"      /* set byte timeout */
103 #define USB_GPIB_TTMO	 "\nIBT"      /* set total timeout */
104 
105 #define USB_GPIB_DEBUG_ON    "\nIBDE\xAA\n"
106 #define USB_GPIB_SET_LISTEN  "\nIBDT0\n"
107 #define USB_GPIB_SET_TALK    "\nIBDT1\n"
108 #define USB_GPIB_SET_LINES   "\nIBDC.\n"
109 #define USB_GPIB_SET_DATA    "\nIBDM.\n"
110 #define USB_GPIB_READ_LINES  "\nIBD?C\n"
111 #define USB_GPIB_READ_DATA   "\nIBD?M\n"
112 #define USB_GPIB_READ_BUS    "\nIBD??\n"
113 
114 /* command sequences */
115 
116 #define USB_GPIB_UNTALK "\nIBC_\n"
117 #define USB_GPIB_UNLISTEN "\nIBC?\n"
118 
119 /* special characters used by the adapter */
120 
121 #define DLE ('\020')
122 #define STX ('\02')
123 #define ETX ('\03')
124 #define ACK ('\06')
125 #define NODATA ('\03')
126 #define NODAV ('\011')
127 
128 #define IB_BUS_REN  0x01
129 #define IB_BUS_IFC  0x02
130 #define IB_BUS_NDAC 0x04
131 #define IB_BUS_NRFD 0x08
132 #define IB_BUS_DAV  0x10
133 #define IB_BUS_EOI  0x20
134 #define IB_BUS_ATN  0x40
135 #define IB_BUS_SRQ  0x80
136 
137 #define INBUF_SIZE 128
138 
139 struct char_buf {		/* used by one_char() routine */
140 	char *inbuf;
141 	int last;
142 	int nchar;
143 };
144 
145 struct usb_gpib_priv {		/* private data to the device */
146 	u8 eos;			/* eos character */
147 	short eos_flags;	/* eos mode */
148 	int timeout;		/* current value for timeout */
149 	void *dev;		/* the usb device private data structure */
150 };
151 
152 #define GPIB_DEV (((struct usb_gpib_priv *)board->private_data)->dev)
153 
show_status(struct gpib_board * board)154 static void show_status(struct gpib_board *board)
155 {
156 	DIA_LOG(2, "# - buffer_length %d\n", board->buffer_length);
157 	DIA_LOG(2, "# - status %lx\n", board->status);
158 	DIA_LOG(2, "# - use_count %d\n", board->use_count);
159 	DIA_LOG(2, "# - pad %x\n", board->pad);
160 	DIA_LOG(2, "# - sad %x\n", board->sad);
161 	DIA_LOG(2, "# - timeout %d\n", board->usec_timeout);
162 	DIA_LOG(2, "# - ppc %d\n", board->parallel_poll_configuration);
163 	DIA_LOG(2, "# - t1delay %d\n", board->t1_nano_sec);
164 	DIA_LOG(2, "# - online %d\n", board->online);
165 	DIA_LOG(2, "# - autopoll %d\n", board->autospollers);
166 	DIA_LOG(2, "# - autopoll task %p\n", board->autospoll_task);
167 	DIA_LOG(2, "# - minor %d\n", board->minor);
168 	DIA_LOG(2, "# - master %d\n", board->master);
169 	DIA_LOG(2, "# - list %d\n", board->ist);
170 }
171 
172 /*
173  * GLOBAL VARIABLES: required for
174  * pairing among gpib minor and usb minor.
175  * MAX_DEV is the max number of usb-gpib adapters; free
176  * to change as you like, but no more than 32
177  */
178 
179 #define MAX_DEV 8
180 static struct usb_interface *lpvo_usb_interfaces[MAX_DEV];   /* registered interfaces */
181 static int usb_minors[MAX_DEV];			   /* usb minors */
182 static int assigned_usb_minors;		   /* mask of filled slots */
183 static struct mutex minors_lock;     /* operations on usb_minors are to be protected */
184 
185 struct lpvo;
186 static ssize_t lpvo_do_write(struct lpvo *, const char *, size_t);
187 static ssize_t lpvo_do_read(struct lpvo *, char *, size_t);
188 static int lpvo_do_open(struct gpib_board *, int);
189 static int lpvo_do_release(struct gpib_board *);
190 
191 /*
192  *  usec_diff : take difference in MICROsec between two 'timespec'
193  *		 (unix time in sec and NANOsec)
194  */
195 
usec_diff(struct timespec64 * a,struct timespec64 * b)196 static inline int usec_diff(struct timespec64 *a, struct timespec64 *b)
197 {
198 	return ((a->tv_sec - b->tv_sec) * 1000000 +
199 		(a->tv_nsec - b->tv_nsec) / 1000);
200 }
201 
202 /*
203  *  ***  these routines are specific to the usb-gpib adapter  ***
204  */
205 
206 /**
207  * write_loop() - Send a byte sequence to the adapter
208  *
209  * @dev:      the private device structure
210  * @msg:      the byte sequence.
211  * @leng:     the byte sequence length.
212  *
213  */
214 
write_loop(void * dev,char * msg,int leng)215 static int write_loop(void *dev, char *msg, int leng)
216 {
217 	return lpvo_do_write(dev, msg, leng);
218 }
219 
220 /**
221  * send_command() - Send a byte sequence and return a single byte reply.
222  *
223  * @board:    the gpib_board_struct data area for this gpib interface
224  * @msg:      the byte sequence.
225  * @leng:     the byte sequence length; can be given as zero and is
226  *	      computed automatically, but if 'msg' contains a zero byte,
227  *	      it has to be given explicitly.
228  */
229 
send_command(struct gpib_board * board,char * msg,int leng)230 static int send_command(struct gpib_board *board, char *msg, int leng)
231 {
232 	char buffer[64];
233 	int nchar;
234 	int retval;
235 	struct timespec64 before, after;
236 
237 	ktime_get_real_ts64 (&before);
238 
239 	if (!leng)
240 		leng = strlen(msg);
241 	retval = write_loop(GPIB_DEV, msg, leng);
242 	if (retval < 0)
243 		return retval;
244 
245 	nchar = lpvo_do_read(GPIB_DEV, buffer, 64);
246 
247 	if (nchar < 0) {
248 		dev_err(board->gpib_dev, " return from read: %d\n", nchar);
249 		return nchar;
250 	} else if (nchar != 1) {
251 		dev_err(board->gpib_dev, " Irregular reply to command: %s\n", msg);
252 		return -EIO;
253 	}
254 	ktime_get_real_ts64 (&after);
255 
256 	DIA_LOG(1, "Sent %d - done %d us.\n", leng, usec_diff(&after, &before));
257 
258 	return buffer[0] & 0xff;
259 }
260 
261 /*
262  * set_control_line() - Set the value of a single gpib control line
263  *
264  * @board:    the gpib_board_struct data area for this gpib interface
265  * @line:     line mask
266  * @value:    line new value (0/1)
267  */
268 
set_control_line(struct gpib_board * board,int line,int value)269 static int set_control_line(struct gpib_board *board, int line, int value)
270 {
271 	char msg[] = USB_GPIB_SET_LINES;
272 	int retval;
273 	int leng = strlen(msg);
274 
275 	DIA_LOG(1, "setting line %x to %x\n", line, value);
276 
277 	retval = send_command(board, USB_GPIB_READ_LINES, 0);
278 
279 	DIA_LOG(1, "old line values: %x\n", retval);
280 
281 	if (retval == -EIO)
282 		return retval;
283 
284 	msg[leng - 2] = value ? (retval & ~line) : retval | line;
285 
286 	retval = send_command(board, msg, 0);
287 
288 	DIA_LOG(1, "operation result: %x\n", retval);
289 
290 	return retval;
291 }
292 
293 /*
294  * one_char() - read one single byte from input buffer
295  *
296  * @board:	the gpib_board_struct data area for this gpib interface
297  * @char_buf:	the routine private data structure
298  */
299 
one_char(struct gpib_board * board,struct char_buf * b)300 static int one_char(struct gpib_board *board, struct char_buf *b)
301 {
302 	struct timespec64 before, after;
303 
304 	if (b->nchar) {
305 		DIA_LOG(2, "-> %x\n", b->inbuf[b->last - b->nchar]);
306 		return b->inbuf[b->last - b->nchar--];
307 	}
308 	ktime_get_real_ts64 (&before);
309 	b->nchar = lpvo_do_read(GPIB_DEV, b->inbuf, INBUF_SIZE);
310 	b->last = b->nchar;
311 	ktime_get_real_ts64 (&after);
312 
313 	DIA_LOG(2, "read %d bytes in %d usec\n",
314 		b->nchar, usec_diff(&after, &before));
315 
316 	if (b->nchar > 0) {
317 		DIA_LOG(2, "--> %x\n", b->inbuf[b->last - b->nchar]);
318 		return b->inbuf[b->last - b->nchar--];
319 	}
320 	return -EIO;
321 }
322 
323 /**
324  * set_timeout() - set single byte / total timeouts on the adapter
325  *
326  * @board:    the gpib_board_struct data area for this gpib interface
327  *
328  *	   For sake of speed, the operation is performed only if it
329  *	   modifies the current (saved) value. Minimum allowed timeout
330  *	   is 30 ms (T30ms -> 8); timeout disable (TNONE -> 0) currently
331  *	   not supported.
332  */
333 
set_timeout(struct gpib_board * board)334 static void set_timeout(struct gpib_board *board)
335 {
336 	int n, val;
337 	char command[sizeof(USB_GPIB_TTMO) + 6];
338 	struct usb_gpib_priv *data = board->private_data;
339 
340 	if (data->timeout == board->usec_timeout)
341 		return;
342 
343 	n = (board->usec_timeout + 32767) / 32768;
344 	if (n < 2)
345 		n = 2;
346 
347 	DIA_LOG(1, "Set timeout to %d us -> %d\n", board->usec_timeout, n);
348 
349 	sprintf(command, "%s%d\n", USB_GPIB_BTMO, n > 255 ? 255 : n);
350 	val = send_command(board, command, 0);
351 
352 	if (val == ACK) {
353 		if (n > 65535)
354 			n = 65535;
355 		sprintf(command, "%s%d\n", USB_GPIB_TTMO, n);
356 		val = send_command(board, command, 0);
357 	}
358 
359 	if (val != ACK)
360 		dev_err(board->gpib_dev, "error in timeout set: <%s>\n", command);
361 	else
362 		data->timeout = board->usec_timeout;
363 }
364 
365 /*
366  * now the standard interface functions - attach and detach
367  */
368 
369 /**
370  * usb_gpib_attach() - activate the usb-gpib converter board
371  *
372  * @board:    the gpib_board_struct data area for this gpib interface
373  * @config:   firmware data, if any (from gpib_config -I <file>)
374  *
375  * The channel name is ttyUSBn, with n=0 by default. Other values for n
376  * passed with gpib_config -b <n>.
377  *
378  * In this routine I trust that when an error code is returned
379  * detach() will be called. Always.
380  */
381 
usb_gpib_attach(struct gpib_board * board,const struct gpib_board_config * config)382 static int usb_gpib_attach(struct gpib_board *board, const struct gpib_board_config *config)
383 {
384 	int retval, j;
385 	u32 base = config->ibbase;
386 	char *device_path;
387 	int match;
388 	struct usb_device *udev;
389 
390 	DIA_LOG(0, "Board %p -t %s -m %d -a %p -u %d -l %d -b %d\n",
391 		board, board->interface->name, board->minor, config->device_path,
392 		config->pci_bus, config->pci_slot, base);
393 
394 	board->private_data = NULL;  /* to be sure - we can detach before setting */
395 
396 	/* identify device to be attached */
397 
398 	mutex_lock(&minors_lock);
399 
400 	if (config->device_path) {
401 		/* if config->device_path given, try that first */
402 		for (j = 0 ; j < MAX_DEV ; j++) {
403 			if ((assigned_usb_minors & 1 << j) == 0)
404 				continue;
405 			udev = interface_to_usbdev(lpvo_usb_interfaces[j]);
406 			device_path = kobject_get_path(&udev->dev.kobj, GFP_KERNEL);
407 			match = gpib_match_device_path(&lpvo_usb_interfaces[j]->dev,
408 						       config->device_path);
409 			DIA_LOG(1, "dev. %d: minor %d  path: %s --> %d\n", j,
410 				lpvo_usb_interfaces[j]->minor, device_path, match);
411 			kfree(device_path);
412 			if (match)
413 				break;
414 		}
415 	} else if (config->pci_bus != -1 && config->pci_slot != -1) {
416 		/* second: look for bus and slot */
417 		for (j = 0 ; j < MAX_DEV ; j++) {
418 			if ((assigned_usb_minors & 1 << j) == 0)
419 				continue;
420 			udev = interface_to_usbdev(lpvo_usb_interfaces[j]);
421 			DIA_LOG(1, "dev. %d: bus %d -> %d  dev: %d -> %d\n", j,
422 				udev->bus->busnum, config->pci_bus, udev->devnum, config->pci_slot);
423 			if (config->pci_bus == udev->bus->busnum &&
424 			    config->pci_slot == udev->devnum)
425 				break;
426 		}
427 	} else {		/* last chance: usb_minor, given as ibbase */
428 		for (j = 0 ; j < MAX_DEV ; j++) {
429 			if (usb_minors[j] == base && assigned_usb_minors & 1 << j)
430 				break;
431 		}
432 	}
433 	mutex_unlock(&minors_lock);
434 
435 	if (j == MAX_DEV) {
436 		dev_err(board->gpib_dev, "Requested device is not registered.\n");
437 		return -EIO;
438 	}
439 
440 	board->private_data = kzalloc_obj(struct usb_gpib_priv);
441 	if (!board->private_data)
442 		return -ENOMEM;
443 
444 	retval = lpvo_do_open(board, usb_minors[j]);
445 
446 	DIA_LOG(1, "lpvo open: %d\n", retval);
447 
448 	if (retval) {
449 		dev_err(board->gpib_dev, "lpvo open failed.\n");
450 		kfree(board->private_data);
451 		board->private_data = NULL;
452 		return -ENODEV;
453 	}
454 
455 	show_status(board);
456 
457 	retval = send_command(board, USB_GPIB_ON, 0);
458 	DIA_LOG(1, "USB_GPIB_ON returns %x\n", retval);
459 	if (retval != ACK)
460 		return -EIO;
461 
462 	/*
463 	 * We must setup debug mode because we need the extended instruction
464 	 * set to cope with the Core (gpib_common) point of view
465 	 */
466 
467 	retval = send_command(board, USB_GPIB_DEBUG_ON, 0);
468 	DIA_LOG(1, "USB_GPIB_DEBUG_ON returns %x\n", retval);
469 	if (retval != ACK)
470 		return -EIO;
471 
472 	/*
473 	 * We must keep REN off after an IFC because so it is
474 	 * assumed by the Core
475 	 */
476 
477 	retval = send_command(board, USB_GPIB_IBm0, 0);
478 	DIA_LOG(1, "USB_GPIB_IBm0 returns %x\n", retval);
479 	if (retval != ACK)
480 		return -EIO;
481 
482 	retval = set_control_line(board, IB_BUS_REN, 0);
483 	if (retval != ACK)
484 		return -EIO;
485 
486 	retval = send_command(board, USB_GPIB_FTMO, 0);
487 	DIA_LOG(1, "USB_GPIB_FTMO returns %x\n", retval);
488 	if (retval != ACK)
489 		return -EIO;
490 
491 	show_status(board);
492 	DIA_LOG(0, "attached\n");
493 	return 0;
494 }
495 
496 /**
497  * usb_gpib_detach() - deactivate the usb-gpib converter board
498  *
499  * @board:    the gpib_board data area for this gpib interface
500  *
501  */
502 
usb_gpib_detach(struct gpib_board * board)503 static void usb_gpib_detach(struct gpib_board *board)
504 {
505 	int retval;
506 
507 	show_status(board);
508 
509 	DIA_LOG(0, "detaching\n");
510 
511 	if (board->private_data) {
512 		if (GPIB_DEV) {
513 			write_loop(GPIB_DEV, USB_GPIB_OFF, strlen(USB_GPIB_OFF));
514 			msleep(100);
515 			DIA_LOG(1, "%s", "GPIB off\n");
516 			retval = lpvo_do_release(board);
517 			DIA_LOG(1, "lpvo release -> %d\n", retval);
518 		}
519 		kfree(board->private_data);
520 		board->private_data = NULL;
521 	}
522 
523 	DIA_LOG(0, "detached\n");
524 }
525 
526 /*
527  *   Other functions follow in alphabetical order
528  */
529 /* command */
usb_gpib_command(struct gpib_board * board,u8 * buffer,size_t length,size_t * bytes_written)530 static int usb_gpib_command(struct gpib_board *board,
531 			    u8 *buffer,
532 			    size_t length,
533 			    size_t *bytes_written)
534 {
535 	int i, retval;
536 	char command[6] = "IBc.\n";
537 
538 	DIA_LOG(1, "enter %p\n", board);
539 
540 	set_timeout(board);
541 
542 	*bytes_written = 0;
543 	for (i = 0 ; i < length ; i++) {
544 		command[3] = buffer[i];
545 		retval = send_command(board, command, 5);
546 		DIA_LOG(2, "%d ==> %x %x\n", i, buffer[i], retval);
547 		if (retval != 0x06)
548 			return retval;
549 		++(*bytes_written);
550 	}
551 	return 0;
552 }
553 
554 /**
555  * usb_gpib_disable_eos() - Disable END on eos byte (END on EOI only)
556  *
557  * @board:    the gpib_board data area for this gpib interface
558  *
559  *   With the lpvo adapter eos can only be handled via software.
560  *   Cannot do nothing here, but remember for future use.
561  */
562 
usb_gpib_disable_eos(struct gpib_board * board)563 static void usb_gpib_disable_eos(struct gpib_board *board)
564 {
565 	((struct usb_gpib_priv *)board->private_data)->eos_flags &= ~REOS;
566 	DIA_LOG(1, "done: %x\n",
567 		((struct usb_gpib_priv *)board->private_data)->eos_flags);
568 }
569 
570 /**
571  * usb_gpib_enable_eos() - Enable END for reads when eos byte is received.
572  *
573  * @board:    the gpib_board data area for this gpib interface
574  * @eos_byte: the 'eos' byte
575  * @compare_8_bits: if zero ignore eigthth bit when comparing
576  *
577  */
578 
usb_gpib_enable_eos(struct gpib_board * board,u8 eos_byte,int compare_8_bits)579 static int usb_gpib_enable_eos(struct gpib_board *board,
580 			       u8 eos_byte,
581 			       int compare_8_bits)
582 {
583 	struct usb_gpib_priv *pd = (struct usb_gpib_priv *)board->private_data;
584 
585 	DIA_LOG(1, "enter with %x\n", eos_byte);
586 	pd->eos = eos_byte;
587 	pd->eos_flags = REOS;
588 	if (compare_8_bits)
589 		pd->eos_flags |= BIN;
590 	return 0;
591 }
592 
593 /**
594  * usb_gpib_go_to_standby() - De-assert ATN
595  *
596  * @board:    the gpib_board data area for this gpib interface
597  */
598 
usb_gpib_go_to_standby(struct gpib_board * board)599 static int usb_gpib_go_to_standby(struct gpib_board *board)
600 {
601 	int retval = set_control_line(board, IB_BUS_ATN, 0);
602 
603 	DIA_LOG(1, "done with %x\n", retval);
604 
605 	if (retval == ACK)
606 		return 0;
607 	return -EIO;
608 }
609 
610 /**
611  * usb_gpib_interface_clear() - Assert or de-assert IFC
612  *
613  * @board:    the gpib_board data area for this gpib interface
614  * @assert:   1: assert IFC;  0: de-assert IFC
615  *
616  *    Currently on the assert request we issue the lpvo IBZ
617  *    command that cycles IFC low for 100 usec, then we ignore
618  *    the de-assert request.
619  */
620 
usb_gpib_interface_clear(struct gpib_board * board,int assert)621 static void usb_gpib_interface_clear(struct gpib_board *board, int assert)
622 {
623 	int retval = 0;
624 
625 	DIA_LOG(1, "enter with %d\n", assert);
626 
627 	if (assert) {
628 		retval = send_command(board, USB_GPIB_IBCL, 0);
629 
630 		set_bit(CIC_NUM, &board->status);
631 	}
632 
633 	DIA_LOG(1, "done with %d %d\n", assert, retval);
634 }
635 
636 /**
637  * usb_gpib_line_status() - Read the status of the bus lines.
638  *
639  *  @board:    the gpib_board data area for this gpib interface
640  *
641  *    We can read all lines.
642  */
usb_gpib_line_status(const struct gpib_board * board)643 static int usb_gpib_line_status(const struct gpib_board *board)
644 {
645 	int buffer;
646 	int line_status = VALID_ALL;   /* all lines will be read */
647 	struct list_head *p, *q;
648 	WQT *item;
649 	unsigned long flags;
650 	int sleep = 0;
651 
652 	DIA_LOG(1, "%s\n", "request");
653 
654 	/*
655 	 * if we are on the wait queue (board->wait), do not hurry
656 	 * reading status line; instead, pause a little
657 	 */
658 
659 	spin_lock_irqsave((spinlock_t *)&board->wait.lock, flags);
660 	q = (struct list_head *)&board->wait.WQH;
661 	list_for_each(p, q) {
662 		item = container_of(p, WQT, WQE);
663 		if (item->private == current) {
664 			sleep = 20;
665 			break;
666 		}
667 		/* pid is: ((struct task_struct *) item->private)->pid); */
668 	}
669 	spin_unlock_irqrestore((spinlock_t *)&board->wait.lock, flags);
670 	if (sleep) {
671 		DIA_LOG(1, "we are on the wait queue - sleep %d ms\n", sleep);
672 		msleep(sleep);
673 	}
674 
675 	buffer = send_command((struct gpib_board *)board, USB_GPIB_STATUS, 0);
676 
677 	if (buffer < 0) {
678 		dev_err(board->gpib_dev, "line status read failed with %d\n", buffer);
679 		return -1;
680 	}
681 
682 	if ((buffer & 0x01) == 0)
683 		line_status |= BUS_REN;
684 	if ((buffer & 0x02) == 0)
685 		line_status |= BUS_IFC;
686 	if ((buffer & 0x04) == 0)
687 		line_status |= BUS_NDAC;
688 	if ((buffer & 0x08) == 0)
689 		line_status |= BUS_NRFD;
690 	if ((buffer & 0x10) == 0)
691 		line_status |= BUS_DAV;
692 	if ((buffer & 0x20) == 0)
693 		line_status |= BUS_EOI;
694 	if ((buffer & 0x40) == 0)
695 		line_status |= BUS_ATN;
696 	if ((buffer & 0x80) == 0)
697 		line_status |= BUS_SRQ;
698 
699 	DIA_LOG(1, "done with %x %x\n", buffer, line_status);
700 
701 	return line_status;
702 }
703 
704 /* parallel_poll */
705 
usb_gpib_parallel_poll(struct gpib_board * board,u8 * result)706 static int usb_gpib_parallel_poll(struct gpib_board *board, u8 *result)
707 {
708 	/*
709 	 * request parallel poll asserting ATN | EOI;
710 	 * we suppose ATN already asserted
711 	 */
712 
713 	int retval;
714 
715 	DIA_LOG(1, "enter %p\n", board);
716 
717 	retval = set_control_line(board, IB_BUS_EOI, 1);
718 	if (retval != ACK)
719 		return -EIO;
720 
721 	*result = send_command(board, USB_GPIB_READ_DATA, 0);
722 
723 	DIA_LOG(1, "done with %x\n", *result);
724 
725 	retval = set_control_line(board, IB_BUS_EOI, 0);
726 	if (retval != 0x06)
727 		return -EIO;
728 
729 	return 0;
730 }
731 
732 /* read */
733 
usb_gpib_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)734 static int usb_gpib_read(struct gpib_board *board,
735 			 u8 *buffer,
736 			 size_t length,
737 			 int *end,
738 			 size_t *bytes_read)
739 {
740 #define MAX_READ_EXCESS 16384
741 
742 	struct char_buf b = {NULL, 0};
743 
744 	int retval;
745 	char c, nc;
746 	int ic;
747 	struct timespec64 before, after;
748 	int read_count = MAX_READ_EXCESS;
749 	struct usb_gpib_priv *pd = (struct usb_gpib_priv *)board->private_data;
750 
751 	DIA_LOG(1, "enter %p -> %zu\n", board, length);
752 
753 	*bytes_read = 0;      /* by default, things go wrong */
754 	*end = 0;
755 
756 	set_timeout(board);
757 
758 	/* single byte read has a special handling */
759 
760 	if (length == 1) {
761 		char inbuf[2] = {0, 0};
762 
763 		/* read a single character */
764 
765 		ktime_get_real_ts64 (&before);
766 
767 		retval = write_loop(GPIB_DEV, USB_GPIB_READ_1, strlen(USB_GPIB_READ_1));
768 		if (retval < 0)
769 			return retval;
770 
771 		retval = lpvo_do_read(GPIB_DEV, inbuf, 1);
772 		retval += lpvo_do_read(GPIB_DEV, inbuf + 1, 1);
773 
774 		ktime_get_real_ts64 (&after);
775 
776 		DIA_LOG(1, "single read: %x %x %x in %d\n", retval,
777 			inbuf[0], inbuf[1],
778 			usec_diff(&after, &before));
779 
780 		/* good char / last char? */
781 
782 		if (retval == 2 && inbuf[1] == ACK) {
783 			buffer[0] = inbuf[0];
784 			*bytes_read = 1;
785 			return 0;
786 		}
787 		if (retval < 2)
788 			return -EIO;
789 		else
790 			return -ETIME;
791 	}
792 
793 	/* allocate buffer for multibyte read */
794 
795 	b.inbuf = kmalloc(INBUF_SIZE, GFP_KERNEL);
796 	if (!b.inbuf)
797 		return -ENOMEM;
798 
799 	/* send read command and check <DLE><STX> sequence */
800 
801 	retval = write_loop(GPIB_DEV, USB_GPIB_READ, strlen(USB_GPIB_READ));
802 	if (retval < 0)
803 		goto read_return;
804 
805 	if (one_char(board, &b) != DLE || one_char(board, &b) != STX) {
806 		dev_err(board->gpib_dev, "wrong <DLE><STX> sequence\n");
807 		retval = -EIO;
808 		goto read_return;
809 	}
810 
811 	/* get data flow */
812 
813 	while (1) {
814 		ic = one_char(board, &b);
815 		if (ic == -EIO) {
816 			retval = -EIO;
817 			goto read_return;
818 		}
819 		c = ic;
820 
821 		if (c == DLE)
822 			nc = one_char(board, &b);
823 		if (c != DLE || nc == DLE) {
824 			/* data byte - store into buffer */
825 
826 			if (*bytes_read == length)
827 				break; /* data overflow */
828 			if (c == DLE)
829 				c = nc;
830 			buffer[(*bytes_read)++] = c;
831 			if (c == pd->eos) {
832 				*end = 1;
833 				break;
834 			}
835 
836 		} else {
837 			/* we are in the closing <DLE><ETX> sequence */
838 			c = nc;
839 			if (c == ETX) {
840 				c = one_char(board, &b);
841 				if (c == ACK) {
842 					*end = 1;
843 					retval = 0;
844 					goto read_return;
845 				} else {
846 					dev_err(board->gpib_dev, "wrong end of message %x", c);
847 					retval = -ETIME;
848 					goto read_return;
849 				}
850 			} else {
851 				dev_err(board->gpib_dev, "lone <DLE> in stream");
852 				retval = -EIO;
853 				goto read_return;
854 			}
855 		}
856 	}
857 
858 	/* we had a data overflow - flush excess data */
859 
860 	while (read_count--) {
861 		if (one_char(board, &b) != DLE)
862 			continue;
863 		c = one_char(board, &b);
864 		if (c == DLE)
865 			continue;
866 		if (c == ETX) {
867 			c = one_char(board, &b);
868 			if (c == ACK) {
869 				if (MAX_READ_EXCESS - read_count > 1)
870 					dev_dbg(board->gpib_dev, "small buffer - maybe some data lost");
871 				retval = 0;
872 				goto read_return;
873 			}
874 			break;
875 		}
876 	}
877 
878 	dev_err(board->gpib_dev, "no input end - board in odd state\n");
879 	retval = -EIO;
880 
881 read_return:
882 	kfree(b.inbuf);
883 
884 	DIA_LOG(1, "done with byte/status: %d %x %d\n",	(int)*bytes_read, retval, *end);
885 
886 	if (retval == 0 || retval == -ETIME) {
887 		if (send_command(board, USB_GPIB_UNTALK, sizeof(USB_GPIB_UNTALK)) == 0x06)
888 			return retval;
889 		return	-EIO;
890 	}
891 
892 	return retval;
893 }
894 
895 /* remote_enable */
896 
usb_gpib_remote_enable(struct gpib_board * board,int enable)897 static void usb_gpib_remote_enable(struct gpib_board *board, int enable)
898 {
899 	int retval;
900 
901 	retval = set_control_line(board, IB_BUS_REN, enable ? 1 : 0);
902 	if (retval != ACK)
903 		dev_err(board->gpib_dev, "could not set REN line: %x\n", retval);
904 
905 	DIA_LOG(1, "done with %x\n", retval);
906 }
907 
908 /* request_system_control */
909 
usb_gpib_request_system_control(struct gpib_board * board,int request_control)910 static int usb_gpib_request_system_control(struct gpib_board *board, int request_control)
911 {
912 	if (!request_control)
913 		return -EINVAL;
914 
915 	DIA_LOG(1, "done with %d -> %lx\n", request_control, board->status);
916 	return 0;
917 }
918 
919 /* take_control */
920 /* beware: the sync flag is ignored; what is its real meaning? */
921 
usb_gpib_take_control(struct gpib_board * board,int sync)922 static int usb_gpib_take_control(struct gpib_board *board, int sync)
923 {
924 	int retval;
925 
926 	retval = set_control_line(board, IB_BUS_ATN, 1);
927 
928 	DIA_LOG(1, "done with %d %x\n", sync, retval);
929 
930 	if (retval == ACK)
931 		return 0;
932 	return -EIO;
933 }
934 
935 /* update_status */
936 
usb_gpib_update_status(struct gpib_board * board,unsigned int clear_mask)937 static unsigned int usb_gpib_update_status(struct gpib_board *board,
938 					   unsigned int clear_mask)
939 {
940 	/* There is nothing we can do here, I guess */
941 
942 	board->status &= ~clear_mask;
943 
944 	DIA_LOG(1, "done with %x %lx\n", clear_mask, board->status);
945 
946 	return board->status;
947 }
948 
949 /* write */
950 /* beware: DLE characters are not escaped - can only send ASCII data */
951 
usb_gpib_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)952 static int usb_gpib_write(struct gpib_board *board,
953 			  u8 *buffer,
954 			  size_t length,
955 			  int send_eoi,
956 			  size_t *bytes_written)
957 {
958 	int retval;
959 	char *msg;
960 
961 	DIA_LOG(1, "enter %p -> %zu\n", board, length);
962 
963 	set_timeout(board);
964 
965 	msg = kmalloc(length + 8, GFP_KERNEL);
966 	if (!msg)
967 		return -ENOMEM;
968 
969 	memcpy(msg, "\nIB\020\002", 5);
970 	memcpy(msg + 5, buffer, length);
971 	memcpy(msg + 5 + length, "\020\003\n", 3);
972 
973 	retval = send_command(board, msg, length + 8);
974 	kfree(msg);
975 
976 	DIA_LOG(1, "<%.*s> -> %x\n", (int)length, buffer, retval);
977 
978 	if (retval != ACK)
979 		return -EPIPE;
980 
981 	*bytes_written = length;
982 
983 	if (send_command(board, USB_GPIB_UNLISTEN, sizeof(USB_GPIB_UNLISTEN)) != 0x06)
984 		return -EPIPE;
985 
986 	return length;
987 }
988 
989 /*
990  *  ***	 following functions not implemented yet  ***
991  */
992 
993 /* parallel_poll configure */
994 
usb_gpib_parallel_poll_configure(struct gpib_board * board,u8 configuration)995 static void usb_gpib_parallel_poll_configure(struct gpib_board *board,
996 					     u8 configuration)
997 {
998 }
999 
1000 /* parallel_poll_response */
1001 
usb_gpib_parallel_poll_response(struct gpib_board * board,int ist)1002 static void usb_gpib_parallel_poll_response(struct gpib_board *board, int ist)
1003 {
1004 }
1005 
1006 /* primary_address */
1007 
usb_gpib_primary_address(struct gpib_board * board,unsigned int address)1008 static int  usb_gpib_primary_address(struct gpib_board *board, unsigned int address)
1009 {
1010 	return 0;
1011 }
1012 
1013 /* return_to_local */
1014 
usb_gpib_return_to_local(struct gpib_board * board)1015 static	void usb_gpib_return_to_local(struct gpib_board *board)
1016 {
1017 }
1018 
1019 /* secondary_address */
1020 
usb_gpib_secondary_address(struct gpib_board * board,unsigned int address,int enable)1021 static int usb_gpib_secondary_address(struct gpib_board *board,
1022 				      unsigned int address,
1023 				      int enable)
1024 {
1025 	return 0;
1026 }
1027 
1028 /* serial_poll_response */
1029 
usb_gpib_serial_poll_response(struct gpib_board * board,u8 status)1030 static void usb_gpib_serial_poll_response(struct gpib_board *board, u8 status)
1031 {
1032 }
1033 
1034 /* serial_poll_status */
1035 
usb_gpib_serial_poll_status(struct gpib_board * board)1036 static u8 usb_gpib_serial_poll_status(struct gpib_board *board)
1037 {
1038 	return 0;
1039 }
1040 
1041 /* t1_delay */
1042 
usb_gpib_t1_delay(struct gpib_board * board,unsigned int nano_sec)1043 static int usb_gpib_t1_delay(struct gpib_board *board, unsigned int nano_sec)
1044 {
1045 	return 0;
1046 }
1047 
1048 /*
1049  *   ***  module dispatch table and init/exit functions	 ***
1050  */
1051 
1052 static struct gpib_interface usb_gpib_interface = {
1053 	.name = NAME,
1054 	.attach = usb_gpib_attach,
1055 	.detach = usb_gpib_detach,
1056 	.read = usb_gpib_read,
1057 	.write = usb_gpib_write,
1058 	.command = usb_gpib_command,
1059 	.take_control = usb_gpib_take_control,
1060 	.go_to_standby = usb_gpib_go_to_standby,
1061 	.request_system_control = usb_gpib_request_system_control,
1062 	.interface_clear = usb_gpib_interface_clear,
1063 	.remote_enable = usb_gpib_remote_enable,
1064 	.enable_eos = usb_gpib_enable_eos,
1065 	.disable_eos = usb_gpib_disable_eos,
1066 	.parallel_poll = usb_gpib_parallel_poll,
1067 	.parallel_poll_configure = usb_gpib_parallel_poll_configure,
1068 	.parallel_poll_response = usb_gpib_parallel_poll_response,
1069 	.local_parallel_poll_mode = NULL, // XXX
1070 	.line_status = usb_gpib_line_status,
1071 	.update_status = usb_gpib_update_status,
1072 	.primary_address = usb_gpib_primary_address,
1073 	.secondary_address = usb_gpib_secondary_address,
1074 	.serial_poll_response = usb_gpib_serial_poll_response,
1075 	.serial_poll_status = usb_gpib_serial_poll_status,
1076 	.t1_delay = usb_gpib_t1_delay,
1077 	.return_to_local = usb_gpib_return_to_local,
1078 	.skip_check_for_command_acceptors = 1
1079 };
1080 
1081 /*
1082  * usb_gpib_init_module(), usb_gpib_exit_module()
1083  *
1084  * This functions are called every time a new device is detected
1085  * and registered or is removed and unregistered.
1086  * We must take note of created and destroyed usb minors to be used
1087  * when usb_gpib_attach() and usb_gpib_detach() will be called on
1088  * request by gpib_config.
1089  */
1090 
usb_gpib_init_module(struct usb_interface * interface)1091 static int usb_gpib_init_module(struct usb_interface *interface)
1092 {
1093 	int j, mask, rv;
1094 
1095 	rv = mutex_lock_interruptible(&minors_lock);
1096 	if (rv < 0)
1097 		return rv;
1098 
1099 	if (!assigned_usb_minors) {
1100 		rv = gpib_register_driver(&usb_gpib_interface, THIS_MODULE);
1101 		if (rv) {
1102 			pr_err("gpib_register_driver failed: error = %d\n", rv);
1103 			goto exit;
1104 		}
1105 	} else {
1106 		/*
1107 		 * check if minor is already registered - maybe useless, but if
1108 		 * it happens the code is inconsistent somewhere
1109 		 */
1110 
1111 		for (j = 0 ; j < MAX_DEV ; j++) {
1112 			if (usb_minors[j] == interface->minor && assigned_usb_minors & 1 << j) {
1113 				pr_err("CODE BUG: USB minor %d registered at %d.\n",
1114 				       interface->minor, j);
1115 				rv = -1;
1116 				goto exit;
1117 			}
1118 		}
1119 	}
1120 
1121 	/* find a free slot */
1122 
1123 	for (j = 0 ; j < MAX_DEV ; j++) {
1124 		mask = 1 << j;
1125 		if ((assigned_usb_minors & mask) == 0) {
1126 			usb_minors[j] = interface->minor;
1127 			lpvo_usb_interfaces[j] = interface;
1128 			assigned_usb_minors |= mask;
1129 			rv = 0;
1130 			goto exit;
1131 		}
1132 	}
1133 	pr_err("No slot available for interface %p minor %d\n", interface, interface->minor);
1134 	rv = -1;
1135 
1136 exit:
1137 	mutex_unlock(&minors_lock);
1138 	return rv;
1139 }
1140 
usb_gpib_exit_module(int minor)1141 static void usb_gpib_exit_module(int minor)
1142 {
1143 	int j;
1144 
1145 	mutex_lock(&minors_lock);
1146 	for (j = 0 ; j < MAX_DEV ; j++) {
1147 		if (usb_minors[j] == minor && assigned_usb_minors & 1 << j) {
1148 			assigned_usb_minors &= ~(1 << j);
1149 			usb_minors[j] = -1;
1150 			if (assigned_usb_minors == 0)
1151 				gpib_unregister_driver(&usb_gpib_interface);
1152 			goto exit;
1153 		}
1154 	}
1155 	pr_err("CODE BUG: USB minor %d not found.\n", minor);
1156 
1157 exit:
1158 	mutex_unlock(&minors_lock);
1159 }
1160 
1161 /*
1162  * Default latency time (16 msec) is too long.
1163  * We must use 1 msec (best); anyhow, no more than 5 msec.
1164  *
1165  * Defines and function taken and modified from the kernel tree
1166  * (see ftdi_sio.h and ftdi_sio.c).
1167  */
1168 
1169 #define FTDI_SIO_SET_LATENCY_TIMER	9 /* Set the latency timer */
1170 #define FTDI_SIO_SET_LATENCY_TIMER_REQUEST FTDI_SIO_SET_LATENCY_TIMER
1171 #define FTDI_SIO_SET_LATENCY_TIMER_REQUEST_TYPE 0x40
1172 #define WDR_TIMEOUT 5000 /* default urb timeout */
1173 #define WDR_SHORT_TIMEOUT 1000	/* shorter urb timeout */
1174 
1175 #define LATENCY_TIMER 1		   /* use a small latency timer: 1 ... 5 msec */
1176 #define LATENCY_CHANNEL 0	   /* channel selection in multichannel devices */
write_latency_timer(struct usb_device * udev)1177 static int write_latency_timer(struct usb_device *udev)
1178 {
1179 	int rv = usb_control_msg(udev,
1180 				 usb_sndctrlpipe(udev, 0),
1181 				 FTDI_SIO_SET_LATENCY_TIMER_REQUEST,
1182 				 FTDI_SIO_SET_LATENCY_TIMER_REQUEST_TYPE,
1183 				 LATENCY_TIMER, LATENCY_CHANNEL,
1184 				 NULL, 0, WDR_TIMEOUT);
1185 	if (rv < 0)
1186 		dev_err(&udev->dev, "Unable to write latency timer: %i\n", rv);
1187 	return rv;
1188 }
1189 
1190 /*****************************************************************************
1191  *									     *
1192  *  The following code is a modified version of the USB Skeleton driver	     *
1193  *  written by Greg Kroah-Hartman and available in the kernel tree.	     *
1194  *									     *
1195  *  Functions skel_open() and skel_release() have been rewritten and named   *
1196  *  lpvo_do_open() and lpvo_do_release() to process the attach and detach    *
1197  *  requests coming from gpib_config.					     *
1198  *									     *
1199  *  Functions lpvo_read() and lpvo_write() have been split into a	     *
1200  *  lpvo_do_read() and lpvo_do_write(), that cover the kernel stuff of read  *
1201  *  and write operations, and the original lpvo_read() and lpvo_write(),     *
1202  *  that handle communication with user space and call their _do_ companion. *
1203  *									     *
1204  *  Only the _do_ versions are used by the lpvo_usb_gpib driver; other ones  *
1205  *  can be (optionally) maintained in the compilation to have direct access  *
1206  *  to a gpib controller for debug and diagnostics.			     *
1207  *									     *
1208  *  To avoid collisions in names, devices in user space have been renamed    *
1209  *  lpvo_raw1, lpvo_raw2 ....  and the usb driver has been renamed with the  *
1210  *  gpib module name.							     *
1211  *									     *
1212  *****************************************************************************/
1213 
1214 /*
1215  * USB Skeleton driver - 2.2
1216  *
1217  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
1218  *
1219  * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c
1220  * but has been rewritten to be easier to read and use.
1221  */
1222 
1223 #include <linux/errno.h>
1224 #include <linux/kref.h>
1225 #include <linux/uaccess.h>
1226 #include <linux/mutex.h>
1227 
1228 /* Get a minor range for your devices from the usb maintainer */
1229 #define USB_LPVO_MINOR_BASE	   192
1230 
1231 /*   private defines   */
1232 
1233 #define MAX_TRANSFER		    (PAGE_SIZE - 512)
1234 /*
1235  * MAX_TRANSFER is chosen so that the VM is not stressed by
1236  * allocations > PAGE_SIZE and the number of packets in a page
1237  * is an integer 512 is the largest possible packet on EHCI
1238  */
1239 
1240 #define WRITES_IN_FLIGHT	1     /* we do not want more than one pending write */
1241 #define USER_DEVICE 1		      /* compile for device(s) in user space */
1242 
1243 /* Structure to hold all of our device specific stuff */
1244 struct lpvo {
1245 	struct usb_device     *udev;		     /* the usb device for this device */
1246 	struct usb_interface  *interface;	     /* the interface for this device */
1247 	struct semaphore      limit_sem;	     /* limiting the number of writes in progress */
1248 	struct usb_anchor     submitted;	     /* in case need to retract our submissions */
1249 	struct urb	      *bulk_in_urb;	     /* the urb to read data with */
1250 	unsigned char	      *bulk_in_buffer;	     /* the buffer to receive data */
1251 	size_t		      bulk_in_size;	     /* the size of the receive buffer */
1252 	size_t		      bulk_in_filled;	     /* number of bytes in the buffer */
1253 	size_t		      bulk_in_copied;	     /* already copied to user space */
1254 	__u8		      bulk_in_endpoint_addr;  /* the address of the bulk in endpoint */
1255 	__u8		      bulk_out_endpoint_addr; /* the address of the bulk out endpoint */
1256 	int		      errors;		     /* the last request tanked */
1257 	bool		      ongoing_read;	     /* a read is going on */
1258 	spinlock_t	      err_lock;		     /* lock for errors */
1259 	struct kref	      kref;
1260 	struct mutex	      io_mutex;		     /* synchronize I/O with disconnect */
1261 	wait_queue_head_t     bulk_in_wait;	     /* to wait for an ongoing read */
1262 };
1263 
1264 #define to_lpvo_dev(d) container_of(d, struct lpvo, kref)
1265 
1266 static struct usb_driver lpvo_driver;
1267 static void lpvo_draw_down(struct lpvo *dev);
1268 
lpvo_delete(struct kref * kref)1269 static void lpvo_delete(struct kref *kref)
1270 {
1271 	struct lpvo *dev = to_lpvo_dev(kref);
1272 
1273 	usb_free_urb(dev->bulk_in_urb);
1274 	usb_put_dev(dev->udev);
1275 	kfree(dev->bulk_in_buffer);
1276 	kfree(dev);
1277 }
1278 
1279 /*
1280  * lpvo_do_open() - to be called by usb_gpib_attach
1281  */
1282 
lpvo_do_open(struct gpib_board * board,int subminor)1283 static int lpvo_do_open(struct gpib_board *board, int subminor)
1284 {
1285 	struct lpvo *dev;
1286 	struct usb_interface *interface;
1287 	int retval = 0;
1288 
1289 	interface = usb_find_interface(&lpvo_driver, subminor);
1290 	if (!interface) {
1291 		dev_err(board->gpib_dev, "can't find device for minor %d\n", subminor);
1292 		retval = -ENODEV;
1293 		goto exit;
1294 	}
1295 
1296 	dev = usb_get_intfdata(interface);
1297 	if (!dev) {
1298 		retval = -ENODEV;
1299 		goto exit;
1300 	}
1301 
1302 	retval = usb_autopm_get_interface(interface);
1303 	if (retval)
1304 		goto exit;
1305 
1306 	/* increment our usage count for the device */
1307 	kref_get(&dev->kref);
1308 
1309 	/* save our object in the file's private structure */
1310 	GPIB_DEV = dev;
1311 
1312 exit:
1313 	return retval;
1314 }
1315 
1316 /*
1317  * lpvo_do_release() - to be called by usb_gpib_detach
1318  */
1319 
lpvo_do_release(struct gpib_board * board)1320 static int lpvo_do_release(struct gpib_board *board)
1321 {
1322 	struct lpvo *dev;
1323 
1324 	dev = GPIB_DEV;
1325 	if (!dev)
1326 		return -ENODEV;
1327 
1328 	/* allow the device to be autosuspended */
1329 	mutex_lock(&dev->io_mutex);
1330 	if (dev->interface)
1331 		usb_autopm_put_interface(dev->interface);
1332 	mutex_unlock(&dev->io_mutex);
1333 
1334 	/* decrement the count on our device */
1335 	kref_put(&dev->kref, lpvo_delete);
1336 	return 0;
1337 }
1338 
1339 /*
1340  * read functions
1341  */
1342 
lpvo_read_bulk_callback(struct urb * urb)1343 static void lpvo_read_bulk_callback(struct urb *urb)
1344 {
1345 	struct lpvo *dev;
1346 	unsigned long flags;
1347 
1348 	dev = urb->context;
1349 
1350 	spin_lock_irqsave(&dev->err_lock, flags);
1351 	/* sync/async unlink faults aren't errors */
1352 	if (urb->status) {
1353 		if (!(urb->status == -ENOENT ||
1354 		      urb->status == -ECONNRESET ||
1355 		      urb->status == -ESHUTDOWN))
1356 			dev_err(&dev->interface->dev, "nonzero read bulk status received: %d\n",
1357 				urb->status);
1358 
1359 		dev->errors = urb->status;
1360 	} else {
1361 		dev->bulk_in_filled = urb->actual_length;
1362 	}
1363 	dev->ongoing_read = 0;
1364 	spin_unlock_irqrestore(&dev->err_lock, flags);
1365 
1366 	wake_up_interruptible(&dev->bulk_in_wait);
1367 }
1368 
lpvo_do_read_io(struct lpvo * dev,size_t count)1369 static int lpvo_do_read_io(struct lpvo *dev, size_t count)
1370 {
1371 	int rv;
1372 
1373 	/* prepare a read */
1374 	usb_fill_bulk_urb(dev->bulk_in_urb,
1375 			  dev->udev,
1376 			  usb_rcvbulkpipe(dev->udev,
1377 					  dev->bulk_in_endpoint_addr),
1378 			  dev->bulk_in_buffer,
1379 			  min(dev->bulk_in_size, count),
1380 			  lpvo_read_bulk_callback,
1381 			  dev);
1382 	/* tell everybody to leave the URB alone */
1383 	spin_lock_irq(&dev->err_lock);
1384 	dev->ongoing_read = 1;
1385 	spin_unlock_irq(&dev->err_lock);
1386 
1387 	/* submit bulk in urb, which means no data to deliver */
1388 	dev->bulk_in_filled = 0;
1389 	dev->bulk_in_copied = 0;
1390 
1391 	/* do it */
1392 	rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL);
1393 	if (rv < 0) {
1394 		dev_err(&dev->interface->dev, "failed submitting read urb, error %d\n", rv);
1395 		rv = (rv == -ENOMEM) ? rv : -EIO;
1396 		spin_lock_irq(&dev->err_lock);
1397 		dev->ongoing_read = 0;
1398 		spin_unlock_irq(&dev->err_lock);
1399 	}
1400 
1401 	return rv;
1402 }
1403 
1404 /*
1405  * lpvo_do_read() - read operations from lpvo_usb_gpib
1406  */
1407 
lpvo_do_read(struct lpvo * dev,char * buffer,size_t count)1408 static ssize_t lpvo_do_read(struct lpvo *dev, char *buffer, size_t count)
1409 {
1410 	int rv;
1411 	bool ongoing_io;
1412 
1413 	/* if we cannot read at all, return EOF */
1414 
1415 	if (!dev->bulk_in_urb || !count)
1416 		return 0;
1417 
1418 restart:  /* added to comply with ftdi timeout technique */
1419 
1420 	/* no concurrent readers */
1421 
1422 	rv = mutex_lock_interruptible(&dev->io_mutex);
1423 	if (rv < 0)
1424 		return rv;
1425 
1426 	if (!dev->interface) {		      /* disconnect() was called */
1427 		rv = -ENODEV;
1428 		goto exit;
1429 	}
1430 
1431 retry:
1432 	/* if IO is under way, we must not touch things */
1433 	spin_lock_irq(&dev->err_lock);
1434 	ongoing_io = dev->ongoing_read;
1435 	spin_unlock_irq(&dev->err_lock);
1436 
1437 	if (ongoing_io) {
1438 //		  /* nonblocking IO shall not wait */
1439 //		  /* no file, no O_NONBLOCK; maybe provide when from user space */
1440 //		  if (file->f_flags & O_NONBLOCK) {
1441 //			  rv = -EAGAIN;
1442 //			  goto exit;
1443 //		  }
1444 
1445 		/*
1446 		 * IO may take forever
1447 		 * hence wait in an interruptible state
1448 		 */
1449 		rv = wait_event_interruptible(dev->bulk_in_wait, (!dev->ongoing_read));
1450 		if (rv < 0)
1451 			goto exit;
1452 	}
1453 
1454 	/* errors must be reported */
1455 	rv = dev->errors;
1456 	if (rv < 0) {
1457 		/* any error is reported once */
1458 		dev->errors = 0;
1459 		/* to preserve notifications about reset */
1460 		rv = (rv == -EPIPE) ? rv : -EIO;
1461 		/* report it */
1462 		goto exit;
1463 	}
1464 
1465 	/*
1466 	 * if the buffer is filled we may satisfy the read
1467 	 * else we need to start IO
1468 	 */
1469 
1470 	if (dev->bulk_in_filled) {
1471 		/* we had read data */
1472 
1473 		size_t available = dev->bulk_in_filled - dev->bulk_in_copied;
1474 //		  size_t chunk = min(available, count);	 /* compute chunk later */
1475 		size_t chunk;
1476 
1477 		if (!available) {
1478 			/*
1479 			 * all data has been used
1480 			 * actual IO needs to be done
1481 			 */
1482 			/*
1483 			 * it seems that requests for less than dev->bulk_in_size
1484 			 *  are not accepted
1485 			 */
1486 			rv = lpvo_do_read_io(dev, dev->bulk_in_size);
1487 			if (rv < 0)
1488 				goto exit;
1489 			else
1490 				goto retry;
1491 		}
1492 
1493 		/*
1494 		 * data is available - chunk tells us how much shall be copied
1495 		 */
1496 
1497 		/*
1498 		 * Condition dev->bulk_in_copied > 0 maybe will never happen. In case,
1499 		 * signal the event and copy using the original procedure, i.e., copy
1500 		 * first two bytes also
1501 		 */
1502 
1503 		if (dev->bulk_in_copied) {
1504 			chunk = min(available, count);
1505 			memcpy(buffer, dev->bulk_in_buffer + dev->bulk_in_copied, chunk);
1506 			rv = chunk;
1507 			dev->bulk_in_copied += chunk;
1508 
1509 			/* copy discarding first two bytes that contain ftdi chip status */
1510 
1511 		} else {
1512 			/* account for two bytes to be discarded */
1513 			chunk = min(available, count + 2);
1514 			if (chunk < 2) {
1515 				dev_err(&dev->udev->dev, "BAD READ - chunk: %zu\n", chunk);
1516 				rv = -EIO;
1517 				goto exit;
1518 			}
1519 
1520 			memcpy(buffer, dev->bulk_in_buffer + 2, chunk - 2);
1521 			rv = chunk;
1522 			dev->bulk_in_copied += chunk;
1523 		}
1524 
1525 		/*
1526 		 * if we are asked for more than we have,
1527 		 * we start IO but don't wait
1528 		 *
1529 		 * No, no read ahead allowed; if the case, more data will be
1530 		 * asked for by the lpvo_usb_gpib layer.
1531 		 */
1532 //		  if (available < count)
1533 //			  lpvo_do_read_io(dev, dev->bulk_in_size);
1534 	} else {
1535 		/* no data in the buffer */
1536 		rv = lpvo_do_read_io(dev, dev->bulk_in_size);
1537 		if (rv < 0)
1538 			goto exit;
1539 		else
1540 			goto retry;
1541 	}
1542 exit:
1543 	mutex_unlock(&dev->io_mutex);
1544 	if (rv == 2)
1545 		goto restart;	/* ftdi chip returns two status bytes after a latency anyhow */
1546 
1547 	if (rv > 0)
1548 		return rv - 2;	/* account for 2 discarded bytes in a valid buffer */
1549 	return rv;
1550 }
1551 
1552 /*
1553  * write functions
1554  */
1555 
lpvo_write_bulk_callback(struct urb * urb)1556 static void lpvo_write_bulk_callback(struct urb *urb)
1557 {
1558 	struct lpvo *dev;
1559 	unsigned long flags;
1560 
1561 	dev = urb->context;
1562 
1563 	/* sync/async unlink faults aren't errors */
1564 	if (urb->status) {
1565 		if (!(urb->status == -ENOENT ||
1566 		      urb->status == -ECONNRESET ||
1567 		      urb->status == -ESHUTDOWN))
1568 			dev_err(&dev->interface->dev,
1569 				"nonzero write bulk status received: %d\n", urb->status);
1570 
1571 		spin_lock_irqsave(&dev->err_lock, flags);
1572 		dev->errors = urb->status;
1573 		spin_unlock_irqrestore(&dev->err_lock, flags);
1574 	}
1575 
1576 	/* free up our allocated buffer */
1577 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
1578 			  urb->transfer_buffer, urb->transfer_dma);
1579 	up(&dev->limit_sem);
1580 }
1581 
1582 /*
1583  * lpvo_do_write() - write operations from lpvo_usb_gpib
1584  */
1585 
lpvo_do_write(struct lpvo * dev,const char * buffer,size_t count)1586 static ssize_t lpvo_do_write(struct lpvo *dev, const char *buffer, size_t count)
1587 {
1588 	int retval = 0;
1589 	struct urb *urb = NULL;
1590 	char *buf = NULL;
1591 	size_t writesize = min_t(size_t, count, (size_t)MAX_TRANSFER);
1592 
1593 	/* verify that we actually have some data to write */
1594 	if (count == 0)
1595 		goto exit;
1596 
1597 	/*
1598 	 * limit the number of URBs in flight to stop a user from using up all
1599 	 * RAM
1600 	 */
1601 	/* Only one URB is used, because we can't have a pending write() and go on */
1602 
1603 //	  if (!(file->f_flags & O_NONBLOCK)) {	/* no NONBLOCK provided */
1604 	if (down_interruptible(&dev->limit_sem)) {
1605 		retval = -ERESTARTSYS;
1606 		goto exit;
1607 	}
1608 //	  } else {
1609 //		  if (down_trylock(&dev->limit_sem)) {
1610 //			  retval = -EAGAIN;
1611 //			  goto exit;
1612 //		  }
1613 //	  }
1614 
1615 	spin_lock_irq(&dev->err_lock);
1616 	retval = dev->errors;
1617 	if (retval < 0) {
1618 		/* any error is reported once */
1619 		dev->errors = 0;
1620 		/* to preserve notifications about reset */
1621 		retval = (retval == -EPIPE) ? retval : -EIO;
1622 	}
1623 	spin_unlock_irq(&dev->err_lock);
1624 	if (retval < 0)
1625 		goto error;
1626 
1627 	/* create a urb, and a buffer for it, and copy the data to the urb */
1628 	urb = usb_alloc_urb(0, GFP_KERNEL);
1629 	if (!urb) {
1630 		retval = -ENOMEM;
1631 		goto error;
1632 	}
1633 
1634 	buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL,
1635 				 &urb->transfer_dma);
1636 	if (!buf) {
1637 		retval = -ENOMEM;
1638 		goto error;
1639 	}
1640 
1641 	memcpy(buf, buffer, count);
1642 
1643 	/* this lock makes sure we don't submit URBs to gone devices */
1644 	mutex_lock(&dev->io_mutex);
1645 	if (!dev->interface) {		      /* disconnect() was called */
1646 		mutex_unlock(&dev->io_mutex);
1647 		retval = -ENODEV;
1648 		goto error;
1649 	}
1650 
1651 	/* initialize the urb properly */
1652 	usb_fill_bulk_urb(urb, dev->udev,
1653 			  usb_sndbulkpipe(dev->udev, dev->bulk_out_endpoint_addr),
1654 			  buf, writesize, lpvo_write_bulk_callback, dev);
1655 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1656 	usb_anchor_urb(urb, &dev->submitted);
1657 
1658 	/* send the data out the bulk port */
1659 	retval = usb_submit_urb(urb, GFP_KERNEL);
1660 	mutex_unlock(&dev->io_mutex);
1661 	if (retval) {
1662 		dev_err(&dev->interface->dev, "failed submitting write urb, error %d\n", retval);
1663 		goto error_unanchor;
1664 	}
1665 
1666 	/*
1667 	 * release our reference to this urb, the USB core will eventually free
1668 	 * it entirely
1669 	 */
1670 	usb_free_urb(urb);
1671 
1672 	return writesize;
1673 
1674 error_unanchor:
1675 	usb_unanchor_urb(urb);
1676 error:
1677 	if (urb) {
1678 		usb_free_coherent(dev->udev, writesize, buf, urb->transfer_dma);
1679 		usb_free_urb(urb);
1680 	}
1681 	up(&dev->limit_sem);
1682 
1683 exit:
1684 	return retval;
1685 }
1686 
1687 /*
1688  * services for the user space devices
1689  */
1690 
1691 #if USER_DEVICE	 /* conditional compilation of user space device */
1692 
lpvo_flush(struct file * file,fl_owner_t id)1693 static int lpvo_flush(struct file *file, fl_owner_t id)
1694 {
1695 	struct lpvo *dev;
1696 	int res;
1697 
1698 	dev = file->private_data;
1699 	if (!dev)
1700 		return -ENODEV;
1701 
1702 	/* wait for io to stop */
1703 	mutex_lock(&dev->io_mutex);
1704 	lpvo_draw_down(dev);
1705 
1706 	/* read out errors, leave subsequent opens a clean slate */
1707 	spin_lock_irq(&dev->err_lock);
1708 	res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0;
1709 	dev->errors = 0;
1710 	spin_unlock_irq(&dev->err_lock);
1711 
1712 	mutex_unlock(&dev->io_mutex);
1713 
1714 	return res;
1715 }
1716 
lpvo_open(struct inode * inode,struct file * file)1717 static int lpvo_open(struct inode *inode, struct file *file)
1718 {
1719 	struct lpvo *dev;
1720 	struct usb_interface *interface;
1721 	int subminor;
1722 	int retval = 0;
1723 
1724 	subminor = iminor(inode);
1725 
1726 	interface = usb_find_interface(&lpvo_driver, subminor);
1727 	if (!interface) {
1728 		pr_err("can't find device for minor %d\n", subminor);
1729 		retval = -ENODEV;
1730 		goto exit;
1731 	}
1732 
1733 	dev = usb_get_intfdata(interface);
1734 	if (!dev) {
1735 		retval = -ENODEV;
1736 		goto exit;
1737 	}
1738 
1739 	retval = usb_autopm_get_interface(interface);
1740 	if (retval)
1741 		goto exit;
1742 
1743 	/* increment our usage count for the device */
1744 	kref_get(&dev->kref);
1745 
1746 	/* save our object in the file's private structure */
1747 	file->private_data = dev;
1748 
1749 exit:
1750 	return retval;
1751 }
1752 
lpvo_release(struct inode * inode,struct file * file)1753 static int lpvo_release(struct inode *inode, struct file *file)
1754 {
1755 	struct lpvo *dev;
1756 
1757 	dev = file->private_data;
1758 	if (!dev)
1759 		return -ENODEV;
1760 
1761 	/* allow the device to be autosuspended */
1762 	mutex_lock(&dev->io_mutex);
1763 	if (dev->interface)
1764 		usb_autopm_put_interface(dev->interface);
1765 	mutex_unlock(&dev->io_mutex);
1766 
1767 	/* decrement the count on our device */
1768 	kref_put(&dev->kref, lpvo_delete);
1769 	return 0;
1770 }
1771 
1772 /*
1773  * user space access to read function
1774  */
1775 
lpvo_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)1776 static ssize_t lpvo_read(struct file *file, char __user *buffer, size_t count,
1777 			 loff_t *ppos)
1778 {
1779 	struct lpvo *dev;
1780 	char *buf;
1781 	ssize_t rv;
1782 
1783 	dev = file->private_data;
1784 
1785 	buf = kmalloc(count, GFP_KERNEL);
1786 	if (!buf)
1787 		return -ENOMEM;
1788 
1789 	rv = lpvo_do_read(dev, buf, count);
1790 
1791 	if (rv > 0) {
1792 		if (copy_to_user(buffer, buf, rv)) {
1793 			kfree(buf);
1794 			return -EFAULT;
1795 		}
1796 	}
1797 	kfree(buf);
1798 	return rv;
1799 }
1800 
1801 /*
1802  * user space access to write function
1803  */
1804 
lpvo_write(struct file * file,const char __user * user_buffer,size_t count,loff_t * ppos)1805 static ssize_t lpvo_write(struct file *file, const char __user *user_buffer,
1806 			  size_t count, loff_t *ppos)
1807 {
1808 	struct lpvo *dev;
1809 	char *buf;
1810 	ssize_t rv;
1811 
1812 	dev = file->private_data;
1813 
1814 	buf = kmalloc(count, GFP_KERNEL);
1815 	if (!buf)
1816 		return -ENOMEM;
1817 
1818 	if (copy_from_user(buf, user_buffer, count)) {
1819 		kfree(buf);
1820 		return -EFAULT;
1821 	}
1822 
1823 	rv = lpvo_do_write(dev, buf, count);
1824 	kfree(buf);
1825 	return rv;
1826 }
1827 #endif
1828 
1829 static const struct file_operations lpvo_fops = {
1830 	.owner =	THIS_MODULE,
1831 #if USER_DEVICE
1832 	.read =	   lpvo_read,
1833 	.write =   lpvo_write,
1834 	.open =	   lpvo_open,
1835 	.release = lpvo_release,
1836 	.flush =   lpvo_flush,
1837 	.llseek =  noop_llseek,
1838 #endif
1839 };
1840 
1841 /*
1842  * usb class driver info in order to get a minor number from the usb core,
1843  * and to have the device registered with the driver core
1844  */
1845 #if USER_DEVICE
1846 static struct usb_class_driver lpvo_class = {
1847 	.name =		       "lpvo_raw%d",
1848 	.fops =		       &lpvo_fops,
1849 	.minor_base =	     USB_LPVO_MINOR_BASE,
1850 };
1851 #endif
1852 
lpvo_probe(struct usb_interface * interface,const struct usb_device_id * id)1853 static int lpvo_probe(struct usb_interface *interface,
1854 		      const struct usb_device_id *id)
1855 {
1856 	struct lpvo *dev;
1857 	struct usb_endpoint_descriptor *bulk_in, *bulk_out;
1858 	int retval;
1859 	char *device_path;
1860 
1861 	mutex_init(&minors_lock);   /* required for handling minor numbers table */
1862 
1863 	/* allocate memory for our device state and initialize it */
1864 	dev = kzalloc_obj(*dev);
1865 	if (!dev)
1866 		return -ENOMEM;
1867 
1868 	kref_init(&dev->kref);
1869 	sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
1870 	mutex_init(&dev->io_mutex);
1871 	spin_lock_init(&dev->err_lock);
1872 	init_usb_anchor(&dev->submitted);
1873 	init_waitqueue_head(&dev->bulk_in_wait);
1874 
1875 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
1876 	dev->interface = interface;
1877 
1878 	/* set up the endpoint information */
1879 	/* use only the first bulk-in and bulk-out endpoints */
1880 	retval = usb_find_common_endpoints(interface->cur_altsetting,
1881 					   &bulk_in, &bulk_out, NULL, NULL);
1882 	if (retval) {
1883 		dev_err(&interface->dev,
1884 			"Could not find both bulk-in and bulk-out endpoints\n");
1885 		goto error;
1886 	}
1887 
1888 	dev->bulk_in_size = usb_endpoint_maxp(bulk_in);
1889 	dev->bulk_in_endpoint_addr = bulk_in->bEndpointAddress;
1890 	dev->bulk_in_buffer = kmalloc(dev->bulk_in_size, GFP_KERNEL);
1891 	if (!dev->bulk_in_buffer) {
1892 		retval = -ENOMEM;
1893 		goto error;
1894 	}
1895 	dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1896 	if (!dev->bulk_in_urb) {
1897 		retval = -ENOMEM;
1898 		goto error;
1899 	}
1900 
1901 	dev->bulk_out_endpoint_addr = bulk_out->bEndpointAddress;
1902 
1903 	/* save our data pointer in this interface device */
1904 	usb_set_intfdata(interface, dev);
1905 
1906 	/* let the world know */
1907 
1908 	device_path = kobject_get_path(&dev->udev->dev.kobj, GFP_KERNEL);
1909 	dev_dbg(&interface->dev, "New lpvo_usb_device -> bus: %d  dev: %d  path: %s\n",
1910 		dev->udev->bus->busnum, dev->udev->devnum, device_path);
1911 	kfree(device_path);
1912 
1913 #if USER_DEVICE
1914 	/* we can register the device now, as it is ready */
1915 	retval = usb_register_dev(interface, &lpvo_class);
1916 	if (retval) {
1917 		/* something prevented us from registering this driver */
1918 		dev_err(&interface->dev,
1919 			"Not able to get a minor for this device.\n");
1920 		usb_set_intfdata(interface, NULL);
1921 		goto error;
1922 	}
1923 #endif
1924 
1925 	write_latency_timer(dev->udev);	    /* adjust the latency timer */
1926 
1927 	usb_gpib_init_module(interface);    /* last, init the lpvo for this minor */
1928 
1929 	return 0;
1930 
1931 error:
1932 	/* this frees allocated memory */
1933 	kref_put(&dev->kref, lpvo_delete);
1934 
1935 	return retval;
1936 }
1937 
lpvo_disconnect(struct usb_interface * interface)1938 static void lpvo_disconnect(struct usb_interface *interface)
1939 {
1940 	struct lpvo *dev;
1941 	int minor = interface->minor;
1942 
1943 	usb_gpib_exit_module(minor);	  /* first, disactivate the lpvo */
1944 
1945 	dev = usb_get_intfdata(interface);
1946 	usb_set_intfdata(interface, NULL);
1947 
1948 #if USER_DEVICE
1949 	/* give back our minor */
1950 	usb_deregister_dev(interface, &lpvo_class);
1951 #endif
1952 
1953 	/* prevent more I/O from starting */
1954 	mutex_lock(&dev->io_mutex);
1955 	dev->interface = NULL;
1956 	mutex_unlock(&dev->io_mutex);
1957 
1958 	usb_kill_anchored_urbs(&dev->submitted);
1959 
1960 	/* decrement our usage count */
1961 	kref_put(&dev->kref, lpvo_delete);
1962 }
1963 
lpvo_draw_down(struct lpvo * dev)1964 static void lpvo_draw_down(struct lpvo *dev)
1965 {
1966 	int time;
1967 
1968 	time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
1969 	if (!time)
1970 		usb_kill_anchored_urbs(&dev->submitted);
1971 	usb_kill_urb(dev->bulk_in_urb);
1972 }
1973 
lpvo_suspend(struct usb_interface * intf,pm_message_t message)1974 static int lpvo_suspend(struct usb_interface *intf, pm_message_t message)
1975 {
1976 	struct lpvo *dev = usb_get_intfdata(intf);
1977 
1978 	if (!dev)
1979 		return 0;
1980 	lpvo_draw_down(dev);
1981 	return 0;
1982 }
1983 
lpvo_resume(struct usb_interface * intf)1984 static int lpvo_resume(struct usb_interface *intf)
1985 {
1986 	return 0;
1987 }
1988 
lpvo_pre_reset(struct usb_interface * intf)1989 static int lpvo_pre_reset(struct usb_interface *intf)
1990 {
1991 	struct lpvo *dev = usb_get_intfdata(intf);
1992 
1993 	mutex_lock(&dev->io_mutex);
1994 	lpvo_draw_down(dev);
1995 
1996 	return 0;
1997 }
1998 
lpvo_post_reset(struct usb_interface * intf)1999 static int lpvo_post_reset(struct usb_interface *intf)
2000 {
2001 	struct lpvo *dev = usb_get_intfdata(intf);
2002 
2003 	/* we are sure no URBs are active - no locking needed */
2004 	dev->errors = -EPIPE;
2005 	mutex_unlock(&dev->io_mutex);
2006 
2007 	return 0;
2008 }
2009 
2010 static struct usb_driver lpvo_driver = {
2011 	.name =			NAME,
2012 	.probe =		lpvo_probe,
2013 	.disconnect =		lpvo_disconnect,
2014 	.suspend =		lpvo_suspend,
2015 	.resume =		lpvo_resume,
2016 	.pre_reset =		lpvo_pre_reset,
2017 	.post_reset =		lpvo_post_reset,
2018 	.id_table =		lpvo_table,
2019 	.supports_autosuspend = 1,
2020 };
2021 
2022 module_usb_driver(lpvo_driver);
2023