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 /* probe is always ok */ 153 device_set_desc(dev, "Parallel I/O"); 154 155 return (0); 156 } 157 158 /* 159 * ppi_attach() 160 */ 161 static int 162 ppi_attach(device_t dev) 163 { 164 struct ppi_data *ppi = DEVTOSOFTC(dev); 165 #ifdef PERIPH_1284 166 int error, rid = 0; 167 168 /* declare our interrupt handler */ 169 ppi->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 170 RF_ACTIVE); 171 if (ppi->intr_resource) { 172 /* register our interrupt handler */ 173 error = bus_setup_intr(dev, ppi->intr_resource, 174 INTR_TYPE_TTY | INTR_MPSAFE, NULL, ppiintr, dev, 175 &ppi->intr_cookie); 176 if (error) { 177 bus_release_resource(dev, SYS_RES_IRQ, rid, 178 ppi->intr_resource); 179 device_printf(dev, 180 "Unable to register interrupt handler\n"); 181 return (error); 182 } 183 } 184 #endif /* PERIPH_1284 */ 185 186 sx_init(&ppi->ppi_lock, "ppi"); 187 ppi->ppi_cdev = make_dev(&ppi_cdevsw, device_get_unit(dev), 188 UID_ROOT, GID_WHEEL, 189 0600, "ppi%d", device_get_unit(dev)); 190 if (ppi->ppi_cdev == NULL) { 191 device_printf(dev, "Failed to create character device\n"); 192 return (ENXIO); 193 } 194 ppi->ppi_cdev->si_drv1 = ppi; 195 ppi->ppi_device = dev; 196 197 return (0); 198 } 199 200 static int 201 ppi_detach(device_t dev) 202 { 203 struct ppi_data *ppi = DEVTOSOFTC(dev); 204 205 destroy_dev(ppi->ppi_cdev); 206 #ifdef PERIPH_1284 207 if (ppi->intr_resource != NULL) { 208 bus_teardown_intr(dev, ppi->intr_resource, ppi->intr_cookie); 209 bus_release_resource(dev, SYS_RES_IRQ, 0, ppi->intr_resource); 210 } 211 #endif 212 sx_destroy(&ppi->ppi_lock); 213 return (0); 214 } 215 216 #ifdef PERIPH_1284 217 /* 218 * Cable 219 * ----- 220 * 221 * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks: 222 * 223 * nStrobe <-> nAck 1 <-> 10 224 * nAutofd <-> Busy 11 <-> 14 225 * nSelectin <-> Select 17 <-> 13 226 * nInit <-> nFault 15 <-> 16 227 * 228 */ 229 static void 230 ppiintr(void *arg) 231 { 232 device_t ppidev = (device_t)arg; 233 device_t ppbus = device_get_parent(ppidev); 234 struct ppi_data *ppi = DEVTOSOFTC(ppidev); 235 236 ppb_assert_locked(ppbus); 237 ppi_disable_intr(ppidev); 238 239 switch (ppb_1284_get_state(ppbus)) { 240 /* accept IEEE1284 negotiation then wakeup a waiting process to 241 * continue negotiation at process level */ 242 case PPB_FORWARD_IDLE: 243 /* Event 1 */ 244 if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) == 245 (SELECT | nBUSY)) { 246 /* IEEE1284 negotiation */ 247 #ifdef DEBUG_1284 248 printf("N"); 249 #endif 250 251 /* Event 2 - prepare for reading the ext. value */ 252 ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN); 253 254 ppb_1284_set_state(ppbus, PPB_NEGOCIATION); 255 256 } else { 257 #ifdef DEBUG_1284 258 printf("0x%x", ppb_rstr(ppbus)); 259 #endif 260 ppb_peripheral_terminate(ppbus, PPB_DONTWAIT); 261 break; 262 } 263 264 /* wake up any process waiting for negotiation from 265 * remote master host */ 266 267 /* XXX should set a variable to warn the process about 268 * the interrupt */ 269 270 wakeup(ppi); 271 break; 272 default: 273 #ifdef DEBUG_1284 274 printf("?%d", ppb_1284_get_state(ppbus)); 275 #endif 276 ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE); 277 ppb_set_mode(ppbus, PPB_COMPATIBLE); 278 break; 279 } 280 281 ppi_enable_intr(ppidev); 282 283 return; 284 } 285 #endif /* PERIPH_1284 */ 286 287 static int 288 ppiopen(struct cdev *dev, int flags, int fmt, struct thread *td) 289 { 290 struct ppi_data *ppi = dev->si_drv1; 291 device_t ppidev = ppi->ppi_device; 292 device_t ppbus = device_get_parent(ppidev); 293 int res; 294 295 sx_xlock(&ppi->ppi_lock); 296 if (!(ppi->ppi_flags & HAVE_PPBUS)) { 297 ppb_lock(ppbus); 298 res = ppb_request_bus(ppbus, ppidev, 299 (flags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT | PPB_INTR); 300 ppb_unlock(ppbus); 301 if (res) { 302 sx_xunlock(&ppi->ppi_lock); 303 return (res); 304 } 305 306 ppi->ppi_flags |= HAVE_PPBUS; 307 } 308 sx_xunlock(&ppi->ppi_lock); 309 310 return (0); 311 } 312 313 static int 314 ppiclose(struct cdev *dev, int flags, int fmt, struct thread *td) 315 { 316 struct ppi_data *ppi = dev->si_drv1; 317 device_t ppidev = ppi->ppi_device; 318 device_t ppbus = device_get_parent(ppidev); 319 320 sx_xlock(&ppi->ppi_lock); 321 ppb_lock(ppbus); 322 #ifdef PERIPH_1284 323 switch (ppb_1284_get_state(ppbus)) { 324 case PPB_PERIPHERAL_IDLE: 325 ppb_peripheral_terminate(ppbus, 0); 326 break; 327 case PPB_REVERSE_IDLE: 328 case PPB_EPP_IDLE: 329 case PPB_ECP_FORWARD_IDLE: 330 default: 331 ppb_1284_terminate(ppbus); 332 break; 333 } 334 #endif /* PERIPH_1284 */ 335 336 /* unregistration of interrupt forced by release */ 337 ppb_release_bus(ppbus, ppidev); 338 ppb_unlock(ppbus); 339 340 ppi->ppi_flags &= ~HAVE_PPBUS; 341 sx_xunlock(&ppi->ppi_lock); 342 343 return (0); 344 } 345 346 /* 347 * ppiread() 348 * 349 * IEEE1284 compliant read. 350 * 351 * First, try negotiation to BYTE then NIBBLE mode 352 * If no data is available, wait for it otherwise transfer as much as possible 353 */ 354 static int 355 ppiread(struct cdev *dev, struct uio *uio, int ioflag) 356 { 357 #ifdef PERIPH_1284 358 struct ppi_data *ppi = dev->si_drv1; 359 device_t ppidev = ppi->ppi_device; 360 device_t ppbus = device_get_parent(ppidev); 361 int len, error = 0; 362 char *buffer; 363 364 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK); 365 366 ppb_lock(ppbus); 367 switch (ppb_1284_get_state(ppbus)) { 368 case PPB_PERIPHERAL_IDLE: 369 ppb_peripheral_terminate(ppbus, 0); 370 /* FALLTHROUGH */ 371 372 case PPB_FORWARD_IDLE: 373 /* if can't negotiate NIBBLE mode then try BYTE mode, 374 * the peripheral may be a computer 375 */ 376 if ((ppb_1284_negociate(ppbus, 377 ppi->ppi_mode = PPB_NIBBLE, 0))) { 378 /* XXX Wait 2 seconds to let the remote host some 379 * time to terminate its interrupt 380 */ 381 ppb_sleep(ppbus, ppi, PPBPRI, "ppiread", 2 * hz); 382 383 if ((error = ppb_1284_negociate(ppbus, 384 ppi->ppi_mode = PPB_BYTE, 0))) { 385 ppb_unlock(ppbus); 386 free(buffer, M_DEVBUF); 387 return (error); 388 } 389 } 390 break; 391 392 case PPB_REVERSE_IDLE: 393 case PPB_EPP_IDLE: 394 case PPB_ECP_FORWARD_IDLE: 395 default: 396 break; 397 } 398 399 #ifdef DEBUG_1284 400 printf("N"); 401 #endif 402 /* read data */ 403 len = 0; 404 while (uio->uio_resid) { 405 error = ppb_1284_read(ppbus, ppi->ppi_mode, 406 buffer, min(BUFSIZE, uio->uio_resid), &len); 407 ppb_unlock(ppbus); 408 if (error) 409 goto error; 410 411 if (!len) 412 goto error; /* no more data */ 413 414 #ifdef DEBUG_1284 415 printf("d"); 416 #endif 417 if ((error = uiomove(buffer, len, uio))) 418 goto error; 419 ppb_lock(ppbus); 420 } 421 ppb_unlock(ppbus); 422 423 error: 424 free(buffer, M_DEVBUF); 425 #else /* PERIPH_1284 */ 426 int error = ENODEV; 427 #endif 428 429 return (error); 430 } 431 432 /* 433 * ppiwrite() 434 * 435 * IEEE1284 compliant write 436 * 437 * Actually, this is the peripheral side of a remote IEEE1284 read 438 * 439 * The first part of the negotiation (IEEE1284 device detection) is 440 * done at interrupt level, then the remaining is done by the writing 441 * process 442 * 443 * Once negotiation done, transfer data 444 */ 445 static int 446 ppiwrite(struct cdev *dev, struct uio *uio, int ioflag) 447 { 448 #ifdef PERIPH_1284 449 struct ppi_data *ppi = dev->si_drv1; 450 device_t ppidev = ppi->ppi_device; 451 device_t ppbus = device_get_parent(ppidev); 452 int len, error = 0, sent; 453 char *buffer; 454 455 #if 0 456 int ret; 457 458 #define ADDRESS MS_PARAM(0, 0, MS_TYP_PTR) 459 #define LENGTH MS_PARAM(0, 1, MS_TYP_INT) 460 461 struct ppb_microseq msq[] = { 462 { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } }, 463 MS_RET(0) 464 }; 465 466 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK); 467 ppb_lock(ppbus); 468 469 /* negotiate ECP mode */ 470 if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) { 471 printf("ppiwrite: ECP negotiation failed\n"); 472 } 473 474 while (!error && (len = min(uio->uio_resid, BUFSIZE))) { 475 ppb_unlock(ppbus); 476 uiomove(buffer, len, uio); 477 478 ppb_MS_init_msq(msq, 2, ADDRESS, buffer, LENGTH, len); 479 480 ppb_lock(ppbus); 481 error = ppb_MS_microseq(ppbus, msq, &ret); 482 } 483 #else 484 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK); 485 ppb_lock(ppbus); 486 #endif 487 488 /* we have to be peripheral to be able to send data, so 489 * wait for the appropriate state 490 */ 491 if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION) 492 ppb_1284_terminate(ppbus); 493 494 while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) { 495 /* XXX should check a variable before sleeping */ 496 #ifdef DEBUG_1284 497 printf("s"); 498 #endif 499 500 ppi_enable_intr(ppidev); 501 502 /* sleep until IEEE1284 negotiation starts */ 503 error = ppb_sleep(ppbus, ppi, PCATCH | PPBPRI, "ppiwrite", 0); 504 505 switch (error) { 506 case 0: 507 /* negotiate peripheral side with BYTE mode */ 508 ppb_peripheral_negociate(ppbus, PPB_BYTE, 0); 509 break; 510 case EWOULDBLOCK: 511 break; 512 default: 513 goto error; 514 } 515 } 516 #ifdef DEBUG_1284 517 printf("N"); 518 #endif 519 520 /* negotiation done, write bytes to master host */ 521 while ((len = min(uio->uio_resid, BUFSIZE)) != 0) { 522 ppb_unlock(ppbus); 523 uiomove(buffer, len, uio); 524 ppb_lock(ppbus); 525 if ((error = byte_peripheral_write(ppbus, 526 buffer, len, &sent))) 527 goto error; 528 #ifdef DEBUG_1284 529 printf("d"); 530 #endif 531 } 532 533 error: 534 ppb_unlock(ppbus); 535 free(buffer, M_DEVBUF); 536 #else /* PERIPH_1284 */ 537 int error = ENODEV; 538 #endif 539 540 return (error); 541 } 542 543 static int 544 ppiioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td) 545 { 546 struct ppi_data *ppi = dev->si_drv1; 547 device_t ppidev = ppi->ppi_device; 548 device_t ppbus = device_get_parent(ppidev); 549 int error = 0; 550 u_int8_t *val = (u_int8_t *)data; 551 552 ppb_lock(ppbus); 553 switch (cmd) { 554 case PPIGDATA: /* get data register */ 555 *val = ppb_rdtr(ppbus); 556 break; 557 case PPIGSTATUS: /* get status bits */ 558 *val = ppb_rstr(ppbus); 559 break; 560 case PPIGCTRL: /* get control bits */ 561 *val = ppb_rctr(ppbus); 562 break; 563 case PPIGEPPD: /* get EPP data bits */ 564 *val = ppb_repp_D(ppbus); 565 break; 566 case PPIGECR: /* get ECP bits */ 567 *val = ppb_recr(ppbus); 568 break; 569 case PPIGFIFO: /* read FIFO */ 570 *val = ppb_rfifo(ppbus); 571 break; 572 case PPISDATA: /* set data register */ 573 ppb_wdtr(ppbus, *val); 574 break; 575 case PPISSTATUS: /* set status bits */ 576 ppb_wstr(ppbus, *val); 577 break; 578 case PPISCTRL: /* set control bits */ 579 ppb_wctr(ppbus, *val); 580 break; 581 case PPISEPPD: /* set EPP data bits */ 582 ppb_wepp_D(ppbus, *val); 583 break; 584 case PPISECR: /* set ECP bits */ 585 ppb_wecr(ppbus, *val); 586 break; 587 case PPISFIFO: /* write FIFO */ 588 ppb_wfifo(ppbus, *val); 589 break; 590 case PPIGEPPA: /* get EPP address bits */ 591 *val = ppb_repp_A(ppbus); 592 break; 593 case PPISEPPA: /* set EPP address bits */ 594 ppb_wepp_A(ppbus, *val); 595 break; 596 default: 597 error = ENOTTY; 598 break; 599 } 600 ppb_unlock(ppbus); 601 602 return (error); 603 } 604 605 static device_method_t ppi_methods[] = { 606 /* device interface */ 607 DEVMETHOD(device_identify, ppi_identify), 608 DEVMETHOD(device_probe, ppi_probe), 609 DEVMETHOD(device_attach, ppi_attach), 610 DEVMETHOD(device_detach, ppi_detach), 611 { 0, 0 } 612 }; 613 614 static driver_t ppi_driver = { 615 "ppi", 616 ppi_methods, 617 sizeof(struct ppi_data), 618 }; 619 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0); 620 MODULE_DEPEND(ppi, ppbus, 1, 1, 1); 621