vt.c 98 KB

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