gpiolib-sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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 const 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 const DEVICE_ATTR(active_low, 0644,
  285. gpio_active_low_show, gpio_active_low_store);
  286. static const struct attribute *gpio_attrs[] = {
  287. &dev_attr_value.attr,
  288. &dev_attr_active_low.attr,
  289. NULL,
  290. };
  291. static const struct attribute_group gpio_attr_group = {
  292. .attrs = (struct attribute **) gpio_attrs,
  293. };
  294. /*
  295. * /sys/class/gpio/gpiochipN/
  296. * /base ... matching gpio_chip.base (N)
  297. * /label ... matching gpio_chip.label
  298. * /ngpio ... matching gpio_chip.ngpio
  299. */
  300. static ssize_t chip_base_show(struct device *dev,
  301. struct device_attribute *attr, char *buf)
  302. {
  303. const struct gpio_chip *chip = dev_get_drvdata(dev);
  304. return sprintf(buf, "%d\n", chip->base);
  305. }
  306. static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
  307. static ssize_t chip_label_show(struct device *dev,
  308. struct device_attribute *attr, char *buf)
  309. {
  310. const struct gpio_chip *chip = dev_get_drvdata(dev);
  311. return sprintf(buf, "%s\n", chip->label ? : "");
  312. }
  313. static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
  314. static ssize_t chip_ngpio_show(struct device *dev,
  315. struct device_attribute *attr, char *buf)
  316. {
  317. const struct gpio_chip *chip = dev_get_drvdata(dev);
  318. return sprintf(buf, "%u\n", chip->ngpio);
  319. }
  320. static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
  321. static const struct attribute *gpiochip_attrs[] = {
  322. &dev_attr_base.attr,
  323. &dev_attr_label.attr,
  324. &dev_attr_ngpio.attr,
  325. NULL,
  326. };
  327. static const struct attribute_group gpiochip_attr_group = {
  328. .attrs = (struct attribute **) gpiochip_attrs,
  329. };
  330. /*
  331. * /sys/class/gpio/export ... write-only
  332. * integer N ... number of GPIO to export (full access)
  333. * /sys/class/gpio/unexport ... write-only
  334. * integer N ... number of GPIO to unexport
  335. */
  336. static ssize_t export_store(struct class *class,
  337. struct class_attribute *attr,
  338. const char *buf, size_t len)
  339. {
  340. long gpio;
  341. struct gpio_desc *desc;
  342. int status;
  343. status = kstrtol(buf, 0, &gpio);
  344. if (status < 0)
  345. goto done;
  346. desc = gpio_to_desc(gpio);
  347. /* reject invalid GPIOs */
  348. if (!desc) {
  349. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  350. return -EINVAL;
  351. }
  352. /* No extra locking here; FLAG_SYSFS just signifies that the
  353. * request and export were done by on behalf of userspace, so
  354. * they may be undone on its behalf too.
  355. */
  356. status = gpiod_request(desc, "sysfs");
  357. if (status < 0) {
  358. if (status == -EPROBE_DEFER)
  359. status = -ENODEV;
  360. goto done;
  361. }
  362. status = gpiod_export(desc, true);
  363. if (status < 0)
  364. gpiod_free(desc);
  365. else
  366. set_bit(FLAG_SYSFS, &desc->flags);
  367. done:
  368. if (status)
  369. pr_debug("%s: status %d\n", __func__, status);
  370. return status ? : len;
  371. }
  372. static ssize_t unexport_store(struct class *class,
  373. struct class_attribute *attr,
  374. const char *buf, size_t len)
  375. {
  376. long gpio;
  377. struct gpio_desc *desc;
  378. int status;
  379. status = kstrtol(buf, 0, &gpio);
  380. if (status < 0)
  381. goto done;
  382. desc = gpio_to_desc(gpio);
  383. /* reject bogus commands (gpio_unexport ignores them) */
  384. if (!desc) {
  385. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  386. return -EINVAL;
  387. }
  388. status = -EINVAL;
  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. if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
  394. status = 0;
  395. gpiod_free(desc);
  396. }
  397. done:
  398. if (status)
  399. pr_debug("%s: status %d\n", __func__, status);
  400. return status ? : len;
  401. }
  402. static struct class_attribute gpio_class_attrs[] = {
  403. __ATTR(export, 0200, NULL, export_store),
  404. __ATTR(unexport, 0200, NULL, unexport_store),
  405. __ATTR_NULL,
  406. };
  407. static struct class gpio_class = {
  408. .name = "gpio",
  409. .owner = THIS_MODULE,
  410. .class_attrs = gpio_class_attrs,
  411. };
  412. /**
  413. * gpiod_export - export a GPIO through sysfs
  414. * @gpio: gpio to make available, already requested
  415. * @direction_may_change: true if userspace may change gpio direction
  416. * Context: arch_initcall or later
  417. *
  418. * When drivers want to make a GPIO accessible to userspace after they
  419. * have requested it -- perhaps while debugging, or as part of their
  420. * public interface -- they may use this routine. If the GPIO can
  421. * change direction (some can't) and the caller allows it, userspace
  422. * will see "direction" sysfs attribute which may be used to change
  423. * the gpio's direction. A "value" attribute will always be provided.
  424. *
  425. * Returns zero on success, else an error.
  426. */
  427. int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  428. {
  429. unsigned long flags;
  430. int status;
  431. const char *ioname = NULL;
  432. struct device *dev;
  433. int offset;
  434. /* can't export until sysfs is available ... */
  435. if (!gpio_class.p) {
  436. pr_debug("%s: called too early!\n", __func__);
  437. return -ENOENT;
  438. }
  439. if (!desc) {
  440. pr_debug("%s: invalid gpio descriptor\n", __func__);
  441. return -EINVAL;
  442. }
  443. mutex_lock(&sysfs_lock);
  444. spin_lock_irqsave(&gpio_lock, flags);
  445. if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
  446. test_bit(FLAG_EXPORT, &desc->flags)) {
  447. spin_unlock_irqrestore(&gpio_lock, flags);
  448. gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
  449. __func__,
  450. test_bit(FLAG_REQUESTED, &desc->flags),
  451. test_bit(FLAG_EXPORT, &desc->flags));
  452. status = -EPERM;
  453. goto fail_unlock;
  454. }
  455. if (!desc->chip->direction_input || !desc->chip->direction_output)
  456. direction_may_change = false;
  457. spin_unlock_irqrestore(&gpio_lock, flags);
  458. offset = gpio_chip_hwgpio(desc);
  459. if (desc->chip->names && desc->chip->names[offset])
  460. ioname = desc->chip->names[offset];
  461. dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
  462. desc, ioname ? ioname : "gpio%u",
  463. desc_to_gpio(desc));
  464. if (IS_ERR(dev)) {
  465. status = PTR_ERR(dev);
  466. goto fail_unlock;
  467. }
  468. status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
  469. if (status)
  470. goto fail_unregister_device;
  471. if (direction_may_change) {
  472. status = device_create_file(dev, &dev_attr_direction);
  473. if (status)
  474. goto fail_unregister_device;
  475. }
  476. if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
  477. !test_bit(FLAG_IS_OUT, &desc->flags))) {
  478. status = device_create_file(dev, &dev_attr_edge);
  479. if (status)
  480. goto fail_unregister_device;
  481. }
  482. set_bit(FLAG_EXPORT, &desc->flags);
  483. mutex_unlock(&sysfs_lock);
  484. return 0;
  485. fail_unregister_device:
  486. device_unregister(dev);
  487. fail_unlock:
  488. mutex_unlock(&sysfs_lock);
  489. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  490. return status;
  491. }
  492. EXPORT_SYMBOL_GPL(gpiod_export);
  493. static int match_export(struct device *dev, const void *data)
  494. {
  495. return dev_get_drvdata(dev) == data;
  496. }
  497. /**
  498. * gpiod_export_link - create a sysfs link to an exported GPIO node
  499. * @dev: device under which to create symlink
  500. * @name: name of the symlink
  501. * @gpio: gpio to create symlink to, already exported
  502. *
  503. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  504. * node. Caller is responsible for unlinking.
  505. *
  506. * Returns zero on success, else an error.
  507. */
  508. int gpiod_export_link(struct device *dev, const char *name,
  509. struct gpio_desc *desc)
  510. {
  511. int status = -EINVAL;
  512. if (!desc) {
  513. pr_warn("%s: invalid GPIO\n", __func__);
  514. return -EINVAL;
  515. }
  516. mutex_lock(&sysfs_lock);
  517. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  518. struct device *tdev;
  519. tdev = class_find_device(&gpio_class, NULL, desc, match_export);
  520. if (tdev != NULL) {
  521. status = sysfs_create_link(&dev->kobj, &tdev->kobj,
  522. name);
  523. } else {
  524. status = -ENODEV;
  525. }
  526. }
  527. mutex_unlock(&sysfs_lock);
  528. if (status)
  529. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  530. return status;
  531. }
  532. EXPORT_SYMBOL_GPL(gpiod_export_link);
  533. /**
  534. * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
  535. * @gpio: gpio to change
  536. * @value: non-zero to use active low, i.e. inverted values
  537. *
  538. * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
  539. * The GPIO does not have to be exported yet. If poll(2) support has
  540. * been enabled for either rising or falling edge, it will be
  541. * reconfigured to follow the new polarity.
  542. *
  543. * Returns zero on success, else an error.
  544. */
  545. int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  546. {
  547. struct device *dev = NULL;
  548. int status = -EINVAL;
  549. if (!desc) {
  550. pr_warn("%s: invalid GPIO\n", __func__);
  551. return -EINVAL;
  552. }
  553. mutex_lock(&sysfs_lock);
  554. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  555. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  556. if (dev == NULL) {
  557. status = -ENODEV;
  558. goto unlock;
  559. }
  560. }
  561. status = sysfs_set_active_low(desc, dev, value);
  562. unlock:
  563. mutex_unlock(&sysfs_lock);
  564. if (status)
  565. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  566. return status;
  567. }
  568. EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
  569. /**
  570. * gpiod_unexport - reverse effect of gpio_export()
  571. * @gpio: gpio to make unavailable
  572. *
  573. * This is implicit on gpio_free().
  574. */
  575. void gpiod_unexport(struct gpio_desc *desc)
  576. {
  577. int status = 0;
  578. struct device *dev = NULL;
  579. if (!desc) {
  580. pr_warn("%s: invalid GPIO\n", __func__);
  581. return;
  582. }
  583. mutex_lock(&sysfs_lock);
  584. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  585. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  586. if (dev) {
  587. gpio_setup_irq(desc, dev, 0);
  588. clear_bit(FLAG_EXPORT, &desc->flags);
  589. } else
  590. status = -ENODEV;
  591. }
  592. mutex_unlock(&sysfs_lock);
  593. if (dev) {
  594. device_unregister(dev);
  595. put_device(dev);
  596. }
  597. if (status)
  598. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  599. }
  600. EXPORT_SYMBOL_GPL(gpiod_unexport);
  601. int gpiochip_export(struct gpio_chip *chip)
  602. {
  603. int status;
  604. struct device *dev;
  605. /* Many systems register gpio chips for SOC support very early,
  606. * before driver model support is available. In those cases we
  607. * export this later, in gpiolib_sysfs_init() ... here we just
  608. * verify that _some_ field of gpio_class got initialized.
  609. */
  610. if (!gpio_class.p)
  611. return 0;
  612. /* use chip->base for the ID; it's already known to be unique */
  613. mutex_lock(&sysfs_lock);
  614. dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
  615. "gpiochip%d", chip->base);
  616. if (!IS_ERR(dev)) {
  617. status = sysfs_create_group(&dev->kobj,
  618. &gpiochip_attr_group);
  619. } else
  620. status = PTR_ERR(dev);
  621. chip->exported = (status == 0);
  622. mutex_unlock(&sysfs_lock);
  623. if (status)
  624. chip_dbg(chip, "%s: status %d\n", __func__, status);
  625. return status;
  626. }
  627. void gpiochip_unexport(struct gpio_chip *chip)
  628. {
  629. int status;
  630. struct device *dev;
  631. mutex_lock(&sysfs_lock);
  632. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  633. if (dev) {
  634. put_device(dev);
  635. device_unregister(dev);
  636. chip->exported = false;
  637. status = 0;
  638. } else
  639. status = -ENODEV;
  640. mutex_unlock(&sysfs_lock);
  641. if (status)
  642. chip_dbg(chip, "%s: status %d\n", __func__, status);
  643. }
  644. static int __init gpiolib_sysfs_init(void)
  645. {
  646. int status;
  647. unsigned long flags;
  648. struct gpio_chip *chip;
  649. status = class_register(&gpio_class);
  650. if (status < 0)
  651. return status;
  652. /* Scan and register the gpio_chips which registered very
  653. * early (e.g. before the class_register above was called).
  654. *
  655. * We run before arch_initcall() so chip->dev nodes can have
  656. * registered, and so arch_initcall() can always gpio_export().
  657. */
  658. spin_lock_irqsave(&gpio_lock, flags);
  659. list_for_each_entry(chip, &gpio_chips, list) {
  660. if (chip->exported)
  661. continue;
  662. /*
  663. * TODO we yield gpio_lock here because gpiochip_export()
  664. * acquires a mutex. This is unsafe and needs to be fixed.
  665. *
  666. * Also it would be nice to use gpiochip_find() here so we
  667. * can keep gpio_chips local to gpiolib.c, but the yield of
  668. * gpio_lock prevents us from doing this.
  669. */
  670. spin_unlock_irqrestore(&gpio_lock, flags);
  671. status = gpiochip_export(chip);
  672. spin_lock_irqsave(&gpio_lock, flags);
  673. }
  674. spin_unlock_irqrestore(&gpio_lock, flags);
  675. return status;
  676. }
  677. postcore_initcall(gpiolib_sysfs_init);