con3270.c 16 KB

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