swnode.c 28 KB

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