serport.c 6.9 KB

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