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