gpiolib-sysfs.c 20 KB

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