vt.c 94 KB

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