/arch/um/drivers/line.c

https://bitbucket.org/evzijst/gittest · C · 681 lines · 569 code · 88 blank · 24 comment · 133 complexity · 38fc33a086b1115e9631fa1e81003820 MD5 · raw file

  1. /*
  2. * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/sched.h"
  6. #include "linux/slab.h"
  7. #include "linux/list.h"
  8. #include "linux/kd.h"
  9. #include "linux/interrupt.h"
  10. #include "linux/devfs_fs_kernel.h"
  11. #include "asm/uaccess.h"
  12. #include "chan_kern.h"
  13. #include "irq_user.h"
  14. #include "line.h"
  15. #include "kern.h"
  16. #include "user_util.h"
  17. #include "kern_util.h"
  18. #include "os.h"
  19. #include "irq_kern.h"
  20. #define LINE_BUFSIZE 4096
  21. static irqreturn_t line_interrupt(int irq, void *data, struct pt_regs *unused)
  22. {
  23. struct tty_struct *tty = data;
  24. struct line *line = tty->driver_data;
  25. if (line)
  26. chan_interrupt(&line->chan_list, &line->task, tty, irq);
  27. return IRQ_HANDLED;
  28. }
  29. static void line_timer_cb(void *arg)
  30. {
  31. struct tty_struct *tty = arg;
  32. struct line *line = tty->driver_data;
  33. line_interrupt(line->driver->read_irq, arg, NULL);
  34. }
  35. static int write_room(struct line *dev)
  36. {
  37. int n;
  38. if (dev->buffer == NULL)
  39. return (LINE_BUFSIZE - 1);
  40. n = dev->head - dev->tail;
  41. if (n <= 0)
  42. n = LINE_BUFSIZE + n;
  43. return (n - 1);
  44. }
  45. static int buffer_data(struct line *line, const char *buf, int len)
  46. {
  47. int end, room;
  48. if(line->buffer == NULL){
  49. line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
  50. if (line->buffer == NULL) {
  51. printk("buffer_data - atomic allocation failed\n");
  52. return(0);
  53. }
  54. line->head = line->buffer;
  55. line->tail = line->buffer;
  56. }
  57. room = write_room(line);
  58. len = (len > room) ? room : len;
  59. end = line->buffer + LINE_BUFSIZE - line->tail;
  60. if(len < end){
  61. memcpy(line->tail, buf, len);
  62. line->tail += len;
  63. }
  64. else {
  65. memcpy(line->tail, buf, end);
  66. buf += end;
  67. memcpy(line->buffer, buf, len - end);
  68. line->tail = line->buffer + len - end;
  69. }
  70. return(len);
  71. }
  72. static int flush_buffer(struct line *line)
  73. {
  74. int n, count;
  75. if ((line->buffer == NULL) || (line->head == line->tail))
  76. return(1);
  77. if (line->tail < line->head) {
  78. count = line->buffer + LINE_BUFSIZE - line->head;
  79. n = write_chan(&line->chan_list, line->head, count,
  80. line->driver->write_irq);
  81. if (n < 0)
  82. return(n);
  83. if (n == count)
  84. line->head = line->buffer;
  85. else {
  86. line->head += n;
  87. return(0);
  88. }
  89. }
  90. count = line->tail - line->head;
  91. n = write_chan(&line->chan_list, line->head, count,
  92. line->driver->write_irq);
  93. if(n < 0) return(n);
  94. line->head += n;
  95. return(line->head == line->tail);
  96. }
  97. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  98. {
  99. struct line *line = tty->driver_data;
  100. unsigned long flags;
  101. int n, err, ret = 0;
  102. if(tty->stopped) return 0;
  103. down(&line->sem);
  104. if(line->head != line->tail){
  105. local_irq_save(flags);
  106. ret = buffer_data(line, buf, len);
  107. err = flush_buffer(line);
  108. local_irq_restore(flags);
  109. if(err <= 0 && (err != -EAGAIN || !ret))
  110. ret = err;
  111. }
  112. else {
  113. n = write_chan(&line->chan_list, buf, len,
  114. line->driver->write_irq);
  115. if(n < 0){
  116. ret = n;
  117. goto out_up;
  118. }
  119. len -= n;
  120. ret += n;
  121. if(len > 0)
  122. ret += buffer_data(line, buf + n, len);
  123. }
  124. out_up:
  125. up(&line->sem);
  126. return(ret);
  127. }
  128. void line_put_char(struct tty_struct *tty, unsigned char ch)
  129. {
  130. line_write(tty, &ch, sizeof(ch));
  131. }
  132. void line_set_termios(struct tty_struct *tty, struct termios * old)
  133. {
  134. /* nothing */
  135. }
  136. int line_chars_in_buffer(struct tty_struct *tty)
  137. {
  138. return 0;
  139. }
  140. static struct {
  141. int cmd;
  142. char *level;
  143. char *name;
  144. } tty_ioctls[] = {
  145. /* don't print these, they flood the log ... */
  146. { TCGETS, NULL, "TCGETS" },
  147. { TCSETS, NULL, "TCSETS" },
  148. { TCSETSW, NULL, "TCSETSW" },
  149. { TCFLSH, NULL, "TCFLSH" },
  150. { TCSBRK, NULL, "TCSBRK" },
  151. /* general tty stuff */
  152. { TCSETSF, KERN_DEBUG, "TCSETSF" },
  153. { TCGETA, KERN_DEBUG, "TCGETA" },
  154. { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
  155. { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
  156. { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
  157. /* linux-specific ones */
  158. { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
  159. { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
  160. { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
  161. { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
  162. };
  163. int line_ioctl(struct tty_struct *tty, struct file * file,
  164. unsigned int cmd, unsigned long arg)
  165. {
  166. int ret;
  167. int i;
  168. ret = 0;
  169. switch(cmd) {
  170. #ifdef TIOCGETP
  171. case TIOCGETP:
  172. case TIOCSETP:
  173. case TIOCSETN:
  174. #endif
  175. #ifdef TIOCGETC
  176. case TIOCGETC:
  177. case TIOCSETC:
  178. #endif
  179. #ifdef TIOCGLTC
  180. case TIOCGLTC:
  181. case TIOCSLTC:
  182. #endif
  183. case TCGETS:
  184. case TCSETSF:
  185. case TCSETSW:
  186. case TCSETS:
  187. case TCGETA:
  188. case TCSETAF:
  189. case TCSETAW:
  190. case TCSETA:
  191. case TCXONC:
  192. case TCFLSH:
  193. case TIOCOUTQ:
  194. case TIOCINQ:
  195. case TIOCGLCKTRMIOS:
  196. case TIOCSLCKTRMIOS:
  197. case TIOCPKT:
  198. case TIOCGSOFTCAR:
  199. case TIOCSSOFTCAR:
  200. return -ENOIOCTLCMD;
  201. #if 0
  202. case TCwhatever:
  203. /* do something */
  204. break;
  205. #endif
  206. default:
  207. for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
  208. if (cmd == tty_ioctls[i].cmd)
  209. break;
  210. if (i < ARRAY_SIZE(tty_ioctls)) {
  211. if (NULL != tty_ioctls[i].level)
  212. printk("%s%s: %s: ioctl %s called\n",
  213. tty_ioctls[i].level, __FUNCTION__,
  214. tty->name, tty_ioctls[i].name);
  215. } else {
  216. printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
  217. __FUNCTION__, tty->name, cmd);
  218. }
  219. ret = -ENOIOCTLCMD;
  220. break;
  221. }
  222. return(ret);
  223. }
  224. static irqreturn_t line_write_interrupt(int irq, void *data,
  225. struct pt_regs *unused)
  226. {
  227. struct tty_struct *tty = data;
  228. struct line *line = tty->driver_data;
  229. int err;
  230. err = flush_buffer(line);
  231. if(err == 0)
  232. return(IRQ_NONE);
  233. else if(err < 0){
  234. line->head = line->buffer;
  235. line->tail = line->buffer;
  236. }
  237. if(tty == NULL)
  238. return(IRQ_NONE);
  239. if(test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
  240. (tty->ldisc.write_wakeup != NULL))
  241. (tty->ldisc.write_wakeup)(tty);
  242. /* BLOCKING mode
  243. * In blocking mode, everything sleeps on tty->write_wait.
  244. * Sleeping in the console driver would break non-blocking
  245. * writes.
  246. */
  247. if(waitqueue_active(&tty->write_wait))
  248. wake_up_interruptible(&tty->write_wait);
  249. return(IRQ_HANDLED);
  250. }
  251. int line_setup_irq(int fd, int input, int output, struct tty_struct *tty)
  252. {
  253. struct line *line = tty->driver_data;
  254. struct line_driver *driver = line->driver;
  255. int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
  256. if(input) err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  257. line_interrupt, flags,
  258. driver->read_irq_name, tty);
  259. if(err) return(err);
  260. if(output) err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  261. line_write_interrupt, flags,
  262. driver->write_irq_name, tty);
  263. line->have_irq = 1;
  264. return(err);
  265. }
  266. void line_disable(struct tty_struct *tty, int current_irq)
  267. {
  268. struct line *line = tty->driver_data;
  269. if(!line->have_irq)
  270. return;
  271. if(line->driver->read_irq == current_irq)
  272. free_irq_later(line->driver->read_irq, tty);
  273. else {
  274. free_irq_by_irq_and_dev(line->driver->read_irq, tty);
  275. free_irq(line->driver->read_irq, tty);
  276. }
  277. if(line->driver->write_irq == current_irq)
  278. free_irq_later(line->driver->write_irq, tty);
  279. else {
  280. free_irq_by_irq_and_dev(line->driver->write_irq, tty);
  281. free_irq(line->driver->write_irq, tty);
  282. }
  283. line->have_irq = 0;
  284. }
  285. int line_open(struct line *lines, struct tty_struct *tty,
  286. struct chan_opts *opts)
  287. {
  288. struct line *line;
  289. int err = 0;
  290. line = &lines[tty->index];
  291. tty->driver_data = line;
  292. down(&line->sem);
  293. if (tty->count == 1) {
  294. if (!line->valid) {
  295. err = -ENODEV;
  296. goto out;
  297. }
  298. if (list_empty(&line->chan_list)) {
  299. err = parse_chan_pair(line->init_str, &line->chan_list,
  300. line->init_pri, tty->index, opts);
  301. if(err) goto out;
  302. err = open_chan(&line->chan_list);
  303. if(err) goto out;
  304. }
  305. enable_chan(&line->chan_list, tty);
  306. INIT_WORK(&line->task, line_timer_cb, tty);
  307. }
  308. if(!line->sigio){
  309. chan_enable_winch(&line->chan_list, tty);
  310. line->sigio = 1;
  311. }
  312. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  313. &tty->winsize.ws_col);
  314. line->count++;
  315. out:
  316. up(&line->sem);
  317. return(err);
  318. }
  319. void line_close(struct tty_struct *tty, struct file * filp)
  320. {
  321. struct line *line = tty->driver_data;
  322. down(&line->sem);
  323. line->count--;
  324. if (tty->count == 1) {
  325. line_disable(tty, -1);
  326. tty->driver_data = NULL;
  327. }
  328. up(&line->sem);
  329. }
  330. void close_lines(struct line *lines, int nlines)
  331. {
  332. int i;
  333. for(i = 0; i < nlines; i++)
  334. close_chan(&lines[i].chan_list);
  335. }
  336. int line_setup(struct line *lines, int num, char *init, int all_allowed)
  337. {
  338. int i, n;
  339. char *end;
  340. if(*init == '=') n = -1;
  341. else {
  342. n = simple_strtoul(init, &end, 0);
  343. if(*end != '='){
  344. printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
  345. init);
  346. return(0);
  347. }
  348. init = end;
  349. }
  350. init++;
  351. if((n >= 0) && (n >= num)){
  352. printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
  353. n, num - 1);
  354. return(0);
  355. }
  356. else if (n >= 0){
  357. if (lines[n].count > 0) {
  358. printk("line_setup - device %d is open\n", n);
  359. return(0);
  360. }
  361. if (lines[n].init_pri <= INIT_ONE){
  362. lines[n].init_pri = INIT_ONE;
  363. if (!strcmp(init, "none"))
  364. lines[n].valid = 0;
  365. else {
  366. lines[n].init_str = init;
  367. lines[n].valid = 1;
  368. }
  369. }
  370. }
  371. else if(!all_allowed){
  372. printk("line_setup - can't configure all devices from "
  373. "mconsole\n");
  374. return(0);
  375. }
  376. else {
  377. for(i = 0; i < num; i++){
  378. if(lines[i].init_pri <= INIT_ALL){
  379. lines[i].init_pri = INIT_ALL;
  380. if(!strcmp(init, "none")) lines[i].valid = 0;
  381. else {
  382. lines[i].init_str = init;
  383. lines[i].valid = 1;
  384. }
  385. }
  386. }
  387. }
  388. return(1);
  389. }
  390. int line_config(struct line *lines, int num, char *str)
  391. {
  392. char *new = uml_strdup(str);
  393. if(new == NULL){
  394. printk("line_config - uml_strdup failed\n");
  395. return(-ENOMEM);
  396. }
  397. return(!line_setup(lines, num, new, 0));
  398. }
  399. int line_get_config(char *name, struct line *lines, int num, char *str,
  400. int size, char **error_out)
  401. {
  402. struct line *line;
  403. char *end;
  404. int dev, n = 0;
  405. dev = simple_strtoul(name, &end, 0);
  406. if((*end != '\0') || (end == name)){
  407. *error_out = "line_get_config failed to parse device number";
  408. return(0);
  409. }
  410. if((dev < 0) || (dev >= num)){
  411. *error_out = "device number of of range";
  412. return(0);
  413. }
  414. line = &lines[dev];
  415. down(&line->sem);
  416. if(!line->valid)
  417. CONFIG_CHUNK(str, size, n, "none", 1);
  418. else if(line->count == 0)
  419. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  420. else n = chan_config_string(&line->chan_list, str, size, error_out);
  421. up(&line->sem);
  422. return(n);
  423. }
  424. int line_remove(struct line *lines, int num, char *str)
  425. {
  426. char config[sizeof("conxxxx=none\0")];
  427. sprintf(config, "%s=none", str);
  428. return(!line_setup(lines, num, config, 0));
  429. }
  430. int line_write_room(struct tty_struct *tty)
  431. {
  432. struct line *dev = tty->driver_data;
  433. int room;
  434. if (tty->stopped)
  435. return 0;
  436. room = write_room(dev);
  437. if (0 == room)
  438. printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
  439. __FUNCTION__,tty->name);
  440. return room;
  441. }
  442. struct tty_driver *line_register_devfs(struct lines *set,
  443. struct line_driver *line_driver,
  444. struct tty_operations *ops, struct line *lines,
  445. int nlines)
  446. {
  447. int i;
  448. struct tty_driver *driver = alloc_tty_driver(nlines);
  449. if (!driver)
  450. return NULL;
  451. driver->driver_name = line_driver->name;
  452. driver->name = line_driver->device_name;
  453. driver->devfs_name = line_driver->devfs_name;
  454. driver->major = line_driver->major;
  455. driver->minor_start = line_driver->minor_start;
  456. driver->type = line_driver->type;
  457. driver->subtype = line_driver->subtype;
  458. driver->flags = TTY_DRIVER_REAL_RAW;
  459. driver->init_termios = tty_std_termios;
  460. tty_set_operations(driver, ops);
  461. if (tty_register_driver(driver)) {
  462. printk("%s: can't register %s driver\n",
  463. __FUNCTION__,line_driver->name);
  464. put_tty_driver(driver);
  465. return NULL;
  466. }
  467. for(i = 0; i < nlines; i++){
  468. if(!lines[i].valid)
  469. tty_unregister_device(driver, i);
  470. }
  471. mconsole_register_dev(&line_driver->mc);
  472. return driver;
  473. }
  474. void lines_init(struct line *lines, int nlines)
  475. {
  476. struct line *line;
  477. int i;
  478. for(i = 0; i < nlines; i++){
  479. line = &lines[i];
  480. INIT_LIST_HEAD(&line->chan_list);
  481. sema_init(&line->sem, 1);
  482. if(line->init_str != NULL){
  483. line->init_str = uml_strdup(line->init_str);
  484. if(line->init_str == NULL)
  485. printk("lines_init - uml_strdup returned "
  486. "NULL\n");
  487. }
  488. }
  489. }
  490. struct winch {
  491. struct list_head list;
  492. int fd;
  493. int tty_fd;
  494. int pid;
  495. struct tty_struct *tty;
  496. };
  497. irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
  498. {
  499. struct winch *winch = data;
  500. struct tty_struct *tty;
  501. struct line *line;
  502. int err;
  503. char c;
  504. if(winch->fd != -1){
  505. err = generic_read(winch->fd, &c, NULL);
  506. if(err < 0){
  507. if(err != -EAGAIN){
  508. printk("winch_interrupt : read failed, "
  509. "errno = %d\n", -err);
  510. printk("fd %d is losing SIGWINCH support\n",
  511. winch->tty_fd);
  512. return(IRQ_HANDLED);
  513. }
  514. goto out;
  515. }
  516. }
  517. tty = winch->tty;
  518. if (tty != NULL) {
  519. line = tty->driver_data;
  520. chan_window_size(&line->chan_list,
  521. &tty->winsize.ws_row,
  522. &tty->winsize.ws_col);
  523. kill_pg(tty->pgrp, SIGWINCH, 1);
  524. }
  525. out:
  526. if(winch->fd != -1)
  527. reactivate_fd(winch->fd, WINCH_IRQ);
  528. return(IRQ_HANDLED);
  529. }
  530. DECLARE_MUTEX(winch_handler_sem);
  531. LIST_HEAD(winch_handlers);
  532. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
  533. {
  534. struct winch *winch;
  535. down(&winch_handler_sem);
  536. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  537. if (winch == NULL) {
  538. printk("register_winch_irq - kmalloc failed\n");
  539. goto out;
  540. }
  541. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  542. .fd = fd,
  543. .tty_fd = tty_fd,
  544. .pid = pid,
  545. .tty = tty });
  546. list_add(&winch->list, &winch_handlers);
  547. if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  548. SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
  549. "winch", winch) < 0)
  550. printk("register_winch_irq - failed to register IRQ\n");
  551. out:
  552. up(&winch_handler_sem);
  553. }
  554. static void winch_cleanup(void)
  555. {
  556. struct list_head *ele;
  557. struct winch *winch;
  558. list_for_each(ele, &winch_handlers){
  559. winch = list_entry(ele, struct winch, list);
  560. if(winch->fd != -1){
  561. deactivate_fd(winch->fd, WINCH_IRQ);
  562. os_close_file(winch->fd);
  563. }
  564. if(winch->pid != -1)
  565. os_kill_process(winch->pid, 1);
  566. }
  567. }
  568. __uml_exitcall(winch_cleanup);
  569. char *add_xterm_umid(char *base)
  570. {
  571. char *umid, *title;
  572. int len;
  573. umid = get_umid(1);
  574. if(umid == NULL) return(base);
  575. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  576. title = kmalloc(len, GFP_KERNEL);
  577. if(title == NULL){
  578. printk("Failed to allocate buffer for xterm title\n");
  579. return(base);
  580. }
  581. snprintf(title, len, "%s (%s)", base, umid);
  582. return(title);
  583. }
  584. /*
  585. * Overrides for Emacs so that we follow Linus's tabbing style.
  586. * Emacs will notice this stuff at the end of the file and automatically
  587. * adjust the settings for this buffer only. This must remain at the end
  588. * of the file.
  589. * ---------------------------------------------------------------------------
  590. * Local variables:
  591. * c-file-style: "linux"
  592. * End:
  593. */