1 | /*
|
---|
2 | * driver.c - centralized device driver management
|
---|
3 | *
|
---|
4 | * Copyright (c) 2002-3 Patrick Mochel
|
---|
5 | * Copyright (c) 2002-3 Open Source Development Labs
|
---|
6 | * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
|
---|
7 | * Copyright (c) 2007 Novell Inc.
|
---|
8 | *
|
---|
9 | * This file is released under the GPLv2
|
---|
10 | *
|
---|
11 | */
|
---|
12 |
|
---|
13 | // current based on linux 3.0.95 code
|
---|
14 |
|
---|
15 | #include <linux/device.h>
|
---|
16 | #include <linux/printk.h>
|
---|
17 | #include <linux/pci.h>
|
---|
18 | #include <linux/module.h>
|
---|
19 | #include <linux/errno.h>
|
---|
20 | #include <linux/slab.h>
|
---|
21 | #include <linux/string.h>
|
---|
22 | #include <linux/mm.h>
|
---|
23 | #include <linux/klist.h>
|
---|
24 | #include <linux/notifier.h>
|
---|
25 | #include <linux/pm_runtime.h>
|
---|
26 | #include "base.h"
|
---|
27 |
|
---|
28 | #define devres_log(dev, node, op) do {} while (0)
|
---|
29 |
|
---|
30 | struct devres_node {
|
---|
31 | struct list_head entry;
|
---|
32 | dr_release_t release;
|
---|
33 | #ifdef CONFIG_DEBUG_DEVRES
|
---|
34 | const char *name;
|
---|
35 | size_t size;
|
---|
36 | #endif
|
---|
37 | };
|
---|
38 |
|
---|
39 | struct devres {
|
---|
40 | struct devres_node node;
|
---|
41 | /* -- 3 pointers */
|
---|
42 | unsigned long long data[1]; /* guarantee ull alignment */
|
---|
43 | };
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * dev_set_name - set a device name
|
---|
48 | * @dev: device
|
---|
49 | * @fmt: format string for the device's name
|
---|
50 | */
|
---|
51 | int dev_set_name(struct device *dev, const char *fmt, ...)
|
---|
52 | {
|
---|
53 | va_list vargs;
|
---|
54 | int err;
|
---|
55 |
|
---|
56 | va_start(vargs, fmt);
|
---|
57 | err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
|
---|
58 | va_end(vargs);
|
---|
59 | return err;
|
---|
60 | }
|
---|
61 |
|
---|
62 | static void add_dr(struct device *dev, struct devres_node *node)
|
---|
63 | {
|
---|
64 | devres_log(dev, node, "ADD");
|
---|
65 | BUG_ON(!list_empty(&node->entry));
|
---|
66 | list_add_tail(&node->entry, &dev->devres_head);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * devres_add - Register device resource
|
---|
71 | * @dev: Device to add resource to
|
---|
72 | * @res: Resource to register
|
---|
73 | *
|
---|
74 | * Register devres @res to @dev. @res should have been allocated
|
---|
75 | * using devres_alloc(). On driver detach, the associated release
|
---|
76 | * function will be invoked and devres will be freed automatically.
|
---|
77 | */
|
---|
78 | void devres_add(struct device *dev, void *res)
|
---|
79 | {
|
---|
80 | struct devres *dr = container_of(res, struct devres, data);
|
---|
81 | unsigned long flags;
|
---|
82 |
|
---|
83 | spin_lock_irqsave(&dev->devres_lock, flags);
|
---|
84 | add_dr(dev, &dr->node);
|
---|
85 | spin_unlock_irqrestore(&dev->devres_lock, flags);
|
---|
86 | }
|
---|
87 |
|
---|
88 | static struct device *next_device(struct klist_iter *i)
|
---|
89 | {
|
---|
90 | struct klist_node *n = klist_next(i);
|
---|
91 | struct device *dev = NULL;
|
---|
92 | struct device_private *dev_prv;
|
---|
93 |
|
---|
94 | if (n) {
|
---|
95 | dev_prv = to_device_private_driver(n);
|
---|
96 | dev = dev_prv->device;
|
---|
97 | }
|
---|
98 | return dev;
|
---|
99 | }
|
---|
100 |
|
---|
101 | #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
|
---|
102 |
|
---|
103 | static struct bus_type *bus_get(struct bus_type *bus)
|
---|
104 | {
|
---|
105 | if (bus) {
|
---|
106 | kset_get(&bus->p->subsys);
|
---|
107 | return bus;
|
---|
108 | }
|
---|
109 | return NULL;
|
---|
110 | }
|
---|
111 |
|
---|
112 | static void bus_put(struct bus_type *bus)
|
---|
113 | {
|
---|
114 | if (bus)
|
---|
115 | kset_put(&bus->p->subsys);
|
---|
116 | }
|
---|
117 |
|
---|
118 | static void driver_release(struct kobject *kobj)
|
---|
119 | {
|
---|
120 | struct driver_private *drv_priv = to_driver(kobj);
|
---|
121 |
|
---|
122 | pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
|
---|
123 | kfree(drv_priv);
|
---|
124 | }
|
---|
125 |
|
---|
126 | static struct kobj_type driver_ktype = {
|
---|
127 | // .sysfs_ops = &driver_sysfs_ops,
|
---|
128 | .release = driver_release,
|
---|
129 | };
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * sysfs bindings for buses
|
---|
133 | */
|
---|
134 | static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
|
---|
135 | char *buf)
|
---|
136 | {
|
---|
137 | struct bus_attribute *bus_attr = to_bus_attr(attr);
|
---|
138 | struct subsys_private *subsys_priv = to_subsys_private(kobj);
|
---|
139 | ssize_t ret = 0;
|
---|
140 |
|
---|
141 | if (bus_attr->show)
|
---|
142 | ret = bus_attr->show(subsys_priv->bus, buf);
|
---|
143 | return ret;
|
---|
144 | }
|
---|
145 |
|
---|
146 | static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
|
---|
147 | const char *buf, size_t count)
|
---|
148 | {
|
---|
149 | struct bus_attribute *bus_attr = to_bus_attr(attr);
|
---|
150 | struct subsys_private *subsys_priv = to_subsys_private(kobj);
|
---|
151 | ssize_t ret = 0;
|
---|
152 |
|
---|
153 | if (bus_attr->store)
|
---|
154 | ret = bus_attr->store(subsys_priv->bus, buf, count);
|
---|
155 | return ret;
|
---|
156 | }
|
---|
157 |
|
---|
158 | static const struct sysfs_ops bus_sysfs_ops = {
|
---|
159 | .show = bus_attr_show,
|
---|
160 | .store = bus_attr_store,
|
---|
161 | };
|
---|
162 |
|
---|
163 | static struct kobj_type bus_ktype = {
|
---|
164 | .sysfs_ops = &bus_sysfs_ops,
|
---|
165 | };
|
---|
166 |
|
---|
167 |
|
---|
168 | static void klist_devices_get(struct klist_node *n)
|
---|
169 | {
|
---|
170 | struct device_private *dev_prv = to_device_private_bus(n);
|
---|
171 | struct device *dev = dev_prv->device;
|
---|
172 |
|
---|
173 | get_device(dev);
|
---|
174 | }
|
---|
175 |
|
---|
176 | static void klist_devices_put(struct klist_node *n)
|
---|
177 | {
|
---|
178 | struct device_private *dev_prv = to_device_private_bus(n);
|
---|
179 | struct device *dev = dev_prv->device;
|
---|
180 |
|
---|
181 | put_device(dev);
|
---|
182 | }
|
---|
183 |
|
---|
184 | static struct kset *bus_kset;
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * bus_add_driver - Add a driver to the bus.
|
---|
188 | * @drv: driver.
|
---|
189 | */
|
---|
190 | int bus_add_driver(struct device_driver *drv)
|
---|
191 | {
|
---|
192 | struct bus_type *bus;
|
---|
193 | struct driver_private *priv;
|
---|
194 | int error = 0;
|
---|
195 |
|
---|
196 | bus = bus_get(drv->bus);
|
---|
197 | if (!bus)
|
---|
198 | return -EINVAL;
|
---|
199 |
|
---|
200 | pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
|
---|
201 |
|
---|
202 | priv = kzalloc(sizeof(*priv), GFP_KERNEL);
|
---|
203 | if (!priv) {
|
---|
204 | error = -ENOMEM;
|
---|
205 | goto out_put_bus;
|
---|
206 | }
|
---|
207 | klist_init(&priv->klist_devices, NULL, NULL);
|
---|
208 | priv->driver = drv;
|
---|
209 | drv->p = priv;
|
---|
210 |
|
---|
211 | priv->kobj.kset = bus->p->drivers_kset; // traps?
|
---|
212 | error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
|
---|
213 | "%s", drv->name);
|
---|
214 | if (error)
|
---|
215 | goto out_unregister;
|
---|
216 |
|
---|
217 | if (drv->bus->p->drivers_autoprobe) {
|
---|
218 | error = driver_attach(drv);
|
---|
219 | if (error)
|
---|
220 | goto out_unregister;
|
---|
221 | }
|
---|
222 | klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
|
---|
223 |
|
---|
224 | module_add_driver(drv->owner, drv);
|
---|
225 |
|
---|
226 | #if 0 //not required ?
|
---|
227 | error = driver_create_file(drv, &driver_attr_uevent);
|
---|
228 | if (error) {
|
---|
229 | printk(KERN_ERR "%s: uevent attr (%s) failed\n",
|
---|
230 | __func__, drv->name);
|
---|
231 | }
|
---|
232 | error = driver_add_attrs(bus, drv);
|
---|
233 | if (error) {
|
---|
234 | /* How the hell do we get out of this pickle? Give up */
|
---|
235 | printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
|
---|
236 | __func__, drv->name);
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (!drv->suppress_bind_attrs) {
|
---|
240 | error = add_bind_files(drv);
|
---|
241 | if (error) {
|
---|
242 | /* Ditto */
|
---|
243 | printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
|
---|
244 | __func__, drv->name);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | #endif
|
---|
248 | // kobject_uevent(&priv->kobj, KOBJ_ADD);
|
---|
249 | return 0;
|
---|
250 |
|
---|
251 | out_unregister:
|
---|
252 | kobject_put(&priv->kobj);
|
---|
253 | kfree(drv->p);
|
---|
254 | drv->p = NULL;
|
---|
255 | out_put_bus:
|
---|
256 | bus_put(bus);
|
---|
257 | return error;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * bus_remove_driver - delete driver from bus's knowledge.
|
---|
262 | * @drv: driver.
|
---|
263 | *
|
---|
264 | * Detach the driver from the devices it controls, and remove
|
---|
265 | * it from its bus's list of drivers. Finally, we drop the reference
|
---|
266 | * to the bus we took in bus_add_driver().
|
---|
267 | */
|
---|
268 | void bus_remove_driver(struct device_driver *drv)
|
---|
269 | {
|
---|
270 | if (!drv->bus)
|
---|
271 | return;
|
---|
272 |
|
---|
273 | // if (!drv->suppress_bind_attrs)
|
---|
274 | // remove_bind_files(drv);
|
---|
275 | // driver_remove_attrs(drv->bus, drv);
|
---|
276 | // driver_remove_file(drv, &driver_attr_uevent);
|
---|
277 | klist_remove(&drv->p->knode_bus);
|
---|
278 | pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
|
---|
279 | driver_detach(drv);
|
---|
280 | module_remove_driver(drv);
|
---|
281 |
|
---|
282 | kobject_put(&drv->p->kobj);
|
---|
283 | bus_put(drv->bus);
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * bus_for_each_dev - device iterator.
|
---|
288 | * @bus: bus type.
|
---|
289 | * @start: device to start iterating from.
|
---|
290 | * @data: data for the callback.
|
---|
291 | * @fn: function to be called for each device.
|
---|
292 | *
|
---|
293 | * Iterate over @bus's list of devices, and call @fn for each,
|
---|
294 | * passing it @data. If @start is not NULL, we use that device to
|
---|
295 | * begin iterating from.
|
---|
296 | *
|
---|
297 | * We check the return of @fn each time. If it returns anything
|
---|
298 | * other than 0, we break out and return that value.
|
---|
299 | *
|
---|
300 | * NOTE: The device that returns a non-zero value is not retained
|
---|
301 | * in any way, nor is its refcount incremented. If the caller needs
|
---|
302 | * to retain this data, it should do so, and increment the reference
|
---|
303 | * count in the supplied callback.
|
---|
304 | */
|
---|
305 | int bus_for_each_dev(struct bus_type *bus, struct device *start,
|
---|
306 | void *data, int (*fn)(struct device *, void *))
|
---|
307 | {
|
---|
308 | struct klist_iter i;
|
---|
309 | struct device *dev;
|
---|
310 | int error = 0;
|
---|
311 |
|
---|
312 | if (!bus || !bus->p)
|
---|
313 | return -EINVAL;
|
---|
314 |
|
---|
315 | klist_iter_init_node(&bus->p->klist_devices, &i,
|
---|
316 | (start ? &start->p->knode_bus : NULL));
|
---|
317 | while ((dev = next_device(&i)) && !error)
|
---|
318 | error = fn(dev, data);
|
---|
319 | klist_iter_exit(&i);
|
---|
320 | return error;
|
---|
321 | }
|
---|
322 | EXPORT_SYMBOL_GPL(bus_for_each_dev);
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * driver_find - locate driver on a bus by its name.
|
---|
326 | * @name: name of the driver.
|
---|
327 | * @bus: bus to scan for the driver.
|
---|
328 | *
|
---|
329 | * Call kset_find_obj() to iterate over list of drivers on
|
---|
330 | * a bus to find driver by name. Return driver if found.
|
---|
331 | *
|
---|
332 | * Note that kset_find_obj increments driver's reference count.
|
---|
333 | */
|
---|
334 | struct device_driver *driver_find(const char *name, struct bus_type *bus)
|
---|
335 | {
|
---|
336 | struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
|
---|
337 | struct driver_private *priv;
|
---|
338 |
|
---|
339 | if (k) {
|
---|
340 | priv = to_driver(k);
|
---|
341 | return priv->driver;
|
---|
342 | }
|
---|
343 | return NULL;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * driver_register - register driver with bus
|
---|
348 | * @drv: driver to register
|
---|
349 | *
|
---|
350 | * We pass off most of the work to the bus_add_driver() call,
|
---|
351 | * since most of the things we have to do deal with the bus
|
---|
352 | * structures.
|
---|
353 | */
|
---|
354 | int driver_register(struct device_driver *drv)
|
---|
355 | {
|
---|
356 | int ret;
|
---|
357 | struct device_driver *other;
|
---|
358 |
|
---|
359 | BUG_ON(!drv->bus->p);
|
---|
360 | #ifndef TARGET_OS2
|
---|
361 | if ((drv->bus->probe && drv->probe) ||
|
---|
362 | (drv->bus->remove && drv->remove) ||
|
---|
363 | (drv->bus->shutdown && drv->shutdown))
|
---|
364 | printk(KERN_WARNING "Driver '%s' needs updating - please use "
|
---|
365 | "bus_type methods\n", drv->name);
|
---|
366 | #endif
|
---|
367 | other = driver_find(drv->name, drv->bus);
|
---|
368 | if (other) {
|
---|
369 | printk(KERN_ERR "Error: Driver '%s' is already registered, "
|
---|
370 | "aborting...\n", drv->name);
|
---|
371 | return -EBUSY;
|
---|
372 | }
|
---|
373 |
|
---|
374 | ret = bus_add_driver(drv);
|
---|
375 | if (ret)
|
---|
376 | return ret;
|
---|
377 | #if 0
|
---|
378 | ret = driver_add_groups(drv, drv->groups);
|
---|
379 | if (ret) {
|
---|
380 | bus_remove_driver(drv);
|
---|
381 | return ret;
|
---|
382 | }
|
---|
383 | kobject_uevent(&drv->p->kobj, KOBJ_ADD);
|
---|
384 | #endif
|
---|
385 | return ret;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * driver_unregister - remove driver from system.
|
---|
390 | * @drv: driver.
|
---|
391 | *
|
---|
392 | * Again, we pass off most of the work to the bus-level call.
|
---|
393 | */
|
---|
394 | void driver_unregister(struct device_driver *drv)
|
---|
395 | {
|
---|
396 | if (!drv || !drv->p) {
|
---|
397 | WARN(1, "Unexpected driver unregister!\n");
|
---|
398 | return;
|
---|
399 | }
|
---|
400 | // driver_remove_groups(drv, drv->groups);
|
---|
401 | bus_remove_driver(drv);
|
---|
402 | }
|
---|
403 | EXPORT_SYMBOL_GPL(driver_unregister);
|
---|
404 |
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * driver_find_device - device iterator for locating a particular device.
|
---|
408 | * @drv: The device's driver
|
---|
409 | * @start: Device to begin with
|
---|
410 | * @data: Data to pass to match function
|
---|
411 | * @match: Callback function to check device
|
---|
412 | *
|
---|
413 | * This is similar to the driver_for_each_device() function above, but
|
---|
414 | * it returns a reference to a device that is 'found' for later use, as
|
---|
415 | * determined by the @match callback.
|
---|
416 | *
|
---|
417 | * The callback should return 0 if the device doesn't match and non-zero
|
---|
418 | * if it does. If the callback returns non-zero, this function will
|
---|
419 | * return to the caller and not iterate over any more devices.
|
---|
420 | */
|
---|
421 | struct device *driver_find_device(struct device_driver *drv,
|
---|
422 | struct device *start, void *data,
|
---|
423 | int (*match)(struct device *dev, void *data))
|
---|
424 | {
|
---|
425 | struct klist_iter i;
|
---|
426 | struct device *dev;
|
---|
427 |
|
---|
428 | if (!drv)
|
---|
429 | return NULL;
|
---|
430 |
|
---|
431 | klist_iter_init_node(&drv->p->klist_devices, &i,
|
---|
432 | (start ? &start->p->knode_driver : NULL));
|
---|
433 | while ((dev = next_device(&i)))
|
---|
434 | if (match(dev, data) && get_device(dev))
|
---|
435 | break;
|
---|
436 | klist_iter_exit(&i);
|
---|
437 | return dev;
|
---|
438 | }
|
---|
439 | EXPORT_SYMBOL_GPL(driver_find_device);
|
---|
440 |
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * bus_register - register a bus with the system.
|
---|
444 | * @bus: bus.
|
---|
445 | *
|
---|
446 | * Once we have that, we registered the bus with the kobject
|
---|
447 | * infrastructure, then register the children subsystems it has:
|
---|
448 | * the devices and drivers that belong to the bus.
|
---|
449 | */
|
---|
450 | int bus_register(struct bus_type *bus)
|
---|
451 | {
|
---|
452 | int retval;
|
---|
453 | struct subsys_private *priv;
|
---|
454 |
|
---|
455 | priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
|
---|
456 | if (!priv)
|
---|
457 | return -ENOMEM;
|
---|
458 |
|
---|
459 | priv->bus = bus;
|
---|
460 | bus->p = priv;
|
---|
461 |
|
---|
462 | BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
|
---|
463 |
|
---|
464 | retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
|
---|
465 | if (retval)
|
---|
466 | goto out;
|
---|
467 |
|
---|
468 | priv->subsys.kobj.kset = bus_kset;
|
---|
469 | priv->subsys.kobj.ktype = &bus_ktype;
|
---|
470 | priv->drivers_autoprobe = 1;
|
---|
471 |
|
---|
472 | retval = kset_register(&priv->subsys);
|
---|
473 | if (retval)
|
---|
474 | goto out;
|
---|
475 |
|
---|
476 | #if 0
|
---|
477 | retval = bus_create_file(bus, &bus_attr_uevent);
|
---|
478 | if (retval)
|
---|
479 | goto bus_uevent_fail;
|
---|
480 | #endif
|
---|
481 | priv->devices_kset = kset_create_and_add("devices", NULL,
|
---|
482 | &priv->subsys.kobj);
|
---|
483 | if (!priv->devices_kset) {
|
---|
484 | retval = -ENOMEM;
|
---|
485 | goto bus_devices_fail;
|
---|
486 | }
|
---|
487 |
|
---|
488 | priv->drivers_kset = kset_create_and_add("drivers", NULL,
|
---|
489 | &priv->subsys.kobj);
|
---|
490 | if (!priv->drivers_kset) {
|
---|
491 | retval = -ENOMEM;
|
---|
492 | goto bus_drivers_fail;
|
---|
493 | }
|
---|
494 |
|
---|
495 | klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
|
---|
496 | klist_init(&priv->klist_drivers, NULL, NULL);
|
---|
497 |
|
---|
498 | #if 0
|
---|
499 | retval = add_probe_files(bus);
|
---|
500 | if (retval)
|
---|
501 | goto bus_probe_files_fail;
|
---|
502 |
|
---|
503 | retval = bus_add_attrs(bus);
|
---|
504 | if (retval)
|
---|
505 | goto bus_attrs_fail;
|
---|
506 | #endif
|
---|
507 | pr_debug("bus: '%s': registered\n", bus->name);
|
---|
508 | return 0;
|
---|
509 |
|
---|
510 | //bus_attrs_fail:
|
---|
511 | // remove_probe_files(bus);
|
---|
512 | //bus_probe_files_fail:
|
---|
513 | // kset_unregister(bus->p->drivers_kset);
|
---|
514 | bus_drivers_fail:
|
---|
515 | kset_unregister(bus->p->devices_kset);
|
---|
516 | bus_devices_fail:
|
---|
517 | // bus_remove_file(bus, &bus_attr_uevent);
|
---|
518 | //bus_uevent_fail:
|
---|
519 | kset_unregister(&bus->p->subsys);
|
---|
520 | out:
|
---|
521 | kfree(bus->p);
|
---|
522 | bus->p = NULL;
|
---|
523 | return retval;
|
---|
524 | }
|
---|
525 | EXPORT_SYMBOL_GPL(bus_register);
|
---|
526 |
|
---|
527 | static int __driver_attach(struct device *dev, void *data)
|
---|
528 | {
|
---|
529 | struct device_driver *drv = data;
|
---|
530 |
|
---|
531 | /*
|
---|
532 | * Lock device and try to bind to it. We drop the error
|
---|
533 | * here and always return 0, because we need to keep trying
|
---|
534 | * to bind to devices and some drivers will return an error
|
---|
535 | * simply if it didn't support the device.
|
---|
536 | *
|
---|
537 | * driver_probe_device() will spit a warning if there
|
---|
538 | * is an error.
|
---|
539 | */
|
---|
540 |
|
---|
541 | if (!driver_match_device(drv, dev))
|
---|
542 | return 0;
|
---|
543 |
|
---|
544 | if (dev->parent) /* Needed for USB */
|
---|
545 | device_lock(dev->parent);
|
---|
546 | device_lock(dev);
|
---|
547 | if (!dev->driver)
|
---|
548 | driver_probe_device(drv, dev);
|
---|
549 | device_unlock(dev);
|
---|
550 | if (dev->parent)
|
---|
551 | device_unlock(dev->parent);
|
---|
552 |
|
---|
553 | return 0;
|
---|
554 | }
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * driver_attach - try to bind driver to devices.
|
---|
558 | * @drv: driver.
|
---|
559 | *
|
---|
560 | * Walk the list of devices that the bus has on it and try to
|
---|
561 | * match the driver with each one. If driver_probe_device()
|
---|
562 | * returns 0 and the @dev->driver is set, we've found a
|
---|
563 | * compatible pair.
|
---|
564 | */
|
---|
565 | int driver_attach(struct device_driver *drv)
|
---|
566 | {
|
---|
567 | return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
|
---|
568 | }
|
---|
569 |
|
---|
570 | static atomic_t probe_count = ATOMIC_INIT(0);
|
---|
571 | static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
|
---|
572 |
|
---|
573 | static int really_probe(struct device *dev, struct device_driver *drv)
|
---|
574 | {
|
---|
575 | int ret = 0;
|
---|
576 | atomic_inc(&probe_count);
|
---|
577 | pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
|
---|
578 | drv->bus->name, __func__, drv->name, dev_name(dev));
|
---|
579 |
|
---|
580 | WARN_ON(!list_empty(&dev->devres_head));
|
---|
581 |
|
---|
582 | dev->driver = drv;
|
---|
583 |
|
---|
584 | #if 0
|
---|
585 | if (driver_sysfs_add(dev)) {
|
---|
586 | printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
|
---|
587 | __func__, dev_name(dev));
|
---|
588 | goto probe_failed;
|
---|
589 | }
|
---|
590 | #endif
|
---|
591 |
|
---|
592 | if (dev->bus->probe) {
|
---|
593 | ret = dev->bus->probe(dev);
|
---|
594 | // if (ret)
|
---|
595 | // goto probe_failed;
|
---|
596 | } else if (drv->probe) {
|
---|
597 | ret = drv->probe(dev);
|
---|
598 | // if (ret)
|
---|
599 | // goto probe_failed;
|
---|
600 | }
|
---|
601 | #ifdef TARGET_OS2xxx
|
---|
602 | driver_bound(dev);
|
---|
603 | ret = 1;
|
---|
604 | pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
|
---|
605 | drv->bus->name, __func__, dev_name(dev), drv->name);
|
---|
606 | goto done;
|
---|
607 |
|
---|
608 | probe_failed:
|
---|
609 | devres_release_all(dev);
|
---|
610 | // driver_sysfs_remove(dev);
|
---|
611 | dev->driver = NULL;
|
---|
612 |
|
---|
613 | if (ret != -ENODEV && ret != -ENXIO) {
|
---|
614 | /* driver matched but the probe failed */
|
---|
615 | printk(KERN_WARNING
|
---|
616 | "%s: probe of %s failed with error %d\n",
|
---|
617 | drv->name, dev_name(dev), ret);
|
---|
618 | }
|
---|
619 | /*
|
---|
620 | * Ignore errors returned by ->probe so that the next driver can try
|
---|
621 | * its luck.
|
---|
622 | */
|
---|
623 | ret = 0;
|
---|
624 | done:
|
---|
625 | atomic_dec(&probe_count);
|
---|
626 | wake_up(&probe_waitqueue);
|
---|
627 | #endif
|
---|
628 | return ret;
|
---|
629 | }
|
---|
630 |
|
---|
631 | /*
|
---|
632 | * __device_release_driver() must be called with @dev lock held.
|
---|
633 | * When called for a USB interface, @dev->parent lock must be held as well.
|
---|
634 | */
|
---|
635 | static void __device_release_driver(struct device *dev)
|
---|
636 | {
|
---|
637 | struct device_driver *drv;
|
---|
638 |
|
---|
639 | drv = dev->driver;
|
---|
640 | if (drv) {
|
---|
641 | pm_runtime_get_sync(dev);
|
---|
642 |
|
---|
643 | #if 0
|
---|
644 | driver_sysfs_remove(dev);
|
---|
645 |
|
---|
646 | if (dev->bus)
|
---|
647 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
|
---|
648 | BUS_NOTIFY_UNBIND_DRIVER,
|
---|
649 | dev);
|
---|
650 | #endif
|
---|
651 | pm_runtime_put_sync(dev);
|
---|
652 |
|
---|
653 | if (dev->bus && dev->bus->remove)
|
---|
654 | dev->bus->remove(dev);
|
---|
655 | else if (drv->remove)
|
---|
656 | drv->remove(dev);
|
---|
657 | devres_release_all(dev);
|
---|
658 | dev->driver = NULL;
|
---|
659 | klist_remove(&dev->p->knode_driver);
|
---|
660 | #if 0
|
---|
661 | if (dev->bus)
|
---|
662 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
|
---|
663 | BUS_NOTIFY_UNBOUND_DRIVER,
|
---|
664 | dev);
|
---|
665 | #endif
|
---|
666 | }
|
---|
667 | }
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * driver_detach - detach driver from all devices it controls.
|
---|
671 | * @drv: driver.
|
---|
672 | */
|
---|
673 | void driver_detach(struct device_driver *drv)
|
---|
674 | {
|
---|
675 | struct device_private *dev_prv;
|
---|
676 | struct device *dev;
|
---|
677 |
|
---|
678 | for (;;) {
|
---|
679 | spin_lock(&drv->p->klist_devices.k_lock);
|
---|
680 | if (list_empty(&drv->p->klist_devices.k_list)) {
|
---|
681 | spin_unlock(&drv->p->klist_devices.k_lock);
|
---|
682 | break;
|
---|
683 | }
|
---|
684 | dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
|
---|
685 | struct device_private,
|
---|
686 | knode_driver.n_node);
|
---|
687 | dev = dev_prv->device;
|
---|
688 | get_device(dev);
|
---|
689 | spin_unlock(&drv->p->klist_devices.k_lock);
|
---|
690 |
|
---|
691 | if (dev->parent) /* Needed for USB */
|
---|
692 | device_lock(dev->parent);
|
---|
693 | device_lock(dev);
|
---|
694 | if (dev->driver == drv)
|
---|
695 | __device_release_driver(dev);
|
---|
696 | device_unlock(dev);
|
---|
697 | if (dev->parent)
|
---|
698 | device_unlock(dev->parent);
|
---|
699 | put_device(dev);
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 | /**
|
---|
704 | * driver_probe_device - attempt to bind device & driver together
|
---|
705 | * @drv: driver to bind a device to
|
---|
706 | * @dev: device to try to bind to the driver
|
---|
707 | *
|
---|
708 | * This function returns -ENODEV if the device is not registered,
|
---|
709 | * 1 if the device is bound successfully and 0 otherwise.
|
---|
710 | *
|
---|
711 | * This function must be called with @dev lock held. When called for a
|
---|
712 | * USB interface, @dev->parent lock must be held as well.
|
---|
713 | */
|
---|
714 | int driver_probe_device(struct device_driver *drv, struct device *dev)
|
---|
715 | {
|
---|
716 | int ret = 0;
|
---|
717 | #if 0
|
---|
718 | if (!device_is_registered(dev))
|
---|
719 | return -ENODEV;
|
---|
720 | #endif
|
---|
721 | pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
|
---|
722 | drv->bus->name, __func__, dev_name(dev), drv->name);
|
---|
723 | pm_runtime_get_noresume(dev);
|
---|
724 | pm_runtime_barrier(dev);
|
---|
725 |
|
---|
726 | ret = really_probe(dev, drv);
|
---|
727 | pm_runtime_put_sync(dev);
|
---|
728 | return ret;
|
---|
729 | }
|
---|
730 |
|
---|
731 | static int __device_attach(struct device_driver *drv, void *data)
|
---|
732 | {
|
---|
733 | struct device *dev = data;
|
---|
734 |
|
---|
735 | if (!driver_match_device(drv, dev))
|
---|
736 | return 0;
|
---|
737 |
|
---|
738 | return driver_probe_device(drv, dev);
|
---|
739 | }
|
---|
740 |
|
---|
741 | /**
|
---|
742 | * device_attach - try to attach device to a driver.
|
---|
743 | * @dev: device.
|
---|
744 | *
|
---|
745 | * Walk the list of drivers that the bus has and call
|
---|
746 | * driver_probe_device() for each pair. If a compatible
|
---|
747 | * pair is found, break out and return.
|
---|
748 | *
|
---|
749 | * Returns 1 if the device was bound to a driver;
|
---|
750 | * 0 if no matching driver was found;
|
---|
751 | * -ENODEV if the device is not registered.
|
---|
752 | *
|
---|
753 | * When called for a USB interface, @dev->parent lock must be held.
|
---|
754 | */
|
---|
755 | int device_attach(struct device *dev)
|
---|
756 | {
|
---|
757 | int ret = 0;
|
---|
758 | device_lock(dev);
|
---|
759 | if (dev->driver) {
|
---|
760 | if (klist_node_attached(&dev->p->knode_driver)) {
|
---|
761 | ret = 1;
|
---|
762 | goto out_unlock;
|
---|
763 | }
|
---|
764 | ret = device_bind_driver(dev);
|
---|
765 | if (ret == 0)
|
---|
766 | ret = 1;
|
---|
767 | else {
|
---|
768 | dev->driver = NULL;
|
---|
769 | ret = 0;
|
---|
770 | }
|
---|
771 | } else {
|
---|
772 | pm_runtime_get_noresume(dev);
|
---|
773 | ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
|
---|
774 | pm_runtime_put_sync(dev);
|
---|
775 | }
|
---|
776 | out_unlock:
|
---|
777 | device_unlock(dev);
|
---|
778 | return ret;
|
---|
779 | }
|
---|
780 | EXPORT_SYMBOL_GPL(device_attach);
|
---|
781 |
|
---|
782 | static struct device_driver *next_driver(struct klist_iter *i)
|
---|
783 | {
|
---|
784 | struct klist_node *n = klist_next(i);
|
---|
785 | struct driver_private *drv_priv;
|
---|
786 |
|
---|
787 | if (n) {
|
---|
788 | drv_priv = container_of(n, struct driver_private, knode_bus);
|
---|
789 | return drv_priv->driver;
|
---|
790 | }
|
---|
791 | return NULL;
|
---|
792 | }
|
---|
793 |
|
---|
794 | /**
|
---|
795 | * bus_for_each_drv - driver iterator
|
---|
796 | * @bus: bus we're dealing with.
|
---|
797 | * @start: driver to start iterating on.
|
---|
798 | * @data: data to pass to the callback.
|
---|
799 | * @fn: function to call for each driver.
|
---|
800 | *
|
---|
801 | * This is nearly identical to the device iterator above.
|
---|
802 | * We iterate over each driver that belongs to @bus, and call
|
---|
803 | * @fn for each. If @fn returns anything but 0, we break out
|
---|
804 | * and return it. If @start is not NULL, we use it as the head
|
---|
805 | * of the list.
|
---|
806 | *
|
---|
807 | * NOTE: we don't return the driver that returns a non-zero
|
---|
808 | * value, nor do we leave the reference count incremented for that
|
---|
809 | * driver. If the caller needs to know that info, it must set it
|
---|
810 | * in the callback. It must also be sure to increment the refcount
|
---|
811 | * so it doesn't disappear before returning to the caller.
|
---|
812 | */
|
---|
813 | int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
|
---|
814 | void *data, int (*fn)(struct device_driver *, void *))
|
---|
815 | {
|
---|
816 | struct klist_iter i;
|
---|
817 | struct device_driver *drv;
|
---|
818 | int error = 0;
|
---|
819 |
|
---|
820 | if (!bus)
|
---|
821 | return -EINVAL;
|
---|
822 |
|
---|
823 | klist_iter_init_node(&bus->p->klist_drivers, &i,
|
---|
824 | start ? &start->p->knode_bus : NULL);
|
---|
825 | while ((drv = next_driver(&i)) && !error)
|
---|
826 | error = fn(drv, data);
|
---|
827 | klist_iter_exit(&i);
|
---|
828 | return error;
|
---|
829 | }
|
---|
830 | EXPORT_SYMBOL_GPL(bus_for_each_drv);
|
---|
831 |
|
---|
832 | static void driver_bound(struct device *dev)
|
---|
833 | {
|
---|
834 | if (klist_node_attached(&dev->p->knode_driver)) {
|
---|
835 | printk(KERN_WARNING "%s: device %s already bound\n",
|
---|
836 | __func__, kobject_name(&dev->kobj));
|
---|
837 | return;
|
---|
838 | }
|
---|
839 |
|
---|
840 | pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),
|
---|
841 | __func__, dev->driver->name);
|
---|
842 |
|
---|
843 | klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
|
---|
844 |
|
---|
845 | #if 0
|
---|
846 | if (dev->bus)
|
---|
847 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
|
---|
848 | BUS_NOTIFY_BOUND_DRIVER, dev);
|
---|
849 | #endif
|
---|
850 | }
|
---|
851 |
|
---|
852 | /**
|
---|
853 | * device_bind_driver - bind a driver to one device.
|
---|
854 | * @dev: device.
|
---|
855 | *
|
---|
856 | * Allow manual attachment of a driver to a device.
|
---|
857 | * Caller must have already set @dev->driver.
|
---|
858 | *
|
---|
859 | * Note that this does not modify the bus reference count
|
---|
860 | * nor take the bus's rwsem. Please verify those are accounted
|
---|
861 | * for before calling this. (It is ok to call with no other effort
|
---|
862 | * from a driver's probe() method.)
|
---|
863 | *
|
---|
864 | * This function must be called with the device lock held.
|
---|
865 | */
|
---|
866 | int device_bind_driver(struct device *dev)
|
---|
867 | {
|
---|
868 | int ret=0;
|
---|
869 |
|
---|
870 | // ret = driver_sysfs_add(dev);
|
---|
871 | // if (!ret)
|
---|
872 | driver_bound(dev);
|
---|
873 | return ret;
|
---|
874 | }
|
---|
875 | EXPORT_SYMBOL_GPL(device_bind_driver);
|
---|