summaryrefslogtreecommitdiffstats
path: root/extra/bcm203x.c
blob: 5990ae8c49ef09e28ac4e935014fe7a243d80446 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2001-2002  Maxim Krasnyansky <maxk@qualcomm.com>
 *  Copyright (C) 2003-2004  Marcel Holtmann <marcel@holtmann.org>
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation;
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
 *  CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *  ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
 *  COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
 *  SOFTWARE IS DISCLAIMED.
 *
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

#include <usb.h>

#ifdef NEED_USB_GET_BUSSES
static inline struct usb_bus *usb_get_busses(void)
{
	return usb_busses;
}
#endif

#ifdef NEED_USB_INTERRUPT_READ
static inline int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout)
{
	return usb_bulk_read(dev, ep, bytes, size, timeout);
}
#endif

static char *fw_path = "/lib/firmware";

static int load_file(struct usb_dev_handle *udev, char *filename)
{
	char buf[4096];
	int fd, err, len;

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		syslog(LOG_ERR, "Can't open file %s", filename);
		return fd;
	}

	while (1) { 
		len = read(fd, buf, sizeof(buf)); 
		if (len < 0) {
			syslog(LOG_ERR, "Can't read from file %s", filename);
			close(fd);
			return len;
		}

		if (len == 0)
			break;

		err = usb_bulk_write(udev, 0x02, buf, len, 100000);
		if (err < 0) {
			syslog(LOG_ERR, "Can't write bulk data packet");
			close(fd);
			return err;
		}

		if (err != len) {
			syslog(LOG_ERR, "Partial bulk packet written");
			close(fd);
			return -EIO;
		}
	}

	close(fd);
	return 0;
}

static void load_firmware(struct usb_device *dev)
{
	struct usb_dev_handle *udev;
	char filename[PATH_MAX + 1];
	char buf[16];

	udev = usb_open(dev);
	if (!udev) {
		syslog(LOG_ERR, "Can't open USB device %s/%s",
					dev->bus->dirname, dev->filename);
		return;
	}

	if (usb_claim_interface(udev, 0) < 0) {
		usb_close(udev);
		return;
	}

	syslog(LOG_INFO, "Loading firmware to device %s/%s",
					dev->bus->dirname, dev->filename);

	snprintf(filename, PATH_MAX, "%s/%s", fw_path, "BCM2033-MD.hex");
	if (load_file(udev, filename) < 0)
		goto done;

	usleep(10);

	if (usb_bulk_write(udev, 0x02, "#", 1, 1000) < 0) {
		syslog(LOG_ERR, "Can't write bulk transfer");
		goto done;
	}

	memset(buf, 0, sizeof(buf));
	if (usb_interrupt_read(udev, 0x81, buf, 16, 1000) < 0) {
		syslog(LOG_ERR, "Can't read interrupt transfer");
		goto done;
	}

	if (buf[0] != '#') {
		syslog(LOG_ERR, "Memory select failed with '%c'", buf[0]);
		goto done;
	}

	snprintf(filename, PATH_MAX, "%s/%s", fw_path, "BCM2033-FW.bin");
	if (load_file(udev, filename) < 0)
		goto done;

	memset(buf, 0, sizeof(buf));
	if (usb_interrupt_read(udev, 0x81, buf, 16, 1000) < 0) {
		syslog(LOG_ERR, "Can't read interrupt transfer");
		goto done;
	}

	if (buf[0] == '.') {
		syslog(LOG_INFO, "Firmware loaded successful to device %s/%s",
					dev->bus->dirname, dev->filename);
	} else {
		syslog(LOG_ERR, "Firmware loading failed with '%c'", buf[0]);
		goto done;
	}

	usleep(500000);

done:
	sleep(1);

	usb_release_interface(udev, 0);
	usb_close(udev);
}

int main(int argc, char *argv[])
{
	struct usb_bus *bus;
	struct usb_device *dev;
	char *action, *device, *busname, *devname;

	action = getenv("ACTION");
	device = getenv("DEVICE");

	if (!action || (strcmp(action, "add") && strcmp(action, "register")))
		exit(0);

	openlog("bcm203x", LOG_NDELAY | LOG_PID, LOG_DAEMON);

	if (!device || strncmp(device, "/proc/bus/usb/", 14)) {
		syslog(LOG_ERR, "Unknown device path %s", device);
		closelog();
		exit(1);
	}

	busname = strtok(device + 14, "/");
	devname = strtok(NULL, "/");

	usb_init();

	usb_find_busses();
	usb_find_devices();

	for (bus = usb_get_busses(); bus; bus = bus->next)
		for (dev = bus->devices; dev; dev = dev->next)
			if (!strcmp(bus->dirname, busname) &&
					!strcmp(dev->filename, devname)) {
				load_firmware(dev);
				break;
			}

	closelog();

	return 0;
}