chan_kern.c 12 KB

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