ppbconf.c revision 184130
1/*- 2 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/dev/ppbus/ppbconf.c 184130 2008-10-21 18:30:10Z jhb $"); 31#include "opt_ppb_1284.h" 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/kernel.h> 36#include <sys/module.h> 37#include <sys/bus.h> 38#include <sys/malloc.h> 39#include <sys/rman.h> 40 41#include <machine/resource.h> 42 43#include <dev/ppbus/ppbconf.h> 44#include <dev/ppbus/ppb_1284.h> 45 46#include "ppbus_if.h" 47 48#define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev)) 49 50static MALLOC_DEFINE(M_PPBUSDEV, "ppbusdev", "Parallel Port bus device"); 51 52 53/* 54 * Device methods 55 */ 56 57static int 58ppbus_print_child(device_t bus, device_t dev) 59{ 60 struct ppb_device *ppbdev; 61 int retval; 62 63 retval = bus_print_child_header(bus, dev); 64 65 ppbdev = (struct ppb_device *)device_get_ivars(dev); 66 67 if (ppbdev->flags != 0) 68 retval += printf(" flags 0x%x", ppbdev->flags); 69 70 retval += bus_print_child_footer(bus, dev); 71 72 return (retval); 73} 74 75static int 76ppbus_probe(device_t dev) 77{ 78 device_set_desc(dev, "Parallel port bus"); 79 80 return (0); 81} 82 83/* 84 * ppbus_add_child() 85 * 86 * Add a ppbus device, allocate/initialize the ivars 87 */ 88static device_t 89ppbus_add_child(device_t dev, int order, const char *name, int unit) 90{ 91 struct ppb_device *ppbdev; 92 device_t child; 93 94 /* allocate ivars for the new ppbus child */ 95 ppbdev = malloc(sizeof(struct ppb_device), M_PPBUSDEV, 96 M_NOWAIT | M_ZERO); 97 if (!ppbdev) 98 return NULL; 99 100 /* initialize the ivars */ 101 ppbdev->name = name; 102 103 /* add the device as a child to the ppbus bus with the allocated 104 * ivars */ 105 child = device_add_child_ordered(dev, order, name, unit); 106 device_set_ivars(child, ppbdev); 107 108 return child; 109} 110 111static int 112ppbus_read_ivar(device_t bus, device_t dev, int index, uintptr_t* val) 113{ 114 115 switch (index) { 116 case PPBUS_IVAR_MODE: 117 /* XXX yet device mode = ppbus mode = chipset mode */ 118 *val = (u_long)ppb_get_mode(bus); 119 break; 120 default: 121 return (ENOENT); 122 } 123 124 return (0); 125} 126 127static int 128ppbus_write_ivar(device_t bus, device_t dev, int index, u_long val) 129{ 130 131 switch (index) { 132 case PPBUS_IVAR_MODE: 133 /* XXX yet device mode = ppbus mode = chipset mode */ 134 ppb_set_mode(bus, val); 135 break; 136 default: 137 return (ENOENT); 138 } 139 140 return (0); 141} 142 143#define PPB_PNP_PRINTER 0 144#define PPB_PNP_MODEM 1 145#define PPB_PNP_NET 2 146#define PPB_PNP_HDC 3 147#define PPB_PNP_PCMCIA 4 148#define PPB_PNP_MEDIA 5 149#define PPB_PNP_FDC 6 150#define PPB_PNP_PORTS 7 151#define PPB_PNP_SCANNER 8 152#define PPB_PNP_DIGICAM 9 153 154#ifndef DONTPROBE_1284 155 156static char *pnp_tokens[] = { 157 "PRINTER", "MODEM", "NET", "HDC", "PCMCIA", "MEDIA", 158 "FDC", "PORTS", "SCANNER", "DIGICAM", "", NULL }; 159 160#if 0 161static char *pnp_classes[] = { 162 "printer", "modem", "network device", 163 "hard disk", "PCMCIA", "multimedia device", 164 "floppy disk", "ports", "scanner", 165 "digital camera", "unknown device", NULL }; 166#endif 167 168/* 169 * search_token() 170 * 171 * Search the first occurence of a token within a string 172 */ 173static char * 174search_token(char *str, int slen, char *token) 175{ 176 int tlen, i; 177 178#define UNKNOWN_LENGTH -1 179 180 if (slen == UNKNOWN_LENGTH) 181 /* get string's length */ 182 slen = strlen(str); 183 184 /* get token's length */ 185 tlen = strlen(token); 186 if (tlen == 0) 187 return (str); 188 189 for (i = 0; i <= slen-tlen; i++) { 190 if (strncmp(str + i, token, tlen) == 0) 191 return (&str[i]); 192 } 193 194 return (NULL); 195} 196 197/* 198 * ppb_pnp_detect() 199 * 200 * Returns the class id. of the peripherial, -1 otherwise 201 */ 202static int 203ppb_pnp_detect(device_t bus) 204{ 205 char *token, *class = 0; 206 int i, len, error; 207 int class_id = -1; 208 char str[PPB_PnP_STRING_SIZE+1]; 209 210 device_printf(bus, "Probing for PnP devices:\n"); 211 212 if ((error = ppb_1284_read_id(bus, PPB_NIBBLE, str, 213 PPB_PnP_STRING_SIZE, &len))) 214 goto end_detect; 215 216#ifdef DEBUG_1284 217 device_printf(bus, "<PnP> %d characters: ", len); 218 for (i = 0; i < len; i++) 219 printf("%c(0x%x) ", str[i], str[i]); 220 printf("\n"); 221#endif 222 223 /* replace ';' characters by '\0' */ 224 for (i = 0; i < len; i++) 225 str[i] = (str[i] == ';') ? '\0' : str[i]; 226 227 if ((token = search_token(str, len, "MFG")) != NULL || 228 (token = search_token(str, len, "MANUFACTURER")) != NULL) 229 device_printf(bus, "<%s", 230 search_token(token, UNKNOWN_LENGTH, ":") + 1); 231 else 232 device_printf(bus, "<unknown"); 233 234 if ((token = search_token(str, len, "MDL")) != NULL || 235 (token = search_token(str, len, "MODEL")) != NULL) 236 printf(" %s", 237 search_token(token, UNKNOWN_LENGTH, ":") + 1); 238 else 239 printf(" unknown"); 240 241 if ((token = search_token(str, len, "VER")) != NULL) 242 printf("/%s", 243 search_token(token, UNKNOWN_LENGTH, ":") + 1); 244 245 if ((token = search_token(str, len, "REV")) != NULL) 246 printf(".%s", 247 search_token(token, UNKNOWN_LENGTH, ":") + 1); 248 249 printf(">"); 250 251 if ((token = search_token(str, len, "CLS")) != NULL) { 252 class = search_token(token, UNKNOWN_LENGTH, ":") + 1; 253 printf(" %s", class); 254 } 255 256 if ((token = search_token(str, len, "CMD")) != NULL || 257 (token = search_token(str, len, "COMMAND")) != NULL) 258 printf(" %s", 259 search_token(token, UNKNOWN_LENGTH, ":") + 1); 260 261 printf("\n"); 262 263 if (class) 264 /* identify class ident */ 265 for (i = 0; pnp_tokens[i] != NULL; i++) { 266 if (search_token(class, len, pnp_tokens[i]) != NULL) { 267 class_id = i; 268 goto end_detect; 269 } 270 } 271 272 class_id = PPB_PnP_UNKNOWN; 273 274end_detect: 275 return (class_id); 276} 277 278/* 279 * ppb_scan_bus() 280 * 281 * Scan the ppbus for IEEE1284 compliant devices 282 */ 283static int 284ppb_scan_bus(device_t bus) 285{ 286 struct ppb_data * ppb = (struct ppb_data *)device_get_softc(bus); 287 int error = 0; 288 289 /* try all IEEE1284 modes, for one device only 290 * 291 * XXX We should implement the IEEE1284.3 standard to detect 292 * daisy chained devices 293 */ 294 295 error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID); 296 297 if ((ppb->state == PPB_ERROR) && (ppb->error == PPB_NOT_IEEE1284)) 298 goto end_scan; 299 300 ppb_1284_terminate(bus); 301 302 device_printf(bus, "IEEE1284 device found "); 303 304 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE, 0))) { 305 printf("/NIBBLE"); 306 ppb_1284_terminate(bus); 307 } 308 309 if (!(error = ppb_1284_negociate(bus, PPB_PS2, 0))) { 310 printf("/PS2"); 311 ppb_1284_terminate(bus); 312 } 313 314 if (!(error = ppb_1284_negociate(bus, PPB_ECP, 0))) { 315 printf("/ECP"); 316 ppb_1284_terminate(bus); 317 } 318 319 if (!(error = ppb_1284_negociate(bus, PPB_ECP, PPB_USE_RLE))) { 320 printf("/ECP_RLE"); 321 ppb_1284_terminate(bus); 322 } 323 324 if (!(error = ppb_1284_negociate(bus, PPB_EPP, 0))) { 325 printf("/EPP"); 326 ppb_1284_terminate(bus); 327 } 328 329 /* try more IEEE1284 modes */ 330 if (bootverbose) { 331 if (!(error = ppb_1284_negociate(bus, PPB_NIBBLE, 332 PPB_REQUEST_ID))) { 333 printf("/NIBBLE_ID"); 334 ppb_1284_terminate(bus); 335 } 336 337 if (!(error = ppb_1284_negociate(bus, PPB_PS2, 338 PPB_REQUEST_ID))) { 339 printf("/PS2_ID"); 340 ppb_1284_terminate(bus); 341 } 342 343 if (!(error = ppb_1284_negociate(bus, PPB_ECP, 344 PPB_REQUEST_ID))) { 345 printf("/ECP_ID"); 346 ppb_1284_terminate(bus); 347 } 348 349 if (!(error = ppb_1284_negociate(bus, PPB_ECP, 350 PPB_REQUEST_ID | PPB_USE_RLE))) { 351 printf("/ECP_RLE_ID"); 352 ppb_1284_terminate(bus); 353 } 354 355 if (!(error = ppb_1284_negociate(bus, PPB_COMPATIBLE, 356 PPB_EXTENSIBILITY_LINK))) { 357 printf("/Extensibility Link"); 358 ppb_1284_terminate(bus); 359 } 360 } 361 362 printf("\n"); 363 364 /* detect PnP devices */ 365 ppb->class_id = ppb_pnp_detect(bus); 366 367 return (0); 368 369end_scan: 370 return (error); 371} 372 373#endif /* !DONTPROBE_1284 */ 374 375static int 376ppbus_attach(device_t dev) 377{ 378 379 /* Locate our children */ 380 bus_generic_probe(dev); 381 382#ifndef DONTPROBE_1284 383 /* detect IEEE1284 compliant devices */ 384 ppb_scan_bus(dev); 385#endif /* !DONTPROBE_1284 */ 386 387 /* launch attachement of the added children */ 388 bus_generic_attach(dev); 389 390 return (0); 391} 392 393static int 394ppbus_detach(device_t dev) 395{ 396 device_t *children; 397 int error, nchildren, i; 398 399 error = bus_generic_detach(dev); 400 if (error) 401 return (error); 402 403 /* detach & delete all children */ 404 if (!device_get_children(dev, &children, &nchildren)) { 405 for (i = 0; i < nchildren; i++) 406 if (children[i]) 407 device_delete_child(dev, children[i]); 408 free(children, M_TEMP); 409 } 410 411 return (0); 412} 413 414static int 415ppbus_setup_intr(device_t bus, device_t child, struct resource *r, int flags, 416 driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep) 417{ 418 int error; 419 struct ppb_data *ppb = DEVTOSOFTC(bus); 420 struct ppb_device *ppbdev = device_get_ivars(child); 421 422 /* a device driver must own the bus to register an interrupt */ 423 if (ppb->ppb_owner != child) 424 return (EINVAL); 425 426 if ((error = BUS_SETUP_INTR(device_get_parent(bus), child, r, flags, 427 filt, ihand, arg, cookiep))) 428 return (error); 429 430 /* store the resource and the cookie for eventually forcing 431 * handler unregistration 432 */ 433 ppbdev->intr_cookie = *cookiep; 434 ppbdev->intr_resource = r; 435 436 return (0); 437} 438 439static int 440ppbus_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih) 441{ 442 struct ppb_data *ppb = DEVTOSOFTC(bus); 443 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(child); 444 445 /* a device driver must own the bus to unregister an interrupt */ 446 if ((ppb->ppb_owner != child) || (ppbdev->intr_cookie != ih) || 447 (ppbdev->intr_resource != r)) 448 return (EINVAL); 449 450 ppbdev->intr_cookie = 0; 451 ppbdev->intr_resource = 0; 452 453 /* pass unregistration to the upper layer */ 454 return (BUS_TEARDOWN_INTR(device_get_parent(bus), child, r, ih)); 455} 456 457/* 458 * ppb_request_bus() 459 * 460 * Allocate the device to perform transfers. 461 * 462 * how : PPB_WAIT or PPB_DONTWAIT 463 */ 464int 465ppb_request_bus(device_t bus, device_t dev, int how) 466{ 467 int s, error = 0; 468 struct ppb_data *ppb = DEVTOSOFTC(bus); 469 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev); 470 471 while (!error) { 472 s = splhigh(); 473 if (ppb->ppb_owner) { 474 splx(s); 475 476 switch (how) { 477 case (PPB_WAIT | PPB_INTR): 478 error = tsleep(ppb, PPBPRI|PCATCH, "ppbreq", 0); 479 break; 480 481 case (PPB_WAIT | PPB_NOINTR): 482 error = tsleep(ppb, PPBPRI, "ppbreq", 0); 483 break; 484 485 default: 486 return (EWOULDBLOCK); 487 break; 488 } 489 490 } else { 491 ppb->ppb_owner = dev; 492 493 /* restore the context of the device 494 * The first time, ctx.valid is certainly false 495 * then do not change anything. This is usefull for 496 * drivers that do not set there operating mode 497 * during attachement 498 */ 499 if (ppbdev->ctx.valid) 500 ppb_set_mode(bus, ppbdev->ctx.mode); 501 502 splx(s); 503 return (0); 504 } 505 } 506 507 return (error); 508} 509 510/* 511 * ppb_release_bus() 512 * 513 * Release the device allocated with ppb_request_bus() 514 */ 515int 516ppb_release_bus(device_t bus, device_t dev) 517{ 518 int s, error; 519 struct ppb_data *ppb = DEVTOSOFTC(bus); 520 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev); 521 522 if (ppbdev->intr_resource != 0) 523 /* force interrupt handler unregistration when the ppbus is released */ 524 if ((error = BUS_TEARDOWN_INTR(bus, dev, ppbdev->intr_resource, 525 ppbdev->intr_cookie))) 526 return (error); 527 528 s = splhigh(); 529 if (ppb->ppb_owner != dev) { 530 splx(s); 531 return (EACCES); 532 } 533 534 ppb->ppb_owner = 0; 535 splx(s); 536 537 /* save the context of the device */ 538 ppbdev->ctx.mode = ppb_get_mode(bus); 539 540 /* ok, now the context of the device is valid */ 541 ppbdev->ctx.valid = 1; 542 543 /* wakeup waiting processes */ 544 wakeup(ppb); 545 546 return (0); 547} 548 549static devclass_t ppbus_devclass; 550 551static device_method_t ppbus_methods[] = { 552 /* device interface */ 553 DEVMETHOD(device_probe, ppbus_probe), 554 DEVMETHOD(device_attach, ppbus_attach), 555 DEVMETHOD(device_detach, ppbus_detach), 556 557 /* bus interface */ 558 DEVMETHOD(bus_add_child, ppbus_add_child), 559 DEVMETHOD(bus_print_child, ppbus_print_child), 560 DEVMETHOD(bus_read_ivar, ppbus_read_ivar), 561 DEVMETHOD(bus_write_ivar, ppbus_write_ivar), 562 DEVMETHOD(bus_setup_intr, ppbus_setup_intr), 563 DEVMETHOD(bus_teardown_intr, ppbus_teardown_intr), 564 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 565 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 566 567 { 0, 0 } 568}; 569 570static driver_t ppbus_driver = { 571 "ppbus", 572 ppbus_methods, 573 sizeof(struct ppb_data), 574}; 575DRIVER_MODULE(ppbus, ppc, ppbus_driver, ppbus_devclass, 0, 0); 576