vt.c 99 KB

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