vt.c 104 KB

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