tty3270.c 45 KB

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