Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/gpib/common/iblib.c
122915 views
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
atomic_set(&board->stuck_srq, 1); // XXX could be better
198
set_bit(SRQI_NUM, &board->status);
199
}
200
}
201
return retval;
202
}
203
204
int ibonline(struct gpib_board *board)
205
{
206
int retval;
207
208
if (board->online)
209
return -EBUSY;
210
if (!board->interface)
211
return -ENODEV;
212
retval = gpib_allocate_board(board);
213
if (retval < 0)
214
return retval;
215
216
board->dev = NULL;
217
board->local_ppoll_mode = 0;
218
retval = board->interface->attach(board, &board->config);
219
if (retval < 0) {
220
board->interface->detach(board);
221
return retval;
222
}
223
/*
224
* nios2nommu on 2.6.11 uclinux kernel has weird problems
225
* with autospoll thread causing huge slowdowns
226
*/
227
#ifndef CONFIG_NIOS2
228
board->autospoll_task = kthread_run(&autospoll_thread, board,
229
"gpib%d_autospoll_kthread", board->minor);
230
if (IS_ERR(board->autospoll_task)) {
231
dev_err(board->gpib_dev, "failed to create autospoll thread\n");
232
board->interface->detach(board);
233
return PTR_ERR(board->autospoll_task);
234
}
235
#endif
236
board->online = 1;
237
dev_dbg(board->gpib_dev, "board online\n");
238
239
return 0;
240
}
241
242
/* XXX need to make sure board is generally not in use (grab board lock?) */
243
int iboffline(struct gpib_board *board)
244
{
245
int retval;
246
247
if (board->online == 0)
248
return 0;
249
if (!board->interface)
250
return -ENODEV;
251
252
if (board->autospoll_task && !IS_ERR(board->autospoll_task)) {
253
retval = kthread_stop(board->autospoll_task);
254
if (retval)
255
dev_err(board->gpib_dev, "kthread_stop returned %i\n", retval);
256
board->autospoll_task = NULL;
257
}
258
259
board->interface->detach(board);
260
gpib_deallocate_board(board);
261
board->online = 0;
262
dev_dbg(board->gpib_dev, "board offline\n");
263
264
return 0;
265
}
266
267
/*
268
* IBLINES
269
* Poll the GPIB control lines and return their status in buf.
270
*
271
* LSB (bits 0-7) - VALID lines mask (lines that can be monitored).
272
* Next LSB (bits 8-15) - STATUS lines mask (lines that are currently set).
273
*
274
*/
275
int iblines(const struct gpib_board *board, short *lines)
276
{
277
int retval;
278
279
*lines = 0;
280
if (!board->interface->line_status)
281
return 0;
282
retval = board->interface->line_status(board);
283
if (retval < 0)
284
return retval;
285
*lines = retval;
286
return 0;
287
}
288
289
/*
290
* IBRD
291
* Read up to 'length' bytes of data from the GPIB into buf. End
292
* on detection of END (EOI and or EOS) and set 'end_flag'.
293
*
294
* NOTE:
295
* 1. The interface is placed in the controller standby
296
* state prior to beginning the read.
297
* 2. Prior to calling ibrd, the intended devices as well
298
* as the interface board itself must be addressed by
299
* calling ibcmd.
300
*/
301
302
int ibrd(struct gpib_board *board, u8 *buf, size_t length, int *end_flag, size_t *nbytes)
303
{
304
ssize_t ret = 0;
305
int retval;
306
size_t bytes_read;
307
308
*nbytes = 0;
309
*end_flag = 0;
310
if (length == 0)
311
return 0;
312
313
if (board->master) {
314
retval = ibgts(board);
315
if (retval < 0)
316
return retval;
317
}
318
/*
319
* XXX resetting timer here could cause timeouts take longer than they should,
320
* since read_ioctl calls this
321
* function in a loop, there is probably a similar problem with writes/commands
322
*/
323
os_start_timer(board, board->usec_timeout);
324
325
do {
326
ret = board->interface->read(board, buf, length - *nbytes, end_flag, &bytes_read);
327
if (ret < 0)
328
goto ibrd_out;
329
330
buf += bytes_read;
331
*nbytes += bytes_read;
332
if (need_resched())
333
schedule();
334
} while (ret == 0 && *nbytes > 0 && *nbytes < length && *end_flag == 0);
335
ibrd_out:
336
os_remove_timer(board);
337
338
return ret;
339
}
340
341
/*
342
* IBRPP
343
* Conduct a parallel poll and return the byte in buf.
344
*
345
* NOTE:
346
* 1. Prior to conducting the poll the interface is placed
347
* in the controller active state.
348
*/
349
int ibrpp(struct gpib_board *board, u8 *result)
350
{
351
int retval = 0;
352
353
os_start_timer(board, board->usec_timeout);
354
retval = ibcac(board, 1, 1);
355
if (retval)
356
return -1;
357
358
retval = board->interface->parallel_poll(board, result);
359
360
os_remove_timer(board);
361
return retval;
362
}
363
364
int ibppc(struct gpib_board *board, u8 configuration)
365
{
366
configuration &= 0x1f;
367
board->interface->parallel_poll_configure(board, configuration);
368
board->parallel_poll_configuration = configuration;
369
370
return 0;
371
}
372
373
int ibrsv2(struct gpib_board *board, u8 status_byte, int new_reason_for_service)
374
{
375
int board_status = ibstatus(board);
376
const unsigned int MSS = status_byte & request_service_bit;
377
378
if ((board_status & CIC))
379
return -EINVAL;
380
381
if (MSS == 0 && new_reason_for_service)
382
return -EINVAL;
383
384
if (board->interface->serial_poll_response2) {
385
board->interface->serial_poll_response2(board, status_byte, new_reason_for_service);
386
// fall back on simpler serial_poll_response if the behavior would be the same
387
} else if (board->interface->serial_poll_response &&
388
(MSS == 0 || (MSS && new_reason_for_service))) {
389
board->interface->serial_poll_response(board, status_byte);
390
} else {
391
return -EOPNOTSUPP;
392
}
393
394
return 0;
395
}
396
397
/*
398
* IBSIC
399
* Send IFC for at least 100 microseconds.
400
*
401
* NOTE:
402
* 1. Ibsic must be called prior to the first call to
403
* ibcmd in order to initialize the bus and enable the
404
* interface to leave the controller idle state.
405
*/
406
int ibsic(struct gpib_board *board, unsigned int usec_duration)
407
{
408
if (board->master == 0)
409
return -EINVAL;
410
411
if (usec_duration < 100)
412
usec_duration = 100;
413
if (usec_duration > 1000)
414
usec_duration = 1000;
415
416
dev_dbg(board->gpib_dev, "sending interface clear, delay = %ius\n", usec_duration);
417
board->interface->interface_clear(board, 1);
418
udelay(usec_duration);
419
board->interface->interface_clear(board, 0);
420
421
return 0;
422
}
423
424
int ibrsc(struct gpib_board *board, int request_control)
425
{
426
int retval;
427
428
if (!board->interface->request_system_control)
429
return -EPERM;
430
431
retval = board->interface->request_system_control(board, request_control);
432
433
if (retval)
434
return retval;
435
436
board->master = request_control != 0;
437
438
return 0;
439
}
440
441
/*
442
* IBSRE
443
* Send REN true if v is non-zero or false if v is zero.
444
*/
445
int ibsre(struct gpib_board *board, int enable)
446
{
447
if (board->master == 0)
448
return -EINVAL;
449
450
board->interface->remote_enable(board, enable); /* set or clear REN */
451
if (!enable)
452
usleep_range(100, 150);
453
454
return 0;
455
}
456
457
/*
458
* IBPAD
459
* change the GPIB address of the interface board. The address
460
* must be 0 through 30. ibonl resets the address to PAD.
461
*/
462
int ibpad(struct gpib_board *board, unsigned int addr)
463
{
464
if (addr > MAX_GPIB_PRIMARY_ADDRESS)
465
return -EINVAL;
466
467
board->pad = addr;
468
if (board->online)
469
board->interface->primary_address(board, board->pad);
470
dev_dbg(board->gpib_dev, "set primary addr to %i\n", board->pad);
471
return 0;
472
}
473
474
/*
475
* IBSAD
476
* change the secondary GPIB address of the interface board.
477
* The address must be 0 through 30, or negative disables. ibonl resets the
478
* address to SAD.
479
*/
480
int ibsad(struct gpib_board *board, int addr)
481
{
482
if (addr > MAX_GPIB_SECONDARY_ADDRESS)
483
return -EINVAL;
484
board->sad = addr;
485
if (board->online) {
486
if (board->sad >= 0)
487
board->interface->secondary_address(board, board->sad, 1);
488
else
489
board->interface->secondary_address(board, 0, 0);
490
}
491
dev_dbg(board->gpib_dev, "set secondary addr to %i\n", board->sad);
492
493
return 0;
494
}
495
496
/*
497
* IBEOS
498
* Set the end-of-string modes for I/O operations to v.
499
*
500
*/
501
int ibeos(struct gpib_board *board, int eos, int eosflags)
502
{
503
int retval;
504
505
if (eosflags & ~EOS_MASK)
506
return -EINVAL;
507
if (eosflags & REOS) {
508
retval = board->interface->enable_eos(board, eos, eosflags & BIN);
509
} else {
510
board->interface->disable_eos(board);
511
retval = 0;
512
}
513
return retval;
514
}
515
516
int ibstatus(struct gpib_board *board)
517
{
518
return general_ibstatus(board, NULL, 0, 0, NULL);
519
}
520
521
int general_ibstatus(struct gpib_board *board, const struct gpib_status_queue *device,
522
int clear_mask, int set_mask, struct gpib_descriptor *desc)
523
{
524
int status = 0;
525
short line_status;
526
527
if (board->private_data) {
528
status = board->interface->update_status(board, clear_mask);
529
/*
530
* XXX should probably stop having drivers use TIMO bit in
531
* board->status to avoid confusion
532
*/
533
status &= ~TIMO;
534
/* get real SRQI status if we can */
535
if (iblines(board, &line_status) == 0) {
536
if ((line_status & VALID_SRQ)) {
537
if ((line_status & BUS_SRQ))
538
status |= SRQI;
539
else
540
status &= ~SRQI;
541
}
542
}
543
}
544
if (device)
545
if (num_status_bytes(device))
546
status |= RQS;
547
548
if (desc) {
549
if (set_mask & CMPL)
550
atomic_set(&desc->io_in_progress, 0);
551
else if (clear_mask & CMPL)
552
atomic_set(&desc->io_in_progress, 1);
553
554
if (atomic_read(&desc->io_in_progress))
555
status &= ~CMPL;
556
else
557
status |= CMPL;
558
}
559
if (num_gpib_events(&board->event_queue))
560
status |= EVENT;
561
else
562
status &= ~EVENT;
563
564
return status;
565
}
566
567
struct wait_info {
568
struct gpib_board *board;
569
struct timer_list timer;
570
int timed_out;
571
unsigned long usec_timeout;
572
};
573
574
static void wait_timeout(struct timer_list *t)
575
{
576
struct wait_info *winfo = timer_container_of(winfo, t, timer);
577
578
winfo->timed_out = 1;
579
wake_up_interruptible(&winfo->board->wait);
580
}
581
582
static void init_wait_info(struct wait_info *winfo)
583
{
584
winfo->board = NULL;
585
winfo->timed_out = 0;
586
timer_setup_on_stack(&winfo->timer, wait_timeout, 0);
587
}
588
589
static int wait_satisfied(struct wait_info *winfo, struct gpib_status_queue *status_queue,
590
int wait_mask, int *status, struct gpib_descriptor *desc)
591
{
592
struct gpib_board *board = winfo->board;
593
int temp_status;
594
595
if (mutex_lock_interruptible(&board->big_gpib_mutex))
596
return -ERESTARTSYS;
597
598
temp_status = general_ibstatus(board, status_queue, 0, 0, desc);
599
600
mutex_unlock(&board->big_gpib_mutex);
601
602
if (winfo->timed_out)
603
temp_status |= TIMO;
604
else
605
temp_status &= ~TIMO;
606
if (wait_mask & temp_status) {
607
*status = temp_status;
608
return 1;
609
}
610
// XXX does wait for END work?
611
return 0;
612
}
613
614
/* install timer interrupt handler */
615
static void start_wait_timer(struct wait_info *winfo)
616
/* Starts the timeout task */
617
{
618
winfo->timed_out = 0;
619
620
if (winfo->usec_timeout > 0)
621
mod_timer(&winfo->timer, jiffies + usec_to_jiffies(winfo->usec_timeout));
622
}
623
624
static void remove_wait_timer(struct wait_info *winfo)
625
{
626
timer_delete_sync(&winfo->timer);
627
timer_destroy_on_stack(&winfo->timer);
628
}
629
630
/*
631
* IBWAIT
632
* Check or wait for a GPIB event to occur. The mask argument
633
* is a bit vector corresponding to the status bit vector. It
634
* has a bit set for each condition which can terminate the wait
635
* If the mask is 0 then
636
* no condition is waited for.
637
*/
638
int ibwait(struct gpib_board *board, int wait_mask, int clear_mask, int set_mask,
639
int *status, unsigned long usec_timeout, struct gpib_descriptor *desc)
640
{
641
int retval = 0;
642
struct gpib_status_queue *status_queue;
643
struct wait_info winfo;
644
645
if (desc->is_board)
646
status_queue = NULL;
647
else
648
status_queue = get_gpib_status_queue(board, desc->pad, desc->sad);
649
650
if (wait_mask == 0) {
651
*status = general_ibstatus(board, status_queue, clear_mask, set_mask, desc);
652
return 0;
653
}
654
655
mutex_unlock(&board->big_gpib_mutex);
656
657
init_wait_info(&winfo);
658
winfo.board = board;
659
winfo.usec_timeout = usec_timeout;
660
start_wait_timer(&winfo);
661
662
if (wait_event_interruptible(board->wait, wait_satisfied(&winfo, status_queue,
663
wait_mask, status, desc))) {
664
dev_dbg(board->gpib_dev, "wait interrupted\n");
665
retval = -ERESTARTSYS;
666
}
667
remove_wait_timer(&winfo);
668
669
if (retval)
670
return retval;
671
if (mutex_lock_interruptible(&board->big_gpib_mutex))
672
return -ERESTARTSYS;
673
674
/* make sure we only clear status bits that we are reporting */
675
if (*status & clear_mask || set_mask)
676
general_ibstatus(board, status_queue, *status & clear_mask, set_mask, NULL);
677
678
return 0;
679
}
680
681
/*
682
* IBWRT
683
* Write cnt bytes of data from buf to the GPIB. The write
684
* operation terminates only on I/O complete.
685
*
686
* NOTE:
687
* 1. Prior to beginning the write, the interface is
688
* placed in the controller standby state.
689
* 2. Prior to calling ibwrt, the intended devices as
690
* well as the interface board itself must be
691
* addressed by calling ibcmd.
692
*/
693
int ibwrt(struct gpib_board *board, u8 *buf, size_t cnt, int send_eoi, size_t *bytes_written)
694
{
695
int ret = 0;
696
int retval;
697
698
if (cnt == 0)
699
return 0;
700
701
if (board->master) {
702
retval = ibgts(board);
703
if (retval < 0)
704
return retval;
705
}
706
os_start_timer(board, board->usec_timeout);
707
ret = board->interface->write(board, buf, cnt, send_eoi, bytes_written);
708
709
if (io_timed_out(board))
710
ret = -ETIMEDOUT;
711
712
os_remove_timer(board);
713
714
return ret;
715
}
716
717
718