Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIB-11: raw (low level) ISRC reads #6

Open
wants to merge 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
fbf133a
read ISRCs raw on Linux
JonnyJD Feb 4, 2013
5fbf4d4
remove TODO comment
JonnyJD Feb 7, 2013
c31d46b
Merge branch 'master' into isrc_raw
JonnyJD Mar 10, 2013
f8a82dd
move read_track_isrc to disc_scsi.c
JonnyJD Mar 10, 2013
a1d73d6
rename disc_scsi.c -> scsi.c
JonnyJD Mar 10, 2013
261aac8
move scsi function declarations in scsi header
JonnyJD Mar 10, 2013
d5a059b
add HACKING file
JonnyJD Mar 11, 2013
b1616d3
prepend mb_scsi to scsi functions
JonnyJD Mar 11, 2013
63c6dc8
include info about SCSI standards
JonnyJD Mar 11, 2013
de09b17
linux: stop the device after reading
JonnyJD Mar 11, 2013
1b859ac
Merge branch 'master' into isrc_raw
JonnyJD Apr 15, 2013
1c013f8
add scsi.h to distribution
JonnyJD Apr 15, 2013
20b7fcb
locate CRC in q-channel
JonnyJD Apr 26, 2013
81e062b
Merge branch 'master' into isrc_raw
JonnyJD May 11, 2013
4df857c
Merge branch 'master' into isrc_raw
JonnyJD Jun 20, 2013
0618e64
Merge branch 'master' into isrc_raw
JonnyJD Jun 29, 2013
a470a34
implement CRC check for raw ISRCs
JonnyJD Aug 2, 2013
5dd8ca2
Merge branch 'master' into crc
JonnyJD Aug 2, 2013
867889f
improve comments and names for raw isrcs
JonnyJD Aug 2, 2013
e77dd95
LIB-11: read ISRCs until CRC is valid
JonnyJD Aug 3, 2013
b8d3ed3
free raw disc read data
JonnyJD Aug 3, 2013
d163282
raw isrcs: don't return main channel data
JonnyJD Aug 3, 2013
fd63dc9
LIB-11: read every sector separately
JonnyJD Aug 3, 2013
02b733d
implement scsi_cmd for Windows
JonnyJD Aug 3, 2013
9e5e389
prepare autotools build with windows scsi_cmd
JonnyJD Aug 5, 2013
80d627e
work around windows dllexport issue
JonnyJD Aug 5, 2013
fb400d6
add ntddscsi.h for MSVC
JonnyJD Aug 5, 2013
ecd2d58
msvc: remove new warnings
JonnyJD Aug 5, 2013
f3ba048
Windows: fix scsi_cmd (return, GENERIC_WRITE)
JonnyJD Aug 8, 2013
724df7c
scsi_cmd: fix return code documentation
JonnyJD Aug 8, 2013
bc102db
add more debug output for w32 scsi_cmd
JonnyJD Aug 8, 2013
4a4ebab
check if there are ISRCs at all
JonnyJD Sep 19, 2013
73e1fa6
define SECTORS_ISRC and read 10 more
JonnyJD Sep 19, 2013
ee22ed7
check for data_len, rather than 96
JonnyJD Sep 19, 2013
aed1d9f
create mb_scsi_handle struct
JonnyJD Sep 19, 2013
e34d987
Merge branch 'scsi_handle' into scsi_windows
JonnyJD Sep 19, 2013
e2fc17f
Merge branch 'scsi_windows' into isrc_raw
JonnyJD Sep 19, 2013
2803ca0
return usable error codes from scsi_cmd
JonnyJD Sep 24, 2013
bdd74be
fix warnings on MinGW
JonnyJD Sep 24, 2013
046d1c0
fall back to normal when raw ISRC fails
JonnyJD Sep 24, 2013
cd58b5d
implement scsi feature detection
JonnyJD Sep 24, 2013
7031c1b
rename subchannel feature to isrc
JonnyJD Sep 24, 2013
55d274f
windows: restructure isrc section
JonnyJD Sep 25, 2013
3923e5a
add newline to stderr message
JonnyJD Oct 1, 2013
6533105
Merge branch 'master' into isrc_raw
JonnyJD Oct 3, 2013
90dbfe5
Merge branch 'master' into isrc_raw
JonnyJD Oct 3, 2013
477da28
Merge branch 'master' into isrc_raw
JonnyJD Oct 3, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ ENDIF()

