vt.c 101 KB

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