6 #define lpcilib_c /* Define the library */
13 /* removing any \n found in a string */
14 static void remove_eol(char *string
)
16 int j
= strlen(string
);
18 for (i
= 0; i
< j
; i
++)
19 if (string
[i
] == '\n')
23 static int pci_getinfo(lua_State
*L
)
25 struct pci_domain
*pci_domain
;
26 struct pci_device
*pci_device
;
29 pci_domain
= pci_scan();
31 lua_newtable(L
); /* list of busses */
33 for_each_pci_func(pci_device
, pci_domain
) {
34 lua_pushnumber(L
, pci_dev
++);
36 lua_newtable(L
); /* device infos */
38 lua_pushstring(L
, "bus");
39 lua_pushnumber(L
, __pci_bus
);
42 lua_pushstring(L
, "slot");
43 lua_pushnumber(L
, __pci_slot
);
46 lua_pushstring(L
, "func");
47 lua_pushnumber(L
, __pci_func
);
50 lua_pushstring(L
, "vendor");
51 lua_pushnumber(L
, pci_device
->vendor
);
54 lua_pushstring(L
, "product");
55 lua_pushnumber(L
, pci_device
->product
);
58 lua_pushstring(L
, "sub_vendor");
59 lua_pushnumber(L
, pci_device
->sub_vendor
);
62 lua_pushstring(L
, "sub_product");
63 lua_pushnumber(L
, pci_device
->sub_product
);
66 lua_settable(L
,-3); /* end device infos */
72 /* Try to match any pci device to the appropriate kernel module */
73 /* it uses the modules.pcimap from the boot device*/
74 static int pci_getidlist(lua_State
*L
)
76 const char *pciidfile
;
81 char productvendor
[9];
82 char productvendorsub
[17];
85 if (lua_gettop(L
) == 1) {
86 pciidfile
= luaL_checkstring(L
, 1);
88 pciidfile
= "pci.ids";
91 lua_newtable(L
); /* list of vendors */
93 /* Opening the pci.ids from the boot device*/
94 f
=fopen(pciidfile
,"r");
98 /* for each line we found in the pci.ids*/
99 while ( fgets(line
, sizeof line
, f
) ) {
100 /* Skipping uncessary lines */
101 if ((line
[0] == '#') || (line
[0] == ' ') || (line
[0] == 'C') ||
105 /* If the line doesn't start with a tab, it means that's a vendor id */
106 if (line
[0] != '\t') {
108 /* the 4th first chars are the vendor_id */
109 strlcpy(vendor_id
,line
,4);
111 /* the vendor name is the next field*/
113 strlcpy(vendor
,skipspace(strstr(line
," ")),255);
116 /* ffff is an invalid vendor id */
117 if (strstr(vendor_id
,"ffff")) break;
119 lua_pushstring(L
, vendor_id
);
120 lua_pushstring(L
, vendor
);
123 /* if we have a tab + a char, it means this is a product id */
124 } else if ((line
[0] == '\t') && (line
[1] != '\t')) {
126 /* the product name the second field */
127 strlcpy(product
,skipspace(strstr(line
," ")),255);
130 /* the 4th first chars are the vendor_id */
131 strlcpy(productvendor
,vendor_id
,4);
132 /* the product id is first field */
133 strlcpy(productvendor
+4,&line
[1],4);
136 lua_pushstring(L
, productvendor
);
137 lua_pushstring(L
, product
);
140 /* if we have two tabs, it means this is a sub product */
141 } else if ((line
[0] == '\t') && (line
[1] == '\t')) {
143 /* the product name is last field */
144 strlcpy(product
,skipspace(strstr(line
," ")),255);
145 strlcpy(product
,skipspace(strstr(product
," ")),255);
148 strlcpy(productvendorsub
, productvendor
,8);
149 strlcpy(productvendorsub
+8, &line
[2],4);
150 strlcpy(productvendorsub
+12, &line
[7],4);
151 productvendorsub
[16]=0;
153 lua_pushstring(L
, productvendorsub
);
154 lua_pushstring(L
, product
);
163 static const luaL_reg pcilib
[] = {
164 {"getinfo", pci_getinfo
},
165 {"getidlist", pci_getidlist
},
169 /* This defines a function that opens up your library. */
171 LUALIB_API
int luaopen_pci (lua_State
*L
) {
172 luaL_openlib(L
, LUA_PCILIBNAME
, pcilib
, 0);