tty3270.c 46 KB

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