2 * Dynamic devices support
4 * Copyright 2006 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"
30 #define NONAMELESSUNION
37 #include "wine/library.h"
38 #include "wine/list.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr
);
44 #define MAX_DOS_DRIVES 26
47 static const WCHAR drive_types
[][8] =
49 { 0 }, /* DEVICE_UNKNOWN */
50 { 0 }, /* DEVICE_HARDDISK */
51 {'h','d',0}, /* DEVICE_HARDDISK_VOL */
52 {'f','l','o','p','p','y',0}, /* DEVICE_FLOPPY */
53 {'c','d','r','o','m',0}, /* DEVICE_CDROM */
54 {'c','d','r','o','m',0}, /* DEVICE_DVD */
55 {'n','e','t','w','o','r','k',0}, /* DEVICE_NETWORK */
56 {'r','a','m','d','i','s','k',0} /* DEVICE_RAMDISK */
59 static const WCHAR drives_keyW
[] = {'S','o','f','t','w','a','r','e','\\',
60 'W','i','n','e','\\','D','r','i','v','e','s',0};
61 static const WCHAR ports_keyW
[] = {'S','o','f','t','w','a','r','e','\\',
62 'W','i','n','e','\\','P','o','r','t','s',0};
66 enum device_type type
; /* drive type */
67 DEVICE_OBJECT
*dev_obj
; /* disk device allocated for this volume */
68 UNICODE_STRING name
; /* device name */
69 UNICODE_STRING symlink
; /* device symlink if any */
70 STORAGE_DEVICE_NUMBER devnum
; /* device number info */
71 char *unix_device
; /* unix device path */
72 char *unix_mount
; /* unix mount point path */
77 struct list entry
; /* entry in volumes list */
78 struct disk_device
*device
; /* disk device */
79 char *udi
; /* unique identifier for dynamic volumes */
80 unsigned int ref
; /* ref count */
81 GUID guid
; /* volume uuid */
82 struct mount_point
*mount
; /* Volume{xxx} mount point */
87 struct list entry
; /* entry in drives list */
88 struct volume
*volume
; /* volume for this drive */
89 int drive
; /* drive letter (0 = A: etc.) */
90 struct mount_point
*mount
; /* DosDevices mount point */
93 static struct list drives_list
= LIST_INIT(drives_list
);
94 static struct list volumes_list
= LIST_INIT(volumes_list
);
96 static DRIVER_OBJECT
*harddisk_driver
;
97 static DRIVER_OBJECT
*serial_driver
;
98 static DRIVER_OBJECT
*parallel_driver
;
100 static CRITICAL_SECTION device_section
;
101 static CRITICAL_SECTION_DEBUG critsect_debug
=
103 0, 0, &device_section
,
104 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
105 0, 0, { (DWORD_PTR
)(__FILE__
": device_section") }
107 static CRITICAL_SECTION device_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
109 static char *get_dosdevices_path( char **device
)
111 const char *config_dir
= wine_get_config_dir();
112 size_t len
= strlen(config_dir
) + sizeof("/dosdevices/com256");
113 char *path
= HeapAlloc( GetProcessHeap(), 0, len
);
116 strcpy( path
, config_dir
);
117 strcat( path
, "/dosdevices/a::" );
118 *device
= path
+ len
- sizeof("com256");
123 static char *strdupA( const char *str
)
127 if (!str
) return NULL
;
128 if ((ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(str
) + 1 ))) strcpy( ret
, str
);
132 static const GUID
*get_default_uuid( int letter
)
136 guid
.Data4
[7] = 'A' + letter
;
140 /* read a Unix symlink; returned buffer must be freed by caller */
141 static char *read_symlink( const char *path
)
148 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
150 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
153 ret
= readlink( path
, buffer
, size
);
156 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
164 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
169 /* update a symlink if it changed; return TRUE if updated */
170 static void update_symlink( const char *path
, const char *dest
, const char *orig_dest
)
174 if (!orig_dest
|| strcmp( orig_dest
, dest
))
177 symlink( dest
, path
);
183 /* send notification about a change to a given drive */
184 static void send_notify( int drive
, int code
)
186 DEV_BROADCAST_VOLUME info
;
188 info
.dbcv_size
= sizeof(info
);
189 info
.dbcv_devicetype
= DBT_DEVTYP_VOLUME
;
190 info
.dbcv_reserved
= 0;
191 info
.dbcv_unitmask
= 1 << drive
;
192 info
.dbcv_flags
= DBTF_MEDIA
;
193 BroadcastSystemMessageW( BSF_FORCEIFHUNG
|BSF_QUERY
, NULL
,
194 WM_DEVICECHANGE
, code
, (LPARAM
)&info
);
197 /* create the disk device for a given volume */
198 static NTSTATUS
create_disk_device( enum device_type type
, struct disk_device
**device_ret
)
200 static const WCHAR harddiskvolW
[] = {'\\','D','e','v','i','c','e',
201 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
202 static const WCHAR harddiskW
[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
203 static const WCHAR cdromW
[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
204 static const WCHAR floppyW
[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
205 static const WCHAR ramdiskW
[] = {'\\','D','e','v','i','c','e','\\','R','a','m','d','i','s','k','%','u',0};
206 static const WCHAR cdromlinkW
[] = {'\\','?','?','\\','C','d','R','o','m','%','u',0};
207 static const WCHAR physdriveW
[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
211 const WCHAR
*format
= NULL
;
212 const WCHAR
*link_format
= NULL
;
214 DEVICE_OBJECT
*dev_obj
;
215 struct disk_device
*device
;
220 case DEVICE_HARDDISK
:
221 case DEVICE_NETWORK
: /* FIXME */
223 link_format
= physdriveW
;
225 case DEVICE_HARDDISK_VOL
:
226 format
= harddiskvolW
;
227 first
= 1; /* harddisk volumes start counting from 1 */
235 link_format
= cdromlinkW
;
242 name
.MaximumLength
= (strlenW(format
) + 10) * sizeof(WCHAR
);
243 name
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, name
.MaximumLength
);
244 for (i
= first
; i
< 32; i
++)
246 sprintfW( name
.Buffer
, format
, i
);
247 name
.Length
= strlenW(name
.Buffer
) * sizeof(WCHAR
);
248 status
= IoCreateDevice( harddisk_driver
, sizeof(*device
), &name
, 0, 0, FALSE
, &dev_obj
);
249 if (status
!= STATUS_OBJECT_NAME_COLLISION
) break;
253 device
= dev_obj
->DeviceExtension
;
254 device
->dev_obj
= dev_obj
;
257 device
->unix_device
= NULL
;
258 device
->unix_mount
= NULL
;
259 device
->symlink
.Buffer
= NULL
;
263 UNICODE_STRING symlink
;
265 symlink
.MaximumLength
= (strlenW(link_format
) + 10) * sizeof(WCHAR
);
266 if ((symlink
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, symlink
.MaximumLength
)))
268 sprintfW( symlink
.Buffer
, link_format
, i
);
269 symlink
.Length
= strlenW(symlink
.Buffer
) * sizeof(WCHAR
);
270 if (!IoCreateSymbolicLink( &symlink
, &name
)) device
->symlink
= symlink
;
278 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
279 device
->devnum
.DeviceNumber
= i
;
280 device
->devnum
.PartitionNumber
= ~0u;
283 device
->devnum
.DeviceType
= FILE_DEVICE_CD_ROM
;
284 device
->devnum
.DeviceNumber
= i
;
285 device
->devnum
.PartitionNumber
= ~0u;
288 device
->devnum
.DeviceType
= FILE_DEVICE_DVD
;
289 device
->devnum
.DeviceNumber
= i
;
290 device
->devnum
.PartitionNumber
= ~0u;
293 case DEVICE_HARDDISK
:
294 case DEVICE_NETWORK
: /* FIXME */
295 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
296 device
->devnum
.DeviceNumber
= i
;
297 device
->devnum
.PartitionNumber
= 0;
299 case DEVICE_HARDDISK_VOL
:
300 device
->devnum
.DeviceType
= FILE_DEVICE_DISK
;
301 device
->devnum
.DeviceNumber
= 0;
302 device
->devnum
.PartitionNumber
= i
;
305 *device_ret
= device
;
306 TRACE( "created device %s\n", debugstr_w(name
.Buffer
) );
310 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name
.Buffer
), status
);
311 RtlFreeUnicodeString( &name
);
316 /* delete the disk device for a given drive */
317 static void delete_disk_device( struct disk_device
*device
)
319 TRACE( "deleting device %s\n", debugstr_w(device
->name
.Buffer
) );
320 if (device
->symlink
.Buffer
)
322 IoDeleteSymbolicLink( &device
->symlink
);
323 RtlFreeUnicodeString( &device
->symlink
);
325 RtlFreeHeap( GetProcessHeap(), 0, device
->unix_device
);
326 RtlFreeHeap( GetProcessHeap(), 0, device
->unix_mount
);
327 RtlFreeUnicodeString( &device
->name
);
328 IoDeleteDevice( device
->dev_obj
);
331 /* grab another reference to a volume */
332 static struct volume
*grab_volume( struct volume
*volume
)
338 /* release a volume and delete the corresponding disk device when refcount is 0 */
339 static unsigned int release_volume( struct volume
*volume
)
341 unsigned int ret
= --volume
->ref
;
345 TRACE( "%s udi %s\n", debugstr_guid(&volume
->guid
), debugstr_a(volume
->udi
) );
346 assert( !volume
->udi
);
347 list_remove( &volume
->entry
);
348 if (volume
->mount
) delete_mount_point( volume
->mount
);
349 delete_disk_device( volume
->device
);
350 RtlFreeHeap( GetProcessHeap(), 0, volume
);
355 /* set the volume udi */
356 static void set_volume_udi( struct volume
*volume
, const char *udi
)
360 assert( !volume
->udi
);
361 /* having a udi means the HAL side holds an extra reference */
362 if ((volume
->udi
= strdupA( udi
))) grab_volume( volume
);
364 else if (volume
->udi
)
366 RtlFreeHeap( GetProcessHeap(), 0, volume
->udi
);
368 release_volume( volume
);
372 /* create a disk volume */
373 static NTSTATUS
create_volume( const char *udi
, enum device_type type
, struct volume
**volume_ret
)
375 struct volume
*volume
;
378 if (!(volume
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*volume
) )))
379 return STATUS_NO_MEMORY
;
381 if (!(status
= create_disk_device( type
, &volume
->device
)))
383 if (udi
) set_volume_udi( volume
, udi
);
384 list_add_tail( &volumes_list
, &volume
->entry
);
385 *volume_ret
= grab_volume( volume
);
387 else RtlFreeHeap( GetProcessHeap(), 0, volume
);
392 /* create the disk device for a given volume */
393 static NTSTATUS
create_dos_device( struct volume
*volume
, const char *udi
, int letter
,
394 enum device_type type
, struct dos_drive
**drive_ret
)
396 struct dos_drive
*drive
;
399 if (!(drive
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*drive
) ))) return STATUS_NO_MEMORY
;
400 drive
->drive
= letter
;
405 if (udi
) set_volume_udi( volume
, udi
);
406 drive
->volume
= grab_volume( volume
);
407 status
= STATUS_SUCCESS
;
409 else status
= create_volume( udi
, type
, &drive
->volume
);
411 if (status
== STATUS_SUCCESS
)
413 list_add_tail( &drives_list
, &drive
->entry
);
416 else RtlFreeHeap( GetProcessHeap(), 0, drive
);
421 /* delete the disk device for a given drive */
422 static void delete_dos_device( struct dos_drive
*drive
)
424 list_remove( &drive
->entry
);
425 if (drive
->mount
) delete_mount_point( drive
->mount
);
426 release_volume( drive
->volume
);
427 RtlFreeHeap( GetProcessHeap(), 0, drive
);
430 /* find a volume that matches the parameters */
431 static struct volume
*find_matching_volume( const char *udi
, const char *device
,
432 const char *mount_point
, enum device_type type
)
434 struct volume
*volume
;
435 struct disk_device
*disk_device
;
437 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
441 /* when we have a udi we only match drives added manually */
442 if (udi
&& volume
->udi
) continue;
443 /* and when we don't have a udi we only match dynamic drives */
444 if (!udi
&& !volume
->udi
) continue;
446 disk_device
= volume
->device
;
447 if (disk_device
->type
!= type
) continue;
448 if (device
&& disk_device
->unix_device
)
450 if (strcmp( device
, disk_device
->unix_device
)) continue;
453 if (mount_point
&& disk_device
->unix_mount
)
455 if (strcmp( mount_point
, disk_device
->unix_mount
)) continue;
458 if (!match
) continue;
459 TRACE( "found matching volume %s for device %s mount %s type %u\n",
460 debugstr_guid(&volume
->guid
), debugstr_a(device
), debugstr_a(mount_point
), type
);
461 return grab_volume( volume
);
466 /* change the information for an existing volume */
467 static NTSTATUS
set_volume_info( struct volume
*volume
, struct dos_drive
*drive
, const char *device
,
468 const char *mount_point
, enum device_type type
, const GUID
*guid
)
471 unsigned int id_len
= 0;
472 struct disk_device
*disk_device
= volume
->device
;
475 if (type
!= disk_device
->type
)
477 if ((status
= create_disk_device( type
, &disk_device
))) return status
;
480 delete_mount_point( volume
->mount
);
481 volume
->mount
= NULL
;
483 if (drive
&& drive
->mount
)
485 delete_mount_point( drive
->mount
);
488 delete_disk_device( volume
->device
);
489 volume
->device
= disk_device
;
493 RtlFreeHeap( GetProcessHeap(), 0, disk_device
->unix_device
);
494 RtlFreeHeap( GetProcessHeap(), 0, disk_device
->unix_mount
);
496 disk_device
->unix_device
= strdupA( device
);
497 disk_device
->unix_mount
= strdupA( mount_point
);
499 if (guid
&& memcmp( &volume
->guid
, guid
, sizeof(volume
->guid
) ))
501 volume
->guid
= *guid
;
504 delete_mount_point( volume
->mount
);
505 volume
->mount
= NULL
;
510 volume
->mount
= add_volume_mount_point( disk_device
->dev_obj
, &disk_device
->name
, &volume
->guid
);
511 if (drive
&& !drive
->mount
)
512 drive
->mount
= add_dosdev_mount_point( disk_device
->dev_obj
, &disk_device
->name
, drive
->drive
);
514 if (disk_device
->unix_mount
)
516 id
= disk_device
->unix_mount
;
517 id_len
= strlen( disk_device
->unix_mount
) + 1;
519 if (volume
->mount
) set_mount_point_id( volume
->mount
, id
, id_len
);
520 if (drive
&& drive
->mount
) set_mount_point_id( drive
->mount
, id
, id_len
);
522 return STATUS_SUCCESS
;
525 /* change the drive letter or volume for an existing drive */
526 static void set_drive_info( struct dos_drive
*drive
, int letter
, struct volume
*volume
)
528 if (drive
->drive
!= letter
)
530 if (drive
->mount
) delete_mount_point( drive
->mount
);
532 drive
->drive
= letter
;
534 if (drive
->volume
!= volume
)
536 if (drive
->mount
) delete_mount_point( drive
->mount
);
538 grab_volume( volume
);
539 release_volume( drive
->volume
);
540 drive
->volume
= volume
;
544 static inline BOOL
is_valid_device( struct stat
*st
)
546 #if defined(linux) || defined(__sun__)
547 return S_ISBLK( st
->st_mode
);
549 /* disks are char devices on *BSD */
550 return S_ISCHR( st
->st_mode
);
554 /* find or create a DOS drive for the corresponding device */
555 static int add_drive( const char *device
, enum device_type type
)
559 struct stat dev_st
, drive_st
;
560 int drive
, first
, last
, avail
= 0;
562 if (stat( device
, &dev_st
) == -1 || !is_valid_device( &dev_st
)) return -1;
564 if (!(path
= get_dosdevices_path( &p
))) return -1;
566 memset( in_use
, 0, sizeof(in_use
) );
588 for (drive
= first
; drive
< last
; drive
++)
590 if (in_use
[drive
]) continue; /* already checked */
592 if (stat( path
, &drive_st
) == -1)
594 if (lstat( path
, &drive_st
) == -1 && errno
== ENOENT
) /* this is a candidate */
599 /* if mount point symlink doesn't exist either, it's available */
600 if (lstat( path
, &drive_st
) == -1 && errno
== ENOENT
) avail
= drive
;
604 else in_use
[drive
] = 1;
609 if (!is_valid_device( &drive_st
)) continue;
610 if (dev_st
.st_rdev
== drive_st
.st_rdev
) goto done
;
615 /* try to use the one we found */
618 if (symlink( device
, path
) != -1) goto done
;
619 /* failed, retry the search */
625 HeapFree( GetProcessHeap(), 0, path
);
629 /* create devices for mapped drives */
630 static void create_drive_devices(void)
632 char *path
, *p
, *link
, *device
;
633 struct dos_drive
*drive
;
634 struct volume
*volume
;
637 enum device_type drive_type
;
638 WCHAR driveW
[] = {'a',':',0};
640 if (!(path
= get_dosdevices_path( &p
))) return;
641 if (RegOpenKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &drives_key
)) drives_key
= 0;
643 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++)
647 if (!(link
= read_symlink( path
))) continue;
649 device
= read_symlink( path
);
651 drive_type
= i
< 2 ? DEVICE_FLOPPY
: DEVICE_HARDDISK_VOL
;
655 DWORD j
, type
, size
= sizeof(buffer
);
658 if (!RegQueryValueExW( drives_key
, driveW
, NULL
, &type
, (BYTE
*)buffer
, &size
) &&
661 for (j
= 0; j
< sizeof(drive_types
)/sizeof(drive_types
[0]); j
++)
662 if (drive_types
[j
][0] && !strcmpiW( buffer
, drive_types
[j
] ))
667 if (drive_type
== DEVICE_FLOPPY
&& i
>= 2) drive_type
= DEVICE_HARDDISK
;
671 volume
= find_matching_volume( NULL
, device
, link
, drive_type
);
672 if (!create_dos_device( volume
, NULL
, i
, drive_type
, &drive
))
674 /* don't reset uuid if we used an existing volume */
675 const GUID
*guid
= volume
? NULL
: get_default_uuid(i
);
676 set_volume_info( drive
->volume
, drive
, device
, link
, drive_type
, guid
);
680 RtlFreeHeap( GetProcessHeap(), 0, link
);
681 RtlFreeHeap( GetProcessHeap(), 0, device
);
683 if (volume
) release_volume( volume
);
685 RegCloseKey( drives_key
);
686 RtlFreeHeap( GetProcessHeap(), 0, path
);
689 /* create a new disk volume */
690 NTSTATUS
add_volume( const char *udi
, const char *device
, const char *mount_point
,
691 enum device_type type
, const GUID
*guid
)
693 struct volume
*volume
;
694 NTSTATUS status
= STATUS_SUCCESS
;
696 TRACE( "adding %s device %s mount %s type %u uuid %s\n", debugstr_a(udi
),
697 debugstr_a(device
), debugstr_a(mount_point
), type
, debugstr_guid(guid
) );
699 EnterCriticalSection( &device_section
);
700 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
701 if (volume
->udi
&& !strcmp( udi
, volume
->udi
))
703 grab_volume( volume
);
707 /* udi not found, search for a non-dynamic volume */
708 if ((volume
= find_matching_volume( udi
, device
, mount_point
, type
))) set_volume_udi( volume
, udi
);
709 else status
= create_volume( udi
, type
, &volume
);
712 if (!status
) status
= set_volume_info( volume
, NULL
, device
, mount_point
, type
, guid
);
713 if (volume
) release_volume( volume
);
714 LeaveCriticalSection( &device_section
);
718 /* create a new disk volume */
719 NTSTATUS
remove_volume( const char *udi
)
721 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
722 struct volume
*volume
;
724 EnterCriticalSection( &device_section
);
725 LIST_FOR_EACH_ENTRY( volume
, &volumes_list
, struct volume
, entry
)
727 if (!volume
->udi
|| strcmp( udi
, volume
->udi
)) continue;
728 set_volume_udi( volume
, NULL
);
729 status
= STATUS_SUCCESS
;
732 LeaveCriticalSection( &device_section
);
737 /* create a new dos drive */
738 NTSTATUS
add_dos_device( int letter
, const char *udi
, const char *device
,
739 const char *mount_point
, enum device_type type
, const GUID
*guid
)
743 NTSTATUS status
= STATUS_SUCCESS
;
744 struct dos_drive
*drive
, *next
;
745 struct volume
*volume
;
748 if (!(path
= get_dosdevices_path( &p
))) return STATUS_NO_MEMORY
;
750 EnterCriticalSection( &device_section
);
751 volume
= find_matching_volume( udi
, device
, mount_point
, type
);
753 if (letter
== -1) /* auto-assign a letter */
755 letter
= add_drive( device
, type
);
758 status
= STATUS_OBJECT_NAME_COLLISION
;
762 LIST_FOR_EACH_ENTRY_SAFE( drive
, next
, &drives_list
, struct dos_drive
, entry
)
764 if (drive
->volume
->udi
&& !strcmp( udi
, drive
->volume
->udi
)) goto found
;
765 if (drive
->drive
== letter
) delete_dos_device( drive
);
768 else /* simply reset the device symlink */
770 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
771 if (drive
->drive
== letter
) break;
774 if (&drive
->entry
== &drives_list
) update_symlink( path
, device
, NULL
);
777 update_symlink( path
, device
, drive
->volume
->device
->unix_device
);
778 delete_dos_device( drive
);
782 if ((status
= create_dos_device( volume
, udi
, letter
, type
, &drive
))) goto done
;
785 if (!guid
&& !volume
) guid
= get_default_uuid( letter
);
786 if (!volume
) volume
= grab_volume( drive
->volume
);
787 set_drive_info( drive
, letter
, volume
);
788 p
[0] = 'a' + drive
->drive
;
790 update_symlink( path
, mount_point
, volume
->device
->unix_mount
);
791 set_volume_info( volume
, drive
, device
, mount_point
, type
, guid
);
793 TRACE( "added device %c: udi %s for %s on %s type %u\n",
794 'a' + drive
->drive
, wine_dbgstr_a(udi
), wine_dbgstr_a(device
),
795 wine_dbgstr_a(mount_point
), type
);
797 /* hack: force the drive type in the registry */
798 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &hkey
))
800 const WCHAR
*type_name
= drive_types
[type
];
801 WCHAR name
[] = {'a',':',0};
803 name
[0] += drive
->drive
;
804 if (!type_name
[0] && type
== DEVICE_HARDDISK
) type_name
= drive_types
[DEVICE_FLOPPY
];
806 RegSetValueExW( hkey
, name
, 0, REG_SZ
, (const BYTE
*)type_name
,
807 (strlenW(type_name
) + 1) * sizeof(WCHAR
) );
809 RegDeleteValueW( hkey
, name
);
813 if (udi
) notify
= drive
->drive
;
816 if (volume
) release_volume( volume
);
817 LeaveCriticalSection( &device_section
);
818 RtlFreeHeap( GetProcessHeap(), 0, path
);
819 if (notify
!= -1) send_notify( notify
, DBT_DEVICEARRIVAL
);
823 /* remove an existing dos drive, by letter or udi */
824 NTSTATUS
remove_dos_device( int letter
, const char *udi
)
826 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
828 struct dos_drive
*drive
;
832 EnterCriticalSection( &device_section
);
833 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
837 if (!drive
->volume
->udi
) continue;
838 if (strcmp( udi
, drive
->volume
->udi
)) continue;
839 set_volume_udi( drive
->volume
, NULL
);
841 else if (drive
->drive
!= letter
) continue;
843 if ((path
= get_dosdevices_path( &p
)))
845 p
[0] = 'a' + drive
->drive
;
848 RtlFreeHeap( GetProcessHeap(), 0, path
);
851 /* clear the registry key too */
852 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE
, drives_keyW
, &hkey
))
854 WCHAR name
[] = {'a',':',0};
855 name
[0] += drive
->drive
;
856 RegDeleteValueW( hkey
, name
);
860 if (udi
&& drive
->volume
->device
->unix_mount
) notify
= drive
->drive
;
862 delete_dos_device( drive
);
863 status
= STATUS_SUCCESS
;
866 LeaveCriticalSection( &device_section
);
867 if (notify
!= -1) send_notify( notify
, DBT_DEVICEREMOVECOMPLETE
);
871 /* query information about an existing dos drive, by letter or udi */
872 NTSTATUS
query_dos_device( int letter
, enum device_type
*type
, char **device
, char **mount_point
)
874 NTSTATUS status
= STATUS_NO_SUCH_DEVICE
;
875 struct dos_drive
*drive
;
876 struct disk_device
*disk_device
;
878 EnterCriticalSection( &device_section
);
879 LIST_FOR_EACH_ENTRY( drive
, &drives_list
, struct dos_drive
, entry
)
881 if (drive
->drive
!= letter
) continue;
882 disk_device
= drive
->volume
->device
;
883 if (type
) *type
= disk_device
->type
;
884 if (device
) *device
= strdupA( disk_device
->unix_device
);
885 if (mount_point
) *mount_point
= strdupA( disk_device
->unix_mount
);
886 status
= STATUS_SUCCESS
;
889 LeaveCriticalSection( &device_section
);
893 /* handler for ioctls on the harddisk device */
894 static NTSTATUS WINAPI
harddisk_ioctl( DEVICE_OBJECT
*device
, IRP
*irp
)
896 IO_STACK_LOCATION
*irpsp
= IoGetCurrentIrpStackLocation( irp
);
897 struct disk_device
*dev
= device
->DeviceExtension
;
899 TRACE( "ioctl %x insize %u outsize %u\n",
900 irpsp
->Parameters
.DeviceIoControl
.IoControlCode
,
901 irpsp
->Parameters
.DeviceIoControl
.InputBufferLength
,
902 irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
904 EnterCriticalSection( &device_section
);
906 switch(irpsp
->Parameters
.DeviceIoControl
.IoControlCode
)
908 case IOCTL_DISK_GET_DRIVE_GEOMETRY
:
911 DWORD len
= min( sizeof(info
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
913 info
.Cylinders
.QuadPart
= 10000;
914 info
.MediaType
= (dev
->devnum
.DeviceType
== FILE_DEVICE_DISK
) ? FixedMedia
: RemovableMedia
;
915 info
.TracksPerCylinder
= 255;
916 info
.SectorsPerTrack
= 63;
917 info
.BytesPerSector
= 512;
918 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &info
, len
);
919 irp
->IoStatus
.Information
= len
;
920 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
923 case IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
:
925 DISK_GEOMETRY_EX info
;
926 DWORD len
= min( sizeof(info
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
928 FIXME("The DISK_PARTITION_INFO and DISK_DETECTION_INFO structures will not be filled\n");
930 info
.Geometry
.Cylinders
.QuadPart
= 10000;
931 info
.Geometry
.MediaType
= (dev
->devnum
.DeviceType
== FILE_DEVICE_DISK
) ? FixedMedia
: RemovableMedia
;
932 info
.Geometry
.TracksPerCylinder
= 255;
933 info
.Geometry
.SectorsPerTrack
= 63;
934 info
.Geometry
.BytesPerSector
= 512;
935 info
.DiskSize
.QuadPart
= info
.Geometry
.Cylinders
.QuadPart
* info
.Geometry
.TracksPerCylinder
*
936 info
.Geometry
.SectorsPerTrack
* info
.Geometry
.BytesPerSector
;
938 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &info
, len
);
939 irp
->IoStatus
.Information
= len
;
940 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
943 case IOCTL_STORAGE_GET_DEVICE_NUMBER
:
945 DWORD len
= min( sizeof(dev
->devnum
), irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
947 memcpy( irp
->AssociatedIrp
.SystemBuffer
, &dev
->devnum
, len
);
948 irp
->IoStatus
.Information
= len
;
949 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
952 case IOCTL_CDROM_READ_TOC
:
953 irp
->IoStatus
.u
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
955 case IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
:
957 DWORD len
= min( 32, irpsp
->Parameters
.DeviceIoControl
.OutputBufferLength
);
959 FIXME( "returning zero-filled buffer for IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS\n" );
960 memset( irp
->AssociatedIrp
.SystemBuffer
, 0, len
);
961 irp
->IoStatus
.Information
= len
;
962 irp
->IoStatus
.u
.Status
= STATUS_SUCCESS
;
967 ULONG code
= irpsp
->Parameters
.DeviceIoControl
.IoControlCode
;
968 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
969 code
, code
>> 16, (code
>> 14) & 3, (code
>> 2) & 0xfff, code
& 3);
970 irp
->IoStatus
.u
.Status
= STATUS_NOT_SUPPORTED
;
975 LeaveCriticalSection( &device_section
);
976 IoCompleteRequest( irp
, IO_NO_INCREMENT
);
977 return STATUS_SUCCESS
;
980 /* driver entry point for the harddisk driver */
981 NTSTATUS WINAPI
harddisk_driver_entry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
983 struct disk_device
*device
;
985 harddisk_driver
= driver
;
986 driver
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = harddisk_ioctl
;
988 /* create a harddisk0 device that isn't assigned to any drive */
989 create_disk_device( DEVICE_HARDDISK
, &device
);
991 create_drive_devices();
993 return STATUS_SUCCESS
;
997 /* create a serial or parallel port */
998 static BOOL
create_port_device( DRIVER_OBJECT
*driver
, int n
, const char *unix_path
, const char *dosdevices_path
, char *p
,
999 HKEY wine_ports_key
, HKEY windows_ports_key
)
1001 static const WCHAR comW
[] = {'C','O','M','%','u',0};
1002 static const WCHAR lptW
[] = {'L','P','T','%','u',0};
1003 static const WCHAR device_serialW
[] = {'\\','D','e','v','i','c','e','\\','S','e','r','i','a','l','%','u',0};
1004 static const WCHAR device_parallelW
[] = {'\\','D','e','v','i','c','e','\\','P','a','r','a','l','l','e','l','%','u',0};
1005 static const WCHAR dosdevices_comW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','%','u',0};
1006 static const WCHAR dosdevices_auxW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','A','U','X',0};
1007 static const WCHAR dosdevices_lptW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','%','u',0};
1008 static const WCHAR dosdevices_prnW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','P','R','N',0};
1009 const WCHAR
*dos_name_format
, *nt_name_format
, *reg_value_format
, *symlink_format
, *default_device
;
1010 WCHAR dos_name
[7], reg_value
[256], nt_buffer
[32], symlink_buffer
[32];
1012 char override_path
[256];
1013 UNICODE_STRING nt_name
, symlink_name
, default_name
;
1014 DEVICE_OBJECT
*dev_obj
;
1017 if (driver
== serial_driver
)
1019 dos_name_format
= comW
;
1020 nt_name_format
= device_serialW
;
1021 reg_value_format
= comW
;
1022 symlink_format
= dosdevices_comW
;
1023 default_device
= dosdevices_auxW
;
1027 dos_name_format
= lptW
;
1028 nt_name_format
= device_parallelW
;
1029 reg_value_format
= dosdevices_lptW
;
1030 symlink_format
= dosdevices_lptW
;
1031 default_device
= dosdevices_prnW
;
1034 sprintfW( dos_name
, dos_name_format
, n
);
1036 /* check for override */
1037 size
= sizeof(reg_value
);
1038 if (RegQueryValueExW( wine_ports_key
, dos_name
, NULL
, &type
, (BYTE
*)reg_value
, &size
) == 0 && type
== REG_SZ
)
1040 if (!reg_value
[0] || !WideCharToMultiByte( CP_UNIXCP
, WC_ERR_INVALID_CHARS
, reg_value
, size
/sizeof(WCHAR
),
1041 override_path
, sizeof(override_path
), NULL
, NULL
))
1043 unix_path
= override_path
;
1048 /* create DOS device */
1049 sprintf( p
, "%u", n
);
1050 if (symlink( unix_path
, dosdevices_path
) != 0)
1053 /* create NT device */
1054 sprintfW( nt_buffer
, nt_name_format
, n
- 1 );
1055 RtlInitUnicodeString( &nt_name
, nt_buffer
);
1056 status
= IoCreateDevice( driver
, 0, &nt_name
, 0, 0, FALSE
, &dev_obj
);
1057 if (status
!= STATUS_SUCCESS
)
1059 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(nt_name
.Buffer
), status
);
1062 sprintfW( symlink_buffer
, symlink_format
, n
);
1063 RtlInitUnicodeString( &symlink_name
, symlink_buffer
);
1064 IoCreateSymbolicLink( &symlink_name
, &nt_name
);
1067 RtlInitUnicodeString( &default_name
, default_device
);
1068 IoCreateSymbolicLink( &default_name
, &symlink_name
);
1071 /* TODO: store information about the Unix device in the NT device */
1073 /* create registry entry */
1074 sprintfW( reg_value
, reg_value_format
, n
);
1075 RegSetValueExW( windows_ports_key
, nt_name
.Buffer
, 0, REG_SZ
,
1076 (BYTE
*)reg_value
, (strlenW( reg_value
) + 1) * sizeof(WCHAR
) );
1081 /* find and create serial or parallel ports */
1082 static void create_port_devices( DRIVER_OBJECT
*driver
)
1084 static const char *serial_search_paths
[] = {
1089 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1091 #elif defined(__DragonFly__)
1097 static const char *parallel_search_paths
[] = {
1104 static const WCHAR serialcomm_keyW
[] = {'H','A','R','D','W','A','R','E','\\',
1105 'D','E','V','I','C','E','M','A','P','\\',
1106 'S','E','R','I','A','L','C','O','M','M',0};
1107 static const WCHAR parallel_ports_keyW
[] = {'H','A','R','D','W','A','R','E','\\',
1108 'D','E','V','I','C','E','M','A','P','\\',
1109 'P','A','R','A','L','L','E','L',' ','P','O','R','T','S',0};
1110 const char **search_paths
;
1111 const WCHAR
*windows_ports_key_name
;
1112 char *dosdevices_path
, *p
;
1113 HKEY wine_ports_key
= NULL
, windows_ports_key
= NULL
;
1114 char unix_path
[256];
1115 int num_search_paths
, i
, j
, n
;
1117 if (!(dosdevices_path
= get_dosdevices_path( &p
)))
1120 if (driver
== serial_driver
)
1125 search_paths
= serial_search_paths
;
1126 num_search_paths
= sizeof(serial_search_paths
)/sizeof(serial_search_paths
[0]);
1127 windows_ports_key_name
= serialcomm_keyW
;
1134 search_paths
= parallel_search_paths
;
1135 num_search_paths
= sizeof(parallel_search_paths
)/sizeof(parallel_search_paths
[0]);
1136 windows_ports_key_name
= parallel_ports_keyW
;
1140 RegCreateKeyExW( HKEY_LOCAL_MACHINE
, ports_keyW
, 0, NULL
, 0,
1141 KEY_QUERY_VALUE
, NULL
, &wine_ports_key
, NULL
);
1142 RegCreateKeyExW( HKEY_LOCAL_MACHINE
, windows_ports_key_name
, 0, NULL
, REG_OPTION_VOLATILE
,
1143 KEY_ALL_ACCESS
, NULL
, &windows_ports_key
, NULL
);
1145 /* remove old symlinks */
1146 for (n
= 1; n
<= MAX_PORTS
; n
++)
1148 sprintf( p
, "%u", n
);
1149 if (unlink( dosdevices_path
) != 0 && errno
== ENOENT
)
1153 /* look for ports in the usual places */
1155 for (i
= 0; i
< num_search_paths
; i
++)
1157 for (j
= 0; n
<= MAX_PORTS
; j
++)
1159 sprintf( unix_path
, search_paths
[i
], j
);
1160 if (access( unix_path
, F_OK
) != 0)
1163 create_port_device( driver
, n
, unix_path
, dosdevices_path
, p
, wine_ports_key
, windows_ports_key
);
1168 /* add any extra user-defined serial ports */
1169 while (n
<= MAX_PORTS
)
1171 if (!create_port_device( driver
, n
, NULL
, dosdevices_path
, p
, wine_ports_key
, windows_ports_key
))
1176 RegCloseKey( wine_ports_key
);
1177 RegCloseKey( windows_ports_key
);
1178 HeapFree( GetProcessHeap(), 0, dosdevices_path
);
1181 /* driver entry point for the serial port driver */
1182 NTSTATUS WINAPI
serial_driver_entry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
1184 serial_driver
= driver
;
1185 /* TODO: fill in driver->MajorFunction */
1187 create_port_devices( driver
);
1189 return STATUS_SUCCESS
;
1192 /* driver entry point for the parallel port driver */
1193 NTSTATUS WINAPI
parallel_driver_entry( DRIVER_OBJECT
*driver
, UNICODE_STRING
*path
)
1195 parallel_driver
= driver
;
1196 /* TODO: fill in driver->MajorFunction */
1198 create_port_devices( driver
);
1200 return STATUS_SUCCESS
;