vt.c 93 KB

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