tty3270.c 46 KB

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