tty3270.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  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/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, 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. if (rc) {
  884. tty3270_free_view(tp);
  885. return rc;
  886. }
  887. tp->screen = tty3270_alloc_screen(tp->view.rows, tp->view.cols);
  888. if (IS_ERR(tp->screen)) {
  889. rc = PTR_ERR(tp->screen);
  890. raw3270_put_view(&tp->view);
  891. raw3270_del_view(&tp->view);
  892. tty3270_free_view(tp);
  893. return rc;
  894. }
  895. tp->port.low_latency = 0;
  896. tty->winsize.ws_row = tp->view.rows - 2;
  897. tty->winsize.ws_col = tp->view.cols;
  898. tty3270_create_prompt(tp);
  899. tty3270_create_status(tp);
  900. tty3270_update_status(tp);
  901. /* Create blank line for every line in the tty output area. */
  902. for (i = 0; i < tp->view.rows - 2; i++)
  903. tty3270_blank_line(tp);
  904. tp->kbd->port = &tp->port;
  905. tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
  906. tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
  907. tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
  908. tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
  909. kbd_ascebc(tp->kbd, tp->view.ascebc);
  910. raw3270_activate_view(&tp->view);
  911. port_install:
  912. rc = tty_port_install(&tp->port, driver, tty);
  913. if (rc) {
  914. raw3270_put_view(&tp->view);
  915. return rc;
  916. }
  917. tty->driver_data = tp;
  918. return 0;
  919. }
  920. /*
  921. * This routine is called whenever a 3270 tty is opened.
  922. */
  923. static int
  924. tty3270_open(struct tty_struct *tty, struct file *filp)
  925. {
  926. struct tty3270 *tp = tty->driver_data;
  927. struct tty_port *port = &tp->port;
  928. port->count++;
  929. tty_port_tty_set(port, tty);
  930. return 0;
  931. }
  932. /*
  933. * This routine is called when the 3270 tty is closed. We wait
  934. * for the remaining request to be completed. Then we clean up.
  935. */
  936. static void
  937. tty3270_close(struct tty_struct *tty, struct file * filp)
  938. {
  939. struct tty3270 *tp = tty->driver_data;
  940. if (tty->count > 1)
  941. return;
  942. if (tp)
  943. tty_port_tty_set(&tp->port, NULL);
  944. }
  945. static void tty3270_cleanup(struct tty_struct *tty)
  946. {
  947. struct tty3270 *tp = tty->driver_data;
  948. if (tp) {
  949. tty->driver_data = NULL;
  950. raw3270_put_view(&tp->view);
  951. }
  952. }
  953. /*
  954. * We always have room.
  955. */
  956. static int
  957. tty3270_write_room(struct tty_struct *tty)
  958. {
  959. return INT_MAX;
  960. }
  961. /*
  962. * Insert character into the screen at the current position with the
  963. * current color and highlight. This function does NOT do cursor movement.
  964. */
  965. static void tty3270_put_character(struct tty3270 *tp, char ch)
  966. {
  967. struct tty3270_line *line;
  968. struct tty3270_cell *cell;
  969. line = tp->screen + tp->cy;
  970. if (line->len <= tp->cx) {
  971. while (line->len < tp->cx) {
  972. cell = line->cells + line->len;
  973. cell->character = tp->view.ascebc[' '];
  974. cell->highlight = tp->highlight;
  975. cell->f_color = tp->f_color;
  976. line->len++;
  977. }
  978. line->len++;
  979. }
  980. cell = line->cells + tp->cx;
  981. cell->character = tp->view.ascebc[(unsigned int) ch];
  982. cell->highlight = tp->highlight;
  983. cell->f_color = tp->f_color;
  984. }
  985. /*
  986. * Convert a tty3270_line to a 3270 data fragment usable for output.
  987. */
  988. static void
  989. tty3270_convert_line(struct tty3270 *tp, int line_nr)
  990. {
  991. struct tty3270_line *line;
  992. struct tty3270_cell *cell;
  993. struct string *s, *n;
  994. unsigned char highlight;
  995. unsigned char f_color;
  996. char *cp;
  997. int flen, i;
  998. /* Determine how long the fragment will be. */
  999. flen = 3; /* Prefix (TO_SBA). */
  1000. line = tp->screen + line_nr;
  1001. flen += line->len;
  1002. highlight = TAX_RESET;
  1003. f_color = TAC_RESET;
  1004. for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
  1005. if (cell->highlight != highlight) {
  1006. flen += 3; /* TO_SA to switch highlight. */
  1007. highlight = cell->highlight;
  1008. }
  1009. if (cell->f_color != f_color) {
  1010. flen += 3; /* TO_SA to switch color. */
  1011. f_color = cell->f_color;
  1012. }
  1013. }
  1014. if (highlight != TAX_RESET)
  1015. flen += 3; /* TO_SA to reset hightlight. */
  1016. if (f_color != TAC_RESET)
  1017. flen += 3; /* TO_SA to reset color. */
  1018. if (line->len < tp->view.cols)
  1019. flen += 4; /* Postfix (TO_RA). */
  1020. /* Find the line in the list. */
  1021. i = tp->view.rows - 2 - line_nr;
  1022. list_for_each_entry_reverse(s, &tp->lines, list)
  1023. if (--i <= 0)
  1024. break;
  1025. /*
  1026. * Check if the line needs to get reallocated.
  1027. */
  1028. if (s->len != flen) {
  1029. /* Reallocate string. */
  1030. n = tty3270_alloc_string(tp, flen);
  1031. list_add(&n->list, &s->list);
  1032. list_del_init(&s->list);
  1033. if (!list_empty(&s->update))
  1034. list_del_init(&s->update);
  1035. free_string(&tp->freemem, s);
  1036. s = n;
  1037. }
  1038. /* Write 3270 data fragment. */
  1039. cp = s->string;
  1040. *cp++ = TO_SBA;
  1041. *cp++ = 0;
  1042. *cp++ = 0;
  1043. highlight = TAX_RESET;
  1044. f_color = TAC_RESET;
  1045. for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
  1046. if (cell->highlight != highlight) {
  1047. *cp++ = TO_SA;
  1048. *cp++ = TAT_EXTHI;
  1049. *cp++ = cell->highlight;
  1050. highlight = cell->highlight;
  1051. }
  1052. if (cell->f_color != f_color) {
  1053. *cp++ = TO_SA;
  1054. *cp++ = TAT_COLOR;
  1055. *cp++ = cell->f_color;
  1056. f_color = cell->f_color;
  1057. }
  1058. *cp++ = cell->character;
  1059. }
  1060. if (highlight != TAX_RESET) {
  1061. *cp++ = TO_SA;
  1062. *cp++ = TAT_EXTHI;
  1063. *cp++ = TAX_RESET;
  1064. }
  1065. if (f_color != TAC_RESET) {
  1066. *cp++ = TO_SA;
  1067. *cp++ = TAT_COLOR;
  1068. *cp++ = TAC_RESET;
  1069. }
  1070. if (line->len < tp->view.cols) {
  1071. *cp++ = TO_RA;
  1072. *cp++ = 0;
  1073. *cp++ = 0;
  1074. *cp++ = 0;
  1075. }
  1076. if (tp->nr_up + line_nr < tp->view.rows - 2) {
  1077. /* Line is currently visible on screen. */
  1078. tty3270_update_string(tp, s, line_nr);
  1079. /* Add line to update list. */
  1080. if (list_empty(&s->update)) {
  1081. list_add_tail(&s->update, &tp->update);
  1082. tp->update_flags |= TTY_UPDATE_LIST;
  1083. }
  1084. }
  1085. }
  1086. /*
  1087. * Do carriage return.
  1088. */
  1089. static void
  1090. tty3270_cr(struct tty3270 *tp)
  1091. {
  1092. tp->cx = 0;
  1093. }
  1094. /*
  1095. * Do line feed.
  1096. */
  1097. static void
  1098. tty3270_lf(struct tty3270 *tp)
  1099. {
  1100. struct tty3270_line temp;
  1101. int i;
  1102. tty3270_convert_line(tp, tp->cy);
  1103. if (tp->cy < tp->view.rows - 3) {
  1104. tp->cy++;
  1105. return;
  1106. }
  1107. /* Last line just filled up. Add new, blank line. */
  1108. tty3270_blank_line(tp);
  1109. temp = tp->screen[0];
  1110. temp.len = 0;
  1111. for (i = 0; i < tp->view.rows - 3; i++)
  1112. tp->screen[i] = tp->screen[i+1];
  1113. tp->screen[tp->view.rows - 3] = temp;
  1114. tty3270_rebuild_update(tp);
  1115. }
  1116. static void
  1117. tty3270_ri(struct tty3270 *tp)
  1118. {
  1119. if (tp->cy > 0) {
  1120. tty3270_convert_line(tp, tp->cy);
  1121. tp->cy--;
  1122. }
  1123. }
  1124. /*
  1125. * Insert characters at current position.
  1126. */
  1127. static void
  1128. tty3270_insert_characters(struct tty3270 *tp, int n)
  1129. {
  1130. struct tty3270_line *line;
  1131. int k;
  1132. line = tp->screen + tp->cy;
  1133. while (line->len < tp->cx) {
  1134. line->cells[line->len].character = tp->view.ascebc[' '];
  1135. line->cells[line->len].highlight = TAX_RESET;
  1136. line->cells[line->len].f_color = TAC_RESET;
  1137. line->len++;
  1138. }
  1139. if (n > tp->view.cols - tp->cx)
  1140. n = tp->view.cols - tp->cx;
  1141. k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
  1142. while (k--)
  1143. line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
  1144. line->len += n;
  1145. if (line->len > tp->view.cols)
  1146. line->len = tp->view.cols;
  1147. while (n-- > 0) {
  1148. line->cells[tp->cx + n].character = tp->view.ascebc[' '];
  1149. line->cells[tp->cx + n].highlight = tp->highlight;
  1150. line->cells[tp->cx + n].f_color = tp->f_color;
  1151. }
  1152. }
  1153. /*
  1154. * Delete characters at current position.
  1155. */
  1156. static void
  1157. tty3270_delete_characters(struct tty3270 *tp, int n)
  1158. {
  1159. struct tty3270_line *line;
  1160. int i;
  1161. line = tp->screen + tp->cy;
  1162. if (line->len <= tp->cx)
  1163. return;
  1164. if (line->len - tp->cx <= n) {
  1165. line->len = tp->cx;
  1166. return;
  1167. }
  1168. for (i = tp->cx; i + n < line->len; i++)
  1169. line->cells[i] = line->cells[i + n];
  1170. line->len -= n;
  1171. }
  1172. /*
  1173. * Erase characters at current position.
  1174. */
  1175. static void
  1176. tty3270_erase_characters(struct tty3270 *tp, int n)
  1177. {
  1178. struct tty3270_line *line;
  1179. struct tty3270_cell *cell;
  1180. line = tp->screen + tp->cy;
  1181. while (line->len > tp->cx && n-- > 0) {
  1182. cell = line->cells + tp->cx++;
  1183. cell->character = ' ';
  1184. cell->highlight = TAX_RESET;
  1185. cell->f_color = TAC_RESET;
  1186. }
  1187. tp->cx += n;
  1188. tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
  1189. }
  1190. /*
  1191. * Erase line, 3 different cases:
  1192. * Esc [ 0 K Erase from current position to end of line inclusive
  1193. * Esc [ 1 K Erase from beginning of line to current position inclusive
  1194. * Esc [ 2 K Erase entire line (without moving cursor)
  1195. */
  1196. static void
  1197. tty3270_erase_line(struct tty3270 *tp, int mode)
  1198. {
  1199. struct tty3270_line *line;
  1200. struct tty3270_cell *cell;
  1201. int i;
  1202. line = tp->screen + tp->cy;
  1203. if (mode == 0)
  1204. line->len = tp->cx;
  1205. else if (mode == 1) {
  1206. for (i = 0; i < tp->cx; i++) {
  1207. cell = line->cells + i;
  1208. cell->character = ' ';
  1209. cell->highlight = TAX_RESET;
  1210. cell->f_color = TAC_RESET;
  1211. }
  1212. if (line->len <= tp->cx)
  1213. line->len = tp->cx + 1;
  1214. } else if (mode == 2)
  1215. line->len = 0;
  1216. tty3270_convert_line(tp, tp->cy);
  1217. }
  1218. /*
  1219. * Erase display, 3 different cases:
  1220. * Esc [ 0 J Erase from current position to bottom of screen inclusive
  1221. * Esc [ 1 J Erase from top of screen to current position inclusive
  1222. * Esc [ 2 J Erase entire screen (without moving the cursor)
  1223. */
  1224. static void
  1225. tty3270_erase_display(struct tty3270 *tp, int mode)
  1226. {
  1227. int i;
  1228. if (mode == 0) {
  1229. tty3270_erase_line(tp, 0);
  1230. for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
  1231. tp->screen[i].len = 0;
  1232. tty3270_convert_line(tp, i);
  1233. }
  1234. } else if (mode == 1) {
  1235. for (i = 0; i < tp->cy; i++) {
  1236. tp->screen[i].len = 0;
  1237. tty3270_convert_line(tp, i);
  1238. }
  1239. tty3270_erase_line(tp, 1);
  1240. } else if (mode == 2) {
  1241. for (i = 0; i < tp->view.rows - 2; i++) {
  1242. tp->screen[i].len = 0;
  1243. tty3270_convert_line(tp, i);
  1244. }
  1245. }
  1246. tty3270_rebuild_update(tp);
  1247. }
  1248. /*
  1249. * Set attributes found in an escape sequence.
  1250. * Esc [ <attr> ; <attr> ; ... m
  1251. */
  1252. static void
  1253. tty3270_set_attributes(struct tty3270 *tp)
  1254. {
  1255. static unsigned char f_colors[] = {
  1256. TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
  1257. TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
  1258. };
  1259. int i, attr;
  1260. for (i = 0; i <= tp->esc_npar; i++) {
  1261. attr = tp->esc_par[i];
  1262. switch (attr) {
  1263. case 0: /* Reset */
  1264. tp->highlight = TAX_RESET;
  1265. tp->f_color = TAC_RESET;
  1266. break;
  1267. /* Highlight. */
  1268. case 4: /* Start underlining. */
  1269. tp->highlight = TAX_UNDER;
  1270. break;
  1271. case 5: /* Start blink. */
  1272. tp->highlight = TAX_BLINK;
  1273. break;
  1274. case 7: /* Start reverse. */
  1275. tp->highlight = TAX_REVER;
  1276. break;
  1277. case 24: /* End underlining */
  1278. if (tp->highlight == TAX_UNDER)
  1279. tp->highlight = TAX_RESET;
  1280. break;
  1281. case 25: /* End blink. */
  1282. if (tp->highlight == TAX_BLINK)
  1283. tp->highlight = TAX_RESET;
  1284. break;
  1285. case 27: /* End reverse. */
  1286. if (tp->highlight == TAX_REVER)
  1287. tp->highlight = TAX_RESET;
  1288. break;
  1289. /* Foreground color. */
  1290. case 30: /* Black */
  1291. case 31: /* Red */
  1292. case 32: /* Green */
  1293. case 33: /* Yellow */
  1294. case 34: /* Blue */
  1295. case 35: /* Magenta */
  1296. case 36: /* Cyan */
  1297. case 37: /* White */
  1298. case 39: /* Black */
  1299. tp->f_color = f_colors[attr - 30];
  1300. break;
  1301. }
  1302. }
  1303. }
  1304. static inline int
  1305. tty3270_getpar(struct tty3270 *tp, int ix)
  1306. {
  1307. return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
  1308. }
  1309. static void
  1310. tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
  1311. {
  1312. int max_cx = max(0, cx);
  1313. int max_cy = max(0, cy);
  1314. tp->cx = min_t(int, tp->view.cols - 1, max_cx);
  1315. cy = min_t(int, tp->view.rows - 3, max_cy);
  1316. if (cy != tp->cy) {
  1317. tty3270_convert_line(tp, tp->cy);
  1318. tp->cy = cy;
  1319. }
  1320. }
  1321. /*
  1322. * Process escape sequences. Known sequences:
  1323. * Esc 7 Save Cursor Position
  1324. * Esc 8 Restore Cursor Position
  1325. * Esc [ Pn ; Pn ; .. m Set attributes
  1326. * Esc [ Pn ; Pn H Cursor Position
  1327. * Esc [ Pn ; Pn f Cursor Position
  1328. * Esc [ Pn A Cursor Up
  1329. * Esc [ Pn B Cursor Down
  1330. * Esc [ Pn C Cursor Forward
  1331. * Esc [ Pn D Cursor Backward
  1332. * Esc [ Pn G Cursor Horizontal Absolute
  1333. * Esc [ Pn X Erase Characters
  1334. * Esc [ Ps J Erase in Display
  1335. * Esc [ Ps K Erase in Line
  1336. * // FIXME: add all the new ones.
  1337. *
  1338. * Pn is a numeric parameter, a string of zero or more decimal digits.
  1339. * Ps is a selective parameter.
  1340. */
  1341. static void
  1342. tty3270_escape_sequence(struct tty3270 *tp, char ch)
  1343. {
  1344. enum { ESnormal, ESesc, ESsquare, ESgetpars };
  1345. if (tp->esc_state == ESnormal) {
  1346. if (ch == 0x1b)
  1347. /* Starting new escape sequence. */
  1348. tp->esc_state = ESesc;
  1349. return;
  1350. }
  1351. if (tp->esc_state == ESesc) {
  1352. tp->esc_state = ESnormal;
  1353. switch (ch) {
  1354. case '[':
  1355. tp->esc_state = ESsquare;
  1356. break;
  1357. case 'E':
  1358. tty3270_cr(tp);
  1359. tty3270_lf(tp);
  1360. break;
  1361. case 'M':
  1362. tty3270_ri(tp);
  1363. break;
  1364. case 'D':
  1365. tty3270_lf(tp);
  1366. break;
  1367. case 'Z': /* Respond ID. */
  1368. kbd_puts_queue(&tp->port, "\033[?6c");
  1369. break;
  1370. case '7': /* Save cursor position. */
  1371. tp->saved_cx = tp->cx;
  1372. tp->saved_cy = tp->cy;
  1373. tp->saved_highlight = tp->highlight;
  1374. tp->saved_f_color = tp->f_color;
  1375. break;
  1376. case '8': /* Restore cursor position. */
  1377. tty3270_convert_line(tp, tp->cy);
  1378. tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
  1379. tp->highlight = tp->saved_highlight;
  1380. tp->f_color = tp->saved_f_color;
  1381. break;
  1382. case 'c': /* Reset terminal. */
  1383. tp->cx = tp->saved_cx = 0;
  1384. tp->cy = tp->saved_cy = 0;
  1385. tp->highlight = tp->saved_highlight = TAX_RESET;
  1386. tp->f_color = tp->saved_f_color = TAC_RESET;
  1387. tty3270_erase_display(tp, 2);
  1388. break;
  1389. }
  1390. return;
  1391. }
  1392. if (tp->esc_state == ESsquare) {
  1393. tp->esc_state = ESgetpars;
  1394. memset(tp->esc_par, 0, sizeof(tp->esc_par));
  1395. tp->esc_npar = 0;
  1396. tp->esc_ques = (ch == '?');
  1397. if (tp->esc_ques)
  1398. return;
  1399. }
  1400. if (tp->esc_state == ESgetpars) {
  1401. if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
  1402. tp->esc_npar++;
  1403. return;
  1404. }
  1405. if (ch >= '0' && ch <= '9') {
  1406. tp->esc_par[tp->esc_npar] *= 10;
  1407. tp->esc_par[tp->esc_npar] += ch - '0';
  1408. return;
  1409. }
  1410. }
  1411. tp->esc_state = ESnormal;
  1412. if (ch == 'n' && !tp->esc_ques) {
  1413. if (tp->esc_par[0] == 5) /* Status report. */
  1414. kbd_puts_queue(&tp->port, "\033[0n");
  1415. else if (tp->esc_par[0] == 6) { /* Cursor report. */
  1416. char buf[40];
  1417. sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
  1418. kbd_puts_queue(&tp->port, buf);
  1419. }
  1420. return;
  1421. }
  1422. if (tp->esc_ques)
  1423. return;
  1424. switch (ch) {
  1425. case 'm':
  1426. tty3270_set_attributes(tp);
  1427. break;
  1428. case 'H': /* Set cursor position. */
  1429. case 'f':
  1430. tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
  1431. tty3270_getpar(tp, 0) - 1);
  1432. break;
  1433. case 'd': /* Set y position. */
  1434. tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
  1435. break;
  1436. case 'A': /* Cursor up. */
  1437. case 'F':
  1438. tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
  1439. break;
  1440. case 'B': /* Cursor down. */
  1441. case 'e':
  1442. case 'E':
  1443. tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
  1444. break;
  1445. case 'C': /* Cursor forward. */
  1446. case 'a':
  1447. tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
  1448. break;
  1449. case 'D': /* Cursor backward. */
  1450. tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
  1451. break;
  1452. case 'G': /* Set x position. */
  1453. case '`':
  1454. tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
  1455. break;
  1456. case 'X': /* Erase Characters. */
  1457. tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
  1458. break;
  1459. case 'J': /* Erase display. */
  1460. tty3270_erase_display(tp, tp->esc_par[0]);
  1461. break;
  1462. case 'K': /* Erase line. */
  1463. tty3270_erase_line(tp, tp->esc_par[0]);
  1464. break;
  1465. case 'P': /* Delete characters. */
  1466. tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
  1467. break;
  1468. case '@': /* Insert characters. */
  1469. tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
  1470. break;
  1471. case 's': /* Save cursor position. */
  1472. tp->saved_cx = tp->cx;
  1473. tp->saved_cy = tp->cy;
  1474. tp->saved_highlight = tp->highlight;
  1475. tp->saved_f_color = tp->f_color;
  1476. break;
  1477. case 'u': /* Restore cursor position. */
  1478. tty3270_convert_line(tp, tp->cy);
  1479. tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
  1480. tp->highlight = tp->saved_highlight;
  1481. tp->f_color = tp->saved_f_color;
  1482. break;
  1483. }
  1484. }
  1485. /*
  1486. * String write routine for 3270 ttys
  1487. */
  1488. static void
  1489. tty3270_do_write(struct tty3270 *tp, struct tty_struct *tty,
  1490. const unsigned char *buf, int count)
  1491. {
  1492. int i_msg, i;
  1493. spin_lock_bh(&tp->view.lock);
  1494. for (i_msg = 0; !tty->stopped && i_msg < count; i_msg++) {
  1495. if (tp->esc_state != 0) {
  1496. /* Continue escape sequence. */
  1497. tty3270_escape_sequence(tp, buf[i_msg]);
  1498. continue;
  1499. }
  1500. switch (buf[i_msg]) {
  1501. case 0x07: /* '\a' -- Alarm */
  1502. tp->wcc |= TW_PLUSALARM;
  1503. break;
  1504. case 0x08: /* Backspace. */
  1505. if (tp->cx > 0) {
  1506. tp->cx--;
  1507. tty3270_put_character(tp, ' ');
  1508. }
  1509. break;
  1510. case 0x09: /* '\t' -- Tabulate */
  1511. for (i = tp->cx % 8; i < 8; i++) {
  1512. if (tp->cx >= tp->view.cols) {
  1513. tty3270_cr(tp);
  1514. tty3270_lf(tp);
  1515. break;
  1516. }
  1517. tty3270_put_character(tp, ' ');
  1518. tp->cx++;
  1519. }
  1520. break;
  1521. case 0x0a: /* '\n' -- New Line */
  1522. tty3270_cr(tp);
  1523. tty3270_lf(tp);
  1524. break;
  1525. case 0x0c: /* '\f' -- Form Feed */
  1526. tty3270_erase_display(tp, 2);
  1527. tp->cx = tp->cy = 0;
  1528. break;
  1529. case 0x0d: /* '\r' -- Carriage Return */
  1530. tp->cx = 0;
  1531. break;
  1532. case 0x0f: /* SuSE "exit alternate mode" */
  1533. break;
  1534. case 0x1b: /* Start escape sequence. */
  1535. tty3270_escape_sequence(tp, buf[i_msg]);
  1536. break;
  1537. default: /* Insert normal character. */
  1538. if (tp->cx >= tp->view.cols) {
  1539. tty3270_cr(tp);
  1540. tty3270_lf(tp);
  1541. }
  1542. tty3270_put_character(tp, buf[i_msg]);
  1543. tp->cx++;
  1544. break;
  1545. }
  1546. }
  1547. /* Convert current line to 3270 data fragment. */
  1548. tty3270_convert_line(tp, tp->cy);
  1549. /* Setup timer to update display after 1/10 second */
  1550. if (!timer_pending(&tp->timer))
  1551. tty3270_set_timer(tp, HZ/10);
  1552. spin_unlock_bh(&tp->view.lock);
  1553. }
  1554. /*
  1555. * String write routine for 3270 ttys
  1556. */
  1557. static int
  1558. tty3270_write(struct tty_struct * tty,
  1559. const unsigned char *buf, int count)
  1560. {
  1561. struct tty3270 *tp;
  1562. tp = tty->driver_data;
  1563. if (!tp)
  1564. return 0;
  1565. if (tp->char_count > 0) {
  1566. tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
  1567. tp->char_count = 0;
  1568. }
  1569. tty3270_do_write(tp, tty, buf, count);
  1570. return count;
  1571. }
  1572. /*
  1573. * Put single characters to the ttys character buffer
  1574. */
  1575. static int tty3270_put_char(struct tty_struct *tty, unsigned char ch)
  1576. {
  1577. struct tty3270 *tp;
  1578. tp = tty->driver_data;
  1579. if (!tp || tp->char_count >= TTY3270_CHAR_BUF_SIZE)
  1580. return 0;
  1581. tp->char_buf[tp->char_count++] = ch;
  1582. return 1;
  1583. }
  1584. /*
  1585. * Flush all characters from the ttys characeter buffer put there
  1586. * by tty3270_put_char.
  1587. */
  1588. static void
  1589. tty3270_flush_chars(struct tty_struct *tty)
  1590. {
  1591. struct tty3270 *tp;
  1592. tp = tty->driver_data;
  1593. if (!tp)
  1594. return;
  1595. if (tp->char_count > 0) {
  1596. tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
  1597. tp->char_count = 0;
  1598. }
  1599. }
  1600. /*
  1601. * Returns the number of characters in the output buffer. This is
  1602. * used in tty_wait_until_sent to wait until all characters have
  1603. * appeared on the screen.
  1604. */
  1605. static int
  1606. tty3270_chars_in_buffer(struct tty_struct *tty)
  1607. {
  1608. return 0;
  1609. }
  1610. static void
  1611. tty3270_flush_buffer(struct tty_struct *tty)
  1612. {
  1613. }
  1614. /*
  1615. * Check for visible/invisible input switches
  1616. */
  1617. static void
  1618. tty3270_set_termios(struct tty_struct *tty, struct ktermios *old)
  1619. {
  1620. struct tty3270 *tp;
  1621. int new;
  1622. tp = tty->driver_data;
  1623. if (!tp)
  1624. return;
  1625. spin_lock_bh(&tp->view.lock);
  1626. if (L_ICANON(tty)) {
  1627. new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
  1628. if (new != tp->inattr) {
  1629. tp->inattr = new;
  1630. tty3270_update_prompt(tp, NULL, 0);
  1631. tty3270_set_timer(tp, 1);
  1632. }
  1633. }
  1634. spin_unlock_bh(&tp->view.lock);
  1635. }
  1636. /*
  1637. * Disable reading from a 3270 tty
  1638. */
  1639. static void
  1640. tty3270_throttle(struct tty_struct * tty)
  1641. {
  1642. struct tty3270 *tp;
  1643. tp = tty->driver_data;
  1644. if (!tp)
  1645. return;
  1646. tp->throttle = 1;
  1647. }
  1648. /*
  1649. * Enable reading from a 3270 tty
  1650. */
  1651. static void
  1652. tty3270_unthrottle(struct tty_struct * tty)
  1653. {
  1654. struct tty3270 *tp;
  1655. tp = tty->driver_data;
  1656. if (!tp)
  1657. return;
  1658. tp->throttle = 0;
  1659. if (tp->attn)
  1660. tty3270_issue_read(tp, 1);
  1661. }
  1662. /*
  1663. * Hang up the tty device.
  1664. */
  1665. static void
  1666. tty3270_hangup(struct tty_struct *tty)
  1667. {
  1668. struct tty3270 *tp;
  1669. tp = tty->driver_data;
  1670. if (!tp)
  1671. return;
  1672. spin_lock_bh(&tp->view.lock);
  1673. tp->cx = tp->saved_cx = 0;
  1674. tp->cy = tp->saved_cy = 0;
  1675. tp->highlight = tp->saved_highlight = TAX_RESET;
  1676. tp->f_color = tp->saved_f_color = TAC_RESET;
  1677. tty3270_blank_screen(tp);
  1678. while (tp->nr_lines < tp->view.rows - 2)
  1679. tty3270_blank_line(tp);
  1680. tp->update_flags = TTY_UPDATE_ALL;
  1681. spin_unlock_bh(&tp->view.lock);
  1682. tty3270_set_timer(tp, 1);
  1683. }
  1684. static void
  1685. tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
  1686. {
  1687. }
  1688. static int tty3270_ioctl(struct tty_struct *tty, unsigned int cmd,
  1689. unsigned long arg)
  1690. {
  1691. struct tty3270 *tp;
  1692. tp = tty->driver_data;
  1693. if (!tp)
  1694. return -ENODEV;
  1695. if (tty_io_error(tty))
  1696. return -EIO;
  1697. return kbd_ioctl(tp->kbd, cmd, arg);
  1698. }
  1699. #ifdef CONFIG_COMPAT
  1700. static long tty3270_compat_ioctl(struct tty_struct *tty,
  1701. unsigned int cmd, unsigned long arg)
  1702. {
  1703. struct tty3270 *tp;
  1704. tp = tty->driver_data;
  1705. if (!tp)
  1706. return -ENODEV;
  1707. if (tty_io_error(tty))
  1708. return -EIO;
  1709. return kbd_ioctl(tp->kbd, cmd, (unsigned long)compat_ptr(arg));
  1710. }
  1711. #endif
  1712. static const struct tty_operations tty3270_ops = {
  1713. .install = tty3270_install,
  1714. .cleanup = tty3270_cleanup,
  1715. .open = tty3270_open,
  1716. .close = tty3270_close,
  1717. .write = tty3270_write,
  1718. .put_char = tty3270_put_char,
  1719. .flush_chars = tty3270_flush_chars,
  1720. .write_room = tty3270_write_room,
  1721. .chars_in_buffer = tty3270_chars_in_buffer,
  1722. .flush_buffer = tty3270_flush_buffer,
  1723. .throttle = tty3270_throttle,
  1724. .unthrottle = tty3270_unthrottle,
  1725. .hangup = tty3270_hangup,
  1726. .wait_until_sent = tty3270_wait_until_sent,
  1727. .ioctl = tty3270_ioctl,
  1728. #ifdef CONFIG_COMPAT
  1729. .compat_ioctl = tty3270_compat_ioctl,
  1730. #endif
  1731. .set_termios = tty3270_set_termios
  1732. };
  1733. static void tty3270_create_cb(int minor)
  1734. {
  1735. tty_register_device(tty3270_driver, minor - RAW3270_FIRSTMINOR, NULL);
  1736. }
  1737. static void tty3270_destroy_cb(int minor)
  1738. {
  1739. tty_unregister_device(tty3270_driver, minor - RAW3270_FIRSTMINOR);
  1740. }
  1741. static struct raw3270_notifier tty3270_notifier =
  1742. {
  1743. .create = tty3270_create_cb,
  1744. .destroy = tty3270_destroy_cb,
  1745. };
  1746. /*
  1747. * 3270 tty registration code called from tty_init().
  1748. * Most kernel services (incl. kmalloc) are available at this poimt.
  1749. */
  1750. static int __init tty3270_init(void)
  1751. {
  1752. struct tty_driver *driver;
  1753. int ret;
  1754. driver = tty_alloc_driver(RAW3270_MAXDEVS,
  1755. TTY_DRIVER_REAL_RAW |
  1756. TTY_DRIVER_DYNAMIC_DEV |
  1757. TTY_DRIVER_RESET_TERMIOS);
  1758. if (IS_ERR(driver))
  1759. return PTR_ERR(driver);
  1760. /*
  1761. * Initialize the tty_driver structure
  1762. * Entries in tty3270_driver that are NOT initialized:
  1763. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1764. */
  1765. driver->driver_name = "tty3270";
  1766. driver->name = "3270/tty";
  1767. driver->major = IBM_TTY3270_MAJOR;
  1768. driver->minor_start = RAW3270_FIRSTMINOR;
  1769. driver->name_base = RAW3270_FIRSTMINOR;
  1770. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1771. driver->subtype = SYSTEM_TYPE_TTY;
  1772. driver->init_termios = tty_std_termios;
  1773. tty_set_operations(driver, &tty3270_ops);
  1774. ret = tty_register_driver(driver);
  1775. if (ret) {
  1776. put_tty_driver(driver);
  1777. return ret;
  1778. }
  1779. tty3270_driver = driver;
  1780. raw3270_register_notifier(&tty3270_notifier);
  1781. return 0;
  1782. }
  1783. static void __exit
  1784. tty3270_exit(void)
  1785. {
  1786. struct tty_driver *driver;
  1787. raw3270_unregister_notifier(&tty3270_notifier);
  1788. driver = tty3270_driver;
  1789. tty3270_driver = NULL;
  1790. tty_unregister_driver(driver);
  1791. put_tty_driver(driver);
  1792. tty3270_del_views();
  1793. }
  1794. MODULE_LICENSE("GPL");
  1795. MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
  1796. module_init(tty3270_init);
  1797. module_exit(tty3270_exit);