libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
gl_usb.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the sigrok project.
00003  *
00004  * Copyright (C) 2010 Sven Peter <sven@fail0verflow.com>
00005  * Copyright (C) 2010 Haxx Enterprises <bushing@gmail.com>
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or
00009  * without modification, are permitted provided that the following
00010  * conditions are met:
00011  *
00012  * * Redistributions of source code must retain the above copyright notice,
00013  *   this list of conditions and the following disclaimer.
00014  *
00015  * * Redistributions in binary form must reproduce the above copyright notice,
00016  *   this list of conditions and the following disclaimer in the documentation
00017  *   and/or other materials provided with the distribution.
00018  *
00019  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00029  *  THE POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #include <libusb.h>
00033 #include <stdio.h>
00034 #include "sigrok.h"
00035 #include "sigrok-internal.h"
00036 #include "gl_usb.h"
00037 
00038 #define CTRL_IN         (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | \
00039                          LIBUSB_RECIPIENT_INTERFACE)
00040 #define CTRL_OUT        (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | \
00041                          LIBUSB_RECIPIENT_INTERFACE)
00042 #define EP1_BULK_IN     (LIBUSB_ENDPOINT_IN | 1)
00043 
00044 #define TIMEOUT         5000    /* Timeout in ms */
00045 
00046 enum {
00047         REQ_READBULK = 0x82,
00048         REQ_WRITEADDR,
00049         REQ_READDATA,
00050         REQ_WRITEDATA,
00051 };
00052 
00053 static int gl_write_address(libusb_device_handle *devh, unsigned int address)
00054 {
00055         unsigned char packet[8] = { address & 0xFF };
00056         int ret;
00057 
00058         ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEADDR,
00059                                          0, packet, 1, TIMEOUT);
00060         if (ret != 1)
00061                 sr_err("zp: %s: libusb_control_transfer returned %d.",
00062                        __func__, ret);
00063         return ret;
00064 }
00065 
00066 static int gl_write_data(libusb_device_handle *devh, unsigned int val)
00067 {
00068         unsigned char packet[8] = { val & 0xFF };
00069         int ret;
00070 
00071         ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA,
00072                                       0, packet, 1, TIMEOUT);
00073         if (ret != 1)
00074                 sr_err("zp: %s: libusb_control_transfer returned %d.",
00075                        __func__, ret);
00076         return ret;
00077 }
00078 
00079 static int gl_read_data(libusb_device_handle *devh)
00080 {
00081         unsigned char packet[8] = { 0 };
00082         int ret;
00083 
00084         ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA,
00085                                       0, packet, 1, TIMEOUT);
00086         if (ret != 1)
00087                 sr_err("zp: %s: libusb_control_transfer returned %d, "
00088                        "val=%hhx.", __func__, ret, packet[0]);
00089         return (ret == 1) ? packet[0] : ret;
00090 }
00091 
00092 SR_PRIV int gl_read_bulk(libusb_device_handle *devh, void *buffer,
00093                          unsigned int size)
00094 {
00095         unsigned char packet[8] =
00096             { 0, 0, 0, 0, size & 0xff, (size & 0xff00) >> 8,
00097               (size & 0xff0000) >> 16, (size & 0xff000000) >> 24 };
00098         int ret, transferred = 0;
00099 
00100         ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK,
00101                                       0, packet, 8, TIMEOUT);
00102         if (ret != 8)
00103                 sr_err("zp: %s: libusb_control_transfer returned %d.",
00104                        __func__, ret);
00105 
00106         ret = libusb_bulk_transfer(devh, EP1_BULK_IN, buffer, size,
00107                                    &transferred, TIMEOUT);
00108         if (ret < 0)
00109                 sr_err("zp: Bulk read error %d.", ret);
00110         return transferred;
00111 }
00112 
00113 SR_PRIV int gl_reg_write(libusb_device_handle *devh, unsigned int reg,
00114                  unsigned int val)
00115 {
00116         int ret;
00117 
00118         ret = gl_write_address(devh, reg);
00119         if (ret < 0)
00120                 return ret;
00121         ret = gl_write_data(devh, val);
00122         return ret;
00123 }
00124 
00125 SR_PRIV int gl_reg_read(libusb_device_handle *devh, unsigned int reg)
00126 {
00127         int ret;
00128 
00129         ret = gl_write_address(devh, reg);
00130         if (ret < 0)
00131                 return ret;
00132         ret = gl_read_data(devh);
00133         return ret;
00134 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines