tty3270.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  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_struct *tty; /* Pointer to tty structure */
  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;
  403. struct string *s;
  404. tp = kbd->tty->driver_data;
  405. spin_lock_bh(&tp->view.lock);
  406. if (tp->inattr == TF_INPUT) {
  407. if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
  408. tp->rcl_walk = tp->rcl_walk->prev;
  409. else if (!list_empty(&tp->rcl_lines))
  410. tp->rcl_walk = tp->rcl_lines.prev;
  411. s = tp->rcl_walk ?
  412. list_entry(tp->rcl_walk, struct string, list) : NULL;
  413. if (tp->rcl_walk) {
  414. s = list_entry(tp->rcl_walk, struct string, list);
  415. tty3270_update_prompt(tp, s->string, s->len);
  416. } else
  417. tty3270_update_prompt(tp, NULL, 0);
  418. tty3270_set_timer(tp, 1);
  419. }
  420. spin_unlock_bh(&tp->view.lock);
  421. }
  422. /*
  423. * Deactivate tty view.
  424. */
  425. static void
  426. tty3270_exit_tty(struct kbd_data *kbd)
  427. {
  428. struct tty3270 *tp;
  429. tp = kbd->tty->driver_data;
  430. raw3270_deactivate_view(&tp->view);
  431. }
  432. /*
  433. * Scroll forward in history.
  434. */
  435. static void
  436. tty3270_scroll_forward(struct kbd_data *kbd)
  437. {
  438. struct tty3270 *tp;
  439. int nr_up;
  440. tp = kbd->tty->driver_data;
  441. spin_lock_bh(&tp->view.lock);
  442. nr_up = tp->nr_up - tp->view.rows + 2;
  443. if (nr_up < 0)
  444. nr_up = 0;
  445. if (nr_up != tp->nr_up) {
  446. tp->nr_up = nr_up;
  447. tty3270_rebuild_update(tp);
  448. tty3270_update_status(tp);
  449. tty3270_set_timer(tp, 1);
  450. }
  451. spin_unlock_bh(&tp->view.lock);
  452. }
  453. /*
  454. * Scroll backward in history.
  455. */
  456. static void
  457. tty3270_scroll_backward(struct kbd_data *kbd)
  458. {
  459. struct tty3270 *tp;
  460. int nr_up;
  461. tp = kbd->tty->driver_data;
  462. spin_lock_bh(&tp->view.lock);
  463. nr_up = tp->nr_up + tp->view.rows - 2;
  464. if (nr_up + tp->view.rows - 2 > tp->nr_lines)
  465. nr_up = tp->nr_lines - tp->view.rows + 2;
  466. if (nr_up != tp->nr_up) {
  467. tp->nr_up = nr_up;
  468. tty3270_rebuild_update(tp);
  469. tty3270_update_status(tp);
  470. tty3270_set_timer(tp, 1);
  471. }
  472. spin_unlock_bh(&tp->view.lock);
  473. }
  474. /*
  475. * Pass input line to tty.
  476. */
  477. static void
  478. tty3270_read_tasklet(struct raw3270_request *rrq)
  479. {
  480. static char kreset_data = TW_KR;
  481. struct tty3270 *tp = container_of(rrq->view, struct tty3270, view);
  482. char *input;
  483. int len;
  484. spin_lock_bh(&tp->view.lock);
  485. /*
  486. * Two AID keys are special: For 0x7d (enter) the input line
  487. * has to be emitted to the tty and for 0x6d the screen
  488. * needs to be redrawn.
  489. */
  490. input = NULL;
  491. len = 0;
  492. if (tp->input->string[0] == 0x7d) {
  493. /* Enter: write input to tty. */
  494. input = tp->input->string + 6;
  495. len = tp->input->len - 6 - rrq->rescnt;
  496. if (tp->inattr != TF_INPUTN)
  497. tty3270_rcl_add(tp, input, len);
  498. if (tp->nr_up > 0) {
  499. tp->nr_up = 0;
  500. tty3270_rebuild_update(tp);
  501. tty3270_update_status(tp);
  502. }
  503. /* Clear input area. */
  504. tty3270_update_prompt(tp, NULL, 0);
  505. tty3270_set_timer(tp, 1);
  506. } else if (tp->input->string[0] == 0x6d) {
  507. /* Display has been cleared. Redraw. */
  508. tp->update_flags = TTY_UPDATE_ALL;
  509. tty3270_set_timer(tp, 1);
  510. }
  511. spin_unlock_bh(&tp->view.lock);
  512. /* Start keyboard reset command. */
  513. raw3270_request_reset(tp->kreset);
  514. raw3270_request_set_cmd(tp->kreset, TC_WRITE);
  515. raw3270_request_add_data(tp->kreset, &kreset_data, 1);
  516. raw3270_start(&tp->view, tp->kreset);
  517. /* Emit input string. */
  518. if (tp->tty) {
  519. while (len-- > 0)
  520. kbd_keycode(tp->kbd, *input++);
  521. /* Emit keycode for AID byte. */
  522. kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
  523. }
  524. raw3270_request_reset(rrq);
  525. xchg(&tp->read, rrq);
  526. raw3270_put_view(&tp->view);
  527. }
  528. /*
  529. * Read request completion callback.
  530. */
  531. static void
  532. tty3270_read_callback(struct raw3270_request *rq, void *data)
  533. {
  534. struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
  535. raw3270_get_view(rq->view);
  536. /* Schedule tasklet to pass input to tty. */
  537. tasklet_schedule(&tp->readlet);
  538. }
  539. /*
  540. * Issue a read request. Call with device lock.
  541. */
  542. static void
  543. tty3270_issue_read(struct tty3270 *tp, int lock)
  544. {
  545. struct raw3270_request *rrq;
  546. int rc;
  547. rrq = xchg(&tp->read, 0);
  548. if (!rrq)
  549. /* Read already scheduled. */
  550. return;
  551. rrq->callback = tty3270_read_callback;
  552. rrq->callback_data = tp;
  553. raw3270_request_set_cmd(rrq, TC_READMOD);
  554. raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
  555. /* Issue the read modified request. */
  556. if (lock) {
  557. rc = raw3270_start(&tp->view, rrq);
  558. } else
  559. rc = raw3270_start_irq(&tp->view, rrq);
  560. if (rc) {
  561. raw3270_request_reset(rrq);
  562. xchg(&tp->read, rrq);
  563. }
  564. }
  565. /*
  566. * Switch to the tty view.
  567. */
  568. static int
  569. tty3270_activate(struct raw3270_view *view)
  570. {
  571. struct tty3270 *tp = container_of(view, struct tty3270, view);
  572. tp->update_flags = TTY_UPDATE_ALL;
  573. tty3270_set_timer(tp, 1);
  574. return 0;
  575. }
  576. static void
  577. tty3270_deactivate(struct raw3270_view *view)
  578. {
  579. struct tty3270 *tp = container_of(view, struct tty3270, view);
  580. del_timer(&tp->timer);
  581. }
  582. static int
  583. tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
  584. {
  585. /* Handle ATTN. Schedule tasklet to read aid. */
  586. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  587. if (!tp->throttle)
  588. tty3270_issue_read(tp, 0);
  589. else
  590. tp->attn = 1;
  591. }
  592. if (rq) {
  593. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
  594. rq->rc = -EIO;
  595. else
  596. /* Normal end. Copy residual count. */
  597. rq->rescnt = irb->scsw.cmd.count;
  598. }
  599. return RAW3270_IO_DONE;
  600. }
  601. /*
  602. * Allocate tty3270 structure.
  603. */
  604. static struct tty3270 *
  605. tty3270_alloc_view(void)
  606. {
  607. struct tty3270 *tp;
  608. int pages;
  609. tp = kzalloc(sizeof(struct tty3270), GFP_KERNEL);
  610. if (!tp)
  611. goto out_err;
  612. tp->freemem_pages =
  613. kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL);
  614. if (!tp->freemem_pages)
  615. goto out_tp;
  616. INIT_LIST_HEAD(&tp->freemem);
  617. INIT_LIST_HEAD(&tp->lines);
  618. INIT_LIST_HEAD(&tp->update);
  619. INIT_LIST_HEAD(&tp->rcl_lines);
  620. tp->rcl_max = 20;
  621. setup_timer(&tp->timer, (void (*)(unsigned long)) tty3270_update,
  622. (unsigned long) tp);
  623. tasklet_init(&tp->readlet,
  624. (void (*)(unsigned long)) tty3270_read_tasklet,
  625. (unsigned long) tp->read);
  626. for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
  627. tp->freemem_pages[pages] = (void *)
  628. __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
  629. if (!tp->freemem_pages[pages])
  630. goto out_pages;
  631. add_string_memory(&tp->freemem,
  632. tp->freemem_pages[pages], PAGE_SIZE);
  633. }
  634. tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
  635. if (IS_ERR(tp->write))
  636. goto out_pages;
  637. tp->read = raw3270_request_alloc(0);
  638. if (IS_ERR(tp->read))
  639. goto out_write;
  640. tp->kreset = raw3270_request_alloc(1);
  641. if (IS_ERR(tp->kreset))
  642. goto out_read;
  643. tp->kbd = kbd_alloc();
  644. if (!tp->kbd)
  645. goto out_reset;
  646. return tp;
  647. out_reset:
  648. raw3270_request_free(tp->kreset);
  649. out_read:
  650. raw3270_request_free(tp->read);
  651. out_write:
  652. raw3270_request_free(tp->write);
  653. out_pages:
  654. while (pages--)
  655. free_pages((unsigned long) tp->freemem_pages[pages], 0);
  656. kfree(tp->freemem_pages);
  657. out_tp:
  658. kfree(tp);
  659. out_err:
  660. return ERR_PTR(-ENOMEM);
  661. }
  662. /*
  663. * Free tty3270 structure.
  664. */
  665. static void
  666. tty3270_free_view(struct tty3270 *tp)
  667. {
  668. int pages;
  669. del_timer_sync(&tp->timer);
  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 = container_of(view, struct tty3270, view);
  723. struct tty_struct *tty;
  724. tty = tp->tty;
  725. if (tty) {
  726. tty->driver_data = NULL;
  727. tp->tty = tp->kbd->tty = NULL;
  728. tty_hangup(tty);
  729. raw3270_put_view(&tp->view);
  730. }
  731. }
  732. /*
  733. * Free tty3270 data structure
  734. */
  735. static void
  736. tty3270_free(struct raw3270_view *view)
  737. {
  738. struct tty3270 *tp = container_of(view, struct tty3270, view);
  739. tty3270_free_screen(tp);
  740. tty3270_free_view(tp);
  741. }
  742. /*
  743. * Delayed freeing of tty3270 views.
  744. */
  745. static void
  746. tty3270_del_views(void)
  747. {
  748. int i;
  749. for (i = 0; i < tty3270_max_index; i++) {
  750. struct raw3270_view *view =
  751. raw3270_find_view(&tty3270_fn, i + RAW3270_FIRSTMINOR);
  752. if (!IS_ERR(view))
  753. raw3270_del_view(view);
  754. }
  755. }
  756. static struct raw3270_fn tty3270_fn = {
  757. .activate = tty3270_activate,
  758. .deactivate = tty3270_deactivate,
  759. .intv = (void *) tty3270_irq,
  760. .release = tty3270_release,
  761. .free = tty3270_free
  762. };
  763. /*
  764. * This routine is called whenever a 3270 tty is opened.
  765. */
  766. static int
  767. tty3270_open(struct tty_struct *tty, struct file * filp)
  768. {
  769. struct raw3270_view *view;
  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. view = raw3270_find_view(&tty3270_fn,
  776. tty->index + RAW3270_FIRSTMINOR);
  777. if (!IS_ERR(view)) {
  778. tp = container_of(view, struct tty3270, view);
  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(view) == -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. rc = raw3270_add_view(&tp->view, &tty3270_fn,
  798. tty->index + RAW3270_FIRSTMINOR);
  799. if (rc) {
  800. tty3270_free_view(tp);
  801. return rc;
  802. }
  803. rc = tty3270_alloc_screen(tp);
  804. if (rc) {
  805. raw3270_put_view(&tp->view);
  806. raw3270_del_view(&tp->view);
  807. return rc;
  808. }
  809. tp->tty = tty;
  810. tty->low_latency = 0;
  811. tty->driver_data = tp;
  812. tty->winsize.ws_row = tp->view.rows - 2;
  813. tty->winsize.ws_col = tp->view.cols;
  814. tty3270_create_prompt(tp);
  815. tty3270_create_status(tp);
  816. tty3270_update_status(tp);
  817. /* Create blank line for every line in the tty output area. */
  818. for (i = 0; i < tp->view.rows - 2; i++)
  819. tty3270_blank_line(tp);
  820. tp->kbd->tty = tty;
  821. tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
  822. tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
  823. tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
  824. tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
  825. kbd_ascebc(tp->kbd, tp->view.ascebc);
  826. raw3270_activate_view(&tp->view);
  827. return 0;
  828. }
  829. /*
  830. * This routine is called when the 3270 tty is closed. We wait
  831. * for the remaining request to be completed. Then we clean up.
  832. */
  833. static void
  834. tty3270_close(struct tty_struct *tty, struct file * filp)
  835. {
  836. struct tty3270 *tp = tty->driver_data;
  837. if (tty->count > 1)
  838. return;
  839. if (tp) {
  840. tty->driver_data = NULL;
  841. tp->tty = tp->kbd->tty = NULL;
  842. raw3270_put_view(&tp->view);
  843. }
  844. }
  845. /*
  846. * We always have room.
  847. */
  848. static int
  849. tty3270_write_room(struct tty_struct *tty)
  850. {
  851. return INT_MAX;
  852. }
  853. /*
  854. * Insert character into the screen at the current position with the
  855. * current color and highlight. This function does NOT do cursor movement.
  856. */
  857. static void tty3270_put_character(struct tty3270 *tp, char ch)
  858. {
  859. struct tty3270_line *line;
  860. struct tty3270_cell *cell;
  861. line = tp->screen + tp->cy;
  862. if (line->len <= tp->cx) {
  863. while (line->len < tp->cx) {
  864. cell = line->cells + line->len;
  865. cell->character = tp->view.ascebc[' '];
  866. cell->highlight = tp->highlight;
  867. cell->f_color = tp->f_color;
  868. line->len++;
  869. }
  870. line->len++;
  871. }
  872. cell = line->cells + tp->cx;
  873. cell->character = tp->view.ascebc[(unsigned int) ch];
  874. cell->highlight = tp->highlight;
  875. cell->f_color = tp->f_color;
  876. }
  877. /*
  878. * Convert a tty3270_line to a 3270 data fragment usable for output.
  879. */
  880. static void
  881. tty3270_convert_line(struct tty3270 *tp, int line_nr)
  882. {
  883. struct tty3270_line *line;
  884. struct tty3270_cell *cell;
  885. struct string *s, *n;
  886. unsigned char highlight;
  887. unsigned char f_color;
  888. char *cp;
  889. int flen, i;
  890. /* Determine how long the fragment will be. */
  891. flen = 3; /* Prefix (TO_SBA). */
  892. line = tp->screen + line_nr;
  893. flen += line->len;
  894. highlight = TAX_RESET;
  895. f_color = TAC_RESET;
  896. for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
  897. if (cell->highlight != highlight) {
  898. flen += 3; /* TO_SA to switch highlight. */
  899. highlight = cell->highlight;
  900. }
  901. if (cell->f_color != f_color) {
  902. flen += 3; /* TO_SA to switch color. */
  903. f_color = cell->f_color;
  904. }
  905. }
  906. if (highlight != TAX_RESET)
  907. flen += 3; /* TO_SA to reset hightlight. */
  908. if (f_color != TAC_RESET)
  909. flen += 3; /* TO_SA to reset color. */
  910. if (line->len < tp->view.cols)
  911. flen += 4; /* Postfix (TO_RA). */
  912. /* Find the line in the list. */
  913. i = tp->view.rows - 2 - line_nr;
  914. list_for_each_entry_reverse(s, &tp->lines, list)
  915. if (--i <= 0)
  916. break;
  917. /*
  918. * Check if the line needs to get reallocated.
  919. */
  920. if (s->len != flen) {
  921. /* Reallocate string. */
  922. n = tty3270_alloc_string(tp, flen);
  923. list_add(&n->list, &s->list);
  924. list_del_init(&s->list);
  925. if (!list_empty(&s->update))
  926. list_del_init(&s->update);
  927. free_string(&tp->freemem, s);
  928. s = n;
  929. }
  930. /* Write 3270 data fragment. */
  931. cp = s->string;
  932. *cp++ = TO_SBA;
  933. *cp++ = 0;
  934. *cp++ = 0;
  935. highlight = TAX_RESET;
  936. f_color = TAC_RESET;
  937. for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
  938. if (cell->highlight != highlight) {
  939. *cp++ = TO_SA;
  940. *cp++ = TAT_EXTHI;
  941. *cp++ = cell->highlight;
  942. highlight = cell->highlight;
  943. }
  944. if (cell->f_color != f_color) {
  945. *cp++ = TO_SA;
  946. *cp++ = TAT_COLOR;
  947. *cp++ = cell->f_color;
  948. f_color = cell->f_color;
  949. }
  950. *cp++ = cell->character;
  951. }
  952. if (highlight != TAX_RESET) {
  953. *cp++ = TO_SA;
  954. *cp++ = TAT_EXTHI;
  955. *cp++ = TAX_RESET;
  956. }
  957. if (f_color != TAC_RESET) {
  958. *cp++ = TO_SA;
  959. *cp++ = TAT_COLOR;
  960. *cp++ = TAC_RESET;
  961. }
  962. if (line->len < tp->view.cols) {
  963. *cp++ = TO_RA;
  964. *cp++ = 0;
  965. *cp++ = 0;
  966. *cp++ = 0;
  967. }
  968. if (tp->nr_up + line_nr < tp->view.rows - 2) {
  969. /* Line is currently visible on screen. */
  970. tty3270_update_string(tp, s, line_nr);
  971. /* Add line to update list. */
  972. if (list_empty(&s->update)) {
  973. list_add_tail(&s->update, &tp->update);
  974. tp->update_flags |= TTY_UPDATE_LIST;
  975. }
  976. }
  977. }
  978. /*
  979. * Do carriage return.
  980. */
  981. static void
  982. tty3270_cr(struct tty3270 *tp)
  983. {
  984. tp->cx = 0;
  985. }
  986. /*
  987. * Do line feed.
  988. */
  989. static void
  990. tty3270_lf(struct tty3270 *tp)
  991. {
  992. struct tty3270_line temp;
  993. int i;
  994. tty3270_convert_line(tp, tp->cy);
  995. if (tp->cy < tp->view.rows - 3) {
  996. tp->cy++;
  997. return;
  998. }
  999. /* Last line just filled up. Add new, blank line. */
  1000. tty3270_blank_line(tp);
  1001. temp = tp->screen[0];
  1002. temp.len = 0;
  1003. for (i = 0; i < tp->view.rows - 3; i++)
  1004. tp->screen[i] = tp->screen[i+1];
  1005. tp->screen[tp->view.rows - 3] = temp;
  1006. tty3270_rebuild_update(tp);
  1007. }
  1008. static void
  1009. tty3270_ri(struct tty3270 *tp)
  1010. {
  1011. if (tp->cy > 0) {
  1012. tty3270_convert_line(tp, tp->cy);
  1013. tp->cy--;
  1014. }
  1015. }
  1016. /*
  1017. * Insert characters at current position.
  1018. */
  1019. static void
  1020. tty3270_insert_characters(struct tty3270 *tp, int n)
  1021. {
  1022. struct tty3270_line *line;
  1023. int k;
  1024. line = tp->screen + tp->cy;
  1025. while (line->len < tp->cx) {
  1026. line->cells[line->len].character = tp->view.ascebc[' '];
  1027. line->cells[line->len].highlight = TAX_RESET;
  1028. line->cells[line->len].f_color = TAC_RESET;
  1029. line->len++;
  1030. }
  1031. if (n > tp->view.cols - tp->cx)
  1032. n = tp->view.cols - tp->cx;
  1033. k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
  1034. while (k--)
  1035. line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
  1036. line->len += n;
  1037. if (line->len > tp->view.cols)
  1038. line->len = tp->view.cols;
  1039. while (n-- > 0) {
  1040. line->cells[tp->cx + n].character = tp->view.ascebc[' '];
  1041. line->cells[tp->cx + n].highlight = tp->highlight;
  1042. line->cells[tp->cx + n].f_color = tp->f_color;
  1043. }
  1044. }
  1045. /*
  1046. * Delete characters at current position.
  1047. */
  1048. static void
  1049. tty3270_delete_characters(struct tty3270 *tp, int n)
  1050. {
  1051. struct tty3270_line *line;
  1052. int i;
  1053. line = tp->screen + tp->cy;
  1054. if (line->len <= tp->cx)
  1055. return;
  1056. if (line->len - tp->cx <= n) {
  1057. line->len = tp->cx;
  1058. return;
  1059. }
  1060. for (i = tp->cx; i + n < line->len; i++)
  1061. line->cells[i] = line->cells[i + n];
  1062. line->len -= n;
  1063. }
  1064. /*
  1065. * Erase characters at current position.
  1066. */
  1067. static void
  1068. tty3270_erase_characters(struct tty3270 *tp, int n)
  1069. {
  1070. struct tty3270_line *line;
  1071. struct tty3270_cell *cell;
  1072. line = tp->screen + tp->cy;
  1073. while (line->len > tp->cx && n-- > 0) {
  1074. cell = line->cells + tp->cx++;
  1075. cell->character = ' ';
  1076. cell->highlight = TAX_RESET;
  1077. cell->f_color = TAC_RESET;
  1078. }
  1079. tp->cx += n;
  1080. tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
  1081. }
  1082. /*
  1083. * Erase line, 3 different cases:
  1084. * Esc [ 0 K Erase from current position to end of line inclusive
  1085. * Esc [ 1 K Erase from beginning of line to current position inclusive
  1086. * Esc [ 2 K Erase entire line (without moving cursor)
  1087. */
  1088. static void
  1089. tty3270_erase_line(struct tty3270 *tp, int mode)
  1090. {
  1091. struct tty3270_line *line;
  1092. struct tty3270_cell *cell;
  1093. int i;
  1094. line = tp->screen + tp->cy;
  1095. if (mode == 0)
  1096. line->len = tp->cx;
  1097. else if (mode == 1) {
  1098. for (i = 0; i < tp->cx; i++) {
  1099. cell = line->cells + i;
  1100. cell->character = ' ';
  1101. cell->highlight = TAX_RESET;
  1102. cell->f_color = TAC_RESET;
  1103. }
  1104. if (line->len <= tp->cx)
  1105. line->len = tp->cx + 1;
  1106. } else if (mode == 2)
  1107. line->len = 0;
  1108. tty3270_convert_line(tp, tp->cy);
  1109. }
  1110. /*
  1111. * Erase display, 3 different cases:
  1112. * Esc [ 0 J Erase from current position to bottom of screen inclusive
  1113. * Esc [ 1 J Erase from top of screen to current position inclusive
  1114. * Esc [ 2 J Erase entire screen (without moving the cursor)
  1115. */
  1116. static void
  1117. tty3270_erase_display(struct tty3270 *tp, int mode)
  1118. {
  1119. int i;
  1120. if (mode == 0) {
  1121. tty3270_erase_line(tp, 0);
  1122. for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
  1123. tp->screen[i].len = 0;
  1124. tty3270_convert_line(tp, i);
  1125. }
  1126. } else if (mode == 1) {
  1127. for (i = 0; i < tp->cy; i++) {
  1128. tp->screen[i].len = 0;
  1129. tty3270_convert_line(tp, i);
  1130. }
  1131. tty3270_erase_line(tp, 1);
  1132. } else if (mode == 2) {
  1133. for (i = 0; i < tp->view.rows - 2; i++) {
  1134. tp->screen[i].len = 0;
  1135. tty3270_convert_line(tp, i);
  1136. }
  1137. }
  1138. tty3270_rebuild_update(tp);
  1139. }
  1140. /*
  1141. * Set attributes found in an escape sequence.
  1142. * Esc [ <attr> ; <attr> ; ... m
  1143. */
  1144. static void
  1145. tty3270_set_attributes(struct tty3270 *tp)
  1146. {
  1147. static unsigned char f_colors[] = {
  1148. TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
  1149. TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
  1150. };
  1151. int i, attr;
  1152. for (i = 0; i <= tp->esc_npar; i++) {
  1153. attr = tp->esc_par[i];
  1154. switch (attr) {
  1155. case 0: /* Reset */
  1156. tp->highlight = TAX_RESET;
  1157. tp->f_color = TAC_RESET;
  1158. break;
  1159. /* Highlight. */
  1160. case 4: /* Start underlining. */
  1161. tp->highlight = TAX_UNDER;
  1162. break;
  1163. case 5: /* Start blink. */
  1164. tp->highlight = TAX_BLINK;
  1165. break;
  1166. case 7: /* Start reverse. */
  1167. tp->highlight = TAX_REVER;
  1168. break;
  1169. case 24: /* End underlining */
  1170. if (tp->highlight == TAX_UNDER)
  1171. tp->highlight = TAX_RESET;
  1172. break;
  1173. case 25: /* End blink. */
  1174. if (tp->highlight == TAX_BLINK)
  1175. tp->highlight = TAX_RESET;
  1176. break;
  1177. case 27: /* End reverse. */
  1178. if (tp->highlight == TAX_REVER)
  1179. tp->highlight = TAX_RESET;
  1180. break;
  1181. /* Foreground color. */
  1182. case 30: /* Black */
  1183. case 31: /* Red */
  1184. case 32: /* Green */
  1185. case 33: /* Yellow */
  1186. case 34: /* Blue */
  1187. case 35: /* Magenta */
  1188. case 36: /* Cyan */
  1189. case 37: /* White */
  1190. case 39: /* Black */
  1191. tp->f_color = f_colors[attr - 30];
  1192. break;
  1193. }
  1194. }
  1195. }
  1196. static inline int
  1197. tty3270_getpar(struct tty3270 *tp, int ix)
  1198. {
  1199. return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
  1200. }
  1201. static void
  1202. tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
  1203. {
  1204. int max_cx = max(0, cx);
  1205. int max_cy = max(0, cy);
  1206. tp->cx = min_t(int, tp->view.cols - 1, max_cx);
  1207. cy = min_t(int, tp->view.rows - 3, max_cy);
  1208. if (cy != tp->cy) {
  1209. tty3270_convert_line(tp, tp->cy);
  1210. tp->cy = cy;
  1211. }
  1212. }
  1213. /*
  1214. * Process escape sequences. Known sequences:
  1215. * Esc 7 Save Cursor Position
  1216. * Esc 8 Restore Cursor Position
  1217. * Esc [ Pn ; Pn ; .. m Set attributes
  1218. * Esc [ Pn ; Pn H Cursor Position
  1219. * Esc [ Pn ; Pn f Cursor Position
  1220. * Esc [ Pn A Cursor Up
  1221. * Esc [ Pn B Cursor Down
  1222. * Esc [ Pn C Cursor Forward
  1223. * Esc [ Pn D Cursor Backward
  1224. * Esc [ Pn G Cursor Horizontal Absolute
  1225. * Esc [ Pn X Erase Characters
  1226. * Esc [ Ps J Erase in Display
  1227. * Esc [ Ps K Erase in Line
  1228. * // FIXME: add all the new ones.
  1229. *
  1230. * Pn is a numeric parameter, a string of zero or more decimal digits.
  1231. * Ps is a selective parameter.
  1232. */
  1233. static void
  1234. tty3270_escape_sequence(struct tty3270 *tp, char ch)
  1235. {
  1236. enum { ESnormal, ESesc, ESsquare, ESgetpars };
  1237. if (tp->esc_state == ESnormal) {
  1238. if (ch == 0x1b)
  1239. /* Starting new escape sequence. */
  1240. tp->esc_state = ESesc;
  1241. return;
  1242. }
  1243. if (tp->esc_state == ESesc) {
  1244. tp->esc_state = ESnormal;
  1245. switch (ch) {
  1246. case '[':
  1247. tp->esc_state = ESsquare;
  1248. break;
  1249. case 'E':
  1250. tty3270_cr(tp);
  1251. tty3270_lf(tp);
  1252. break;
  1253. case 'M':
  1254. tty3270_ri(tp);
  1255. break;
  1256. case 'D':
  1257. tty3270_lf(tp);
  1258. break;
  1259. case 'Z': /* Respond ID. */
  1260. kbd_puts_queue(tp->tty, "\033[?6c");
  1261. break;
  1262. case '7': /* Save cursor position. */
  1263. tp->saved_cx = tp->cx;
  1264. tp->saved_cy = tp->cy;
  1265. tp->saved_highlight = tp->highlight;
  1266. tp->saved_f_color = tp->f_color;
  1267. break;
  1268. case '8': /* Restore cursor position. */
  1269. tty3270_convert_line(tp, tp->cy);
  1270. tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
  1271. tp->highlight = tp->saved_highlight;
  1272. tp->f_color = tp->saved_f_color;
  1273. break;
  1274. case 'c': /* Reset terminal. */
  1275. tp->cx = tp->saved_cx = 0;
  1276. tp->cy = tp->saved_cy = 0;
  1277. tp->highlight = tp->saved_highlight = TAX_RESET;
  1278. tp->f_color = tp->saved_f_color = TAC_RESET;
  1279. tty3270_erase_display(tp, 2);
  1280. break;
  1281. }
  1282. return;
  1283. }
  1284. if (tp->esc_state == ESsquare) {
  1285. tp->esc_state = ESgetpars;
  1286. memset(tp->esc_par, 0, sizeof(tp->esc_par));
  1287. tp->esc_npar = 0;
  1288. tp->esc_ques = (ch == '?');
  1289. if (tp->esc_ques)
  1290. return;
  1291. }
  1292. if (tp->esc_state == ESgetpars) {
  1293. if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
  1294. tp->esc_npar++;
  1295. return;
  1296. }
  1297. if (ch >= '0' && ch <= '9') {
  1298. tp->esc_par[tp->esc_npar] *= 10;
  1299. tp->esc_par[tp->esc_npar] += ch - '0';
  1300. return;
  1301. }
  1302. }
  1303. tp->esc_state = ESnormal;
  1304. if (ch == 'n' && !tp->esc_ques) {
  1305. if (tp->esc_par[0] == 5) /* Status report. */
  1306. kbd_puts_queue(tp->tty, "\033[0n");
  1307. else if (tp->esc_par[0] == 6) { /* Cursor report. */
  1308. char buf[40];
  1309. sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
  1310. kbd_puts_queue(tp->tty, buf);
  1311. }
  1312. return;
  1313. }
  1314. if (tp->esc_ques)
  1315. return;
  1316. switch (ch) {
  1317. case 'm':
  1318. tty3270_set_attributes(tp);
  1319. break;
  1320. case 'H': /* Set cursor position. */
  1321. case 'f':
  1322. tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
  1323. tty3270_getpar(tp, 0) - 1);
  1324. break;
  1325. case 'd': /* Set y position. */
  1326. tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
  1327. break;
  1328. case 'A': /* Cursor up. */
  1329. case 'F':
  1330. tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
  1331. break;
  1332. case 'B': /* Cursor down. */
  1333. case 'e':
  1334. case 'E':
  1335. tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
  1336. break;
  1337. case 'C': /* Cursor forward. */
  1338. case 'a':
  1339. tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
  1340. break;
  1341. case 'D': /* Cursor backward. */
  1342. tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
  1343. break;
  1344. case 'G': /* Set x position. */
  1345. case '`':
  1346. tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
  1347. break;
  1348. case 'X': /* Erase Characters. */
  1349. tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
  1350. break;
  1351. case 'J': /* Erase display. */
  1352. tty3270_erase_display(tp, tp->esc_par[0]);
  1353. break;
  1354. case 'K': /* Erase line. */
  1355. tty3270_erase_line(tp, tp->esc_par[0]);
  1356. break;
  1357. case 'P': /* Delete characters. */
  1358. tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
  1359. break;
  1360. case '@': /* Insert characters. */
  1361. tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
  1362. break;
  1363. case 's': /* Save cursor position. */
  1364. tp->saved_cx = tp->cx;
  1365. tp->saved_cy = tp->cy;
  1366. tp->saved_highlight = tp->highlight;
  1367. tp->saved_f_color = tp->f_color;
  1368. break;
  1369. case 'u': /* Restore cursor position. */
  1370. tty3270_convert_line(tp, tp->cy);
  1371. tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
  1372. tp->highlight = tp->saved_highlight;
  1373. tp->f_color = tp->saved_f_color;
  1374. break;
  1375. }
  1376. }
  1377. /*
  1378. * String write routine for 3270 ttys
  1379. */
  1380. static void
  1381. tty3270_do_write(struct tty3270 *tp, struct tty_struct *tty,
  1382. const unsigned char *buf, int count)
  1383. {
  1384. int i_msg, i;
  1385. spin_lock_bh(&tp->view.lock);
  1386. for (i_msg = 0; !tty->stopped && i_msg < count; i_msg++) {
  1387. if (tp->esc_state != 0) {
  1388. /* Continue escape sequence. */
  1389. tty3270_escape_sequence(tp, buf[i_msg]);
  1390. continue;
  1391. }
  1392. switch (buf[i_msg]) {
  1393. case 0x07: /* '\a' -- Alarm */
  1394. tp->wcc |= TW_PLUSALARM;
  1395. break;
  1396. case 0x08: /* Backspace. */
  1397. if (tp->cx > 0) {
  1398. tp->cx--;
  1399. tty3270_put_character(tp, ' ');
  1400. }
  1401. break;
  1402. case 0x09: /* '\t' -- Tabulate */
  1403. for (i = tp->cx % 8; i < 8; i++) {
  1404. if (tp->cx >= tp->view.cols) {
  1405. tty3270_cr(tp);
  1406. tty3270_lf(tp);
  1407. break;
  1408. }
  1409. tty3270_put_character(tp, ' ');
  1410. tp->cx++;
  1411. }
  1412. break;
  1413. case 0x0a: /* '\n' -- New Line */
  1414. tty3270_cr(tp);
  1415. tty3270_lf(tp);
  1416. break;
  1417. case 0x0c: /* '\f' -- Form Feed */
  1418. tty3270_erase_display(tp, 2);
  1419. tp->cx = tp->cy = 0;
  1420. break;
  1421. case 0x0d: /* '\r' -- Carriage Return */
  1422. tp->cx = 0;
  1423. break;
  1424. case 0x0f: /* SuSE "exit alternate mode" */
  1425. break;
  1426. case 0x1b: /* Start escape sequence. */
  1427. tty3270_escape_sequence(tp, buf[i_msg]);
  1428. break;
  1429. default: /* Insert normal character. */
  1430. if (tp->cx >= tp->view.cols) {
  1431. tty3270_cr(tp);
  1432. tty3270_lf(tp);
  1433. }
  1434. tty3270_put_character(tp, buf[i_msg]);
  1435. tp->cx++;
  1436. break;
  1437. }
  1438. }
  1439. /* Convert current line to 3270 data fragment. */
  1440. tty3270_convert_line(tp, tp->cy);
  1441. /* Setup timer to update display after 1/10 second */
  1442. if (!timer_pending(&tp->timer))
  1443. tty3270_set_timer(tp, HZ/10);
  1444. spin_unlock_bh(&tp->view.lock);
  1445. }
  1446. /*
  1447. * String write routine for 3270 ttys
  1448. */
  1449. static int
  1450. tty3270_write(struct tty_struct * tty,
  1451. const unsigned char *buf, int count)
  1452. {
  1453. struct tty3270 *tp;
  1454. tp = tty->driver_data;
  1455. if (!tp)
  1456. return 0;
  1457. if (tp->char_count > 0) {
  1458. tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
  1459. tp->char_count = 0;
  1460. }
  1461. tty3270_do_write(tp, tty, buf, count);
  1462. return count;
  1463. }
  1464. /*
  1465. * Put single characters to the ttys character buffer
  1466. */
  1467. static int tty3270_put_char(struct tty_struct *tty, unsigned char ch)
  1468. {
  1469. struct tty3270 *tp;
  1470. tp = tty->driver_data;
  1471. if (!tp || tp->char_count >= TTY3270_CHAR_BUF_SIZE)
  1472. return 0;
  1473. tp->char_buf[tp->char_count++] = ch;
  1474. return 1;
  1475. }
  1476. /*
  1477. * Flush all characters from the ttys characeter buffer put there
  1478. * by tty3270_put_char.
  1479. */
  1480. static void
  1481. tty3270_flush_chars(struct tty_struct *tty)
  1482. {
  1483. struct tty3270 *tp;
  1484. tp = tty->driver_data;
  1485. if (!tp)
  1486. return;
  1487. if (tp->char_count > 0) {
  1488. tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
  1489. tp->char_count = 0;
  1490. }
  1491. }
  1492. /*
  1493. * Returns the number of characters in the output buffer. This is
  1494. * used in tty_wait_until_sent to wait until all characters have
  1495. * appeared on the screen.
  1496. */
  1497. static int
  1498. tty3270_chars_in_buffer(struct tty_struct *tty)
  1499. {
  1500. return 0;
  1501. }
  1502. static void
  1503. tty3270_flush_buffer(struct tty_struct *tty)
  1504. {
  1505. }
  1506. /*
  1507. * Check for visible/invisible input switches
  1508. */
  1509. static void
  1510. tty3270_set_termios(struct tty_struct *tty, struct ktermios *old)
  1511. {
  1512. struct tty3270 *tp;
  1513. int new;
  1514. tp = tty->driver_data;
  1515. if (!tp)
  1516. return;
  1517. spin_lock_bh(&tp->view.lock);
  1518. if (L_ICANON(tty)) {
  1519. new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
  1520. if (new != tp->inattr) {
  1521. tp->inattr = new;
  1522. tty3270_update_prompt(tp, NULL, 0);
  1523. tty3270_set_timer(tp, 1);
  1524. }
  1525. }
  1526. spin_unlock_bh(&tp->view.lock);
  1527. }
  1528. /*
  1529. * Disable reading from a 3270 tty
  1530. */
  1531. static void
  1532. tty3270_throttle(struct tty_struct * tty)
  1533. {
  1534. struct tty3270 *tp;
  1535. tp = tty->driver_data;
  1536. if (!tp)
  1537. return;
  1538. tp->throttle = 1;
  1539. }
  1540. /*
  1541. * Enable reading from a 3270 tty
  1542. */
  1543. static void
  1544. tty3270_unthrottle(struct tty_struct * tty)
  1545. {
  1546. struct tty3270 *tp;
  1547. tp = tty->driver_data;
  1548. if (!tp)
  1549. return;
  1550. tp->throttle = 0;
  1551. if (tp->attn)
  1552. tty3270_issue_read(tp, 1);
  1553. }
  1554. /*
  1555. * Hang up the tty device.
  1556. */
  1557. static void
  1558. tty3270_hangup(struct tty_struct *tty)
  1559. {
  1560. // FIXME: implement
  1561. }
  1562. static void
  1563. tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
  1564. {
  1565. }
  1566. static int tty3270_ioctl(struct tty_struct *tty, unsigned int cmd,
  1567. unsigned long arg)
  1568. {
  1569. struct tty3270 *tp;
  1570. tp = tty->driver_data;
  1571. if (!tp)
  1572. return -ENODEV;
  1573. if (tty->flags & (1 << TTY_IO_ERROR))
  1574. return -EIO;
  1575. return kbd_ioctl(tp->kbd, cmd, arg);
  1576. }
  1577. #ifdef CONFIG_COMPAT
  1578. static long tty3270_compat_ioctl(struct tty_struct *tty,
  1579. unsigned int cmd, unsigned long arg)
  1580. {
  1581. struct tty3270 *tp;
  1582. tp = tty->driver_data;
  1583. if (!tp)
  1584. return -ENODEV;
  1585. if (tty->flags & (1 << TTY_IO_ERROR))
  1586. return -EIO;
  1587. return kbd_ioctl(tp->kbd, cmd, (unsigned long)compat_ptr(arg));
  1588. }
  1589. #endif
  1590. static const struct tty_operations tty3270_ops = {
  1591. .open = tty3270_open,
  1592. .close = tty3270_close,
  1593. .write = tty3270_write,
  1594. .put_char = tty3270_put_char,
  1595. .flush_chars = tty3270_flush_chars,
  1596. .write_room = tty3270_write_room,
  1597. .chars_in_buffer = tty3270_chars_in_buffer,
  1598. .flush_buffer = tty3270_flush_buffer,
  1599. .throttle = tty3270_throttle,
  1600. .unthrottle = tty3270_unthrottle,
  1601. .hangup = tty3270_hangup,
  1602. .wait_until_sent = tty3270_wait_until_sent,
  1603. .ioctl = tty3270_ioctl,
  1604. #ifdef CONFIG_COMPAT
  1605. .compat_ioctl = tty3270_compat_ioctl,
  1606. #endif
  1607. .set_termios = tty3270_set_termios
  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->driver_name = "ttyTUB";
  1626. driver->name = "ttyTUB";
  1627. driver->major = IBM_TTY3270_MAJOR;
  1628. driver->minor_start = RAW3270_FIRSTMINOR;
  1629. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1630. driver->subtype = SYSTEM_TYPE_TTY;
  1631. driver->init_termios = tty_std_termios;
  1632. driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_DYNAMIC_DEV;
  1633. tty_set_operations(driver, &tty3270_ops);
  1634. ret = tty_register_driver(driver);
  1635. if (ret) {
  1636. put_tty_driver(driver);
  1637. return ret;
  1638. }
  1639. tty3270_driver = driver;
  1640. return 0;
  1641. }
  1642. static void __exit
  1643. tty3270_exit(void)
  1644. {
  1645. struct tty_driver *driver;
  1646. driver = tty3270_driver;
  1647. tty3270_driver = NULL;
  1648. tty_unregister_driver(driver);
  1649. tty3270_del_views();
  1650. }
  1651. MODULE_LICENSE("GPL");
  1652. MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
  1653. module_init(tty3270_init);
  1654. module_exit(tty3270_exit);