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/device.h>
37 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
42 #include <machine/clock.h>
44 #include <bus/ppbus/ppbconf.h>
45 #include <bus/ppbus/ppb_msq.h>
48 #include <bus/ppbus/ppb_1284.h>
55 #include <bus/ppbus/ppbio.h>
63 #define HAVE_PPBUS (1<<0)
64 #define HAD_PPBUS (1<<1)
67 int ppi_mode; /* IEEE1284 mode */
68 char ppi_buffer[BUFSIZE];
71 struct resource *intr_resource; /* interrupt resource */
72 void *intr_cookie; /* interrupt registration cookie */
73 #endif /* PERIPH_1284 */
76 #define DEVTOSOFTC(dev) \
77 ((struct ppi_data *)device_get_softc(dev))
78 #define UNITOSOFTC(unit) \
79 ((struct ppi_data *)devclass_get_softc(ppi_devclass, (unit)))
80 #define UNITODEVICE(unit) \
81 (devclass_get_device(ppi_devclass, (unit)))
83 static devclass_t ppi_devclass;
85 static d_open_t ppiopen;
86 static d_close_t ppiclose;
87 static d_ioctl_t ppiioctl;
88 static d_write_t ppiwrite;
89 static d_read_t ppiread;
91 static struct dev_ops ppi_ops = {
103 ppi_enable_intr(device_t ppidev)
106 device_t ppbus = device_get_parent(ppidev);
109 ppb_wctr(ppbus, r | IRQENABLE);
115 ppi_disable_intr(device_t ppidev)
118 device_t ppbus = device_get_parent(ppidev);
121 ppb_wctr(ppbus, r & ~IRQENABLE);
126 #endif /* PERIPH_1284 */
132 ppi_probe(device_t dev)
134 struct ppi_data *ppi;
136 /* probe is always ok */
137 device_set_desc(dev, "Parallel I/O");
139 ppi = DEVTOSOFTC(dev);
140 bzero(ppi, sizeof(struct ppi_data));
149 ppi_attach(device_t dev)
154 struct ppi_data *ppi = DEVTOSOFTC(dev);
156 /* retrive the irq */
157 BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
159 /* declare our interrupt handler */
160 ppi->intr_resource = bus_alloc_legacy_irq_resource(dev, &zero, irq,
162 #endif /* PERIPH_1284 */
164 make_dev(&ppi_ops, device_get_unit(dev), /* XXX cleanup */
166 0600, "ppi%d", device_get_unit(dev));
176 * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
178 * nStrobe <-> nAck 1 <-> 10
179 * nAutofd <-> Busy 11 <-> 14
180 * nSelectin <-> Select 17 <-> 13
181 * nInit <-> nFault 15 <-> 16
187 device_t ppidev = (device_t)arg;
188 device_t ppbus = device_get_parent(ppidev);
189 struct ppi_data *ppi = DEVTOSOFTC(ppidev);
191 ppi_disable_intr(ppidev);
193 switch (ppb_1284_get_state(ppbus)) {
195 /* accept IEEE1284 negociation then wakeup an waiting process to
196 * continue negociation at process level */
197 case PPB_FORWARD_IDLE:
199 if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
201 /* IEEE1284 negociation */
206 /* Event 2 - prepare for reading the ext. value */
207 ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
209 ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
213 kprintf("0x%x", ppb_rstr(ppbus));
215 ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
219 /* wake up any process waiting for negociation from
220 * remote master host */
222 /* XXX should set a variable to warn the process about
229 kprintf("?%d", ppb_1284_get_state(ppbus));
231 ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
232 ppb_set_mode(ppbus, PPB_COMPATIBLE);
236 ppi_enable_intr(ppidev);
240 #endif /* PERIPH_1284 */
243 ppiopen(struct dev_open_args *ap)
245 cdev_t dev = ap->a_head.a_dev;
246 u_int unit = minor(dev);
247 struct ppi_data *ppi = UNITOSOFTC(unit);
248 device_t ppidev = UNITODEVICE(unit);
249 device_t ppbus = device_get_parent(ppidev);
255 if (!(ppi->ppi_flags & HAVE_PPBUS)) {
256 if ((res = ppb_request_bus(ppbus, ppidev,
257 (ap->a_oflags & O_NONBLOCK) ? PPB_DONTWAIT :
258 (PPB_WAIT | PPB_INTR))))
261 ppi->ppi_flags |= HAVE_PPBUS;
264 if (ppi->intr_resource) {
265 /* register our interrupt handler */
266 BUS_SETUP_INTR(device_get_parent(ppidev), ppidev,
267 ppi->intr_resource, 0,
269 &ppi->intr_cookie, NULL, NULL);
271 #endif /* PERIPH_1284 */
279 ppiclose(struct dev_close_args *ap)
281 cdev_t dev = ap->a_head.a_dev;
282 u_int unit = minor(dev);
283 struct ppi_data *ppi = UNITOSOFTC(unit);
284 device_t ppidev = UNITODEVICE(unit);
285 device_t ppbus = device_get_parent(ppidev);
288 if (!ppi->ppi_count) {
291 switch (ppb_1284_get_state(ppbus)) {
292 case PPB_PERIPHERAL_IDLE:
293 ppb_peripheral_terminate(ppbus, 0);
295 case PPB_REVERSE_IDLE:
297 case PPB_ECP_FORWARD_IDLE:
299 ppb_1284_terminate(ppbus);
302 #endif /* PERIPH_1284 */
304 /* unregistration of interrupt forced by release */
305 ppb_release_bus(ppbus, ppidev);
307 ppi->ppi_flags &= ~HAVE_PPBUS;
316 * IEEE1284 compliant read.
318 * First, try negociation to BYTE then NIBBLE mode
319 * If no data is available, wait for it otherwise transfer as much as possible
322 ppiread(struct dev_read_args *ap)
325 cdev_t dev = ap->a_head.a_dev;
326 struct uio *uio = ap->a_uio;
327 u_int unit = minor(dev);
328 struct ppi_data *ppi = UNITOSOFTC(unit);
329 device_t ppidev = UNITODEVICE(unit);
330 device_t ppbus = device_get_parent(ppidev);
333 switch (ppb_1284_get_state(ppbus)) {
334 case PPB_PERIPHERAL_IDLE:
335 ppb_peripheral_terminate(ppbus, 0);
338 case PPB_FORWARD_IDLE:
339 /* if can't negociate NIBBLE mode then try BYTE mode,
340 * the peripheral may be a computer
342 if ((ppb_1284_negociate(ppbus,
343 ppi->ppi_mode = PPB_NIBBLE, 0))) {
345 /* XXX Wait 2 seconds to let the remote host some
346 * time to terminate its interrupt
348 tsleep(ppi, 0, "ppiread", 2*hz);
350 if ((error = ppb_1284_negociate(ppbus,
351 ppi->ppi_mode = PPB_BYTE, 0)))
356 case PPB_REVERSE_IDLE:
358 case PPB_ECP_FORWARD_IDLE:
368 while (uio->uio_resid) {
369 error = ppb_1284_read(ppbus, ppi->ppi_mode, ppi->ppi_buffer,
370 (int)szmin(BUFSIZE, uio->uio_resid),
376 goto error; /* no more data */
381 if ((error = uiomove(ppi->ppi_buffer, (size_t)len, uio)))
387 #else /* PERIPH_1284 */
397 * IEEE1284 compliant write
399 * Actually, this is the peripheral side of a remote IEEE1284 read
401 * The first part of the negociation (IEEE1284 device detection) is
402 * done at interrupt level, then the remaining is done by the writing
405 * Once negociation done, transfer data
408 ppiwrite(struct dev_write_args *ap)
411 cdev_t dev = ap->a_head.a_dev;
412 struct uio *uio = ap->a_uio;
413 u_int unit = minor(dev);
414 struct ppi_data *ppi = UNITOSOFTC(unit);
415 device_t ppidev = UNITODEVICE(unit);
416 device_t ppbus = device_get_parent(ppidev);
417 int len, error = 0, sent;
422 #define ADDRESS MS_PARAM(0, 0, MS_TYP_PTR)
423 #define LENGTH MS_PARAM(0, 1, MS_TYP_INT)
425 struct ppb_microseq msq[] = {
426 { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
430 /* negociate ECP mode */
431 if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
432 kprintf("ppiwrite: ECP negotiation failed\n");
435 while (!error && (len = (int)szmin(uio->uio_resid, BUFSIZE))) {
436 uiomove(ppi->ppi_buffer, (size_t)len, uio);
438 ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
440 error = ppb_MS_microseq(ppbus, msq, &ret);
444 /* we have to be peripheral to be able to send data, so
445 * wait for the appropriate state
447 if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
448 ppb_1284_terminate(ppbus);
450 while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
451 /* XXX should check a variable before sleeping */
456 ppi_enable_intr(ppidev);
458 /* sleep until IEEE1284 negociation starts */
459 error = tsleep(ppi, PCATCH, "ppiwrite", 0);
463 /* negociate peripheral side with BYTE mode */
464 ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
476 /* negociation done, write bytes to master host */
477 while ((len = (int)szmin(uio->uio_resid, BUFSIZE)) != 0) {
478 uiomove(ppi->ppi_buffer, (size_t)len, uio);
479 if ((error = byte_peripheral_write(ppbus,
480 ppi->ppi_buffer, len, &sent)))
489 #else /* PERIPH_1284 */
497 ppiioctl(struct dev_ioctl_args *ap)
499 cdev_t dev = ap->a_head.a_dev;
500 u_int unit = minor(dev);
501 device_t ppidev = UNITODEVICE(unit);
502 device_t ppbus = device_get_parent(ppidev);
504 u_int8_t *val = (u_int8_t *)ap->a_data;
507 case PPIGDATA: /* get data register */
508 *val = ppb_rdtr(ppbus);
510 case PPIGSTATUS: /* get status bits */
511 *val = ppb_rstr(ppbus);
513 case PPIGCTRL: /* get control bits */
514 *val = ppb_rctr(ppbus);
516 case PPIGEPPD: /* get EPP data bits */
517 *val = ppb_repp_D(ppbus);
519 case PPIGECR: /* get ECP bits */
520 *val = ppb_recr(ppbus);
522 case PPIGFIFO: /* read FIFO */
523 *val = ppb_rfifo(ppbus);
525 case PPISDATA: /* set data register */
526 ppb_wdtr(ppbus, *val);
528 case PPISSTATUS: /* set status bits */
529 ppb_wstr(ppbus, *val);
531 case PPISCTRL: /* set control bits */
532 ppb_wctr(ppbus, *val);
534 case PPISEPPD: /* set EPP data bits */
535 ppb_wepp_D(ppbus, *val);
537 case PPISECR: /* set ECP bits */
538 ppb_wecr(ppbus, *val);
540 case PPISFIFO: /* write FIFO */
541 ppb_wfifo(ppbus, *val);
543 case PPIGEPPA: /* get EPP address bits */
544 *val = ppb_repp_A(ppbus);
546 case PPISEPPA: /* set EPP address bits */
547 ppb_wepp_A(ppbus, *val);
558 * Because ppi is a static device under any attached ppbuf, and not
559 * scanned by the ppbuf, we need an identify function to create the
562 static device_method_t ppi_methods[] = {
563 /* device interface */
564 DEVMETHOD(device_identify, bus_generic_identify),
565 DEVMETHOD(device_probe, ppi_probe),
566 DEVMETHOD(device_attach, ppi_attach),
571 static driver_t ppi_driver = {
574 sizeof(struct ppi_data),
576 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, NULL, NULL);