source: mainline/uspace/drv/nic/ar9271/ath_usb.c@ 59fa7ab

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 59fa7ab was 59fa7ab, checked in by Jan Kolarik <kolarik@…>, 11 years ago

Added all forgotten files

  • Property mode set to 100644
File size: 5.7 KB
Line 
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
39#include "ath_usb.h"
40
41static int ath_usb_send_ctrl_message(ath_t *ath, void *buffer,
42 size_t buffer_size);
43
44static int ath_usb_read_ctrl_message(ath_t *ath, void *buffer,
45 size_t buffer_size, size_t *transferred_size);
46
47static int ath_usb_send_data_message(ath_t *ath, void *buffer,
48 size_t buffer_size);
49
50static int ath_usb_read_data_message(ath_t *ath, void *buffer,
51 size_t buffer_size, size_t *transferred_size);
52
53static ath_ops_t ath_usb_ops = {
54 .send_ctrl_message = ath_usb_send_ctrl_message,
55 .read_ctrl_message = ath_usb_read_ctrl_message,
56 .send_data_message = ath_usb_send_data_message,
57 .read_data_message = ath_usb_read_data_message
58};
59
60/**
61 * Initialize Atheros WiFi USB device.
62 *
63 * @param ath Generic Atheros WiFi device structure.
64 * @param usb_device Connected USB device.
65 *
66 * @return EOK if succeed, negative error code otherwise.
67 */
68int ath_usb_init(ath_t *ath, usb_device_t *usb_device)
69{
70 ath_usb_t *ath_usb = malloc(sizeof(ath_usb_t));
71 if (!ath_usb) {
72 usb_log_error("Failed to allocate memory for ath usb device "
73 "structure.\n");
74 return ENOMEM;
75 }
76
77 ath_usb->usb_device = usb_device;
78
79 /* TODO: Assign by iterating over pipes. */
80 ath_usb->output_data_pipe_number = 0;
81 ath_usb->input_data_pipe_number = 1;
82 ath_usb->input_ctrl_pipe_number = 2;
83 ath_usb->output_ctrl_pipe_number = 3;
84
85 ath->ctrl_response_length = 64;
86 ath->data_response_length = 512;
87
88 ath->specific_data = ath_usb;
89 ath->ops = &ath_usb_ops;
90
91 return EOK;
92}
93
94/**
95 * Send control message.
96 *
97 * @param ath Generic Atheros WiFi device structure.
98 * @param buffer Buffer with data to send.
99 * @param buffer_size Buffer size.
100 *
101 * @return EOK if succeed, negative error code otherwise.
102 */
103static int ath_usb_send_ctrl_message(ath_t *ath, void *buffer,
104 size_t buffer_size)
105{
106 ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
107 usb_pipe_t *pipe =
108 &ath_usb->usb_device->pipes[ath_usb->output_ctrl_pipe_number].
109 pipe;
110
111 return usb_pipe_write(pipe, buffer, buffer_size);
112}
113
114/**
115 * Read control message.
116 *
117 * @param ath Generic Atheros WiFi device structure.
118 * @param buffer Buffer with data to send.
119 * @param buffer_size Buffer size.
120 * @param transferred_size Real size of read data.
121 *
122 * @return EOK if succeed, negative error code otherwise.
123 */
124static int ath_usb_read_ctrl_message(ath_t *ath, void *buffer,
125 size_t buffer_size, size_t *transferred_size)
126{
127 ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
128 usb_pipe_t *pipe =
129 &ath_usb->usb_device->pipes[ath_usb->input_ctrl_pipe_number].
130 pipe;
131
132 return usb_pipe_read(pipe, buffer, buffer_size, transferred_size);
133}
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 */
144static int ath_usb_send_data_message(ath_t *ath, void *buffer,
145 size_t buffer_size)
146{
147 size_t complete_buffer_size = buffer_size +
148 sizeof(ath_usb_data_header_t);
149 void *complete_buffer = malloc(complete_buffer_size);
150 memcpy(complete_buffer + sizeof(ath_usb_data_header_t),
151 buffer, buffer_size);
152
153 ath_usb_data_header_t *data_header =
154 (ath_usb_data_header_t *) complete_buffer;
155 data_header->length = host2uint16_t_le(buffer_size);
156 data_header->tag = host2uint16_t_le(TX_TAG);
157
158 ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
159 usb_pipe_t *pipe =
160 &ath_usb->usb_device->pipes[ath_usb->output_data_pipe_number].
161 pipe;
162
163 int ret_val = usb_pipe_write(pipe, complete_buffer,
164 complete_buffer_size);
165
166 free(complete_buffer);
167
168 return ret_val;
169}
170
171/**
172 * Read data message.
173 *
174 * @param ath Generic Atheros WiFi device structure.
175 * @param buffer Buffer with data to send.
176 * @param buffer_size Buffer size.
177 * @param transferred_size Real size of read data.
178 *
179 * @return EOK if succeed, negative error code otherwise.
180 */
181static int ath_usb_read_data_message(ath_t *ath, void *buffer,
182 size_t buffer_size, size_t *transferred_size)
183{
184 ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
185 usb_pipe_t *pipe =
186 &ath_usb->usb_device->pipes[ath_usb->input_data_pipe_number].
187 pipe;
188
189 return usb_pipe_read(pipe, buffer, buffer_size,
190 transferred_size);
191}
Note: See TracBrowser for help on using the repository browser.