gpiolib-sysfs.c 20 KB

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