vt.c 103 KB

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