1 /* am7xxx-modeswitch - a simple usb-modeswitch for am7xxx devices
3 * Copyright (C) 2012-2014 Antonio Ospite <ao2@ao2.it>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #define AM7XXX_STORAGE_VID 0x1de1
25 #define AM7XXX_STORAGE_PID 0x1101
26 #define AM7XXX_STORAGE_CONFIGURATION 1
27 #define AM7XXX_STORAGE_INTERFACE 0
28 #define AM7XXX_STORAGE_OUT_EP 0x01
30 static unsigned char switch_command[] =
31 "\x55\x53\x42\x43\x08\x70\x52\x89\x00\x00\x00\x00\x00\x00"
32 "\x0c\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
38 libusb_device_handle *usb_device;
42 ret = libusb_init(NULL);
44 fprintf(stderr, "libusb_init failed: %s\n",
45 libusb_error_name(ret));
49 libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_INFO);
51 usb_device = libusb_open_device_with_vid_pid(NULL,
54 if (usb_device == NULL) {
55 fprintf(stderr, "libusb_open failed: %s\n", strerror(errno));
60 ret = libusb_set_configuration(usb_device, AM7XXX_STORAGE_CONFIGURATION);
62 fprintf(stderr, "libusb_set_configuration failed: %s\n",
63 libusb_error_name(ret));
64 fprintf(stderr, "Cannot set configuration %hhu\n",
65 AM7XXX_STORAGE_CONFIGURATION);
66 goto out_libusb_close;
69 libusb_set_auto_detach_kernel_driver(usb_device, 1);
71 ret = libusb_claim_interface(usb_device, AM7XXX_STORAGE_INTERFACE);
73 fprintf(stderr, "libusb_claim_interface failed: %s\n",
74 libusb_error_name(ret));
75 fprintf(stderr, "Cannot claim interface %hhu\n",
76 AM7XXX_STORAGE_INTERFACE);
77 goto out_libusb_close;
80 len = sizeof(switch_command);
82 ret = libusb_bulk_transfer(usb_device, AM7XXX_STORAGE_OUT_EP,
83 switch_command, len, &transferred, 0);
84 if (ret != 0 || (unsigned int)transferred != len) {
85 fprintf(stderr, "ret: %d\ttransferred: %d (expected %u)\n",
86 ret, transferred, len);
87 goto out_libusb_release_interface;
90 fprintf(stderr, "OK, command sent!\n");
92 out_libusb_release_interface:
93 libusb_release_interface(usb_device, AM7XXX_STORAGE_INTERFACE);
95 libusb_close(usb_device);