vt.c 98 KB

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