vt.c 102 KB

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