serport.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/serio.h>
  20. #include <linux/tty.h>
  21. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  22. MODULE_DESCRIPTION("Input device TTY line discipline");
  23. MODULE_LICENSE("GPL");
  24. MODULE_ALIAS_LDISC(N_MOUSE);
  25. #define SERPORT_BUSY 1
  26. struct serport {
  27. struct tty_struct *tty;
  28. wait_queue_head_t wait;
  29. struct serio *serio;
  30. unsigned long flags;
  31. };
  32. /*
  33. * Callback functions from the serio code.
  34. */
  35. static int serport_serio_write(struct serio *serio, unsigned char data)
  36. {
  37. struct serport *serport = serio->port_data;
  38. return -(serport->tty->driver->write(serport->tty, &data, 1) != 1);
  39. }
  40. static void serport_serio_close(struct serio *serio)
  41. {
  42. struct serport *serport = serio->port_data;
  43. serport->serio->id.type = 0;
  44. wake_up_interruptible(&serport->wait);
  45. }
  46. /*
  47. * serport_ldisc_open() is the routine that is called upon setting our line
  48. * discipline on a tty. It prepares the serio struct.
  49. */
  50. static int serport_ldisc_open(struct tty_struct *tty)
  51. {
  52. struct serport *serport;
  53. struct serio *serio;
  54. char name[64];
  55. if (!capable(CAP_SYS_ADMIN))
  56. return -EPERM;
  57. serport = kmalloc(sizeof(struct serport), GFP_KERNEL);
  58. serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
  59. if (unlikely(!serport || !serio)) {
  60. kfree(serport);
  61. kfree(serio);
  62. return -ENOMEM;
  63. }
  64. memset(serport, 0, sizeof(struct serport));
  65. serport->serio = serio;
  66. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  67. serport->tty = tty;
  68. tty->disc_data = serport;
  69. memset(serio, 0, sizeof(struct serio));
  70. strlcpy(serio->name, "Serial port", sizeof(serio->name));
  71. snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name));
  72. serio->id.type = SERIO_RS232;
  73. serio->write = serport_serio_write;
  74. serio->close = serport_serio_close;
  75. serio->port_data = serport;
  76. init_waitqueue_head(&serport->wait);
  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, one by one to the 'interrupt'
  90. * routine.
  91. *
  92. * FIXME: We should get pt_regs from the tty layer and forward them to
  93. * serio_interrupt here.
  94. */
  95. static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
  96. {
  97. struct serport *serport = (struct serport*) tty->disc_data;
  98. int i;
  99. for (i = 0; i < count; i++)
  100. serio_interrupt(serport->serio, cp[i], 0, NULL);
  101. }
  102. /*
  103. * serport_ldisc_room() reports how much room we do have for receiving data.
  104. * Although we in fact have infinite room, we need to specify some value
  105. * here, and 256 seems to be reasonable.
  106. */
  107. static int serport_ldisc_room(struct tty_struct *tty)
  108. {
  109. return 256;
  110. }
  111. /*
  112. * serport_ldisc_read() just waits indefinitely if everything goes well.
  113. * However, when the serio driver closes the serio port, it finishes,
  114. * returning 0 characters.
  115. */
  116. static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
  117. {
  118. struct serport *serport = (struct serport*) tty->disc_data;
  119. char name[64];
  120. if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
  121. return -EBUSY;
  122. serio_register_port(serport->serio);
  123. printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
  124. wait_event_interruptible(serport->wait, !serport->serio->id.type);
  125. serio_unregister_port(serport->serio);
  126. clear_bit(SERPORT_BUSY, &serport->flags);
  127. return 0;
  128. }
  129. /*
  130. * serport_ldisc_ioctl() allows to set the port protocol, and device ID
  131. */
  132. static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
  133. {
  134. struct serport *serport = (struct serport*) tty->disc_data;
  135. struct serio *serio = serport->serio;
  136. unsigned long type;
  137. if (cmd == SPIOCSTYPE) {
  138. if (get_user(type, (unsigned long __user *) arg))
  139. return -EFAULT;
  140. serio->id.proto = type & 0x000000ff;
  141. serio->id.id = (type & 0x0000ff00) >> 8;
  142. serio->id.extra = (type & 0x00ff0000) >> 16;
  143. return 0;
  144. }
  145. return -EINVAL;
  146. }
  147. static void serport_ldisc_write_wakeup(struct tty_struct * tty)
  148. {
  149. struct serport *sp = (struct serport *) tty->disc_data;
  150. serio_drv_write_wakeup(sp->serio);
  151. }
  152. /*
  153. * The line discipline structure.
  154. */
  155. static struct tty_ldisc serport_ldisc = {
  156. .owner = THIS_MODULE,
  157. .name = "input",
  158. .open = serport_ldisc_open,
  159. .close = serport_ldisc_close,
  160. .read = serport_ldisc_read,
  161. .ioctl = serport_ldisc_ioctl,
  162. .receive_buf = serport_ldisc_receive,
  163. .receive_room = serport_ldisc_room,
  164. .write_wakeup = serport_ldisc_write_wakeup
  165. };
  166. /*
  167. * The functions for insering/removing us as a module.
  168. */
  169. static int __init serport_init(void)
  170. {
  171. int retval;
  172. retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
  173. if (retval)
  174. printk(KERN_ERR "serport.c: Error registering line discipline.\n");
  175. return retval;
  176. }
  177. static void __exit serport_exit(void)
  178. {
  179. tty_register_ldisc(N_MOUSE, NULL);
  180. }
  181. module_init(serport_init);
  182. module_exit(serport_exit);