vt.c 114 KB

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