c99dbc13994ae9aa4dcb87606194c6ce416ee3aa
[carl9170fw.git] / tools / carlu / src / usb.c
1 /*
2  * carl9170user - userspace testing utility for ar9170 devices
3  *
4  * USB back-end driver
5  *
6  * Copyright 2009, 2010 Christian Lamparter <chunkeey@googlemail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include "libusb.h"
33
34 #include "carlu.h"
35 #include "usb.h"
36 #include "debug.h"
37 #include "list.h"
38
39 #define ADD_DEV(_vid, _pid, _vs, _ps)   {               \
40         .idVendor = _vid,                               \
41         .idProduct = _pid,                              \
42         .vendor_name = _vs,                             \
43         .product_name = _ps                             \
44 }
45
46 static const struct {
47         uint16_t idVendor;
48         uint16_t idProduct;
49         char *vendor_name;
50         char *product_name;
51 } dev_list[] = {
52         ADD_DEV(0x0cf3, 0x9170, "Atheros", "9170"),
53         ADD_DEV(0x0cf3, 0x1001, "Atheros", "TG121N"),
54         ADD_DEV(0x0cf3, 0x1002, "TP-Link", "TL-WN821N v2"),
55         ADD_DEV(0xcace, 0x0300, "Cace", "Airpcap NX"),
56         ADD_DEV(0x07d1, 0x3c10, "D-Link", "DWA 160 A1"),
57         ADD_DEV(0x07d1, 0x3a09, "D-Link", "DWA 160 A2"),
58         ADD_DEV(0x0846, 0x9010, "Netgear", "WNDA3100"),
59         ADD_DEV(0x0846, 0x9001, "Netgear", "WN111 v2"),
60         ADD_DEV(0x0ace, 0x1221, "Zydas", "ZD1221"),
61         ADD_DEV(0x0586, 0x3417, "ZyXEL", "NWD271N"),
62         ADD_DEV(0x0cde, 0x0023, "Z-Com", "UB81 BG"),
63         ADD_DEV(0x0cde, 0x0026, "Z-Com", "UB82 ABG"),
64         ADD_DEV(0x0cde, 0x0027, "Sphairon", "Homelink 1202"),
65         ADD_DEV(0x083a, 0xf522, "Arcadyan", "WN7512"),
66         ADD_DEV(0x2019, 0x5304, "Planex", "GWUS300"),
67         ADD_DEV(0x04bb, 0x093f, "IO-Data", "WNGDNUS2"),
68         ADD_DEV(0x057C, 0x8401, "AVM", "FRITZ!WLAN USB Stick N"),
69         ADD_DEV(0x057C, 0x8402, "AVM", "FRITZ!WLAN USB Stick N 2.4"),
70 };
71
72 static libusb_context *usb_ctx;
73 static LIST_HEAD(active_dev_list);
74
75 static int carlusb_event_thread(void *_ar)
76 {
77         struct carlusb *ar = (void *)_ar;
78         dbg("event thread active and polling.\n");
79
80         while (!ar->stop_event_polling)
81                 libusb_handle_events(ar->ctx);
82
83         dbg("==> event thread desixed.\n");
84         return 0;
85 }
86
87 static int carlusb_is_ar9170(struct libusb_device_descriptor *desc)
88 {
89         unsigned int i;
90
91         for (i = 0; i < ARRAY_SIZE(dev_list); i++) {
92                 if ((desc->idVendor == dev_list[i].idVendor) &&
93                     (desc->idProduct == dev_list[i].idProduct)) {
94                         dbg("== found device \"%s %s\" [0x%04x:0x%04x]\n",
95                                 dev_list[i].vendor_name, dev_list[i].product_name,
96                                 desc->idVendor, desc->idProduct);
97
98                         return i;
99                 }
100         }
101
102         return -1;
103 }
104
105 static bool carlusb_is_dev(struct carlusb *iter,
106                                struct libusb_device *dev)
107 {
108         libusb_device *list_dev;
109
110         if (!iter->dev)
111                 return false;
112
113         list_dev = libusb_get_device(iter->dev);
114
115         if (libusb_get_bus_number(list_dev) == libusb_get_bus_number(dev) &&
116             libusb_get_device_address(list_dev) == libusb_get_device_address(dev))
117                 return true;
118
119         return false;
120 }
121
122 int carlusb_show_devinfo(struct carlusb *ar)
123 {
124         struct libusb_device_descriptor desc;
125         libusb_device *dev;
126         int err;
127
128         dev = libusb_get_device(ar->dev);
129
130         err = libusb_get_device_descriptor(dev, &desc);
131         if (err)
132                 return err;
133
134         info("USB Device Information:\n");
135         info("\tUSB VendorID:%.4x(%s), ProductID:%.4x(%s)\n",
136              dev_list[ar->idx].idVendor, dev_list[ar->idx].vendor_name,
137              dev_list[ar->idx].idProduct, dev_list[ar->idx].product_name);
138         info("\tBus:%d Address:%d\n", libusb_get_bus_number(dev),
139              libusb_get_device_address(dev));
140
141         return 0;
142 }
143
144 static int carlusb_get_dev(struct carlusb *ar, bool reset)
145 {
146         struct carlusb *iter;
147         libusb_device_handle *dev;
148         libusb_device **list;
149         int ret, err, i, idx = -1;
150
151         ret = libusb_get_device_list(usb_ctx, &list);
152         if (ret < 0) {
153                 err("usb device enum failed (%d)\n", ret);
154                 return ret;
155         }
156
157         for (i = 0; i < ret; i++) {
158                 struct libusb_device_descriptor desc;
159
160                 memset(&desc, 0, sizeof(desc));
161                 err = libusb_get_device_descriptor(list[i], &desc);
162                 if (err != 0)
163                         continue;
164
165                 idx = carlusb_is_ar9170(&desc);
166                 if (idx < 0)
167                         continue;
168
169                 list_for_each_entry(iter, &active_dev_list, dev_list) {
170                         if (carlusb_is_dev(iter, list[i])) {
171                                 err = -EALREADY;
172                                 break;
173                         }
174                 }
175
176                 if (err)
177                         continue;
178
179                 err = libusb_open(list[i], &dev);
180                 if (err != 0) {
181                         err("failed to open device (%d)\n", err);
182                         continue;
183                 }
184
185                 err = libusb_kernel_driver_active(dev, 0);
186                 switch (err) {
187                 case 0:
188                         break;
189                 default:
190                         err("failed to aquire exculusive access (%d).\n", err);
191                         goto skip;
192                 }
193
194                 if (reset) {
195                         err = libusb_reset_device(dev);
196                         if (err != 0) {
197                                 err("failed to reset device (%d)\n", err);
198                                 goto skip;
199                         }
200                 }
201
202                 err = libusb_claim_interface(dev, 0);
203                 if (err == 0) {
204                         dbg(">device is now under our control.\n");
205                         break;
206                 } else {
207                         err("failed to claim device (%d)\n", err);
208                         goto skip;
209                 }
210
211 skip:
212                 libusb_close(dev);
213         }
214
215         if (i != ret) {
216                 ar->idx = idx;
217                 ar->ctx = usb_ctx;
218                 ar->dev = dev;
219                 list_add_tail(&ar->dev_list, &active_dev_list);
220                 ret = 0;
221         } else {
222                 ret = -ENODEV;
223         }
224
225         libusb_free_device_list(list, 1);
226         return ret;
227 }
228
229 static void carlusb_tx_cb(struct carlusb *ar,
230                               struct frame *frame)
231 {
232         if (ar->common.tx_cb)
233                 ar->common.tx_cb(&ar->common, frame);
234
235         ar->common.tx_octets += frame->len;
236
237         carlu_free_frame(&ar->common, frame);
238 }
239
240 static void carlusb_zap_queues(struct carlusb *ar)
241 {
242         struct frame *frame;
243
244         BUG_ON(SDL_mutexP(ar->tx_queue_lock) != 0);
245         while (!list_empty(&ar->tx_queue)) {
246                 frame = list_first_entry(&ar->tx_queue, struct frame, dcb.list);
247                 list_del(&frame->dcb.list);
248                 carlusb_tx_cb(ar, frame);
249         }
250         SDL_mutexV(ar->tx_queue_lock);
251 }
252
253 static void carlusb_free_driver(struct carlusb *ar)
254 {
255         if (ar) {
256                 if (ar->event_pipe[0] > -1)
257                         close(ar->event_pipe[0]);
258
259                 if (ar->event_pipe[1] > -1)
260                         close(ar->event_pipe[1]);
261
262                 carlusb_zap_queues(ar);
263                 carlfw_release(ar->common.fw);
264                 ar->common.fw = NULL;
265
266                 if (ar->dev) {
267                         libusb_release_interface(ar->dev, 0);
268                         libusb_close(ar->dev);
269                         ar->dev = NULL;
270                 }
271                 carlu_free_driver(&ar->common);
272         }
273 }
274
275 static int carlusb_init(struct carlusb *ar)
276 {
277         init_list_head(&ar->tx_queue);
278         ar->tx_queue_lock = SDL_CreateMutex();
279         ar->event_pipe[0] = ar->event_pipe[1] = -1;
280
281         return 0;
282 }
283
284 static struct carlusb *carlusb_open(void)
285 {
286         struct carlusb *tmp;
287         int err;
288
289         tmp = carlu_alloc_driver(sizeof(*tmp));
290         if (tmp == NULL)
291                 return NULL;
292
293         err = carlusb_init(tmp);
294         if (err < 0)
295                 goto err_out;
296
297         err = carlusb_get_dev(tmp, true);
298         if (err < 0)
299                 goto err_out;
300
301         return tmp;
302
303 err_out:
304         carlusb_free_driver(tmp);
305         return NULL;
306 }
307
308 static void carlusb_cancel_rings(struct carlusb *ar)
309 {
310         unsigned int i;
311
312         for (i = 0; i < ARRAY_SIZE(ar->rx_ring); i++)
313                 libusb_cancel_transfer(ar->rx_ring[i]);
314
315         libusb_cancel_transfer(ar->rx_interrupt);
316 }
317
318 static void carlusb_free_rings(struct carlusb *ar)
319 {
320         unsigned int i;
321
322         for (i = 0; i < ARRAY_SIZE(ar->rx_ring); i++)
323                 libusb_free_transfer(ar->rx_ring[i]);
324
325         libusb_free_transfer(ar->rx_interrupt);
326 }
327
328 static void carlusb_destroy(struct carlusb *ar)
329 {
330         int event_thread_status;
331
332         dbg("==>release device.\n");
333
334         ar->stop_event_polling = true;
335
336         carlusb_cancel_rings(ar);
337
338         SDL_WaitThread(ar->event_thread, &event_thread_status);
339
340         carlusb_free_rings(ar);
341         list_del(&ar->dev_list);
342 }
343
344 static void carlusb_tx_bulk_cb(struct libusb_transfer *transfer);
345
346 static void carlusb_tx_pending(struct carlusb *ar)
347 {
348         struct frame *frame;
349         struct libusb_transfer *urb;
350         int err;
351
352         BUG_ON(SDL_mutexP(ar->tx_queue_lock) != 0);
353         if (ar->tx_queue_len >= (AR9170_TX_MAX_ACTIVE_URBS) ||
354             list_empty(&ar->tx_queue))
355                 goto out;
356
357         ar->tx_queue_len++;
358
359         urb = libusb_alloc_transfer(0);
360         if (urb == NULL)
361                 goto out;
362
363         frame = list_first_entry(&ar->tx_queue, struct frame, dcb.list);
364         list_del(&frame->dcb.list);
365
366         if (ar->common.tx_stream) {
367                 struct ar9170_stream *tx_stream;
368
369                 tx_stream = frame_push(frame, sizeof(*tx_stream));
370                 tx_stream->length = cpu_to_le16(frame->len);
371                 tx_stream->tag = cpu_to_le16(0x697e);
372         }
373
374         libusb_fill_bulk_transfer(urb, ar->dev, AR9170_EP_TX, (unsigned char *)
375                 frame->data, frame->len, carlusb_tx_bulk_cb, (void *)frame, 0);
376
377         /* FIXME: ZERO_PACKET support! */
378         urb->flags |= LIBUSB_TRANSFER_FREE_TRANSFER;
379 /*      urb->flags |= LIBUSB_TRANSFER_ZERO_PACKET; */
380         frame->dev = (void *) ar;
381         frame_get(frame);
382
383         err = libusb_submit_transfer(urb);
384         if (err != 0) {
385                 err("->usb bulk tx submit failed (%d).\n", err);
386                 libusb_free_transfer(urb);
387         }
388
389 out:
390         SDL_mutexV(ar->tx_queue_lock);
391         return;
392 }
393
394 void carlusb_tx(struct carlu *_ar, struct frame *frame)
395 {
396         struct carlusb *ar = (void *)_ar;
397
398         BUG_ON(SDL_mutexP(ar->tx_queue_lock) != 0);
399
400         list_add_tail(&frame->dcb.list, &ar->tx_queue);
401         SDL_mutexV(ar->tx_queue_lock);
402
403         carlusb_tx_pending(ar);
404 }
405
406 static void carlusb_tx_bulk_cb(struct libusb_transfer *transfer)
407 {
408         struct frame *frame = (void *) transfer->user_data;
409         struct carlusb *ar = (void *) frame->dev;
410
411         BUG_ON(SDL_mutexP(ar->tx_queue_lock) != 0);
412         ar->tx_queue_len--;
413         SDL_mutexV(ar->tx_queue_lock);
414
415         if (ar->common.tx_stream)
416                 frame_pull(frame, 4);
417
418         carlusb_tx_cb(ar, frame);
419         carlusb_tx_pending(ar);
420 }
421
422 static void carlusb_rx_interrupt_cb(struct libusb_transfer *transfer)
423 {
424         struct carlusb *ar = (void *) transfer->user_data;
425         int err;
426
427         switch (transfer->status) {
428         case LIBUSB_TRANSFER_COMPLETED:
429                 carlu_handle_command(&ar->common, transfer->buffer, transfer->actual_length);
430                 break;
431
432         case LIBUSB_TRANSFER_CANCELLED:
433                 return;
434
435         default:
436                 err("==>rx_irq urb died (%d)\n", transfer->status);
437                 break;
438         }
439
440         err = libusb_submit_transfer(transfer);
441         if (err != 0)
442                 err("==>rx_irq urb resubmit failed (%d)\n", err);
443 }
444
445 static void carlusb_rx_bulk_cb(struct libusb_transfer *transfer)
446 {
447         struct frame *frame = (void *) transfer->user_data;
448         struct carlusb *ar = (void *) frame->dev;
449         int err;
450
451         switch (transfer->status) {
452         case LIBUSB_TRANSFER_COMPLETED:
453                 frame_put(frame, transfer->actual_length);
454
455                 carlu_rx(&ar->common, frame);
456
457                 frame_trim(frame, 0);
458                 break;
459
460         case LIBUSB_TRANSFER_CANCELLED:
461                 return;
462
463         default:
464                 err("==>rx_bulk urb died (%d)\n", transfer->status);
465                 break;
466         }
467
468         err = libusb_submit_transfer(transfer);
469         if (err != 0)
470                 err("->rx_bulk urb resubmit failed (%d)\n", err);
471 }
472
473 static int carlusb_initialize_rxirq(struct carlusb *ar)
474 {
475         int err;
476
477         ar->rx_interrupt = libusb_alloc_transfer(0);
478         if (ar->rx_interrupt == NULL) {
479                 err("==>cannot alloc rx interrupt urb\n");
480                 return -1;
481         }
482
483         libusb_fill_interrupt_transfer(ar->rx_interrupt, ar->dev, AR9170_EP_IRQ,
484                                        (unsigned char *)&ar->irq_buf, sizeof(ar->irq_buf),
485                                        carlusb_rx_interrupt_cb, (void *) ar, 0);
486
487         err = libusb_submit_transfer(ar->rx_interrupt);
488         if (err != 0) {
489                 err("==>failed to submit rx interrupt (%d)\n", err);
490                 return err;
491         }
492
493         dbg("rx interrupt is now operational.\n");
494         return 0;
495 }
496
497 static int carlusb_initialize_rxrings(struct carlusb *ar)
498 {
499         struct frame *tmp;
500         unsigned int i;
501         int err;
502
503         for (i = 0; i < ARRAY_SIZE(ar->rx_ring); i++) {
504                 tmp = frame_alloc(ar->rx_max);
505                 if (tmp == NULL)
506                         return -ENOMEM;
507
508                 tmp->dev = (void *) ar;
509
510                 ar->rx_ring[i] = libusb_alloc_transfer(0);
511                 if (ar->rx_ring[i] == NULL) {
512                         frame_free(tmp);
513                         return -ENOMEM;
514                 }
515
516                 libusb_fill_bulk_transfer(ar->rx_ring[i], ar->dev,
517                         AR9170_EP_RX, (unsigned char *)tmp->data,
518                         ar->rx_max, carlusb_rx_bulk_cb, (void *)tmp, 0);
519
520                 err = libusb_submit_transfer(ar->rx_ring[i]);
521                 if (err != 0) {
522                         err("==>failed to submit rx buld urb (%d)\n", err);
523                         return EXIT_FAILURE;
524                 }
525         }
526
527         dbg("rx ring is now ready to receive.\n");
528         return 0;
529 }
530
531 static int carlusb_load_firmware(struct carlusb *ar)
532 {
533         int ret = 0;
534
535         dbg("loading firmware...\n");
536
537         ar->common.fw = carlfw_load(CARL9170_FIRMWARE_FILE);
538         if (IS_ERR_OR_NULL(ar->common.fw))
539                 return PTR_ERR(ar->common.fw);
540
541         ret = carlu_fw_check(&ar->common);
542         if (ret)
543                 return ret;
544
545         ret = carlusb_fw_check(ar);
546         if (ret)
547                 return ret;
548
549         return 0;
550 }
551
552 static int carlusb_upload_firmware(struct carlusb *ar, bool boot)
553 {
554         uint32_t addr = 0x200000;
555         size_t len;
556         void *buf;
557         int ret = 0;
558
559         dbg("initiating firmware image upload procedure.\n");
560
561         buf = carlfw_get_fw(ar->common.fw, &len);
562         if (IS_ERR_OR_NULL(buf))
563                 return PTR_ERR(buf);
564
565         if (ar->miniboot_size) {
566                 dbg("Miniboot firmware size:%d\n", ar->miniboot_size);
567                 len -= ar->miniboot_size;
568                 buf += ar->miniboot_size;
569         }
570
571         while (len) {
572                 int blocklen = len > 4096 ? 4096 : len;
573
574                 ret = libusb_control_transfer(ar->dev, 0x40, 0x30, addr >> 8, 0, buf, blocklen, 1000);
575                 if (ret != blocklen && ret != LIBUSB_ERROR_TIMEOUT) {
576                         err("==>firmware upload failed (%d)\n", ret);
577                         return -EXIT_FAILURE;
578                 }
579
580                 dbg("uploaded %d bytes to start address 0x%04x.\n", blocklen, addr);
581
582                 buf += blocklen;
583                 addr += blocklen;
584                 len -= blocklen;
585         }
586
587         if (boot) {
588                 ret = libusb_control_transfer(ar->dev, 0x40, 0x31, 0, 0, NULL, 0, 5000);
589                 if (ret < 0) {
590                         err("unable to boot firmware (%d)\n", ret);
591                         return -EXIT_FAILURE;
592                 }
593
594                 /* give the firmware some time to reset & reboot */
595                 SDL_Delay(100);
596
597                 /*
598                  * since the device did a full usb reset,
599                  * we have to get a new "dev".
600                  */
601                 libusb_release_interface(ar->dev, 0);
602                 libusb_close(ar->dev);
603                 ar->dev = NULL;
604                 list_del(&ar->dev_list);
605
606                 ret = carlusb_get_dev(ar, false);
607         }
608
609         return 0;
610 }
611
612 int carlusb_cmd(struct carlu *_ar, uint8_t oid,
613                       uint8_t *cmd, size_t clen,
614                       uint8_t *rsp, size_t rlen)
615 {
616         struct carlusb *ar = (void *)_ar;
617         int ret, send;
618
619         if (clen > (CARL9170_MAX_CMD_LEN - 4)) {
620                 err("|-> OID:0x%.2x has too much payload (%d octs)\n", oid, (int)clen);
621                 return -EINVAL;
622         }
623
624         ret = SDL_mutexP(ar->common.resp_lock);
625         if (ret != 0) {
626                 err("failed to acquire resp_lock.\n");
627                 print_hex_dump_bytes(ERROR, "CMD:", ar->cmd_buf, clen);
628                 return -1;
629         }
630
631         ar->cmd_buf[0] = clen;
632         ar->cmd_buf[1] = oid;
633         /* buf[2] & buf[3] are padding */
634         if (clen && cmd != (uint8_t *)(&ar->cmd_buf[4]))
635                 memcpy(&ar->cmd_buf[4], cmd, clen);
636
637         ar->common.resp_buf = (uint8_t *)rsp;
638         ar->common.resp_len = rlen;
639
640         ret = libusb_interrupt_transfer(ar->dev, AR9170_EP_CMD, ar->cmd_buf, clen + 4, &send, 32);
641         if (ret != 0) {
642                 err("OID:0x%.2x failed due to (%d) (%d).\n", oid, ret, send);
643                 print_hex_dump_bytes(ERROR, "CMD:", ar->cmd_buf, clen);
644                 SDL_mutexV(ar->common.resp_lock);
645                 return ret;
646         }
647
648         ret = SDL_CondWaitTimeout(ar->common.resp_pend, ar->common.resp_lock, 100);
649         if (ret != 0) {
650                 err("|-> OID:0x%.2x timed out %d.\n", oid, ret);
651                 ar->common.resp_buf = NULL;
652                 ar->common.resp_len = 0;
653                 ret = -ETIMEDOUT;
654         }
655
656         SDL_mutexV(ar->common.resp_lock);
657         return ret;
658 }
659
660 struct carlu *carlusb_probe(void)
661 {
662         struct carlusb *ar;
663         int ret = -ENOMEM;
664
665         ar = carlusb_open();
666         if (ar == NULL)
667                 goto err_out;
668
669         ret = carlusb_show_devinfo(ar);
670         if (ret)
671                 goto err_out;
672
673         ret = carlusb_load_firmware(ar);
674         if (ret)
675                 goto err_out;
676
677         ret = pipe(ar->event_pipe);
678         if (ret)
679                 goto err_out;
680
681         ar->stop_event_polling = false;
682         ar->event_thread = SDL_CreateThread(carlusb_event_thread, ar);
683
684         ret = carlusb_upload_firmware(ar, true);
685         if (ret)
686                 goto err_kill;
687
688         ret = carlusb_initialize_rxirq(ar);
689         if (ret)
690                 goto err_kill;
691
692         ret = carlusb_initialize_rxrings(ar);
693         if (ret)
694                 goto err_kill;
695
696         ret = carlu_cmd_echo(&ar->common, 0x44110dee);
697         if (ret) {
698                 err("echo test failed...\n");
699                 goto err_kill;
700         }
701
702         info("firmware is active and running.\n");
703
704         carlu_fw_info(&ar->common);
705
706         return &ar->common;
707
708 err_kill:
709         carlusb_destroy(ar);
710
711 err_out:
712         carlusb_free_driver(ar);
713         err("usb device rendezvous failed (%d).\n", ret);
714
715         return NULL;
716 }
717
718 void carlusb_close(struct carlu *_ar)
719 {
720         struct carlusb *ar = (void *) _ar;
721
722         carlu_cmd_reboot(_ar);
723
724         carlusb_destroy(ar);
725         carlusb_free_driver(ar);
726 }
727
728 int carlusb_print_known_devices(void)
729 {
730         unsigned int i;
731
732         debug_level = INFO;
733
734         info("==> dumping known device list <==\n");
735         for (i = 0; i < ARRAY_SIZE(dev_list); i++) {
736                 info("Vendor:\"%-9s\" Product:\"%-26s\" => USBID:[0x%04x:0x%04x]\n",
737                      dev_list[i].vendor_name, dev_list[i].product_name,
738                      dev_list[i].idVendor, dev_list[i].idProduct);
739         }
740         info("==> end of device list <==\n");
741
742         return EXIT_SUCCESS;
743 }
744
745 int usb_init(void)
746 {
747         int ret;
748
749         ret = libusb_init(&usb_ctx);
750         if (ret != 0) {
751                 err("failed to initialize usb subsystem (%d)\n", ret);
752                 return ret;
753         }
754
755         /* like a silent chatterbox! */
756         libusb_set_debug(usb_ctx, 2);
757
758         return 0;
759 }
760
761 void usb_exit(void)
762 {
763         libusb_exit(usb_ctx);
764 }