xref: /linux/drivers/gpib/common/iblib.c (revision c16ce856e422e73a54c41131e0332de1afe09b8b)
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  */
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 		/*
37 		 * tcs (take control synchronously) can only possibly work when
38 		 * controller is listener.  Error code also needs to be -ETIMEDOUT
39 		 * or it will giveout without doing fallback.
40 		 */
41 		retval = -ETIMEDOUT;
42 	else
43 		retval = board->interface->take_control(board, sync);
44 
45 	if (retval < 0 && fallback_to_async) {
46 		if (sync && retval == -ETIMEDOUT)
47 			retval = board->interface->take_control(board, 0);
48 	}
49 	board->interface->update_status(board, 0);
50 
51 	return retval;
52 }
53 
54 /*
55  * After ATN is asserted, it should cause any connected devices
56  * to start listening for command bytes and leave acceptor idle state.
57  * So if ATN is asserted and neither NDAC or NRFD are asserted,
58  * then there are no devices and ibcmd should error out immediately.
59  * Some gpib hardware sees itself asserting NDAC/NRFD when it
60  * is controller in charge, in which case this check will
61  * do nothing useful (but shouldn't cause any harm either).
62  * Drivers that don't need this check (ni_usb for example) may
63  * set the skip_check_for_command_acceptors flag in their
64  * gpib_interface_struct to avoid useless overhead.
65  */
66 static int check_for_command_acceptors(struct gpib_board *board)
67 {
68 	int lines;
69 
70 	if (board->interface->skip_check_for_command_acceptors)
71 		return 0;
72 	if (!board->interface->line_status)
73 		return 0;
74 
75 	udelay(2); // allow time for devices to respond to ATN if it was just asserted
76 
77 	lines = board->interface->line_status(board);
78 	if (lines < 0)
79 		return lines;
80 
81 	if ((lines & VALID_NRFD) && (lines & VALID_NDAC))	{
82 		if ((lines & BUS_NRFD) == 0 && (lines & BUS_NDAC) == 0)
83 			return -ENOTCONN;
84 	}
85 
86 	return 0;
87 }
88 
89 /*
90  * IBCMD
91  * Write cnt command bytes from buf to the GPIB.  The
92  * command operation terminates only on I/O complete.
93  *
94  * NOTE:
95  *      1.  Prior to beginning the command, the interface is
96  *          placed in the controller active state.
97  *      2.  Before calling ibcmd for the first time, ibsic
98  *          must be called to initialize the GPIB and enable
99  *          the interface to leave the controller idle state.
100  */
101 int ibcmd(struct gpib_board *board, u8 *buf, size_t length, size_t *bytes_written)
102 {
103 	ssize_t ret = 0;
104 	int status;
105 
106 	*bytes_written = 0;
107 
108 	status = ibstatus(board);
109 
110 	if ((status & CIC) == 0)
111 		return -EINVAL;
112 
113 	os_start_timer(board, board->usec_timeout);
114 
115 	ret = ibcac(board, 1, 1);
116 	if (ret == 0) {
117 		ret = check_for_command_acceptors(board);
118 		if (ret == 0)
119 			ret = board->interface->command(board, buf, length, bytes_written);
120 	}
121 
122 	os_remove_timer(board);
123 
124 	if (io_timed_out(board))
125 		ret = -ETIMEDOUT;
126 
127 	return ret;
128 }
129 
130 /*
131  * IBGTS
132  * Go to the controller standby state from the controller
133  * active state, i.e., turn ATN off.
134  */
135 
136 int ibgts(struct gpib_board *board)
137 {
138 	int status = ibstatus(board);
139 	int retval;
140 
141 	if ((status & CIC) == 0)
142 		return -EINVAL;
143 
144 	retval = board->interface->go_to_standby(board);    /* go to standby */
145 
146 	board->interface->update_status(board, 0);
147 
148 	return retval;
149 }
150 
151 static int autospoll_wait_should_wake_up(struct gpib_board *board)
152 {
153 	int retval;
154 
155 	mutex_lock(&board->big_gpib_mutex);
156 
157 	retval = board->master && board->autospollers > 0 &&
158 		!atomic_read(&board->stuck_srq) &&
159 		test_and_clear_bit(SRQI_NUM, &board->status);
160 
161 	mutex_unlock(&board->big_gpib_mutex);
162 	return retval;
163 }
164 
165 static int autospoll_thread(void *board_void)
166 {
167 	struct gpib_board *board = board_void;
168 	int retval = 0;
169 
170 	dev_dbg(board->gpib_dev, "entering autospoll thread\n");
171 
172 	while (1) {
173 		wait_event_interruptible(board->wait,
174 					 kthread_should_stop() ||
175 					 autospoll_wait_should_wake_up(board));
176 		dev_dbg(board->gpib_dev, "autospoll wait satisfied\n");
177 		if (kthread_should_stop())
178 			break;
179 
180 		mutex_lock(&board->big_gpib_mutex);
181 		/* make sure we are still good after we have lock */
182 		if (board->autospollers <= 0 || board->master == 0) {
183 			mutex_unlock(&board->big_gpib_mutex);
184 			continue;
185 		}
186 		mutex_unlock(&board->big_gpib_mutex);
187 
188 		if (try_module_get(board->provider_module)) {
189 			retval = autopoll_all_devices(board);
190 			module_put(board->provider_module);
191 		} else {
192 			dev_err(board->gpib_dev, "try_module_get() failed!\n");
193 		}
194 		if (retval <= 0) {
195 			dev_err(board->gpib_dev, "stuck SRQ\n");
196 		}
197 	}
198 	return retval;
199 }
200 
201 int ibonline(struct gpib_board *board)
202 {
203 	int retval;
204 
205 	if (board->online)
206 		return -EBUSY;
207 	if (!board->interface)
208 		return -ENODEV;
209 	retval = gpib_allocate_board(board);
210 	if (retval < 0)
211 		return retval;
212 
213 	board->dev = NULL;
214 	board->local_ppoll_mode = 0;
215 	retval = board->interface->attach(board, &board->config);
216 	if (retval < 0) {
217 		board->interface->detach(board);
218 		return retval;
219 	}
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 	if (IS_ERR(board->autospoll_task)) {
228 		dev_err(board->gpib_dev, "failed to create autospoll thread\n");
229 		board->interface->detach(board);
230 		return PTR_ERR(board->autospoll_task);
231 	}
232 #endif
233 	board->online = 1;
234 	dev_dbg(board->gpib_dev, "board online\n");
235 
236 	return 0;
237 }
238 
239 /* XXX need to make sure board is generally not in use (grab board lock?) */
240 int iboffline(struct gpib_board *board)
241 {
242 	int retval;
243 
244 	if (board->online == 0)
245 		return 0;
246 	if (!board->interface)
247 		return -ENODEV;
248 
249 	if (board->autospoll_task && !IS_ERR(board->autospoll_task)) {
250 		retval = kthread_stop(board->autospoll_task);
251 		if (retval)
252 			dev_err(board->gpib_dev, "kthread_stop returned %i\n", retval);
253 		board->autospoll_task = NULL;
254 	}
255 
256 	board->interface->detach(board);
257 	gpib_deallocate_board(board);
258 	board->online = 0;
259 	dev_dbg(board->gpib_dev, "board offline\n");
260 
261 	return 0;
262 }
263 
264 /*
265  * IBLINES
266  * Poll the GPIB control lines and return their status in buf.
267  *
268  *      LSB (bits 0-7)  -  VALID lines mask (lines that can be monitored).
269  * Next LSB (bits 8-15) - STATUS lines mask (lines that are currently set).
270  *
271  */
272 int iblines(const struct gpib_board *board, short *lines)
273 {
274 	int retval;
275 
276 	*lines = 0;
277 	if (!board->interface->line_status)
278 		return 0;
279 	retval = board->interface->line_status(board);
280 	if (retval < 0)
281 		return retval;
282 	*lines = retval;
283 	return 0;
284 }
285 
286 /*
287  * IBRD
288  * Read up to 'length' bytes of data from the GPIB into buf.  End
289  * on detection of END (EOI and or EOS) and set 'end_flag'.
290  *
291  * NOTE:
292  *      1.  The interface is placed in the controller standby
293  *          state prior to beginning the read.
294  *      2.  Prior to calling ibrd, the intended devices as well
295  *          as the interface board itself must be addressed by
296  *          calling ibcmd.
297  */
298 
299 int ibrd(struct gpib_board *board, u8 *buf, size_t length, int *end_flag, size_t *nbytes)
300 {
301 	ssize_t ret = 0;
302 	int retval;
303 	size_t bytes_read;
304 
305 	*nbytes = 0;
306 	*end_flag = 0;
307 	if (length == 0)
308 		return 0;
309 
310 	if (board->master) {
311 		retval = ibgts(board);
312 		if (retval < 0)
313 			return retval;
314 	}
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  */
346 int ibrpp(struct gpib_board *board, u8 *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 
361 int ibppc(struct gpib_board *board, u8 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 
370 int ibrsv2(struct gpib_board *board, u8 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  */
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 int ibrsc(struct gpib_board *board, int request_control)
422 {
423 	int retval;
424 
425 	if (!board->interface->request_system_control)
426 		return -EPERM;
427 
428 	retval = board->interface->request_system_control(board, request_control);
429 
430 	if (retval)
431 		return retval;
432 
433 	board->master = request_control != 0;
434 
435 	return  0;
436 }
437 
438 /*
439  * IBSRE
440  * Send REN true if v is non-zero or false if v is zero.
441  */
442 int ibsre(struct gpib_board *board, int enable)
443 {
444 	if (board->master == 0)
445 		return -EINVAL;
446 
447 	board->interface->remote_enable(board, enable);	/* set or clear REN */
448 	if (!enable)
449 		usleep_range(100, 150);
450 
451 	return 0;
452 }
453 
454 /*
455  * IBPAD
456  * change the GPIB address of the interface board.  The address
457  * must be 0 through 30.  ibonl resets the address to PAD.
458  */
459 int ibpad(struct gpib_board *board, unsigned int addr)
460 {
461 	if (addr > MAX_GPIB_PRIMARY_ADDRESS)
462 		return -EINVAL;
463 
464 	board->pad = addr;
465 	if (board->online)
466 		board->interface->primary_address(board, board->pad);
467 	dev_dbg(board->gpib_dev, "set primary addr to %i\n", board->pad);
468 	return 0;
469 }
470 
471 /*
472  * IBSAD
473  * change the secondary GPIB address of the interface board.
474  * The address must be 0 through 30, or negative disables.  ibonl resets the
475  * address to SAD.
476  */
477 int ibsad(struct gpib_board *board, int addr)
478 {
479 	if (addr > MAX_GPIB_SECONDARY_ADDRESS)
480 		return -EINVAL;
481 	board->sad = addr;
482 	if (board->online) {
483 		if (board->sad >= 0)
484 			board->interface->secondary_address(board, board->sad, 1);
485 		else
486 			board->interface->secondary_address(board, 0, 0);
487 	}
488 	dev_dbg(board->gpib_dev, "set secondary addr to %i\n", board->sad);
489 
490 	return 0;
491 }
492 
493 /*
494  * IBEOS
495  * Set the end-of-string modes for I/O operations to v.
496  *
497  */
498 int ibeos(struct gpib_board *board, int eos, int eosflags)
499 {
500 	int retval;
501 
502 	if (eosflags & ~EOS_MASK)
503 		return -EINVAL;
504 	if (eosflags & REOS) {
505 		retval = board->interface->enable_eos(board, eos, eosflags & BIN);
506 	} else {
507 		board->interface->disable_eos(board);
508 		retval = 0;
509 	}
510 	return retval;
511 }
512 
513 int ibstatus(struct gpib_board *board)
514 {
515 	return general_ibstatus(board, NULL, 0, 0, NULL);
516 }
517 
518 int general_ibstatus(struct gpib_board *board, const struct gpib_status_queue *device,
519 		     int clear_mask, int set_mask, struct gpib_descriptor *desc)
520 {
521 	int status = 0;
522 	short line_status;
523 
524 	if (board->private_data) {
525 		status = board->interface->update_status(board, clear_mask);
526 		/*
527 		 * XXX should probably stop having drivers use TIMO bit in
528 		 * board->status to avoid confusion
529 		 */
530 		status &= ~TIMO;
531 		/* get real SRQI status if we can */
532 		if (iblines(board, &line_status) == 0) {
533 			if ((line_status & VALID_SRQ)) {
534 				if ((line_status & BUS_SRQ))
535 					status |= SRQI;
536 				else
537 					status &= ~SRQI;
538 			}
539 		}
540 	}
541 	if (device)
542 		if (num_status_bytes(device))
543 			status |= RQS;
544 
545 	if (desc) {
546 		if (set_mask & CMPL)
547 			atomic_set(&desc->io_in_progress, 0);
548 		else if (clear_mask & CMPL)
549 			atomic_set(&desc->io_in_progress, 1);
550 
551 		if (atomic_read(&desc->io_in_progress))
552 			status &= ~CMPL;
553 		else
554 			status |= CMPL;
555 	}
556 	if (num_gpib_events(&board->event_queue))
557 		status |= EVENT;
558 	else
559 		status &= ~EVENT;
560 
561 	return status;
562 }
563 
564 struct wait_info {
565 	struct gpib_board *board;
566 	struct timer_list timer;
567 	int timed_out;
568 	unsigned long usec_timeout;
569 };
570 
571 static void wait_timeout(struct timer_list *t)
572 {
573 	struct wait_info *winfo = timer_container_of(winfo, t, timer);
574 
575 	winfo->timed_out = 1;
576 	wake_up_interruptible(&winfo->board->wait);
577 }
578 
579 static void init_wait_info(struct wait_info *winfo)
580 {
581 	winfo->board = NULL;
582 	winfo->timed_out = 0;
583 	timer_setup_on_stack(&winfo->timer, wait_timeout, 0);
584 }
585 
586 static int wait_satisfied(struct wait_info *winfo, struct gpib_status_queue *status_queue,
587 			  int wait_mask, int *status, struct gpib_descriptor *desc)
588 {
589 	struct gpib_board *board = winfo->board;
590 	int temp_status;
591 
592 	if (mutex_lock_interruptible(&board->big_gpib_mutex))
593 		return -ERESTARTSYS;
594 
595 	temp_status = general_ibstatus(board, status_queue, 0, 0, desc);
596 
597 	mutex_unlock(&board->big_gpib_mutex);
598 
599 	if (winfo->timed_out)
600 		temp_status |= TIMO;
601 	else
602 		temp_status &= ~TIMO;
603 	if (wait_mask & temp_status) {
604 		*status = temp_status;
605 		return 1;
606 	}
607 // XXX does wait for END work?
608 	return 0;
609 }
610 
611 /* install timer interrupt handler */
612 static void start_wait_timer(struct wait_info *winfo)
613 /* Starts the timeout task  */
614 {
615 	winfo->timed_out = 0;
616 
617 	if (winfo->usec_timeout > 0)
618 		mod_timer(&winfo->timer, jiffies + usec_to_jiffies(winfo->usec_timeout));
619 }
620 
621 static void remove_wait_timer(struct wait_info *winfo)
622 {
623 	timer_delete_sync(&winfo->timer);
624 	timer_destroy_on_stack(&winfo->timer);
625 }
626 
627 /*
628  * IBWAIT
629  * Check or wait for a GPIB event to occur.  The mask argument
630  * is a bit vector corresponding to the status bit vector.  It
631  * has a bit set for each condition which can terminate the wait
632  * If the mask is 0 then
633  * no condition is waited for.
634  */
635 int ibwait(struct gpib_board *board, int wait_mask, int clear_mask, int set_mask,
636 	   int *status, unsigned long usec_timeout, struct gpib_descriptor *desc)
637 {
638 	int retval = 0;
639 	struct gpib_status_queue *status_queue;
640 	struct wait_info winfo;
641 
642 	if (desc->is_board)
643 		status_queue = NULL;
644 	else
645 		status_queue = get_gpib_status_queue(board, desc->pad, desc->sad);
646 
647 	if (wait_mask == 0) {
648 		*status = general_ibstatus(board, status_queue, clear_mask, set_mask, desc);
649 		return 0;
650 	}
651 
652 	mutex_unlock(&board->big_gpib_mutex);
653 
654 	init_wait_info(&winfo);
655 	winfo.board = board;
656 	winfo.usec_timeout = usec_timeout;
657 	start_wait_timer(&winfo);
658 
659 	if (wait_event_interruptible(board->wait, wait_satisfied(&winfo, status_queue,
660 								 wait_mask, status, desc))) {
661 		dev_dbg(board->gpib_dev, "wait interrupted\n");
662 		retval = -ERESTARTSYS;
663 	}
664 	remove_wait_timer(&winfo);
665 
666 	if (retval)
667 		return retval;
668 	if (mutex_lock_interruptible(&board->big_gpib_mutex))
669 		return -ERESTARTSYS;
670 
671 	/* make sure we only clear status bits that we are reporting */
672 	if (*status & clear_mask || set_mask)
673 		general_ibstatus(board, status_queue, *status & clear_mask, set_mask, NULL);
674 
675 	return 0;
676 }
677 
678 /*
679  * IBWRT
680  * Write cnt bytes of data from buf to the GPIB.  The write
681  * operation terminates only on I/O complete.
682  *
683  * NOTE:
684  *      1.  Prior to beginning the write, the interface is
685  *          placed in the controller standby state.
686  *      2.  Prior to calling ibwrt, the intended devices as
687  *          well as the interface board itself must be
688  *          addressed by calling ibcmd.
689  */
690 int ibwrt(struct gpib_board *board, u8 *buf, size_t cnt, int send_eoi, size_t *bytes_written)
691 {
692 	int ret = 0;
693 	int retval;
694 
695 	if (cnt == 0)
696 		return 0;
697 
698 	if (board->master) {
699 		retval = ibgts(board);
700 		if (retval < 0)
701 			return retval;
702 	}
703 	os_start_timer(board, board->usec_timeout);
704 	ret = board->interface->write(board, buf, cnt, send_eoi, bytes_written);
705 
706 	if (io_timed_out(board))
707 		ret = -ETIMEDOUT;
708 
709 	os_remove_timer(board);
710 
711 	return ret;
712 }
713 
714