blob: a952007b76085bdb409a4ba3a395060d1781deff [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Chris Wrightc70e0d92008-11-25 21:17:13 -08002/* pci-stub - simple stub driver to reserve a pci device
3 *
4 * Copyright (C) 2008 Red Hat, Inc.
5 * Author:
Bjorn Helgaasf7625982013-11-14 11:28:18 -07006 * Chris Wright
Chris Wrightc70e0d92008-11-25 21:17:13 -08007 *
Chris Wrightc70e0d92008-11-25 21:17:13 -08008 * Usage is simple, allocate a new id to the stub driver and bind the
9 * device to it. For example:
Bjorn Helgaasf7625982013-11-14 11:28:18 -070010 *
Chris Wrightc70e0d92008-11-25 21:17:13 -080011 * # echo "8086 10f5" > /sys/bus/pci/drivers/pci-stub/new_id
12 * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind
13 * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/pci-stub/bind
14 * # ls -l /sys/bus/pci/devices/0000:00:19.0/driver
15 * .../0000:00:19.0/driver -> ../../../bus/pci/drivers/pci-stub
16 */
17
18#include <linux/module.h>
19#include <linux/pci.h>
20
Tejun Heob439b1d2009-09-03 15:27:27 +090021static char ids[1024] __initdata;
22
23module_param_string(ids, ids, sizeof(ids), 0);
24MODULE_PARM_DESC(ids, "Initial PCI IDs to add to the stub driver, format is "
25 "\"vendor:device[:subvendor[:subdevice[:class[:class_mask]]]]\""
26 " and multiple comma separated entries can be specified");
27
Chris Wrightc70e0d92008-11-25 21:17:13 -080028static int pci_stub_probe(struct pci_dev *dev, const struct pci_device_id *id)
29{
Joe Perches438be3c2012-10-28 01:05:49 -070030 dev_info(&dev->dev, "claimed by stub\n");
Chris Wrightc70e0d92008-11-25 21:17:13 -080031 return 0;
32}
33
34static struct pci_driver stub_driver = {
35 .name = "pci-stub",
36 .id_table = NULL, /* only dynamic id's */
37 .probe = pci_stub_probe,
38};
39
40static int __init pci_stub_init(void)
41{
Tejun Heob439b1d2009-09-03 15:27:27 +090042 char *p, *id;
43 int rc;
44
45 rc = pci_register_driver(&stub_driver);
46 if (rc)
47 return rc;
48
Yinghai Luee8abf72010-11-17 12:10:40 -080049 /* no ids passed actually */
50 if (ids[0] == '\0')
51 return 0;
52
Tejun Heob439b1d2009-09-03 15:27:27 +090053 /* add ids specified in the module parameter */
54 p = ids;
55 while ((id = strsep(&p, ","))) {
56 unsigned int vendor, device, subvendor = PCI_ANY_ID,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040057 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
Tejun Heob439b1d2009-09-03 15:27:27 +090058 int fields;
59
Tejun Heo99a0fad2010-12-22 10:06:36 +010060 if (!strlen(id))
61 continue;
62
Tejun Heob439b1d2009-09-03 15:27:27 +090063 fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
64 &vendor, &device, &subvendor, &subdevice,
65 &class, &class_mask);
66
67 if (fields < 2) {
68 printk(KERN_WARNING
69 "pci-stub: invalid id string \"%s\"\n", id);
70 continue;
71 }
72
73 printk(KERN_INFO
74 "pci-stub: add %04X:%04X sub=%04X:%04X cls=%08X/%08X\n",
75 vendor, device, subvendor, subdevice, class, class_mask);
76
77 rc = pci_add_dynid(&stub_driver, vendor, device,
78 subvendor, subdevice, class, class_mask, 0);
79 if (rc)
80 printk(KERN_WARNING
81 "pci-stub: failed to add dynamic id (%d)\n", rc);
82 }
83
84 return 0;
Chris Wrightc70e0d92008-11-25 21:17:13 -080085}
86
87static void __exit pci_stub_exit(void)
88{
89 pci_unregister_driver(&stub_driver);
90}
91
92module_init(pci_stub_init);
93module_exit(pci_stub_exit);
94
95MODULE_LICENSE("GPL");
96MODULE_AUTHOR("Chris Wright <chrisw@sous-sol.org>");