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