vt.c 96 KB

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