serport.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #define SERPORT_ACTIVE 2
  27. #define SERPORT_DEAD 3
  28. struct serport {
  29. struct tty_struct *tty;
  30. wait_queue_head_t wait;
  31. struct serio *serio;
  32. struct serio_device_id id;
  33. spinlock_t lock;
  34. unsigned long flags;
  35. };
  36. /*
  37. * Callback functions from the serio code.
  38. */
  39. static int serport_serio_write(struct serio *serio, unsigned char data)
  40. {
  41. struct serport *serport = serio->port_data;
  42. return -(serport->tty->driver->write(serport->tty, &data, 1) != 1);
  43. }
  44. static int serport_serio_open(struct serio *serio)
  45. {
  46. struct serport *serport = serio->port_data;
  47. unsigned long flags;
  48. spin_lock_irqsave(&serport->lock, flags);
  49. set_bit(SERPORT_ACTIVE, &serport->flags);
  50. spin_unlock_irqrestore(&serport->lock, flags);
  51. return 0;
  52. }
  53. static void serport_serio_close(struct serio *serio)
  54. {
  55. struct serport *serport = serio->port_data;
  56. unsigned long flags;
  57. spin_lock_irqsave(&serport->lock, flags);
  58. clear_bit(SERPORT_ACTIVE, &serport->flags);
  59. set_bit(SERPORT_DEAD, &serport->flags);
  60. spin_unlock_irqrestore(&serport->lock, flags);
  61. wake_up_interruptible(&serport->wait);
  62. }
  63. /*
  64. * serport_ldisc_open() is the routine that is called upon setting our line
  65. * discipline on a tty. It prepares the serio struct.
  66. */
  67. static int serport_ldisc_open(struct tty_struct *tty)
  68. {
  69. struct serport *serport;
  70. if (!capable(CAP_SYS_ADMIN))
  71. return -EPERM;
  72. serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
  73. if (!serport)
  74. return -ENOMEM;
  75. serport->tty = tty;
  76. spin_lock_init(&serport->lock);
  77. init_waitqueue_head(&serport->wait);
  78. tty->disc_data = serport;
  79. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  80. return 0;
  81. }
  82. /*
  83. * serport_ldisc_close() is the opposite of serport_ldisc_open()
  84. */
  85. static void serport_ldisc_close(struct tty_struct *tty)
  86. {
  87. struct serport *serport = (struct serport *) tty->disc_data;
  88. kfree(serport);
  89. }
  90. /*
  91. * serport_ldisc_receive() is called by the low level tty driver when characters
  92. * are ready for us. We forward the characters, one by one to the 'interrupt'
  93. * routine.
  94. *
  95. * FIXME: We should get pt_regs from the tty layer and forward them to
  96. * serio_interrupt here.
  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. 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. serio_interrupt(serport->serio, cp[i], 0, NULL);
  108. out:
  109. spin_unlock_irqrestore(&serport->lock, flags);
  110. }
  111. /*
  112. * serport_ldisc_room() reports how much room we do have for receiving data.
  113. * Although we in fact have infinite room, we need to specify some value
  114. * here, and 256 seems to be reasonable.
  115. */
  116. static int serport_ldisc_room(struct tty_struct *tty)
  117. {
  118. return 256;
  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, unsigned char __user * buf, size_t nr)
  126. {
  127. struct serport *serport = (struct serport*) tty->disc_data;
  128. struct serio *serio;
  129. char name[64];
  130. if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
  131. return -EBUSY;
  132. serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  133. if (!serio)
  134. return -ENOMEM;
  135. strlcpy(serio->name, "Serial port", sizeof(serio->name));
  136. snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name));
  137. serio->id = serport->id;
  138. serio->id.type = SERIO_RS232;
  139. serio->write = serport_serio_write;
  140. serio->open = serport_serio_open;
  141. serio->close = serport_serio_close;
  142. serio->port_data = serport;
  143. serio_register_port(serport->serio);
  144. printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
  145. wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
  146. serio_unregister_port(serport->serio);
  147. serport->serio = NULL;
  148. clear_bit(SERPORT_DEAD, &serport->flags);
  149. clear_bit(SERPORT_BUSY, &serport->flags);
  150. return 0;
  151. }
  152. /*
  153. * serport_ldisc_ioctl() allows to set the port protocol, and device ID
  154. */
  155. static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
  156. {
  157. struct serport *serport = (struct serport*) tty->disc_data;
  158. unsigned long type;
  159. if (cmd == SPIOCSTYPE) {
  160. if (get_user(type, (unsigned long __user *) arg))
  161. return -EFAULT;
  162. serport->id.proto = type & 0x000000ff;
  163. serport->id.id = (type & 0x0000ff00) >> 8;
  164. serport->id.extra = (type & 0x00ff0000) >> 16;
  165. return 0;
  166. }
  167. return -EINVAL;
  168. }
  169. static void serport_ldisc_write_wakeup(struct tty_struct * tty)
  170. {
  171. struct serport *serport = (struct serport *) tty->disc_data;
  172. unsigned long flags;
  173. spin_lock_irqsave(&serport->lock, flags);
  174. if (test_bit(SERPORT_ACTIVE, &serport->flags))
  175. serio_drv_write_wakeup(serport->serio);
  176. spin_unlock_irqrestore(&serport->lock, flags);
  177. }
  178. /*
  179. * The line discipline structure.
  180. */
  181. static struct tty_ldisc serport_ldisc = {
  182. .owner = THIS_MODULE,
  183. .name = "input",
  184. .open = serport_ldisc_open,
  185. .close = serport_ldisc_close,
  186. .read = serport_ldisc_read,
  187. .ioctl = serport_ldisc_ioctl,
  188. .receive_buf = serport_ldisc_receive,
  189. .receive_room = serport_ldisc_room,
  190. .write_wakeup = serport_ldisc_write_wakeup
  191. };
  192. /*
  193. * The functions for insering/removing us as a module.
  194. */
  195. static int __init serport_init(void)
  196. {
  197. int retval;
  198. retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
  199. if (retval)
  200. printk(KERN_ERR "serport.c: Error registering line discipline.\n");
  201. return retval;
  202. }
  203. static void __exit serport_exit(void)
  204. {
  205. tty_unregister_ldisc(N_MOUSE);
  206. }
  207. module_init(serport_init);
  208. module_exit(serport_exit);