line.c 21 KB

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