chan_kern.c 12 KB

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