vt.c 102 KB

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