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