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");
56 static int ppbus_intr(void *arg);
63 ppbus_print_child(device_t bus, device_t dev)
65 struct ppb_device *ppbdev;
68 retval = bus_print_child_header(bus, dev);
70 ppbdev = (struct ppb_device *)device_get_ivars(dev);
72 if (ppbdev->flags != 0)
73 retval += printf(" flags 0x%x", ppbdev->flags);
75 retval += bus_print_child_footer(bus, dev);
81 ppbus_probe(device_t dev)
83 device_set_desc(dev, "Parallel port bus");
91 * Add a ppbus device, allocate/initialize the ivars
94 ppbus_add_child(device_t dev, u_int order, const char *name, int unit)
96 struct ppb_device *ppbdev;
99 /* allocate ivars for the new ppbus child */
100 ppbdev = malloc(sizeof(struct ppb_device), M_PPBUSDEV,
105 /* initialize the ivars */
108 /* add the device as a child to the ppbus bus with the allocated
110 child = device_add_child_ordered(dev, order, name, unit);
111 device_set_ivars(child, ppbdev);
117 ppbus_read_ivar(device_t bus, device_t dev, int index, uintptr_t* val)
121 case PPBUS_IVAR_MODE:
122 /* XXX yet device mode = ppbus mode = chipset mode */
123 *val = (u_long)ppb_get_mode(bus);
133 ppbus_write_ivar(device_t bus, device_t dev, int index, uintptr_t val)
137 case PPBUS_IVAR_MODE:
138 /* XXX yet device mode = ppbus mode = chipset mode */
139 ppb_set_mode(bus, val);
148 #define PPB_PNP_PRINTER 0
149 #define PPB_PNP_MODEM 1
150 #define PPB_PNP_NET 2
151 #define PPB_PNP_HDC 3
152 #define PPB_PNP_PCMCIA 4
153 #define PPB_PNP_MEDIA 5
154 #define PPB_PNP_FDC 6
155 #define PPB_PNP_PORTS 7
156 #define PPB_PNP_SCANNER 8
157 #define PPB_PNP_DIGICAM 9
159 #ifndef DONTPROBE_1284
161 static char *pnp_tokens[] = {
162 "PRINTER", "MODEM", "NET", "HDC", "PCMCIA", "MEDIA",
163 "FDC", "PORTS", "SCANNER", "DIGICAM", "", NULL };
166 static char *pnp_classes[] = {
167 "printer", "modem", "network device",
168 "hard disk", "PCMCIA", "multimedia device",
169 "floppy disk", "ports", "scanner",
170 "digital camera", "unknown device", NULL };
176 * Search the first occurrence of a token within a string
179 search_token(char *str, int slen, char *token)
183 #define UNKNOWN_LENGTH -1
185 if (slen == UNKNOWN_LENGTH)
186 /* get string's length */
189 /* get token's length */
190 tlen = strlen(token);
194 for (i = 0; i <= slen-tlen; i++) {
195 if (strncmp(str + i, token, tlen) == 0)
205 * Returns the class id. of the peripherial, -1 otherwise
208 ppb_pnp_detect(device_t bus)
210 char *token, *class = NULL;
213 char str[PPB_PnP_STRING_SIZE+1];
215 device_printf(bus, "Probing for PnP devices:\n");
217 if ((error = ppb_1284_read_id(bus, PPB_NIBBLE, str,
218 PPB_PnP_STRING_SIZE, &len)))
222 device_printf(bus, "<PnP> %d characters: ", len);
223 for (i = 0; i < len; i++)
224 printf("%c(0x%x) ", str[i], str[i]);
228 /* replace ';' characters by '\0' */
229 for (i = 0; i < len; i++)
230 str[i] = (str[i] == ';') ? '\0' : str[i];
232 if ((token = search_token(str, len, "MFG")) != NULL ||
233 (token = search_token(str, len, "MANUFACTURER")) != NULL)
234 device_printf(bus, "<%s",
235 search_token(token, UNKNOWN_LENGTH, ":") + 1);
237 device_printf(bus, "<unknown");
239 if ((token = search_token(str, len, "MDL")) != NULL ||
240 (token = search_token(str, len, "MODEL")) != NULL)
242 search_token(token, UNKNOWN_LENGTH, ":") + 1);
246 if ((token = search_token(str, len, "VER")) != NULL)
248 search_token(token, UNKNOWN_LENGTH, ":") + 1);
250 if ((token = search_token(str, len, "REV")) != NULL)
252 search_token(token, UNKNOWN_LENGTH, ":") + 1);
256 if ((token = search_token(str, len, "CLS")) != NULL) {
257 class = search_token(token, UNKNOWN_LENGTH, ":") + 1;
258 printf(" %s", class);
261 if ((token = search_token(str, len, "CMD")) != NULL ||
262 (token = search_token(str, len, "COMMAND")) != NULL)
264 search_token(token, UNKNOWN_LENGTH, ":") + 1);
269 /* identify class ident */
270 for (i = 0; pnp_tokens[i] != NULL; i++) {
271 if (search_token(class, len, pnp_tokens[i]) != NULL) {
277 class_id = PPB_PnP_UNKNOWN;
286 * Scan the ppbus for IEEE1284 compliant devices
289 ppb_scan_bus(device_t bus)
291 struct ppb_data * ppb = (struct ppb_data *)device_get_softc(bus);
294 /* try all IEEE1284 modes, for one device only
296 * XXX We should implement the IEEE1284.3 standard to detect
297 * daisy chained devices
300 error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID);
302 if ((ppb->state == PPB_ERROR) && (ppb->error == PPB_NOT_IEEE1284))
305 ppb_1284_terminate(bus);
307 device_printf(bus, "IEEE1284 device found ");
309 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE, 0))) {
311 ppb_1284_terminate(bus);
314 if (!(error = ppb_1284_negociate(bus, PPB_PS2, 0))) {
316 ppb_1284_terminate(bus);
319 if (!(error = ppb_1284_negociate(bus, PPB_ECP, 0))) {
321 ppb_1284_terminate(bus);
324 if (!(error = ppb_1284_negociate(bus, PPB_ECP, PPB_USE_RLE))) {
326 ppb_1284_terminate(bus);
329 if (!(error = ppb_1284_negociate(bus, PPB_EPP, 0))) {
331 ppb_1284_terminate(bus);
334 /* try more IEEE1284 modes */
336 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE,
338 printf("/NIBBLE_ID");
339 ppb_1284_terminate(bus);
342 if (!(error = ppb_1284_negociate(bus, PPB_PS2,
345 ppb_1284_terminate(bus);
348 if (!(error = ppb_1284_negociate(bus, PPB_ECP,
351 ppb_1284_terminate(bus);
354 if (!(error = ppb_1284_negociate(bus, PPB_ECP,
355 PPB_REQUEST_ID | PPB_USE_RLE))) {
356 printf("/ECP_RLE_ID");
357 ppb_1284_terminate(bus);
360 if (!(error = ppb_1284_negociate(bus, PPB_COMPATIBLE,
361 PPB_EXTENSIBILITY_LINK))) {
362 printf("/Extensibility Link");
363 ppb_1284_terminate(bus);
369 /* detect PnP devices */
370 ppb->class_id = ppb_pnp_detect(bus);
378 #endif /* !DONTPROBE_1284 */
381 ppbus_attach(device_t dev)
383 struct ppb_data *ppb = device_get_softc(dev);
386 error = BUS_READ_IVAR(device_get_parent(dev), dev, PPC_IVAR_LOCK,
387 (uintptr_t *)&ppb->ppc_lock);
389 device_printf(dev, "Unable to fetch parent's lock\n");
394 ppb->ppc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
396 if (ppb->ppc_irq_res != NULL) {
397 mtx_lock(ppb->ppc_lock);
398 error = BUS_WRITE_IVAR(device_get_parent(dev), dev,
399 PPC_IVAR_INTR_HANDLER, (uintptr_t)&ppbus_intr);
400 mtx_unlock(ppb->ppc_lock);
402 device_printf(dev, "Unable to set interrupt handler\n");
407 /* Locate our children */
408 bus_generic_probe(dev);
410 #ifndef DONTPROBE_1284
411 /* detect IEEE1284 compliant devices */
412 mtx_lock(ppb->ppc_lock);
414 mtx_unlock(ppb->ppc_lock);
415 #endif /* !DONTPROBE_1284 */
417 /* launch attachment of the added children */
418 bus_generic_attach(dev);
424 ppbus_detach(device_t dev)
428 error = bus_generic_detach(dev);
432 /* detach & delete all children */
433 device_delete_children(dev);
439 ppbus_intr(void *arg)
441 struct ppb_device *ppbdev;
442 struct ppb_data *ppb = arg;
444 mtx_assert(ppb->ppc_lock, MA_OWNED);
445 if (ppb->ppb_owner == NULL)
448 ppbdev = device_get_ivars(ppb->ppb_owner);
449 if (ppbdev->intr_hook == NULL)
452 ppbdev->intr_hook(ppbdev->intr_arg);
457 ppbus_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
458 driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
460 struct ppb_device *ppbdev = device_get_ivars(child);
461 struct ppb_data *ppb = DEVTOSOFTC(bus);
463 /* We do not support filters. */
464 if (filt != NULL || ihand == NULL)
467 /* Can only attach handlers to the parent device's resource. */
468 if (ppb->ppc_irq_res != r)
471 mtx_lock(ppb->ppc_lock);
472 ppbdev->intr_hook = ihand;
473 ppbdev->intr_arg = arg;
475 mtx_unlock(ppb->ppc_lock);
481 ppbus_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih)
483 struct ppb_device *ppbdev = device_get_ivars(child);
484 struct ppb_data *ppb = DEVTOSOFTC(bus);
486 mtx_lock(ppb->ppc_lock);
487 if (ppbdev != ih || ppb->ppc_irq_res != r) {
488 mtx_unlock(ppb->ppc_lock);
492 ppbdev->intr_hook = NULL;
493 mtx_unlock(ppb->ppc_lock);
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)
508 struct ppb_data *ppb = DEVTOSOFTC(bus);
509 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
512 mtx_assert(ppb->ppc_lock, MA_OWNED);
514 if (ppb->ppb_owner) {
516 case PPB_WAIT | PPB_INTR:
517 error = mtx_sleep(ppb, ppb->ppc_lock,
518 PPBPRI | PCATCH, "ppbreq", 0);
521 case PPB_WAIT | PPB_NOINTR:
522 error = mtx_sleep(ppb, ppb->ppc_lock, PPBPRI,
527 return (EWOULDBLOCK);
531 ppb->ppb_owner = dev;
533 /* restore the context of the device
534 * The first time, ctx.valid is certainly false
535 * then do not change anything. This is useful for
536 * drivers that do not set there operating mode
539 if (ppbdev->ctx.valid)
540 ppb_set_mode(bus, ppbdev->ctx.mode);
552 * Release the device allocated with ppb_request_bus()
555 ppb_release_bus(device_t bus, device_t dev)
557 struct ppb_data *ppb = DEVTOSOFTC(bus);
558 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
560 mtx_assert(ppb->ppc_lock, MA_OWNED);
561 if (ppb->ppb_owner != dev)
564 /* save the context of the device */
565 ppbdev->ctx.mode = ppb_get_mode(bus);
567 /* ok, now the context of the device is valid */
568 ppbdev->ctx.valid = 1;
572 /* wakeup waiting processes */
578 static devclass_t ppbus_devclass;
580 static device_method_t ppbus_methods[] = {
581 /* device interface */
582 DEVMETHOD(device_probe, ppbus_probe),
583 DEVMETHOD(device_attach, ppbus_attach),
584 DEVMETHOD(device_detach, ppbus_detach),
587 DEVMETHOD(bus_add_child, ppbus_add_child),
588 DEVMETHOD(bus_print_child, ppbus_print_child),
589 DEVMETHOD(bus_read_ivar, ppbus_read_ivar),
590 DEVMETHOD(bus_write_ivar, ppbus_write_ivar),
591 DEVMETHOD(bus_setup_intr, ppbus_setup_intr),
592 DEVMETHOD(bus_teardown_intr, ppbus_teardown_intr),
593 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
594 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
598 static driver_t ppbus_driver = {
601 sizeof(struct ppb_data),
603 DRIVER_MODULE(ppbus, ppc, ppbus_driver, ppbus_devclass, 0, 0);