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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fb1007ef was fb1007ef, checked in by Jan Vesely <jano.vesely@…>, 10 years ago

ar9271: Add missing headers

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[59fa7ab]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>
[fb1007ef]38#include <errno.h>
[59fa7ab]39#include "ath_usb.h"
40
[8a64320e]41static int ath_usb_send_ctrl_message(ath_t *, void *, size_t);
42static int ath_usb_read_ctrl_message(ath_t *, void *, size_t, size_t *);
43static int ath_usb_send_data_message(ath_t *, void *, size_t);
44static int ath_usb_read_data_message(ath_t *, void *, size_t, size_t *);
[59fa7ab]45
46static 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
[8a64320e]53/** Initialize Atheros WiFi USB device.
54 *
[59fa7ab]55 * @param ath Generic Atheros WiFi device structure.
[8a64320e]56 * @param usb_device Connected USB device.
57 *
[59fa7ab]58 * @return EOK if succeed, negative error code otherwise.
[8a64320e]59 *
[59fa7ab]60 */
61int 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
[8a64320e]87/** Send control message.
88 *
89 * @param ath Generic Atheros WiFi device structure.
90 * @param buffer Buffer with data to send.
[59fa7ab]91 * @param buffer_size Buffer size.
[8a64320e]92 *
[59fa7ab]93 * @return EOK if succeed, negative error code otherwise.
[8a64320e]94 *
[59fa7ab]95 */
[8a64320e]96static int ath_usb_send_ctrl_message(ath_t *ath, void *buffer,
97 size_t buffer_size)
[59fa7ab]98{
99 ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
[8a64320e]100 usb_pipe_t *pipe =
101 &ath_usb->usb_device->pipes[ath_usb->output_ctrl_pipe_number].pipe;
[59fa7ab]102
103 return usb_pipe_write(pipe, buffer, buffer_size);
104}
105
[8a64320e]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.
[59fa7ab]111 * @param transferred_size Real size of read data.
[8a64320e]112 *
[59fa7ab]113 * @return EOK if succeed, negative error code otherwise.
[8a64320e]114 *
[59fa7ab]115 */
[8a64320e]116static int ath_usb_read_ctrl_message(ath_t *ath, void *buffer,
117 size_t buffer_size, size_t *transferred_size)
[59fa7ab]118{
119 ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
[8a64320e]120 usb_pipe_t *pipe =
121 &ath_usb->usb_device->pipes[ath_usb->input_ctrl_pipe_number].pipe;
[59fa7ab]122
123 return usb_pipe_read(pipe, buffer, buffer_size, transferred_size);
124}
125
[8a64320e]126/** Send data message.
127 *
128 * @param ath Generic Atheros WiFi device structure.
129 * @param buffer Buffer with data to send.
[59fa7ab]130 * @param buffer_size Buffer size.
[8a64320e]131 *
[59fa7ab]132 * @return EOK if succeed, negative error code otherwise.
[8a64320e]133 *
[59fa7ab]134 */
[8a64320e]135static int ath_usb_send_data_message(ath_t *ath, void *buffer,
136 size_t buffer_size)
[59fa7ab]137{
[8a64320e]138 size_t complete_buffer_size = buffer_size +
139 sizeof(ath_usb_data_header_t);
[59fa7ab]140 void *complete_buffer = malloc(complete_buffer_size);
[8a64320e]141 memcpy(complete_buffer + sizeof(ath_usb_data_header_t),
142 buffer, buffer_size);
[59fa7ab]143
[8a64320e]144 ath_usb_data_header_t *data_header =
145 (ath_usb_data_header_t *) complete_buffer;
[59fa7ab]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;
[8a64320e]150 usb_pipe_t *pipe =
151 &ath_usb->usb_device->pipes[ath_usb->output_data_pipe_number].pipe;
[59fa7ab]152
[8a64320e]153 int ret_val = usb_pipe_write(pipe, complete_buffer,
154 complete_buffer_size);
[59fa7ab]155
156 free(complete_buffer);
157
158 return ret_val;
159}
160
[8a64320e]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.
[59fa7ab]166 * @param transferred_size Real size of read data.
[8a64320e]167 *
[59fa7ab]168 * @return EOK if succeed, negative error code otherwise.
[8a64320e]169 *
[59fa7ab]170 */
[8a64320e]171static int ath_usb_read_data_message(ath_t *ath, void *buffer,
172 size_t buffer_size, size_t *transferred_size)
[59fa7ab]173{
174 ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
[8a64320e]175 usb_pipe_t *pipe =
176 &ath_usb->usb_device->pipes[ath_usb->input_data_pipe_number].pipe;
[59fa7ab]177
[8a64320e]178 return usb_pipe_read(pipe, buffer, buffer_size,
179 transferred_size);
180}
Note: See TracBrowser for help on using the repository browser.