chan_kern.c 14 KB

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