tty3270.c 42 KB

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