tty3270.c 42 KB

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