chan_kern.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. static 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 && chan->enabled)
  179. free_irq(chan->line->driver->read_irq, chan);
  180. if (chan->output && chan->enabled)
  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 && chan->enabled)
  197. free_irq(chan->line->driver->read_irq, chan);
  198. if (chan->output && chan->enabled)
  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. if (len == 0)
  246. return 0;
  247. list_for_each(ele, chans) {
  248. chan = list_entry(ele, struct chan, list);
  249. if (!chan->output || (chan->ops->write == NULL))
  250. continue;
  251. n = chan->ops->write(chan->fd, buf, len, chan->data);
  252. if (chan->primary) {
  253. ret = n;
  254. if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
  255. reactivate_fd(chan->fd, write_irq);
  256. }
  257. }
  258. return ret;
  259. }
  260. int console_write_chan(struct list_head *chans, const char *buf, int len)
  261. {
  262. struct list_head *ele;
  263. struct chan *chan;
  264. int n, ret = 0;
  265. list_for_each(ele, chans) {
  266. chan = list_entry(ele, struct chan, list);
  267. if (!chan->output || (chan->ops->console_write == NULL))
  268. continue;
  269. n = chan->ops->console_write(chan->fd, buf, len);
  270. if (chan->primary)
  271. ret = n;
  272. }
  273. return ret;
  274. }
  275. int console_open_chan(struct line *line, struct console *co)
  276. {
  277. int err;
  278. err = open_chan(&line->chan_list);
  279. if (err)
  280. return err;
  281. printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
  282. co->index);
  283. return 0;
  284. }
  285. int chan_window_size(struct list_head *chans, unsigned short *rows_out,
  286. unsigned short *cols_out)
  287. {
  288. struct list_head *ele;
  289. struct chan *chan;
  290. list_for_each(ele, chans) {
  291. chan = list_entry(ele, struct chan, list);
  292. if (chan->primary) {
  293. if (chan->ops->window_size == NULL)
  294. return 0;
  295. return chan->ops->window_size(chan->fd, chan->data,
  296. rows_out, cols_out);
  297. }
  298. }
  299. return 0;
  300. }
  301. static void free_one_chan(struct chan *chan, int delay_free_irq)
  302. {
  303. list_del(&chan->list);
  304. close_one_chan(chan, delay_free_irq);
  305. if (chan->ops->free != NULL)
  306. (*chan->ops->free)(chan->data);
  307. if (chan->primary && chan->output)
  308. ignore_sigio_fd(chan->fd);
  309. kfree(chan);
  310. }
  311. static void free_chan(struct list_head *chans, int delay_free_irq)
  312. {
  313. struct list_head *ele, *next;
  314. struct chan *chan;
  315. list_for_each_safe(ele, next, chans) {
  316. chan = list_entry(ele, struct chan, list);
  317. free_one_chan(chan, delay_free_irq);
  318. }
  319. }
  320. static int one_chan_config_string(struct chan *chan, char *str, int size,
  321. char **error_out)
  322. {
  323. int n = 0;
  324. if (chan == NULL) {
  325. CONFIG_CHUNK(str, size, n, "none", 1);
  326. return n;
  327. }
  328. CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
  329. if (chan->dev == NULL) {
  330. CONFIG_CHUNK(str, size, n, "", 1);
  331. return n;
  332. }
  333. CONFIG_CHUNK(str, size, n, ":", 0);
  334. CONFIG_CHUNK(str, size, n, chan->dev, 0);
  335. return n;
  336. }
  337. static int chan_pair_config_string(struct chan *in, struct chan *out,
  338. char *str, int size, char **error_out)
  339. {
  340. int n;
  341. n = one_chan_config_string(in, str, size, error_out);
  342. str += n;
  343. size -= n;
  344. if (in == out) {
  345. CONFIG_CHUNK(str, size, n, "", 1);
  346. return n;
  347. }
  348. CONFIG_CHUNK(str, size, n, ",", 1);
  349. n = one_chan_config_string(out, str, size, error_out);
  350. str += n;
  351. size -= n;
  352. CONFIG_CHUNK(str, size, n, "", 1);
  353. return n;
  354. }
  355. int chan_config_string(struct list_head *chans, char *str, int size,
  356. char **error_out)
  357. {
  358. struct list_head *ele;
  359. struct chan *chan, *in = NULL, *out = NULL;
  360. list_for_each(ele, chans) {
  361. chan = list_entry(ele, struct chan, list);
  362. if (!chan->primary)
  363. continue;
  364. if (chan->input)
  365. in = chan;
  366. if (chan->output)
  367. out = chan;
  368. }
  369. return chan_pair_config_string(in, out, str, size, error_out);
  370. }
  371. struct chan_type {
  372. char *key;
  373. const struct chan_ops *ops;
  374. };
  375. static const struct chan_type chan_table[] = {
  376. { "fd", &fd_ops },
  377. #ifdef CONFIG_NULL_CHAN
  378. { "null", &null_ops },
  379. #else
  380. { "null", &not_configged_ops },
  381. #endif
  382. #ifdef CONFIG_PORT_CHAN
  383. { "port", &port_ops },
  384. #else
  385. { "port", &not_configged_ops },
  386. #endif
  387. #ifdef CONFIG_PTY_CHAN
  388. { "pty", &pty_ops },
  389. { "pts", &pts_ops },
  390. #else
  391. { "pty", &not_configged_ops },
  392. { "pts", &not_configged_ops },
  393. #endif
  394. #ifdef CONFIG_TTY_CHAN
  395. { "tty", &tty_ops },
  396. #else
  397. { "tty", &not_configged_ops },
  398. #endif
  399. #ifdef CONFIG_XTERM_CHAN
  400. { "xterm", &xterm_ops },
  401. #else
  402. { "xterm", &not_configged_ops },
  403. #endif
  404. };
  405. static struct chan *parse_chan(struct line *line, char *str, int device,
  406. const struct chan_opts *opts, char **error_out)
  407. {
  408. const struct chan_type *entry;
  409. const struct chan_ops *ops;
  410. struct chan *chan;
  411. void *data;
  412. int i;
  413. ops = NULL;
  414. data = NULL;
  415. for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
  416. entry = &chan_table[i];
  417. if (!strncmp(str, entry->key, strlen(entry->key))) {
  418. ops = entry->ops;
  419. str += strlen(entry->key);
  420. break;
  421. }
  422. }
  423. if (ops == NULL) {
  424. *error_out = "No match for configured backends";
  425. return NULL;
  426. }
  427. data = (*ops->init)(str, device, opts);
  428. if (data == NULL) {
  429. *error_out = "Configuration failed";
  430. return NULL;
  431. }
  432. chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
  433. if (chan == NULL) {
  434. *error_out = "Memory allocation failed";
  435. return NULL;
  436. }
  437. *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
  438. .free_list =
  439. LIST_HEAD_INIT(chan->free_list),
  440. .line = line,
  441. .primary = 1,
  442. .input = 0,
  443. .output = 0,
  444. .opened = 0,
  445. .enabled = 0,
  446. .fd = -1,
  447. .ops = ops,
  448. .data = data });
  449. return chan;
  450. }
  451. int parse_chan_pair(char *str, struct line *line, int device,
  452. const struct chan_opts *opts, char **error_out)
  453. {
  454. struct list_head *chans = &line->chan_list;
  455. struct chan *new, *chan;
  456. char *in, *out;
  457. if (!list_empty(chans)) {
  458. chan = list_entry(chans->next, struct chan, list);
  459. free_chan(chans, 0);
  460. INIT_LIST_HEAD(chans);
  461. }
  462. out = strchr(str, ',');
  463. if (out != NULL) {
  464. in = str;
  465. *out = '\0';
  466. out++;
  467. new = parse_chan(line, in, device, opts, error_out);
  468. if (new == NULL)
  469. return -1;
  470. new->input = 1;
  471. list_add(&new->list, chans);
  472. new = parse_chan(line, out, device, opts, error_out);
  473. if (new == NULL)
  474. return -1;
  475. list_add(&new->list, chans);
  476. new->output = 1;
  477. }
  478. else {
  479. new = parse_chan(line, str, device, opts, error_out);
  480. if (new == NULL)
  481. return -1;
  482. list_add(&new->list, chans);
  483. new->input = 1;
  484. new->output = 1;
  485. }
  486. return 0;
  487. }
  488. void chan_interrupt(struct list_head *chans, struct delayed_work *task,
  489. struct tty_struct *tty, int irq)
  490. {
  491. struct list_head *ele, *next;
  492. struct chan *chan;
  493. int err;
  494. char c;
  495. list_for_each_safe(ele, next, chans) {
  496. chan = list_entry(ele, struct chan, list);
  497. if (!chan->input || (chan->ops->read == NULL))
  498. continue;
  499. do {
  500. if (tty && !tty_buffer_request_room(tty, 1)) {
  501. schedule_delayed_work(task, 1);
  502. goto out;
  503. }
  504. err = chan->ops->read(chan->fd, &c, chan->data);
  505. if (err > 0)
  506. tty_receive_char(tty, c);
  507. } while (err > 0);
  508. if (err == 0)
  509. reactivate_fd(chan->fd, irq);
  510. if (err == -EIO) {
  511. if (chan->primary) {
  512. if (tty != NULL)
  513. tty_hangup(tty);
  514. close_chan(chans, 1);
  515. return;
  516. }
  517. else close_one_chan(chan, 1);
  518. }
  519. }
  520. out:
  521. if (tty)
  522. tty_flip_buffer_push(tty);
  523. }