gpiolib-sysfs.c 20 KB

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