line.c 19 KB

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