con3270.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 7
  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. if (timer_pending(&cp->timer))
  66. del_timer(&cp->timer);
  67. return;
  68. }
  69. if (timer_pending(&cp->timer) &&
  70. mod_timer(&cp->timer, jiffies + expires))
  71. return;
  72. cp->timer.function = (void (*)(unsigned long)) con3270_update;
  73. cp->timer.data = (unsigned long) cp;
  74. cp->timer.expires = jiffies + expires;
  75. add_timer(&cp->timer);
  76. }
  77. /*
  78. * The status line is the last line of the screen. It shows the string
  79. * "console view" in the lower left corner and "Running"/"More..."/"Holding"
  80. * in the lower right corner of the screen.
  81. */
  82. static void
  83. con3270_update_status(struct con3270 *cp)
  84. {
  85. char *str;
  86. str = (cp->nr_up != 0) ? "History" : "Running";
  87. memcpy(cp->status->string + 24, str, 7);
  88. codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
  89. cp->update_flags |= CON_UPDATE_STATUS;
  90. }
  91. static void
  92. con3270_create_status(struct con3270 *cp)
  93. {
  94. static const unsigned char blueprint[] =
  95. { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
  96. 'c','o','n','s','o','l','e',' ','v','i','e','w',
  97. TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
  98. cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
  99. /* Copy blueprint to status line */
  100. memcpy(cp->status->string, blueprint, sizeof(blueprint));
  101. /* Set TO_RA addresses. */
  102. raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
  103. cp->view.cols * (cp->view.rows - 1));
  104. raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
  105. cp->view.cols * cp->view.rows - 8);
  106. /* Convert strings to ebcdic. */
  107. codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
  108. codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
  109. }
  110. /*
  111. * Set output offsets to 3270 datastream fragment of a console string.
  112. */
  113. static void
  114. con3270_update_string(struct con3270 *cp, struct string *s, int nr)
  115. {
  116. if (s->len >= cp->view.cols - 5)
  117. return;
  118. raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
  119. cp->view.cols * (nr + 1));
  120. }
  121. /*
  122. * Rebuild update list to print all lines.
  123. */
  124. static void
  125. con3270_rebuild_update(struct con3270 *cp)
  126. {
  127. struct string *s, *n;
  128. int nr;
  129. /*
  130. * Throw away update list and create a new one,
  131. * containing all lines that will fit on the screen.
  132. */
  133. list_for_each_entry_safe(s, n, &cp->update, update)
  134. list_del_init(&s->update);
  135. nr = cp->view.rows - 2 + cp->nr_up;
  136. list_for_each_entry_reverse(s, &cp->lines, list) {
  137. if (nr < cp->view.rows - 1)
  138. list_add(&s->update, &cp->update);
  139. if (--nr < 0)
  140. break;
  141. }
  142. cp->line_nr = 0;
  143. cp->update_flags |= CON_UPDATE_LIST;
  144. }
  145. /*
  146. * Alloc string for size bytes. Free strings from history if necessary.
  147. */
  148. static struct string *
  149. con3270_alloc_string(struct con3270 *cp, size_t size)
  150. {
  151. struct string *s, *n;
  152. s = alloc_string(&cp->freemem, size);
  153. if (s)
  154. return s;
  155. list_for_each_entry_safe(s, n, &cp->lines, list) {
  156. list_del(&s->list);
  157. if (!list_empty(&s->update))
  158. list_del(&s->update);
  159. cp->nr_lines--;
  160. if (free_string(&cp->freemem, s) >= size)
  161. break;
  162. }
  163. s = alloc_string(&cp->freemem, size);
  164. BUG_ON(!s);
  165. if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
  166. cp->nr_up = cp->nr_lines - cp->view.rows + 1;
  167. con3270_rebuild_update(cp);
  168. con3270_update_status(cp);
  169. }
  170. return s;
  171. }
  172. /*
  173. * Write completion callback.
  174. */
  175. static void
  176. con3270_write_callback(struct raw3270_request *rq, void *data)
  177. {
  178. raw3270_request_reset(rq);
  179. xchg(&((struct con3270 *) rq->view)->write, rq);
  180. }
  181. /*
  182. * Update console display.
  183. */
  184. static void
  185. con3270_update(struct con3270 *cp)
  186. {
  187. struct raw3270_request *wrq;
  188. char wcc, prolog[6];
  189. unsigned long flags;
  190. unsigned long updated;
  191. struct string *s, *n;
  192. int rc;
  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_ERASE) {
  203. /* Use erase write alternate to initialize display. */
  204. raw3270_request_set_cmd(wrq, TC_EWRITEA);
  205. updated |= CON_UPDATE_ERASE;
  206. } else
  207. raw3270_request_set_cmd(wrq, TC_WRITE);
  208. wcc = TW_NONE;
  209. raw3270_request_add_data(wrq, &wcc, 1);
  210. /*
  211. * Update status line.
  212. */
  213. if (cp->update_flags & CON_UPDATE_STATUS)
  214. if (raw3270_request_add_data(wrq, cp->status->string,
  215. cp->status->len) == 0)
  216. updated |= CON_UPDATE_STATUS;
  217. if (cp->update_flags & CON_UPDATE_LIST) {
  218. prolog[0] = TO_SBA;
  219. prolog[3] = TO_SA;
  220. prolog[4] = TAT_COLOR;
  221. prolog[5] = TAC_TURQ;
  222. raw3270_buffer_address(cp->view.dev, prolog + 1,
  223. cp->view.cols * cp->line_nr);
  224. raw3270_request_add_data(wrq, prolog, 6);
  225. /* Write strings in the update list to the screen. */
  226. list_for_each_entry_safe(s, n, &cp->update, update) {
  227. if (s != cp->cline)
  228. con3270_update_string(cp, s, cp->line_nr);
  229. if (raw3270_request_add_data(wrq, s->string,
  230. s->len) != 0)
  231. break;
  232. list_del_init(&s->update);
  233. if (s != cp->cline)
  234. cp->line_nr++;
  235. }
  236. if (list_empty(&cp->update))
  237. updated |= CON_UPDATE_LIST;
  238. }
  239. wrq->callback = con3270_write_callback;
  240. rc = raw3270_start(&cp->view, wrq);
  241. if (rc == 0) {
  242. cp->update_flags &= ~updated;
  243. if (cp->update_flags)
  244. con3270_set_timer(cp, 1);
  245. } else {
  246. raw3270_request_reset(wrq);
  247. xchg(&cp->write, wrq);
  248. }
  249. spin_unlock_irqrestore(&cp->view.lock, flags);
  250. }
  251. /*
  252. * Read tasklet.
  253. */
  254. static void
  255. con3270_read_tasklet(struct raw3270_request *rrq)
  256. {
  257. static char kreset_data = TW_KR;
  258. struct con3270 *cp;
  259. unsigned long flags;
  260. int nr_up, deactivate;
  261. cp = (struct con3270 *) rrq->view;
  262. spin_lock_irqsave(&cp->view.lock, flags);
  263. nr_up = cp->nr_up;
  264. deactivate = 0;
  265. /* Check aid byte. */
  266. switch (cp->input->string[0]) {
  267. case 0x7d: /* enter: jump to bottom. */
  268. nr_up = 0;
  269. break;
  270. case 0xf3: /* PF3: deactivate the console view. */
  271. deactivate = 1;
  272. break;
  273. case 0x6d: /* clear: start from scratch. */
  274. con3270_rebuild_update(cp);
  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. unsigned long flags;
  348. struct con3270 *cp;
  349. cp = (struct con3270 *) view;
  350. spin_lock_irqsave(&cp->view.lock, flags);
  351. cp->nr_up = 0;
  352. con3270_rebuild_update(cp);
  353. con3270_update_status(cp);
  354. cp->update_flags = CON_UPDATE_ALL;
  355. con3270_set_timer(cp, 1);
  356. spin_unlock_irqrestore(&cp->view.lock, flags);
  357. return 0;
  358. }
  359. static void
  360. con3270_deactivate(struct raw3270_view *view)
  361. {
  362. unsigned long flags;
  363. struct con3270 *cp;
  364. cp = (struct con3270 *) view;
  365. spin_lock_irqsave(&cp->view.lock, flags);
  366. del_timer(&cp->timer);
  367. spin_unlock_irqrestore(&cp->view.lock, flags);
  368. }
  369. static int
  370. con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
  371. {
  372. /* Handle ATTN. Schedule tasklet to read aid. */
  373. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
  374. con3270_issue_read(cp);
  375. if (rq) {
  376. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
  377. rq->rc = -EIO;
  378. else
  379. /* Normal end. Copy residual count. */
  380. rq->rescnt = irb->scsw.cmd.count;
  381. }
  382. return RAW3270_IO_DONE;
  383. }
  384. /* Console view to a 3270 device. */
  385. static struct raw3270_fn con3270_fn = {
  386. .activate = con3270_activate,
  387. .deactivate = con3270_deactivate,
  388. .intv = (void *) con3270_irq
  389. };
  390. static inline void
  391. con3270_cline_add(struct con3270 *cp)
  392. {
  393. if (!list_empty(&cp->cline->list))
  394. /* Already added. */
  395. return;
  396. list_add_tail(&cp->cline->list, &cp->lines);
  397. cp->nr_lines++;
  398. con3270_rebuild_update(cp);
  399. }
  400. static inline void
  401. con3270_cline_insert(struct con3270 *cp, unsigned char c)
  402. {
  403. cp->cline->string[cp->cline->len++] =
  404. cp->view.ascebc[(c < ' ') ? ' ' : c];
  405. if (list_empty(&cp->cline->update)) {
  406. list_add_tail(&cp->cline->update, &cp->update);
  407. cp->update_flags |= CON_UPDATE_LIST;
  408. }
  409. }
  410. static inline void
  411. con3270_cline_end(struct con3270 *cp)
  412. {
  413. struct string *s;
  414. unsigned int size;
  415. /* Copy cline. */
  416. size = (cp->cline->len < cp->view.cols - 5) ?
  417. cp->cline->len + 4 : cp->view.cols;
  418. s = con3270_alloc_string(cp, size);
  419. memcpy(s->string, cp->cline->string, cp->cline->len);
  420. if (s->len < cp->view.cols - 5) {
  421. s->string[s->len - 4] = TO_RA;
  422. s->string[s->len - 1] = 0;
  423. } else {
  424. while (--size > cp->cline->len)
  425. s->string[size] = cp->view.ascebc[' '];
  426. }
  427. /* Replace cline with allocated line s and reset cline. */
  428. list_add(&s->list, &cp->cline->list);
  429. list_del_init(&cp->cline->list);
  430. if (!list_empty(&cp->cline->update)) {
  431. list_add(&s->update, &cp->cline->update);
  432. list_del_init(&cp->cline->update);
  433. }
  434. cp->cline->len = 0;
  435. }
  436. /*
  437. * Write a string to the 3270 console
  438. */
  439. static void
  440. con3270_write(struct console *co, const char *str, unsigned int count)
  441. {
  442. struct con3270 *cp;
  443. unsigned long flags;
  444. unsigned char c;
  445. cp = condev;
  446. spin_lock_irqsave(&cp->view.lock, flags);
  447. while (count-- > 0) {
  448. c = *str++;
  449. if (cp->cline->len == 0)
  450. con3270_cline_add(cp);
  451. if (c != '\n')
  452. con3270_cline_insert(cp, c);
  453. if (c == '\n' || cp->cline->len >= cp->view.cols)
  454. con3270_cline_end(cp);
  455. }
  456. /* Setup timer to output current console buffer after 1/10 second */
  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. spin_lock_irqsave(&cp->view.lock, flags);
  491. con3270_wait_write(cp);
  492. cp->nr_up = 0;
  493. con3270_rebuild_update(cp);
  494. con3270_update_status(cp);
  495. while (cp->update_flags != 0) {
  496. spin_unlock_irqrestore(&cp->view.lock, flags);
  497. con3270_update(cp);
  498. spin_lock_irqsave(&cp->view.lock, flags);
  499. con3270_wait_write(cp);
  500. }
  501. spin_unlock_irqrestore(&cp->view.lock, flags);
  502. }
  503. static int con3270_notify(struct notifier_block *self,
  504. unsigned long event, void *data)
  505. {
  506. con3270_flush();
  507. return NOTIFY_OK;
  508. }
  509. static struct notifier_block on_panic_nb = {
  510. .notifier_call = con3270_notify,
  511. .priority = 0,
  512. };
  513. static struct notifier_block on_reboot_nb = {
  514. .notifier_call = con3270_notify,
  515. .priority = 0,
  516. };
  517. /*
  518. * The console structure for the 3270 console
  519. */
  520. static struct console con3270 = {
  521. .name = "tty3270",
  522. .write = con3270_write,
  523. .device = con3270_device,
  524. .flags = CON_PRINTBUFFER,
  525. };
  526. /*
  527. * 3270 console initialization code called from console_init().
  528. * NOTE: This is called before kmalloc is available.
  529. */
  530. static int __init
  531. con3270_init(void)
  532. {
  533. struct ccw_device *cdev;
  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. cdev = ccw_device_probe_console();
  546. if (IS_ERR(cdev))
  547. return -ENODEV;
  548. rp = raw3270_setup_console(cdev);
  549. if (IS_ERR(rp))
  550. return PTR_ERR(rp);
  551. condev = (struct con3270 *) alloc_bootmem_low(sizeof(struct con3270));
  552. memset(condev, 0, sizeof(struct con3270));
  553. condev->view.dev = rp;
  554. condev->read = raw3270_request_alloc_bootmem(0);
  555. condev->read->callback = con3270_read_callback;
  556. condev->read->callback_data = condev;
  557. condev->write =
  558. raw3270_request_alloc_bootmem(CON3270_OUTPUT_BUFFER_SIZE);
  559. condev->kreset = raw3270_request_alloc_bootmem(1);
  560. INIT_LIST_HEAD(&condev->lines);
  561. INIT_LIST_HEAD(&condev->update);
  562. init_timer(&condev->timer);
  563. tasklet_init(&condev->readlet,
  564. (void (*)(unsigned long)) con3270_read_tasklet,
  565. (unsigned long) condev->read);
  566. raw3270_add_view(&condev->view, &con3270_fn, 1);
  567. INIT_LIST_HEAD(&condev->freemem);
  568. for (i = 0; i < CON3270_STRING_PAGES; i++) {
  569. cbuf = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
  570. add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
  571. }
  572. condev->cline = alloc_string(&condev->freemem, condev->view.cols);
  573. condev->cline->len = 0;
  574. con3270_create_status(condev);
  575. condev->input = alloc_string(&condev->freemem, 80);
  576. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  577. register_reboot_notifier(&on_reboot_nb);
  578. register_console(&con3270);
  579. return 0;
  580. }
  581. console_initcall(con3270_init);