chan_kern.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. } else {
  180. if (chan->input && chan->enabled)
  181. um_free_irq(chan->line->driver->read_irq, chan);
  182. if (chan->output && chan->enabled)
  183. um_free_irq(chan->line->driver->write_irq, chan);
  184. chan->enabled = 0;
  185. }
  186. if (chan->ops->close != NULL)
  187. (*chan->ops->close)(chan->fd, chan->data);
  188. chan->opened = 0;
  189. chan->fd = -1;
  190. }
  191. void close_chan(struct line *line)
  192. {
  193. struct chan *chan;
  194. /* Close in reverse order as open in case more than one of them
  195. * refers to the same device and they save and restore that device's
  196. * state. Then, the first one opened will have the original state,
  197. * so it must be the last closed.
  198. */
  199. list_for_each_entry_reverse(chan, &line->chan_list, list) {
  200. close_one_chan(chan, 0);
  201. }
  202. }
  203. void deactivate_chan(struct chan *chan, int irq)
  204. {
  205. if (chan && chan->enabled)
  206. deactivate_fd(chan->fd, irq);
  207. }
  208. void reactivate_chan(struct chan *chan, int irq)
  209. {
  210. if (chan && chan->enabled)
  211. reactivate_fd(chan->fd, irq);
  212. }
  213. int write_chan(struct chan *chan, const char *buf, int len,
  214. int write_irq)
  215. {
  216. int n, ret = 0;
  217. if (len == 0 || !chan || !chan->ops->write)
  218. return 0;
  219. n = chan->ops->write(chan->fd, buf, len, chan->data);
  220. if (chan->primary) {
  221. ret = n;
  222. if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
  223. reactivate_fd(chan->fd, write_irq);
  224. }
  225. return ret;
  226. }
  227. int console_write_chan(struct chan *chan, const char *buf, int len)
  228. {
  229. int n, ret = 0;
  230. if (!chan || !chan->ops->console_write)
  231. return 0;
  232. n = chan->ops->console_write(chan->fd, buf, len);
  233. if (chan->primary)
  234. ret = n;
  235. return ret;
  236. }
  237. int console_open_chan(struct line *line, struct console *co)
  238. {
  239. int err;
  240. err = open_chan(&line->chan_list);
  241. if (err)
  242. return err;
  243. printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
  244. co->index);
  245. return 0;
  246. }
  247. int chan_window_size(struct line *line, unsigned short *rows_out,
  248. unsigned short *cols_out)
  249. {
  250. struct chan *chan;
  251. chan = line->chan_in;
  252. if (chan && chan->primary) {
  253. if (chan->ops->window_size == NULL)
  254. return 0;
  255. return chan->ops->window_size(chan->fd, chan->data,
  256. rows_out, cols_out);
  257. }
  258. chan = line->chan_out;
  259. if (chan && chan->primary) {
  260. if (chan->ops->window_size == NULL)
  261. return 0;
  262. return chan->ops->window_size(chan->fd, chan->data,
  263. rows_out, cols_out);
  264. }
  265. return 0;
  266. }
  267. static void free_one_chan(struct chan *chan)
  268. {
  269. list_del(&chan->list);
  270. close_one_chan(chan, 0);
  271. if (chan->ops->free != NULL)
  272. (*chan->ops->free)(chan->data);
  273. if (chan->primary && chan->output)
  274. ignore_sigio_fd(chan->fd);
  275. kfree(chan);
  276. }
  277. static void free_chan(struct list_head *chans)
  278. {
  279. struct list_head *ele, *next;
  280. struct chan *chan;
  281. list_for_each_safe(ele, next, chans) {
  282. chan = list_entry(ele, struct chan, list);
  283. free_one_chan(chan);
  284. }
  285. }
  286. static int one_chan_config_string(struct chan *chan, char *str, int size,
  287. char **error_out)
  288. {
  289. int n = 0;
  290. if (chan == NULL) {
  291. CONFIG_CHUNK(str, size, n, "none", 1);
  292. return n;
  293. }
  294. CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
  295. if (chan->dev == NULL) {
  296. CONFIG_CHUNK(str, size, n, "", 1);
  297. return n;
  298. }
  299. CONFIG_CHUNK(str, size, n, ":", 0);
  300. CONFIG_CHUNK(str, size, n, chan->dev, 0);
  301. return n;
  302. }
  303. static int chan_pair_config_string(struct chan *in, struct chan *out,
  304. char *str, int size, char **error_out)
  305. {
  306. int n;
  307. n = one_chan_config_string(in, str, size, error_out);
  308. str += n;
  309. size -= n;
  310. if (in == out) {
  311. CONFIG_CHUNK(str, size, n, "", 1);
  312. return n;
  313. }
  314. CONFIG_CHUNK(str, size, n, ",", 1);
  315. n = one_chan_config_string(out, str, size, error_out);
  316. str += n;
  317. size -= n;
  318. CONFIG_CHUNK(str, size, n, "", 1);
  319. return n;
  320. }
  321. int chan_config_string(struct line *line, char *str, int size,
  322. char **error_out)
  323. {
  324. struct chan *in = line->chan_in, *out = line->chan_out;
  325. if (in && !in->primary)
  326. in = NULL;
  327. if (out && !out->primary)
  328. out = NULL;
  329. return chan_pair_config_string(in, out, str, size, error_out);
  330. }
  331. struct chan_type {
  332. char *key;
  333. const struct chan_ops *ops;
  334. };
  335. static const struct chan_type chan_table[] = {
  336. { "fd", &fd_ops },
  337. #ifdef CONFIG_NULL_CHAN
  338. { "null", &null_ops },
  339. #else
  340. { "null", &not_configged_ops },
  341. #endif
  342. #ifdef CONFIG_PORT_CHAN
  343. { "port", &port_ops },
  344. #else
  345. { "port", &not_configged_ops },
  346. #endif
  347. #ifdef CONFIG_PTY_CHAN
  348. { "pty", &pty_ops },
  349. { "pts", &pts_ops },
  350. #else
  351. { "pty", &not_configged_ops },
  352. { "pts", &not_configged_ops },
  353. #endif
  354. #ifdef CONFIG_TTY_CHAN
  355. { "tty", &tty_ops },
  356. #else
  357. { "tty", &not_configged_ops },
  358. #endif
  359. #ifdef CONFIG_XTERM_CHAN
  360. { "xterm", &xterm_ops },
  361. #else
  362. { "xterm", &not_configged_ops },
  363. #endif
  364. };
  365. static struct chan *parse_chan(struct line *line, char *str, int device,
  366. const struct chan_opts *opts, char **error_out)
  367. {
  368. const struct chan_type *entry;
  369. const struct chan_ops *ops;
  370. struct chan *chan;
  371. void *data;
  372. int i;
  373. ops = NULL;
  374. data = NULL;
  375. for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
  376. entry = &chan_table[i];
  377. if (!strncmp(str, entry->key, strlen(entry->key))) {
  378. ops = entry->ops;
  379. str += strlen(entry->key);
  380. break;
  381. }
  382. }
  383. if (ops == NULL) {
  384. *error_out = "No match for configured backends";
  385. return NULL;
  386. }
  387. data = (*ops->init)(str, device, opts);
  388. if (data == NULL) {
  389. *error_out = "Configuration failed";
  390. return NULL;
  391. }
  392. chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
  393. if (chan == NULL) {
  394. *error_out = "Memory allocation failed";
  395. return NULL;
  396. }
  397. *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
  398. .free_list =
  399. LIST_HEAD_INIT(chan->free_list),
  400. .line = line,
  401. .primary = 1,
  402. .input = 0,
  403. .output = 0,
  404. .opened = 0,
  405. .enabled = 0,
  406. .fd = -1,
  407. .ops = ops,
  408. .data = data });
  409. return chan;
  410. }
  411. int parse_chan_pair(char *str, struct line *line, int device,
  412. const struct chan_opts *opts, char **error_out)
  413. {
  414. struct list_head *chans = &line->chan_list;
  415. struct chan *new;
  416. char *in, *out;
  417. if (!list_empty(chans)) {
  418. line->chan_in = line->chan_out = NULL;
  419. free_chan(chans);
  420. INIT_LIST_HEAD(chans);
  421. }
  422. if (!str)
  423. return 0;
  424. out = strchr(str, ',');
  425. if (out != NULL) {
  426. in = str;
  427. *out = '\0';
  428. out++;
  429. new = parse_chan(line, in, device, opts, error_out);
  430. if (new == NULL)
  431. return -1;
  432. new->input = 1;
  433. list_add(&new->list, chans);
  434. line->chan_in = new;
  435. new = parse_chan(line, out, device, opts, error_out);
  436. if (new == NULL)
  437. return -1;
  438. list_add(&new->list, chans);
  439. new->output = 1;
  440. line->chan_out = new;
  441. }
  442. else {
  443. new = parse_chan(line, str, device, opts, error_out);
  444. if (new == NULL)
  445. return -1;
  446. list_add(&new->list, chans);
  447. new->input = 1;
  448. new->output = 1;
  449. line->chan_in = line->chan_out = new;
  450. }
  451. return 0;
  452. }
  453. void chan_interrupt(struct line *line, int irq)
  454. {
  455. struct tty_port *port = &line->port;
  456. struct chan *chan = line->chan_in;
  457. int err;
  458. char c;
  459. if (!chan || !chan->ops->read)
  460. goto out;
  461. do {
  462. if (!tty_buffer_request_room(port, 1)) {
  463. schedule_delayed_work(&line->task, 1);
  464. goto out;
  465. }
  466. err = chan->ops->read(chan->fd, &c, chan->data);
  467. if (err > 0)
  468. tty_insert_flip_char(port, c, TTY_NORMAL);
  469. } while (err > 0);
  470. if (err == 0)
  471. reactivate_fd(chan->fd, irq);
  472. if (err == -EIO) {
  473. if (chan->primary) {
  474. tty_port_tty_hangup(&line->port, false);
  475. if (line->chan_out != chan)
  476. close_one_chan(line->chan_out, 1);
  477. }
  478. close_one_chan(chan, 1);
  479. if (chan->primary)
  480. return;
  481. }
  482. out:
  483. tty_flip_buffer_push(port);
  484. }