1 /** @file
2   Internal include file of the VGA Class Driver.
3 
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution.  The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9 
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 
16 #ifndef _VGA_CLASS_H__
17 #define _VGA_CLASS_H__
18 
19 #include <FrameworkDxe.h>
20 
21 #include <Protocol/SimpleTextOut.h>
22 #include <Protocol/PciIo.h>
23 #include <Protocol/VgaMiniPort.h>
24 #include <Protocol/DevicePath.h>
25 
26 #include <Library/DebugLib.h>
27 #include <Library/UefiDriverEntryPoint.h>
28 #include <Library/UefiLib.h>
29 #include <Library/MemoryAllocationLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/ReportStatusCodeLib.h>
32 
33 #include <IndustryStandard/Pci.h>
34 
35 //
36 // Global Variables
37 //
38 extern EFI_DRIVER_BINDING_PROTOCOL   gVgaClassDriverBinding;
39 extern EFI_COMPONENT_NAME_PROTOCOL   gVgaClassComponentName;
40 extern EFI_COMPONENT_NAME2_PROTOCOL  gVgaClassComponentName2;
41 
42 
43 //
44 // Structure for tuple containing mapping among uniocde, PC Ansi and ASCII code.
45 //
46 typedef struct {
47   CHAR16  Unicode;
48   CHAR8   PcAnsi;
49   CHAR8   Ascii;
50 } UNICODE_TO_CHAR;
51 
52 //
53 // VGA specific registers
54 //
55 #define CRTC_CURSOR_START         0xA
56 #define CRTC_CURSOR_END           0xB
57 
58 #define CRTC_CURSOR_LOCATION_HIGH 0xE
59 #define CRTC_CURSOR_LOCATION_LOW  0xF
60 
61 #define EFI_MAX_ATTRIBUTE         0x7f
62 
63 //
64 // VGA Class Device Structure
65 //
66 #define VGA_CLASS_DEV_SIGNATURE SIGNATURE_32 ('V', 'G', 'A', 'C')
67 
68 typedef struct {
69   UINTN                            Signature;
70   EFI_HANDLE                       Handle;
71   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  SimpleTextOut;
72   EFI_SIMPLE_TEXT_OUTPUT_MODE      SimpleTextOutputMode;
73   EFI_VGA_MINI_PORT_PROTOCOL       *VgaMiniPort;
74   EFI_PCI_IO_PROTOCOL              *PciIo;
75   EFI_DEVICE_PATH_PROTOCOL         *DevicePath;
76 } VGA_CLASS_DEV;
77 
78 #define VGA_CLASS_DEV_FROM_THIS(a)  CR (a, VGA_CLASS_DEV, SimpleTextOut, VGA_CLASS_DEV_SIGNATURE)
79 
80 //
81 // Driver Binding Protocol functions
82 //
83 
84 /**
85   Tests to see if this driver supports a given controller.
86 
87   This function implments EFI_DRIVER_BINDING_PROTOCOL.Supported().
88   It Checks if this driver supports the controller specified. Any Controller
89   with VgaMiniPort Protocol and Pci I/O protocol can be supported.
90 
91   @param  This                A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
92   @param  ControllerHandle    Handle of device to test
93   @param  RemainingDevicePath Optional parameter use to pick a specific child
94                               device to start.
95 
96   @retval EFI_SUCCESS         This driver supports this device.
97   @retval EFI_ALREADY_STARTED This driver is already running on this device.
98   @retval EFI_UNSUPPORTED     This driver does not support this device.
99 
100 **/
101 EFI_STATUS
102 EFIAPI
103 VgaClassDriverBindingSupported (
104   IN EFI_DRIVER_BINDING_PROTOCOL  *This,
105   IN EFI_HANDLE                   Controller,
106   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL
107   );
108 
109 /**
110   Starts the device controller.
111 
112   This function implments EFI_DRIVER_BINDING_PROTOCOL.Start().
113   It starts the device specified by Controller with the driver based on PCI I/O Protocol
114   and VgaMiniPort Protocol. It creates context for device instance and install EFI_SIMPLE_TEXT_OUT_PROTOCOL.
115 
116   @param  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
117   @param  ControllerHandle     Handle of device to bind driver to
118   @param  RemainingDevicePath  Optional parameter use to pick a specific child
119                                device to start.
120 
121   @retval EFI_SUCCESS          The device was started.
122   @retval other                Fail to start the device.
123 
124 **/
125 EFI_STATUS
126 EFIAPI
127 VgaClassDriverBindingStart (
128   IN EFI_DRIVER_BINDING_PROTOCOL  *This,
129   IN EFI_HANDLE                   Controller,
130   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL
131   );
132 
133 /**
134   Starts the device controller.
135 
136   This function implments EFI_DRIVER_BINDING_PROTOCOL.Stop().
137   It stops this driver on Controller. Support stoping any child handles
138   created by this driver.
139 
140   @param  This              A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
141   @param  ControllerHandle  A handle to the device being stopped.
142   @param  NumberOfChildren  The number of child device handles in ChildHandleBuffer.
143   @param  ChildHandleBuffer An array of child handles to be freed.
144 
145   @retval EFI_SUCCESS       This driver is removed ControllerHandle
146   @retval other             This driver was not removed from this device
147 
148 **/
149 EFI_STATUS
150 EFIAPI
151 VgaClassDriverBindingStop (
152   IN  EFI_DRIVER_BINDING_PROTOCOL     *This,
153   IN  EFI_HANDLE                      Controller,
154   IN  UINTN                           NumberOfChildren,
155   IN  EFI_HANDLE                      *ChildHandleBuffer OPTIONAL
156   );
157 
158 //
159 // EFI Component Name Functions
160 //
161 
162 /**
163   Retrieves a Unicode string that is the user readable name of the driver.
164 
165   This function retrieves the user readable name of a driver in the form of a
166   Unicode string. If the driver specified by This has a user readable name in
167   the language specified by Language, then a pointer to the driver name is
168   returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
169   by This does not support the language specified by Language,
170   then EFI_UNSUPPORTED is returned.
171 
172   @param  This                  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
173                                 EFI_COMPONENT_NAME_PROTOCOL instance.
174   @param  Language              A pointer to a Null-terminated ASCII string
175                                 array indicating the language. This is the
176                                 language of the driver name that the caller is
177                                 requesting, and it must match one of the
178                                 languages specified in SupportedLanguages. The
179                                 number of languages supported by a driver is up
180                                 to the driver writer. Language is specified
181                                 in RFC 4646 or ISO 639-2 language code format.
182   @param  DriverName            A pointer to the Unicode string to return.
183                                 This Unicode string is the name of the
184                                 driver specified by This in the language
185                                 specified by Language.
186 
187   @retval EFI_SUCCESS           The Unicode string for the Driver specified by
188                                 This and the language specified by Language was
189                                 returned in DriverName.
190   @retval EFI_INVALID_PARAMETER Language is NULL.
191   @retval EFI_INVALID_PARAMETER DriverName is NULL.
192   @retval EFI_UNSUPPORTED       The driver specified by This does not support
193                                 the language specified by Language.
194 
195 **/
196 EFI_STATUS
197 EFIAPI
198 VgaClassComponentNameGetDriverName (
199   IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
200   IN  CHAR8                        *Language,
201   OUT CHAR16                       **DriverName
202   );
203 
204 /**
205   Retrieves a Unicode string that is the user readable name of the controller
206   that is being managed by a driver.
207 
208   This function retrieves the user readable name of the controller specified by
209   ControllerHandle and ChildHandle in the form of a Unicode string. If the
210   driver specified by This has a user readable name in the language specified by
211   Language, then a pointer to the controller name is returned in ControllerName,
212   and EFI_SUCCESS is returned.  If the driver specified by This is not currently
213   managing the controller specified by ControllerHandle and ChildHandle,
214   then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
215   support the language specified by Language, then EFI_UNSUPPORTED is returned.
216 
217   @param  This                  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
218                                 EFI_COMPONENT_NAME_PROTOCOL instance.
219   @param  ControllerHandle      The handle of a controller that the driver
220                                 specified by This is managing.  This handle
221                                 specifies the controller whose name is to be
222                                 returned.
223   @param  ChildHandle           The handle of the child controller to retrieve
224                                 the name of.  This is an optional parameter that
225                                 may be NULL.  It will be NULL for device
226                                 drivers.  It will also be NULL for a bus drivers
227                                 that wish to retrieve the name of the bus
228                                 controller.  It will not be NULL for a bus
229                                 driver that wishes to retrieve the name of a
230                                 child controller.
231   @param  Language              A pointer to a Null-terminated ASCII string
232                                 array indicating the language.  This is the
233                                 language of the driver name that the caller is
234                                 requesting, and it must match one of the
235                                 languages specified in SupportedLanguages. The
236                                 number of languages supported by a driver is up
237                                 to the driver writer. Language is specified in
238                                 RFC 4646 or ISO 639-2 language code format.
239   @param  ControllerName        A pointer to the Unicode string to return.
240                                 This Unicode string is the name of the
241                                 controller specified by ControllerHandle and
242                                 ChildHandle in the language specified by
243                                 Language from the point of view of the driver
244                                 specified by This.
245 
246   @retval EFI_SUCCESS           The Unicode string for the user readable name in
247                                 the language specified by Language for the
248                                 driver specified by This was returned in
249                                 DriverName.
250   @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
251   @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
252                                 EFI_HANDLE.
253   @retval EFI_INVALID_PARAMETER Language is NULL.
254   @retval EFI_INVALID_PARAMETER ControllerName is NULL.
255   @retval EFI_UNSUPPORTED       The driver specified by This is not currently
256                                 managing the controller specified by
257                                 ControllerHandle and ChildHandle.
258   @retval EFI_UNSUPPORTED       The driver specified by This does not support
259                                 the language specified by Language.
260 
261 **/
262 EFI_STATUS
263 EFIAPI
264 VgaClassComponentNameGetControllerName (
265   IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,
266   IN  EFI_HANDLE                                      ControllerHandle,
267   IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,
268   IN  CHAR8                                           *Language,
269   OUT CHAR16                                          **ControllerName
270   );
271 
272 //
273 // Simple Text Output Protocol functions
274 //
275 /**
276   Resets the text output device hardware.
277 
278   This function implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset().
279   It resets the text output device hardware. The cursor position is set to (0, 0),
280   and the screen is cleared to the default background color for the output device.
281 
282   @param  This                 Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL instance.
283   @param  ExtendedVerification Indicates that the driver may perform a more exhaustive
284                                verification operation of the device during reset.
285 
286   @retval EFI_SUCCESS          The text output device was reset.
287   @retval EFI_DEVICE_ERROR     The text output device is not functioning correctly and could not be reset.
288 
289 **/
290 EFI_STATUS
291 EFIAPI
292 VgaClassReset (
293   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL     *This,
294   IN  BOOLEAN                             ExtendedVerification
295   );
296 
297 /**
298   Writes a Unicode string to the output device.
299 
300   This function implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString().
301   It writes a Unicode string to the output device. This is the most basic output mechanism
302   on an output device.
303 
304   @param  This                   Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL instance.
305   @param  String                 The Null-terminated Unicode string to be displayed on the output device(s).
306 
307   @retval EFI_SUCCESS            The string was output to the device.
308   @retval EFI_DEVICE_ERROR       The device reported an error while attempting to output the text.
309   @retval EFI_UNSUPPORTED        The output device's mode is not currently in a defined text mode.
310   @retval EFI_WARN_UNKNOWN_GLYPH This warning code indicates that some of the characters in
311                                  the Unicode string could not be rendered and were skipped.
312 **/
313 EFI_STATUS
314 EFIAPI
315 VgaClassOutputString (
316   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
317   IN  CHAR16                          *String
318   );
319 
320 /**
321   Verifies that all characters in a Unicode string can be output to the target device.
322 
323   This function implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.TestString().
324   It verifies that all characters in a Unicode string can be output to the target device.
325 
326   @param  This                   Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL instance.
327   @param  String                 The Null-terminated Unicode string to be examined for the output device(s).
328 
329   @retval EFI_SUCCESS            The device(s) are capable of rendering the output string.
330   @retval EFI_UNSUPPORTED        Some of the characters in the Unicode string cannot be rendered by
331                                  one or more of the output devices mapped by the EFI handle.
332 
333 **/
334 EFI_STATUS
335 EFIAPI
336 VgaClassTestString (
337   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
338   IN  CHAR16                          *String
339   );
340 
341 /**
342   Clears the output device(s) display to the currently selected background color.
343 
344   This function implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.ClearScreen().
345   The ClearScreen() function clears the output device(s) display to the currently
346   selected background color. The cursor position is set to (0, 0).
347 
348   @param  This                   Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL instance.
349 
350   @retval EFI_SUCESS             The operation completed successfully.
351   @retval EFI_DEVICE_ERROR       The device had an error and could not complete the request.
352   @retval EFI_UNSUPPORTED        The output device is not in a valid text mode.
353 
354 **/
355 EFI_STATUS
356 EFIAPI
357 VgaClassClearScreen (
358   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL    *This
359   );
360 
361 /**
362   Sets the background and foreground colors for theOutputString() and ClearScreen() functions.
363 
364   This function implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute().
365   It sets the background and foreground colors for the OutputString() and ClearScreen() functions.
366   The color mask can be set even when the device is in an invalid text mode.
367   Devices supporting a different number of text colors are required to emulate the above colors
368   to the best of the device's capabilities.
369 
370   @param  This                   Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL instance.
371   @param  Attribute              The attribute to set.
372                                  Bits 0..3 are the foreground color,
373                                  and bits 4..6 are the background color.
374 
375   @retval EFI_SUCCESS            The requested attributes were set.
376   @retval EFI_DEVICE_ERROR       The device had an error and could not complete the request.
377 
378 **/
379 EFI_STATUS
380 EFIAPI
381 VgaClassSetAttribute (
382   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
383   IN  UINTN                           Attribute
384   );
385 
386 /**
387   Sets the current coordinates of the cursor position.
388 
389   This function implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetCursorPosition().
390   It sets the current coordinates of the cursor position.
391   The upper left corner of the screen is defined as coordinate (0, 0).
392 
393   @param  This                   Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL instance.
394   @param  Column                 Column of position to set the cursor to.
395   @param  Row                    Row of position to set the cursor to.
396 
397   @retval EFI_SUCCESS            The operation completed successfully.
398   @retval EFI_DEVICE_ERROR       The device had an error and could not complete the request.
399   @retval EFI_UNSUPPORTED        The output device is not in a valid text mode, or the cursor
400                                  position is invalid for the current mode.
401 
402 **/
403 EFI_STATUS
404 EFIAPI
405 VgaClassSetCursorPosition (
406   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
407   IN  UINTN                           Column,
408   IN  UINTN                           Row
409   );
410 
411 /**
412   Makes the cursor visible or invisible.
413 
414   This function implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.EnableCursor().
415   It makes the cursor visible or invisible.
416 
417   @param  This                   Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL instance.
418   @param  Visible                If TRUE, the cursor is set to be visible.
419                                  If FALSE, the cursor is set to be invisible.
420 
421   @retval EFI_SUCESS             The operation completed successfully.
422   @retval EFI_DEVICE_ERROR       The device had an error and could not complete the request or the
423                                  device does not support changing the cursor mode.
424   @retval EFI_UNSUPPORTED        The output device does not support visibility control of the cursor.
425 
426 **/
427 EFI_STATUS
428 EFIAPI
429 VgaClassEnableCursor (
430   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
431   IN  BOOLEAN                         Visible
432   );
433 
434 /**
435   Returns information for an available text mode that the output device(s) supports.
436 
437   This function implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.QueryMode().
438   It returns information for an available text mode that the output device(s) supports.
439   It is required that all output devices support at least 80x25 text mode. This mode is defined to be mode 0.
440   If the output devices support 80x50, that is defined to be mode 1.
441 
442   @param  This                   Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL instance.
443   @param  ModeNumber             The mode number to return information on.
444   @param  Columns                Columen in current mode number
445   @param  Rows                   Row in current mode number.
446 
447   @retval EFI_SUCCESS            The requested mode information was returned.
448   @retval EFI_DEVICE_ERROR       The device had an error and could not complete the request.
449   @retval EFI_UNSUPPORTED        The mode number was not valid.
450 
451 **/
452 EFI_STATUS
453 EFIAPI
454 VgaClassQueryMode (
455   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
456   IN  UINTN                           ModeNumber,
457   OUT UINTN                           *Columns,
458   OUT UINTN                           *Rows
459   );
460 
461 /**
462   Sets the output device(s) to a specified mode.
463 
464   This function implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.QueryMode().
465   It sets the output device(s) to the requested mode.
466   On success the device is in the geometry for the requested mode,
467   and the device has been cleared to the current background color with the cursor at (0,0).
468 
469   @param  This                   Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL instance.
470   @param  ModeNumber             The text mode to set.
471 
472   @retval EFI_SUCCESS            The requested text mode was set.
473   @retval EFI_DEVICE_ERROR       The device had an error and could not complete the request.
474   @retval EFI_UNSUPPORTED        The mode number was not valid.
475 
476 **/
477 EFI_STATUS
478 EFIAPI
479 VgaClassSetMode (
480   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
481   IN  UINTN                           ModeNumber
482   );
483 
484 #endif
485