driver.c 18.7 KB
Newer Older
1 2 3 4 5 6
/*
 * WINE Drivers functions
 *
 * Copyright 1994 Martin Ayotte
 * Copyright 1998 Marcus Meissner
 * Copyright 1999 Eric Pouech
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22
 */

23 24 25
#include "config.h"
#include "wine/port.h"

26
#include <string.h>
27
#include <stdarg.h>
28
#include "windef.h"
29
#include "winbase.h"
30 31
#include "wingdi.h"
#include "winuser.h"
32
#include "winnls.h"
33
#include "winreg.h"
34 35
#include "mmddk.h"
#include "winemm.h"
36
#include "wine/debug.h"
37
#include "wine/unicode.h"
38 39
#include "excpt.h"
#include "wine/exception.h"
40

41
WINE_DEFAULT_DEBUG_CHANNEL(driver);
42

43 44 45 46 47 48 49 50 51
static CRITICAL_SECTION mmdriver_lock;
static CRITICAL_SECTION_DEBUG mmdriver_lock_debug =
{
    0, 0, &mmdriver_lock,
    { &mmdriver_lock_debug.ProcessLocksList, &mmdriver_lock_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": mmdriver_lock") }
};
static CRITICAL_SECTION mmdriver_lock = { &mmdriver_lock_debug, -1, 0, 0, 0, 0 };

52
static LPWINE_DRIVER   lpDrvItemList  /* = NULL */;
53 54
static const WCHAR HKLM_BASE[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
                                  'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
55

56 57 58 59 60 61 62 63 64 65 66
static void DRIVER_Dump(const char *comment)
{
#if 0
    LPWINE_DRIVER 	lpDrv;

    TRACE("%s\n", comment);

    EnterCriticalSection( &mmdriver_lock );

    for (lpDrv = lpDrvItemList; lpDrv != NULL; lpDrv = lpDrv->lpNextItem)
    {
67
        TRACE("%p, magic %04lx, id %p, next %p\n", lpDrv, lpDrv->dwMagic, lpDrv->d.d32.dwDriverID, lpDrv->lpNextItem);
68 69 70 71 72 73
    }

    LeaveCriticalSection( &mmdriver_lock );
#endif
}

74 75 76 77 78
/**************************************************************************
 *			DRIVER_GetNumberOfModuleRefs		[internal]
 *
 * Returns the number of open drivers which share the same module.
 */
79
static	unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule, WINE_DRIVER** found)
80 81
{
    LPWINE_DRIVER	lpDrv;
82 83
    unsigned		count = 0;

84 85
    EnterCriticalSection( &mmdriver_lock );

86
    if (found) *found = NULL;
87
    for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem)
88
    {
89
	if (lpDrv->hModule == hModule)
90 91
        {
            if (found && !*found) *found = lpDrv;
92 93 94
	    count++;
	}
    }
95 96

    LeaveCriticalSection( &mmdriver_lock );
97 98 99 100 101
    return count;
}

/**************************************************************************
 *				DRIVER_FindFromHDrvr		[internal]
102
 *
103 104 105
 * From a hDrvr being 32 bits, returns the WINE internal structure.
 */
LPWINE_DRIVER	DRIVER_FindFromHDrvr(HDRVR hDrvr)
106
{
107
    LPWINE_DRIVER d;
108

109 110
    __TRY
    {
111
        d = (LPWINE_DRIVER)hDrvr;
112 113
        if (d && d->dwMagic != WINE_DI_MAGIC) d = NULL;
    }
114
    __EXCEPT_PAGE_FAULT
115 116
    {
        return NULL;
117
    }
118 119
    __ENDTRY;

120
    if (d) TRACE("%p -> %p, %p\n", hDrvr, d->lpDrvProc, (void *)d->dwDriverID);
121 122
    else TRACE("%p -> NULL\n", hDrvr);

123
    return d;
124 125 126 127 128
}

/**************************************************************************
 *				DRIVER_SendMessage		[internal]
 */
129
static inline LRESULT DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT msg,
130
                                         LPARAM lParam1, LPARAM lParam2)
