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