core.c 16 KB

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