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