core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * drivers/base/core.c - core driver model code (device registration, etc)
  3. *
  4. * Copyright (c) 2002-3 Patrick Mochel
  5. * Copyright (c) 2002-3 Open Source Development Labs
  6. * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
  7. * Copyright (c) 2006 Novell, Inc.
  8. *
  9. * This file is released under the GPLv2
  10. *
  11. */
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/kdev_t.h>
  19. #include <asm/semaphore.h>
  20. #include "base.h"
  21. #include "power/power.h"
  22. int (*platform_notify)(struct device * dev) = NULL;
  23. int (*platform_notify_remove)(struct device * dev) = NULL;
  24. /*
  25. * sysfs bindings for devices.
  26. */
  27. /**
  28. * dev_driver_string - Return a device's driver name, if at all possible
  29. * @dev: struct device to get the name of
  30. *
  31. * Will return the device's driver's name if it is bound to a device. If
  32. * the device is not bound to a device, it will return the name of the bus
  33. * it is attached to. If it is not attached to a bus either, an empty
  34. * string will be returned.
  35. */
  36. const char *dev_driver_string(struct device *dev)
  37. {
  38. return dev->driver ? dev->driver->name :
  39. (dev->bus ? dev->bus->name : "");
  40. }
  41. EXPORT_SYMBOL_GPL(dev_driver_string);
  42. #define to_dev(obj) container_of(obj, struct device, kobj)
  43. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  44. static ssize_t
  45. dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  46. {
  47. struct device_attribute * dev_attr = to_dev_attr(attr);
  48. struct device * dev = to_dev(kobj);
  49. ssize_t ret = -EIO;
  50. if (dev_attr->show)
  51. ret = dev_attr->show(dev, dev_attr, buf);
  52. return ret;
  53. }
  54. static ssize_t
  55. dev_attr_store(struct kobject * kobj, struct attribute * attr,
  56. const char * buf, size_t count)
  57. {
  58. struct device_attribute * dev_attr = to_dev_attr(attr);
  59. struct device * dev = to_dev(kobj);
  60. ssize_t ret = -EIO;
  61. if (dev_attr->store)
  62. ret = dev_attr->store(dev, dev_attr, buf, count);
  63. return ret;
  64. }
  65. static struct sysfs_ops dev_sysfs_ops = {
  66. .show = dev_attr_show,
  67. .store = dev_attr_store,
  68. };
  69. /**
  70. * device_release - free device structure.
  71. * @kobj: device's kobject.
  72. *
  73. * This is called once the reference count for the object
  74. * reaches 0. We forward the call to the device's release
  75. * method, which should handle actually freeing the structure.
  76. */
  77. static void device_release(struct kobject * kobj)
  78. {
  79. struct device * dev = to_dev(kobj);
  80. if (dev->release)
  81. dev->release(dev);
  82. else {
  83. printk(KERN_ERR "Device '%s' does not have a release() function, "
  84. "it is broken and must be fixed.\n",
  85. dev->bus_id);
  86. WARN_ON(1);
  87. }
  88. }
  89. static struct kobj_type ktype_device = {
  90. .release = device_release,
  91. .sysfs_ops = &dev_sysfs_ops,
  92. };
  93. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  94. {
  95. struct kobj_type *ktype = get_ktype(kobj);
  96. if (ktype == &ktype_device) {
  97. struct device *dev = to_dev(kobj);
  98. if (dev->bus)
  99. return 1;
  100. if (dev->class)
  101. return 1;
  102. }
  103. return 0;
  104. }
  105. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  106. {
  107. struct device *dev = to_dev(kobj);
  108. if (dev->bus)
  109. return dev->bus->name;
  110. if (dev->class)
  111. return dev->class->name;
  112. return NULL;
  113. }
  114. static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
  115. int num_envp, char *buffer, int buffer_size)
  116. {
  117. struct device *dev = to_dev(kobj);
  118. int i = 0;
  119. int length = 0;
  120. int retval = 0;
  121. /* add the major/minor if present */
  122. if (MAJOR(dev->devt)) {
  123. add_uevent_var(envp, num_envp, &i,
  124. buffer, buffer_size, &length,
  125. "MAJOR=%u", MAJOR(dev->devt));
  126. add_uevent_var(envp, num_envp, &i,
  127. buffer, buffer_size, &length,
  128. "MINOR=%u", MINOR(dev->devt));
  129. }
  130. /* add bus name (same as SUBSYSTEM, deprecated) */
  131. if (dev->bus)
  132. add_uevent_var(envp, num_envp, &i,
  133. buffer, buffer_size, &length,
  134. "PHYSDEVBUS=%s", dev->bus->name);
  135. /* add driver name (PHYSDEV* values are deprecated)*/
  136. if (dev->driver) {
  137. add_uevent_var(envp, num_envp, &i,
  138. buffer, buffer_size, &length,
  139. "DRIVER=%s", dev->driver->name);
  140. add_uevent_var(envp, num_envp, &i,
  141. buffer, buffer_size, &length,
  142. "PHYSDEVDRIVER=%s", dev->driver->name);
  143. }
  144. /* terminate, set to next free slot, shrink available space */
  145. envp[i] = NULL;
  146. envp = &envp[i];
  147. num_envp -= i;
  148. buffer = &buffer[length];
  149. buffer_size -= length;
  150. if (dev->bus && dev->bus->uevent) {
  151. /* have the bus specific function add its stuff */
  152. retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
  153. if (retval) {
  154. pr_debug ("%s - uevent() returned %d\n",
  155. __FUNCTION__, retval);
  156. }
  157. }
  158. return retval;
  159. }
  160. static struct kset_uevent_ops device_uevent_ops = {
  161. .filter = dev_uevent_filter,
  162. .name = dev_uevent_name,
  163. .uevent = dev_uevent,
  164. };
  165. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  166. const char *buf, size_t count)
  167. {
  168. kobject_uevent(&dev->kobj, KOBJ_ADD);
  169. return count;
  170. }
  171. static int device_add_groups(struct device *dev)
  172. {
  173. int i;
  174. int error = 0;
  175. if (dev->groups) {
  176. for (i = 0; dev->groups[i]; i++) {
  177. error = sysfs_create_group(&dev->kobj, dev->groups[i]);
  178. if (error) {
  179. while (--i >= 0)
  180. sysfs_remove_group(&dev->kobj, dev->groups[i]);
  181. goto out;
  182. }
  183. }
  184. }
  185. out:
  186. return error;
  187. }
  188. static void device_remove_groups(struct device *dev)
  189. {
  190. int i;
  191. if (dev->groups) {
  192. for (i = 0; dev->groups[i]; i++) {
  193. sysfs_remove_group(&dev->kobj, dev->groups[i]);
  194. }
  195. }
  196. }
  197. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  198. char *buf)
  199. {
  200. return print_dev_t(buf, dev->devt);
  201. }
  202. /*
  203. * devices_subsys - structure to be registered with kobject core.
  204. */
  205. decl_subsys(devices, &ktype_device, &device_uevent_ops);
  206. /**
  207. * device_create_file - create sysfs attribute file for device.
  208. * @dev: device.
  209. * @attr: device attribute descriptor.
  210. */
  211. int device_create_file(struct device * dev, struct device_attribute * attr)
  212. {
  213. int error = 0;
  214. if (get_device(dev)) {
  215. error = sysfs_create_file(&dev->kobj, &attr->attr);
  216. put_device(dev);
  217. }
  218. return error;
  219. }
  220. /**
  221. * device_remove_file - remove sysfs attribute file.
  222. * @dev: device.
  223. * @attr: device attribute descriptor.
  224. */
  225. void device_remove_file(struct device * dev, struct device_attribute * attr)
  226. {
  227. if (get_device(dev)) {
  228. sysfs_remove_file(&dev->kobj, &attr->attr);
  229. put_device(dev);
  230. }
  231. }
  232. static void klist_children_get(struct klist_node *n)
  233. {
  234. struct device *dev = container_of(n, struct device, knode_parent);
  235. get_device(dev);
  236. }
  237. static void klist_children_put(struct klist_node *n)
  238. {
  239. struct device *dev = container_of(n, struct device, knode_parent);
  240. put_device(dev);
  241. }
  242. /**
  243. * device_initialize - init device structure.
  244. * @dev: device.
  245. *
  246. * This prepares the device for use by other layers,
  247. * including adding it to the device hierarchy.
  248. * It is the first half of device_register(), if called by
  249. * that, though it can also be called separately, so one
  250. * may use @dev's fields (e.g. the refcount).
  251. */
  252. void device_initialize(struct device *dev)
  253. {
  254. kobj_set_kset_s(dev, devices_subsys);
  255. kobject_init(&dev->kobj);
  256. klist_init(&dev->klist_children, klist_children_get,
  257. klist_children_put);
  258. INIT_LIST_HEAD(&dev->dma_pools);
  259. INIT_LIST_HEAD(&dev->node);
  260. init_MUTEX(&dev->sem);
  261. device_init_wakeup(dev, 0);
  262. }
  263. /**
  264. * device_add - add device to device hierarchy.
  265. * @dev: device.
  266. *
  267. * This is part 2 of device_register(), though may be called
  268. * separately _iff_ device_initialize() has been called separately.
  269. *
  270. * This adds it to the kobject hierarchy via kobject_add(), adds it
  271. * to the global and sibling lists for the device, then
  272. * adds it to the other relevant subsystems of the driver model.
  273. */
  274. int device_add(struct device *dev)
  275. {
  276. struct device *parent = NULL;
  277. char *class_name = NULL;
  278. int error = -EINVAL;
  279. dev = get_device(dev);
  280. if (!dev || !strlen(dev->bus_id))
  281. goto Error;
  282. parent = get_device(dev->parent);
  283. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  284. /* first, register with generic layer. */
  285. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  286. if (parent)
  287. dev->kobj.parent = &parent->kobj;
  288. if ((error = kobject_add(&dev->kobj)))
  289. goto Error;
  290. dev->uevent_attr.attr.name = "uevent";
  291. dev->uevent_attr.attr.mode = S_IWUSR;
  292. if (dev->driver)
  293. dev->uevent_attr.attr.owner = dev->driver->owner;
  294. dev->uevent_attr.store = store_uevent;
  295. device_create_file(dev, &dev->uevent_attr);
  296. if (MAJOR(dev->devt)) {
  297. struct device_attribute *attr;
  298. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  299. if (!attr) {
  300. error = -ENOMEM;
  301. goto PMError;
  302. }
  303. attr->attr.name = "dev";
  304. attr->attr.mode = S_IRUGO;
  305. if (dev->driver)
  306. attr->attr.owner = dev->driver->owner;
  307. attr->show = show_dev;
  308. error = device_create_file(dev, attr);
  309. if (error) {
  310. kfree(attr);
  311. goto attrError;
  312. }
  313. dev->devt_attr = attr;
  314. }
  315. if (dev->class) {
  316. sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
  317. "subsystem");
  318. sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
  319. dev->bus_id);
  320. if (parent) {
  321. sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
  322. class_name = make_class_name(dev->class->name, &dev->kobj);
  323. sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
  324. }
  325. }
  326. if ((error = device_add_groups(dev)))
  327. goto GroupError;
  328. if ((error = device_pm_add(dev)))
  329. goto PMError;
  330. if ((error = bus_add_device(dev)))
  331. goto BusError;
  332. kobject_uevent(&dev->kobj, KOBJ_ADD);
  333. bus_attach_device(dev);
  334. if (parent)
  335. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  336. if (dev->class) {
  337. /* tie the class to the device */
  338. down(&dev->class->sem);
  339. list_add_tail(&dev->node, &dev->class->devices);
  340. up(&dev->class->sem);
  341. }
  342. /* notify platform of device entry */
  343. if (platform_notify)
  344. platform_notify(dev);
  345. Done:
  346. kfree(class_name);
  347. put_device(dev);
  348. return error;
  349. BusError:
  350. device_pm_remove(dev);
  351. PMError:
  352. device_remove_groups(dev);
  353. GroupError:
  354. if (dev->devt_attr) {
  355. device_remove_file(dev, dev->devt_attr);
  356. kfree(dev->devt_attr);
  357. }
  358. attrError:
  359. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  360. kobject_del(&dev->kobj);
  361. Error:
  362. if (parent)
  363. put_device(parent);
  364. goto Done;
  365. }
  366. /**
  367. * device_register - register a device with the system.
  368. * @dev: pointer to the device structure
  369. *
  370. * This happens in two clean steps - initialize the device
  371. * and add it to the system. The two steps can be called
  372. * separately, but this is the easiest and most common.
  373. * I.e. you should only call the two helpers separately if
  374. * have a clearly defined need to use and refcount the device
  375. * before it is added to the hierarchy.
  376. */
  377. int device_register(struct device *dev)
  378. {
  379. device_initialize(dev);
  380. return device_add(dev);
  381. }
  382. /**
  383. * get_device - increment reference count for device.
  384. * @dev: device.
  385. *
  386. * This simply forwards the call to kobject_get(), though
  387. * we do take care to provide for the case that we get a NULL
  388. * pointer passed in.
  389. */
  390. struct device * get_device(struct device * dev)
  391. {
  392. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  393. }
  394. /**
  395. * put_device - decrement reference count.
  396. * @dev: device in question.
  397. */
  398. void put_device(struct device * dev)
  399. {
  400. if (dev)
  401. kobject_put(&dev->kobj);
  402. }
  403. /**
  404. * device_del - delete device from system.
  405. * @dev: device.
  406. *
  407. * This is the first part of the device unregistration
  408. * sequence. This removes the device from the lists we control
  409. * from here, has it removed from the other driver model
  410. * subsystems it was added to in device_add(), and removes it
  411. * from the kobject hierarchy.
  412. *
  413. * NOTE: this should be called manually _iff_ device_add() was
  414. * also called manually.
  415. */
  416. void device_del(struct device * dev)
  417. {
  418. struct device * parent = dev->parent;
  419. char *class_name = NULL;
  420. if (parent)
  421. klist_del(&dev->knode_parent);
  422. if (dev->devt_attr)
  423. device_remove_file(dev, dev->devt_attr);
  424. if (dev->class) {
  425. sysfs_remove_link(&dev->kobj, "subsystem");
  426. sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
  427. class_name = make_class_name(dev->class->name, &dev->kobj);
  428. if (parent) {
  429. sysfs_remove_link(&dev->kobj, "device");
  430. sysfs_remove_link(&dev->parent->kobj, class_name);
  431. }
  432. kfree(class_name);
  433. down(&dev->class->sem);
  434. list_del_init(&dev->node);
  435. up(&dev->class->sem);
  436. }
  437. device_remove_file(dev, &dev->uevent_attr);
  438. device_remove_groups(dev);
  439. /* Notify the platform of the removal, in case they
  440. * need to do anything...
  441. */
  442. if (platform_notify_remove)
  443. platform_notify_remove(dev);
  444. bus_remove_device(dev);
  445. device_pm_remove(dev);
  446. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  447. kobject_del(&dev->kobj);
  448. if (parent)
  449. put_device(parent);
  450. }
  451. /**
  452. * device_unregister - unregister device from system.
  453. * @dev: device going away.
  454. *
  455. * We do this in two parts, like we do device_register(). First,
  456. * we remove it from all the subsystems with device_del(), then
  457. * we decrement the reference count via put_device(). If that
  458. * is the final reference count, the device will be cleaned up
  459. * via device_release() above. Otherwise, the structure will
  460. * stick around until the final reference to the device is dropped.
  461. */
  462. void device_unregister(struct device * dev)
  463. {
  464. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  465. device_del(dev);
  466. put_device(dev);
  467. }
  468. static struct device * next_device(struct klist_iter * i)
  469. {
  470. struct klist_node * n = klist_next(i);
  471. return n ? container_of(n, struct device, knode_parent) : NULL;
  472. }
  473. /**
  474. * device_for_each_child - device child iterator.
  475. * @parent: parent struct device.
  476. * @data: data for the callback.
  477. * @fn: function to be called for each device.
  478. *
  479. * Iterate over @parent's child devices, and call @fn for each,
  480. * passing it @data.
  481. *
  482. * We check the return of @fn each time. If it returns anything
  483. * other than 0, we break out and return that value.
  484. */
  485. int device_for_each_child(struct device * parent, void * data,
  486. int (*fn)(struct device *, void *))
  487. {
  488. struct klist_iter i;
  489. struct device * child;
  490. int error = 0;
  491. klist_iter_init(&parent->klist_children, &i);
  492. while ((child = next_device(&i)) && !error)
  493. error = fn(child, data);
  494. klist_iter_exit(&i);
  495. return error;
  496. }
  497. int __init devices_init(void)
  498. {
  499. return subsystem_register(&devices_subsys);
  500. }
  501. EXPORT_SYMBOL_GPL(device_for_each_child);
  502. EXPORT_SYMBOL_GPL(device_initialize);
  503. EXPORT_SYMBOL_GPL(device_add);
  504. EXPORT_SYMBOL_GPL(device_register);
  505. EXPORT_SYMBOL_GPL(device_del);
  506. EXPORT_SYMBOL_GPL(device_unregister);
  507. EXPORT_SYMBOL_GPL(get_device);
  508. EXPORT_SYMBOL_GPL(put_device);
  509. EXPORT_SYMBOL_GPL(device_create_file);
  510. EXPORT_SYMBOL_GPL(device_remove_file);
  511. static void device_create_release(struct device *dev)
  512. {
  513. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  514. kfree(dev);
  515. }
  516. /**
  517. * device_create - creates a device and registers it with sysfs
  518. * @class: pointer to the struct class that this device should be registered to
  519. * @parent: pointer to the parent struct device of this new device, if any
  520. * @devt: the dev_t for the char device to be added
  521. * @fmt: string for the device's name
  522. *
  523. * This function can be used by char device classes. A struct device
  524. * will be created in sysfs, registered to the specified class.
  525. *
  526. * A "dev" file will be created, showing the dev_t for the device, if
  527. * the dev_t is not 0,0.
  528. * If a pointer to a parent struct device is passed in, the newly created
  529. * struct device will be a child of that device in sysfs.
  530. * The pointer to the struct device will be returned from the call.
  531. * Any further sysfs files that might be required can be created using this
  532. * pointer.
  533. *
  534. * Note: the struct class passed to this function must have previously
  535. * been created with a call to class_create().
  536. */
  537. struct device *device_create(struct class *class, struct device *parent,
  538. dev_t devt, const char *fmt, ...)
  539. {
  540. va_list args;
  541. struct device *dev = NULL;
  542. int retval = -ENODEV;
  543. if (class == NULL || IS_ERR(class))
  544. goto error;
  545. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  546. if (!dev) {
  547. retval = -ENOMEM;
  548. goto error;
  549. }
  550. dev->devt = devt;
  551. dev->class = class;
  552. dev->parent = parent;
  553. dev->release = device_create_release;
  554. va_start(args, fmt);
  555. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  556. va_end(args);
  557. retval = device_register(dev);
  558. if (retval)
  559. goto error;
  560. return dev;
  561. error:
  562. kfree(dev);
  563. return ERR_PTR(retval);
  564. }
  565. EXPORT_SYMBOL_GPL(device_create);
  566. /**
  567. * device_destroy - removes a device that was created with device_create()
  568. * @class: pointer to the struct class that this device was registered with
  569. * @devt: the dev_t of the device that was previously registered
  570. *
  571. * This call unregisters and cleans up a device that was created with a
  572. * call to device_create().
  573. */
  574. void device_destroy(struct class *class, dev_t devt)
  575. {
  576. struct device *dev = NULL;
  577. struct device *dev_tmp;
  578. down(&class->sem);
  579. list_for_each_entry(dev_tmp, &class->devices, node) {
  580. if (dev_tmp->devt == devt) {
  581. dev = dev_tmp;
  582. break;
  583. }
  584. }
  585. up(&class->sem);
  586. if (dev)
  587. device_unregister(dev);
  588. }
  589. EXPORT_SYMBOL_GPL(device_destroy);