sclp_tty.c 15 KB

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