sclp_tty.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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/kmod.h>
  10. #include <linux/tty.h>
  11. #include <linux/tty_driver.h>
  12. #include <linux/tty_flip.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/gfp.h>
  17. #include <linux/uaccess.h>
  18. #include "ctrlchar.h"
  19. #include "sclp.h"
  20. #include "sclp_rw.h"
  21. #include "sclp_tty.h"
  22. /*
  23. * size of a buffer that collects single characters coming in
  24. * via sclp_tty_put_char()
  25. */
  26. #define SCLP_TTY_BUF_SIZE 512
  27. /*
  28. * There is exactly one SCLP terminal, so we can keep things simple
  29. * and allocate all variables statically.
  30. */
  31. /* Lock to guard over changes to global variables. */
  32. static spinlock_t sclp_tty_lock;
  33. /* List of free pages that can be used for console output buffering. */
  34. static struct list_head sclp_tty_pages;
  35. /* List of full struct sclp_buffer structures ready for output. */
  36. static struct list_head sclp_tty_outqueue;
  37. /* Counter how many buffers are emitted. */
  38. static int sclp_tty_buffer_count;
  39. /* Pointer to current console buffer. */
  40. static struct sclp_buffer *sclp_ttybuf;
  41. /* Timer for delayed output of console messages. */
  42. static struct timer_list sclp_tty_timer;
  43. static struct tty_port sclp_port;
  44. static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
  45. static unsigned short int sclp_tty_chars_count;
  46. struct tty_driver *sclp_tty_driver;
  47. static int sclp_tty_tolower;
  48. static int sclp_tty_columns = 80;
  49. #define SPACES_PER_TAB 8
  50. #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */
  51. /* This routine is called whenever we try to open a SCLP terminal. */
  52. static int
  53. sclp_tty_open(struct tty_struct *tty, struct file *filp)
  54. {
  55. tty_port_tty_set(&sclp_port, tty);
  56. tty->driver_data = NULL;
  57. sclp_port.low_latency = 0;
  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 int
  78. sclp_tty_write_room (struct tty_struct *tty)
  79. {
  80. unsigned long flags;
  81. struct list_head *l;
  82. 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(unsigned long data)
  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. init_timer(&sclp_tty_timer);
  198. sclp_tty_timer.function = sclp_tty_timeout;
  199. sclp_tty_timer.data = 0UL;
  200. sclp_tty_timer.expires = jiffies + HZ/10;
  201. add_timer(&sclp_tty_timer);
  202. }
  203. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  204. out:
  205. return overall_written;
  206. }
  207. /*
  208. * This routine is called by the kernel to write a series of characters to the
  209. * tty device. The characters may come from user space or kernel space. This
  210. * routine will return the number of characters actually accepted for writing.
  211. */
  212. static int
  213. sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  214. {
  215. if (sclp_tty_chars_count > 0) {
  216. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
  217. sclp_tty_chars_count = 0;
  218. }
  219. return sclp_tty_write_string(buf, count, 1);
  220. }
  221. /*
  222. * This routine is called by the kernel to write a single character to the tty
  223. * device. If the kernel uses this routine, it must call the flush_chars()
  224. * routine (if defined) when it is done stuffing characters into the driver.
  225. *
  226. * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
  227. * If the given character is a '\n' the contents of the SCLP write buffer
  228. * - including previous characters from sclp_tty_put_char() and strings from
  229. * sclp_write() without final '\n' - will be written.
  230. */
  231. static int
  232. sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
  233. {
  234. sclp_tty_chars[sclp_tty_chars_count++] = ch;
  235. if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
  236. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
  237. sclp_tty_chars_count = 0;
  238. }
  239. return 1;
  240. }
  241. /*
  242. * This routine is called by the kernel after it has written a series of
  243. * characters to the tty device using put_char().
  244. */
  245. static void
  246. sclp_tty_flush_chars(struct tty_struct *tty)
  247. {
  248. if (sclp_tty_chars_count > 0) {
  249. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
  250. sclp_tty_chars_count = 0;
  251. }
  252. }
  253. /*
  254. * This routine returns the number of characters in the write buffer of the
  255. * SCLP driver. The provided number includes all characters that are stored
  256. * in the SCCB (will be written next time the SCLP is not busy) as well as
  257. * characters in the write buffer (will not be written as long as there is a
  258. * final line feed missing).
  259. */
  260. static int
  261. sclp_tty_chars_in_buffer(struct tty_struct *tty)
  262. {
  263. unsigned long flags;
  264. struct list_head *l;
  265. struct sclp_buffer *t;
  266. int count;
  267. spin_lock_irqsave(&sclp_tty_lock, flags);
  268. count = 0;
  269. if (sclp_ttybuf != NULL)
  270. count = sclp_chars_in_buffer(sclp_ttybuf);
  271. list_for_each(l, &sclp_tty_outqueue) {
  272. t = list_entry(l, struct sclp_buffer, list);
  273. count += sclp_chars_in_buffer(t);
  274. }
  275. spin_unlock_irqrestore(&sclp_tty_lock, flags);
  276. return count;
  277. }
  278. /*
  279. * removes all content from buffers of low level driver
  280. */
  281. static void
  282. sclp_tty_flush_buffer(struct tty_struct *tty)
  283. {
  284. if (sclp_tty_chars_count > 0) {
  285. sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
  286. sclp_tty_chars_count = 0;
  287. }
  288. }
  289. /*
  290. * push input to tty
  291. */
  292. static void
  293. sclp_tty_input(unsigned char* buf, unsigned int count)
  294. {
  295. struct tty_struct *tty = tty_port_tty_get(&sclp_port);
  296. unsigned int cchar;
  297. /*
  298. * If this tty driver is currently closed
  299. * then throw the received input away.
  300. */
  301. if (tty == NULL)
  302. return;
  303. cchar = ctrlchar_handle(buf, count, tty);
  304. switch (cchar & CTRLCHAR_MASK) {
  305. case CTRLCHAR_SYSRQ:
  306. break;
  307. case CTRLCHAR_CTRL:
  308. tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL);
  309. tty_flip_buffer_push(&sclp_port);
  310. break;
  311. case CTRLCHAR_NONE:
  312. /* send (normal) input to line discipline */
  313. if (count < 2 ||
  314. (strncmp((const char *) buf + count - 2, "^n", 2) &&
  315. strncmp((const char *) buf + count - 2, "\252n", 2))) {
  316. /* add the auto \n */
  317. tty_insert_flip_string(&sclp_port, buf, count);
  318. tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL);
  319. } else
  320. tty_insert_flip_string(&sclp_port, buf, count - 2);
  321. tty_flip_buffer_push(&sclp_port);
  322. break;
  323. }
  324. tty_kref_put(tty);
  325. }
  326. /*
  327. * get a EBCDIC string in upper/lower case,
  328. * find out characters in lower/upper case separated by a special character,
  329. * modifiy original string,
  330. * returns length of resulting string
  331. */
  332. static int sclp_switch_cases(unsigned char *buf, int count)
  333. {
  334. unsigned char *ip, *op;
  335. int toggle;
  336. /* initially changing case is off */
  337. toggle = 0;
  338. ip = op = buf;
  339. while (count-- > 0) {
  340. /* compare with special character */
  341. if (*ip == CASE_DELIMITER) {
  342. /* followed by another special character? */
  343. if (count && ip[1] == CASE_DELIMITER) {
  344. /*
  345. * ... then put a single copy of the special
  346. * character to the output string
  347. */
  348. *op++ = *ip++;
  349. count--;
  350. } else
  351. /*
  352. * ... special character follower by a normal
  353. * character toggles the case change behaviour
  354. */
  355. toggle = ~toggle;
  356. /* skip special character */
  357. ip++;
  358. } else
  359. /* not the special character */
  360. if (toggle)
  361. /* but case switching is on */
  362. if (sclp_tty_tolower)
  363. /* switch to uppercase */
  364. *op++ = _ebc_toupper[(int) *ip++];
  365. else
  366. /* switch to lowercase */
  367. *op++ = _ebc_tolower[(int) *ip++];
  368. else
  369. /* no case switching, copy the character */
  370. *op++ = *ip++;
  371. }
  372. /* return length of reformatted string. */
  373. return op - buf;
  374. }
  375. static void sclp_get_input(struct gds_subvector *sv)
  376. {
  377. unsigned char *str;
  378. int count;
  379. str = (unsigned char *) (sv + 1);
  380. count = sv->length - sizeof(*sv);
  381. if (sclp_tty_tolower)
  382. EBC_TOLOWER(str, count);
  383. count = sclp_switch_cases(str, count);
  384. /* convert EBCDIC to ASCII (modify original input in SCCB) */
  385. sclp_ebcasc_str(str, count);
  386. /* transfer input to high level driver */
  387. sclp_tty_input(str, count);
  388. }
  389. static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
  390. {
  391. void *end;
  392. end = (void *) sv + sv->length;
  393. for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
  394. if (sv->key == 0x30)
  395. sclp_get_input(sv);
  396. }
  397. static inline void sclp_eval_textcmd(struct gds_vector *v)
  398. {
  399. struct gds_subvector *sv;
  400. void *end;
  401. end = (void *) v + v->length;
  402. for (sv = (struct gds_subvector *) (v + 1);
  403. (void *) sv < end; sv = (void *) sv + sv->length)
  404. if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
  405. sclp_eval_selfdeftextmsg(sv);
  406. }
  407. static inline void sclp_eval_cpmsu(struct gds_vector *v)
  408. {
  409. void *end;
  410. end = (void *) v + v->length;
  411. for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
  412. if (v->gds_id == GDS_ID_TEXTCMD)
  413. sclp_eval_textcmd(v);
  414. }
  415. static inline void sclp_eval_mdsmu(struct gds_vector *v)
  416. {
  417. v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
  418. if (v)
  419. sclp_eval_cpmsu(v);
  420. }
  421. static void sclp_tty_receiver(struct evbuf_header *evbuf)
  422. {
  423. struct gds_vector *v;
  424. v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
  425. GDS_ID_MDSMU);
  426. if (v)
  427. sclp_eval_mdsmu(v);
  428. }
  429. static void
  430. sclp_tty_state_change(struct sclp_register *reg)
  431. {
  432. }
  433. static struct sclp_register sclp_input_event =
  434. {
  435. .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
  436. .state_change_fn = sclp_tty_state_change,
  437. .receiver_fn = sclp_tty_receiver
  438. };
  439. static const struct tty_operations sclp_ops = {
  440. .open = sclp_tty_open,
  441. .close = sclp_tty_close,
  442. .write = sclp_tty_write,
  443. .put_char = sclp_tty_put_char,
  444. .flush_chars = sclp_tty_flush_chars,
  445. .write_room = sclp_tty_write_room,
  446. .chars_in_buffer = sclp_tty_chars_in_buffer,
  447. .flush_buffer = sclp_tty_flush_buffer,
  448. };
  449. static int __init
  450. sclp_tty_init(void)
  451. {
  452. struct tty_driver *driver;
  453. void *page;
  454. int i;
  455. int rc;
  456. if (!CONSOLE_IS_SCLP)
  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. init_timer(&sclp_tty_timer);
  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);