chan_kern.c 14 KB

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