2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
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/kernel.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
42 #include <sys/malloc.h>
45 #include <machine/resource.h>
47 #include <dev/ppbus/ppbconf.h>
48 #include <dev/ppbus/ppb_1284.h>
52 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
54 static MALLOC_DEFINE(M_PPBUSDEV, "ppbusdev", "Parallel Port bus device");
57 static int ppbus_intr(void *arg);
64 ppbus_print_child(device_t bus, device_t dev)
66 struct ppb_device *ppbdev;
69 retval = bus_print_child_header(bus, dev);
71 ppbdev = (struct ppb_device *)device_get_ivars(dev);
73 if (ppbdev->flags != 0)
74 retval += printf(" flags 0x%x", ppbdev->flags);
76 retval += bus_print_child_footer(bus, dev);
82 ppbus_probe(device_t dev)
84 device_set_desc(dev, "Parallel port bus");
92 * Add a ppbus device, allocate/initialize the ivars
95 ppbus_add_child(device_t dev, u_int order, const char *name, int unit)
97 struct ppb_device *ppbdev;
100 /* allocate ivars for the new ppbus child */
101 ppbdev = malloc(sizeof(struct ppb_device), M_PPBUSDEV,
106 /* initialize the ivars */
109 /* add the device as a child to the ppbus bus with the allocated
111 child = device_add_child_ordered(dev, order, name, unit);
112 device_set_ivars(child, ppbdev);
118 ppbus_read_ivar(device_t bus, device_t dev, int index, uintptr_t* val)
122 case PPBUS_IVAR_MODE:
123 /* XXX yet device mode = ppbus mode = chipset mode */
124 *val = (u_long)ppb_get_mode(bus);
134 ppbus_write_ivar(device_t bus, device_t dev, int index, uintptr_t val)
138 case PPBUS_IVAR_MODE:
139 /* XXX yet device mode = ppbus mode = chipset mode */
140 ppb_set_mode(bus, val);
149 #define PPB_PNP_PRINTER 0
150 #define PPB_PNP_MODEM 1
151 #define PPB_PNP_NET 2
152 #define PPB_PNP_HDC 3
153 #define PPB_PNP_PCMCIA 4
154 #define PPB_PNP_MEDIA 5
155 #define PPB_PNP_FDC 6
156 #define PPB_PNP_PORTS 7
157 #define PPB_PNP_SCANNER 8
158 #define PPB_PNP_DIGICAM 9
160 #ifndef DONTPROBE_1284
162 static char *pnp_tokens[] = {
163 "PRINTER", "MODEM", "NET", "HDC", "PCMCIA", "MEDIA",
164 "FDC", "PORTS", "SCANNER", "DIGICAM", "", NULL };
167 static char *pnp_classes[] = {
168 "printer", "modem", "network device",
169 "hard disk", "PCMCIA", "multimedia device",
170 "floppy disk", "ports", "scanner",
171 "digital camera", "unknown device", NULL };
177 * Search the first occurrence of a token within a string
180 search_token(char *str, int slen, char *token)
184 #define UNKNOWN_LENGTH -1
186 if (slen == UNKNOWN_LENGTH)
187 /* get string's length */
190 /* get token's length */
191 tlen = strlen(token);
195 for (i = 0; i <= slen-tlen; i++) {
196 if (strncmp(str + i, token, tlen) == 0)
206 * Returns the class id. of the peripherial, -1 otherwise
209 ppb_pnp_detect(device_t bus)
211 char *token, *class = NULL;
214 char str[PPB_PnP_STRING_SIZE+1];
216 device_printf(bus, "Probing for PnP devices:\n");
218 if ((error = ppb_1284_read_id(bus, PPB_NIBBLE, str,
219 PPB_PnP_STRING_SIZE, &len)))
223 device_printf(bus, "<PnP> %d characters: ", len);
224 for (i = 0; i < len; i++)
225 printf("%c(0x%x) ", str[i], str[i]);
229 /* replace ';' characters by '\0' */
230 for (i = 0; i < len; i++)
231 str[i] = (str[i] == ';') ? '\0' : str[i];
233 if ((token = search_token(str, len, "MFG")) != NULL ||
234 (token = search_token(str, len, "MANUFACTURER")) != NULL)
235 device_printf(bus, "<%s",
236 search_token(token, UNKNOWN_LENGTH, ":") + 1);
238 device_printf(bus, "<unknown");
240 if ((token = search_token(str, len, "MDL")) != NULL ||
241 (token = search_token(str, len, "MODEL")) != NULL)
243 search_token(token, UNKNOWN_LENGTH, ":") + 1);
247 if ((token = search_token(str, len, "VER")) != NULL)
249 search_token(token, UNKNOWN_LENGTH, ":") + 1);
251 if ((token = search_token(str, len, "REV")) != NULL)
253 search_token(token, UNKNOWN_LENGTH, ":") + 1);
257 if ((token = search_token(str, len, "CLS")) != NULL) {
258 class = search_token(token, UNKNOWN_LENGTH, ":") + 1;
259 printf(" %s", class);
262 if ((token = search_token(str, len, "CMD")) != NULL ||
263 (token = search_token(str, len, "COMMAND")) != NULL)
265 search_token(token, UNKNOWN_LENGTH, ":") + 1);
270 /* identify class ident */
271 for (i = 0; pnp_tokens[i] != NULL; i++) {
272 if (search_token(class, len, pnp_tokens[i]) != NULL) {
278 class_id = PPB_PnP_UNKNOWN;
287 * Scan the ppbus for IEEE1284 compliant devices
290 ppb_scan_bus(device_t bus)
292 struct ppb_data * ppb = (struct ppb_data *)device_get_softc(bus);
295 /* try all IEEE1284 modes, for one device only
297 * XXX We should implement the IEEE1284.3 standard to detect
298 * daisy chained devices
301 error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID);
303 if ((ppb->state == PPB_ERROR) && (ppb->error == PPB_NOT_IEEE1284))
306 ppb_1284_terminate(bus);
308 device_printf(bus, "IEEE1284 device found ");
310 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE, 0))) {
312 ppb_1284_terminate(bus);
315 if (!(error = ppb_1284_negociate(bus, PPB_PS2, 0))) {
317 ppb_1284_terminate(bus);
320 if (!(error = ppb_1284_negociate(bus, PPB_ECP, 0))) {
322 ppb_1284_terminate(bus);
325 if (!(error = ppb_1284_negociate(bus, PPB_ECP, PPB_USE_RLE))) {
327 ppb_1284_terminate(bus);
330 if (!(error = ppb_1284_negociate(bus, PPB_EPP, 0))) {
332 ppb_1284_terminate(bus);
335 /* try more IEEE1284 modes */
337 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE,
339 printf("/NIBBLE_ID");
340 ppb_1284_terminate(bus);
343 if (!(error = ppb_1284_negociate(bus, PPB_PS2,
346 ppb_1284_terminate(bus);
349 if (!(error = ppb_1284_negociate(bus, PPB_ECP,
352 ppb_1284_terminate(bus);
355 if (!(error = ppb_1284_negociate(bus, PPB_ECP,
356 PPB_REQUEST_ID | PPB_USE_RLE))) {
357 printf("/ECP_RLE_ID");
358 ppb_1284_terminate(bus);
361 if (!(error = ppb_1284_negociate(bus, PPB_COMPATIBLE,
362 PPB_EXTENSIBILITY_LINK))) {
363 printf("/Extensibility Link");
364 ppb_1284_terminate(bus);
370 /* detect PnP devices */
371 ppb->class_id = ppb_pnp_detect(bus);
379 #endif /* !DONTPROBE_1284 */
382 ppbus_attach(device_t dev)
384 struct ppb_data *ppb = device_get_softc(dev);
387 error = BUS_READ_IVAR(device_get_parent(dev), dev, PPC_IVAR_LOCK,
388 (uintptr_t *)&ppb->ppc_lock);
390 device_printf(dev, "Unable to fetch parent's lock\n");
395 ppb->ppc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
397 if (ppb->ppc_irq_res != NULL) {
398 mtx_lock(ppb->ppc_lock);
399 error = BUS_WRITE_IVAR(device_get_parent(dev), dev,
400 PPC_IVAR_INTR_HANDLER, (uintptr_t)&ppbus_intr);
401 mtx_unlock(ppb->ppc_lock);
403 device_printf(dev, "Unable to set interrupt handler\n");
408 /* Locate our children */
409 bus_generic_probe(dev);
411 #ifndef DONTPROBE_1284
412 /* detect IEEE1284 compliant devices */
413 mtx_lock(ppb->ppc_lock);
415 mtx_unlock(ppb->ppc_lock);
416 #endif /* !DONTPROBE_1284 */
418 /* launch attachment of the added children */
419 bus_generic_attach(dev);
425 ppbus_detach(device_t dev)
429 error = bus_generic_detach(dev);
433 /* detach & delete all children */
434 device_delete_children(dev);
440 ppbus_intr(void *arg)
442 struct ppb_device *ppbdev;
443 struct ppb_data *ppb = arg;
445 mtx_assert(ppb->ppc_lock, MA_OWNED);
446 if (ppb->ppb_owner == NULL)
449 ppbdev = device_get_ivars(ppb->ppb_owner);
450 if (ppbdev->intr_hook == NULL)
453 ppbdev->intr_hook(ppbdev->intr_arg);
458 ppbus_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
459 driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
461 struct ppb_device *ppbdev = device_get_ivars(child);
462 struct ppb_data *ppb = DEVTOSOFTC(bus);
464 /* We do not support filters. */
465 if (filt != NULL || ihand == NULL)
468 /* Can only attach handlers to the parent device's resource. */
469 if (ppb->ppc_irq_res != r)
472 mtx_lock(ppb->ppc_lock);
473 ppbdev->intr_hook = ihand;
474 ppbdev->intr_arg = arg;
476 mtx_unlock(ppb->ppc_lock);
482 ppbus_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih)
484 struct ppb_device *ppbdev = device_get_ivars(child);
485 struct ppb_data *ppb = DEVTOSOFTC(bus);
487 mtx_lock(ppb->ppc_lock);
488 if (ppbdev != ih || ppb->ppc_irq_res != r) {
489 mtx_unlock(ppb->ppc_lock);
493 ppbdev->intr_hook = NULL;
494 mtx_unlock(ppb->ppc_lock);
502 * Allocate the device to perform transfers.
504 * how : PPB_WAIT or PPB_DONTWAIT
507 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);
513 mtx_assert(ppb->ppc_lock, MA_OWNED);
515 if (ppb->ppb_owner) {
517 case PPB_WAIT | PPB_INTR:
518 error = mtx_sleep(ppb, ppb->ppc_lock,
519 PPBPRI | PCATCH, "ppbreq", 0);
522 case PPB_WAIT | PPB_NOINTR:
523 error = mtx_sleep(ppb, ppb->ppc_lock, PPBPRI,
528 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 useful for
537 * drivers that do not set there operating mode
540 if (ppbdev->ctx.valid)
541 ppb_set_mode(bus, ppbdev->ctx.mode);
553 * Release the device allocated with ppb_request_bus()
556 ppb_release_bus(device_t bus, device_t dev)
558 struct ppb_data *ppb = DEVTOSOFTC(bus);
559 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
561 mtx_assert(ppb->ppc_lock, MA_OWNED);
562 if (ppb->ppb_owner != dev)
565 /* save the context of the device */
566 ppbdev->ctx.mode = ppb_get_mode(bus);
568 /* ok, now the context of the device is valid */
569 ppbdev->ctx.valid = 1;
573 /* wakeup waiting processes */
579 static devclass_t ppbus_devclass;
581 static device_method_t ppbus_methods[] = {
582 /* device interface */
583 DEVMETHOD(device_probe, ppbus_probe),
584 DEVMETHOD(device_attach, ppbus_attach),
585 DEVMETHOD(device_detach, ppbus_detach),
588 DEVMETHOD(bus_add_child, ppbus_add_child),
589 DEVMETHOD(bus_print_child, ppbus_print_child),
590 DEVMETHOD(bus_read_ivar, ppbus_read_ivar),
591 DEVMETHOD(bus_write_ivar, ppbus_write_ivar),
592 DEVMETHOD(bus_setup_intr, ppbus_setup_intr),
593 DEVMETHOD(bus_teardown_intr, ppbus_teardown_intr),
594 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
595 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
600 static driver_t ppbus_driver = {
603 sizeof(struct ppb_data),
605 DRIVER_MODULE(ppbus, ppc, ppbus_driver, ppbus_devclass, 0, 0);