core.c 42 KB

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