chan_kern.c 14 KB

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