picvue_proc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Picvue PVC160206 display driver
  3. *
  4. * Brian Murphy <brian.murphy@eicon.com>
  5. *
  6. */
  7. #include <linux/bug.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/errno.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/timer.h>
  16. #include <linux/mutex.h>
  17. #include <linux/uaccess.h>
  18. #include "picvue.h"
  19. static DEFINE_MUTEX(pvc_mutex);
  20. static char pvc_lines[PVC_NLINES][PVC_LINELEN+1];
  21. static int pvc_linedata[PVC_NLINES];
  22. static char *pvc_linename[PVC_NLINES] = {"line1", "line2"};
  23. #define DISPLAY_DIR_NAME "display"
  24. static int scroll_dir, scroll_interval;
  25. static struct timer_list timer;
  26. static void pvc_display(unsigned long data)
  27. {
  28. int i;
  29. pvc_clear();
  30. for (i = 0; i < PVC_NLINES; i++)
  31. pvc_write_string(pvc_lines[i], 0, i);
  32. }
  33. static DECLARE_TASKLET(pvc_display_tasklet, &pvc_display, 0);
  34. static int pvc_line_proc_show(struct seq_file *m, void *v)
  35. {
  36. int lineno = *(int *)m->private;
  37. if (lineno < 0 || lineno >= PVC_NLINES) {
  38. printk(KERN_WARNING "proc_read_line: invalid lineno %d\n", lineno);
  39. return 0;
  40. }
  41. mutex_lock(&pvc_mutex);
  42. seq_printf(m, "%s\n", pvc_lines[lineno]);
  43. mutex_unlock(&pvc_mutex);
  44. return 0;
  45. }
  46. static int pvc_line_proc_open(struct inode *inode, struct file *file)
  47. {
  48. return single_open(file, pvc_line_proc_show, PDE_DATA(inode));
  49. }
  50. static ssize_t pvc_line_proc_write(struct file *file, const char __user *buf,
  51. size_t count, loff_t *pos)
  52. {
  53. int lineno = *(int *)PDE_DATA(file_inode(file));
  54. char kbuf[PVC_LINELEN];
  55. size_t len;
  56. BUG_ON(lineno < 0 || lineno >= PVC_NLINES);
  57. len = min(count, sizeof(kbuf) - 1);
  58. if (copy_from_user(kbuf, buf, len))
  59. return -EFAULT;
  60. kbuf[len] = '\0';
  61. if (len > 0 && kbuf[len - 1] == '\n')
  62. len--;
  63. mutex_lock(&pvc_mutex);
  64. strncpy(pvc_lines[lineno], kbuf, len);
  65. pvc_lines[lineno][len] = '\0';
  66. mutex_unlock(&pvc_mutex);
  67. tasklet_schedule(&pvc_display_tasklet);
  68. return count;
  69. }
  70. static const struct file_operations pvc_line_proc_fops = {
  71. .owner = THIS_MODULE,
  72. .open = pvc_line_proc_open,
  73. .read = seq_read,
  74. .llseek = seq_lseek,
  75. .release = single_release,
  76. .write = pvc_line_proc_write,
  77. };
  78. static ssize_t pvc_scroll_proc_write(struct file *file, const char __user *buf,
  79. size_t count, loff_t *pos)
  80. {
  81. char kbuf[42];
  82. size_t len;
  83. int cmd;
  84. len = min(count, sizeof(kbuf) - 1);
  85. if (copy_from_user(kbuf, buf, len))
  86. return -EFAULT;
  87. kbuf[len] = '\0';
  88. cmd = simple_strtol(kbuf, NULL, 10);
  89. mutex_lock(&pvc_mutex);
  90. if (scroll_interval != 0)
  91. del_timer(&timer);
  92. if (cmd == 0) {
  93. scroll_dir = 0;
  94. scroll_interval = 0;
  95. } else {
  96. if (cmd < 0) {
  97. scroll_dir = -1;
  98. scroll_interval = -cmd;
  99. } else {
  100. scroll_dir = 1;
  101. scroll_interval = cmd;
  102. }
  103. add_timer(&timer);
  104. }
  105. mutex_unlock(&pvc_mutex);
  106. return count;
  107. }
  108. static int pvc_scroll_proc_show(struct seq_file *m, void *v)
  109. {
  110. mutex_lock(&pvc_mutex);
  111. seq_printf(m, "%d\n", scroll_dir * scroll_interval);
  112. mutex_unlock(&pvc_mutex);
  113. return 0;
  114. }
  115. static int pvc_scroll_proc_open(struct inode *inode, struct file *file)
  116. {
  117. return single_open(file, pvc_scroll_proc_show, NULL);
  118. }
  119. static const struct file_operations pvc_scroll_proc_fops = {
  120. .owner = THIS_MODULE,
  121. .open = pvc_scroll_proc_open,
  122. .read = seq_read,
  123. .llseek = seq_lseek,
  124. .release = single_release,
  125. .write = pvc_scroll_proc_write,
  126. };
  127. void pvc_proc_timerfunc(unsigned long data)
  128. {
  129. if (scroll_dir < 0)
  130. pvc_move(DISPLAY|RIGHT);
  131. else if (scroll_dir > 0)
  132. pvc_move(DISPLAY|LEFT);
  133. timer.expires = jiffies + scroll_interval;
  134. add_timer(&timer);
  135. }
  136. static void pvc_proc_cleanup(void)
  137. {
  138. remove_proc_subtree(DISPLAY_DIR_NAME, NULL);
  139. del_timer_sync(&timer);
  140. }
  141. static int __init pvc_proc_init(void)
  142. {
  143. struct proc_dir_entry *dir, *proc_entry;
  144. int i;
  145. dir = proc_mkdir(DISPLAY_DIR_NAME, NULL);
  146. if (dir == NULL)
  147. goto error;
  148. for (i = 0; i < PVC_NLINES; i++) {
  149. strcpy(pvc_lines[i], "");
  150. pvc_linedata[i] = i;
  151. }
  152. for (i = 0; i < PVC_NLINES; i++) {
  153. proc_entry = proc_create_data(pvc_linename[i], 0644, dir,
  154. &pvc_line_proc_fops, &pvc_linedata[i]);
  155. if (proc_entry == NULL)
  156. goto error;
  157. }
  158. proc_entry = proc_create("scroll", 0644, dir,
  159. &pvc_scroll_proc_fops);
  160. if (proc_entry == NULL)
  161. goto error;
  162. init_timer(&timer);
  163. timer.function = pvc_proc_timerfunc;
  164. return 0;
  165. error:
  166. pvc_proc_cleanup();
  167. return -ENOMEM;
  168. }
  169. module_init(pvc_proc_init);
  170. module_exit(pvc_proc_cleanup);
  171. MODULE_LICENSE("GPL");