tty3270.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IBM/3270 Driver - tty functions.
  4. *
  5. * Author(s):
  6. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  7. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. * -- Copyright IBM Corp. 2003
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/tty.h>
  14. #include <linux/vt_kern.h>
  15. #include <linux/init.h>
  16. #include <linux/console.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/slab.h>
  20. #include <linux/bootmem.h>
  21. #include <linux/compat.h>
  22. #include <asm/ccwdev.h>
  23. #include <asm/cio.h>
  24. #include <asm/ebcdic.h>
  25. #include <linux/uaccess.h>
  26. #include "raw3270.h"
  27. #include "tty3270.h"
  28. #include "keyboard.h"
  29. #define TTY3270_CHAR_BUF_SIZE 256
  30. #define TTY3270_OUTPUT_BUFFER_SIZE 1024
  31. #define TTY3270_STRING_PAGES 5
  32. struct tty_driver *tty3270_driver;
  33. static int tty3270_max_index;
  34. static struct raw3270_fn tty3270_fn;
  35. struct tty3270_cell {
  36. unsigned char character;
  37. unsigned char highlight;
  38. unsigned char f_color;
  39. };
  40. struct tty3270_line {
  41. struct tty3270_cell *cells;
  42. int len;
  43. };
  44. #define ESCAPE_NPAR 8
  45. /*
  46. * The main tty view data structure.
  47. * FIXME:
  48. * 1) describe line orientation & lines list concept against screen
  49. * 2) describe conversion of screen to lines
  50. * 3) describe line format.
  51. */
  52. struct tty3270 {
  53. struct raw3270_view view;
  54. struct tty_port port;
  55. void **freemem_pages; /* Array of pages used for freemem. */
  56. struct list_head freemem; /* List of free memory for strings. */
  57. /* Output stuff. */
  58. struct list_head lines; /* List of lines. */
  59. struct list_head update; /* List of lines to update. */
  60. unsigned char wcc; /* Write control character. */
  61. int nr_lines; /* # lines in list. */
  62. int nr_up; /* # lines up in history. */
  63. unsigned long update_flags; /* Update indication bits. */
  64. struct string *status; /* Lower right of display. */
  65. struct raw3270_request *write; /* Single write request. */
  66. struct timer_list timer; /* Output delay timer. */
  67. /* Current tty screen. */
  68. unsigned int cx, cy; /* Current output position. */
  69. unsigned int highlight; /* Blink/reverse/underscore */
  70. unsigned int f_color; /* Foreground color */
  71. struct tty3270_line *screen;
  72. unsigned int n_model, n_cols, n_rows; /* New model & size */
  73. struct work_struct resize_work;
  74. /* Input stuff. */
  75. struct string *prompt; /* Output string for input area. */
  76. struct string *input; /* Input string for read request. */
  77. struct raw3270_request *read; /* Single read request. */
  78. struct raw3270_request *kreset; /* Single keyboard reset request. */
  79. unsigned char inattr; /* Visible/invisible input. */
  80. int throttle, attn; /* tty throttle/unthrottle. */
  81. struct tasklet_struct readlet; /* Tasklet to issue read request. */
  82. struct tasklet_struct hanglet; /* Tasklet to hang up the tty. */
  83. struct kbd_data *kbd; /* key_maps stuff. */
  84. /* Escape sequence parsing. */
  85. int esc_state, esc_ques, esc_npar;
  86. int esc_par[ESCAPE_NPAR];
  87. unsigned int saved_cx, saved_cy;
  88. unsigned int saved_highlight, saved_f_color;
  89. /* Command recalling. */
  90. struct list_head rcl_lines; /* List of recallable lines. */
  91. struct list_head *rcl_walk; /* Point in rcl_lines list. */
  92. int rcl_nr, rcl_max; /* Number/max number of rcl_lines. */
  93. /* Character array for put_char/flush_chars. */
  94. unsigned int char_count;
  95. char char_buf[TTY3270_CHAR_BUF_SIZE];
  96. };
  97. /* tty3270->update_flags. See tty3270_update for details. */
  98. #define TTY_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
  99. #define TTY_UPDATE_LIST 2 /* Update lines in tty3270->update. */
  100. #define TTY_UPDATE_INPUT 4 /* Update input line. */
  101. #define TTY_UPDATE_STATUS 8 /* Update status line. */
  102. #define TTY_UPDATE_ALL 16 /* Recreate screen. */
  103. static void tty3270_update(struct timer_list *);
  104. static void tty3270_resize_work(struct work_struct *work);
  105. /*
  106. * Setup timeout for a device. On timeout trigger an update.
  107. */
  108. static void tty3270_set_timer(struct tty3270 *tp, int expires)
  109. {
  110. mod_timer(&tp->timer, jiffies + expires);
  111. }
  112. /*
  113. * The input line are the two last lines of the screen.
  114. */
  115. static void
  116. tty3270_update_prompt(struct tty3270 *tp, char *input, int count)
  117. {
  118. struct string *line;
  119. unsigned int off;
  120. line = tp->prompt;
  121. if (count != 0)
  122. line->string[5] = TF_INMDT;
  123. else
  124. line->string[5] = tp->inattr;
  125. if (count > tp->view.cols * 2 - 11)
  126. count = tp->view.cols * 2 - 11;
  127. memcpy(line->string + 6, input, count);
  128. line->string[6 + count] = TO_IC;
  129. /* Clear to end of input line. */
  130. if (count < tp->view.cols * 2 - 11) {
  131. line->string[7 + count] = TO_RA;
  132. line->string[10 + count] = 0;
  133. off = tp->view.cols * tp->view.rows - 9;
  134. raw3270_buffer_address(tp->view.dev, line->string+count+8, off);
  135. line->len = 11 + count;
  136. } else
  137. line->len = 7 + count;
  138. tp->update_flags |= TTY_UPDATE_INPUT;
  139. }
  140. static void
  141. tty3270_create_prompt(struct tty3270 *tp)
  142. {
  143. static const unsigned char blueprint[] =
  144. { TO_SBA, 0, 0, 0x6e, TO_SF, TF_INPUT,
  145. /* empty input string */
  146. TO_IC, TO_RA, 0, 0, 0 };
  147. struct string *line;
  148. unsigned int offset;
  149. line = alloc_string(&tp->freemem,
  150. sizeof(blueprint) + tp->view.cols * 2 - 9);
  151. tp->prompt = line;
  152. tp->inattr = TF_INPUT;
  153. /* Copy blueprint to status line */
  154. memcpy(line->string, blueprint, sizeof(blueprint));
  155. line->len = sizeof(blueprint);
  156. /* Set output offsets. */
  157. offset = tp->view.cols * (tp->view.rows - 2);
  158. raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
  159. offset = tp->view.cols * tp->view.rows - 9;
  160. raw3270_buffer_address(tp->view.dev, line->string + 8, offset);
  161. /* Allocate input string for reading. */
  162. tp->input = alloc_string(&tp->freemem, tp->view.cols * 2 - 9 + 6);
  163. }
  164. /*
  165. * The status line is the last line of the screen. It shows the string
  166. * "Running"/"Holding" in the lower right corner of the screen.
  167. */
  168. static void
  169. tty3270_update_status(struct tty3270 * tp)
  170. {
  171. char *str;
  172. str = (tp->nr_up != 0) ? "History" : "Running";
  173. memcpy(tp->status->string + 8, str, 7);
  174. codepage_convert(tp->view.ascebc, tp->status->string + 8, 7);
  175. tp->update_flags |= TTY_UPDATE_STATUS;
  176. }
  177. static void
  178. tty3270_create_status(struct tty3270 * tp)
  179. {
  180. static const unsigned char blueprint[] =
  181. { TO_SBA, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR, TAC_GREEN,
  182. 0, 0, 0, 0, 0, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR,
  183. TAC_RESET };
  184. struct string *line;
  185. unsigned int offset;
  186. line = alloc_string(&tp->freemem,sizeof(blueprint));
  187. tp->status = line;
  188. /* Copy blueprint to status line */
  189. memcpy(line->string, blueprint, sizeof(blueprint));
  190. /* Set address to start of status string (= last 9 characters). */
  191. offset = tp->view.cols * tp->view.rows - 9;
  192. raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
  193. }
  194. /*
  195. * Set output offsets to 3270 datastream fragment of a tty string.
  196. * (TO_SBA offset at the start and TO_RA offset at the end of the string)
  197. */
  198. static void
  199. tty3270_update_string(struct tty3270 *tp, struct string *line, int nr)
  200. {
  201. unsigned char *cp;
  202. raw3270_buffer_address(tp->view.dev, line->string + 1,
  203. tp->view.cols * nr);
  204. cp = line->string + line->len - 4;
  205. if (*cp == TO_RA)
  206. raw3270_buffer_address(tp->view.dev, cp + 1,
  207. tp->view.cols * (nr + 1));
  208. }
  209. /*
  210. * Rebuild update list to print all lines.
  211. */
  212. static void
  213. tty3270_rebuild_update(struct tty3270 *tp)
  214. {
  215. struct string *s, *n;
  216. int line, nr_up;
  217. /*
  218. * Throw away update list and create a new one,
  219. * containing all lines that will fit on the screen.
  220. */
  221. list_for_each_entry_safe(s, n, &tp->update, update)
  222. list_del_init(&s->update);
  223. line = tp->view.rows - 3;
  224. nr_up = tp->nr_up;
  225. list_for_each_entry_reverse(s, &tp->lines, list) {
  226. if (nr_up > 0) {
  227. nr_up--;
  228. continue;
  229. }
  230. tty3270_update_string(tp, s, line);
  231. list_add(&s->update, &tp->update);
  232. if (--line < 0)
  233. break;
  234. }
  235. tp->update_flags |= TTY_UPDATE_LIST;
  236. }
  237. /*
  238. * Alloc string for size bytes. If there is not enough room in
  239. * freemem, free strings until there is room.
  240. */
  241. static struct string *
  242. tty3270_alloc_string(struct tty3270 *tp, size_t size)
  243. {
  244. struct string *s, *n;
  245. s = alloc_string(&tp->freemem, size);
  246. if (s)
  247. return s;
  248. list_for_each_entry_safe(s, n, &tp->lines, list) {
  249. BUG_ON(tp->nr_lines <= tp->view.rows - 2);
  250. list_del(&s->list);
  251. if (!list_empty(&s->update))
  252. list_del(&s->update);
  253. tp->nr_lines--;
  254. if (free_string(&tp->freemem, s) >= size)
  255. break;
  256. }
  257. s = alloc_string(&tp->freemem, size);
  258. BUG_ON(!s);
  259. if (tp->nr_up != 0 &&
  260. tp->nr_up + tp->view.rows - 2 >= tp->nr_lines) {
  261. tp->nr_up = tp->nr_lines - tp->view.rows + 2;
  262. tty3270_rebuild_update(tp);
  263. tty3270_update_status(tp);
  264. }
  265. return s;
  266. }
  267. /*
  268. * Add an empty line to the list.
  269. */
  270. static void
  271. tty3270_blank_line(struct tty3270 *tp)
  272. {
  273. static const unsigned char blueprint[] =
  274. { TO_SBA, 0, 0, TO_SA, TAT_EXTHI, TAX_RESET,
  275. TO_SA, TAT_COLOR, TAC_RESET, TO_RA, 0, 0, 0 };
  276. struct string *s;
  277. s = tty3270_alloc_string(tp, sizeof(blueprint));
  278. memcpy(s->string, blueprint, sizeof(blueprint));
  279. s->len = sizeof(blueprint);
  280. list_add_tail(&s->list, &tp->lines);
  281. tp->nr_lines++;
  282. if (tp->nr_up != 0)
  283. tp->nr_up++;
  284. }
  285. /*
  286. * Create a blank screen and remove all lines from the history.
  287. */
  288. static void
  289. tty3270_blank_screen(struct tty3270 *tp)
  290. {
  291. struct string *s, *n;
  292. int i;
  293. for (i = 0; i < tp->view.rows - 2; i++)
  294. tp->screen[i].len = 0;
  295. tp->nr_up = 0;
  296. list_for_each_entry_safe(s, n, &tp->lines, list) {
  297. list_del(&s->list);
  298. if (!list_empty(&s->update))
  299. list_del(&s->update);
  300. tp->nr_lines--;
  301. free_string(&tp->freemem, s);
  302. }
  303. }
  304. /*
  305. * Write request completion callback.
  306. */
  307. static void
  308. tty3270_write_callback(struct raw3270_request *rq, void *data)
  309. {
  310. struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
  311. if (rq->rc != 0) {
  312. /* Write wasn't successful. Refresh all. */
  313. tp->update_flags = TTY_UPDATE_ALL;
  314. tty3270_set_timer(tp, 1);
  315. }
  316. raw3270_request_reset(rq);
  317. xchg(&tp->write, rq);
  318. }
  319. /*
  320. * Update 3270 display.
  321. */
  322. static void
  323. tty3270_update(struct timer_list *t)
  324. {
  325. struct tty3270 *tp = from_timer(tp, t, timer);
  326. static char invalid_sba[2] = { 0xff, 0xff };
  327. struct raw3270_request *wrq;
  328. unsigned long updated;
  329. struct string *s, *n;
  330. char *sba, *str;
  331. int rc, len;
  332. wrq = xchg(&tp->write, 0);
  333. if (!wrq) {
  334. tty3270_set_timer(tp, 1);
  335. return;
  336. }
  337. spin_lock(&tp->view.lock);
  338. updated = 0;
  339. if (tp->update_flags & TTY_UPDATE_ALL) {
  340. tty3270_rebuild_update(tp);
  341. tty3270_update_status(tp);
  342. tp->update_flags = TTY_UPDATE_ERASE | TTY_UPDATE_LIST |
  343. TTY_UPDATE_INPUT | TTY_UPDATE_STATUS;
  344. }
  345. if (tp->update_flags & TTY_UPDATE_ERASE) {
  346. /* Use erase write alternate to erase display. */
  347. raw3270_request_set_cmd(wrq, TC_EWRITEA);
  348. updated |= TTY_UPDATE_ERASE;
  349. } else
  350. raw3270_request_set_cmd(wrq, TC_WRITE);
  351. raw3270_request_add_data(wrq, &tp->wcc, 1);
  352. tp->wcc = TW_NONE;
  353. /*
  354. * Update status line.
  355. */
  356. if (tp->update_flags & TTY_UPDATE_STATUS)
  357. if (raw3270_request_add_data(wrq, tp->status->string,
  358. tp->status->len) == 0)
  359. updated |= TTY_UPDATE_STATUS;
  360. /*
  361. * Write input line.
  362. */
  363. if (tp->update_flags & TTY_UPDATE_INPUT)
  364. if (raw3270_request_add_data(wrq, tp->prompt->string,
  365. tp->prompt->len) == 0)
  366. updated |= TTY_UPDATE_INPUT;
  367. sba = invalid_sba;
  368. if (tp->update_flags & TTY_UPDATE_LIST) {
  369. /* Write strings in the update list to the screen. */
  370. list_for_each_entry_safe(s, n, &tp->update, update) {
  371. str = s->string;
  372. len = s->len;
  373. /*
  374. * Skip TO_SBA at the start of the string if the
  375. * last output position matches the start address
  376. * of this line.
  377. */
  378. if (s->string[1] == sba[0] && s->string[2] == sba[1])
  379. str += 3, len -= 3;
  380. if (raw3270_request_add_data(wrq, str, len) != 0)
  381. break;
  382. list_del_init(&s->update);
  383. if (s->string[s->len - 4] == TO_RA)
  384. sba = s->string + s->len - 3;
  385. else
  386. sba = invalid_sba;
  387. }
  388. if (list_empty(&tp->update))
  389. updated |= TTY_UPDATE_LIST;
  390. }
  391. wrq->callback = tty3270_write_callback;
  392. rc = raw3270_start(&tp->view, wrq);
  393. if (rc == 0) {
  394. tp->update_flags &= ~updated;
  395. if (tp->update_flags)
  396. tty3270_set_timer(tp, 1);
  397. } else {
  398. raw3270_request_reset(wrq);
  399. xchg(&tp->write, wrq);
  400. }
  401. spin_unlock(&tp->view.lock);
  402. }
  403. /*
  404. * Command recalling.
  405. */
  406. static void
  407. tty3270_rcl_add(struct tty3270 *tp, char *input, int len)
  408. {
  409. struct string *s;
  410. tp->rcl_walk = NULL;
  411. if (len <= 0)
  412. return;
  413. if (tp->rcl_nr >= tp->rcl_max) {
  414. s = list_entry(tp->rcl_lines.next, struct string, list);
  415. list_del(&s->list);
  416. free_string(&tp->freemem, s);
  417. tp->rcl_nr--;
  418. }
  419. s = tty3270_alloc_string(tp, len);
  420. memcpy(s->string, input, len);
  421. list_add_tail(&s->list, &tp->rcl_lines);
  422. tp->rcl_nr++;
  423. }
  424. static void
  425. tty3270_rcl_backward(struct kbd_data *kbd)
  426. {
  427. struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
  428. struct string *s;
  429. spin_lock_bh(&tp->view.lock);
  430. if (tp->inattr == TF_INPUT) {
  431. if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
  432. tp->rcl_walk = tp->rcl_walk->prev;
  433. else if (!list_empty(&tp->rcl_lines))
  434. tp->rcl_walk = tp->rcl_lines.prev;
  435. s = tp->rcl_walk ?
  436. list_entry(tp->rcl_walk, struct string, list) : NULL;
  437. if (tp->rcl_walk) {
  438. s = list_entry(tp->rcl_walk, struct string, list);
  439. tty3270_update_prompt(tp, s->string, s->len);
  440. } else
  441. tty3270_update_prompt(tp, NULL, 0);
  442. tty3270_set_timer(tp, 1);
  443. }
  444. spin_unlock_bh(&tp->view.lock);
  445. }
  446. /*
  447. * Deactivate tty view.
  448. */
  449. static void
  450. tty3270_exit_tty(struct kbd_data *kbd)
  451. {
  452. struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
  453. raw3270_deactivate_view(&tp->view);
  454. }
  455. /*
  456. * Scroll forward in history.
  457. */
  458. static void
  459. tty3270_scroll_forward(struct kbd_data *kbd)
  460. {
  461. struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
  462. int nr_up;
  463. spin_lock_bh(&tp->view.lock);
  464. nr_up = tp->nr_up - tp->view.rows + 2;
  465. if (nr_up < 0)
  466. nr_up = 0;
  467. if (nr_up != tp->nr_up) {
  468. tp->nr_up = nr_up;
  469. tty3270_rebuild_update(tp);
  470. tty3270_update_status(tp);
  471. tty3270_set_timer(tp, 1);
  472. }
  473. spin_unlock_bh(&tp->view.lock);
  474. }
  475. /*
  476. * Scroll backward in history.
  477. */
  478. static void
  479. tty3270_scroll_backward(struct kbd_data *kbd)
  480. {
  481. struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
  482. int nr_up;
  483. spin_lock_bh(&tp->view.lock);
  484. nr_up = tp->nr_up + tp->view.rows - 2;
  485. if (nr_up + tp->view.rows - 2 > tp->nr_lines)
  486. nr_up = tp->nr_lines - tp->view.rows + 2;
  487. if (nr_up != tp->nr_up) {
  488. tp->nr_up = nr_up;
  489. tty3270_rebuild_update(tp);
  490. tty3270_update_status(tp);
  491. tty3270_set_timer(tp, 1);
  492. }
  493. spin_unlock_bh(&tp->view.lock);
  494. }
  495. /*
  496. * Pass input line to tty.
  497. */
  498. static void
  499. tty3270_read_tasklet(struct raw3270_request *rrq)
  500. {
  501. static char kreset_data = TW_KR;
  502. struct tty3270 *tp = container_of(rrq->view, struct tty3270, view);
  503. char *input;
  504. int len;
  505. spin_lock_bh(&tp->view.lock);
  506. /*
  507. * Two AID keys are special: For 0x7d (enter) the input line
  508. * has to be emitted to the tty and for 0x6d the screen
  509. * needs to be redrawn.
  510. */
  511. input = NULL;
  512. len = 0;
  513. if (tp->input->string[0] == 0x7d) {
  514. /* Enter: write input to tty. */
  515. input = tp->input->string + 6;
  516. len = tp->input->len - 6 - rrq->rescnt;
  517. if (tp->inattr != TF_INPUTN)
  518. tty3270_rcl_add(tp, input, len);
  519. if (tp->nr_up > 0) {
  520. tp->nr_up = 0;
  521. tty3270_rebuild_update(tp);
  522. tty3270_update_status(tp);
  523. }
  524. /* Clear input area. */
  525. tty3270_update_prompt(tp, NULL, 0);
  526. tty3270_set_timer(tp, 1);
  527. } else if (tp->input->string[0] == 0x6d) {
  528. /* Display has been cleared. Redraw. */
  529. tp->update_flags = TTY_UPDATE_ALL;
  530. tty3270_set_timer(tp, 1);
  531. }
  532. spin_unlock_bh(&tp->view.lock);
  533. /* Start keyboard reset command. */
  534. raw3270_request_reset(tp->kreset);
  535. raw3270_request_set_cmd(tp->kreset, TC_WRITE);
  536. raw3270_request_add_data(tp->kreset, &kreset_data, 1);
  537. raw3270_start(&tp->view, tp->kreset);
  538. while (len-- > 0)
  539. kbd_keycode(tp->kbd, *input++);
  540. /* Emit keycode for AID byte. */
  541. kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
  542. raw3270_request_reset(rrq);
  543. xchg(&tp->read, rrq);
  544. raw3270_put_view(&tp->view);
  545. }
  546. /*
  547. * Read request completion callback.
  548. */
  549. static void
  550. tty3270_read_callback(struct raw3270_request *rq, void *data)
  551. {
  552. struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
  553. raw3270_get_view(rq->view);
  554. /* Schedule tasklet to pass input to tty. */
  555. tasklet_schedule(&tp->readlet);
  556. }
  557. /*
  558. * Issue a read request. Call with device lock.
  559. */
  560. static void
  561. tty3270_issue_read(struct tty3270 *tp, int lock)
  562. {
  563. struct raw3270_request *rrq;
  564. int rc;
  565. rrq = xchg(&tp->read, 0);
  566. if (!rrq)
  567. /* Read already scheduled. */
  568. return;
  569. rrq->callback = tty3270_read_callback;
  570. rrq->callback_data = tp;
  571. raw3270_request_set_cmd(rrq, TC_READMOD);
  572. raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
  573. /* Issue the read modified request. */
  574. if (lock) {
  575. rc = raw3270_start(&tp->view, rrq);
  576. } else
  577. rc = raw3270_start_irq(&tp->view, rrq);
  578. if (rc) {
  579. raw3270_request_reset(rrq);
  580. xchg(&tp->read, rrq);
  581. }
  582. }
  583. /*
  584. * Hang up the tty
  585. */
  586. static void
  587. tty3270_hangup_tasklet(struct tty3270 *tp)
  588. {
  589. tty_port_tty_hangup(&tp->port, true);
  590. raw3270_put_view(&tp->view);
  591. }
  592. /*
  593. * Switch to the tty view.
  594. */
  595. static int
  596. tty3270_activate(struct raw3270_view *view)
  597. {
  598. struct tty3270 *tp = container_of(view, struct tty3270, view);
  599. tp->update_flags = TTY_UPDATE_ALL;
  600. tty3270_set_timer(tp, 1);
  601. return 0;
  602. }
  603. static void
  604. tty3270_deactivate(struct raw3270_view *view)
  605. {
  606. struct tty3270 *tp = container_of(view, struct tty3270, view);
  607. del_timer(&tp->timer);
  608. }
  609. static void
  610. tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
  611. {
  612. /* Handle ATTN. Schedule tasklet to read aid. */
  613. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  614. if (!tp->throttle)
  615. tty3270_issue_read(tp, 0);
  616. else
  617. tp->attn = 1;
  618. }
  619. if (rq) {
  620. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
  621. rq->rc = -EIO;
  622. raw3270_get_view(&tp->view);
  623. tasklet_schedule(&tp->hanglet);
  624. } else {
  625. /* Normal end. Copy residual count. */
  626. rq->rescnt = irb->scsw.cmd.count;
  627. }
  628. } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
  629. /* Interrupt without an outstanding request -> update all */
  630. tp->update_flags = TTY_UPDATE_ALL;
  631. tty3270_set_timer(tp, 1);
  632. }
  633. }
  634. /*
  635. * Allocate tty3270 structure.
  636. */
  637. static struct tty3270 *
  638. tty3270_alloc_view(void)
  639. {
  640. struct tty3270 *tp;
  641. int pages;
  642. tp = kzalloc(sizeof(struct tty3270), GFP_KERNEL);
  643. if (!tp)
  644. goto out_err;
  645. tp->freemem_pages =
  646. kmalloc_array(TTY3270_STRING_PAGES, sizeof(void *),
  647. GFP_KERNEL);
  648. if (!tp->freemem_pages)
  649. goto out_tp;
  650. INIT_LIST_HEAD(&tp->freemem);
  651. INIT_LIST_HEAD(&tp->lines);
  652. INIT_LIST_HEAD(&tp->update);
  653. INIT_LIST_HEAD(&tp->rcl_lines);
  654. tp->rcl_max = 20;
  655. for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
  656. tp->freemem_pages[pages] = (void *)
  657. __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
  658. if (!tp->freemem_pages[pages])
  659. goto out_pages;
  660. add_string_memory(&tp->freemem,
  661. tp->freemem_pages[pages], PAGE_SIZE);
  662. }
  663. tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
  664. if (IS_ERR(tp->write))
  665. goto out_pages;
  666. tp->read = raw3270_request_alloc(0);
  667. if (IS_ERR(tp->read))
  668. goto out_write;
  669. tp->kreset = raw3270_request_alloc(1);
  670. if (IS_ERR(tp->kreset))
  671. goto out_read;
  672. tp->kbd = kbd_alloc();
  673. if (!tp->kbd)
  674. goto out_reset;
  675. tty_port_init(&tp->port);
  676. timer_setup(&tp->timer, tty3270_update, 0);
  677. tasklet_init(&tp->readlet,
  678. (void (*)(unsigned long)) tty3270_read_tasklet,
  679. (unsigned long) tp->read);
  680. tasklet_init(&tp->hanglet,
  681. (void (*)(unsigned long)) tty3270_hangup_tasklet,
  682. (unsigned long) tp);
  683. INIT_WORK(&tp->resize_work, tty3270_resize_work);
  684. return tp;
  685. out_reset:
  686. raw3270_request_free(tp->kreset);
  687. out_read:
  688. raw3270_request_free(tp->read);
  689. out_write:
  690. raw3270_request_free(tp->write);
  691. out_pages:
  692. while (pages--)
  693. free_pages((unsigned long) tp->freemem_pages[pages], 0);
  694. kfree(tp->freemem_pages);
  695. tty_port_destroy(&tp->port);
  696. out_tp:
  697. kfree(tp);
  698. out_err:
  699. return ERR_PTR(-ENOMEM);
  700. }
  701. /*
  702. * Free tty3270 structure.
  703. */
  704. static void
  705. tty3270_free_view(struct tty3270 *tp)
  706. {
  707. int pages;
  708. kbd_free(tp->kbd);
  709. raw3270_request_free(tp->kreset);
  710. raw3270_request_free(tp->read);
  711. raw3270_request_free(tp->write);
  712. for (pages = 0; pages < TTY3270_STRING_PAGES; pages++)
  713. free_pages((unsigned long) tp->freemem_pages[pages], 0);
  714. kfree(tp->freemem_pages);
  715. tty_port_destroy(&tp->port);
  716. kfree(tp);
  717. }
  718. /*
  719. * Allocate tty3270 screen.
  720. */
  721. static struct tty3270_line *
  722. tty3270_alloc_screen(unsigned int rows, unsigned int cols)
  723. {
  724. struct tty3270_line *screen;
  725. unsigned long size;
  726. int lines;
  727. size = sizeof(struct tty3270_line) * (rows - 2);
  728. screen = kzalloc(size, GFP_KERNEL);
  729. if (!screen)
  730. goto out_err;
  731. for (lines = 0; lines < rows - 2; lines++) {
  732. size = sizeof(struct tty3270_cell) * cols;
  733. screen[lines].cells = kzalloc(size, GFP_KERNEL);
  734. if (!screen[lines].cells)
  735. goto out_screen;
  736. }
  737. return screen;
  738. out_screen:
  739. while (lines--)
  740. kfree(screen[lines].cells);
  741. kfree(screen);
  742. out_err:
  743. return ERR_PTR(-ENOMEM);
  744. }
  745. /*
  746. * Free tty3270 screen.
  747. */
  748. static void
  749. tty3270_free_screen(struct tty3270_line *screen, unsigned int rows)
  750. {
  751. int lines;
  752. for (lines = 0; lines < rows - 2; lines++)
  753. kfree(screen[lines].cells);
  754. kfree(screen);
  755. }
  756. /*
  757. * Resize tty3270 screen
  758. */
  759. static void tty3270_resize_work(struct work_struct *work)
  760. {
  761. struct tty3270 *tp = container_of(work, struct tty3270, resize_work);
  762. struct tty3270_line *screen, *oscreen;
  763. struct tty_struct *tty;
  764. unsigned int orows;
  765. struct winsize ws;
  766. screen = tty3270_alloc_screen(tp->n_rows, tp->n_cols);
  767. if (IS_ERR(screen))
  768. return;
  769. /* Switch to new output size */
  770. spin_lock_bh(&tp->view.lock);
  771. tty3270_blank_screen(tp);
  772. oscreen = tp->screen;
  773. orows = tp->view.rows;
  774. tp->view.model = tp->n_model;
  775. tp->view.rows = tp->n_rows;
  776. tp->view.cols = tp->n_cols;
  777. tp->screen = screen;
  778. free_string(&tp->freemem, tp->prompt);
  779. free_string(&tp->freemem, tp->status);
  780. tty3270_create_prompt(tp);
  781. tty3270_create_status(tp);
  782. while (tp->nr_lines < tp->view.rows - 2)
  783. tty3270_blank_line(tp);
  784. tp->update_flags = TTY_UPDATE_ALL;
  785. spin_unlock_bh(&tp->view.lock);
  786. tty3270_free_screen(oscreen, orows);
  787. tty3270_set_timer(tp, 1);
  788. /* Informat tty layer about new size */
  789. tty = tty_port_tty_get(&tp->port);
  790. if (!tty)
  791. return;
  792. ws.ws_row = tp->view.rows - 2;
  793. ws.ws_col = tp->view.cols;
  794. tty_do_resize(tty, &ws);
  795. tty_kref_put(tty);
  796. }
  797. static void
  798. tty3270_resize(struct raw3270_view *view, int model, int rows, int cols)
  799. {
  800. struct tty3270 *tp = container_of(view, struct tty3270, view);
  801. if (tp->n_model == model && tp->n_rows == rows && tp->n_cols == cols)
  802. return;
  803. tp->n_model = model;
  804. tp->n_rows = rows;
  805. tp->n_cols = cols;
  806. schedule_work(&tp->resize_work);
  807. }
  808. /*
  809. * Unlink tty3270 data structure from tty.
  810. */
  811. static void
  812. tty3270_release(struct raw3270_view *view)
  813. {
  814. struct tty3270 *tp = container_of(view, struct tty3270, view);
  815. struct tty_struct *tty = tty_port_tty_get(&tp->port);
  816. if (tty) {
  817. tty->driver_data = NULL;
  818. tty_port_tty_set(&tp->port, NULL);
  819. tty_hangup(tty);
  820. raw3270_put_view(&tp->view);
  821. tty_kref_put(tty);
  822. }
  823. }
  824. /*
  825. * Free tty3270 data structure
  826. */
  827. static void
  828. tty3270_free(struct raw3270_view *view)
  829. {
  830. struct tty3270 *tp = container_of(view, struct tty3270, view);
  831. del_timer_sync(&tp->timer);
  832. tty3270_free_screen(tp->screen, tp->view.rows);
  833. tty3270_free_view(tp);
  834. }
  835. /*
  836. * Delayed freeing of tty3270 views.
  837. */
  838. static void
  839. tty3270_del_views(void)
  840. {
  841. int i;
  842. for (i = RAW3270_FIRSTMINOR; i <= tty3270_max_index; i++) {
  843. struct raw3270_view *view = raw3270_find_view(&tty3270_fn, i);
  844. if (!IS_ERR(view))
  845. raw3270_del_view(view);
  846. }
  847. }
  848. static struct raw3270_fn tty3270_fn = {
  849. .activate = tty3270_activate,
  850. .deactivate = tty3270_deactivate,
  851. .intv = (void *) tty3270_irq,
  852. .release = tty3270_release,
  853. .free = tty3270_free,
  854. .resize = tty3270_resize
  855. };
  856. /*
  857. * This routine is called whenever a 3270 tty is opened first time.
  858. */
  859. static int tty3270_install(struct tty_driver *driver, struct tty_struct *tty)
  860. {
  861. struct raw3270_view *view;
  862. struct tty3270 *tp;
  863. int i, rc;
  864. /* Check if the tty3270 is already there. */
  865. view = raw3270_find_view(&tty3270_fn, tty->index + RAW3270_FIRSTMINOR);
  866. if (!IS_ERR(view)) {
  867. tp = container_of(view, struct tty3270, view);
  868. tty->driver_data = tp;
  869. tty->winsize.ws_row = tp->view.rows - 2;
  870. tty->winsize.ws_col = tp->view.cols;
  871. tp->port.low_latency = 0;
  872. tp->inattr = TF_INPUT;
  873. goto port_install;
  874. }
  875. if (tty3270_max_index < tty->index + 1)
  876. tty3270_max_index = tty->index + 1;
  877. /* Allocate tty3270 structure on first open. */
  878. tp = tty3270_alloc_view();
  879. if (IS_ERR(tp))
  880. return PTR_ERR(tp);
  881. rc = raw3270_add_view(&tp->view, &tty3270_fn,
  882. tty->index + RAW3270_FIRSTMINOR,
  883. RAW3270_VIEW_LOCK_BH);
  884. if (rc) {
  885. tty3270_free_view(tp);
  886. return rc;
  887. }
  888. tp->screen = tty3270_alloc_screen(tp->view.rows, tp->view.cols);
  889. if (IS_ERR(tp->screen)) {
  890. rc = PTR_ERR(tp->screen);
  891. raw3270_put_view(&tp->view);
  892. raw3270_del_view(&tp->view);
  893. tty3270_free_view(tp);
  894. return rc;
  895. }
  896. tp->port.low_latency = 0;
  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 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->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. * Returns the number of characters in the output buffer. This is
  1603. * used in tty_wait_until_sent to wait until all characters have
  1604. * appeared on the screen.
  1605. */
  1606. static int
  1607. tty3270_chars_in_buffer(struct tty_struct *tty)
  1608. {
  1609. return 0;
  1610. }
  1611. static void
  1612. tty3270_flush_buffer(struct tty_struct *tty)
  1613. {
  1614. }
  1615. /*
  1616. * Check for visible/invisible input switches
  1617. */
  1618. static void
  1619. tty3270_set_termios(struct tty_struct *tty, struct ktermios *old)
  1620. {
  1621. struct tty3270 *tp;
  1622. int new;
  1623. tp = tty->driver_data;
  1624. if (!tp)
  1625. return;
  1626. spin_lock_bh(&tp->view.lock);
  1627. if (L_ICANON(tty)) {
  1628. new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
  1629. if (new != tp->inattr) {
  1630. tp->inattr = new;
  1631. tty3270_update_prompt(tp, NULL, 0);
  1632. tty3270_set_timer(tp, 1);
  1633. }
  1634. }
  1635. spin_unlock_bh(&tp->view.lock);
  1636. }
  1637. /*
  1638. * Disable reading from a 3270 tty
  1639. */
  1640. static void
  1641. tty3270_throttle(struct tty_struct * tty)
  1642. {
  1643. struct tty3270 *tp;
  1644. tp = tty->driver_data;
  1645. if (!tp)
  1646. return;
  1647. tp->throttle = 1;
  1648. }
  1649. /*
  1650. * Enable reading from a 3270 tty
  1651. */
  1652. static void
  1653. tty3270_unthrottle(struct tty_struct * tty)
  1654. {
  1655. struct tty3270 *tp;
  1656. tp = tty->driver_data;
  1657. if (!tp)
  1658. return;
  1659. tp->throttle = 0;
  1660. if (tp->attn)
  1661. tty3270_issue_read(tp, 1);
  1662. }
  1663. /*
  1664. * Hang up the tty device.
  1665. */
  1666. static void
  1667. tty3270_hangup(struct tty_struct *tty)
  1668. {
  1669. struct tty3270 *tp;
  1670. tp = tty->driver_data;
  1671. if (!tp)
  1672. return;
  1673. spin_lock_bh(&tp->view.lock);
  1674. tp->cx = tp->saved_cx = 0;
  1675. tp->cy = tp->saved_cy = 0;
  1676. tp->highlight = tp->saved_highlight = TAX_RESET;
  1677. tp->f_color = tp->saved_f_color = TAC_RESET;
  1678. tty3270_blank_screen(tp);
  1679. while (tp->nr_lines < tp->view.rows - 2)
  1680. tty3270_blank_line(tp);
  1681. tp->update_flags = TTY_UPDATE_ALL;
  1682. spin_unlock_bh(&tp->view.lock);
  1683. tty3270_set_timer(tp, 1);
  1684. }
  1685. static void
  1686. tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
  1687. {
  1688. }
  1689. static int tty3270_ioctl(struct tty_struct *tty, unsigned int cmd,
  1690. unsigned long arg)
  1691. {
  1692. struct tty3270 *tp;
  1693. tp = tty->driver_data;
  1694. if (!tp)
  1695. return -ENODEV;
  1696. if (tty_io_error(tty))
  1697. return -EIO;
  1698. return kbd_ioctl(tp->kbd, cmd, arg);
  1699. }
  1700. #ifdef CONFIG_COMPAT
  1701. static long tty3270_compat_ioctl(struct tty_struct *tty,
  1702. unsigned int cmd, unsigned long arg)
  1703. {
  1704. struct tty3270 *tp;
  1705. tp = tty->driver_data;
  1706. if (!tp)
  1707. return -ENODEV;
  1708. if (tty_io_error(tty))
  1709. return -EIO;
  1710. return kbd_ioctl(tp->kbd, cmd, (unsigned long)compat_ptr(arg));
  1711. }
  1712. #endif
  1713. static const struct tty_operations tty3270_ops = {
  1714. .install = tty3270_install,
  1715. .cleanup = tty3270_cleanup,
  1716. .open = tty3270_open,
  1717. .close = tty3270_close,
  1718. .write = tty3270_write,
  1719. .put_char = tty3270_put_char,
  1720. .flush_chars = tty3270_flush_chars,
  1721. .write_room = tty3270_write_room,
  1722. .chars_in_buffer = tty3270_chars_in_buffer,
  1723. .flush_buffer = tty3270_flush_buffer,
  1724. .throttle = tty3270_throttle,
  1725. .unthrottle = tty3270_unthrottle,
  1726. .hangup = tty3270_hangup,
  1727. .wait_until_sent = tty3270_wait_until_sent,
  1728. .ioctl = tty3270_ioctl,
  1729. #ifdef CONFIG_COMPAT
  1730. .compat_ioctl = tty3270_compat_ioctl,
  1731. #endif
  1732. .set_termios = tty3270_set_termios
  1733. };
  1734. static void tty3270_create_cb(int minor)
  1735. {
  1736. tty_register_device(tty3270_driver, minor - RAW3270_FIRSTMINOR, NULL);
  1737. }
  1738. static void tty3270_destroy_cb(int minor)
  1739. {
  1740. tty_unregister_device(tty3270_driver, minor - RAW3270_FIRSTMINOR);
  1741. }
  1742. static struct raw3270_notifier tty3270_notifier =
  1743. {
  1744. .create = tty3270_create_cb,
  1745. .destroy = tty3270_destroy_cb,
  1746. };
  1747. /*
  1748. * 3270 tty registration code called from tty_init().
  1749. * Most kernel services (incl. kmalloc) are available at this poimt.
  1750. */
  1751. static int __init tty3270_init(void)
  1752. {
  1753. struct tty_driver *driver;
  1754. int ret;
  1755. driver = tty_alloc_driver(RAW3270_MAXDEVS,
  1756. TTY_DRIVER_REAL_RAW |
  1757. TTY_DRIVER_DYNAMIC_DEV |
  1758. TTY_DRIVER_RESET_TERMIOS);
  1759. if (IS_ERR(driver))
  1760. return PTR_ERR(driver);
  1761. /*
  1762. * Initialize the tty_driver structure
  1763. * Entries in tty3270_driver that are NOT initialized:
  1764. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1765. */
  1766. driver->driver_name = "tty3270";
  1767. driver->name = "3270/tty";
  1768. driver->major = IBM_TTY3270_MAJOR;
  1769. driver->minor_start = RAW3270_FIRSTMINOR;
  1770. driver->name_base = RAW3270_FIRSTMINOR;
  1771. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1772. driver->subtype = SYSTEM_TYPE_TTY;
  1773. driver->init_termios = tty_std_termios;
  1774. tty_set_operations(driver, &tty3270_ops);
  1775. ret = tty_register_driver(driver);
  1776. if (ret) {
  1777. put_tty_driver(driver);
  1778. return ret;
  1779. }
  1780. tty3270_driver = driver;
  1781. raw3270_register_notifier(&tty3270_notifier);
  1782. return 0;
  1783. }
  1784. static void __exit
  1785. tty3270_exit(void)
  1786. {
  1787. struct tty_driver *driver;
  1788. raw3270_unregister_notifier(&tty3270_notifier);
  1789. driver = tty3270_driver;
  1790. tty3270_driver = NULL;
  1791. tty_unregister_driver(driver);
  1792. put_tty_driver(driver);
  1793. tty3270_del_views();
  1794. }
  1795. MODULE_LICENSE("GPL");
  1796. MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
  1797. module_init(tty3270_init);
  1798. module_exit(tty3270_exit);