/usr/share/doc/libam7xxx-dev/examples/am7xxx-modeswitch.c is in libam7xxx-dev 0.1.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | /* am7xxx-modeswitch - a simple usb-modeswitch for am7xxx devices
*
* Copyright (C) 2012-2014 Antonio Ospite <ao2@ao2.it>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <libusb.h>
#define AM7XXX_STORAGE_VID 0x1de1
#define AM7XXX_STORAGE_PID 0x1101
#define AM7XXX_STORAGE_CONFIGURATION 1
#define AM7XXX_STORAGE_INTERFACE 0
#define AM7XXX_STORAGE_OUT_EP 0x01
static unsigned char switch_command[] =
"\x55\x53\x42\x43\x08\x70\x52\x89\x00\x00\x00\x00\x00\x00"
"\x10\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
int main(void)
{
int ret;
libusb_device_handle *usb_device;
int current_configuration;
unsigned int len;
int transferred;
ret = libusb_init(NULL);
if (ret < 0) {
fprintf(stderr, "libusb_init failed: %s\n",
libusb_error_name(ret));
goto out;
}
libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_INFO);
usb_device = libusb_open_device_with_vid_pid(NULL,
AM7XXX_STORAGE_VID,
AM7XXX_STORAGE_PID);
if (usb_device == NULL) {
fprintf(stderr, "libusb_open failed: %s\n", strerror(errno));
ret = -errno;
goto out;
}
current_configuration = -1;
ret = libusb_get_configuration(usb_device, ¤t_configuration);
if (ret < 0) {
fprintf(stderr, "libusb_get_configuration failed: %s\n",
libusb_error_name(ret));
goto out_libusb_close;
}
if (current_configuration != AM7XXX_STORAGE_CONFIGURATION) {
ret = libusb_set_configuration(usb_device,
AM7XXX_STORAGE_CONFIGURATION);
if (ret < 0) {
fprintf(stderr, "libusb_set_configuration failed: %s\n",
libusb_error_name(ret));
fprintf(stderr, "Cannot set configuration %d\n",
AM7XXX_STORAGE_CONFIGURATION);
goto out_libusb_close;
}
}
libusb_set_auto_detach_kernel_driver(usb_device, 1);
ret = libusb_claim_interface(usb_device, AM7XXX_STORAGE_INTERFACE);
if (ret < 0) {
fprintf(stderr, "libusb_claim_interface failed: %s\n",
libusb_error_name(ret));
fprintf(stderr, "Cannot claim interface %d\n",
AM7XXX_STORAGE_INTERFACE);
goto out_libusb_close;
}
/* Checking that the configuration has not changed, as suggested in
* http://libusb.sourceforge.net/api-1.0/caveats.html
*/
current_configuration = -1;
ret = libusb_get_configuration(usb_device, ¤t_configuration);
if (ret < 0) {
fprintf(stderr, "libusb_get_configuration after claim failed: %s\n",
libusb_error_name(ret));
goto out_libusb_release_interface;
}
if (current_configuration != AM7XXX_STORAGE_CONFIGURATION) {
fprintf(stderr, "libusb configuration changed (expected: %d, current: %d\n",
AM7XXX_STORAGE_CONFIGURATION, current_configuration);
ret = -EINVAL;
goto out_libusb_release_interface;
}
len = sizeof(switch_command);
transferred = 0;
ret = libusb_bulk_transfer(usb_device, AM7XXX_STORAGE_OUT_EP,
switch_command, len, &transferred, 0);
if (ret != 0 || (unsigned int)transferred != len) {
fprintf(stderr, "ret: %d\ttransferred: %d (expected %u)\n",
ret, transferred, len);
goto out_libusb_release_interface;
}
fprintf(stderr, "OK, command sent!\n");
out_libusb_release_interface:
libusb_release_interface(usb_device, AM7XXX_STORAGE_INTERFACE);
out_libusb_close:
libusb_close(usb_device);
usb_device = NULL;
out:
libusb_exit(NULL);
return ret;
}
|