chan_kern.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/stddef.h>
  6. #include <linux/kernel.h>
  7. #include <linux/list.h>
  8. #include <linux/slab.h>
  9. #include <linux/tty.h>
  10. #include <linux/string.h>
  11. #include <linux/tty_flip.h>
  12. #include <asm/irq.h>
  13. #include "chan_kern.h"
  14. #include "user_util.h"
  15. #include "kern.h"
  16. #include "irq_user.h"
  17. #include "sigio.h"
  18. #include "line.h"
  19. #include "os.h"
  20. #ifdef CONFIG_NOCONFIG_CHAN
  21. /* The printk's here are wrong because we are complaining that there is no
  22. * output device, but printk is printing to that output device. The user will
  23. * never see the error. printf would be better, except it can't run on a
  24. * kernel stack because it will overflow it.
  25. * Use printk for now since that will avoid crashing.
  26. */
  27. static void *not_configged_init(char *str, int device, struct chan_opts *opts)
  28. {
  29. printk(KERN_ERR "Using a channel type which is configured out of "
  30. "UML\n");
  31. return(NULL);
  32. }
  33. static int not_configged_open(int input, int output, int primary, void *data,
  34. char **dev_out)
  35. {
  36. printk(KERN_ERR "Using a channel type which is configured out of "
  37. "UML\n");
  38. return(-ENODEV);
  39. }
  40. static void not_configged_close(int fd, void *data)
  41. {
  42. printk(KERN_ERR "Using a channel type which is configured out of "
  43. "UML\n");
  44. }
  45. static int not_configged_read(int fd, char *c_out, void *data)
  46. {
  47. printk(KERN_ERR "Using a channel type which is configured out of "
  48. "UML\n");
  49. return(-EIO);
  50. }
  51. static int not_configged_write(int fd, const char *buf, int len, void *data)
  52. {
  53. printk(KERN_ERR "Using a channel type which is configured out of "
  54. "UML\n");
  55. return(-EIO);
  56. }
  57. static int not_configged_console_write(int fd, const char *buf, int len,
  58. void *data)
  59. {
  60. printk(KERN_ERR "Using a channel type which is configured out of "
  61. "UML\n");
  62. return(-EIO);
  63. }
  64. static int not_configged_window_size(int fd, void *data, unsigned short *rows,
  65. unsigned short *cols)
  66. {
  67. printk(KERN_ERR "Using a channel type which is configured out of "
  68. "UML\n");
  69. return(-ENODEV);
  70. }
  71. static void not_configged_free(void *data)
  72. {
  73. printf(KERN_ERR "Using a channel type which is configured out of "
  74. "UML\n");
  75. }
  76. static struct chan_ops not_configged_ops = {
  77. .init = not_configged_init,
  78. .open = not_configged_open,
  79. .close = not_configged_close,
  80. .read = not_configged_read,
  81. .write = not_configged_write,
  82. .console_write = not_configged_console_write,
  83. .window_size = not_configged_window_size,
  84. .free = not_configged_free,
  85. .winch = 0,
  86. };
  87. #endif /* CONFIG_NOCONFIG_CHAN */
  88. void generic_close(int fd, void *unused)
  89. {
  90. os_close_file(fd);
  91. }
  92. int generic_read(int fd, char *c_out, void *unused)
  93. {
  94. int n;
  95. n = os_read_file(fd, c_out, sizeof(*c_out));
  96. if(n == -EAGAIN)
  97. return(0);
  98. else if(n == 0)
  99. return(-EIO);
  100. return(n);
  101. }
  102. /* XXX Trivial wrapper around os_write_file */
  103. int generic_write(int fd, const char *buf, int n, void *unused)
  104. {
  105. return(os_write_file(fd, buf, n));
  106. }
  107. int generic_window_size(int fd, void *unused, unsigned short *rows_out,
  108. unsigned short *cols_out)
  109. {
  110. int rows, cols;
  111. int ret;
  112. ret = os_window_size(fd, &rows, &cols);
  113. if(ret < 0)
  114. return(ret);
  115. ret = ((*rows_out != rows) || (*cols_out != cols));
  116. *rows_out = rows;
  117. *cols_out = cols;
  118. return(ret);
  119. }
  120. void generic_free(void *data)
  121. {
  122. kfree(data);
  123. }
  124. static void tty_receive_char(struct tty_struct *tty, char ch)
  125. {
  126. if(tty == NULL) return;
  127. if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
  128. if(ch == STOP_CHAR(tty)){
  129. stop_tty(tty);
  130. return;
  131. }
  132. else if(ch == START_CHAR(tty)){
  133. start_tty(tty);
  134. return;
  135. }
  136. }
  137. if((tty->flip.flag_buf_ptr == NULL) ||
  138. (tty->flip.char_buf_ptr == NULL))
  139. return;
  140. tty_insert_flip_char(tty, ch, TTY_NORMAL);
  141. }
  142. static int open_one_chan(struct chan *chan, int input, int output, int primary)
  143. {
  144. int fd;
  145. if(chan->opened) return(0);
  146. if(chan->ops->open == NULL) fd = 0;
  147. else fd = (*chan->ops->open)(input, output, primary, chan->data,
  148. &chan->dev);
  149. if(fd < 0) return(fd);
  150. chan->fd = fd;
  151. chan->opened = 1;
  152. return(0);
  153. }
  154. int open_chan(struct list_head *chans)
  155. {
  156. struct list_head *ele;
  157. struct chan *chan;
  158. int ret, err = 0;
  159. list_for_each(ele, chans){
  160. chan = list_entry(ele, struct chan, list);
  161. ret = open_one_chan(chan, chan->input, chan->output,
  162. chan->primary);
  163. if(chan->primary) err = ret;
  164. }
  165. return(err);
  166. }
  167. void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
  168. {
  169. struct list_head *ele;
  170. struct chan *chan;
  171. list_for_each(ele, chans){
  172. chan = list_entry(ele, struct chan, list);
  173. if(chan->primary && chan->output && chan->ops->winch){
  174. register_winch(chan->fd, tty);
  175. return;
  176. }
  177. }
  178. }
  179. void enable_chan(struct list_head *chans, struct tty_struct *tty)
  180. {
  181. struct list_head *ele;
  182. struct chan *chan;
  183. list_for_each(ele, chans){
  184. chan = list_entry(ele, struct chan, list);
  185. if(!chan->opened) continue;
  186. line_setup_irq(chan->fd, chan->input, chan->output, tty);
  187. }
  188. }
  189. void close_chan(struct list_head *chans)
  190. {
  191. struct chan *chan;
  192. /* Close in reverse order as open in case more than one of them
  193. * refers to the same device and they save and restore that device's
  194. * state. Then, the first one opened will have the original state,
  195. * so it must be the last closed.
  196. */
  197. list_for_each_entry_reverse(chan, chans, list) {
  198. if(!chan->opened) continue;
  199. if(chan->ops->close != NULL)
  200. (*chan->ops->close)(chan->fd, chan->data);
  201. chan->opened = 0;
  202. chan->fd = -1;
  203. }
  204. }
  205. int write_chan(struct list_head *chans, const char *buf, int len,
  206. int write_irq)
  207. {
  208. struct list_head *ele;
  209. struct chan *chan = NULL;
  210. int n, ret = 0;
  211. list_for_each(ele, chans) {
  212. chan = list_entry(ele, struct chan, list);
  213. if (!chan->output || (chan->ops->write == NULL))
  214. continue;
  215. n = chan->ops->write(chan->fd, buf, len, chan->data);
  216. if (chan->primary) {
  217. ret = n;
  218. if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
  219. reactivate_fd(chan->fd, write_irq);
  220. }
  221. }
  222. return(ret);
  223. }
  224. int console_write_chan(struct list_head *chans, const char *buf, int len)
  225. {
  226. struct list_head *ele;
  227. struct chan *chan;
  228. int n, ret = 0;
  229. list_for_each(ele, chans){
  230. chan = list_entry(ele, struct chan, list);
  231. if(!chan->output || (chan->ops->console_write == NULL))
  232. continue;
  233. n = chan->ops->console_write(chan->fd, buf, len, chan->data);
  234. if(chan->primary) ret = n;
  235. }
  236. return(ret);
  237. }
  238. int console_open_chan(struct line *line, struct console *co, struct chan_opts *opts)
  239. {
  240. if (!list_empty(&line->chan_list))
  241. return 0;
  242. if (0 != parse_chan_pair(line->init_str, &line->chan_list,
  243. line->init_pri, co->index, opts))
  244. return -1;
  245. if (0 != open_chan(&line->chan_list))
  246. return -1;
  247. printk("Console initialized on /dev/%s%d\n",co->name,co->index);
  248. return 0;
  249. }
  250. int chan_window_size(struct list_head *chans, unsigned short *rows_out,
  251. unsigned short *cols_out)
  252. {
  253. struct list_head *ele;
  254. struct chan *chan;
  255. list_for_each(ele, chans){
  256. chan = list_entry(ele, struct chan, list);
  257. if(chan->primary){
  258. if(chan->ops->window_size == NULL) return(0);
  259. return(chan->ops->window_size(chan->fd, chan->data,
  260. rows_out, cols_out));
  261. }
  262. }
  263. return(0);
  264. }
  265. void free_one_chan(struct chan *chan)
  266. {
  267. list_del(&chan->list);
  268. if(chan->ops->free != NULL)
  269. (*chan->ops->free)(chan->data);
  270. free_irq_by_fd(chan->fd);
  271. if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
  272. kfree(chan);
  273. }
  274. void free_chan(struct list_head *chans)
  275. {
  276. struct list_head *ele, *next;
  277. struct chan *chan;
  278. list_for_each_safe(ele, next, chans){
  279. chan = list_entry(ele, struct chan, list);
  280. free_one_chan(chan);
  281. }
  282. }
  283. static int one_chan_config_string(struct chan *chan, char *str, int size,
  284. char **error_out)
  285. {
  286. int n = 0;
  287. if(chan == NULL){
  288. CONFIG_CHUNK(str, size, n, "none", 1);
  289. return(n);
  290. }
  291. CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
  292. if(chan->dev == NULL){
  293. CONFIG_CHUNK(str, size, n, "", 1);
  294. return(n);
  295. }
  296. CONFIG_CHUNK(str, size, n, ":", 0);
  297. CONFIG_CHUNK(str, size, n, chan->dev, 0);
  298. return(n);
  299. }
  300. static int chan_pair_config_string(struct chan *in, struct chan *out,
  301. char *str, int size, char **error_out)
  302. {
  303. int n;
  304. n = one_chan_config_string(in, str, size, error_out);
  305. str += n;
  306. size -= n;
  307. if(in == out){
  308. CONFIG_CHUNK(str, size, n, "", 1);
  309. return(n);
  310. }
  311. CONFIG_CHUNK(str, size, n, ",", 1);
  312. n = one_chan_config_string(out, str, size, error_out);
  313. str += n;
  314. size -= n;
  315. CONFIG_CHUNK(str, size, n, "", 1);
  316. return(n);
  317. }
  318. int chan_config_string(struct list_head *chans, char *str, int size,
  319. char **error_out)
  320. {
  321. struct list_head *ele;
  322. struct chan *chan, *in = NULL, *out = NULL;
  323. list_for_each(ele, chans){
  324. chan = list_entry(ele, struct chan, list);
  325. if(!chan->primary)
  326. continue;
  327. if(chan->input)
  328. in = chan;
  329. if(chan->output)
  330. out = chan;
  331. }
  332. return(chan_pair_config_string(in, out, str, size, error_out));
  333. }
  334. struct chan_type {
  335. char *key;
  336. struct chan_ops *ops;
  337. };
  338. struct chan_type chan_table[] = {
  339. { "fd", &fd_ops },
  340. #ifdef CONFIG_NULL_CHAN
  341. { "null", &null_ops },
  342. #else
  343. { "null", &not_configged_ops },
  344. #endif
  345. #ifdef CONFIG_PORT_CHAN
  346. { "port", &port_ops },
  347. #else
  348. { "port", &not_configged_ops },
  349. #endif
  350. #ifdef CONFIG_PTY_CHAN
  351. { "pty", &pty_ops },
  352. { "pts", &pts_ops },
  353. #else
  354. { "pty", &not_configged_ops },
  355. { "pts", &not_configged_ops },
  356. #endif
  357. #ifdef CONFIG_TTY_CHAN
  358. { "tty", &tty_ops },
  359. #else
  360. { "tty", &not_configged_ops },
  361. #endif
  362. #ifdef CONFIG_XTERM_CHAN
  363. { "xterm", &xterm_ops },
  364. #else
  365. { "xterm", &not_configged_ops },
  366. #endif
  367. };
  368. static struct chan *parse_chan(char *str, int pri, int device,
  369. struct chan_opts *opts)
  370. {
  371. struct chan_type *entry;
  372. struct chan_ops *ops;
  373. struct chan *chan;
  374. void *data;
  375. int i;
  376. ops = NULL;
  377. data = NULL;
  378. for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
  379. entry = &chan_table[i];
  380. if(!strncmp(str, entry->key, strlen(entry->key))){
  381. ops = entry->ops;
  382. str += strlen(entry->key);
  383. break;
  384. }
  385. }
  386. if(ops == NULL){
  387. printk(KERN_ERR "parse_chan couldn't parse \"%s\"\n",
  388. str);
  389. return(NULL);
  390. }
  391. if(ops->init == NULL) return(NULL);
  392. data = (*ops->init)(str, device, opts);
  393. if(data == NULL) return(NULL);
  394. chan = kmalloc(sizeof(*chan), GFP_KERNEL);
  395. if(chan == NULL) return(NULL);
  396. *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
  397. .primary = 1,
  398. .input = 0,
  399. .output = 0,
  400. .opened = 0,
  401. .fd = -1,
  402. .pri = pri,
  403. .ops = ops,
  404. .data = data });
  405. return(chan);
  406. }
  407. int parse_chan_pair(char *str, struct list_head *chans, int pri, int device,
  408. struct chan_opts *opts)
  409. {
  410. struct chan *new, *chan;
  411. char *in, *out;
  412. if(!list_empty(chans)){
  413. chan = list_entry(chans->next, struct chan, list);
  414. if(chan->pri >= pri) return(0);
  415. free_chan(chans);
  416. INIT_LIST_HEAD(chans);
  417. }
  418. out = strchr(str, ',');
  419. if(out != NULL){
  420. in = str;
  421. *out = '\0';
  422. out++;
  423. new = parse_chan(in, pri, device, opts);
  424. if(new == NULL) return(-1);
  425. new->input = 1;
  426. list_add(&new->list, chans);
  427. new = parse_chan(out, pri, device, opts);
  428. if(new == NULL) return(-1);
  429. list_add(&new->list, chans);
  430. new->output = 1;
  431. }
  432. else {
  433. new = parse_chan(str, pri, device, opts);
  434. if(new == NULL) return(-1);
  435. list_add(&new->list, chans);
  436. new->input = 1;
  437. new->output = 1;
  438. }
  439. return(0);
  440. }
  441. int chan_out_fd(struct list_head *chans)
  442. {
  443. struct list_head *ele;
  444. struct chan *chan;
  445. list_for_each(ele, chans){
  446. chan = list_entry(ele, struct chan, list);
  447. if(chan->primary && chan->output)
  448. return(chan->fd);
  449. }
  450. return(-1);
  451. }
  452. void chan_interrupt(struct list_head *chans, struct work_struct *task,
  453. struct tty_struct *tty, int irq)
  454. {
  455. struct list_head *ele, *next;
  456. struct chan *chan;
  457. int err;
  458. char c;
  459. list_for_each_safe(ele, next, chans){
  460. chan = list_entry(ele, struct chan, list);
  461. if(!chan->input || (chan->ops->read == NULL)) continue;
  462. do {
  463. if((tty != NULL) &&
  464. (tty->flip.count >= TTY_FLIPBUF_SIZE)){
  465. schedule_work(task);
  466. goto out;
  467. }
  468. err = chan->ops->read(chan->fd, &c, chan->data);
  469. if(err > 0)
  470. tty_receive_char(tty, c);
  471. } while(err > 0);
  472. if(err == 0) reactivate_fd(chan->fd, irq);
  473. if(err == -EIO){
  474. if(chan->primary){
  475. if(tty != NULL)
  476. tty_hangup(tty);
  477. line_disable(tty, irq);
  478. close_chan(chans);
  479. free_chan(chans);
  480. return;
  481. }
  482. else {
  483. if(chan->ops->close != NULL)
  484. chan->ops->close(chan->fd, chan->data);
  485. free_one_chan(chan);
  486. }
  487. }
  488. }
  489. out:
  490. if(tty) tty_flip_buffer_push(tty);
  491. }
  492. /*
  493. * Overrides for Emacs so that we follow Linus's tabbing style.
  494. * Emacs will notice this stuff at the end of the file and automatically
  495. * adjust the settings for this buffer only. This must remain at the end
  496. * of the file.
  497. * ---------------------------------------------------------------------------
  498. * Local variables:
  499. * c-file-style: "linux"
  500. * End:
  501. */