vt.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255
  1. /*
  2. * linux/drivers/char/vt.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Hopefully this will be a rather complete VT102 implementation.
  8. *
  9. * Beeping thanks to John T Kohl.
  10. *
  11. * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
  12. * Chars, and VT100 enhancements by Peter MacDonald.
  13. *
  14. * Copy and paste function by Andrew Haylett,
  15. * some enhancements by Alessandro Rubini.
  16. *
  17. * Code to check for different video-cards mostly by Galen Hunt,
  18. * <g-hunt@ee.utah.edu>
  19. *
  20. * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
  21. * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
  22. *
  23. * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
  24. * Resizing of consoles, aeb, 940926
  25. *
  26. * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
  27. * <poe@daimi.aau.dk>
  28. *
  29. * User-defined bell sound, new setterm control sequences and printk
  30. * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
  31. *
  32. * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
  33. *
  34. * Merge with the abstract console driver by Geert Uytterhoeven
  35. * <geert@linux-m68k.org>, Jan 1997.
  36. *
  37. * Original m68k console driver modifications by
  38. *
  39. * - Arno Griffioen <arno@usn.nl>
  40. * - David Carter <carter@cs.bris.ac.uk>
  41. *
  42. * The abstract console driver provides a generic interface for a text
  43. * console. It supports VGA text mode, frame buffer based graphical consoles
  44. * and special graphics processors that are only accessible through some
  45. * registers (e.g. a TMS340x0 GSP).
  46. *
  47. * The interface to the hardware is specified using a special structure
  48. * (struct consw) which contains function pointers to console operations
  49. * (see <linux/console.h> for more information).
  50. *
  51. * Support for changeable cursor shape
  52. * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
  53. *
  54. * Ported to i386 and con_scrolldelta fixed
  55. * by Emmanuel Marty <core@ggi-project.org>, April 1998
  56. *
  57. * Resurrected character buffers in videoram plus lots of other trickery
  58. * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
  59. *
  60. * Removed old-style timers, introduced console_timer, made timer
  61. * deletion SMP-safe. 17Jun00, Andrew Morton <andrewm@uow.edu.au>
  62. *
  63. * Removed console_lock, enabled interrupts across all console operations
  64. * 13 March 2001, Andrew Morton
  65. */
  66. #include <linux/module.h>
  67. #include <linux/types.h>
  68. #include <linux/sched.h>
  69. #include <linux/tty.h>
  70. #include <linux/tty_flip.h>
  71. #include <linux/kernel.h>
  72. #include <linux/string.h>
  73. #include <linux/errno.h>
  74. #include <linux/kd.h>
  75. #include <linux/slab.h>
  76. #include <linux/major.h>
  77. #include <linux/mm.h>
  78. #include <linux/console.h>
  79. #include <linux/init.h>
  80. #include <linux/devfs_fs_kernel.h>
  81. #include <linux/vt_kern.h>
  82. #include <linux/selection.h>
  83. #include <linux/tiocl.h>
  84. #include <linux/kbd_kern.h>
  85. #include <linux/consolemap.h>
  86. #include <linux/timer.h>
  87. #include <linux/interrupt.h>
  88. #include <linux/config.h>
  89. #include <linux/workqueue.h>
  90. #include <linux/bootmem.h>
  91. #include <linux/pm.h>
  92. #include <linux/font.h>
  93. #include <linux/bitops.h>
  94. #include <asm/io.h>
  95. #include <asm/system.h>
  96. #include <asm/uaccess.h>
  97. const struct consw *conswitchp;
  98. /* A bitmap for codes <32. A bit of 1 indicates that the code
  99. * corresponding to that bit number invokes some special action
  100. * (such as cursor movement) and should not be displayed as a
  101. * glyph unless the disp_ctrl mode is explicitly enabled.
  102. */
  103. #define CTRL_ACTION 0x0d00ff81
  104. #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
  105. /*
  106. * Here is the default bell parameters: 750HZ, 1/8th of a second
  107. */
  108. #define DEFAULT_BELL_PITCH 750
  109. #define DEFAULT_BELL_DURATION (HZ/8)
  110. extern void vcs_make_devfs(struct tty_struct *tty);
  111. extern void vcs_remove_devfs(struct tty_struct *tty);
  112. extern void console_map_init(void);
  113. #ifdef CONFIG_PROM_CONSOLE
  114. extern void prom_con_init(void);
  115. #endif
  116. #ifdef CONFIG_MDA_CONSOLE
  117. extern int mda_console_init(void);
  118. #endif
  119. struct vc vc_cons [MAX_NR_CONSOLES];
  120. #ifndef VT_SINGLE_DRIVER
  121. static const struct consw *con_driver_map[MAX_NR_CONSOLES];
  122. #endif
  123. static int con_open(struct tty_struct *, struct file *);
  124. static void vc_init(struct vc_data *vc, unsigned int rows,
  125. unsigned int cols, int do_clear);
  126. static void gotoxy(struct vc_data *vc, int new_x, int new_y);
  127. static void save_cur(struct vc_data *vc);
  128. static void reset_terminal(struct vc_data *vc, int do_clear);
  129. static void con_flush_chars(struct tty_struct *tty);
  130. static void set_vesa_blanking(char __user *p);
  131. static void set_cursor(struct vc_data *vc);
  132. static void hide_cursor(struct vc_data *vc);
  133. static void console_callback(void *ignored);
  134. static void blank_screen_t(unsigned long dummy);
  135. static void set_palette(struct vc_data *vc);
  136. static int printable; /* Is console ready for printing? */
  137. /*
  138. * ignore_poke: don't unblank the screen when things are typed. This is
  139. * mainly for the privacy of braille terminal users.
  140. */
  141. static int ignore_poke;
  142. int do_poke_blanked_console;
  143. int console_blanked;
  144. static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
  145. static int blankinterval = 10*60*HZ;
  146. static int vesa_off_interval;
  147. static DECLARE_WORK(console_work, console_callback, NULL);
  148. /*
  149. * fg_console is the current virtual console,
  150. * last_console is the last used one,
  151. * want_console is the console we want to switch to,
  152. * kmsg_redirect is the console for kernel messages,
  153. */
  154. int fg_console;
  155. int last_console;
  156. int want_console = -1;
  157. int kmsg_redirect;
  158. /*
  159. * For each existing display, we have a pointer to console currently visible
  160. * on that display, allowing consoles other than fg_console to be refreshed
  161. * appropriately. Unless the low-level driver supplies its own display_fg
  162. * variable, we use this one for the "master display".
  163. */
  164. static struct vc_data *master_display_fg;
  165. /*
  166. * Unfortunately, we need to delay tty echo when we're currently writing to the
  167. * console since the code is (and always was) not re-entrant, so we schedule
  168. * all flip requests to process context with schedule-task() and run it from
  169. * console_callback().
  170. */
  171. /*
  172. * For the same reason, we defer scrollback to the console callback.
  173. */
  174. static int scrollback_delta;
  175. /*
  176. * Hook so that the power management routines can (un)blank
  177. * the console on our behalf.
  178. */
  179. int (*console_blank_hook)(int);
  180. static struct timer_list console_timer;
  181. static int blank_state;
  182. static int blank_timer_expired;
  183. enum {
  184. blank_off = 0,
  185. blank_normal_wait,
  186. blank_vesa_wait,
  187. };
  188. /*
  189. * Low-Level Functions
  190. */
  191. #define IS_FG(vc) ((vc)->vc_num == fg_console)
  192. #ifdef VT_BUF_VRAM_ONLY
  193. #define DO_UPDATE(vc) 0
  194. #else
  195. #define DO_UPDATE(vc) CON_IS_VISIBLE(vc)
  196. #endif
  197. static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
  198. {
  199. unsigned short *p;
  200. if (!viewed)
  201. p = (unsigned short *)(vc->vc_origin + offset);
  202. else if (!vc->vc_sw->con_screen_pos)
  203. p = (unsigned short *)(vc->vc_visible_origin + offset);
  204. else
  205. p = vc->vc_sw->con_screen_pos(vc, offset);
  206. return p;
  207. }
  208. static inline void scrolldelta(int lines)
  209. {
  210. scrollback_delta += lines;
  211. schedule_console_callback();
  212. }
  213. void schedule_console_callback(void)
  214. {
  215. schedule_work(&console_work);
  216. }
  217. static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
  218. {
  219. unsigned short *d, *s;
  220. if (t+nr >= b)
  221. nr = b - t - 1;
  222. if (b > vc->vc_rows || t >= b || nr < 1)
  223. return;
  224. if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
  225. return;
  226. d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
  227. s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
  228. scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
  229. scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
  230. vc->vc_size_row * nr);
  231. }
  232. static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
  233. {
  234. unsigned short *s;
  235. unsigned int step;
  236. if (t+nr >= b)
  237. nr = b - t - 1;
  238. if (b > vc->vc_rows || t >= b || nr < 1)
  239. return;
  240. if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
  241. return;
  242. s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
  243. step = vc->vc_cols * nr;
  244. scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
  245. scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
  246. }
  247. static void do_update_region(struct vc_data *vc, unsigned long start, int count)
  248. {
  249. #ifndef VT_BUF_VRAM_ONLY
  250. unsigned int xx, yy, offset;
  251. u16 *p;
  252. p = (u16 *) start;
  253. if (!vc->vc_sw->con_getxy) {
  254. offset = (start - vc->vc_origin) / 2;
  255. xx = offset % vc->vc_cols;
  256. yy = offset / vc->vc_cols;
  257. } else {
  258. int nxx, nyy;
  259. start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
  260. xx = nxx; yy = nyy;
  261. }
  262. for(;;) {
  263. u16 attrib = scr_readw(p) & 0xff00;
  264. int startx = xx;
  265. u16 *q = p;
  266. while (xx < vc->vc_cols && count) {
  267. if (attrib != (scr_readw(p) & 0xff00)) {
  268. if (p > q)
  269. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  270. startx = xx;
  271. q = p;
  272. attrib = scr_readw(p) & 0xff00;
  273. }
  274. p++;
  275. xx++;
  276. count--;
  277. }
  278. if (p > q)
  279. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  280. if (!count)
  281. break;
  282. xx = 0;
  283. yy++;
  284. if (vc->vc_sw->con_getxy) {
  285. p = (u16 *)start;
  286. start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
  287. }
  288. }
  289. #endif
  290. }
  291. void update_region(struct vc_data *vc, unsigned long start, int count)
  292. {
  293. WARN_CONSOLE_UNLOCKED();
  294. if (DO_UPDATE(vc)) {
  295. hide_cursor(vc);
  296. do_update_region(vc, start, count);
  297. set_cursor(vc);
  298. }
  299. }
  300. /* Structure of attributes is hardware-dependent */
  301. static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink, u8 _underline, u8 _reverse)
  302. {
  303. if (vc->vc_sw->con_build_attr)
  304. return vc->vc_sw->con_build_attr(vc, _color, _intensity, _blink, _underline, _reverse);
  305. #ifndef VT_BUF_VRAM_ONLY
  306. /*
  307. * ++roman: I completely changed the attribute format for monochrome
  308. * mode (!can_do_color). The formerly used MDA (monochrome display
  309. * adapter) format didn't allow the combination of certain effects.
  310. * Now the attribute is just a bit vector:
  311. * Bit 0..1: intensity (0..2)
  312. * Bit 2 : underline
  313. * Bit 3 : reverse
  314. * Bit 7 : blink
  315. */
  316. {
  317. u8 a = vc->vc_color;
  318. if (!vc->vc_can_do_color)
  319. return _intensity |
  320. (_underline ? 4 : 0) |
  321. (_reverse ? 8 : 0) |
  322. (_blink ? 0x80 : 0);
  323. if (_underline)
  324. a = (a & 0xf0) | vc->vc_ulcolor;
  325. else if (_intensity == 0)
  326. a = (a & 0xf0) | vc->vc_ulcolor;
  327. if (_reverse)
  328. a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
  329. if (_blink)
  330. a ^= 0x80;
  331. if (_intensity == 2)
  332. a ^= 0x08;
  333. if (vc->vc_hi_font_mask == 0x100)
  334. a <<= 1;
  335. return a;
  336. }
  337. #else
  338. return 0;
  339. #endif
  340. }
  341. static void update_attr(struct vc_data *vc)
  342. {
  343. vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity, vc->vc_blink, vc->vc_underline, vc->vc_reverse ^ vc->vc_decscnm);
  344. vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm) << 8) | ' ';
  345. }
  346. /* Note: inverting the screen twice should revert to the original state */
  347. void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
  348. {
  349. unsigned short *p;
  350. WARN_CONSOLE_UNLOCKED();
  351. count /= 2;
  352. p = screenpos(vc, offset, viewed);
  353. if (vc->vc_sw->con_invert_region)
  354. vc->vc_sw->con_invert_region(vc, p, count);
  355. #ifndef VT_BUF_VRAM_ONLY
  356. else {
  357. u16 *q = p;
  358. int cnt = count;
  359. u16 a;
  360. if (!vc->vc_can_do_color) {
  361. while (cnt--) {
  362. a = scr_readw(q);
  363. a ^= 0x0800;
  364. scr_writew(a, q);
  365. q++;
  366. }
  367. } else if (vc->vc_hi_font_mask == 0x100) {
  368. while (cnt--) {
  369. a = scr_readw(q);
  370. a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
  371. scr_writew(a, q);
  372. q++;
  373. }
  374. } else {
  375. while (cnt--) {
  376. a = scr_readw(q);
  377. a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
  378. scr_writew(a, q);
  379. q++;
  380. }
  381. }
  382. }
  383. #endif
  384. if (DO_UPDATE(vc))
  385. do_update_region(vc, (unsigned long) p, count);
  386. }
  387. /* used by selection: complement pointer position */
  388. void complement_pos(struct vc_data *vc, int offset)
  389. {
  390. static int old_offset = -1;
  391. static unsigned short old;
  392. static unsigned short oldx, oldy;
  393. WARN_CONSOLE_UNLOCKED();
  394. if (old_offset != -1 && old_offset >= 0 &&
  395. old_offset < vc->vc_screenbuf_size) {
  396. scr_writew(old, screenpos(vc, old_offset, 1));
  397. if (DO_UPDATE(vc))
  398. vc->vc_sw->con_putc(vc, old, oldy, oldx);
  399. }
  400. old_offset = offset;
  401. if (offset != -1 && offset >= 0 &&
  402. offset < vc->vc_screenbuf_size) {
  403. unsigned short new;
  404. unsigned short *p;
  405. p = screenpos(vc, offset, 1);
  406. old = scr_readw(p);
  407. new = old ^ vc->vc_complement_mask;
  408. scr_writew(new, p);
  409. if (DO_UPDATE(vc)) {
  410. oldx = (offset >> 1) % vc->vc_cols;
  411. oldy = (offset >> 1) / vc->vc_cols;
  412. vc->vc_sw->con_putc(vc, new, oldy, oldx);
  413. }
  414. }
  415. }
  416. static void insert_char(struct vc_data *vc, unsigned int nr)
  417. {
  418. unsigned short *p, *q = (unsigned short *)vc->vc_pos;
  419. p = q + vc->vc_cols - nr - vc->vc_x;
  420. while (--p >= q)
  421. scr_writew(scr_readw(p), p + nr);
  422. scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
  423. vc->vc_need_wrap = 0;
  424. if (DO_UPDATE(vc)) {
  425. unsigned short oldattr = vc->vc_attr;
  426. vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
  427. vc->vc_cols - vc->vc_x - nr);
  428. vc->vc_attr = vc->vc_video_erase_char >> 8;
  429. while (nr--)
  430. vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
  431. vc->vc_attr = oldattr;
  432. }
  433. }
  434. static void delete_char(struct vc_data *vc, unsigned int nr)
  435. {
  436. unsigned int i = vc->vc_x;
  437. unsigned short *p = (unsigned short *)vc->vc_pos;
  438. while (++i <= vc->vc_cols - nr) {
  439. scr_writew(scr_readw(p+nr), p);
  440. p++;
  441. }
  442. scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
  443. vc->vc_need_wrap = 0;
  444. if (DO_UPDATE(vc)) {
  445. unsigned short oldattr = vc->vc_attr;
  446. vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
  447. vc->vc_cols - vc->vc_x - nr);
  448. vc->vc_attr = vc->vc_video_erase_char >> 8;
  449. while (nr--)
  450. vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
  451. vc->vc_cols - 1 - nr);
  452. vc->vc_attr = oldattr;
  453. }
  454. }
  455. static int softcursor_original;
  456. static void add_softcursor(struct vc_data *vc)
  457. {
  458. int i = scr_readw((u16 *) vc->vc_pos);
  459. u32 type = vc->vc_cursor_type;
  460. if (! (type & 0x10)) return;
  461. if (softcursor_original != -1) return;
  462. softcursor_original = i;
  463. i |= ((type >> 8) & 0xff00 );
  464. i ^= ((type) & 0xff00 );
  465. if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
  466. if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
  467. scr_writew(i, (u16 *) vc->vc_pos);
  468. if (DO_UPDATE(vc))
  469. vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
  470. }
  471. static void hide_softcursor(struct vc_data *vc)
  472. {
  473. if (softcursor_original != -1) {
  474. scr_writew(softcursor_original, (u16 *)vc->vc_pos);
  475. if (DO_UPDATE(vc))
  476. vc->vc_sw->con_putc(vc, softcursor_original,
  477. vc->vc_y, vc->vc_x);
  478. softcursor_original = -1;
  479. }
  480. }
  481. static void hide_cursor(struct vc_data *vc)
  482. {
  483. if (vc == sel_cons)
  484. clear_selection();
  485. vc->vc_sw->con_cursor(vc, CM_ERASE);
  486. hide_softcursor(vc);
  487. }
  488. static void set_cursor(struct vc_data *vc)
  489. {
  490. if (!IS_FG(vc) || console_blanked ||
  491. vc->vc_mode == KD_GRAPHICS)
  492. return;
  493. if (vc->vc_deccm) {
  494. if (vc == sel_cons)
  495. clear_selection();
  496. add_softcursor(vc);
  497. if ((vc->vc_cursor_type & 0x0f) != 1)
  498. vc->vc_sw->con_cursor(vc, CM_DRAW);
  499. } else
  500. hide_cursor(vc);
  501. }
  502. static void set_origin(struct vc_data *vc)
  503. {
  504. WARN_CONSOLE_UNLOCKED();
  505. if (!CON_IS_VISIBLE(vc) ||
  506. !vc->vc_sw->con_set_origin ||
  507. !vc->vc_sw->con_set_origin(vc))
  508. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  509. vc->vc_visible_origin = vc->vc_origin;
  510. vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
  511. vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
  512. }
  513. static inline void save_screen(struct vc_data *vc)
  514. {
  515. WARN_CONSOLE_UNLOCKED();
  516. if (vc->vc_sw->con_save_screen)
  517. vc->vc_sw->con_save_screen(vc);
  518. }
  519. /*
  520. * Redrawing of screen
  521. */
  522. static void clear_buffer_attributes(struct vc_data *vc)
  523. {
  524. unsigned short *p = (unsigned short *)vc->vc_origin;
  525. int count = vc->vc_screenbuf_size / 2;
  526. int mask = vc->vc_hi_font_mask | 0xff;
  527. for (; count > 0; count--, p++) {
  528. scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
  529. }
  530. }
  531. void redraw_screen(struct vc_data *vc, int is_switch)
  532. {
  533. int redraw = 0;
  534. WARN_CONSOLE_UNLOCKED();
  535. if (!vc) {
  536. /* strange ... */
  537. /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
  538. return;
  539. }
  540. if (is_switch) {
  541. struct vc_data *old_vc = vc_cons[fg_console].d;
  542. if (old_vc == vc)
  543. return;
  544. if (!CON_IS_VISIBLE(vc))
  545. redraw = 1;
  546. *vc->vc_display_fg = vc;
  547. fg_console = vc->vc_num;
  548. hide_cursor(old_vc);
  549. if (!CON_IS_VISIBLE(old_vc)) {
  550. save_screen(old_vc);
  551. set_origin(old_vc);
  552. }
  553. } else {
  554. hide_cursor(vc);
  555. redraw = 1;
  556. }
  557. if (redraw) {
  558. int update;
  559. int old_was_color = vc->vc_can_do_color;
  560. set_origin(vc);
  561. update = vc->vc_sw->con_switch(vc);
  562. set_palette(vc);
  563. /*
  564. * If console changed from mono<->color, the best we can do
  565. * is to clear the buffer attributes. As it currently stands,
  566. * rebuilding new attributes from the old buffer is not doable
  567. * without overly complex code.
  568. */
  569. if (old_was_color != vc->vc_can_do_color) {
  570. update_attr(vc);
  571. clear_buffer_attributes(vc);
  572. }
  573. if (update && vc->vc_mode != KD_GRAPHICS)
  574. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  575. }
  576. set_cursor(vc);
  577. if (is_switch) {
  578. set_leds();
  579. compute_shiftstate();
  580. }
  581. }
  582. /*
  583. * Allocation, freeing and resizing of VTs.
  584. */
  585. int vc_cons_allocated(unsigned int i)
  586. {
  587. return (i < MAX_NR_CONSOLES && vc_cons[i].d);
  588. }
  589. static void visual_init(struct vc_data *vc, int num, int init)
  590. {
  591. /* ++Geert: vc->vc_sw->con_init determines console size */
  592. if (vc->vc_sw)
  593. module_put(vc->vc_sw->owner);
  594. vc->vc_sw = conswitchp;
  595. #ifndef VT_SINGLE_DRIVER
  596. if (con_driver_map[num])
  597. vc->vc_sw = con_driver_map[num];
  598. #endif
  599. __module_get(vc->vc_sw->owner);
  600. vc->vc_num = num;
  601. vc->vc_display_fg = &master_display_fg;
  602. vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
  603. vc->vc_uni_pagedir = 0;
  604. vc->vc_hi_font_mask = 0;
  605. vc->vc_complement_mask = 0;
  606. vc->vc_can_do_color = 0;
  607. vc->vc_sw->con_init(vc, init);
  608. if (!vc->vc_complement_mask)
  609. vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
  610. vc->vc_s_complement_mask = vc->vc_complement_mask;
  611. vc->vc_size_row = vc->vc_cols << 1;
  612. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  613. }
  614. int vc_allocate(unsigned int currcons) /* return 0 on success */
  615. {
  616. WARN_CONSOLE_UNLOCKED();
  617. if (currcons >= MAX_NR_CONSOLES)
  618. return -ENXIO;
  619. if (!vc_cons[currcons].d) {
  620. struct vc_data *vc;
  621. /* prevent users from taking too much memory */
  622. if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
  623. return -EPERM;
  624. /* due to the granularity of kmalloc, we waste some memory here */
  625. /* the alloc is done in two steps, to optimize the common situation
  626. of a 25x80 console (structsize=216, screenbuf_size=4000) */
  627. /* although the numbers above are not valid since long ago, the
  628. point is still up-to-date and the comment still has its value
  629. even if only as a historical artifact. --mj, July 1998 */
  630. vc = kmalloc(sizeof(struct vc_data), GFP_KERNEL);
  631. if (!vc)
  632. return -ENOMEM;
  633. memset(vc, 0, sizeof(*vc));
  634. vc_cons[currcons].d = vc;
  635. visual_init(vc, currcons, 1);
  636. if (!*vc->vc_uni_pagedir_loc)
  637. con_set_default_unimap(vc);
  638. vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
  639. if (!vc->vc_screenbuf) {
  640. kfree(vc);
  641. vc_cons[currcons].d = NULL;
  642. return -ENOMEM;
  643. }
  644. vc->vc_kmalloced = 1;
  645. vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
  646. }
  647. return 0;
  648. }
  649. static inline int resize_screen(struct vc_data *vc, int width, int height)
  650. {
  651. /* Resizes the resolution of the display adapater */
  652. int err = 0;
  653. if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
  654. err = vc->vc_sw->con_resize(vc, width, height);
  655. return err;
  656. }
  657. /*
  658. * Change # of rows and columns (0 means unchanged/the size of fg_console)
  659. * [this is to be used together with some user program
  660. * like resize that changes the hardware videomode]
  661. */
  662. #define VC_RESIZE_MAXCOL (32767)
  663. #define VC_RESIZE_MAXROW (32767)
  664. int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int lines)
  665. {
  666. unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
  667. unsigned int old_cols, old_rows, old_row_size, old_screen_size;
  668. unsigned int new_cols, new_rows, new_row_size, new_screen_size;
  669. unsigned short *newscreen;
  670. WARN_CONSOLE_UNLOCKED();
  671. if (!vc)
  672. return -ENXIO;
  673. if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
  674. return -EINVAL;
  675. new_cols = (cols ? cols : vc->vc_cols);
  676. new_rows = (lines ? lines : vc->vc_rows);
  677. new_row_size = new_cols << 1;
  678. new_screen_size = new_row_size * new_rows;
  679. if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
  680. return 0;
  681. newscreen = (unsigned short *) kmalloc(new_screen_size, GFP_USER);
  682. if (!newscreen)
  683. return -ENOMEM;
  684. old_rows = vc->vc_rows;
  685. old_cols = vc->vc_cols;
  686. old_row_size = vc->vc_size_row;
  687. old_screen_size = vc->vc_screenbuf_size;
  688. err = resize_screen(vc, new_cols, new_rows);
  689. if (err) {
  690. kfree(newscreen);
  691. return err;
  692. }
  693. vc->vc_rows = new_rows;
  694. vc->vc_cols = new_cols;
  695. vc->vc_size_row = new_row_size;
  696. vc->vc_screenbuf_size = new_screen_size;
  697. rlth = min(old_row_size, new_row_size);
  698. rrem = new_row_size - rlth;
  699. old_origin = vc->vc_origin;
  700. new_origin = (long) newscreen;
  701. new_scr_end = new_origin + new_screen_size;
  702. if (new_rows < old_rows)
  703. old_origin += (old_rows - new_rows) * old_row_size;
  704. update_attr(vc);
  705. while (old_origin < vc->vc_scr_end) {
  706. scr_memcpyw((unsigned short *) new_origin, (unsigned short *) old_origin, rlth);
  707. if (rrem)
  708. scr_memsetw((void *)(new_origin + rlth), vc->vc_video_erase_char, rrem);
  709. old_origin += old_row_size;
  710. new_origin += new_row_size;
  711. }
  712. if (new_scr_end > new_origin)
  713. scr_memsetw((void *)new_origin, vc->vc_video_erase_char, new_scr_end - new_origin);
  714. if (vc->vc_kmalloced)
  715. kfree(vc->vc_screenbuf);
  716. vc->vc_screenbuf = newscreen;
  717. vc->vc_kmalloced = 1;
  718. vc->vc_screenbuf_size = new_screen_size;
  719. set_origin(vc);
  720. /* do part of a reset_terminal() */
  721. vc->vc_top = 0;
  722. vc->vc_bottom = vc->vc_rows;
  723. gotoxy(vc, vc->vc_x, vc->vc_y);
  724. save_cur(vc);
  725. if (vc->vc_tty) {
  726. struct winsize ws, *cws = &vc->vc_tty->winsize;
  727. memset(&ws, 0, sizeof(ws));
  728. ws.ws_row = vc->vc_rows;
  729. ws.ws_col = vc->vc_cols;
  730. ws.ws_ypixel = vc->vc_scan_lines;
  731. if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
  732. vc->vc_tty->pgrp > 0)
  733. kill_pg(vc->vc_tty->pgrp, SIGWINCH, 1);
  734. *cws = ws;
  735. }
  736. if (CON_IS_VISIBLE(vc))
  737. update_screen(vc);
  738. return err;
  739. }
  740. void vc_disallocate(unsigned int currcons)
  741. {
  742. WARN_CONSOLE_UNLOCKED();
  743. if (vc_cons_allocated(currcons)) {
  744. struct vc_data *vc = vc_cons[currcons].d;
  745. vc->vc_sw->con_deinit(vc);
  746. if (vc->vc_kmalloced)
  747. kfree(vc->vc_screenbuf);
  748. if (currcons >= MIN_NR_CONSOLES)
  749. kfree(vc);
  750. vc_cons[currcons].d = NULL;
  751. }
  752. }
  753. /*
  754. * VT102 emulator
  755. */
  756. #define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  757. #define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  758. #define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  759. #define decarm VC_REPEAT
  760. #define decckm VC_CKMODE
  761. #define kbdapplic VC_APPLIC
  762. #define lnm VC_CRLF
  763. /*
  764. * this is what the terminal answers to a ESC-Z or csi0c query.
  765. */
  766. #define VT100ID "\033[?1;2c"
  767. #define VT102ID "\033[?6c"
  768. unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
  769. 8,12,10,14, 9,13,11,15 };
  770. /* the default colour table, for VGA+ colour systems */
  771. int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
  772. 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
  773. int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
  774. 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
  775. int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
  776. 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
  777. /*
  778. * gotoxy() must verify all boundaries, because the arguments
  779. * might also be negative. If the given position is out of
  780. * bounds, the cursor is placed at the nearest margin.
  781. */
  782. static void gotoxy(struct vc_data *vc, int new_x, int new_y)
  783. {
  784. int min_y, max_y;
  785. if (new_x < 0)
  786. vc->vc_x = 0;
  787. else {
  788. if (new_x >= vc->vc_cols)
  789. vc->vc_x = vc->vc_cols - 1;
  790. else
  791. vc->vc_x = new_x;
  792. }
  793. if (vc->vc_decom) {
  794. min_y = vc->vc_top;
  795. max_y = vc->vc_bottom;
  796. } else {
  797. min_y = 0;
  798. max_y = vc->vc_rows;
  799. }
  800. if (new_y < min_y)
  801. vc->vc_y = min_y;
  802. else if (new_y >= max_y)
  803. vc->vc_y = max_y - 1;
  804. else
  805. vc->vc_y = new_y;
  806. vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
  807. vc->vc_need_wrap = 0;
  808. }
  809. /* for absolute user moves, when decom is set */
  810. static void gotoxay(struct vc_data *vc, int new_x, int new_y)
  811. {
  812. gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
  813. }
  814. void scrollback(struct vc_data *vc, int lines)
  815. {
  816. if (!lines)
  817. lines = vc->vc_rows / 2;
  818. scrolldelta(-lines);
  819. }
  820. void scrollfront(struct vc_data *vc, int lines)
  821. {
  822. if (!lines)
  823. lines = vc->vc_rows / 2;
  824. scrolldelta(lines);
  825. }
  826. static void lf(struct vc_data *vc)
  827. {
  828. /* don't scroll if above bottom of scrolling region, or
  829. * if below scrolling region
  830. */
  831. if (vc->vc_y + 1 == vc->vc_bottom)
  832. scrup(vc, vc->vc_top, vc->vc_bottom, 1);
  833. else if (vc->vc_y < vc->vc_rows - 1) {
  834. vc->vc_y++;
  835. vc->vc_pos += vc->vc_size_row;
  836. }
  837. vc->vc_need_wrap = 0;
  838. }
  839. static void ri(struct vc_data *vc)
  840. {
  841. /* don't scroll if below top of scrolling region, or
  842. * if above scrolling region
  843. */
  844. if (vc->vc_y == vc->vc_top)
  845. scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
  846. else if (vc->vc_y > 0) {
  847. vc->vc_y--;
  848. vc->vc_pos -= vc->vc_size_row;
  849. }
  850. vc->vc_need_wrap = 0;
  851. }
  852. static inline void cr(struct vc_data *vc)
  853. {
  854. vc->vc_pos -= vc->vc_x << 1;
  855. vc->vc_need_wrap = vc->vc_x = 0;
  856. }
  857. static inline void bs(struct vc_data *vc)
  858. {
  859. if (vc->vc_x) {
  860. vc->vc_pos -= 2;
  861. vc->vc_x--;
  862. vc->vc_need_wrap = 0;
  863. }
  864. }
  865. static inline void del(struct vc_data *vc)
  866. {
  867. /* ignored */
  868. }
  869. static void csi_J(struct vc_data *vc, int vpar)
  870. {
  871. unsigned int count;
  872. unsigned short * start;
  873. switch (vpar) {
  874. case 0: /* erase from cursor to end of display */
  875. count = (vc->vc_scr_end - vc->vc_pos) >> 1;
  876. start = (unsigned short *)vc->vc_pos;
  877. if (DO_UPDATE(vc)) {
  878. /* do in two stages */
  879. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
  880. vc->vc_cols - vc->vc_x);
  881. vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
  882. vc->vc_rows - vc->vc_y - 1,
  883. vc->vc_cols);
  884. }
  885. break;
  886. case 1: /* erase from start to cursor */
  887. count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
  888. start = (unsigned short *)vc->vc_origin;
  889. if (DO_UPDATE(vc)) {
  890. /* do in two stages */
  891. vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
  892. vc->vc_cols);
  893. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  894. vc->vc_x + 1);
  895. }
  896. break;
  897. case 2: /* erase whole display */
  898. count = vc->vc_cols * vc->vc_rows;
  899. start = (unsigned short *)vc->vc_origin;
  900. if (DO_UPDATE(vc))
  901. vc->vc_sw->con_clear(vc, 0, 0,
  902. vc->vc_rows,
  903. vc->vc_cols);
  904. break;
  905. default:
  906. return;
  907. }
  908. scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
  909. vc->vc_need_wrap = 0;
  910. }
  911. static void csi_K(struct vc_data *vc, int vpar)
  912. {
  913. unsigned int count;
  914. unsigned short * start;
  915. switch (vpar) {
  916. case 0: /* erase from cursor to end of line */
  917. count = vc->vc_cols - vc->vc_x;
  918. start = (unsigned short *)vc->vc_pos;
  919. if (DO_UPDATE(vc))
  920. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
  921. vc->vc_cols - vc->vc_x);
  922. break;
  923. case 1: /* erase from start of line to cursor */
  924. start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
  925. count = vc->vc_x + 1;
  926. if (DO_UPDATE(vc))
  927. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  928. vc->vc_x + 1);
  929. break;
  930. case 2: /* erase whole line */
  931. start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
  932. count = vc->vc_cols;
  933. if (DO_UPDATE(vc))
  934. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  935. vc->vc_cols);
  936. break;
  937. default:
  938. return;
  939. }
  940. scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
  941. vc->vc_need_wrap = 0;
  942. }
  943. static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
  944. { /* not vt100? */
  945. int count;
  946. if (!vpar)
  947. vpar++;
  948. count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
  949. scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
  950. if (DO_UPDATE(vc))
  951. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
  952. vc->vc_need_wrap = 0;
  953. }
  954. static void default_attr(struct vc_data *vc)
  955. {
  956. vc->vc_intensity = 1;
  957. vc->vc_underline = 0;
  958. vc->vc_reverse = 0;
  959. vc->vc_blink = 0;
  960. vc->vc_color = vc->vc_def_color;
  961. }
  962. /* console_sem is held */
  963. static void csi_m(struct vc_data *vc)
  964. {
  965. int i;
  966. for (i = 0; i <= vc->vc_npar; i++)
  967. switch (vc->vc_par[i]) {
  968. case 0: /* all attributes off */
  969. default_attr(vc);
  970. break;
  971. case 1:
  972. vc->vc_intensity = 2;
  973. break;
  974. case 2:
  975. vc->vc_intensity = 0;
  976. break;
  977. case 4:
  978. vc->vc_underline = 1;
  979. break;
  980. case 5:
  981. vc->vc_blink = 1;
  982. break;
  983. case 7:
  984. vc->vc_reverse = 1;
  985. break;
  986. case 10: /* ANSI X3.64-1979 (SCO-ish?)
  987. * Select primary font, don't display
  988. * control chars if defined, don't set
  989. * bit 8 on output.
  990. */
  991. vc->vc_translate = set_translate(vc->vc_charset == 0
  992. ? vc->vc_G0_charset
  993. : vc->vc_G1_charset, vc);
  994. vc->vc_disp_ctrl = 0;
  995. vc->vc_toggle_meta = 0;
  996. break;
  997. case 11: /* ANSI X3.64-1979 (SCO-ish?)
  998. * Select first alternate font, lets
  999. * chars < 32 be displayed as ROM chars.
  1000. */
  1001. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1002. vc->vc_disp_ctrl = 1;
  1003. vc->vc_toggle_meta = 0;
  1004. break;
  1005. case 12: /* ANSI X3.64-1979 (SCO-ish?)
  1006. * Select second alternate font, toggle
  1007. * high bit before displaying as ROM char.
  1008. */
  1009. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1010. vc->vc_disp_ctrl = 1;
  1011. vc->vc_toggle_meta = 1;
  1012. break;
  1013. case 21:
  1014. case 22:
  1015. vc->vc_intensity = 1;
  1016. break;
  1017. case 24:
  1018. vc->vc_underline = 0;
  1019. break;
  1020. case 25:
  1021. vc->vc_blink = 0;
  1022. break;
  1023. case 27:
  1024. vc->vc_reverse = 0;
  1025. break;
  1026. case 38: /* ANSI X3.64-1979 (SCO-ish?)
  1027. * Enables underscore, white foreground
  1028. * with white underscore (Linux - use
  1029. * default foreground).
  1030. */
  1031. vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
  1032. vc->vc_underline = 1;
  1033. break;
  1034. case 39: /* ANSI X3.64-1979 (SCO-ish?)
  1035. * Disable underline option.
  1036. * Reset colour to default? It did this
  1037. * before...
  1038. */
  1039. vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
  1040. vc->vc_underline = 0;
  1041. break;
  1042. case 49:
  1043. vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
  1044. break;
  1045. default:
  1046. if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
  1047. vc->vc_color = color_table[vc->vc_par[i] - 30]
  1048. | (vc->vc_color & 0xf0);
  1049. else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
  1050. vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
  1051. | (vc->vc_color & 0x0f);
  1052. break;
  1053. }
  1054. update_attr(vc);
  1055. }
  1056. static void respond_string(const char *p, struct tty_struct *tty)
  1057. {
  1058. while (*p) {
  1059. tty_insert_flip_char(tty, *p, 0);
  1060. p++;
  1061. }
  1062. con_schedule_flip(tty);
  1063. }
  1064. static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
  1065. {
  1066. char buf[40];
  1067. sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
  1068. respond_string(buf, tty);
  1069. }
  1070. static inline void status_report(struct tty_struct *tty)
  1071. {
  1072. respond_string("\033[0n", tty); /* Terminal ok */
  1073. }
  1074. static inline void respond_ID(struct tty_struct * tty)
  1075. {
  1076. respond_string(VT102ID, tty);
  1077. }
  1078. void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
  1079. {
  1080. char buf[8];
  1081. sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
  1082. (char)('!' + mry));
  1083. respond_string(buf, tty);
  1084. }
  1085. /* invoked via ioctl(TIOCLINUX) and through set_selection */
  1086. int mouse_reporting(void)
  1087. {
  1088. return vc_cons[fg_console].d->vc_report_mouse;
  1089. }
  1090. /* console_sem is held */
  1091. static void set_mode(struct vc_data *vc, int on_off)
  1092. {
  1093. int i;
  1094. for (i = 0; i <= vc->vc_npar; i++)
  1095. if (vc->vc_ques) {
  1096. switch(vc->vc_par[i]) { /* DEC private modes set/reset */
  1097. case 1: /* Cursor keys send ^[Ox/^[[x */
  1098. if (on_off)
  1099. set_kbd(vc, decckm);
  1100. else
  1101. clr_kbd(vc, decckm);
  1102. break;
  1103. case 3: /* 80/132 mode switch unimplemented */
  1104. vc->vc_deccolm = on_off;
  1105. #if 0
  1106. vc_resize(deccolm ? 132 : 80, vc->vc_rows);
  1107. /* this alone does not suffice; some user mode
  1108. utility has to change the hardware regs */
  1109. #endif
  1110. break;
  1111. case 5: /* Inverted screen on/off */
  1112. if (vc->vc_decscnm != on_off) {
  1113. vc->vc_decscnm = on_off;
  1114. invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
  1115. update_attr(vc);
  1116. }
  1117. break;
  1118. case 6: /* Origin relative/absolute */
  1119. vc->vc_decom = on_off;
  1120. gotoxay(vc, 0, 0);
  1121. break;
  1122. case 7: /* Autowrap on/off */
  1123. vc->vc_decawm = on_off;
  1124. break;
  1125. case 8: /* Autorepeat on/off */
  1126. if (on_off)
  1127. set_kbd(vc, decarm);
  1128. else
  1129. clr_kbd(vc, decarm);
  1130. break;
  1131. case 9:
  1132. vc->vc_report_mouse = on_off ? 1 : 0;
  1133. break;
  1134. case 25: /* Cursor on/off */
  1135. vc->vc_deccm = on_off;
  1136. break;
  1137. case 1000:
  1138. vc->vc_report_mouse = on_off ? 2 : 0;
  1139. break;
  1140. }
  1141. } else {
  1142. switch(vc->vc_par[i]) { /* ANSI modes set/reset */
  1143. case 3: /* Monitor (display ctrls) */
  1144. vc->vc_disp_ctrl = on_off;
  1145. break;
  1146. case 4: /* Insert Mode on/off */
  1147. vc->vc_decim = on_off;
  1148. break;
  1149. case 20: /* Lf, Enter == CrLf/Lf */
  1150. if (on_off)
  1151. set_kbd(vc, lnm);
  1152. else
  1153. clr_kbd(vc, lnm);
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. /* console_sem is held */
  1159. static void setterm_command(struct vc_data *vc)
  1160. {
  1161. switch(vc->vc_par[0]) {
  1162. case 1: /* set color for underline mode */
  1163. if (vc->vc_can_do_color &&
  1164. vc->vc_par[1] < 16) {
  1165. vc->vc_ulcolor = color_table[vc->vc_par[1]];
  1166. if (vc->vc_underline)
  1167. update_attr(vc);
  1168. }
  1169. break;
  1170. case 2: /* set color for half intensity mode */
  1171. if (vc->vc_can_do_color &&
  1172. vc->vc_par[1] < 16) {
  1173. vc->vc_halfcolor = color_table[vc->vc_par[1]];
  1174. if (vc->vc_intensity == 0)
  1175. update_attr(vc);
  1176. }
  1177. break;
  1178. case 8: /* store colors as defaults */
  1179. vc->vc_def_color = vc->vc_attr;
  1180. if (vc->vc_hi_font_mask == 0x100)
  1181. vc->vc_def_color >>= 1;
  1182. default_attr(vc);
  1183. update_attr(vc);
  1184. break;
  1185. case 9: /* set blanking interval */
  1186. blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
  1187. poke_blanked_console();
  1188. break;
  1189. case 10: /* set bell frequency in Hz */
  1190. if (vc->vc_npar >= 1)
  1191. vc->vc_bell_pitch = vc->vc_par[1];
  1192. else
  1193. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1194. break;
  1195. case 11: /* set bell duration in msec */
  1196. if (vc->vc_npar >= 1)
  1197. vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
  1198. vc->vc_par[1] * HZ / 1000 : 0;
  1199. else
  1200. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1201. break;
  1202. case 12: /* bring specified console to the front */
  1203. if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
  1204. set_console(vc->vc_par[1] - 1);
  1205. break;
  1206. case 13: /* unblank the screen */
  1207. poke_blanked_console();
  1208. break;
  1209. case 14: /* set vesa powerdown interval */
  1210. vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
  1211. break;
  1212. case 15: /* activate the previous console */
  1213. set_console(last_console);
  1214. break;
  1215. }
  1216. }
  1217. /* console_sem is held */
  1218. static void csi_at(struct vc_data *vc, unsigned int nr)
  1219. {
  1220. if (nr > vc->vc_cols - vc->vc_x)
  1221. nr = vc->vc_cols - vc->vc_x;
  1222. else if (!nr)
  1223. nr = 1;
  1224. insert_char(vc, nr);
  1225. }
  1226. /* console_sem is held */
  1227. static void csi_L(struct vc_data *vc, unsigned int nr)
  1228. {
  1229. if (nr > vc->vc_rows - vc->vc_y)
  1230. nr = vc->vc_rows - vc->vc_y;
  1231. else if (!nr)
  1232. nr = 1;
  1233. scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
  1234. vc->vc_need_wrap = 0;
  1235. }
  1236. /* console_sem is held */
  1237. static void csi_P(struct vc_data *vc, unsigned int nr)
  1238. {
  1239. if (nr > vc->vc_cols - vc->vc_x)
  1240. nr = vc->vc_cols - vc->vc_x;
  1241. else if (!nr)
  1242. nr = 1;
  1243. delete_char(vc, nr);
  1244. }
  1245. /* console_sem is held */
  1246. static void csi_M(struct vc_data *vc, unsigned int nr)
  1247. {
  1248. if (nr > vc->vc_rows - vc->vc_y)
  1249. nr = vc->vc_rows - vc->vc_y;
  1250. else if (!nr)
  1251. nr=1;
  1252. scrup(vc, vc->vc_y, vc->vc_bottom, nr);
  1253. vc->vc_need_wrap = 0;
  1254. }
  1255. /* console_sem is held (except via vc_init->reset_terminal */
  1256. static void save_cur(struct vc_data *vc)
  1257. {
  1258. vc->vc_saved_x = vc->vc_x;
  1259. vc->vc_saved_y = vc->vc_y;
  1260. vc->vc_s_intensity = vc->vc_intensity;
  1261. vc->vc_s_underline = vc->vc_underline;
  1262. vc->vc_s_blink = vc->vc_blink;
  1263. vc->vc_s_reverse = vc->vc_reverse;
  1264. vc->vc_s_charset = vc->vc_charset;
  1265. vc->vc_s_color = vc->vc_color;
  1266. vc->vc_saved_G0 = vc->vc_G0_charset;
  1267. vc->vc_saved_G1 = vc->vc_G1_charset;
  1268. }
  1269. /* console_sem is held */
  1270. static void restore_cur(struct vc_data *vc)
  1271. {
  1272. gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
  1273. vc->vc_intensity = vc->vc_s_intensity;
  1274. vc->vc_underline = vc->vc_s_underline;
  1275. vc->vc_blink = vc->vc_s_blink;
  1276. vc->vc_reverse = vc->vc_s_reverse;
  1277. vc->vc_charset = vc->vc_s_charset;
  1278. vc->vc_color = vc->vc_s_color;
  1279. vc->vc_G0_charset = vc->vc_saved_G0;
  1280. vc->vc_G1_charset = vc->vc_saved_G1;
  1281. vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
  1282. update_attr(vc);
  1283. vc->vc_need_wrap = 0;
  1284. }
  1285. enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
  1286. EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
  1287. ESpalette };
  1288. /* console_sem is held (except via vc_init()) */
  1289. static void reset_terminal(struct vc_data *vc, int do_clear)
  1290. {
  1291. vc->vc_top = 0;
  1292. vc->vc_bottom = vc->vc_rows;
  1293. vc->vc_state = ESnormal;
  1294. vc->vc_ques = 0;
  1295. vc->vc_translate = set_translate(LAT1_MAP, vc);
  1296. vc->vc_G0_charset = LAT1_MAP;
  1297. vc->vc_G1_charset = GRAF_MAP;
  1298. vc->vc_charset = 0;
  1299. vc->vc_need_wrap = 0;
  1300. vc->vc_report_mouse = 0;
  1301. vc->vc_utf = 0;
  1302. vc->vc_utf_count = 0;
  1303. vc->vc_disp_ctrl = 0;
  1304. vc->vc_toggle_meta = 0;
  1305. vc->vc_decscnm = 0;
  1306. vc->vc_decom = 0;
  1307. vc->vc_decawm = 1;
  1308. vc->vc_deccm = 1;
  1309. vc->vc_decim = 0;
  1310. set_kbd(vc, decarm);
  1311. clr_kbd(vc, decckm);
  1312. clr_kbd(vc, kbdapplic);
  1313. clr_kbd(vc, lnm);
  1314. kbd_table[vc->vc_num].lockstate = 0;
  1315. kbd_table[vc->vc_num].slockstate = 0;
  1316. kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
  1317. kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
  1318. /* do not do set_leds here because this causes an endless tasklet loop
  1319. when the keyboard hasn't been initialized yet */
  1320. vc->vc_cursor_type = CUR_DEFAULT;
  1321. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1322. default_attr(vc);
  1323. update_attr(vc);
  1324. vc->vc_tab_stop[0] = 0x01010100;
  1325. vc->vc_tab_stop[1] =
  1326. vc->vc_tab_stop[2] =
  1327. vc->vc_tab_stop[3] =
  1328. vc->vc_tab_stop[4] = 0x01010101;
  1329. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1330. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1331. gotoxy(vc, 0, 0);
  1332. save_cur(vc);
  1333. if (do_clear)
  1334. csi_J(vc, 2);
  1335. }
  1336. /* console_sem is held */
  1337. static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
  1338. {
  1339. /*
  1340. * Control characters can be used in the _middle_
  1341. * of an escape sequence.
  1342. */
  1343. switch (c) {
  1344. case 0:
  1345. return;
  1346. case 7:
  1347. if (vc->vc_bell_duration)
  1348. kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
  1349. return;
  1350. case 8:
  1351. bs(vc);
  1352. return;
  1353. case 9:
  1354. vc->vc_pos -= (vc->vc_x << 1);
  1355. while (vc->vc_x < vc->vc_cols - 1) {
  1356. vc->vc_x++;
  1357. if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
  1358. break;
  1359. }
  1360. vc->vc_pos += (vc->vc_x << 1);
  1361. return;
  1362. case 10: case 11: case 12:
  1363. lf(vc);
  1364. if (!is_kbd(vc, lnm))
  1365. return;
  1366. case 13:
  1367. cr(vc);
  1368. return;
  1369. case 14:
  1370. vc->vc_charset = 1;
  1371. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1372. vc->vc_disp_ctrl = 1;
  1373. return;
  1374. case 15:
  1375. vc->vc_charset = 0;
  1376. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1377. vc->vc_disp_ctrl = 0;
  1378. return;
  1379. case 24: case 26:
  1380. vc->vc_state = ESnormal;
  1381. return;
  1382. case 27:
  1383. vc->vc_state = ESesc;
  1384. return;
  1385. case 127:
  1386. del(vc);
  1387. return;
  1388. case 128+27:
  1389. vc->vc_state = ESsquare;
  1390. return;
  1391. }
  1392. switch(vc->vc_state) {
  1393. case ESesc:
  1394. vc->vc_state = ESnormal;
  1395. switch (c) {
  1396. case '[':
  1397. vc->vc_state = ESsquare;
  1398. return;
  1399. case ']':
  1400. vc->vc_state = ESnonstd;
  1401. return;
  1402. case '%':
  1403. vc->vc_state = ESpercent;
  1404. return;
  1405. case 'E':
  1406. cr(vc);
  1407. lf(vc);
  1408. return;
  1409. case 'M':
  1410. ri(vc);
  1411. return;
  1412. case 'D':
  1413. lf(vc);
  1414. return;
  1415. case 'H':
  1416. vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
  1417. return;
  1418. case 'Z':
  1419. respond_ID(tty);
  1420. return;
  1421. case '7':
  1422. save_cur(vc);
  1423. return;
  1424. case '8':
  1425. restore_cur(vc);
  1426. return;
  1427. case '(':
  1428. vc->vc_state = ESsetG0;
  1429. return;
  1430. case ')':
  1431. vc->vc_state = ESsetG1;
  1432. return;
  1433. case '#':
  1434. vc->vc_state = EShash;
  1435. return;
  1436. case 'c':
  1437. reset_terminal(vc, 1);
  1438. return;
  1439. case '>': /* Numeric keypad */
  1440. clr_kbd(vc, kbdapplic);
  1441. return;
  1442. case '=': /* Appl. keypad */
  1443. set_kbd(vc, kbdapplic);
  1444. return;
  1445. }
  1446. return;
  1447. case ESnonstd:
  1448. if (c=='P') { /* palette escape sequence */
  1449. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1450. vc->vc_par[vc->vc_npar] = 0;
  1451. vc->vc_npar = 0;
  1452. vc->vc_state = ESpalette;
  1453. return;
  1454. } else if (c=='R') { /* reset palette */
  1455. reset_palette(vc);
  1456. vc->vc_state = ESnormal;
  1457. } else
  1458. vc->vc_state = ESnormal;
  1459. return;
  1460. case ESpalette:
  1461. if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
  1462. vc->vc_par[vc->vc_npar++] = (c > '9' ? (c & 0xDF) - 'A' + 10 : c - '0');
  1463. if (vc->vc_npar == 7) {
  1464. int i = vc->vc_par[0] * 3, j = 1;
  1465. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1466. vc->vc_palette[i++] += vc->vc_par[j++];
  1467. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1468. vc->vc_palette[i++] += vc->vc_par[j++];
  1469. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1470. vc->vc_palette[i] += vc->vc_par[j];
  1471. set_palette(vc);
  1472. vc->vc_state = ESnormal;
  1473. }
  1474. } else
  1475. vc->vc_state = ESnormal;
  1476. return;
  1477. case ESsquare:
  1478. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1479. vc->vc_par[vc->vc_npar] = 0;
  1480. vc->vc_npar = 0;
  1481. vc->vc_state = ESgetpars;
  1482. if (c == '[') { /* Function key */
  1483. vc->vc_state=ESfunckey;
  1484. return;
  1485. }
  1486. vc->vc_ques = (c == '?');
  1487. if (vc->vc_ques)
  1488. return;
  1489. case ESgetpars:
  1490. if (c == ';' && vc->vc_npar < NPAR - 1) {
  1491. vc->vc_npar++;
  1492. return;
  1493. } else if (c>='0' && c<='9') {
  1494. vc->vc_par[vc->vc_npar] *= 10;
  1495. vc->vc_par[vc->vc_npar] += c - '0';
  1496. return;
  1497. } else
  1498. vc->vc_state = ESgotpars;
  1499. case ESgotpars:
  1500. vc->vc_state = ESnormal;
  1501. switch(c) {
  1502. case 'h':
  1503. set_mode(vc, 1);
  1504. return;
  1505. case 'l':
  1506. set_mode(vc, 0);
  1507. return;
  1508. case 'c':
  1509. if (vc->vc_ques) {
  1510. if (vc->vc_par[0])
  1511. vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
  1512. else
  1513. vc->vc_cursor_type = CUR_DEFAULT;
  1514. return;
  1515. }
  1516. break;
  1517. case 'm':
  1518. if (vc->vc_ques) {
  1519. clear_selection();
  1520. if (vc->vc_par[0])
  1521. vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
  1522. else
  1523. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1524. return;
  1525. }
  1526. break;
  1527. case 'n':
  1528. if (!vc->vc_ques) {
  1529. if (vc->vc_par[0] == 5)
  1530. status_report(tty);
  1531. else if (vc->vc_par[0] == 6)
  1532. cursor_report(vc, tty);
  1533. }
  1534. return;
  1535. }
  1536. if (vc->vc_ques) {
  1537. vc->vc_ques = 0;
  1538. return;
  1539. }
  1540. switch(c) {
  1541. case 'G': case '`':
  1542. if (vc->vc_par[0])
  1543. vc->vc_par[0]--;
  1544. gotoxy(vc, vc->vc_par[0], vc->vc_y);
  1545. return;
  1546. case 'A':
  1547. if (!vc->vc_par[0])
  1548. vc->vc_par[0]++;
  1549. gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
  1550. return;
  1551. case 'B': case 'e':
  1552. if (!vc->vc_par[0])
  1553. vc->vc_par[0]++;
  1554. gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
  1555. return;
  1556. case 'C': case 'a':
  1557. if (!vc->vc_par[0])
  1558. vc->vc_par[0]++;
  1559. gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
  1560. return;
  1561. case 'D':
  1562. if (!vc->vc_par[0])
  1563. vc->vc_par[0]++;
  1564. gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
  1565. return;
  1566. case 'E':
  1567. if (!vc->vc_par[0])
  1568. vc->vc_par[0]++;
  1569. gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
  1570. return;
  1571. case 'F':
  1572. if (!vc->vc_par[0])
  1573. vc->vc_par[0]++;
  1574. gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
  1575. return;
  1576. case 'd':
  1577. if (vc->vc_par[0])
  1578. vc->vc_par[0]--;
  1579. gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
  1580. return;
  1581. case 'H': case 'f':
  1582. if (vc->vc_par[0])
  1583. vc->vc_par[0]--;
  1584. if (vc->vc_par[1])
  1585. vc->vc_par[1]--;
  1586. gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
  1587. return;
  1588. case 'J':
  1589. csi_J(vc, vc->vc_par[0]);
  1590. return;
  1591. case 'K':
  1592. csi_K(vc, vc->vc_par[0]);
  1593. return;
  1594. case 'L':
  1595. csi_L(vc, vc->vc_par[0]);
  1596. return;
  1597. case 'M':
  1598. csi_M(vc, vc->vc_par[0]);
  1599. return;
  1600. case 'P':
  1601. csi_P(vc, vc->vc_par[0]);
  1602. return;
  1603. case 'c':
  1604. if (!vc->vc_par[0])
  1605. respond_ID(tty);
  1606. return;
  1607. case 'g':
  1608. if (!vc->vc_par[0])
  1609. vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
  1610. else if (vc->vc_par[0] == 3) {
  1611. vc->vc_tab_stop[0] =
  1612. vc->vc_tab_stop[1] =
  1613. vc->vc_tab_stop[2] =
  1614. vc->vc_tab_stop[3] =
  1615. vc->vc_tab_stop[4] = 0;
  1616. }
  1617. return;
  1618. case 'm':
  1619. csi_m(vc);
  1620. return;
  1621. case 'q': /* DECLL - but only 3 leds */
  1622. /* map 0,1,2,3 to 0,1,2,4 */
  1623. if (vc->vc_par[0] < 4)
  1624. setledstate(kbd_table + vc->vc_num,
  1625. (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
  1626. return;
  1627. case 'r':
  1628. if (!vc->vc_par[0])
  1629. vc->vc_par[0]++;
  1630. if (!vc->vc_par[1])
  1631. vc->vc_par[1] = vc->vc_rows;
  1632. /* Minimum allowed region is 2 lines */
  1633. if (vc->vc_par[0] < vc->vc_par[1] &&
  1634. vc->vc_par[1] <= vc->vc_rows) {
  1635. vc->vc_top = vc->vc_par[0] - 1;
  1636. vc->vc_bottom = vc->vc_par[1];
  1637. gotoxay(vc, 0, 0);
  1638. }
  1639. return;
  1640. case 's':
  1641. save_cur(vc);
  1642. return;
  1643. case 'u':
  1644. restore_cur(vc);
  1645. return;
  1646. case 'X':
  1647. csi_X(vc, vc->vc_par[0]);
  1648. return;
  1649. case '@':
  1650. csi_at(vc, vc->vc_par[0]);
  1651. return;
  1652. case ']': /* setterm functions */
  1653. setterm_command(vc);
  1654. return;
  1655. }
  1656. return;
  1657. case ESpercent:
  1658. vc->vc_state = ESnormal;
  1659. switch (c) {
  1660. case '@': /* defined in ISO 2022 */
  1661. vc->vc_utf = 0;
  1662. return;
  1663. case 'G': /* prelim official escape code */
  1664. case '8': /* retained for compatibility */
  1665. vc->vc_utf = 1;
  1666. return;
  1667. }
  1668. return;
  1669. case ESfunckey:
  1670. vc->vc_state = ESnormal;
  1671. return;
  1672. case EShash:
  1673. vc->vc_state = ESnormal;
  1674. if (c == '8') {
  1675. /* DEC screen alignment test. kludge :-) */
  1676. vc->vc_video_erase_char =
  1677. (vc->vc_video_erase_char & 0xff00) | 'E';
  1678. csi_J(vc, 2);
  1679. vc->vc_video_erase_char =
  1680. (vc->vc_video_erase_char & 0xff00) | ' ';
  1681. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  1682. }
  1683. return;
  1684. case ESsetG0:
  1685. if (c == '0')
  1686. vc->vc_G0_charset = GRAF_MAP;
  1687. else if (c == 'B')
  1688. vc->vc_G0_charset = LAT1_MAP;
  1689. else if (c == 'U')
  1690. vc->vc_G0_charset = IBMPC_MAP;
  1691. else if (c == 'K')
  1692. vc->vc_G0_charset = USER_MAP;
  1693. if (vc->vc_charset == 0)
  1694. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1695. vc->vc_state = ESnormal;
  1696. return;
  1697. case ESsetG1:
  1698. if (c == '0')
  1699. vc->vc_G1_charset = GRAF_MAP;
  1700. else if (c == 'B')
  1701. vc->vc_G1_charset = LAT1_MAP;
  1702. else if (c == 'U')
  1703. vc->vc_G1_charset = IBMPC_MAP;
  1704. else if (c == 'K')
  1705. vc->vc_G1_charset = USER_MAP;
  1706. if (vc->vc_charset == 1)
  1707. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1708. vc->vc_state = ESnormal;
  1709. return;
  1710. default:
  1711. vc->vc_state = ESnormal;
  1712. }
  1713. }
  1714. /* This is a temporary buffer used to prepare a tty console write
  1715. * so that we can easily avoid touching user space while holding the
  1716. * console spinlock. It is allocated in con_init and is shared by
  1717. * this code and the vc_screen read/write tty calls.
  1718. *
  1719. * We have to allocate this statically in the kernel data section
  1720. * since console_init (and thus con_init) are called before any
  1721. * kernel memory allocation is available.
  1722. */
  1723. char con_buf[CON_BUF_SIZE];
  1724. DECLARE_MUTEX(con_buf_sem);
  1725. /* acquires console_sem */
  1726. static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1727. {
  1728. #ifdef VT_BUF_VRAM_ONLY
  1729. #define FLUSH do { } while(0);
  1730. #else
  1731. #define FLUSH if (draw_x >= 0) { \
  1732. vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
  1733. draw_x = -1; \
  1734. }
  1735. #endif
  1736. int c, tc, ok, n = 0, draw_x = -1;
  1737. unsigned int currcons;
  1738. unsigned long draw_from = 0, draw_to = 0;
  1739. struct vc_data *vc;
  1740. u16 himask, charmask;
  1741. const unsigned char *orig_buf = NULL;
  1742. int orig_count;
  1743. if (in_interrupt())
  1744. return count;
  1745. might_sleep();
  1746. acquire_console_sem();
  1747. vc = tty->driver_data;
  1748. if (vc == NULL) {
  1749. printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
  1750. release_console_sem();
  1751. return 0;
  1752. }
  1753. currcons = vc->vc_num;
  1754. if (!vc_cons_allocated(currcons)) {
  1755. /* could this happen? */
  1756. static int error = 0;
  1757. if (!error) {
  1758. error = 1;
  1759. printk("con_write: tty %d not allocated\n", currcons+1);
  1760. }
  1761. release_console_sem();
  1762. return 0;
  1763. }
  1764. release_console_sem();
  1765. orig_buf = buf;
  1766. orig_count = count;
  1767. /* At this point 'buf' is guaranteed to be a kernel buffer
  1768. * and therefore no access to userspace (and therefore sleeping)
  1769. * will be needed. The con_buf_sem serializes all tty based
  1770. * console rendering and vcs write/read operations. We hold
  1771. * the console spinlock during the entire write.
  1772. */
  1773. acquire_console_sem();
  1774. vc = tty->driver_data;
  1775. if (vc == NULL) {
  1776. printk(KERN_ERR "vt: argh, driver_data _became_ NULL !\n");
  1777. release_console_sem();
  1778. goto out;
  1779. }
  1780. himask = vc->vc_hi_font_mask;
  1781. charmask = himask ? 0x1ff : 0xff;
  1782. /* undraw cursor first */
  1783. if (IS_FG(vc))
  1784. hide_cursor(vc);
  1785. while (!tty->stopped && count) {
  1786. int orig = *buf;
  1787. c = orig;
  1788. buf++;
  1789. n++;
  1790. count--;
  1791. /* Do no translation at all in control states */
  1792. if (vc->vc_state != ESnormal) {
  1793. tc = c;
  1794. } else if (vc->vc_utf) {
  1795. /* Combine UTF-8 into Unicode */
  1796. /* Incomplete characters silently ignored */
  1797. if(c > 0x7f) {
  1798. if (vc->vc_utf_count > 0 && (c & 0xc0) == 0x80) {
  1799. vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
  1800. vc->vc_utf_count--;
  1801. if (vc->vc_utf_count == 0)
  1802. tc = c = vc->vc_utf_char;
  1803. else continue;
  1804. } else {
  1805. if ((c & 0xe0) == 0xc0) {
  1806. vc->vc_utf_count = 1;
  1807. vc->vc_utf_char = (c & 0x1f);
  1808. } else if ((c & 0xf0) == 0xe0) {
  1809. vc->vc_utf_count = 2;
  1810. vc->vc_utf_char = (c & 0x0f);
  1811. } else if ((c & 0xf8) == 0xf0) {
  1812. vc->vc_utf_count = 3;
  1813. vc->vc_utf_char = (c & 0x07);
  1814. } else if ((c & 0xfc) == 0xf8) {
  1815. vc->vc_utf_count = 4;
  1816. vc->vc_utf_char = (c & 0x03);
  1817. } else if ((c & 0xfe) == 0xfc) {
  1818. vc->vc_utf_count = 5;
  1819. vc->vc_utf_char = (c & 0x01);
  1820. } else
  1821. vc->vc_utf_count = 0;
  1822. continue;
  1823. }
  1824. } else {
  1825. tc = c;
  1826. vc->vc_utf_count = 0;
  1827. }
  1828. } else { /* no utf */
  1829. tc = vc->vc_translate[vc->vc_toggle_meta ? (c | 0x80) : c];
  1830. }
  1831. /* If the original code was a control character we
  1832. * only allow a glyph to be displayed if the code is
  1833. * not normally used (such as for cursor movement) or
  1834. * if the disp_ctrl mode has been explicitly enabled.
  1835. * Certain characters (as given by the CTRL_ALWAYS
  1836. * bitmap) are always displayed as control characters,
  1837. * as the console would be pretty useless without
  1838. * them; to display an arbitrary font position use the
  1839. * direct-to-font zone in UTF-8 mode.
  1840. */
  1841. ok = tc && (c >= 32 ||
  1842. (!vc->vc_utf && !(((vc->vc_disp_ctrl ? CTRL_ALWAYS
  1843. : CTRL_ACTION) >> c) & 1)))
  1844. && (c != 127 || vc->vc_disp_ctrl)
  1845. && (c != 128+27);
  1846. if (vc->vc_state == ESnormal && ok) {
  1847. /* Now try to find out how to display it */
  1848. tc = conv_uni_to_pc(vc, tc);
  1849. if ( tc == -4 ) {
  1850. /* If we got -4 (not found) then see if we have
  1851. defined a replacement character (U+FFFD) */
  1852. tc = conv_uni_to_pc(vc, 0xfffd);
  1853. /* One reason for the -4 can be that we just
  1854. did a clear_unimap();
  1855. try at least to show something. */
  1856. if (tc == -4)
  1857. tc = c;
  1858. } else if ( tc == -3 ) {
  1859. /* Bad hash table -- hope for the best */
  1860. tc = c;
  1861. }
  1862. if (tc & ~charmask)
  1863. continue; /* Conversion failed */
  1864. if (vc->vc_need_wrap || vc->vc_decim)
  1865. FLUSH
  1866. if (vc->vc_need_wrap) {
  1867. cr(vc);
  1868. lf(vc);
  1869. }
  1870. if (vc->vc_decim)
  1871. insert_char(vc, 1);
  1872. scr_writew(himask ?
  1873. ((vc->vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
  1874. (vc->vc_attr << 8) + tc,
  1875. (u16 *) vc->vc_pos);
  1876. if (DO_UPDATE(vc) && draw_x < 0) {
  1877. draw_x = vc->vc_x;
  1878. draw_from = vc->vc_pos;
  1879. }
  1880. if (vc->vc_x == vc->vc_cols - 1) {
  1881. vc->vc_need_wrap = vc->vc_decawm;
  1882. draw_to = vc->vc_pos + 2;
  1883. } else {
  1884. vc->vc_x++;
  1885. draw_to = (vc->vc_pos += 2);
  1886. }
  1887. continue;
  1888. }
  1889. FLUSH
  1890. do_con_trol(tty, vc, orig);
  1891. }
  1892. FLUSH
  1893. console_conditional_schedule();
  1894. release_console_sem();
  1895. out:
  1896. return n;
  1897. #undef FLUSH
  1898. }
  1899. /*
  1900. * This is the console switching callback.
  1901. *
  1902. * Doing console switching in a process context allows
  1903. * us to do the switches asynchronously (needed when we want
  1904. * to switch due to a keyboard interrupt). Synchronization
  1905. * with other console code and prevention of re-entrancy is
  1906. * ensured with console_sem.
  1907. */
  1908. static void console_callback(void *ignored)
  1909. {
  1910. acquire_console_sem();
  1911. if (want_console >= 0) {
  1912. if (want_console != fg_console &&
  1913. vc_cons_allocated(want_console)) {
  1914. hide_cursor(vc_cons[fg_console].d);
  1915. change_console(vc_cons[want_console].d);
  1916. /* we only changed when the console had already
  1917. been allocated - a new console is not created
  1918. in an interrupt routine */
  1919. }
  1920. want_console = -1;
  1921. }
  1922. if (do_poke_blanked_console) { /* do not unblank for a LED change */
  1923. do_poke_blanked_console = 0;
  1924. poke_blanked_console();
  1925. }
  1926. if (scrollback_delta) {
  1927. struct vc_data *vc = vc_cons[fg_console].d;
  1928. clear_selection();
  1929. if (vc->vc_mode == KD_TEXT)
  1930. vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
  1931. scrollback_delta = 0;
  1932. }
  1933. if (blank_timer_expired) {
  1934. do_blank_screen(0);
  1935. blank_timer_expired = 0;
  1936. }
  1937. release_console_sem();
  1938. }
  1939. void set_console(int nr)
  1940. {
  1941. want_console = nr;
  1942. schedule_console_callback();
  1943. }
  1944. struct tty_driver *console_driver;
  1945. #ifdef CONFIG_VT_CONSOLE
  1946. /*
  1947. * Console on virtual terminal
  1948. *
  1949. * The console must be locked when we get here.
  1950. */
  1951. static void vt_console_print(struct console *co, const char *b, unsigned count)
  1952. {
  1953. struct vc_data *vc = vc_cons[fg_console].d;
  1954. unsigned char c;
  1955. static unsigned long printing;
  1956. const ushort *start;
  1957. ushort cnt = 0;
  1958. ushort myx;
  1959. /* console busy or not yet initialized */
  1960. if (!printable || test_and_set_bit(0, &printing))
  1961. return;
  1962. if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
  1963. vc = vc_cons[kmsg_redirect - 1].d;
  1964. /* read `x' only after setting currcons properly (otherwise
  1965. the `x' macro will read the x of the foreground console). */
  1966. myx = vc->vc_x;
  1967. if (!vc_cons_allocated(fg_console)) {
  1968. /* impossible */
  1969. /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
  1970. goto quit;
  1971. }
  1972. if (vc->vc_mode != KD_TEXT)
  1973. goto quit;
  1974. /* undraw cursor first */
  1975. if (IS_FG(vc))
  1976. hide_cursor(vc);
  1977. start = (ushort *)vc->vc_pos;
  1978. /* Contrived structure to try to emulate original need_wrap behaviour
  1979. * Problems caused when we have need_wrap set on '\n' character */
  1980. while (count--) {
  1981. c = *b++;
  1982. if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
  1983. if (cnt > 0) {
  1984. if (CON_IS_VISIBLE(vc))
  1985. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  1986. vc->vc_x += cnt;
  1987. if (vc->vc_need_wrap)
  1988. vc->vc_x--;
  1989. cnt = 0;
  1990. }
  1991. if (c == 8) { /* backspace */
  1992. bs(vc);
  1993. start = (ushort *)vc->vc_pos;
  1994. myx = vc->vc_x;
  1995. continue;
  1996. }
  1997. if (c != 13)
  1998. lf(vc);
  1999. cr(vc);
  2000. start = (ushort *)vc->vc_pos;
  2001. myx = vc->vc_x;
  2002. if (c == 10 || c == 13)
  2003. continue;
  2004. }
  2005. scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
  2006. cnt++;
  2007. if (myx == vc->vc_cols - 1) {
  2008. vc->vc_need_wrap = 1;
  2009. continue;
  2010. }
  2011. vc->vc_pos += 2;
  2012. myx++;
  2013. }
  2014. if (cnt > 0) {
  2015. if (CON_IS_VISIBLE(vc))
  2016. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  2017. vc->vc_x += cnt;
  2018. if (vc->vc_x == vc->vc_cols) {
  2019. vc->vc_x--;
  2020. vc->vc_need_wrap = 1;
  2021. }
  2022. }
  2023. set_cursor(vc);
  2024. quit:
  2025. clear_bit(0, &printing);
  2026. }
  2027. static struct tty_driver *vt_console_device(struct console *c, int *index)
  2028. {
  2029. *index = c->index ? c->index-1 : fg_console;
  2030. return console_driver;
  2031. }
  2032. static struct console vt_console_driver = {
  2033. .name = "tty",
  2034. .write = vt_console_print,
  2035. .device = vt_console_device,
  2036. .unblank = unblank_screen,
  2037. .flags = CON_PRINTBUFFER,
  2038. .index = -1,
  2039. };
  2040. #endif
  2041. /*
  2042. * Handling of Linux-specific VC ioctls
  2043. */
  2044. /*
  2045. * Generally a bit racy with respect to console_sem().
  2046. *
  2047. * There are some functions which don't need it.
  2048. *
  2049. * There are some functions which can sleep for arbitrary periods
  2050. * (paste_selection) but we don't need the lock there anyway.
  2051. *
  2052. * set_selection has locking, and definitely needs it
  2053. */
  2054. int tioclinux(struct tty_struct *tty, unsigned long arg)
  2055. {
  2056. char type, data;
  2057. char __user *p = (char __user *)arg;
  2058. int lines;
  2059. int ret;
  2060. if (tty->driver->type != TTY_DRIVER_TYPE_CONSOLE)
  2061. return -EINVAL;
  2062. if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
  2063. return -EPERM;
  2064. if (get_user(type, p))
  2065. return -EFAULT;
  2066. ret = 0;
  2067. switch (type)
  2068. {
  2069. case TIOCL_SETSEL:
  2070. acquire_console_sem();
  2071. ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
  2072. release_console_sem();
  2073. break;
  2074. case TIOCL_PASTESEL:
  2075. ret = paste_selection(tty);
  2076. break;
  2077. case TIOCL_UNBLANKSCREEN:
  2078. acquire_console_sem();
  2079. unblank_screen();
  2080. release_console_sem();
  2081. break;
  2082. case TIOCL_SELLOADLUT:
  2083. ret = sel_loadlut(p);
  2084. break;
  2085. case TIOCL_GETSHIFTSTATE:
  2086. /*
  2087. * Make it possible to react to Shift+Mousebutton.
  2088. * Note that 'shift_state' is an undocumented
  2089. * kernel-internal variable; programs not closely
  2090. * related to the kernel should not use this.
  2091. */
  2092. data = shift_state;
  2093. ret = __put_user(data, p);
  2094. break;
  2095. case TIOCL_GETMOUSEREPORTING:
  2096. data = mouse_reporting();
  2097. ret = __put_user(data, p);
  2098. break;
  2099. case TIOCL_SETVESABLANK:
  2100. set_vesa_blanking(p);
  2101. break;
  2102. case TIOCL_SETKMSGREDIRECT:
  2103. if (!capable(CAP_SYS_ADMIN)) {
  2104. ret = -EPERM;
  2105. } else {
  2106. if (get_user(data, p+1))
  2107. ret = -EFAULT;
  2108. else
  2109. kmsg_redirect = data;
  2110. }
  2111. break;
  2112. case TIOCL_GETFGCONSOLE:
  2113. ret = fg_console;
  2114. break;
  2115. case TIOCL_SCROLLCONSOLE:
  2116. if (get_user(lines, (s32 __user *)(p+4))) {
  2117. ret = -EFAULT;
  2118. } else {
  2119. scrollfront(vc_cons[fg_console].d, lines);
  2120. ret = 0;
  2121. }
  2122. break;
  2123. case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
  2124. acquire_console_sem();
  2125. ignore_poke = 1;
  2126. do_blank_screen(0);
  2127. release_console_sem();
  2128. break;
  2129. case TIOCL_BLANKEDSCREEN:
  2130. ret = console_blanked;
  2131. break;
  2132. default:
  2133. ret = -EINVAL;
  2134. break;
  2135. }
  2136. return ret;
  2137. }
  2138. /*
  2139. * /dev/ttyN handling
  2140. */
  2141. static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  2142. {
  2143. int retval;
  2144. retval = do_con_write(tty, buf, count);
  2145. con_flush_chars(tty);
  2146. return retval;
  2147. }
  2148. static void con_put_char(struct tty_struct *tty, unsigned char ch)
  2149. {
  2150. if (in_interrupt())
  2151. return; /* n_r3964 calls put_char() from interrupt context */
  2152. do_con_write(tty, &ch, 1);
  2153. }
  2154. static int con_write_room(struct tty_struct *tty)
  2155. {
  2156. if (tty->stopped)
  2157. return 0;
  2158. return 4096; /* No limit, really; we're not buffering */
  2159. }
  2160. static int con_chars_in_buffer(struct tty_struct *tty)
  2161. {
  2162. return 0; /* we're not buffering */
  2163. }
  2164. /*
  2165. * con_throttle and con_unthrottle are only used for
  2166. * paste_selection(), which has to stuff in a large number of
  2167. * characters...
  2168. */
  2169. static void con_throttle(struct tty_struct *tty)
  2170. {
  2171. }
  2172. static void con_unthrottle(struct tty_struct *tty)
  2173. {
  2174. struct vc_data *vc = tty->driver_data;
  2175. wake_up_interruptible(&vc->paste_wait);
  2176. }
  2177. /*
  2178. * Turn the Scroll-Lock LED on when the tty is stopped
  2179. */
  2180. static void con_stop(struct tty_struct *tty)
  2181. {
  2182. int console_num;
  2183. if (!tty)
  2184. return;
  2185. console_num = tty->index;
  2186. if (!vc_cons_allocated(console_num))
  2187. return;
  2188. set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
  2189. set_leds();
  2190. }
  2191. /*
  2192. * Turn the Scroll-Lock LED off when the console is started
  2193. */
  2194. static void con_start(struct tty_struct *tty)
  2195. {
  2196. int console_num;
  2197. if (!tty)
  2198. return;
  2199. console_num = tty->index;
  2200. if (!vc_cons_allocated(console_num))
  2201. return;
  2202. clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
  2203. set_leds();
  2204. }
  2205. static void con_flush_chars(struct tty_struct *tty)
  2206. {
  2207. struct vc_data *vc;
  2208. if (in_interrupt()) /* from flush_to_ldisc */
  2209. return;
  2210. /* if we race with con_close(), vt may be null */
  2211. acquire_console_sem();
  2212. vc = tty->driver_data;
  2213. if (vc)
  2214. set_cursor(vc);
  2215. release_console_sem();
  2216. }
  2217. /*
  2218. * Allocate the console screen memory.
  2219. */
  2220. static int con_open(struct tty_struct *tty, struct file *filp)
  2221. {
  2222. unsigned int currcons = tty->index;
  2223. int ret = 0;
  2224. acquire_console_sem();
  2225. if (tty->driver_data == NULL) {
  2226. ret = vc_allocate(currcons);
  2227. if (ret == 0) {
  2228. struct vc_data *vc = vc_cons[currcons].d;
  2229. tty->driver_data = vc;
  2230. vc->vc_tty = tty;
  2231. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  2232. tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
  2233. tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
  2234. }
  2235. release_console_sem();
  2236. vcs_make_devfs(tty);
  2237. return ret;
  2238. }
  2239. }
  2240. release_console_sem();
  2241. return ret;
  2242. }
  2243. /*
  2244. * We take tty_sem in here to prevent another thread from coming in via init_dev
  2245. * and taking a ref against the tty while we're in the process of forgetting
  2246. * about it and cleaning things up.
  2247. *
  2248. * This is because vcs_remove_devfs() can sleep and will drop the BKL.
  2249. */
  2250. static void con_close(struct tty_struct *tty, struct file *filp)
  2251. {
  2252. down(&tty_sem);
  2253. acquire_console_sem();
  2254. if (tty && tty->count == 1) {
  2255. struct vc_data *vc = tty->driver_data;
  2256. if (vc)
  2257. vc->vc_tty = NULL;
  2258. tty->driver_data = NULL;
  2259. release_console_sem();
  2260. vcs_remove_devfs(tty);
  2261. up(&tty_sem);
  2262. /*
  2263. * tty_sem is released, but we still hold BKL, so there is
  2264. * still exclusion against init_dev()
  2265. */
  2266. return;
  2267. }
  2268. release_console_sem();
  2269. up(&tty_sem);
  2270. }
  2271. static void vc_init(struct vc_data *vc, unsigned int rows,
  2272. unsigned int cols, int do_clear)
  2273. {
  2274. int j, k ;
  2275. vc->vc_cols = cols;
  2276. vc->vc_rows = rows;
  2277. vc->vc_size_row = cols << 1;
  2278. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  2279. set_origin(vc);
  2280. vc->vc_pos = vc->vc_origin;
  2281. reset_vc(vc);
  2282. for (j=k=0; j<16; j++) {
  2283. vc->vc_palette[k++] = default_red[j] ;
  2284. vc->vc_palette[k++] = default_grn[j] ;
  2285. vc->vc_palette[k++] = default_blu[j] ;
  2286. }
  2287. vc->vc_def_color = 0x07; /* white */
  2288. vc->vc_ulcolor = 0x0f; /* bold white */
  2289. vc->vc_halfcolor = 0x08; /* grey */
  2290. init_waitqueue_head(&vc->paste_wait);
  2291. reset_terminal(vc, do_clear);
  2292. }
  2293. /*
  2294. * This routine initializes console interrupts, and does nothing
  2295. * else. If you want the screen to clear, call tty_write with
  2296. * the appropriate escape-sequence.
  2297. */
  2298. static int __init con_init(void)
  2299. {
  2300. const char *display_desc = NULL;
  2301. struct vc_data *vc;
  2302. unsigned int currcons = 0;
  2303. acquire_console_sem();
  2304. if (conswitchp)
  2305. display_desc = conswitchp->con_startup();
  2306. if (!display_desc) {
  2307. fg_console = 0;
  2308. release_console_sem();
  2309. return 0;
  2310. }
  2311. init_timer(&console_timer);
  2312. console_timer.function = blank_screen_t;
  2313. if (blankinterval) {
  2314. blank_state = blank_normal_wait;
  2315. mod_timer(&console_timer, jiffies + blankinterval);
  2316. }
  2317. /*
  2318. * kmalloc is not running yet - we use the bootmem allocator.
  2319. */
  2320. for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
  2321. vc_cons[currcons].d = vc = alloc_bootmem(sizeof(struct vc_data));
  2322. visual_init(vc, currcons, 1);
  2323. vc->vc_screenbuf = (unsigned short *)alloc_bootmem(vc->vc_screenbuf_size);
  2324. vc->vc_kmalloced = 0;
  2325. vc_init(vc, vc->vc_rows, vc->vc_cols,
  2326. currcons || !vc->vc_sw->con_save_screen);
  2327. }
  2328. currcons = fg_console = 0;
  2329. master_display_fg = vc = vc_cons[currcons].d;
  2330. set_origin(vc);
  2331. save_screen(vc);
  2332. gotoxy(vc, vc->vc_x, vc->vc_y);
  2333. csi_J(vc, 0);
  2334. update_screen(vc);
  2335. printk("Console: %s %s %dx%d",
  2336. vc->vc_can_do_color ? "colour" : "mono",
  2337. display_desc, vc->vc_cols, vc->vc_rows);
  2338. printable = 1;
  2339. printk("\n");
  2340. release_console_sem();
  2341. #ifdef CONFIG_VT_CONSOLE
  2342. register_console(&vt_console_driver);
  2343. #endif
  2344. return 0;
  2345. }
  2346. console_initcall(con_init);
  2347. static struct tty_operations con_ops = {
  2348. .open = con_open,
  2349. .close = con_close,
  2350. .write = con_write,
  2351. .write_room = con_write_room,
  2352. .put_char = con_put_char,
  2353. .flush_chars = con_flush_chars,
  2354. .chars_in_buffer = con_chars_in_buffer,
  2355. .ioctl = vt_ioctl,
  2356. .stop = con_stop,
  2357. .start = con_start,
  2358. .throttle = con_throttle,
  2359. .unthrottle = con_unthrottle,
  2360. };
  2361. int __init vty_init(void)
  2362. {
  2363. vcs_init();
  2364. console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
  2365. if (!console_driver)
  2366. panic("Couldn't allocate console driver\n");
  2367. console_driver->owner = THIS_MODULE;
  2368. console_driver->devfs_name = "vc/";
  2369. console_driver->name = "tty";
  2370. console_driver->name_base = 1;
  2371. console_driver->major = TTY_MAJOR;
  2372. console_driver->minor_start = 1;
  2373. console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  2374. console_driver->init_termios = tty_std_termios;
  2375. console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  2376. tty_set_operations(console_driver, &con_ops);
  2377. if (tty_register_driver(console_driver))
  2378. panic("Couldn't register console driver\n");
  2379. kbd_init();
  2380. console_map_init();
  2381. #ifdef CONFIG_PROM_CONSOLE
  2382. prom_con_init();
  2383. #endif
  2384. #ifdef CONFIG_MDA_CONSOLE
  2385. mda_console_init();
  2386. #endif
  2387. return 0;
  2388. }
  2389. #ifndef VT_SINGLE_DRIVER
  2390. /*
  2391. * If we support more console drivers, this function is used
  2392. * when a driver wants to take over some existing consoles
  2393. * and become default driver for newly opened ones.
  2394. */
  2395. int take_over_console(const struct consw *csw, int first, int last, int deflt)
  2396. {
  2397. int i, j = -1;
  2398. const char *desc;
  2399. struct module *owner;
  2400. owner = csw->owner;
  2401. if (!try_module_get(owner))
  2402. return -ENODEV;
  2403. acquire_console_sem();
  2404. desc = csw->con_startup();
  2405. if (!desc) {
  2406. release_console_sem();
  2407. module_put(owner);
  2408. return -ENODEV;
  2409. }
  2410. if (deflt) {
  2411. if (conswitchp)
  2412. module_put(conswitchp->owner);
  2413. __module_get(owner);
  2414. conswitchp = csw;
  2415. }
  2416. for (i = first; i <= last; i++) {
  2417. int old_was_color;
  2418. struct vc_data *vc = vc_cons[i].d;
  2419. if (con_driver_map[i])
  2420. module_put(con_driver_map[i]->owner);
  2421. __module_get(owner);
  2422. con_driver_map[i] = csw;
  2423. if (!vc || !vc->vc_sw)
  2424. continue;
  2425. j = i;
  2426. if (CON_IS_VISIBLE(vc))
  2427. save_screen(vc);
  2428. old_was_color = vc->vc_can_do_color;
  2429. vc->vc_sw->con_deinit(vc);
  2430. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  2431. vc->vc_visible_origin = vc->vc_origin;
  2432. vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
  2433. vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
  2434. visual_init(vc, i, 0);
  2435. update_attr(vc);
  2436. /* If the console changed between mono <-> color, then
  2437. * the attributes in the screenbuf will be wrong. The
  2438. * following resets all attributes to something sane.
  2439. */
  2440. if (old_was_color != vc->vc_can_do_color)
  2441. clear_buffer_attributes(vc);
  2442. if (CON_IS_VISIBLE(vc))
  2443. update_screen(vc);
  2444. }
  2445. printk("Console: switching ");
  2446. if (!deflt)
  2447. printk("consoles %d-%d ", first+1, last+1);
  2448. if (j >= 0)
  2449. printk("to %s %s %dx%d\n",
  2450. vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
  2451. desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
  2452. else
  2453. printk("to %s\n", desc);
  2454. release_console_sem();
  2455. module_put(owner);
  2456. return 0;
  2457. }
  2458. void give_up_console(const struct consw *csw)
  2459. {
  2460. int i;
  2461. for(i = 0; i < MAX_NR_CONSOLES; i++)
  2462. if (con_driver_map[i] == csw) {
  2463. module_put(csw->owner);
  2464. con_driver_map[i] = NULL;
  2465. }
  2466. }
  2467. #endif
  2468. /*
  2469. * Screen blanking
  2470. */
  2471. static void set_vesa_blanking(char __user *p)
  2472. {
  2473. unsigned int mode;
  2474. get_user(mode, p + 1);
  2475. vesa_blank_mode = (mode < 4) ? mode : 0;
  2476. }
  2477. /*
  2478. * This is called by a timer handler
  2479. */
  2480. static void vesa_powerdown(void)
  2481. {
  2482. struct vc_data *c = vc_cons[fg_console].d;
  2483. /*
  2484. * Power down if currently suspended (1 or 2),
  2485. * suspend if currently blanked (0),
  2486. * else do nothing (i.e. already powered down (3)).
  2487. * Called only if powerdown features are allowed.
  2488. */
  2489. switch (vesa_blank_mode) {
  2490. case VESA_NO_BLANKING:
  2491. c->vc_sw->con_blank(c, VESA_VSYNC_SUSPEND+1, 0);
  2492. break;
  2493. case VESA_VSYNC_SUSPEND:
  2494. case VESA_HSYNC_SUSPEND:
  2495. c->vc_sw->con_blank(c, VESA_POWERDOWN+1, 0);
  2496. break;
  2497. }
  2498. }
  2499. void do_blank_screen(int entering_gfx)
  2500. {
  2501. struct vc_data *vc = vc_cons[fg_console].d;
  2502. int i;
  2503. WARN_CONSOLE_UNLOCKED();
  2504. if (console_blanked) {
  2505. if (blank_state == blank_vesa_wait) {
  2506. blank_state = blank_off;
  2507. vesa_powerdown();
  2508. }
  2509. return;
  2510. }
  2511. if (blank_state != blank_normal_wait)
  2512. return;
  2513. blank_state = blank_off;
  2514. /* entering graphics mode? */
  2515. if (entering_gfx) {
  2516. hide_cursor(vc);
  2517. save_screen(vc);
  2518. vc->vc_sw->con_blank(vc, -1, 1);
  2519. console_blanked = fg_console + 1;
  2520. set_origin(vc);
  2521. return;
  2522. }
  2523. /* don't blank graphics */
  2524. if (vc->vc_mode != KD_TEXT) {
  2525. console_blanked = fg_console + 1;
  2526. return;
  2527. }
  2528. hide_cursor(vc);
  2529. del_timer_sync(&console_timer);
  2530. blank_timer_expired = 0;
  2531. save_screen(vc);
  2532. /* In case we need to reset origin, blanking hook returns 1 */
  2533. i = vc->vc_sw->con_blank(vc, 1, 0);
  2534. console_blanked = fg_console + 1;
  2535. if (i)
  2536. set_origin(vc);
  2537. if (console_blank_hook && console_blank_hook(1))
  2538. return;
  2539. if (vesa_off_interval) {
  2540. blank_state = blank_vesa_wait;
  2541. mod_timer(&console_timer, jiffies + vesa_off_interval);
  2542. }
  2543. if (vesa_blank_mode)
  2544. vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
  2545. }
  2546. EXPORT_SYMBOL(do_blank_screen);
  2547. /*
  2548. * Called by timer as well as from vt_console_driver
  2549. */
  2550. void do_unblank_screen(int leaving_gfx)
  2551. {
  2552. struct vc_data *vc;
  2553. /* This should now always be called from a "sane" (read: can schedule)
  2554. * context for the sake of the low level drivers, except in the special
  2555. * case of oops_in_progress
  2556. */
  2557. if (!oops_in_progress)
  2558. might_sleep();
  2559. WARN_CONSOLE_UNLOCKED();
  2560. ignore_poke = 0;
  2561. if (!console_blanked)
  2562. return;
  2563. if (!vc_cons_allocated(fg_console)) {
  2564. /* impossible */
  2565. printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
  2566. return;
  2567. }
  2568. vc = vc_cons[fg_console].d;
  2569. if (vc->vc_mode != KD_TEXT)
  2570. return; /* but leave console_blanked != 0 */
  2571. if (blankinterval) {
  2572. mod_timer(&console_timer, jiffies + blankinterval);
  2573. blank_state = blank_normal_wait;
  2574. }
  2575. console_blanked = 0;
  2576. if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
  2577. /* Low-level driver cannot restore -> do it ourselves */
  2578. update_screen(vc);
  2579. if (console_blank_hook)
  2580. console_blank_hook(0);
  2581. set_palette(vc);
  2582. set_cursor(vc);
  2583. }
  2584. EXPORT_SYMBOL(do_unblank_screen);
  2585. /*
  2586. * This is called by the outside world to cause a forced unblank, mostly for
  2587. * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
  2588. * call it with 1 as an argument and so force a mode restore... that may kill
  2589. * X or at least garbage the screen but would also make the Oops visible...
  2590. */
  2591. void unblank_screen(void)
  2592. {
  2593. do_unblank_screen(0);
  2594. }
  2595. /*
  2596. * We defer the timer blanking to work queue so it can take the console semaphore
  2597. * (console operations can still happen at irq time, but only from printk which
  2598. * has the console semaphore. Not perfect yet, but better than no locking
  2599. */
  2600. static void blank_screen_t(unsigned long dummy)
  2601. {
  2602. if (unlikely(!keventd_up())) {
  2603. mod_timer(&console_timer, jiffies + blankinterval);
  2604. return;
  2605. }
  2606. blank_timer_expired = 1;
  2607. schedule_work(&console_work);
  2608. }
  2609. void poke_blanked_console(void)
  2610. {
  2611. WARN_CONSOLE_UNLOCKED();
  2612. /* Add this so we quickly catch whoever might call us in a non
  2613. * safe context. Nowadays, unblank_screen() isn't to be called in
  2614. * atomic contexts and is allowed to schedule (with the special case
  2615. * of oops_in_progress, but that isn't of any concern for this
  2616. * function. --BenH.
  2617. */
  2618. might_sleep();
  2619. /* This isn't perfectly race free, but a race here would be mostly harmless,
  2620. * at worse, we'll do a spurrious blank and it's unlikely
  2621. */
  2622. del_timer(&console_timer);
  2623. blank_timer_expired = 0;
  2624. if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
  2625. return;
  2626. if (console_blanked)
  2627. unblank_screen();
  2628. else if (blankinterval) {
  2629. mod_timer(&console_timer, jiffies + blankinterval);
  2630. blank_state = blank_normal_wait;
  2631. }
  2632. }
  2633. /*
  2634. * Palettes
  2635. */
  2636. static void set_palette(struct vc_data *vc)
  2637. {
  2638. WARN_CONSOLE_UNLOCKED();
  2639. if (vc->vc_mode != KD_GRAPHICS)
  2640. vc->vc_sw->con_set_palette(vc, color_table);
  2641. }
  2642. static int set_get_cmap(unsigned char __user *arg, int set)
  2643. {
  2644. int i, j, k;
  2645. WARN_CONSOLE_UNLOCKED();
  2646. for (i = 0; i < 16; i++)
  2647. if (set) {
  2648. get_user(default_red[i], arg++);
  2649. get_user(default_grn[i], arg++);
  2650. get_user(default_blu[i], arg++);
  2651. } else {
  2652. put_user(default_red[i], arg++);
  2653. put_user(default_grn[i], arg++);
  2654. put_user(default_blu[i], arg++);
  2655. }
  2656. if (set) {
  2657. for (i = 0; i < MAX_NR_CONSOLES; i++)
  2658. if (vc_cons_allocated(i)) {
  2659. for (j = k = 0; j < 16; j++) {
  2660. vc_cons[i].d->vc_palette[k++] = default_red[j];
  2661. vc_cons[i].d->vc_palette[k++] = default_grn[j];
  2662. vc_cons[i].d->vc_palette[k++] = default_blu[j];
  2663. }
  2664. set_palette(vc_cons[i].d);
  2665. }
  2666. }
  2667. return 0;
  2668. }
  2669. /*
  2670. * Load palette into the DAC registers. arg points to a colour
  2671. * map, 3 bytes per colour, 16 colours, range from 0 to 255.
  2672. */
  2673. int con_set_cmap(unsigned char __user *arg)
  2674. {
  2675. int rc;
  2676. acquire_console_sem();
  2677. rc = set_get_cmap (arg,1);
  2678. release_console_sem();
  2679. return rc;
  2680. }
  2681. int con_get_cmap(unsigned char __user *arg)
  2682. {
  2683. int rc;
  2684. acquire_console_sem();
  2685. rc = set_get_cmap (arg,0);
  2686. release_console_sem();
  2687. return rc;
  2688. }
  2689. void reset_palette(struct vc_data *vc)
  2690. {
  2691. int j, k;
  2692. for (j=k=0; j<16; j++) {
  2693. vc->vc_palette[k++] = default_red[j];
  2694. vc->vc_palette[k++] = default_grn[j];
  2695. vc->vc_palette[k++] = default_blu[j];
  2696. }
  2697. set_palette(vc);
  2698. }
  2699. /*
  2700. * Font switching
  2701. *
  2702. * Currently we only support fonts up to 32 pixels wide, at a maximum height
  2703. * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
  2704. * depending on width) reserved for each character which is kinda wasty, but
  2705. * this is done in order to maintain compatibility with the EGA/VGA fonts. It
  2706. * is upto the actual low-level console-driver convert data into its favorite
  2707. * format (maybe we should add a `fontoffset' field to the `display'
  2708. * structure so we won't have to convert the fontdata all the time.
  2709. * /Jes
  2710. */
  2711. #define max_font_size 65536
  2712. static int con_font_get(struct vc_data *vc, struct console_font_op *op)
  2713. {
  2714. struct console_font font;
  2715. int rc = -EINVAL;
  2716. int c;
  2717. if (vc->vc_mode != KD_TEXT)
  2718. return -EINVAL;
  2719. if (op->data) {
  2720. font.data = kmalloc(max_font_size, GFP_KERNEL);
  2721. if (!font.data)
  2722. return -ENOMEM;
  2723. } else
  2724. font.data = NULL;
  2725. acquire_console_sem();
  2726. if (vc->vc_sw->con_font_get)
  2727. rc = vc->vc_sw->con_font_get(vc, &font);
  2728. else
  2729. rc = -ENOSYS;
  2730. release_console_sem();
  2731. if (rc)
  2732. goto out;
  2733. c = (font.width+7)/8 * 32 * font.charcount;
  2734. if (op->data && font.charcount > op->charcount)
  2735. rc = -ENOSPC;
  2736. if (!(op->flags & KD_FONT_FLAG_OLD)) {
  2737. if (font.width > op->width || font.height > op->height)
  2738. rc = -ENOSPC;
  2739. } else {
  2740. if (font.width != 8)
  2741. rc = -EIO;
  2742. else if ((op->height && font.height > op->height) ||
  2743. font.height > 32)
  2744. rc = -ENOSPC;
  2745. }
  2746. if (rc)
  2747. goto out;
  2748. op->height = font.height;
  2749. op->width = font.width;
  2750. op->charcount = font.charcount;
  2751. if (op->data && copy_to_user(op->data, font.data, c))
  2752. rc = -EFAULT;
  2753. out:
  2754. kfree(font.data);
  2755. return rc;
  2756. }
  2757. static int con_font_set(struct vc_data *vc, struct console_font_op *op)
  2758. {
  2759. struct console_font font;
  2760. int rc = -EINVAL;
  2761. int size;
  2762. if (vc->vc_mode != KD_TEXT)
  2763. return -EINVAL;
  2764. if (!op->data)
  2765. return -EINVAL;
  2766. if (op->charcount > 512)
  2767. return -EINVAL;
  2768. if (!op->height) { /* Need to guess font height [compat] */
  2769. int h, i;
  2770. u8 __user *charmap = op->data;
  2771. u8 tmp;
  2772. /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
  2773. so that we can get rid of this soon */
  2774. if (!(op->flags & KD_FONT_FLAG_OLD))
  2775. return -EINVAL;
  2776. for (h = 32; h > 0; h--)
  2777. for (i = 0; i < op->charcount; i++) {
  2778. if (get_user(tmp, &charmap[32*i+h-1]))
  2779. return -EFAULT;
  2780. if (tmp)
  2781. goto nonzero;
  2782. }
  2783. return -EINVAL;
  2784. nonzero:
  2785. op->height = h;
  2786. }
  2787. if (op->width <= 0 || op->width > 32 || op->height > 32)
  2788. return -EINVAL;
  2789. size = (op->width+7)/8 * 32 * op->charcount;
  2790. if (size > max_font_size)
  2791. return -ENOSPC;
  2792. font.charcount = op->charcount;
  2793. font.height = op->height;
  2794. font.width = op->width;
  2795. font.data = kmalloc(size, GFP_KERNEL);
  2796. if (!font.data)
  2797. return -ENOMEM;
  2798. if (copy_from_user(font.data, op->data, size)) {
  2799. kfree(font.data);
  2800. return -EFAULT;
  2801. }
  2802. acquire_console_sem();
  2803. if (vc->vc_sw->con_font_set)
  2804. rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
  2805. else
  2806. rc = -ENOSYS;
  2807. release_console_sem();
  2808. kfree(font.data);
  2809. return rc;
  2810. }
  2811. static int con_font_default(struct vc_data *vc, struct console_font_op *op)
  2812. {
  2813. struct console_font font = {.width = op->width, .height = op->height};
  2814. char name[MAX_FONT_NAME];
  2815. char *s = name;
  2816. int rc;
  2817. if (vc->vc_mode != KD_TEXT)
  2818. return -EINVAL;
  2819. if (!op->data)
  2820. s = NULL;
  2821. else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
  2822. return -EFAULT;
  2823. else
  2824. name[MAX_FONT_NAME - 1] = 0;
  2825. acquire_console_sem();
  2826. if (vc->vc_sw->con_font_default)
  2827. rc = vc->vc_sw->con_font_default(vc, &font, s);
  2828. else
  2829. rc = -ENOSYS;
  2830. release_console_sem();
  2831. if (!rc) {
  2832. op->width = font.width;
  2833. op->height = font.height;
  2834. }
  2835. return rc;
  2836. }
  2837. static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
  2838. {
  2839. int con = op->height;
  2840. int rc;
  2841. if (vc->vc_mode != KD_TEXT)
  2842. return -EINVAL;
  2843. acquire_console_sem();
  2844. if (!vc->vc_sw->con_font_copy)
  2845. rc = -ENOSYS;
  2846. else if (con < 0 || !vc_cons_allocated(con))
  2847. rc = -ENOTTY;
  2848. else if (con == vc->vc_num) /* nothing to do */
  2849. rc = 0;
  2850. else
  2851. rc = vc->vc_sw->con_font_copy(vc, con);
  2852. release_console_sem();
  2853. return rc;
  2854. }
  2855. int con_font_op(struct vc_data *vc, struct console_font_op *op)
  2856. {
  2857. switch (op->op) {
  2858. case KD_FONT_OP_SET:
  2859. return con_font_set(vc, op);
  2860. case KD_FONT_OP_GET:
  2861. return con_font_get(vc, op);
  2862. case KD_FONT_OP_SET_DEFAULT:
  2863. return con_font_default(vc, op);
  2864. case KD_FONT_OP_COPY:
  2865. return con_font_copy(vc, op);
  2866. }
  2867. return -ENOSYS;
  2868. }
  2869. /*
  2870. * Interface exported to selection and vcs.
  2871. */
  2872. /* used by selection */
  2873. u16 screen_glyph(struct vc_data *vc, int offset)
  2874. {
  2875. u16 w = scr_readw(screenpos(vc, offset, 1));
  2876. u16 c = w & 0xff;
  2877. if (w & vc->vc_hi_font_mask)
  2878. c |= 0x100;
  2879. return c;
  2880. }
  2881. /* used by vcs - note the word offset */
  2882. unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
  2883. {
  2884. return screenpos(vc, 2 * w_offset, viewed);
  2885. }
  2886. void getconsxy(struct vc_data *vc, unsigned char *p)
  2887. {
  2888. p[0] = vc->vc_x;
  2889. p[1] = vc->vc_y;
  2890. }
  2891. void putconsxy(struct vc_data *vc, unsigned char *p)
  2892. {
  2893. gotoxy(vc, p[0], p[1]);
  2894. set_cursor(vc);
  2895. }
  2896. u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
  2897. {
  2898. if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
  2899. return softcursor_original;
  2900. return scr_readw(org);
  2901. }
  2902. void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
  2903. {
  2904. scr_writew(val, org);
  2905. if ((unsigned long)org == vc->vc_pos) {
  2906. softcursor_original = -1;
  2907. add_softcursor(vc);
  2908. }
  2909. }
  2910. /*
  2911. * Visible symbols for modules
  2912. */
  2913. EXPORT_SYMBOL(color_table);
  2914. EXPORT_SYMBOL(default_red);
  2915. EXPORT_SYMBOL(default_grn);
  2916. EXPORT_SYMBOL(default_blu);
  2917. EXPORT_SYMBOL(update_region);
  2918. EXPORT_SYMBOL(redraw_screen);
  2919. EXPORT_SYMBOL(vc_resize);
  2920. EXPORT_SYMBOL(fg_console);
  2921. EXPORT_SYMBOL(console_blank_hook);
  2922. EXPORT_SYMBOL(console_blanked);
  2923. EXPORT_SYMBOL(vc_cons);
  2924. #ifndef VT_SINGLE_DRIVER
  2925. EXPORT_SYMBOL(take_over_console);
  2926. EXPORT_SYMBOL(give_up_console);
  2927. #endif