vt.c 102 KB

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