core.c 44 KB

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