chan_kern.c 13 KB

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