# choose platform dependent source files
IF(libdiscid_OS MATCHES "win32")
SET(libdiscid_OSDEP_SRCS src/toc.c src/disc_win32.c)
SET(libdiscid_OSDEP_SRCS src/toc.c src/disc_win32.c src/scsi.c)
SET(libdiscid_RCS ${CMAKE_CURRENT_BINARY_DIR}/versioninfo.rc)
ELSEIF(libdiscid_OS MATCHES "linux")
SET(libdiscid_OSDEP_SRCS src/toc.c src/unix.c src/disc_linux.c src/scsi.c)
ELSEIF(libdiscid_OS MATCHES "darwin")
SET(libdiscid_OSDEP_SRCS src/toc.c src/unix.c src/disc_darwin.c)
FIND_LIBRARY(COREFOUNDATION_LIBRARY CoreFoundation)
Expand Down
20 changes: 20 additions & 0 deletions HACKING
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Libdiscid supports many platforms and the disc access code
for every platform is different.
Since you need physical machines with a CD drive to test a platform,
we do need your help!

The platform specific code is in the src/disc_*.c files.
These have to implement the *_unportable functions given
in include/discid/discid_private.h
The main function being mb_disc_read_unportable,
where the mb_disc_private struct is filled with data.
Parts of the data are optional features that don't need to be
implemented (ISRC, MCN). The TOC always has to be read.

You are free on how to implement the features defined in discid.h,
but it might help for many advanced features
when mb_scsi_cmd_unportable is implemented on the platform.
After this is done, the scsi functions available in src/scsi.h
can be used.
These functions are implemented as scsi commands,
which in itself should be platform independent.
6 changes: 3 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pc_DATA = libdiscid.pc
discid_incdir = $(includedir)/discid
discid_inc_HEADERS = include/discid/discid.h
noinst_HEADERS = include/discid/discid_private.h src/base64.h src/sha1.h
noinst_HEADERS += test/test.h src/unix.h src/ntddcdrm.h
noinst_HEADERS += test/test.h src/scsi.h src/unix.h src/ntddcdrm.h


if RUN_TESTS
Expand Down Expand Up @@ -91,7 +91,7 @@ if OS_GENERIC
libdiscid_la_SOURCES += src/disc_generic.c
endif
if OS_LINUX
libdiscid_la_SOURCES += src/toc.c src/unix.c src/disc_linux.c
libdiscid_la_SOURCES += src/toc.c src/scsi.c src/unix.c src/disc_linux.c
endif
if OS_NETBSD
libdiscid_la_SOURCES += src/toc.c src/unix.c src/disc_netbsd.c
Expand All @@ -103,7 +103,7 @@ if OS_SOLARIS
libdiscid_la_SOURCES += src/toc.c src/unix.c src/disc_solaris.c
endif
if OS_WIN32
libdiscid_la_SOURCES += src/toc.c src/disc_win32.c versioninfo.rc
libdiscid_la_SOURCES += src/toc.c src/scsi.c src/disc_win32.c versioninfo.rc
endif


