vt.c 101 KB

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