ledtrig-adm5120-switch.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * LED ADM5120 Switch Port State Trigger
  3. *
  4. * Copyright (C) 2007 Bernhard Held <bernhard at bernhardheld.de>
  5. * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
  6. *
  7. * This file was based on: drivers/leds/ledtrig-timer.c
  8. * Copyright 2005-2006 Openedhand Ltd.
  9. * Author: Richard Purdie
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/gpio.h>
  21. #include "leds.h"
  22. #define DRV_NAME "port_state"
  23. #define DRV_DESC "LED ADM5120 Switch Port State Trigger"
  24. struct port_state {
  25. char *name;
  26. unsigned int value;
  27. };
  28. #define PORT_STATE(n, v) {.name = (n), .value = (v)}
  29. static struct port_state port_states[] = {
  30. PORT_STATE("off", LED_OFF),
  31. PORT_STATE("on", LED_FULL),
  32. PORT_STATE("flash", ADM5120_GPIO_FLASH),
  33. PORT_STATE("link", ADM5120_GPIO_LINK),
  34. PORT_STATE("speed", ADM5120_GPIO_SPEED),
  35. PORT_STATE("duplex", ADM5120_GPIO_DUPLEX),
  36. PORT_STATE("act", ADM5120_GPIO_ACT),
  37. PORT_STATE("coll", ADM5120_GPIO_COLL),
  38. PORT_STATE("link_act", ADM5120_GPIO_LINK_ACT),
  39. PORT_STATE("duplex_coll", ADM5120_GPIO_DUPLEX_COLL),
  40. PORT_STATE("10M_act", ADM5120_GPIO_10M_ACT),
  41. PORT_STATE("100M_act", ADM5120_GPIO_100M_ACT),
  42. };
  43. static ssize_t led_port_state_show(struct device *dev,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  47. struct port_state *state = led_cdev->trigger_data;
  48. int len = 0;
  49. int i;
  50. *buf = '\0';
  51. for (i = 0; i < ARRAY_SIZE(port_states); i++) {
  52. if (&port_states[i] == state)
  53. len += sprintf(buf+len, "[%s] ", port_states[i].name);
  54. else
  55. len += sprintf(buf+len, "%s ", port_states[i].name);
  56. }
  57. len += sprintf(buf+len, "\n");
  58. return len;
  59. }
  60. static ssize_t led_port_state_store(struct device *dev,
  61. struct device_attribute *attr, const char *buf, size_t size)
  62. {
  63. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  64. size_t len;
  65. int i;
  66. for (i = 0; i < ARRAY_SIZE(port_states); i++) {
  67. len = strlen(port_states[i].name);
  68. if (strncmp(port_states[i].name, buf, len) != 0)
  69. continue;
  70. if (buf[len] != '\0' && buf[len] != '\n')
  71. continue;
  72. led_cdev->trigger_data = &port_states[i];
  73. led_set_brightness(led_cdev, port_states[i].value);
  74. return size;
  75. }
  76. return -EINVAL;
  77. }
  78. static DEVICE_ATTR(port_state, 0644, led_port_state_show,
  79. led_port_state_store);
  80. static void adm5120_switch_trig_activate(struct led_classdev *led_cdev)
  81. {
  82. struct port_state *state = port_states;
  83. int rc;
  84. led_cdev->trigger_data = state;
  85. rc = device_create_file(led_cdev->dev, &dev_attr_port_state);
  86. if (rc)
  87. goto err;
  88. led_set_brightness(led_cdev, state->value);
  89. return;
  90. err:
  91. led_cdev->trigger_data = NULL;
  92. }
  93. static void adm5120_switch_trig_deactivate(struct led_classdev *led_cdev)
  94. {
  95. struct port_state *state = led_cdev->trigger_data;
  96. if (!state)
  97. return;
  98. device_remove_file(led_cdev->dev, &dev_attr_port_state);
  99. }
  100. static struct led_trigger adm5120_switch_led_trigger = {
  101. .name = DRV_NAME,
  102. .activate = adm5120_switch_trig_activate,
  103. .deactivate = adm5120_switch_trig_deactivate,
  104. };
  105. static int __init adm5120_switch_trig_init(void)
  106. {
  107. led_trigger_register(&adm5120_switch_led_trigger);
  108. return 0;
  109. }
  110. static void __exit adm5120_switch_trig_exit(void)
  111. {
  112. led_trigger_unregister(&adm5120_switch_led_trigger);
  113. }
  114. module_init(adm5120_switch_trig_init);
  115. module_exit(adm5120_switch_trig_exit);
  116. MODULE_AUTHOR("Bernhard Held <bernhard at bernhardheld.de>, "
  117. "Gabor Juhos <juhosg@openwrt.org>");
  118. MODULE_DESCRIPTION(DRV_DESC);
  119. MODULE_LICENSE("GPL v2");