1 // SPDX-License-Identifier: GPL-2.0
2
3 /***************************************************************************
4 * copyright : (C) 2001, 2002 by Frank Mori Hess
5 ***************************************************************************/
6
7 #define dev_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9 #include "ibsys.h"
10 #include <linux/delay.h>
11 #include <linux/kthread.h>
12 #include <linux/vmalloc.h>
13
14 /*
15 * IBCAC
16 * Return to the controller active state from the
17 * controller standby state, i.e., turn ATN on. Note
18 * that in order to enter the controller active state
19 * from the controller idle state, ibsic must be called.
20 * If sync is non-zero, attempt to take control synchronously.
21 * If fallback_to_async is non-zero, try to take control asynchronously
22 * if synchronous attempt fails.
23 */
ibcac(struct gpib_board * board,int sync,int fallback_to_async)24 int ibcac(struct gpib_board *board, int sync, int fallback_to_async)
25 {
26 int status = ibstatus(board);
27 int retval;
28
29 if ((status & CIC) == 0)
30 return -EINVAL;
31
32 if (status & ATN)
33 return 0;
34
35 if (sync && (status & LACS) == 0)
36 /* tcs (take control synchronously) can only possibly work when
37 * controller is listener. Error code also needs to be -ETIMEDOUT
38 * or it will giveout without doing fallback.
39 */
40 retval = -ETIMEDOUT;
41 else
42 retval = board->interface->take_control(board, sync);
43
44 if (retval < 0 && fallback_to_async) {
45 if (sync && retval == -ETIMEDOUT)
46 retval = board->interface->take_control(board, 0);
47 }
48 board->interface->update_status(board, 0);
49
50 return retval;
51 }
52
53 /* After ATN is asserted, it should cause any connected devices
54 * to start listening for command bytes and leave acceptor idle state.
55 * So if ATN is asserted and neither NDAC or NRFD are asserted,
56 * then there are no devices and ibcmd should error out immediately.
57 * Some gpib hardware sees itself asserting NDAC/NRFD when it
58 * is controller in charge, in which case this check will
59 * do nothing useful (but shouldn't cause any harm either).
60 * Drivers that don't need this check (ni_usb for example) may
61 * set the skip_check_for_command_acceptors flag in their
62 * gpib_interface_struct to avoid useless overhead.
63 */
check_for_command_acceptors(struct gpib_board * board)64 static int check_for_command_acceptors(struct gpib_board *board)
65 {
66 int lines;
67
68 if (board->interface->skip_check_for_command_acceptors)
69 return 0;
70 if (!board->interface->line_status)
71 return 0;
72
73 udelay(2); // allow time for devices to respond to ATN if it was just asserted
74
75 lines = board->interface->line_status(board);
76 if (lines < 0)
77 return lines;
78
79 if ((lines & VALID_NRFD) && (lines & VALID_NDAC)) {
80 if ((lines & BUS_NRFD) == 0 && (lines & BUS_NDAC) == 0)
81 return -ENOTCONN;
82 }
83
84 return 0;
85 }
86
87 /*
88 * IBCMD
89 * Write cnt command bytes from buf to the GPIB. The
90 * command operation terminates only on I/O complete.
91 *
92 * NOTE:
93 * 1. Prior to beginning the command, the interface is
94 * placed in the controller active state.
95 * 2. Before calling ibcmd for the first time, ibsic
96 * must be called to initialize the GPIB and enable
97 * the interface to leave the controller idle state.
98 */
ibcmd(struct gpib_board * board,uint8_t * buf,size_t length,size_t * bytes_written)99 int ibcmd(struct gpib_board *board, uint8_t *buf, size_t length, size_t *bytes_written)
100 {
101 ssize_t ret = 0;
102 int status;
103
104 *bytes_written = 0;
105
106 status = ibstatus(board);
107
108 if ((status & CIC) == 0)
109 return -EINVAL;
110
111 os_start_timer(board, board->usec_timeout);
112
113 ret = ibcac(board, 1, 1);
114 if (ret == 0) {
115 ret = check_for_command_acceptors(board);
116 if (ret == 0)
117 ret = board->interface->command(board, buf, length, bytes_written);
118 }
119
120 os_remove_timer(board);
121
122 if (io_timed_out(board))
123 ret = -ETIMEDOUT;
124
125 return ret;
126 }
127
128 /*
129 * IBGTS
130 * Go to the controller standby state from the controller
131 * active state, i.e., turn ATN off.
132 */
133
ibgts(struct gpib_board * board)134 int ibgts(struct gpib_board *board)
135 {
136 int status = ibstatus(board);
137 int retval;
138
139 if ((status & CIC) == 0)
140 return -EINVAL;
141
142 retval = board->interface->go_to_standby(board); /* go to standby */
143
144 board->interface->update_status(board, 0);
145
146 return retval;
147 }
148
autospoll_wait_should_wake_up(struct gpib_board * board)149 static int autospoll_wait_should_wake_up(struct gpib_board *board)
150 {
151 int retval;
152
153 mutex_lock(&board->big_gpib_mutex);
154
155 retval = board->master && board->autospollers > 0 &&
156 !atomic_read(&board->stuck_srq) &&
157 test_and_clear_bit(SRQI_NUM, &board->status);
158
159 mutex_unlock(&board->big_gpib_mutex);
160 return retval;
161 }
162
autospoll_thread(void * board_void)163 static int autospoll_thread(void *board_void)
164 {
165 struct gpib_board *board = board_void;
166 int retval = 0;
167
168 dev_dbg(board->gpib_dev, "entering autospoll thread\n");
169
170 while (1) {
171 wait_event_interruptible(board->wait,
172 kthread_should_stop() ||
173 autospoll_wait_should_wake_up(board));
174 dev_dbg(board->gpib_dev, "autospoll wait satisfied\n");
175 if (kthread_should_stop())
176 break;
177
178 mutex_lock(&board->big_gpib_mutex);
179 /* make sure we are still good after we have lock */
180 if (board->autospollers <= 0 || board->master == 0) {
181 mutex_unlock(&board->big_gpib_mutex);
182 continue;
183 }
184 mutex_unlock(&board->big_gpib_mutex);
185
186 if (try_module_get(board->provider_module)) {
187 retval = autopoll_all_devices(board);
188 module_put(board->provider_module);
189 } else {
190 dev_err(board->gpib_dev, "try_module_get() failed!\n");
191 }
192 if (retval <= 0) {
193 dev_err(board->gpib_dev, "stuck SRQ\n");
194
195 atomic_set(&board->stuck_srq, 1); // XXX could be better
196 set_bit(SRQI_NUM, &board->status);
197 }
198 }
199 return retval;
200 }
201
ibonline(struct gpib_board * board)202 int ibonline(struct gpib_board *board)
203 {
204 int retval;
205
206 if (board->online)
207 return -EBUSY;
208 if (!board->interface)
209 return -ENODEV;
210 retval = gpib_allocate_board(board);
211 if (retval < 0)
212 return retval;
213
214 board->dev = NULL;
215 board->local_ppoll_mode = 0;
216 retval = board->interface->attach(board, &board->config);
217 if (retval < 0) {
218 board->interface->detach(board);
219 return retval;
220 }
221 /* nios2nommu on 2.6.11 uclinux kernel has weird problems
222 * with autospoll thread causing huge slowdowns
223 */
224 #ifndef CONFIG_NIOS2
225 board->autospoll_task = kthread_run(&autospoll_thread, board,
226 "gpib%d_autospoll_kthread", board->minor);
227 retval = IS_ERR(board->autospoll_task);
228 if (retval) {
229 dev_err(board->gpib_dev, "failed to create autospoll thread\n");
230 board->interface->detach(board);
231 return retval;
232 }
233 #endif
234 board->online = 1;
235 dev_dbg(board->gpib_dev, "board online\n");
236
237 return 0;
238 }
239
240 /* XXX need to make sure board is generally not in use (grab board lock?) */
iboffline(struct gpib_board * board)241 int iboffline(struct gpib_board *board)
242 {
243 int retval;
244
245 if (board->online == 0)
246 return 0;
247 if (!board->interface)
248 return -ENODEV;
249
250 if (board->autospoll_task && !IS_ERR(board->autospoll_task)) {
251 retval = kthread_stop(board->autospoll_task);
252 if (retval)
253 dev_err(board->gpib_dev, "kthread_stop returned %i\n", retval);
254 board->autospoll_task = NULL;
255 }
256
257 board->interface->detach(board);
258 gpib_deallocate_board(board);
259 board->online = 0;
260 dev_dbg(board->gpib_dev, "board offline\n");
261
262 return 0;
263 }
264
265 /*
266 * IBLINES
267 * Poll the GPIB control lines and return their status in buf.
268 *
269 * LSB (bits 0-7) - VALID lines mask (lines that can be monitored).
270 * Next LSB (bits 8-15) - STATUS lines mask (lines that are currently set).
271 *
272 */
iblines(const struct gpib_board * board,short * lines)273 int iblines(const struct gpib_board *board, short *lines)
274 {
275 int retval;
276
277 *lines = 0;
278 if (!board->interface->line_status)
279 return 0;
280 retval = board->interface->line_status(board);
281 if (retval < 0)
282 return retval;
283 *lines = retval;
284 return 0;
285 }
286
287 /*
288 * IBRD
289 * Read up to 'length' bytes of data from the GPIB into buf. End
290 * on detection of END (EOI and or EOS) and set 'end_flag'.
291 *
292 * NOTE:
293 * 1. The interface is placed in the controller standby
294 * state prior to beginning the read.
295 * 2. Prior to calling ibrd, the intended devices as well
296 * as the interface board itself must be addressed by
297 * calling ibcmd.
298 */
299
ibrd(struct gpib_board * board,uint8_t * buf,size_t length,int * end_flag,size_t * nbytes)300 int ibrd(struct gpib_board *board, uint8_t *buf, size_t length, int *end_flag, size_t *nbytes)
301 {
302 ssize_t ret = 0;
303 int retval;
304 size_t bytes_read;
305
306 *nbytes = 0;
307 *end_flag = 0;
308 if (length == 0)
309 return 0;
310
311 if (board->master) {
312 retval = ibgts(board);
313 if (retval < 0)
314 return retval;
315 }
316 /* XXX resetting timer here could cause timeouts take longer than they should,
317 * since read_ioctl calls this
318 * function in a loop, there is probably a similar problem with writes/commands
319 */
320 os_start_timer(board, board->usec_timeout);
321
322 do {
323 ret = board->interface->read(board, buf, length - *nbytes, end_flag, &bytes_read);
324 if (ret < 0)
325 goto ibrd_out;
326
327 buf += bytes_read;
328 *nbytes += bytes_read;
329 if (need_resched())
330 schedule();
331 } while (ret == 0 && *nbytes > 0 && *nbytes < length && *end_flag == 0);
332 ibrd_out:
333 os_remove_timer(board);
334
335 return ret;
336 }
337
338 /*
339 * IBRPP
340 * Conduct a parallel poll and return the byte in buf.
341 *
342 * NOTE:
343 * 1. Prior to conducting the poll the interface is placed
344 * in the controller active state.
345 */
ibrpp(struct gpib_board * board,uint8_t * result)346 int ibrpp(struct gpib_board *board, uint8_t *result)
347 {
348 int retval = 0;
349
350 os_start_timer(board, board->usec_timeout);
351 retval = ibcac(board, 1, 1);
352 if (retval)
353 return -1;
354
355 retval = board->interface->parallel_poll(board, result);
356
357 os_remove_timer(board);
358 return retval;
359 }
360
ibppc(struct gpib_board * board,uint8_t configuration)361 int ibppc(struct gpib_board *board, uint8_t configuration)
362 {
363 configuration &= 0x1f;
364 board->interface->parallel_poll_configure(board, configuration);
365 board->parallel_poll_configuration = configuration;
366
367 return 0;
368 }
369
ibrsv2(struct gpib_board * board,uint8_t status_byte,int new_reason_for_service)370 int ibrsv2(struct gpib_board *board, uint8_t status_byte, int new_reason_for_service)
371 {
372 int board_status = ibstatus(board);
373 const unsigned int MSS = status_byte & request_service_bit;
374
375 if ((board_status & CIC))
376 return -EINVAL;
377
378 if (MSS == 0 && new_reason_for_service)
379 return -EINVAL;
380
381 if (board->interface->serial_poll_response2) {
382 board->interface->serial_poll_response2(board, status_byte, new_reason_for_service);
383 // fall back on simpler serial_poll_response if the behavior would be the same
384 } else if (board->interface->serial_poll_response &&
385 (MSS == 0 || (MSS && new_reason_for_service))) {
386 board->interface->serial_poll_response(board, status_byte);
387 } else {
388 return -EOPNOTSUPP;
389 }
390
391 return 0;
392 }
393
394 /*
395 * IBSIC
396 * Send IFC for at least 100 microseconds.
397 *
398 * NOTE:
399 * 1. Ibsic must be called prior to the first call to
400 * ibcmd in order to initialize the bus and enable the
401 * interface to leave the controller idle state.
402 */
ibsic(struct gpib_board * board,unsigned int usec_duration)403 int ibsic(struct gpib_board *board, unsigned int usec_duration)
404 {
405 if (board->master == 0)
406 return -EINVAL;
407
408 if (usec_duration < 100)
409 usec_duration = 100;
410 if (usec_duration > 1000)
411 usec_duration = 1000;
412
413 dev_dbg(board->gpib_dev, "sending interface clear, delay = %ius\n", usec_duration);
414 board->interface->interface_clear(board, 1);
415 udelay(usec_duration);
416 board->interface->interface_clear(board, 0);
417
418 return 0;
419 }
420
421 /* FIXME make int */
ibrsc(struct gpib_board * board,int request_control)422 void ibrsc(struct gpib_board *board, int request_control)
423 {
424 board->master = request_control != 0;
425 if (board->interface->request_system_control)
426 board->interface->request_system_control(board, request_control);
427 }
428
429 /*
430 * IBSRE
431 * Send REN true if v is non-zero or false if v is zero.
432 */
ibsre(struct gpib_board * board,int enable)433 int ibsre(struct gpib_board *board, int enable)
434 {
435 if (board->master == 0)
436 return -EINVAL;
437
438 board->interface->remote_enable(board, enable); /* set or clear REN */
439 if (!enable)
440 usleep_range(100, 150);
441
442 return 0;
443 }
444
445 /*
446 * IBPAD
447 * change the GPIB address of the interface board. The address
448 * must be 0 through 30. ibonl resets the address to PAD.
449 */
ibpad(struct gpib_board * board,unsigned int addr)450 int ibpad(struct gpib_board *board, unsigned int addr)
451 {
452 if (addr > MAX_GPIB_PRIMARY_ADDRESS)
453 return -EINVAL;
454
455 board->pad = addr;
456 if (board->online)
457 board->interface->primary_address(board, board->pad);
458 dev_dbg(board->gpib_dev, "set primary addr to %i\n", board->pad);
459 return 0;
460 }
461
462 /*
463 * IBSAD
464 * change the secondary GPIB address of the interface board.
465 * The address must be 0 through 30, or negative disables. ibonl resets the
466 * address to SAD.
467 */
ibsad(struct gpib_board * board,int addr)468 int ibsad(struct gpib_board *board, int addr)
469 {
470 if (addr > MAX_GPIB_SECONDARY_ADDRESS)
471 return -EINVAL;
472 board->sad = addr;
473 if (board->online) {
474 if (board->sad >= 0)
475 board->interface->secondary_address(board, board->sad, 1);
476 else
477 board->interface->secondary_address(board, 0, 0);
478 }
479 dev_dbg(board->gpib_dev, "set secondary addr to %i\n", board->sad);
480
481 return 0;
482 }
483
484 /*
485 * IBEOS
486 * Set the end-of-string modes for I/O operations to v.
487 *
488 */
ibeos(struct gpib_board * board,int eos,int eosflags)489 int ibeos(struct gpib_board *board, int eos, int eosflags)
490 {
491 int retval;
492
493 if (eosflags & ~EOS_MASK)
494 return -EINVAL;
495 if (eosflags & REOS) {
496 retval = board->interface->enable_eos(board, eos, eosflags & BIN);
497 } else {
498 board->interface->disable_eos(board);
499 retval = 0;
500 }
501 return retval;
502 }
503
ibstatus(struct gpib_board * board)504 int ibstatus(struct gpib_board *board)
505 {
506 return general_ibstatus(board, NULL, 0, 0, NULL);
507 }
508
general_ibstatus(struct gpib_board * board,const gpib_status_queue_t * device,int clear_mask,int set_mask,gpib_descriptor_t * desc)509 int general_ibstatus(struct gpib_board *board, const gpib_status_queue_t *device,
510 int clear_mask, int set_mask, gpib_descriptor_t *desc)
511 {
512 int status = 0;
513 short line_status;
514
515 if (board->private_data) {
516 status = board->interface->update_status(board, clear_mask);
517 /* XXX should probably stop having drivers use TIMO bit in
518 * board->status to avoid confusion
519 */
520 status &= ~TIMO;
521 /* get real SRQI status if we can */
522 if (iblines(board, &line_status) == 0) {
523 if ((line_status & VALID_SRQ)) {
524 if ((line_status & BUS_SRQ))
525 status |= SRQI;
526 else
527 status &= ~SRQI;
528 }
529 }
530 }
531 if (device)
532 if (num_status_bytes(device))
533 status |= RQS;
534
535 if (desc) {
536 if (set_mask & CMPL)
537 atomic_set(&desc->io_in_progress, 0);
538 else if (clear_mask & CMPL)
539 atomic_set(&desc->io_in_progress, 1);
540
541 if (atomic_read(&desc->io_in_progress))
542 status &= ~CMPL;
543 else
544 status |= CMPL;
545 }
546 if (num_gpib_events(&board->event_queue))
547 status |= EVENT;
548 else
549 status &= ~EVENT;
550
551 return status;
552 }
553
554 struct wait_info {
555 struct gpib_board *board;
556 struct timer_list timer;
557 int timed_out;
558 unsigned long usec_timeout;
559 };
560
wait_timeout(struct timer_list * t)561 static void wait_timeout(struct timer_list *t)
562 {
563 struct wait_info *winfo = from_timer(winfo, t, timer);
564
565 winfo->timed_out = 1;
566 wake_up_interruptible(&winfo->board->wait);
567 }
568
init_wait_info(struct wait_info * winfo)569 static void init_wait_info(struct wait_info *winfo)
570 {
571 winfo->board = NULL;
572 winfo->timed_out = 0;
573 timer_setup_on_stack(&winfo->timer, wait_timeout, 0);
574 }
575
wait_satisfied(struct wait_info * winfo,gpib_status_queue_t * status_queue,int wait_mask,int * status,gpib_descriptor_t * desc)576 static int wait_satisfied(struct wait_info *winfo, gpib_status_queue_t *status_queue,
577 int wait_mask, int *status, gpib_descriptor_t *desc)
578 {
579 struct gpib_board *board = winfo->board;
580 int temp_status;
581
582 if (mutex_lock_interruptible(&board->big_gpib_mutex))
583 return -ERESTARTSYS;
584
585 temp_status = general_ibstatus(board, status_queue, 0, 0, desc);
586
587 mutex_unlock(&board->big_gpib_mutex);
588
589 if (winfo->timed_out)
590 temp_status |= TIMO;
591 else
592 temp_status &= ~TIMO;
593 if (wait_mask & temp_status) {
594 *status = temp_status;
595 return 1;
596 }
597 //XXX does wait for END work?
598 return 0;
599 }
600
601 /* install timer interrupt handler */
start_wait_timer(struct wait_info * winfo)602 static void start_wait_timer(struct wait_info *winfo)
603 /* Starts the timeout task */
604 {
605 winfo->timed_out = 0;
606
607 if (winfo->usec_timeout > 0)
608 mod_timer(&winfo->timer, jiffies + usec_to_jiffies(winfo->usec_timeout));
609 }
610
remove_wait_timer(struct wait_info * winfo)611 static void remove_wait_timer(struct wait_info *winfo)
612 {
613 timer_delete_sync(&winfo->timer);
614 destroy_timer_on_stack(&winfo->timer);
615 }
616
617 /*
618 * IBWAIT
619 * Check or wait for a GPIB event to occur. The mask argument
620 * is a bit vector corresponding to the status bit vector. It
621 * has a bit set for each condition which can terminate the wait
622 * If the mask is 0 then
623 * no condition is waited for.
624 */
ibwait(struct gpib_board * board,int wait_mask,int clear_mask,int set_mask,int * status,unsigned long usec_timeout,gpib_descriptor_t * desc)625 int ibwait(struct gpib_board *board, int wait_mask, int clear_mask, int set_mask,
626 int *status, unsigned long usec_timeout, gpib_descriptor_t *desc)
627 {
628 int retval = 0;
629 gpib_status_queue_t *status_queue;
630 struct wait_info winfo;
631
632 if (desc->is_board)
633 status_queue = NULL;
634 else
635 status_queue = get_gpib_status_queue(board, desc->pad, desc->sad);
636
637 if (wait_mask == 0) {
638 *status = general_ibstatus(board, status_queue, clear_mask, set_mask, desc);
639 return 0;
640 }
641
642 mutex_unlock(&board->big_gpib_mutex);
643
644 init_wait_info(&winfo);
645 winfo.board = board;
646 winfo.usec_timeout = usec_timeout;
647 start_wait_timer(&winfo);
648
649 if (wait_event_interruptible(board->wait, wait_satisfied(&winfo, status_queue,
650 wait_mask, status, desc))) {
651 dev_dbg(board->gpib_dev, "wait interrupted\n");
652 retval = -ERESTARTSYS;
653 }
654 remove_wait_timer(&winfo);
655
656 if (retval)
657 return retval;
658 if (mutex_lock_interruptible(&board->big_gpib_mutex))
659 return -ERESTARTSYS;
660
661 /* make sure we only clear status bits that we are reporting */
662 if (*status & clear_mask || set_mask)
663 general_ibstatus(board, status_queue, *status & clear_mask, set_mask, NULL);
664
665 return 0;
666 }
667
668 /*
669 * IBWRT
670 * Write cnt bytes of data from buf to the GPIB. The write
671 * operation terminates only on I/O complete.
672 *
673 * NOTE:
674 * 1. Prior to beginning the write, the interface is
675 * placed in the controller standby state.
676 * 2. Prior to calling ibwrt, the intended devices as
677 * well as the interface board itself must be
678 * addressed by calling ibcmd.
679 */
ibwrt(struct gpib_board * board,uint8_t * buf,size_t cnt,int send_eoi,size_t * bytes_written)680 int ibwrt(struct gpib_board *board, uint8_t *buf, size_t cnt, int send_eoi, size_t *bytes_written)
681 {
682 int ret = 0;
683 int retval;
684
685 if (cnt == 0)
686 return 0;
687
688 if (board->master) {
689 retval = ibgts(board);
690 if (retval < 0)
691 return retval;
692 }
693 os_start_timer(board, board->usec_timeout);
694 ret = board->interface->write(board, buf, cnt, send_eoi, bytes_written);
695
696 if (io_timed_out(board))
697 ret = -ETIMEDOUT;
698
699 os_remove_timer(board);
700
701 return ret;
702 }
703
704