line.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/irqreturn.h>
  6. #include <linux/kd.h>
  7. #include <linux/sched.h>
  8. #include <linux/slab.h>
  9. #include "chan.h"
  10. #include <irq_kern.h>
  11. #include <irq_user.h>
  12. #include <kern_util.h>
  13. #include <os.h>
  14. #define LINE_BUFSIZE 4096
  15. static irqreturn_t line_interrupt(int irq, void *data)
  16. {
  17. struct chan *chan = data;
  18. struct line *line = chan->line;
  19. struct tty_struct *tty = tty_port_tty_get(&line->port);
  20. if (line)
  21. chan_interrupt(line, tty, irq);
  22. tty_kref_put(tty);
  23. return IRQ_HANDLED;
  24. }
  25. /*
  26. * Returns the free space inside the ring buffer of this line.
  27. *
  28. * Should be called while holding line->lock (this does not modify data).
  29. */
  30. static int write_room(struct line *line)
  31. {
  32. int n;
  33. if (line->buffer == NULL)
  34. return LINE_BUFSIZE - 1;
  35. /* This is for the case where the buffer is wrapped! */
  36. n = line->head - line->tail;
  37. if (n <= 0)
  38. n += LINE_BUFSIZE; /* The other case */
  39. return n - 1;
  40. }
  41. int line_write_room(struct tty_struct *tty)
  42. {
  43. struct line *line = tty->driver_data;
  44. unsigned long flags;
  45. int room;
  46. spin_lock_irqsave(&line->lock, flags);
  47. room = write_room(line);
  48. spin_unlock_irqrestore(&line->lock, flags);
  49. return room;
  50. }
  51. int line_chars_in_buffer(struct tty_struct *tty)
  52. {
  53. struct line *line = tty->driver_data;
  54. unsigned long flags;
  55. int ret;
  56. spin_lock_irqsave(&line->lock, flags);
  57. /* write_room subtracts 1 for the needed NULL, so we readd it.*/
  58. ret = LINE_BUFSIZE - (write_room(line) + 1);
  59. spin_unlock_irqrestore(&line->lock, flags);
  60. return ret;
  61. }
  62. /*
  63. * This copies the content of buf into the circular buffer associated with
  64. * this line.
  65. * The return value is the number of characters actually copied, i.e. the ones
  66. * for which there was space: this function is not supposed to ever flush out
  67. * the circular buffer.
  68. *
  69. * Must be called while holding line->lock!
  70. */
  71. static int buffer_data(struct line *line, const char *buf, int len)
  72. {
  73. int end, room;
  74. if (line->buffer == NULL) {
  75. line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
  76. if (line->buffer == NULL) {
  77. printk(KERN_ERR "buffer_data - atomic allocation "
  78. "failed\n");
  79. return 0;
  80. }
  81. line->head = line->buffer;
  82. line->tail = line->buffer;
  83. }
  84. room = write_room(line);
  85. len = (len > room) ? room : len;
  86. end = line->buffer + LINE_BUFSIZE - line->tail;
  87. if (len < end) {
  88. memcpy(line->tail, buf, len);
  89. line->tail += len;
  90. }
  91. else {
  92. /* The circular buffer is wrapping */
  93. memcpy(line->tail, buf, end);
  94. buf += end;
  95. memcpy(line->buffer, buf, len - end);
  96. line->tail = line->buffer + len - end;
  97. }
  98. return len;
  99. }
  100. /*
  101. * Flushes the ring buffer to the output channels. That is, write_chan is
  102. * called, passing it line->head as buffer, and an appropriate count.
  103. *
  104. * On exit, returns 1 when the buffer is empty,
  105. * 0 when the buffer is not empty on exit,
  106. * and -errno when an error occurred.
  107. *
  108. * Must be called while holding line->lock!*/
  109. static int flush_buffer(struct line *line)
  110. {
  111. int n, count;
  112. if ((line->buffer == NULL) || (line->head == line->tail))
  113. return 1;
  114. if (line->tail < line->head) {
  115. /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
  116. count = line->buffer + LINE_BUFSIZE - line->head;
  117. n = write_chan(line->chan_out, line->head, count,
  118. line->driver->write_irq);
  119. if (n < 0)
  120. return n;
  121. if (n == count) {
  122. /*
  123. * We have flushed from ->head to buffer end, now we
  124. * must flush only from the beginning to ->tail.
  125. */
  126. line->head = line->buffer;
  127. } else {
  128. line->head += n;
  129. return 0;
  130. }
  131. }
  132. count = line->tail - line->head;
  133. n = write_chan(line->chan_out, line->head, count,
  134. line->driver->write_irq);
  135. if (n < 0)
  136. return n;
  137. line->head += n;
  138. return line->head == line->tail;
  139. }
  140. void line_flush_buffer(struct tty_struct *tty)
  141. {
  142. struct line *line = tty->driver_data;
  143. unsigned long flags;
  144. spin_lock_irqsave(&line->lock, flags);
  145. flush_buffer(line);
  146. spin_unlock_irqrestore(&line->lock, flags);
  147. }
  148. /*
  149. * We map both ->flush_chars and ->put_char (which go in pair) onto
  150. * ->flush_buffer and ->write. Hope it's not that bad.
  151. */
  152. void line_flush_chars(struct tty_struct *tty)
  153. {
  154. line_flush_buffer(tty);
  155. }
  156. int line_put_char(struct tty_struct *tty, unsigned char ch)
  157. {
  158. return line_write(tty, &ch, sizeof(ch));
  159. }
  160. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  161. {
  162. struct line *line = tty->driver_data;
  163. unsigned long flags;
  164. int n, ret = 0;
  165. spin_lock_irqsave(&line->lock, flags);
  166. if (line->head != line->tail)
  167. ret = buffer_data(line, buf, len);
  168. else {
  169. n = write_chan(line->chan_out, buf, len,
  170. line->driver->write_irq);
  171. if (n < 0) {
  172. ret = n;
  173. goto out_up;
  174. }
  175. len -= n;
  176. ret += n;
  177. if (len > 0)
  178. ret += buffer_data(line, buf + n, len);
  179. }
  180. out_up:
  181. spin_unlock_irqrestore(&line->lock, flags);
  182. return ret;
  183. }
  184. void line_set_termios(struct tty_struct *tty, struct ktermios * old)
  185. {
  186. /* nothing */
  187. }
  188. void line_throttle(struct tty_struct *tty)
  189. {
  190. struct line *line = tty->driver_data;
  191. deactivate_chan(line->chan_in, line->driver->read_irq);
  192. line->throttled = 1;
  193. }
  194. void line_unthrottle(struct tty_struct *tty)
  195. {
  196. struct line *line = tty->driver_data;
  197. line->throttled = 0;
  198. chan_interrupt(line, tty, line->driver->read_irq);
  199. /*
  200. * Maybe there is enough stuff pending that calling the interrupt
  201. * throttles us again. In this case, line->throttled will be 1
  202. * again and we shouldn't turn the interrupt back on.
  203. */
  204. if (!line->throttled)
  205. reactivate_chan(line->chan_in, line->driver->read_irq);
  206. }
  207. static irqreturn_t line_write_interrupt(int irq, void *data)
  208. {
  209. struct chan *chan = data;
  210. struct line *line = chan->line;
  211. struct tty_struct *tty;
  212. int err;
  213. /*
  214. * Interrupts are disabled here because genirq keep irqs disabled when
  215. * calling the action handler.
  216. */
  217. spin_lock(&line->lock);
  218. err = flush_buffer(line);
  219. if (err == 0) {
  220. spin_unlock(&line->lock);
  221. return IRQ_NONE;
  222. } else if (err < 0) {
  223. line->head = line->buffer;
  224. line->tail = line->buffer;
  225. }
  226. spin_unlock(&line->lock);
  227. tty = tty_port_tty_get(&line->port);
  228. if (tty == NULL)
  229. return IRQ_NONE;
  230. tty_wakeup(tty);
  231. tty_kref_put(tty);
  232. return IRQ_HANDLED;
  233. }
  234. int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
  235. {
  236. const struct line_driver *driver = line->driver;
  237. int err = 0;
  238. if (input)
  239. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  240. line_interrupt, IRQF_SHARED,
  241. driver->read_irq_name, data);
  242. if (err)
  243. return err;
  244. if (output)
  245. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  246. line_write_interrupt, IRQF_SHARED,
  247. driver->write_irq_name, data);
  248. return err;
  249. }
  250. static int line_activate(struct tty_port *port, struct tty_struct *tty)
  251. {
  252. int ret;
  253. struct line *line = tty->driver_data;
  254. ret = enable_chan(line);
  255. if (ret)
  256. return ret;
  257. if (!line->sigio) {
  258. chan_enable_winch(line->chan_out, tty);
  259. line->sigio = 1;
  260. }
  261. chan_window_size(line, &tty->winsize.ws_row,
  262. &tty->winsize.ws_col);
  263. return 0;
  264. }
  265. static const struct tty_port_operations line_port_ops = {
  266. .activate = line_activate,
  267. };
  268. int line_open(struct tty_struct *tty, struct file *filp)
  269. {
  270. struct line *line = tty->driver_data;
  271. return tty_port_open(&line->port, tty, filp);
  272. }
  273. int line_install(struct tty_driver *driver, struct tty_struct *tty,
  274. struct line *line)
  275. {
  276. int ret;
  277. ret = tty_standard_install(driver, tty);
  278. if (ret)
  279. return ret;
  280. tty->driver_data = line;
  281. return 0;
  282. }
  283. static void unregister_winch(struct tty_struct *tty);
  284. void line_cleanup(struct tty_struct *tty)
  285. {
  286. struct line *line = tty->driver_data;
  287. if (line->sigio) {
  288. unregister_winch(tty);
  289. line->sigio = 0;
  290. }
  291. }
  292. void line_close(struct tty_struct *tty, struct file * filp)
  293. {
  294. struct line *line = tty->driver_data;
  295. tty_port_close(&line->port, tty, filp);
  296. }
  297. void line_hangup(struct tty_struct *tty)
  298. {
  299. struct line *line = tty->driver_data;
  300. tty_port_hangup(&line->port);
  301. }
  302. void close_lines(struct line *lines, int nlines)
  303. {
  304. int i;
  305. for(i = 0; i < nlines; i++)
  306. close_chan(&lines[i]);
  307. }
  308. int setup_one_line(struct line *lines, int n, char *init,
  309. const struct chan_opts *opts, char **error_out)
  310. {
  311. struct line *line = &lines[n];
  312. struct tty_driver *driver = line->driver->driver;
  313. int err = -EINVAL;
  314. if (line->port.count) {
  315. *error_out = "Device is already open";
  316. goto out;
  317. }
  318. if (!strcmp(init, "none")) {
  319. if (line->valid) {
  320. line->valid = 0;
  321. kfree(line->init_str);
  322. tty_unregister_device(driver, n);
  323. parse_chan_pair(NULL, line, n, opts, error_out);
  324. err = 0;
  325. }
  326. } else {
  327. char *new = kstrdup(init, GFP_KERNEL);
  328. if (!new) {
  329. *error_out = "Failed to allocate memory";
  330. return -ENOMEM;
  331. }
  332. if (line->valid) {
  333. tty_unregister_device(driver, n);
  334. kfree(line->init_str);
  335. }
  336. line->init_str = new;
  337. line->valid = 1;
  338. err = parse_chan_pair(new, line, n, opts, error_out);
  339. if (!err) {
  340. struct device *d = tty_port_register_device(&line->port,
  341. driver, n, NULL);
  342. if (IS_ERR(d)) {
  343. *error_out = "Failed to register device";
  344. err = PTR_ERR(d);
  345. parse_chan_pair(NULL, line, n, opts, error_out);
  346. }
  347. }
  348. if (err) {
  349. line->init_str = NULL;
  350. line->valid = 0;
  351. kfree(new);
  352. }
  353. }
  354. out:
  355. return err;
  356. }
  357. /*
  358. * Common setup code for both startup command line and mconsole initialization.
  359. * @lines contains the array (of size @num) to modify;
  360. * @init is the setup string;
  361. * @error_out is an error string in the case of failure;
  362. */
  363. int line_setup(char **conf, unsigned int num, char **def,
  364. char *init, char *name)
  365. {
  366. char *error;
  367. if (*init == '=') {
  368. /*
  369. * We said con=/ssl= instead of con#=, so we are configuring all
  370. * consoles at once.
  371. */
  372. *def = init + 1;
  373. } else {
  374. char *end;
  375. unsigned n = simple_strtoul(init, &end, 0);
  376. if (*end != '=') {
  377. error = "Couldn't parse device number";
  378. goto out;
  379. }
  380. if (n >= num) {
  381. error = "Device number out of range";
  382. goto out;
  383. }
  384. conf[n] = end + 1;
  385. }
  386. return 0;
  387. out:
  388. printk(KERN_ERR "Failed to set up %s with "
  389. "configuration string \"%s\" : %s\n", name, init, error);
  390. return -EINVAL;
  391. }
  392. int line_config(struct line *lines, unsigned int num, char *str,
  393. const struct chan_opts *opts, char **error_out)
  394. {
  395. char *end;
  396. int n;
  397. if (*str == '=') {
  398. *error_out = "Can't configure all devices from mconsole";
  399. return -EINVAL;
  400. }
  401. n = simple_strtoul(str, &end, 0);
  402. if (*end++ != '=') {
  403. *error_out = "Couldn't parse device number";
  404. return -EINVAL;
  405. }
  406. if (n >= num) {
  407. *error_out = "Device number out of range";
  408. return -EINVAL;
  409. }
  410. return setup_one_line(lines, n, end, opts, error_out);
  411. }
  412. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  413. int size, char **error_out)
  414. {
  415. struct line *line;
  416. char *end;
  417. int dev, n = 0;
  418. dev = simple_strtoul(name, &end, 0);
  419. if ((*end != '\0') || (end == name)) {
  420. *error_out = "line_get_config failed to parse device number";
  421. return 0;
  422. }
  423. if ((dev < 0) || (dev >= num)) {
  424. *error_out = "device number out of range";
  425. return 0;
  426. }
  427. line = &lines[dev];
  428. if (!line->valid)
  429. CONFIG_CHUNK(str, size, n, "none", 1);
  430. else {
  431. struct tty_struct *tty = tty_port_tty_get(&line->port);
  432. if (tty == NULL) {
  433. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  434. } else {
  435. n = chan_config_string(line, str, size, error_out);
  436. tty_kref_put(tty);
  437. }
  438. }
  439. return n;
  440. }
  441. int line_id(char **str, int *start_out, int *end_out)
  442. {
  443. char *end;
  444. int n;
  445. n = simple_strtoul(*str, &end, 0);
  446. if ((*end != '\0') || (end == *str))
  447. return -1;
  448. *str = end;
  449. *start_out = n;
  450. *end_out = n;
  451. return n;
  452. }
  453. int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
  454. {
  455. if (n >= num) {
  456. *error_out = "Device number out of range";
  457. return -EINVAL;
  458. }
  459. return setup_one_line(lines, n, "none", NULL, error_out);
  460. }
  461. int register_lines(struct line_driver *line_driver,
  462. const struct tty_operations *ops,
  463. struct line *lines, int nlines)
  464. {
  465. struct tty_driver *driver = alloc_tty_driver(nlines);
  466. int err;
  467. int i;
  468. if (!driver)
  469. return -ENOMEM;
  470. driver->driver_name = line_driver->name;
  471. driver->name = line_driver->device_name;
  472. driver->major = line_driver->major;
  473. driver->minor_start = line_driver->minor_start;
  474. driver->type = line_driver->type;
  475. driver->subtype = line_driver->subtype;
  476. driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  477. driver->init_termios = tty_std_termios;
  478. for (i = 0; i < nlines; i++) {
  479. tty_port_init(&lines[i].port);
  480. lines[i].port.ops = &line_port_ops;
  481. spin_lock_init(&lines[i].lock);
  482. lines[i].driver = line_driver;
  483. INIT_LIST_HEAD(&lines[i].chan_list);
  484. }
  485. tty_set_operations(driver, ops);
  486. err = tty_register_driver(driver);
  487. if (err) {
  488. printk(KERN_ERR "register_lines : can't register %s driver\n",
  489. line_driver->name);
  490. put_tty_driver(driver);
  491. return err;
  492. }
  493. line_driver->driver = driver;
  494. mconsole_register_dev(&line_driver->mc);
  495. return 0;
  496. }
  497. static DEFINE_SPINLOCK(winch_handler_lock);
  498. static LIST_HEAD(winch_handlers);
  499. struct winch {
  500. struct list_head list;
  501. int fd;
  502. int tty_fd;
  503. int pid;
  504. struct tty_struct *tty;
  505. unsigned long stack;
  506. struct work_struct work;
  507. };
  508. static void __free_winch(struct work_struct *work)
  509. {
  510. struct winch *winch = container_of(work, struct winch, work);
  511. um_free_irq(WINCH_IRQ, winch);
  512. if (winch->pid != -1)
  513. os_kill_process(winch->pid, 1);
  514. if (winch->stack != 0)
  515. free_stack(winch->stack, 0);
  516. kfree(winch);
  517. }
  518. static void free_winch(struct winch *winch)
  519. {
  520. int fd = winch->fd;
  521. winch->fd = -1;
  522. if (fd != -1)
  523. os_close_file(fd);
  524. list_del(&winch->list);
  525. __free_winch(&winch->work);
  526. }
  527. static irqreturn_t winch_interrupt(int irq, void *data)
  528. {
  529. struct winch *winch = data;
  530. struct tty_struct *tty;
  531. struct line *line;
  532. int fd = winch->fd;
  533. int err;
  534. char c;
  535. if (fd != -1) {
  536. err = generic_read(fd, &c, NULL);
  537. if (err < 0) {
  538. if (err != -EAGAIN) {
  539. winch->fd = -1;
  540. list_del(&winch->list);
  541. os_close_file(fd);
  542. printk(KERN_ERR "winch_interrupt : "
  543. "read failed, errno = %d\n", -err);
  544. printk(KERN_ERR "fd %d is losing SIGWINCH "
  545. "support\n", winch->tty_fd);
  546. INIT_WORK(&winch->work, __free_winch);
  547. schedule_work(&winch->work);
  548. return IRQ_HANDLED;
  549. }
  550. goto out;
  551. }
  552. }
  553. tty = winch->tty;
  554. if (tty != NULL) {
  555. line = tty->driver_data;
  556. if (line != NULL) {
  557. chan_window_size(line, &tty->winsize.ws_row,
  558. &tty->winsize.ws_col);
  559. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  560. }
  561. }
  562. out:
  563. if (winch->fd != -1)
  564. reactivate_fd(winch->fd, WINCH_IRQ);
  565. return IRQ_HANDLED;
  566. }
  567. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
  568. unsigned long stack)
  569. {
  570. struct winch *winch;
  571. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  572. if (winch == NULL) {
  573. printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
  574. goto cleanup;
  575. }
  576. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  577. .fd = fd,
  578. .tty_fd = tty_fd,
  579. .pid = pid,
  580. .tty = tty,
  581. .stack = stack });
  582. if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  583. IRQF_SHARED, "winch", winch) < 0) {
  584. printk(KERN_ERR "register_winch_irq - failed to register "
  585. "IRQ\n");
  586. goto out_free;
  587. }
  588. spin_lock(&winch_handler_lock);
  589. list_add(&winch->list, &winch_handlers);
  590. spin_unlock(&winch_handler_lock);
  591. return;
  592. out_free:
  593. kfree(winch);
  594. cleanup:
  595. os_kill_process(pid, 1);
  596. os_close_file(fd);
  597. if (stack != 0)
  598. free_stack(stack, 0);
  599. }
  600. static void unregister_winch(struct tty_struct *tty)
  601. {
  602. struct list_head *ele, *next;
  603. struct winch *winch;
  604. spin_lock(&winch_handler_lock);
  605. list_for_each_safe(ele, next, &winch_handlers) {
  606. winch = list_entry(ele, struct winch, list);
  607. if (winch->tty == tty) {
  608. free_winch(winch);
  609. break;
  610. }
  611. }
  612. spin_unlock(&winch_handler_lock);
  613. }
  614. static void winch_cleanup(void)
  615. {
  616. struct list_head *ele, *next;
  617. struct winch *winch;
  618. spin_lock(&winch_handler_lock);
  619. list_for_each_safe(ele, next, &winch_handlers) {
  620. winch = list_entry(ele, struct winch, list);
  621. free_winch(winch);
  622. }
  623. spin_unlock(&winch_handler_lock);
  624. }
  625. __uml_exitcall(winch_cleanup);
  626. char *add_xterm_umid(char *base)
  627. {
  628. char *umid, *title;
  629. int len;
  630. umid = get_umid();
  631. if (*umid == '\0')
  632. return base;
  633. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  634. title = kmalloc(len, GFP_KERNEL);
  635. if (title == NULL) {
  636. printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
  637. return base;
  638. }
  639. snprintf(title, len, "%s (%s)", base, umid);
  640. return title;
  641. }