1 /*- 2 * Copyright (c) 1997, 1998 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 * $Id: ppbconf.c,v 1.8 1998/09/20 14:41:54 nsouch Exp $ 27 * 28 */ 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/kernel.h> 32 #include <sys/malloc.h> 33 34 #include <vm/vm.h> 35 #include <vm/pmap.h> 36 37 #include <dev/ppbus/ppbconf.h> 38 #include <dev/ppbus/ppb_1284.h> 39 40 LIST_HEAD(, ppb_data) ppbdata; /* list of existing ppbus */ 41 42 /* 43 * Add a null driver so that the linker set always exists. 44 */ 45 46 static struct ppb_driver nulldriver = { 47 NULL, NULL, "null" 48 }; 49 DATA_SET(ppbdriver_set, nulldriver); 50 51 52 /* 53 * ppb_alloc_bus() 54 * 55 * Allocate area to store the ppbus description. 56 */ 57 struct ppb_data * 58 ppb_alloc_bus(void) 59 { 60 struct ppb_data *ppb; 61 static int ppbdata_initted = 0; /* done-init flag */ 62 63 ppb = (struct ppb_data *) malloc(sizeof(struct ppb_data), 64 M_TEMP, M_NOWAIT); 65 66 /* 67 * Add the new parallel port bus to the list of existing ppbus. 68 */ 69 if (ppb) { 70 bzero(ppb, sizeof(struct ppb_data)); 71 72 if (!ppbdata_initted) { /* list not initialised */ 73 LIST_INIT(&ppbdata); 74 ppbdata_initted = 1; 75 } 76 LIST_INSERT_HEAD(&ppbdata, ppb, ppb_chain); 77 } else { 78 printf("ppb_alloc_bus: cannot malloc!\n"); 79 } 80 return(ppb); 81 } 82 83 static char *pnp_tokens[] = { 84 "PRINTER", "MODEM", "NET", "HDC", "PCMCIA", "MEDIA", 85 "FDC", "PORTS", "SCANNER", "DIGICAM", "", NULL }; 86 87 static char *pnp_classes[] = { 88 "printer", "modem", "network device", 89 "hard disk", "PCMCIA", "multimedia device", 90 "floppy disk", "ports", "scanner", 91 "digital camera", "unknown device", NULL }; 92 93 /* 94 * search_token() 95 * 96 * Search the first occurence of a token within a string 97 * 98 * XXX should use strxxx() calls 99 */ 100 static char * 101 search_token(char *str, int slen, char *token) 102 { 103 char *p; 104 int tlen, i, j; 105 106 #define UNKNOWN_LENGTH -1 107 108 if (slen == UNKNOWN_LENGTH) 109 /* get string's length */ 110 for (slen = 0, p = str; *p != '\0'; p++) 111 slen ++; 112 113 /* get token's length */ 114 for (tlen = 0, p = token; *p != '\0'; p++) 115 tlen ++; 116 117 if (tlen == 0) 118 return (str); 119 120 for (i = 0; i <= slen-tlen; i++) { 121 for (j = 0; j < tlen; j++) 122 if (str[i+j] != token[j]) 123 break; 124 if (j == tlen) 125 return (&str[i]); 126 } 127 128 return (NULL); 129 } 130 131 /* 132 * ppb_pnp_detect() 133 * 134 * Returns the class id. of the peripherial, -1 otherwise 135 */ 136 static int 137 ppb_pnp_detect(struct ppb_data *ppb) 138 { 139 char *token, *q, *class = 0; 140 int i, len, error; 141 int class_id = -1; 142 char str[PPB_PnP_STRING_SIZE+1]; 143 struct ppb_device pnpdev; /* temporary device to perform I/O */ 144 145 /* initialize the pnpdev structure for future use */ 146 bzero(&pnpdev, sizeof(pnpdev)); 147 148 pnpdev.ppb = ppb; 149 150 if (bootverbose) 151 printf("ppb: <PnP> probing devices on ppbus %d...\n", 152 ppb->ppb_link->adapter_unit); 153 154 if (ppb_request_bus(&pnpdev, PPB_DONTWAIT)) { 155 if (bootverbose) 156 printf("ppb: <PnP> cannot allocate ppbus!\n"); 157 return (-1); 158 } 159 160 if ((error = ppb_1284_negociate(&pnpdev, NIBBLE_1284_REQUEST_ID))) { 161 if (bootverbose) 162 printf("ppb: <PnP> ppb_1284_negociate()=%d\n", error); 163 164 goto end_detect; 165 } 166 167 len = 0; 168 for (q=str; !(ppb_rstr(&pnpdev) & PERROR); q++) { 169 if ((error = nibble_1284_inbyte(&pnpdev, q))) { 170 if (bootverbose) { 171 *q = '\0'; 172 printf("ppb: <PnP> len=%d, %s\n", len, str); 173 printf("ppb: <PnP> nibble_1284_inbyte()=%d\n", 174 error); 175 } 176 goto end_detect; 177 } 178 179 if (len++ >= PPB_PnP_STRING_SIZE) { 180 printf("ppb: <PnP> not space left!\n"); 181 goto end_detect; 182 } 183 } 184 *q = '\0'; 185 186 nibble_1284_sync(&pnpdev); 187 188 if (bootverbose) { 189 printf("ppb: <PnP> %d characters: ", len); 190 for (i = 0; i < len; i++) 191 printf("0x%x ", str[i]); 192 printf("\n"); 193 } 194 195 /* replace ';' characters by '\0' */ 196 for (i = 0; i < len; i++) 197 str[i] = (str[i] == ';') ? '\0' : str[i]; 198 199 if ((token = search_token(str, len, "MFG")) != NULL) 200 printf("ppbus%d: <%s", ppb->ppb_link->adapter_unit, 201 search_token(token, UNKNOWN_LENGTH, ":") + 1); 202 else 203 printf("ppbus%d: <unknown", ppb->ppb_link->adapter_unit); 204 205 if ((token = search_token(str, len, "MDL")) != NULL) 206 printf(" %s", 207 search_token(token, UNKNOWN_LENGTH, ":") + 1); 208 else 209 printf(" unknown"); 210 211 if ((token = search_token(str, len, "VER")) != NULL) 212 printf("/%s", 213 search_token(token, UNKNOWN_LENGTH, ":") + 1); 214 215 if ((token = search_token(str, len, "REV")) != NULL) 216 printf(".%s", 217 search_token(token, UNKNOWN_LENGTH, ":") + 1); 218 219 printf(">"); 220 221 if ((token = search_token(str, len, "CLS")) != NULL) { 222 class = search_token(token, UNKNOWN_LENGTH, ":") + 1; 223 printf(" %s", class); 224 } 225 226 if ((token = search_token(str, len, "CMD")) != NULL) 227 printf(" %s", 228 search_token(token, UNKNOWN_LENGTH, ":") + 1); 229 230 printf("\n"); 231 232 if (class) 233 /* identify class ident */ 234 for (i = 0; pnp_tokens[i] != NULL; i++) { 235 if (search_token(class, len, pnp_tokens[i]) != NULL) { 236 class_id = i; 237 goto end_detect; 238 } 239 } 240 241 class_id = PPB_PnP_UNKNOWN; 242 243 end_detect: 244 if ((error = ppb_1284_terminate(&pnpdev, VALID_STATE)) && bootverbose) 245 printf("ppb: ppb_1284_terminate()=%d\n", error); 246 247 ppb_release_bus(&pnpdev); 248 return (class_id); 249 } 250 251 /* 252 * ppb_attachdevs() 253 * 254 * Called by ppcattach(), this function probes the ppbus and 255 * attaches found devices. 256 */ 257 int 258 ppb_attachdevs(struct ppb_data *ppb) 259 { 260 struct ppb_device *dev; 261 struct ppb_driver **p_drvpp, *p_drvp; 262 263 LIST_INIT(&ppb->ppb_devs); /* initialise device/driver list */ 264 p_drvpp = (struct ppb_driver **)ppbdriver_set.ls_items; 265 266 /* XXX wait for ieee1284 good support */ 267 #if 0 268 /* detect PnP devices */ 269 ppb->class_id = ppb_pnp_detect(ppb); 270 #endif 271 272 /* 273 * Blindly try all probes here. Later we should look at 274 * the parallel-port PnP standard, and intelligently seek 275 * drivers based on configuration first. 276 */ 277 while ((p_drvp = *p_drvpp++) != NULL) { 278 if (p_drvp->probe && (dev = (p_drvp->probe(ppb))) != NULL) { 279 /* 280 * Add the device to the list of probed devices. 281 */ 282 LIST_INSERT_HEAD(&ppb->ppb_devs, dev, chain); 283 284 /* Call the device's attach routine */ 285 (void)p_drvp->attach(dev); 286 } 287 } 288 return (0); 289 } 290 291 /* 292 * ppb_next_bus() 293 * 294 * Return the next bus in ppbus queue 295 */ 296 struct ppb_data * 297 ppb_next_bus(struct ppb_data *ppb) 298 { 299 300 if (ppb == NULL) 301 return (ppbdata.lh_first); 302 303 return (ppb->ppb_chain.le_next); 304 } 305 306 /* 307 * ppb_lookup_bus() 308 * 309 * Get ppb_data structure pointer according to the base address of the ppbus 310 */ 311 struct ppb_data * 312 ppb_lookup_bus(int base_port) 313 { 314 struct ppb_data *ppb; 315 316 for (ppb = ppbdata.lh_first; ppb; ppb = ppb->ppb_chain.le_next) 317 if (ppb->ppb_link->base == base_port) 318 break; 319 320 return (ppb); 321 } 322 323 /* 324 * ppb_lookup_link() 325 * 326 * Get ppb_data structure pointer according to the unit value 327 * of the corresponding link structure 328 */ 329 struct ppb_data * 330 ppb_lookup_link(int unit) 331 { 332 struct ppb_data *ppb; 333 334 for (ppb = ppbdata.lh_first; ppb; ppb = ppb->ppb_chain.le_next) 335 if (ppb->ppb_link->adapter_unit == unit) 336 break; 337 338 return (ppb); 339 } 340 341 /* 342 * ppb_attach_device() 343 * 344 * Called by loadable kernel modules to add a device 345 */ 346 int 347 ppb_attach_device(struct ppb_device *dev) 348 { 349 struct ppb_data *ppb = dev->ppb; 350 351 /* add the device to the list of probed devices */ 352 LIST_INSERT_HEAD(&ppb->ppb_devs, dev, chain); 353 354 return (0); 355 } 356 357 /* 358 * ppb_remove_device() 359 * 360 * Called by loadable kernel modules to remove a device 361 */ 362 void 363 ppb_remove_device(struct ppb_device *dev) 364 { 365 366 /* remove the device from the list of probed devices */ 367 LIST_REMOVE(dev, chain); 368 369 return; 370 } 371 372 /* 373 * ppb_request_bus() 374 * 375 * Allocate the device to perform transfers. 376 * 377 * how : PPB_WAIT or PPB_DONTWAIT 378 */ 379 int 380 ppb_request_bus(struct ppb_device *dev, int how) 381 { 382 int s, error = 0; 383 struct ppb_data *ppb = dev->ppb; 384 385 while (!error) { 386 s = splhigh(); 387 if (ppb->ppb_owner) { 388 splx(s); 389 390 switch (how) { 391 case (PPB_WAIT | PPB_INTR): 392 error = tsleep(ppb, PPBPRI|PCATCH, "ppbreq", 0); 393 break; 394 395 case (PPB_WAIT | PPB_NOINTR): 396 error = tsleep(ppb, PPBPRI, "ppbreq", 0); 397 break; 398 399 default: 400 return (EWOULDBLOCK); 401 break; 402 } 403 404 } else { 405 ppb->ppb_owner = dev; 406 407 /* restore the context of the device 408 * The first time, ctx.valid is certainly false 409 * then do not change anything. This is usefull for 410 * drivers that do not set there operating mode 411 * during attachement 412 */ 413 if (dev->ctx.valid) 414 ppb_set_mode(dev, dev->ctx.mode); 415 416 splx(s); 417 return (0); 418 } 419 } 420 421 return (error); 422 } 423 424 /* 425 * ppb_release_bus() 426 * 427 * Release the device allocated with ppb_request_dev() 428 */ 429 int 430 ppb_release_bus(struct ppb_device *dev) 431 { 432 int s; 433 struct ppb_data *ppb = dev->ppb; 434 435 s = splhigh(); 436 if (ppb->ppb_owner != dev) { 437 splx(s); 438 return (EACCES); 439 } 440 441 ppb->ppb_owner = 0; 442 splx(s); 443 444 /* save the context of the device */ 445 dev->ctx.mode = ppb_get_mode(dev); 446 447 /* ok, now the context of the device is valid */ 448 dev->ctx.valid = 1; 449 450 /* wakeup waiting processes */ 451 wakeup(ppb); 452 453 return (0); 454 } 455