vt.c 97 KB

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