con3270.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 < 4) {
  110. /* This indicates a bug, but printing a warning would
  111. * cause a deadlock. */
  112. return;
  113. }
  114. if (s->string[s->len - 4] != TO_RA)
  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 (!auto_update && !raw3270_view_active(&cp->view))
  192. return;
  193. if (cp->view.dev)
  194. raw3270_activate_view(&cp->view);
  195. wrq = xchg(&cp->write, 0);
  196. if (!wrq) {
  197. con3270_set_timer(cp, 1);
  198. return;
  199. }
  200. spin_lock_irqsave(&cp->view.lock, flags);
  201. updated = 0;
  202. if (cp->update_flags & CON_UPDATE_ALL) {
  203. con3270_rebuild_update(cp);
  204. con3270_update_status(cp);
  205. cp->update_flags = CON_UPDATE_ERASE | CON_UPDATE_LIST |
  206. CON_UPDATE_STATUS;
  207. }
  208. if (cp->update_flags & CON_UPDATE_ERASE) {
  209. /* Use erase write alternate to initialize display. */
  210. raw3270_request_set_cmd(wrq, TC_EWRITEA);
  211. updated |= CON_UPDATE_ERASE;
  212. } else
  213. raw3270_request_set_cmd(wrq, TC_WRITE);
  214. wcc = TW_NONE;
  215. raw3270_request_add_data(wrq, &wcc, 1);
  216. /*
  217. * Update status line.
  218. */
  219. if (cp->update_flags & CON_UPDATE_STATUS)
  220. if (raw3270_request_add_data(wrq, cp->status->string,
  221. cp->status->len) == 0)
  222. updated |= CON_UPDATE_STATUS;
  223. if (cp->update_flags & CON_UPDATE_LIST) {
  224. prolog[0] = TO_SBA;
  225. prolog[3] = TO_SA;
  226. prolog[4] = TAT_COLOR;
  227. prolog[5] = TAC_TURQ;
  228. raw3270_buffer_address(cp->view.dev, prolog + 1,
  229. cp->view.cols * cp->line_nr);
  230. raw3270_request_add_data(wrq, prolog, 6);
  231. /* Write strings in the update list to the screen. */
  232. list_for_each_entry_safe(s, n, &cp->update, update) {
  233. if (s != cp->cline)
  234. con3270_update_string(cp, s, cp->line_nr);
  235. if (raw3270_request_add_data(wrq, s->string,
  236. s->len) != 0)
  237. break;
  238. list_del_init(&s->update);
  239. if (s != cp->cline)
  240. cp->line_nr++;
  241. }
  242. if (list_empty(&cp->update))
  243. updated |= CON_UPDATE_LIST;
  244. }
  245. wrq->callback = con3270_write_callback;
  246. rc = raw3270_start(&cp->view, wrq);
  247. if (rc == 0) {
  248. cp->update_flags &= ~updated;
  249. if (cp->update_flags)
  250. con3270_set_timer(cp, 1);
  251. } else {
  252. raw3270_request_reset(wrq);
  253. xchg(&cp->write, wrq);
  254. }
  255. spin_unlock_irqrestore(&cp->view.lock, flags);
  256. }
  257. /*
  258. * Read tasklet.
  259. */
  260. static void
  261. con3270_read_tasklet(struct raw3270_request *rrq)
  262. {
  263. static char kreset_data = TW_KR;
  264. struct con3270 *cp;
  265. unsigned long flags;
  266. int nr_up, deactivate;
  267. cp = (struct con3270 *) rrq->view;
  268. spin_lock_irqsave(&cp->view.lock, flags);
  269. nr_up = cp->nr_up;
  270. deactivate = 0;
  271. /* Check aid byte. */
  272. switch (cp->input->string[0]) {
  273. case 0x7d: /* enter: jump to bottom. */
  274. nr_up = 0;
  275. break;
  276. case 0xf3: /* PF3: deactivate the console view. */
  277. deactivate = 1;
  278. break;
  279. case 0x6d: /* clear: start from scratch. */
  280. cp->update_flags = CON_UPDATE_ALL;
  281. con3270_set_timer(cp, 1);
  282. break;
  283. case 0xf7: /* PF7: do a page up in the console log. */
  284. nr_up += cp->view.rows - 2;
  285. if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
  286. nr_up = cp->nr_lines - cp->view.rows + 1;
  287. if (nr_up < 0)
  288. nr_up = 0;
  289. }
  290. break;
  291. case 0xf8: /* PF8: do a page down in the console log. */
  292. nr_up -= cp->view.rows - 2;
  293. if (nr_up < 0)
  294. nr_up = 0;
  295. break;
  296. }
  297. if (nr_up != cp->nr_up) {
  298. cp->nr_up = nr_up;
  299. con3270_rebuild_update(cp);
  300. con3270_update_status(cp);
  301. con3270_set_timer(cp, 1);
  302. }
  303. spin_unlock_irqrestore(&cp->view.lock, flags);
  304. /* Start keyboard reset command. */
  305. raw3270_request_reset(cp->kreset);
  306. raw3270_request_set_cmd(cp->kreset, TC_WRITE);
  307. raw3270_request_add_data(cp->kreset, &kreset_data, 1);
  308. raw3270_start(&cp->view, cp->kreset);
  309. if (deactivate)
  310. raw3270_deactivate_view(&cp->view);
  311. raw3270_request_reset(rrq);
  312. xchg(&cp->read, rrq);
  313. raw3270_put_view(&cp->view);
  314. }
  315. /*
  316. * Read request completion callback.
  317. */
  318. static void
  319. con3270_read_callback(struct raw3270_request *rq, void *data)
  320. {
  321. raw3270_get_view(rq->view);
  322. /* Schedule tasklet to pass input to tty. */
  323. tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
  324. }
  325. /*
  326. * Issue a read request. Called only from interrupt function.
  327. */
  328. static void
  329. con3270_issue_read(struct con3270 *cp)
  330. {
  331. struct raw3270_request *rrq;
  332. int rc;
  333. rrq = xchg(&cp->read, 0);
  334. if (!rrq)
  335. /* Read already scheduled. */
  336. return;
  337. rrq->callback = con3270_read_callback;
  338. rrq->callback_data = cp;
  339. raw3270_request_set_cmd(rrq, TC_READMOD);
  340. raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
  341. /* Issue the read modified request. */
  342. rc = raw3270_start_irq(&cp->view, rrq);
  343. if (rc)
  344. raw3270_request_reset(rrq);
  345. }
  346. /*
  347. * Switch to the console view.
  348. */
  349. static int
  350. con3270_activate(struct raw3270_view *view)
  351. {
  352. struct con3270 *cp;
  353. cp = (struct con3270 *) view;
  354. cp->update_flags = CON_UPDATE_ALL;
  355. con3270_set_timer(cp, 1);
  356. return 0;
  357. }
  358. static void
  359. con3270_deactivate(struct raw3270_view *view)
  360. {
  361. struct con3270 *cp;
  362. cp = (struct con3270 *) view;
  363. del_timer(&cp->timer);
  364. }
  365. static void
  366. con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
  367. {
  368. /* Handle ATTN. Schedule tasklet to read aid. */
  369. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
  370. con3270_issue_read(cp);
  371. if (rq) {
  372. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
  373. rq->rc = -EIO;
  374. else
  375. /* Normal end. Copy residual count. */
  376. rq->rescnt = irb->scsw.cmd.count;
  377. } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
  378. /* Interrupt without an outstanding request -> update all */
  379. cp->update_flags = CON_UPDATE_ALL;
  380. con3270_set_timer(cp, 1);
  381. }
  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 (cp->cline->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. cp->nr_up = 0;
  457. if (cp->view.dev && !timer_pending(&cp->timer))
  458. con3270_set_timer(cp, HZ/10);
  459. spin_unlock_irqrestore(&cp->view.lock,flags);
  460. }
  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 con3270_flush through a panic_notifier
  480. * before the system enters a disabled, endless loop.
  481. */
  482. static void
  483. con3270_flush(void)
  484. {
  485. struct con3270 *cp;
  486. unsigned long flags;
  487. cp = condev;
  488. if (!cp->view.dev)
  489. return;
  490. raw3270_pm_unfreeze(&cp->view);
  491. raw3270_activate_view(&cp->view);
  492. spin_lock_irqsave(&cp->view.lock, flags);
  493. con3270_wait_write(cp);
  494. cp->nr_up = 0;
  495. con3270_rebuild_update(cp);
  496. con3270_update_status(cp);
  497. while (cp->update_flags != 0) {
  498. spin_unlock_irqrestore(&cp->view.lock, flags);
  499. con3270_update(cp);
  500. spin_lock_irqsave(&cp->view.lock, flags);
  501. con3270_wait_write(cp);
  502. }
  503. spin_unlock_irqrestore(&cp->view.lock, flags);
  504. }
  505. static int con3270_notify(struct notifier_block *self,
  506. unsigned long event, void *data)
  507. {
  508. con3270_flush();
  509. return NOTIFY_OK;
  510. }
  511. static struct notifier_block on_panic_nb = {
  512. .notifier_call = con3270_notify,
  513. .priority = 0,
  514. };
  515. static struct notifier_block on_reboot_nb = {
  516. .notifier_call = con3270_notify,
  517. .priority = 0,
  518. };
  519. /*
  520. * The console structure for the 3270 console
  521. */
  522. static struct console con3270 = {
  523. .name = "tty3270",
  524. .write = con3270_write,
  525. .device = con3270_device,
  526. .flags = CON_PRINTBUFFER,
  527. };
  528. /*
  529. * 3270 console initialization code called from console_init().
  530. */
  531. static int __init
  532. con3270_init(void)
  533. {
  534. struct raw3270 *rp;
  535. void *cbuf;
  536. int i;
  537. /* Check if 3270 is to be the console */
  538. if (!CONSOLE_IS_3270)
  539. return -ENODEV;
  540. /* Set the console mode for VM */
  541. if (MACHINE_IS_VM) {
  542. cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
  543. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  544. }
  545. rp = raw3270_setup_console();
  546. if (IS_ERR(rp))
  547. return PTR_ERR(rp);
  548. condev = kzalloc(sizeof(struct con3270), GFP_KERNEL | GFP_DMA);
  549. if (!condev)
  550. return -ENOMEM;
  551. condev->view.dev = rp;
  552. condev->read = raw3270_request_alloc(0);
  553. condev->read->callback = con3270_read_callback;
  554. condev->read->callback_data = condev;
  555. condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE);
  556. condev->kreset = raw3270_request_alloc(1);
  557. INIT_LIST_HEAD(&condev->lines);
  558. INIT_LIST_HEAD(&condev->update);
  559. setup_timer(&condev->timer, (void (*)(unsigned long)) con3270_update,
  560. (unsigned long) condev);
  561. tasklet_init(&condev->readlet,
  562. (void (*)(unsigned long)) con3270_read_tasklet,
  563. (unsigned long) condev->read);
  564. raw3270_add_view(&condev->view, &con3270_fn, 1);
  565. INIT_LIST_HEAD(&condev->freemem);
  566. for (i = 0; i < CON3270_STRING_PAGES; i++) {
  567. cbuf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  568. add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
  569. }
  570. condev->cline = alloc_string(&condev->freemem, condev->view.cols);
  571. condev->cline->len = 0;
  572. con3270_create_status(condev);
  573. condev->input = alloc_string(&condev->freemem, 80);
  574. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  575. register_reboot_notifier(&on_reboot_nb);
  576. register_console(&con3270);
  577. return 0;
  578. }
  579. console_initcall(con3270_init);