gpiolib-sysfs.c 19 KB

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