swnode.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
  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. refnode = software_node_fwnode(ref->node);
  431. if (!refnode)
  432. return -ENOENT;
  433. if (nargs_prop) {
  434. error = property_entry_read_int_array(ref->node->properties,
  435. nargs_prop, sizeof(u32),
  436. &nargs_prop_val, 1);
  437. if (error)
  438. return error;
  439. nargs = nargs_prop_val;
  440. }
  441. if (nargs > NR_FWNODE_REFERENCE_ARGS)
  442. return -EINVAL;
  443. if (!args)
  444. return 0;
  445. args->fwnode = software_node_get(refnode);
  446. args->nargs = nargs;
  447. for (i = 0; i < nargs; i++)
  448. args->args[i] = ref->args[i];
  449. return 0;
  450. }
  451. static struct fwnode_handle *
  452. swnode_graph_find_next_port(const struct fwnode_handle *parent,
  453. struct fwnode_handle *port)
  454. {
  455. struct fwnode_handle *old = port;
  456. while ((port = software_node_get_next_child(parent, old))) {
  457. /*
  458. * fwnode ports have naming style "port@", so we search for any
  459. * children that follow that convention.
  460. */
  461. if (!strncmp(to_swnode(port)->node->name, "port@",
  462. strlen("port@")))
  463. return port;
  464. old = port;
  465. }
  466. return NULL;
  467. }
  468. static struct fwnode_handle *
  469. software_node_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
  470. struct fwnode_handle *endpoint)
  471. {
  472. struct swnode *swnode = to_swnode(fwnode);
  473. struct fwnode_handle *parent;
  474. struct fwnode_handle *port;
  475. if (!swnode)
  476. return NULL;
  477. if (endpoint) {
  478. port = software_node_get_parent(endpoint);
  479. parent = software_node_get_parent(port);
  480. } else {
  481. parent = software_node_get_named_child_node(fwnode, "ports");
  482. if (!parent)
  483. parent = software_node_get(&swnode->fwnode);
  484. port = swnode_graph_find_next_port(parent, NULL);
  485. }
  486. for (; port; port = swnode_graph_find_next_port(parent, port)) {
  487. endpoint = software_node_get_next_child(port, endpoint);
  488. if (endpoint) {
  489. fwnode_handle_put(port);
  490. break;
  491. }
  492. }
  493. fwnode_handle_put(parent);
  494. return endpoint;
  495. }
  496. static struct fwnode_handle *
  497. software_node_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
  498. {
  499. struct swnode *swnode = to_swnode(fwnode);
  500. const struct software_node_ref_args *ref;
  501. const struct property_entry *prop;
  502. if (!swnode)
  503. return NULL;
  504. prop = property_entry_get(swnode->node->properties, "remote-endpoint");
  505. if (!prop || prop->type != DEV_PROP_REF || prop->is_inline)
  506. return NULL;
  507. ref = prop->pointer;
  508. return software_node_get(software_node_fwnode(ref[0].node));
  509. }
  510. static struct fwnode_handle *
  511. software_node_graph_get_port_parent(struct fwnode_handle *fwnode)
  512. {
  513. struct swnode *swnode = to_swnode(fwnode);
  514. swnode = swnode->parent;
  515. if (swnode && !strcmp(swnode->node->name, "ports"))
  516. swnode = swnode->parent;
  517. return swnode ? software_node_get(&swnode->fwnode) : NULL;
  518. }
  519. static int
  520. software_node_graph_parse_endpoint(const struct fwnode_handle *fwnode,
  521. struct fwnode_endpoint *endpoint)
  522. {
  523. struct swnode *swnode = to_swnode(fwnode);
  524. const char *parent_name = swnode->parent->node->name;
  525. int ret;
  526. if (strlen("port@") >= strlen(parent_name) ||
  527. strncmp(parent_name, "port@", strlen("port@")))
  528. return -EINVAL;
  529. /* Ports have naming style "port@n", we need to select the n */
  530. ret = kstrtou32(parent_name + strlen("port@"), 10, &endpoint->port);
  531. if (ret)
  532. return ret;
  533. endpoint->id = swnode->id;
  534. endpoint->local_fwnode = fwnode;
  535. return 0;
  536. }
  537. static const struct fwnode_operations software_node_ops = {
  538. .get = software_node_get,
  539. .put = software_node_put,
  540. .property_present = software_node_property_present,
  541. .property_read_int_array = software_node_read_int_array,
  542. .property_read_string_array = software_node_read_string_array,
  543. .get_name = software_node_get_name,
  544. .get_name_prefix = software_node_get_name_prefix,
  545. .get_parent = software_node_get_parent,
  546. .get_next_child_node = software_node_get_next_child,
  547. .get_named_child_node = software_node_get_named_child_node,
  548. .get_reference_args = software_node_get_reference_args,
  549. .graph_get_next_endpoint = software_node_graph_get_next_endpoint,
  550. .graph_get_remote_endpoint = software_node_graph_get_remote_endpoint,
  551. .graph_get_port_parent = software_node_graph_get_port_parent,
  552. .graph_parse_endpoint = software_node_graph_parse_endpoint,
  553. };
  554. /* -------------------------------------------------------------------------- */
  555. /**
  556. * software_node_find_by_name - Find software node by name
  557. * @parent: Parent of the software node
  558. * @name: Name of the software node
  559. *
  560. * The function will find a node that is child of @parent and that is named
  561. * @name. If no node is found, the function returns NULL.
  562. *
  563. * NOTE: you will need to drop the reference with fwnode_handle_put() after use.
  564. */
  565. const struct software_node *
  566. software_node_find_by_name(const struct software_node *parent, const char *name)
  567. {
  568. struct swnode *swnode = NULL;
  569. struct kobject *k;
  570. if (!name)
  571. return NULL;
  572. spin_lock(&swnode_kset->list_lock);
  573. list_for_each_entry(k, &swnode_kset->list, entry) {
  574. swnode = kobj_to_swnode(k);
  575. if (parent == swnode->node->parent && swnode->node->name &&
  576. !strcmp(name, swnode->node->name)) {
  577. kobject_get(&swnode->kobj);
  578. break;
  579. }
  580. swnode = NULL;
  581. }
  582. spin_unlock(&swnode_kset->list_lock);
  583. return swnode ? swnode->node : NULL;
  584. }
  585. EXPORT_SYMBOL_GPL(software_node_find_by_name);
  586. static struct software_node *software_node_alloc(const struct property_entry *properties)
  587. {
  588. struct property_entry *props;
  589. struct software_node *node;
  590. props = property_entries_dup(properties);
  591. if (IS_ERR(props))
  592. return ERR_CAST(props);
  593. node = kzalloc(sizeof(*node), GFP_KERNEL);
  594. if (!node) {
  595. property_entries_free(props);
  596. return ERR_PTR(-ENOMEM);
  597. }
  598. node->properties = props;
  599. return node;
  600. }
  601. static void software_node_free(const struct software_node *node)
  602. {
  603. property_entries_free(node->properties);
  604. kfree(node);
  605. }
  606. static void software_node_release(struct kobject *kobj)
  607. {
  608. struct swnode *swnode = kobj_to_swnode(kobj);
  609. if (swnode->parent) {
  610. ida_free(&swnode->parent->child_ids, swnode->id);
  611. list_del(&swnode->entry);
  612. } else {
  613. ida_free(&swnode_root_ids, swnode->id);
  614. }
  615. if (swnode->allocated)
  616. software_node_free(swnode->node);
  617. ida_destroy(&swnode->child_ids);
  618. kfree(swnode);
  619. }
  620. static const struct kobj_type software_node_type = {
  621. .release = software_node_release,
  622. .sysfs_ops = &kobj_sysfs_ops,
  623. };
  624. static struct fwnode_handle *
  625. swnode_register(const struct software_node *node, struct swnode *parent,
  626. unsigned int allocated)
  627. {
  628. struct swnode *swnode;
  629. int ret;
  630. swnode = kzalloc(sizeof(*swnode), GFP_KERNEL);
  631. if (!swnode)
  632. return ERR_PTR(-ENOMEM);
  633. ret = ida_alloc(parent ? &parent->child_ids : &swnode_root_ids,
  634. GFP_KERNEL);
  635. if (ret < 0) {
  636. kfree(swnode);
  637. return ERR_PTR(ret);
  638. }
  639. swnode->id = ret;
  640. swnode->node = node;
  641. swnode->parent = parent;
  642. swnode->kobj.kset = swnode_kset;
  643. fwnode_init(&swnode->fwnode, &software_node_ops);
  644. ida_init(&swnode->child_ids);
  645. INIT_LIST_HEAD(&swnode->entry);
  646. INIT_LIST_HEAD(&swnode->children);
  647. if (node->name)
  648. ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
  649. parent ? &parent->kobj : NULL,
  650. "%s", node->name);
  651. else
  652. ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
  653. parent ? &parent->kobj : NULL,
  654. "node%d", swnode->id);
  655. if (ret) {
  656. kobject_put(&swnode->kobj);
  657. return ERR_PTR(ret);
  658. }
  659. /*
  660. * Assign the flag only in the successful case, so
  661. * the above kobject_put() won't mess up with properties.
  662. */
  663. swnode->allocated = allocated;
  664. if (parent)
  665. list_add_tail(&swnode->entry, &parent->children);
  666. kobject_uevent(&swnode->kobj, KOBJ_ADD);
  667. return &swnode->fwnode;
  668. }
  669. /**
  670. * software_node_register_node_group - Register a group of software nodes
  671. * @node_group: NULL terminated array of software node pointers to be registered
  672. *
  673. * Register multiple software nodes at once. If any node in the array
  674. * has its .parent pointer set (which can only be to another software_node),
  675. * then its parent **must** have been registered before it is; either outside
  676. * of this function or by ordering the array such that parent comes before
  677. * child.
  678. */
  679. int software_node_register_node_group(const struct software_node **node_group)
  680. {
  681. unsigned int i;
  682. int ret;
  683. if (!node_group)
  684. return 0;
  685. for (i = 0; node_group[i]; i++) {
  686. ret = software_node_register(node_group[i]);
  687. if (ret) {
  688. software_node_unregister_node_group(node_group);
  689. return ret;
  690. }
  691. }
  692. return 0;
  693. }
  694. EXPORT_SYMBOL_GPL(software_node_register_node_group);
  695. /**
  696. * software_node_unregister_node_group - Unregister a group of software nodes
  697. * @node_group: NULL terminated array of software node pointers to be unregistered
  698. *
  699. * Unregister multiple software nodes at once. If parent pointers are set up
  700. * in any of the software nodes then the array **must** be ordered such that
  701. * parents come before their children.
  702. *
  703. * NOTE: If you are uncertain whether the array is ordered such that
  704. * parents will be unregistered before their children, it is wiser to
  705. * remove the nodes individually, in the correct order (child before
  706. * parent).
  707. */
  708. void software_node_unregister_node_group(
  709. const struct software_node **node_group)
  710. {
  711. unsigned int i = 0;
  712. if (!node_group)
  713. return;
  714. while (node_group[i])
  715. i++;
  716. while (i--)
  717. software_node_unregister(node_group[i]);
  718. }
  719. EXPORT_SYMBOL_GPL(software_node_unregister_node_group);
  720. /**
  721. * software_node_register - Register static software node
  722. * @node: The software node to be registered
  723. */
  724. int software_node_register(const struct software_node *node)
  725. {
  726. struct swnode *parent = software_node_to_swnode(node->parent);
  727. if (software_node_to_swnode(node))
  728. return -EEXIST;
  729. if (node->parent && !parent)
  730. return -EINVAL;
  731. return PTR_ERR_OR_ZERO(swnode_register(node, parent, 0));
  732. }
  733. EXPORT_SYMBOL_GPL(software_node_register);
  734. /**
  735. * software_node_unregister - Unregister static software node
  736. * @node: The software node to be unregistered
  737. */
  738. void software_node_unregister(const struct software_node *node)
  739. {
  740. struct swnode *swnode;
  741. swnode = software_node_to_swnode(node);
  742. if (swnode)
  743. fwnode_remove_software_node(&swnode->fwnode);
  744. }
  745. EXPORT_SYMBOL_GPL(software_node_unregister);
  746. struct fwnode_handle *
  747. fwnode_create_software_node(const struct property_entry *properties,
  748. const struct fwnode_handle *parent)
  749. {
  750. struct fwnode_handle *fwnode;
  751. struct software_node *node;
  752. struct swnode *p;
  753. if (IS_ERR(parent))
  754. return ERR_CAST(parent);
  755. p = to_swnode(parent);
  756. if (parent && !p)
  757. return ERR_PTR(-EINVAL);
  758. node = software_node_alloc(properties);
  759. if (IS_ERR(node))
  760. return ERR_CAST(node);
  761. node->parent = p ? p->node : NULL;
  762. fwnode = swnode_register(node, p, 1);
  763. if (IS_ERR(fwnode))
  764. software_node_free(node);
  765. return fwnode;
  766. }
  767. EXPORT_SYMBOL_GPL(fwnode_create_software_node);
  768. void fwnode_remove_software_node(struct fwnode_handle *fwnode)
  769. {
  770. struct swnode *swnode = to_swnode(fwnode);
  771. if (!swnode)
  772. return;
  773. kobject_put(&swnode->kobj);
  774. }
  775. EXPORT_SYMBOL_GPL(fwnode_remove_software_node);
  776. /**
  777. * device_add_software_node - Assign software node to a device
  778. * @dev: The device the software node is meant for.
  779. * @node: The software node.
  780. *
  781. * This function will make @node the secondary firmware node pointer of @dev. If
  782. * @dev has no primary node, then @node will become the primary node. The
  783. * function will register @node automatically if it wasn't already registered.
  784. */
  785. int device_add_software_node(struct device *dev, const struct software_node *node)
  786. {
  787. struct swnode *swnode;
  788. int ret;
  789. /* Only one software node per device. */
  790. if (dev_to_swnode(dev))
  791. return -EBUSY;
  792. swnode = software_node_to_swnode(node);
  793. if (swnode) {
  794. kobject_get(&swnode->kobj);
  795. } else {
  796. ret = software_node_register(node);
  797. if (ret)
  798. return ret;
  799. swnode = software_node_to_swnode(node);
  800. }
  801. set_secondary_fwnode(dev, &swnode->fwnode);
  802. /*
  803. * If the device has been fully registered by the time this function is
  804. * called, software_node_notify() must be called separately so that the
  805. * symlinks get created and the reference count of the node is kept in
  806. * balance.
  807. */
  808. if (device_is_registered(dev))
  809. software_node_notify(dev);
  810. return 0;
  811. }
  812. EXPORT_SYMBOL_GPL(device_add_software_node);
  813. /**
  814. * device_remove_software_node - Remove device's software node
  815. * @dev: The device with the software node.
  816. *
  817. * This function will unregister the software node of @dev.
  818. */
  819. void device_remove_software_node(struct device *dev)
  820. {
  821. struct swnode *swnode;
  822. swnode = dev_to_swnode(dev);
  823. if (!swnode)
  824. return;
  825. if (device_is_registered(dev))
  826. software_node_notify_remove(dev);
  827. set_secondary_fwnode(dev, NULL);
  828. kobject_put(&swnode->kobj);
  829. }
  830. EXPORT_SYMBOL_GPL(device_remove_software_node);
  831. /**
  832. * device_create_managed_software_node - Create a software node for a device
  833. * @dev: The device the software node is assigned to.
  834. * @properties: Device properties for the software node.
  835. * @parent: Parent of the software node.
  836. *
  837. * Creates a software node as a managed resource for @dev, which means the
  838. * lifetime of the newly created software node is tied to the lifetime of @dev.
  839. * Software nodes created with this function should not be reused or shared
  840. * because of that. The function takes a deep copy of @properties for the
  841. * software node.
  842. *
  843. * Since the new software node is assigned directly to @dev, and since it should
  844. * not be shared, it is not returned to the caller. The function returns 0 on
  845. * success, and errno in case of an error.
  846. */
  847. int device_create_managed_software_node(struct device *dev,
  848. const struct property_entry *properties,
  849. const struct software_node *parent)
  850. {
  851. struct fwnode_handle *p = software_node_fwnode(parent);
  852. struct fwnode_handle *fwnode;
  853. if (parent && !p)
  854. return -EINVAL;
  855. fwnode = fwnode_create_software_node(properties, p);
  856. if (IS_ERR(fwnode))
  857. return PTR_ERR(fwnode);
  858. to_swnode(fwnode)->managed = true;
  859. set_secondary_fwnode(dev, fwnode);
  860. if (device_is_registered(dev))
  861. software_node_notify(dev);
  862. return 0;
  863. }
  864. EXPORT_SYMBOL_GPL(device_create_managed_software_node);
  865. void software_node_notify(struct device *dev)
  866. {
  867. struct swnode *swnode;
  868. int ret;
  869. swnode = dev_to_swnode(dev);
  870. if (!swnode)
  871. return;
  872. ret = sysfs_create_link(&dev->kobj, &swnode->kobj, "software_node");
  873. if (ret)
  874. return;
  875. ret = sysfs_create_link(&swnode->kobj, &dev->kobj, dev_name(dev));
  876. if (ret) {
  877. sysfs_remove_link(&dev->kobj, "software_node");
  878. return;
  879. }
  880. kobject_get(&swnode->kobj);
  881. }
  882. void software_node_notify_remove(struct device *dev)
  883. {
  884. struct swnode *swnode;
  885. swnode = dev_to_swnode(dev);
  886. if (!swnode)
  887. return;
  888. sysfs_remove_link(&swnode->kobj, dev_name(dev));
  889. sysfs_remove_link(&dev->kobj, "software_node");
  890. kobject_put(&swnode->kobj);
  891. if (swnode->managed) {
  892. set_secondary_fwnode(dev, NULL);
  893. kobject_put(&swnode->kobj);
  894. }
  895. }
  896. static int __init software_node_init(void)
  897. {
  898. swnode_kset = kset_create_and_add("software_nodes", NULL, kernel_kobj);
  899. if (!swnode_kset)
  900. return -ENOMEM;
  901. return 0;
  902. }
  903. postcore_initcall(software_node_init);
  904. static void __exit software_node_exit(void)
  905. {
  906. ida_destroy(&swnode_root_ids);
  907. kset_unregister(swnode_kset);
  908. }
  909. __exitcall(software_node_exit);