[59fa7ab] | 1 | /*
|
---|
| 2 | * Copyright (c) 2015 Jan Kolarik
|
---|
[e0a5d4c] | 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
[59fa7ab] | 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>
|
---|
[38d150e] | 38 | #include <stdlib.h>
|
---|
[fb1007ef] | 39 | #include <errno.h>
|
---|
[59fa7ab] | 40 | #include "ath_usb.h"
|
---|
| 41 |
|
---|
[5a6cc679] | 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 *);
|
---|
[59fa7ab] | 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 |
|
---|
[8a64320e] | 54 | /** Initialize Atheros WiFi USB device.
|
---|
| 55 | *
|
---|
[59fa7ab] | 56 | * @param ath Generic Atheros WiFi device structure.
|
---|
[8a64320e] | 57 | * @param usb_device Connected USB device.
|
---|
| 58 | *
|
---|
[cde999a] | 59 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 60 | *
|
---|
[59fa7ab] | 61 | */
|
---|
[5a6cc679] | 62 | errno_t ath_usb_init(ath_t *ath, usb_device_t *usb_device, const usb_endpoint_description_t **endpoints)
|
---|
[59fa7ab] | 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 |
|
---|
[b3c39690] | 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
|
---|
[59fa7ab] | 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;
|
---|
[b3c39690] | 99 | err_ath_usb:
|
---|
| 100 | free(ath_usb);
|
---|
| 101 | return rc;
|
---|
[59fa7ab] | 102 | }
|
---|
| 103 |
|
---|
[8a64320e] | 104 | /** Send control message.
|
---|
| 105 | *
|
---|
| 106 | * @param ath Generic Atheros WiFi device structure.
|
---|
| 107 | * @param buffer Buffer with data to send.
|
---|
[59fa7ab] | 108 | * @param buffer_size Buffer size.
|
---|
[8a64320e] | 109 | *
|
---|
[cde999a] | 110 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 111 | *
|
---|
[59fa7ab] | 112 | */
|
---|
[5a6cc679] | 113 | static errno_t ath_usb_send_ctrl_message(ath_t *ath, void *buffer,
|
---|
[8a64320e] | 114 | size_t buffer_size)
|
---|
[59fa7ab] | 115 | {
|
---|
| 116 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
[b3c39690] | 117 | return usb_pipe_write(ath_usb->output_ctrl_pipe, buffer, buffer_size);
|
---|
[59fa7ab] | 118 | }
|
---|
| 119 |
|
---|
[8a64320e] | 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.
|
---|
[59fa7ab] | 125 | * @param transferred_size Real size of read data.
|
---|
[8a64320e] | 126 | *
|
---|
[cde999a] | 127 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 128 | *
|
---|
[59fa7ab] | 129 | */
|
---|
[5a6cc679] | 130 | static errno_t ath_usb_read_ctrl_message(ath_t *ath, void *buffer,
|
---|
[8a64320e] | 131 | size_t buffer_size, size_t *transferred_size)
|
---|
[59fa7ab] | 132 | {
|
---|
| 133 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
[b3c39690] | 134 | return usb_pipe_read(ath_usb->input_ctrl_pipe, buffer, buffer_size, transferred_size);
|
---|
[59fa7ab] | 135 | }
|
---|
| 136 |
|
---|
[8a64320e] | 137 | /** Send data message.
|
---|
| 138 | *
|
---|
| 139 | * @param ath Generic Atheros WiFi device structure.
|
---|
| 140 | * @param buffer Buffer with data to send.
|
---|
[59fa7ab] | 141 | * @param buffer_size Buffer size.
|
---|
[8a64320e] | 142 | *
|
---|
[cde999a] | 143 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 144 | *
|
---|
[59fa7ab] | 145 | */
|
---|
[5a6cc679] | 146 | static errno_t ath_usb_send_data_message(ath_t *ath, void *buffer,
|
---|
[8a64320e] | 147 | size_t buffer_size)
|
---|
[59fa7ab] | 148 | {
|
---|
[8a64320e] | 149 | size_t complete_buffer_size = buffer_size +
|
---|
| 150 | sizeof(ath_usb_data_header_t);
|
---|
[59fa7ab] | 151 | void *complete_buffer = malloc(complete_buffer_size);
|
---|
[8a64320e] | 152 | memcpy(complete_buffer + sizeof(ath_usb_data_header_t),
|
---|
| 153 | buffer, buffer_size);
|
---|
[58563585] | 154 |
|
---|
[8a64320e] | 155 | ath_usb_data_header_t *data_header =
|
---|
| 156 | (ath_usb_data_header_t *) complete_buffer;
|
---|
[59fa7ab] | 157 | data_header->length = host2uint16_t_le(buffer_size);
|
---|
| 158 | data_header->tag = host2uint16_t_le(TX_TAG);
|
---|
[58563585] | 159 |
|
---|
[59fa7ab] | 160 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
[5a6cc679] | 161 | const errno_t ret_val = usb_pipe_write(ath_usb->output_data_pipe,
|
---|
| 162 | complete_buffer, complete_buffer_size);
|
---|
[58563585] | 163 |
|
---|
[59fa7ab] | 164 | free(complete_buffer);
|
---|
[58563585] | 165 |
|
---|
[59fa7ab] | 166 | return ret_val;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[8a64320e] | 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.
|
---|
[59fa7ab] | 174 | * @param transferred_size Real size of read data.
|
---|
[8a64320e] | 175 | *
|
---|
[cde999a] | 176 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 177 | *
|
---|
[59fa7ab] | 178 | */
|
---|
[5a6cc679] | 179 | static errno_t ath_usb_read_data_message(ath_t *ath, void *buffer,
|
---|
[8a64320e] | 180 | size_t buffer_size, size_t *transferred_size)
|
---|
[59fa7ab] | 181 | {
|
---|
| 182 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
[b3c39690] | 183 | return usb_pipe_read(ath_usb->input_data_pipe, buffer, buffer_size, transferred_size);
|
---|
[8a64320e] | 184 | }
|
---|