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)) {
244 /* accept IEEE1284 negotiation then wakeup a waiting process to
245 * continue negotiation at process level */
246 case PPB_FORWARD_IDLE:
248 if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
250 /* IEEE1284 negotiation */
255 /* Event 2 - prepare for reading the ext. value */
256 ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
258 ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
262 printf("0x%x", ppb_rstr(ppbus));
264 ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
268 /* wake up any process waiting for negotiation from
269 * remote master host */
271 /* XXX should set a variable to warn the process about
278 printf("?%d", ppb_1284_get_state(ppbus));
280 ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
281 ppb_set_mode(ppbus, PPB_COMPATIBLE);
285 ppi_enable_intr(ppidev);
289 #endif /* PERIPH_1284 */
292 ppiopen(struct cdev *dev, int flags, int fmt, struct thread *td)
294 struct ppi_data *ppi = dev->si_drv1;
295 device_t ppidev = ppi->ppi_device;
296 device_t ppbus = device_get_parent(ppidev);
299 sx_xlock(&ppi->ppi_lock);
300 if (!(ppi->ppi_flags & HAVE_PPBUS)) {
302 res = ppb_request_bus(ppbus, ppidev,
303 (flags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT | PPB_INTR);
306 sx_xunlock(&ppi->ppi_lock);
310 ppi->ppi_flags |= HAVE_PPBUS;
312 sx_xunlock(&ppi->ppi_lock);
318 ppiclose(struct cdev *dev, int flags, int fmt, struct thread *td)
320 struct ppi_data *ppi = dev->si_drv1;
321 device_t ppidev = ppi->ppi_device;
322 device_t ppbus = device_get_parent(ppidev);
324 sx_xlock(&ppi->ppi_lock);
327 switch (ppb_1284_get_state(ppbus)) {
328 case PPB_PERIPHERAL_IDLE:
329 ppb_peripheral_terminate(ppbus, 0);
331 case PPB_REVERSE_IDLE:
333 case PPB_ECP_FORWARD_IDLE:
335 ppb_1284_terminate(ppbus);
338 #endif /* PERIPH_1284 */
340 /* unregistration of interrupt forced by release */
341 ppb_release_bus(ppbus, ppidev);
344 ppi->ppi_flags &= ~HAVE_PPBUS;
345 sx_xunlock(&ppi->ppi_lock);
353 * IEEE1284 compliant read.
355 * First, try negotiation to BYTE then NIBBLE mode
356 * If no data is available, wait for it otherwise transfer as much as possible
359 ppiread(struct cdev *dev, struct uio *uio, int ioflag)
362 struct ppi_data *ppi = dev->si_drv1;
363 device_t ppidev = ppi->ppi_device;
364 device_t ppbus = device_get_parent(ppidev);
368 buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
371 switch (ppb_1284_get_state(ppbus)) {
372 case PPB_PERIPHERAL_IDLE:
373 ppb_peripheral_terminate(ppbus, 0);
376 case PPB_FORWARD_IDLE:
377 /* if can't negotiate NIBBLE mode then try BYTE mode,
378 * the peripheral may be a computer
380 if ((ppb_1284_negociate(ppbus,
381 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;
558 case PPIGDATA: /* get data register */
559 *val = ppb_rdtr(ppbus);
561 case PPIGSTATUS: /* get status bits */
562 *val = ppb_rstr(ppbus);
564 case PPIGCTRL: /* get control bits */
565 *val = ppb_rctr(ppbus);
567 case PPIGEPPD: /* get EPP data bits */
568 *val = ppb_repp_D(ppbus);
570 case PPIGECR: /* get ECP bits */
571 *val = ppb_recr(ppbus);
573 case PPIGFIFO: /* read FIFO */
574 *val = ppb_rfifo(ppbus);
576 case PPISDATA: /* set data register */
577 ppb_wdtr(ppbus, *val);
579 case PPISSTATUS: /* set status bits */
580 ppb_wstr(ppbus, *val);
582 case PPISCTRL: /* set control bits */
583 ppb_wctr(ppbus, *val);
585 case PPISEPPD: /* set EPP data bits */
586 ppb_wepp_D(ppbus, *val);
588 case PPISECR: /* set ECP bits */
589 ppb_wecr(ppbus, *val);
591 case PPISFIFO: /* write FIFO */
592 ppb_wfifo(ppbus, *val);
594 case PPIGEPPA: /* get EPP address bits */
595 *val = ppb_repp_A(ppbus);
597 case PPISEPPA: /* set EPP address bits */
598 ppb_wepp_A(ppbus, *val);
609 static device_method_t ppi_methods[] = {
610 /* device interface */
611 DEVMETHOD(device_identify, ppi_identify),
612 DEVMETHOD(device_probe, ppi_probe),
613 DEVMETHOD(device_attach, ppi_attach),
614 DEVMETHOD(device_detach, ppi_detach),
618 static driver_t ppi_driver = {
621 sizeof(struct ppi_data),
623 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);
624 MODULE_DEPEND(ppi, ppbus, 1, 1, 1);