chan_kern.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. #ifdef CONFIG_NOCONFIG_CHAN
  21. static void *not_configged_init(char *str, int device, struct chan_opts *opts)
  22. {
  23. printk(KERN_ERR "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(KERN_ERR "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(KERN_ERR "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(KERN_ERR "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(KERN_ERR "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. void *data)
  53. {
  54. printk(KERN_ERR "Using a channel type which is configured out of "
  55. "UML\n");
  56. return(-EIO);
  57. }
  58. static int not_configged_window_size(int fd, void *data, unsigned short *rows,
  59. unsigned short *cols)
  60. {
  61. printk(KERN_ERR "Using a channel type which is configured out of "
  62. "UML\n");
  63. return(-ENODEV);
  64. }
  65. static void not_configged_free(void *data)
  66. {
  67. printk(KERN_ERR "Using a channel type which is configured out of "
  68. "UML\n");
  69. }
  70. static struct chan_ops not_configged_ops = {
  71. .init = not_configged_init,
  72. .open = not_configged_open,
  73. .close = not_configged_close,
  74. .read = not_configged_read,
  75. .write = not_configged_write,
  76. .console_write = not_configged_console_write,
  77. .window_size = not_configged_window_size,
  78. .free = not_configged_free,
  79. .winch = 0,
  80. };
  81. #endif /* CONFIG_NOCONFIG_CHAN */
  82. void generic_close(int fd, void *unused)
  83. {
  84. os_close_file(fd);
  85. }
  86. int generic_read(int fd, char *c_out, void *unused)
  87. {
  88. int n;
  89. n = os_read_file(fd, c_out, sizeof(*c_out));
  90. if(n == -EAGAIN)
  91. return(0);
  92. else if(n == 0)
  93. return(-EIO);
  94. return(n);
  95. }
  96. /* XXX Trivial wrapper around os_write_file */
  97. int generic_write(int fd, const char *buf, int n, void *unused)
  98. {
  99. return(os_write_file(fd, buf, n));
  100. }
  101. int generic_window_size(int fd, void *unused, unsigned short *rows_out,
  102. unsigned short *cols_out)
  103. {
  104. int rows, cols;
  105. int ret;
  106. ret = os_window_size(fd, &rows, &cols);
  107. if(ret < 0)
  108. return(ret);
  109. ret = ((*rows_out != rows) || (*cols_out != cols));
  110. *rows_out = rows;
  111. *cols_out = cols;
  112. return(ret);
  113. }
  114. void generic_free(void *data)
  115. {
  116. kfree(data);
  117. }
  118. static void tty_receive_char(struct tty_struct *tty, char ch)
  119. {
  120. if(tty == NULL) return;
  121. if(I_IXON(tty) && !I_IXOFF(tty) && !tty->raw) {
  122. if(ch == STOP_CHAR(tty)){
  123. stop_tty(tty);
  124. return;
  125. }
  126. else if(ch == START_CHAR(tty)){
  127. start_tty(tty);
  128. return;
  129. }
  130. }
  131. if((tty->flip.flag_buf_ptr == NULL) ||
  132. (tty->flip.char_buf_ptr == NULL))
  133. return;
  134. tty_insert_flip_char(tty, ch, TTY_NORMAL);
  135. }
  136. static int open_one_chan(struct chan *chan, int input, int output, int primary)
  137. {
  138. int fd;
  139. if(chan->opened) return(0);
  140. if(chan->ops->open == NULL) fd = 0;
  141. else fd = (*chan->ops->open)(input, output, primary, chan->data,
  142. &chan->dev);
  143. if(fd < 0) return(fd);
  144. chan->fd = fd;
  145. chan->opened = 1;
  146. return(0);
  147. }
  148. int open_chan(struct list_head *chans)
  149. {
  150. struct list_head *ele;
  151. struct chan *chan;
  152. int ret, err = 0;
  153. list_for_each(ele, chans){
  154. chan = list_entry(ele, struct chan, list);
  155. ret = open_one_chan(chan, chan->input, chan->output,
  156. chan->primary);
  157. if(chan->primary) err = ret;
  158. }
  159. return(err);
  160. }
  161. void chan_enable_winch(struct list_head *chans, struct tty_struct *tty)
  162. {
  163. struct list_head *ele;
  164. struct chan *chan;
  165. list_for_each(ele, chans){
  166. chan = list_entry(ele, struct chan, list);
  167. if(chan->primary && chan->output && chan->ops->winch){
  168. register_winch(chan->fd, tty);
  169. return;
  170. }
  171. }
  172. }
  173. void enable_chan(struct list_head *chans, struct tty_struct *tty)
  174. {
  175. struct list_head *ele;
  176. struct chan *chan;
  177. list_for_each(ele, chans){
  178. chan = list_entry(ele, struct chan, list);
  179. if(!chan->opened) continue;
  180. line_setup_irq(chan->fd, chan->input, chan->output, tty);
  181. }
  182. }
  183. void close_chan(struct list_head *chans)
  184. {
  185. struct chan *chan;
  186. /* Close in reverse order as open in case more than one of them
  187. * refers to the same device and they save and restore that device's
  188. * state. Then, the first one opened will have the original state,
  189. * so it must be the last closed.
  190. */
  191. list_for_each_entry_reverse(chan, chans, list) {
  192. if(!chan->opened) continue;
  193. if(chan->ops->close != NULL)
  194. (*chan->ops->close)(chan->fd, chan->data);
  195. chan->opened = 0;
  196. chan->fd = -1;
  197. }
  198. }
  199. int write_chan(struct list_head *chans, const char *buf, int len,
  200. int write_irq)
  201. {
  202. struct list_head *ele;
  203. struct chan *chan = NULL;
  204. int n, ret = 0;
  205. list_for_each(ele, chans) {
  206. chan = list_entry(ele, struct chan, list);
  207. if (!chan->output || (chan->ops->write == NULL))
  208. continue;
  209. n = chan->ops->write(chan->fd, buf, len, chan->data);
  210. if (chan->primary) {
  211. ret = n;
  212. if ((ret == -EAGAIN) || ((ret >= 0) && (ret < len)))
  213. reactivate_fd(chan->fd, write_irq);
  214. }
  215. }
  216. return(ret);
  217. }
  218. int console_write_chan(struct list_head *chans, const char *buf, int len)
  219. {
  220. struct list_head *ele;
  221. struct chan *chan;
  222. int n, ret = 0;
  223. list_for_each(ele, chans){
  224. chan = list_entry(ele, struct chan, list);
  225. if(!chan->output || (chan->ops->console_write == NULL))
  226. continue;
  227. n = chan->ops->console_write(chan->fd, buf, len, chan->data);
  228. if(chan->primary) ret = n;
  229. }
  230. return(ret);
  231. }
  232. int console_open_chan(struct line *line, struct console *co, struct chan_opts *opts)
  233. {
  234. if (!list_empty(&line->chan_list))
  235. return 0;
  236. if (0 != parse_chan_pair(line->init_str, &line->chan_list,
  237. line->init_pri, co->index, opts))
  238. return -1;
  239. if (0 != open_chan(&line->chan_list))
  240. return -1;
  241. printk("Console initialized on /dev/%s%d\n",co->name,co->index);
  242. return 0;
  243. }
  244. int chan_window_size(struct list_head *chans, unsigned short *rows_out,
  245. unsigned short *cols_out)
  246. {
  247. struct list_head *ele;
  248. struct chan *chan;
  249. list_for_each(ele, chans){
  250. chan = list_entry(ele, struct chan, list);
  251. if(chan->primary){
  252. if(chan->ops->window_size == NULL) return(0);
  253. return(chan->ops->window_size(chan->fd, chan->data,
  254. rows_out, cols_out));
  255. }
  256. }
  257. return(0);
  258. }
  259. void free_one_chan(struct chan *chan)
  260. {
  261. list_del(&chan->list);
  262. if(chan->ops->free != NULL)
  263. (*chan->ops->free)(chan->data);
  264. free_irq_by_fd(chan->fd);
  265. if(chan->primary && chan->output) ignore_sigio_fd(chan->fd);
  266. kfree(chan);
  267. }
  268. void free_chan(struct list_head *chans)
  269. {
  270. struct list_head *ele, *next;
  271. struct chan *chan;
  272. list_for_each_safe(ele, next, chans){
  273. chan = list_entry(ele, struct chan, list);
  274. free_one_chan(chan);
  275. }
  276. }
  277. static int one_chan_config_string(struct chan *chan, char *str, int size,
  278. char **error_out)
  279. {
  280. int n = 0;
  281. if(chan == NULL){
  282. CONFIG_CHUNK(str, size, n, "none", 1);
  283. return(n);
  284. }
  285. CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
  286. if(chan->dev == NULL){
  287. CONFIG_CHUNK(str, size, n, "", 1);
  288. return(n);
  289. }
  290. CONFIG_CHUNK(str, size, n, ":", 0);
  291. CONFIG_CHUNK(str, size, n, chan->dev, 0);
  292. return(n);
  293. }
  294. static int chan_pair_config_string(struct chan *in, struct chan *out,
  295. char *str, int size, char **error_out)
  296. {
  297. int n;
  298. n = one_chan_config_string(in, str, size, error_out);
  299. str += n;
  300. size -= n;
  301. if(in == out){
  302. CONFIG_CHUNK(str, size, n, "", 1);
  303. return(n);
  304. }
  305. CONFIG_CHUNK(str, size, n, ",", 1);
  306. n = one_chan_config_string(out, str, size, error_out);
  307. str += n;
  308. size -= n;
  309. CONFIG_CHUNK(str, size, n, "", 1);
  310. return(n);
  311. }
  312. int chan_config_string(struct list_head *chans, char *str, int size,
  313. char **error_out)
  314. {
  315. struct list_head *ele;
  316. struct chan *chan, *in = NULL, *out = NULL;
  317. list_for_each(ele, chans){
  318. chan = list_entry(ele, struct chan, list);
  319. if(!chan->primary)
  320. continue;
  321. if(chan->input)
  322. in = chan;
  323. if(chan->output)
  324. out = chan;
  325. }
  326. return(chan_pair_config_string(in, out, str, size, error_out));
  327. }
  328. struct chan_type {
  329. char *key;
  330. struct chan_ops *ops;
  331. };
  332. struct chan_type chan_table[] = {
  333. { "fd", &fd_ops },
  334. #ifdef CONFIG_NULL_CHAN
  335. { "null", &null_ops },
  336. #else
  337. { "null", &not_configged_ops },
  338. #endif
  339. #ifdef CONFIG_PORT_CHAN
  340. { "port", &port_ops },
  341. #else
  342. { "port", &not_configged_ops },
  343. #endif
  344. #ifdef CONFIG_PTY_CHAN
  345. { "pty", &pty_ops },
  346. { "pts", &pts_ops },
  347. #else
  348. { "pty", &not_configged_ops },
  349. { "pts", &not_configged_ops },
  350. #endif
  351. #ifdef CONFIG_TTY_CHAN
  352. { "tty", &tty_ops },
  353. #else
  354. { "tty", &not_configged_ops },
  355. #endif
  356. #ifdef CONFIG_XTERM_CHAN
  357. { "xterm", &xterm_ops },
  358. #else
  359. { "xterm", &not_configged_ops },
  360. #endif
  361. };
  362. static struct chan *parse_chan(char *str, int pri, int device,
  363. struct chan_opts *opts)
  364. {
  365. struct chan_type *entry;
  366. struct chan_ops *ops;
  367. struct chan *chan;
  368. void *data;
  369. int i;
  370. ops = NULL;
  371. data = NULL;
  372. for(i = 0; i < sizeof(chan_table)/sizeof(chan_table[0]); i++){
  373. entry = &chan_table[i];
  374. if(!strncmp(str, entry->key, strlen(entry->key))){
  375. ops = entry->ops;
  376. str += strlen(entry->key);
  377. break;
  378. }
  379. }
  380. if(ops == NULL){
  381. printk(KERN_ERR "parse_chan couldn't parse \"%s\"\n",
  382. str);
  383. return(NULL);
  384. }
  385. if(ops->init == NULL) return(NULL);
  386. data = (*ops->init)(str, device, opts);
  387. if(data == NULL) return(NULL);
  388. chan = kmalloc(sizeof(*chan), GFP_KERNEL);
  389. if(chan == NULL) return(NULL);
  390. *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
  391. .primary = 1,
  392. .input = 0,
  393. .output = 0,
  394. .opened = 0,
  395. .fd = -1,
  396. .pri = pri,
  397. .ops = ops,
  398. .data = data });
  399. return(chan);
  400. }
  401. int parse_chan_pair(char *str, struct list_head *chans, int pri, int device,
  402. struct chan_opts *opts)
  403. {
  404. struct chan *new, *chan;
  405. char *in, *out;
  406. if(!list_empty(chans)){
  407. chan = list_entry(chans->next, struct chan, list);
  408. if(chan->pri >= pri) return(0);
  409. free_chan(chans);
  410. INIT_LIST_HEAD(chans);
  411. }
  412. out = strchr(str, ',');
  413. if(out != NULL){
  414. in = str;
  415. *out = '\0';
  416. out++;
  417. new = parse_chan(in, pri, device, opts);
  418. if(new == NULL) return(-1);
  419. new->input = 1;
  420. list_add(&new->list, chans);
  421. new = parse_chan(out, pri, device, opts);
  422. if(new == NULL) return(-1);
  423. list_add(&new->list, chans);
  424. new->output = 1;
  425. }
  426. else {
  427. new = parse_chan(str, pri, device, opts);
  428. if(new == NULL) return(-1);
  429. list_add(&new->list, chans);
  430. new->input = 1;
  431. new->output = 1;
  432. }
  433. return(0);
  434. }
  435. int chan_out_fd(struct list_head *chans)
  436. {
  437. struct list_head *ele;
  438. struct chan *chan;
  439. list_for_each(ele, chans){
  440. chan = list_entry(ele, struct chan, list);
  441. if(chan->primary && chan->output)
  442. return(chan->fd);
  443. }
  444. return(-1);
  445. }
  446. void chan_interrupt(struct list_head *chans, struct work_struct *task,
  447. struct tty_struct *tty, int irq)
  448. {
  449. struct list_head *ele, *next;
  450. struct chan *chan;
  451. int err;
  452. char c;
  453. list_for_each_safe(ele, next, chans){
  454. chan = list_entry(ele, struct chan, list);
  455. if(!chan->input || (chan->ops->read == NULL)) continue;
  456. do {
  457. if((tty != NULL) &&
  458. (tty->flip.count >= TTY_FLIPBUF_SIZE)){
  459. schedule_work(task);
  460. goto out;
  461. }
  462. err = chan->ops->read(chan->fd, &c, chan->data);
  463. if(err > 0)
  464. tty_receive_char(tty, c);
  465. } while(err > 0);
  466. if(err == 0) reactivate_fd(chan->fd, irq);
  467. if(err == -EIO){
  468. if(chan->primary){
  469. if(tty != NULL)
  470. tty_hangup(tty);
  471. line_disable(tty, irq);
  472. close_chan(chans);
  473. free_chan(chans);
  474. return;
  475. }
  476. else {
  477. if(chan->ops->close != NULL)
  478. chan->ops->close(chan->fd, chan->data);
  479. free_one_chan(chan);
  480. }
  481. }
  482. }
  483. out:
  484. if(tty) tty_flip_buffer_push(tty);
  485. }
  486. /*
  487. * Overrides for Emacs so that we follow Linus's tabbing style.
  488. * Emacs will notice this stuff at the end of the file and automatically
  489. * adjust the settings for this buffer only. This must remain at the end
  490. * of the file.
  491. * ---------------------------------------------------------------------------
  492. * Local variables:
  493. * c-file-style: "linux"
  494. * End:
  495. */