131
{
132
    LRESULT		ret;
133

134 135 136 137 138 139
    TRACE("Before call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx\n",
          lpDrv->lpDrvProc, lpDrv->dwDriverID, lpDrv, msg, lParam1, lParam2);
    ret = lpDrv->lpDrvProc(lpDrv->dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
    TRACE("After  call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx => %08lx\n",
          lpDrv->lpDrvProc, lpDrv->dwDriverID, lpDrv, msg, lParam1, lParam2, ret);

140
    return ret;
141 142 143
}

/**************************************************************************
144
 *				SendDriverMessage		[WINMM.@]
Patrik Stridvall's avatar
Patrik Stridvall committed
145
 *				DrvSendMessage			[WINMM.@]
146 147 148 149 150 151
 */
LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
				 LPARAM lParam2)
{
    LPWINE_DRIVER	lpDrv;
    LRESULT 		retval = 0;
152

153
    TRACE("(%p, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
154

155 156 157
    if ((lpDrv = DRIVER_FindFromHDrvr(hDriver)) != NULL) {
	retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
    } else {
158
	WARN("Bad driver handle %p\n", hDriver);
159 160
    }
    TRACE("retval = %ld\n", retval);
161

162 163 164 165 166 167 168 169 170 171 172
    return retval;
}

/**************************************************************************
 *				DRIVER_RemoveFromList		[internal]
 *
 * Generates all the logic to handle driver closure / deletion
 * Removes a driver struct to the list of open drivers.
 */
static	BOOL	DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
{
173 174 175 176
    /* last of this driver in list ? */
    if (DRIVER_GetNumberOfModuleRefs(lpDrv->hModule, NULL) == 1) {
        DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
        DRIVER_SendMessage(lpDrv, DRV_FREE,    0L, 0L);
177
    }
178

179 180
    EnterCriticalSection( &mmdriver_lock );

181 182 183 184 185 186
    if (lpDrv->lpPrevItem)
	lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
    else
	lpDrvItemList = lpDrv->lpNextItem;
    if (lpDrv->lpNextItem)
	lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
187 188
    /* trash magic number */
    lpDrv->dwMagic ^= 0xa5a5a5a5;
189 190
    lpDrv->lpDrvProc = NULL;
    lpDrv->dwDriverID = 0;
191 192

    LeaveCriticalSection( &mmdriver_lock );
193 194 195 196 197 198 199 200 201 202 203 204 205 206

    return TRUE;
}

/**************************************************************************
 *				DRIVER_AddToList		[internal]
 *
 * Adds a driver struct to the list of open drivers.
 * Generates all the logic to handle driver creation / open.
 */
static	BOOL	DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
{
    lpNewDrv->dwMagic = WINE_DI_MAGIC;
    /* First driver to be loaded for this module, need to load correctly the module */
207 208 209 210 211 212 213 214 215
    /* first of this driver in list ? */
    if (DRIVER_GetNumberOfModuleRefs(lpNewDrv->hModule, NULL) == 0) {
        if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
            TRACE("DRV_LOAD failed on driver %p\n", lpNewDrv);
            return FALSE;
        }
        /* returned value is not checked */
        DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
    }
216

217 218
    /* Now just open a new instance of a driver on this module */
    lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
219

220 221 222 223
    if (lpNewDrv->dwDriverID == 0)
    {
        TRACE("DRV_OPEN failed on driver %p\n", lpNewDrv);
        return FALSE;
224 225
    }

226 227
    EnterCriticalSection( &mmdriver_lock );

228 229 230 231 232 233 234 235
    lpNewDrv->lpNextItem = NULL;
    if (lpDrvItemList == NULL) {
	lpDrvItemList = lpNewDrv;
	lpNewDrv->lpPrevItem = NULL;
    } else {
	LPWINE_DRIVER	lpDrv = lpDrvItemList;	/* find end of list */
	while (lpDrv->lpNextItem != NULL)
	    lpDrv = lpDrv->lpNextItem;
236

237 238 239 240
	lpDrv->lpNextItem = lpNewDrv;
	lpNewDrv->lpPrevItem = lpDrv;
    }

241
    LeaveCriticalSection( &mmdriver_lock );
242 243 244 245 246 247 248
    return TRUE;
}

