chan_kern.c 13 KB

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