core.c 80 KB

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