/**************************************************************************
 *				DRIVER_GetLibName		[internal]
 *
 */
249
BOOL	DRIVER_GetLibName(LPCWSTR keyName, LPCWSTR sectName, LPWSTR buf, int sz)
250
{
251 252
    HKEY	hKey, hSecKey;
    DWORD	bufLen, lRet;
253 254
    static const WCHAR wszSystemIni[] = {'S','Y','S','T','E','M','.','I','N','I',0};
    WCHAR       wsznull = '\0';
255

256 257
    TRACE("registry: %s, %s, %p, %d\n", debugstr_w(keyName), debugstr_w(sectName), buf, sz);

258
    lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, HKLM_BASE, 0, KEY_QUERY_VALUE, &hKey);
259
    if (lRet == ERROR_SUCCESS) {
260
	lRet = RegOpenKeyExW(hKey, sectName, 0, KEY_QUERY_VALUE, &hSecKey);
261
	if (lRet == ERROR_SUCCESS) {
262 263
            bufLen = sz;
	    lRet = RegQueryValueExW(hSecKey, keyName, 0, 0, (void*)buf, &bufLen);
264 265 266 267 268
	    RegCloseKey( hSecKey );
	}
        RegCloseKey( hKey );
    }
    if (lRet == ERROR_SUCCESS) return TRUE;
269

270 271
    /* default to system.ini if we can't find it in the registry,
     * to support native installations where system.ini is still used */
272
    TRACE("system.ini: %s, %s, %p, %d\n", debugstr_w(keyName), debugstr_w(sectName), buf, sz);
273
    return GetPrivateProfileStringW(sectName, keyName, &wsznull, buf, sz / sizeof(WCHAR), wszSystemIni);
274 275 276 277 278 279 280
}

/**************************************************************************
 *				DRIVER_TryOpenDriver32		[internal]
 *
 * Tries to load a 32 bit driver whose DLL's (module) name is fn
 */
281
LPWINE_DRIVER	DRIVER_TryOpenDriver32(LPCWSTR fn, LPARAM lParam2)
282 283 284
{
    LPWINE_DRIVER 	lpDrv = NULL;
    HMODULE		hModule = 0;
285
    LPWSTR		ptr;
286 287
    LPCSTR		cause = 0;

288
    TRACE("(%s, %08lX);\n", debugstr_w(fn), lParam2);
289

290
    if ((ptr = strchrW(fn, ' ')) != NULL) {
291 292 293 294 295 296 297 298
	*ptr++ = '\0';
	while (*ptr == ' ') ptr++;
	if (*ptr == '\0') ptr = NULL;
    }

    lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
    if (lpDrv == NULL) {cause = "OOM"; goto exit;}

299
    if ((hModule = LoadLibraryW(fn)) == 0) {cause = "Not a 32 bit lib"; goto exit;}
300

301 302
    lpDrv->lpDrvProc = (DRIVERPROC)GetProcAddress(hModule, "DriverProc");
    if (lpDrv->lpDrvProc == NULL) {cause = "no DriverProc"; goto exit;}
303

304 305 306
    lpDrv->dwFlags    = 0;
    lpDrv->hModule    = hModule;
    lpDrv->dwDriverID = 0;
307

308
    /* Win32 installable drivers must support a two phase opening scheme:
309
     * + first open with NULL as lParam2 (session instance),
310 311
     * + then do a second open with the real non null lParam2)
     */
312
    if (DRIVER_GetNumberOfModuleRefs(lpDrv->hModule, NULL) == 0 && lParam2)
313 314 315 316 317
    {
        LPWINE_DRIVER   ret;

        if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, 0L))
        {
318
            cause = "load0 failed";
319 320 321
            goto exit;
        }
        ret = DRIVER_TryOpenDriver32(fn, lParam2);
322
        if (!ret)
323 324
        {
            CloseDriver((HDRVR)lpDrv, 0L, 0L);
325
            cause = "load1 failed";
326 327
            goto exit;
        }
