vt.c 98 KB

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