sclp_tty.c 21 KB

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