vt.c 95 KB

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