gpiolib-sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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->gdev->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->gdev->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->gdev->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 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. * @gpio: 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. * @gpio: 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,
  607. MKDEV(0, 0),
  608. chip, gpiochip_groups,
  609. "gpiochip%d", chip->base);
  610. if (IS_ERR(dev))
  611. return PTR_ERR(dev);
  612. mutex_lock(&sysfs_lock);
  613. gdev->mockdev = dev;
  614. mutex_unlock(&sysfs_lock);
  615. return 0;
  616. }
  617. void gpiochip_sysfs_unregister(struct gpio_device *gdev)
  618. {
  619. struct gpio_desc *desc;
  620. struct gpio_chip *chip = gdev->chip;
  621. unsigned int i;
  622. if (!gdev->mockdev)
  623. return;
  624. device_unregister(gdev->mockdev);
  625. /* prevent further gpiod exports */
  626. mutex_lock(&sysfs_lock);
  627. gdev->mockdev = NULL;
  628. mutex_unlock(&sysfs_lock);
  629. /* unregister gpiod class devices owned by sysfs */
  630. for (i = 0; i < chip->ngpio; i++) {
  631. desc = &gdev->descs[i];
  632. if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
  633. gpiod_free(desc);
  634. }
  635. }
  636. static int __init gpiolib_sysfs_init(void)
  637. {
  638. int status;
  639. unsigned long flags;
  640. struct gpio_device *gdev;
  641. status = class_register(&gpio_class);
  642. if (status < 0)
  643. return status;
  644. /* Scan and register the gpio_chips which registered very
  645. * early (e.g. before the class_register above was called).
  646. *
  647. * We run before arch_initcall() so chip->dev nodes can have
  648. * registered, and so arch_initcall() can always gpio_export().
  649. */
  650. spin_lock_irqsave(&gpio_lock, flags);
  651. list_for_each_entry(gdev, &gpio_devices, list) {
  652. if (gdev->mockdev)
  653. continue;
  654. /*
  655. * TODO we yield gpio_lock here because
  656. * gpiochip_sysfs_register() acquires a mutex. This is unsafe
  657. * and needs to be fixed.
  658. *
  659. * Also it would be nice to use gpiochip_find() here so we
  660. * can keep gpio_chips local to gpiolib.c, but the yield of
  661. * gpio_lock prevents us from doing this.
  662. */
  663. spin_unlock_irqrestore(&gpio_lock, flags);
  664. status = gpiochip_sysfs_register(gdev);
  665. spin_lock_irqsave(&gpio_lock, flags);
  666. }
  667. spin_unlock_irqrestore(&gpio_lock, flags);
  668. return status;
  669. }
  670. postcore_initcall(gpiolib_sysfs_init);