| 1 | /*
|
|---|
| 2 | * Copyright (c) 2015 Jan Kolarik
|
|---|
| 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @file ath_usb.c
|
|---|
| 31 | *
|
|---|
| 32 | * Implementation of Atheros USB wifi device functions.
|
|---|
| 33 | *
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <usb/dev/pipes.h>
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include "ath_usb.h"
|
|---|
| 41 |
|
|---|
| 42 | static errno_t ath_usb_send_ctrl_message(ath_t *, void *, size_t);
|
|---|
| 43 | static errno_t ath_usb_read_ctrl_message(ath_t *, void *, size_t, size_t *);
|
|---|
| 44 | static errno_t ath_usb_send_data_message(ath_t *, void *, size_t);
|
|---|
| 45 | static errno_t ath_usb_read_data_message(ath_t *, void *, size_t, size_t *);
|
|---|
| 46 |
|
|---|
| 47 | static ath_ops_t ath_usb_ops = {
|
|---|
| 48 | .send_ctrl_message = ath_usb_send_ctrl_message,
|
|---|
| 49 | .read_ctrl_message = ath_usb_read_ctrl_message,
|
|---|
| 50 | .send_data_message = ath_usb_send_data_message,
|
|---|
| 51 | .read_data_message = ath_usb_read_data_message
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | /** Initialize Atheros WiFi USB device.
|
|---|
| 55 | *
|
|---|
| 56 | * @param ath Generic Atheros WiFi device structure.
|
|---|
| 57 | * @param usb_device Connected USB device.
|
|---|
| 58 | *
|
|---|
| 59 | * @return EOK if succeed, error code otherwise.
|
|---|
| 60 | *
|
|---|
| 61 | */
|
|---|
| 62 | errno_t ath_usb_init(ath_t *ath, usb_device_t *usb_device, const usb_endpoint_description_t **endpoints)
|
|---|
| 63 | {
|
|---|
| 64 | ath_usb_t *ath_usb = malloc(sizeof(ath_usb_t));
|
|---|
| 65 | if (!ath_usb) {
|
|---|
| 66 | usb_log_error("Failed to allocate memory for ath usb device "
|
|---|
| 67 | "structure.\n");
|
|---|
| 68 | return ENOMEM;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | ath_usb->usb_device = usb_device;
|
|---|
| 72 |
|
|---|
| 73 | int rc;
|
|---|
| 74 |
|
|---|
| 75 | #define _MAP_EP(target, ep_no) do {\
|
|---|
| 76 | usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep_desc(usb_device, endpoints[ep_no]);\
|
|---|
| 77 | if (!epm || !epm->present) {\
|
|---|
| 78 | usb_log_error("Failed to map endpoint: " #ep_no ".");\
|
|---|
| 79 | rc = ENOENT;\
|
|---|
| 80 | goto err_ath_usb;\
|
|---|
| 81 | }\
|
|---|
| 82 | target = &epm->pipe;\
|
|---|
| 83 | } while (0);
|
|---|
| 84 |
|
|---|
| 85 | _MAP_EP(ath_usb->output_data_pipe, 0);
|
|---|
| 86 | _MAP_EP(ath_usb->input_data_pipe, 1);
|
|---|
| 87 | _MAP_EP(ath_usb->input_ctrl_pipe, 2);
|
|---|
| 88 | _MAP_EP(ath_usb->output_ctrl_pipe, 3);
|
|---|
| 89 |
|
|---|
| 90 | #undef _MAP_EP
|
|---|
| 91 |
|
|---|
| 92 | ath->ctrl_response_length = 64;
|
|---|
| 93 | ath->data_response_length = 512;
|
|---|
| 94 |
|
|---|
| 95 | ath->specific_data = ath_usb;
|
|---|
| 96 | ath->ops = &ath_usb_ops;
|
|---|
| 97 |
|
|---|
| 98 | return EOK;
|
|---|
| 99 | err_ath_usb:
|
|---|
| 100 | free(ath_usb);
|
|---|
| 101 | return rc;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /** Send control message.
|
|---|
| 105 | *
|
|---|
| 106 | * @param ath Generic Atheros WiFi device structure.
|
|---|
| 107 | * @param buffer Buffer with data to send.
|
|---|
| 108 | * @param buffer_size Buffer size.
|
|---|
| 109 | *
|
|---|
| 110 | * @return EOK if succeed, error code otherwise.
|
|---|
| 111 | *
|
|---|
| 112 | */
|
|---|
| 113 | static errno_t ath_usb_send_ctrl_message(ath_t *ath, void *buffer,
|
|---|
| 114 | size_t buffer_size)
|
|---|
| 115 | {
|
|---|
| 116 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
|---|
| 117 | return usb_pipe_write(ath_usb->output_ctrl_pipe, buffer, buffer_size);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /** Read control message.
|
|---|
| 121 | *
|
|---|
| 122 | * @param ath Generic Atheros WiFi device structure.
|
|---|
| 123 | * @param buffer Buffer with data to send.
|
|---|
| 124 | * @param buffer_size Buffer size.
|
|---|
| 125 | * @param transferred_size Real size of read data.
|
|---|
| 126 | *
|
|---|
| 127 | * @return EOK if succeed, error code otherwise.
|
|---|
| 128 | *
|
|---|
| 129 | */
|
|---|
| 130 | static errno_t ath_usb_read_ctrl_message(ath_t *ath, void *buffer,
|
|---|
| 131 | size_t buffer_size, size_t *transferred_size)
|
|---|
| 132 | {
|
|---|
| 133 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
|---|
| 134 | return usb_pipe_read(ath_usb->input_ctrl_pipe, buffer, buffer_size, transferred_size);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /** Send data message.
|
|---|
| 138 | *
|
|---|
| 139 | * @param ath Generic Atheros WiFi device structure.
|
|---|
| 140 | * @param buffer Buffer with data to send.
|
|---|
| 141 | * @param buffer_size Buffer size.
|
|---|
| 142 | *
|
|---|
| 143 | * @return EOK if succeed, error code otherwise.
|
|---|
| 144 | *
|
|---|
| 145 | */
|
|---|
| 146 | static errno_t ath_usb_send_data_message(ath_t *ath, void *buffer,
|
|---|
| 147 | size_t buffer_size)
|
|---|
| 148 | {
|
|---|
| 149 | size_t complete_buffer_size = buffer_size +
|
|---|
| 150 | sizeof(ath_usb_data_header_t);
|
|---|
| 151 | void *complete_buffer = malloc(complete_buffer_size);
|
|---|
| 152 | memcpy(complete_buffer + sizeof(ath_usb_data_header_t),
|
|---|
| 153 | buffer, buffer_size);
|
|---|
| 154 |
|
|---|
| 155 | ath_usb_data_header_t *data_header =
|
|---|
| 156 | (ath_usb_data_header_t *) complete_buffer;
|
|---|
| 157 | data_header->length = host2uint16_t_le(buffer_size);
|
|---|
| 158 | data_header->tag = host2uint16_t_le(TX_TAG);
|
|---|
| 159 |
|
|---|
| 160 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
|---|
| 161 | const errno_t ret_val = usb_pipe_write(ath_usb->output_data_pipe,
|
|---|
| 162 | complete_buffer, complete_buffer_size);
|
|---|
| 163 |
|
|---|
| 164 | free(complete_buffer);
|
|---|
| 165 |
|
|---|
| 166 | return ret_val;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /** Read data message.
|
|---|
| 170 | *
|
|---|
| 171 | * @param ath Generic Atheros WiFi device structure.
|
|---|
| 172 | * @param buffer Buffer with data to send.
|
|---|
| 173 | * @param buffer_size Buffer size.
|
|---|
| 174 | * @param transferred_size Real size of read data.
|
|---|
| 175 | *
|
|---|
| 176 | * @return EOK if succeed, error code otherwise.
|
|---|
| 177 | *
|
|---|
| 178 | */
|
|---|
| 179 | static errno_t ath_usb_read_data_message(ath_t *ath, void *buffer,
|
|---|
| 180 | size_t buffer_size, size_t *transferred_size)
|
|---|
| 181 | {
|
|---|
| 182 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
|---|
| 183 | return usb_pipe_read(ath_usb->input_data_pipe, buffer, buffer_size, transferred_size);
|
|---|
| 184 | }
|
|---|