vt.c 103 KB

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