sclp_tty.c 15 KB

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