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