vt.c 103 KB

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