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 $
27 * $DragonFly: src/contrib/pnpinfo/pnpinfo.c,v 1.3 2003/08/08 04:18:31 dillon Exp $
38 #include <machine/cpufunc.h>
40 #include <bus/isa/pnpreg.h>
49 static void pnp_write(int, u_char
);
50 static u_char
pnp_read(int);
51 static void DELAY(int i
);
52 static void send_Initiation_LFSR(void);
53 static int get_serial(u_char
*);
54 static int get_resource_info(u_char
*, int);
55 static void report_dma_info(int);
56 static void report_memory_info(int);
57 static int handle_small_res(u_char
*, int, int);
58 static void handle_large_res(u_char
*, int, int);
59 static void dump_resdata(u_char
*, int);
60 static int isolation_protocol(void);
62 /* The READ_DATA port that we are using currently */
68 pnp_write(int d
, u_char r
)
70 outb (_PNP_ADDRESS
, d
);
71 outb (_PNP_WRITE_DATA
, r
);
77 outb(_PNP_ADDRESS
, d
);
78 return inb( (rd_port
<< 2) + 3) & 0xff;
82 * DELAY does accurate delaying in user-space.
83 * This function busy-waits.
93 gettimeofday (&t
, NULL
);
94 start
= t
.tv_sec
* 1000000 + t
.tv_usec
;
96 gettimeofday (&t
, NULL
);
97 stop
= t
.tv_sec
* 1000000 + t
.tv_usec
;
98 } while (start
+ i
> stop
);
103 * Send Initiation LFSR as described in "Plug and Play ISA Specification,
107 send_Initiation_LFSR(void)
111 pnp_write(PNP_CONFIG_CONTROL
, 0x2);
114 outb(_PNP_ADDRESS
, 0);
115 outb(_PNP_ADDRESS
, 0); /* yes, we do need it twice! */
119 for (i
= 0; i
< 32; i
++) {
120 outb(_PNP_ADDRESS
, cur
);
121 cur
= (cur
>> 1) | (((cur
^ (cur
>> 1)) << 7) & 0xff);
126 * Get the device's serial number. Returns 1 if the serial is valid.
129 get_serial(u_char
*data
)
131 int i
, bit
, valid
= 0, sum
= 0x6a;
133 bzero(data
, sizeof(char) * 9);
135 for (i
= 0; i
< 72; i
++) {
136 bit
= inb((rd_port
<< 2) | 0x3) == 0x55;
137 DELAY(250); /* Delay 250 usec */
139 /* Can't Short Circuit the next evaluation, so 'and' is last */
140 bit
= (inb((rd_port
<< 2) | 0x3) == 0xaa) && bit
;
141 DELAY(250); /* Delay 250 usec */
143 valid
= valid
|| bit
;
147 (((sum
^ (sum
>> 1) ^ bit
) << 7) & 0xff);
149 data
[i
/ 8] = (data
[i
/ 8] >> 1) | (bit
? 0x80 : 0);
152 valid
= valid
&& (data
[8] == sum
);
159 * Fill's the buffer with resource info from the device.
160 * Returns 0 if the device fails to report
163 get_resource_info(u_char
*buffer
, int len
)
167 for (i
= 0; i
< len
; i
++) {
168 outb(_PNP_ADDRESS
, PNP_STATUS
);
169 for (j
= 0; j
< 100; j
++) {
170 if ((inb((rd_port
<< 2) | 0x3)) & 0x1)
175 printf("PnP device failed to report resource data\n");
178 outb(_PNP_ADDRESS
, PNP_RESOURCE_DATA
);
179 buffer
[i
] = inb((rd_port
<< 2) | 0x3);
180 DEB(printf("--- get_resource_info: got 0x%02x\n",(unsigned)buffer
[i
]));
186 report_dma_info(int x
)
188 const char *s1
=NULL
, *s2
=NULL
, *s3
=NULL
, *s4
=NULL
, *s5
=NULL
;
207 s2
= (x
& 0x4) ? "bus master" : "not a bus master";
209 s3
= (x
& 0x8) ? "count by byte" : "";
211 s4
= (x
& 0x10) ? "count by word" : "";
213 switch ((x
& 0x60) >> 5) {
215 s5
="Compatibility mode";
227 printf("\t%s, %s, %s, %s, %s\n",s1
,s2
,s3
,s4
,s5
);
232 report_memory_info(int x
)
235 printf ("Memory Range: Writeable\n");
237 printf ("Memory Range: Not writeable (ROM)\n");
240 printf ("Memory Range: Read-cacheable, write-through\n");
242 printf ("Memory Range: Non-cacheable\n");
245 printf ("Memory Range: Decode supports high address\n");
247 printf ("Memory Range: Decode supports range length\n");
249 switch ((x
& 0x18) >> 3) {
251 printf ("Memory Range: 8-bit memory only\n");
254 printf ("Memory Range: 16-bit memory only\n");
257 printf ("Memory Range: 8-bit and 16-bit memory supported\n");
261 printf ("Memory Range: Reserved\n");
267 printf ("Memory Range: Memory is shadowable\n");
269 printf ("Memory Range: Memory is not shadowable\n");
272 printf ("Memory Range: Memory is an expansion ROM\n");
274 printf ("Memory Range: Memory is not an expansion ROM\n");
278 printf ("Memory Range: Reserved (Device is brain-damaged)\n");
284 * Small Resource Tag Handler
286 * Returns 1 if checksum was valid (and an END_TAG was received).
287 * Returns -1 if checksum was invalid (and an END_TAG was received).
288 * Returns 0 for other tags.
291 handle_small_res(u_char
*resinfo
, int item
, int len
)
295 DEB(printf("*** ITEM 0x%04x len %d detected\n", item
, len
));
299 printf("*** ITEM 0x%02x detected\n", item
);
301 case PNP_TAG_VERSION
:
302 printf("PnP Version %d.%d, Vendor Version %d\n",
303 resinfo
[0] >> 4, resinfo
[0] & (0xf), resinfo
[1]);
305 case PNP_TAG_LOGICAL_DEVICE
:
306 printf("\nLogical Device ID: %c%c%c%02x%02x 0x%08x #%d\n",
307 ((resinfo
[0] & 0x7c) >> 2) + 64,
308 (((resinfo
[0] & 0x03) << 3) |
309 ((resinfo
[1] & 0xe0) >> 5)) + 64,
310 (resinfo
[1] & 0x1f) + 64,
311 resinfo
[2], resinfo
[3], *(int *)(resinfo
),
314 if (resinfo
[4] & 0x1)
315 printf ("\tDevice powers up active\n"); /* XXX */
316 if (resinfo
[4] & 0x2)
317 printf ("\tDevice supports I/O Range Check\n");
318 if (resinfo
[4] > 0x3)
319 printf ("\tReserved register funcs %02x\n",
323 printf("\tVendor register funcs %02x\n", resinfo
[5]);
325 case PNP_TAG_COMPAT_DEVICE
:
326 printf("Compatible Device ID: %c%c%c%02x%02x (%08x)\n",
327 ((resinfo
[0] & 0x7c) >> 2) + 64,
328 (((resinfo
[0] & 0x03) << 3) |
329 ((resinfo
[1] & 0xe0) >> 5)) + 64,
330 (resinfo
[1] & 0x1f) + 64,
331 resinfo
[2], resinfo
[3], *(int *)resinfo
);
333 case PNP_TAG_IRQ_FORMAT
:
336 for (i
= 0; i
< 8; i
++)
337 if (resinfo
[0] & (1<<i
))
339 for (i
= 0; i
< 8; i
++)
340 if (resinfo
[1] & (1<<i
))
341 printf("%d ", i
+ 8);
343 if (resinfo
[2] & 0x1)
344 printf("IRQ: High true edge sensitive\n");
345 if (resinfo
[2] & 0x2)
346 printf("IRQ: Low true edge sensitive\n");
347 if (resinfo
[2] & 0x4)
348 printf("IRQ: High true level sensitive\n");
349 if (resinfo
[2] & 0x8)
350 printf("IRQ: Low true level sensitive\n");
352 printf(" - only one type (true/edge)\n");
355 case PNP_TAG_DMA_FORMAT
:
356 printf(" DMA: channel(s) ");
357 for (i
= 0; i
< 8; i
++)
358 if (resinfo
[0] & (1<<i
))
361 report_dma_info (resinfo
[1]);
363 case PNP_TAG_START_DEPENDANT
:
364 printf("TAG Start DF\n");
366 switch (resinfo
[0]) {
368 printf("Good Configuration\n");
371 printf("Acceptable Configuration\n");
374 printf("Sub-optimal Configuration\n");
379 case PNP_TAG_END_DEPENDANT
:
380 printf("TAG End DF\n");
382 case PNP_TAG_IO_RANGE
:
383 printf(" I/O Range 0x%x .. 0x%x, alignment 0x%x, len 0x%x\n",
384 resinfo
[1] + (resinfo
[2] << 8),
385 resinfo
[3] + (resinfo
[4] << 8),
386 resinfo
[5], resinfo
[6] );
388 printf("\t[16-bit addr]\n");
390 printf("\t[not 16-bit addr]\n");
392 case PNP_TAG_IO_FIXED
:
393 printf (" FIXED I/O base address 0x%x length 0x%x\n",
394 resinfo
[0] + ( (resinfo
[1] & 3 ) << 8), /* XXX */
398 case PNP_TAG_RESERVED
:
399 printf("Reserved Tag Detected\n");
403 printf("*** Small Vendor Tag Detected\n");
406 printf("End Tag\n\n");
407 /* XXX Record and Verify Checksum */
416 handle_large_res(u_char
*resinfo
, int item
, int len
)
420 DEB(printf("*** Large ITEM %d len %d found\n", item
, len
));
422 case PNP_TAG_MEMORY_RANGE
:
423 report_memory_info(resinfo
[0]);
424 printf("Memory range minimum address: 0x%x\n",
425 (resinfo
[1] << 8) + (resinfo
[2] << 16));
426 printf("Memory range maximum address: 0x%x\n",
427 (resinfo
[3] << 8) + (resinfo
[4] << 16));
428 printf("Memory range base alignment: 0x%x\n",
429 (i
= (resinfo
[5] + (resinfo
[6] << 8))) ? i
: (1 << 16));
430 printf("Memory range length: 0x%x\n",
431 (resinfo
[7] + (resinfo
[8] << 8)) * 256);
433 case PNP_TAG_ID_ANSI
:
434 printf("Device Description: ");
436 for (i
= 0; i
< len
; i
++) {
437 if (resinfo
[i
]) /* XXX */
438 printf("%c", resinfo
[i
]);
442 case PNP_TAG_ID_UNICODE
:
443 printf("ID String Unicode Detected (Undefined)\n");
445 case PNP_TAG_LARGE_VENDOR
:
446 printf("Large Vendor Defined Detected\n");
448 case PNP_TAG_MEMORY32_RANGE
:
449 printf("32bit Memory Range Desc Unimplemented\n");
451 case PNP_TAG_MEMORY32_FIXED
:
452 printf("32bit Fixed Location Desc Unimplemented\n");
455 case PNP_TAG_LARGE_RESERVED
:
456 printf("Large Reserved Tag Detected\n");
464 * Dump all the information about configurations.
467 dump_resdata(u_char
*data
, int csn
)
471 u_char tag
, *resinfo
;
473 DDB(printf("\nCard assigned CSN #%d\n", csn
));
474 printf("Vendor ID %c%c%c%02x%02x (0x%08x), Serial Number 0x%08x\n",
475 ((data
[0] & 0x7c) >> 2) + 64,
476 (((data
[0] & 0x03) << 3) | ((data
[1] & 0xe0) >> 5)) + 64,
477 (data
[1] & 0x1f) + 64, data
[2], data
[3],
481 pnp_write(PNP_SET_CSN
, csn
); /* Move this out of this function XXX */
482 outb(_PNP_ADDRESS
, PNP_STATUS
);
484 /* Allows up to 1kb of Resource Info, Should be plenty */
485 for (i
= 0; i
< 1024; i
++) {
486 if (!get_resource_info(&tag
, 1))
489 if (PNP_RES_TYPE(tag
) == 0) {
490 /* Handle small resouce data types */
492 resinfo
= malloc(PNP_SRES_LEN(tag
));
493 if (!get_resource_info(resinfo
, PNP_SRES_LEN(tag
)))
496 if (handle_small_res(resinfo
, PNP_SRES_NUM(tag
), PNP_SRES_LEN(tag
)) == 1)
500 /* Handle large resouce data types */
502 if (!get_resource_info((char *)buf
, 2))
504 large_len
= (buf
[1] << 8) + buf
[0];
506 resinfo
= malloc(large_len
);
507 if (!get_resource_info(resinfo
, large_len
))
510 handle_large_res(resinfo
, PNP_LRES_NUM(tag
), large_len
);
514 printf("Successfully got %d resources, %d logical fdevs\n", i
,
516 printf("-- card select # 0x%04x\n", pnp_read(PNP_SET_CSN
));
517 printf("\nCSN %c%c%c%02x%02x (0x%08x), Serial Number 0x%08x\n",
518 ((data
[0] & 0x7c) >> 2) + 64,
519 (((data
[0] & 0x03) << 3) | ((data
[1] & 0xe0) >> 5)) + 64,
520 (data
[1] & 0x1f) + 64, data
[2], data
[3],
524 for (i
=0; i
<logdevs
; i
++) {
527 pnp_write(PNP_SET_LDN
, i
);
529 printf("\nLogical device #%d\n", pnp_read(PNP_SET_LDN
) );
532 printf(" 0x%02x%02x", pnp_read(PNP_IO_BASE_HIGH(i
)),
533 pnp_read(PNP_IO_BASE_LOW(i
)));
534 printf("\nIRQ %d %d\n",
535 pnp_read(PNP_IRQ_LEVEL(0)), pnp_read(PNP_IRQ_LEVEL(1)) );
536 printf("DMA %d %d\n",
537 pnp_read(PNP_DMA_CHANNEL(0)), pnp_read(PNP_DMA_CHANNEL(1)) );
538 printf("IO range check 0x%02x activate 0x%02x\n",
539 pnp_read(PNP_IO_RANGE_CHECK
), pnp_read(PNP_ACTIVATE
) );
545 * Run the isolation protocol. Use rd_port as the READ_DATA port
546 * value (caller should try multiple READ_DATA locations before giving
547 * up). Upon exiting, all cards are aware that they should use rd_port
548 * as the READ_DATA port;
552 isolation_protocol(void)
557 send_Initiation_LFSR();
559 /* Reset CSN for All Cards */
560 pnp_write(PNP_CONFIG_CONTROL
, 0x04);
562 for (csn
= 1; (csn
< PNP_MAX_CARDS
); csn
++) {
563 /* Wake up cards without a CSN */
565 pnp_write(PNP_WAKE
, 0);
566 pnp_write(PNP_SET_RD_DATA
, rd_port
);
567 outb(_PNP_ADDRESS
, PNP_SERIAL_ISOLATION
);
568 DELAY(1000); /* Delay 1 msec */
570 if (get_serial(data
))
571 dump_resdata(data
, csn
);
585 /* Hey what about a i386_iopl() call :) */
586 if (open("/dev/io", O_RDONLY
) < 0) {
587 fprintf (stderr
, "pnpinfo: Can't get I/O privilege.\n");
592 printf("Checking for Plug-n-Play devices...\n");
594 /* Try various READ_DATA ports from 0x203-0x3ff */
595 for (rd_port
= 0x80; (rd_port
< 0xff); rd_port
+= 0x10) {
596 DEB(printf("Trying Read_Port at %x...\n", (rd_port
<< 2) | 0x3) );
597 num_pnp_devs
= isolation_protocol();
602 printf("No Plug-n-Play devices were found\n");