2 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include "opt_ppb_1284.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/malloc.h>
41 #include <machine/resource.h>
43 #include <dev/ppbus/ppbconf.h>
44 #include <dev/ppbus/ppb_1284.h>
48 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
50 static MALLOC_DEFINE(M_PPBUSDEV, "ppbusdev", "Parallel Port bus device");
58 ppbus_print_child(device_t bus, device_t dev)
60 struct ppb_device *ppbdev;
62 bus_print_child_header(bus, dev);
64 ppbdev = (struct ppb_device *)device_get_ivars(dev);
66 if (ppbdev->flags != 0)
67 printf(" flags 0x%x", ppbdev->flags);
69 printf(" on %s%d\n", device_get_name(bus), device_get_unit(bus));
75 ppbus_probe(device_t dev)
77 device_set_desc(dev, "Parallel port bus");
85 * Add a ppbus device, allocate/initialize the ivars
88 ppbus_add_child(device_t dev, int order, const char *name, int unit)
90 struct ppb_device *ppbdev;
93 /* allocate ivars for the new ppbus child */
94 ppbdev = malloc(sizeof(struct ppb_device), M_PPBUSDEV,
99 /* initialize the ivars */
102 /* add the device as a child to the ppbus bus with the allocated
104 child = device_add_child_ordered(dev, order, name, unit);
105 device_set_ivars(child, ppbdev);
111 ppbus_read_ivar(device_t bus, device_t dev, int index, uintptr_t* val)
113 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
116 case PPBUS_IVAR_MODE:
117 /* XXX yet device mode = ppbus mode = chipset mode */
118 *val = (u_long)ppb_get_mode(bus);
119 ppbdev->mode = (u_short)*val;
122 *val = (u_long)ppbdev->avm;
125 BUS_READ_IVAR(device_get_parent(bus), bus, PPC_IVAR_IRQ, val);
135 ppbus_write_ivar(device_t bus, device_t dev, int index, u_long val)
137 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
140 case PPBUS_IVAR_MODE:
141 /* XXX yet device mode = ppbus mode = chipset mode */
142 ppb_set_mode(bus,val);
143 ppbdev->mode = ppb_get_mode(bus);
152 #define PPB_PNP_PRINTER 0
153 #define PPB_PNP_MODEM 1
154 #define PPB_PNP_NET 2
155 #define PPB_PNP_HDC 3
156 #define PPB_PNP_PCMCIA 4
157 #define PPB_PNP_MEDIA 5
158 #define PPB_PNP_FDC 6
159 #define PPB_PNP_PORTS 7
160 #define PPB_PNP_SCANNER 8
161 #define PPB_PNP_DIGICAM 9
163 #ifndef DONTPROBE_1284
165 static char *pnp_tokens[] = {
166 "PRINTER", "MODEM", "NET", "HDC", "PCMCIA", "MEDIA",
167 "FDC", "PORTS", "SCANNER", "DIGICAM", "", NULL };
170 static char *pnp_classes[] = {
171 "printer", "modem", "network device",
172 "hard disk", "PCMCIA", "multimedia device",
173 "floppy disk", "ports", "scanner",
174 "digital camera", "unknown device", NULL };
180 * Search the first occurence of a token within a string
183 search_token(char *str, int slen, char *token)
187 #define UNKNOWN_LENGTH -1
189 if (slen == UNKNOWN_LENGTH)
190 /* get string's length */
193 /* get token's length */
194 tlen = strlen(token);
198 for (i = 0; i <= slen-tlen; i++) {
199 if (strncmp(str + i, token, tlen) == 0)
209 * Returns the class id. of the peripherial, -1 otherwise
212 ppb_pnp_detect(device_t bus)
214 char *token, *class = 0;
217 char str[PPB_PnP_STRING_SIZE+1];
218 int unit = device_get_unit(bus);
220 printf("Probing for PnP devices on ppbus%d:\n", unit);
222 if ((error = ppb_1284_read_id(bus, PPB_NIBBLE, str,
223 PPB_PnP_STRING_SIZE, &len)))
227 printf("ppb: <PnP> %d characters: ", len);
228 for (i = 0; i < len; i++)
229 printf("%c(0x%x) ", str[i], str[i]);
233 /* replace ';' characters by '\0' */
234 for (i = 0; i < len; i++)
235 str[i] = (str[i] == ';') ? '\0' : str[i];
237 if ((token = search_token(str, len, "MFG")) != NULL ||
238 (token = search_token(str, len, "MANUFACTURER")) != NULL)
239 printf("ppbus%d: <%s", unit,
240 search_token(token, UNKNOWN_LENGTH, ":") + 1);
242 printf("ppbus%d: <unknown", unit);
244 if ((token = search_token(str, len, "MDL")) != NULL ||
245 (token = search_token(str, len, "MODEL")) != NULL)
247 search_token(token, UNKNOWN_LENGTH, ":") + 1);
251 if ((token = search_token(str, len, "VER")) != NULL)
253 search_token(token, UNKNOWN_LENGTH, ":") + 1);
255 if ((token = search_token(str, len, "REV")) != NULL)
257 search_token(token, UNKNOWN_LENGTH, ":") + 1);
261 if ((token = search_token(str, len, "CLS")) != NULL) {
262 class = search_token(token, UNKNOWN_LENGTH, ":") + 1;
263 printf(" %s", class);
266 if ((token = search_token(str, len, "CMD")) != NULL ||
267 (token = search_token(str, len, "COMMAND")) != NULL)
269 search_token(token, UNKNOWN_LENGTH, ":") + 1);
274 /* identify class ident */
275 for (i = 0; pnp_tokens[i] != NULL; i++) {
276 if (search_token(class, len, pnp_tokens[i]) != NULL) {
282 class_id = PPB_PnP_UNKNOWN;
291 * Scan the ppbus for IEEE1284 compliant devices
294 ppb_scan_bus(device_t bus)
296 struct ppb_data * ppb = (struct ppb_data *)device_get_softc(bus);
298 int unit = device_get_unit(bus);
300 /* try all IEEE1284 modes, for one device only
302 * XXX We should implement the IEEE1284.3 standard to detect
303 * daisy chained devices
306 error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID);
308 if ((ppb->state == PPB_ERROR) && (ppb->error == PPB_NOT_IEEE1284))
311 ppb_1284_terminate(bus);
313 printf("ppbus%d: IEEE1284 device found ", unit);
315 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE, 0))) {
317 ppb_1284_terminate(bus);
320 if (!(error = ppb_1284_negociate(bus, PPB_PS2, 0))) {
322 ppb_1284_terminate(bus);
325 if (!(error = ppb_1284_negociate(bus, PPB_ECP, 0))) {
327 ppb_1284_terminate(bus);
330 if (!(error = ppb_1284_negociate(bus, PPB_ECP, PPB_USE_RLE))) {
332 ppb_1284_terminate(bus);
335 if (!(error = ppb_1284_negociate(bus, PPB_EPP, 0))) {
337 ppb_1284_terminate(bus);
340 /* try more IEEE1284 modes */
342 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE,
344 printf("/NIBBLE_ID");
345 ppb_1284_terminate(bus);
348 if (!(error = ppb_1284_negociate(bus, PPB_PS2,
351 ppb_1284_terminate(bus);
354 if (!(error = ppb_1284_negociate(bus, PPB_ECP,
357 ppb_1284_terminate(bus);
360 if (!(error = ppb_1284_negociate(bus, PPB_ECP,
361 PPB_REQUEST_ID | PPB_USE_RLE))) {
362 printf("/ECP_RLE_ID");
363 ppb_1284_terminate(bus);
366 if (!(error = ppb_1284_negociate(bus, PPB_COMPATIBLE,
367 PPB_EXTENSIBILITY_LINK))) {
368 printf("/Extensibility Link");
369 ppb_1284_terminate(bus);
375 /* detect PnP devices */
376 ppb->class_id = ppb_pnp_detect(bus);
384 #endif /* !DONTPROBE_1284 */
387 ppbus_dummy_intr(void *arg)
392 ppbus_attach(device_t dev)
394 struct ppb_data *ppb = (struct ppb_data *)device_get_softc(dev);
398 /* Attach a dummy interrupt handler to suck up any stray interrupts. */
399 BUS_READ_IVAR(device_get_parent(dev), dev, PPC_IVAR_IRQ, &irq);
403 ppb->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq,
404 irq, 1, RF_SHAREABLE);
405 if (ppb->irq_res != NULL) {
406 error = bus_setup_intr(dev, ppb->irq_res,
407 INTR_TYPE_TTY | INTR_MPSAFE, NULL, ppbus_dummy_intr,
408 ppb, &ppb->intr_cookie);
411 "failed to setup interrupt handler\n");
412 bus_release_resource(dev, SYS_RES_IRQ, 0,
419 /* Locate our children */
420 bus_generic_probe(dev);
422 #ifndef DONTPROBE_1284
423 /* detect IEEE1284 compliant devices */
425 #endif /* !DONTPROBE_1284 */
427 /* launch attachement of the added children */
428 bus_generic_attach(dev);
434 ppbus_detach(device_t dev)
436 struct ppb_data *ppb = (struct ppb_data *)device_get_softc(dev);
440 /* detach & delete all children */
441 if (!device_get_children(dev, &children, &nchildren)) {
442 for (i = 0; i < nchildren; i++)
444 device_delete_child(dev, children[i]);
445 free(children, M_TEMP);
448 if (ppb->irq_res != NULL) {
449 bus_teardown_intr(dev, ppb->irq_res, ppb->intr_cookie);
450 bus_release_resource(dev, SYS_RES_IRQ, 0, ppb->irq_res);
456 ppbus_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
457 driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
460 struct ppb_data *ppb = DEVTOSOFTC(bus);
461 struct ppb_device *ppbdev = device_get_ivars(child);
463 /* a device driver must own the bus to register an interrupt */
464 if (ppb->ppb_owner != child)
467 if ((error = BUS_SETUP_INTR(device_get_parent(bus), child, r, flags,
468 filt, ihand, arg, cookiep)))
471 /* store the resource and the cookie for eventually forcing
472 * handler unregistration
474 ppbdev->intr_cookie = *cookiep;
475 ppbdev->intr_resource = r;
481 ppbus_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih)
483 struct ppb_data *ppb = DEVTOSOFTC(bus);
484 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(child);
486 /* a device driver must own the bus to unregister an interrupt */
487 if ((ppb->ppb_owner != child) || (ppbdev->intr_cookie != ih) ||
488 (ppbdev->intr_resource != r))
491 ppbdev->intr_cookie = 0;
492 ppbdev->intr_resource = 0;
494 /* pass unregistration to the upper layer */
495 return (BUS_TEARDOWN_INTR(device_get_parent(bus), child, r, ih));
501 * Allocate the device to perform transfers.
503 * how : PPB_WAIT or PPB_DONTWAIT
506 ppb_request_bus(device_t bus, device_t dev, int how)
509 struct ppb_data *ppb = DEVTOSOFTC(bus);
510 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
514 if (ppb->ppb_owner) {
518 case (PPB_WAIT | PPB_INTR):
519 error = tsleep(ppb, PPBPRI|PCATCH, "ppbreq", 0);
522 case (PPB_WAIT | PPB_NOINTR):
523 error = tsleep(ppb, PPBPRI, "ppbreq", 0);
527 return (EWOULDBLOCK);
532 ppb->ppb_owner = dev;
534 /* restore the context of the device
535 * The first time, ctx.valid is certainly false
536 * then do not change anything. This is usefull for
537 * drivers that do not set there operating mode
540 if (ppbdev->ctx.valid)
541 ppb_set_mode(bus, ppbdev->ctx.mode);
554 * Release the device allocated with ppb_request_bus()
557 ppb_release_bus(device_t bus, device_t dev)
560 struct ppb_data *ppb = DEVTOSOFTC(bus);
561 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
563 if (ppbdev->intr_resource != 0)
564 /* force interrupt handler unregistration when the ppbus is released */
565 if ((error = BUS_TEARDOWN_INTR(bus, dev, ppbdev->intr_resource,
566 ppbdev->intr_cookie)))
570 if (ppb->ppb_owner != dev) {
578 /* save the context of the device */
579 ppbdev->ctx.mode = ppb_get_mode(bus);
581 /* ok, now the context of the device is valid */
582 ppbdev->ctx.valid = 1;
584 /* wakeup waiting processes */
590 static devclass_t ppbus_devclass;
592 static device_method_t ppbus_methods[] = {
593 /* device interface */
594 DEVMETHOD(device_probe, ppbus_probe),
595 DEVMETHOD(device_attach, ppbus_attach),
596 DEVMETHOD(device_detach, ppbus_detach),
599 DEVMETHOD(bus_add_child, ppbus_add_child),
600 DEVMETHOD(bus_print_child, ppbus_print_child),
601 DEVMETHOD(bus_read_ivar, ppbus_read_ivar),
602 DEVMETHOD(bus_write_ivar, ppbus_write_ivar),
603 DEVMETHOD(bus_setup_intr, ppbus_setup_intr),
604 DEVMETHOD(bus_teardown_intr, ppbus_teardown_intr),
605 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
610 static driver_t ppbus_driver = {
613 sizeof(struct ppb_data),
615 DRIVER_MODULE(ppbus, ppc, ppbus_driver, ppbus_devclass, 0, 0);