chan_kern.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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, int input, int output, int primary)
  164. {
  165. int fd;
  166. if(chan->opened) return(0);
  167. if(chan->ops->open == NULL) fd = 0;
  168. else fd = (*chan->ops->open)(input, output, primary, chan->data,
  169. &chan->dev);
  170. if(fd < 0) return(fd);
  171. chan->fd = fd;
  172. chan->opened = 1;
  173. return(0);
  174. }
  175. int open_chan(struct list_head *chans)
  176. {
  177. struct list_head *ele;
  178. struct chan *chan;
  179. int ret, err = 0;
  180. list_for_each(ele, chans){
  181. chan = list_entry(ele, struct chan, list);
  182. ret = open_one_chan(chan, chan->input, chan->output,
  183. chan->primary);
  184. if(chan->primary) err = ret;
  185. }
  186. return(err);
  187. }
  188. void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
  189. {
  190. struct list_head *ele;
  191. struct chan *chan;
  192. list_for_each(ele, chans){
  193. chan = list_entry(ele, struct chan, list);
  194. if(chan->primary && chan->output && chan->ops->winch){
  195. register_winch(chan->fd, tty);
  196. return;
  197. }
  198. }
  199. }
  200. void enable_chan(struct list_head *chans, struct tty_struct *tty)
  201. {
  202. struct list_head *ele;
  203. struct chan *chan;
  204. list_for_each(ele, chans){
  205. chan = list_entry(ele, struct chan, list);
  206. if(!chan->opened) continue;
  207. line_setup_irq(chan->fd, chan->input, chan->output, tty);
  208. }
  209. }
  210. void close_chan(struct list_head *chans)
  211. {
  212. struct chan *chan;
  213. /* Close in reverse order as open in case more than one of them
  214. * refers to the same device and they save and restore that device's
  215. * state. Then, the first one opened will have the original state,
  216. * so it must be the last closed.
  217. */
  218. list_for_each_entry_reverse(chan, chans, list) {
  219. if(!chan->opened) continue;
  220. if(chan->ops->close != NULL)
  221. (*chan->ops->close)(chan->fd, chan->data);
  222. chan->opened = 0;
  223. chan->fd = -1;
  224. }
  225. }
  226. int write_chan(struct list_head *chans, const char *buf, int len,
  227. int write_irq)
  228. {
  229. struct list_head *ele;
  230. struct chan *chan = NULL;
  231. int n, ret = 0;
  232. list_for_each(ele, chans) {
  233. chan = list_entry(ele, struct chan, list);
  234. if (!chan->output || (chan->ops->write == NULL))
  235. continue;
  236. n = chan->ops->write(chan->fd, buf, len, chan->data);
  237. if (chan->primary) {
  238. ret = n;
  239. if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
  240. reactivate_fd(chan->fd, write_irq);
  241. }
  242. }
  243. return(ret);
  244. }
  245. int console_write_chan(struct list_head *chans, const char *buf, int len)
  246. {
  247. struct list_head *ele;
  248. struct chan *chan;
  249. int n, ret = 0;
  250. list_for_each(ele, chans){
  251. chan = list_entry(ele, struct chan, list);
  252. if(!chan->output || (chan->ops->console_write == NULL))
  253. continue;
  254. n = chan->ops->console_write(chan->fd, buf, len);
  255. if(chan->primary) ret = n;
  256. }
  257. return(ret);
  258. }
  259. int console_open_chan(struct line *line, struct console *co, struct chan_opts *opts)
  260. {
  261. if (!list_empty(&line->chan_list))
  262. return 0;
  263. if (0 != parse_chan_pair(line->init_str, &line->chan_list,
  264. line->init_pri, co->index, opts))
  265. return -1;
  266. if (0 != open_chan(&line->chan_list))
  267. return -1;
  268. printk("Console initialized on /dev/%s%d\n",co->name,co->index);
  269. return 0;
  270. }
  271. int chan_window_size(struct list_head *chans, unsigned short *rows_out,
  272. unsigned short *cols_out)
  273. {
  274. struct list_head *ele;
  275. struct chan *chan;
  276. list_for_each(ele, chans){
  277. chan = list_entry(ele, struct chan, list);
  278. if(chan->primary){
  279. if(chan->ops->window_size == NULL) return(0);
  280. return(chan->ops->window_size(chan->fd, chan->data,
  281. rows_out, cols_out));
  282. }
  283. }
  284. return(0);
  285. }
  286. void free_one_chan(struct chan *chan)
  287. {
  288. list_del(&chan->list);
  289. if(chan->ops->free != NULL)
  290. (*chan->ops->free)(chan->data);
  291. free_irq_by_fd(chan->fd);
  292. if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
  293. kfree(chan);
  294. }
  295. void free_chan(struct list_head *chans)
  296. {
  297. struct list_head *ele, *next;
  298. struct chan *chan;
  299. list_for_each_safe(ele, next, chans){
  300. chan = list_entry(ele, struct chan, list);
  301. free_one_chan(chan);
  302. }
  303. }
  304. static int one_chan_config_string(struct chan *chan, char *str, int size,
  305. char **error_out)
  306. {
  307. int n = 0;
  308. if(chan == NULL){
  309. CONFIG_CHUNK(str, size, n, "none", 1);
  310. return(n);
  311. }
  312. CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
  313. if(chan->dev == NULL){
  314. CONFIG_CHUNK(str, size, n, "", 1);
  315. return(n);
  316. }
  317. CONFIG_CHUNK(str, size, n, ":", 0);
  318. CONFIG_CHUNK(str, size, n, chan->dev, 0);
  319. return(n);
  320. }
  321. static int chan_pair_config_string(struct chan *in, struct chan *out,
  322. char *str, int size, char **error_out)
  323. {
  324. int n;
  325. n = one_chan_config_string(in, str, size, error_out);
  326. str += n;
  327. size -= n;
  328. if(in == out){
  329. CONFIG_CHUNK(str, size, n, "", 1);
  330. return(n);
  331. }
  332. CONFIG_CHUNK(str, size, n, ",", 1);
  333. n = one_chan_config_string(out, str, size, error_out);
  334. str += n;
  335. size -= n;
  336. CONFIG_CHUNK(str, size, n, "", 1);
  337. return(n);
  338. }
  339. int chan_config_string(struct list_head *chans, char *str, int size,
  340. char **error_out)
  341. {
  342. struct list_head *ele;
  343. struct chan *chan, *in = NULL, *out = NULL;
  344. list_for_each(ele, chans){
  345. chan = list_entry(ele, struct chan, list);
  346. if(!chan->primary)
  347. continue;
  348. if(chan->input)
  349. in = chan;
  350. if(chan->output)
  351. out = chan;
  352. }
  353. return(chan_pair_config_string(in, out, str, size, error_out));
  354. }
  355. struct chan_type {
  356. char *key;
  357. struct chan_ops *ops;
  358. };
  359. struct chan_type chan_table[] = {
  360. { "fd", &fd_ops },
  361. #ifdef CONFIG_NULL_CHAN
  362. { "null", &null_ops },
  363. #else
  364. { "null", &not_configged_ops },
  365. #endif
  366. #ifdef CONFIG_PORT_CHAN
  367. { "port", &port_ops },
  368. #else
  369. { "port", &not_configged_ops },
  370. #endif
  371. #ifdef CONFIG_PTY_CHAN
  372. { "pty", &pty_ops },
  373. { "pts", &pts_ops },
  374. #else
  375. { "pty", &not_configged_ops },
  376. { "pts", &not_configged_ops },
  377. #endif
  378. #ifdef CONFIG_TTY_CHAN
  379. { "tty", &tty_ops },
  380. #else
  381. { "tty", &not_configged_ops },
  382. #endif
  383. #ifdef CONFIG_XTERM_CHAN
  384. { "xterm", &xterm_ops },
  385. #else
  386. { "xterm", &not_configged_ops },
  387. #endif
  388. };
  389. static struct chan *parse_chan(char *str, int pri, int device,
  390. struct chan_opts *opts)
  391. {
  392. struct chan_type *entry;
  393. struct chan_ops *ops;
  394. struct chan *chan;
  395. void *data;
  396. int i;
  397. ops = NULL;
  398. data = NULL;
  399. for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
  400. entry = &chan_table[i];
  401. if(!strncmp(str, entry->key, strlen(entry->key))){
  402. ops = entry->ops;
  403. str += strlen(entry->key);
  404. break;
  405. }
  406. }
  407. if(ops == NULL){
  408. my_printf("parse_chan couldn't parse \"%s\"\n",
  409. str);
  410. return(NULL);
  411. }
  412. if(ops->init == NULL) return(NULL);
  413. data = (*ops->init)(str, device, opts);
  414. if(data == NULL) return(NULL);
  415. chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
  416. if(chan == NULL) return(NULL);
  417. *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
  418. .primary = 1,
  419. .input = 0,
  420. .output = 0,
  421. .opened = 0,
  422. .fd = -1,
  423. .pri = pri,
  424. .ops = ops,
  425. .data = data });
  426. return(chan);
  427. }
  428. int parse_chan_pair(char *str, struct list_head *chans, int pri, int device,
  429. struct chan_opts *opts)
  430. {
  431. struct chan *new, *chan;
  432. char *in, *out;
  433. if(!list_empty(chans)){
  434. chan = list_entry(chans->next, struct chan, list);
  435. if(chan->pri >= pri) return(0);
  436. free_chan(chans);
  437. INIT_LIST_HEAD(chans);
  438. }
  439. out = strchr(str, ',');
  440. if(out != NULL){
  441. in = str;
  442. *out = '\0';
  443. out++;
  444. new = parse_chan(in, pri, device, opts);
  445. if(new == NULL) return(-1);
  446. new->input = 1;
  447. list_add(&new->list, chans);
  448. new = parse_chan(out, pri, device, opts);
  449. if(new == NULL) return(-1);
  450. list_add(&new->list, chans);
  451. new->output = 1;
  452. }
  453. else {
  454. new = parse_chan(str, pri, device, opts);
  455. if(new == NULL) return(-1);
  456. list_add(&new->list, chans);
  457. new->input = 1;
  458. new->output = 1;
  459. }
  460. return(0);
  461. }
  462. int chan_out_fd(struct list_head *chans)
  463. {
  464. struct list_head *ele;
  465. struct chan *chan;
  466. list_for_each(ele, chans){
  467. chan = list_entry(ele, struct chan, list);
  468. if(chan->primary && chan->output)
  469. return(chan->fd);
  470. }
  471. return(-1);
  472. }
  473. void chan_interrupt(struct list_head *chans, struct work_struct *task,
  474. struct tty_struct *tty, int irq)
  475. {
  476. struct list_head *ele, *next;
  477. struct chan *chan;
  478. int err;
  479. char c;
  480. list_for_each_safe(ele, next, chans){
  481. chan = list_entry(ele, struct chan, list);
  482. if(!chan->input || (chan->ops->read == NULL)) continue;
  483. do {
  484. if((tty != NULL) &&
  485. (tty->flip.count >= TTY_FLIPBUF_SIZE)){
  486. schedule_work(task);
  487. goto out;
  488. }
  489. err = chan->ops->read(chan->fd, &c, chan->data);
  490. if(err > 0)
  491. tty_receive_char(tty, c);
  492. } while(err > 0);
  493. if(err == 0) reactivate_fd(chan->fd, irq);
  494. if(err == -EIO){
  495. if(chan->primary){
  496. if(tty != NULL)
  497. tty_hangup(tty);
  498. line_disable(tty, irq);
  499. close_chan(chans);
  500. free_chan(chans);
  501. return;
  502. }
  503. else {
  504. if(chan->ops->close != NULL)
  505. chan->ops->close(chan->fd, chan->data);
  506. free_one_chan(chan);
  507. }
  508. }
  509. }
  510. out:
  511. if(tty) tty_flip_buffer_push(tty);
  512. }
  513. /*
  514. * Overrides for Emacs so that we follow Linus's tabbing style.
  515. * Emacs will notice this stuff at the end of the file and automatically
  516. * adjust the settings for this buffer only. This must remain at the end
  517. * of the file.
  518. * ---------------------------------------------------------------------------
  519. * Local variables:
  520. * c-file-style: "linux"
  521. * End:
  522. */