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