Eric Pouech's avatar
Eric Pouech committed
328
        lpDrv->dwFlags |= WINE_GDF_SESSION;
329 330 331
        return ret;
    }

332
    if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2))
333
    {cause = "load failed"; goto exit;}
334 335 336 337 338 339

    TRACE("=> %p\n", lpDrv);
    return lpDrv;
 exit:
    FreeLibrary(hModule);
    HeapFree(GetProcessHeap(), 0, lpDrv);
340
    TRACE("Unable to load 32 bit module %s: %s\n", debugstr_w(fn), cause);
341 342 343 344
    return NULL;
}

/**************************************************************************
345
 *				OpenDriverA		        [WINMM.@]
Patrik Stridvall's avatar
Patrik Stridvall committed
346
 *				DrvOpenA			[WINMM.@]
347 348 349 350
 * (0,1,DRV_LOAD  ,0       ,0)
 * (0,1,DRV_ENABLE,0       ,0)
 * (0,1,DRV_OPEN  ,buf[256],0)
 */
351 352 353 354 355
HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
{
    INT                 len;
    LPWSTR 		dn = NULL;
    LPWSTR 		sn = NULL;
356
    HDRVR		ret = 0;
357 358 359 360 361

    if (lpDriverName)
    {
        len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
        dn = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
362
        if (!dn) goto done;
363 364 365 366 367 368 369
        MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, dn, len );
    }

    if (lpSectionName)
    {
        len = MultiByteToWideChar( CP_ACP, 0, lpSectionName, -1, NULL, 0 );
        sn = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
370
        if (!sn) goto done;
371 372 373 374 375
        MultiByteToWideChar( CP_ACP, 0, lpSectionName, -1, sn, len );
    }

    ret = OpenDriver(dn, sn, lParam);

376 377 378
done:
    HeapFree(GetProcessHeap(), 0, dn);
    HeapFree(GetProcessHeap(), 0, sn);
379 380 381 382 383 384 385 386
    return ret;
}

/**************************************************************************
 *				OpenDriver 		        [WINMM.@]
 *				DrvOpen				[WINMM.@]
 */
