core.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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 <linux/genhd.h>
  21. #include <linux/kallsyms.h>
  22. #include <linux/semaphore.h>
  23. #include <linux/mutex.h>
  24. #include "base.h"
  25. #include "power/power.h"
  26. int (*platform_notify)(struct device *dev) = NULL;
  27. int (*platform_notify_remove)(struct device *dev) = NULL;
  28. static struct kobject *dev_kobj;
  29. struct kobject *sysfs_dev_char_kobj;
  30. struct kobject *sysfs_dev_block_kobj;
  31. #ifdef CONFIG_BLOCK
  32. static inline int device_is_not_partition(struct device *dev)
  33. {
  34. return !(dev->type == &part_type);
  35. }
  36. #else
  37. static inline int device_is_not_partition(struct device *dev)
  38. {
  39. return 1;
  40. }
  41. #endif
  42. /**
  43. * dev_driver_string - Return a device's driver name, if at all possible
  44. * @dev: struct device to get the name of
  45. *
  46. * Will return the device's driver's name if it is bound to a device. If
  47. * the device is not bound to a device, it will return the name of the bus
  48. * it is attached to. If it is not attached to a bus either, an empty
  49. * string will be returned.
  50. */
  51. const char *dev_driver_string(const struct device *dev)
  52. {
  53. return dev->driver ? dev->driver->name :
  54. (dev->bus ? dev->bus->name :
  55. (dev->class ? dev->class->name : ""));
  56. }
  57. EXPORT_SYMBOL(dev_driver_string);
  58. #define to_dev(obj) container_of(obj, struct device, kobj)
  59. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  60. static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  61. char *buf)
  62. {
  63. struct device_attribute *dev_attr = to_dev_attr(attr);
  64. struct device *dev = to_dev(kobj);
  65. ssize_t ret = -EIO;
  66. if (dev_attr->show)
  67. ret = dev_attr->show(dev, dev_attr, buf);
  68. if (ret >= (ssize_t)PAGE_SIZE) {
  69. print_symbol("dev_attr_show: %s returned bad count\n",
  70. (unsigned long)dev_attr->show);
  71. }
  72. return ret;
  73. }
  74. static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  75. const char *buf, size_t count)
  76. {
  77. struct device_attribute *dev_attr = to_dev_attr(attr);
  78. struct device *dev = to_dev(kobj);
  79. ssize_t ret = -EIO;
  80. if (dev_attr->store)
  81. ret = dev_attr->store(dev, dev_attr, buf, count);
  82. return ret;
  83. }
  84. static struct sysfs_ops dev_sysfs_ops = {
  85. .show = dev_attr_show,
  86. .store = dev_attr_store,
  87. };
  88. /**
  89. * device_release - free device structure.
  90. * @kobj: device's kobject.
  91. *
  92. * This is called once the reference count for the object
  93. * reaches 0. We forward the call to the device's release
  94. * method, which should handle actually freeing the structure.
  95. */
  96. static void device_release(struct kobject *kobj)
  97. {
  98. struct device *dev = to_dev(kobj);
  99. struct device_private *p = dev->p;
  100. if (dev->release)
  101. dev->release(dev);
  102. else if (dev->type && dev->type->release)
  103. dev->type->release(dev);
  104. else if (dev->class && dev->class->dev_release)
  105. dev->class->dev_release(dev);
  106. else
  107. WARN(1, KERN_ERR "Device '%s' does not have a release() "
  108. "function, it is broken and must be fixed.\n",
  109. dev_name(dev));
  110. kfree(p);
  111. }
  112. static struct kobj_type device_ktype = {
  113. .release = device_release,
  114. .sysfs_ops = &dev_sysfs_ops,
  115. };
  116. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  117. {
  118. struct kobj_type *ktype = get_ktype(kobj);
  119. if (ktype == &device_ktype) {
  120. struct device *dev = to_dev(kobj);
  121. if (dev->uevent_suppress)
  122. return 0;
  123. if (dev->bus)
  124. return 1;
  125. if (dev->class)
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  131. {
  132. struct device *dev = to_dev(kobj);
  133. if (dev->bus)
  134. return dev->bus->name;
  135. if (dev->class)
  136. return dev->class->name;
  137. return NULL;
  138. }
  139. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  140. struct kobj_uevent_env *env)
  141. {
  142. struct device *dev = to_dev(kobj);
  143. int retval = 0;
  144. /* add the major/minor if present */
  145. if (MAJOR(dev->devt)) {
  146. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  147. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  148. }
  149. if (dev->type && dev->type->name)
  150. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  151. if (dev->driver)
  152. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  153. #ifdef CONFIG_SYSFS_DEPRECATED
  154. if (dev->class) {
  155. struct device *parent = dev->parent;
  156. /* find first bus device in parent chain */
  157. while (parent && !parent->bus)
  158. parent = parent->parent;
  159. if (parent && parent->bus) {
  160. const char *path;
  161. path = kobject_get_path(&parent->kobj, GFP_KERNEL);
  162. if (path) {
  163. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  164. kfree(path);
  165. }
  166. add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
  167. if (parent->driver)
  168. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  169. parent->driver->name);
  170. }
  171. } else if (dev->bus) {
  172. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  173. if (dev->driver)
  174. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  175. dev->driver->name);
  176. }
  177. #endif
  178. /* have the bus specific function add its stuff */
  179. if (dev->bus && dev->bus->uevent) {
  180. retval = dev->bus->uevent(dev, env);
  181. if (retval)
  182. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  183. dev_name(dev), __func__, retval);
  184. }
  185. /* have the class specific function add its stuff */
  186. if (dev->class && dev->class->dev_uevent) {
  187. retval = dev->class->dev_uevent(dev, env);
  188. if (retval)
  189. pr_debug("device: '%s': %s: class uevent() "
  190. "returned %d\n", dev_name(dev),
  191. __func__, retval);
  192. }
  193. /* have the device type specific fuction add its stuff */
  194. if (dev->type && dev->type->uevent) {
  195. retval = dev->type->uevent(dev, env);
  196. if (retval)
  197. pr_debug("device: '%s': %s: dev_type uevent() "
  198. "returned %d\n", dev_name(dev),
  199. __func__, retval);
  200. }
  201. return retval;
  202. }
  203. static struct kset_uevent_ops device_uevent_ops = {
  204. .filter = dev_uevent_filter,
  205. .name = dev_uevent_name,
  206. .uevent = dev_uevent,
  207. };
  208. static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  209. char *buf)
  210. {
  211. struct kobject *top_kobj;
  212. struct kset *kset;
  213. struct kobj_uevent_env *env = NULL;
  214. int i;
  215. size_t count = 0;
  216. int retval;
  217. /* search the kset, the device belongs to */
  218. top_kobj = &dev->kobj;
  219. while (!top_kobj->kset && top_kobj->parent)
  220. top_kobj = top_kobj->parent;
  221. if (!top_kobj->kset)
  222. goto out;
  223. kset = top_kobj->kset;
  224. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  225. goto out;
  226. /* respect filter */
  227. if (kset->uevent_ops && kset->uevent_ops->filter)
  228. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  229. goto out;
  230. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  231. if (!env)
  232. return -ENOMEM;
  233. /* let the kset specific function add its keys */
  234. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  235. if (retval)
  236. goto out;
  237. /* copy keys to file */
  238. for (i = 0; i < env->envp_idx; i++)
  239. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  240. out:
  241. kfree(env);
  242. return count;
  243. }
  244. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. enum kobject_action action;
  248. if (kobject_action_type(buf, count, &action) == 0) {
  249. kobject_uevent(&dev->kobj, action);
  250. goto out;
  251. }
  252. dev_err(dev, "uevent: unsupported action-string; this will "
  253. "be ignored in a future kernel version\n");
  254. kobject_uevent(&dev->kobj, KOBJ_ADD);
  255. out:
  256. return count;
  257. }
  258. static struct device_attribute uevent_attr =
  259. __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
  260. static int device_add_attributes(struct device *dev,
  261. struct device_attribute *attrs)
  262. {
  263. int error = 0;
  264. int i;
  265. if (attrs) {
  266. for (i = 0; attr_name(attrs[i]); i++) {
  267. error = device_create_file(dev, &attrs[i]);
  268. if (error)
  269. break;
  270. }
  271. if (error)
  272. while (--i >= 0)
  273. device_remove_file(dev, &attrs[i]);
  274. }
  275. return error;
  276. }
  277. static void device_remove_attributes(struct device *dev,
  278. struct device_attribute *attrs)
  279. {
  280. int i;
  281. if (attrs)
  282. for (i = 0; attr_name(attrs[i]); i++)
  283. device_remove_file(dev, &attrs[i]);
  284. }
  285. static int device_add_groups(struct device *dev,
  286. struct attribute_group **groups)
  287. {
  288. int error = 0;
  289. int i;
  290. if (groups) {
  291. for (i = 0; groups[i]; i++) {
  292. error = sysfs_create_group(&dev->kobj, groups[i]);
  293. if (error) {
  294. while (--i >= 0)
  295. sysfs_remove_group(&dev->kobj,
  296. groups[i]);
  297. break;
  298. }
  299. }
  300. }
  301. return error;
  302. }
  303. static void device_remove_groups(struct device *dev,
  304. struct attribute_group **groups)
  305. {
  306. int i;
  307. if (groups)
  308. for (i = 0; groups[i]; i++)
  309. sysfs_remove_group(&dev->kobj, groups[i]);
  310. }
  311. static int device_add_attrs(struct device *dev)
  312. {
  313. struct class *class = dev->class;
  314. struct device_type *type = dev->type;
  315. int error;
  316. if (class) {
  317. error = device_add_attributes(dev, class->dev_attrs);
  318. if (error)
  319. return error;
  320. }
  321. if (type) {
  322. error = device_add_groups(dev, type->groups);
  323. if (error)
  324. goto err_remove_class_attrs;
  325. }
  326. error = device_add_groups(dev, dev->groups);
  327. if (error)
  328. goto err_remove_type_groups;
  329. return 0;
  330. err_remove_type_groups:
  331. if (type)
  332. device_remove_groups(dev, type->groups);
  333. err_remove_class_attrs:
  334. if (class)
  335. device_remove_attributes(dev, class->dev_attrs);
  336. return error;
  337. }
  338. static void device_remove_attrs(struct device *dev)
  339. {
  340. struct class *class = dev->class;
  341. struct device_type *type = dev->type;
  342. device_remove_groups(dev, dev->groups);
  343. if (type)
  344. device_remove_groups(dev, type->groups);
  345. if (class)
  346. device_remove_attributes(dev, class->dev_attrs);
  347. }
  348. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  349. char *buf)
  350. {
  351. return print_dev_t(buf, dev->devt);
  352. }
  353. static struct device_attribute devt_attr =
  354. __ATTR(dev, S_IRUGO, show_dev, NULL);
  355. /* kset to create /sys/devices/ */
  356. struct kset *devices_kset;
  357. /**
  358. * device_create_file - create sysfs attribute file for device.
  359. * @dev: device.
  360. * @attr: device attribute descriptor.
  361. */
  362. int device_create_file(struct device *dev, struct device_attribute *attr)
  363. {
  364. int error = 0;
  365. if (dev)
  366. error = sysfs_create_file(&dev->kobj, &attr->attr);
  367. return error;
  368. }
  369. /**
  370. * device_remove_file - remove sysfs attribute file.
  371. * @dev: device.
  372. * @attr: device attribute descriptor.
  373. */
  374. void device_remove_file(struct device *dev, struct device_attribute *attr)
  375. {
  376. if (dev)
  377. sysfs_remove_file(&dev->kobj, &attr->attr);
  378. }
  379. /**
  380. * device_create_bin_file - create sysfs binary attribute file for device.
  381. * @dev: device.
  382. * @attr: device binary attribute descriptor.
  383. */
  384. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  385. {
  386. int error = -EINVAL;
  387. if (dev)
  388. error = sysfs_create_bin_file(&dev->kobj, attr);
  389. return error;
  390. }
  391. EXPORT_SYMBOL_GPL(device_create_bin_file);
  392. /**
  393. * device_remove_bin_file - remove sysfs binary attribute file
  394. * @dev: device.
  395. * @attr: device binary attribute descriptor.
  396. */
  397. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  398. {
  399. if (dev)
  400. sysfs_remove_bin_file(&dev->kobj, attr);
  401. }
  402. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  403. /**
  404. * device_schedule_callback_owner - helper to schedule a callback for a device
  405. * @dev: device.
  406. * @func: callback function to invoke later.
  407. * @owner: module owning the callback routine
  408. *
  409. * Attribute methods must not unregister themselves or their parent device
  410. * (which would amount to the same thing). Attempts to do so will deadlock,
  411. * since unregistration is mutually exclusive with driver callbacks.
  412. *
  413. * Instead methods can call this routine, which will attempt to allocate
  414. * and schedule a workqueue request to call back @func with @dev as its
  415. * argument in the workqueue's process context. @dev will be pinned until
  416. * @func returns.
  417. *
  418. * This routine is usually called via the inline device_schedule_callback(),
  419. * which automatically sets @owner to THIS_MODULE.
  420. *
  421. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  422. * be allocated, -ENODEV if a reference to @owner isn't available.
  423. *
  424. * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
  425. * underlying sysfs routine (since it is intended for use by attribute
  426. * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
  427. */
  428. int device_schedule_callback_owner(struct device *dev,
  429. void (*func)(struct device *), struct module *owner)
  430. {
  431. return sysfs_schedule_callback(&dev->kobj,
  432. (void (*)(void *)) func, dev, owner);
  433. }
  434. EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
  435. static void klist_children_get(struct klist_node *n)
  436. {
  437. struct device_private *p = to_device_private_parent(n);
  438. struct device *dev = p->device;
  439. get_device(dev);
  440. }
  441. static void klist_children_put(struct klist_node *n)
  442. {
  443. struct device_private *p = to_device_private_parent(n);
  444. struct device *dev = p->device;
  445. put_device(dev);
  446. }
  447. /**
  448. * device_initialize - init device structure.
  449. * @dev: device.
  450. *
  451. * This prepares the device for use by other layers by initializing
  452. * its fields.
  453. * It is the first half of device_register(), if called by
  454. * that function, though it can also be called separately, so one
  455. * may use @dev's fields. In particular, get_device()/put_device()
  456. * may be used for reference counting of @dev after calling this
  457. * function.
  458. *
  459. * NOTE: Use put_device() to give up your reference instead of freeing
  460. * @dev directly once you have called this function.
  461. */
  462. void device_initialize(struct device *dev)
  463. {
  464. dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
  465. if (!dev->p) {
  466. WARN_ON(1);
  467. return;
  468. }
  469. dev->p->device = dev;
  470. dev->kobj.kset = devices_kset;
  471. kobject_init(&dev->kobj, &device_ktype);
  472. klist_init(&dev->p->klist_children, klist_children_get,
  473. klist_children_put);
  474. INIT_LIST_HEAD(&dev->dma_pools);
  475. init_MUTEX(&dev->sem);
  476. spin_lock_init(&dev->devres_lock);
  477. INIT_LIST_HEAD(&dev->devres_head);
  478. device_init_wakeup(dev, 0);
  479. device_pm_init(dev);
  480. set_dev_node(dev, -1);
  481. }
  482. #ifdef CONFIG_SYSFS_DEPRECATED
  483. static struct kobject *get_device_parent(struct device *dev,
  484. struct device *parent)
  485. {
  486. /* class devices without a parent live in /sys/class/<classname>/ */
  487. if (dev->class && (!parent || parent->class != dev->class))
  488. return &dev->class->p->class_subsys.kobj;
  489. /* all other devices keep their parent */
  490. else if (parent)
  491. return &parent->kobj;
  492. return NULL;
  493. }
  494. static inline void cleanup_device_parent(struct device *dev) {}
  495. static inline void cleanup_glue_dir(struct device *dev,
  496. struct kobject *glue_dir) {}
  497. #else
  498. static struct kobject *virtual_device_parent(struct device *dev)
  499. {
  500. static struct kobject *virtual_dir = NULL;
  501. if (!virtual_dir)
  502. virtual_dir = kobject_create_and_add("virtual",
  503. &devices_kset->kobj);
  504. return virtual_dir;
  505. }
  506. static struct kobject *get_device_parent(struct device *dev,
  507. struct device *parent)
  508. {
  509. int retval;
  510. if (dev->class) {
  511. struct kobject *kobj = NULL;
  512. struct kobject *parent_kobj;
  513. struct kobject *k;
  514. /*
  515. * If we have no parent, we live in "virtual".
  516. * Class-devices with a non class-device as parent, live
  517. * in a "glue" directory to prevent namespace collisions.
  518. */
  519. if (parent == NULL)
  520. parent_kobj = virtual_device_parent(dev);
  521. else if (parent->class)
  522. return &parent->kobj;
  523. else
  524. parent_kobj = &parent->kobj;
  525. /* find our class-directory at the parent and reference it */
  526. spin_lock(&dev->class->p->class_dirs.list_lock);
  527. list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
  528. if (k->parent == parent_kobj) {
  529. kobj = kobject_get(k);
  530. break;
  531. }
  532. spin_unlock(&dev->class->p->class_dirs.list_lock);
  533. if (kobj)
  534. return kobj;
  535. /* or create a new class-directory at the parent device */
  536. k = kobject_create();
  537. if (!k)
  538. return NULL;
  539. k->kset = &dev->class->p->class_dirs;
  540. retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
  541. if (retval < 0) {
  542. kobject_put(k);
  543. return NULL;
  544. }
  545. /* do not emit an uevent for this simple "glue" directory */
  546. return k;
  547. }
  548. if (parent)
  549. return &parent->kobj;
  550. return NULL;
  551. }
  552. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  553. {
  554. /* see if we live in a "glue" directory */
  555. if (!glue_dir || !dev->class ||
  556. glue_dir->kset != &dev->class->p->class_dirs)
  557. return;
  558. kobject_put(glue_dir);
  559. }
  560. static void cleanup_device_parent(struct device *dev)
  561. {
  562. cleanup_glue_dir(dev, dev->kobj.parent);
  563. }
  564. #endif
  565. static void setup_parent(struct device *dev, struct device *parent)
  566. {
  567. struct kobject *kobj;
  568. kobj = get_device_parent(dev, parent);
  569. if (kobj)
  570. dev->kobj.parent = kobj;
  571. }
  572. static int device_add_class_symlinks(struct device *dev)
  573. {
  574. int error;
  575. if (!dev->class)
  576. return 0;
  577. error = sysfs_create_link(&dev->kobj,
  578. &dev->class->p->class_subsys.kobj,
  579. "subsystem");
  580. if (error)
  581. goto out;
  582. #ifdef CONFIG_SYSFS_DEPRECATED
  583. /* stacked class devices need a symlink in the class directory */
  584. if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
  585. device_is_not_partition(dev)) {
  586. error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
  587. &dev->kobj, dev_name(dev));
  588. if (error)
  589. goto out_subsys;
  590. }
  591. if (dev->parent && device_is_not_partition(dev)) {
  592. struct device *parent = dev->parent;
  593. char *class_name;
  594. /*
  595. * stacked class devices have the 'device' link
  596. * pointing to the bus device instead of the parent
  597. */
  598. while (parent->class && !parent->bus && parent->parent)
  599. parent = parent->parent;
  600. error = sysfs_create_link(&dev->kobj,
  601. &parent->kobj,
  602. "device");
  603. if (error)
  604. goto out_busid;
  605. class_name = make_class_name(dev->class->name,
  606. &dev->kobj);
  607. if (class_name)
  608. error = sysfs_create_link(&dev->parent->kobj,
  609. &dev->kobj, class_name);
  610. kfree(class_name);
  611. if (error)
  612. goto out_device;
  613. }
  614. return 0;
  615. out_device:
  616. if (dev->parent && device_is_not_partition(dev))
  617. sysfs_remove_link(&dev->kobj, "device");
  618. out_busid:
  619. if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
  620. device_is_not_partition(dev))
  621. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  622. dev_name(dev));
  623. #else
  624. /* link in the class directory pointing to the device */
  625. error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
  626. &dev->kobj, dev_name(dev));
  627. if (error)
  628. goto out_subsys;
  629. if (dev->parent && device_is_not_partition(dev)) {
  630. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  631. "device");
  632. if (error)
  633. goto out_busid;
  634. }
  635. return 0;
  636. out_busid:
  637. sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
  638. #endif
  639. out_subsys:
  640. sysfs_remove_link(&dev->kobj, "subsystem");
  641. out:
  642. return error;
  643. }
  644. static void device_remove_class_symlinks(struct device *dev)
  645. {
  646. if (!dev->class)
  647. return;
  648. #ifdef CONFIG_SYSFS_DEPRECATED
  649. if (dev->parent && device_is_not_partition(dev)) {
  650. char *class_name;
  651. class_name = make_class_name(dev->class->name, &dev->kobj);
  652. if (class_name) {
  653. sysfs_remove_link(&dev->parent->kobj, class_name);
  654. kfree(class_name);
  655. }
  656. sysfs_remove_link(&dev->kobj, "device");
  657. }
  658. if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
  659. device_is_not_partition(dev))
  660. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  661. dev_name(dev));
  662. #else
  663. if (dev->parent && device_is_not_partition(dev))
  664. sysfs_remove_link(&dev->kobj, "device");
  665. sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
  666. #endif
  667. sysfs_remove_link(&dev->kobj, "subsystem");
  668. }
  669. /**
  670. * dev_set_name - set a device name
  671. * @dev: device
  672. * @fmt: format string for the device's name
  673. */
  674. int dev_set_name(struct device *dev, const char *fmt, ...)
  675. {
  676. va_list vargs;
  677. va_start(vargs, fmt);
  678. vsnprintf(dev->bus_id, sizeof(dev->bus_id), fmt, vargs);
  679. va_end(vargs);
  680. return 0;
  681. }
  682. EXPORT_SYMBOL_GPL(dev_set_name);
  683. /**
  684. * device_to_dev_kobj - select a /sys/dev/ directory for the device
  685. * @dev: device
  686. *
  687. * By default we select char/ for new entries. Setting class->dev_obj
  688. * to NULL prevents an entry from being created. class->dev_kobj must
  689. * be set (or cleared) before any devices are registered to the class
  690. * otherwise device_create_sys_dev_entry() and
  691. * device_remove_sys_dev_entry() will disagree about the the presence
  692. * of the link.
  693. */
  694. static struct kobject *device_to_dev_kobj(struct device *dev)
  695. {
  696. struct kobject *kobj;
  697. if (dev->class)
  698. kobj = dev->class->dev_kobj;
  699. else
  700. kobj = sysfs_dev_char_kobj;
  701. return kobj;
  702. }
  703. static int device_create_sys_dev_entry(struct device *dev)
  704. {
  705. struct kobject *kobj = device_to_dev_kobj(dev);
  706. int error = 0;
  707. char devt_str[15];
  708. if (kobj) {
  709. format_dev_t(devt_str, dev->devt);
  710. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  711. }
  712. return error;
  713. }
  714. static void device_remove_sys_dev_entry(struct device *dev)
  715. {
  716. struct kobject *kobj = device_to_dev_kobj(dev);
  717. char devt_str[15];
  718. if (kobj) {
  719. format_dev_t(devt_str, dev->devt);
  720. sysfs_remove_link(kobj, devt_str);
  721. }
  722. }
  723. /**
  724. * device_add - add device to device hierarchy.
  725. * @dev: device.
  726. *
  727. * This is part 2 of device_register(), though may be called
  728. * separately _iff_ device_initialize() has been called separately.
  729. *
  730. * This adds @dev to the kobject hierarchy via kobject_add(), adds it
  731. * to the global and sibling lists for the device, then
  732. * adds it to the other relevant subsystems of the driver model.
  733. *
  734. * NOTE: _Never_ directly free @dev after calling this function, even
  735. * if it returned an error! Always use put_device() to give up your
  736. * reference instead.
  737. */
  738. int device_add(struct device *dev)
  739. {
  740. struct device *parent = NULL;
  741. struct class_interface *class_intf;
  742. int error = -EINVAL;
  743. dev = get_device(dev);
  744. if (!dev)
  745. goto done;
  746. /* Temporarily support init_name if it is set.
  747. * It will override bus_id for now */
  748. if (dev->init_name)
  749. dev_set_name(dev, "%s", dev->init_name);
  750. if (!strlen(dev->bus_id))
  751. goto done;
  752. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  753. parent = get_device(dev->parent);
  754. setup_parent(dev, parent);
  755. /* use parent numa_node */
  756. if (parent)
  757. set_dev_node(dev, dev_to_node(parent));
  758. /* first, register with generic layer. */
  759. error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
  760. if (error)
  761. goto Error;
  762. /* notify platform of device entry */
  763. if (platform_notify)
  764. platform_notify(dev);
  765. error = device_create_file(dev, &uevent_attr);
  766. if (error)
  767. goto attrError;
  768. if (MAJOR(dev->devt)) {
  769. error = device_create_file(dev, &devt_attr);
  770. if (error)
  771. goto ueventattrError;
  772. error = device_create_sys_dev_entry(dev);
  773. if (error)
  774. goto devtattrError;
  775. }
  776. error = device_add_class_symlinks(dev);
  777. if (error)
  778. goto SymlinkError;
  779. error = device_add_attrs(dev);
  780. if (error)
  781. goto AttrsError;
  782. error = bus_add_device(dev);
  783. if (error)
  784. goto BusError;
  785. error = dpm_sysfs_add(dev);
  786. if (error)
  787. goto DPMError;
  788. device_pm_add(dev);
  789. /* Notify clients of device addition. This call must come
  790. * after dpm_sysf_add() and before kobject_uevent().
  791. */
  792. if (dev->bus)
  793. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  794. BUS_NOTIFY_ADD_DEVICE, dev);
  795. kobject_uevent(&dev->kobj, KOBJ_ADD);
  796. bus_attach_device(dev);
  797. if (parent)
  798. klist_add_tail(&dev->p->knode_parent,
  799. &parent->p->klist_children);
  800. if (dev->class) {
  801. mutex_lock(&dev->class->p->class_mutex);
  802. /* tie the class to the device */
  803. klist_add_tail(&dev->knode_class,
  804. &dev->class->p->class_devices);
  805. /* notify any interfaces that the device is here */
  806. list_for_each_entry(class_intf,
  807. &dev->class->p->class_interfaces, node)
  808. if (class_intf->add_dev)
  809. class_intf->add_dev(dev, class_intf);
  810. mutex_unlock(&dev->class->p->class_mutex);
  811. }
  812. done:
  813. put_device(dev);
  814. return error;
  815. DPMError:
  816. bus_remove_device(dev);
  817. BusError:
  818. device_remove_attrs(dev);
  819. AttrsError:
  820. device_remove_class_symlinks(dev);
  821. SymlinkError:
  822. if (MAJOR(dev->devt))
  823. device_remove_sys_dev_entry(dev);
  824. devtattrError:
  825. if (MAJOR(dev->devt))
  826. device_remove_file(dev, &devt_attr);
  827. ueventattrError:
  828. device_remove_file(dev, &uevent_attr);
  829. attrError:
  830. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  831. kobject_del(&dev->kobj);
  832. Error:
  833. cleanup_device_parent(dev);
  834. if (parent)
  835. put_device(parent);
  836. goto done;
  837. }
  838. /**
  839. * device_register - register a device with the system.
  840. * @dev: pointer to the device structure
  841. *
  842. * This happens in two clean steps - initialize the device
  843. * and add it to the system. The two steps can be called
  844. * separately, but this is the easiest and most common.
  845. * I.e. you should only call the two helpers separately if
  846. * have a clearly defined need to use and refcount the device
  847. * before it is added to the hierarchy.
  848. *
  849. * NOTE: _Never_ directly free @dev after calling this function, even
  850. * if it returned an error! Always use put_device() to give up the
  851. * reference initialized in this function instead.
  852. */
  853. int device_register(struct device *dev)
  854. {
  855. device_initialize(dev);
  856. return device_add(dev);
  857. }
  858. /**
  859. * get_device - increment reference count for device.
  860. * @dev: device.
  861. *
  862. * This simply forwards the call to kobject_get(), though
  863. * we do take care to provide for the case that we get a NULL
  864. * pointer passed in.
  865. */
  866. struct device *get_device(struct device *dev)
  867. {
  868. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  869. }
  870. /**
  871. * put_device - decrement reference count.
  872. * @dev: device in question.
  873. */
  874. void put_device(struct device *dev)
  875. {
  876. /* might_sleep(); */
  877. if (dev)
  878. kobject_put(&dev->kobj);
  879. }
  880. /**
  881. * device_del - delete device from system.
  882. * @dev: device.
  883. *
  884. * This is the first part of the device unregistration
  885. * sequence. This removes the device from the lists we control
  886. * from here, has it removed from the other driver model
  887. * subsystems it was added to in device_add(), and removes it
  888. * from the kobject hierarchy.
  889. *
  890. * NOTE: this should be called manually _iff_ device_add() was
  891. * also called manually.
  892. */
  893. void device_del(struct device *dev)
  894. {
  895. struct device *parent = dev->parent;
  896. struct class_interface *class_intf;
  897. /* Notify clients of device removal. This call must come
  898. * before dpm_sysfs_remove().
  899. */
  900. if (dev->bus)
  901. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  902. BUS_NOTIFY_DEL_DEVICE, dev);
  903. device_pm_remove(dev);
  904. dpm_sysfs_remove(dev);
  905. if (parent)
  906. klist_del(&dev->p->knode_parent);
  907. if (MAJOR(dev->devt)) {
  908. device_remove_sys_dev_entry(dev);
  909. device_remove_file(dev, &devt_attr);
  910. }
  911. if (dev->class) {
  912. device_remove_class_symlinks(dev);
  913. mutex_lock(&dev->class->p->class_mutex);
  914. /* notify any interfaces that the device is now gone */
  915. list_for_each_entry(class_intf,
  916. &dev->class->p->class_interfaces, node)
  917. if (class_intf->remove_dev)
  918. class_intf->remove_dev(dev, class_intf);
  919. /* remove the device from the class list */
  920. klist_del(&dev->knode_class);
  921. mutex_unlock(&dev->class->p->class_mutex);
  922. }
  923. device_remove_file(dev, &uevent_attr);
  924. device_remove_attrs(dev);
  925. bus_remove_device(dev);
  926. /*
  927. * Some platform devices are driven without driver attached
  928. * and managed resources may have been acquired. Make sure
  929. * all resources are released.
  930. */
  931. devres_release_all(dev);
  932. /* Notify the platform of the removal, in case they
  933. * need to do anything...
  934. */
  935. if (platform_notify_remove)
  936. platform_notify_remove(dev);
  937. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  938. cleanup_device_parent(dev);
  939. kobject_del(&dev->kobj);
  940. put_device(parent);
  941. }
  942. /**
  943. * device_unregister - unregister device from system.
  944. * @dev: device going away.
  945. *
  946. * We do this in two parts, like we do device_register(). First,
  947. * we remove it from all the subsystems with device_del(), then
  948. * we decrement the reference count via put_device(). If that
  949. * is the final reference count, the device will be cleaned up
  950. * via device_release() above. Otherwise, the structure will
  951. * stick around until the final reference to the device is dropped.
  952. */
  953. void device_unregister(struct device *dev)
  954. {
  955. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  956. device_del(dev);
  957. put_device(dev);
  958. }
  959. static struct device *next_device(struct klist_iter *i)
  960. {
  961. struct klist_node *n = klist_next(i);
  962. struct device *dev = NULL;
  963. struct device_private *p;
  964. if (n) {
  965. p = to_device_private_parent(n);
  966. dev = p->device;
  967. }
  968. return dev;
  969. }
  970. /**
  971. * device_for_each_child - device child iterator.
  972. * @parent: parent struct device.
  973. * @data: data for the callback.
  974. * @fn: function to be called for each device.
  975. *
  976. * Iterate over @parent's child devices, and call @fn for each,
  977. * passing it @data.
  978. *
  979. * We check the return of @fn each time. If it returns anything
  980. * other than 0, we break out and return that value.
  981. */
  982. int device_for_each_child(struct device *parent, void *data,
  983. int (*fn)(struct device *dev, void *data))
  984. {
  985. struct klist_iter i;
  986. struct device *child;
  987. int error = 0;
  988. klist_iter_init(&parent->p->klist_children, &i);
  989. while ((child = next_device(&i)) && !error)
  990. error = fn(child, data);
  991. klist_iter_exit(&i);
  992. return error;
  993. }
  994. /**
  995. * device_find_child - device iterator for locating a particular device.
  996. * @parent: parent struct device
  997. * @data: Data to pass to match function
  998. * @match: Callback function to check device
  999. *
  1000. * This is similar to the device_for_each_child() function above, but it
  1001. * returns a reference to a device that is 'found' for later use, as
  1002. * determined by the @match callback.
  1003. *
  1004. * The callback should return 0 if the device doesn't match and non-zero
  1005. * if it does. If the callback returns non-zero and a reference to the
  1006. * current device can be obtained, this function will return to the caller
  1007. * and not iterate over any more devices.
  1008. */
  1009. struct device *device_find_child(struct device *parent, void *data,
  1010. int (*match)(struct device *dev, void *data))
  1011. {
  1012. struct klist_iter i;
  1013. struct device *child;
  1014. if (!parent)
  1015. return NULL;
  1016. klist_iter_init(&parent->p->klist_children, &i);
  1017. while ((child = next_device(&i)))
  1018. if (match(child, data) && get_device(child))
  1019. break;
  1020. klist_iter_exit(&i);
  1021. return child;
  1022. }
  1023. int __init devices_init(void)
  1024. {
  1025. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  1026. if (!devices_kset)
  1027. return -ENOMEM;
  1028. dev_kobj = kobject_create_and_add("dev", NULL);
  1029. if (!dev_kobj)
  1030. goto dev_kobj_err;
  1031. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  1032. if (!sysfs_dev_block_kobj)
  1033. goto block_kobj_err;
  1034. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  1035. if (!sysfs_dev_char_kobj)
  1036. goto char_kobj_err;
  1037. return 0;
  1038. char_kobj_err:
  1039. kobject_put(sysfs_dev_block_kobj);
  1040. block_kobj_err:
  1041. kobject_put(dev_kobj);
  1042. dev_kobj_err:
  1043. kset_unregister(devices_kset);
  1044. return -ENOMEM;
  1045. }
  1046. EXPORT_SYMBOL_GPL(device_for_each_child);
  1047. EXPORT_SYMBOL_GPL(device_find_child);
  1048. EXPORT_SYMBOL_GPL(device_initialize);
  1049. EXPORT_SYMBOL_GPL(device_add);
  1050. EXPORT_SYMBOL_GPL(device_register);
  1051. EXPORT_SYMBOL_GPL(device_del);
  1052. EXPORT_SYMBOL_GPL(device_unregister);
  1053. EXPORT_SYMBOL_GPL(get_device);
  1054. EXPORT_SYMBOL_GPL(put_device);
  1055. EXPORT_SYMBOL_GPL(device_create_file);
  1056. EXPORT_SYMBOL_GPL(device_remove_file);
  1057. struct root_device
  1058. {
  1059. struct device dev;
  1060. struct module *owner;
  1061. };
  1062. #define to_root_device(dev) container_of(dev, struct root_device, dev)
  1063. static void root_device_release(struct device *dev)
  1064. {
  1065. kfree(to_root_device(dev));
  1066. }
  1067. /**
  1068. * __root_device_register - allocate and register a root device
  1069. * @name: root device name
  1070. * @owner: owner module of the root device, usually THIS_MODULE
  1071. *
  1072. * This function allocates a root device and registers it
  1073. * using device_register(). In order to free the returned
  1074. * device, use root_device_unregister().
  1075. *
  1076. * Root devices are dummy devices which allow other devices
  1077. * to be grouped under /sys/devices. Use this function to
  1078. * allocate a root device and then use it as the parent of
  1079. * any device which should appear under /sys/devices/{name}
  1080. *
  1081. * The /sys/devices/{name} directory will also contain a
  1082. * 'module' symlink which points to the @owner directory
  1083. * in sysfs.
  1084. *
  1085. * Note: You probably want to use root_device_register().
  1086. */
  1087. struct device *__root_device_register(const char *name, struct module *owner)
  1088. {
  1089. struct root_device *root;
  1090. int err = -ENOMEM;
  1091. root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  1092. if (!root)
  1093. return ERR_PTR(err);
  1094. err = dev_set_name(&root->dev, name);
  1095. if (err) {
  1096. kfree(root);
  1097. return ERR_PTR(err);
  1098. }
  1099. root->dev.release = root_device_release;
  1100. err = device_register(&root->dev);
  1101. if (err) {
  1102. put_device(&root->dev);
  1103. return ERR_PTR(err);
  1104. }
  1105. #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
  1106. if (owner) {
  1107. struct module_kobject *mk = &owner->mkobj;
  1108. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  1109. if (err) {
  1110. device_unregister(&root->dev);
  1111. return ERR_PTR(err);
  1112. }
  1113. root->owner = owner;
  1114. }
  1115. #endif
  1116. return &root->dev;
  1117. }
  1118. EXPORT_SYMBOL_GPL(__root_device_register);
  1119. /**
  1120. * root_device_unregister - unregister and free a root device
  1121. * @root: device going away.
  1122. *
  1123. * This function unregisters and cleans up a device that was created by
  1124. * root_device_register().
  1125. */
  1126. void root_device_unregister(struct device *dev)
  1127. {
  1128. struct root_device *root = to_root_device(dev);
  1129. if (root->owner)
  1130. sysfs_remove_link(&root->dev.kobj, "module");
  1131. device_unregister(dev);
  1132. }
  1133. EXPORT_SYMBOL_GPL(root_device_unregister);
  1134. static void device_create_release(struct device *dev)
  1135. {
  1136. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1137. kfree(dev);
  1138. }
  1139. /**
  1140. * device_create_vargs - creates a device and registers it with sysfs
  1141. * @class: pointer to the struct class that this device should be registered to
  1142. * @parent: pointer to the parent struct device of this new device, if any
  1143. * @devt: the dev_t for the char device to be added
  1144. * @drvdata: the data to be added to the device for callbacks
  1145. * @fmt: string for the device's name
  1146. * @args: va_list for the device's name
  1147. *
  1148. * This function can be used by char device classes. A struct device
  1149. * will be created in sysfs, registered to the specified class.
  1150. *
  1151. * A "dev" file will be created, showing the dev_t for the device, if
  1152. * the dev_t is not 0,0.
  1153. * If a pointer to a parent struct device is passed in, the newly created
  1154. * struct device will be a child of that device in sysfs.
  1155. * The pointer to the struct device will be returned from the call.
  1156. * Any further sysfs files that might be required can be created using this
  1157. * pointer.
  1158. *
  1159. * Note: the struct class passed to this function must have previously
  1160. * been created with a call to class_create().
  1161. */
  1162. struct device *device_create_vargs(struct class *class, struct device *parent,
  1163. dev_t devt, void *drvdata, const char *fmt,
  1164. va_list args)
  1165. {
  1166. struct device *dev = NULL;
  1167. int retval = -ENODEV;
  1168. if (class == NULL || IS_ERR(class))
  1169. goto error;
  1170. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1171. if (!dev) {
  1172. retval = -ENOMEM;
  1173. goto error;
  1174. }
  1175. dev->devt = devt;
  1176. dev->class = class;
  1177. dev->parent = parent;
  1178. dev->release = device_create_release;
  1179. dev_set_drvdata(dev, drvdata);
  1180. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  1181. retval = device_register(dev);
  1182. if (retval)
  1183. goto error;
  1184. return dev;
  1185. error:
  1186. put_device(dev);
  1187. return ERR_PTR(retval);
  1188. }
  1189. EXPORT_SYMBOL_GPL(device_create_vargs);
  1190. /**
  1191. * device_create - creates a device and registers it with sysfs
  1192. * @class: pointer to the struct class that this device should be registered to
  1193. * @parent: pointer to the parent struct device of this new device, if any
  1194. * @devt: the dev_t for the char device to be added
  1195. * @drvdata: the data to be added to the device for callbacks
  1196. * @fmt: string for the device's name
  1197. *
  1198. * This function can be used by char device classes. A struct device
  1199. * will be created in sysfs, registered to the specified class.
  1200. *
  1201. * A "dev" file will be created, showing the dev_t for the device, if
  1202. * the dev_t is not 0,0.
  1203. * If a pointer to a parent struct device is passed in, the newly created
  1204. * struct device will be a child of that device in sysfs.
  1205. * The pointer to the struct device will be returned from the call.
  1206. * Any further sysfs files that might be required can be created using this
  1207. * pointer.
  1208. *
  1209. * Note: the struct class passed to this function must have previously
  1210. * been created with a call to class_create().
  1211. */
  1212. struct device *device_create(struct class *class, struct device *parent,
  1213. dev_t devt, void *drvdata, const char *fmt, ...)
  1214. {
  1215. va_list vargs;
  1216. struct device *dev;
  1217. va_start(vargs, fmt);
  1218. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  1219. va_end(vargs);
  1220. return dev;
  1221. }
  1222. EXPORT_SYMBOL_GPL(device_create);
  1223. static int __match_devt(struct device *dev, void *data)
  1224. {
  1225. dev_t *devt = data;
  1226. return dev->devt == *devt;
  1227. }
  1228. /**
  1229. * device_destroy - removes a device that was created with device_create()
  1230. * @class: pointer to the struct class that this device was registered with
  1231. * @devt: the dev_t of the device that was previously registered
  1232. *
  1233. * This call unregisters and cleans up a device that was created with a
  1234. * call to device_create().
  1235. */
  1236. void device_destroy(struct class *class, dev_t devt)
  1237. {
  1238. struct device *dev;
  1239. dev = class_find_device(class, NULL, &devt, __match_devt);
  1240. if (dev) {
  1241. put_device(dev);
  1242. device_unregister(dev);
  1243. }
  1244. }
  1245. EXPORT_SYMBOL_GPL(device_destroy);
  1246. /**
  1247. * device_rename - renames a device
  1248. * @dev: the pointer to the struct device to be renamed
  1249. * @new_name: the new name of the device
  1250. *
  1251. * It is the responsibility of the caller to provide mutual
  1252. * exclusion between two different calls of device_rename
  1253. * on the same device to ensure that new_name is valid and
  1254. * won't conflict with other devices.
  1255. */
  1256. int device_rename(struct device *dev, char *new_name)
  1257. {
  1258. char *old_class_name = NULL;
  1259. char *new_class_name = NULL;
  1260. char *old_device_name = NULL;
  1261. int error;
  1262. dev = get_device(dev);
  1263. if (!dev)
  1264. return -EINVAL;
  1265. pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
  1266. __func__, new_name);
  1267. #ifdef CONFIG_SYSFS_DEPRECATED
  1268. if ((dev->class) && (dev->parent))
  1269. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1270. #endif
  1271. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1272. if (!old_device_name) {
  1273. error = -ENOMEM;
  1274. goto out;
  1275. }
  1276. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1277. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1278. error = kobject_rename(&dev->kobj, new_name);
  1279. if (error) {
  1280. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1281. goto out;
  1282. }
  1283. #ifdef CONFIG_SYSFS_DEPRECATED
  1284. if (old_class_name) {
  1285. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1286. if (new_class_name) {
  1287. error = sysfs_create_link_nowarn(&dev->parent->kobj,
  1288. &dev->kobj,
  1289. new_class_name);
  1290. if (error)
  1291. goto out;
  1292. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1293. }
  1294. }
  1295. #else
  1296. if (dev->class) {
  1297. error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
  1298. &dev->kobj, dev_name(dev));
  1299. if (error)
  1300. goto out;
  1301. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  1302. old_device_name);
  1303. }
  1304. #endif
  1305. out:
  1306. put_device(dev);
  1307. kfree(new_class_name);
  1308. kfree(old_class_name);
  1309. kfree(old_device_name);
  1310. return error;
  1311. }
  1312. EXPORT_SYMBOL_GPL(device_rename);
  1313. static int device_move_class_links(struct device *dev,
  1314. struct device *old_parent,
  1315. struct device *new_parent)
  1316. {
  1317. int error = 0;
  1318. #ifdef CONFIG_SYSFS_DEPRECATED
  1319. char *class_name;
  1320. class_name = make_class_name(dev->class->name, &dev->kobj);
  1321. if (!class_name) {
  1322. error = -ENOMEM;
  1323. goto out;
  1324. }
  1325. if (old_parent) {
  1326. sysfs_remove_link(&dev->kobj, "device");
  1327. sysfs_remove_link(&old_parent->kobj, class_name);
  1328. }
  1329. if (new_parent) {
  1330. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1331. "device");
  1332. if (error)
  1333. goto out;
  1334. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1335. class_name);
  1336. if (error)
  1337. sysfs_remove_link(&dev->kobj, "device");
  1338. } else
  1339. error = 0;
  1340. out:
  1341. kfree(class_name);
  1342. return error;
  1343. #else
  1344. if (old_parent)
  1345. sysfs_remove_link(&dev->kobj, "device");
  1346. if (new_parent)
  1347. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1348. "device");
  1349. return error;
  1350. #endif
  1351. }
  1352. /**
  1353. * device_move - moves a device to a new parent
  1354. * @dev: the pointer to the struct device to be moved
  1355. * @new_parent: the new parent of the device (can by NULL)
  1356. */
  1357. int device_move(struct device *dev, struct device *new_parent)
  1358. {
  1359. int error;
  1360. struct device *old_parent;
  1361. struct kobject *new_parent_kobj;
  1362. dev = get_device(dev);
  1363. if (!dev)
  1364. return -EINVAL;
  1365. new_parent = get_device(new_parent);
  1366. new_parent_kobj = get_device_parent(dev, new_parent);
  1367. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  1368. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  1369. error = kobject_move(&dev->kobj, new_parent_kobj);
  1370. if (error) {
  1371. cleanup_glue_dir(dev, new_parent_kobj);
  1372. put_device(new_parent);
  1373. goto out;
  1374. }
  1375. old_parent = dev->parent;
  1376. dev->parent = new_parent;
  1377. if (old_parent)
  1378. klist_remove(&dev->p->knode_parent);
  1379. if (new_parent) {
  1380. klist_add_tail(&dev->p->knode_parent,
  1381. &new_parent->p->klist_children);
  1382. set_dev_node(dev, dev_to_node(new_parent));
  1383. }
  1384. if (!dev->class)
  1385. goto out_put;
  1386. error = device_move_class_links(dev, old_parent, new_parent);
  1387. if (error) {
  1388. /* We ignore errors on cleanup since we're hosed anyway... */
  1389. device_move_class_links(dev, new_parent, old_parent);
  1390. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1391. if (new_parent)
  1392. klist_remove(&dev->p->knode_parent);
  1393. dev->parent = old_parent;
  1394. if (old_parent) {
  1395. klist_add_tail(&dev->p->knode_parent,
  1396. &old_parent->p->klist_children);
  1397. set_dev_node(dev, dev_to_node(old_parent));
  1398. }
  1399. }
  1400. cleanup_glue_dir(dev, new_parent_kobj);
  1401. put_device(new_parent);
  1402. goto out;
  1403. }
  1404. out_put:
  1405. put_device(old_parent);
  1406. out:
  1407. put_device(dev);
  1408. return error;
  1409. }
  1410. EXPORT_SYMBOL_GPL(device_move);
  1411. /**
  1412. * device_shutdown - call ->shutdown() on each device to shutdown.
  1413. */
  1414. void device_shutdown(void)
  1415. {
  1416. struct device *dev, *devn;
  1417. list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
  1418. kobj.entry) {
  1419. if (dev->bus && dev->bus->shutdown) {
  1420. dev_dbg(dev, "shutdown\n");
  1421. dev->bus->shutdown(dev);
  1422. } else if (dev->driver && dev->driver->shutdown) {
  1423. dev_dbg(dev, "shutdown\n");
  1424. dev->driver->shutdown(dev);
  1425. }
  1426. }
  1427. kobject_put(sysfs_dev_char_kobj);
  1428. kobject_put(sysfs_dev_block_kobj);
  1429. kobject_put(dev_kobj);
  1430. }