picoproj: make the help about power mode more consistent
[libam7xxx.git] / examples / am7xxx_mode_switch.c
1 /* am7xxx_mode_switch - a simple usb_mode_switch for am7xxx devices
2  *
3  * Copyright (C) 2012  Antonio Ospite <ospite@studenti.unina.it>
4  *
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 2 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
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/>.
17  */
18
19 #include <stdio.h>
20 #include <errno.h>
21 #include <libusb.h>
22
23 #define AM7XXX_STORAGE_VID           0x1de1
24 #define AM7XXX_STORAGE_PID           0x1101
25 #define AM7XXX_STORAGE_CONFIGURATION 1
26 #define AM7XXX_STORAGE_INTERFACE     0
27 #define AM7XXX_STORAGE_OUT_EP        0x01
28
29 static unsigned char switch_command[] =
30         "\x55\x53\x42\x43\x08\x70\x52\x89\x00\x00\x00\x00\x00\x00"
31         "\x0c\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
32
33 int main(void)
34 {
35         int ret;
36         int transferred;
37         libusb_device_handle *usb_device = NULL;
38
39         unsigned int len;
40
41         ret = libusb_init(NULL);
42         if (ret < 0)
43                 goto out;
44
45         libusb_set_debug(NULL, 3);
46
47         usb_device = libusb_open_device_with_vid_pid(NULL,
48                                                      AM7XXX_STORAGE_VID,
49                                                      AM7XXX_STORAGE_PID);
50         if (usb_device == NULL) {
51                 fprintf(stderr, "cannot open the device: %d.\n", errno);
52                 ret = -errno;
53                 goto out;
54         }
55
56         if (libusb_kernel_driver_active(usb_device, AM7XXX_STORAGE_INTERFACE)) {
57                 ret = libusb_detach_kernel_driver(usb_device,
58                                                   AM7XXX_STORAGE_INTERFACE);
59                 if (ret < 0)
60                         fprintf(stderr, "Warning: cannot detach kernel driver.\n");
61         } else {
62                 fprintf(stderr, "kernel driver not active.\n");
63         }
64
65         ret = libusb_set_configuration(usb_device, AM7XXX_STORAGE_CONFIGURATION);
66         if (ret < 0) {
67                 fprintf(stderr, "cannot set configuration.\n");
68                 goto out_libusb_close;
69         }
70
71         ret = libusb_claim_interface(usb_device, AM7XXX_STORAGE_INTERFACE);
72         if (ret < 0) {
73                 fprintf(stderr, "cannot claim interface.\n");
74                 goto out_libusb_close;
75         }
76
77         len = sizeof(switch_command);
78         transferred = 0;
79         ret = libusb_bulk_transfer(usb_device, AM7XXX_STORAGE_OUT_EP,
80                                    switch_command, len, &transferred, 0);
81         if (ret != 0 || (unsigned int)transferred != len) {
82                 fprintf(stderr, "ret: %d\ttransferred: %d (expected %u)\n",
83                       ret, transferred, len);
84                 goto out_libusb_close;
85         }
86
87         fprintf(stderr, "OK, command sent!\n");
88
89 out_libusb_close:
90         libusb_release_interface(usb_device, AM7XXX_STORAGE_INTERFACE);
91         libusb_close(usb_device);
92         usb_device = NULL;
93
94 out:
95         libusb_exit(NULL);
96         return ret;
97 }