line.c 20 KB

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