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_sierra(struct usbdev_data
*data
, struct blob_attr
**tb
)
142 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
;
143 send_control_packet(data
, type
, LIBUSB_REQUEST_SET_INTERFACE
, 1, 0, 0);
146 static void handle_sony(struct usbdev_data
*data
, struct blob_attr
**tb
)
148 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_ENDPOINT_IN
;
152 send_control_packet(data
, type
, 0x11, 2, 0, 3);
154 libusb_close(data
->devh
);
157 for (i
= 0; i
< 25; i
++) {
158 data
->devh
= libusb_open_device_with_vid_pid(usb
,
159 data
->desc
.idVendor
, data
->desc
.idProduct
);
164 send_control_packet(data
, type
, 0x11, 2, 0, 3);
167 static void handle_qisda(struct usbdev_data
*data
, struct blob_attr
**tb
)
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
;
172 libusb_control_transfer(data
->devh
, type
, 0x04, 0, 0, buffer
, 16, 1000);
175 static void handle_gct(struct usbdev_data
*data
, struct blob_attr
**tb
)
177 int type
= LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_RECIPIENT_INTERFACE
| LIBUSB_ENDPOINT_IN
;
181 if (libusb_claim_interface(data
->devh
, data
->interface
))
184 send_control_packet(data
, type
, 0xa0, 0, data
->interface
, 1);
185 send_control_packet(data
, type
, 0xfe, 0, data
->interface
, 1);
187 libusb_release_interface(data
->devh
, data
->interface
);
190 static void handle_kobil(struct usbdev_data
*data
, struct blob_attr
**tb
)
192 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
| LIBUSB_ENDPOINT_IN
;
195 send_control_packet(data
, type
, 0x88, 0, 0, 8);
198 static void handle_sequans(struct usbdev_data
*data
, struct blob_attr
**tb
)
200 int type
= LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
;
201 send_control_packet(data
, type
, LIBUSB_REQUEST_SET_INTERFACE
, 2, 0, 0);
204 static void mobile_action_interrupt_msg(struct usbdev_data
*data
, void *msg
, int n_in
)
206 unsigned char *buf
= alloca(8);
207 int ep_out
= 0x02, ep_in
= 0x81;
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);
217 static void handle_mobile_action(struct usbdev_data
*data
, struct blob_attr
**tb
)
219 int type
= LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_RECIPIENT_INTERFACE
;
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"
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);
246 static void handle_cisco(struct usbdev_data
*data
, struct blob_attr
**tb
)
248 static struct msg_entry msgs
[] = {
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
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
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
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
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
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
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
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
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
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
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
287 data
->need_response
= true;
288 send_messages(data
, msgs
, ARRAY_SIZE(msgs
));
291 static void set_alt_setting(struct usbdev_data
*data
, int setting
)
293 if (libusb_claim_interface(data
->devh
, data
->interface
))
296 libusb_set_interface_alt_setting(data
->devh
, data
->interface
, setting
);
297 libusb_release_interface(data
->devh
, data
->interface
);
314 static const struct {
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
},
330 void handle_switch(struct usbdev_data
*data
)
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
},
342 struct blob_attr
*tb
[__DATA_MAX
];
343 int mode
= MODE_GENERIC
;
345 blobmsg_parse(data_policy
, __DATA_MAX
, tb
, blobmsg_data(data
->info
), blobmsg_data_len(data
->info
));
347 if (tb
[DATA_INTERFACE
])
348 data
->interface
= blobmsg_get_u32(tb
[DATA_INTERFACE
]);
351 data
->msg_endpoint
= blobmsg_get_u32(tb
[DATA_MSG_EP
]);
354 data
->response_endpoint
= blobmsg_get_u32(tb
[DATA_RES_EP
]);
356 if (tb
[DATA_RELEASE_DELAY
])
357 data
->release_delay
= blobmsg_get_u32(tb
[DATA_RELEASE_DELAY
]);
359 if (tb
[DATA_RESPONSE
])
360 data
->need_response
= blobmsg_get_bool(tb
[DATA_RESPONSE
]);
366 modestr
= blobmsg_data(tb
[DATA_MODE
]);
367 for (i
= 0; i
< __MODE_MAX
; i
++) {
368 if (strcmp(modeswitch_cb
[i
].name
, modestr
) != 0)
376 modeswitch_cb
[mode
].cb(data
, tb
);
378 if (tb
[DATA_CONFIG
]) {
379 int config
, config_new
;
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
);
388 int new = blobmsg_get_u32(tb
[DATA_ALT
]);
389 set_alt_setting(data
, new);