line.c 18 KB

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