2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu, Michael Smith
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 #include "opt_ppb_1284.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/module.h>
40 #include <sys/kernel.h>
44 #include <sys/fcntl.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
50 #include <dev/ppbus/ppbconf.h>
51 #include <dev/ppbus/ppb_msq.h>
54 #include <sys/malloc.h>
55 #include <dev/ppbus/ppb_1284.h>
58 #include <dev/ppbus/ppi.h>
62 #include <dev/ppbus/ppbio.h>
68 struct cdev *ppi_cdev;
71 #define HAVE_PPBUS (1<<0)
73 int ppi_mode; /* IEEE1284 mode */
74 char ppi_buffer[BUFSIZE];
77 struct resource *intr_resource; /* interrupt resource */
78 void *intr_cookie; /* interrupt registration cookie */
79 #endif /* PERIPH_1284 */
82 #define DEVTOSOFTC(dev) \
83 ((struct ppi_data *)device_get_softc(dev))
85 static devclass_t ppi_devclass;
88 static void ppiintr(void *arg);
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;
97 static struct cdevsw ppi_cdevsw = {
98 .d_version = D_VERSION,
110 ppi_enable_intr(device_t ppidev)
113 device_t ppbus = device_get_parent(ppidev);
116 ppb_wctr(ppbus, r | IRQENABLE);
122 ppi_disable_intr(device_t ppidev)
125 device_t ppbus = device_get_parent(ppidev);
128 ppb_wctr(ppbus, r & ~IRQENABLE);
133 #endif /* PERIPH_1284 */
136 ppi_identify(driver_t *driver, device_t parent)
141 dev = device_find_child(parent, "ppi", -1);
143 BUS_ADD_CHILD(parent, 0, "ppi", -1);
150 ppi_probe(device_t dev)
152 struct ppi_data *ppi;
154 /* probe is always ok */
155 device_set_desc(dev, "Parallel I/O");
157 ppi = DEVTOSOFTC(dev);
166 ppi_attach(device_t dev)
168 struct ppi_data *ppi = DEVTOSOFTC(dev);
172 /* declare our interrupt handler */
173 ppi->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
175 if (ppi->intr_resource) {
176 /* register our interrupt handler */
177 error = bus_setup_intr(dev, ppi->intr_resource,
178 INTR_TYPE_TTY | INTR_MPSAFE, NULL, ppiintr, dev,
181 bus_release_resource(dev, SYS_RES_IRQ, rid,
184 "Unable to register interrupt handler\n");
188 #endif /* PERIPH_1284 */
190 sx_init(&ppi->ppi_lock, "ppi");
191 ppi->ppi_cdev = make_dev(&ppi_cdevsw, device_get_unit(dev),
193 0600, "ppi%d", device_get_unit(dev));
194 if (ppi->ppi_cdev == NULL) {
195 device_printf(dev, "Failed to create character device\n");
198 ppi->ppi_cdev->si_drv1 = ppi;
199 ppi->ppi_device = dev;
205 ppi_detach(device_t dev)
207 struct ppi_data *ppi = DEVTOSOFTC(dev);
209 destroy_dev(ppi->ppi_cdev);
211 if (ppi->intr_resource != NULL) {
212 bus_teardown_intr(dev, ppi->intr_resource, ppi->intr_cookie);
213 bus_release_resource(dev, SYS_RES_IRQ, 0, ppi->intr_resource);
216 sx_destroy(&ppi->ppi_lock);
225 * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
227 * nStrobe <-> nAck 1 <-> 10
228 * nAutofd <-> Busy 11 <-> 14
229 * nSelectin <-> Select 17 <-> 13
230 * nInit <-> nFault 15 <-> 16
236 device_t ppidev = (device_t)arg;
237 device_t ppbus = device_get_parent(ppidev);
238 struct ppi_data *ppi = DEVTOSOFTC(ppidev);
240 ppb_assert_locked(ppbus);
241 ppi_disable_intr(ppidev);
243 switch (ppb_1284_get_state(ppbus)) {
245 /* accept IEEE1284 negotiation then wakeup a waiting process to
246 * continue negotiation at process level */
247 case PPB_FORWARD_IDLE:
249 if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
251 /* IEEE1284 negotiation */
256 /* Event 2 - prepare for reading the ext. value */
257 ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
259 ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
263 printf("0x%x", ppb_rstr(ppbus));
265 ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
269 /* wake up any process waiting for negotiation from
270 * remote master host */
272 /* XXX should set a variable to warn the process about
279 printf("?%d", ppb_1284_get_state(ppbus));
281 ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
282 ppb_set_mode(ppbus, PPB_COMPATIBLE);
286 ppi_enable_intr(ppidev);
290 #endif /* PERIPH_1284 */
293 ppiopen(struct cdev *dev, int flags, int fmt, struct thread *td)
295 struct ppi_data *ppi = dev->si_drv1;
296 device_t ppidev = ppi->ppi_device;
297 device_t ppbus = device_get_parent(ppidev);
300 sx_xlock(&ppi->ppi_lock);
301 if (!(ppi->ppi_flags & HAVE_PPBUS)) {
303 res = ppb_request_bus(ppbus, ppidev,
304 (flags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT | PPB_INTR);
307 sx_xunlock(&ppi->ppi_lock);
311 ppi->ppi_flags |= HAVE_PPBUS;
313 sx_xunlock(&ppi->ppi_lock);
319 ppiclose(struct cdev *dev, int flags, int fmt, struct thread *td)
321 struct ppi_data *ppi = dev->si_drv1;
322 device_t ppidev = ppi->ppi_device;
323 device_t ppbus = device_get_parent(ppidev);
325 sx_xlock(&ppi->ppi_lock);
328 switch (ppb_1284_get_state(ppbus)) {
329 case PPB_PERIPHERAL_IDLE:
330 ppb_peripheral_terminate(ppbus, 0);
332 case PPB_REVERSE_IDLE:
334 case PPB_ECP_FORWARD_IDLE:
336 ppb_1284_terminate(ppbus);
339 #endif /* PERIPH_1284 */
341 /* unregistration of interrupt forced by release */
342 ppb_release_bus(ppbus, ppidev);
345 ppi->ppi_flags &= ~HAVE_PPBUS;
346 sx_xunlock(&ppi->ppi_lock);
354 * IEEE1284 compliant read.
356 * First, try negotiation to BYTE then NIBBLE mode
357 * If no data is available, wait for it otherwise transfer as much as possible
360 ppiread(struct cdev *dev, struct uio *uio, int ioflag)
363 struct ppi_data *ppi = dev->si_drv1;
364 device_t ppidev = ppi->ppi_device;
365 device_t ppbus = device_get_parent(ppidev);
369 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
372 switch (ppb_1284_get_state(ppbus)) {
373 case PPB_PERIPHERAL_IDLE:
374 ppb_peripheral_terminate(ppbus, 0);
377 case PPB_FORWARD_IDLE:
378 /* if can't negotiate NIBBLE mode then try BYTE mode,
379 * the peripheral may be a computer
381 if ((ppb_1284_negociate(ppbus,
382 ppi->ppi_mode = PPB_NIBBLE, 0))) {
384 /* XXX Wait 2 seconds to let the remote host some
385 * time to terminate its interrupt
387 ppb_sleep(ppbus, ppi, PPBPRI, "ppiread", 2 * hz);
389 if ((error = ppb_1284_negociate(ppbus,
390 ppi->ppi_mode = PPB_BYTE, 0))) {
392 free(buffer, M_DEVBUF);
398 case PPB_REVERSE_IDLE:
400 case PPB_ECP_FORWARD_IDLE:
410 while (uio->uio_resid) {
411 error = ppb_1284_read(ppbus, ppi->ppi_mode,
412 buffer, min(BUFSIZE, uio->uio_resid), &len);
418 goto error; /* no more data */
423 if ((error = uiomove(buffer, len, uio)))
430 free(buffer, M_DEVBUF);
431 #else /* PERIPH_1284 */
441 * IEEE1284 compliant write
443 * Actually, this is the peripheral side of a remote IEEE1284 read
445 * The first part of the negotiation (IEEE1284 device detection) is
446 * done at interrupt level, then the remaining is done by the writing
449 * Once negotiation done, transfer data
452 ppiwrite(struct cdev *dev, struct uio *uio, int ioflag)
455 struct ppi_data *ppi = dev->si_drv1;
456 device_t ppidev = ppi->ppi_device;
457 device_t ppbus = device_get_parent(ppidev);
458 int len, error = 0, sent;
464 #define ADDRESS MS_PARAM(0, 0, MS_TYP_PTR)
465 #define LENGTH MS_PARAM(0, 1, MS_TYP_INT)
467 struct ppb_microseq msq[] = {
468 { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
472 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
475 /* negotiate ECP mode */
476 if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
477 printf("ppiwrite: ECP negotiation failed\n");
480 while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
482 uiomove(buffer, len, uio);
484 ppb_MS_init_msq(msq, 2, ADDRESS, buffer, LENGTH, len);
487 error = ppb_MS_microseq(ppbus, msq, &ret);
490 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
494 /* we have to be peripheral to be able to send data, so
495 * wait for the appropriate state
497 if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
498 ppb_1284_terminate(ppbus);
500 while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
501 /* XXX should check a variable before sleeping */
506 ppi_enable_intr(ppidev);
508 /* sleep until IEEE1284 negotiation starts */
509 error = ppb_sleep(ppbus, ppi, PCATCH | PPBPRI, "ppiwrite", 0);
513 /* negotiate peripheral side with BYTE mode */
514 ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
526 /* negotiation done, write bytes to master host */
527 while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
529 uiomove(buffer, len, uio);
531 if ((error = byte_peripheral_write(ppbus,
532 buffer, len, &sent)))
541 free(buffer, M_DEVBUF);
542 #else /* PERIPH_1284 */
550 ppiioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
552 struct ppi_data *ppi = dev->si_drv1;
553 device_t ppidev = ppi->ppi_device;
554 device_t ppbus = device_get_parent(ppidev);
556 u_int8_t *val = (u_int8_t *)data;
561 case PPIGDATA: /* get data register */
562 *val = ppb_rdtr(ppbus);
564 case PPIGSTATUS: /* get status bits */
565 *val = ppb_rstr(ppbus);
567 case PPIGCTRL: /* get control bits */
568 *val = ppb_rctr(ppbus);
570 case PPIGEPPD: /* get EPP data bits */
571 *val = ppb_repp_D(ppbus);
573 case PPIGECR: /* get ECP bits */
574 *val = ppb_recr(ppbus);
576 case PPIGFIFO: /* read FIFO */
577 *val = ppb_rfifo(ppbus);
579 case PPISDATA: /* set data register */
580 ppb_wdtr(ppbus, *val);
582 case PPISSTATUS: /* set status bits */
583 ppb_wstr(ppbus, *val);
585 case PPISCTRL: /* set control bits */
586 ppb_wctr(ppbus, *val);
588 case PPISEPPD: /* set EPP data bits */
589 ppb_wepp_D(ppbus, *val);
591 case PPISECR: /* set ECP bits */
592 ppb_wecr(ppbus, *val);
594 case PPISFIFO: /* write FIFO */
595 ppb_wfifo(ppbus, *val);
597 case PPIGEPPA: /* get EPP address bits */
598 *val = ppb_repp_A(ppbus);
600 case PPISEPPA: /* set EPP address bits */
601 ppb_wepp_A(ppbus, *val);
612 static device_method_t ppi_methods[] = {
613 /* device interface */
614 DEVMETHOD(device_identify, ppi_identify),
615 DEVMETHOD(device_probe, ppi_probe),
616 DEVMETHOD(device_attach, ppi_attach),
617 DEVMETHOD(device_detach, ppi_detach),
622 static driver_t ppi_driver = {
625 sizeof(struct ppi_data),
627 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);
628 MODULE_DEPEND(ppi, ppbus, 1, 1, 1);