serport.c 6.2 KB

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