rte_cb_leds.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * include/asm-v850/rte_cb_leds.c -- Midas lab RTE-CB board LED device support
  3. *
  4. * Copyright (C) 2002,03 NEC Electronics Corporation
  5. * Copyright (C) 2002,03 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * Written by Miles Bader <miles@gnu.org>
  12. */
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/fs.h>
  16. #include <linux/miscdevice.h>
  17. #include <asm/uaccess.h>
  18. #define LEDS_MINOR 169 /* Minor device number, using misc major. */
  19. /* The actual LED hardware is write-only, so we hold the contents here too. */
  20. static unsigned char leds_image[LED_NUM_DIGITS] = { 0 };
  21. /* Spinlock protecting the above leds. */
  22. static DEFINE_SPINLOCK(leds_lock);
  23. /* Common body of LED read/write functions, checks POS and LEN for
  24. correctness, declares a variable using IMG_DECL, initialized pointing at
  25. the POS position in the LED image buffer, and and iterates COPY_EXPR
  26. until BUF is equal to the last buffer position; finally, sets LEN to be
  27. the amount actually copied. IMG should be a variable declaration
  28. (without an initializer or a terminating semicolon); POS, BUF, and LEN
  29. should all be simple variables. */
  30. #define DO_LED_COPY(img_decl, pos, buf, len, copy_expr) \
  31. do { \
  32. if (pos > LED_NUM_DIGITS) \
  33. len = 0; \
  34. else { \
  35. if (pos + len > LED_NUM_DIGITS) \
  36. len = LED_NUM_DIGITS - pos; \
  37. \
  38. if (len > 0) { \
  39. unsigned long _flags; \
  40. const char *_end = buf + len; \
  41. img_decl = &leds_image[pos]; \
  42. \
  43. spin_lock_irqsave (leds_lock, _flags); \
  44. do \
  45. (copy_expr); \
  46. while (buf != _end); \
  47. spin_unlock_irqrestore (leds_lock, _flags); \
  48. } \
  49. } \
  50. } while (0)
  51. /* Read LEN bytes from LEDs at position POS, into BUF.
  52. Returns actual amount read. */
  53. unsigned read_leds (unsigned pos, char *buf, unsigned len)
  54. {
  55. DO_LED_COPY (const char *img, pos, buf, len, *buf++ = *img++);
  56. return len;
  57. }
  58. /* Write LEN bytes to LEDs at position POS, from BUF.
  59. Returns actual amount written. */
  60. unsigned write_leds (unsigned pos, const char *buf, unsigned len)
  61. {
  62. /* We write the actual LED values backwards, because
  63. increasing memory addresses reflect LEDs right-to-left. */
  64. volatile char *led = &LED (LED_NUM_DIGITS - pos - 1);
  65. /* We invert the value written to the hardware, because 1 = off,
  66. and 0 = on. */
  67. DO_LED_COPY (char *img, pos, buf, len,
  68. *led-- = 0xFF ^ (*img++ = *buf++));
  69. return len;
  70. }
  71. /* Device functions. */
  72. static ssize_t leds_dev_read (struct file *file, char *buf, size_t len,
  73. loff_t *pos)
  74. {
  75. char temp_buf[LED_NUM_DIGITS];
  76. len = read_leds (*pos, temp_buf, len);
  77. if (copy_to_user (buf, temp_buf, len))
  78. return -EFAULT;
  79. *pos += len;
  80. return len;
  81. }
  82. static ssize_t leds_dev_write (struct file *file, const char *buf, size_t len,
  83. loff_t *pos)
  84. {
  85. char temp_buf[LED_NUM_DIGITS];
  86. if (copy_from_user (temp_buf, buf, min_t(size_t, len, LED_NUM_DIGITS)))
  87. return -EFAULT;
  88. len = write_leds (*pos, temp_buf, len);
  89. *pos += len;
  90. return len;
  91. }
  92. static loff_t leds_dev_lseek (struct file *file, loff_t offs, int whence)
  93. {
  94. if (whence == 1)
  95. offs += file->f_pos; /* relative */
  96. else if (whence == 2)
  97. offs += LED_NUM_DIGITS; /* end-relative */
  98. if (offs < 0 || offs > LED_NUM_DIGITS)
  99. return -EINVAL;
  100. file->f_pos = offs;
  101. return 0;
  102. }
  103. static struct file_operations leds_fops = {
  104. .read = leds_dev_read,
  105. .write = leds_dev_write,
  106. .llseek = leds_dev_lseek
  107. };
  108. static struct miscdevice leds_miscdev = {
  109. .name = "leds",
  110. .minor = LEDS_MINOR,
  111. .fops = &leds_fops
  112. };
  113. int __init leds_dev_init (void)
  114. {
  115. return misc_register (&leds_miscdev);
  116. }
  117. __initcall (leds_dev_init);