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