Skip to content

Commit

Permalink
Merge pull request gavinbenda#3 from deenine/master
Browse files Browse the repository at this point in the history
PoC MD Deck & Bookshelf support
  • Loading branch information
gavinbenda authored Nov 16, 2020
2 parents fd8ed95 + bca95c1 commit 5d446e1
Showing 9 changed files with 190 additions and 86 deletions.
35 changes: 26 additions & 9 deletions libnetmd/common.c
Original file line number Diff line number Diff line change
@@ -28,18 +28,13 @@
#include "common.h"
#include "const.h"
#include "log.h"
#include "utils.h"

#define NETMD_POLL_TIMEOUT 1000 /* miliseconds */
#define NETMD_SEND_TIMEOUT 1000
#define NETMD_RECV_TIMEOUT 1000
#define NETMD_RECV_TRIES 30
#define NETMD_RECV_TRIES 39

#ifdef WIN32
#include <windows.h>
#define msleep(x) Sleep(x)
#else
#define msleep(x) usleep(1000*x)
#endif

/*
polls to see if minidisc wants to send data
@@ -51,7 +46,15 @@
*/
static int netmd_poll(libusb_device_handle *dev, unsigned char *buf, int tries)
{
/* original netmd poll sleep time was 1s, which lead to a print disc info
taking ~50s on a JE780. Dropping down to 5ms dropped print disc info
time to 0.54s, but was hitting timeout limits when sending tracks.
Unsure if just increasing limit is good practice, so set sleep time to
grow back to 1s if it retries more than 10x. Testing with 780 shows this
works for track transfers, typically hitting 15 loop iterations.
*/
int i;
int sleepytime = 5;

for (i = 0; i < tries; i++) {
/* send a poll message */
@@ -69,7 +72,11 @@ static int netmd_poll(libusb_device_handle *dev, unsigned char *buf, int tries)
}

if (i > 0) {
msleep(1000);
msleep(sleepytime);
sleepytime = 100;
}
if (i > 10) {
sleepytime = 1000;
}
}

@@ -80,8 +87,18 @@ static int netmd_poll(libusb_device_handle *dev, unsigned char *buf, int tries)
int netmd_exch_message(netmd_dev_handle *devh, unsigned char *cmd,
const size_t cmdlen, unsigned char *rsp)
{
int len;
netmd_send_message(devh, cmd, cmdlen);
return netmd_recv_message(devh, rsp);
len = netmd_recv_message(devh, rsp);
netmd_log(NETMD_LOG_DEBUG, "Response code:\n");
netmd_log_hex(NETMD_LOG_DEBUG, &rsp[0], 1);
if (rsp[0] == NETMD_STATUS_INTERIM) {
netmd_log(NETMD_LOG_DEBUG, "Re-reading:\n");
len = netmd_recv_message(devh, rsp);
netmd_log(NETMD_LOG_DEBUG, "Response code:\n");
netmd_log_hex(NETMD_LOG_DEBUG, &rsp[0], 1);
}
return len;
}


