vt.c 103 KB

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