line.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 "asm/uaccess.h"
  11. #include "chan_kern.h"
  12. #include "irq_user.h"
  13. #include "line.h"
  14. #include "kern.h"
  15. #include "user_util.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(void *arg)
  30. {
  31. struct line *line = arg;
  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. //return 0;
  162. spin_lock_irqsave(&line->lock, flags);
  163. err = flush_buffer(line);
  164. /*if (err == 1)
  165. err = 0;*/
  166. spin_unlock_irqrestore(&line->lock, flags);
  167. //return err;
  168. }
  169. /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
  170. * and ->write. Hope it's not that bad.*/
  171. void line_flush_chars(struct tty_struct *tty)
  172. {
  173. line_flush_buffer(tty);
  174. }
  175. void line_put_char(struct tty_struct *tty, unsigned char ch)
  176. {
  177. line_write(tty, &ch, sizeof(ch));
  178. }
  179. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  180. {
  181. struct line *line = tty->driver_data;
  182. unsigned long flags;
  183. int n, err, ret = 0;
  184. if(tty->stopped)
  185. return 0;
  186. spin_lock_irqsave(&line->lock, flags);
  187. if (line->head != line->tail) {
  188. ret = buffer_data(line, buf, len);
  189. err = flush_buffer(line);
  190. if (err <= 0 && (err != -EAGAIN || !ret))
  191. ret = err;
  192. } else {
  193. n = write_chan(&line->chan_list, buf, len,
  194. line->driver->write_irq);
  195. if (n < 0) {
  196. ret = n;
  197. goto out_up;
  198. }
  199. len -= n;
  200. ret += n;
  201. if (len > 0)
  202. ret += buffer_data(line, buf + n, len);
  203. }
  204. out_up:
  205. spin_unlock_irqrestore(&line->lock, flags);
  206. return ret;
  207. }
  208. void line_set_termios(struct tty_struct *tty, struct termios * old)
  209. {
  210. /* nothing */
  211. }
  212. static const struct {
  213. int cmd;
  214. char *level;
  215. char *name;
  216. } tty_ioctls[] = {
  217. /* don't print these, they flood the log ... */
  218. { TCGETS, NULL, "TCGETS" },
  219. { TCSETS, NULL, "TCSETS" },
  220. { TCSETSW, NULL, "TCSETSW" },
  221. { TCFLSH, NULL, "TCFLSH" },
  222. { TCSBRK, NULL, "TCSBRK" },
  223. /* general tty stuff */
  224. { TCSETSF, KERN_DEBUG, "TCSETSF" },
  225. { TCGETA, KERN_DEBUG, "TCGETA" },
  226. { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
  227. { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
  228. { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
  229. /* linux-specific ones */
  230. { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
  231. { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
  232. { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
  233. { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
  234. };
  235. int line_ioctl(struct tty_struct *tty, struct file * file,
  236. unsigned int cmd, unsigned long arg)
  237. {
  238. int ret;
  239. int i;
  240. ret = 0;
  241. switch(cmd) {
  242. #ifdef TIOCGETP
  243. case TIOCGETP:
  244. case TIOCSETP:
  245. case TIOCSETN:
  246. #endif
  247. #ifdef TIOCGETC
  248. case TIOCGETC:
  249. case TIOCSETC:
  250. #endif
  251. #ifdef TIOCGLTC
  252. case TIOCGLTC:
  253. case TIOCSLTC:
  254. #endif
  255. case TCGETS:
  256. case TCSETSF:
  257. case TCSETSW:
  258. case TCSETS:
  259. case TCGETA:
  260. case TCSETAF:
  261. case TCSETAW:
  262. case TCSETA:
  263. case TCXONC:
  264. case TCFLSH:
  265. case TIOCOUTQ:
  266. case TIOCINQ:
  267. case TIOCGLCKTRMIOS:
  268. case TIOCSLCKTRMIOS:
  269. case TIOCPKT:
  270. case TIOCGSOFTCAR:
  271. case TIOCSSOFTCAR:
  272. return -ENOIOCTLCMD;
  273. #if 0
  274. case TCwhatever:
  275. /* do something */
  276. break;
  277. #endif
  278. default:
  279. for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
  280. if (cmd == tty_ioctls[i].cmd)
  281. break;
  282. if (i < ARRAY_SIZE(tty_ioctls)) {
  283. if (NULL != tty_ioctls[i].level)
  284. printk("%s%s: %s: ioctl %s called\n",
  285. tty_ioctls[i].level, __FUNCTION__,
  286. tty->name, tty_ioctls[i].name);
  287. } else {
  288. printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
  289. __FUNCTION__, tty->name, cmd);
  290. }
  291. ret = -ENOIOCTLCMD;
  292. break;
  293. }
  294. return ret;
  295. }
  296. void line_throttle(struct tty_struct *tty)
  297. {
  298. struct line *line = tty->driver_data;
  299. deactivate_chan(&line->chan_list, line->driver->read_irq);
  300. line->throttled = 1;
  301. }
  302. void line_unthrottle(struct tty_struct *tty)
  303. {
  304. struct line *line = tty->driver_data;
  305. line->throttled = 0;
  306. chan_interrupt(&line->chan_list, &line->task, tty,
  307. line->driver->read_irq);
  308. /* Maybe there is enough stuff pending that calling the interrupt
  309. * throttles us again. In this case, line->throttled will be 1
  310. * again and we shouldn't turn the interrupt back on.
  311. */
  312. if(!line->throttled)
  313. reactivate_chan(&line->chan_list, line->driver->read_irq);
  314. }
  315. static irqreturn_t line_write_interrupt(int irq, void *data)
  316. {
  317. struct chan *chan = data;
  318. struct line *line = chan->line;
  319. struct tty_struct *tty = line->tty;
  320. int err;
  321. /* Interrupts are enabled here because we registered the interrupt with
  322. * IRQF_DISABLED (see line_setup_irq).*/
  323. spin_lock_irq(&line->lock);
  324. err = flush_buffer(line);
  325. if (err == 0) {
  326. return IRQ_NONE;
  327. } else if(err < 0) {
  328. line->head = line->buffer;
  329. line->tail = line->buffer;
  330. }
  331. spin_unlock_irq(&line->lock);
  332. if(tty == NULL)
  333. return IRQ_NONE;
  334. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
  335. (tty->ldisc.write_wakeup != NULL))
  336. (tty->ldisc.write_wakeup)(tty);
  337. /* BLOCKING mode
  338. * In blocking mode, everything sleeps on tty->write_wait.
  339. * Sleeping in the console driver would break non-blocking
  340. * writes.
  341. */
  342. if (waitqueue_active(&tty->write_wait))
  343. wake_up_interruptible(&tty->write_wait);
  344. return IRQ_HANDLED;
  345. }
  346. int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
  347. {
  348. const struct line_driver *driver = line->driver;
  349. int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
  350. if (input)
  351. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  352. line_interrupt, flags,
  353. driver->read_irq_name, data);
  354. if (err)
  355. return err;
  356. if (output)
  357. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  358. line_write_interrupt, flags,
  359. driver->write_irq_name, data);
  360. line->have_irq = 1;
  361. return err;
  362. }
  363. int line_open(struct line *lines, struct tty_struct *tty)
  364. {
  365. struct line *line;
  366. int err = -ENODEV;
  367. line = &lines[tty->index];
  368. tty->driver_data = line;
  369. /* The IRQ which takes this lock is not yet enabled and won't be run
  370. * before the end, so we don't need to use spin_lock_irq.*/
  371. spin_lock(&line->lock);
  372. tty->driver_data = line;
  373. line->tty = tty;
  374. if(!line->valid)
  375. goto out;
  376. if(tty->count == 1){
  377. /* Here the device is opened, if necessary, and interrupt
  378. * is registered.
  379. */
  380. enable_chan(line);
  381. INIT_WORK(&line->task, line_timer_cb, line);
  382. if(!line->sigio){
  383. chan_enable_winch(&line->chan_list, tty);
  384. line->sigio = 1;
  385. }
  386. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  387. &tty->winsize.ws_col);
  388. }
  389. err = 0;
  390. out:
  391. spin_unlock(&line->lock);
  392. return err;
  393. }
  394. static void unregister_winch(struct tty_struct *tty);
  395. void line_close(struct tty_struct *tty, struct file * filp)
  396. {
  397. struct line *line = tty->driver_data;
  398. /* XXX: I assume this should be called in process context, not with
  399. * interrupts disabled!
  400. */
  401. spin_lock_irq(&line->lock);
  402. /* We ignore the error anyway! */
  403. flush_buffer(line);
  404. if(tty->count == 1){
  405. line->tty = NULL;
  406. tty->driver_data = NULL;
  407. if(line->sigio){
  408. unregister_winch(tty);
  409. line->sigio = 0;
  410. }
  411. }
  412. spin_unlock_irq(&line->lock);
  413. }
  414. void close_lines(struct line *lines, int nlines)
  415. {
  416. int i;
  417. for(i = 0; i < nlines; i++)
  418. close_chan(&lines[i].chan_list, 0);
  419. }
  420. /* Common setup code for both startup command line and mconsole initialization.
  421. * @lines contains the array (of size @num) to modify;
  422. * @init is the setup string;
  423. */
  424. int line_setup(struct line *lines, unsigned int num, char *init)
  425. {
  426. int i, n;
  427. char *end;
  428. if(*init == '=') {
  429. /* We said con=/ssl= instead of con#=, so we are configuring all
  430. * consoles at once.*/
  431. n = -1;
  432. }
  433. else {
  434. n = simple_strtoul(init, &end, 0);
  435. if(*end != '='){
  436. printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
  437. init);
  438. return 0;
  439. }
  440. init = end;
  441. }
  442. init++;
  443. if (n >= (signed int) num) {
  444. printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
  445. n, num - 1);
  446. return 0;
  447. }
  448. else if (n >= 0){
  449. if (lines[n].tty != NULL) {
  450. printk("line_setup - device %d is open\n", n);
  451. return 0;
  452. }
  453. if (lines[n].init_pri <= INIT_ONE){
  454. lines[n].init_pri = INIT_ONE;
  455. if (!strcmp(init, "none"))
  456. lines[n].valid = 0;
  457. else {
  458. lines[n].init_str = init;
  459. lines[n].valid = 1;
  460. }
  461. }
  462. }
  463. else {
  464. for(i = 0; i < num; i++){
  465. if(lines[i].init_pri <= INIT_ALL){
  466. lines[i].init_pri = INIT_ALL;
  467. if(!strcmp(init, "none")) lines[i].valid = 0;
  468. else {
  469. lines[i].init_str = init;
  470. lines[i].valid = 1;
  471. }
  472. }
  473. }
  474. }
  475. return n == -1 ? num : n;
  476. }
  477. int line_config(struct line *lines, unsigned int num, char *str,
  478. const struct chan_opts *opts)
  479. {
  480. struct line *line;
  481. char *new;
  482. int n;
  483. if(*str == '='){
  484. printk("line_config - can't configure all devices from "
  485. "mconsole\n");
  486. return 1;
  487. }
  488. new = kstrdup(str, GFP_KERNEL);
  489. if(new == NULL){
  490. printk("line_config - kstrdup failed\n");
  491. return 1;
  492. }
  493. n = line_setup(lines, num, new);
  494. if(n < 0)
  495. return 1;
  496. line = &lines[n];
  497. return parse_chan_pair(line->init_str, line, n, opts);
  498. }
  499. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  500. int size, char **error_out)
  501. {
  502. struct line *line;
  503. char *end;
  504. int dev, n = 0;
  505. dev = simple_strtoul(name, &end, 0);
  506. if((*end != '\0') || (end == name)){
  507. *error_out = "line_get_config failed to parse device number";
  508. return 0;
  509. }
  510. if((dev < 0) || (dev >= num)){
  511. *error_out = "device number out of range";
  512. return 0;
  513. }
  514. line = &lines[dev];
  515. spin_lock(&line->lock);
  516. if(!line->valid)
  517. CONFIG_CHUNK(str, size, n, "none", 1);
  518. else if(line->tty == NULL)
  519. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  520. else n = chan_config_string(&line->chan_list, str, size, error_out);
  521. spin_unlock(&line->lock);
  522. return n;
  523. }
  524. int line_id(char **str, int *start_out, int *end_out)
  525. {
  526. char *end;
  527. int n;
  528. n = simple_strtoul(*str, &end, 0);
  529. if((*end != '\0') || (end == *str))
  530. return -1;
  531. *str = end;
  532. *start_out = n;
  533. *end_out = n;
  534. return n;
  535. }
  536. int line_remove(struct line *lines, unsigned int num, int n)
  537. {
  538. int err;
  539. char config[sizeof("conxxxx=none\0")];
  540. sprintf(config, "%d=none", n);
  541. err = line_setup(lines, num, config);
  542. if(err >= 0)
  543. err = 0;
  544. return err;
  545. }
  546. struct tty_driver *line_register_devfs(struct lines *set,
  547. struct line_driver *line_driver,
  548. const struct tty_operations *ops,
  549. struct line *lines, int nlines)
  550. {
  551. int i;
  552. struct tty_driver *driver = alloc_tty_driver(nlines);
  553. if (!driver)
  554. return NULL;
  555. driver->driver_name = line_driver->name;
  556. driver->name = line_driver->device_name;
  557. driver->major = line_driver->major;
  558. driver->minor_start = line_driver->minor_start;
  559. driver->type = line_driver->type;
  560. driver->subtype = line_driver->subtype;
  561. driver->flags = TTY_DRIVER_REAL_RAW;
  562. driver->init_termios = tty_std_termios;
  563. tty_set_operations(driver, ops);
  564. if (tty_register_driver(driver)) {
  565. printk("%s: can't register %s driver\n",
  566. __FUNCTION__,line_driver->name);
  567. put_tty_driver(driver);
  568. return NULL;
  569. }
  570. for(i = 0; i < nlines; i++){
  571. if(!lines[i].valid)
  572. tty_unregister_device(driver, i);
  573. }
  574. mconsole_register_dev(&line_driver->mc);
  575. return driver;
  576. }
  577. static DEFINE_SPINLOCK(winch_handler_lock);
  578. static LIST_HEAD(winch_handlers);
  579. void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
  580. {
  581. struct line *line;
  582. int i;
  583. for(i = 0; i < nlines; i++){
  584. line = &lines[i];
  585. INIT_LIST_HEAD(&line->chan_list);
  586. if(line->init_str == NULL)
  587. continue;
  588. line->init_str = kstrdup(line->init_str, GFP_KERNEL);
  589. if(line->init_str == NULL)
  590. printk("lines_init - kstrdup returned NULL\n");
  591. if(parse_chan_pair(line->init_str, line, i, opts)){
  592. printk("parse_chan_pair failed for device %d\n", i);
  593. line->valid = 0;
  594. }
  595. }
  596. }
  597. struct winch {
  598. struct list_head list;
  599. int fd;
  600. int tty_fd;
  601. int pid;
  602. struct tty_struct *tty;
  603. };
  604. static irqreturn_t winch_interrupt(int irq, void *data)
  605. {
  606. struct winch *winch = data;
  607. struct tty_struct *tty;
  608. struct line *line;
  609. int err;
  610. char c;
  611. if(winch->fd != -1){
  612. err = generic_read(winch->fd, &c, NULL);
  613. if(err < 0){
  614. if(err != -EAGAIN){
  615. printk("winch_interrupt : read failed, "
  616. "errno = %d\n", -err);
  617. printk("fd %d is losing SIGWINCH support\n",
  618. winch->tty_fd);
  619. return IRQ_HANDLED;
  620. }
  621. goto out;
  622. }
  623. }
  624. tty = winch->tty;
  625. if (tty != NULL) {
  626. line = tty->driver_data;
  627. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  628. &tty->winsize.ws_col);
  629. kill_pg(tty->pgrp, SIGWINCH, 1);
  630. }
  631. out:
  632. if(winch->fd != -1)
  633. reactivate_fd(winch->fd, WINCH_IRQ);
  634. return IRQ_HANDLED;
  635. }
  636. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
  637. {
  638. struct winch *winch;
  639. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  640. if (winch == NULL) {
  641. printk("register_winch_irq - kmalloc failed\n");
  642. return;
  643. }
  644. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  645. .fd = fd,
  646. .tty_fd = tty_fd,
  647. .pid = pid,
  648. .tty = tty });
  649. spin_lock(&winch_handler_lock);
  650. list_add(&winch->list, &winch_handlers);
  651. spin_unlock(&winch_handler_lock);
  652. if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  653. IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  654. "winch", winch) < 0)
  655. printk("register_winch_irq - failed to register IRQ\n");
  656. }
  657. static void free_winch(struct winch *winch)
  658. {
  659. list_del(&winch->list);
  660. if(winch->pid != -1)
  661. os_kill_process(winch->pid, 1);
  662. if(winch->fd != -1)
  663. os_close_file(winch->fd);
  664. free_irq(WINCH_IRQ, winch);
  665. kfree(winch);
  666. }
  667. static void unregister_winch(struct tty_struct *tty)
  668. {
  669. struct list_head *ele;
  670. struct winch *winch;
  671. spin_lock(&winch_handler_lock);
  672. list_for_each(ele, &winch_handlers){
  673. winch = list_entry(ele, struct winch, list);
  674. if(winch->tty == tty){
  675. free_winch(winch);
  676. break;
  677. }
  678. }
  679. spin_unlock(&winch_handler_lock);
  680. }
  681. static void winch_cleanup(void)
  682. {
  683. struct list_head *ele, *next;
  684. struct winch *winch;
  685. spin_lock(&winch_handler_lock);
  686. list_for_each_safe(ele, next, &winch_handlers){
  687. winch = list_entry(ele, struct winch, list);
  688. free_winch(winch);
  689. }
  690. spin_unlock(&winch_handler_lock);
  691. }
  692. __uml_exitcall(winch_cleanup);
  693. char *add_xterm_umid(char *base)
  694. {
  695. char *umid, *title;
  696. int len;
  697. umid = get_umid();
  698. if(*umid == '\0')
  699. return base;
  700. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  701. title = kmalloc(len, GFP_KERNEL);
  702. if(title == NULL){
  703. printk("Failed to allocate buffer for xterm title\n");
  704. return base;
  705. }
  706. snprintf(title, len, "%s (%s)", base, umid);
  707. return title;
  708. }