core.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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. memset(envp, 0, sizeof(envp));
  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. if (dev->parent) {
  584. #ifdef CONFIG_SYSFS_DEPRECATED
  585. {
  586. struct device *parent = dev->parent;
  587. char *class_name;
  588. /*
  589. * In old sysfs stacked class devices had 'device'
  590. * link pointing to real device instead of parent
  591. */
  592. while (parent->class && !parent->bus && parent->parent)
  593. parent = parent->parent;
  594. error = sysfs_create_link(&dev->kobj,
  595. &parent->kobj,
  596. "device");
  597. if (error)
  598. goto out_busid;
  599. class_name = make_class_name(dev->class->name,
  600. &dev->kobj);
  601. if (class_name)
  602. error = sysfs_create_link(&dev->parent->kobj,
  603. &dev->kobj, class_name);
  604. kfree(class_name);
  605. if (error)
  606. goto out_device;
  607. }
  608. #else
  609. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  610. "device");
  611. if (error)
  612. goto out_busid;
  613. #endif
  614. }
  615. return 0;
  616. #ifdef CONFIG_SYSFS_DEPRECATED
  617. out_device:
  618. if (dev->parent)
  619. sysfs_remove_link(&dev->kobj, "device");
  620. #endif
  621. out_busid:
  622. if (dev->kobj.parent != &dev->class->subsys.kobj)
  623. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  624. out_subsys:
  625. sysfs_remove_link(&dev->kobj, "subsystem");
  626. out:
  627. return error;
  628. }
  629. static void device_remove_class_symlinks(struct device *dev)
  630. {
  631. if (!dev->class)
  632. return;
  633. if (dev->parent) {
  634. #ifdef CONFIG_SYSFS_DEPRECATED
  635. char *class_name;
  636. class_name = make_class_name(dev->class->name, &dev->kobj);
  637. if (class_name) {
  638. sysfs_remove_link(&dev->parent->kobj, class_name);
  639. kfree(class_name);
  640. }
  641. #endif
  642. sysfs_remove_link(&dev->kobj, "device");
  643. }
  644. if (dev->kobj.parent != &dev->class->subsys.kobj)
  645. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  646. sysfs_remove_link(&dev->kobj, "subsystem");
  647. }
  648. /**
  649. * device_add - add device to device hierarchy.
  650. * @dev: device.
  651. *
  652. * This is part 2 of device_register(), though may be called
  653. * separately _iff_ device_initialize() has been called separately.
  654. *
  655. * This adds it to the kobject hierarchy via kobject_add(), adds it
  656. * to the global and sibling lists for the device, then
  657. * adds it to the other relevant subsystems of the driver model.
  658. */
  659. int device_add(struct device *dev)
  660. {
  661. struct device *parent = NULL;
  662. struct class_interface *class_intf;
  663. int error = -EINVAL;
  664. dev = get_device(dev);
  665. if (!dev || !strlen(dev->bus_id))
  666. goto Error;
  667. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  668. parent = get_device(dev->parent);
  669. error = setup_parent(dev, parent);
  670. if (error)
  671. goto Error;
  672. /* first, register with generic layer. */
  673. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  674. error = kobject_add(&dev->kobj);
  675. if (error)
  676. goto Error;
  677. /* notify platform of device entry */
  678. if (platform_notify)
  679. platform_notify(dev);
  680. /* notify clients of device entry (new way) */
  681. if (dev->bus)
  682. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  683. BUS_NOTIFY_ADD_DEVICE, dev);
  684. error = device_create_file(dev, &uevent_attr);
  685. if (error)
  686. goto attrError;
  687. if (MAJOR(dev->devt)) {
  688. error = device_create_file(dev, &devt_attr);
  689. if (error)
  690. goto ueventattrError;
  691. }
  692. error = device_add_class_symlinks(dev);
  693. if (error)
  694. goto SymlinkError;
  695. error = device_add_attrs(dev);
  696. if (error)
  697. goto AttrsError;
  698. error = device_pm_add(dev);
  699. if (error)
  700. goto PMError;
  701. error = bus_add_device(dev);
  702. if (error)
  703. goto BusError;
  704. kobject_uevent(&dev->kobj, KOBJ_ADD);
  705. bus_attach_device(dev);
  706. if (parent)
  707. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  708. if (dev->class) {
  709. down(&dev->class->sem);
  710. /* tie the class to the device */
  711. list_add_tail(&dev->node, &dev->class->devices);
  712. /* notify any interfaces that the device is here */
  713. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  714. if (class_intf->add_dev)
  715. class_intf->add_dev(dev, class_intf);
  716. up(&dev->class->sem);
  717. }
  718. Done:
  719. put_device(dev);
  720. return error;
  721. BusError:
  722. device_pm_remove(dev);
  723. PMError:
  724. if (dev->bus)
  725. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  726. BUS_NOTIFY_DEL_DEVICE, dev);
  727. device_remove_attrs(dev);
  728. AttrsError:
  729. device_remove_class_symlinks(dev);
  730. SymlinkError:
  731. if (MAJOR(dev->devt))
  732. device_remove_file(dev, &devt_attr);
  733. if (dev->class) {
  734. sysfs_remove_link(&dev->kobj, "subsystem");
  735. /* If this is not a "fake" compatible device, remove the
  736. * symlink from the class to the device. */
  737. if (dev->kobj.parent != &dev->class->subsys.kobj)
  738. sysfs_remove_link(&dev->class->subsys.kobj,
  739. dev->bus_id);
  740. if (parent) {
  741. #ifdef CONFIG_SYSFS_DEPRECATED
  742. char *class_name = make_class_name(dev->class->name,
  743. &dev->kobj);
  744. if (class_name)
  745. sysfs_remove_link(&dev->parent->kobj,
  746. class_name);
  747. kfree(class_name);
  748. #endif
  749. sysfs_remove_link(&dev->kobj, "device");
  750. }
  751. }
  752. ueventattrError:
  753. device_remove_file(dev, &uevent_attr);
  754. attrError:
  755. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  756. kobject_del(&dev->kobj);
  757. Error:
  758. if (parent)
  759. put_device(parent);
  760. goto Done;
  761. }
  762. /**
  763. * device_register - register a device with the system.
  764. * @dev: pointer to the device structure
  765. *
  766. * This happens in two clean steps - initialize the device
  767. * and add it to the system. The two steps can be called
  768. * separately, but this is the easiest and most common.
  769. * I.e. you should only call the two helpers separately if
  770. * have a clearly defined need to use and refcount the device
  771. * before it is added to the hierarchy.
  772. */
  773. int device_register(struct device *dev)
  774. {
  775. device_initialize(dev);
  776. return device_add(dev);
  777. }
  778. /**
  779. * get_device - increment reference count for device.
  780. * @dev: device.
  781. *
  782. * This simply forwards the call to kobject_get(), though
  783. * we do take care to provide for the case that we get a NULL
  784. * pointer passed in.
  785. */
  786. struct device * get_device(struct device * dev)
  787. {
  788. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  789. }
  790. /**
  791. * put_device - decrement reference count.
  792. * @dev: device in question.
  793. */
  794. void put_device(struct device * dev)
  795. {
  796. if (dev)
  797. kobject_put(&dev->kobj);
  798. }
  799. /**
  800. * device_del - delete device from system.
  801. * @dev: device.
  802. *
  803. * This is the first part of the device unregistration
  804. * sequence. This removes the device from the lists we control
  805. * from here, has it removed from the other driver model
  806. * subsystems it was added to in device_add(), and removes it
  807. * from the kobject hierarchy.
  808. *
  809. * NOTE: this should be called manually _iff_ device_add() was
  810. * also called manually.
  811. */
  812. void device_del(struct device * dev)
  813. {
  814. struct device * parent = dev->parent;
  815. struct class_interface *class_intf;
  816. if (parent)
  817. klist_del(&dev->knode_parent);
  818. if (MAJOR(dev->devt))
  819. device_remove_file(dev, &devt_attr);
  820. if (dev->class) {
  821. sysfs_remove_link(&dev->kobj, "subsystem");
  822. /* If this is not a "fake" compatible device, remove the
  823. * symlink from the class to the device. */
  824. if (dev->kobj.parent != &dev->class->subsys.kobj)
  825. sysfs_remove_link(&dev->class->subsys.kobj,
  826. dev->bus_id);
  827. if (parent) {
  828. #ifdef CONFIG_SYSFS_DEPRECATED
  829. char *class_name = make_class_name(dev->class->name,
  830. &dev->kobj);
  831. if (class_name)
  832. sysfs_remove_link(&dev->parent->kobj,
  833. class_name);
  834. kfree(class_name);
  835. #endif
  836. sysfs_remove_link(&dev->kobj, "device");
  837. }
  838. down(&dev->class->sem);
  839. /* notify any interfaces that the device is now gone */
  840. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  841. if (class_intf->remove_dev)
  842. class_intf->remove_dev(dev, class_intf);
  843. /* remove the device from the class list */
  844. list_del_init(&dev->node);
  845. up(&dev->class->sem);
  846. /* If we live in a parent class-directory, unreference it */
  847. if (dev->kobj.parent->kset == &dev->class->class_dirs) {
  848. struct device *d;
  849. int other = 0;
  850. /*
  851. * if we are the last child of our class, delete
  852. * our class-directory at this parent
  853. */
  854. down(&dev->class->sem);
  855. list_for_each_entry(d, &dev->class->devices, node) {
  856. if (d == dev)
  857. continue;
  858. if (d->kobj.parent == dev->kobj.parent) {
  859. other = 1;
  860. break;
  861. }
  862. }
  863. if (!other)
  864. kobject_del(dev->kobj.parent);
  865. kobject_put(dev->kobj.parent);
  866. up(&dev->class->sem);
  867. }
  868. }
  869. device_remove_file(dev, &uevent_attr);
  870. device_remove_attrs(dev);
  871. bus_remove_device(dev);
  872. /*
  873. * Some platform devices are driven without driver attached
  874. * and managed resources may have been acquired. Make sure
  875. * all resources are released.
  876. */
  877. devres_release_all(dev);
  878. /* Notify the platform of the removal, in case they
  879. * need to do anything...
  880. */
  881. if (platform_notify_remove)
  882. platform_notify_remove(dev);
  883. if (dev->bus)
  884. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  885. BUS_NOTIFY_DEL_DEVICE, dev);
  886. device_pm_remove(dev);
  887. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  888. kobject_del(&dev->kobj);
  889. if (parent)
  890. put_device(parent);
  891. }
  892. /**
  893. * device_unregister - unregister device from system.
  894. * @dev: device going away.
  895. *
  896. * We do this in two parts, like we do device_register(). First,
  897. * we remove it from all the subsystems with device_del(), then
  898. * we decrement the reference count via put_device(). If that
  899. * is the final reference count, the device will be cleaned up
  900. * via device_release() above. Otherwise, the structure will
  901. * stick around until the final reference to the device is dropped.
  902. */
  903. void device_unregister(struct device * dev)
  904. {
  905. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  906. device_del(dev);
  907. put_device(dev);
  908. }
  909. static struct device * next_device(struct klist_iter * i)
  910. {
  911. struct klist_node * n = klist_next(i);
  912. return n ? container_of(n, struct device, knode_parent) : NULL;
  913. }
  914. /**
  915. * device_for_each_child - device child iterator.
  916. * @parent: parent struct device.
  917. * @data: data for the callback.
  918. * @fn: function to be called for each device.
  919. *
  920. * Iterate over @parent's child devices, and call @fn for each,
  921. * passing it @data.
  922. *
  923. * We check the return of @fn each time. If it returns anything
  924. * other than 0, we break out and return that value.
  925. */
  926. int device_for_each_child(struct device * parent, void * data,
  927. int (*fn)(struct device *, void *))
  928. {
  929. struct klist_iter i;
  930. struct device * child;
  931. int error = 0;
  932. klist_iter_init(&parent->klist_children, &i);
  933. while ((child = next_device(&i)) && !error)
  934. error = fn(child, data);
  935. klist_iter_exit(&i);
  936. return error;
  937. }
  938. /**
  939. * device_find_child - device iterator for locating a particular device.
  940. * @parent: parent struct device
  941. * @data: Data to pass to match function
  942. * @match: Callback function to check device
  943. *
  944. * This is similar to the device_for_each_child() function above, but it
  945. * returns a reference to a device that is 'found' for later use, as
  946. * determined by the @match callback.
  947. *
  948. * The callback should return 0 if the device doesn't match and non-zero
  949. * if it does. If the callback returns non-zero and a reference to the
  950. * current device can be obtained, this function will return to the caller
  951. * and not iterate over any more devices.
  952. */
  953. struct device * device_find_child(struct device *parent, void *data,
  954. int (*match)(struct device *, void *))
  955. {
  956. struct klist_iter i;
  957. struct device *child;
  958. if (!parent)
  959. return NULL;
  960. klist_iter_init(&parent->klist_children, &i);
  961. while ((child = next_device(&i)))
  962. if (match(child, data) && get_device(child))
  963. break;
  964. klist_iter_exit(&i);
  965. return child;
  966. }
  967. int __init devices_init(void)
  968. {
  969. return subsystem_register(&devices_subsys);
  970. }
  971. EXPORT_SYMBOL_GPL(device_for_each_child);
  972. EXPORT_SYMBOL_GPL(device_find_child);
  973. EXPORT_SYMBOL_GPL(device_initialize);
  974. EXPORT_SYMBOL_GPL(device_add);
  975. EXPORT_SYMBOL_GPL(device_register);
  976. EXPORT_SYMBOL_GPL(device_del);
  977. EXPORT_SYMBOL_GPL(device_unregister);
  978. EXPORT_SYMBOL_GPL(get_device);
  979. EXPORT_SYMBOL_GPL(put_device);
  980. EXPORT_SYMBOL_GPL(device_create_file);
  981. EXPORT_SYMBOL_GPL(device_remove_file);
  982. static void device_create_release(struct device *dev)
  983. {
  984. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  985. kfree(dev);
  986. }
  987. /**
  988. * device_create - creates a device and registers it with sysfs
  989. * @class: pointer to the struct class that this device should be registered to
  990. * @parent: pointer to the parent struct device of this new device, if any
  991. * @devt: the dev_t for the char device to be added
  992. * @fmt: string for the device's name
  993. *
  994. * This function can be used by char device classes. A struct device
  995. * will be created in sysfs, registered to the specified class.
  996. *
  997. * A "dev" file will be created, showing the dev_t for the device, if
  998. * the dev_t is not 0,0.
  999. * If a pointer to a parent struct device is passed in, the newly created
  1000. * struct device will be a child of that device in sysfs.
  1001. * The pointer to the struct device will be returned from the call.
  1002. * Any further sysfs files that might be required can be created using this
  1003. * pointer.
  1004. *
  1005. * Note: the struct class passed to this function must have previously
  1006. * been created with a call to class_create().
  1007. */
  1008. struct device *device_create(struct class *class, struct device *parent,
  1009. dev_t devt, const char *fmt, ...)
  1010. {
  1011. va_list args;
  1012. struct device *dev = NULL;
  1013. int retval = -ENODEV;
  1014. if (class == NULL || IS_ERR(class))
  1015. goto error;
  1016. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1017. if (!dev) {
  1018. retval = -ENOMEM;
  1019. goto error;
  1020. }
  1021. dev->devt = devt;
  1022. dev->class = class;
  1023. dev->parent = parent;
  1024. dev->release = device_create_release;
  1025. va_start(args, fmt);
  1026. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  1027. va_end(args);
  1028. retval = device_register(dev);
  1029. if (retval)
  1030. goto error;
  1031. return dev;
  1032. error:
  1033. kfree(dev);
  1034. return ERR_PTR(retval);
  1035. }
  1036. EXPORT_SYMBOL_GPL(device_create);
  1037. /**
  1038. * device_destroy - removes a device that was created with device_create()
  1039. * @class: pointer to the struct class that this device was registered with
  1040. * @devt: the dev_t of the device that was previously registered
  1041. *
  1042. * This call unregisters and cleans up a device that was created with a
  1043. * call to device_create().
  1044. */
  1045. void device_destroy(struct class *class, dev_t devt)
  1046. {
  1047. struct device *dev = NULL;
  1048. struct device *dev_tmp;
  1049. down(&class->sem);
  1050. list_for_each_entry(dev_tmp, &class->devices, node) {
  1051. if (dev_tmp->devt == devt) {
  1052. dev = dev_tmp;
  1053. break;
  1054. }
  1055. }
  1056. up(&class->sem);
  1057. if (dev)
  1058. device_unregister(dev);
  1059. }
  1060. EXPORT_SYMBOL_GPL(device_destroy);
  1061. /**
  1062. * device_rename - renames a device
  1063. * @dev: the pointer to the struct device to be renamed
  1064. * @new_name: the new name of the device
  1065. */
  1066. int device_rename(struct device *dev, char *new_name)
  1067. {
  1068. char *old_class_name = NULL;
  1069. char *new_class_name = NULL;
  1070. char *old_device_name = NULL;
  1071. int error;
  1072. dev = get_device(dev);
  1073. if (!dev)
  1074. return -EINVAL;
  1075. pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
  1076. #ifdef CONFIG_SYSFS_DEPRECATED
  1077. if ((dev->class) && (dev->parent))
  1078. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1079. #endif
  1080. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1081. if (!old_device_name) {
  1082. error = -ENOMEM;
  1083. goto out;
  1084. }
  1085. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1086. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1087. error = kobject_rename(&dev->kobj, new_name);
  1088. if (error) {
  1089. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1090. goto out;
  1091. }
  1092. #ifdef CONFIG_SYSFS_DEPRECATED
  1093. if (old_class_name) {
  1094. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1095. if (new_class_name) {
  1096. error = sysfs_create_link(&dev->parent->kobj,
  1097. &dev->kobj, new_class_name);
  1098. if (error)
  1099. goto out;
  1100. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1101. }
  1102. }
  1103. #endif
  1104. if (dev->class) {
  1105. sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
  1106. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  1107. dev->bus_id);
  1108. if (error) {
  1109. /* Uh... how to unravel this if restoring can fail? */
  1110. dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
  1111. __FUNCTION__, error);
  1112. }
  1113. }
  1114. out:
  1115. put_device(dev);
  1116. kfree(new_class_name);
  1117. kfree(old_class_name);
  1118. kfree(old_device_name);
  1119. return error;
  1120. }
  1121. EXPORT_SYMBOL_GPL(device_rename);
  1122. static int device_move_class_links(struct device *dev,
  1123. struct device *old_parent,
  1124. struct device *new_parent)
  1125. {
  1126. int error = 0;
  1127. #ifdef CONFIG_SYSFS_DEPRECATED
  1128. char *class_name;
  1129. class_name = make_class_name(dev->class->name, &dev->kobj);
  1130. if (!class_name) {
  1131. error = -ENOMEM;
  1132. goto out;
  1133. }
  1134. if (old_parent) {
  1135. sysfs_remove_link(&dev->kobj, "device");
  1136. sysfs_remove_link(&old_parent->kobj, class_name);
  1137. }
  1138. if (new_parent) {
  1139. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1140. "device");
  1141. if (error)
  1142. goto out;
  1143. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1144. class_name);
  1145. if (error)
  1146. sysfs_remove_link(&dev->kobj, "device");
  1147. }
  1148. else
  1149. error = 0;
  1150. out:
  1151. kfree(class_name);
  1152. return error;
  1153. #else
  1154. if (old_parent)
  1155. sysfs_remove_link(&dev->kobj, "device");
  1156. if (new_parent)
  1157. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1158. "device");
  1159. return error;
  1160. #endif
  1161. }
  1162. /**
  1163. * device_move - moves a device to a new parent
  1164. * @dev: the pointer to the struct device to be moved
  1165. * @new_parent: the new parent of the device (can by NULL)
  1166. */
  1167. int device_move(struct device *dev, struct device *new_parent)
  1168. {
  1169. int error;
  1170. struct device *old_parent;
  1171. struct kobject *new_parent_kobj;
  1172. dev = get_device(dev);
  1173. if (!dev)
  1174. return -EINVAL;
  1175. new_parent = get_device(new_parent);
  1176. new_parent_kobj = get_device_parent (dev, new_parent);
  1177. if (IS_ERR(new_parent_kobj)) {
  1178. error = PTR_ERR(new_parent_kobj);
  1179. put_device(new_parent);
  1180. goto out;
  1181. }
  1182. pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
  1183. new_parent ? new_parent->bus_id : "<NULL>");
  1184. error = kobject_move(&dev->kobj, new_parent_kobj);
  1185. if (error) {
  1186. put_device(new_parent);
  1187. goto out;
  1188. }
  1189. old_parent = dev->parent;
  1190. dev->parent = new_parent;
  1191. if (old_parent)
  1192. klist_remove(&dev->knode_parent);
  1193. if (new_parent)
  1194. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1195. if (!dev->class)
  1196. goto out_put;
  1197. error = device_move_class_links(dev, old_parent, new_parent);
  1198. if (error) {
  1199. /* We ignore errors on cleanup since we're hosed anyway... */
  1200. device_move_class_links(dev, new_parent, old_parent);
  1201. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1202. if (new_parent)
  1203. klist_remove(&dev->knode_parent);
  1204. if (old_parent)
  1205. klist_add_tail(&dev->knode_parent,
  1206. &old_parent->klist_children);
  1207. }
  1208. put_device(new_parent);
  1209. goto out;
  1210. }
  1211. out_put:
  1212. put_device(old_parent);
  1213. out:
  1214. put_device(dev);
  1215. return error;
  1216. }
  1217. EXPORT_SYMBOL_GPL(device_move);