core.c 34 KB

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