vt.c 100 KB

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