tty3270.c 42 KB

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