Expand Down
8 changes: 8 additions & 0 deletions include/discid/discid_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,12 @@ LIBDISCID_INTERNAL int mb_disc_has_feature_unportable(enum discid_feature featur
*/
LIBDISCID_INTERNAL int mb_disc_load_toc(mb_disc_private *disc, mb_disc_toc *toc);

/*
* This is the code for discid_get_track_length(), implemented in disc.c.
* On Windows we can't use any LIBDISCID_API functions outside of disc.c,
* so we have this workaround for scsi.c usage
*/
LIBDISCID_INTERNAL int mb_disc_get_track_length(mb_disc_private *disc,
int track_num);

#endif /* MUSICBRAINZ_DISC_ID_PRIVATE_H */
14 changes: 11 additions & 3 deletions src/disc.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,8 @@ int discid_get_track_length(DiscId *d, int i) {

if (!disc->success || !TRACK_NUM_IS_VALID(disc, i))
return -1;
else if (i < disc->last_track_num)
return disc->track_offsets[i+1] - disc->track_offsets[i];
else
return disc->track_offsets[0] - disc->track_offsets[i];
return mb_disc_get_track_length(disc, i);
}

char *discid_get_mcn(DiscId *d) {
Expand Down Expand Up @@ -356,6 +354,16 @@ char *discid_get_version_string(void) {
*
****************************************************************************/

/* This is used in scsi.c, which can't use discid_get_track_length
* on Windows due to declspec(dllexport)
*/
int mb_disc_get_track_length(mb_disc_private *disc, int i) {
if (i < disc->last_track_num)
return disc->track_offsets[i+1] - disc->track_offsets[i];
else
return disc->track_offsets[0] - disc->track_offsets[i];
}

/*
* Create a DiscID based on the TOC data found in the DiscId object.
* The DiscID is placed in the provided string buffer.
Expand Down
89 changes: 47 additions & 42 deletions src/disc_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>


#include "discid/discid.h"
#include "discid/discid_private.h"
#include "scsi.h"
#include "unix.h"


#define MB_DEFAULT_DEVICE "/dev/cdrom"

/* timeout better shouldn't happen for scsi commands -> device is reset */
#define DEFAULT_TIMEOUT 30000 /* in ms */

Expand All @@ -58,6 +62,7 @@
#endif

static THREAD_LOCAL char default_device[MAX_DEV_LEN] = "";
static THREAD_LOCAL int feature_warning_printed = 0;


static int get_device(int number, char *device, int device_len) {
Expand Down Expand Up @@ -110,6 +115,7 @@ static int get_device(int number, char *device, int device_len) {
return return_value;
}


int mb_disc_unix_read_toc_header(int fd, mb_disc_toc *toc) {
struct cdrom_tochdr th;

Expand Down Expand Up @@ -171,10 +177,12 @@ void mb_disc_unix_read_mcn(int fd, mb_disc_private *disc) {
}

/* Send a scsi command and receive data. */
static int scsi_cmd(int fd, unsigned char *cmd, int cmd_len,
unsigned char *data, int data_len) {
mb_scsi_status mb_scsi_cmd_unportable(mb_scsi_handle handle,
unsigned char *cmd, int cmd_len,
unsigned char *data, int data_len) {
unsigned char sense_buffer[SG_MAX_SENSE]; /* for "error situations" */
sg_io_hdr_t io_hdr;
int return_value;

memset(&io_hdr, 0, sizeof io_hdr);

Expand All @@ -192,53 +200,50 @@ static int scsi_cmd(int fd, unsigned char *cmd, int cmd_len,
io_hdr.dxfer_len = data_len;
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;

if (ioctl(fd, SG_IO, &io_hdr) != 0) {
return errno;
return_value = ioctl(handle.fd, SG_IO, &io_hdr);

if (return_value == -1) {
/* failure */
fprintf(stderr, "ioctl error: %d\n", errno);
return IO_ERROR;
} else {
return io_hdr.status; /* 0 = success */
/* check for potenticall informative success codes
* 1 seems to be what is mostly returned */
if (return_value != 0) {
fprintf(stderr, "ioctl return value: %d\n",
return_value);
/* no error, but possibly informative */
}

/* check scsi status */
if (io_hdr.masked_status != GOOD) {
fprintf(stderr, "scsi status: %d\n", io_hdr.status);
return STATUS_ERROR;
} else if (data_len > 0 && io_hdr.resid == data_len) {
/* not receiving data, when requested */
fprintf(stderr, "data requested, but none returned\n");
return NO_DATA_RETURNED;
} else {
return SUCCESS;
}
}
}

void mb_disc_unix_read_isrc(int fd, mb_disc_private *disc, int track_num) {
int i;
unsigned char cmd[10];
unsigned char data[24];
char buffer[ISRC_STR_LENGTH+1];

memset(cmd, 0, sizeof cmd);
memset(data, 0, sizeof data);
memset(buffer, 0, sizeof buffer);

/* data read from the last appropriate sector encountered
* by a current or previous media access operation.
* The Logical Unit accesses the media when there is/was no access.
* TODO: force access at a specific block? -> no duplicate ISRCs?
*/
cmd[0] = 0x42; /* READ SUB-CHANNEL */
/* cmd[1] reserved / MSF bit (unused) */
cmd[2] = 1 << 6; /* 6th bit set (SUBQ) -> get sub-channel data */
cmd[3] = 0x03; /* get ISRC (ADR 3, Q sub-channel Mode-3) */
/* 4+5 reserved */
cmd[6] = track_num;
/* cmd[7] = upper byte of the transfer length */
cmd[8] = sizeof data; /* transfer length in bytes (4 header, 20 data)*/
/* cmd[9] = control byte */

if (scsi_cmd(fd, cmd, sizeof cmd, data, sizeof data) != 0) {
fprintf(stderr, "Warning: Cannot get ISRC code for track %d\n",
track_num);
return;
}

/* data[1:4] = sub-q channel data header (audio status, data length) */
if (data[8] & (1 << 7)) { /* TCVAL is set -> ISRCs valid */
for (i = 0; i < ISRC_STR_LENGTH; i++) {
buffer[i] = data[9 + i];
mb_scsi_handle handle;
mb_scsi_features features;
memset(&handle, 0, sizeof handle);
handle.fd = fd;
features = mb_scsi_get_features(handle);
if (features.raw_isrc) {
mb_scsi_read_track_isrc_raw(handle, disc, track_num);
} else {
if (!feature_warning_printed) {
fprintf(stderr, "Warning: raw ISRCs not available, using ISRCs given by subchannel read\n");
feature_warning_printed = 1;
}
buffer[ISRC_STR_LENGTH] = 0;
strncpy(disc->isrc[track_num], buffer, ISRC_STR_LENGTH);
mb_scsi_read_track_isrc(handle, disc, track_num);
}
/* data[21:23] = zero, AFRAME, reserved */
}

int mb_disc_has_feature_unportable(enum discid_feature feature) {
Expand Down
Loading