core.c 33 KB

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