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