-
Notifications
You must be signed in to change notification settings - Fork 3
/
quickcall.c
279 lines (211 loc) · 6.34 KB
/
quickcall.c
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <glob.h>
#include <usb.h>
#include <ccan/talloc/talloc.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/str/str.h>
#include "lib.h"
#include "quickcall.h"
static struct usb_bus *find_libusb_bus(const char *sysdir)
{
struct usb_bus *bus;
long busnum;
int rc;
rc = get_sys_attrib(sysdir, "busnum", 10, &busnum);
if (rc)
die("Couldn't get busnum from %s: %s\n",
sysdir, strerror(errno));
for (bus = usb_get_busses(); bus; bus = bus->next) {
char *eptr;
int num = strtol(bus->dirname, &eptr, 10);
if (*eptr)
die("Couldn't parse bus dirname \"%s\"\n", bus->dirname);
if (busnum == num)
return bus;
}
return NULL;
}
static struct usb_device *find_libusb_dev(const char *sysdir, struct usb_bus *bus)
{
struct usb_device *dev;
long devnum;
int rc;
rc = get_sys_attrib(sysdir, "devnum", 10, &devnum);
if (rc)
die("Couldn't get devnum from %s: %s\n",
sysdir, strerror(errno));
for (dev = bus->devices; dev; dev = dev->next) {
char *eptr;
int num = strtol(dev->filename, &eptr, 10);
if (*eptr)
die("Couldn't parse dev filename \"%s\"\n",
dev->filename);
if (devnum == num)
return dev;
}
return NULL;
}
static int sysfs_is_quickcall(const char *sysdir, uint16_t vendor, uint16_t product)
{
int rc;
struct stat sb;
long idv, idp;
rc = stat(sysdir, &sb);
if (rc != 0)
die("Couldn't stat %s: %s\n", sysdir, strerror(errno));
if (!S_ISDIR(sb.st_mode))
die("%s is not a directory\n", sysdir);
rc = get_sys_attrib(sysdir, "idVendor", 16, &idv);
if (rc < 0)
die("Couldn't get idVendor from %s: %s\n",
sysdir, strerror(errno));
rc = get_sys_attrib(sysdir, "idProduct", 16, &idp);
if (rc < 0)
die("Couldn't get idProduct from %s: %s\n",
sysdir, strerror(errno));
return (idv == vendor) && (idp == product);
}
static char *find_hiddev(const char *sysdir)
{
char *pattern, *tmp, *hiddev;
glob_t gglob;
int rc;
struct stat sb;
tmp = strrchr(sysdir, '/');
assert(tmp);
pattern = talloc_asprintf(NULL, "%s/%s:1.3/usb*/hiddev*",
sysdir, tmp + 1);
rc = glob(pattern, GLOB_NOSORT, NULL, &gglob);
if (rc != 0)
die("glob(\"%s\") failed\n", pattern);
if (gglob.gl_pathc != 1)
die("%zd matches for %s instead of expected 1\n",
gglob.gl_pathc, pattern);
talloc_free(pattern);
tmp = strrchr(gglob.gl_pathv[0], '/');
assert(tmp);
hiddev = talloc_asprintf(NULL, "/dev/usb/%s", tmp + 1);
globfree(&gglob);
rc = stat(hiddev, &sb);
if (rc != 0)
die("Couldn't stat %s: %s\n", hiddev, strerror(errno));
if (!S_ISCHR(sb.st_mode))
die("%s is not a character device\n", hiddev);
return hiddev;
}
static char *find_alsadev(const char *sysdir)
{
char *pattern, *tmp, *eptr, *ctldev;
glob_t gglob;
int rc;
struct stat sb;
int cardnum;
tmp = strrchr(sysdir, '/');
assert(tmp);
pattern = talloc_asprintf(NULL, "%s/%s:1.0/sound/card**",
sysdir, tmp + 1);
rc = glob(pattern, GLOB_NOSORT, NULL, &gglob);
if (rc != 0)
die("glob(\"%s\") failed\n", pattern);
if (gglob.gl_pathc != 1)
die("%zd matches for %s instead of expected 1\n",
gglob.gl_pathc, pattern);
talloc_free(pattern);
tmp = strrchr(gglob.gl_pathv[0], '/');
assert(tmp);
tmp += 5; /* skip past "/card" */
cardnum = strtol(tmp, &eptr, 10);
if (*eptr)
die("Couldn't parse ALSA card number from \"%s\"\n",
gglob.gl_pathv[0]);
globfree(&gglob);
/* Sanity check */
ctldev = talloc_asprintf(NULL, "/dev/snd/controlC%d", cardnum);
rc = stat(ctldev, &sb);
if (rc != 0)
die("Couldn't stat %s: %s\n", ctldev, strerror(errno));
if (!S_ISCHR(sb.st_mode))
die("%s is not a character device\n", ctldev);
talloc_free(ctldev);
return talloc_asprintf(NULL, "hw:%d", cardnum);
}
struct quickcall *quickcall_probe(const char *sysdir)
{
struct usb_bus *bus;
struct usb_device *dev;
char *hiddev, *alsadev;
struct quickcall *qc;
debug("Probing for Quickcall at %s\n", sysdir);
if (!sysfs_is_quickcall(sysdir, QUICKCALL_VENDORID, QUICKCALL_PRODUCTID))
return NULL;
bus = find_libusb_bus(sysdir);
if (!bus)
die("Couldn't locate bus for %s via libusb\n", sysdir);
dev = find_libusb_dev(sysdir, bus);
if (!dev)
die("Couldn't locate dev for %s via libusb\n", sysdir);
/* Sanity check */
if ((dev->descriptor.idVendor != QUICKCALL_VENDORID)
|| (dev->descriptor.idProduct != QUICKCALL_PRODUCTID))
die("ID retreived from libusb (%04x:%04x) does not"
" match Quickcall (%04x:%04x)",
dev->descriptor.idVendor, dev->descriptor.idProduct,
QUICKCALL_VENDORID, QUICKCALL_PRODUCTID);
debug("Quickcall located at %s\n", sysdir);
hiddev = find_hiddev(sysdir);
debug("Quickcall hiddev is %s\n", hiddev);
alsadev = find_alsadev(sysdir);
debug("ALSA device is %s\n", alsadev);
qc = talloc_zero(NULL, struct quickcall);
if (!qc)
die("Couldn't allocate memory\n");
qc->sysdir = talloc_strdup(qc, sysdir);
qc->bus = bus;
qc->dev = dev;
qc->hiddev = hiddev;
talloc_steal(qc, hiddev);
qc->alsadev = alsadev;
talloc_steal(qc, alsadev);
return qc;
}
void quickcall_open(struct quickcall *qc)
{
char outbuf = 0x01;
char inbuf[2];
int rc;
qc->handle = usb_open(qc->dev);
if (!qc->handle)
die("Couldn't usb_open(): %s\n", strerror(errno));
debug("Opened Quickcall libusb handle\n");
qc->hidfd = open(qc->hiddev, O_RDWR);
if (qc->hidfd < 0)
die("Couldn't open %s: %s\n", qc->hiddev, strerror(errno));
debug("Opened Quickcall hiddev (fd=%d)\n", qc->hidfd);
rc = snd_ctl_open(&qc->alsactl, qc->alsadev, 0);
if (rc != 0)
die("Error on snd_ctl_open(): %s\n", strerror(errno));
/* Initialize the hardware */
rc = usb_control_msg(qc->handle,
USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
0x82, 0, 0, &outbuf, sizeof(outbuf), 100);
if (rc < 0)
die("Error sending control message 0x82: %s\n", strerror(errno));
if (rc != sizeof(outbuf))
die("Short write on initialization request 0x82\n");
rc = usb_control_msg(qc->handle,
USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
0x81, 0, 0, inbuf, sizeof(inbuf), 100);
if (rc < 0)
die("Error sending control message 0x81: %s\n", strerror(errno));
if (rc != sizeof(inbuf))
die("Short read on initialization request 0x81\n");
debug("Hardware initialization complete (0x81 -> 0x%02x,0x%02x)\n",
inbuf[0], inbuf[1]);
}