2 * carlu - userspace testing utility for ar9170 devices
6 * Copyright 2009-2011 Christian Lamparter <chunkeey@googlemail.com>
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.
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.
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.
40 #define ADD_DEV(_vid, _pid, _vs, _ps) { \
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"),
73 static libusb_context *usb_ctx;
74 static LIST_HEAD(active_dev_list);
76 static int carlusb_event_thread(void *_ar)
78 struct carlu *ar = (void *)_ar;
79 dbg("event thread active and polling.\n");
81 while (!ar->stop_event_polling)
82 libusb_handle_events(ar->ctx);
84 dbg("==> event thread desixed.\n");
88 static int carlusb_is_ar9170(struct libusb_device_descriptor *desc)
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);
106 static bool carlusb_is_dev(struct carlu *iter,
107 struct libusb_device *dev)
109 libusb_device *list_dev;
114 list_dev = libusb_get_device(iter->dev);
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))
123 int carlusb_show_devinfo(struct carlu *ar)
125 struct libusb_device_descriptor desc;
129 dev = libusb_get_device(ar->dev);
131 err = libusb_get_device_descriptor(dev, &desc);
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));
145 static int carlusb_get_dev(struct carlu *ar, bool reset)
148 libusb_device_handle *dev;
149 libusb_device **list;
150 int ret, err, i, idx = -1;
152 ret = libusb_get_device_list(usb_ctx, &list);
154 err("usb device enum failed (%d)\n", ret);
158 for (i = 0; i < ret; i++) {
159 struct libusb_device_descriptor desc;
161 memset(&desc, 0, sizeof(desc));
162 err = libusb_get_device_descriptor(list[i], &desc);
166 idx = carlusb_is_ar9170(&desc);
170 list_for_each_entry(iter, &active_dev_list, dev_list) {
171 if (carlusb_is_dev(iter, list[i])) {
180 err = libusb_open(list[i], &dev);
182 err("failed to open device (%d)\n", err);
186 err = libusb_kernel_driver_active(dev, 0);
191 err("failed to aquire exculusive access (%d).\n", err);
196 err = libusb_reset_device(dev);
198 err("failed to reset device (%d)\n", err);
203 err = libusb_claim_interface(dev, 0);
205 dbg(">device is now under our control.\n");
208 err("failed to claim device (%d)\n", err);
220 list_add_tail(&ar->dev_list, &active_dev_list);
226 libusb_free_device_list(list, 1);
230 static void carlusb_tx_cb(struct carlu *ar,
234 ar->tx_cb(ar, frame);
236 ar->tx_octets += frame->len;
238 carlu_free_frame(ar, frame);
241 static void carlusb_zap_queues(struct carlu *ar)
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);
251 SDL_mutexV(ar->tx_queue_lock);
254 static void carlusb_free_driver(struct carlu *ar)
257 if (ar->event_pipe[0] > -1)
258 close(ar->event_pipe[0]);
260 if (ar->event_pipe[1] > -1)
261 close(ar->event_pipe[1]);
263 carlusb_zap_queues(ar);
264 carlfw_release(ar->fw);
268 libusb_release_interface(ar->dev, 0);
269 libusb_close(ar->dev);
272 carlu_free_driver(ar);
276 static int carlusb_init(struct carlu *ar)
278 init_list_head(&ar->tx_queue);
279 ar->tx_queue_lock = SDL_CreateMutex();
280 ar->event_pipe[0] = ar->event_pipe[1] = -1;
285 static struct carlu *carlusb_open(void)
290 tmp = carlu_alloc_driver(sizeof(*tmp));
294 err = carlusb_init(tmp);
298 err = carlusb_get_dev(tmp, true);
305 carlusb_free_driver(tmp);
309 static void carlusb_cancel_rings(struct carlu *ar)
313 for (i = 0; i < ARRAY_SIZE(ar->rx_ring); i++)
314 libusb_cancel_transfer(ar->rx_ring[i]);
316 libusb_cancel_transfer(ar->rx_interrupt);
319 static void carlusb_free_rings(struct carlu *ar)
323 for (i = 0; i < ARRAY_SIZE(ar->rx_ring); i++)
324 libusb_free_transfer(ar->rx_ring[i]);
326 libusb_free_transfer(ar->rx_interrupt);
329 static void carlusb_destroy(struct carlu *ar)
331 int event_thread_status;
333 dbg("==>release device.\n");
335 ar->stop_event_polling = true;
337 carlusb_cancel_rings(ar);
339 SDL_WaitThread(ar->event_thread, &event_thread_status);
341 carlusb_free_rings(ar);
342 list_del(&ar->dev_list);
345 static void carlusb_tx_bulk_cb(struct libusb_transfer *transfer);
347 static void carlusb_tx_pending(struct carlu *ar)
350 struct libusb_transfer *urb;
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))
360 urb = libusb_alloc_transfer(0);
364 frame = list_first_entry(&ar->tx_queue, struct frame, dcb.list);
365 list_del(&frame->dcb.list);
368 struct ar9170_stream *tx_stream;
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);
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);
378 /* FIXME: ZERO_PACKET support! */
379 urb->flags |= LIBUSB_TRANSFER_FREE_TRANSFER;
380 /* urb->flags |= LIBUSB_TRANSFER_ZERO_PACKET; */
381 frame->dev = (void *) ar;
384 err = libusb_submit_transfer(urb);
386 err("->usb bulk tx submit failed (%d).\n", err);
387 libusb_free_transfer(urb);
391 SDL_mutexV(ar->tx_queue_lock);
395 void carlusb_tx(struct carlu *ar, struct frame *frame)
397 BUG_ON(SDL_mutexP(ar->tx_queue_lock) != 0);
399 list_add_tail(&frame->dcb.list, &ar->tx_queue);
400 SDL_mutexV(ar->tx_queue_lock);
402 carlusb_tx_pending(ar);
405 static void carlusb_tx_bulk_cb(struct libusb_transfer *transfer)
407 struct frame *frame = (void *) transfer->user_data;
408 struct carlu *ar = (void *) frame->dev;
410 BUG_ON(SDL_mutexP(ar->tx_queue_lock) != 0);
412 SDL_mutexV(ar->tx_queue_lock);
415 frame_pull(frame, 4);
417 carlusb_tx_cb(ar, frame);
418 carlusb_tx_pending(ar);
421 static void carlusb_rx_interrupt_cb(struct libusb_transfer *transfer)
423 struct carlu *ar = (void *) transfer->user_data;
426 switch (transfer->status) {
427 case LIBUSB_TRANSFER_COMPLETED:
428 carlu_handle_command(ar, transfer->buffer, transfer->actual_length);
431 case LIBUSB_TRANSFER_CANCELLED:
435 err("==>rx_irq urb died (%d)\n", transfer->status);
439 err = libusb_submit_transfer(transfer);
441 err("==>rx_irq urb resubmit failed (%d)\n", err);
444 static void carlusb_rx_bulk_cb(struct libusb_transfer *transfer)
446 struct frame *frame = (void *) transfer->user_data;
447 struct carlu *ar = (void *) frame->dev;
450 switch (transfer->status) {
451 case LIBUSB_TRANSFER_COMPLETED:
452 frame_put(frame, transfer->actual_length);
456 frame_trim(frame, 0);
459 case LIBUSB_TRANSFER_CANCELLED:
463 err("==>rx_bulk urb died (%d)\n", transfer->status);
467 err = libusb_submit_transfer(transfer);
469 err("->rx_bulk urb resubmit failed (%d)\n", err);
472 static int carlusb_initialize_rxirq(struct carlu *ar)
476 ar->rx_interrupt = libusb_alloc_transfer(0);
477 if (ar->rx_interrupt == NULL) {
478 err("==>cannot alloc rx interrupt urb\n");
482 libusb_fill_interrupt_transfer(ar->rx_interrupt, ar->dev, AR9170_EP_IRQ,
483 (unsigned char *)&ar->irq_buf, sizeof(ar->irq_buf),
484 carlusb_rx_interrupt_cb, (void *) ar, 0);
486 err = libusb_submit_transfer(ar->rx_interrupt);
488 err("==>failed to submit rx interrupt (%d)\n", err);
492 dbg("rx interrupt is now operational.\n");
496 static int carlusb_initialize_rxrings(struct carlu *ar)
502 for (i = 0; i < ARRAY_SIZE(ar->rx_ring); i++) {
503 tmp = frame_alloc(ar->rx_max);
507 tmp->dev = (void *) ar;
509 ar->rx_ring[i] = libusb_alloc_transfer(0);
510 if (ar->rx_ring[i] == NULL) {
515 libusb_fill_bulk_transfer(ar->rx_ring[i], ar->dev,
516 AR9170_EP_RX, (unsigned char *)tmp->data,
517 ar->rx_max, carlusb_rx_bulk_cb, (void *)tmp, 0);
519 err = libusb_submit_transfer(ar->rx_ring[i]);
521 err("==>failed to submit rx buld urb (%d)\n", err);
526 dbg("rx ring is now ready to receive.\n");
530 static int carlusb_load_firmware(struct carlu *ar)
534 dbg("loading firmware...\n");
536 ar->fw = carlfw_load(CARL9170_FIRMWARE_FILE);
537 if (IS_ERR_OR_NULL(ar->fw))
538 return PTR_ERR(ar->fw);
540 ret = carlu_fw_check(ar);
544 ret = carlusb_fw_check(ar);
551 static int carlusb_upload_firmware(struct carlu *ar, bool boot)
553 uint32_t addr = 0x200000;
558 dbg("initiating firmware image upload procedure.\n");
560 buf = carlfw_get_fw(ar->fw, &len);
561 if (IS_ERR_OR_NULL(buf))
564 if (ar->miniboot_size) {
565 dbg("Miniboot firmware size:%d\n", ar->miniboot_size);
566 len -= ar->miniboot_size;
567 buf += ar->miniboot_size;
571 int blocklen = len > 4096 ? 4096 : len;
573 ret = libusb_control_transfer(ar->dev, 0x40, 0x30, addr >> 8, 0, buf, blocklen, 1000);
574 if (ret != blocklen && ret != LIBUSB_ERROR_TIMEOUT) {
575 err("==>firmware upload failed (%d)\n", ret);
576 return -EXIT_FAILURE;
579 dbg("uploaded %d bytes to start address 0x%04x.\n", blocklen, addr);
587 ret = libusb_control_transfer(ar->dev, 0x40, 0x31, 0, 0, NULL, 0, 5000);
589 err("unable to boot firmware (%d)\n", ret);
590 return -EXIT_FAILURE;
593 /* give the firmware some time to reset & reboot */
597 * since the device did a full usb reset,
598 * we have to get a new "dev".
600 libusb_release_interface(ar->dev, 0);
601 libusb_close(ar->dev);
603 list_del(&ar->dev_list);
605 ret = carlusb_get_dev(ar, false);
611 int carlusb_cmd_async(struct carlu *ar, struct carl9170_cmd *cmd,
614 struct libusb_transfer *urb;
617 if (cmd->hdr.len > (CARL9170_MAX_CMD_LEN - 4)) {
618 err("|-> too much payload\n");
623 if (cmd->hdr.len % 4) {
624 err("|-> invalid command length\n");
629 ret = libusb_interrupt_transfer(ar->dev, AR9170_EP_CMD, (void *) cmd, cmd->hdr.len + 4, &send, 32);
631 err("OID:0x%.2x failed due to (%d) (%d).\n", cmd->hdr.cmd, ret, send);
632 print_hex_dump_bytes(ERROR, "CMD:", cmd, cmd->hdr.len);
642 int carlusb_cmd(struct carlu *ar, uint8_t oid,
643 uint8_t *cmd, size_t clen,
644 uint8_t *rsp, size_t rlen)
648 if (clen > (CARL9170_MAX_CMD_LEN - 4)) {
649 err("|-> OID:0x%.2x has too much payload (%d octs)\n", oid, (int)clen);
653 ret = SDL_mutexP(ar->resp_lock);
655 err("failed to acquire resp_lock.\n");
656 print_hex_dump_bytes(ERROR, "CMD:", ar->cmd.buf, clen);
660 ar->cmd.cmd.hdr.len = clen;
661 ar->cmd.cmd.hdr.cmd = oid;
662 /* buf[2] & buf[3] are padding */
663 if (clen && cmd != (uint8_t *)(&ar->cmd.cmd.data))
664 memcpy(&ar->cmd.cmd.data, cmd, clen);
666 ar->resp_buf = (uint8_t *)rsp;
669 ret = carlusb_cmd_async(ar, &ar->cmd.cmd, false);
671 err("OID:0x%.2x failed due to (%d) (%d).\n", oid, ret, send);
672 print_hex_dump_bytes(ERROR, "CMD:", ar->cmd.buf, clen);
673 SDL_mutexV(ar->resp_lock);
677 ret = SDL_CondWaitTimeout(ar->resp_pend, ar->resp_lock, 100);
679 err("|-> OID:0x%.2x timed out %d.\n", oid, ret);
685 SDL_mutexV(ar->resp_lock);
689 struct carlu *carlusb_probe(void)
698 ret = carlusb_show_devinfo(ar);
702 ret = carlusb_load_firmware(ar);
706 ret = pipe(ar->event_pipe);
710 ar->stop_event_polling = false;
711 ar->event_thread = SDL_CreateThread(carlusb_event_thread, ar);
713 ret = carlusb_upload_firmware(ar, true);
717 ret = carlusb_initialize_rxirq(ar);
721 ret = carlusb_initialize_rxrings(ar);
725 ret = carlu_cmd_echo(ar, 0x44110dee);
727 err("echo test failed...\n");
731 info("firmware is active and running.\n");
741 carlusb_free_driver(ar);
742 err("usb device rendezvous failed (%d).\n", ret);
747 void carlusb_close(struct carlu *ar)
749 carlu_cmd_reboot(ar);
752 carlusb_free_driver(ar);
755 int carlusb_print_known_devices(void)
761 info("==> dumping known device list <==\n");
762 for (i = 0; i < ARRAY_SIZE(dev_list); i++) {
763 info("Vendor:\"%-9s\" Product:\"%-26s\" => USBID:[0x%04x:0x%04x]\n",
764 dev_list[i].vendor_name, dev_list[i].product_name,
765 dev_list[i].idVendor, dev_list[i].idProduct);
767 info("==> end of device list <==\n");
776 ret = libusb_init(&usb_ctx);
778 err("failed to initialize usb subsystem (%d)\n", ret);
782 /* like a silent chatterbox! */
783 libusb_set_debug(usb_ctx, 2);
790 libusb_exit(usb_ctx);