tty3270.c 42 KB

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