Debian Codegraph @

src/libdfu.c

raw
1 /*
2 * dfu-programmer
3 *
4 * $Id$
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <libusb-1.0/libusb.h>
22 #include <string.h>
23
24 #include "config.h"
25 #include "dfu-device.h"
26 #include "dfu.h"
27 #include "arguments.h"
28 #include "commands.h"
29 #include "atmel.h"
30 #include "libdfu.h"
31 #include "config.h"
32
33 // NOTE: Technically not thread safe... but since it's not changed when used as a library, it's safe.
34 int debug;
35
36 static const char *progname = PACKAGE;
37
38 int dfu_programmer(struct programmer_arguments * args)
39 {
40 int retval = SUCCESS;
41 dfu_device_t dfu_device;
42 struct libusb_device *device = NULL;
43 libusb_context *usbContext;
44
45 memset(&dfu_device, 0, sizeof(dfu_device));
46
47 if (libusb_init(&usbContext))
48 {
49 fprintf(stderr, "%s: can't init libusb.\n", progname);
50 return DEVICE_ACCESS_ERROR;
51 }
52
53 if (debug >= 200)
54 {
55 #if LIBUSB_API_VERSION >= 0x01000106
56 libusb_set_option(usbContext, LIBUSB_OPTION_LOG_LEVEL, debug);
57 #else
58 libusb_set_debug(usbContext, debug);
59 #endif
60 }
61
62 device = dfu_device_init(args->vendor_id, args->chip_id,
63 args->bus_id, args->device_address,
64 &dfu_device,
65 args->initial_abort,
66 args->honor_interfaceclass,
67 usbContext);
68
69 if (NULL == device)
70 {
71 fprintf(stderr, "%s: no device present.\n", progname);
72 retval = DEVICE_ACCESS_ERROR;
73 goto error;
74 }
75
76 retval = execute_command(&dfu_device, args);
77
78 error:
79 if (NULL != dfu_device.handle)
80 {
81 int rv;
82
83 rv = libusb_release_interface(dfu_device.handle, dfu_device.interface);
84 /* The RESET command sometimes causes the usb_release_interface command to fail.
85 It is not obvious why this happens but it may be a glitch due to the hardware
86 reset in the attached device. In any event, since reset causes a USB detach
87 this should not matter, so there is no point in raising an alarm.
88 */
89 if (0 != rv && !(com_launch == args->command &&
90 args->com_launch_config.noreset == 0))
91 {
92 fprintf(stderr, "%s: failed to release interface %d.\n",
93 progname, dfu_device.interface);
94 retval = DEVICE_ACCESS_ERROR;
95 }
96 }
97
98 if (NULL != dfu_device.handle)
99 {
100 libusb_close(dfu_device.handle);
101 }
102
103 libusb_exit(usbContext);
104
105 return retval;
106 }