vt.c 99 KB

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