chan_kern.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. /* Items are added in IRQ context, when free_irq can't be called, and
  189. * removed in process context, when it can.
  190. * This handles interrupt sources which disappear, and which need to
  191. * be permanently disabled. This is discovered in IRQ context, but
  192. * the freeing of the IRQ must be done later.
  193. */
  194. static DEFINE_SPINLOCK(irqs_to_free_lock);
  195. static LIST_HEAD(irqs_to_free);
  196. void free_irqs(void)
  197. {
  198. struct chan *chan;
  199. LIST_HEAD(list);
  200. struct list_head *ele;
  201. spin_lock_irq(&irqs_to_free_lock);
  202. list_splice_init(&irqs_to_free, &list);
  203. INIT_LIST_HEAD(&irqs_to_free);
  204. spin_unlock_irq(&irqs_to_free_lock);
  205. list_for_each(ele, &list){
  206. chan = list_entry(ele, struct chan, free_list);
  207. if(chan->input)
  208. free_irq(chan->line->driver->read_irq, chan);
  209. if(chan->output)
  210. free_irq(chan->line->driver->write_irq, chan);
  211. chan->enabled = 0;
  212. }
  213. }
  214. static void close_one_chan(struct chan *chan, int delay_free_irq)
  215. {
  216. if(!chan->opened)
  217. return;
  218. if(delay_free_irq){
  219. spin_lock_irq(&irqs_to_free_lock);
  220. list_add(&chan->free_list, &irqs_to_free);
  221. spin_unlock_irq(&irqs_to_free_lock);
  222. }
  223. else {
  224. if(chan->input)
  225. free_irq(chan->line->driver->read_irq, chan);
  226. if(chan->output)
  227. free_irq(chan->line->driver->write_irq, chan);
  228. chan->enabled = 0;
  229. }
  230. if(chan->ops->close != NULL)
  231. (*chan->ops->close)(chan->fd, chan->data);
  232. chan->opened = 0;
  233. chan->fd = -1;
  234. }
  235. void close_chan(struct list_head *chans, int delay_free_irq)
  236. {
  237. struct chan *chan;
  238. /* Close in reverse order as open in case more than one of them
  239. * refers to the same device and they save and restore that device's
  240. * state. Then, the first one opened will have the original state,
  241. * so it must be the last closed.
  242. */
  243. list_for_each_entry_reverse(chan, chans, list) {
  244. close_one_chan(chan, delay_free_irq);
  245. }
  246. }
  247. void deactivate_chan(struct list_head *chans, int irq)
  248. {
  249. struct list_head *ele;
  250. struct chan *chan;
  251. list_for_each(ele, chans) {
  252. chan = list_entry(ele, struct chan, list);
  253. if(chan->enabled && chan->input)
  254. deactivate_fd(chan->fd, irq);
  255. }
  256. }
  257. void reactivate_chan(struct list_head *chans, int irq)
  258. {
  259. struct list_head *ele;
  260. struct chan *chan;
  261. list_for_each(ele, chans) {
  262. chan = list_entry(ele, struct chan, list);
  263. if(chan->enabled && chan->input)
  264. reactivate_fd(chan->fd, irq);
  265. }
  266. }
  267. int write_chan(struct list_head *chans, const char *buf, int len,
  268. int write_irq)
  269. {
  270. struct list_head *ele;
  271. struct chan *chan = NULL;
  272. int n, ret = 0;
  273. list_for_each(ele, chans) {
  274. chan = list_entry(ele, struct chan, list);
  275. if (!chan->output || (chan->ops->write == NULL))
  276. continue;
  277. n = chan->ops->write(chan->fd, buf, len, chan->data);
  278. if (chan->primary) {
  279. ret = n;
  280. if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
  281. reactivate_fd(chan->fd, write_irq);
  282. }
  283. }
  284. return ret;
  285. }
  286. int console_write_chan(struct list_head *chans, const char *buf, int len)
  287. {
  288. struct list_head *ele;
  289. struct chan *chan;
  290. int n, ret = 0;
  291. list_for_each(ele, chans){
  292. chan = list_entry(ele, struct chan, list);
  293. if(!chan->output || (chan->ops->console_write == NULL))
  294. continue;
  295. n = chan->ops->console_write(chan->fd, buf, len);
  296. if(chan->primary) ret = n;
  297. }
  298. return ret;
  299. }
  300. int console_open_chan(struct line *line, struct console *co)
  301. {
  302. int err;
  303. err = open_chan(&line->chan_list);
  304. if(err)
  305. return err;
  306. printk("Console initialized on /dev/%s%d\n", co->name, co->index);
  307. return 0;
  308. }
  309. int chan_window_size(struct list_head *chans, unsigned short *rows_out,
  310. unsigned short *cols_out)
  311. {
  312. struct list_head *ele;
  313. struct chan *chan;
  314. list_for_each(ele, chans){
  315. chan = list_entry(ele, struct chan, list);
  316. if(chan->primary){
  317. if(chan->ops->window_size == NULL)
  318. return 0;
  319. return chan->ops->window_size(chan->fd, chan->data,
  320. rows_out, cols_out);
  321. }
  322. }
  323. return 0;
  324. }
  325. static void free_one_chan(struct chan *chan, int delay_free_irq)
  326. {
  327. list_del(&chan->list);
  328. close_one_chan(chan, delay_free_irq);
  329. if(chan->ops->free != NULL)
  330. (*chan->ops->free)(chan->data);
  331. if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
  332. kfree(chan);
  333. }
  334. static void free_chan(struct list_head *chans, int delay_free_irq)
  335. {
  336. struct list_head *ele, *next;
  337. struct chan *chan;
  338. list_for_each_safe(ele, next, chans){
  339. chan = list_entry(ele, struct chan, list);
  340. free_one_chan(chan, delay_free_irq);
  341. }
  342. }
  343. static int one_chan_config_string(struct chan *chan, char *str, int size,
  344. char **error_out)
  345. {
  346. int n = 0;
  347. if(chan == NULL){
  348. CONFIG_CHUNK(str, size, n, "none", 1);
  349. return n;
  350. }
  351. CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
  352. if(chan->dev == NULL){
  353. CONFIG_CHUNK(str, size, n, "", 1);
  354. return n;
  355. }
  356. CONFIG_CHUNK(str, size, n, ":", 0);
  357. CONFIG_CHUNK(str, size, n, chan->dev, 0);
  358. return n;
  359. }
  360. static int chan_pair_config_string(struct chan *in, struct chan *out,
  361. char *str, int size, char **error_out)
  362. {
  363. int n;
  364. n = one_chan_config_string(in, str, size, error_out);
  365. str += n;
  366. size -= n;
  367. if(in == out){
  368. CONFIG_CHUNK(str, size, n, "", 1);
  369. return n;
  370. }
  371. CONFIG_CHUNK(str, size, n, ",", 1);
  372. n = one_chan_config_string(out, str, size, error_out);
  373. str += n;
  374. size -= n;
  375. CONFIG_CHUNK(str, size, n, "", 1);
  376. return n;
  377. }
  378. int chan_config_string(struct list_head *chans, char *str, int size,
  379. char **error_out)
  380. {
  381. struct list_head *ele;
  382. struct chan *chan, *in = NULL, *out = NULL;
  383. list_for_each(ele, chans){
  384. chan = list_entry(ele, struct chan, list);
  385. if(!chan->primary)
  386. continue;
  387. if(chan->input)
  388. in = chan;
  389. if(chan->output)
  390. out = chan;
  391. }
  392. return chan_pair_config_string(in, out, str, size, error_out);
  393. }
  394. struct chan_type {
  395. char *key;
  396. const struct chan_ops *ops;
  397. };
  398. static const struct chan_type chan_table[] = {
  399. { "fd", &fd_ops },
  400. #ifdef CONFIG_NULL_CHAN
  401. { "null", &null_ops },
  402. #else
  403. { "null", &not_configged_ops },
  404. #endif
  405. #ifdef CONFIG_PORT_CHAN
  406. { "port", &port_ops },
  407. #else
  408. { "port", &not_configged_ops },
  409. #endif
  410. #ifdef CONFIG_PTY_CHAN
  411. { "pty", &pty_ops },
  412. { "pts", &pts_ops },
  413. #else
  414. { "pty", &not_configged_ops },
  415. { "pts", &not_configged_ops },
  416. #endif
  417. #ifdef CONFIG_TTY_CHAN
  418. { "tty", &tty_ops },
  419. #else
  420. { "tty", &not_configged_ops },
  421. #endif
  422. #ifdef CONFIG_XTERM_CHAN
  423. { "xterm", &xterm_ops },
  424. #else
  425. { "xterm", &not_configged_ops },
  426. #endif
  427. };
  428. static struct chan *parse_chan(struct line *line, char *str, int device,
  429. const struct chan_opts *opts, char **error_out)
  430. {
  431. const struct chan_type *entry;
  432. const struct chan_ops *ops;
  433. struct chan *chan;
  434. void *data;
  435. int i;
  436. ops = NULL;
  437. data = NULL;
  438. for(i = 0; i < ARRAY_SIZE(chan_table); i++){
  439. entry = &chan_table[i];
  440. if(!strncmp(str, entry->key, strlen(entry->key))){
  441. ops = entry->ops;
  442. str += strlen(entry->key);
  443. break;
  444. }
  445. }
  446. if(ops == NULL){
  447. *error_out = "No match for configured backends";
  448. return NULL;
  449. }
  450. data = (*ops->init)(str, device, opts);
  451. if(data == NULL){
  452. *error_out = "Configuration failed";
  453. return NULL;
  454. }
  455. chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
  456. if(chan == NULL){
  457. *error_out = "Memory allocation failed";
  458. return NULL;
  459. }
  460. *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
  461. .free_list =
  462. LIST_HEAD_INIT(chan->free_list),
  463. .line = line,
  464. .primary = 1,
  465. .input = 0,
  466. .output = 0,
  467. .opened = 0,
  468. .enabled = 0,
  469. .fd = -1,
  470. .ops = ops,
  471. .data = data });
  472. return chan;
  473. }
  474. int parse_chan_pair(char *str, struct line *line, int device,
  475. const struct chan_opts *opts, char **error_out)
  476. {
  477. struct list_head *chans = &line->chan_list;
  478. struct chan *new, *chan;
  479. char *in, *out;
  480. if(!list_empty(chans)){
  481. chan = list_entry(chans->next, struct chan, list);
  482. free_chan(chans, 0);
  483. INIT_LIST_HEAD(chans);
  484. }
  485. out = strchr(str, ',');
  486. if(out != NULL){
  487. in = str;
  488. *out = '\0';
  489. out++;
  490. new = parse_chan(line, in, device, opts, error_out);
  491. if(new == NULL)
  492. return -1;
  493. new->input = 1;
  494. list_add(&new->list, chans);
  495. new = parse_chan(line, out, device, opts, error_out);
  496. if(new == NULL)
  497. return -1;
  498. list_add(&new->list, chans);
  499. new->output = 1;
  500. }
  501. else {
  502. new = parse_chan(line, str, device, opts, error_out);
  503. if(new == NULL)
  504. return -1;
  505. list_add(&new->list, chans);
  506. new->input = 1;
  507. new->output = 1;
  508. }
  509. return 0;
  510. }
  511. int chan_out_fd(struct list_head *chans)
  512. {
  513. struct list_head *ele;
  514. struct chan *chan;
  515. list_for_each(ele, chans){
  516. chan = list_entry(ele, struct chan, list);
  517. if(chan->primary && chan->output)
  518. return chan->fd;
  519. }
  520. return -1;
  521. }
  522. void chan_interrupt(struct list_head *chans, struct delayed_work *task,
  523. struct tty_struct *tty, int irq)
  524. {
  525. struct list_head *ele, *next;
  526. struct chan *chan;
  527. int err;
  528. char c;
  529. list_for_each_safe(ele, next, chans){
  530. chan = list_entry(ele, struct chan, list);
  531. if(!chan->input || (chan->ops->read == NULL)) continue;
  532. do {
  533. if (tty && !tty_buffer_request_room(tty, 1)) {
  534. schedule_delayed_work(task, 1);
  535. goto out;
  536. }
  537. err = chan->ops->read(chan->fd, &c, chan->data);
  538. if(err > 0)
  539. tty_receive_char(tty, c);
  540. } while(err > 0);
  541. if(err == 0) reactivate_fd(chan->fd, irq);
  542. if(err == -EIO){
  543. if(chan->primary){
  544. if(tty != NULL)
  545. tty_hangup(tty);
  546. close_chan(chans, 1);
  547. return;
  548. }
  549. else close_one_chan(chan, 1);
  550. }
  551. }
  552. out:
  553. if(tty) tty_flip_buffer_push(tty);
  554. }