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 70 static int sn7516x_used = 1, sn7516x; 71 module_param(sn7516x_used, int, 0660); 72 73 #define PINMAP_0 "elektronomikon" 74 #define PINMAP_1 "gpib4pi-1.1" 75 #define PINMAP_2 "yoga" 76 static char *pin_map = PINMAP_0; 77 module_param(pin_map, charp, 0660); 78 MODULE_PARM_DESC(pin_map, " valid values: " PINMAP_0 " " PINMAP_1 " " PINMAP_2); 79 80 /********************************************** 81 * Signal pairing and pin wiring between the * 82 * Raspberry-Pi connector and the GPIB bus * 83 * * 84 * signal pin wiring * 85 * GPIB Pi-gpio GPIB -> RPi * 86 ********************************************** 87 */ 88 enum lines_t { 89 D01_pin_nr = 20, /* 1 -> 38 */ 90 D02_pin_nr = 26, /* 2 -> 37 */ 91 D03_pin_nr = 16, /* 3 -> 36 */ 92 D04_pin_nr = 19, /* 4 -> 35 */ 93 D05_pin_nr = 13, /* 13 -> 33 */ 94 D06_pin_nr = 12, /* 14 -> 32 */ 95 D07_pin_nr = 6, /* 15 -> 31 */ 96 D08_pin_nr = 5, /* 16 -> 29 */ 97 EOI_pin_nr = 9, /* 5 -> 21 */ 98 DAV_pin_nr = 10, /* 6 -> 19 */ 99 NRFD_pin_nr = 24, /* 7 -> 18 */ 100 NDAC_pin_nr = 23, /* 8 -> 16 */ 101 IFC_pin_nr = 22, /* 9 -> 15 */ 102 SRQ_pin_nr = 11, /* 10 -> 23 */ 103 _ATN_pin_nr = 25, /* 11 -> 22 */ 104 REN_pin_nr = 27, /* 17 -> 13 */ 105 /* 106 * GROUND PINS 107 * 12,18,19,20,21,22,23,24 => 14,20,25,30,34,39 108 */ 109 110 /* 111 * These lines are used to control the external 112 * SN75160/161 driver chips when used. 113 * When not used there is reduced fan out; 114 * currently tested with up to 4 devices. 115 */ 116 117 /* Pi GPIO RPI 75161B 75160B Description */ 118 PE_pin_nr = 7, /* 26 -> nc 11 Pullup Enable */ 119 DC_pin_nr = 8, /* 24 -> 12 nc Direction control */ 120 TE_pin_nr = 18, /* 12 -> 2 1 Talk Enable */ 121 ACT_LED_pin_nr = 4, /* 7 -> LED */ 122 123 /* YOGA adapter uses different pinout to ease layout */ 124 YOGA_D03_pin_nr = 13, 125 YOGA_D04_pin_nr = 12, 126 YOGA_D05_pin_nr = 21, 127 YOGA_D06_pin_nr = 19, 128 }; 129 130 /* 131 * GPIO descriptors and pins - WARNING: STRICTLY KEEP ITEMS ORDER 132 */ 133 134 #define GPIB_PINS 16 135 #define SN7516X_PINS 4 136 #define NUM_PINS (GPIB_PINS + SN7516X_PINS) 137 138 #define 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 147 static 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 175 static 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 202 static 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 232 static 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 262 static 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 270 struct 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 314 static inline long usec_diff(struct timespec64 *a, struct timespec64 *b); 315 static void bb_buffer_print(struct gpib_board *board, unsigned char *buffer, size_t length, 316 int cmd, int eoi); 317 static void set_data_lines(u8 byte); 318 static u8 get_data_lines(void); 319 static void set_data_lines_input(void); 320 static void set_data_lines_output(void); 321 static inline int check_for_eos(struct bb_priv *priv, u8 byte); 322 static void set_atn(struct gpib_board *board, int atn_asserted); 323 324 static inline void SET_DIR_WRITE(struct bb_priv *priv); 325 static inline void SET_DIR_READ(struct bb_priv *priv); 326 327 #define DIR_READ 0 328 #define DIR_WRITE 1 329 330 MODULE_LICENSE("GPL"); 331 MODULE_DESCRIPTION("GPIB helper functions for bitbanging I/O"); 332 333 /**** global variables ****/ 334 static int debug; 335 module_param(debug, int, 0644); 336 337 static 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 350 static 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 405 read_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 420 static 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 476 dav_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 488 static 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 561 write_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 575 static 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 637 nrfd_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 649 static 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 699 ndac_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 710 static 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 726 static 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 772 static 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 807 static 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 ***************************************************************************/ 840 static 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 865 static 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 872 static 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 879 static 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 899 static 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 914 static 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 926 static 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 939 static 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 947 static 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 978 static 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 985 static 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 993 static int bb_parallel_poll(struct gpib_board *board, u8 *result) 994 { 995 return -ENOENT; 996 } 997 998 static void bb_parallel_poll_configure(struct gpib_board *board, u8 config) 999 { 1000 } 1001 1002 static void bb_parallel_poll_response(struct gpib_board *board, int ist) 1003 { 1004 } 1005 1006 static void bb_serial_poll_response(struct gpib_board *board, u8 status) 1007 { 1008 } 1009 1010 static u8 bb_serial_poll_status(struct gpib_board *board) 1011 { 1012 return 0; // -ENOENT; 1013 } 1014 1015 static 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 1031 static void bb_return_to_local(struct gpib_board *board) 1032 { 1033 } 1034 1035 static 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 1067 static int allocate_private(struct gpib_board *board) 1068 { 1069 board->private_data = kzalloc_obj(struct bb_priv); 1070 if (!board->private_data) 1071 return -ENOMEM; 1072 return 0; 1073 } 1074 1075 static void free_private(struct gpib_board *board) 1076 { 1077 kfree(board->private_data); 1078 board->private_data = NULL; 1079 } 1080 1081 static 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 1103 static 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 1112 static 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 1124 static 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]); 1147 try_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 1172 alloc_gpios_fail: 1173 release_gpios(); 1174 return -1; 1175 } 1176 1177 static 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 1205 static int bb_attach(struct gpib_board *board, const struct gpib_board_config *config) 1206 { 1207 struct bb_priv *priv; 1208 int retval; 1209 1210 dbg_printk(2, "%s\n", "Enter ..."); 1211 1212 board->status = 0; 1213 1214 retval = allocate_private(board); 1215 if (retval) 1216 return retval; 1217 priv = board->private_data; 1218 priv->direction = -1; 1219 priv->t1_delay = 2000; 1220 priv->listener_state = listener_idle; 1221 priv->talker_state = talker_idle; 1222 1223 sn7516x = sn7516x_used; 1224 if (strcmp(PINMAP_0, pin_map) == 0) { 1225 if (!sn7516x) { 1226 gpios_vector[&(PE) - &all_descriptors[0]] = -1; 1227 gpios_vector[&(DC) - &all_descriptors[0]] = -1; 1228 gpios_vector[&(TE) - &all_descriptors[0]] = -1; 1229 } 1230 } else if (strcmp(PINMAP_1, pin_map) == 0) { 1231 if (!sn7516x) { 1232 gpios_vector[&(PE) - &all_descriptors[0]] = -1; 1233 gpios_vector[&(DC) - &all_descriptors[0]] = -1; 1234 gpios_vector[&(TE) - &all_descriptors[0]] = -1; 1235 } 1236 gpios_vector[&(REN) - &all_descriptors[0]] = 0; /* 27 -> 0 REN on GPIB pin 0 */ 1237 } else if (strcmp(PINMAP_2, pin_map) == 0) { /* YOGA */ 1238 sn7516x = 0; 1239 gpios_vector[&(D03) - &all_descriptors[0]] = YOGA_D03_pin_nr; 1240 gpios_vector[&(D04) - &all_descriptors[0]] = YOGA_D04_pin_nr; 1241 gpios_vector[&(D05) - &all_descriptors[0]] = YOGA_D05_pin_nr; 1242 gpios_vector[&(D06) - &all_descriptors[0]] = YOGA_D06_pin_nr; 1243 gpios_vector[&(PE) - &all_descriptors[0]] = -1; 1244 gpios_vector[&(DC) - &all_descriptors[0]] = -1; 1245 } else { 1246 dev_err(board->gpib_dev, "Unrecognized pin map %s\n", pin_map); 1247 goto bb_attach_fail; 1248 } 1249 dbg_printk(0, "Using pin map \"%s\" %s\n", pin_map, (sn7516x) ? 1250 " with SN7516x driver support" : ""); 1251 1252 if (allocate_gpios(board)) 1253 goto bb_attach_fail; 1254 1255 /* 1256 * Configure SN7516X control lines. 1257 * drive ATN, IFC and REN as outputs only when master 1258 * i.e. system controller. In this mode can only be the CIC 1259 * When not master then enable device mode ATN, IFC & REN as inputs 1260 */ 1261 if (sn7516x) { 1262 gpiod_direction_output(DC, 0); 1263 gpiod_direction_output(TE, 1); 1264 gpiod_direction_output(PE, 1); 1265 } 1266 /* Set main control lines to a known state */ 1267 gpiod_direction_output(IFC, 1); 1268 gpiod_direction_output(REN, 1); 1269 gpiod_direction_output(_ATN, 1); 1270 1271 if (strcmp(PINMAP_2, pin_map) == 0) { /* YOGA: enable level shifters */ 1272 gpiod_direction_output(YOGA_ENABLE, 1); 1273 } 1274 1275 spin_lock_init(&priv->rw_lock); 1276 1277 /* request DAV interrupt for read */ 1278 if (bb_get_irq(board, NAME "_DAV", DAV, &priv->irq_DAV, bb_DAV_interrupt, NULL, 1279 IRQF_TRIGGER_NONE)) 1280 goto bb_attach_fail_r; 1281 1282 /* request NRFD interrupt for write */ 1283 if (bb_get_irq(board, NAME "_NRFD", NRFD, &priv->irq_NRFD, bb_NRFD_interrupt, NULL, 1284 IRQF_TRIGGER_NONE)) 1285 goto bb_attach_fail_r; 1286 1287 /* request NDAC interrupt for write */ 1288 if (bb_get_irq(board, NAME "_NDAC", NDAC, &priv->irq_NDAC, bb_NDAC_interrupt, NULL, 1289 IRQF_TRIGGER_NONE)) 1290 goto bb_attach_fail_r; 1291 1292 /* request SRQ interrupt for Service Request */ 1293 if (bb_get_irq(board, NAME "_SRQ", SRQ, &priv->irq_SRQ, bb_SRQ_interrupt, NULL, 1294 IRQF_TRIGGER_NONE)) 1295 goto bb_attach_fail_r; 1296 1297 dbg_printk(0, "attached board %d\n", board->minor); 1298 goto bb_attach_out; 1299 1300 bb_attach_fail_r: 1301 release_gpios(); 1302 bb_attach_fail: 1303 retval = -1; 1304 bb_attach_out: 1305 return retval; 1306 } 1307 1308 static struct gpib_interface bb_interface = { 1309 .name = NAME, 1310 .attach = bb_attach, 1311 .detach = bb_detach, 1312 .read = bb_read, 1313 .write = bb_write, 1314 .command = bb_command, 1315 .take_control = bb_take_control, 1316 .go_to_standby = bb_go_to_standby, 1317 .request_system_control = bb_request_system_control, 1318 .interface_clear = bb_interface_clear, 1319 .remote_enable = bb_remote_enable, 1320 .enable_eos = bb_enable_eos, 1321 .disable_eos = bb_disable_eos, 1322 .parallel_poll = bb_parallel_poll, 1323 .parallel_poll_configure = bb_parallel_poll_configure, 1324 .parallel_poll_response = bb_parallel_poll_response, 1325 .line_status = bb_line_status, 1326 .update_status = bb_update_status, 1327 .primary_address = bb_primary_address, 1328 .secondary_address = bb_secondary_address, 1329 .serial_poll_response = bb_serial_poll_response, 1330 .serial_poll_status = bb_serial_poll_status, 1331 .t1_delay = bb_t1_delay, 1332 .return_to_local = bb_return_to_local, 1333 }; 1334 1335 static int __init bb_init_module(void) 1336 { 1337 int result = gpib_register_driver(&bb_interface, THIS_MODULE); 1338 1339 if (result) { 1340 pr_err("gpib_register_driver failed: error = %d\n", result); 1341 return result; 1342 } 1343 1344 return 0; 1345 } 1346 1347 static void __exit bb_exit_module(void) 1348 { 1349 gpib_unregister_driver(&bb_interface); 1350 } 1351 1352 module_init(bb_init_module); 1353 module_exit(bb_exit_module); 1354 1355 /*************************************************************************** 1356 * * 1357 * UTILITY Functions * 1358 * * 1359 ***************************************************************************/ 1360 inline long usec_diff(struct timespec64 *a, struct timespec64 *b) 1361 { 1362 return ((a->tv_sec - b->tv_sec) * 1000000 + 1363 (a->tv_nsec - b->tv_nsec) / 1000); 1364 } 1365 1366 static inline int check_for_eos(struct bb_priv *priv, u8 byte) 1367 { 1368 if (priv->eos_check) 1369 return 0; 1370 1371 if (priv->eos_check_8) { 1372 if (priv->eos == byte) 1373 return 1; 1374 } else { 1375 if (priv->eos_mask_7 == (byte & 0x7f)) 1376 return 1; 1377 } 1378 return 0; 1379 } 1380 1381 static void set_data_lines_output(void) 1382 { 1383 gpiod_direction_output(D01, 1); 1384 gpiod_direction_output(D02, 1); 1385 gpiod_direction_output(D03, 1); 1386 gpiod_direction_output(D04, 1); 1387 gpiod_direction_output(D05, 1); 1388 gpiod_direction_output(D06, 1); 1389 gpiod_direction_output(D07, 1); 1390 gpiod_direction_output(D08, 1); 1391 } 1392 1393 static void set_data_lines(u8 byte) 1394 { 1395 gpiod_set_value(D01, !(byte & 0x01)); 1396 gpiod_set_value(D02, !(byte & 0x02)); 1397 gpiod_set_value(D03, !(byte & 0x04)); 1398 gpiod_set_value(D04, !(byte & 0x08)); 1399 gpiod_set_value(D05, !(byte & 0x10)); 1400 gpiod_set_value(D06, !(byte & 0x20)); 1401 gpiod_set_value(D07, !(byte & 0x40)); 1402 gpiod_set_value(D08, !(byte & 0x80)); 1403 } 1404 1405 static u8 get_data_lines(void) 1406 { 1407 u8 ret; 1408 1409 ret = gpiod_get_value(D01); 1410 ret |= gpiod_get_value(D02) << 1; 1411 ret |= gpiod_get_value(D03) << 2; 1412 ret |= gpiod_get_value(D04) << 3; 1413 ret |= gpiod_get_value(D05) << 4; 1414 ret |= gpiod_get_value(D06) << 5; 1415 ret |= gpiod_get_value(D07) << 6; 1416 ret |= gpiod_get_value(D08) << 7; 1417 return ~ret; 1418 } 1419 1420 static void set_data_lines_input(void) 1421 { 1422 gpiod_direction_input(D01); 1423 gpiod_direction_input(D02); 1424 gpiod_direction_input(D03); 1425 gpiod_direction_input(D04); 1426 gpiod_direction_input(D05); 1427 gpiod_direction_input(D06); 1428 gpiod_direction_input(D07); 1429 gpiod_direction_input(D08); 1430 } 1431 1432 static inline void SET_DIR_WRITE(struct bb_priv *priv) 1433 { 1434 if (priv->direction == DIR_WRITE) 1435 return; 1436 1437 gpiod_direction_input(NRFD); 1438 gpiod_direction_input(NDAC); 1439 set_data_lines_output(); 1440 gpiod_direction_output(DAV, 1); 1441 gpiod_direction_output(EOI, 1); 1442 1443 if (sn7516x) { 1444 gpiod_set_value(PE, 1); /* set data lines to transmit on sn75160b */ 1445 gpiod_set_value(TE, 1); /* set NDAC and NRFD to receive and DAV to transmit */ 1446 } 1447 1448 priv->direction = DIR_WRITE; 1449 } 1450 1451 static inline void SET_DIR_READ(struct bb_priv *priv) 1452 { 1453 if (priv->direction == DIR_READ) 1454 return; 1455 1456 gpiod_direction_input(DAV); 1457 gpiod_direction_input(EOI); 1458 1459 set_data_lines_input(); 1460 1461 if (sn7516x) { 1462 gpiod_set_value(PE, 0); /* set data lines to receive on sn75160b */ 1463 gpiod_set_value(TE, 0); /* set NDAC and NRFD to transmit and DAV to receive */ 1464 } 1465 1466 gpiod_direction_output(NRFD, 0); /* hold off the talker */ 1467 gpiod_direction_output(NDAC, 0); /* data not accepted */ 1468 1469 priv->direction = DIR_READ; 1470 } 1471