vt.c 104 KB

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