2 #include <aura/private.h>
3 #include <aura/usb_helpers.h>
11 SUSB_DEVICE_SEARCHING=0,
12 SUSB_DEVICE_OPERATIONAL,
17 struct aura_node *node;
19 struct aura_export_table *etbl;
27 struct libusb_context *ctx;
28 libusb_device_handle *handle;
31 struct libusb_transfer* ctransfer;
35 struct ncusb_devwatch_data dev_descr;
38 static void usb_panic_and_reset_state(
struct aura_node *node)
43 BUG(node,
"susb: Failing with busy ctransfer");
45 inf->state = SUSB_DEVICE_RESTART;
48 static void submit_control(
struct aura_node *node)
53 ret = libusb_submit_transfer(inf->ctransfer);
55 slog(0, SLOG_ERROR,
"usb: error submitting control transfer");
56 usb_panic_and_reset_state(node);
62 static int check_control(
struct libusb_transfer *transfer)
64 struct aura_node *node = transfer->user_data;
70 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
71 slog(0, SLOG_ERROR,
"usb: error completing control transfer");
72 ncusb_print_libusb_transfer(transfer);
73 usb_panic_and_reset_state(node);
79 static int usb_start_ops(
struct libusb_device_handle *hndl,
void *arg)
87 struct usb_dev_info *inf = arg;
90 inf->state = SUSB_DEVICE_OPERATIONAL;
92 slog(4, SLOG_DEBUG,
"susb: Device opened and ready to accept calls");
98 static void lua_settoken(lua_State *L,
const char* name,
char t) {
102 lua_pushstring(L, tmp);
103 lua_setglobal(L, name);
106 static char *lua_strfromstack(lua_State *L,
int n)
109 if (lua_isstring(L, n))
110 ret = strdup(lua_tostring(L, n));
114 extern int luaopen_auracore (lua_State *L);
115 static int susb_open(
struct aura_node *node,
const char *conf)
118 struct libusb_context *ctx;
119 struct usb_dev_info *inf = calloc(1,
sizeof(*inf));
126 ret = libusb_init(&ctx);
135 inf->ctransfer = libusb_alloc_transfer(0);
141 lua_setglobal(L,
"aura");
143 slog(2, SLOG_INFO,
"usbsimple: config file %s", conf);
145 const char scr[] =
"return require(\"aura/conf-loader\")\n";
146 ret = luaL_loadbuffer (L, scr, strlen(scr),
"ldr");
149 slog(0, SLOG_ERROR, lua_tostring(L, -1));
150 slog(0, SLOG_ERROR,
"usbsimple: config file load error");
154 lua_pushstring(L, conf);
155 lua_setglobal(L,
"simpleconf");
157 lua_pushlightuserdata(L, node);
158 lua_setglobal(L,
"node");
164 lua_settoken(L,
"UINT8", URPC_U8);
165 lua_settoken(L,
"UINT16", URPC_U16);
166 lua_settoken(L,
"UINT32", URPC_U32);
167 lua_settoken(L,
"UINT64", URPC_U64);
169 lua_settoken(L,
"SINT8", URPC_S8);
170 lua_settoken(L,
"SINT16", URPC_S16);
171 lua_settoken(L,
"SINT32", URPC_S32);
172 lua_settoken(L,
"SINT64", URPC_S64);
174 lua_settoken(L,
"FMT_BIN", URPC_BIN);
176 ret = lua_pcall(L, 0, 6, 0);
178 const char* err = lua_tostring(L, -1);
179 slog(0, SLOG_FATAL,
"usbsimple: %s", err);
183 inf->dev_descr.vid = lua_tonumber(L, -6);
184 inf->dev_descr.pid = lua_tonumber(L, -5);
185 inf->dev_descr.vendor = lua_strfromstack(L, -4);
186 inf->dev_descr.product = lua_strfromstack(L, -3);
187 inf->dev_descr.serial = lua_strfromstack(L, -2);
188 inf->etbl = lua_touserdata(L, -1);
190 inf->dev_descr.device_found_func = usb_start_ops;
191 inf->dev_descr.arg = inf;
198 ncusb_start_descriptor_watching(node, inf->ctx);
199 slog(1, SLOG_INFO,
"usb: Now looking for a device %x:%x %s/%s/%s",
200 inf->dev_descr.vid, inf->dev_descr.pid,
201 inf->dev_descr.vendor, inf->dev_descr.product, inf->dev_descr.serial);
202 ncusb_watch_for_device(inf->ctx, &inf->dev_descr);
206 libusb_free_transfer(inf->ctransfer);
210 libusb_exit(inf->ctx);
216 static void susb_close(
struct aura_node *node)
219 slog(3, SLOG_INFO,
"Closing susb transport");
222 libusb_handle_events(inf->ctx);
224 if (inf->dev_descr.vendor)
225 free(inf->dev_descr.vendor);
226 if (inf->dev_descr.product)
227 free(inf->dev_descr.product);
228 if (inf->dev_descr.serial)
229 free(inf->dev_descr.serial);
231 libusb_free_transfer(inf->ctransfer);
234 libusb_close(inf->handle);
236 libusb_exit(inf->ctx);
240 static void cb_call_done(
struct libusb_transfer *transfer)
242 struct aura_node *node = transfer->user_data;
248 if (0 == check_control(transfer)) {
249 slog(4, SLOG_DEBUG,
"Requeuing!");
252 inf->current_buffer = NULL;
256 static void susb_issue_call(
struct aura_node *node,
struct aura_buffer *buf)
258 struct aura_object *o = buf->
object;
261 uint16_t wIndex, wValue, *ptr;
265 rqtype = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN;
268 datalen = o->arglen - 2 *
sizeof(uint16_t);
269 rqtype = LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT;
271 ptr = (uint16_t *) &buf->
data[buf->
pos];
275 memmove(&buf->
data[buf->
pos], ptr, datalen);
300 if (node->need_endian_swap) {
301 wValue = __swap16(wValue);
302 wIndex = __swap16(wValue);
305 inf->current_buffer = buf;
306 libusb_fill_control_setup((
unsigned char *) buf->
data, rqtype, o->id, wValue, wIndex,
308 libusb_fill_control_transfer(inf->ctransfer, inf->handle,
309 (
unsigned char *) buf->
data, cb_call_done, node, 4000);
310 submit_control(node);
313 static void susb_loop(
struct aura_node *node,
const struct aura_pollfds *fd)
317 struct timeval tv = {
322 libusb_handle_events_timeout(inf->ctx, &tv);
326 if (inf->state == SUSB_DEVICE_RESTART) {
327 slog(4, SLOG_DEBUG,
"usb: transport offlined, starting to look for a device");
329 libusb_close(inf->handle);
331 inf->state = SUSB_DEVICE_SEARCHING;
332 ncusb_watch_for_device(inf->ctx, &inf->dev_descr);
333 }
else if (inf->state == SUSB_DEVICE_OPERATIONAL) {
341 for (i=0; i < inf->etbl->next; i++) {
342 struct aura_object *tmp;
343 tmp=&inf->etbl->objects[i];
344 tmp->arglen += tmp->retlen;
346 aura_etable_activate(inf->etbl);
353 susb_issue_call(node, buf);
364 .buffer_overhead = LIBUSB_CONTROL_SETUP_SIZE,
365 .buffer_offset = LIBUSB_CONTROL_SETUP_SIZE
368 AURA_TRANSPORT(tusb);
void aura_set_status(struct aura_node *node, int status)
struct aura_buffer * aura_dequeue_buffer(struct list_head *head)
const char * name
Required.
static void aura_set_transportdata(struct aura_node *node, void *udata)
static void * aura_get_transportdata(struct aura_node *node)
void aura_queue_buffer(struct list_head *list, struct aura_buffer *buf)
struct aura_buffer * aura_peek_buffer(struct list_head *head)
struct aura_object * object