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