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)
|
---|
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 | /* TODO: Assign by iterating over pipes. */
|
---|
73 | ath_usb->output_data_pipe_number = 0;
|
---|
74 | ath_usb->input_data_pipe_number = 1;
|
---|
75 | ath_usb->input_ctrl_pipe_number = 2;
|
---|
76 | ath_usb->output_ctrl_pipe_number = 3;
|
---|
77 |
|
---|
78 | ath->ctrl_response_length = 64;
|
---|
79 | ath->data_response_length = 512;
|
---|
80 |
|
---|
81 | ath->specific_data = ath_usb;
|
---|
82 | ath->ops = &ath_usb_ops;
|
---|
83 |
|
---|
84 | return EOK;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Send control message.
|
---|
88 | *
|
---|
89 | * @param ath Generic Atheros WiFi device structure.
|
---|
90 | * @param buffer Buffer with data to send.
|
---|
91 | * @param buffer_size Buffer size.
|
---|
92 | *
|
---|
93 | * @return EOK if succeed, negative error code otherwise.
|
---|
94 | *
|
---|
95 | */
|
---|
96 | static int ath_usb_send_ctrl_message(ath_t *ath, void *buffer,
|
---|
97 | size_t buffer_size)
|
---|
98 | {
|
---|
99 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
100 | usb_pipe_t *pipe =
|
---|
101 | &ath_usb->usb_device->pipes[ath_usb->output_ctrl_pipe_number].pipe;
|
---|
102 |
|
---|
103 | return usb_pipe_write(pipe, buffer, buffer_size);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Read control message.
|
---|
107 | *
|
---|
108 | * @param ath Generic Atheros WiFi device structure.
|
---|
109 | * @param buffer Buffer with data to send.
|
---|
110 | * @param buffer_size Buffer size.
|
---|
111 | * @param transferred_size Real size of read data.
|
---|
112 | *
|
---|
113 | * @return EOK if succeed, negative error code otherwise.
|
---|
114 | *
|
---|
115 | */
|
---|
116 | static int ath_usb_read_ctrl_message(ath_t *ath, void *buffer,
|
---|
117 | size_t buffer_size, size_t *transferred_size)
|
---|
118 | {
|
---|
119 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
120 | usb_pipe_t *pipe =
|
---|
121 | &ath_usb->usb_device->pipes[ath_usb->input_ctrl_pipe_number].pipe;
|
---|
122 |
|
---|
123 | return usb_pipe_read(pipe, buffer, buffer_size, transferred_size);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** Send data message.
|
---|
127 | *
|
---|
128 | * @param ath Generic Atheros WiFi device structure.
|
---|
129 | * @param buffer Buffer with data to send.
|
---|
130 | * @param buffer_size Buffer size.
|
---|
131 | *
|
---|
132 | * @return EOK if succeed, negative error code otherwise.
|
---|
133 | *
|
---|
134 | */
|
---|
135 | static int ath_usb_send_data_message(ath_t *ath, void *buffer,
|
---|
136 | size_t buffer_size)
|
---|
137 | {
|
---|
138 | size_t complete_buffer_size = buffer_size +
|
---|
139 | sizeof(ath_usb_data_header_t);
|
---|
140 | void *complete_buffer = malloc(complete_buffer_size);
|
---|
141 | memcpy(complete_buffer + sizeof(ath_usb_data_header_t),
|
---|
142 | buffer, buffer_size);
|
---|
143 |
|
---|
144 | ath_usb_data_header_t *data_header =
|
---|
145 | (ath_usb_data_header_t *) complete_buffer;
|
---|
146 | data_header->length = host2uint16_t_le(buffer_size);
|
---|
147 | data_header->tag = host2uint16_t_le(TX_TAG);
|
---|
148 |
|
---|
149 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
150 | usb_pipe_t *pipe =
|
---|
151 | &ath_usb->usb_device->pipes[ath_usb->output_data_pipe_number].pipe;
|
---|
152 |
|
---|
153 | int ret_val = usb_pipe_write(pipe, complete_buffer,
|
---|
154 | complete_buffer_size);
|
---|
155 |
|
---|
156 | free(complete_buffer);
|
---|
157 |
|
---|
158 | return ret_val;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Read data message.
|
---|
162 | *
|
---|
163 | * @param ath Generic Atheros WiFi device structure.
|
---|
164 | * @param buffer Buffer with data to send.
|
---|
165 | * @param buffer_size Buffer size.
|
---|
166 | * @param transferred_size Real size of read data.
|
---|
167 | *
|
---|
168 | * @return EOK if succeed, negative error code otherwise.
|
---|
169 | *
|
---|
170 | */
|
---|
171 | static int ath_usb_read_data_message(ath_t *ath, void *buffer,
|
---|
172 | size_t buffer_size, size_t *transferred_size)
|
---|
173 | {
|
---|
174 | ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
|
---|
175 | usb_pipe_t *pipe =
|
---|
176 | &ath_usb->usb_device->pipes[ath_usb->input_data_pipe_number].pipe;
|
---|
177 |
|
---|
178 | return usb_pipe_read(pipe, buffer, buffer_size,
|
---|
179 | transferred_size);
|
---|
180 | }
|
---|