2 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu, Michael Smith
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include "opt_ppb_1284.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
38 #include <sys/kernel.h>
42 #include <sys/fcntl.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
48 #include <dev/ppbus/ppbconf.h>
49 #include <dev/ppbus/ppb_msq.h>
52 #include <sys/malloc.h>
53 #include <dev/ppbus/ppb_1284.h>
56 #include <dev/ppbus/ppi.h>
60 #include <dev/ppbus/ppbio.h>
66 struct cdev *ppi_cdev;
69 #define HAVE_PPBUS (1<<0)
71 int ppi_mode; /* IEEE1284 mode */
72 char ppi_buffer[BUFSIZE];
75 struct resource *intr_resource; /* interrupt resource */
76 void *intr_cookie; /* interrupt registration cookie */
77 #endif /* PERIPH_1284 */
80 #define DEVTOSOFTC(dev) \
81 ((struct ppi_data *)device_get_softc(dev))
83 static devclass_t ppi_devclass;
86 static void ppiintr(void *arg);
89 static d_open_t ppiopen;
90 static d_close_t ppiclose;
91 static d_ioctl_t ppiioctl;
92 static d_write_t ppiwrite;
93 static d_read_t ppiread;
95 static struct cdevsw ppi_cdevsw = {
96 .d_version = D_VERSION,
108 ppi_enable_intr(device_t ppidev)
111 device_t ppbus = device_get_parent(ppidev);
114 ppb_wctr(ppbus, r | IRQENABLE);
120 ppi_disable_intr(device_t ppidev)
123 device_t ppbus = device_get_parent(ppidev);
126 ppb_wctr(ppbus, r & ~IRQENABLE);
131 #endif /* PERIPH_1284 */
134 ppi_identify(driver_t *driver, device_t parent)
139 dev = device_find_child(parent, "ppi", -1);
141 BUS_ADD_CHILD(parent, 0, "ppi", -1);
148 ppi_probe(device_t dev)
150 struct ppi_data *ppi;
152 /* probe is always ok */
153 device_set_desc(dev, "Parallel I/O");
155 ppi = DEVTOSOFTC(dev);
164 ppi_attach(device_t dev)
166 struct ppi_data *ppi = DEVTOSOFTC(dev);
170 /* declare our interrupt handler */
171 ppi->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
173 if (ppi->intr_resource) {
174 /* register our interrupt handler */
175 error = bus_setup_intr(dev, ppi->intr_resource,
176 INTR_TYPE_TTY | INTR_MPSAFE, NULL, ppiintr, dev,
179 bus_release_resource(dev, SYS_RES_IRQ, rid,
182 "Unable to register interrupt handler\n");
186 #endif /* PERIPH_1284 */
188 sx_init(&ppi->ppi_lock, "ppi");
189 ppi->ppi_cdev = make_dev(&ppi_cdevsw, device_get_unit(dev),
191 0600, "ppi%d", device_get_unit(dev));
192 if (ppi->ppi_cdev == NULL) {
193 device_printf(dev, "Failed to create character device\n");
196 ppi->ppi_cdev->si_drv1 = ppi;
197 ppi->ppi_device = dev;
203 ppi_detach(device_t dev)
205 struct ppi_data *ppi = DEVTOSOFTC(dev);
207 destroy_dev(ppi->ppi_cdev);
209 if (ppi->intr_resource != NULL) {
210 bus_teardown_intr(dev, ppi->intr_resource, ppi->intr_cookie);
211 bus_release_resource(dev, SYS_RES_IRQ, 0, ppi->intr_resource);
214 sx_destroy(&ppi->ppi_lock);
223 * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
225 * nStrobe <-> nAck 1 <-> 10
226 * nAutofd <-> Busy 11 <-> 14
227 * nSelectin <-> Select 17 <-> 13
228 * nInit <-> nFault 15 <-> 16
234 device_t ppidev = (device_t)arg;
235 device_t ppbus = device_get_parent(ppidev);
236 struct ppi_data *ppi = DEVTOSOFTC(ppidev);
238 ppb_assert_locked(ppbus);
239 ppi_disable_intr(ppidev);
241 switch (ppb_1284_get_state(ppbus)) {
243 /* accept IEEE1284 negotiation then wakeup a waiting process to
244 * continue negotiation at process level */
245 case PPB_FORWARD_IDLE:
247 if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
249 /* IEEE1284 negotiation */
254 /* Event 2 - prepare for reading the ext. value */
255 ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
257 ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
261 printf("0x%x", ppb_rstr(ppbus));
263 ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
267 /* wake up any process waiting for negotiation from
268 * remote master host */
270 /* XXX should set a variable to warn the process about
277 printf("?%d", ppb_1284_get_state(ppbus));
279 ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
280 ppb_set_mode(ppbus, PPB_COMPATIBLE);
284 ppi_enable_intr(ppidev);
288 #endif /* PERIPH_1284 */
291 ppiopen(struct cdev *dev, int flags, int fmt, struct thread *td)
293 struct ppi_data *ppi = dev->si_drv1;
294 device_t ppidev = ppi->ppi_device;
295 device_t ppbus = device_get_parent(ppidev);
298 sx_xlock(&ppi->ppi_lock);
299 if (!(ppi->ppi_flags & HAVE_PPBUS)) {
301 res = ppb_request_bus(ppbus, ppidev,
302 (flags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT | PPB_INTR);
305 sx_xunlock(&ppi->ppi_lock);
309 ppi->ppi_flags |= HAVE_PPBUS;
311 sx_xunlock(&ppi->ppi_lock);
317 ppiclose(struct cdev *dev, int flags, int fmt, struct thread *td)
319 struct ppi_data *ppi = dev->si_drv1;
320 device_t ppidev = ppi->ppi_device;
321 device_t ppbus = device_get_parent(ppidev);
323 sx_xlock(&ppi->ppi_lock);
326 switch (ppb_1284_get_state(ppbus)) {
327 case PPB_PERIPHERAL_IDLE:
328 ppb_peripheral_terminate(ppbus, 0);
330 case PPB_REVERSE_IDLE:
332 case PPB_ECP_FORWARD_IDLE:
334 ppb_1284_terminate(ppbus);
337 #endif /* PERIPH_1284 */
339 /* unregistration of interrupt forced by release */
340 ppb_release_bus(ppbus, ppidev);
343 ppi->ppi_flags &= ~HAVE_PPBUS;
344 sx_xunlock(&ppi->ppi_lock);
352 * IEEE1284 compliant read.
354 * First, try negotiation to BYTE then NIBBLE mode
355 * If no data is available, wait for it otherwise transfer as much as possible
358 ppiread(struct cdev *dev, struct uio *uio, int ioflag)
361 struct ppi_data *ppi = dev->si_drv1;
362 device_t ppidev = ppi->ppi_device;
363 device_t ppbus = device_get_parent(ppidev);
367 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
370 switch (ppb_1284_get_state(ppbus)) {
371 case PPB_PERIPHERAL_IDLE:
372 ppb_peripheral_terminate(ppbus, 0);
375 case PPB_FORWARD_IDLE:
376 /* if can't negotiate NIBBLE mode then try BYTE mode,
377 * the peripheral may be a computer
379 if ((ppb_1284_negociate(ppbus,
380 ppi->ppi_mode = PPB_NIBBLE, 0))) {
382 /* XXX Wait 2 seconds to let the remote host some
383 * time to terminate its interrupt
385 ppb_sleep(ppbus, ppi, PPBPRI, "ppiread", 2 * hz);
387 if ((error = ppb_1284_negociate(ppbus,
388 ppi->ppi_mode = PPB_BYTE, 0))) {
390 free(buffer, M_DEVBUF);
396 case PPB_REVERSE_IDLE:
398 case PPB_ECP_FORWARD_IDLE:
408 while (uio->uio_resid) {
409 error = ppb_1284_read(ppbus, ppi->ppi_mode,
410 buffer, min(BUFSIZE, uio->uio_resid), &len);
416 goto error; /* no more data */
421 if ((error = uiomove(buffer, len, uio)))
428 free(buffer, M_DEVBUF);
429 #else /* PERIPH_1284 */
439 * IEEE1284 compliant write
441 * Actually, this is the peripheral side of a remote IEEE1284 read
443 * The first part of the negotiation (IEEE1284 device detection) is
444 * done at interrupt level, then the remaining is done by the writing
447 * Once negotiation done, transfer data
450 ppiwrite(struct cdev *dev, struct uio *uio, int ioflag)
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;
462 #define ADDRESS MS_PARAM(0, 0, MS_TYP_PTR)
463 #define LENGTH MS_PARAM(0, 1, MS_TYP_INT)
465 struct ppb_microseq msq[] = {
466 { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
470 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
473 /* negotiate ECP mode */
474 if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
475 printf("ppiwrite: ECP negotiation failed\n");
478 while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
480 uiomove(buffer, len, uio);
482 ppb_MS_init_msq(msq, 2, ADDRESS, buffer, LENGTH, len);
485 error = ppb_MS_microseq(ppbus, msq, &ret);
488 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
492 /* we have to be peripheral to be able to send data, so
493 * wait for the appropriate state
495 if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
496 ppb_1284_terminate(ppbus);
498 while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
499 /* XXX should check a variable before sleeping */
504 ppi_enable_intr(ppidev);
506 /* sleep until IEEE1284 negotiation starts */
507 error = ppb_sleep(ppbus, ppi, PCATCH | PPBPRI, "ppiwrite", 0);
511 /* negotiate peripheral side with BYTE mode */
512 ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
524 /* negotiation done, write bytes to master host */
525 while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
527 uiomove(buffer, len, uio);
529 if ((error = byte_peripheral_write(ppbus,
530 buffer, len, &sent)))
539 free(buffer, M_DEVBUF);
540 #else /* PERIPH_1284 */
548 ppiioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
550 struct ppi_data *ppi = dev->si_drv1;
551 device_t ppidev = ppi->ppi_device;
552 device_t ppbus = device_get_parent(ppidev);
554 u_int8_t *val = (u_int8_t *)data;
559 case PPIGDATA: /* get data register */
560 *val = ppb_rdtr(ppbus);
562 case PPIGSTATUS: /* get status bits */
563 *val = ppb_rstr(ppbus);
565 case PPIGCTRL: /* get control bits */
566 *val = ppb_rctr(ppbus);
568 case PPIGEPPD: /* get EPP data bits */
569 *val = ppb_repp_D(ppbus);
571 case PPIGECR: /* get ECP bits */
572 *val = ppb_recr(ppbus);
574 case PPIGFIFO: /* read FIFO */
575 *val = ppb_rfifo(ppbus);
577 case PPISDATA: /* set data register */
578 ppb_wdtr(ppbus, *val);
580 case PPISSTATUS: /* set status bits */
581 ppb_wstr(ppbus, *val);
583 case PPISCTRL: /* set control bits */
584 ppb_wctr(ppbus, *val);
586 case PPISEPPD: /* set EPP data bits */
587 ppb_wepp_D(ppbus, *val);
589 case PPISECR: /* set ECP bits */
590 ppb_wecr(ppbus, *val);
592 case PPISFIFO: /* write FIFO */
593 ppb_wfifo(ppbus, *val);
595 case PPIGEPPA: /* get EPP address bits */
596 *val = ppb_repp_A(ppbus);
598 case PPISEPPA: /* set EPP address bits */
599 ppb_wepp_A(ppbus, *val);
610 static device_method_t ppi_methods[] = {
611 /* device interface */
612 DEVMETHOD(device_identify, ppi_identify),
613 DEVMETHOD(device_probe, ppi_probe),
614 DEVMETHOD(device_attach, ppi_attach),
615 DEVMETHOD(device_detach, ppi_detach),
620 static driver_t ppi_driver = {
623 sizeof(struct ppi_data),
625 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);
626 MODULE_DEPEND(ppi, ppbus, 1, 1, 1);