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