vt.c 104 KB

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