core.c 35 KB

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