xref: /linux/drivers/pci/pci-driver.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
1 /*
2  * drivers/pci/pci-driver.c
3  *
4  */
5 
6 #include <linux/pci.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/device.h>
10 #include <linux/mempolicy.h>
11 #include <linux/string.h>
12 #include <linux/slab.h>
13 #include <linux/sched.h>
14 #include "pci.h"
15 
16 /*
17  *  Registration of PCI drivers and handling of hot-pluggable devices.
18  */
19 
20 /*
21  * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
22  */
23 
24 struct pci_dynid {
25 	struct list_head node;
26 	struct pci_device_id id;
27 };
28 
29 #ifdef CONFIG_HOTPLUG
30 
31 /**
32  * store_new_id - add a new PCI device ID to this driver and re-probe devices
33  * @driver: target device driver
34  * @buf: buffer for scanning device ID data
35  * @count: input size
36  *
37  * Adds a new dynamic pci device ID to this driver,
38  * and causes the driver to probe for all devices again.
39  */
40 static ssize_t
41 store_new_id(struct device_driver *driver, const char *buf, size_t count)
42 {
43 	struct pci_dynid *dynid;
44 	struct pci_driver *pdrv = to_pci_driver(driver);
45 	__u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
46 		subdevice=PCI_ANY_ID, class=0, class_mask=0;
47 	unsigned long driver_data=0;
48 	int fields=0;
49 
50 	fields = sscanf(buf, "%x %x %x %x %x %x %lux",
51 			&vendor, &device, &subvendor, &subdevice,
52 			&class, &class_mask, &driver_data);
53 	if (fields < 0)
54 		return -EINVAL;
55 
56 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
57 	if (!dynid)
58 		return -ENOMEM;
59 
60 	INIT_LIST_HEAD(&dynid->node);
61 	dynid->id.vendor = vendor;
62 	dynid->id.device = device;
63 	dynid->id.subvendor = subvendor;
64 	dynid->id.subdevice = subdevice;
65 	dynid->id.class = class;
66 	dynid->id.class_mask = class_mask;
67 	dynid->id.driver_data = pdrv->dynids.use_driver_data ?
68 		driver_data : 0UL;
69 
70 	spin_lock(&pdrv->dynids.lock);
71 	list_add_tail(&pdrv->dynids.list, &dynid->node);
72 	spin_unlock(&pdrv->dynids.lock);
73 
74 	if (get_driver(&pdrv->driver)) {
75 		driver_attach(&pdrv->driver);
76 		put_driver(&pdrv->driver);
77 	}
78 
79 	return count;
80 }
81 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
82 
83 static void
84 pci_free_dynids(struct pci_driver *drv)
85 {
86 	struct pci_dynid *dynid, *n;
87 
88 	spin_lock(&drv->dynids.lock);
89 	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
90 		list_del(&dynid->node);
91 		kfree(dynid);
92 	}
93 	spin_unlock(&drv->dynids.lock);
94 }
95 
96 static int
97 pci_create_newid_file(struct pci_driver *drv)
98 {
99 	int error = 0;
100 	if (drv->probe != NULL)
101 		error = sysfs_create_file(&drv->driver.kobj,
102 					  &driver_attr_new_id.attr);
103 	return error;
104 }
105 
106 #else /* !CONFIG_HOTPLUG */
107 static inline void pci_free_dynids(struct pci_driver *drv) {}
108 static inline int pci_create_newid_file(struct pci_driver *drv)
109 {
110 	return 0;
111 }
112 #endif
113 
114 /**
115  * pci_match_id - See if a pci device matches a given pci_id table
116  * @ids: array of PCI device id structures to search in
117  * @dev: the PCI device structure to match against.
118  *
119  * Used by a driver to check whether a PCI device present in the
120  * system is in its list of supported devices.  Returns the matching
121  * pci_device_id structure or %NULL if there is no match.
122  *
123  * Depreciated, don't use this as it will not catch any dynamic ids
124  * that a driver might want to check for.
125  */
126 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
127 					 struct pci_dev *dev)
128 {
129 	if (ids) {
130 		while (ids->vendor || ids->subvendor || ids->class_mask) {
131 			if (pci_match_one_device(ids, dev))
132 				return ids;
133 			ids++;
134 		}
135 	}
136 	return NULL;
137 }
138 
139 /**
140  * pci_match_device - Tell if a PCI device structure has a matching
141  *                    PCI device id structure
142  * @ids: array of PCI device id structures to search in
143  * @dev: the PCI device structure to match against
144  * @drv: the PCI driver to match against
145  *
146  * Used by a driver to check whether a PCI device present in the
147  * system is in its list of supported devices.  Returns the matching
148  * pci_device_id structure or %NULL if there is no match.
149  */
150 const struct pci_device_id *pci_match_device(struct pci_driver *drv,
151 					     struct pci_dev *dev)
152 {
153 	const struct pci_device_id *id;
154 	struct pci_dynid *dynid;
155 
156 	id = pci_match_id(drv->id_table, dev);
157 	if (id)
158 		return id;
159 
160 	/* static ids didn't match, lets look at the dynamic ones */
161 	spin_lock(&drv->dynids.lock);
162 	list_for_each_entry(dynid, &drv->dynids.list, node) {
163 		if (pci_match_one_device(&dynid->id, dev)) {
164 			spin_unlock(&drv->dynids.lock);
165 			return &dynid->id;
166 		}
167 	}
168 	spin_unlock(&drv->dynids.lock);
169 	return NULL;
170 }
171 
172 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
173 			  const struct pci_device_id *id)
174 {
175 	int error;
176 #ifdef CONFIG_NUMA
177 	/* Execute driver initialization on node where the
178 	   device's bus is attached to.  This way the driver likely
179 	   allocates its local memory on the right node without
180 	   any need to change it. */
181 	struct mempolicy *oldpol;
182 	cpumask_t oldmask = current->cpus_allowed;
183 	int node = pcibus_to_node(dev->bus);
184 	if (node >= 0 && node_online(node))
185 	    set_cpus_allowed(current, node_to_cpumask(node));
186 	/* And set default memory allocation policy */
187 	oldpol = current->mempolicy;
188 	current->mempolicy = &default_policy;
189 	mpol_get(current->mempolicy);
190 #endif
191 	error = drv->probe(dev, id);
192 #ifdef CONFIG_NUMA
193 	set_cpus_allowed(current, oldmask);
194 	mpol_free(current->mempolicy);
195 	current->mempolicy = oldpol;
196 #endif
197 	return error;
198 }
199 
200 /**
201  * __pci_device_probe()
202  * @drv: driver to call to check if it wants the PCI device
203  * @pci_dev: PCI device being probed
204  *
205  * returns 0 on success, else error.
206  * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
207  */
208 static int
209 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
210 {
211 	const struct pci_device_id *id;
212 	int error = 0;
213 
214 	if (!pci_dev->driver && drv->probe) {
215 		error = -ENODEV;
216 
217 		id = pci_match_device(drv, pci_dev);
218 		if (id)
219 			error = pci_call_probe(drv, pci_dev, id);
220 		if (error >= 0) {
221 			pci_dev->driver = drv;
222 			error = 0;
223 		}
224 	}
225 	return error;
226 }
227 
228 static int pci_device_probe(struct device * dev)
229 {
230 	int error = 0;
231 	struct pci_driver *drv;
232 	struct pci_dev *pci_dev;
233 
234 	drv = to_pci_driver(dev->driver);
235 	pci_dev = to_pci_dev(dev);
236 	pci_dev_get(pci_dev);
237 	error = __pci_device_probe(drv, pci_dev);
238 	if (error)
239 		pci_dev_put(pci_dev);
240 
241 	return error;
242 }
243 
244 static int pci_device_remove(struct device * dev)
245 {
246 	struct pci_dev * pci_dev = to_pci_dev(dev);
247 	struct pci_driver * drv = pci_dev->driver;
248 
249 	if (drv) {
250 		if (drv->remove)
251 			drv->remove(pci_dev);
252 		pci_dev->driver = NULL;
253 	}
254 
255 	/*
256 	 * We would love to complain here if pci_dev->is_enabled is set, that
257 	 * the driver should have called pci_disable_device(), but the
258 	 * unfortunate fact is there are too many odd BIOS and bridge setups
259 	 * that don't like drivers doing that all of the time.
260 	 * Oh well, we can dream of sane hardware when we sleep, no matter how
261 	 * horrible the crap we have to deal with is when we are awake...
262 	 */
263 
264 	pci_dev_put(pci_dev);
265 	return 0;
266 }
267 
268 static int pci_device_suspend(struct device * dev, pm_message_t state)
269 {
270 	struct pci_dev * pci_dev = to_pci_dev(dev);
271 	struct pci_driver * drv = pci_dev->driver;
272 	int i = 0;
273 
274 	if (drv && drv->suspend) {
275 		i = drv->suspend(pci_dev, state);
276 		suspend_report_result(drv->suspend, i);
277 	} else {
278 		pci_save_state(pci_dev);
279 	}
280 	return i;
281 }
282 
283 
284 /*
285  * Default resume method for devices that have no driver provided resume,
286  * or not even a driver at all.
287  */
288 static int pci_default_resume(struct pci_dev *pci_dev)
289 {
290 	int retval = 0;
291 
292 	/* restore the PCI config space */
293 	pci_restore_state(pci_dev);
294 	/* if the device was enabled before suspend, reenable */
295 	if (pci_dev->is_enabled)
296 		retval = pci_enable_device(pci_dev);
297 	/* if the device was busmaster before the suspend, make it busmaster again */
298 	if (pci_dev->is_busmaster)
299 		pci_set_master(pci_dev);
300 
301 	return retval;
302 }
303 
304 static int pci_device_resume(struct device * dev)
305 {
306 	int error;
307 	struct pci_dev * pci_dev = to_pci_dev(dev);
308 	struct pci_driver * drv = pci_dev->driver;
309 
310 	if (drv && drv->resume)
311 		error = drv->resume(pci_dev);
312 	else
313 		error = pci_default_resume(pci_dev);
314 	return error;
315 }
316 
317 static void pci_device_shutdown(struct device *dev)
318 {
319 	struct pci_dev *pci_dev = to_pci_dev(dev);
320 	struct pci_driver *drv = pci_dev->driver;
321 
322 	if (drv && drv->shutdown)
323 		drv->shutdown(pci_dev);
324 }
325 
326 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
327 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
328 
329 static ssize_t
330 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
331 {
332 	struct device_driver *driver = kobj_to_pci_driver(kobj);
333 	struct driver_attribute *dattr = attr_to_driver_attribute(attr);
334 	ssize_t ret;
335 
336 	if (!get_driver(driver))
337 		return -ENODEV;
338 
339 	ret = dattr->show ? dattr->show(driver, buf) : -EIO;
340 
341 	put_driver(driver);
342 	return ret;
343 }
344 
345 static ssize_t
346 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
347 		      const char *buf, size_t count)
348 {
349 	struct device_driver *driver = kobj_to_pci_driver(kobj);
350 	struct driver_attribute *dattr = attr_to_driver_attribute(attr);
351 	ssize_t ret;
352 
353 	if (!get_driver(driver))
354 		return -ENODEV;
355 
356 	ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
357 
358 	put_driver(driver);
359 	return ret;
360 }
361 
362 static struct sysfs_ops pci_driver_sysfs_ops = {
363 	.show = pci_driver_attr_show,
364 	.store = pci_driver_attr_store,
365 };
366 static struct kobj_type pci_driver_kobj_type = {
367 	.sysfs_ops = &pci_driver_sysfs_ops,
368 };
369 
370 /**
371  * __pci_register_driver - register a new pci driver
372  * @drv: the driver structure to register
373  * @owner: owner module of drv
374  *
375  * Adds the driver structure to the list of registered drivers.
376  * Returns a negative value on error, otherwise 0.
377  * If no error occurred, the driver remains registered even if
378  * no device was claimed during registration.
379  */
380 int __pci_register_driver(struct pci_driver *drv, struct module *owner)
381 {
382 	int error;
383 
384 	/* initialize common driver fields */
385 	drv->driver.name = drv->name;
386 	drv->driver.bus = &pci_bus_type;
387 	drv->driver.owner = owner;
388 	drv->driver.kobj.ktype = &pci_driver_kobj_type;
389 
390 	spin_lock_init(&drv->dynids.lock);
391 	INIT_LIST_HEAD(&drv->dynids.list);
392 
393 	/* register with core */
394 	error = driver_register(&drv->driver);
395 
396 	if (!error)
397 		error = pci_create_newid_file(drv);
398 
399 	return error;
400 }
401 
402 /**
403  * pci_unregister_driver - unregister a pci driver
404  * @drv: the driver structure to unregister
405  *
406  * Deletes the driver structure from the list of registered PCI drivers,
407  * gives it a chance to clean up by calling its remove() function for
408  * each device it was responsible for, and marks those devices as
409  * driverless.
410  */
411 
412 void
413 pci_unregister_driver(struct pci_driver *drv)
414 {
415 	driver_unregister(&drv->driver);
416 	pci_free_dynids(drv);
417 }
418 
419 static struct pci_driver pci_compat_driver = {
420 	.name = "compat"
421 };
422 
423 /**
424  * pci_dev_driver - get the pci_driver of a device
425  * @dev: the device to query
426  *
427  * Returns the appropriate pci_driver structure or %NULL if there is no
428  * registered driver for the device.
429  */
430 struct pci_driver *
431 pci_dev_driver(const struct pci_dev *dev)
432 {
433 	if (dev->driver)
434 		return dev->driver;
435 	else {
436 		int i;
437 		for(i=0; i<=PCI_ROM_RESOURCE; i++)
438 			if (dev->resource[i].flags & IORESOURCE_BUSY)
439 				return &pci_compat_driver;
440 	}
441 	return NULL;
442 }
443 
444 /**
445  * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
446  * @dev: the PCI device structure to match against
447  * @drv: the device driver to search for matching PCI device id structures
448  *
449  * Used by a driver to check whether a PCI device present in the
450  * system is in its list of supported devices. Returns the matching
451  * pci_device_id structure or %NULL if there is no match.
452  */
453 static int pci_bus_match(struct device *dev, struct device_driver *drv)
454 {
455 	struct pci_dev *pci_dev = to_pci_dev(dev);
456 	struct pci_driver *pci_drv = to_pci_driver(drv);
457 	const struct pci_device_id *found_id;
458 
459 	found_id = pci_match_device(pci_drv, pci_dev);
460 	if (found_id)
461 		return 1;
462 
463 	return 0;
464 }
465 
466 /**
467  * pci_dev_get - increments the reference count of the pci device structure
468  * @dev: the device being referenced
469  *
470  * Each live reference to a device should be refcounted.
471  *
472  * Drivers for PCI devices should normally record such references in
473  * their probe() methods, when they bind to a device, and release
474  * them by calling pci_dev_put(), in their disconnect() methods.
475  *
476  * A pointer to the device with the incremented reference counter is returned.
477  */
478 struct pci_dev *pci_dev_get(struct pci_dev *dev)
479 {
480 	if (dev)
481 		get_device(&dev->dev);
482 	return dev;
483 }
484 
485 /**
486  * pci_dev_put - release a use of the pci device structure
487  * @dev: device that's been disconnected
488  *
489  * Must be called when a user of a device is finished with it.  When the last
490  * user of the device calls this function, the memory of the device is freed.
491  */
492 void pci_dev_put(struct pci_dev *dev)
493 {
494 	if (dev)
495 		put_device(&dev->dev);
496 }
497 
498 #ifndef CONFIG_HOTPLUG
499 int pci_uevent(struct device *dev, char **envp, int num_envp,
500 	       char *buffer, int buffer_size)
501 {
502 	return -ENODEV;
503 }
504 #endif
505 
506 struct bus_type pci_bus_type = {
507 	.name		= "pci",
508 	.match		= pci_bus_match,
509 	.uevent		= pci_uevent,
510 	.probe		= pci_device_probe,
511 	.remove		= pci_device_remove,
512 	.suspend	= pci_device_suspend,
513 	.shutdown	= pci_device_shutdown,
514 	.resume		= pci_device_resume,
515 	.dev_attrs	= pci_dev_attrs,
516 };
517 
518 static int __init pci_driver_init(void)
519 {
520 	return bus_register(&pci_bus_type);
521 }
522 
523 postcore_initcall(pci_driver_init);
524 
525 EXPORT_SYMBOL(pci_match_id);
526 EXPORT_SYMBOL(pci_match_device);
527 EXPORT_SYMBOL(__pci_register_driver);
528 EXPORT_SYMBOL(pci_unregister_driver);
529 EXPORT_SYMBOL(pci_dev_driver);
530 EXPORT_SYMBOL(pci_bus_type);
531 EXPORT_SYMBOL(pci_dev_get);
532 EXPORT_SYMBOL(pci_dev_put);
533