File Index
Files and directories
Sign in
FILE

gpib_bitbang.c

File preview with syntax highlighting.

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