chan_kern.c 12 KB

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