1
/***************************************************************************
4
* probe-storage.c : storage prober
6
* Copyright (C) 2006 Jean-Yves Lefort <jylefort@FreeBSD.org>
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
**************************************************************************/
33
#include <sys/types.h>
34
#include <sys/ioctl.h>
36
#include <netinet/in.h>
38
#include <libvolume_id.h>
40
#include "libhal/libhal.h"
42
#include "../libprobe/hfp.h"
43
#include "../libprobe/hfp-cdrom.h"
45
#include "freebsd_dvd_rw_utils.h"
48
hf_probe_storage_get_cdrom_capabilities (const char *device_file,
52
HFPCDROMCapabilities caps;
53
gboolean status = FALSE;
59
g_return_if_fail(device_file != NULL);
60
g_return_if_fail(parent != NULL);
62
cdrom = hfp_cdrom_new(device_file, parent);
65
hfp_warning("unable to open CD-ROM device %s", device_file);
69
/* according to sys/dev/ata/atapi-cd.c some buggy drives need this loop */
70
for (i = 0; i < 5; i++)
72
static char ccb[16] = { HFP_CDROM_MODE_SENSE_BIG, 0, HFP_CDROM_CAP_PAGE, 0, 0, 0, 0, sizeof(caps) >> 8, sizeof(caps) };
75
if (! hfp_cdrom_send_ccb(cdrom, ccb, 10, HFP_CDROM_DIRECTION_IN, &caps, sizeof(caps), &err))
77
hfp_warning("%s: unable to get capabilities: %s", device_file, err);
81
if (caps.page_code != HFP_CDROM_CAP_PAGE)
83
hfp_warning("%s: bad page code %i", device_file, caps.page_code);
93
if ((caps.media & HFP_CDROM_MST_WRITE_CDR) != 0)
94
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.cdr", TRUE, &hfp_error);
95
if ((caps.media & HFP_CDROM_MST_WRITE_CDRW) != 0)
96
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.cdrw", TRUE, &hfp_error);
97
if ((caps.media & HFP_CDROM_MST_READ_DVDROM) != 0)
111
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.dvd", TRUE, &hfp_error);
113
profile = get_dvd_r_rw_profile(cdrom);
114
r = (profile & DRIVE_CDROM_CAPS_DVDPLUSR) != 0;
115
rw = (profile & DRIVE_CDROM_CAPS_DVDRW) != 0;
116
rdl = (profile & DRIVE_CDROM_CAPS_DVDPLUSRDL) != 0;
117
rwdl = (profile & DRIVE_CDROM_CAPS_DVDPLUSRWDL) != 0;
118
bd = (profile & DRIVE_CDROM_CAPS_BDROM) != 0;
119
bdr = (profile & DRIVE_CDROM_CAPS_BDR) != 0;
120
bdre = (profile & DRIVE_CDROM_CAPS_BDRE) != 0;
121
hddvd = (profile & DRIVE_CDROM_CAPS_HDDVDROM) != 0;
122
hddvdr = (profile & DRIVE_CDROM_CAPS_HDDVDR) != 0;
123
hddvdrw = (profile & DRIVE_CDROM_CAPS_HDDVDRW) != 0;
125
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.dvdplusr", r, &hfp_error);
126
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.dvdplusrw", rw, &hfp_error);
127
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.dvdplusrdl", rdl, &hfp_error);
128
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.dvdplusrwdl", rwdl, &hfp_error);
129
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.bd", bd, &hfp_error);
130
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.bdr", bdr, &hfp_error);
131
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.bdre", bdre, &hfp_error);
132
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.hddvd", hddvd, &hfp_error);
133
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.hddvdr", hddvdr, &hfp_error);
134
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.hddvdrw", hddvdrw, &hfp_error);
136
if ((caps.media & HFP_CDROM_MST_WRITE_DVDR) != 0)
137
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.dvdr", TRUE, &hfp_error);
138
if ((caps.media & HFP_CDROM_MST_WRITE_DVDRAM) != 0)
139
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.cdrom.dvdram", TRUE, &hfp_error);
141
if (get_read_write_speed(cdrom, &read_speed, &write_speed, &write_speeds) >= 0)
143
libhal_device_set_property_int(hfp_ctx, hfp_udi, "storage.cdrom.read_speed", read_speed, &hfp_error);
146
libhal_device_set_property_int(hfp_ctx, hfp_udi, "storage.cdrom.write_speed", write_speed, &hfp_error);
148
if (write_speeds != NULL)
153
speedv = g_strsplit_set(write_speeds, ",", 0);
156
for (i = 0; speedv[i] != NULL; i++)
158
libhal_device_property_strlist_append(hfp_ctx, hfp_udi, "storage.cdrom.write_speeds", speedv[i], &hfp_error);
167
hfp_cdrom_free(cdrom);
171
main (int argc, char **argv)
176
int ret = 0; /* no media/filesystem */
177
gboolean has_children;
178
gboolean only_check_for_media;
181
if (! hfp_init(argc, argv))
184
device_file = getenv("HAL_PROP_BLOCK_DEVICE");
188
drive_type = getenv("HAL_PROP_STORAGE_DRIVE_TYPE");
192
parent = getenv("HAL_PROP_INFO_PARENT");
197
/* give a meaningful process title for ps(1) */
198
setproctitle("%s", device_file);
201
has_children = hfp_getenv_bool("HF_HAS_CHILDREN");
202
only_check_for_media = hfp_getenv_bool("HF_ONLY_CHECK_FOR_MEDIA");
204
is_cdrom = ! strcmp(drive_type, "cdrom");
206
if (! only_check_for_media && is_cdrom)
207
hf_probe_storage_get_cdrom_capabilities(device_file, parent);
213
cdrom = hfp_cdrom_new(device_file, parent);
217
if (hfp_cdrom_test_unit_ready(cdrom))
222
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.removable.media_available", TRUE, &hfp_error);
223
fd = open(device_file, O_RDONLY | O_NONBLOCK);
226
if (ioctl (fd, DIOCGMEDIASIZE, &size) == 0)
228
libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "storage.removable.media_size", size, &hfp_error);
232
ret = 2; /* has media */
236
libhal_device_set_property_bool(hfp_ctx, hfp_udi, "storage.removable.media_available", FALSE, &hfp_error);
239
hfp_cdrom_free(cdrom);
241
else if (! has_children) /* by definition, if it has children it has no fs */
243
struct volume_id *vid;
245
vid = volume_id_open_node(device_file);
249
if (volume_id_probe_all(vid, 0, 0) == 0 && vid->usage_id == VOLUME_ID_FILESYSTEM)
250
ret = 2; /* has a filesystem */
252
volume_id_close(vid);