2 * cdrom_id - optical drive and media information prober
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <linux/cdrom.h>
40 #include "../../udev.h"
43 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
49 void log_message(int priority, const char *format, ...)
52 static int udev_log = -1;
57 value = getenv("UDEV_LOG");
59 udev_log = log_priority(value);
63 if (debug && udev_log < LOG_INFO)
67 if (priority > udev_log)
70 va_start(args, format);
72 fprintf(stderr, "[%d] ", (int) getpid());
73 vfprintf(stderr, format, args);
74 fprintf(stderr, "\n");
76 vsyslog(priority, format, args);
82 static unsigned int cd_cd_rom;
83 static unsigned int cd_cd_r;
84 static unsigned int cd_cd_rw;
85 static unsigned int cd_dvd_rom;
86 static unsigned int cd_dvd_r;
87 static unsigned int cd_dvd_rw;
88 static unsigned int cd_dvd_ram;
89 static unsigned int cd_dvd_plus_r;
90 static unsigned int cd_dvd_plus_rw;
91 static unsigned int cd_dvd_plus_r_dl;
92 static unsigned int cd_dvd_plus_rw_dl;
93 static unsigned int cd_bd;
94 static unsigned int cd_bd_r;
95 static unsigned int cd_bd_re;
96 static unsigned int cd_hddvd;
97 static unsigned int cd_hddvd_r;
98 static unsigned int cd_hddvd_rw;
99 static unsigned int cd_mo;
100 static unsigned int cd_mrw;
101 static unsigned int cd_mrw_w;
104 static unsigned int cd_media_cd_rom;
105 static unsigned int cd_media_cd_r;
106 static unsigned int cd_media_cd_rw;
107 static unsigned int cd_media_dvd_rom;
108 static unsigned int cd_media_dvd_r;
109 static unsigned int cd_media_dvd_rw;
110 static unsigned int cd_media_dvd_ram;
111 static unsigned int cd_media_dvd_plus_r;
112 static unsigned int cd_media_dvd_plus_rw;
113 static unsigned int cd_media_dvd_plus_r_dl;
114 static unsigned int cd_media_dvd_plus_rw_dl;
115 static unsigned int cd_media_bd;
116 static unsigned int cd_media_bd_r;
117 static unsigned int cd_media_bd_re;
118 static unsigned int cd_media_hddvd;
119 static unsigned int cd_media_hddvd_r;
120 static unsigned int cd_media_hddvd_rw;
121 static unsigned int cd_media_mo;
122 static unsigned int cd_media_mrw;
123 static unsigned int cd_media_mrw_w;
125 static const char *cd_media_state;
126 static unsigned int cd_media_has_audio;
127 static unsigned int cd_media_session_next;
128 static unsigned int cd_media_session_count;
129 static unsigned int cd_media_track_count;
130 static unsigned long long int cd_media_session_last_offset;
132 #define ERRCODE(s) ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
133 #define SK(errcode) (((errcode) >> 16) & 0xF)
134 #define ASC(errcode) (((errcode) >> 8) & 0xFF)
135 #define ASCQ(errcode) ((errcode) & 0xFF)
137 static void info_scsi_cmd_err(const char *cmd, int err)
140 info("%s failed", cmd);
143 info("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd, SK(err), ASC(err), ASCQ(err));
147 struct cdrom_generic_command cgc;
149 struct request_sense s;
152 struct sg_io_hdr sg_io;
155 static void scsi_cmd_set(struct scsi_cmd *cmd, size_t i, int arg)
158 memset(cmd, 0x00, sizeof(struct scsi_cmd));
160 cmd->cgc.sense = &cmd->_sense.s;
161 memset(&cmd->sg_io, 0, sizeof(cmd->sg_io));
162 cmd->sg_io.interface_id = 'S';
163 cmd->sg_io.mx_sb_len = sizeof(cmd->_sense);
164 cmd->sg_io.cmdp = cmd->cgc.cmd;
165 cmd->sg_io.sbp = cmd->_sense.u;
166 cmd->sg_io.flags = SG_FLAG_LUN_INHIBIT | SG_FLAG_DIRECT_IO;
168 cmd->sg_io.cmd_len = i + 1;
169 cmd->cgc.cmd[i] = arg;
172 #define CHECK_CONDITION 0x01
174 static int scsi_cmd_run(struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
178 cmd->sg_io.dxferp = buf;
179 cmd->sg_io.dxfer_len = bufsize;
180 cmd->sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
181 if (ioctl(fd, SG_IO, &cmd->sg_io))
184 if ((cmd->sg_io.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
187 if (cmd->sg_io.masked_status & CHECK_CONDITION) {
188 ret = ERRCODE(cmd->_sense.u);
196 static int cd_capability_compat(int fd)
200 capabilty = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
202 info("CDROM_GET_CAPABILITY failed");
206 if (capabilty & CDC_CD_R)
208 if (capabilty & CDC_CD_RW)
210 if (capabilty & CDC_DVD)
212 if (capabilty & CDC_DVD_R)
214 if (capabilty & CDC_DVD_RAM)
216 if (capabilty & CDC_MRW)
218 if (capabilty & CDC_MRW_W)
223 static int cd_inquiry(int fd) {
225 unsigned char inq[128];
228 scsi_cmd_set(&sc, 0, 0x12);
229 scsi_cmd_set(&sc, 4, 36);
230 scsi_cmd_set(&sc, 5, 0);
231 err = scsi_cmd_run(&sc, fd, inq, 36);
233 info_scsi_cmd_err("INQUIRY", err);
237 if ((inq[0] & 0x1F) != 5) {
238 info("not an MMC unit");
242 info("INQUIRY: [%.8s][%.16s][%.4s]", inq + 8, inq + 16, inq + 32);
246 static int cd_profiles(int fd)
249 unsigned char header[8];
250 unsigned char profiles[512];
251 unsigned int cur_profile;
256 scsi_cmd_set(&sc, 0, 0x46);
257 scsi_cmd_set(&sc, 1, 0);
258 scsi_cmd_set(&sc, 8, sizeof(header));
259 scsi_cmd_set(&sc, 9, 0);
260 err = scsi_cmd_run(&sc, fd, header, sizeof(header));
262 info_scsi_cmd_err("GET CONFIGURATION", err);
266 len = 4 + (header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3]);
267 info("GET CONFIGURATION: number of profiles %i", len);
268 if (len > sizeof(profiles)) {
269 info("invalid number of profiles");
273 scsi_cmd_set(&sc, 0, 0x46);
274 scsi_cmd_set(&sc, 1, 1);
275 scsi_cmd_set(&sc, 6, len >> 16);
276 scsi_cmd_set(&sc, 7, len >> 8);
277 scsi_cmd_set(&sc, 8, len);
278 scsi_cmd_set(&sc, 9, 0);
279 err = scsi_cmd_run(&sc, fd, profiles, len);
281 info_scsi_cmd_err("GET CONFIGURATION", err);
285 /* device profiles */
286 for (i = 12; i < profiles[11]; i += 4) {
287 unsigned int profile = (profiles[i] << 8 | profiles[i + 1]);
290 info("profile 0x%02x", profile);
315 cd_dvd_plus_rw_dl = 1;
318 cd_dvd_plus_r_dl = 1;
344 /* current media profile */
345 cur_profile = header[6] << 8 | header[7];
346 info("current profile 0x%02x", cur_profile);
347 if (cur_profile == 0) {
348 info("no current profile, assuming no media");
352 switch (cur_profile) {
368 cd_media_dvd_rom = 1;
374 cd_media_dvd_ram = 1;
381 cd_media_dvd_plus_r = 1;
384 cd_media_dvd_plus_rw = 1;
387 cd_media_dvd_plus_rw_dl = 1;
390 cd_media_dvd_plus_r_dl = 1;
406 cd_media_hddvd_r = 1;
409 cd_media_hddvd_rw = 1;
417 static int cd_media_info(int fd)
420 unsigned char header[32];
421 static const char *media_status[] = {
429 scsi_cmd_set(&sc, 0, 0x51);
430 scsi_cmd_set(&sc, 8, sizeof(header));
431 scsi_cmd_set(&sc, 9, 0);
432 err = scsi_cmd_run(&sc, fd, header, sizeof(header));
434 info_scsi_cmd_err("READ DISC INFORMATION", err);
438 info("disk type %02x", header[8]);
440 if ((header[2] & 3) < 4)
441 cd_media_state = media_status[header[2] & 3];
442 if ((header[2] & 3) != 2)
443 cd_media_session_next = header[10] << 8 | header[5];
444 cd_media_session_count = header[9] << 8 | header[4];
445 cd_media_track_count = header[11] << 8 | header[6];
450 static int cd_media_toc(int fd)
453 unsigned char header[12];
454 unsigned char toc[2048];
459 scsi_cmd_set(&sc, 0, 0x43);
460 scsi_cmd_set(&sc, 6, 1);
461 scsi_cmd_set(&sc, 8, sizeof(header));
462 scsi_cmd_set(&sc, 9, 0);
463 err = scsi_cmd_run(&sc, fd, header, sizeof(header));
465 info_scsi_cmd_err("READ TOC", err);
469 len = (header[0] << 8 | header[1]) + 2;
470 info("READ TOC: len: %d", len);
471 if (len > sizeof(toc))
474 /* check if we have a data track */
475 info("ctl %02x (0x04 is data/audio)", header[5]);
476 cd_media_has_audio = (header[5] & 0x04) == 0;
478 scsi_cmd_set(&sc, 0, 0x43);
479 scsi_cmd_set(&sc, 6, header[2]); /* First Track/Session Number */
480 scsi_cmd_set(&sc, 7, len >> 8);
481 scsi_cmd_set(&sc, 8, len);
482 scsi_cmd_set(&sc, 9, 0);
483 err = scsi_cmd_run(&sc, fd, toc, len);
485 info_scsi_cmd_err("READ TOC (tracks)", err);
489 for (p = toc+4, i = 4; i < len-8; i += 8, p += 8) {
492 block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
493 info("track %u starts at block %u", p[2], block);
496 scsi_cmd_set(&sc, 0, 0x43);
497 scsi_cmd_set(&sc, 2, 1); /* Session Info */
498 scsi_cmd_set(&sc, 8, 12);
499 scsi_cmd_set(&sc, 9, 0);
500 err = scsi_cmd_run(&sc, fd, header, sizeof(header));
502 info_scsi_cmd_err("READ TOC (multi session)", err);
505 len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
506 info("last track %u starts at block %u", header[4+2], len);
507 cd_media_session_last_offset = (unsigned long long int)len * 2048;
511 int main(int argc, char *argv[])
513 static const struct option options[] = {
514 { "export", 0, NULL, 'x' },
515 { "debug", 0, NULL, 'd' },
516 { "help", 0, NULL, 'h' },
519 const char *node = NULL;
524 logging_init("cdrom_id");
529 option = getopt_long(argc, argv, "dxh", options, NULL);
541 printf("Usage: cdrom_id [options] <device>\n"
542 " --export export key/value pairs\n"
554 fprintf(stderr, "no device\n");
559 fd = open(node, O_RDONLY | O_NONBLOCK);
561 info("unable to open '%s'", node);
565 info("probing: '%s'", node);
567 /* same data as original cdrom_id */
568 if (cd_capability_compat(fd) < 0) {
574 if (cd_inquiry(fd) < 0) {
579 /* read drive and possibly current profile */
580 if (cd_profiles(fd) < 0)
583 /* get session/track info */
584 if (cd_media_toc(fd) < 0)
587 /* get writable media state */
588 if (cd_media_info(fd) < 0)
592 printf("ID_CDROM=1\n");
594 printf("ID_CDROM_CD=1\n");
596 printf("ID_CDROM_CD_R=1\n");
598 printf("ID_CDROM_CD_RW=1\n");
600 printf("ID_CDROM_DVD=1\n");
602 printf("ID_CDROM_DVD_R=1\n");
604 printf("ID_CDROM_DVD_RW=1\n");
606 printf("ID_CDROM_DVD_RAM=1\n");
608 printf("ID_CDROM_DVD_PLUS_R=1\n");
610 printf("ID_CDROM_DVD_PLUS_RW=1\n");
611 if (cd_dvd_plus_r_dl)
612 printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
613 if (cd_dvd_plus_rw_dl)
614 printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
616 printf("ID_CDROM_BD=1\n");
618 printf("ID_CDROM_BD_R=1\n");
620 printf("ID_CDROM_BD_RE=1\n");
622 printf("ID_CDROM_HDDVD=1\n");
624 printf("ID_CDROM_HDDVD_R=1\n");
626 printf("ID_CDROM_HDDVD_RW=1\n");
628 printf("ID_CDROM_MO=1\n");
630 printf("ID_CDROM_MRW=1\n");
632 printf("ID_CDROM_MRW_W=1\n");
635 printf("ID_CDROM_MEDIA_MO=1\n");
637 printf("ID_CDROM_MEDIA_MRW=1\n");
639 printf("ID_CDROM_MEDIA_MRW_W=1\n");
641 printf("ID_CDROM_MEDIA_CD=1\n");
643 printf("ID_CDROM_MEDIA_CD_R=1\n");
645 printf("ID_CDROM_MEDIA_CD_RW=1\n");
646 if (cd_media_dvd_rom)
647 printf("ID_CDROM_MEDIA_DVD=1\n");
649 printf("ID_CDROM_MEDIA_DVD_R=1\n");
650 if (cd_media_dvd_ram)
651 printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
653 printf("ID_CDROM_MEDIA_DVD_RW=1\n");
654 if (cd_media_dvd_plus_r)
655 printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
656 if (cd_media_dvd_plus_rw)
657 printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
658 if (cd_media_dvd_plus_rw_dl)
659 printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
660 if (cd_media_dvd_plus_r_dl)
661 printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
663 printf("ID_CDROM_MEDIA_BD=1\n");
665 printf("ID_CDROM_MEDIA_BD_R=1\n");
667 printf("ID_CDROM_MEDIA_BD_RE=1\n");
669 printf("ID_CDROM_MEDIA_HDDVD=1\n");
670 if (cd_media_hddvd_r)
671 printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
672 if (cd_media_hddvd_rw)
673 printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
675 if (cd_media_state != NULL)
676 printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
677 if (cd_media_has_audio)
678 printf("ID_CDROM_MEDIA_HAS_AUDIO=1\n");
679 if (cd_media_session_next > 0)
680 printf("ID_CDROM_MEDIA_SESSION_NEXT=%d\n", cd_media_session_next);
681 if (cd_media_session_count > 0)
682 printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count);
683 if (cd_media_track_count > 0)
684 printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count);
685 if (cd_media_session_last_offset)
686 printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset);