vt.c 94 KB

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