sclp_tty.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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/err.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/gfp.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 sclp_get_input(struct gds_subvector *sv)
  379. {
  380. unsigned char *str;
  381. int count;
  382. str = (unsigned char *) (sv + 1);
  383. count = sv->length - sizeof(*sv);
  384. if (sclp_tty_tolower)
  385. EBC_TOLOWER(str, count);
  386. count = sclp_switch_cases(str, count);
  387. /* convert EBCDIC to ASCII (modify original input in SCCB) */
  388. sclp_ebcasc_str(str, count);
  389. /* transfer input to high level driver */
  390. sclp_tty_input(str, count);
  391. }
  392. static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
  393. {
  394. void *end;
  395. end = (void *) sv + sv->length;
  396. for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
  397. if (sv->key == 0x30)
  398. sclp_get_input(sv);
  399. }
  400. static inline void sclp_eval_textcmd(struct gds_vector *v)
  401. {
  402. struct gds_subvector *sv;
  403. void *end;
  404. end = (void *) v + v->length;
  405. for (sv = (struct gds_subvector *) (v + 1);
  406. (void *) sv < end; sv = (void *) sv + sv->length)
  407. if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
  408. sclp_eval_selfdeftextmsg(sv);
  409. }
  410. static inline void sclp_eval_cpmsu(struct gds_vector *v)
  411. {
  412. void *end;
  413. end = (void *) v + v->length;
  414. for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
  415. if (v->gds_id == GDS_ID_TEXTCMD)
  416. sclp_eval_textcmd(v);
  417. }
  418. static inline void sclp_eval_mdsmu(struct gds_vector *v)
  419. {
  420. v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
  421. if (v)
  422. sclp_eval_cpmsu(v);
  423. }
  424. static void sclp_tty_receiver(struct evbuf_header *evbuf)
  425. {
  426. struct gds_vector *v;
  427. v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
  428. GDS_ID_MDSMU);
  429. if (v)
  430. sclp_eval_mdsmu(v);
  431. }
  432. static void
  433. sclp_tty_state_change(struct sclp_register *reg)
  434. {
  435. }
  436. static struct sclp_register sclp_input_event =
  437. {
  438. .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
  439. .state_change_fn = sclp_tty_state_change,
  440. .receiver_fn = sclp_tty_receiver
  441. };
  442. static const struct tty_operations sclp_ops = {
  443. .open = sclp_tty_open,
  444. .close = sclp_tty_close,
  445. .write = sclp_tty_write,
  446. .put_char = sclp_tty_put_char,
  447. .flush_chars = sclp_tty_flush_chars,
  448. .write_room = sclp_tty_write_room,
  449. .chars_in_buffer = sclp_tty_chars_in_buffer,
  450. .flush_buffer = sclp_tty_flush_buffer,
  451. };
  452. static int __init
  453. sclp_tty_init(void)
  454. {
  455. struct tty_driver *driver;
  456. void *page;
  457. int i;
  458. int rc;
  459. if (!CONSOLE_IS_SCLP)
  460. return 0;
  461. driver = alloc_tty_driver(1);
  462. if (!driver)
  463. return -ENOMEM;
  464. rc = sclp_rw_init();
  465. if (rc) {
  466. put_tty_driver(driver);
  467. return rc;
  468. }
  469. /* Allocate pages for output buffering */
  470. INIT_LIST_HEAD(&sclp_tty_pages);
  471. for (i = 0; i < MAX_KMEM_PAGES; i++) {
  472. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  473. if (page == NULL) {
  474. put_tty_driver(driver);
  475. return -ENOMEM;
  476. }
  477. list_add_tail((struct list_head *) page, &sclp_tty_pages);
  478. }
  479. INIT_LIST_HEAD(&sclp_tty_outqueue);
  480. spin_lock_init(&sclp_tty_lock);
  481. init_timer(&sclp_tty_timer);
  482. sclp_ttybuf = NULL;
  483. sclp_tty_buffer_count = 0;
  484. if (MACHINE_IS_VM) {
  485. /*
  486. * save 4 characters for the CPU number
  487. * written at start of each line by VM/CP
  488. */
  489. sclp_tty_columns = 76;
  490. /* case input lines to lowercase */
  491. sclp_tty_tolower = 1;
  492. }
  493. sclp_tty_chars_count = 0;
  494. sclp_tty = NULL;
  495. rc = sclp_register(&sclp_input_event);
  496. if (rc) {
  497. put_tty_driver(driver);
  498. return rc;
  499. }
  500. driver->driver_name = "sclp_line";
  501. driver->name = "sclp_line";
  502. driver->major = TTY_MAJOR;
  503. driver->minor_start = 64;
  504. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  505. driver->subtype = SYSTEM_TYPE_TTY;
  506. driver->init_termios = tty_std_termios;
  507. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  508. driver->init_termios.c_oflag = ONLCR | XTABS;
  509. driver->init_termios.c_lflag = ISIG | ECHO;
  510. driver->flags = TTY_DRIVER_REAL_RAW;
  511. tty_set_operations(driver, &sclp_ops);
  512. rc = tty_register_driver(driver);
  513. if (rc) {
  514. put_tty_driver(driver);
  515. return rc;
  516. }
  517. sclp_tty_driver = driver;
  518. return 0;
  519. }
  520. module_init(sclp_tty_init);