FU Logo
  • Startseite
  • Kontakt
  • Impressum
  • Home
  • Listenauswahl
  • Anleitungen

[linux-minidisc] [PATCH] Port libnetmd from the old libusb-0.1 to the current libusb-1.0.

<-- thread -->
<-- date -->
  • From: Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
  • To: linux-minidisc@lists.fu-berlin.de
  • Date: Thu, 8 Sep 2011 23:28:34 +0200
  • Subject: [linux-minidisc] [PATCH] Port libnetmd from the old libusb-0.1 to the current libusb-1.0.

---
 libnetmd/common.c     |   33 ++++++++++++-----------
 libnetmd/common.h     |    4 +-
 libnetmd/libnetmd.c   |   23 +++++++++-------
 libnetmd/libnetmd.h   |    2 +-
 libnetmd/libnetmd.pro |    2 +-
 libnetmd/netmd_dev.c  |   70 +++++++++++++++++++++++-------------------------
 libnetmd/netmd_dev.h  |    4 +-
 libnetmd/secure.c     |   10 ++++---
 8 files changed, 76 insertions(+), 72 deletions(-)

diff --git a/libnetmd/common.c b/libnetmd/common.c
index a244889..1f7b660 100644
--- a/libnetmd/common.c
+++ b/libnetmd/common.c
@@ -23,6 +23,7 @@
  */
 
 #include <string.h>
+#include <unistd.h>
 
 #include "common.h"
 #include "const.h"
@@ -41,7 +42,7 @@
   @param tries maximum attempts to poll the minidisc
   @return if error <0, else number of bytes that md wants to send
 */
