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>
40 #include <dev/ppbus/ppbconf.h>
41 #include <dev/ppbus/ppb_1284.h>
45 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
47 static MALLOC_DEFINE(M_PPBUSDEV, "ppbusdev", "Parallel Port bus device");
55 ppbus_print_child(device_t bus, device_t dev)
57 struct ppb_device *ppbdev;
59 bus_print_child_header(bus, dev);
61 ppbdev = (struct ppb_device *)device_get_ivars(dev);
63 if (ppbdev->flags != 0)
64 printf(" flags 0x%x", ppbdev->flags);
66 printf(" on %s%d\n", device_get_name(bus), device_get_unit(bus));
72 ppbus_probe(device_t dev)
74 device_set_desc(dev, "Parallel port bus");
82 * Add a ppbus device, allocate/initialize the ivars
85 ppbus_add_child(device_t dev, int order, const char *name, int unit)
87 struct ppb_device *ppbdev;
90 /* allocate ivars for the new ppbus child */
91 ppbdev = malloc(sizeof(struct ppb_device), M_PPBUSDEV,
96 /* initialize the ivars */
99 /* add the device as a child to the ppbus bus with the allocated
101 child = device_add_child_ordered(dev, order, name, unit);
102 device_set_ivars(child, ppbdev);
108 ppbus_read_ivar(device_t bus, device_t dev, int index, uintptr_t* val)
110 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
113 case PPBUS_IVAR_MODE:
114 /* XXX yet device mode = ppbus mode = chipset mode */
115 *val = (u_long)ppb_get_mode(bus);
116 ppbdev->mode = (u_short)*val;
119 *val = (u_long)ppbdev->avm;
122 BUS_READ_IVAR(device_get_parent(bus), bus, PPC_IVAR_IRQ, val);
132 ppbus_write_ivar(device_t bus, device_t dev, int index, u_long val)
134 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
137 case PPBUS_IVAR_MODE:
138 /* XXX yet device mode = ppbus mode = chipset mode */
139 ppb_set_mode(bus,val);
140 ppbdev->mode = ppb_get_mode(bus);
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 occurence 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 = 0;
214 char str[PPB_PnP_STRING_SIZE+1];
215 int unit = device_get_unit(bus);
217 printf("Probing for PnP devices on ppbus%d:\n", unit);
219 if ((error = ppb_1284_read_id(bus, PPB_NIBBLE, str,
220 PPB_PnP_STRING_SIZE, &len)))
224 printf("ppb: <PnP> %d characters: ", len);
225 for (i = 0; i < len; i++)
226 printf("%c(0x%x) ", str[i], str[i]);
230 /* replace ';' characters by '\0' */
231 for (i = 0; i < len; i++)
232 str[i] = (str[i] == ';') ? '\0' : str[i];
234 if ((token = search_token(str, len, "MFG")) != NULL ||
235 (token = search_token(str, len, "MANUFACTURER")) != NULL)
236 printf("ppbus%d: <%s", unit,
237 search_token(token, UNKNOWN_LENGTH, ":") + 1);
239 printf("ppbus%d: <unknown", unit);
241 if ((token = search_token(str, len, "MDL")) != NULL ||
242 (token = search_token(str, len, "MODEL")) != NULL)
244 search_token(token, UNKNOWN_LENGTH, ":") + 1);
248 if ((token = search_token(str, len, "VER")) != NULL)
250 search_token(token, UNKNOWN_LENGTH, ":") + 1);
252 if ((token = search_token(str, len, "REV")) != NULL)
254 search_token(token, UNKNOWN_LENGTH, ":") + 1);
258 if ((token = search_token(str, len, "CLS")) != NULL) {
259 class = search_token(token, UNKNOWN_LENGTH, ":") + 1;
260 printf(" %s", class);
263 if ((token = search_token(str, len, "CMD")) != NULL ||
264 (token = search_token(str, len, "COMMAND")) != NULL)
266 search_token(token, UNKNOWN_LENGTH, ":") + 1);
271 /* identify class ident */
272 for (i = 0; pnp_tokens[i] != NULL; i++) {
273 if (search_token(class, len, pnp_tokens[i]) != NULL) {
279 class_id = PPB_PnP_UNKNOWN;
288 * Scan the ppbus for IEEE1284 compliant devices
291 ppb_scan_bus(device_t bus)
293 struct ppb_data * ppb = (struct ppb_data *)device_get_softc(bus);
295 int unit = device_get_unit(bus);
297 /* try all IEEE1284 modes, for one device only
299 * XXX We should implement the IEEE1284.3 standard to detect
300 * daisy chained devices
303 error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID);
305 if ((ppb->state == PPB_ERROR) && (ppb->error == PPB_NOT_IEEE1284))
308 ppb_1284_terminate(bus);
310 printf("ppbus%d: IEEE1284 device found ", unit);
312 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE, 0))) {
314 ppb_1284_terminate(bus);
317 if (!(error = ppb_1284_negociate(bus, PPB_PS2, 0))) {
319 ppb_1284_terminate(bus);
322 if (!(error = ppb_1284_negociate(bus, PPB_ECP, 0))) {
324 ppb_1284_terminate(bus);
327 if (!(error = ppb_1284_negociate(bus, PPB_ECP, PPB_USE_RLE))) {
329 ppb_1284_terminate(bus);
332 if (!(error = ppb_1284_negociate(bus, PPB_EPP, 0))) {
334 ppb_1284_terminate(bus);
337 /* try more IEEE1284 modes */
339 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE,
341 printf("/NIBBLE_ID");
342 ppb_1284_terminate(bus);
345 if (!(error = ppb_1284_negociate(bus, PPB_PS2,
348 ppb_1284_terminate(bus);
351 if (!(error = ppb_1284_negociate(bus, PPB_ECP,
354 ppb_1284_terminate(bus);
357 if (!(error = ppb_1284_negociate(bus, PPB_ECP,
358 PPB_REQUEST_ID | PPB_USE_RLE))) {
359 printf("/ECP_RLE_ID");
360 ppb_1284_terminate(bus);
363 if (!(error = ppb_1284_negociate(bus, PPB_COMPATIBLE,
364 PPB_EXTENSIBILITY_LINK))) {
365 printf("/Extensibility Link");
366 ppb_1284_terminate(bus);
372 /* detect PnP devices */
373 ppb->class_id = ppb_pnp_detect(bus);
381 #endif /* !DONTPROBE_1284 */
384 ppbus_attach(device_t dev)
387 /* Locate our children */
388 bus_generic_probe(dev);
390 #ifndef DONTPROBE_1284
391 /* detect IEEE1284 compliant devices */
393 #endif /* !DONTPROBE_1284 */
395 /* launch attachement of the added children */
396 bus_generic_attach(dev);
402 ppbus_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
403 void (*ihand)(void *), void *arg, void **cookiep)
406 struct ppb_data *ppb = DEVTOSOFTC(bus);
407 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(child);
409 /* a device driver must own the bus to register an interrupt */
410 if (ppb->ppb_owner != child)
413 if ((error = BUS_SETUP_INTR(device_get_parent(bus), child, r, flags,
414 ihand, arg, cookiep)))
417 /* store the resource and the cookie for eventually forcing
418 * handler unregistration
420 ppbdev->intr_cookie = *cookiep;
421 ppbdev->intr_resource = r;
427 ppbus_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih)
429 struct ppb_data *ppb = DEVTOSOFTC(bus);
430 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(child);
432 /* a device driver must own the bus to unregister an interrupt */
433 if ((ppb->ppb_owner != child) || (ppbdev->intr_cookie != ih) ||
434 (ppbdev->intr_resource != r))
437 ppbdev->intr_cookie = 0;
438 ppbdev->intr_resource = 0;
440 /* pass unregistration to the upper layer */
441 return (BUS_TEARDOWN_INTR(device_get_parent(bus), child, r, ih));
447 * Allocate the device to perform transfers.
449 * how : PPB_WAIT or PPB_DONTWAIT
452 ppb_request_bus(device_t bus, device_t dev, int how)
455 struct ppb_data *ppb = DEVTOSOFTC(bus);
456 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
460 if (ppb->ppb_owner) {
464 case (PPB_WAIT | PPB_INTR):
465 error = tsleep(ppb, PPBPRI|PCATCH, "ppbreq", 0);
468 case (PPB_WAIT | PPB_NOINTR):
469 error = tsleep(ppb, PPBPRI, "ppbreq", 0);
473 return (EWOULDBLOCK);
478 ppb->ppb_owner = dev;
480 /* restore the context of the device
481 * The first time, ctx.valid is certainly false
482 * then do not change anything. This is usefull for
483 * drivers that do not set there operating mode
486 if (ppbdev->ctx.valid)
487 ppb_set_mode(bus, ppbdev->ctx.mode);
500 * Release the device allocated with ppb_request_bus()
503 ppb_release_bus(device_t bus, device_t dev)
506 struct ppb_data *ppb = DEVTOSOFTC(bus);
507 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
509 if (ppbdev->intr_resource != 0)
510 /* force interrupt handler unregistration when the ppbus is released */
511 if ((error = BUS_TEARDOWN_INTR(bus, dev, ppbdev->intr_resource,
512 ppbdev->intr_cookie)))
516 if (ppb->ppb_owner != dev) {
524 /* save the context of the device */
525 ppbdev->ctx.mode = ppb_get_mode(bus);
527 /* ok, now the context of the device is valid */
528 ppbdev->ctx.valid = 1;
530 /* wakeup waiting processes */
536 static devclass_t ppbus_devclass;
538 static device_method_t ppbus_methods[] = {
539 /* device interface */
540 DEVMETHOD(device_probe, ppbus_probe),
541 DEVMETHOD(device_attach, ppbus_attach),
544 DEVMETHOD(bus_add_child, ppbus_add_child),
545 DEVMETHOD(bus_print_child, ppbus_print_child),
546 DEVMETHOD(bus_read_ivar, ppbus_read_ivar),
547 DEVMETHOD(bus_write_ivar, ppbus_write_ivar),
548 DEVMETHOD(bus_setup_intr, ppbus_setup_intr),
549 DEVMETHOD(bus_teardown_intr, ppbus_teardown_intr),
550 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
555 static driver_t ppbus_driver = {
558 sizeof(struct ppb_data),
560 DRIVER_MODULE(ppbus, ppc, ppbus_driver, ppbus_devclass, 0, 0);