line.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. if (line)
  20. chan_interrupt(line, irq);
  21. return IRQ_HANDLED;
  22. }
  23. /*
  24. * Returns the free space inside the ring buffer of this line.
  25. *
  26. * Should be called while holding line->lock (this does not modify data).
  27. */
  28. static int write_room(struct line *line)
  29. {
  30. int n;
  31. if (line->buffer == NULL)
  32. return LINE_BUFSIZE - 1;
  33. /* This is for the case where the buffer is wrapped! */
  34. n = line->head - line->tail;
  35. if (n <= 0)
  36. n += LINE_BUFSIZE; /* The other case */
  37. return n - 1;
  38. }
  39. int line_write_room(struct tty_struct *tty)
  40. {
  41. struct line *line = tty->driver_data;
  42. unsigned long flags;
  43. int room;
  44. spin_lock_irqsave(&line->lock, flags);
  45. room = write_room(line);
  46. spin_unlock_irqrestore(&line->lock, flags);
  47. return room;
  48. }
  49. int line_chars_in_buffer(struct tty_struct *tty)
  50. {
  51. struct line *line = tty->driver_data;
  52. unsigned long flags;
  53. int ret;
  54. spin_lock_irqsave(&line->lock, flags);
  55. /* write_room subtracts 1 for the needed NULL, so we readd it.*/
  56. ret = LINE_BUFSIZE - (write_room(line) + 1);
  57. spin_unlock_irqrestore(&line->lock, flags);
  58. return ret;
  59. }
  60. /*
  61. * This copies the content of buf into the circular buffer associated with
  62. * this line.
  63. * The return value is the number of characters actually copied, i.e. the ones
  64. * for which there was space: this function is not supposed to ever flush out
  65. * the circular buffer.
  66. *
  67. * Must be called while holding line->lock!
  68. */
  69. static int buffer_data(struct line *line, const char *buf, int len)
  70. {
  71. int end, room;
  72. if (line->buffer == NULL) {
  73. line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
  74. if (line->buffer == NULL) {
  75. printk(KERN_ERR "buffer_data - atomic allocation "
  76. "failed\n");
  77. return 0;
  78. }
  79. line->head = line->buffer;
  80. line->tail = line->buffer;
  81. }
  82. room = write_room(line);
  83. len = (len > room) ? room : len;
  84. end = line->buffer + LINE_BUFSIZE - line->tail;
  85. if (len < end) {
  86. memcpy(line->tail, buf, len);
  87. line->tail += len;
  88. }
  89. else {
  90. /* The circular buffer is wrapping */
  91. memcpy(line->tail, buf, end);
  92. buf += end;
  93. memcpy(line->buffer, buf, len - end);
  94. line->tail = line->buffer + len - end;
  95. }
  96. return len;
  97. }
  98. /*
  99. * Flushes the ring buffer to the output channels. That is, write_chan is
  100. * called, passing it line->head as buffer, and an appropriate count.
  101. *
  102. * On exit, returns 1 when the buffer is empty,
  103. * 0 when the buffer is not empty on exit,
  104. * and -errno when an error occurred.
  105. *
  106. * Must be called while holding line->lock!*/
  107. static int flush_buffer(struct line *line)
  108. {
  109. int n, count;
  110. if ((line->buffer == NULL) || (line->head == line->tail))
  111. return 1;
  112. if (line->tail < line->head) {
  113. /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
  114. count = line->buffer + LINE_BUFSIZE - line->head;
  115. n = write_chan(line->chan_out, line->head, count,
  116. line->driver->write_irq);
  117. if (n < 0)
  118. return n;
  119. if (n == count) {
  120. /*
  121. * We have flushed from ->head to buffer end, now we
  122. * must flush only from the beginning to ->tail.
  123. */
  124. line->head = line->buffer;
  125. } else {
  126. line->head += n;
  127. return 0;
  128. }
  129. }
  130. count = line->tail - line->head;
  131. n = write_chan(line->chan_out, line->head, count,
  132. line->driver->write_irq);
  133. if (n < 0)
  134. return n;
  135. line->head += n;
  136. return line->head == line->tail;
  137. }
  138. void line_flush_buffer(struct tty_struct *tty)
  139. {
  140. struct line *line = tty->driver_data;
  141. unsigned long flags;
  142. spin_lock_irqsave(&line->lock, flags);
  143. flush_buffer(line);
  144. spin_unlock_irqrestore(&line->lock, flags);
  145. }
  146. /*
  147. * We map both ->flush_chars and ->put_char (which go in pair) onto
  148. * ->flush_buffer and ->write. Hope it's not that bad.
  149. */
  150. void line_flush_chars(struct tty_struct *tty)
  151. {
  152. line_flush_buffer(tty);
  153. }
  154. int line_put_char(struct tty_struct *tty, unsigned char ch)
  155. {
  156. return line_write(tty, &ch, sizeof(ch));
  157. }
  158. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  159. {
  160. struct line *line = tty->driver_data;
  161. unsigned long flags;
  162. int n, ret = 0;
  163. spin_lock_irqsave(&line->lock, flags);
  164. if (line->head != line->tail)
  165. ret = buffer_data(line, buf, len);
  166. else {
  167. n = write_chan(line->chan_out, buf, len,
  168. line->driver->write_irq);
  169. if (n < 0) {
  170. ret = n;
  171. goto out_up;
  172. }
  173. len -= n;
  174. ret += n;
  175. if (len > 0)
  176. ret += buffer_data(line, buf + n, len);
  177. }
  178. out_up:
  179. spin_unlock_irqrestore(&line->lock, flags);
  180. return ret;
  181. }
  182. void line_set_termios(struct tty_struct *tty, struct ktermios * old)
  183. {
  184. /* nothing */
  185. }
  186. void line_throttle(struct tty_struct *tty)
  187. {
  188. struct line *line = tty->driver_data;
  189. deactivate_chan(line->chan_in, line->driver->read_irq);
  190. line->throttled = 1;
  191. }
  192. void line_unthrottle(struct tty_struct *tty)
  193. {
  194. struct line *line = tty->driver_data;
  195. line->throttled = 0;
  196. chan_interrupt(line, line->driver->read_irq);
  197. /*
  198. * Maybe there is enough stuff pending that calling the interrupt
  199. * throttles us again. In this case, line->throttled will be 1
  200. * again and we shouldn't turn the interrupt back on.
  201. */
  202. if (!line->throttled)
  203. reactivate_chan(line->chan_in, line->driver->read_irq);
  204. }
  205. static irqreturn_t line_write_interrupt(int irq, void *data)
  206. {
  207. struct chan *chan = data;
  208. struct line *line = chan->line;
  209. struct tty_struct *tty;
  210. int err;
  211. /*
  212. * Interrupts are disabled here because genirq keep irqs disabled when
  213. * calling the action handler.
  214. */
  215. spin_lock(&line->lock);
  216. err = flush_buffer(line);
  217. if (err == 0) {
  218. spin_unlock(&line->lock);
  219. return IRQ_NONE;
  220. } else if (err < 0) {
  221. line->head = line->buffer;
  222. line->tail = line->buffer;
  223. }
  224. spin_unlock(&line->lock);
  225. tty = tty_port_tty_get(&line->port);
  226. if (tty == NULL)
  227. return IRQ_NONE;
  228. tty_wakeup(tty);
  229. tty_kref_put(tty);
  230. return IRQ_HANDLED;
  231. }
  232. int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
  233. {
  234. const struct line_driver *driver = line->driver;
  235. int err = 0;
  236. if (input)
  237. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  238. line_interrupt, IRQF_SHARED,
  239. driver->read_irq_name, data);
  240. if (err)
  241. return err;
  242. if (output)
  243. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  244. line_write_interrupt, IRQF_SHARED,
  245. driver->write_irq_name, data);
  246. return err;
  247. }
  248. static int line_activate(struct tty_port *port, struct tty_struct *tty)
  249. {
  250. int ret;
  251. struct line *line = tty->driver_data;
  252. ret = enable_chan(line);
  253. if (ret)
  254. return ret;
  255. if (!line->sigio) {
  256. chan_enable_winch(line->chan_out, tty);
  257. line->sigio = 1;
  258. }
  259. chan_window_size(line, &tty->winsize.ws_row,
  260. &tty->winsize.ws_col);
  261. return 0;
  262. }
  263. static const struct tty_port_operations line_port_ops = {
  264. .activate = line_activate,
  265. };
  266. int line_open(struct tty_struct *tty, struct file *filp)
  267. {
  268. struct line *line = tty->driver_data;
  269. return tty_port_open(&line->port, tty, filp);
  270. }
  271. int line_install(struct tty_driver *driver, struct tty_struct *tty,
  272. struct line *line)
  273. {
  274. int ret;
  275. ret = tty_standard_install(driver, tty);
  276. if (ret)
  277. return ret;
  278. tty->driver_data = line;
  279. return 0;
  280. }
  281. static void unregister_winch(struct tty_struct *tty);
  282. void line_cleanup(struct tty_struct *tty)
  283. {
  284. struct line *line = tty->driver_data;
  285. if (line->sigio) {
  286. unregister_winch(tty);
  287. line->sigio = 0;
  288. }
  289. }
  290. void line_close(struct tty_struct *tty, struct file * filp)
  291. {
  292. struct line *line = tty->driver_data;
  293. tty_port_close(&line->port, tty, filp);
  294. }
  295. void line_hangup(struct tty_struct *tty)
  296. {
  297. struct line *line = tty->driver_data;
  298. tty_port_hangup(&line->port);
  299. }
  300. void close_lines(struct line *lines, int nlines)
  301. {
  302. int i;
  303. for(i = 0; i < nlines; i++)
  304. close_chan(&lines[i]);
  305. }
  306. int setup_one_line(struct line *lines, int n, char *init,
  307. const struct chan_opts *opts, char **error_out)
  308. {
  309. struct line *line = &lines[n];
  310. struct tty_driver *driver = line->driver->driver;
  311. int err = -EINVAL;
  312. if (line->port.count) {
  313. *error_out = "Device is already open";
  314. goto out;
  315. }
  316. if (!strcmp(init, "none")) {
  317. if (line->valid) {
  318. line->valid = 0;
  319. kfree(line->init_str);
  320. tty_unregister_device(driver, n);
  321. parse_chan_pair(NULL, line, n, opts, error_out);
  322. err = 0;
  323. }
  324. } else {
  325. char *new = kstrdup(init, GFP_KERNEL);
  326. if (!new) {
  327. *error_out = "Failed to allocate memory";
  328. return -ENOMEM;
  329. }
  330. if (line->valid) {
  331. tty_unregister_device(driver, n);
  332. kfree(line->init_str);
  333. }
  334. line->init_str = new;
  335. line->valid = 1;
  336. err = parse_chan_pair(new, line, n, opts, error_out);
  337. if (!err) {
  338. struct device *d = tty_port_register_device(&line->port,
  339. driver, n, NULL);
  340. if (IS_ERR(d)) {
  341. *error_out = "Failed to register device";
  342. err = PTR_ERR(d);
  343. parse_chan_pair(NULL, line, n, opts, error_out);
  344. }
  345. }
  346. if (err) {
  347. line->init_str = NULL;
  348. line->valid = 0;
  349. kfree(new);
  350. }
  351. }
  352. out:
  353. return err;
  354. }
  355. /*
  356. * Common setup code for both startup command line and mconsole initialization.
  357. * @lines contains the array (of size @num) to modify;
  358. * @init is the setup string;
  359. * @error_out is an error string in the case of failure;
  360. */
  361. int line_setup(char **conf, unsigned int num, char **def,
  362. char *init, char *name)
  363. {
  364. char *error;
  365. if (*init == '=') {
  366. /*
  367. * We said con=/ssl= instead of con#=, so we are configuring all
  368. * consoles at once.
  369. */
  370. *def = init + 1;
  371. } else {
  372. char *end;
  373. unsigned n = simple_strtoul(init, &end, 0);
  374. if (*end != '=') {
  375. error = "Couldn't parse device number";
  376. goto out;
  377. }
  378. if (n >= num) {
  379. error = "Device number out of range";
  380. goto out;
  381. }
  382. conf[n] = end + 1;
  383. }
  384. return 0;
  385. out:
  386. printk(KERN_ERR "Failed to set up %s with "
  387. "configuration string \"%s\" : %s\n", name, init, error);
  388. return -EINVAL;
  389. }
  390. int line_config(struct line *lines, unsigned int num, char *str,
  391. const struct chan_opts *opts, char **error_out)
  392. {
  393. char *end;
  394. int n;
  395. if (*str == '=') {
  396. *error_out = "Can't configure all devices from mconsole";
  397. return -EINVAL;
  398. }
  399. n = simple_strtoul(str, &end, 0);
  400. if (*end++ != '=') {
  401. *error_out = "Couldn't parse device number";
  402. return -EINVAL;
  403. }
  404. if (n >= num) {
  405. *error_out = "Device number out of range";
  406. return -EINVAL;
  407. }
  408. return setup_one_line(lines, n, end, opts, error_out);
  409. }
  410. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  411. int size, char **error_out)
  412. {
  413. struct line *line;
  414. char *end;
  415. int dev, n = 0;
  416. dev = simple_strtoul(name, &end, 0);
  417. if ((*end != '\0') || (end == name)) {
  418. *error_out = "line_get_config failed to parse device number";
  419. return 0;
  420. }
  421. if ((dev < 0) || (dev >= num)) {
  422. *error_out = "device number out of range";
  423. return 0;
  424. }
  425. line = &lines[dev];
  426. if (!line->valid)
  427. CONFIG_CHUNK(str, size, n, "none", 1);
  428. else {
  429. struct tty_struct *tty = tty_port_tty_get(&line->port);
  430. if (tty == NULL) {
  431. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  432. } else {
  433. n = chan_config_string(line, str, size, error_out);
  434. tty_kref_put(tty);
  435. }
  436. }
  437. return n;
  438. }
  439. int line_id(char **str, int *start_out, int *end_out)
  440. {
  441. char *end;
  442. int n;
  443. n = simple_strtoul(*str, &end, 0);
  444. if ((*end != '\0') || (end == *str))
  445. return -1;
  446. *str = end;
  447. *start_out = n;
  448. *end_out = n;
  449. return n;
  450. }
  451. int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
  452. {
  453. if (n >= num) {
  454. *error_out = "Device number out of range";
  455. return -EINVAL;
  456. }
  457. return setup_one_line(lines, n, "none", NULL, error_out);
  458. }
  459. int register_lines(struct line_driver *line_driver,
  460. const struct tty_operations *ops,
  461. struct line *lines, int nlines)
  462. {
  463. struct tty_driver *driver = alloc_tty_driver(nlines);
  464. int err;
  465. int i;
  466. if (!driver)
  467. return -ENOMEM;
  468. driver->driver_name = line_driver->name;
  469. driver->name = line_driver->device_name;
  470. driver->major = line_driver->major;
  471. driver->minor_start = line_driver->minor_start;
  472. driver->type = line_driver->type;
  473. driver->subtype = line_driver->subtype;
  474. driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  475. driver->init_termios = tty_std_termios;
  476. for (i = 0; i < nlines; i++) {
  477. tty_port_init(&lines[i].port);
  478. lines[i].port.ops = &line_port_ops;
  479. spin_lock_init(&lines[i].lock);
  480. lines[i].driver = line_driver;
  481. INIT_LIST_HEAD(&lines[i].chan_list);
  482. }
  483. tty_set_operations(driver, ops);
  484. err = tty_register_driver(driver);
  485. if (err) {
  486. printk(KERN_ERR "register_lines : can't register %s driver\n",
  487. line_driver->name);
  488. put_tty_driver(driver);
  489. for (i = 0; i < nlines; i++)
  490. tty_port_destroy(&lines[i].port);
  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. }