core.c 81 KB

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