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