core.c 34 KB

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