core.c 15 KB

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