core.c 30 KB

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