1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu, Michael Smith 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 #include "opt_ppb_1284.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/module.h> 38 #include <sys/bus.h> 39 #include <sys/conf.h> 40 #include <sys/kernel.h> 41 #include <sys/lock.h> 42 #include <sys/sx.h> 43 #include <sys/uio.h> 44 #include <sys/fcntl.h> 45 46 #include <machine/bus.h> 47 #include <machine/resource.h> 48 #include <sys/rman.h> 49 50 #include <dev/ppbus/ppbconf.h> 51 #include <dev/ppbus/ppb_msq.h> 52 53 #ifdef PERIPH_1284 54 #include <sys/malloc.h> 55 #include <dev/ppbus/ppb_1284.h> 56 #endif 57 58 #include <dev/ppbus/ppi.h> 59 60 #include "ppbus_if.h" 61 62 #include <dev/ppbus/ppbio.h> 63 64 #define BUFSIZE 512 65 66 struct ppi_data { 67 device_t ppi_device; 68 struct cdev *ppi_cdev; 69 struct sx ppi_lock; 70 int ppi_flags; 71 #define HAVE_PPBUS (1<<0) 72 73 int ppi_mode; /* IEEE1284 mode */ 74 char ppi_buffer[BUFSIZE]; 75 76 #ifdef PERIPH_1284 77 struct resource *intr_resource; /* interrupt resource */ 78 void *intr_cookie; /* interrupt registration cookie */ 79 #endif /* PERIPH_1284 */ 80 }; 81 82 #define DEVTOSOFTC(dev) \ 83 ((struct ppi_data *)device_get_softc(dev)) 84 85 static devclass_t ppi_devclass; 86 87 #ifdef PERIPH_1284 88 static void ppiintr(void *arg); 89 #endif 90 91 static d_open_t ppiopen; 92 static d_close_t ppiclose; 93 static d_ioctl_t ppiioctl; 94 static d_write_t ppiwrite; 95 static d_read_t ppiread; 96 97 static struct cdevsw ppi_cdevsw = { 98 .d_version = D_VERSION, 99 .d_open = ppiopen, 100 .d_close = ppiclose, 101 .d_read = ppiread, 102 .d_write = ppiwrite, 103 .d_ioctl = ppiioctl, 104 .d_name = "ppi", 105 }; 106 107 #ifdef PERIPH_1284 108 109 static void 110 ppi_enable_intr(device_t ppidev) 111 { 112 char r; 113 device_t ppbus = device_get_parent(ppidev); 114 115 r = ppb_rctr(ppbus); 116 ppb_wctr(ppbus, r | IRQENABLE); 117 118 return; 119 } 120 121 static void 122 ppi_disable_intr(device_t ppidev) 123 { 124 char r; 125 device_t ppbus = device_get_parent(ppidev); 126 127 r = ppb_rctr(ppbus); 128 ppb_wctr(ppbus, r & ~IRQENABLE); 129 130 return; 131 } 132 133 #endif /* PERIPH_1284 */ 134 135 static void 136 ppi_identify(driver_t *driver, device_t parent) 137 { 138 139 device_t dev; 140 141 dev = device_find_child(parent, "ppi", -1); 142 if (!dev) 143 BUS_ADD_CHILD(parent, 0, "ppi", -1); 144 } 145 146 /* 147 * ppi_probe() 148 */ 149 static int 150 ppi_probe(device_t dev) 151 { 152 struct ppi_data *ppi; 153 154 /* probe is always ok */ 155 device_set_desc(dev, "Parallel I/O"); 156 157 ppi = DEVTOSOFTC(dev); 158 159 return (0); 160 } 161 162 /* 163 * ppi_attach() 164 */ 165 static int 166 ppi_attach(device_t dev) 167 { 168 struct ppi_data *ppi = DEVTOSOFTC(dev); 169 #ifdef PERIPH_1284 170 int error, rid = 0; 171 172 /* declare our interrupt handler */ 173 ppi->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 174 RF_ACTIVE); 175 if (ppi->intr_resource) { 176 /* register our interrupt handler */ 177 error = bus_setup_intr(dev, ppi->intr_resource, 178 INTR_TYPE_TTY | INTR_MPSAFE, NULL, ppiintr, dev, 179 &ppi->intr_cookie); 180 if (error) { 181 bus_release_resource(dev, SYS_RES_IRQ, rid, 182 ppi->intr_resource); 183 device_printf(dev, 184 "Unable to register interrupt handler\n"); 185 return (error); 186 } 187 } 188 #endif /* PERIPH_1284 */ 189 190 sx_init(&ppi->ppi_lock, "ppi"); 191 ppi->ppi_cdev = make_dev(&ppi_cdevsw, device_get_unit(dev), 192 UID_ROOT, GID_WHEEL, 193 0600, "ppi%d", device_get_unit(dev)); 194 if (ppi->ppi_cdev == NULL) { 195 device_printf(dev, "Failed to create character device\n"); 196 return (ENXIO); 197 } 198 ppi->ppi_cdev->si_drv1 = ppi; 199 ppi->ppi_device = dev; 200 201 return (0); 202 } 203 204 static int 205 ppi_detach(device_t dev) 206 { 207 struct ppi_data *ppi = DEVTOSOFTC(dev); 208 209 destroy_dev(ppi->ppi_cdev); 210 #ifdef PERIPH_1284 211 if (ppi->intr_resource != NULL) { 212 bus_teardown_intr(dev, ppi->intr_resource, ppi->intr_cookie); 213 bus_release_resource(dev, SYS_RES_IRQ, 0, ppi->intr_resource); 214 } 215 #endif 216 sx_destroy(&ppi->ppi_lock); 217 return (0); 218 } 219 220 #ifdef PERIPH_1284 221 /* 222 * Cable 223 * ----- 224 * 225 * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks: 226 * 227 * nStrobe <-> nAck 1 <-> 10 228 * nAutofd <-> Busy 11 <-> 14 229 * nSelectin <-> Select 17 <-> 13 230 * nInit <-> nFault 15 <-> 16 231 * 232 */ 233 static void 234 ppiintr(void *arg) 235 { 236 device_t ppidev = (device_t)arg; 237 device_t ppbus = device_get_parent(ppidev); 238 struct ppi_data *ppi = DEVTOSOFTC(ppidev); 239 240 ppb_assert_locked(ppbus); 241 ppi_disable_intr(ppidev); 242 243 switch (ppb_1284_get_state(ppbus)) { 244 /* accept IEEE1284 negotiation then wakeup a waiting process to 245 * continue negotiation at process level */ 246 case PPB_FORWARD_IDLE: 247 /* Event 1 */ 248 if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) == 249 (SELECT | nBUSY)) { 250 /* IEEE1284 negotiation */ 251 #ifdef DEBUG_1284 252 printf("N"); 253 #endif 254 255 /* Event 2 - prepare for reading the ext. value */ 256 ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN); 257 258 ppb_1284_set_state(ppbus, PPB_NEGOCIATION); 259 260 } else { 261 #ifdef DEBUG_1284 262 printf("0x%x", ppb_rstr(ppbus)); 263 #endif 264 ppb_peripheral_terminate(ppbus, PPB_DONTWAIT); 265 break; 266 } 267 268 /* wake up any process waiting for negotiation from 269 * remote master host */ 270 271 /* XXX should set a variable to warn the process about 272 * the interrupt */ 273 274 wakeup(ppi); 275 break; 276 default: 277 #ifdef DEBUG_1284 278 printf("?%d", ppb_1284_get_state(ppbus)); 279 #endif 280 ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE); 281 ppb_set_mode(ppbus, PPB_COMPATIBLE); 282 break; 283 } 284 285 ppi_enable_intr(ppidev); 286 287 return; 288 } 289 #endif /* PERIPH_1284 */ 290 291 static int 292 ppiopen(struct cdev *dev, int flags, int fmt, struct thread *td) 293 { 294 struct ppi_data *ppi = dev->si_drv1; 295 device_t ppidev = ppi->ppi_device; 296 device_t ppbus = device_get_parent(ppidev); 297 int res; 298 299 sx_xlock(&ppi->ppi_lock); 300 if (!(ppi->ppi_flags & HAVE_PPBUS)) { 301 ppb_lock(ppbus); 302 res = ppb_request_bus(ppbus, ppidev, 303 (flags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT | PPB_INTR); 304 ppb_unlock(ppbus); 305 if (res) { 306 sx_xunlock(&ppi->ppi_lock); 307 return (res); 308 } 309 310 ppi->ppi_flags |= HAVE_PPBUS; 311 } 312 sx_xunlock(&ppi->ppi_lock); 313 314 return (0); 315 } 316 317 static int 318 ppiclose(struct cdev *dev, int flags, int fmt, struct thread *td) 319 { 320 struct ppi_data *ppi = dev->si_drv1; 321 device_t ppidev = ppi->ppi_device; 322 device_t ppbus = device_get_parent(ppidev); 323 324 sx_xlock(&ppi->ppi_lock); 325 ppb_lock(ppbus); 326 #ifdef PERIPH_1284 327 switch (ppb_1284_get_state(ppbus)) { 328 case PPB_PERIPHERAL_IDLE: 329 ppb_peripheral_terminate(ppbus, 0); 330 break; 331 case PPB_REVERSE_IDLE: 332 case PPB_EPP_IDLE: 333 case PPB_ECP_FORWARD_IDLE: 334 default: 335 ppb_1284_terminate(ppbus); 336 break; 337 } 338 #endif /* PERIPH_1284 */ 339 340 /* unregistration of interrupt forced by release */ 341 ppb_release_bus(ppbus, ppidev); 342 ppb_unlock(ppbus); 343 344 ppi->ppi_flags &= ~HAVE_PPBUS; 345 sx_xunlock(&ppi->ppi_lock); 346 347 return (0); 348 } 349 350 /* 351 * ppiread() 352 * 353 * IEEE1284 compliant read. 354 * 355 * First, try negotiation to BYTE then NIBBLE mode 356 * If no data is available, wait for it otherwise transfer as much as possible 357 */ 358 static int 359 ppiread(struct cdev *dev, struct uio *uio, int ioflag) 360 { 361 #ifdef PERIPH_1284 362 struct ppi_data *ppi = dev->si_drv1; 363 device_t ppidev = ppi->ppi_device; 364 device_t ppbus = device_get_parent(ppidev); 365 int len, error = 0; 366 char *buffer; 367 368 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK); 369 370 ppb_lock(ppbus); 371 switch (ppb_1284_get_state(ppbus)) { 372 case PPB_PERIPHERAL_IDLE: 373 ppb_peripheral_terminate(ppbus, 0); 374 /* FALLTHROUGH */ 375 376 case PPB_FORWARD_IDLE: 377 /* if can't negotiate NIBBLE mode then try BYTE mode, 378 * the peripheral may be a computer 379 */ 380 if ((ppb_1284_negociate(ppbus, 381 ppi->ppi_mode = PPB_NIBBLE, 0))) { 382 /* XXX Wait 2 seconds to let the remote host some 383 * time to terminate its interrupt 384 */ 385 ppb_sleep(ppbus, ppi, PPBPRI, "ppiread", 2 * hz); 386 387 if ((error = ppb_1284_negociate(ppbus, 388 ppi->ppi_mode = PPB_BYTE, 0))) { 389 ppb_unlock(ppbus); 390 free(buffer, M_DEVBUF); 391 return (error); 392 } 393 } 394 break; 395 396 case PPB_REVERSE_IDLE: 397 case PPB_EPP_IDLE: 398 case PPB_ECP_FORWARD_IDLE: 399 default: 400 break; 401 } 402 403 #ifdef DEBUG_1284 404 printf("N"); 405 #endif 406 /* read data */ 407 len = 0; 408 while (uio->uio_resid) { 409 error = ppb_1284_read(ppbus, ppi->ppi_mode, 410 buffer, min(BUFSIZE, uio->uio_resid), &len); 411 ppb_unlock(ppbus); 412 if (error) 413 goto error; 414 415 if (!len) 416 goto error; /* no more data */ 417 418 #ifdef DEBUG_1284 419 printf("d"); 420 #endif 421 if ((error = uiomove(buffer, len, uio))) 422 goto error; 423 ppb_lock(ppbus); 424 } 425 ppb_unlock(ppbus); 426 427 error: 428 free(buffer, M_DEVBUF); 429 #else /* PERIPH_1284 */ 430 int error = ENODEV; 431 #endif 432 433 return (error); 434 } 435 436 /* 437 * ppiwrite() 438 * 439 * IEEE1284 compliant write 440 * 441 * Actually, this is the peripheral side of a remote IEEE1284 read 442 * 443 * The first part of the negotiation (IEEE1284 device detection) is 444 * done at interrupt level, then the remaining is done by the writing 445 * process 446 * 447 * Once negotiation done, transfer data 448 */ 449 static int 450 ppiwrite(struct cdev *dev, struct uio *uio, int ioflag) 451 { 452 #ifdef PERIPH_1284 453 struct ppi_data *ppi = dev->si_drv1; 454 device_t ppidev = ppi->ppi_device; 455 device_t ppbus = device_get_parent(ppidev); 456 int len, error = 0, sent; 457 char *buffer; 458 459 #if 0 460 int ret; 461 462 #define ADDRESS MS_PARAM(0, 0, MS_TYP_PTR) 463 #define LENGTH MS_PARAM(0, 1, MS_TYP_INT) 464 465 struct ppb_microseq msq[] = { 466 { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } }, 467 MS_RET(0) 468 }; 469 470 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK); 471 ppb_lock(ppbus); 472 473 /* negotiate ECP mode */ 474 if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) { 475 printf("ppiwrite: ECP negotiation failed\n"); 476 } 477 478 while (!error && (len = min(uio->uio_resid, BUFSIZE))) { 479 ppb_unlock(ppbus); 480 uiomove(buffer, len, uio); 481 482 ppb_MS_init_msq(msq, 2, ADDRESS, buffer, LENGTH, len); 483 484 ppb_lock(ppbus); 485 error = ppb_MS_microseq(ppbus, msq, &ret); 486 } 487 #else 488 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK); 489 ppb_lock(ppbus); 490 #endif 491 492 /* we have to be peripheral to be able to send data, so 493 * wait for the appropriate state 494 */ 495 if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION) 496 ppb_1284_terminate(ppbus); 497 498 while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) { 499 /* XXX should check a variable before sleeping */ 500 #ifdef DEBUG_1284 501 printf("s"); 502 #endif 503 504 ppi_enable_intr(ppidev); 505 506 /* sleep until IEEE1284 negotiation starts */ 507 error = ppb_sleep(ppbus, ppi, PCATCH | PPBPRI, "ppiwrite", 0); 508 509 switch (error) { 510 case 0: 511 /* negotiate peripheral side with BYTE mode */ 512 ppb_peripheral_negociate(ppbus, PPB_BYTE, 0); 513 break; 514 case EWOULDBLOCK: 515 break; 516 default: 517 goto error; 518 } 519 } 520 #ifdef DEBUG_1284 521 printf("N"); 522 #endif 523 524 /* negotiation done, write bytes to master host */ 525 while ((len = min(uio->uio_resid, BUFSIZE)) != 0) { 526 ppb_unlock(ppbus); 527 uiomove(buffer, len, uio); 528 ppb_lock(ppbus); 529 if ((error = byte_peripheral_write(ppbus, 530 buffer, len, &sent))) 531 goto error; 532 #ifdef DEBUG_1284 533 printf("d"); 534 #endif 535 } 536 537 error: 538 ppb_unlock(ppbus); 539 free(buffer, M_DEVBUF); 540 #else /* PERIPH_1284 */ 541 int error = ENODEV; 542 #endif 543 544 return (error); 545 } 546 547 static int 548 ppiioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td) 549 { 550 struct ppi_data *ppi = dev->si_drv1; 551 device_t ppidev = ppi->ppi_device; 552 device_t ppbus = device_get_parent(ppidev); 553 int error = 0; 554 u_int8_t *val = (u_int8_t *)data; 555 556 ppb_lock(ppbus); 557 switch (cmd) { 558 case PPIGDATA: /* get data register */ 559 *val = ppb_rdtr(ppbus); 560 break; 561 case PPIGSTATUS: /* get status bits */ 562 *val = ppb_rstr(ppbus); 563 break; 564 case PPIGCTRL: /* get control bits */ 565 *val = ppb_rctr(ppbus); 566 break; 567 case PPIGEPPD: /* get EPP data bits */ 568 *val = ppb_repp_D(ppbus); 569 break; 570 case PPIGECR: /* get ECP bits */ 571 *val = ppb_recr(ppbus); 572 break; 573 case PPIGFIFO: /* read FIFO */ 574 *val = ppb_rfifo(ppbus); 575 break; 576 case PPISDATA: /* set data register */ 577 ppb_wdtr(ppbus, *val); 578 break; 579 case PPISSTATUS: /* set status bits */ 580 ppb_wstr(ppbus, *val); 581 break; 582 case PPISCTRL: /* set control bits */ 583 ppb_wctr(ppbus, *val); 584 break; 585 case PPISEPPD: /* set EPP data bits */ 586 ppb_wepp_D(ppbus, *val); 587 break; 588 case PPISECR: /* set ECP bits */ 589 ppb_wecr(ppbus, *val); 590 break; 591 case PPISFIFO: /* write FIFO */ 592 ppb_wfifo(ppbus, *val); 593 break; 594 case PPIGEPPA: /* get EPP address bits */ 595 *val = ppb_repp_A(ppbus); 596 break; 597 case PPISEPPA: /* set EPP address bits */ 598 ppb_wepp_A(ppbus, *val); 599 break; 600 default: 601 error = ENOTTY; 602 break; 603 } 604 ppb_unlock(ppbus); 605 606 return (error); 607 } 608 609 static device_method_t ppi_methods[] = { 610 /* device interface */ 611 DEVMETHOD(device_identify, ppi_identify), 612 DEVMETHOD(device_probe, ppi_probe), 613 DEVMETHOD(device_attach, ppi_attach), 614 DEVMETHOD(device_detach, ppi_detach), 615 { 0, 0 } 616 }; 617 618 static driver_t ppi_driver = { 619 "ppi", 620 ppi_methods, 621 sizeof(struct ppi_data), 622 }; 623 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0); 624 MODULE_DEPEND(ppi, ppbus, 1, 1, 1); 625