tty3270.c 42 KB

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