chan_kern.c 12 KB

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