vt.c 102 KB

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