chan_kern.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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, err;
  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. err = os_set_fd_block(fd, 0);
  144. if (err) {
  145. (*chan->ops->close)(fd, chan->data);
  146. return err;
  147. }
  148. chan->fd = fd;
  149. chan->opened = 1;
  150. return 0;
  151. }
  152. int open_chan(struct list_head *chans)
  153. {
  154. struct list_head *ele;
  155. struct chan *chan;
  156. int ret, err = 0;
  157. list_for_each(ele, chans){
  158. chan = list_entry(ele, struct chan, list);
  159. ret = open_one_chan(chan);
  160. if(chan->primary)
  161. err = ret;
  162. }
  163. return err;
  164. }
  165. void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
  166. {
  167. struct list_head *ele;
  168. struct chan *chan;
  169. list_for_each(ele, chans){
  170. chan = list_entry(ele, struct chan, list);
  171. if(chan->primary && chan->output && chan->ops->winch){
  172. register_winch(chan->fd, tty);
  173. return;
  174. }
  175. }
  176. }
  177. int enable_chan(struct line *line)
  178. {
  179. struct list_head *ele;
  180. struct chan *chan;
  181. int err;
  182. list_for_each(ele, &line->chan_list){
  183. chan = list_entry(ele, struct chan, list);
  184. err = open_one_chan(chan);
  185. if (err) {
  186. if (chan->primary)
  187. goto out_close;
  188. continue;
  189. }
  190. if(chan->enabled)
  191. continue;
  192. err = line_setup_irq(chan->fd, chan->input, chan->output, line,
  193. chan);
  194. if (err)
  195. goto out_close;
  196. chan->enabled = 1;
  197. }
  198. return 0;
  199. out_close:
  200. close_chan(&line->chan_list, 0);
  201. return err;
  202. }
  203. /* Items are added in IRQ context, when free_irq can't be called, and
  204. * removed in process context, when it can.
  205. * This handles interrupt sources which disappear, and which need to
  206. * be permanently disabled. This is discovered in IRQ context, but
  207. * the freeing of the IRQ must be done later.
  208. */
  209. static DEFINE_SPINLOCK(irqs_to_free_lock);
  210. static LIST_HEAD(irqs_to_free);
  211. void free_irqs(void)
  212. {
  213. struct chan *chan;
  214. LIST_HEAD(list);
  215. struct list_head *ele;
  216. unsigned long flags;
  217. spin_lock_irqsave(&irqs_to_free_lock, flags);
  218. list_splice_init(&irqs_to_free, &list);
  219. spin_unlock_irqrestore(&irqs_to_free_lock, flags);
  220. list_for_each(ele, &list){
  221. chan = list_entry(ele, struct chan, free_list);
  222. if(chan->input)
  223. free_irq(chan->line->driver->read_irq, chan);
  224. if(chan->output)
  225. free_irq(chan->line->driver->write_irq, chan);
  226. chan->enabled = 0;
  227. }
  228. }
  229. static void close_one_chan(struct chan *chan, int delay_free_irq)
  230. {
  231. unsigned long flags;
  232. if(!chan->opened)
  233. return;
  234. if(delay_free_irq){
  235. spin_lock_irqsave(&irqs_to_free_lock, flags);
  236. list_add(&chan->free_list, &irqs_to_free);
  237. spin_unlock_irqrestore(&irqs_to_free_lock, flags);
  238. }
  239. else {
  240. if(chan->input)
  241. free_irq(chan->line->driver->read_irq, chan);
  242. if(chan->output)
  243. free_irq(chan->line->driver->write_irq, chan);
  244. chan->enabled = 0;
  245. }
  246. if(chan->ops->close != NULL)
  247. (*chan->ops->close)(chan->fd, chan->data);
  248. chan->opened = 0;
  249. chan->fd = -1;
  250. }
  251. void close_chan(struct list_head *chans, int delay_free_irq)
  252. {
  253. struct chan *chan;
  254. /* Close in reverse order as open in case more than one of them
  255. * refers to the same device and they save and restore that device's
  256. * state. Then, the first one opened will have the original state,
  257. * so it must be the last closed.
  258. */
  259. list_for_each_entry_reverse(chan, chans, list) {
  260. close_one_chan(chan, delay_free_irq);
  261. }
  262. }
  263. void deactivate_chan(struct list_head *chans, int irq)
  264. {
  265. struct list_head *ele;
  266. struct chan *chan;
  267. list_for_each(ele, chans) {
  268. chan = list_entry(ele, struct chan, list);
  269. if(chan->enabled && chan->input)
  270. deactivate_fd(chan->fd, irq);
  271. }
  272. }
  273. void reactivate_chan(struct list_head *chans, int irq)
  274. {
  275. struct list_head *ele;
  276. struct chan *chan;
  277. list_for_each(ele, chans) {
  278. chan = list_entry(ele, struct chan, list);
  279. if(chan->enabled && chan->input)
  280. reactivate_fd(chan->fd, irq);
  281. }
  282. }
  283. int write_chan(struct list_head *chans, const char *buf, int len,
  284. int write_irq)
  285. {
  286. struct list_head *ele;
  287. struct chan *chan = NULL;
  288. int n, ret = 0;
  289. list_for_each(ele, chans) {
  290. chan = list_entry(ele, struct chan, list);
  291. if (!chan->output || (chan->ops->write == NULL))
  292. continue;
  293. n = chan->ops->write(chan->fd, buf, len, chan->data);
  294. if (chan->primary) {
  295. ret = n;
  296. if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
  297. reactivate_fd(chan->fd, write_irq);
  298. }
  299. }
  300. return ret;
  301. }
  302. int console_write_chan(struct list_head *chans, const char *buf, int len)
  303. {
  304. struct list_head *ele;
  305. struct chan *chan;
  306. int n, ret = 0;
  307. list_for_each(ele, chans){
  308. chan = list_entry(ele, struct chan, list);
  309. if(!chan->output || (chan->ops->console_write == NULL))
  310. continue;
  311. n = chan->ops->console_write(chan->fd, buf, len);
  312. if(chan->primary) ret = n;
  313. }
  314. return ret;
  315. }
  316. int console_open_chan(struct line *line, struct console *co)
  317. {
  318. int err;
  319. err = open_chan(&line->chan_list);
  320. if(err)
  321. return err;
  322. printk("Console initialized on /dev/%s%d\n", co->name, co->index);
  323. return 0;
  324. }
  325. int chan_window_size(struct list_head *chans, unsigned short *rows_out,
  326. unsigned short *cols_out)
  327. {
  328. struct list_head *ele;
  329. struct chan *chan;
  330. list_for_each(ele, chans){
  331. chan = list_entry(ele, struct chan, list);
  332. if(chan->primary){
  333. if(chan->ops->window_size == NULL)
  334. return 0;
  335. return chan->ops->window_size(chan->fd, chan->data,
  336. rows_out, cols_out);
  337. }
  338. }
  339. return 0;
  340. }
  341. static void free_one_chan(struct chan *chan, int delay_free_irq)
  342. {
  343. list_del(&chan->list);
  344. close_one_chan(chan, delay_free_irq);
  345. if(chan->ops->free != NULL)
  346. (*chan->ops->free)(chan->data);
  347. if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
  348. kfree(chan);
  349. }
  350. static void free_chan(struct list_head *chans, int delay_free_irq)
  351. {
  352. struct list_head *ele, *next;
  353. struct chan *chan;
  354. list_for_each_safe(ele, next, chans){
  355. chan = list_entry(ele, struct chan, list);
  356. free_one_chan(chan, delay_free_irq);
  357. }
  358. }
  359. static int one_chan_config_string(struct chan *chan, char *str, int size,
  360. char **error_out)
  361. {
  362. int n = 0;
  363. if(chan == NULL){
  364. CONFIG_CHUNK(str, size, n, "none", 1);
  365. return n;
  366. }
  367. CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
  368. if(chan->dev == NULL){
  369. CONFIG_CHUNK(str, size, n, "", 1);
  370. return n;
  371. }
  372. CONFIG_CHUNK(str, size, n, ":", 0);
  373. CONFIG_CHUNK(str, size, n, chan->dev, 0);
  374. return n;
  375. }
  376. static int chan_pair_config_string(struct chan *in, struct chan *out,
  377. char *str, int size, char **error_out)
  378. {
  379. int n;
  380. n = one_chan_config_string(in, str, size, error_out);
  381. str += n;
  382. size -= n;
  383. if(in == out){
  384. CONFIG_CHUNK(str, size, n, "", 1);
  385. return n;
  386. }
  387. CONFIG_CHUNK(str, size, n, ",", 1);
  388. n = one_chan_config_string(out, str, size, error_out);
  389. str += n;
  390. size -= n;
  391. CONFIG_CHUNK(str, size, n, "", 1);
  392. return n;
  393. }
  394. int chan_config_string(struct list_head *chans, char *str, int size,
  395. char **error_out)
  396. {
  397. struct list_head *ele;
  398. struct chan *chan, *in = NULL, *out = NULL;
  399. list_for_each(ele, chans){
  400. chan = list_entry(ele, struct chan, list);
  401. if(!chan->primary)
  402. continue;
  403. if(chan->input)
  404. in = chan;
  405. if(chan->output)
  406. out = chan;
  407. }
  408. return chan_pair_config_string(in, out, str, size, error_out);
  409. }
  410. struct chan_type {
  411. char *key;
  412. const struct chan_ops *ops;
  413. };
  414. static const struct chan_type chan_table[] = {
  415. { "fd", &fd_ops },
  416. #ifdef CONFIG_NULL_CHAN
  417. { "null", &null_ops },
  418. #else
  419. { "null", &not_configged_ops },
  420. #endif
  421. #ifdef CONFIG_PORT_CHAN
  422. { "port", &port_ops },
  423. #else
  424. { "port", &not_configged_ops },
  425. #endif
  426. #ifdef CONFIG_PTY_CHAN
  427. { "pty", &pty_ops },
  428. { "pts", &pts_ops },
  429. #else
  430. { "pty", &not_configged_ops },
  431. { "pts", &not_configged_ops },
  432. #endif
  433. #ifdef CONFIG_TTY_CHAN
  434. { "tty", &tty_ops },
  435. #else
  436. { "tty", &not_configged_ops },
  437. #endif
  438. #ifdef CONFIG_XTERM_CHAN
  439. { "xterm", &xterm_ops },
  440. #else
  441. { "xterm", &not_configged_ops },
  442. #endif
  443. };
  444. static struct chan *parse_chan(struct line *line, char *str, int device,
  445. const struct chan_opts *opts, char **error_out)
  446. {
  447. const struct chan_type *entry;
  448. const struct chan_ops *ops;
  449. struct chan *chan;
  450. void *data;
  451. int i;
  452. ops = NULL;
  453. data = NULL;
  454. for(i = 0; i < ARRAY_SIZE(chan_table); i++){
  455. entry = &chan_table[i];
  456. if(!strncmp(str, entry->key, strlen(entry->key))){
  457. ops = entry->ops;
  458. str += strlen(entry->key);
  459. break;
  460. }
  461. }
  462. if(ops == NULL){
  463. *error_out = "No match for configured backends";
  464. return NULL;
  465. }
  466. data = (*ops->init)(str, device, opts);
  467. if(data == NULL){
  468. *error_out = "Configuration failed";
  469. return NULL;
  470. }
  471. chan = kmalloc(sizeof(*chan), GFP_ATOMIC);
  472. if(chan == NULL){
  473. *error_out = "Memory allocation failed";
  474. return NULL;
  475. }
  476. *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
  477. .free_list =
  478. LIST_HEAD_INIT(chan->free_list),
  479. .line = line,
  480. .primary = 1,
  481. .input = 0,
  482. .output = 0,
  483. .opened = 0,
  484. .enabled = 0,
  485. .fd = -1,
  486. .ops = ops,
  487. .data = data });
  488. return chan;
  489. }
  490. int parse_chan_pair(char *str, struct line *line, int device,
  491. const struct chan_opts *opts, char **error_out)
  492. {
  493. struct list_head *chans = &line->chan_list;
  494. struct chan *new, *chan;
  495. char *in, *out;
  496. if(!list_empty(chans)){
  497. chan = list_entry(chans->next, struct chan, list);
  498. free_chan(chans, 0);
  499. INIT_LIST_HEAD(chans);
  500. }
  501. out = strchr(str, ',');
  502. if(out != NULL){
  503. in = str;
  504. *out = '\0';
  505. out++;
  506. new = parse_chan(line, in, device, opts, error_out);
  507. if(new == NULL)
  508. return -1;
  509. new->input = 1;
  510. list_add(&new->list, chans);
  511. new = parse_chan(line, out, device, opts, error_out);
  512. if(new == NULL)
  513. return -1;
  514. list_add(&new->list, chans);
  515. new->output = 1;
  516. }
  517. else {
  518. new = parse_chan(line, str, device, opts, error_out);
  519. if(new == NULL)
  520. return -1;
  521. list_add(&new->list, chans);
  522. new->input = 1;
  523. new->output = 1;
  524. }
  525. return 0;
  526. }
  527. int chan_out_fd(struct list_head *chans)
  528. {
  529. struct list_head *ele;
  530. struct chan *chan;
  531. list_for_each(ele, chans){
  532. chan = list_entry(ele, struct chan, list);
  533. if(chan->primary && chan->output)
  534. return chan->fd;
  535. }
  536. return -1;
  537. }
  538. void chan_interrupt(struct list_head *chans, struct delayed_work *task,
  539. struct tty_struct *tty, int irq)
  540. {
  541. struct list_head *ele, *next;
  542. struct chan *chan;
  543. int err;
  544. char c;
  545. list_for_each_safe(ele, next, chans){
  546. chan = list_entry(ele, struct chan, list);
  547. if(!chan->input || (chan->ops->read == NULL)) continue;
  548. do {
  549. if (tty && !tty_buffer_request_room(tty, 1)) {
  550. schedule_delayed_work(task, 1);
  551. goto out;
  552. }
  553. err = chan->ops->read(chan->fd, &c, chan->data);
  554. if(err > 0)
  555. tty_receive_char(tty, c);
  556. } while(err > 0);
  557. if(err == 0) reactivate_fd(chan->fd, irq);
  558. if(err == -EIO){
  559. if(chan->primary){
  560. if(tty != NULL)
  561. tty_hangup(tty);
  562. close_chan(chans, 1);
  563. return;
  564. }
  565. else close_one_chan(chan, 1);
  566. }
  567. }
  568. out:
  569. if(tty) tty_flip_buffer_push(tty);
  570. }