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