line.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/kernel.h"
  6. #include "linux/sched.h"
  7. #include "linux/slab.h"
  8. #include "linux/list.h"
  9. #include "linux/kd.h"
  10. #include "linux/interrupt.h"
  11. #include "asm/uaccess.h"
  12. #include "chan_kern.h"
  13. #include "irq_user.h"
  14. #include "line.h"
  15. #include "kern.h"
  16. #include "kern_util.h"
  17. #include "os.h"
  18. #include "irq_kern.h"
  19. #define LINE_BUFSIZE 4096
  20. static irqreturn_t line_interrupt(int irq, void *data)
  21. {
  22. struct chan *chan = data;
  23. struct line *line = chan->line;
  24. struct tty_struct *tty = line->tty;
  25. if (line)
  26. chan_interrupt(&line->chan_list, &line->task, tty, irq);
  27. return IRQ_HANDLED;
  28. }
  29. static void line_timer_cb(struct work_struct *work)
  30. {
  31. struct line *line = container_of(work, struct line, task.work);
  32. if(!line->throttled)
  33. chan_interrupt(&line->chan_list, &line->task, line->tty,
  34. line->driver->read_irq);
  35. }
  36. /* Returns the free space inside the ring buffer of this line.
  37. *
  38. * Should be called while holding line->lock (this does not modify datas).
  39. */
  40. static int write_room(struct line *line)
  41. {
  42. int n;
  43. if (line->buffer == NULL)
  44. return LINE_BUFSIZE - 1;
  45. /* This is for the case where the buffer is wrapped! */
  46. n = line->head - line->tail;
  47. if (n <= 0)
  48. n = LINE_BUFSIZE + n; /* The other case */
  49. return n - 1;
  50. }
  51. int line_write_room(struct tty_struct *tty)
  52. {
  53. struct line *line = tty->driver_data;
  54. unsigned long flags;
  55. int room;
  56. if (tty->stopped)
  57. return 0;
  58. spin_lock_irqsave(&line->lock, flags);
  59. room = write_room(line);
  60. spin_unlock_irqrestore(&line->lock, flags);
  61. /*XXX: Warning to remove */
  62. if (0 == room)
  63. printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
  64. __FUNCTION__,tty->name);
  65. return room;
  66. }
  67. int line_chars_in_buffer(struct tty_struct *tty)
  68. {
  69. struct line *line = tty->driver_data;
  70. unsigned long flags;
  71. int ret;
  72. spin_lock_irqsave(&line->lock, flags);
  73. /*write_room subtracts 1 for the needed NULL, so we readd it.*/
  74. ret = LINE_BUFSIZE - (write_room(line) + 1);
  75. spin_unlock_irqrestore(&line->lock, flags);
  76. return ret;
  77. }
  78. /*
  79. * This copies the content of buf into the circular buffer associated with
  80. * this line.
  81. * The return value is the number of characters actually copied, i.e. the ones
  82. * for which there was space: this function is not supposed to ever flush out
  83. * the circular buffer.
  84. *
  85. * Must be called while holding line->lock!
  86. */
  87. static int buffer_data(struct line *line, const char *buf, int len)
  88. {
  89. int end, room;
  90. if(line->buffer == NULL){
  91. line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
  92. if (line->buffer == NULL) {
  93. printk("buffer_data - atomic allocation failed\n");
  94. return(0);
  95. }
  96. line->head = line->buffer;
  97. line->tail = line->buffer;
  98. }
  99. room = write_room(line);
  100. len = (len > room) ? room : len;
  101. end = line->buffer + LINE_BUFSIZE - line->tail;
  102. if (len < end){
  103. memcpy(line->tail, buf, len);
  104. line->tail += len;
  105. }
  106. else {
  107. /* The circular buffer is wrapping */
  108. memcpy(line->tail, buf, end);
  109. buf += end;
  110. memcpy(line->buffer, buf, len - end);
  111. line->tail = line->buffer + len - end;
  112. }
  113. return len;
  114. }
  115. /*
  116. * Flushes the ring buffer to the output channels. That is, write_chan is
  117. * called, passing it line->head as buffer, and an appropriate count.
  118. *
  119. * On exit, returns 1 when the buffer is empty,
  120. * 0 when the buffer is not empty on exit,
  121. * and -errno when an error occurred.
  122. *
  123. * Must be called while holding line->lock!*/
  124. static int flush_buffer(struct line *line)
  125. {
  126. int n, count;
  127. if ((line->buffer == NULL) || (line->head == line->tail))
  128. return 1;
  129. if (line->tail < line->head) {
  130. /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
  131. count = line->buffer + LINE_BUFSIZE - line->head;
  132. n = write_chan(&line->chan_list, line->head, count,
  133. line->driver->write_irq);
  134. if (n < 0)
  135. return n;
  136. if (n == count) {
  137. /* We have flushed from ->head to buffer end, now we
  138. * must flush only from the beginning to ->tail.*/
  139. line->head = line->buffer;
  140. } else {
  141. line->head += n;
  142. return 0;
  143. }
  144. }
  145. count = line->tail - line->head;
  146. n = write_chan(&line->chan_list, line->head, count,
  147. line->driver->write_irq);
  148. if(n < 0)
  149. return n;
  150. line->head += n;
  151. return line->head == line->tail;
  152. }
  153. void line_flush_buffer(struct tty_struct *tty)
  154. {
  155. struct line *line = tty->driver_data;
  156. unsigned long flags;
  157. int err;
  158. /*XXX: copied from line_write, verify if it is correct!*/
  159. if(tty->stopped)
  160. return;
  161. spin_lock_irqsave(&line->lock, flags);
  162. err = flush_buffer(line);
  163. /*if (err == 1)
  164. err = 0;*/
  165. spin_unlock_irqrestore(&line->lock, flags);
  166. //return err;
  167. }
  168. /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
  169. * and ->write. Hope it's not that bad.*/
  170. void line_flush_chars(struct tty_struct *tty)
  171. {
  172. line_flush_buffer(tty);
  173. }
  174. void line_put_char(struct tty_struct *tty, unsigned char ch)
  175. {
  176. line_write(tty, &ch, sizeof(ch));
  177. }
  178. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  179. {
  180. struct line *line = tty->driver_data;
  181. unsigned long flags;
  182. int n, 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 disabled here because we registered the interrupt with
  321. * IRQF_DISABLED (see line_setup_irq).*/
  322. spin_lock(&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(&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. err = enable_chan(line);
  391. if (err)
  392. return err;
  393. INIT_DELAYED_WORK(&line->task, line_timer_cb);
  394. if(!line->sigio){
  395. chan_enable_winch(&line->chan_list, tty);
  396. line->sigio = 1;
  397. }
  398. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  399. &tty->winsize.ws_col);
  400. return err;
  401. out_unlock:
  402. spin_unlock(&line->count_lock);
  403. return err;
  404. }
  405. static void unregister_winch(struct tty_struct *tty);
  406. void line_close(struct tty_struct *tty, struct file * filp)
  407. {
  408. struct line *line = tty->driver_data;
  409. /* If line_open fails (and tty->driver_data is never set),
  410. * tty_open will call line_close. So just return in this case.
  411. */
  412. if(line == NULL)
  413. return;
  414. /* We ignore the error anyway! */
  415. flush_buffer(line);
  416. spin_lock(&line->count_lock);
  417. if(!line->valid)
  418. goto out_unlock;
  419. if(tty->count > 1)
  420. goto out_unlock;
  421. spin_unlock(&line->count_lock);
  422. line->tty = NULL;
  423. tty->driver_data = NULL;
  424. if(line->sigio){
  425. unregister_winch(tty);
  426. line->sigio = 0;
  427. }
  428. return;
  429. out_unlock:
  430. spin_unlock(&line->count_lock);
  431. }
  432. void close_lines(struct line *lines, int nlines)
  433. {
  434. int i;
  435. for(i = 0; i < nlines; i++)
  436. close_chan(&lines[i].chan_list, 0);
  437. }
  438. static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
  439. char **error_out)
  440. {
  441. struct line *line = &lines[n];
  442. int err = -EINVAL;
  443. spin_lock(&line->count_lock);
  444. if(line->tty != NULL){
  445. *error_out = "Device is already open";
  446. goto out;
  447. }
  448. if (line->init_pri <= init_prio){
  449. line->init_pri = init_prio;
  450. if (!strcmp(init, "none"))
  451. line->valid = 0;
  452. else {
  453. line->init_str = init;
  454. line->valid = 1;
  455. }
  456. }
  457. err = 0;
  458. out:
  459. spin_unlock(&line->count_lock);
  460. return err;
  461. }
  462. /* Common setup code for both startup command line and mconsole initialization.
  463. * @lines contains the array (of size @num) to modify;
  464. * @init is the setup string;
  465. * @error_out is an error string in the case of failure;
  466. */
  467. int line_setup(struct line *lines, unsigned int num, char *init,
  468. char **error_out)
  469. {
  470. int i, n, err;
  471. char *end;
  472. if(*init == '=') {
  473. /* We said con=/ssl= instead of con#=, so we are configuring all
  474. * consoles at once.*/
  475. n = -1;
  476. }
  477. else {
  478. n = simple_strtoul(init, &end, 0);
  479. if(*end != '='){
  480. *error_out = "Couldn't parse device number";
  481. return -EINVAL;
  482. }
  483. init = end;
  484. }
  485. init++;
  486. if (n >= (signed int) num) {
  487. *error_out = "Device number out of range";
  488. return -EINVAL;
  489. }
  490. else if (n >= 0){
  491. err = setup_one_line(lines, n, init, INIT_ONE, error_out);
  492. if(err)
  493. return err;
  494. }
  495. else {
  496. for(i = 0; i < num; i++){
  497. err = setup_one_line(lines, i, init, INIT_ALL,
  498. error_out);
  499. if(err)
  500. return err;
  501. }
  502. }
  503. return n == -1 ? num : n;
  504. }
  505. int line_config(struct line *lines, unsigned int num, char *str,
  506. const struct chan_opts *opts, char **error_out)
  507. {
  508. struct line *line;
  509. char *new;
  510. int n;
  511. if(*str == '='){
  512. *error_out = "Can't configure all devices from mconsole";
  513. return -EINVAL;
  514. }
  515. new = kstrdup(str, GFP_KERNEL);
  516. if(new == NULL){
  517. *error_out = "Failed to allocate memory";
  518. return -ENOMEM;
  519. }
  520. n = line_setup(lines, num, new, error_out);
  521. if(n < 0)
  522. return n;
  523. line = &lines[n];
  524. return parse_chan_pair(line->init_str, line, n, opts, error_out);
  525. }
  526. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  527. int size, char **error_out)
  528. {
  529. struct line *line;
  530. char *end;
  531. int dev, n = 0;
  532. dev = simple_strtoul(name, &end, 0);
  533. if((*end != '\0') || (end == name)){
  534. *error_out = "line_get_config failed to parse device number";
  535. return 0;
  536. }
  537. if((dev < 0) || (dev >= num)){
  538. *error_out = "device number out of range";
  539. return 0;
  540. }
  541. line = &lines[dev];
  542. spin_lock(&line->count_lock);
  543. if(!line->valid)
  544. CONFIG_CHUNK(str, size, n, "none", 1);
  545. else if(line->tty == NULL)
  546. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  547. else n = chan_config_string(&line->chan_list, str, size, error_out);
  548. spin_unlock(&line->count_lock);
  549. return n;
  550. }
  551. int line_id(char **str, int *start_out, int *end_out)
  552. {
  553. char *end;
  554. int n;
  555. n = simple_strtoul(*str, &end, 0);
  556. if((*end != '\0') || (end == *str))
  557. return -1;
  558. *str = end;
  559. *start_out = n;
  560. *end_out = n;
  561. return n;
  562. }
  563. int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
  564. {
  565. int err;
  566. char config[sizeof("conxxxx=none\0")];
  567. sprintf(config, "%d=none", n);
  568. err = line_setup(lines, num, config, error_out);
  569. if(err >= 0)
  570. err = 0;
  571. return err;
  572. }
  573. struct tty_driver *register_lines(struct line_driver *line_driver,
  574. const struct tty_operations *ops,
  575. struct line *lines, int nlines)
  576. {
  577. int i;
  578. struct tty_driver *driver = alloc_tty_driver(nlines);
  579. if (!driver)
  580. return NULL;
  581. driver->driver_name = line_driver->name;
  582. driver->name = line_driver->device_name;
  583. driver->major = line_driver->major;
  584. driver->minor_start = line_driver->minor_start;
  585. driver->type = line_driver->type;
  586. driver->subtype = line_driver->subtype;
  587. driver->flags = TTY_DRIVER_REAL_RAW;
  588. driver->init_termios = tty_std_termios;
  589. tty_set_operations(driver, ops);
  590. if (tty_register_driver(driver)) {
  591. printk("%s: can't register %s driver\n",
  592. __FUNCTION__,line_driver->name);
  593. put_tty_driver(driver);
  594. return NULL;
  595. }
  596. for(i = 0; i < nlines; i++){
  597. if(!lines[i].valid)
  598. tty_unregister_device(driver, i);
  599. }
  600. mconsole_register_dev(&line_driver->mc);
  601. return driver;
  602. }
  603. static DEFINE_SPINLOCK(winch_handler_lock);
  604. static LIST_HEAD(winch_handlers);
  605. void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
  606. {
  607. struct line *line;
  608. char *error;
  609. int i;
  610. for(i = 0; i < nlines; i++){
  611. line = &lines[i];
  612. INIT_LIST_HEAD(&line->chan_list);
  613. if(line->init_str == NULL)
  614. continue;
  615. line->init_str = kstrdup(line->init_str, GFP_KERNEL);
  616. if(line->init_str == NULL)
  617. printk("lines_init - kstrdup returned NULL\n");
  618. if(parse_chan_pair(line->init_str, line, i, opts, &error)){
  619. printk("parse_chan_pair failed for device %d : %s\n",
  620. i, error);
  621. line->valid = 0;
  622. }
  623. }
  624. }
  625. struct winch {
  626. struct list_head list;
  627. int fd;
  628. int tty_fd;
  629. int pid;
  630. struct tty_struct *tty;
  631. unsigned long stack;
  632. };
  633. static void free_winch(struct winch *winch, int free_irq_ok)
  634. {
  635. list_del(&winch->list);
  636. if (winch->pid != -1)
  637. os_kill_process(winch->pid, 1);
  638. if (winch->fd != -1)
  639. os_close_file(winch->fd);
  640. if (winch->stack != 0)
  641. free_stack(winch->stack, 0);
  642. if (free_irq_ok)
  643. free_irq(WINCH_IRQ, winch);
  644. kfree(winch);
  645. }
  646. static irqreturn_t winch_interrupt(int irq, void *data)
  647. {
  648. struct winch *winch = data;
  649. struct tty_struct *tty;
  650. struct line *line;
  651. int err;
  652. char c;
  653. if(winch->fd != -1){
  654. err = generic_read(winch->fd, &c, NULL);
  655. if(err < 0){
  656. if(err != -EAGAIN){
  657. printk("winch_interrupt : read failed, "
  658. "errno = %d\n", -err);
  659. printk("fd %d is losing SIGWINCH support\n",
  660. winch->tty_fd);
  661. free_winch(winch, 0);
  662. return IRQ_HANDLED;
  663. }
  664. goto out;
  665. }
  666. }
  667. tty = winch->tty;
  668. if (tty != NULL) {
  669. line = tty->driver_data;
  670. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  671. &tty->winsize.ws_col);
  672. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  673. }
  674. out:
  675. if(winch->fd != -1)
  676. reactivate_fd(winch->fd, WINCH_IRQ);
  677. return IRQ_HANDLED;
  678. }
  679. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
  680. unsigned long stack)
  681. {
  682. struct winch *winch;
  683. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  684. if (winch == NULL) {
  685. printk("register_winch_irq - kmalloc failed\n");
  686. goto cleanup;
  687. }
  688. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  689. .fd = fd,
  690. .tty_fd = tty_fd,
  691. .pid = pid,
  692. .tty = tty,
  693. .stack = stack });
  694. if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  695. IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  696. "winch", winch) < 0) {
  697. printk("register_winch_irq - failed to register IRQ\n");
  698. goto out_free;
  699. }
  700. spin_lock(&winch_handler_lock);
  701. list_add(&winch->list, &winch_handlers);
  702. spin_unlock(&winch_handler_lock);
  703. return;
  704. out_free:
  705. kfree(winch);
  706. cleanup:
  707. os_kill_process(pid, 1);
  708. os_close_file(fd);
  709. if (stack != 0)
  710. free_stack(stack, 0);
  711. }
  712. static void unregister_winch(struct tty_struct *tty)
  713. {
  714. struct list_head *ele;
  715. struct winch *winch;
  716. spin_lock(&winch_handler_lock);
  717. list_for_each(ele, &winch_handlers){
  718. winch = list_entry(ele, struct winch, list);
  719. if(winch->tty == tty){
  720. free_winch(winch, 1);
  721. break;
  722. }
  723. }
  724. spin_unlock(&winch_handler_lock);
  725. }
  726. static void winch_cleanup(void)
  727. {
  728. struct list_head *ele, *next;
  729. struct winch *winch;
  730. spin_lock(&winch_handler_lock);
  731. list_for_each_safe(ele, next, &winch_handlers){
  732. winch = list_entry(ele, struct winch, list);
  733. free_winch(winch, 1);
  734. }
  735. spin_unlock(&winch_handler_lock);
  736. }
  737. __uml_exitcall(winch_cleanup);
  738. char *add_xterm_umid(char *base)
  739. {
  740. char *umid, *title;
  741. int len;
  742. umid = get_umid();
  743. if(*umid == '\0')
  744. return base;
  745. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  746. title = kmalloc(len, GFP_KERNEL);
  747. if(title == NULL){
  748. printk("Failed to allocate buffer for xterm title\n");
  749. return base;
  750. }
  751. snprintf(title, len, "%s (%s)", base, umid);
  752. return title;
  753. }