HDRVR WINAPI OpenDriver(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
387 388
{
    LPWINE_DRIVER	lpDrv = NULL;
389
    WCHAR 		libName[MAX_PATH + 1];
390
    LPCWSTR		lsn = lpSectionName;
391

392 393
    TRACE("(%s, %s, 0x%08lx);\n", 
          debugstr_w(lpDriverName), debugstr_w(lpSectionName), lParam);
394

395 396
    DRIVER_Dump("BEFORE:");

397
    if (lsn == NULL) {
398 399
        static const WCHAR wszDrivers32[] = {'D','r','i','v','e','r','s','3','2',0};
	lstrcpynW(libName, lpDriverName, sizeof(libName) / sizeof(WCHAR));
400

401
	if ((lpDrv = DRIVER_TryOpenDriver32(libName, lParam)))
402
	    goto the_end;
403
	lsn = wszDrivers32;
404 405
    }
    if (DRIVER_GetLibName(lpDriverName, lsn, libName, sizeof(libName)) &&
406
	(lpDrv = DRIVER_TryOpenDriver32(libName, lParam)))
407 408
	goto the_end;

409 410
    TRACE("Failed to open driver %s from system.ini file, section %s\n", 
          debugstr_w(lpDriverName), debugstr_w(lpSectionName));
411

412 413 414 415 416
the_end:
    TRACE("=> %p\n", lpDrv);

    DRIVER_Dump("AFTER:");

417
    return (HDRVR)lpDrv;
418 419 420
}

/**************************************************************************
421
 *			CloseDriver				[WINMM.@]
Patrik Stridvall's avatar
Patrik Stridvall committed
422
 *			DrvClose				[WINMM.@]
423 424 425
 */
LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
{
426
    BOOL ret;
427 428
    LPWINE_DRIVER	lpDrv;

429
    TRACE("(%p, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
430

431 432
    DRIVER_Dump("BEFORE:");

433
    if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL)
434
    {
435 436 437
        LPWINE_DRIVER lpDrv0;

        DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
438 439 440

        DRIVER_RemoveFromList(lpDrv);

441 442 443 444 445
        if (lpDrv->dwFlags & WINE_GDF_SESSION)
            FIXME("WINE_GDF_SESSION: Shouldn't happen (%p)\n", lpDrv);
        /* if driver has an opened session instance, we have to close it too */
        if (DRIVER_GetNumberOfModuleRefs(lpDrv->hModule, &lpDrv0) == 1 &&
            (lpDrv0->dwFlags & WINE_GDF_SESSION))
446
        {
447 448 449 450
            DRIVER_SendMessage(lpDrv0, DRV_CLOSE, 0, 0);
            DRIVER_RemoveFromList(lpDrv0);
            FreeLibrary(lpDrv0->hModule);
            HeapFree(GetProcessHeap(), 0, lpDrv0);
451
        }
452 453
        FreeLibrary(lpDrv->hModule);

454 455 456 457 458 459 460
        HeapFree(GetProcessHeap(), 0, lpDrv);
        ret = TRUE;
    }
    else
    {
        WARN("Failed to close driver\n");
        ret = FALSE;
461
    }
462 463 464 465

    DRIVER_Dump("AFTER:");

    return ret;
466 467 468
}

/**************************************************************************
469
 *				GetDriverFlags		[WINMM.@]
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
 * [in] hDrvr handle to the driver
 *
 * Returns:
 *	0x00000000 if hDrvr is an invalid handle
 *	0x80000000 if hDrvr is a valid 32 bit driver
 *	0x90000000 if hDrvr is a valid 16 bit driver
 *
 * native WINMM doesn't return those flags
 *	0x80000000 for a valid 32 bit driver and that's it
 *	(I may have mixed up the two flags :-(
 */
DWORD	WINAPI GetDriverFlags(HDRVR hDrvr)
{
    LPWINE_DRIVER 	lpDrv;
    DWORD		ret = 0;

486
    TRACE("(%p)\n", hDrvr);
487 488

    if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
Eric Pouech's avatar
Eric Pouech committed
489
	ret = WINE_GDF_EXIST | (lpDrv->dwFlags & WINE_GDF_EXTERNAL_MASK);
490 491 492 493 494
    }
    return ret;
}

/**************************************************************************
495
 *				GetDriverModuleHandle	[WINMM.@]
Patrik Stridvall's avatar
Patrik Stridvall committed
496
 *				DrvGetModuleHandle	[WINMM.@]
497 498 499 500 501
 */
HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
{
    LPWINE_DRIVER 	lpDrv;
    HMODULE		hModule = 0;
502

503
    TRACE("(%p);\n", hDrvr);
504

505
    if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
506
        hModule = lpDrv->hModule;
507
    }
508
    TRACE("=> %p\n", hModule);
509 510 511 512
    return hModule;
}

/**************************************************************************
513
 * 				DefDriverProc			  [WINMM.@]
Patrik Stridvall's avatar
Patrik Stridvall committed
514
 * 				DrvDefDriverProc		  [WINMM.@]
515
 */
516
LRESULT WINAPI DefDriverProc(DWORD_PTR dwDriverIdentifier, HDRVR hDrv,
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
			     UINT Msg, LPARAM lParam1, LPARAM lParam2)
{
    switch (Msg) {
    case DRV_LOAD:
    case DRV_FREE:
    case DRV_ENABLE:
    case DRV_DISABLE:
        return 1;
    case DRV_INSTALL:
    case DRV_REMOVE:
        return DRV_SUCCESS;
    default:
        return 0;
    }
}
532

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
/**************************************************************************
 *				DRIVER_getCallback		[internal]
 */
static const char* DRIVER_getCallback(DWORD uFlags)
{
    switch(uFlags & DCB_TYPEMASK) {
    case DCB_NULL:     return "null";
    case DCB_WINDOW:   return "window";
    case DCB_TASK:     return "task";
    case DCB_EVENT:    return "event";
    case DCB_FUNCTION: return "32bit function";
    default:           return "UNKNOWN";
    }
}

