2 * Copyright 2006-2012, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
15 #include <drivers/device_manager.h>
16 #include <drivers/module.h>
17 #include <drivers/PCI.h>
18 #include <drivers/bus/PCI.h>
19 #include <drivers/bus/SCSI.h>
21 #include "dm_wrapper.h"
23 #include "pci-utils.h"
26 extern const char *__progname
;
30 int gMode
= USER_MODE
;
38 get_scsi_device_type(uint8 type
)
41 case 0x0: return "Direct Access";
42 case 0x1: return "Sequential Access";
43 case 0x2: return "Printer";
44 case 0x3: return "Processor";
45 case 0x4: return "WORM";
46 case 0x5: return "CDROM";
47 case 0x6: return "Scanner";
48 case 0x7: return "Optical memory";
49 case 0x8: return "Medium changer";
50 case 0x9: return "Communication";
51 case 0xc: return "Storage array controller";
52 case 0xd: return "Enclosure services";
53 case 0xe: return "Simplified Direct Access";
62 fprintf(stderr
, "usage: %s [-d]\n", __progname
);
63 fprintf(stderr
, "Displays devices in a user friendly way\n");
64 fprintf(stderr
, "-d : dumps the tree\n");
70 put_level(int32 level
)
78 dump_attribute(struct device_attr_info
*attr
, int32 level
)
84 printf("\"%s\" : ", attr
->name
);
87 printf("string : \"%s\"", attr
->value
.string
);
90 printf("uint8 : %" B_PRIu8
" (%#" B_PRIx8
")", attr
->value
.ui8
,
94 printf("uint16 : %" B_PRIu16
" (%#" B_PRIx16
")", attr
->value
.ui16
,
98 printf("uint32 : %" B_PRIu32
" (%#" B_PRIx32
")", attr
->value
.ui32
,
102 printf("uint64 : %" B_PRIu64
" (%#" B_PRIx64
")", attr
->value
.ui64
,
113 dump_device(device_node_cookie
*node
, uint8 level
)
116 struct device_attr_info attr
;
118 attr
.node_cookie
= *node
;
119 attr
.value
.raw
.data
= data
;
120 attr
.value
.raw
.length
= sizeof(data
);
123 printf("(%d)\n", level
);
124 while (dm_get_next_attr(&attr
) == B_OK
) {
125 dump_attribute(&attr
, level
);
131 dump_nodes(device_node_cookie
*node
, uint8 level
)
134 device_node_cookie child
= *node
;
135 dump_device(node
, level
);
137 if (get_child(&child
) != B_OK
)
141 dump_nodes(&child
, level
+ 1);
142 } while ((err
= get_next_child(&child
)) == B_OK
);
148 display_device(device_node_cookie
*node
, uint8 level
)
150 uint8 new_level
= level
;
153 struct device_attr_info attr
;
157 uint8 scsi_path_id
= 255;
161 uint8 pci_class_base_id
= 0;
162 uint8 pci_class_sub_id
= 0;
163 uint8 pci_class_api_id
= 0;
164 uint16 pci_vendor_id
= 0;
165 uint16 pci_device_id
= 0;
166 uint16 pci_subsystem_vendor_id
= 0;
167 uint16 pci_subsystem_id
= 0;
170 uint8 scsi_target_lun
= 0;
171 uint8 scsi_target_id
= 0;
172 uint8 scsi_type
= 255;
173 char scsi_vendor
[64];
174 char scsi_product
[64];
176 const char *venShort
;
178 const char *devShort
;
182 attr
.node_cookie
= *node
;
183 attr
.value
.raw
.data
= data
;
184 attr
.value
.raw
.length
= sizeof(data
);
186 while (dm_get_next_attr(&attr
) == B_OK
) {
187 if (!strcmp(attr
.name
, B_DEVICE_BUS
)
188 && attr
.type
== B_STRING_TYPE
) {
189 strlcpy(device_bus
, attr
.value
.string
, 64);
190 } else if (!strcmp(attr
.name
, "scsi/path_id")
191 && attr
.type
== B_UINT8_TYPE
) {
192 scsi_path_id
= attr
.value
.ui8
;
193 } else if (!strcmp(attr
.name
, B_DEVICE_TYPE
)
194 && attr
.type
== B_UINT16_TYPE
)
195 pci_class_base_id
= attr
.value
.ui8
;
196 else if (!strcmp(attr
.name
, B_DEVICE_SUB_TYPE
)
197 && attr
.type
== B_UINT16_TYPE
)
198 pci_class_sub_id
= attr
.value
.ui8
;
199 else if (!strcmp(attr
.name
, B_DEVICE_INTERFACE
)
200 && attr
.type
== B_UINT16_TYPE
)
201 pci_class_api_id
= attr
.value
.ui8
;
202 else if (!strcmp(attr
.name
, B_DEVICE_VENDOR_ID
)
203 && attr
.type
== B_UINT16_TYPE
)
204 pci_vendor_id
= attr
.value
.ui16
;
205 else if (!strcmp(attr
.name
, B_DEVICE_ID
)
206 && attr
.type
== B_UINT16_TYPE
)
207 pci_device_id
= attr
.value
.ui16
;
208 else if (!strcmp(attr
.name
, SCSI_DEVICE_TARGET_LUN_ITEM
)
209 && attr
.type
== B_UINT8_TYPE
)
210 scsi_target_lun
= attr
.value
.ui8
;
211 else if (!strcmp(attr
.name
, SCSI_DEVICE_TARGET_ID_ITEM
)
212 && attr
.type
== B_UINT8_TYPE
)
213 scsi_target_id
= attr
.value
.ui8
;
214 else if (!strcmp(attr
.name
, SCSI_DEVICE_TYPE_ITEM
)
215 && attr
.type
== B_UINT8_TYPE
)
216 scsi_type
= attr
.value
.ui8
;
217 else if (!strcmp(attr
.name
, SCSI_DEVICE_VENDOR_ITEM
)
218 && attr
.type
== B_STRING_TYPE
)
219 strlcpy(scsi_vendor
, attr
.value
.string
, 64);
220 else if (!strcmp(attr
.name
, SCSI_DEVICE_PRODUCT_ITEM
)
221 && attr
.type
== B_STRING_TYPE
)
222 strlcpy(scsi_product
, attr
.value
.string
, 64);
224 if (!strcmp(device_bus
, "isa"))
226 else if (!strcmp(device_bus
, "pci"))
228 else if (scsi_path_id
< 255)
231 /*else if (!strcmp(attr.name, PCI_DEVICE_SUBVENDOR_ID_ITEM)
232 && attr.type == B_UINT16_TYPE)
233 pci_subsystem_vendor_id = attr.value.ui16;
234 else if (!strcmp(attr.name, PCI_DEVICE_SUBSYSTEM_ID_ITEM)
235 && attr.type == B_UINT16_TYPE)
236 pci_subsystem_id = attr.value.ui16;*/
238 attr
.value
.raw
.data
= data
;
239 attr
.value
.raw
.length
= sizeof(data
);
244 new_level
= level
+ 1;
250 get_class_info(pci_class_base_id
, pci_class_sub_id
,
251 pci_class_api_id
, classInfo
, 64);
253 printf("device %s [%x|%x|%x]\n", classInfo
, pci_class_base_id
,
254 pci_class_sub_id
, pci_class_api_id
);
259 get_vendor_info(pci_vendor_id
, &venShort
, &venFull
);
260 if (!venShort
&& !venFull
) {
261 printf("vendor %04x: Unknown\n", pci_vendor_id
);
262 } else if (venShort
&& venFull
) {
263 printf("vendor %04x: %s - %s\n", pci_vendor_id
,
266 printf("vendor %04x: %s\n", pci_vendor_id
,
267 venShort
? venShort
: venFull
);
272 get_device_info(pci_vendor_id
, pci_device_id
,
273 pci_subsystem_vendor_id
, pci_subsystem_id
, &devShort
, &devFull
);
274 if (!devShort
&& !devFull
) {
275 printf("device %04x: Unknown\n", pci_device_id
);
276 } else if (devShort
&& devFull
) {
277 printf("device %04x: %s (%s)\n", pci_device_id
,
280 printf("device %04x: %s\n", pci_device_id
,
281 devShort
? devShort
: devFull
);
283 new_level
= level
+ 1;
286 if (scsi_type
== 255)
289 printf(" device [%x|%x]\n", scsi_target_id
, scsi_target_lun
);
291 printf(" vendor %15s\tmodel %15s\ttype %s\n", scsi_vendor
,
292 scsi_product
, get_scsi_device_type(scsi_type
));
294 new_level
= level
+ 1;
303 display_nodes(device_node_cookie
*node
, uint8 level
)
306 device_node_cookie child
= *node
;
307 level
= display_device(node
, level
);
309 if (get_child(&child
) != B_OK
)
313 display_nodes(&child
, level
);
314 } while ((err
= get_next_child(&child
)) == B_OK
);
319 main(int argc
, char **argv
)
322 device_node_cookie root
;
324 if ((error
= init_dm_wrapper()) < 0) {
325 printf("Error initializing device manager (%s)\n", strerror(error
));
333 if (!strcmp(argv
[1], "-d")) {
340 if (gMode
== DUMP_MODE
) {
342 dump_nodes(&root
, 0);
345 display_nodes(&root
, 0);