sclp_tty.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * drivers/s390/char/sclp_tty.c
  3. * SCLP line mode terminal driver.
  4. *
  5. * S390 version
  6. * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Martin Peschke <mpeschke@de.ibm.com>
  8. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  9. */
  10. #include <linux/config.h>
  11. #include <linux/module.h>
  12. #include <linux/kmod.h>
  13. #include <linux/tty.h>
  14. #include <linux/tty_driver.h>
  15. #include <linux/sched.h>
  16. #include <linux/wait.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <asm/uaccess.h>
  22. #include "ctrlchar.h"
  23. #include "sclp.h"
  24. #include "sclp_rw.h"
  25. #include "sclp_tty.h"
  26. #define SCLP_TTY_PRINT_HEADER "sclp tty driver: "
  27. /*
  28. * size of a buffer that collects single characters coming in
  29. * via sclp_tty_put_char()
  30. */
  31. #define SCLP_TTY_BUF_SIZE 512
  32. /*
  33. * There is exactly one SCLP terminal, so we can keep things simple
  34. * and allocate all variables statically.
  35. */
  36. /* Lock to guard over changes to global variables. */
  37. static spinlock_t sclp_tty_lock;
  38. /* List of free pages that can be used for console output buffering. */
  39. static struct list_head sclp_tty_pages;
  40. /* List of full struct sclp_buffer structures ready for output. */
  41. static struct list_head sclp_tty_outqueue;
  42. /* Counter how many buffers are emitted. */
  43. static int sclp_tty_buffer_count;
  44. /* Pointer to current console buffer. */
  45. static struct sclp_buffer *sclp_ttybuf;
  46. /* Timer for delayed output of console messages. */
  47. static struct timer_list sclp_tty_timer;
  48. /* Waitqueue to wait for buffers to get empty. */
  49. static wait_queue_head_t sclp_tty_waitq;
  50. static struct tty_struct *sclp_tty;
  51. static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
  52. static unsigned short int sclp_tty_chars_count;
  53. struct tty_driver *sclp_tty_driver;
  54. extern struct termios tty_std_termios;
  55. static struct sclp_ioctls sclp_ioctls;
  56. static struct sclp_ioctls sclp_ioctls_init =
  57. {
  58. 8, /* 1 hor. tab. = 8 spaces */
  59. 0, /* no echo of input by this driver */
  60. 80, /* 80 characters/line */
  61. 1, /* write after 1/10 s without final new line */
  62. MAX_KMEM_PAGES, /* quick fix: avoid __alloc_pages */
  63. MAX_KMEM_PAGES, /* take 32/64 pages from kernel memory, */
  64. 0, /* do not convert to lower case */
  65. 0x6c /* to seprate upper and lower case */
  66. /* ('%' in EBCDIC) */
  67. };
  68. /* This routine is called whenever we try to open a SCLP terminal. */
  69. static int
  70. sclp_tty_open(struct tty_struct *tty, struct file *filp)
  71. {
  72. sclp_tty = tty;
  73. tty->driver_data = NULL;
  74. tty->low_latency = 0;
  75. return 0;
  76. }
  77. /* This routine is called when the SCLP terminal is closed. */
  78. static void
  79. sclp_tty_close(struct tty_struct *tty, struct file *filp)
  80. {
  81. if (tty->count > 1)
  82. return;
  83. sclp_tty = NULL;
  84. }
  85. /* execute commands to control the i/o behaviour of the SCLP tty at runtime */
  86. static int
  87. sclp_tty_ioctl(struct tty_struct *tty, struct file * file,
  88. unsigned int cmd, unsigned long arg)
  89. {
  90. unsigned long flags;
  91. unsigned int obuf;
  92. int check;
  93. int rc;
  94. if (tty->flags & (1 << TTY_IO_ERROR))
  95. return -EIO;
  96. rc = 0;
  97. check = 0;
  98. switch (cmd) {
  99. case TIOCSCLPSHTAB:
  100. /* set width of horizontal tab */
  101. if (get_user(sclp_ioctls.htab, (unsigned short __user *) arg))
  102. rc = -EFAULT;
  103. else
  104. check = 1;
  105. break;
  106. case TIOCSCLPGHTAB:
  107. /* get width of horizontal tab */
  108. if (put_user(sclp_ioctls.htab, (unsigned short __user *) arg))
  109. rc = -EFAULT;
  110. break;
  111. case TIOCSCLPSECHO:
  112. /* enable/disable echo of input */
  113. if (get_user(sclp_ioctls.echo, (unsigned char __user *) arg))
  114. rc = -EFAULT;
  115. break;
  116. case TIOCSCLPGECHO:
  117. /* Is echo of input enabled ? */
  118. if (put_user(sclp_ioctls.echo, (unsigned char __user *) arg))
  119. rc = -EFAULT;
  120. break;
  121. case TIOCSCLPSCOLS:
  122. /* set number of columns for output */
  123. if (get_user(sclp_ioctls.columns, (unsigned short __user *) arg))
  124. rc = -EFAULT;
  125. else
  126. check = 1;
  127. break;
  128. case TIOCSCLPGCOLS:
  129. /* get number of columns for output */
  130. if (put_user(sclp_ioctls.columns, (unsigned short __user *) arg))
  131. rc = -EFAULT;
  132. break;
  133. case TIOCSCLPSNL:
  134. /* enable/disable writing without final new line character */
  135. if (get_user(sclp_ioctls.final_nl, (signed char __user *) arg))
  136. rc = -EFAULT;
  137. break;
  138. case TIOCSCLPGNL:
  139. /* Is writing without final new line character enabled ? */
  140. if (put_user(sclp_ioctls.final_nl, (signed char __user *) arg))
  141. rc = -EFAULT;
  142. break;
  143. case TIOCSCLPSOBUF:
  144. /*
  145. * set the maximum buffers size for output, will be rounded
  146. * up to next 4kB boundary and stored as number of SCCBs
  147. * (4kB Buffers) limitation: 256 x 4kB
  148. */
  149. if (get_user(obuf, (unsigned int __user *) arg) == 0) {
  150. if (obuf & 0xFFF)
  151. sclp_ioctls.max_sccb = (obuf >> 12) + 1;
  152. else
  153. sclp_ioctls.max_sccb = (obuf >> 12);
  154. } else
  155. rc = -EFAULT;
  156. break;
  157. case TIOCSCLPGOBUF:
  158. /* get the maximum buffers size for output */
  159. obuf = sclp_ioctls.max_sccb << 12;
  160. if (put_user(obuf, (unsigned int __user *) arg))
  161. rc = -EFAULT;
  162. break;
  163. case TIOCSCLPGKBUF:
  164. /* get the number of buffers got from kernel at startup */
  165. if (put_user(sclp_ioctls.kmem_sccb, (unsigned short __user *) arg))
  166. rc = -EFAULT;
  167. break;
  168. case TIOCSCLPSCASE:
  169. /* enable/disable conversion from upper to lower case */
  170. if (get_user(sclp_ioctls.tolower, (unsigned char __user *) arg))
  171. rc = -EFAULT;
  172. break;
  173. case TIOCSCLPGCASE:
  174. /* Is conversion from upper to lower case of input enabled? */
  175. if (put_user(sclp_ioctls.tolower, (unsigned char __user *) arg))
  176. rc = -EFAULT;
  177. break;
  178. case TIOCSCLPSDELIM:
  179. /*
  180. * set special character used for separating upper and
  181. * lower case, 0x00 disables this feature
  182. */
  183. if (get_user(sclp_ioctls.delim, (unsigned char __user *) arg))
  184. rc = -EFAULT;
  185. break;
  186. case TIOCSCLPGDELIM:
  187. /*
  188. * get special character used for separating upper and
  189. * lower case, 0x00 disables this feature
  190. */
  191. if (put_user(sclp_ioctls.delim, (unsigned char __user *) arg))
  192. rc = -EFAULT;
  193. break;
  194. case TIOCSCLPSINIT:
  195. /* set initial (default) sclp ioctls */
  196. sclp_ioctls = sclp_ioctls_init;
  197. check = 1;
  198. break;
  199. default:
  200. rc = -ENOIOCTLCMD;
  201. break;
  202. }
  203. if (check) {
  204. spin_lock_irqsave(&sclp_tty_lock, flags);
  205. if (sclp_ttybuf != NULL) {
  206. sclp_set_htab(sclp_ttybuf, sclp_ioctls.htab);
  207. sclp_set_columns(sclp_ttybuf, sclp_ioctls.columns);
  208. }
  209. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  210. }
  211. return rc;
  212. }
  213. /*
  214. * This routine returns the numbers of characters the tty driver
  215. * will accept for queuing to be written. This number is subject
  216. * to change as output buffers get emptied, or if the output flow
  217. * control is acted. This is not an exact number because not every
  218. * character needs the same space in the sccb. The worst case is
  219. * a string of newlines. Every newlines creates a new mto which
  220. * needs 8 bytes.
  221. */
  222. static int
  223. sclp_tty_write_room (struct tty_struct *tty)
  224. {
  225. unsigned long flags;
  226. struct list_head *l;
  227. int count;
  228. spin_lock_irqsave(&sclp_tty_lock, flags);
  229. count = 0;
  230. if (sclp_ttybuf != NULL)
  231. count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto);
  232. list_for_each(l, &sclp_tty_pages)
  233. count += NR_EMPTY_MTO_PER_SCCB;
  234. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  235. return count;
  236. }
  237. static void
  238. sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
  239. {
  240. unsigned long flags;
  241. void *page;
  242. do {
  243. page = sclp_unmake_buffer(buffer);
  244. spin_lock_irqsave(&sclp_tty_lock, flags);
  245. /* Remove buffer from outqueue */
  246. list_del(&buffer->list);
  247. sclp_tty_buffer_count--;
  248. list_add_tail((struct list_head *) page, &sclp_tty_pages);
  249. /* Check if there is a pending buffer on the out queue. */
  250. buffer = NULL;
  251. if (!list_empty(&sclp_tty_outqueue))
  252. buffer = list_entry(sclp_tty_outqueue.next,
  253. struct sclp_buffer, list);
  254. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  255. } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
  256. wake_up(&sclp_tty_waitq);
  257. /* check if the tty needs a wake up call */
  258. if (sclp_tty != NULL) {
  259. tty_wakeup(sclp_tty);
  260. }
  261. }
  262. static inline void
  263. __sclp_ttybuf_emit(struct sclp_buffer *buffer)
  264. {
  265. unsigned long flags;
  266. int count;
  267. int rc;
  268. spin_lock_irqsave(&sclp_tty_lock, flags);
  269. list_add_tail(&buffer->list, &sclp_tty_outqueue);
  270. count = sclp_tty_buffer_count++;
  271. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  272. if (count)
  273. return;
  274. rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
  275. if (rc)
  276. sclp_ttybuf_callback(buffer, rc);
  277. }
  278. /*
  279. * When this routine is called from the timer then we flush the
  280. * temporary write buffer.
  281. */
  282. static void
  283. sclp_tty_timeout(unsigned long data)
  284. {
  285. unsigned long flags;
  286. struct sclp_buffer *buf;
  287. spin_lock_irqsave(&sclp_tty_lock, flags);
  288. buf = sclp_ttybuf;
  289. sclp_ttybuf = NULL;
  290. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  291. if (buf != NULL) {
  292. __sclp_ttybuf_emit(buf);
  293. }
  294. }
  295. /*
  296. * Write a string to the sclp tty.
  297. */
  298. static void
  299. sclp_tty_write_string(const unsigned char *str, int count)
  300. {
  301. unsigned long flags;
  302. void *page;
  303. int written;
  304. struct sclp_buffer *buf;
  305. if (count <= 0)
  306. return;
  307. spin_lock_irqsave(&sclp_tty_lock, flags);
  308. do {
  309. /* Create a sclp output buffer if none exists yet */
  310. if (sclp_ttybuf == NULL) {
  311. while (list_empty(&sclp_tty_pages)) {
  312. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  313. if (in_interrupt())
  314. sclp_sync_wait();
  315. else
  316. wait_event(sclp_tty_waitq,
  317. !list_empty(&sclp_tty_pages));
  318. spin_lock_irqsave(&sclp_tty_lock, flags);
  319. }
  320. page = sclp_tty_pages.next;
  321. list_del((struct list_head *) page);
  322. sclp_ttybuf = sclp_make_buffer(page,
  323. sclp_ioctls.columns,
  324. sclp_ioctls.htab);
  325. }
  326. /* try to write the string to the current output buffer */
  327. written = sclp_write(sclp_ttybuf, str, count);
  328. if (written == count)
  329. break;
  330. /*
  331. * Not all characters could be written to the current
  332. * output buffer. Emit the buffer, create a new buffer
  333. * and then output the rest of the string.
  334. */
  335. buf = sclp_ttybuf;
  336. sclp_ttybuf = NULL;
  337. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  338. __sclp_ttybuf_emit(buf);
  339. spin_lock_irqsave(&sclp_tty_lock, flags);
  340. str += written;
  341. count -= written;
  342. } while (count > 0);
  343. /* Setup timer to output current console buffer after 1/10 second */
  344. if (sclp_ioctls.final_nl) {
  345. if (sclp_ttybuf != NULL &&
  346. sclp_chars_in_buffer(sclp_ttybuf) != 0 &&
  347. !timer_pending(&sclp_tty_timer)) {
  348. init_timer(&sclp_tty_timer);
  349. sclp_tty_timer.function = sclp_tty_timeout;
  350. sclp_tty_timer.data = 0UL;
  351. sclp_tty_timer.expires = jiffies + HZ/10;
  352. add_timer(&sclp_tty_timer);
  353. }
  354. } else {
  355. if (sclp_ttybuf != NULL &&
  356. sclp_chars_in_buffer(sclp_ttybuf) != 0) {
  357. buf = sclp_ttybuf;
  358. sclp_ttybuf = NULL;
  359. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  360. __sclp_ttybuf_emit(buf);
  361. spin_lock_irqsave(&sclp_tty_lock, flags);
  362. }
  363. }
  364. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  365. }
  366. /*
  367. * This routine is called by the kernel to write a series of characters to the
  368. * tty device. The characters may come from user space or kernel space. This
  369. * routine will return the number of characters actually accepted for writing.
  370. */
  371. static int
  372. sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  373. {
  374. if (sclp_tty_chars_count > 0) {
  375. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
  376. sclp_tty_chars_count = 0;
  377. }
  378. sclp_tty_write_string(buf, count);
  379. return count;
  380. }
  381. /*
  382. * This routine is called by the kernel to write a single character to the tty
  383. * device. If the kernel uses this routine, it must call the flush_chars()
  384. * routine (if defined) when it is done stuffing characters into the driver.
  385. *
  386. * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
  387. * If the given character is a '\n' the contents of the SCLP write buffer
  388. * - including previous characters from sclp_tty_put_char() and strings from
  389. * sclp_write() without final '\n' - will be written.
  390. */
  391. static void
  392. sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
  393. {
  394. sclp_tty_chars[sclp_tty_chars_count++] = ch;
  395. if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
  396. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
  397. sclp_tty_chars_count = 0;
  398. }
  399. }
  400. /*
  401. * This routine is called by the kernel after it has written a series of
  402. * characters to the tty device using put_char().
  403. */
  404. static void
  405. sclp_tty_flush_chars(struct tty_struct *tty)
  406. {
  407. if (sclp_tty_chars_count > 0) {
  408. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
  409. sclp_tty_chars_count = 0;
  410. }
  411. }
  412. /*
  413. * This routine returns the number of characters in the write buffer of the
  414. * SCLP driver. The provided number includes all characters that are stored
  415. * in the SCCB (will be written next time the SCLP is not busy) as well as
  416. * characters in the write buffer (will not be written as long as there is a
  417. * final line feed missing).
  418. */
  419. static int
  420. sclp_tty_chars_in_buffer(struct tty_struct *tty)
  421. {
  422. unsigned long flags;
  423. struct list_head *l;
  424. struct sclp_buffer *t;
  425. int count;
  426. spin_lock_irqsave(&sclp_tty_lock, flags);
  427. count = 0;
  428. if (sclp_ttybuf != NULL)
  429. count = sclp_chars_in_buffer(sclp_ttybuf);
  430. list_for_each(l, &sclp_tty_outqueue) {
  431. t = list_entry(l, struct sclp_buffer, list);
  432. count += sclp_chars_in_buffer(t);
  433. }
  434. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  435. return count;
  436. }
  437. /*
  438. * removes all content from buffers of low level driver
  439. */
  440. static void
  441. sclp_tty_flush_buffer(struct tty_struct *tty)
  442. {
  443. if (sclp_tty_chars_count > 0) {
  444. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
  445. sclp_tty_chars_count = 0;
  446. }
  447. }
  448. /*
  449. * push input to tty
  450. */
  451. static void
  452. sclp_tty_input(unsigned char* buf, unsigned int count)
  453. {
  454. unsigned int cchar;
  455. /*
  456. * If this tty driver is currently closed
  457. * then throw the received input away.
  458. */
  459. if (sclp_tty == NULL)
  460. return;
  461. cchar = ctrlchar_handle(buf, count, sclp_tty);
  462. switch (cchar & CTRLCHAR_MASK) {
  463. case CTRLCHAR_SYSRQ:
  464. break;
  465. case CTRLCHAR_CTRL:
  466. sclp_tty->flip.count++;
  467. *sclp_tty->flip.flag_buf_ptr++ = TTY_NORMAL;
  468. *sclp_tty->flip.char_buf_ptr++ = cchar;
  469. tty_flip_buffer_push(sclp_tty);
  470. break;
  471. case CTRLCHAR_NONE:
  472. /* send (normal) input to line discipline */
  473. memcpy(sclp_tty->flip.char_buf_ptr, buf, count);
  474. if (count < 2 ||
  475. (strncmp ((const char *) buf + count - 2, "^n", 2) &&
  476. strncmp ((const char *) buf + count - 2, "\0252n", 2))) {
  477. sclp_tty->flip.char_buf_ptr[count] = '\n';
  478. count++;
  479. } else
  480. count -= 2;
  481. memset(sclp_tty->flip.flag_buf_ptr, TTY_NORMAL, count);
  482. sclp_tty->flip.char_buf_ptr += count;
  483. sclp_tty->flip.flag_buf_ptr += count;
  484. sclp_tty->flip.count += count;
  485. tty_flip_buffer_push(sclp_tty);
  486. break;
  487. }
  488. }
  489. /*
  490. * get a EBCDIC string in upper/lower case,
  491. * find out characters in lower/upper case separated by a special character,
  492. * modifiy original string,
  493. * returns length of resulting string
  494. */
  495. static int
  496. sclp_switch_cases(unsigned char *buf, int count,
  497. unsigned char delim, int tolower)
  498. {
  499. unsigned char *ip, *op;
  500. int toggle;
  501. /* initially changing case is off */
  502. toggle = 0;
  503. ip = op = buf;
  504. while (count-- > 0) {
  505. /* compare with special character */
  506. if (*ip == delim) {
  507. /* followed by another special character? */
  508. if (count && ip[1] == delim) {
  509. /*
  510. * ... then put a single copy of the special
  511. * character to the output string
  512. */
  513. *op++ = *ip++;
  514. count--;
  515. } else
  516. /*
  517. * ... special character follower by a normal
  518. * character toggles the case change behaviour
  519. */
  520. toggle = ~toggle;
  521. /* skip special character */
  522. ip++;
  523. } else
  524. /* not the special character */
  525. if (toggle)
  526. /* but case switching is on */
  527. if (tolower)
  528. /* switch to uppercase */
  529. *op++ = _ebc_toupper[(int) *ip++];
  530. else
  531. /* switch to lowercase */
  532. *op++ = _ebc_tolower[(int) *ip++];
  533. else
  534. /* no case switching, copy the character */
  535. *op++ = *ip++;
  536. }
  537. /* return length of reformatted string. */
  538. return op - buf;
  539. }
  540. static void
  541. sclp_get_input(unsigned char *start, unsigned char *end)
  542. {
  543. int count;
  544. count = end - start;
  545. /*
  546. * if set in ioctl convert EBCDIC to lower case
  547. * (modify original input in SCCB)
  548. */
  549. if (sclp_ioctls.tolower)
  550. EBC_TOLOWER(start, count);
  551. /*
  552. * if set in ioctl find out characters in lower or upper case
  553. * (depends on current case) separated by a special character,
  554. * works on EBCDIC
  555. */
  556. if (sclp_ioctls.delim)
  557. count = sclp_switch_cases(start, count,
  558. sclp_ioctls.delim,
  559. sclp_ioctls.tolower);
  560. /* convert EBCDIC to ASCII (modify original input in SCCB) */
  561. sclp_ebcasc_str(start, count);
  562. /* if set in ioctl write operators input to console */
  563. if (sclp_ioctls.echo)
  564. sclp_tty_write(sclp_tty, start, count);
  565. /* transfer input to high level driver */
  566. sclp_tty_input(start, count);
  567. }
  568. static inline struct gds_vector *
  569. find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id)
  570. {
  571. struct gds_vector *vec;
  572. for (vec = start; vec < end; vec = (void *) vec + vec->length)
  573. if (vec->gds_id == id)
  574. return vec;
  575. return NULL;
  576. }
  577. static inline struct gds_subvector *
  578. find_gds_subvector(struct gds_subvector *start,
  579. struct gds_subvector *end, u8 key)
  580. {
  581. struct gds_subvector *subvec;
  582. for (subvec = start; subvec < end;
  583. subvec = (void *) subvec + subvec->length)
  584. if (subvec->key == key)
  585. return subvec;
  586. return NULL;
  587. }
  588. static inline void
  589. sclp_eval_selfdeftextmsg(struct gds_subvector *start,
  590. struct gds_subvector *end)
  591. {
  592. struct gds_subvector *subvec;
  593. subvec = start;
  594. while (subvec < end) {
  595. subvec = find_gds_subvector(subvec, end, 0x30);
  596. if (!subvec)
  597. break;
  598. sclp_get_input((unsigned char *)(subvec + 1),
  599. (unsigned char *) subvec + subvec->length);
  600. subvec = (void *) subvec + subvec->length;
  601. }
  602. }
  603. static inline void
  604. sclp_eval_textcmd(struct gds_subvector *start,
  605. struct gds_subvector *end)
  606. {
  607. struct gds_subvector *subvec;
  608. subvec = start;
  609. while (subvec < end) {
  610. subvec = find_gds_subvector(subvec, end,
  611. GDS_KEY_SelfDefTextMsg);
  612. if (!subvec)
  613. break;
  614. sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1),
  615. (void *)subvec + subvec->length);
  616. subvec = (void *) subvec + subvec->length;
  617. }
  618. }
  619. static inline void
  620. sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end)
  621. {
  622. struct gds_vector *vec;
  623. vec = start;
  624. while (vec < end) {
  625. vec = find_gds_vector(vec, end, GDS_ID_TextCmd);
  626. if (!vec)
  627. break;
  628. sclp_eval_textcmd((struct gds_subvector *)(vec + 1),
  629. (void *) vec + vec->length);
  630. vec = (void *) vec + vec->length;
  631. }
  632. }
  633. static inline void
  634. sclp_eval_mdsmu(struct gds_vector *start, void *end)
  635. {
  636. struct gds_vector *vec;
  637. vec = find_gds_vector(start, end, GDS_ID_CPMSU);
  638. if (vec)
  639. sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length);
  640. }
  641. static void
  642. sclp_tty_receiver(struct evbuf_header *evbuf)
  643. {
  644. struct gds_vector *start, *end, *vec;
  645. start = (struct gds_vector *)(evbuf + 1);
  646. end = (void *) evbuf + evbuf->length;
  647. vec = find_gds_vector(start, end, GDS_ID_MDSMU);
  648. if (vec)
  649. sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length);
  650. }
  651. static void
  652. sclp_tty_state_change(struct sclp_register *reg)
  653. {
  654. }
  655. static struct sclp_register sclp_input_event =
  656. {
  657. .receive_mask = EvTyp_OpCmd_Mask | EvTyp_PMsgCmd_Mask,
  658. .state_change_fn = sclp_tty_state_change,
  659. .receiver_fn = sclp_tty_receiver
  660. };
  661. static struct tty_operations sclp_ops = {
  662. .open = sclp_tty_open,
  663. .close = sclp_tty_close,
  664. .write = sclp_tty_write,
  665. .put_char = sclp_tty_put_char,
  666. .flush_chars = sclp_tty_flush_chars,
  667. .write_room = sclp_tty_write_room,
  668. .chars_in_buffer = sclp_tty_chars_in_buffer,
  669. .flush_buffer = sclp_tty_flush_buffer,
  670. .ioctl = sclp_tty_ioctl,
  671. };
  672. int __init
  673. sclp_tty_init(void)
  674. {
  675. struct tty_driver *driver;
  676. void *page;
  677. int i;
  678. int rc;
  679. if (!CONSOLE_IS_SCLP)
  680. return 0;
  681. driver = alloc_tty_driver(1);
  682. if (!driver)
  683. return -ENOMEM;
  684. rc = sclp_rw_init();
  685. if (rc) {
  686. printk(KERN_ERR SCLP_TTY_PRINT_HEADER
  687. "could not register tty - "
  688. "sclp_rw_init returned %d\n", rc);
  689. put_tty_driver(driver);
  690. return rc;
  691. }
  692. /* Allocate pages for output buffering */
  693. INIT_LIST_HEAD(&sclp_tty_pages);
  694. for (i = 0; i < MAX_KMEM_PAGES; i++) {
  695. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  696. if (page == NULL) {
  697. put_tty_driver(driver);
  698. return -ENOMEM;
  699. }
  700. list_add_tail((struct list_head *) page, &sclp_tty_pages);
  701. }
  702. INIT_LIST_HEAD(&sclp_tty_outqueue);
  703. spin_lock_init(&sclp_tty_lock);
  704. init_waitqueue_head(&sclp_tty_waitq);
  705. init_timer(&sclp_tty_timer);
  706. sclp_ttybuf = NULL;
  707. sclp_tty_buffer_count = 0;
  708. if (MACHINE_IS_VM) {
  709. /*
  710. * save 4 characters for the CPU number
  711. * written at start of each line by VM/CP
  712. */
  713. sclp_ioctls_init.columns = 76;
  714. /* case input lines to lowercase */
  715. sclp_ioctls_init.tolower = 1;
  716. }
  717. sclp_ioctls = sclp_ioctls_init;
  718. sclp_tty_chars_count = 0;
  719. sclp_tty = NULL;
  720. rc = sclp_register(&sclp_input_event);
  721. if (rc) {
  722. put_tty_driver(driver);
  723. return rc;
  724. }
  725. driver->owner = THIS_MODULE;
  726. driver->driver_name = "sclp_line";
  727. driver->name = "sclp_line";
  728. driver->major = TTY_MAJOR;
  729. driver->minor_start = 64;
  730. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  731. driver->subtype = SYSTEM_TYPE_TTY;
  732. driver->init_termios = tty_std_termios;
  733. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  734. driver->init_termios.c_oflag = ONLCR | XTABS;
  735. driver->init_termios.c_lflag = ISIG | ECHO;
  736. driver->flags = TTY_DRIVER_REAL_RAW;
  737. tty_set_operations(driver, &sclp_ops);
  738. rc = tty_register_driver(driver);
  739. if (rc) {
  740. printk(KERN_ERR SCLP_TTY_PRINT_HEADER
  741. "could not register tty - "
  742. "tty_register_driver returned %d\n", rc);
  743. put_tty_driver(driver);
  744. return rc;
  745. }
  746. sclp_tty_driver = driver;
  747. return 0;
  748. }
  749. module_init(sclp_tty_init);