vt.c 102 KB

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