All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] PCI device enumeration for lua
@ 2010-10-27 19:29 Evan Broder
  2010-11-05 22:02 ` Evan Broder
  0 siblings, 1 reply; 6+ messages in thread
From: Evan Broder @ 2010-10-27 19:29 UTC (permalink / raw)
  To: grub-devel

[-- Attachment #1: Type: text/plain, Size: 672 bytes --]

Hi -
    This patch adds anew enum_pci(callback) function to the Lua grub
library for enumerating devices on the PCI bus. This is not currently
possible because there's no way to get the output of a command from
grub.run.

While I think that would be useful to have in the more general case,
in this particular case using the internal grub_pci_iterate function
is better, because it keeps Lua code from having to parse the lengthy
output from the lspci command.

The callback function passed to enum_pci is called once for each
device found: the bus index, the device index, the function index, the
PCI ID as a single numeric value, and the device class.

Thanks,
 - Evan

[-- Attachment #2: grub_lua_enum_pci.diff --]
[-- Type: application/octet-stream, Size: 1696 bytes --]

=== modified file 'grub_lib.c'
--- grub_lib.c	2010-09-21 18:00:15 +0000
+++ grub_lib.c	2010-10-27 19:25:37 +0000
@@ -27,6 +27,7 @@
 #include <grub/normal.h>
 #include <grub/file.h>
 #include <grub/device.h>
+#include <grub/pci.h>
 
 /* Updates the globals grub_errno and grub_msg, leaving their values on the 
    top of the stack, and clears grub_errno. When grub_errno is zero, grub_msg
@@ -251,6 +252,38 @@
 }
 
 static int
+grub_lua_enum_pci (lua_State *state)
+{
+  auto int NESTED_FUNC_ATTR enum_pci (grub_pci_device_t dev, grub_pci_id_t pciid);
+  int NESTED_FUNC_ATTR enum_pci (grub_pci_device_t dev, grub_pci_id_t pciid)
+  {
+    int result;
+    grub_pci_address_t addr;
+    grub_uint32_t class;
+
+    lua_pushvalue (state, 1);
+    lua_pushinteger (state, grub_pci_get_bus (dev));
+    lua_pushinteger (state, grub_pci_get_device (dev));
+    lua_pushinteger (state, grub_pci_get_function (dev));
+    lua_pushinteger (state, pciid);
+
+    addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
+    class = grub_pci_read (addr);
+    lua_pushinteger (state, class);
+
+    lua_call (state, 5, 1);
+    result = lua_tointeger (state, -1);
+    lua_pop (state, 1);
+
+    return result;
+  }
+
+  luaL_checktype (state, 1, LUA_TFUNCTION);
+  grub_pci_iterate (enum_pci);
+  return push_result (state);
+}
+
+static int
 grub_lua_file_open (lua_State *state)
 {
   grub_file_t file;
@@ -446,6 +479,7 @@
     {"setenv", grub_lua_setenv},
     {"enum_device", grub_lua_enum_device},
     {"enum_file", grub_lua_enum_file},
+    {"enum_pci", grub_lua_enum_pci},
     {"file_open", grub_lua_file_open},
     {"file_close", grub_lua_file_close},
     {"file_seek", grub_lua_file_seek},


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-11-07  5:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-10-27 19:29 [PATCH] PCI device enumeration for lua Evan Broder
2010-11-05 22:02 ` Evan Broder
2010-11-06 10:06   ` Duboucher Thomas
2010-11-06 20:12     ` Evan Broder
2010-11-06 21:25       ` Duboucher Thomas
2010-11-07  5:35         ` Evan Broder

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.