tty3270.c 46 KB

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