tty3270.c 42 KB

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