vt.c 95 KB

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