core.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036
  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/fwnode.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/kdev_t.h>
  20. #include <linux/notifier.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/genhd.h>
  24. #include <linux/kallsyms.h>
  25. #include <linux/mutex.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/sched/signal.h>
  29. #include <linux/sysfs.h>
  30. #include "base.h"
  31. #include "power/power.h"
  32. #ifdef CONFIG_SYSFS_DEPRECATED
  33. #ifdef CONFIG_SYSFS_DEPRECATED_V2
  34. long sysfs_deprecated = 1;
  35. #else
  36. long sysfs_deprecated = 0;
  37. #endif
  38. static int __init sysfs_deprecated_setup(char *arg)
  39. {
  40. return kstrtol(arg, 10, &sysfs_deprecated);
  41. }
  42. early_param("sysfs.deprecated", sysfs_deprecated_setup);
  43. #endif
  44. /* Device links support. */
  45. #ifdef CONFIG_SRCU
  46. static DEFINE_MUTEX(device_links_lock);
  47. DEFINE_STATIC_SRCU(device_links_srcu);
  48. static inline void device_links_write_lock(void)
  49. {
  50. mutex_lock(&device_links_lock);
  51. }
  52. static inline void device_links_write_unlock(void)
  53. {
  54. mutex_unlock(&device_links_lock);
  55. }
  56. int device_links_read_lock(void)
  57. {
  58. return srcu_read_lock(&device_links_srcu);
  59. }
  60. void device_links_read_unlock(int idx)
  61. {
  62. srcu_read_unlock(&device_links_srcu, idx);
  63. }
  64. #else /* !CONFIG_SRCU */
  65. static DECLARE_RWSEM(device_links_lock);
  66. static inline void device_links_write_lock(void)
  67. {
  68. down_write(&device_links_lock);
  69. }
  70. static inline void device_links_write_unlock(void)
  71. {
  72. up_write(&device_links_lock);
  73. }
  74. int device_links_read_lock(void)
  75. {
  76. down_read(&device_links_lock);
  77. return 0;
  78. }
  79. void device_links_read_unlock(int not_used)
  80. {
  81. up_read(&device_links_lock);
  82. }
  83. #endif /* !CONFIG_SRCU */
  84. /**
  85. * device_is_dependent - Check if one device depends on another one
  86. * @dev: Device to check dependencies for.
  87. * @target: Device to check against.
  88. *
  89. * Check if @target depends on @dev or any device dependent on it (its child or
  90. * its consumer etc). Return 1 if that is the case or 0 otherwise.
  91. */
  92. static int device_is_dependent(struct device *dev, void *target)
  93. {
  94. struct device_link *link;
  95. int ret;
  96. if (WARN_ON(dev == target))
  97. return 1;
  98. ret = device_for_each_child(dev, target, device_is_dependent);
  99. if (ret)
  100. return ret;
  101. list_for_each_entry(link, &dev->links.consumers, s_node) {
  102. if (WARN_ON(link->consumer == target))
  103. return 1;
  104. ret = device_is_dependent(link->consumer, target);
  105. if (ret)
  106. break;
  107. }
  108. return ret;
  109. }
  110. static int device_reorder_to_tail(struct device *dev, void *not_used)
  111. {
  112. struct device_link *link;
  113. /*
  114. * Devices that have not been registered yet will be put to the ends
  115. * of the lists during the registration, so skip them here.
  116. */
  117. if (device_is_registered(dev))
  118. devices_kset_move_last(dev);
  119. if (device_pm_initialized(dev))
  120. device_pm_move_last(dev);
  121. device_for_each_child(dev, NULL, device_reorder_to_tail);
  122. list_for_each_entry(link, &dev->links.consumers, s_node)
  123. device_reorder_to_tail(link->consumer, NULL);
  124. return 0;
  125. }
  126. /**
  127. * device_link_add - Create a link between two devices.
  128. * @consumer: Consumer end of the link.
  129. * @supplier: Supplier end of the link.
  130. * @flags: Link flags.
  131. *
  132. * The caller is responsible for the proper synchronization of the link creation
  133. * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
  134. * runtime PM framework to take the link into account. Second, if the
  135. * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
  136. * be forced into the active metastate and reference-counted upon the creation
  137. * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
  138. * ignored.
  139. *
  140. * If the DL_FLAG_AUTOREMOVE is set, the link will be removed automatically
  141. * when the consumer device driver unbinds from it. The combination of both
  142. * DL_FLAG_AUTOREMOVE and DL_FLAG_STATELESS set is invalid and will cause NULL
  143. * to be returned.
  144. *
  145. * A side effect of the link creation is re-ordering of dpm_list and the
  146. * devices_kset list by moving the consumer device and all devices depending
  147. * on it to the ends of these lists (that does not happen to devices that have
  148. * not been registered when this function is called).
  149. *
  150. * The supplier device is required to be registered when this function is called
  151. * and NULL will be returned if that is not the case. The consumer device need
  152. * not be registered, however.
  153. */
  154. struct device_link *device_link_add(struct device *consumer,
  155. struct device *supplier, u32 flags)
  156. {
  157. struct device_link *link;
  158. if (!consumer || !supplier ||
  159. ((flags & DL_FLAG_STATELESS) && (flags & DL_FLAG_AUTOREMOVE)))
  160. return NULL;
  161. device_links_write_lock();
  162. device_pm_lock();
  163. /*
  164. * If the supplier has not been fully registered yet or there is a
  165. * reverse dependency between the consumer and the supplier already in
  166. * the graph, return NULL.
  167. */
  168. if (!device_pm_initialized(supplier)
  169. || device_is_dependent(consumer, supplier)) {
  170. link = NULL;
  171. goto out;
  172. }
  173. list_for_each_entry(link, &supplier->links.consumers, s_node)
  174. if (link->consumer == consumer)
  175. goto out;
  176. link = kzalloc(sizeof(*link), GFP_KERNEL);
  177. if (!link)
  178. goto out;
  179. if (flags & DL_FLAG_PM_RUNTIME) {
  180. if (flags & DL_FLAG_RPM_ACTIVE) {
  181. if (pm_runtime_get_sync(supplier) < 0) {
  182. pm_runtime_put_noidle(supplier);
  183. kfree(link);
  184. link = NULL;
  185. goto out;
  186. }
  187. link->rpm_active = true;
  188. }
  189. pm_runtime_new_link(consumer);
  190. }
  191. get_device(supplier);
  192. link->supplier = supplier;
  193. INIT_LIST_HEAD(&link->s_node);
  194. get_device(consumer);
  195. link->consumer = consumer;
  196. INIT_LIST_HEAD(&link->c_node);
  197. link->flags = flags;
  198. /* Determine the initial link state. */
  199. if (flags & DL_FLAG_STATELESS) {
  200. link->status = DL_STATE_NONE;
  201. } else {
  202. switch (supplier->links.status) {
  203. case DL_DEV_DRIVER_BOUND:
  204. switch (consumer->links.status) {
  205. case DL_DEV_PROBING:
  206. /*
  207. * Balance the decrementation of the supplier's
  208. * runtime PM usage counter after consumer probe
  209. * in driver_probe_device().
  210. */
  211. if (flags & DL_FLAG_PM_RUNTIME)
  212. pm_runtime_get_sync(supplier);
  213. link->status = DL_STATE_CONSUMER_PROBE;
  214. break;
  215. case DL_DEV_DRIVER_BOUND:
  216. link->status = DL_STATE_ACTIVE;
  217. break;
  218. default:
  219. link->status = DL_STATE_AVAILABLE;
  220. break;
  221. }
  222. break;
  223. case DL_DEV_UNBINDING:
  224. link->status = DL_STATE_SUPPLIER_UNBIND;
  225. break;
  226. default:
  227. link->status = DL_STATE_DORMANT;
  228. break;
  229. }
  230. }
  231. /*
  232. * Move the consumer and all of the devices depending on it to the end
  233. * of dpm_list and the devices_kset list.
  234. *
  235. * It is necessary to hold dpm_list locked throughout all that or else
  236. * we may end up suspending with a wrong ordering of it.
  237. */
  238. device_reorder_to_tail(consumer, NULL);
  239. list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
  240. list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
  241. dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
  242. out:
  243. device_pm_unlock();
  244. device_links_write_unlock();
  245. return link;
  246. }
  247. EXPORT_SYMBOL_GPL(device_link_add);
  248. static void device_link_free(struct device_link *link)
  249. {
  250. put_device(link->consumer);
  251. put_device(link->supplier);
  252. kfree(link);
  253. }
  254. #ifdef CONFIG_SRCU
  255. static void __device_link_free_srcu(struct rcu_head *rhead)
  256. {
  257. device_link_free(container_of(rhead, struct device_link, rcu_head));
  258. }
  259. static void __device_link_del(struct device_link *link)
  260. {
  261. dev_info(link->consumer, "Dropping the link to %s\n",
  262. dev_name(link->supplier));
  263. if (link->flags & DL_FLAG_PM_RUNTIME)
  264. pm_runtime_drop_link(link->consumer);
  265. list_del_rcu(&link->s_node);
  266. list_del_rcu(&link->c_node);
  267. call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
  268. }
  269. #else /* !CONFIG_SRCU */
  270. static void __device_link_del(struct device_link *link)
  271. {
  272. dev_info(link->consumer, "Dropping the link to %s\n",
  273. dev_name(link->supplier));
  274. list_del(&link->s_node);
  275. list_del(&link->c_node);
  276. device_link_free(link);
  277. }
  278. #endif /* !CONFIG_SRCU */
  279. /**
  280. * device_link_del - Delete a link between two devices.
  281. * @link: Device link to delete.
  282. *
  283. * The caller must ensure proper synchronization of this function with runtime
  284. * PM.
  285. */
  286. void device_link_del(struct device_link *link)
  287. {
  288. device_links_write_lock();
  289. device_pm_lock();
  290. __device_link_del(link);
  291. device_pm_unlock();
  292. device_links_write_unlock();
  293. }
  294. EXPORT_SYMBOL_GPL(device_link_del);
  295. static void device_links_missing_supplier(struct device *dev)
  296. {
  297. struct device_link *link;
  298. list_for_each_entry(link, &dev->links.suppliers, c_node)
  299. if (link->status == DL_STATE_CONSUMER_PROBE)
  300. WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
  301. }
  302. /**
  303. * device_links_check_suppliers - Check presence of supplier drivers.
  304. * @dev: Consumer device.
  305. *
  306. * Check links from this device to any suppliers. Walk the list of the device's
  307. * links to suppliers and see if all of them are available. If not, simply
  308. * return -EPROBE_DEFER.
  309. *
  310. * We need to guarantee that the supplier will not go away after the check has
  311. * been positive here. It only can go away in __device_release_driver() and
  312. * that function checks the device's links to consumers. This means we need to
  313. * mark the link as "consumer probe in progress" to make the supplier removal
  314. * wait for us to complete (or bad things may happen).
  315. *
  316. * Links with the DL_FLAG_STATELESS flag set are ignored.
  317. */
  318. int device_links_check_suppliers(struct device *dev)
  319. {
  320. struct device_link *link;
  321. int ret = 0;
  322. device_links_write_lock();
  323. list_for_each_entry(link, &dev->links.suppliers, c_node) {
  324. if (link->flags & DL_FLAG_STATELESS)
  325. continue;
  326. if (link->status != DL_STATE_AVAILABLE) {
  327. device_links_missing_supplier(dev);
  328. ret = -EPROBE_DEFER;
  329. break;
  330. }
  331. WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
  332. }
  333. dev->links.status = DL_DEV_PROBING;
  334. device_links_write_unlock();
  335. return ret;
  336. }
  337. /**
  338. * device_links_driver_bound - Update device links after probing its driver.
  339. * @dev: Device to update the links for.
  340. *
  341. * The probe has been successful, so update links from this device to any
  342. * consumers by changing their status to "available".
  343. *
  344. * Also change the status of @dev's links to suppliers to "active".
  345. *
  346. * Links with the DL_FLAG_STATELESS flag set are ignored.
  347. */
  348. void device_links_driver_bound(struct device *dev)
  349. {
  350. struct device_link *link;
  351. device_links_write_lock();
  352. list_for_each_entry(link, &dev->links.consumers, s_node) {
  353. if (link->flags & DL_FLAG_STATELESS)
  354. continue;
  355. WARN_ON(link->status != DL_STATE_DORMANT);
  356. WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
  357. }
  358. list_for_each_entry(link, &dev->links.suppliers, c_node) {
  359. if (link->flags & DL_FLAG_STATELESS)
  360. continue;
  361. WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
  362. WRITE_ONCE(link->status, DL_STATE_ACTIVE);
  363. }
  364. dev->links.status = DL_DEV_DRIVER_BOUND;
  365. device_links_write_unlock();
  366. }
  367. /**
  368. * __device_links_no_driver - Update links of a device without a driver.
  369. * @dev: Device without a drvier.
  370. *
  371. * Delete all non-persistent links from this device to any suppliers.
  372. *
  373. * Persistent links stay around, but their status is changed to "available",
  374. * unless they already are in the "supplier unbind in progress" state in which
  375. * case they need not be updated.
  376. *
  377. * Links with the DL_FLAG_STATELESS flag set are ignored.
  378. */
  379. static void __device_links_no_driver(struct device *dev)
  380. {
  381. struct device_link *link, *ln;
  382. list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
  383. if (link->flags & DL_FLAG_STATELESS)
  384. continue;
  385. if (link->flags & DL_FLAG_AUTOREMOVE)
  386. __device_link_del(link);
  387. else if (link->status != DL_STATE_SUPPLIER_UNBIND)
  388. WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
  389. }
  390. dev->links.status = DL_DEV_NO_DRIVER;
  391. }
  392. void device_links_no_driver(struct device *dev)
  393. {
  394. device_links_write_lock();
  395. __device_links_no_driver(dev);
  396. device_links_write_unlock();
  397. }
  398. /**
  399. * device_links_driver_cleanup - Update links after driver removal.
  400. * @dev: Device whose driver has just gone away.
  401. *
  402. * Update links to consumers for @dev by changing their status to "dormant" and
  403. * invoke %__device_links_no_driver() to update links to suppliers for it as
  404. * appropriate.
  405. *
  406. * Links with the DL_FLAG_STATELESS flag set are ignored.
  407. */
  408. void device_links_driver_cleanup(struct device *dev)
  409. {
  410. struct device_link *link;
  411. device_links_write_lock();
  412. list_for_each_entry(link, &dev->links.consumers, s_node) {
  413. if (link->flags & DL_FLAG_STATELESS)
  414. continue;
  415. WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
  416. WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
  417. WRITE_ONCE(link->status, DL_STATE_DORMANT);
  418. }
  419. __device_links_no_driver(dev);
  420. device_links_write_unlock();
  421. }
  422. /**
  423. * device_links_busy - Check if there are any busy links to consumers.
  424. * @dev: Device to check.
  425. *
  426. * Check each consumer of the device and return 'true' if its link's status
  427. * is one of "consumer probe" or "active" (meaning that the given consumer is
  428. * probing right now or its driver is present). Otherwise, change the link
  429. * state to "supplier unbind" to prevent the consumer from being probed
  430. * successfully going forward.
  431. *
  432. * Return 'false' if there are no probing or active consumers.
  433. *
  434. * Links with the DL_FLAG_STATELESS flag set are ignored.
  435. */
  436. bool device_links_busy(struct device *dev)
  437. {
  438. struct device_link *link;
  439. bool ret = false;
  440. device_links_write_lock();
  441. list_for_each_entry(link, &dev->links.consumers, s_node) {
  442. if (link->flags & DL_FLAG_STATELESS)
  443. continue;
  444. if (link->status == DL_STATE_CONSUMER_PROBE
  445. || link->status == DL_STATE_ACTIVE) {
  446. ret = true;
  447. break;
  448. }
  449. WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
  450. }
  451. dev->links.status = DL_DEV_UNBINDING;
  452. device_links_write_unlock();
  453. return ret;
  454. }
  455. /**
  456. * device_links_unbind_consumers - Force unbind consumers of the given device.
  457. * @dev: Device to unbind the consumers of.
  458. *
  459. * Walk the list of links to consumers for @dev and if any of them is in the
  460. * "consumer probe" state, wait for all device probes in progress to complete
  461. * and start over.
  462. *
  463. * If that's not the case, change the status of the link to "supplier unbind"
  464. * and check if the link was in the "active" state. If so, force the consumer
  465. * driver to unbind and start over (the consumer will not re-probe as we have
  466. * changed the state of the link already).
  467. *
  468. * Links with the DL_FLAG_STATELESS flag set are ignored.
  469. */
  470. void device_links_unbind_consumers(struct device *dev)
  471. {
  472. struct device_link *link;
  473. start:
  474. device_links_write_lock();
  475. list_for_each_entry(link, &dev->links.consumers, s_node) {
  476. enum device_link_state status;
  477. if (link->flags & DL_FLAG_STATELESS)
  478. continue;
  479. status = link->status;
  480. if (status == DL_STATE_CONSUMER_PROBE) {
  481. device_links_write_unlock();
  482. wait_for_device_probe();
  483. goto start;
  484. }
  485. WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
  486. if (status == DL_STATE_ACTIVE) {
  487. struct device *consumer = link->consumer;
  488. get_device(consumer);
  489. device_links_write_unlock();
  490. device_release_driver_internal(consumer, NULL,
  491. consumer->parent);
  492. put_device(consumer);
  493. goto start;
  494. }
  495. }
  496. device_links_write_unlock();
  497. }
  498. /**
  499. * device_links_purge - Delete existing links to other devices.
  500. * @dev: Target device.
  501. */
  502. static void device_links_purge(struct device *dev)
  503. {
  504. struct device_link *link, *ln;
  505. /*
  506. * Delete all of the remaining links from this device to any other
  507. * devices (either consumers or suppliers).
  508. */
  509. device_links_write_lock();
  510. list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
  511. WARN_ON(link->status == DL_STATE_ACTIVE);
  512. __device_link_del(link);
  513. }
  514. list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
  515. WARN_ON(link->status != DL_STATE_DORMANT &&
  516. link->status != DL_STATE_NONE);
  517. __device_link_del(link);
  518. }
  519. device_links_write_unlock();
  520. }
  521. /* Device links support end. */
  522. int (*platform_notify)(struct device *dev) = NULL;
  523. int (*platform_notify_remove)(struct device *dev) = NULL;
  524. static struct kobject *dev_kobj;
  525. struct kobject *sysfs_dev_char_kobj;
  526. struct kobject *sysfs_dev_block_kobj;
  527. static DEFINE_MUTEX(device_hotplug_lock);
  528. void lock_device_hotplug(void)
  529. {
  530. mutex_lock(&device_hotplug_lock);
  531. }
  532. void unlock_device_hotplug(void)
  533. {
  534. mutex_unlock(&device_hotplug_lock);
  535. }
  536. int lock_device_hotplug_sysfs(void)
  537. {
  538. if (mutex_trylock(&device_hotplug_lock))
  539. return 0;
  540. /* Avoid busy looping (5 ms of sleep should do). */
  541. msleep(5);
  542. return restart_syscall();
  543. }
  544. #ifdef CONFIG_BLOCK
  545. static inline int device_is_not_partition(struct device *dev)
  546. {
  547. return !(dev->type == &part_type);
  548. }
  549. #else
  550. static inline int device_is_not_partition(struct device *dev)
  551. {
  552. return 1;
  553. }
  554. #endif
  555. /**
  556. * dev_driver_string - Return a device's driver name, if at all possible
  557. * @dev: struct device to get the name of
  558. *
  559. * Will return the device's driver's name if it is bound to a device. If
  560. * the device is not bound to a driver, it will return the name of the bus
  561. * it is attached to. If it is not attached to a bus either, an empty
  562. * string will be returned.
  563. */
  564. const char *dev_driver_string(const struct device *dev)
  565. {
  566. struct device_driver *drv;
  567. /* dev->driver can change to NULL underneath us because of unbinding,
  568. * so be careful about accessing it. dev->bus and dev->class should
  569. * never change once they are set, so they don't need special care.
  570. */
  571. drv = READ_ONCE(dev->driver);
  572. return drv ? drv->name :
  573. (dev->bus ? dev->bus->name :
  574. (dev->class ? dev->class->name : ""));
  575. }
  576. EXPORT_SYMBOL(dev_driver_string);
  577. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  578. static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  579. char *buf)
  580. {
  581. struct device_attribute *dev_attr = to_dev_attr(attr);
  582. struct device *dev = kobj_to_dev(kobj);
  583. ssize_t ret = -EIO;
  584. if (dev_attr->show)
  585. ret = dev_attr->show(dev, dev_attr, buf);
  586. if (ret >= (ssize_t)PAGE_SIZE) {
  587. print_symbol("dev_attr_show: %s returned bad count\n",
  588. (unsigned long)dev_attr->show);
  589. }
  590. return ret;
  591. }
  592. static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  593. const char *buf, size_t count)
  594. {
  595. struct device_attribute *dev_attr = to_dev_attr(attr);
  596. struct device *dev = kobj_to_dev(kobj);
  597. ssize_t ret = -EIO;
  598. if (dev_attr->store)
  599. ret = dev_attr->store(dev, dev_attr, buf, count);
  600. return ret;
  601. }
  602. static const struct sysfs_ops dev_sysfs_ops = {
  603. .show = dev_attr_show,
  604. .store = dev_attr_store,
  605. };
  606. #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
  607. ssize_t device_store_ulong(struct device *dev,
  608. struct device_attribute *attr,
  609. const char *buf, size_t size)
  610. {
  611. struct dev_ext_attribute *ea = to_ext_attr(attr);
  612. char *end;
  613. unsigned long new = simple_strtoul(buf, &end, 0);
  614. if (end == buf)
  615. return -EINVAL;
  616. *(unsigned long *)(ea->var) = new;
  617. /* Always return full write size even if we didn't consume all */
  618. return size;
  619. }
  620. EXPORT_SYMBOL_GPL(device_store_ulong);
  621. ssize_t device_show_ulong(struct device *dev,
  622. struct device_attribute *attr,
  623. char *buf)
  624. {
  625. struct dev_ext_attribute *ea = to_ext_attr(attr);
  626. return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
  627. }
  628. EXPORT_SYMBOL_GPL(device_show_ulong);
  629. ssize_t device_store_int(struct device *dev,
  630. struct device_attribute *attr,
  631. const char *buf, size_t size)
  632. {
  633. struct dev_ext_attribute *ea = to_ext_attr(attr);
  634. char *end;
  635. long new = simple_strtol(buf, &end, 0);
  636. if (end == buf || new > INT_MAX || new < INT_MIN)
  637. return -EINVAL;
  638. *(int *)(ea->var) = new;
  639. /* Always return full write size even if we didn't consume all */
  640. return size;
  641. }
  642. EXPORT_SYMBOL_GPL(device_store_int);
  643. ssize_t device_show_int(struct device *dev,
  644. struct device_attribute *attr,
  645. char *buf)
  646. {
  647. struct dev_ext_attribute *ea = to_ext_attr(attr);
  648. return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
  649. }
  650. EXPORT_SYMBOL_GPL(device_show_int);
  651. ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
  652. const char *buf, size_t size)
  653. {
  654. struct dev_ext_attribute *ea = to_ext_attr(attr);
  655. if (strtobool(buf, ea->var) < 0)
  656. return -EINVAL;
  657. return size;
  658. }
  659. EXPORT_SYMBOL_GPL(device_store_bool);
  660. ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
  661. char *buf)
  662. {
  663. struct dev_ext_attribute *ea = to_ext_attr(attr);
  664. return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
  665. }
  666. EXPORT_SYMBOL_GPL(device_show_bool);
  667. /**
  668. * device_release - free device structure.
  669. * @kobj: device's kobject.
  670. *
  671. * This is called once the reference count for the object
  672. * reaches 0. We forward the call to the device's release
  673. * method, which should handle actually freeing the structure.
  674. */
  675. static void device_release(struct kobject *kobj)
  676. {
  677. struct device *dev = kobj_to_dev(kobj);
  678. struct device_private *p = dev->p;
  679. /*
  680. * Some platform devices are driven without driver attached
  681. * and managed resources may have been acquired. Make sure
  682. * all resources are released.
  683. *
  684. * Drivers still can add resources into device after device
  685. * is deleted but alive, so release devres here to avoid
  686. * possible memory leak.
  687. */
  688. devres_release_all(dev);
  689. if (dev->release)
  690. dev->release(dev);
  691. else if (dev->type && dev->type->release)
  692. dev->type->release(dev);
  693. else if (dev->class && dev->class->dev_release)
  694. dev->class->dev_release(dev);
  695. else
  696. WARN(1, KERN_ERR "Device '%s' does not have a release() "
  697. "function, it is broken and must be fixed.\n",
  698. dev_name(dev));
  699. kfree(p);
  700. }
  701. static const void *device_namespace(struct kobject *kobj)
  702. {
  703. struct device *dev = kobj_to_dev(kobj);
  704. const void *ns = NULL;
  705. if (dev->class && dev->class->ns_type)
  706. ns = dev->class->namespace(dev);
  707. return ns;
  708. }
  709. static struct kobj_type device_ktype = {
  710. .release = device_release,
  711. .sysfs_ops = &dev_sysfs_ops,
  712. .namespace = device_namespace,
  713. };
  714. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  715. {
  716. struct kobj_type *ktype = get_ktype(kobj);
  717. if (ktype == &device_ktype) {
  718. struct device *dev = kobj_to_dev(kobj);
  719. if (dev->bus)
  720. return 1;
  721. if (dev->class)
  722. return 1;
  723. }
  724. return 0;
  725. }
  726. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  727. {
  728. struct device *dev = kobj_to_dev(kobj);
  729. if (dev->bus)
  730. return dev->bus->name;
  731. if (dev->class)
  732. return dev->class->name;
  733. return NULL;
  734. }
  735. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  736. struct kobj_uevent_env *env)
  737. {
  738. struct device *dev = kobj_to_dev(kobj);
  739. int retval = 0;
  740. /* add device node properties if present */
  741. if (MAJOR(dev->devt)) {
  742. const char *tmp;
  743. const char *name;
  744. umode_t mode = 0;
  745. kuid_t uid = GLOBAL_ROOT_UID;
  746. kgid_t gid = GLOBAL_ROOT_GID;
  747. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  748. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  749. name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
  750. if (name) {
  751. add_uevent_var(env, "DEVNAME=%s", name);
  752. if (mode)
  753. add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
  754. if (!uid_eq(uid, GLOBAL_ROOT_UID))
  755. add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
  756. if (!gid_eq(gid, GLOBAL_ROOT_GID))
  757. add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
  758. kfree(tmp);
  759. }
  760. }
  761. if (dev->type && dev->type->name)
  762. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  763. if (dev->driver)
  764. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  765. /* Add common DT information about the device */
  766. of_device_uevent(dev, env);
  767. /* have the bus specific function add its stuff */
  768. if (dev->bus && dev->bus->uevent) {
  769. retval = dev->bus->uevent(dev, env);
  770. if (retval)
  771. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  772. dev_name(dev), __func__, retval);
  773. }
  774. /* have the class specific function add its stuff */
  775. if (dev->class && dev->class->dev_uevent) {
  776. retval = dev->class->dev_uevent(dev, env);
  777. if (retval)
  778. pr_debug("device: '%s': %s: class uevent() "
  779. "returned %d\n", dev_name(dev),
  780. __func__, retval);
  781. }
  782. /* have the device type specific function add its stuff */
  783. if (dev->type && dev->type->uevent) {
  784. retval = dev->type->uevent(dev, env);
  785. if (retval)
  786. pr_debug("device: '%s': %s: dev_type uevent() "
  787. "returned %d\n", dev_name(dev),
  788. __func__, retval);
  789. }
  790. return retval;
  791. }
  792. static const struct kset_uevent_ops device_uevent_ops = {
  793. .filter = dev_uevent_filter,
  794. .name = dev_uevent_name,
  795. .uevent = dev_uevent,
  796. };
  797. static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
  798. char *buf)
  799. {
  800. struct kobject *top_kobj;
  801. struct kset *kset;
  802. struct kobj_uevent_env *env = NULL;
  803. int i;
  804. size_t count = 0;
  805. int retval;
  806. /* search the kset, the device belongs to */
  807. top_kobj = &dev->kobj;
  808. while (!top_kobj->kset && top_kobj->parent)
  809. top_kobj = top_kobj->parent;
  810. if (!top_kobj->kset)
  811. goto out;
  812. kset = top_kobj->kset;
  813. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  814. goto out;
  815. /* respect filter */
  816. if (kset->uevent_ops && kset->uevent_ops->filter)
  817. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  818. goto out;
  819. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  820. if (!env)
  821. return -ENOMEM;
  822. /* let the kset specific function add its keys */
  823. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  824. if (retval)
  825. goto out;
  826. /* copy keys to file */
  827. for (i = 0; i < env->envp_idx; i++)
  828. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  829. out:
  830. kfree(env);
  831. return count;
  832. }
  833. static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
  834. const char *buf, size_t count)
  835. {
  836. if (kobject_synth_uevent(&dev->kobj, buf, count))
  837. dev_err(dev, "uevent: failed to send synthetic uevent\n");
  838. return count;
  839. }
  840. static DEVICE_ATTR_RW(uevent);
  841. static ssize_t online_show(struct device *dev, struct device_attribute *attr,
  842. char *buf)
  843. {
  844. bool val;
  845. device_lock(dev);
  846. val = !dev->offline;
  847. device_unlock(dev);
  848. return sprintf(buf, "%u\n", val);
  849. }
  850. static ssize_t online_store(struct device *dev, struct device_attribute *attr,
  851. const char *buf, size_t count)
  852. {
  853. bool val;
  854. int ret;
  855. ret = strtobool(buf, &val);
  856. if (ret < 0)
  857. return ret;
  858. ret = lock_device_hotplug_sysfs();
  859. if (ret)
  860. return ret;
  861. ret = val ? device_online(dev) : device_offline(dev);
  862. unlock_device_hotplug();
  863. return ret < 0 ? ret : count;
  864. }
  865. static DEVICE_ATTR_RW(online);
  866. int device_add_groups(struct device *dev, const struct attribute_group **groups)
  867. {
  868. return sysfs_create_groups(&dev->kobj, groups);
  869. }
  870. EXPORT_SYMBOL_GPL(device_add_groups);
  871. void device_remove_groups(struct device *dev,
  872. const struct attribute_group **groups)
  873. {
  874. sysfs_remove_groups(&dev->kobj, groups);
  875. }
  876. EXPORT_SYMBOL_GPL(device_remove_groups);
  877. union device_attr_group_devres {
  878. const struct attribute_group *group;
  879. const struct attribute_group **groups;
  880. };
  881. static int devm_attr_group_match(struct device *dev, void *res, void *data)
  882. {
  883. return ((union device_attr_group_devres *)res)->group == data;
  884. }
  885. static void devm_attr_group_remove(struct device *dev, void *res)
  886. {
  887. union device_attr_group_devres *devres = res;
  888. const struct attribute_group *group = devres->group;
  889. dev_dbg(dev, "%s: removing group %p\n", __func__, group);
  890. sysfs_remove_group(&dev->kobj, group);
  891. }
  892. static void devm_attr_groups_remove(struct device *dev, void *res)
  893. {
  894. union device_attr_group_devres *devres = res;
  895. const struct attribute_group **groups = devres->groups;
  896. dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
  897. sysfs_remove_groups(&dev->kobj, groups);
  898. }
  899. /**
  900. * devm_device_add_group - given a device, create a managed attribute group
  901. * @dev: The device to create the group for
  902. * @grp: The attribute group to create
  903. *
  904. * This function creates a group for the first time. It will explicitly
  905. * warn and error if any of the attribute files being created already exist.
  906. *
  907. * Returns 0 on success or error code on failure.
  908. */
  909. int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
  910. {
  911. union device_attr_group_devres *devres;
  912. int error;
  913. devres = devres_alloc(devm_attr_group_remove,
  914. sizeof(*devres), GFP_KERNEL);
  915. if (!devres)
  916. return -ENOMEM;
  917. error = sysfs_create_group(&dev->kobj, grp);
  918. if (error) {
  919. devres_free(devres);
  920. return error;
  921. }
  922. devres->group = grp;
  923. devres_add(dev, devres);
  924. return 0;
  925. }
  926. EXPORT_SYMBOL_GPL(devm_device_add_group);
  927. /**
  928. * devm_device_remove_group: remove a managed group from a device
  929. * @dev: device to remove the group from
  930. * @grp: group to remove
  931. *
  932. * This function removes a group of attributes from a device. The attributes
  933. * previously have to have been created for this group, otherwise it will fail.
  934. */
  935. void devm_device_remove_group(struct device *dev,
  936. const struct attribute_group *grp)
  937. {
  938. WARN_ON(devres_release(dev, devm_attr_group_remove,
  939. devm_attr_group_match,
  940. /* cast away const */ (void *)grp));
  941. }
  942. EXPORT_SYMBOL_GPL(devm_device_remove_group);
  943. /**
  944. * devm_device_add_groups - create a bunch of managed attribute groups
  945. * @dev: The device to create the group for
  946. * @groups: The attribute groups to create, NULL terminated
  947. *
  948. * This function creates a bunch of managed attribute groups. If an error
  949. * occurs when creating a group, all previously created groups will be
  950. * removed, unwinding everything back to the original state when this
  951. * function was called. It will explicitly warn and error if any of the
  952. * attribute files being created already exist.
  953. *
  954. * Returns 0 on success or error code from sysfs_create_group on failure.
  955. */
  956. int devm_device_add_groups(struct device *dev,
  957. const struct attribute_group **groups)
  958. {
  959. union device_attr_group_devres *devres;
  960. int error;
  961. devres = devres_alloc(devm_attr_groups_remove,
  962. sizeof(*devres), GFP_KERNEL);
  963. if (!devres)
  964. return -ENOMEM;
  965. error = sysfs_create_groups(&dev->kobj, groups);
  966. if (error) {
  967. devres_free(devres);
  968. return error;
  969. }
  970. devres->groups = groups;
  971. devres_add(dev, devres);
  972. return 0;
  973. }
  974. EXPORT_SYMBOL_GPL(devm_device_add_groups);
  975. /**
  976. * devm_device_remove_groups - remove a list of managed groups
  977. *
  978. * @dev: The device for the groups to be removed from
  979. * @groups: NULL terminated list of groups to be removed
  980. *
  981. * If groups is not NULL, remove the specified groups from the device.
  982. */
  983. void devm_device_remove_groups(struct device *dev,
  984. const struct attribute_group **groups)
  985. {
  986. WARN_ON(devres_release(dev, devm_attr_groups_remove,
  987. devm_attr_group_match,
  988. /* cast away const */ (void *)groups));
  989. }
  990. EXPORT_SYMBOL_GPL(devm_device_remove_groups);
  991. static int device_add_attrs(struct device *dev)
  992. {
  993. struct class *class = dev->class;
  994. const struct device_type *type = dev->type;
  995. int error;
  996. if (class) {
  997. error = device_add_groups(dev, class->dev_groups);
  998. if (error)
  999. return error;
  1000. }
  1001. if (type) {
  1002. error = device_add_groups(dev, type->groups);
  1003. if (error)
  1004. goto err_remove_class_groups;
  1005. }
  1006. error = device_add_groups(dev, dev->groups);
  1007. if (error)
  1008. goto err_remove_type_groups;
  1009. if (device_supports_offline(dev) && !dev->offline_disabled) {
  1010. error = device_create_file(dev, &dev_attr_online);
  1011. if (error)
  1012. goto err_remove_dev_groups;
  1013. }
  1014. return 0;
  1015. err_remove_dev_groups:
  1016. device_remove_groups(dev, dev->groups);
  1017. err_remove_type_groups:
  1018. if (type)
  1019. device_remove_groups(dev, type->groups);
  1020. err_remove_class_groups:
  1021. if (class)
  1022. device_remove_groups(dev, class->dev_groups);
  1023. return error;
  1024. }
  1025. static void device_remove_attrs(struct device *dev)
  1026. {
  1027. struct class *class = dev->class;
  1028. const struct device_type *type = dev->type;
  1029. device_remove_file(dev, &dev_attr_online);
  1030. device_remove_groups(dev, dev->groups);
  1031. if (type)
  1032. device_remove_groups(dev, type->groups);
  1033. if (class)
  1034. device_remove_groups(dev, class->dev_groups);
  1035. }
  1036. static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
  1037. char *buf)
  1038. {
  1039. return print_dev_t(buf, dev->devt);
  1040. }
  1041. static DEVICE_ATTR_RO(dev);
  1042. /* /sys/devices/ */
  1043. struct kset *devices_kset;
  1044. /**
  1045. * devices_kset_move_before - Move device in the devices_kset's list.
  1046. * @deva: Device to move.
  1047. * @devb: Device @deva should come before.
  1048. */
  1049. static void devices_kset_move_before(struct device *deva, struct device *devb)
  1050. {
  1051. if (!devices_kset)
  1052. return;
  1053. pr_debug("devices_kset: Moving %s before %s\n",
  1054. dev_name(deva), dev_name(devb));
  1055. spin_lock(&devices_kset->list_lock);
  1056. list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
  1057. spin_unlock(&devices_kset->list_lock);
  1058. }
  1059. /**
  1060. * devices_kset_move_after - Move device in the devices_kset's list.
  1061. * @deva: Device to move
  1062. * @devb: Device @deva should come after.
  1063. */
  1064. static void devices_kset_move_after(struct device *deva, struct device *devb)
  1065. {
  1066. if (!devices_kset)
  1067. return;
  1068. pr_debug("devices_kset: Moving %s after %s\n",
  1069. dev_name(deva), dev_name(devb));
  1070. spin_lock(&devices_kset->list_lock);
  1071. list_move(&deva->kobj.entry, &devb->kobj.entry);
  1072. spin_unlock(&devices_kset->list_lock);
  1073. }
  1074. /**
  1075. * devices_kset_move_last - move the device to the end of devices_kset's list.
  1076. * @dev: device to move
  1077. */
  1078. void devices_kset_move_last(struct device *dev)
  1079. {
  1080. if (!devices_kset)
  1081. return;
  1082. pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
  1083. spin_lock(&devices_kset->list_lock);
  1084. list_move_tail(&dev->kobj.entry, &devices_kset->list);
  1085. spin_unlock(&devices_kset->list_lock);
  1086. }
  1087. /**
  1088. * device_create_file - create sysfs attribute file for device.
  1089. * @dev: device.
  1090. * @attr: device attribute descriptor.
  1091. */
  1092. int device_create_file(struct device *dev,
  1093. const struct device_attribute *attr)
  1094. {
  1095. int error = 0;
  1096. if (dev) {
  1097. WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
  1098. "Attribute %s: write permission without 'store'\n",
  1099. attr->attr.name);
  1100. WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
  1101. "Attribute %s: read permission without 'show'\n",
  1102. attr->attr.name);
  1103. error = sysfs_create_file(&dev->kobj, &attr->attr);
  1104. }
  1105. return error;
  1106. }
  1107. EXPORT_SYMBOL_GPL(device_create_file);
  1108. /**
  1109. * device_remove_file - remove sysfs attribute file.
  1110. * @dev: device.
  1111. * @attr: device attribute descriptor.
  1112. */
  1113. void device_remove_file(struct device *dev,
  1114. const struct device_attribute *attr)
  1115. {
  1116. if (dev)
  1117. sysfs_remove_file(&dev->kobj, &attr->attr);
  1118. }
  1119. EXPORT_SYMBOL_GPL(device_remove_file);
  1120. /**
  1121. * device_remove_file_self - remove sysfs attribute file from its own method.
  1122. * @dev: device.
  1123. * @attr: device attribute descriptor.
  1124. *
  1125. * See kernfs_remove_self() for details.
  1126. */
  1127. bool device_remove_file_self(struct device *dev,
  1128. const struct device_attribute *attr)
  1129. {
  1130. if (dev)
  1131. return sysfs_remove_file_self(&dev->kobj, &attr->attr);
  1132. else
  1133. return false;
  1134. }
  1135. EXPORT_SYMBOL_GPL(device_remove_file_self);
  1136. /**
  1137. * device_create_bin_file - create sysfs binary attribute file for device.
  1138. * @dev: device.
  1139. * @attr: device binary attribute descriptor.
  1140. */
  1141. int device_create_bin_file(struct device *dev,
  1142. const struct bin_attribute *attr)
  1143. {
  1144. int error = -EINVAL;
  1145. if (dev)
  1146. error = sysfs_create_bin_file(&dev->kobj, attr);
  1147. return error;
  1148. }
  1149. EXPORT_SYMBOL_GPL(device_create_bin_file);
  1150. /**
  1151. * device_remove_bin_file - remove sysfs binary attribute file
  1152. * @dev: device.
  1153. * @attr: device binary attribute descriptor.
  1154. */
  1155. void device_remove_bin_file(struct device *dev,
  1156. const struct bin_attribute *attr)
  1157. {
  1158. if (dev)
  1159. sysfs_remove_bin_file(&dev->kobj, attr);
  1160. }
  1161. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  1162. static void klist_children_get(struct klist_node *n)
  1163. {
  1164. struct device_private *p = to_device_private_parent(n);
  1165. struct device *dev = p->device;
  1166. get_device(dev);
  1167. }
  1168. static void klist_children_put(struct klist_node *n)
  1169. {
  1170. struct device_private *p = to_device_private_parent(n);
  1171. struct device *dev = p->device;
  1172. put_device(dev);
  1173. }
  1174. /**
  1175. * device_initialize - init device structure.
  1176. * @dev: device.
  1177. *
  1178. * This prepares the device for use by other layers by initializing
  1179. * its fields.
  1180. * It is the first half of device_register(), if called by
  1181. * that function, though it can also be called separately, so one
  1182. * may use @dev's fields. In particular, get_device()/put_device()
  1183. * may be used for reference counting of @dev after calling this
  1184. * function.
  1185. *
  1186. * All fields in @dev must be initialized by the caller to 0, except
  1187. * for those explicitly set to some other value. The simplest
  1188. * approach is to use kzalloc() to allocate the structure containing
  1189. * @dev.
  1190. *
  1191. * NOTE: Use put_device() to give up your reference instead of freeing
  1192. * @dev directly once you have called this function.
  1193. */
  1194. void device_initialize(struct device *dev)
  1195. {
  1196. dev->kobj.kset = devices_kset;
  1197. kobject_init(&dev->kobj, &device_ktype);
  1198. INIT_LIST_HEAD(&dev->dma_pools);
  1199. mutex_init(&dev->mutex);
  1200. lockdep_set_novalidate_class(&dev->mutex);
  1201. spin_lock_init(&dev->devres_lock);
  1202. INIT_LIST_HEAD(&dev->devres_head);
  1203. device_pm_init(dev);
  1204. set_dev_node(dev, -1);
  1205. #ifdef CONFIG_GENERIC_MSI_IRQ
  1206. INIT_LIST_HEAD(&dev->msi_list);
  1207. #endif
  1208. INIT_LIST_HEAD(&dev->links.consumers);
  1209. INIT_LIST_HEAD(&dev->links.suppliers);
  1210. dev->links.status = DL_DEV_NO_DRIVER;
  1211. }
  1212. EXPORT_SYMBOL_GPL(device_initialize);
  1213. struct kobject *virtual_device_parent(struct device *dev)
  1214. {
  1215. static struct kobject *virtual_dir = NULL;
  1216. if (!virtual_dir)
  1217. virtual_dir = kobject_create_and_add("virtual",
  1218. &devices_kset->kobj);
  1219. return virtual_dir;
  1220. }
  1221. struct class_dir {
  1222. struct kobject kobj;
  1223. struct class *class;
  1224. };
  1225. #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
  1226. static void class_dir_release(struct kobject *kobj)
  1227. {
  1228. struct class_dir *dir = to_class_dir(kobj);
  1229. kfree(dir);
  1230. }
  1231. static const
  1232. struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
  1233. {
  1234. struct class_dir *dir = to_class_dir(kobj);
  1235. return dir->class->ns_type;
  1236. }
  1237. static struct kobj_type class_dir_ktype = {
  1238. .release = class_dir_release,
  1239. .sysfs_ops = &kobj_sysfs_ops,
  1240. .child_ns_type = class_dir_child_ns_type
  1241. };
  1242. static struct kobject *
  1243. class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
  1244. {
  1245. struct class_dir *dir;
  1246. int retval;
  1247. dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  1248. if (!dir)
  1249. return NULL;
  1250. dir->class = class;
  1251. kobject_init(&dir->kobj, &class_dir_ktype);
  1252. dir->kobj.kset = &class->p->glue_dirs;
  1253. retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
  1254. if (retval < 0) {
  1255. kobject_put(&dir->kobj);
  1256. return NULL;
  1257. }
  1258. return &dir->kobj;
  1259. }
  1260. static DEFINE_MUTEX(gdp_mutex);
  1261. static struct kobject *get_device_parent(struct device *dev,
  1262. struct device *parent)
  1263. {
  1264. if (dev->class) {
  1265. struct kobject *kobj = NULL;
  1266. struct kobject *parent_kobj;
  1267. struct kobject *k;
  1268. #ifdef CONFIG_BLOCK
  1269. /* block disks show up in /sys/block */
  1270. if (sysfs_deprecated && dev->class == &block_class) {
  1271. if (parent && parent->class == &block_class)
  1272. return &parent->kobj;
  1273. return &block_class.p->subsys.kobj;
  1274. }
  1275. #endif
  1276. /*
  1277. * If we have no parent, we live in "virtual".
  1278. * Class-devices with a non class-device as parent, live
  1279. * in a "glue" directory to prevent namespace collisions.
  1280. */
  1281. if (parent == NULL)
  1282. parent_kobj = virtual_device_parent(dev);
  1283. else if (parent->class && !dev->class->ns_type)
  1284. return &parent->kobj;
  1285. else
  1286. parent_kobj = &parent->kobj;
  1287. mutex_lock(&gdp_mutex);
  1288. /* find our class-directory at the parent and reference it */
  1289. spin_lock(&dev->class->p->glue_dirs.list_lock);
  1290. list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
  1291. if (k->parent == parent_kobj) {
  1292. kobj = kobject_get(k);
  1293. break;
  1294. }
  1295. spin_unlock(&dev->class->p->glue_dirs.list_lock);
  1296. if (kobj) {
  1297. mutex_unlock(&gdp_mutex);
  1298. return kobj;
  1299. }
  1300. /* or create a new class-directory at the parent device */
  1301. k = class_dir_create_and_add(dev->class, parent_kobj);
  1302. /* do not emit an uevent for this simple "glue" directory */
  1303. mutex_unlock(&gdp_mutex);
  1304. return k;
  1305. }
  1306. /* subsystems can specify a default root directory for their devices */
  1307. if (!parent && dev->bus && dev->bus->dev_root)
  1308. return &dev->bus->dev_root->kobj;
  1309. if (parent)
  1310. return &parent->kobj;
  1311. return NULL;
  1312. }
  1313. static inline bool live_in_glue_dir(struct kobject *kobj,
  1314. struct device *dev)
  1315. {
  1316. if (!kobj || !dev->class ||
  1317. kobj->kset != &dev->class->p->glue_dirs)
  1318. return false;
  1319. return true;
  1320. }
  1321. static inline struct kobject *get_glue_dir(struct device *dev)
  1322. {
  1323. return dev->kobj.parent;
  1324. }
  1325. /*
  1326. * make sure cleaning up dir as the last step, we need to make
  1327. * sure .release handler of kobject is run with holding the
  1328. * global lock
  1329. */
  1330. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  1331. {
  1332. /* see if we live in a "glue" directory */
  1333. if (!live_in_glue_dir(glue_dir, dev))
  1334. return;
  1335. mutex_lock(&gdp_mutex);
  1336. kobject_put(glue_dir);
  1337. mutex_unlock(&gdp_mutex);
  1338. }
  1339. static int device_add_class_symlinks(struct device *dev)
  1340. {
  1341. struct device_node *of_node = dev_of_node(dev);
  1342. int error;
  1343. if (of_node) {
  1344. error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
  1345. if (error)
  1346. dev_warn(dev, "Error %d creating of_node link\n",error);
  1347. /* An error here doesn't warrant bringing down the device */
  1348. }
  1349. if (!dev->class)
  1350. return 0;
  1351. error = sysfs_create_link(&dev->kobj,
  1352. &dev->class->p->subsys.kobj,
  1353. "subsystem");
  1354. if (error)
  1355. goto out_devnode;
  1356. if (dev->parent && device_is_not_partition(dev)) {
  1357. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  1358. "device");
  1359. if (error)
  1360. goto out_subsys;
  1361. }
  1362. #ifdef CONFIG_BLOCK
  1363. /* /sys/block has directories and does not need symlinks */
  1364. if (sysfs_deprecated && dev->class == &block_class)
  1365. return 0;
  1366. #endif
  1367. /* link in the class directory pointing to the device */
  1368. error = sysfs_create_link(&dev->class->p->subsys.kobj,
  1369. &dev->kobj, dev_name(dev));
  1370. if (error)
  1371. goto out_device;
  1372. return 0;
  1373. out_device:
  1374. sysfs_remove_link(&dev->kobj, "device");
  1375. out_subsys:
  1376. sysfs_remove_link(&dev->kobj, "subsystem");
  1377. out_devnode:
  1378. sysfs_remove_link(&dev->kobj, "of_node");
  1379. return error;
  1380. }
  1381. static void device_remove_class_symlinks(struct device *dev)
  1382. {
  1383. if (dev_of_node(dev))
  1384. sysfs_remove_link(&dev->kobj, "of_node");
  1385. if (!dev->class)
  1386. return;
  1387. if (dev->parent && device_is_not_partition(dev))
  1388. sysfs_remove_link(&dev->kobj, "device");
  1389. sysfs_remove_link(&dev->kobj, "subsystem");
  1390. #ifdef CONFIG_BLOCK
  1391. if (sysfs_deprecated && dev->class == &block_class)
  1392. return;
  1393. #endif
  1394. sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
  1395. }
  1396. /**
  1397. * dev_set_name - set a device name
  1398. * @dev: device
  1399. * @fmt: format string for the device's name
  1400. */
  1401. int dev_set_name(struct device *dev, const char *fmt, ...)
  1402. {
  1403. va_list vargs;
  1404. int err;
  1405. va_start(vargs, fmt);
  1406. err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
  1407. va_end(vargs);
  1408. return err;
  1409. }
  1410. EXPORT_SYMBOL_GPL(dev_set_name);
  1411. /**
  1412. * device_to_dev_kobj - select a /sys/dev/ directory for the device
  1413. * @dev: device
  1414. *
  1415. * By default we select char/ for new entries. Setting class->dev_obj
  1416. * to NULL prevents an entry from being created. class->dev_kobj must
  1417. * be set (or cleared) before any devices are registered to the class
  1418. * otherwise device_create_sys_dev_entry() and
  1419. * device_remove_sys_dev_entry() will disagree about the presence of
  1420. * the link.
  1421. */
  1422. static struct kobject *device_to_dev_kobj(struct device *dev)
  1423. {
  1424. struct kobject *kobj;
  1425. if (dev->class)
  1426. kobj = dev->class->dev_kobj;
  1427. else
  1428. kobj = sysfs_dev_char_kobj;
  1429. return kobj;
  1430. }
  1431. static int device_create_sys_dev_entry(struct device *dev)
  1432. {
  1433. struct kobject *kobj = device_to_dev_kobj(dev);
  1434. int error = 0;
  1435. char devt_str[15];
  1436. if (kobj) {
  1437. format_dev_t(devt_str, dev->devt);
  1438. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  1439. }
  1440. return error;
  1441. }
  1442. static void device_remove_sys_dev_entry(struct device *dev)
  1443. {
  1444. struct kobject *kobj = device_to_dev_kobj(dev);
  1445. char devt_str[15];
  1446. if (kobj) {
  1447. format_dev_t(devt_str, dev->devt);
  1448. sysfs_remove_link(kobj, devt_str);
  1449. }
  1450. }
  1451. int device_private_init(struct device *dev)
  1452. {
  1453. dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
  1454. if (!dev->p)
  1455. return -ENOMEM;
  1456. dev->p->device = dev;
  1457. klist_init(&dev->p->klist_children, klist_children_get,
  1458. klist_children_put);
  1459. INIT_LIST_HEAD(&dev->p->deferred_probe);
  1460. return 0;
  1461. }
  1462. /**
  1463. * device_add - add device to device hierarchy.
  1464. * @dev: device.
  1465. *
  1466. * This is part 2 of device_register(), though may be called
  1467. * separately _iff_ device_initialize() has been called separately.
  1468. *
  1469. * This adds @dev to the kobject hierarchy via kobject_add(), adds it
  1470. * to the global and sibling lists for the device, then
  1471. * adds it to the other relevant subsystems of the driver model.
  1472. *
  1473. * Do not call this routine or device_register() more than once for
  1474. * any device structure. The driver model core is not designed to work
  1475. * with devices that get unregistered and then spring back to life.
  1476. * (Among other things, it's very hard to guarantee that all references
  1477. * to the previous incarnation of @dev have been dropped.) Allocate
  1478. * and register a fresh new struct device instead.
  1479. *
  1480. * NOTE: _Never_ directly free @dev after calling this function, even
  1481. * if it returned an error! Always use put_device() to give up your
  1482. * reference instead.
  1483. */
  1484. int device_add(struct device *dev)
  1485. {
  1486. struct device *parent;
  1487. struct kobject *kobj;
  1488. struct class_interface *class_intf;
  1489. int error = -EINVAL;
  1490. struct kobject *glue_dir = NULL;
  1491. dev = get_device(dev);
  1492. if (!dev)
  1493. goto done;
  1494. if (!dev->p) {
  1495. error = device_private_init(dev);
  1496. if (error)
  1497. goto done;
  1498. }
  1499. /*
  1500. * for statically allocated devices, which should all be converted
  1501. * some day, we need to initialize the name. We prevent reading back
  1502. * the name, and force the use of dev_name()
  1503. */
  1504. if (dev->init_name) {
  1505. dev_set_name(dev, "%s", dev->init_name);
  1506. dev->init_name = NULL;
  1507. }
  1508. /* subsystems can specify simple device enumeration */
  1509. if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
  1510. dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
  1511. if (!dev_name(dev)) {
  1512. error = -EINVAL;
  1513. goto name_error;
  1514. }
  1515. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1516. parent = get_device(dev->parent);
  1517. kobj = get_device_parent(dev, parent);
  1518. if (kobj)
  1519. dev->kobj.parent = kobj;
  1520. /* use parent numa_node */
  1521. if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
  1522. set_dev_node(dev, dev_to_node(parent));
  1523. /* first, register with generic layer. */
  1524. /* we require the name to be set before, and pass NULL */
  1525. error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
  1526. if (error) {
  1527. glue_dir = get_glue_dir(dev);
  1528. goto Error;
  1529. }
  1530. /* notify platform of device entry */
  1531. if (platform_notify)
  1532. platform_notify(dev);
  1533. error = device_create_file(dev, &dev_attr_uevent);
  1534. if (error)
  1535. goto attrError;
  1536. error = device_add_class_symlinks(dev);
  1537. if (error)
  1538. goto SymlinkError;
  1539. error = device_add_attrs(dev);
  1540. if (error)
  1541. goto AttrsError;
  1542. error = bus_add_device(dev);
  1543. if (error)
  1544. goto BusError;
  1545. error = dpm_sysfs_add(dev);
  1546. if (error)
  1547. goto DPMError;
  1548. device_pm_add(dev);
  1549. if (MAJOR(dev->devt)) {
  1550. error = device_create_file(dev, &dev_attr_dev);
  1551. if (error)
  1552. goto DevAttrError;
  1553. error = device_create_sys_dev_entry(dev);
  1554. if (error)
  1555. goto SysEntryError;
  1556. devtmpfs_create_node(dev);
  1557. }
  1558. /* Notify clients of device addition. This call must come
  1559. * after dpm_sysfs_add() and before kobject_uevent().
  1560. */
  1561. if (dev->bus)
  1562. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  1563. BUS_NOTIFY_ADD_DEVICE, dev);
  1564. kobject_uevent(&dev->kobj, KOBJ_ADD);
  1565. bus_probe_device(dev);
  1566. if (parent)
  1567. klist_add_tail(&dev->p->knode_parent,
  1568. &parent->p->klist_children);
  1569. if (dev->class) {
  1570. mutex_lock(&dev->class->p->mutex);
  1571. /* tie the class to the device */
  1572. klist_add_tail(&dev->knode_class,
  1573. &dev->class->p->klist_devices);
  1574. /* notify any interfaces that the device is here */
  1575. list_for_each_entry(class_intf,
  1576. &dev->class->p->interfaces, node)
  1577. if (class_intf->add_dev)
  1578. class_intf->add_dev(dev, class_intf);
  1579. mutex_unlock(&dev->class->p->mutex);
  1580. }
  1581. done:
  1582. put_device(dev);
  1583. return error;
  1584. SysEntryError:
  1585. if (MAJOR(dev->devt))
  1586. device_remove_file(dev, &dev_attr_dev);
  1587. DevAttrError:
  1588. device_pm_remove(dev);
  1589. dpm_sysfs_remove(dev);
  1590. DPMError:
  1591. bus_remove_device(dev);
  1592. BusError:
  1593. device_remove_attrs(dev);
  1594. AttrsError:
  1595. device_remove_class_symlinks(dev);
  1596. SymlinkError:
  1597. device_remove_file(dev, &dev_attr_uevent);
  1598. attrError:
  1599. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  1600. glue_dir = get_glue_dir(dev);
  1601. kobject_del(&dev->kobj);
  1602. Error:
  1603. cleanup_glue_dir(dev, glue_dir);
  1604. put_device(parent);
  1605. name_error:
  1606. kfree(dev->p);
  1607. dev->p = NULL;
  1608. goto done;
  1609. }
  1610. EXPORT_SYMBOL_GPL(device_add);
  1611. /**
  1612. * device_register - register a device with the system.
  1613. * @dev: pointer to the device structure
  1614. *
  1615. * This happens in two clean steps - initialize the device
  1616. * and add it to the system. The two steps can be called
  1617. * separately, but this is the easiest and most common.
  1618. * I.e. you should only call the two helpers separately if
  1619. * have a clearly defined need to use and refcount the device
  1620. * before it is added to the hierarchy.
  1621. *
  1622. * For more information, see the kerneldoc for device_initialize()
  1623. * and device_add().
  1624. *
  1625. * NOTE: _Never_ directly free @dev after calling this function, even
  1626. * if it returned an error! Always use put_device() to give up the
  1627. * reference initialized in this function instead.
  1628. */
  1629. int device_register(struct device *dev)
  1630. {
  1631. device_initialize(dev);
  1632. return device_add(dev);
  1633. }
  1634. EXPORT_SYMBOL_GPL(device_register);
  1635. /**
  1636. * get_device - increment reference count for device.
  1637. * @dev: device.
  1638. *
  1639. * This simply forwards the call to kobject_get(), though
  1640. * we do take care to provide for the case that we get a NULL
  1641. * pointer passed in.
  1642. */
  1643. struct device *get_device(struct device *dev)
  1644. {
  1645. return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
  1646. }
  1647. EXPORT_SYMBOL_GPL(get_device);
  1648. /**
  1649. * put_device - decrement reference count.
  1650. * @dev: device in question.
  1651. */
  1652. void put_device(struct device *dev)
  1653. {
  1654. /* might_sleep(); */
  1655. if (dev)
  1656. kobject_put(&dev->kobj);
  1657. }
  1658. EXPORT_SYMBOL_GPL(put_device);
  1659. /**
  1660. * device_del - delete device from system.
  1661. * @dev: device.
  1662. *
  1663. * This is the first part of the device unregistration
  1664. * sequence. This removes the device from the lists we control
  1665. * from here, has it removed from the other driver model
  1666. * subsystems it was added to in device_add(), and removes it
  1667. * from the kobject hierarchy.
  1668. *
  1669. * NOTE: this should be called manually _iff_ device_add() was
  1670. * also called manually.
  1671. */
  1672. void device_del(struct device *dev)
  1673. {
  1674. struct device *parent = dev->parent;
  1675. struct kobject *glue_dir = NULL;
  1676. struct class_interface *class_intf;
  1677. /* Notify clients of device removal. This call must come
  1678. * before dpm_sysfs_remove().
  1679. */
  1680. if (dev->bus)
  1681. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  1682. BUS_NOTIFY_DEL_DEVICE, dev);
  1683. dpm_sysfs_remove(dev);
  1684. if (parent)
  1685. klist_del(&dev->p->knode_parent);
  1686. if (MAJOR(dev->devt)) {
  1687. devtmpfs_delete_node(dev);
  1688. device_remove_sys_dev_entry(dev);
  1689. device_remove_file(dev, &dev_attr_dev);
  1690. }
  1691. if (dev->class) {
  1692. device_remove_class_symlinks(dev);
  1693. mutex_lock(&dev->class->p->mutex);
  1694. /* notify any interfaces that the device is now gone */
  1695. list_for_each_entry(class_intf,
  1696. &dev->class->p->interfaces, node)
  1697. if (class_intf->remove_dev)
  1698. class_intf->remove_dev(dev, class_intf);
  1699. /* remove the device from the class list */
  1700. klist_del(&dev->knode_class);
  1701. mutex_unlock(&dev->class->p->mutex);
  1702. }
  1703. device_remove_file(dev, &dev_attr_uevent);
  1704. device_remove_attrs(dev);
  1705. bus_remove_device(dev);
  1706. device_pm_remove(dev);
  1707. driver_deferred_probe_del(dev);
  1708. device_remove_properties(dev);
  1709. device_links_purge(dev);
  1710. /* Notify the platform of the removal, in case they
  1711. * need to do anything...
  1712. */
  1713. if (platform_notify_remove)
  1714. platform_notify_remove(dev);
  1715. if (dev->bus)
  1716. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  1717. BUS_NOTIFY_REMOVED_DEVICE, dev);
  1718. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  1719. glue_dir = get_glue_dir(dev);
  1720. kobject_del(&dev->kobj);
  1721. cleanup_glue_dir(dev, glue_dir);
  1722. put_device(parent);
  1723. }
  1724. EXPORT_SYMBOL_GPL(device_del);
  1725. /**
  1726. * device_unregister - unregister device from system.
  1727. * @dev: device going away.
  1728. *
  1729. * We do this in two parts, like we do device_register(). First,
  1730. * we remove it from all the subsystems with device_del(), then
  1731. * we decrement the reference count via put_device(). If that
  1732. * is the final reference count, the device will be cleaned up
  1733. * via device_release() above. Otherwise, the structure will
  1734. * stick around until the final reference to the device is dropped.
  1735. */
  1736. void device_unregister(struct device *dev)
  1737. {
  1738. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1739. device_del(dev);
  1740. put_device(dev);
  1741. }
  1742. EXPORT_SYMBOL_GPL(device_unregister);
  1743. static struct device *prev_device(struct klist_iter *i)
  1744. {
  1745. struct klist_node *n = klist_prev(i);
  1746. struct device *dev = NULL;
  1747. struct device_private *p;
  1748. if (n) {
  1749. p = to_device_private_parent(n);
  1750. dev = p->device;
  1751. }
  1752. return dev;
  1753. }
  1754. static struct device *next_device(struct klist_iter *i)
  1755. {
  1756. struct klist_node *n = klist_next(i);
  1757. struct device *dev = NULL;
  1758. struct device_private *p;
  1759. if (n) {
  1760. p = to_device_private_parent(n);
  1761. dev = p->device;
  1762. }
  1763. return dev;
  1764. }
  1765. /**
  1766. * device_get_devnode - path of device node file
  1767. * @dev: device
  1768. * @mode: returned file access mode
  1769. * @uid: returned file owner
  1770. * @gid: returned file group
  1771. * @tmp: possibly allocated string
  1772. *
  1773. * Return the relative path of a possible device node.
  1774. * Non-default names may need to allocate a memory to compose
  1775. * a name. This memory is returned in tmp and needs to be
  1776. * freed by the caller.
  1777. */
  1778. const char *device_get_devnode(struct device *dev,
  1779. umode_t *mode, kuid_t *uid, kgid_t *gid,
  1780. const char **tmp)
  1781. {
  1782. char *s;
  1783. *tmp = NULL;
  1784. /* the device type may provide a specific name */
  1785. if (dev->type && dev->type->devnode)
  1786. *tmp = dev->type->devnode(dev, mode, uid, gid);
  1787. if (*tmp)
  1788. return *tmp;
  1789. /* the class may provide a specific name */
  1790. if (dev->class && dev->class->devnode)
  1791. *tmp = dev->class->devnode(dev, mode);
  1792. if (*tmp)
  1793. return *tmp;
  1794. /* return name without allocation, tmp == NULL */
  1795. if (strchr(dev_name(dev), '!') == NULL)
  1796. return dev_name(dev);
  1797. /* replace '!' in the name with '/' */
  1798. s = kstrdup(dev_name(dev), GFP_KERNEL);
  1799. if (!s)
  1800. return NULL;
  1801. strreplace(s, '!', '/');
  1802. return *tmp = s;
  1803. }
  1804. /**
  1805. * device_for_each_child - device child iterator.
  1806. * @parent: parent struct device.
  1807. * @fn: function to be called for each device.
  1808. * @data: data for the callback.
  1809. *
  1810. * Iterate over @parent's child devices, and call @fn for each,
  1811. * passing it @data.
  1812. *
  1813. * We check the return of @fn each time. If it returns anything
  1814. * other than 0, we break out and return that value.
  1815. */
  1816. int device_for_each_child(struct device *parent, void *data,
  1817. int (*fn)(struct device *dev, void *data))
  1818. {
  1819. struct klist_iter i;
  1820. struct device *child;
  1821. int error = 0;
  1822. if (!parent->p)
  1823. return 0;
  1824. klist_iter_init(&parent->p->klist_children, &i);
  1825. while ((child = next_device(&i)) && !error)
  1826. error = fn(child, data);
  1827. klist_iter_exit(&i);
  1828. return error;
  1829. }
  1830. EXPORT_SYMBOL_GPL(device_for_each_child);
  1831. /**
  1832. * device_for_each_child_reverse - device child iterator in reversed order.
  1833. * @parent: parent struct device.
  1834. * @fn: function to be called for each device.
  1835. * @data: data for the callback.
  1836. *
  1837. * Iterate over @parent's child devices, and call @fn for each,
  1838. * passing it @data.
  1839. *
  1840. * We check the return of @fn each time. If it returns anything
  1841. * other than 0, we break out and return that value.
  1842. */
  1843. int device_for_each_child_reverse(struct device *parent, void *data,
  1844. int (*fn)(struct device *dev, void *data))
  1845. {
  1846. struct klist_iter i;
  1847. struct device *child;
  1848. int error = 0;
  1849. if (!parent->p)
  1850. return 0;
  1851. klist_iter_init(&parent->p->klist_children, &i);
  1852. while ((child = prev_device(&i)) && !error)
  1853. error = fn(child, data);
  1854. klist_iter_exit(&i);
  1855. return error;
  1856. }
  1857. EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
  1858. /**
  1859. * device_find_child - device iterator for locating a particular device.
  1860. * @parent: parent struct device
  1861. * @match: Callback function to check device
  1862. * @data: Data to pass to match function
  1863. *
  1864. * This is similar to the device_for_each_child() function above, but it
  1865. * returns a reference to a device that is 'found' for later use, as
  1866. * determined by the @match callback.
  1867. *
  1868. * The callback should return 0 if the device doesn't match and non-zero
  1869. * if it does. If the callback returns non-zero and a reference to the
  1870. * current device can be obtained, this function will return to the caller
  1871. * and not iterate over any more devices.
  1872. *
  1873. * NOTE: you will need to drop the reference with put_device() after use.
  1874. */
  1875. struct device *device_find_child(struct device *parent, void *data,
  1876. int (*match)(struct device *dev, void *data))
  1877. {
  1878. struct klist_iter i;
  1879. struct device *child;
  1880. if (!parent)
  1881. return NULL;
  1882. klist_iter_init(&parent->p->klist_children, &i);
  1883. while ((child = next_device(&i)))
  1884. if (match(child, data) && get_device(child))
  1885. break;
  1886. klist_iter_exit(&i);
  1887. return child;
  1888. }
  1889. EXPORT_SYMBOL_GPL(device_find_child);
  1890. int __init devices_init(void)
  1891. {
  1892. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  1893. if (!devices_kset)
  1894. return -ENOMEM;
  1895. dev_kobj = kobject_create_and_add("dev", NULL);
  1896. if (!dev_kobj)
  1897. goto dev_kobj_err;
  1898. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  1899. if (!sysfs_dev_block_kobj)
  1900. goto block_kobj_err;
  1901. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  1902. if (!sysfs_dev_char_kobj)
  1903. goto char_kobj_err;
  1904. return 0;
  1905. char_kobj_err:
  1906. kobject_put(sysfs_dev_block_kobj);
  1907. block_kobj_err:
  1908. kobject_put(dev_kobj);
  1909. dev_kobj_err:
  1910. kset_unregister(devices_kset);
  1911. return -ENOMEM;
  1912. }
  1913. static int device_check_offline(struct device *dev, void *not_used)
  1914. {
  1915. int ret;
  1916. ret = device_for_each_child(dev, NULL, device_check_offline);
  1917. if (ret)
  1918. return ret;
  1919. return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
  1920. }
  1921. /**
  1922. * device_offline - Prepare the device for hot-removal.
  1923. * @dev: Device to be put offline.
  1924. *
  1925. * Execute the device bus type's .offline() callback, if present, to prepare
  1926. * the device for a subsequent hot-removal. If that succeeds, the device must
  1927. * not be used until either it is removed or its bus type's .online() callback
  1928. * is executed.
  1929. *
  1930. * Call under device_hotplug_lock.
  1931. */
  1932. int device_offline(struct device *dev)
  1933. {
  1934. int ret;
  1935. if (dev->offline_disabled)
  1936. return -EPERM;
  1937. ret = device_for_each_child(dev, NULL, device_check_offline);
  1938. if (ret)
  1939. return ret;
  1940. device_lock(dev);
  1941. if (device_supports_offline(dev)) {
  1942. if (dev->offline) {
  1943. ret = 1;
  1944. } else {
  1945. ret = dev->bus->offline(dev);
  1946. if (!ret) {
  1947. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  1948. dev->offline = true;
  1949. }
  1950. }
  1951. }
  1952. device_unlock(dev);
  1953. return ret;
  1954. }
  1955. /**
  1956. * device_online - Put the device back online after successful device_offline().
  1957. * @dev: Device to be put back online.
  1958. *
  1959. * If device_offline() has been successfully executed for @dev, but the device
  1960. * has not been removed subsequently, execute its bus type's .online() callback
  1961. * to indicate that the device can be used again.
  1962. *
  1963. * Call under device_hotplug_lock.
  1964. */
  1965. int device_online(struct device *dev)
  1966. {
  1967. int ret = 0;
  1968. device_lock(dev);
  1969. if (device_supports_offline(dev)) {
  1970. if (dev->offline) {
  1971. ret = dev->bus->online(dev);
  1972. if (!ret) {
  1973. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  1974. dev->offline = false;
  1975. }
  1976. } else {
  1977. ret = 1;
  1978. }
  1979. }
  1980. device_unlock(dev);
  1981. return ret;
  1982. }
  1983. struct root_device {
  1984. struct device dev;
  1985. struct module *owner;
  1986. };
  1987. static inline struct root_device *to_root_device(struct device *d)
  1988. {
  1989. return container_of(d, struct root_device, dev);
  1990. }
  1991. static void root_device_release(struct device *dev)
  1992. {
  1993. kfree(to_root_device(dev));
  1994. }
  1995. /**
  1996. * __root_device_register - allocate and register a root device
  1997. * @name: root device name
  1998. * @owner: owner module of the root device, usually THIS_MODULE
  1999. *
  2000. * This function allocates a root device and registers it
  2001. * using device_register(). In order to free the returned
  2002. * device, use root_device_unregister().
  2003. *
  2004. * Root devices are dummy devices which allow other devices
  2005. * to be grouped under /sys/devices. Use this function to
  2006. * allocate a root device and then use it as the parent of
  2007. * any device which should appear under /sys/devices/{name}
  2008. *
  2009. * The /sys/devices/{name} directory will also contain a
  2010. * 'module' symlink which points to the @owner directory
  2011. * in sysfs.
  2012. *
  2013. * Returns &struct device pointer on success, or ERR_PTR() on error.
  2014. *
  2015. * Note: You probably want to use root_device_register().
  2016. */
  2017. struct device *__root_device_register(const char *name, struct module *owner)
  2018. {
  2019. struct root_device *root;
  2020. int err = -ENOMEM;
  2021. root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  2022. if (!root)
  2023. return ERR_PTR(err);
  2024. err = dev_set_name(&root->dev, "%s", name);
  2025. if (err) {
  2026. kfree(root);
  2027. return ERR_PTR(err);
  2028. }
  2029. root->dev.release = root_device_release;
  2030. err = device_register(&root->dev);
  2031. if (err) {
  2032. put_device(&root->dev);
  2033. return ERR_PTR(err);
  2034. }
  2035. #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
  2036. if (owner) {
  2037. struct module_kobject *mk = &owner->mkobj;
  2038. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  2039. if (err) {
  2040. device_unregister(&root->dev);
  2041. return ERR_PTR(err);
  2042. }
  2043. root->owner = owner;
  2044. }
  2045. #endif
  2046. return &root->dev;
  2047. }
  2048. EXPORT_SYMBOL_GPL(__root_device_register);
  2049. /**
  2050. * root_device_unregister - unregister and free a root device
  2051. * @dev: device going away
  2052. *
  2053. * This function unregisters and cleans up a device that was created by
  2054. * root_device_register().
  2055. */
  2056. void root_device_unregister(struct device *dev)
  2057. {
  2058. struct root_device *root = to_root_device(dev);
  2059. if (root->owner)
  2060. sysfs_remove_link(&root->dev.kobj, "module");
  2061. device_unregister(dev);
  2062. }
  2063. EXPORT_SYMBOL_GPL(root_device_unregister);
  2064. static void device_create_release(struct device *dev)
  2065. {
  2066. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  2067. kfree(dev);
  2068. }
  2069. static struct device *
  2070. device_create_groups_vargs(struct class *class, struct device *parent,
  2071. dev_t devt, void *drvdata,
  2072. const struct attribute_group **groups,
  2073. const char *fmt, va_list args)
  2074. {
  2075. struct device *dev = NULL;
  2076. int retval = -ENODEV;
  2077. if (class == NULL || IS_ERR(class))
  2078. goto error;
  2079. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2080. if (!dev) {
  2081. retval = -ENOMEM;
  2082. goto error;
  2083. }
  2084. device_initialize(dev);
  2085. dev->devt = devt;
  2086. dev->class = class;
  2087. dev->parent = parent;
  2088. dev->groups = groups;
  2089. dev->release = device_create_release;
  2090. dev_set_drvdata(dev, drvdata);
  2091. retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  2092. if (retval)
  2093. goto error;
  2094. retval = device_add(dev);
  2095. if (retval)
  2096. goto error;
  2097. return dev;
  2098. error:
  2099. put_device(dev);
  2100. return ERR_PTR(retval);
  2101. }
  2102. /**
  2103. * device_create_vargs - creates a device and registers it with sysfs
  2104. * @class: pointer to the struct class that this device should be registered to
  2105. * @parent: pointer to the parent struct device of this new device, if any
  2106. * @devt: the dev_t for the char device to be added
  2107. * @drvdata: the data to be added to the device for callbacks
  2108. * @fmt: string for the device's name
  2109. * @args: va_list for the device's name
  2110. *
  2111. * This function can be used by char device classes. A struct device
  2112. * will be created in sysfs, registered to the specified class.
  2113. *
  2114. * A "dev" file will be created, showing the dev_t for the device, if
  2115. * the dev_t is not 0,0.
  2116. * If a pointer to a parent struct device is passed in, the newly created
  2117. * struct device will be a child of that device in sysfs.
  2118. * The pointer to the struct device will be returned from the call.
  2119. * Any further sysfs files that might be required can be created using this
  2120. * pointer.
  2121. *
  2122. * Returns &struct device pointer on success, or ERR_PTR() on error.
  2123. *
  2124. * Note: the struct class passed to this function must have previously
  2125. * been created with a call to class_create().
  2126. */
  2127. struct device *device_create_vargs(struct class *class, struct device *parent,
  2128. dev_t devt, void *drvdata, const char *fmt,
  2129. va_list args)
  2130. {
  2131. return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
  2132. fmt, args);
  2133. }
  2134. EXPORT_SYMBOL_GPL(device_create_vargs);
  2135. /**
  2136. * device_create - creates a device and registers it with sysfs
  2137. * @class: pointer to the struct class that this device should be registered to
  2138. * @parent: pointer to the parent struct device of this new device, if any
  2139. * @devt: the dev_t for the char device to be added
  2140. * @drvdata: the data to be added to the device for callbacks
  2141. * @fmt: string for the device's name
  2142. *
  2143. * This function can be used by char device classes. A struct device
  2144. * will be created in sysfs, registered to the specified class.
  2145. *
  2146. * A "dev" file will be created, showing the dev_t for the device, if
  2147. * the dev_t is not 0,0.
  2148. * If a pointer to a parent struct device is passed in, the newly created
  2149. * struct device will be a child of that device in sysfs.
  2150. * The pointer to the struct device will be returned from the call.
  2151. * Any further sysfs files that might be required can be created using this
  2152. * pointer.
  2153. *
  2154. * Returns &struct device pointer on success, or ERR_PTR() on error.
  2155. *
  2156. * Note: the struct class passed to this function must have previously
  2157. * been created with a call to class_create().
  2158. */
  2159. struct device *device_create(struct class *class, struct device *parent,
  2160. dev_t devt, void *drvdata, const char *fmt, ...)
  2161. {
  2162. va_list vargs;
  2163. struct device *dev;
  2164. va_start(vargs, fmt);
  2165. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  2166. va_end(vargs);
  2167. return dev;
  2168. }
  2169. EXPORT_SYMBOL_GPL(device_create);
  2170. /**
  2171. * device_create_with_groups - creates a device and registers it with sysfs
  2172. * @class: pointer to the struct class that this device should be registered to
  2173. * @parent: pointer to the parent struct device of this new device, if any
  2174. * @devt: the dev_t for the char device to be added
  2175. * @drvdata: the data to be added to the device for callbacks
  2176. * @groups: NULL-terminated list of attribute groups to be created
  2177. * @fmt: string for the device's name
  2178. *
  2179. * This function can be used by char device classes. A struct device
  2180. * will be created in sysfs, registered to the specified class.
  2181. * Additional attributes specified in the groups parameter will also
  2182. * be created automatically.
  2183. *
  2184. * A "dev" file will be created, showing the dev_t for the device, if
  2185. * the dev_t is not 0,0.
  2186. * If a pointer to a parent struct device is passed in, the newly created
  2187. * struct device will be a child of that device in sysfs.
  2188. * The pointer to the struct device will be returned from the call.
  2189. * Any further sysfs files that might be required can be created using this
  2190. * pointer.
  2191. *
  2192. * Returns &struct device pointer on success, or ERR_PTR() on error.
  2193. *
  2194. * Note: the struct class passed to this function must have previously
  2195. * been created with a call to class_create().
  2196. */
  2197. struct device *device_create_with_groups(struct class *class,
  2198. struct device *parent, dev_t devt,
  2199. void *drvdata,
  2200. const struct attribute_group **groups,
  2201. const char *fmt, ...)
  2202. {
  2203. va_list vargs;
  2204. struct device *dev;
  2205. va_start(vargs, fmt);
  2206. dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
  2207. fmt, vargs);
  2208. va_end(vargs);
  2209. return dev;
  2210. }
  2211. EXPORT_SYMBOL_GPL(device_create_with_groups);
  2212. static int __match_devt(struct device *dev, const void *data)
  2213. {
  2214. const dev_t *devt = data;
  2215. return dev->devt == *devt;
  2216. }
  2217. /**
  2218. * device_destroy - removes a device that was created with device_create()
  2219. * @class: pointer to the struct class that this device was registered with
  2220. * @devt: the dev_t of the device that was previously registered
  2221. *
  2222. * This call unregisters and cleans up a device that was created with a
  2223. * call to device_create().
  2224. */
  2225. void device_destroy(struct class *class, dev_t devt)
  2226. {
  2227. struct device *dev;
  2228. dev = class_find_device(class, NULL, &devt, __match_devt);
  2229. if (dev) {
  2230. put_device(dev);
  2231. device_unregister(dev);
  2232. }
  2233. }
  2234. EXPORT_SYMBOL_GPL(device_destroy);
  2235. /**
  2236. * device_rename - renames a device
  2237. * @dev: the pointer to the struct device to be renamed
  2238. * @new_name: the new name of the device
  2239. *
  2240. * It is the responsibility of the caller to provide mutual
  2241. * exclusion between two different calls of device_rename
  2242. * on the same device to ensure that new_name is valid and
  2243. * won't conflict with other devices.
  2244. *
  2245. * Note: Don't call this function. Currently, the networking layer calls this
  2246. * function, but that will change. The following text from Kay Sievers offers
  2247. * some insight:
  2248. *
  2249. * Renaming devices is racy at many levels, symlinks and other stuff are not
  2250. * replaced atomically, and you get a "move" uevent, but it's not easy to
  2251. * connect the event to the old and new device. Device nodes are not renamed at
  2252. * all, there isn't even support for that in the kernel now.
  2253. *
  2254. * In the meantime, during renaming, your target name might be taken by another
  2255. * driver, creating conflicts. Or the old name is taken directly after you
  2256. * renamed it -- then you get events for the same DEVPATH, before you even see
  2257. * the "move" event. It's just a mess, and nothing new should ever rely on
  2258. * kernel device renaming. Besides that, it's not even implemented now for
  2259. * other things than (driver-core wise very simple) network devices.
  2260. *
  2261. * We are currently about to change network renaming in udev to completely
  2262. * disallow renaming of devices in the same namespace as the kernel uses,
  2263. * because we can't solve the problems properly, that arise with swapping names
  2264. * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
  2265. * be allowed to some other name than eth[0-9]*, for the aforementioned
  2266. * reasons.
  2267. *
  2268. * Make up a "real" name in the driver before you register anything, or add
  2269. * some other attributes for userspace to find the device, or use udev to add
  2270. * symlinks -- but never rename kernel devices later, it's a complete mess. We
  2271. * don't even want to get into that and try to implement the missing pieces in
  2272. * the core. We really have other pieces to fix in the driver core mess. :)
  2273. */
  2274. int device_rename(struct device *dev, const char *new_name)
  2275. {
  2276. struct kobject *kobj = &dev->kobj;
  2277. char *old_device_name = NULL;
  2278. int error;
  2279. dev = get_device(dev);
  2280. if (!dev)
  2281. return -EINVAL;
  2282. dev_dbg(dev, "renaming to %s\n", new_name);
  2283. old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
  2284. if (!old_device_name) {
  2285. error = -ENOMEM;
  2286. goto out;
  2287. }
  2288. if (dev->class) {
  2289. error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
  2290. kobj, old_device_name,
  2291. new_name, kobject_namespace(kobj));
  2292. if (error)
  2293. goto out;
  2294. }
  2295. error = kobject_rename(kobj, new_name);
  2296. if (error)
  2297. goto out;
  2298. out:
  2299. put_device(dev);
  2300. kfree(old_device_name);
  2301. return error;
  2302. }
  2303. EXPORT_SYMBOL_GPL(device_rename);
  2304. static int device_move_class_links(struct device *dev,
  2305. struct device *old_parent,
  2306. struct device *new_parent)
  2307. {
  2308. int error = 0;
  2309. if (old_parent)
  2310. sysfs_remove_link(&dev->kobj, "device");
  2311. if (new_parent)
  2312. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  2313. "device");
  2314. return error;
  2315. }
  2316. /**
  2317. * device_move - moves a device to a new parent
  2318. * @dev: the pointer to the struct device to be moved
  2319. * @new_parent: the new parent of the device (can by NULL)
  2320. * @dpm_order: how to reorder the dpm_list
  2321. */
  2322. int device_move(struct device *dev, struct device *new_parent,
  2323. enum dpm_order dpm_order)
  2324. {
  2325. int error;
  2326. struct device *old_parent;
  2327. struct kobject *new_parent_kobj;
  2328. dev = get_device(dev);
  2329. if (!dev)
  2330. return -EINVAL;
  2331. device_pm_lock();
  2332. new_parent = get_device(new_parent);
  2333. new_parent_kobj = get_device_parent(dev, new_parent);
  2334. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  2335. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  2336. error = kobject_move(&dev->kobj, new_parent_kobj);
  2337. if (error) {
  2338. cleanup_glue_dir(dev, new_parent_kobj);
  2339. put_device(new_parent);
  2340. goto out;
  2341. }
  2342. old_parent = dev->parent;
  2343. dev->parent = new_parent;
  2344. if (old_parent)
  2345. klist_remove(&dev->p->knode_parent);
  2346. if (new_parent) {
  2347. klist_add_tail(&dev->p->knode_parent,
  2348. &new_parent->p->klist_children);
  2349. set_dev_node(dev, dev_to_node(new_parent));
  2350. }
  2351. if (dev->class) {
  2352. error = device_move_class_links(dev, old_parent, new_parent);
  2353. if (error) {
  2354. /* We ignore errors on cleanup since we're hosed anyway... */
  2355. device_move_class_links(dev, new_parent, old_parent);
  2356. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  2357. if (new_parent)
  2358. klist_remove(&dev->p->knode_parent);
  2359. dev->parent = old_parent;
  2360. if (old_parent) {
  2361. klist_add_tail(&dev->p->knode_parent,
  2362. &old_parent->p->klist_children);
  2363. set_dev_node(dev, dev_to_node(old_parent));
  2364. }
  2365. }
  2366. cleanup_glue_dir(dev, new_parent_kobj);
  2367. put_device(new_parent);
  2368. goto out;
  2369. }
  2370. }
  2371. switch (dpm_order) {
  2372. case DPM_ORDER_NONE:
  2373. break;
  2374. case DPM_ORDER_DEV_AFTER_PARENT:
  2375. device_pm_move_after(dev, new_parent);
  2376. devices_kset_move_after(dev, new_parent);
  2377. break;
  2378. case DPM_ORDER_PARENT_BEFORE_DEV:
  2379. device_pm_move_before(new_parent, dev);
  2380. devices_kset_move_before(new_parent, dev);
  2381. break;
  2382. case DPM_ORDER_DEV_LAST:
  2383. device_pm_move_last(dev);
  2384. devices_kset_move_last(dev);
  2385. break;
  2386. }
  2387. put_device(old_parent);
  2388. out:
  2389. device_pm_unlock();
  2390. put_device(dev);
  2391. return error;
  2392. }
  2393. EXPORT_SYMBOL_GPL(device_move);
  2394. /**
  2395. * device_shutdown - call ->shutdown() on each device to shutdown.
  2396. */
  2397. void device_shutdown(void)
  2398. {
  2399. struct device *dev, *parent;
  2400. spin_lock(&devices_kset->list_lock);
  2401. /*
  2402. * Walk the devices list backward, shutting down each in turn.
  2403. * Beware that device unplug events may also start pulling
  2404. * devices offline, even as the system is shutting down.
  2405. */
  2406. while (!list_empty(&devices_kset->list)) {
  2407. dev = list_entry(devices_kset->list.prev, struct device,
  2408. kobj.entry);
  2409. /*
  2410. * hold reference count of device's parent to
  2411. * prevent it from being freed because parent's
  2412. * lock is to be held
  2413. */
  2414. parent = get_device(dev->parent);
  2415. get_device(dev);
  2416. /*
  2417. * Make sure the device is off the kset list, in the
  2418. * event that dev->*->shutdown() doesn't remove it.
  2419. */
  2420. list_del_init(&dev->kobj.entry);
  2421. spin_unlock(&devices_kset->list_lock);
  2422. /* hold lock to avoid race with probe/release */
  2423. if (parent)
  2424. device_lock(parent);
  2425. device_lock(dev);
  2426. /* Don't allow any more runtime suspends */
  2427. pm_runtime_get_noresume(dev);
  2428. pm_runtime_barrier(dev);
  2429. if (dev->class && dev->class->shutdown_pre) {
  2430. if (initcall_debug)
  2431. dev_info(dev, "shutdown_pre\n");
  2432. dev->class->shutdown_pre(dev);
  2433. }
  2434. if (dev->bus && dev->bus->shutdown) {
  2435. if (initcall_debug)
  2436. dev_info(dev, "shutdown\n");
  2437. dev->bus->shutdown(dev);
  2438. } else if (dev->driver && dev->driver->shutdown) {
  2439. if (initcall_debug)
  2440. dev_info(dev, "shutdown\n");
  2441. dev->driver->shutdown(dev);
  2442. }
  2443. device_unlock(dev);
  2444. if (parent)
  2445. device_unlock(parent);
  2446. put_device(dev);
  2447. put_device(parent);
  2448. spin_lock(&devices_kset->list_lock);
  2449. }
  2450. spin_unlock(&devices_kset->list_lock);
  2451. }
  2452. /*
  2453. * Device logging functions
  2454. */
  2455. #ifdef CONFIG_PRINTK
  2456. static int
  2457. create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
  2458. {
  2459. const char *subsys;
  2460. size_t pos = 0;
  2461. if (dev->class)
  2462. subsys = dev->class->name;
  2463. else if (dev->bus)
  2464. subsys = dev->bus->name;
  2465. else
  2466. return 0;
  2467. pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
  2468. if (pos >= hdrlen)
  2469. goto overflow;
  2470. /*
  2471. * Add device identifier DEVICE=:
  2472. * b12:8 block dev_t
  2473. * c127:3 char dev_t
  2474. * n8 netdev ifindex
  2475. * +sound:card0 subsystem:devname
  2476. */
  2477. if (MAJOR(dev->devt)) {
  2478. char c;
  2479. if (strcmp(subsys, "block") == 0)
  2480. c = 'b';
  2481. else
  2482. c = 'c';
  2483. pos++;
  2484. pos += snprintf(hdr + pos, hdrlen - pos,
  2485. "DEVICE=%c%u:%u",
  2486. c, MAJOR(dev->devt), MINOR(dev->devt));
  2487. } else if (strcmp(subsys, "net") == 0) {
  2488. struct net_device *net = to_net_dev(dev);
  2489. pos++;
  2490. pos += snprintf(hdr + pos, hdrlen - pos,
  2491. "DEVICE=n%u", net->ifindex);
  2492. } else {
  2493. pos++;
  2494. pos += snprintf(hdr + pos, hdrlen - pos,
  2495. "DEVICE=+%s:%s", subsys, dev_name(dev));
  2496. }
  2497. if (pos >= hdrlen)
  2498. goto overflow;
  2499. return pos;
  2500. overflow:
  2501. dev_WARN(dev, "device/subsystem name too long");
  2502. return 0;
  2503. }
  2504. int dev_vprintk_emit(int level, const struct device *dev,
  2505. const char *fmt, va_list args)
  2506. {
  2507. char hdr[128];
  2508. size_t hdrlen;
  2509. hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
  2510. return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
  2511. }
  2512. EXPORT_SYMBOL(dev_vprintk_emit);
  2513. int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
  2514. {
  2515. va_list args;
  2516. int r;
  2517. va_start(args, fmt);
  2518. r = dev_vprintk_emit(level, dev, fmt, args);
  2519. va_end(args);
  2520. return r;
  2521. }
  2522. EXPORT_SYMBOL(dev_printk_emit);
  2523. static void __dev_printk(const char *level, const struct device *dev,
  2524. struct va_format *vaf)
  2525. {
  2526. if (dev)
  2527. dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
  2528. dev_driver_string(dev), dev_name(dev), vaf);
  2529. else
  2530. printk("%s(NULL device *): %pV", level, vaf);
  2531. }
  2532. void dev_printk(const char *level, const struct device *dev,
  2533. const char *fmt, ...)
  2534. {
  2535. struct va_format vaf;
  2536. va_list args;
  2537. va_start(args, fmt);
  2538. vaf.fmt = fmt;
  2539. vaf.va = &args;
  2540. __dev_printk(level, dev, &vaf);
  2541. va_end(args);
  2542. }
  2543. EXPORT_SYMBOL(dev_printk);
  2544. #define define_dev_printk_level(func, kern_level) \
  2545. void func(const struct device *dev, const char *fmt, ...) \
  2546. { \
  2547. struct va_format vaf; \
  2548. va_list args; \
  2549. \
  2550. va_start(args, fmt); \
  2551. \
  2552. vaf.fmt = fmt; \
  2553. vaf.va = &args; \
  2554. \
  2555. __dev_printk(kern_level, dev, &vaf); \
  2556. \
  2557. va_end(args); \
  2558. } \
  2559. EXPORT_SYMBOL(func);
  2560. define_dev_printk_level(dev_emerg, KERN_EMERG);
  2561. define_dev_printk_level(dev_alert, KERN_ALERT);
  2562. define_dev_printk_level(dev_crit, KERN_CRIT);
  2563. define_dev_printk_level(dev_err, KERN_ERR);
  2564. define_dev_printk_level(dev_warn, KERN_WARNING);
  2565. define_dev_printk_level(dev_notice, KERN_NOTICE);
  2566. define_dev_printk_level(_dev_info, KERN_INFO);
  2567. #endif
  2568. static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
  2569. {
  2570. return fwnode && !IS_ERR(fwnode->secondary);
  2571. }
  2572. /**
  2573. * set_primary_fwnode - Change the primary firmware node of a given device.
  2574. * @dev: Device to handle.
  2575. * @fwnode: New primary firmware node of the device.
  2576. *
  2577. * Set the device's firmware node pointer to @fwnode, but if a secondary
  2578. * firmware node of the device is present, preserve it.
  2579. */
  2580. void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
  2581. {
  2582. if (fwnode) {
  2583. struct fwnode_handle *fn = dev->fwnode;
  2584. if (fwnode_is_primary(fn))
  2585. fn = fn->secondary;
  2586. if (fn) {
  2587. WARN_ON(fwnode->secondary);
  2588. fwnode->secondary = fn;
  2589. }
  2590. dev->fwnode = fwnode;
  2591. } else {
  2592. dev->fwnode = fwnode_is_primary(dev->fwnode) ?
  2593. dev->fwnode->secondary : NULL;
  2594. }
  2595. }
  2596. EXPORT_SYMBOL_GPL(set_primary_fwnode);
  2597. /**
  2598. * set_secondary_fwnode - Change the secondary firmware node of a given device.
  2599. * @dev: Device to handle.
  2600. * @fwnode: New secondary firmware node of the device.
  2601. *
  2602. * If a primary firmware node of the device is present, set its secondary
  2603. * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
  2604. * @fwnode.
  2605. */
  2606. void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
  2607. {
  2608. if (fwnode)
  2609. fwnode->secondary = ERR_PTR(-ENODEV);
  2610. if (fwnode_is_primary(dev->fwnode))
  2611. dev->fwnode->secondary = fwnode;
  2612. else
  2613. dev->fwnode = fwnode;
  2614. }
  2615. /**
  2616. * device_set_of_node_from_dev - reuse device-tree node of another device
  2617. * @dev: device whose device-tree node is being set
  2618. * @dev2: device whose device-tree node is being reused
  2619. *
  2620. * Takes another reference to the new device-tree node after first dropping
  2621. * any reference held to the old node.
  2622. */
  2623. void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
  2624. {
  2625. of_node_put(dev->of_node);
  2626. dev->of_node = of_node_get(dev2->of_node);
  2627. dev->of_node_reused = true;
  2628. }
  2629. EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);