vt.c 101 KB

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