vt.c 101 KB

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