vt.c 102 KB

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