chan_kern.c 12 KB

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