line-display.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <paul.burton@mips.com>
  7. *
  8. * Copyright (C) 2021 Glider bv
  9. */
  10. #ifndef CONFIG_PANEL_BOOT_MESSAGE
  11. #include <generated/utsrelease.h>
  12. #endif
  13. #include <linux/container_of.h>
  14. #include <linux/device.h>
  15. #include <linux/export.h>
  16. #include <linux/idr.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/kstrtox.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/timer.h>
  24. #include <linux/map_to_7segment.h>
  25. #include <linux/map_to_14segment.h>
  26. #include "line-display.h"
  27. #define DEFAULT_SCROLL_RATE (HZ / 2)
  28. /**
  29. * linedisp_scroll() - scroll the display by a character
  30. * @t: really a pointer to the private data structure
  31. *
  32. * Scroll the current message along the display by one character, rearming the
  33. * timer if required.
  34. */
  35. static void linedisp_scroll(struct timer_list *t)
  36. {
  37. struct linedisp *linedisp = from_timer(linedisp, t, timer);
  38. unsigned int i, ch = linedisp->scroll_pos;
  39. unsigned int num_chars = linedisp->num_chars;
  40. /* update the current message string */
  41. for (i = 0; i < num_chars;) {
  42. /* copy as many characters from the string as possible */
  43. for (; i < num_chars && ch < linedisp->message_len; i++, ch++)
  44. linedisp->buf[i] = linedisp->message[ch];
  45. /* wrap around to the start of the string */
  46. ch = 0;
  47. }
  48. /* update the display */
  49. linedisp->ops->update(linedisp);
  50. /* move on to the next character */
  51. linedisp->scroll_pos++;
  52. linedisp->scroll_pos %= linedisp->message_len;
  53. /* rearm the timer */
  54. if (linedisp->message_len > num_chars && linedisp->scroll_rate)
  55. mod_timer(&linedisp->timer, jiffies + linedisp->scroll_rate);
  56. }
  57. /**
  58. * linedisp_display() - set the message to be displayed
  59. * @linedisp: pointer to the private data structure
  60. * @msg: the message to display
  61. * @count: length of msg, or -1
  62. *
  63. * Display a new message @msg on the display. @msg can be longer than the
  64. * number of characters the display can display, in which case it will begin
  65. * scrolling across the display.
  66. *
  67. * Return: 0 on success, -ENOMEM on memory allocation failure
  68. */
  69. static int linedisp_display(struct linedisp *linedisp, const char *msg,
  70. ssize_t count)
  71. {
  72. char *new_msg;
  73. /* stop the scroll timer */
  74. del_timer_sync(&linedisp->timer);
  75. if (count == -1)
  76. count = strlen(msg);
  77. /* if the string ends with a newline, trim it */
  78. if (msg[count - 1] == '\n')
  79. count--;
  80. if (!count) {
  81. /* Clear the display */
  82. kfree(linedisp->message);
  83. linedisp->message = NULL;
  84. linedisp->message_len = 0;
  85. memset(linedisp->buf, ' ', linedisp->num_chars);
  86. linedisp->ops->update(linedisp);
  87. return 0;
  88. }
  89. new_msg = kmemdup_nul(msg, count, GFP_KERNEL);
  90. if (!new_msg)
  91. return -ENOMEM;
  92. kfree(linedisp->message);
  93. linedisp->message = new_msg;
  94. linedisp->message_len = count;
  95. linedisp->scroll_pos = 0;
  96. /* update the display */
  97. linedisp_scroll(&linedisp->timer);
  98. return 0;
  99. }
  100. /**
  101. * message_show() - read message via sysfs
  102. * @dev: the display device
  103. * @attr: the display message attribute
  104. * @buf: the buffer to read the message into
  105. *
  106. * Read the current message being displayed or scrolled across the display into
  107. * @buf, for reads from sysfs.
  108. *
  109. * Return: the number of characters written to @buf
  110. */
  111. static ssize_t message_show(struct device *dev, struct device_attribute *attr,
  112. char *buf)
  113. {
  114. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  115. return sysfs_emit(buf, "%s\n", linedisp->message);
  116. }
  117. /**
  118. * message_store() - write a new message via sysfs
  119. * @dev: the display device
  120. * @attr: the display message attribute
  121. * @buf: the buffer containing the new message
  122. * @count: the size of the message in @buf
  123. *
  124. * Write a new message to display or scroll across the display from sysfs.
  125. *
  126. * Return: the size of the message on success, else -ERRNO
  127. */
  128. static ssize_t message_store(struct device *dev, struct device_attribute *attr,
  129. const char *buf, size_t count)
  130. {
  131. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  132. int err;
  133. err = linedisp_display(linedisp, buf, count);
  134. return err ?: count;
  135. }
  136. static DEVICE_ATTR_RW(message);
  137. static ssize_t scroll_step_ms_show(struct device *dev,
  138. struct device_attribute *attr, char *buf)
  139. {
  140. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  141. return sysfs_emit(buf, "%u\n", jiffies_to_msecs(linedisp->scroll_rate));
  142. }
  143. static ssize_t scroll_step_ms_store(struct device *dev,
  144. struct device_attribute *attr,
  145. const char *buf, size_t count)
  146. {
  147. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  148. unsigned int ms;
  149. int err;
  150. err = kstrtouint(buf, 10, &ms);
  151. if (err)
  152. return err;
  153. linedisp->scroll_rate = msecs_to_jiffies(ms);
  154. if (linedisp->message && linedisp->message_len > linedisp->num_chars) {
  155. del_timer_sync(&linedisp->timer);
  156. if (linedisp->scroll_rate)
  157. linedisp_scroll(&linedisp->timer);
  158. }
  159. return count;
  160. }
  161. static DEVICE_ATTR_RW(scroll_step_ms);
  162. static ssize_t map_seg_show(struct device *dev, struct device_attribute *attr, char *buf)
  163. {
  164. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  165. struct linedisp_map *map = linedisp->map;
  166. memcpy(buf, &map->map, map->size);
  167. return map->size;
  168. }
  169. static ssize_t map_seg_store(struct device *dev, struct device_attribute *attr,
  170. const char *buf, size_t count)
  171. {
  172. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  173. struct linedisp_map *map = linedisp->map;
  174. if (count != map->size)
  175. return -EINVAL;
  176. memcpy(&map->map, buf, count);
  177. return count;
  178. }
  179. static const SEG7_DEFAULT_MAP(initial_map_seg7);
  180. static DEVICE_ATTR(map_seg7, 0644, map_seg_show, map_seg_store);
  181. static const SEG14_DEFAULT_MAP(initial_map_seg14);
  182. static DEVICE_ATTR(map_seg14, 0644, map_seg_show, map_seg_store);
  183. static struct attribute *linedisp_attrs[] = {
  184. &dev_attr_message.attr,
  185. &dev_attr_scroll_step_ms.attr,
  186. &dev_attr_map_seg7.attr,
  187. &dev_attr_map_seg14.attr,
  188. NULL
  189. };
  190. static umode_t linedisp_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
  191. {
  192. struct device *dev = kobj_to_dev(kobj);
  193. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  194. struct linedisp_map *map = linedisp->map;
  195. umode_t mode = attr->mode;
  196. if (attr == &dev_attr_map_seg7.attr) {
  197. if (!map)
  198. return 0;
  199. if (map->type != LINEDISP_MAP_SEG7)
  200. return 0;
  201. }
  202. if (attr == &dev_attr_map_seg14.attr) {
  203. if (!map)
  204. return 0;
  205. if (map->type != LINEDISP_MAP_SEG14)
  206. return 0;
  207. }
  208. return mode;
  209. };
  210. static const struct attribute_group linedisp_group = {
  211. .is_visible = linedisp_attr_is_visible,
  212. .attrs = linedisp_attrs,
  213. };
  214. __ATTRIBUTE_GROUPS(linedisp);
  215. static DEFINE_IDA(linedisp_id);
  216. static void linedisp_release(struct device *dev)
  217. {
  218. struct linedisp *linedisp = container_of(dev, struct linedisp, dev);
  219. kfree(linedisp->map);
  220. kfree(linedisp->message);
  221. kfree(linedisp->buf);
  222. ida_free(&linedisp_id, linedisp->id);
  223. }
  224. static const struct device_type linedisp_type = {
  225. .groups = linedisp_groups,
  226. .release = linedisp_release,
  227. };
  228. static int linedisp_init_map(struct linedisp *linedisp)
  229. {
  230. struct linedisp_map *map;
  231. int err;
  232. if (!linedisp->ops->get_map_type)
  233. return 0;
  234. err = linedisp->ops->get_map_type(linedisp);
  235. if (err < 0)
  236. return err;
  237. map = kmalloc(sizeof(*map), GFP_KERNEL);
  238. if (!map)
  239. return -ENOMEM;
  240. map->type = err;
  241. /* assign initial mapping */
  242. switch (map->type) {
  243. case LINEDISP_MAP_SEG7:
  244. map->map.seg7 = initial_map_seg7;
  245. map->size = sizeof(map->map.seg7);
  246. break;
  247. case LINEDISP_MAP_SEG14:
  248. map->map.seg14 = initial_map_seg14;
  249. map->size = sizeof(map->map.seg14);
  250. break;
  251. default:
  252. kfree(map);
  253. return -EINVAL;
  254. }
  255. linedisp->map = map;
  256. return 0;
  257. }
  258. #ifdef CONFIG_PANEL_BOOT_MESSAGE
  259. #define LINEDISP_INIT_TEXT CONFIG_PANEL_BOOT_MESSAGE
  260. #else
  261. #define LINEDISP_INIT_TEXT "Linux " UTS_RELEASE " "
  262. #endif
  263. /**
  264. * linedisp_register - register a character line display
  265. * @linedisp: pointer to character line display structure
  266. * @parent: parent device
  267. * @num_chars: the number of characters that can be displayed
  268. * @ops: character line display operations
  269. *
  270. * Return: zero on success, else a negative error code.
  271. */
  272. int linedisp_register(struct linedisp *linedisp, struct device *parent,
  273. unsigned int num_chars, const struct linedisp_ops *ops)
  274. {
  275. int err;
  276. memset(linedisp, 0, sizeof(*linedisp));
  277. linedisp->dev.parent = parent;
  278. linedisp->dev.type = &linedisp_type;
  279. linedisp->ops = ops;
  280. linedisp->num_chars = num_chars;
  281. linedisp->scroll_rate = DEFAULT_SCROLL_RATE;
  282. err = ida_alloc(&linedisp_id, GFP_KERNEL);
  283. if (err < 0)
  284. return err;
  285. linedisp->id = err;
  286. device_initialize(&linedisp->dev);
  287. dev_set_name(&linedisp->dev, "linedisp.%u", linedisp->id);
  288. err = -ENOMEM;
  289. linedisp->buf = kzalloc(linedisp->num_chars, GFP_KERNEL);
  290. if (!linedisp->buf)
  291. goto out_put_device;
  292. /* initialise a character mapping, if required */
  293. err = linedisp_init_map(linedisp);
  294. if (err)
  295. goto out_put_device;
  296. /* initialise a timer for scrolling the message */
  297. timer_setup(&linedisp->timer, linedisp_scroll, 0);
  298. err = device_add(&linedisp->dev);
  299. if (err)
  300. goto out_del_timer;
  301. /* display a default message */
  302. err = linedisp_display(linedisp, LINEDISP_INIT_TEXT, -1);
  303. if (err)
  304. goto out_del_dev;
  305. return 0;
  306. out_del_dev:
  307. device_del(&linedisp->dev);
  308. out_del_timer:
  309. del_timer_sync(&linedisp->timer);
  310. out_put_device:
  311. put_device(&linedisp->dev);
  312. return err;
  313. }
  314. EXPORT_SYMBOL_NS_GPL(linedisp_register, LINEDISP);
  315. /**
  316. * linedisp_unregister - unregister a character line display
  317. * @linedisp: pointer to character line display structure registered previously
  318. * with linedisp_register()
  319. */
  320. void linedisp_unregister(struct linedisp *linedisp)
  321. {
  322. device_del(&linedisp->dev);
  323. del_timer_sync(&linedisp->timer);
  324. put_device(&linedisp->dev);
  325. }
  326. EXPORT_SYMBOL_NS_GPL(linedisp_unregister, LINEDISP);
  327. MODULE_DESCRIPTION("Character line display core support");
  328. MODULE_LICENSE("GPL");