1 // SPDX-License-Identifier: GPL-2.0
3 /***************************************************************************
4 * copyright : (C) 2001, 2002 by Frank Mori Hess
5 ***************************************************************************/
8 #include <linux/delay.h>
9 #include <linux/kthread.h>
10 #include <linux/vmalloc.h>
14 * Return to the controller active state from the
15 * controller standby state, i.e., turn ATN on. Note
16 * that in order to enter the controller active state
17 * from the controller idle state, ibsic must be called.
18 * If sync is non-zero, attempt to take control synchronously.
19 * If fallback_to_async is non-zero, try to take control asynchronously
20 * if synchronous attempt fails.
22 int ibcac(gpib_board_t *board, int sync, int fallback_to_async)
24 int status = ibstatus(board);
27 if ((status & CIC) == 0) {
28 pr_err("gpib: not CIC during %s()\n", __func__);
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.
42 retval = board->interface->take_control(board, sync);
44 if (retval < 0 && fallback_to_async) {
45 if (sync && retval == -ETIMEDOUT)
46 retval = board->interface->take_control(board, 0);
48 board->interface->update_status(board, 0);
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.
64 static int check_for_command_acceptors(gpib_board_t *board)
68 if (board->interface->skip_check_for_command_acceptors)
70 if (!board->interface->line_status)
73 udelay(2); // allow time for devices to respond to ATN if it was just asserted
75 lines = board->interface->line_status(board);
79 if (lines & ValidATN) {
80 if ((lines & BusATN) == 0) {
81 pr_err("gpib: ATN not asserted in %s()?", __func__);
86 if ((lines & ValidNRFD) && (lines & ValidNDAC)) {
87 if ((lines & BusNRFD) == 0 && (lines & BusNDAC) == 0)
96 * Write cnt command bytes from buf to the GPIB. The
97 * command operation terminates only on I/O complete.
100 * 1. Prior to beginning the command, the interface is
101 * placed in the controller active state.
102 * 2. Before calling ibcmd for the first time, ibsic
103 * must be called to initialize the GPIB and enable
104 * the interface to leave the controller idle state.
106 int ibcmd(gpib_board_t *board, uint8_t *buf, size_t length, size_t *bytes_written)
113 status = ibstatus(board);
115 if ((status & CIC) == 0) {
116 pr_err("gpib: cannot send command when not controller-in-charge\n");
120 os_start_timer(board, board->usec_timeout);
122 ret = ibcac(board, 1, 1);
124 ret = check_for_command_acceptors(board);
126 ret = board->interface->command(board, buf, length, bytes_written);
129 os_remove_timer(board);
131 if (io_timed_out(board))
139 * Go to the controller standby state from the controller
140 * active state, i.e., turn ATN off.
143 int ibgts(gpib_board_t *board)
145 int status = ibstatus(board);
148 if ((status & CIC) == 0) {
149 pr_err("gpib: not CIC during %s()\n", __func__);
153 retval = board->interface->go_to_standby(board); /* go to standby */
155 pr_err("gpib: error while going to standby\n");
157 board->interface->update_status(board, 0);
162 static int autospoll_wait_should_wake_up(gpib_board_t *board)
166 mutex_lock(&board->big_gpib_mutex);
168 retval = board->master && board->autospollers > 0 &&
169 !atomic_read(&board->stuck_srq) &&
170 test_and_clear_bit(SRQI_NUM, &board->status);
172 mutex_unlock(&board->big_gpib_mutex);
176 static int autospoll_thread(void *board_void)
178 gpib_board_t *board = board_void;
181 dev_dbg(board->gpib_dev, "entering autospoll thread\n");
184 wait_event_interruptible(board->wait,
185 kthread_should_stop() ||
186 autospoll_wait_should_wake_up(board));
187 dev_dbg(board->gpib_dev, "autospoll wait satisfied\n");
188 if (kthread_should_stop())
191 mutex_lock(&board->big_gpib_mutex);
192 /* make sure we are still good after we have lock */
193 if (board->autospollers <= 0 || board->master == 0) {
194 mutex_unlock(&board->big_gpib_mutex);
197 mutex_unlock(&board->big_gpib_mutex);
199 if (try_module_get(board->provider_module)) {
200 retval = autopoll_all_devices(board);
201 module_put(board->provider_module);
203 pr_err("gpib%i: %s: try_module_get() failed!\n", board->minor, __func__);
206 pr_err("gpib%i: %s: stuck SRQ\n", board->minor, __func__);
208 atomic_set(&board->stuck_srq, 1); // XXX could be better
209 set_bit(SRQI_NUM, &board->status);
212 pr_info("gpib%i: exiting autospoll thread\n", board->minor);
216 int ibonline(gpib_board_t *board)
222 if (!board->interface)
224 retval = gpib_allocate_board(board);
229 board->local_ppoll_mode = 0;
230 retval = board->interface->attach(board, &board->config);
232 board->interface->detach(board);
233 pr_err("gpib: interface attach failed\n");
236 /* nios2nommu on 2.6.11 uclinux kernel has weird problems
237 * with autospoll thread causing huge slowdowns
240 board->autospoll_task = kthread_run(&autospoll_thread, board,
241 "gpib%d_autospoll_kthread", board->minor);
242 retval = IS_ERR(board->autospoll_task);
244 pr_err("gpib: failed to create autospoll thread\n");
245 board->interface->detach(board);
250 dev_dbg(board->gpib_dev, "gpib: board online\n");
255 /* XXX need to make sure board is generally not in use (grab board lock?) */
256 int iboffline(gpib_board_t *board)
260 if (board->online == 0)
262 if (!board->interface)
265 if (board->autospoll_task && !IS_ERR(board->autospoll_task)) {
266 retval = kthread_stop(board->autospoll_task);
268 pr_err("gpib: kthread_stop returned %i\n", retval);
269 board->autospoll_task = NULL;
272 board->interface->detach(board);
273 gpib_deallocate_board(board);
275 dev_dbg(board->gpib_dev, "gpib: board offline\n");
282 * Poll the GPIB control lines and return their status in buf.
284 * LSB (bits 0-7) - VALID lines mask (lines that can be monitored).
285 * Next LSB (bits 8-15) - STATUS lines mask (lines that are currently set).
288 int iblines(const gpib_board_t *board, short *lines)
293 if (!board->interface->line_status)
295 retval = board->interface->line_status(board);
304 * Read up to 'length' bytes of data from the GPIB into buf. End
305 * on detection of END (EOI and or EOS) and set 'end_flag'.
308 * 1. The interface is placed in the controller standby
309 * state prior to beginning the read.
310 * 2. Prior to calling ibrd, the intended devices as well
311 * as the interface board itself must be addressed by
315 int ibrd(gpib_board_t *board, uint8_t *buf, size_t length, int *end_flag, size_t *nbytes)
324 pr_warn("gpib: %s() called with zero length?\n", __func__);
329 retval = ibgts(board);
333 /* XXX resetting timer here could cause timeouts take longer than they should,
334 * since read_ioctl calls this
335 * function in a loop, there is probably a similar problem with writes/commands
337 os_start_timer(board, board->usec_timeout);
340 ret = board->interface->read(board, buf, length - *nbytes, end_flag, &bytes_read);
342 pr_err("gpib read error\n");
346 *nbytes += bytes_read;
349 } while (ret == 0 && *nbytes > 0 && *nbytes < length && *end_flag == 0);
351 os_remove_timer(board);
358 * Conduct a parallel poll and return the byte in buf.
361 * 1. Prior to conducting the poll the interface is placed
362 * in the controller active state.
364 int ibrpp(gpib_board_t *board, uint8_t *result)
368 os_start_timer(board, board->usec_timeout);
369 retval = ibcac(board, 1, 1);
373 if (board->interface->parallel_poll(board, result)) {
374 pr_err("gpib: parallel poll failed\n");
377 os_remove_timer(board);
381 int ibppc(gpib_board_t *board, uint8_t configuration)
383 configuration &= 0x1f;
384 board->interface->parallel_poll_configure(board, configuration);
385 board->parallel_poll_configuration = configuration;
390 int ibrsv2(gpib_board_t *board, uint8_t status_byte, int new_reason_for_service)
392 int board_status = ibstatus(board);
393 const unsigned int MSS = status_byte & request_service_bit;
395 if ((board_status & CIC)) {
396 pr_err("gpib: interface requested service while CIC\n");
400 if (MSS == 0 && new_reason_for_service)
403 if (board->interface->serial_poll_response2) {
404 board->interface->serial_poll_response2(board, status_byte, new_reason_for_service);
405 // fall back on simpler serial_poll_response if the behavior would be the same
406 } else if (board->interface->serial_poll_response &&
407 (MSS == 0 || (MSS && new_reason_for_service))) {
408 board->interface->serial_poll_response(board, status_byte);
418 * Send IFC for at least 100 microseconds.
421 * 1. Ibsic must be called prior to the first call to
422 * ibcmd in order to initialize the bus and enable the
423 * interface to leave the controller idle state.
425 int ibsic(gpib_board_t *board, unsigned int usec_duration)
427 if (board->master == 0) {
428 pr_err("gpib: tried to assert IFC when not system controller\n");
432 if (usec_duration < 100)
434 if (usec_duration > 1000) {
435 usec_duration = 1000;
436 pr_warn("gpib: warning, shortening long udelay\n");
439 dev_dbg(board->gpib_dev, "sending interface clear\n");
440 board->interface->interface_clear(board, 1);
441 udelay(usec_duration);
442 board->interface->interface_clear(board, 0);
447 void ibrsc(gpib_board_t *board, int request_control)
449 board->master = request_control != 0;
450 if (!board->interface->request_system_control) {
451 pr_err("gpib: bug! driver does not implement request_system_control()\n");
454 board->interface->request_system_control(board, request_control);
459 * Send REN true if v is non-zero or false if v is zero.
461 int ibsre(gpib_board_t *board, int enable)
463 if (board->master == 0) {
464 pr_err("gpib: tried to set REN when not system controller\n");
468 board->interface->remote_enable(board, enable); /* set or clear REN */
470 usleep_range(100, 150);
477 * change the GPIB address of the interface board. The address
478 * must be 0 through 30. ibonl resets the address to PAD.
480 int ibpad(gpib_board_t *board, unsigned int addr)
482 if (addr > MAX_GPIB_PRIMARY_ADDRESS) {
483 pr_err("gpib: invalid primary address %u\n", addr);
488 board->interface->primary_address(board, board->pad);
489 dev_dbg(board->gpib_dev, "set primary addr to %i\n", board->pad);
495 * change the secondary GPIB address of the interface board.
496 * The address must be 0 through 30, or negative disables. ibonl resets the
499 int ibsad(gpib_board_t *board, int addr)
501 if (addr > MAX_GPIB_SECONDARY_ADDRESS) {
502 pr_err("gpib: invalid secondary address %i\n", addr);
508 board->interface->secondary_address(board, board->sad, 1);
510 board->interface->secondary_address(board, 0, 0);
512 dev_dbg(board->gpib_dev, "set secondary addr to %i\n", board->sad);
519 * Set the end-of-string modes for I/O operations to v.
522 int ibeos(gpib_board_t *board, int eos, int eosflags)
526 if (eosflags & ~EOS_MASK) {
527 pr_err("bad EOS modes\n");
530 if (eosflags & REOS) {
531 retval = board->interface->enable_eos(board, eos, eosflags & BIN);
533 board->interface->disable_eos(board);
539 int ibstatus(gpib_board_t *board)
541 return general_ibstatus(board, NULL, 0, 0, NULL);
544 int general_ibstatus(gpib_board_t *board, const gpib_status_queue_t *device,
545 int clear_mask, int set_mask, gpib_descriptor_t *desc)
550 if (board->private_data) {
551 status = board->interface->update_status(board, clear_mask);
552 /* XXX should probably stop having drivers use TIMO bit in
553 * board->status to avoid confusion
556 /* get real SRQI status if we can */
557 if (iblines(board, &line_status) == 0) {
558 if ((line_status & ValidSRQ)) {
559 if ((line_status & BusSRQ))
567 if (num_status_bytes(device))
572 atomic_set(&desc->io_in_progress, 0);
573 else if (clear_mask & CMPL)
574 atomic_set(&desc->io_in_progress, 1);
576 if (atomic_read(&desc->io_in_progress))
581 if (num_gpib_events(&board->event_queue))
591 struct timer_list timer;
593 unsigned long usec_timeout;
596 static void wait_timeout(struct timer_list *t)
598 struct wait_info *winfo = from_timer(winfo, t, timer);
600 winfo->timed_out = 1;
601 wake_up_interruptible(&winfo->board->wait);
604 static void init_wait_info(struct wait_info *winfo)
607 winfo->timed_out = 0;
608 timer_setup_on_stack(&winfo->timer, wait_timeout, 0);
611 static int wait_satisfied(struct wait_info *winfo, gpib_status_queue_t *status_queue,
612 int wait_mask, int *status, gpib_descriptor_t *desc)
614 gpib_board_t *board = winfo->board;
617 if (mutex_lock_interruptible(&board->big_gpib_mutex))
620 temp_status = general_ibstatus(board, status_queue, 0, 0, desc);
622 mutex_unlock(&board->big_gpib_mutex);
624 if (winfo->timed_out)
627 temp_status &= ~TIMO;
628 if (wait_mask & temp_status) {
629 *status = temp_status;
632 //XXX does wait for END work?
636 /* install timer interrupt handler */
637 static void start_wait_timer(struct wait_info *winfo)
638 /* Starts the timeout task */
640 winfo->timed_out = 0;
642 if (winfo->usec_timeout > 0)
643 mod_timer(&winfo->timer, jiffies + usec_to_jiffies(winfo->usec_timeout));
646 static void remove_wait_timer(struct wait_info *winfo)
648 del_timer_sync(&winfo->timer);
649 destroy_timer_on_stack(&winfo->timer);
654 * Check or wait for a GPIB event to occur. The mask argument
655 * is a bit vector corresponding to the status bit vector. It
656 * has a bit set for each condition which can terminate the wait
657 * If the mask is 0 then
658 * no condition is waited for.
660 int ibwait(gpib_board_t *board, int wait_mask, int clear_mask, int set_mask,
661 int *status, unsigned long usec_timeout, gpib_descriptor_t *desc)
664 gpib_status_queue_t *status_queue;
665 struct wait_info winfo;
670 status_queue = get_gpib_status_queue(board, desc->pad, desc->sad);
672 if (wait_mask == 0) {
673 *status = general_ibstatus(board, status_queue, clear_mask, set_mask, desc);
677 mutex_unlock(&board->big_gpib_mutex);
679 init_wait_info(&winfo);
681 winfo.usec_timeout = usec_timeout;
682 start_wait_timer(&winfo);
684 if (wait_event_interruptible(board->wait, wait_satisfied(&winfo, status_queue,
685 wait_mask, status, desc))) {
686 dev_dbg(board->gpib_dev, "wait interrupted\n");
687 retval = -ERESTARTSYS;
689 remove_wait_timer(&winfo);
693 if (mutex_lock_interruptible(&board->big_gpib_mutex))
696 /* make sure we only clear status bits that we are reporting */
697 if (*status & clear_mask || set_mask)
698 general_ibstatus(board, status_queue, *status & clear_mask, set_mask, 0);
705 * Write cnt bytes of data from buf to the GPIB. The write
706 * operation terminates only on I/O complete.
709 * 1. Prior to beginning the write, the interface is
710 * placed in the controller standby state.
711 * 2. Prior to calling ibwrt, the intended devices as
712 * well as the interface board itself must be
713 * addressed by calling ibcmd.
715 int ibwrt(gpib_board_t *board, uint8_t *buf, size_t cnt, int send_eoi, size_t *bytes_written)
721 pr_warn("gpib: %s() called with zero length?\n", __func__);
726 retval = ibgts(board);
730 os_start_timer(board, board->usec_timeout);
731 ret = board->interface->write(board, buf, cnt, send_eoi, bytes_written);
733 if (io_timed_out(board))
736 os_remove_timer(board);