core.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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 <linux/notifier.h>
  20. #include <linux/genhd.h>
  21. #include <linux/kallsyms.h>
  22. #include <linux/semaphore.h>
  23. #include "base.h"
  24. #include "power/power.h"
  25. int (*platform_notify)(struct device *dev) = NULL;
  26. int (*platform_notify_remove)(struct device *dev) = NULL;
  27. #ifdef CONFIG_BLOCK
  28. static inline int device_is_not_partition(struct device *dev)
  29. {
  30. return !(dev->type == &part_type);
  31. }
  32. #else
  33. static inline int device_is_not_partition(struct device *dev)
  34. {
  35. return 1;
  36. }
  37. #endif
  38. /**
  39. * dev_driver_string - Return a device's driver name, if at all possible
  40. * @dev: struct device to get the name of
  41. *
  42. * Will return the device's driver's name if it is bound to a device. If
  43. * the device is not bound to a device, it will return the name of the bus
  44. * it is attached to. If it is not attached to a bus either, an empty
  45. * string will be returned.
  46. */
  47. const char *dev_driver_string(struct device *dev)
  48. {
  49. return dev->driver ? dev->driver->name :
  50. (dev->bus ? dev->bus->name :
  51. (dev->class ? dev->class->name : ""));
  52. }
  53. EXPORT_SYMBOL(dev_driver_string);
  54. #define to_dev(obj) container_of(obj, struct device, kobj)
  55. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  56. static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  57. char *buf)
  58. {
  59. struct device_attribute *dev_attr = to_dev_attr(attr);
  60. struct device *dev = to_dev(kobj);
  61. ssize_t ret = -EIO;
  62. if (dev_attr->show)
  63. ret = dev_attr->show(dev, dev_attr, buf);
  64. if (ret >= (ssize_t)PAGE_SIZE) {
  65. print_symbol("dev_attr_show: %s returned bad count\n",
  66. (unsigned long)dev_attr->show);
  67. }
  68. return ret;
  69. }
  70. static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  71. const char *buf, size_t count)
  72. {
  73. struct device_attribute *dev_attr = to_dev_attr(attr);
  74. struct device *dev = to_dev(kobj);
  75. ssize_t ret = -EIO;
  76. if (dev_attr->store)
  77. ret = dev_attr->store(dev, dev_attr, buf, count);
  78. return ret;
  79. }
  80. static struct sysfs_ops dev_sysfs_ops = {
  81. .show = dev_attr_show,
  82. .store = dev_attr_store,
  83. };
  84. /**
  85. * device_release - free device structure.
  86. * @kobj: device's kobject.
  87. *
  88. * This is called once the reference count for the object
  89. * reaches 0. We forward the call to the device's release
  90. * method, which should handle actually freeing the structure.
  91. */
  92. static void device_release(struct kobject *kobj)
  93. {
  94. struct device *dev = to_dev(kobj);
  95. if (dev->release)
  96. dev->release(dev);
  97. else if (dev->type && dev->type->release)
  98. dev->type->release(dev);
  99. else if (dev->class && dev->class->dev_release)
  100. dev->class->dev_release(dev);
  101. else {
  102. printk(KERN_ERR "Device '%s' does not have a release() "
  103. "function, it is broken and must be fixed.\n",
  104. dev->bus_id);
  105. WARN_ON(1);
  106. }
  107. }
  108. static struct kobj_type device_ktype = {
  109. .release = device_release,
  110. .sysfs_ops = &dev_sysfs_ops,
  111. };
  112. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  113. {
  114. struct kobj_type *ktype = get_ktype(kobj);
  115. if (ktype == &device_ktype) {
  116. struct device *dev = to_dev(kobj);
  117. if (dev->uevent_suppress)
  118. return 0;
  119. if (dev->bus)
  120. return 1;
  121. if (dev->class)
  122. return 1;
  123. }
  124. return 0;
  125. }
  126. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  127. {
  128. struct device *dev = to_dev(kobj);
  129. if (dev->bus)
  130. return dev->bus->name;
  131. if (dev->class)
  132. return dev->class->name;
  133. return NULL;
  134. }
  135. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  136. struct kobj_uevent_env *env)
  137. {
  138. struct device *dev = to_dev(kobj);
  139. int retval = 0;
  140. /* add the major/minor if present */
  141. if (MAJOR(dev->devt)) {
  142. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  143. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  144. }
  145. if (dev->type && dev->type->name)
  146. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  147. if (dev->driver)
  148. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  149. #ifdef CONFIG_SYSFS_DEPRECATED
  150. if (dev->class) {
  151. struct device *parent = dev->parent;
  152. /* find first bus device in parent chain */
  153. while (parent && !parent->bus)
  154. parent = parent->parent;
  155. if (parent && parent->bus) {
  156. const char *path;
  157. path = kobject_get_path(&parent->kobj, GFP_KERNEL);
  158. if (path) {
  159. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  160. kfree(path);
  161. }
  162. add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
  163. if (parent->driver)
  164. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  165. parent->driver->name);
  166. }
  167. } else if (dev->bus) {
  168. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  169. if (dev->driver)
  170. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  171. dev->driver->name);
  172. }
  173. #endif
  174. /* have the bus specific function add its stuff */
  175. if (dev->bus && dev->bus->uevent) {
  176. retval = dev->bus->uevent(dev, env);
  177. if (retval)
  178. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  179. dev->bus_id, __func__, retval);
  180. }
  181. /* have the class specific function add its stuff */
  182. if (dev->class && dev->class->dev_uevent) {
  183. retval = dev->class->dev_uevent(dev, env);
  184. if (retval)
  185. pr_debug("device: '%s': %s: class uevent() "
  186. "returned %d\n", dev->bus_id,
  187. __func__, retval);
  188. }
  189. /* have the device type specific fuction add its stuff */
  190. if (dev->type && dev->type->uevent) {
  191. retval = dev->type->uevent(dev, env);
  192. if (retval)
  193. pr_debug("device: '%s': %s: dev_type uevent() "
  194. "returned %d\n", dev->bus_id,
  195. __func__, retval);
  196. }
  197. return retval;
  198. }
  199. static struct kset_uevent_ops device_uevent_ops = {
  200. .filter = dev_uevent_filter,
  201. .name = dev_uevent_name,
  202. .uevent = dev_uevent,
  203. };
  204. static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  205. char *buf)
  206. {
  207. struct kobject *top_kobj;
  208. struct kset *kset;
  209. struct kobj_uevent_env *env = NULL;
  210. int i;
  211. size_t count = 0;
  212. int retval;
  213. /* search the kset, the device belongs to */
  214. top_kobj = &dev->kobj;
  215. while (!top_kobj->kset && top_kobj->parent)
  216. top_kobj = top_kobj->parent;
  217. if (!top_kobj->kset)
  218. goto out;
  219. kset = top_kobj->kset;
  220. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  221. goto out;
  222. /* respect filter */
  223. if (kset->uevent_ops && kset->uevent_ops->filter)
  224. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  225. goto out;
  226. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  227. if (!env)
  228. return -ENOMEM;
  229. /* let the kset specific function add its keys */
  230. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  231. if (retval)
  232. goto out;
  233. /* copy keys to file */
  234. for (i = 0; i < env->envp_idx; i++)
  235. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  236. out:
  237. kfree(env);
  238. return count;
  239. }
  240. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  241. const char *buf, size_t count)
  242. {
  243. enum kobject_action action;
  244. if (kobject_action_type(buf, count, &action) == 0) {
  245. kobject_uevent(&dev->kobj, action);
  246. goto out;
  247. }
  248. dev_err(dev, "uevent: unsupported action-string; this will "
  249. "be ignored in a future kernel version\n");
  250. kobject_uevent(&dev->kobj, KOBJ_ADD);
  251. out:
  252. return count;
  253. }
  254. static struct device_attribute uevent_attr =
  255. __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
  256. static int device_add_attributes(struct device *dev,
  257. struct device_attribute *attrs)
  258. {
  259. int error = 0;
  260. int i;
  261. if (attrs) {
  262. for (i = 0; attr_name(attrs[i]); i++) {
  263. error = device_create_file(dev, &attrs[i]);
  264. if (error)
  265. break;
  266. }
  267. if (error)
  268. while (--i >= 0)
  269. device_remove_file(dev, &attrs[i]);
  270. }
  271. return error;
  272. }
  273. static void device_remove_attributes(struct device *dev,
  274. struct device_attribute *attrs)
  275. {
  276. int i;
  277. if (attrs)
  278. for (i = 0; attr_name(attrs[i]); i++)
  279. device_remove_file(dev, &attrs[i]);
  280. }
  281. static int device_add_groups(struct device *dev,
  282. struct attribute_group **groups)
  283. {
  284. int error = 0;
  285. int i;
  286. if (groups) {
  287. for (i = 0; groups[i]; i++) {
  288. error = sysfs_create_group(&dev->kobj, groups[i]);
  289. if (error) {
  290. while (--i >= 0)
  291. sysfs_remove_group(&dev->kobj,
  292. groups[i]);
  293. break;
  294. }
  295. }
  296. }
  297. return error;
  298. }
  299. static void device_remove_groups(struct device *dev,
  300. struct attribute_group **groups)
  301. {
  302. int i;
  303. if (groups)
  304. for (i = 0; groups[i]; i++)
  305. sysfs_remove_group(&dev->kobj, groups[i]);
  306. }
  307. static int device_add_attrs(struct device *dev)
  308. {
  309. struct class *class = dev->class;
  310. struct device_type *type = dev->type;
  311. int error;
  312. if (class) {
  313. error = device_add_attributes(dev, class->dev_attrs);
  314. if (error)
  315. return error;
  316. }
  317. if (type) {
  318. error = device_add_groups(dev, type->groups);
  319. if (error)
  320. goto err_remove_class_attrs;
  321. }
  322. error = device_add_groups(dev, dev->groups);
  323. if (error)
  324. goto err_remove_type_groups;
  325. return 0;
  326. err_remove_type_groups:
  327. if (type)
  328. device_remove_groups(dev, type->groups);
  329. err_remove_class_attrs:
  330. if (class)
  331. device_remove_attributes(dev, class->dev_attrs);
  332. return error;
  333. }
  334. static void device_remove_attrs(struct device *dev)
  335. {
  336. struct class *class = dev->class;
  337. struct device_type *type = dev->type;
  338. device_remove_groups(dev, dev->groups);
  339. if (type)
  340. device_remove_groups(dev, type->groups);
  341. if (class)
  342. device_remove_attributes(dev, class->dev_attrs);
  343. }
  344. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  345. char *buf)
  346. {
  347. return print_dev_t(buf, dev->devt);
  348. }
  349. static struct device_attribute devt_attr =
  350. __ATTR(dev, S_IRUGO, show_dev, NULL);
  351. /* kset to create /sys/devices/ */
  352. struct kset *devices_kset;
  353. /**
  354. * device_create_file - create sysfs attribute file for device.
  355. * @dev: device.
  356. * @attr: device attribute descriptor.
  357. */
  358. int device_create_file(struct device *dev, struct device_attribute *attr)
  359. {
  360. int error = 0;
  361. if (dev)
  362. error = sysfs_create_file(&dev->kobj, &attr->attr);
  363. return error;
  364. }
  365. /**
  366. * device_remove_file - remove sysfs attribute file.
  367. * @dev: device.
  368. * @attr: device attribute descriptor.
  369. */
  370. void device_remove_file(struct device *dev, struct device_attribute *attr)
  371. {
  372. if (dev)
  373. sysfs_remove_file(&dev->kobj, &attr->attr);
  374. }
  375. /**
  376. * device_create_bin_file - create sysfs binary attribute file for device.
  377. * @dev: device.
  378. * @attr: device binary attribute descriptor.
  379. */
  380. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  381. {
  382. int error = -EINVAL;
  383. if (dev)
  384. error = sysfs_create_bin_file(&dev->kobj, attr);
  385. return error;
  386. }
  387. EXPORT_SYMBOL_GPL(device_create_bin_file);
  388. /**
  389. * device_remove_bin_file - remove sysfs binary attribute file
  390. * @dev: device.
  391. * @attr: device binary attribute descriptor.
  392. */
  393. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  394. {
  395. if (dev)
  396. sysfs_remove_bin_file(&dev->kobj, attr);
  397. }
  398. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  399. /**
  400. * device_schedule_callback_owner - helper to schedule a callback for a device
  401. * @dev: device.
  402. * @func: callback function to invoke later.
  403. * @owner: module owning the callback routine
  404. *
  405. * Attribute methods must not unregister themselves or their parent device
  406. * (which would amount to the same thing). Attempts to do so will deadlock,
  407. * since unregistration is mutually exclusive with driver callbacks.
  408. *
  409. * Instead methods can call this routine, which will attempt to allocate
  410. * and schedule a workqueue request to call back @func with @dev as its
  411. * argument in the workqueue's process context. @dev will be pinned until
  412. * @func returns.
  413. *
  414. * This routine is usually called via the inline device_schedule_callback(),
  415. * which automatically sets @owner to THIS_MODULE.
  416. *
  417. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  418. * be allocated, -ENODEV if a reference to @owner isn't available.
  419. *
  420. * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
  421. * underlying sysfs routine (since it is intended for use by attribute
  422. * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
  423. */
  424. int device_schedule_callback_owner(struct device *dev,
  425. void (*func)(struct device *), struct module *owner)
  426. {
  427. return sysfs_schedule_callback(&dev->kobj,
  428. (void (*)(void *)) func, dev, owner);
  429. }
  430. EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
  431. static void klist_children_get(struct klist_node *n)
  432. {
  433. struct device *dev = container_of(n, struct device, knode_parent);
  434. get_device(dev);
  435. }
  436. static void klist_children_put(struct klist_node *n)
  437. {
  438. struct device *dev = container_of(n, struct device, knode_parent);
  439. put_device(dev);
  440. }
  441. /**
  442. * device_initialize - init device structure.
  443. * @dev: device.
  444. *
  445. * This prepares the device for use by other layers,
  446. * including adding it to the device hierarchy.
  447. * It is the first half of device_register(), if called by
  448. * that, though it can also be called separately, so one
  449. * may use @dev's fields (e.g. the refcount).
  450. */
  451. void device_initialize(struct device *dev)
  452. {
  453. dev->kobj.kset = devices_kset;
  454. kobject_init(&dev->kobj, &device_ktype);
  455. klist_init(&dev->klist_children, klist_children_get,
  456. klist_children_put);
  457. INIT_LIST_HEAD(&dev->dma_pools);
  458. INIT_LIST_HEAD(&dev->node);
  459. init_MUTEX(&dev->sem);
  460. spin_lock_init(&dev->devres_lock);
  461. INIT_LIST_HEAD(&dev->devres_head);
  462. device_init_wakeup(dev, 0);
  463. set_dev_node(dev, -1);
  464. }
  465. #ifdef CONFIG_SYSFS_DEPRECATED
  466. static struct kobject *get_device_parent(struct device *dev,
  467. struct device *parent)
  468. {
  469. /* class devices without a parent live in /sys/class/<classname>/ */
  470. if (dev->class && (!parent || parent->class != dev->class))
  471. return &dev->class->subsys.kobj;
  472. /* all other devices keep their parent */
  473. else if (parent)
  474. return &parent->kobj;
  475. return NULL;
  476. }
  477. static inline void cleanup_device_parent(struct device *dev) {}
  478. static inline void cleanup_glue_dir(struct device *dev,
  479. struct kobject *glue_dir) {}
  480. #else
  481. static struct kobject *virtual_device_parent(struct device *dev)
  482. {
  483. static struct kobject *virtual_dir = NULL;
  484. if (!virtual_dir)
  485. virtual_dir = kobject_create_and_add("virtual",
  486. &devices_kset->kobj);
  487. return virtual_dir;
  488. }
  489. static struct kobject *get_device_parent(struct device *dev,
  490. struct device *parent)
  491. {
  492. int retval;
  493. if (dev->class) {
  494. struct kobject *kobj = NULL;
  495. struct kobject *parent_kobj;
  496. struct kobject *k;
  497. /*
  498. * If we have no parent, we live in "virtual".
  499. * Class-devices with a non class-device as parent, live
  500. * in a "glue" directory to prevent namespace collisions.
  501. */
  502. if (parent == NULL)
  503. parent_kobj = virtual_device_parent(dev);
  504. else if (parent->class)
  505. return &parent->kobj;
  506. else
  507. parent_kobj = &parent->kobj;
  508. /* find our class-directory at the parent and reference it */
  509. spin_lock(&dev->class->class_dirs.list_lock);
  510. list_for_each_entry(k, &dev->class->class_dirs.list, entry)
  511. if (k->parent == parent_kobj) {
  512. kobj = kobject_get(k);
  513. break;
  514. }
  515. spin_unlock(&dev->class->class_dirs.list_lock);
  516. if (kobj)
  517. return kobj;
  518. /* or create a new class-directory at the parent device */
  519. k = kobject_create();
  520. if (!k)
  521. return NULL;
  522. k->kset = &dev->class->class_dirs;
  523. retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
  524. if (retval < 0) {
  525. kobject_put(k);
  526. return NULL;
  527. }
  528. /* do not emit an uevent for this simple "glue" directory */
  529. return k;
  530. }
  531. if (parent)
  532. return &parent->kobj;
  533. return NULL;
  534. }
  535. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  536. {
  537. /* see if we live in a "glue" directory */
  538. if (!glue_dir || !dev->class ||
  539. glue_dir->kset != &dev->class->class_dirs)
  540. return;
  541. kobject_put(glue_dir);
  542. }
  543. static void cleanup_device_parent(struct device *dev)
  544. {
  545. cleanup_glue_dir(dev, dev->kobj.parent);
  546. }
  547. #endif
  548. static void setup_parent(struct device *dev, struct device *parent)
  549. {
  550. struct kobject *kobj;
  551. kobj = get_device_parent(dev, parent);
  552. if (kobj)
  553. dev->kobj.parent = kobj;
  554. }
  555. static int device_add_class_symlinks(struct device *dev)
  556. {
  557. int error;
  558. if (!dev->class)
  559. return 0;
  560. error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
  561. "subsystem");
  562. if (error)
  563. goto out;
  564. #ifdef CONFIG_SYSFS_DEPRECATED
  565. /* stacked class devices need a symlink in the class directory */
  566. if (dev->kobj.parent != &dev->class->subsys.kobj &&
  567. device_is_not_partition(dev)) {
  568. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  569. dev->bus_id);
  570. if (error)
  571. goto out_subsys;
  572. }
  573. if (dev->parent && device_is_not_partition(dev)) {
  574. struct device *parent = dev->parent;
  575. char *class_name;
  576. /*
  577. * stacked class devices have the 'device' link
  578. * pointing to the bus device instead of the parent
  579. */
  580. while (parent->class && !parent->bus && parent->parent)
  581. parent = parent->parent;
  582. error = sysfs_create_link(&dev->kobj,
  583. &parent->kobj,
  584. "device");
  585. if (error)
  586. goto out_busid;
  587. class_name = make_class_name(dev->class->name,
  588. &dev->kobj);
  589. if (class_name)
  590. error = sysfs_create_link(&dev->parent->kobj,
  591. &dev->kobj, class_name);
  592. kfree(class_name);
  593. if (error)
  594. goto out_device;
  595. }
  596. return 0;
  597. out_device:
  598. if (dev->parent && device_is_not_partition(dev))
  599. sysfs_remove_link(&dev->kobj, "device");
  600. out_busid:
  601. if (dev->kobj.parent != &dev->class->subsys.kobj &&
  602. device_is_not_partition(dev))
  603. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  604. #else
  605. /* link in the class directory pointing to the device */
  606. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  607. dev->bus_id);
  608. if (error)
  609. goto out_subsys;
  610. if (dev->parent && device_is_not_partition(dev)) {
  611. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  612. "device");
  613. if (error)
  614. goto out_busid;
  615. }
  616. return 0;
  617. out_busid:
  618. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  619. #endif
  620. out_subsys:
  621. sysfs_remove_link(&dev->kobj, "subsystem");
  622. out:
  623. return error;
  624. }
  625. static void device_remove_class_symlinks(struct device *dev)
  626. {
  627. if (!dev->class)
  628. return;
  629. #ifdef CONFIG_SYSFS_DEPRECATED
  630. if (dev->parent && device_is_not_partition(dev)) {
  631. char *class_name;
  632. class_name = make_class_name(dev->class->name, &dev->kobj);
  633. if (class_name) {
  634. sysfs_remove_link(&dev->parent->kobj, class_name);
  635. kfree(class_name);
  636. }
  637. sysfs_remove_link(&dev->kobj, "device");
  638. }
  639. if (dev->kobj.parent != &dev->class->subsys.kobj &&
  640. device_is_not_partition(dev))
  641. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  642. #else
  643. if (dev->parent && device_is_not_partition(dev))
  644. sysfs_remove_link(&dev->kobj, "device");
  645. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  646. #endif
  647. sysfs_remove_link(&dev->kobj, "subsystem");
  648. }
  649. /**
  650. * device_add - add device to device hierarchy.
  651. * @dev: device.
  652. *
  653. * This is part 2 of device_register(), though may be called
  654. * separately _iff_ device_initialize() has been called separately.
  655. *
  656. * This adds it to the kobject hierarchy via kobject_add(), adds it
  657. * to the global and sibling lists for the device, then
  658. * adds it to the other relevant subsystems of the driver model.
  659. */
  660. int device_add(struct device *dev)
  661. {
  662. struct device *parent = NULL;
  663. struct class_interface *class_intf;
  664. int error;
  665. dev = get_device(dev);
  666. if (!dev || !strlen(dev->bus_id)) {
  667. error = -EINVAL;
  668. goto Done;
  669. }
  670. pr_debug("device: '%s': %s\n", dev->bus_id, __func__);
  671. parent = get_device(dev->parent);
  672. setup_parent(dev, parent);
  673. /* use parent numa_node */
  674. if (parent)
  675. set_dev_node(dev, dev_to_node(parent));
  676. /* first, register with generic layer. */
  677. error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
  678. if (error)
  679. goto Error;
  680. /* notify platform of device entry */
  681. if (platform_notify)
  682. platform_notify(dev);
  683. /* notify clients of device entry (new way) */
  684. if (dev->bus)
  685. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  686. BUS_NOTIFY_ADD_DEVICE, dev);
  687. error = device_create_file(dev, &uevent_attr);
  688. if (error)
  689. goto attrError;
  690. if (MAJOR(dev->devt)) {
  691. error = device_create_file(dev, &devt_attr);
  692. if (error)
  693. goto ueventattrError;
  694. }
  695. error = device_add_class_symlinks(dev);
  696. if (error)
  697. goto SymlinkError;
  698. error = device_add_attrs(dev);
  699. if (error)
  700. goto AttrsError;
  701. error = bus_add_device(dev);
  702. if (error)
  703. goto BusError;
  704. error = device_pm_add(dev);
  705. if (error)
  706. goto PMError;
  707. kobject_uevent(&dev->kobj, KOBJ_ADD);
  708. bus_attach_device(dev);
  709. if (parent)
  710. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  711. if (dev->class) {
  712. down(&dev->class->sem);
  713. /* tie the class to the device */
  714. list_add_tail(&dev->node, &dev->class->devices);
  715. /* notify any interfaces that the device is here */
  716. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  717. if (class_intf->add_dev)
  718. class_intf->add_dev(dev, class_intf);
  719. up(&dev->class->sem);
  720. }
  721. Done:
  722. put_device(dev);
  723. return error;
  724. PMError:
  725. bus_remove_device(dev);
  726. BusError:
  727. if (dev->bus)
  728. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  729. BUS_NOTIFY_DEL_DEVICE, dev);
  730. device_remove_attrs(dev);
  731. AttrsError:
  732. device_remove_class_symlinks(dev);
  733. SymlinkError:
  734. if (MAJOR(dev->devt))
  735. device_remove_file(dev, &devt_attr);
  736. ueventattrError:
  737. device_remove_file(dev, &uevent_attr);
  738. attrError:
  739. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  740. kobject_del(&dev->kobj);
  741. Error:
  742. cleanup_device_parent(dev);
  743. if (parent)
  744. put_device(parent);
  745. goto Done;
  746. }
  747. /**
  748. * device_register - register a device with the system.
  749. * @dev: pointer to the device structure
  750. *
  751. * This happens in two clean steps - initialize the device
  752. * and add it to the system. The two steps can be called
  753. * separately, but this is the easiest and most common.
  754. * I.e. you should only call the two helpers separately if
  755. * have a clearly defined need to use and refcount the device
  756. * before it is added to the hierarchy.
  757. */
  758. int device_register(struct device *dev)
  759. {
  760. device_initialize(dev);
  761. return device_add(dev);
  762. }
  763. /**
  764. * get_device - increment reference count for device.
  765. * @dev: device.
  766. *
  767. * This simply forwards the call to kobject_get(), though
  768. * we do take care to provide for the case that we get a NULL
  769. * pointer passed in.
  770. */
  771. struct device *get_device(struct device *dev)
  772. {
  773. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  774. }
  775. /**
  776. * put_device - decrement reference count.
  777. * @dev: device in question.
  778. */
  779. void put_device(struct device *dev)
  780. {
  781. /* might_sleep(); */
  782. if (dev)
  783. kobject_put(&dev->kobj);
  784. }
  785. /**
  786. * device_del - delete device from system.
  787. * @dev: device.
  788. *
  789. * This is the first part of the device unregistration
  790. * sequence. This removes the device from the lists we control
  791. * from here, has it removed from the other driver model
  792. * subsystems it was added to in device_add(), and removes it
  793. * from the kobject hierarchy.
  794. *
  795. * NOTE: this should be called manually _iff_ device_add() was
  796. * also called manually.
  797. */
  798. void device_del(struct device *dev)
  799. {
  800. struct device *parent = dev->parent;
  801. struct class_interface *class_intf;
  802. device_pm_remove(dev);
  803. if (parent)
  804. klist_del(&dev->knode_parent);
  805. if (MAJOR(dev->devt))
  806. device_remove_file(dev, &devt_attr);
  807. if (dev->class) {
  808. device_remove_class_symlinks(dev);
  809. down(&dev->class->sem);
  810. /* notify any interfaces that the device is now gone */
  811. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  812. if (class_intf->remove_dev)
  813. class_intf->remove_dev(dev, class_intf);
  814. /* remove the device from the class list */
  815. list_del_init(&dev->node);
  816. up(&dev->class->sem);
  817. }
  818. device_remove_file(dev, &uevent_attr);
  819. device_remove_attrs(dev);
  820. bus_remove_device(dev);
  821. /*
  822. * Some platform devices are driven without driver attached
  823. * and managed resources may have been acquired. Make sure
  824. * all resources are released.
  825. */
  826. devres_release_all(dev);
  827. /* Notify the platform of the removal, in case they
  828. * need to do anything...
  829. */
  830. if (platform_notify_remove)
  831. platform_notify_remove(dev);
  832. if (dev->bus)
  833. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  834. BUS_NOTIFY_DEL_DEVICE, dev);
  835. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  836. cleanup_device_parent(dev);
  837. kobject_del(&dev->kobj);
  838. put_device(parent);
  839. }
  840. /**
  841. * device_unregister - unregister device from system.
  842. * @dev: device going away.
  843. *
  844. * We do this in two parts, like we do device_register(). First,
  845. * we remove it from all the subsystems with device_del(), then
  846. * we decrement the reference count via put_device(). If that
  847. * is the final reference count, the device will be cleaned up
  848. * via device_release() above. Otherwise, the structure will
  849. * stick around until the final reference to the device is dropped.
  850. */
  851. void device_unregister(struct device *dev)
  852. {
  853. pr_debug("device: '%s': %s\n", dev->bus_id, __func__);
  854. device_del(dev);
  855. put_device(dev);
  856. }
  857. static struct device *next_device(struct klist_iter *i)
  858. {
  859. struct klist_node *n = klist_next(i);
  860. return n ? container_of(n, struct device, knode_parent) : NULL;
  861. }
  862. /**
  863. * device_for_each_child - device child iterator.
  864. * @parent: parent struct device.
  865. * @data: data for the callback.
  866. * @fn: function to be called for each device.
  867. *
  868. * Iterate over @parent's child devices, and call @fn for each,
  869. * passing it @data.
  870. *
  871. * We check the return of @fn each time. If it returns anything
  872. * other than 0, we break out and return that value.
  873. */
  874. int device_for_each_child(struct device *parent, void *data,
  875. int (*fn)(struct device *dev, void *data))
  876. {
  877. struct klist_iter i;
  878. struct device *child;
  879. int error = 0;
  880. klist_iter_init(&parent->klist_children, &i);
  881. while ((child = next_device(&i)) && !error)
  882. error = fn(child, data);
  883. klist_iter_exit(&i);
  884. return error;
  885. }
  886. /**
  887. * device_find_child - device iterator for locating a particular device.
  888. * @parent: parent struct device
  889. * @data: Data to pass to match function
  890. * @match: Callback function to check device
  891. *
  892. * This is similar to the device_for_each_child() function above, but it
  893. * returns a reference to a device that is 'found' for later use, as
  894. * determined by the @match callback.
  895. *
  896. * The callback should return 0 if the device doesn't match and non-zero
  897. * if it does. If the callback returns non-zero and a reference to the
  898. * current device can be obtained, this function will return to the caller
  899. * and not iterate over any more devices.
  900. */
  901. struct device *device_find_child(struct device *parent, void *data,
  902. int (*match)(struct device *dev, void *data))
  903. {
  904. struct klist_iter i;
  905. struct device *child;
  906. if (!parent)
  907. return NULL;
  908. klist_iter_init(&parent->klist_children, &i);
  909. while ((child = next_device(&i)))
  910. if (match(child, data) && get_device(child))
  911. break;
  912. klist_iter_exit(&i);
  913. return child;
  914. }
  915. int __init devices_init(void)
  916. {
  917. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  918. if (!devices_kset)
  919. return -ENOMEM;
  920. return 0;
  921. }
  922. EXPORT_SYMBOL_GPL(device_for_each_child);
  923. EXPORT_SYMBOL_GPL(device_find_child);
  924. EXPORT_SYMBOL_GPL(device_initialize);
  925. EXPORT_SYMBOL_GPL(device_add);
  926. EXPORT_SYMBOL_GPL(device_register);
  927. EXPORT_SYMBOL_GPL(device_del);
  928. EXPORT_SYMBOL_GPL(device_unregister);
  929. EXPORT_SYMBOL_GPL(get_device);
  930. EXPORT_SYMBOL_GPL(put_device);
  931. EXPORT_SYMBOL_GPL(device_create_file);
  932. EXPORT_SYMBOL_GPL(device_remove_file);
  933. static void device_create_release(struct device *dev)
  934. {
  935. pr_debug("device: '%s': %s\n", dev->bus_id, __func__);
  936. kfree(dev);
  937. }
  938. /**
  939. * device_create - creates a device and registers it with sysfs
  940. * @class: pointer to the struct class that this device should be registered to
  941. * @parent: pointer to the parent struct device of this new device, if any
  942. * @devt: the dev_t for the char device to be added
  943. * @fmt: string for the device's name
  944. *
  945. * This function can be used by char device classes. A struct device
  946. * will be created in sysfs, registered to the specified class.
  947. *
  948. * A "dev" file will be created, showing the dev_t for the device, if
  949. * the dev_t is not 0,0.
  950. * If a pointer to a parent struct device is passed in, the newly created
  951. * struct device will be a child of that device in sysfs.
  952. * The pointer to the struct device will be returned from the call.
  953. * Any further sysfs files that might be required can be created using this
  954. * pointer.
  955. *
  956. * Note: the struct class passed to this function must have previously
  957. * been created with a call to class_create().
  958. */
  959. struct device *device_create(struct class *class, struct device *parent,
  960. dev_t devt, const char *fmt, ...)
  961. {
  962. va_list args;
  963. struct device *dev = NULL;
  964. int retval = -ENODEV;
  965. if (class == NULL || IS_ERR(class))
  966. goto error;
  967. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  968. if (!dev) {
  969. retval = -ENOMEM;
  970. goto error;
  971. }
  972. dev->devt = devt;
  973. dev->class = class;
  974. dev->parent = parent;
  975. dev->release = device_create_release;
  976. va_start(args, fmt);
  977. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  978. va_end(args);
  979. retval = device_register(dev);
  980. if (retval)
  981. goto error;
  982. return dev;
  983. error:
  984. kfree(dev);
  985. return ERR_PTR(retval);
  986. }
  987. EXPORT_SYMBOL_GPL(device_create);
  988. static int __match_devt(struct device *dev, void *data)
  989. {
  990. dev_t *devt = data;
  991. return dev->devt == *devt;
  992. }
  993. /**
  994. * device_destroy - removes a device that was created with device_create()
  995. * @class: pointer to the struct class that this device was registered with
  996. * @devt: the dev_t of the device that was previously registered
  997. *
  998. * This call unregisters and cleans up a device that was created with a
  999. * call to device_create().
  1000. */
  1001. void device_destroy(struct class *class, dev_t devt)
  1002. {
  1003. struct device *dev;
  1004. dev = class_find_device(class, &devt, __match_devt);
  1005. if (dev) {
  1006. put_device(dev);
  1007. device_unregister(dev);
  1008. }
  1009. }
  1010. EXPORT_SYMBOL_GPL(device_destroy);
  1011. /**
  1012. * device_rename - renames a device
  1013. * @dev: the pointer to the struct device to be renamed
  1014. * @new_name: the new name of the device
  1015. */
  1016. int device_rename(struct device *dev, char *new_name)
  1017. {
  1018. char *old_class_name = NULL;
  1019. char *new_class_name = NULL;
  1020. char *old_device_name = NULL;
  1021. int error;
  1022. dev = get_device(dev);
  1023. if (!dev)
  1024. return -EINVAL;
  1025. pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id,
  1026. __func__, new_name);
  1027. #ifdef CONFIG_SYSFS_DEPRECATED
  1028. if ((dev->class) && (dev->parent))
  1029. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1030. #endif
  1031. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1032. if (!old_device_name) {
  1033. error = -ENOMEM;
  1034. goto out;
  1035. }
  1036. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1037. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1038. error = kobject_rename(&dev->kobj, new_name);
  1039. if (error) {
  1040. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1041. goto out;
  1042. }
  1043. #ifdef CONFIG_SYSFS_DEPRECATED
  1044. if (old_class_name) {
  1045. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1046. if (new_class_name) {
  1047. error = sysfs_create_link(&dev->parent->kobj,
  1048. &dev->kobj, new_class_name);
  1049. if (error)
  1050. goto out;
  1051. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1052. }
  1053. }
  1054. #else
  1055. if (dev->class) {
  1056. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  1057. dev->bus_id);
  1058. if (error)
  1059. goto out;
  1060. sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
  1061. }
  1062. #endif
  1063. out:
  1064. put_device(dev);
  1065. kfree(new_class_name);
  1066. kfree(old_class_name);
  1067. kfree(old_device_name);
  1068. return error;
  1069. }
  1070. EXPORT_SYMBOL_GPL(device_rename);
  1071. static int device_move_class_links(struct device *dev,
  1072. struct device *old_parent,
  1073. struct device *new_parent)
  1074. {
  1075. int error = 0;
  1076. #ifdef CONFIG_SYSFS_DEPRECATED
  1077. char *class_name;
  1078. class_name = make_class_name(dev->class->name, &dev->kobj);
  1079. if (!class_name) {
  1080. error = -ENOMEM;
  1081. goto out;
  1082. }
  1083. if (old_parent) {
  1084. sysfs_remove_link(&dev->kobj, "device");
  1085. sysfs_remove_link(&old_parent->kobj, class_name);
  1086. }
  1087. if (new_parent) {
  1088. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1089. "device");
  1090. if (error)
  1091. goto out;
  1092. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1093. class_name);
  1094. if (error)
  1095. sysfs_remove_link(&dev->kobj, "device");
  1096. } else
  1097. error = 0;
  1098. out:
  1099. kfree(class_name);
  1100. return error;
  1101. #else
  1102. if (old_parent)
  1103. sysfs_remove_link(&dev->kobj, "device");
  1104. if (new_parent)
  1105. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1106. "device");
  1107. return error;
  1108. #endif
  1109. }
  1110. /**
  1111. * device_move - moves a device to a new parent
  1112. * @dev: the pointer to the struct device to be moved
  1113. * @new_parent: the new parent of the device (can by NULL)
  1114. */
  1115. int device_move(struct device *dev, struct device *new_parent)
  1116. {
  1117. int error;
  1118. struct device *old_parent;
  1119. struct kobject *new_parent_kobj;
  1120. dev = get_device(dev);
  1121. if (!dev)
  1122. return -EINVAL;
  1123. new_parent = get_device(new_parent);
  1124. new_parent_kobj = get_device_parent(dev, new_parent);
  1125. pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
  1126. __func__, new_parent ? new_parent->bus_id : "<NULL>");
  1127. error = kobject_move(&dev->kobj, new_parent_kobj);
  1128. if (error) {
  1129. cleanup_glue_dir(dev, new_parent_kobj);
  1130. put_device(new_parent);
  1131. goto out;
  1132. }
  1133. old_parent = dev->parent;
  1134. dev->parent = new_parent;
  1135. if (old_parent)
  1136. klist_remove(&dev->knode_parent);
  1137. if (new_parent) {
  1138. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1139. set_dev_node(dev, dev_to_node(new_parent));
  1140. }
  1141. if (!dev->class)
  1142. goto out_put;
  1143. error = device_move_class_links(dev, old_parent, new_parent);
  1144. if (error) {
  1145. /* We ignore errors on cleanup since we're hosed anyway... */
  1146. device_move_class_links(dev, new_parent, old_parent);
  1147. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1148. if (new_parent)
  1149. klist_remove(&dev->knode_parent);
  1150. dev->parent = old_parent;
  1151. if (old_parent) {
  1152. klist_add_tail(&dev->knode_parent,
  1153. &old_parent->klist_children);
  1154. set_dev_node(dev, dev_to_node(old_parent));
  1155. }
  1156. }
  1157. cleanup_glue_dir(dev, new_parent_kobj);
  1158. put_device(new_parent);
  1159. goto out;
  1160. }
  1161. out_put:
  1162. put_device(old_parent);
  1163. out:
  1164. put_device(dev);
  1165. return error;
  1166. }
  1167. EXPORT_SYMBOL_GPL(device_move);
  1168. /**
  1169. * device_shutdown - call ->shutdown() on each device to shutdown.
  1170. */
  1171. void device_shutdown(void)
  1172. {
  1173. struct device *dev, *devn;
  1174. list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
  1175. kobj.entry) {
  1176. if (dev->bus && dev->bus->shutdown) {
  1177. dev_dbg(dev, "shutdown\n");
  1178. dev->bus->shutdown(dev);
  1179. } else if (dev->driver && dev->driver->shutdown) {
  1180. dev_dbg(dev, "shutdown\n");
  1181. dev->driver->shutdown(dev);
  1182. }
  1183. }
  1184. }