vt.c 98 KB

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