chan_kern.c 13 KB

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