1 // SPDX-License-Identifier: GPL-2.0
2
3 /***************************************************************************
4 * copyright : (C) 2001, 2002 by Frank Mori Hess
5 ***************************************************************************/
6
7 #include "ibsys.h"
8 #include <linux/delay.h>
9 #include <linux/kthread.h>
10 #include <linux/vmalloc.h>
11
12 /*
13 * IBCAC
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.
21 */
ibcac(gpib_board_t * board,int sync,int fallback_to_async)22 int ibcac(gpib_board_t *board, int sync, int fallback_to_async)
23 {
24 int status = ibstatus(board);
25 int retval;
26
27 if ((status & CIC) == 0) {
28 pr_err("gpib: not CIC during %s()\n", __func__);
29 return -1;
30 }
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(gpib_board_t * board)64 static int check_for_command_acceptors(gpib_board_t *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 & ValidATN) {
80 if ((lines & BusATN) == 0) {
81 pr_err("gpib: ATN not asserted in %s()?", __func__);
82 return 0;
83 }
84 }
85
86 if ((lines & ValidNRFD) && (lines & ValidNDAC)) {
87 if ((lines & BusNRFD) == 0 && (lines & BusNDAC) == 0)
88 return -ENOTCONN;
89 }
90
91 return 0;
92 }
93
94 /*
95 * IBCMD
96 * Write cnt command bytes from buf to the GPIB. The
97 * command operation terminates only on I/O complete.
98 *
99 * NOTE:
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.
105 */
ibcmd(gpib_board_t * board,uint8_t * buf,size_t length,size_t * bytes_written)106 int ibcmd(gpib_board_t *board, uint8_t *buf, size_t length, size_t *bytes_written)
107 {
108 ssize_t ret = 0;
109 int status;
110
111 *bytes_written = 0;
112
113 status = ibstatus(board);
114
115 if ((status & CIC) == 0) {
116 pr_err("gpib: cannot send command when not controller-in-charge\n");
117 return -EIO;
118 }
119
120 os_start_timer(board, board->usec_timeout);
121
122 ret = ibcac(board, 1, 1);
123 if (ret == 0) {
124 ret = check_for_command_acceptors(board);
125 if (ret == 0)
126 ret = board->interface->command(board, buf, length, bytes_written);
127 }
128
129 os_remove_timer(board);
130
131 if (io_timed_out(board))
132 ret = -ETIMEDOUT;
133
134 return ret;
135 }
136
137 /*
138 * IBGTS
139 * Go to the controller standby state from the controller
140 * active state, i.e., turn ATN off.
141 */
142
ibgts(gpib_board_t * board)143 int ibgts(gpib_board_t *board)
144 {
145 int status = ibstatus(board);
146 int retval;
147
148 if ((status & CIC) == 0) {
149 pr_err("gpib: not CIC during %s()\n", __func__);
150 return -1;
151 }
152
153 retval = board->interface->go_to_standby(board); /* go to standby */
154 if (retval < 0)
155 pr_err("gpib: error while going to standby\n");
156
157 board->interface->update_status(board, 0);
158
159 return retval;
160 }
161
autospoll_wait_should_wake_up(gpib_board_t * board)162 static int autospoll_wait_should_wake_up(gpib_board_t *board)
163 {
164 int retval;
165
166 mutex_lock(&board->big_gpib_mutex);
167
168 retval = board->master && board->autospollers > 0 &&
169 !atomic_read(&board->stuck_srq) &&
170 test_and_clear_bit(SRQI_NUM, &board->status);
171
172 mutex_unlock(&board->big_gpib_mutex);
173 return retval;
174 }
175
autospoll_thread(void * board_void)176 static int autospoll_thread(void *board_void)
177 {
178 gpib_board_t *board = board_void;
179 int retval = 0;
180
181 dev_dbg(board->gpib_dev, "entering autospoll thread\n");
182
183 while (1) {
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())
189 break;
190
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);
195 continue;
196 }
197 mutex_unlock(&board->big_gpib_mutex);
198
199 if (try_module_get(board->provider_module)) {
200 retval = autopoll_all_devices(board);
201 module_put(board->provider_module);
202 } else {
203 pr_err("gpib%i: %s: try_module_get() failed!\n", board->minor, __func__);
204 }
205 if (retval <= 0) {
206 pr_err("gpib%i: %s: stuck SRQ\n", board->minor, __func__);
207
208 atomic_set(&board->stuck_srq, 1); // XXX could be better
209 set_bit(SRQI_NUM, &board->status);
210 }
211 }
212 pr_info("gpib%i: exiting autospoll thread\n", board->minor);
213 return retval;
214 }
215
ibonline(gpib_board_t * board)216 int ibonline(gpib_board_t *board)
217 {
218 int retval;
219
220 if (board->online)
221 return -EBUSY;
222 if (!board->interface)
223 return -ENODEV;
224 retval = gpib_allocate_board(board);
225 if (retval < 0)
226 return retval;
227
228 board->dev = NULL;
229 board->local_ppoll_mode = 0;
230 retval = board->interface->attach(board, &board->config);
231 if (retval < 0) {
232 board->interface->detach(board);
233 pr_err("gpib: interface attach failed\n");
234 return retval;
235 }
236 /* nios2nommu on 2.6.11 uclinux kernel has weird problems
237 * with autospoll thread causing huge slowdowns
238 */
239 #ifndef CONFIG_NIOS2
240 board->autospoll_task = kthread_run(&autospoll_thread, board,
241 "gpib%d_autospoll_kthread", board->minor);
242 retval = IS_ERR(board->autospoll_task);
243 if (retval) {
244 pr_err("gpib: failed to create autospoll thread\n");
245 board->interface->detach(board);
246 return retval;
247 }
248 #endif
249 board->online = 1;
250 dev_dbg(board->gpib_dev, "gpib: board online\n");
251
252 return 0;
253 }
254
255 /* XXX need to make sure board is generally not in use (grab board lock?) */
iboffline(gpib_board_t * board)256 int iboffline(gpib_board_t *board)
257 {
258 int retval;
259
260 if (board->online == 0)
261 return 0;
262 if (!board->interface)
263 return -ENODEV;
264
265 if (board->autospoll_task && !IS_ERR(board->autospoll_task)) {
266 retval = kthread_stop(board->autospoll_task);
267 if (retval)
268 pr_err("gpib: kthread_stop returned %i\n", retval);
269 board->autospoll_task = NULL;
270 }
271
272 board->interface->detach(board);
273 gpib_deallocate_board(board);
274 board->online = 0;
275 dev_dbg(board->gpib_dev, "gpib: board offline\n");
276
277 return 0;
278 }
279
280 /*
281 * IBLINES
282 * Poll the GPIB control lines and return their status in buf.
283 *
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).
286 *
287 */
iblines(const gpib_board_t * board,short * lines)288 int iblines(const gpib_board_t *board, short *lines)
289 {
290 int retval;
291
292 *lines = 0;
293 if (!board->interface->line_status)
294 return 0;
295 retval = board->interface->line_status(board);
296 if (retval < 0)
297 return retval;
298 *lines = retval;
299 return 0;
300 }
301
302 /*
303 * IBRD
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'.
306 *
307 * NOTE:
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
312 * calling ibcmd.
313 */
314
ibrd(gpib_board_t * board,uint8_t * buf,size_t length,int * end_flag,size_t * nbytes)315 int ibrd(gpib_board_t *board, uint8_t *buf, size_t length, int *end_flag, size_t *nbytes)
316 {
317 ssize_t ret = 0;
318 int retval;
319 size_t bytes_read;
320
321 *nbytes = 0;
322 *end_flag = 0;
323 if (length == 0) {
324 pr_warn("gpib: %s() called with zero length?\n", __func__);
325 return 0;
326 }
327
328 if (board->master) {
329 retval = ibgts(board);
330 if (retval < 0)
331 return retval;
332 }
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
336 */
337 os_start_timer(board, board->usec_timeout);
338
339 do {
340 ret = board->interface->read(board, buf, length - *nbytes, end_flag, &bytes_read);
341 if (ret < 0) {
342 pr_err("gpib read error\n");
343 goto ibrd_out;
344 }
345 buf += bytes_read;
346 *nbytes += bytes_read;
347 if (need_resched())
348 schedule();
349 } while (ret == 0 && *nbytes > 0 && *nbytes < length && *end_flag == 0);
350 ibrd_out:
351 os_remove_timer(board);
352
353 return ret;
354 }
355
356 /*
357 * IBRPP
358 * Conduct a parallel poll and return the byte in buf.
359 *
360 * NOTE:
361 * 1. Prior to conducting the poll the interface is placed
362 * in the controller active state.
363 */
ibrpp(gpib_board_t * board,uint8_t * result)364 int ibrpp(gpib_board_t *board, uint8_t *result)
365 {
366 int retval = 0;
367
368 os_start_timer(board, board->usec_timeout);
369 retval = ibcac(board, 1, 1);
370 if (retval)
371 return -1;
372
373 if (board->interface->parallel_poll(board, result)) {
374 pr_err("gpib: parallel poll failed\n");
375 retval = -1;
376 }
377 os_remove_timer(board);
378 return retval;
379 }
380
ibppc(gpib_board_t * board,uint8_t configuration)381 int ibppc(gpib_board_t *board, uint8_t configuration)
382 {
383 configuration &= 0x1f;
384 board->interface->parallel_poll_configure(board, configuration);
385 board->parallel_poll_configuration = configuration;
386
387 return 0;
388 }
389
ibrsv2(gpib_board_t * board,uint8_t status_byte,int new_reason_for_service)390 int ibrsv2(gpib_board_t *board, uint8_t status_byte, int new_reason_for_service)
391 {
392 int board_status = ibstatus(board);
393 const unsigned int MSS = status_byte & request_service_bit;
394
395 if ((board_status & CIC)) {
396 pr_err("gpib: interface requested service while CIC\n");
397 return -EINVAL;
398 }
399
400 if (MSS == 0 && new_reason_for_service)
401 return -EINVAL;
402
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);
409 } else {
410 return -EOPNOTSUPP;
411 }
412
413 return 0;
414 }
415
416 /*
417 * IBSIC
418 * Send IFC for at least 100 microseconds.
419 *
420 * NOTE:
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.
424 */
ibsic(gpib_board_t * board,unsigned int usec_duration)425 int ibsic(gpib_board_t *board, unsigned int usec_duration)
426 {
427 if (board->master == 0) {
428 pr_err("gpib: tried to assert IFC when not system controller\n");
429 return -1;
430 }
431
432 if (usec_duration < 100)
433 usec_duration = 100;
434 if (usec_duration > 1000) {
435 usec_duration = 1000;
436 pr_warn("gpib: warning, shortening long udelay\n");
437 }
438
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);
443
444 return 0;
445 }
446
ibrsc(gpib_board_t * board,int request_control)447 void ibrsc(gpib_board_t *board, int request_control)
448 {
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");
452 return;
453 }
454 board->interface->request_system_control(board, request_control);
455 }
456
457 /*
458 * IBSRE
459 * Send REN true if v is non-zero or false if v is zero.
460 */
ibsre(gpib_board_t * board,int enable)461 int ibsre(gpib_board_t *board, int enable)
462 {
463 if (board->master == 0) {
464 pr_err("gpib: tried to set REN when not system controller\n");
465 return -1;
466 }
467
468 board->interface->remote_enable(board, enable); /* set or clear REN */
469 if (!enable)
470 usleep_range(100, 150);
471
472 return 0;
473 }
474
475 /*
476 * IBPAD
477 * change the GPIB address of the interface board. The address
478 * must be 0 through 30. ibonl resets the address to PAD.
479 */
ibpad(gpib_board_t * board,unsigned int addr)480 int ibpad(gpib_board_t *board, unsigned int addr)
481 {
482 if (addr > MAX_GPIB_PRIMARY_ADDRESS) {
483 pr_err("gpib: invalid primary address %u\n", addr);
484 return -1;
485 }
486 board->pad = addr;
487 if (board->online)
488 board->interface->primary_address(board, board->pad);
489 dev_dbg(board->gpib_dev, "set primary addr to %i\n", board->pad);
490 return 0;
491 }
492
493 /*
494 * IBSAD
495 * change the secondary GPIB address of the interface board.
496 * The address must be 0 through 30, or negative disables. ibonl resets the
497 * address to SAD.
498 */
ibsad(gpib_board_t * board,int addr)499 int ibsad(gpib_board_t *board, int addr)
500 {
501 if (addr > MAX_GPIB_SECONDARY_ADDRESS) {
502 pr_err("gpib: invalid secondary address %i\n", addr);
503 return -1;
504 }
505 board->sad = addr;
506 if (board->online) {
507 if (board->sad >= 0)
508 board->interface->secondary_address(board, board->sad, 1);
509 else
510 board->interface->secondary_address(board, 0, 0);
511 }
512 dev_dbg(board->gpib_dev, "set secondary addr to %i\n", board->sad);
513
514 return 0;
515 }
516
517 /*
518 * IBEOS
519 * Set the end-of-string modes for I/O operations to v.
520 *
521 */
ibeos(gpib_board_t * board,int eos,int eosflags)522 int ibeos(gpib_board_t *board, int eos, int eosflags)
523 {
524 int retval;
525
526 if (eosflags & ~EOS_MASK) {
527 pr_err("bad EOS modes\n");
528 return -EINVAL;
529 }
530 if (eosflags & REOS) {
531 retval = board->interface->enable_eos(board, eos, eosflags & BIN);
532 } else {
533 board->interface->disable_eos(board);
534 retval = 0;
535 }
536 return retval;
537 }
538
ibstatus(gpib_board_t * board)539 int ibstatus(gpib_board_t *board)
540 {
541 return general_ibstatus(board, NULL, 0, 0, NULL);
542 }
543
general_ibstatus(gpib_board_t * board,const gpib_status_queue_t * device,int clear_mask,int set_mask,gpib_descriptor_t * desc)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)
546 {
547 int status = 0;
548 short line_status;
549
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
554 */
555 status &= ~TIMO;
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))
560 status |= SRQI;
561 else
562 status &= ~SRQI;
563 }
564 }
565 }
566 if (device)
567 if (num_status_bytes(device))
568 status |= RQS;
569
570 if (desc) {
571 if (set_mask & CMPL)
572 atomic_set(&desc->io_in_progress, 0);
573 else if (clear_mask & CMPL)
574 atomic_set(&desc->io_in_progress, 1);
575
576 if (atomic_read(&desc->io_in_progress))
577 status &= ~CMPL;
578 else
579 status |= CMPL;
580 }
581 if (num_gpib_events(&board->event_queue))
582 status |= EVENT;
583 else
584 status &= ~EVENT;
585
586 return status;
587 }
588
589 struct wait_info {
590 gpib_board_t *board;
591 struct timer_list timer;
592 int timed_out;
593 unsigned long usec_timeout;
594 };
595
wait_timeout(struct timer_list * t)596 static void wait_timeout(struct timer_list *t)
597 {
598 struct wait_info *winfo = from_timer(winfo, t, timer);
599
600 winfo->timed_out = 1;
601 wake_up_interruptible(&winfo->board->wait);
602 }
603
init_wait_info(struct wait_info * winfo)604 static void init_wait_info(struct wait_info *winfo)
605 {
606 winfo->board = NULL;
607 winfo->timed_out = 0;
608 timer_setup_on_stack(&winfo->timer, wait_timeout, 0);
609 }
610
wait_satisfied(struct wait_info * winfo,gpib_status_queue_t * status_queue,int wait_mask,int * status,gpib_descriptor_t * desc)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)
613 {
614 gpib_board_t *board = winfo->board;
615 int temp_status;
616
617 if (mutex_lock_interruptible(&board->big_gpib_mutex))
618 return -ERESTARTSYS;
619
620 temp_status = general_ibstatus(board, status_queue, 0, 0, desc);
621
622 mutex_unlock(&board->big_gpib_mutex);
623
624 if (winfo->timed_out)
625 temp_status |= TIMO;
626 else
627 temp_status &= ~TIMO;
628 if (wait_mask & temp_status) {
629 *status = temp_status;
630 return 1;
631 }
632 //XXX does wait for END work?
633 return 0;
634 }
635
636 /* install timer interrupt handler */
start_wait_timer(struct wait_info * winfo)637 static void start_wait_timer(struct wait_info *winfo)
638 /* Starts the timeout task */
639 {
640 winfo->timed_out = 0;
641
642 if (winfo->usec_timeout > 0)
643 mod_timer(&winfo->timer, jiffies + usec_to_jiffies(winfo->usec_timeout));
644 }
645
remove_wait_timer(struct wait_info * winfo)646 static void remove_wait_timer(struct wait_info *winfo)
647 {
648 del_timer_sync(&winfo->timer);
649 destroy_timer_on_stack(&winfo->timer);
650 }
651
652 /*
653 * IBWAIT
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.
659 */
ibwait(gpib_board_t * board,int wait_mask,int clear_mask,int set_mask,int * status,unsigned long usec_timeout,gpib_descriptor_t * desc)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)
662 {
663 int retval = 0;
664 gpib_status_queue_t *status_queue;
665 struct wait_info winfo;
666
667 if (desc->is_board)
668 status_queue = NULL;
669 else
670 status_queue = get_gpib_status_queue(board, desc->pad, desc->sad);
671
672 if (wait_mask == 0) {
673 *status = general_ibstatus(board, status_queue, clear_mask, set_mask, desc);
674 return 0;
675 }
676
677 mutex_unlock(&board->big_gpib_mutex);
678
679 init_wait_info(&winfo);
680 winfo.board = board;
681 winfo.usec_timeout = usec_timeout;
682 start_wait_timer(&winfo);
683
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;
688 }
689 remove_wait_timer(&winfo);
690
691 if (retval)
692 return retval;
693 if (mutex_lock_interruptible(&board->big_gpib_mutex))
694 return -ERESTARTSYS;
695
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, NULL);
699
700 return 0;
701 }
702
703 /*
704 * IBWRT
705 * Write cnt bytes of data from buf to the GPIB. The write
706 * operation terminates only on I/O complete.
707 *
708 * NOTE:
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.
714 */
ibwrt(gpib_board_t * board,uint8_t * buf,size_t cnt,int send_eoi,size_t * bytes_written)715 int ibwrt(gpib_board_t *board, uint8_t *buf, size_t cnt, int send_eoi, size_t *bytes_written)
716 {
717 int ret = 0;
718 int retval;
719
720 if (cnt == 0) {
721 pr_warn("gpib: %s() called with zero length?\n", __func__);
722 return 0;
723 }
724
725 if (board->master) {
726 retval = ibgts(board);
727 if (retval < 0)
728 return retval;
729 }
730 os_start_timer(board, board->usec_timeout);
731 ret = board->interface->write(board, buf, cnt, send_eoi, bytes_written);
732
733 if (io_timed_out(board))
734 ret = -ETIMEDOUT;
735
736 os_remove_timer(board);
737
738 return ret;
739 }
740
741