aspi.c 4.25 KB
Newer Older
1
/**************************************************************************
2 3
 * ASPI routines
 * Copyright (C) 2000 David Elliott <dfe@infinite-internet.net>
4
 * Copyright (C) 2005 Vitaliy Margolen
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 */
20 21 22 23 24 25 26 27 28 29 30 31 32

/* These routines are to be called from either WNASPI32 or WINASPI */

/* FIXME:
 * - No way to override automatic /proc detection, maybe provide an
 *   HKEY_LOCAL_MACHINE\Software\Wine\Wine\Scsi regkey
 * - Somewhat debating an #ifdef linux... technically all this code will
 *   run on another UNIX.. it will fail nicely.
 * - Please add support for mapping multiple channels on host adapters to
 *   aspi controllers, e-mail me if you need help.
 */

#include <stdio.h>
33
#include <stdarg.h>
34
#include <sys/types.h>
35
#include <string.h>
36
#include <stdlib.h>
37

38 39
#include "windef.h"
#include "winbase.h"
40 41 42 43
#include "winreg.h"
#include "winerror.h"
#include "winescsi.h"

44 45
#include "wine/debug.h"

46
WINE_DEFAULT_DEBUG_CHANNEL(aspi);
47

48 49
static const WCHAR wDevicemapScsi[] = {'H','A','R','D','W','A','R','E','\\','D','E','V','I','C','E','M','A','P','\\','S','c','s','i',0};

50
/* Exported functions */
51
int ASPI_GetNumControllers(void)
52
{
53 54
    HKEY hkeyScsi, hkeyPort;
    DWORD i = 0, numPorts, num_ha = 0;
55
    WCHAR wPortName[15];
56

57 58 59 60 61 62
    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wDevicemapScsi, 0,
        KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hkeyScsi) != ERROR_SUCCESS )
    {
        ERR("Could not open HKLM\\%s\n", debugstr_w(wDevicemapScsi));
        return 0;
    }
63
    while (RegEnumKeyW(hkeyScsi, i++, wPortName, ARRAY_SIZE(wPortName)) == ERROR_SUCCESS)
64 65 66 67 68 69 70 71 72 73 74
    {
        if (RegOpenKeyExW(hkeyScsi, wPortName, 0, KEY_QUERY_VALUE, &hkeyPort) == ERROR_SUCCESS)
        {
            if (RegQueryInfoKeyW(hkeyPort, NULL, NULL, NULL, &numPorts, NULL, NULL,
                             NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
            {
                num_ha += numPorts;
            }
            RegCloseKey(hkeyPort);
        }
    }
75

76
    RegCloseKey(hkeyScsi);
77
    TRACE("Returning %ld host adapters\n", num_ha );
78
    return num_ha;
79 80 81 82 83 84 85
}

/* SCSI_GetHCforController
 * RETURNS
 * 	HIWORD: Host Adapter
 * 	LOWORD: Channel
 */
86
DWORD ASPI_GetHCforController( int controller )
87
{
88 89 90
    HKEY hkeyScsi, hkeyPort;
    DWORD i = 0, numPorts;
    int num_ha = controller + 1;
91 92
    WCHAR wPortName[15];
    WCHAR wBusName[15];
93

94 95 96 97 98 99
    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wDevicemapScsi, 0,
        KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hkeyScsi) != ERROR_SUCCESS )
    {
        ERR("Could not open HKLM\\%s\n", debugstr_w(wDevicemapScsi));
        return 0xFFFFFFFF;
    }
100
    while (RegEnumKeyW(hkeyScsi, i++, wPortName, ARRAY_SIZE(wPortName)) == ERROR_SUCCESS)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    {
        if (RegOpenKeyExW(hkeyScsi, wPortName, 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS,
                          &hkeyPort) == ERROR_SUCCESS)
        {
            if (RegQueryInfoKeyW(hkeyPort, NULL, NULL, NULL, &numPorts, NULL, NULL,
                             NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
            {
                num_ha -= numPorts;
                if (num_ha <= 0) break;
            }
            else
            RegCloseKey(hkeyPort);
        }
    }
    RegCloseKey(hkeyScsi);

    if (num_ha > 0)
    {
        ERR("Invalid controller(%d)\n", controller);
        return 0xFFFFFFFF;
    }

123
    if (RegEnumKeyW(hkeyPort, -num_ha, wBusName, ARRAY_SIZE(wBusName)) != ERROR_SUCCESS)
124 125 126 127 128 129
    {
        ERR("Failed to enumerate keys\n");
        RegCloseKey(hkeyPort);
        return 0xFFFFFFFF;
    }
    RegCloseKey(hkeyPort);
130

131
    return (wcstol(&wPortName[9], NULL, 10) << 16) + wcstol(&wBusName[9], NULL, 10);
Eric Pouech's avatar
Eric Pouech committed
132
}