vt.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218
  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, .c = unicode };
  235. atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
  236. }
  237. static void notify_update(struct vc_data *vc)
  238. {
  239. struct vt_notifier_param param = { .vc = vc };
  240. atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
  241. }
  242. /*
  243. * Low-Level Functions
  244. */
  245. #define IS_FG(vc) ((vc)->vc_num == fg_console)
  246. #ifdef VT_BUF_VRAM_ONLY
  247. #define DO_UPDATE(vc) 0
  248. #else
  249. #define DO_UPDATE(vc) (CON_IS_VISIBLE(vc) && !console_blanked)
  250. #endif
  251. static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
  252. {
  253. unsigned short *p;
  254. if (!viewed)
  255. p = (unsigned short *)(vc->vc_origin + offset);
  256. else if (!vc->vc_sw->con_screen_pos)
  257. p = (unsigned short *)(vc->vc_visible_origin + offset);
  258. else
  259. p = vc->vc_sw->con_screen_pos(vc, offset);
  260. return p;
  261. }
  262. /* Called from the keyboard irq path.. */
  263. static inline void scrolldelta(int lines)
  264. {
  265. /* FIXME */
  266. /* scrolldelta needs some kind of consistency lock, but the BKL was
  267. and still is not protecting versus the scheduled back end */
  268. scrollback_delta += lines;
  269. schedule_console_callback();
  270. }
  271. void schedule_console_callback(void)
  272. {
  273. schedule_work(&console_work);
  274. }
  275. static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
  276. {
  277. unsigned short *d, *s;
  278. if (t+nr >= b)
  279. nr = b - t - 1;
  280. if (b > vc->vc_rows || t >= b || nr < 1)
  281. return;
  282. if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
  283. return;
  284. d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
  285. s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
  286. scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
  287. scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
  288. vc->vc_size_row * nr);
  289. }
  290. static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
  291. {
  292. unsigned short *s;
  293. unsigned int step;
  294. if (t+nr >= b)
  295. nr = b - t - 1;
  296. if (b > vc->vc_rows || t >= b || nr < 1)
  297. return;
  298. if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
  299. return;
  300. s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
  301. step = vc->vc_cols * nr;
  302. scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
  303. scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
  304. }
  305. static void do_update_region(struct vc_data *vc, unsigned long start, int count)
  306. {
  307. #ifndef VT_BUF_VRAM_ONLY
  308. unsigned int xx, yy, offset;
  309. u16 *p;
  310. p = (u16 *) start;
  311. if (!vc->vc_sw->con_getxy) {
  312. offset = (start - vc->vc_origin) / 2;
  313. xx = offset % vc->vc_cols;
  314. yy = offset / vc->vc_cols;
  315. } else {
  316. int nxx, nyy;
  317. start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
  318. xx = nxx; yy = nyy;
  319. }
  320. for(;;) {
  321. u16 attrib = scr_readw(p) & 0xff00;
  322. int startx = xx;
  323. u16 *q = p;
  324. while (xx < vc->vc_cols && count) {
  325. if (attrib != (scr_readw(p) & 0xff00)) {
  326. if (p > q)
  327. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  328. startx = xx;
  329. q = p;
  330. attrib = scr_readw(p) & 0xff00;
  331. }
  332. p++;
  333. xx++;
  334. count--;
  335. }
  336. if (p > q)
  337. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  338. if (!count)
  339. break;
  340. xx = 0;
  341. yy++;
  342. if (vc->vc_sw->con_getxy) {
  343. p = (u16 *)start;
  344. start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
  345. }
  346. }
  347. #endif
  348. }
  349. void update_region(struct vc_data *vc, unsigned long start, int count)
  350. {
  351. WARN_CONSOLE_UNLOCKED();
  352. if (DO_UPDATE(vc)) {
  353. hide_cursor(vc);
  354. do_update_region(vc, start, count);
  355. set_cursor(vc);
  356. }
  357. }
  358. /* Structure of attributes is hardware-dependent */
  359. static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
  360. u8 _underline, u8 _reverse, u8 _italic)
  361. {
  362. if (vc->vc_sw->con_build_attr)
  363. return vc->vc_sw->con_build_attr(vc, _color, _intensity,
  364. _blink, _underline, _reverse, _italic);
  365. #ifndef VT_BUF_VRAM_ONLY
  366. /*
  367. * ++roman: I completely changed the attribute format for monochrome
  368. * mode (!can_do_color). The formerly used MDA (monochrome display
  369. * adapter) format didn't allow the combination of certain effects.
  370. * Now the attribute is just a bit vector:
  371. * Bit 0..1: intensity (0..2)
  372. * Bit 2 : underline
  373. * Bit 3 : reverse
  374. * Bit 7 : blink
  375. */
  376. {
  377. u8 a = _color;
  378. if (!vc->vc_can_do_color)
  379. return _intensity |
  380. (_italic ? 2 : 0) |
  381. (_underline ? 4 : 0) |
  382. (_reverse ? 8 : 0) |
  383. (_blink ? 0x80 : 0);
  384. if (_italic)
  385. a = (a & 0xF0) | vc->vc_itcolor;
  386. else if (_underline)
  387. a = (a & 0xf0) | vc->vc_ulcolor;
  388. else if (_intensity == 0)
  389. a = (a & 0xf0) | vc->vc_ulcolor;
  390. if (_reverse)
  391. a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
  392. if (_blink)
  393. a ^= 0x80;
  394. if (_intensity == 2)
  395. a ^= 0x08;
  396. if (vc->vc_hi_font_mask == 0x100)
  397. a <<= 1;
  398. return a;
  399. }
  400. #else
  401. return 0;
  402. #endif
  403. }
  404. static void update_attr(struct vc_data *vc)
  405. {
  406. vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
  407. vc->vc_blink, vc->vc_underline,
  408. vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
  409. vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
  410. }
  411. /* Note: inverting the screen twice should revert to the original state */
  412. void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
  413. {
  414. unsigned short *p;
  415. WARN_CONSOLE_UNLOCKED();
  416. count /= 2;
  417. p = screenpos(vc, offset, viewed);
  418. if (vc->vc_sw->con_invert_region)
  419. vc->vc_sw->con_invert_region(vc, p, count);
  420. #ifndef VT_BUF_VRAM_ONLY
  421. else {
  422. u16 *q = p;
  423. int cnt = count;
  424. u16 a;
  425. if (!vc->vc_can_do_color) {
  426. while (cnt--) {
  427. a = scr_readw(q);
  428. a ^= 0x0800;
  429. scr_writew(a, q);
  430. q++;
  431. }
  432. } else if (vc->vc_hi_font_mask == 0x100) {
  433. while (cnt--) {
  434. a = scr_readw(q);
  435. a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
  436. scr_writew(a, q);
  437. q++;
  438. }
  439. } else {
  440. while (cnt--) {
  441. a = scr_readw(q);
  442. a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
  443. scr_writew(a, q);
  444. q++;
  445. }
  446. }
  447. }
  448. #endif
  449. if (DO_UPDATE(vc))
  450. do_update_region(vc, (unsigned long) p, count);
  451. }
  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) vt_set_kbd_mode_bit((vc)->vc_num, (x))
  909. #define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
  910. #define is_kbd(vc, x) vt_get_kbd_mode_bit((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. vt_reset_keyboard(vc->vc_num);
  1485. vc->vc_cursor_type = cur_default;
  1486. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1487. default_attr(vc);
  1488. update_attr(vc);
  1489. vc->vc_tab_stop[0] = 0x01010100;
  1490. vc->vc_tab_stop[1] =
  1491. vc->vc_tab_stop[2] =
  1492. vc->vc_tab_stop[3] =
  1493. vc->vc_tab_stop[4] =
  1494. vc->vc_tab_stop[5] =
  1495. vc->vc_tab_stop[6] =
  1496. vc->vc_tab_stop[7] = 0x01010101;
  1497. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1498. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1499. gotoxy(vc, 0, 0);
  1500. save_cur(vc);
  1501. if (do_clear)
  1502. csi_J(vc, 2);
  1503. }
  1504. /* console_lock is held */
  1505. static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
  1506. {
  1507. /*
  1508. * Control characters can be used in the _middle_
  1509. * of an escape sequence.
  1510. */
  1511. switch (c) {
  1512. case 0:
  1513. return;
  1514. case 7:
  1515. if (vc->vc_bell_duration)
  1516. kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
  1517. return;
  1518. case 8:
  1519. bs(vc);
  1520. return;
  1521. case 9:
  1522. vc->vc_pos -= (vc->vc_x << 1);
  1523. while (vc->vc_x < vc->vc_cols - 1) {
  1524. vc->vc_x++;
  1525. if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
  1526. break;
  1527. }
  1528. vc->vc_pos += (vc->vc_x << 1);
  1529. notify_write(vc, '\t');
  1530. return;
  1531. case 10: case 11: case 12:
  1532. lf(vc);
  1533. if (!is_kbd(vc, lnm))
  1534. return;
  1535. case 13:
  1536. cr(vc);
  1537. return;
  1538. case 14:
  1539. vc->vc_charset = 1;
  1540. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1541. vc->vc_disp_ctrl = 1;
  1542. return;
  1543. case 15:
  1544. vc->vc_charset = 0;
  1545. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1546. vc->vc_disp_ctrl = 0;
  1547. return;
  1548. case 24: case 26:
  1549. vc->vc_state = ESnormal;
  1550. return;
  1551. case 27:
  1552. vc->vc_state = ESesc;
  1553. return;
  1554. case 127:
  1555. del(vc);
  1556. return;
  1557. case 128+27:
  1558. vc->vc_state = ESsquare;
  1559. return;
  1560. }
  1561. switch(vc->vc_state) {
  1562. case ESesc:
  1563. vc->vc_state = ESnormal;
  1564. switch (c) {
  1565. case '[':
  1566. vc->vc_state = ESsquare;
  1567. return;
  1568. case ']':
  1569. vc->vc_state = ESnonstd;
  1570. return;
  1571. case '%':
  1572. vc->vc_state = ESpercent;
  1573. return;
  1574. case 'E':
  1575. cr(vc);
  1576. lf(vc);
  1577. return;
  1578. case 'M':
  1579. ri(vc);
  1580. return;
  1581. case 'D':
  1582. lf(vc);
  1583. return;
  1584. case 'H':
  1585. vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
  1586. return;
  1587. case 'Z':
  1588. respond_ID(tty);
  1589. return;
  1590. case '7':
  1591. save_cur(vc);
  1592. return;
  1593. case '8':
  1594. restore_cur(vc);
  1595. return;
  1596. case '(':
  1597. vc->vc_state = ESsetG0;
  1598. return;
  1599. case ')':
  1600. vc->vc_state = ESsetG1;
  1601. return;
  1602. case '#':
  1603. vc->vc_state = EShash;
  1604. return;
  1605. case 'c':
  1606. reset_terminal(vc, 1);
  1607. return;
  1608. case '>': /* Numeric keypad */
  1609. clr_kbd(vc, kbdapplic);
  1610. return;
  1611. case '=': /* Appl. keypad */
  1612. set_kbd(vc, kbdapplic);
  1613. return;
  1614. }
  1615. return;
  1616. case ESnonstd:
  1617. if (c=='P') { /* palette escape sequence */
  1618. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1619. vc->vc_par[vc->vc_npar] = 0;
  1620. vc->vc_npar = 0;
  1621. vc->vc_state = ESpalette;
  1622. return;
  1623. } else if (c=='R') { /* reset palette */
  1624. reset_palette(vc);
  1625. vc->vc_state = ESnormal;
  1626. } else
  1627. vc->vc_state = ESnormal;
  1628. return;
  1629. case ESpalette:
  1630. if (isxdigit(c)) {
  1631. vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
  1632. if (vc->vc_npar == 7) {
  1633. int i = vc->vc_par[0] * 3, j = 1;
  1634. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1635. vc->vc_palette[i++] += vc->vc_par[j++];
  1636. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1637. vc->vc_palette[i++] += vc->vc_par[j++];
  1638. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1639. vc->vc_palette[i] += vc->vc_par[j];
  1640. set_palette(vc);
  1641. vc->vc_state = ESnormal;
  1642. }
  1643. } else
  1644. vc->vc_state = ESnormal;
  1645. return;
  1646. case ESsquare:
  1647. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1648. vc->vc_par[vc->vc_npar] = 0;
  1649. vc->vc_npar = 0;
  1650. vc->vc_state = ESgetpars;
  1651. if (c == '[') { /* Function key */
  1652. vc->vc_state=ESfunckey;
  1653. return;
  1654. }
  1655. vc->vc_ques = (c == '?');
  1656. if (vc->vc_ques)
  1657. return;
  1658. case ESgetpars:
  1659. if (c == ';' && vc->vc_npar < NPAR - 1) {
  1660. vc->vc_npar++;
  1661. return;
  1662. } else if (c>='0' && c<='9') {
  1663. vc->vc_par[vc->vc_npar] *= 10;
  1664. vc->vc_par[vc->vc_npar] += c - '0';
  1665. return;
  1666. } else
  1667. vc->vc_state = ESgotpars;
  1668. case ESgotpars:
  1669. vc->vc_state = ESnormal;
  1670. switch(c) {
  1671. case 'h':
  1672. set_mode(vc, 1);
  1673. return;
  1674. case 'l':
  1675. set_mode(vc, 0);
  1676. return;
  1677. case 'c':
  1678. if (vc->vc_ques) {
  1679. if (vc->vc_par[0])
  1680. vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
  1681. else
  1682. vc->vc_cursor_type = cur_default;
  1683. return;
  1684. }
  1685. break;
  1686. case 'm':
  1687. if (vc->vc_ques) {
  1688. clear_selection();
  1689. if (vc->vc_par[0])
  1690. vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
  1691. else
  1692. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1693. return;
  1694. }
  1695. break;
  1696. case 'n':
  1697. if (!vc->vc_ques) {
  1698. if (vc->vc_par[0] == 5)
  1699. status_report(tty);
  1700. else if (vc->vc_par[0] == 6)
  1701. cursor_report(vc, tty);
  1702. }
  1703. return;
  1704. }
  1705. if (vc->vc_ques) {
  1706. vc->vc_ques = 0;
  1707. return;
  1708. }
  1709. switch(c) {
  1710. case 'G': case '`':
  1711. if (vc->vc_par[0])
  1712. vc->vc_par[0]--;
  1713. gotoxy(vc, vc->vc_par[0], vc->vc_y);
  1714. return;
  1715. case 'A':
  1716. if (!vc->vc_par[0])
  1717. vc->vc_par[0]++;
  1718. gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
  1719. return;
  1720. case 'B': case 'e':
  1721. if (!vc->vc_par[0])
  1722. vc->vc_par[0]++;
  1723. gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
  1724. return;
  1725. case 'C': case 'a':
  1726. if (!vc->vc_par[0])
  1727. vc->vc_par[0]++;
  1728. gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
  1729. return;
  1730. case 'D':
  1731. if (!vc->vc_par[0])
  1732. vc->vc_par[0]++;
  1733. gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
  1734. return;
  1735. case 'E':
  1736. if (!vc->vc_par[0])
  1737. vc->vc_par[0]++;
  1738. gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
  1739. return;
  1740. case 'F':
  1741. if (!vc->vc_par[0])
  1742. vc->vc_par[0]++;
  1743. gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
  1744. return;
  1745. case 'd':
  1746. if (vc->vc_par[0])
  1747. vc->vc_par[0]--;
  1748. gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
  1749. return;
  1750. case 'H': case 'f':
  1751. if (vc->vc_par[0])
  1752. vc->vc_par[0]--;
  1753. if (vc->vc_par[1])
  1754. vc->vc_par[1]--;
  1755. gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
  1756. return;
  1757. case 'J':
  1758. csi_J(vc, vc->vc_par[0]);
  1759. return;
  1760. case 'K':
  1761. csi_K(vc, vc->vc_par[0]);
  1762. return;
  1763. case 'L':
  1764. csi_L(vc, vc->vc_par[0]);
  1765. return;
  1766. case 'M':
  1767. csi_M(vc, vc->vc_par[0]);
  1768. return;
  1769. case 'P':
  1770. csi_P(vc, vc->vc_par[0]);
  1771. return;
  1772. case 'c':
  1773. if (!vc->vc_par[0])
  1774. respond_ID(tty);
  1775. return;
  1776. case 'g':
  1777. if (!vc->vc_par[0])
  1778. vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
  1779. else if (vc->vc_par[0] == 3) {
  1780. vc->vc_tab_stop[0] =
  1781. vc->vc_tab_stop[1] =
  1782. vc->vc_tab_stop[2] =
  1783. vc->vc_tab_stop[3] =
  1784. vc->vc_tab_stop[4] =
  1785. vc->vc_tab_stop[5] =
  1786. vc->vc_tab_stop[6] =
  1787. vc->vc_tab_stop[7] = 0;
  1788. }
  1789. return;
  1790. case 'm':
  1791. csi_m(vc);
  1792. return;
  1793. case 'q': /* DECLL - but only 3 leds */
  1794. /* map 0,1,2,3 to 0,1,2,4 */
  1795. if (vc->vc_par[0] < 4)
  1796. vt_set_led_state(vc->vc_num,
  1797. (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
  1798. return;
  1799. case 'r':
  1800. if (!vc->vc_par[0])
  1801. vc->vc_par[0]++;
  1802. if (!vc->vc_par[1])
  1803. vc->vc_par[1] = vc->vc_rows;
  1804. /* Minimum allowed region is 2 lines */
  1805. if (vc->vc_par[0] < vc->vc_par[1] &&
  1806. vc->vc_par[1] <= vc->vc_rows) {
  1807. vc->vc_top = vc->vc_par[0] - 1;
  1808. vc->vc_bottom = vc->vc_par[1];
  1809. gotoxay(vc, 0, 0);
  1810. }
  1811. return;
  1812. case 's':
  1813. save_cur(vc);
  1814. return;
  1815. case 'u':
  1816. restore_cur(vc);
  1817. return;
  1818. case 'X':
  1819. csi_X(vc, vc->vc_par[0]);
  1820. return;
  1821. case '@':
  1822. csi_at(vc, vc->vc_par[0]);
  1823. return;
  1824. case ']': /* setterm functions */
  1825. setterm_command(vc);
  1826. return;
  1827. }
  1828. return;
  1829. case ESpercent:
  1830. vc->vc_state = ESnormal;
  1831. switch (c) {
  1832. case '@': /* defined in ISO 2022 */
  1833. vc->vc_utf = 0;
  1834. return;
  1835. case 'G': /* prelim official escape code */
  1836. case '8': /* retained for compatibility */
  1837. vc->vc_utf = 1;
  1838. return;
  1839. }
  1840. return;
  1841. case ESfunckey:
  1842. vc->vc_state = ESnormal;
  1843. return;
  1844. case EShash:
  1845. vc->vc_state = ESnormal;
  1846. if (c == '8') {
  1847. /* DEC screen alignment test. kludge :-) */
  1848. vc->vc_video_erase_char =
  1849. (vc->vc_video_erase_char & 0xff00) | 'E';
  1850. csi_J(vc, 2);
  1851. vc->vc_video_erase_char =
  1852. (vc->vc_video_erase_char & 0xff00) | ' ';
  1853. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  1854. }
  1855. return;
  1856. case ESsetG0:
  1857. if (c == '0')
  1858. vc->vc_G0_charset = GRAF_MAP;
  1859. else if (c == 'B')
  1860. vc->vc_G0_charset = LAT1_MAP;
  1861. else if (c == 'U')
  1862. vc->vc_G0_charset = IBMPC_MAP;
  1863. else if (c == 'K')
  1864. vc->vc_G0_charset = USER_MAP;
  1865. if (vc->vc_charset == 0)
  1866. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1867. vc->vc_state = ESnormal;
  1868. return;
  1869. case ESsetG1:
  1870. if (c == '0')
  1871. vc->vc_G1_charset = GRAF_MAP;
  1872. else if (c == 'B')
  1873. vc->vc_G1_charset = LAT1_MAP;
  1874. else if (c == 'U')
  1875. vc->vc_G1_charset = IBMPC_MAP;
  1876. else if (c == 'K')
  1877. vc->vc_G1_charset = USER_MAP;
  1878. if (vc->vc_charset == 1)
  1879. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1880. vc->vc_state = ESnormal;
  1881. return;
  1882. default:
  1883. vc->vc_state = ESnormal;
  1884. }
  1885. }
  1886. /* is_double_width() is based on the wcwidth() implementation by
  1887. * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
  1888. * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
  1889. */
  1890. struct interval {
  1891. uint32_t first;
  1892. uint32_t last;
  1893. };
  1894. static int bisearch(uint32_t ucs, const struct interval *table, int max)
  1895. {
  1896. int min = 0;
  1897. int mid;
  1898. if (ucs < table[0].first || ucs > table[max].last)
  1899. return 0;
  1900. while (max >= min) {
  1901. mid = (min + max) / 2;
  1902. if (ucs > table[mid].last)
  1903. min = mid + 1;
  1904. else if (ucs < table[mid].first)
  1905. max = mid - 1;
  1906. else
  1907. return 1;
  1908. }
  1909. return 0;
  1910. }
  1911. static int is_double_width(uint32_t ucs)
  1912. {
  1913. static const struct interval double_width[] = {
  1914. { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
  1915. { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
  1916. { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
  1917. { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
  1918. };
  1919. return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
  1920. }
  1921. /* acquires console_lock */
  1922. static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1923. {
  1924. #ifdef VT_BUF_VRAM_ONLY
  1925. #define FLUSH do { } while(0);
  1926. #else
  1927. #define FLUSH if (draw_x >= 0) { \
  1928. vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
  1929. draw_x = -1; \
  1930. }
  1931. #endif
  1932. int c, tc, ok, n = 0, draw_x = -1;
  1933. unsigned int currcons;
  1934. unsigned long draw_from = 0, draw_to = 0;
  1935. struct vc_data *vc;
  1936. unsigned char vc_attr;
  1937. struct vt_notifier_param param;
  1938. uint8_t rescan;
  1939. uint8_t inverse;
  1940. uint8_t width;
  1941. u16 himask, charmask;
  1942. if (in_interrupt())
  1943. return count;
  1944. might_sleep();
  1945. console_lock();
  1946. vc = tty->driver_data;
  1947. if (vc == NULL) {
  1948. printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
  1949. console_unlock();
  1950. return 0;
  1951. }
  1952. currcons = vc->vc_num;
  1953. if (!vc_cons_allocated(currcons)) {
  1954. /* could this happen? */
  1955. pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
  1956. console_unlock();
  1957. return 0;
  1958. }
  1959. himask = vc->vc_hi_font_mask;
  1960. charmask = himask ? 0x1ff : 0xff;
  1961. /* undraw cursor first */
  1962. if (IS_FG(vc))
  1963. hide_cursor(vc);
  1964. param.vc = vc;
  1965. while (!tty->stopped && count) {
  1966. int orig = *buf;
  1967. c = orig;
  1968. buf++;
  1969. n++;
  1970. count--;
  1971. rescan = 0;
  1972. inverse = 0;
  1973. width = 1;
  1974. /* Do no translation at all in control states */
  1975. if (vc->vc_state != ESnormal) {
  1976. tc = c;
  1977. } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
  1978. /* Combine UTF-8 into Unicode in vc_utf_char.
  1979. * vc_utf_count is the number of continuation bytes still
  1980. * expected to arrive.
  1981. * vc_npar is the number of continuation bytes arrived so
  1982. * far
  1983. */
  1984. rescan_last_byte:
  1985. if ((c & 0xc0) == 0x80) {
  1986. /* Continuation byte received */
  1987. static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
  1988. if (vc->vc_utf_count) {
  1989. vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
  1990. vc->vc_npar++;
  1991. if (--vc->vc_utf_count) {
  1992. /* Still need some bytes */
  1993. continue;
  1994. }
  1995. /* Got a whole character */
  1996. c = vc->vc_utf_char;
  1997. /* Reject overlong sequences */
  1998. if (c <= utf8_length_changes[vc->vc_npar - 1] ||
  1999. c > utf8_length_changes[vc->vc_npar])
  2000. c = 0xfffd;
  2001. } else {
  2002. /* Unexpected continuation byte */
  2003. vc->vc_utf_count = 0;
  2004. c = 0xfffd;
  2005. }
  2006. } else {
  2007. /* Single ASCII byte or first byte of a sequence received */
  2008. if (vc->vc_utf_count) {
  2009. /* Continuation byte expected */
  2010. rescan = 1;
  2011. vc->vc_utf_count = 0;
  2012. c = 0xfffd;
  2013. } else if (c > 0x7f) {
  2014. /* First byte of a multibyte sequence received */
  2015. vc->vc_npar = 0;
  2016. if ((c & 0xe0) == 0xc0) {
  2017. vc->vc_utf_count = 1;
  2018. vc->vc_utf_char = (c & 0x1f);
  2019. } else if ((c & 0xf0) == 0xe0) {
  2020. vc->vc_utf_count = 2;
  2021. vc->vc_utf_char = (c & 0x0f);
  2022. } else if ((c & 0xf8) == 0xf0) {
  2023. vc->vc_utf_count = 3;
  2024. vc->vc_utf_char = (c & 0x07);
  2025. } else if ((c & 0xfc) == 0xf8) {
  2026. vc->vc_utf_count = 4;
  2027. vc->vc_utf_char = (c & 0x03);
  2028. } else if ((c & 0xfe) == 0xfc) {
  2029. vc->vc_utf_count = 5;
  2030. vc->vc_utf_char = (c & 0x01);
  2031. } else {
  2032. /* 254 and 255 are invalid */
  2033. c = 0xfffd;
  2034. }
  2035. if (vc->vc_utf_count) {
  2036. /* Still need some bytes */
  2037. continue;
  2038. }
  2039. }
  2040. /* Nothing to do if an ASCII byte was received */
  2041. }
  2042. /* End of UTF-8 decoding. */
  2043. /* c is the received character, or U+FFFD for invalid sequences. */
  2044. /* Replace invalid Unicode code points with U+FFFD too */
  2045. if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
  2046. c = 0xfffd;
  2047. tc = c;
  2048. } else { /* no utf or alternate charset mode */
  2049. tc = vc_translate(vc, c);
  2050. }
  2051. param.c = tc;
  2052. if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
  2053. &param) == NOTIFY_STOP)
  2054. continue;
  2055. /* If the original code was a control character we
  2056. * only allow a glyph to be displayed if the code is
  2057. * not normally used (such as for cursor movement) or
  2058. * if the disp_ctrl mode has been explicitly enabled.
  2059. * Certain characters (as given by the CTRL_ALWAYS
  2060. * bitmap) are always displayed as control characters,
  2061. * as the console would be pretty useless without
  2062. * them; to display an arbitrary font position use the
  2063. * direct-to-font zone in UTF-8 mode.
  2064. */
  2065. ok = tc && (c >= 32 ||
  2066. !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
  2067. vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
  2068. && (c != 127 || vc->vc_disp_ctrl)
  2069. && (c != 128+27);
  2070. if (vc->vc_state == ESnormal && ok) {
  2071. if (vc->vc_utf && !vc->vc_disp_ctrl) {
  2072. if (is_double_width(c))
  2073. width = 2;
  2074. }
  2075. /* Now try to find out how to display it */
  2076. tc = conv_uni_to_pc(vc, tc);
  2077. if (tc & ~charmask) {
  2078. if (tc == -1 || tc == -2) {
  2079. continue; /* nothing to display */
  2080. }
  2081. /* Glyph not found */
  2082. if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
  2083. /* In legacy mode use the glyph we get by a 1:1 mapping.
  2084. This would make absolutely no sense with Unicode in mind,
  2085. but do this for ASCII characters since a font may lack
  2086. Unicode mapping info and we don't want to end up with
  2087. having question marks only. */
  2088. tc = c;
  2089. } else {
  2090. /* Display U+FFFD. If it's not found, display an inverse question mark. */
  2091. tc = conv_uni_to_pc(vc, 0xfffd);
  2092. if (tc < 0) {
  2093. inverse = 1;
  2094. tc = conv_uni_to_pc(vc, '?');
  2095. if (tc < 0) tc = '?';
  2096. }
  2097. }
  2098. }
  2099. if (!inverse) {
  2100. vc_attr = vc->vc_attr;
  2101. } else {
  2102. /* invert vc_attr */
  2103. if (!vc->vc_can_do_color) {
  2104. vc_attr = (vc->vc_attr) ^ 0x08;
  2105. } else if (vc->vc_hi_font_mask == 0x100) {
  2106. vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
  2107. } else {
  2108. vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
  2109. }
  2110. FLUSH
  2111. }
  2112. while (1) {
  2113. if (vc->vc_need_wrap || vc->vc_decim)
  2114. FLUSH
  2115. if (vc->vc_need_wrap) {
  2116. cr(vc);
  2117. lf(vc);
  2118. }
  2119. if (vc->vc_decim)
  2120. insert_char(vc, 1);
  2121. scr_writew(himask ?
  2122. ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
  2123. (vc_attr << 8) + tc,
  2124. (u16 *) vc->vc_pos);
  2125. if (DO_UPDATE(vc) && draw_x < 0) {
  2126. draw_x = vc->vc_x;
  2127. draw_from = vc->vc_pos;
  2128. }
  2129. if (vc->vc_x == vc->vc_cols - 1) {
  2130. vc->vc_need_wrap = vc->vc_decawm;
  2131. draw_to = vc->vc_pos + 2;
  2132. } else {
  2133. vc->vc_x++;
  2134. draw_to = (vc->vc_pos += 2);
  2135. }
  2136. if (!--width) break;
  2137. tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
  2138. if (tc < 0) tc = ' ';
  2139. }
  2140. notify_write(vc, c);
  2141. if (inverse) {
  2142. FLUSH
  2143. }
  2144. if (rescan) {
  2145. rescan = 0;
  2146. inverse = 0;
  2147. width = 1;
  2148. c = orig;
  2149. goto rescan_last_byte;
  2150. }
  2151. continue;
  2152. }
  2153. FLUSH
  2154. do_con_trol(tty, vc, orig);
  2155. }
  2156. FLUSH
  2157. console_conditional_schedule();
  2158. console_unlock();
  2159. notify_update(vc);
  2160. return n;
  2161. #undef FLUSH
  2162. }
  2163. /*
  2164. * This is the console switching callback.
  2165. *
  2166. * Doing console switching in a process context allows
  2167. * us to do the switches asynchronously (needed when we want
  2168. * to switch due to a keyboard interrupt). Synchronization
  2169. * with other console code and prevention of re-entrancy is
  2170. * ensured with console_lock.
  2171. */
  2172. static void console_callback(struct work_struct *ignored)
  2173. {
  2174. console_lock();
  2175. if (want_console >= 0) {
  2176. if (want_console != fg_console &&
  2177. vc_cons_allocated(want_console)) {
  2178. hide_cursor(vc_cons[fg_console].d);
  2179. change_console(vc_cons[want_console].d);
  2180. /* we only changed when the console had already
  2181. been allocated - a new console is not created
  2182. in an interrupt routine */
  2183. }
  2184. want_console = -1;
  2185. }
  2186. if (do_poke_blanked_console) { /* do not unblank for a LED change */
  2187. do_poke_blanked_console = 0;
  2188. poke_blanked_console();
  2189. }
  2190. if (scrollback_delta) {
  2191. struct vc_data *vc = vc_cons[fg_console].d;
  2192. clear_selection();
  2193. if (vc->vc_mode == KD_TEXT)
  2194. vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
  2195. scrollback_delta = 0;
  2196. }
  2197. if (blank_timer_expired) {
  2198. do_blank_screen(0);
  2199. blank_timer_expired = 0;
  2200. }
  2201. notify_update(vc_cons[fg_console].d);
  2202. console_unlock();
  2203. }
  2204. int set_console(int nr)
  2205. {
  2206. struct vc_data *vc = vc_cons[fg_console].d;
  2207. if (!vc_cons_allocated(nr) || vt_dont_switch ||
  2208. (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
  2209. /*
  2210. * Console switch will fail in console_callback() or
  2211. * change_console() so there is no point scheduling
  2212. * the callback
  2213. *
  2214. * Existing set_console() users don't check the return
  2215. * value so this shouldn't break anything
  2216. */
  2217. return -EINVAL;
  2218. }
  2219. want_console = nr;
  2220. schedule_console_callback();
  2221. return 0;
  2222. }
  2223. struct tty_driver *console_driver;
  2224. #ifdef CONFIG_VT_CONSOLE
  2225. /**
  2226. * vt_kmsg_redirect() - Sets/gets the kernel message console
  2227. * @new: The new virtual terminal number or -1 if the console should stay
  2228. * unchanged
  2229. *
  2230. * By default, the kernel messages are always printed on the current virtual
  2231. * console. However, the user may modify that default with the
  2232. * TIOCL_SETKMSGREDIRECT ioctl call.
  2233. *
  2234. * This function sets the kernel message console to be @new. It returns the old
  2235. * virtual console number. The virtual terminal number 0 (both as parameter and
  2236. * return value) means no redirection (i.e. always printed on the currently
  2237. * active console).
  2238. *
  2239. * The parameter -1 means that only the current console is returned, but the
  2240. * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
  2241. * case to make the code more understandable.
  2242. *
  2243. * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
  2244. * the parameter and always returns 0.
  2245. */
  2246. int vt_kmsg_redirect(int new)
  2247. {
  2248. static int kmsg_con;
  2249. if (new != -1)
  2250. return xchg(&kmsg_con, new);
  2251. else
  2252. return kmsg_con;
  2253. }
  2254. /*
  2255. * Console on virtual terminal
  2256. *
  2257. * The console must be locked when we get here.
  2258. */
  2259. static void vt_console_print(struct console *co, const char *b, unsigned count)
  2260. {
  2261. struct vc_data *vc = vc_cons[fg_console].d;
  2262. unsigned char c;
  2263. static DEFINE_SPINLOCK(printing_lock);
  2264. const ushort *start;
  2265. ushort cnt = 0;
  2266. ushort myx;
  2267. int kmsg_console;
  2268. /* console busy or not yet initialized */
  2269. if (!printable)
  2270. return;
  2271. if (!spin_trylock(&printing_lock))
  2272. return;
  2273. kmsg_console = vt_get_kmsg_redirect();
  2274. if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
  2275. vc = vc_cons[kmsg_console - 1].d;
  2276. /* read `x' only after setting currcons properly (otherwise
  2277. the `x' macro will read the x of the foreground console). */
  2278. myx = vc->vc_x;
  2279. if (!vc_cons_allocated(fg_console)) {
  2280. /* impossible */
  2281. /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
  2282. goto quit;
  2283. }
  2284. if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
  2285. goto quit;
  2286. /* undraw cursor first */
  2287. if (IS_FG(vc))
  2288. hide_cursor(vc);
  2289. start = (ushort *)vc->vc_pos;
  2290. /* Contrived structure to try to emulate original need_wrap behaviour
  2291. * Problems caused when we have need_wrap set on '\n' character */
  2292. while (count--) {
  2293. c = *b++;
  2294. if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
  2295. if (cnt > 0) {
  2296. if (CON_IS_VISIBLE(vc))
  2297. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  2298. vc->vc_x += cnt;
  2299. if (vc->vc_need_wrap)
  2300. vc->vc_x--;
  2301. cnt = 0;
  2302. }
  2303. if (c == 8) { /* backspace */
  2304. bs(vc);
  2305. start = (ushort *)vc->vc_pos;
  2306. myx = vc->vc_x;
  2307. continue;
  2308. }
  2309. if (c != 13)
  2310. lf(vc);
  2311. cr(vc);
  2312. start = (ushort *)vc->vc_pos;
  2313. myx = vc->vc_x;
  2314. if (c == 10 || c == 13)
  2315. continue;
  2316. }
  2317. scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
  2318. notify_write(vc, c);
  2319. cnt++;
  2320. if (myx == vc->vc_cols - 1) {
  2321. vc->vc_need_wrap = 1;
  2322. continue;
  2323. }
  2324. vc->vc_pos += 2;
  2325. myx++;
  2326. }
  2327. if (cnt > 0) {
  2328. if (CON_IS_VISIBLE(vc))
  2329. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  2330. vc->vc_x += cnt;
  2331. if (vc->vc_x == vc->vc_cols) {
  2332. vc->vc_x--;
  2333. vc->vc_need_wrap = 1;
  2334. }
  2335. }
  2336. set_cursor(vc);
  2337. notify_update(vc);
  2338. quit:
  2339. spin_unlock(&printing_lock);
  2340. }
  2341. static struct tty_driver *vt_console_device(struct console *c, int *index)
  2342. {
  2343. *index = c->index ? c->index-1 : fg_console;
  2344. return console_driver;
  2345. }
  2346. static struct console vt_console_driver = {
  2347. .name = "tty",
  2348. .write = vt_console_print,
  2349. .device = vt_console_device,
  2350. .unblank = unblank_screen,
  2351. .flags = CON_PRINTBUFFER,
  2352. .index = -1,
  2353. };
  2354. #endif
  2355. /*
  2356. * Handling of Linux-specific VC ioctls
  2357. */
  2358. /*
  2359. * Generally a bit racy with respect to console_lock();.
  2360. *
  2361. * There are some functions which don't need it.
  2362. *
  2363. * There are some functions which can sleep for arbitrary periods
  2364. * (paste_selection) but we don't need the lock there anyway.
  2365. *
  2366. * set_selection has locking, and definitely needs it
  2367. */
  2368. int tioclinux(struct tty_struct *tty, unsigned long arg)
  2369. {
  2370. char type, data;
  2371. char __user *p = (char __user *)arg;
  2372. int lines;
  2373. int ret;
  2374. if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
  2375. return -EPERM;
  2376. if (get_user(type, p))
  2377. return -EFAULT;
  2378. ret = 0;
  2379. switch (type)
  2380. {
  2381. case TIOCL_SETSEL:
  2382. console_lock();
  2383. ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
  2384. console_unlock();
  2385. break;
  2386. case TIOCL_PASTESEL:
  2387. ret = paste_selection(tty);
  2388. break;
  2389. case TIOCL_UNBLANKSCREEN:
  2390. console_lock();
  2391. unblank_screen();
  2392. console_unlock();
  2393. break;
  2394. case TIOCL_SELLOADLUT:
  2395. ret = sel_loadlut(p);
  2396. break;
  2397. case TIOCL_GETSHIFTSTATE:
  2398. /*
  2399. * Make it possible to react to Shift+Mousebutton.
  2400. * Note that 'shift_state' is an undocumented
  2401. * kernel-internal variable; programs not closely
  2402. * related to the kernel should not use this.
  2403. */
  2404. data = vt_get_shift_state();
  2405. ret = __put_user(data, p);
  2406. break;
  2407. case TIOCL_GETMOUSEREPORTING:
  2408. data = mouse_reporting();
  2409. ret = __put_user(data, p);
  2410. break;
  2411. case TIOCL_SETVESABLANK:
  2412. ret = set_vesa_blanking(p);
  2413. break;
  2414. case TIOCL_GETKMSGREDIRECT:
  2415. data = vt_get_kmsg_redirect();
  2416. ret = __put_user(data, p);
  2417. break;
  2418. case TIOCL_SETKMSGREDIRECT:
  2419. if (!capable(CAP_SYS_ADMIN)) {
  2420. ret = -EPERM;
  2421. } else {
  2422. if (get_user(data, p+1))
  2423. ret = -EFAULT;
  2424. else
  2425. vt_kmsg_redirect(data);
  2426. }
  2427. break;
  2428. case TIOCL_GETFGCONSOLE:
  2429. ret = fg_console;
  2430. break;
  2431. case TIOCL_SCROLLCONSOLE:
  2432. if (get_user(lines, (s32 __user *)(p+4))) {
  2433. ret = -EFAULT;
  2434. } else {
  2435. scrollfront(vc_cons[fg_console].d, lines);
  2436. ret = 0;
  2437. }
  2438. break;
  2439. case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
  2440. console_lock();
  2441. ignore_poke = 1;
  2442. do_blank_screen(0);
  2443. console_unlock();
  2444. break;
  2445. case TIOCL_BLANKEDSCREEN:
  2446. ret = console_blanked;
  2447. break;
  2448. default:
  2449. ret = -EINVAL;
  2450. break;
  2451. }
  2452. return ret;
  2453. }
  2454. /*
  2455. * /dev/ttyN handling
  2456. */
  2457. static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  2458. {
  2459. int retval;
  2460. retval = do_con_write(tty, buf, count);
  2461. con_flush_chars(tty);
  2462. return retval;
  2463. }
  2464. static int con_put_char(struct tty_struct *tty, unsigned char ch)
  2465. {
  2466. if (in_interrupt())
  2467. return 0; /* n_r3964 calls put_char() from interrupt context */
  2468. return do_con_write(tty, &ch, 1);
  2469. }
  2470. static int con_write_room(struct tty_struct *tty)
  2471. {
  2472. if (tty->stopped)
  2473. return 0;
  2474. return 32768; /* No limit, really; we're not buffering */
  2475. }
  2476. static int con_chars_in_buffer(struct tty_struct *tty)
  2477. {
  2478. return 0; /* we're not buffering */
  2479. }
  2480. /*
  2481. * con_throttle and con_unthrottle are only used for
  2482. * paste_selection(), which has to stuff in a large number of
  2483. * characters...
  2484. */
  2485. static void con_throttle(struct tty_struct *tty)
  2486. {
  2487. }
  2488. static void con_unthrottle(struct tty_struct *tty)
  2489. {
  2490. struct vc_data *vc = tty->driver_data;
  2491. wake_up_interruptible(&vc->paste_wait);
  2492. }
  2493. /*
  2494. * Turn the Scroll-Lock LED on when the tty is stopped
  2495. */
  2496. static void con_stop(struct tty_struct *tty)
  2497. {
  2498. int console_num;
  2499. if (!tty)
  2500. return;
  2501. console_num = tty->index;
  2502. if (!vc_cons_allocated(console_num))
  2503. return;
  2504. vt_kbd_con_stop(console_num);
  2505. }
  2506. /*
  2507. * Turn the Scroll-Lock LED off when the console is started
  2508. */
  2509. static void con_start(struct tty_struct *tty)
  2510. {
  2511. int console_num;
  2512. if (!tty)
  2513. return;
  2514. console_num = tty->index;
  2515. if (!vc_cons_allocated(console_num))
  2516. return;
  2517. vt_kbd_con_start(console_num);
  2518. }
  2519. static void con_flush_chars(struct tty_struct *tty)
  2520. {
  2521. struct vc_data *vc;
  2522. if (in_interrupt()) /* from flush_to_ldisc */
  2523. return;
  2524. /* if we race with con_close(), vt may be null */
  2525. console_lock();
  2526. vc = tty->driver_data;
  2527. if (vc)
  2528. set_cursor(vc);
  2529. console_unlock();
  2530. }
  2531. /*
  2532. * Allocate the console screen memory.
  2533. */
  2534. static int con_open(struct tty_struct *tty, struct file *filp)
  2535. {
  2536. unsigned int currcons = tty->index;
  2537. int ret = 0;
  2538. console_lock();
  2539. if (tty->driver_data == NULL) {
  2540. ret = vc_allocate(currcons);
  2541. if (ret == 0) {
  2542. struct vc_data *vc = vc_cons[currcons].d;
  2543. /* Still being freed */
  2544. if (vc->port.tty) {
  2545. console_unlock();
  2546. return -ERESTARTSYS;
  2547. }
  2548. tty->driver_data = vc;
  2549. vc->port.tty = tty;
  2550. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  2551. tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
  2552. tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
  2553. }
  2554. if (vc->vc_utf)
  2555. tty->termios->c_iflag |= IUTF8;
  2556. else
  2557. tty->termios->c_iflag &= ~IUTF8;
  2558. console_unlock();
  2559. return ret;
  2560. }
  2561. }
  2562. console_unlock();
  2563. return ret;
  2564. }
  2565. static void con_close(struct tty_struct *tty, struct file *filp)
  2566. {
  2567. /* Nothing to do - we defer to shutdown */
  2568. }
  2569. static void con_shutdown(struct tty_struct *tty)
  2570. {
  2571. struct vc_data *vc = tty->driver_data;
  2572. BUG_ON(vc == NULL);
  2573. console_lock();
  2574. vc->port.tty = NULL;
  2575. console_unlock();
  2576. tty_shutdown(tty);
  2577. }
  2578. static int default_italic_color = 2; // green (ASCII)
  2579. static int default_underline_color = 3; // cyan (ASCII)
  2580. module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
  2581. module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
  2582. static void vc_init(struct vc_data *vc, unsigned int rows,
  2583. unsigned int cols, int do_clear)
  2584. {
  2585. int j, k ;
  2586. vc->vc_cols = cols;
  2587. vc->vc_rows = rows;
  2588. vc->vc_size_row = cols << 1;
  2589. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  2590. set_origin(vc);
  2591. vc->vc_pos = vc->vc_origin;
  2592. reset_vc(vc);
  2593. for (j=k=0; j<16; j++) {
  2594. vc->vc_palette[k++] = default_red[j] ;
  2595. vc->vc_palette[k++] = default_grn[j] ;
  2596. vc->vc_palette[k++] = default_blu[j] ;
  2597. }
  2598. vc->vc_def_color = 0x07; /* white */
  2599. vc->vc_ulcolor = default_underline_color;
  2600. vc->vc_itcolor = default_italic_color;
  2601. vc->vc_halfcolor = 0x08; /* grey */
  2602. init_waitqueue_head(&vc->paste_wait);
  2603. reset_terminal(vc, do_clear);
  2604. }
  2605. /*
  2606. * This routine initializes console interrupts, and does nothing
  2607. * else. If you want the screen to clear, call tty_write with
  2608. * the appropriate escape-sequence.
  2609. */
  2610. static int __init con_init(void)
  2611. {
  2612. const char *display_desc = NULL;
  2613. struct vc_data *vc;
  2614. unsigned int currcons = 0, i;
  2615. console_lock();
  2616. if (conswitchp)
  2617. display_desc = conswitchp->con_startup();
  2618. if (!display_desc) {
  2619. fg_console = 0;
  2620. console_unlock();
  2621. return 0;
  2622. }
  2623. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2624. struct con_driver *con_driver = &registered_con_driver[i];
  2625. if (con_driver->con == NULL) {
  2626. con_driver->con = conswitchp;
  2627. con_driver->desc = display_desc;
  2628. con_driver->flag = CON_DRIVER_FLAG_INIT;
  2629. con_driver->first = 0;
  2630. con_driver->last = MAX_NR_CONSOLES - 1;
  2631. break;
  2632. }
  2633. }
  2634. for (i = 0; i < MAX_NR_CONSOLES; i++)
  2635. con_driver_map[i] = conswitchp;
  2636. if (blankinterval) {
  2637. blank_state = blank_normal_wait;
  2638. mod_timer(&console_timer, jiffies + (blankinterval * HZ));
  2639. }
  2640. for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
  2641. vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
  2642. INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
  2643. tty_port_init(&vc->port);
  2644. visual_init(vc, currcons, 1);
  2645. vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
  2646. vc_init(vc, vc->vc_rows, vc->vc_cols,
  2647. currcons || !vc->vc_sw->con_save_screen);
  2648. }
  2649. currcons = fg_console = 0;
  2650. master_display_fg = vc = vc_cons[currcons].d;
  2651. set_origin(vc);
  2652. save_screen(vc);
  2653. gotoxy(vc, vc->vc_x, vc->vc_y);
  2654. csi_J(vc, 0);
  2655. update_screen(vc);
  2656. pr_info("Console: %s %s %dx%d",
  2657. vc->vc_can_do_color ? "colour" : "mono",
  2658. display_desc, vc->vc_cols, vc->vc_rows);
  2659. printable = 1;
  2660. printk("\n");
  2661. console_unlock();
  2662. #ifdef CONFIG_VT_CONSOLE
  2663. register_console(&vt_console_driver);
  2664. #endif
  2665. return 0;
  2666. }
  2667. console_initcall(con_init);
  2668. static const struct tty_operations con_ops = {
  2669. .open = con_open,
  2670. .close = con_close,
  2671. .write = con_write,
  2672. .write_room = con_write_room,
  2673. .put_char = con_put_char,
  2674. .flush_chars = con_flush_chars,
  2675. .chars_in_buffer = con_chars_in_buffer,
  2676. .ioctl = vt_ioctl,
  2677. #ifdef CONFIG_COMPAT
  2678. .compat_ioctl = vt_compat_ioctl,
  2679. #endif
  2680. .stop = con_stop,
  2681. .start = con_start,
  2682. .throttle = con_throttle,
  2683. .unthrottle = con_unthrottle,
  2684. .resize = vt_resize,
  2685. .shutdown = con_shutdown
  2686. };
  2687. static struct cdev vc0_cdev;
  2688. static ssize_t show_tty_active(struct device *dev,
  2689. struct device_attribute *attr, char *buf)
  2690. {
  2691. return sprintf(buf, "tty%d\n", fg_console + 1);
  2692. }
  2693. static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
  2694. int __init vty_init(const struct file_operations *console_fops)
  2695. {
  2696. cdev_init(&vc0_cdev, console_fops);
  2697. if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
  2698. register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
  2699. panic("Couldn't register /dev/tty0 driver\n");
  2700. tty0dev = device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
  2701. if (IS_ERR(tty0dev))
  2702. tty0dev = NULL;
  2703. else
  2704. WARN_ON(device_create_file(tty0dev, &dev_attr_active) < 0);
  2705. vcs_init();
  2706. console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
  2707. if (!console_driver)
  2708. panic("Couldn't allocate console driver\n");
  2709. console_driver->owner = THIS_MODULE;
  2710. console_driver->name = "tty";
  2711. console_driver->name_base = 1;
  2712. console_driver->major = TTY_MAJOR;
  2713. console_driver->minor_start = 1;
  2714. console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  2715. console_driver->init_termios = tty_std_termios;
  2716. if (default_utf8)
  2717. console_driver->init_termios.c_iflag |= IUTF8;
  2718. console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  2719. tty_set_operations(console_driver, &con_ops);
  2720. if (tty_register_driver(console_driver))
  2721. panic("Couldn't register console driver\n");
  2722. kbd_init();
  2723. console_map_init();
  2724. #ifdef CONFIG_MDA_CONSOLE
  2725. mda_console_init();
  2726. #endif
  2727. return 0;
  2728. }
  2729. #ifndef VT_SINGLE_DRIVER
  2730. static struct class *vtconsole_class;
  2731. static int bind_con_driver(const struct consw *csw, int first, int last,
  2732. int deflt)
  2733. {
  2734. struct module *owner = csw->owner;
  2735. const char *desc = NULL;
  2736. struct con_driver *con_driver;
  2737. int i, j = -1, k = -1, retval = -ENODEV;
  2738. if (!try_module_get(owner))
  2739. return -ENODEV;
  2740. console_lock();
  2741. /* check if driver is registered */
  2742. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2743. con_driver = &registered_con_driver[i];
  2744. if (con_driver->con == csw) {
  2745. desc = con_driver->desc;
  2746. retval = 0;
  2747. break;
  2748. }
  2749. }
  2750. if (retval)
  2751. goto err;
  2752. if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
  2753. csw->con_startup();
  2754. con_driver->flag |= CON_DRIVER_FLAG_INIT;
  2755. }
  2756. if (deflt) {
  2757. if (conswitchp)
  2758. module_put(conswitchp->owner);
  2759. __module_get(owner);
  2760. conswitchp = csw;
  2761. }
  2762. first = max(first, con_driver->first);
  2763. last = min(last, con_driver->last);
  2764. for (i = first; i <= last; i++) {
  2765. int old_was_color;
  2766. struct vc_data *vc = vc_cons[i].d;
  2767. if (con_driver_map[i])
  2768. module_put(con_driver_map[i]->owner);
  2769. __module_get(owner);
  2770. con_driver_map[i] = csw;
  2771. if (!vc || !vc->vc_sw)
  2772. continue;
  2773. j = i;
  2774. if (CON_IS_VISIBLE(vc)) {
  2775. k = i;
  2776. save_screen(vc);
  2777. }
  2778. old_was_color = vc->vc_can_do_color;
  2779. vc->vc_sw->con_deinit(vc);
  2780. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  2781. visual_init(vc, i, 0);
  2782. set_origin(vc);
  2783. update_attr(vc);
  2784. /* If the console changed between mono <-> color, then
  2785. * the attributes in the screenbuf will be wrong. The
  2786. * following resets all attributes to something sane.
  2787. */
  2788. if (old_was_color != vc->vc_can_do_color)
  2789. clear_buffer_attributes(vc);
  2790. }
  2791. pr_info("Console: switching ");
  2792. if (!deflt)
  2793. printk("consoles %d-%d ", first+1, last+1);
  2794. if (j >= 0) {
  2795. struct vc_data *vc = vc_cons[j].d;
  2796. printk("to %s %s %dx%d\n",
  2797. vc->vc_can_do_color ? "colour" : "mono",
  2798. desc, vc->vc_cols, vc->vc_rows);
  2799. if (k >= 0) {
  2800. vc = vc_cons[k].d;
  2801. update_screen(vc);
  2802. }
  2803. } else
  2804. printk("to %s\n", desc);
  2805. retval = 0;
  2806. err:
  2807. console_unlock();
  2808. module_put(owner);
  2809. return retval;
  2810. };
  2811. #ifdef CONFIG_VT_HW_CONSOLE_BINDING
  2812. static int con_is_graphics(const struct consw *csw, int first, int last)
  2813. {
  2814. int i, retval = 0;
  2815. for (i = first; i <= last; i++) {
  2816. struct vc_data *vc = vc_cons[i].d;
  2817. if (vc && vc->vc_mode == KD_GRAPHICS) {
  2818. retval = 1;
  2819. break;
  2820. }
  2821. }
  2822. return retval;
  2823. }
  2824. /**
  2825. * unbind_con_driver - unbind a console driver
  2826. * @csw: pointer to console driver to unregister
  2827. * @first: first in range of consoles that @csw should be unbound from
  2828. * @last: last in range of consoles that @csw should be unbound from
  2829. * @deflt: should next bound console driver be default after @csw is unbound?
  2830. *
  2831. * To unbind a driver from all possible consoles, pass 0 as @first and
  2832. * %MAX_NR_CONSOLES as @last.
  2833. *
  2834. * @deflt controls whether the console that ends up replacing @csw should be
  2835. * the default console.
  2836. *
  2837. * RETURNS:
  2838. * -ENODEV if @csw isn't a registered console driver or can't be unregistered
  2839. * or 0 on success.
  2840. */
  2841. int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
  2842. {
  2843. struct module *owner = csw->owner;
  2844. const struct consw *defcsw = NULL;
  2845. struct con_driver *con_driver = NULL, *con_back = NULL;
  2846. int i, retval = -ENODEV;
  2847. if (!try_module_get(owner))
  2848. return -ENODEV;
  2849. console_lock();
  2850. /* check if driver is registered and if it is unbindable */
  2851. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2852. con_driver = &registered_con_driver[i];
  2853. if (con_driver->con == csw &&
  2854. con_driver->flag & CON_DRIVER_FLAG_MODULE) {
  2855. retval = 0;
  2856. break;
  2857. }
  2858. }
  2859. if (retval) {
  2860. console_unlock();
  2861. goto err;
  2862. }
  2863. retval = -ENODEV;
  2864. /* check if backup driver exists */
  2865. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2866. con_back = &registered_con_driver[i];
  2867. if (con_back->con &&
  2868. !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
  2869. defcsw = con_back->con;
  2870. retval = 0;
  2871. break;
  2872. }
  2873. }
  2874. if (retval) {
  2875. console_unlock();
  2876. goto err;
  2877. }
  2878. if (!con_is_bound(csw)) {
  2879. console_unlock();
  2880. goto err;
  2881. }
  2882. first = max(first, con_driver->first);
  2883. last = min(last, con_driver->last);
  2884. for (i = first; i <= last; i++) {
  2885. if (con_driver_map[i] == csw) {
  2886. module_put(csw->owner);
  2887. con_driver_map[i] = NULL;
  2888. }
  2889. }
  2890. if (!con_is_bound(defcsw)) {
  2891. const struct consw *defconsw = conswitchp;
  2892. defcsw->con_startup();
  2893. con_back->flag |= CON_DRIVER_FLAG_INIT;
  2894. /*
  2895. * vgacon may change the default driver to point
  2896. * to dummycon, we restore it here...
  2897. */
  2898. conswitchp = defconsw;
  2899. }
  2900. if (!con_is_bound(csw))
  2901. con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
  2902. console_unlock();
  2903. /* ignore return value, binding should not fail */
  2904. bind_con_driver(defcsw, first, last, deflt);
  2905. err:
  2906. module_put(owner);
  2907. return retval;
  2908. }
  2909. EXPORT_SYMBOL(unbind_con_driver);
  2910. static int vt_bind(struct con_driver *con)
  2911. {
  2912. const struct consw *defcsw = NULL, *csw = NULL;
  2913. int i, more = 1, first = -1, last = -1, deflt = 0;
  2914. if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
  2915. con_is_graphics(con->con, con->first, con->last))
  2916. goto err;
  2917. csw = con->con;
  2918. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2919. struct con_driver *con = &registered_con_driver[i];
  2920. if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
  2921. defcsw = con->con;
  2922. break;
  2923. }
  2924. }
  2925. if (!defcsw)
  2926. goto err;
  2927. while (more) {
  2928. more = 0;
  2929. for (i = con->first; i <= con->last; i++) {
  2930. if (con_driver_map[i] == defcsw) {
  2931. if (first == -1)
  2932. first = i;
  2933. last = i;
  2934. more = 1;
  2935. } else if (first != -1)
  2936. break;
  2937. }
  2938. if (first == 0 && last == MAX_NR_CONSOLES -1)
  2939. deflt = 1;
  2940. if (first != -1)
  2941. bind_con_driver(csw, first, last, deflt);
  2942. first = -1;
  2943. last = -1;
  2944. deflt = 0;
  2945. }
  2946. err:
  2947. return 0;
  2948. }
  2949. static int vt_unbind(struct con_driver *con)
  2950. {
  2951. const struct consw *csw = NULL;
  2952. int i, more = 1, first = -1, last = -1, deflt = 0;
  2953. if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
  2954. con_is_graphics(con->con, con->first, con->last))
  2955. goto err;
  2956. csw = con->con;
  2957. while (more) {
  2958. more = 0;
  2959. for (i = con->first; i <= con->last; i++) {
  2960. if (con_driver_map[i] == csw) {
  2961. if (first == -1)
  2962. first = i;
  2963. last = i;
  2964. more = 1;
  2965. } else if (first != -1)
  2966. break;
  2967. }
  2968. if (first == 0 && last == MAX_NR_CONSOLES -1)
  2969. deflt = 1;
  2970. if (first != -1)
  2971. unbind_con_driver(csw, first, last, deflt);
  2972. first = -1;
  2973. last = -1;
  2974. deflt = 0;
  2975. }
  2976. err:
  2977. return 0;
  2978. }
  2979. #else
  2980. static inline int vt_bind(struct con_driver *con)
  2981. {
  2982. return 0;
  2983. }
  2984. static inline int vt_unbind(struct con_driver *con)
  2985. {
  2986. return 0;
  2987. }
  2988. #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
  2989. static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
  2990. const char *buf, size_t count)
  2991. {
  2992. struct con_driver *con = dev_get_drvdata(dev);
  2993. int bind = simple_strtoul(buf, NULL, 0);
  2994. if (bind)
  2995. vt_bind(con);
  2996. else
  2997. vt_unbind(con);
  2998. return count;
  2999. }
  3000. static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
  3001. char *buf)
  3002. {
  3003. struct con_driver *con = dev_get_drvdata(dev);
  3004. int bind = con_is_bound(con->con);
  3005. return snprintf(buf, PAGE_SIZE, "%i\n", bind);
  3006. }
  3007. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  3008. char *buf)
  3009. {
  3010. struct con_driver *con = dev_get_drvdata(dev);
  3011. return snprintf(buf, PAGE_SIZE, "%s %s\n",
  3012. (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
  3013. con->desc);
  3014. }
  3015. static struct device_attribute device_attrs[] = {
  3016. __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
  3017. __ATTR(name, S_IRUGO, show_name, NULL),
  3018. };
  3019. static int vtconsole_init_device(struct con_driver *con)
  3020. {
  3021. int i;
  3022. int error = 0;
  3023. con->flag |= CON_DRIVER_FLAG_ATTR;
  3024. dev_set_drvdata(con->dev, con);
  3025. for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
  3026. error = device_create_file(con->dev, &device_attrs[i]);
  3027. if (error)
  3028. break;
  3029. }
  3030. if (error) {
  3031. while (--i >= 0)
  3032. device_remove_file(con->dev, &device_attrs[i]);
  3033. con->flag &= ~CON_DRIVER_FLAG_ATTR;
  3034. }
  3035. return error;
  3036. }
  3037. static void vtconsole_deinit_device(struct con_driver *con)
  3038. {
  3039. int i;
  3040. if (con->flag & CON_DRIVER_FLAG_ATTR) {
  3041. for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
  3042. device_remove_file(con->dev, &device_attrs[i]);
  3043. con->flag &= ~CON_DRIVER_FLAG_ATTR;
  3044. }
  3045. }
  3046. /**
  3047. * con_is_bound - checks if driver is bound to the console
  3048. * @csw: console driver
  3049. *
  3050. * RETURNS: zero if unbound, nonzero if bound
  3051. *
  3052. * Drivers can call this and if zero, they should release
  3053. * all resources allocated on con_startup()
  3054. */
  3055. int con_is_bound(const struct consw *csw)
  3056. {
  3057. int i, bound = 0;
  3058. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  3059. if (con_driver_map[i] == csw) {
  3060. bound = 1;
  3061. break;
  3062. }
  3063. }
  3064. return bound;
  3065. }
  3066. EXPORT_SYMBOL(con_is_bound);
  3067. /**
  3068. * con_debug_enter - prepare the console for the kernel debugger
  3069. * @sw: console driver
  3070. *
  3071. * Called when the console is taken over by the kernel debugger, this
  3072. * function needs to save the current console state, then put the console
  3073. * into a state suitable for the kernel debugger.
  3074. *
  3075. * RETURNS:
  3076. * Zero on success, nonzero if a failure occurred when trying to prepare
  3077. * the console for the debugger.
  3078. */
  3079. int con_debug_enter(struct vc_data *vc)
  3080. {
  3081. int ret = 0;
  3082. saved_fg_console = fg_console;
  3083. saved_last_console = last_console;
  3084. saved_want_console = want_console;
  3085. saved_vc_mode = vc->vc_mode;
  3086. saved_console_blanked = console_blanked;
  3087. vc->vc_mode = KD_TEXT;
  3088. console_blanked = 0;
  3089. if (vc->vc_sw->con_debug_enter)
  3090. ret = vc->vc_sw->con_debug_enter(vc);
  3091. #ifdef CONFIG_KGDB_KDB
  3092. /* Set the initial LINES variable if it is not already set */
  3093. if (vc->vc_rows < 999) {
  3094. int linecount;
  3095. char lns[4];
  3096. const char *setargs[3] = {
  3097. "set",
  3098. "LINES",
  3099. lns,
  3100. };
  3101. if (kdbgetintenv(setargs[0], &linecount)) {
  3102. snprintf(lns, 4, "%i", vc->vc_rows);
  3103. kdb_set(2, setargs);
  3104. }
  3105. }
  3106. #endif /* CONFIG_KGDB_KDB */
  3107. return ret;
  3108. }
  3109. EXPORT_SYMBOL_GPL(con_debug_enter);
  3110. /**
  3111. * con_debug_leave - restore console state
  3112. * @sw: console driver
  3113. *
  3114. * Restore the console state to what it was before the kernel debugger
  3115. * was invoked.
  3116. *
  3117. * RETURNS:
  3118. * Zero on success, nonzero if a failure occurred when trying to restore
  3119. * the console.
  3120. */
  3121. int con_debug_leave(void)
  3122. {
  3123. struct vc_data *vc;
  3124. int ret = 0;
  3125. fg_console = saved_fg_console;
  3126. last_console = saved_last_console;
  3127. want_console = saved_want_console;
  3128. console_blanked = saved_console_blanked;
  3129. vc_cons[fg_console].d->vc_mode = saved_vc_mode;
  3130. vc = vc_cons[fg_console].d;
  3131. if (vc->vc_sw->con_debug_leave)
  3132. ret = vc->vc_sw->con_debug_leave(vc);
  3133. return ret;
  3134. }
  3135. EXPORT_SYMBOL_GPL(con_debug_leave);
  3136. /**
  3137. * register_con_driver - register console driver to console layer
  3138. * @csw: console driver
  3139. * @first: the first console to take over, minimum value is 0
  3140. * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
  3141. *
  3142. * DESCRIPTION: This function registers a console driver which can later
  3143. * bind to a range of consoles specified by @first and @last. It will
  3144. * also initialize the console driver by calling con_startup().
  3145. */
  3146. int register_con_driver(const struct consw *csw, int first, int last)
  3147. {
  3148. struct module *owner = csw->owner;
  3149. struct con_driver *con_driver;
  3150. const char *desc;
  3151. int i, retval = 0;
  3152. if (!try_module_get(owner))
  3153. return -ENODEV;
  3154. console_lock();
  3155. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3156. con_driver = &registered_con_driver[i];
  3157. /* already registered */
  3158. if (con_driver->con == csw)
  3159. retval = -EBUSY;
  3160. }
  3161. if (retval)
  3162. goto err;
  3163. desc = csw->con_startup();
  3164. if (!desc)
  3165. goto err;
  3166. retval = -EINVAL;
  3167. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3168. con_driver = &registered_con_driver[i];
  3169. if (con_driver->con == NULL) {
  3170. con_driver->con = csw;
  3171. con_driver->desc = desc;
  3172. con_driver->node = i;
  3173. con_driver->flag = CON_DRIVER_FLAG_MODULE |
  3174. CON_DRIVER_FLAG_INIT;
  3175. con_driver->first = first;
  3176. con_driver->last = last;
  3177. retval = 0;
  3178. break;
  3179. }
  3180. }
  3181. if (retval)
  3182. goto err;
  3183. con_driver->dev = device_create(vtconsole_class, NULL,
  3184. MKDEV(0, con_driver->node),
  3185. NULL, "vtcon%i",
  3186. con_driver->node);
  3187. if (IS_ERR(con_driver->dev)) {
  3188. printk(KERN_WARNING "Unable to create device for %s; "
  3189. "errno = %ld\n", con_driver->desc,
  3190. PTR_ERR(con_driver->dev));
  3191. con_driver->dev = NULL;
  3192. } else {
  3193. vtconsole_init_device(con_driver);
  3194. }
  3195. err:
  3196. console_unlock();
  3197. module_put(owner);
  3198. return retval;
  3199. }
  3200. EXPORT_SYMBOL(register_con_driver);
  3201. /**
  3202. * unregister_con_driver - unregister console driver from console layer
  3203. * @csw: console driver
  3204. *
  3205. * DESCRIPTION: All drivers that registers to the console layer must
  3206. * call this function upon exit, or if the console driver is in a state
  3207. * where it won't be able to handle console services, such as the
  3208. * framebuffer console without loaded framebuffer drivers.
  3209. *
  3210. * The driver must unbind first prior to unregistration.
  3211. */
  3212. int unregister_con_driver(const struct consw *csw)
  3213. {
  3214. int i, retval = -ENODEV;
  3215. console_lock();
  3216. /* cannot unregister a bound driver */
  3217. if (con_is_bound(csw))
  3218. goto err;
  3219. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3220. struct con_driver *con_driver = &registered_con_driver[i];
  3221. if (con_driver->con == csw &&
  3222. con_driver->flag & CON_DRIVER_FLAG_MODULE) {
  3223. vtconsole_deinit_device(con_driver);
  3224. device_destroy(vtconsole_class,
  3225. MKDEV(0, con_driver->node));
  3226. con_driver->con = NULL;
  3227. con_driver->desc = NULL;
  3228. con_driver->dev = NULL;
  3229. con_driver->node = 0;
  3230. con_driver->flag = 0;
  3231. con_driver->first = 0;
  3232. con_driver->last = 0;
  3233. retval = 0;
  3234. break;
  3235. }
  3236. }
  3237. err:
  3238. console_unlock();
  3239. return retval;
  3240. }
  3241. EXPORT_SYMBOL(unregister_con_driver);
  3242. /*
  3243. * If we support more console drivers, this function is used
  3244. * when a driver wants to take over some existing consoles
  3245. * and become default driver for newly opened ones.
  3246. *
  3247. * take_over_console is basically a register followed by unbind
  3248. */
  3249. int take_over_console(const struct consw *csw, int first, int last, int deflt)
  3250. {
  3251. int err;
  3252. err = register_con_driver(csw, first, last);
  3253. /* if we get an busy error we still want to bind the console driver
  3254. * and return success, as we may have unbound the console driver
  3255.  * but not unregistered it.
  3256. */
  3257. if (err == -EBUSY)
  3258. err = 0;
  3259. if (!err)
  3260. bind_con_driver(csw, first, last, deflt);
  3261. return err;
  3262. }
  3263. /*
  3264. * give_up_console is a wrapper to unregister_con_driver. It will only
  3265. * work if driver is fully unbound.
  3266. */
  3267. void give_up_console(const struct consw *csw)
  3268. {
  3269. unregister_con_driver(csw);
  3270. }
  3271. static int __init vtconsole_class_init(void)
  3272. {
  3273. int i;
  3274. vtconsole_class = class_create(THIS_MODULE, "vtconsole");
  3275. if (IS_ERR(vtconsole_class)) {
  3276. printk(KERN_WARNING "Unable to create vt console class; "
  3277. "errno = %ld\n", PTR_ERR(vtconsole_class));
  3278. vtconsole_class = NULL;
  3279. }
  3280. /* Add system drivers to sysfs */
  3281. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3282. struct con_driver *con = &registered_con_driver[i];
  3283. if (con->con && !con->dev) {
  3284. con->dev = device_create(vtconsole_class, NULL,
  3285. MKDEV(0, con->node),
  3286. NULL, "vtcon%i",
  3287. con->node);
  3288. if (IS_ERR(con->dev)) {
  3289. printk(KERN_WARNING "Unable to create "
  3290. "device for %s; errno = %ld\n",
  3291. con->desc, PTR_ERR(con->dev));
  3292. con->dev = NULL;
  3293. } else {
  3294. vtconsole_init_device(con);
  3295. }
  3296. }
  3297. }
  3298. return 0;
  3299. }
  3300. postcore_initcall(vtconsole_class_init);
  3301. #endif
  3302. /*
  3303. * Screen blanking
  3304. */
  3305. static int set_vesa_blanking(char __user *p)
  3306. {
  3307. unsigned int mode;
  3308. if (get_user(mode, p + 1))
  3309. return -EFAULT;
  3310. vesa_blank_mode = (mode < 4) ? mode : 0;
  3311. return 0;
  3312. }
  3313. void do_blank_screen(int entering_gfx)
  3314. {
  3315. struct vc_data *vc = vc_cons[fg_console].d;
  3316. int i;
  3317. WARN_CONSOLE_UNLOCKED();
  3318. if (console_blanked) {
  3319. if (blank_state == blank_vesa_wait) {
  3320. blank_state = blank_off;
  3321. vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
  3322. }
  3323. return;
  3324. }
  3325. /* entering graphics mode? */
  3326. if (entering_gfx) {
  3327. hide_cursor(vc);
  3328. save_screen(vc);
  3329. vc->vc_sw->con_blank(vc, -1, 1);
  3330. console_blanked = fg_console + 1;
  3331. blank_state = blank_off;
  3332. set_origin(vc);
  3333. return;
  3334. }
  3335. if (blank_state != blank_normal_wait)
  3336. return;
  3337. blank_state = blank_off;
  3338. /* don't blank graphics */
  3339. if (vc->vc_mode != KD_TEXT) {
  3340. console_blanked = fg_console + 1;
  3341. return;
  3342. }
  3343. hide_cursor(vc);
  3344. del_timer_sync(&console_timer);
  3345. blank_timer_expired = 0;
  3346. save_screen(vc);
  3347. /* In case we need to reset origin, blanking hook returns 1 */
  3348. i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
  3349. console_blanked = fg_console + 1;
  3350. if (i)
  3351. set_origin(vc);
  3352. if (console_blank_hook && console_blank_hook(1))
  3353. return;
  3354. if (vesa_off_interval && vesa_blank_mode) {
  3355. blank_state = blank_vesa_wait;
  3356. mod_timer(&console_timer, jiffies + vesa_off_interval);
  3357. }
  3358. vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
  3359. }
  3360. EXPORT_SYMBOL(do_blank_screen);
  3361. /*
  3362. * Called by timer as well as from vt_console_driver
  3363. */
  3364. void do_unblank_screen(int leaving_gfx)
  3365. {
  3366. struct vc_data *vc;
  3367. /* This should now always be called from a "sane" (read: can schedule)
  3368. * context for the sake of the low level drivers, except in the special
  3369. * case of oops_in_progress
  3370. */
  3371. if (!oops_in_progress)
  3372. might_sleep();
  3373. WARN_CONSOLE_UNLOCKED();
  3374. ignore_poke = 0;
  3375. if (!console_blanked)
  3376. return;
  3377. if (!vc_cons_allocated(fg_console)) {
  3378. /* impossible */
  3379. pr_warning("unblank_screen: tty %d not allocated ??\n",
  3380. fg_console+1);
  3381. return;
  3382. }
  3383. vc = vc_cons[fg_console].d;
  3384. /* Try to unblank in oops case too */
  3385. if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
  3386. return; /* but leave console_blanked != 0 */
  3387. if (blankinterval) {
  3388. mod_timer(&console_timer, jiffies + (blankinterval * HZ));
  3389. blank_state = blank_normal_wait;
  3390. }
  3391. console_blanked = 0;
  3392. if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
  3393. /* Low-level driver cannot restore -> do it ourselves */
  3394. update_screen(vc);
  3395. if (console_blank_hook)
  3396. console_blank_hook(0);
  3397. set_palette(vc);
  3398. set_cursor(vc);
  3399. vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
  3400. }
  3401. EXPORT_SYMBOL(do_unblank_screen);
  3402. /*
  3403. * This is called by the outside world to cause a forced unblank, mostly for
  3404. * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
  3405. * call it with 1 as an argument and so force a mode restore... that may kill
  3406. * X or at least garbage the screen but would also make the Oops visible...
  3407. */
  3408. void unblank_screen(void)
  3409. {
  3410. do_unblank_screen(0);
  3411. }
  3412. /*
  3413. * We defer the timer blanking to work queue so it can take the console mutex
  3414. * (console operations can still happen at irq time, but only from printk which
  3415. * has the console mutex. Not perfect yet, but better than no locking
  3416. */
  3417. static void blank_screen_t(unsigned long dummy)
  3418. {
  3419. if (unlikely(!keventd_up())) {
  3420. mod_timer(&console_timer, jiffies + (blankinterval * HZ));
  3421. return;
  3422. }
  3423. blank_timer_expired = 1;
  3424. schedule_work(&console_work);
  3425. }
  3426. void poke_blanked_console(void)
  3427. {
  3428. WARN_CONSOLE_UNLOCKED();
  3429. /* Add this so we quickly catch whoever might call us in a non
  3430. * safe context. Nowadays, unblank_screen() isn't to be called in
  3431. * atomic contexts and is allowed to schedule (with the special case
  3432. * of oops_in_progress, but that isn't of any concern for this
  3433. * function. --BenH.
  3434. */
  3435. might_sleep();
  3436. /* This isn't perfectly race free, but a race here would be mostly harmless,
  3437. * at worse, we'll do a spurrious blank and it's unlikely
  3438. */
  3439. del_timer(&console_timer);
  3440. blank_timer_expired = 0;
  3441. if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
  3442. return;
  3443. if (console_blanked)
  3444. unblank_screen();
  3445. else if (blankinterval) {
  3446. mod_timer(&console_timer, jiffies + (blankinterval * HZ));
  3447. blank_state = blank_normal_wait;
  3448. }
  3449. }
  3450. /*
  3451. * Palettes
  3452. */
  3453. static void set_palette(struct vc_data *vc)
  3454. {
  3455. WARN_CONSOLE_UNLOCKED();
  3456. if (vc->vc_mode != KD_GRAPHICS)
  3457. vc->vc_sw->con_set_palette(vc, color_table);
  3458. }
  3459. static int set_get_cmap(unsigned char __user *arg, int set)
  3460. {
  3461. int i, j, k;
  3462. WARN_CONSOLE_UNLOCKED();
  3463. for (i = 0; i < 16; i++)
  3464. if (set) {
  3465. get_user(default_red[i], arg++);
  3466. get_user(default_grn[i], arg++);
  3467. get_user(default_blu[i], arg++);
  3468. } else {
  3469. put_user(default_red[i], arg++);
  3470. put_user(default_grn[i], arg++);
  3471. put_user(default_blu[i], arg++);
  3472. }
  3473. if (set) {
  3474. for (i = 0; i < MAX_NR_CONSOLES; i++)
  3475. if (vc_cons_allocated(i)) {
  3476. for (j = k = 0; j < 16; j++) {
  3477. vc_cons[i].d->vc_palette[k++] = default_red[j];
  3478. vc_cons[i].d->vc_palette[k++] = default_grn[j];
  3479. vc_cons[i].d->vc_palette[k++] = default_blu[j];
  3480. }
  3481. set_palette(vc_cons[i].d);
  3482. }
  3483. }
  3484. return 0;
  3485. }
  3486. /*
  3487. * Load palette into the DAC registers. arg points to a colour
  3488. * map, 3 bytes per colour, 16 colours, range from 0 to 255.
  3489. */
  3490. int con_set_cmap(unsigned char __user *arg)
  3491. {
  3492. int rc;
  3493. console_lock();
  3494. rc = set_get_cmap (arg,1);
  3495. console_unlock();
  3496. return rc;
  3497. }
  3498. int con_get_cmap(unsigned char __user *arg)
  3499. {
  3500. int rc;
  3501. console_lock();
  3502. rc = set_get_cmap (arg,0);
  3503. console_unlock();
  3504. return rc;
  3505. }
  3506. void reset_palette(struct vc_data *vc)
  3507. {
  3508. int j, k;
  3509. for (j=k=0; j<16; j++) {
  3510. vc->vc_palette[k++] = default_red[j];
  3511. vc->vc_palette[k++] = default_grn[j];
  3512. vc->vc_palette[k++] = default_blu[j];
  3513. }
  3514. set_palette(vc);
  3515. }
  3516. /*
  3517. * Font switching
  3518. *
  3519. * Currently we only support fonts up to 32 pixels wide, at a maximum height
  3520. * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
  3521. * depending on width) reserved for each character which is kinda wasty, but
  3522. * this is done in order to maintain compatibility with the EGA/VGA fonts. It
  3523. * is up to the actual low-level console-driver convert data into its favorite
  3524. * format (maybe we should add a `fontoffset' field to the `display'
  3525. * structure so we won't have to convert the fontdata all the time.
  3526. * /Jes
  3527. */
  3528. #define max_font_size 65536
  3529. static int con_font_get(struct vc_data *vc, struct console_font_op *op)
  3530. {
  3531. struct console_font font;
  3532. int rc = -EINVAL;
  3533. int c;
  3534. if (op->data) {
  3535. font.data = kmalloc(max_font_size, GFP_KERNEL);
  3536. if (!font.data)
  3537. return -ENOMEM;
  3538. } else
  3539. font.data = NULL;
  3540. console_lock();
  3541. if (vc->vc_mode != KD_TEXT)
  3542. rc = -EINVAL;
  3543. else if (vc->vc_sw->con_font_get)
  3544. rc = vc->vc_sw->con_font_get(vc, &font);
  3545. else
  3546. rc = -ENOSYS;
  3547. console_unlock();
  3548. if (rc)
  3549. goto out;
  3550. c = (font.width+7)/8 * 32 * font.charcount;
  3551. if (op->data && font.charcount > op->charcount)
  3552. rc = -ENOSPC;
  3553. if (!(op->flags & KD_FONT_FLAG_OLD)) {
  3554. if (font.width > op->width || font.height > op->height)
  3555. rc = -ENOSPC;
  3556. } else {
  3557. if (font.width != 8)
  3558. rc = -EIO;
  3559. else if ((op->height && font.height > op->height) ||
  3560. font.height > 32)
  3561. rc = -ENOSPC;
  3562. }
  3563. if (rc)
  3564. goto out;
  3565. op->height = font.height;
  3566. op->width = font.width;
  3567. op->charcount = font.charcount;
  3568. if (op->data && copy_to_user(op->data, font.data, c))
  3569. rc = -EFAULT;
  3570. out:
  3571. kfree(font.data);
  3572. return rc;
  3573. }
  3574. static int con_font_set(struct vc_data *vc, struct console_font_op *op)
  3575. {
  3576. struct console_font font;
  3577. int rc = -EINVAL;
  3578. int size;
  3579. if (vc->vc_mode != KD_TEXT)
  3580. return -EINVAL;
  3581. if (!op->data)
  3582. return -EINVAL;
  3583. if (op->charcount > 512)
  3584. return -EINVAL;
  3585. if (!op->height) { /* Need to guess font height [compat] */
  3586. int h, i;
  3587. u8 __user *charmap = op->data;
  3588. u8 tmp;
  3589. /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
  3590. so that we can get rid of this soon */
  3591. if (!(op->flags & KD_FONT_FLAG_OLD))
  3592. return -EINVAL;
  3593. for (h = 32; h > 0; h--)
  3594. for (i = 0; i < op->charcount; i++) {
  3595. if (get_user(tmp, &charmap[32*i+h-1]))
  3596. return -EFAULT;
  3597. if (tmp)
  3598. goto nonzero;
  3599. }
  3600. return -EINVAL;
  3601. nonzero:
  3602. op->height = h;
  3603. }
  3604. if (op->width <= 0 || op->width > 32 || op->height > 32)
  3605. return -EINVAL;
  3606. size = (op->width+7)/8 * 32 * op->charcount;
  3607. if (size > max_font_size)
  3608. return -ENOSPC;
  3609. font.charcount = op->charcount;
  3610. font.height = op->height;
  3611. font.width = op->width;
  3612. font.data = memdup_user(op->data, size);
  3613. if (IS_ERR(font.data))
  3614. return PTR_ERR(font.data);
  3615. console_lock();
  3616. if (vc->vc_mode != KD_TEXT)
  3617. rc = -EINVAL;
  3618. else if (vc->vc_sw->con_font_set)
  3619. rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
  3620. else
  3621. rc = -ENOSYS;
  3622. console_unlock();
  3623. kfree(font.data);
  3624. return rc;
  3625. }
  3626. static int con_font_default(struct vc_data *vc, struct console_font_op *op)
  3627. {
  3628. struct console_font font = {.width = op->width, .height = op->height};
  3629. char name[MAX_FONT_NAME];
  3630. char *s = name;
  3631. int rc;
  3632. if (!op->data)
  3633. s = NULL;
  3634. else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
  3635. return -EFAULT;
  3636. else
  3637. name[MAX_FONT_NAME - 1] = 0;
  3638. console_lock();
  3639. if (vc->vc_mode != KD_TEXT) {
  3640. console_unlock();
  3641. return -EINVAL;
  3642. }
  3643. if (vc->vc_sw->con_font_default)
  3644. rc = vc->vc_sw->con_font_default(vc, &font, s);
  3645. else
  3646. rc = -ENOSYS;
  3647. console_unlock();
  3648. if (!rc) {
  3649. op->width = font.width;
  3650. op->height = font.height;
  3651. }
  3652. return rc;
  3653. }
  3654. static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
  3655. {
  3656. int con = op->height;
  3657. int rc;
  3658. console_lock();
  3659. if (vc->vc_mode != KD_TEXT)
  3660. rc = -EINVAL;
  3661. else if (!vc->vc_sw->con_font_copy)
  3662. rc = -ENOSYS;
  3663. else if (con < 0 || !vc_cons_allocated(con))
  3664. rc = -ENOTTY;
  3665. else if (con == vc->vc_num) /* nothing to do */
  3666. rc = 0;
  3667. else
  3668. rc = vc->vc_sw->con_font_copy(vc, con);
  3669. console_unlock();
  3670. return rc;
  3671. }
  3672. int con_font_op(struct vc_data *vc, struct console_font_op *op)
  3673. {
  3674. switch (op->op) {
  3675. case KD_FONT_OP_SET:
  3676. return con_font_set(vc, op);
  3677. case KD_FONT_OP_GET:
  3678. return con_font_get(vc, op);
  3679. case KD_FONT_OP_SET_DEFAULT:
  3680. return con_font_default(vc, op);
  3681. case KD_FONT_OP_COPY:
  3682. return con_font_copy(vc, op);
  3683. }
  3684. return -ENOSYS;
  3685. }
  3686. /*
  3687. * Interface exported to selection and vcs.
  3688. */
  3689. /* used by selection */
  3690. u16 screen_glyph(struct vc_data *vc, int offset)
  3691. {
  3692. u16 w = scr_readw(screenpos(vc, offset, 1));
  3693. u16 c = w & 0xff;
  3694. if (w & vc->vc_hi_font_mask)
  3695. c |= 0x100;
  3696. return c;
  3697. }
  3698. EXPORT_SYMBOL_GPL(screen_glyph);
  3699. /* used by vcs - note the word offset */
  3700. unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
  3701. {
  3702. return screenpos(vc, 2 * w_offset, viewed);
  3703. }
  3704. void getconsxy(struct vc_data *vc, unsigned char *p)
  3705. {
  3706. p[0] = vc->vc_x;
  3707. p[1] = vc->vc_y;
  3708. }
  3709. void putconsxy(struct vc_data *vc, unsigned char *p)
  3710. {
  3711. hide_cursor(vc);
  3712. gotoxy(vc, p[0], p[1]);
  3713. set_cursor(vc);
  3714. }
  3715. u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
  3716. {
  3717. if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
  3718. return softcursor_original;
  3719. return scr_readw(org);
  3720. }
  3721. void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
  3722. {
  3723. scr_writew(val, org);
  3724. if ((unsigned long)org == vc->vc_pos) {
  3725. softcursor_original = -1;
  3726. add_softcursor(vc);
  3727. }
  3728. }
  3729. void vcs_scr_updated(struct vc_data *vc)
  3730. {
  3731. notify_update(vc);
  3732. }
  3733. /*
  3734. * Visible symbols for modules
  3735. */
  3736. EXPORT_SYMBOL(color_table);
  3737. EXPORT_SYMBOL(default_red);
  3738. EXPORT_SYMBOL(default_grn);
  3739. EXPORT_SYMBOL(default_blu);
  3740. EXPORT_SYMBOL(update_region);
  3741. EXPORT_SYMBOL(redraw_screen);
  3742. EXPORT_SYMBOL(vc_resize);
  3743. EXPORT_SYMBOL(fg_console);
  3744. EXPORT_SYMBOL(console_blank_hook);
  3745. EXPORT_SYMBOL(console_blanked);
  3746. EXPORT_SYMBOL(vc_cons);
  3747. EXPORT_SYMBOL(global_cursor_default);
  3748. #ifndef VT_SINGLE_DRIVER
  3749. EXPORT_SYMBOL(take_over_console);
  3750. EXPORT_SYMBOL(give_up_console);
  3751. #endif