tty.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_util.h"
  11. #include "user.h"
  12. #include "os.h"
  13. #include "um_malloc.h"
  14. struct tty_chan {
  15. char *dev;
  16. int raw;
  17. struct termios tt;
  18. };
  19. static void *tty_chan_init(char *str, int device, const struct chan_opts *opts)
  20. {
  21. struct tty_chan *data;
  22. if(*str != ':'){
  23. printk("tty_init : channel type 'tty' must specify "
  24. "a device\n");
  25. return NULL;
  26. }
  27. str++;
  28. data = um_kmalloc(sizeof(*data));
  29. if(data == NULL)
  30. return NULL;
  31. *data = ((struct tty_chan) { .dev = str,
  32. .raw = opts->raw });
  33. return data;
  34. }
  35. static int tty_open(int input, int output, int primary, void *d,
  36. char **dev_out)
  37. {
  38. struct tty_chan *data = d;
  39. int fd, err;
  40. fd = os_open_file(data->dev, of_set_rw(OPENFLAGS(), input, output), 0);
  41. if(fd < 0)
  42. return fd;
  43. if(data->raw){
  44. CATCH_EINTR(err = tcgetattr(fd, &data->tt));
  45. if(err)
  46. return err;
  47. err = raw(fd);
  48. if(err)
  49. return err;
  50. }
  51. *dev_out = data->dev;
  52. return fd;
  53. }
  54. const struct chan_ops tty_ops = {
  55. .type = "tty",
  56. .init = tty_chan_init,
  57. .open = tty_open,
  58. .close = generic_close,
  59. .read = generic_read,
  60. .write = generic_write,
  61. .console_write = generic_console_write,
  62. .window_size = generic_window_size,
  63. .free = generic_free,
  64. .winch = 0,
  65. };