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