-static int netmd_poll(usb_dev_handle *dev, char *buf, int tries)
+static int netmd_poll(libusb_device_handle *dev, unsigned char *buf, int tries)
 {
     int i;
 
@@ -49,10 +50,10 @@ static int netmd_poll(usb_dev_handle *dev, char *buf, int tries)
         /* send a poll message */
         memset(buf, 0, 4);
 
-        if (usb_control_msg(dev, USB_ENDPOINT_IN | USB_TYPE_VENDOR |
-                            USB_RECIP_INTERFACE, 0x01, 0, 0, buf, 4,
+        if (libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR |
+                            LIBUSB_RECIPIENT_INTERFACE, 0x01, 0, 0, buf, 4,
                             NETMD_POLL_TIMEOUT) < 0) {
-            netmd_log(NETMD_LOG_ERROR, "netmd_poll: usb_control_msg failed\n");
+            netmd_log(NETMD_LOG_ERROR, "netmd_poll: libusb_control_transfer failed\n");
             return NETMDERR_USB;
         }
 
@@ -80,11 +81,11 @@ int netmd_exch_message(netmd_dev_handle *devh, unsigned char *cmd,
 int netmd_send_message(netmd_dev_handle *devh, unsigned char *cmd,
                        const size_t cmdlen)
 {
-    char pollbuf[4];
+    unsigned char pollbuf[4];
     int	len;
-    usb_dev_handle *dev;
+    libusb_device_handle *dev;
 
-    dev = (usb_dev_handle *)devh;
+    dev = (libusb_device_handle *)devh;
 
     /* poll to see if we can send data */
     len = netmd_poll(dev, pollbuf, 1);
@@ -96,10 +97,10 @@ int netmd_send_message(netmd_dev_handle *devh, unsigned char *cmd,
     /* send data */
     netmd_log(NETMD_LOG_DEBUG, "Command:\n");
     netmd_log_hex(NETMD_LOG_DEBUG, cmd, cmdlen);
-    if (usb_control_msg(dev, USB_ENDPOINT_OUT | USB_TYPE_VENDOR |
-                        USB_RECIP_INTERFACE, 0x80, 0, 0, (char*)cmd, (int)cmdlen,
+    if (libusb_control_transfer(dev, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR |
+                        LIBUSB_RECIPIENT_INTERFACE, 0x80, 0, 0, cmd, (int)cmdlen,
                         NETMD_SEND_TIMEOUT) < 0) {
-        netmd_log(NETMD_LOG_ERROR, "netmd_exch_message: usb_control_msg failed\n");
+        netmd_log(NETMD_LOG_ERROR, "netmd_exch_message: libusb_control_transfer failed\n");
         return NETMDERR_USB;
     }
 
@@ -109,10 +110,10 @@ int netmd_send_message(netmd_dev_handle *devh, unsigned char *cmd,
 int netmd_recv_message(netmd_dev_handle *devh, unsigned char* rsp)
 {
     int len;
-    char pollbuf[4];
-    usb_dev_handle *dev;
+    unsigned char pollbuf[4];
+    libusb_device_handle *dev;
 
-    dev = (usb_dev_handle *)devh;
+    dev = (libusb_device_handle *)devh;
 
     /* poll for data that minidisc wants to send */
     len = netmd_poll(dev, pollbuf, NETMD_RECV_TRIES);
@@ -122,10 +123,10 @@ int netmd_recv_message(netmd_dev_handle *devh, unsigned char* rsp)
     }
 
     /* receive data */
-    if (usb_control_msg(dev, USB_ENDPOINT_IN | USB_TYPE_VENDOR |
-                        USB_RECIP_INTERFACE, pollbuf[1], 0, 0, (char*)rsp, len,
+    if (libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR |
+                        LIBUSB_RECIPIENT_INTERFACE, pollbuf[1], 0, 0, rsp, len,
                         NETMD_RECV_TIMEOUT) < 0) {
-        netmd_log(NETMD_LOG_ERROR, "netmd_exch_message: usb_control_msg failed\n");
+        netmd_log(NETMD_LOG_ERROR, "netmd_exch_message: libusb_control_transfer failed\n");
         return NETMDERR_USB;
     }
 
diff --git a/libnetmd/common.h b/libnetmd/common.h
index f3a51bf..8dcff6b 100644
--- a/libnetmd/common.h
+++ b/libnetmd/common.h
@@ -1,13 +1,13 @@
 #ifndef LIBNETMD_COMMON_H
 #define LIBNETMD_COMMON_H
 
-#include <usb.h>
+#include <libusb-1.0/libusb.h>
 
 /**
    Typedef that nearly all netmd_* functions use to identify the USB connection
    with the minidisc player.
 */
-typedef usb_dev_handle*	netmd_dev_handle;
+typedef libusb_device_handle *netmd_dev_handle;
 
 /**
   Function to exchange command/response buffer with minidisc player.
diff --git a/libnetmd/libnetmd.c b/libnetmd/libnetmd.c
index 432c5a3..0291cbe 100644
--- a/libnetmd/libnetmd.c
+++ b/libnetmd/libnetmd.c
@@ -22,6 +22,8 @@
  *
  */
 
+#include <unistd.h>
+
 #include "libnetmd.h"
 #include "utils.h"
 
@@ -58,12 +60,12 @@ struct netmd_pair const* find_pair(int hex, struct netmd_pair const* array)
     return &unknown_pair;
 }
 
-static void waitforsync(usb_dev_handle* dev)
+static void waitforsync(libusb_device_handle* dev)
 {
-    char syncmsg[4];
+    unsigned char syncmsg[4];
     fprintf(stderr,"Waiting for Sync: \n");
     do {
-        usb_control_msg(dev, 0xc1, 0x01, 0, 0, syncmsg, 0x04, 5000);
+        libusb_control_transfer(dev, 0xc1, 0x01, 0, 0, syncmsg, 0x04, 5000);
     } while  (memcmp(syncmsg,"\0\0\0\0",4)!=0);
 
 }
@@ -881,6 +883,7 @@ int netmd_write_disc_header(netmd_dev_handle* devh, minidisc* md)
 int netmd_write_track(netmd_dev_handle* devh, char* szFile)
 {
     int ret = 0;
+    int transferred = 0;
     int fd = open(szFile, O_RDONLY); /* File descriptor to omg file */
     unsigned char *data = malloc(4096); /* Buffer for reading the omg file */
     unsigned char *p = NULL; /* Pointer to index into data */
@@ -925,9 +928,9 @@ int netmd_write_track(netmd_dev_handle* devh, char* szFile)
     size_t data_size_i; /* the size of the data part, later it will be used to point out the last byte in file */
     unsigned int size;
     unsigned char* buf=NULL; /* A buffer for recieving file info */
-    usb_dev_handle	*dev;
+    libusb_device_handle *dev;
 
-    dev = (usb_dev_handle *)devh;
+    dev = (libusb_device_handle *)devh;
 
     if(fd < 0)
         return fd;
@@ -1034,7 +1037,7 @@ int netmd_write_track(netmd_dev_handle* devh, char* szFile)
 
         netmd_log(NETMD_LOG_DEBUG, "Sending %d bytes to md\n", bytes_to_send);
         netmd_log_hex(NETMD_LOG_DEBUG, data, bytes_to_send);
-        ret = usb_bulk_write(dev,0x02, (char*)data, (int)bytes_to_send, 5000);
+        ret = libusb_bulk_transfer(dev, 0x02, data, (int)bytes_to_send, &transferred, 5000);
     } /* End while */
 
     if (ret<0) {
@@ -1046,7 +1049,7 @@ int netmd_write_track(netmd_dev_handle* devh, char* szFile)
     /******** End transfer wait for unit ready ********/
     fprintf(stderr,"Waiting for Done:\n");
     do {
-        usb_control_msg(dev, 0xc1, 0x01, 0, 0, (char*)size_request, 0x04, 5000);
+        libusb_control_transfer(dev, 0xc1, 0x01, 0, 0, size_request, 0x04, 5000);
     } while  (memcmp(size_request,"\0\0\0\0",4)==0);
 
     netmd_log(NETMD_LOG_DEBUG, "Recieving response: \n");
@@ -1057,7 +1060,7 @@ int netmd_write_track(netmd_dev_handle* devh, char* szFile)
         return -1;
     }
     buf = malloc(size);
-    usb_control_msg(dev, 0xc1, 0x81, 0, 0, (char*)buf, (int)size, 500);
+    libusb_control_transfer(dev, 0xc1, 0x81, 0, 0, buf, (int)size, 500);
     netmd_log_hex(NETMD_LOG_DEBUG, buf, size);
     free(buf);
 
@@ -1071,11 +1074,11 @@ int netmd_write_track(netmd_dev_handle* devh, char* szFile)
 
 
     /********* End TOC Edit **********/
-    ret = usb_control_msg(dev, 0x41, 0x80, 0, 0, (char*)fintoc, 0x19, 800);
+    ret = libusb_control_transfer(dev, 0x41, 0x80, 0, 0, fintoc, 0x19, 800);
 
     fprintf(stderr,"Waiting for Done: \n");
     do {
-        usb_control_msg(dev, 0xc1, 0x01, 0, 0, (char*)size_request, 0x04, 5000);
+        libusb_control_transfer(dev, 0xc1, 0x01, 0, 0, size_request, 0x04, 5000);
     } while  (memcmp(size_request,"\0\0\0\0",4)==0);
 
     return ret;
diff --git a/libnetmd/libnetmd.h b/libnetmd/libnetmd.h
index cc33d78..bbd001c 100644
--- a/libnetmd/libnetmd.h
+++ b/libnetmd/libnetmd.h
@@ -32,7 +32,7 @@
 #include <errno.h>
 #include <stdint.h>
 
-#include <usb.h>
+#include <libusb-1.0/libusb.h>
 
 #include "const.h"
 #include "error.h"
diff --git a/libnetmd/libnetmd.pro b/libnetmd/libnetmd.pro
index ca93ac9..5c4f6ec 100644
--- a/libnetmd/libnetmd.pro
+++ b/libnetmd/libnetmd.pro
@@ -7,4 +7,4 @@ DEFINES += G_DISABLE_DEPRECATED=1
 PKGCONFIG += 
 HEADERS += common.h const.h error.h libnetmd.h log.h netmd_dev.h playercontrol.h secure.h trackinformation.h utils.h
 SOURCES += common.c error.c libnetmd.c log.c netmd_dev.c playercontrol.c secure.c trackinformation.c utils.c
-LIBS    += -lusb -lgcrypt
+LIBS    += -lusb-1.0 -lgcrypt
diff --git a/libnetmd/netmd_dev.c b/libnetmd/netmd_dev.c
index 0c0e8b0..9e186b9 100644
--- a/libnetmd/netmd_dev.c
+++ b/libnetmd/netmd_dev.c
@@ -21,11 +21,14 @@
 #include <string.h>
 #include <errno.h>
 #include <assert.h>
+#include <stdlib.h>
 
 #include "netmd_dev.h"
 #include "log.h"
 #include "const.h"
 
+static libusb_context *ctx = NULL;
+
 /*! list of known vendor/prod id's for NetMD devices */
 static struct netmd_devices const known_devices[] =
 {
@@ -76,33 +79,28 @@ static struct netmd_devices const known_devices[] =
 
 netmd_error netmd_init(netmd_device **device_list)
 {
-    struct usb_bus *bus;
-    struct usb_device *dev;
     int count = 0;
+    ssize_t usb_device_count;
+    ssize_t i = 0;
     netmd_device *new_device;
+    libusb_device **list;
+    struct libusb_device_descriptor desc;
 
-    usb_init();
-
-    usb_find_busses();
-    usb_find_devices();
+    libusb_init(&ctx);
 
     *device_list = NULL;
-    for(bus = usb_get_busses(); bus; bus = bus->next)
-    {
-        for(dev = bus->devices; dev; dev = dev->next)
-        {
-            for(count = 0; (known_devices[count].idVendor != 0); count++)
-            {
-                if(dev->descriptor.idVendor == known_devices[count].idVendor
-                   && dev->descriptor.idProduct == known_devices[count].idProduct)
-                {
-                    new_device = malloc(sizeof(netmd_device));
-                    new_device->usb_dev = dev;
-                    new_device->link = *device_list;
-                    *device_list = new_device;
-                }
-            }
-        }
+
+    usb_device_count = libusb_get_device_list(NULL, &list);
+
+    for (i = 0; i < usb_device_count; i++) {
+      libusb_get_device_descriptor(list[i], &desc);
+      if(desc.idVendor == known_devices[count].idVendor &&
+	 desc.idProduct == known_devices[count].idProduct) {
+	new_device = malloc(sizeof(netmd_device));
+	new_device->usb_dev = list[i];
+	new_device->link = *device_list;
+	*device_list = new_device;
+      }
     }
 
     return NETMD_NO_ERROR;
@@ -112,10 +110,10 @@ netmd_error netmd_init(netmd_device **device_list)
 netmd_error netmd_open(netmd_device *dev, netmd_dev_handle **dev_handle)
 {
     int result;
-    usb_dev_handle *dh;
+    libusb_device_handle **dh = NULL;
 
-    dh = usb_open(dev->usb_dev);
-    result = usb_claim_interface(dh, 0);
+    libusb_open(dev->usb_dev, dh);
+    result = libusb_claim_interface(*dh, 0);
 
     if (result == 0) {
         *dev_handle = (netmd_dev_handle*)dh;
@@ -127,13 +125,13 @@ netmd_error netmd_open(netmd_device *dev, netmd_dev_handle **dev_handle)
     }
 }
 
-netmd_error netmd_get_devname(netmd_dev_handle* devh, char* buf, size_t buffsize)
+netmd_error netmd_get_devname(netmd_dev_handle* devh, unsigned char* buf, size_t buffsize)
 {
     int result;
 
-    result = usb_get_string_simple((usb_dev_handle *)devh, 2, buf, buffsize);
+    result = libusb_get_string_descriptor_ascii((libusb_device_handle *)devh, 2, buf, buffsize);
     if (result < 0) {
-        netmd_log(NETMD_LOG_ERROR, "usb_get_string_simple failed, %s (%d)\n", strerror(errno), errno);
+        netmd_log(NETMD_LOG_ERROR, "libusb_get_string_descriptor_asci failed, %s (%d)\n", strerror(errno), errno);
         buf[0] = 0;
         return NETMD_USB_ERROR;
     }
@@ -144,15 +142,13 @@ netmd_error netmd_get_devname(netmd_dev_handle* devh, char* buf, size_t buffsize
 netmd_error netmd_close(netmd_dev_handle* devh)
 {
     int result;
-    usb_dev_handle *dev;
-
-    dev = (usb_dev_handle *)devh;
-    result = usb_release_interface(dev, 0);
-    if (result == 0) {
-        result = usb_close(dev);
-    }
+    libusb_device_handle *dev;
 
-    if (result < 0) {
+    dev = (libusb_device_handle *)devh;
+    result = libusb_release_interface(dev, 0);
+    if (result == 0)
+      libusb_close(dev);
+    else{
         return NETMD_USB_ERROR;
     }
 
@@ -172,4 +168,6 @@ void netmd_clean(netmd_device **device_list)
     }
 
     *device_list = NULL;
+
+    libusb_exit(ctx);
 }
diff --git a/libnetmd/netmd_dev.h b/libnetmd/netmd_dev.h
index fba35a3..ba6cea5 100644
--- a/libnetmd/netmd_dev.h
+++ b/libnetmd/netmd_dev.h
@@ -7,7 +7,7 @@
 typedef struct netmd_device {
     struct netmd_device *link;
     char name[32];
-    struct usb_device *usb_dev;
+    struct libusb_device *usb_dev;
 } netmd_device;
 
 /**
@@ -43,7 +43,7 @@ netmd_error netmd_open(netmd_device *dev, netmd_dev_handle **dev_handle);
   @param buf Buffer to hold the name.
   @param buffsize Available size in buf.
 */
-netmd_error netmd_get_devname(netmd_dev_handle* devh, char* buf, size_t buffsize);
+netmd_error netmd_get_devname(netmd_dev_handle* devh, unsigned char* buf, size_t buffsize);
 
 /**
   Closes the usb descriptors.
diff --git a/libnetmd/secure.c b/libnetmd/secure.c
index c58b1e8..f74e4cd 100644
--- a/libnetmd/secure.c
+++ b/libnetmd/secure.c
@@ -31,7 +31,7 @@
 
 #include <string.h>
 #include <stdio.h>
-#include <usb.h>
+#include <libusb-1.0/libusb.h>
 #include <errno.h>
 #include <gcrypt.h>
 
@@ -355,6 +355,7 @@ void netmd_transfer_song_packets(netmd_dev_handle *dev,
     unsigned char *packet, *buf;
     size_t packet_size;
     int error;
+    int transferred = 0;
 
     p = packets;
     while (p != NULL) {
@@ -370,7 +371,7 @@ void netmd_transfer_song_packets(netmd_dev_handle *dev,
         memcpy(buf + 16, p->data, p->length);
 
         /* ... send it */
-        error = usb_bulk_write((usb_dev_handle*)dev, 2, (char*)packet, (int)packet_size, 10000);
+        error = libusb_bulk_transfer((libusb_device_handle*)dev, 2, packet, (int)packet_size, &transferred, 10000);
         netmd_log(NETMD_LOG_DEBUG, "%d %d\n", packet_size, error);
 
         /* cleanup */
@@ -557,9 +558,10 @@ netmd_error netmd_secure_send_track(netmd_dev_handle *dev,
 netmd_error netmd_secure_real_recv_track(netmd_dev_handle *dev, uint32_t length, FILE *file, size_t chunksize)
 {
     uint32_t done = 0;
-    char *data;
+    unsigned char *data;
     int32_t read;
     netmd_error error = NETMD_NO_ERROR;
+    int transferred = 0;
 
     data = malloc(chunksize);
     while (done < length) {
@@ -567,7 +569,7 @@ netmd_error netmd_secure_real_recv_track(netmd_dev_handle *dev, uint32_t length,
             chunksize = length - done;
         }
 
-        read = usb_bulk_read((usb_dev_handle*)dev, 0x81, data, (int)chunksize, 10000);
+        read = libusb_bulk_transfer((libusb_device_handle*)dev, 0x81, data, (int)chunksize, &transferred, 10000);
 
         if (read >= 0) {
             done += (uint32_t)read;
-- 
1.7.5.4




<-- thread -->
<-- date -->
  • References:
    • [linux-minidisc] [PATCH] Port libnetmd to libusb-1.0
      • From: Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
  • linux-minidisc - September 2011 - Archives indexes sorted by:
    [ thread ] [ subject ] [ author ] [ date ]
  • Complete archive of the linux-minidisc mailing list
  • More info on this list...

Hilfe

  • FAQ
  • Dienstbeschreibung
  • ZEDAT Beratung
  • postmaster@lists.fu-berlin.de

Service-Navigation

  • Startseite
  • Listenauswahl

Einrichtung Mailingliste

  • ZEDAT-Portal
  • Mailinglisten Portal