chan_kern.c 13 KB

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