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