Imported Upstream version 1.15.1
[deb_xorg-server.git] / hw / xfree86 / common / xf86AutoConfig.c
CommitLineData
a09e091a
JB
1/*
2 * Copyright 2003 by David H. Dawes.
3 * Copyright 2003 by X-Oz Technologies.
4 * All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Except as contained in this notice, the name of the copyright holder(s)
25 * and author(s) shall not be used in advertising or otherwise to promote
26 * the sale, use or other dealings in this Software without prior written
27 * authorization from the copyright holder(s) and author(s).
28 *
29 * Author: David Dawes <dawes@XFree86.Org>.
30 */
31
32#ifdef HAVE_XORG_CONFIG_H
33#include <xorg-config.h>
34#endif
35
36#include "xf86.h"
37#include "xf86Parser.h"
38#include "xf86tokens.h"
39#include "xf86Config.h"
40#include "xf86Priv.h"
41#include "xf86_OSlib.h"
42#include "xf86platformBus.h"
43#include "xf86pciBus.h"
44#ifdef __sparc__
45#include "xf86sbusBus.h"
46#endif
47
48#ifdef sun
49#include <sys/visual_io.h>
50#include <ctype.h>
51#endif
52
53/* Sections for the default built-in configuration. */
54
55#define BUILTIN_DEVICE_NAME \
56 "\"Builtin Default %s Device %d\""
57
58#define BUILTIN_DEVICE_SECTION_PRE \
59 "Section \"Device\"\n" \
60 "\tIdentifier\t" BUILTIN_DEVICE_NAME "\n" \
61 "\tDriver\t\"%s\"\n"
62
63#define BUILTIN_DEVICE_SECTION_POST \
64 "EndSection\n\n"
65
66#define BUILTIN_DEVICE_SECTION \
67 BUILTIN_DEVICE_SECTION_PRE \
68 BUILTIN_DEVICE_SECTION_POST
69
70#define BUILTIN_SCREEN_NAME \
71 "\"Builtin Default %s Screen %d\""
72
73#define BUILTIN_SCREEN_SECTION \
74 "Section \"Screen\"\n" \
75 "\tIdentifier\t" BUILTIN_SCREEN_NAME "\n" \
76 "\tDevice\t" BUILTIN_DEVICE_NAME "\n" \
77 "EndSection\n\n"
78
79#define BUILTIN_LAYOUT_SECTION_PRE \
80 "Section \"ServerLayout\"\n" \
81 "\tIdentifier\t\"Builtin Default Layout\"\n"
82
83#define BUILTIN_LAYOUT_SCREEN_LINE \
84 "\tScreen\t" BUILTIN_SCREEN_NAME "\n"
85
86#define BUILTIN_LAYOUT_SECTION_POST \
87 "EndSection\n\n"
88
89static const char **builtinConfig = NULL;
90static int builtinLines = 0;
91
92static void listPossibleVideoDrivers(char *matches[], int nmatches);
93
94/*
95 * A built-in config file is stored as an array of strings, with each string
96 * representing a single line. AppendToConfig() breaks up the string "s"
97 * into lines, and appends those lines it to builtinConfig.
98 */
99
100static void
101AppendToList(const char *s, const char ***list, int *lines)
102{
103 char *str, *newstr, *p;
104
105 str = xnfstrdup(s);
106 for (p = strtok(str, "\n"); p; p = strtok(NULL, "\n")) {
107 (*lines)++;
108 *list = xnfrealloc(*list, (*lines + 1) * sizeof(**list));
109 newstr = xnfalloc(strlen(p) + 2);
110 strcpy(newstr, p);
111 strcat(newstr, "\n");
112 (*list)[*lines - 1] = newstr;
113 (*list)[*lines] = NULL;
114 }
115 free(str);
116}
117
118static void
119FreeList(const char ***list, int *lines)
120{
121 int i;
122
123 for (i = 0; i < *lines; i++) {
124 free((char *) ((*list)[i]));
125 }
126 free(*list);
127 *list = NULL;
128 *lines = 0;
129}
130
131static void
132FreeConfig(void)
133{
134 FreeList(&builtinConfig, &builtinLines);
135}
136
137static void
138AppendToConfig(const char *s)
139{
140 AppendToList(s, &builtinConfig, &builtinLines);
141}
142
143Bool
144xf86AutoConfig(void)
145{
146 char *deviceList[20];
147 char **p;
148 const char **cp;
149 char buf[1024];
150 ConfigStatus ret;
151
152 listPossibleVideoDrivers(deviceList, 20);
153
154 for (p = deviceList; *p; p++) {
155 snprintf(buf, sizeof(buf), BUILTIN_DEVICE_SECTION, *p, 0, *p);
156 AppendToConfig(buf);
157 snprintf(buf, sizeof(buf), BUILTIN_SCREEN_SECTION, *p, 0, *p, 0);
158 AppendToConfig(buf);
159 }
160
161 AppendToConfig(BUILTIN_LAYOUT_SECTION_PRE);
162 for (p = deviceList; *p; p++) {
163 snprintf(buf, sizeof(buf), BUILTIN_LAYOUT_SCREEN_LINE, *p, 0);
164 AppendToConfig(buf);
165 }
166 AppendToConfig(BUILTIN_LAYOUT_SECTION_POST);
167
168 for (p = deviceList; *p; p++) {
169 free(*p);
170 }
171
172 xf86MsgVerb(X_DEFAULT, 0,
173 "Using default built-in configuration (%d lines)\n",
174 builtinLines);
175
176 xf86MsgVerb(X_DEFAULT, 3, "--- Start of built-in configuration ---\n");
177 for (cp = builtinConfig; *cp; cp++)
178 xf86ErrorFVerb(3, "\t%s", *cp);
179 xf86MsgVerb(X_DEFAULT, 3, "--- End of built-in configuration ---\n");
180
181 xf86initConfigFiles();
182 xf86setBuiltinConfig(builtinConfig);
183 ret = xf86HandleConfigFile(TRUE);
184 FreeConfig();
185
186 if (ret != CONFIG_OK)
187 xf86Msg(X_ERROR, "Error parsing the built-in default configuration.\n");
188
189 return ret == CONFIG_OK;
190}
191
192static void
193listPossibleVideoDrivers(char *matches[], int nmatches)
194{
195 int i;
196
197 for (i = 0; i < nmatches; i++) {
198 matches[i] = NULL;
199 }
200 i = 0;
201
202#ifdef XSERVER_PLATFORM_BUS
203 i = xf86PlatformMatchDriver(matches, nmatches);
204#endif
205#ifdef sun
206 /* Check for driver type based on /dev/fb type and if valid, use
207 it instead of PCI bus probe results */
208 if (xf86Info.consoleFd >= 0 && (i < (nmatches - 1))) {
209 struct vis_identifier visid;
210 const char *cp;
211 extern char xf86SolarisFbDev[PATH_MAX];
212 int iret;
213
214 SYSCALL(iret = ioctl(xf86Info.consoleFd, VIS_GETIDENTIFIER, &visid));
215 if (iret < 0) {
216 int fbfd;
217
218 fbfd = open(xf86SolarisFbDev, O_RDONLY);
219 if (fbfd >= 0) {
220 SYSCALL(iret = ioctl(fbfd, VIS_GETIDENTIFIER, &visid));
221 close(fbfd);
222 }
223 }
224
225 if (iret < 0) {
226 xf86Msg(X_WARNING,
227 "could not get frame buffer identifier from %s\n",
228 xf86SolarisFbDev);
229 }
230 else {
231 xf86Msg(X_PROBED, "console driver: %s\n", visid.name);
232
233 /* Special case from before the general case was set */
234 if (strcmp(visid.name, "NVDAnvda") == 0) {
235 matches[i++] = xnfstrdup("nvidia");
236 }
237
238 /* General case - split into vendor name (initial all-caps
239 prefix) & driver name (rest of the string). */
240 if (strcmp(visid.name, "SUNWtext") != 0) {
241 for (cp = visid.name; (*cp != '\0') && isupper(*cp); cp++) {
242 /* find end of all uppercase vendor section */
243 }
244 if ((cp != visid.name) && (*cp != '\0')) {
245 char *driverName = xnfstrdup(cp);
246 char *vendorName = xnfstrdup(visid.name);
247
248 vendorName[cp - visid.name] = '\0';
249
250 matches[i++] = vendorName;
251 matches[i++] = driverName;
252 }
253 }
254 }
255 }
256#endif
257#ifdef __sparc__
258 if (i < (nmatches - 1))
259 {
260 char *sbusDriver = sparcDriverName();
261
262 if (sbusDriver)
263 matches[i++] = xnfstrdup(sbusDriver);
264 }
265#endif
266#ifdef XSERVER_LIBPCIACCESS
267 if (i < (nmatches - 1))
268 i = xf86PciMatchDriver(matches, nmatches);
269#endif
270
271#if defined(__linux__)
272 matches[i++] = xnfstrdup("modesetting");
273#endif
274
275#if !defined(sun)
276 /* Fallback to platform default frame buffer driver */
277 if (i < (nmatches - 1)) {
278#if !defined(__linux__) && defined(__sparc__)
279 matches[i++] = xnfstrdup("wsfb");
280#else
281 matches[i++] = xnfstrdup("fbdev");
282#endif
283 }
284#endif /* !sun */
285
286 /* Fallback to platform default hardware */
287 if (i < (nmatches - 1)) {
288#if defined(__i386__) || defined(__amd64__) || defined(__hurd__)
289 matches[i++] = xnfstrdup("vesa");
290#elif defined(__sparc__) && !defined(sun)
291 matches[i++] = xnfstrdup("sunffb");
292#endif
293 }
294}
295
296/* copy a screen section and enter the desired driver
297 * and insert it at i in the list of screens */
298static Bool
299copyScreen(confScreenPtr oscreen, GDevPtr odev, int i, char *driver)
300{
301 confScreenPtr nscreen;
302 GDevPtr cptr = NULL;
303
304 nscreen = malloc(sizeof(confScreenRec));
305 if (!nscreen)
306 return FALSE;
307 memcpy(nscreen, oscreen, sizeof(confScreenRec));
308
309 cptr = malloc(sizeof(GDevRec));
310 if (!cptr) {
311 free(nscreen);
312 return FALSE;
313 }
314 memcpy(cptr, odev, sizeof(GDevRec));
315
316 if (asprintf(&cptr->identifier, "Autoconfigured Video Device %s", driver)
317 == -1) {
318 free(cptr);
319 free(nscreen);
320 return FALSE;
321 }
322 cptr->driver = driver;
323
324 xf86ConfigLayout.screens[i].screen = nscreen;
325
326 /* now associate the new driver entry with the new screen entry */
327 xf86ConfigLayout.screens[i].screen->device = cptr;
328 cptr->myScreenSection = xf86ConfigLayout.screens[i].screen;
329
330 return TRUE;
331}
332
333GDevPtr
334autoConfigDevice(GDevPtr preconf_device)
335{
336 GDevPtr ptr = NULL;
337 char *matches[20]; /* If we have more than 20 drivers we're in trouble */
338 int num_matches = 0, num_screens = 0, i;
339 screenLayoutPtr slp;
340
341 if (!xf86configptr) {
342 return NULL;
343 }
344
345 /* If there's a configured section with no driver chosen, use it */
346 if (preconf_device) {
347 ptr = preconf_device;
348 }
349 else {
350 ptr = calloc(1, sizeof(GDevRec));
351 if (!ptr) {
352 return NULL;
353 }
354 ptr->chipID = -1;
355 ptr->chipRev = -1;
356 ptr->irq = -1;
357
358 ptr->active = TRUE;
359 ptr->claimed = FALSE;
360 ptr->identifier = "Autoconfigured Video Device";
361 ptr->driver = NULL;
362 }
363 if (!ptr->driver) {
364 /* get all possible video drivers and count them */
365 listPossibleVideoDrivers(matches, 20);
366 for (; matches[num_matches]; num_matches++) {
367 xf86Msg(X_DEFAULT, "Matched %s as autoconfigured driver %d\n",
368 matches[num_matches], num_matches);
369 }
370
371 slp = xf86ConfigLayout.screens;
372 if (slp) {
373 /* count the number of screens and make space for
374 * a new screen for each additional possible driver
375 * minus one for the already existing first one
376 * plus one for the terminating NULL */
377 for (; slp[num_screens].screen; num_screens++);
378 xf86ConfigLayout.screens = xnfcalloc(num_screens + num_matches,
379 sizeof(screenLayoutRec));
380 xf86ConfigLayout.screens[0] = slp[0];
381
382 /* do the first match and set that for the original first screen */
383 ptr->driver = matches[0];
384 if (!xf86ConfigLayout.screens[0].screen->device) {
385 xf86ConfigLayout.screens[0].screen->device = ptr;
386 ptr->myScreenSection = xf86ConfigLayout.screens[0].screen;
387 }
388
389 /* for each other driver found, copy the first screen, insert it
390 * into the list of screens and set the driver */
391 i = 0;
392 while (i++ < num_matches) {
393 if (!copyScreen(slp[0].screen, ptr, i, matches[i]))
394 return NULL;
395 }
396
397 /* shift the rest of the original screen list
398 * to the end of the current screen list
399 *
400 * TODO Handle rest of multiple screen sections */
401 for (i = 1; i < num_screens; i++) {
402 xf86ConfigLayout.screens[i + num_matches] = slp[i];
403 }
404 xf86ConfigLayout.screens[num_screens + num_matches - 1].screen =
405 NULL;
406 free(slp);
407 }
408 else {
409 /* layout does not have any screens, not much to do */
410 ptr->driver = matches[0];
411 for (i = 1; matches[i]; i++) {
412 if (matches[i] != matches[0]) {
413 free(matches[i]);
414 }
415 }
416 }
417 }
418
419 xf86Msg(X_DEFAULT, "Assigned the driver to the xf86ConfigLayout\n");
420
421 return ptr;
422}