41 changes: 37 additions & 4 deletions libnetmd/libnetmd.c
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@
/*! list of known codecs (mapped to protocol ID) that can be used in NetMD devices */
/*! Bertrik: the original interpretation of these numbers as codecs appears incorrect.
These values look like track protection values instead */
struct netmd_pair const trprot_settings[] =
struct netmd_pair const trprot_settings[] =
{
{0x00, "UnPROT"},
{0x03, "TrPROT"},
@@ -42,7 +42,7 @@ struct netmd_pair const trprot_settings[] =
/*! list of known bitrates (mapped to protocol ID) that can be used in NetMD devices */
struct netmd_pair const bitrates[] =
{
{NETMD_ENCODING_SP, "Stereo"},
{NETMD_ENCODING_SP, "SP"},
{NETMD_ENCODING_LP2, "LP2"},
{NETMD_ENCODING_LP4, "LP4"},
{0, 0} /* terminating pair */
@@ -148,6 +148,7 @@ static int request_disc_title(netmd_dev_handle* dev, char* buffer, size_t size)
int netmd_request_track_time(netmd_dev_handle* dev, const uint16_t track, struct netmd_track* buffer)
{
int ret = 0;
unsigned char hs[] = {0x00, 0x18, 0x08, 0x10, 0x10, 0x01, 0x01, 0x00};
unsigned char request[] = {0x00, 0x18, 0x06, 0x02, 0x20, 0x10,
0x01, 0x00, 0x01, 0x30, 0x00, 0x01,
0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
@@ -157,6 +158,7 @@ int netmd_request_track_time(netmd_dev_handle* dev, const uint16_t track, struct

buf = request + 7;
netmd_copy_word_to_buffer(&buf, track, 0);
netmd_exch_message(dev, hs, 8, time_request);
ret = netmd_exch_message(dev, request, 0x13, time_request);
if(ret < 0)
{
@@ -176,6 +178,10 @@ int netmd_set_title(netmd_dev_handle* dev, const uint16_t track, const char* con
{
int ret = 1;
unsigned char *title_request = NULL;
/* handshakes for 780/980/etc */
unsigned char hs2[] = {0x00, 0x18, 0x08, 0x10, 0x18, 0x02, 0x00, 0x00};
unsigned char hs3[] = {0x00, 0x18, 0x08, 0x10, 0x18, 0x02, 0x03, 0x00};

unsigned char title_header[] = {0x00, 0x18, 0x07, 0x02, 0x20, 0x18,
0x02, 0x00, 0x00, 0x30, 0x00, 0x0a,
0x00, 0x50, 0x00, 0x00, 0x0a, 0x00,
@@ -200,6 +206,11 @@ int netmd_set_title(netmd_dev_handle* dev, const uint16_t track, const char* con
title_request[16] = size & 0xff;
title_request[20] = oldsize & 0xff;


/* send handshakes */
netmd_exch_message(dev, hs2, 8, reply);
netmd_exch_message(dev, hs3, 8, reply);

ret = netmd_exch_message(dev, title_request, 0x15 + size, reply);
free(title_request);

@@ -214,6 +225,7 @@ int netmd_set_title(netmd_dev_handle* dev, const uint16_t track, const char* con
int netmd_move_track(netmd_dev_handle* dev, const uint16_t start, const uint16_t finish)
{
int ret = 0;
unsigned char hs[] = {0x00, 0x18, 0x08, 0x10, 0x10, 0x01, 0x00, 0x00};
unsigned char request[] = {0x00, 0x18, 0x43, 0xff, 0x00, 0x00,
0x20, 0x10, 0x01, 0x00, 0x04, 0x20,
0x10, 0x01, 0x00, 0x03};
@@ -226,6 +238,8 @@ int netmd_move_track(netmd_dev_handle* dev, const uint16_t start, const uint16_t
buf = request + 14;
netmd_copy_word_to_buffer(&buf, finish, 0);

netmd_exch_message(dev, hs, 8, reply);
netmd_exch_message(dev, request, 16, reply);
ret = netmd_exch_message(dev, request, 16, reply);

if(ret < 0)
@@ -481,6 +495,10 @@ int netmd_set_disc_title(netmd_dev_handle* dev, char* title, size_t title_length
unsigned char write_req[] = {0x00, 0x18, 0x07, 0x02, 0x20, 0x18,
0x01, 0x00, 0x00, 0x30, 0x00, 0x0a,
0x00, 0x50, 0x00, 0x00};
unsigned char hs1[] = {0x00, 0x18, 0x08, 0x10, 0x18, 0x01, 0x01, 0x00};
unsigned char hs2[] = {0x00, 0x18, 0x08, 0x10, 0x18, 0x01, 0x00, 0x00};
unsigned char hs3[] = {0x00, 0x18, 0x08, 0x10, 0x18, 0x01, 0x03, 0x00};
unsigned char hs4[] = {0x00, 0x18, 0x08, 0x10, 0x18, 0x01, 0x00, 0x00};
unsigned char reply[256];
int result;
int oldsize;
@@ -500,7 +518,13 @@ int netmd_set_disc_title(netmd_dev_handle* dev, char* title, size_t title_length
p = request + 21;
memcpy(p, title, title_length);

/* send handshakes */
netmd_exch_message(dev, hs1, sizeof(hs1), reply);
netmd_exch_message(dev, hs2, sizeof(hs2), reply);
netmd_exch_message(dev, hs3, sizeof(hs3), reply);
result = netmd_exch_message(dev, request, 0x15 + title_length, reply);
/* send handshake to write */
netmd_exch_message(dev, hs4, sizeof(hs4), reply);
return result;
}

@@ -867,17 +891,22 @@ char* netmd_generate_disc_header(minidisc* md, char* header, size_t header_lengt

int netmd_write_disc_header(netmd_dev_handle* devh, minidisc* md)
{

size_t header_size;
size_t request_size;
char* header = 0;
unsigned char* request = 0;
unsigned char hs[] = {0x00, 0x18, 0x08, 0x10, 0x18, 0x01, 0x03, 0x00};
unsigned char hs2[] = {0x00, 0x18, 0x08, 0x10, 0x18, 0x01, 0x00, 0x00};
unsigned char write_req[] = {0x00, 0x18, 0x07, 0x02, 0x20, 0x18,
0x01, 0x00, 0x00, 0x30, 0x00, 0x0a,
0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00};
unsigned char reply[255];
int ret;

printf("sending write disc header handshake");
netmd_exch_message(devh, hs, 8, reply);
printf("...OK\n");
header_size = netmd_calculate_disc_header_length(md);
header = malloc(sizeof(char) * header_size);
memset(header, 0, header_size);
@@ -1165,7 +1194,11 @@ int netmd_acquire_dev(netmd_dev_handle* dev)
unsigned char reply[255];

ret = netmd_exch_message(dev, request, sizeof(request), reply);
return ret;
if (reply[0] == NETMD_STATUS_ACCEPTED){
return NETMD_NO_ERROR;
} else {
return NETMD_COMMAND_FAILED_UNKNOWN_ERROR;
}
}

int netmd_release_dev(netmd_dev_handle* dev)
96 changes: 53 additions & 43 deletions libnetmd/netmd_dev.c
Original file line number Diff line number Diff line change
@@ -30,51 +30,60 @@

static libusb_context *ctx = NULL;

/*! list of known vendor/prod id's for NetMD devices */
/*! list of known vendor/prod id's for NetMD devices
patch credit to Thomas Arp, 2011:
https://lists.fu-berlin.de/pipermail/linux-minidisc/2011-September/msg00027.html
*/
static struct netmd_devices const known_devices[] =
{
{0x54c, 0x34}, /* Sony PCLK-XX */
{0x54c, 0x36}, /* Sony (unknown model) */
{0x54c, 0x75}, /* Sony MZ-N1 */
{0x54c, 0x7c}, /* Sony (unknown model) */
{0x54c, 0x80}, /* Sony LAM-1 */
{0x54c, 0x81}, /* Sony MDS-JE780/JB980 */
{0x54c, 0x84}, /* Sony MZ-N505 */
{0x54c, 0x85}, /* Sony MZ-S1 */
{0x54c, 0x86}, /* Sony MZ-N707 */
{0x54c, 0x8e}, /* Sony CMT-C7NT */
{0x54c, 0x97}, /* Sony PCGA-MDN1 */
{0x54c, 0xad}, /* Sony CMT-L7HD */
{0x54c, 0xc6}, /* Sony MZ-N10 */
{0x54c, 0xc7}, /* Sony MZ-N910 */
{0x54c, 0xc8}, /* Sony MZ-N710/NE810/NF810 */
{0x54c, 0xc9}, /* Sony MZ-N510/NF610 */
{0x54c, 0xca}, /* Sony MZ-NE410/DN430/NF520 */
{0x54c, 0xeb}, /* Sony MZ-NE810/NE910 */
{0x54c, 0xe7}, /* Sony CMT-M333NT/M373NT */
{0x54c, 0x101}, /* Sony LAM-10 */
{0x54c, 0x113}, /* Aiwa AM-NX1 */
{0x54c, 0x14c}, /* Aiwa AM-NX9 */
{0x54c, 0x17e}, /* Sony MZ-NH1 */
{0x54c, 0x180}, /* Sony MZ-NH3D */
{0x54c, 0x182}, /* Sony MZ-NH900 */
{0x54c, 0x184}, /* Sony MZ-NH700/800 */
{0x54c, 0x186}, /* Sony MZ-NH600/600D */
{0x54c, 0x188}, /* Sony MZ-N920 */
{0x54c, 0x18a}, /* Sony LAM-3 */
{0x54c, 0x1e9}, /* Sony MZ-DH10P */
{0x54c, 0x219}, /* Sony MZ-RH10 */
{0x54c, 0x21b}, /* Sony MZ-RH910 */
{0x54c, 0x21d}, /* Sony CMT-AH10 */
{0x54c, 0x22c}, /* Sony CMT-AH10 */
{0x54c, 0x23c}, /* Sony DS-HMD1 */
{0x54c, 0x286}, /* Sony MZ-RH1 */

{0x4dd, 0x7202}, /* Sharp IM-MT880H/MT899H */
{0x4dd, 0x9013}, /* Sharp IM-DR400/DR410 */
{0x4dd, 0x9014}, /* Sharp IM-DR80/DR420/DR580 and Kenwood DMC-S9NET */

{0, 0} /* terminating pair */

{0x54c, 0x34, "Sony PCLK-XX"},
{0x54c, 0x36, "Sony (unknown model)"},
{0x54c, 0x6F, "Sony NW-E7"},
{0x54c, 0x75, "Sony MZ-N1"},
{0x54c, 0x7c, "Sony (unknown model)"},
{0x54c, 0x80, "Sony LAM-1"},
{0x54c, 0x81, "Sony MDS-JE780/JB980"},
{0x54c, 0x84, "Sony MZ-N505"},
{0x54c, 0x85, "Sony MZ-S1"},
{0x54c, 0x86, "Sony MZ-N707"},
{0x54c, 0x8e, "Sony CMT-C7NT"},
{0x54c, 0x97, "Sony PCGA-MDN1"},
{0x54c, 0xad, "Sony CMT-L7HD"},
{0x54c, 0xc6, "Sony MZ-N10"},
{0x54c, 0xc7, "Sony MZ-N910"},
{0x54c, 0xc8, "Sony MZ-N710/NE810/NF810"},
{0x54c, 0xc9, "Sony MZ-N510/NF610"},
{0x54c, 0xca, "Sony MZ-NE410/DN430/NF520"},
{0x54c, 0xeb, "Sony MZ-NE810/NE910"},
{0x54c, 0xe7, "Sony CMT-M333NT/M373NT"},
{0x54c, 0x101, "Sony LAM-10"},
{0x54c, 0x113, "Aiwa AM-NX1"},
{0x54c, 0x119, "Sony CMT-SE9"},
{0x54c, 0x13f, "Sony MDS-S500"},
{0x54c, 0x14c, "Aiwa AM-NX9"},
{0x54c, 0x17e, "Sony MZ-NH1"},
{0x54c, 0x180, "Sony MZ-NH3D"},
{0x54c, 0x182, "Sony MZ-NH900"},
{0x54c, 0x184, "Sony MZ-NH700/800"},
{0x54c, 0x186, "Sony MZ-NH600/600D"},
{0x54c, 0x188, "Sony MZ-N920"},
{0x54c, 0x18a, "Sony LAM-3"},
{0x54c, 0x1e9, "Sony MZ-DH10P"},
{0x54c, 0x219, "Sony MZ-RH10"},
{0x54c, 0x21b, "Sony MZ-RH910"},
{0x54c, 0x21d, "Sony CMT-AH10"},
{0x54c, 0x22c, "Sony CMT-AH10"},
{0x54c, 0x23c, "Sony DS-HMD1"},
{0x54c, 0x286, "Sony MZ-RH1"},

{0x4dd, 0x7202, "Sharp IM-MT880H/MT899H"},
{0x4dd, 0x9013, "Sharp IM-DR400/DR410"},
{0x4dd, 0x9014, "Sharp IM-DR80/DR420/DR580 or Kenwood DMC-S9NET"},

{0x04, 0x23b3, "Panasonic SJ-MR250"},

{0, 0, NULL} /* terminating pair */
};


@@ -109,6 +118,7 @@ netmd_error netmd_init(netmd_device **device_list, libusb_context *hctx)
new_device = malloc(sizeof(netmd_device));
new_device->usb_dev = list[i];
new_device->link = *device_list;
new_device->model = known_devices[count].model;
*device_list = new_device;
}
}
2 changes: 2 additions & 0 deletions libnetmd/netmd_dev.h
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
typedef struct netmd_device {
struct netmd_device *link;
char name[32];
char *model;
struct libusb_device *usb_dev;
} netmd_device;

@@ -16,6 +17,7 @@ typedef struct netmd_device {
struct netmd_devices {
int idVendor;
int idProduct;
char *model;
};

/**
2 changes: 2 additions & 0 deletions libnetmd/playercontrol.c
Original file line number Diff line number Diff line change
@@ -209,12 +209,14 @@ netmd_error netmd_get_position(netmd_dev_handle* dev, netmd_time* time)

netmd_error netmd_get_disc_capacity(netmd_dev_handle* dev, netmd_disc_capacity* capacity)
{
unsigned char hs[] = {0x00, 0x18, 0x08, 0x10, 0x10, 0x00, 0x01, 0x00};
unsigned char request[] = {0x00, 0x18, 0x06, 0x02, 0x10, 0x10,
0x00, 0x30, 0x80, 0x03, 0x00, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char buf[255];

/* TODO: error checking */
netmd_exch_message(dev, hs, sizeof(hs), buf);
netmd_exch_message(dev, request, sizeof(request), buf);
netmd_parse_time(buf + 27, &capacity->recorded);
netmd_parse_time(buf + 34, &capacity->total);
Loading

0 comments on commit 5d446e1

Please sign in to comment.