con3270.c 16 KB

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