con3270.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
  373. /* Interrupt without an outstanding request -> update all */
  374. cp->update_flags = CON_UPDATE_ALL;
  375. con3270_set_timer(cp, 1);
  376. }
  377. return RAW3270_IO_DONE;
  378. }
  379. /* Console view to a 3270 device. */
  380. static struct raw3270_fn con3270_fn = {
  381. .activate = con3270_activate,
  382. .deactivate = con3270_deactivate,
  383. .intv = (void *) con3270_irq
  384. };
  385. static inline void
  386. con3270_cline_add(struct con3270 *cp)
  387. {
  388. if (!list_empty(&cp->cline->list))
  389. /* Already added. */
  390. return;
  391. list_add_tail(&cp->cline->list, &cp->lines);
  392. cp->nr_lines++;
  393. con3270_rebuild_update(cp);
  394. }
  395. static inline void
  396. con3270_cline_insert(struct con3270 *cp, unsigned char c)
  397. {
  398. cp->cline->string[cp->cline->len++] =
  399. cp->view.ascebc[(c < ' ') ? ' ' : c];
  400. if (list_empty(&cp->cline->update)) {
  401. list_add_tail(&cp->cline->update, &cp->update);
  402. cp->update_flags |= CON_UPDATE_LIST;
  403. }
  404. }
  405. static inline void
  406. con3270_cline_end(struct con3270 *cp)
  407. {
  408. struct string *s;
  409. unsigned int size;
  410. /* Copy cline. */
  411. size = (cp->cline->len < cp->view.cols - 5) ?
  412. cp->cline->len + 4 : cp->view.cols;
  413. s = con3270_alloc_string(cp, size);
  414. memcpy(s->string, cp->cline->string, cp->cline->len);
  415. if (s->len < cp->view.cols - 5) {
  416. s->string[s->len - 4] = TO_RA;
  417. s->string[s->len - 1] = 0;
  418. } else {
  419. while (--size > cp->cline->len)
  420. s->string[size] = cp->view.ascebc[' '];
  421. }
  422. /* Replace cline with allocated line s and reset cline. */
  423. list_add(&s->list, &cp->cline->list);
  424. list_del_init(&cp->cline->list);
  425. if (!list_empty(&cp->cline->update)) {
  426. list_add(&s->update, &cp->cline->update);
  427. list_del_init(&cp->cline->update);
  428. }
  429. cp->cline->len = 0;
  430. }
  431. /*
  432. * Write a string to the 3270 console
  433. */
  434. static void
  435. con3270_write(struct console *co, const char *str, unsigned int count)
  436. {
  437. struct con3270 *cp;
  438. unsigned long flags;
  439. unsigned char c;
  440. cp = condev;
  441. spin_lock_irqsave(&cp->view.lock, flags);
  442. while (count-- > 0) {
  443. c = *str++;
  444. if (cp->cline->len == 0)
  445. con3270_cline_add(cp);
  446. if (c != '\n')
  447. con3270_cline_insert(cp, c);
  448. if (c == '\n' || cp->cline->len >= cp->view.cols)
  449. con3270_cline_end(cp);
  450. }
  451. /* Setup timer to output current console buffer after 1/10 second */
  452. cp->nr_up = 0;
  453. if (cp->view.dev && !timer_pending(&cp->timer))
  454. con3270_set_timer(cp, HZ/10);
  455. spin_unlock_irqrestore(&cp->view.lock,flags);
  456. }
  457. static struct tty_driver *
  458. con3270_device(struct console *c, int *index)
  459. {
  460. *index = c->index;
  461. return tty3270_driver;
  462. }
  463. /*
  464. * Wait for end of write request.
  465. */
  466. static void
  467. con3270_wait_write(struct con3270 *cp)
  468. {
  469. while (!cp->write) {
  470. raw3270_wait_cons_dev(cp->view.dev);
  471. barrier();
  472. }
  473. }
  474. /*
  475. * panic() calls con3270_flush through a panic_notifier
  476. * before the system enters a disabled, endless loop.
  477. */
  478. static void
  479. con3270_flush(void)
  480. {
  481. struct con3270 *cp;
  482. unsigned long flags;
  483. cp = condev;
  484. if (!cp->view.dev)
  485. return;
  486. raw3270_pm_unfreeze(&cp->view);
  487. raw3270_activate_view(&cp->view);
  488. spin_lock_irqsave(&cp->view.lock, flags);
  489. con3270_wait_write(cp);
  490. cp->nr_up = 0;
  491. con3270_rebuild_update(cp);
  492. con3270_update_status(cp);
  493. while (cp->update_flags != 0) {
  494. spin_unlock_irqrestore(&cp->view.lock, flags);
  495. con3270_update(cp);
  496. spin_lock_irqsave(&cp->view.lock, flags);
  497. con3270_wait_write(cp);
  498. }
  499. spin_unlock_irqrestore(&cp->view.lock, flags);
  500. }
  501. static int con3270_notify(struct notifier_block *self,
  502. unsigned long event, void *data)
  503. {
  504. con3270_flush();
  505. return NOTIFY_OK;
  506. }
  507. static struct notifier_block on_panic_nb = {
  508. .notifier_call = con3270_notify,
  509. .priority = 0,
  510. };
  511. static struct notifier_block on_reboot_nb = {
  512. .notifier_call = con3270_notify,
  513. .priority = 0,
  514. };
  515. /*
  516. * The console structure for the 3270 console
  517. */
  518. static struct console con3270 = {
  519. .name = "tty3270",
  520. .write = con3270_write,
  521. .device = con3270_device,
  522. .flags = CON_PRINTBUFFER,
  523. };
  524. /*
  525. * 3270 console initialization code called from console_init().
  526. */
  527. static int __init
  528. con3270_init(void)
  529. {
  530. struct raw3270 *rp;
  531. void *cbuf;
  532. int i;
  533. /* Check if 3270 is to be the console */
  534. if (!CONSOLE_IS_3270)
  535. return -ENODEV;
  536. /* Set the console mode for VM */
  537. if (MACHINE_IS_VM) {
  538. cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
  539. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  540. }
  541. rp = raw3270_setup_console();
  542. if (IS_ERR(rp))
  543. return PTR_ERR(rp);
  544. condev = kzalloc(sizeof(struct con3270), GFP_KERNEL | GFP_DMA);
  545. condev->view.dev = rp;
  546. condev->read = raw3270_request_alloc(0);
  547. condev->read->callback = con3270_read_callback;
  548. condev->read->callback_data = condev;
  549. condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE);
  550. condev->kreset = raw3270_request_alloc(1);
  551. INIT_LIST_HEAD(&condev->lines);
  552. INIT_LIST_HEAD(&condev->update);
  553. setup_timer(&condev->timer, (void (*)(unsigned long)) con3270_update,
  554. (unsigned long) condev);
  555. tasklet_init(&condev->readlet,
  556. (void (*)(unsigned long)) con3270_read_tasklet,
  557. (unsigned long) condev->read);
  558. raw3270_add_view(&condev->view, &con3270_fn, 1);
  559. INIT_LIST_HEAD(&condev->freemem);
  560. for (i = 0; i < CON3270_STRING_PAGES; i++) {
  561. cbuf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  562. add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
  563. }
  564. condev->cline = alloc_string(&condev->freemem, condev->view.cols);
  565. condev->cline->len = 0;
  566. con3270_create_status(condev);
  567. condev->input = alloc_string(&condev->freemem, 80);
  568. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  569. register_reboot_notifier(&on_reboot_nb);
  570. register_console(&con3270);
  571. return 0;
  572. }
  573. console_initcall(con3270_init);