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
26 * $FreeBSD: src/sys/dev/ppbus/ppi.c,v 1.21.2.3 2000/08/07 18:24:43 peter Exp $
29 #include "opt_ppb_1284.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
36 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
40 #include <machine/clock.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
45 #include <dev/ppbus/ppbconf.h>
46 #include <dev/ppbus/ppb_msq.h>
49 #include <dev/ppbus/ppb_1284.h>
52 #include <dev/ppbus/ppi.h>
56 #include <dev/ppbus/ppbio.h>
64 #define HAVE_PPBUS (1<<0)
65 #define HAD_PPBUS (1<<1)
68 int ppi_mode; /* IEEE1284 mode */
69 char ppi_buffer[BUFSIZE];
72 struct resource *intr_resource; /* interrupt resource */
73 void *intr_cookie; /* interrupt registration cookie */
74 #endif /* PERIPH_1284 */
77 #define DEVTOSOFTC(dev) \
78 ((struct ppi_data *)device_get_softc(dev))
79 #define UNITOSOFTC(unit) \
80 ((struct ppi_data *)devclass_get_softc(ppi_devclass, (unit)))
81 #define UNITODEVICE(unit) \
82 (devclass_get_device(ppi_devclass, (unit)))
84 static devclass_t ppi_devclass;
86 static d_open_t ppiopen;
87 static d_close_t ppiclose;
88 static d_ioctl_t ppiioctl;
89 static d_write_t ppiwrite;
90 static d_read_t ppiread;
93 static struct cdevsw ppi_cdevsw = {
101 /* strategy */ nostrategy,
103 /* maj */ CDEV_MAJOR,
113 ppi_enable_intr(device_t ppidev)
116 device_t ppbus = device_get_parent(ppidev);
119 ppb_wctr(ppbus, r | IRQENABLE);
125 ppi_disable_intr(device_t ppidev)
128 device_t ppbus = device_get_parent(ppidev);
131 ppb_wctr(ppbus, r & ~IRQENABLE);
136 #endif /* PERIPH_1284 */
139 ppi_identify(driver_t *driver, device_t parent)
142 BUS_ADD_CHILD(parent, 0, "ppi", 0);
149 ppi_probe(device_t dev)
151 struct ppi_data *ppi;
153 /* probe is always ok */
154 device_set_desc(dev, "Parallel I/O");
156 ppi = DEVTOSOFTC(dev);
157 bzero(ppi, sizeof(struct ppi_data));
166 ppi_attach(device_t dev)
171 struct ppi_data *ppi = DEVTOSOFTC(dev);
173 /* retrive the irq */
174 BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
176 /* declare our interrupt handler */
177 ppi->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
178 &zero, irq, irq, 1, RF_ACTIVE);
179 #endif /* PERIPH_1284 */
181 make_dev(&ppi_cdevsw, device_get_unit(dev), /* XXX cleanup */
183 0600, "ppi%d", device_get_unit(dev));
193 * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
195 * nStrobe <-> nAck 1 <-> 10
196 * nAutofd <-> Busy 11 <-> 14
197 * nSelectin <-> Select 17 <-> 13
198 * nInit <-> nFault 15 <-> 16
204 device_t ppidev = (device_t)arg;
205 device_t ppbus = device_get_parent(ppidev);
206 struct ppi_data *ppi = DEVTOSOFTC(ppidev);
208 ppi_disable_intr(ppidev);
210 switch (ppb_1284_get_state(ppbus)) {
212 /* accept IEEE1284 negociation then wakeup an waiting process to
213 * continue negociation at process level */
214 case PPB_FORWARD_IDLE:
216 if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
218 /* IEEE1284 negociation */
223 /* Event 2 - prepare for reading the ext. value */
224 ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
226 ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
230 printf("0x%x", ppb_rstr(ppbus));
232 ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
236 /* wake up any process waiting for negociation from
237 * remote master host */
239 /* XXX should set a variable to warn the process about
246 printf("?%d", ppb_1284_get_state(ppbus));
248 ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
249 ppb_set_mode(ppbus, PPB_COMPATIBLE);
253 ppi_enable_intr(ppidev);
257 #endif /* PERIPH_1284 */
260 ppiopen(dev_t dev, int flags, int fmt, struct proc *p)
262 u_int unit = minor(dev);
263 struct ppi_data *ppi = UNITOSOFTC(unit);
264 device_t ppidev = UNITODEVICE(unit);
265 device_t ppbus = device_get_parent(ppidev);
271 if (!(ppi->ppi_flags & HAVE_PPBUS)) {
272 if ((res = ppb_request_bus(ppbus, ppidev,
273 (flags & O_NONBLOCK) ? PPB_DONTWAIT :
274 (PPB_WAIT | PPB_INTR))))
277 ppi->ppi_flags |= HAVE_PPBUS;
280 if (ppi->intr_resource) {
281 /* register our interrupt handler */
282 BUS_SETUP_INTR(device_get_parent(ppidev), ppidev, ppi->intr_resource,
283 INTR_TYPE_TTY, ppiintr, dev, &ppi->intr_cookie);
285 #endif /* PERIPH_1284 */
293 ppiclose(dev_t dev, int flags, int fmt, struct proc *p)
295 u_int unit = minor(dev);
296 struct ppi_data *ppi = UNITOSOFTC(unit);
297 device_t ppidev = UNITODEVICE(unit);
298 device_t ppbus = device_get_parent(ppidev);
301 if (!ppi->ppi_count) {
304 switch (ppb_1284_get_state(ppbus)) {
305 case PPB_PERIPHERAL_IDLE:
306 ppb_peripheral_terminate(ppbus, 0);
308 case PPB_REVERSE_IDLE:
310 case PPB_ECP_FORWARD_IDLE:
312 ppb_1284_terminate(ppbus);
315 #endif /* PERIPH_1284 */
317 /* unregistration of interrupt forced by release */
318 ppb_release_bus(ppbus, ppidev);
320 ppi->ppi_flags &= ~HAVE_PPBUS;
329 * IEEE1284 compliant read.
331 * First, try negociation to BYTE then NIBBLE mode
332 * If no data is available, wait for it otherwise transfer as much as possible
335 ppiread(dev_t dev, struct uio *uio, int ioflag)
338 u_int unit = minor(dev);
339 struct ppi_data *ppi = UNITOSOFTC(unit);
340 device_t ppidev = UNITODEVICE(unit);
341 device_t ppbus = device_get_parent(ppidev);
344 switch (ppb_1284_get_state(ppbus)) {
345 case PPB_PERIPHERAL_IDLE:
346 ppb_peripheral_terminate(ppbus, 0);
349 case PPB_FORWARD_IDLE:
350 /* if can't negociate NIBBLE mode then try BYTE mode,
351 * the peripheral may be a computer
353 if ((ppb_1284_negociate(ppbus,
354 ppi->ppi_mode = PPB_NIBBLE, 0))) {
356 /* XXX Wait 2 seconds to let the remote host some
357 * time to terminate its interrupt
359 tsleep(ppi, PPBPRI, "ppiread", 2*hz);
361 if ((error = ppb_1284_negociate(ppbus,
362 ppi->ppi_mode = PPB_BYTE, 0)))
367 case PPB_REVERSE_IDLE:
369 case PPB_ECP_FORWARD_IDLE:
379 while (uio->uio_resid) {
380 if ((error = ppb_1284_read(ppbus, ppi->ppi_mode,
381 ppi->ppi_buffer, min(BUFSIZE, uio->uio_resid),
387 goto error; /* no more data */
392 if ((error = uiomove(ppi->ppi_buffer, len, uio)))
398 #else /* PERIPH_1284 */
408 * IEEE1284 compliant write
410 * Actually, this is the peripheral side of a remote IEEE1284 read
412 * The first part of the negociation (IEEE1284 device detection) is
413 * done at interrupt level, then the remaining is done by the writing
416 * Once negociation done, transfer data
419 ppiwrite(dev_t dev, struct uio *uio, int ioflag)
422 u_int unit = minor(dev);
423 struct ppi_data *ppi = UNITOSOFTC(unit);
424 device_t ppidev = UNITODEVICE(unit);
425 device_t ppbus = device_get_parent(ppidev);
426 int len, error = 0, sent;
431 #define ADDRESS MS_PARAM(0, 0, MS_TYP_PTR)
432 #define LENGTH MS_PARAM(0, 1, MS_TYP_INT)
434 struct ppb_microseq msq[] = {
435 { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
439 /* negociate ECP mode */
440 if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
441 printf("ppiwrite: ECP negociation failed\n");
444 while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
445 uiomove(ppi->ppi_buffer, len, uio);
447 ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
449 error = ppb_MS_microseq(ppbus, msq, &ret);
453 /* we have to be peripheral to be able to send data, so
454 * wait for the appropriate state
456 if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
457 ppb_1284_terminate(ppbus);
459 while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
460 /* XXX should check a variable before sleeping */
465 ppi_enable_intr(ppidev);
467 /* sleep until IEEE1284 negociation starts */
468 error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
472 /* negociate peripheral side with BYTE mode */
473 ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
485 /* negociation done, write bytes to master host */
486 while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
487 uiomove(ppi->ppi_buffer, len, uio);
488 if ((error = byte_peripheral_write(ppbus,
489 ppi->ppi_buffer, len, &sent)))
498 #else /* PERIPH_1284 */
506 ppiioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
508 u_int unit = minor(dev);
509 device_t ppidev = UNITODEVICE(unit);
510 device_t ppbus = device_get_parent(ppidev);
512 u_int8_t *val = (u_int8_t *)data;
516 case PPIGDATA: /* get data register */
517 *val = ppb_rdtr(ppbus);
519 case PPIGSTATUS: /* get status bits */
520 *val = ppb_rstr(ppbus);
522 case PPIGCTRL: /* get control bits */
523 *val = ppb_rctr(ppbus);
525 case PPIGEPPD: /* get EPP data bits */
526 *val = ppb_repp_D(ppbus);
528 case PPIGECR: /* get ECP bits */
529 *val = ppb_recr(ppbus);
531 case PPIGFIFO: /* read FIFO */
532 *val = ppb_rfifo(ppbus);
534 case PPISDATA: /* set data register */
535 ppb_wdtr(ppbus, *val);
537 case PPISSTATUS: /* set status bits */
538 ppb_wstr(ppbus, *val);
540 case PPISCTRL: /* set control bits */
541 ppb_wctr(ppbus, *val);
543 case PPISEPPD: /* set EPP data bits */
544 ppb_wepp_D(ppbus, *val);
546 case PPISECR: /* set ECP bits */
547 ppb_wecr(ppbus, *val);
549 case PPISFIFO: /* write FIFO */
550 ppb_wfifo(ppbus, *val);
552 case PPIGEPPA: /* get EPP address bits */
553 *val = ppb_repp_A(ppbus);
555 case PPISEPPA: /* set EPP address bits */
556 ppb_wepp_A(ppbus, *val);
566 static device_method_t ppi_methods[] = {
567 /* device interface */
568 DEVMETHOD(device_identify, ppi_identify),
569 DEVMETHOD(device_probe, ppi_probe),
570 DEVMETHOD(device_attach, ppi_attach),
575 static driver_t ppi_driver = {
578 sizeof(struct ppi_data),
580 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);