line.c 18 KB

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