serport.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Input device TTY line discipline
  4. *
  5. * Copyright (c) 1999-2002 Vojtech Pavlik
  6. *
  7. * This is a module that converts a tty line into a much simpler
  8. * 'serial io port' abstraction that the input device drivers use.
  9. */
  10. #include <linux/uaccess.h>
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/serio.h>
  17. #include <linux/tty.h>
  18. #include <linux/compat.h>
  19. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  20. MODULE_DESCRIPTION("Input device TTY line discipline");
  21. MODULE_LICENSE("GPL");
  22. MODULE_ALIAS_LDISC(N_MOUSE);
  23. #define SERPORT_BUSY 1
  24. #define SERPORT_ACTIVE 2
  25. #define SERPORT_DEAD 3
  26. struct serport {
  27. struct tty_struct *tty;
  28. wait_queue_head_t wait;
  29. struct serio *serio;
  30. struct serio_device_id id;
  31. spinlock_t lock;
  32. unsigned long flags;
  33. };
  34. /*
  35. * Callback functions from the serio code.
  36. */
  37. static int serport_serio_write(struct serio *serio, unsigned char data)
  38. {
  39. struct serport *serport = serio->port_data;
  40. return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
  41. }
  42. static int serport_serio_open(struct serio *serio)
  43. {
  44. struct serport *serport = serio->port_data;
  45. unsigned long flags;
  46. spin_lock_irqsave(&serport->lock, flags);
  47. set_bit(SERPORT_ACTIVE, &serport->flags);
  48. spin_unlock_irqrestore(&serport->lock, flags);
  49. return 0;
  50. }
  51. static void serport_serio_close(struct serio *serio)
  52. {
  53. struct serport *serport = serio->port_data;
  54. unsigned long flags;
  55. spin_lock_irqsave(&serport->lock, flags);
  56. clear_bit(SERPORT_ACTIVE, &serport->flags);
  57. spin_unlock_irqrestore(&serport->lock, flags);
  58. }
  59. /*
  60. * serport_ldisc_open() is the routine that is called upon setting our line
  61. * discipline on a tty. It prepares the serio struct.
  62. */
  63. static int serport_ldisc_open(struct tty_struct *tty)
  64. {
  65. struct serport *serport;
  66. if (!capable(CAP_SYS_ADMIN))
  67. return -EPERM;
  68. serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
  69. if (!serport)
  70. return -ENOMEM;
  71. serport->tty = tty;
  72. spin_lock_init(&serport->lock);
  73. init_waitqueue_head(&serport->wait);
  74. tty->disc_data = serport;
  75. tty->receive_room = 256;
  76. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  77. return 0;
  78. }
  79. /*
  80. * serport_ldisc_close() is the opposite of serport_ldisc_open()
  81. */
  82. static void serport_ldisc_close(struct tty_struct *tty)
  83. {
  84. struct serport *serport = (struct serport *) tty->disc_data;
  85. kfree(serport);
  86. }
  87. /*
  88. * serport_ldisc_receive() is called by the low level tty driver when characters
  89. * are ready for us. We forward the characters and flags, one by one to the
  90. * 'interrupt' routine.
  91. */
  92. static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
  93. {
  94. struct serport *serport = (struct serport*) tty->disc_data;
  95. unsigned long flags;
  96. unsigned int ch_flags = 0;
  97. int i;
  98. spin_lock_irqsave(&serport->lock, flags);
  99. if (!test_bit(SERPORT_ACTIVE, &serport->flags))
  100. goto out;
  101. for (i = 0; i < count; i++) {
  102. if (fp) {
  103. switch (fp[i]) {
  104. case TTY_FRAME:
  105. ch_flags = SERIO_FRAME;
  106. break;
  107. case TTY_PARITY:
  108. ch_flags = SERIO_PARITY;
  109. break;
  110. default:
  111. ch_flags = 0;
  112. break;
  113. }
  114. }
  115. serio_interrupt(serport->serio, cp[i], ch_flags);
  116. }
  117. out:
  118. spin_unlock_irqrestore(&serport->lock, flags);
  119. }
  120. /*
  121. * serport_ldisc_read() just waits indefinitely if everything goes well.
  122. * However, when the serio driver closes the serio port, it finishes,
  123. * returning 0 characters.
  124. */
  125. static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file,
  126. unsigned char *kbuf, size_t nr,
  127. void **cookie, unsigned long offset)
  128. {
  129. struct serport *serport = (struct serport*) tty->disc_data;
  130. struct serio *serio;
  131. if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
  132. return -EBUSY;
  133. serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  134. if (!serio)
  135. return -ENOMEM;
  136. strlcpy(serio->name, "Serial port", sizeof(serio->name));
  137. snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty));
  138. serio->id = serport->id;
  139. serio->id.type = SERIO_RS232;
  140. serio->write = serport_serio_write;
  141. serio->open = serport_serio_open;
  142. serio->close = serport_serio_close;
  143. serio->port_data = serport;
  144. serio->dev.parent = tty->dev;
  145. serio_register_port(serport->serio);
  146. printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty));
  147. wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
  148. serio_unregister_port(serport->serio);
  149. serport->serio = NULL;
  150. clear_bit(SERPORT_DEAD, &serport->flags);
  151. clear_bit(SERPORT_BUSY, &serport->flags);
  152. return 0;
  153. }
  154. static void serport_set_type(struct tty_struct *tty, unsigned long type)
  155. {
  156. struct serport *serport = tty->disc_data;
  157. serport->id.proto = type & 0x000000ff;
  158. serport->id.id = (type & 0x0000ff00) >> 8;
  159. serport->id.extra = (type & 0x00ff0000) >> 16;
  160. }
  161. /*
  162. * serport_ldisc_ioctl() allows to set the port protocol, and device ID
  163. */
  164. static int serport_ldisc_ioctl(struct tty_struct *tty, struct file *file,
  165. unsigned int cmd, unsigned long arg)
  166. {
  167. if (cmd == SPIOCSTYPE) {
  168. unsigned long type;
  169. if (get_user(type, (unsigned long __user *) arg))
  170. return -EFAULT;
  171. serport_set_type(tty, type);
  172. return 0;
  173. }
  174. return -EINVAL;
  175. }
  176. #ifdef CONFIG_COMPAT
  177. #define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t)
  178. static int serport_ldisc_compat_ioctl(struct tty_struct *tty,
  179. struct file *file,
  180. unsigned int cmd, unsigned long arg)
  181. {
  182. if (cmd == COMPAT_SPIOCSTYPE) {
  183. void __user *uarg = compat_ptr(arg);
  184. compat_ulong_t compat_type;
  185. if (get_user(compat_type, (compat_ulong_t __user *)uarg))
  186. return -EFAULT;
  187. serport_set_type(tty, compat_type);
  188. return 0;
  189. }
  190. return -EINVAL;
  191. }
  192. #endif
  193. static int serport_ldisc_hangup(struct tty_struct *tty)
  194. {
  195. struct serport *serport = (struct serport *) tty->disc_data;
  196. unsigned long flags;
  197. spin_lock_irqsave(&serport->lock, flags);
  198. set_bit(SERPORT_DEAD, &serport->flags);
  199. spin_unlock_irqrestore(&serport->lock, flags);
  200. wake_up_interruptible(&serport->wait);
  201. return 0;
  202. }
  203. static void serport_ldisc_write_wakeup(struct tty_struct * tty)
  204. {
  205. struct serport *serport = (struct serport *) tty->disc_data;
  206. unsigned long flags;
  207. spin_lock_irqsave(&serport->lock, flags);
  208. if (test_bit(SERPORT_ACTIVE, &serport->flags))
  209. serio_drv_write_wakeup(serport->serio);
  210. spin_unlock_irqrestore(&serport->lock, flags);
  211. }
  212. /*
  213. * The line discipline structure.
  214. */
  215. static struct tty_ldisc_ops serport_ldisc = {
  216. .owner = THIS_MODULE,
  217. .name = "input",
  218. .open = serport_ldisc_open,
  219. .close = serport_ldisc_close,
  220. .read = serport_ldisc_read,
  221. .ioctl = serport_ldisc_ioctl,
  222. #ifdef CONFIG_COMPAT
  223. .compat_ioctl = serport_ldisc_compat_ioctl,
  224. #endif
  225. .receive_buf = serport_ldisc_receive,
  226. .hangup = serport_ldisc_hangup,
  227. .write_wakeup = serport_ldisc_write_wakeup
  228. };
  229. /*
  230. * The functions for insering/removing us as a module.
  231. */
  232. static int __init serport_init(void)
  233. {
  234. int retval;
  235. retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
  236. if (retval)
  237. printk(KERN_ERR "serport.c: Error registering line discipline.\n");
  238. return retval;
  239. }
  240. static void __exit serport_exit(void)
  241. {
  242. tty_unregister_ldisc(N_MOUSE);
  243. }
  244. module_init(serport_init);
  245. module_exit(serport_exit);