vt.c 120 KB

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