2 * Copyright (c) 1996, Sujal M. Patel
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
26 * $FreeBSD: src/contrib/pnpinfo/pnpinfo.c,v 1.7 1999/09/05 17:27:05 peter Exp $
37 #include <machine/cpufunc.h>
39 #include <isa/pnpreg.h>
49 pnp_write(int d, u_char r)
51 outb (_PNP_ADDRESS, d);
52 outb (_PNP_WRITE_DATA, r);
55 /* The READ_DATA port that we are using currently */
61 outb(_PNP_ADDRESS, d);
62 return inb( (rd_port << 2) + 3) & 0xff;
68 int c = pnp_read(d) << 8 ;
75 void DELAY __P((int i));
76 void send_Initiation_LFSR();
77 int get_serial __P((u_char *data));
78 int get_resource_info __P((u_char *buffer, int len));
79 int handle_small_res __P((u_char *resinfo, int item, int len));
80 void handle_large_res __P((u_char *resinfo, int item, int len));
81 void dump_resdata __P((u_char *data, int csn));
82 int isolation_protocol();
86 * DELAY does accurate delaying in user-space.
87 * This function busy-waits.
97 gettimeofday (&t, NULL);
98 start = t.tv_sec * 1000000 + t.tv_usec;
100 gettimeofday (&t, NULL);
101 stop = t.tv_sec * 1000000 + t.tv_usec;
102 } while (start + i > stop);
107 * Send Initiation LFSR as described in "Plug and Play ISA Specification,
111 send_Initiation_LFSR()
115 pnp_write(PNP_CONFIG_CONTROL, 0x2);
118 outb(_PNP_ADDRESS, 0);
119 outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
123 for (i = 0; i < 32; i++) {
124 outb(_PNP_ADDRESS, cur);
125 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
130 * Get the device's serial number. Returns 1 if the serial is valid.
133 get_serial(u_char *data)
135 int i, bit, valid = 0, sum = 0x6a;
137 bzero(data, sizeof(char) * 9);
139 for (i = 0; i < 72; i++) {
140 bit = inb((rd_port << 2) | 0x3) == 0x55;
141 DELAY(250); /* Delay 250 usec */
143 /* Can't Short Circuit the next evaluation, so 'and' is last */
144 bit = (inb((rd_port << 2) | 0x3) == 0xaa) && bit;
145 DELAY(250); /* Delay 250 usec */
147 valid = valid || bit;
151 (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
153 data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
156 valid = valid && (data[8] == sum);
163 * Fill's the buffer with resource info from the device.
164 * Returns 0 if the device fails to report
167 get_resource_info(u_char *buffer, int len)
171 for (i = 0; i < len; i++) {
172 outb(_PNP_ADDRESS, PNP_STATUS);
173 for (j = 0; j < 100; j++) {
174 if ((inb((rd_port << 2) | 0x3)) & 0x1)
179 printf("PnP device failed to report resource data\n");
182 outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
183 buffer[i] = inb((rd_port << 2) | 0x3);
184 DEB(printf("--- get_resource_info: got 0x%02x\n",(unsigned)buffer[i]));
193 char *s1=NULL, *s2=NULL, *s3=NULL, *s4=NULL, *s5=NULL;
212 s2 = (x & 0x4) ? "bus master" : "not a bus master";
214 s3 = (x & 0x8) ? "count by byte" : "";
216 s4 = (x & 0x10) ? "count by word" : "";
218 switch ((x & 0x60) >> 5) {
220 s5="Compatibility mode";
232 printf("\t%s, %s, %s, %s, %s\n",s1,s2,s3,s4,s5);
237 report_memory_info (int x)
240 printf ("Memory Range: Writeable\n");
242 printf ("Memory Range: Not writeable (ROM)\n");
245 printf ("Memory Range: Read-cacheable, write-through\n");
247 printf ("Memory Range: Non-cacheable\n");
250 printf ("Memory Range: Decode supports high address\n");
252 printf ("Memory Range: Decode supports range length\n");
254 switch ((x & 0x18) >> 3) {
256 printf ("Memory Range: 8-bit memory only\n");
259 printf ("Memory Range: 16-bit memory only\n");
262 printf ("Memory Range: 8-bit and 16-bit memory supported\n");
266 printf ("Memory Range: Reserved\n");
272 printf ("Memory Range: Memory is shadowable\n");
274 printf ("Memory Range: Memory is not shadowable\n");
277 printf ("Memory Range: Memory is an expansion ROM\n");
279 printf ("Memory Range: Memory is not an expansion ROM\n");
283 printf ("Memory Range: Reserved (Device is brain-damaged)\n");
289 * Small Resource Tag Handler
291 * Returns 1 if checksum was valid (and an END_TAG was received).
292 * Returns -1 if checksum was invalid (and an END_TAG was received).
293 * Returns 0 for other tags.
296 handle_small_res(u_char *resinfo, int item, int len)
300 DEB(printf("*** ITEM 0x%04x len %d detected\n", item, len));
304 printf("*** ITEM 0x%02x detected\n", item);
306 case PNP_TAG_VERSION:
307 printf("PnP Version %d.%d, Vendor Version %d\n",
308 resinfo[0] >> 4, resinfo[0] & (0xf), resinfo[1]);
310 case PNP_TAG_LOGICAL_DEVICE:
311 printf("\nLogical Device ID: %c%c%c%02x%02x 0x%08x #%d\n",
312 ((resinfo[0] & 0x7c) >> 2) + 64,
313 (((resinfo[0] & 0x03) << 3) |
314 ((resinfo[1] & 0xe0) >> 5)) + 64,
315 (resinfo[1] & 0x1f) + 64,
316 resinfo[2], resinfo[3], *(int *)(resinfo),
319 if (resinfo[4] & 0x1)
320 printf ("\tDevice powers up active\n"); /* XXX */
321 if (resinfo[4] & 0x2)
322 printf ("\tDevice supports I/O Range Check\n");
323 if (resinfo[4] > 0x3)
324 printf ("\tReserved register funcs %02x\n",
328 printf("\tVendor register funcs %02x\n", resinfo[5]);
330 case PNP_TAG_COMPAT_DEVICE:
331 printf("Compatible Device ID: %c%c%c%02x%02x (%08x)\n",
332 ((resinfo[0] & 0x7c) >> 2) + 64,
333 (((resinfo[0] & 0x03) << 3) |
334 ((resinfo[1] & 0xe0) >> 5)) + 64,
335 (resinfo[1] & 0x1f) + 64,
336 resinfo[2], resinfo[3], *(int *)resinfo);
338 case PNP_TAG_IRQ_FORMAT:
341 for (i = 0; i < 8; i++)
342 if (resinfo[0] & (1<<i))
344 for (i = 0; i < 8; i++)
345 if (resinfo[1] & (1<<i))
346 printf("%d ", i + 8);
348 if (resinfo[2] & 0x1)
349 printf("IRQ: High true edge sensitive\n");
350 if (resinfo[2] & 0x2)
351 printf("IRQ: Low true edge sensitive\n");
352 if (resinfo[2] & 0x4)
353 printf("IRQ: High true level sensitive\n");
354 if (resinfo[2] & 0x8)
355 printf("IRQ: Low true level sensitive\n");
357 printf(" - only one type (true/edge)\n");
360 case PNP_TAG_DMA_FORMAT:
361 printf(" DMA: channel(s) ");
362 for (i = 0; i < 8; i++)
363 if (resinfo[0] & (1<<i))
366 report_dma_info (resinfo[1]);
368 case PNP_TAG_START_DEPENDANT:
369 printf("TAG Start DF\n");
371 switch (resinfo[0]) {
373 printf("Good Configuration\n");
376 printf("Acceptable Configuration\n");
379 printf("Sub-optimal Configuration\n");
384 case PNP_TAG_END_DEPENDANT:
385 printf("TAG End DF\n");
387 case PNP_TAG_IO_RANGE:
388 printf(" I/O Range 0x%x .. 0x%x, alignment 0x%x, len 0x%x\n",
389 resinfo[1] + (resinfo[2] << 8),
390 resinfo[3] + (resinfo[4] << 8),
391 resinfo[5], resinfo[6] );
393 printf("\t[16-bit addr]\n");
395 printf("\t[not 16-bit addr]\n");
397 case PNP_TAG_IO_FIXED:
398 printf (" FIXED I/O base address 0x%x length 0x%x\n",
399 resinfo[0] + ( (resinfo[1] & 3 ) << 8), /* XXX */
403 case PNP_TAG_RESERVED:
404 printf("Reserved Tag Detected\n");
408 printf("*** Small Vendor Tag Detected\n");
411 printf("End Tag\n\n");
412 /* XXX Record and Verify Checksum */
421 handle_large_res(u_char *resinfo, int item, int len)
425 DEB(printf("*** Large ITEM %d len %d found\n", item, len));
427 case PNP_TAG_MEMORY_RANGE:
428 report_memory_info(resinfo[0]);
429 printf("Memory range minimum address: 0x%x\n",
430 (resinfo[1] << 8) + (resinfo[2] << 16));
431 printf("Memory range maximum address: 0x%x\n",
432 (resinfo[3] << 8) + (resinfo[4] << 16));
433 printf("Memory range base alignment: 0x%x\n",
434 (i = (resinfo[5] + (resinfo[6] << 8))) ? i : (1 << 16));
435 printf("Memory range length: 0x%x\n",
436 (resinfo[7] + (resinfo[8] << 8)) * 256);
438 case PNP_TAG_ID_ANSI:
439 printf("Device Description: ");
441 for (i = 0; i < len; i++) {
442 if (resinfo[i]) /* XXX */
443 printf("%c", resinfo[i]);
447 case PNP_TAG_ID_UNICODE:
448 printf("ID String Unicode Detected (Undefined)\n");
450 case PNP_TAG_LARGE_VENDOR:
451 printf("Large Vendor Defined Detected\n");
453 case PNP_TAG_MEMORY32_RANGE:
454 printf("32bit Memory Range Desc Unimplemented\n");
456 case PNP_TAG_MEMORY32_FIXED:
457 printf("32bit Fixed Location Desc Unimplemented\n");
460 case PNP_TAG_LARGE_RESERVED:
461 printf("Large Reserved Tag Detected\n");
469 * Dump all the information about configurations.
472 dump_resdata(u_char *data, int csn)
476 u_char tag, *resinfo;
478 DDB(printf("\nCard assigned CSN #%d\n", csn));
479 printf("Vendor ID %c%c%c%02x%02x (0x%08x), Serial Number 0x%08x\n",
480 ((data[0] & 0x7c) >> 2) + 64,
481 (((data[0] & 0x03) << 3) | ((data[1] & 0xe0) >> 5)) + 64,
482 (data[1] & 0x1f) + 64, data[2], data[3],
486 pnp_write(PNP_SET_CSN, csn); /* Move this out of this function XXX */
487 outb(_PNP_ADDRESS, PNP_STATUS);
489 /* Allows up to 1kb of Resource Info, Should be plenty */
490 for (i = 0; i < 1024; i++) {
491 if (!get_resource_info(&tag, 1))
494 if (PNP_RES_TYPE(tag) == 0) {
495 /* Handle small resouce data types */
497 resinfo = malloc(PNP_SRES_LEN(tag));
498 if (!get_resource_info(resinfo, PNP_SRES_LEN(tag)))
501 if (handle_small_res(resinfo, PNP_SRES_NUM(tag), PNP_SRES_LEN(tag)) == 1)
505 /* Handle large resouce data types */
507 if (!get_resource_info((char *)buf, 2))
509 large_len = (buf[1] << 8) + buf[0];
511 resinfo = malloc(large_len);
512 if (!get_resource_info(resinfo, large_len))
515 handle_large_res(resinfo, PNP_LRES_NUM(tag), large_len);
519 printf("Successfully got %d resources, %d logical fdevs\n", i,
521 printf("-- card select # 0x%04x\n", pnp_read(PNP_SET_CSN));
522 printf("\nCSN %c%c%c%02x%02x (0x%08x), Serial Number 0x%08x\n",
523 ((data[0] & 0x7c) >> 2) + 64,
524 (((data[0] & 0x03) << 3) | ((data[1] & 0xe0) >> 5)) + 64,
525 (data[1] & 0x1f) + 64, data[2], data[3],
529 for (i=0; i<logdevs; i++) {
532 pnp_write(PNP_SET_LDN, i);
534 printf("\nLogical device #%d\n", pnp_read(PNP_SET_LDN) );
537 printf(" 0x%02x%02x", pnp_read(PNP_IO_BASE_HIGH(i)),
538 pnp_read(PNP_IO_BASE_LOW(i)));
539 printf("\nIRQ %d %d\n",
540 pnp_read(PNP_IRQ_LEVEL(0)), pnp_read(PNP_IRQ_LEVEL(1)) );
541 printf("DMA %d %d\n",
542 pnp_read(PNP_DMA_CHANNEL(0)), pnp_read(PNP_DMA_CHANNEL(1)) );
543 printf("IO range check 0x%02x activate 0x%02x\n",
544 pnp_read(PNP_IO_RANGE_CHECK), pnp_read(PNP_ACTIVATE) );
550 * Run the isolation protocol. Use rd_port as the READ_DATA port
551 * value (caller should try multiple READ_DATA locations before giving
552 * up). Upon exiting, all cards are aware that they should use rd_port
553 * as the READ_DATA port;
562 send_Initiation_LFSR();
564 /* Reset CSN for All Cards */
565 pnp_write(PNP_CONFIG_CONTROL, 0x04);
567 for (csn = 1; (csn < PNP_MAX_CARDS); csn++) {
568 /* Wake up cards without a CSN */
570 pnp_write(PNP_WAKE, 0);
571 pnp_write(PNP_SET_RD_DATA, rd_port);
572 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
573 DELAY(1000); /* Delay 1 msec */
575 if (get_serial(data))
576 dump_resdata(data, csn);
585 main(int argc, char **argv)
590 /* Hey what about a i386_iopl() call :) */
591 if (open("/dev/io", O_RDONLY) < 0) {
592 fprintf (stderr, "pnpinfo: Can't get I/O privilege.\n");
597 ioperm(0x203, 0x400 - 0x203, 1);
600 printf("Checking for Plug-n-Play devices...\n");
602 /* Try various READ_DATA ports from 0x203-0x3ff */
603 for (rd_port = 0x80; (rd_port < 0xff); rd_port += 0x10) {
604 DEB(printf("Trying Read_Port at %x...\n", (rd_port << 2) | 0x3) );
605 num_pnp_devs = isolation_protocol(rd_port);
610 printf("No Plug-n-Play devices were found\n");