vt.c 102 KB

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