vt.c 101 KB

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