2 * ntoskrnl.exe implementation
4 * Copyright (C) 2007 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * 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
22 #include "wine/port.h"
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
30 #define WIN32_NO_STATUS
34 #include "wine/unicode.h"
35 #include "wine/server.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ntoskrnl
);
40 static inline LPCSTR
debugstr_us( const UNICODE_STRING
*us
)
42 if (!us
) return "<null>";
43 return debugstr_wn( us
->Buffer
, us
->Length
/ sizeof(WCHAR
) );
46 static HANDLE
get_device_manager(void)
48 static HANDLE device_manager
;
49 HANDLE handle
= 0, ret
= device_manager
;
53 SERVER_START_REQ( create_device_manager
)
55 req
->access
= SYNCHRONIZE
;
57 if (!wine_server_call( req
)) handle
= reply
->handle
;
63 ERR( "failed to create the device manager\n" );
66 if (!(ret
= InterlockedCompareExchangePointer( &device_manager
, handle
, 0 )))
69 NtClose( handle
); /* somebody beat us to it */
75 /***********************************************************************
76 * IoCreateDevice (NTOSKRNL.EXE.@)
78 NTSTATUS WINAPI
IoCreateDevice( DRIVER_OBJECT
*driver
, ULONG ext_size
,
79 UNICODE_STRING
*name
, DEVICE_TYPE type
,
80 ULONG characteristics
, BOOLEAN exclusive
,
81 DEVICE_OBJECT
**ret_device
)
84 DEVICE_OBJECT
*device
;
86 HANDLE manager
= get_device_manager();
88 TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
89 driver
, ext_size
, debugstr_us(name
), type
, characteristics
, exclusive
, ret_device
);
91 if (!(device
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*device
) + ext_size
)))
92 return STATUS_NO_MEMORY
;
94 SERVER_START_REQ( create_device
)
99 req
->manager
= manager
;
100 req
->user_ptr
= device
;
101 if (name
) wine_server_add_data( req
, name
->Buffer
, name
->Length
);
102 if (!(status
= wine_server_call( req
))) handle
= reply
->handle
;
106 if (status
== STATUS_SUCCESS
)
108 device
->DriverObject
= driver
;
109 device
->DeviceExtension
= device
+ 1;
110 device
->DeviceType
= type
;
111 device
->Reserved
= handle
;
113 device
->NextDevice
= driver
->DeviceObject
;
114 driver
->DeviceObject
= device
;
116 *ret_device
= device
;
118 else HeapFree( GetProcessHeap(), 0, device
);
124 /***********************************************************************
125 * IoDeleteDevice (NTOSKRNL.EXE.@)
127 void WINAPI
IoDeleteDevice( DEVICE_OBJECT
*device
)
131 TRACE( "%p\n", device
);
133 SERVER_START_REQ( delete_device
)
135 req
->handle
= device
->Reserved
;
136 status
= wine_server_call( req
);
140 if (status
== STATUS_SUCCESS
)
142 DEVICE_OBJECT
**prev
= &device
->DriverObject
->DeviceObject
;
143 while (*prev
&& *prev
!= device
) prev
= &(*prev
)->NextDevice
;
144 if (*prev
) *prev
= (*prev
)->NextDevice
;
145 NtClose( device
->Reserved
);
146 HeapFree( GetProcessHeap(), 0, device
);
151 /*****************************************************
154 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
158 case DLL_PROCESS_ATTACH
:
159 DisableThreadLibraryCalls( inst
);