vt.c 104 KB

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