line.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/sched.h"
  6. #include "linux/slab.h"
  7. #include "linux/list.h"
  8. #include "linux/kd.h"
  9. #include "linux/interrupt.h"
  10. #include "linux/devfs_fs_kernel.h"
  11. #include "asm/uaccess.h"
  12. #include "chan_kern.h"
  13. #include "irq_user.h"
  14. #include "line.h"
  15. #include "kern.h"
  16. #include "user_util.h"
  17. #include "kern_util.h"
  18. #include "os.h"
  19. #include "irq_kern.h"
  20. #define LINE_BUFSIZE 4096
  21. static irqreturn_t line_interrupt(int irq, void *data, struct pt_regs *unused)
  22. {
  23. struct tty_struct *tty = data;
  24. struct line *line = tty->driver_data;
  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 tty_struct *tty = arg;
  32. struct line *line = tty->driver_data;
  33. line_interrupt(line->driver->read_irq, arg, NULL);
  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. } else {
  105. /* The circular buffer is wrapping */
  106. memcpy(line->tail, buf, end);
  107. buf += end;
  108. memcpy(line->buffer, buf, len - end);
  109. line->tail = line->buffer + len - end;
  110. }
  111. return len;
  112. }
  113. /*
  114. * Flushes the ring buffer to the output channels. That is, write_chan is
  115. * called, passing it line->head as buffer, and an appropriate count.
  116. *
  117. * On exit, returns 1 when the buffer is empty,
  118. * 0 when the buffer is not empty on exit,
  119. * and -errno when an error occurred.
  120. *
  121. * Must be called while holding line->lock!*/
  122. static int flush_buffer(struct line *line)
  123. {
  124. int n, count;
  125. if ((line->buffer == NULL) || (line->head == line->tail))
  126. return 1;
  127. if (line->tail < line->head) {
  128. /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
  129. count = line->buffer + LINE_BUFSIZE - line->head;
  130. n = write_chan(&line->chan_list, line->head, count,
  131. line->driver->write_irq);
  132. if (n < 0)
  133. return n;
  134. if (n == count) {
  135. /* We have flushed from ->head to buffer end, now we
  136. * must flush only from the beginning to ->tail.*/
  137. line->head = line->buffer;
  138. } else {
  139. line->head += n;
  140. return 0;
  141. }
  142. }
  143. count = line->tail - line->head;
  144. n = write_chan(&line->chan_list, line->head, count,
  145. line->driver->write_irq);
  146. if(n < 0)
  147. return n;
  148. line->head += n;
  149. return line->head == line->tail;
  150. }
  151. void line_flush_buffer(struct tty_struct *tty)
  152. {
  153. struct line *line = tty->driver_data;
  154. unsigned long flags;
  155. int err;
  156. /*XXX: copied from line_write, verify if it is correct!*/
  157. if(tty->stopped)
  158. return;
  159. //return 0;
  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 termios * old)
  207. {
  208. /* nothing */
  209. }
  210. static 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. static irqreturn_t line_write_interrupt(int irq, void *data,
  295. struct pt_regs *unused)
  296. {
  297. struct tty_struct *tty = data;
  298. struct line *line = tty->driver_data;
  299. int err;
  300. /* Interrupts are enabled here because we registered the interrupt with
  301. * SA_INTERRUPT (see line_setup_irq).*/
  302. spin_lock_irq(&line->lock);
  303. err = flush_buffer(line);
  304. if (err == 0) {
  305. return IRQ_NONE;
  306. } else if(err < 0) {
  307. line->head = line->buffer;
  308. line->tail = line->buffer;
  309. }
  310. spin_unlock_irq(&line->lock);
  311. if(tty == NULL)
  312. return IRQ_NONE;
  313. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
  314. (tty->ldisc.write_wakeup != NULL))
  315. (tty->ldisc.write_wakeup)(tty);
  316. /* BLOCKING mode
  317. * In blocking mode, everything sleeps on tty->write_wait.
  318. * Sleeping in the console driver would break non-blocking
  319. * writes.
  320. */
  321. if (waitqueue_active(&tty->write_wait))
  322. wake_up_interruptible(&tty->write_wait);
  323. return IRQ_HANDLED;
  324. }
  325. int line_setup_irq(int fd, int input, int output, struct tty_struct *tty)
  326. {
  327. struct line *line = tty->driver_data;
  328. struct line_driver *driver = line->driver;
  329. int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
  330. if (input)
  331. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  332. line_interrupt, flags,
  333. driver->read_irq_name, tty);
  334. if (err)
  335. return err;
  336. if (output)
  337. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  338. line_write_interrupt, flags,
  339. driver->write_irq_name, tty);
  340. line->have_irq = 1;
  341. return err;
  342. }
  343. void line_disable(struct tty_struct *tty, int current_irq)
  344. {
  345. struct line *line = tty->driver_data;
  346. if(!line->have_irq)
  347. return;
  348. if(line->driver->read_irq == current_irq)
  349. free_irq_later(line->driver->read_irq, tty);
  350. else {
  351. free_irq(line->driver->read_irq, tty);
  352. }
  353. if(line->driver->write_irq == current_irq)
  354. free_irq_later(line->driver->write_irq, tty);
  355. else {
  356. free_irq(line->driver->write_irq, tty);
  357. }
  358. line->have_irq = 0;
  359. }
  360. int line_open(struct line *lines, struct tty_struct *tty,
  361. struct chan_opts *opts)
  362. {
  363. struct line *line;
  364. int err = 0;
  365. line = &lines[tty->index];
  366. tty->driver_data = line;
  367. /* The IRQ which takes this lock is not yet enabled and won't be run
  368. * before the end, so we don't need to use spin_lock_irq.*/
  369. spin_lock(&line->lock);
  370. if (tty->count == 1) {
  371. if (!line->valid) {
  372. err = -ENODEV;
  373. goto out;
  374. }
  375. if (list_empty(&line->chan_list)) {
  376. err = parse_chan_pair(line->init_str, &line->chan_list,
  377. line->init_pri, tty->index, opts);
  378. if(err) goto out;
  379. err = open_chan(&line->chan_list);
  380. if(err) goto out;
  381. }
  382. /* Here the interrupt is registered.*/
  383. enable_chan(&line->chan_list, tty);
  384. INIT_WORK(&line->task, line_timer_cb, tty);
  385. }
  386. if(!line->sigio){
  387. chan_enable_winch(&line->chan_list, tty);
  388. line->sigio = 1;
  389. }
  390. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  391. &tty->winsize.ws_col);
  392. line->count++;
  393. out:
  394. spin_unlock(&line->lock);
  395. return err;
  396. }
  397. static void unregister_winch(struct tty_struct *tty);
  398. void line_close(struct tty_struct *tty, struct file * filp)
  399. {
  400. struct line *line = tty->driver_data;
  401. /* XXX: I assume this should be called in process context, not with
  402. * interrupts disabled!
  403. */
  404. spin_lock_irq(&line->lock);
  405. /* We ignore the error anyway! */
  406. flush_buffer(line);
  407. line->count--;
  408. if (tty->count == 1) {
  409. line_disable(tty, -1);
  410. tty->driver_data = NULL;
  411. }
  412. if((line->count == 0) && line->sigio){
  413. unregister_winch(tty);
  414. line->sigio = 0;
  415. }
  416. spin_unlock_irq(&line->lock);
  417. }
  418. void close_lines(struct line *lines, int nlines)
  419. {
  420. int i;
  421. for(i = 0; i < nlines; i++)
  422. close_chan(&lines[i].chan_list);
  423. }
  424. /* Common setup code for both startup command line and mconsole initialization.
  425. * @lines contains the the array (of size @num) to modify;
  426. * @init is the setup string;
  427. * @all_allowed is a boolean saying if we can setup the whole @lines
  428. * at once. For instance, it will be usually true for startup init. (where we
  429. * can use con=xterm) and false for mconsole.*/
  430. int line_setup(struct line *lines, unsigned int num, char *init, int all_allowed)
  431. {
  432. int i, n;
  433. char *end;
  434. if(*init == '=') {
  435. /* We said con=/ssl= instead of con#=, so we are configuring all
  436. * consoles at once.*/
  437. n = -1;
  438. } else {
  439. n = simple_strtoul(init, &end, 0);
  440. if(*end != '='){
  441. printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
  442. init);
  443. return 0;
  444. }
  445. init = end;
  446. }
  447. init++;
  448. if (n >= (signed int) num) {
  449. printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
  450. n, num - 1);
  451. return 0;
  452. } else if (n >= 0){
  453. if (lines[n].count > 0) {
  454. printk("line_setup - device %d is open\n", n);
  455. return 0;
  456. }
  457. if (lines[n].init_pri <= INIT_ONE){
  458. lines[n].init_pri = INIT_ONE;
  459. if (!strcmp(init, "none"))
  460. lines[n].valid = 0;
  461. else {
  462. lines[n].init_str = init;
  463. lines[n].valid = 1;
  464. }
  465. }
  466. } else if(!all_allowed){
  467. printk("line_setup - can't configure all devices from "
  468. "mconsole\n");
  469. return 0;
  470. } else {
  471. for(i = 0; i < num; i++){
  472. if(lines[i].init_pri <= INIT_ALL){
  473. lines[i].init_pri = INIT_ALL;
  474. if(!strcmp(init, "none")) lines[i].valid = 0;
  475. else {
  476. lines[i].init_str = init;
  477. lines[i].valid = 1;
  478. }
  479. }
  480. }
  481. }
  482. return 1;
  483. }
  484. int line_config(struct line *lines, unsigned int num, char *str)
  485. {
  486. char *new = uml_strdup(str);
  487. if(new == NULL){
  488. printk("line_config - uml_strdup failed\n");
  489. return -ENOMEM;
  490. }
  491. return !line_setup(lines, num, new, 0);
  492. }
  493. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  494. int size, char **error_out)
  495. {
  496. struct line *line;
  497. char *end;
  498. int dev, n = 0;
  499. dev = simple_strtoul(name, &end, 0);
  500. if((*end != '\0') || (end == name)){
  501. *error_out = "line_get_config failed to parse device number";
  502. return 0;
  503. }
  504. if((dev < 0) || (dev >= num)){
  505. *error_out = "device number out of range";
  506. return 0;
  507. }
  508. line = &lines[dev];
  509. spin_lock(&line->lock);
  510. if(!line->valid)
  511. CONFIG_CHUNK(str, size, n, "none", 1);
  512. else if(line->count == 0)
  513. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  514. else n = chan_config_string(&line->chan_list, str, size, error_out);
  515. spin_unlock(&line->lock);
  516. return n;
  517. }
  518. int line_id(char **str, int *start_out, int *end_out)
  519. {
  520. char *end;
  521. int n;
  522. n = simple_strtoul(*str, &end, 0);
  523. if((*end != '\0') || (end == *str))
  524. return -1;
  525. *str = end;
  526. *start_out = n;
  527. *end_out = n;
  528. return n;
  529. }
  530. int line_remove(struct line *lines, unsigned int num, int n)
  531. {
  532. char config[sizeof("conxxxx=none\0")];
  533. sprintf(config, "%d=none", n);
  534. return !line_setup(lines, num, config, 0);
  535. }
  536. struct tty_driver *line_register_devfs(struct lines *set,
  537. struct line_driver *line_driver,
  538. struct tty_operations *ops, struct line *lines,
  539. int nlines)
  540. {
  541. int i;
  542. struct tty_driver *driver = alloc_tty_driver(nlines);
  543. if (!driver)
  544. return NULL;
  545. driver->driver_name = line_driver->name;
  546. driver->name = line_driver->device_name;
  547. driver->devfs_name = line_driver->devfs_name;
  548. driver->major = line_driver->major;
  549. driver->minor_start = line_driver->minor_start;
  550. driver->type = line_driver->type;
  551. driver->subtype = line_driver->subtype;
  552. driver->flags = TTY_DRIVER_REAL_RAW;
  553. driver->init_termios = tty_std_termios;
  554. tty_set_operations(driver, ops);
  555. if (tty_register_driver(driver)) {
  556. printk("%s: can't register %s driver\n",
  557. __FUNCTION__,line_driver->name);
  558. put_tty_driver(driver);
  559. return NULL;
  560. }
  561. for(i = 0; i < nlines; i++){
  562. if(!lines[i].valid)
  563. tty_unregister_device(driver, i);
  564. }
  565. mconsole_register_dev(&line_driver->mc);
  566. return driver;
  567. }
  568. static spinlock_t winch_handler_lock;
  569. LIST_HEAD(winch_handlers);
  570. void lines_init(struct line *lines, int nlines)
  571. {
  572. struct line *line;
  573. int i;
  574. spin_lock_init(&winch_handler_lock);
  575. for(i = 0; i < nlines; i++){
  576. line = &lines[i];
  577. INIT_LIST_HEAD(&line->chan_list);
  578. spin_lock_init(&line->lock);
  579. if(line->init_str != NULL){
  580. line->init_str = uml_strdup(line->init_str);
  581. if(line->init_str == NULL)
  582. printk("lines_init - uml_strdup returned "
  583. "NULL\n");
  584. }
  585. }
  586. }
  587. struct winch {
  588. struct list_head list;
  589. int fd;
  590. int tty_fd;
  591. int pid;
  592. struct tty_struct *tty;
  593. };
  594. irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
  595. {
  596. struct winch *winch = data;
  597. struct tty_struct *tty;
  598. struct line *line;
  599. int err;
  600. char c;
  601. if(winch->fd != -1){
  602. err = generic_read(winch->fd, &c, NULL);
  603. if(err < 0){
  604. if(err != -EAGAIN){
  605. printk("winch_interrupt : read failed, "
  606. "errno = %d\n", -err);
  607. printk("fd %d is losing SIGWINCH support\n",
  608. winch->tty_fd);
  609. return IRQ_HANDLED;
  610. }
  611. goto out;
  612. }
  613. }
  614. tty = winch->tty;
  615. if (tty != NULL) {
  616. line = tty->driver_data;
  617. chan_window_size(&line->chan_list,
  618. &tty->winsize.ws_row,
  619. &tty->winsize.ws_col);
  620. kill_pg(tty->pgrp, SIGWINCH, 1);
  621. }
  622. out:
  623. if(winch->fd != -1)
  624. reactivate_fd(winch->fd, WINCH_IRQ);
  625. return IRQ_HANDLED;
  626. }
  627. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
  628. {
  629. struct winch *winch;
  630. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  631. if (winch == NULL) {
  632. printk("register_winch_irq - kmalloc failed\n");
  633. return;
  634. }
  635. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  636. .fd = fd,
  637. .tty_fd = tty_fd,
  638. .pid = pid,
  639. .tty = tty });
  640. spin_lock(&winch_handler_lock);
  641. list_add(&winch->list, &winch_handlers);
  642. spin_unlock(&winch_handler_lock);
  643. if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  644. SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
  645. "winch", winch) < 0)
  646. printk("register_winch_irq - failed to register IRQ\n");
  647. }
  648. static void unregister_winch(struct tty_struct *tty)
  649. {
  650. struct list_head *ele;
  651. struct winch *winch, *found = NULL;
  652. spin_lock(&winch_handler_lock);
  653. list_for_each(ele, &winch_handlers){
  654. winch = list_entry(ele, struct winch, list);
  655. if(winch->tty == tty){
  656. found = winch;
  657. break;
  658. }
  659. }
  660. if(found == NULL)
  661. goto err;
  662. list_del(&winch->list);
  663. spin_unlock(&winch_handler_lock);
  664. if(winch->pid != -1)
  665. os_kill_process(winch->pid, 1);
  666. free_irq(WINCH_IRQ, winch);
  667. kfree(winch);
  668. return;
  669. err:
  670. spin_unlock(&winch_handler_lock);
  671. }
  672. /* XXX: No lock as it's an exitcall... is this valid? Depending on cleanup
  673. * order... are we sure that nothing else is done on the list? */
  674. static void winch_cleanup(void)
  675. {
  676. struct list_head *ele;
  677. struct winch *winch;
  678. list_for_each(ele, &winch_handlers){
  679. winch = list_entry(ele, struct winch, list);
  680. if(winch->fd != -1){
  681. /* Why is this different from the above free_irq(),
  682. * which deactivates SIGIO? This searches the FD
  683. * somewhere else and removes it from the list... */
  684. deactivate_fd(winch->fd, WINCH_IRQ);
  685. os_close_file(winch->fd);
  686. }
  687. if(winch->pid != -1)
  688. os_kill_process(winch->pid, 1);
  689. }
  690. }
  691. __uml_exitcall(winch_cleanup);
  692. char *add_xterm_umid(char *base)
  693. {
  694. char *umid, *title;
  695. int len;
  696. umid = get_umid(1);
  697. if(umid == NULL)
  698. return base;
  699. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  700. title = kmalloc(len, GFP_KERNEL);
  701. if(title == NULL){
  702. printk("Failed to allocate buffer for xterm title\n");
  703. return base;
  704. }
  705. snprintf(title, len, "%s (%s)", base, umid);
  706. return title;
  707. }