core.c 43 KB

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