vt.c 102 KB

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