Skip to content

Instantly share code, notes, and snippets.

@levex
Created June 6, 2014 12:48
Show Gist options
  • Save levex/cd78f50565d2e5d6ceeb to your computer and use it in GitHub Desktop.
Save levex/cd78f50565d2e5d6ceeb to your computer and use it in GitHub Desktop.
Module for the QEMU+PCI tutorial
#define pr_fmt(fmt) "tut: " fmt
/* random includes... */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/printk.h>
/* various module stuff, we use GPL! */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Levente Kurusa <levex@linux.com>");
MODULE_DESCRIPTION("PCI driver for the tutorial device");
static const struct pci_device_id pcidevtbl[] = {
/* remember the vendor id we supplied to QEMU?
* and the device id?
* you can safely ignore the rest here for now.
*/
{ 0x1337, 0x0001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
{ } /* terminate */
};
/* this is the function which gets called when the pci core
* sees a device that is registered in the @pcidevtbl struct
*/
static int tutpci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
pr_info("picked up device\n");
return 0;
}
/* called when the PCI core realizes that one of the pci_dev's handled by
* this driver are removed (for whatever reason) from the system
*/
static void tutpci_remove(struct pci_dev *pdev) {
pr_debug("unloaded driver\n");
}
/* definition of a simple PCI driver */
static struct pci_driver tut_pci_driver = {
.name = "tutorial driver",
.id_table = pcidevtbl,
.probe = tutpci_probe,
.remove = tutpci_remove,
};
/* called when we are insmod'd. yupee party time! */
static int __init tutpci_init(void)
{
int rc;
pr_debug("module loaded, registering pci driver.\n");
/* this is how we turn this module into a PCI driver */
rc = pci_register_driver(&tut_pci_driver);
if (rc) {
pr_err("failed to register driver.\n");
/* remember to propagate errors! */
return rc;
}
return 0;
}
/* called when the module is removed! clean up time! */
static void __exit tutpci_exit(void)
{
pr_debug("driver unloaded :-(\n");
return;
}
/* these lines show what should be done on insmod/rmmod */
module_init(tutpci_init);
module_exit(tutpci_exit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment