gpiolib-sysfs.c 19 KB

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