vt.c 102 KB

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