tty3270.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. /*
  2. * IBM/3270 Driver - tty functions.
  3. *
  4. * Author(s):
  5. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  6. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * -- Copyright IBM Corp. 2003
  8. */
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/kdev_t.h>
  12. #include <linux/tty.h>
  13. #include <linux/vt_kern.h>
  14. #include <linux/init.h>
  15. #include <linux/console.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/slab.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/compat.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_port port;
  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 = container_of(rq->view, struct tty3270, view);
  289. if (rq->rc != 0) {
  290. /* Write wasn't successful. Refresh all. */
  291. tp->update_flags = TTY_UPDATE_ALL;
  292. tty3270_set_timer(tp, 1);
  293. }
  294. raw3270_request_reset(rq);
  295. xchg(&tp->write, rq);
  296. }
  297. /*
  298. * Update 3270 display.
  299. */
  300. static void
  301. tty3270_update(struct tty3270 *tp)
  302. {
  303. static char invalid_sba[2] = { 0xff, 0xff };
  304. struct raw3270_request *wrq;
  305. unsigned long updated;
  306. struct string *s, *n;
  307. char *sba, *str;
  308. int rc, len;
  309. wrq = xchg(&tp->write, 0);
  310. if (!wrq) {
  311. tty3270_set_timer(tp, 1);
  312. return;
  313. }
  314. spin_lock(&tp->view.lock);
  315. updated = 0;
  316. if (tp->update_flags & TTY_UPDATE_ALL) {
  317. tty3270_rebuild_update(tp);
  318. tty3270_update_status(tp);
  319. tp->update_flags = TTY_UPDATE_ERASE | TTY_UPDATE_LIST |
  320. TTY_UPDATE_INPUT | TTY_UPDATE_STATUS;
  321. }
  322. if (tp->update_flags & TTY_UPDATE_ERASE) {
  323. /* Use erase write alternate to erase display. */
  324. raw3270_request_set_cmd(wrq, TC_EWRITEA);
  325. updated |= TTY_UPDATE_ERASE;
  326. } else
  327. raw3270_request_set_cmd(wrq, TC_WRITE);
  328. raw3270_request_add_data(wrq, &tp->wcc, 1);
  329. tp->wcc = TW_NONE;
  330. /*
  331. * Update status line.
  332. */
  333. if (tp->update_flags & TTY_UPDATE_STATUS)
  334. if (raw3270_request_add_data(wrq, tp->status->string,
  335. tp->status->len) == 0)
  336. updated |= TTY_UPDATE_STATUS;
  337. /*
  338. * Write input line.
  339. */
  340. if (tp->update_flags & TTY_UPDATE_INPUT)
  341. if (raw3270_request_add_data(wrq, tp->prompt->string,
  342. tp->prompt->len) == 0)
  343. updated |= TTY_UPDATE_INPUT;
  344. sba = invalid_sba;
  345. if (tp->update_flags & TTY_UPDATE_LIST) {
  346. /* Write strings in the update list to the screen. */
  347. list_for_each_entry_safe(s, n, &tp->update, update) {
  348. str = s->string;
  349. len = s->len;
  350. /*
  351. * Skip TO_SBA at the start of the string if the
  352. * last output position matches the start address
  353. * of this line.
  354. */
  355. if (s->string[1] == sba[0] && s->string[2] == sba[1])
  356. str += 3, len -= 3;
  357. if (raw3270_request_add_data(wrq, str, len) != 0)
  358. break;
  359. list_del_init(&s->update);
  360. sba = s->string + s->len - 3;
  361. }
  362. if (list_empty(&tp->update))
  363. updated |= TTY_UPDATE_LIST;
  364. }
  365. wrq->callback = tty3270_write_callback;
  366. rc = raw3270_start(&tp->view, wrq);
  367. if (rc == 0) {
  368. tp->update_flags &= ~updated;
  369. if (tp->update_flags)
  370. tty3270_set_timer(tp, 1);
  371. } else {
  372. raw3270_request_reset(wrq);
  373. xchg(&tp->write, wrq);
  374. }
  375. spin_unlock(&tp->view.lock);
  376. }
  377. /*
  378. * Command recalling.
  379. */
  380. static void
  381. tty3270_rcl_add(struct tty3270 *tp, char *input, int len)
  382. {
  383. struct string *s;
  384. tp->rcl_walk = NULL;
  385. if (len <= 0)
  386. return;
  387. if (tp->rcl_nr >= tp->rcl_max) {
  388. s = list_entry(tp->rcl_lines.next, struct string, list);
  389. list_del(&s->list);
  390. free_string(&tp->freemem, s);
  391. tp->rcl_nr--;
  392. }
  393. s = tty3270_alloc_string(tp, len);
  394. memcpy(s->string, input, len);
  395. list_add_tail(&s->list, &tp->rcl_lines);
  396. tp->rcl_nr++;
  397. }
  398. static void
  399. tty3270_rcl_backward(struct kbd_data *kbd)
  400. {
  401. struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
  402. struct string *s;
  403. spin_lock_bh(&tp->view.lock);
  404. if (tp->inattr == TF_INPUT) {
  405. if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
  406. tp->rcl_walk = tp->rcl_walk->prev;
  407. else if (!list_empty(&tp->rcl_lines))
  408. tp->rcl_walk = tp->rcl_lines.prev;
  409. s = tp->rcl_walk ?
  410. list_entry(tp->rcl_walk, struct string, list) : NULL;
  411. if (tp->rcl_walk) {
  412. s = list_entry(tp->rcl_walk, struct string, list);
  413. tty3270_update_prompt(tp, s->string, s->len);
  414. } else
  415. tty3270_update_prompt(tp, NULL, 0);
  416. tty3270_set_timer(tp, 1);
  417. }
  418. spin_unlock_bh(&tp->view.lock);
  419. }
  420. /*
  421. * Deactivate tty view.
  422. */
  423. static void
  424. tty3270_exit_tty(struct kbd_data *kbd)
  425. {
  426. struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
  427. raw3270_deactivate_view(&tp->view);
  428. }
  429. /*
  430. * Scroll forward in history.
  431. */
  432. static void
  433. tty3270_scroll_forward(struct kbd_data *kbd)
  434. {
  435. struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
  436. int nr_up;
  437. spin_lock_bh(&tp->view.lock);
  438. nr_up = tp->nr_up - tp->view.rows + 2;
  439. if (nr_up < 0)
  440. nr_up = 0;
  441. if (nr_up != tp->nr_up) {
  442. tp->nr_up = nr_up;
  443. tty3270_rebuild_update(tp);
  444. tty3270_update_status(tp);
  445. tty3270_set_timer(tp, 1);
  446. }
  447. spin_unlock_bh(&tp->view.lock);
  448. }
  449. /*
  450. * Scroll backward in history.
  451. */
  452. static void
  453. tty3270_scroll_backward(struct kbd_data *kbd)
  454. {
  455. struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
  456. int nr_up;
  457. spin_lock_bh(&tp->view.lock);
  458. nr_up = tp->nr_up + tp->view.rows - 2;
  459. if (nr_up + tp->view.rows - 2 > tp->nr_lines)
  460. nr_up = tp->nr_lines - tp->view.rows + 2;
  461. if (nr_up != tp->nr_up) {
  462. tp->nr_up = nr_up;
  463. tty3270_rebuild_update(tp);
  464. tty3270_update_status(tp);
  465. tty3270_set_timer(tp, 1);
  466. }
  467. spin_unlock_bh(&tp->view.lock);
  468. }
  469. /*
  470. * Pass input line to tty.
  471. */
  472. static void
  473. tty3270_read_tasklet(struct raw3270_request *rrq)
  474. {
  475. static char kreset_data = TW_KR;
  476. struct tty3270 *tp = container_of(rrq->view, struct tty3270, view);
  477. char *input;
  478. int len;
  479. spin_lock_bh(&tp->view.lock);
  480. /*
  481. * Two AID keys are special: For 0x7d (enter) the input line
  482. * has to be emitted to the tty and for 0x6d the screen
  483. * needs to be redrawn.
  484. */
  485. input = NULL;
  486. len = 0;
  487. if (tp->input->string[0] == 0x7d) {
  488. /* Enter: write input to tty. */
  489. input = tp->input->string + 6;
  490. len = tp->input->len - 6 - rrq->rescnt;
  491. if (tp->inattr != TF_INPUTN)
  492. tty3270_rcl_add(tp, input, len);
  493. if (tp->nr_up > 0) {
  494. tp->nr_up = 0;
  495. tty3270_rebuild_update(tp);
  496. tty3270_update_status(tp);
  497. }
  498. /* Clear input area. */
  499. tty3270_update_prompt(tp, NULL, 0);
  500. tty3270_set_timer(tp, 1);
  501. } else if (tp->input->string[0] == 0x6d) {
  502. /* Display has been cleared. Redraw. */
  503. tp->update_flags = TTY_UPDATE_ALL;
  504. tty3270_set_timer(tp, 1);
  505. }
  506. spin_unlock_bh(&tp->view.lock);
  507. /* Start keyboard reset command. */
  508. raw3270_request_reset(tp->kreset);
  509. raw3270_request_set_cmd(tp->kreset, TC_WRITE);
  510. raw3270_request_add_data(tp->kreset, &kreset_data, 1);
  511. raw3270_start(&tp->view, tp->kreset);
  512. while (len-- > 0)
  513. kbd_keycode(tp->kbd, *input++);
  514. /* Emit keycode for AID byte. */
  515. kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
  516. raw3270_request_reset(rrq);
  517. xchg(&tp->read, rrq);
  518. raw3270_put_view(&tp->view);
  519. }
  520. /*
  521. * Read request completion callback.
  522. */
  523. static void
  524. tty3270_read_callback(struct raw3270_request *rq, void *data)
  525. {
  526. struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
  527. raw3270_get_view(rq->view);
  528. /* Schedule tasklet to pass input to tty. */
  529. tasklet_schedule(&tp->readlet);
  530. }
  531. /*
  532. * Issue a read request. Call with device lock.
  533. */
  534. static void
  535. tty3270_issue_read(struct tty3270 *tp, int lock)
  536. {
  537. struct raw3270_request *rrq;
  538. int rc;
  539. rrq = xchg(&tp->read, 0);
  540. if (!rrq)
  541. /* Read already scheduled. */
  542. return;
  543. rrq->callback = tty3270_read_callback;
  544. rrq->callback_data = tp;
  545. raw3270_request_set_cmd(rrq, TC_READMOD);
  546. raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
  547. /* Issue the read modified request. */
  548. if (lock) {
  549. rc = raw3270_start(&tp->view, rrq);
  550. } else
  551. rc = raw3270_start_irq(&tp->view, rrq);
  552. if (rc) {
  553. raw3270_request_reset(rrq);
  554. xchg(&tp->read, rrq);
  555. }
  556. }
  557. /*
  558. * Switch to the tty view.
  559. */
  560. static int
  561. tty3270_activate(struct raw3270_view *view)
  562. {
  563. struct tty3270 *tp = container_of(view, struct tty3270, view);
  564. tp->update_flags = TTY_UPDATE_ALL;
  565. tty3270_set_timer(tp, 1);
  566. return 0;
  567. }
  568. static void
  569. tty3270_deactivate(struct raw3270_view *view)
  570. {
  571. struct tty3270 *tp = container_of(view, struct tty3270, view);
  572. del_timer(&tp->timer);
  573. }
  574. static int
  575. tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
  576. {
  577. /* Handle ATTN. Schedule tasklet to read aid. */
  578. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  579. if (!tp->throttle)
  580. tty3270_issue_read(tp, 0);
  581. else
  582. tp->attn = 1;
  583. }
  584. if (rq) {
  585. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
  586. rq->rc = -EIO;
  587. else
  588. /* Normal end. Copy residual count. */
  589. rq->rescnt = irb->scsw.cmd.count;
  590. }
  591. return RAW3270_IO_DONE;
  592. }
  593. /*
  594. * Allocate tty3270 structure.
  595. */
  596. static struct tty3270 *
  597. tty3270_alloc_view(void)
  598. {
  599. struct tty3270 *tp;
  600. int pages;
  601. tp = kzalloc(sizeof(struct tty3270), GFP_KERNEL);
  602. if (!tp)
  603. goto out_err;
  604. tp->freemem_pages =
  605. kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL);
  606. if (!tp->freemem_pages)
  607. goto out_tp;
  608. INIT_LIST_HEAD(&tp->freemem);
  609. INIT_LIST_HEAD(&tp->lines);
  610. INIT_LIST_HEAD(&tp->update);
  611. INIT_LIST_HEAD(&tp->rcl_lines);
  612. tp->rcl_max = 20;
  613. tty_port_init(&tp->port);
  614. setup_timer(&tp->timer, (void (*)(unsigned long)) tty3270_update,
  615. (unsigned long) tp);
  616. tasklet_init(&tp->readlet,
  617. (void (*)(unsigned long)) tty3270_read_tasklet,
  618. (unsigned long) tp->read);
  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 = container_of(view, struct tty3270, view);
  716. struct tty_struct *tty = tty_port_tty_get(&tp->port);
  717. if (tty) {
  718. tty->driver_data = NULL;
  719. tty_port_tty_set(&tp->port, NULL);
  720. tty_hangup(tty);
  721. raw3270_put_view(&tp->view);
  722. tty_kref_put(tty);
  723. }
  724. }
  725. /*
  726. * Free tty3270 data structure
  727. */
  728. static void
  729. tty3270_free(struct raw3270_view *view)
  730. {
  731. struct tty3270 *tp = container_of(view, struct tty3270, view);
  732. tty3270_free_screen(tp);
  733. tty3270_free_view(tp);
  734. }
  735. /*
  736. * Delayed freeing of tty3270 views.
  737. */
  738. static void
  739. tty3270_del_views(void)
  740. {
  741. int i;
  742. for (i = 0; i < tty3270_max_index; i++) {
  743. struct raw3270_view *view =
  744. raw3270_find_view(&tty3270_fn, i + RAW3270_FIRSTMINOR);
  745. if (!IS_ERR(view))
  746. raw3270_del_view(view);
  747. }
  748. }
  749. static struct raw3270_fn tty3270_fn = {
  750. .activate = tty3270_activate,
  751. .deactivate = tty3270_deactivate,
  752. .intv = (void *) tty3270_irq,
  753. .release = tty3270_release,
  754. .free = tty3270_free
  755. };
  756. /*
  757. * This routine is called whenever a 3270 tty is opened first time.
  758. */
  759. static int tty3270_install(struct tty_driver *driver, struct tty_struct *tty)
  760. {
  761. struct raw3270_view *view;
  762. struct tty3270 *tp;
  763. int i, rc;
  764. /* Check if the tty3270 is already there. */
  765. view = raw3270_find_view(&tty3270_fn,
  766. tty->index + RAW3270_FIRSTMINOR);
  767. if (!IS_ERR(view)) {
  768. tp = container_of(view, struct tty3270, view);
  769. tty->driver_data = tp;
  770. tty->winsize.ws_row = tp->view.rows - 2;
  771. tty->winsize.ws_col = tp->view.cols;
  772. tty->low_latency = 0;
  773. /* why to reassign? */
  774. tty_port_tty_set(&tp->port, tty);
  775. tp->inattr = TF_INPUT;
  776. return tty_port_install(&tp->port, driver, tty);
  777. }
  778. if (tty3270_max_index < tty->index + 1)
  779. tty3270_max_index = tty->index + 1;
  780. /* Quick exit if there is no device for tty->index. */
  781. if (PTR_ERR(view) == -ENODEV)
  782. return -ENODEV;
  783. /* Allocate tty3270 structure on first open. */
  784. tp = tty3270_alloc_view();
  785. if (IS_ERR(tp))
  786. return PTR_ERR(tp);
  787. rc = raw3270_add_view(&tp->view, &tty3270_fn,
  788. tty->index + RAW3270_FIRSTMINOR);
  789. if (rc) {
  790. tty3270_free_view(tp);
  791. return rc;
  792. }
  793. rc = tty3270_alloc_screen(tp);
  794. if (rc) {
  795. raw3270_put_view(&tp->view);
  796. raw3270_del_view(&tp->view);
  797. return rc;
  798. }
  799. tty_port_tty_set(&tp->port, tty);
  800. tty->low_latency = 0;
  801. tty->winsize.ws_row = tp->view.rows - 2;
  802. tty->winsize.ws_col = tp->view.cols;
  803. tty3270_create_prompt(tp);
  804. tty3270_create_status(tp);
  805. tty3270_update_status(tp);
  806. /* Create blank line for every line in the tty output area. */
  807. for (i = 0; i < tp->view.rows - 2; i++)
  808. tty3270_blank_line(tp);
  809. tp->kbd->port = &tp->port;
  810. tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
  811. tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
  812. tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
  813. tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
  814. kbd_ascebc(tp->kbd, tp->view.ascebc);
  815. raw3270_activate_view(&tp->view);
  816. rc = tty_port_install(&tp->port, driver, tty);
  817. if (rc) {
  818. raw3270_put_view(&tp->view);
  819. return rc;
  820. }
  821. tty->driver_data = tp;
  822. return 0;
  823. }
  824. /*
  825. * This routine is called when the 3270 tty is closed. We wait
  826. * for the remaining request to be completed. Then we clean up.
  827. */
  828. static void
  829. tty3270_close(struct tty_struct *tty, struct file * filp)
  830. {
  831. struct tty3270 *tp = tty->driver_data;
  832. if (tty->count > 1)
  833. return;
  834. if (tp) {
  835. tty->driver_data = NULL;
  836. tty_port_tty_set(&tp->port, NULL);
  837. }
  838. }
  839. static void tty3270_cleanup(struct tty_struct *tty)
  840. {
  841. struct tty3270 *tp = tty->driver_data;
  842. if (tp)
  843. raw3270_put_view(&tp->view);
  844. }
  845. /*
  846. * We always have room.
  847. */
  848. static int
  849. tty3270_write_room(struct tty_struct *tty)
  850. {
  851. return INT_MAX;
  852. }
  853. /*
  854. * Insert character into the screen at the current position with the
  855. * current color and highlight. This function does NOT do cursor movement.
  856. */
  857. static void tty3270_put_character(struct tty3270 *tp, char ch)
  858. {
  859. struct tty3270_line *line;
  860. struct tty3270_cell *cell;
  861. line = tp->screen + tp->cy;
  862. if (line->len <= tp->cx) {
  863. while (line->len < tp->cx) {
  864. cell = line->cells + line->len;
  865. cell->character = tp->view.ascebc[' '];
  866. cell->highlight = tp->highlight;
  867. cell->f_color = tp->f_color;
  868. line->len++;
  869. }
  870. line->len++;
  871. }
  872. cell = line->cells + tp->cx;
  873. cell->character = tp->view.ascebc[(unsigned int) ch];
  874. cell->highlight = tp->highlight;
  875. cell->f_color = tp->f_color;
  876. }
  877. /*
  878. * Convert a tty3270_line to a 3270 data fragment usable for output.
  879. */
  880. static void
  881. tty3270_convert_line(struct tty3270 *tp, int line_nr)
  882. {
  883. struct tty3270_line *line;
  884. struct tty3270_cell *cell;
  885. struct string *s, *n;
  886. unsigned char highlight;
  887. unsigned char f_color;
  888. char *cp;
  889. int flen, i;
  890. /* Determine how long the fragment will be. */
  891. flen = 3; /* Prefix (TO_SBA). */
  892. line = tp->screen + line_nr;
  893. flen += line->len;
  894. highlight = TAX_RESET;
  895. f_color = TAC_RESET;
  896. for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
  897. if (cell->highlight != highlight) {
  898. flen += 3; /* TO_SA to switch highlight. */
  899. highlight = cell->highlight;
  900. }
  901. if (cell->f_color != f_color) {
  902. flen += 3; /* TO_SA to switch color. */
  903. f_color = cell->f_color;
  904. }
  905. }
  906. if (highlight != TAX_RESET)
  907. flen += 3; /* TO_SA to reset hightlight. */
  908. if (f_color != TAC_RESET)
  909. flen += 3; /* TO_SA to reset color. */
  910. if (line->len < tp->view.cols)
  911. flen += 4; /* Postfix (TO_RA). */
  912. /* Find the line in the list. */
  913. i = tp->view.rows - 2 - line_nr;
  914. list_for_each_entry_reverse(s, &tp->lines, list)
  915. if (--i <= 0)
  916. break;
  917. /*
  918. * Check if the line needs to get reallocated.
  919. */
  920. if (s->len != flen) {
  921. /* Reallocate string. */
  922. n = tty3270_alloc_string(tp, flen);
  923. list_add(&n->list, &s->list);
  924. list_del_init(&s->list);
  925. if (!list_empty(&s->update))
  926. list_del_init(&s->update);
  927. free_string(&tp->freemem, s);
  928. s = n;
  929. }
  930. /* Write 3270 data fragment. */
  931. cp = s->string;
  932. *cp++ = TO_SBA;
  933. *cp++ = 0;
  934. *cp++ = 0;
  935. highlight = TAX_RESET;
  936. f_color = TAC_RESET;
  937. for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
  938. if (cell->highlight != highlight) {
  939. *cp++ = TO_SA;
  940. *cp++ = TAT_EXTHI;
  941. *cp++ = cell->highlight;
  942. highlight = cell->highlight;
  943. }
  944. if (cell->f_color != f_color) {
  945. *cp++ = TO_SA;
  946. *cp++ = TAT_COLOR;
  947. *cp++ = cell->f_color;
  948. f_color = cell->f_color;
  949. }
  950. *cp++ = cell->character;
  951. }
  952. if (highlight != TAX_RESET) {
  953. *cp++ = TO_SA;
  954. *cp++ = TAT_EXTHI;
  955. *cp++ = TAX_RESET;
  956. }
  957. if (f_color != TAC_RESET) {
  958. *cp++ = TO_SA;
  959. *cp++ = TAT_COLOR;
  960. *cp++ = TAC_RESET;
  961. }
  962. if (line->len < tp->view.cols) {
  963. *cp++ = TO_RA;
  964. *cp++ = 0;
  965. *cp++ = 0;
  966. *cp++ = 0;
  967. }
  968. if (tp->nr_up + line_nr < tp->view.rows - 2) {
  969. /* Line is currently visible on screen. */
  970. tty3270_update_string(tp, s, line_nr);
  971. /* Add line to update list. */
  972. if (list_empty(&s->update)) {
  973. list_add_tail(&s->update, &tp->update);
  974. tp->update_flags |= TTY_UPDATE_LIST;
  975. }
  976. }
  977. }
  978. /*
  979. * Do carriage return.
  980. */
  981. static void
  982. tty3270_cr(struct tty3270 *tp)
  983. {
  984. tp->cx = 0;
  985. }
  986. /*
  987. * Do line feed.
  988. */
  989. static void
  990. tty3270_lf(struct tty3270 *tp)
  991. {
  992. struct tty3270_line temp;
  993. int i;
  994. tty3270_convert_line(tp, tp->cy);
  995. if (tp->cy < tp->view.rows - 3) {
  996. tp->cy++;
  997. return;
  998. }
  999. /* Last line just filled up. Add new, blank line. */
  1000. tty3270_blank_line(tp);
  1001. temp = tp->screen[0];
  1002. temp.len = 0;
  1003. for (i = 0; i < tp->view.rows - 3; i++)
  1004. tp->screen[i] = tp->screen[i+1];
  1005. tp->screen[tp->view.rows - 3] = temp;
  1006. tty3270_rebuild_update(tp);
  1007. }
  1008. static void
  1009. tty3270_ri(struct tty3270 *tp)
  1010. {
  1011. if (tp->cy > 0) {
  1012. tty3270_convert_line(tp, tp->cy);
  1013. tp->cy--;
  1014. }
  1015. }
  1016. /*
  1017. * Insert characters at current position.
  1018. */
  1019. static void
  1020. tty3270_insert_characters(struct tty3270 *tp, int n)
  1021. {
  1022. struct tty3270_line *line;
  1023. int k;
  1024. line = tp->screen + tp->cy;
  1025. while (line->len < tp->cx) {
  1026. line->cells[line->len].character = tp->view.ascebc[' '];
  1027. line->cells[line->len].highlight = TAX_RESET;
  1028. line->cells[line->len].f_color = TAC_RESET;
  1029. line->len++;
  1030. }
  1031. if (n > tp->view.cols - tp->cx)
  1032. n = tp->view.cols - tp->cx;
  1033. k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
  1034. while (k--)
  1035. line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
  1036. line->len += n;
  1037. if (line->len > tp->view.cols)
  1038. line->len = tp->view.cols;
  1039. while (n-- > 0) {
  1040. line->cells[tp->cx + n].character = tp->view.ascebc[' '];
  1041. line->cells[tp->cx + n].highlight = tp->highlight;
  1042. line->cells[tp->cx + n].f_color = tp->f_color;
  1043. }
  1044. }
  1045. /*
  1046. * Delete characters at current position.
  1047. */
  1048. static void
  1049. tty3270_delete_characters(struct tty3270 *tp, int n)
  1050. {
  1051. struct tty3270_line *line;
  1052. int i;
  1053. line = tp->screen + tp->cy;
  1054. if (line->len <= tp->cx)
  1055. return;
  1056. if (line->len - tp->cx <= n) {
  1057. line->len = tp->cx;
  1058. return;
  1059. }
  1060. for (i = tp->cx; i + n < line->len; i++)
  1061. line->cells[i] = line->cells[i + n];
  1062. line->len -= n;
  1063. }
  1064. /*
  1065. * Erase characters at current position.
  1066. */
  1067. static void
  1068. tty3270_erase_characters(struct tty3270 *tp, int n)
  1069. {
  1070. struct tty3270_line *line;
  1071. struct tty3270_cell *cell;
  1072. line = tp->screen + tp->cy;
  1073. while (line->len > tp->cx && n-- > 0) {
  1074. cell = line->cells + tp->cx++;
  1075. cell->character = ' ';
  1076. cell->highlight = TAX_RESET;
  1077. cell->f_color = TAC_RESET;
  1078. }
  1079. tp->cx += n;
  1080. tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
  1081. }
  1082. /*
  1083. * Erase line, 3 different cases:
  1084. * Esc [ 0 K Erase from current position to end of line inclusive
  1085. * Esc [ 1 K Erase from beginning of line to current position inclusive
  1086. * Esc [ 2 K Erase entire line (without moving cursor)
  1087. */
  1088. static void
  1089. tty3270_erase_line(struct tty3270 *tp, int mode)
  1090. {
  1091. struct tty3270_line *line;
  1092. struct tty3270_cell *cell;
  1093. int i;
  1094. line = tp->screen + tp->cy;
  1095. if (mode == 0)
  1096. line->len = tp->cx;
  1097. else if (mode == 1) {
  1098. for (i = 0; i < tp->cx; i++) {
  1099. cell = line->cells + i;
  1100. cell->character = ' ';
  1101. cell->highlight = TAX_RESET;
  1102. cell->f_color = TAC_RESET;
  1103. }
  1104. if (line->len <= tp->cx)
  1105. line->len = tp->cx + 1;
  1106. } else if (mode == 2)
  1107. line->len = 0;
  1108. tty3270_convert_line(tp, tp->cy);
  1109. }
  1110. /*
  1111. * Erase display, 3 different cases:
  1112. * Esc [ 0 J Erase from current position to bottom of screen inclusive
  1113. * Esc [ 1 J Erase from top of screen to current position inclusive
  1114. * Esc [ 2 J Erase entire screen (without moving the cursor)
  1115. */
  1116. static void
  1117. tty3270_erase_display(struct tty3270 *tp, int mode)
  1118. {
  1119. int i;
  1120. if (mode == 0) {
  1121. tty3270_erase_line(tp, 0);
  1122. for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
  1123. tp->screen[i].len = 0;
  1124. tty3270_convert_line(tp, i);
  1125. }
  1126. } else if (mode == 1) {
  1127. for (i = 0; i < tp->cy; i++) {
  1128. tp->screen[i].len = 0;
  1129. tty3270_convert_line(tp, i);
  1130. }
  1131. tty3270_erase_line(tp, 1);
  1132. } else if (mode == 2) {
  1133. for (i = 0; i < tp->view.rows - 2; i++) {
  1134. tp->screen[i].len = 0;
  1135. tty3270_convert_line(tp, i);
  1136. }
  1137. }
  1138. tty3270_rebuild_update(tp);
  1139. }
  1140. /*
  1141. * Set attributes found in an escape sequence.
  1142. * Esc [ <attr> ; <attr> ; ... m
  1143. */
  1144. static void
  1145. tty3270_set_attributes(struct tty3270 *tp)
  1146. {
  1147. static unsigned char f_colors[] = {
  1148. TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
  1149. TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
  1150. };
  1151. int i, attr;
  1152. for (i = 0; i <= tp->esc_npar; i++) {
  1153. attr = tp->esc_par[i];
  1154. switch (attr) {
  1155. case 0: /* Reset */
  1156. tp->highlight = TAX_RESET;
  1157. tp->f_color = TAC_RESET;
  1158. break;
  1159. /* Highlight. */
  1160. case 4: /* Start underlining. */
  1161. tp->highlight = TAX_UNDER;
  1162. break;
  1163. case 5: /* Start blink. */
  1164. tp->highlight = TAX_BLINK;
  1165. break;
  1166. case 7: /* Start reverse. */
  1167. tp->highlight = TAX_REVER;
  1168. break;
  1169. case 24: /* End underlining */
  1170. if (tp->highlight == TAX_UNDER)
  1171. tp->highlight = TAX_RESET;
  1172. break;
  1173. case 25: /* End blink. */
  1174. if (tp->highlight == TAX_BLINK)
  1175. tp->highlight = TAX_RESET;
  1176. break;
  1177. case 27: /* End reverse. */
  1178. if (tp->highlight == TAX_REVER)
  1179. tp->highlight = TAX_RESET;
  1180. break;
  1181. /* Foreground color. */
  1182. case 30: /* Black */
  1183. case 31: /* Red */
  1184. case 32: /* Green */
  1185. case 33: /* Yellow */
  1186. case 34: /* Blue */
  1187. case 35: /* Magenta */
  1188. case 36: /* Cyan */
  1189. case 37: /* White */
  1190. case 39: /* Black */
  1191. tp->f_color = f_colors[attr - 30];
  1192. break;
  1193. }
  1194. }
  1195. }
  1196. static inline int
  1197. tty3270_getpar(struct tty3270 *tp, int ix)
  1198. {
  1199. return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
  1200. }
  1201. static void
  1202. tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
  1203. {
  1204. int max_cx = max(0, cx);
  1205. int max_cy = max(0, cy);
  1206. tp->cx = min_t(int, tp->view.cols - 1, max_cx);
  1207. cy = min_t(int, tp->view.rows - 3, max_cy);
  1208. if (cy != tp->cy) {
  1209. tty3270_convert_line(tp, tp->cy);
  1210. tp->cy = cy;
  1211. }
  1212. }
  1213. /*
  1214. * Process escape sequences. Known sequences:
  1215. * Esc 7 Save Cursor Position
  1216. * Esc 8 Restore Cursor Position
  1217. * Esc [ Pn ; Pn ; .. m Set attributes
  1218. * Esc [ Pn ; Pn H Cursor Position
  1219. * Esc [ Pn ; Pn f Cursor Position
  1220. * Esc [ Pn A Cursor Up
  1221. * Esc [ Pn B Cursor Down
  1222. * Esc [ Pn C Cursor Forward
  1223. * Esc [ Pn D Cursor Backward
  1224. * Esc [ Pn G Cursor Horizontal Absolute
  1225. * Esc [ Pn X Erase Characters
  1226. * Esc [ Ps J Erase in Display
  1227. * Esc [ Ps K Erase in Line
  1228. * // FIXME: add all the new ones.
  1229. *
  1230. * Pn is a numeric parameter, a string of zero or more decimal digits.
  1231. * Ps is a selective parameter.
  1232. */
  1233. static void
  1234. tty3270_escape_sequence(struct tty3270 *tp, char ch)
  1235. {
  1236. enum { ESnormal, ESesc, ESsquare, ESgetpars };
  1237. if (tp->esc_state == ESnormal) {
  1238. if (ch == 0x1b)
  1239. /* Starting new escape sequence. */
  1240. tp->esc_state = ESesc;
  1241. return;
  1242. }
  1243. if (tp->esc_state == ESesc) {
  1244. tp->esc_state = ESnormal;
  1245. switch (ch) {
  1246. case '[':
  1247. tp->esc_state = ESsquare;
  1248. break;
  1249. case 'E':
  1250. tty3270_cr(tp);
  1251. tty3270_lf(tp);
  1252. break;
  1253. case 'M':
  1254. tty3270_ri(tp);
  1255. break;
  1256. case 'D':
  1257. tty3270_lf(tp);
  1258. break;
  1259. case 'Z': /* Respond ID. */
  1260. kbd_puts_queue(&tp->port, "\033[?6c");
  1261. break;
  1262. case '7': /* Save cursor position. */
  1263. tp->saved_cx = tp->cx;
  1264. tp->saved_cy = tp->cy;
  1265. tp->saved_highlight = tp->highlight;
  1266. tp->saved_f_color = tp->f_color;
  1267. break;
  1268. case '8': /* Restore cursor position. */
  1269. tty3270_convert_line(tp, tp->cy);
  1270. tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
  1271. tp->highlight = tp->saved_highlight;
  1272. tp->f_color = tp->saved_f_color;
  1273. break;
  1274. case 'c': /* Reset terminal. */
  1275. tp->cx = tp->saved_cx = 0;
  1276. tp->cy = tp->saved_cy = 0;
  1277. tp->highlight = tp->saved_highlight = TAX_RESET;
  1278. tp->f_color = tp->saved_f_color = TAC_RESET;
  1279. tty3270_erase_display(tp, 2);
  1280. break;
  1281. }
  1282. return;
  1283. }
  1284. if (tp->esc_state == ESsquare) {
  1285. tp->esc_state = ESgetpars;
  1286. memset(tp->esc_par, 0, sizeof(tp->esc_par));
  1287. tp->esc_npar = 0;
  1288. tp->esc_ques = (ch == '?');
  1289. if (tp->esc_ques)
  1290. return;
  1291. }
  1292. if (tp->esc_state == ESgetpars) {
  1293. if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
  1294. tp->esc_npar++;
  1295. return;
  1296. }
  1297. if (ch >= '0' && ch <= '9') {
  1298. tp->esc_par[tp->esc_npar] *= 10;
  1299. tp->esc_par[tp->esc_npar] += ch - '0';
  1300. return;
  1301. }
  1302. }
  1303. tp->esc_state = ESnormal;
  1304. if (ch == 'n' && !tp->esc_ques) {
  1305. if (tp->esc_par[0] == 5) /* Status report. */
  1306. kbd_puts_queue(&tp->port, "\033[0n");
  1307. else if (tp->esc_par[0] == 6) { /* Cursor report. */
  1308. char buf[40];
  1309. sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
  1310. kbd_puts_queue(&tp->port, buf);
  1311. }
  1312. return;
  1313. }
  1314. if (tp->esc_ques)
  1315. return;
  1316. switch (ch) {
  1317. case 'm':
  1318. tty3270_set_attributes(tp);
  1319. break;
  1320. case 'H': /* Set cursor position. */
  1321. case 'f':
  1322. tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
  1323. tty3270_getpar(tp, 0) - 1);
  1324. break;
  1325. case 'd': /* Set y position. */
  1326. tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
  1327. break;
  1328. case 'A': /* Cursor up. */
  1329. case 'F':
  1330. tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
  1331. break;
  1332. case 'B': /* Cursor down. */
  1333. case 'e':
  1334. case 'E':
  1335. tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
  1336. break;
  1337. case 'C': /* Cursor forward. */
  1338. case 'a':
  1339. tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
  1340. break;
  1341. case 'D': /* Cursor backward. */
  1342. tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
  1343. break;
  1344. case 'G': /* Set x position. */
  1345. case '`':
  1346. tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
  1347. break;
  1348. case 'X': /* Erase Characters. */
  1349. tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
  1350. break;
  1351. case 'J': /* Erase display. */
  1352. tty3270_erase_display(tp, tp->esc_par[0]);
  1353. break;
  1354. case 'K': /* Erase line. */
  1355. tty3270_erase_line(tp, tp->esc_par[0]);
  1356. break;
  1357. case 'P': /* Delete characters. */
  1358. tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
  1359. break;
  1360. case '@': /* Insert characters. */
  1361. tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
  1362. break;
  1363. case 's': /* Save cursor position. */
  1364. tp->saved_cx = tp->cx;
  1365. tp->saved_cy = tp->cy;
  1366. tp->saved_highlight = tp->highlight;
  1367. tp->saved_f_color = tp->f_color;
  1368. break;
  1369. case 'u': /* Restore cursor position. */
  1370. tty3270_convert_line(tp, tp->cy);
  1371. tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
  1372. tp->highlight = tp->saved_highlight;
  1373. tp->f_color = tp->saved_f_color;
  1374. break;
  1375. }
  1376. }
  1377. /*
  1378. * String write routine for 3270 ttys
  1379. */
  1380. static void
  1381. tty3270_do_write(struct tty3270 *tp, struct tty_struct *tty,
  1382. const unsigned char *buf, int count)
  1383. {
  1384. int i_msg, i;
  1385. spin_lock_bh(&tp->view.lock);
  1386. for (i_msg = 0; !tty->stopped && i_msg < count; i_msg++) {
  1387. if (tp->esc_state != 0) {
  1388. /* Continue escape sequence. */
  1389. tty3270_escape_sequence(tp, buf[i_msg]);
  1390. continue;
  1391. }
  1392. switch (buf[i_msg]) {
  1393. case 0x07: /* '\a' -- Alarm */
  1394. tp->wcc |= TW_PLUSALARM;
  1395. break;
  1396. case 0x08: /* Backspace. */
  1397. if (tp->cx > 0) {
  1398. tp->cx--;
  1399. tty3270_put_character(tp, ' ');
  1400. }
  1401. break;
  1402. case 0x09: /* '\t' -- Tabulate */
  1403. for (i = tp->cx % 8; i < 8; i++) {
  1404. if (tp->cx >= tp->view.cols) {
  1405. tty3270_cr(tp);
  1406. tty3270_lf(tp);
  1407. break;
  1408. }
  1409. tty3270_put_character(tp, ' ');
  1410. tp->cx++;
  1411. }
  1412. break;
  1413. case 0x0a: /* '\n' -- New Line */
  1414. tty3270_cr(tp);
  1415. tty3270_lf(tp);
  1416. break;
  1417. case 0x0c: /* '\f' -- Form Feed */
  1418. tty3270_erase_display(tp, 2);
  1419. tp->cx = tp->cy = 0;
  1420. break;
  1421. case 0x0d: /* '\r' -- Carriage Return */
  1422. tp->cx = 0;
  1423. break;
  1424. case 0x0f: /* SuSE "exit alternate mode" */
  1425. break;
  1426. case 0x1b: /* Start escape sequence. */
  1427. tty3270_escape_sequence(tp, buf[i_msg]);
  1428. break;
  1429. default: /* Insert normal character. */
  1430. if (tp->cx >= tp->view.cols) {
  1431. tty3270_cr(tp);
  1432. tty3270_lf(tp);
  1433. }
  1434. tty3270_put_character(tp, buf[i_msg]);
  1435. tp->cx++;
  1436. break;
  1437. }
  1438. }
  1439. /* Convert current line to 3270 data fragment. */
  1440. tty3270_convert_line(tp, tp->cy);
  1441. /* Setup timer to update display after 1/10 second */
  1442. if (!timer_pending(&tp->timer))
  1443. tty3270_set_timer(tp, HZ/10);
  1444. spin_unlock_bh(&tp->view.lock);
  1445. }
  1446. /*
  1447. * String write routine for 3270 ttys
  1448. */
  1449. static int
  1450. tty3270_write(struct tty_struct * tty,
  1451. const unsigned char *buf, int count)
  1452. {
  1453. struct tty3270 *tp;
  1454. tp = tty->driver_data;
  1455. if (!tp)
  1456. return 0;
  1457. if (tp->char_count > 0) {
  1458. tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
  1459. tp->char_count = 0;
  1460. }
  1461. tty3270_do_write(tp, tty, buf, count);
  1462. return count;
  1463. }
  1464. /*
  1465. * Put single characters to the ttys character buffer
  1466. */
  1467. static int tty3270_put_char(struct tty_struct *tty, unsigned char ch)
  1468. {
  1469. struct tty3270 *tp;
  1470. tp = tty->driver_data;
  1471. if (!tp || tp->char_count >= TTY3270_CHAR_BUF_SIZE)
  1472. return 0;
  1473. tp->char_buf[tp->char_count++] = ch;
  1474. return 1;
  1475. }
  1476. /*
  1477. * Flush all characters from the ttys characeter buffer put there
  1478. * by tty3270_put_char.
  1479. */
  1480. static void
  1481. tty3270_flush_chars(struct tty_struct *tty)
  1482. {
  1483. struct tty3270 *tp;
  1484. tp = tty->driver_data;
  1485. if (!tp)
  1486. return;
  1487. if (tp->char_count > 0) {
  1488. tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
  1489. tp->char_count = 0;
  1490. }
  1491. }
  1492. /*
  1493. * Returns the number of characters in the output buffer. This is
  1494. * used in tty_wait_until_sent to wait until all characters have
  1495. * appeared on the screen.
  1496. */
  1497. static int
  1498. tty3270_chars_in_buffer(struct tty_struct *tty)
  1499. {
  1500. return 0;
  1501. }
  1502. static void
  1503. tty3270_flush_buffer(struct tty_struct *tty)
  1504. {
  1505. }
  1506. /*
  1507. * Check for visible/invisible input switches
  1508. */
  1509. static void
  1510. tty3270_set_termios(struct tty_struct *tty, struct ktermios *old)
  1511. {
  1512. struct tty3270 *tp;
  1513. int new;
  1514. tp = tty->driver_data;
  1515. if (!tp)
  1516. return;
  1517. spin_lock_bh(&tp->view.lock);
  1518. if (L_ICANON(tty)) {
  1519. new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
  1520. if (new != tp->inattr) {
  1521. tp->inattr = new;
  1522. tty3270_update_prompt(tp, NULL, 0);
  1523. tty3270_set_timer(tp, 1);
  1524. }
  1525. }
  1526. spin_unlock_bh(&tp->view.lock);
  1527. }
  1528. /*
  1529. * Disable reading from a 3270 tty
  1530. */
  1531. static void
  1532. tty3270_throttle(struct tty_struct * tty)
  1533. {
  1534. struct tty3270 *tp;
  1535. tp = tty->driver_data;
  1536. if (!tp)
  1537. return;
  1538. tp->throttle = 1;
  1539. }
  1540. /*
  1541. * Enable reading from a 3270 tty
  1542. */
  1543. static void
  1544. tty3270_unthrottle(struct tty_struct * tty)
  1545. {
  1546. struct tty3270 *tp;
  1547. tp = tty->driver_data;
  1548. if (!tp)
  1549. return;
  1550. tp->throttle = 0;
  1551. if (tp->attn)
  1552. tty3270_issue_read(tp, 1);
  1553. }
  1554. /*
  1555. * Hang up the tty device.
  1556. */
  1557. static void
  1558. tty3270_hangup(struct tty_struct *tty)
  1559. {
  1560. // FIXME: implement
  1561. }
  1562. static void
  1563. tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
  1564. {
  1565. }
  1566. static int tty3270_ioctl(struct tty_struct *tty, unsigned int cmd,
  1567. unsigned long arg)
  1568. {
  1569. struct tty3270 *tp;
  1570. tp = tty->driver_data;
  1571. if (!tp)
  1572. return -ENODEV;
  1573. if (tty->flags & (1 << TTY_IO_ERROR))
  1574. return -EIO;
  1575. return kbd_ioctl(tp->kbd, cmd, arg);
  1576. }
  1577. #ifdef CONFIG_COMPAT
  1578. static long tty3270_compat_ioctl(struct tty_struct *tty,
  1579. unsigned int cmd, unsigned long arg)
  1580. {
  1581. struct tty3270 *tp;
  1582. tp = tty->driver_data;
  1583. if (!tp)
  1584. return -ENODEV;
  1585. if (tty->flags & (1 << TTY_IO_ERROR))
  1586. return -EIO;
  1587. return kbd_ioctl(tp->kbd, cmd, (unsigned long)compat_ptr(arg));
  1588. }
  1589. #endif
  1590. static const struct tty_operations tty3270_ops = {
  1591. .install = tty3270_install,
  1592. .cleanup = tty3270_cleanup,
  1593. .close = tty3270_close,
  1594. .write = tty3270_write,
  1595. .put_char = tty3270_put_char,
  1596. .flush_chars = tty3270_flush_chars,
  1597. .write_room = tty3270_write_room,
  1598. .chars_in_buffer = tty3270_chars_in_buffer,
  1599. .flush_buffer = tty3270_flush_buffer,
  1600. .throttle = tty3270_throttle,
  1601. .unthrottle = tty3270_unthrottle,
  1602. .hangup = tty3270_hangup,
  1603. .wait_until_sent = tty3270_wait_until_sent,
  1604. .ioctl = tty3270_ioctl,
  1605. #ifdef CONFIG_COMPAT
  1606. .compat_ioctl = tty3270_compat_ioctl,
  1607. #endif
  1608. .set_termios = tty3270_set_termios
  1609. };
  1610. /*
  1611. * 3270 tty registration code called from tty_init().
  1612. * Most kernel services (incl. kmalloc) are available at this poimt.
  1613. */
  1614. static int __init tty3270_init(void)
  1615. {
  1616. struct tty_driver *driver;
  1617. int ret;
  1618. driver = alloc_tty_driver(RAW3270_MAXDEVS);
  1619. if (!driver)
  1620. return -ENOMEM;
  1621. /*
  1622. * Initialize the tty_driver structure
  1623. * Entries in tty3270_driver that are NOT initialized:
  1624. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1625. */
  1626. driver->driver_name = "ttyTUB";
  1627. driver->name = "ttyTUB";
  1628. driver->major = IBM_TTY3270_MAJOR;
  1629. driver->minor_start = RAW3270_FIRSTMINOR;
  1630. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1631. driver->subtype = SYSTEM_TYPE_TTY;
  1632. driver->init_termios = tty_std_termios;
  1633. driver->flags = TTY_DRIVER_RESET_TERMIOS;
  1634. tty_set_operations(driver, &tty3270_ops);
  1635. ret = tty_register_driver(driver);
  1636. if (ret) {
  1637. put_tty_driver(driver);
  1638. return ret;
  1639. }
  1640. tty3270_driver = driver;
  1641. return 0;
  1642. }
  1643. static void __exit
  1644. tty3270_exit(void)
  1645. {
  1646. struct tty_driver *driver;
  1647. driver = tty3270_driver;
  1648. tty3270_driver = NULL;
  1649. tty_unregister_driver(driver);
  1650. put_tty_driver(driver);
  1651. tty3270_del_views();
  1652. }
  1653. MODULE_LICENSE("GPL");
  1654. MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
  1655. module_init(tty3270_init);
  1656. module_exit(tty3270_exit);