pci-stub.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* pci-stub - simple stub driver to reserve a pci device
  2. *
  3. * Copyright (C) 2008 Red Hat, Inc.
  4. * Author:
  5. * Chris Wright
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2.
  8. *
  9. * Usage is simple, allocate a new id to the stub driver and bind the
  10. * device to it. For example:
  11. *
  12. * # echo "8086 10f5" > /sys/bus/pci/drivers/pci-stub/new_id
  13. * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind
  14. * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/pci-stub/bind
  15. * # ls -l /sys/bus/pci/devices/0000:00:19.0/driver
  16. * .../0000:00:19.0/driver -> ../../../bus/pci/drivers/pci-stub
  17. */
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. static char ids[1024] __initdata;
  21. module_param_string(ids, ids, sizeof(ids), 0);
  22. MODULE_PARM_DESC(ids, "Initial PCI IDs to add to the stub driver, format is "
  23. "\"vendor:device[:subvendor[:subdevice[:class[:class_mask]]]]\""
  24. " and multiple comma separated entries can be specified");
  25. static int pci_stub_probe(struct pci_dev *dev, const struct pci_device_id *id)
  26. {
  27. dev_info(&dev->dev, "claimed by stub\n");
  28. return 0;
  29. }
  30. static struct pci_driver stub_driver = {
  31. .name = "pci-stub",
  32. .id_table = NULL, /* only dynamic id's */
  33. .probe = pci_stub_probe,
  34. };
  35. static int __init pci_stub_init(void)
  36. {
  37. char *p, *id;
  38. int rc;
  39. rc = pci_register_driver(&stub_driver);
  40. if (rc)
  41. return rc;
  42. /* no ids passed actually */
  43. if (ids[0] == '\0')
  44. return 0;
  45. /* add ids specified in the module parameter */
  46. p = ids;
  47. while ((id = strsep(&p, ","))) {
  48. unsigned int vendor, device, subvendor = PCI_ANY_ID,
  49. subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
  50. int fields;
  51. if (!strlen(id))
  52. continue;
  53. fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
  54. &vendor, &device, &subvendor, &subdevice,
  55. &class, &class_mask);
  56. if (fields < 2) {
  57. printk(KERN_WARNING
  58. "pci-stub: invalid id string \"%s\"\n", id);
  59. continue;
  60. }
  61. printk(KERN_INFO
  62. "pci-stub: add %04X:%04X sub=%04X:%04X cls=%08X/%08X\n",
  63. vendor, device, subvendor, subdevice, class, class_mask);
  64. rc = pci_add_dynid(&stub_driver, vendor, device,
  65. subvendor, subdevice, class, class_mask, 0);
  66. if (rc)
  67. printk(KERN_WARNING
  68. "pci-stub: failed to add dynamic id (%d)\n", rc);
  69. }
  70. return 0;
  71. }
  72. static void __exit pci_stub_exit(void)
  73. {
  74. pci_unregister_driver(&stub_driver);
  75. }
  76. module_init(pci_stub_init);
  77. module_exit(pci_stub_exit);
  78. MODULE_LICENSE("GPL");
  79. MODULE_AUTHOR("Chris Wright <chrisw@sous-sol.org>");