19 static void detach_driver(struct usbdev_data
*data
)
21 libusb_detach_kernel_driver(data
->devh
, data
->interface
);
29 static int send_msg(struct usbdev_data
*data
, struct msg_entry
*msg
)
33 return libusb_bulk_transfer(data
->devh
, data
->msg_endpoint
,
34 (void *) msg
->data
, msg
->len
,
38 static int read_response(struct usbdev_data
*data
, int len
)
46 ret
= libusb_bulk_transfer(data
->devh
, data
->response_endpoint
,
47 buf
, len
, &transferred
, 3000);
48 libusb_bulk_transfer(data
->devh
, data
->response_endpoint
,
49 buf
, 13, &transferred
, 100);
53 static void send_messages(struct usbdev_data
*data
, struct msg_entry
*msg
, int n_msg
)
57 libusb_claim_interface(data
->devh
, data
->interface
);
58 libusb_clear_halt(data
->devh
, data
->msg_endpoint
);
60 for (i
= 0; i
< n_msg
; i
++) {
61 if (send_msg(data
, &msg
[i
])) {
62 fprintf(stderr
, "Failed to send switch message\n");
66 if (!data
->need_response
)
69 if (!memcmp(msg
[i
].data
, "\x55\x53\x42\x43", 4))
74 if (read_response(data
, len
))
78 libusb_clear_halt(data
->devh
, data
->msg_endpoint
);
79 libusb_clear_halt(data
->devh
, data
->response_endpoint
);
83 if (data
->release_delay
)
84 usleep(data
->release_delay
* 1000);
86 libusb_release_interface(data
->devh
, data
->interface
);
90 static void send_config_messages(struct usbdev_data
*data
, struct blob_attr
*attr
)
92 struct blob_attr
*cur
;
94 struct msg_entry
*msg
;
96 blobmsg_for_each_attr(cur
, attr
, rem
)
99 msg
= alloca(n_msg
* sizeof(*msg
));
101 blobmsg_for_each_attr(cur
, attr
, rem
) {
104 if (blobmsg_type(cur
) != BLOBMSG_TYPE_INT32
) {
105 fprintf(stderr
, "Invalid data in message list\n");
109 msg_nr
= blobmsg_get_u32(cur
);
110 if (msg_nr
>= n_messages
) {
111 fprintf(stderr
, "Message index out of range!\n");
115 msg
[n_msg
].data
= messages
[msg_nr
];
116 msg
[n_msg
++].len
= message_len
[msg_nr
];
119 send_messages(data
, msg
, n_msg
);
122 static void handle_generic(struct usbdev_data
*data
, struct blob_attr
**tb
)
125 send_config_messages(data
, tb
[DATA_MSG
]);
128 static void send_control_packet(struct usbdev_data
*data
, uint8_t type
, uint8_t req
,
129 uint16_t val
, uint16_t idx
, int len
)
131 unsigned char *buffer
= alloca(len
? len
: 1);
133 libusb_control_transfer(data
->devh
, type
, req
, val
, idx
, buffer
, len
, 1000);
136 static void handle_huawei(struct usbdev_data
*data
, struct blob_attr
**tb
)
138 int type
= LIBUSB_REQUEST_TYPE_STANDARD
| LIBUSB_RECIPIENT_DEVICE
;
139 send_control_packet(data
, type
, LIBUSB_REQUEST_SET_FEATURE
, 1, 0, 0);
142 static void handle_huaweinew(struct usbdev_data
*data
, struct blob_attr
**tb
)
144 static struct msg_entry msgs
[] = {
146 "\x55\x53\x42\x43\x12\x34\x56\x78\x00\x00\x00\x00\x00\x00\x00\x11"
147 "\x06\x20\x00\x00\x01\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
152 data
->need_response
= false;
153 send_messages(data
, msgs
, ARRAY_SIZE(msgs
));
156 static void handle_option(struct usbdev_data
*data
, struct blob_attr
**tb
)
158 static struct msg_entry msgs
[] = {
160 "\x55\x53\x42\x43\x12\x34\x56\x78\x00\x00\x00\x00\x00\x00\x06\x01"
161 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
166 data
->need_response
= false;
167 send_messages(data
, msgs
, ARRAY_SIZE(msgs
));
170 static void handle_standardeject(struct usbdev_data
*data
, struct blob_attr
**tb
)
172 static struct msg_entry msgs
[] = {
174 "\x55\x53\x42\x43\x12\x34\x56\x78\x00\x00\x00\x00\x00\x00\x06\x1e"
175 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
177 "\x55\x53\x42\x43\x12\x34\x56\x79\x00\x00\x00\x00\x00\x00\x06\x1b"
178 "\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
180 "\x55\x53\x42\x43\x12\x34\x56\x78\x00\x00\x00\x00\x00\x01\x06\x1e"
181 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
183 "\x55\x53\x42\x43\x12\x34\x56\x79\x00\x00\x00\x00\x00\x01\x06\x1b"
184 "\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
189 data
->need_response
= true;
190 send_messages(data
, msgs
, ARRAY_SIZE(msgs
));
193 static void handle_sierra(struct usbdev_data
*data
, struct blob_attr
**tb
)
195 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
;
196 send_control_packet(data
, type
, LIBUSB_REQUEST_SET_INTERFACE
, 1, 0, 0);
199 static void handle_sony(struct usbdev_data
*data
, struct blob_attr
**tb
)
201 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_ENDPOINT_IN
;
205 send_control_packet(data
, type
, 0x11, 2, 0, 3);
207 libusb_close(data
->devh
);
210 for (i
= 0; i
< 25; i
++) {
211 data
->devh
= libusb_open_device_with_vid_pid(usb
,
212 data
->desc
.idVendor
, data
->desc
.idProduct
);
217 send_control_packet(data
, type
, 0x11, 2, 0, 3);
220 static void handle_qisda(struct usbdev_data
*data
, struct blob_attr
**tb
)
222 static unsigned char buffer
[] = "\x05\x8c\x04\x08\xa0\xee\x20\x00\x5c\x01\x04\x08\x98\xcd\xea\xbf";
223 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
;
225 libusb_control_transfer(data
->devh
, type
, 0x04, 0, 0, buffer
, 16, 1000);
228 static void handle_gct(struct usbdev_data
*data
, struct blob_attr
**tb
)
230 int type
= LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_RECIPIENT_INTERFACE
| LIBUSB_ENDPOINT_IN
;
234 if (libusb_claim_interface(data
->devh
, data
->interface
))
237 send_control_packet(data
, type
, 0xa0, 0, data
->interface
, 1);
238 send_control_packet(data
, type
, 0xfe, 0, data
->interface
, 1);
240 libusb_release_interface(data
->devh
, data
->interface
);
243 static void handle_kobil(struct usbdev_data
*data
, struct blob_attr
**tb
)
245 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
| LIBUSB_ENDPOINT_IN
;
248 send_control_packet(data
, type
, 0x88, 0, 0, 8);
251 static void handle_sequans(struct usbdev_data
*data
, struct blob_attr
**tb
)
253 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
;
254 send_control_packet(data
, type
, LIBUSB_REQUEST_SET_INTERFACE
, 2, 0, 0);
257 static void mobile_action_interrupt_msg(struct usbdev_data
*data
, void *msg
, int n_in
)
259 unsigned char *buf
= alloca(8);
260 int ep_out
= 0x02, ep_in
= 0x81;
265 libusb_interrupt_transfer(data
->devh
, ep_out
, msg
, 8, &transferred
, 1000);
266 for (i
= 0; i
< n_in
; i
++)
267 libusb_interrupt_transfer(data
->devh
, ep_in
, buf
, 8, &transferred
, 1000);
270 static void handle_mobile_action(struct usbdev_data
*data
, struct blob_attr
**tb
)
272 int type
= LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_RECIPIENT_INTERFACE
;
274 "\xb0\x04\x00\x00\x02\x90\x26\x86",
275 "\x37\x01\xfe\xdb\xc1\x33\x1f\x83",
276 "\x37\x0e\xb5\x9d\x3b\x8a\x91\x51",
277 "\x34\x87\xba\x0d\xfc\x8a\x91\x51",
278 "\x37\x01\xfe\xdb\xc1\x33\x1f\x83",
279 "\x37\x0e\xb5\x9d\x3b\x8a\x91\x51",
280 "\x34\x87\xba\x0d\xfc\x8a\x91\x51",
281 "\x33\x04\xfe\x00\xf4\x6c\x1f\xf0",
282 "\x32\x07\xfe\xf0\x29\xb9\x3a\xf0"
286 for (i
= 0; i
< 2; i
++)
287 libusb_control_transfer(data
->devh
, type
, 0x09, 0x0300, 0, (void *) msg
[0], 8, 1000);
288 mobile_action_interrupt_msg(data
, NULL
, 2);
289 mobile_action_interrupt_msg(data
, msg
[1], 1);
290 mobile_action_interrupt_msg(data
, msg
[2], 1);
291 mobile_action_interrupt_msg(data
, msg
[3], 63);
292 mobile_action_interrupt_msg(data
, msg
[4], 1);
293 mobile_action_interrupt_msg(data
, msg
[5], 1);
294 mobile_action_interrupt_msg(data
, msg
[6], 73);
295 mobile_action_interrupt_msg(data
, msg
[7], 1);
296 mobile_action_interrupt_msg(data
, msg
[8], 1);
299 static void handle_cisco(struct usbdev_data
*data
, struct blob_attr
**tb
)
301 static struct msg_entry msgs
[] = {
303 "\x55\x53\x42\x43\xf8\x3b\xcd\x81\x00\x02\x00\x00\x80\x00\x0a\xfd"
304 "\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
306 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
307 "\x00\x00\x00\x07\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
309 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
310 "\x00\x01\x00\x07\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
312 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
313 "\x00\x02\x00\x23\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
315 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
316 "\x00\x03\x00\x23\x82\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
318 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
319 "\x00\x02\x00\x26\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
321 "\x55\x53\x42\x43\x98\x43\x00\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
322 "\x00\x03\x00\x26\xc8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
324 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
325 "\x00\x00\x10\x73\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
327 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x02\x00\x00\x80\x00\x0a\xfd"
328 "\x00\x02\x00\x24\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 31
330 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
331 "\x00\x03\x00\x24\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
333 "\x55\x53\x42\x43\xd8\x4c\x04\x82\x00\x00\x00\x00\x00\x00\x0a\xfd"
334 "\x00\x01\x10\x73\x24\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 31
340 data
->need_response
= true;
341 send_messages(data
, msgs
, ARRAY_SIZE(msgs
));
344 static void handle_mbim(struct usbdev_data
*data
, struct blob_attr
**tb
)
348 if (data
->desc
.bNumConfigurations
< 2)
351 for (j
= 0; j
< data
->desc
.bNumConfigurations
; j
++) {
352 struct libusb_config_descriptor
*config
;
355 libusb_get_config_descriptor(data
->dev
, j
, &config
);
357 for (i
= 0; i
< config
->bNumInterfaces
; i
++) {
358 if (config
->interface
[i
].altsetting
[0].bInterfaceClass
== 2) {
359 if (config
->interface
[i
].altsetting
[0].bInterfaceSubClass
== 0x0e) {
360 struct libusb_config_descriptor
*active
;
363 libusb_get_active_config_descriptor(data
->dev
, &active
);
364 if (active
->bConfigurationValue
== config
->bConfigurationValue
)
366 while ((libusb_set_configuration(data
->devh
, config
->bConfigurationValue
) < 0) && --count
)
367 libusb_detach_kernel_driver(data
->devh
, active
->interface
[0].altsetting
[0].bInterfaceNumber
);
369 libusb_free_config_descriptor(config
);
375 libusb_free_config_descriptor(config
);
379 static void handle_quanta(struct usbdev_data
*data
, struct blob_attr
**tb
)
381 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
| LIBUSB_ENDPOINT_IN
;
384 send_control_packet(data
, type
, 0xff, 0, 0, 8);
387 static void handle_blackberry(struct usbdev_data
*data
, struct blob_attr
**tb
)
389 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
| LIBUSB_ENDPOINT_IN
;
392 send_control_packet(data
, type
, 0xb1, 0x0000, 0, 8);
393 send_control_packet(data
, type
, 0xa9, 0x000e, 0, 8);
396 static void handle_pantech(struct usbdev_data
*data
, struct blob_attr
**tb
)
398 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
| LIBUSB_ENDPOINT_OUT
;
401 if (tb
[DATA_MODEVAL
])
402 val
= blobmsg_get_u32(tb
[DATA_MODEVAL
]);
405 send_control_packet(data
, type
, 0x70, val
, 0, 0);
408 static void set_alt_setting(struct usbdev_data
*data
, int setting
)
410 if (libusb_claim_interface(data
->devh
, data
->interface
))
413 libusb_set_interface_alt_setting(data
->devh
, data
->interface
, setting
);
414 libusb_release_interface(data
->devh
, data
->interface
);
438 static const struct {
440 void (*cb
)(struct usbdev_data
*data
, struct blob_attr
**tb
);
441 } modeswitch_cb
[__MODE_MAX
] = {
442 [MODE_GENERIC
] = { "Generic", handle_generic
},
443 [MODE_STDEJECT
] = { "StandardEject", handle_standardeject
},
444 [MODE_HUAWEI
] = { "Huawei", handle_huawei
},
445 [MODE_HUAWEINEW
] = { "HuaweiNew", handle_huaweinew
},
446 [MODE_SIERRA
] = { "Sierra", handle_sierra
},
447 [MODE_SONY
] = { "Sony", handle_sony
},
448 [MODE_QISDA
] = { "Qisda", handle_qisda
},
449 [MODE_GCT
] = { "GCT", handle_gct
},
450 [MODE_KOBIL
] = { "Kobil", handle_kobil
},
451 [MODE_SEQUANS
] = { "Sequans", handle_sequans
},
452 [MODE_MOBILE_ACTION
] = { "MobileAction", handle_mobile_action
},
453 [MODE_CISCO
] = { "Cisco", handle_cisco
},
454 [MODE_MBIM
] = { "MBIM", handle_mbim
},
455 [MODE_OPTION
] = { "Option", handle_option
},
456 [MODE_QUANTA
] = { "Quanta", handle_quanta
},
457 [MODE_BLACKBERRY
] = { "Blackberry", handle_blackberry
},
458 [MODE_PANTECH
] = { "Pantech", handle_pantech
},
461 void handle_switch(struct usbdev_data
*data
)
463 static const struct blobmsg_policy data_policy
[__DATA_MAX
] = {
464 [DATA_MODE
] = { .name
= "mode", .type
= BLOBMSG_TYPE_STRING
},
465 [DATA_MODEVAL
] = { .name
= "modeval", .type
= BLOBMSG_TYPE_INT32
},
466 [DATA_MSG
] = { .name
= "msg", .type
= BLOBMSG_TYPE_ARRAY
},
467 [DATA_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_INT32
},
468 [DATA_MSG_EP
] = { .name
= "msg_endpoint", .type
= BLOBMSG_TYPE_INT32
},
469 [DATA_RES_EP
] = { .name
= "response_endpoint", .type
= BLOBMSG_TYPE_INT32
},
470 [DATA_RESPONSE
] = { .name
= "response", .type
= BLOBMSG_TYPE_BOOL
},
471 [DATA_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_INT32
},
472 [DATA_ALT
] = { .name
= "alt", .type
= BLOBMSG_TYPE_INT32
},
473 [DATA_DEV_CLASS
] = { .name
= "t_class", .type
= BLOBMSG_TYPE_INT32
},
475 struct blob_attr
*tb
[__DATA_MAX
];
476 int mode
= MODE_GENERIC
;
479 blobmsg_parse(data_policy
, __DATA_MAX
, tb
, blobmsg_data(data
->info
), blobmsg_data_len(data
->info
));
481 if (tb
[DATA_DEV_CLASS
])
482 t_class
= blobmsg_get_u32(tb
[DATA_DEV_CLASS
]);
484 if (tb
[DATA_INTERFACE
])
485 data
->interface
= blobmsg_get_u32(tb
[DATA_INTERFACE
]);
488 data
->msg_endpoint
= blobmsg_get_u32(tb
[DATA_MSG_EP
]);
491 data
->response_endpoint
= blobmsg_get_u32(tb
[DATA_RES_EP
]);
493 if (tb
[DATA_RELEASE_DELAY
])
494 data
->release_delay
= blobmsg_get_u32(tb
[DATA_RELEASE_DELAY
]);
496 if (tb
[DATA_RESPONSE
])
497 data
->need_response
= blobmsg_get_bool(tb
[DATA_RESPONSE
]);
499 if (t_class
> 0 && data
->dev_class
!= t_class
)
506 modestr
= blobmsg_data(tb
[DATA_MODE
]);
507 for (i
= 0; i
< __MODE_MAX
; i
++) {
508 if (strcmp(modeswitch_cb
[i
].name
, modestr
) != 0)
516 modeswitch_cb
[mode
].cb(data
, tb
);
518 if (tb
[DATA_CONFIG
]) {
519 int config
, config_new
;
521 config_new
= blobmsg_get_u32(tb
[DATA_CONFIG
]);
522 if (libusb_get_configuration(data
->devh
, &config
) ||
523 config
!= config_new
) {
524 libusb_set_configuration(data
->devh
, 0);
526 libusb_set_configuration(data
->devh
, config_new
);
531 int new = blobmsg_get_u32(tb
[DATA_ALT
]);
532 set_alt_setting(data
, new);