vt.c 112 KB

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