core.c 16 KB

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