gpiolib-sysfs.c 19 KB

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