source: mainline/uspace/drv/nic/ar9271/ar9271.c

Last change on this file was 5f97ef44, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Sleep is more natural as part of the fibril API.
(the implementation will move later)

  • Property mode set to 100644
File size: 25.0 KB
Line 
1/*
2 * Copyright (c) 2014 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 ar9271.c
30 *
31 * Driver for AR9271 USB WiFi dongle.
32 *
33 */
34
35#include <async.h>
36#include <ieee80211.h>
37#include <usb/classes/classes.h>
38#include <usb/dev/request.h>
39#include <usb/dev/poll.h>
40#include <usb/debug.h>
41#include <stdio.h>
42#include <ddf/interrupt.h>
43#include <errno.h>
44#include <str_error.h>
45#include <nic.h>
46#include <macros.h>
47#include "ath_usb.h"
48#include "wmi.h"
49#include "hw.h"
50#include "ar9271.h"
51
52#define NAME "ar9271"
53#define FIRMWARE_FILENAME "/drv/ar9271/ar9271.fw"
54
55const usb_endpoint_description_t usb_ar9271_out_bulk_endpoint_description = {
56 .transfer_type = USB_TRANSFER_BULK,
57 .direction = USB_DIRECTION_OUT,
58 .interface_class = USB_CLASS_VENDOR_SPECIFIC,
59 .interface_subclass = 0x0,
60 .interface_protocol = 0x0,
61 .flags = 0
62};
63
64const usb_endpoint_description_t usb_ar9271_in_bulk_endpoint_description = {
65 .transfer_type = USB_TRANSFER_BULK,
66 .direction = USB_DIRECTION_IN,
67 .interface_class = USB_CLASS_VENDOR_SPECIFIC,
68 .interface_subclass = 0x0,
69 .interface_protocol = 0x0,
70 .flags = 0
71};
72
73const usb_endpoint_description_t usb_ar9271_in_int_endpoint_description = {
74 .transfer_type = USB_TRANSFER_INTERRUPT,
75 .direction = USB_DIRECTION_IN,
76 .interface_class = USB_CLASS_VENDOR_SPECIFIC,
77 .interface_subclass = 0x0,
78 .interface_protocol = 0x0,
79 .flags = 0
80};
81
82const usb_endpoint_description_t usb_ar9271_out_int_endpoint_description = {
83 .transfer_type = USB_TRANSFER_INTERRUPT,
84 .direction = USB_DIRECTION_OUT,
85 .interface_class = USB_CLASS_VENDOR_SPECIFIC,
86 .interface_subclass = 0x0,
87 .interface_protocol = 0x0,
88 .flags = 0
89};
90
91/* Array of endpoints expected on the device, NULL terminated. */
92const usb_endpoint_description_t *endpoints[] = {
93 &usb_ar9271_out_bulk_endpoint_description,
94 &usb_ar9271_in_bulk_endpoint_description,
95 &usb_ar9271_in_int_endpoint_description,
96 &usb_ar9271_out_int_endpoint_description,
97 NULL
98};
99
100/* Callback when new device is to be controlled by this driver. */
101static errno_t ar9271_add_device(ddf_dev_t *);
102
103/* IEEE 802.11 callbacks */
104static errno_t ar9271_ieee80211_start(ieee80211_dev_t *);
105static errno_t ar9271_ieee80211_tx_handler(ieee80211_dev_t *, void *, size_t);
106static errno_t ar9271_ieee80211_set_freq(ieee80211_dev_t *, uint16_t);
107static errno_t ar9271_ieee80211_bssid_change(ieee80211_dev_t *, bool);
108static errno_t ar9271_ieee80211_key_config(ieee80211_dev_t *, ieee80211_key_config_t *,
109 bool);
110
111static driver_ops_t ar9271_driver_ops = {
112 .dev_add = ar9271_add_device
113};
114
115static driver_t ar9271_driver = {
116 .name = NAME,
117 .driver_ops = &ar9271_driver_ops
118};
119
120static ieee80211_ops_t ar9271_ieee80211_ops = {
121 .start = ar9271_ieee80211_start,
122 .tx_handler = ar9271_ieee80211_tx_handler,
123 .set_freq = ar9271_ieee80211_set_freq,
124 .bssid_change = ar9271_ieee80211_bssid_change,
125 .key_config = ar9271_ieee80211_key_config
126};
127
128static ieee80211_iface_t ar9271_ieee80211_iface;
129
130static errno_t ar9271_get_device_info(ddf_fun_t *, nic_device_info_t *);
131static errno_t ar9271_get_cable_state(ddf_fun_t *, nic_cable_state_t *);
132static errno_t ar9271_get_operation_mode(ddf_fun_t *, int *, nic_channel_mode_t *,
133 nic_role_t *);
134
135static nic_iface_t ar9271_ieee80211_nic_iface = {
136 .get_device_info = &ar9271_get_device_info,
137 .get_cable_state = &ar9271_get_cable_state,
138 .get_operation_mode = &ar9271_get_operation_mode
139};
140
141static ddf_dev_ops_t ar9271_ieee80211_dev_ops;
142
143/** Get device information.
144 *
145 */
146static errno_t ar9271_get_device_info(ddf_fun_t *dev, nic_device_info_t *info)
147{
148 assert(dev);
149 assert(info);
150
151 memset(info, 0, sizeof(nic_device_info_t));
152
153 info->vendor_id = 0x0cf3;
154 info->device_id = 0x9271;
155 str_cpy(info->vendor_name, NIC_VENDOR_MAX_LENGTH,
156 "Atheros Communications, Inc.");
157 str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH,
158 "AR9271");
159
160 return EOK;
161}
162
163/** Get cable state.
164 *
165 */
166static errno_t ar9271_get_cable_state(ddf_fun_t *fun, nic_cable_state_t *state)
167{
168 nic_t *nic_data = nic_get_from_ddf_fun(fun);
169 if (!nic_data)
170 return ENOENT;
171 ar9271_t *ar9271 = nic_get_specific(nic_data);
172 if (!ar9271)
173 return ENOENT;
174
175 if (ieee80211_is_connected(ar9271->ieee80211_dev))
176 *state = NIC_CS_PLUGGED;
177 else
178 *state = NIC_CS_UNPLUGGED;
179
180 return EOK;
181}
182
183/** Get operation mode of the device.
184 *
185 */
186static errno_t ar9271_get_operation_mode(ddf_fun_t *fun, int *speed,
187 nic_channel_mode_t *duplex, nic_role_t *role)
188{
189 *duplex = NIC_CM_FULL_DUPLEX;
190 *speed = 10;
191 *role = NIC_ROLE_UNKNOWN;
192
193 return EOK;
194}
195
196/** Set multicast frames acceptance mode.
197 *
198 */
199static errno_t ar9271_on_multicast_mode_change(nic_t *nic,
200 nic_multicast_mode_t mode, const nic_address_t *addr, size_t addr_cnt)
201{
202 switch (mode) {
203 case NIC_MULTICAST_BLOCKED:
204 /* TODO */
205 break;
206 case NIC_MULTICAST_LIST:
207 /* TODO */
208 break;
209 case NIC_MULTICAST_PROMISC:
210 /* TODO */
211 break;
212 default:
213 return ENOTSUP;
214 }
215
216 return EOK;
217}
218
219/** Set unicast frames acceptance mode.
220 *
221 */
222static errno_t ar9271_on_unicast_mode_change(nic_t *nic, nic_unicast_mode_t mode,
223 const nic_address_t *addr, size_t addr_cnt)
224{
225 switch (mode) {
226 case NIC_UNICAST_BLOCKED:
227 /* TODO */
228 break;
229 case NIC_UNICAST_DEFAULT:
230 /* TODO */
231 break;
232 case NIC_UNICAST_LIST:
233 /* TODO */
234 break;
235 case NIC_UNICAST_PROMISC:
236 /* TODO */
237 break;
238 default:
239 return ENOTSUP;
240 }
241
242 return EOK;
243}
244
245/** Set broadcast frames acceptance mode.
246 *
247 */
248static errno_t ar9271_on_broadcast_mode_change(nic_t *nic,
249 nic_broadcast_mode_t mode)
250{
251 switch (mode) {
252 case NIC_BROADCAST_BLOCKED:
253 /* TODO */
254 break;
255 case NIC_BROADCAST_ACCEPTED:
256 /* TODO */
257 break;
258 default:
259 return ENOTSUP;
260 }
261
262 return EOK;
263}
264
265static bool ar9271_rx_status_error(uint8_t status)
266{
267 return (status & AR9271_RX_ERROR_PHY) || (status & AR9271_RX_ERROR_CRC);
268}
269
270static errno_t ar9271_data_polling(void *arg)
271{
272 assert(arg);
273
274 ar9271_t *ar9271 = (ar9271_t *) arg;
275
276 size_t buffer_size = ar9271->ath_device->data_response_length;
277 void *buffer = malloc(buffer_size);
278
279 while (true) {
280 size_t transferred_size;
281 if (htc_read_data_message(ar9271->htc_device,
282 buffer, buffer_size, &transferred_size) == EOK) {
283 size_t strip_length =
284 sizeof(ath_usb_data_header_t) +
285 sizeof(htc_frame_header_t) +
286 sizeof(htc_rx_status_t);
287
288 if (transferred_size < strip_length)
289 continue;
290
291 ath_usb_data_header_t *data_header =
292 (ath_usb_data_header_t *) buffer;
293
294 /* Invalid packet. */
295 if (data_header->tag != uint16_t_le2host(RX_TAG))
296 continue;
297
298 htc_rx_status_t *rx_status =
299 (htc_rx_status_t *) ((void *) buffer +
300 sizeof(ath_usb_data_header_t) +
301 sizeof(htc_frame_header_t));
302
303 uint16_t data_length =
304 uint16_t_be2host(rx_status->data_length);
305
306 int16_t payload_length =
307 transferred_size - strip_length;
308
309 if (payload_length - data_length < 0)
310 continue;
311
312 if (ar9271_rx_status_error(rx_status->status))
313 continue;
314
315 void *strip_buffer = buffer + strip_length;
316
317 ieee80211_rx_handler(ar9271->ieee80211_dev,
318 strip_buffer,
319 payload_length);
320 }
321 }
322
323 free(buffer);
324
325 return EOK;
326}
327
328/** IEEE 802.11 handlers.
329 *
330 */
331static errno_t ar9271_ieee80211_set_freq(ieee80211_dev_t *ieee80211_dev,
332 uint16_t freq)
333{
334 assert(ieee80211_dev);
335
336 ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev);
337
338 wmi_send_command(ar9271->htc_device, WMI_DISABLE_INTR, NULL, 0, NULL);
339 wmi_send_command(ar9271->htc_device, WMI_DRAIN_TXQ_ALL, NULL, 0, NULL);
340 wmi_send_command(ar9271->htc_device, WMI_STOP_RECV, NULL, 0, NULL);
341
342 errno_t rc = hw_freq_switch(ar9271, freq);
343 if (rc != EOK) {
344 usb_log_error("Failed to HW switch frequency.\n");
345 return rc;
346 }
347
348 wmi_send_command(ar9271->htc_device, WMI_START_RECV, NULL, 0, NULL);
349
350 rc = hw_rx_init(ar9271);
351 if (rc != EOK) {
352 usb_log_error("Failed to initialize RX.\n");
353 return rc;
354 }
355
356 uint16_t htc_mode = host2uint16_t_be(1);
357 wmi_send_command(ar9271->htc_device, WMI_SET_MODE,
358 (uint8_t *) &htc_mode, sizeof(htc_mode), NULL);
359 wmi_send_command(ar9271->htc_device, WMI_ENABLE_INTR, NULL, 0, NULL);
360
361 return EOK;
362}
363
364static errno_t ar9271_ieee80211_bssid_change(ieee80211_dev_t *ieee80211_dev,
365 bool connected)
366{
367 assert(ieee80211_dev);
368
369 ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev);
370
371 if (connected) {
372 nic_address_t bssid;
373 ieee80211_query_bssid(ieee80211_dev, &bssid);
374
375 htc_sta_msg_t sta_msg;
376 memset(&sta_msg, 0, sizeof(htc_sta_msg_t));
377 sta_msg.is_vif_sta = 0;
378 sta_msg.max_ampdu =
379 host2uint16_t_be(1 << IEEE80211_MAX_AMPDU_FACTOR);
380 sta_msg.sta_index = 1;
381 sta_msg.vif_index = 0;
382 memcpy(&sta_msg.addr, bssid.address, ETH_ADDR);
383
384 wmi_send_command(ar9271->htc_device, WMI_NODE_CREATE,
385 (uint8_t *) &sta_msg, sizeof(sta_msg), NULL);
386
387 htc_rate_msg_t rate_msg;
388 memset(&rate_msg, 0, sizeof(htc_rate_msg_t));
389 rate_msg.sta_index = 1;
390 rate_msg.is_new = 1;
391 rate_msg.legacy_rates_count = ARRAY_SIZE(ieee80211bg_data_rates);
392 memcpy(&rate_msg.legacy_rates,
393 ieee80211bg_data_rates,
394 ARRAY_SIZE(ieee80211bg_data_rates));
395
396 wmi_send_command(ar9271->htc_device, WMI_RC_RATE_UPDATE,
397 (uint8_t *) &rate_msg, sizeof(rate_msg), NULL);
398
399 hw_set_rx_filter(ar9271, true);
400 } else {
401 uint8_t station_id = 1;
402 wmi_send_command(ar9271->htc_device, WMI_NODE_REMOVE,
403 &station_id, sizeof(station_id), NULL);
404
405 hw_set_rx_filter(ar9271, false);
406 }
407
408 hw_set_bssid(ar9271);
409
410 return EOK;
411}
412
413static errno_t ar9271_ieee80211_key_config(ieee80211_dev_t *ieee80211_dev,
414 ieee80211_key_config_t *key_conf, bool insert)
415{
416 assert(ieee80211_dev);
417
418 ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev);
419
420 if (insert) {
421 assert(key_conf);
422
423 uint32_t key[5];
424 uint32_t key_type;
425 uint32_t reg_ptr, mic_reg_ptr;
426 void *data_start;
427
428 nic_address_t bssid;
429 ieee80211_query_bssid(ieee80211_dev, &bssid);
430
431 switch (key_conf->suite) {
432 case IEEE80211_SECURITY_SUITE_WEP40:
433 key_type = AR9271_KEY_TABLE_TYPE_WEP40;
434 break;
435 case IEEE80211_SECURITY_SUITE_WEP104:
436 key_type = AR9271_KEY_TABLE_TYPE_WEP104;
437 break;
438 case IEEE80211_SECURITY_SUITE_TKIP:
439 key_type = AR9271_KEY_TABLE_TYPE_TKIP;
440 break;
441 case IEEE80211_SECURITY_SUITE_CCMP:
442 key_type = AR9271_KEY_TABLE_TYPE_CCMP;
443 break;
444 default:
445 key_type = -1;
446 }
447
448 uint8_t key_id =
449 (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_PAIRWISE) ?
450 AR9271_STA_KEY_INDEX : key_conf->id;
451
452 reg_ptr = AR9271_KEY_TABLE(key_id);
453 mic_reg_ptr = AR9271_KEY_TABLE(key_id + 64);
454 data_start = (void *) key_conf->data;
455
456 key[0] = uint32_t_le2host(*((uint32_t *) data_start));
457 key[1] = uint16_t_le2host(*((uint16_t *) (data_start + 4)));
458 key[2] = uint32_t_le2host(*((uint32_t *) (data_start + 6)));
459 key[3] = uint16_t_le2host(*((uint16_t *) (data_start + 10)));
460 key[4] = uint32_t_le2host(*((uint32_t *) (data_start + 12)));
461
462 if ((key_conf->suite == IEEE80211_SECURITY_SUITE_WEP40) ||
463 (key_conf->suite == IEEE80211_SECURITY_SUITE_WEP104))
464 key[4] &= 0xFF;
465
466 wmi_reg_write(ar9271->htc_device, reg_ptr + 0, key[0]);
467 wmi_reg_write(ar9271->htc_device, reg_ptr + 4, key[1]);
468 wmi_reg_write(ar9271->htc_device, reg_ptr + 8, key[2]);
469 wmi_reg_write(ar9271->htc_device, reg_ptr + 12, key[3]);
470 wmi_reg_write(ar9271->htc_device, reg_ptr + 16, key[4]);
471 wmi_reg_write(ar9271->htc_device, reg_ptr + 20, key_type);
472
473 uint32_t macl;
474 uint32_t mach;
475 if (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_PAIRWISE) {
476 data_start = (void *) bssid.address;
477 macl = uint32_t_le2host(*((uint32_t *) data_start));
478 mach = uint16_t_le2host(*((uint16_t *) (data_start + 4)));
479 } else {
480 macl = 0;
481 mach = 0;
482 }
483
484 macl >>= 1;
485 macl |= (mach & 1) << 31;
486 mach >>= 1;
487 mach |= 0x8000;
488
489 wmi_reg_write(ar9271->htc_device, reg_ptr + 24, macl);
490 wmi_reg_write(ar9271->htc_device, reg_ptr + 28, mach);
491
492 /* Setup MIC keys for TKIP. */
493 if (key_conf->suite == IEEE80211_SECURITY_SUITE_TKIP) {
494 uint32_t mic[5];
495 uint8_t *gen_mic = data_start + IEEE80211_TKIP_RX_MIC_OFFSET;
496 uint8_t *tx_mic;
497
498 if (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_GROUP)
499 tx_mic = gen_mic;
500 else
501 tx_mic = data_start + IEEE80211_TKIP_TX_MIC_OFFSET;
502
503 mic[0] = uint32_t_le2host(*((uint32_t *) gen_mic));
504 mic[1] = uint16_t_le2host(*((uint16_t *) (tx_mic + 2))) & 0xFFFF;
505 mic[2] = uint32_t_le2host(*((uint32_t *) (gen_mic + 4)));
506 mic[3] = uint16_t_le2host(*((uint16_t *) tx_mic)) & 0xFFFF;
507 mic[4] = uint32_t_le2host(*((uint32_t *) (tx_mic + 4)));
508
509 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 0, mic[0]);
510 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 4, mic[1]);
511 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 8, mic[2]);
512 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 12, mic[3]);
513 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 16, mic[4]);
514 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 20,
515 AR9271_KEY_TABLE_TYPE_CLR);
516
517 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 24, 0);
518 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 28, 0);
519 }
520
521 if (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_GROUP)
522 ieee80211_setup_key_confirm(ieee80211_dev, true);
523 } else {
524 /* TODO: Delete keys from device */
525 ieee80211_setup_key_confirm(ieee80211_dev, false);
526 }
527
528 return EOK;
529}
530
531static errno_t ar9271_ieee80211_tx_handler(ieee80211_dev_t *ieee80211_dev,
532 void *buffer, size_t buffer_size)
533{
534 assert(ieee80211_dev);
535
536 size_t complete_size;
537 size_t offset;
538 void *complete_buffer;
539 int endpoint;
540
541 ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev);
542
543 uint16_t frame_ctrl = *((uint16_t *) buffer);
544 if (ieee80211_is_data_frame(frame_ctrl)) {
545 offset = sizeof(htc_tx_data_header_t) +
546 sizeof(htc_frame_header_t);
547 complete_size = buffer_size + offset;
548 complete_buffer = malloc(complete_size);
549 memset(complete_buffer, 0, complete_size);
550
551 /*
552 * Because we handle just station mode yet, node ID and VIF ID
553 * are fixed.
554 */
555 htc_tx_data_header_t *data_header =
556 (htc_tx_data_header_t *)
557 (complete_buffer + sizeof(htc_frame_header_t));
558 data_header->data_type = HTC_DATA_NORMAL;
559 data_header->node_idx = 1;
560 data_header->vif_idx = 0;
561 data_header->cookie = 0;
562
563 if (ieee80211_query_using_key(ieee80211_dev)) {
564 data_header->keyix = AR9271_STA_KEY_INDEX;
565
566 int sec_suite =
567 ieee80211_get_pairwise_security(ieee80211_dev);
568
569 switch (sec_suite) {
570 case IEEE80211_SECURITY_SUITE_WEP40:
571 case IEEE80211_SECURITY_SUITE_WEP104:
572 data_header->key_type = AR9271_KEY_TYPE_WEP;
573 break;
574 case IEEE80211_SECURITY_SUITE_TKIP:
575 data_header->key_type = AR9271_KEY_TYPE_TKIP;
576 break;
577 case IEEE80211_SECURITY_SUITE_CCMP:
578 data_header->key_type = AR9271_KEY_TYPE_AES;
579 break;
580 }
581 } else {
582 data_header->key_type = 0;
583 data_header->keyix = 0xFF;
584 }
585
586 endpoint = ar9271->htc_device->endpoints.data_be_endpoint;
587 } else {
588 offset = sizeof(htc_tx_management_header_t) +
589 sizeof(htc_frame_header_t);
590 complete_size = buffer_size + offset;
591 complete_buffer = malloc(complete_size);
592 memset(complete_buffer, 0, complete_size);
593
594 /*
595 * Because we handle just station mode yet, node ID and VIF ID
596 * are fixed.
597 */
598 htc_tx_management_header_t *mgmt_header =
599 (htc_tx_management_header_t *)
600 (complete_buffer + sizeof(htc_frame_header_t));
601 mgmt_header->node_idx = 0;
602 mgmt_header->vif_idx = 0;
603 mgmt_header->cookie = 0;
604 mgmt_header->keyix = 0xFF;
605
606 endpoint = ar9271->htc_device->endpoints.mgmt_endpoint;
607 }
608
609 /* Copy IEEE802.11 data to new allocated buffer with HTC headers. */
610 memcpy(complete_buffer + offset, buffer, buffer_size);
611
612 htc_send_data_message(ar9271->htc_device, complete_buffer,
613 complete_size, endpoint);
614
615 free(complete_buffer);
616
617 return EOK;
618}
619
620static errno_t ar9271_ieee80211_start(ieee80211_dev_t *ieee80211_dev)
621{
622 assert(ieee80211_dev);
623
624 ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev);
625
626 wmi_send_command(ar9271->htc_device, WMI_FLUSH_RECV, NULL, 0, NULL);
627
628 errno_t rc = hw_reset(ar9271);
629 if (rc != EOK) {
630 usb_log_error("Failed to do HW reset.\n");
631 return rc;
632 }
633
634 uint16_t htc_mode = host2uint16_t_be(1);
635 wmi_send_command(ar9271->htc_device, WMI_SET_MODE,
636 (uint8_t *) &htc_mode, sizeof(htc_mode), NULL);
637 wmi_send_command(ar9271->htc_device, WMI_ATH_INIT, NULL, 0, NULL);
638 wmi_send_command(ar9271->htc_device, WMI_START_RECV, NULL, 0, NULL);
639 wmi_send_command(ar9271->htc_device, WMI_ENABLE_INTR, NULL, 0, NULL);
640
641 rc = hw_rx_init(ar9271);
642 if (rc != EOK) {
643 usb_log_error("Failed to initialize RX.\n");
644 return rc;
645 }
646
647 /* Send capability message to target. */
648 htc_cap_msg_t cap_msg;
649 cap_msg.ampdu_limit = host2uint32_t_be(0xffff);
650 cap_msg.ampdu_subframes = 0xff;
651 cap_msg.enable_coex = 0;
652 cap_msg.tx_chainmask = 0x1;
653
654 wmi_send_command(ar9271->htc_device, WMI_TARGET_IC_UPDATE,
655 (uint8_t *) &cap_msg, sizeof(cap_msg), NULL);
656
657 rc = htc_init_new_vif(ar9271->htc_device);
658 if (rc != EOK) {
659 usb_log_error("Failed to initialize new VIF.\n");
660 return rc;
661 }
662
663 /* Add data polling fibril. */
664 fid_t fibril = fibril_create(ar9271_data_polling, ar9271);
665 if (fibril == 0)
666 return ENOMEM;
667
668 fibril_add_ready(fibril);
669
670 ar9271->starting_up = false;
671 ieee80211_set_ready(ieee80211_dev, true);
672
673 usb_log_info("Device fully initialized.\n");
674
675 return EOK;
676}
677
678static errno_t ar9271_init(ar9271_t *ar9271, usb_device_t *usb_device, const usb_endpoint_description_t **endpoints)
679{
680 ar9271->starting_up = true;
681 ar9271->usb_device = usb_device;
682
683 fibril_mutex_initialize(&ar9271->ar9271_lock);
684
685 ar9271->ath_device = calloc(1, sizeof(ath_t));
686 if (!ar9271->ath_device) {
687 usb_log_error("Failed to allocate memory for ath device "
688 "structure.\n");
689 return ENOMEM;
690 }
691
692 errno_t rc = ath_usb_init(ar9271->ath_device, usb_device, endpoints);
693 if (rc != EOK) {
694 free(ar9271->ath_device);
695 usb_log_error("Failed to initialize ath device.\n");
696 return rc;
697 }
698
699 /* IEEE 802.11 framework structure initialization. */
700 ar9271->ieee80211_dev = ieee80211_device_create();
701 if (!ar9271->ieee80211_dev) {
702 free(ar9271->ath_device);
703 usb_log_error("Failed to allocate memory for IEEE80211 device "
704 "structure.\n");
705 return ENOMEM;
706 }
707
708 rc = ieee80211_device_init(ar9271->ieee80211_dev, ar9271->ddf_dev);
709 if (rc != EOK) {
710 free(ar9271->ieee80211_dev);
711 free(ar9271->ath_device);
712 usb_log_error("Failed to initialize IEEE80211 device structure."
713 "\n");
714 return rc;
715 }
716
717 ieee80211_set_specific(ar9271->ieee80211_dev, ar9271);
718
719 /* HTC device structure initialization. */
720 ar9271->htc_device = calloc(1, sizeof(htc_device_t));
721 if (!ar9271->htc_device) {
722 free(ar9271->ieee80211_dev);
723 free(ar9271->ath_device);
724 usb_log_error("Failed to allocate memory for HTC device "
725 "structure.\n");
726 return ENOMEM;
727 }
728
729 rc = htc_device_init(ar9271->ath_device, ar9271->ieee80211_dev,
730 ar9271->htc_device);
731 if (rc != EOK) {
732 free(ar9271->htc_device);
733 free(ar9271->ieee80211_dev);
734 free(ar9271->ath_device);
735 usb_log_error("Failed to initialize HTC device structure.\n");
736 return rc;
737 }
738
739 return EOK;
740}
741
742/** Upload firmware to WiFi device.
743 *
744 * @param ar9271 AR9271 device structure
745 *
746 * @return EOK if succeed, error code otherwise
747 *
748 */
749static errno_t ar9271_upload_fw(ar9271_t *ar9271)
750{
751 usb_device_t *usb_device = ar9271->usb_device;
752
753 /* TODO: Set by maximum packet size in pipe. */
754 static const size_t MAX_TRANSFER_SIZE = 512;
755
756 /* Load FW from file. */
757 FILE *fw_file = fopen(FIRMWARE_FILENAME, "rb");
758 if (fw_file == NULL) {
759 usb_log_error("Failed opening file with firmware.\n");
760 return ENOENT;
761 }
762
763 fseek(fw_file, 0, SEEK_END);
764 uint64_t file_size = ftell(fw_file);
765 fseek(fw_file, 0, SEEK_SET);
766
767 void *fw_data = malloc(file_size);
768 if (fw_data == NULL) {
769 fclose(fw_file);
770 usb_log_error("Failed allocating memory for firmware.\n");
771 return ENOMEM;
772 }
773
774 fread(fw_data, file_size, 1, fw_file);
775 fclose(fw_file);
776
777 /* Upload FW to device. */
778 uint64_t remain_size = file_size;
779 uint32_t current_addr = AR9271_FW_ADDRESS;
780 uint8_t *current_data = fw_data;
781 uint8_t *buffer = malloc(MAX_TRANSFER_SIZE);
782
783 while (remain_size > 0) {
784 size_t chunk_size = min(remain_size, MAX_TRANSFER_SIZE);
785 memcpy(buffer, current_data, chunk_size);
786 usb_pipe_t *ctrl_pipe = usb_device_get_default_pipe(usb_device);
787 errno_t rc = usb_control_request_set(ctrl_pipe,
788 USB_REQUEST_TYPE_VENDOR,
789 USB_REQUEST_RECIPIENT_DEVICE,
790 AR9271_FW_DOWNLOAD,
791 uint16_host2usb(current_addr >> 8),
792 0, buffer, chunk_size);
793 if (rc != EOK) {
794 free(fw_data);
795 free(buffer);
796 usb_log_error("Error while uploading firmware. "
797 "Error: %s\n", str_error_name(rc));
798 return rc;
799 }
800
801 remain_size -= chunk_size;
802 current_addr += chunk_size;
803 current_data += chunk_size;
804 }
805
806 free(fw_data);
807 free(buffer);
808
809 /*
810 * Send command that firmware is successfully uploaded.
811 * This should initiate creating confirmation message in
812 * device side buffer which we will check in htc_check_ready function.
813 */
814 usb_pipe_t *ctrl_pipe = usb_device_get_default_pipe(usb_device);
815 errno_t rc = usb_control_request_set(ctrl_pipe,
816 USB_REQUEST_TYPE_VENDOR,
817 USB_REQUEST_RECIPIENT_DEVICE,
818 AR9271_FW_DOWNLOAD_COMP,
819 uint16_host2usb(AR9271_FW_OFFSET >> 8),
820 0, NULL, 0);
821
822 if (rc != EOK) {
823 usb_log_error("IO error when sending fw upload confirmation "
824 "message.\n");
825 return rc;
826 }
827
828 usb_log_info("Firmware uploaded successfully.\n");
829
830 /* Wait until firmware is ready - wait for 1 second to be sure. */
831 fibril_sleep(1);
832
833 return rc;
834}
835
836/** Create driver data structure.
837 *
838 * @param dev The device structure
839 *
840 * @return Intialized device data structure or NULL if error occured
841 */
842static ar9271_t *ar9271_create_dev_data(ddf_dev_t *dev)
843{
844 /* USB framework initialization. */
845 const char *err_msg = NULL;
846 errno_t rc = usb_device_create_ddf(dev, endpoints, &err_msg);
847 if (rc != EOK) {
848 usb_log_error("Failed to create USB device: %s, "
849 "ERR_NUM = %s\n", err_msg, str_error_name(rc));
850 return NULL;
851 }
852
853 /* AR9271 structure initialization. */
854 ar9271_t *ar9271 = calloc(1, sizeof(ar9271_t));
855 if (!ar9271) {
856 usb_log_error("Failed to allocate memory for device "
857 "structure.\n");
858 return NULL;
859 }
860
861 ar9271->ddf_dev = dev;
862
863 rc = ar9271_init(ar9271, usb_device_get(dev), endpoints);
864 if (rc != EOK) {
865 free(ar9271);
866 usb_log_error("Failed to initialize AR9271 structure: %s\n",
867 str_error_name(rc));
868 return NULL;
869 }
870
871 return ar9271;
872}
873
874/** Clean up the ar9271 device structure.
875 *
876 * @param dev The device structure.
877 */
878static void ar9271_delete_dev_data(ar9271_t *ar9271)
879{
880 assert(ar9271);
881
882 // TODO
883}
884
885/** Probe and initialize the newly added device.
886 *
887 * @param dev The device structure.
888 *
889 * @return EOK if succeed, error code otherwise
890 */
891static errno_t ar9271_add_device(ddf_dev_t *dev)
892{
893 assert(dev);
894
895 /* Allocate driver data for the device. */
896 ar9271_t *ar9271 = ar9271_create_dev_data(dev);
897 if (ar9271 == NULL) {
898 usb_log_error("Unable to allocate device softstate.\n");
899 return ENOMEM;
900 }
901
902 usb_log_info("HelenOS AR9271 device initialized.\n");
903
904 /* Upload AR9271 firmware. */
905 ar9271_upload_fw(ar9271);
906
907 /* Initialize AR9271 HTC services. */
908 errno_t rc = htc_init(ar9271->htc_device);
909 if (rc != EOK) {
910 ar9271_delete_dev_data(ar9271);
911 usb_log_error("HTC initialization failed.\n");
912 return rc;
913 }
914
915 /* Initialize AR9271 HW. */
916 rc = hw_init(ar9271);
917 if (rc != EOK) {
918 ar9271_delete_dev_data(ar9271);
919 usb_log_error("HW initialization failed.\n");
920 return rc;
921 }
922
923 /* Initialize AR9271 IEEE 802.11 framework. */
924 rc = ieee80211_init(ar9271->ieee80211_dev, &ar9271_ieee80211_ops,
925 &ar9271_ieee80211_iface, &ar9271_ieee80211_nic_iface,
926 &ar9271_ieee80211_dev_ops);
927 if (rc != EOK) {
928 ar9271_delete_dev_data(ar9271);
929 usb_log_error("Failed to initialize IEEE80211 framework.\n");
930 return rc;
931 }
932
933 nic_set_filtering_change_handlers(nic_get_from_ddf_dev(dev),
934 ar9271_on_unicast_mode_change, ar9271_on_multicast_mode_change,
935 ar9271_on_broadcast_mode_change, NULL, NULL);
936
937 usb_log_info("HelenOS AR9271 added device.\n");
938
939 return EOK;
940}
941
942int main(void)
943{
944 log_init(NAME);
945
946 if (nic_driver_init(NAME) != EOK)
947 return 1;
948
949 usb_log_info("HelenOS AR9271 driver started.\n");
950
951 return ddf_driver_main(&ar9271_driver);
952}
Note: See TracBrowser for help on using the repository browser.