gpiolib-sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bitops.h>
  3. #include <linux/cleanup.h>
  4. #include <linux/device.h>
  5. #include <linux/idr.h>
  6. #include <linux/init.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/kdev_t.h>
  9. #include <linux/kstrtox.h>
  10. #include <linux/list.h>
  11. #include <linux/mutex.h>
  12. #include <linux/printk.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/string.h>
  16. #include <linux/srcu.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/types.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/gpio/driver.h>
  21. #include "gpiolib.h"
  22. #include "gpiolib-sysfs.h"
  23. struct kernfs_node;
  24. #define GPIO_IRQF_TRIGGER_NONE 0
  25. #define GPIO_IRQF_TRIGGER_FALLING BIT(0)
  26. #define GPIO_IRQF_TRIGGER_RISING BIT(1)
  27. #define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \
  28. GPIO_IRQF_TRIGGER_RISING)
  29. struct gpiod_data {
  30. struct gpio_desc *desc;
  31. struct mutex mutex;
  32. struct kernfs_node *value_kn;
  33. int irq;
  34. unsigned char irq_flags;
  35. bool direction_can_change;
  36. };
  37. /*
  38. * Lock to serialise gpiod export and unexport, and prevent re-export of
  39. * gpiod whose chip is being unregistered.
  40. */
  41. static DEFINE_MUTEX(sysfs_lock);
  42. /*
  43. * /sys/class/gpio/gpioN... only for GPIOs that are exported
  44. * /direction
  45. * * MAY BE OMITTED if kernel won't allow direction changes
  46. * * is read/write as "in" or "out"
  47. * * may also be written as "high" or "low", initializing
  48. * output value as specified ("out" implies "low")
  49. * /value
  50. * * always readable, subject to hardware behavior
  51. * * may be writable, as zero/nonzero
  52. * /edge
  53. * * configures behavior of poll(2) on /value
  54. * * available only if pin can generate IRQs on input
  55. * * is read/write as "none", "falling", "rising", or "both"
  56. * /active_low
  57. * * configures polarity of /value
  58. * * is read/write as zero/nonzero
  59. * * also affects existing and subsequent "falling" and "rising"
  60. * /edge configuration
  61. */
  62. static ssize_t direction_show(struct device *dev,
  63. struct device_attribute *attr, char *buf)
  64. {
  65. struct gpiod_data *data = dev_get_drvdata(dev);
  66. struct gpio_desc *desc = data->desc;
  67. int value;
  68. mutex_lock(&data->mutex);
  69. gpiod_get_direction(desc);
  70. value = !!test_bit(FLAG_IS_OUT, &desc->flags);
  71. mutex_unlock(&data->mutex);
  72. return sysfs_emit(buf, "%s\n", value ? "out" : "in");
  73. }
  74. static ssize_t direction_store(struct device *dev,
  75. struct device_attribute *attr, const char *buf, size_t size)
  76. {
  77. struct gpiod_data *data = dev_get_drvdata(dev);
  78. struct gpio_desc *desc = data->desc;
  79. ssize_t status;
  80. mutex_lock(&data->mutex);
  81. if (sysfs_streq(buf, "high"))
  82. status = gpiod_direction_output_raw(desc, 1);
  83. else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
  84. status = gpiod_direction_output_raw(desc, 0);
  85. else if (sysfs_streq(buf, "in"))
  86. status = gpiod_direction_input(desc);
  87. else
  88. status = -EINVAL;
  89. mutex_unlock(&data->mutex);
  90. return status ? : size;
  91. }
  92. static DEVICE_ATTR_RW(direction);
  93. static ssize_t value_show(struct device *dev,
  94. struct device_attribute *attr, char *buf)
  95. {
  96. struct gpiod_data *data = dev_get_drvdata(dev);
  97. struct gpio_desc *desc = data->desc;
  98. ssize_t status;
  99. mutex_lock(&data->mutex);
  100. status = gpiod_get_value_cansleep(desc);
  101. mutex_unlock(&data->mutex);
  102. if (status < 0)
  103. return status;
  104. return sysfs_emit(buf, "%zd\n", status);
  105. }
  106. static ssize_t value_store(struct device *dev,
  107. struct device_attribute *attr, const char *buf, size_t size)
  108. {
  109. struct gpiod_data *data = dev_get_drvdata(dev);
  110. struct gpio_desc *desc = data->desc;
  111. ssize_t status;
  112. long value;
  113. status = kstrtol(buf, 0, &value);
  114. mutex_lock(&data->mutex);
  115. if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
  116. status = -EPERM;
  117. } else if (status == 0) {
  118. gpiod_set_value_cansleep(desc, value);
  119. status = size;
  120. }
  121. mutex_unlock(&data->mutex);
  122. return status;
  123. }
  124. static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store);
  125. static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
  126. {
  127. struct gpiod_data *data = priv;
  128. sysfs_notify_dirent(data->value_kn);
  129. return IRQ_HANDLED;
  130. }
  131. /* Caller holds gpiod-data mutex. */
  132. static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
  133. {
  134. struct gpiod_data *data = dev_get_drvdata(dev);
  135. struct gpio_desc *desc = data->desc;
  136. unsigned long irq_flags;
  137. int ret;
  138. CLASS(gpio_chip_guard, guard)(desc);
  139. if (!guard.gc)
  140. return -ENODEV;
  141. data->irq = gpiod_to_irq(desc);
  142. if (data->irq < 0)
  143. return -EIO;
  144. data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
  145. if (!data->value_kn)
  146. return -ENODEV;
  147. irq_flags = IRQF_SHARED;
  148. if (flags & GPIO_IRQF_TRIGGER_FALLING)
  149. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  150. IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
  151. if (flags & GPIO_IRQF_TRIGGER_RISING)
  152. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  153. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  154. /*
  155. * FIXME: This should be done in the irq_request_resources callback
  156. * when the irq is requested, but a few drivers currently fail
  157. * to do so.
  158. *
  159. * Remove this redundant call (along with the corresponding
  160. * unlock) when those drivers have been fixed.
  161. */
  162. ret = gpiochip_lock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
  163. if (ret < 0)
  164. goto err_put_kn;
  165. ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
  166. "gpiolib", data);
  167. if (ret < 0)
  168. goto err_unlock;
  169. data->irq_flags = flags;
  170. return 0;
  171. err_unlock:
  172. gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
  173. err_put_kn:
  174. sysfs_put(data->value_kn);
  175. return ret;
  176. }
  177. /*
  178. * Caller holds gpiod-data mutex (unless called after class-device
  179. * deregistration).
  180. */
  181. static void gpio_sysfs_free_irq(struct device *dev)
  182. {
  183. struct gpiod_data *data = dev_get_drvdata(dev);
  184. struct gpio_desc *desc = data->desc;
  185. CLASS(gpio_chip_guard, guard)(desc);
  186. if (!guard.gc)
  187. return;
  188. data->irq_flags = 0;
  189. free_irq(data->irq, data);
  190. gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
  191. sysfs_put(data->value_kn);
  192. }
  193. static const char * const trigger_names[] = {
  194. [GPIO_IRQF_TRIGGER_NONE] = "none",
  195. [GPIO_IRQF_TRIGGER_FALLING] = "falling",
  196. [GPIO_IRQF_TRIGGER_RISING] = "rising",
  197. [GPIO_IRQF_TRIGGER_BOTH] = "both",
  198. };
  199. static ssize_t edge_show(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct gpiod_data *data = dev_get_drvdata(dev);
  203. int flags;
  204. mutex_lock(&data->mutex);
  205. flags = data->irq_flags;
  206. mutex_unlock(&data->mutex);
  207. if (flags >= ARRAY_SIZE(trigger_names))
  208. return 0;
  209. return sysfs_emit(buf, "%s\n", trigger_names[flags]);
  210. }
  211. static ssize_t edge_store(struct device *dev,
  212. struct device_attribute *attr, const char *buf, size_t size)
  213. {
  214. struct gpiod_data *data = dev_get_drvdata(dev);
  215. ssize_t status = size;
  216. int flags;
  217. flags = sysfs_match_string(trigger_names, buf);
  218. if (flags < 0)
  219. return flags;
  220. mutex_lock(&data->mutex);
  221. if (flags == data->irq_flags) {
  222. status = size;
  223. goto out_unlock;
  224. }
  225. if (data->irq_flags)
  226. gpio_sysfs_free_irq(dev);
  227. if (flags) {
  228. status = gpio_sysfs_request_irq(dev, flags);
  229. if (!status)
  230. status = size;
  231. }
  232. out_unlock:
  233. mutex_unlock(&data->mutex);
  234. return status;
  235. }
  236. static DEVICE_ATTR_RW(edge);
  237. /* Caller holds gpiod-data mutex. */
  238. static int gpio_sysfs_set_active_low(struct device *dev, int value)
  239. {
  240. struct gpiod_data *data = dev_get_drvdata(dev);
  241. unsigned int flags = data->irq_flags;
  242. struct gpio_desc *desc = data->desc;
  243. int status = 0;
  244. if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
  245. return 0;
  246. assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value);
  247. /* reconfigure poll(2) support if enabled on one edge only */
  248. if (flags == GPIO_IRQF_TRIGGER_FALLING ||
  249. flags == GPIO_IRQF_TRIGGER_RISING) {
  250. gpio_sysfs_free_irq(dev);
  251. status = gpio_sysfs_request_irq(dev, flags);
  252. }
  253. return status;
  254. }
  255. static ssize_t active_low_show(struct device *dev,
  256. struct device_attribute *attr, char *buf)
  257. {
  258. struct gpiod_data *data = dev_get_drvdata(dev);
  259. struct gpio_desc *desc = data->desc;
  260. int value;
  261. mutex_lock(&data->mutex);
  262. value = !!test_bit(FLAG_ACTIVE_LOW, &desc->flags);
  263. mutex_unlock(&data->mutex);
  264. return sysfs_emit(buf, "%d\n", value);
  265. }
  266. static ssize_t active_low_store(struct device *dev,
  267. struct device_attribute *attr, const char *buf, size_t size)
  268. {
  269. struct gpiod_data *data = dev_get_drvdata(dev);
  270. ssize_t status;
  271. long value;
  272. status = kstrtol(buf, 0, &value);
  273. if (status)
  274. return status;
  275. mutex_lock(&data->mutex);
  276. status = gpio_sysfs_set_active_low(dev, value);
  277. mutex_unlock(&data->mutex);
  278. return status ? : size;
  279. }
  280. static DEVICE_ATTR_RW(active_low);
  281. static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
  282. int n)
  283. {
  284. struct device *dev = kobj_to_dev(kobj);
  285. struct gpiod_data *data = dev_get_drvdata(dev);
  286. struct gpio_desc *desc = data->desc;
  287. umode_t mode = attr->mode;
  288. bool show_direction = data->direction_can_change;
  289. if (attr == &dev_attr_direction.attr) {
  290. if (!show_direction)
  291. mode = 0;
  292. } else if (attr == &dev_attr_edge.attr) {
  293. if (gpiod_to_irq(desc) < 0)
  294. mode = 0;
  295. if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
  296. mode = 0;
  297. }
  298. return mode;
  299. }
  300. static struct attribute *gpio_attrs[] = {
  301. &dev_attr_direction.attr,
  302. &dev_attr_edge.attr,
  303. &dev_attr_value.attr,
  304. &dev_attr_active_low.attr,
  305. NULL,
  306. };
  307. static const struct attribute_group gpio_group = {
  308. .attrs = gpio_attrs,
  309. .is_visible = gpio_is_visible,
  310. };
  311. static const struct attribute_group *gpio_groups[] = {
  312. &gpio_group,
  313. NULL
  314. };
  315. /*
  316. * /sys/class/gpio/gpiochipN/
  317. * /base ... matching gpio_chip.base (N)
  318. * /label ... matching gpio_chip.label
  319. * /ngpio ... matching gpio_chip.ngpio
  320. */
  321. static ssize_t base_show(struct device *dev,
  322. struct device_attribute *attr, char *buf)
  323. {
  324. const struct gpio_device *gdev = dev_get_drvdata(dev);
  325. return sysfs_emit(buf, "%u\n", gdev->base);
  326. }
  327. static DEVICE_ATTR_RO(base);
  328. static ssize_t label_show(struct device *dev,
  329. struct device_attribute *attr, char *buf)
  330. {
  331. const struct gpio_device *gdev = dev_get_drvdata(dev);
  332. return sysfs_emit(buf, "%s\n", gdev->label);
  333. }
  334. static DEVICE_ATTR_RO(label);
  335. static ssize_t ngpio_show(struct device *dev,
  336. struct device_attribute *attr, char *buf)
  337. {
  338. const struct gpio_device *gdev = dev_get_drvdata(dev);
  339. return sysfs_emit(buf, "%u\n", gdev->ngpio);
  340. }
  341. static DEVICE_ATTR_RO(ngpio);
  342. static struct attribute *gpiochip_attrs[] = {
  343. &dev_attr_base.attr,
  344. &dev_attr_label.attr,
  345. &dev_attr_ngpio.attr,
  346. NULL,
  347. };
  348. ATTRIBUTE_GROUPS(gpiochip);
  349. /*
  350. * /sys/class/gpio/export ... write-only
  351. * integer N ... number of GPIO to export (full access)
  352. * /sys/class/gpio/unexport ... write-only
  353. * integer N ... number of GPIO to unexport
  354. */
  355. static ssize_t export_store(const struct class *class,
  356. const struct class_attribute *attr,
  357. const char *buf, size_t len)
  358. {
  359. struct gpio_desc *desc;
  360. int status, offset;
  361. long gpio;
  362. status = kstrtol(buf, 0, &gpio);
  363. if (status)
  364. return status;
  365. desc = gpio_to_desc(gpio);
  366. /* reject invalid GPIOs */
  367. if (!desc) {
  368. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  369. return -EINVAL;
  370. }
  371. CLASS(gpio_chip_guard, guard)(desc);
  372. if (!guard.gc)
  373. return -ENODEV;
  374. offset = gpio_chip_hwgpio(desc);
  375. if (!gpiochip_line_is_valid(guard.gc, offset)) {
  376. pr_warn("%s: GPIO %ld masked\n", __func__, gpio);
  377. return -EINVAL;
  378. }
  379. /* No extra locking here; FLAG_SYSFS just signifies that the
  380. * request and export were done by on behalf of userspace, so
  381. * they may be undone on its behalf too.
  382. */
  383. status = gpiod_request_user(desc, "sysfs");
  384. if (status)
  385. goto done;
  386. status = gpiod_set_transitory(desc, false);
  387. if (status) {
  388. gpiod_free(desc);
  389. goto done;
  390. }
  391. status = gpiod_export(desc, true);
  392. if (status < 0)
  393. gpiod_free(desc);
  394. else
  395. set_bit(FLAG_SYSFS, &desc->flags);
  396. done:
  397. if (status)
  398. pr_debug("%s: status %d\n", __func__, status);
  399. return status ? : len;
  400. }
  401. static CLASS_ATTR_WO(export);
  402. static ssize_t unexport_store(const struct class *class,
  403. const struct class_attribute *attr,
  404. const char *buf, size_t len)
  405. {
  406. struct gpio_desc *desc;
  407. int status;
  408. long gpio;
  409. status = kstrtol(buf, 0, &gpio);
  410. if (status < 0)
  411. goto done;
  412. desc = gpio_to_desc(gpio);
  413. /* reject bogus commands (gpiod_unexport() ignores them) */
  414. if (!desc) {
  415. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  416. return -EINVAL;
  417. }
  418. status = -EINVAL;
  419. /* No extra locking here; FLAG_SYSFS just signifies that the
  420. * request and export were done by on behalf of userspace, so
  421. * they may be undone on its behalf too.
  422. */
  423. if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
  424. gpiod_unexport(desc);
  425. gpiod_free(desc);
  426. status = 0;
  427. }
  428. done:
  429. if (status)
  430. pr_debug("%s: status %d\n", __func__, status);
  431. return status ? : len;
  432. }
  433. static CLASS_ATTR_WO(unexport);
  434. static struct attribute *gpio_class_attrs[] = {
  435. &class_attr_export.attr,
  436. &class_attr_unexport.attr,
  437. NULL,
  438. };
  439. ATTRIBUTE_GROUPS(gpio_class);
  440. static struct class gpio_class = {
  441. .name = "gpio",
  442. .class_groups = gpio_class_groups,
  443. };
  444. /**
  445. * gpiod_export - export a GPIO through sysfs
  446. * @desc: GPIO to make available, already requested
  447. * @direction_may_change: true if userspace may change GPIO direction
  448. * Context: arch_initcall or later
  449. *
  450. * When drivers want to make a GPIO accessible to userspace after they
  451. * have requested it -- perhaps while debugging, or as part of their
  452. * public interface -- they may use this routine. If the GPIO can
  453. * change direction (some can't) and the caller allows it, userspace
  454. * will see "direction" sysfs attribute which may be used to change
  455. * the gpio's direction. A "value" attribute will always be provided.
  456. *
  457. * Returns:
  458. * 0 on success, or negative errno on failure.
  459. */
  460. int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  461. {
  462. const char *ioname = NULL;
  463. struct gpio_device *gdev;
  464. struct gpiod_data *data;
  465. struct device *dev;
  466. int status, offset;
  467. /* can't export until sysfs is available ... */
  468. if (!class_is_registered(&gpio_class)) {
  469. pr_debug("%s: called too early!\n", __func__);
  470. return -ENOENT;
  471. }
  472. if (!desc) {
  473. pr_debug("%s: invalid gpio descriptor\n", __func__);
  474. return -EINVAL;
  475. }
  476. CLASS(gpio_chip_guard, guard)(desc);
  477. if (!guard.gc)
  478. return -ENODEV;
  479. if (test_and_set_bit(FLAG_EXPORT, &desc->flags))
  480. return -EPERM;
  481. gdev = desc->gdev;
  482. mutex_lock(&sysfs_lock);
  483. /* check if chip is being removed */
  484. if (!gdev->mockdev) {
  485. status = -ENODEV;
  486. goto err_unlock;
  487. }
  488. if (!test_bit(FLAG_REQUESTED, &desc->flags)) {
  489. gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__);
  490. status = -EPERM;
  491. goto err_unlock;
  492. }
  493. data = kzalloc(sizeof(*data), GFP_KERNEL);
  494. if (!data) {
  495. status = -ENOMEM;
  496. goto err_unlock;
  497. }
  498. data->desc = desc;
  499. mutex_init(&data->mutex);
  500. if (guard.gc->direction_input && guard.gc->direction_output)
  501. data->direction_can_change = direction_may_change;
  502. else
  503. data->direction_can_change = false;
  504. offset = gpio_chip_hwgpio(desc);
  505. if (guard.gc->names && guard.gc->names[offset])
  506. ioname = guard.gc->names[offset];
  507. dev = device_create_with_groups(&gpio_class, &gdev->dev,
  508. MKDEV(0, 0), data, gpio_groups,
  509. ioname ? ioname : "gpio%u",
  510. desc_to_gpio(desc));
  511. if (IS_ERR(dev)) {
  512. status = PTR_ERR(dev);
  513. goto err_free_data;
  514. }
  515. mutex_unlock(&sysfs_lock);
  516. return 0;
  517. err_free_data:
  518. kfree(data);
  519. err_unlock:
  520. mutex_unlock(&sysfs_lock);
  521. clear_bit(FLAG_EXPORT, &desc->flags);
  522. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  523. return status;
  524. }
  525. EXPORT_SYMBOL_GPL(gpiod_export);
  526. static int match_export(struct device *dev, const void *desc)
  527. {
  528. struct gpiod_data *data = dev_get_drvdata(dev);
  529. return data->desc == desc;
  530. }
  531. /**
  532. * gpiod_export_link - create a sysfs link to an exported GPIO node
  533. * @dev: device under which to create symlink
  534. * @name: name of the symlink
  535. * @desc: GPIO to create symlink to, already exported
  536. *
  537. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  538. * node. Caller is responsible for unlinking.
  539. *
  540. * Returns:
  541. * 0 on success, or negative errno on failure.
  542. */
  543. int gpiod_export_link(struct device *dev, const char *name,
  544. struct gpio_desc *desc)
  545. {
  546. struct device *cdev;
  547. int ret;
  548. if (!desc) {
  549. pr_warn("%s: invalid GPIO\n", __func__);
  550. return -EINVAL;
  551. }
  552. cdev = class_find_device(&gpio_class, NULL, desc, match_export);
  553. if (!cdev)
  554. return -ENODEV;
  555. ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
  556. put_device(cdev);
  557. return ret;
  558. }
  559. EXPORT_SYMBOL_GPL(gpiod_export_link);
  560. /**
  561. * gpiod_unexport - reverse effect of gpiod_export()
  562. * @desc: GPIO to make unavailable
  563. *
  564. * This is implicit on gpiod_free().
  565. */
  566. void gpiod_unexport(struct gpio_desc *desc)
  567. {
  568. struct gpiod_data *data;
  569. struct device *dev;
  570. if (!desc) {
  571. pr_warn("%s: invalid GPIO\n", __func__);
  572. return;
  573. }
  574. mutex_lock(&sysfs_lock);
  575. if (!test_bit(FLAG_EXPORT, &desc->flags))
  576. goto err_unlock;
  577. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  578. if (!dev)
  579. goto err_unlock;
  580. data = dev_get_drvdata(dev);
  581. clear_bit(FLAG_EXPORT, &desc->flags);
  582. device_unregister(dev);
  583. /*
  584. * Release irq after deregistration to prevent race with edge_store.
  585. */
  586. if (data->irq_flags)
  587. gpio_sysfs_free_irq(dev);
  588. mutex_unlock(&sysfs_lock);
  589. put_device(dev);
  590. kfree(data);
  591. return;
  592. err_unlock:
  593. mutex_unlock(&sysfs_lock);
  594. }
  595. EXPORT_SYMBOL_GPL(gpiod_unexport);
  596. int gpiochip_sysfs_register(struct gpio_device *gdev)
  597. {
  598. struct gpio_chip *chip;
  599. struct device *parent;
  600. struct device *dev;
  601. /*
  602. * Many systems add gpio chips for SOC support very early,
  603. * before driver model support is available. In those cases we
  604. * register later, in gpiolib_sysfs_init() ... here we just
  605. * verify that _some_ field of gpio_class got initialized.
  606. */
  607. if (!class_is_registered(&gpio_class))
  608. return 0;
  609. guard(srcu)(&gdev->srcu);
  610. chip = srcu_dereference(gdev->chip, &gdev->srcu);
  611. if (!chip)
  612. return -ENODEV;
  613. /*
  614. * For sysfs backward compatibility we need to preserve this
  615. * preferred parenting to the gpio_chip parent field, if set.
  616. */
  617. if (chip->parent)
  618. parent = chip->parent;
  619. else
  620. parent = &gdev->dev;
  621. /* use chip->base for the ID; it's already known to be unique */
  622. dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), gdev,
  623. gpiochip_groups, GPIOCHIP_NAME "%d",
  624. chip->base);
  625. if (IS_ERR(dev))
  626. return PTR_ERR(dev);
  627. mutex_lock(&sysfs_lock);
  628. gdev->mockdev = dev;
  629. mutex_unlock(&sysfs_lock);
  630. return 0;
  631. }
  632. void gpiochip_sysfs_unregister(struct gpio_device *gdev)
  633. {
  634. struct gpio_desc *desc;
  635. struct gpio_chip *chip;
  636. scoped_guard(mutex, &sysfs_lock) {
  637. if (!gdev->mockdev)
  638. return;
  639. device_unregister(gdev->mockdev);
  640. /* prevent further gpiod exports */
  641. gdev->mockdev = NULL;
  642. }
  643. guard(srcu)(&gdev->srcu);
  644. chip = srcu_dereference(gdev->chip, &gdev->srcu);
  645. if (!chip)
  646. return;
  647. /* unregister gpiod class devices owned by sysfs */
  648. for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) {
  649. gpiod_unexport(desc);
  650. gpiod_free(desc);
  651. }
  652. }
  653. /*
  654. * We're not really looking for a device - we just want to iterate over the
  655. * list and call this callback for each GPIO device. This is why this function
  656. * always returns 0.
  657. */
  658. static int gpiofind_sysfs_register(struct gpio_chip *gc, const void *data)
  659. {
  660. struct gpio_device *gdev = gc->gpiodev;
  661. int ret;
  662. if (gdev->mockdev)
  663. return 0;
  664. ret = gpiochip_sysfs_register(gdev);
  665. if (ret)
  666. chip_err(gc, "failed to register the sysfs entry: %d\n", ret);
  667. return 0;
  668. }
  669. static int __init gpiolib_sysfs_init(void)
  670. {
  671. int status;
  672. status = class_register(&gpio_class);
  673. if (status < 0)
  674. return status;
  675. /* Scan and register the gpio_chips which registered very
  676. * early (e.g. before the class_register above was called).
  677. *
  678. * We run before arch_initcall() so chip->dev nodes can have
  679. * registered, and so arch_initcall() can always gpiod_export().
  680. */
  681. (void)gpio_device_find(NULL, gpiofind_sysfs_register);
  682. return 0;
  683. }
  684. postcore_initcall(gpiolib_sysfs_init);