vt.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. */
  5. /*
  6. * Hopefully this will be a rather complete VT102 implementation.
  7. *
  8. * Beeping thanks to John T Kohl.
  9. *
  10. * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
  11. * Chars, and VT100 enhancements by Peter MacDonald.
  12. *
  13. * Copy and paste function by Andrew Haylett,
  14. * some enhancements by Alessandro Rubini.
  15. *
  16. * Code to check for different video-cards mostly by Galen Hunt,
  17. * <g-hunt@ee.utah.edu>
  18. *
  19. * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
  20. * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
  21. *
  22. * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
  23. * Resizing of consoles, aeb, 940926
  24. *
  25. * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
  26. * <poe@daimi.aau.dk>
  27. *
  28. * User-defined bell sound, new setterm control sequences and printk
  29. * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
  30. *
  31. * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
  32. *
  33. * Merge with the abstract console driver by Geert Uytterhoeven
  34. * <geert@linux-m68k.org>, Jan 1997.
  35. *
  36. * Original m68k console driver modifications by
  37. *
  38. * - Arno Griffioen <arno@usn.nl>
  39. * - David Carter <carter@cs.bris.ac.uk>
  40. *
  41. * The abstract console driver provides a generic interface for a text
  42. * console. It supports VGA text mode, frame buffer based graphical consoles
  43. * and special graphics processors that are only accessible through some
  44. * registers (e.g. a TMS340x0 GSP).
  45. *
  46. * The interface to the hardware is specified using a special structure
  47. * (struct consw) which contains function pointers to console operations
  48. * (see <linux/console.h> for more information).
  49. *
  50. * Support for changeable cursor shape
  51. * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
  52. *
  53. * Ported to i386 and con_scrolldelta fixed
  54. * by Emmanuel Marty <core@ggi-project.org>, April 1998
  55. *
  56. * Resurrected character buffers in videoram plus lots of other trickery
  57. * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
  58. *
  59. * Removed old-style timers, introduced console_timer, made timer
  60. * deletion SMP-safe. 17Jun00, Andrew Morton
  61. *
  62. * Removed console_lock, enabled interrupts across all console operations
  63. * 13 March 2001, Andrew Morton
  64. *
  65. * Fixed UTF-8 mode so alternate charset modes always work according
  66. * to control sequences interpreted in do_con_trol function
  67. * preserving backward VT100 semigraphics compatibility,
  68. * malformed UTF sequences represented as sequences of replacement glyphs,
  69. * original codes or '?' as a last resort if replacement glyph is undefined
  70. * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
  71. */
  72. #include <linux/module.h>
  73. #include <linux/types.h>
  74. #include <linux/sched/signal.h>
  75. #include <linux/tty.h>
  76. #include <linux/tty_flip.h>
  77. #include <linux/kernel.h>
  78. #include <linux/string.h>
  79. #include <linux/errno.h>
  80. #include <linux/kd.h>
  81. #include <linux/slab.h>
  82. #include <linux/vmalloc.h>
  83. #include <linux/major.h>
  84. #include <linux/mm.h>
  85. #include <linux/console.h>
  86. #include <linux/init.h>
  87. #include <linux/mutex.h>
  88. #include <linux/vt_kern.h>
  89. #include <linux/selection.h>
  90. #include <linux/tiocl.h>
  91. #include <linux/kbd_kern.h>
  92. #include <linux/consolemap.h>
  93. #include <linux/timer.h>
  94. #include <linux/interrupt.h>
  95. #include <linux/workqueue.h>
  96. #include <linux/pm.h>
  97. #include <linux/font.h>
  98. #include <linux/bitops.h>
  99. #include <linux/notifier.h>
  100. #include <linux/device.h>
  101. #include <linux/io.h>
  102. #include <linux/uaccess.h>
  103. #include <linux/kdb.h>
  104. #include <linux/ctype.h>
  105. #include <linux/bsearch.h>
  106. #include <linux/gcd.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. #define CON_DRIVER_FLAG_ZOMBIE 8
  112. struct con_driver {
  113. const struct consw *con;
  114. const char *desc;
  115. struct device *dev;
  116. int node;
  117. int first;
  118. int last;
  119. int flag;
  120. };
  121. static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
  122. const struct consw *conswitchp;
  123. /*
  124. * Here is the default bell parameters: 750HZ, 1/8th of a second
  125. */
  126. #define DEFAULT_BELL_PITCH 750
  127. #define DEFAULT_BELL_DURATION (HZ/8)
  128. #define DEFAULT_CURSOR_BLINK_MS 200
  129. struct vc vc_cons [MAX_NR_CONSOLES];
  130. EXPORT_SYMBOL(vc_cons);
  131. static const struct consw *con_driver_map[MAX_NR_CONSOLES];
  132. static int con_open(struct tty_struct *, struct file *);
  133. static void vc_init(struct vc_data *vc, int do_clear);
  134. static void gotoxy(struct vc_data *vc, int new_x, int new_y);
  135. static void save_cur(struct vc_data *vc);
  136. static void reset_terminal(struct vc_data *vc, int do_clear);
  137. static void con_flush_chars(struct tty_struct *tty);
  138. static int set_vesa_blanking(u8 __user *mode);
  139. static void set_cursor(struct vc_data *vc);
  140. static void hide_cursor(struct vc_data *vc);
  141. static void console_callback(struct work_struct *ignored);
  142. static void con_driver_unregister_callback(struct work_struct *ignored);
  143. static void blank_screen_t(struct timer_list *unused);
  144. static void set_palette(struct vc_data *vc);
  145. static void unblank_screen(void);
  146. #define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
  147. int default_utf8 = true;
  148. module_param(default_utf8, int, S_IRUGO | S_IWUSR);
  149. int global_cursor_default = -1;
  150. module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
  151. EXPORT_SYMBOL(global_cursor_default);
  152. static int cur_default = CUR_UNDERLINE;
  153. module_param(cur_default, int, S_IRUGO | S_IWUSR);
  154. /*
  155. * ignore_poke: don't unblank the screen when things are typed. This is
  156. * mainly for the privacy of braille terminal users.
  157. */
  158. static int ignore_poke;
  159. int do_poke_blanked_console;
  160. int console_blanked;
  161. EXPORT_SYMBOL(console_blanked);
  162. static enum vesa_blank_mode vesa_blank_mode;
  163. static int vesa_off_interval;
  164. static int blankinterval;
  165. core_param(consoleblank, blankinterval, int, 0444);
  166. static DECLARE_WORK(console_work, console_callback);
  167. static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
  168. /*
  169. * fg_console is the current virtual console,
  170. * last_console is the last used one,
  171. * want_console is the console we want to switch to,
  172. * saved_* variants are for save/restore around kernel debugger enter/leave
  173. */
  174. int fg_console;
  175. EXPORT_SYMBOL(fg_console);
  176. int last_console;
  177. int want_console = -1;
  178. static int saved_fg_console;
  179. static int saved_last_console;
  180. static int saved_want_console;
  181. static int saved_vc_mode;
  182. static int saved_console_blanked;
  183. /*
  184. * For each existing display, we have a pointer to console currently visible
  185. * on that display, allowing consoles other than fg_console to be refreshed
  186. * appropriately. Unless the low-level driver supplies its own display_fg
  187. * variable, we use this one for the "master display".
  188. */
  189. static struct vc_data *master_display_fg;
  190. /*
  191. * Unfortunately, we need to delay tty echo when we're currently writing to the
  192. * console since the code is (and always was) not re-entrant, so we schedule
  193. * all flip requests to process context with schedule-task() and run it from
  194. * console_callback().
  195. */
  196. /*
  197. * For the same reason, we defer scrollback to the console callback.
  198. */
  199. static int scrollback_delta;
  200. /*
  201. * Hook so that the power management routines can (un)blank
  202. * the console on our behalf.
  203. */
  204. int (*console_blank_hook)(int);
  205. EXPORT_SYMBOL(console_blank_hook);
  206. static DEFINE_TIMER(console_timer, blank_screen_t);
  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. * /sys/class/tty/tty0/
  216. *
  217. * the attribute 'active' contains the name of the current vc
  218. * console and it supports poll() to detect vc switches
  219. */
  220. static struct device *tty0dev;
  221. /*
  222. * Notifier list for console events.
  223. */
  224. static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
  225. int register_vt_notifier(struct notifier_block *nb)
  226. {
  227. return atomic_notifier_chain_register(&vt_notifier_list, nb);
  228. }
  229. EXPORT_SYMBOL_GPL(register_vt_notifier);
  230. int unregister_vt_notifier(struct notifier_block *nb)
  231. {
  232. return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
  233. }
  234. EXPORT_SYMBOL_GPL(unregister_vt_notifier);
  235. static void notify_write(struct vc_data *vc, unsigned int unicode)
  236. {
  237. struct vt_notifier_param param = { .vc = vc, .c = unicode };
  238. atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
  239. }
  240. static void notify_update(struct vc_data *vc)
  241. {
  242. struct vt_notifier_param param = { .vc = vc };
  243. atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
  244. }
  245. /*
  246. * Low-Level Functions
  247. */
  248. static inline bool con_is_fg(const struct vc_data *vc)
  249. {
  250. return vc->vc_num == fg_console;
  251. }
  252. static inline bool con_should_update(const struct vc_data *vc)
  253. {
  254. return con_is_visible(vc) && !console_blanked;
  255. }
  256. static inline u16 *screenpos(const struct vc_data *vc, unsigned int offset,
  257. bool viewed)
  258. {
  259. unsigned long origin = viewed ? vc->vc_visible_origin : vc->vc_origin;
  260. return (u16 *)(origin + offset);
  261. }
  262. static void con_putc(struct vc_data *vc, u16 ca, unsigned int y, unsigned int x)
  263. {
  264. if (vc->vc_sw->con_putc)
  265. vc->vc_sw->con_putc(vc, ca, y, x);
  266. else
  267. vc->vc_sw->con_putcs(vc, &ca, 1, y, x);
  268. }
  269. /* Called from the keyboard irq path.. */
  270. static inline void scrolldelta(int lines)
  271. {
  272. /* FIXME */
  273. /* scrolldelta needs some kind of consistency lock, but the BKL was
  274. and still is not protecting versus the scheduled back end */
  275. scrollback_delta += lines;
  276. schedule_console_callback();
  277. }
  278. void schedule_console_callback(void)
  279. {
  280. schedule_work(&console_work);
  281. }
  282. /*
  283. * Code to manage unicode-based screen buffers
  284. */
  285. /*
  286. * Our screen buffer is preceded by an array of line pointers so that
  287. * scrolling only implies some pointer shuffling.
  288. */
  289. static u32 **vc_uniscr_alloc(unsigned int cols, unsigned int rows)
  290. {
  291. u32 **uni_lines;
  292. void *p;
  293. unsigned int memsize, i, col_size = cols * sizeof(**uni_lines);
  294. /* allocate everything in one go */
  295. memsize = col_size * rows;
  296. memsize += rows * sizeof(*uni_lines);
  297. uni_lines = vzalloc(memsize);
  298. if (!uni_lines)
  299. return NULL;
  300. /* initial line pointers */
  301. p = uni_lines + rows;
  302. for (i = 0; i < rows; i++) {
  303. uni_lines[i] = p;
  304. p += col_size;
  305. }
  306. return uni_lines;
  307. }
  308. static void vc_uniscr_free(u32 **uni_lines)
  309. {
  310. vfree(uni_lines);
  311. }
  312. static void vc_uniscr_set(struct vc_data *vc, u32 **new_uni_lines)
  313. {
  314. vc_uniscr_free(vc->vc_uni_lines);
  315. vc->vc_uni_lines = new_uni_lines;
  316. }
  317. static void vc_uniscr_putc(struct vc_data *vc, u32 uc)
  318. {
  319. if (vc->vc_uni_lines)
  320. vc->vc_uni_lines[vc->state.y][vc->state.x] = uc;
  321. }
  322. static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr)
  323. {
  324. if (vc->vc_uni_lines) {
  325. u32 *ln = vc->vc_uni_lines[vc->state.y];
  326. unsigned int x = vc->state.x, cols = vc->vc_cols;
  327. memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln));
  328. memset32(&ln[x], ' ', nr);
  329. }
  330. }
  331. static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr)
  332. {
  333. if (vc->vc_uni_lines) {
  334. u32 *ln = vc->vc_uni_lines[vc->state.y];
  335. unsigned int x = vc->state.x, cols = vc->vc_cols;
  336. memmove(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln));
  337. memset32(&ln[cols - nr], ' ', nr);
  338. }
  339. }
  340. static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x,
  341. unsigned int nr)
  342. {
  343. if (vc->vc_uni_lines)
  344. memset32(&vc->vc_uni_lines[vc->state.y][x], ' ', nr);
  345. }
  346. static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y,
  347. unsigned int nr)
  348. {
  349. if (vc->vc_uni_lines)
  350. while (nr--)
  351. memset32(vc->vc_uni_lines[y++], ' ', vc->vc_cols);
  352. }
  353. /* juggling array rotation algorithm (complexity O(N), size complexity O(1)) */
  354. static void juggle_array(u32 **array, unsigned int size, unsigned int nr)
  355. {
  356. unsigned int gcd_idx;
  357. for (gcd_idx = 0; gcd_idx < gcd(nr, size); gcd_idx++) {
  358. u32 *gcd_idx_val = array[gcd_idx];
  359. unsigned int dst_idx = gcd_idx;
  360. while (1) {
  361. unsigned int src_idx = (dst_idx + nr) % size;
  362. if (src_idx == gcd_idx)
  363. break;
  364. array[dst_idx] = array[src_idx];
  365. dst_idx = src_idx;
  366. }
  367. array[dst_idx] = gcd_idx_val;
  368. }
  369. }
  370. static void vc_uniscr_scroll(struct vc_data *vc, unsigned int top,
  371. unsigned int bottom, enum con_scroll dir,
  372. unsigned int nr)
  373. {
  374. u32 **uni_lines = vc->vc_uni_lines;
  375. unsigned int size = bottom - top;
  376. if (!uni_lines)
  377. return;
  378. if (dir == SM_DOWN) {
  379. juggle_array(&uni_lines[top], size, size - nr);
  380. vc_uniscr_clear_lines(vc, top, nr);
  381. } else {
  382. juggle_array(&uni_lines[top], size, nr);
  383. vc_uniscr_clear_lines(vc, bottom - nr, nr);
  384. }
  385. }
  386. static void vc_uniscr_copy_area(u32 **dst_lines,
  387. unsigned int dst_cols,
  388. unsigned int dst_rows,
  389. u32 **src_lines,
  390. unsigned int src_cols,
  391. unsigned int src_top_row,
  392. unsigned int src_bot_row)
  393. {
  394. unsigned int dst_row = 0;
  395. if (!dst_lines)
  396. return;
  397. while (src_top_row < src_bot_row) {
  398. u32 *src_line = src_lines[src_top_row];
  399. u32 *dst_line = dst_lines[dst_row];
  400. memcpy(dst_line, src_line, src_cols * sizeof(*src_line));
  401. if (dst_cols - src_cols)
  402. memset32(dst_line + src_cols, ' ', dst_cols - src_cols);
  403. src_top_row++;
  404. dst_row++;
  405. }
  406. while (dst_row < dst_rows) {
  407. u32 *dst_line = dst_lines[dst_row];
  408. memset32(dst_line, ' ', dst_cols);
  409. dst_row++;
  410. }
  411. }
  412. /*
  413. * Called from vcs_read() to make sure unicode screen retrieval is possible.
  414. * This will initialize the unicode screen buffer if not already done.
  415. * This returns 0 if OK, or a negative error code otherwise.
  416. * In particular, -ENODATA is returned if the console is not in UTF-8 mode.
  417. */
  418. int vc_uniscr_check(struct vc_data *vc)
  419. {
  420. u32 **uni_lines;
  421. unsigned short *p;
  422. int x, y, mask;
  423. WARN_CONSOLE_UNLOCKED();
  424. if (!vc->vc_utf)
  425. return -ENODATA;
  426. if (vc->vc_uni_lines)
  427. return 0;
  428. uni_lines = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows);
  429. if (!uni_lines)
  430. return -ENOMEM;
  431. /*
  432. * Let's populate it initially with (imperfect) reverse translation.
  433. * This is the next best thing we can do short of having it enabled
  434. * from the start even when no users rely on this functionality. True
  435. * unicode content will be available after a complete screen refresh.
  436. */
  437. p = (unsigned short *)vc->vc_origin;
  438. mask = vc->vc_hi_font_mask | 0xff;
  439. for (y = 0; y < vc->vc_rows; y++) {
  440. u32 *line = uni_lines[y];
  441. for (x = 0; x < vc->vc_cols; x++) {
  442. u16 glyph = scr_readw(p++) & mask;
  443. line[x] = inverse_translate(vc, glyph, true);
  444. }
  445. }
  446. vc->vc_uni_lines = uni_lines;
  447. return 0;
  448. }
  449. /*
  450. * Called from vcs_read() to get the unicode data from the screen.
  451. * This must be preceded by a successful call to vc_uniscr_check() once
  452. * the console lock has been taken.
  453. */
  454. void vc_uniscr_copy_line(const struct vc_data *vc, void *dest, bool viewed,
  455. unsigned int row, unsigned int col, unsigned int nr)
  456. {
  457. u32 **uni_lines = vc->vc_uni_lines;
  458. int offset = row * vc->vc_size_row + col * 2;
  459. unsigned long pos;
  460. if (WARN_ON_ONCE(!uni_lines))
  461. return;
  462. pos = (unsigned long)screenpos(vc, offset, viewed);
  463. if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
  464. /*
  465. * Desired position falls in the main screen buffer.
  466. * However the actual row/col might be different if
  467. * scrollback is active.
  468. */
  469. row = (pos - vc->vc_origin) / vc->vc_size_row;
  470. col = ((pos - vc->vc_origin) % vc->vc_size_row) / 2;
  471. memcpy(dest, &uni_lines[row][col], nr * sizeof(u32));
  472. } else {
  473. /*
  474. * Scrollback is active. For now let's simply backtranslate
  475. * the screen glyphs until the unicode screen buffer does
  476. * synchronize with console display drivers for a scrollback
  477. * buffer of its own.
  478. */
  479. u16 *p = (u16 *)pos;
  480. int mask = vc->vc_hi_font_mask | 0xff;
  481. u32 *uni_buf = dest;
  482. while (nr--) {
  483. u16 glyph = scr_readw(p++) & mask;
  484. *uni_buf++ = inverse_translate(vc, glyph, true);
  485. }
  486. }
  487. }
  488. static void con_scroll(struct vc_data *vc, unsigned int top,
  489. unsigned int bottom, enum con_scroll dir,
  490. unsigned int nr)
  491. {
  492. unsigned int rows = bottom - top;
  493. u16 *clear, *dst, *src;
  494. if (top + nr >= bottom)
  495. nr = rows - 1;
  496. if (bottom > vc->vc_rows || top >= bottom || nr < 1)
  497. return;
  498. vc_uniscr_scroll(vc, top, bottom, dir, nr);
  499. if (con_is_visible(vc) &&
  500. vc->vc_sw->con_scroll(vc, top, bottom, dir, nr))
  501. return;
  502. src = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * top);
  503. dst = (u16 *)(vc->vc_origin + vc->vc_size_row * (top + nr));
  504. if (dir == SM_UP) {
  505. clear = src + (rows - nr) * vc->vc_cols;
  506. swap(src, dst);
  507. }
  508. scr_memmovew(dst, src, (rows - nr) * vc->vc_size_row);
  509. scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr);
  510. }
  511. static void do_update_region(struct vc_data *vc, unsigned long start, int count)
  512. {
  513. unsigned int xx, yy, offset;
  514. u16 *p = (u16 *)start;
  515. offset = (start - vc->vc_origin) / 2;
  516. xx = offset % vc->vc_cols;
  517. yy = offset / vc->vc_cols;
  518. for(;;) {
  519. u16 attrib = scr_readw(p) & 0xff00;
  520. int startx = xx;
  521. u16 *q = p;
  522. while (xx < vc->vc_cols && count) {
  523. if (attrib != (scr_readw(p) & 0xff00)) {
  524. if (p > q)
  525. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  526. startx = xx;
  527. q = p;
  528. attrib = scr_readw(p) & 0xff00;
  529. }
  530. p++;
  531. xx++;
  532. count--;
  533. }
  534. if (p > q)
  535. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  536. if (!count)
  537. break;
  538. xx = 0;
  539. yy++;
  540. }
  541. }
  542. void update_region(struct vc_data *vc, unsigned long start, int count)
  543. {
  544. WARN_CONSOLE_UNLOCKED();
  545. if (con_should_update(vc)) {
  546. hide_cursor(vc);
  547. do_update_region(vc, start, count);
  548. set_cursor(vc);
  549. }
  550. }
  551. EXPORT_SYMBOL(update_region);
  552. /* Structure of attributes is hardware-dependent */
  553. static u8 build_attr(struct vc_data *vc, u8 _color,
  554. enum vc_intensity _intensity, bool _blink, bool _underline,
  555. bool _reverse, bool _italic)
  556. {
  557. if (vc->vc_sw->con_build_attr)
  558. return vc->vc_sw->con_build_attr(vc, _color, _intensity,
  559. _blink, _underline, _reverse, _italic);
  560. /*
  561. * ++roman: I completely changed the attribute format for monochrome
  562. * mode (!can_do_color). The formerly used MDA (monochrome display
  563. * adapter) format didn't allow the combination of certain effects.
  564. * Now the attribute is just a bit vector:
  565. * Bit 0..1: intensity (0..2)
  566. * Bit 2 : underline
  567. * Bit 3 : reverse
  568. * Bit 7 : blink
  569. */
  570. {
  571. u8 a = _color;
  572. if (!vc->vc_can_do_color)
  573. return _intensity |
  574. (_italic << 1) |
  575. (_underline << 2) |
  576. (_reverse << 3) |
  577. (_blink << 7);
  578. if (_italic)
  579. a = (a & 0xF0) | vc->vc_itcolor;
  580. else if (_underline)
  581. a = (a & 0xf0) | vc->vc_ulcolor;
  582. else if (_intensity == VCI_HALF_BRIGHT)
  583. a = (a & 0xf0) | vc->vc_halfcolor;
  584. if (_reverse)
  585. a = (a & 0x88) | (((a >> 4) | (a << 4)) & 0x77);
  586. if (_blink)
  587. a ^= 0x80;
  588. if (_intensity == VCI_BOLD)
  589. a ^= 0x08;
  590. if (vc->vc_hi_font_mask == 0x100)
  591. a <<= 1;
  592. return a;
  593. }
  594. }
  595. static void update_attr(struct vc_data *vc)
  596. {
  597. vc->vc_attr = build_attr(vc, vc->state.color, vc->state.intensity,
  598. vc->state.blink, vc->state.underline,
  599. vc->state.reverse ^ vc->vc_decscnm, vc->state.italic);
  600. vc->vc_video_erase_char = ' ' | (build_attr(vc, vc->state.color,
  601. VCI_NORMAL, vc->state.blink, false,
  602. vc->vc_decscnm, false) << 8);
  603. }
  604. /* Note: inverting the screen twice should revert to the original state */
  605. void invert_screen(struct vc_data *vc, int offset, int count, bool viewed)
  606. {
  607. u16 *p;
  608. WARN_CONSOLE_UNLOCKED();
  609. count /= 2;
  610. p = screenpos(vc, offset, viewed);
  611. if (vc->vc_sw->con_invert_region) {
  612. vc->vc_sw->con_invert_region(vc, p, count);
  613. } else {
  614. u16 *q = p;
  615. int cnt = count;
  616. u16 a;
  617. if (!vc->vc_can_do_color) {
  618. while (cnt--) {
  619. a = scr_readw(q);
  620. a ^= 0x0800;
  621. scr_writew(a, q);
  622. q++;
  623. }
  624. } else if (vc->vc_hi_font_mask == 0x100) {
  625. while (cnt--) {
  626. a = scr_readw(q);
  627. a = (a & 0x11ff) |
  628. ((a & 0xe000) >> 4) |
  629. ((a & 0x0e00) << 4);
  630. scr_writew(a, q);
  631. q++;
  632. }
  633. } else {
  634. while (cnt--) {
  635. a = scr_readw(q);
  636. a = (a & 0x88ff) |
  637. ((a & 0x7000) >> 4) |
  638. ((a & 0x0700) << 4);
  639. scr_writew(a, q);
  640. q++;
  641. }
  642. }
  643. }
  644. if (con_should_update(vc))
  645. do_update_region(vc, (unsigned long) p, count);
  646. notify_update(vc);
  647. }
  648. /* used by selection: complement pointer position */
  649. void complement_pos(struct vc_data *vc, int offset)
  650. {
  651. static int old_offset = -1;
  652. static unsigned short old;
  653. static unsigned short oldx, oldy;
  654. WARN_CONSOLE_UNLOCKED();
  655. if (old_offset != -1 && old_offset >= 0 &&
  656. old_offset < vc->vc_screenbuf_size) {
  657. scr_writew(old, screenpos(vc, old_offset, true));
  658. if (con_should_update(vc))
  659. con_putc(vc, old, oldy, oldx);
  660. notify_update(vc);
  661. }
  662. old_offset = offset;
  663. if (offset != -1 && offset >= 0 &&
  664. offset < vc->vc_screenbuf_size) {
  665. unsigned short new;
  666. u16 *p = screenpos(vc, offset, true);
  667. old = scr_readw(p);
  668. new = old ^ vc->vc_complement_mask;
  669. scr_writew(new, p);
  670. if (con_should_update(vc)) {
  671. oldx = (offset >> 1) % vc->vc_cols;
  672. oldy = (offset >> 1) / vc->vc_cols;
  673. con_putc(vc, new, oldy, oldx);
  674. }
  675. notify_update(vc);
  676. }
  677. }
  678. static void insert_char(struct vc_data *vc, unsigned int nr)
  679. {
  680. unsigned short *p = (unsigned short *) vc->vc_pos;
  681. vc_uniscr_insert(vc, nr);
  682. scr_memmovew(p + nr, p, (vc->vc_cols - vc->state.x - nr) * 2);
  683. scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
  684. vc->vc_need_wrap = 0;
  685. if (con_should_update(vc))
  686. do_update_region(vc, (unsigned long) p,
  687. vc->vc_cols - vc->state.x);
  688. }
  689. static void delete_char(struct vc_data *vc, unsigned int nr)
  690. {
  691. unsigned short *p = (unsigned short *) vc->vc_pos;
  692. vc_uniscr_delete(vc, nr);
  693. scr_memmovew(p, p + nr, (vc->vc_cols - vc->state.x - nr) * 2);
  694. scr_memsetw(p + vc->vc_cols - vc->state.x - nr, vc->vc_video_erase_char,
  695. nr * 2);
  696. vc->vc_need_wrap = 0;
  697. if (con_should_update(vc))
  698. do_update_region(vc, (unsigned long) p,
  699. vc->vc_cols - vc->state.x);
  700. }
  701. static int softcursor_original = -1;
  702. static void add_softcursor(struct vc_data *vc)
  703. {
  704. int i = scr_readw((u16 *) vc->vc_pos);
  705. u32 type = vc->vc_cursor_type;
  706. if (!(type & CUR_SW))
  707. return;
  708. if (softcursor_original != -1)
  709. return;
  710. softcursor_original = i;
  711. i |= CUR_SET(type);
  712. i ^= CUR_CHANGE(type);
  713. if ((type & CUR_ALWAYS_BG) &&
  714. (softcursor_original & CUR_BG) == (i & CUR_BG))
  715. i ^= CUR_BG;
  716. if ((type & CUR_INVERT_FG_BG) && (i & CUR_FG) == ((i & CUR_BG) >> 4))
  717. i ^= CUR_FG;
  718. scr_writew(i, (u16 *)vc->vc_pos);
  719. if (con_should_update(vc))
  720. con_putc(vc, i, vc->state.y, vc->state.x);
  721. }
  722. static void hide_softcursor(struct vc_data *vc)
  723. {
  724. if (softcursor_original != -1) {
  725. scr_writew(softcursor_original, (u16 *)vc->vc_pos);
  726. if (con_should_update(vc))
  727. con_putc(vc, softcursor_original, vc->state.y,
  728. vc->state.x);
  729. softcursor_original = -1;
  730. }
  731. }
  732. static void hide_cursor(struct vc_data *vc)
  733. {
  734. if (vc_is_sel(vc))
  735. clear_selection();
  736. vc->vc_sw->con_cursor(vc, false);
  737. hide_softcursor(vc);
  738. }
  739. static void set_cursor(struct vc_data *vc)
  740. {
  741. if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
  742. return;
  743. if (vc->vc_deccm) {
  744. if (vc_is_sel(vc))
  745. clear_selection();
  746. add_softcursor(vc);
  747. if (CUR_SIZE(vc->vc_cursor_type) != CUR_NONE)
  748. vc->vc_sw->con_cursor(vc, true);
  749. } else
  750. hide_cursor(vc);
  751. }
  752. static void set_origin(struct vc_data *vc)
  753. {
  754. WARN_CONSOLE_UNLOCKED();
  755. if (!con_is_visible(vc) ||
  756. !vc->vc_sw->con_set_origin ||
  757. !vc->vc_sw->con_set_origin(vc))
  758. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  759. vc->vc_visible_origin = vc->vc_origin;
  760. vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
  761. vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->state.y +
  762. 2 * vc->state.x;
  763. }
  764. static void save_screen(struct vc_data *vc)
  765. {
  766. WARN_CONSOLE_UNLOCKED();
  767. if (vc->vc_sw->con_save_screen)
  768. vc->vc_sw->con_save_screen(vc);
  769. }
  770. static void flush_scrollback(struct vc_data *vc)
  771. {
  772. WARN_CONSOLE_UNLOCKED();
  773. set_origin(vc);
  774. if (!con_is_visible(vc))
  775. return;
  776. /*
  777. * The legacy way for flushing the scrollback buffer is to use a side
  778. * effect of the con_switch method. We do it only on the foreground
  779. * console as background consoles have no scrollback buffers in that
  780. * case and we obviously don't want to switch to them.
  781. */
  782. hide_cursor(vc);
  783. vc->vc_sw->con_switch(vc);
  784. set_cursor(vc);
  785. }
  786. /*
  787. * Redrawing of screen
  788. */
  789. void clear_buffer_attributes(struct vc_data *vc)
  790. {
  791. unsigned short *p = (unsigned short *)vc->vc_origin;
  792. int count = vc->vc_screenbuf_size / 2;
  793. int mask = vc->vc_hi_font_mask | 0xff;
  794. for (; count > 0; count--, p++) {
  795. scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
  796. }
  797. }
  798. void redraw_screen(struct vc_data *vc, int is_switch)
  799. {
  800. int redraw = 0;
  801. WARN_CONSOLE_UNLOCKED();
  802. if (!vc) {
  803. /* strange ... */
  804. /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
  805. return;
  806. }
  807. if (is_switch) {
  808. struct vc_data *old_vc = vc_cons[fg_console].d;
  809. if (old_vc == vc)
  810. return;
  811. if (!con_is_visible(vc))
  812. redraw = 1;
  813. *vc->vc_display_fg = vc;
  814. fg_console = vc->vc_num;
  815. hide_cursor(old_vc);
  816. if (!con_is_visible(old_vc)) {
  817. save_screen(old_vc);
  818. set_origin(old_vc);
  819. }
  820. if (tty0dev)
  821. sysfs_notify(&tty0dev->kobj, NULL, "active");
  822. } else {
  823. hide_cursor(vc);
  824. redraw = 1;
  825. }
  826. if (redraw) {
  827. bool update;
  828. int old_was_color = vc->vc_can_do_color;
  829. set_origin(vc);
  830. update = vc->vc_sw->con_switch(vc);
  831. set_palette(vc);
  832. /*
  833. * If console changed from mono<->color, the best we can do
  834. * is to clear the buffer attributes. As it currently stands,
  835. * rebuilding new attributes from the old buffer is not doable
  836. * without overly complex code.
  837. */
  838. if (old_was_color != vc->vc_can_do_color) {
  839. update_attr(vc);
  840. clear_buffer_attributes(vc);
  841. }
  842. if (update && vc->vc_mode != KD_GRAPHICS)
  843. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  844. }
  845. set_cursor(vc);
  846. if (is_switch) {
  847. vt_set_leds_compute_shiftstate();
  848. notify_update(vc);
  849. }
  850. }
  851. EXPORT_SYMBOL(redraw_screen);
  852. /*
  853. * Allocation, freeing and resizing of VTs.
  854. */
  855. int vc_cons_allocated(unsigned int i)
  856. {
  857. return (i < MAX_NR_CONSOLES && vc_cons[i].d);
  858. }
  859. static void visual_init(struct vc_data *vc, int num, bool init)
  860. {
  861. /* ++Geert: vc->vc_sw->con_init determines console size */
  862. if (vc->vc_sw)
  863. module_put(vc->vc_sw->owner);
  864. vc->vc_sw = conswitchp;
  865. if (con_driver_map[num])
  866. vc->vc_sw = con_driver_map[num];
  867. __module_get(vc->vc_sw->owner);
  868. vc->vc_num = num;
  869. vc->vc_display_fg = &master_display_fg;
  870. if (vc->uni_pagedict_loc)
  871. con_free_unimap(vc);
  872. vc->uni_pagedict_loc = &vc->uni_pagedict;
  873. vc->uni_pagedict = NULL;
  874. vc->vc_hi_font_mask = 0;
  875. vc->vc_complement_mask = 0;
  876. vc->vc_can_do_color = 0;
  877. vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
  878. vc->vc_sw->con_init(vc, init);
  879. if (!vc->vc_complement_mask)
  880. vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
  881. vc->vc_s_complement_mask = vc->vc_complement_mask;
  882. vc->vc_size_row = vc->vc_cols << 1;
  883. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  884. }
  885. static void visual_deinit(struct vc_data *vc)
  886. {
  887. vc->vc_sw->con_deinit(vc);
  888. module_put(vc->vc_sw->owner);
  889. }
  890. static void vc_port_destruct(struct tty_port *port)
  891. {
  892. struct vc_data *vc = container_of(port, struct vc_data, port);
  893. kfree(vc);
  894. }
  895. static const struct tty_port_operations vc_port_ops = {
  896. .destruct = vc_port_destruct,
  897. };
  898. /*
  899. * Change # of rows and columns (0 means unchanged/the size of fg_console)
  900. * [this is to be used together with some user program
  901. * like resize that changes the hardware videomode]
  902. */
  903. #define VC_MAXCOL (32767)
  904. #define VC_MAXROW (32767)
  905. int vc_allocate(unsigned int currcons) /* return 0 on success */
  906. {
  907. struct vt_notifier_param param;
  908. struct vc_data *vc;
  909. int err;
  910. WARN_CONSOLE_UNLOCKED();
  911. if (currcons >= MAX_NR_CONSOLES)
  912. return -ENXIO;
  913. if (vc_cons[currcons].d)
  914. return 0;
  915. /* due to the granularity of kmalloc, we waste some memory here */
  916. /* the alloc is done in two steps, to optimize the common situation
  917. of a 25x80 console (structsize=216, screenbuf_size=4000) */
  918. /* although the numbers above are not valid since long ago, the
  919. point is still up-to-date and the comment still has its value
  920. even if only as a historical artifact. --mj, July 1998 */
  921. param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
  922. if (!vc)
  923. return -ENOMEM;
  924. vc_cons[currcons].d = vc;
  925. tty_port_init(&vc->port);
  926. vc->port.ops = &vc_port_ops;
  927. INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
  928. visual_init(vc, currcons, true);
  929. if (!*vc->uni_pagedict_loc)
  930. con_set_default_unimap(vc);
  931. err = -EINVAL;
  932. if (vc->vc_cols > VC_MAXCOL || vc->vc_rows > VC_MAXROW ||
  933. vc->vc_screenbuf_size > KMALLOC_MAX_SIZE || !vc->vc_screenbuf_size)
  934. goto err_free;
  935. err = -ENOMEM;
  936. vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
  937. if (!vc->vc_screenbuf)
  938. goto err_free;
  939. /* If no drivers have overridden us and the user didn't pass a
  940. boot option, default to displaying the cursor */
  941. if (global_cursor_default == -1)
  942. global_cursor_default = 1;
  943. vc_init(vc, 1);
  944. vcs_make_sysfs(currcons);
  945. atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
  946. return 0;
  947. err_free:
  948. visual_deinit(vc);
  949. kfree(vc);
  950. vc_cons[currcons].d = NULL;
  951. return err;
  952. }
  953. static inline int resize_screen(struct vc_data *vc, int width, int height,
  954. bool from_user)
  955. {
  956. /* Resizes the resolution of the display adapater */
  957. int err = 0;
  958. if (vc->vc_sw->con_resize)
  959. err = vc->vc_sw->con_resize(vc, width, height, from_user);
  960. return err;
  961. }
  962. /**
  963. * vc_do_resize - resizing method for the tty
  964. * @tty: tty being resized
  965. * @vc: virtual console private data
  966. * @cols: columns
  967. * @lines: lines
  968. * @from_user: invoked by a user?
  969. *
  970. * Resize a virtual console, clipping according to the actual constraints. If
  971. * the caller passes a tty structure then update the termios winsize
  972. * information and perform any necessary signal handling.
  973. *
  974. * Locking: Caller must hold the console semaphore. Takes the termios rwsem and
  975. * ctrl.lock of the tty IFF a tty is passed.
  976. */
  977. static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
  978. unsigned int cols, unsigned int lines, bool from_user)
  979. {
  980. unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
  981. unsigned long end;
  982. unsigned int old_rows, old_row_size, first_copied_row;
  983. unsigned int new_cols, new_rows, new_row_size, new_screen_size;
  984. unsigned short *oldscreen, *newscreen;
  985. u32 **new_uniscr = NULL;
  986. WARN_CONSOLE_UNLOCKED();
  987. if (cols > VC_MAXCOL || lines > VC_MAXROW)
  988. return -EINVAL;
  989. new_cols = (cols ? cols : vc->vc_cols);
  990. new_rows = (lines ? lines : vc->vc_rows);
  991. new_row_size = new_cols << 1;
  992. new_screen_size = new_row_size * new_rows;
  993. if (new_cols == vc->vc_cols && new_rows == vc->vc_rows) {
  994. /*
  995. * This function is being called here to cover the case
  996. * where the userspace calls the FBIOPUT_VSCREENINFO twice,
  997. * passing the same fb_var_screeninfo containing the fields
  998. * yres/xres equal to a number non-multiple of vc_font.height
  999. * and yres_virtual/xres_virtual equal to number lesser than the
  1000. * vc_font.height and yres/xres.
  1001. * In the second call, the struct fb_var_screeninfo isn't
  1002. * being modified by the underlying driver because of the
  1003. * if above, and this causes the fbcon_display->vrows to become
  1004. * negative and it eventually leads to out-of-bound
  1005. * access by the imageblit function.
  1006. * To give the correct values to the struct and to not have
  1007. * to deal with possible errors from the code below, we call
  1008. * the resize_screen here as well.
  1009. */
  1010. return resize_screen(vc, new_cols, new_rows, from_user);
  1011. }
  1012. if (new_screen_size > KMALLOC_MAX_SIZE || !new_screen_size)
  1013. return -EINVAL;
  1014. newscreen = kzalloc(new_screen_size, GFP_USER);
  1015. if (!newscreen)
  1016. return -ENOMEM;
  1017. if (vc->vc_uni_lines) {
  1018. new_uniscr = vc_uniscr_alloc(new_cols, new_rows);
  1019. if (!new_uniscr) {
  1020. kfree(newscreen);
  1021. return -ENOMEM;
  1022. }
  1023. }
  1024. if (vc_is_sel(vc))
  1025. clear_selection();
  1026. old_rows = vc->vc_rows;
  1027. old_row_size = vc->vc_size_row;
  1028. err = resize_screen(vc, new_cols, new_rows, from_user);
  1029. if (err) {
  1030. kfree(newscreen);
  1031. vc_uniscr_free(new_uniscr);
  1032. return err;
  1033. }
  1034. vc->vc_rows = new_rows;
  1035. vc->vc_cols = new_cols;
  1036. vc->vc_size_row = new_row_size;
  1037. vc->vc_screenbuf_size = new_screen_size;
  1038. rlth = min(old_row_size, new_row_size);
  1039. rrem = new_row_size - rlth;
  1040. old_origin = vc->vc_origin;
  1041. new_origin = (long) newscreen;
  1042. new_scr_end = new_origin + new_screen_size;
  1043. if (vc->state.y > new_rows) {
  1044. if (old_rows - vc->state.y < new_rows) {
  1045. /*
  1046. * Cursor near the bottom, copy contents from the
  1047. * bottom of buffer
  1048. */
  1049. first_copied_row = (old_rows - new_rows);
  1050. } else {
  1051. /*
  1052. * Cursor is in no man's land, copy 1/2 screenful
  1053. * from the top and bottom of cursor position
  1054. */
  1055. first_copied_row = (vc->state.y - new_rows/2);
  1056. }
  1057. old_origin += first_copied_row * old_row_size;
  1058. } else
  1059. first_copied_row = 0;
  1060. end = old_origin + old_row_size * min(old_rows, new_rows);
  1061. vc_uniscr_copy_area(new_uniscr, new_cols, new_rows,
  1062. vc->vc_uni_lines, rlth/2, first_copied_row,
  1063. min(old_rows, new_rows));
  1064. vc_uniscr_set(vc, new_uniscr);
  1065. update_attr(vc);
  1066. while (old_origin < end) {
  1067. scr_memcpyw((unsigned short *) new_origin,
  1068. (unsigned short *) old_origin, rlth);
  1069. if (rrem)
  1070. scr_memsetw((void *)(new_origin + rlth),
  1071. vc->vc_video_erase_char, rrem);
  1072. old_origin += old_row_size;
  1073. new_origin += new_row_size;
  1074. }
  1075. if (new_scr_end > new_origin)
  1076. scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
  1077. new_scr_end - new_origin);
  1078. oldscreen = vc->vc_screenbuf;
  1079. vc->vc_screenbuf = newscreen;
  1080. vc->vc_screenbuf_size = new_screen_size;
  1081. set_origin(vc);
  1082. kfree(oldscreen);
  1083. /* do part of a reset_terminal() */
  1084. vc->vc_top = 0;
  1085. vc->vc_bottom = vc->vc_rows;
  1086. gotoxy(vc, vc->state.x, vc->state.y);
  1087. save_cur(vc);
  1088. if (tty) {
  1089. /* Rewrite the requested winsize data with the actual
  1090. resulting sizes */
  1091. struct winsize ws;
  1092. memset(&ws, 0, sizeof(ws));
  1093. ws.ws_row = vc->vc_rows;
  1094. ws.ws_col = vc->vc_cols;
  1095. ws.ws_ypixel = vc->vc_scan_lines;
  1096. tty_do_resize(tty, &ws);
  1097. }
  1098. if (con_is_visible(vc))
  1099. update_screen(vc);
  1100. vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
  1101. notify_update(vc);
  1102. return err;
  1103. }
  1104. /**
  1105. * __vc_resize - resize a VT
  1106. * @vc: virtual console
  1107. * @cols: columns
  1108. * @rows: rows
  1109. * @from_user: invoked by a user?
  1110. *
  1111. * Resize a virtual console as seen from the console end of things. We use the
  1112. * common vc_do_resize() method to update the structures.
  1113. *
  1114. * Locking: The caller must hold the console sem to protect console internals
  1115. * and @vc->port.tty.
  1116. */
  1117. int __vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows,
  1118. bool from_user)
  1119. {
  1120. return vc_do_resize(vc->port.tty, vc, cols, rows, from_user);
  1121. }
  1122. EXPORT_SYMBOL(__vc_resize);
  1123. /**
  1124. * vt_resize - resize a VT
  1125. * @tty: tty to resize
  1126. * @ws: winsize attributes
  1127. *
  1128. * Resize a virtual terminal. This is called by the tty layer as we register
  1129. * our own handler for resizing. The mutual helper does all the actual work.
  1130. *
  1131. * Locking: Takes the console sem and the called methods then take the tty
  1132. * termios_rwsem and the tty ctrl.lock in that order.
  1133. */
  1134. static int vt_resize(struct tty_struct *tty, struct winsize *ws)
  1135. {
  1136. struct vc_data *vc = tty->driver_data;
  1137. int ret;
  1138. console_lock();
  1139. ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row, false);
  1140. console_unlock();
  1141. return ret;
  1142. }
  1143. struct vc_data *vc_deallocate(unsigned int currcons)
  1144. {
  1145. struct vc_data *vc = NULL;
  1146. WARN_CONSOLE_UNLOCKED();
  1147. if (vc_cons_allocated(currcons)) {
  1148. struct vt_notifier_param param;
  1149. param.vc = vc = vc_cons[currcons].d;
  1150. atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
  1151. vcs_remove_sysfs(currcons);
  1152. visual_deinit(vc);
  1153. con_free_unimap(vc);
  1154. put_pid(vc->vt_pid);
  1155. vc_uniscr_set(vc, NULL);
  1156. kfree(vc->vc_screenbuf);
  1157. vc_cons[currcons].d = NULL;
  1158. }
  1159. return vc;
  1160. }
  1161. /*
  1162. * VT102 emulator
  1163. */
  1164. enum { EPecma = 0, EPdec, EPeq, EPgt, EPlt};
  1165. #define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
  1166. #define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
  1167. #define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
  1168. #define decarm VC_REPEAT
  1169. #define decckm VC_CKMODE
  1170. #define kbdapplic VC_APPLIC
  1171. #define lnm VC_CRLF
  1172. const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
  1173. 8,12,10,14, 9,13,11,15 };
  1174. EXPORT_SYMBOL(color_table);
  1175. /* the default colour table, for VGA+ colour systems */
  1176. unsigned char default_red[] = {
  1177. 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
  1178. 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
  1179. };
  1180. module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
  1181. EXPORT_SYMBOL(default_red);
  1182. unsigned char default_grn[] = {
  1183. 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
  1184. 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
  1185. };
  1186. module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
  1187. EXPORT_SYMBOL(default_grn);
  1188. unsigned char default_blu[] = {
  1189. 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
  1190. 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
  1191. };
  1192. module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
  1193. EXPORT_SYMBOL(default_blu);
  1194. /*
  1195. * gotoxy() must verify all boundaries, because the arguments
  1196. * might also be negative. If the given position is out of
  1197. * bounds, the cursor is placed at the nearest margin.
  1198. */
  1199. static void gotoxy(struct vc_data *vc, int new_x, int new_y)
  1200. {
  1201. int min_y, max_y;
  1202. if (new_x < 0)
  1203. vc->state.x = 0;
  1204. else {
  1205. if (new_x >= vc->vc_cols)
  1206. vc->state.x = vc->vc_cols - 1;
  1207. else
  1208. vc->state.x = new_x;
  1209. }
  1210. if (vc->vc_decom) {
  1211. min_y = vc->vc_top;
  1212. max_y = vc->vc_bottom;
  1213. } else {
  1214. min_y = 0;
  1215. max_y = vc->vc_rows;
  1216. }
  1217. if (new_y < min_y)
  1218. vc->state.y = min_y;
  1219. else if (new_y >= max_y)
  1220. vc->state.y = max_y - 1;
  1221. else
  1222. vc->state.y = new_y;
  1223. vc->vc_pos = vc->vc_origin + vc->state.y * vc->vc_size_row +
  1224. (vc->state.x << 1);
  1225. vc->vc_need_wrap = 0;
  1226. }
  1227. /* for absolute user moves, when decom is set */
  1228. static void gotoxay(struct vc_data *vc, int new_x, int new_y)
  1229. {
  1230. gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
  1231. }
  1232. void scrollback(struct vc_data *vc)
  1233. {
  1234. scrolldelta(-(vc->vc_rows / 2));
  1235. }
  1236. void scrollfront(struct vc_data *vc, int lines)
  1237. {
  1238. if (!lines)
  1239. lines = vc->vc_rows / 2;
  1240. scrolldelta(lines);
  1241. }
  1242. static void lf(struct vc_data *vc)
  1243. {
  1244. /* don't scroll if above bottom of scrolling region, or
  1245. * if below scrolling region
  1246. */
  1247. if (vc->state.y + 1 == vc->vc_bottom)
  1248. con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1);
  1249. else if (vc->state.y < vc->vc_rows - 1) {
  1250. vc->state.y++;
  1251. vc->vc_pos += vc->vc_size_row;
  1252. }
  1253. vc->vc_need_wrap = 0;
  1254. notify_write(vc, '\n');
  1255. }
  1256. static void ri(struct vc_data *vc)
  1257. {
  1258. /* don't scroll if below top of scrolling region, or
  1259. * if above scrolling region
  1260. */
  1261. if (vc->state.y == vc->vc_top)
  1262. con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1);
  1263. else if (vc->state.y > 0) {
  1264. vc->state.y--;
  1265. vc->vc_pos -= vc->vc_size_row;
  1266. }
  1267. vc->vc_need_wrap = 0;
  1268. }
  1269. static inline void cr(struct vc_data *vc)
  1270. {
  1271. vc->vc_pos -= vc->state.x << 1;
  1272. vc->vc_need_wrap = vc->state.x = 0;
  1273. notify_write(vc, '\r');
  1274. }
  1275. static inline void bs(struct vc_data *vc)
  1276. {
  1277. if (vc->state.x) {
  1278. vc->vc_pos -= 2;
  1279. vc->state.x--;
  1280. vc->vc_need_wrap = 0;
  1281. notify_write(vc, '\b');
  1282. }
  1283. }
  1284. static inline void del(struct vc_data *vc)
  1285. {
  1286. /* ignored */
  1287. }
  1288. enum CSI_J {
  1289. CSI_J_CURSOR_TO_END = 0,
  1290. CSI_J_START_TO_CURSOR = 1,
  1291. CSI_J_VISIBLE = 2,
  1292. CSI_J_FULL = 3,
  1293. };
  1294. static void csi_J(struct vc_data *vc, enum CSI_J vpar)
  1295. {
  1296. unsigned short *start;
  1297. unsigned int count;
  1298. switch (vpar) {
  1299. case CSI_J_CURSOR_TO_END:
  1300. vc_uniscr_clear_line(vc, vc->state.x,
  1301. vc->vc_cols - vc->state.x);
  1302. vc_uniscr_clear_lines(vc, vc->state.y + 1,
  1303. vc->vc_rows - vc->state.y - 1);
  1304. count = (vc->vc_scr_end - vc->vc_pos) >> 1;
  1305. start = (unsigned short *)vc->vc_pos;
  1306. break;
  1307. case CSI_J_START_TO_CURSOR:
  1308. vc_uniscr_clear_line(vc, 0, vc->state.x + 1);
  1309. vc_uniscr_clear_lines(vc, 0, vc->state.y);
  1310. count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
  1311. start = (unsigned short *)vc->vc_origin;
  1312. break;
  1313. case CSI_J_FULL:
  1314. flush_scrollback(vc);
  1315. fallthrough;
  1316. case CSI_J_VISIBLE:
  1317. vc_uniscr_clear_lines(vc, 0, vc->vc_rows);
  1318. count = vc->vc_cols * vc->vc_rows;
  1319. start = (unsigned short *)vc->vc_origin;
  1320. break;
  1321. default:
  1322. return;
  1323. }
  1324. scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
  1325. if (con_should_update(vc))
  1326. do_update_region(vc, (unsigned long) start, count);
  1327. vc->vc_need_wrap = 0;
  1328. }
  1329. enum {
  1330. CSI_K_CURSOR_TO_LINEEND = 0,
  1331. CSI_K_LINESTART_TO_CURSOR = 1,
  1332. CSI_K_LINE = 2,
  1333. };
  1334. static void csi_K(struct vc_data *vc)
  1335. {
  1336. unsigned int count;
  1337. unsigned short *start = (unsigned short *)vc->vc_pos;
  1338. int offset;
  1339. switch (vc->vc_par[0]) {
  1340. case CSI_K_CURSOR_TO_LINEEND:
  1341. offset = 0;
  1342. count = vc->vc_cols - vc->state.x;
  1343. break;
  1344. case CSI_K_LINESTART_TO_CURSOR:
  1345. offset = -vc->state.x;
  1346. count = vc->state.x + 1;
  1347. break;
  1348. case CSI_K_LINE:
  1349. offset = -vc->state.x;
  1350. count = vc->vc_cols;
  1351. break;
  1352. default:
  1353. return;
  1354. }
  1355. vc_uniscr_clear_line(vc, vc->state.x + offset, count);
  1356. scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count);
  1357. vc->vc_need_wrap = 0;
  1358. if (con_should_update(vc))
  1359. do_update_region(vc, (unsigned long)(start + offset), count);
  1360. }
  1361. /* erase the following count positions */
  1362. static void csi_X(struct vc_data *vc)
  1363. { /* not vt100? */
  1364. unsigned int count = clamp(vc->vc_par[0], 1, vc->vc_cols - vc->state.x);
  1365. vc_uniscr_clear_line(vc, vc->state.x, count);
  1366. scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
  1367. if (con_should_update(vc))
  1368. vc->vc_sw->con_clear(vc, vc->state.y, vc->state.x, count);
  1369. vc->vc_need_wrap = 0;
  1370. }
  1371. static void default_attr(struct vc_data *vc)
  1372. {
  1373. vc->state.intensity = VCI_NORMAL;
  1374. vc->state.italic = false;
  1375. vc->state.underline = false;
  1376. vc->state.reverse = false;
  1377. vc->state.blink = false;
  1378. vc->state.color = vc->vc_def_color;
  1379. }
  1380. struct rgb { u8 r; u8 g; u8 b; };
  1381. static void rgb_from_256(unsigned int i, struct rgb *c)
  1382. {
  1383. if (i < 8) { /* Standard colours. */
  1384. c->r = i&1 ? 0xaa : 0x00;
  1385. c->g = i&2 ? 0xaa : 0x00;
  1386. c->b = i&4 ? 0xaa : 0x00;
  1387. } else if (i < 16) {
  1388. c->r = i&1 ? 0xff : 0x55;
  1389. c->g = i&2 ? 0xff : 0x55;
  1390. c->b = i&4 ? 0xff : 0x55;
  1391. } else if (i < 232) { /* 6x6x6 colour cube. */
  1392. i -= 16;
  1393. c->b = i % 6 * 255 / 6;
  1394. i /= 6;
  1395. c->g = i % 6 * 255 / 6;
  1396. i /= 6;
  1397. c->r = i * 255 / 6;
  1398. } else /* Grayscale ramp. */
  1399. c->r = c->g = c->b = i * 10 - 2312;
  1400. }
  1401. static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
  1402. {
  1403. u8 hue = 0, max = max3(c->r, c->g, c->b);
  1404. if (c->r > max / 2)
  1405. hue |= 4;
  1406. if (c->g > max / 2)
  1407. hue |= 2;
  1408. if (c->b > max / 2)
  1409. hue |= 1;
  1410. if (hue == 7 && max <= 0x55) {
  1411. hue = 0;
  1412. vc->state.intensity = VCI_BOLD;
  1413. } else if (max > 0xaa)
  1414. vc->state.intensity = VCI_BOLD;
  1415. else
  1416. vc->state.intensity = VCI_NORMAL;
  1417. vc->state.color = (vc->state.color & 0xf0) | hue;
  1418. }
  1419. static void rgb_background(struct vc_data *vc, const struct rgb *c)
  1420. {
  1421. /* For backgrounds, err on the dark side. */
  1422. vc->state.color = (vc->state.color & 0x0f)
  1423. | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
  1424. }
  1425. /*
  1426. * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
  1427. * and thus need to be detected and ignored by hand. That standard also
  1428. * wants : rather than ; as separators but sequences containing : are currently
  1429. * completely ignored by the parser.
  1430. *
  1431. * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
  1432. * supporting them.
  1433. */
  1434. static int vc_t416_color(struct vc_data *vc, int i,
  1435. void(*set_color)(struct vc_data *vc, const struct rgb *c))
  1436. {
  1437. struct rgb c;
  1438. i++;
  1439. if (i > vc->vc_npar)
  1440. return i;
  1441. if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
  1442. /* 256 colours */
  1443. i++;
  1444. rgb_from_256(vc->vc_par[i], &c);
  1445. } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
  1446. /* 24 bit */
  1447. c.r = vc->vc_par[i + 1];
  1448. c.g = vc->vc_par[i + 2];
  1449. c.b = vc->vc_par[i + 3];
  1450. i += 3;
  1451. } else
  1452. return i;
  1453. set_color(vc, &c);
  1454. return i;
  1455. }
  1456. enum {
  1457. CSI_m_DEFAULT = 0,
  1458. CSI_m_BOLD = 1,
  1459. CSI_m_HALF_BRIGHT = 2,
  1460. CSI_m_ITALIC = 3,
  1461. CSI_m_UNDERLINE = 4,
  1462. CSI_m_BLINK = 5,
  1463. CSI_m_REVERSE = 7,
  1464. CSI_m_PRI_FONT = 10,
  1465. CSI_m_ALT_FONT1 = 11,
  1466. CSI_m_ALT_FONT2 = 12,
  1467. CSI_m_DOUBLE_UNDERLINE = 21,
  1468. CSI_m_NORMAL_INTENSITY = 22,
  1469. CSI_m_NO_ITALIC = 23,
  1470. CSI_m_NO_UNDERLINE = 24,
  1471. CSI_m_NO_BLINK = 25,
  1472. CSI_m_NO_REVERSE = 27,
  1473. CSI_m_FG_COLOR_BEG = 30,
  1474. CSI_m_FG_COLOR_END = 37,
  1475. CSI_m_FG_COLOR = 38,
  1476. CSI_m_DEFAULT_FG_COLOR = 39,
  1477. CSI_m_BG_COLOR_BEG = 40,
  1478. CSI_m_BG_COLOR_END = 47,
  1479. CSI_m_BG_COLOR = 48,
  1480. CSI_m_DEFAULT_BG_COLOR = 49,
  1481. CSI_m_BRIGHT_FG_COLOR_BEG = 90,
  1482. CSI_m_BRIGHT_FG_COLOR_END = 97,
  1483. CSI_m_BRIGHT_FG_COLOR_OFF = CSI_m_BRIGHT_FG_COLOR_BEG - CSI_m_FG_COLOR_BEG,
  1484. CSI_m_BRIGHT_BG_COLOR_BEG = 100,
  1485. CSI_m_BRIGHT_BG_COLOR_END = 107,
  1486. CSI_m_BRIGHT_BG_COLOR_OFF = CSI_m_BRIGHT_BG_COLOR_BEG - CSI_m_BG_COLOR_BEG,
  1487. };
  1488. /* console_lock is held */
  1489. static void csi_m(struct vc_data *vc)
  1490. {
  1491. int i;
  1492. for (i = 0; i <= vc->vc_npar; i++)
  1493. switch (vc->vc_par[i]) {
  1494. case CSI_m_DEFAULT: /* all attributes off */
  1495. default_attr(vc);
  1496. break;
  1497. case CSI_m_BOLD:
  1498. vc->state.intensity = VCI_BOLD;
  1499. break;
  1500. case CSI_m_HALF_BRIGHT:
  1501. vc->state.intensity = VCI_HALF_BRIGHT;
  1502. break;
  1503. case CSI_m_ITALIC:
  1504. vc->state.italic = true;
  1505. break;
  1506. case CSI_m_DOUBLE_UNDERLINE:
  1507. /*
  1508. * No console drivers support double underline, so
  1509. * convert it to a single underline.
  1510. */
  1511. case CSI_m_UNDERLINE:
  1512. vc->state.underline = true;
  1513. break;
  1514. case CSI_m_BLINK:
  1515. vc->state.blink = true;
  1516. break;
  1517. case CSI_m_REVERSE:
  1518. vc->state.reverse = true;
  1519. break;
  1520. case CSI_m_PRI_FONT: /* ANSI X3.64-1979 (SCO-ish?)
  1521. * Select primary font, don't display control chars if
  1522. * defined, don't set bit 8 on output.
  1523. */
  1524. vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], vc);
  1525. vc->vc_disp_ctrl = 0;
  1526. vc->vc_toggle_meta = 0;
  1527. break;
  1528. case CSI_m_ALT_FONT1: /* ANSI X3.64-1979 (SCO-ish?)
  1529. * Select first alternate font, lets chars < 32 be
  1530. * displayed as ROM chars.
  1531. */
  1532. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1533. vc->vc_disp_ctrl = 1;
  1534. vc->vc_toggle_meta = 0;
  1535. break;
  1536. case CSI_m_ALT_FONT2: /* ANSI X3.64-1979 (SCO-ish?)
  1537. * Select second alternate font, toggle high bit
  1538. * before displaying as ROM char.
  1539. */
  1540. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1541. vc->vc_disp_ctrl = 1;
  1542. vc->vc_toggle_meta = 1;
  1543. break;
  1544. case CSI_m_NORMAL_INTENSITY:
  1545. vc->state.intensity = VCI_NORMAL;
  1546. break;
  1547. case CSI_m_NO_ITALIC:
  1548. vc->state.italic = false;
  1549. break;
  1550. case CSI_m_NO_UNDERLINE:
  1551. vc->state.underline = false;
  1552. break;
  1553. case CSI_m_NO_BLINK:
  1554. vc->state.blink = false;
  1555. break;
  1556. case CSI_m_NO_REVERSE:
  1557. vc->state.reverse = false;
  1558. break;
  1559. case CSI_m_FG_COLOR:
  1560. i = vc_t416_color(vc, i, rgb_foreground);
  1561. break;
  1562. case CSI_m_BG_COLOR:
  1563. i = vc_t416_color(vc, i, rgb_background);
  1564. break;
  1565. case CSI_m_DEFAULT_FG_COLOR:
  1566. vc->state.color = (vc->vc_def_color & 0x0f) |
  1567. (vc->state.color & 0xf0);
  1568. break;
  1569. case CSI_m_DEFAULT_BG_COLOR:
  1570. vc->state.color = (vc->vc_def_color & 0xf0) |
  1571. (vc->state.color & 0x0f);
  1572. break;
  1573. case CSI_m_BRIGHT_FG_COLOR_BEG ... CSI_m_BRIGHT_FG_COLOR_END:
  1574. vc->state.intensity = VCI_BOLD;
  1575. vc->vc_par[i] -= CSI_m_BRIGHT_FG_COLOR_OFF;
  1576. fallthrough;
  1577. case CSI_m_FG_COLOR_BEG ... CSI_m_FG_COLOR_END:
  1578. vc->vc_par[i] -= CSI_m_FG_COLOR_BEG;
  1579. vc->state.color = color_table[vc->vc_par[i]] |
  1580. (vc->state.color & 0xf0);
  1581. break;
  1582. case CSI_m_BRIGHT_BG_COLOR_BEG ... CSI_m_BRIGHT_BG_COLOR_END:
  1583. vc->vc_par[i] -= CSI_m_BRIGHT_BG_COLOR_OFF;
  1584. fallthrough;
  1585. case CSI_m_BG_COLOR_BEG ... CSI_m_BG_COLOR_END:
  1586. vc->vc_par[i] -= CSI_m_BG_COLOR_BEG;
  1587. vc->state.color = (color_table[vc->vc_par[i]] << 4) |
  1588. (vc->state.color & 0x0f);
  1589. break;
  1590. }
  1591. update_attr(vc);
  1592. }
  1593. static void respond_string(const char *p, size_t len, struct tty_port *port)
  1594. {
  1595. tty_insert_flip_string(port, p, len);
  1596. tty_flip_buffer_push(port);
  1597. }
  1598. static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
  1599. {
  1600. char buf[40];
  1601. int len;
  1602. len = sprintf(buf, "\033[%d;%dR", vc->state.y +
  1603. (vc->vc_decom ? vc->vc_top + 1 : 1),
  1604. vc->state.x + 1);
  1605. respond_string(buf, len, tty->port);
  1606. }
  1607. static inline void status_report(struct tty_struct *tty)
  1608. {
  1609. static const char teminal_ok[] = "\033[0n";
  1610. respond_string(teminal_ok, strlen(teminal_ok), tty->port);
  1611. }
  1612. static inline void respond_ID(struct tty_struct *tty)
  1613. {
  1614. /* terminal answer to an ESC-Z or csi0c query. */
  1615. static const char vt102_id[] = "\033[?6c";
  1616. respond_string(vt102_id, strlen(vt102_id), tty->port);
  1617. }
  1618. void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
  1619. {
  1620. char buf[8];
  1621. int len;
  1622. len = sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt),
  1623. (char)('!' + mrx), (char)('!' + mry));
  1624. respond_string(buf, len, tty->port);
  1625. }
  1626. /* invoked via ioctl(TIOCLINUX) and through set_selection_user */
  1627. int mouse_reporting(void)
  1628. {
  1629. return vc_cons[fg_console].d->vc_report_mouse;
  1630. }
  1631. enum {
  1632. CSI_DEC_hl_CURSOR_KEYS = 1, /* CKM: cursor keys send ^[Ox/^[[x */
  1633. CSI_DEC_hl_132_COLUMNS = 3, /* COLM: 80/132 mode switch */
  1634. CSI_DEC_hl_REVERSE_VIDEO = 5, /* SCNM */
  1635. CSI_DEC_hl_ORIGIN_MODE = 6, /* OM: origin relative/absolute */
  1636. CSI_DEC_hl_AUTOWRAP = 7, /* AWM */
  1637. CSI_DEC_hl_AUTOREPEAT = 8, /* ARM */
  1638. CSI_DEC_hl_MOUSE_X10 = 9,
  1639. CSI_DEC_hl_SHOW_CURSOR = 25, /* TCEM */
  1640. CSI_DEC_hl_MOUSE_VT200 = 1000,
  1641. };
  1642. /* console_lock is held */
  1643. static void csi_DEC_hl(struct vc_data *vc, bool on_off)
  1644. {
  1645. unsigned int i;
  1646. for (i = 0; i <= vc->vc_npar; i++)
  1647. switch (vc->vc_par[i]) {
  1648. case CSI_DEC_hl_CURSOR_KEYS:
  1649. if (on_off)
  1650. set_kbd(vc, decckm);
  1651. else
  1652. clr_kbd(vc, decckm);
  1653. break;
  1654. case CSI_DEC_hl_132_COLUMNS: /* unimplemented */
  1655. #if 0
  1656. vc_resize(deccolm ? 132 : 80, vc->vc_rows);
  1657. /* this alone does not suffice; some user mode
  1658. utility has to change the hardware regs */
  1659. #endif
  1660. break;
  1661. case CSI_DEC_hl_REVERSE_VIDEO:
  1662. if (vc->vc_decscnm != on_off) {
  1663. vc->vc_decscnm = on_off;
  1664. invert_screen(vc, 0, vc->vc_screenbuf_size,
  1665. false);
  1666. update_attr(vc);
  1667. }
  1668. break;
  1669. case CSI_DEC_hl_ORIGIN_MODE:
  1670. vc->vc_decom = on_off;
  1671. gotoxay(vc, 0, 0);
  1672. break;
  1673. case CSI_DEC_hl_AUTOWRAP:
  1674. vc->vc_decawm = on_off;
  1675. break;
  1676. case CSI_DEC_hl_AUTOREPEAT:
  1677. if (on_off)
  1678. set_kbd(vc, decarm);
  1679. else
  1680. clr_kbd(vc, decarm);
  1681. break;
  1682. case CSI_DEC_hl_MOUSE_X10:
  1683. vc->vc_report_mouse = on_off ? 1 : 0;
  1684. break;
  1685. case CSI_DEC_hl_SHOW_CURSOR:
  1686. vc->vc_deccm = on_off;
  1687. break;
  1688. case CSI_DEC_hl_MOUSE_VT200:
  1689. vc->vc_report_mouse = on_off ? 2 : 0;
  1690. break;
  1691. }
  1692. }
  1693. enum {
  1694. CSI_hl_DISPLAY_CTRL = 3, /* handle ansi control chars */
  1695. CSI_hl_INSERT = 4, /* IRM: insert/replace */
  1696. CSI_hl_AUTO_NL = 20, /* LNM: Enter == CrLf/Lf */
  1697. };
  1698. /* console_lock is held */
  1699. static void csi_hl(struct vc_data *vc, bool on_off)
  1700. {
  1701. unsigned int i;
  1702. for (i = 0; i <= vc->vc_npar; i++)
  1703. switch (vc->vc_par[i]) { /* ANSI modes set/reset */
  1704. case CSI_hl_DISPLAY_CTRL:
  1705. vc->vc_disp_ctrl = on_off;
  1706. break;
  1707. case CSI_hl_INSERT:
  1708. vc->vc_decim = on_off;
  1709. break;
  1710. case CSI_hl_AUTO_NL:
  1711. if (on_off)
  1712. set_kbd(vc, lnm);
  1713. else
  1714. clr_kbd(vc, lnm);
  1715. break;
  1716. }
  1717. }
  1718. enum CSI_right_square_bracket {
  1719. CSI_RSB_COLOR_FOR_UNDERLINE = 1,
  1720. CSI_RSB_COLOR_FOR_HALF_BRIGHT = 2,
  1721. CSI_RSB_MAKE_CUR_COLOR_DEFAULT = 8,
  1722. CSI_RSB_BLANKING_INTERVAL = 9,
  1723. CSI_RSB_BELL_FREQUENCY = 10,
  1724. CSI_RSB_BELL_DURATION = 11,
  1725. CSI_RSB_BRING_CONSOLE_TO_FRONT = 12,
  1726. CSI_RSB_UNBLANK = 13,
  1727. CSI_RSB_VESA_OFF_INTERVAL = 14,
  1728. CSI_RSB_BRING_PREV_CONSOLE_TO_FRONT = 15,
  1729. CSI_RSB_CURSOR_BLINK_INTERVAL = 16,
  1730. };
  1731. /*
  1732. * csi_RSB - csi+] (Right Square Bracket) handler
  1733. *
  1734. * These are linux console private sequences.
  1735. *
  1736. * console_lock is held
  1737. */
  1738. static void csi_RSB(struct vc_data *vc)
  1739. {
  1740. switch (vc->vc_par[0]) {
  1741. case CSI_RSB_COLOR_FOR_UNDERLINE:
  1742. if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
  1743. vc->vc_ulcolor = color_table[vc->vc_par[1]];
  1744. if (vc->state.underline)
  1745. update_attr(vc);
  1746. }
  1747. break;
  1748. case CSI_RSB_COLOR_FOR_HALF_BRIGHT:
  1749. if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
  1750. vc->vc_halfcolor = color_table[vc->vc_par[1]];
  1751. if (vc->state.intensity == VCI_HALF_BRIGHT)
  1752. update_attr(vc);
  1753. }
  1754. break;
  1755. case CSI_RSB_MAKE_CUR_COLOR_DEFAULT:
  1756. vc->vc_def_color = vc->vc_attr;
  1757. if (vc->vc_hi_font_mask == 0x100)
  1758. vc->vc_def_color >>= 1;
  1759. default_attr(vc);
  1760. update_attr(vc);
  1761. break;
  1762. case CSI_RSB_BLANKING_INTERVAL:
  1763. blankinterval = min(vc->vc_par[1], 60U) * 60;
  1764. poke_blanked_console();
  1765. break;
  1766. case CSI_RSB_BELL_FREQUENCY:
  1767. if (vc->vc_npar >= 1)
  1768. vc->vc_bell_pitch = vc->vc_par[1];
  1769. else
  1770. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1771. break;
  1772. case CSI_RSB_BELL_DURATION:
  1773. if (vc->vc_npar >= 1)
  1774. vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
  1775. msecs_to_jiffies(vc->vc_par[1]) : 0;
  1776. else
  1777. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1778. break;
  1779. case CSI_RSB_BRING_CONSOLE_TO_FRONT:
  1780. if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
  1781. set_console(vc->vc_par[1] - 1);
  1782. break;
  1783. case CSI_RSB_UNBLANK:
  1784. poke_blanked_console();
  1785. break;
  1786. case CSI_RSB_VESA_OFF_INTERVAL:
  1787. vesa_off_interval = min(vc->vc_par[1], 60U) * 60 * HZ;
  1788. break;
  1789. case CSI_RSB_BRING_PREV_CONSOLE_TO_FRONT:
  1790. set_console(last_console);
  1791. break;
  1792. case CSI_RSB_CURSOR_BLINK_INTERVAL:
  1793. if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
  1794. vc->vc_par[1] <= USHRT_MAX)
  1795. vc->vc_cur_blink_ms = vc->vc_par[1];
  1796. else
  1797. vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
  1798. break;
  1799. }
  1800. }
  1801. /* console_lock is held */
  1802. static void csi_at(struct vc_data *vc, unsigned int nr)
  1803. {
  1804. nr = clamp(nr, 1, vc->vc_cols - vc->state.x);
  1805. insert_char(vc, nr);
  1806. }
  1807. /* console_lock is held */
  1808. static void csi_L(struct vc_data *vc)
  1809. {
  1810. unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_rows - vc->state.y);
  1811. con_scroll(vc, vc->state.y, vc->vc_bottom, SM_DOWN, nr);
  1812. vc->vc_need_wrap = 0;
  1813. }
  1814. /* console_lock is held */
  1815. static void csi_P(struct vc_data *vc)
  1816. {
  1817. unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_cols - vc->state.x);
  1818. delete_char(vc, nr);
  1819. }
  1820. /* console_lock is held */
  1821. static void csi_M(struct vc_data *vc)
  1822. {
  1823. unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_rows - vc->state.y);
  1824. con_scroll(vc, vc->state.y, vc->vc_bottom, SM_UP, nr);
  1825. vc->vc_need_wrap = 0;
  1826. }
  1827. /* console_lock is held (except via vc_init->reset_terminal */
  1828. static void save_cur(struct vc_data *vc)
  1829. {
  1830. memcpy(&vc->saved_state, &vc->state, sizeof(vc->state));
  1831. }
  1832. /* console_lock is held */
  1833. static void restore_cur(struct vc_data *vc)
  1834. {
  1835. memcpy(&vc->state, &vc->saved_state, sizeof(vc->state));
  1836. gotoxy(vc, vc->state.x, vc->state.y);
  1837. vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset],
  1838. vc);
  1839. update_attr(vc);
  1840. vc->vc_need_wrap = 0;
  1841. }
  1842. /**
  1843. * enum vc_ctl_state - control characters state of a vt
  1844. *
  1845. * @ESnormal: initial state, no control characters parsed
  1846. * @ESesc: ESC parsed
  1847. * @ESsquare: CSI parsed -- modifiers/parameters/ctrl chars expected
  1848. * @ESgetpars: CSI parsed -- parameters/ctrl chars expected
  1849. * @ESfunckey: CSI [ parsed
  1850. * @EShash: ESC # parsed
  1851. * @ESsetG0: ESC ( parsed
  1852. * @ESsetG1: ESC ) parsed
  1853. * @ESpercent: ESC % parsed
  1854. * @EScsiignore: CSI [0x20-0x3f] parsed
  1855. * @ESnonstd: OSC parsed
  1856. * @ESpalette: OSC P parsed
  1857. * @ESosc: OSC [0-9] parsed
  1858. * @ESANSI_first: first state for ignoring ansi control sequences
  1859. * @ESapc: ESC _ parsed
  1860. * @ESpm: ESC ^ parsed
  1861. * @ESdcs: ESC P parsed
  1862. * @ESANSI_last: last state for ignoring ansi control sequences
  1863. */
  1864. enum vc_ctl_state {
  1865. ESnormal,
  1866. ESesc,
  1867. ESsquare,
  1868. ESgetpars,
  1869. ESfunckey,
  1870. EShash,
  1871. ESsetG0,
  1872. ESsetG1,
  1873. ESpercent,
  1874. EScsiignore,
  1875. ESnonstd,
  1876. ESpalette,
  1877. ESosc,
  1878. ESANSI_first = ESosc,
  1879. ESapc,
  1880. ESpm,
  1881. ESdcs,
  1882. ESANSI_last = ESdcs,
  1883. };
  1884. /* console_lock is held (except via vc_init()) */
  1885. static void reset_terminal(struct vc_data *vc, int do_clear)
  1886. {
  1887. unsigned int i;
  1888. vc->vc_top = 0;
  1889. vc->vc_bottom = vc->vc_rows;
  1890. vc->vc_state = ESnormal;
  1891. vc->vc_priv = EPecma;
  1892. vc->vc_translate = set_translate(LAT1_MAP, vc);
  1893. vc->state.Gx_charset[0] = LAT1_MAP;
  1894. vc->state.Gx_charset[1] = GRAF_MAP;
  1895. vc->state.charset = 0;
  1896. vc->vc_need_wrap = 0;
  1897. vc->vc_report_mouse = 0;
  1898. vc->vc_utf = default_utf8;
  1899. vc->vc_utf_count = 0;
  1900. vc->vc_disp_ctrl = 0;
  1901. vc->vc_toggle_meta = 0;
  1902. vc->vc_decscnm = 0;
  1903. vc->vc_decom = 0;
  1904. vc->vc_decawm = 1;
  1905. vc->vc_deccm = global_cursor_default;
  1906. vc->vc_decim = 0;
  1907. vt_reset_keyboard(vc->vc_num);
  1908. vc->vc_cursor_type = cur_default;
  1909. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1910. default_attr(vc);
  1911. update_attr(vc);
  1912. bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT);
  1913. for (i = 0; i < VC_TABSTOPS_COUNT; i += 8)
  1914. set_bit(i, vc->vc_tab_stop);
  1915. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1916. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1917. vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
  1918. gotoxy(vc, 0, 0);
  1919. save_cur(vc);
  1920. if (do_clear)
  1921. csi_J(vc, CSI_J_VISIBLE);
  1922. }
  1923. static void vc_setGx(struct vc_data *vc, unsigned int which, u8 c)
  1924. {
  1925. unsigned char *charset = &vc->state.Gx_charset[which];
  1926. switch (c) {
  1927. case '0':
  1928. *charset = GRAF_MAP;
  1929. break;
  1930. case 'B':
  1931. *charset = LAT1_MAP;
  1932. break;
  1933. case 'U':
  1934. *charset = IBMPC_MAP;
  1935. break;
  1936. case 'K':
  1937. *charset = USER_MAP;
  1938. break;
  1939. }
  1940. if (vc->state.charset == which)
  1941. vc->vc_translate = set_translate(*charset, vc);
  1942. }
  1943. static bool ansi_control_string(enum vc_ctl_state state)
  1944. {
  1945. return state >= ESANSI_first && state <= ESANSI_last;
  1946. }
  1947. enum {
  1948. ASCII_NULL = 0,
  1949. ASCII_BELL = 7,
  1950. ASCII_BACKSPACE = 8,
  1951. ASCII_IGNORE_FIRST = ASCII_BACKSPACE,
  1952. ASCII_HTAB = 9,
  1953. ASCII_LINEFEED = 10,
  1954. ASCII_VTAB = 11,
  1955. ASCII_FORMFEED = 12,
  1956. ASCII_CAR_RET = 13,
  1957. ASCII_IGNORE_LAST = ASCII_CAR_RET,
  1958. ASCII_SHIFTOUT = 14,
  1959. ASCII_SHIFTIN = 15,
  1960. ASCII_CANCEL = 24,
  1961. ASCII_SUBSTITUTE = 26,
  1962. ASCII_ESCAPE = 27,
  1963. ASCII_CSI_IGNORE_FIRST = ' ', /* 0x2x, 0x3a and 0x3c - 0x3f */
  1964. ASCII_CSI_IGNORE_LAST = '?',
  1965. ASCII_DEL = 127,
  1966. ASCII_EXT_CSI = 128 + ASCII_ESCAPE,
  1967. };
  1968. /*
  1969. * Handle ascii characters in control sequences and change states accordingly.
  1970. * E.g. ESC sets the state of vc to ESesc.
  1971. *
  1972. * Returns: true if @c handled.
  1973. */
  1974. static bool handle_ascii(struct tty_struct *tty, struct vc_data *vc, u8 c)
  1975. {
  1976. switch (c) {
  1977. case ASCII_NULL:
  1978. return true;
  1979. case ASCII_BELL:
  1980. if (ansi_control_string(vc->vc_state))
  1981. vc->vc_state = ESnormal;
  1982. else if (vc->vc_bell_duration)
  1983. kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
  1984. return true;
  1985. case ASCII_BACKSPACE:
  1986. bs(vc);
  1987. return true;
  1988. case ASCII_HTAB:
  1989. vc->vc_pos -= (vc->state.x << 1);
  1990. vc->state.x = find_next_bit(vc->vc_tab_stop,
  1991. min(vc->vc_cols - 1, VC_TABSTOPS_COUNT),
  1992. vc->state.x + 1);
  1993. if (vc->state.x >= VC_TABSTOPS_COUNT)
  1994. vc->state.x = vc->vc_cols - 1;
  1995. vc->vc_pos += (vc->state.x << 1);
  1996. notify_write(vc, '\t');
  1997. return true;
  1998. case ASCII_LINEFEED:
  1999. case ASCII_VTAB:
  2000. case ASCII_FORMFEED:
  2001. lf(vc);
  2002. if (!is_kbd(vc, lnm))
  2003. return true;
  2004. fallthrough;
  2005. case ASCII_CAR_RET:
  2006. cr(vc);
  2007. return true;
  2008. case ASCII_SHIFTOUT:
  2009. vc->state.charset = 1;
  2010. vc->vc_translate = set_translate(vc->state.Gx_charset[1], vc);
  2011. vc->vc_disp_ctrl = 1;
  2012. return true;
  2013. case ASCII_SHIFTIN:
  2014. vc->state.charset = 0;
  2015. vc->vc_translate = set_translate(vc->state.Gx_charset[0], vc);
  2016. vc->vc_disp_ctrl = 0;
  2017. return true;
  2018. case ASCII_CANCEL:
  2019. case ASCII_SUBSTITUTE:
  2020. vc->vc_state = ESnormal;
  2021. return true;
  2022. case ASCII_ESCAPE:
  2023. vc->vc_state = ESesc;
  2024. return true;
  2025. case ASCII_DEL:
  2026. del(vc);
  2027. return true;
  2028. case ASCII_EXT_CSI:
  2029. vc->vc_state = ESsquare;
  2030. return true;
  2031. }
  2032. return false;
  2033. }
  2034. /*
  2035. * Handle a character (@c) following an ESC (when @vc is in the ESesc state).
  2036. * E.g. previous ESC with @c == '[' here yields the ESsquare state (that is:
  2037. * CSI).
  2038. */
  2039. static void handle_esc(struct tty_struct *tty, struct vc_data *vc, u8 c)
  2040. {
  2041. vc->vc_state = ESnormal;
  2042. switch (c) {
  2043. case '[':
  2044. vc->vc_state = ESsquare;
  2045. break;
  2046. case ']':
  2047. vc->vc_state = ESnonstd;
  2048. break;
  2049. case '_':
  2050. vc->vc_state = ESapc;
  2051. break;
  2052. case '^':
  2053. vc->vc_state = ESpm;
  2054. break;
  2055. case '%':
  2056. vc->vc_state = ESpercent;
  2057. break;
  2058. case 'E':
  2059. cr(vc);
  2060. lf(vc);
  2061. break;
  2062. case 'M':
  2063. ri(vc);
  2064. break;
  2065. case 'D':
  2066. lf(vc);
  2067. break;
  2068. case 'H':
  2069. if (vc->state.x < VC_TABSTOPS_COUNT)
  2070. set_bit(vc->state.x, vc->vc_tab_stop);
  2071. break;
  2072. case 'P':
  2073. vc->vc_state = ESdcs;
  2074. break;
  2075. case 'Z':
  2076. respond_ID(tty);
  2077. break;
  2078. case '7':
  2079. save_cur(vc);
  2080. break;
  2081. case '8':
  2082. restore_cur(vc);
  2083. break;
  2084. case '(':
  2085. vc->vc_state = ESsetG0;
  2086. break;
  2087. case ')':
  2088. vc->vc_state = ESsetG1;
  2089. break;
  2090. case '#':
  2091. vc->vc_state = EShash;
  2092. break;
  2093. case 'c':
  2094. reset_terminal(vc, 1);
  2095. break;
  2096. case '>': /* Numeric keypad */
  2097. clr_kbd(vc, kbdapplic);
  2098. break;
  2099. case '=': /* Appl. keypad */
  2100. set_kbd(vc, kbdapplic);
  2101. break;
  2102. }
  2103. }
  2104. /*
  2105. * Handle special DEC control sequences ("ESC [ ? parameters char"). Parameters
  2106. * are in @vc->vc_par and the char is in @c here.
  2107. */
  2108. static void csi_DEC(struct tty_struct *tty, struct vc_data *vc, u8 c)
  2109. {
  2110. switch (c) {
  2111. case 'h':
  2112. csi_DEC_hl(vc, true);
  2113. break;
  2114. case 'l':
  2115. csi_DEC_hl(vc, false);
  2116. break;
  2117. case 'c':
  2118. if (vc->vc_par[0])
  2119. vc->vc_cursor_type = CUR_MAKE(vc->vc_par[0],
  2120. vc->vc_par[1],
  2121. vc->vc_par[2]);
  2122. else
  2123. vc->vc_cursor_type = cur_default;
  2124. break;
  2125. case 'm':
  2126. clear_selection();
  2127. if (vc->vc_par[0])
  2128. vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
  2129. else
  2130. vc->vc_complement_mask = vc->vc_s_complement_mask;
  2131. break;
  2132. case 'n':
  2133. if (vc->vc_par[0] == 5)
  2134. status_report(tty);
  2135. else if (vc->vc_par[0] == 6)
  2136. cursor_report(vc, tty);
  2137. break;
  2138. }
  2139. }
  2140. /*
  2141. * Handle Control Sequence Introducer control characters. That is
  2142. * "ESC [ parameters char". Parameters are in @vc->vc_par and the char is in
  2143. * @c here.
  2144. */
  2145. static void csi_ECMA(struct tty_struct *tty, struct vc_data *vc, u8 c)
  2146. {
  2147. switch (c) {
  2148. case 'G':
  2149. case '`':
  2150. if (vc->vc_par[0])
  2151. vc->vc_par[0]--;
  2152. gotoxy(vc, vc->vc_par[0], vc->state.y);
  2153. break;
  2154. case 'A':
  2155. if (!vc->vc_par[0])
  2156. vc->vc_par[0]++;
  2157. gotoxy(vc, vc->state.x, vc->state.y - vc->vc_par[0]);
  2158. break;
  2159. case 'B':
  2160. case 'e':
  2161. if (!vc->vc_par[0])
  2162. vc->vc_par[0]++;
  2163. gotoxy(vc, vc->state.x, vc->state.y + vc->vc_par[0]);
  2164. break;
  2165. case 'C':
  2166. case 'a':
  2167. if (!vc->vc_par[0])
  2168. vc->vc_par[0]++;
  2169. gotoxy(vc, vc->state.x + vc->vc_par[0], vc->state.y);
  2170. break;
  2171. case 'D':
  2172. if (!vc->vc_par[0])
  2173. vc->vc_par[0]++;
  2174. gotoxy(vc, vc->state.x - vc->vc_par[0], vc->state.y);
  2175. break;
  2176. case 'E':
  2177. if (!vc->vc_par[0])
  2178. vc->vc_par[0]++;
  2179. gotoxy(vc, 0, vc->state.y + vc->vc_par[0]);
  2180. break;
  2181. case 'F':
  2182. if (!vc->vc_par[0])
  2183. vc->vc_par[0]++;
  2184. gotoxy(vc, 0, vc->state.y - vc->vc_par[0]);
  2185. break;
  2186. case 'd':
  2187. if (vc->vc_par[0])
  2188. vc->vc_par[0]--;
  2189. gotoxay(vc, vc->state.x ,vc->vc_par[0]);
  2190. break;
  2191. case 'H':
  2192. case 'f':
  2193. if (vc->vc_par[0])
  2194. vc->vc_par[0]--;
  2195. if (vc->vc_par[1])
  2196. vc->vc_par[1]--;
  2197. gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
  2198. break;
  2199. case 'J':
  2200. csi_J(vc, vc->vc_par[0]);
  2201. break;
  2202. case 'K':
  2203. csi_K(vc);
  2204. break;
  2205. case 'L':
  2206. csi_L(vc);
  2207. break;
  2208. case 'M':
  2209. csi_M(vc);
  2210. break;
  2211. case 'P':
  2212. csi_P(vc);
  2213. break;
  2214. case 'c':
  2215. if (!vc->vc_par[0])
  2216. respond_ID(tty);
  2217. break;
  2218. case 'g':
  2219. if (!vc->vc_par[0] && vc->state.x < VC_TABSTOPS_COUNT)
  2220. set_bit(vc->state.x, vc->vc_tab_stop);
  2221. else if (vc->vc_par[0] == 3)
  2222. bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT);
  2223. break;
  2224. case 'h':
  2225. csi_hl(vc, true);
  2226. break;
  2227. case 'l':
  2228. csi_hl(vc, false);
  2229. break;
  2230. case 'm':
  2231. csi_m(vc);
  2232. break;
  2233. case 'n':
  2234. if (vc->vc_par[0] == 5)
  2235. status_report(tty);
  2236. else if (vc->vc_par[0] == 6)
  2237. cursor_report(vc, tty);
  2238. break;
  2239. case 'q': /* DECLL - but only 3 leds */
  2240. /* map 0,1,2,3 to 0,1,2,4 */
  2241. if (vc->vc_par[0] < 4)
  2242. vt_set_led_state(vc->vc_num,
  2243. (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
  2244. break;
  2245. case 'r':
  2246. if (!vc->vc_par[0])
  2247. vc->vc_par[0]++;
  2248. if (!vc->vc_par[1])
  2249. vc->vc_par[1] = vc->vc_rows;
  2250. /* Minimum allowed region is 2 lines */
  2251. if (vc->vc_par[0] < vc->vc_par[1] &&
  2252. vc->vc_par[1] <= vc->vc_rows) {
  2253. vc->vc_top = vc->vc_par[0] - 1;
  2254. vc->vc_bottom = vc->vc_par[1];
  2255. gotoxay(vc, 0, 0);
  2256. }
  2257. break;
  2258. case 's':
  2259. save_cur(vc);
  2260. break;
  2261. case 'u':
  2262. restore_cur(vc);
  2263. break;
  2264. case 'X':
  2265. csi_X(vc);
  2266. break;
  2267. case '@':
  2268. csi_at(vc, vc->vc_par[0]);
  2269. break;
  2270. case ']':
  2271. csi_RSB(vc);
  2272. break;
  2273. }
  2274. }
  2275. static void vc_reset_params(struct vc_data *vc)
  2276. {
  2277. memset(vc->vc_par, 0, sizeof(vc->vc_par));
  2278. vc->vc_npar = 0;
  2279. }
  2280. /* console_lock is held */
  2281. static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, u8 c)
  2282. {
  2283. /*
  2284. * Control characters can be used in the _middle_
  2285. * of an escape sequence, aside from ANSI control strings.
  2286. */
  2287. if (ansi_control_string(vc->vc_state) && c >= ASCII_IGNORE_FIRST &&
  2288. c <= ASCII_IGNORE_LAST)
  2289. return;
  2290. if (handle_ascii(tty, vc, c))
  2291. return;
  2292. switch(vc->vc_state) {
  2293. case ESesc: /* ESC */
  2294. handle_esc(tty, vc, c);
  2295. return;
  2296. case ESnonstd: /* ESC ] aka OSC */
  2297. switch (c) {
  2298. case 'P': /* palette escape sequence */
  2299. vc_reset_params(vc);
  2300. vc->vc_state = ESpalette;
  2301. return;
  2302. case 'R': /* reset palette */
  2303. reset_palette(vc);
  2304. break;
  2305. case '0' ... '9':
  2306. vc->vc_state = ESosc;
  2307. return;
  2308. }
  2309. vc->vc_state = ESnormal;
  2310. return;
  2311. case ESpalette: /* ESC ] P aka OSC P */
  2312. if (isxdigit(c)) {
  2313. vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
  2314. if (vc->vc_npar == 7) {
  2315. int i = vc->vc_par[0] * 3, j = 1;
  2316. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  2317. vc->vc_palette[i++] += vc->vc_par[j++];
  2318. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  2319. vc->vc_palette[i++] += vc->vc_par[j++];
  2320. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  2321. vc->vc_palette[i] += vc->vc_par[j];
  2322. set_palette(vc);
  2323. vc->vc_state = ESnormal;
  2324. }
  2325. } else
  2326. vc->vc_state = ESnormal;
  2327. return;
  2328. case ESsquare: /* ESC [ aka CSI, parameters or modifiers expected */
  2329. vc_reset_params(vc);
  2330. vc->vc_state = ESgetpars;
  2331. switch (c) {
  2332. case '[': /* Function key */
  2333. vc->vc_state = ESfunckey;
  2334. return;
  2335. case '?':
  2336. vc->vc_priv = EPdec;
  2337. return;
  2338. case '>':
  2339. vc->vc_priv = EPgt;
  2340. return;
  2341. case '=':
  2342. vc->vc_priv = EPeq;
  2343. return;
  2344. case '<':
  2345. vc->vc_priv = EPlt;
  2346. return;
  2347. }
  2348. vc->vc_priv = EPecma;
  2349. fallthrough;
  2350. case ESgetpars: /* ESC [ aka CSI, parameters expected */
  2351. switch (c) {
  2352. case ';':
  2353. if (vc->vc_npar < NPAR - 1) {
  2354. vc->vc_npar++;
  2355. return;
  2356. }
  2357. break;
  2358. case '0' ... '9':
  2359. vc->vc_par[vc->vc_npar] *= 10;
  2360. vc->vc_par[vc->vc_npar] += c - '0';
  2361. return;
  2362. }
  2363. if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST) {
  2364. vc->vc_state = EScsiignore;
  2365. return;
  2366. }
  2367. /* parameters done, handle the control char @c */
  2368. vc->vc_state = ESnormal;
  2369. switch (vc->vc_priv) {
  2370. case EPdec:
  2371. csi_DEC(tty, vc, c);
  2372. return;
  2373. case EPecma:
  2374. csi_ECMA(tty, vc, c);
  2375. return;
  2376. default:
  2377. return;
  2378. }
  2379. case EScsiignore:
  2380. if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST)
  2381. return;
  2382. vc->vc_state = ESnormal;
  2383. return;
  2384. case ESpercent: /* ESC % */
  2385. vc->vc_state = ESnormal;
  2386. switch (c) {
  2387. case '@': /* defined in ISO 2022 */
  2388. vc->vc_utf = 0;
  2389. return;
  2390. case 'G': /* prelim official escape code */
  2391. case '8': /* retained for compatibility */
  2392. vc->vc_utf = 1;
  2393. return;
  2394. }
  2395. return;
  2396. case ESfunckey: /* ESC [ [ aka CSI [ */
  2397. vc->vc_state = ESnormal;
  2398. return;
  2399. case EShash: /* ESC # */
  2400. vc->vc_state = ESnormal;
  2401. if (c == '8') {
  2402. /* DEC screen alignment test. kludge :-) */
  2403. vc->vc_video_erase_char =
  2404. (vc->vc_video_erase_char & 0xff00) | 'E';
  2405. csi_J(vc, CSI_J_VISIBLE);
  2406. vc->vc_video_erase_char =
  2407. (vc->vc_video_erase_char & 0xff00) | ' ';
  2408. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  2409. }
  2410. return;
  2411. case ESsetG0: /* ESC ( */
  2412. vc_setGx(vc, 0, c);
  2413. vc->vc_state = ESnormal;
  2414. return;
  2415. case ESsetG1: /* ESC ) */
  2416. vc_setGx(vc, 1, c);
  2417. vc->vc_state = ESnormal;
  2418. return;
  2419. case ESapc: /* ESC _ */
  2420. return;
  2421. case ESosc: /* ESC ] [0-9] aka OSC [0-9] */
  2422. return;
  2423. case ESpm: /* ESC ^ */
  2424. return;
  2425. case ESdcs: /* ESC P */
  2426. return;
  2427. default:
  2428. vc->vc_state = ESnormal;
  2429. }
  2430. }
  2431. /* is_double_width() is based on the wcwidth() implementation by
  2432. * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
  2433. * Latest version: https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
  2434. */
  2435. struct interval {
  2436. uint32_t first;
  2437. uint32_t last;
  2438. };
  2439. static int ucs_cmp(const void *key, const void *elt)
  2440. {
  2441. uint32_t ucs = *(uint32_t *)key;
  2442. struct interval e = *(struct interval *) elt;
  2443. if (ucs > e.last)
  2444. return 1;
  2445. else if (ucs < e.first)
  2446. return -1;
  2447. return 0;
  2448. }
  2449. static int is_double_width(uint32_t ucs)
  2450. {
  2451. static const struct interval double_width[] = {
  2452. { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
  2453. { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
  2454. { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
  2455. { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
  2456. };
  2457. if (ucs < double_width[0].first ||
  2458. ucs > double_width[ARRAY_SIZE(double_width) - 1].last)
  2459. return 0;
  2460. return bsearch(&ucs, double_width, ARRAY_SIZE(double_width),
  2461. sizeof(struct interval), ucs_cmp) != NULL;
  2462. }
  2463. struct vc_draw_region {
  2464. unsigned long from, to;
  2465. int x;
  2466. };
  2467. static void con_flush(struct vc_data *vc, struct vc_draw_region *draw)
  2468. {
  2469. if (draw->x < 0)
  2470. return;
  2471. vc->vc_sw->con_putcs(vc, (u16 *)draw->from,
  2472. (u16 *)draw->to - (u16 *)draw->from, vc->state.y,
  2473. draw->x);
  2474. draw->x = -1;
  2475. }
  2476. static inline int vc_translate_ascii(const struct vc_data *vc, int c)
  2477. {
  2478. if (IS_ENABLED(CONFIG_CONSOLE_TRANSLATIONS)) {
  2479. if (vc->vc_toggle_meta)
  2480. c |= 0x80;
  2481. return vc->vc_translate[c];
  2482. }
  2483. return c;
  2484. }
  2485. /**
  2486. * vc_sanitize_unicode - Replace invalid Unicode code points with ``U+FFFD``
  2487. * @c: the received code point
  2488. */
  2489. static inline int vc_sanitize_unicode(const int c)
  2490. {
  2491. if (c >= 0xd800 && c <= 0xdfff)
  2492. return 0xfffd;
  2493. return c;
  2494. }
  2495. /**
  2496. * vc_translate_unicode - Combine UTF-8 into Unicode in &vc_data.vc_utf_char
  2497. * @vc: virtual console
  2498. * @c: UTF-8 byte to translate
  2499. * @rescan: set to true iff @c wasn't consumed here and needs to be re-processed
  2500. *
  2501. * * &vc_data.vc_utf_char is the being-constructed Unicode code point.
  2502. * * &vc_data.vc_utf_count is the number of continuation bytes still expected to
  2503. * arrive.
  2504. * * &vc_data.vc_npar is the number of continuation bytes arrived so far.
  2505. *
  2506. * Return:
  2507. * * %-1 - Input OK so far, @c consumed, further bytes expected.
  2508. * * %0xFFFD - Possibility 1: input invalid, @c may have been consumed (see
  2509. * desc. of @rescan). Possibility 2: input OK, @c consumed,
  2510. * ``U+FFFD`` is the resulting code point. ``U+FFFD`` is valid,
  2511. * ``REPLACEMENT CHARACTER``.
  2512. * * otherwise - Input OK, @c consumed, resulting code point returned.
  2513. */
  2514. static int vc_translate_unicode(struct vc_data *vc, int c, bool *rescan)
  2515. {
  2516. static const u32 utf8_length_changes[] = {0x7f, 0x7ff, 0xffff, 0x10ffff};
  2517. /* Continuation byte received */
  2518. if ((c & 0xc0) == 0x80) {
  2519. /* Unexpected continuation byte? */
  2520. if (!vc->vc_utf_count)
  2521. return 0xfffd;
  2522. vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
  2523. vc->vc_npar++;
  2524. if (--vc->vc_utf_count)
  2525. goto need_more_bytes;
  2526. /* Got a whole character */
  2527. c = vc->vc_utf_char;
  2528. /* Reject overlong sequences */
  2529. if (c <= utf8_length_changes[vc->vc_npar - 1] ||
  2530. c > utf8_length_changes[vc->vc_npar])
  2531. return 0xfffd;
  2532. return vc_sanitize_unicode(c);
  2533. }
  2534. /* Single ASCII byte or first byte of a sequence received */
  2535. if (vc->vc_utf_count) {
  2536. /* Continuation byte expected */
  2537. *rescan = true;
  2538. vc->vc_utf_count = 0;
  2539. return 0xfffd;
  2540. }
  2541. /* Nothing to do if an ASCII byte was received */
  2542. if (c <= 0x7f)
  2543. return c;
  2544. /* First byte of a multibyte sequence received */
  2545. vc->vc_npar = 0;
  2546. if ((c & 0xe0) == 0xc0) {
  2547. vc->vc_utf_count = 1;
  2548. vc->vc_utf_char = (c & 0x1f);
  2549. } else if ((c & 0xf0) == 0xe0) {
  2550. vc->vc_utf_count = 2;
  2551. vc->vc_utf_char = (c & 0x0f);
  2552. } else if ((c & 0xf8) == 0xf0) {
  2553. vc->vc_utf_count = 3;
  2554. vc->vc_utf_char = (c & 0x07);
  2555. } else {
  2556. return 0xfffd;
  2557. }
  2558. need_more_bytes:
  2559. return -1;
  2560. }
  2561. static int vc_translate(struct vc_data *vc, int *c, bool *rescan)
  2562. {
  2563. /* Do no translation at all in control states */
  2564. if (vc->vc_state != ESnormal)
  2565. return *c;
  2566. if (vc->vc_utf && !vc->vc_disp_ctrl)
  2567. return *c = vc_translate_unicode(vc, *c, rescan);
  2568. /* no utf or alternate charset mode */
  2569. return vc_translate_ascii(vc, *c);
  2570. }
  2571. static inline unsigned char vc_invert_attr(const struct vc_data *vc)
  2572. {
  2573. if (!vc->vc_can_do_color)
  2574. return vc->vc_attr ^ 0x08;
  2575. if (vc->vc_hi_font_mask == 0x100)
  2576. return (vc->vc_attr & 0x11) |
  2577. ((vc->vc_attr & 0xe0) >> 4) |
  2578. ((vc->vc_attr & 0x0e) << 4);
  2579. return (vc->vc_attr & 0x88) |
  2580. ((vc->vc_attr & 0x70) >> 4) |
  2581. ((vc->vc_attr & 0x07) << 4);
  2582. }
  2583. static bool vc_is_control(struct vc_data *vc, int tc, int c)
  2584. {
  2585. /*
  2586. * A bitmap for codes <32. A bit of 1 indicates that the code
  2587. * corresponding to that bit number invokes some special action (such
  2588. * as cursor movement) and should not be displayed as a glyph unless
  2589. * the disp_ctrl mode is explicitly enabled.
  2590. */
  2591. static const u32 CTRL_ACTION = BIT(ASCII_NULL) |
  2592. GENMASK(ASCII_SHIFTIN, ASCII_BELL) | BIT(ASCII_CANCEL) |
  2593. BIT(ASCII_SUBSTITUTE) | BIT(ASCII_ESCAPE);
  2594. /* Cannot be overridden by disp_ctrl */
  2595. static const u32 CTRL_ALWAYS = BIT(ASCII_NULL) | BIT(ASCII_BACKSPACE) |
  2596. BIT(ASCII_LINEFEED) | BIT(ASCII_SHIFTIN) | BIT(ASCII_SHIFTOUT) |
  2597. BIT(ASCII_CAR_RET) | BIT(ASCII_FORMFEED) | BIT(ASCII_ESCAPE);
  2598. if (vc->vc_state != ESnormal)
  2599. return true;
  2600. if (!tc)
  2601. return true;
  2602. /*
  2603. * If the original code was a control character we only allow a glyph
  2604. * to be displayed if the code is not normally used (such as for cursor
  2605. * movement) or if the disp_ctrl mode has been explicitly enabled.
  2606. * Certain characters (as given by the CTRL_ALWAYS bitmap) are always
  2607. * displayed as control characters, as the console would be pretty
  2608. * useless without them; to display an arbitrary font position use the
  2609. * direct-to-font zone in UTF-8 mode.
  2610. */
  2611. if (c < BITS_PER_TYPE(CTRL_ALWAYS)) {
  2612. if (vc->vc_disp_ctrl)
  2613. return CTRL_ALWAYS & BIT(c);
  2614. else
  2615. return vc->vc_utf || (CTRL_ACTION & BIT(c));
  2616. }
  2617. if (c == ASCII_DEL && !vc->vc_disp_ctrl)
  2618. return true;
  2619. if (c == ASCII_EXT_CSI)
  2620. return true;
  2621. return false;
  2622. }
  2623. static int vc_con_write_normal(struct vc_data *vc, int tc, int c,
  2624. struct vc_draw_region *draw)
  2625. {
  2626. int next_c;
  2627. unsigned char vc_attr = vc->vc_attr;
  2628. u16 himask = vc->vc_hi_font_mask, charmask = himask ? 0x1ff : 0xff;
  2629. u8 width = 1;
  2630. bool inverse = false;
  2631. if (vc->vc_utf && !vc->vc_disp_ctrl) {
  2632. if (is_double_width(c))
  2633. width = 2;
  2634. }
  2635. /* Now try to find out how to display it */
  2636. tc = conv_uni_to_pc(vc, tc);
  2637. if (tc & ~charmask) {
  2638. if (tc == -1 || tc == -2)
  2639. return -1; /* nothing to display */
  2640. /* Glyph not found */
  2641. if ((!vc->vc_utf || vc->vc_disp_ctrl || c < 128) &&
  2642. !(c & ~charmask)) {
  2643. /*
  2644. * In legacy mode use the glyph we get by a 1:1
  2645. * mapping.
  2646. * This would make absolutely no sense with Unicode in
  2647. * mind, but do this for ASCII characters since a font
  2648. * may lack Unicode mapping info and we don't want to
  2649. * end up with having question marks only.
  2650. */
  2651. tc = c;
  2652. } else {
  2653. /*
  2654. * Display U+FFFD. If it's not found, display an inverse
  2655. * question mark.
  2656. */
  2657. tc = conv_uni_to_pc(vc, 0xfffd);
  2658. if (tc < 0) {
  2659. inverse = true;
  2660. tc = conv_uni_to_pc(vc, '?');
  2661. if (tc < 0)
  2662. tc = '?';
  2663. vc_attr = vc_invert_attr(vc);
  2664. con_flush(vc, draw);
  2665. }
  2666. }
  2667. }
  2668. next_c = c;
  2669. while (1) {
  2670. if (vc->vc_need_wrap || vc->vc_decim)
  2671. con_flush(vc, draw);
  2672. if (vc->vc_need_wrap) {
  2673. cr(vc);
  2674. lf(vc);
  2675. }
  2676. if (vc->vc_decim)
  2677. insert_char(vc, 1);
  2678. vc_uniscr_putc(vc, next_c);
  2679. if (himask)
  2680. tc = ((tc & 0x100) ? himask : 0) |
  2681. (tc & 0xff);
  2682. tc |= (vc_attr << 8) & ~himask;
  2683. scr_writew(tc, (u16 *)vc->vc_pos);
  2684. if (con_should_update(vc) && draw->x < 0) {
  2685. draw->x = vc->state.x;
  2686. draw->from = vc->vc_pos;
  2687. }
  2688. if (vc->state.x == vc->vc_cols - 1) {
  2689. vc->vc_need_wrap = vc->vc_decawm;
  2690. draw->to = vc->vc_pos + 2;
  2691. } else {
  2692. vc->state.x++;
  2693. draw->to = (vc->vc_pos += 2);
  2694. }
  2695. if (!--width)
  2696. break;
  2697. /* A space is printed in the second column */
  2698. tc = conv_uni_to_pc(vc, ' ');
  2699. if (tc < 0)
  2700. tc = ' ';
  2701. next_c = ' ';
  2702. }
  2703. notify_write(vc, c);
  2704. if (inverse)
  2705. con_flush(vc, draw);
  2706. return 0;
  2707. }
  2708. /* acquires console_lock */
  2709. static int do_con_write(struct tty_struct *tty, const u8 *buf, int count)
  2710. {
  2711. struct vc_draw_region draw = {
  2712. .x = -1,
  2713. };
  2714. int c, tc, n = 0;
  2715. unsigned int currcons;
  2716. struct vc_data *vc = tty->driver_data;
  2717. struct vt_notifier_param param;
  2718. bool rescan;
  2719. if (in_interrupt())
  2720. return count;
  2721. console_lock();
  2722. currcons = vc->vc_num;
  2723. if (!vc_cons_allocated(currcons)) {
  2724. /* could this happen? */
  2725. pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
  2726. console_unlock();
  2727. return 0;
  2728. }
  2729. /* undraw cursor first */
  2730. if (con_is_fg(vc))
  2731. hide_cursor(vc);
  2732. param.vc = vc;
  2733. while (!tty->flow.stopped && count) {
  2734. u8 orig = *buf;
  2735. buf++;
  2736. n++;
  2737. count--;
  2738. rescan_last_byte:
  2739. c = orig;
  2740. rescan = false;
  2741. tc = vc_translate(vc, &c, &rescan);
  2742. if (tc == -1)
  2743. continue;
  2744. param.c = tc;
  2745. if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
  2746. &param) == NOTIFY_STOP)
  2747. continue;
  2748. if (vc_is_control(vc, tc, c)) {
  2749. con_flush(vc, &draw);
  2750. do_con_trol(tty, vc, orig);
  2751. continue;
  2752. }
  2753. if (vc_con_write_normal(vc, tc, c, &draw) < 0)
  2754. continue;
  2755. if (rescan)
  2756. goto rescan_last_byte;
  2757. }
  2758. con_flush(vc, &draw);
  2759. console_conditional_schedule();
  2760. notify_update(vc);
  2761. console_unlock();
  2762. return n;
  2763. }
  2764. /*
  2765. * This is the console switching callback.
  2766. *
  2767. * Doing console switching in a process context allows
  2768. * us to do the switches asynchronously (needed when we want
  2769. * to switch due to a keyboard interrupt). Synchronization
  2770. * with other console code and prevention of re-entrancy is
  2771. * ensured with console_lock.
  2772. */
  2773. static void console_callback(struct work_struct *ignored)
  2774. {
  2775. console_lock();
  2776. if (want_console >= 0) {
  2777. if (want_console != fg_console &&
  2778. vc_cons_allocated(want_console)) {
  2779. hide_cursor(vc_cons[fg_console].d);
  2780. change_console(vc_cons[want_console].d);
  2781. /* we only changed when the console had already
  2782. been allocated - a new console is not created
  2783. in an interrupt routine */
  2784. }
  2785. want_console = -1;
  2786. }
  2787. if (do_poke_blanked_console) { /* do not unblank for a LED change */
  2788. do_poke_blanked_console = 0;
  2789. poke_blanked_console();
  2790. }
  2791. if (scrollback_delta) {
  2792. struct vc_data *vc = vc_cons[fg_console].d;
  2793. clear_selection();
  2794. if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
  2795. vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
  2796. scrollback_delta = 0;
  2797. }
  2798. if (blank_timer_expired) {
  2799. do_blank_screen(0);
  2800. blank_timer_expired = 0;
  2801. }
  2802. notify_update(vc_cons[fg_console].d);
  2803. console_unlock();
  2804. }
  2805. int set_console(int nr)
  2806. {
  2807. struct vc_data *vc = vc_cons[fg_console].d;
  2808. if (!vc_cons_allocated(nr) || vt_dont_switch ||
  2809. (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
  2810. /*
  2811. * Console switch will fail in console_callback() or
  2812. * change_console() so there is no point scheduling
  2813. * the callback
  2814. *
  2815. * Existing set_console() users don't check the return
  2816. * value so this shouldn't break anything
  2817. */
  2818. return -EINVAL;
  2819. }
  2820. want_console = nr;
  2821. schedule_console_callback();
  2822. return 0;
  2823. }
  2824. struct tty_driver *console_driver;
  2825. #ifdef CONFIG_VT_CONSOLE
  2826. /**
  2827. * vt_kmsg_redirect() - sets/gets the kernel message console
  2828. * @new: the new virtual terminal number or -1 if the console should stay
  2829. * unchanged
  2830. *
  2831. * By default, the kernel messages are always printed on the current virtual
  2832. * console. However, the user may modify that default with the
  2833. * %TIOCL_SETKMSGREDIRECT ioctl call.
  2834. *
  2835. * This function sets the kernel message console to be @new. It returns the old
  2836. * virtual console number. The virtual terminal number %0 (both as parameter and
  2837. * return value) means no redirection (i.e. always printed on the currently
  2838. * active console).
  2839. *
  2840. * The parameter -1 means that only the current console is returned, but the
  2841. * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
  2842. * case to make the code more understandable.
  2843. *
  2844. * When the kernel is compiled without %CONFIG_VT_CONSOLE, this function ignores
  2845. * the parameter and always returns %0.
  2846. */
  2847. int vt_kmsg_redirect(int new)
  2848. {
  2849. static int kmsg_con;
  2850. if (new != -1)
  2851. return xchg(&kmsg_con, new);
  2852. else
  2853. return kmsg_con;
  2854. }
  2855. /*
  2856. * Console on virtual terminal
  2857. *
  2858. * The console must be locked when we get here.
  2859. */
  2860. static void vt_console_print(struct console *co, const char *b, unsigned count)
  2861. {
  2862. struct vc_data *vc = vc_cons[fg_console].d;
  2863. unsigned char c;
  2864. static DEFINE_SPINLOCK(printing_lock);
  2865. const ushort *start;
  2866. ushort start_x, cnt;
  2867. int kmsg_console;
  2868. WARN_CONSOLE_UNLOCKED();
  2869. /* this protects against concurrent oops only */
  2870. if (!spin_trylock(&printing_lock))
  2871. return;
  2872. kmsg_console = vt_get_kmsg_redirect();
  2873. if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
  2874. vc = vc_cons[kmsg_console - 1].d;
  2875. if (!vc_cons_allocated(fg_console)) {
  2876. /* impossible */
  2877. /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
  2878. goto quit;
  2879. }
  2880. if (vc->vc_mode != KD_TEXT)
  2881. goto quit;
  2882. /* undraw cursor first */
  2883. if (con_is_fg(vc))
  2884. hide_cursor(vc);
  2885. start = (ushort *)vc->vc_pos;
  2886. start_x = vc->state.x;
  2887. cnt = 0;
  2888. while (count--) {
  2889. c = *b++;
  2890. if (c == ASCII_LINEFEED || c == ASCII_CAR_RET ||
  2891. c == ASCII_BACKSPACE || vc->vc_need_wrap) {
  2892. if (cnt && con_is_visible(vc))
  2893. vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
  2894. cnt = 0;
  2895. if (c == ASCII_BACKSPACE) {
  2896. bs(vc);
  2897. start = (ushort *)vc->vc_pos;
  2898. start_x = vc->state.x;
  2899. continue;
  2900. }
  2901. if (c != ASCII_CAR_RET)
  2902. lf(vc);
  2903. cr(vc);
  2904. start = (ushort *)vc->vc_pos;
  2905. start_x = vc->state.x;
  2906. if (c == ASCII_LINEFEED || c == ASCII_CAR_RET)
  2907. continue;
  2908. }
  2909. vc_uniscr_putc(vc, c);
  2910. scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
  2911. notify_write(vc, c);
  2912. cnt++;
  2913. if (vc->state.x == vc->vc_cols - 1) {
  2914. vc->vc_need_wrap = 1;
  2915. } else {
  2916. vc->vc_pos += 2;
  2917. vc->state.x++;
  2918. }
  2919. }
  2920. if (cnt && con_is_visible(vc))
  2921. vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
  2922. set_cursor(vc);
  2923. notify_update(vc);
  2924. quit:
  2925. spin_unlock(&printing_lock);
  2926. }
  2927. static struct tty_driver *vt_console_device(struct console *c, int *index)
  2928. {
  2929. *index = c->index ? c->index-1 : fg_console;
  2930. return console_driver;
  2931. }
  2932. static int vt_console_setup(struct console *co, char *options)
  2933. {
  2934. return co->index >= MAX_NR_CONSOLES ? -EINVAL : 0;
  2935. }
  2936. static struct console vt_console_driver = {
  2937. .name = "tty",
  2938. .setup = vt_console_setup,
  2939. .write = vt_console_print,
  2940. .device = vt_console_device,
  2941. .unblank = unblank_screen,
  2942. .flags = CON_PRINTBUFFER,
  2943. .index = -1,
  2944. };
  2945. #endif
  2946. /*
  2947. * Handling of Linux-specific VC ioctls
  2948. */
  2949. /*
  2950. * Generally a bit racy with respect to console_lock();.
  2951. *
  2952. * There are some functions which don't need it.
  2953. *
  2954. * There are some functions which can sleep for arbitrary periods
  2955. * (paste_selection) but we don't need the lock there anyway.
  2956. *
  2957. * set_selection_user has locking, and definitely needs it
  2958. */
  2959. int tioclinux(struct tty_struct *tty, unsigned long arg)
  2960. {
  2961. char type, data;
  2962. char __user *p = (char __user *)arg;
  2963. void __user *param_aligned32 = (u32 __user *)arg + 1;
  2964. void __user *param = (void __user *)arg + 1;
  2965. int lines;
  2966. int ret;
  2967. if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
  2968. return -EPERM;
  2969. if (get_user(type, p))
  2970. return -EFAULT;
  2971. ret = 0;
  2972. switch (type) {
  2973. case TIOCL_SETSEL:
  2974. return set_selection_user(param, tty);
  2975. case TIOCL_PASTESEL:
  2976. if (!capable(CAP_SYS_ADMIN))
  2977. return -EPERM;
  2978. return paste_selection(tty);
  2979. case TIOCL_UNBLANKSCREEN:
  2980. console_lock();
  2981. unblank_screen();
  2982. console_unlock();
  2983. break;
  2984. case TIOCL_SELLOADLUT:
  2985. if (!capable(CAP_SYS_ADMIN))
  2986. return -EPERM;
  2987. return sel_loadlut(param_aligned32);
  2988. case TIOCL_GETSHIFTSTATE:
  2989. /*
  2990. * Make it possible to react to Shift+Mousebutton. Note that
  2991. * 'shift_state' is an undocumented kernel-internal variable;
  2992. * programs not closely related to the kernel should not use
  2993. * this.
  2994. */
  2995. data = vt_get_shift_state();
  2996. return put_user(data, p);
  2997. case TIOCL_GETMOUSEREPORTING:
  2998. console_lock(); /* May be overkill */
  2999. data = mouse_reporting();
  3000. console_unlock();
  3001. return put_user(data, p);
  3002. case TIOCL_SETVESABLANK:
  3003. return set_vesa_blanking(param);
  3004. case TIOCL_GETKMSGREDIRECT:
  3005. data = vt_get_kmsg_redirect();
  3006. return put_user(data, p);
  3007. case TIOCL_SETKMSGREDIRECT:
  3008. if (!capable(CAP_SYS_ADMIN))
  3009. return -EPERM;
  3010. if (get_user(data, p+1))
  3011. return -EFAULT;
  3012. vt_kmsg_redirect(data);
  3013. break;
  3014. case TIOCL_GETFGCONSOLE:
  3015. /*
  3016. * No locking needed as this is a transiently correct return
  3017. * anyway if the caller hasn't disabled switching.
  3018. */
  3019. return fg_console;
  3020. case TIOCL_SCROLLCONSOLE:
  3021. if (get_user(lines, (s32 __user *)param_aligned32))
  3022. return -EFAULT;
  3023. /*
  3024. * Needs the console lock here. Note that lots of other calls
  3025. * need fixing before the lock is actually useful!
  3026. */
  3027. console_lock();
  3028. scrollfront(vc_cons[fg_console].d, lines);
  3029. console_unlock();
  3030. break;
  3031. case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
  3032. console_lock();
  3033. ignore_poke = 1;
  3034. do_blank_screen(0);
  3035. console_unlock();
  3036. break;
  3037. case TIOCL_BLANKEDSCREEN:
  3038. return console_blanked;
  3039. default:
  3040. return -EINVAL;
  3041. }
  3042. return ret;
  3043. }
  3044. /*
  3045. * /dev/ttyN handling
  3046. */
  3047. static ssize_t con_write(struct tty_struct *tty, const u8 *buf, size_t count)
  3048. {
  3049. int retval;
  3050. retval = do_con_write(tty, buf, count);
  3051. con_flush_chars(tty);
  3052. return retval;
  3053. }
  3054. static int con_put_char(struct tty_struct *tty, u8 ch)
  3055. {
  3056. return do_con_write(tty, &ch, 1);
  3057. }
  3058. static unsigned int con_write_room(struct tty_struct *tty)
  3059. {
  3060. if (tty->flow.stopped)
  3061. return 0;
  3062. return 32768; /* No limit, really; we're not buffering */
  3063. }
  3064. /*
  3065. * con_throttle and con_unthrottle are only used for
  3066. * paste_selection(), which has to stuff in a large number of
  3067. * characters...
  3068. */
  3069. static void con_throttle(struct tty_struct *tty)
  3070. {
  3071. }
  3072. static void con_unthrottle(struct tty_struct *tty)
  3073. {
  3074. struct vc_data *vc = tty->driver_data;
  3075. wake_up_interruptible(&vc->paste_wait);
  3076. }
  3077. /*
  3078. * Turn the Scroll-Lock LED on when the tty is stopped
  3079. */
  3080. static void con_stop(struct tty_struct *tty)
  3081. {
  3082. int console_num;
  3083. if (!tty)
  3084. return;
  3085. console_num = tty->index;
  3086. if (!vc_cons_allocated(console_num))
  3087. return;
  3088. vt_kbd_con_stop(console_num);
  3089. }
  3090. /*
  3091. * Turn the Scroll-Lock LED off when the console is started
  3092. */
  3093. static void con_start(struct tty_struct *tty)
  3094. {
  3095. int console_num;
  3096. if (!tty)
  3097. return;
  3098. console_num = tty->index;
  3099. if (!vc_cons_allocated(console_num))
  3100. return;
  3101. vt_kbd_con_start(console_num);
  3102. }
  3103. static void con_flush_chars(struct tty_struct *tty)
  3104. {
  3105. struct vc_data *vc = tty->driver_data;
  3106. if (in_interrupt()) /* from flush_to_ldisc */
  3107. return;
  3108. console_lock();
  3109. set_cursor(vc);
  3110. console_unlock();
  3111. }
  3112. /*
  3113. * Allocate the console screen memory.
  3114. */
  3115. static int con_install(struct tty_driver *driver, struct tty_struct *tty)
  3116. {
  3117. unsigned int currcons = tty->index;
  3118. struct vc_data *vc;
  3119. int ret;
  3120. console_lock();
  3121. ret = vc_allocate(currcons);
  3122. if (ret)
  3123. goto unlock;
  3124. vc = vc_cons[currcons].d;
  3125. /* Still being freed */
  3126. if (vc->port.tty) {
  3127. ret = -ERESTARTSYS;
  3128. goto unlock;
  3129. }
  3130. ret = tty_port_install(&vc->port, driver, tty);
  3131. if (ret)
  3132. goto unlock;
  3133. tty->driver_data = vc;
  3134. vc->port.tty = tty;
  3135. tty_port_get(&vc->port);
  3136. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  3137. tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
  3138. tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
  3139. }
  3140. if (vc->vc_utf)
  3141. tty->termios.c_iflag |= IUTF8;
  3142. else
  3143. tty->termios.c_iflag &= ~IUTF8;
  3144. unlock:
  3145. console_unlock();
  3146. return ret;
  3147. }
  3148. static int con_open(struct tty_struct *tty, struct file *filp)
  3149. {
  3150. /* everything done in install */
  3151. return 0;
  3152. }
  3153. static void con_close(struct tty_struct *tty, struct file *filp)
  3154. {
  3155. /* Nothing to do - we defer to shutdown */
  3156. }
  3157. static void con_shutdown(struct tty_struct *tty)
  3158. {
  3159. struct vc_data *vc = tty->driver_data;
  3160. BUG_ON(vc == NULL);
  3161. console_lock();
  3162. vc->port.tty = NULL;
  3163. console_unlock();
  3164. }
  3165. static void con_cleanup(struct tty_struct *tty)
  3166. {
  3167. struct vc_data *vc = tty->driver_data;
  3168. tty_port_put(&vc->port);
  3169. }
  3170. /*
  3171. * We can't deal with anything but the N_TTY ldisc,
  3172. * because we can sleep in our write() routine.
  3173. */
  3174. static int con_ldisc_ok(struct tty_struct *tty, int ldisc)
  3175. {
  3176. return ldisc == N_TTY ? 0 : -EINVAL;
  3177. }
  3178. static int default_color = 7; /* white */
  3179. static int default_italic_color = 2; // green (ASCII)
  3180. static int default_underline_color = 3; // cyan (ASCII)
  3181. module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
  3182. module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
  3183. module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
  3184. static void vc_init(struct vc_data *vc, int do_clear)
  3185. {
  3186. int j, k ;
  3187. set_origin(vc);
  3188. vc->vc_pos = vc->vc_origin;
  3189. reset_vc(vc);
  3190. for (j=k=0; j<16; j++) {
  3191. vc->vc_palette[k++] = default_red[j] ;
  3192. vc->vc_palette[k++] = default_grn[j] ;
  3193. vc->vc_palette[k++] = default_blu[j] ;
  3194. }
  3195. vc->vc_def_color = default_color;
  3196. vc->vc_ulcolor = default_underline_color;
  3197. vc->vc_itcolor = default_italic_color;
  3198. vc->vc_halfcolor = 0x08; /* grey */
  3199. init_waitqueue_head(&vc->paste_wait);
  3200. reset_terminal(vc, do_clear);
  3201. }
  3202. /*
  3203. * This routine initializes console interrupts, and does nothing
  3204. * else. If you want the screen to clear, call tty_write with
  3205. * the appropriate escape-sequence.
  3206. */
  3207. static int __init con_init(void)
  3208. {
  3209. const char *display_desc = NULL;
  3210. struct vc_data *vc;
  3211. unsigned int currcons = 0, i;
  3212. console_lock();
  3213. if (!conswitchp)
  3214. conswitchp = &dummy_con;
  3215. display_desc = conswitchp->con_startup();
  3216. if (!display_desc) {
  3217. fg_console = 0;
  3218. console_unlock();
  3219. return 0;
  3220. }
  3221. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3222. struct con_driver *con_driver = &registered_con_driver[i];
  3223. if (con_driver->con == NULL) {
  3224. con_driver->con = conswitchp;
  3225. con_driver->desc = display_desc;
  3226. con_driver->flag = CON_DRIVER_FLAG_INIT;
  3227. con_driver->first = 0;
  3228. con_driver->last = MAX_NR_CONSOLES - 1;
  3229. break;
  3230. }
  3231. }
  3232. for (i = 0; i < MAX_NR_CONSOLES; i++)
  3233. con_driver_map[i] = conswitchp;
  3234. if (blankinterval) {
  3235. blank_state = blank_normal_wait;
  3236. mod_timer(&console_timer, jiffies + (blankinterval * HZ));
  3237. }
  3238. for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
  3239. vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
  3240. INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
  3241. tty_port_init(&vc->port);
  3242. visual_init(vc, currcons, true);
  3243. /* Assuming vc->vc_{cols,rows,screenbuf_size} are sane here. */
  3244. vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
  3245. vc_init(vc, currcons || !vc->vc_sw->con_save_screen);
  3246. }
  3247. currcons = fg_console = 0;
  3248. master_display_fg = vc = vc_cons[currcons].d;
  3249. set_origin(vc);
  3250. save_screen(vc);
  3251. gotoxy(vc, vc->state.x, vc->state.y);
  3252. csi_J(vc, CSI_J_CURSOR_TO_END);
  3253. update_screen(vc);
  3254. pr_info("Console: %s %s %dx%d\n",
  3255. vc->vc_can_do_color ? "colour" : "mono",
  3256. display_desc, vc->vc_cols, vc->vc_rows);
  3257. console_unlock();
  3258. #ifdef CONFIG_VT_CONSOLE
  3259. register_console(&vt_console_driver);
  3260. #endif
  3261. return 0;
  3262. }
  3263. console_initcall(con_init);
  3264. static const struct tty_operations con_ops = {
  3265. .install = con_install,
  3266. .open = con_open,
  3267. .close = con_close,
  3268. .write = con_write,
  3269. .write_room = con_write_room,
  3270. .put_char = con_put_char,
  3271. .flush_chars = con_flush_chars,
  3272. .ioctl = vt_ioctl,
  3273. #ifdef CONFIG_COMPAT
  3274. .compat_ioctl = vt_compat_ioctl,
  3275. #endif
  3276. .stop = con_stop,
  3277. .start = con_start,
  3278. .throttle = con_throttle,
  3279. .unthrottle = con_unthrottle,
  3280. .resize = vt_resize,
  3281. .shutdown = con_shutdown,
  3282. .cleanup = con_cleanup,
  3283. .ldisc_ok = con_ldisc_ok,
  3284. };
  3285. static struct cdev vc0_cdev;
  3286. static ssize_t show_tty_active(struct device *dev,
  3287. struct device_attribute *attr, char *buf)
  3288. {
  3289. return sprintf(buf, "tty%d\n", fg_console + 1);
  3290. }
  3291. static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
  3292. static struct attribute *vt_dev_attrs[] = {
  3293. &dev_attr_active.attr,
  3294. NULL
  3295. };
  3296. ATTRIBUTE_GROUPS(vt_dev);
  3297. int __init vty_init(const struct file_operations *console_fops)
  3298. {
  3299. cdev_init(&vc0_cdev, console_fops);
  3300. if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
  3301. register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
  3302. panic("Couldn't register /dev/tty0 driver\n");
  3303. tty0dev = device_create_with_groups(&tty_class, NULL,
  3304. MKDEV(TTY_MAJOR, 0), NULL,
  3305. vt_dev_groups, "tty0");
  3306. if (IS_ERR(tty0dev))
  3307. tty0dev = NULL;
  3308. vcs_init();
  3309. console_driver = tty_alloc_driver(MAX_NR_CONSOLES, TTY_DRIVER_REAL_RAW |
  3310. TTY_DRIVER_RESET_TERMIOS);
  3311. if (IS_ERR(console_driver))
  3312. panic("Couldn't allocate console driver\n");
  3313. console_driver->name = "tty";
  3314. console_driver->name_base = 1;
  3315. console_driver->major = TTY_MAJOR;
  3316. console_driver->minor_start = 1;
  3317. console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  3318. console_driver->init_termios = tty_std_termios;
  3319. if (default_utf8)
  3320. console_driver->init_termios.c_iflag |= IUTF8;
  3321. tty_set_operations(console_driver, &con_ops);
  3322. if (tty_register_driver(console_driver))
  3323. panic("Couldn't register console driver\n");
  3324. kbd_init();
  3325. console_map_init();
  3326. #ifdef CONFIG_MDA_CONSOLE
  3327. mda_console_init();
  3328. #endif
  3329. return 0;
  3330. }
  3331. static const struct class vtconsole_class = {
  3332. .name = "vtconsole",
  3333. };
  3334. static int do_bind_con_driver(const struct consw *csw, int first, int last,
  3335. int deflt)
  3336. {
  3337. struct module *owner = csw->owner;
  3338. const char *desc = NULL;
  3339. struct con_driver *con_driver;
  3340. int i, j = -1, k = -1, retval = -ENODEV;
  3341. if (!try_module_get(owner))
  3342. return -ENODEV;
  3343. WARN_CONSOLE_UNLOCKED();
  3344. /* check if driver is registered */
  3345. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3346. con_driver = &registered_con_driver[i];
  3347. if (con_driver->con == csw) {
  3348. desc = con_driver->desc;
  3349. retval = 0;
  3350. break;
  3351. }
  3352. }
  3353. if (retval)
  3354. goto err;
  3355. if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
  3356. csw->con_startup();
  3357. con_driver->flag |= CON_DRIVER_FLAG_INIT;
  3358. }
  3359. if (deflt) {
  3360. if (conswitchp)
  3361. module_put(conswitchp->owner);
  3362. __module_get(owner);
  3363. conswitchp = csw;
  3364. }
  3365. first = max(first, con_driver->first);
  3366. last = min(last, con_driver->last);
  3367. for (i = first; i <= last; i++) {
  3368. int old_was_color;
  3369. struct vc_data *vc = vc_cons[i].d;
  3370. if (con_driver_map[i])
  3371. module_put(con_driver_map[i]->owner);
  3372. __module_get(owner);
  3373. con_driver_map[i] = csw;
  3374. if (!vc || !vc->vc_sw)
  3375. continue;
  3376. j = i;
  3377. if (con_is_visible(vc)) {
  3378. k = i;
  3379. save_screen(vc);
  3380. }
  3381. old_was_color = vc->vc_can_do_color;
  3382. vc->vc_sw->con_deinit(vc);
  3383. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  3384. visual_init(vc, i, false);
  3385. set_origin(vc);
  3386. update_attr(vc);
  3387. /* If the console changed between mono <-> color, then
  3388. * the attributes in the screenbuf will be wrong. The
  3389. * following resets all attributes to something sane.
  3390. */
  3391. if (old_was_color != vc->vc_can_do_color)
  3392. clear_buffer_attributes(vc);
  3393. }
  3394. pr_info("Console: switching ");
  3395. if (!deflt)
  3396. pr_cont("consoles %d-%d ", first + 1, last + 1);
  3397. if (j >= 0) {
  3398. struct vc_data *vc = vc_cons[j].d;
  3399. pr_cont("to %s %s %dx%d\n",
  3400. vc->vc_can_do_color ? "colour" : "mono",
  3401. desc, vc->vc_cols, vc->vc_rows);
  3402. if (k >= 0) {
  3403. vc = vc_cons[k].d;
  3404. update_screen(vc);
  3405. }
  3406. } else {
  3407. pr_cont("to %s\n", desc);
  3408. }
  3409. retval = 0;
  3410. err:
  3411. module_put(owner);
  3412. return retval;
  3413. };
  3414. #ifdef CONFIG_VT_HW_CONSOLE_BINDING
  3415. int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
  3416. {
  3417. struct module *owner = csw->owner;
  3418. const struct consw *defcsw = NULL;
  3419. struct con_driver *con_driver = NULL, *con_back = NULL;
  3420. int i, retval = -ENODEV;
  3421. if (!try_module_get(owner))
  3422. return -ENODEV;
  3423. WARN_CONSOLE_UNLOCKED();
  3424. /* check if driver is registered and if it is unbindable */
  3425. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3426. con_driver = &registered_con_driver[i];
  3427. if (con_driver->con == csw &&
  3428. con_driver->flag & CON_DRIVER_FLAG_MODULE) {
  3429. retval = 0;
  3430. break;
  3431. }
  3432. }
  3433. if (retval)
  3434. goto err;
  3435. retval = -ENODEV;
  3436. /* check if backup driver exists */
  3437. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3438. con_back = &registered_con_driver[i];
  3439. if (con_back->con && con_back->con != csw) {
  3440. defcsw = con_back->con;
  3441. retval = 0;
  3442. break;
  3443. }
  3444. }
  3445. if (retval)
  3446. goto err;
  3447. if (!con_is_bound(csw))
  3448. goto err;
  3449. first = max(first, con_driver->first);
  3450. last = min(last, con_driver->last);
  3451. for (i = first; i <= last; i++) {
  3452. if (con_driver_map[i] == csw) {
  3453. module_put(csw->owner);
  3454. con_driver_map[i] = NULL;
  3455. }
  3456. }
  3457. if (!con_is_bound(defcsw)) {
  3458. const struct consw *defconsw = conswitchp;
  3459. defcsw->con_startup();
  3460. con_back->flag |= CON_DRIVER_FLAG_INIT;
  3461. /*
  3462. * vgacon may change the default driver to point
  3463. * to dummycon, we restore it here...
  3464. */
  3465. conswitchp = defconsw;
  3466. }
  3467. if (!con_is_bound(csw))
  3468. con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
  3469. /* ignore return value, binding should not fail */
  3470. do_bind_con_driver(defcsw, first, last, deflt);
  3471. err:
  3472. module_put(owner);
  3473. return retval;
  3474. }
  3475. EXPORT_SYMBOL_GPL(do_unbind_con_driver);
  3476. static int vt_bind(struct con_driver *con)
  3477. {
  3478. const struct consw *defcsw = NULL, *csw = NULL;
  3479. int i, more = 1, first = -1, last = -1, deflt = 0;
  3480. if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
  3481. goto err;
  3482. csw = con->con;
  3483. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3484. struct con_driver *con = &registered_con_driver[i];
  3485. if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
  3486. defcsw = con->con;
  3487. break;
  3488. }
  3489. }
  3490. if (!defcsw)
  3491. goto err;
  3492. while (more) {
  3493. more = 0;
  3494. for (i = con->first; i <= con->last; i++) {
  3495. if (con_driver_map[i] == defcsw) {
  3496. if (first == -1)
  3497. first = i;
  3498. last = i;
  3499. more = 1;
  3500. } else if (first != -1)
  3501. break;
  3502. }
  3503. if (first == 0 && last == MAX_NR_CONSOLES -1)
  3504. deflt = 1;
  3505. if (first != -1)
  3506. do_bind_con_driver(csw, first, last, deflt);
  3507. first = -1;
  3508. last = -1;
  3509. deflt = 0;
  3510. }
  3511. err:
  3512. return 0;
  3513. }
  3514. static int vt_unbind(struct con_driver *con)
  3515. {
  3516. const struct consw *csw = NULL;
  3517. int i, more = 1, first = -1, last = -1, deflt = 0;
  3518. int ret;
  3519. if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
  3520. goto err;
  3521. csw = con->con;
  3522. while (more) {
  3523. more = 0;
  3524. for (i = con->first; i <= con->last; i++) {
  3525. if (con_driver_map[i] == csw) {
  3526. if (first == -1)
  3527. first = i;
  3528. last = i;
  3529. more = 1;
  3530. } else if (first != -1)
  3531. break;
  3532. }
  3533. if (first == 0 && last == MAX_NR_CONSOLES -1)
  3534. deflt = 1;
  3535. if (first != -1) {
  3536. ret = do_unbind_con_driver(csw, first, last, deflt);
  3537. if (ret != 0)
  3538. return ret;
  3539. }
  3540. first = -1;
  3541. last = -1;
  3542. deflt = 0;
  3543. }
  3544. err:
  3545. return 0;
  3546. }
  3547. #else
  3548. static inline int vt_bind(struct con_driver *con)
  3549. {
  3550. return 0;
  3551. }
  3552. static inline int vt_unbind(struct con_driver *con)
  3553. {
  3554. return 0;
  3555. }
  3556. #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
  3557. static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
  3558. const char *buf, size_t count)
  3559. {
  3560. struct con_driver *con = dev_get_drvdata(dev);
  3561. int bind = simple_strtoul(buf, NULL, 0);
  3562. console_lock();
  3563. if (bind)
  3564. vt_bind(con);
  3565. else
  3566. vt_unbind(con);
  3567. console_unlock();
  3568. return count;
  3569. }
  3570. static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
  3571. char *buf)
  3572. {
  3573. struct con_driver *con = dev_get_drvdata(dev);
  3574. int bind;
  3575. console_lock();
  3576. bind = con_is_bound(con->con);
  3577. console_unlock();
  3578. return sysfs_emit(buf, "%i\n", bind);
  3579. }
  3580. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  3581. char *buf)
  3582. {
  3583. struct con_driver *con = dev_get_drvdata(dev);
  3584. return sysfs_emit(buf, "%s %s\n",
  3585. (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
  3586. con->desc);
  3587. }
  3588. static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
  3589. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  3590. static struct attribute *con_dev_attrs[] = {
  3591. &dev_attr_bind.attr,
  3592. &dev_attr_name.attr,
  3593. NULL
  3594. };
  3595. ATTRIBUTE_GROUPS(con_dev);
  3596. static int vtconsole_init_device(struct con_driver *con)
  3597. {
  3598. con->flag |= CON_DRIVER_FLAG_ATTR;
  3599. return 0;
  3600. }
  3601. static void vtconsole_deinit_device(struct con_driver *con)
  3602. {
  3603. con->flag &= ~CON_DRIVER_FLAG_ATTR;
  3604. }
  3605. /**
  3606. * con_is_bound - checks if driver is bound to the console
  3607. * @csw: console driver
  3608. *
  3609. * RETURNS: zero if unbound, nonzero if bound
  3610. *
  3611. * Drivers can call this and if zero, they should release
  3612. * all resources allocated on &consw.con_startup()
  3613. */
  3614. int con_is_bound(const struct consw *csw)
  3615. {
  3616. int i, bound = 0;
  3617. WARN_CONSOLE_UNLOCKED();
  3618. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  3619. if (con_driver_map[i] == csw) {
  3620. bound = 1;
  3621. break;
  3622. }
  3623. }
  3624. return bound;
  3625. }
  3626. EXPORT_SYMBOL(con_is_bound);
  3627. /**
  3628. * con_is_visible - checks whether the current console is visible
  3629. * @vc: virtual console
  3630. *
  3631. * RETURNS: zero if not visible, nonzero if visible
  3632. */
  3633. bool con_is_visible(const struct vc_data *vc)
  3634. {
  3635. WARN_CONSOLE_UNLOCKED();
  3636. return *vc->vc_display_fg == vc;
  3637. }
  3638. EXPORT_SYMBOL(con_is_visible);
  3639. /**
  3640. * con_debug_enter - prepare the console for the kernel debugger
  3641. * @vc: virtual console
  3642. *
  3643. * Called when the console is taken over by the kernel debugger, this
  3644. * function needs to save the current console state, then put the console
  3645. * into a state suitable for the kernel debugger.
  3646. */
  3647. void con_debug_enter(struct vc_data *vc)
  3648. {
  3649. saved_fg_console = fg_console;
  3650. saved_last_console = last_console;
  3651. saved_want_console = want_console;
  3652. saved_vc_mode = vc->vc_mode;
  3653. saved_console_blanked = console_blanked;
  3654. vc->vc_mode = KD_TEXT;
  3655. console_blanked = 0;
  3656. if (vc->vc_sw->con_debug_enter)
  3657. vc->vc_sw->con_debug_enter(vc);
  3658. #ifdef CONFIG_KGDB_KDB
  3659. /* Set the initial LINES variable if it is not already set */
  3660. if (vc->vc_rows < 999) {
  3661. int linecount;
  3662. char lns[4];
  3663. const char *setargs[3] = {
  3664. "set",
  3665. "LINES",
  3666. lns,
  3667. };
  3668. if (kdbgetintenv(setargs[0], &linecount)) {
  3669. snprintf(lns, 4, "%i", vc->vc_rows);
  3670. kdb_set(2, setargs);
  3671. }
  3672. }
  3673. if (vc->vc_cols < 999) {
  3674. int colcount;
  3675. char cols[4];
  3676. const char *setargs[3] = {
  3677. "set",
  3678. "COLUMNS",
  3679. cols,
  3680. };
  3681. if (kdbgetintenv(setargs[0], &colcount)) {
  3682. snprintf(cols, 4, "%i", vc->vc_cols);
  3683. kdb_set(2, setargs);
  3684. }
  3685. }
  3686. #endif /* CONFIG_KGDB_KDB */
  3687. }
  3688. EXPORT_SYMBOL_GPL(con_debug_enter);
  3689. /**
  3690. * con_debug_leave - restore console state
  3691. *
  3692. * Restore the console state to what it was before the kernel debugger
  3693. * was invoked.
  3694. */
  3695. void con_debug_leave(void)
  3696. {
  3697. struct vc_data *vc;
  3698. fg_console = saved_fg_console;
  3699. last_console = saved_last_console;
  3700. want_console = saved_want_console;
  3701. console_blanked = saved_console_blanked;
  3702. vc_cons[fg_console].d->vc_mode = saved_vc_mode;
  3703. vc = vc_cons[fg_console].d;
  3704. if (vc->vc_sw->con_debug_leave)
  3705. vc->vc_sw->con_debug_leave(vc);
  3706. }
  3707. EXPORT_SYMBOL_GPL(con_debug_leave);
  3708. static int do_register_con_driver(const struct consw *csw, int first, int last)
  3709. {
  3710. struct module *owner = csw->owner;
  3711. struct con_driver *con_driver;
  3712. const char *desc;
  3713. int i, retval;
  3714. WARN_CONSOLE_UNLOCKED();
  3715. if (!try_module_get(owner))
  3716. return -ENODEV;
  3717. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3718. con_driver = &registered_con_driver[i];
  3719. /* already registered */
  3720. if (con_driver->con == csw) {
  3721. retval = -EBUSY;
  3722. goto err;
  3723. }
  3724. }
  3725. desc = csw->con_startup();
  3726. if (!desc) {
  3727. retval = -ENODEV;
  3728. goto err;
  3729. }
  3730. retval = -EINVAL;
  3731. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3732. con_driver = &registered_con_driver[i];
  3733. if (con_driver->con == NULL &&
  3734. !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
  3735. con_driver->con = csw;
  3736. con_driver->desc = desc;
  3737. con_driver->node = i;
  3738. con_driver->flag = CON_DRIVER_FLAG_MODULE |
  3739. CON_DRIVER_FLAG_INIT;
  3740. con_driver->first = first;
  3741. con_driver->last = last;
  3742. retval = 0;
  3743. break;
  3744. }
  3745. }
  3746. if (retval)
  3747. goto err;
  3748. con_driver->dev =
  3749. device_create_with_groups(&vtconsole_class, NULL,
  3750. MKDEV(0, con_driver->node),
  3751. con_driver, con_dev_groups,
  3752. "vtcon%i", con_driver->node);
  3753. if (IS_ERR(con_driver->dev)) {
  3754. pr_warn("Unable to create device for %s; errno = %ld\n",
  3755. con_driver->desc, PTR_ERR(con_driver->dev));
  3756. con_driver->dev = NULL;
  3757. } else {
  3758. vtconsole_init_device(con_driver);
  3759. }
  3760. err:
  3761. module_put(owner);
  3762. return retval;
  3763. }
  3764. /**
  3765. * do_unregister_con_driver - unregister console driver from console layer
  3766. * @csw: console driver
  3767. *
  3768. * DESCRIPTION: All drivers that registers to the console layer must
  3769. * call this function upon exit, or if the console driver is in a state
  3770. * where it won't be able to handle console services, such as the
  3771. * framebuffer console without loaded framebuffer drivers.
  3772. *
  3773. * The driver must unbind first prior to unregistration.
  3774. */
  3775. int do_unregister_con_driver(const struct consw *csw)
  3776. {
  3777. int i;
  3778. /* cannot unregister a bound driver */
  3779. if (con_is_bound(csw))
  3780. return -EBUSY;
  3781. if (csw == conswitchp)
  3782. return -EINVAL;
  3783. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3784. struct con_driver *con_driver = &registered_con_driver[i];
  3785. if (con_driver->con == csw) {
  3786. /*
  3787. * Defer the removal of the sysfs entries since that
  3788. * will acquire the kernfs s_active lock and we can't
  3789. * acquire this lock while holding the console lock:
  3790. * the unbind sysfs entry imposes already the opposite
  3791. * order. Reset con already here to prevent any later
  3792. * lookup to succeed and mark this slot as zombie, so
  3793. * it won't get reused until we complete the removal
  3794. * in the deferred work.
  3795. */
  3796. con_driver->con = NULL;
  3797. con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
  3798. schedule_work(&con_driver_unregister_work);
  3799. return 0;
  3800. }
  3801. }
  3802. return -ENODEV;
  3803. }
  3804. EXPORT_SYMBOL_GPL(do_unregister_con_driver);
  3805. static void con_driver_unregister_callback(struct work_struct *ignored)
  3806. {
  3807. int i;
  3808. console_lock();
  3809. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3810. struct con_driver *con_driver = &registered_con_driver[i];
  3811. if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
  3812. continue;
  3813. console_unlock();
  3814. vtconsole_deinit_device(con_driver);
  3815. device_destroy(&vtconsole_class, MKDEV(0, con_driver->node));
  3816. console_lock();
  3817. if (WARN_ON_ONCE(con_driver->con))
  3818. con_driver->con = NULL;
  3819. con_driver->desc = NULL;
  3820. con_driver->dev = NULL;
  3821. con_driver->node = 0;
  3822. WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
  3823. con_driver->flag = 0;
  3824. con_driver->first = 0;
  3825. con_driver->last = 0;
  3826. }
  3827. console_unlock();
  3828. }
  3829. /*
  3830. * If we support more console drivers, this function is used
  3831. * when a driver wants to take over some existing consoles
  3832. * and become default driver for newly opened ones.
  3833. *
  3834. * do_take_over_console is basically a register followed by bind
  3835. */
  3836. int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
  3837. {
  3838. int err;
  3839. err = do_register_con_driver(csw, first, last);
  3840. /*
  3841. * If we get an busy error we still want to bind the console driver
  3842. * and return success, as we may have unbound the console driver
  3843. * but not unregistered it.
  3844. */
  3845. if (err == -EBUSY)
  3846. err = 0;
  3847. if (!err)
  3848. do_bind_con_driver(csw, first, last, deflt);
  3849. return err;
  3850. }
  3851. EXPORT_SYMBOL_GPL(do_take_over_console);
  3852. /*
  3853. * give_up_console is a wrapper to unregister_con_driver. It will only
  3854. * work if driver is fully unbound.
  3855. */
  3856. void give_up_console(const struct consw *csw)
  3857. {
  3858. console_lock();
  3859. do_unregister_con_driver(csw);
  3860. console_unlock();
  3861. }
  3862. EXPORT_SYMBOL(give_up_console);
  3863. static int __init vtconsole_class_init(void)
  3864. {
  3865. int i;
  3866. i = class_register(&vtconsole_class);
  3867. if (i)
  3868. pr_warn("Unable to create vt console class; errno = %d\n", i);
  3869. /* Add system drivers to sysfs */
  3870. for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
  3871. struct con_driver *con = &registered_con_driver[i];
  3872. if (con->con && !con->dev) {
  3873. con->dev =
  3874. device_create_with_groups(&vtconsole_class, NULL,
  3875. MKDEV(0, con->node),
  3876. con, con_dev_groups,
  3877. "vtcon%i", con->node);
  3878. if (IS_ERR(con->dev)) {
  3879. pr_warn("Unable to create device for %s; errno = %ld\n",
  3880. con->desc, PTR_ERR(con->dev));
  3881. con->dev = NULL;
  3882. } else {
  3883. vtconsole_init_device(con);
  3884. }
  3885. }
  3886. }
  3887. return 0;
  3888. }
  3889. postcore_initcall(vtconsole_class_init);
  3890. /*
  3891. * Screen blanking
  3892. */
  3893. static int set_vesa_blanking(u8 __user *mode_user)
  3894. {
  3895. u8 mode;
  3896. if (get_user(mode, mode_user))
  3897. return -EFAULT;
  3898. console_lock();
  3899. vesa_blank_mode = (mode <= VESA_BLANK_MAX) ? mode : VESA_NO_BLANKING;
  3900. console_unlock();
  3901. return 0;
  3902. }
  3903. void do_blank_screen(int entering_gfx)
  3904. {
  3905. struct vc_data *vc = vc_cons[fg_console].d;
  3906. int i;
  3907. might_sleep();
  3908. WARN_CONSOLE_UNLOCKED();
  3909. if (console_blanked) {
  3910. if (blank_state == blank_vesa_wait) {
  3911. blank_state = blank_off;
  3912. vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
  3913. }
  3914. return;
  3915. }
  3916. /* entering graphics mode? */
  3917. if (entering_gfx) {
  3918. hide_cursor(vc);
  3919. save_screen(vc);
  3920. vc->vc_sw->con_blank(vc, VESA_VSYNC_SUSPEND, 1);
  3921. console_blanked = fg_console + 1;
  3922. blank_state = blank_off;
  3923. set_origin(vc);
  3924. return;
  3925. }
  3926. blank_state = blank_off;
  3927. /* don't blank graphics */
  3928. if (vc->vc_mode != KD_TEXT) {
  3929. console_blanked = fg_console + 1;
  3930. return;
  3931. }
  3932. hide_cursor(vc);
  3933. del_timer_sync(&console_timer);
  3934. blank_timer_expired = 0;
  3935. save_screen(vc);
  3936. /* In case we need to reset origin, blanking hook returns 1 */
  3937. i = vc->vc_sw->con_blank(vc, vesa_off_interval ? VESA_VSYNC_SUSPEND :
  3938. (vesa_blank_mode + 1), 0);
  3939. console_blanked = fg_console + 1;
  3940. if (i)
  3941. set_origin(vc);
  3942. if (console_blank_hook && console_blank_hook(1))
  3943. return;
  3944. if (vesa_off_interval && vesa_blank_mode) {
  3945. blank_state = blank_vesa_wait;
  3946. mod_timer(&console_timer, jiffies + vesa_off_interval);
  3947. }
  3948. vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
  3949. }
  3950. EXPORT_SYMBOL(do_blank_screen);
  3951. /*
  3952. * Called by timer as well as from vt_console_driver
  3953. */
  3954. void do_unblank_screen(int leaving_gfx)
  3955. {
  3956. struct vc_data *vc;
  3957. /* This should now always be called from a "sane" (read: can schedule)
  3958. * context for the sake of the low level drivers, except in the special
  3959. * case of oops_in_progress
  3960. */
  3961. if (!oops_in_progress)
  3962. might_sleep();
  3963. WARN_CONSOLE_UNLOCKED();
  3964. ignore_poke = 0;
  3965. if (!console_blanked)
  3966. return;
  3967. if (!vc_cons_allocated(fg_console)) {
  3968. /* impossible */
  3969. pr_warn("unblank_screen: tty %d not allocated ??\n",
  3970. fg_console + 1);
  3971. return;
  3972. }
  3973. vc = vc_cons[fg_console].d;
  3974. if (vc->vc_mode != KD_TEXT)
  3975. return; /* but leave console_blanked != 0 */
  3976. if (blankinterval) {
  3977. mod_timer(&console_timer, jiffies + (blankinterval * HZ));
  3978. blank_state = blank_normal_wait;
  3979. }
  3980. console_blanked = 0;
  3981. if (vc->vc_sw->con_blank(vc, VESA_NO_BLANKING, leaving_gfx))
  3982. /* Low-level driver cannot restore -> do it ourselves */
  3983. update_screen(vc);
  3984. if (console_blank_hook)
  3985. console_blank_hook(0);
  3986. set_palette(vc);
  3987. set_cursor(vc);
  3988. vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
  3989. notify_update(vc);
  3990. }
  3991. EXPORT_SYMBOL(do_unblank_screen);
  3992. /*
  3993. * This is called by the outside world to cause a forced unblank, mostly for
  3994. * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
  3995. * call it with 1 as an argument and so force a mode restore... that may kill
  3996. * X or at least garbage the screen but would also make the Oops visible...
  3997. */
  3998. static void unblank_screen(void)
  3999. {
  4000. do_unblank_screen(0);
  4001. }
  4002. /*
  4003. * We defer the timer blanking to work queue so it can take the console mutex
  4004. * (console operations can still happen at irq time, but only from printk which
  4005. * has the console mutex. Not perfect yet, but better than no locking
  4006. */
  4007. static void blank_screen_t(struct timer_list *unused)
  4008. {
  4009. blank_timer_expired = 1;
  4010. schedule_work(&console_work);
  4011. }
  4012. void poke_blanked_console(void)
  4013. {
  4014. WARN_CONSOLE_UNLOCKED();
  4015. /* Add this so we quickly catch whoever might call us in a non
  4016. * safe context. Nowadays, unblank_screen() isn't to be called in
  4017. * atomic contexts and is allowed to schedule (with the special case
  4018. * of oops_in_progress, but that isn't of any concern for this
  4019. * function. --BenH.
  4020. */
  4021. might_sleep();
  4022. /* This isn't perfectly race free, but a race here would be mostly harmless,
  4023. * at worst, we'll do a spurious blank and it's unlikely
  4024. */
  4025. del_timer(&console_timer);
  4026. blank_timer_expired = 0;
  4027. if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
  4028. return;
  4029. if (console_blanked)
  4030. unblank_screen();
  4031. else if (blankinterval) {
  4032. mod_timer(&console_timer, jiffies + (blankinterval * HZ));
  4033. blank_state = blank_normal_wait;
  4034. }
  4035. }
  4036. /*
  4037. * Palettes
  4038. */
  4039. static void set_palette(struct vc_data *vc)
  4040. {
  4041. WARN_CONSOLE_UNLOCKED();
  4042. if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
  4043. vc->vc_sw->con_set_palette(vc, color_table);
  4044. }
  4045. /*
  4046. * Load palette into the DAC registers. arg points to a colour
  4047. * map, 3 bytes per colour, 16 colours, range from 0 to 255.
  4048. */
  4049. int con_set_cmap(unsigned char __user *arg)
  4050. {
  4051. int i, j, k;
  4052. unsigned char colormap[3*16];
  4053. if (copy_from_user(colormap, arg, sizeof(colormap)))
  4054. return -EFAULT;
  4055. console_lock();
  4056. for (i = k = 0; i < 16; i++) {
  4057. default_red[i] = colormap[k++];
  4058. default_grn[i] = colormap[k++];
  4059. default_blu[i] = colormap[k++];
  4060. }
  4061. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  4062. if (!vc_cons_allocated(i))
  4063. continue;
  4064. for (j = k = 0; j < 16; j++) {
  4065. vc_cons[i].d->vc_palette[k++] = default_red[j];
  4066. vc_cons[i].d->vc_palette[k++] = default_grn[j];
  4067. vc_cons[i].d->vc_palette[k++] = default_blu[j];
  4068. }
  4069. set_palette(vc_cons[i].d);
  4070. }
  4071. console_unlock();
  4072. return 0;
  4073. }
  4074. int con_get_cmap(unsigned char __user *arg)
  4075. {
  4076. int i, k;
  4077. unsigned char colormap[3*16];
  4078. console_lock();
  4079. for (i = k = 0; i < 16; i++) {
  4080. colormap[k++] = default_red[i];
  4081. colormap[k++] = default_grn[i];
  4082. colormap[k++] = default_blu[i];
  4083. }
  4084. console_unlock();
  4085. if (copy_to_user(arg, colormap, sizeof(colormap)))
  4086. return -EFAULT;
  4087. return 0;
  4088. }
  4089. void reset_palette(struct vc_data *vc)
  4090. {
  4091. int j, k;
  4092. for (j=k=0; j<16; j++) {
  4093. vc->vc_palette[k++] = default_red[j];
  4094. vc->vc_palette[k++] = default_grn[j];
  4095. vc->vc_palette[k++] = default_blu[j];
  4096. }
  4097. set_palette(vc);
  4098. }
  4099. /*
  4100. * Font switching
  4101. *
  4102. * Currently we only support fonts up to 128 pixels wide, at a maximum height
  4103. * of 128 pixels. Userspace fontdata may have to be stored with 32 bytes
  4104. * (shorts/ints, depending on width) reserved for each character which is
  4105. * kinda wasty, but this is done in order to maintain compatibility with the
  4106. * EGA/VGA fonts. It is up to the actual low-level console-driver convert data
  4107. * into its favorite format (maybe we should add a `fontoffset' field to the
  4108. * `display' structure so we won't have to convert the fontdata all the time.
  4109. * /Jes
  4110. */
  4111. #define max_font_width 64
  4112. #define max_font_height 128
  4113. #define max_font_glyphs 512
  4114. #define max_font_size (max_font_glyphs*max_font_width*max_font_height)
  4115. static int con_font_get(struct vc_data *vc, struct console_font_op *op)
  4116. {
  4117. struct console_font font;
  4118. int rc = -EINVAL;
  4119. int c;
  4120. unsigned int vpitch = op->op == KD_FONT_OP_GET_TALL ? op->height : 32;
  4121. if (vpitch > max_font_height)
  4122. return -EINVAL;
  4123. if (op->data) {
  4124. font.data = kvzalloc(max_font_size, GFP_KERNEL);
  4125. if (!font.data)
  4126. return -ENOMEM;
  4127. } else
  4128. font.data = NULL;
  4129. console_lock();
  4130. if (vc->vc_mode != KD_TEXT)
  4131. rc = -EINVAL;
  4132. else if (vc->vc_sw->con_font_get)
  4133. rc = vc->vc_sw->con_font_get(vc, &font, vpitch);
  4134. else
  4135. rc = -ENOSYS;
  4136. console_unlock();
  4137. if (rc)
  4138. goto out;
  4139. c = (font.width+7)/8 * vpitch * font.charcount;
  4140. if (op->data && font.charcount > op->charcount)
  4141. rc = -ENOSPC;
  4142. if (font.width > op->width || font.height > op->height)
  4143. rc = -ENOSPC;
  4144. if (rc)
  4145. goto out;
  4146. op->height = font.height;
  4147. op->width = font.width;
  4148. op->charcount = font.charcount;
  4149. if (op->data && copy_to_user(op->data, font.data, c))
  4150. rc = -EFAULT;
  4151. out:
  4152. kvfree(font.data);
  4153. return rc;
  4154. }
  4155. static int con_font_set(struct vc_data *vc, const struct console_font_op *op)
  4156. {
  4157. struct console_font font;
  4158. int rc = -EINVAL;
  4159. int size;
  4160. unsigned int vpitch = op->op == KD_FONT_OP_SET_TALL ? op->height : 32;
  4161. if (vc->vc_mode != KD_TEXT)
  4162. return -EINVAL;
  4163. if (!op->data)
  4164. return -EINVAL;
  4165. if (op->charcount > max_font_glyphs)
  4166. return -EINVAL;
  4167. if (op->width <= 0 || op->width > max_font_width || !op->height ||
  4168. op->height > max_font_height)
  4169. return -EINVAL;
  4170. if (vpitch < op->height)
  4171. return -EINVAL;
  4172. size = (op->width+7)/8 * vpitch * op->charcount;
  4173. if (size > max_font_size)
  4174. return -ENOSPC;
  4175. font.data = memdup_user(op->data, size);
  4176. if (IS_ERR(font.data))
  4177. return PTR_ERR(font.data);
  4178. font.charcount = op->charcount;
  4179. font.width = op->width;
  4180. font.height = op->height;
  4181. console_lock();
  4182. if (vc->vc_mode != KD_TEXT)
  4183. rc = -EINVAL;
  4184. else if (vc->vc_sw->con_font_set) {
  4185. if (vc_is_sel(vc))
  4186. clear_selection();
  4187. rc = vc->vc_sw->con_font_set(vc, &font, vpitch, op->flags);
  4188. } else
  4189. rc = -ENOSYS;
  4190. console_unlock();
  4191. kfree(font.data);
  4192. return rc;
  4193. }
  4194. static int con_font_default(struct vc_data *vc, struct console_font_op *op)
  4195. {
  4196. struct console_font font = {.width = op->width, .height = op->height};
  4197. char name[MAX_FONT_NAME];
  4198. char *s = name;
  4199. int rc;
  4200. if (!op->data)
  4201. s = NULL;
  4202. else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
  4203. return -EFAULT;
  4204. else
  4205. name[MAX_FONT_NAME - 1] = 0;
  4206. console_lock();
  4207. if (vc->vc_mode != KD_TEXT) {
  4208. console_unlock();
  4209. return -EINVAL;
  4210. }
  4211. if (vc->vc_sw->con_font_default) {
  4212. if (vc_is_sel(vc))
  4213. clear_selection();
  4214. rc = vc->vc_sw->con_font_default(vc, &font, s);
  4215. } else
  4216. rc = -ENOSYS;
  4217. console_unlock();
  4218. if (!rc) {
  4219. op->width = font.width;
  4220. op->height = font.height;
  4221. }
  4222. return rc;
  4223. }
  4224. int con_font_op(struct vc_data *vc, struct console_font_op *op)
  4225. {
  4226. switch (op->op) {
  4227. case KD_FONT_OP_SET:
  4228. case KD_FONT_OP_SET_TALL:
  4229. return con_font_set(vc, op);
  4230. case KD_FONT_OP_GET:
  4231. case KD_FONT_OP_GET_TALL:
  4232. return con_font_get(vc, op);
  4233. case KD_FONT_OP_SET_DEFAULT:
  4234. return con_font_default(vc, op);
  4235. case KD_FONT_OP_COPY:
  4236. /* was buggy and never really used */
  4237. return -EINVAL;
  4238. }
  4239. return -ENOSYS;
  4240. }
  4241. /*
  4242. * Interface exported to selection and vcs.
  4243. */
  4244. /* used by selection */
  4245. u16 screen_glyph(const struct vc_data *vc, int offset)
  4246. {
  4247. u16 w = scr_readw(screenpos(vc, offset, true));
  4248. u16 c = w & 0xff;
  4249. if (w & vc->vc_hi_font_mask)
  4250. c |= 0x100;
  4251. return c;
  4252. }
  4253. EXPORT_SYMBOL_GPL(screen_glyph);
  4254. u32 screen_glyph_unicode(const struct vc_data *vc, int n)
  4255. {
  4256. u32 **uni_lines = vc->vc_uni_lines;
  4257. if (uni_lines)
  4258. return uni_lines[n / vc->vc_cols][n % vc->vc_cols];
  4259. return inverse_translate(vc, screen_glyph(vc, n * 2), true);
  4260. }
  4261. EXPORT_SYMBOL_GPL(screen_glyph_unicode);
  4262. /* used by vcs - note the word offset */
  4263. unsigned short *screen_pos(const struct vc_data *vc, int w_offset, bool viewed)
  4264. {
  4265. return screenpos(vc, 2 * w_offset, viewed);
  4266. }
  4267. EXPORT_SYMBOL_GPL(screen_pos);
  4268. void getconsxy(const struct vc_data *vc, unsigned char xy[static 2])
  4269. {
  4270. /* clamp values if they don't fit */
  4271. xy[0] = min(vc->state.x, 0xFFu);
  4272. xy[1] = min(vc->state.y, 0xFFu);
  4273. }
  4274. void putconsxy(struct vc_data *vc, unsigned char xy[static const 2])
  4275. {
  4276. hide_cursor(vc);
  4277. gotoxy(vc, xy[0], xy[1]);
  4278. set_cursor(vc);
  4279. }
  4280. u16 vcs_scr_readw(const struct vc_data *vc, const u16 *org)
  4281. {
  4282. if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
  4283. return softcursor_original;
  4284. return scr_readw(org);
  4285. }
  4286. void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
  4287. {
  4288. scr_writew(val, org);
  4289. if ((unsigned long)org == vc->vc_pos) {
  4290. softcursor_original = -1;
  4291. add_softcursor(vc);
  4292. }
  4293. }
  4294. void vcs_scr_updated(struct vc_data *vc)
  4295. {
  4296. notify_update(vc);
  4297. }