sclp_tty.c 15 KB

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