core.c 80 KB

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