core.c 27 KB

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