chan_kern.c 11 KB

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