1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * WINE Drivers functions
6 * Copyright 1994 Martin Ayotte
7 * Copyright 1998 Marcus Meissner
8 * Copyright 1999 Eric Pouech
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/winbase16.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(driver);
35 static LPWINE_DRIVER lpDrvItemList = NULL;
37 WINE_MMTHREAD* (*pFnGetMMThread16)(HANDLE16 h) /* = NULL */;
39 /**************************************************************************
40 * DRIVER_GetNumberOfModuleRefs [internal]
42 * Returns the number of open drivers which share the same module.
44 static unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule, WINE_DRIVER** found)
49 if (found) *found = NULL;
50 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem)
52 if (!(lpDrv->dwFlags & WINE_GDF_16BIT) && lpDrv->d.d32.hModule == hModule)
54 if (found && !*found) *found = lpDrv;
61 /**************************************************************************
62 * DRIVER_FindFromHDrvr [internal]
64 * From a hDrvr being 32 bits, returns the WINE internal structure.
66 LPWINE_DRIVER DRIVER_FindFromHDrvr(HDRVR hDrvr)
68 LPWINE_DRIVER d = (LPWINE_DRIVER)hDrvr;
70 if (hDrvr && HeapValidate(GetProcessHeap(), 0, d) && d->dwMagic == WINE_DI_MAGIC) {
76 /**************************************************************************
77 * DRIVER_SendMessage [internal]
79 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT msg,
80 LPARAM lParam1, LPARAM lParam2)
82 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
85 TRACE("Before sdm16 call hDrv=%04x wMsg=%04x p1=%08lx p2=%08lx\n",
86 lpDrv->d.d16.hDriver16, msg, lParam1, lParam2);
88 if ((map = DRIVER_MapMsg32To16(msg, &lParam1, &lParam2)) >= 0) {
89 ret = SendDriverMessage16(lpDrv->d.d16.hDriver16, msg, lParam1, lParam2);
91 DRIVER_UnMapMsg32To16(msg, lParam1, lParam2);
97 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%08x wMsg=%04x p1=%08lx p2=%08lx\n",
98 lpDrv->d.d32.lpDrvProc, lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
99 return lpDrv->d.d32.lpDrvProc(lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
102 /**************************************************************************
103 * SendDriverMessage [WINMM.@]
104 * DrvSendMessage [WINMM.@]
106 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
112 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
114 if ((lpDrv = DRIVER_FindFromHDrvr(hDriver)) != NULL) {
115 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
117 WARN("Bad driver handle %u\n", hDriver);
119 TRACE("retval = %ld\n", retval);
124 /**************************************************************************
125 * DRIVER_RemoveFromList [internal]
127 * Generates all the logic to handle driver closure / deletion
128 * Removes a driver struct to the list of open drivers.
130 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
132 if (!(lpDrv->dwFlags & WINE_GDF_16BIT)) {
133 /* last of this driver in list ? */
134 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, NULL) == 1) {
135 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
136 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
140 if (lpDrv->lpPrevItem)
141 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
143 lpDrvItemList = lpDrv->lpNextItem;
144 if (lpDrv->lpNextItem)
145 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
146 /* trash magic number */
147 lpDrv->dwMagic ^= 0xa5a5a5a5;
152 /**************************************************************************
153 * DRIVER_AddToList [internal]
155 * Adds a driver struct to the list of open drivers.
156 * Generates all the logic to handle driver creation / open.
158 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
160 lpNewDrv->dwMagic = WINE_DI_MAGIC;
161 /* First driver to be loaded for this module, need to load correctly the module */
162 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
163 /* first of this driver in list ? */
164 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv->d.d32.hModule, NULL) == 0) {
165 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
166 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
169 /* returned value is not checked */
170 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
174 lpNewDrv->lpNextItem = NULL;
175 if (lpDrvItemList == NULL) {
176 lpDrvItemList = lpNewDrv;
177 lpNewDrv->lpPrevItem = NULL;
179 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
180 while (lpDrv->lpNextItem != NULL)
181 lpDrv = lpDrv->lpNextItem;
183 lpDrv->lpNextItem = lpNewDrv;
184 lpNewDrv->lpPrevItem = lpDrv;
187 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
188 /* Now just open a new instance of a driver on this module */
189 lpNewDrv->d.d32.dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
191 if (lpNewDrv->d.d32.dwDriverID == 0) {
192 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
193 DRIVER_RemoveFromList(lpNewDrv);
200 /**************************************************************************
201 * DRIVER_GetLibName [internal]
204 BOOL DRIVER_GetLibName(LPCSTR keyName, LPCSTR sectName, LPSTR buf, int sz)
206 /* should also do some registry diving */
207 return GetPrivateProfileStringA(sectName, keyName, "", buf, sz, "SYSTEM.INI");
210 /**************************************************************************
211 * DRIVER_TryOpenDriver32 [internal]
213 * Tries to load a 32 bit driver whose DLL's (module) name is fn
215 LPWINE_DRIVER DRIVER_TryOpenDriver32(LPCSTR fn, LPARAM lParam2)
217 LPWINE_DRIVER lpDrv = NULL;
222 TRACE("(%s, %08lX);\n", debugstr_a(fn), lParam2);
224 if ((ptr = strchr(fn, ' ')) != NULL) {
226 while (*ptr == ' ') ptr++;
227 if (*ptr == '\0') ptr = NULL;
230 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
231 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
233 if ((hModule = LoadLibraryA(fn)) == 0) {cause = "Not a 32 bit lib"; goto exit;}
235 lpDrv->d.d32.lpDrvProc = (DRIVERPROC)GetProcAddress(hModule, "DriverProc");
236 if (lpDrv->d.d32.lpDrvProc == NULL) {cause = "no DriverProc"; goto exit;}
239 lpDrv->d.d32.hModule = hModule;
240 lpDrv->d.d32.dwDriverID = 0;
242 /* Win32 installable drivers must support a two phase opening scheme:
243 * + first open with NULL as lParam2 (session instance),
244 * + then do a second open with the real non null lParam2)
246 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, NULL) == 0 && lParam2)
250 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, 0L))
252 cause = "load0 failed";
255 ret = DRIVER_TryOpenDriver32(fn, lParam2);
258 CloseDriver((HDRVR)lpDrv, 0L, 0L);
259 cause = "load1 failed";
265 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2))
266 {cause = "load failed"; goto exit;}
268 TRACE("=> %p\n", lpDrv);
271 FreeLibrary(hModule);
272 HeapFree(GetProcessHeap(), 0, lpDrv);
273 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn), cause);
277 /**************************************************************************
278 * DRIVER_TryOpenDriver16 [internal]
280 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
282 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR fn, LPCSTR sn, LPARAM lParam2)
284 LPWINE_DRIVER lpDrv = NULL;
287 TRACE("(%s, %08lX);\n", debugstr_a(sn), lParam2);
289 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
290 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
292 /* FIXME: shall we do some black magic here on sn ?
293 * drivers32 => drivers
297 lpDrv->d.d16.hDriver16 = OpenDriver16(fn, sn, lParam2);
298 if (lpDrv->d.d16.hDriver16 == 0) {cause = "Not a 16 bit driver"; goto exit;}
299 lpDrv->dwFlags = WINE_GDF_16BIT;
301 if (!DRIVER_AddToList(lpDrv, 0, lParam2)) {cause = "load failed"; goto exit;}
303 TRACE("=> %p\n", lpDrv);
306 HeapFree(GetProcessHeap(), 0, lpDrv);
307 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn), cause);
311 /**************************************************************************
312 * OpenDriverA [WINMM.@]
314 * (0,1,DRV_LOAD ,0 ,0)
315 * (0,1,DRV_ENABLE,0 ,0)
316 * (0,1,DRV_OPEN ,buf[256],0)
318 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
320 LPWINE_DRIVER lpDrv = NULL;
322 LPCSTR lsn = lpSectionName;
324 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
327 lstrcpynA(libName, lpDriverName, sizeof(libName));
329 if ((lpDrv = DRIVER_TryOpenDriver32(libName, lParam2)))
333 if (DRIVER_GetLibName(lpDriverName, lsn, libName, sizeof(libName)) &&
334 (lpDrv = DRIVER_TryOpenDriver32(libName, lParam2)))
337 if (!(lpDrv = DRIVER_TryOpenDriver16(lpDriverName, lpSectionName, lParam2)))
338 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
340 if (lpDrv) TRACE("=> %08lx\n", (DWORD)lpDrv);
344 /**************************************************************************
345 * OpenDriver [WINMM.@]
348 HDRVR WINAPI OpenDriverW(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
350 LPSTR dn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpDriverName);
351 LPSTR sn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpSectionName);
352 HDRVR ret = OpenDriverA(dn, sn, lParam);
354 if (dn) HeapFree(GetProcessHeap(), 0, dn);
355 if (sn) HeapFree(GetProcessHeap(), 0, sn);
359 /**************************************************************************
360 * CloseDriver [WINMM.@]
363 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
367 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
369 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL)
371 if (lpDrv->dwFlags & WINE_GDF_16BIT)
372 CloseDriver16(lpDrv->d.d16.hDriver16, lParam1, lParam2);
375 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
376 lpDrv->d.d32.dwDriverID = 0;
378 if (DRIVER_RemoveFromList(lpDrv)) {
379 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
381 LPWINE_DRIVER lpDrv0;
383 /* if driver has an opened session instance, we have to close it too */
384 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, &lpDrv0) == 1)
386 DRIVER_SendMessage(lpDrv0, DRV_CLOSE, 0L, 0L);
387 lpDrv0->d.d32.dwDriverID = 0;
388 DRIVER_RemoveFromList(lpDrv0);
389 FreeLibrary(lpDrv->d.d32.hModule);
390 HeapFree(GetProcessHeap(), 0, lpDrv0);
392 FreeLibrary(lpDrv->d.d32.hModule);
394 HeapFree(GetProcessHeap(), 0, lpDrv);
398 WARN("Failed to close driver\n");
402 /**************************************************************************
403 * GetDriverFlags [WINMM.@]
404 * [in] hDrvr handle to the driver
407 * 0x00000000 if hDrvr is an invalid handle
408 * 0x80000000 if hDrvr is a valid 32 bit driver
409 * 0x90000000 if hDrvr is a valid 16 bit driver
411 * native WINMM doesn't return those flags
412 * 0x80000000 for a valid 32 bit driver and that's it
413 * (I may have mixed up the two flags :-(
415 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
420 TRACE("(%04x)\n", hDrvr);
422 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
423 ret = WINE_GDF_EXIST | lpDrv->dwFlags;
428 /**************************************************************************
429 * GetDriverModuleHandle [WINMM.@]
430 * DrvGetModuleHandle [WINMM.@]
432 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
437 TRACE("(%04x);\n", hDrvr);
439 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
440 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
441 hModule = lpDrv->d.d32.hModule;
443 TRACE("=> %04x\n", hModule);
447 /**************************************************************************
448 * DefDriverProc [WINMM.@]
449 * DrvDefDriverProc [WINMM.@]
451 LRESULT WINAPI DefDriverProc(DWORD dwDriverIdentifier, HDRVR hDrv,
452 UINT Msg, LPARAM lParam1, LPARAM lParam2)