sclp_tty.c 21 KB

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