line.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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 "chan_kern.h"
  8. #include "irq_kern.h"
  9. #include "irq_user.h"
  10. #include "kern_util.h"
  11. #include "os.h"
  12. #define LINE_BUFSIZE 4096
  13. static irqreturn_t line_interrupt(int irq, void *data)
  14. {
  15. struct chan *chan = data;
  16. struct line *line = chan->line;
  17. struct tty_struct *tty = line->tty;
  18. if (line)
  19. chan_interrupt(&line->chan_list, &line->task, tty, irq);
  20. return IRQ_HANDLED;
  21. }
  22. static void line_timer_cb(struct work_struct *work)
  23. {
  24. struct line *line = container_of(work, struct line, task.work);
  25. if (!line->throttled)
  26. chan_interrupt(&line->chan_list, &line->task, line->tty,
  27. line->driver->read_irq);
  28. }
  29. /*
  30. * Returns the free space inside the ring buffer of this line.
  31. *
  32. * Should be called while holding line->lock (this does not modify data).
  33. */
  34. static int write_room(struct line *line)
  35. {
  36. int n;
  37. if (line->buffer == NULL)
  38. return LINE_BUFSIZE - 1;
  39. /* This is for the case where the buffer is wrapped! */
  40. n = line->head - line->tail;
  41. if (n <= 0)
  42. n += LINE_BUFSIZE; /* The other case */
  43. return n - 1;
  44. }
  45. int line_write_room(struct tty_struct *tty)
  46. {
  47. struct line *line = tty->driver_data;
  48. unsigned long flags;
  49. int room;
  50. spin_lock_irqsave(&line->lock, flags);
  51. room = write_room(line);
  52. spin_unlock_irqrestore(&line->lock, flags);
  53. return room;
  54. }
  55. int line_chars_in_buffer(struct tty_struct *tty)
  56. {
  57. struct line *line = tty->driver_data;
  58. unsigned long flags;
  59. int ret;
  60. spin_lock_irqsave(&line->lock, flags);
  61. /* write_room subtracts 1 for the needed NULL, so we readd it.*/
  62. ret = LINE_BUFSIZE - (write_room(line) + 1);
  63. spin_unlock_irqrestore(&line->lock, flags);
  64. return ret;
  65. }
  66. /*
  67. * This copies the content of buf into the circular buffer associated with
  68. * this line.
  69. * The return value is the number of characters actually copied, i.e. the ones
  70. * for which there was space: this function is not supposed to ever flush out
  71. * the circular buffer.
  72. *
  73. * Must be called while holding line->lock!
  74. */
  75. static int buffer_data(struct line *line, const char *buf, int len)
  76. {
  77. int end, room;
  78. if (line->buffer == NULL) {
  79. line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
  80. if (line->buffer == NULL) {
  81. printk(KERN_ERR "buffer_data - atomic allocation "
  82. "failed\n");
  83. return 0;
  84. }
  85. line->head = line->buffer;
  86. line->tail = line->buffer;
  87. }
  88. room = write_room(line);
  89. len = (len > room) ? room : len;
  90. end = line->buffer + LINE_BUFSIZE - line->tail;
  91. if (len < end) {
  92. memcpy(line->tail, buf, len);
  93. line->tail += len;
  94. }
  95. else {
  96. /* The circular buffer is wrapping */
  97. memcpy(line->tail, buf, end);
  98. buf += end;
  99. memcpy(line->buffer, buf, len - end);
  100. line->tail = line->buffer + len - end;
  101. }
  102. return len;
  103. }
  104. /*
  105. * Flushes the ring buffer to the output channels. That is, write_chan is
  106. * called, passing it line->head as buffer, and an appropriate count.
  107. *
  108. * On exit, returns 1 when the buffer is empty,
  109. * 0 when the buffer is not empty on exit,
  110. * and -errno when an error occurred.
  111. *
  112. * Must be called while holding line->lock!*/
  113. static int flush_buffer(struct line *line)
  114. {
  115. int n, count;
  116. if ((line->buffer == NULL) || (line->head == line->tail))
  117. return 1;
  118. if (line->tail < line->head) {
  119. /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
  120. count = line->buffer + LINE_BUFSIZE - line->head;
  121. n = write_chan(&line->chan_list, line->head, count,
  122. line->driver->write_irq);
  123. if (n < 0)
  124. return n;
  125. if (n == count) {
  126. /*
  127. * We have flushed from ->head to buffer end, now we
  128. * must flush only from the beginning to ->tail.
  129. */
  130. line->head = line->buffer;
  131. } else {
  132. line->head += n;
  133. return 0;
  134. }
  135. }
  136. count = line->tail - line->head;
  137. n = write_chan(&line->chan_list, line->head, count,
  138. line->driver->write_irq);
  139. if (n < 0)
  140. return n;
  141. line->head += n;
  142. return line->head == line->tail;
  143. }
  144. void line_flush_buffer(struct tty_struct *tty)
  145. {
  146. struct line *line = tty->driver_data;
  147. unsigned long flags;
  148. int err;
  149. spin_lock_irqsave(&line->lock, flags);
  150. err = flush_buffer(line);
  151. spin_unlock_irqrestore(&line->lock, flags);
  152. }
  153. /*
  154. * We map both ->flush_chars and ->put_char (which go in pair) onto
  155. * ->flush_buffer and ->write. Hope it's not that bad.
  156. */
  157. void line_flush_chars(struct tty_struct *tty)
  158. {
  159. line_flush_buffer(tty);
  160. }
  161. int line_put_char(struct tty_struct *tty, unsigned char ch)
  162. {
  163. return line_write(tty, &ch, sizeof(ch));
  164. }
  165. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  166. {
  167. struct line *line = tty->driver_data;
  168. unsigned long flags;
  169. int n, ret = 0;
  170. spin_lock_irqsave(&line->lock, flags);
  171. if (line->head != line->tail)
  172. ret = buffer_data(line, buf, len);
  173. else {
  174. n = write_chan(&line->chan_list, buf, len,
  175. line->driver->write_irq);
  176. if (n < 0) {
  177. ret = n;
  178. goto out_up;
  179. }
  180. len -= n;
  181. ret += n;
  182. if (len > 0)
  183. ret += buffer_data(line, buf + n, len);
  184. }
  185. out_up:
  186. spin_unlock_irqrestore(&line->lock, flags);
  187. return ret;
  188. }
  189. void line_set_termios(struct tty_struct *tty, struct ktermios * old)
  190. {
  191. /* nothing */
  192. }
  193. static const struct {
  194. int cmd;
  195. char *level;
  196. char *name;
  197. } tty_ioctls[] = {
  198. /* don't print these, they flood the log ... */
  199. { TCGETS, NULL, "TCGETS" },
  200. { TCSETS, NULL, "TCSETS" },
  201. { TCSETSW, NULL, "TCSETSW" },
  202. { TCFLSH, NULL, "TCFLSH" },
  203. { TCSBRK, NULL, "TCSBRK" },
  204. /* general tty stuff */
  205. { TCSETSF, KERN_DEBUG, "TCSETSF" },
  206. { TCGETA, KERN_DEBUG, "TCGETA" },
  207. { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
  208. { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
  209. { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
  210. /* linux-specific ones */
  211. { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
  212. { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
  213. { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
  214. { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
  215. };
  216. int line_ioctl(struct tty_struct *tty, struct file * file,
  217. unsigned int cmd, unsigned long arg)
  218. {
  219. int ret;
  220. int i;
  221. ret = 0;
  222. switch(cmd) {
  223. #ifdef TIOCGETP
  224. case TIOCGETP:
  225. case TIOCSETP:
  226. case TIOCSETN:
  227. #endif
  228. #ifdef TIOCGETC
  229. case TIOCGETC:
  230. case TIOCSETC:
  231. #endif
  232. #ifdef TIOCGLTC
  233. case TIOCGLTC:
  234. case TIOCSLTC:
  235. #endif
  236. case TCGETS:
  237. case TCSETSF:
  238. case TCSETSW:
  239. case TCSETS:
  240. case TCGETA:
  241. case TCSETAF:
  242. case TCSETAW:
  243. case TCSETA:
  244. case TCXONC:
  245. case TCFLSH:
  246. case TIOCOUTQ:
  247. case TIOCINQ:
  248. case TIOCGLCKTRMIOS:
  249. case TIOCSLCKTRMIOS:
  250. case TIOCPKT:
  251. case TIOCGSOFTCAR:
  252. case TIOCSSOFTCAR:
  253. return -ENOIOCTLCMD;
  254. #if 0
  255. case TCwhatever:
  256. /* do something */
  257. break;
  258. #endif
  259. default:
  260. for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
  261. if (cmd == tty_ioctls[i].cmd)
  262. break;
  263. if (i == ARRAY_SIZE(tty_ioctls)) {
  264. printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
  265. __func__, tty->name, cmd);
  266. }
  267. ret = -ENOIOCTLCMD;
  268. break;
  269. }
  270. return ret;
  271. }
  272. void line_throttle(struct tty_struct *tty)
  273. {
  274. struct line *line = tty->driver_data;
  275. deactivate_chan(&line->chan_list, line->driver->read_irq);
  276. line->throttled = 1;
  277. }
  278. void line_unthrottle(struct tty_struct *tty)
  279. {
  280. struct line *line = tty->driver_data;
  281. line->throttled = 0;
  282. chan_interrupt(&line->chan_list, &line->task, tty,
  283. line->driver->read_irq);
  284. /*
  285. * Maybe there is enough stuff pending that calling the interrupt
  286. * throttles us again. In this case, line->throttled will be 1
  287. * again and we shouldn't turn the interrupt back on.
  288. */
  289. if (!line->throttled)
  290. reactivate_chan(&line->chan_list, line->driver->read_irq);
  291. }
  292. static irqreturn_t line_write_interrupt(int irq, void *data)
  293. {
  294. struct chan *chan = data;
  295. struct line *line = chan->line;
  296. struct tty_struct *tty = line->tty;
  297. int err;
  298. /*
  299. * Interrupts are disabled here because we registered the interrupt with
  300. * IRQF_DISABLED (see line_setup_irq).
  301. */
  302. spin_lock(&line->lock);
  303. err = flush_buffer(line);
  304. if (err == 0) {
  305. return IRQ_NONE;
  306. } else if (err < 0) {
  307. line->head = line->buffer;
  308. line->tail = line->buffer;
  309. }
  310. spin_unlock(&line->lock);
  311. if (tty == NULL)
  312. return IRQ_NONE;
  313. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
  314. (tty->ldisc.write_wakeup != NULL))
  315. (tty->ldisc.write_wakeup)(tty);
  316. /*
  317. * BLOCKING mode
  318. * In blocking mode, everything sleeps on tty->write_wait.
  319. * Sleeping in the console driver would break non-blocking
  320. * writes.
  321. */
  322. if (waitqueue_active(&tty->write_wait))
  323. wake_up_interruptible(&tty->write_wait);
  324. return IRQ_HANDLED;
  325. }
  326. int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
  327. {
  328. const struct line_driver *driver = line->driver;
  329. int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
  330. if (input)
  331. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  332. line_interrupt, flags,
  333. driver->read_irq_name, data);
  334. if (err)
  335. return err;
  336. if (output)
  337. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  338. line_write_interrupt, flags,
  339. driver->write_irq_name, data);
  340. line->have_irq = 1;
  341. return err;
  342. }
  343. /*
  344. * Normally, a driver like this can rely mostly on the tty layer
  345. * locking, particularly when it comes to the driver structure.
  346. * However, in this case, mconsole requests can come in "from the
  347. * side", and race with opens and closes.
  348. *
  349. * mconsole config requests will want to be sure the device isn't in
  350. * use, and get_config, open, and close will want a stable
  351. * configuration. The checking and modification of the configuration
  352. * is done under a spinlock. Checking whether the device is in use is
  353. * line->tty->count > 1, also under the spinlock.
  354. *
  355. * tty->count serves to decide whether the device should be enabled or
  356. * disabled on the host. If it's equal to 1, then we are doing the
  357. * first open or last close. Otherwise, open and close just return.
  358. */
  359. int line_open(struct line *lines, struct tty_struct *tty)
  360. {
  361. struct line *line = &lines[tty->index];
  362. int err = -ENODEV;
  363. spin_lock(&line->count_lock);
  364. if (!line->valid)
  365. goto out_unlock;
  366. err = 0;
  367. if (tty->count > 1)
  368. goto out_unlock;
  369. spin_unlock(&line->count_lock);
  370. tty->driver_data = line;
  371. line->tty = tty;
  372. err = enable_chan(line);
  373. if (err)
  374. return err;
  375. INIT_DELAYED_WORK(&line->task, line_timer_cb);
  376. if (!line->sigio) {
  377. chan_enable_winch(&line->chan_list, tty);
  378. line->sigio = 1;
  379. }
  380. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  381. &tty->winsize.ws_col);
  382. return err;
  383. out_unlock:
  384. spin_unlock(&line->count_lock);
  385. return err;
  386. }
  387. static void unregister_winch(struct tty_struct *tty);
  388. void line_close(struct tty_struct *tty, struct file * filp)
  389. {
  390. struct line *line = tty->driver_data;
  391. /*
  392. * If line_open fails (and tty->driver_data is never set),
  393. * tty_open will call line_close. So just return in this case.
  394. */
  395. if (line == NULL)
  396. return;
  397. /* We ignore the error anyway! */
  398. flush_buffer(line);
  399. spin_lock(&line->count_lock);
  400. if (!line->valid)
  401. goto out_unlock;
  402. if (tty->count > 1)
  403. goto out_unlock;
  404. spin_unlock(&line->count_lock);
  405. line->tty = NULL;
  406. tty->driver_data = NULL;
  407. if (line->sigio) {
  408. unregister_winch(tty);
  409. line->sigio = 0;
  410. }
  411. return;
  412. out_unlock:
  413. spin_unlock(&line->count_lock);
  414. }
  415. void close_lines(struct line *lines, int nlines)
  416. {
  417. int i;
  418. for(i = 0; i < nlines; i++)
  419. close_chan(&lines[i].chan_list, 0);
  420. }
  421. static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
  422. char **error_out)
  423. {
  424. struct line *line = &lines[n];
  425. int err = -EINVAL;
  426. spin_lock(&line->count_lock);
  427. if (line->tty != NULL) {
  428. *error_out = "Device is already open";
  429. goto out;
  430. }
  431. if (line->init_pri <= init_prio) {
  432. line->init_pri = init_prio;
  433. if (!strcmp(init, "none"))
  434. line->valid = 0;
  435. else {
  436. line->init_str = init;
  437. line->valid = 1;
  438. }
  439. }
  440. err = 0;
  441. out:
  442. spin_unlock(&line->count_lock);
  443. return err;
  444. }
  445. /*
  446. * Common setup code for both startup command line and mconsole initialization.
  447. * @lines contains the array (of size @num) to modify;
  448. * @init is the setup string;
  449. * @error_out is an error string in the case of failure;
  450. */
  451. int line_setup(struct line *lines, unsigned int num, char *init,
  452. char **error_out)
  453. {
  454. int i, n, err;
  455. char *end;
  456. if (*init == '=') {
  457. /*
  458. * We said con=/ssl= instead of con#=, so we are configuring all
  459. * consoles at once.
  460. */
  461. n = -1;
  462. }
  463. else {
  464. n = simple_strtoul(init, &end, 0);
  465. if (*end != '=') {
  466. *error_out = "Couldn't parse device number";
  467. return -EINVAL;
  468. }
  469. init = end;
  470. }
  471. init++;
  472. if (n >= (signed int) num) {
  473. *error_out = "Device number out of range";
  474. return -EINVAL;
  475. }
  476. else if (n >= 0) {
  477. err = setup_one_line(lines, n, init, INIT_ONE, error_out);
  478. if (err)
  479. return err;
  480. }
  481. else {
  482. for(i = 0; i < num; i++) {
  483. err = setup_one_line(lines, i, init, INIT_ALL,
  484. error_out);
  485. if (err)
  486. return err;
  487. }
  488. }
  489. return n == -1 ? num : n;
  490. }
  491. int line_config(struct line *lines, unsigned int num, char *str,
  492. const struct chan_opts *opts, char **error_out)
  493. {
  494. struct line *line;
  495. char *new;
  496. int n;
  497. if (*str == '=') {
  498. *error_out = "Can't configure all devices from mconsole";
  499. return -EINVAL;
  500. }
  501. new = kstrdup(str, GFP_KERNEL);
  502. if (new == NULL) {
  503. *error_out = "Failed to allocate memory";
  504. return -ENOMEM;
  505. }
  506. n = line_setup(lines, num, new, error_out);
  507. if (n < 0)
  508. return n;
  509. line = &lines[n];
  510. return parse_chan_pair(line->init_str, line, n, opts, error_out);
  511. }
  512. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  513. int size, char **error_out)
  514. {
  515. struct line *line;
  516. char *end;
  517. int dev, n = 0;
  518. dev = simple_strtoul(name, &end, 0);
  519. if ((*end != '\0') || (end == name)) {
  520. *error_out = "line_get_config failed to parse device number";
  521. return 0;
  522. }
  523. if ((dev < 0) || (dev >= num)) {
  524. *error_out = "device number out of range";
  525. return 0;
  526. }
  527. line = &lines[dev];
  528. spin_lock(&line->count_lock);
  529. if (!line->valid)
  530. CONFIG_CHUNK(str, size, n, "none", 1);
  531. else if (line->tty == NULL)
  532. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  533. else n = chan_config_string(&line->chan_list, str, size, error_out);
  534. spin_unlock(&line->count_lock);
  535. return n;
  536. }
  537. int line_id(char **str, int *start_out, int *end_out)
  538. {
  539. char *end;
  540. int n;
  541. n = simple_strtoul(*str, &end, 0);
  542. if ((*end != '\0') || (end == *str))
  543. return -1;
  544. *str = end;
  545. *start_out = n;
  546. *end_out = n;
  547. return n;
  548. }
  549. int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
  550. {
  551. int err;
  552. char config[sizeof("conxxxx=none\0")];
  553. sprintf(config, "%d=none", n);
  554. err = line_setup(lines, num, config, error_out);
  555. if (err >= 0)
  556. err = 0;
  557. return err;
  558. }
  559. struct tty_driver *register_lines(struct line_driver *line_driver,
  560. const struct tty_operations *ops,
  561. struct line *lines, int nlines)
  562. {
  563. int i;
  564. struct tty_driver *driver = alloc_tty_driver(nlines);
  565. if (!driver)
  566. return NULL;
  567. driver->driver_name = line_driver->name;
  568. driver->name = line_driver->device_name;
  569. driver->major = line_driver->major;
  570. driver->minor_start = line_driver->minor_start;
  571. driver->type = line_driver->type;
  572. driver->subtype = line_driver->subtype;
  573. driver->flags = TTY_DRIVER_REAL_RAW;
  574. driver->init_termios = tty_std_termios;
  575. tty_set_operations(driver, ops);
  576. if (tty_register_driver(driver)) {
  577. printk(KERN_ERR "register_lines : can't register %s driver\n",
  578. line_driver->name);
  579. put_tty_driver(driver);
  580. return NULL;
  581. }
  582. for(i = 0; i < nlines; i++) {
  583. if (!lines[i].valid)
  584. tty_unregister_device(driver, i);
  585. }
  586. mconsole_register_dev(&line_driver->mc);
  587. return driver;
  588. }
  589. static DEFINE_SPINLOCK(winch_handler_lock);
  590. static LIST_HEAD(winch_handlers);
  591. void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
  592. {
  593. struct line *line;
  594. char *error;
  595. int i;
  596. for(i = 0; i < nlines; i++) {
  597. line = &lines[i];
  598. INIT_LIST_HEAD(&line->chan_list);
  599. if (line->init_str == NULL)
  600. continue;
  601. line->init_str = kstrdup(line->init_str, GFP_KERNEL);
  602. if (line->init_str == NULL)
  603. printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
  604. if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
  605. printk(KERN_ERR "parse_chan_pair failed for "
  606. "device %d : %s\n", i, error);
  607. line->valid = 0;
  608. }
  609. }
  610. }
  611. struct winch {
  612. struct list_head list;
  613. int fd;
  614. int tty_fd;
  615. int pid;
  616. struct tty_struct *tty;
  617. unsigned long stack;
  618. };
  619. static void free_winch(struct winch *winch, int free_irq_ok)
  620. {
  621. list_del(&winch->list);
  622. if (winch->pid != -1)
  623. os_kill_process(winch->pid, 1);
  624. if (winch->fd != -1)
  625. os_close_file(winch->fd);
  626. if (winch->stack != 0)
  627. free_stack(winch->stack, 0);
  628. if (free_irq_ok)
  629. free_irq(WINCH_IRQ, winch);
  630. kfree(winch);
  631. }
  632. static irqreturn_t winch_interrupt(int irq, void *data)
  633. {
  634. struct winch *winch = data;
  635. struct tty_struct *tty;
  636. struct line *line;
  637. int err;
  638. char c;
  639. if (winch->fd != -1) {
  640. err = generic_read(winch->fd, &c, NULL);
  641. if (err < 0) {
  642. if (err != -EAGAIN) {
  643. printk(KERN_ERR "winch_interrupt : "
  644. "read failed, errno = %d\n", -err);
  645. printk(KERN_ERR "fd %d is losing SIGWINCH "
  646. "support\n", winch->tty_fd);
  647. free_winch(winch, 0);
  648. return IRQ_HANDLED;
  649. }
  650. goto out;
  651. }
  652. }
  653. tty = winch->tty;
  654. if (tty != NULL) {
  655. line = tty->driver_data;
  656. if (line != NULL) {
  657. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  658. &tty->winsize.ws_col);
  659. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  660. }
  661. }
  662. out:
  663. if (winch->fd != -1)
  664. reactivate_fd(winch->fd, WINCH_IRQ);
  665. return IRQ_HANDLED;
  666. }
  667. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
  668. unsigned long stack)
  669. {
  670. struct winch *winch;
  671. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  672. if (winch == NULL) {
  673. printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
  674. goto cleanup;
  675. }
  676. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  677. .fd = fd,
  678. .tty_fd = tty_fd,
  679. .pid = pid,
  680. .tty = tty,
  681. .stack = stack });
  682. if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  683. IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  684. "winch", winch) < 0) {
  685. printk(KERN_ERR "register_winch_irq - failed to register "
  686. "IRQ\n");
  687. goto out_free;
  688. }
  689. spin_lock(&winch_handler_lock);
  690. list_add(&winch->list, &winch_handlers);
  691. spin_unlock(&winch_handler_lock);
  692. return;
  693. out_free:
  694. kfree(winch);
  695. cleanup:
  696. os_kill_process(pid, 1);
  697. os_close_file(fd);
  698. if (stack != 0)
  699. free_stack(stack, 0);
  700. }
  701. static void unregister_winch(struct tty_struct *tty)
  702. {
  703. struct list_head *ele;
  704. struct winch *winch;
  705. spin_lock(&winch_handler_lock);
  706. list_for_each(ele, &winch_handlers) {
  707. winch = list_entry(ele, struct winch, list);
  708. if (winch->tty == tty) {
  709. free_winch(winch, 1);
  710. break;
  711. }
  712. }
  713. spin_unlock(&winch_handler_lock);
  714. }
  715. static void winch_cleanup(void)
  716. {
  717. struct list_head *ele, *next;
  718. struct winch *winch;
  719. spin_lock(&winch_handler_lock);
  720. list_for_each_safe(ele, next, &winch_handlers) {
  721. winch = list_entry(ele, struct winch, list);
  722. free_winch(winch, 1);
  723. }
  724. spin_unlock(&winch_handler_lock);
  725. }
  726. __uml_exitcall(winch_cleanup);
  727. char *add_xterm_umid(char *base)
  728. {
  729. char *umid, *title;
  730. int len;
  731. umid = get_umid();
  732. if (*umid == '\0')
  733. return base;
  734. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  735. title = kmalloc(len, GFP_KERNEL);
  736. if (title == NULL) {
  737. printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
  738. return base;
  739. }
  740. snprintf(title, len, "%s (%s)", base, umid);
  741. return title;
  742. }