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