vt.c 102 KB

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