line.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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_register_device(driver, n, NULL);
  341. if (IS_ERR(d)) {
  342. *error_out = "Failed to register device";
  343. err = PTR_ERR(d);
  344. parse_chan_pair(NULL, line, n, opts, error_out);
  345. }
  346. }
  347. if (err) {
  348. line->init_str = NULL;
  349. line->valid = 0;
  350. kfree(new);
  351. }
  352. }
  353. out:
  354. return err;
  355. }
  356. /*
  357. * Common setup code for both startup command line and mconsole initialization.
  358. * @lines contains the array (of size @num) to modify;
  359. * @init is the setup string;
  360. * @error_out is an error string in the case of failure;
  361. */
  362. int line_setup(char **conf, unsigned int num, char **def,
  363. char *init, char *name)
  364. {
  365. char *error;
  366. if (*init == '=') {
  367. /*
  368. * We said con=/ssl= instead of con#=, so we are configuring all
  369. * consoles at once.
  370. */
  371. *def = init + 1;
  372. } else {
  373. char *end;
  374. unsigned n = simple_strtoul(init, &end, 0);
  375. if (*end != '=') {
  376. error = "Couldn't parse device number";
  377. goto out;
  378. }
  379. if (n >= num) {
  380. error = "Device number out of range";
  381. goto out;
  382. }
  383. conf[n] = end + 1;
  384. }
  385. return 0;
  386. out:
  387. printk(KERN_ERR "Failed to set up %s with "
  388. "configuration string \"%s\" : %s\n", name, init, error);
  389. return -EINVAL;
  390. }
  391. int line_config(struct line *lines, unsigned int num, char *str,
  392. const struct chan_opts *opts, char **error_out)
  393. {
  394. char *end;
  395. int n;
  396. if (*str == '=') {
  397. *error_out = "Can't configure all devices from mconsole";
  398. return -EINVAL;
  399. }
  400. n = simple_strtoul(str, &end, 0);
  401. if (*end++ != '=') {
  402. *error_out = "Couldn't parse device number";
  403. return -EINVAL;
  404. }
  405. if (n >= num) {
  406. *error_out = "Device number out of range";
  407. return -EINVAL;
  408. }
  409. return setup_one_line(lines, n, end, opts, error_out);
  410. }
  411. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  412. int size, char **error_out)
  413. {
  414. struct line *line;
  415. char *end;
  416. int dev, n = 0;
  417. dev = simple_strtoul(name, &end, 0);
  418. if ((*end != '\0') || (end == name)) {
  419. *error_out = "line_get_config failed to parse device number";
  420. return 0;
  421. }
  422. if ((dev < 0) || (dev >= num)) {
  423. *error_out = "device number out of range";
  424. return 0;
  425. }
  426. line = &lines[dev];
  427. if (!line->valid)
  428. CONFIG_CHUNK(str, size, n, "none", 1);
  429. else {
  430. struct tty_struct *tty = tty_port_tty_get(&line->port);
  431. if (tty == NULL) {
  432. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  433. } else {
  434. n = chan_config_string(line, str, size, error_out);
  435. tty_kref_put(tty);
  436. }
  437. }
  438. return n;
  439. }
  440. int line_id(char **str, int *start_out, int *end_out)
  441. {
  442. char *end;
  443. int n;
  444. n = simple_strtoul(*str, &end, 0);
  445. if ((*end != '\0') || (end == *str))
  446. return -1;
  447. *str = end;
  448. *start_out = n;
  449. *end_out = n;
  450. return n;
  451. }
  452. int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
  453. {
  454. if (n >= num) {
  455. *error_out = "Device number out of range";
  456. return -EINVAL;
  457. }
  458. return setup_one_line(lines, n, "none", NULL, error_out);
  459. }
  460. int register_lines(struct line_driver *line_driver,
  461. const struct tty_operations *ops,
  462. struct line *lines, int nlines)
  463. {
  464. struct tty_driver *driver = alloc_tty_driver(nlines);
  465. int err;
  466. int i;
  467. if (!driver)
  468. return -ENOMEM;
  469. driver->driver_name = line_driver->name;
  470. driver->name = line_driver->device_name;
  471. driver->major = line_driver->major;
  472. driver->minor_start = line_driver->minor_start;
  473. driver->type = line_driver->type;
  474. driver->subtype = line_driver->subtype;
  475. driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  476. driver->init_termios = tty_std_termios;
  477. for (i = 0; i < nlines; i++) {
  478. tty_port_init(&lines[i].port);
  479. lines[i].port.ops = &line_port_ops;
  480. spin_lock_init(&lines[i].lock);
  481. lines[i].driver = line_driver;
  482. INIT_LIST_HEAD(&lines[i].chan_list);
  483. }
  484. tty_set_operations(driver, ops);
  485. err = tty_register_driver(driver);
  486. if (err) {
  487. printk(KERN_ERR "register_lines : can't register %s driver\n",
  488. line_driver->name);
  489. put_tty_driver(driver);
  490. return err;
  491. }
  492. line_driver->driver = driver;
  493. mconsole_register_dev(&line_driver->mc);
  494. return 0;
  495. }
  496. static DEFINE_SPINLOCK(winch_handler_lock);
  497. static LIST_HEAD(winch_handlers);
  498. struct winch {
  499. struct list_head list;
  500. int fd;
  501. int tty_fd;
  502. int pid;
  503. struct tty_struct *tty;
  504. unsigned long stack;
  505. struct work_struct work;
  506. };
  507. static void __free_winch(struct work_struct *work)
  508. {
  509. struct winch *winch = container_of(work, struct winch, work);
  510. um_free_irq(WINCH_IRQ, winch);
  511. if (winch->pid != -1)
  512. os_kill_process(winch->pid, 1);
  513. if (winch->stack != 0)
  514. free_stack(winch->stack, 0);
  515. kfree(winch);
  516. }
  517. static void free_winch(struct winch *winch)
  518. {
  519. int fd = winch->fd;
  520. winch->fd = -1;
  521. if (fd != -1)
  522. os_close_file(fd);
  523. list_del(&winch->list);
  524. __free_winch(&winch->work);
  525. }
  526. static irqreturn_t winch_interrupt(int irq, void *data)
  527. {
  528. struct winch *winch = data;
  529. struct tty_struct *tty;
  530. struct line *line;
  531. int fd = winch->fd;
  532. int err;
  533. char c;
  534. if (fd != -1) {
  535. err = generic_read(fd, &c, NULL);
  536. if (err < 0) {
  537. if (err != -EAGAIN) {
  538. winch->fd = -1;
  539. list_del(&winch->list);
  540. os_close_file(fd);
  541. printk(KERN_ERR "winch_interrupt : "
  542. "read failed, errno = %d\n", -err);
  543. printk(KERN_ERR "fd %d is losing SIGWINCH "
  544. "support\n", winch->tty_fd);
  545. INIT_WORK(&winch->work, __free_winch);
  546. schedule_work(&winch->work);
  547. return IRQ_HANDLED;
  548. }
  549. goto out;
  550. }
  551. }
  552. tty = winch->tty;
  553. if (tty != NULL) {
  554. line = tty->driver_data;
  555. if (line != NULL) {
  556. chan_window_size(line, &tty->winsize.ws_row,
  557. &tty->winsize.ws_col);
  558. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  559. }
  560. }
  561. out:
  562. if (winch->fd != -1)
  563. reactivate_fd(winch->fd, WINCH_IRQ);
  564. return IRQ_HANDLED;
  565. }
  566. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
  567. unsigned long stack)
  568. {
  569. struct winch *winch;
  570. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  571. if (winch == NULL) {
  572. printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
  573. goto cleanup;
  574. }
  575. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  576. .fd = fd,
  577. .tty_fd = tty_fd,
  578. .pid = pid,
  579. .tty = tty,
  580. .stack = stack });
  581. if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  582. IRQF_SHARED, "winch", winch) < 0) {
  583. printk(KERN_ERR "register_winch_irq - failed to register "
  584. "IRQ\n");
  585. goto out_free;
  586. }
  587. spin_lock(&winch_handler_lock);
  588. list_add(&winch->list, &winch_handlers);
  589. spin_unlock(&winch_handler_lock);
  590. return;
  591. out_free:
  592. kfree(winch);
  593. cleanup:
  594. os_kill_process(pid, 1);
  595. os_close_file(fd);
  596. if (stack != 0)
  597. free_stack(stack, 0);
  598. }
  599. static void unregister_winch(struct tty_struct *tty)
  600. {
  601. struct list_head *ele, *next;
  602. struct winch *winch;
  603. spin_lock(&winch_handler_lock);
  604. list_for_each_safe(ele, next, &winch_handlers) {
  605. winch = list_entry(ele, struct winch, list);
  606. if (winch->tty == tty) {
  607. free_winch(winch);
  608. break;
  609. }
  610. }
  611. spin_unlock(&winch_handler_lock);
  612. }
  613. static void winch_cleanup(void)
  614. {
  615. struct list_head *ele, *next;
  616. struct winch *winch;
  617. spin_lock(&winch_handler_lock);
  618. list_for_each_safe(ele, next, &winch_handlers) {
  619. winch = list_entry(ele, struct winch, list);
  620. free_winch(winch);
  621. }
  622. spin_unlock(&winch_handler_lock);
  623. }
  624. __uml_exitcall(winch_cleanup);
  625. char *add_xterm_umid(char *base)
  626. {
  627. char *umid, *title;
  628. int len;
  629. umid = get_umid();
  630. if (*umid == '\0')
  631. return base;
  632. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  633. title = kmalloc(len, GFP_KERNEL);
  634. if (title == NULL) {
  635. printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
  636. return base;
  637. }
  638. snprintf(title, len, "%s (%s)", base, umid);
  639. return title;
  640. }