line.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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 chan *chan = data;
  24. struct line *line = chan->line;
  25. struct tty_struct *tty = line->tty;
  26. if (line)
  27. chan_interrupt(&line->chan_list, &line->task, tty, irq);
  28. return IRQ_HANDLED;
  29. }
  30. static void line_timer_cb(void *arg)
  31. {
  32. struct line *line = arg;
  33. chan_interrupt(&line->chan_list, &line->task, line->tty,
  34. line->driver->read_irq);
  35. }
  36. /* Returns the free space inside the ring buffer of this line.
  37. *
  38. * Should be called while holding line->lock (this does not modify datas).
  39. */
  40. static int write_room(struct line *line)
  41. {
  42. int n;
  43. if (line->buffer == NULL)
  44. return LINE_BUFSIZE - 1;
  45. /* This is for the case where the buffer is wrapped! */
  46. n = line->head - line->tail;
  47. if (n <= 0)
  48. n = LINE_BUFSIZE + n; /* The other case */
  49. return n - 1;
  50. }
  51. int line_write_room(struct tty_struct *tty)
  52. {
  53. struct line *line = tty->driver_data;
  54. unsigned long flags;
  55. int room;
  56. if (tty->stopped)
  57. return 0;
  58. spin_lock_irqsave(&line->lock, flags);
  59. room = write_room(line);
  60. spin_unlock_irqrestore(&line->lock, flags);
  61. /*XXX: Warning to remove */
  62. if (0 == room)
  63. printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
  64. __FUNCTION__,tty->name);
  65. return room;
  66. }
  67. int line_chars_in_buffer(struct tty_struct *tty)
  68. {
  69. struct line *line = tty->driver_data;
  70. unsigned long flags;
  71. int ret;
  72. spin_lock_irqsave(&line->lock, flags);
  73. /*write_room subtracts 1 for the needed NULL, so we readd it.*/
  74. ret = LINE_BUFSIZE - (write_room(line) + 1);
  75. spin_unlock_irqrestore(&line->lock, flags);
  76. return ret;
  77. }
  78. /*
  79. * This copies the content of buf into the circular buffer associated with
  80. * this line.
  81. * The return value is the number of characters actually copied, i.e. the ones
  82. * for which there was space: this function is not supposed to ever flush out
  83. * the circular buffer.
  84. *
  85. * Must be called while holding line->lock!
  86. */
  87. static int buffer_data(struct line *line, const char *buf, int len)
  88. {
  89. int end, room;
  90. if(line->buffer == NULL){
  91. line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
  92. if (line->buffer == NULL) {
  93. printk("buffer_data - atomic allocation failed\n");
  94. return(0);
  95. }
  96. line->head = line->buffer;
  97. line->tail = line->buffer;
  98. }
  99. room = write_room(line);
  100. len = (len > room) ? room : len;
  101. end = line->buffer + LINE_BUFSIZE - line->tail;
  102. if (len < end){
  103. memcpy(line->tail, buf, len);
  104. line->tail += len;
  105. }
  106. else {
  107. /* The circular buffer is wrapping */
  108. memcpy(line->tail, buf, end);
  109. buf += end;
  110. memcpy(line->buffer, buf, len - end);
  111. line->tail = line->buffer + len - end;
  112. }
  113. return len;
  114. }
  115. /*
  116. * Flushes the ring buffer to the output channels. That is, write_chan is
  117. * called, passing it line->head as buffer, and an appropriate count.
  118. *
  119. * On exit, returns 1 when the buffer is empty,
  120. * 0 when the buffer is not empty on exit,
  121. * and -errno when an error occurred.
  122. *
  123. * Must be called while holding line->lock!*/
  124. static int flush_buffer(struct line *line)
  125. {
  126. int n, count;
  127. if ((line->buffer == NULL) || (line->head == line->tail))
  128. return 1;
  129. if (line->tail < line->head) {
  130. /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
  131. count = line->buffer + LINE_BUFSIZE - line->head;
  132. n = write_chan(&line->chan_list, line->head, count,
  133. line->driver->write_irq);
  134. if (n < 0)
  135. return n;
  136. if (n == count) {
  137. /* We have flushed from ->head to buffer end, now we
  138. * must flush only from the beginning to ->tail.*/
  139. line->head = line->buffer;
  140. } else {
  141. line->head += n;
  142. return 0;
  143. }
  144. }
  145. count = line->tail - line->head;
  146. n = write_chan(&line->chan_list, line->head, count,
  147. line->driver->write_irq);
  148. if(n < 0)
  149. return n;
  150. line->head += n;
  151. return line->head == line->tail;
  152. }
  153. void line_flush_buffer(struct tty_struct *tty)
  154. {
  155. struct line *line = tty->driver_data;
  156. unsigned long flags;
  157. int err;
  158. /*XXX: copied from line_write, verify if it is correct!*/
  159. if(tty->stopped)
  160. return;
  161. //return 0;
  162. spin_lock_irqsave(&line->lock, flags);
  163. err = flush_buffer(line);
  164. /*if (err == 1)
  165. err = 0;*/
  166. spin_unlock_irqrestore(&line->lock, flags);
  167. //return err;
  168. }
  169. /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
  170. * and ->write. Hope it's not that bad.*/
  171. void line_flush_chars(struct tty_struct *tty)
  172. {
  173. line_flush_buffer(tty);
  174. }
  175. void line_put_char(struct tty_struct *tty, unsigned char ch)
  176. {
  177. line_write(tty, &ch, sizeof(ch));
  178. }
  179. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  180. {
  181. struct line *line = tty->driver_data;
  182. unsigned long flags;
  183. int n, err, ret = 0;
  184. if(tty->stopped)
  185. return 0;
  186. spin_lock_irqsave(&line->lock, flags);
  187. if (line->head != line->tail) {
  188. ret = buffer_data(line, buf, len);
  189. err = flush_buffer(line);
  190. if (err <= 0 && (err != -EAGAIN || !ret))
  191. ret = err;
  192. } else {
  193. n = write_chan(&line->chan_list, buf, len,
  194. line->driver->write_irq);
  195. if (n < 0) {
  196. ret = n;
  197. goto out_up;
  198. }
  199. len -= n;
  200. ret += n;
  201. if (len > 0)
  202. ret += buffer_data(line, buf + n, len);
  203. }
  204. out_up:
  205. spin_unlock_irqrestore(&line->lock, flags);
  206. return ret;
  207. }
  208. void line_set_termios(struct tty_struct *tty, struct termios * old)
  209. {
  210. /* nothing */
  211. }
  212. static struct {
  213. int cmd;
  214. char *level;
  215. char *name;
  216. } tty_ioctls[] = {
  217. /* don't print these, they flood the log ... */
  218. { TCGETS, NULL, "TCGETS" },
  219. { TCSETS, NULL, "TCSETS" },
  220. { TCSETSW, NULL, "TCSETSW" },
  221. { TCFLSH, NULL, "TCFLSH" },
  222. { TCSBRK, NULL, "TCSBRK" },
  223. /* general tty stuff */
  224. { TCSETSF, KERN_DEBUG, "TCSETSF" },
  225. { TCGETA, KERN_DEBUG, "TCGETA" },
  226. { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
  227. { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
  228. { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
  229. /* linux-specific ones */
  230. { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
  231. { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
  232. { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
  233. { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
  234. };
  235. int line_ioctl(struct tty_struct *tty, struct file * file,
  236. unsigned int cmd, unsigned long arg)
  237. {
  238. int ret;
  239. int i;
  240. ret = 0;
  241. switch(cmd) {
  242. #ifdef TIOCGETP
  243. case TIOCGETP:
  244. case TIOCSETP:
  245. case TIOCSETN:
  246. #endif
  247. #ifdef TIOCGETC
  248. case TIOCGETC:
  249. case TIOCSETC:
  250. #endif
  251. #ifdef TIOCGLTC
  252. case TIOCGLTC:
  253. case TIOCSLTC:
  254. #endif
  255. case TCGETS:
  256. case TCSETSF:
  257. case TCSETSW:
  258. case TCSETS:
  259. case TCGETA:
  260. case TCSETAF:
  261. case TCSETAW:
  262. case TCSETA:
  263. case TCXONC:
  264. case TCFLSH:
  265. case TIOCOUTQ:
  266. case TIOCINQ:
  267. case TIOCGLCKTRMIOS:
  268. case TIOCSLCKTRMIOS:
  269. case TIOCPKT:
  270. case TIOCGSOFTCAR:
  271. case TIOCSSOFTCAR:
  272. return -ENOIOCTLCMD;
  273. #if 0
  274. case TCwhatever:
  275. /* do something */
  276. break;
  277. #endif
  278. default:
  279. for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
  280. if (cmd == tty_ioctls[i].cmd)
  281. break;
  282. if (i < ARRAY_SIZE(tty_ioctls)) {
  283. if (NULL != tty_ioctls[i].level)
  284. printk("%s%s: %s: ioctl %s called\n",
  285. tty_ioctls[i].level, __FUNCTION__,
  286. tty->name, tty_ioctls[i].name);
  287. } else {
  288. printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
  289. __FUNCTION__, tty->name, cmd);
  290. }
  291. ret = -ENOIOCTLCMD;
  292. break;
  293. }
  294. return ret;
  295. }
  296. static irqreturn_t line_write_interrupt(int irq, void *data,
  297. struct pt_regs *unused)
  298. {
  299. struct chan *chan = data;
  300. struct line *line = chan->line;
  301. struct tty_struct *tty = line->tty;
  302. int err;
  303. /* Interrupts are enabled here because we registered the interrupt with
  304. * SA_INTERRUPT (see line_setup_irq).*/
  305. spin_lock_irq(&line->lock);
  306. err = flush_buffer(line);
  307. if (err == 0) {
  308. return IRQ_NONE;
  309. } else if(err < 0) {
  310. line->head = line->buffer;
  311. line->tail = line->buffer;
  312. }
  313. spin_unlock_irq(&line->lock);
  314. if(tty == NULL)
  315. return IRQ_NONE;
  316. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
  317. (tty->ldisc.write_wakeup != NULL))
  318. (tty->ldisc.write_wakeup)(tty);
  319. /* BLOCKING mode
  320. * In blocking mode, everything sleeps on tty->write_wait.
  321. * Sleeping in the console driver would break non-blocking
  322. * writes.
  323. */
  324. if (waitqueue_active(&tty->write_wait))
  325. wake_up_interruptible(&tty->write_wait);
  326. return IRQ_HANDLED;
  327. }
  328. int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
  329. {
  330. struct line_driver *driver = line->driver;
  331. int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
  332. if (input)
  333. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  334. line_interrupt, flags,
  335. driver->read_irq_name, data);
  336. if (err)
  337. return err;
  338. if (output)
  339. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  340. line_write_interrupt, flags,
  341. driver->write_irq_name, data);
  342. line->have_irq = 1;
  343. return err;
  344. }
  345. int line_open(struct line *lines, struct tty_struct *tty)
  346. {
  347. struct line *line;
  348. int err = -ENODEV;
  349. line = &lines[tty->index];
  350. tty->driver_data = line;
  351. /* The IRQ which takes this lock is not yet enabled and won't be run
  352. * before the end, so we don't need to use spin_lock_irq.*/
  353. spin_lock(&line->lock);
  354. tty->driver_data = line;
  355. line->tty = tty;
  356. if(!line->valid)
  357. goto out;
  358. if(tty->count == 1){
  359. /* Here the device is opened, if necessary, and interrupt
  360. * is registered.
  361. */
  362. enable_chan(line);
  363. INIT_WORK(&line->task, line_timer_cb, line);
  364. if(!line->sigio){
  365. chan_enable_winch(&line->chan_list, tty);
  366. line->sigio = 1;
  367. }
  368. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  369. &tty->winsize.ws_col);
  370. }
  371. err = 0;
  372. out:
  373. spin_unlock(&line->lock);
  374. return err;
  375. }
  376. static void unregister_winch(struct tty_struct *tty);
  377. void line_close(struct tty_struct *tty, struct file * filp)
  378. {
  379. struct line *line = tty->driver_data;
  380. /* XXX: I assume this should be called in process context, not with
  381. * interrupts disabled!
  382. */
  383. spin_lock_irq(&line->lock);
  384. /* We ignore the error anyway! */
  385. flush_buffer(line);
  386. if(tty->count == 1){
  387. line->tty = NULL;
  388. tty->driver_data = NULL;
  389. if(line->sigio){
  390. unregister_winch(tty);
  391. line->sigio = 0;
  392. }
  393. }
  394. spin_unlock_irq(&line->lock);
  395. }
  396. void close_lines(struct line *lines, int nlines)
  397. {
  398. int i;
  399. for(i = 0; i < nlines; i++)
  400. close_chan(&lines[i].chan_list, 0);
  401. }
  402. /* Common setup code for both startup command line and mconsole initialization.
  403. * @lines contains the the array (of size @num) to modify;
  404. * @init is the setup string;
  405. */
  406. int line_setup(struct line *lines, unsigned int num, char *init)
  407. {
  408. int i, n;
  409. char *end;
  410. if(*init == '=') {
  411. /* We said con=/ssl= instead of con#=, so we are configuring all
  412. * consoles at once.*/
  413. n = -1;
  414. }
  415. else {
  416. n = simple_strtoul(init, &end, 0);
  417. if(*end != '='){
  418. printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
  419. init);
  420. return 0;
  421. }
  422. init = end;
  423. }
  424. init++;
  425. if (n >= (signed int) num) {
  426. printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
  427. n, num - 1);
  428. return 0;
  429. }
  430. else if (n >= 0){
  431. if (lines[n].tty != NULL) {
  432. printk("line_setup - device %d is open\n", n);
  433. return 0;
  434. }
  435. if (lines[n].init_pri <= INIT_ONE){
  436. lines[n].init_pri = INIT_ONE;
  437. if (!strcmp(init, "none"))
  438. lines[n].valid = 0;
  439. else {
  440. lines[n].init_str = init;
  441. lines[n].valid = 1;
  442. }
  443. }
  444. }
  445. else {
  446. for(i = 0; i < num; i++){
  447. if(lines[i].init_pri <= INIT_ALL){
  448. lines[i].init_pri = INIT_ALL;
  449. if(!strcmp(init, "none")) lines[i].valid = 0;
  450. else {
  451. lines[i].init_str = init;
  452. lines[i].valid = 1;
  453. }
  454. }
  455. }
  456. }
  457. return n == -1 ? num : n;
  458. }
  459. int line_config(struct line *lines, unsigned int num, char *str,
  460. struct chan_opts *opts)
  461. {
  462. struct line *line;
  463. char *new;
  464. int n;
  465. if(*str == '='){
  466. printk("line_config - can't configure all devices from "
  467. "mconsole\n");
  468. return 1;
  469. }
  470. new = kstrdup(str, GFP_KERNEL);
  471. if(new == NULL){
  472. printk("line_config - kstrdup failed\n");
  473. return 1;
  474. }
  475. n = line_setup(lines, num, new);
  476. if(n < 0)
  477. return 1;
  478. line = &lines[n];
  479. return parse_chan_pair(line->init_str, line, n, opts);
  480. }
  481. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  482. int size, char **error_out)
  483. {
  484. struct line *line;
  485. char *end;
  486. int dev, n = 0;
  487. dev = simple_strtoul(name, &end, 0);
  488. if((*end != '\0') || (end == name)){
  489. *error_out = "line_get_config failed to parse device number";
  490. return 0;
  491. }
  492. if((dev < 0) || (dev >= num)){
  493. *error_out = "device number out of range";
  494. return 0;
  495. }
  496. line = &lines[dev];
  497. spin_lock(&line->lock);
  498. if(!line->valid)
  499. CONFIG_CHUNK(str, size, n, "none", 1);
  500. else if(line->tty == NULL)
  501. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  502. else n = chan_config_string(&line->chan_list, str, size, error_out);
  503. spin_unlock(&line->lock);
  504. return n;
  505. }
  506. int line_id(char **str, int *start_out, int *end_out)
  507. {
  508. char *end;
  509. int n;
  510. n = simple_strtoul(*str, &end, 0);
  511. if((*end != '\0') || (end == *str))
  512. return -1;
  513. *str = end;
  514. *start_out = n;
  515. *end_out = n;
  516. return n;
  517. }
  518. int line_remove(struct line *lines, unsigned int num, int n)
  519. {
  520. int err;
  521. char config[sizeof("conxxxx=none\0")];
  522. sprintf(config, "%d=none", n);
  523. err = line_setup(lines, num, config);
  524. if(err >= 0)
  525. err = 0;
  526. return err;
  527. }
  528. struct tty_driver *line_register_devfs(struct lines *set,
  529. struct line_driver *line_driver,
  530. struct tty_operations *ops, struct line *lines,
  531. int nlines)
  532. {
  533. int i;
  534. struct tty_driver *driver = alloc_tty_driver(nlines);
  535. if (!driver)
  536. return NULL;
  537. driver->driver_name = line_driver->name;
  538. driver->name = line_driver->device_name;
  539. driver->devfs_name = line_driver->devfs_name;
  540. driver->major = line_driver->major;
  541. driver->minor_start = line_driver->minor_start;
  542. driver->type = line_driver->type;
  543. driver->subtype = line_driver->subtype;
  544. driver->flags = TTY_DRIVER_REAL_RAW;
  545. driver->init_termios = tty_std_termios;
  546. tty_set_operations(driver, ops);
  547. if (tty_register_driver(driver)) {
  548. printk("%s: can't register %s driver\n",
  549. __FUNCTION__,line_driver->name);
  550. put_tty_driver(driver);
  551. return NULL;
  552. }
  553. for(i = 0; i < nlines; i++){
  554. if(!lines[i].valid)
  555. tty_unregister_device(driver, i);
  556. }
  557. mconsole_register_dev(&line_driver->mc);
  558. return driver;
  559. }
  560. static DEFINE_SPINLOCK(winch_handler_lock);
  561. static LIST_HEAD(winch_handlers);
  562. void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
  563. {
  564. struct line *line;
  565. int i;
  566. for(i = 0; i < nlines; i++){
  567. line = &lines[i];
  568. INIT_LIST_HEAD(&line->chan_list);
  569. if(line->init_str == NULL)
  570. continue;
  571. line->init_str = kstrdup(line->init_str, GFP_KERNEL);
  572. if(line->init_str == NULL)
  573. printk("lines_init - kstrdup returned NULL\n");
  574. if(parse_chan_pair(line->init_str, line, i, opts)){
  575. printk("parse_chan_pair failed for device %d\n", i);
  576. line->valid = 0;
  577. }
  578. }
  579. }
  580. struct winch {
  581. struct list_head list;
  582. int fd;
  583. int tty_fd;
  584. int pid;
  585. struct tty_struct *tty;
  586. };
  587. irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
  588. {
  589. struct winch *winch = data;
  590. struct tty_struct *tty;
  591. struct line *line;
  592. int err;
  593. char c;
  594. if(winch->fd != -1){
  595. err = generic_read(winch->fd, &c, NULL);
  596. if(err < 0){
  597. if(err != -EAGAIN){
  598. printk("winch_interrupt : read failed, "
  599. "errno = %d\n", -err);
  600. printk("fd %d is losing SIGWINCH support\n",
  601. winch->tty_fd);
  602. return IRQ_HANDLED;
  603. }
  604. goto out;
  605. }
  606. }
  607. tty = winch->tty;
  608. if (tty != NULL) {
  609. line = tty->driver_data;
  610. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  611. &tty->winsize.ws_col);
  612. kill_pg(tty->pgrp, SIGWINCH, 1);
  613. }
  614. out:
  615. if(winch->fd != -1)
  616. reactivate_fd(winch->fd, WINCH_IRQ);
  617. return IRQ_HANDLED;
  618. }
  619. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
  620. {
  621. struct winch *winch;
  622. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  623. if (winch == NULL) {
  624. printk("register_winch_irq - kmalloc failed\n");
  625. return;
  626. }
  627. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  628. .fd = fd,
  629. .tty_fd = tty_fd,
  630. .pid = pid,
  631. .tty = tty });
  632. spin_lock(&winch_handler_lock);
  633. list_add(&winch->list, &winch_handlers);
  634. spin_unlock(&winch_handler_lock);
  635. if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  636. SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
  637. "winch", winch) < 0)
  638. printk("register_winch_irq - failed to register IRQ\n");
  639. }
  640. static void unregister_winch(struct tty_struct *tty)
  641. {
  642. struct list_head *ele;
  643. struct winch *winch, *found = NULL;
  644. spin_lock(&winch_handler_lock);
  645. list_for_each(ele, &winch_handlers){
  646. winch = list_entry(ele, struct winch, list);
  647. if(winch->tty == tty){
  648. found = winch;
  649. break;
  650. }
  651. }
  652. if(found == NULL)
  653. goto err;
  654. list_del(&winch->list);
  655. spin_unlock(&winch_handler_lock);
  656. if(winch->pid != -1)
  657. os_kill_process(winch->pid, 1);
  658. free_irq(WINCH_IRQ, winch);
  659. kfree(winch);
  660. return;
  661. err:
  662. spin_unlock(&winch_handler_lock);
  663. }
  664. /* XXX: No lock as it's an exitcall... is this valid? Depending on cleanup
  665. * order... are we sure that nothing else is done on the list? */
  666. static void winch_cleanup(void)
  667. {
  668. struct list_head *ele;
  669. struct winch *winch;
  670. list_for_each(ele, &winch_handlers){
  671. winch = list_entry(ele, struct winch, list);
  672. if(winch->fd != -1){
  673. /* Why is this different from the above free_irq(),
  674. * which deactivates SIGIO? This searches the FD
  675. * somewhere else and removes it from the list... */
  676. deactivate_fd(winch->fd, WINCH_IRQ);
  677. os_close_file(winch->fd);
  678. }
  679. if(winch->pid != -1)
  680. os_kill_process(winch->pid, 1);
  681. }
  682. }
  683. __uml_exitcall(winch_cleanup);
  684. char *add_xterm_umid(char *base)
  685. {
  686. char *umid, *title;
  687. int len;
  688. umid = get_umid(1);
  689. if(umid == NULL)
  690. return base;
  691. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  692. title = kmalloc(len, GFP_KERNEL);
  693. if(title == NULL){
  694. printk("Failed to allocate buffer for xterm title\n");
  695. return base;
  696. }
  697. snprintf(title, len, "%s (%s)", base, umid);
  698. return title;
  699. }