vt.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280
  1. /*
  2. * linux/drivers/char/vt.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Hopefully this will be a rather complete VT102 implementation.
  8. *
  9. * Beeping thanks to John T Kohl.
  10. *
  11. * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
  12. * Chars, and VT100 enhancements by Peter MacDonald.
  13. *
  14. * Copy and paste function by Andrew Haylett,
  15. * some enhancements by Alessandro Rubini.
  16. *
  17. * Code to check for different video-cards mostly by Galen Hunt,
  18. * <g-hunt@ee.utah.edu>
  19. *
  20. * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
  21. * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
  22. *
  23. * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
  24. * Resizing of consoles, aeb, 940926
  25. *
  26. * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
  27. * <poe@daimi.aau.dk>
  28. *
  29. * User-defined bell sound, new setterm control sequences and printk
  30. * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
  31. *
  32. * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
  33. *
  34. * Merge with the abstract console driver by Geert Uytterhoeven
  35. * <geert@linux-m68k.org>, Jan 1997.
  36. *
  37. * Original m68k console driver modifications by
  38. *
  39. * - Arno Griffioen <arno@usn.nl>
  40. * - David Carter <carter@cs.bris.ac.uk>
  41. *
  42. * The abstract console driver provides a generic interface for a text
  43. * console. It supports VGA text mode, frame buffer based graphical consoles
  44. * and special graphics processors that are only accessible through some
  45. * registers (e.g. a TMS340x0 GSP).
  46. *
  47. * The interface to the hardware is specified using a special structure
  48. * (struct consw) which contains function pointers to console operations
  49. * (see <linux/console.h> for more information).
  50. *
  51. * Support for changeable cursor shape
  52. * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
  53. *
  54. * Ported to i386 and con_scrolldelta fixed
  55. * by Emmanuel Marty <core@ggi-project.org>, April 1998
  56. *
  57. * Resurrected character buffers in videoram plus lots of other trickery
  58. * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
  59. *
  60. * Removed old-style timers, introduced console_timer, made timer
  61. * deletion SMP-safe. 17Jun00, Andrew Morton <andrewm@uow.edu.au>
  62. *
  63. * Removed console_lock, enabled interrupts across all console operations
  64. * 13 March 2001, Andrew Morton
  65. */
  66. #include <linux/module.h>
  67. #include <linux/types.h>
  68. #include <linux/sched.h>
  69. #include <linux/tty.h>
  70. #include <linux/tty_flip.h>
  71. #include <linux/kernel.h>
  72. #include <linux/string.h>
  73. #include <linux/errno.h>
  74. #include <linux/kd.h>
  75. #include <linux/slab.h>
  76. #include <linux/major.h>
  77. #include <linux/mm.h>
  78. #include <linux/console.h>
  79. #include <linux/init.h>
  80. #include <linux/devfs_fs_kernel.h>
  81. #include <linux/vt_kern.h>
  82. #include <linux/selection.h>
  83. #include <linux/tiocl.h>
  84. #include <linux/kbd_kern.h>
  85. #include <linux/consolemap.h>
  86. #include <linux/timer.h>
  87. #include <linux/interrupt.h>
  88. #include <linux/config.h>
  89. #include <linux/workqueue.h>
  90. #include <linux/bootmem.h>
  91. #include <linux/pm.h>
  92. #include <linux/font.h>
  93. #include <linux/bitops.h>
  94. #include <asm/io.h>
  95. #include <asm/system.h>
  96. #include <asm/uaccess.h>
  97. const struct consw *conswitchp;
  98. /* A bitmap for codes <32. A bit of 1 indicates that the code
  99. * corresponding to that bit number invokes some special action
  100. * (such as cursor movement) and should not be displayed as a
  101. * glyph unless the disp_ctrl mode is explicitly enabled.
  102. */
  103. #define CTRL_ACTION 0x0d00ff81
  104. #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
  105. /*
  106. * Here is the default bell parameters: 750HZ, 1/8th of a second
  107. */
  108. #define DEFAULT_BELL_PITCH 750
  109. #define DEFAULT_BELL_DURATION (HZ/8)
  110. extern void vcs_make_devfs(struct tty_struct *tty);
  111. extern void vcs_remove_devfs(struct tty_struct *tty);
  112. extern void console_map_init(void);
  113. #ifdef CONFIG_PROM_CONSOLE
  114. extern void prom_con_init(void);
  115. #endif
  116. #ifdef CONFIG_MDA_CONSOLE
  117. extern int mda_console_init(void);
  118. #endif
  119. struct vc vc_cons [MAX_NR_CONSOLES];
  120. #ifndef VT_SINGLE_DRIVER
  121. static const struct consw *con_driver_map[MAX_NR_CONSOLES];
  122. #endif
  123. static int con_open(struct tty_struct *, struct file *);
  124. static void vc_init(struct vc_data *vc, unsigned int rows,
  125. unsigned int cols, int do_clear);
  126. static void gotoxy(struct vc_data *vc, int new_x, int new_y);
  127. static void save_cur(struct vc_data *vc);
  128. static void reset_terminal(struct vc_data *vc, int do_clear);
  129. static void con_flush_chars(struct tty_struct *tty);
  130. static void set_vesa_blanking(char __user *p);
  131. static void set_cursor(struct vc_data *vc);
  132. static void hide_cursor(struct vc_data *vc);
  133. static void console_callback(void *ignored);
  134. static void blank_screen_t(unsigned long dummy);
  135. static void set_palette(struct vc_data *vc);
  136. static int printable; /* Is console ready for printing? */
  137. /*
  138. * ignore_poke: don't unblank the screen when things are typed. This is
  139. * mainly for the privacy of braille terminal users.
  140. */
  141. static int ignore_poke;
  142. int do_poke_blanked_console;
  143. int console_blanked;
  144. static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
  145. static int blankinterval = 10*60*HZ;
  146. static int vesa_off_interval;
  147. static DECLARE_WORK(console_work, console_callback, NULL);
  148. /*
  149. * fg_console is the current virtual console,
  150. * last_console is the last used one,
  151. * want_console is the console we want to switch to,
  152. * kmsg_redirect is the console for kernel messages,
  153. */
  154. int fg_console;
  155. int last_console;
  156. int want_console = -1;
  157. int kmsg_redirect;
  158. /*
  159. * For each existing display, we have a pointer to console currently visible
  160. * on that display, allowing consoles other than fg_console to be refreshed
  161. * appropriately. Unless the low-level driver supplies its own display_fg
  162. * variable, we use this one for the "master display".
  163. */
  164. static struct vc_data *master_display_fg;
  165. /*
  166. * Unfortunately, we need to delay tty echo when we're currently writing to the
  167. * console since the code is (and always was) not re-entrant, so we schedule
  168. * all flip requests to process context with schedule-task() and run it from
  169. * console_callback().
  170. */
  171. /*
  172. * For the same reason, we defer scrollback to the console callback.
  173. */
  174. static int scrollback_delta;
  175. /*
  176. * Hook so that the power management routines can (un)blank
  177. * the console on our behalf.
  178. */
  179. int (*console_blank_hook)(int);
  180. static struct timer_list console_timer;
  181. static int blank_state;
  182. static int blank_timer_expired;
  183. enum {
  184. blank_off = 0,
  185. blank_normal_wait,
  186. blank_vesa_wait,
  187. };
  188. /*
  189. * Low-Level Functions
  190. */
  191. #define IS_FG(vc) ((vc)->vc_num == fg_console)
  192. #ifdef VT_BUF_VRAM_ONLY
  193. #define DO_UPDATE(vc) 0
  194. #else
  195. #define DO_UPDATE(vc) CON_IS_VISIBLE(vc)
  196. #endif
  197. static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
  198. {
  199. unsigned short *p;
  200. if (!viewed)
  201. p = (unsigned short *)(vc->vc_origin + offset);
  202. else if (!vc->vc_sw->con_screen_pos)
  203. p = (unsigned short *)(vc->vc_visible_origin + offset);
  204. else
  205. p = vc->vc_sw->con_screen_pos(vc, offset);
  206. return p;
  207. }
  208. static inline void scrolldelta(int lines)
  209. {
  210. scrollback_delta += lines;
  211. schedule_console_callback();
  212. }
  213. void schedule_console_callback(void)
  214. {
  215. schedule_work(&console_work);
  216. }
  217. static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
  218. {
  219. unsigned short *d, *s;
  220. if (t+nr >= b)
  221. nr = b - t - 1;
  222. if (b > vc->vc_rows || t >= b || nr < 1)
  223. return;
  224. if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
  225. return;
  226. d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
  227. s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
  228. scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
  229. scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
  230. vc->vc_size_row * nr);
  231. }
  232. static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
  233. {
  234. unsigned short *s;
  235. unsigned int step;
  236. if (t+nr >= b)
  237. nr = b - t - 1;
  238. if (b > vc->vc_rows || t >= b || nr < 1)
  239. return;
  240. if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
  241. return;
  242. s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
  243. step = vc->vc_cols * nr;
  244. scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
  245. scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
  246. }
  247. static void do_update_region(struct vc_data *vc, unsigned long start, int count)
  248. {
  249. #ifndef VT_BUF_VRAM_ONLY
  250. unsigned int xx, yy, offset;
  251. u16 *p;
  252. p = (u16 *) start;
  253. if (!vc->vc_sw->con_getxy) {
  254. offset = (start - vc->vc_origin) / 2;
  255. xx = offset % vc->vc_cols;
  256. yy = offset / vc->vc_cols;
  257. } else {
  258. int nxx, nyy;
  259. start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
  260. xx = nxx; yy = nyy;
  261. }
  262. for(;;) {
  263. u16 attrib = scr_readw(p) & 0xff00;
  264. int startx = xx;
  265. u16 *q = p;
  266. while (xx < vc->vc_cols && count) {
  267. if (attrib != (scr_readw(p) & 0xff00)) {
  268. if (p > q)
  269. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  270. startx = xx;
  271. q = p;
  272. attrib = scr_readw(p) & 0xff00;
  273. }
  274. p++;
  275. xx++;
  276. count--;
  277. }
  278. if (p > q)
  279. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  280. if (!count)
  281. break;
  282. xx = 0;
  283. yy++;
  284. if (vc->vc_sw->con_getxy) {
  285. p = (u16 *)start;
  286. start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
  287. }
  288. }
  289. #endif
  290. }
  291. void update_region(struct vc_data *vc, unsigned long start, int count)
  292. {
  293. WARN_CONSOLE_UNLOCKED();
  294. if (DO_UPDATE(vc)) {
  295. hide_cursor(vc);
  296. do_update_region(vc, start, count);
  297. set_cursor(vc);
  298. }
  299. }
  300. /* Structure of attributes is hardware-dependent */
  301. static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink, u8 _underline, u8 _reverse)
  302. {
  303. if (vc->vc_sw->con_build_attr)
  304. return vc->vc_sw->con_build_attr(vc, _color, _intensity, _blink, _underline, _reverse);
  305. #ifndef VT_BUF_VRAM_ONLY
  306. /*
  307. * ++roman: I completely changed the attribute format for monochrome
  308. * mode (!can_do_color). The formerly used MDA (monochrome display
  309. * adapter) format didn't allow the combination of certain effects.
  310. * Now the attribute is just a bit vector:
  311. * Bit 0..1: intensity (0..2)
  312. * Bit 2 : underline
  313. * Bit 3 : reverse
  314. * Bit 7 : blink
  315. */
  316. {
  317. u8 a = vc->vc_color;
  318. if (!vc->vc_can_do_color)
  319. return _intensity |
  320. (_underline ? 4 : 0) |
  321. (_reverse ? 8 : 0) |
  322. (_blink ? 0x80 : 0);
  323. if (_underline)
  324. a = (a & 0xf0) | vc->vc_ulcolor;
  325. else if (_intensity == 0)
  326. a = (a & 0xf0) | vc->vc_ulcolor;
  327. if (_reverse)
  328. a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
  329. if (_blink)
  330. a ^= 0x80;
  331. if (_intensity == 2)
  332. a ^= 0x08;
  333. if (vc->vc_hi_font_mask == 0x100)
  334. a <<= 1;
  335. return a;
  336. }
  337. #else
  338. return 0;
  339. #endif
  340. }
  341. static void update_attr(struct vc_data *vc)
  342. {
  343. vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity, vc->vc_blink, vc->vc_underline, vc->vc_reverse ^ vc->vc_decscnm);
  344. vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm) << 8) | ' ';
  345. }
  346. /* Note: inverting the screen twice should revert to the original state */
  347. void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
  348. {
  349. unsigned short *p;
  350. WARN_CONSOLE_UNLOCKED();
  351. count /= 2;
  352. p = screenpos(vc, offset, viewed);
  353. if (vc->vc_sw->con_invert_region)
  354. vc->vc_sw->con_invert_region(vc, p, count);
  355. #ifndef VT_BUF_VRAM_ONLY
  356. else {
  357. u16 *q = p;
  358. int cnt = count;
  359. u16 a;
  360. if (!vc->vc_can_do_color) {
  361. while (cnt--) {
  362. a = scr_readw(q);
  363. a ^= 0x0800;
  364. scr_writew(a, q);
  365. q++;
  366. }
  367. } else if (vc->vc_hi_font_mask == 0x100) {
  368. while (cnt--) {
  369. a = scr_readw(q);
  370. a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
  371. scr_writew(a, q);
  372. q++;
  373. }
  374. } else {
  375. while (cnt--) {
  376. a = scr_readw(q);
  377. a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
  378. scr_writew(a, q);
  379. q++;
  380. }
  381. }
  382. }
  383. #endif
  384. if (DO_UPDATE(vc))
  385. do_update_region(vc, (unsigned long) p, count);
  386. }
  387. /* used by selection: complement pointer position */
  388. void complement_pos(struct vc_data *vc, int offset)
  389. {
  390. static int old_offset = -1;
  391. static unsigned short old;
  392. static unsigned short oldx, oldy;
  393. WARN_CONSOLE_UNLOCKED();
  394. if (old_offset != -1 && old_offset >= 0 &&
  395. old_offset < vc->vc_screenbuf_size) {
  396. scr_writew(old, screenpos(vc, old_offset, 1));
  397. if (DO_UPDATE(vc))
  398. vc->vc_sw->con_putc(vc, old, oldy, oldx);
  399. }
  400. old_offset = offset;
  401. if (offset != -1 && offset >= 0 &&
  402. offset < vc->vc_screenbuf_size) {
  403. unsigned short new;
  404. unsigned short *p;
  405. p = screenpos(vc, offset, 1);
  406. old = scr_readw(p);
  407. new = old ^ vc->vc_complement_mask;
  408. scr_writew(new, p);
  409. if (DO_UPDATE(vc)) {
  410. oldx = (offset >> 1) % vc->vc_cols;
  411. oldy = (offset >> 1) / vc->vc_cols;
  412. vc->vc_sw->con_putc(vc, new, oldy, oldx);
  413. }
  414. }
  415. }
  416. static void insert_char(struct vc_data *vc, unsigned int nr)
  417. {
  418. unsigned short *p, *q = (unsigned short *)vc->vc_pos;
  419. p = q + vc->vc_cols - nr - vc->vc_x;
  420. while (--p >= q)
  421. scr_writew(scr_readw(p), p + nr);
  422. scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
  423. vc->vc_need_wrap = 0;
  424. if (DO_UPDATE(vc)) {
  425. unsigned short oldattr = vc->vc_attr;
  426. vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
  427. vc->vc_cols - vc->vc_x - nr);
  428. vc->vc_attr = vc->vc_video_erase_char >> 8;
  429. while (nr--)
  430. vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
  431. vc->vc_attr = oldattr;
  432. }
  433. }
  434. static void delete_char(struct vc_data *vc, unsigned int nr)
  435. {
  436. unsigned int i = vc->vc_x;
  437. unsigned short *p = (unsigned short *)vc->vc_pos;
  438. while (++i <= vc->vc_cols - nr) {
  439. scr_writew(scr_readw(p+nr), p);
  440. p++;
  441. }
  442. scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
  443. vc->vc_need_wrap = 0;
  444. if (DO_UPDATE(vc)) {
  445. unsigned short oldattr = vc->vc_attr;
  446. vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
  447. vc->vc_cols - vc->vc_x - nr);
  448. vc->vc_attr = vc->vc_video_erase_char >> 8;
  449. while (nr--)
  450. vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
  451. vc->vc_cols - 1 - nr);
  452. vc->vc_attr = oldattr;
  453. }
  454. }
  455. static int softcursor_original;
  456. static void add_softcursor(struct vc_data *vc)
  457. {
  458. int i = scr_readw((u16 *) vc->vc_pos);
  459. u32 type = vc->vc_cursor_type;
  460. if (! (type & 0x10)) return;
  461. if (softcursor_original != -1) return;
  462. softcursor_original = i;
  463. i |= ((type >> 8) & 0xff00 );
  464. i ^= ((type) & 0xff00 );
  465. if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
  466. if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
  467. scr_writew(i, (u16 *) vc->vc_pos);
  468. if (DO_UPDATE(vc))
  469. vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
  470. }
  471. static void hide_softcursor(struct vc_data *vc)
  472. {
  473. if (softcursor_original != -1) {
  474. scr_writew(softcursor_original, (u16 *)vc->vc_pos);
  475. if (DO_UPDATE(vc))
  476. vc->vc_sw->con_putc(vc, softcursor_original,
  477. vc->vc_y, vc->vc_x);
  478. softcursor_original = -1;
  479. }
  480. }
  481. static void hide_cursor(struct vc_data *vc)
  482. {
  483. if (vc == sel_cons)
  484. clear_selection();
  485. vc->vc_sw->con_cursor(vc, CM_ERASE);
  486. hide_softcursor(vc);
  487. }
  488. static void set_cursor(struct vc_data *vc)
  489. {
  490. if (!IS_FG(vc) || console_blanked ||
  491. vc->vc_mode == KD_GRAPHICS)
  492. return;
  493. if (vc->vc_deccm) {
  494. if (vc == sel_cons)
  495. clear_selection();
  496. add_softcursor(vc);
  497. if ((vc->vc_cursor_type & 0x0f) != 1)
  498. vc->vc_sw->con_cursor(vc, CM_DRAW);
  499. } else
  500. hide_cursor(vc);
  501. }
  502. static void set_origin(struct vc_data *vc)
  503. {
  504. WARN_CONSOLE_UNLOCKED();
  505. if (!CON_IS_VISIBLE(vc) ||
  506. !vc->vc_sw->con_set_origin ||
  507. !vc->vc_sw->con_set_origin(vc))
  508. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  509. vc->vc_visible_origin = vc->vc_origin;
  510. vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
  511. vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
  512. }
  513. static inline void save_screen(struct vc_data *vc)
  514. {
  515. WARN_CONSOLE_UNLOCKED();
  516. if (vc->vc_sw->con_save_screen)
  517. vc->vc_sw->con_save_screen(vc);
  518. }
  519. /*
  520. * Redrawing of screen
  521. */
  522. static void clear_buffer_attributes(struct vc_data *vc)
  523. {
  524. unsigned short *p = (unsigned short *)vc->vc_origin;
  525. int count = vc->vc_screenbuf_size / 2;
  526. int mask = vc->vc_hi_font_mask | 0xff;
  527. for (; count > 0; count--, p++) {
  528. scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
  529. }
  530. }
  531. void redraw_screen(struct vc_data *vc, int is_switch)
  532. {
  533. int redraw = 0;
  534. WARN_CONSOLE_UNLOCKED();
  535. if (!vc) {
  536. /* strange ... */
  537. /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
  538. return;
  539. }
  540. if (is_switch) {
  541. struct vc_data *old_vc = vc_cons[fg_console].d;
  542. if (old_vc == vc)
  543. return;
  544. if (!CON_IS_VISIBLE(vc))
  545. redraw = 1;
  546. *vc->vc_display_fg = vc;
  547. fg_console = vc->vc_num;
  548. hide_cursor(old_vc);
  549. if (!CON_IS_VISIBLE(old_vc)) {
  550. save_screen(old_vc);
  551. set_origin(old_vc);
  552. }
  553. } else {
  554. hide_cursor(vc);
  555. redraw = 1;
  556. }
  557. if (redraw) {
  558. int update;
  559. int old_was_color = vc->vc_can_do_color;
  560. set_origin(vc);
  561. update = vc->vc_sw->con_switch(vc);
  562. set_palette(vc);
  563. /*
  564. * If console changed from mono<->color, the best we can do
  565. * is to clear the buffer attributes. As it currently stands,
  566. * rebuilding new attributes from the old buffer is not doable
  567. * without overly complex code.
  568. */
  569. if (old_was_color != vc->vc_can_do_color) {
  570. update_attr(vc);
  571. clear_buffer_attributes(vc);
  572. }
  573. if (update && vc->vc_mode != KD_GRAPHICS)
  574. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  575. }
  576. set_cursor(vc);
  577. if (is_switch) {
  578. set_leds();
  579. compute_shiftstate();
  580. }
  581. }
  582. /*
  583. * Allocation, freeing and resizing of VTs.
  584. */
  585. int vc_cons_allocated(unsigned int i)
  586. {
  587. return (i < MAX_NR_CONSOLES && vc_cons[i].d);
  588. }
  589. static void visual_init(struct vc_data *vc, int num, int init)
  590. {
  591. /* ++Geert: vc->vc_sw->con_init determines console size */
  592. if (vc->vc_sw)
  593. module_put(vc->vc_sw->owner);
  594. vc->vc_sw = conswitchp;
  595. #ifndef VT_SINGLE_DRIVER
  596. if (con_driver_map[num])
  597. vc->vc_sw = con_driver_map[num];
  598. #endif
  599. __module_get(vc->vc_sw->owner);
  600. vc->vc_num = num;
  601. vc->vc_display_fg = &master_display_fg;
  602. vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
  603. vc->vc_uni_pagedir = 0;
  604. vc->vc_hi_font_mask = 0;
  605. vc->vc_complement_mask = 0;
  606. vc->vc_can_do_color = 0;
  607. vc->vc_sw->con_init(vc, init);
  608. if (!vc->vc_complement_mask)
  609. vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
  610. vc->vc_s_complement_mask = vc->vc_complement_mask;
  611. vc->vc_size_row = vc->vc_cols << 1;
  612. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  613. }
  614. int vc_allocate(unsigned int currcons) /* return 0 on success */
  615. {
  616. WARN_CONSOLE_UNLOCKED();
  617. if (currcons >= MAX_NR_CONSOLES)
  618. return -ENXIO;
  619. if (!vc_cons[currcons].d) {
  620. struct vc_data *vc;
  621. /* prevent users from taking too much memory */
  622. if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
  623. return -EPERM;
  624. /* due to the granularity of kmalloc, we waste some memory here */
  625. /* the alloc is done in two steps, to optimize the common situation
  626. of a 25x80 console (structsize=216, screenbuf_size=4000) */
  627. /* although the numbers above are not valid since long ago, the
  628. point is still up-to-date and the comment still has its value
  629. even if only as a historical artifact. --mj, July 1998 */
  630. vc = kmalloc(sizeof(struct vc_data), GFP_KERNEL);
  631. if (!vc)
  632. return -ENOMEM;
  633. memset(vc, 0, sizeof(*vc));
  634. vc_cons[currcons].d = vc;
  635. visual_init(vc, currcons, 1);
  636. if (!*vc->vc_uni_pagedir_loc)
  637. con_set_default_unimap(vc);
  638. vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
  639. if (!vc->vc_screenbuf) {
  640. kfree(vc);
  641. vc_cons[currcons].d = NULL;
  642. return -ENOMEM;
  643. }
  644. vc->vc_kmalloced = 1;
  645. vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
  646. }
  647. return 0;
  648. }
  649. static inline int resize_screen(struct vc_data *vc, int width, int height)
  650. {
  651. /* Resizes the resolution of the display adapater */
  652. int err = 0;
  653. if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
  654. err = vc->vc_sw->con_resize(vc, width, height);
  655. return err;
  656. }
  657. /*
  658. * Change # of rows and columns (0 means unchanged/the size of fg_console)
  659. * [this is to be used together with some user program
  660. * like resize that changes the hardware videomode]
  661. */
  662. #define VC_RESIZE_MAXCOL (32767)
  663. #define VC_RESIZE_MAXROW (32767)
  664. int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int lines)
  665. {
  666. unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
  667. unsigned int old_cols, old_rows, old_row_size, old_screen_size;
  668. unsigned int new_cols, new_rows, new_row_size, new_screen_size;
  669. unsigned int end;
  670. unsigned short *newscreen;
  671. WARN_CONSOLE_UNLOCKED();
  672. if (!vc)
  673. return -ENXIO;
  674. if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
  675. return -EINVAL;
  676. new_cols = (cols ? cols : vc->vc_cols);
  677. new_rows = (lines ? lines : vc->vc_rows);
  678. new_row_size = new_cols << 1;
  679. new_screen_size = new_row_size * new_rows;
  680. if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
  681. return 0;
  682. newscreen = (unsigned short *) kmalloc(new_screen_size, GFP_USER);
  683. if (!newscreen)
  684. return -ENOMEM;
  685. old_rows = vc->vc_rows;
  686. old_cols = vc->vc_cols;
  687. old_row_size = vc->vc_size_row;
  688. old_screen_size = vc->vc_screenbuf_size;
  689. err = resize_screen(vc, new_cols, new_rows);
  690. if (err) {
  691. kfree(newscreen);
  692. return err;
  693. }
  694. vc->vc_rows = new_rows;
  695. vc->vc_cols = new_cols;
  696. vc->vc_size_row = new_row_size;
  697. vc->vc_screenbuf_size = new_screen_size;
  698. rlth = min(old_row_size, new_row_size);
  699. rrem = new_row_size - rlth;
  700. old_origin = vc->vc_origin;
  701. new_origin = (long) newscreen;
  702. new_scr_end = new_origin + new_screen_size;
  703. if (vc->vc_y > new_rows) {
  704. if (old_rows - vc->vc_y < new_rows) {
  705. /*
  706. * Cursor near the bottom, copy contents from the
  707. * bottom of buffer
  708. */
  709. old_origin += (old_rows - new_rows) * old_row_size;
  710. end = vc->vc_scr_end;
  711. } else {
  712. /*
  713. * Cursor is in no man's land, copy 1/2 screenful
  714. * from the top and bottom of cursor position
  715. */
  716. old_origin += (vc->vc_y - new_rows/2) * old_row_size;
  717. end = old_origin + new_screen_size;
  718. }
  719. } else
  720. /*
  721. * Cursor near the top, copy contents from the top of buffer
  722. */
  723. end = (old_rows > new_rows) ? old_origin + new_screen_size :
  724. vc->vc_scr_end;
  725. update_attr(vc);
  726. while (old_origin < end) {
  727. scr_memcpyw((unsigned short *) new_origin,
  728. (unsigned short *) old_origin, rlth);
  729. if (rrem)
  730. scr_memsetw((void *)(new_origin + rlth),
  731. vc->vc_video_erase_char, rrem);
  732. old_origin += old_row_size;
  733. new_origin += new_row_size;
  734. }
  735. if (new_scr_end > new_origin)
  736. scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
  737. new_scr_end - new_origin);
  738. if (vc->vc_kmalloced)
  739. kfree(vc->vc_screenbuf);
  740. vc->vc_screenbuf = newscreen;
  741. vc->vc_kmalloced = 1;
  742. vc->vc_screenbuf_size = new_screen_size;
  743. set_origin(vc);
  744. /* do part of a reset_terminal() */
  745. vc->vc_top = 0;
  746. vc->vc_bottom = vc->vc_rows;
  747. gotoxy(vc, vc->vc_x, vc->vc_y);
  748. save_cur(vc);
  749. if (vc->vc_tty) {
  750. struct winsize ws, *cws = &vc->vc_tty->winsize;
  751. memset(&ws, 0, sizeof(ws));
  752. ws.ws_row = vc->vc_rows;
  753. ws.ws_col = vc->vc_cols;
  754. ws.ws_ypixel = vc->vc_scan_lines;
  755. if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
  756. vc->vc_tty->pgrp > 0)
  757. kill_pg(vc->vc_tty->pgrp, SIGWINCH, 1);
  758. *cws = ws;
  759. }
  760. if (CON_IS_VISIBLE(vc))
  761. update_screen(vc);
  762. return err;
  763. }
  764. void vc_disallocate(unsigned int currcons)
  765. {
  766. WARN_CONSOLE_UNLOCKED();
  767. if (vc_cons_allocated(currcons)) {
  768. struct vc_data *vc = vc_cons[currcons].d;
  769. vc->vc_sw->con_deinit(vc);
  770. if (vc->vc_kmalloced)
  771. kfree(vc->vc_screenbuf);
  772. if (currcons >= MIN_NR_CONSOLES)
  773. kfree(vc);
  774. vc_cons[currcons].d = NULL;
  775. }
  776. }
  777. /*
  778. * VT102 emulator
  779. */
  780. #define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  781. #define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  782. #define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  783. #define decarm VC_REPEAT
  784. #define decckm VC_CKMODE
  785. #define kbdapplic VC_APPLIC
  786. #define lnm VC_CRLF
  787. /*
  788. * this is what the terminal answers to a ESC-Z or csi0c query.
  789. */
  790. #define VT100ID "\033[?1;2c"
  791. #define VT102ID "\033[?6c"
  792. unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
  793. 8,12,10,14, 9,13,11,15 };
  794. /* the default colour table, for VGA+ colour systems */
  795. int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
  796. 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
  797. int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
  798. 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
  799. int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
  800. 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
  801. /*
  802. * gotoxy() must verify all boundaries, because the arguments
  803. * might also be negative. If the given position is out of
  804. * bounds, the cursor is placed at the nearest margin.
  805. */
  806. static void gotoxy(struct vc_data *vc, int new_x, int new_y)
  807. {
  808. int min_y, max_y;
  809. if (new_x < 0)
  810. vc->vc_x = 0;
  811. else {
  812. if (new_x >= vc->vc_cols)
  813. vc->vc_x = vc->vc_cols - 1;
  814. else
  815. vc->vc_x = new_x;
  816. }
  817. if (vc->vc_decom) {
  818. min_y = vc->vc_top;
  819. max_y = vc->vc_bottom;
  820. } else {
  821. min_y = 0;
  822. max_y = vc->vc_rows;
  823. }
  824. if (new_y < min_y)
  825. vc->vc_y = min_y;
  826. else if (new_y >= max_y)
  827. vc->vc_y = max_y - 1;
  828. else
  829. vc->vc_y = new_y;
  830. vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
  831. vc->vc_need_wrap = 0;
  832. }
  833. /* for absolute user moves, when decom is set */
  834. static void gotoxay(struct vc_data *vc, int new_x, int new_y)
  835. {
  836. gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
  837. }
  838. void scrollback(struct vc_data *vc, int lines)
  839. {
  840. if (!lines)
  841. lines = vc->vc_rows / 2;
  842. scrolldelta(-lines);
  843. }
  844. void scrollfront(struct vc_data *vc, int lines)
  845. {
  846. if (!lines)
  847. lines = vc->vc_rows / 2;
  848. scrolldelta(lines);
  849. }
  850. static void lf(struct vc_data *vc)
  851. {
  852. /* don't scroll if above bottom of scrolling region, or
  853. * if below scrolling region
  854. */
  855. if (vc->vc_y + 1 == vc->vc_bottom)
  856. scrup(vc, vc->vc_top, vc->vc_bottom, 1);
  857. else if (vc->vc_y < vc->vc_rows - 1) {
  858. vc->vc_y++;
  859. vc->vc_pos += vc->vc_size_row;
  860. }
  861. vc->vc_need_wrap = 0;
  862. }
  863. static void ri(struct vc_data *vc)
  864. {
  865. /* don't scroll if below top of scrolling region, or
  866. * if above scrolling region
  867. */
  868. if (vc->vc_y == vc->vc_top)
  869. scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
  870. else if (vc->vc_y > 0) {
  871. vc->vc_y--;
  872. vc->vc_pos -= vc->vc_size_row;
  873. }
  874. vc->vc_need_wrap = 0;
  875. }
  876. static inline void cr(struct vc_data *vc)
  877. {
  878. vc->vc_pos -= vc->vc_x << 1;
  879. vc->vc_need_wrap = vc->vc_x = 0;
  880. }
  881. static inline void bs(struct vc_data *vc)
  882. {
  883. if (vc->vc_x) {
  884. vc->vc_pos -= 2;
  885. vc->vc_x--;
  886. vc->vc_need_wrap = 0;
  887. }
  888. }
  889. static inline void del(struct vc_data *vc)
  890. {
  891. /* ignored */
  892. }
  893. static void csi_J(struct vc_data *vc, int vpar)
  894. {
  895. unsigned int count;
  896. unsigned short * start;
  897. switch (vpar) {
  898. case 0: /* erase from cursor to end of display */
  899. count = (vc->vc_scr_end - vc->vc_pos) >> 1;
  900. start = (unsigned short *)vc->vc_pos;
  901. if (DO_UPDATE(vc)) {
  902. /* do in two stages */
  903. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
  904. vc->vc_cols - vc->vc_x);
  905. vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
  906. vc->vc_rows - vc->vc_y - 1,
  907. vc->vc_cols);
  908. }
  909. break;
  910. case 1: /* erase from start to cursor */
  911. count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
  912. start = (unsigned short *)vc->vc_origin;
  913. if (DO_UPDATE(vc)) {
  914. /* do in two stages */
  915. vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
  916. vc->vc_cols);
  917. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  918. vc->vc_x + 1);
  919. }
  920. break;
  921. case 2: /* erase whole display */
  922. count = vc->vc_cols * vc->vc_rows;
  923. start = (unsigned short *)vc->vc_origin;
  924. if (DO_UPDATE(vc))
  925. vc->vc_sw->con_clear(vc, 0, 0,
  926. vc->vc_rows,
  927. vc->vc_cols);
  928. break;
  929. default:
  930. return;
  931. }
  932. scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
  933. vc->vc_need_wrap = 0;
  934. }
  935. static void csi_K(struct vc_data *vc, int vpar)
  936. {
  937. unsigned int count;
  938. unsigned short * start;
  939. switch (vpar) {
  940. case 0: /* erase from cursor to end of line */
  941. count = vc->vc_cols - vc->vc_x;
  942. start = (unsigned short *)vc->vc_pos;
  943. if (DO_UPDATE(vc))
  944. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
  945. vc->vc_cols - vc->vc_x);
  946. break;
  947. case 1: /* erase from start of line to cursor */
  948. start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
  949. count = vc->vc_x + 1;
  950. if (DO_UPDATE(vc))
  951. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  952. vc->vc_x + 1);
  953. break;
  954. case 2: /* erase whole line */
  955. start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
  956. count = vc->vc_cols;
  957. if (DO_UPDATE(vc))
  958. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  959. vc->vc_cols);
  960. break;
  961. default:
  962. return;
  963. }
  964. scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
  965. vc->vc_need_wrap = 0;
  966. }
  967. static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
  968. { /* not vt100? */
  969. int count;
  970. if (!vpar)
  971. vpar++;
  972. count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
  973. scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
  974. if (DO_UPDATE(vc))
  975. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
  976. vc->vc_need_wrap = 0;
  977. }
  978. static void default_attr(struct vc_data *vc)
  979. {
  980. vc->vc_intensity = 1;
  981. vc->vc_underline = 0;
  982. vc->vc_reverse = 0;
  983. vc->vc_blink = 0;
  984. vc->vc_color = vc->vc_def_color;
  985. }
  986. /* console_sem is held */
  987. static void csi_m(struct vc_data *vc)
  988. {
  989. int i;
  990. for (i = 0; i <= vc->vc_npar; i++)
  991. switch (vc->vc_par[i]) {
  992. case 0: /* all attributes off */
  993. default_attr(vc);
  994. break;
  995. case 1:
  996. vc->vc_intensity = 2;
  997. break;
  998. case 2:
  999. vc->vc_intensity = 0;
  1000. break;
  1001. case 4:
  1002. vc->vc_underline = 1;
  1003. break;
  1004. case 5:
  1005. vc->vc_blink = 1;
  1006. break;
  1007. case 7:
  1008. vc->vc_reverse = 1;
  1009. break;
  1010. case 10: /* ANSI X3.64-1979 (SCO-ish?)
  1011. * Select primary font, don't display
  1012. * control chars if defined, don't set
  1013. * bit 8 on output.
  1014. */
  1015. vc->vc_translate = set_translate(vc->vc_charset == 0
  1016. ? vc->vc_G0_charset
  1017. : vc->vc_G1_charset, vc);
  1018. vc->vc_disp_ctrl = 0;
  1019. vc->vc_toggle_meta = 0;
  1020. break;
  1021. case 11: /* ANSI X3.64-1979 (SCO-ish?)
  1022. * Select first alternate font, lets
  1023. * chars < 32 be displayed as ROM chars.
  1024. */
  1025. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1026. vc->vc_disp_ctrl = 1;
  1027. vc->vc_toggle_meta = 0;
  1028. break;
  1029. case 12: /* ANSI X3.64-1979 (SCO-ish?)
  1030. * Select second alternate font, toggle
  1031. * high bit before displaying as ROM char.
  1032. */
  1033. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1034. vc->vc_disp_ctrl = 1;
  1035. vc->vc_toggle_meta = 1;
  1036. break;
  1037. case 21:
  1038. case 22:
  1039. vc->vc_intensity = 1;
  1040. break;
  1041. case 24:
  1042. vc->vc_underline = 0;
  1043. break;
  1044. case 25:
  1045. vc->vc_blink = 0;
  1046. break;
  1047. case 27:
  1048. vc->vc_reverse = 0;
  1049. break;
  1050. case 38: /* ANSI X3.64-1979 (SCO-ish?)
  1051. * Enables underscore, white foreground
  1052. * with white underscore (Linux - use
  1053. * default foreground).
  1054. */
  1055. vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
  1056. vc->vc_underline = 1;
  1057. break;
  1058. case 39: /* ANSI X3.64-1979 (SCO-ish?)
  1059. * Disable underline option.
  1060. * Reset colour to default? It did this
  1061. * before...
  1062. */
  1063. vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
  1064. vc->vc_underline = 0;
  1065. break;
  1066. case 49:
  1067. vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
  1068. break;
  1069. default:
  1070. if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
  1071. vc->vc_color = color_table[vc->vc_par[i] - 30]
  1072. | (vc->vc_color & 0xf0);
  1073. else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
  1074. vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
  1075. | (vc->vc_color & 0x0f);
  1076. break;
  1077. }
  1078. update_attr(vc);
  1079. }
  1080. static void respond_string(const char *p, struct tty_struct *tty)
  1081. {
  1082. while (*p) {
  1083. tty_insert_flip_char(tty, *p, 0);
  1084. p++;
  1085. }
  1086. con_schedule_flip(tty);
  1087. }
  1088. static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
  1089. {
  1090. char buf[40];
  1091. sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
  1092. respond_string(buf, tty);
  1093. }
  1094. static inline void status_report(struct tty_struct *tty)
  1095. {
  1096. respond_string("\033[0n", tty); /* Terminal ok */
  1097. }
  1098. static inline void respond_ID(struct tty_struct * tty)
  1099. {
  1100. respond_string(VT102ID, tty);
  1101. }
  1102. void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
  1103. {
  1104. char buf[8];
  1105. sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
  1106. (char)('!' + mry));
  1107. respond_string(buf, tty);
  1108. }
  1109. /* invoked via ioctl(TIOCLINUX) and through set_selection */
  1110. int mouse_reporting(void)
  1111. {
  1112. return vc_cons[fg_console].d->vc_report_mouse;
  1113. }
  1114. /* console_sem is held */
  1115. static void set_mode(struct vc_data *vc, int on_off)
  1116. {
  1117. int i;
  1118. for (i = 0; i <= vc->vc_npar; i++)
  1119. if (vc->vc_ques) {
  1120. switch(vc->vc_par[i]) { /* DEC private modes set/reset */
  1121. case 1: /* Cursor keys send ^[Ox/^[[x */
  1122. if (on_off)
  1123. set_kbd(vc, decckm);
  1124. else
  1125. clr_kbd(vc, decckm);
  1126. break;
  1127. case 3: /* 80/132 mode switch unimplemented */
  1128. vc->vc_deccolm = on_off;
  1129. #if 0
  1130. vc_resize(deccolm ? 132 : 80, vc->vc_rows);
  1131. /* this alone does not suffice; some user mode
  1132. utility has to change the hardware regs */
  1133. #endif
  1134. break;
  1135. case 5: /* Inverted screen on/off */
  1136. if (vc->vc_decscnm != on_off) {
  1137. vc->vc_decscnm = on_off;
  1138. invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
  1139. update_attr(vc);
  1140. }
  1141. break;
  1142. case 6: /* Origin relative/absolute */
  1143. vc->vc_decom = on_off;
  1144. gotoxay(vc, 0, 0);
  1145. break;
  1146. case 7: /* Autowrap on/off */
  1147. vc->vc_decawm = on_off;
  1148. break;
  1149. case 8: /* Autorepeat on/off */
  1150. if (on_off)
  1151. set_kbd(vc, decarm);
  1152. else
  1153. clr_kbd(vc, decarm);
  1154. break;
  1155. case 9:
  1156. vc->vc_report_mouse = on_off ? 1 : 0;
  1157. break;
  1158. case 25: /* Cursor on/off */
  1159. vc->vc_deccm = on_off;
  1160. break;
  1161. case 1000:
  1162. vc->vc_report_mouse = on_off ? 2 : 0;
  1163. break;
  1164. }
  1165. } else {
  1166. switch(vc->vc_par[i]) { /* ANSI modes set/reset */
  1167. case 3: /* Monitor (display ctrls) */
  1168. vc->vc_disp_ctrl = on_off;
  1169. break;
  1170. case 4: /* Insert Mode on/off */
  1171. vc->vc_decim = on_off;
  1172. break;
  1173. case 20: /* Lf, Enter == CrLf/Lf */
  1174. if (on_off)
  1175. set_kbd(vc, lnm);
  1176. else
  1177. clr_kbd(vc, lnm);
  1178. break;
  1179. }
  1180. }
  1181. }
  1182. /* console_sem is held */
  1183. static void setterm_command(struct vc_data *vc)
  1184. {
  1185. switch(vc->vc_par[0]) {
  1186. case 1: /* set color for underline mode */
  1187. if (vc->vc_can_do_color &&
  1188. vc->vc_par[1] < 16) {
  1189. vc->vc_ulcolor = color_table[vc->vc_par[1]];
  1190. if (vc->vc_underline)
  1191. update_attr(vc);
  1192. }
  1193. break;
  1194. case 2: /* set color for half intensity mode */
  1195. if (vc->vc_can_do_color &&
  1196. vc->vc_par[1] < 16) {
  1197. vc->vc_halfcolor = color_table[vc->vc_par[1]];
  1198. if (vc->vc_intensity == 0)
  1199. update_attr(vc);
  1200. }
  1201. break;
  1202. case 8: /* store colors as defaults */
  1203. vc->vc_def_color = vc->vc_attr;
  1204. if (vc->vc_hi_font_mask == 0x100)
  1205. vc->vc_def_color >>= 1;
  1206. default_attr(vc);
  1207. update_attr(vc);
  1208. break;
  1209. case 9: /* set blanking interval */
  1210. blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
  1211. poke_blanked_console();
  1212. break;
  1213. case 10: /* set bell frequency in Hz */
  1214. if (vc->vc_npar >= 1)
  1215. vc->vc_bell_pitch = vc->vc_par[1];
  1216. else
  1217. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1218. break;
  1219. case 11: /* set bell duration in msec */
  1220. if (vc->vc_npar >= 1)
  1221. vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
  1222. vc->vc_par[1] * HZ / 1000 : 0;
  1223. else
  1224. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1225. break;
  1226. case 12: /* bring specified console to the front */
  1227. if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
  1228. set_console(vc->vc_par[1] - 1);
  1229. break;
  1230. case 13: /* unblank the screen */
  1231. poke_blanked_console();
  1232. break;
  1233. case 14: /* set vesa powerdown interval */
  1234. vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
  1235. break;
  1236. case 15: /* activate the previous console */
  1237. set_console(last_console);
  1238. break;
  1239. }
  1240. }
  1241. /* console_sem is held */
  1242. static void csi_at(struct vc_data *vc, unsigned int nr)
  1243. {
  1244. if (nr > vc->vc_cols - vc->vc_x)
  1245. nr = vc->vc_cols - vc->vc_x;
  1246. else if (!nr)
  1247. nr = 1;
  1248. insert_char(vc, nr);
  1249. }
  1250. /* console_sem is held */
  1251. static void csi_L(struct vc_data *vc, unsigned int nr)
  1252. {
  1253. if (nr > vc->vc_rows - vc->vc_y)
  1254. nr = vc->vc_rows - vc->vc_y;
  1255. else if (!nr)
  1256. nr = 1;
  1257. scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
  1258. vc->vc_need_wrap = 0;
  1259. }
  1260. /* console_sem is held */
  1261. static void csi_P(struct vc_data *vc, unsigned int nr)
  1262. {
  1263. if (nr > vc->vc_cols - vc->vc_x)
  1264. nr = vc->vc_cols - vc->vc_x;
  1265. else if (!nr)
  1266. nr = 1;
  1267. delete_char(vc, nr);
  1268. }
  1269. /* console_sem is held */
  1270. static void csi_M(struct vc_data *vc, unsigned int nr)
  1271. {
  1272. if (nr > vc->vc_rows - vc->vc_y)
  1273. nr = vc->vc_rows - vc->vc_y;
  1274. else if (!nr)
  1275. nr=1;
  1276. scrup(vc, vc->vc_y, vc->vc_bottom, nr);
  1277. vc->vc_need_wrap = 0;
  1278. }
  1279. /* console_sem is held (except via vc_init->reset_terminal */
  1280. static void save_cur(struct vc_data *vc)
  1281. {
  1282. vc->vc_saved_x = vc->vc_x;
  1283. vc->vc_saved_y = vc->vc_y;
  1284. vc->vc_s_intensity = vc->vc_intensity;
  1285. vc->vc_s_underline = vc->vc_underline;
  1286. vc->vc_s_blink = vc->vc_blink;
  1287. vc->vc_s_reverse = vc->vc_reverse;
  1288. vc->vc_s_charset = vc->vc_charset;
  1289. vc->vc_s_color = vc->vc_color;
  1290. vc->vc_saved_G0 = vc->vc_G0_charset;
  1291. vc->vc_saved_G1 = vc->vc_G1_charset;
  1292. }
  1293. /* console_sem is held */
  1294. static void restore_cur(struct vc_data *vc)
  1295. {
  1296. gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
  1297. vc->vc_intensity = vc->vc_s_intensity;
  1298. vc->vc_underline = vc->vc_s_underline;
  1299. vc->vc_blink = vc->vc_s_blink;
  1300. vc->vc_reverse = vc->vc_s_reverse;
  1301. vc->vc_charset = vc->vc_s_charset;
  1302. vc->vc_color = vc->vc_s_color;
  1303. vc->vc_G0_charset = vc->vc_saved_G0;
  1304. vc->vc_G1_charset = vc->vc_saved_G1;
  1305. vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
  1306. update_attr(vc);
  1307. vc->vc_need_wrap = 0;
  1308. }
  1309. enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
  1310. EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
  1311. ESpalette };
  1312. /* console_sem is held (except via vc_init()) */
  1313. static void reset_terminal(struct vc_data *vc, int do_clear)
  1314. {
  1315. vc->vc_top = 0;
  1316. vc->vc_bottom = vc->vc_rows;
  1317. vc->vc_state = ESnormal;
  1318. vc->vc_ques = 0;
  1319. vc->vc_translate = set_translate(LAT1_MAP, vc);
  1320. vc->vc_G0_charset = LAT1_MAP;
  1321. vc->vc_G1_charset = GRAF_MAP;
  1322. vc->vc_charset = 0;
  1323. vc->vc_need_wrap = 0;
  1324. vc->vc_report_mouse = 0;
  1325. vc->vc_utf = 0;
  1326. vc->vc_utf_count = 0;
  1327. vc->vc_disp_ctrl = 0;
  1328. vc->vc_toggle_meta = 0;
  1329. vc->vc_decscnm = 0;
  1330. vc->vc_decom = 0;
  1331. vc->vc_decawm = 1;
  1332. vc->vc_deccm = 1;
  1333. vc->vc_decim = 0;
  1334. set_kbd(vc, decarm);
  1335. clr_kbd(vc, decckm);
  1336. clr_kbd(vc, kbdapplic);
  1337. clr_kbd(vc, lnm);
  1338. kbd_table[vc->vc_num].lockstate = 0;
  1339. kbd_table[vc->vc_num].slockstate = 0;
  1340. kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
  1341. kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
  1342. /* do not do set_leds here because this causes an endless tasklet loop
  1343. when the keyboard hasn't been initialized yet */
  1344. vc->vc_cursor_type = CUR_DEFAULT;
  1345. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1346. default_attr(vc);
  1347. update_attr(vc);
  1348. vc->vc_tab_stop[0] = 0x01010100;
  1349. vc->vc_tab_stop[1] =
  1350. vc->vc_tab_stop[2] =
  1351. vc->vc_tab_stop[3] =
  1352. vc->vc_tab_stop[4] = 0x01010101;
  1353. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1354. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1355. gotoxy(vc, 0, 0);
  1356. save_cur(vc);
  1357. if (do_clear)
  1358. csi_J(vc, 2);
  1359. }
  1360. /* console_sem is held */
  1361. static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
  1362. {
  1363. /*
  1364. * Control characters can be used in the _middle_
  1365. * of an escape sequence.
  1366. */
  1367. switch (c) {
  1368. case 0:
  1369. return;
  1370. case 7:
  1371. if (vc->vc_bell_duration)
  1372. kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
  1373. return;
  1374. case 8:
  1375. bs(vc);
  1376. return;
  1377. case 9:
  1378. vc->vc_pos -= (vc->vc_x << 1);
  1379. while (vc->vc_x < vc->vc_cols - 1) {
  1380. vc->vc_x++;
  1381. if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
  1382. break;
  1383. }
  1384. vc->vc_pos += (vc->vc_x << 1);
  1385. return;
  1386. case 10: case 11: case 12:
  1387. lf(vc);
  1388. if (!is_kbd(vc, lnm))
  1389. return;
  1390. case 13:
  1391. cr(vc);
  1392. return;
  1393. case 14:
  1394. vc->vc_charset = 1;
  1395. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1396. vc->vc_disp_ctrl = 1;
  1397. return;
  1398. case 15:
  1399. vc->vc_charset = 0;
  1400. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1401. vc->vc_disp_ctrl = 0;
  1402. return;
  1403. case 24: case 26:
  1404. vc->vc_state = ESnormal;
  1405. return;
  1406. case 27:
  1407. vc->vc_state = ESesc;
  1408. return;
  1409. case 127:
  1410. del(vc);
  1411. return;
  1412. case 128+27:
  1413. vc->vc_state = ESsquare;
  1414. return;
  1415. }
  1416. switch(vc->vc_state) {
  1417. case ESesc:
  1418. vc->vc_state = ESnormal;
  1419. switch (c) {
  1420. case '[':
  1421. vc->vc_state = ESsquare;
  1422. return;
  1423. case ']':
  1424. vc->vc_state = ESnonstd;
  1425. return;
  1426. case '%':
  1427. vc->vc_state = ESpercent;
  1428. return;
  1429. case 'E':
  1430. cr(vc);
  1431. lf(vc);
  1432. return;
  1433. case 'M':
  1434. ri(vc);
  1435. return;
  1436. case 'D':
  1437. lf(vc);
  1438. return;
  1439. case 'H':
  1440. vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
  1441. return;
  1442. case 'Z':
  1443. respond_ID(tty);
  1444. return;
  1445. case '7':
  1446. save_cur(vc);
  1447. return;
  1448. case '8':
  1449. restore_cur(vc);
  1450. return;
  1451. case '(':
  1452. vc->vc_state = ESsetG0;
  1453. return;
  1454. case ')':
  1455. vc->vc_state = ESsetG1;
  1456. return;
  1457. case '#':
  1458. vc->vc_state = EShash;
  1459. return;
  1460. case 'c':
  1461. reset_terminal(vc, 1);
  1462. return;
  1463. case '>': /* Numeric keypad */
  1464. clr_kbd(vc, kbdapplic);
  1465. return;
  1466. case '=': /* Appl. keypad */
  1467. set_kbd(vc, kbdapplic);
  1468. return;
  1469. }
  1470. return;
  1471. case ESnonstd:
  1472. if (c=='P') { /* palette escape sequence */
  1473. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1474. vc->vc_par[vc->vc_npar] = 0;
  1475. vc->vc_npar = 0;
  1476. vc->vc_state = ESpalette;
  1477. return;
  1478. } else if (c=='R') { /* reset palette */
  1479. reset_palette(vc);
  1480. vc->vc_state = ESnormal;
  1481. } else
  1482. vc->vc_state = ESnormal;
  1483. return;
  1484. case ESpalette:
  1485. if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
  1486. vc->vc_par[vc->vc_npar++] = (c > '9' ? (c & 0xDF) - 'A' + 10 : c - '0');
  1487. if (vc->vc_npar == 7) {
  1488. int i = vc->vc_par[0] * 3, j = 1;
  1489. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1490. vc->vc_palette[i++] += vc->vc_par[j++];
  1491. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1492. vc->vc_palette[i++] += vc->vc_par[j++];
  1493. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1494. vc->vc_palette[i] += vc->vc_par[j];
  1495. set_palette(vc);
  1496. vc->vc_state = ESnormal;
  1497. }
  1498. } else
  1499. vc->vc_state = ESnormal;
  1500. return;
  1501. case ESsquare:
  1502. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1503. vc->vc_par[vc->vc_npar] = 0;
  1504. vc->vc_npar = 0;
  1505. vc->vc_state = ESgetpars;
  1506. if (c == '[') { /* Function key */
  1507. vc->vc_state=ESfunckey;
  1508. return;
  1509. }
  1510. vc->vc_ques = (c == '?');
  1511. if (vc->vc_ques)
  1512. return;
  1513. case ESgetpars:
  1514. if (c == ';' && vc->vc_npar < NPAR - 1) {
  1515. vc->vc_npar++;
  1516. return;
  1517. } else if (c>='0' && c<='9') {
  1518. vc->vc_par[vc->vc_npar] *= 10;
  1519. vc->vc_par[vc->vc_npar] += c - '0';
  1520. return;
  1521. } else
  1522. vc->vc_state = ESgotpars;
  1523. case ESgotpars:
  1524. vc->vc_state = ESnormal;
  1525. switch(c) {
  1526. case 'h':
  1527. set_mode(vc, 1);
  1528. return;
  1529. case 'l':
  1530. set_mode(vc, 0);
  1531. return;
  1532. case 'c':
  1533. if (vc->vc_ques) {
  1534. if (vc->vc_par[0])
  1535. vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
  1536. else
  1537. vc->vc_cursor_type = CUR_DEFAULT;
  1538. return;
  1539. }
  1540. break;
  1541. case 'm':
  1542. if (vc->vc_ques) {
  1543. clear_selection();
  1544. if (vc->vc_par[0])
  1545. vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
  1546. else
  1547. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1548. return;
  1549. }
  1550. break;
  1551. case 'n':
  1552. if (!vc->vc_ques) {
  1553. if (vc->vc_par[0] == 5)
  1554. status_report(tty);
  1555. else if (vc->vc_par[0] == 6)
  1556. cursor_report(vc, tty);
  1557. }
  1558. return;
  1559. }
  1560. if (vc->vc_ques) {
  1561. vc->vc_ques = 0;
  1562. return;
  1563. }
  1564. switch(c) {
  1565. case 'G': case '`':
  1566. if (vc->vc_par[0])
  1567. vc->vc_par[0]--;
  1568. gotoxy(vc, vc->vc_par[0], vc->vc_y);
  1569. return;
  1570. case 'A':
  1571. if (!vc->vc_par[0])
  1572. vc->vc_par[0]++;
  1573. gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
  1574. return;
  1575. case 'B': case 'e':
  1576. if (!vc->vc_par[0])
  1577. vc->vc_par[0]++;
  1578. gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
  1579. return;
  1580. case 'C': case 'a':
  1581. if (!vc->vc_par[0])
  1582. vc->vc_par[0]++;
  1583. gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
  1584. return;
  1585. case 'D':
  1586. if (!vc->vc_par[0])
  1587. vc->vc_par[0]++;
  1588. gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
  1589. return;
  1590. case 'E':
  1591. if (!vc->vc_par[0])
  1592. vc->vc_par[0]++;
  1593. gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
  1594. return;
  1595. case 'F':
  1596. if (!vc->vc_par[0])
  1597. vc->vc_par[0]++;
  1598. gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
  1599. return;
  1600. case 'd':
  1601. if (vc->vc_par[0])
  1602. vc->vc_par[0]--;
  1603. gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
  1604. return;
  1605. case 'H': case 'f':
  1606. if (vc->vc_par[0])
  1607. vc->vc_par[0]--;
  1608. if (vc->vc_par[1])
  1609. vc->vc_par[1]--;
  1610. gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
  1611. return;
  1612. case 'J':
  1613. csi_J(vc, vc->vc_par[0]);
  1614. return;
  1615. case 'K':
  1616. csi_K(vc, vc->vc_par[0]);
  1617. return;
  1618. case 'L':
  1619. csi_L(vc, vc->vc_par[0]);
  1620. return;
  1621. case 'M':
  1622. csi_M(vc, vc->vc_par[0]);
  1623. return;
  1624. case 'P':
  1625. csi_P(vc, vc->vc_par[0]);
  1626. return;
  1627. case 'c':
  1628. if (!vc->vc_par[0])
  1629. respond_ID(tty);
  1630. return;
  1631. case 'g':
  1632. if (!vc->vc_par[0])
  1633. vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
  1634. else if (vc->vc_par[0] == 3) {
  1635. vc->vc_tab_stop[0] =
  1636. vc->vc_tab_stop[1] =
  1637. vc->vc_tab_stop[2] =
  1638. vc->vc_tab_stop[3] =
  1639. vc->vc_tab_stop[4] = 0;
  1640. }
  1641. return;
  1642. case 'm':
  1643. csi_m(vc);
  1644. return;
  1645. case 'q': /* DECLL - but only 3 leds */
  1646. /* map 0,1,2,3 to 0,1,2,4 */
  1647. if (vc->vc_par[0] < 4)
  1648. setledstate(kbd_table + vc->vc_num,
  1649. (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
  1650. return;
  1651. case 'r':
  1652. if (!vc->vc_par[0])
  1653. vc->vc_par[0]++;
  1654. if (!vc->vc_par[1])
  1655. vc->vc_par[1] = vc->vc_rows;
  1656. /* Minimum allowed region is 2 lines */
  1657. if (vc->vc_par[0] < vc->vc_par[1] &&
  1658. vc->vc_par[1] <= vc->vc_rows) {
  1659. vc->vc_top = vc->vc_par[0] - 1;
  1660. vc->vc_bottom = vc->vc_par[1];
  1661. gotoxay(vc, 0, 0);
  1662. }
  1663. return;
  1664. case 's':
  1665. save_cur(vc);
  1666. return;
  1667. case 'u':
  1668. restore_cur(vc);
  1669. return;
  1670. case 'X':
  1671. csi_X(vc, vc->vc_par[0]);
  1672. return;
  1673. case '@':
  1674. csi_at(vc, vc->vc_par[0]);
  1675. return;
  1676. case ']': /* setterm functions */
  1677. setterm_command(vc);
  1678. return;
  1679. }
  1680. return;
  1681. case ESpercent:
  1682. vc->vc_state = ESnormal;
  1683. switch (c) {
  1684. case '@': /* defined in ISO 2022 */
  1685. vc->vc_utf = 0;
  1686. return;
  1687. case 'G': /* prelim official escape code */
  1688. case '8': /* retained for compatibility */
  1689. vc->vc_utf = 1;
  1690. return;
  1691. }
  1692. return;
  1693. case ESfunckey:
  1694. vc->vc_state = ESnormal;
  1695. return;
  1696. case EShash:
  1697. vc->vc_state = ESnormal;
  1698. if (c == '8') {
  1699. /* DEC screen alignment test. kludge :-) */
  1700. vc->vc_video_erase_char =
  1701. (vc->vc_video_erase_char & 0xff00) | 'E';
  1702. csi_J(vc, 2);
  1703. vc->vc_video_erase_char =
  1704. (vc->vc_video_erase_char & 0xff00) | ' ';
  1705. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  1706. }
  1707. return;
  1708. case ESsetG0:
  1709. if (c == '0')
  1710. vc->vc_G0_charset = GRAF_MAP;
  1711. else if (c == 'B')
  1712. vc->vc_G0_charset = LAT1_MAP;
  1713. else if (c == 'U')
  1714. vc->vc_G0_charset = IBMPC_MAP;
  1715. else if (c == 'K')
  1716. vc->vc_G0_charset = USER_MAP;
  1717. if (vc->vc_charset == 0)
  1718. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1719. vc->vc_state = ESnormal;
  1720. return;
  1721. case ESsetG1:
  1722. if (c == '0')
  1723. vc->vc_G1_charset = GRAF_MAP;
  1724. else if (c == 'B')
  1725. vc->vc_G1_charset = LAT1_MAP;
  1726. else if (c == 'U')
  1727. vc->vc_G1_charset = IBMPC_MAP;
  1728. else if (c == 'K')
  1729. vc->vc_G1_charset = USER_MAP;
  1730. if (vc->vc_charset == 1)
  1731. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1732. vc->vc_state = ESnormal;
  1733. return;
  1734. default:
  1735. vc->vc_state = ESnormal;
  1736. }
  1737. }
  1738. /* This is a temporary buffer used to prepare a tty console write
  1739. * so that we can easily avoid touching user space while holding the
  1740. * console spinlock. It is allocated in con_init and is shared by
  1741. * this code and the vc_screen read/write tty calls.
  1742. *
  1743. * We have to allocate this statically in the kernel data section
  1744. * since console_init (and thus con_init) are called before any
  1745. * kernel memory allocation is available.
  1746. */
  1747. char con_buf[CON_BUF_SIZE];
  1748. DECLARE_MUTEX(con_buf_sem);
  1749. /* acquires console_sem */
  1750. static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1751. {
  1752. #ifdef VT_BUF_VRAM_ONLY
  1753. #define FLUSH do { } while(0);
  1754. #else
  1755. #define FLUSH if (draw_x >= 0) { \
  1756. vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
  1757. draw_x = -1; \
  1758. }
  1759. #endif
  1760. int c, tc, ok, n = 0, draw_x = -1;
  1761. unsigned int currcons;
  1762. unsigned long draw_from = 0, draw_to = 0;
  1763. struct vc_data *vc;
  1764. u16 himask, charmask;
  1765. const unsigned char *orig_buf = NULL;
  1766. int orig_count;
  1767. if (in_interrupt())
  1768. return count;
  1769. might_sleep();
  1770. acquire_console_sem();
  1771. vc = tty->driver_data;
  1772. if (vc == NULL) {
  1773. printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
  1774. release_console_sem();
  1775. return 0;
  1776. }
  1777. currcons = vc->vc_num;
  1778. if (!vc_cons_allocated(currcons)) {
  1779. /* could this happen? */
  1780. static int error = 0;
  1781. if (!error) {
  1782. error = 1;
  1783. printk("con_write: tty %d not allocated\n", currcons+1);
  1784. }
  1785. release_console_sem();
  1786. return 0;
  1787. }
  1788. release_console_sem();
  1789. orig_buf = buf;
  1790. orig_count = count;
  1791. /* At this point 'buf' is guaranteed to be a kernel buffer
  1792. * and therefore no access to userspace (and therefore sleeping)
  1793. * will be needed. The con_buf_sem serializes all tty based
  1794. * console rendering and vcs write/read operations. We hold
  1795. * the console spinlock during the entire write.
  1796. */
  1797. acquire_console_sem();
  1798. vc = tty->driver_data;
  1799. if (vc == NULL) {
  1800. printk(KERN_ERR "vt: argh, driver_data _became_ NULL !\n");
  1801. release_console_sem();
  1802. goto out;
  1803. }
  1804. himask = vc->vc_hi_font_mask;
  1805. charmask = himask ? 0x1ff : 0xff;
  1806. /* undraw cursor first */
  1807. if (IS_FG(vc))
  1808. hide_cursor(vc);
  1809. while (!tty->stopped && count) {
  1810. int orig = *buf;
  1811. c = orig;
  1812. buf++;
  1813. n++;
  1814. count--;
  1815. /* Do no translation at all in control states */
  1816. if (vc->vc_state != ESnormal) {
  1817. tc = c;
  1818. } else if (vc->vc_utf) {
  1819. /* Combine UTF-8 into Unicode */
  1820. /* Incomplete characters silently ignored */
  1821. if(c > 0x7f) {
  1822. if (vc->vc_utf_count > 0 && (c & 0xc0) == 0x80) {
  1823. vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
  1824. vc->vc_utf_count--;
  1825. if (vc->vc_utf_count == 0)
  1826. tc = c = vc->vc_utf_char;
  1827. else continue;
  1828. } else {
  1829. if ((c & 0xe0) == 0xc0) {
  1830. vc->vc_utf_count = 1;
  1831. vc->vc_utf_char = (c & 0x1f);
  1832. } else if ((c & 0xf0) == 0xe0) {
  1833. vc->vc_utf_count = 2;
  1834. vc->vc_utf_char = (c & 0x0f);
  1835. } else if ((c & 0xf8) == 0xf0) {
  1836. vc->vc_utf_count = 3;
  1837. vc->vc_utf_char = (c & 0x07);
  1838. } else if ((c & 0xfc) == 0xf8) {
  1839. vc->vc_utf_count = 4;
  1840. vc->vc_utf_char = (c & 0x03);
  1841. } else if ((c & 0xfe) == 0xfc) {
  1842. vc->vc_utf_count = 5;
  1843. vc->vc_utf_char = (c & 0x01);
  1844. } else
  1845. vc->vc_utf_count = 0;
  1846. continue;
  1847. }
  1848. } else {
  1849. tc = c;
  1850. vc->vc_utf_count = 0;
  1851. }
  1852. } else { /* no utf */
  1853. tc = vc->vc_translate[vc->vc_toggle_meta ? (c | 0x80) : c];
  1854. }
  1855. /* If the original code was a control character we
  1856. * only allow a glyph to be displayed if the code is
  1857. * not normally used (such as for cursor movement) or
  1858. * if the disp_ctrl mode has been explicitly enabled.
  1859. * Certain characters (as given by the CTRL_ALWAYS
  1860. * bitmap) are always displayed as control characters,
  1861. * as the console would be pretty useless without
  1862. * them; to display an arbitrary font position use the
  1863. * direct-to-font zone in UTF-8 mode.
  1864. */
  1865. ok = tc && (c >= 32 ||
  1866. (!vc->vc_utf && !(((vc->vc_disp_ctrl ? CTRL_ALWAYS
  1867. : CTRL_ACTION) >> c) & 1)))
  1868. && (c != 127 || vc->vc_disp_ctrl)
  1869. && (c != 128+27);
  1870. if (vc->vc_state == ESnormal && ok) {
  1871. /* Now try to find out how to display it */
  1872. tc = conv_uni_to_pc(vc, tc);
  1873. if ( tc == -4 ) {
  1874. /* If we got -4 (not found) then see if we have
  1875. defined a replacement character (U+FFFD) */
  1876. tc = conv_uni_to_pc(vc, 0xfffd);
  1877. /* One reason for the -4 can be that we just
  1878. did a clear_unimap();
  1879. try at least to show something. */
  1880. if (tc == -4)
  1881. tc = c;
  1882. } else if ( tc == -3 ) {
  1883. /* Bad hash table -- hope for the best */
  1884. tc = c;
  1885. }
  1886. if (tc & ~charmask)
  1887. continue; /* Conversion failed */
  1888. if (vc->vc_need_wrap || vc->vc_decim)
  1889. FLUSH
  1890. if (vc->vc_need_wrap) {
  1891. cr(vc);
  1892. lf(vc);
  1893. }
  1894. if (vc->vc_decim)
  1895. insert_char(vc, 1);
  1896. scr_writew(himask ?
  1897. ((vc->vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
  1898. (vc->vc_attr << 8) + tc,
  1899. (u16 *) vc->vc_pos);
  1900. if (DO_UPDATE(vc) && draw_x < 0) {
  1901. draw_x = vc->vc_x;
  1902. draw_from = vc->vc_pos;
  1903. }
  1904. if (vc->vc_x == vc->vc_cols - 1) {
  1905. vc->vc_need_wrap = vc->vc_decawm;
  1906. draw_to = vc->vc_pos + 2;
  1907. } else {
  1908. vc->vc_x++;
  1909. draw_to = (vc->vc_pos += 2);
  1910. }
  1911. continue;
  1912. }
  1913. FLUSH
  1914. do_con_trol(tty, vc, orig);
  1915. }
  1916. FLUSH
  1917. console_conditional_schedule();
  1918. release_console_sem();
  1919. out:
  1920. return n;
  1921. #undef FLUSH
  1922. }
  1923. /*
  1924. * This is the console switching callback.
  1925. *
  1926. * Doing console switching in a process context allows
  1927. * us to do the switches asynchronously (needed when we want
  1928. * to switch due to a keyboard interrupt). Synchronization
  1929. * with other console code and prevention of re-entrancy is
  1930. * ensured with console_sem.
  1931. */
  1932. static void console_callback(void *ignored)
  1933. {
  1934. acquire_console_sem();
  1935. if (want_console >= 0) {
  1936. if (want_console != fg_console &&
  1937. vc_cons_allocated(want_console)) {
  1938. hide_cursor(vc_cons[fg_console].d);
  1939. change_console(vc_cons[want_console].d);
  1940. /* we only changed when the console had already
  1941. been allocated - a new console is not created
  1942. in an interrupt routine */
  1943. }
  1944. want_console = -1;
  1945. }
  1946. if (do_poke_blanked_console) { /* do not unblank for a LED change */
  1947. do_poke_blanked_console = 0;
  1948. poke_blanked_console();
  1949. }
  1950. if (scrollback_delta) {
  1951. struct vc_data *vc = vc_cons[fg_console].d;
  1952. clear_selection();
  1953. if (vc->vc_mode == KD_TEXT)
  1954. vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
  1955. scrollback_delta = 0;
  1956. }
  1957. if (blank_timer_expired) {
  1958. do_blank_screen(0);
  1959. blank_timer_expired = 0;
  1960. }
  1961. release_console_sem();
  1962. }
  1963. void set_console(int nr)
  1964. {
  1965. want_console = nr;
  1966. schedule_console_callback();
  1967. }
  1968. struct tty_driver *console_driver;
  1969. #ifdef CONFIG_VT_CONSOLE
  1970. /*
  1971. * Console on virtual terminal
  1972. *
  1973. * The console must be locked when we get here.
  1974. */
  1975. static void vt_console_print(struct console *co, const char *b, unsigned count)
  1976. {
  1977. struct vc_data *vc = vc_cons[fg_console].d;
  1978. unsigned char c;
  1979. static unsigned long printing;
  1980. const ushort *start;
  1981. ushort cnt = 0;
  1982. ushort myx;
  1983. /* console busy or not yet initialized */
  1984. if (!printable || test_and_set_bit(0, &printing))
  1985. return;
  1986. if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
  1987. vc = vc_cons[kmsg_redirect - 1].d;
  1988. /* read `x' only after setting currcons properly (otherwise
  1989. the `x' macro will read the x of the foreground console). */
  1990. myx = vc->vc_x;
  1991. if (!vc_cons_allocated(fg_console)) {
  1992. /* impossible */
  1993. /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
  1994. goto quit;
  1995. }
  1996. if (vc->vc_mode != KD_TEXT)
  1997. goto quit;
  1998. /* undraw cursor first */
  1999. if (IS_FG(vc))
  2000. hide_cursor(vc);
  2001. start = (ushort *)vc->vc_pos;
  2002. /* Contrived structure to try to emulate original need_wrap behaviour
  2003. * Problems caused when we have need_wrap set on '\n' character */
  2004. while (count--) {
  2005. c = *b++;
  2006. if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
  2007. if (cnt > 0) {
  2008. if (CON_IS_VISIBLE(vc))
  2009. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  2010. vc->vc_x += cnt;
  2011. if (vc->vc_need_wrap)
  2012. vc->vc_x--;
  2013. cnt = 0;
  2014. }
  2015. if (c == 8) { /* backspace */
  2016. bs(vc);
  2017. start = (ushort *)vc->vc_pos;
  2018. myx = vc->vc_x;
  2019. continue;
  2020. }
  2021. if (c != 13)
  2022. lf(vc);
  2023. cr(vc);
  2024. start = (ushort *)vc->vc_pos;
  2025. myx = vc->vc_x;
  2026. if (c == 10 || c == 13)
  2027. continue;
  2028. }
  2029. scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
  2030. cnt++;
  2031. if (myx == vc->vc_cols - 1) {
  2032. vc->vc_need_wrap = 1;
  2033. continue;
  2034. }
  2035. vc->vc_pos += 2;
  2036. myx++;
  2037. }
  2038. if (cnt > 0) {
  2039. if (CON_IS_VISIBLE(vc))
  2040. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  2041. vc->vc_x += cnt;
  2042. if (vc->vc_x == vc->vc_cols) {
  2043. vc->vc_x--;
  2044. vc->vc_need_wrap = 1;
  2045. }
  2046. }
  2047. set_cursor(vc);
  2048. quit:
  2049. clear_bit(0, &printing);
  2050. }
  2051. static struct tty_driver *vt_console_device(struct console *c, int *index)
  2052. {
  2053. *index = c->index ? c->index-1 : fg_console;
  2054. return console_driver;
  2055. }
  2056. static struct console vt_console_driver = {
  2057. .name = "tty",
  2058. .write = vt_console_print,
  2059. .device = vt_console_device,
  2060. .unblank = unblank_screen,
  2061. .flags = CON_PRINTBUFFER,
  2062. .index = -1,
  2063. };
  2064. #endif
  2065. /*
  2066. * Handling of Linux-specific VC ioctls
  2067. */
  2068. /*
  2069. * Generally a bit racy with respect to console_sem().
  2070. *
  2071. * There are some functions which don't need it.
  2072. *
  2073. * There are some functions which can sleep for arbitrary periods
  2074. * (paste_selection) but we don't need the lock there anyway.
  2075. *
  2076. * set_selection has locking, and definitely needs it
  2077. */
  2078. int tioclinux(struct tty_struct *tty, unsigned long arg)
  2079. {
  2080. char type, data;
  2081. char __user *p = (char __user *)arg;
  2082. int lines;
  2083. int ret;
  2084. if (tty->driver->type != TTY_DRIVER_TYPE_CONSOLE)
  2085. return -EINVAL;
  2086. if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
  2087. return -EPERM;
  2088. if (get_user(type, p))
  2089. return -EFAULT;
  2090. ret = 0;
  2091. switch (type)
  2092. {
  2093. case TIOCL_SETSEL:
  2094. acquire_console_sem();
  2095. ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
  2096. release_console_sem();
  2097. break;
  2098. case TIOCL_PASTESEL:
  2099. ret = paste_selection(tty);
  2100. break;
  2101. case TIOCL_UNBLANKSCREEN:
  2102. acquire_console_sem();
  2103. unblank_screen();
  2104. release_console_sem();
  2105. break;
  2106. case TIOCL_SELLOADLUT:
  2107. ret = sel_loadlut(p);
  2108. break;
  2109. case TIOCL_GETSHIFTSTATE:
  2110. /*
  2111. * Make it possible to react to Shift+Mousebutton.
  2112. * Note that 'shift_state' is an undocumented
  2113. * kernel-internal variable; programs not closely
  2114. * related to the kernel should not use this.
  2115. */
  2116. data = shift_state;
  2117. ret = __put_user(data, p);
  2118. break;
  2119. case TIOCL_GETMOUSEREPORTING:
  2120. data = mouse_reporting();
  2121. ret = __put_user(data, p);
  2122. break;
  2123. case TIOCL_SETVESABLANK:
  2124. set_vesa_blanking(p);
  2125. break;
  2126. case TIOCL_SETKMSGREDIRECT:
  2127. if (!capable(CAP_SYS_ADMIN)) {
  2128. ret = -EPERM;
  2129. } else {
  2130. if (get_user(data, p+1))
  2131. ret = -EFAULT;
  2132. else
  2133. kmsg_redirect = data;
  2134. }
  2135. break;
  2136. case TIOCL_GETFGCONSOLE:
  2137. ret = fg_console;
  2138. break;
  2139. case TIOCL_SCROLLCONSOLE:
  2140. if (get_user(lines, (s32 __user *)(p+4))) {
  2141. ret = -EFAULT;
  2142. } else {
  2143. scrollfront(vc_cons[fg_console].d, lines);
  2144. ret = 0;
  2145. }
  2146. break;
  2147. case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
  2148. acquire_console_sem();
  2149. ignore_poke = 1;
  2150. do_blank_screen(0);
  2151. release_console_sem();
  2152. break;
  2153. case TIOCL_BLANKEDSCREEN:
  2154. ret = console_blanked;
  2155. break;
  2156. default:
  2157. ret = -EINVAL;
  2158. break;
  2159. }
  2160. return ret;
  2161. }
  2162. /*
  2163. * /dev/ttyN handling
  2164. */
  2165. static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  2166. {
  2167. int retval;
  2168. retval = do_con_write(tty, buf, count);
  2169. con_flush_chars(tty);
  2170. return retval;
  2171. }
  2172. static void con_put_char(struct tty_struct *tty, unsigned char ch)
  2173. {
  2174. if (in_interrupt())
  2175. return; /* n_r3964 calls put_char() from interrupt context */
  2176. do_con_write(tty, &ch, 1);
  2177. }
  2178. static int con_write_room(struct tty_struct *tty)
  2179. {
  2180. if (tty->stopped)
  2181. return 0;
  2182. return 4096; /* No limit, really; we're not buffering */
  2183. }
  2184. static int con_chars_in_buffer(struct tty_struct *tty)
  2185. {
  2186. return 0; /* we're not buffering */
  2187. }
  2188. /*
  2189. * con_throttle and con_unthrottle are only used for
  2190. * paste_selection(), which has to stuff in a large number of
  2191. * characters...
  2192. */
  2193. static void con_throttle(struct tty_struct *tty)
  2194. {
  2195. }
  2196. static void con_unthrottle(struct tty_struct *tty)
  2197. {
  2198. struct vc_data *vc = tty->driver_data;
  2199. wake_up_interruptible(&vc->paste_wait);
  2200. }
  2201. /*
  2202. * Turn the Scroll-Lock LED on when the tty is stopped
  2203. */
  2204. static void con_stop(struct tty_struct *tty)
  2205. {
  2206. int console_num;
  2207. if (!tty)
  2208. return;
  2209. console_num = tty->index;
  2210. if (!vc_cons_allocated(console_num))
  2211. return;
  2212. set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
  2213. set_leds();
  2214. }
  2215. /*
  2216. * Turn the Scroll-Lock LED off when the console is started
  2217. */
  2218. static void con_start(struct tty_struct *tty)
  2219. {
  2220. int console_num;
  2221. if (!tty)
  2222. return;
  2223. console_num = tty->index;
  2224. if (!vc_cons_allocated(console_num))
  2225. return;
  2226. clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
  2227. set_leds();
  2228. }
  2229. static void con_flush_chars(struct tty_struct *tty)
  2230. {
  2231. struct vc_data *vc;
  2232. if (in_interrupt()) /* from flush_to_ldisc */
  2233. return;
  2234. /* if we race with con_close(), vt may be null */
  2235. acquire_console_sem();
  2236. vc = tty->driver_data;
  2237. if (vc)
  2238. set_cursor(vc);
  2239. release_console_sem();
  2240. }
  2241. /*
  2242. * Allocate the console screen memory.
  2243. */
  2244. static int con_open(struct tty_struct *tty, struct file *filp)
  2245. {
  2246. unsigned int currcons = tty->index;
  2247. int ret = 0;
  2248. acquire_console_sem();
  2249. if (tty->driver_data == NULL) {
  2250. ret = vc_allocate(currcons);
  2251. if (ret == 0) {
  2252. struct vc_data *vc = vc_cons[currcons].d;
  2253. tty->driver_data = vc;
  2254. vc->vc_tty = tty;
  2255. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  2256. tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
  2257. tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
  2258. }
  2259. release_console_sem();
  2260. vcs_make_devfs(tty);
  2261. return ret;
  2262. }
  2263. }
  2264. release_console_sem();
  2265. return ret;
  2266. }
  2267. /*
  2268. * We take tty_sem in here to prevent another thread from coming in via init_dev
  2269. * and taking a ref against the tty while we're in the process of forgetting
  2270. * about it and cleaning things up.
  2271. *
  2272. * This is because vcs_remove_devfs() can sleep and will drop the BKL.
  2273. */
  2274. static void con_close(struct tty_struct *tty, struct file *filp)
  2275. {
  2276. down(&tty_sem);
  2277. acquire_console_sem();
  2278. if (tty && tty->count == 1) {
  2279. struct vc_data *vc = tty->driver_data;
  2280. if (vc)
  2281. vc->vc_tty = NULL;
  2282. tty->driver_data = NULL;
  2283. release_console_sem();
  2284. vcs_remove_devfs(tty);
  2285. up(&tty_sem);
  2286. /*
  2287. * tty_sem is released, but we still hold BKL, so there is
  2288. * still exclusion against init_dev()
  2289. */
  2290. return;
  2291. }
  2292. release_console_sem();
  2293. up(&tty_sem);
  2294. }
  2295. static void vc_init(struct vc_data *vc, unsigned int rows,
  2296. unsigned int cols, int do_clear)
  2297. {
  2298. int j, k ;
  2299. vc->vc_cols = cols;
  2300. vc->vc_rows = rows;
  2301. vc->vc_size_row = cols << 1;
  2302. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  2303. set_origin(vc);
  2304. vc->vc_pos = vc->vc_origin;
  2305. reset_vc(vc);
  2306. for (j=k=0; j<16; j++) {
  2307. vc->vc_palette[k++] = default_red[j] ;
  2308. vc->vc_palette[k++] = default_grn[j] ;
  2309. vc->vc_palette[k++] = default_blu[j] ;
  2310. }
  2311. vc->vc_def_color = 0x07; /* white */
  2312. vc->vc_ulcolor = 0x0f; /* bold white */
  2313. vc->vc_halfcolor = 0x08; /* grey */
  2314. init_waitqueue_head(&vc->paste_wait);
  2315. reset_terminal(vc, do_clear);
  2316. }
  2317. /*
  2318. * This routine initializes console interrupts, and does nothing
  2319. * else. If you want the screen to clear, call tty_write with
  2320. * the appropriate escape-sequence.
  2321. */
  2322. static int __init con_init(void)
  2323. {
  2324. const char *display_desc = NULL;
  2325. struct vc_data *vc;
  2326. unsigned int currcons = 0;
  2327. acquire_console_sem();
  2328. if (conswitchp)
  2329. display_desc = conswitchp->con_startup();
  2330. if (!display_desc) {
  2331. fg_console = 0;
  2332. release_console_sem();
  2333. return 0;
  2334. }
  2335. init_timer(&console_timer);
  2336. console_timer.function = blank_screen_t;
  2337. if (blankinterval) {
  2338. blank_state = blank_normal_wait;
  2339. mod_timer(&console_timer, jiffies + blankinterval);
  2340. }
  2341. /*
  2342. * kmalloc is not running yet - we use the bootmem allocator.
  2343. */
  2344. for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
  2345. vc_cons[currcons].d = vc = alloc_bootmem(sizeof(struct vc_data));
  2346. visual_init(vc, currcons, 1);
  2347. vc->vc_screenbuf = (unsigned short *)alloc_bootmem(vc->vc_screenbuf_size);
  2348. vc->vc_kmalloced = 0;
  2349. vc_init(vc, vc->vc_rows, vc->vc_cols,
  2350. currcons || !vc->vc_sw->con_save_screen);
  2351. }
  2352. currcons = fg_console = 0;
  2353. master_display_fg = vc = vc_cons[currcons].d;
  2354. set_origin(vc);
  2355. save_screen(vc);
  2356. gotoxy(vc, vc->vc_x, vc->vc_y);
  2357. csi_J(vc, 0);
  2358. update_screen(vc);
  2359. printk("Console: %s %s %dx%d",
  2360. vc->vc_can_do_color ? "colour" : "mono",
  2361. display_desc, vc->vc_cols, vc->vc_rows);
  2362. printable = 1;
  2363. printk("\n");
  2364. release_console_sem();
  2365. #ifdef CONFIG_VT_CONSOLE
  2366. register_console(&vt_console_driver);
  2367. #endif
  2368. return 0;
  2369. }
  2370. console_initcall(con_init);
  2371. static struct tty_operations con_ops = {
  2372. .open = con_open,
  2373. .close = con_close,
  2374. .write = con_write,
  2375. .write_room = con_write_room,
  2376. .put_char = con_put_char,
  2377. .flush_chars = con_flush_chars,
  2378. .chars_in_buffer = con_chars_in_buffer,
  2379. .ioctl = vt_ioctl,
  2380. .stop = con_stop,
  2381. .start = con_start,
  2382. .throttle = con_throttle,
  2383. .unthrottle = con_unthrottle,
  2384. };
  2385. int __init vty_init(void)
  2386. {
  2387. vcs_init();
  2388. console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
  2389. if (!console_driver)
  2390. panic("Couldn't allocate console driver\n");
  2391. console_driver->owner = THIS_MODULE;
  2392. console_driver->devfs_name = "vc/";
  2393. console_driver->name = "tty";
  2394. console_driver->name_base = 1;
  2395. console_driver->major = TTY_MAJOR;
  2396. console_driver->minor_start = 1;
  2397. console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  2398. console_driver->init_termios = tty_std_termios;
  2399. console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  2400. tty_set_operations(console_driver, &con_ops);
  2401. if (tty_register_driver(console_driver))
  2402. panic("Couldn't register console driver\n");
  2403. kbd_init();
  2404. console_map_init();
  2405. #ifdef CONFIG_PROM_CONSOLE
  2406. prom_con_init();
  2407. #endif
  2408. #ifdef CONFIG_MDA_CONSOLE
  2409. mda_console_init();
  2410. #endif
  2411. return 0;
  2412. }
  2413. #ifndef VT_SINGLE_DRIVER
  2414. /*
  2415. * If we support more console drivers, this function is used
  2416. * when a driver wants to take over some existing consoles
  2417. * and become default driver for newly opened ones.
  2418. */
  2419. int take_over_console(const struct consw *csw, int first, int last, int deflt)
  2420. {
  2421. int i, j = -1;
  2422. const char *desc;
  2423. struct module *owner;
  2424. owner = csw->owner;
  2425. if (!try_module_get(owner))
  2426. return -ENODEV;
  2427. acquire_console_sem();
  2428. desc = csw->con_startup();
  2429. if (!desc) {
  2430. release_console_sem();
  2431. module_put(owner);
  2432. return -ENODEV;
  2433. }
  2434. if (deflt) {
  2435. if (conswitchp)
  2436. module_put(conswitchp->owner);
  2437. __module_get(owner);
  2438. conswitchp = csw;
  2439. }
  2440. for (i = first; i <= last; i++) {
  2441. int old_was_color;
  2442. struct vc_data *vc = vc_cons[i].d;
  2443. if (con_driver_map[i])
  2444. module_put(con_driver_map[i]->owner);
  2445. __module_get(owner);
  2446. con_driver_map[i] = csw;
  2447. if (!vc || !vc->vc_sw)
  2448. continue;
  2449. j = i;
  2450. if (CON_IS_VISIBLE(vc))
  2451. save_screen(vc);
  2452. old_was_color = vc->vc_can_do_color;
  2453. vc->vc_sw->con_deinit(vc);
  2454. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  2455. vc->vc_visible_origin = vc->vc_origin;
  2456. vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
  2457. vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
  2458. visual_init(vc, i, 0);
  2459. update_attr(vc);
  2460. /* If the console changed between mono <-> color, then
  2461. * the attributes in the screenbuf will be wrong. The
  2462. * following resets all attributes to something sane.
  2463. */
  2464. if (old_was_color != vc->vc_can_do_color)
  2465. clear_buffer_attributes(vc);
  2466. if (CON_IS_VISIBLE(vc))
  2467. update_screen(vc);
  2468. }
  2469. printk("Console: switching ");
  2470. if (!deflt)
  2471. printk("consoles %d-%d ", first+1, last+1);
  2472. if (j >= 0)
  2473. printk("to %s %s %dx%d\n",
  2474. vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
  2475. desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
  2476. else
  2477. printk("to %s\n", desc);
  2478. release_console_sem();
  2479. module_put(owner);
  2480. return 0;
  2481. }
  2482. void give_up_console(const struct consw *csw)
  2483. {
  2484. int i;
  2485. for(i = 0; i < MAX_NR_CONSOLES; i++)
  2486. if (con_driver_map[i] == csw) {
  2487. module_put(csw->owner);
  2488. con_driver_map[i] = NULL;
  2489. }
  2490. }
  2491. #endif
  2492. /*
  2493. * Screen blanking
  2494. */
  2495. static void set_vesa_blanking(char __user *p)
  2496. {
  2497. unsigned int mode;
  2498. get_user(mode, p + 1);
  2499. vesa_blank_mode = (mode < 4) ? mode : 0;
  2500. }
  2501. /*
  2502. * This is called by a timer handler
  2503. */
  2504. static void vesa_powerdown(void)
  2505. {
  2506. struct vc_data *c = vc_cons[fg_console].d;
  2507. /*
  2508. * Power down if currently suspended (1 or 2),
  2509. * suspend if currently blanked (0),
  2510. * else do nothing (i.e. already powered down (3)).
  2511. * Called only if powerdown features are allowed.
  2512. */
  2513. switch (vesa_blank_mode) {
  2514. case VESA_NO_BLANKING:
  2515. c->vc_sw->con_blank(c, VESA_VSYNC_SUSPEND+1, 0);
  2516. break;
  2517. case VESA_VSYNC_SUSPEND:
  2518. case VESA_HSYNC_SUSPEND:
  2519. c->vc_sw->con_blank(c, VESA_POWERDOWN+1, 0);
  2520. break;
  2521. }
  2522. }
  2523. void do_blank_screen(int entering_gfx)
  2524. {
  2525. struct vc_data *vc = vc_cons[fg_console].d;
  2526. int i;
  2527. WARN_CONSOLE_UNLOCKED();
  2528. if (console_blanked) {
  2529. if (blank_state == blank_vesa_wait) {
  2530. blank_state = blank_off;
  2531. vesa_powerdown();
  2532. }
  2533. return;
  2534. }
  2535. if (blank_state != blank_normal_wait)
  2536. return;
  2537. blank_state = blank_off;
  2538. /* entering graphics mode? */
  2539. if (entering_gfx) {
  2540. hide_cursor(vc);
  2541. save_screen(vc);
  2542. vc->vc_sw->con_blank(vc, -1, 1);
  2543. console_blanked = fg_console + 1;
  2544. set_origin(vc);
  2545. return;
  2546. }
  2547. /* don't blank graphics */
  2548. if (vc->vc_mode != KD_TEXT) {
  2549. console_blanked = fg_console + 1;
  2550. return;
  2551. }
  2552. hide_cursor(vc);
  2553. del_timer_sync(&console_timer);
  2554. blank_timer_expired = 0;
  2555. save_screen(vc);
  2556. /* In case we need to reset origin, blanking hook returns 1 */
  2557. i = vc->vc_sw->con_blank(vc, 1, 0);
  2558. console_blanked = fg_console + 1;
  2559. if (i)
  2560. set_origin(vc);
  2561. if (console_blank_hook && console_blank_hook(1))
  2562. return;
  2563. if (vesa_off_interval) {
  2564. blank_state = blank_vesa_wait;
  2565. mod_timer(&console_timer, jiffies + vesa_off_interval);
  2566. }
  2567. if (vesa_blank_mode)
  2568. vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
  2569. }
  2570. EXPORT_SYMBOL(do_blank_screen);
  2571. /*
  2572. * Called by timer as well as from vt_console_driver
  2573. */
  2574. void do_unblank_screen(int leaving_gfx)
  2575. {
  2576. struct vc_data *vc;
  2577. /* This should now always be called from a "sane" (read: can schedule)
  2578. * context for the sake of the low level drivers, except in the special
  2579. * case of oops_in_progress
  2580. */
  2581. if (!oops_in_progress)
  2582. might_sleep();
  2583. WARN_CONSOLE_UNLOCKED();
  2584. ignore_poke = 0;
  2585. if (!console_blanked)
  2586. return;
  2587. if (!vc_cons_allocated(fg_console)) {
  2588. /* impossible */
  2589. printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
  2590. return;
  2591. }
  2592. vc = vc_cons[fg_console].d;
  2593. if (vc->vc_mode != KD_TEXT)
  2594. return; /* but leave console_blanked != 0 */
  2595. if (blankinterval) {
  2596. mod_timer(&console_timer, jiffies + blankinterval);
  2597. blank_state = blank_normal_wait;
  2598. }
  2599. console_blanked = 0;
  2600. if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
  2601. /* Low-level driver cannot restore -> do it ourselves */
  2602. update_screen(vc);
  2603. if (console_blank_hook)
  2604. console_blank_hook(0);
  2605. set_palette(vc);
  2606. set_cursor(vc);
  2607. }
  2608. EXPORT_SYMBOL(do_unblank_screen);
  2609. /*
  2610. * This is called by the outside world to cause a forced unblank, mostly for
  2611. * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
  2612. * call it with 1 as an argument and so force a mode restore... that may kill
  2613. * X or at least garbage the screen but would also make the Oops visible...
  2614. */
  2615. void unblank_screen(void)
  2616. {
  2617. do_unblank_screen(0);
  2618. }
  2619. /*
  2620. * We defer the timer blanking to work queue so it can take the console semaphore
  2621. * (console operations can still happen at irq time, but only from printk which
  2622. * has the console semaphore. Not perfect yet, but better than no locking
  2623. */
  2624. static void blank_screen_t(unsigned long dummy)
  2625. {
  2626. if (unlikely(!keventd_up())) {
  2627. mod_timer(&console_timer, jiffies + blankinterval);
  2628. return;
  2629. }
  2630. blank_timer_expired = 1;
  2631. schedule_work(&console_work);
  2632. }
  2633. void poke_blanked_console(void)
  2634. {
  2635. WARN_CONSOLE_UNLOCKED();
  2636. /* Add this so we quickly catch whoever might call us in a non
  2637. * safe context. Nowadays, unblank_screen() isn't to be called in
  2638. * atomic contexts and is allowed to schedule (with the special case
  2639. * of oops_in_progress, but that isn't of any concern for this
  2640. * function. --BenH.
  2641. */
  2642. might_sleep();
  2643. /* This isn't perfectly race free, but a race here would be mostly harmless,
  2644. * at worse, we'll do a spurrious blank and it's unlikely
  2645. */
  2646. del_timer(&console_timer);
  2647. blank_timer_expired = 0;
  2648. if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
  2649. return;
  2650. if (console_blanked)
  2651. unblank_screen();
  2652. else if (blankinterval) {
  2653. mod_timer(&console_timer, jiffies + blankinterval);
  2654. blank_state = blank_normal_wait;
  2655. }
  2656. }
  2657. /*
  2658. * Palettes
  2659. */
  2660. static void set_palette(struct vc_data *vc)
  2661. {
  2662. WARN_CONSOLE_UNLOCKED();
  2663. if (vc->vc_mode != KD_GRAPHICS)
  2664. vc->vc_sw->con_set_palette(vc, color_table);
  2665. }
  2666. static int set_get_cmap(unsigned char __user *arg, int set)
  2667. {
  2668. int i, j, k;
  2669. WARN_CONSOLE_UNLOCKED();
  2670. for (i = 0; i < 16; i++)
  2671. if (set) {
  2672. get_user(default_red[i], arg++);
  2673. get_user(default_grn[i], arg++);
  2674. get_user(default_blu[i], arg++);
  2675. } else {
  2676. put_user(default_red[i], arg++);
  2677. put_user(default_grn[i], arg++);
  2678. put_user(default_blu[i], arg++);
  2679. }
  2680. if (set) {
  2681. for (i = 0; i < MAX_NR_CONSOLES; i++)
  2682. if (vc_cons_allocated(i)) {
  2683. for (j = k = 0; j < 16; j++) {
  2684. vc_cons[i].d->vc_palette[k++] = default_red[j];
  2685. vc_cons[i].d->vc_palette[k++] = default_grn[j];
  2686. vc_cons[i].d->vc_palette[k++] = default_blu[j];
  2687. }
  2688. set_palette(vc_cons[i].d);
  2689. }
  2690. }
  2691. return 0;
  2692. }
  2693. /*
  2694. * Load palette into the DAC registers. arg points to a colour
  2695. * map, 3 bytes per colour, 16 colours, range from 0 to 255.
  2696. */
  2697. int con_set_cmap(unsigned char __user *arg)
  2698. {
  2699. int rc;
  2700. acquire_console_sem();
  2701. rc = set_get_cmap (arg,1);
  2702. release_console_sem();
  2703. return rc;
  2704. }
  2705. int con_get_cmap(unsigned char __user *arg)
  2706. {
  2707. int rc;
  2708. acquire_console_sem();
  2709. rc = set_get_cmap (arg,0);
  2710. release_console_sem();
  2711. return rc;
  2712. }
  2713. void reset_palette(struct vc_data *vc)
  2714. {
  2715. int j, k;
  2716. for (j=k=0; j<16; j++) {
  2717. vc->vc_palette[k++] = default_red[j];
  2718. vc->vc_palette[k++] = default_grn[j];
  2719. vc->vc_palette[k++] = default_blu[j];
  2720. }
  2721. set_palette(vc);
  2722. }
  2723. /*
  2724. * Font switching
  2725. *
  2726. * Currently we only support fonts up to 32 pixels wide, at a maximum height
  2727. * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
  2728. * depending on width) reserved for each character which is kinda wasty, but
  2729. * this is done in order to maintain compatibility with the EGA/VGA fonts. It
  2730. * is upto the actual low-level console-driver convert data into its favorite
  2731. * format (maybe we should add a `fontoffset' field to the `display'
  2732. * structure so we won't have to convert the fontdata all the time.
  2733. * /Jes
  2734. */
  2735. #define max_font_size 65536
  2736. static int con_font_get(struct vc_data *vc, struct console_font_op *op)
  2737. {
  2738. struct console_font font;
  2739. int rc = -EINVAL;
  2740. int c;
  2741. if (vc->vc_mode != KD_TEXT)
  2742. return -EINVAL;
  2743. if (op->data) {
  2744. font.data = kmalloc(max_font_size, GFP_KERNEL);
  2745. if (!font.data)
  2746. return -ENOMEM;
  2747. } else
  2748. font.data = NULL;
  2749. acquire_console_sem();
  2750. if (vc->vc_sw->con_font_get)
  2751. rc = vc->vc_sw->con_font_get(vc, &font);
  2752. else
  2753. rc = -ENOSYS;
  2754. release_console_sem();
  2755. if (rc)
  2756. goto out;
  2757. c = (font.width+7)/8 * 32 * font.charcount;
  2758. if (op->data && font.charcount > op->charcount)
  2759. rc = -ENOSPC;
  2760. if (!(op->flags & KD_FONT_FLAG_OLD)) {
  2761. if (font.width > op->width || font.height > op->height)
  2762. rc = -ENOSPC;
  2763. } else {
  2764. if (font.width != 8)
  2765. rc = -EIO;
  2766. else if ((op->height && font.height > op->height) ||
  2767. font.height > 32)
  2768. rc = -ENOSPC;
  2769. }
  2770. if (rc)
  2771. goto out;
  2772. op->height = font.height;
  2773. op->width = font.width;
  2774. op->charcount = font.charcount;
  2775. if (op->data && copy_to_user(op->data, font.data, c))
  2776. rc = -EFAULT;
  2777. out:
  2778. kfree(font.data);
  2779. return rc;
  2780. }
  2781. static int con_font_set(struct vc_data *vc, struct console_font_op *op)
  2782. {
  2783. struct console_font font;
  2784. int rc = -EINVAL;
  2785. int size;
  2786. if (vc->vc_mode != KD_TEXT)
  2787. return -EINVAL;
  2788. if (!op->data)
  2789. return -EINVAL;
  2790. if (op->charcount > 512)
  2791. return -EINVAL;
  2792. if (!op->height) { /* Need to guess font height [compat] */
  2793. int h, i;
  2794. u8 __user *charmap = op->data;
  2795. u8 tmp;
  2796. /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
  2797. so that we can get rid of this soon */
  2798. if (!(op->flags & KD_FONT_FLAG_OLD))
  2799. return -EINVAL;
  2800. for (h = 32; h > 0; h--)
  2801. for (i = 0; i < op->charcount; i++) {
  2802. if (get_user(tmp, &charmap[32*i+h-1]))
  2803. return -EFAULT;
  2804. if (tmp)
  2805. goto nonzero;
  2806. }
  2807. return -EINVAL;
  2808. nonzero:
  2809. op->height = h;
  2810. }
  2811. if (op->width <= 0 || op->width > 32 || op->height > 32)
  2812. return -EINVAL;
  2813. size = (op->width+7)/8 * 32 * op->charcount;
  2814. if (size > max_font_size)
  2815. return -ENOSPC;
  2816. font.charcount = op->charcount;
  2817. font.height = op->height;
  2818. font.width = op->width;
  2819. font.data = kmalloc(size, GFP_KERNEL);
  2820. if (!font.data)
  2821. return -ENOMEM;
  2822. if (copy_from_user(font.data, op->data, size)) {
  2823. kfree(font.data);
  2824. return -EFAULT;
  2825. }
  2826. acquire_console_sem();
  2827. if (vc->vc_sw->con_font_set)
  2828. rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
  2829. else
  2830. rc = -ENOSYS;
  2831. release_console_sem();
  2832. kfree(font.data);
  2833. return rc;
  2834. }
  2835. static int con_font_default(struct vc_data *vc, struct console_font_op *op)
  2836. {
  2837. struct console_font font = {.width = op->width, .height = op->height};
  2838. char name[MAX_FONT_NAME];
  2839. char *s = name;
  2840. int rc;
  2841. if (vc->vc_mode != KD_TEXT)
  2842. return -EINVAL;
  2843. if (!op->data)
  2844. s = NULL;
  2845. else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
  2846. return -EFAULT;
  2847. else
  2848. name[MAX_FONT_NAME - 1] = 0;
  2849. acquire_console_sem();
  2850. if (vc->vc_sw->con_font_default)
  2851. rc = vc->vc_sw->con_font_default(vc, &font, s);
  2852. else
  2853. rc = -ENOSYS;
  2854. release_console_sem();
  2855. if (!rc) {
  2856. op->width = font.width;
  2857. op->height = font.height;
  2858. }
  2859. return rc;
  2860. }
  2861. static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
  2862. {
  2863. int con = op->height;
  2864. int rc;
  2865. if (vc->vc_mode != KD_TEXT)
  2866. return -EINVAL;
  2867. acquire_console_sem();
  2868. if (!vc->vc_sw->con_font_copy)
  2869. rc = -ENOSYS;
  2870. else if (con < 0 || !vc_cons_allocated(con))
  2871. rc = -ENOTTY;
  2872. else if (con == vc->vc_num) /* nothing to do */
  2873. rc = 0;
  2874. else
  2875. rc = vc->vc_sw->con_font_copy(vc, con);
  2876. release_console_sem();
  2877. return rc;
  2878. }
  2879. int con_font_op(struct vc_data *vc, struct console_font_op *op)
  2880. {
  2881. switch (op->op) {
  2882. case KD_FONT_OP_SET:
  2883. return con_font_set(vc, op);
  2884. case KD_FONT_OP_GET:
  2885. return con_font_get(vc, op);
  2886. case KD_FONT_OP_SET_DEFAULT:
  2887. return con_font_default(vc, op);
  2888. case KD_FONT_OP_COPY:
  2889. return con_font_copy(vc, op);
  2890. }
  2891. return -ENOSYS;
  2892. }
  2893. /*
  2894. * Interface exported to selection and vcs.
  2895. */
  2896. /* used by selection */
  2897. u16 screen_glyph(struct vc_data *vc, int offset)
  2898. {
  2899. u16 w = scr_readw(screenpos(vc, offset, 1));
  2900. u16 c = w & 0xff;
  2901. if (w & vc->vc_hi_font_mask)
  2902. c |= 0x100;
  2903. return c;
  2904. }
  2905. /* used by vcs - note the word offset */
  2906. unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
  2907. {
  2908. return screenpos(vc, 2 * w_offset, viewed);
  2909. }
  2910. void getconsxy(struct vc_data *vc, unsigned char *p)
  2911. {
  2912. p[0] = vc->vc_x;
  2913. p[1] = vc->vc_y;
  2914. }
  2915. void putconsxy(struct vc_data *vc, unsigned char *p)
  2916. {
  2917. gotoxy(vc, p[0], p[1]);
  2918. set_cursor(vc);
  2919. }
  2920. u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
  2921. {
  2922. if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
  2923. return softcursor_original;
  2924. return scr_readw(org);
  2925. }
  2926. void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
  2927. {
  2928. scr_writew(val, org);
  2929. if ((unsigned long)org == vc->vc_pos) {
  2930. softcursor_original = -1;
  2931. add_softcursor(vc);
  2932. }
  2933. }
  2934. /*
  2935. * Visible symbols for modules
  2936. */
  2937. EXPORT_SYMBOL(color_table);
  2938. EXPORT_SYMBOL(default_red);
  2939. EXPORT_SYMBOL(default_grn);
  2940. EXPORT_SYMBOL(default_blu);
  2941. EXPORT_SYMBOL(update_region);
  2942. EXPORT_SYMBOL(redraw_screen);
  2943. EXPORT_SYMBOL(vc_resize);
  2944. EXPORT_SYMBOL(fg_console);
  2945. EXPORT_SYMBOL(console_blank_hook);
  2946. EXPORT_SYMBOL(console_blanked);
  2947. EXPORT_SYMBOL(vc_cons);
  2948. #ifndef VT_SINGLE_DRIVER
  2949. EXPORT_SYMBOL(take_over_console);
  2950. EXPORT_SYMBOL(give_up_console);
  2951. #endif