tty3270.c 45 KB

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