line.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 "kern_util.h"
  16. #include "os.h"
  17. #include "irq_kern.h"
  18. #define LINE_BUFSIZE 4096
  19. static irqreturn_t line_interrupt(int irq, void *data)
  20. {
  21. struct chan *chan = data;
  22. struct line *line = chan->line;
  23. struct tty_struct *tty = line->tty;
  24. if (line)
  25. chan_interrupt(&line->chan_list, &line->task, tty, irq);
  26. return IRQ_HANDLED;
  27. }
  28. static void line_timer_cb(struct work_struct *work)
  29. {
  30. struct line *line = container_of(work, struct line, task.work);
  31. if(!line->throttled)
  32. chan_interrupt(&line->chan_list, &line->task, line->tty,
  33. line->driver->read_irq);
  34. }
  35. /* Returns the free space inside the ring buffer of this line.
  36. *
  37. * Should be called while holding line->lock (this does not modify datas).
  38. */
  39. static int write_room(struct line *line)
  40. {
  41. int n;
  42. if (line->buffer == NULL)
  43. return LINE_BUFSIZE - 1;
  44. /* This is for the case where the buffer is wrapped! */
  45. n = line->head - line->tail;
  46. if (n <= 0)
  47. n = LINE_BUFSIZE + n; /* The other case */
  48. return n - 1;
  49. }
  50. int line_write_room(struct tty_struct *tty)
  51. {
  52. struct line *line = tty->driver_data;
  53. unsigned long flags;
  54. int room;
  55. if (tty->stopped)
  56. return 0;
  57. spin_lock_irqsave(&line->lock, flags);
  58. room = write_room(line);
  59. spin_unlock_irqrestore(&line->lock, flags);
  60. /*XXX: Warning to remove */
  61. if (0 == room)
  62. printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
  63. __FUNCTION__,tty->name);
  64. return room;
  65. }
  66. int line_chars_in_buffer(struct tty_struct *tty)
  67. {
  68. struct line *line = tty->driver_data;
  69. unsigned long flags;
  70. int ret;
  71. spin_lock_irqsave(&line->lock, flags);
  72. /*write_room subtracts 1 for the needed NULL, so we readd it.*/
  73. ret = LINE_BUFSIZE - (write_room(line) + 1);
  74. spin_unlock_irqrestore(&line->lock, flags);
  75. return ret;
  76. }
  77. /*
  78. * This copies the content of buf into the circular buffer associated with
  79. * this line.
  80. * The return value is the number of characters actually copied, i.e. the ones
  81. * for which there was space: this function is not supposed to ever flush out
  82. * the circular buffer.
  83. *
  84. * Must be called while holding line->lock!
  85. */
  86. static int buffer_data(struct line *line, const char *buf, int len)
  87. {
  88. int end, room;
  89. if(line->buffer == NULL){
  90. line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
  91. if (line->buffer == NULL) {
  92. printk("buffer_data - atomic allocation failed\n");
  93. return(0);
  94. }
  95. line->head = line->buffer;
  96. line->tail = line->buffer;
  97. }
  98. room = write_room(line);
  99. len = (len > room) ? room : len;
  100. end = line->buffer + LINE_BUFSIZE - line->tail;
  101. if (len < end){
  102. memcpy(line->tail, buf, len);
  103. line->tail += len;
  104. }
  105. else {
  106. /* The circular buffer is wrapping */
  107. memcpy(line->tail, buf, end);
  108. buf += end;
  109. memcpy(line->buffer, buf, len - end);
  110. line->tail = line->buffer + len - end;
  111. }
  112. return len;
  113. }
  114. /*
  115. * Flushes the ring buffer to the output channels. That is, write_chan is
  116. * called, passing it line->head as buffer, and an appropriate count.
  117. *
  118. * On exit, returns 1 when the buffer is empty,
  119. * 0 when the buffer is not empty on exit,
  120. * and -errno when an error occurred.
  121. *
  122. * Must be called while holding line->lock!*/
  123. static int flush_buffer(struct line *line)
  124. {
  125. int n, count;
  126. if ((line->buffer == NULL) || (line->head == line->tail))
  127. return 1;
  128. if (line->tail < line->head) {
  129. /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
  130. count = line->buffer + LINE_BUFSIZE - line->head;
  131. n = write_chan(&line->chan_list, line->head, count,
  132. line->driver->write_irq);
  133. if (n < 0)
  134. return n;
  135. if (n == count) {
  136. /* We have flushed from ->head to buffer end, now we
  137. * must flush only from the beginning to ->tail.*/
  138. line->head = line->buffer;
  139. } else {
  140. line->head += n;
  141. return 0;
  142. }
  143. }
  144. count = line->tail - line->head;
  145. n = write_chan(&line->chan_list, line->head, count,
  146. line->driver->write_irq);
  147. if(n < 0)
  148. return n;
  149. line->head += n;
  150. return line->head == line->tail;
  151. }
  152. void line_flush_buffer(struct tty_struct *tty)
  153. {
  154. struct line *line = tty->driver_data;
  155. unsigned long flags;
  156. int err;
  157. /*XXX: copied from line_write, verify if it is correct!*/
  158. if(tty->stopped)
  159. return;
  160. spin_lock_irqsave(&line->lock, flags);
  161. err = flush_buffer(line);
  162. /*if (err == 1)
  163. err = 0;*/
  164. spin_unlock_irqrestore(&line->lock, flags);
  165. //return err;
  166. }
  167. /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
  168. * and ->write. Hope it's not that bad.*/
  169. void line_flush_chars(struct tty_struct *tty)
  170. {
  171. line_flush_buffer(tty);
  172. }
  173. void line_put_char(struct tty_struct *tty, unsigned char ch)
  174. {
  175. line_write(tty, &ch, sizeof(ch));
  176. }
  177. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  178. {
  179. struct line *line = tty->driver_data;
  180. unsigned long flags;
  181. int n, err, ret = 0;
  182. if(tty->stopped)
  183. return 0;
  184. spin_lock_irqsave(&line->lock, flags);
  185. if (line->head != line->tail) {
  186. ret = buffer_data(line, buf, len);
  187. err = flush_buffer(line);
  188. if (err <= 0 && (err != -EAGAIN || !ret))
  189. ret = err;
  190. } else {
  191. n = write_chan(&line->chan_list, buf, len,
  192. line->driver->write_irq);
  193. if (n < 0) {
  194. ret = n;
  195. goto out_up;
  196. }
  197. len -= n;
  198. ret += n;
  199. if (len > 0)
  200. ret += buffer_data(line, buf + n, len);
  201. }
  202. out_up:
  203. spin_unlock_irqrestore(&line->lock, flags);
  204. return ret;
  205. }
  206. void line_set_termios(struct tty_struct *tty, struct ktermios * old)
  207. {
  208. /* nothing */
  209. }
  210. static const struct {
  211. int cmd;
  212. char *level;
  213. char *name;
  214. } tty_ioctls[] = {
  215. /* don't print these, they flood the log ... */
  216. { TCGETS, NULL, "TCGETS" },
  217. { TCSETS, NULL, "TCSETS" },
  218. { TCSETSW, NULL, "TCSETSW" },
  219. { TCFLSH, NULL, "TCFLSH" },
  220. { TCSBRK, NULL, "TCSBRK" },
  221. /* general tty stuff */
  222. { TCSETSF, KERN_DEBUG, "TCSETSF" },
  223. { TCGETA, KERN_DEBUG, "TCGETA" },
  224. { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
  225. { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
  226. { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
  227. /* linux-specific ones */
  228. { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
  229. { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
  230. { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
  231. { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
  232. };
  233. int line_ioctl(struct tty_struct *tty, struct file * file,
  234. unsigned int cmd, unsigned long arg)
  235. {
  236. int ret;
  237. int i;
  238. ret = 0;
  239. switch(cmd) {
  240. #ifdef TIOCGETP
  241. case TIOCGETP:
  242. case TIOCSETP:
  243. case TIOCSETN:
  244. #endif
  245. #ifdef TIOCGETC
  246. case TIOCGETC:
  247. case TIOCSETC:
  248. #endif
  249. #ifdef TIOCGLTC
  250. case TIOCGLTC:
  251. case TIOCSLTC:
  252. #endif
  253. case TCGETS:
  254. case TCSETSF:
  255. case TCSETSW:
  256. case TCSETS:
  257. case TCGETA:
  258. case TCSETAF:
  259. case TCSETAW:
  260. case TCSETA:
  261. case TCXONC:
  262. case TCFLSH:
  263. case TIOCOUTQ:
  264. case TIOCINQ:
  265. case TIOCGLCKTRMIOS:
  266. case TIOCSLCKTRMIOS:
  267. case TIOCPKT:
  268. case TIOCGSOFTCAR:
  269. case TIOCSSOFTCAR:
  270. return -ENOIOCTLCMD;
  271. #if 0
  272. case TCwhatever:
  273. /* do something */
  274. break;
  275. #endif
  276. default:
  277. for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
  278. if (cmd == tty_ioctls[i].cmd)
  279. break;
  280. if (i < ARRAY_SIZE(tty_ioctls)) {
  281. if (NULL != tty_ioctls[i].level)
  282. printk("%s%s: %s: ioctl %s called\n",
  283. tty_ioctls[i].level, __FUNCTION__,
  284. tty->name, tty_ioctls[i].name);
  285. } else {
  286. printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
  287. __FUNCTION__, tty->name, cmd);
  288. }
  289. ret = -ENOIOCTLCMD;
  290. break;
  291. }
  292. return ret;
  293. }
  294. void line_throttle(struct tty_struct *tty)
  295. {
  296. struct line *line = tty->driver_data;
  297. deactivate_chan(&line->chan_list, line->driver->read_irq);
  298. line->throttled = 1;
  299. }
  300. void line_unthrottle(struct tty_struct *tty)
  301. {
  302. struct line *line = tty->driver_data;
  303. line->throttled = 0;
  304. chan_interrupt(&line->chan_list, &line->task, tty,
  305. line->driver->read_irq);
  306. /* Maybe there is enough stuff pending that calling the interrupt
  307. * throttles us again. In this case, line->throttled will be 1
  308. * again and we shouldn't turn the interrupt back on.
  309. */
  310. if(!line->throttled)
  311. reactivate_chan(&line->chan_list, line->driver->read_irq);
  312. }
  313. static irqreturn_t line_write_interrupt(int irq, void *data)
  314. {
  315. struct chan *chan = data;
  316. struct line *line = chan->line;
  317. struct tty_struct *tty = line->tty;
  318. int err;
  319. /* Interrupts are disabled here because we registered the interrupt with
  320. * IRQF_DISABLED (see line_setup_irq).*/
  321. spin_lock(&line->lock);
  322. err = flush_buffer(line);
  323. if (err == 0) {
  324. return IRQ_NONE;
  325. } else if(err < 0) {
  326. line->head = line->buffer;
  327. line->tail = line->buffer;
  328. }
  329. spin_unlock(&line->lock);
  330. if(tty == NULL)
  331. return IRQ_NONE;
  332. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
  333. (tty->ldisc.write_wakeup != NULL))
  334. (tty->ldisc.write_wakeup)(tty);
  335. /* BLOCKING mode
  336. * In blocking mode, everything sleeps on tty->write_wait.
  337. * Sleeping in the console driver would break non-blocking
  338. * writes.
  339. */
  340. if (waitqueue_active(&tty->write_wait))
  341. wake_up_interruptible(&tty->write_wait);
  342. return IRQ_HANDLED;
  343. }
  344. int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
  345. {
  346. const struct line_driver *driver = line->driver;
  347. int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
  348. if (input)
  349. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  350. line_interrupt, flags,
  351. driver->read_irq_name, data);
  352. if (err)
  353. return err;
  354. if (output)
  355. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  356. line_write_interrupt, flags,
  357. driver->write_irq_name, data);
  358. line->have_irq = 1;
  359. return err;
  360. }
  361. /* Normally, a driver like this can rely mostly on the tty layer
  362. * locking, particularly when it comes to the driver structure.
  363. * However, in this case, mconsole requests can come in "from the
  364. * side", and race with opens and closes.
  365. *
  366. * mconsole config requests will want to be sure the device isn't in
  367. * use, and get_config, open, and close will want a stable
  368. * configuration. The checking and modification of the configuration
  369. * is done under a spinlock. Checking whether the device is in use is
  370. * line->tty->count > 1, also under the spinlock.
  371. *
  372. * tty->count serves to decide whether the device should be enabled or
  373. * disabled on the host. If it's equal to 1, then we are doing the
  374. * first open or last close. Otherwise, open and close just return.
  375. */
  376. int line_open(struct line *lines, struct tty_struct *tty)
  377. {
  378. struct line *line = &lines[tty->index];
  379. int err = -ENODEV;
  380. spin_lock(&line->count_lock);
  381. if(!line->valid)
  382. goto out_unlock;
  383. err = 0;
  384. if(tty->count > 1)
  385. goto out_unlock;
  386. spin_unlock(&line->count_lock);
  387. tty->driver_data = line;
  388. line->tty = tty;
  389. enable_chan(line);
  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. };
  629. static irqreturn_t winch_interrupt(int irq, void *data)
  630. {
  631. struct winch *winch = data;
  632. struct tty_struct *tty;
  633. struct line *line;
  634. int err;
  635. char c;
  636. if(winch->fd != -1){
  637. err = generic_read(winch->fd, &c, NULL);
  638. if(err < 0){
  639. if(err != -EAGAIN){
  640. printk("winch_interrupt : read failed, "
  641. "errno = %d\n", -err);
  642. printk("fd %d is losing SIGWINCH support\n",
  643. winch->tty_fd);
  644. return IRQ_HANDLED;
  645. }
  646. goto out;
  647. }
  648. }
  649. tty = winch->tty;
  650. if (tty != NULL) {
  651. line = tty->driver_data;
  652. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  653. &tty->winsize.ws_col);
  654. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  655. }
  656. out:
  657. if(winch->fd != -1)
  658. reactivate_fd(winch->fd, WINCH_IRQ);
  659. return IRQ_HANDLED;
  660. }
  661. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
  662. {
  663. struct winch *winch;
  664. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  665. if (winch == NULL) {
  666. printk("register_winch_irq - kmalloc failed\n");
  667. return;
  668. }
  669. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  670. .fd = fd,
  671. .tty_fd = tty_fd,
  672. .pid = pid,
  673. .tty = tty });
  674. spin_lock(&winch_handler_lock);
  675. list_add(&winch->list, &winch_handlers);
  676. spin_unlock(&winch_handler_lock);
  677. if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  678. IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  679. "winch", winch) < 0)
  680. printk("register_winch_irq - failed to register IRQ\n");
  681. }
  682. static void free_winch(struct winch *winch)
  683. {
  684. list_del(&winch->list);
  685. if(winch->pid != -1)
  686. os_kill_process(winch->pid, 1);
  687. if(winch->fd != -1)
  688. os_close_file(winch->fd);
  689. free_irq(WINCH_IRQ, winch);
  690. kfree(winch);
  691. }
  692. static void unregister_winch(struct tty_struct *tty)
  693. {
  694. struct list_head *ele;
  695. struct winch *winch;
  696. spin_lock(&winch_handler_lock);
  697. list_for_each(ele, &winch_handlers){
  698. winch = list_entry(ele, struct winch, list);
  699. if(winch->tty == tty){
  700. free_winch(winch);
  701. break;
  702. }
  703. }
  704. spin_unlock(&winch_handler_lock);
  705. }
  706. static void winch_cleanup(void)
  707. {
  708. struct list_head *ele, *next;
  709. struct winch *winch;
  710. spin_lock(&winch_handler_lock);
  711. list_for_each_safe(ele, next, &winch_handlers){
  712. winch = list_entry(ele, struct winch, list);
  713. free_winch(winch);
  714. }
  715. spin_unlock(&winch_handler_lock);
  716. }
  717. __uml_exitcall(winch_cleanup);
  718. char *add_xterm_umid(char *base)
  719. {
  720. char *umid, *title;
  721. int len;
  722. umid = get_umid();
  723. if(*umid == '\0')
  724. return base;
  725. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  726. title = kmalloc(len, GFP_KERNEL);
  727. if(title == NULL){
  728. printk("Failed to allocate buffer for xterm title\n");
  729. return base;
  730. }
  731. snprintf(title, len, "%s (%s)", base, umid);
  732. return title;
  733. }