line.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. tty_wakeup(tty);
  314. return IRQ_HANDLED;
  315. }
  316. int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
  317. {
  318. const struct line_driver *driver = line->driver;
  319. int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
  320. if (input)
  321. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  322. line_interrupt, flags,
  323. driver->read_irq_name, data);
  324. if (err)
  325. return err;
  326. if (output)
  327. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  328. line_write_interrupt, flags,
  329. driver->write_irq_name, data);
  330. line->have_irq = 1;
  331. return err;
  332. }
  333. /*
  334. * Normally, a driver like this can rely mostly on the tty layer
  335. * locking, particularly when it comes to the driver structure.
  336. * However, in this case, mconsole requests can come in "from the
  337. * side", and race with opens and closes.
  338. *
  339. * mconsole config requests will want to be sure the device isn't in
  340. * use, and get_config, open, and close will want a stable
  341. * configuration. The checking and modification of the configuration
  342. * is done under a spinlock. Checking whether the device is in use is
  343. * line->tty->count > 1, also under the spinlock.
  344. *
  345. * tty->count serves to decide whether the device should be enabled or
  346. * disabled on the host. If it's equal to 1, then we are doing the
  347. * first open or last close. Otherwise, open and close just return.
  348. */
  349. int line_open(struct line *lines, struct tty_struct *tty)
  350. {
  351. struct line *line = &lines[tty->index];
  352. int err = -ENODEV;
  353. spin_lock(&line->count_lock);
  354. if (!line->valid)
  355. goto out_unlock;
  356. err = 0;
  357. if (tty->count > 1)
  358. goto out_unlock;
  359. spin_unlock(&line->count_lock);
  360. tty->driver_data = line;
  361. line->tty = tty;
  362. err = enable_chan(line);
  363. if (err)
  364. return err;
  365. INIT_DELAYED_WORK(&line->task, line_timer_cb);
  366. if (!line->sigio) {
  367. chan_enable_winch(&line->chan_list, tty);
  368. line->sigio = 1;
  369. }
  370. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  371. &tty->winsize.ws_col);
  372. return err;
  373. out_unlock:
  374. spin_unlock(&line->count_lock);
  375. return err;
  376. }
  377. static void unregister_winch(struct tty_struct *tty);
  378. void line_close(struct tty_struct *tty, struct file * filp)
  379. {
  380. struct line *line = tty->driver_data;
  381. /*
  382. * If line_open fails (and tty->driver_data is never set),
  383. * tty_open will call line_close. So just return in this case.
  384. */
  385. if (line == NULL)
  386. return;
  387. /* We ignore the error anyway! */
  388. flush_buffer(line);
  389. spin_lock(&line->count_lock);
  390. if (!line->valid)
  391. goto out_unlock;
  392. if (tty->count > 1)
  393. goto out_unlock;
  394. spin_unlock(&line->count_lock);
  395. line->tty = NULL;
  396. tty->driver_data = NULL;
  397. if (line->sigio) {
  398. unregister_winch(tty);
  399. line->sigio = 0;
  400. }
  401. return;
  402. out_unlock:
  403. spin_unlock(&line->count_lock);
  404. }
  405. void close_lines(struct line *lines, int nlines)
  406. {
  407. int i;
  408. for(i = 0; i < nlines; i++)
  409. close_chan(&lines[i].chan_list, 0);
  410. }
  411. static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
  412. char **error_out)
  413. {
  414. struct line *line = &lines[n];
  415. int err = -EINVAL;
  416. spin_lock(&line->count_lock);
  417. if (line->tty != NULL) {
  418. *error_out = "Device is already open";
  419. goto out;
  420. }
  421. if (line->init_pri <= init_prio) {
  422. line->init_pri = init_prio;
  423. if (!strcmp(init, "none"))
  424. line->valid = 0;
  425. else {
  426. line->init_str = init;
  427. line->valid = 1;
  428. }
  429. }
  430. err = 0;
  431. out:
  432. spin_unlock(&line->count_lock);
  433. return err;
  434. }
  435. /*
  436. * Common setup code for both startup command line and mconsole initialization.
  437. * @lines contains the array (of size @num) to modify;
  438. * @init is the setup string;
  439. * @error_out is an error string in the case of failure;
  440. */
  441. int line_setup(struct line *lines, unsigned int num, char *init,
  442. char **error_out)
  443. {
  444. int i, n, err;
  445. char *end;
  446. if (*init == '=') {
  447. /*
  448. * We said con=/ssl= instead of con#=, so we are configuring all
  449. * consoles at once.
  450. */
  451. n = -1;
  452. }
  453. else {
  454. n = simple_strtoul(init, &end, 0);
  455. if (*end != '=') {
  456. *error_out = "Couldn't parse device number";
  457. return -EINVAL;
  458. }
  459. init = end;
  460. }
  461. init++;
  462. if (n >= (signed int) num) {
  463. *error_out = "Device number out of range";
  464. return -EINVAL;
  465. }
  466. else if (n >= 0) {
  467. err = setup_one_line(lines, n, init, INIT_ONE, error_out);
  468. if (err)
  469. return err;
  470. }
  471. else {
  472. for(i = 0; i < num; i++) {
  473. err = setup_one_line(lines, i, init, INIT_ALL,
  474. error_out);
  475. if (err)
  476. return err;
  477. }
  478. }
  479. return n == -1 ? num : n;
  480. }
  481. int line_config(struct line *lines, unsigned int num, char *str,
  482. const struct chan_opts *opts, char **error_out)
  483. {
  484. struct line *line;
  485. char *new;
  486. int n;
  487. if (*str == '=') {
  488. *error_out = "Can't configure all devices from mconsole";
  489. return -EINVAL;
  490. }
  491. new = kstrdup(str, GFP_KERNEL);
  492. if (new == NULL) {
  493. *error_out = "Failed to allocate memory";
  494. return -ENOMEM;
  495. }
  496. n = line_setup(lines, num, new, error_out);
  497. if (n < 0)
  498. return n;
  499. line = &lines[n];
  500. return parse_chan_pair(line->init_str, line, n, opts, error_out);
  501. }
  502. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  503. int size, char **error_out)
  504. {
  505. struct line *line;
  506. char *end;
  507. int dev, n = 0;
  508. dev = simple_strtoul(name, &end, 0);
  509. if ((*end != '\0') || (end == name)) {
  510. *error_out = "line_get_config failed to parse device number";
  511. return 0;
  512. }
  513. if ((dev < 0) || (dev >= num)) {
  514. *error_out = "device number out of range";
  515. return 0;
  516. }
  517. line = &lines[dev];
  518. spin_lock(&line->count_lock);
  519. if (!line->valid)
  520. CONFIG_CHUNK(str, size, n, "none", 1);
  521. else if (line->tty == NULL)
  522. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  523. else n = chan_config_string(&line->chan_list, str, size, error_out);
  524. spin_unlock(&line->count_lock);
  525. return n;
  526. }
  527. int line_id(char **str, int *start_out, int *end_out)
  528. {
  529. char *end;
  530. int n;
  531. n = simple_strtoul(*str, &end, 0);
  532. if ((*end != '\0') || (end == *str))
  533. return -1;
  534. *str = end;
  535. *start_out = n;
  536. *end_out = n;
  537. return n;
  538. }
  539. int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
  540. {
  541. int err;
  542. char config[sizeof("conxxxx=none\0")];
  543. sprintf(config, "%d=none", n);
  544. err = line_setup(lines, num, config, error_out);
  545. if (err >= 0)
  546. err = 0;
  547. return err;
  548. }
  549. struct tty_driver *register_lines(struct line_driver *line_driver,
  550. const struct tty_operations *ops,
  551. struct line *lines, int nlines)
  552. {
  553. int i;
  554. struct tty_driver *driver = alloc_tty_driver(nlines);
  555. if (!driver)
  556. return NULL;
  557. driver->driver_name = line_driver->name;
  558. driver->name = line_driver->device_name;
  559. driver->major = line_driver->major;
  560. driver->minor_start = line_driver->minor_start;
  561. driver->type = line_driver->type;
  562. driver->subtype = line_driver->subtype;
  563. driver->flags = TTY_DRIVER_REAL_RAW;
  564. driver->init_termios = tty_std_termios;
  565. tty_set_operations(driver, ops);
  566. if (tty_register_driver(driver)) {
  567. printk(KERN_ERR "register_lines : can't register %s driver\n",
  568. line_driver->name);
  569. put_tty_driver(driver);
  570. return NULL;
  571. }
  572. for(i = 0; i < nlines; i++) {
  573. if (!lines[i].valid)
  574. tty_unregister_device(driver, i);
  575. }
  576. mconsole_register_dev(&line_driver->mc);
  577. return driver;
  578. }
  579. static DEFINE_SPINLOCK(winch_handler_lock);
  580. static LIST_HEAD(winch_handlers);
  581. void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
  582. {
  583. struct line *line;
  584. char *error;
  585. int i;
  586. for(i = 0; i < nlines; i++) {
  587. line = &lines[i];
  588. INIT_LIST_HEAD(&line->chan_list);
  589. if (line->init_str == NULL)
  590. continue;
  591. line->init_str = kstrdup(line->init_str, GFP_KERNEL);
  592. if (line->init_str == NULL)
  593. printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
  594. if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
  595. printk(KERN_ERR "parse_chan_pair failed for "
  596. "device %d : %s\n", i, error);
  597. line->valid = 0;
  598. }
  599. }
  600. }
  601. struct winch {
  602. struct list_head list;
  603. int fd;
  604. int tty_fd;
  605. int pid;
  606. struct tty_struct *tty;
  607. unsigned long stack;
  608. };
  609. static void free_winch(struct winch *winch, int free_irq_ok)
  610. {
  611. list_del(&winch->list);
  612. if (winch->pid != -1)
  613. os_kill_process(winch->pid, 1);
  614. if (winch->fd != -1)
  615. os_close_file(winch->fd);
  616. if (winch->stack != 0)
  617. free_stack(winch->stack, 0);
  618. if (free_irq_ok)
  619. free_irq(WINCH_IRQ, winch);
  620. kfree(winch);
  621. }
  622. static irqreturn_t winch_interrupt(int irq, void *data)
  623. {
  624. struct winch *winch = data;
  625. struct tty_struct *tty;
  626. struct line *line;
  627. int err;
  628. char c;
  629. if (winch->fd != -1) {
  630. err = generic_read(winch->fd, &c, NULL);
  631. if (err < 0) {
  632. if (err != -EAGAIN) {
  633. printk(KERN_ERR "winch_interrupt : "
  634. "read failed, errno = %d\n", -err);
  635. printk(KERN_ERR "fd %d is losing SIGWINCH "
  636. "support\n", winch->tty_fd);
  637. free_winch(winch, 0);
  638. return IRQ_HANDLED;
  639. }
  640. goto out;
  641. }
  642. }
  643. tty = winch->tty;
  644. if (tty != NULL) {
  645. line = tty->driver_data;
  646. if (line != NULL) {
  647. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  648. &tty->winsize.ws_col);
  649. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  650. }
  651. }
  652. out:
  653. if (winch->fd != -1)
  654. reactivate_fd(winch->fd, WINCH_IRQ);
  655. return IRQ_HANDLED;
  656. }
  657. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
  658. unsigned long stack)
  659. {
  660. struct winch *winch;
  661. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  662. if (winch == NULL) {
  663. printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
  664. goto cleanup;
  665. }
  666. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  667. .fd = fd,
  668. .tty_fd = tty_fd,
  669. .pid = pid,
  670. .tty = tty,
  671. .stack = stack });
  672. if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  673. IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  674. "winch", winch) < 0) {
  675. printk(KERN_ERR "register_winch_irq - failed to register "
  676. "IRQ\n");
  677. goto out_free;
  678. }
  679. spin_lock(&winch_handler_lock);
  680. list_add(&winch->list, &winch_handlers);
  681. spin_unlock(&winch_handler_lock);
  682. return;
  683. out_free:
  684. kfree(winch);
  685. cleanup:
  686. os_kill_process(pid, 1);
  687. os_close_file(fd);
  688. if (stack != 0)
  689. free_stack(stack, 0);
  690. }
  691. static void unregister_winch(struct tty_struct *tty)
  692. {
  693. struct list_head *ele;
  694. struct winch *winch;
  695. spin_lock(&winch_handler_lock);
  696. list_for_each(ele, &winch_handlers) {
  697. winch = list_entry(ele, struct winch, list);
  698. if (winch->tty == tty) {
  699. free_winch(winch, 1);
  700. break;
  701. }
  702. }
  703. spin_unlock(&winch_handler_lock);
  704. }
  705. static void winch_cleanup(void)
  706. {
  707. struct list_head *ele, *next;
  708. struct winch *winch;
  709. spin_lock(&winch_handler_lock);
  710. list_for_each_safe(ele, next, &winch_handlers) {
  711. winch = list_entry(ele, struct winch, list);
  712. free_winch(winch, 1);
  713. }
  714. spin_unlock(&winch_handler_lock);
  715. }
  716. __uml_exitcall(winch_cleanup);
  717. char *add_xterm_umid(char *base)
  718. {
  719. char *umid, *title;
  720. int len;
  721. umid = get_umid();
  722. if (*umid == '\0')
  723. return base;
  724. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  725. title = kmalloc(len, GFP_KERNEL);
  726. if (title == NULL) {
  727. printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
  728. return base;
  729. }
  730. snprintf(title, len, "%s (%s)", base, umid);
  731. return title;
  732. }