chan_kern.c 13 KB

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