sclp_tty.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <asm/uaccess.h>
  20. #include "ctrlchar.h"
  21. #include "sclp.h"
  22. #include "sclp_rw.h"
  23. #include "sclp_tty.h"
  24. /*
  25. * size of a buffer that collects single characters coming in
  26. * via sclp_tty_put_char()
  27. */
  28. #define SCLP_TTY_BUF_SIZE 512
  29. /*
  30. * There is exactly one SCLP terminal, so we can keep things simple
  31. * and allocate all variables statically.
  32. */
  33. /* Lock to guard over changes to global variables. */
  34. static spinlock_t sclp_tty_lock;
  35. /* List of free pages that can be used for console output buffering. */
  36. static struct list_head sclp_tty_pages;
  37. /* List of full struct sclp_buffer structures ready for output. */
  38. static struct list_head sclp_tty_outqueue;
  39. /* Counter how many buffers are emitted. */
  40. static int sclp_tty_buffer_count;
  41. /* Pointer to current console buffer. */
  42. static struct sclp_buffer *sclp_ttybuf;
  43. /* Timer for delayed output of console messages. */
  44. static struct timer_list sclp_tty_timer;
  45. static struct tty_struct *sclp_tty;
  46. static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
  47. static unsigned short int sclp_tty_chars_count;
  48. struct tty_driver *sclp_tty_driver;
  49. static int sclp_tty_tolower;
  50. static int sclp_tty_columns = 80;
  51. #define SPACES_PER_TAB 8
  52. #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */
  53. /* This routine is called whenever we try to open a SCLP terminal. */
  54. static int
  55. sclp_tty_open(struct tty_struct *tty, struct file *filp)
  56. {
  57. sclp_tty = tty;
  58. tty->driver_data = NULL;
  59. tty->low_latency = 0;
  60. return 0;
  61. }
  62. /* This routine is called when the SCLP terminal is closed. */
  63. static void
  64. sclp_tty_close(struct tty_struct *tty, struct file *filp)
  65. {
  66. if (tty->count > 1)
  67. return;
  68. sclp_tty = NULL;
  69. }
  70. /*
  71. * This routine returns the numbers of characters the tty driver
  72. * will accept for queuing to be written. This number is subject
  73. * to change as output buffers get emptied, or if the output flow
  74. * control is acted. This is not an exact number because not every
  75. * character needs the same space in the sccb. The worst case is
  76. * a string of newlines. Every newlines creates a new mto which
  77. * needs 8 bytes.
  78. */
  79. static int
  80. sclp_tty_write_room (struct tty_struct *tty)
  81. {
  82. unsigned long flags;
  83. struct list_head *l;
  84. int count;
  85. spin_lock_irqsave(&sclp_tty_lock, flags);
  86. count = 0;
  87. if (sclp_ttybuf != NULL)
  88. count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto);
  89. list_for_each(l, &sclp_tty_pages)
  90. count += NR_EMPTY_MTO_PER_SCCB;
  91. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  92. return count;
  93. }
  94. static void
  95. sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
  96. {
  97. unsigned long flags;
  98. void *page;
  99. do {
  100. page = sclp_unmake_buffer(buffer);
  101. spin_lock_irqsave(&sclp_tty_lock, flags);
  102. /* Remove buffer from outqueue */
  103. list_del(&buffer->list);
  104. sclp_tty_buffer_count--;
  105. list_add_tail((struct list_head *) page, &sclp_tty_pages);
  106. /* Check if there is a pending buffer on the out queue. */
  107. buffer = NULL;
  108. if (!list_empty(&sclp_tty_outqueue))
  109. buffer = list_entry(sclp_tty_outqueue.next,
  110. struct sclp_buffer, list);
  111. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  112. } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
  113. /* check if the tty needs a wake up call */
  114. if (sclp_tty != NULL) {
  115. tty_wakeup(sclp_tty);
  116. }
  117. }
  118. static inline void
  119. __sclp_ttybuf_emit(struct sclp_buffer *buffer)
  120. {
  121. unsigned long flags;
  122. int count;
  123. int rc;
  124. spin_lock_irqsave(&sclp_tty_lock, flags);
  125. list_add_tail(&buffer->list, &sclp_tty_outqueue);
  126. count = sclp_tty_buffer_count++;
  127. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  128. if (count)
  129. return;
  130. rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
  131. if (rc)
  132. sclp_ttybuf_callback(buffer, rc);
  133. }
  134. /*
  135. * When this routine is called from the timer then we flush the
  136. * temporary write buffer.
  137. */
  138. static void
  139. sclp_tty_timeout(unsigned long data)
  140. {
  141. unsigned long flags;
  142. struct sclp_buffer *buf;
  143. spin_lock_irqsave(&sclp_tty_lock, flags);
  144. buf = sclp_ttybuf;
  145. sclp_ttybuf = NULL;
  146. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  147. if (buf != NULL) {
  148. __sclp_ttybuf_emit(buf);
  149. }
  150. }
  151. /*
  152. * Write a string to the sclp tty.
  153. */
  154. static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail)
  155. {
  156. unsigned long flags;
  157. void *page;
  158. int written;
  159. int overall_written;
  160. struct sclp_buffer *buf;
  161. if (count <= 0)
  162. return 0;
  163. overall_written = 0;
  164. spin_lock_irqsave(&sclp_tty_lock, flags);
  165. do {
  166. /* Create a sclp output buffer if none exists yet */
  167. if (sclp_ttybuf == NULL) {
  168. while (list_empty(&sclp_tty_pages)) {
  169. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  170. if (may_fail)
  171. goto out;
  172. else
  173. sclp_sync_wait();
  174. spin_lock_irqsave(&sclp_tty_lock, flags);
  175. }
  176. page = sclp_tty_pages.next;
  177. list_del((struct list_head *) page);
  178. sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns,
  179. SPACES_PER_TAB);
  180. }
  181. /* try to write the string to the current output buffer */
  182. written = sclp_write(sclp_ttybuf, str, count);
  183. overall_written += written;
  184. if (written == count)
  185. break;
  186. /*
  187. * Not all characters could be written to the current
  188. * output buffer. Emit the buffer, create a new buffer
  189. * and then output the rest of the string.
  190. */
  191. buf = sclp_ttybuf;
  192. sclp_ttybuf = NULL;
  193. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  194. __sclp_ttybuf_emit(buf);
  195. spin_lock_irqsave(&sclp_tty_lock, flags);
  196. str += written;
  197. count -= written;
  198. } while (count > 0);
  199. /* Setup timer to output current console buffer after 1/10 second */
  200. if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) &&
  201. !timer_pending(&sclp_tty_timer)) {
  202. init_timer(&sclp_tty_timer);
  203. sclp_tty_timer.function = sclp_tty_timeout;
  204. sclp_tty_timer.data = 0UL;
  205. sclp_tty_timer.expires = jiffies + HZ/10;
  206. add_timer(&sclp_tty_timer);
  207. }
  208. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  209. out:
  210. return overall_written;
  211. }
  212. /*
  213. * This routine is called by the kernel to write a series of characters to the
  214. * tty device. The characters may come from user space or kernel space. This
  215. * routine will return the number of characters actually accepted for writing.
  216. */
  217. static int
  218. sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  219. {
  220. if (sclp_tty_chars_count > 0) {
  221. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
  222. sclp_tty_chars_count = 0;
  223. }
  224. return sclp_tty_write_string(buf, count, 1);
  225. }
  226. /*
  227. * This routine is called by the kernel to write a single character to the tty
  228. * device. If the kernel uses this routine, it must call the flush_chars()
  229. * routine (if defined) when it is done stuffing characters into the driver.
  230. *
  231. * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
  232. * If the given character is a '\n' the contents of the SCLP write buffer
  233. * - including previous characters from sclp_tty_put_char() and strings from
  234. * sclp_write() without final '\n' - will be written.
  235. */
  236. static int
  237. sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
  238. {
  239. sclp_tty_chars[sclp_tty_chars_count++] = ch;
  240. if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
  241. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
  242. sclp_tty_chars_count = 0;
  243. }
  244. return 1;
  245. }
  246. /*
  247. * This routine is called by the kernel after it has written a series of
  248. * characters to the tty device using put_char().
  249. */
  250. static void
  251. sclp_tty_flush_chars(struct tty_struct *tty)
  252. {
  253. if (sclp_tty_chars_count > 0) {
  254. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
  255. sclp_tty_chars_count = 0;
  256. }
  257. }
  258. /*
  259. * This routine returns the number of characters in the write buffer of the
  260. * SCLP driver. The provided number includes all characters that are stored
  261. * in the SCCB (will be written next time the SCLP is not busy) as well as
  262. * characters in the write buffer (will not be written as long as there is a
  263. * final line feed missing).
  264. */
  265. static int
  266. sclp_tty_chars_in_buffer(struct tty_struct *tty)
  267. {
  268. unsigned long flags;
  269. struct list_head *l;
  270. struct sclp_buffer *t;
  271. int count;
  272. spin_lock_irqsave(&sclp_tty_lock, flags);
  273. count = 0;
  274. if (sclp_ttybuf != NULL)
  275. count = sclp_chars_in_buffer(sclp_ttybuf);
  276. list_for_each(l, &sclp_tty_outqueue) {
  277. t = list_entry(l, struct sclp_buffer, list);
  278. count += sclp_chars_in_buffer(t);
  279. }
  280. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  281. return count;
  282. }
  283. /*
  284. * removes all content from buffers of low level driver
  285. */
  286. static void
  287. sclp_tty_flush_buffer(struct tty_struct *tty)
  288. {
  289. if (sclp_tty_chars_count > 0) {
  290. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
  291. sclp_tty_chars_count = 0;
  292. }
  293. }
  294. /*
  295. * push input to tty
  296. */
  297. static void
  298. sclp_tty_input(unsigned char* buf, unsigned int count)
  299. {
  300. unsigned int cchar;
  301. /*
  302. * If this tty driver is currently closed
  303. * then throw the received input away.
  304. */
  305. if (sclp_tty == NULL)
  306. return;
  307. cchar = ctrlchar_handle(buf, count, sclp_tty);
  308. switch (cchar & CTRLCHAR_MASK) {
  309. case CTRLCHAR_SYSRQ:
  310. break;
  311. case CTRLCHAR_CTRL:
  312. tty_insert_flip_char(sclp_tty, cchar, TTY_NORMAL);
  313. tty_flip_buffer_push(sclp_tty);
  314. break;
  315. case CTRLCHAR_NONE:
  316. /* send (normal) input to line discipline */
  317. if (count < 2 ||
  318. (strncmp((const char *) buf + count - 2, "^n", 2) &&
  319. strncmp((const char *) buf + count - 2, "\252n", 2))) {
  320. /* add the auto \n */
  321. tty_insert_flip_string(sclp_tty, buf, count);
  322. tty_insert_flip_char(sclp_tty, '\n', TTY_NORMAL);
  323. } else
  324. tty_insert_flip_string(sclp_tty, buf, count - 2);
  325. tty_flip_buffer_push(sclp_tty);
  326. break;
  327. }
  328. }
  329. /*
  330. * get a EBCDIC string in upper/lower case,
  331. * find out characters in lower/upper case separated by a special character,
  332. * modifiy original string,
  333. * returns length of resulting string
  334. */
  335. static int sclp_switch_cases(unsigned char *buf, int count)
  336. {
  337. unsigned char *ip, *op;
  338. int toggle;
  339. /* initially changing case is off */
  340. toggle = 0;
  341. ip = op = buf;
  342. while (count-- > 0) {
  343. /* compare with special character */
  344. if (*ip == CASE_DELIMITER) {
  345. /* followed by another special character? */
  346. if (count && ip[1] == CASE_DELIMITER) {
  347. /*
  348. * ... then put a single copy of the special
  349. * character to the output string
  350. */
  351. *op++ = *ip++;
  352. count--;
  353. } else
  354. /*
  355. * ... special character follower by a normal
  356. * character toggles the case change behaviour
  357. */
  358. toggle = ~toggle;
  359. /* skip special character */
  360. ip++;
  361. } else
  362. /* not the special character */
  363. if (toggle)
  364. /* but case switching is on */
  365. if (sclp_tty_tolower)
  366. /* switch to uppercase */
  367. *op++ = _ebc_toupper[(int) *ip++];
  368. else
  369. /* switch to lowercase */
  370. *op++ = _ebc_tolower[(int) *ip++];
  371. else
  372. /* no case switching, copy the character */
  373. *op++ = *ip++;
  374. }
  375. /* return length of reformatted string. */
  376. return op - buf;
  377. }
  378. static void
  379. sclp_get_input(unsigned char *start, unsigned char *end)
  380. {
  381. int count;
  382. count = end - start;
  383. if (sclp_tty_tolower)
  384. EBC_TOLOWER(start, count);
  385. count = sclp_switch_cases(start, count);
  386. /* convert EBCDIC to ASCII (modify original input in SCCB) */
  387. sclp_ebcasc_str(start, count);
  388. /* transfer input to high level driver */
  389. sclp_tty_input(start, count);
  390. }
  391. static inline struct gds_vector *
  392. find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id)
  393. {
  394. struct gds_vector *vec;
  395. for (vec = start; vec < end; vec = (void *) vec + vec->length)
  396. if (vec->gds_id == id)
  397. return vec;
  398. return NULL;
  399. }
  400. static inline struct gds_subvector *
  401. find_gds_subvector(struct gds_subvector *start,
  402. struct gds_subvector *end, u8 key)
  403. {
  404. struct gds_subvector *subvec;
  405. for (subvec = start; subvec < end;
  406. subvec = (void *) subvec + subvec->length)
  407. if (subvec->key == key)
  408. return subvec;
  409. return NULL;
  410. }
  411. static inline void
  412. sclp_eval_selfdeftextmsg(struct gds_subvector *start,
  413. struct gds_subvector *end)
  414. {
  415. struct gds_subvector *subvec;
  416. subvec = start;
  417. while (subvec < end) {
  418. subvec = find_gds_subvector(subvec, end, 0x30);
  419. if (!subvec)
  420. break;
  421. sclp_get_input((unsigned char *)(subvec + 1),
  422. (unsigned char *) subvec + subvec->length);
  423. subvec = (void *) subvec + subvec->length;
  424. }
  425. }
  426. static inline void
  427. sclp_eval_textcmd(struct gds_subvector *start,
  428. struct gds_subvector *end)
  429. {
  430. struct gds_subvector *subvec;
  431. subvec = start;
  432. while (subvec < end) {
  433. subvec = find_gds_subvector(subvec, end,
  434. GDS_KEY_SELFDEFTEXTMSG);
  435. if (!subvec)
  436. break;
  437. sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1),
  438. (void *)subvec + subvec->length);
  439. subvec = (void *) subvec + subvec->length;
  440. }
  441. }
  442. static inline void
  443. sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end)
  444. {
  445. struct gds_vector *vec;
  446. vec = start;
  447. while (vec < end) {
  448. vec = find_gds_vector(vec, end, GDS_ID_TEXTCMD);
  449. if (!vec)
  450. break;
  451. sclp_eval_textcmd((struct gds_subvector *)(vec + 1),
  452. (void *) vec + vec->length);
  453. vec = (void *) vec + vec->length;
  454. }
  455. }
  456. static inline void
  457. sclp_eval_mdsmu(struct gds_vector *start, void *end)
  458. {
  459. struct gds_vector *vec;
  460. vec = find_gds_vector(start, end, GDS_ID_CPMSU);
  461. if (vec)
  462. sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length);
  463. }
  464. static void
  465. sclp_tty_receiver(struct evbuf_header *evbuf)
  466. {
  467. struct gds_vector *start, *end, *vec;
  468. start = (struct gds_vector *)(evbuf + 1);
  469. end = (void *) evbuf + evbuf->length;
  470. vec = find_gds_vector(start, end, GDS_ID_MDSMU);
  471. if (vec)
  472. sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length);
  473. }
  474. static void
  475. sclp_tty_state_change(struct sclp_register *reg)
  476. {
  477. }
  478. static struct sclp_register sclp_input_event =
  479. {
  480. .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
  481. .state_change_fn = sclp_tty_state_change,
  482. .receiver_fn = sclp_tty_receiver
  483. };
  484. static const struct tty_operations sclp_ops = {
  485. .open = sclp_tty_open,
  486. .close = sclp_tty_close,
  487. .write = sclp_tty_write,
  488. .put_char = sclp_tty_put_char,
  489. .flush_chars = sclp_tty_flush_chars,
  490. .write_room = sclp_tty_write_room,
  491. .chars_in_buffer = sclp_tty_chars_in_buffer,
  492. .flush_buffer = sclp_tty_flush_buffer,
  493. };
  494. static int __init
  495. sclp_tty_init(void)
  496. {
  497. struct tty_driver *driver;
  498. void *page;
  499. int i;
  500. int rc;
  501. if (!CONSOLE_IS_SCLP)
  502. return 0;
  503. driver = alloc_tty_driver(1);
  504. if (!driver)
  505. return -ENOMEM;
  506. rc = sclp_rw_init();
  507. if (rc) {
  508. put_tty_driver(driver);
  509. return rc;
  510. }
  511. /* Allocate pages for output buffering */
  512. INIT_LIST_HEAD(&sclp_tty_pages);
  513. for (i = 0; i < MAX_KMEM_PAGES; i++) {
  514. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  515. if (page == NULL) {
  516. put_tty_driver(driver);
  517. return -ENOMEM;
  518. }
  519. list_add_tail((struct list_head *) page, &sclp_tty_pages);
  520. }
  521. INIT_LIST_HEAD(&sclp_tty_outqueue);
  522. spin_lock_init(&sclp_tty_lock);
  523. init_timer(&sclp_tty_timer);
  524. sclp_ttybuf = NULL;
  525. sclp_tty_buffer_count = 0;
  526. if (MACHINE_IS_VM) {
  527. /*
  528. * save 4 characters for the CPU number
  529. * written at start of each line by VM/CP
  530. */
  531. sclp_tty_columns = 76;
  532. /* case input lines to lowercase */
  533. sclp_tty_tolower = 1;
  534. }
  535. sclp_tty_chars_count = 0;
  536. sclp_tty = NULL;
  537. rc = sclp_register(&sclp_input_event);
  538. if (rc) {
  539. put_tty_driver(driver);
  540. return rc;
  541. }
  542. driver->owner = THIS_MODULE;
  543. driver->driver_name = "sclp_line";
  544. driver->name = "sclp_line";
  545. driver->major = TTY_MAJOR;
  546. driver->minor_start = 64;
  547. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  548. driver->subtype = SYSTEM_TYPE_TTY;
  549. driver->init_termios = tty_std_termios;
  550. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  551. driver->init_termios.c_oflag = ONLCR | XTABS;
  552. driver->init_termios.c_lflag = ISIG | ECHO;
  553. driver->flags = TTY_DRIVER_REAL_RAW;
  554. tty_set_operations(driver, &sclp_ops);
  555. rc = tty_register_driver(driver);
  556. if (rc) {
  557. put_tty_driver(driver);
  558. return rc;
  559. }
  560. sclp_tty_driver = driver;
  561. return 0;
  562. }
  563. module_init(sclp_tty_init);