swnode.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Software nodes for the firmware node framework.
  4. *
  5. * Copyright (C) 2018, Intel Corporation
  6. * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
  7. */
  8. #include <linux/container_of.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/export.h>
  12. #include <linux/idr.h>
  13. #include <linux/init.h>
  14. #include <linux/kobject.h>
  15. #include <linux/kstrtox.h>
  16. #include <linux/list.h>
  17. #include <linux/property.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/string.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/types.h>
  23. #include "base.h"
  24. struct swnode {
  25. struct kobject kobj;
  26. struct fwnode_handle fwnode;
  27. const struct software_node *node;
  28. int id;
  29. /* hierarchy */
  30. struct ida child_ids;
  31. struct list_head entry;
  32. struct list_head children;
  33. struct swnode *parent;
  34. unsigned int allocated:1;
  35. unsigned int managed:1;
  36. };
  37. static DEFINE_IDA(swnode_root_ids);
  38. static struct kset *swnode_kset;
  39. #define kobj_to_swnode(_kobj_) container_of(_kobj_, struct swnode, kobj)
  40. static const struct fwnode_operations software_node_ops;
  41. bool is_software_node(const struct fwnode_handle *fwnode)
  42. {
  43. return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &software_node_ops;
  44. }
  45. EXPORT_SYMBOL_GPL(is_software_node);
  46. #define to_swnode(__fwnode) \
  47. ({ \
  48. typeof(__fwnode) __to_swnode_fwnode = __fwnode; \
  49. \
  50. is_software_node(__to_swnode_fwnode) ? \
  51. container_of(__to_swnode_fwnode, \
  52. struct swnode, fwnode) : NULL; \
  53. })
  54. static inline struct swnode *dev_to_swnode(struct device *dev)
  55. {
  56. struct fwnode_handle *fwnode = dev_fwnode(dev);
  57. if (!fwnode)
  58. return NULL;
  59. if (!is_software_node(fwnode))
  60. fwnode = fwnode->secondary;
  61. return to_swnode(fwnode);
  62. }
  63. static struct swnode *
  64. software_node_to_swnode(const struct software_node *node)
  65. {
  66. struct swnode *swnode = NULL;
  67. struct kobject *k;
  68. if (!node)
  69. return NULL;
  70. spin_lock(&swnode_kset->list_lock);
  71. list_for_each_entry(k, &swnode_kset->list, entry) {
  72. swnode = kobj_to_swnode(k);
  73. if (swnode->node == node)
  74. break;
  75. swnode = NULL;
  76. }
  77. spin_unlock(&swnode_kset->list_lock);
  78. return swnode;
  79. }
  80. const struct software_node *to_software_node(const struct fwnode_handle *fwnode)
  81. {
  82. const struct swnode *swnode = to_swnode(fwnode);
  83. return swnode ? swnode->node : NULL;
  84. }
  85. EXPORT_SYMBOL_GPL(to_software_node);
  86. struct fwnode_handle *software_node_fwnode(const struct software_node *node)
  87. {
  88. struct swnode *swnode = software_node_to_swnode(node);
  89. return swnode ? &swnode->fwnode : NULL;
  90. }
  91. EXPORT_SYMBOL_GPL(software_node_fwnode);
  92. /* -------------------------------------------------------------------------- */
  93. /* property_entry processing */
  94. static const struct property_entry *
  95. property_entry_get(const struct property_entry *prop, const char *name)
  96. {
  97. if (!prop)
  98. return NULL;
  99. for (; prop->name; prop++)
  100. if (!strcmp(name, prop->name))
  101. return prop;
  102. return NULL;
  103. }
  104. static const void *property_get_pointer(const struct property_entry *prop)
  105. {
  106. if (!prop->length)
  107. return NULL;
  108. return prop->is_inline ? &prop->value : prop->pointer;
  109. }
  110. static const void *property_entry_find(const struct property_entry *props,
  111. const char *propname, size_t length)
  112. {
  113. const struct property_entry *prop;
  114. const void *pointer;
  115. prop = property_entry_get(props, propname);
  116. if (!prop)
  117. return ERR_PTR(-EINVAL);
  118. pointer = property_get_pointer(prop);
  119. if (!pointer)
  120. return ERR_PTR(-ENODATA);
  121. if (length > prop->length)
  122. return ERR_PTR(-EOVERFLOW);
  123. return pointer;
  124. }
  125. static int
  126. property_entry_count_elems_of_size(const struct property_entry *props,
  127. const char *propname, size_t length)
  128. {
  129. const struct property_entry *prop;
  130. prop = property_entry_get(props, propname);
  131. if (!prop)
  132. return -EINVAL;
  133. return prop->length / length;
  134. }
  135. static int property_entry_read_int_array(const struct property_entry *props,
  136. const char *name,
  137. unsigned int elem_size, void *val,
  138. size_t nval)
  139. {
  140. const void *pointer;
  141. size_t length;
  142. if (!val)
  143. return property_entry_count_elems_of_size(props, name,
  144. elem_size);
  145. if (!is_power_of_2(elem_size) || elem_size > sizeof(u64))
  146. return -ENXIO;
  147. length = nval * elem_size;
  148. pointer = property_entry_find(props, name, length);
  149. if (IS_ERR(pointer))
  150. return PTR_ERR(pointer);
  151. memcpy(val, pointer, length);
  152. return 0;
  153. }
  154. static int property_entry_read_string_array(const struct property_entry *props,
  155. const char *propname,
  156. const char **strings, size_t nval)
  157. {
  158. const void *pointer;
  159. size_t length;
  160. int array_len;
  161. /* Find out the array length. */
  162. array_len = property_entry_count_elems_of_size(props, propname,
  163. sizeof(const char *));
  164. if (array_len < 0)
  165. return array_len;
  166. /* Return how many there are if strings is NULL. */
  167. if (!strings)
  168. return array_len;
  169. array_len = min_t(size_t, nval, array_len);
  170. length = array_len * sizeof(*strings);
  171. pointer = property_entry_find(props, propname, length);
  172. if (IS_ERR(pointer))
  173. return PTR_ERR(pointer);
  174. memcpy(strings, pointer, length);
  175. return array_len;
  176. }
  177. static void property_entry_free_data(const struct property_entry *p)
  178. {
  179. const char * const *src_str;
  180. size_t i, nval;
  181. if (p->type == DEV_PROP_STRING) {
  182. src_str = property_get_pointer(p);
  183. nval = p->length / sizeof(*src_str);
  184. for (i = 0; i < nval; i++)
  185. kfree(src_str[i]);
  186. }
  187. if (!p->is_inline)
  188. kfree(p->pointer);
  189. kfree(p->name);
  190. }
  191. static bool property_copy_string_array(const char **dst_ptr,
  192. const char * const *src_ptr,
  193. size_t nval)
  194. {
  195. int i;
  196. for (i = 0; i < nval; i++) {
  197. dst_ptr[i] = kstrdup(src_ptr[i], GFP_KERNEL);
  198. if (!dst_ptr[i] && src_ptr[i]) {
  199. while (--i >= 0)
  200. kfree(dst_ptr[i]);
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. static int property_entry_copy_data(struct property_entry *dst,
  207. const struct property_entry *src)
  208. {
  209. const void *pointer = property_get_pointer(src);
  210. void *dst_ptr;
  211. size_t nval;
  212. /*
  213. * Properties with no data should not be marked as stored
  214. * out of line.
  215. */
  216. if (!src->is_inline && !src->length)
  217. return -ENODATA;
  218. /*
  219. * Reference properties are never stored inline as
  220. * they are too big.
  221. */
  222. if (src->type == DEV_PROP_REF && src->is_inline)
  223. return -EINVAL;
  224. if (src->length <= sizeof(dst->value)) {
  225. dst_ptr = &dst->value;
  226. dst->is_inline = true;
  227. } else {
  228. dst_ptr = kmalloc(src->length, GFP_KERNEL);
  229. if (!dst_ptr)
  230. return -ENOMEM;
  231. dst->pointer = dst_ptr;
  232. }
  233. if (src->type == DEV_PROP_STRING) {
  234. nval = src->length / sizeof(const char *);
  235. if (!property_copy_string_array(dst_ptr, pointer, nval)) {
  236. if (!dst->is_inline)
  237. kfree(dst->pointer);
  238. return -ENOMEM;
  239. }
  240. } else {
  241. memcpy(dst_ptr, pointer, src->length);
  242. }
  243. dst->length = src->length;
  244. dst->type = src->type;
  245. dst->name = kstrdup(src->name, GFP_KERNEL);
  246. if (!dst->name) {
  247. property_entry_free_data(dst);
  248. return -ENOMEM;
  249. }
  250. return 0;
  251. }
  252. /**
  253. * property_entries_dup - duplicate array of properties
  254. * @properties: array of properties to copy
  255. *
  256. * This function creates a deep copy of the given NULL-terminated array
  257. * of property entries.
  258. */
  259. struct property_entry *
  260. property_entries_dup(const struct property_entry *properties)
  261. {
  262. struct property_entry *p;
  263. int i, n = 0;
  264. int ret;
  265. if (!properties)
  266. return NULL;
  267. while (properties[n].name)
  268. n++;
  269. p = kzalloc_objs(*p, n + 1);
  270. if (!p)
  271. return ERR_PTR(-ENOMEM);
  272. for (i = 0; i < n; i++) {
  273. ret = property_entry_copy_data(&p[i], &properties[i]);
  274. if (ret) {
  275. while (--i >= 0)
  276. property_entry_free_data(&p[i]);
  277. kfree(p);
  278. return ERR_PTR(ret);
  279. }
  280. }
  281. return p;
  282. }
  283. EXPORT_SYMBOL_GPL(property_entries_dup);
  284. /**
  285. * property_entries_free - free previously allocated array of properties
  286. * @properties: array of properties to destroy
  287. *
  288. * This function frees given NULL-terminated array of property entries,
  289. * along with their data.
  290. */
  291. void property_entries_free(const struct property_entry *properties)
  292. {
  293. const struct property_entry *p;
  294. if (!properties)
  295. return;
  296. for (p = properties; p->name; p++)
  297. property_entry_free_data(p);
  298. kfree(properties);
  299. }
  300. EXPORT_SYMBOL_GPL(property_entries_free);
  301. /* -------------------------------------------------------------------------- */
  302. /* fwnode operations */
  303. static struct fwnode_handle *software_node_get(struct fwnode_handle *fwnode)
  304. {
  305. struct swnode *swnode = to_swnode(fwnode);
  306. kobject_get(&swnode->kobj);
  307. return &swnode->fwnode;
  308. }
  309. static void software_node_put(struct fwnode_handle *fwnode)
  310. {
  311. struct swnode *swnode = to_swnode(fwnode);
  312. kobject_put(&swnode->kobj);
  313. }
  314. static bool software_node_property_present(const struct fwnode_handle *fwnode,
  315. const char *propname)
  316. {
  317. struct swnode *swnode = to_swnode(fwnode);
  318. return !!property_entry_get(swnode->node->properties, propname);
  319. }
  320. static int software_node_read_int_array(const struct fwnode_handle *fwnode,
  321. const char *propname,
  322. unsigned int elem_size, void *val,
  323. size_t nval)
  324. {
  325. struct swnode *swnode = to_swnode(fwnode);
  326. return property_entry_read_int_array(swnode->node->properties, propname,
  327. elem_size, val, nval);
  328. }
  329. static int software_node_read_string_array(const struct fwnode_handle *fwnode,
  330. const char *propname,
  331. const char **val, size_t nval)
  332. {
  333. struct swnode *swnode = to_swnode(fwnode);
  334. return property_entry_read_string_array(swnode->node->properties,
  335. propname, val, nval);
  336. }
  337. static const char *
  338. software_node_get_name(const struct fwnode_handle *fwnode)
  339. {
  340. const struct swnode *swnode = to_swnode(fwnode);
  341. return kobject_name(&swnode->kobj);
  342. }
  343. static const char *
  344. software_node_get_name_prefix(const struct fwnode_handle *fwnode)
  345. {
  346. struct fwnode_handle *parent;
  347. const char *prefix;
  348. parent = fwnode_get_parent(fwnode);
  349. if (!parent)
  350. return "";
  351. /* Figure out the prefix from the parents. */
  352. while (is_software_node(parent))
  353. parent = fwnode_get_next_parent(parent);
  354. prefix = fwnode_get_name_prefix(parent);
  355. fwnode_handle_put(parent);
  356. /* Guess something if prefix was NULL. */
  357. return prefix ?: "/";
  358. }
  359. static struct fwnode_handle *
  360. software_node_get_parent(const struct fwnode_handle *fwnode)
  361. {
  362. struct swnode *swnode = to_swnode(fwnode);
  363. if (!swnode || !swnode->parent)
  364. return NULL;
  365. return fwnode_handle_get(&swnode->parent->fwnode);
  366. }
  367. static struct fwnode_handle *
  368. software_node_get_next_child(const struct fwnode_handle *fwnode,
  369. struct fwnode_handle *child)
  370. {
  371. struct swnode *p = to_swnode(fwnode);
  372. struct swnode *c = to_swnode(child);
  373. if (!p || list_empty(&p->children) ||
  374. (c && list_is_last(&c->entry, &p->children))) {
  375. fwnode_handle_put(child);
  376. return NULL;
  377. }
  378. if (c)
  379. c = list_next_entry(c, entry);
  380. else
  381. c = list_first_entry(&p->children, struct swnode, entry);
  382. fwnode_handle_put(child);
  383. return fwnode_handle_get(&c->fwnode);
  384. }
  385. static struct fwnode_handle *
  386. software_node_get_named_child_node(const struct fwnode_handle *fwnode,
  387. const char *childname)
  388. {
  389. struct swnode *swnode = to_swnode(fwnode);
  390. struct swnode *child;
  391. if (!swnode || list_empty(&swnode->children))
  392. return NULL;
  393. list_for_each_entry(child, &swnode->children, entry) {
  394. if (!strcmp(childname, kobject_name(&child->kobj))) {
  395. kobject_get(&child->kobj);
  396. return &child->fwnode;
  397. }
  398. }
  399. return NULL;
  400. }
  401. static int
  402. software_node_get_reference_args(const struct fwnode_handle *fwnode,
  403. const char *propname, const char *nargs_prop,
  404. unsigned int nargs, unsigned int index,
  405. struct fwnode_reference_args *args)
  406. {
  407. struct swnode *swnode = to_swnode(fwnode);
  408. const struct software_node_ref_args *ref_array;
  409. const struct software_node_ref_args *ref;
  410. const struct property_entry *prop;
  411. struct fwnode_handle *refnode;
  412. u32 nargs_prop_val;
  413. int error;
  414. int i;
  415. prop = property_entry_get(swnode->node->properties, propname);
  416. if (!prop)
  417. return -ENOENT;
  418. if (prop->type != DEV_PROP_REF)
  419. return -EINVAL;
  420. /*
  421. * We expect that references are never stored inline, even
  422. * single ones, as they are too big.
  423. */
  424. if (prop->is_inline)
  425. return -EINVAL;
  426. if ((index + 1) * sizeof(*ref) > prop->length)
  427. return -ENOENT;
  428. ref_array = prop->pointer;
  429. ref = &ref_array[index];
  430. /*
  431. * A software node can reference other software nodes or firmware
  432. * nodes (which are the abstraction layer sitting on top of them).
  433. * This is done to ensure we can create references to static software
  434. * nodes before they're registered with the firmware node framework.
  435. * At the time the reference is being resolved, we expect the swnodes
  436. * in question to already have been registered and to be backed by
  437. * a firmware node. This is why we use the fwnode API below to read the
  438. * relevant properties and bump the reference count.
  439. */
  440. if (ref->swnode)
  441. refnode = software_node_fwnode(ref->swnode);
  442. else if (ref->fwnode)
  443. refnode = ref->fwnode;
  444. else
  445. return -EINVAL;
  446. if (!refnode)
  447. return -ENOENT;
  448. if (nargs_prop) {
  449. error = fwnode_property_read_u32(refnode, nargs_prop, &nargs_prop_val);
  450. if (error)
  451. return error;
  452. nargs = nargs_prop_val;
  453. }
  454. if (nargs > NR_FWNODE_REFERENCE_ARGS)
  455. return -EINVAL;
  456. if (!args)
  457. return 0;
  458. args->fwnode = fwnode_handle_get(refnode);
  459. args->nargs = nargs;
  460. for (i = 0; i < nargs; i++)
  461. args->args[i] = ref->args[i];
  462. return 0;
  463. }
  464. static struct fwnode_handle *
  465. swnode_graph_find_next_port(const struct fwnode_handle *parent,
  466. struct fwnode_handle *port)
  467. {
  468. struct fwnode_handle *old = port;
  469. while ((port = software_node_get_next_child(parent, old))) {
  470. /*
  471. * fwnode ports have naming style "port@", so we search for any
  472. * children that follow that convention.
  473. */
  474. if (!strncmp(to_swnode(port)->node->name, "port@",
  475. strlen("port@")))
  476. return port;
  477. old = port;
  478. }
  479. return NULL;
  480. }
  481. static struct fwnode_handle *
  482. software_node_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
  483. struct fwnode_handle *endpoint)
  484. {
  485. struct swnode *swnode = to_swnode(fwnode);
  486. struct fwnode_handle *parent;
  487. struct fwnode_handle *port;
  488. if (!swnode)
  489. return NULL;
  490. if (endpoint) {
  491. port = software_node_get_parent(endpoint);
  492. parent = software_node_get_parent(port);
  493. } else {
  494. parent = software_node_get_named_child_node(fwnode, "ports");
  495. if (!parent)
  496. parent = software_node_get(&swnode->fwnode);
  497. port = swnode_graph_find_next_port(parent, NULL);
  498. }
  499. for (; port; port = swnode_graph_find_next_port(parent, port)) {
  500. endpoint = software_node_get_next_child(port, endpoint);
  501. if (endpoint) {
  502. fwnode_handle_put(port);
  503. break;
  504. }
  505. }
  506. fwnode_handle_put(parent);
  507. return endpoint;
  508. }
  509. static struct fwnode_handle *
  510. software_node_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
  511. {
  512. struct swnode *swnode = to_swnode(fwnode);
  513. const struct software_node_ref_args *ref;
  514. const struct property_entry *prop;
  515. if (!swnode)
  516. return NULL;
  517. prop = property_entry_get(swnode->node->properties, "remote-endpoint");
  518. if (!prop || prop->type != DEV_PROP_REF || prop->is_inline)
  519. return NULL;
  520. ref = prop->pointer;
  521. if (!ref->swnode)
  522. return NULL;
  523. return software_node_get(software_node_fwnode(ref->swnode));
  524. }
  525. static struct fwnode_handle *
  526. software_node_graph_get_port_parent(struct fwnode_handle *fwnode)
  527. {
  528. struct swnode *swnode = to_swnode(fwnode);
  529. swnode = swnode->parent;
  530. if (swnode && !strcmp(swnode->node->name, "ports"))
  531. swnode = swnode->parent;
  532. return swnode ? software_node_get(&swnode->fwnode) : NULL;
  533. }
  534. static int
  535. software_node_graph_parse_endpoint(const struct fwnode_handle *fwnode,
  536. struct fwnode_endpoint *endpoint)
  537. {
  538. struct swnode *swnode = to_swnode(fwnode);
  539. const char *parent_name = swnode->parent->node->name;
  540. int ret;
  541. if (strlen("port@") >= strlen(parent_name) ||
  542. strncmp(parent_name, "port@", strlen("port@")))
  543. return -EINVAL;
  544. /* Ports have naming style "port@n", we need to select the n */
  545. ret = kstrtou32(parent_name + strlen("port@"), 10, &endpoint->port);
  546. if (ret)
  547. return ret;
  548. endpoint->id = swnode->id;
  549. endpoint->local_fwnode = fwnode;
  550. return 0;
  551. }
  552. static const struct fwnode_operations software_node_ops = {
  553. .get = software_node_get,
  554. .put = software_node_put,
  555. .property_present = software_node_property_present,
  556. .property_read_bool = software_node_property_present,
  557. .property_read_int_array = software_node_read_int_array,
  558. .property_read_string_array = software_node_read_string_array,
  559. .get_name = software_node_get_name,
  560. .get_name_prefix = software_node_get_name_prefix,
  561. .get_parent = software_node_get_parent,
  562. .get_next_child_node = software_node_get_next_child,
  563. .get_named_child_node = software_node_get_named_child_node,
  564. .get_reference_args = software_node_get_reference_args,
  565. .graph_get_next_endpoint = software_node_graph_get_next_endpoint,
  566. .graph_get_remote_endpoint = software_node_graph_get_remote_endpoint,
  567. .graph_get_port_parent = software_node_graph_get_port_parent,
  568. .graph_parse_endpoint = software_node_graph_parse_endpoint,
  569. };
  570. /* -------------------------------------------------------------------------- */
  571. /**
  572. * software_node_find_by_name - Find software node by name
  573. * @parent: Parent of the software node
  574. * @name: Name of the software node
  575. *
  576. * The function will find a node that is child of @parent and that is named
  577. * @name. If no node is found, the function returns NULL.
  578. *
  579. * NOTE: you will need to drop the reference with fwnode_handle_put() after use.
  580. */
  581. const struct software_node *
  582. software_node_find_by_name(const struct software_node *parent, const char *name)
  583. {
  584. struct swnode *swnode = NULL;
  585. struct kobject *k;
  586. if (!name)
  587. return NULL;
  588. spin_lock(&swnode_kset->list_lock);
  589. list_for_each_entry(k, &swnode_kset->list, entry) {
  590. swnode = kobj_to_swnode(k);
  591. if (parent == swnode->node->parent && swnode->node->name &&
  592. !strcmp(name, swnode->node->name)) {
  593. kobject_get(&swnode->kobj);
  594. break;
  595. }
  596. swnode = NULL;
  597. }
  598. spin_unlock(&swnode_kset->list_lock);
  599. return swnode ? swnode->node : NULL;
  600. }
  601. EXPORT_SYMBOL_GPL(software_node_find_by_name);
  602. static struct software_node *software_node_alloc(const struct property_entry *properties)
  603. {
  604. struct property_entry *props;
  605. struct software_node *node;
  606. props = property_entries_dup(properties);
  607. if (IS_ERR(props))
  608. return ERR_CAST(props);
  609. node = kzalloc_obj(*node);
  610. if (!node) {
  611. property_entries_free(props);
  612. return ERR_PTR(-ENOMEM);
  613. }
  614. node->properties = props;
  615. return node;
  616. }
  617. static void software_node_free(const struct software_node *node)
  618. {
  619. property_entries_free(node->properties);
  620. kfree(node);
  621. }
  622. static void software_node_release(struct kobject *kobj)
  623. {
  624. struct swnode *swnode = kobj_to_swnode(kobj);
  625. if (swnode->parent) {
  626. ida_free(&swnode->parent->child_ids, swnode->id);
  627. list_del(&swnode->entry);
  628. } else {
  629. ida_free(&swnode_root_ids, swnode->id);
  630. }
  631. if (swnode->allocated)
  632. software_node_free(swnode->node);
  633. ida_destroy(&swnode->child_ids);
  634. kfree(swnode);
  635. }
  636. static const struct kobj_type software_node_type = {
  637. .release = software_node_release,
  638. .sysfs_ops = &kobj_sysfs_ops,
  639. };
  640. static struct fwnode_handle *
  641. swnode_register(const struct software_node *node, struct swnode *parent,
  642. unsigned int allocated)
  643. {
  644. struct swnode *swnode;
  645. int ret;
  646. swnode = kzalloc_obj(*swnode);
  647. if (!swnode)
  648. return ERR_PTR(-ENOMEM);
  649. ret = ida_alloc(parent ? &parent->child_ids : &swnode_root_ids,
  650. GFP_KERNEL);
  651. if (ret < 0) {
  652. kfree(swnode);
  653. return ERR_PTR(ret);
  654. }
  655. swnode->id = ret;
  656. swnode->node = node;
  657. swnode->parent = parent;
  658. swnode->kobj.kset = swnode_kset;
  659. fwnode_init(&swnode->fwnode, &software_node_ops);
  660. ida_init(&swnode->child_ids);
  661. INIT_LIST_HEAD(&swnode->entry);
  662. INIT_LIST_HEAD(&swnode->children);
  663. if (node->name)
  664. ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
  665. parent ? &parent->kobj : NULL,
  666. "%s", node->name);
  667. else
  668. ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
  669. parent ? &parent->kobj : NULL,
  670. "node%d", swnode->id);
  671. if (ret) {
  672. kobject_put(&swnode->kobj);
  673. return ERR_PTR(ret);
  674. }
  675. /*
  676. * Assign the flag only in the successful case, so
  677. * the above kobject_put() won't mess up with properties.
  678. */
  679. swnode->allocated = allocated;
  680. if (parent)
  681. list_add_tail(&swnode->entry, &parent->children);
  682. kobject_uevent(&swnode->kobj, KOBJ_ADD);
  683. return &swnode->fwnode;
  684. }
  685. /**
  686. * software_node_register_node_group - Register a group of software nodes
  687. * @node_group: NULL terminated array of software node pointers to be registered
  688. *
  689. * Register multiple software nodes at once. If any node in the array
  690. * has its .parent pointer set (which can only be to another software_node),
  691. * then its parent **must** have been registered before it is; either outside
  692. * of this function or by ordering the array such that parent comes before
  693. * child.
  694. */
  695. int software_node_register_node_group(const struct software_node * const *node_group)
  696. {
  697. unsigned int i;
  698. int ret;
  699. if (!node_group)
  700. return 0;
  701. for (i = 0; node_group[i]; i++) {
  702. ret = software_node_register(node_group[i]);
  703. if (ret) {
  704. software_node_unregister_node_group(node_group);
  705. return ret;
  706. }
  707. }
  708. return 0;
  709. }
  710. EXPORT_SYMBOL_GPL(software_node_register_node_group);
  711. /**
  712. * software_node_unregister_node_group - Unregister a group of software nodes
  713. * @node_group: NULL terminated array of software node pointers to be unregistered
  714. *
  715. * Unregister multiple software nodes at once. If parent pointers are set up
  716. * in any of the software nodes then the array **must** be ordered such that
  717. * parents come before their children.
  718. *
  719. * NOTE: If you are uncertain whether the array is ordered such that
  720. * parents will be unregistered before their children, it is wiser to
  721. * remove the nodes individually, in the correct order (child before
  722. * parent).
  723. */
  724. void software_node_unregister_node_group(const struct software_node * const *node_group)
  725. {
  726. unsigned int i = 0;
  727. if (!node_group)
  728. return;
  729. while (node_group[i])
  730. i++;
  731. while (i--)
  732. software_node_unregister(node_group[i]);
  733. }
  734. EXPORT_SYMBOL_GPL(software_node_unregister_node_group);
  735. /**
  736. * software_node_register - Register static software node
  737. * @node: The software node to be registered
  738. */
  739. int software_node_register(const struct software_node *node)
  740. {
  741. struct swnode *parent = software_node_to_swnode(node->parent);
  742. if (software_node_to_swnode(node))
  743. return -EEXIST;
  744. if (node->parent && !parent)
  745. return -EINVAL;
  746. return PTR_ERR_OR_ZERO(swnode_register(node, parent, 0));
  747. }
  748. EXPORT_SYMBOL_GPL(software_node_register);
  749. /**
  750. * software_node_unregister - Unregister static software node
  751. * @node: The software node to be unregistered
  752. */
  753. void software_node_unregister(const struct software_node *node)
  754. {
  755. struct swnode *swnode;
  756. swnode = software_node_to_swnode(node);
  757. if (swnode)
  758. fwnode_remove_software_node(&swnode->fwnode);
  759. }
  760. EXPORT_SYMBOL_GPL(software_node_unregister);
  761. struct fwnode_handle *
  762. fwnode_create_software_node(const struct property_entry *properties,
  763. const struct fwnode_handle *parent)
  764. {
  765. struct fwnode_handle *fwnode;
  766. struct software_node *node;
  767. struct swnode *p;
  768. if (IS_ERR(parent))
  769. return ERR_CAST(parent);
  770. p = to_swnode(parent);
  771. if (parent && !p)
  772. return ERR_PTR(-EINVAL);
  773. node = software_node_alloc(properties);
  774. if (IS_ERR(node))
  775. return ERR_CAST(node);
  776. node->parent = p ? p->node : NULL;
  777. fwnode = swnode_register(node, p, 1);
  778. if (IS_ERR(fwnode))
  779. software_node_free(node);
  780. return fwnode;
  781. }
  782. EXPORT_SYMBOL_GPL(fwnode_create_software_node);
  783. void fwnode_remove_software_node(struct fwnode_handle *fwnode)
  784. {
  785. struct swnode *swnode = to_swnode(fwnode);
  786. if (!swnode)
  787. return;
  788. kobject_put(&swnode->kobj);
  789. }
  790. EXPORT_SYMBOL_GPL(fwnode_remove_software_node);
  791. /**
  792. * device_add_software_node - Assign software node to a device
  793. * @dev: The device the software node is meant for.
  794. * @node: The software node.
  795. *
  796. * This function will make @node the secondary firmware node pointer of @dev. If
  797. * @dev has no primary node, then @node will become the primary node. The
  798. * function will register @node automatically if it wasn't already registered.
  799. */
  800. int device_add_software_node(struct device *dev, const struct software_node *node)
  801. {
  802. struct swnode *swnode;
  803. int ret;
  804. /* Only one software node per device. */
  805. if (dev_to_swnode(dev))
  806. return -EBUSY;
  807. swnode = software_node_to_swnode(node);
  808. if (swnode) {
  809. kobject_get(&swnode->kobj);
  810. } else {
  811. ret = software_node_register(node);
  812. if (ret)
  813. return ret;
  814. swnode = software_node_to_swnode(node);
  815. }
  816. set_secondary_fwnode(dev, &swnode->fwnode);
  817. /*
  818. * If the device has been fully registered by the time this function is
  819. * called, software_node_notify() must be called separately so that the
  820. * symlinks get created and the reference count of the node is kept in
  821. * balance.
  822. */
  823. if (device_is_registered(dev))
  824. software_node_notify(dev);
  825. return 0;
  826. }
  827. EXPORT_SYMBOL_GPL(device_add_software_node);
  828. /**
  829. * device_remove_software_node - Remove device's software node
  830. * @dev: The device with the software node.
  831. *
  832. * This function will unregister the software node of @dev.
  833. */
  834. void device_remove_software_node(struct device *dev)
  835. {
  836. struct swnode *swnode;
  837. swnode = dev_to_swnode(dev);
  838. if (!swnode)
  839. return;
  840. if (device_is_registered(dev))
  841. software_node_notify_remove(dev);
  842. set_secondary_fwnode(dev, NULL);
  843. kobject_put(&swnode->kobj);
  844. }
  845. EXPORT_SYMBOL_GPL(device_remove_software_node);
  846. /**
  847. * device_create_managed_software_node - Create a software node for a device
  848. * @dev: The device the software node is assigned to.
  849. * @properties: Device properties for the software node.
  850. * @parent: Parent of the software node.
  851. *
  852. * Creates a software node as a managed resource for @dev, which means the
  853. * lifetime of the newly created software node is tied to the lifetime of @dev.
  854. * Software nodes created with this function should not be reused or shared
  855. * because of that. The function takes a deep copy of @properties for the
  856. * software node.
  857. *
  858. * Since the new software node is assigned directly to @dev, and since it should
  859. * not be shared, it is not returned to the caller. The function returns 0 on
  860. * success, and errno in case of an error.
  861. */
  862. int device_create_managed_software_node(struct device *dev,
  863. const struct property_entry *properties,
  864. const struct software_node *parent)
  865. {
  866. struct fwnode_handle *p = software_node_fwnode(parent);
  867. struct fwnode_handle *fwnode;
  868. if (parent && !p)
  869. return -EINVAL;
  870. fwnode = fwnode_create_software_node(properties, p);
  871. if (IS_ERR(fwnode))
  872. return PTR_ERR(fwnode);
  873. to_swnode(fwnode)->managed = true;
  874. set_secondary_fwnode(dev, fwnode);
  875. if (device_is_registered(dev))
  876. software_node_notify(dev);
  877. return 0;
  878. }
  879. EXPORT_SYMBOL_GPL(device_create_managed_software_node);
  880. void software_node_notify(struct device *dev)
  881. {
  882. struct swnode *swnode;
  883. int ret;
  884. swnode = dev_to_swnode(dev);
  885. if (!swnode)
  886. return;
  887. kobject_get(&swnode->kobj);
  888. ret = sysfs_create_link(&dev->kobj, &swnode->kobj, "software_node");
  889. if (ret)
  890. return;
  891. ret = sysfs_create_link(&swnode->kobj, &dev->kobj, dev_name(dev));
  892. if (ret) {
  893. sysfs_remove_link(&dev->kobj, "software_node");
  894. return;
  895. }
  896. }
  897. void software_node_notify_remove(struct device *dev)
  898. {
  899. struct swnode *swnode;
  900. swnode = dev_to_swnode(dev);
  901. if (!swnode)
  902. return;
  903. sysfs_remove_link(&swnode->kobj, dev_name(dev));
  904. sysfs_remove_link(&dev->kobj, "software_node");
  905. kobject_put(&swnode->kobj);
  906. if (swnode->managed) {
  907. set_secondary_fwnode(dev, NULL);
  908. kobject_put(&swnode->kobj);
  909. }
  910. }
  911. static int __init software_node_init(void)
  912. {
  913. swnode_kset = kset_create_and_add("software_nodes", NULL, kernel_kobj);
  914. if (!swnode_kset)
  915. return -ENOMEM;
  916. return 0;
  917. }
  918. postcore_initcall(software_node_init);
  919. static void __exit software_node_exit(void)
  920. {
  921. ida_destroy(&swnode_root_ids);
  922. kset_unregister(swnode_kset);
  923. }
  924. __exitcall(software_node_exit);