vt.c 97 KB

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