vt.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974
  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,
  1827. sizeof(double_width) / sizeof(*double_width) - 1);
  1828. }
  1829. /* acquires console_sem */
  1830. static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1831. {
  1832. #ifdef VT_BUF_VRAM_ONLY
  1833. #define FLUSH do { } while(0);
  1834. #else
  1835. #define FLUSH if (draw_x >= 0) { \
  1836. vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
  1837. draw_x = -1; \
  1838. }
  1839. #endif
  1840. int c, tc, ok, n = 0, draw_x = -1;
  1841. unsigned int currcons;
  1842. unsigned long draw_from = 0, draw_to = 0;
  1843. struct vc_data *vc;
  1844. unsigned char vc_attr;
  1845. uint8_t rescan;
  1846. uint8_t inverse;
  1847. uint8_t width;
  1848. u16 himask, charmask;
  1849. const unsigned char *orig_buf = NULL;
  1850. int orig_count;
  1851. if (in_interrupt())
  1852. return count;
  1853. might_sleep();
  1854. acquire_console_sem();
  1855. vc = tty->driver_data;
  1856. if (vc == NULL) {
  1857. printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
  1858. release_console_sem();
  1859. return 0;
  1860. }
  1861. currcons = vc->vc_num;
  1862. if (!vc_cons_allocated(currcons)) {
  1863. /* could this happen? */
  1864. static int error = 0;
  1865. if (!error) {
  1866. error = 1;
  1867. printk("con_write: tty %d not allocated\n", currcons+1);
  1868. }
  1869. release_console_sem();
  1870. return 0;
  1871. }
  1872. release_console_sem();
  1873. orig_buf = buf;
  1874. orig_count = count;
  1875. /* At this point 'buf' is guaranteed to be a kernel buffer
  1876. * and therefore no access to userspace (and therefore sleeping)
  1877. * will be needed. The con_buf_mtx serializes all tty based
  1878. * console rendering and vcs write/read operations. We hold
  1879. * the console spinlock during the entire write.
  1880. */
  1881. acquire_console_sem();
  1882. vc = tty->driver_data;
  1883. if (vc == NULL) {
  1884. printk(KERN_ERR "vt: argh, driver_data _became_ NULL !\n");
  1885. release_console_sem();
  1886. goto out;
  1887. }
  1888. himask = vc->vc_hi_font_mask;
  1889. charmask = himask ? 0x1ff : 0xff;
  1890. /* undraw cursor first */
  1891. if (IS_FG(vc))
  1892. hide_cursor(vc);
  1893. while (!tty->stopped && count) {
  1894. int orig = *buf;
  1895. c = orig;
  1896. buf++;
  1897. n++;
  1898. count--;
  1899. rescan = 0;
  1900. inverse = 0;
  1901. width = 1;
  1902. /* Do no translation at all in control states */
  1903. if (vc->vc_state != ESnormal) {
  1904. tc = c;
  1905. } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
  1906. /* Combine UTF-8 into Unicode in vc_utf_char.
  1907. * vc_utf_count is the number of continuation bytes still
  1908. * expected to arrive.
  1909. * vc_npar is the number of continuation bytes arrived so
  1910. * far
  1911. */
  1912. rescan_last_byte:
  1913. if ((c & 0xc0) == 0x80) {
  1914. /* Continuation byte received */
  1915. static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
  1916. if (vc->vc_utf_count) {
  1917. vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
  1918. vc->vc_npar++;
  1919. if (--vc->vc_utf_count) {
  1920. /* Still need some bytes */
  1921. continue;
  1922. }
  1923. /* Got a whole character */
  1924. c = vc->vc_utf_char;
  1925. /* Reject overlong sequences */
  1926. if (c <= utf8_length_changes[vc->vc_npar - 1] ||
  1927. c > utf8_length_changes[vc->vc_npar])
  1928. c = 0xfffd;
  1929. } else {
  1930. /* Unexpected continuation byte */
  1931. vc->vc_utf_count = 0;
  1932. c = 0xfffd;
  1933. }
  1934. } else {
  1935. /* Single ASCII byte or first byte of a sequence received */
  1936. if (vc->vc_utf_count) {
  1937. /* Continuation byte expected */
  1938. rescan = 1;
  1939. vc->vc_utf_count = 0;
  1940. c = 0xfffd;
  1941. } else if (c > 0x7f) {
  1942. /* First byte of a multibyte sequence received */
  1943. vc->vc_npar = 0;
  1944. if ((c & 0xe0) == 0xc0) {
  1945. vc->vc_utf_count = 1;
  1946. vc->vc_utf_char = (c & 0x1f);
  1947. } else if ((c & 0xf0) == 0xe0) {
  1948. vc->vc_utf_count = 2;
  1949. vc->vc_utf_char = (c & 0x0f);
  1950. } else if ((c & 0xf8) == 0xf0) {
  1951. vc->vc_utf_count = 3;
  1952. vc->vc_utf_char = (c & 0x07);
  1953. } else if ((c & 0xfc) == 0xf8) {
  1954. vc->vc_utf_count = 4;
  1955. vc->vc_utf_char = (c & 0x03);
  1956. } else if ((c & 0xfe) == 0xfc) {
  1957. vc->vc_utf_count = 5;
  1958. vc->vc_utf_char = (c & 0x01);
  1959. } else {
  1960. /* 254 and 255 are invalid */
  1961. c = 0xfffd;
  1962. }
  1963. if (vc->vc_utf_count) {
  1964. /* Still need some bytes */
  1965. continue;
  1966. }
  1967. }
  1968. /* Nothing to do if an ASCII byte was received */
  1969. }
  1970. /* End of UTF-8 decoding. */
  1971. /* c is the received character, or U+FFFD for invalid sequences. */
  1972. /* Replace invalid Unicode code points with U+FFFD too */
  1973. if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
  1974. c = 0xfffd;
  1975. tc = c;
  1976. } else { /* no utf or alternate charset mode */
  1977. tc = vc->vc_translate[vc->vc_toggle_meta ? (c | 0x80) : c];
  1978. }
  1979. /* If the original code was a control character we
  1980. * only allow a glyph to be displayed if the code is
  1981. * not normally used (such as for cursor movement) or
  1982. * if the disp_ctrl mode has been explicitly enabled.
  1983. * Certain characters (as given by the CTRL_ALWAYS
  1984. * bitmap) are always displayed as control characters,
  1985. * as the console would be pretty useless without
  1986. * them; to display an arbitrary font position use the
  1987. * direct-to-font zone in UTF-8 mode.
  1988. */
  1989. ok = tc && (c >= 32 ||
  1990. !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
  1991. vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
  1992. && (c != 127 || vc->vc_disp_ctrl)
  1993. && (c != 128+27);
  1994. if (vc->vc_state == ESnormal && ok) {
  1995. if (vc->vc_utf && !vc->vc_disp_ctrl) {
  1996. if (is_double_width(c))
  1997. width = 2;
  1998. }
  1999. /* Now try to find out how to display it */
  2000. tc = conv_uni_to_pc(vc, tc);
  2001. if (tc & ~charmask) {
  2002. if (tc == -1 || tc == -2) {
  2003. continue; /* nothing to display */
  2004. }
  2005. /* Glyph not found */
  2006. if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
  2007. /* In legacy mode use the glyph we get by a 1:1 mapping.
  2008. This would make absolutely no sense with Unicode in mind,
  2009. but do this for ASCII characters since a font may lack
  2010. Unicode mapping info and we don't want to end up with
  2011. having question marks only. */
  2012. tc = c;
  2013. } else {
  2014. /* Display U+FFFD. If it's not found, display an inverse question mark. */
  2015. tc = conv_uni_to_pc(vc, 0xfffd);
  2016. if (tc < 0) {
  2017. inverse = 1;
  2018. tc = conv_uni_to_pc(vc, '?');
  2019. if (tc < 0) tc = '?';
  2020. }
  2021. }
  2022. }
  2023. if (!inverse) {
  2024. vc_attr = vc->vc_attr;
  2025. } else {
  2026. /* invert vc_attr */
  2027. if (!vc->vc_can_do_color) {
  2028. vc_attr = (vc->vc_attr) ^ 0x08;
  2029. } else if (vc->vc_hi_font_mask == 0x100) {
  2030. vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
  2031. } else {
  2032. vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
  2033. }
  2034. FLUSH
  2035. }
  2036. while (1) {
  2037. if (vc->vc_need_wrap || vc->vc_decim)
  2038. FLUSH
  2039. if (vc->vc_need_wrap) {
  2040. cr(vc);
  2041. lf(vc);
  2042. }
  2043. if (vc->vc_decim)
  2044. insert_char(vc, 1);
  2045. scr_writew(himask ?
  2046. ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
  2047. (vc_attr << 8) + tc,
  2048. (u16 *) vc->vc_pos);
  2049. if (DO_UPDATE(vc) && draw_x < 0) {
  2050. draw_x = vc->vc_x;
  2051. draw_from = vc->vc_pos;
  2052. }
  2053. if (vc->vc_x == vc->vc_cols - 1) {
  2054. vc->vc_need_wrap = vc->vc_decawm;
  2055. draw_to = vc->vc_pos + 2;
  2056. } else {
  2057. vc->vc_x++;
  2058. draw_to = (vc->vc_pos += 2);
  2059. }
  2060. if (!--width) break;
  2061. tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
  2062. if (tc < 0) tc = ' ';
  2063. }
  2064. if (inverse) {
  2065. FLUSH
  2066. }
  2067. if (rescan) {
  2068. rescan = 0;
  2069. inverse = 0;
  2070. width = 1;
  2071. c = orig;
  2072. goto rescan_last_byte;
  2073. }
  2074. continue;
  2075. }
  2076. FLUSH
  2077. do_con_trol(tty, vc, orig);
  2078. }
  2079. FLUSH
  2080. console_conditional_schedule();
  2081. release_console_sem();
  2082. out:
  2083. return n;
  2084. #undef FLUSH
  2085. }
  2086. /*
  2087. * This is the console switching callback.
  2088. *
  2089. * Doing console switching in a process context allows
  2090. * us to do the switches asynchronously (needed when we want
  2091. * to switch due to a keyboard interrupt). Synchronization
  2092. * with other console code and prevention of re-entrancy is
  2093. * ensured with console_sem.
  2094. */
  2095. static void console_callback(struct work_struct *ignored)
  2096. {
  2097. acquire_console_sem();
  2098. if (want_console >= 0) {
  2099. if (want_console != fg_console &&
  2100. vc_cons_allocated(want_console)) {
  2101. hide_cursor(vc_cons[fg_console].d);
  2102. change_console(vc_cons[want_console].d);
  2103. /* we only changed when the console had already
  2104. been allocated - a new console is not created
  2105. in an interrupt routine */
  2106. }
  2107. want_console = -1;
  2108. }
  2109. if (do_poke_blanked_console) { /* do not unblank for a LED change */
  2110. do_poke_blanked_console = 0;
  2111. poke_blanked_console();
  2112. }
  2113. if (scrollback_delta) {
  2114. struct vc_data *vc = vc_cons[fg_console].d;
  2115. clear_selection();
  2116. if (vc->vc_mode == KD_TEXT)
  2117. vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
  2118. scrollback_delta = 0;
  2119. }
  2120. if (blank_timer_expired) {
  2121. do_blank_screen(0);
  2122. blank_timer_expired = 0;
  2123. }
  2124. release_console_sem();
  2125. }
  2126. int set_console(int nr)
  2127. {
  2128. struct vc_data *vc = vc_cons[fg_console].d;
  2129. if (!vc_cons_allocated(nr) || vt_dont_switch ||
  2130. (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
  2131. /*
  2132. * Console switch will fail in console_callback() or
  2133. * change_console() so there is no point scheduling
  2134. * the callback
  2135. *
  2136. * Existing set_console() users don't check the return
  2137. * value so this shouldn't break anything
  2138. */
  2139. return -EINVAL;
  2140. }
  2141. want_console = nr;
  2142. schedule_console_callback();
  2143. return 0;
  2144. }
  2145. struct tty_driver *console_driver;
  2146. #ifdef CONFIG_VT_CONSOLE
  2147. /*
  2148. * Console on virtual terminal
  2149. *
  2150. * The console must be locked when we get here.
  2151. */
  2152. static void vt_console_print(struct console *co, const char *b, unsigned count)
  2153. {
  2154. struct vc_data *vc = vc_cons[fg_console].d;
  2155. unsigned char c;
  2156. static unsigned long printing;
  2157. const ushort *start;
  2158. ushort cnt = 0;
  2159. ushort myx;
  2160. /* console busy or not yet initialized */
  2161. if (!printable || test_and_set_bit(0, &printing))
  2162. return;
  2163. if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
  2164. vc = vc_cons[kmsg_redirect - 1].d;
  2165. /* read `x' only after setting currcons properly (otherwise
  2166. the `x' macro will read the x of the foreground console). */
  2167. myx = vc->vc_x;
  2168. if (!vc_cons_allocated(fg_console)) {
  2169. /* impossible */
  2170. /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
  2171. goto quit;
  2172. }
  2173. if (vc->vc_mode != KD_TEXT)
  2174. goto quit;
  2175. /* undraw cursor first */
  2176. if (IS_FG(vc))
  2177. hide_cursor(vc);
  2178. start = (ushort *)vc->vc_pos;
  2179. /* Contrived structure to try to emulate original need_wrap behaviour
  2180. * Problems caused when we have need_wrap set on '\n' character */
  2181. while (count--) {
  2182. c = *b++;
  2183. if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
  2184. if (cnt > 0) {
  2185. if (CON_IS_VISIBLE(vc))
  2186. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  2187. vc->vc_x += cnt;
  2188. if (vc->vc_need_wrap)
  2189. vc->vc_x--;
  2190. cnt = 0;
  2191. }
  2192. if (c == 8) { /* backspace */
  2193. bs(vc);
  2194. start = (ushort *)vc->vc_pos;
  2195. myx = vc->vc_x;
  2196. continue;
  2197. }
  2198. if (c != 13)
  2199. lf(vc);
  2200. cr(vc);
  2201. start = (ushort *)vc->vc_pos;
  2202. myx = vc->vc_x;
  2203. if (c == 10 || c == 13)
  2204. continue;
  2205. }
  2206. scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
  2207. cnt++;
  2208. if (myx == vc->vc_cols - 1) {
  2209. vc->vc_need_wrap = 1;
  2210. continue;
  2211. }
  2212. vc->vc_pos += 2;
  2213. myx++;
  2214. }
  2215. if (cnt > 0) {
  2216. if (CON_IS_VISIBLE(vc))
  2217. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  2218. vc->vc_x += cnt;
  2219. if (vc->vc_x == vc->vc_cols) {
  2220. vc->vc_x--;
  2221. vc->vc_need_wrap = 1;
  2222. }
  2223. }
  2224. set_cursor(vc);
  2225. quit:
  2226. clear_bit(0, &printing);
  2227. }
  2228. static struct tty_driver *vt_console_device(struct console *c, int *index)
  2229. {
  2230. *index = c->index ? c->index-1 : fg_console;
  2231. return console_driver;
  2232. }
  2233. static struct console vt_console_driver = {
  2234. .name = "tty",
  2235. .write = vt_console_print,
  2236. .device = vt_console_device,
  2237. .unblank = unblank_screen,
  2238. .flags = CON_PRINTBUFFER,
  2239. .index = -1,
  2240. };
  2241. #endif
  2242. /*
  2243. * Handling of Linux-specific VC ioctls
  2244. */
  2245. /*
  2246. * Generally a bit racy with respect to console_sem().
  2247. *
  2248. * There are some functions which don't need it.
  2249. *
  2250. * There are some functions which can sleep for arbitrary periods
  2251. * (paste_selection) but we don't need the lock there anyway.
  2252. *
  2253. * set_selection has locking, and definitely needs it
  2254. */
  2255. int tioclinux(struct tty_struct *tty, unsigned long arg)
  2256. {
  2257. char type, data;
  2258. char __user *p = (char __user *)arg;
  2259. int lines;
  2260. int ret;
  2261. if (tty->driver->type != TTY_DRIVER_TYPE_CONSOLE)
  2262. return -EINVAL;
  2263. if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
  2264. return -EPERM;
  2265. if (get_user(type, p))
  2266. return -EFAULT;
  2267. ret = 0;
  2268. switch (type)
  2269. {
  2270. case TIOCL_SETSEL:
  2271. acquire_console_sem();
  2272. ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
  2273. release_console_sem();
  2274. break;
  2275. case TIOCL_PASTESEL:
  2276. ret = paste_selection(tty);
  2277. break;
  2278. case TIOCL_UNBLANKSCREEN:
  2279. acquire_console_sem();
  2280. unblank_screen();
  2281. release_console_sem();
  2282. break;
  2283. case TIOCL_SELLOADLUT:
  2284. ret = sel_loadlut(p);
  2285. break;
  2286. case TIOCL_GETSHIFTSTATE:
  2287. /*
  2288. * Make it possible to react to Shift+Mousebutton.
  2289. * Note that 'shift_state' is an undocumented
  2290. * kernel-internal variable; programs not closely
  2291. * related to the kernel should not use this.
  2292. */
  2293. data = shift_state;
  2294. ret = __put_user(data, p);
  2295. break;
  2296. case TIOCL_GETMOUSEREPORTING:
  2297. data = mouse_reporting();
  2298. ret = __put_user(data, p);
  2299. break;
  2300. case TIOCL_SETVESABLANK:
  2301. ret = set_vesa_blanking(p);
  2302. break;
  2303. case TIOCL_GETKMSGREDIRECT:
  2304. data = kmsg_redirect;
  2305. ret = __put_user(data, p);
  2306. break;
  2307. case TIOCL_SETKMSGREDIRECT:
  2308. if (!capable(CAP_SYS_ADMIN)) {
  2309. ret = -EPERM;
  2310. } else {
  2311. if (get_user(data, p+1))
  2312. ret = -EFAULT;
  2313. else
  2314. kmsg_redirect = data;
  2315. }
  2316. break;
  2317. case TIOCL_GETFGCONSOLE:
  2318. ret = fg_console;
  2319. break;
  2320. case TIOCL_SCROLLCONSOLE:
  2321. if (get_user(lines, (s32 __user *)(p+4))) {
  2322. ret = -EFAULT;
  2323. } else {
  2324. scrollfront(vc_cons[fg_console].d, lines);
  2325. ret = 0;
  2326. }
  2327. break;
  2328. case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
  2329. acquire_console_sem();
  2330. ignore_poke = 1;
  2331. do_blank_screen(0);
  2332. release_console_sem();
  2333. break;
  2334. case TIOCL_BLANKEDSCREEN:
  2335. ret = console_blanked;
  2336. break;
  2337. default:
  2338. ret = -EINVAL;
  2339. break;
  2340. }
  2341. return ret;
  2342. }
  2343. /*
  2344. * /dev/ttyN handling
  2345. */
  2346. static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  2347. {
  2348. int retval;
  2349. retval = do_con_write(tty, buf, count);
  2350. con_flush_chars(tty);
  2351. return retval;
  2352. }
  2353. static void con_put_char(struct tty_struct *tty, unsigned char ch)
  2354. {
  2355. if (in_interrupt())
  2356. return; /* n_r3964 calls put_char() from interrupt context */
  2357. do_con_write(tty, &ch, 1);
  2358. }
  2359. static int con_write_room(struct tty_struct *tty)
  2360. {
  2361. if (tty->stopped)
  2362. return 0;
  2363. return 4096; /* No limit, really; we're not buffering */
  2364. }
  2365. static int con_chars_in_buffer(struct tty_struct *tty)
  2366. {
  2367. return 0; /* we're not buffering */
  2368. }
  2369. /*
  2370. * con_throttle and con_unthrottle are only used for
  2371. * paste_selection(), which has to stuff in a large number of
  2372. * characters...
  2373. */
  2374. static void con_throttle(struct tty_struct *tty)
  2375. {
  2376. }
  2377. static void con_unthrottle(struct tty_struct *tty)
  2378. {
  2379. struct vc_data *vc = tty->driver_data;
  2380. wake_up_interruptible(&vc->paste_wait);
  2381. }
  2382. /*
  2383. * Turn the Scroll-Lock LED on when the tty is stopped
  2384. */
  2385. static void con_stop(struct tty_struct *tty)
  2386. {
  2387. int console_num;
  2388. if (!tty)
  2389. return;
  2390. console_num = tty->index;
  2391. if (!vc_cons_allocated(console_num))
  2392. return;
  2393. set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
  2394. set_leds();
  2395. }
  2396. /*
  2397. * Turn the Scroll-Lock LED off when the console is started
  2398. */
  2399. static void con_start(struct tty_struct *tty)
  2400. {
  2401. int console_num;
  2402. if (!tty)
  2403. return;
  2404. console_num = tty->index;
  2405. if (!vc_cons_allocated(console_num))
  2406. return;
  2407. clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
  2408. set_leds();
  2409. }
  2410. static void con_flush_chars(struct tty_struct *tty)
  2411. {
  2412. struct vc_data *vc;
  2413. if (in_interrupt()) /* from flush_to_ldisc */
  2414. return;
  2415. /* if we race with con_close(), vt may be null */
  2416. acquire_console_sem();
  2417. vc = tty->driver_data;
  2418. if (vc)
  2419. set_cursor(vc);
  2420. release_console_sem();
  2421. }
  2422. /*
  2423. * Allocate the console screen memory.
  2424. */
  2425. static int con_open(struct tty_struct *tty, struct file *filp)
  2426. {
  2427. unsigned int currcons = tty->index;
  2428. int ret = 0;
  2429. acquire_console_sem();
  2430. if (tty->driver_data == NULL) {
  2431. ret = vc_allocate(currcons);
  2432. if (ret == 0) {
  2433. struct vc_data *vc = vc_cons[currcons].d;
  2434. tty->driver_data = vc;
  2435. vc->vc_tty = tty;
  2436. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  2437. tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
  2438. tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
  2439. }
  2440. release_console_sem();
  2441. vcs_make_sysfs(tty);
  2442. return ret;
  2443. }
  2444. }
  2445. release_console_sem();
  2446. return ret;
  2447. }
  2448. /*
  2449. * We take tty_mutex in here to prevent another thread from coming in via init_dev
  2450. * and taking a ref against the tty while we're in the process of forgetting
  2451. * about it and cleaning things up.
  2452. *
  2453. * This is because vcs_remove_sysfs() can sleep and will drop the BKL.
  2454. */
  2455. static void con_close(struct tty_struct *tty, struct file *filp)
  2456. {
  2457. mutex_lock(&tty_mutex);
  2458. acquire_console_sem();
  2459. if (tty && tty->count == 1) {
  2460. struct vc_data *vc = tty->driver_data;
  2461. if (vc)
  2462. vc->vc_tty = NULL;
  2463. tty->driver_data = NULL;
  2464. release_console_sem();
  2465. vcs_remove_sysfs(tty);
  2466. mutex_unlock(&tty_mutex);
  2467. /*
  2468. * tty_mutex is released, but we still hold BKL, so there is
  2469. * still exclusion against init_dev()
  2470. */
  2471. return;
  2472. }
  2473. release_console_sem();
  2474. mutex_unlock(&tty_mutex);
  2475. }
  2476. static int default_italic_color = 2; // green (ASCII)
  2477. static int default_underline_color = 3; // cyan (ASCII)
  2478. module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
  2479. module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
  2480. static void vc_init(struct vc_data *vc, unsigned int rows,
  2481. unsigned int cols, int do_clear)
  2482. {
  2483. int j, k ;
  2484. vc->vc_cols = cols;
  2485. vc->vc_rows = rows;
  2486. vc->vc_size_row = cols << 1;
  2487. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  2488. set_origin(vc);
  2489. vc->vc_pos = vc->vc_origin;
  2490. reset_vc(vc);
  2491. for (j=k=0; j<16; j++) {
  2492. vc->vc_palette[k++] = default_red[j] ;
  2493. vc->vc_palette[k++] = default_grn[j] ;
  2494. vc->vc_palette[k++] = default_blu[j] ;
  2495. }
  2496. vc->vc_def_color = 0x07; /* white */
  2497. vc->vc_ulcolor = default_underline_color;
  2498. vc->vc_itcolor = default_italic_color;
  2499. vc->vc_halfcolor = 0x08; /* grey */
  2500. init_waitqueue_head(&vc->paste_wait);
  2501. reset_terminal(vc, do_clear);
  2502. }
  2503. /*
  2504. * This routine initializes console interrupts, and does nothing
  2505. * else. If you want the screen to clear, call tty_write with
  2506. * the appropriate escape-sequence.
  2507. */
  2508. static int __init con_init(void)
  2509. {
  2510. const char *display_desc = NULL;
  2511. struct vc_data *vc;
  2512. unsigned int currcons = 0, i;
  2513. acquire_console_sem();
  2514. if (conswitchp)
  2515. display_desc = conswitchp->con_startup();
  2516. if (!display_desc) {
  2517. fg_console = 0;
  2518. release_console_sem();
  2519. return 0;
  2520. }
  2521. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2522. struct con_driver *con_driver = &registered_con_driver[i];
  2523. if (con_driver->con == NULL) {
  2524. con_driver->con = conswitchp;
  2525. con_driver->desc = display_desc;
  2526. con_driver->flag = CON_DRIVER_FLAG_INIT;
  2527. con_driver->first = 0;
  2528. con_driver->last = MAX_NR_CONSOLES - 1;
  2529. break;
  2530. }
  2531. }
  2532. for (i = 0; i < MAX_NR_CONSOLES; i++)
  2533. con_driver_map[i] = conswitchp;
  2534. if (blankinterval) {
  2535. blank_state = blank_normal_wait;
  2536. mod_timer(&console_timer, jiffies + blankinterval);
  2537. }
  2538. /*
  2539. * kmalloc is not running yet - we use the bootmem allocator.
  2540. */
  2541. for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
  2542. vc_cons[currcons].d = vc = alloc_bootmem(sizeof(struct vc_data));
  2543. INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
  2544. visual_init(vc, currcons, 1);
  2545. vc->vc_screenbuf = (unsigned short *)alloc_bootmem(vc->vc_screenbuf_size);
  2546. vc->vc_kmalloced = 0;
  2547. vc_init(vc, vc->vc_rows, vc->vc_cols,
  2548. currcons || !vc->vc_sw->con_save_screen);
  2549. }
  2550. currcons = fg_console = 0;
  2551. master_display_fg = vc = vc_cons[currcons].d;
  2552. set_origin(vc);
  2553. save_screen(vc);
  2554. gotoxy(vc, vc->vc_x, vc->vc_y);
  2555. csi_J(vc, 0);
  2556. update_screen(vc);
  2557. printk("Console: %s %s %dx%d",
  2558. vc->vc_can_do_color ? "colour" : "mono",
  2559. display_desc, vc->vc_cols, vc->vc_rows);
  2560. printable = 1;
  2561. printk("\n");
  2562. release_console_sem();
  2563. #ifdef CONFIG_VT_CONSOLE
  2564. register_console(&vt_console_driver);
  2565. #endif
  2566. return 0;
  2567. }
  2568. console_initcall(con_init);
  2569. static const struct tty_operations con_ops = {
  2570. .open = con_open,
  2571. .close = con_close,
  2572. .write = con_write,
  2573. .write_room = con_write_room,
  2574. .put_char = con_put_char,
  2575. .flush_chars = con_flush_chars,
  2576. .chars_in_buffer = con_chars_in_buffer,
  2577. .ioctl = vt_ioctl,
  2578. .stop = con_stop,
  2579. .start = con_start,
  2580. .throttle = con_throttle,
  2581. .unthrottle = con_unthrottle,
  2582. };
  2583. int __init vty_init(void)
  2584. {
  2585. vcs_init();
  2586. console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
  2587. if (!console_driver)
  2588. panic("Couldn't allocate console driver\n");
  2589. console_driver->owner = THIS_MODULE;
  2590. console_driver->name = "tty";
  2591. console_driver->name_base = 1;
  2592. console_driver->major = TTY_MAJOR;
  2593. console_driver->minor_start = 1;
  2594. console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  2595. console_driver->init_termios = tty_std_termios;
  2596. console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  2597. tty_set_operations(console_driver, &con_ops);
  2598. if (tty_register_driver(console_driver))
  2599. panic("Couldn't register console driver\n");
  2600. kbd_init();
  2601. console_map_init();
  2602. #ifdef CONFIG_PROM_CONSOLE
  2603. prom_con_init();
  2604. #endif
  2605. #ifdef CONFIG_MDA_CONSOLE
  2606. mda_console_init();
  2607. #endif
  2608. return 0;
  2609. }
  2610. #ifndef VT_SINGLE_DRIVER
  2611. #include <linux/device.h>
  2612. static struct class *vtconsole_class;
  2613. static int bind_con_driver(const struct consw *csw, int first, int last,
  2614. int deflt)
  2615. {
  2616. struct module *owner = csw->owner;
  2617. const char *desc = NULL;
  2618. struct con_driver *con_driver;
  2619. int i, j = -1, k = -1, retval = -ENODEV;
  2620. if (!try_module_get(owner))
  2621. return -ENODEV;
  2622. acquire_console_sem();
  2623. /* check if driver is registered */
  2624. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2625. con_driver = &registered_con_driver[i];
  2626. if (con_driver->con == csw) {
  2627. desc = con_driver->desc;
  2628. retval = 0;
  2629. break;
  2630. }
  2631. }
  2632. if (retval)
  2633. goto err;
  2634. if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
  2635. csw->con_startup();
  2636. con_driver->flag |= CON_DRIVER_FLAG_INIT;
  2637. }
  2638. if (deflt) {
  2639. if (conswitchp)
  2640. module_put(conswitchp->owner);
  2641. __module_get(owner);
  2642. conswitchp = csw;
  2643. }
  2644. first = max(first, con_driver->first);
  2645. last = min(last, con_driver->last);
  2646. for (i = first; i <= last; i++) {
  2647. int old_was_color;
  2648. struct vc_data *vc = vc_cons[i].d;
  2649. if (con_driver_map[i])
  2650. module_put(con_driver_map[i]->owner);
  2651. __module_get(owner);
  2652. con_driver_map[i] = csw;
  2653. if (!vc || !vc->vc_sw)
  2654. continue;
  2655. j = i;
  2656. if (CON_IS_VISIBLE(vc)) {
  2657. k = i;
  2658. save_screen(vc);
  2659. }
  2660. old_was_color = vc->vc_can_do_color;
  2661. vc->vc_sw->con_deinit(vc);
  2662. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  2663. visual_init(vc, i, 0);
  2664. set_origin(vc);
  2665. update_attr(vc);
  2666. /* If the console changed between mono <-> color, then
  2667. * the attributes in the screenbuf will be wrong. The
  2668. * following resets all attributes to something sane.
  2669. */
  2670. if (old_was_color != vc->vc_can_do_color)
  2671. clear_buffer_attributes(vc);
  2672. }
  2673. printk("Console: switching ");
  2674. if (!deflt)
  2675. printk("consoles %d-%d ", first+1, last+1);
  2676. if (j >= 0) {
  2677. struct vc_data *vc = vc_cons[j].d;
  2678. printk("to %s %s %dx%d\n",
  2679. vc->vc_can_do_color ? "colour" : "mono",
  2680. desc, vc->vc_cols, vc->vc_rows);
  2681. if (k >= 0) {
  2682. vc = vc_cons[k].d;
  2683. update_screen(vc);
  2684. }
  2685. } else
  2686. printk("to %s\n", desc);
  2687. retval = 0;
  2688. err:
  2689. release_console_sem();
  2690. module_put(owner);
  2691. return retval;
  2692. };
  2693. #ifdef CONFIG_VT_HW_CONSOLE_BINDING
  2694. static int con_is_graphics(const struct consw *csw, int first, int last)
  2695. {
  2696. int i, retval = 0;
  2697. for (i = first; i <= last; i++) {
  2698. struct vc_data *vc = vc_cons[i].d;
  2699. if (vc && vc->vc_mode == KD_GRAPHICS) {
  2700. retval = 1;
  2701. break;
  2702. }
  2703. }
  2704. return retval;
  2705. }
  2706. static int unbind_con_driver(const struct consw *csw, int first, int last,
  2707. int deflt)
  2708. {
  2709. struct module *owner = csw->owner;
  2710. const struct consw *defcsw = NULL;
  2711. struct con_driver *con_driver = NULL, *con_back = NULL;
  2712. int i, retval = -ENODEV;
  2713. if (!try_module_get(owner))
  2714. return -ENODEV;
  2715. acquire_console_sem();
  2716. /* check if driver is registered and if it is unbindable */
  2717. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2718. con_driver = &registered_con_driver[i];
  2719. if (con_driver->con == csw &&
  2720. con_driver->flag & CON_DRIVER_FLAG_MODULE) {
  2721. retval = 0;
  2722. break;
  2723. }
  2724. }
  2725. if (retval) {
  2726. release_console_sem();
  2727. goto err;
  2728. }
  2729. retval = -ENODEV;
  2730. /* check if backup driver exists */
  2731. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2732. con_back = &registered_con_driver[i];
  2733. if (con_back->con &&
  2734. !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
  2735. defcsw = con_back->con;
  2736. retval = 0;
  2737. break;
  2738. }
  2739. }
  2740. if (retval) {
  2741. release_console_sem();
  2742. goto err;
  2743. }
  2744. if (!con_is_bound(csw)) {
  2745. release_console_sem();
  2746. goto err;
  2747. }
  2748. first = max(first, con_driver->first);
  2749. last = min(last, con_driver->last);
  2750. for (i = first; i <= last; i++) {
  2751. if (con_driver_map[i] == csw) {
  2752. module_put(csw->owner);
  2753. con_driver_map[i] = NULL;
  2754. }
  2755. }
  2756. if (!con_is_bound(defcsw)) {
  2757. const struct consw *defconsw = conswitchp;
  2758. defcsw->con_startup();
  2759. con_back->flag |= CON_DRIVER_FLAG_INIT;
  2760. /*
  2761. * vgacon may change the default driver to point
  2762. * to dummycon, we restore it here...
  2763. */
  2764. conswitchp = defconsw;
  2765. }
  2766. if (!con_is_bound(csw))
  2767. con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
  2768. release_console_sem();
  2769. /* ignore return value, binding should not fail */
  2770. bind_con_driver(defcsw, first, last, deflt);
  2771. err:
  2772. module_put(owner);
  2773. return retval;
  2774. }
  2775. static int vt_bind(struct con_driver *con)
  2776. {
  2777. const struct consw *defcsw = NULL, *csw = NULL;
  2778. int i, more = 1, first = -1, last = -1, deflt = 0;
  2779. if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
  2780. con_is_graphics(con->con, con->first, con->last))
  2781. goto err;
  2782. csw = con->con;
  2783. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2784. struct con_driver *con = &registered_con_driver[i];
  2785. if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
  2786. defcsw = con->con;
  2787. break;
  2788. }
  2789. }
  2790. if (!defcsw)
  2791. goto err;
  2792. while (more) {
  2793. more = 0;
  2794. for (i = con->first; i <= con->last; i++) {
  2795. if (con_driver_map[i] == defcsw) {
  2796. if (first == -1)
  2797. first = i;
  2798. last = i;
  2799. more = 1;
  2800. } else if (first != -1)
  2801. break;
  2802. }
  2803. if (first == 0 && last == MAX_NR_CONSOLES -1)
  2804. deflt = 1;
  2805. if (first != -1)
  2806. bind_con_driver(csw, first, last, deflt);
  2807. first = -1;
  2808. last = -1;
  2809. deflt = 0;
  2810. }
  2811. err:
  2812. return 0;
  2813. }
  2814. static int vt_unbind(struct con_driver *con)
  2815. {
  2816. const struct consw *csw = NULL;
  2817. int i, more = 1, first = -1, last = -1, deflt = 0;
  2818. if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
  2819. con_is_graphics(con->con, con->first, con->last))
  2820. goto err;
  2821. csw = con->con;
  2822. while (more) {
  2823. more = 0;
  2824. for (i = con->first; i <= con->last; i++) {
  2825. if (con_driver_map[i] == csw) {
  2826. if (first == -1)
  2827. first = i;
  2828. last = i;
  2829. more = 1;
  2830. } else if (first != -1)
  2831. break;
  2832. }
  2833. if (first == 0 && last == MAX_NR_CONSOLES -1)
  2834. deflt = 1;
  2835. if (first != -1)
  2836. unbind_con_driver(csw, first, last, deflt);
  2837. first = -1;
  2838. last = -1;
  2839. deflt = 0;
  2840. }
  2841. err:
  2842. return 0;
  2843. }
  2844. #else
  2845. static inline int vt_bind(struct con_driver *con)
  2846. {
  2847. return 0;
  2848. }
  2849. static inline int vt_unbind(struct con_driver *con)
  2850. {
  2851. return 0;
  2852. }
  2853. #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
  2854. static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
  2855. const char *buf, size_t count)
  2856. {
  2857. struct con_driver *con = dev_get_drvdata(dev);
  2858. int bind = simple_strtoul(buf, NULL, 0);
  2859. if (bind)
  2860. vt_bind(con);
  2861. else
  2862. vt_unbind(con);
  2863. return count;
  2864. }
  2865. static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
  2866. char *buf)
  2867. {
  2868. struct con_driver *con = dev_get_drvdata(dev);
  2869. int bind = con_is_bound(con->con);
  2870. return snprintf(buf, PAGE_SIZE, "%i\n", bind);
  2871. }
  2872. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  2873. char *buf)
  2874. {
  2875. struct con_driver *con = dev_get_drvdata(dev);
  2876. return snprintf(buf, PAGE_SIZE, "%s %s\n",
  2877. (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
  2878. con->desc);
  2879. }
  2880. static struct device_attribute device_attrs[] = {
  2881. __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
  2882. __ATTR(name, S_IRUGO, show_name, NULL),
  2883. };
  2884. static int vtconsole_init_device(struct con_driver *con)
  2885. {
  2886. int i;
  2887. int error = 0;
  2888. con->flag |= CON_DRIVER_FLAG_ATTR;
  2889. dev_set_drvdata(con->dev, con);
  2890. for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
  2891. error = device_create_file(con->dev, &device_attrs[i]);
  2892. if (error)
  2893. break;
  2894. }
  2895. if (error) {
  2896. while (--i >= 0)
  2897. device_remove_file(con->dev, &device_attrs[i]);
  2898. con->flag &= ~CON_DRIVER_FLAG_ATTR;
  2899. }
  2900. return error;
  2901. }
  2902. static void vtconsole_deinit_device(struct con_driver *con)
  2903. {
  2904. int i;
  2905. if (con->flag & CON_DRIVER_FLAG_ATTR) {
  2906. for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
  2907. device_remove_file(con->dev, &device_attrs[i]);
  2908. con->flag &= ~CON_DRIVER_FLAG_ATTR;
  2909. }
  2910. }
  2911. /**
  2912. * con_is_bound - checks if driver is bound to the console
  2913. * @csw: console driver
  2914. *
  2915. * RETURNS: zero if unbound, nonzero if bound
  2916. *
  2917. * Drivers can call this and if zero, they should release
  2918. * all resources allocated on con_startup()
  2919. */
  2920. int con_is_bound(const struct consw *csw)
  2921. {
  2922. int i, bound = 0;
  2923. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  2924. if (con_driver_map[i] == csw) {
  2925. bound = 1;
  2926. break;
  2927. }
  2928. }
  2929. return bound;
  2930. }
  2931. EXPORT_SYMBOL(con_is_bound);
  2932. /**
  2933. * register_con_driver - register console driver to console layer
  2934. * @csw: console driver
  2935. * @first: the first console to take over, minimum value is 0
  2936. * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
  2937. *
  2938. * DESCRIPTION: This function registers a console driver which can later
  2939. * bind to a range of consoles specified by @first and @last. It will
  2940. * also initialize the console driver by calling con_startup().
  2941. */
  2942. int register_con_driver(const struct consw *csw, int first, int last)
  2943. {
  2944. struct module *owner = csw->owner;
  2945. struct con_driver *con_driver;
  2946. const char *desc;
  2947. int i, retval = 0;
  2948. if (!try_module_get(owner))
  2949. return -ENODEV;
  2950. acquire_console_sem();
  2951. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2952. con_driver = &registered_con_driver[i];
  2953. /* already registered */
  2954. if (con_driver->con == csw)
  2955. retval = -EINVAL;
  2956. }
  2957. if (retval)
  2958. goto err;
  2959. desc = csw->con_startup();
  2960. if (!desc)
  2961. goto err;
  2962. retval = -EINVAL;
  2963. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  2964. con_driver = &registered_con_driver[i];
  2965. if (con_driver->con == NULL) {
  2966. con_driver->con = csw;
  2967. con_driver->desc = desc;
  2968. con_driver->node = i;
  2969. con_driver->flag = CON_DRIVER_FLAG_MODULE |
  2970. CON_DRIVER_FLAG_INIT;
  2971. con_driver->first = first;
  2972. con_driver->last = last;
  2973. retval = 0;
  2974. break;
  2975. }
  2976. }
  2977. if (retval)
  2978. goto err;
  2979. con_driver->dev = device_create(vtconsole_class, NULL,
  2980. MKDEV(0, con_driver->node),
  2981. "vtcon%i", con_driver->node);
  2982. if (IS_ERR(con_driver->dev)) {
  2983. printk(KERN_WARNING "Unable to create device for %s; "
  2984. "errno = %ld\n", con_driver->desc,
  2985. PTR_ERR(con_driver->dev));
  2986. con_driver->dev = NULL;
  2987. } else {
  2988. vtconsole_init_device(con_driver);
  2989. }
  2990. err:
  2991. release_console_sem();
  2992. module_put(owner);
  2993. return retval;
  2994. }
  2995. EXPORT_SYMBOL(register_con_driver);
  2996. /**
  2997. * unregister_con_driver - unregister console driver from console layer
  2998. * @csw: console driver
  2999. *
  3000. * DESCRIPTION: All drivers that registers to the console layer must
  3001. * call this function upon exit, or if the console driver is in a state
  3002. * where it won't be able to handle console services, such as the
  3003. * framebuffer console without loaded framebuffer drivers.
  3004. *
  3005. * The driver must unbind first prior to unregistration.
  3006. */
  3007. int unregister_con_driver(const struct consw *csw)
  3008. {
  3009. int i, retval = -ENODEV;
  3010. acquire_console_sem();
  3011. /* cannot unregister a bound driver */
  3012. if (con_is_bound(csw))
  3013. goto err;
  3014. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3015. struct con_driver *con_driver = &registered_con_driver[i];
  3016. if (con_driver->con == csw &&
  3017. con_driver->flag & CON_DRIVER_FLAG_MODULE) {
  3018. vtconsole_deinit_device(con_driver);
  3019. device_destroy(vtconsole_class,
  3020. MKDEV(0, con_driver->node));
  3021. con_driver->con = NULL;
  3022. con_driver->desc = NULL;
  3023. con_driver->dev = NULL;
  3024. con_driver->node = 0;
  3025. con_driver->flag = 0;
  3026. con_driver->first = 0;
  3027. con_driver->last = 0;
  3028. retval = 0;
  3029. break;
  3030. }
  3031. }
  3032. err:
  3033. release_console_sem();
  3034. return retval;
  3035. }
  3036. EXPORT_SYMBOL(unregister_con_driver);
  3037. /*
  3038. * If we support more console drivers, this function is used
  3039. * when a driver wants to take over some existing consoles
  3040. * and become default driver for newly opened ones.
  3041. *
  3042. * take_over_console is basically a register followed by unbind
  3043. */
  3044. int take_over_console(const struct consw *csw, int first, int last, int deflt)
  3045. {
  3046. int err;
  3047. err = register_con_driver(csw, first, last);
  3048. if (!err)
  3049. bind_con_driver(csw, first, last, deflt);
  3050. return err;
  3051. }
  3052. /*
  3053. * give_up_console is a wrapper to unregister_con_driver. It will only
  3054. * work if driver is fully unbound.
  3055. */
  3056. void give_up_console(const struct consw *csw)
  3057. {
  3058. unregister_con_driver(csw);
  3059. }
  3060. static int __init vtconsole_class_init(void)
  3061. {
  3062. int i;
  3063. vtconsole_class = class_create(THIS_MODULE, "vtconsole");
  3064. if (IS_ERR(vtconsole_class)) {
  3065. printk(KERN_WARNING "Unable to create vt console class; "
  3066. "errno = %ld\n", PTR_ERR(vtconsole_class));
  3067. vtconsole_class = NULL;
  3068. }
  3069. /* Add system drivers to sysfs */
  3070. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3071. struct con_driver *con = &registered_con_driver[i];
  3072. if (con->con && !con->dev) {
  3073. con->dev = device_create(vtconsole_class, NULL,
  3074. MKDEV(0, con->node),
  3075. "vtcon%i", con->node);
  3076. if (IS_ERR(con->dev)) {
  3077. printk(KERN_WARNING "Unable to create "
  3078. "device for %s; errno = %ld\n",
  3079. con->desc, PTR_ERR(con->dev));
  3080. con->dev = NULL;
  3081. } else {
  3082. vtconsole_init_device(con);
  3083. }
  3084. }
  3085. }
  3086. return 0;
  3087. }
  3088. postcore_initcall(vtconsole_class_init);
  3089. #endif
  3090. /*
  3091. * Screen blanking
  3092. */
  3093. static int set_vesa_blanking(char __user *p)
  3094. {
  3095. unsigned int mode;
  3096. if (get_user(mode, p + 1))
  3097. return -EFAULT;
  3098. vesa_blank_mode = (mode < 4) ? mode : 0;
  3099. return 0;
  3100. }
  3101. void do_blank_screen(int entering_gfx)
  3102. {
  3103. struct vc_data *vc = vc_cons[fg_console].d;
  3104. int i;
  3105. WARN_CONSOLE_UNLOCKED();
  3106. if (console_blanked) {
  3107. if (blank_state == blank_vesa_wait) {
  3108. blank_state = blank_off;
  3109. vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
  3110. }
  3111. return;
  3112. }
  3113. if (blank_state != blank_normal_wait)
  3114. return;
  3115. blank_state = blank_off;
  3116. /* entering graphics mode? */
  3117. if (entering_gfx) {
  3118. hide_cursor(vc);
  3119. save_screen(vc);
  3120. vc->vc_sw->con_blank(vc, -1, 1);
  3121. console_blanked = fg_console + 1;
  3122. set_origin(vc);
  3123. return;
  3124. }
  3125. /* don't blank graphics */
  3126. if (vc->vc_mode != KD_TEXT) {
  3127. console_blanked = fg_console + 1;
  3128. return;
  3129. }
  3130. hide_cursor(vc);
  3131. del_timer_sync(&console_timer);
  3132. blank_timer_expired = 0;
  3133. save_screen(vc);
  3134. /* In case we need to reset origin, blanking hook returns 1 */
  3135. i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
  3136. console_blanked = fg_console + 1;
  3137. if (i)
  3138. set_origin(vc);
  3139. if (console_blank_hook && console_blank_hook(1))
  3140. return;
  3141. if (vesa_off_interval && vesa_blank_mode) {
  3142. blank_state = blank_vesa_wait;
  3143. mod_timer(&console_timer, jiffies + vesa_off_interval);
  3144. }
  3145. }
  3146. EXPORT_SYMBOL(do_blank_screen);
  3147. /*
  3148. * Called by timer as well as from vt_console_driver
  3149. */
  3150. void do_unblank_screen(int leaving_gfx)
  3151. {
  3152. struct vc_data *vc;
  3153. /* This should now always be called from a "sane" (read: can schedule)
  3154. * context for the sake of the low level drivers, except in the special
  3155. * case of oops_in_progress
  3156. */
  3157. if (!oops_in_progress)
  3158. might_sleep();
  3159. WARN_CONSOLE_UNLOCKED();
  3160. ignore_poke = 0;
  3161. if (!console_blanked)
  3162. return;
  3163. if (!vc_cons_allocated(fg_console)) {
  3164. /* impossible */
  3165. printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
  3166. return;
  3167. }
  3168. vc = vc_cons[fg_console].d;
  3169. if (vc->vc_mode != KD_TEXT)
  3170. return; /* but leave console_blanked != 0 */
  3171. if (blankinterval) {
  3172. mod_timer(&console_timer, jiffies + blankinterval);
  3173. blank_state = blank_normal_wait;
  3174. }
  3175. console_blanked = 0;
  3176. if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
  3177. /* Low-level driver cannot restore -> do it ourselves */
  3178. update_screen(vc);
  3179. if (console_blank_hook)
  3180. console_blank_hook(0);
  3181. set_palette(vc);
  3182. set_cursor(vc);
  3183. }
  3184. EXPORT_SYMBOL(do_unblank_screen);
  3185. /*
  3186. * This is called by the outside world to cause a forced unblank, mostly for
  3187. * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
  3188. * call it with 1 as an argument and so force a mode restore... that may kill
  3189. * X or at least garbage the screen but would also make the Oops visible...
  3190. */
  3191. void unblank_screen(void)
  3192. {
  3193. do_unblank_screen(0);
  3194. }
  3195. /*
  3196. * We defer the timer blanking to work queue so it can take the console mutex
  3197. * (console operations can still happen at irq time, but only from printk which
  3198. * has the console mutex. Not perfect yet, but better than no locking
  3199. */
  3200. static void blank_screen_t(unsigned long dummy)
  3201. {
  3202. if (unlikely(!keventd_up())) {
  3203. mod_timer(&console_timer, jiffies + blankinterval);
  3204. return;
  3205. }
  3206. blank_timer_expired = 1;
  3207. schedule_work(&console_work);
  3208. }
  3209. void poke_blanked_console(void)
  3210. {
  3211. WARN_CONSOLE_UNLOCKED();
  3212. /* Add this so we quickly catch whoever might call us in a non
  3213. * safe context. Nowadays, unblank_screen() isn't to be called in
  3214. * atomic contexts and is allowed to schedule (with the special case
  3215. * of oops_in_progress, but that isn't of any concern for this
  3216. * function. --BenH.
  3217. */
  3218. might_sleep();
  3219. /* This isn't perfectly race free, but a race here would be mostly harmless,
  3220. * at worse, we'll do a spurrious blank and it's unlikely
  3221. */
  3222. del_timer(&console_timer);
  3223. blank_timer_expired = 0;
  3224. if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
  3225. return;
  3226. if (console_blanked)
  3227. unblank_screen();
  3228. else if (blankinterval) {
  3229. mod_timer(&console_timer, jiffies + blankinterval);
  3230. blank_state = blank_normal_wait;
  3231. }
  3232. }
  3233. /*
  3234. * Palettes
  3235. */
  3236. static void set_palette(struct vc_data *vc)
  3237. {
  3238. WARN_CONSOLE_UNLOCKED();
  3239. if (vc->vc_mode != KD_GRAPHICS)
  3240. vc->vc_sw->con_set_palette(vc, color_table);
  3241. }
  3242. static int set_get_cmap(unsigned char __user *arg, int set)
  3243. {
  3244. int i, j, k;
  3245. WARN_CONSOLE_UNLOCKED();
  3246. for (i = 0; i < 16; i++)
  3247. if (set) {
  3248. get_user(default_red[i], arg++);
  3249. get_user(default_grn[i], arg++);
  3250. get_user(default_blu[i], arg++);
  3251. } else {
  3252. put_user(default_red[i], arg++);
  3253. put_user(default_grn[i], arg++);
  3254. put_user(default_blu[i], arg++);
  3255. }
  3256. if (set) {
  3257. for (i = 0; i < MAX_NR_CONSOLES; i++)
  3258. if (vc_cons_allocated(i)) {
  3259. for (j = k = 0; j < 16; j++) {
  3260. vc_cons[i].d->vc_palette[k++] = default_red[j];
  3261. vc_cons[i].d->vc_palette[k++] = default_grn[j];
  3262. vc_cons[i].d->vc_palette[k++] = default_blu[j];
  3263. }
  3264. set_palette(vc_cons[i].d);
  3265. }
  3266. }
  3267. return 0;
  3268. }
  3269. /*
  3270. * Load palette into the DAC registers. arg points to a colour
  3271. * map, 3 bytes per colour, 16 colours, range from 0 to 255.
  3272. */
  3273. int con_set_cmap(unsigned char __user *arg)
  3274. {
  3275. int rc;
  3276. acquire_console_sem();
  3277. rc = set_get_cmap (arg,1);
  3278. release_console_sem();
  3279. return rc;
  3280. }
  3281. int con_get_cmap(unsigned char __user *arg)
  3282. {
  3283. int rc;
  3284. acquire_console_sem();
  3285. rc = set_get_cmap (arg,0);
  3286. release_console_sem();
  3287. return rc;
  3288. }
  3289. void reset_palette(struct vc_data *vc)
  3290. {
  3291. int j, k;
  3292. for (j=k=0; j<16; j++) {
  3293. vc->vc_palette[k++] = default_red[j];
  3294. vc->vc_palette[k++] = default_grn[j];
  3295. vc->vc_palette[k++] = default_blu[j];
  3296. }
  3297. set_palette(vc);
  3298. }
  3299. /*
  3300. * Font switching
  3301. *
  3302. * Currently we only support fonts up to 32 pixels wide, at a maximum height
  3303. * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
  3304. * depending on width) reserved for each character which is kinda wasty, but
  3305. * this is done in order to maintain compatibility with the EGA/VGA fonts. It
  3306. * is upto the actual low-level console-driver convert data into its favorite
  3307. * format (maybe we should add a `fontoffset' field to the `display'
  3308. * structure so we won't have to convert the fontdata all the time.
  3309. * /Jes
  3310. */
  3311. #define max_font_size 65536
  3312. static int con_font_get(struct vc_data *vc, struct console_font_op *op)
  3313. {
  3314. struct console_font font;
  3315. int rc = -EINVAL;
  3316. int c;
  3317. if (vc->vc_mode != KD_TEXT)
  3318. return -EINVAL;
  3319. if (op->data) {
  3320. font.data = kmalloc(max_font_size, GFP_KERNEL);
  3321. if (!font.data)
  3322. return -ENOMEM;
  3323. } else
  3324. font.data = NULL;
  3325. acquire_console_sem();
  3326. if (vc->vc_sw->con_font_get)
  3327. rc = vc->vc_sw->con_font_get(vc, &font);
  3328. else
  3329. rc = -ENOSYS;
  3330. release_console_sem();
  3331. if (rc)
  3332. goto out;
  3333. c = (font.width+7)/8 * 32 * font.charcount;
  3334. if (op->data && font.charcount > op->charcount)
  3335. rc = -ENOSPC;
  3336. if (!(op->flags & KD_FONT_FLAG_OLD)) {
  3337. if (font.width > op->width || font.height > op->height)
  3338. rc = -ENOSPC;
  3339. } else {
  3340. if (font.width != 8)
  3341. rc = -EIO;
  3342. else if ((op->height && font.height > op->height) ||
  3343. font.height > 32)
  3344. rc = -ENOSPC;
  3345. }
  3346. if (rc)
  3347. goto out;
  3348. op->height = font.height;
  3349. op->width = font.width;
  3350. op->charcount = font.charcount;
  3351. if (op->data && copy_to_user(op->data, font.data, c))
  3352. rc = -EFAULT;
  3353. out:
  3354. kfree(font.data);
  3355. return rc;
  3356. }
  3357. static int con_font_set(struct vc_data *vc, struct console_font_op *op)
  3358. {
  3359. struct console_font font;
  3360. int rc = -EINVAL;
  3361. int size;
  3362. if (vc->vc_mode != KD_TEXT)
  3363. return -EINVAL;
  3364. if (!op->data)
  3365. return -EINVAL;
  3366. if (op->charcount > 512)
  3367. return -EINVAL;
  3368. if (!op->height) { /* Need to guess font height [compat] */
  3369. int h, i;
  3370. u8 __user *charmap = op->data;
  3371. u8 tmp;
  3372. /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
  3373. so that we can get rid of this soon */
  3374. if (!(op->flags & KD_FONT_FLAG_OLD))
  3375. return -EINVAL;
  3376. for (h = 32; h > 0; h--)
  3377. for (i = 0; i < op->charcount; i++) {
  3378. if (get_user(tmp, &charmap[32*i+h-1]))
  3379. return -EFAULT;
  3380. if (tmp)
  3381. goto nonzero;
  3382. }
  3383. return -EINVAL;
  3384. nonzero:
  3385. op->height = h;
  3386. }
  3387. if (op->width <= 0 || op->width > 32 || op->height > 32)
  3388. return -EINVAL;
  3389. size = (op->width+7)/8 * 32 * op->charcount;
  3390. if (size > max_font_size)
  3391. return -ENOSPC;
  3392. font.charcount = op->charcount;
  3393. font.height = op->height;
  3394. font.width = op->width;
  3395. font.data = kmalloc(size, GFP_KERNEL);
  3396. if (!font.data)
  3397. return -ENOMEM;
  3398. if (copy_from_user(font.data, op->data, size)) {
  3399. kfree(font.data);
  3400. return -EFAULT;
  3401. }
  3402. acquire_console_sem();
  3403. if (vc->vc_sw->con_font_set)
  3404. rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
  3405. else
  3406. rc = -ENOSYS;
  3407. release_console_sem();
  3408. kfree(font.data);
  3409. return rc;
  3410. }
  3411. static int con_font_default(struct vc_data *vc, struct console_font_op *op)
  3412. {
  3413. struct console_font font = {.width = op->width, .height = op->height};
  3414. char name[MAX_FONT_NAME];
  3415. char *s = name;
  3416. int rc;
  3417. if (vc->vc_mode != KD_TEXT)
  3418. return -EINVAL;
  3419. if (!op->data)
  3420. s = NULL;
  3421. else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
  3422. return -EFAULT;
  3423. else
  3424. name[MAX_FONT_NAME - 1] = 0;
  3425. acquire_console_sem();
  3426. if (vc->vc_sw->con_font_default)
  3427. rc = vc->vc_sw->con_font_default(vc, &font, s);
  3428. else
  3429. rc = -ENOSYS;
  3430. release_console_sem();
  3431. if (!rc) {
  3432. op->width = font.width;
  3433. op->height = font.height;
  3434. }
  3435. return rc;
  3436. }
  3437. static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
  3438. {
  3439. int con = op->height;
  3440. int rc;
  3441. if (vc->vc_mode != KD_TEXT)
  3442. return -EINVAL;
  3443. acquire_console_sem();
  3444. if (!vc->vc_sw->con_font_copy)
  3445. rc = -ENOSYS;
  3446. else if (con < 0 || !vc_cons_allocated(con))
  3447. rc = -ENOTTY;
  3448. else if (con == vc->vc_num) /* nothing to do */
  3449. rc = 0;
  3450. else
  3451. rc = vc->vc_sw->con_font_copy(vc, con);
  3452. release_console_sem();
  3453. return rc;
  3454. }
  3455. int con_font_op(struct vc_data *vc, struct console_font_op *op)
  3456. {
  3457. switch (op->op) {
  3458. case KD_FONT_OP_SET:
  3459. return con_font_set(vc, op);
  3460. case KD_FONT_OP_GET:
  3461. return con_font_get(vc, op);
  3462. case KD_FONT_OP_SET_DEFAULT:
  3463. return con_font_default(vc, op);
  3464. case KD_FONT_OP_COPY:
  3465. return con_font_copy(vc, op);
  3466. }
  3467. return -ENOSYS;
  3468. }
  3469. /*
  3470. * Interface exported to selection and vcs.
  3471. */
  3472. /* used by selection */
  3473. u16 screen_glyph(struct vc_data *vc, int offset)
  3474. {
  3475. u16 w = scr_readw(screenpos(vc, offset, 1));
  3476. u16 c = w & 0xff;
  3477. if (w & vc->vc_hi_font_mask)
  3478. c |= 0x100;
  3479. return c;
  3480. }
  3481. /* used by vcs - note the word offset */
  3482. unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
  3483. {
  3484. return screenpos(vc, 2 * w_offset, viewed);
  3485. }
  3486. void getconsxy(struct vc_data *vc, unsigned char *p)
  3487. {
  3488. p[0] = vc->vc_x;
  3489. p[1] = vc->vc_y;
  3490. }
  3491. void putconsxy(struct vc_data *vc, unsigned char *p)
  3492. {
  3493. hide_cursor(vc);
  3494. gotoxy(vc, p[0], p[1]);
  3495. set_cursor(vc);
  3496. }
  3497. u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
  3498. {
  3499. if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
  3500. return softcursor_original;
  3501. return scr_readw(org);
  3502. }
  3503. void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
  3504. {
  3505. scr_writew(val, org);
  3506. if ((unsigned long)org == vc->vc_pos) {
  3507. softcursor_original = -1;
  3508. add_softcursor(vc);
  3509. }
  3510. }
  3511. /*
  3512. * Visible symbols for modules
  3513. */
  3514. EXPORT_SYMBOL(color_table);
  3515. EXPORT_SYMBOL(default_red);
  3516. EXPORT_SYMBOL(default_grn);
  3517. EXPORT_SYMBOL(default_blu);
  3518. EXPORT_SYMBOL(update_region);
  3519. EXPORT_SYMBOL(redraw_screen);
  3520. EXPORT_SYMBOL(vc_resize);
  3521. EXPORT_SYMBOL(vc_lock_resize);
  3522. EXPORT_SYMBOL(fg_console);
  3523. EXPORT_SYMBOL(console_blank_hook);
  3524. EXPORT_SYMBOL(console_blanked);
  3525. EXPORT_SYMBOL(vc_cons);
  3526. #ifndef VT_SINGLE_DRIVER
  3527. EXPORT_SYMBOL(take_over_console);
  3528. EXPORT_SYMBOL(give_up_console);
  3529. #endif