core.c 81 KB

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