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 <errno.h>
|
---|
39 | #include "ath_usb.h"
|
---|
40 |
|
---|
41 | static int ath_usb_send_ctrl_message(ath_t *, void *, size_t);
|
---|
42 | static int ath_usb_read_ctrl_message(ath_t *, void *, size_t, size_t *);
|
---|
43 | static int ath_usb_send_data_message(ath_t *, void *, size_t);
|
---|
44 | static int ath_usb_read_data_message(ath_t *, void *, size_t, size_t *);
|
---|
45 |
|
---|
46 | static ath_ops_t ath_usb_ops = {
|
---|
47 | .send_ctrl_message = ath_usb_send_ctrl_message,
|
---|
48 | .read_ctrl_message = ath_usb_read_ctrl_message,
|
---|
49 | .send_data_message = ath_usb_send_data_message,
|
---|
50 | .read_data_message = ath_usb_read_data_message
|
---|
51 | };
|
---|
52 |
|
---|
53 | /** Initialize Atheros WiFi USB device.
|
---|
54 | *
|
---|
55 | * @param ath Generic Atheros WiFi device structure.
|
---|
56 | * @param usb_device Connected USB device.
|
---|
57 | *
|
---|
58 | * @return EOK if succeed, negative error code otherwise.
|
---|
59 | *
|
---|
60 | */
|
---|
61 | int ath_usb_init(ath_t *ath, usb_device_t *usb_device, const usb_endpoint_description_t **endpoints)
|
---|
62 | {
|
---|
63 | ath_usb_t *ath_usb = malloc(sizeof(ath_usb_t));
|
---|
64 | if (!ath_usb) {
|
---|
65 | usb_log_error("Failed to allocate memory for ath usb device "
|
---|
66 | "structure.\n");
|
---|
67 | return ENOMEM;
|
---|
68 | }
|
---|
69 |
|
---|
70 | ath_usb->usb_device = usb_device;
|
---|
71 |
|
---|
72 | int rc;
|
---|
73 |
|
---|
74 | #define _MAP_EP(target, ep_no) do {\
|
---|
75 | usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep_desc(usb_device, endpoints[ep_no]);\
|
---|
76 | if (!epm || !epm->present) {\
|
---|
77 | usb_log_error("Failed to map endpoint: " #ep_no ".");\
|
---|
78 | rc = ENOENT;\
|
---|
79 | goto err_ath_usb;\
|
---|
80 | }\
|
---|
81 | target = &epm->pipe;\
|
---|
82 | } while (0);
|
---|
83 |
|
---|
84 | _MAP_EP(ath_usb->output_data_pipe, 0);
|
---|
85 | _MAP_EP(ath_usb->input_data_pipe, 1);
|
---|
86 | _MAP_EP(ath_usb->input_ctrl_pipe, 2);
|
---|
87 | _MAP_EP(ath_usb->output_ctrl_pipe, 3);
|
---|
88 |
|
---|
89 | #undef _MAP_EP
|
---|
90 |
|
---|
91 | ath->ctrl_response_length = 64;
|
---|
92 | ath->data_response_length = 512;
|
---|
93 |
|
---|
94 | ath->specific_data = ath_usb;
|
---|
95 | ath->ops = &ath_usb_ops;
|
---|
96 |
|
---|
97 | return EOK;
|
---|
98 | err_ath_usb:
|
---|
99 | free(ath_usb);
|
---|
100 | return rc;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Send control message.
|
---|
104 | *
|
---|
105 | * @param ath Generic Atheros WiFi device structure.
|
---|
106 | * @param buffer Buffer with data to send.
|
---|
107 | * @param buffer_size Buffer size.
|
---|
108 | *
|
---|
109 | * @return EOK if succeed, negative error code otherwise.
|
---|
110 | *
|
---|
111 | */
|
---|
112 | static int ath_usb_send_ctrl_message(ath_t *ath, void *buffer,
|
---|
113 | size_t buffer_size)
|
---|
114 | {
|
---|
115 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
116 | return usb_pipe_write(ath_usb->output_ctrl_pipe, buffer, buffer_size);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Read control message.
|
---|
120 | *
|
---|
121 | * @param ath Generic Atheros WiFi device structure.
|
---|
122 | * @param buffer Buffer with data to send.
|
---|
123 | * @param buffer_size Buffer size.
|
---|
124 | * @param transferred_size Real size of read data.
|
---|
125 | *
|
---|
126 | * @return EOK if succeed, negative error code otherwise.
|
---|
127 | *
|
---|
128 | */
|
---|
129 | static int ath_usb_read_ctrl_message(ath_t *ath, void *buffer,
|
---|
130 | size_t buffer_size, size_t *transferred_size)
|
---|
131 | {
|
---|
132 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
133 | return usb_pipe_read(ath_usb->input_ctrl_pipe, buffer, buffer_size, transferred_size);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** Send data message.
|
---|
137 | *
|
---|
138 | * @param ath Generic Atheros WiFi device structure.
|
---|
139 | * @param buffer Buffer with data to send.
|
---|
140 | * @param buffer_size Buffer size.
|
---|
141 | *
|
---|
142 | * @return EOK if succeed, negative error code otherwise.
|
---|
143 | *
|
---|
144 | */
|
---|
145 | static int ath_usb_send_data_message(ath_t *ath, void *buffer,
|
---|
146 | size_t buffer_size)
|
---|
147 | {
|
---|
148 | size_t complete_buffer_size = buffer_size +
|
---|
149 | sizeof(ath_usb_data_header_t);
|
---|
150 | void *complete_buffer = malloc(complete_buffer_size);
|
---|
151 | memcpy(complete_buffer + sizeof(ath_usb_data_header_t),
|
---|
152 | buffer, buffer_size);
|
---|
153 |
|
---|
154 | ath_usb_data_header_t *data_header =
|
---|
155 | (ath_usb_data_header_t *) complete_buffer;
|
---|
156 | data_header->length = host2uint16_t_le(buffer_size);
|
---|
157 | data_header->tag = host2uint16_t_le(TX_TAG);
|
---|
158 |
|
---|
159 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
160 | int ret_val = usb_pipe_write(ath_usb->output_data_pipe, complete_buffer,
|
---|
161 | complete_buffer_size);
|
---|
162 |
|
---|
163 | free(complete_buffer);
|
---|
164 |
|
---|
165 | return ret_val;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** Read data message.
|
---|
169 | *
|
---|
170 | * @param ath Generic Atheros WiFi device structure.
|
---|
171 | * @param buffer Buffer with data to send.
|
---|
172 | * @param buffer_size Buffer size.
|
---|
173 | * @param transferred_size Real size of read data.
|
---|
174 | *
|
---|
175 | * @return EOK if succeed, negative error code otherwise.
|
---|
176 | *
|
---|
177 | */
|
---|
178 | static int ath_usb_read_data_message(ath_t *ath, void *buffer,
|
---|
179 | size_t buffer_size, size_t *transferred_size)
|
---|
180 | {
|
---|
181 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
182 | return usb_pipe_read(ath_usb->input_data_pipe, buffer, buffer_size, transferred_size);
|
---|
183 | }
|
---|