fix data type of the "response" field (https://dev.openwrt.org/ticket/14062)
[project/usbmode.git] / switch.c
1 #include <unistd.h>
2 #include "switch.h"
3
4 enum {
5 DATA_MODE,
6 DATA_MSG,
7 DATA_INTERFACE,
8 DATA_MSG_EP,
9 DATA_RES_EP,
10 DATA_RESPONSE,
11 DATA_RELEASE_DELAY,
12 DATA_CONFIG,
13 DATA_ALT,
14 __DATA_MAX
15 };
16
17 static void detach_driver(struct usbdev_data *data)
18 {
19 libusb_detach_kernel_driver(data->devh, data->interface);
20 }
21
22 struct msg_entry {
23 char *data;
24 int len;
25 };
26
27 static int send_msg(struct usbdev_data *data, struct msg_entry *msg)
28 {
29 int transferred;
30
31 return libusb_bulk_transfer(data->devh, data->msg_endpoint,
32 (void *) msg->data, msg->len,
33 &transferred, 3000);
34 }
35
36 static int read_response(struct usbdev_data *data, int len)
37 {
38 unsigned char *buf;
39 int ret, transferred;
40
41 if (len < 13)
42 len = 13;
43 buf = alloca(len);
44 ret = libusb_bulk_transfer(data->devh, data->response_endpoint,
45 buf, len, &transferred, 3000);
46 libusb_bulk_transfer(data->devh, data->response_endpoint,
47 buf, 13, &transferred, 100);
48 return ret;
49 }
50
51 static void send_messages(struct usbdev_data *data, struct msg_entry *msg, int n_msg)
52 {
53 int i, len;
54
55 libusb_claim_interface(data->devh, data->interface);
56 libusb_clear_halt(data->devh, data->msg_endpoint);
57
58 for (i = 0; i < n_msg; i++) {
59 if (send_msg(data, &msg[i])) {
60 fprintf(stderr, "Failed to send switch message\n");
61 continue;
62 }
63
64 if (!data->need_response)
65 continue;
66
67 if (!memcmp(msg[i].data, "\x55\x53\x42\x43", 4))
68 len = 13;
69 else
70 len = msg[i].len;
71
72 if (read_response(data, len))
73 return;
74 }
75
76 libusb_clear_halt(data->devh, data->msg_endpoint);
77 libusb_clear_halt(data->devh, data->response_endpoint);
78
79 usleep(200000);
80
81 if (data->release_delay)
82 usleep(data->release_delay * 1000);
83
84 libusb_release_interface(data->devh, data->interface);
85 return;
86 }
87
88 static void send_config_messages(struct usbdev_data *data, struct blob_attr *attr)
89 {
90 struct blob_attr *cur;
91 int rem, n_msg = 0;
92 struct msg_entry *msg;
93
94 blobmsg_for_each_attr(cur, attr, rem)
95 n_msg++;
96
97 msg = alloca(n_msg * sizeof(*msg));
98 n_msg = 0;
99 blobmsg_for_each_attr(cur, attr, rem) {
100 int msg_nr;
101
102 if (blobmsg_type(cur) != BLOBMSG_TYPE_INT32) {
103 fprintf(stderr, "Invalid data in message list\n");
104 return;
105 }
106
107 msg_nr = blobmsg_get_u32(cur);
108 if (msg_nr >= n_messages) {
109 fprintf(stderr, "Message index out of range!\n");
110 return;
111 }
112
113 msg[n_msg].data = messages[msg_nr];
114 msg[n_msg++].len = message_len[msg_nr];
115 }
116
117 send_messages(data, msg, n_msg);
118 }
119
120 static void handle_generic(struct usbdev_data *data, struct blob_attr **tb)
121 {
122 detach_driver(data);
123 send_config_messages(data, tb[DATA_MSG]);
124 }
125
126 static void send_control_packet(struct usbdev_data *data, uint8_t type, uint8_t req,
127 uint16_t val, uint16_t idx, int len)
128 {
129 unsigned char *buffer = alloca(len ? len : 1);
130
131 libusb_control_transfer(data->devh, type, req, val, idx, buffer, len, 1000);
132 }
133
134 static void handle_huawei(struct usbdev_data *data, struct blob_attr **tb)
135 {
136 int type = LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE;
137 send_control_packet(data, type, LIBUSB_REQUEST_SET_FEATURE, 1, 0, 0);
138 }
139
140 static void handle_sierra(struct usbdev_data *data, struct blob_attr **tb)
141 {
142 int type = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE;
143 send_control_packet(data, type, LIBUSB_REQUEST_SET_INTERFACE, 1, 0, 0);
144 }
145
146 static void handle_sony(struct usbdev_data *data, struct blob_attr **tb)
147 {
148 int type = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN;
149 int i;
150
151 detach_driver(data);
152 send_control_packet(data, type, 0x11, 2, 0, 3);
153
154 libusb_close(data->devh);
155 sleep(5);
156
157 for (i = 0; i < 25; i++) {
158 data->devh = libusb_open_device_with_vid_pid(usb,
159 data->desc.idVendor, data->desc.idProduct);
160 if (data->devh)
161 break;
162 }
163
164 send_control_packet(data, type, 0x11, 2, 0, 3);
165 }
166
167 static void handle_qisda(struct usbdev_data *data, struct blob_attr **tb)
168 {
169 static unsigned char buffer[] = "\x05\x8c\x04\x08\xa0\xee\x20\x00\x5c\x01\x04\x08\x98\xcd\xea\xbf";
170 int type = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE;
171
172 libusb_control_transfer(data->devh, type, 0x04, 0, 0, buffer, 16, 1000);
173 }
174
175 static void handle_gct(struct usbdev_data *data, struct blob_attr **tb)
176 {
177 int type = LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_IN;
178
179 detach_driver(data);
180
181 if (libusb_claim_interface(data->devh, data->interface))
182 return;
183
184 send_control_packet(data, type, 0xa0, 0, data->interface, 1);
185 send_control_packet(data, type, 0xfe, 0, data->interface, 1);
186
187 libusb_release_interface(data->devh, data->interface);
188 }
189
190 static void handle_kobil(struct usbdev_data *data, struct blob_attr **tb)
191 {
192 int type = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN;
193
194 detach_driver(data);
195 send_control_packet(data, type, 0x88, 0, 0, 8);
196 }
197
198 static void handle_sequans(struct usbdev_data *data, struct blob_attr **tb)
199 {
200 int type = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE;
201 send_control_packet(data, type, LIBUSB_REQUEST_SET_INTERFACE, 2, 0, 0);
202 }
203
204 static void mobile_action_interrupt_msg(struct usbdev_data *data, void *msg, int n_in)
205 {
206 unsigned char *buf = alloca(8);
207 int ep_out = 0x02, ep_in = 0x81;
208 int transferred;
209 int i;
210
211 if (msg)
212 libusb_interrupt_transfer(data->devh, ep_out, msg, 8, &transferred, 1000);
213 for (i = 0; i < n_in; i++)
214 libusb_interrupt_transfer(data->devh, ep_in, buf, 8, &transferred, 1000);
215 }
216
217 static void handle_mobile_action(struct usbdev_data *data, struct blob_attr **tb)
218 {
219 int type = LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE;
220 char *msg[] = {
221 "\xb0\x04\x00\x00\x02\x90\x26\x86",
222 "\x37\x01\xfe\xdb\xc1\x33\x1f\x83",
223 "\x37\x0e\xb5\x9d\x3b\x8a\x91\x51",
224 "\x34\x87\xba\x0d\xfc\x8a\x91\x51",
225 "\x37\x01\xfe\xdb\xc1\x33\x1f\x83",
226 "\x37\x0e\xb5\x9d\x3b\x8a\x91\x51",
227 "\x34\x87\xba\x0d\xfc\x8a\x91\x51",
228 "\x33\x04\xfe\x00\xf4\x6c\x1f\xf0",
229 "\x32\x07\xfe\xf0\x29\xb9\x3a\xf0"
230 };
231 int i;
232
233 for (i = 0; i < 2; i++)
234 libusb_control_transfer(data->devh, type, 0x09, 0x0300, 0, (void *) msg[0], 8, 1000);
235 mobile_action_interrupt_msg(data, NULL, 2);
236 mobile_action_interrupt_msg(data, msg[1], 1);
237 mobile_action_interrupt_msg(data, msg[2], 1);
238 mobile_action_interrupt_msg(data, msg[3], 63);
239 mobile_action_interrupt_msg(data, msg[4], 1);
240 mobile_action_interrupt_msg(data, msg[5], 1);
241 mobile_action_interrupt_msg(data, msg[6], 73);
242 mobile_action_interrupt_msg(data, msg[7], 1);
243 mobile_action_interrupt_msg(data, msg[8], 1);
244 }
245
246 static void handle_cisco(struct usbdev_data *data, struct blob_attr **tb)
247 {
248 static struct msg_entry msgs[] = {
249 {
250 "\x55\x53\x42\x43\xf8\x3b\xcd\x81\x00\x02\x00\x00\x80\x00\x0a\xfd"
251 "\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
252 }, {
253 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
254 "\x00\x00\x00\x07\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
255 }, {
256 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
257 "\x00\x01\x00\x07\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
258 }, {
259 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
260 "\x00\x02\x00\x23\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
261 }, {
262 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
263 "\x00\x03\x00\x23\x82\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
264 }, {
265 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
266 "\x00\x02\x00\x26\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
267 }, {
268 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
269 "\x00\x03\x00\x26\xc8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
270 }, {
271 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
272 "\x00\x00\x10\x73\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
273 }, {
274 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
275 "\x00\x02\x00\x24\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
276 }, {
277 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
278 "\x00\x03\x00\x24\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
279 }, {
280 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
281 "\x00\x01\x10\x73\x24\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
282 }
283
284 };
285
286 detach_driver(data);
287 data->need_response = true;
288 send_messages(data, msgs, ARRAY_SIZE(msgs));
289 }
290
291 static void set_alt_setting(struct usbdev_data *data, int setting)
292 {
293 if (libusb_claim_interface(data->devh, data->interface))
294 return;
295
296 libusb_set_interface_alt_setting(data->devh, data->interface, setting);
297 libusb_release_interface(data->devh, data->interface);
298 }
299
300 enum {
301 MODE_GENERIC,
302 MODE_HUAWEI,
303 MODE_SIERRA,
304 MODE_SONY,
305 MODE_QISDA,
306 MODE_GCT,
307 MODE_KOBIL,
308 MODE_SEQUANS,
309 MODE_MOBILE_ACTION,
310 MODE_CISCO,
311 __MODE_MAX
312 };
313
314 static const struct {
315 const char *name;
316 void (*cb)(struct usbdev_data *data, struct blob_attr **tb);
317 } modeswitch_cb[__MODE_MAX] = {
318 [MODE_GENERIC] = { "Generic", handle_generic },
319 [MODE_HUAWEI] = { "Huawei", handle_huawei },
320 [MODE_SIERRA] = { "Sierra", handle_sierra },
321 [MODE_SONY] = { "Sony", handle_sony },
322 [MODE_QISDA] = { "Qisda", handle_qisda },
323 [MODE_GCT] = { "GCT", handle_gct },
324 [MODE_KOBIL] = { "Kobil", handle_kobil },
325 [MODE_SEQUANS] = { "Sequans", handle_sequans },
326 [MODE_MOBILE_ACTION] = { "MobileAction", handle_mobile_action },
327 [MODE_CISCO] = { "Cisco", handle_cisco },
328 };
329
330 void handle_switch(struct usbdev_data *data)
331 {
332 static const struct blobmsg_policy data_policy[__DATA_MAX] = {
333 [DATA_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_STRING },
334 [DATA_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_ARRAY },
335 [DATA_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_INT32 },
336 [DATA_MSG_EP] = { .name = "msg_endpoint", .type = BLOBMSG_TYPE_INT32 },
337 [DATA_RES_EP] = { .name = "response_endpoint", .type = BLOBMSG_TYPE_INT32 },
338 [DATA_RESPONSE] = { .name = "response", .type = BLOBMSG_TYPE_BOOL },
339 [DATA_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_INT32 },
340 [DATA_ALT] = { .name = "alt", .type = BLOBMSG_TYPE_INT32 },
341 };
342 struct blob_attr *tb[__DATA_MAX];
343 int mode = MODE_GENERIC;
344
345 blobmsg_parse(data_policy, __DATA_MAX, tb, blobmsg_data(data->info), blobmsg_data_len(data->info));
346
347 if (tb[DATA_INTERFACE])
348 data->interface = blobmsg_get_u32(tb[DATA_INTERFACE]);
349
350 if (tb[DATA_MSG_EP])
351 data->msg_endpoint = blobmsg_get_u32(tb[DATA_MSG_EP]);
352
353 if (tb[DATA_RES_EP])
354 data->response_endpoint = blobmsg_get_u32(tb[DATA_RES_EP]);
355
356 if (tb[DATA_RELEASE_DELAY])
357 data->release_delay = blobmsg_get_u32(tb[DATA_RELEASE_DELAY]);
358
359 if (tb[DATA_RESPONSE])
360 data->need_response = blobmsg_get_bool(tb[DATA_RESPONSE]);
361
362 if (tb[DATA_MODE]) {
363 const char *modestr;
364 int i;
365
366 modestr = blobmsg_data(tb[DATA_MODE]);
367 for (i = 0; i < __MODE_MAX; i++) {
368 if (strcmp(modeswitch_cb[i].name, modestr) != 0)
369 continue;
370
371 mode = i;
372 break;
373 }
374 }
375
376 modeswitch_cb[mode].cb(data, tb);
377
378 if (tb[DATA_CONFIG]) {
379 int config, config_new;
380
381 config_new = blobmsg_get_u32(tb[DATA_CONFIG]);
382 if (libusb_get_configuration(data->devh, &config) ||
383 config != config_new)
384 libusb_set_configuration(data->devh, config_new);
385 }
386
387 if (tb[DATA_ALT]) {
388 int new = blobmsg_get_u32(tb[DATA_ALT]);
389 set_alt_setting(data, new);
390 }
391 }