vt.c 101 KB

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