vt.c 94 KB

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