con3270.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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/config.h>
  11. #include <linux/bootmem.h>
  12. #include <linux/console.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/list.h>
  16. #include <linux/types.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. wrq = xchg(&cp->write, 0);
  193. if (!wrq) {
  194. con3270_set_timer(cp, 1);
  195. return;
  196. }
  197. spin_lock_irqsave(&cp->view.lock, flags);
  198. updated = 0;
  199. if (cp->update_flags & CON_UPDATE_ERASE) {
  200. /* Use erase write alternate to initialize display. */
  201. raw3270_request_set_cmd(wrq, TC_EWRITEA);
  202. updated |= CON_UPDATE_ERASE;
  203. } else
  204. raw3270_request_set_cmd(wrq, TC_WRITE);
  205. wcc = TW_NONE;
  206. raw3270_request_add_data(wrq, &wcc, 1);
  207. /*
  208. * Update status line.
  209. */
  210. if (cp->update_flags & CON_UPDATE_STATUS)
  211. if (raw3270_request_add_data(wrq, cp->status->string,
  212. cp->status->len) == 0)
  213. updated |= CON_UPDATE_STATUS;
  214. if (cp->update_flags & CON_UPDATE_LIST) {
  215. prolog[0] = TO_SBA;
  216. prolog[3] = TO_SA;
  217. prolog[4] = TAT_COLOR;
  218. prolog[5] = TAC_TURQ;
  219. raw3270_buffer_address(cp->view.dev, prolog + 1,
  220. cp->view.cols * cp->line_nr);
  221. raw3270_request_add_data(wrq, prolog, 6);
  222. /* Write strings in the update list to the screen. */
  223. list_for_each_entry_safe(s, n, &cp->update, update) {
  224. if (s != cp->cline)
  225. con3270_update_string(cp, s, cp->line_nr);
  226. if (raw3270_request_add_data(wrq, s->string,
  227. s->len) != 0)
  228. break;
  229. list_del_init(&s->update);
  230. if (s != cp->cline)
  231. cp->line_nr++;
  232. }
  233. if (list_empty(&cp->update))
  234. updated |= CON_UPDATE_LIST;
  235. }
  236. wrq->callback = con3270_write_callback;
  237. rc = raw3270_start(&cp->view, wrq);
  238. if (rc == 0) {
  239. cp->update_flags &= ~updated;
  240. if (cp->update_flags)
  241. con3270_set_timer(cp, 1);
  242. } else {
  243. raw3270_request_reset(wrq);
  244. xchg(&cp->write, wrq);
  245. }
  246. spin_unlock_irqrestore(&cp->view.lock, flags);
  247. }
  248. /*
  249. * Read tasklet.
  250. */
  251. static void
  252. con3270_read_tasklet(struct raw3270_request *rrq)
  253. {
  254. static char kreset_data = TW_KR;
  255. struct con3270 *cp;
  256. unsigned long flags;
  257. int nr_up, deactivate;
  258. cp = (struct con3270 *) rrq->view;
  259. spin_lock_irqsave(&cp->view.lock, flags);
  260. nr_up = cp->nr_up;
  261. deactivate = 0;
  262. /* Check aid byte. */
  263. switch (cp->input->string[0]) {
  264. case 0x7d: /* enter: jump to bottom. */
  265. nr_up = 0;
  266. break;
  267. case 0xf3: /* PF3: deactivate the console view. */
  268. deactivate = 1;
  269. break;
  270. case 0x6d: /* clear: start from scratch. */
  271. con3270_rebuild_update(cp);
  272. cp->update_flags = CON_UPDATE_ALL;
  273. con3270_set_timer(cp, 1);
  274. break;
  275. case 0xf7: /* PF7: do a page up in the console log. */
  276. nr_up += cp->view.rows - 2;
  277. if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
  278. nr_up = cp->nr_lines - cp->view.rows + 1;
  279. if (nr_up < 0)
  280. nr_up = 0;
  281. }
  282. break;
  283. case 0xf8: /* PF8: do a page down in the console log. */
  284. nr_up -= cp->view.rows - 2;
  285. if (nr_up < 0)
  286. nr_up = 0;
  287. break;
  288. }
  289. if (nr_up != cp->nr_up) {
  290. cp->nr_up = nr_up;
  291. con3270_rebuild_update(cp);
  292. con3270_update_status(cp);
  293. con3270_set_timer(cp, 1);
  294. }
  295. spin_unlock_irqrestore(&cp->view.lock, flags);
  296. /* Start keyboard reset command. */
  297. raw3270_request_reset(cp->kreset);
  298. raw3270_request_set_cmd(cp->kreset, TC_WRITE);
  299. raw3270_request_add_data(cp->kreset, &kreset_data, 1);
  300. raw3270_start(&cp->view, cp->kreset);
  301. if (deactivate)
  302. raw3270_deactivate_view(&cp->view);
  303. raw3270_request_reset(rrq);
  304. xchg(&cp->read, rrq);
  305. raw3270_put_view(&cp->view);
  306. }
  307. /*
  308. * Read request completion callback.
  309. */
  310. static void
  311. con3270_read_callback(struct raw3270_request *rq, void *data)
  312. {
  313. raw3270_get_view(rq->view);
  314. /* Schedule tasklet to pass input to tty. */
  315. tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
  316. }
  317. /*
  318. * Issue a read request. Called only from interrupt function.
  319. */
  320. static void
  321. con3270_issue_read(struct con3270 *cp)
  322. {
  323. struct raw3270_request *rrq;
  324. int rc;
  325. rrq = xchg(&cp->read, 0);
  326. if (!rrq)
  327. /* Read already scheduled. */
  328. return;
  329. rrq->callback = con3270_read_callback;
  330. rrq->callback_data = cp;
  331. raw3270_request_set_cmd(rrq, TC_READMOD);
  332. raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
  333. /* Issue the read modified request. */
  334. rc = raw3270_start_irq(&cp->view, rrq);
  335. if (rc)
  336. raw3270_request_reset(rrq);
  337. }
  338. /*
  339. * Switch to the console view.
  340. */
  341. static int
  342. con3270_activate(struct raw3270_view *view)
  343. {
  344. unsigned long flags;
  345. struct con3270 *cp;
  346. cp = (struct con3270 *) view;
  347. spin_lock_irqsave(&cp->view.lock, flags);
  348. cp->nr_up = 0;
  349. con3270_rebuild_update(cp);
  350. con3270_update_status(cp);
  351. cp->update_flags = CON_UPDATE_ALL;
  352. con3270_set_timer(cp, 1);
  353. spin_unlock_irqrestore(&cp->view.lock, flags);
  354. return 0;
  355. }
  356. static void
  357. con3270_deactivate(struct raw3270_view *view)
  358. {
  359. unsigned long flags;
  360. struct con3270 *cp;
  361. cp = (struct con3270 *) view;
  362. spin_lock_irqsave(&cp->view.lock, flags);
  363. del_timer(&cp->timer);
  364. spin_unlock_irqrestore(&cp->view.lock, flags);
  365. }
  366. static int
  367. con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
  368. {
  369. /* Handle ATTN. Schedule tasklet to read aid. */
  370. if (irb->scsw.dstat & DEV_STAT_ATTENTION)
  371. con3270_issue_read(cp);
  372. if (rq) {
  373. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
  374. rq->rc = -EIO;
  375. else
  376. /* Normal end. Copy residual count. */
  377. rq->rescnt = irb->scsw.count;
  378. }
  379. return RAW3270_IO_DONE;
  380. }
  381. /* Console view to a 3270 device. */
  382. static struct raw3270_fn con3270_fn = {
  383. .activate = con3270_activate,
  384. .deactivate = con3270_deactivate,
  385. .intv = (void *) con3270_irq
  386. };
  387. static inline void
  388. con3270_cline_add(struct con3270 *cp)
  389. {
  390. if (!list_empty(&cp->cline->list))
  391. /* Already added. */
  392. return;
  393. list_add_tail(&cp->cline->list, &cp->lines);
  394. cp->nr_lines++;
  395. con3270_rebuild_update(cp);
  396. }
  397. static inline void
  398. con3270_cline_insert(struct con3270 *cp, unsigned char c)
  399. {
  400. cp->cline->string[cp->cline->len++] =
  401. cp->view.ascebc[(c < ' ') ? ' ' : c];
  402. if (list_empty(&cp->cline->update)) {
  403. list_add_tail(&cp->cline->update, &cp->update);
  404. cp->update_flags |= CON_UPDATE_LIST;
  405. }
  406. }
  407. static inline void
  408. con3270_cline_end(struct con3270 *cp)
  409. {
  410. struct string *s;
  411. unsigned int size;
  412. /* Copy cline. */
  413. size = (cp->cline->len < cp->view.cols - 5) ?
  414. cp->cline->len + 4 : cp->view.cols;
  415. s = con3270_alloc_string(cp, size);
  416. memcpy(s->string, cp->cline->string, cp->cline->len);
  417. if (s->len < cp->view.cols - 5) {
  418. s->string[s->len - 4] = TO_RA;
  419. s->string[s->len - 1] = 0;
  420. } else {
  421. while (--size > cp->cline->len)
  422. s->string[size] = cp->view.ascebc[' '];
  423. }
  424. /* Replace cline with allocated line s and reset cline. */
  425. list_add(&s->list, &cp->cline->list);
  426. list_del_init(&cp->cline->list);
  427. if (!list_empty(&cp->cline->update)) {
  428. list_add(&s->update, &cp->cline->update);
  429. list_del_init(&cp->cline->update);
  430. }
  431. cp->cline->len = 0;
  432. }
  433. /*
  434. * Write a string to the 3270 console
  435. */
  436. static void
  437. con3270_write(struct console *co, const char *str, unsigned int count)
  438. {
  439. struct con3270 *cp;
  440. unsigned long flags;
  441. unsigned char c;
  442. cp = condev;
  443. if (cp->view.dev)
  444. raw3270_activate_view(&cp->view);
  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 (!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, 0);
  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);