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 $
27 * $DragonFly: src/sys/dev/misc/ppi/ppi.c,v 1.2 2003/06/17 04:28:29 dillon Exp $
30 #include "opt_ppb_1284.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
37 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
41 #include <machine/clock.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
46 #include <dev/ppbus/ppbconf.h>
47 #include <dev/ppbus/ppb_msq.h>
50 #include <dev/ppbus/ppb_1284.h>
53 #include <dev/ppbus/ppi.h>
57 #include <dev/ppbus/ppbio.h>
65 #define HAVE_PPBUS (1<<0)
66 #define HAD_PPBUS (1<<1)
69 int ppi_mode; /* IEEE1284 mode */
70 char ppi_buffer[BUFSIZE];
73 struct resource *intr_resource; /* interrupt resource */
74 void *intr_cookie; /* interrupt registration cookie */
75 #endif /* PERIPH_1284 */
78 #define DEVTOSOFTC(dev) \
79 ((struct ppi_data *)device_get_softc(dev))
80 #define UNITOSOFTC(unit) \
81 ((struct ppi_data *)devclass_get_softc(ppi_devclass, (unit)))
82 #define UNITODEVICE(unit) \
83 (devclass_get_device(ppi_devclass, (unit)))
85 static devclass_t ppi_devclass;
87 static d_open_t ppiopen;
88 static d_close_t ppiclose;
89 static d_ioctl_t ppiioctl;
90 static d_write_t ppiwrite;
91 static d_read_t ppiread;
94 static struct cdevsw ppi_cdevsw = {
102 /* strategy */ nostrategy,
104 /* maj */ CDEV_MAJOR,
114 ppi_enable_intr(device_t ppidev)
117 device_t ppbus = device_get_parent(ppidev);
120 ppb_wctr(ppbus, r | IRQENABLE);
126 ppi_disable_intr(device_t ppidev)
129 device_t ppbus = device_get_parent(ppidev);
132 ppb_wctr(ppbus, r & ~IRQENABLE);
137 #endif /* PERIPH_1284 */
140 ppi_identify(driver_t *driver, device_t parent)
143 BUS_ADD_CHILD(parent, 0, "ppi", 0);
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);
158 bzero(ppi, sizeof(struct ppi_data));
167 ppi_attach(device_t dev)
172 struct ppi_data *ppi = DEVTOSOFTC(dev);
174 /* retrive the irq */
175 BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
177 /* declare our interrupt handler */
178 ppi->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
179 &zero, irq, irq, 1, RF_ACTIVE);
180 #endif /* PERIPH_1284 */
182 make_dev(&ppi_cdevsw, device_get_unit(dev), /* XXX cleanup */
184 0600, "ppi%d", device_get_unit(dev));
194 * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
196 * nStrobe <-> nAck 1 <-> 10
197 * nAutofd <-> Busy 11 <-> 14
198 * nSelectin <-> Select 17 <-> 13
199 * nInit <-> nFault 15 <-> 16
205 device_t ppidev = (device_t)arg;
206 device_t ppbus = device_get_parent(ppidev);
207 struct ppi_data *ppi = DEVTOSOFTC(ppidev);
209 ppi_disable_intr(ppidev);
211 switch (ppb_1284_get_state(ppbus)) {
213 /* accept IEEE1284 negociation then wakeup an waiting process to
214 * continue negociation at process level */
215 case PPB_FORWARD_IDLE:
217 if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
219 /* IEEE1284 negociation */
224 /* Event 2 - prepare for reading the ext. value */
225 ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
227 ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
231 printf("0x%x", ppb_rstr(ppbus));
233 ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
237 /* wake up any process waiting for negociation from
238 * remote master host */
240 /* XXX should set a variable to warn the process about
247 printf("?%d", ppb_1284_get_state(ppbus));
249 ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
250 ppb_set_mode(ppbus, PPB_COMPATIBLE);
254 ppi_enable_intr(ppidev);
258 #endif /* PERIPH_1284 */
261 ppiopen(dev_t dev, int flags, int fmt, struct proc *p)
263 u_int unit = minor(dev);
264 struct ppi_data *ppi = UNITOSOFTC(unit);
265 device_t ppidev = UNITODEVICE(unit);
266 device_t ppbus = device_get_parent(ppidev);
272 if (!(ppi->ppi_flags & HAVE_PPBUS)) {
273 if ((res = ppb_request_bus(ppbus, ppidev,
274 (flags & O_NONBLOCK) ? PPB_DONTWAIT :
275 (PPB_WAIT | PPB_INTR))))
278 ppi->ppi_flags |= HAVE_PPBUS;
281 if (ppi->intr_resource) {
282 /* register our interrupt handler */
283 BUS_SETUP_INTR(device_get_parent(ppidev), ppidev, ppi->intr_resource,
284 INTR_TYPE_TTY, ppiintr, dev, &ppi->intr_cookie);
286 #endif /* PERIPH_1284 */
294 ppiclose(dev_t dev, int flags, int fmt, struct proc *p)
296 u_int unit = minor(dev);
297 struct ppi_data *ppi = UNITOSOFTC(unit);
298 device_t ppidev = UNITODEVICE(unit);
299 device_t ppbus = device_get_parent(ppidev);
302 if (!ppi->ppi_count) {
305 switch (ppb_1284_get_state(ppbus)) {
306 case PPB_PERIPHERAL_IDLE:
307 ppb_peripheral_terminate(ppbus, 0);
309 case PPB_REVERSE_IDLE:
311 case PPB_ECP_FORWARD_IDLE:
313 ppb_1284_terminate(ppbus);
316 #endif /* PERIPH_1284 */
318 /* unregistration of interrupt forced by release */
319 ppb_release_bus(ppbus, ppidev);
321 ppi->ppi_flags &= ~HAVE_PPBUS;
330 * IEEE1284 compliant read.
332 * First, try negociation to BYTE then NIBBLE mode
333 * If no data is available, wait for it otherwise transfer as much as possible
336 ppiread(dev_t dev, struct uio *uio, int ioflag)
339 u_int unit = minor(dev);
340 struct ppi_data *ppi = UNITOSOFTC(unit);
341 device_t ppidev = UNITODEVICE(unit);
342 device_t ppbus = device_get_parent(ppidev);
345 switch (ppb_1284_get_state(ppbus)) {
346 case PPB_PERIPHERAL_IDLE:
347 ppb_peripheral_terminate(ppbus, 0);
350 case PPB_FORWARD_IDLE:
351 /* if can't negociate NIBBLE mode then try BYTE mode,
352 * the peripheral may be a computer
354 if ((ppb_1284_negociate(ppbus,
355 ppi->ppi_mode = PPB_NIBBLE, 0))) {
357 /* XXX Wait 2 seconds to let the remote host some
358 * time to terminate its interrupt
360 tsleep(ppi, PPBPRI, "ppiread", 2*hz);
362 if ((error = ppb_1284_negociate(ppbus,
363 ppi->ppi_mode = PPB_BYTE, 0)))
368 case PPB_REVERSE_IDLE:
370 case PPB_ECP_FORWARD_IDLE:
380 while (uio->uio_resid) {
381 if ((error = ppb_1284_read(ppbus, ppi->ppi_mode,
382 ppi->ppi_buffer, min(BUFSIZE, uio->uio_resid),
388 goto error; /* no more data */
393 if ((error = uiomove(ppi->ppi_buffer, len, uio)))
399 #else /* PERIPH_1284 */
409 * IEEE1284 compliant write
411 * Actually, this is the peripheral side of a remote IEEE1284 read
413 * The first part of the negociation (IEEE1284 device detection) is
414 * done at interrupt level, then the remaining is done by the writing
417 * Once negociation done, transfer data
420 ppiwrite(dev_t dev, struct uio *uio, int ioflag)
423 u_int unit = minor(dev);
424 struct ppi_data *ppi = UNITOSOFTC(unit);
425 device_t ppidev = UNITODEVICE(unit);
426 device_t ppbus = device_get_parent(ppidev);
427 int len, error = 0, sent;
432 #define ADDRESS MS_PARAM(0, 0, MS_TYP_PTR)
433 #define LENGTH MS_PARAM(0, 1, MS_TYP_INT)
435 struct ppb_microseq msq[] = {
436 { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
440 /* negociate ECP mode */
441 if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
442 printf("ppiwrite: ECP negociation failed\n");
445 while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
446 uiomove(ppi->ppi_buffer, len, uio);
448 ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
450 error = ppb_MS_microseq(ppbus, msq, &ret);
454 /* we have to be peripheral to be able to send data, so
455 * wait for the appropriate state
457 if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
458 ppb_1284_terminate(ppbus);
460 while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
461 /* XXX should check a variable before sleeping */
466 ppi_enable_intr(ppidev);
468 /* sleep until IEEE1284 negociation starts */
469 error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
473 /* negociate peripheral side with BYTE mode */
474 ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
486 /* negociation done, write bytes to master host */
487 while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
488 uiomove(ppi->ppi_buffer, len, uio);
489 if ((error = byte_peripheral_write(ppbus,
490 ppi->ppi_buffer, len, &sent)))
499 #else /* PERIPH_1284 */
507 ppiioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
509 u_int unit = minor(dev);
510 device_t ppidev = UNITODEVICE(unit);
511 device_t ppbus = device_get_parent(ppidev);
513 u_int8_t *val = (u_int8_t *)data;
517 case PPIGDATA: /* get data register */
518 *val = ppb_rdtr(ppbus);
520 case PPIGSTATUS: /* get status bits */
521 *val = ppb_rstr(ppbus);
523 case PPIGCTRL: /* get control bits */
524 *val = ppb_rctr(ppbus);
526 case PPIGEPPD: /* get EPP data bits */
527 *val = ppb_repp_D(ppbus);
529 case PPIGECR: /* get ECP bits */
530 *val = ppb_recr(ppbus);
532 case PPIGFIFO: /* read FIFO */
533 *val = ppb_rfifo(ppbus);
535 case PPISDATA: /* set data register */
536 ppb_wdtr(ppbus, *val);
538 case PPISSTATUS: /* set status bits */
539 ppb_wstr(ppbus, *val);
541 case PPISCTRL: /* set control bits */
542 ppb_wctr(ppbus, *val);
544 case PPISEPPD: /* set EPP data bits */
545 ppb_wepp_D(ppbus, *val);
547 case PPISECR: /* set ECP bits */
548 ppb_wecr(ppbus, *val);
550 case PPISFIFO: /* write FIFO */
551 ppb_wfifo(ppbus, *val);
553 case PPIGEPPA: /* get EPP address bits */
554 *val = ppb_repp_A(ppbus);
556 case PPISEPPA: /* set EPP address bits */
557 ppb_wepp_A(ppbus, *val);
567 static device_method_t ppi_methods[] = {
568 /* device interface */
569 DEVMETHOD(device_identify, ppi_identify),
570 DEVMETHOD(device_probe, ppi_probe),
571 DEVMETHOD(device_attach, ppi_attach),
576 static driver_t ppi_driver = {
579 sizeof(struct ppi_data),
581 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);