serport.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. guard(spinlock_irqsave)(&serport->lock);
  46. set_bit(SERPORT_ACTIVE, &serport->flags);
  47. return 0;
  48. }
  49. static void serport_serio_close(struct serio *serio)
  50. {
  51. struct serport *serport = serio->port_data;
  52. guard(spinlock_irqsave)(&serport->lock);
  53. clear_bit(SERPORT_ACTIVE, &serport->flags);
  54. }
  55. /*
  56. * serport_ldisc_open() is the routine that is called upon setting our line
  57. * discipline on a tty. It prepares the serio struct.
  58. */
  59. static int serport_ldisc_open(struct tty_struct *tty)
  60. {
  61. struct serport *serport;
  62. if (!capable(CAP_SYS_ADMIN))
  63. return -EPERM;
  64. serport = kzalloc_obj(*serport);
  65. if (!serport)
  66. return -ENOMEM;
  67. serport->tty = tty;
  68. spin_lock_init(&serport->lock);
  69. init_waitqueue_head(&serport->wait);
  70. tty->disc_data = serport;
  71. tty->receive_room = 256;
  72. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  73. return 0;
  74. }
  75. /*
  76. * serport_ldisc_close() is the opposite of serport_ldisc_open()
  77. */
  78. static void serport_ldisc_close(struct tty_struct *tty)
  79. {
  80. struct serport *serport = tty->disc_data;
  81. kfree(serport);
  82. }
  83. /*
  84. * serport_ldisc_receive() is called by the low level tty driver when characters
  85. * are ready for us. We forward the characters and flags, one by one to the
  86. * 'interrupt' routine.
  87. */
  88. static void serport_ldisc_receive(struct tty_struct *tty, const u8 *cp,
  89. const u8 *fp, size_t count)
  90. {
  91. struct serport *serport = tty->disc_data;
  92. unsigned int ch_flags = 0;
  93. int i;
  94. guard(spinlock_irqsave)(&serport->lock);
  95. if (!test_bit(SERPORT_ACTIVE, &serport->flags))
  96. return;
  97. for (i = 0; i < count; i++) {
  98. if (fp) {
  99. switch (fp[i]) {
  100. case TTY_FRAME:
  101. ch_flags = SERIO_FRAME;
  102. break;
  103. case TTY_PARITY:
  104. ch_flags = SERIO_PARITY;
  105. break;
  106. default:
  107. ch_flags = 0;
  108. break;
  109. }
  110. }
  111. serio_interrupt(serport->serio, cp[i], ch_flags);
  112. }
  113. }
  114. /*
  115. * serport_ldisc_read() just waits indefinitely if everything goes well.
  116. * However, when the serio driver closes the serio port, it finishes,
  117. * returning 0 characters.
  118. */
  119. static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file,
  120. u8 *kbuf, size_t nr, void **cookie,
  121. unsigned long offset)
  122. {
  123. struct serport *serport = tty->disc_data;
  124. struct serio *serio;
  125. if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
  126. return -EBUSY;
  127. serport->serio = serio = kzalloc_obj(*serio);
  128. if (!serio)
  129. return -ENOMEM;
  130. strscpy(serio->name, "Serial port", sizeof(serio->name));
  131. snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty));
  132. serio->id = serport->id;
  133. serio->id.type = SERIO_RS232;
  134. serio->write = serport_serio_write;
  135. serio->open = serport_serio_open;
  136. serio->close = serport_serio_close;
  137. serio->port_data = serport;
  138. serio->dev.parent = tty->dev;
  139. serio_register_port(serport->serio);
  140. printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty));
  141. wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
  142. serio_unregister_port(serport->serio);
  143. serport->serio = NULL;
  144. clear_bit(SERPORT_DEAD, &serport->flags);
  145. clear_bit(SERPORT_BUSY, &serport->flags);
  146. return 0;
  147. }
  148. static void serport_set_type(struct tty_struct *tty, unsigned long type)
  149. {
  150. struct serport *serport = tty->disc_data;
  151. serport->id.proto = type & 0x000000ff;
  152. serport->id.id = (type & 0x0000ff00) >> 8;
  153. serport->id.extra = (type & 0x00ff0000) >> 16;
  154. }
  155. /*
  156. * serport_ldisc_ioctl() allows to set the port protocol, and device ID
  157. */
  158. static int serport_ldisc_ioctl(struct tty_struct *tty, unsigned int cmd,
  159. unsigned long arg)
  160. {
  161. if (cmd == SPIOCSTYPE) {
  162. unsigned long type;
  163. if (get_user(type, (unsigned long __user *) arg))
  164. return -EFAULT;
  165. serport_set_type(tty, type);
  166. return 0;
  167. }
  168. return -EINVAL;
  169. }
  170. #ifdef CONFIG_COMPAT
  171. #define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t)
  172. static int serport_ldisc_compat_ioctl(struct tty_struct *tty,
  173. unsigned int cmd, unsigned long arg)
  174. {
  175. if (cmd == COMPAT_SPIOCSTYPE) {
  176. void __user *uarg = compat_ptr(arg);
  177. compat_ulong_t compat_type;
  178. if (get_user(compat_type, (compat_ulong_t __user *)uarg))
  179. return -EFAULT;
  180. serport_set_type(tty, compat_type);
  181. return 0;
  182. }
  183. return -EINVAL;
  184. }
  185. #endif
  186. static void serport_ldisc_hangup(struct tty_struct *tty)
  187. {
  188. struct serport *serport = tty->disc_data;
  189. scoped_guard(spinlock_irqsave, &serport->lock)
  190. set_bit(SERPORT_DEAD, &serport->flags);
  191. wake_up_interruptible(&serport->wait);
  192. }
  193. static void serport_ldisc_write_wakeup(struct tty_struct * tty)
  194. {
  195. struct serport *serport = tty->disc_data;
  196. guard(spinlock_irqsave)(&serport->lock);
  197. if (test_bit(SERPORT_ACTIVE, &serport->flags))
  198. serio_drv_write_wakeup(serport->serio);
  199. }
  200. /*
  201. * The line discipline structure.
  202. */
  203. static struct tty_ldisc_ops serport_ldisc = {
  204. .owner = THIS_MODULE,
  205. .num = N_MOUSE,
  206. .name = "input",
  207. .open = serport_ldisc_open,
  208. .close = serport_ldisc_close,
  209. .read = serport_ldisc_read,
  210. .ioctl = serport_ldisc_ioctl,
  211. #ifdef CONFIG_COMPAT
  212. .compat_ioctl = serport_ldisc_compat_ioctl,
  213. #endif
  214. .receive_buf = serport_ldisc_receive,
  215. .hangup = serport_ldisc_hangup,
  216. .write_wakeup = serport_ldisc_write_wakeup
  217. };
  218. /*
  219. * The functions for insering/removing us as a module.
  220. */
  221. static int __init serport_init(void)
  222. {
  223. int retval;
  224. retval = tty_register_ldisc(&serport_ldisc);
  225. if (retval)
  226. printk(KERN_ERR "serport.c: Error registering line discipline.\n");
  227. return retval;
  228. }
  229. static void __exit serport_exit(void)
  230. {
  231. tty_unregister_ldisc(&serport_ldisc);
  232. }
  233. module_init(serport_init);
  234. module_exit(serport_exit);