con3270.c 16 KB

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