line-display.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Character line display core support
  4. *
  5. * Copyright (C) 2016 Imagination Technologies
  6. * Author: Paul Burton <[email protected]>
  7. *
  8. * Copyright (C) 2021 Glider bv
  9. */
  10. #include <generated/utsrelease.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/string.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/timer.h>
  17. #include "line-display.h"
  18. #define DEFAULT_SCROLL_RATE (HZ / 2)
  19. /**
  20. * linedisp_scroll() - scroll the display by a character
  21. * @t: really a pointer to the private data structure
  22. *
  23. * Scroll the current message along the display by one character, rearming the
  24. * timer if required.
  25. */
  26. static void linedisp_scroll(struct timer_list *t)
  27. {
  28. struct linedisp *linedisp = from_timer(linedisp, t, timer);
  29. unsigned int i, ch = linedisp->scroll_pos;
  30. unsigned int num_chars = linedisp->num_chars;
  31. /* update the current message string */
  32. for (i = 0; i < num_chars;) {
  33. /* copy as many characters from the string as possible */
  34. for (; i < num_chars && ch < linedisp->message_len; i++, ch++)
  35. linedisp->buf[i] = linedisp->message[ch];
  36. /* wrap around to the start of the string */
  37. ch = 0;
  38. }
  39. /* update the display */
  40. linedisp->update(linedisp);
  41. /* move on to the next character */
  42. linedisp->scroll_pos++;
  43. linedisp->scroll_pos %= linedisp->message_len;
  44. /* rearm the timer */
  45. if (linedisp->message_len > num_chars && linedisp->scroll_rate)
  46. mod_timer(&linedisp->timer, jiffies + linedisp->scroll_rate);
  47. }
  48. /**
  49. * linedisp_display() - set the message to be displayed
  50. * @linedisp: pointer to the private data structure
  51. * @msg: the message to display
  52. * @count: length of msg, or -1
  53. *
  54. * Display a new message @msg on the display. @msg can be longer than the
  55. * number of characters the display can display, in which case it will begin
  56. * scrolling across the display.
  57. *
  58. * Return: 0 on success, -ENOMEM on memory allocation failure
  59. */
  60. static int linedisp_display(struct linedisp *linedisp, const char *msg,
  61. ssize_t count)
  62. {
  63. char *new_msg;
  64. /* stop the scroll timer */
  65. del_timer_sync(&linedisp->timer);
  66. if (count == -1)
  67. count = strlen(msg);
  68. /* if the string ends with a newline, trim it */
  69. if (msg[count - 1] == '\n')
  70. count--;
  71. if (!count) {
  72. /* Clear the display */
  73. kfree(linedisp->message);
  74. linedisp->message = NULL;
  75. linedisp->message_len = 0;
  76. memset(linedisp->buf, ' ', linedisp->num_chars);
  77. linedisp->update(linedisp);
  78. return 0;
  79. }
  80. new_msg = kmemdup_nul(msg, count, GFP_KERNEL);
  81. if (!new_msg)
  82. return -ENOMEM;
  83. kfree(linedisp->message);
  84. linedisp->message = new_msg;
  85. linedisp->message_len = count;
  86. linedisp->scroll_pos = 0;
  87. /* update the display */
  88. linedisp_scroll(&linedisp->timer);
  89. return 0;
  90. }
  91. /**
  92. * message_show() - read message via sysfs
  93. * @dev: the display device
  94. * @attr: the display message attribute
  95. * @buf: the buffer to read the message into
  96. *
  97. * Read the current message being displayed or scrolled across the display into
  98. * @buf, for reads from sysfs.
  99. *
  100. * Return: the number of characters written to @buf
  101. */
  102. static ssize_t message_show(struct device *dev, struct device_attribute *attr,
  103. char *buf)
  104. {
  105. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  106. return sysfs_emit(buf, "%s\n", linedisp->message);
  107. }
  108. /**
  109. * message_store() - write a new message via sysfs
  110. * @dev: the display device
  111. * @attr: the display message attribute
  112. * @buf: the buffer containing the new message
  113. * @count: the size of the message in @buf
  114. *
  115. * Write a new message to display or scroll across the display from sysfs.
  116. *
  117. * Return: the size of the message on success, else -ERRNO
  118. */
  119. static ssize_t message_store(struct device *dev, struct device_attribute *attr,
  120. const char *buf, size_t count)
  121. {
  122. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  123. int err;
  124. err = linedisp_display(linedisp, buf, count);
  125. return err ?: count;
  126. }
  127. static DEVICE_ATTR_RW(message);
  128. static ssize_t scroll_step_ms_show(struct device *dev,
  129. struct device_attribute *attr, char *buf)
  130. {
  131. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  132. return sysfs_emit(buf, "%u\n", jiffies_to_msecs(linedisp->scroll_rate));
  133. }
  134. static ssize_t scroll_step_ms_store(struct device *dev,
  135. struct device_attribute *attr,
  136. const char *buf, size_t count)
  137. {
  138. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  139. unsigned int ms;
  140. if (kstrtouint(buf, 10, &ms) != 0)
  141. return -EINVAL;
  142. linedisp->scroll_rate = msecs_to_jiffies(ms);
  143. if (linedisp->message && linedisp->message_len > linedisp->num_chars) {
  144. del_timer_sync(&linedisp->timer);
  145. if (linedisp->scroll_rate)
  146. linedisp_scroll(&linedisp->timer);
  147. }
  148. return count;
  149. }
  150. static DEVICE_ATTR_RW(scroll_step_ms);
  151. static struct attribute *linedisp_attrs[] = {
  152. &dev_attr_message.attr,
  153. &dev_attr_scroll_step_ms.attr,
  154. NULL,
  155. };
  156. ATTRIBUTE_GROUPS(linedisp);
  157. static const struct device_type linedisp_type = {
  158. .groups = linedisp_groups,
  159. };
  160. /**
  161. * linedisp_register - register a character line display
  162. * @linedisp: pointer to character line display structure
  163. * @parent: parent device
  164. * @num_chars: the number of characters that can be displayed
  165. * @buf: pointer to a buffer that can hold @num_chars characters
  166. * @update: Function called to update the display. This must not sleep!
  167. *
  168. * Return: zero on success, else a negative error code.
  169. */
  170. int linedisp_register(struct linedisp *linedisp, struct device *parent,
  171. unsigned int num_chars, char *buf,
  172. void (*update)(struct linedisp *linedisp))
  173. {
  174. static atomic_t linedisp_id = ATOMIC_INIT(-1);
  175. int err;
  176. memset(linedisp, 0, sizeof(*linedisp));
  177. linedisp->dev.parent = parent;
  178. linedisp->dev.type = &linedisp_type;
  179. linedisp->update = update;
  180. linedisp->buf = buf;
  181. linedisp->num_chars = num_chars;
  182. linedisp->scroll_rate = DEFAULT_SCROLL_RATE;
  183. device_initialize(&linedisp->dev);
  184. dev_set_name(&linedisp->dev, "linedisp.%lu",
  185. (unsigned long)atomic_inc_return(&linedisp_id));
  186. /* initialise a timer for scrolling the message */
  187. timer_setup(&linedisp->timer, linedisp_scroll, 0);
  188. err = device_add(&linedisp->dev);
  189. if (err)
  190. goto out_del_timer;
  191. /* display a default message */
  192. err = linedisp_display(linedisp, "Linux " UTS_RELEASE " ", -1);
  193. if (err)
  194. goto out_del_dev;
  195. return 0;
  196. out_del_dev:
  197. device_del(&linedisp->dev);
  198. out_del_timer:
  199. del_timer_sync(&linedisp->timer);
  200. put_device(&linedisp->dev);
  201. return err;
  202. }
  203. EXPORT_SYMBOL_GPL(linedisp_register);
  204. /**
  205. * linedisp_unregister - unregister a character line display
  206. * @linedisp: pointer to character line display structure registered previously
  207. * with linedisp_register()
  208. */
  209. void linedisp_unregister(struct linedisp *linedisp)
  210. {
  211. device_del(&linedisp->dev);
  212. del_timer_sync(&linedisp->timer);
  213. kfree(linedisp->message);
  214. put_device(&linedisp->dev);
  215. }
  216. EXPORT_SYMBOL_GPL(linedisp_unregister);
  217. MODULE_LICENSE("GPL");