548 549 550
/**************************************************************************
 * 				DriverCallback			[WINMM.@]
 */
551 552 553
BOOL WINAPI DriverCallback(DWORD_PTR dwCallBack, DWORD uFlags, HDRVR hDev,
			   DWORD wMsg, DWORD_PTR dwUser, DWORD_PTR dwParam1,
			   DWORD_PTR dwParam2)
554
{
555
    BOOL ret = FALSE;
556 557
    TRACE("(%08lX, %s %04X, %p, %04X, %08lX, %08lX, %08lX)\n",
	  dwCallBack, DRIVER_getCallback(uFlags), uFlags, hDev, wMsg, dwUser, dwParam1, dwParam2);
558 559
    if (!dwCallBack)
	return ret;
560 561 562

    switch (uFlags & DCB_TYPEMASK) {
    case DCB_NULL:
563 564
	/* Native returns FALSE = no notification, not TRUE */
	return ret;
565
    case DCB_WINDOW:
566
	ret = PostMessageA((HWND)dwCallBack, wMsg, (WPARAM)hDev, dwParam1);
567 568
	break;
    case DCB_TASK: /* aka DCB_THREAD */
569
	ret = PostThreadMessageA(dwCallBack, wMsg, (WPARAM)hDev, dwParam1);
570 571
	break;
    case DCB_FUNCTION:
572 573
	((LPDRVCALLBACK)dwCallBack)(hDev, wMsg, dwUser, dwParam1, dwParam2);
	ret = TRUE;
574 575
	break;
    case DCB_EVENT:
576
	ret = SetEvent((HANDLE)dwCallBack);
577
	break;
578 579 580 581
#if 0
        /* FIXME: for now only usable in mmsystem.dll16
         * If needed, should be enabled back
         */
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    case 6: /* I would dub it DCB_MMTHREADSIGNAL */
	/* this is an undocumented DCB_ value used for mmThreads
	 * loword of dwCallBack contains the handle of the lpMMThd block
	 * which dwSignalCount has to be incremented
	 */     
        if (pFnGetMMThread16)
	{
	    WINE_MMTHREAD*	lpMMThd = pFnGetMMThread16(LOWORD(dwCallBack));

	    TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack), lpMMThd);
	    /* same as mmThreadSignal16 */
	    InterlockedIncrement(&lpMMThd->dwSignalCount);
	    SetEvent(lpMMThd->hEvent);
	    /* some other stuff on lpMMThd->hVxD */
	}
	break;
598
#endif
599 600 601 602 603 604 605 606 607
#if 0
    case 4:
	/* this is an undocumented DCB_ value for... I don't know */
	break;
#endif
    default:
	WARN("Unknown callback type %d\n", uFlags & DCB_TYPEMASK);
	return FALSE;
    }
608 609 610 611 612
    if (ret)
	TRACE("Done\n");
    else
	WARN("Notification failure\n");
    return ret;
613
}
614 615 616 617 618 619 620 621 622 623 624 625

/******************************************************************
 *		DRIVER_UnloadAll
 *
 *
 */
void    DRIVER_UnloadAll(void)
{
    LPWINE_DRIVER 	lpDrv;
    LPWINE_DRIVER 	lpNextDrv = NULL;
    unsigned            count = 0;

626 627 628
restart:
    EnterCriticalSection( &mmdriver_lock );

629 630 631
    for (lpDrv = lpDrvItemList; lpDrv != NULL; lpDrv = lpNextDrv)
    {
        lpNextDrv = lpDrv->lpNextItem;
632 633 634 635 636 637 638 639 640 641

        /* session instances will be unloaded automatically */
        if (!(lpDrv->dwFlags & WINE_GDF_SESSION))
        {
            LeaveCriticalSection( &mmdriver_lock );
            CloseDriver((HDRVR)lpDrv, 0, 0);
            count++;
            /* restart from the beginning of the list */
            goto restart;
        }
642
    }
643 644 645

    LeaveCriticalSection( &mmdriver_lock );

646 647
    TRACE("Unloaded %u drivers\n", count);
}