tty.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2001 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <termios.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include "chan_user.h"
  10. #include "user.h"
  11. #include "os.h"
  12. #include "um_malloc.h"
  13. struct tty_chan {
  14. char *dev;
  15. int raw;
  16. struct termios tt;
  17. };
  18. static void *tty_chan_init(char *str, int device, const struct chan_opts *opts)
  19. {
  20. struct tty_chan *data;
  21. if(*str != ':'){
  22. printk("tty_init : channel type 'tty' must specify "
  23. "a device\n");
  24. return NULL;
  25. }
  26. str++;
  27. data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
  28. if(data == NULL)
  29. return NULL;
  30. *data = ((struct tty_chan) { .dev = str,
  31. .raw = opts->raw });
  32. return data;
  33. }
  34. static int tty_open(int input, int output, int primary, void *d,
  35. char **dev_out)
  36. {
  37. struct tty_chan *data = d;
  38. int fd, err;
  39. fd = os_open_file(data->dev, of_set_rw(OPENFLAGS(), input, output), 0);
  40. if(fd < 0)
  41. return fd;
  42. if(data->raw){
  43. CATCH_EINTR(err = tcgetattr(fd, &data->tt));
  44. if(err)
  45. return err;
  46. err = raw(fd);
  47. if(err)
  48. return err;
  49. }
  50. *dev_out = data->dev;
  51. return fd;
  52. }
  53. const struct chan_ops tty_ops = {
  54. .type = "tty",
  55. .init = tty_chan_init,
  56. .open = tty_open,
  57. .close = generic_close,
  58. .read = generic_read,
  59. .write = generic_write,
  60. .console_write = generic_console_write,
  61. .window_size = generic_window_size,
  62. .free = generic_free,
  63. .winch = 0,
  64. };