sclp_tty.c 21 KB

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