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