vt.c 102 KB

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