vt.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267
  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 int end;
  670. unsigned short *newscreen;
  671. WARN_CONSOLE_UNLOCKED();
  672. if (!vc)
  673. return -ENXIO;
  674. if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
  675. return -EINVAL;
  676. new_cols = (cols ? cols : vc->vc_cols);
  677. new_rows = (lines ? lines : vc->vc_rows);
  678. new_row_size = new_cols << 1;
  679. new_screen_size = new_row_size * new_rows;
  680. if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
  681. return 0;
  682. newscreen = (unsigned short *) kmalloc(new_screen_size, GFP_USER);
  683. if (!newscreen)
  684. return -ENOMEM;
  685. old_rows = vc->vc_rows;
  686. old_cols = vc->vc_cols;
  687. old_row_size = vc->vc_size_row;
  688. old_screen_size = vc->vc_screenbuf_size;
  689. err = resize_screen(vc, new_cols, new_rows);
  690. if (err) {
  691. kfree(newscreen);
  692. return err;
  693. }
  694. vc->vc_rows = new_rows;
  695. vc->vc_cols = new_cols;
  696. vc->vc_size_row = new_row_size;
  697. vc->vc_screenbuf_size = new_screen_size;
  698. rlth = min(old_row_size, new_row_size);
  699. rrem = new_row_size - rlth;
  700. old_origin = vc->vc_origin;
  701. new_origin = (long) newscreen;
  702. new_scr_end = new_origin + new_screen_size;
  703. if (vc->vc_y > new_rows) {
  704. if (old_rows - vc->vc_y < new_rows) {
  705. /*
  706. * Cursor near the bottom, copy contents from the
  707. * bottom of buffer
  708. */
  709. old_origin += (old_rows - new_rows) * old_row_size;
  710. end = vc->vc_scr_end;
  711. } else {
  712. /*
  713. * Cursor is in no man's land, copy 1/2 screenful
  714. * from the top and bottom of cursor position
  715. */
  716. old_origin += (vc->vc_y - new_rows/2) * old_row_size;
  717. end = old_origin + (old_row_size * new_rows);
  718. }
  719. } else
  720. /*
  721. * Cursor near the top, copy contents from the top of buffer
  722. */
  723. end = (old_rows > new_rows) ? old_origin +
  724. (old_row_size * new_rows) :
  725. vc->vc_scr_end;
  726. update_attr(vc);
  727. while (old_origin < end) {
  728. scr_memcpyw((unsigned short *) new_origin,
  729. (unsigned short *) old_origin, rlth);
  730. if (rrem)
  731. scr_memsetw((void *)(new_origin + rlth),
  732. vc->vc_video_erase_char, rrem);
  733. old_origin += old_row_size;
  734. new_origin += new_row_size;
  735. }
  736. if (new_scr_end > new_origin)
  737. scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
  738. new_scr_end - new_origin);
  739. if (vc->vc_kmalloced)
  740. kfree(vc->vc_screenbuf);
  741. vc->vc_screenbuf = newscreen;
  742. vc->vc_kmalloced = 1;
  743. vc->vc_screenbuf_size = new_screen_size;
  744. set_origin(vc);
  745. /* do part of a reset_terminal() */
  746. vc->vc_top = 0;
  747. vc->vc_bottom = vc->vc_rows;
  748. gotoxy(vc, vc->vc_x, vc->vc_y);
  749. save_cur(vc);
  750. if (vc->vc_tty) {
  751. struct winsize ws, *cws = &vc->vc_tty->winsize;
  752. memset(&ws, 0, sizeof(ws));
  753. ws.ws_row = vc->vc_rows;
  754. ws.ws_col = vc->vc_cols;
  755. ws.ws_ypixel = vc->vc_scan_lines;
  756. if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
  757. vc->vc_tty->pgrp > 0)
  758. kill_pg(vc->vc_tty->pgrp, SIGWINCH, 1);
  759. *cws = ws;
  760. }
  761. if (CON_IS_VISIBLE(vc))
  762. update_screen(vc);
  763. return err;
  764. }
  765. void vc_disallocate(unsigned int currcons)
  766. {
  767. WARN_CONSOLE_UNLOCKED();
  768. if (vc_cons_allocated(currcons)) {
  769. struct vc_data *vc = vc_cons[currcons].d;
  770. vc->vc_sw->con_deinit(vc);
  771. if (vc->vc_kmalloced)
  772. kfree(vc->vc_screenbuf);
  773. if (currcons >= MIN_NR_CONSOLES)
  774. kfree(vc);
  775. vc_cons[currcons].d = NULL;
  776. }
  777. }
  778. /*
  779. * VT102 emulator
  780. */
  781. #define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  782. #define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  783. #define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  784. #define decarm VC_REPEAT
  785. #define decckm VC_CKMODE
  786. #define kbdapplic VC_APPLIC
  787. #define lnm VC_CRLF
  788. /*
  789. * this is what the terminal answers to a ESC-Z or csi0c query.
  790. */
  791. #define VT100ID "\033[?1;2c"
  792. #define VT102ID "\033[?6c"
  793. unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
  794. 8,12,10,14, 9,13,11,15 };
  795. /* the default colour table, for VGA+ colour systems */
  796. int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
  797. 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
  798. int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
  799. 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
  800. int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
  801. 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
  802. /*
  803. * gotoxy() must verify all boundaries, because the arguments
  804. * might also be negative. If the given position is out of
  805. * bounds, the cursor is placed at the nearest margin.
  806. */
  807. static void gotoxy(struct vc_data *vc, int new_x, int new_y)
  808. {
  809. int min_y, max_y;
  810. if (new_x < 0)
  811. vc->vc_x = 0;
  812. else {
  813. if (new_x >= vc->vc_cols)
  814. vc->vc_x = vc->vc_cols - 1;
  815. else
  816. vc->vc_x = new_x;
  817. }
  818. if (vc->vc_decom) {
  819. min_y = vc->vc_top;
  820. max_y = vc->vc_bottom;
  821. } else {
  822. min_y = 0;
  823. max_y = vc->vc_rows;
  824. }
  825. if (new_y < min_y)
  826. vc->vc_y = min_y;
  827. else if (new_y >= max_y)
  828. vc->vc_y = max_y - 1;
  829. else
  830. vc->vc_y = new_y;
  831. vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
  832. vc->vc_need_wrap = 0;
  833. }
  834. /* for absolute user moves, when decom is set */
  835. static void gotoxay(struct vc_data *vc, int new_x, int new_y)
  836. {
  837. gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
  838. }
  839. void scrollback(struct vc_data *vc, int lines)
  840. {
  841. if (!lines)
  842. lines = vc->vc_rows / 2;
  843. scrolldelta(-lines);
  844. }
  845. void scrollfront(struct vc_data *vc, int lines)
  846. {
  847. if (!lines)
  848. lines = vc->vc_rows / 2;
  849. scrolldelta(lines);
  850. }
  851. static void lf(struct vc_data *vc)
  852. {
  853. /* don't scroll if above bottom of scrolling region, or
  854. * if below scrolling region
  855. */
  856. if (vc->vc_y + 1 == vc->vc_bottom)
  857. scrup(vc, vc->vc_top, vc->vc_bottom, 1);
  858. else if (vc->vc_y < vc->vc_rows - 1) {
  859. vc->vc_y++;
  860. vc->vc_pos += vc->vc_size_row;
  861. }
  862. vc->vc_need_wrap = 0;
  863. }
  864. static void ri(struct vc_data *vc)
  865. {
  866. /* don't scroll if below top of scrolling region, or
  867. * if above scrolling region
  868. */
  869. if (vc->vc_y == vc->vc_top)
  870. scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
  871. else if (vc->vc_y > 0) {
  872. vc->vc_y--;
  873. vc->vc_pos -= vc->vc_size_row;
  874. }
  875. vc->vc_need_wrap = 0;
  876. }
  877. static inline void cr(struct vc_data *vc)
  878. {
  879. vc->vc_pos -= vc->vc_x << 1;
  880. vc->vc_need_wrap = vc->vc_x = 0;
  881. }
  882. static inline void bs(struct vc_data *vc)
  883. {
  884. if (vc->vc_x) {
  885. vc->vc_pos -= 2;
  886. vc->vc_x--;
  887. vc->vc_need_wrap = 0;
  888. }
  889. }
  890. static inline void del(struct vc_data *vc)
  891. {
  892. /* ignored */
  893. }
  894. static void csi_J(struct vc_data *vc, int vpar)
  895. {
  896. unsigned int count;
  897. unsigned short * start;
  898. switch (vpar) {
  899. case 0: /* erase from cursor to end of display */
  900. count = (vc->vc_scr_end - vc->vc_pos) >> 1;
  901. start = (unsigned short *)vc->vc_pos;
  902. if (DO_UPDATE(vc)) {
  903. /* do in two stages */
  904. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
  905. vc->vc_cols - vc->vc_x);
  906. vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
  907. vc->vc_rows - vc->vc_y - 1,
  908. vc->vc_cols);
  909. }
  910. break;
  911. case 1: /* erase from start to cursor */
  912. count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
  913. start = (unsigned short *)vc->vc_origin;
  914. if (DO_UPDATE(vc)) {
  915. /* do in two stages */
  916. vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
  917. vc->vc_cols);
  918. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  919. vc->vc_x + 1);
  920. }
  921. break;
  922. case 2: /* erase whole display */
  923. count = vc->vc_cols * vc->vc_rows;
  924. start = (unsigned short *)vc->vc_origin;
  925. if (DO_UPDATE(vc))
  926. vc->vc_sw->con_clear(vc, 0, 0,
  927. vc->vc_rows,
  928. vc->vc_cols);
  929. break;
  930. default:
  931. return;
  932. }
  933. scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
  934. vc->vc_need_wrap = 0;
  935. }
  936. static void csi_K(struct vc_data *vc, int vpar)
  937. {
  938. unsigned int count;
  939. unsigned short * start;
  940. switch (vpar) {
  941. case 0: /* erase from cursor to end of line */
  942. count = vc->vc_cols - vc->vc_x;
  943. start = (unsigned short *)vc->vc_pos;
  944. if (DO_UPDATE(vc))
  945. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
  946. vc->vc_cols - vc->vc_x);
  947. break;
  948. case 1: /* erase from start of line to cursor */
  949. start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
  950. count = vc->vc_x + 1;
  951. if (DO_UPDATE(vc))
  952. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  953. vc->vc_x + 1);
  954. break;
  955. case 2: /* erase whole line */
  956. start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
  957. count = vc->vc_cols;
  958. if (DO_UPDATE(vc))
  959. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  960. vc->vc_cols);
  961. break;
  962. default:
  963. return;
  964. }
  965. scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
  966. vc->vc_need_wrap = 0;
  967. }
  968. static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
  969. { /* not vt100? */
  970. int count;
  971. if (!vpar)
  972. vpar++;
  973. count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
  974. scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
  975. if (DO_UPDATE(vc))
  976. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
  977. vc->vc_need_wrap = 0;
  978. }
  979. static void default_attr(struct vc_data *vc)
  980. {
  981. vc->vc_intensity = 1;
  982. vc->vc_underline = 0;
  983. vc->vc_reverse = 0;
  984. vc->vc_blink = 0;
  985. vc->vc_color = vc->vc_def_color;
  986. }
  987. /* console_sem is held */
  988. static void csi_m(struct vc_data *vc)
  989. {
  990. int i;
  991. for (i = 0; i <= vc->vc_npar; i++)
  992. switch (vc->vc_par[i]) {
  993. case 0: /* all attributes off */
  994. default_attr(vc);
  995. break;
  996. case 1:
  997. vc->vc_intensity = 2;
  998. break;
  999. case 2:
  1000. vc->vc_intensity = 0;
  1001. break;
  1002. case 4:
  1003. vc->vc_underline = 1;
  1004. break;
  1005. case 5:
  1006. vc->vc_blink = 1;
  1007. break;
  1008. case 7:
  1009. vc->vc_reverse = 1;
  1010. break;
  1011. case 10: /* ANSI X3.64-1979 (SCO-ish?)
  1012. * Select primary font, don't display
  1013. * control chars if defined, don't set
  1014. * bit 8 on output.
  1015. */
  1016. vc->vc_translate = set_translate(vc->vc_charset == 0
  1017. ? vc->vc_G0_charset
  1018. : vc->vc_G1_charset, vc);
  1019. vc->vc_disp_ctrl = 0;
  1020. vc->vc_toggle_meta = 0;
  1021. break;
  1022. case 11: /* ANSI X3.64-1979 (SCO-ish?)
  1023. * Select first alternate font, lets
  1024. * chars < 32 be displayed as ROM chars.
  1025. */
  1026. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1027. vc->vc_disp_ctrl = 1;
  1028. vc->vc_toggle_meta = 0;
  1029. break;
  1030. case 12: /* ANSI X3.64-1979 (SCO-ish?)
  1031. * Select second alternate font, toggle
  1032. * high bit before displaying as ROM char.
  1033. */
  1034. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1035. vc->vc_disp_ctrl = 1;
  1036. vc->vc_toggle_meta = 1;
  1037. break;
  1038. case 21:
  1039. case 22:
  1040. vc->vc_intensity = 1;
  1041. break;
  1042. case 24:
  1043. vc->vc_underline = 0;
  1044. break;
  1045. case 25:
  1046. vc->vc_blink = 0;
  1047. break;
  1048. case 27:
  1049. vc->vc_reverse = 0;
  1050. break;
  1051. case 38: /* ANSI X3.64-1979 (SCO-ish?)
  1052. * Enables underscore, white foreground
  1053. * with white underscore (Linux - use
  1054. * default foreground).
  1055. */
  1056. vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
  1057. vc->vc_underline = 1;
  1058. break;
  1059. case 39: /* ANSI X3.64-1979 (SCO-ish?)
  1060. * Disable underline option.
  1061. * Reset colour to default? It did this
  1062. * before...
  1063. */
  1064. vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
  1065. vc->vc_underline = 0;
  1066. break;
  1067. case 49:
  1068. vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
  1069. break;
  1070. default:
  1071. if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
  1072. vc->vc_color = color_table[vc->vc_par[i] - 30]
  1073. | (vc->vc_color & 0xf0);
  1074. else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
  1075. vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
  1076. | (vc->vc_color & 0x0f);
  1077. break;
  1078. }
  1079. update_attr(vc);
  1080. }
  1081. static void respond_string(const char *p, struct tty_struct *tty)
  1082. {
  1083. while (*p) {
  1084. tty_insert_flip_char(tty, *p, 0);
  1085. p++;
  1086. }
  1087. con_schedule_flip(tty);
  1088. }
  1089. static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
  1090. {
  1091. char buf[40];
  1092. sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
  1093. respond_string(buf, tty);
  1094. }
  1095. static inline void status_report(struct tty_struct *tty)
  1096. {
  1097. respond_string("\033[0n", tty); /* Terminal ok */
  1098. }
  1099. static inline void respond_ID(struct tty_struct * tty)
  1100. {
  1101. respond_string(VT102ID, tty);
  1102. }
  1103. void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
  1104. {
  1105. char buf[8];
  1106. sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
  1107. (char)('!' + mry));
  1108. respond_string(buf, tty);
  1109. }
  1110. /* invoked via ioctl(TIOCLINUX) and through set_selection */
  1111. int mouse_reporting(void)
  1112. {
  1113. return vc_cons[fg_console].d->vc_report_mouse;
  1114. }
  1115. /* console_sem is held */
  1116. static void set_mode(struct vc_data *vc, int on_off)
  1117. {
  1118. int i;
  1119. for (i = 0; i <= vc->vc_npar; i++)
  1120. if (vc->vc_ques) {
  1121. switch(vc->vc_par[i]) { /* DEC private modes set/reset */
  1122. case 1: /* Cursor keys send ^[Ox/^[[x */
  1123. if (on_off)
  1124. set_kbd(vc, decckm);
  1125. else
  1126. clr_kbd(vc, decckm);
  1127. break;
  1128. case 3: /* 80/132 mode switch unimplemented */
  1129. vc->vc_deccolm = on_off;
  1130. #if 0
  1131. vc_resize(deccolm ? 132 : 80, vc->vc_rows);
  1132. /* this alone does not suffice; some user mode
  1133. utility has to change the hardware regs */
  1134. #endif
  1135. break;
  1136. case 5: /* Inverted screen on/off */
  1137. if (vc->vc_decscnm != on_off) {
  1138. vc->vc_decscnm = on_off;
  1139. invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
  1140. update_attr(vc);
  1141. }
  1142. break;
  1143. case 6: /* Origin relative/absolute */
  1144. vc->vc_decom = on_off;
  1145. gotoxay(vc, 0, 0);
  1146. break;
  1147. case 7: /* Autowrap on/off */
  1148. vc->vc_decawm = on_off;
  1149. break;
  1150. case 8: /* Autorepeat on/off */
  1151. if (on_off)
  1152. set_kbd(vc, decarm);
  1153. else
  1154. clr_kbd(vc, decarm);
  1155. break;
  1156. case 9:
  1157. vc->vc_report_mouse = on_off ? 1 : 0;
  1158. break;
  1159. case 25: /* Cursor on/off */
  1160. vc->vc_deccm = on_off;
  1161. break;
  1162. case 1000:
  1163. vc->vc_report_mouse = on_off ? 2 : 0;
  1164. break;
  1165. }
  1166. } else {
  1167. switch(vc->vc_par[i]) { /* ANSI modes set/reset */
  1168. case 3: /* Monitor (display ctrls) */
  1169. vc->vc_disp_ctrl = on_off;
  1170. break;
  1171. case 4: /* Insert Mode on/off */
  1172. vc->vc_decim = on_off;
  1173. break;
  1174. case 20: /* Lf, Enter == CrLf/Lf */
  1175. if (on_off)
  1176. set_kbd(vc, lnm);
  1177. else
  1178. clr_kbd(vc, lnm);
  1179. break;
  1180. }
  1181. }
  1182. }
  1183. /* console_sem is held */
  1184. static void setterm_command(struct vc_data *vc)
  1185. {
  1186. switch(vc->vc_par[0]) {
  1187. case 1: /* set color for underline mode */
  1188. if (vc->vc_can_do_color &&
  1189. vc->vc_par[1] < 16) {
  1190. vc->vc_ulcolor = color_table[vc->vc_par[1]];
  1191. if (vc->vc_underline)
  1192. update_attr(vc);
  1193. }
  1194. break;
  1195. case 2: /* set color for half intensity mode */
  1196. if (vc->vc_can_do_color &&
  1197. vc->vc_par[1] < 16) {
  1198. vc->vc_halfcolor = color_table[vc->vc_par[1]];
  1199. if (vc->vc_intensity == 0)
  1200. update_attr(vc);
  1201. }
  1202. break;
  1203. case 8: /* store colors as defaults */
  1204. vc->vc_def_color = vc->vc_attr;
  1205. if (vc->vc_hi_font_mask == 0x100)
  1206. vc->vc_def_color >>= 1;
  1207. default_attr(vc);
  1208. update_attr(vc);
  1209. break;
  1210. case 9: /* set blanking interval */
  1211. blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
  1212. poke_blanked_console();
  1213. break;
  1214. case 10: /* set bell frequency in Hz */
  1215. if (vc->vc_npar >= 1)
  1216. vc->vc_bell_pitch = vc->vc_par[1];
  1217. else
  1218. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1219. break;
  1220. case 11: /* set bell duration in msec */
  1221. if (vc->vc_npar >= 1)
  1222. vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
  1223. vc->vc_par[1] * HZ / 1000 : 0;
  1224. else
  1225. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1226. break;
  1227. case 12: /* bring specified console to the front */
  1228. if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
  1229. set_console(vc->vc_par[1] - 1);
  1230. break;
  1231. case 13: /* unblank the screen */
  1232. poke_blanked_console();
  1233. break;
  1234. case 14: /* set vesa powerdown interval */
  1235. vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
  1236. break;
  1237. case 15: /* activate the previous console */
  1238. set_console(last_console);
  1239. break;
  1240. }
  1241. }
  1242. /* console_sem is held */
  1243. static void csi_at(struct vc_data *vc, unsigned int nr)
  1244. {
  1245. if (nr > vc->vc_cols - vc->vc_x)
  1246. nr = vc->vc_cols - vc->vc_x;
  1247. else if (!nr)
  1248. nr = 1;
  1249. insert_char(vc, nr);
  1250. }
  1251. /* console_sem is held */
  1252. static void csi_L(struct vc_data *vc, unsigned int nr)
  1253. {
  1254. if (nr > vc->vc_rows - vc->vc_y)
  1255. nr = vc->vc_rows - vc->vc_y;
  1256. else if (!nr)
  1257. nr = 1;
  1258. scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
  1259. vc->vc_need_wrap = 0;
  1260. }
  1261. /* console_sem is held */
  1262. static void csi_P(struct vc_data *vc, unsigned int nr)
  1263. {
  1264. if (nr > vc->vc_cols - vc->vc_x)
  1265. nr = vc->vc_cols - vc->vc_x;
  1266. else if (!nr)
  1267. nr = 1;
  1268. delete_char(vc, nr);
  1269. }
  1270. /* console_sem is held */
  1271. static void csi_M(struct vc_data *vc, unsigned int nr)
  1272. {
  1273. if (nr > vc->vc_rows - vc->vc_y)
  1274. nr = vc->vc_rows - vc->vc_y;
  1275. else if (!nr)
  1276. nr=1;
  1277. scrup(vc, vc->vc_y, vc->vc_bottom, nr);
  1278. vc->vc_need_wrap = 0;
  1279. }
  1280. /* console_sem is held (except via vc_init->reset_terminal */
  1281. static void save_cur(struct vc_data *vc)
  1282. {
  1283. vc->vc_saved_x = vc->vc_x;
  1284. vc->vc_saved_y = vc->vc_y;
  1285. vc->vc_s_intensity = vc->vc_intensity;
  1286. vc->vc_s_underline = vc->vc_underline;
  1287. vc->vc_s_blink = vc->vc_blink;
  1288. vc->vc_s_reverse = vc->vc_reverse;
  1289. vc->vc_s_charset = vc->vc_charset;
  1290. vc->vc_s_color = vc->vc_color;
  1291. vc->vc_saved_G0 = vc->vc_G0_charset;
  1292. vc->vc_saved_G1 = vc->vc_G1_charset;
  1293. }
  1294. /* console_sem is held */
  1295. static void restore_cur(struct vc_data *vc)
  1296. {
  1297. gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
  1298. vc->vc_intensity = vc->vc_s_intensity;
  1299. vc->vc_underline = vc->vc_s_underline;
  1300. vc->vc_blink = vc->vc_s_blink;
  1301. vc->vc_reverse = vc->vc_s_reverse;
  1302. vc->vc_charset = vc->vc_s_charset;
  1303. vc->vc_color = vc->vc_s_color;
  1304. vc->vc_G0_charset = vc->vc_saved_G0;
  1305. vc->vc_G1_charset = vc->vc_saved_G1;
  1306. vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
  1307. update_attr(vc);
  1308. vc->vc_need_wrap = 0;
  1309. }
  1310. enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
  1311. EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
  1312. ESpalette };
  1313. /* console_sem is held (except via vc_init()) */
  1314. static void reset_terminal(struct vc_data *vc, int do_clear)
  1315. {
  1316. vc->vc_top = 0;
  1317. vc->vc_bottom = vc->vc_rows;
  1318. vc->vc_state = ESnormal;
  1319. vc->vc_ques = 0;
  1320. vc->vc_translate = set_translate(LAT1_MAP, vc);
  1321. vc->vc_G0_charset = LAT1_MAP;
  1322. vc->vc_G1_charset = GRAF_MAP;
  1323. vc->vc_charset = 0;
  1324. vc->vc_need_wrap = 0;
  1325. vc->vc_report_mouse = 0;
  1326. vc->vc_utf = 0;
  1327. vc->vc_utf_count = 0;
  1328. vc->vc_disp_ctrl = 0;
  1329. vc->vc_toggle_meta = 0;
  1330. vc->vc_decscnm = 0;
  1331. vc->vc_decom = 0;
  1332. vc->vc_decawm = 1;
  1333. vc->vc_deccm = 1;
  1334. vc->vc_decim = 0;
  1335. set_kbd(vc, decarm);
  1336. clr_kbd(vc, decckm);
  1337. clr_kbd(vc, kbdapplic);
  1338. clr_kbd(vc, lnm);
  1339. kbd_table[vc->vc_num].lockstate = 0;
  1340. kbd_table[vc->vc_num].slockstate = 0;
  1341. kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
  1342. kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
  1343. /* do not do set_leds here because this causes an endless tasklet loop
  1344. when the keyboard hasn't been initialized yet */
  1345. vc->vc_cursor_type = CUR_DEFAULT;
  1346. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1347. default_attr(vc);
  1348. update_attr(vc);
  1349. vc->vc_tab_stop[0] = 0x01010100;
  1350. vc->vc_tab_stop[1] =
  1351. vc->vc_tab_stop[2] =
  1352. vc->vc_tab_stop[3] =
  1353. vc->vc_tab_stop[4] = 0x01010101;
  1354. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1355. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1356. gotoxy(vc, 0, 0);
  1357. save_cur(vc);
  1358. if (do_clear)
  1359. csi_J(vc, 2);
  1360. }
  1361. /* console_sem is held */
  1362. static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
  1363. {
  1364. /*
  1365. * Control characters can be used in the _middle_
  1366. * of an escape sequence.
  1367. */
  1368. switch (c) {
  1369. case 0:
  1370. return;
  1371. case 7:
  1372. if (vc->vc_bell_duration)
  1373. kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
  1374. return;
  1375. case 8:
  1376. bs(vc);
  1377. return;
  1378. case 9:
  1379. vc->vc_pos -= (vc->vc_x << 1);
  1380. while (vc->vc_x < vc->vc_cols - 1) {
  1381. vc->vc_x++;
  1382. if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
  1383. break;
  1384. }
  1385. vc->vc_pos += (vc->vc_x << 1);
  1386. return;
  1387. case 10: case 11: case 12:
  1388. lf(vc);
  1389. if (!is_kbd(vc, lnm))
  1390. return;
  1391. case 13:
  1392. cr(vc);
  1393. return;
  1394. case 14:
  1395. vc->vc_charset = 1;
  1396. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1397. vc->vc_disp_ctrl = 1;
  1398. return;
  1399. case 15:
  1400. vc->vc_charset = 0;
  1401. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1402. vc->vc_disp_ctrl = 0;
  1403. return;
  1404. case 24: case 26:
  1405. vc->vc_state = ESnormal;
  1406. return;
  1407. case 27:
  1408. vc->vc_state = ESesc;
  1409. return;
  1410. case 127:
  1411. del(vc);
  1412. return;
  1413. case 128+27:
  1414. vc->vc_state = ESsquare;
  1415. return;
  1416. }
  1417. switch(vc->vc_state) {
  1418. case ESesc:
  1419. vc->vc_state = ESnormal;
  1420. switch (c) {
  1421. case '[':
  1422. vc->vc_state = ESsquare;
  1423. return;
  1424. case ']':
  1425. vc->vc_state = ESnonstd;
  1426. return;
  1427. case '%':
  1428. vc->vc_state = ESpercent;
  1429. return;
  1430. case 'E':
  1431. cr(vc);
  1432. lf(vc);
  1433. return;
  1434. case 'M':
  1435. ri(vc);
  1436. return;
  1437. case 'D':
  1438. lf(vc);
  1439. return;
  1440. case 'H':
  1441. vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
  1442. return;
  1443. case 'Z':
  1444. respond_ID(tty);
  1445. return;
  1446. case '7':
  1447. save_cur(vc);
  1448. return;
  1449. case '8':
  1450. restore_cur(vc);
  1451. return;
  1452. case '(':
  1453. vc->vc_state = ESsetG0;
  1454. return;
  1455. case ')':
  1456. vc->vc_state = ESsetG1;
  1457. return;
  1458. case '#':
  1459. vc->vc_state = EShash;
  1460. return;
  1461. case 'c':
  1462. reset_terminal(vc, 1);
  1463. return;
  1464. case '>': /* Numeric keypad */
  1465. clr_kbd(vc, kbdapplic);
  1466. return;
  1467. case '=': /* Appl. keypad */
  1468. set_kbd(vc, kbdapplic);
  1469. return;
  1470. }
  1471. return;
  1472. case ESnonstd:
  1473. if (c=='P') { /* palette escape sequence */
  1474. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1475. vc->vc_par[vc->vc_npar] = 0;
  1476. vc->vc_npar = 0;
  1477. vc->vc_state = ESpalette;
  1478. return;
  1479. } else if (c=='R') { /* reset palette */
  1480. reset_palette(vc);
  1481. vc->vc_state = ESnormal;
  1482. } else
  1483. vc->vc_state = ESnormal;
  1484. return;
  1485. case ESpalette:
  1486. if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
  1487. vc->vc_par[vc->vc_npar++] = (c > '9' ? (c & 0xDF) - 'A' + 10 : c - '0');
  1488. if (vc->vc_npar == 7) {
  1489. int i = vc->vc_par[0] * 3, j = 1;
  1490. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1491. vc->vc_palette[i++] += vc->vc_par[j++];
  1492. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1493. vc->vc_palette[i++] += vc->vc_par[j++];
  1494. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1495. vc->vc_palette[i] += vc->vc_par[j];
  1496. set_palette(vc);
  1497. vc->vc_state = ESnormal;
  1498. }
  1499. } else
  1500. vc->vc_state = ESnormal;
  1501. return;
  1502. case ESsquare:
  1503. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1504. vc->vc_par[vc->vc_npar] = 0;
  1505. vc->vc_npar = 0;
  1506. vc->vc_state = ESgetpars;
  1507. if (c == '[') { /* Function key */
  1508. vc->vc_state=ESfunckey;
  1509. return;
  1510. }
  1511. vc->vc_ques = (c == '?');
  1512. if (vc->vc_ques)
  1513. return;
  1514. case ESgetpars:
  1515. if (c == ';' && vc->vc_npar < NPAR - 1) {
  1516. vc->vc_npar++;
  1517. return;
  1518. } else if (c>='0' && c<='9') {
  1519. vc->vc_par[vc->vc_npar] *= 10;
  1520. vc->vc_par[vc->vc_npar] += c - '0';
  1521. return;
  1522. } else
  1523. vc->vc_state = ESgotpars;
  1524. case ESgotpars:
  1525. vc->vc_state = ESnormal;
  1526. switch(c) {
  1527. case 'h':
  1528. set_mode(vc, 1);
  1529. return;
  1530. case 'l':
  1531. set_mode(vc, 0);
  1532. return;
  1533. case 'c':
  1534. if (vc->vc_ques) {
  1535. if (vc->vc_par[0])
  1536. vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
  1537. else
  1538. vc->vc_cursor_type = CUR_DEFAULT;
  1539. return;
  1540. }
  1541. break;
  1542. case 'm':
  1543. if (vc->vc_ques) {
  1544. clear_selection();
  1545. if (vc->vc_par[0])
  1546. vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
  1547. else
  1548. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1549. return;
  1550. }
  1551. break;
  1552. case 'n':
  1553. if (!vc->vc_ques) {
  1554. if (vc->vc_par[0] == 5)
  1555. status_report(tty);
  1556. else if (vc->vc_par[0] == 6)
  1557. cursor_report(vc, tty);
  1558. }
  1559. return;
  1560. }
  1561. if (vc->vc_ques) {
  1562. vc->vc_ques = 0;
  1563. return;
  1564. }
  1565. switch(c) {
  1566. case 'G': case '`':
  1567. if (vc->vc_par[0])
  1568. vc->vc_par[0]--;
  1569. gotoxy(vc, vc->vc_par[0], vc->vc_y);
  1570. return;
  1571. case 'A':
  1572. if (!vc->vc_par[0])
  1573. vc->vc_par[0]++;
  1574. gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
  1575. return;
  1576. case 'B': case 'e':
  1577. if (!vc->vc_par[0])
  1578. vc->vc_par[0]++;
  1579. gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
  1580. return;
  1581. case 'C': case 'a':
  1582. if (!vc->vc_par[0])
  1583. vc->vc_par[0]++;
  1584. gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
  1585. return;
  1586. case 'D':
  1587. if (!vc->vc_par[0])
  1588. vc->vc_par[0]++;
  1589. gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
  1590. return;
  1591. case 'E':
  1592. if (!vc->vc_par[0])
  1593. vc->vc_par[0]++;
  1594. gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
  1595. return;
  1596. case 'F':
  1597. if (!vc->vc_par[0])
  1598. vc->vc_par[0]++;
  1599. gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
  1600. return;
  1601. case 'd':
  1602. if (vc->vc_par[0])
  1603. vc->vc_par[0]--;
  1604. gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
  1605. return;
  1606. case 'H': case 'f':
  1607. if (vc->vc_par[0])
  1608. vc->vc_par[0]--;
  1609. if (vc->vc_par[1])
  1610. vc->vc_par[1]--;
  1611. gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
  1612. return;
  1613. case 'J':
  1614. csi_J(vc, vc->vc_par[0]);
  1615. return;
  1616. case 'K':
  1617. csi_K(vc, vc->vc_par[0]);
  1618. return;
  1619. case 'L':
  1620. csi_L(vc, vc->vc_par[0]);
  1621. return;
  1622. case 'M':
  1623. csi_M(vc, vc->vc_par[0]);
  1624. return;
  1625. case 'P':
  1626. csi_P(vc, vc->vc_par[0]);
  1627. return;
  1628. case 'c':
  1629. if (!vc->vc_par[0])
  1630. respond_ID(tty);
  1631. return;
  1632. case 'g':
  1633. if (!vc->vc_par[0])
  1634. vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
  1635. else if (vc->vc_par[0] == 3) {
  1636. vc->vc_tab_stop[0] =
  1637. vc->vc_tab_stop[1] =
  1638. vc->vc_tab_stop[2] =
  1639. vc->vc_tab_stop[3] =
  1640. vc->vc_tab_stop[4] = 0;
  1641. }
  1642. return;
  1643. case 'm':
  1644. csi_m(vc);
  1645. return;
  1646. case 'q': /* DECLL - but only 3 leds */
  1647. /* map 0,1,2,3 to 0,1,2,4 */
  1648. if (vc->vc_par[0] < 4)
  1649. setledstate(kbd_table + vc->vc_num,
  1650. (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
  1651. return;
  1652. case 'r':
  1653. if (!vc->vc_par[0])
  1654. vc->vc_par[0]++;
  1655. if (!vc->vc_par[1])
  1656. vc->vc_par[1] = vc->vc_rows;
  1657. /* Minimum allowed region is 2 lines */
  1658. if (vc->vc_par[0] < vc->vc_par[1] &&
  1659. vc->vc_par[1] <= vc->vc_rows) {
  1660. vc->vc_top = vc->vc_par[0] - 1;
  1661. vc->vc_bottom = vc->vc_par[1];
  1662. gotoxay(vc, 0, 0);
  1663. }
  1664. return;
  1665. case 's':
  1666. save_cur(vc);
  1667. return;
  1668. case 'u':
  1669. restore_cur(vc);
  1670. return;
  1671. case 'X':
  1672. csi_X(vc, vc->vc_par[0]);
  1673. return;
  1674. case '@':
  1675. csi_at(vc, vc->vc_par[0]);
  1676. return;
  1677. case ']': /* setterm functions */
  1678. setterm_command(vc);
  1679. return;
  1680. }
  1681. return;
  1682. case ESpercent:
  1683. vc->vc_state = ESnormal;
  1684. switch (c) {
  1685. case '@': /* defined in ISO 2022 */
  1686. vc->vc_utf = 0;
  1687. return;
  1688. case 'G': /* prelim official escape code */
  1689. case '8': /* retained for compatibility */
  1690. vc->vc_utf = 1;
  1691. return;
  1692. }
  1693. return;
  1694. case ESfunckey:
  1695. vc->vc_state = ESnormal;
  1696. return;
  1697. case EShash:
  1698. vc->vc_state = ESnormal;
  1699. if (c == '8') {
  1700. /* DEC screen alignment test. kludge :-) */
  1701. vc->vc_video_erase_char =
  1702. (vc->vc_video_erase_char & 0xff00) | 'E';
  1703. csi_J(vc, 2);
  1704. vc->vc_video_erase_char =
  1705. (vc->vc_video_erase_char & 0xff00) | ' ';
  1706. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  1707. }
  1708. return;
  1709. case ESsetG0:
  1710. if (c == '0')
  1711. vc->vc_G0_charset = GRAF_MAP;
  1712. else if (c == 'B')
  1713. vc->vc_G0_charset = LAT1_MAP;
  1714. else if (c == 'U')
  1715. vc->vc_G0_charset = IBMPC_MAP;
  1716. else if (c == 'K')
  1717. vc->vc_G0_charset = USER_MAP;
  1718. if (vc->vc_charset == 0)
  1719. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1720. vc->vc_state = ESnormal;
  1721. return;
  1722. case ESsetG1:
  1723. if (c == '0')
  1724. vc->vc_G1_charset = GRAF_MAP;
  1725. else if (c == 'B')
  1726. vc->vc_G1_charset = LAT1_MAP;
  1727. else if (c == 'U')
  1728. vc->vc_G1_charset = IBMPC_MAP;
  1729. else if (c == 'K')
  1730. vc->vc_G1_charset = USER_MAP;
  1731. if (vc->vc_charset == 1)
  1732. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1733. vc->vc_state = ESnormal;
  1734. return;
  1735. default:
  1736. vc->vc_state = ESnormal;
  1737. }
  1738. }
  1739. /* This is a temporary buffer used to prepare a tty console write
  1740. * so that we can easily avoid touching user space while holding the
  1741. * console spinlock. It is allocated in con_init and is shared by
  1742. * this code and the vc_screen read/write tty calls.
  1743. *
  1744. * We have to allocate this statically in the kernel data section
  1745. * since console_init (and thus con_init) are called before any
  1746. * kernel memory allocation is available.
  1747. */
  1748. char con_buf[CON_BUF_SIZE];
  1749. DECLARE_MUTEX(con_buf_sem);
  1750. /* acquires console_sem */
  1751. static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1752. {
  1753. #ifdef VT_BUF_VRAM_ONLY
  1754. #define FLUSH do { } while(0);
  1755. #else
  1756. #define FLUSH if (draw_x >= 0) { \
  1757. vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
  1758. draw_x = -1; \
  1759. }
  1760. #endif
  1761. int c, tc, ok, n = 0, draw_x = -1;
  1762. unsigned int currcons;
  1763. unsigned long draw_from = 0, draw_to = 0;
  1764. struct vc_data *vc;
  1765. u16 himask, charmask;
  1766. const unsigned char *orig_buf = NULL;
  1767. int orig_count;
  1768. if (in_interrupt())
  1769. return count;
  1770. might_sleep();
  1771. acquire_console_sem();
  1772. vc = tty->driver_data;
  1773. if (vc == NULL) {
  1774. printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
  1775. release_console_sem();
  1776. return 0;
  1777. }
  1778. currcons = vc->vc_num;
  1779. if (!vc_cons_allocated(currcons)) {
  1780. /* could this happen? */
  1781. static int error = 0;
  1782. if (!error) {
  1783. error = 1;
  1784. printk("con_write: tty %d not allocated\n", currcons+1);
  1785. }
  1786. release_console_sem();
  1787. return 0;
  1788. }
  1789. release_console_sem();
  1790. orig_buf = buf;
  1791. orig_count = count;
  1792. /* At this point 'buf' is guaranteed to be a kernel buffer
  1793. * and therefore no access to userspace (and therefore sleeping)
  1794. * will be needed. The con_buf_sem serializes all tty based
  1795. * console rendering and vcs write/read operations. We hold
  1796. * the console spinlock during the entire write.
  1797. */
  1798. acquire_console_sem();
  1799. vc = tty->driver_data;
  1800. if (vc == NULL) {
  1801. printk(KERN_ERR "vt: argh, driver_data _became_ NULL !\n");
  1802. release_console_sem();
  1803. goto out;
  1804. }
  1805. himask = vc->vc_hi_font_mask;
  1806. charmask = himask ? 0x1ff : 0xff;
  1807. /* undraw cursor first */
  1808. if (IS_FG(vc))
  1809. hide_cursor(vc);
  1810. while (!tty->stopped && count) {
  1811. int orig = *buf;
  1812. c = orig;
  1813. buf++;
  1814. n++;
  1815. count--;
  1816. /* Do no translation at all in control states */
  1817. if (vc->vc_state != ESnormal) {
  1818. tc = c;
  1819. } else if (vc->vc_utf) {
  1820. /* Combine UTF-8 into Unicode */
  1821. /* Incomplete characters silently ignored */
  1822. if(c > 0x7f) {
  1823. if (vc->vc_utf_count > 0 && (c & 0xc0) == 0x80) {
  1824. vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
  1825. vc->vc_utf_count--;
  1826. if (vc->vc_utf_count == 0)
  1827. tc = c = vc->vc_utf_char;
  1828. else continue;
  1829. } else {
  1830. if ((c & 0xe0) == 0xc0) {
  1831. vc->vc_utf_count = 1;
  1832. vc->vc_utf_char = (c & 0x1f);
  1833. } else if ((c & 0xf0) == 0xe0) {
  1834. vc->vc_utf_count = 2;
  1835. vc->vc_utf_char = (c & 0x0f);
  1836. } else if ((c & 0xf8) == 0xf0) {
  1837. vc->vc_utf_count = 3;
  1838. vc->vc_utf_char = (c & 0x07);
  1839. } else if ((c & 0xfc) == 0xf8) {
  1840. vc->vc_utf_count = 4;
  1841. vc->vc_utf_char = (c & 0x03);
  1842. } else if ((c & 0xfe) == 0xfc) {
  1843. vc->vc_utf_count = 5;
  1844. vc->vc_utf_char = (c & 0x01);
  1845. } else
  1846. vc->vc_utf_count = 0;
  1847. continue;
  1848. }
  1849. } else {
  1850. tc = c;
  1851. vc->vc_utf_count = 0;
  1852. }
  1853. } else { /* no utf */
  1854. tc = vc->vc_translate[vc->vc_toggle_meta ? (c | 0x80) : c];
  1855. }
  1856. /* If the original code was a control character we
  1857. * only allow a glyph to be displayed if the code is
  1858. * not normally used (such as for cursor movement) or
  1859. * if the disp_ctrl mode has been explicitly enabled.
  1860. * Certain characters (as given by the CTRL_ALWAYS
  1861. * bitmap) are always displayed as control characters,
  1862. * as the console would be pretty useless without
  1863. * them; to display an arbitrary font position use the
  1864. * direct-to-font zone in UTF-8 mode.
  1865. */
  1866. ok = tc && (c >= 32 ||
  1867. (!vc->vc_utf && !(((vc->vc_disp_ctrl ? CTRL_ALWAYS
  1868. : CTRL_ACTION) >> c) & 1)))
  1869. && (c != 127 || vc->vc_disp_ctrl)
  1870. && (c != 128+27);
  1871. if (vc->vc_state == ESnormal && ok) {
  1872. /* Now try to find out how to display it */
  1873. tc = conv_uni_to_pc(vc, tc);
  1874. if ( tc == -4 ) {
  1875. /* If we got -4 (not found) then see if we have
  1876. defined a replacement character (U+FFFD) */
  1877. tc = conv_uni_to_pc(vc, 0xfffd);
  1878. /* One reason for the -4 can be that we just
  1879. did a clear_unimap();
  1880. try at least to show something. */
  1881. if (tc == -4)
  1882. tc = c;
  1883. } else if ( tc == -3 ) {
  1884. /* Bad hash table -- hope for the best */
  1885. tc = c;
  1886. }
  1887. if (tc & ~charmask)
  1888. continue; /* Conversion failed */
  1889. if (vc->vc_need_wrap || vc->vc_decim)
  1890. FLUSH
  1891. if (vc->vc_need_wrap) {
  1892. cr(vc);
  1893. lf(vc);
  1894. }
  1895. if (vc->vc_decim)
  1896. insert_char(vc, 1);
  1897. scr_writew(himask ?
  1898. ((vc->vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
  1899. (vc->vc_attr << 8) + tc,
  1900. (u16 *) vc->vc_pos);
  1901. if (DO_UPDATE(vc) && draw_x < 0) {
  1902. draw_x = vc->vc_x;
  1903. draw_from = vc->vc_pos;
  1904. }
  1905. if (vc->vc_x == vc->vc_cols - 1) {
  1906. vc->vc_need_wrap = vc->vc_decawm;
  1907. draw_to = vc->vc_pos + 2;
  1908. } else {
  1909. vc->vc_x++;
  1910. draw_to = (vc->vc_pos += 2);
  1911. }
  1912. continue;
  1913. }
  1914. FLUSH
  1915. do_con_trol(tty, vc, orig);
  1916. }
  1917. FLUSH
  1918. console_conditional_schedule();
  1919. release_console_sem();
  1920. out:
  1921. return n;
  1922. #undef FLUSH
  1923. }
  1924. /*
  1925. * This is the console switching callback.
  1926. *
  1927. * Doing console switching in a process context allows
  1928. * us to do the switches asynchronously (needed when we want
  1929. * to switch due to a keyboard interrupt). Synchronization
  1930. * with other console code and prevention of re-entrancy is
  1931. * ensured with console_sem.
  1932. */
  1933. static void console_callback(void *ignored)
  1934. {
  1935. acquire_console_sem();
  1936. if (want_console >= 0) {
  1937. if (want_console != fg_console &&
  1938. vc_cons_allocated(want_console)) {
  1939. hide_cursor(vc_cons[fg_console].d);
  1940. change_console(vc_cons[want_console].d);
  1941. /* we only changed when the console had already
  1942. been allocated - a new console is not created
  1943. in an interrupt routine */
  1944. }
  1945. want_console = -1;
  1946. }
  1947. if (do_poke_blanked_console) { /* do not unblank for a LED change */
  1948. do_poke_blanked_console = 0;
  1949. poke_blanked_console();
  1950. }
  1951. if (scrollback_delta) {
  1952. struct vc_data *vc = vc_cons[fg_console].d;
  1953. clear_selection();
  1954. if (vc->vc_mode == KD_TEXT)
  1955. vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
  1956. scrollback_delta = 0;
  1957. }
  1958. if (blank_timer_expired) {
  1959. do_blank_screen(0);
  1960. blank_timer_expired = 0;
  1961. }
  1962. release_console_sem();
  1963. }
  1964. void set_console(int nr)
  1965. {
  1966. want_console = nr;
  1967. schedule_console_callback();
  1968. }
  1969. struct tty_driver *console_driver;
  1970. #ifdef CONFIG_VT_CONSOLE
  1971. /*
  1972. * Console on virtual terminal
  1973. *
  1974. * The console must be locked when we get here.
  1975. */
  1976. static void vt_console_print(struct console *co, const char *b, unsigned count)
  1977. {
  1978. struct vc_data *vc = vc_cons[fg_console].d;
  1979. unsigned char c;
  1980. static unsigned long printing;
  1981. const ushort *start;
  1982. ushort cnt = 0;
  1983. ushort myx;
  1984. /* console busy or not yet initialized */
  1985. if (!printable || test_and_set_bit(0, &printing))
  1986. return;
  1987. if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
  1988. vc = vc_cons[kmsg_redirect - 1].d;
  1989. /* read `x' only after setting currcons properly (otherwise
  1990. the `x' macro will read the x of the foreground console). */
  1991. myx = vc->vc_x;
  1992. if (!vc_cons_allocated(fg_console)) {
  1993. /* impossible */
  1994. /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
  1995. goto quit;
  1996. }
  1997. if (vc->vc_mode != KD_TEXT)
  1998. goto quit;
  1999. /* undraw cursor first */
  2000. if (IS_FG(vc))
  2001. hide_cursor(vc);
  2002. start = (ushort *)vc->vc_pos;
  2003. /* Contrived structure to try to emulate original need_wrap behaviour
  2004. * Problems caused when we have need_wrap set on '\n' character */
  2005. while (count--) {
  2006. c = *b++;
  2007. if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
  2008. if (cnt > 0) {
  2009. if (CON_IS_VISIBLE(vc))
  2010. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  2011. vc->vc_x += cnt;
  2012. if (vc->vc_need_wrap)
  2013. vc->vc_x--;
  2014. cnt = 0;
  2015. }
  2016. if (c == 8) { /* backspace */
  2017. bs(vc);
  2018. start = (ushort *)vc->vc_pos;
  2019. myx = vc->vc_x;
  2020. continue;
  2021. }
  2022. if (c != 13)
  2023. lf(vc);
  2024. cr(vc);
  2025. start = (ushort *)vc->vc_pos;
  2026. myx = vc->vc_x;
  2027. if (c == 10 || c == 13)
  2028. continue;
  2029. }
  2030. scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
  2031. cnt++;
  2032. if (myx == vc->vc_cols - 1) {
  2033. vc->vc_need_wrap = 1;
  2034. continue;
  2035. }
  2036. vc->vc_pos += 2;
  2037. myx++;
  2038. }
  2039. if (cnt > 0) {
  2040. if (CON_IS_VISIBLE(vc))
  2041. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  2042. vc->vc_x += cnt;
  2043. if (vc->vc_x == vc->vc_cols) {
  2044. vc->vc_x--;
  2045. vc->vc_need_wrap = 1;
  2046. }
  2047. }
  2048. set_cursor(vc);
  2049. quit:
  2050. clear_bit(0, &printing);
  2051. }
  2052. static struct tty_driver *vt_console_device(struct console *c, int *index)
  2053. {
  2054. *index = c->index ? c->index-1 : fg_console;
  2055. return console_driver;
  2056. }
  2057. static struct console vt_console_driver = {
  2058. .name = "tty",
  2059. .write = vt_console_print,
  2060. .device = vt_console_device,
  2061. .unblank = unblank_screen,
  2062. .flags = CON_PRINTBUFFER,
  2063. .index = -1,
  2064. };
  2065. #endif
  2066. /*
  2067. * Handling of Linux-specific VC ioctls
  2068. */
  2069. /*
  2070. * Generally a bit racy with respect to console_sem().
  2071. *
  2072. * There are some functions which don't need it.
  2073. *
  2074. * There are some functions which can sleep for arbitrary periods
  2075. * (paste_selection) but we don't need the lock there anyway.
  2076. *
  2077. * set_selection has locking, and definitely needs it
  2078. */
  2079. int tioclinux(struct tty_struct *tty, unsigned long arg)
  2080. {
  2081. char type, data;
  2082. char __user *p = (char __user *)arg;
  2083. int lines;
  2084. int ret;
  2085. if (tty->driver->type != TTY_DRIVER_TYPE_CONSOLE)
  2086. return -EINVAL;
  2087. if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
  2088. return -EPERM;
  2089. if (get_user(type, p))
  2090. return -EFAULT;
  2091. ret = 0;
  2092. switch (type)
  2093. {
  2094. case TIOCL_SETSEL:
  2095. acquire_console_sem();
  2096. ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
  2097. release_console_sem();
  2098. break;
  2099. case TIOCL_PASTESEL:
  2100. ret = paste_selection(tty);
  2101. break;
  2102. case TIOCL_UNBLANKSCREEN:
  2103. acquire_console_sem();
  2104. unblank_screen();
  2105. release_console_sem();
  2106. break;
  2107. case TIOCL_SELLOADLUT:
  2108. ret = sel_loadlut(p);
  2109. break;
  2110. case TIOCL_GETSHIFTSTATE:
  2111. /*
  2112. * Make it possible to react to Shift+Mousebutton.
  2113. * Note that 'shift_state' is an undocumented
  2114. * kernel-internal variable; programs not closely
  2115. * related to the kernel should not use this.
  2116. */
  2117. data = shift_state;
  2118. ret = __put_user(data, p);
  2119. break;
  2120. case TIOCL_GETMOUSEREPORTING:
  2121. data = mouse_reporting();
  2122. ret = __put_user(data, p);
  2123. break;
  2124. case TIOCL_SETVESABLANK:
  2125. set_vesa_blanking(p);
  2126. break;
  2127. case TIOCL_GETKMSGREDIRECT:
  2128. data = kmsg_redirect;
  2129. ret = __put_user(data, p);
  2130. break;
  2131. case TIOCL_SETKMSGREDIRECT:
  2132. if (!capable(CAP_SYS_ADMIN)) {
  2133. ret = -EPERM;
  2134. } else {
  2135. if (get_user(data, p+1))
  2136. ret = -EFAULT;
  2137. else
  2138. kmsg_redirect = data;
  2139. }
  2140. break;
  2141. case TIOCL_GETFGCONSOLE:
  2142. ret = fg_console;
  2143. break;
  2144. case TIOCL_SCROLLCONSOLE:
  2145. if (get_user(lines, (s32 __user *)(p+4))) {
  2146. ret = -EFAULT;
  2147. } else {
  2148. scrollfront(vc_cons[fg_console].d, lines);
  2149. ret = 0;
  2150. }
  2151. break;
  2152. case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
  2153. acquire_console_sem();
  2154. ignore_poke = 1;
  2155. do_blank_screen(0);
  2156. release_console_sem();
  2157. break;
  2158. case TIOCL_BLANKEDSCREEN:
  2159. ret = console_blanked;
  2160. break;
  2161. default:
  2162. ret = -EINVAL;
  2163. break;
  2164. }
  2165. return ret;
  2166. }
  2167. /*
  2168. * /dev/ttyN handling
  2169. */
  2170. static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  2171. {
  2172. int retval;
  2173. retval = do_con_write(tty, buf, count);
  2174. con_flush_chars(tty);
  2175. return retval;
  2176. }
  2177. static void con_put_char(struct tty_struct *tty, unsigned char ch)
  2178. {
  2179. if (in_interrupt())
  2180. return; /* n_r3964 calls put_char() from interrupt context */
  2181. do_con_write(tty, &ch, 1);
  2182. }
  2183. static int con_write_room(struct tty_struct *tty)
  2184. {
  2185. if (tty->stopped)
  2186. return 0;
  2187. return 4096; /* No limit, really; we're not buffering */
  2188. }
  2189. static int con_chars_in_buffer(struct tty_struct *tty)
  2190. {
  2191. return 0; /* we're not buffering */
  2192. }
  2193. /*
  2194. * con_throttle and con_unthrottle are only used for
  2195. * paste_selection(), which has to stuff in a large number of
  2196. * characters...
  2197. */
  2198. static void con_throttle(struct tty_struct *tty)
  2199. {
  2200. }
  2201. static void con_unthrottle(struct tty_struct *tty)
  2202. {
  2203. struct vc_data *vc = tty->driver_data;
  2204. wake_up_interruptible(&vc->paste_wait);
  2205. }
  2206. /*
  2207. * Turn the Scroll-Lock LED on when the tty is stopped
  2208. */
  2209. static void con_stop(struct tty_struct *tty)
  2210. {
  2211. int console_num;
  2212. if (!tty)
  2213. return;
  2214. console_num = tty->index;
  2215. if (!vc_cons_allocated(console_num))
  2216. return;
  2217. set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
  2218. set_leds();
  2219. }
  2220. /*
  2221. * Turn the Scroll-Lock LED off when the console is started
  2222. */
  2223. static void con_start(struct tty_struct *tty)
  2224. {
  2225. int console_num;
  2226. if (!tty)
  2227. return;
  2228. console_num = tty->index;
  2229. if (!vc_cons_allocated(console_num))
  2230. return;
  2231. clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
  2232. set_leds();
  2233. }
  2234. static void con_flush_chars(struct tty_struct *tty)
  2235. {
  2236. struct vc_data *vc;
  2237. if (in_interrupt()) /* from flush_to_ldisc */
  2238. return;
  2239. /* if we race with con_close(), vt may be null */
  2240. acquire_console_sem();
  2241. vc = tty->driver_data;
  2242. if (vc)
  2243. set_cursor(vc);
  2244. release_console_sem();
  2245. }
  2246. /*
  2247. * Allocate the console screen memory.
  2248. */
  2249. static int con_open(struct tty_struct *tty, struct file *filp)
  2250. {
  2251. unsigned int currcons = tty->index;
  2252. int ret = 0;
  2253. acquire_console_sem();
  2254. if (tty->driver_data == NULL) {
  2255. ret = vc_allocate(currcons);
  2256. if (ret == 0) {
  2257. struct vc_data *vc = vc_cons[currcons].d;
  2258. tty->driver_data = vc;
  2259. vc->vc_tty = tty;
  2260. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  2261. tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
  2262. tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
  2263. }
  2264. release_console_sem();
  2265. vcs_make_devfs(tty);
  2266. return ret;
  2267. }
  2268. }
  2269. release_console_sem();
  2270. return ret;
  2271. }
  2272. /*
  2273. * We take tty_mutex in here to prevent another thread from coming in via init_dev
  2274. * and taking a ref against the tty while we're in the process of forgetting
  2275. * about it and cleaning things up.
  2276. *
  2277. * This is because vcs_remove_devfs() can sleep and will drop the BKL.
  2278. */
  2279. static void con_close(struct tty_struct *tty, struct file *filp)
  2280. {
  2281. mutex_lock(&tty_mutex);
  2282. acquire_console_sem();
  2283. if (tty && tty->count == 1) {
  2284. struct vc_data *vc = tty->driver_data;
  2285. if (vc)
  2286. vc->vc_tty = NULL;
  2287. tty->driver_data = NULL;
  2288. release_console_sem();
  2289. vcs_remove_devfs(tty);
  2290. mutex_unlock(&tty_mutex);
  2291. /*
  2292. * tty_mutex is released, but we still hold BKL, so there is
  2293. * still exclusion against init_dev()
  2294. */
  2295. return;
  2296. }
  2297. release_console_sem();
  2298. mutex_unlock(&tty_mutex);
  2299. }
  2300. static void vc_init(struct vc_data *vc, unsigned int rows,
  2301. unsigned int cols, int do_clear)
  2302. {
  2303. int j, k ;
  2304. vc->vc_cols = cols;
  2305. vc->vc_rows = rows;
  2306. vc->vc_size_row = cols << 1;
  2307. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  2308. set_origin(vc);
  2309. vc->vc_pos = vc->vc_origin;
  2310. reset_vc(vc);
  2311. for (j=k=0; j<16; j++) {
  2312. vc->vc_palette[k++] = default_red[j] ;
  2313. vc->vc_palette[k++] = default_grn[j] ;
  2314. vc->vc_palette[k++] = default_blu[j] ;
  2315. }
  2316. vc->vc_def_color = 0x07; /* white */
  2317. vc->vc_ulcolor = 0x0f; /* bold white */
  2318. vc->vc_halfcolor = 0x08; /* grey */
  2319. init_waitqueue_head(&vc->paste_wait);
  2320. reset_terminal(vc, do_clear);
  2321. }
  2322. /*
  2323. * This routine initializes console interrupts, and does nothing
  2324. * else. If you want the screen to clear, call tty_write with
  2325. * the appropriate escape-sequence.
  2326. */
  2327. static int __init con_init(void)
  2328. {
  2329. const char *display_desc = NULL;
  2330. struct vc_data *vc;
  2331. unsigned int currcons = 0;
  2332. acquire_console_sem();
  2333. if (conswitchp)
  2334. display_desc = conswitchp->con_startup();
  2335. if (!display_desc) {
  2336. fg_console = 0;
  2337. release_console_sem();
  2338. return 0;
  2339. }
  2340. init_timer(&console_timer);
  2341. console_timer.function = blank_screen_t;
  2342. if (blankinterval) {
  2343. blank_state = blank_normal_wait;
  2344. mod_timer(&console_timer, jiffies + blankinterval);
  2345. }
  2346. /*
  2347. * kmalloc is not running yet - we use the bootmem allocator.
  2348. */
  2349. for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
  2350. vc_cons[currcons].d = vc = alloc_bootmem(sizeof(struct vc_data));
  2351. visual_init(vc, currcons, 1);
  2352. vc->vc_screenbuf = (unsigned short *)alloc_bootmem(vc->vc_screenbuf_size);
  2353. vc->vc_kmalloced = 0;
  2354. vc_init(vc, vc->vc_rows, vc->vc_cols,
  2355. currcons || !vc->vc_sw->con_save_screen);
  2356. }
  2357. currcons = fg_console = 0;
  2358. master_display_fg = vc = vc_cons[currcons].d;
  2359. set_origin(vc);
  2360. save_screen(vc);
  2361. gotoxy(vc, vc->vc_x, vc->vc_y);
  2362. csi_J(vc, 0);
  2363. update_screen(vc);
  2364. printk("Console: %s %s %dx%d",
  2365. vc->vc_can_do_color ? "colour" : "mono",
  2366. display_desc, vc->vc_cols, vc->vc_rows);
  2367. printable = 1;
  2368. printk("\n");
  2369. release_console_sem();
  2370. #ifdef CONFIG_VT_CONSOLE
  2371. register_console(&vt_console_driver);
  2372. #endif
  2373. return 0;
  2374. }
  2375. console_initcall(con_init);
  2376. static struct tty_operations con_ops = {
  2377. .open = con_open,
  2378. .close = con_close,
  2379. .write = con_write,
  2380. .write_room = con_write_room,
  2381. .put_char = con_put_char,
  2382. .flush_chars = con_flush_chars,
  2383. .chars_in_buffer = con_chars_in_buffer,
  2384. .ioctl = vt_ioctl,
  2385. .stop = con_stop,
  2386. .start = con_start,
  2387. .throttle = con_throttle,
  2388. .unthrottle = con_unthrottle,
  2389. };
  2390. int __init vty_init(void)
  2391. {
  2392. vcs_init();
  2393. console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
  2394. if (!console_driver)
  2395. panic("Couldn't allocate console driver\n");
  2396. console_driver->owner = THIS_MODULE;
  2397. console_driver->devfs_name = "vc/";
  2398. console_driver->name = "tty";
  2399. console_driver->name_base = 1;
  2400. console_driver->major = TTY_MAJOR;
  2401. console_driver->minor_start = 1;
  2402. console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  2403. console_driver->init_termios = tty_std_termios;
  2404. console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  2405. tty_set_operations(console_driver, &con_ops);
  2406. if (tty_register_driver(console_driver))
  2407. panic("Couldn't register console driver\n");
  2408. kbd_init();
  2409. console_map_init();
  2410. #ifdef CONFIG_PROM_CONSOLE
  2411. prom_con_init();
  2412. #endif
  2413. #ifdef CONFIG_MDA_CONSOLE
  2414. mda_console_init();
  2415. #endif
  2416. return 0;
  2417. }
  2418. #ifndef VT_SINGLE_DRIVER
  2419. /*
  2420. * If we support more console drivers, this function is used
  2421. * when a driver wants to take over some existing consoles
  2422. * and become default driver for newly opened ones.
  2423. */
  2424. int take_over_console(const struct consw *csw, int first, int last, int deflt)
  2425. {
  2426. int i, j = -1;
  2427. const char *desc;
  2428. struct module *owner;
  2429. owner = csw->owner;
  2430. if (!try_module_get(owner))
  2431. return -ENODEV;
  2432. acquire_console_sem();
  2433. desc = csw->con_startup();
  2434. if (!desc) {
  2435. release_console_sem();
  2436. module_put(owner);
  2437. return -ENODEV;
  2438. }
  2439. if (deflt) {
  2440. if (conswitchp)
  2441. module_put(conswitchp->owner);
  2442. __module_get(owner);
  2443. conswitchp = csw;
  2444. }
  2445. for (i = first; i <= last; i++) {
  2446. int old_was_color;
  2447. struct vc_data *vc = vc_cons[i].d;
  2448. if (con_driver_map[i])
  2449. module_put(con_driver_map[i]->owner);
  2450. __module_get(owner);
  2451. con_driver_map[i] = csw;
  2452. if (!vc || !vc->vc_sw)
  2453. continue;
  2454. j = i;
  2455. if (CON_IS_VISIBLE(vc))
  2456. save_screen(vc);
  2457. old_was_color = vc->vc_can_do_color;
  2458. vc->vc_sw->con_deinit(vc);
  2459. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  2460. vc->vc_visible_origin = vc->vc_origin;
  2461. vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
  2462. vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
  2463. visual_init(vc, i, 0);
  2464. update_attr(vc);
  2465. /* If the console changed between mono <-> color, then
  2466. * the attributes in the screenbuf will be wrong. The
  2467. * following resets all attributes to something sane.
  2468. */
  2469. if (old_was_color != vc->vc_can_do_color)
  2470. clear_buffer_attributes(vc);
  2471. if (CON_IS_VISIBLE(vc))
  2472. update_screen(vc);
  2473. }
  2474. printk("Console: switching ");
  2475. if (!deflt)
  2476. printk("consoles %d-%d ", first+1, last+1);
  2477. if (j >= 0)
  2478. printk("to %s %s %dx%d\n",
  2479. vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
  2480. desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
  2481. else
  2482. printk("to %s\n", desc);
  2483. release_console_sem();
  2484. module_put(owner);
  2485. return 0;
  2486. }
  2487. void give_up_console(const struct consw *csw)
  2488. {
  2489. int i;
  2490. for(i = 0; i < MAX_NR_CONSOLES; i++)
  2491. if (con_driver_map[i] == csw) {
  2492. module_put(csw->owner);
  2493. con_driver_map[i] = NULL;
  2494. }
  2495. }
  2496. #endif
  2497. /*
  2498. * Screen blanking
  2499. */
  2500. static void set_vesa_blanking(char __user *p)
  2501. {
  2502. unsigned int mode;
  2503. get_user(mode, p + 1);
  2504. vesa_blank_mode = (mode < 4) ? mode : 0;
  2505. }
  2506. void do_blank_screen(int entering_gfx)
  2507. {
  2508. struct vc_data *vc = vc_cons[fg_console].d;
  2509. int i;
  2510. WARN_CONSOLE_UNLOCKED();
  2511. if (console_blanked) {
  2512. if (blank_state == blank_vesa_wait) {
  2513. blank_state = blank_off;
  2514. vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
  2515. }
  2516. return;
  2517. }
  2518. if (blank_state != blank_normal_wait)
  2519. return;
  2520. blank_state = blank_off;
  2521. /* entering graphics mode? */
  2522. if (entering_gfx) {
  2523. hide_cursor(vc);
  2524. save_screen(vc);
  2525. vc->vc_sw->con_blank(vc, -1, 1);
  2526. console_blanked = fg_console + 1;
  2527. set_origin(vc);
  2528. return;
  2529. }
  2530. /* don't blank graphics */
  2531. if (vc->vc_mode != KD_TEXT) {
  2532. console_blanked = fg_console + 1;
  2533. return;
  2534. }
  2535. hide_cursor(vc);
  2536. del_timer_sync(&console_timer);
  2537. blank_timer_expired = 0;
  2538. save_screen(vc);
  2539. /* In case we need to reset origin, blanking hook returns 1 */
  2540. i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
  2541. console_blanked = fg_console + 1;
  2542. if (i)
  2543. set_origin(vc);
  2544. if (console_blank_hook && console_blank_hook(1))
  2545. return;
  2546. if (vesa_off_interval && vesa_blank_mode) {
  2547. blank_state = blank_vesa_wait;
  2548. mod_timer(&console_timer, jiffies + vesa_off_interval);
  2549. }
  2550. }
  2551. EXPORT_SYMBOL(do_blank_screen);
  2552. /*
  2553. * Called by timer as well as from vt_console_driver
  2554. */
  2555. void do_unblank_screen(int leaving_gfx)
  2556. {
  2557. struct vc_data *vc;
  2558. /* This should now always be called from a "sane" (read: can schedule)
  2559. * context for the sake of the low level drivers, except in the special
  2560. * case of oops_in_progress
  2561. */
  2562. if (!oops_in_progress)
  2563. might_sleep();
  2564. WARN_CONSOLE_UNLOCKED();
  2565. ignore_poke = 0;
  2566. if (!console_blanked)
  2567. return;
  2568. if (!vc_cons_allocated(fg_console)) {
  2569. /* impossible */
  2570. printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
  2571. return;
  2572. }
  2573. vc = vc_cons[fg_console].d;
  2574. if (vc->vc_mode != KD_TEXT)
  2575. return; /* but leave console_blanked != 0 */
  2576. if (blankinterval) {
  2577. mod_timer(&console_timer, jiffies + blankinterval);
  2578. blank_state = blank_normal_wait;
  2579. }
  2580. console_blanked = 0;
  2581. if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
  2582. /* Low-level driver cannot restore -> do it ourselves */
  2583. update_screen(vc);
  2584. if (console_blank_hook)
  2585. console_blank_hook(0);
  2586. set_palette(vc);
  2587. set_cursor(vc);
  2588. }
  2589. EXPORT_SYMBOL(do_unblank_screen);
  2590. /*
  2591. * This is called by the outside world to cause a forced unblank, mostly for
  2592. * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
  2593. * call it with 1 as an argument and so force a mode restore... that may kill
  2594. * X or at least garbage the screen but would also make the Oops visible...
  2595. */
  2596. void unblank_screen(void)
  2597. {
  2598. do_unblank_screen(0);
  2599. }
  2600. /*
  2601. * We defer the timer blanking to work queue so it can take the console mutex
  2602. * (console operations can still happen at irq time, but only from printk which
  2603. * has the console mutex. Not perfect yet, but better than no locking
  2604. */
  2605. static void blank_screen_t(unsigned long dummy)
  2606. {
  2607. if (unlikely(!keventd_up())) {
  2608. mod_timer(&console_timer, jiffies + blankinterval);
  2609. return;
  2610. }
  2611. blank_timer_expired = 1;
  2612. schedule_work(&console_work);
  2613. }
  2614. void poke_blanked_console(void)
  2615. {
  2616. WARN_CONSOLE_UNLOCKED();
  2617. /* Add this so we quickly catch whoever might call us in a non
  2618. * safe context. Nowadays, unblank_screen() isn't to be called in
  2619. * atomic contexts and is allowed to schedule (with the special case
  2620. * of oops_in_progress, but that isn't of any concern for this
  2621. * function. --BenH.
  2622. */
  2623. might_sleep();
  2624. /* This isn't perfectly race free, but a race here would be mostly harmless,
  2625. * at worse, we'll do a spurrious blank and it's unlikely
  2626. */
  2627. del_timer(&console_timer);
  2628. blank_timer_expired = 0;
  2629. if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
  2630. return;
  2631. if (console_blanked)
  2632. unblank_screen();
  2633. else if (blankinterval) {
  2634. mod_timer(&console_timer, jiffies + blankinterval);
  2635. blank_state = blank_normal_wait;
  2636. }
  2637. }
  2638. /*
  2639. * Palettes
  2640. */
  2641. static void set_palette(struct vc_data *vc)
  2642. {
  2643. WARN_CONSOLE_UNLOCKED();
  2644. if (vc->vc_mode != KD_GRAPHICS)
  2645. vc->vc_sw->con_set_palette(vc, color_table);
  2646. }
  2647. static int set_get_cmap(unsigned char __user *arg, int set)
  2648. {
  2649. int i, j, k;
  2650. WARN_CONSOLE_UNLOCKED();
  2651. for (i = 0; i < 16; i++)
  2652. if (set) {
  2653. get_user(default_red[i], arg++);
  2654. get_user(default_grn[i], arg++);
  2655. get_user(default_blu[i], arg++);
  2656. } else {
  2657. put_user(default_red[i], arg++);
  2658. put_user(default_grn[i], arg++);
  2659. put_user(default_blu[i], arg++);
  2660. }
  2661. if (set) {
  2662. for (i = 0; i < MAX_NR_CONSOLES; i++)
  2663. if (vc_cons_allocated(i)) {
  2664. for (j = k = 0; j < 16; j++) {
  2665. vc_cons[i].d->vc_palette[k++] = default_red[j];
  2666. vc_cons[i].d->vc_palette[k++] = default_grn[j];
  2667. vc_cons[i].d->vc_palette[k++] = default_blu[j];
  2668. }
  2669. set_palette(vc_cons[i].d);
  2670. }
  2671. }
  2672. return 0;
  2673. }
  2674. /*
  2675. * Load palette into the DAC registers. arg points to a colour
  2676. * map, 3 bytes per colour, 16 colours, range from 0 to 255.
  2677. */
  2678. int con_set_cmap(unsigned char __user *arg)
  2679. {
  2680. int rc;
  2681. acquire_console_sem();
  2682. rc = set_get_cmap (arg,1);
  2683. release_console_sem();
  2684. return rc;
  2685. }
  2686. int con_get_cmap(unsigned char __user *arg)
  2687. {
  2688. int rc;
  2689. acquire_console_sem();
  2690. rc = set_get_cmap (arg,0);
  2691. release_console_sem();
  2692. return rc;
  2693. }
  2694. void reset_palette(struct vc_data *vc)
  2695. {
  2696. int j, k;
  2697. for (j=k=0; j<16; j++) {
  2698. vc->vc_palette[k++] = default_red[j];
  2699. vc->vc_palette[k++] = default_grn[j];
  2700. vc->vc_palette[k++] = default_blu[j];
  2701. }
  2702. set_palette(vc);
  2703. }
  2704. /*
  2705. * Font switching
  2706. *
  2707. * Currently we only support fonts up to 32 pixels wide, at a maximum height
  2708. * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
  2709. * depending on width) reserved for each character which is kinda wasty, but
  2710. * this is done in order to maintain compatibility with the EGA/VGA fonts. It
  2711. * is upto the actual low-level console-driver convert data into its favorite
  2712. * format (maybe we should add a `fontoffset' field to the `display'
  2713. * structure so we won't have to convert the fontdata all the time.
  2714. * /Jes
  2715. */
  2716. #define max_font_size 65536
  2717. static int con_font_get(struct vc_data *vc, struct console_font_op *op)
  2718. {
  2719. struct console_font font;
  2720. int rc = -EINVAL;
  2721. int c;
  2722. if (vc->vc_mode != KD_TEXT)
  2723. return -EINVAL;
  2724. if (op->data) {
  2725. font.data = kmalloc(max_font_size, GFP_KERNEL);
  2726. if (!font.data)
  2727. return -ENOMEM;
  2728. } else
  2729. font.data = NULL;
  2730. acquire_console_sem();
  2731. if (vc->vc_sw->con_font_get)
  2732. rc = vc->vc_sw->con_font_get(vc, &font);
  2733. else
  2734. rc = -ENOSYS;
  2735. release_console_sem();
  2736. if (rc)
  2737. goto out;
  2738. c = (font.width+7)/8 * 32 * font.charcount;
  2739. if (op->data && font.charcount > op->charcount)
  2740. rc = -ENOSPC;
  2741. if (!(op->flags & KD_FONT_FLAG_OLD)) {
  2742. if (font.width > op->width || font.height > op->height)
  2743. rc = -ENOSPC;
  2744. } else {
  2745. if (font.width != 8)
  2746. rc = -EIO;
  2747. else if ((op->height && font.height > op->height) ||
  2748. font.height > 32)
  2749. rc = -ENOSPC;
  2750. }
  2751. if (rc)
  2752. goto out;
  2753. op->height = font.height;
  2754. op->width = font.width;
  2755. op->charcount = font.charcount;
  2756. if (op->data && copy_to_user(op->data, font.data, c))
  2757. rc = -EFAULT;
  2758. out:
  2759. kfree(font.data);
  2760. return rc;
  2761. }
  2762. static int con_font_set(struct vc_data *vc, struct console_font_op *op)
  2763. {
  2764. struct console_font font;
  2765. int rc = -EINVAL;
  2766. int size;
  2767. if (vc->vc_mode != KD_TEXT)
  2768. return -EINVAL;
  2769. if (!op->data)
  2770. return -EINVAL;
  2771. if (op->charcount > 512)
  2772. return -EINVAL;
  2773. if (!op->height) { /* Need to guess font height [compat] */
  2774. int h, i;
  2775. u8 __user *charmap = op->data;
  2776. u8 tmp;
  2777. /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
  2778. so that we can get rid of this soon */
  2779. if (!(op->flags & KD_FONT_FLAG_OLD))
  2780. return -EINVAL;
  2781. for (h = 32; h > 0; h--)
  2782. for (i = 0; i < op->charcount; i++) {
  2783. if (get_user(tmp, &charmap[32*i+h-1]))
  2784. return -EFAULT;
  2785. if (tmp)
  2786. goto nonzero;
  2787. }
  2788. return -EINVAL;
  2789. nonzero:
  2790. op->height = h;
  2791. }
  2792. if (op->width <= 0 || op->width > 32 || op->height > 32)
  2793. return -EINVAL;
  2794. size = (op->width+7)/8 * 32 * op->charcount;
  2795. if (size > max_font_size)
  2796. return -ENOSPC;
  2797. font.charcount = op->charcount;
  2798. font.height = op->height;
  2799. font.width = op->width;
  2800. font.data = kmalloc(size, GFP_KERNEL);
  2801. if (!font.data)
  2802. return -ENOMEM;
  2803. if (copy_from_user(font.data, op->data, size)) {
  2804. kfree(font.data);
  2805. return -EFAULT;
  2806. }
  2807. acquire_console_sem();
  2808. if (vc->vc_sw->con_font_set)
  2809. rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
  2810. else
  2811. rc = -ENOSYS;
  2812. release_console_sem();
  2813. kfree(font.data);
  2814. return rc;
  2815. }
  2816. static int con_font_default(struct vc_data *vc, struct console_font_op *op)
  2817. {
  2818. struct console_font font = {.width = op->width, .height = op->height};
  2819. char name[MAX_FONT_NAME];
  2820. char *s = name;
  2821. int rc;
  2822. if (vc->vc_mode != KD_TEXT)
  2823. return -EINVAL;
  2824. if (!op->data)
  2825. s = NULL;
  2826. else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
  2827. return -EFAULT;
  2828. else
  2829. name[MAX_FONT_NAME - 1] = 0;
  2830. acquire_console_sem();
  2831. if (vc->vc_sw->con_font_default)
  2832. rc = vc->vc_sw->con_font_default(vc, &font, s);
  2833. else
  2834. rc = -ENOSYS;
  2835. release_console_sem();
  2836. if (!rc) {
  2837. op->width = font.width;
  2838. op->height = font.height;
  2839. }
  2840. return rc;
  2841. }
  2842. static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
  2843. {
  2844. int con = op->height;
  2845. int rc;
  2846. if (vc->vc_mode != KD_TEXT)
  2847. return -EINVAL;
  2848. acquire_console_sem();
  2849. if (!vc->vc_sw->con_font_copy)
  2850. rc = -ENOSYS;
  2851. else if (con < 0 || !vc_cons_allocated(con))
  2852. rc = -ENOTTY;
  2853. else if (con == vc->vc_num) /* nothing to do */
  2854. rc = 0;
  2855. else
  2856. rc = vc->vc_sw->con_font_copy(vc, con);
  2857. release_console_sem();
  2858. return rc;
  2859. }
  2860. int con_font_op(struct vc_data *vc, struct console_font_op *op)
  2861. {
  2862. switch (op->op) {
  2863. case KD_FONT_OP_SET:
  2864. return con_font_set(vc, op);
  2865. case KD_FONT_OP_GET:
  2866. return con_font_get(vc, op);
  2867. case KD_FONT_OP_SET_DEFAULT:
  2868. return con_font_default(vc, op);
  2869. case KD_FONT_OP_COPY:
  2870. return con_font_copy(vc, op);
  2871. }
  2872. return -ENOSYS;
  2873. }
  2874. /*
  2875. * Interface exported to selection and vcs.
  2876. */
  2877. /* used by selection */
  2878. u16 screen_glyph(struct vc_data *vc, int offset)
  2879. {
  2880. u16 w = scr_readw(screenpos(vc, offset, 1));
  2881. u16 c = w & 0xff;
  2882. if (w & vc->vc_hi_font_mask)
  2883. c |= 0x100;
  2884. return c;
  2885. }
  2886. /* used by vcs - note the word offset */
  2887. unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
  2888. {
  2889. return screenpos(vc, 2 * w_offset, viewed);
  2890. }
  2891. void getconsxy(struct vc_data *vc, unsigned char *p)
  2892. {
  2893. p[0] = vc->vc_x;
  2894. p[1] = vc->vc_y;
  2895. }
  2896. void putconsxy(struct vc_data *vc, unsigned char *p)
  2897. {
  2898. hide_cursor(vc);
  2899. gotoxy(vc, p[0], p[1]);
  2900. set_cursor(vc);
  2901. }
  2902. u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
  2903. {
  2904. if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
  2905. return softcursor_original;
  2906. return scr_readw(org);
  2907. }
  2908. void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
  2909. {
  2910. scr_writew(val, org);
  2911. if ((unsigned long)org == vc->vc_pos) {
  2912. softcursor_original = -1;
  2913. add_softcursor(vc);
  2914. }
  2915. }
  2916. int is_console_suspend_safe(void)
  2917. {
  2918. /* It is unsafe to suspend devices while X has control of the
  2919. * hardware. Make sure we are running on a kernel-controlled console.
  2920. */
  2921. return vc_cons[fg_console].d->vc_mode == KD_TEXT;
  2922. }
  2923. /*
  2924. * Visible symbols for modules
  2925. */
  2926. EXPORT_SYMBOL(color_table);
  2927. EXPORT_SYMBOL(default_red);
  2928. EXPORT_SYMBOL(default_grn);
  2929. EXPORT_SYMBOL(default_blu);
  2930. EXPORT_SYMBOL(update_region);
  2931. EXPORT_SYMBOL(redraw_screen);
  2932. EXPORT_SYMBOL(vc_resize);
  2933. EXPORT_SYMBOL(fg_console);
  2934. EXPORT_SYMBOL(console_blank_hook);
  2935. EXPORT_SYMBOL(console_blanked);
  2936. EXPORT_SYMBOL(vc_cons);
  2937. #ifndef VT_SINGLE_DRIVER
  2938. EXPORT_SYMBOL(take_over_console);
  2939. EXPORT_SYMBOL(give_up_console);
  2940. #endif