sclp_tty.c 15 KB

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