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>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
40 #include <sys/malloc.h>
43 #include <machine/resource.h>
45 #include <dev/ppbus/ppbconf.h>
46 #include <dev/ppbus/ppb_1284.h>
50 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
52 static MALLOC_DEFINE(M_PPBUSDEV, "ppbusdev", "Parallel Port bus device");
55 static int ppbus_intr(void *arg);
62 ppbus_print_child(device_t bus, device_t dev)
64 struct ppb_device *ppbdev;
67 retval = bus_print_child_header(bus, dev);
69 ppbdev = (struct ppb_device *)device_get_ivars(dev);
71 if (ppbdev->flags != 0)
72 retval += printf(" flags 0x%x", ppbdev->flags);
74 retval += bus_print_child_footer(bus, dev);
80 ppbus_probe(device_t dev)
82 device_set_desc(dev, "Parallel port bus");
90 * Add a ppbus device, allocate/initialize the ivars
93 ppbus_add_child(device_t dev, u_int order, const char *name, int unit)
95 struct ppb_device *ppbdev;
98 /* allocate ivars for the new ppbus child */
99 ppbdev = malloc(sizeof(struct ppb_device), M_PPBUSDEV,
104 /* initialize the ivars */
107 /* add the device as a child to the ppbus bus with the allocated
109 child = device_add_child_ordered(dev, order, name, unit);
110 device_set_ivars(child, ppbdev);
116 ppbus_read_ivar(device_t bus, device_t dev, int index, uintptr_t* val)
120 case PPBUS_IVAR_MODE:
121 /* XXX yet device mode = ppbus mode = chipset mode */
122 *val = (u_long)ppb_get_mode(bus);
132 ppbus_write_ivar(device_t bus, device_t dev, int index, uintptr_t val)
136 case PPBUS_IVAR_MODE:
137 /* XXX yet device mode = ppbus mode = chipset mode */
138 ppb_set_mode(bus, val);
147 #define PPB_PNP_PRINTER 0
148 #define PPB_PNP_MODEM 1
149 #define PPB_PNP_NET 2
150 #define PPB_PNP_HDC 3
151 #define PPB_PNP_PCMCIA 4
152 #define PPB_PNP_MEDIA 5
153 #define PPB_PNP_FDC 6
154 #define PPB_PNP_PORTS 7
155 #define PPB_PNP_SCANNER 8
156 #define PPB_PNP_DIGICAM 9
158 #ifndef DONTPROBE_1284
160 static char *pnp_tokens[] = {
161 "PRINTER", "MODEM", "NET", "HDC", "PCMCIA", "MEDIA",
162 "FDC", "PORTS", "SCANNER", "DIGICAM", "", NULL };
165 static char *pnp_classes[] = {
166 "printer", "modem", "network device",
167 "hard disk", "PCMCIA", "multimedia device",
168 "floppy disk", "ports", "scanner",
169 "digital camera", "unknown device", NULL };
175 * Search the first occurence of a token within a string
178 search_token(char *str, int slen, char *token)
182 #define UNKNOWN_LENGTH -1
184 if (slen == UNKNOWN_LENGTH)
185 /* get string's length */
188 /* get token's length */
189 tlen = strlen(token);
193 for (i = 0; i <= slen-tlen; i++) {
194 if (strncmp(str + i, token, tlen) == 0)
204 * Returns the class id. of the peripherial, -1 otherwise
207 ppb_pnp_detect(device_t bus)
209 char *token, *class = 0;
212 char str[PPB_PnP_STRING_SIZE+1];
214 device_printf(bus, "Probing for PnP devices:\n");
216 if ((error = ppb_1284_read_id(bus, PPB_NIBBLE, str,
217 PPB_PnP_STRING_SIZE, &len)))
221 device_printf(bus, "<PnP> %d characters: ", len);
222 for (i = 0; i < len; i++)
223 printf("%c(0x%x) ", str[i], str[i]);
227 /* replace ';' characters by '\0' */
228 for (i = 0; i < len; i++)
229 str[i] = (str[i] == ';') ? '\0' : str[i];
231 if ((token = search_token(str, len, "MFG")) != NULL ||
232 (token = search_token(str, len, "MANUFACTURER")) != NULL)
233 device_printf(bus, "<%s",
234 search_token(token, UNKNOWN_LENGTH, ":") + 1);
236 device_printf(bus, "<unknown");
238 if ((token = search_token(str, len, "MDL")) != NULL ||
239 (token = search_token(str, len, "MODEL")) != NULL)
241 search_token(token, UNKNOWN_LENGTH, ":") + 1);
245 if ((token = search_token(str, len, "VER")) != NULL)
247 search_token(token, UNKNOWN_LENGTH, ":") + 1);
249 if ((token = search_token(str, len, "REV")) != NULL)
251 search_token(token, UNKNOWN_LENGTH, ":") + 1);
255 if ((token = search_token(str, len, "CLS")) != NULL) {
256 class = search_token(token, UNKNOWN_LENGTH, ":") + 1;
257 printf(" %s", class);
260 if ((token = search_token(str, len, "CMD")) != NULL ||
261 (token = search_token(str, len, "COMMAND")) != NULL)
263 search_token(token, UNKNOWN_LENGTH, ":") + 1);
268 /* identify class ident */
269 for (i = 0; pnp_tokens[i] != NULL; i++) {
270 if (search_token(class, len, pnp_tokens[i]) != NULL) {
276 class_id = PPB_PnP_UNKNOWN;
285 * Scan the ppbus for IEEE1284 compliant devices
288 ppb_scan_bus(device_t bus)
290 struct ppb_data * ppb = (struct ppb_data *)device_get_softc(bus);
293 /* try all IEEE1284 modes, for one device only
295 * XXX We should implement the IEEE1284.3 standard to detect
296 * daisy chained devices
299 error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID);
301 if ((ppb->state == PPB_ERROR) && (ppb->error == PPB_NOT_IEEE1284))
304 ppb_1284_terminate(bus);
306 device_printf(bus, "IEEE1284 device found ");
308 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE, 0))) {
310 ppb_1284_terminate(bus);
313 if (!(error = ppb_1284_negociate(bus, PPB_PS2, 0))) {
315 ppb_1284_terminate(bus);
318 if (!(error = ppb_1284_negociate(bus, PPB_ECP, 0))) {
320 ppb_1284_terminate(bus);
323 if (!(error = ppb_1284_negociate(bus, PPB_ECP, PPB_USE_RLE))) {
325 ppb_1284_terminate(bus);
328 if (!(error = ppb_1284_negociate(bus, PPB_EPP, 0))) {
330 ppb_1284_terminate(bus);
333 /* try more IEEE1284 modes */
335 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE,
337 printf("/NIBBLE_ID");
338 ppb_1284_terminate(bus);
341 if (!(error = ppb_1284_negociate(bus, PPB_PS2,
344 ppb_1284_terminate(bus);
347 if (!(error = ppb_1284_negociate(bus, PPB_ECP,
350 ppb_1284_terminate(bus);
353 if (!(error = ppb_1284_negociate(bus, PPB_ECP,
354 PPB_REQUEST_ID | PPB_USE_RLE))) {
355 printf("/ECP_RLE_ID");
356 ppb_1284_terminate(bus);
359 if (!(error = ppb_1284_negociate(bus, PPB_COMPATIBLE,
360 PPB_EXTENSIBILITY_LINK))) {
361 printf("/Extensibility Link");
362 ppb_1284_terminate(bus);
368 /* detect PnP devices */
369 ppb->class_id = ppb_pnp_detect(bus);
377 #endif /* !DONTPROBE_1284 */
380 ppbus_attach(device_t dev)
382 struct ppb_data *ppb = device_get_softc(dev);
385 error = BUS_READ_IVAR(device_get_parent(dev), dev, PPC_IVAR_LOCK,
386 (uintptr_t *)&ppb->ppc_lock);
388 device_printf(dev, "Unable to fetch parent's lock\n");
393 ppb->ppc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
395 if (ppb->ppc_irq_res != NULL) {
396 mtx_lock(ppb->ppc_lock);
397 error = BUS_WRITE_IVAR(device_get_parent(dev), dev,
398 PPC_IVAR_INTR_HANDLER, (uintptr_t)&ppbus_intr);
399 mtx_unlock(ppb->ppc_lock);
401 device_printf(dev, "Unable to set interrupt handler\n");
406 /* Locate our children */
407 bus_generic_probe(dev);
409 #ifndef DONTPROBE_1284
410 /* detect IEEE1284 compliant devices */
411 mtx_lock(ppb->ppc_lock);
413 mtx_unlock(ppb->ppc_lock);
414 #endif /* !DONTPROBE_1284 */
416 /* launch attachment of the added children */
417 bus_generic_attach(dev);
423 ppbus_detach(device_t dev)
426 int error, nchildren, i;
428 error = bus_generic_detach(dev);
432 /* detach & delete all children */
433 if (!device_get_children(dev, &children, &nchildren)) {
434 for (i = 0; i < nchildren; i++)
436 device_delete_child(dev, children[i]);
437 free(children, M_TEMP);
444 ppbus_intr(void *arg)
446 struct ppb_device *ppbdev;
447 struct ppb_data *ppb = arg;
449 mtx_assert(ppb->ppc_lock, MA_OWNED);
450 if (ppb->ppb_owner == NULL)
453 ppbdev = device_get_ivars(ppb->ppb_owner);
454 if (ppbdev->intr_hook == NULL)
457 ppbdev->intr_hook(ppbdev->intr_arg);
462 ppbus_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
463 driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
465 struct ppb_device *ppbdev = device_get_ivars(child);
466 struct ppb_data *ppb = DEVTOSOFTC(bus);
468 /* We do not support filters. */
469 if (filt != NULL || ihand == NULL)
472 /* Can only attach handlers to the parent device's resource. */
473 if (ppb->ppc_irq_res != r)
476 mtx_lock(ppb->ppc_lock);
477 ppbdev->intr_hook = ihand;
478 ppbdev->intr_arg = arg;
480 mtx_unlock(ppb->ppc_lock);
486 ppbus_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih)
488 struct ppb_device *ppbdev = device_get_ivars(child);
489 struct ppb_data *ppb = DEVTOSOFTC(bus);
491 mtx_lock(ppb->ppc_lock);
492 if (ppbdev != ih || ppb->ppc_irq_res != r) {
493 mtx_unlock(ppb->ppc_lock);
497 ppbdev->intr_hook = NULL;
498 mtx_unlock(ppb->ppc_lock);
506 * Allocate the device to perform transfers.
508 * how : PPB_WAIT or PPB_DONTWAIT
511 ppb_request_bus(device_t bus, device_t dev, int how)
513 struct ppb_data *ppb = DEVTOSOFTC(bus);
514 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
517 mtx_assert(ppb->ppc_lock, MA_OWNED);
519 if (ppb->ppb_owner) {
521 case PPB_WAIT | PPB_INTR:
522 error = mtx_sleep(ppb, ppb->ppc_lock,
523 PPBPRI | PCATCH, "ppbreq", 0);
526 case PPB_WAIT | PPB_NOINTR:
527 error = mtx_sleep(ppb, ppb->ppc_lock, PPBPRI,
532 return (EWOULDBLOCK);
536 ppb->ppb_owner = dev;
538 /* restore the context of the device
539 * The first time, ctx.valid is certainly false
540 * then do not change anything. This is usefull for
541 * drivers that do not set there operating mode
544 if (ppbdev->ctx.valid)
545 ppb_set_mode(bus, ppbdev->ctx.mode);
557 * Release the device allocated with ppb_request_bus()
560 ppb_release_bus(device_t bus, device_t dev)
562 struct ppb_data *ppb = DEVTOSOFTC(bus);
563 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
565 mtx_assert(ppb->ppc_lock, MA_OWNED);
566 if (ppb->ppb_owner != dev)
569 /* save the context of the device */
570 ppbdev->ctx.mode = ppb_get_mode(bus);
572 /* ok, now the context of the device is valid */
573 ppbdev->ctx.valid = 1;
577 /* wakeup waiting processes */
583 static devclass_t ppbus_devclass;
585 static device_method_t ppbus_methods[] = {
586 /* device interface */
587 DEVMETHOD(device_probe, ppbus_probe),
588 DEVMETHOD(device_attach, ppbus_attach),
589 DEVMETHOD(device_detach, ppbus_detach),
592 DEVMETHOD(bus_add_child, ppbus_add_child),
593 DEVMETHOD(bus_print_child, ppbus_print_child),
594 DEVMETHOD(bus_read_ivar, ppbus_read_ivar),
595 DEVMETHOD(bus_write_ivar, ppbus_write_ivar),
596 DEVMETHOD(bus_setup_intr, ppbus_setup_intr),
597 DEVMETHOD(bus_teardown_intr, ppbus_teardown_intr),
598 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
599 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
604 static driver_t ppbus_driver = {
607 sizeof(struct ppb_data),
609 DRIVER_MODULE(ppbus, ppc, ppbus_driver, ppbus_devclass, 0, 0);