con3270.c 16 KB

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