chan_kern.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/stddef.h>
  6. #include <linux/kernel.h>
  7. #include <linux/list.h>
  8. #include <linux/slab.h>
  9. #include <linux/tty.h>
  10. #include <linux/string.h>
  11. #include <linux/tty_flip.h>
  12. #include <asm/irq.h>
  13. #include "chan_kern.h"
  14. #include "kern.h"
  15. #include "irq_user.h"
  16. #include "sigio.h"
  17. #include "line.h"
  18. #include "os.h"
  19. #ifdef CONFIG_NOCONFIG_CHAN
  20. static void *not_configged_init(char *str, int device,
  21. const struct chan_opts *opts)
  22. {
  23. printk("Using a channel type which is configured out of "
  24. "UML\n");
  25. return NULL;
  26. }
  27. static int not_configged_open(int input, int output, int primary, void *data,
  28. char **dev_out)
  29. {
  30. printk("Using a channel type which is configured out of "
  31. "UML\n");
  32. return -ENODEV;
  33. }
  34. static void not_configged_close(int fd, void *data)
  35. {
  36. printk("Using a channel type which is configured out of "
  37. "UML\n");
  38. }
  39. static int not_configged_read(int fd, char *c_out, void *data)
  40. {
  41. printk("Using a channel type which is configured out of "
  42. "UML\n");
  43. return -EIO;
  44. }
  45. static int not_configged_write(int fd, const char *buf, int len, void *data)
  46. {
  47. printk("Using a channel type which is configured out of "
  48. "UML\n");
  49. return -EIO;
  50. }
  51. static int not_configged_console_write(int fd, const char *buf, int len)
  52. {
  53. printk("Using a channel type which is configured out of "
  54. "UML\n");
  55. return -EIO;
  56. }
  57. static int not_configged_window_size(int fd, void *data, unsigned short *rows,
  58. unsigned short *cols)
  59. {
  60. printk("Using a channel type which is configured out of "
  61. "UML\n");
  62. return -ENODEV;
  63. }
  64. static void not_configged_free(void *data)
  65. {
  66. printk("Using a channel type which is configured out of "
  67. "UML\n");
  68. }
  69. static const struct chan_ops not_configged_ops = {
  70. .init = not_configged_init,
  71. .open = not_configged_open,
  72. .close = not_configged_close,
  73. .read = not_configged_read,
  74. .write = not_configged_write,
  75. .console_write = not_configged_console_write,
  76. .window_size = not_configged_window_size,
  77. .free = not_configged_free,
  78. .winch = 0,
  79. };
  80. #endif /* CONFIG_NOCONFIG_CHAN */
  81. void generic_close(int fd, void *unused)
  82. {
  83. os_close_file(fd);
  84. }
  85. int generic_read(int fd, char *c_out, void *unused)
  86. {
  87. int n;
  88. n = os_read_file(fd, c_out, sizeof(*c_out));
  89. if(n == -EAGAIN)
  90. return 0;
  91. else if(n == 0)
  92. return -EIO;
  93. return n;
  94. }
  95. /* XXX Trivial wrapper around os_write_file */
  96. int generic_write(int fd, const char *buf, int n, void *unused)
  97. {
  98. return os_write_file(fd, buf, n);
  99. }
  100. int generic_window_size(int fd, void *unused, unsigned short *rows_out,
  101. unsigned short *cols_out)
  102. {
  103. int rows, cols;
  104. int ret;
  105. ret = os_window_size(fd, &rows, &cols);
  106. if(ret < 0)
  107. return ret;
  108. ret = ((*rows_out != rows) || (*cols_out != cols));
  109. *rows_out = rows;
  110. *cols_out = cols;
  111. return ret;
  112. }
  113. void generic_free(void *data)
  114. {
  115. kfree(data);
  116. }
  117. static void tty_receive_char(struct tty_struct *tty, char ch)
  118. {
  119. if(tty == NULL) return;
  120. if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
  121. if(ch == STOP_CHAR(tty)){
  122. stop_tty(tty);
  123. return;
  124. }
  125. else if(ch == START_CHAR(tty)){
  126. start_tty(tty);
  127. return;
  128. }
  129. }
  130. tty_insert_flip_char(tty, ch, TTY_NORMAL);
  131. }
  132. static int open_one_chan(struct chan *chan)
  133. {
  134. int fd;
  135. if(chan->opened)
  136. return 0;
  137. if(chan->ops->open == NULL)
  138. fd = 0;
  139. else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
  140. chan->data, &chan->dev);
  141. if(fd < 0)
  142. return fd;
  143. chan->fd = fd;
  144. chan->opened = 1;
  145. return 0;
  146. }
  147. int open_chan(struct list_head *chans)
  148. {
  149. struct list_head *ele;
  150. struct chan *chan;
  151. int ret, err = 0;
  152. list_for_each(ele, chans){
  153. chan = list_entry(ele, struct chan, list);
  154. ret = open_one_chan(chan);
  155. if(chan->primary)
  156. err = ret;
  157. }
  158. return err;
  159. }
  160. void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
  161. {
  162. struct list_head *ele;
  163. struct chan *chan;
  164. list_for_each(ele, chans){
  165. chan = list_entry(ele, struct chan, list);
  166. if(chan->primary && chan->output && chan->ops->winch){
  167. register_winch(chan->fd, tty);
  168. return;
  169. }
  170. }
  171. }
  172. void enable_chan(struct line *line)
  173. {
  174. struct list_head *ele;
  175. struct chan *chan;
  176. list_for_each(ele, &line->chan_list){
  177. chan = list_entry(ele, struct chan, list);
  178. if(open_one_chan(chan))
  179. continue;
  180. if(chan->enabled)
  181. continue;
  182. line_setup_irq(chan->fd, chan->input, chan->output, line,
  183. chan);
  184. chan->enabled = 1;
  185. }
  186. }
  187. /* Items are added in IRQ context, when free_irq can't be called, and
  188. * removed in process context, when it can.
  189. * This handles interrupt sources which disappear, and which need to
  190. * be permanently disabled. This is discovered in IRQ context, but
  191. * the freeing of the IRQ must be done later.
  192. */
  193. static DEFINE_SPINLOCK(irqs_to_free_lock);
  194. static LIST_HEAD(irqs_to_free);
  195. void free_irqs(void)
  196. {
  197. struct chan *chan;
  198. LIST_HEAD(list);
  199. struct list_head *ele;
  200. unsigned long flags;
  201. spin_lock_irqsave(&irqs_to_free_lock, flags);
  202. list_splice_init(&irqs_to_free, &list);
  203. spin_unlock_irqrestore(&irqs_to_free_lock, flags);
  204. list_for_each(ele, &list){
  205. chan = list_entry(ele, struct chan, free_list);
  206. if(chan->input)
  207. free_irq(chan->line->driver->read_irq, chan);
  208. if(chan->output)
  209. free_irq(chan->line->driver->write_irq, chan);
  210. chan->enabled = 0;
  211. }
  212. }
  213. static void close_one_chan(struct chan *chan, int delay_free_irq)
  214. {
  215. unsigned long flags;
  216. if(!chan->opened)
  217. return;
  218. if(delay_free_irq){
  219. spin_lock_irqsave(&irqs_to_free_lock, flags);
  220. list_add(&chan->free_list, &irqs_to_free);
  221. spin_unlock_irqrestore(&irqs_to_free_lock, flags);
  222. }
  223. else {
  224. if(chan->input)
  225. free_irq(chan->line->driver->read_irq, chan);
  226. if(chan->output)
  227. free_irq(chan->line->driver->write_irq, chan);
  228. chan->enabled = 0;
  229. }
  230. if(chan->ops->close != NULL)
  231. (*chan->ops->close)(chan->fd, chan->data);
  232. chan->opened = 0;
  233. chan->fd = -1;
  234. }
  235. void close_chan(struct list_head *chans, int delay_free_irq)
  236. {
  237. struct chan *chan;
  238. /* Close in reverse order as open in case more than one of them
  239. * refers to the same device and they save and restore that device's
  240. * state. Then, the first one opened will have the original state,
  241. * so it must be the last closed.
  242. */
  243. list_for_each_entry_reverse(chan, chans, list) {
  244. close_one_chan(chan, delay_free_irq);
  245. }
  246. }
  247. void deactivate_chan(struct list_head *chans, int irq)
  248. {
  249. struct list_head *ele;
  250. struct chan *chan;
  251. list_for_each(ele, chans) {
  252. chan = list_entry(ele, struct chan, list);
  253. if(chan->enabled && chan->input)
  254. deactivate_fd(chan->fd, irq);
  255. }
  256. }
  257. void reactivate_chan(struct list_head *chans, int irq)
  258. {
  259. struct list_head *ele;
  260. struct chan *chan;
  261. list_for_each(ele, chans) {
  262. chan = list_entry(ele, struct chan, list);
  263. if(chan->enabled && chan->input)
  264. reactivate_fd(chan->fd, irq);
  265. }
  266. }
  267. int write_chan(struct list_head *chans, const char *buf, int len,
  268. int write_irq)
  269. {
  270. struct list_head *ele;
  271. struct chan *chan = NULL;
  272. int n, ret = 0;
  273. list_for_each(ele, chans) {
  274. chan = list_entry(ele, struct chan, list);
  275. if (!chan->output || (chan->ops->write == NULL))
  276. continue;
  277. n = chan->ops->write(chan->fd, buf, len, chan->data);
  278. if (chan->primary) {
  279. ret = n;
  280. if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
  281. reactivate_fd(chan->fd, write_irq);
  282. }
  283. }
  284. return ret;
  285. }
  286. int console_write_chan(struct list_head *chans, const char *buf, int len)
  287. {
  288. struct list_head *ele;
  289. struct chan *chan;
  290. int n, ret = 0;
  291. list_for_each(ele, chans){
  292. chan = list_entry(ele, struct chan, list);
  293. if(!chan->output || (chan->ops->console_write == NULL))
  294. continue;
  295. n = chan->ops->console_write(chan->fd, buf, len);
  296. if(chan->primary) ret = n;
  297. }
  298. return ret;
  299. }
  300. int console_open_chan(struct line *line, struct console *co)
  301. {
  302. int err;
  303. err = open_chan(&line->chan_list);
  304. if(err)
  305. return err;
  306. printk("Console initialized on /dev/%s%d\n", co->name, co->index);
  307. return 0;
  308. }
  309. int chan_window_size(struct list_head *chans, unsigned short *rows_out,
  310. unsigned short *cols_out)
  311. {
  312. struct list_head *ele;
  313. struct chan *chan;
  314. list_for_each(ele, chans){
  315. chan = list_entry(ele, struct chan, list);
  316. if(chan->primary){
  317. if(chan->ops->window_size == NULL)
  318. return 0;
  319. return chan->ops->window_size(chan->fd, chan->data,
  320. rows_out, cols_out);
  321. }
  322. }
  323. return 0;
  324. }
  325. static void free_one_chan(struct chan *chan, int delay_free_irq)
  326. {
  327. list_del(&chan->list);
  328. close_one_chan(chan, delay_free_irq);
  329. if(chan->ops->free != NULL)
  330. (*chan->ops->free)(chan->data);
  331. if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
  332. kfree(chan);
  333. }
  334. static void free_chan(struct list_head *chans, int delay_free_irq)
  335. {
  336. struct list_head *ele, *next;
  337. struct chan *chan;
  338. list_for_each_safe(ele, next, chans){
  339. chan = list_entry(ele, struct chan, list);
  340. free_one_chan(chan, delay_free_irq);
  341. }
  342. }
  343. static int one_chan_config_string(struct chan *chan, char *str, int size,
  344. char **error_out)
  345. {
  346. int n = 0;
  347. if(chan == NULL){
  348. CONFIG_CHUNK(str, size, n, "none", 1);
  349. return n;
  350. }
  351. CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
  352. if(chan->dev == NULL){
  353. CONFIG_CHUNK(str, size, n, "", 1);
  354. return n;
  355. }
  356. CONFIG_CHUNK(str, size, n, ":", 0);
  357. CONFIG_CHUNK(str, size, n, chan->dev, 0);
  358. return n;
  359. }
  360. static int chan_pair_config_string(struct chan *in, struct chan *out,
  361. char *str, int size, char **error_out)
  362. {
  363. int n;
  364. n = one_chan_config_string(in, str, size, error_out);
  365. str += n;
  366. size -= n;
  367. if(in == out){
  368. CONFIG_CHUNK(str, size, n, "", 1);
  369. return n;
  370. }
  371. CONFIG_CHUNK(str, size, n, ",", 1);
  372. n = one_chan_config_string(out, str, size, error_out);
  373. str += n;
  374. size -= n;
  375. CONFIG_CHUNK(str, size, n, "", 1);
  376. return n;
  377. }
  378. int chan_config_string(struct list_head *chans, char *str, int size,
  379. char **error_out)
  380. {
  381. struct list_head *ele;
  382. struct chan *chan, *in = NULL, *out = NULL;
  383. list_for_each(ele, chans){
  384. chan = list_entry(ele, struct chan, list);
  385. if(!chan->primary)
  386. continue;
  387. if(chan->input)
  388. in = chan;
  389. if(chan->output)
  390. out = chan;
  391. }
  392. return chan_pair_config_string(in, out, str, size, error_out);
  393. }
  394. struct chan_type {
  395. char *key;
  396. const struct chan_ops *ops;
  397. };
  398. static const struct chan_type chan_table[] = {
  399. { "fd", &fd_ops },
  400. #ifdef CONFIG_NULL_CHAN
  401. { "null", &null_ops },
  402. #else
  403. { "null", &not_configged_ops },
  404. #endif
  405. #ifdef CONFIG_PORT_CHAN
  406. { "port", &port_ops },
  407. #else
  408. { "port", &not_configged_ops },
  409. #endif
  410. #ifdef CONFIG_PTY_CHAN
  411. { "pty", &pty_ops },
  412. { "pts", &pts_ops },
  413. #else
  414. { "pty", &not_configged_ops },
  415. { "pts", &not_configged_ops },
  416. #endif
  417. #ifdef CONFIG_TTY_CHAN
  418. { "tty", &tty_ops },
  419. #else
  420. { "tty", &not_configged_ops },
  421. #endif
  422. #ifdef CONFIG_XTERM_CHAN
  423. { "xterm", &xterm_ops },
  424. #else
  425. { "xterm", &not_configged_ops },
  426. #endif
  427. };
  428. static struct chan *parse_chan(struct line *line, char *str, int device,
  429. const struct chan_opts *opts, char **error_out)
  430. {
  431. const struct chan_type *entry;
  432. const struct chan_ops *ops;
  433. struct chan *chan;
  434. void *data;
  435. int i;
  436. ops = NULL;
  437. data = NULL;
  438. for(i = 0; i < ARRAY_SIZE(chan_table); i++){
  439. entry = &chan_table[i];
  440. if(!strncmp(str, entry->key, strlen(entry->key))){
  441. ops = entry->ops;
  442. str += strlen(entry->key);
  443. break;
  444. }
  445. }
  446. if(ops == NULL){
  447. *error_out = "No match for configured backends";
  448. return NULL;
  449. }
  450. data = (*ops->init)(str, device, opts);
  451. if(data == NULL){
  452. *error_out = "Configuration failed";
  453. return NULL;
  454. }
  455. chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
  456. if(chan == NULL){
  457. *error_out = "Memory allocation failed";
  458. return NULL;
  459. }
  460. *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
  461. .free_list =
  462. LIST_HEAD_INIT(chan->free_list),
  463. .line = line,
  464. .primary = 1,
  465. .input = 0,
  466. .output = 0,
  467. .opened = 0,
  468. .enabled = 0,
  469. .fd = -1,
  470. .ops = ops,
  471. .data = data });
  472. return chan;
  473. }
  474. int parse_chan_pair(char *str, struct line *line, int device,
  475. const struct chan_opts *opts, char **error_out)
  476. {
  477. struct list_head *chans = &line->chan_list;
  478. struct chan *new, *chan;
  479. char *in, *out;
  480. if(!list_empty(chans)){
  481. chan = list_entry(chans->next, struct chan, list);
  482. free_chan(chans, 0);
  483. INIT_LIST_HEAD(chans);
  484. }
  485. out = strchr(str, ',');
  486. if(out != NULL){
  487. in = str;
  488. *out = '\0';
  489. out++;
  490. new = parse_chan(line, in, device, opts, error_out);
  491. if(new == NULL)
  492. return -1;
  493. new->input = 1;
  494. list_add(&new->list, chans);
  495. new = parse_chan(line, out, device, opts, error_out);
  496. if(new == NULL)
  497. return -1;
  498. list_add(&new->list, chans);
  499. new->output = 1;
  500. }
  501. else {
  502. new = parse_chan(line, str, device, opts, error_out);
  503. if(new == NULL)
  504. return -1;
  505. list_add(&new->list, chans);
  506. new->input = 1;
  507. new->output = 1;
  508. }
  509. return 0;
  510. }
  511. int chan_out_fd(struct list_head *chans)
  512. {
  513. struct list_head *ele;
  514. struct chan *chan;
  515. list_for_each(ele, chans){
  516. chan = list_entry(ele, struct chan, list);
  517. if(chan->primary && chan->output)
  518. return chan->fd;
  519. }
  520. return -1;
  521. }
  522. void chan_interrupt(struct list_head *chans, struct delayed_work *task,
  523. struct tty_struct *tty, int irq)
  524. {
  525. struct list_head *ele, *next;
  526. struct chan *chan;
  527. int err;
  528. char c;
  529. list_for_each_safe(ele, next, chans){
  530. chan = list_entry(ele, struct chan, list);
  531. if(!chan->input || (chan->ops->read == NULL)) continue;
  532. do {
  533. if (tty && !tty_buffer_request_room(tty, 1)) {
  534. schedule_delayed_work(task, 1);
  535. goto out;
  536. }
  537. err = chan->ops->read(chan->fd, &c, chan->data);
  538. if(err > 0)
  539. tty_receive_char(tty, c);
  540. } while(err > 0);
  541. if(err == 0) reactivate_fd(chan->fd, irq);
  542. if(err == -EIO){
  543. if(chan->primary){
  544. if(tty != NULL)
  545. tty_hangup(tty);
  546. close_chan(chans, 1);
  547. return;
  548. }
  549. else close_one_chan(chan, 1);
  550. }
  551. }
  552. out:
  553. if(tty) tty_flip_buffer_push(tty);
  554. }