tty.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. struct tty_chan {
  14. char *dev;
  15. int raw;
  16. struct termios tt;
  17. };
  18. static void *tty_chan_init(char *str, int device, 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 = um_kmalloc(sizeof(*data));
  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) return(fd);
  41. if(data->raw){
  42. CATCH_EINTR(err = tcgetattr(fd, &data->tt));
  43. if(err)
  44. return(err);
  45. err = raw(fd);
  46. if(err)
  47. return(err);
  48. }
  49. *dev_out = data->dev;
  50. return(fd);
  51. }
  52. static int tty_console_write(int fd, const char *buf, int n, void *d)
  53. {
  54. struct tty_chan *data = d;
  55. return(generic_console_write(fd, buf, n, &data->tt));
  56. }
  57. struct chan_ops tty_ops = {
  58. .type = "tty",
  59. .init = tty_chan_init,
  60. .open = tty_open,
  61. .close = generic_close,
  62. .read = generic_read,
  63. .write = generic_write,
  64. .console_write = tty_console_write,
  65. .window_size = generic_window_size,
  66. .free = generic_free,
  67. .winch = 0,
  68. };
  69. /*
  70. * Overrides for Emacs so that we follow Linus's tabbing style.
  71. * Emacs will notice this stuff at the end of the file and automatically
  72. * adjust the settings for this buffer only. This must remain at the end
  73. * of the file.
  74. * ---------------------------------------------------------------------------
  75. * Local variables:
  76. * c-file-style: "linux"
  77. * End:
  78. */