con3270.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * drivers/s390/char/con3270.c
  3. * IBM/3270 Driver - console view.
  4. *
  5. * Author(s):
  6. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  7. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. * -- Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
  9. */
  10. #include <linux/bootmem.h>
  11. #include <linux/console.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/list.h>
  15. #include <linux/types.h>
  16. #include <linux/err.h>
  17. #include <asm/ccwdev.h>
  18. #include <asm/cio.h>
  19. #include <asm/cpcmd.h>
  20. #include <asm/ebcdic.h>
  21. #include "raw3270.h"
  22. #include "ctrlchar.h"
  23. #define CON3270_OUTPUT_BUFFER_SIZE 1024
  24. #define CON3270_STRING_PAGES 4
  25. static struct raw3270_fn con3270_fn;
  26. /*
  27. * Main 3270 console view data structure.
  28. */
  29. struct con3270 {
  30. struct raw3270_view view;
  31. spinlock_t lock;
  32. struct list_head freemem; /* list of free memory for strings. */
  33. /* Output stuff. */
  34. struct list_head lines; /* list of lines. */
  35. struct list_head update; /* list of lines to update. */
  36. int line_nr; /* line number for next update. */
  37. int nr_lines; /* # lines in list. */
  38. int nr_up; /* # lines up in history. */
  39. unsigned long update_flags; /* Update indication bits. */
  40. struct string *cline; /* current output line. */
  41. struct string *status; /* last line of display. */
  42. struct raw3270_request *write; /* single write request. */
  43. struct timer_list timer;
  44. /* Input stuff. */
  45. struct string *input; /* input string for read request. */
  46. struct raw3270_request *read; /* single read request. */
  47. struct raw3270_request *kreset; /* single keyboard reset request. */
  48. struct tasklet_struct readlet; /* tasklet to issue read request. */
  49. };
  50. static struct con3270 *condev;
  51. /* con3270->update_flags. See con3270_update for details. */
  52. #define CON_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
  53. #define CON_UPDATE_LIST 2 /* Update lines in tty3270->update. */
  54. #define CON_UPDATE_STATUS 4 /* Update status line. */
  55. #define CON_UPDATE_ALL 7
  56. static void con3270_update(struct con3270 *);
  57. /*
  58. * Setup timeout for a device. On timeout trigger an update.
  59. */
  60. void
  61. con3270_set_timer(struct con3270 *cp, int expires)
  62. {
  63. if (expires == 0) {
  64. if (timer_pending(&cp->timer))
  65. del_timer(&cp->timer);
  66. return;
  67. }
  68. if (timer_pending(&cp->timer) &&
  69. mod_timer(&cp->timer, jiffies + expires))
  70. return;
  71. cp->timer.function = (void (*)(unsigned long)) con3270_update;
  72. cp->timer.data = (unsigned long) cp;
  73. cp->timer.expires = jiffies + expires;
  74. add_timer(&cp->timer);
  75. }
  76. /*
  77. * The status line is the last line of the screen. It shows the string
  78. * "console view" in the lower left corner and "Running"/"More..."/"Holding"
  79. * in the lower right corner of the screen.
  80. */
  81. static void
  82. con3270_update_status(struct con3270 *cp)
  83. {
  84. char *str;
  85. str = (cp->nr_up != 0) ? "History" : "Running";
  86. memcpy(cp->status->string + 24, str, 7);
  87. codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
  88. cp->update_flags |= CON_UPDATE_STATUS;
  89. }
  90. static void
  91. con3270_create_status(struct con3270 *cp)
  92. {
  93. static const unsigned char blueprint[] =
  94. { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
  95. 'c','o','n','s','o','l','e',' ','v','i','e','w',
  96. TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
  97. cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
  98. /* Copy blueprint to status line */
  99. memcpy(cp->status->string, blueprint, sizeof(blueprint));
  100. /* Set TO_RA addresses. */
  101. raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
  102. cp->view.cols * (cp->view.rows - 1));
  103. raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
  104. cp->view.cols * cp->view.rows - 8);
  105. /* Convert strings to ebcdic. */
  106. codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
  107. codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
  108. }
  109. /*
  110. * Set output offsets to 3270 datastream fragment of a console string.
  111. */
  112. static void
  113. con3270_update_string(struct con3270 *cp, struct string *s, int nr)
  114. {
  115. if (s->len >= cp->view.cols - 5)
  116. return;
  117. raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
  118. cp->view.cols * (nr + 1));
  119. }
  120. /*
  121. * Rebuild update list to print all lines.
  122. */
  123. static void
  124. con3270_rebuild_update(struct con3270 *cp)
  125. {
  126. struct string *s, *n;
  127. int nr;
  128. /*
  129. * Throw away update list and create a new one,
  130. * containing all lines that will fit on the screen.
  131. */
  132. list_for_each_entry_safe(s, n, &cp->update, update)
  133. list_del_init(&s->update);
  134. nr = cp->view.rows - 2 + cp->nr_up;
  135. list_for_each_entry_reverse(s, &cp->lines, list) {
  136. if (nr < cp->view.rows - 1)
  137. list_add(&s->update, &cp->update);
  138. if (--nr < 0)
  139. break;
  140. }
  141. cp->line_nr = 0;
  142. cp->update_flags |= CON_UPDATE_LIST;
  143. }
  144. /*
  145. * Alloc string for size bytes. Free strings from history if necessary.
  146. */
  147. static struct string *
  148. con3270_alloc_string(struct con3270 *cp, size_t size)
  149. {
  150. struct string *s, *n;
  151. s = alloc_string(&cp->freemem, size);
  152. if (s)
  153. return s;
  154. list_for_each_entry_safe(s, n, &cp->lines, list) {
  155. list_del(&s->list);
  156. if (!list_empty(&s->update))
  157. list_del(&s->update);
  158. cp->nr_lines--;
  159. if (free_string(&cp->freemem, s) >= size)
  160. break;
  161. }
  162. s = alloc_string(&cp->freemem, size);
  163. BUG_ON(!s);
  164. if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
  165. cp->nr_up = cp->nr_lines - cp->view.rows + 1;
  166. con3270_rebuild_update(cp);
  167. con3270_update_status(cp);
  168. }
  169. return s;
  170. }
  171. /*
  172. * Write completion callback.
  173. */
  174. static void
  175. con3270_write_callback(struct raw3270_request *rq, void *data)
  176. {
  177. raw3270_request_reset(rq);
  178. xchg(&((struct con3270 *) rq->view)->write, rq);
  179. }
  180. /*
  181. * Update console display.
  182. */
  183. static void
  184. con3270_update(struct con3270 *cp)
  185. {
  186. struct raw3270_request *wrq;
  187. char wcc, prolog[6];
  188. unsigned long flags;
  189. unsigned long updated;
  190. struct string *s, *n;
  191. int rc;
  192. if (cp->view.dev)
  193. raw3270_activate_view(&cp->view);
  194. wrq = xchg(&cp->write, 0);
  195. if (!wrq) {
  196. con3270_set_timer(cp, 1);
  197. return;
  198. }
  199. spin_lock_irqsave(&cp->view.lock, flags);
  200. updated = 0;
  201. if (cp->update_flags & CON_UPDATE_ERASE) {
  202. /* Use erase write alternate to initialize display. */
  203. raw3270_request_set_cmd(wrq, TC_EWRITEA);
  204. updated |= CON_UPDATE_ERASE;
  205. } else
  206. raw3270_request_set_cmd(wrq, TC_WRITE);
  207. wcc = TW_NONE;
  208. raw3270_request_add_data(wrq, &wcc, 1);
  209. /*
  210. * Update status line.
  211. */
  212. if (cp->update_flags & CON_UPDATE_STATUS)
  213. if (raw3270_request_add_data(wrq, cp->status->string,
  214. cp->status->len) == 0)
  215. updated |= CON_UPDATE_STATUS;
  216. if (cp->update_flags & CON_UPDATE_LIST) {
  217. prolog[0] = TO_SBA;
  218. prolog[3] = TO_SA;
  219. prolog[4] = TAT_COLOR;
  220. prolog[5] = TAC_TURQ;
  221. raw3270_buffer_address(cp->view.dev, prolog + 1,
  222. cp->view.cols * cp->line_nr);
  223. raw3270_request_add_data(wrq, prolog, 6);
  224. /* Write strings in the update list to the screen. */
  225. list_for_each_entry_safe(s, n, &cp->update, update) {
  226. if (s != cp->cline)
  227. con3270_update_string(cp, s, cp->line_nr);
  228. if (raw3270_request_add_data(wrq, s->string,
  229. s->len) != 0)
  230. break;
  231. list_del_init(&s->update);
  232. if (s != cp->cline)
  233. cp->line_nr++;
  234. }
  235. if (list_empty(&cp->update))
  236. updated |= CON_UPDATE_LIST;
  237. }
  238. wrq->callback = con3270_write_callback;
  239. rc = raw3270_start(&cp->view, wrq);
  240. if (rc == 0) {
  241. cp->update_flags &= ~updated;
  242. if (cp->update_flags)
  243. con3270_set_timer(cp, 1);
  244. } else {
  245. raw3270_request_reset(wrq);
  246. xchg(&cp->write, wrq);
  247. }
  248. spin_unlock_irqrestore(&cp->view.lock, flags);
  249. }
  250. /*
  251. * Read tasklet.
  252. */
  253. static void
  254. con3270_read_tasklet(struct raw3270_request *rrq)
  255. {
  256. static char kreset_data = TW_KR;
  257. struct con3270 *cp;
  258. unsigned long flags;
  259. int nr_up, deactivate;
  260. cp = (struct con3270 *) rrq->view;
  261. spin_lock_irqsave(&cp->view.lock, flags);
  262. nr_up = cp->nr_up;
  263. deactivate = 0;
  264. /* Check aid byte. */
  265. switch (cp->input->string[0]) {
  266. case 0x7d: /* enter: jump to bottom. */
  267. nr_up = 0;
  268. break;
  269. case 0xf3: /* PF3: deactivate the console view. */
  270. deactivate = 1;
  271. break;
  272. case 0x6d: /* clear: start from scratch. */
  273. con3270_rebuild_update(cp);
  274. cp->update_flags = CON_UPDATE_ALL;
  275. con3270_set_timer(cp, 1);
  276. break;
  277. case 0xf7: /* PF7: do a page up in the console log. */
  278. nr_up += cp->view.rows - 2;
  279. if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
  280. nr_up = cp->nr_lines - cp->view.rows + 1;
  281. if (nr_up < 0)
  282. nr_up = 0;
  283. }
  284. break;
  285. case 0xf8: /* PF8: do a page down in the console log. */
  286. nr_up -= cp->view.rows - 2;
  287. if (nr_up < 0)
  288. nr_up = 0;
  289. break;
  290. }
  291. if (nr_up != cp->nr_up) {
  292. cp->nr_up = nr_up;
  293. con3270_rebuild_update(cp);
  294. con3270_update_status(cp);
  295. con3270_set_timer(cp, 1);
  296. }
  297. spin_unlock_irqrestore(&cp->view.lock, flags);
  298. /* Start keyboard reset command. */
  299. raw3270_request_reset(cp->kreset);
  300. raw3270_request_set_cmd(cp->kreset, TC_WRITE);
  301. raw3270_request_add_data(cp->kreset, &kreset_data, 1);
  302. raw3270_start(&cp->view, cp->kreset);
  303. if (deactivate)
  304. raw3270_deactivate_view(&cp->view);
  305. raw3270_request_reset(rrq);
  306. xchg(&cp->read, rrq);
  307. raw3270_put_view(&cp->view);
  308. }
  309. /*
  310. * Read request completion callback.
  311. */
  312. static void
  313. con3270_read_callback(struct raw3270_request *rq, void *data)
  314. {
  315. raw3270_get_view(rq->view);
  316. /* Schedule tasklet to pass input to tty. */
  317. tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
  318. }
  319. /*
  320. * Issue a read request. Called only from interrupt function.
  321. */
  322. static void
  323. con3270_issue_read(struct con3270 *cp)
  324. {
  325. struct raw3270_request *rrq;
  326. int rc;
  327. rrq = xchg(&cp->read, 0);
  328. if (!rrq)
  329. /* Read already scheduled. */
  330. return;
  331. rrq->callback = con3270_read_callback;
  332. rrq->callback_data = cp;
  333. raw3270_request_set_cmd(rrq, TC_READMOD);
  334. raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
  335. /* Issue the read modified request. */
  336. rc = raw3270_start_irq(&cp->view, rrq);
  337. if (rc)
  338. raw3270_request_reset(rrq);
  339. }
  340. /*
  341. * Switch to the console view.
  342. */
  343. static int
  344. con3270_activate(struct raw3270_view *view)
  345. {
  346. unsigned long flags;
  347. struct con3270 *cp;
  348. cp = (struct con3270 *) view;
  349. spin_lock_irqsave(&cp->view.lock, flags);
  350. cp->nr_up = 0;
  351. con3270_rebuild_update(cp);
  352. con3270_update_status(cp);
  353. cp->update_flags = CON_UPDATE_ALL;
  354. con3270_set_timer(cp, 1);
  355. spin_unlock_irqrestore(&cp->view.lock, flags);
  356. return 0;
  357. }
  358. static void
  359. con3270_deactivate(struct raw3270_view *view)
  360. {
  361. unsigned long flags;
  362. struct con3270 *cp;
  363. cp = (struct con3270 *) view;
  364. spin_lock_irqsave(&cp->view.lock, flags);
  365. del_timer(&cp->timer);
  366. spin_unlock_irqrestore(&cp->view.lock, flags);
  367. }
  368. static int
  369. con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
  370. {
  371. /* Handle ATTN. Schedule tasklet to read aid. */
  372. if (irb->scsw.dstat & DEV_STAT_ATTENTION)
  373. con3270_issue_read(cp);
  374. if (rq) {
  375. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
  376. rq->rc = -EIO;
  377. else
  378. /* Normal end. Copy residual count. */
  379. rq->rescnt = irb->scsw.count;
  380. }
  381. return RAW3270_IO_DONE;
  382. }
  383. /* Console view to a 3270 device. */
  384. static struct raw3270_fn con3270_fn = {
  385. .activate = con3270_activate,
  386. .deactivate = con3270_deactivate,
  387. .intv = (void *) con3270_irq
  388. };
  389. static inline void
  390. con3270_cline_add(struct con3270 *cp)
  391. {
  392. if (!list_empty(&cp->cline->list))
  393. /* Already added. */
  394. return;
  395. list_add_tail(&cp->cline->list, &cp->lines);
  396. cp->nr_lines++;
  397. con3270_rebuild_update(cp);
  398. }
  399. static inline void
  400. con3270_cline_insert(struct con3270 *cp, unsigned char c)
  401. {
  402. cp->cline->string[cp->cline->len++] =
  403. cp->view.ascebc[(c < ' ') ? ' ' : c];
  404. if (list_empty(&cp->cline->update)) {
  405. list_add_tail(&cp->cline->update, &cp->update);
  406. cp->update_flags |= CON_UPDATE_LIST;
  407. }
  408. }
  409. static inline void
  410. con3270_cline_end(struct con3270 *cp)
  411. {
  412. struct string *s;
  413. unsigned int size;
  414. /* Copy cline. */
  415. size = (cp->cline->len < cp->view.cols - 5) ?
  416. cp->cline->len + 4 : cp->view.cols;
  417. s = con3270_alloc_string(cp, size);
  418. memcpy(s->string, cp->cline->string, cp->cline->len);
  419. if (s->len < cp->view.cols - 5) {
  420. s->string[s->len - 4] = TO_RA;
  421. s->string[s->len - 1] = 0;
  422. } else {
  423. while (--size > cp->cline->len)
  424. s->string[size] = cp->view.ascebc[' '];
  425. }
  426. /* Replace cline with allocated line s and reset cline. */
  427. list_add(&s->list, &cp->cline->list);
  428. list_del_init(&cp->cline->list);
  429. if (!list_empty(&cp->cline->update)) {
  430. list_add(&s->update, &cp->cline->update);
  431. list_del_init(&cp->cline->update);
  432. }
  433. cp->cline->len = 0;
  434. }
  435. /*
  436. * Write a string to the 3270 console
  437. */
  438. static void
  439. con3270_write(struct console *co, const char *str, unsigned int count)
  440. {
  441. struct con3270 *cp;
  442. unsigned long flags;
  443. unsigned char c;
  444. cp = condev;
  445. spin_lock_irqsave(&cp->view.lock, flags);
  446. while (count-- > 0) {
  447. c = *str++;
  448. if (cp->cline->len == 0)
  449. con3270_cline_add(cp);
  450. if (c != '\n')
  451. con3270_cline_insert(cp, c);
  452. if (c == '\n' || cp->cline->len >= cp->view.cols)
  453. con3270_cline_end(cp);
  454. }
  455. /* Setup timer to output current console buffer after 1/10 second */
  456. if (cp->view.dev && !timer_pending(&cp->timer))
  457. con3270_set_timer(cp, HZ/10);
  458. spin_unlock_irqrestore(&cp->view.lock,flags);
  459. }
  460. extern struct tty_driver *tty3270_driver;
  461. static struct tty_driver *
  462. con3270_device(struct console *c, int *index)
  463. {
  464. *index = c->index;
  465. return tty3270_driver;
  466. }
  467. /*
  468. * Wait for end of write request.
  469. */
  470. static void
  471. con3270_wait_write(struct con3270 *cp)
  472. {
  473. while (!cp->write) {
  474. raw3270_wait_cons_dev(cp->view.dev);
  475. barrier();
  476. }
  477. }
  478. /*
  479. * panic() calls console_unblank before the system enters a
  480. * disabled, endless loop.
  481. */
  482. static void
  483. con3270_unblank(void)
  484. {
  485. struct con3270 *cp;
  486. unsigned long flags;
  487. cp = condev;
  488. if (!cp->view.dev)
  489. return;
  490. spin_lock_irqsave(&cp->view.lock, flags);
  491. con3270_wait_write(cp);
  492. cp->nr_up = 0;
  493. con3270_rebuild_update(cp);
  494. con3270_update_status(cp);
  495. while (cp->update_flags != 0) {
  496. spin_unlock_irqrestore(&cp->view.lock, flags);
  497. con3270_update(cp);
  498. spin_lock_irqsave(&cp->view.lock, flags);
  499. con3270_wait_write(cp);
  500. }
  501. spin_unlock_irqrestore(&cp->view.lock, flags);
  502. }
  503. static int __init
  504. con3270_consetup(struct console *co, char *options)
  505. {
  506. return 0;
  507. }
  508. /*
  509. * The console structure for the 3270 console
  510. */
  511. static struct console con3270 = {
  512. .name = "tty3270",
  513. .write = con3270_write,
  514. .device = con3270_device,
  515. .unblank = con3270_unblank,
  516. .setup = con3270_consetup,
  517. .flags = CON_PRINTBUFFER,
  518. };
  519. /*
  520. * 3270 console initialization code called from console_init().
  521. * NOTE: This is called before kmalloc is available.
  522. */
  523. static int __init
  524. con3270_init(void)
  525. {
  526. struct ccw_device *cdev;
  527. struct raw3270 *rp;
  528. void *cbuf;
  529. int i;
  530. /* Check if 3270 is to be the console */
  531. if (!CONSOLE_IS_3270)
  532. return -ENODEV;
  533. /* Set the console mode for VM */
  534. if (MACHINE_IS_VM) {
  535. cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
  536. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  537. }
  538. cdev = ccw_device_probe_console();
  539. if (IS_ERR(cdev))
  540. return -ENODEV;
  541. rp = raw3270_setup_console(cdev);
  542. if (IS_ERR(rp))
  543. return PTR_ERR(rp);
  544. condev = (struct con3270 *) alloc_bootmem_low(sizeof(struct con3270));
  545. memset(condev, 0, sizeof(struct con3270));
  546. condev->view.dev = rp;
  547. condev->read = raw3270_request_alloc_bootmem(0);
  548. condev->read->callback = con3270_read_callback;
  549. condev->read->callback_data = condev;
  550. condev->write =
  551. raw3270_request_alloc_bootmem(CON3270_OUTPUT_BUFFER_SIZE);
  552. condev->kreset = raw3270_request_alloc_bootmem(1);
  553. INIT_LIST_HEAD(&condev->lines);
  554. INIT_LIST_HEAD(&condev->update);
  555. init_timer(&condev->timer);
  556. tasklet_init(&condev->readlet,
  557. (void (*)(unsigned long)) con3270_read_tasklet,
  558. (unsigned long) condev->read);
  559. raw3270_add_view(&condev->view, &con3270_fn, 1);
  560. INIT_LIST_HEAD(&condev->freemem);
  561. for (i = 0; i < CON3270_STRING_PAGES; i++) {
  562. cbuf = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
  563. add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
  564. }
  565. condev->cline = alloc_string(&condev->freemem, condev->view.cols);
  566. condev->cline->len = 0;
  567. con3270_create_status(condev);
  568. condev->input = alloc_string(&condev->freemem, 80);
  569. register_console(&con3270);
  570. return 0;
  571. }
  572. console_initcall(con3270_init);