1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
|
// SPDX-License-Identifier: GPL-2.0
/*************************************************************************
* This code has been developed at the Institute of Sensor and Actuator *
* Systems (Technical University of Vienna, Austria) to enable the GPIO *
* lines (e.g. of a raspberry pi) to function as a GPIO master device *
* *
* authors : Thomas Klima *
* Marcello Carla' *
* Dave Penkler *
* *
* copyright : (C) 2016 Thomas Klima *
* *
*************************************************************************/
/*
* limitations:
* works only on RPi
* cannot function as non-CIC system controller with SN7516x because
* SN75161B cannot simultaneously make ATN input with IFC and REN as
* outputs.
* not implemented:
* parallel poll
* return2local
* device support (non master operation)
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define dev_fmt pr_fmt
#define NAME KBUILD_MODNAME
#define ENABLE_IRQ(IRQ, TYPE) irq_set_irq_type(IRQ, TYPE)
#define DISABLE_IRQ(IRQ) irq_set_irq_type(IRQ, IRQ_TYPE_NONE)
/*
* Debug print levels:
* 0 = load/unload info and errors that make the driver fail;
* 1 = + warnings for unforeseen events that may break the current
* operation and lead to a timeout, but do not affect the
* driver integrity (mainly unexpected interrupts);
* 2 = + trace of function calls;
* 3 = + trace of protocol codes;
* 4 = + trace of interrupt operation.
*/
#define dbg_printk(level, frm, ...) \
do { if (debug >= (level)) \
dev_dbg(board->gpib_dev, frm, ## __VA_ARGS__); } \
while (0)
#define LINVAL gpiod_get_value(DAV), \
gpiod_get_value(NRFD), \
gpiod_get_value(NDAC), \
gpiod_get_value(SRQ)
#define LINFMT "DAV: %d NRFD:%d NDAC: %d SRQ: %d"
#include "gpibP.h"
#include "gpib_state_machines.h"
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio/driver.h>
#include <linux/gpio/machine.h>
#include <linux/gpio.h>
#include <linux/irq.h>
static int sn7516x_used = 1, sn7516x;
module_param(sn7516x_used, int, 0660);
#define PINMAP_0 "elektronomikon"
#define PINMAP_1 "gpib4pi-1.1"
#define PINMAP_2 "yoga"
static char *pin_map = PINMAP_0;
module_param(pin_map, charp, 0660);
MODULE_PARM_DESC(pin_map, " valid values: " PINMAP_0 " " PINMAP_1 " " PINMAP_2);
/**********************************************
* Signal pairing and pin wiring between the *
* Raspberry-Pi connector and the GPIB bus *
* *
* signal pin wiring *
* GPIB Pi-gpio GPIB -> RPi *
**********************************************
*/
enum lines_t {
D01_pin_nr = 20, /* 1 -> 38 */
D02_pin_nr = 26, /* 2 -> 37 */
D03_pin_nr = 16, /* 3 -> 36 */
D04_pin_nr = 19, /* 4 -> 35 */
D05_pin_nr = 13, /* 13 -> 33 */
D06_pin_nr = 12, /* 14 -> 32 */
D07_pin_nr = 6, /* 15 -> 31 */
D08_pin_nr = 5, /* 16 -> 29 */
EOI_pin_nr = 9, /* 5 -> 21 */
DAV_pin_nr = 10, /* 6 -> 19 */
NRFD_pin_nr = 24, /* 7 -> 18 */
NDAC_pin_nr = 23, /* 8 -> 16 */
IFC_pin_nr = 22, /* 9 -> 15 */
SRQ_pin_nr = 11, /* 10 -> 23 */
_ATN_pin_nr = 25, /* 11 -> 22 */
REN_pin_nr = 27, /* 17 -> 13 */
/*
* GROUND PINS
* 12,18,19,20,21,22,23,24 => 14,20,25,30,34,39
*/
/*
* These lines are used to control the external
* SN75160/161 driver chips when used.
* When not used there is reduced fan out;
* currently tested with up to 4 devices.
*/
/* Pi GPIO RPI 75161B 75160B Description */
PE_pin_nr = 7, /* 26 -> nc 11 Pullup Enable */
DC_pin_nr = 8, /* 24 -> 12 nc Direction control */
TE_pin_nr = 18, /* 12 -> 2 1 Talk Enable */
ACT_LED_pin_nr = 4, /* 7 -> LED */
/* YOGA adapter uses different pinout to ease layout */
YOGA_D03_pin_nr = 13,
YOGA_D04_pin_nr = 12,
YOGA_D05_pin_nr = 21,
YOGA_D06_pin_nr = 19,
};
/*
* GPIO descriptors and pins - WARNING: STRICTLY KEEP ITEMS ORDER
*/
#define GPIB_PINS 16
#define SN7516X_PINS 4
#define NUM_PINS (GPIB_PINS + SN7516X_PINS)
#define ACT_LED_ON do { \
if (ACT_LED) \
gpiod_direction_output(ACT_LED, 1); \
} while (0)
#define ACT_LED_OFF do { \
if (ACT_LED) \
gpiod_direction_output(ACT_LED, 0); \
} while (0)
static struct gpio_desc *all_descriptors[GPIB_PINS + SN7516X_PINS];
#define D01 all_descriptors[0]
#define D02 all_descriptors[1]
#define D03 all_descriptors[2]
#define D04 all_descriptors[3]
#define D05 all_descriptors[4]
#define D06 all_descriptors[5]
#define D07 all_descriptors[6]
#define D08 all_descriptors[7]
#define EOI all_descriptors[8]
#define NRFD all_descriptors[9]
#define IFC all_descriptors[10]
#define _ATN all_descriptors[11]
#define REN all_descriptors[12]
#define DAV all_descriptors[13]
#define NDAC all_descriptors[14]
#define SRQ all_descriptors[15]
#define PE all_descriptors[16]
#define DC all_descriptors[17]
#define TE all_descriptors[18]
#define ACT_LED all_descriptors[19]
/* YOGA dapter uses a global enable for the buffer chips, re-using the TE pin */
#define YOGA_ENABLE TE
static int gpios_vector[] = {
D01_pin_nr,
D02_pin_nr,
D03_pin_nr,
D04_pin_nr,
D05_pin_nr,
D06_pin_nr,
D07_pin_nr,
D08_pin_nr,
EOI_pin_nr,
NRFD_pin_nr,
IFC_pin_nr,
_ATN_pin_nr,
REN_pin_nr,
DAV_pin_nr,
NDAC_pin_nr,
SRQ_pin_nr,
PE_pin_nr,
DC_pin_nr,
TE_pin_nr,
ACT_LED_pin_nr
};
/* Lookup table for general GPIOs */
static struct gpiod_lookup_table gpib_gpio_table_1 = {
// for bcm2835/6
.dev_id = "", // device id of board device
.table = {
GPIO_LOOKUP_IDX("GPIO_GCLK", U16_MAX, NULL, 4, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO5", U16_MAX, NULL, 5, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO6", U16_MAX, NULL, 6, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("SPI_CE1_N", U16_MAX, NULL, 7, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("SPI_CE0_N", U16_MAX, NULL, 8, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("SPI_MISO", U16_MAX, NULL, 9, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("SPI_MOSI", U16_MAX, NULL, 10, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("SPI_SCLK", U16_MAX, NULL, 11, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO12", U16_MAX, NULL, 12, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO13", U16_MAX, NULL, 13, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO16", U16_MAX, NULL, 16, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO17", U16_MAX, NULL, 17, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO18", U16_MAX, NULL, 18, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO19", U16_MAX, NULL, 19, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO20", U16_MAX, NULL, 20, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO21", U16_MAX, NULL, 21, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO22", U16_MAX, NULL, 22, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO23", U16_MAX, NULL, 23, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO24", U16_MAX, NULL, 24, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO25", U16_MAX, NULL, 25, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO26", U16_MAX, NULL, 26, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO27", U16_MAX, NULL, 27, GPIO_ACTIVE_HIGH),
{ }
},
};
static struct gpiod_lookup_table gpib_gpio_table_0 = {
.dev_id = "", // device id of board device
.table = {
// for bcm27xx based pis (b b+ 2b 3b 3b+ 4 5)
GPIO_LOOKUP_IDX("GPIO4", U16_MAX, NULL, 4, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO5", U16_MAX, NULL, 5, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO6", U16_MAX, NULL, 6, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO7", U16_MAX, NULL, 7, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO8", U16_MAX, NULL, 8, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO9", U16_MAX, NULL, 9, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO10", U16_MAX, NULL, 10, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO11", U16_MAX, NULL, 11, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO12", U16_MAX, NULL, 12, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO13", U16_MAX, NULL, 13, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO16", U16_MAX, NULL, 16, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO17", U16_MAX, NULL, 17, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO18", U16_MAX, NULL, 18, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO19", U16_MAX, NULL, 19, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO20", U16_MAX, NULL, 20, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO21", U16_MAX, NULL, 21, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO22", U16_MAX, NULL, 22, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO23", U16_MAX, NULL, 23, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO24", U16_MAX, NULL, 24, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO25", U16_MAX, NULL, 25, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO26", U16_MAX, NULL, 26, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("GPIO27", U16_MAX, NULL, 27, GPIO_ACTIVE_HIGH),
{ }
},
};
static struct gpiod_lookup_table *lookup_tables[] = {
&gpib_gpio_table_0,
&gpib_gpio_table_1,
NULL
};
/* struct which defines private_data for gpio driver */
struct bb_priv {
int irq_NRFD;
int irq_NDAC;
int irq_DAV;
int irq_SRQ;
int dav_mode; /* dav interrupt mode 0/1 -> edge/levels */
int nrfd_mode; /* nrfd interrupt mode 0/1 -> edge/levels */
int ndac_mode; /* nrfd interrupt mode 0/1 -> edge/levels */
int dav_tx; /* keep trace of DAV status while sending */
int dav_rx; /* keep trace of DAV status while receiving */
u8 eos; // eos character
short eos_flags; // eos mode
short eos_check; /* eos check required in current operation ... */
short eos_check_8; /* ... with byte comparison */
short eos_mask_7; /* ... with 7 bit masked character */
short int end;
int request;
int count;
int direction;
int t1_delay;
u8 *rbuf;
u8 *wbuf;
int end_flag;
int r_busy; /* 0==idle 1==busy */
int w_busy;
int write_done;
int cmd; /* 1 = cmd write in progress */
size_t w_cnt;
size_t length;
u8 *w_buf;
spinlock_t rw_lock; // protect mods to rw_lock
int phase;
int ndac_idle;
int ndac_seq;
int nrfd_idle;
int nrfd_seq;
int dav_seq;
long all_irqs;
int dav_idle;
enum talker_function_state talker_state;
enum listener_function_state listener_state;
};
static inline long usec_diff(struct timespec64 *a, struct timespec64 *b);
static void bb_buffer_print(struct gpib_board *board, unsigned char *buffer, size_t length,
int cmd, int eoi);
static void set_data_lines(u8 byte);
static u8 get_data_lines(void);
static void set_data_lines_input(void);
static void set_data_lines_output(void);
static inline int check_for_eos(struct bb_priv *priv, u8 byte);
static void set_atn(struct gpib_board *board, int atn_asserted);
static inline void SET_DIR_WRITE(struct bb_priv *priv);
static inline void SET_DIR_READ(struct bb_priv *priv);
#define DIR_READ 0
#define DIR_WRITE 1
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("GPIB helper functions for bitbanging I/O");
/**** global variables ****/
static int debug;
module_param(debug, int, 0644);
static char printable(char x)
{
if (x < 32 || x > 126)
return ' ';
return x;
}
/***************************************************************************
* *
* READ *
* *
***************************************************************************/
static int bb_read(struct gpib_board *board, u8 *buffer, size_t length,
int *end, size_t *bytes_read)
{
struct bb_priv *priv = board->private_data;
unsigned long flags;
int retval = 0;
ACT_LED_ON;
SET_DIR_READ(priv);
dbg_printk(2, "board: %p lock %d length: %zu\n",
board, mutex_is_locked(&board->user_mutex), length);
priv->end = 0;
priv->count = 0;
priv->rbuf = buffer;
if (length == 0)
goto read_end;
priv->request = length;
priv->eos_check = (priv->eos_flags & REOS) == 0; /* do eos check */
priv->eos_check_8 = priv->eos_flags & BIN; /* over 8 bits */
priv->eos_mask_7 = priv->eos & 0x7f; /* with this 7 bit eos */
dbg_printk(3, ".........." LINFMT "\n", LINVAL);
spin_lock_irqsave(&priv->rw_lock, flags);
priv->dav_mode = 1;
priv->dav_rx = 1;
ENABLE_IRQ(priv->irq_DAV, IRQ_TYPE_LEVEL_LOW);
priv->end_flag = 0;
gpiod_set_value(NRFD, 1); // ready for data
priv->r_busy = 1;
priv->phase = 100;
spin_unlock_irqrestore(&priv->rw_lock, flags);
/* wait for the interrupt routines finish their work */
retval = wait_event_interruptible(board->wait,
(priv->end_flag || board->status & TIMO));
dbg_printk(3, "awake from wait queue: %d\n", retval);
if (retval == 0 && board->status & TIMO) {
retval = -ETIMEDOUT;
dbg_printk(1, "timeout\n");
} else if (retval) {
retval = -ERESTARTSYS;
}
DISABLE_IRQ(priv->irq_DAV);
spin_lock_irqsave(&priv->rw_lock, flags);
gpiod_set_value(NRFD, 0); // DIR_READ line state
priv->r_busy = 0;
spin_unlock_irqrestore(&priv->rw_lock, flags);
read_end:
ACT_LED_OFF;
*bytes_read = priv->count;
*end = priv->end;
priv->r_busy = 0;
dbg_printk(2, "return: %d eoi|eos: %d count: %d\n\n", retval, priv->end, priv->count);
return retval;
}
/***************************************************************************
* *
* READ interrupt routine (DAV line) *
* *
***************************************************************************/
static irqreturn_t bb_DAV_interrupt(int irq, void *arg)
{
struct gpib_board *board = arg;
struct bb_priv *priv = board->private_data;
int val;
unsigned long flags;
spin_lock_irqsave(&priv->rw_lock, flags);
priv->all_irqs++;
if (priv->dav_mode) {
ENABLE_IRQ(priv->irq_DAV, IRQ_TYPE_EDGE_BOTH);
priv->dav_mode = 0;
}
if (priv->r_busy == 0) {
dbg_printk(1, "interrupt while idle after %d at %d\n",
priv->count, priv->phase);
priv->dav_idle++;
priv->phase = 200;
goto dav_exit; /* idle */
}
val = gpiod_get_value(DAV);
if (val == priv->dav_rx) {
dbg_printk(1, "out of order DAV interrupt %d/%d after %zu/%zu at %d cmd %d "
LINFMT ".\n", val, priv->dav_rx, priv->w_cnt, priv->length,
priv->phase, priv->cmd, LINVAL);
priv->dav_seq++;
}
priv->dav_rx = val;
dbg_printk(3, "> irq: %d DAV: %d st: %4lx dir: %d busy: %d:%d\n",
irq, val, board->status, priv->direction, priv->r_busy, priv->w_busy);
if (val == 0) {
gpiod_set_value(NRFD, 0); // not ready for data
priv->rbuf[priv->count++] = get_data_lines();
priv->end = !gpiod_get_value(EOI);
gpiod_set_value(NDAC, 1); // data accepted
priv->end |= check_for_eos(priv, priv->rbuf[priv->count - 1]);
priv->end_flag = ((priv->count >= priv->request) || priv->end);
priv->phase = 210;
} else {
gpiod_set_value(NDAC, 0); // data not accepted
if (priv->end_flag) {
priv->r_busy = 0;
wake_up_interruptible(&board->wait);
priv->phase = 220;
} else {
gpiod_set_value(NRFD, 1); // ready for data
priv->phase = 230;
}
}
dav_exit:
spin_unlock_irqrestore(&priv->rw_lock, flags);
dbg_printk(3, "< irq: %d count %d\n", irq, priv->count);
return IRQ_HANDLED;
}
/***************************************************************************
* *
* WRITE *
* *
***************************************************************************/
static int bb_write(struct gpib_board *board, u8 *buffer, size_t length,
int send_eoi, size_t *bytes_written)
{
unsigned long flags;
int retval = 0;
struct bb_priv *priv = board->private_data;
ACT_LED_ON;
priv->w_cnt = 0;
priv->w_buf = buffer;
dbg_printk(2, "board %p lock %d length: %zu\n",
board, mutex_is_locked(&board->user_mutex), length);
if (debug > 1)
bb_buffer_print(board, buffer, length, priv->cmd, send_eoi);
priv->count = 0;
priv->phase = 300;
if (length == 0)
goto write_end;
priv->end = send_eoi;
priv->length = length;
SET_DIR_WRITE(priv);
dbg_printk(2, "Enabling interrupts - NRFD: %d NDAC: %d\n",
gpiod_get_value(NRFD), gpiod_get_value(NDAC));
if (gpiod_get_value(NRFD) && gpiod_get_value(NDAC)) { /* check for listener */
retval = -ENOTCONN;
goto write_end;
}
spin_lock_irqsave(&priv->rw_lock, flags);
priv->w_busy = 1; /* make the interrupt routines active */
priv->write_done = 0;
priv->nrfd_mode = 1;
priv->ndac_mode = 1;
priv->dav_tx = 1;
ENABLE_IRQ(priv->irq_NDAC, IRQ_TYPE_LEVEL_HIGH);
ENABLE_IRQ(priv->irq_NRFD, IRQ_TYPE_LEVEL_HIGH);
spin_unlock_irqrestore(&priv->rw_lock, flags);
/* wait for the interrupt routines finish their work */
retval = wait_event_interruptible(board->wait,
priv->write_done || (board->status & TIMO));
dbg_printk(3, "awake from wait queue: %d\n", retval);
if (retval == 0) {
if (board->status & TIMO) {
retval = -ETIMEDOUT;
dbg_printk(1, "timeout after %zu/%zu at %d " LINFMT " eoi: %d\n",
priv->w_cnt, length, priv->phase, LINVAL, send_eoi);
} else {
retval = priv->w_cnt;
}
} else {
retval = -ERESTARTSYS;
}
DISABLE_IRQ(priv->irq_NRFD);
DISABLE_IRQ(priv->irq_NDAC);
spin_lock_irqsave(&priv->rw_lock, flags);
priv->w_busy = 0;
gpiod_set_value(DAV, 1); // DIR_WRITE line state
gpiod_set_value(EOI, 1); // De-assert EOI (in case)
spin_unlock_irqrestore(&priv->rw_lock, flags);
write_end:
*bytes_written = priv->w_cnt;
ACT_LED_OFF;
dbg_printk(2, "sent %zu bytes\r\n\r\n", *bytes_written);
priv->phase = 310;
return retval;
}
/***************************************************************************
* *
* WRITE interrupt routine (NRFD line) *
* *
***************************************************************************/
static irqreturn_t bb_NRFD_interrupt(int irq, void *arg)
{
struct gpib_board *board = arg;
struct bb_priv *priv = board->private_data;
unsigned long flags;
int nrfd;
spin_lock_irqsave(&priv->rw_lock, flags);
nrfd = gpiod_get_value(NRFD);
priv->all_irqs++;
dbg_printk(3, "> irq: %d NRFD: %d NDAC: %d st: %4lx dir: %d busy: %d:%d\n",
irq, nrfd, gpiod_get_value(NDAC), board->status, priv->direction,
priv->w_busy, priv->r_busy);
if (priv->nrfd_mode) {
ENABLE_IRQ(priv->irq_NRFD, IRQ_TYPE_EDGE_RISING);
priv->nrfd_mode = 0;
}
if (priv->w_busy == 0) {
dbg_printk(1, "interrupt while idle after %zu/%zu at %d\n",
priv->w_cnt, priv->length, priv->phase);
priv->nrfd_idle++;
goto nrfd_exit; /* idle */
}
if (nrfd == 0) {
dbg_printk(1, "out of order interrupt after %zu/%zu at %d cmd %d " LINFMT ".\n",
priv->w_cnt, priv->length, priv->phase, priv->cmd, LINVAL);
priv->phase = 400;
priv->nrfd_seq++;
goto nrfd_exit;
}
if (!priv->dav_tx) {
dbg_printk(1, "DAV low after %zu/%zu cmd %d " LINFMT ". No action.\n",
priv->w_cnt, priv->length, priv->cmd, LINVAL);
priv->dav_seq++;
goto nrfd_exit;
}
if (priv->w_cnt >= priv->length) { // test for missed NDAC end of transfer
dev_err(board->gpib_dev, "Unexpected NRFD exit\n");
priv->write_done = 1;
priv->w_busy = 0;
wake_up_interruptible(&board->wait);
goto nrfd_exit;
}
dbg_printk(3, "sending %zu\n", priv->w_cnt);
set_data_lines(priv->w_buf[priv->w_cnt++]); // put the data on the lines
if (priv->w_cnt == priv->length && priv->end) {
dbg_printk(3, "Asserting EOI\n");
gpiod_set_value(EOI, 0); // Assert EOI
}
gpiod_set_value(DAV, 0); // Data available
priv->dav_tx = 0;
priv->phase = 410;
nrfd_exit:
spin_unlock_irqrestore(&priv->rw_lock, flags);
return IRQ_HANDLED;
}
/***************************************************************************
* *
* WRITE interrupt routine (NDAC line) *
* *
***************************************************************************/
static irqreturn_t bb_NDAC_interrupt(int irq, void *arg)
{
struct gpib_board *board = arg;
struct bb_priv *priv = board->private_data;
unsigned long flags;
int ndac;
spin_lock_irqsave(&priv->rw_lock, flags);
ndac = gpiod_get_value(NDAC);
priv->all_irqs++;
dbg_printk(3, "> irq: %d NRFD: %d NDAC: %d st: %4lx dir: %d busy: %d:%d\n",
irq, gpiod_get_value(NRFD), ndac, board->status, priv->direction,
priv->w_busy, priv->r_busy);
if (priv->ndac_mode) {
ENABLE_IRQ(priv->irq_NDAC, IRQ_TYPE_EDGE_RISING);
priv->ndac_mode = 0;
}
if (priv->w_busy == 0) {
dbg_printk(1, "interrupt while idle.\n");
priv->ndac_idle++;
goto ndac_exit;
}
if (ndac == 0) {
dbg_printk(1, "out of order interrupt at %zu:%d.\n", priv->w_cnt, priv->phase);
priv->phase = 500;
priv->ndac_seq++;
goto ndac_exit;
}
if (priv->dav_tx) {
dbg_printk(1, "DAV high after %zu/%zu cmd %d " LINFMT ". No action.\n",
priv->w_cnt, priv->length, priv->cmd, LINVAL);
priv->dav_seq++;
goto ndac_exit;
}
dbg_printk(3, "accepted %zu\n", priv->w_cnt - 1);
gpiod_set_value(DAV, 1); // Data not available
priv->dav_tx = 1;
priv->phase = 510;
if (priv->w_cnt >= priv->length) { // test for end of transfer
priv->write_done = 1;
priv->w_busy = 0;
wake_up_interruptible(&board->wait);
}
ndac_exit:
spin_unlock_irqrestore(&priv->rw_lock, flags);
return IRQ_HANDLED;
}
/***************************************************************************
* *
* interrupt routine for SRQ line *
* *
***************************************************************************/
static irqreturn_t bb_SRQ_interrupt(int irq, void *arg)
{
struct gpib_board *board = arg;
int val = gpiod_get_value(SRQ);
dbg_printk(3, "> %d st: %4lx\n", val, board->status);
if (!val)
set_bit(SRQI_NUM, &board->status); /* set_bit() is atomic */
wake_up_interruptible(&board->wait);
return IRQ_HANDLED;
}
static int bb_command(struct gpib_board *board, u8 *buffer,
size_t length, size_t *bytes_written)
{
size_t ret;
struct bb_priv *priv = board->private_data;
int i;
dbg_printk(2, "%p %p\n", buffer, board->buffer);
/* the _ATN line has already been asserted by bb_take_control() */
priv->cmd = 1;
ret = bb_write(board, buffer, length, 0, bytes_written); // no eoi
for (i = 0; i < length; i++) {
if (buffer[i] == UNT) {
priv->talker_state = talker_idle;
} else {
if (buffer[i] == UNL) {
priv->listener_state = listener_idle;
} else {
if (buffer[i] == (MTA(board->pad))) {
priv->talker_state = talker_addressed;
priv->listener_state = listener_idle;
} else if (buffer[i] == (MLA(board->pad))) {
priv->listener_state = listener_addressed;
priv->talker_state = talker_idle;
}
}
}
}
/* the _ATN line will be released by bb_go_to_stby */
priv->cmd = 0;
return ret;
}
/***************************************************************************
* *
* Buffer print with decode for debug/trace *
* *
***************************************************************************/
static char *cmd_string[32] = {
"", // 0x00
"GTL", // 0x01
"", // 0x02
"", // 0x03
"SDC", // 0x04
"PPC", // 0x05
"", // 0x06
"", // 0x07
"GET", // 0x08
"TCT", // 0x09
"", // 0x0a
"", // 0x0b
"", // 0x0c
"", // 0x0d
"", // 0x0e
"", // 0x0f
"", // 0x10
"LLO", // 0x11
"", // 0x12
"", // 0x13
"DCL", // 0x14
"PPU", // 0x15
"", // 0x16
"", // 0x17
"SPE", // 0x18
"SPD", // 0x19
"", // 0x1a
"", // 0x1b
"", // 0x1c
"", // 0x1d
"", // 0x1e
"CFE" // 0x1f
};
static void bb_buffer_print(struct gpib_board *board, unsigned char *buffer, size_t length,
int cmd, int eoi)
{
int i;
if (cmd) {
dbg_printk(2, "<cmd len %zu>\n", length);
for (i = 0; i < length; i++) {
if (buffer[i] < 0x20) {
dbg_printk(3, "0x%x=%s\n", buffer[i], cmd_string[buffer[i]]);
} else if (buffer[i] == 0x3f) {
dbg_printk(3, "0x%x=%s\n", buffer[i], "UNL");
} else if (buffer[i] == 0x5f) {
dbg_printk(3, "0x%x=%s\n", buffer[i], "UNT");
} else if (buffer[i] < 0x60) {
dbg_printk(3, "0x%x=%s%d\n", buffer[i],
(buffer[i] & 0x40) ? "TLK" : "LSN", buffer[i] & 0x1F);
} else {
dbg_printk(3, "0x%x\n", buffer[i]);
}
}
} else {
dbg_printk(2, "<data len %zu %s>\n", length, (eoi) ? "w.EOI" : " ");
for (i = 0; i < length; i++)
dbg_printk(2, "%3d 0x%x->%c\n", i, buffer[i], printable(buffer[i]));
}
}
/***************************************************************************
* *
* STATUS Management *
* *
***************************************************************************/
static void set_atn(struct gpib_board *board, int atn_asserted)
{
struct bb_priv *priv = board->private_data;
if (priv->listener_state != listener_idle &&
priv->talker_state != talker_idle) {
dev_err(board->gpib_dev, "listener/talker state machine conflict\n");
}
if (atn_asserted) {
if (priv->listener_state == listener_active)
priv->listener_state = listener_addressed;
if (priv->talker_state == talker_active)
priv->talker_state = talker_addressed;
SET_DIR_WRITE(priv); // need to be able to read bus NRFD/NDAC
} else {
if (priv->listener_state == listener_addressed) {
priv->listener_state = listener_active;
SET_DIR_READ(priv); // make sure holdoff is active when we unassert ATN
}
if (priv->talker_state == talker_addressed)
priv->talker_state = talker_active;
}
gpiod_direction_output(_ATN, !atn_asserted);
}
static int bb_take_control(struct gpib_board *board, int synchronous)
{
dbg_printk(2, "%d\n", synchronous);
set_atn(board, 1);
return 0;
}
static int bb_go_to_standby(struct gpib_board *board)
{
dbg_printk(2, "\n");
set_atn(board, 0);
return 0;
}
static int bb_request_system_control(struct gpib_board *board, int request_control)
{
struct bb_priv *priv = board->private_data;
dbg_printk(2, "%d\n", request_control);
if (!request_control)
return -EINVAL;
gpiod_direction_output(REN, 1); /* user space must enable REN if needed */
gpiod_direction_output(IFC, 1); /* user space must toggle IFC if needed */
if (sn7516x)
gpiod_direction_output(DC, 0); /* enable ATN as output on SN75161/2 */
gpiod_direction_input(SRQ);
ENABLE_IRQ(priv->irq_SRQ, IRQ_TYPE_EDGE_FALLING);
return 0;
}
static void bb_interface_clear(struct gpib_board *board, int assert)
{
struct bb_priv *priv = board->private_data;
dbg_printk(2, "%d\n", assert);
if (assert) {
gpiod_direction_output(IFC, 0);
priv->talker_state = talker_idle;
priv->listener_state = listener_idle;
set_bit(CIC_NUM, &board->status);
} else {
gpiod_direction_output(IFC, 1);
}
}
static void bb_remote_enable(struct gpib_board *board, int enable)
{
dbg_printk(2, "%d\n", enable);
if (enable) {
set_bit(REM_NUM, &board->status);
gpiod_direction_output(REN, 0);
} else {
clear_bit(REM_NUM, &board->status);
gpiod_direction_output(REN, 1);
}
}
static int bb_enable_eos(struct gpib_board *board, u8 eos_byte, int compare_8_bits)
{
struct bb_priv *priv = board->private_data;
dbg_printk(2, "%s\n", "EOS_en");
priv->eos = eos_byte;
priv->eos_flags = REOS;
if (compare_8_bits)
priv->eos_flags |= BIN;
return 0;
}
static void bb_disable_eos(struct gpib_board *board)
{
struct bb_priv *priv = board->private_data;
dbg_printk(2, "\n");
priv->eos_flags &= ~REOS;
}
static unsigned int bb_update_status(struct gpib_board *board, unsigned int clear_mask)
{
struct bb_priv *priv = board->private_data;
board->status &= ~clear_mask;
if (gpiod_get_value(SRQ)) /* SRQ asserted low */
clear_bit(SRQI_NUM, &board->status);
else
set_bit(SRQI_NUM, &board->status);
if (gpiod_get_value(_ATN)) /* ATN asserted low */
clear_bit(ATN_NUM, &board->status);
else
set_bit(ATN_NUM, &board->status);
if (priv->talker_state == talker_active ||
priv->talker_state == talker_addressed)
set_bit(TACS_NUM, &board->status);
else
clear_bit(TACS_NUM, &board->status);
if (priv->listener_state == listener_active ||
priv->listener_state == listener_addressed)
set_bit(LACS_NUM, &board->status);
else
clear_bit(LACS_NUM, &board->status);
dbg_printk(2, "0x%lx mask 0x%x\n", board->status, clear_mask);
return board->status;
}
static int bb_primary_address(struct gpib_board *board, unsigned int address)
{
dbg_printk(2, "%d\n", address);
board->pad = address;
return 0;
}
static int bb_secondary_address(struct gpib_board *board, unsigned int address, int enable)
{
dbg_printk(2, "%d %d\n", address, enable);
if (enable)
board->sad = address;
return 0;
}
static int bb_parallel_poll(struct gpib_board *board, u8 *result)
{
return -ENOENT;
}
static void bb_parallel_poll_configure(struct gpib_board *board, u8 config)
{
}
static void bb_parallel_poll_response(struct gpib_board *board, int ist)
{
}
static void bb_serial_poll_response(struct gpib_board *board, u8 status)
{
}
static u8 bb_serial_poll_status(struct gpib_board *board)
{
return 0; // -ENOENT;
}
static int bb_t1_delay(struct gpib_board *board, unsigned int nano_sec)
{
struct bb_priv *priv = board->private_data;
if (nano_sec <= 350)
priv->t1_delay = 350;
else if (nano_sec <= 1100)
priv->t1_delay = 1100;
else
priv->t1_delay = 2000;
dbg_printk(2, "t1 delay set to %d nanosec\n", priv->t1_delay);
return priv->t1_delay;
}
static void bb_return_to_local(struct gpib_board *board)
{
}
static int bb_line_status(const struct gpib_board *board)
{
int line_status = VALID_ALL;
if (gpiod_get_value(REN) == 0)
line_status |= BUS_REN;
if (gpiod_get_value(IFC) == 0)
line_status |= BUS_IFC;
if (gpiod_get_value(NDAC) == 0)
line_status |= BUS_NDAC;
if (gpiod_get_value(NRFD) == 0)
line_status |= BUS_NRFD;
if (gpiod_get_value(DAV) == 0)
line_status |= BUS_DAV;
if (gpiod_get_value(EOI) == 0)
line_status |= BUS_EOI;
if (gpiod_get_value(_ATN) == 0)
line_status |= BUS_ATN;
if (gpiod_get_value(SRQ) == 0)
line_status |= BUS_SRQ;
dbg_printk(2, "status lines: %4x\n", line_status);
return line_status;
}
/***************************************************************************
* *
* Module Management *
* *
***************************************************************************/
static int allocate_private(struct gpib_board *board)
{
board->private_data = kzalloc(sizeof(struct bb_priv), GFP_KERNEL);
if (!board->private_data)
return -1;
return 0;
}
static void free_private(struct gpib_board *board)
{
kfree(board->private_data);
board->private_data = NULL;
}
static int bb_get_irq(struct gpib_board *board, char *name,
struct gpio_desc *gpio, int *irq,
irq_handler_t handler, irq_handler_t thread_fn, unsigned long flags)
{
if (!gpio)
return -1;
gpiod_direction_input(gpio);
*irq = gpiod_to_irq(gpio);
dbg_printk(2, "IRQ %s: %d\n", name, *irq);
if (*irq < 0) {
dev_err(board->gpib_dev, "can't get IRQ for %s\n", name);
return -1;
}
if (request_threaded_irq(*irq, handler, thread_fn, flags, name, board)) {
dev_err(board->gpib_dev, "can't request IRQ for %s %d\n", name, *irq);
*irq = 0;
return -1;
}
DISABLE_IRQ(*irq);
return 0;
}
static void bb_free_irq(struct gpib_board *board, int *irq, char *name)
{
if (*irq) {
free_irq(*irq, board);
dbg_printk(2, "IRQ %d(%s) freed\n", *irq, name);
*irq = 0;
}
}
static void release_gpios(void)
{
int j;
for (j = 0 ; j < NUM_PINS ; j++) {
if (all_descriptors[j]) {
gpiod_put(all_descriptors[j]);
all_descriptors[j] = NULL;
}
}
}
static int allocate_gpios(struct gpib_board *board)
{
int j;
int table_index = 0;
char name[256];
struct gpio_desc *desc;
struct gpiod_lookup_table *lookup_table;
if (!board->gpib_dev) {
pr_err("NULL gpib dev for board\n");
return -ENOENT;
}
lookup_table = lookup_tables[table_index];
lookup_table->dev_id = dev_name(board->gpib_dev);
gpiod_add_lookup_table(lookup_table);
dbg_printk(1, "Allocating gpios using table index %d\n", table_index);
for (j = 0 ; j < NUM_PINS ; j++) {
if (gpios_vector[j] < 0)
continue;
/* name not really used in gpiod_get_index() */
sprintf(name, "GPIO%d", gpios_vector[j]);
try_again:
dbg_printk(1, "Allocating gpio %s pin no %d\n", name, gpios_vector[j]);
desc = gpiod_get_index(board->gpib_dev, name, gpios_vector[j], GPIOD_IN);
if (IS_ERR(desc)) {
gpiod_remove_lookup_table(lookup_table);
table_index++;
lookup_table = lookup_tables[table_index];
if (!lookup_table) {
dev_err(board->gpib_dev, "Unable to obtain gpio descriptor for pin %d error %ld\n",
gpios_vector[j], PTR_ERR(desc));
goto alloc_gpios_fail;
}
dbg_printk(1, "Allocation failed, now using table_index %d\n", table_index);
lookup_table->dev_id = dev_name(board->gpib_dev);
gpiod_add_lookup_table(lookup_table);
goto try_again;
}
all_descriptors[j] = desc;
}
gpiod_remove_lookup_table(lookup_table);
return 0;
alloc_gpios_fail:
release_gpios();
return -1;
}
static void bb_detach(struct gpib_board *board)
{
struct bb_priv *priv = board->private_data;
dbg_printk(2, "Enter with data %p\n", board->private_data);
if (!board->private_data)
return;
bb_free_irq(board, &priv->irq_DAV, NAME "_DAV");
bb_free_irq(board, &priv->irq_NRFD, NAME "_NRFD");
bb_free_irq(board, &priv->irq_NDAC, NAME "_NDAC");
bb_free_irq(board, &priv->irq_SRQ, NAME "_SRQ");
if (strcmp(PINMAP_2, pin_map) == 0) { /* YOGA */
gpiod_set_value(YOGA_ENABLE, 0);
}
release_gpios();
dbg_printk(2, "detached board: %d\n", board->minor);
dbg_printk(0, "NRFD: idle %d, seq %d, NDAC: idle %d, seq %d DAV: idle %d seq: %d all: %ld",
priv->nrfd_idle, priv->nrfd_seq,
priv->ndac_idle, priv->ndac_seq,
priv->dav_idle, priv->dav_seq, priv->all_irqs);
free_private(board);
}
static int bb_attach(struct gpib_board *board, const struct gpib_board_config *config)
{
struct bb_priv *priv;
int retval = 0;
dbg_printk(2, "%s\n", "Enter ...");
board->status = 0;
if (allocate_private(board))
return -ENOMEM;
priv = board->private_data;
priv->direction = -1;
priv->t1_delay = 2000;
priv->listener_state = listener_idle;
priv->talker_state = talker_idle;
sn7516x = sn7516x_used;
if (strcmp(PINMAP_0, pin_map) == 0) {
if (!sn7516x) {
gpios_vector[&(PE) - &all_descriptors[0]] = -1;
gpios_vector[&(DC) - &all_descriptors[0]] = -1;
gpios_vector[&(TE) - &all_descriptors[0]] = -1;
}
} else if (strcmp(PINMAP_1, pin_map) == 0) {
if (!sn7516x) {
gpios_vector[&(PE) - &all_descriptors[0]] = -1;
gpios_vector[&(DC) - &all_descriptors[0]] = -1;
gpios_vector[&(TE) - &all_descriptors[0]] = -1;
}
gpios_vector[&(REN) - &all_descriptors[0]] = 0; /* 27 -> 0 REN on GPIB pin 0 */
} else if (strcmp(PINMAP_2, pin_map) == 0) { /* YOGA */
sn7516x = 0;
gpios_vector[&(D03) - &all_descriptors[0]] = YOGA_D03_pin_nr;
gpios_vector[&(D04) - &all_descriptors[0]] = YOGA_D04_pin_nr;
gpios_vector[&(D05) - &all_descriptors[0]] = YOGA_D05_pin_nr;
gpios_vector[&(D06) - &all_descriptors[0]] = YOGA_D06_pin_nr;
gpios_vector[&(PE) - &all_descriptors[0]] = -1;
gpios_vector[&(DC) - &all_descriptors[0]] = -1;
} else {
dev_err(board->gpib_dev, "Unrecognized pin map %s\n", pin_map);
goto bb_attach_fail;
}
dbg_printk(0, "Using pin map \"%s\" %s\n", pin_map, (sn7516x) ?
" with SN7516x driver support" : "");
if (allocate_gpios(board))
goto bb_attach_fail;
/*
* Configure SN7516X control lines.
* drive ATN, IFC and REN as outputs only when master
* i.e. system controller. In this mode can only be the CIC
* When not master then enable device mode ATN, IFC & REN as inputs
*/
if (sn7516x) {
gpiod_direction_output(DC, 0);
gpiod_direction_output(TE, 1);
gpiod_direction_output(PE, 1);
}
/* Set main control lines to a known state */
gpiod_direction_output(IFC, 1);
gpiod_direction_output(REN, 1);
gpiod_direction_output(_ATN, 1);
if (strcmp(PINMAP_2, pin_map) == 0) { /* YOGA: enable level shifters */
gpiod_direction_output(YOGA_ENABLE, 1);
}
spin_lock_init(&priv->rw_lock);
/* request DAV interrupt for read */
if (bb_get_irq(board, NAME "_DAV", DAV, &priv->irq_DAV, bb_DAV_interrupt, NULL,
IRQF_TRIGGER_NONE))
goto bb_attach_fail_r;
/* request NRFD interrupt for write */
if (bb_get_irq(board, NAME "_NRFD", NRFD, &priv->irq_NRFD, bb_NRFD_interrupt, NULL,
IRQF_TRIGGER_NONE))
goto bb_attach_fail_r;
/* request NDAC interrupt for write */
if (bb_get_irq(board, NAME "_NDAC", NDAC, &priv->irq_NDAC, bb_NDAC_interrupt, NULL,
IRQF_TRIGGER_NONE))
goto bb_attach_fail_r;
/* request SRQ interrupt for Service Request */
if (bb_get_irq(board, NAME "_SRQ", SRQ, &priv->irq_SRQ, bb_SRQ_interrupt, NULL,
IRQF_TRIGGER_NONE))
goto bb_attach_fail_r;
dbg_printk(0, "attached board %d\n", board->minor);
goto bb_attach_out;
bb_attach_fail_r:
release_gpios();
bb_attach_fail:
retval = -1;
bb_attach_out:
return retval;
}
static struct gpib_interface bb_interface = {
.name = NAME,
.attach = bb_attach,
.detach = bb_detach,
.read = bb_read,
.write = bb_write,
.command = bb_command,
.take_control = bb_take_control,
.go_to_standby = bb_go_to_standby,
.request_system_control = bb_request_system_control,
.interface_clear = bb_interface_clear,
.remote_enable = bb_remote_enable,
.enable_eos = bb_enable_eos,
.disable_eos = bb_disable_eos,
.parallel_poll = bb_parallel_poll,
.parallel_poll_configure = bb_parallel_poll_configure,
.parallel_poll_response = bb_parallel_poll_response,
.line_status = bb_line_status,
.update_status = bb_update_status,
.primary_address = bb_primary_address,
.secondary_address = bb_secondary_address,
.serial_poll_response = bb_serial_poll_response,
.serial_poll_status = bb_serial_poll_status,
.t1_delay = bb_t1_delay,
.return_to_local = bb_return_to_local,
};
static int __init bb_init_module(void)
{
int result = gpib_register_driver(&bb_interface, THIS_MODULE);
if (result) {
pr_err("gpib_register_driver failed: error = %d\n", result);
return result;
}
return 0;
}
static void __exit bb_exit_module(void)
{
gpib_unregister_driver(&bb_interface);
}
module_init(bb_init_module);
module_exit(bb_exit_module);
/***************************************************************************
* *
* UTILITY Functions *
* *
***************************************************************************/
inline long usec_diff(struct timespec64 *a, struct timespec64 *b)
{
return ((a->tv_sec - b->tv_sec) * 1000000 +
(a->tv_nsec - b->tv_nsec) / 1000);
}
static inline int check_for_eos(struct bb_priv *priv, u8 byte)
{
if (priv->eos_check)
return 0;
if (priv->eos_check_8) {
if (priv->eos == byte)
return 1;
} else {
if (priv->eos_mask_7 == (byte & 0x7f))
return 1;
}
return 0;
}
static void set_data_lines_output(void)
{
gpiod_direction_output(D01, 1);
gpiod_direction_output(D02, 1);
gpiod_direction_output(D03, 1);
gpiod_direction_output(D04, 1);
gpiod_direction_output(D05, 1);
gpiod_direction_output(D06, 1);
gpiod_direction_output(D07, 1);
gpiod_direction_output(D08, 1);
}
static void set_data_lines(u8 byte)
{
gpiod_set_value(D01, !(byte & 0x01));
gpiod_set_value(D02, !(byte & 0x02));
gpiod_set_value(D03, !(byte & 0x04));
gpiod_set_value(D04, !(byte & 0x08));
gpiod_set_value(D05, !(byte & 0x10));
gpiod_set_value(D06, !(byte & 0x20));
gpiod_set_value(D07, !(byte & 0x40));
gpiod_set_value(D08, !(byte & 0x80));
}
static u8 get_data_lines(void)
{
u8 ret;
ret = gpiod_get_value(D01);
ret |= gpiod_get_value(D02) << 1;
ret |= gpiod_get_value(D03) << 2;
ret |= gpiod_get_value(D04) << 3;
ret |= gpiod_get_value(D05) << 4;
ret |= gpiod_get_value(D06) << 5;
ret |= gpiod_get_value(D07) << 6;
ret |= gpiod_get_value(D08) << 7;
return ~ret;
}
static void set_data_lines_input(void)
{
gpiod_direction_input(D01);
gpiod_direction_input(D02);
gpiod_direction_input(D03);
gpiod_direction_input(D04);
gpiod_direction_input(D05);
gpiod_direction_input(D06);
gpiod_direction_input(D07);
gpiod_direction_input(D08);
}
static inline void SET_DIR_WRITE(struct bb_priv *priv)
{
if (priv->direction == DIR_WRITE)
return;
gpiod_direction_input(NRFD);
gpiod_direction_input(NDAC);
set_data_lines_output();
gpiod_direction_output(DAV, 1);
gpiod_direction_output(EOI, 1);
if (sn7516x) {
gpiod_set_value(PE, 1); /* set data lines to transmit on sn75160b */
gpiod_set_value(TE, 1); /* set NDAC and NRFD to receive and DAV to transmit */
}
priv->direction = DIR_WRITE;
}
static inline void SET_DIR_READ(struct bb_priv *priv)
{
if (priv->direction == DIR_READ)
return;
gpiod_direction_input(DAV);
gpiod_direction_input(EOI);
set_data_lines_input();
if (sn7516x) {
gpiod_set_value(PE, 0); /* set data lines to receive on sn75160b */
gpiod_set_value(TE, 0); /* set NDAC and NRFD to transmit and DAV to receive */
}
gpiod_direction_output(NRFD, 0); // hold off the talker
gpiod_direction_output(NDAC, 0); // data not accepted
priv->direction = DIR_READ;
}
|