tty3270.c 42 KB

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