sclp_tty.c 21 KB

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