tty3270.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  1. /*
  2. * drivers/s390/char/tty3270.c
  3. * IBM/3270 Driver - tty functions.
  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/module.h>
  11. #include <linux/types.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/tty.h>
  14. #include <linux/vt_kern.h>
  15. #include <linux/init.h>
  16. #include <linux/console.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/slab.h>
  19. #include <linux/bootmem.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/cio.h>
  22. #include <asm/ebcdic.h>
  23. #include <asm/uaccess.h>
  24. #include "raw3270.h"
  25. #include "tty3270.h"
  26. #include "keyboard.h"
  27. #define TTY3270_CHAR_BUF_SIZE 256
  28. #define TTY3270_OUTPUT_BUFFER_SIZE 1024
  29. #define TTY3270_STRING_PAGES 5
  30. struct tty_driver *tty3270_driver;
  31. static int tty3270_max_index;
  32. static struct raw3270_fn tty3270_fn;
  33. struct tty3270_cell {
  34. unsigned char character;
  35. unsigned char highlight;
  36. unsigned char f_color;
  37. };
  38. struct tty3270_line {
  39. struct tty3270_cell *cells;
  40. int len;
  41. };
  42. #define ESCAPE_NPAR 8
  43. /*
  44. * The main tty view data structure.
  45. * FIXME:
  46. * 1) describe line orientation & lines list concept against screen
  47. * 2) describe conversion of screen to lines
  48. * 3) describe line format.
  49. */
  50. struct tty3270 {
  51. struct raw3270_view view;
  52. struct tty_struct *tty; /* Pointer to tty structure */
  53. void **freemem_pages; /* Array of pages used for freemem. */
  54. struct list_head freemem; /* List of free memory for strings. */
  55. /* Output stuff. */
  56. struct list_head lines; /* List of lines. */
  57. struct list_head update; /* List of lines to update. */
  58. unsigned char wcc; /* Write control character. */
  59. int nr_lines; /* # lines in list. */
  60. int nr_up; /* # lines up in history. */
  61. unsigned long update_flags; /* Update indication bits. */
  62. struct string *status; /* Lower right of display. */
  63. struct raw3270_request *write; /* Single write request. */
  64. struct timer_list timer; /* Output delay timer. */
  65. /* Current tty screen. */
  66. unsigned int cx, cy; /* Current output position. */
  67. unsigned int highlight; /* Blink/reverse/underscore */
  68. unsigned int f_color; /* Foreground color */
  69. struct tty3270_line *screen;
  70. /* Input stuff. */
  71. struct string *prompt; /* Output string for input area. */
  72. struct string *input; /* Input string for read request. */
  73. struct raw3270_request *read; /* Single read request. */
  74. struct raw3270_request *kreset; /* Single keyboard reset request. */
  75. unsigned char inattr; /* Visible/invisible input. */
  76. int throttle, attn; /* tty throttle/unthrottle. */
  77. struct tasklet_struct readlet; /* Tasklet to issue read request. */
  78. struct kbd_data *kbd; /* key_maps stuff. */
  79. /* Escape sequence parsing. */
  80. int esc_state, esc_ques, esc_npar;
  81. int esc_par[ESCAPE_NPAR];
  82. unsigned int saved_cx, saved_cy;
  83. unsigned int saved_highlight, saved_f_color;
  84. /* Command recalling. */
  85. struct list_head rcl_lines; /* List of recallable lines. */
  86. struct list_head *rcl_walk; /* Point in rcl_lines list. */
  87. int rcl_nr, rcl_max; /* Number/max number of rcl_lines. */
  88. /* Character array for put_char/flush_chars. */
  89. unsigned int char_count;
  90. char char_buf[TTY3270_CHAR_BUF_SIZE];
  91. };
  92. /* tty3270->update_flags. See tty3270_update for details. */
  93. #define TTY_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
  94. #define TTY_UPDATE_LIST 2 /* Update lines in tty3270->update. */
  95. #define TTY_UPDATE_INPUT 4 /* Update input line. */
  96. #define TTY_UPDATE_STATUS 8 /* Update status line. */
  97. #define TTY_UPDATE_ALL 16 /* Recreate screen. */
  98. static void tty3270_update(struct tty3270 *);
  99. /*
  100. * Setup timeout for a device. On timeout trigger an update.
  101. */
  102. static void tty3270_set_timer(struct tty3270 *tp, int expires)
  103. {
  104. if (expires == 0)
  105. del_timer(&tp->timer);
  106. else
  107. mod_timer(&tp->timer, jiffies + expires);
  108. }
  109. /*
  110. * The input line are the two last lines of the screen.
  111. */
  112. static void
  113. tty3270_update_prompt(struct tty3270 *tp, char *input, int count)
  114. {
  115. struct string *line;
  116. unsigned int off;
  117. line = tp->prompt;
  118. if (count != 0)
  119. line->string[5] = TF_INMDT;
  120. else
  121. line->string[5] = tp->inattr;
  122. if (count > tp->view.cols * 2 - 11)
  123. count = tp->view.cols * 2 - 11;
  124. memcpy(line->string + 6, input, count);
  125. line->string[6 + count] = TO_IC;
  126. /* Clear to end of input line. */
  127. if (count < tp->view.cols * 2 - 11) {
  128. line->string[7 + count] = TO_RA;
  129. line->string[10 + count] = 0;
  130. off = tp->view.cols * tp->view.rows - 9;
  131. raw3270_buffer_address(tp->view.dev, line->string+count+8, off);
  132. line->len = 11 + count;
  133. } else
  134. line->len = 7 + count;
  135. tp->update_flags |= TTY_UPDATE_INPUT;
  136. }
  137. static void
  138. tty3270_create_prompt(struct tty3270 *tp)
  139. {
  140. static const unsigned char blueprint[] =
  141. { TO_SBA, 0, 0, 0x6e, TO_SF, TF_INPUT,
  142. /* empty input string */
  143. TO_IC, TO_RA, 0, 0, 0 };
  144. struct string *line;
  145. unsigned int offset;
  146. line = alloc_string(&tp->freemem,
  147. sizeof(blueprint) + tp->view.cols * 2 - 9);
  148. tp->prompt = line;
  149. tp->inattr = TF_INPUT;
  150. /* Copy blueprint to status line */
  151. memcpy(line->string, blueprint, sizeof(blueprint));
  152. line->len = sizeof(blueprint);
  153. /* Set output offsets. */
  154. offset = tp->view.cols * (tp->view.rows - 2);
  155. raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
  156. offset = tp->view.cols * tp->view.rows - 9;
  157. raw3270_buffer_address(tp->view.dev, line->string + 8, offset);
  158. /* Allocate input string for reading. */
  159. tp->input = alloc_string(&tp->freemem, tp->view.cols * 2 - 9 + 6);
  160. }
  161. /*
  162. * The status line is the last line of the screen. It shows the string
  163. * "Running"/"Holding" in the lower right corner of the screen.
  164. */
  165. static void
  166. tty3270_update_status(struct tty3270 * tp)
  167. {
  168. char *str;
  169. str = (tp->nr_up != 0) ? "History" : "Running";
  170. memcpy(tp->status->string + 8, str, 7);
  171. codepage_convert(tp->view.ascebc, tp->status->string + 8, 7);
  172. tp->update_flags |= TTY_UPDATE_STATUS;
  173. }
  174. static void
  175. tty3270_create_status(struct tty3270 * tp)
  176. {
  177. static const unsigned char blueprint[] =
  178. { TO_SBA, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR, TAC_GREEN,
  179. 0, 0, 0, 0, 0, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR,
  180. TAC_RESET };
  181. struct string *line;
  182. unsigned int offset;
  183. line = alloc_string(&tp->freemem,sizeof(blueprint));
  184. tp->status = line;
  185. /* Copy blueprint to status line */
  186. memcpy(line->string, blueprint, sizeof(blueprint));
  187. /* Set address to start of status string (= last 9 characters). */
  188. offset = tp->view.cols * tp->view.rows - 9;
  189. raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
  190. }
  191. /*
  192. * Set output offsets to 3270 datastream fragment of a tty string.
  193. * (TO_SBA offset at the start and TO_RA offset at the end of the string)
  194. */
  195. static void
  196. tty3270_update_string(struct tty3270 *tp, struct string *line, int nr)
  197. {
  198. unsigned char *cp;
  199. raw3270_buffer_address(tp->view.dev, line->string + 1,
  200. tp->view.cols * nr);
  201. cp = line->string + line->len - 4;
  202. if (*cp == TO_RA)
  203. raw3270_buffer_address(tp->view.dev, cp + 1,
  204. tp->view.cols * (nr + 1));
  205. }
  206. /*
  207. * Rebuild update list to print all lines.
  208. */
  209. static void
  210. tty3270_rebuild_update(struct tty3270 *tp)
  211. {
  212. struct string *s, *n;
  213. int line, nr_up;
  214. /*
  215. * Throw away update list and create a new one,
  216. * containing all lines that will fit on the screen.
  217. */
  218. list_for_each_entry_safe(s, n, &tp->update, update)
  219. list_del_init(&s->update);
  220. line = tp->view.rows - 3;
  221. nr_up = tp->nr_up;
  222. list_for_each_entry_reverse(s, &tp->lines, list) {
  223. if (nr_up > 0) {
  224. nr_up--;
  225. continue;
  226. }
  227. tty3270_update_string(tp, s, line);
  228. list_add(&s->update, &tp->update);
  229. if (--line < 0)
  230. break;
  231. }
  232. tp->update_flags |= TTY_UPDATE_LIST;
  233. }
  234. /*
  235. * Alloc string for size bytes. If there is not enough room in
  236. * freemem, free strings until there is room.
  237. */
  238. static struct string *
  239. tty3270_alloc_string(struct tty3270 *tp, size_t size)
  240. {
  241. struct string *s, *n;
  242. s = alloc_string(&tp->freemem, size);
  243. if (s)
  244. return s;
  245. list_for_each_entry_safe(s, n, &tp->lines, list) {
  246. BUG_ON(tp->nr_lines <= tp->view.rows - 2);
  247. list_del(&s->list);
  248. if (!list_empty(&s->update))
  249. list_del(&s->update);
  250. tp->nr_lines--;
  251. if (free_string(&tp->freemem, s) >= size)
  252. break;
  253. }
  254. s = alloc_string(&tp->freemem, size);
  255. BUG_ON(!s);
  256. if (tp->nr_up != 0 &&
  257. tp->nr_up + tp->view.rows - 2 >= tp->nr_lines) {
  258. tp->nr_up = tp->nr_lines - tp->view.rows + 2;
  259. tty3270_rebuild_update(tp);
  260. tty3270_update_status(tp);
  261. }
  262. return s;
  263. }
  264. /*
  265. * Add an empty line to the list.
  266. */
  267. static void
  268. tty3270_blank_line(struct tty3270 *tp)
  269. {
  270. static const unsigned char blueprint[] =
  271. { TO_SBA, 0, 0, TO_SA, TAT_EXTHI, TAX_RESET,
  272. TO_SA, TAT_COLOR, TAC_RESET, TO_RA, 0, 0, 0 };
  273. struct string *s;
  274. s = tty3270_alloc_string(tp, sizeof(blueprint));
  275. memcpy(s->string, blueprint, sizeof(blueprint));
  276. s->len = sizeof(blueprint);
  277. list_add_tail(&s->list, &tp->lines);
  278. tp->nr_lines++;
  279. if (tp->nr_up != 0)
  280. tp->nr_up++;
  281. }
  282. /*
  283. * Write request completion callback.
  284. */
  285. static void
  286. tty3270_write_callback(struct raw3270_request *rq, void *data)
  287. {
  288. struct tty3270 *tp;
  289. tp = (struct tty3270 *) rq->view;
  290. if (rq->rc != 0) {
  291. /* Write wasn't successfull. Refresh all. */
  292. tp->update_flags = TTY_UPDATE_ALL;
  293. tty3270_set_timer(tp, 1);
  294. }
  295. raw3270_request_reset(rq);
  296. xchg(&tp->write, rq);
  297. }
  298. /*
  299. * Update 3270 display.
  300. */
  301. static void
  302. tty3270_update(struct tty3270 *tp)
  303. {
  304. static char invalid_sba[2] = { 0xff, 0xff };
  305. struct raw3270_request *wrq;
  306. unsigned long updated;
  307. struct string *s, *n;
  308. char *sba, *str;
  309. int rc, len;
  310. wrq = xchg(&tp->write, 0);
  311. if (!wrq) {
  312. tty3270_set_timer(tp, 1);
  313. return;
  314. }
  315. spin_lock(&tp->view.lock);
  316. updated = 0;
  317. if (tp->update_flags & TTY_UPDATE_ALL) {
  318. tty3270_rebuild_update(tp);
  319. tty3270_update_status(tp);
  320. tp->update_flags = TTY_UPDATE_ERASE | TTY_UPDATE_LIST |
  321. TTY_UPDATE_INPUT | TTY_UPDATE_STATUS;
  322. }
  323. if (tp->update_flags & TTY_UPDATE_ERASE) {
  324. /* Use erase write alternate to erase display. */
  325. raw3270_request_set_cmd(wrq, TC_EWRITEA);
  326. updated |= TTY_UPDATE_ERASE;
  327. } else
  328. raw3270_request_set_cmd(wrq, TC_WRITE);
  329. raw3270_request_add_data(wrq, &tp->wcc, 1);
  330. tp->wcc = TW_NONE;
  331. /*
  332. * Update status line.
  333. */
  334. if (tp->update_flags & TTY_UPDATE_STATUS)
  335. if (raw3270_request_add_data(wrq, tp->status->string,
  336. tp->status->len) == 0)
  337. updated |= TTY_UPDATE_STATUS;
  338. /*
  339. * Write input line.
  340. */
  341. if (tp->update_flags & TTY_UPDATE_INPUT)
  342. if (raw3270_request_add_data(wrq, tp->prompt->string,
  343. tp->prompt->len) == 0)
  344. updated |= TTY_UPDATE_INPUT;
  345. sba = invalid_sba;
  346. if (tp->update_flags & TTY_UPDATE_LIST) {
  347. /* Write strings in the update list to the screen. */
  348. list_for_each_entry_safe(s, n, &tp->update, update) {
  349. str = s->string;
  350. len = s->len;
  351. /*
  352. * Skip TO_SBA at the start of the string if the
  353. * last output position matches the start address
  354. * of this line.
  355. */
  356. if (s->string[1] == sba[0] && s->string[2] == sba[1])
  357. str += 3, len -= 3;
  358. if (raw3270_request_add_data(wrq, str, len) != 0)
  359. break;
  360. list_del_init(&s->update);
  361. sba = s->string + s->len - 3;
  362. }
  363. if (list_empty(&tp->update))
  364. updated |= TTY_UPDATE_LIST;
  365. }
  366. wrq->callback = tty3270_write_callback;
  367. rc = raw3270_start(&tp->view, wrq);
  368. if (rc == 0) {
  369. tp->update_flags &= ~updated;
  370. if (tp->update_flags)
  371. tty3270_set_timer(tp, 1);
  372. } else {
  373. raw3270_request_reset(wrq);
  374. xchg(&tp->write, wrq);
  375. }
  376. spin_unlock(&tp->view.lock);
  377. }
  378. /*
  379. * Command recalling.
  380. */
  381. static void
  382. tty3270_rcl_add(struct tty3270 *tp, char *input, int len)
  383. {
  384. struct string *s;
  385. tp->rcl_walk = NULL;
  386. if (len <= 0)
  387. return;
  388. if (tp->rcl_nr >= tp->rcl_max) {
  389. s = list_entry(tp->rcl_lines.next, struct string, list);
  390. list_del(&s->list);
  391. free_string(&tp->freemem, s);
  392. tp->rcl_nr--;
  393. }
  394. s = tty3270_alloc_string(tp, len);
  395. memcpy(s->string, input, len);
  396. list_add_tail(&s->list, &tp->rcl_lines);
  397. tp->rcl_nr++;
  398. }
  399. static void
  400. tty3270_rcl_backward(struct kbd_data *kbd)
  401. {
  402. struct tty3270 *tp;
  403. struct string *s;
  404. tp = kbd->tty->driver_data;
  405. spin_lock_bh(&tp->view.lock);
  406. if (tp->inattr == TF_INPUT) {
  407. if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
  408. tp->rcl_walk = tp->rcl_walk->prev;
  409. else if (!list_empty(&tp->rcl_lines))
  410. tp->rcl_walk = tp->rcl_lines.prev;
  411. s = tp->rcl_walk ?
  412. list_entry(tp->rcl_walk, struct string, list) : NULL;
  413. if (tp->rcl_walk) {
  414. s = list_entry(tp->rcl_walk, struct string, list);
  415. tty3270_update_prompt(tp, s->string, s->len);
  416. } else
  417. tty3270_update_prompt(tp, NULL, 0);
  418. tty3270_set_timer(tp, 1);
  419. }
  420. spin_unlock_bh(&tp->view.lock);
  421. }
  422. /*
  423. * Deactivate tty view.
  424. */
  425. static void
  426. tty3270_exit_tty(struct kbd_data *kbd)
  427. {
  428. struct tty3270 *tp;
  429. tp = kbd->tty->driver_data;
  430. raw3270_deactivate_view(&tp->view);
  431. }
  432. /*
  433. * Scroll forward in history.
  434. */
  435. static void
  436. tty3270_scroll_forward(struct kbd_data *kbd)
  437. {
  438. struct tty3270 *tp;
  439. int nr_up;
  440. tp = kbd->tty->driver_data;
  441. spin_lock_bh(&tp->view.lock);
  442. nr_up = tp->nr_up - tp->view.rows + 2;
  443. if (nr_up < 0)
  444. nr_up = 0;
  445. if (nr_up != tp->nr_up) {
  446. tp->nr_up = nr_up;
  447. tty3270_rebuild_update(tp);
  448. tty3270_update_status(tp);
  449. tty3270_set_timer(tp, 1);
  450. }
  451. spin_unlock_bh(&tp->view.lock);
  452. }
  453. /*
  454. * Scroll backward in history.
  455. */
  456. static void
  457. tty3270_scroll_backward(struct kbd_data *kbd)
  458. {
  459. struct tty3270 *tp;
  460. int nr_up;
  461. tp = kbd->tty->driver_data;
  462. spin_lock_bh(&tp->view.lock);
  463. nr_up = tp->nr_up + tp->view.rows - 2;
  464. if (nr_up + tp->view.rows - 2 > tp->nr_lines)
  465. nr_up = tp->nr_lines - tp->view.rows + 2;
  466. if (nr_up != tp->nr_up) {
  467. tp->nr_up = nr_up;
  468. tty3270_rebuild_update(tp);
  469. tty3270_update_status(tp);
  470. tty3270_set_timer(tp, 1);
  471. }
  472. spin_unlock_bh(&tp->view.lock);
  473. }
  474. /*
  475. * Pass input line to tty.
  476. */
  477. static void
  478. tty3270_read_tasklet(struct raw3270_request *rrq)
  479. {
  480. static char kreset_data = TW_KR;
  481. struct tty3270 *tp;
  482. char *input;
  483. int len;
  484. tp = (struct tty3270 *) rrq->view;
  485. spin_lock_bh(&tp->view.lock);
  486. /*
  487. * Two AID keys are special: For 0x7d (enter) the input line
  488. * has to be emitted to the tty and for 0x6d the screen
  489. * needs to be redrawn.
  490. */
  491. input = NULL;
  492. len = 0;
  493. if (tp->input->string[0] == 0x7d) {
  494. /* Enter: write input to tty. */
  495. input = tp->input->string + 6;
  496. len = tp->input->len - 6 - rrq->rescnt;
  497. if (tp->inattr != TF_INPUTN)
  498. tty3270_rcl_add(tp, input, len);
  499. if (tp->nr_up > 0) {
  500. tp->nr_up = 0;
  501. tty3270_rebuild_update(tp);
  502. tty3270_update_status(tp);
  503. }
  504. /* Clear input area. */
  505. tty3270_update_prompt(tp, NULL, 0);
  506. tty3270_set_timer(tp, 1);
  507. } else if (tp->input->string[0] == 0x6d) {
  508. /* Display has been cleared. Redraw. */
  509. tp->update_flags = TTY_UPDATE_ALL;
  510. tty3270_set_timer(tp, 1);
  511. }
  512. spin_unlock_bh(&tp->view.lock);
  513. /* Start keyboard reset command. */
  514. raw3270_request_reset(tp->kreset);
  515. raw3270_request_set_cmd(tp->kreset, TC_WRITE);
  516. raw3270_request_add_data(tp->kreset, &kreset_data, 1);
  517. raw3270_start(&tp->view, tp->kreset);
  518. /* Emit input string. */
  519. if (tp->tty) {
  520. while (len-- > 0)
  521. kbd_keycode(tp->kbd, *input++);
  522. /* Emit keycode for AID byte. */
  523. kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
  524. }
  525. raw3270_request_reset(rrq);
  526. xchg(&tp->read, rrq);
  527. raw3270_put_view(&tp->view);
  528. }
  529. /*
  530. * Read request completion callback.
  531. */
  532. static void
  533. tty3270_read_callback(struct raw3270_request *rq, void *data)
  534. {
  535. raw3270_get_view(rq->view);
  536. /* Schedule tasklet to pass input to tty. */
  537. tasklet_schedule(&((struct tty3270 *) rq->view)->readlet);
  538. }
  539. /*
  540. * Issue a read request. Call with device lock.
  541. */
  542. static void
  543. tty3270_issue_read(struct tty3270 *tp, int lock)
  544. {
  545. struct raw3270_request *rrq;
  546. int rc;
  547. rrq = xchg(&tp->read, 0);
  548. if (!rrq)
  549. /* Read already scheduled. */
  550. return;
  551. rrq->callback = tty3270_read_callback;
  552. rrq->callback_data = tp;
  553. raw3270_request_set_cmd(rrq, TC_READMOD);
  554. raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
  555. /* Issue the read modified request. */
  556. if (lock) {
  557. rc = raw3270_start(&tp->view, rrq);
  558. } else
  559. rc = raw3270_start_irq(&tp->view, rrq);
  560. if (rc) {
  561. raw3270_request_reset(rrq);
  562. xchg(&tp->read, rrq);
  563. }
  564. }
  565. /*
  566. * Switch to the tty view.
  567. */
  568. static int
  569. tty3270_activate(struct raw3270_view *view)
  570. {
  571. struct tty3270 *tp;
  572. tp = (struct tty3270 *) view;
  573. tp->update_flags = TTY_UPDATE_ALL;
  574. tty3270_set_timer(tp, 1);
  575. return 0;
  576. }
  577. static void
  578. tty3270_deactivate(struct raw3270_view *view)
  579. {
  580. struct tty3270 *tp;
  581. tp = (struct tty3270 *) view;
  582. del_timer(&tp->timer);
  583. }
  584. static int
  585. tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
  586. {
  587. /* Handle ATTN. Schedule tasklet to read aid. */
  588. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  589. if (!tp->throttle)
  590. tty3270_issue_read(tp, 0);
  591. else
  592. tp->attn = 1;
  593. }
  594. if (rq) {
  595. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
  596. rq->rc = -EIO;
  597. else
  598. /* Normal end. Copy residual count. */
  599. rq->rescnt = irb->scsw.cmd.count;
  600. }
  601. return RAW3270_IO_DONE;
  602. }
  603. /*
  604. * Allocate tty3270 structure.
  605. */
  606. static struct tty3270 *
  607. tty3270_alloc_view(void)
  608. {
  609. struct tty3270 *tp;
  610. int pages;
  611. tp = kzalloc(sizeof(struct tty3270), GFP_KERNEL);
  612. if (!tp)
  613. goto out_err;
  614. tp->freemem_pages =
  615. kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL);
  616. if (!tp->freemem_pages)
  617. goto out_tp;
  618. INIT_LIST_HEAD(&tp->freemem);
  619. for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
  620. tp->freemem_pages[pages] = (void *)
  621. __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
  622. if (!tp->freemem_pages[pages])
  623. goto out_pages;
  624. add_string_memory(&tp->freemem,
  625. tp->freemem_pages[pages], PAGE_SIZE);
  626. }
  627. tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
  628. if (IS_ERR(tp->write))
  629. goto out_pages;
  630. tp->read = raw3270_request_alloc(0);
  631. if (IS_ERR(tp->read))
  632. goto out_write;
  633. tp->kreset = raw3270_request_alloc(1);
  634. if (IS_ERR(tp->kreset))
  635. goto out_read;
  636. tp->kbd = kbd_alloc();
  637. if (!tp->kbd)
  638. goto out_reset;
  639. return tp;
  640. out_reset:
  641. raw3270_request_free(tp->kreset);
  642. out_read:
  643. raw3270_request_free(tp->read);
  644. out_write:
  645. raw3270_request_free(tp->write);
  646. out_pages:
  647. while (pages--)
  648. free_pages((unsigned long) tp->freemem_pages[pages], 0);
  649. kfree(tp->freemem_pages);
  650. out_tp:
  651. kfree(tp);
  652. out_err:
  653. return ERR_PTR(-ENOMEM);
  654. }
  655. /*
  656. * Free tty3270 structure.
  657. */
  658. static void
  659. tty3270_free_view(struct tty3270 *tp)
  660. {
  661. int pages;
  662. del_timer_sync(&tp->timer);
  663. kbd_free(tp->kbd);
  664. raw3270_request_free(tp->kreset);
  665. raw3270_request_free(tp->read);
  666. raw3270_request_free(tp->write);
  667. for (pages = 0; pages < TTY3270_STRING_PAGES; pages++)
  668. free_pages((unsigned long) tp->freemem_pages[pages], 0);
  669. kfree(tp->freemem_pages);
  670. kfree(tp);
  671. }
  672. /*
  673. * Allocate tty3270 screen.
  674. */
  675. static int
  676. tty3270_alloc_screen(struct tty3270 *tp)
  677. {
  678. unsigned long size;
  679. int lines;
  680. size = sizeof(struct tty3270_line) * (tp->view.rows - 2);
  681. tp->screen = kzalloc(size, GFP_KERNEL);
  682. if (!tp->screen)
  683. goto out_err;
  684. for (lines = 0; lines < tp->view.rows - 2; lines++) {
  685. size = sizeof(struct tty3270_cell) * tp->view.cols;
  686. tp->screen[lines].cells = kzalloc(size, GFP_KERNEL);
  687. if (!tp->screen[lines].cells)
  688. goto out_screen;
  689. }
  690. return 0;
  691. out_screen:
  692. while (lines--)
  693. kfree(tp->screen[lines].cells);
  694. kfree(tp->screen);
  695. out_err:
  696. return -ENOMEM;
  697. }
  698. /*
  699. * Free tty3270 screen.
  700. */
  701. static void
  702. tty3270_free_screen(struct tty3270 *tp)
  703. {
  704. int lines;
  705. for (lines = 0; lines < tp->view.rows - 2; lines++)
  706. kfree(tp->screen[lines].cells);
  707. kfree(tp->screen);
  708. }
  709. /*
  710. * Unlink tty3270 data structure from tty.
  711. */
  712. static void
  713. tty3270_release(struct raw3270_view *view)
  714. {
  715. struct tty3270 *tp;
  716. struct tty_struct *tty;
  717. tp = (struct tty3270 *) view;
  718. tty = tp->tty;
  719. if (tty) {
  720. tty->driver_data = NULL;
  721. tp->tty = tp->kbd->tty = NULL;
  722. tty_hangup(tty);
  723. raw3270_put_view(&tp->view);
  724. }
  725. }
  726. /*
  727. * Free tty3270 data structure
  728. */
  729. static void
  730. tty3270_free(struct raw3270_view *view)
  731. {
  732. tty3270_free_screen((struct tty3270 *) view);
  733. tty3270_free_view((struct tty3270 *) view);
  734. }
  735. /*
  736. * Delayed freeing of tty3270 views.
  737. */
  738. static void
  739. tty3270_del_views(void)
  740. {
  741. struct tty3270 *tp;
  742. int i;
  743. for (i = 0; i < tty3270_max_index; i++) {
  744. tp = (struct tty3270 *)
  745. raw3270_find_view(&tty3270_fn, i + RAW3270_FIRSTMINOR);
  746. if (!IS_ERR(tp))
  747. raw3270_del_view(&tp->view);
  748. }
  749. }
  750. static struct raw3270_fn tty3270_fn = {
  751. .activate = tty3270_activate,
  752. .deactivate = tty3270_deactivate,
  753. .intv = (void *) tty3270_irq,
  754. .release = tty3270_release,
  755. .free = tty3270_free
  756. };
  757. /*
  758. * This routine is called whenever a 3270 tty is opened.
  759. */
  760. static int
  761. tty3270_open(struct tty_struct *tty, struct file * filp)
  762. {
  763. struct tty3270 *tp;
  764. int i, rc;
  765. if (tty->count > 1)
  766. return 0;
  767. /* Check if the tty3270 is already there. */
  768. tp = (struct tty3270 *)
  769. raw3270_find_view(&tty3270_fn,
  770. tty->index + RAW3270_FIRSTMINOR);
  771. if (!IS_ERR(tp)) {
  772. tty->driver_data = tp;
  773. tty->winsize.ws_row = tp->view.rows - 2;
  774. tty->winsize.ws_col = tp->view.cols;
  775. tty->low_latency = 0;
  776. tp->tty = tty;
  777. tp->kbd->tty = tty;
  778. tp->inattr = TF_INPUT;
  779. return 0;
  780. }
  781. if (tty3270_max_index < tty->index + 1)
  782. tty3270_max_index = tty->index + 1;
  783. /* Quick exit if there is no device for tty->index. */
  784. if (PTR_ERR(tp) == -ENODEV)
  785. return -ENODEV;
  786. /* Allocate tty3270 structure on first open. */
  787. tp = tty3270_alloc_view();
  788. if (IS_ERR(tp))
  789. return PTR_ERR(tp);
  790. INIT_LIST_HEAD(&tp->lines);
  791. INIT_LIST_HEAD(&tp->update);
  792. INIT_LIST_HEAD(&tp->rcl_lines);
  793. tp->rcl_max = 20;
  794. setup_timer(&tp->timer, (void (*)(unsigned long)) tty3270_update,
  795. (unsigned long) tp);
  796. tasklet_init(&tp->readlet,
  797. (void (*)(unsigned long)) tty3270_read_tasklet,
  798. (unsigned long) tp->read);
  799. rc = raw3270_add_view(&tp->view, &tty3270_fn,
  800. tty->index + RAW3270_FIRSTMINOR);
  801. if (rc) {
  802. tty3270_free_view(tp);
  803. return rc;
  804. }
  805. rc = tty3270_alloc_screen(tp);
  806. if (rc) {
  807. raw3270_put_view(&tp->view);
  808. raw3270_del_view(&tp->view);
  809. return rc;
  810. }
  811. tp->tty = tty;
  812. tty->low_latency = 0;
  813. tty->driver_data = tp;
  814. tty->winsize.ws_row = tp->view.rows - 2;
  815. tty->winsize.ws_col = tp->view.cols;
  816. tty3270_create_prompt(tp);
  817. tty3270_create_status(tp);
  818. tty3270_update_status(tp);
  819. /* Create blank line for every line in the tty output area. */
  820. for (i = 0; i < tp->view.rows - 2; i++)
  821. tty3270_blank_line(tp);
  822. tp->kbd->tty = tty;
  823. tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
  824. tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
  825. tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
  826. tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
  827. kbd_ascebc(tp->kbd, tp->view.ascebc);
  828. raw3270_activate_view(&tp->view);
  829. return 0;
  830. }
  831. /*
  832. * This routine is called when the 3270 tty is closed. We wait
  833. * for the remaining request to be completed. Then we clean up.
  834. */
  835. static void
  836. tty3270_close(struct tty_struct *tty, struct file * filp)
  837. {
  838. struct tty3270 *tp;
  839. if (tty->count > 1)
  840. return;
  841. tp = (struct tty3270 *) tty->driver_data;
  842. if (tp) {
  843. tty->driver_data = NULL;
  844. tp->tty = tp->kbd->tty = NULL;
  845. raw3270_put_view(&tp->view);
  846. }
  847. }
  848. /*
  849. * We always have room.
  850. */
  851. static int
  852. tty3270_write_room(struct tty_struct *tty)
  853. {
  854. return INT_MAX;
  855. }
  856. /*
  857. * Insert character into the screen at the current position with the
  858. * current color and highlight. This function does NOT do cursor movement.
  859. */
  860. static void tty3270_put_character(struct tty3270 *tp, char ch)
  861. {
  862. struct tty3270_line *line;
  863. struct tty3270_cell *cell;
  864. line = tp->screen + tp->cy;
  865. if (line->len <= tp->cx) {
  866. while (line->len < tp->cx) {
  867. cell = line->cells + line->len;
  868. cell->character = tp->view.ascebc[' '];
  869. cell->highlight = tp->highlight;
  870. cell->f_color = tp->f_color;
  871. line->len++;
  872. }
  873. line->len++;
  874. }
  875. cell = line->cells + tp->cx;
  876. cell->character = tp->view.ascebc[(unsigned int) ch];
  877. cell->highlight = tp->highlight;
  878. cell->f_color = tp->f_color;
  879. }
  880. /*
  881. * Convert a tty3270_line to a 3270 data fragment usable for output.
  882. */
  883. static void
  884. tty3270_convert_line(struct tty3270 *tp, int line_nr)
  885. {
  886. struct tty3270_line *line;
  887. struct tty3270_cell *cell;
  888. struct string *s, *n;
  889. unsigned char highlight;
  890. unsigned char f_color;
  891. char *cp;
  892. int flen, i;
  893. /* Determine how long the fragment will be. */
  894. flen = 3; /* Prefix (TO_SBA). */
  895. line = tp->screen + line_nr;
  896. flen += line->len;
  897. highlight = TAX_RESET;
  898. f_color = TAC_RESET;
  899. for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
  900. if (cell->highlight != highlight) {
  901. flen += 3; /* TO_SA to switch highlight. */
  902. highlight = cell->highlight;
  903. }
  904. if (cell->f_color != f_color) {
  905. flen += 3; /* TO_SA to switch color. */
  906. f_color = cell->f_color;
  907. }
  908. }
  909. if (highlight != TAX_RESET)
  910. flen += 3; /* TO_SA to reset hightlight. */
  911. if (f_color != TAC_RESET)
  912. flen += 3; /* TO_SA to reset color. */
  913. if (line->len < tp->view.cols)
  914. flen += 4; /* Postfix (TO_RA). */
  915. /* Find the line in the list. */
  916. i = tp->view.rows - 2 - line_nr;
  917. list_for_each_entry_reverse(s, &tp->lines, list)
  918. if (--i <= 0)
  919. break;
  920. /*
  921. * Check if the line needs to get reallocated.
  922. */
  923. if (s->len != flen) {
  924. /* Reallocate string. */
  925. n = tty3270_alloc_string(tp, flen);
  926. list_add(&n->list, &s->list);
  927. list_del_init(&s->list);
  928. if (!list_empty(&s->update))
  929. list_del_init(&s->update);
  930. free_string(&tp->freemem, s);
  931. s = n;
  932. }
  933. /* Write 3270 data fragment. */
  934. cp = s->string;
  935. *cp++ = TO_SBA;
  936. *cp++ = 0;
  937. *cp++ = 0;
  938. highlight = TAX_RESET;
  939. f_color = TAC_RESET;
  940. for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
  941. if (cell->highlight != highlight) {
  942. *cp++ = TO_SA;
  943. *cp++ = TAT_EXTHI;
  944. *cp++ = cell->highlight;
  945. highlight = cell->highlight;
  946. }
  947. if (cell->f_color != f_color) {
  948. *cp++ = TO_SA;
  949. *cp++ = TAT_COLOR;
  950. *cp++ = cell->f_color;
  951. f_color = cell->f_color;
  952. }
  953. *cp++ = cell->character;
  954. }
  955. if (highlight != TAX_RESET) {
  956. *cp++ = TO_SA;
  957. *cp++ = TAT_EXTHI;
  958. *cp++ = TAX_RESET;
  959. }
  960. if (f_color != TAC_RESET) {
  961. *cp++ = TO_SA;
  962. *cp++ = TAT_COLOR;
  963. *cp++ = TAC_RESET;
  964. }
  965. if (line->len < tp->view.cols) {
  966. *cp++ = TO_RA;
  967. *cp++ = 0;
  968. *cp++ = 0;
  969. *cp++ = 0;
  970. }
  971. if (tp->nr_up + line_nr < tp->view.rows - 2) {
  972. /* Line is currently visible on screen. */
  973. tty3270_update_string(tp, s, line_nr);
  974. /* Add line to update list. */
  975. if (list_empty(&s->update)) {
  976. list_add_tail(&s->update, &tp->update);
  977. tp->update_flags |= TTY_UPDATE_LIST;
  978. }
  979. }
  980. }
  981. /*
  982. * Do carriage return.
  983. */
  984. static void
  985. tty3270_cr(struct tty3270 *tp)
  986. {
  987. tp->cx = 0;
  988. }
  989. /*
  990. * Do line feed.
  991. */
  992. static void
  993. tty3270_lf(struct tty3270 *tp)
  994. {
  995. struct tty3270_line temp;
  996. int i;
  997. tty3270_convert_line(tp, tp->cy);
  998. if (tp->cy < tp->view.rows - 3) {
  999. tp->cy++;
  1000. return;
  1001. }
  1002. /* Last line just filled up. Add new, blank line. */
  1003. tty3270_blank_line(tp);
  1004. temp = tp->screen[0];
  1005. temp.len = 0;
  1006. for (i = 0; i < tp->view.rows - 3; i++)
  1007. tp->screen[i] = tp->screen[i+1];
  1008. tp->screen[tp->view.rows - 3] = temp;
  1009. tty3270_rebuild_update(tp);
  1010. }
  1011. static void
  1012. tty3270_ri(struct tty3270 *tp)
  1013. {
  1014. if (tp->cy > 0) {
  1015. tty3270_convert_line(tp, tp->cy);
  1016. tp->cy--;
  1017. }
  1018. }
  1019. /*
  1020. * Insert characters at current position.
  1021. */
  1022. static void
  1023. tty3270_insert_characters(struct tty3270 *tp, int n)
  1024. {
  1025. struct tty3270_line *line;
  1026. int k;
  1027. line = tp->screen + tp->cy;
  1028. while (line->len < tp->cx) {
  1029. line->cells[line->len].character = tp->view.ascebc[' '];
  1030. line->cells[line->len].highlight = TAX_RESET;
  1031. line->cells[line->len].f_color = TAC_RESET;
  1032. line->len++;
  1033. }
  1034. if (n > tp->view.cols - tp->cx)
  1035. n = tp->view.cols - tp->cx;
  1036. k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
  1037. while (k--)
  1038. line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
  1039. line->len += n;
  1040. if (line->len > tp->view.cols)
  1041. line->len = tp->view.cols;
  1042. while (n-- > 0) {
  1043. line->cells[tp->cx + n].character = tp->view.ascebc[' '];
  1044. line->cells[tp->cx + n].highlight = tp->highlight;
  1045. line->cells[tp->cx + n].f_color = tp->f_color;
  1046. }
  1047. }
  1048. /*
  1049. * Delete characters at current position.
  1050. */
  1051. static void
  1052. tty3270_delete_characters(struct tty3270 *tp, int n)
  1053. {
  1054. struct tty3270_line *line;
  1055. int i;
  1056. line = tp->screen + tp->cy;
  1057. if (line->len <= tp->cx)
  1058. return;
  1059. if (line->len - tp->cx <= n) {
  1060. line->len = tp->cx;
  1061. return;
  1062. }
  1063. for (i = tp->cx; i + n < line->len; i++)
  1064. line->cells[i] = line->cells[i + n];
  1065. line->len -= n;
  1066. }
  1067. /*
  1068. * Erase characters at current position.
  1069. */
  1070. static void
  1071. tty3270_erase_characters(struct tty3270 *tp, int n)
  1072. {
  1073. struct tty3270_line *line;
  1074. struct tty3270_cell *cell;
  1075. line = tp->screen + tp->cy;
  1076. while (line->len > tp->cx && n-- > 0) {
  1077. cell = line->cells + tp->cx++;
  1078. cell->character = ' ';
  1079. cell->highlight = TAX_RESET;
  1080. cell->f_color = TAC_RESET;
  1081. }
  1082. tp->cx += n;
  1083. tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
  1084. }
  1085. /*
  1086. * Erase line, 3 different cases:
  1087. * Esc [ 0 K Erase from current position to end of line inclusive
  1088. * Esc [ 1 K Erase from beginning of line to current position inclusive
  1089. * Esc [ 2 K Erase entire line (without moving cursor)
  1090. */
  1091. static void
  1092. tty3270_erase_line(struct tty3270 *tp, int mode)
  1093. {
  1094. struct tty3270_line *line;
  1095. struct tty3270_cell *cell;
  1096. int i;
  1097. line = tp->screen + tp->cy;
  1098. if (mode == 0)
  1099. line->len = tp->cx;
  1100. else if (mode == 1) {
  1101. for (i = 0; i < tp->cx; i++) {
  1102. cell = line->cells + i;
  1103. cell->character = ' ';
  1104. cell->highlight = TAX_RESET;
  1105. cell->f_color = TAC_RESET;
  1106. }
  1107. if (line->len <= tp->cx)
  1108. line->len = tp->cx + 1;
  1109. } else if (mode == 2)
  1110. line->len = 0;
  1111. tty3270_convert_line(tp, tp->cy);
  1112. }
  1113. /*
  1114. * Erase display, 3 different cases:
  1115. * Esc [ 0 J Erase from current position to bottom of screen inclusive
  1116. * Esc [ 1 J Erase from top of screen to current position inclusive
  1117. * Esc [ 2 J Erase entire screen (without moving the cursor)
  1118. */
  1119. static void
  1120. tty3270_erase_display(struct tty3270 *tp, int mode)
  1121. {
  1122. int i;
  1123. if (mode == 0) {
  1124. tty3270_erase_line(tp, 0);
  1125. for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
  1126. tp->screen[i].len = 0;
  1127. tty3270_convert_line(tp, i);
  1128. }
  1129. } else if (mode == 1) {
  1130. for (i = 0; i < tp->cy; i++) {
  1131. tp->screen[i].len = 0;
  1132. tty3270_convert_line(tp, i);
  1133. }
  1134. tty3270_erase_line(tp, 1);
  1135. } else if (mode == 2) {
  1136. for (i = 0; i < tp->view.rows - 2; i++) {
  1137. tp->screen[i].len = 0;
  1138. tty3270_convert_line(tp, i);
  1139. }
  1140. }
  1141. tty3270_rebuild_update(tp);
  1142. }
  1143. /*
  1144. * Set attributes found in an escape sequence.
  1145. * Esc [ <attr> ; <attr> ; ... m
  1146. */
  1147. static void
  1148. tty3270_set_attributes(struct tty3270 *tp)
  1149. {
  1150. static unsigned char f_colors[] = {
  1151. TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
  1152. TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
  1153. };
  1154. int i, attr;
  1155. for (i = 0; i <= tp->esc_npar; i++) {
  1156. attr = tp->esc_par[i];
  1157. switch (attr) {
  1158. case 0: /* Reset */
  1159. tp->highlight = TAX_RESET;
  1160. tp->f_color = TAC_RESET;
  1161. break;
  1162. /* Highlight. */
  1163. case 4: /* Start underlining. */
  1164. tp->highlight = TAX_UNDER;
  1165. break;
  1166. case 5: /* Start blink. */
  1167. tp->highlight = TAX_BLINK;
  1168. break;
  1169. case 7: /* Start reverse. */
  1170. tp->highlight = TAX_REVER;
  1171. break;
  1172. case 24: /* End underlining */
  1173. if (tp->highlight == TAX_UNDER)
  1174. tp->highlight = TAX_RESET;
  1175. break;
  1176. case 25: /* End blink. */
  1177. if (tp->highlight == TAX_BLINK)
  1178. tp->highlight = TAX_RESET;
  1179. break;
  1180. case 27: /* End reverse. */
  1181. if (tp->highlight == TAX_REVER)
  1182. tp->highlight = TAX_RESET;
  1183. break;
  1184. /* Foreground color. */
  1185. case 30: /* Black */
  1186. case 31: /* Red */
  1187. case 32: /* Green */
  1188. case 33: /* Yellow */
  1189. case 34: /* Blue */
  1190. case 35: /* Magenta */
  1191. case 36: /* Cyan */
  1192. case 37: /* White */
  1193. case 39: /* Black */
  1194. tp->f_color = f_colors[attr - 30];
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. static inline int
  1200. tty3270_getpar(struct tty3270 *tp, int ix)
  1201. {
  1202. return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
  1203. }
  1204. static void
  1205. tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
  1206. {
  1207. int max_cx = max(0, cx);
  1208. int max_cy = max(0, cy);
  1209. tp->cx = min_t(int, tp->view.cols - 1, max_cx);
  1210. cy = min_t(int, tp->view.rows - 3, max_cy);
  1211. if (cy != tp->cy) {
  1212. tty3270_convert_line(tp, tp->cy);
  1213. tp->cy = cy;
  1214. }
  1215. }
  1216. /*
  1217. * Process escape sequences. Known sequences:
  1218. * Esc 7 Save Cursor Position
  1219. * Esc 8 Restore Cursor Position
  1220. * Esc [ Pn ; Pn ; .. m Set attributes
  1221. * Esc [ Pn ; Pn H Cursor Position
  1222. * Esc [ Pn ; Pn f Cursor Position
  1223. * Esc [ Pn A Cursor Up
  1224. * Esc [ Pn B Cursor Down
  1225. * Esc [ Pn C Cursor Forward
  1226. * Esc [ Pn D Cursor Backward
  1227. * Esc [ Pn G Cursor Horizontal Absolute
  1228. * Esc [ Pn X Erase Characters
  1229. * Esc [ Ps J Erase in Display
  1230. * Esc [ Ps K Erase in Line
  1231. * // FIXME: add all the new ones.
  1232. *
  1233. * Pn is a numeric parameter, a string of zero or more decimal digits.
  1234. * Ps is a selective parameter.
  1235. */
  1236. static void
  1237. tty3270_escape_sequence(struct tty3270 *tp, char ch)
  1238. {
  1239. enum { ESnormal, ESesc, ESsquare, ESgetpars };
  1240. if (tp->esc_state == ESnormal) {
  1241. if (ch == 0x1b)
  1242. /* Starting new escape sequence. */
  1243. tp->esc_state = ESesc;
  1244. return;
  1245. }
  1246. if (tp->esc_state == ESesc) {
  1247. tp->esc_state = ESnormal;
  1248. switch (ch) {
  1249. case '[':
  1250. tp->esc_state = ESsquare;
  1251. break;
  1252. case 'E':
  1253. tty3270_cr(tp);
  1254. tty3270_lf(tp);
  1255. break;
  1256. case 'M':
  1257. tty3270_ri(tp);
  1258. break;
  1259. case 'D':
  1260. tty3270_lf(tp);
  1261. break;
  1262. case 'Z': /* Respond ID. */
  1263. kbd_puts_queue(tp->tty, "\033[?6c");
  1264. break;
  1265. case '7': /* Save cursor position. */
  1266. tp->saved_cx = tp->cx;
  1267. tp->saved_cy = tp->cy;
  1268. tp->saved_highlight = tp->highlight;
  1269. tp->saved_f_color = tp->f_color;
  1270. break;
  1271. case '8': /* Restore cursor position. */
  1272. tty3270_convert_line(tp, tp->cy);
  1273. tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
  1274. tp->highlight = tp->saved_highlight;
  1275. tp->f_color = tp->saved_f_color;
  1276. break;
  1277. case 'c': /* Reset terminal. */
  1278. tp->cx = tp->saved_cx = 0;
  1279. tp->cy = tp->saved_cy = 0;
  1280. tp->highlight = tp->saved_highlight = TAX_RESET;
  1281. tp->f_color = tp->saved_f_color = TAC_RESET;
  1282. tty3270_erase_display(tp, 2);
  1283. break;
  1284. }
  1285. return;
  1286. }
  1287. if (tp->esc_state == ESsquare) {
  1288. tp->esc_state = ESgetpars;
  1289. memset(tp->esc_par, 0, sizeof(tp->esc_par));
  1290. tp->esc_npar = 0;
  1291. tp->esc_ques = (ch == '?');
  1292. if (tp->esc_ques)
  1293. return;
  1294. }
  1295. if (tp->esc_state == ESgetpars) {
  1296. if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
  1297. tp->esc_npar++;
  1298. return;
  1299. }
  1300. if (ch >= '0' && ch <= '9') {
  1301. tp->esc_par[tp->esc_npar] *= 10;
  1302. tp->esc_par[tp->esc_npar] += ch - '0';
  1303. return;
  1304. }
  1305. }
  1306. tp->esc_state = ESnormal;
  1307. if (ch == 'n' && !tp->esc_ques) {
  1308. if (tp->esc_par[0] == 5) /* Status report. */
  1309. kbd_puts_queue(tp->tty, "\033[0n");
  1310. else if (tp->esc_par[0] == 6) { /* Cursor report. */
  1311. char buf[40];
  1312. sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
  1313. kbd_puts_queue(tp->tty, buf);
  1314. }
  1315. return;
  1316. }
  1317. if (tp->esc_ques)
  1318. return;
  1319. switch (ch) {
  1320. case 'm':
  1321. tty3270_set_attributes(tp);
  1322. break;
  1323. case 'H': /* Set cursor position. */
  1324. case 'f':
  1325. tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
  1326. tty3270_getpar(tp, 0) - 1);
  1327. break;
  1328. case 'd': /* Set y position. */
  1329. tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
  1330. break;
  1331. case 'A': /* Cursor up. */
  1332. case 'F':
  1333. tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
  1334. break;
  1335. case 'B': /* Cursor down. */
  1336. case 'e':
  1337. case 'E':
  1338. tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
  1339. break;
  1340. case 'C': /* Cursor forward. */
  1341. case 'a':
  1342. tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
  1343. break;
  1344. case 'D': /* Cursor backward. */
  1345. tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
  1346. break;
  1347. case 'G': /* Set x position. */
  1348. case '`':
  1349. tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
  1350. break;
  1351. case 'X': /* Erase Characters. */
  1352. tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
  1353. break;
  1354. case 'J': /* Erase display. */
  1355. tty3270_erase_display(tp, tp->esc_par[0]);
  1356. break;
  1357. case 'K': /* Erase line. */
  1358. tty3270_erase_line(tp, tp->esc_par[0]);
  1359. break;
  1360. case 'P': /* Delete characters. */
  1361. tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
  1362. break;
  1363. case '@': /* Insert characters. */
  1364. tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
  1365. break;
  1366. case 's': /* Save cursor position. */
  1367. tp->saved_cx = tp->cx;
  1368. tp->saved_cy = tp->cy;
  1369. tp->saved_highlight = tp->highlight;
  1370. tp->saved_f_color = tp->f_color;
  1371. break;
  1372. case 'u': /* Restore cursor position. */
  1373. tty3270_convert_line(tp, tp->cy);
  1374. tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
  1375. tp->highlight = tp->saved_highlight;
  1376. tp->f_color = tp->saved_f_color;
  1377. break;
  1378. }
  1379. }
  1380. /*
  1381. * String write routine for 3270 ttys
  1382. */
  1383. static void
  1384. tty3270_do_write(struct tty3270 *tp, const unsigned char *buf, int count)
  1385. {
  1386. int i_msg, i;
  1387. spin_lock_bh(&tp->view.lock);
  1388. for (i_msg = 0; !tp->tty->stopped && i_msg < count; i_msg++) {
  1389. if (tp->esc_state != 0) {
  1390. /* Continue escape sequence. */
  1391. tty3270_escape_sequence(tp, buf[i_msg]);
  1392. continue;
  1393. }
  1394. switch (buf[i_msg]) {
  1395. case 0x07: /* '\a' -- Alarm */
  1396. tp->wcc |= TW_PLUSALARM;
  1397. break;
  1398. case 0x08: /* Backspace. */
  1399. if (tp->cx > 0) {
  1400. tp->cx--;
  1401. tty3270_put_character(tp, ' ');
  1402. }
  1403. break;
  1404. case 0x09: /* '\t' -- Tabulate */
  1405. for (i = tp->cx % 8; i < 8; i++) {
  1406. if (tp->cx >= tp->view.cols) {
  1407. tty3270_cr(tp);
  1408. tty3270_lf(tp);
  1409. break;
  1410. }
  1411. tty3270_put_character(tp, ' ');
  1412. tp->cx++;
  1413. }
  1414. break;
  1415. case 0x0a: /* '\n' -- New Line */
  1416. tty3270_cr(tp);
  1417. tty3270_lf(tp);
  1418. break;
  1419. case 0x0c: /* '\f' -- Form Feed */
  1420. tty3270_erase_display(tp, 2);
  1421. tp->cx = tp->cy = 0;
  1422. break;
  1423. case 0x0d: /* '\r' -- Carriage Return */
  1424. tp->cx = 0;
  1425. break;
  1426. case 0x0f: /* SuSE "exit alternate mode" */
  1427. break;
  1428. case 0x1b: /* Start escape sequence. */
  1429. tty3270_escape_sequence(tp, buf[i_msg]);
  1430. break;
  1431. default: /* Insert normal character. */
  1432. if (tp->cx >= tp->view.cols) {
  1433. tty3270_cr(tp);
  1434. tty3270_lf(tp);
  1435. }
  1436. tty3270_put_character(tp, buf[i_msg]);
  1437. tp->cx++;
  1438. break;
  1439. }
  1440. }
  1441. /* Convert current line to 3270 data fragment. */
  1442. tty3270_convert_line(tp, tp->cy);
  1443. /* Setup timer to update display after 1/10 second */
  1444. if (!timer_pending(&tp->timer))
  1445. tty3270_set_timer(tp, HZ/10);
  1446. spin_unlock_bh(&tp->view.lock);
  1447. }
  1448. /*
  1449. * String write routine for 3270 ttys
  1450. */
  1451. static int
  1452. tty3270_write(struct tty_struct * tty,
  1453. const unsigned char *buf, int count)
  1454. {
  1455. struct tty3270 *tp;
  1456. tp = tty->driver_data;
  1457. if (!tp)
  1458. return 0;
  1459. if (tp->char_count > 0) {
  1460. tty3270_do_write(tp, tp->char_buf, tp->char_count);
  1461. tp->char_count = 0;
  1462. }
  1463. tty3270_do_write(tp, buf, count);
  1464. return count;
  1465. }
  1466. /*
  1467. * Put single characters to the ttys character buffer
  1468. */
  1469. static int tty3270_put_char(struct tty_struct *tty, unsigned char ch)
  1470. {
  1471. struct tty3270 *tp;
  1472. tp = tty->driver_data;
  1473. if (!tp || tp->char_count >= TTY3270_CHAR_BUF_SIZE)
  1474. return 0;
  1475. tp->char_buf[tp->char_count++] = ch;
  1476. return 1;
  1477. }
  1478. /*
  1479. * Flush all characters from the ttys characeter buffer put there
  1480. * by tty3270_put_char.
  1481. */
  1482. static void
  1483. tty3270_flush_chars(struct tty_struct *tty)
  1484. {
  1485. struct tty3270 *tp;
  1486. tp = tty->driver_data;
  1487. if (!tp)
  1488. return;
  1489. if (tp->char_count > 0) {
  1490. tty3270_do_write(tp, tp->char_buf, tp->char_count);
  1491. tp->char_count = 0;
  1492. }
  1493. }
  1494. /*
  1495. * Returns the number of characters in the output buffer. This is
  1496. * used in tty_wait_until_sent to wait until all characters have
  1497. * appeared on the screen.
  1498. */
  1499. static int
  1500. tty3270_chars_in_buffer(struct tty_struct *tty)
  1501. {
  1502. return 0;
  1503. }
  1504. static void
  1505. tty3270_flush_buffer(struct tty_struct *tty)
  1506. {
  1507. }
  1508. /*
  1509. * Check for visible/invisible input switches
  1510. */
  1511. static void
  1512. tty3270_set_termios(struct tty_struct *tty, struct ktermios *old)
  1513. {
  1514. struct tty3270 *tp;
  1515. int new;
  1516. tp = tty->driver_data;
  1517. if (!tp)
  1518. return;
  1519. spin_lock_bh(&tp->view.lock);
  1520. if (L_ICANON(tty)) {
  1521. new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
  1522. if (new != tp->inattr) {
  1523. tp->inattr = new;
  1524. tty3270_update_prompt(tp, NULL, 0);
  1525. tty3270_set_timer(tp, 1);
  1526. }
  1527. }
  1528. spin_unlock_bh(&tp->view.lock);
  1529. }
  1530. /*
  1531. * Disable reading from a 3270 tty
  1532. */
  1533. static void
  1534. tty3270_throttle(struct tty_struct * tty)
  1535. {
  1536. struct tty3270 *tp;
  1537. tp = tty->driver_data;
  1538. if (!tp)
  1539. return;
  1540. tp->throttle = 1;
  1541. }
  1542. /*
  1543. * Enable reading from a 3270 tty
  1544. */
  1545. static void
  1546. tty3270_unthrottle(struct tty_struct * tty)
  1547. {
  1548. struct tty3270 *tp;
  1549. tp = tty->driver_data;
  1550. if (!tp)
  1551. return;
  1552. tp->throttle = 0;
  1553. if (tp->attn)
  1554. tty3270_issue_read(tp, 1);
  1555. }
  1556. /*
  1557. * Hang up the tty device.
  1558. */
  1559. static void
  1560. tty3270_hangup(struct tty_struct *tty)
  1561. {
  1562. // FIXME: implement
  1563. }
  1564. static void
  1565. tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
  1566. {
  1567. }
  1568. static int
  1569. tty3270_ioctl(struct tty_struct *tty, struct file *file,
  1570. unsigned int cmd, unsigned long arg)
  1571. {
  1572. struct tty3270 *tp;
  1573. tp = tty->driver_data;
  1574. if (!tp)
  1575. return -ENODEV;
  1576. if (tty->flags & (1 << TTY_IO_ERROR))
  1577. return -EIO;
  1578. return kbd_ioctl(tp->kbd, file, cmd, arg);
  1579. }
  1580. static const struct tty_operations tty3270_ops = {
  1581. .open = tty3270_open,
  1582. .close = tty3270_close,
  1583. .write = tty3270_write,
  1584. .put_char = tty3270_put_char,
  1585. .flush_chars = tty3270_flush_chars,
  1586. .write_room = tty3270_write_room,
  1587. .chars_in_buffer = tty3270_chars_in_buffer,
  1588. .flush_buffer = tty3270_flush_buffer,
  1589. .throttle = tty3270_throttle,
  1590. .unthrottle = tty3270_unthrottle,
  1591. .hangup = tty3270_hangup,
  1592. .wait_until_sent = tty3270_wait_until_sent,
  1593. .ioctl = tty3270_ioctl,
  1594. .set_termios = tty3270_set_termios
  1595. };
  1596. /*
  1597. * 3270 tty registration code called from tty_init().
  1598. * Most kernel services (incl. kmalloc) are available at this poimt.
  1599. */
  1600. static int __init tty3270_init(void)
  1601. {
  1602. struct tty_driver *driver;
  1603. int ret;
  1604. driver = alloc_tty_driver(RAW3270_MAXDEVS);
  1605. if (!driver)
  1606. return -ENOMEM;
  1607. /*
  1608. * Initialize the tty_driver structure
  1609. * Entries in tty3270_driver that are NOT initialized:
  1610. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1611. */
  1612. driver->owner = THIS_MODULE;
  1613. driver->driver_name = "ttyTUB";
  1614. driver->name = "ttyTUB";
  1615. driver->major = IBM_TTY3270_MAJOR;
  1616. driver->minor_start = RAW3270_FIRSTMINOR;
  1617. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1618. driver->subtype = SYSTEM_TYPE_TTY;
  1619. driver->init_termios = tty_std_termios;
  1620. driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_DYNAMIC_DEV;
  1621. tty_set_operations(driver, &tty3270_ops);
  1622. ret = tty_register_driver(driver);
  1623. if (ret) {
  1624. put_tty_driver(driver);
  1625. return ret;
  1626. }
  1627. tty3270_driver = driver;
  1628. return 0;
  1629. }
  1630. static void __exit
  1631. tty3270_exit(void)
  1632. {
  1633. struct tty_driver *driver;
  1634. driver = tty3270_driver;
  1635. tty3270_driver = NULL;
  1636. tty_unregister_driver(driver);
  1637. tty3270_del_views();
  1638. }
  1639. MODULE_LICENSE("GPL");
  1640. MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
  1641. module_init(tty3270_init);
  1642. module_exit(tty3270_exit);