con3270.c 16 KB

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