17 static void detach_driver(struct usbdev_data
*data
)
19 libusb_detach_kernel_driver(data
->devh
, data
->interface
);
27 static int send_msg(struct usbdev_data
*data
, struct msg_entry
*msg
)
31 return libusb_bulk_transfer(data
->devh
, data
->msg_endpoint
,
32 (void *) msg
->data
, msg
->len
,
36 static int read_response(struct usbdev_data
*data
, int 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);
51 static void send_messages(struct usbdev_data
*data
, struct msg_entry
*msg
, int n_msg
)
55 libusb_claim_interface(data
->devh
, data
->interface
);
56 libusb_clear_halt(data
->devh
, data
->msg_endpoint
);
58 for (i
= 0; i
< n_msg
; i
++) {
59 if (send_msg(data
, &msg
[i
])) {
60 fprintf(stderr
, "Failed to send switch message\n");
64 if (!data
->need_response
)
67 if (!memcmp(msg
[i
].data
, "\x55\x53\x42\x43", 4))
72 if (read_response(data
, len
))
76 libusb_clear_halt(data
->devh
, data
->msg_endpoint
);
77 libusb_clear_halt(data
->devh
, data
->response_endpoint
);
81 if (data
->release_delay
)
82 usleep(data
->release_delay
* 1000);
84 libusb_release_interface(data
->devh
, data
->interface
);
88 static void send_config_messages(struct usbdev_data
*data
, struct blob_attr
*attr
)
90 struct blob_attr
*cur
;
92 struct msg_entry
*msg
;
94 blobmsg_for_each_attr(cur
, attr
, rem
)
97 msg
= alloca(n_msg
* sizeof(*msg
));
99 blobmsg_for_each_attr(cur
, attr
, rem
) {
102 if (blobmsg_type(cur
) != BLOBMSG_TYPE_INT32
) {
103 fprintf(stderr
, "Invalid data in message list\n");
107 msg_nr
= blobmsg_get_u32(cur
);
108 if (msg_nr
>= n_messages
) {
109 fprintf(stderr
, "Message index out of range!\n");
113 msg
[n_msg
].data
= messages
[msg_nr
];
114 msg
[n_msg
++].len
= message_len
[msg_nr
];
117 send_messages(data
, msg
, n_msg
);
120 static void handle_generic(struct usbdev_data
*data
, struct blob_attr
**tb
)
123 send_config_messages(data
, tb
[DATA_MSG
]);
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
)
129 unsigned char *buffer
= alloca(len
? len
: 1);
131 libusb_control_transfer(data
->devh
, type
, req
, val
, idx
, buffer
, len
, 1000);
134 static void handle_huawei(struct usbdev_data
*data
, struct blob_attr
**tb
)
136 int type
= LIBUSB_REQUEST_TYPE_STANDARD
| LIBUSB_RECIPIENT_DEVICE
;
137 send_control_packet(data
, type
, LIBUSB_REQUEST_SET_FEATURE
, 1, 0, 0);
140 static void handle_huaweinew(struct usbdev_data
*data
, struct blob_attr
**tb
)
142 static struct msg_entry msgs
[] = {
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
150 data
->need_response
= false;
151 send_messages(data
, msgs
, ARRAY_SIZE(msgs
));
154 static void handle_standardeject(struct usbdev_data
*data
, struct blob_attr
**tb
)
156 static struct msg_entry msgs
[] = {
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
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
167 data
->need_response
= true;
168 send_messages(data
, msgs
, ARRAY_SIZE(msgs
));
171 static void handle_sierra(struct usbdev_data
*data
, struct blob_attr
**tb
)
173 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
;
174 send_control_packet(data
, type
, LIBUSB_REQUEST_SET_INTERFACE
, 1, 0, 0);
177 static void handle_sony(struct usbdev_data
*data
, struct blob_attr
**tb
)
179 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_ENDPOINT_IN
;
183 send_control_packet(data
, type
, 0x11, 2, 0, 3);
185 libusb_close(data
->devh
);
188 for (i
= 0; i
< 25; i
++) {
189 data
->devh
= libusb_open_device_with_vid_pid(usb
,
190 data
->desc
.idVendor
, data
->desc
.idProduct
);
195 send_control_packet(data
, type
, 0x11, 2, 0, 3);
198 static void handle_qisda(struct usbdev_data
*data
, struct blob_attr
**tb
)
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
;
203 libusb_control_transfer(data
->devh
, type
, 0x04, 0, 0, buffer
, 16, 1000);
206 static void handle_gct(struct usbdev_data
*data
, struct blob_attr
**tb
)
208 int type
= LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_RECIPIENT_INTERFACE
| LIBUSB_ENDPOINT_IN
;
212 if (libusb_claim_interface(data
->devh
, data
->interface
))
215 send_control_packet(data
, type
, 0xa0, 0, data
->interface
, 1);
216 send_control_packet(data
, type
, 0xfe, 0, data
->interface
, 1);
218 libusb_release_interface(data
->devh
, data
->interface
);
221 static void handle_kobil(struct usbdev_data
*data
, struct blob_attr
**tb
)
223 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
| LIBUSB_ENDPOINT_IN
;
226 send_control_packet(data
, type
, 0x88, 0, 0, 8);
229 static void handle_sequans(struct usbdev_data
*data
, struct blob_attr
**tb
)
231 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
;
232 send_control_packet(data
, type
, LIBUSB_REQUEST_SET_INTERFACE
, 2, 0, 0);
235 static void mobile_action_interrupt_msg(struct usbdev_data
*data
, void *msg
, int n_in
)
237 unsigned char *buf
= alloca(8);
238 int ep_out
= 0x02, ep_in
= 0x81;
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);
248 static void handle_mobile_action(struct usbdev_data
*data
, struct blob_attr
**tb
)
250 int type
= LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_RECIPIENT_INTERFACE
;
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"
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);
277 static void handle_cisco(struct usbdev_data
*data
, struct blob_attr
**tb
)
279 static struct msg_entry msgs
[] = {
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
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
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
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
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
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
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
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
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
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
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
318 data
->need_response
= true;
319 send_messages(data
, msgs
, ARRAY_SIZE(msgs
));
322 static void set_alt_setting(struct usbdev_data
*data
, int setting
)
324 if (libusb_claim_interface(data
->devh
, data
->interface
))
327 libusb_set_interface_alt_setting(data
->devh
, data
->interface
, setting
);
328 libusb_release_interface(data
->devh
, data
->interface
);
347 static const struct {
349 void (*cb
)(struct usbdev_data
*data
, struct blob_attr
**tb
);
350 } modeswitch_cb
[__MODE_MAX
] = {
351 [MODE_GENERIC
] = { "Generic", handle_generic
},
352 [MODE_STDEJECT
] = { "StandardEject", handle_standardeject
},
353 [MODE_HUAWEI
] = { "Huawei", handle_huawei
},
354 [MODE_HUAWEINEW
] = { "HuaweiNew", handle_huaweinew
},
355 [MODE_SIERRA
] = { "Sierra", handle_sierra
},
356 [MODE_SONY
] = { "Sony", handle_sony
},
357 [MODE_QISDA
] = { "Qisda", handle_qisda
},
358 [MODE_GCT
] = { "GCT", handle_gct
},
359 [MODE_KOBIL
] = { "Kobil", handle_kobil
},
360 [MODE_SEQUANS
] = { "Sequans", handle_sequans
},
361 [MODE_MOBILE_ACTION
] = { "MobileAction", handle_mobile_action
},
362 [MODE_CISCO
] = { "Cisco", handle_cisco
},
365 void handle_switch(struct usbdev_data
*data
)
367 static const struct blobmsg_policy data_policy
[__DATA_MAX
] = {
368 [DATA_MODE
] = { .name
= "mode", .type
= BLOBMSG_TYPE_STRING
},
369 [DATA_MSG
] = { .name
= "msg", .type
= BLOBMSG_TYPE_ARRAY
},
370 [DATA_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_INT32
},
371 [DATA_MSG_EP
] = { .name
= "msg_endpoint", .type
= BLOBMSG_TYPE_INT32
},
372 [DATA_RES_EP
] = { .name
= "response_endpoint", .type
= BLOBMSG_TYPE_INT32
},
373 [DATA_RESPONSE
] = { .name
= "response", .type
= BLOBMSG_TYPE_BOOL
},
374 [DATA_CONFIG
] = { .name
= "config", .type
= BLOBMSG_TYPE_INT32
},
375 [DATA_ALT
] = { .name
= "alt", .type
= BLOBMSG_TYPE_INT32
},
377 struct blob_attr
*tb
[__DATA_MAX
];
378 int mode
= MODE_GENERIC
;
380 blobmsg_parse(data_policy
, __DATA_MAX
, tb
, blobmsg_data(data
->info
), blobmsg_data_len(data
->info
));
382 if (tb
[DATA_INTERFACE
])
383 data
->interface
= blobmsg_get_u32(tb
[DATA_INTERFACE
]);
386 data
->msg_endpoint
= blobmsg_get_u32(tb
[DATA_MSG_EP
]);
389 data
->response_endpoint
= blobmsg_get_u32(tb
[DATA_RES_EP
]);
391 if (tb
[DATA_RELEASE_DELAY
])
392 data
->release_delay
= blobmsg_get_u32(tb
[DATA_RELEASE_DELAY
]);
394 if (tb
[DATA_RESPONSE
])
395 data
->need_response
= blobmsg_get_bool(tb
[DATA_RESPONSE
]);
401 modestr
= blobmsg_data(tb
[DATA_MODE
]);
402 for (i
= 0; i
< __MODE_MAX
; i
++) {
403 if (strcmp(modeswitch_cb
[i
].name
, modestr
) != 0)
411 modeswitch_cb
[mode
].cb(data
, tb
);
413 if (tb
[DATA_CONFIG
]) {
414 int config
, config_new
;
416 config_new
= blobmsg_get_u32(tb
[DATA_CONFIG
]);
417 if (libusb_get_configuration(data
->devh
, &config
) ||
418 config
!= config_new
)
419 libusb_set_configuration(data
->devh
, config_new
);
423 int new = blobmsg_get_u32(tb
[DATA_ALT
]);
424 set_alt_setting(data
, new);