core.c 33 KB

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