add a mbim eject handler
[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_huaweinew(struct usbdev_data *data, struct blob_attr **tb)
141 {
142 static struct msg_entry msgs[] = {
143 {
144 "\x55\x53\x42\x43\x12\x34\x56\x78\x00\x00\x00\x00\x00\x00\x00\x11"
145 "\x06\x20\x00\x00\x01\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
146 }
147 };
148
149 detach_driver(data);
150 data->need_response = false;
151 send_messages(data, msgs, ARRAY_SIZE(msgs));
152 }
153
154 static void handle_standardeject(struct usbdev_data *data, struct blob_attr **tb)
155 {
156 static struct msg_entry msgs[] = {
157 {
158 "\x55\x53\x42\x43\x12\x34\x56\x78\x00\x00\x00\x00\x00\x00\x06\x1e"
159 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
160 }, {
161 "\x55\x53\x42\x43\x12\x34\x56\x79\x00\x00\x00\x00\x00\x00\x06\x1b"
162 "\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
163 }
164 };
165
166 detach_driver(data);
167 data->need_response = true;
168 send_messages(data, msgs, ARRAY_SIZE(msgs));
169 }
170
171 static void handle_sierra(struct usbdev_data *data, struct blob_attr **tb)
172 {
173 int type = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE;
174 send_control_packet(data, type, LIBUSB_REQUEST_SET_INTERFACE, 1, 0, 0);
175 }
176
177 static void handle_sony(struct usbdev_data *data, struct blob_attr **tb)
178 {
179 int type = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN;
180 int i;
181
182 detach_driver(data);
183 send_control_packet(data, type, 0x11, 2, 0, 3);
184
185 libusb_close(data->devh);
186 sleep(5);
187
188 for (i = 0; i < 25; i++) {
189 data->devh = libusb_open_device_with_vid_pid(usb,
190 data->desc.idVendor, data->desc.idProduct);
191 if (data->devh)
192 break;
193 }
194
195 send_control_packet(data, type, 0x11, 2, 0, 3);
196 }
197
198 static void handle_qisda(struct usbdev_data *data, struct blob_attr **tb)
199 {
200 static unsigned char buffer[] = "\x05\x8c\x04\x08\xa0\xee\x20\x00\x5c\x01\x04\x08\x98\xcd\xea\xbf";
201 int type = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE;
202
203 libusb_control_transfer(data->devh, type, 0x04, 0, 0, buffer, 16, 1000);
204 }
205
206 static void handle_gct(struct usbdev_data *data, struct blob_attr **tb)
207 {
208 int type = LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_IN;
209
210 detach_driver(data);
211
212 if (libusb_claim_interface(data->devh, data->interface))
213 return;
214
215 send_control_packet(data, type, 0xa0, 0, data->interface, 1);
216 send_control_packet(data, type, 0xfe, 0, data->interface, 1);
217
218 libusb_release_interface(data->devh, data->interface);
219 }
220
221 static void handle_kobil(struct usbdev_data *data, struct blob_attr **tb)
222 {
223 int type = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN;
224
225 detach_driver(data);
226 send_control_packet(data, type, 0x88, 0, 0, 8);
227 }
228
229 static void handle_sequans(struct usbdev_data *data, struct blob_attr **tb)
230 {
231 int type = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE;
232 send_control_packet(data, type, LIBUSB_REQUEST_SET_INTERFACE, 2, 0, 0);
233 }
234
235 static void mobile_action_interrupt_msg(struct usbdev_data *data, void *msg, int n_in)
236 {
237 unsigned char *buf = alloca(8);
238 int ep_out = 0x02, ep_in = 0x81;
239 int transferred;
240 int i;
241
242 if (msg)
243 libusb_interrupt_transfer(data->devh, ep_out, msg, 8, &transferred, 1000);
244 for (i = 0; i < n_in; i++)
245 libusb_interrupt_transfer(data->devh, ep_in, buf, 8, &transferred, 1000);
246 }
247
248 static void handle_mobile_action(struct usbdev_data *data, struct blob_attr **tb)
249 {
250 int type = LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE;
251 char *msg[] = {
252 "\xb0\x04\x00\x00\x02\x90\x26\x86",
253 "\x37\x01\xfe\xdb\xc1\x33\x1f\x83",
254 "\x37\x0e\xb5\x9d\x3b\x8a\x91\x51",
255 "\x34\x87\xba\x0d\xfc\x8a\x91\x51",
256 "\x37\x01\xfe\xdb\xc1\x33\x1f\x83",
257 "\x37\x0e\xb5\x9d\x3b\x8a\x91\x51",
258 "\x34\x87\xba\x0d\xfc\x8a\x91\x51",
259 "\x33\x04\xfe\x00\xf4\x6c\x1f\xf0",
260 "\x32\x07\xfe\xf0\x29\xb9\x3a\xf0"
261 };
262 int i;
263
264 for (i = 0; i < 2; i++)
265 libusb_control_transfer(data->devh, type, 0x09, 0x0300, 0, (void *) msg[0], 8, 1000);
266 mobile_action_interrupt_msg(data, NULL, 2);
267 mobile_action_interrupt_msg(data, msg[1], 1);
268 mobile_action_interrupt_msg(data, msg[2], 1);
269 mobile_action_interrupt_msg(data, msg[3], 63);
270 mobile_action_interrupt_msg(data, msg[4], 1);
271 mobile_action_interrupt_msg(data, msg[5], 1);
272 mobile_action_interrupt_msg(data, msg[6], 73);
273 mobile_action_interrupt_msg(data, msg[7], 1);
274 mobile_action_interrupt_msg(data, msg[8], 1);
275 }
276
277 static void handle_cisco(struct usbdev_data *data, struct blob_attr **tb)
278 {
279 static struct msg_entry msgs[] = {
280 {
281 "\x55\x53\x42\x43\xf8\x3b\xcd\x81\x00\x02\x00\x00\x80\x00\x0a\xfd"
282 "\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
283 }, {
284 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
285 "\x00\x00\x00\x07\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
286 }, {
287 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
288 "\x00\x01\x00\x07\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
289 }, {
290 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
291 "\x00\x02\x00\x23\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
292 }, {
293 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
294 "\x00\x03\x00\x23\x82\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
295 }, {
296 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
297 "\x00\x02\x00\x26\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
298 }, {
299 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
300 "\x00\x03\x00\x26\xc8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
301 }, {
302 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
303 "\x00\x00\x10\x73\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
304 }, {
305 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
306 "\x00\x02\x00\x24\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
307 }, {
308 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
309 "\x00\x03\x00\x24\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
310 }, {
311 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
312 "\x00\x01\x10\x73\x24\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
313 }
314
315 };
316
317 detach_driver(data);
318 data->need_response = true;
319 send_messages(data, msgs, ARRAY_SIZE(msgs));
320 }
321
322 static void handle_mbim(struct usbdev_data *data, struct blob_attr **tb)
323 {
324 int j;
325
326 if (data->desc.bNumConfigurations < 2)
327 return;
328
329 for (j = 0; j < data->desc.bNumConfigurations; j++) {
330 struct libusb_config_descriptor *config;
331 int i;
332
333 libusb_get_config_descriptor(data->dev, j, &config);
334
335 for (i = 0; i < config->bNumInterfaces; i++) {
336 if (config->interface[i].altsetting[0].bInterfaceClass == 2) {
337 if (config->interface[i].altsetting[0].bInterfaceSubClass == 0x0e) {
338 struct libusb_config_descriptor *active;
339 int count = 5;
340
341 libusb_get_active_config_descriptor(data->dev, &active);
342 if (active->bConfigurationValue == config->bConfigurationValue)
343 return;
344 while ((libusb_set_configuration(data->devh, config->bConfigurationValue) < 0) && --count)
345 libusb_detach_kernel_driver(data->devh, active->interface[0].altsetting[0].bInterfaceNumber);
346
347 libusb_free_config_descriptor(config);
348 return;
349 }
350 }
351 }
352
353 libusb_free_config_descriptor(config);
354 }
355 }
356
357 static void set_alt_setting(struct usbdev_data *data, int setting)
358 {
359 if (libusb_claim_interface(data->devh, data->interface))
360 return;
361
362 libusb_set_interface_alt_setting(data->devh, data->interface, setting);
363 libusb_release_interface(data->devh, data->interface);
364 }
365
366 enum {
367 MODE_GENERIC,
368 MODE_HUAWEI,
369 MODE_HUAWEINEW,
370 MODE_SIERRA,
371 MODE_STDEJECT,
372 MODE_SONY,
373 MODE_QISDA,
374 MODE_GCT,
375 MODE_KOBIL,
376 MODE_SEQUANS,
377 MODE_MOBILE_ACTION,
378 MODE_CISCO,
379 MODE_MBIM,
380 __MODE_MAX
381 };
382
383 static const struct {
384 const char *name;
385 void (*cb)(struct usbdev_data *data, struct blob_attr **tb);
386 } modeswitch_cb[__MODE_MAX] = {
387 [MODE_GENERIC] = { "Generic", handle_generic },
388 [MODE_STDEJECT] = { "StandardEject", handle_standardeject },
389 [MODE_HUAWEI] = { "Huawei", handle_huawei },
390 [MODE_HUAWEINEW] = { "HuaweiNew", handle_huaweinew },
391 [MODE_SIERRA] = { "Sierra", handle_sierra },
392 [MODE_SONY] = { "Sony", handle_sony },
393 [MODE_QISDA] = { "Qisda", handle_qisda },
394 [MODE_GCT] = { "GCT", handle_gct },
395 [MODE_KOBIL] = { "Kobil", handle_kobil },
396 [MODE_SEQUANS] = { "Sequans", handle_sequans },
397 [MODE_MOBILE_ACTION] = { "MobileAction", handle_mobile_action },
398 [MODE_CISCO] = { "Cisco", handle_cisco },
399 [MODE_MBIM] = { "MBIM", handle_mbim },
400 };
401
402 void handle_switch(struct usbdev_data *data)
403 {
404 static const struct blobmsg_policy data_policy[__DATA_MAX] = {
405 [DATA_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_STRING },
406 [DATA_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_ARRAY },
407 [DATA_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_INT32 },
408 [DATA_MSG_EP] = { .name = "msg_endpoint", .type = BLOBMSG_TYPE_INT32 },
409 [DATA_RES_EP] = { .name = "response_endpoint", .type = BLOBMSG_TYPE_INT32 },
410 [DATA_RESPONSE] = { .name = "response", .type = BLOBMSG_TYPE_BOOL },
411 [DATA_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_INT32 },
412 [DATA_ALT] = { .name = "alt", .type = BLOBMSG_TYPE_INT32 },
413 };
414 struct blob_attr *tb[__DATA_MAX];
415 int mode = MODE_GENERIC;
416
417 blobmsg_parse(data_policy, __DATA_MAX, tb, blobmsg_data(data->info), blobmsg_data_len(data->info));
418
419 if (tb[DATA_INTERFACE])
420 data->interface = blobmsg_get_u32(tb[DATA_INTERFACE]);
421
422 if (tb[DATA_MSG_EP])
423 data->msg_endpoint = blobmsg_get_u32(tb[DATA_MSG_EP]);
424
425 if (tb[DATA_RES_EP])
426 data->response_endpoint = blobmsg_get_u32(tb[DATA_RES_EP]);
427
428 if (tb[DATA_RELEASE_DELAY])
429 data->release_delay = blobmsg_get_u32(tb[DATA_RELEASE_DELAY]);
430
431 if (tb[DATA_RESPONSE])
432 data->need_response = blobmsg_get_bool(tb[DATA_RESPONSE]);
433
434 if (tb[DATA_MODE]) {
435 const char *modestr;
436 int i;
437
438 modestr = blobmsg_data(tb[DATA_MODE]);
439 for (i = 0; i < __MODE_MAX; i++) {
440 if (strcmp(modeswitch_cb[i].name, modestr) != 0)
441 continue;
442
443 mode = i;
444 break;
445 }
446 }
447
448 modeswitch_cb[mode].cb(data, tb);
449
450 if (tb[DATA_CONFIG]) {
451 int config, config_new;
452
453 config_new = blobmsg_get_u32(tb[DATA_CONFIG]);
454 if (libusb_get_configuration(data->devh, &config) ||
455 config != config_new)
456 libusb_set_configuration(data->devh, config_new);
457 }
458
459 if (tb[DATA_ALT]) {
460 int new = blobmsg_get_u32(tb[DATA_ALT]);
461 set_alt_setting(data, new);
462 }
463 }