sclp_tty.c 15 KB

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