vt.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. */
  5. /*
  6. * Hopefully this will be a rather complete VT102 implementation.
  7. *
  8. * Beeping thanks to John T Kohl.
  9. *
  10. * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
  11. * Chars, and VT100 enhancements by Peter MacDonald.
  12. *
  13. * Copy and paste function by Andrew Haylett,
  14. * some enhancements by Alessandro Rubini.
  15. *
  16. * Code to check for different video-cards mostly by Galen Hunt,
  17. * <g-hunt@ee.utah.edu>
  18. *
  19. * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
  20. * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
  21. *
  22. * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
  23. * Resizing of consoles, aeb, 940926
  24. *
  25. * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
  26. * <poe@daimi.aau.dk>
  27. *
  28. * User-defined bell sound, new setterm control sequences and printk
  29. * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
  30. *
  31. * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
  32. *
  33. * Merge with the abstract console driver by Geert Uytterhoeven
  34. * <geert@linux-m68k.org>, Jan 1997.
  35. *
  36. * Original m68k console driver modifications by
  37. *
  38. * - Arno Griffioen <arno@usn.nl>
  39. * - David Carter <carter@cs.bris.ac.uk>
  40. *
  41. * The abstract console driver provides a generic interface for a text
  42. * console. It supports VGA text mode, frame buffer based graphical consoles
  43. * and special graphics processors that are only accessible through some
  44. * registers (e.g. a TMS340x0 GSP).
  45. *
  46. * The interface to the hardware is specified using a special structure
  47. * (struct consw) which contains function pointers to console operations
  48. * (see <linux/console.h> for more information).
  49. *
  50. * Support for changeable cursor shape
  51. * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
  52. *
  53. * Ported to i386 and con_scrolldelta fixed
  54. * by Emmanuel Marty <core@ggi-project.org>, April 1998
  55. *
  56. * Resurrected character buffers in videoram plus lots of other trickery
  57. * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
  58. *
  59. * Removed old-style timers, introduced console_timer, made timer
  60. * deletion SMP-safe. 17Jun00, Andrew Morton
  61. *
  62. * Removed console_lock, enabled interrupts across all console operations
  63. * 13 March 2001, Andrew Morton
  64. *
  65. * Fixed UTF-8 mode so alternate charset modes always work according
  66. * to control sequences interpreted in do_con_trol function
  67. * preserving backward VT100 semigraphics compatibility,
  68. * malformed UTF sequences represented as sequences of replacement glyphs,
  69. * original codes or '?' as a last resort if replacement glyph is undefined
  70. * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
  71. */
  72. #include <linux/module.h>
  73. #include <linux/types.h>
  74. #include <linux/sched/signal.h>
  75. #include <linux/tty.h>
  76. #include <linux/tty_flip.h>
  77. #include <linux/kernel.h>
  78. #include <linux/string.h>
  79. #include <linux/errno.h>
  80. #include <linux/kd.h>
  81. #include <linux/slab.h>
  82. #include <linux/vmalloc.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/pm.h>
  97. #include <linux/font.h>
  98. #include <linux/bitops.h>
  99. #include <linux/notifier.h>
  100. #include <linux/device.h>
  101. #include <linux/io.h>
  102. #include <linux/uaccess.h>
  103. #include <linux/kdb.h>
  104. #include <linux/ctype.h>
  105. #include <linux/bsearch.h>
  106. #include <linux/gcd.h>
  107. #define MAX_NR_CON_DRIVER 16
  108. #define CON_DRIVER_FLAG_MODULE 1
  109. #define CON_DRIVER_FLAG_INIT 2
  110. #define CON_DRIVER_FLAG_ATTR 4
  111. #define CON_DRIVER_FLAG_ZOMBIE 8
  112. struct con_driver {
  113. const struct consw *con;
  114. const char *desc;
  115. struct device *dev;
  116. int node;
  117. int first;
  118. int last;
  119. int flag;
  120. };
  121. static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
  122. const struct consw *conswitchp;
  123. /* A bitmap for codes <32. A bit of 1 indicates that the code
  124. * corresponding to that bit number invokes some special action
  125. * (such as cursor movement) and should not be displayed as a
  126. * glyph unless the disp_ctrl mode is explicitly enabled.
  127. */
  128. #define CTRL_ACTION 0x0d00ff81
  129. #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
  130. /*
  131. * Here is the default bell parameters: 750HZ, 1/8th of a second
  132. */
  133. #define DEFAULT_BELL_PITCH 750
  134. #define DEFAULT_BELL_DURATION (HZ/8)
  135. #define DEFAULT_CURSOR_BLINK_MS 200
  136. struct vc vc_cons [MAX_NR_CONSOLES];
  137. #ifndef VT_SINGLE_DRIVER
  138. static const struct consw *con_driver_map[MAX_NR_CONSOLES];
  139. #endif
  140. static int con_open(struct tty_struct *, struct file *);
  141. static void vc_init(struct vc_data *vc, unsigned int rows,
  142. unsigned int cols, int do_clear);
  143. static void gotoxy(struct vc_data *vc, int new_x, int new_y);
  144. static void save_cur(struct vc_data *vc);
  145. static void reset_terminal(struct vc_data *vc, int do_clear);
  146. static void con_flush_chars(struct tty_struct *tty);
  147. static int set_vesa_blanking(char __user *p);
  148. static void set_cursor(struct vc_data *vc);
  149. static void hide_cursor(struct vc_data *vc);
  150. static void console_callback(struct work_struct *ignored);
  151. static void con_driver_unregister_callback(struct work_struct *ignored);
  152. static void blank_screen_t(struct timer_list *unused);
  153. static void set_palette(struct vc_data *vc);
  154. #define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
  155. static int printable; /* Is console ready for printing? */
  156. int default_utf8 = true;
  157. module_param(default_utf8, int, S_IRUGO | S_IWUSR);
  158. int global_cursor_default = -1;
  159. module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
  160. static int cur_default = CUR_DEFAULT;
  161. module_param(cur_default, int, S_IRUGO | S_IWUSR);
  162. /*
  163. * ignore_poke: don't unblank the screen when things are typed. This is
  164. * mainly for the privacy of braille terminal users.
  165. */
  166. static int ignore_poke;
  167. int do_poke_blanked_console;
  168. int console_blanked;
  169. static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
  170. static int vesa_off_interval;
  171. static int blankinterval;
  172. core_param(consoleblank, blankinterval, int, 0444);
  173. static DECLARE_WORK(console_work, console_callback);
  174. static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
  175. /*
  176. * fg_console is the current virtual console,
  177. * last_console is the last used one,
  178. * want_console is the console we want to switch to,
  179. * saved_* variants are for save/restore around kernel debugger enter/leave
  180. */
  181. int fg_console;
  182. int last_console;
  183. int want_console = -1;
  184. static int saved_fg_console;
  185. static int saved_last_console;
  186. static int saved_want_console;
  187. static int saved_vc_mode;
  188. static int saved_console_blanked;
  189. /*
  190. * For each existing display, we have a pointer to console currently visible
  191. * on that display, allowing consoles other than fg_console to be refreshed
  192. * appropriately. Unless the low-level driver supplies its own display_fg
  193. * variable, we use this one for the "master display".
  194. */
  195. static struct vc_data *master_display_fg;
  196. /*
  197. * Unfortunately, we need to delay tty echo when we're currently writing to the
  198. * console since the code is (and always was) not re-entrant, so we schedule
  199. * all flip requests to process context with schedule-task() and run it from
  200. * console_callback().
  201. */
  202. /*
  203. * For the same reason, we defer scrollback to the console callback.
  204. */
  205. static int scrollback_delta;
  206. /*
  207. * Hook so that the power management routines can (un)blank
  208. * the console on our behalf.
  209. */
  210. int (*console_blank_hook)(int);
  211. static DEFINE_TIMER(console_timer, blank_screen_t);
  212. static int blank_state;
  213. static int blank_timer_expired;
  214. enum {
  215. blank_off = 0,
  216. blank_normal_wait,
  217. blank_vesa_wait,
  218. };
  219. /*
  220. * /sys/class/tty/tty0/
  221. *
  222. * the attribute 'active' contains the name of the current vc
  223. * console and it supports poll() to detect vc switches
  224. */
  225. static struct device *tty0dev;
  226. /*
  227. * Notifier list for console events.
  228. */
  229. static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
  230. int register_vt_notifier(struct notifier_block *nb)
  231. {
  232. return atomic_notifier_chain_register(&vt_notifier_list, nb);
  233. }
  234. EXPORT_SYMBOL_GPL(register_vt_notifier);
  235. int unregister_vt_notifier(struct notifier_block *nb)
  236. {
  237. return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
  238. }
  239. EXPORT_SYMBOL_GPL(unregister_vt_notifier);
  240. static void notify_write(struct vc_data *vc, unsigned int unicode)
  241. {
  242. struct vt_notifier_param param = { .vc = vc, .c = unicode };
  243. atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
  244. }
  245. static void notify_update(struct vc_data *vc)
  246. {
  247. struct vt_notifier_param param = { .vc = vc };
  248. atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
  249. }
  250. /*
  251. * Low-Level Functions
  252. */
  253. static inline bool con_is_fg(const struct vc_data *vc)
  254. {
  255. return vc->vc_num == fg_console;
  256. }
  257. static inline bool con_should_update(const struct vc_data *vc)
  258. {
  259. return con_is_visible(vc) && !console_blanked;
  260. }
  261. static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
  262. {
  263. unsigned short *p;
  264. if (!viewed)
  265. p = (unsigned short *)(vc->vc_origin + offset);
  266. else if (!vc->vc_sw->con_screen_pos)
  267. p = (unsigned short *)(vc->vc_visible_origin + offset);
  268. else
  269. p = vc->vc_sw->con_screen_pos(vc, offset);
  270. return p;
  271. }
  272. /* Called from the keyboard irq path.. */
  273. static inline void scrolldelta(int lines)
  274. {
  275. /* FIXME */
  276. /* scrolldelta needs some kind of consistency lock, but the BKL was
  277. and still is not protecting versus the scheduled back end */
  278. scrollback_delta += lines;
  279. schedule_console_callback();
  280. }
  281. void schedule_console_callback(void)
  282. {
  283. schedule_work(&console_work);
  284. }
  285. /*
  286. * Code to manage unicode-based screen buffers
  287. */
  288. #ifdef NO_VC_UNI_SCREEN
  289. /* this disables and optimizes related code away at compile time */
  290. #define get_vc_uniscr(vc) NULL
  291. #else
  292. #define get_vc_uniscr(vc) vc->vc_uni_screen
  293. #endif
  294. #define VC_UNI_SCREEN_DEBUG 0
  295. typedef uint32_t char32_t;
  296. /*
  297. * Our screen buffer is preceded by an array of line pointers so that
  298. * scrolling only implies some pointer shuffling.
  299. */
  300. struct uni_screen {
  301. char32_t *lines[0];
  302. };
  303. static struct uni_screen *vc_uniscr_alloc(unsigned int cols, unsigned int rows)
  304. {
  305. struct uni_screen *uniscr;
  306. void *p;
  307. unsigned int memsize, i;
  308. /* allocate everything in one go */
  309. memsize = cols * rows * sizeof(char32_t);
  310. memsize += rows * sizeof(char32_t *);
  311. p = vmalloc(memsize);
  312. if (!p)
  313. return NULL;
  314. /* initial line pointers */
  315. uniscr = p;
  316. p = uniscr->lines + rows;
  317. for (i = 0; i < rows; i++) {
  318. uniscr->lines[i] = p;
  319. p += cols * sizeof(char32_t);
  320. }
  321. return uniscr;
  322. }
  323. static void vc_uniscr_free(struct uni_screen *uniscr)
  324. {
  325. vfree(uniscr);
  326. }
  327. static void vc_uniscr_set(struct vc_data *vc, struct uni_screen *new_uniscr)
  328. {
  329. vc_uniscr_free(vc->vc_uni_screen);
  330. vc->vc_uni_screen = new_uniscr;
  331. }
  332. static void vc_uniscr_putc(struct vc_data *vc, char32_t uc)
  333. {
  334. struct uni_screen *uniscr = get_vc_uniscr(vc);
  335. if (uniscr)
  336. uniscr->lines[vc->vc_y][vc->vc_x] = uc;
  337. }
  338. static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr)
  339. {
  340. struct uni_screen *uniscr = get_vc_uniscr(vc);
  341. if (uniscr) {
  342. char32_t *ln = uniscr->lines[vc->vc_y];
  343. unsigned int x = vc->vc_x, cols = vc->vc_cols;
  344. memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln));
  345. memset32(&ln[x], ' ', nr);
  346. }
  347. }
  348. static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr)
  349. {
  350. struct uni_screen *uniscr = get_vc_uniscr(vc);
  351. if (uniscr) {
  352. char32_t *ln = uniscr->lines[vc->vc_y];
  353. unsigned int x = vc->vc_x, cols = vc->vc_cols;
  354. memcpy(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln));
  355. memset32(&ln[cols - nr], ' ', nr);
  356. }
  357. }
  358. static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x,
  359. unsigned int nr)
  360. {
  361. struct uni_screen *uniscr = get_vc_uniscr(vc);
  362. if (uniscr) {
  363. char32_t *ln = uniscr->lines[vc->vc_y];
  364. memset32(&ln[x], ' ', nr);
  365. }
  366. }
  367. static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y,
  368. unsigned int nr)
  369. {
  370. struct uni_screen *uniscr = get_vc_uniscr(vc);
  371. if (uniscr) {
  372. unsigned int cols = vc->vc_cols;
  373. while (nr--)
  374. memset32(uniscr->lines[y++], ' ', cols);
  375. }
  376. }
  377. static void vc_uniscr_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
  378. enum con_scroll dir, unsigned int nr)
  379. {
  380. struct uni_screen *uniscr = get_vc_uniscr(vc);
  381. if (uniscr) {
  382. unsigned int i, j, k, sz, d, clear;
  383. sz = b - t;
  384. clear = b - nr;
  385. d = nr;
  386. if (dir == SM_DOWN) {
  387. clear = t;
  388. d = sz - nr;
  389. }
  390. for (i = 0; i < gcd(d, sz); i++) {
  391. char32_t *tmp = uniscr->lines[t + i];
  392. j = i;
  393. while (1) {
  394. k = j + d;
  395. if (k >= sz)
  396. k -= sz;
  397. if (k == i)
  398. break;
  399. uniscr->lines[t + j] = uniscr->lines[t + k];
  400. j = k;
  401. }
  402. uniscr->lines[t + j] = tmp;
  403. }
  404. vc_uniscr_clear_lines(vc, clear, nr);
  405. }
  406. }
  407. static void vc_uniscr_copy_area(struct uni_screen *dst,
  408. unsigned int dst_cols,
  409. unsigned int dst_rows,
  410. struct uni_screen *src,
  411. unsigned int src_cols,
  412. unsigned int src_top_row,
  413. unsigned int src_bot_row)
  414. {
  415. unsigned int dst_row = 0;
  416. if (!dst)
  417. return;
  418. while (src_top_row < src_bot_row) {
  419. char32_t *src_line = src->lines[src_top_row];
  420. char32_t *dst_line = dst->lines[dst_row];
  421. memcpy(dst_line, src_line, src_cols * sizeof(char32_t));
  422. if (dst_cols - src_cols)
  423. memset32(dst_line + src_cols, ' ', dst_cols - src_cols);
  424. src_top_row++;
  425. dst_row++;
  426. }
  427. while (dst_row < dst_rows) {
  428. char32_t *dst_line = dst->lines[dst_row];
  429. memset32(dst_line, ' ', dst_cols);
  430. dst_row++;
  431. }
  432. }
  433. /*
  434. * Called from vcs_read() to make sure unicode screen retrieval is possible.
  435. * This will initialize the unicode screen buffer if not already done.
  436. * This returns 0 if OK, or a negative error code otherwise.
  437. * In particular, -ENODATA is returned if the console is not in UTF-8 mode.
  438. */
  439. int vc_uniscr_check(struct vc_data *vc)
  440. {
  441. struct uni_screen *uniscr;
  442. unsigned short *p;
  443. int x, y, mask;
  444. if (__is_defined(NO_VC_UNI_SCREEN))
  445. return -EOPNOTSUPP;
  446. WARN_CONSOLE_UNLOCKED();
  447. if (!vc->vc_utf)
  448. return -ENODATA;
  449. if (vc->vc_uni_screen)
  450. return 0;
  451. uniscr = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows);
  452. if (!uniscr)
  453. return -ENOMEM;
  454. /*
  455. * Let's populate it initially with (imperfect) reverse translation.
  456. * This is the next best thing we can do short of having it enabled
  457. * from the start even when no users rely on this functionality. True
  458. * unicode content will be available after a complete screen refresh.
  459. */
  460. p = (unsigned short *)vc->vc_origin;
  461. mask = vc->vc_hi_font_mask | 0xff;
  462. for (y = 0; y < vc->vc_rows; y++) {
  463. char32_t *line = uniscr->lines[y];
  464. for (x = 0; x < vc->vc_cols; x++) {
  465. u16 glyph = scr_readw(p++) & mask;
  466. line[x] = inverse_translate(vc, glyph, true);
  467. }
  468. }
  469. vc->vc_uni_screen = uniscr;
  470. return 0;
  471. }
  472. /*
  473. * Called from vcs_read() to get the unicode data from the screen.
  474. * This must be preceded by a successful call to vc_uniscr_check() once
  475. * the console lock has been taken.
  476. */
  477. void vc_uniscr_copy_line(struct vc_data *vc, void *dest, int viewed,
  478. unsigned int row, unsigned int col, unsigned int nr)
  479. {
  480. struct uni_screen *uniscr = get_vc_uniscr(vc);
  481. int offset = row * vc->vc_size_row + col * 2;
  482. unsigned long pos;
  483. BUG_ON(!uniscr);
  484. pos = (unsigned long)screenpos(vc, offset, viewed);
  485. if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
  486. /*
  487. * Desired position falls in the main screen buffer.
  488. * However the actual row/col might be different if
  489. * scrollback is active.
  490. */
  491. row = (pos - vc->vc_origin) / vc->vc_size_row;
  492. col = ((pos - vc->vc_origin) % vc->vc_size_row) / 2;
  493. memcpy(dest, &uniscr->lines[row][col], nr * sizeof(char32_t));
  494. } else {
  495. /*
  496. * Scrollback is active. For now let's simply backtranslate
  497. * the screen glyphs until the unicode screen buffer does
  498. * synchronize with console display drivers for a scrollback
  499. * buffer of its own.
  500. */
  501. u16 *p = (u16 *)pos;
  502. int mask = vc->vc_hi_font_mask | 0xff;
  503. char32_t *uni_buf = dest;
  504. while (nr--) {
  505. u16 glyph = scr_readw(p++) & mask;
  506. *uni_buf++ = inverse_translate(vc, glyph, true);
  507. }
  508. }
  509. }
  510. /* this is for validation and debugging only */
  511. static void vc_uniscr_debug_check(struct vc_data *vc)
  512. {
  513. struct uni_screen *uniscr = get_vc_uniscr(vc);
  514. unsigned short *p;
  515. int x, y, mask;
  516. if (!VC_UNI_SCREEN_DEBUG || !uniscr)
  517. return;
  518. WARN_CONSOLE_UNLOCKED();
  519. /*
  520. * Make sure our unicode screen translates into the same glyphs
  521. * as the actual screen. This is brutal indeed.
  522. */
  523. p = (unsigned short *)vc->vc_origin;
  524. mask = vc->vc_hi_font_mask | 0xff;
  525. for (y = 0; y < vc->vc_rows; y++) {
  526. char32_t *line = uniscr->lines[y];
  527. for (x = 0; x < vc->vc_cols; x++) {
  528. u16 glyph = scr_readw(p++) & mask;
  529. char32_t uc = line[x];
  530. int tc = conv_uni_to_pc(vc, uc);
  531. if (tc == -4)
  532. tc = conv_uni_to_pc(vc, 0xfffd);
  533. if (tc == -4)
  534. tc = conv_uni_to_pc(vc, '?');
  535. if (tc != glyph)
  536. pr_err_ratelimited(
  537. "%s: mismatch at %d,%d: glyph=%#x tc=%#x\n",
  538. __func__, x, y, glyph, tc);
  539. }
  540. }
  541. }
  542. static void con_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
  543. enum con_scroll dir, unsigned int nr)
  544. {
  545. u16 *clear, *d, *s;
  546. if (t + nr >= b)
  547. nr = b - t - 1;
  548. if (b > vc->vc_rows || t >= b || nr < 1)
  549. return;
  550. vc_uniscr_scroll(vc, t, b, dir, nr);
  551. if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, dir, nr))
  552. return;
  553. s = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * t);
  554. d = (u16 *)(vc->vc_origin + vc->vc_size_row * (t + nr));
  555. if (dir == SM_UP) {
  556. clear = s + (b - t - nr) * vc->vc_cols;
  557. swap(s, d);
  558. }
  559. scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
  560. scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr);
  561. }
  562. static void do_update_region(struct vc_data *vc, unsigned long start, int count)
  563. {
  564. unsigned int xx, yy, offset;
  565. u16 *p;
  566. p = (u16 *) start;
  567. if (!vc->vc_sw->con_getxy) {
  568. offset = (start - vc->vc_origin) / 2;
  569. xx = offset % vc->vc_cols;
  570. yy = offset / vc->vc_cols;
  571. } else {
  572. int nxx, nyy;
  573. start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
  574. xx = nxx; yy = nyy;
  575. }
  576. for(;;) {
  577. u16 attrib = scr_readw(p) & 0xff00;
  578. int startx = xx;
  579. u16 *q = p;
  580. while (xx < vc->vc_cols && count) {
  581. if (attrib != (scr_readw(p) & 0xff00)) {
  582. if (p > q)
  583. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  584. startx = xx;
  585. q = p;
  586. attrib = scr_readw(p) & 0xff00;
  587. }
  588. p++;
  589. xx++;
  590. count--;
  591. }
  592. if (p > q)
  593. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  594. if (!count)
  595. break;
  596. xx = 0;
  597. yy++;
  598. if (vc->vc_sw->con_getxy) {
  599. p = (u16 *)start;
  600. start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
  601. }
  602. }
  603. }
  604. void update_region(struct vc_data *vc, unsigned long start, int count)
  605. {
  606. WARN_CONSOLE_UNLOCKED();
  607. if (con_should_update(vc)) {
  608. hide_cursor(vc);
  609. do_update_region(vc, start, count);
  610. set_cursor(vc);
  611. }
  612. }
  613. /* Structure of attributes is hardware-dependent */
  614. static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
  615. u8 _underline, u8 _reverse, u8 _italic)
  616. {
  617. if (vc->vc_sw->con_build_attr)
  618. return vc->vc_sw->con_build_attr(vc, _color, _intensity,
  619. _blink, _underline, _reverse, _italic);
  620. /*
  621. * ++roman: I completely changed the attribute format for monochrome
  622. * mode (!can_do_color). The formerly used MDA (monochrome display
  623. * adapter) format didn't allow the combination of certain effects.
  624. * Now the attribute is just a bit vector:
  625. * Bit 0..1: intensity (0..2)
  626. * Bit 2 : underline
  627. * Bit 3 : reverse
  628. * Bit 7 : blink
  629. */
  630. {
  631. u8 a = _color;
  632. if (!vc->vc_can_do_color)
  633. return _intensity |
  634. (_italic ? 2 : 0) |
  635. (_underline ? 4 : 0) |
  636. (_reverse ? 8 : 0) |
  637. (_blink ? 0x80 : 0);
  638. if (_italic)
  639. a = (a & 0xF0) | vc->vc_itcolor;
  640. else if (_underline)
  641. a = (a & 0xf0) | vc->vc_ulcolor;
  642. else if (_intensity == 0)
  643. a = (a & 0xf0) | vc->vc_halfcolor;
  644. if (_reverse)
  645. a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
  646. if (_blink)
  647. a ^= 0x80;
  648. if (_intensity == 2)
  649. a ^= 0x08;
  650. if (vc->vc_hi_font_mask == 0x100)
  651. a <<= 1;
  652. return a;
  653. }
  654. }
  655. static void update_attr(struct vc_data *vc)
  656. {
  657. vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
  658. vc->vc_blink, vc->vc_underline,
  659. vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
  660. vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
  661. }
  662. /* Note: inverting the screen twice should revert to the original state */
  663. void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
  664. {
  665. unsigned short *p;
  666. WARN_CONSOLE_UNLOCKED();
  667. count /= 2;
  668. p = screenpos(vc, offset, viewed);
  669. if (vc->vc_sw->con_invert_region) {
  670. vc->vc_sw->con_invert_region(vc, p, count);
  671. } else {
  672. u16 *q = p;
  673. int cnt = count;
  674. u16 a;
  675. if (!vc->vc_can_do_color) {
  676. while (cnt--) {
  677. a = scr_readw(q);
  678. a ^= 0x0800;
  679. scr_writew(a, q);
  680. q++;
  681. }
  682. } else if (vc->vc_hi_font_mask == 0x100) {
  683. while (cnt--) {
  684. a = scr_readw(q);
  685. a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
  686. scr_writew(a, q);
  687. q++;
  688. }
  689. } else {
  690. while (cnt--) {
  691. a = scr_readw(q);
  692. a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
  693. scr_writew(a, q);
  694. q++;
  695. }
  696. }
  697. }
  698. if (con_should_update(vc))
  699. do_update_region(vc, (unsigned long) p, count);
  700. notify_update(vc);
  701. }
  702. /* used by selection: complement pointer position */
  703. void complement_pos(struct vc_data *vc, int offset)
  704. {
  705. static int old_offset = -1;
  706. static unsigned short old;
  707. static unsigned short oldx, oldy;
  708. WARN_CONSOLE_UNLOCKED();
  709. if (old_offset != -1 && old_offset >= 0 &&
  710. old_offset < vc->vc_screenbuf_size) {
  711. scr_writew(old, screenpos(vc, old_offset, 1));
  712. if (con_should_update(vc))
  713. vc->vc_sw->con_putc(vc, old, oldy, oldx);
  714. notify_update(vc);
  715. }
  716. old_offset = offset;
  717. if (offset != -1 && offset >= 0 &&
  718. offset < vc->vc_screenbuf_size) {
  719. unsigned short new;
  720. unsigned short *p;
  721. p = screenpos(vc, offset, 1);
  722. old = scr_readw(p);
  723. new = old ^ vc->vc_complement_mask;
  724. scr_writew(new, p);
  725. if (con_should_update(vc)) {
  726. oldx = (offset >> 1) % vc->vc_cols;
  727. oldy = (offset >> 1) / vc->vc_cols;
  728. vc->vc_sw->con_putc(vc, new, oldy, oldx);
  729. }
  730. notify_update(vc);
  731. }
  732. }
  733. static void insert_char(struct vc_data *vc, unsigned int nr)
  734. {
  735. unsigned short *p = (unsigned short *) vc->vc_pos;
  736. vc_uniscr_insert(vc, nr);
  737. scr_memmovew(p + nr, p, (vc->vc_cols - vc->vc_x - nr) * 2);
  738. scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
  739. vc->vc_need_wrap = 0;
  740. if (con_should_update(vc))
  741. do_update_region(vc, (unsigned long) p,
  742. vc->vc_cols - vc->vc_x);
  743. }
  744. static void delete_char(struct vc_data *vc, unsigned int nr)
  745. {
  746. unsigned short *p = (unsigned short *) vc->vc_pos;
  747. vc_uniscr_delete(vc, nr);
  748. scr_memcpyw(p, p + nr, (vc->vc_cols - vc->vc_x - nr) * 2);
  749. scr_memsetw(p + vc->vc_cols - vc->vc_x - nr, vc->vc_video_erase_char,
  750. nr * 2);
  751. vc->vc_need_wrap = 0;
  752. if (con_should_update(vc))
  753. do_update_region(vc, (unsigned long) p,
  754. vc->vc_cols - vc->vc_x);
  755. }
  756. static int softcursor_original = -1;
  757. static void add_softcursor(struct vc_data *vc)
  758. {
  759. int i = scr_readw((u16 *) vc->vc_pos);
  760. u32 type = vc->vc_cursor_type;
  761. if (! (type & 0x10)) return;
  762. if (softcursor_original != -1) return;
  763. softcursor_original = i;
  764. i |= ((type >> 8) & 0xff00 );
  765. i ^= ((type) & 0xff00 );
  766. if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
  767. if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
  768. scr_writew(i, (u16 *) vc->vc_pos);
  769. if (con_should_update(vc))
  770. vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
  771. }
  772. static void hide_softcursor(struct vc_data *vc)
  773. {
  774. if (softcursor_original != -1) {
  775. scr_writew(softcursor_original, (u16 *)vc->vc_pos);
  776. if (con_should_update(vc))
  777. vc->vc_sw->con_putc(vc, softcursor_original,
  778. vc->vc_y, vc->vc_x);
  779. softcursor_original = -1;
  780. }
  781. }
  782. static void hide_cursor(struct vc_data *vc)
  783. {
  784. if (vc_is_sel(vc))
  785. clear_selection();
  786. vc->vc_sw->con_cursor(vc, CM_ERASE);
  787. hide_softcursor(vc);
  788. }
  789. static void set_cursor(struct vc_data *vc)
  790. {
  791. if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
  792. return;
  793. if (vc->vc_deccm) {
  794. if (vc_is_sel(vc))
  795. clear_selection();
  796. add_softcursor(vc);
  797. if ((vc->vc_cursor_type & 0x0f) != 1)
  798. vc->vc_sw->con_cursor(vc, CM_DRAW);
  799. } else
  800. hide_cursor(vc);
  801. }
  802. static void set_origin(struct vc_data *vc)
  803. {
  804. WARN_CONSOLE_UNLOCKED();
  805. if (!con_is_visible(vc) ||
  806. !vc->vc_sw->con_set_origin ||
  807. !vc->vc_sw->con_set_origin(vc))
  808. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  809. vc->vc_visible_origin = vc->vc_origin;
  810. vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
  811. vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
  812. }
  813. static void save_screen(struct vc_data *vc)
  814. {
  815. WARN_CONSOLE_UNLOCKED();
  816. if (vc->vc_sw->con_save_screen)
  817. vc->vc_sw->con_save_screen(vc);
  818. }
  819. static void flush_scrollback(struct vc_data *vc)
  820. {
  821. WARN_CONSOLE_UNLOCKED();
  822. set_origin(vc);
  823. if (vc->vc_sw->con_flush_scrollback) {
  824. vc->vc_sw->con_flush_scrollback(vc);
  825. } else if (con_is_visible(vc)) {
  826. /*
  827. * When no con_flush_scrollback method is provided then the
  828. * legacy way for flushing the scrollback buffer is to use
  829. * a side effect of the con_switch method. We do it only on
  830. * the foreground console as background consoles have no
  831. * scrollback buffers in that case and we obviously don't
  832. * want to switch to them.
  833. */
  834. hide_cursor(vc);
  835. vc->vc_sw->con_switch(vc);
  836. set_cursor(vc);
  837. }
  838. }
  839. /*
  840. * Redrawing of screen
  841. */
  842. void clear_buffer_attributes(struct vc_data *vc)
  843. {
  844. unsigned short *p = (unsigned short *)vc->vc_origin;
  845. int count = vc->vc_screenbuf_size / 2;
  846. int mask = vc->vc_hi_font_mask | 0xff;
  847. for (; count > 0; count--, p++) {
  848. scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
  849. }
  850. }
  851. void redraw_screen(struct vc_data *vc, int is_switch)
  852. {
  853. int redraw = 0;
  854. WARN_CONSOLE_UNLOCKED();
  855. if (!vc) {
  856. /* strange ... */
  857. /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
  858. return;
  859. }
  860. if (is_switch) {
  861. struct vc_data *old_vc = vc_cons[fg_console].d;
  862. if (old_vc == vc)
  863. return;
  864. if (!con_is_visible(vc))
  865. redraw = 1;
  866. *vc->vc_display_fg = vc;
  867. fg_console = vc->vc_num;
  868. hide_cursor(old_vc);
  869. if (!con_is_visible(old_vc)) {
  870. save_screen(old_vc);
  871. set_origin(old_vc);
  872. }
  873. if (tty0dev)
  874. sysfs_notify(&tty0dev->kobj, NULL, "active");
  875. } else {
  876. hide_cursor(vc);
  877. redraw = 1;
  878. }
  879. if (redraw) {
  880. int update;
  881. int old_was_color = vc->vc_can_do_color;
  882. set_origin(vc);
  883. update = vc->vc_sw->con_switch(vc);
  884. set_palette(vc);
  885. /*
  886. * If console changed from mono<->color, the best we can do
  887. * is to clear the buffer attributes. As it currently stands,
  888. * rebuilding new attributes from the old buffer is not doable
  889. * without overly complex code.
  890. */
  891. if (old_was_color != vc->vc_can_do_color) {
  892. update_attr(vc);
  893. clear_buffer_attributes(vc);
  894. }
  895. /* Forcibly update if we're panicing */
  896. if ((update && vc->vc_mode != KD_GRAPHICS) ||
  897. vt_force_oops_output(vc))
  898. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  899. }
  900. set_cursor(vc);
  901. if (is_switch) {
  902. set_leds();
  903. compute_shiftstate();
  904. notify_update(vc);
  905. }
  906. }
  907. /*
  908. * Allocation, freeing and resizing of VTs.
  909. */
  910. int vc_cons_allocated(unsigned int i)
  911. {
  912. return (i < MAX_NR_CONSOLES && vc_cons[i].d);
  913. }
  914. static void visual_init(struct vc_data *vc, int num, int init)
  915. {
  916. /* ++Geert: vc->vc_sw->con_init determines console size */
  917. if (vc->vc_sw)
  918. module_put(vc->vc_sw->owner);
  919. vc->vc_sw = conswitchp;
  920. #ifndef VT_SINGLE_DRIVER
  921. if (con_driver_map[num])
  922. vc->vc_sw = con_driver_map[num];
  923. #endif
  924. __module_get(vc->vc_sw->owner);
  925. vc->vc_num = num;
  926. vc->vc_display_fg = &master_display_fg;
  927. if (vc->vc_uni_pagedir_loc)
  928. con_free_unimap(vc);
  929. vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
  930. vc->vc_uni_pagedir = NULL;
  931. vc->vc_hi_font_mask = 0;
  932. vc->vc_complement_mask = 0;
  933. vc->vc_can_do_color = 0;
  934. vc->vc_panic_force_write = false;
  935. vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
  936. vc->vc_sw->con_init(vc, init);
  937. if (!vc->vc_complement_mask)
  938. vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
  939. vc->vc_s_complement_mask = vc->vc_complement_mask;
  940. vc->vc_size_row = vc->vc_cols << 1;
  941. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  942. }
  943. static void visual_deinit(struct vc_data *vc)
  944. {
  945. vc->vc_sw->con_deinit(vc);
  946. module_put(vc->vc_sw->owner);
  947. }
  948. static void vc_port_destruct(struct tty_port *port)
  949. {
  950. struct vc_data *vc = container_of(port, struct vc_data, port);
  951. kfree(vc);
  952. }
  953. static const struct tty_port_operations vc_port_ops = {
  954. .destruct = vc_port_destruct,
  955. };
  956. /*
  957. * Change # of rows and columns (0 means unchanged/the size of fg_console)
  958. * [this is to be used together with some user program
  959. * like resize that changes the hardware videomode]
  960. */
  961. #define VC_MAXCOL (32767)
  962. #define VC_MAXROW (32767)
  963. int vc_allocate(unsigned int currcons) /* return 0 on success */
  964. {
  965. struct vt_notifier_param param;
  966. struct vc_data *vc;
  967. int err;
  968. WARN_CONSOLE_UNLOCKED();
  969. if (currcons >= MAX_NR_CONSOLES)
  970. return -ENXIO;
  971. if (vc_cons[currcons].d)
  972. return 0;
  973. /* due to the granularity of kmalloc, we waste some memory here */
  974. /* the alloc is done in two steps, to optimize the common situation
  975. of a 25x80 console (structsize=216, screenbuf_size=4000) */
  976. /* although the numbers above are not valid since long ago, the
  977. point is still up-to-date and the comment still has its value
  978. even if only as a historical artifact. --mj, July 1998 */
  979. param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
  980. if (!vc)
  981. return -ENOMEM;
  982. vc_cons[currcons].d = vc;
  983. tty_port_init(&vc->port);
  984. vc->port.ops = &vc_port_ops;
  985. INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
  986. visual_init(vc, currcons, 1);
  987. if (!*vc->vc_uni_pagedir_loc)
  988. con_set_default_unimap(vc);
  989. err = -EINVAL;
  990. if (vc->vc_cols > VC_MAXCOL || vc->vc_rows > VC_MAXROW ||
  991. vc->vc_screenbuf_size > KMALLOC_MAX_SIZE || !vc->vc_screenbuf_size)
  992. goto err_free;
  993. err = -ENOMEM;
  994. vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
  995. if (!vc->vc_screenbuf)
  996. goto err_free;
  997. /* If no drivers have overridden us and the user didn't pass a
  998. boot option, default to displaying the cursor */
  999. if (global_cursor_default == -1)
  1000. global_cursor_default = 1;
  1001. vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
  1002. vcs_make_sysfs(currcons);
  1003. atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
  1004. return 0;
  1005. err_free:
  1006. visual_deinit(vc);
  1007. kfree(vc);
  1008. vc_cons[currcons].d = NULL;
  1009. return err;
  1010. }
  1011. static inline int resize_screen(struct vc_data *vc, int width, int height,
  1012. int user)
  1013. {
  1014. /* Resizes the resolution of the display adapater */
  1015. int err = 0;
  1016. if (vc->vc_sw->con_resize)
  1017. err = vc->vc_sw->con_resize(vc, width, height, user);
  1018. return err;
  1019. }
  1020. /**
  1021. * vc_do_resize - resizing method for the tty
  1022. * @tty: tty being resized
  1023. * @real_tty: real tty (different to tty if a pty/tty pair)
  1024. * @vc: virtual console private data
  1025. * @cols: columns
  1026. * @lines: lines
  1027. *
  1028. * Resize a virtual console, clipping according to the actual constraints.
  1029. * If the caller passes a tty structure then update the termios winsize
  1030. * information and perform any necessary signal handling.
  1031. *
  1032. * Caller must hold the console semaphore. Takes the termios rwsem and
  1033. * ctrl_lock of the tty IFF a tty is passed.
  1034. */
  1035. static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
  1036. unsigned int cols, unsigned int lines)
  1037. {
  1038. unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
  1039. unsigned long end;
  1040. unsigned int old_rows, old_row_size, first_copied_row;
  1041. unsigned int new_cols, new_rows, new_row_size, new_screen_size;
  1042. unsigned int user;
  1043. unsigned short *oldscreen, *newscreen;
  1044. struct uni_screen *new_uniscr = NULL;
  1045. WARN_CONSOLE_UNLOCKED();
  1046. if (!vc)
  1047. return -ENXIO;
  1048. user = vc->vc_resize_user;
  1049. vc->vc_resize_user = 0;
  1050. if (cols > VC_MAXCOL || lines > VC_MAXROW)
  1051. return -EINVAL;
  1052. new_cols = (cols ? cols : vc->vc_cols);
  1053. new_rows = (lines ? lines : vc->vc_rows);
  1054. new_row_size = new_cols << 1;
  1055. new_screen_size = new_row_size * new_rows;
  1056. if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
  1057. return 0;
  1058. if (new_screen_size > KMALLOC_MAX_SIZE || !new_screen_size)
  1059. return -EINVAL;
  1060. newscreen = kzalloc(new_screen_size, GFP_USER);
  1061. if (!newscreen)
  1062. return -ENOMEM;
  1063. if (get_vc_uniscr(vc)) {
  1064. new_uniscr = vc_uniscr_alloc(new_cols, new_rows);
  1065. if (!new_uniscr) {
  1066. kfree(newscreen);
  1067. return -ENOMEM;
  1068. }
  1069. }
  1070. if (vc_is_sel(vc))
  1071. clear_selection();
  1072. old_rows = vc->vc_rows;
  1073. old_row_size = vc->vc_size_row;
  1074. err = resize_screen(vc, new_cols, new_rows, user);
  1075. if (err) {
  1076. kfree(newscreen);
  1077. vc_uniscr_free(new_uniscr);
  1078. return err;
  1079. }
  1080. vc->vc_rows = new_rows;
  1081. vc->vc_cols = new_cols;
  1082. vc->vc_size_row = new_row_size;
  1083. vc->vc_screenbuf_size = new_screen_size;
  1084. rlth = min(old_row_size, new_row_size);
  1085. rrem = new_row_size - rlth;
  1086. old_origin = vc->vc_origin;
  1087. new_origin = (long) newscreen;
  1088. new_scr_end = new_origin + new_screen_size;
  1089. if (vc->vc_y > new_rows) {
  1090. if (old_rows - vc->vc_y < new_rows) {
  1091. /*
  1092. * Cursor near the bottom, copy contents from the
  1093. * bottom of buffer
  1094. */
  1095. first_copied_row = (old_rows - new_rows);
  1096. } else {
  1097. /*
  1098. * Cursor is in no man's land, copy 1/2 screenful
  1099. * from the top and bottom of cursor position
  1100. */
  1101. first_copied_row = (vc->vc_y - new_rows/2);
  1102. }
  1103. old_origin += first_copied_row * old_row_size;
  1104. } else
  1105. first_copied_row = 0;
  1106. end = old_origin + old_row_size * min(old_rows, new_rows);
  1107. vc_uniscr_copy_area(new_uniscr, new_cols, new_rows,
  1108. get_vc_uniscr(vc), rlth/2, first_copied_row,
  1109. min(old_rows, new_rows));
  1110. vc_uniscr_set(vc, new_uniscr);
  1111. update_attr(vc);
  1112. while (old_origin < end) {
  1113. scr_memcpyw((unsigned short *) new_origin,
  1114. (unsigned short *) old_origin, rlth);
  1115. if (rrem)
  1116. scr_memsetw((void *)(new_origin + rlth),
  1117. vc->vc_video_erase_char, rrem);
  1118. old_origin += old_row_size;
  1119. new_origin += new_row_size;
  1120. }
  1121. if (new_scr_end > new_origin)
  1122. scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
  1123. new_scr_end - new_origin);
  1124. oldscreen = vc->vc_screenbuf;
  1125. vc->vc_screenbuf = newscreen;
  1126. vc->vc_screenbuf_size = new_screen_size;
  1127. set_origin(vc);
  1128. kfree(oldscreen);
  1129. /* do part of a reset_terminal() */
  1130. vc->vc_top = 0;
  1131. vc->vc_bottom = vc->vc_rows;
  1132. gotoxy(vc, vc->vc_x, vc->vc_y);
  1133. save_cur(vc);
  1134. if (tty) {
  1135. /* Rewrite the requested winsize data with the actual
  1136. resulting sizes */
  1137. struct winsize ws;
  1138. memset(&ws, 0, sizeof(ws));
  1139. ws.ws_row = vc->vc_rows;
  1140. ws.ws_col = vc->vc_cols;
  1141. ws.ws_ypixel = vc->vc_scan_lines;
  1142. tty_do_resize(tty, &ws);
  1143. }
  1144. if (con_is_visible(vc))
  1145. update_screen(vc);
  1146. vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
  1147. notify_update(vc);
  1148. return err;
  1149. }
  1150. /**
  1151. * vc_resize - resize a VT
  1152. * @vc: virtual console
  1153. * @cols: columns
  1154. * @rows: rows
  1155. *
  1156. * Resize a virtual console as seen from the console end of things. We
  1157. * use the common vc_do_resize methods to update the structures. The
  1158. * caller must hold the console sem to protect console internals and
  1159. * vc->port.tty
  1160. */
  1161. int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
  1162. {
  1163. return vc_do_resize(vc->port.tty, vc, cols, rows);
  1164. }
  1165. /**
  1166. * vt_resize - resize a VT
  1167. * @tty: tty to resize
  1168. * @ws: winsize attributes
  1169. *
  1170. * Resize a virtual terminal. This is called by the tty layer as we
  1171. * register our own handler for resizing. The mutual helper does all
  1172. * the actual work.
  1173. *
  1174. * Takes the console sem and the called methods then take the tty
  1175. * termios_rwsem and the tty ctrl_lock in that order.
  1176. */
  1177. static int vt_resize(struct tty_struct *tty, struct winsize *ws)
  1178. {
  1179. struct vc_data *vc = tty->driver_data;
  1180. int ret;
  1181. console_lock();
  1182. ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
  1183. console_unlock();
  1184. return ret;
  1185. }
  1186. struct vc_data *vc_deallocate(unsigned int currcons)
  1187. {
  1188. struct vc_data *vc = NULL;
  1189. WARN_CONSOLE_UNLOCKED();
  1190. if (vc_cons_allocated(currcons)) {
  1191. struct vt_notifier_param param;
  1192. param.vc = vc = vc_cons[currcons].d;
  1193. atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
  1194. vcs_remove_sysfs(currcons);
  1195. visual_deinit(vc);
  1196. con_free_unimap(vc);
  1197. put_pid(vc->vt_pid);
  1198. vc_uniscr_set(vc, NULL);
  1199. kfree(vc->vc_screenbuf);
  1200. vc_cons[currcons].d = NULL;
  1201. }
  1202. return vc;
  1203. }
  1204. /*
  1205. * VT102 emulator
  1206. */
  1207. #define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
  1208. #define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
  1209. #define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
  1210. #define decarm VC_REPEAT
  1211. #define decckm VC_CKMODE
  1212. #define kbdapplic VC_APPLIC
  1213. #define lnm VC_CRLF
  1214. /*
  1215. * this is what the terminal answers to a ESC-Z or csi0c query.
  1216. */
  1217. #define VT100ID "\033[?1;2c"
  1218. #define VT102ID "\033[?6c"
  1219. const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
  1220. 8,12,10,14, 9,13,11,15 };
  1221. /* the default colour table, for VGA+ colour systems */
  1222. unsigned char default_red[] = {
  1223. 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
  1224. 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
  1225. };
  1226. module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
  1227. unsigned char default_grn[] = {
  1228. 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
  1229. 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
  1230. };
  1231. module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
  1232. unsigned char default_blu[] = {
  1233. 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
  1234. 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
  1235. };
  1236. module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
  1237. /*
  1238. * gotoxy() must verify all boundaries, because the arguments
  1239. * might also be negative. If the given position is out of
  1240. * bounds, the cursor is placed at the nearest margin.
  1241. */
  1242. static void gotoxy(struct vc_data *vc, int new_x, int new_y)
  1243. {
  1244. int min_y, max_y;
  1245. if (new_x < 0)
  1246. vc->vc_x = 0;
  1247. else {
  1248. if (new_x >= vc->vc_cols)
  1249. vc->vc_x = vc->vc_cols - 1;
  1250. else
  1251. vc->vc_x = new_x;
  1252. }
  1253. if (vc->vc_decom) {
  1254. min_y = vc->vc_top;
  1255. max_y = vc->vc_bottom;
  1256. } else {
  1257. min_y = 0;
  1258. max_y = vc->vc_rows;
  1259. }
  1260. if (new_y < min_y)
  1261. vc->vc_y = min_y;
  1262. else if (new_y >= max_y)
  1263. vc->vc_y = max_y - 1;
  1264. else
  1265. vc->vc_y = new_y;
  1266. vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
  1267. vc->vc_need_wrap = 0;
  1268. }
  1269. /* for absolute user moves, when decom is set */
  1270. static void gotoxay(struct vc_data *vc, int new_x, int new_y)
  1271. {
  1272. gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
  1273. }
  1274. void scrollback(struct vc_data *vc)
  1275. {
  1276. scrolldelta(-(vc->vc_rows / 2));
  1277. }
  1278. void scrollfront(struct vc_data *vc, int lines)
  1279. {
  1280. if (!lines)
  1281. lines = vc->vc_rows / 2;
  1282. scrolldelta(lines);
  1283. }
  1284. static void lf(struct vc_data *vc)
  1285. {
  1286. /* don't scroll if above bottom of scrolling region, or
  1287. * if below scrolling region
  1288. */
  1289. if (vc->vc_y + 1 == vc->vc_bottom)
  1290. con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1);
  1291. else if (vc->vc_y < vc->vc_rows - 1) {
  1292. vc->vc_y++;
  1293. vc->vc_pos += vc->vc_size_row;
  1294. }
  1295. vc->vc_need_wrap = 0;
  1296. notify_write(vc, '\n');
  1297. }
  1298. static void ri(struct vc_data *vc)
  1299. {
  1300. /* don't scroll if below top of scrolling region, or
  1301. * if above scrolling region
  1302. */
  1303. if (vc->vc_y == vc->vc_top)
  1304. con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1);
  1305. else if (vc->vc_y > 0) {
  1306. vc->vc_y--;
  1307. vc->vc_pos -= vc->vc_size_row;
  1308. }
  1309. vc->vc_need_wrap = 0;
  1310. }
  1311. static inline void cr(struct vc_data *vc)
  1312. {
  1313. vc->vc_pos -= vc->vc_x << 1;
  1314. vc->vc_need_wrap = vc->vc_x = 0;
  1315. notify_write(vc, '\r');
  1316. }
  1317. static inline void bs(struct vc_data *vc)
  1318. {
  1319. if (vc->vc_x) {
  1320. vc->vc_pos -= 2;
  1321. vc->vc_x--;
  1322. vc->vc_need_wrap = 0;
  1323. notify_write(vc, '\b');
  1324. }
  1325. }
  1326. static inline void del(struct vc_data *vc)
  1327. {
  1328. /* ignored */
  1329. }
  1330. static void csi_J(struct vc_data *vc, int vpar)
  1331. {
  1332. unsigned int count;
  1333. unsigned short * start;
  1334. switch (vpar) {
  1335. case 0: /* erase from cursor to end of display */
  1336. vc_uniscr_clear_line(vc, vc->vc_x,
  1337. vc->vc_cols - vc->vc_x);
  1338. vc_uniscr_clear_lines(vc, vc->vc_y + 1,
  1339. vc->vc_rows - vc->vc_y - 1);
  1340. count = (vc->vc_scr_end - vc->vc_pos) >> 1;
  1341. start = (unsigned short *)vc->vc_pos;
  1342. break;
  1343. case 1: /* erase from start to cursor */
  1344. vc_uniscr_clear_line(vc, 0, vc->vc_x + 1);
  1345. vc_uniscr_clear_lines(vc, 0, vc->vc_y);
  1346. count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
  1347. start = (unsigned short *)vc->vc_origin;
  1348. break;
  1349. case 3: /* include scrollback */
  1350. flush_scrollback(vc);
  1351. /* fallthrough */
  1352. case 2: /* erase whole display */
  1353. vc_uniscr_clear_lines(vc, 0, vc->vc_rows);
  1354. count = vc->vc_cols * vc->vc_rows;
  1355. start = (unsigned short *)vc->vc_origin;
  1356. break;
  1357. default:
  1358. return;
  1359. }
  1360. scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
  1361. if (con_should_update(vc))
  1362. do_update_region(vc, (unsigned long) start, count);
  1363. vc->vc_need_wrap = 0;
  1364. }
  1365. static void csi_K(struct vc_data *vc, int vpar)
  1366. {
  1367. unsigned int count;
  1368. unsigned short *start = (unsigned short *)vc->vc_pos;
  1369. int offset;
  1370. switch (vpar) {
  1371. case 0: /* erase from cursor to end of line */
  1372. offset = 0;
  1373. count = vc->vc_cols - vc->vc_x;
  1374. break;
  1375. case 1: /* erase from start of line to cursor */
  1376. offset = -vc->vc_x;
  1377. count = vc->vc_x + 1;
  1378. break;
  1379. case 2: /* erase whole line */
  1380. offset = -vc->vc_x;
  1381. count = vc->vc_cols;
  1382. break;
  1383. default:
  1384. return;
  1385. }
  1386. vc_uniscr_clear_line(vc, vc->vc_x + offset, count);
  1387. scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count);
  1388. vc->vc_need_wrap = 0;
  1389. if (con_should_update(vc))
  1390. do_update_region(vc, (unsigned long)(start + offset), count);
  1391. }
  1392. static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
  1393. { /* not vt100? */
  1394. int count;
  1395. if (!vpar)
  1396. vpar++;
  1397. count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
  1398. vc_uniscr_clear_line(vc, vc->vc_x, count);
  1399. scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
  1400. if (con_should_update(vc))
  1401. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
  1402. vc->vc_need_wrap = 0;
  1403. }
  1404. static void default_attr(struct vc_data *vc)
  1405. {
  1406. vc->vc_intensity = 1;
  1407. vc->vc_italic = 0;
  1408. vc->vc_underline = 0;
  1409. vc->vc_reverse = 0;
  1410. vc->vc_blink = 0;
  1411. vc->vc_color = vc->vc_def_color;
  1412. }
  1413. struct rgb { u8 r; u8 g; u8 b; };
  1414. static void rgb_from_256(int i, struct rgb *c)
  1415. {
  1416. if (i < 8) { /* Standard colours. */
  1417. c->r = i&1 ? 0xaa : 0x00;
  1418. c->g = i&2 ? 0xaa : 0x00;
  1419. c->b = i&4 ? 0xaa : 0x00;
  1420. } else if (i < 16) {
  1421. c->r = i&1 ? 0xff : 0x55;
  1422. c->g = i&2 ? 0xff : 0x55;
  1423. c->b = i&4 ? 0xff : 0x55;
  1424. } else if (i < 232) { /* 6x6x6 colour cube. */
  1425. c->r = (i - 16) / 36 * 85 / 2;
  1426. c->g = (i - 16) / 6 % 6 * 85 / 2;
  1427. c->b = (i - 16) % 6 * 85 / 2;
  1428. } else /* Grayscale ramp. */
  1429. c->r = c->g = c->b = i * 10 - 2312;
  1430. }
  1431. static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
  1432. {
  1433. u8 hue = 0, max = max3(c->r, c->g, c->b);
  1434. if (c->r > max / 2)
  1435. hue |= 4;
  1436. if (c->g > max / 2)
  1437. hue |= 2;
  1438. if (c->b > max / 2)
  1439. hue |= 1;
  1440. if (hue == 7 && max <= 0x55) {
  1441. hue = 0;
  1442. vc->vc_intensity = 2;
  1443. } else if (max > 0xaa)
  1444. vc->vc_intensity = 2;
  1445. else
  1446. vc->vc_intensity = 1;
  1447. vc->vc_color = (vc->vc_color & 0xf0) | hue;
  1448. }
  1449. static void rgb_background(struct vc_data *vc, const struct rgb *c)
  1450. {
  1451. /* For backgrounds, err on the dark side. */
  1452. vc->vc_color = (vc->vc_color & 0x0f)
  1453. | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
  1454. }
  1455. /*
  1456. * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
  1457. * and thus need to be detected and ignored by hand. Strictly speaking, that
  1458. * standard also wants : rather than ; as separators, contrary to ECMA-48, but
  1459. * no one produces such codes and almost no one accepts them.
  1460. *
  1461. * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
  1462. * supporting them.
  1463. */
  1464. static int vc_t416_color(struct vc_data *vc, int i,
  1465. void(*set_color)(struct vc_data *vc, const struct rgb *c))
  1466. {
  1467. struct rgb c;
  1468. i++;
  1469. if (i > vc->vc_npar)
  1470. return i;
  1471. if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
  1472. /* 256 colours */
  1473. i++;
  1474. rgb_from_256(vc->vc_par[i], &c);
  1475. } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
  1476. /* 24 bit */
  1477. c.r = vc->vc_par[i + 1];
  1478. c.g = vc->vc_par[i + 2];
  1479. c.b = vc->vc_par[i + 3];
  1480. i += 3;
  1481. } else
  1482. return i;
  1483. set_color(vc, &c);
  1484. return i;
  1485. }
  1486. /* console_lock is held */
  1487. static void csi_m(struct vc_data *vc)
  1488. {
  1489. int i;
  1490. for (i = 0; i <= vc->vc_npar; i++)
  1491. switch (vc->vc_par[i]) {
  1492. case 0: /* all attributes off */
  1493. default_attr(vc);
  1494. break;
  1495. case 1:
  1496. vc->vc_intensity = 2;
  1497. break;
  1498. case 2:
  1499. vc->vc_intensity = 0;
  1500. break;
  1501. case 3:
  1502. vc->vc_italic = 1;
  1503. break;
  1504. case 21:
  1505. /*
  1506. * No console drivers support double underline, so
  1507. * convert it to a single underline.
  1508. */
  1509. case 4:
  1510. vc->vc_underline = 1;
  1511. break;
  1512. case 5:
  1513. vc->vc_blink = 1;
  1514. break;
  1515. case 7:
  1516. vc->vc_reverse = 1;
  1517. break;
  1518. case 10: /* ANSI X3.64-1979 (SCO-ish?)
  1519. * Select primary font, don't display control chars if
  1520. * defined, don't set bit 8 on output.
  1521. */
  1522. vc->vc_translate = set_translate(vc->vc_charset == 0
  1523. ? vc->vc_G0_charset
  1524. : vc->vc_G1_charset, vc);
  1525. vc->vc_disp_ctrl = 0;
  1526. vc->vc_toggle_meta = 0;
  1527. break;
  1528. case 11: /* ANSI X3.64-1979 (SCO-ish?)
  1529. * Select first alternate font, lets chars < 32 be
  1530. * displayed as ROM chars.
  1531. */
  1532. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1533. vc->vc_disp_ctrl = 1;
  1534. vc->vc_toggle_meta = 0;
  1535. break;
  1536. case 12: /* ANSI X3.64-1979 (SCO-ish?)
  1537. * Select second alternate font, toggle high bit
  1538. * before displaying as ROM char.
  1539. */
  1540. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1541. vc->vc_disp_ctrl = 1;
  1542. vc->vc_toggle_meta = 1;
  1543. break;
  1544. case 22:
  1545. vc->vc_intensity = 1;
  1546. break;
  1547. case 23:
  1548. vc->vc_italic = 0;
  1549. break;
  1550. case 24:
  1551. vc->vc_underline = 0;
  1552. break;
  1553. case 25:
  1554. vc->vc_blink = 0;
  1555. break;
  1556. case 27:
  1557. vc->vc_reverse = 0;
  1558. break;
  1559. case 38:
  1560. i = vc_t416_color(vc, i, rgb_foreground);
  1561. break;
  1562. case 48:
  1563. i = vc_t416_color(vc, i, rgb_background);
  1564. break;
  1565. case 39:
  1566. vc->vc_color = (vc->vc_def_color & 0x0f) |
  1567. (vc->vc_color & 0xf0);
  1568. break;
  1569. case 49:
  1570. vc->vc_color = (vc->vc_def_color & 0xf0) |
  1571. (vc->vc_color & 0x0f);
  1572. break;
  1573. default:
  1574. if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
  1575. if (vc->vc_par[i] < 100)
  1576. vc->vc_intensity = 2;
  1577. vc->vc_par[i] -= 60;
  1578. }
  1579. if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
  1580. vc->vc_color = color_table[vc->vc_par[i] - 30]
  1581. | (vc->vc_color & 0xf0);
  1582. else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
  1583. vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
  1584. | (vc->vc_color & 0x0f);
  1585. break;
  1586. }
  1587. update_attr(vc);
  1588. }
  1589. static void respond_string(const char *p, struct tty_port *port)
  1590. {
  1591. while (*p) {
  1592. tty_insert_flip_char(port, *p, 0);
  1593. p++;
  1594. }
  1595. tty_schedule_flip(port);
  1596. }
  1597. static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
  1598. {
  1599. char buf[40];
  1600. sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
  1601. respond_string(buf, tty->port);
  1602. }
  1603. static inline void status_report(struct tty_struct *tty)
  1604. {
  1605. respond_string("\033[0n", tty->port); /* Terminal ok */
  1606. }
  1607. static inline void respond_ID(struct tty_struct *tty)
  1608. {
  1609. respond_string(VT102ID, tty->port);
  1610. }
  1611. void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
  1612. {
  1613. char buf[8];
  1614. sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
  1615. (char)('!' + mry));
  1616. respond_string(buf, tty->port);
  1617. }
  1618. /* invoked via ioctl(TIOCLINUX) and through set_selection */
  1619. int mouse_reporting(void)
  1620. {
  1621. return vc_cons[fg_console].d->vc_report_mouse;
  1622. }
  1623. /* console_lock is held */
  1624. static void set_mode(struct vc_data *vc, int on_off)
  1625. {
  1626. int i;
  1627. for (i = 0; i <= vc->vc_npar; i++)
  1628. if (vc->vc_ques) {
  1629. switch(vc->vc_par[i]) { /* DEC private modes set/reset */
  1630. case 1: /* Cursor keys send ^[Ox/^[[x */
  1631. if (on_off)
  1632. set_kbd(vc, decckm);
  1633. else
  1634. clr_kbd(vc, decckm);
  1635. break;
  1636. case 3: /* 80/132 mode switch unimplemented */
  1637. #if 0
  1638. vc_resize(deccolm ? 132 : 80, vc->vc_rows);
  1639. /* this alone does not suffice; some user mode
  1640. utility has to change the hardware regs */
  1641. #endif
  1642. break;
  1643. case 5: /* Inverted screen on/off */
  1644. if (vc->vc_decscnm != on_off) {
  1645. vc->vc_decscnm = on_off;
  1646. invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
  1647. update_attr(vc);
  1648. }
  1649. break;
  1650. case 6: /* Origin relative/absolute */
  1651. vc->vc_decom = on_off;
  1652. gotoxay(vc, 0, 0);
  1653. break;
  1654. case 7: /* Autowrap on/off */
  1655. vc->vc_decawm = on_off;
  1656. break;
  1657. case 8: /* Autorepeat on/off */
  1658. if (on_off)
  1659. set_kbd(vc, decarm);
  1660. else
  1661. clr_kbd(vc, decarm);
  1662. break;
  1663. case 9:
  1664. vc->vc_report_mouse = on_off ? 1 : 0;
  1665. break;
  1666. case 25: /* Cursor on/off */
  1667. vc->vc_deccm = on_off;
  1668. break;
  1669. case 1000:
  1670. vc->vc_report_mouse = on_off ? 2 : 0;
  1671. break;
  1672. }
  1673. } else {
  1674. switch(vc->vc_par[i]) { /* ANSI modes set/reset */
  1675. case 3: /* Monitor (display ctrls) */
  1676. vc->vc_disp_ctrl = on_off;
  1677. break;
  1678. case 4: /* Insert Mode on/off */
  1679. vc->vc_decim = on_off;
  1680. break;
  1681. case 20: /* Lf, Enter == CrLf/Lf */
  1682. if (on_off)
  1683. set_kbd(vc, lnm);
  1684. else
  1685. clr_kbd(vc, lnm);
  1686. break;
  1687. }
  1688. }
  1689. }
  1690. /* console_lock is held */
  1691. static void setterm_command(struct vc_data *vc)
  1692. {
  1693. switch(vc->vc_par[0]) {
  1694. case 1: /* set color for underline mode */
  1695. if (vc->vc_can_do_color &&
  1696. vc->vc_par[1] < 16) {
  1697. vc->vc_ulcolor = color_table[vc->vc_par[1]];
  1698. if (vc->vc_underline)
  1699. update_attr(vc);
  1700. }
  1701. break;
  1702. case 2: /* set color for half intensity mode */
  1703. if (vc->vc_can_do_color &&
  1704. vc->vc_par[1] < 16) {
  1705. vc->vc_halfcolor = color_table[vc->vc_par[1]];
  1706. if (vc->vc_intensity == 0)
  1707. update_attr(vc);
  1708. }
  1709. break;
  1710. case 8: /* store colors as defaults */
  1711. vc->vc_def_color = vc->vc_attr;
  1712. if (vc->vc_hi_font_mask == 0x100)
  1713. vc->vc_def_color >>= 1;
  1714. default_attr(vc);
  1715. update_attr(vc);
  1716. break;
  1717. case 9: /* set blanking interval */
  1718. blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
  1719. poke_blanked_console();
  1720. break;
  1721. case 10: /* set bell frequency in Hz */
  1722. if (vc->vc_npar >= 1)
  1723. vc->vc_bell_pitch = vc->vc_par[1];
  1724. else
  1725. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1726. break;
  1727. case 11: /* set bell duration in msec */
  1728. if (vc->vc_npar >= 1)
  1729. vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
  1730. msecs_to_jiffies(vc->vc_par[1]) : 0;
  1731. else
  1732. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1733. break;
  1734. case 12: /* bring specified console to the front */
  1735. if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
  1736. set_console(vc->vc_par[1] - 1);
  1737. break;
  1738. case 13: /* unblank the screen */
  1739. poke_blanked_console();
  1740. break;
  1741. case 14: /* set vesa powerdown interval */
  1742. vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
  1743. break;
  1744. case 15: /* activate the previous console */
  1745. set_console(last_console);
  1746. break;
  1747. case 16: /* set cursor blink duration in msec */
  1748. if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
  1749. vc->vc_par[1] <= USHRT_MAX)
  1750. vc->vc_cur_blink_ms = vc->vc_par[1];
  1751. else
  1752. vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
  1753. break;
  1754. }
  1755. }
  1756. /* console_lock is held */
  1757. static void csi_at(struct vc_data *vc, unsigned int nr)
  1758. {
  1759. if (nr > vc->vc_cols - vc->vc_x)
  1760. nr = vc->vc_cols - vc->vc_x;
  1761. else if (!nr)
  1762. nr = 1;
  1763. insert_char(vc, nr);
  1764. }
  1765. /* console_lock is held */
  1766. static void csi_L(struct vc_data *vc, unsigned int nr)
  1767. {
  1768. if (nr > vc->vc_rows - vc->vc_y)
  1769. nr = vc->vc_rows - vc->vc_y;
  1770. else if (!nr)
  1771. nr = 1;
  1772. con_scroll(vc, vc->vc_y, vc->vc_bottom, SM_DOWN, nr);
  1773. vc->vc_need_wrap = 0;
  1774. }
  1775. /* console_lock is held */
  1776. static void csi_P(struct vc_data *vc, unsigned int nr)
  1777. {
  1778. if (nr > vc->vc_cols - vc->vc_x)
  1779. nr = vc->vc_cols - vc->vc_x;
  1780. else if (!nr)
  1781. nr = 1;
  1782. delete_char(vc, nr);
  1783. }
  1784. /* console_lock is held */
  1785. static void csi_M(struct vc_data *vc, unsigned int nr)
  1786. {
  1787. if (nr > vc->vc_rows - vc->vc_y)
  1788. nr = vc->vc_rows - vc->vc_y;
  1789. else if (!nr)
  1790. nr=1;
  1791. con_scroll(vc, vc->vc_y, vc->vc_bottom, SM_UP, nr);
  1792. vc->vc_need_wrap = 0;
  1793. }
  1794. /* console_lock is held (except via vc_init->reset_terminal */
  1795. static void save_cur(struct vc_data *vc)
  1796. {
  1797. vc->vc_saved_x = vc->vc_x;
  1798. vc->vc_saved_y = vc->vc_y;
  1799. vc->vc_s_intensity = vc->vc_intensity;
  1800. vc->vc_s_italic = vc->vc_italic;
  1801. vc->vc_s_underline = vc->vc_underline;
  1802. vc->vc_s_blink = vc->vc_blink;
  1803. vc->vc_s_reverse = vc->vc_reverse;
  1804. vc->vc_s_charset = vc->vc_charset;
  1805. vc->vc_s_color = vc->vc_color;
  1806. vc->vc_saved_G0 = vc->vc_G0_charset;
  1807. vc->vc_saved_G1 = vc->vc_G1_charset;
  1808. }
  1809. /* console_lock is held */
  1810. static void restore_cur(struct vc_data *vc)
  1811. {
  1812. gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
  1813. vc->vc_intensity = vc->vc_s_intensity;
  1814. vc->vc_italic = vc->vc_s_italic;
  1815. vc->vc_underline = vc->vc_s_underline;
  1816. vc->vc_blink = vc->vc_s_blink;
  1817. vc->vc_reverse = vc->vc_s_reverse;
  1818. vc->vc_charset = vc->vc_s_charset;
  1819. vc->vc_color = vc->vc_s_color;
  1820. vc->vc_G0_charset = vc->vc_saved_G0;
  1821. vc->vc_G1_charset = vc->vc_saved_G1;
  1822. vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
  1823. update_attr(vc);
  1824. vc->vc_need_wrap = 0;
  1825. }
  1826. enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
  1827. EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
  1828. ESpalette, ESosc };
  1829. /* console_lock is held (except via vc_init()) */
  1830. static void reset_terminal(struct vc_data *vc, int do_clear)
  1831. {
  1832. vc->vc_top = 0;
  1833. vc->vc_bottom = vc->vc_rows;
  1834. vc->vc_state = ESnormal;
  1835. vc->vc_ques = 0;
  1836. vc->vc_translate = set_translate(LAT1_MAP, vc);
  1837. vc->vc_G0_charset = LAT1_MAP;
  1838. vc->vc_G1_charset = GRAF_MAP;
  1839. vc->vc_charset = 0;
  1840. vc->vc_need_wrap = 0;
  1841. vc->vc_report_mouse = 0;
  1842. vc->vc_utf = default_utf8;
  1843. vc->vc_utf_count = 0;
  1844. vc->vc_disp_ctrl = 0;
  1845. vc->vc_toggle_meta = 0;
  1846. vc->vc_decscnm = 0;
  1847. vc->vc_decom = 0;
  1848. vc->vc_decawm = 1;
  1849. vc->vc_deccm = global_cursor_default;
  1850. vc->vc_decim = 0;
  1851. vt_reset_keyboard(vc->vc_num);
  1852. vc->vc_cursor_type = cur_default;
  1853. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1854. default_attr(vc);
  1855. update_attr(vc);
  1856. vc->vc_tab_stop[0] =
  1857. vc->vc_tab_stop[1] =
  1858. vc->vc_tab_stop[2] =
  1859. vc->vc_tab_stop[3] =
  1860. vc->vc_tab_stop[4] =
  1861. vc->vc_tab_stop[5] =
  1862. vc->vc_tab_stop[6] =
  1863. vc->vc_tab_stop[7] = 0x01010101;
  1864. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1865. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1866. vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
  1867. gotoxy(vc, 0, 0);
  1868. save_cur(vc);
  1869. if (do_clear)
  1870. csi_J(vc, 2);
  1871. }
  1872. /* console_lock is held */
  1873. static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
  1874. {
  1875. /*
  1876. * Control characters can be used in the _middle_
  1877. * of an escape sequence.
  1878. */
  1879. if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
  1880. return;
  1881. switch (c) {
  1882. case 0:
  1883. return;
  1884. case 7:
  1885. if (vc->vc_state == ESosc)
  1886. vc->vc_state = ESnormal;
  1887. else if (vc->vc_bell_duration)
  1888. kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
  1889. return;
  1890. case 8:
  1891. bs(vc);
  1892. return;
  1893. case 9:
  1894. vc->vc_pos -= (vc->vc_x << 1);
  1895. while (vc->vc_x < vc->vc_cols - 1) {
  1896. vc->vc_x++;
  1897. if (vc->vc_tab_stop[7 & (vc->vc_x >> 5)] & (1 << (vc->vc_x & 31)))
  1898. break;
  1899. }
  1900. vc->vc_pos += (vc->vc_x << 1);
  1901. notify_write(vc, '\t');
  1902. return;
  1903. case 10: case 11: case 12:
  1904. lf(vc);
  1905. if (!is_kbd(vc, lnm))
  1906. return;
  1907. case 13:
  1908. cr(vc);
  1909. return;
  1910. case 14:
  1911. vc->vc_charset = 1;
  1912. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1913. vc->vc_disp_ctrl = 1;
  1914. return;
  1915. case 15:
  1916. vc->vc_charset = 0;
  1917. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1918. vc->vc_disp_ctrl = 0;
  1919. return;
  1920. case 24: case 26:
  1921. vc->vc_state = ESnormal;
  1922. return;
  1923. case 27:
  1924. vc->vc_state = ESesc;
  1925. return;
  1926. case 127:
  1927. del(vc);
  1928. return;
  1929. case 128+27:
  1930. vc->vc_state = ESsquare;
  1931. return;
  1932. }
  1933. switch(vc->vc_state) {
  1934. case ESesc:
  1935. vc->vc_state = ESnormal;
  1936. switch (c) {
  1937. case '[':
  1938. vc->vc_state = ESsquare;
  1939. return;
  1940. case ']':
  1941. vc->vc_state = ESnonstd;
  1942. return;
  1943. case '%':
  1944. vc->vc_state = ESpercent;
  1945. return;
  1946. case 'E':
  1947. cr(vc);
  1948. lf(vc);
  1949. return;
  1950. case 'M':
  1951. ri(vc);
  1952. return;
  1953. case 'D':
  1954. lf(vc);
  1955. return;
  1956. case 'H':
  1957. vc->vc_tab_stop[7 & (vc->vc_x >> 5)] |= (1 << (vc->vc_x & 31));
  1958. return;
  1959. case 'Z':
  1960. respond_ID(tty);
  1961. return;
  1962. case '7':
  1963. save_cur(vc);
  1964. return;
  1965. case '8':
  1966. restore_cur(vc);
  1967. return;
  1968. case '(':
  1969. vc->vc_state = ESsetG0;
  1970. return;
  1971. case ')':
  1972. vc->vc_state = ESsetG1;
  1973. return;
  1974. case '#':
  1975. vc->vc_state = EShash;
  1976. return;
  1977. case 'c':
  1978. reset_terminal(vc, 1);
  1979. return;
  1980. case '>': /* Numeric keypad */
  1981. clr_kbd(vc, kbdapplic);
  1982. return;
  1983. case '=': /* Appl. keypad */
  1984. set_kbd(vc, kbdapplic);
  1985. return;
  1986. }
  1987. return;
  1988. case ESnonstd:
  1989. if (c=='P') { /* palette escape sequence */
  1990. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1991. vc->vc_par[vc->vc_npar] = 0;
  1992. vc->vc_npar = 0;
  1993. vc->vc_state = ESpalette;
  1994. return;
  1995. } else if (c=='R') { /* reset palette */
  1996. reset_palette(vc);
  1997. vc->vc_state = ESnormal;
  1998. } else if (c>='0' && c<='9')
  1999. vc->vc_state = ESosc;
  2000. else
  2001. vc->vc_state = ESnormal;
  2002. return;
  2003. case ESpalette:
  2004. if (isxdigit(c)) {
  2005. vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
  2006. if (vc->vc_npar == 7) {
  2007. int i = vc->vc_par[0] * 3, j = 1;
  2008. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  2009. vc->vc_palette[i++] += vc->vc_par[j++];
  2010. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  2011. vc->vc_palette[i++] += vc->vc_par[j++];
  2012. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  2013. vc->vc_palette[i] += vc->vc_par[j];
  2014. set_palette(vc);
  2015. vc->vc_state = ESnormal;
  2016. }
  2017. } else
  2018. vc->vc_state = ESnormal;
  2019. return;
  2020. case ESsquare:
  2021. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  2022. vc->vc_par[vc->vc_npar] = 0;
  2023. vc->vc_npar = 0;
  2024. vc->vc_state = ESgetpars;
  2025. if (c == '[') { /* Function key */
  2026. vc->vc_state=ESfunckey;
  2027. return;
  2028. }
  2029. vc->vc_ques = (c == '?');
  2030. if (vc->vc_ques)
  2031. return;
  2032. case ESgetpars:
  2033. if (c == ';' && vc->vc_npar < NPAR - 1) {
  2034. vc->vc_npar++;
  2035. return;
  2036. } else if (c>='0' && c<='9') {
  2037. vc->vc_par[vc->vc_npar] *= 10;
  2038. vc->vc_par[vc->vc_npar] += c - '0';
  2039. return;
  2040. }
  2041. vc->vc_state = ESnormal;
  2042. switch(c) {
  2043. case 'h':
  2044. set_mode(vc, 1);
  2045. return;
  2046. case 'l':
  2047. set_mode(vc, 0);
  2048. return;
  2049. case 'c':
  2050. if (vc->vc_ques) {
  2051. if (vc->vc_par[0])
  2052. vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
  2053. else
  2054. vc->vc_cursor_type = cur_default;
  2055. return;
  2056. }
  2057. break;
  2058. case 'm':
  2059. if (vc->vc_ques) {
  2060. clear_selection();
  2061. if (vc->vc_par[0])
  2062. vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
  2063. else
  2064. vc->vc_complement_mask = vc->vc_s_complement_mask;
  2065. return;
  2066. }
  2067. break;
  2068. case 'n':
  2069. if (!vc->vc_ques) {
  2070. if (vc->vc_par[0] == 5)
  2071. status_report(tty);
  2072. else if (vc->vc_par[0] == 6)
  2073. cursor_report(vc, tty);
  2074. }
  2075. return;
  2076. }
  2077. if (vc->vc_ques) {
  2078. vc->vc_ques = 0;
  2079. return;
  2080. }
  2081. switch(c) {
  2082. case 'G': case '`':
  2083. if (vc->vc_par[0])
  2084. vc->vc_par[0]--;
  2085. gotoxy(vc, vc->vc_par[0], vc->vc_y);
  2086. return;
  2087. case 'A':
  2088. if (!vc->vc_par[0])
  2089. vc->vc_par[0]++;
  2090. gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
  2091. return;
  2092. case 'B': case 'e':
  2093. if (!vc->vc_par[0])
  2094. vc->vc_par[0]++;
  2095. gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
  2096. return;
  2097. case 'C': case 'a':
  2098. if (!vc->vc_par[0])
  2099. vc->vc_par[0]++;
  2100. gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
  2101. return;
  2102. case 'D':
  2103. if (!vc->vc_par[0])
  2104. vc->vc_par[0]++;
  2105. gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
  2106. return;
  2107. case 'E':
  2108. if (!vc->vc_par[0])
  2109. vc->vc_par[0]++;
  2110. gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
  2111. return;
  2112. case 'F':
  2113. if (!vc->vc_par[0])
  2114. vc->vc_par[0]++;
  2115. gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
  2116. return;
  2117. case 'd':
  2118. if (vc->vc_par[0])
  2119. vc->vc_par[0]--;
  2120. gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
  2121. return;
  2122. case 'H': case 'f':
  2123. if (vc->vc_par[0])
  2124. vc->vc_par[0]--;
  2125. if (vc->vc_par[1])
  2126. vc->vc_par[1]--;
  2127. gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
  2128. return;
  2129. case 'J':
  2130. csi_J(vc, vc->vc_par[0]);
  2131. return;
  2132. case 'K':
  2133. csi_K(vc, vc->vc_par[0]);
  2134. return;
  2135. case 'L':
  2136. csi_L(vc, vc->vc_par[0]);
  2137. return;
  2138. case 'M':
  2139. csi_M(vc, vc->vc_par[0]);
  2140. return;
  2141. case 'P':
  2142. csi_P(vc, vc->vc_par[0]);
  2143. return;
  2144. case 'c':
  2145. if (!vc->vc_par[0])
  2146. respond_ID(tty);
  2147. return;
  2148. case 'g':
  2149. if (!vc->vc_par[0])
  2150. vc->vc_tab_stop[7 & (vc->vc_x >> 5)] &= ~(1 << (vc->vc_x & 31));
  2151. else if (vc->vc_par[0] == 3) {
  2152. vc->vc_tab_stop[0] =
  2153. vc->vc_tab_stop[1] =
  2154. vc->vc_tab_stop[2] =
  2155. vc->vc_tab_stop[3] =
  2156. vc->vc_tab_stop[4] =
  2157. vc->vc_tab_stop[5] =
  2158. vc->vc_tab_stop[6] =
  2159. vc->vc_tab_stop[7] = 0;
  2160. }
  2161. return;
  2162. case 'm':
  2163. csi_m(vc);
  2164. return;
  2165. case 'q': /* DECLL - but only 3 leds */
  2166. /* map 0,1,2,3 to 0,1,2,4 */
  2167. if (vc->vc_par[0] < 4)
  2168. vt_set_led_state(vc->vc_num,
  2169. (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
  2170. return;
  2171. case 'r':
  2172. if (!vc->vc_par[0])
  2173. vc->vc_par[0]++;
  2174. if (!vc->vc_par[1])
  2175. vc->vc_par[1] = vc->vc_rows;
  2176. /* Minimum allowed region is 2 lines */
  2177. if (vc->vc_par[0] < vc->vc_par[1] &&
  2178. vc->vc_par[1] <= vc->vc_rows) {
  2179. vc->vc_top = vc->vc_par[0] - 1;
  2180. vc->vc_bottom = vc->vc_par[1];
  2181. gotoxay(vc, 0, 0);
  2182. }
  2183. return;
  2184. case 's':
  2185. save_cur(vc);
  2186. return;
  2187. case 'u':
  2188. restore_cur(vc);
  2189. return;
  2190. case 'X':
  2191. csi_X(vc, vc->vc_par[0]);
  2192. return;
  2193. case '@':
  2194. csi_at(vc, vc->vc_par[0]);
  2195. return;
  2196. case ']': /* setterm functions */
  2197. setterm_command(vc);
  2198. return;
  2199. }
  2200. return;
  2201. case ESpercent:
  2202. vc->vc_state = ESnormal;
  2203. switch (c) {
  2204. case '@': /* defined in ISO 2022 */
  2205. vc->vc_utf = 0;
  2206. return;
  2207. case 'G': /* prelim official escape code */
  2208. case '8': /* retained for compatibility */
  2209. vc->vc_utf = 1;
  2210. return;
  2211. }
  2212. return;
  2213. case ESfunckey:
  2214. vc->vc_state = ESnormal;
  2215. return;
  2216. case EShash:
  2217. vc->vc_state = ESnormal;
  2218. if (c == '8') {
  2219. /* DEC screen alignment test. kludge :-) */
  2220. vc->vc_video_erase_char =
  2221. (vc->vc_video_erase_char & 0xff00) | 'E';
  2222. csi_J(vc, 2);
  2223. vc->vc_video_erase_char =
  2224. (vc->vc_video_erase_char & 0xff00) | ' ';
  2225. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  2226. }
  2227. return;
  2228. case ESsetG0:
  2229. if (c == '0')
  2230. vc->vc_G0_charset = GRAF_MAP;
  2231. else if (c == 'B')
  2232. vc->vc_G0_charset = LAT1_MAP;
  2233. else if (c == 'U')
  2234. vc->vc_G0_charset = IBMPC_MAP;
  2235. else if (c == 'K')
  2236. vc->vc_G0_charset = USER_MAP;
  2237. if (vc->vc_charset == 0)
  2238. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  2239. vc->vc_state = ESnormal;
  2240. return;
  2241. case ESsetG1:
  2242. if (c == '0')
  2243. vc->vc_G1_charset = GRAF_MAP;
  2244. else if (c == 'B')
  2245. vc->vc_G1_charset = LAT1_MAP;
  2246. else if (c == 'U')
  2247. vc->vc_G1_charset = IBMPC_MAP;
  2248. else if (c == 'K')
  2249. vc->vc_G1_charset = USER_MAP;
  2250. if (vc->vc_charset == 1)
  2251. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  2252. vc->vc_state = ESnormal;
  2253. return;
  2254. case ESosc:
  2255. return;
  2256. default:
  2257. vc->vc_state = ESnormal;
  2258. }
  2259. }
  2260. /* is_double_width() is based on the wcwidth() implementation by
  2261. * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
  2262. * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
  2263. */
  2264. struct interval {
  2265. uint32_t first;
  2266. uint32_t last;
  2267. };
  2268. static int ucs_cmp(const void *key, const void *elt)
  2269. {
  2270. uint32_t ucs = *(uint32_t *)key;
  2271. struct interval e = *(struct interval *) elt;
  2272. if (ucs > e.last)
  2273. return 1;
  2274. else if (ucs < e.first)
  2275. return -1;
  2276. return 0;
  2277. }
  2278. static int is_double_width(uint32_t ucs)
  2279. {
  2280. static const struct interval double_width[] = {
  2281. { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
  2282. { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
  2283. { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
  2284. { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
  2285. };
  2286. if (ucs < double_width[0].first ||
  2287. ucs > double_width[ARRAY_SIZE(double_width) - 1].last)
  2288. return 0;
  2289. return bsearch(&ucs, double_width, ARRAY_SIZE(double_width),
  2290. sizeof(struct interval), ucs_cmp) != NULL;
  2291. }
  2292. static void con_flush(struct vc_data *vc, unsigned long draw_from,
  2293. unsigned long draw_to, int *draw_x)
  2294. {
  2295. if (*draw_x < 0)
  2296. return;
  2297. vc->vc_sw->con_putcs(vc, (u16 *)draw_from,
  2298. (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, *draw_x);
  2299. *draw_x = -1;
  2300. }
  2301. /* acquires console_lock */
  2302. static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  2303. {
  2304. int c, next_c, tc, ok, n = 0, draw_x = -1;
  2305. unsigned int currcons;
  2306. unsigned long draw_from = 0, draw_to = 0;
  2307. struct vc_data *vc;
  2308. unsigned char vc_attr;
  2309. struct vt_notifier_param param;
  2310. uint8_t rescan;
  2311. uint8_t inverse;
  2312. uint8_t width;
  2313. u16 himask, charmask;
  2314. if (in_interrupt())
  2315. return count;
  2316. might_sleep();
  2317. console_lock();
  2318. vc = tty->driver_data;
  2319. if (vc == NULL) {
  2320. pr_err("vt: argh, driver_data is NULL !\n");
  2321. console_unlock();
  2322. return 0;
  2323. }
  2324. currcons = vc->vc_num;
  2325. if (!vc_cons_allocated(currcons)) {
  2326. /* could this happen? */
  2327. pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
  2328. console_unlock();
  2329. return 0;
  2330. }
  2331. himask = vc->vc_hi_font_mask;
  2332. charmask = himask ? 0x1ff : 0xff;
  2333. /* undraw cursor first */
  2334. if (con_is_fg(vc))
  2335. hide_cursor(vc);
  2336. param.vc = vc;
  2337. while (!tty->stopped && count) {
  2338. int orig = *buf;
  2339. c = orig;
  2340. buf++;
  2341. n++;
  2342. count--;
  2343. rescan = 0;
  2344. inverse = 0;
  2345. width = 1;
  2346. /* Do no translation at all in control states */
  2347. if (vc->vc_state != ESnormal) {
  2348. tc = c;
  2349. } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
  2350. /* Combine UTF-8 into Unicode in vc_utf_char.
  2351. * vc_utf_count is the number of continuation bytes still
  2352. * expected to arrive.
  2353. * vc_npar is the number of continuation bytes arrived so
  2354. * far
  2355. */
  2356. rescan_last_byte:
  2357. if ((c & 0xc0) == 0x80) {
  2358. /* Continuation byte received */
  2359. static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
  2360. if (vc->vc_utf_count) {
  2361. vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
  2362. vc->vc_npar++;
  2363. if (--vc->vc_utf_count) {
  2364. /* Still need some bytes */
  2365. continue;
  2366. }
  2367. /* Got a whole character */
  2368. c = vc->vc_utf_char;
  2369. /* Reject overlong sequences */
  2370. if (c <= utf8_length_changes[vc->vc_npar - 1] ||
  2371. c > utf8_length_changes[vc->vc_npar])
  2372. c = 0xfffd;
  2373. } else {
  2374. /* Unexpected continuation byte */
  2375. vc->vc_utf_count = 0;
  2376. c = 0xfffd;
  2377. }
  2378. } else {
  2379. /* Single ASCII byte or first byte of a sequence received */
  2380. if (vc->vc_utf_count) {
  2381. /* Continuation byte expected */
  2382. rescan = 1;
  2383. vc->vc_utf_count = 0;
  2384. c = 0xfffd;
  2385. } else if (c > 0x7f) {
  2386. /* First byte of a multibyte sequence received */
  2387. vc->vc_npar = 0;
  2388. if ((c & 0xe0) == 0xc0) {
  2389. vc->vc_utf_count = 1;
  2390. vc->vc_utf_char = (c & 0x1f);
  2391. } else if ((c & 0xf0) == 0xe0) {
  2392. vc->vc_utf_count = 2;
  2393. vc->vc_utf_char = (c & 0x0f);
  2394. } else if ((c & 0xf8) == 0xf0) {
  2395. vc->vc_utf_count = 3;
  2396. vc->vc_utf_char = (c & 0x07);
  2397. } else if ((c & 0xfc) == 0xf8) {
  2398. vc->vc_utf_count = 4;
  2399. vc->vc_utf_char = (c & 0x03);
  2400. } else if ((c & 0xfe) == 0xfc) {
  2401. vc->vc_utf_count = 5;
  2402. vc->vc_utf_char = (c & 0x01);
  2403. } else {
  2404. /* 254 and 255 are invalid */
  2405. c = 0xfffd;
  2406. }
  2407. if (vc->vc_utf_count) {
  2408. /* Still need some bytes */
  2409. continue;
  2410. }
  2411. }
  2412. /* Nothing to do if an ASCII byte was received */
  2413. }
  2414. /* End of UTF-8 decoding. */
  2415. /* c is the received character, or U+FFFD for invalid sequences. */
  2416. /* Replace invalid Unicode code points with U+FFFD too */
  2417. if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
  2418. c = 0xfffd;
  2419. tc = c;
  2420. } else { /* no utf or alternate charset mode */
  2421. tc = vc_translate(vc, c);
  2422. }
  2423. param.c = tc;
  2424. if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
  2425. &param) == NOTIFY_STOP)
  2426. continue;
  2427. /* If the original code was a control character we
  2428. * only allow a glyph to be displayed if the code is
  2429. * not normally used (such as for cursor movement) or
  2430. * if the disp_ctrl mode has been explicitly enabled.
  2431. * Certain characters (as given by the CTRL_ALWAYS
  2432. * bitmap) are always displayed as control characters,
  2433. * as the console would be pretty useless without
  2434. * them; to display an arbitrary font position use the
  2435. * direct-to-font zone in UTF-8 mode.
  2436. */
  2437. ok = tc && (c >= 32 ||
  2438. !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
  2439. vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
  2440. && (c != 127 || vc->vc_disp_ctrl)
  2441. && (c != 128+27);
  2442. if (vc->vc_state == ESnormal && ok) {
  2443. if (vc->vc_utf && !vc->vc_disp_ctrl) {
  2444. if (is_double_width(c))
  2445. width = 2;
  2446. }
  2447. /* Now try to find out how to display it */
  2448. tc = conv_uni_to_pc(vc, tc);
  2449. if (tc & ~charmask) {
  2450. if (tc == -1 || tc == -2) {
  2451. continue; /* nothing to display */
  2452. }
  2453. /* Glyph not found */
  2454. if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
  2455. /* In legacy mode use the glyph we get by a 1:1 mapping.
  2456. This would make absolutely no sense with Unicode in mind,
  2457. but do this for ASCII characters since a font may lack
  2458. Unicode mapping info and we don't want to end up with
  2459. having question marks only. */
  2460. tc = c;
  2461. } else {
  2462. /* Display U+FFFD. If it's not found, display an inverse question mark. */
  2463. tc = conv_uni_to_pc(vc, 0xfffd);
  2464. if (tc < 0) {
  2465. inverse = 1;
  2466. tc = conv_uni_to_pc(vc, '?');
  2467. if (tc < 0) tc = '?';
  2468. }
  2469. }
  2470. }
  2471. if (!inverse) {
  2472. vc_attr = vc->vc_attr;
  2473. } else {
  2474. /* invert vc_attr */
  2475. if (!vc->vc_can_do_color) {
  2476. vc_attr = (vc->vc_attr) ^ 0x08;
  2477. } else if (vc->vc_hi_font_mask == 0x100) {
  2478. vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
  2479. } else {
  2480. vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
  2481. }
  2482. con_flush(vc, draw_from, draw_to, &draw_x);
  2483. }
  2484. next_c = c;
  2485. while (1) {
  2486. if (vc->vc_need_wrap || vc->vc_decim)
  2487. con_flush(vc, draw_from, draw_to,
  2488. &draw_x);
  2489. if (vc->vc_need_wrap) {
  2490. cr(vc);
  2491. lf(vc);
  2492. }
  2493. if (vc->vc_decim)
  2494. insert_char(vc, 1);
  2495. vc_uniscr_putc(vc, next_c);
  2496. scr_writew(himask ?
  2497. ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
  2498. (vc_attr << 8) + tc,
  2499. (u16 *) vc->vc_pos);
  2500. if (con_should_update(vc) && draw_x < 0) {
  2501. draw_x = vc->vc_x;
  2502. draw_from = vc->vc_pos;
  2503. }
  2504. if (vc->vc_x == vc->vc_cols - 1) {
  2505. vc->vc_need_wrap = vc->vc_decawm;
  2506. draw_to = vc->vc_pos + 2;
  2507. } else {
  2508. vc->vc_x++;
  2509. draw_to = (vc->vc_pos += 2);
  2510. }
  2511. if (!--width) break;
  2512. tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
  2513. if (tc < 0) tc = ' ';
  2514. next_c = ' ';
  2515. }
  2516. notify_write(vc, c);
  2517. if (inverse)
  2518. con_flush(vc, draw_from, draw_to, &draw_x);
  2519. if (rescan) {
  2520. rescan = 0;
  2521. inverse = 0;
  2522. width = 1;
  2523. c = orig;
  2524. goto rescan_last_byte;
  2525. }
  2526. continue;
  2527. }
  2528. con_flush(vc, draw_from, draw_to, &draw_x);
  2529. do_con_trol(tty, vc, orig);
  2530. }
  2531. con_flush(vc, draw_from, draw_to, &draw_x);
  2532. vc_uniscr_debug_check(vc);
  2533. console_conditional_schedule();
  2534. notify_update(vc);
  2535. console_unlock();
  2536. return n;
  2537. }
  2538. /*
  2539. * This is the console switching callback.
  2540. *
  2541. * Doing console switching in a process context allows
  2542. * us to do the switches asynchronously (needed when we want
  2543. * to switch due to a keyboard interrupt). Synchronization
  2544. * with other console code and prevention of re-entrancy is
  2545. * ensured with console_lock.
  2546. */
  2547. static void console_callback(struct work_struct *ignored)
  2548. {
  2549. console_lock();
  2550. if (want_console >= 0) {
  2551. if (want_console != fg_console &&
  2552. vc_cons_allocated(want_console)) {
  2553. hide_cursor(vc_cons[fg_console].d);
  2554. change_console(vc_cons[want_console].d);
  2555. /* we only changed when the console had already
  2556. been allocated - a new console is not created
  2557. in an interrupt routine */
  2558. }
  2559. want_console = -1;
  2560. }
  2561. if (do_poke_blanked_console) { /* do not unblank for a LED change */
  2562. do_poke_blanked_console = 0;
  2563. poke_blanked_console();
  2564. }
  2565. if (scrollback_delta) {
  2566. struct vc_data *vc = vc_cons[fg_console].d;
  2567. clear_selection();
  2568. if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
  2569. vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
  2570. scrollback_delta = 0;
  2571. }
  2572. if (blank_timer_expired) {
  2573. do_blank_screen(0);
  2574. blank_timer_expired = 0;
  2575. }
  2576. notify_update(vc_cons[fg_console].d);
  2577. console_unlock();
  2578. }
  2579. int set_console(int nr)
  2580. {
  2581. struct vc_data *vc = vc_cons[fg_console].d;
  2582. if (!vc_cons_allocated(nr) || vt_dont_switch ||
  2583. (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
  2584. /*
  2585. * Console switch will fail in console_callback() or
  2586. * change_console() so there is no point scheduling
  2587. * the callback
  2588. *
  2589. * Existing set_console() users don't check the return
  2590. * value so this shouldn't break anything
  2591. */
  2592. return -EINVAL;
  2593. }
  2594. want_console = nr;
  2595. schedule_console_callback();
  2596. return 0;
  2597. }
  2598. struct tty_driver *console_driver;
  2599. #ifdef CONFIG_VT_CONSOLE
  2600. /**
  2601. * vt_kmsg_redirect() - Sets/gets the kernel message console
  2602. * @new: The new virtual terminal number or -1 if the console should stay
  2603. * unchanged
  2604. *
  2605. * By default, the kernel messages are always printed on the current virtual
  2606. * console. However, the user may modify that default with the
  2607. * TIOCL_SETKMSGREDIRECT ioctl call.
  2608. *
  2609. * This function sets the kernel message console to be @new. It returns the old
  2610. * virtual console number. The virtual terminal number 0 (both as parameter and
  2611. * return value) means no redirection (i.e. always printed on the currently
  2612. * active console).
  2613. *
  2614. * The parameter -1 means that only the current console is returned, but the
  2615. * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
  2616. * case to make the code more understandable.
  2617. *
  2618. * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
  2619. * the parameter and always returns 0.
  2620. */
  2621. int vt_kmsg_redirect(int new)
  2622. {
  2623. static int kmsg_con;
  2624. if (new != -1)
  2625. return xchg(&kmsg_con, new);
  2626. else
  2627. return kmsg_con;
  2628. }
  2629. /*
  2630. * Console on virtual terminal
  2631. *
  2632. * The console must be locked when we get here.
  2633. */
  2634. static void vt_console_print(struct console *co, const char *b, unsigned count)
  2635. {
  2636. struct vc_data *vc = vc_cons[fg_console].d;
  2637. unsigned char c;
  2638. static DEFINE_SPINLOCK(printing_lock);
  2639. const ushort *start;
  2640. ushort start_x, cnt;
  2641. int kmsg_console;
  2642. /* console busy or not yet initialized */
  2643. if (!printable)
  2644. return;
  2645. if (!spin_trylock(&printing_lock))
  2646. return;
  2647. kmsg_console = vt_get_kmsg_redirect();
  2648. if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
  2649. vc = vc_cons[kmsg_console - 1].d;
  2650. if (!vc_cons_allocated(fg_console)) {
  2651. /* impossible */
  2652. /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
  2653. goto quit;
  2654. }
  2655. if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
  2656. goto quit;
  2657. /* undraw cursor first */
  2658. if (con_is_fg(vc))
  2659. hide_cursor(vc);
  2660. start = (ushort *)vc->vc_pos;
  2661. start_x = vc->vc_x;
  2662. cnt = 0;
  2663. while (count--) {
  2664. c = *b++;
  2665. if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
  2666. if (cnt && con_is_visible(vc))
  2667. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, start_x);
  2668. cnt = 0;
  2669. if (c == 8) { /* backspace */
  2670. bs(vc);
  2671. start = (ushort *)vc->vc_pos;
  2672. start_x = vc->vc_x;
  2673. continue;
  2674. }
  2675. if (c != 13)
  2676. lf(vc);
  2677. cr(vc);
  2678. start = (ushort *)vc->vc_pos;
  2679. start_x = vc->vc_x;
  2680. if (c == 10 || c == 13)
  2681. continue;
  2682. }
  2683. vc_uniscr_putc(vc, c);
  2684. scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
  2685. notify_write(vc, c);
  2686. cnt++;
  2687. if (vc->vc_x == vc->vc_cols - 1) {
  2688. vc->vc_need_wrap = 1;
  2689. } else {
  2690. vc->vc_pos += 2;
  2691. vc->vc_x++;
  2692. }
  2693. }
  2694. if (cnt && con_is_visible(vc))
  2695. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, start_x);
  2696. set_cursor(vc);
  2697. notify_update(vc);
  2698. quit:
  2699. spin_unlock(&printing_lock);
  2700. }
  2701. static struct tty_driver *vt_console_device(struct console *c, int *index)
  2702. {
  2703. *index = c->index ? c->index-1 : fg_console;
  2704. return console_driver;
  2705. }
  2706. static struct console vt_console_driver = {
  2707. .name = "tty",
  2708. .write = vt_console_print,
  2709. .device = vt_console_device,
  2710. .unblank = unblank_screen,
  2711. .flags = CON_PRINTBUFFER,
  2712. .index = -1,
  2713. };
  2714. #endif
  2715. /*
  2716. * Handling of Linux-specific VC ioctls
  2717. */
  2718. /*
  2719. * Generally a bit racy with respect to console_lock();.
  2720. *
  2721. * There are some functions which don't need it.
  2722. *
  2723. * There are some functions which can sleep for arbitrary periods
  2724. * (paste_selection) but we don't need the lock there anyway.
  2725. *
  2726. * set_selection has locking, and definitely needs it
  2727. */
  2728. int tioclinux(struct tty_struct *tty, unsigned long arg)
  2729. {
  2730. char type, data;
  2731. char __user *p = (char __user *)arg;
  2732. int lines;
  2733. int ret;
  2734. if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
  2735. return -EPERM;
  2736. if (get_user(type, p))
  2737. return -EFAULT;
  2738. ret = 0;
  2739. switch (type)
  2740. {
  2741. case TIOCL_SETSEL:
  2742. ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
  2743. break;
  2744. case TIOCL_PASTESEL:
  2745. ret = paste_selection(tty);
  2746. break;
  2747. case TIOCL_UNBLANKSCREEN:
  2748. console_lock();
  2749. unblank_screen();
  2750. console_unlock();
  2751. break;
  2752. case TIOCL_SELLOADLUT:
  2753. console_lock();
  2754. ret = sel_loadlut(p);
  2755. console_unlock();
  2756. break;
  2757. case TIOCL_GETSHIFTSTATE:
  2758. /*
  2759. * Make it possible to react to Shift+Mousebutton.
  2760. * Note that 'shift_state' is an undocumented
  2761. * kernel-internal variable; programs not closely
  2762. * related to the kernel should not use this.
  2763. */
  2764. data = vt_get_shift_state();
  2765. ret = put_user(data, p);
  2766. break;
  2767. case TIOCL_GETMOUSEREPORTING:
  2768. console_lock(); /* May be overkill */
  2769. data = mouse_reporting();
  2770. console_unlock();
  2771. ret = put_user(data, p);
  2772. break;
  2773. case TIOCL_SETVESABLANK:
  2774. console_lock();
  2775. ret = set_vesa_blanking(p);
  2776. console_unlock();
  2777. break;
  2778. case TIOCL_GETKMSGREDIRECT:
  2779. data = vt_get_kmsg_redirect();
  2780. ret = put_user(data, p);
  2781. break;
  2782. case TIOCL_SETKMSGREDIRECT:
  2783. if (!capable(CAP_SYS_ADMIN)) {
  2784. ret = -EPERM;
  2785. } else {
  2786. if (get_user(data, p+1))
  2787. ret = -EFAULT;
  2788. else
  2789. vt_kmsg_redirect(data);
  2790. }
  2791. break;
  2792. case TIOCL_GETFGCONSOLE:
  2793. /* No locking needed as this is a transiently
  2794. correct return anyway if the caller hasn't
  2795. disabled switching */
  2796. ret = fg_console;
  2797. break;
  2798. case TIOCL_SCROLLCONSOLE:
  2799. if (get_user(lines, (s32 __user *)(p+4))) {
  2800. ret = -EFAULT;
  2801. } else {
  2802. /* Need the console lock here. Note that lots
  2803. of other calls need fixing before the lock
  2804. is actually useful ! */
  2805. console_lock();
  2806. scrollfront(vc_cons[fg_console].d, lines);
  2807. console_unlock();
  2808. ret = 0;
  2809. }
  2810. break;
  2811. case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
  2812. console_lock();
  2813. ignore_poke = 1;
  2814. do_blank_screen(0);
  2815. console_unlock();
  2816. break;
  2817. case TIOCL_BLANKEDSCREEN:
  2818. ret = console_blanked;
  2819. break;
  2820. default:
  2821. ret = -EINVAL;
  2822. break;
  2823. }
  2824. return ret;
  2825. }
  2826. /*
  2827. * /dev/ttyN handling
  2828. */
  2829. static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  2830. {
  2831. int retval;
  2832. retval = do_con_write(tty, buf, count);
  2833. con_flush_chars(tty);
  2834. return retval;
  2835. }
  2836. static int con_put_char(struct tty_struct *tty, unsigned char ch)
  2837. {
  2838. if (in_interrupt())
  2839. return 0; /* n_r3964 calls put_char() from interrupt context */
  2840. return do_con_write(tty, &ch, 1);
  2841. }
  2842. static int con_write_room(struct tty_struct *tty)
  2843. {
  2844. if (tty->stopped)
  2845. return 0;
  2846. return 32768; /* No limit, really; we're not buffering */
  2847. }
  2848. static int con_chars_in_buffer(struct tty_struct *tty)
  2849. {
  2850. return 0; /* we're not buffering */
  2851. }
  2852. /*
  2853. * con_throttle and con_unthrottle are only used for
  2854. * paste_selection(), which has to stuff in a large number of
  2855. * characters...
  2856. */
  2857. static void con_throttle(struct tty_struct *tty)
  2858. {
  2859. }
  2860. static void con_unthrottle(struct tty_struct *tty)
  2861. {
  2862. struct vc_data *vc = tty->driver_data;
  2863. wake_up_interruptible(&vc->paste_wait);
  2864. }
  2865. /*
  2866. * Turn the Scroll-Lock LED on when the tty is stopped
  2867. */
  2868. static void con_stop(struct tty_struct *tty)
  2869. {
  2870. int console_num;
  2871. if (!tty)
  2872. return;
  2873. console_num = tty->index;
  2874. if (!vc_cons_allocated(console_num))
  2875. return;
  2876. vt_kbd_con_stop(console_num);
  2877. }
  2878. /*
  2879. * Turn the Scroll-Lock LED off when the console is started
  2880. */
  2881. static void con_start(struct tty_struct *tty)
  2882. {
  2883. int console_num;
  2884. if (!tty)
  2885. return;
  2886. console_num = tty->index;
  2887. if (!vc_cons_allocated(console_num))
  2888. return;
  2889. vt_kbd_con_start(console_num);
  2890. }
  2891. static void con_flush_chars(struct tty_struct *tty)
  2892. {
  2893. struct vc_data *vc;
  2894. if (in_interrupt()) /* from flush_to_ldisc */
  2895. return;
  2896. /* if we race with con_close(), vt may be null */
  2897. console_lock();
  2898. vc = tty->driver_data;
  2899. if (vc)
  2900. set_cursor(vc);
  2901. console_unlock();
  2902. }
  2903. /*
  2904. * Allocate the console screen memory.
  2905. */
  2906. static int con_install(struct tty_driver *driver, struct tty_struct *tty)
  2907. {
  2908. unsigned int currcons = tty->index;
  2909. struct vc_data *vc;
  2910. int ret;
  2911. console_lock();
  2912. ret = vc_allocate(currcons);
  2913. if (ret)
  2914. goto unlock;
  2915. vc = vc_cons[currcons].d;
  2916. /* Still being freed */
  2917. if (vc->port.tty) {
  2918. ret = -ERESTARTSYS;
  2919. goto unlock;
  2920. }
  2921. ret = tty_port_install(&vc->port, driver, tty);
  2922. if (ret)
  2923. goto unlock;
  2924. tty->driver_data = vc;
  2925. vc->port.tty = tty;
  2926. tty_port_get(&vc->port);
  2927. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  2928. tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
  2929. tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
  2930. }
  2931. if (vc->vc_utf)
  2932. tty->termios.c_iflag |= IUTF8;
  2933. else
  2934. tty->termios.c_iflag &= ~IUTF8;
  2935. unlock:
  2936. console_unlock();
  2937. return ret;
  2938. }
  2939. static int con_open(struct tty_struct *tty, struct file *filp)
  2940. {
  2941. /* everything done in install */
  2942. return 0;
  2943. }
  2944. static void con_close(struct tty_struct *tty, struct file *filp)
  2945. {
  2946. /* Nothing to do - we defer to shutdown */
  2947. }
  2948. static void con_shutdown(struct tty_struct *tty)
  2949. {
  2950. struct vc_data *vc = tty->driver_data;
  2951. BUG_ON(vc == NULL);
  2952. console_lock();
  2953. vc->port.tty = NULL;
  2954. console_unlock();
  2955. }
  2956. static void con_cleanup(struct tty_struct *tty)
  2957. {
  2958. struct vc_data *vc = tty->driver_data;
  2959. tty_port_put(&vc->port);
  2960. }
  2961. static int default_color = 7; /* white */
  2962. static int default_italic_color = 2; // green (ASCII)
  2963. static int default_underline_color = 3; // cyan (ASCII)
  2964. module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
  2965. module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
  2966. module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
  2967. static void vc_init(struct vc_data *vc, unsigned int rows,
  2968. unsigned int cols, int do_clear)
  2969. {
  2970. int j, k ;
  2971. vc->vc_cols = cols;
  2972. vc->vc_rows = rows;
  2973. vc->vc_size_row = cols << 1;
  2974. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  2975. set_origin(vc);
  2976. vc->vc_pos = vc->vc_origin;
  2977. reset_vc(vc);
  2978. for (j=k=0; j<16; j++) {
  2979. vc->vc_palette[k++] = default_red[j] ;
  2980. vc->vc_palette[k++] = default_grn[j] ;
  2981. vc->vc_palette[k++] = default_blu[j] ;
  2982. }
  2983. vc->vc_def_color = default_color;
  2984. vc->vc_ulcolor = default_underline_color;
  2985. vc->vc_itcolor = default_italic_color;
  2986. vc->vc_halfcolor = 0x08; /* grey */
  2987. init_waitqueue_head(&vc->paste_wait);
  2988. reset_terminal(vc, do_clear);
  2989. }
  2990. /*
  2991. * This routine initializes console interrupts, and does nothing
  2992. * else. If you want the screen to clear, call tty_write with
  2993. * the appropriate escape-sequence.
  2994. */
  2995. static int __init con_init(void)
  2996. {
  2997. const char *display_desc = NULL;
  2998. struct vc_data *vc;
  2999. unsigned int currcons = 0, i;
  3000. console_lock();
  3001. if (conswitchp)
  3002. display_desc = conswitchp->con_startup();
  3003. if (!display_desc) {
  3004. fg_console = 0;
  3005. console_unlock();
  3006. return 0;
  3007. }
  3008. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3009. struct con_driver *con_driver = &registered_con_driver[i];
  3010. if (con_driver->con == NULL) {
  3011. con_driver->con = conswitchp;
  3012. con_driver->desc = display_desc;
  3013. con_driver->flag = CON_DRIVER_FLAG_INIT;
  3014. con_driver->first = 0;
  3015. con_driver->last = MAX_NR_CONSOLES - 1;
  3016. break;
  3017. }
  3018. }
  3019. for (i = 0; i < MAX_NR_CONSOLES; i++)
  3020. con_driver_map[i] = conswitchp;
  3021. if (blankinterval) {
  3022. blank_state = blank_normal_wait;
  3023. mod_timer(&console_timer, jiffies + (blankinterval * HZ));
  3024. }
  3025. for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
  3026. vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
  3027. INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
  3028. tty_port_init(&vc->port);
  3029. visual_init(vc, currcons, 1);
  3030. /* Assuming vc->vc_{cols,rows,screenbuf_size} are sane here. */
  3031. vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
  3032. vc_init(vc, vc->vc_rows, vc->vc_cols,
  3033. currcons || !vc->vc_sw->con_save_screen);
  3034. }
  3035. currcons = fg_console = 0;
  3036. master_display_fg = vc = vc_cons[currcons].d;
  3037. set_origin(vc);
  3038. save_screen(vc);
  3039. gotoxy(vc, vc->vc_x, vc->vc_y);
  3040. csi_J(vc, 0);
  3041. update_screen(vc);
  3042. pr_info("Console: %s %s %dx%d\n",
  3043. vc->vc_can_do_color ? "colour" : "mono",
  3044. display_desc, vc->vc_cols, vc->vc_rows);
  3045. printable = 1;
  3046. console_unlock();
  3047. #ifdef CONFIG_VT_CONSOLE
  3048. register_console(&vt_console_driver);
  3049. #endif
  3050. return 0;
  3051. }
  3052. console_initcall(con_init);
  3053. static const struct tty_operations con_ops = {
  3054. .install = con_install,
  3055. .open = con_open,
  3056. .close = con_close,
  3057. .write = con_write,
  3058. .write_room = con_write_room,
  3059. .put_char = con_put_char,
  3060. .flush_chars = con_flush_chars,
  3061. .chars_in_buffer = con_chars_in_buffer,
  3062. .ioctl = vt_ioctl,
  3063. #ifdef CONFIG_COMPAT
  3064. .compat_ioctl = vt_compat_ioctl,
  3065. #endif
  3066. .stop = con_stop,
  3067. .start = con_start,
  3068. .throttle = con_throttle,
  3069. .unthrottle = con_unthrottle,
  3070. .resize = vt_resize,
  3071. .shutdown = con_shutdown,
  3072. .cleanup = con_cleanup,
  3073. };
  3074. static struct cdev vc0_cdev;
  3075. static ssize_t show_tty_active(struct device *dev,
  3076. struct device_attribute *attr, char *buf)
  3077. {
  3078. return sprintf(buf, "tty%d\n", fg_console + 1);
  3079. }
  3080. static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
  3081. static struct attribute *vt_dev_attrs[] = {
  3082. &dev_attr_active.attr,
  3083. NULL
  3084. };
  3085. ATTRIBUTE_GROUPS(vt_dev);
  3086. int __init vty_init(const struct file_operations *console_fops)
  3087. {
  3088. cdev_init(&vc0_cdev, console_fops);
  3089. if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
  3090. register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
  3091. panic("Couldn't register /dev/tty0 driver\n");
  3092. tty0dev = device_create_with_groups(tty_class, NULL,
  3093. MKDEV(TTY_MAJOR, 0), NULL,
  3094. vt_dev_groups, "tty0");
  3095. if (IS_ERR(tty0dev))
  3096. tty0dev = NULL;
  3097. vcs_init();
  3098. console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
  3099. if (!console_driver)
  3100. panic("Couldn't allocate console driver\n");
  3101. console_driver->name = "tty";
  3102. console_driver->name_base = 1;
  3103. console_driver->major = TTY_MAJOR;
  3104. console_driver->minor_start = 1;
  3105. console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  3106. console_driver->init_termios = tty_std_termios;
  3107. if (default_utf8)
  3108. console_driver->init_termios.c_iflag |= IUTF8;
  3109. console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  3110. tty_set_operations(console_driver, &con_ops);
  3111. if (tty_register_driver(console_driver))
  3112. panic("Couldn't register console driver\n");
  3113. kbd_init();
  3114. console_map_init();
  3115. #ifdef CONFIG_MDA_CONSOLE
  3116. mda_console_init();
  3117. #endif
  3118. return 0;
  3119. }
  3120. #ifndef VT_SINGLE_DRIVER
  3121. static struct class *vtconsole_class;
  3122. static int do_bind_con_driver(const struct consw *csw, int first, int last,
  3123. int deflt)
  3124. {
  3125. struct module *owner = csw->owner;
  3126. const char *desc = NULL;
  3127. struct con_driver *con_driver;
  3128. int i, j = -1, k = -1, retval = -ENODEV;
  3129. if (!try_module_get(owner))
  3130. return -ENODEV;
  3131. WARN_CONSOLE_UNLOCKED();
  3132. /* check if driver is registered */
  3133. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3134. con_driver = &registered_con_driver[i];
  3135. if (con_driver->con == csw) {
  3136. desc = con_driver->desc;
  3137. retval = 0;
  3138. break;
  3139. }
  3140. }
  3141. if (retval)
  3142. goto err;
  3143. if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
  3144. csw->con_startup();
  3145. con_driver->flag |= CON_DRIVER_FLAG_INIT;
  3146. }
  3147. if (deflt) {
  3148. if (conswitchp)
  3149. module_put(conswitchp->owner);
  3150. __module_get(owner);
  3151. conswitchp = csw;
  3152. }
  3153. first = max(first, con_driver->first);
  3154. last = min(last, con_driver->last);
  3155. for (i = first; i <= last; i++) {
  3156. int old_was_color;
  3157. struct vc_data *vc = vc_cons[i].d;
  3158. if (con_driver_map[i])
  3159. module_put(con_driver_map[i]->owner);
  3160. __module_get(owner);
  3161. con_driver_map[i] = csw;
  3162. if (!vc || !vc->vc_sw)
  3163. continue;
  3164. j = i;
  3165. if (con_is_visible(vc)) {
  3166. k = i;
  3167. save_screen(vc);
  3168. }
  3169. old_was_color = vc->vc_can_do_color;
  3170. vc->vc_sw->con_deinit(vc);
  3171. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  3172. visual_init(vc, i, 0);
  3173. set_origin(vc);
  3174. update_attr(vc);
  3175. /* If the console changed between mono <-> color, then
  3176. * the attributes in the screenbuf will be wrong. The
  3177. * following resets all attributes to something sane.
  3178. */
  3179. if (old_was_color != vc->vc_can_do_color)
  3180. clear_buffer_attributes(vc);
  3181. }
  3182. pr_info("Console: switching ");
  3183. if (!deflt)
  3184. pr_cont("consoles %d-%d ", first + 1, last + 1);
  3185. if (j >= 0) {
  3186. struct vc_data *vc = vc_cons[j].d;
  3187. pr_cont("to %s %s %dx%d\n",
  3188. vc->vc_can_do_color ? "colour" : "mono",
  3189. desc, vc->vc_cols, vc->vc_rows);
  3190. if (k >= 0) {
  3191. vc = vc_cons[k].d;
  3192. update_screen(vc);
  3193. }
  3194. } else {
  3195. pr_cont("to %s\n", desc);
  3196. }
  3197. retval = 0;
  3198. err:
  3199. module_put(owner);
  3200. return retval;
  3201. };
  3202. #ifdef CONFIG_VT_HW_CONSOLE_BINDING
  3203. /* unlocked version of unbind_con_driver() */
  3204. int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
  3205. {
  3206. struct module *owner = csw->owner;
  3207. const struct consw *defcsw = NULL;
  3208. struct con_driver *con_driver = NULL, *con_back = NULL;
  3209. int i, retval = -ENODEV;
  3210. if (!try_module_get(owner))
  3211. return -ENODEV;
  3212. WARN_CONSOLE_UNLOCKED();
  3213. /* check if driver is registered and if it is unbindable */
  3214. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3215. con_driver = &registered_con_driver[i];
  3216. if (con_driver->con == csw &&
  3217. con_driver->flag & CON_DRIVER_FLAG_MODULE) {
  3218. retval = 0;
  3219. break;
  3220. }
  3221. }
  3222. if (retval)
  3223. goto err;
  3224. retval = -ENODEV;
  3225. /* check if backup driver exists */
  3226. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3227. con_back = &registered_con_driver[i];
  3228. if (con_back->con && con_back->con != csw) {
  3229. defcsw = con_back->con;
  3230. retval = 0;
  3231. break;
  3232. }
  3233. }
  3234. if (retval)
  3235. goto err;
  3236. if (!con_is_bound(csw))
  3237. goto err;
  3238. first = max(first, con_driver->first);
  3239. last = min(last, con_driver->last);
  3240. for (i = first; i <= last; i++) {
  3241. if (con_driver_map[i] == csw) {
  3242. module_put(csw->owner);
  3243. con_driver_map[i] = NULL;
  3244. }
  3245. }
  3246. if (!con_is_bound(defcsw)) {
  3247. const struct consw *defconsw = conswitchp;
  3248. defcsw->con_startup();
  3249. con_back->flag |= CON_DRIVER_FLAG_INIT;
  3250. /*
  3251. * vgacon may change the default driver to point
  3252. * to dummycon, we restore it here...
  3253. */
  3254. conswitchp = defconsw;
  3255. }
  3256. if (!con_is_bound(csw))
  3257. con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
  3258. /* ignore return value, binding should not fail */
  3259. do_bind_con_driver(defcsw, first, last, deflt);
  3260. err:
  3261. module_put(owner);
  3262. return retval;
  3263. }
  3264. EXPORT_SYMBOL_GPL(do_unbind_con_driver);
  3265. static int vt_bind(struct con_driver *con)
  3266. {
  3267. const struct consw *defcsw = NULL, *csw = NULL;
  3268. int i, more = 1, first = -1, last = -1, deflt = 0;
  3269. if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
  3270. goto err;
  3271. csw = con->con;
  3272. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3273. struct con_driver *con = &registered_con_driver[i];
  3274. if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
  3275. defcsw = con->con;
  3276. break;
  3277. }
  3278. }
  3279. if (!defcsw)
  3280. goto err;
  3281. while (more) {
  3282. more = 0;
  3283. for (i = con->first; i <= con->last; i++) {
  3284. if (con_driver_map[i] == defcsw) {
  3285. if (first == -1)
  3286. first = i;
  3287. last = i;
  3288. more = 1;
  3289. } else if (first != -1)
  3290. break;
  3291. }
  3292. if (first == 0 && last == MAX_NR_CONSOLES -1)
  3293. deflt = 1;
  3294. if (first != -1)
  3295. do_bind_con_driver(csw, first, last, deflt);
  3296. first = -1;
  3297. last = -1;
  3298. deflt = 0;
  3299. }
  3300. err:
  3301. return 0;
  3302. }
  3303. static int vt_unbind(struct con_driver *con)
  3304. {
  3305. const struct consw *csw = NULL;
  3306. int i, more = 1, first = -1, last = -1, deflt = 0;
  3307. int ret;
  3308. if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
  3309. goto err;
  3310. csw = con->con;
  3311. while (more) {
  3312. more = 0;
  3313. for (i = con->first; i <= con->last; i++) {
  3314. if (con_driver_map[i] == csw) {
  3315. if (first == -1)
  3316. first = i;
  3317. last = i;
  3318. more = 1;
  3319. } else if (first != -1)
  3320. break;
  3321. }
  3322. if (first == 0 && last == MAX_NR_CONSOLES -1)
  3323. deflt = 1;
  3324. if (first != -1) {
  3325. ret = do_unbind_con_driver(csw, first, last, deflt);
  3326. if (ret != 0)
  3327. return ret;
  3328. }
  3329. first = -1;
  3330. last = -1;
  3331. deflt = 0;
  3332. }
  3333. err:
  3334. return 0;
  3335. }
  3336. #else
  3337. static inline int vt_bind(struct con_driver *con)
  3338. {
  3339. return 0;
  3340. }
  3341. static inline int vt_unbind(struct con_driver *con)
  3342. {
  3343. return 0;
  3344. }
  3345. #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
  3346. static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
  3347. const char *buf, size_t count)
  3348. {
  3349. struct con_driver *con = dev_get_drvdata(dev);
  3350. int bind = simple_strtoul(buf, NULL, 0);
  3351. console_lock();
  3352. if (bind)
  3353. vt_bind(con);
  3354. else
  3355. vt_unbind(con);
  3356. console_unlock();
  3357. return count;
  3358. }
  3359. static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
  3360. char *buf)
  3361. {
  3362. struct con_driver *con = dev_get_drvdata(dev);
  3363. int bind = con_is_bound(con->con);
  3364. return snprintf(buf, PAGE_SIZE, "%i\n", bind);
  3365. }
  3366. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  3367. char *buf)
  3368. {
  3369. struct con_driver *con = dev_get_drvdata(dev);
  3370. return snprintf(buf, PAGE_SIZE, "%s %s\n",
  3371. (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
  3372. con->desc);
  3373. }
  3374. static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
  3375. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  3376. static struct attribute *con_dev_attrs[] = {
  3377. &dev_attr_bind.attr,
  3378. &dev_attr_name.attr,
  3379. NULL
  3380. };
  3381. ATTRIBUTE_GROUPS(con_dev);
  3382. static int vtconsole_init_device(struct con_driver *con)
  3383. {
  3384. con->flag |= CON_DRIVER_FLAG_ATTR;
  3385. return 0;
  3386. }
  3387. static void vtconsole_deinit_device(struct con_driver *con)
  3388. {
  3389. con->flag &= ~CON_DRIVER_FLAG_ATTR;
  3390. }
  3391. /**
  3392. * con_is_bound - checks if driver is bound to the console
  3393. * @csw: console driver
  3394. *
  3395. * RETURNS: zero if unbound, nonzero if bound
  3396. *
  3397. * Drivers can call this and if zero, they should release
  3398. * all resources allocated on con_startup()
  3399. */
  3400. int con_is_bound(const struct consw *csw)
  3401. {
  3402. int i, bound = 0;
  3403. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  3404. if (con_driver_map[i] == csw) {
  3405. bound = 1;
  3406. break;
  3407. }
  3408. }
  3409. return bound;
  3410. }
  3411. EXPORT_SYMBOL(con_is_bound);
  3412. /**
  3413. * con_debug_enter - prepare the console for the kernel debugger
  3414. * @sw: console driver
  3415. *
  3416. * Called when the console is taken over by the kernel debugger, this
  3417. * function needs to save the current console state, then put the console
  3418. * into a state suitable for the kernel debugger.
  3419. *
  3420. * RETURNS:
  3421. * Zero on success, nonzero if a failure occurred when trying to prepare
  3422. * the console for the debugger.
  3423. */
  3424. int con_debug_enter(struct vc_data *vc)
  3425. {
  3426. int ret = 0;
  3427. saved_fg_console = fg_console;
  3428. saved_last_console = last_console;
  3429. saved_want_console = want_console;
  3430. saved_vc_mode = vc->vc_mode;
  3431. saved_console_blanked = console_blanked;
  3432. vc->vc_mode = KD_TEXT;
  3433. console_blanked = 0;
  3434. if (vc->vc_sw->con_debug_enter)
  3435. ret = vc->vc_sw->con_debug_enter(vc);
  3436. #ifdef CONFIG_KGDB_KDB
  3437. /* Set the initial LINES variable if it is not already set */
  3438. if (vc->vc_rows < 999) {
  3439. int linecount;
  3440. char lns[4];
  3441. const char *setargs[3] = {
  3442. "set",
  3443. "LINES",
  3444. lns,
  3445. };
  3446. if (kdbgetintenv(setargs[0], &linecount)) {
  3447. snprintf(lns, 4, "%i", vc->vc_rows);
  3448. kdb_set(2, setargs);
  3449. }
  3450. }
  3451. if (vc->vc_cols < 999) {
  3452. int colcount;
  3453. char cols[4];
  3454. const char *setargs[3] = {
  3455. "set",
  3456. "COLUMNS",
  3457. cols,
  3458. };
  3459. if (kdbgetintenv(setargs[0], &colcount)) {
  3460. snprintf(cols, 4, "%i", vc->vc_cols);
  3461. kdb_set(2, setargs);
  3462. }
  3463. }
  3464. #endif /* CONFIG_KGDB_KDB */
  3465. return ret;
  3466. }
  3467. EXPORT_SYMBOL_GPL(con_debug_enter);
  3468. /**
  3469. * con_debug_leave - restore console state
  3470. * @sw: console driver
  3471. *
  3472. * Restore the console state to what it was before the kernel debugger
  3473. * was invoked.
  3474. *
  3475. * RETURNS:
  3476. * Zero on success, nonzero if a failure occurred when trying to restore
  3477. * the console.
  3478. */
  3479. int con_debug_leave(void)
  3480. {
  3481. struct vc_data *vc;
  3482. int ret = 0;
  3483. fg_console = saved_fg_console;
  3484. last_console = saved_last_console;
  3485. want_console = saved_want_console;
  3486. console_blanked = saved_console_blanked;
  3487. vc_cons[fg_console].d->vc_mode = saved_vc_mode;
  3488. vc = vc_cons[fg_console].d;
  3489. if (vc->vc_sw->con_debug_leave)
  3490. ret = vc->vc_sw->con_debug_leave(vc);
  3491. return ret;
  3492. }
  3493. EXPORT_SYMBOL_GPL(con_debug_leave);
  3494. static int do_register_con_driver(const struct consw *csw, int first, int last)
  3495. {
  3496. struct module *owner = csw->owner;
  3497. struct con_driver *con_driver;
  3498. const char *desc;
  3499. int i, retval;
  3500. WARN_CONSOLE_UNLOCKED();
  3501. if (!try_module_get(owner))
  3502. return -ENODEV;
  3503. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3504. con_driver = &registered_con_driver[i];
  3505. /* already registered */
  3506. if (con_driver->con == csw) {
  3507. retval = -EBUSY;
  3508. goto err;
  3509. }
  3510. }
  3511. desc = csw->con_startup();
  3512. if (!desc) {
  3513. retval = -ENODEV;
  3514. goto err;
  3515. }
  3516. retval = -EINVAL;
  3517. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3518. con_driver = &registered_con_driver[i];
  3519. if (con_driver->con == NULL &&
  3520. !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
  3521. con_driver->con = csw;
  3522. con_driver->desc = desc;
  3523. con_driver->node = i;
  3524. con_driver->flag = CON_DRIVER_FLAG_MODULE |
  3525. CON_DRIVER_FLAG_INIT;
  3526. con_driver->first = first;
  3527. con_driver->last = last;
  3528. retval = 0;
  3529. break;
  3530. }
  3531. }
  3532. if (retval)
  3533. goto err;
  3534. con_driver->dev =
  3535. device_create_with_groups(vtconsole_class, NULL,
  3536. MKDEV(0, con_driver->node),
  3537. con_driver, con_dev_groups,
  3538. "vtcon%i", con_driver->node);
  3539. if (IS_ERR(con_driver->dev)) {
  3540. pr_warn("Unable to create device for %s; errno = %ld\n",
  3541. con_driver->desc, PTR_ERR(con_driver->dev));
  3542. con_driver->dev = NULL;
  3543. } else {
  3544. vtconsole_init_device(con_driver);
  3545. }
  3546. err:
  3547. module_put(owner);
  3548. return retval;
  3549. }
  3550. /**
  3551. * do_unregister_con_driver - unregister console driver from console layer
  3552. * @csw: console driver
  3553. *
  3554. * DESCRIPTION: All drivers that registers to the console layer must
  3555. * call this function upon exit, or if the console driver is in a state
  3556. * where it won't be able to handle console services, such as the
  3557. * framebuffer console without loaded framebuffer drivers.
  3558. *
  3559. * The driver must unbind first prior to unregistration.
  3560. */
  3561. int do_unregister_con_driver(const struct consw *csw)
  3562. {
  3563. int i;
  3564. /* cannot unregister a bound driver */
  3565. if (con_is_bound(csw))
  3566. return -EBUSY;
  3567. if (csw == conswitchp)
  3568. return -EINVAL;
  3569. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3570. struct con_driver *con_driver = &registered_con_driver[i];
  3571. if (con_driver->con == csw) {
  3572. /*
  3573. * Defer the removal of the sysfs entries since that
  3574. * will acquire the kernfs s_active lock and we can't
  3575. * acquire this lock while holding the console lock:
  3576. * the unbind sysfs entry imposes already the opposite
  3577. * order. Reset con already here to prevent any later
  3578. * lookup to succeed and mark this slot as zombie, so
  3579. * it won't get reused until we complete the removal
  3580. * in the deferred work.
  3581. */
  3582. con_driver->con = NULL;
  3583. con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
  3584. schedule_work(&con_driver_unregister_work);
  3585. return 0;
  3586. }
  3587. }
  3588. return -ENODEV;
  3589. }
  3590. EXPORT_SYMBOL_GPL(do_unregister_con_driver);
  3591. static void con_driver_unregister_callback(struct work_struct *ignored)
  3592. {
  3593. int i;
  3594. console_lock();
  3595. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3596. struct con_driver *con_driver = &registered_con_driver[i];
  3597. if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
  3598. continue;
  3599. console_unlock();
  3600. vtconsole_deinit_device(con_driver);
  3601. device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
  3602. console_lock();
  3603. if (WARN_ON_ONCE(con_driver->con))
  3604. con_driver->con = NULL;
  3605. con_driver->desc = NULL;
  3606. con_driver->dev = NULL;
  3607. con_driver->node = 0;
  3608. WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
  3609. con_driver->flag = 0;
  3610. con_driver->first = 0;
  3611. con_driver->last = 0;
  3612. }
  3613. console_unlock();
  3614. }
  3615. /*
  3616. * If we support more console drivers, this function is used
  3617. * when a driver wants to take over some existing consoles
  3618. * and become default driver for newly opened ones.
  3619. *
  3620. * do_take_over_console is basically a register followed by unbind
  3621. */
  3622. int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
  3623. {
  3624. int err;
  3625. err = do_register_con_driver(csw, first, last);
  3626. /*
  3627. * If we get an busy error we still want to bind the console driver
  3628. * and return success, as we may have unbound the console driver
  3629. * but not unregistered it.
  3630. */
  3631. if (err == -EBUSY)
  3632. err = 0;
  3633. if (!err)
  3634. do_bind_con_driver(csw, first, last, deflt);
  3635. return err;
  3636. }
  3637. EXPORT_SYMBOL_GPL(do_take_over_console);
  3638. /*
  3639. * give_up_console is a wrapper to unregister_con_driver. It will only
  3640. * work if driver is fully unbound.
  3641. */
  3642. void give_up_console(const struct consw *csw)
  3643. {
  3644. console_lock();
  3645. do_unregister_con_driver(csw);
  3646. console_unlock();
  3647. }
  3648. static int __init vtconsole_class_init(void)
  3649. {
  3650. int i;
  3651. vtconsole_class = class_create(THIS_MODULE, "vtconsole");
  3652. if (IS_ERR(vtconsole_class)) {
  3653. pr_warn("Unable to create vt console class; errno = %ld\n",
  3654. PTR_ERR(vtconsole_class));
  3655. vtconsole_class = NULL;
  3656. }
  3657. /* Add system drivers to sysfs */
  3658. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3659. struct con_driver *con = &registered_con_driver[i];
  3660. if (con->con && !con->dev) {
  3661. con->dev =
  3662. device_create_with_groups(vtconsole_class, NULL,
  3663. MKDEV(0, con->node),
  3664. con, con_dev_groups,
  3665. "vtcon%i", con->node);
  3666. if (IS_ERR(con->dev)) {
  3667. pr_warn("Unable to create device for %s; errno = %ld\n",
  3668. con->desc, PTR_ERR(con->dev));
  3669. con->dev = NULL;
  3670. } else {
  3671. vtconsole_init_device(con);
  3672. }
  3673. }
  3674. }
  3675. return 0;
  3676. }
  3677. postcore_initcall(vtconsole_class_init);
  3678. #endif
  3679. /*
  3680. * Screen blanking
  3681. */
  3682. static int set_vesa_blanking(char __user *p)
  3683. {
  3684. unsigned int mode;
  3685. if (get_user(mode, p + 1))
  3686. return -EFAULT;
  3687. vesa_blank_mode = (mode < 4) ? mode : 0;
  3688. return 0;
  3689. }
  3690. void do_blank_screen(int entering_gfx)
  3691. {
  3692. struct vc_data *vc = vc_cons[fg_console].d;
  3693. int i;
  3694. WARN_CONSOLE_UNLOCKED();
  3695. if (console_blanked) {
  3696. if (blank_state == blank_vesa_wait) {
  3697. blank_state = blank_off;
  3698. vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
  3699. }
  3700. return;
  3701. }
  3702. /* entering graphics mode? */
  3703. if (entering_gfx) {
  3704. hide_cursor(vc);
  3705. save_screen(vc);
  3706. vc->vc_sw->con_blank(vc, -1, 1);
  3707. console_blanked = fg_console + 1;
  3708. blank_state = blank_off;
  3709. set_origin(vc);
  3710. return;
  3711. }
  3712. blank_state = blank_off;
  3713. /* don't blank graphics */
  3714. if (vc->vc_mode != KD_TEXT) {
  3715. console_blanked = fg_console + 1;
  3716. return;
  3717. }
  3718. hide_cursor(vc);
  3719. del_timer_sync(&console_timer);
  3720. blank_timer_expired = 0;
  3721. save_screen(vc);
  3722. /* In case we need to reset origin, blanking hook returns 1 */
  3723. i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
  3724. console_blanked = fg_console + 1;
  3725. if (i)
  3726. set_origin(vc);
  3727. if (console_blank_hook && console_blank_hook(1))
  3728. return;
  3729. if (vesa_off_interval && vesa_blank_mode) {
  3730. blank_state = blank_vesa_wait;
  3731. mod_timer(&console_timer, jiffies + vesa_off_interval);
  3732. }
  3733. vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
  3734. }
  3735. EXPORT_SYMBOL(do_blank_screen);
  3736. /*
  3737. * Called by timer as well as from vt_console_driver
  3738. */
  3739. void do_unblank_screen(int leaving_gfx)
  3740. {
  3741. struct vc_data *vc;
  3742. /* This should now always be called from a "sane" (read: can schedule)
  3743. * context for the sake of the low level drivers, except in the special
  3744. * case of oops_in_progress
  3745. */
  3746. if (!oops_in_progress)
  3747. might_sleep();
  3748. WARN_CONSOLE_UNLOCKED();
  3749. ignore_poke = 0;
  3750. if (!console_blanked)
  3751. return;
  3752. if (!vc_cons_allocated(fg_console)) {
  3753. /* impossible */
  3754. pr_warn("unblank_screen: tty %d not allocated ??\n",
  3755. fg_console + 1);
  3756. return;
  3757. }
  3758. vc = vc_cons[fg_console].d;
  3759. /* Try to unblank in oops case too */
  3760. if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
  3761. return; /* but leave console_blanked != 0 */
  3762. if (blankinterval) {
  3763. mod_timer(&console_timer, jiffies + (blankinterval * HZ));
  3764. blank_state = blank_normal_wait;
  3765. }
  3766. console_blanked = 0;
  3767. if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
  3768. /* Low-level driver cannot restore -> do it ourselves */
  3769. update_screen(vc);
  3770. if (console_blank_hook)
  3771. console_blank_hook(0);
  3772. set_palette(vc);
  3773. set_cursor(vc);
  3774. vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
  3775. }
  3776. EXPORT_SYMBOL(do_unblank_screen);
  3777. /*
  3778. * This is called by the outside world to cause a forced unblank, mostly for
  3779. * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
  3780. * call it with 1 as an argument and so force a mode restore... that may kill
  3781. * X or at least garbage the screen but would also make the Oops visible...
  3782. */
  3783. void unblank_screen(void)
  3784. {
  3785. do_unblank_screen(0);
  3786. }
  3787. /*
  3788. * We defer the timer blanking to work queue so it can take the console mutex
  3789. * (console operations can still happen at irq time, but only from printk which
  3790. * has the console mutex. Not perfect yet, but better than no locking
  3791. */
  3792. static void blank_screen_t(struct timer_list *unused)
  3793. {
  3794. blank_timer_expired = 1;
  3795. schedule_work(&console_work);
  3796. }
  3797. void poke_blanked_console(void)
  3798. {
  3799. WARN_CONSOLE_UNLOCKED();
  3800. /* Add this so we quickly catch whoever might call us in a non
  3801. * safe context. Nowadays, unblank_screen() isn't to be called in
  3802. * atomic contexts and is allowed to schedule (with the special case
  3803. * of oops_in_progress, but that isn't of any concern for this
  3804. * function. --BenH.
  3805. */
  3806. might_sleep();
  3807. /* This isn't perfectly race free, but a race here would be mostly harmless,
  3808. * at worse, we'll do a spurrious blank and it's unlikely
  3809. */
  3810. del_timer(&console_timer);
  3811. blank_timer_expired = 0;
  3812. if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
  3813. return;
  3814. if (console_blanked)
  3815. unblank_screen();
  3816. else if (blankinterval) {
  3817. mod_timer(&console_timer, jiffies + (blankinterval * HZ));
  3818. blank_state = blank_normal_wait;
  3819. }
  3820. }
  3821. /*
  3822. * Palettes
  3823. */
  3824. static void set_palette(struct vc_data *vc)
  3825. {
  3826. WARN_CONSOLE_UNLOCKED();
  3827. if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
  3828. vc->vc_sw->con_set_palette(vc, color_table);
  3829. }
  3830. /*
  3831. * Load palette into the DAC registers. arg points to a colour
  3832. * map, 3 bytes per colour, 16 colours, range from 0 to 255.
  3833. */
  3834. int con_set_cmap(unsigned char __user *arg)
  3835. {
  3836. int i, j, k;
  3837. unsigned char colormap[3*16];
  3838. if (copy_from_user(colormap, arg, sizeof(colormap)))
  3839. return -EFAULT;
  3840. console_lock();
  3841. for (i = k = 0; i < 16; i++) {
  3842. default_red[i] = colormap[k++];
  3843. default_grn[i] = colormap[k++];
  3844. default_blu[i] = colormap[k++];
  3845. }
  3846. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  3847. if (!vc_cons_allocated(i))
  3848. continue;
  3849. for (j = k = 0; j < 16; j++) {
  3850. vc_cons[i].d->vc_palette[k++] = default_red[j];
  3851. vc_cons[i].d->vc_palette[k++] = default_grn[j];
  3852. vc_cons[i].d->vc_palette[k++] = default_blu[j];
  3853. }
  3854. set_palette(vc_cons[i].d);
  3855. }
  3856. console_unlock();
  3857. return 0;
  3858. }
  3859. int con_get_cmap(unsigned char __user *arg)
  3860. {
  3861. int i, k;
  3862. unsigned char colormap[3*16];
  3863. console_lock();
  3864. for (i = k = 0; i < 16; i++) {
  3865. colormap[k++] = default_red[i];
  3866. colormap[k++] = default_grn[i];
  3867. colormap[k++] = default_blu[i];
  3868. }
  3869. console_unlock();
  3870. if (copy_to_user(arg, colormap, sizeof(colormap)))
  3871. return -EFAULT;
  3872. return 0;
  3873. }
  3874. void reset_palette(struct vc_data *vc)
  3875. {
  3876. int j, k;
  3877. for (j=k=0; j<16; j++) {
  3878. vc->vc_palette[k++] = default_red[j];
  3879. vc->vc_palette[k++] = default_grn[j];
  3880. vc->vc_palette[k++] = default_blu[j];
  3881. }
  3882. set_palette(vc);
  3883. }
  3884. /*
  3885. * Font switching
  3886. *
  3887. * Currently we only support fonts up to 32 pixels wide, at a maximum height
  3888. * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
  3889. * depending on width) reserved for each character which is kinda wasty, but
  3890. * this is done in order to maintain compatibility with the EGA/VGA fonts. It
  3891. * is up to the actual low-level console-driver convert data into its favorite
  3892. * format (maybe we should add a `fontoffset' field to the `display'
  3893. * structure so we won't have to convert the fontdata all the time.
  3894. * /Jes
  3895. */
  3896. #define max_font_size 65536
  3897. static int con_font_get(struct vc_data *vc, struct console_font_op *op)
  3898. {
  3899. struct console_font font;
  3900. int rc = -EINVAL;
  3901. int c;
  3902. if (op->data) {
  3903. font.data = kmalloc(max_font_size, GFP_KERNEL);
  3904. if (!font.data)
  3905. return -ENOMEM;
  3906. } else
  3907. font.data = NULL;
  3908. console_lock();
  3909. if (vc->vc_mode != KD_TEXT)
  3910. rc = -EINVAL;
  3911. else if (vc->vc_sw->con_font_get)
  3912. rc = vc->vc_sw->con_font_get(vc, &font);
  3913. else
  3914. rc = -ENOSYS;
  3915. console_unlock();
  3916. if (rc)
  3917. goto out;
  3918. c = (font.width+7)/8 * 32 * font.charcount;
  3919. if (op->data && font.charcount > op->charcount)
  3920. rc = -ENOSPC;
  3921. if (!(op->flags & KD_FONT_FLAG_OLD)) {
  3922. if (font.width > op->width || font.height > op->height)
  3923. rc = -ENOSPC;
  3924. } else {
  3925. if (font.width != 8)
  3926. rc = -EIO;
  3927. else if ((op->height && font.height > op->height) ||
  3928. font.height > 32)
  3929. rc = -ENOSPC;
  3930. }
  3931. if (rc)
  3932. goto out;
  3933. op->height = font.height;
  3934. op->width = font.width;
  3935. op->charcount = font.charcount;
  3936. if (op->data && copy_to_user(op->data, font.data, c))
  3937. rc = -EFAULT;
  3938. out:
  3939. kfree(font.data);
  3940. return rc;
  3941. }
  3942. static int con_font_set(struct vc_data *vc, struct console_font_op *op)
  3943. {
  3944. struct console_font font;
  3945. int rc = -EINVAL;
  3946. int size;
  3947. if (vc->vc_mode != KD_TEXT)
  3948. return -EINVAL;
  3949. if (!op->data)
  3950. return -EINVAL;
  3951. if (op->charcount > 512)
  3952. return -EINVAL;
  3953. if (op->width <= 0 || op->width > 32 || op->height > 32)
  3954. return -EINVAL;
  3955. size = (op->width+7)/8 * 32 * op->charcount;
  3956. if (size > max_font_size)
  3957. return -ENOSPC;
  3958. font.data = memdup_user(op->data, size);
  3959. if (IS_ERR(font.data))
  3960. return PTR_ERR(font.data);
  3961. if (!op->height) { /* Need to guess font height [compat] */
  3962. int h, i;
  3963. u8 *charmap = font.data;
  3964. /*
  3965. * If from KDFONTOP ioctl, don't allow things which can be done
  3966. * in userland,so that we can get rid of this soon
  3967. */
  3968. if (!(op->flags & KD_FONT_FLAG_OLD)) {
  3969. kfree(font.data);
  3970. return -EINVAL;
  3971. }
  3972. for (h = 32; h > 0; h--)
  3973. for (i = 0; i < op->charcount; i++)
  3974. if (charmap[32*i+h-1])
  3975. goto nonzero;
  3976. kfree(font.data);
  3977. return -EINVAL;
  3978. nonzero:
  3979. op->height = h;
  3980. }
  3981. font.charcount = op->charcount;
  3982. font.width = op->width;
  3983. font.height = op->height;
  3984. console_lock();
  3985. if (vc->vc_mode != KD_TEXT)
  3986. rc = -EINVAL;
  3987. else if (vc->vc_sw->con_font_set)
  3988. rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
  3989. else
  3990. rc = -ENOSYS;
  3991. console_unlock();
  3992. kfree(font.data);
  3993. return rc;
  3994. }
  3995. static int con_font_default(struct vc_data *vc, struct console_font_op *op)
  3996. {
  3997. struct console_font font = {.width = op->width, .height = op->height};
  3998. char name[MAX_FONT_NAME];
  3999. char *s = name;
  4000. int rc;
  4001. if (!op->data)
  4002. s = NULL;
  4003. else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
  4004. return -EFAULT;
  4005. else
  4006. name[MAX_FONT_NAME - 1] = 0;
  4007. console_lock();
  4008. if (vc->vc_mode != KD_TEXT) {
  4009. console_unlock();
  4010. return -EINVAL;
  4011. }
  4012. if (vc->vc_sw->con_font_default)
  4013. rc = vc->vc_sw->con_font_default(vc, &font, s);
  4014. else
  4015. rc = -ENOSYS;
  4016. console_unlock();
  4017. if (!rc) {
  4018. op->width = font.width;
  4019. op->height = font.height;
  4020. }
  4021. return rc;
  4022. }
  4023. int con_font_op(struct vc_data *vc, struct console_font_op *op)
  4024. {
  4025. switch (op->op) {
  4026. case KD_FONT_OP_SET:
  4027. return con_font_set(vc, op);
  4028. case KD_FONT_OP_GET:
  4029. return con_font_get(vc, op);
  4030. case KD_FONT_OP_SET_DEFAULT:
  4031. return con_font_default(vc, op);
  4032. case KD_FONT_OP_COPY:
  4033. /* was buggy and never really used */
  4034. return -EINVAL;
  4035. }
  4036. return -ENOSYS;
  4037. }
  4038. /*
  4039. * Interface exported to selection and vcs.
  4040. */
  4041. /* used by selection */
  4042. u16 screen_glyph(struct vc_data *vc, int offset)
  4043. {
  4044. u16 w = scr_readw(screenpos(vc, offset, 1));
  4045. u16 c = w & 0xff;
  4046. if (w & vc->vc_hi_font_mask)
  4047. c |= 0x100;
  4048. return c;
  4049. }
  4050. EXPORT_SYMBOL_GPL(screen_glyph);
  4051. u32 screen_glyph_unicode(struct vc_data *vc, int n)
  4052. {
  4053. struct uni_screen *uniscr = get_vc_uniscr(vc);
  4054. if (uniscr)
  4055. return uniscr->lines[n / vc->vc_cols][n % vc->vc_cols];
  4056. return inverse_translate(vc, screen_glyph(vc, n * 2), 1);
  4057. }
  4058. EXPORT_SYMBOL_GPL(screen_glyph_unicode);
  4059. /* used by vcs - note the word offset */
  4060. unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
  4061. {
  4062. return screenpos(vc, 2 * w_offset, viewed);
  4063. }
  4064. EXPORT_SYMBOL_GPL(screen_pos);
  4065. void getconsxy(struct vc_data *vc, unsigned char *p)
  4066. {
  4067. p[0] = vc->vc_x;
  4068. p[1] = vc->vc_y;
  4069. }
  4070. void putconsxy(struct vc_data *vc, unsigned char *p)
  4071. {
  4072. hide_cursor(vc);
  4073. gotoxy(vc, p[0], p[1]);
  4074. set_cursor(vc);
  4075. }
  4076. u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
  4077. {
  4078. if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
  4079. return softcursor_original;
  4080. return scr_readw(org);
  4081. }
  4082. void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
  4083. {
  4084. scr_writew(val, org);
  4085. if ((unsigned long)org == vc->vc_pos) {
  4086. softcursor_original = -1;
  4087. add_softcursor(vc);
  4088. }
  4089. }
  4090. void vcs_scr_updated(struct vc_data *vc)
  4091. {
  4092. notify_update(vc);
  4093. }
  4094. void vc_scrolldelta_helper(struct vc_data *c, int lines,
  4095. unsigned int rolled_over, void *base, unsigned int size)
  4096. {
  4097. unsigned long ubase = (unsigned long)base;
  4098. ptrdiff_t scr_end = (void *)c->vc_scr_end - base;
  4099. ptrdiff_t vorigin = (void *)c->vc_visible_origin - base;
  4100. ptrdiff_t origin = (void *)c->vc_origin - base;
  4101. int margin = c->vc_size_row * 4;
  4102. int from, wrap, from_off, avail;
  4103. /* Turn scrollback off */
  4104. if (!lines) {
  4105. c->vc_visible_origin = c->vc_origin;
  4106. return;
  4107. }
  4108. /* Do we have already enough to allow jumping from 0 to the end? */
  4109. if (rolled_over > scr_end + margin) {
  4110. from = scr_end;
  4111. wrap = rolled_over + c->vc_size_row;
  4112. } else {
  4113. from = 0;
  4114. wrap = size;
  4115. }
  4116. from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row;
  4117. avail = (origin - from + wrap) % wrap;
  4118. /* Only a little piece would be left? Show all incl. the piece! */
  4119. if (avail < 2 * margin)
  4120. margin = 0;
  4121. if (from_off < margin)
  4122. from_off = 0;
  4123. if (from_off > avail - margin)
  4124. from_off = avail;
  4125. c->vc_visible_origin = ubase + (from + from_off) % wrap;
  4126. }
  4127. EXPORT_SYMBOL_GPL(vc_scrolldelta_helper);
  4128. /*
  4129. * Visible symbols for modules
  4130. */
  4131. EXPORT_SYMBOL(color_table);
  4132. EXPORT_SYMBOL(default_red);
  4133. EXPORT_SYMBOL(default_grn);
  4134. EXPORT_SYMBOL(default_blu);
  4135. EXPORT_SYMBOL(update_region);
  4136. EXPORT_SYMBOL(redraw_screen);
  4137. EXPORT_SYMBOL(vc_resize);
  4138. EXPORT_SYMBOL(fg_console);
  4139. EXPORT_SYMBOL(console_blank_hook);
  4140. EXPORT_SYMBOL(console_blanked);
  4141. EXPORT_SYMBOL(vc_cons);
  4142. EXPORT_SYMBOL(global_cursor_default);
  4143. #ifndef VT_SINGLE_DRIVER
  4144. EXPORT_SYMBOL(give_up_console);
  4145. #endif