| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (c) 2021 pureLiFi |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <linux/kernel.h> |
| 7 |
#include <linux/init.h> |
| 8 |
#include <linux/device.h> |
| 9 |
#include <linux/errno.h> |
| 10 |
#include <linux/slab.h> |
| 11 |
#include <linux/skbuff.h> |
| 12 |
#include <linux/usb.h> |
| 13 |
#include <linux/workqueue.h> |
| 14 |
#include <linux/proc_fs.h> |
| 15 |
#include <linux/fs.h> |
| 16 |
#include <linux/string.h> |
| 17 |
#include <linux/module.h> |
| 18 |
#include <net/mac80211.h> |
| 19 |
#include <linux/unaligned.h> |
| 20 |
#include <linux/sysfs.h> |
| 21 |
|
| 22 |
#include "mac.h" |
| 23 |
#include "usb.h" |
| 24 |
#include "chip.h" |
| 25 |
|
| 26 |
static const struct usb_device_id usb_ids[] = { |
| 27 |
{ USB_DEVICE(PURELIFI_X_VENDOR_ID_0, PURELIFI_X_PRODUCT_ID_0), |
| 28 |
.driver_info = DEVICE_LIFI_X }, |
| 29 |
{ USB_DEVICE(PURELIFI_XC_VENDOR_ID_0, PURELIFI_XC_PRODUCT_ID_0), |
| 30 |
.driver_info = DEVICE_LIFI_XC }, |
| 31 |
{ USB_DEVICE(PURELIFI_XL_VENDOR_ID_0, PURELIFI_XL_PRODUCT_ID_0), |
| 32 |
.driver_info = DEVICE_LIFI_XL }, |
| 33 |
{} |
| 34 |
}; |
| 35 |
|
| 36 |
void plfxlc_send_packet_from_data_queue(struct plfxlc_usb *usb) |
| 37 |
{ |
| 38 |
struct plfxlc_usb_tx *tx = &usb->tx; |
| 39 |
struct sk_buff *skb = NULL; |
| 40 |
unsigned long flags; |
| 41 |
u8 last_served_sidx; |
| 42 |
|
| 43 |
spin_lock_irqsave(&tx->lock, flags); |
| 44 |
last_served_sidx = usb->sidx; |
| 45 |
do { |
| 46 |
usb->sidx = (usb->sidx + 1) % MAX_STA_NUM; |
| 47 |
if (!(tx->station[usb->sidx].flag & STATION_CONNECTED_FLAG)) |
| 48 |
continue; |
| 49 |
if (!(tx->station[usb->sidx].flag & STATION_FIFO_FULL_FLAG)) |
| 50 |
skb = skb_peek(&tx->station[usb->sidx].data_list); |
| 51 |
} while ((usb->sidx != last_served_sidx) && (!skb)); |
| 52 |
|
| 53 |
if (skb) { |
| 54 |
skb = skb_dequeue(&tx->station[usb->sidx].data_list); |
| 55 |
plfxlc_usb_wreq_async(usb, skb->data, skb->len, USB_REQ_DATA_TX, |
| 56 |
plfxlc_tx_urb_complete, skb); |
| 57 |
if (skb_queue_len(&tx->station[usb->sidx].data_list) <= 60) |
| 58 |
ieee80211_wake_queues(plfxlc_usb_to_hw(usb)); |
| 59 |
} |
| 60 |
spin_unlock_irqrestore(&tx->lock, flags); |
| 61 |
} |
| 62 |
|
| 63 |
static void handle_rx_packet(struct plfxlc_usb *usb, const u8 *buffer, |
| 64 |
unsigned int length) |
| 65 |
{ |
| 66 |
plfxlc_mac_rx(plfxlc_usb_to_hw(usb), buffer, length); |
| 67 |
} |
| 68 |
|
| 69 |
static void rx_urb_complete(struct urb *urb) |
| 70 |
{ |
| 71 |
struct plfxlc_usb_tx *tx; |
| 72 |
struct plfxlc_usb *usb; |
| 73 |
unsigned int length; |
| 74 |
const u8 *buffer; |
| 75 |
u16 status; |
| 76 |
u8 sidx; |
| 77 |
int r; |
| 78 |
|
| 79 |
if (!urb) { |
| 80 |
pr_err("urb is NULL\n"); |
| 81 |
return; |
| 82 |
} |
| 83 |
if (!urb->context) { |
| 84 |
pr_err("urb ctx is NULL\n"); |
| 85 |
return; |
| 86 |
} |
| 87 |
usb = urb->context; |
| 88 |
|
| 89 |
if (usb->initialized != 1) { |
| 90 |
pr_err("usb is not initialized\n"); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
tx = &usb->tx; |
| 95 |
switch (urb->status) { |
| 96 |
case 0: |
| 97 |
break; |
| 98 |
case -ESHUTDOWN: |
| 99 |
case -EINVAL: |
| 100 |
case -ENODEV: |
| 101 |
case -ENOENT: |
| 102 |
case -ECONNRESET: |
| 103 |
case -EPIPE: |
| 104 |
dev_dbg(plfxlc_urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 105 |
return; |
| 106 |
default: |
| 107 |
dev_dbg(plfxlc_urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 108 |
if (tx->submitted_urbs++ < PURELIFI_URB_RETRY_MAX) { |
| 109 |
dev_dbg(plfxlc_urb_dev(urb), "urb %p resubmit %d", urb, |
| 110 |
tx->submitted_urbs++); |
| 111 |
goto resubmit; |
| 112 |
} else { |
| 113 |
dev_dbg(plfxlc_urb_dev(urb), "urb %p max resubmits reached", urb); |
| 114 |
tx->submitted_urbs = 0; |
| 115 |
return; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
buffer = urb->transfer_buffer; |
| 120 |
length = le32_to_cpu(*(__le32 *)(buffer + sizeof(struct rx_status))) |
| 121 |
+ sizeof(u32); |
| 122 |
|
| 123 |
if (urb->actual_length != (PLF_MSG_STATUS_OFFSET + 1)) { |
| 124 |
if (usb->initialized && usb->link_up) |
| 125 |
handle_rx_packet(usb, buffer, length); |
| 126 |
goto resubmit; |
| 127 |
} |
| 128 |
|
| 129 |
status = buffer[PLF_MSG_STATUS_OFFSET]; |
| 130 |
|
| 131 |
switch (status) { |
| 132 |
case STATION_FIFO_ALMOST_FULL_NOT_MESSAGE: |
| 133 |
dev_dbg(&usb->intf->dev, |
| 134 |
"FIFO full not packet receipt\n"); |
| 135 |
tx->mac_fifo_full = 1; |
| 136 |
for (sidx = 0; sidx < MAX_STA_NUM; sidx++) |
| 137 |
tx->station[sidx].flag |= STATION_FIFO_FULL_FLAG; |
| 138 |
break; |
| 139 |
case STATION_FIFO_ALMOST_FULL_MESSAGE: |
| 140 |
dev_dbg(&usb->intf->dev, "FIFO full packet receipt\n"); |
| 141 |
|
| 142 |
for (sidx = 0; sidx < MAX_STA_NUM; sidx++) |
| 143 |
tx->station[sidx].flag &= STATION_ACTIVE_FLAG; |
| 144 |
|
| 145 |
plfxlc_send_packet_from_data_queue(usb); |
| 146 |
break; |
| 147 |
case STATION_CONNECT_MESSAGE: |
| 148 |
usb->link_up = 1; |
| 149 |
dev_dbg(&usb->intf->dev, "ST_CONNECT_MSG packet receipt\n"); |
| 150 |
break; |
| 151 |
case STATION_DISCONNECT_MESSAGE: |
| 152 |
usb->link_up = 0; |
| 153 |
dev_dbg(&usb->intf->dev, "ST_DISCONN_MSG packet receipt\n"); |
| 154 |
break; |
| 155 |
default: |
| 156 |
dev_dbg(&usb->intf->dev, "Unknown packet receipt\n"); |
| 157 |
break; |
| 158 |
} |
| 159 |
|
| 160 |
resubmit: |
| 161 |
r = usb_submit_urb(urb, GFP_ATOMIC); |
| 162 |
if (r) |
| 163 |
dev_dbg(plfxlc_urb_dev(urb), "urb %p resubmit fail (%d)\n", urb, r); |
| 164 |
} |
| 165 |
|
| 166 |
static struct urb *alloc_rx_urb(struct plfxlc_usb *usb) |
| 167 |
{ |
| 168 |
struct usb_device *udev = plfxlc_usb_to_usbdev(usb); |
| 169 |
struct urb *urb; |
| 170 |
void *buffer; |
| 171 |
|
| 172 |
urb = usb_alloc_urb(0, GFP_KERNEL); |
| 173 |
if (!urb) |
| 174 |
return NULL; |
| 175 |
|
| 176 |
buffer = usb_alloc_coherent(udev, USB_MAX_RX_SIZE, GFP_KERNEL, |
| 177 |
&urb->transfer_dma); |
| 178 |
if (!buffer) { |
| 179 |
usb_free_urb(urb); |
| 180 |
return NULL; |
| 181 |
} |
| 182 |
|
| 183 |
usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN), |
| 184 |
buffer, USB_MAX_RX_SIZE, |
| 185 |
rx_urb_complete, usb); |
| 186 |
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 187 |
|
| 188 |
return urb; |
| 189 |
} |
| 190 |
|
| 191 |
static void free_rx_urb(struct urb *urb) |
| 192 |
{ |
| 193 |
if (!urb) |
| 194 |
return; |
| 195 |
usb_free_coherent(urb->dev, urb->transfer_buffer_length, |
| 196 |
urb->transfer_buffer, urb->transfer_dma); |
| 197 |
usb_free_urb(urb); |
| 198 |
} |
| 199 |
|
| 200 |
static int __lf_x_usb_enable_rx(struct plfxlc_usb *usb) |
| 201 |
{ |
| 202 |
struct plfxlc_usb_rx *rx = &usb->rx; |
| 203 |
struct urb **urbs; |
| 204 |
int i, r; |
| 205 |
|
| 206 |
r = -ENOMEM; |
| 207 |
urbs = kzalloc_objs(struct urb *, RX_URBS_COUNT); |
| 208 |
if (!urbs) |
| 209 |
goto error; |
| 210 |
|
| 211 |
for (i = 0; i < RX_URBS_COUNT; i++) { |
| 212 |
urbs[i] = alloc_rx_urb(usb); |
| 213 |
if (!urbs[i]) |
| 214 |
goto error; |
| 215 |
} |
| 216 |
|
| 217 |
spin_lock_irq(&rx->lock); |
| 218 |
|
| 219 |
dev_dbg(plfxlc_usb_dev(usb), "irq_disabled %d\n", irqs_disabled()); |
| 220 |
|
| 221 |
if (rx->urbs) { |
| 222 |
spin_unlock_irq(&rx->lock); |
| 223 |
r = 0; |
| 224 |
goto error; |
| 225 |
} |
| 226 |
rx->urbs = urbs; |
| 227 |
rx->urbs_count = RX_URBS_COUNT; |
| 228 |
spin_unlock_irq(&rx->lock); |
| 229 |
|
| 230 |
for (i = 0; i < RX_URBS_COUNT; i++) { |
| 231 |
r = usb_submit_urb(urbs[i], GFP_KERNEL); |
| 232 |
if (r) |
| 233 |
goto error_submit; |
| 234 |
} |
| 235 |
|
| 236 |
return 0; |
| 237 |
|
| 238 |
error_submit: |
| 239 |
for (i = 0; i < RX_URBS_COUNT; i++) |
| 240 |
usb_kill_urb(urbs[i]); |
| 241 |
spin_lock_irq(&rx->lock); |
| 242 |
rx->urbs = NULL; |
| 243 |
rx->urbs_count = 0; |
| 244 |
spin_unlock_irq(&rx->lock); |
| 245 |
error: |
| 246 |
if (urbs) { |
| 247 |
for (i = 0; i < RX_URBS_COUNT; i++) |
| 248 |
free_rx_urb(urbs[i]); |
| 249 |
} |
| 250 |
kfree(urbs); |
| 251 |
return r; |
| 252 |
} |
| 253 |
|
| 254 |
int plfxlc_usb_enable_rx(struct plfxlc_usb *usb) |
| 255 |
{ |
| 256 |
struct plfxlc_usb_rx *rx = &usb->rx; |
| 257 |
int r; |
| 258 |
|
| 259 |
mutex_lock(&rx->setup_mutex); |
| 260 |
r = __lf_x_usb_enable_rx(usb); |
| 261 |
if (!r) |
| 262 |
usb->rx_usb_enabled = 1; |
| 263 |
|
| 264 |
mutex_unlock(&rx->setup_mutex); |
| 265 |
|
| 266 |
return r; |
| 267 |
} |
| 268 |
|
| 269 |
static void __lf_x_usb_disable_rx(struct plfxlc_usb *usb) |
| 270 |
{ |
| 271 |
struct plfxlc_usb_rx *rx = &usb->rx; |
| 272 |
unsigned long flags; |
| 273 |
unsigned int count; |
| 274 |
struct urb **urbs; |
| 275 |
int i; |
| 276 |
|
| 277 |
spin_lock_irqsave(&rx->lock, flags); |
| 278 |
urbs = rx->urbs; |
| 279 |
count = rx->urbs_count; |
| 280 |
spin_unlock_irqrestore(&rx->lock, flags); |
| 281 |
|
| 282 |
if (!urbs) |
| 283 |
return; |
| 284 |
|
| 285 |
for (i = 0; i < count; i++) { |
| 286 |
usb_kill_urb(urbs[i]); |
| 287 |
free_rx_urb(urbs[i]); |
| 288 |
} |
| 289 |
kfree(urbs); |
| 290 |
rx->urbs = NULL; |
| 291 |
rx->urbs_count = 0; |
| 292 |
} |
| 293 |
|
| 294 |
void plfxlc_usb_disable_rx(struct plfxlc_usb *usb) |
| 295 |
{ |
| 296 |
struct plfxlc_usb_rx *rx = &usb->rx; |
| 297 |
|
| 298 |
mutex_lock(&rx->setup_mutex); |
| 299 |
__lf_x_usb_disable_rx(usb); |
| 300 |
usb->rx_usb_enabled = 0; |
| 301 |
mutex_unlock(&rx->setup_mutex); |
| 302 |
} |
| 303 |
|
| 304 |
void plfxlc_usb_disable_tx(struct plfxlc_usb *usb) |
| 305 |
{ |
| 306 |
struct plfxlc_usb_tx *tx = &usb->tx; |
| 307 |
unsigned long flags; |
| 308 |
|
| 309 |
clear_bit(PLF_BIT_ENABLED, &tx->enabled); |
| 310 |
|
| 311 |
/* kill all submitted tx-urbs */ |
| 312 |
usb_kill_anchored_urbs(&tx->submitted); |
| 313 |
|
| 314 |
spin_lock_irqsave(&tx->lock, flags); |
| 315 |
WARN_ON(!skb_queue_empty(&tx->submitted_skbs)); |
| 316 |
WARN_ON(tx->submitted_urbs != 0); |
| 317 |
tx->submitted_urbs = 0; |
| 318 |
spin_unlock_irqrestore(&tx->lock, flags); |
| 319 |
|
| 320 |
/* The stopped state is ignored, relying on ieee80211_wake_queues() |
| 321 |
* in a potentionally following plfxlc_usb_enable_tx(). |
| 322 |
*/ |
| 323 |
} |
| 324 |
|
| 325 |
void plfxlc_usb_enable_tx(struct plfxlc_usb *usb) |
| 326 |
{ |
| 327 |
struct plfxlc_usb_tx *tx = &usb->tx; |
| 328 |
unsigned long flags; |
| 329 |
|
| 330 |
spin_lock_irqsave(&tx->lock, flags); |
| 331 |
set_bit(PLF_BIT_ENABLED, &tx->enabled); |
| 332 |
tx->submitted_urbs = 0; |
| 333 |
ieee80211_wake_queues(plfxlc_usb_to_hw(usb)); |
| 334 |
tx->stopped = 0; |
| 335 |
spin_unlock_irqrestore(&tx->lock, flags); |
| 336 |
} |
| 337 |
|
| 338 |
void plfxlc_tx_urb_complete(struct urb *urb) |
| 339 |
{ |
| 340 |
struct ieee80211_tx_info *info; |
| 341 |
struct plfxlc_usb *usb; |
| 342 |
struct sk_buff *skb; |
| 343 |
|
| 344 |
skb = urb->context; |
| 345 |
info = IEEE80211_SKB_CB(skb); |
| 346 |
/* grab 'usb' pointer before handing off the skb (since |
| 347 |
* it might be freed by plfxlc_mac_tx_to_dev or mac80211) |
| 348 |
*/ |
| 349 |
usb = &plfxlc_hw_mac(info->rate_driver_data[0])->chip.usb; |
| 350 |
|
| 351 |
switch (urb->status) { |
| 352 |
case 0: |
| 353 |
break; |
| 354 |
case -ESHUTDOWN: |
| 355 |
case -EINVAL: |
| 356 |
case -ENODEV: |
| 357 |
case -ENOENT: |
| 358 |
case -ECONNRESET: |
| 359 |
case -EPIPE: |
| 360 |
dev_dbg(plfxlc_urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 361 |
break; |
| 362 |
default: |
| 363 |
dev_dbg(plfxlc_urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
plfxlc_mac_tx_to_dev(skb, urb->status); |
| 368 |
plfxlc_send_packet_from_data_queue(usb); |
| 369 |
usb_free_urb(urb); |
| 370 |
} |
| 371 |
|
| 372 |
static inline void init_usb_rx(struct plfxlc_usb *usb) |
| 373 |
{ |
| 374 |
struct plfxlc_usb_rx *rx = &usb->rx; |
| 375 |
|
| 376 |
spin_lock_init(&rx->lock); |
| 377 |
mutex_init(&rx->setup_mutex); |
| 378 |
|
| 379 |
if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) |
| 380 |
rx->usb_packet_size = 512; |
| 381 |
else |
| 382 |
rx->usb_packet_size = 64; |
| 383 |
|
| 384 |
if (rx->fragment_length != 0) |
| 385 |
dev_dbg(plfxlc_usb_dev(usb), "fragment_length error\n"); |
| 386 |
} |
| 387 |
|
| 388 |
static inline void init_usb_tx(struct plfxlc_usb *usb) |
| 389 |
{ |
| 390 |
struct plfxlc_usb_tx *tx = &usb->tx; |
| 391 |
|
| 392 |
spin_lock_init(&tx->lock); |
| 393 |
clear_bit(PLF_BIT_ENABLED, &tx->enabled); |
| 394 |
tx->stopped = 0; |
| 395 |
skb_queue_head_init(&tx->submitted_skbs); |
| 396 |
init_usb_anchor(&tx->submitted); |
| 397 |
} |
| 398 |
|
| 399 |
void plfxlc_usb_init(struct plfxlc_usb *usb, struct ieee80211_hw *hw, |
| 400 |
struct usb_interface *intf) |
| 401 |
{ |
| 402 |
memset(usb, 0, sizeof(*usb)); |
| 403 |
usb->intf = usb_get_intf(intf); |
| 404 |
usb_set_intfdata(usb->intf, hw); |
| 405 |
init_usb_tx(usb); |
| 406 |
init_usb_rx(usb); |
| 407 |
} |
| 408 |
|
| 409 |
void plfxlc_usb_release(struct plfxlc_usb *usb) |
| 410 |
{ |
| 411 |
plfxlc_op_stop(plfxlc_usb_to_hw(usb), false); |
| 412 |
plfxlc_usb_disable_tx(usb); |
| 413 |
plfxlc_usb_disable_rx(usb); |
| 414 |
usb_set_intfdata(usb->intf, NULL); |
| 415 |
usb_put_intf(usb->intf); |
| 416 |
} |
| 417 |
|
| 418 |
const char *plfxlc_speed(enum usb_device_speed speed) |
| 419 |
{ |
| 420 |
switch (speed) { |
| 421 |
case USB_SPEED_LOW: |
| 422 |
return "low"; |
| 423 |
case USB_SPEED_FULL: |
| 424 |
return "full"; |
| 425 |
case USB_SPEED_HIGH: |
| 426 |
return "high"; |
| 427 |
default: |
| 428 |
return "unknown"; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
int plfxlc_usb_init_hw(struct plfxlc_usb *usb) |
| 433 |
{ |
| 434 |
int r; |
| 435 |
|
| 436 |
r = usb_reset_configuration(plfxlc_usb_to_usbdev(usb)); |
| 437 |
if (r) { |
| 438 |
dev_err(plfxlc_usb_dev(usb), "cfg reset failed (%d)\n", r); |
| 439 |
return r; |
| 440 |
} |
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
static void get_usb_req(struct usb_device *udev, void *buffer, |
| 445 |
u32 buffer_len, enum plf_usb_req_enum usb_req_id, |
| 446 |
struct plf_usb_req *usb_req) |
| 447 |
{ |
| 448 |
__be32 payload_len_nw = cpu_to_be32(buffer_len + FCS_LEN); |
| 449 |
const u8 *buffer_src_p = buffer; |
| 450 |
u8 *buffer_dst = usb_req->buf; |
| 451 |
u32 temp_usb_len = 0; |
| 452 |
|
| 453 |
usb_req->id = cpu_to_be32(usb_req_id); |
| 454 |
usb_req->len = cpu_to_be32(0); |
| 455 |
|
| 456 |
/* Copy buffer length into the transmitted buffer, as it is important |
| 457 |
* for the Rx MAC to know its exact length. |
| 458 |
*/ |
| 459 |
if (usb_req->id == cpu_to_be32(USB_REQ_BEACON_WR)) { |
| 460 |
memcpy(buffer_dst, &payload_len_nw, sizeof(payload_len_nw)); |
| 461 |
buffer_dst += sizeof(payload_len_nw); |
| 462 |
temp_usb_len += sizeof(payload_len_nw); |
| 463 |
} |
| 464 |
|
| 465 |
memcpy(buffer_dst, buffer_src_p, buffer_len); |
| 466 |
buffer_dst += buffer_len; |
| 467 |
buffer_src_p += buffer_len; |
| 468 |
temp_usb_len += buffer_len; |
| 469 |
|
| 470 |
/* Set the FCS_LEN (4) bytes as 0 for CRC checking. */ |
| 471 |
memset(buffer_dst, 0, FCS_LEN); |
| 472 |
buffer_dst += FCS_LEN; |
| 473 |
temp_usb_len += FCS_LEN; |
| 474 |
|
| 475 |
/* Round the packet to be transmitted to 4 bytes. */ |
| 476 |
if (temp_usb_len % PURELIFI_BYTE_NUM_ALIGNMENT) { |
| 477 |
memset(buffer_dst, 0, PURELIFI_BYTE_NUM_ALIGNMENT - |
| 478 |
(temp_usb_len % |
| 479 |
PURELIFI_BYTE_NUM_ALIGNMENT)); |
| 480 |
buffer_dst += PURELIFI_BYTE_NUM_ALIGNMENT - |
| 481 |
(temp_usb_len % |
| 482 |
PURELIFI_BYTE_NUM_ALIGNMENT); |
| 483 |
temp_usb_len += PURELIFI_BYTE_NUM_ALIGNMENT - |
| 484 |
(temp_usb_len % PURELIFI_BYTE_NUM_ALIGNMENT); |
| 485 |
} |
| 486 |
|
| 487 |
usb_req->len = cpu_to_be32(temp_usb_len); |
| 488 |
} |
| 489 |
|
| 490 |
int plfxlc_usb_wreq_async(struct plfxlc_usb *usb, const u8 *buffer, |
| 491 |
int buffer_len, enum plf_usb_req_enum usb_req_id, |
| 492 |
usb_complete_t complete_fn, |
| 493 |
void *context) |
| 494 |
{ |
| 495 |
struct usb_device *udev = interface_to_usbdev(usb->ez_usb); |
| 496 |
struct urb *urb; |
| 497 |
int r; |
| 498 |
|
| 499 |
urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 500 |
if (!urb) |
| 501 |
return -ENOMEM; |
| 502 |
usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT), |
| 503 |
(void *)buffer, buffer_len, complete_fn, context); |
| 504 |
|
| 505 |
r = usb_submit_urb(urb, GFP_ATOMIC); |
| 506 |
if (r) { |
| 507 |
usb_free_urb(urb); |
| 508 |
dev_err(&udev->dev, "Async write submit failed (%d)\n", r); |
| 509 |
} |
| 510 |
|
| 511 |
return r; |
| 512 |
} |
| 513 |
|
| 514 |
int plfxlc_usb_wreq(struct usb_interface *ez_usb, void *buffer, int buffer_len, |
| 515 |
enum plf_usb_req_enum usb_req_id) |
| 516 |
{ |
| 517 |
struct usb_device *udev = interface_to_usbdev(ez_usb); |
| 518 |
unsigned char *dma_buffer = NULL; |
| 519 |
struct plf_usb_req usb_req; |
| 520 |
int usb_bulk_msg_len; |
| 521 |
int actual_length; |
| 522 |
int r; |
| 523 |
|
| 524 |
get_usb_req(udev, buffer, buffer_len, usb_req_id, &usb_req); |
| 525 |
usb_bulk_msg_len = sizeof(__le32) + sizeof(__le32) + |
| 526 |
be32_to_cpu(usb_req.len); |
| 527 |
|
| 528 |
dma_buffer = kmemdup(&usb_req, usb_bulk_msg_len, GFP_KERNEL); |
| 529 |
|
| 530 |
if (!dma_buffer) { |
| 531 |
r = -ENOMEM; |
| 532 |
goto error; |
| 533 |
} |
| 534 |
|
| 535 |
r = usb_bulk_msg(udev, |
| 536 |
usb_sndbulkpipe(udev, EP_DATA_OUT), |
| 537 |
dma_buffer, usb_bulk_msg_len, |
| 538 |
&actual_length, USB_BULK_MSG_TIMEOUT_MS); |
| 539 |
kfree(dma_buffer); |
| 540 |
error: |
| 541 |
if (r) { |
| 542 |
r = -ENOMEM; |
| 543 |
dev_err(&udev->dev, "usb_bulk_msg failed (%d)\n", r); |
| 544 |
} |
| 545 |
|
| 546 |
return r; |
| 547 |
} |
| 548 |
|
| 549 |
static void slif_data_plane_sap_timer_callb(struct timer_list *t) |
| 550 |
{ |
| 551 |
struct plfxlc_usb *usb = timer_container_of(usb, t, tx.tx_retry_timer); |
| 552 |
|
| 553 |
plfxlc_send_packet_from_data_queue(usb); |
| 554 |
timer_setup(&usb->tx.tx_retry_timer, |
| 555 |
slif_data_plane_sap_timer_callb, 0); |
| 556 |
mod_timer(&usb->tx.tx_retry_timer, jiffies + TX_RETRY_BACKOFF_JIFF); |
| 557 |
} |
| 558 |
|
| 559 |
static void sta_queue_cleanup_timer_callb(struct timer_list *t) |
| 560 |
{ |
| 561 |
struct plfxlc_usb *usb = timer_container_of(usb, t, sta_queue_cleanup); |
| 562 |
struct plfxlc_usb_tx *tx = &usb->tx; |
| 563 |
int sidx; |
| 564 |
|
| 565 |
for (sidx = 0; sidx < MAX_STA_NUM - 1; sidx++) { |
| 566 |
if (!(tx->station[sidx].flag & STATION_CONNECTED_FLAG)) |
| 567 |
continue; |
| 568 |
if (tx->station[sidx].flag & STATION_HEARTBEAT_FLAG) { |
| 569 |
tx->station[sidx].flag ^= STATION_HEARTBEAT_FLAG; |
| 570 |
} else { |
| 571 |
eth_zero_addr(tx->station[sidx].mac); |
| 572 |
tx->station[sidx].flag = 0; |
| 573 |
} |
| 574 |
} |
| 575 |
timer_setup(&usb->sta_queue_cleanup, |
| 576 |
sta_queue_cleanup_timer_callb, 0); |
| 577 |
mod_timer(&usb->sta_queue_cleanup, jiffies + STA_QUEUE_CLEANUP_JIFF); |
| 578 |
} |
| 579 |
|
| 580 |
static int probe(struct usb_interface *intf, |
| 581 |
const struct usb_device_id *id) |
| 582 |
{ |
| 583 |
u8 serial_number[PURELIFI_SERIAL_LEN]; |
| 584 |
struct ieee80211_hw *hw = NULL; |
| 585 |
struct plfxlc_usb_tx *tx; |
| 586 |
struct plfxlc_chip *chip; |
| 587 |
struct plfxlc_usb *usb; |
| 588 |
u8 hw_address[ETH_ALEN]; |
| 589 |
unsigned int i; |
| 590 |
int r = 0; |
| 591 |
|
| 592 |
hw = plfxlc_mac_alloc_hw(intf); |
| 593 |
|
| 594 |
if (!hw) { |
| 595 |
r = -ENOMEM; |
| 596 |
goto error; |
| 597 |
} |
| 598 |
|
| 599 |
chip = &plfxlc_hw_mac(hw)->chip; |
| 600 |
usb = &chip->usb; |
| 601 |
usb->ez_usb = intf; |
| 602 |
tx = &usb->tx; |
| 603 |
|
| 604 |
r = plfxlc_upload_mac_and_serial(intf, hw_address, serial_number); |
| 605 |
if (r) { |
| 606 |
dev_err(&intf->dev, "MAC and Serial upload failed (%d)\n", r); |
| 607 |
goto error_free_hw; |
| 608 |
} |
| 609 |
|
| 610 |
chip->unit_type = STA; |
| 611 |
dev_err(&intf->dev, "Unit type is station"); |
| 612 |
|
| 613 |
r = plfxlc_mac_preinit_hw(hw, hw_address); |
| 614 |
if (r) { |
| 615 |
dev_err(&intf->dev, "Init mac failed (%d)\n", r); |
| 616 |
goto error_free_hw; |
| 617 |
} |
| 618 |
|
| 619 |
r = ieee80211_register_hw(hw); |
| 620 |
if (r) { |
| 621 |
dev_err(&intf->dev, "Register device failed (%d)\n", r); |
| 622 |
goto error_free_hw; |
| 623 |
} |
| 624 |
|
| 625 |
if ((le16_to_cpu(interface_to_usbdev(intf)->descriptor.idVendor) == |
| 626 |
PURELIFI_XL_VENDOR_ID_0) && |
| 627 |
(le16_to_cpu(interface_to_usbdev(intf)->descriptor.idProduct) == |
| 628 |
PURELIFI_XL_PRODUCT_ID_0)) { |
| 629 |
r = plfxlc_download_xl_firmware(intf); |
| 630 |
} else { |
| 631 |
r = plfxlc_download_fpga(intf); |
| 632 |
} |
| 633 |
if (r != 0) { |
| 634 |
dev_err(&intf->dev, "FPGA download failed (%d)\n", r); |
| 635 |
goto error_unreg_hw; |
| 636 |
} |
| 637 |
|
| 638 |
tx->mac_fifo_full = 0; |
| 639 |
spin_lock_init(&tx->lock); |
| 640 |
|
| 641 |
msleep(PLF_MSLEEP_TIME); |
| 642 |
r = plfxlc_usb_init_hw(usb); |
| 643 |
if (r < 0) { |
| 644 |
dev_err(&intf->dev, "usb_init_hw failed (%d)\n", r); |
| 645 |
goto error_unreg_hw; |
| 646 |
} |
| 647 |
|
| 648 |
msleep(PLF_MSLEEP_TIME); |
| 649 |
r = plfxlc_chip_switch_radio(chip, PLFXLC_RADIO_ON); |
| 650 |
if (r < 0) { |
| 651 |
dev_dbg(&intf->dev, "chip_switch_radio_on failed (%d)\n", r); |
| 652 |
goto error_unreg_hw; |
| 653 |
} |
| 654 |
|
| 655 |
msleep(PLF_MSLEEP_TIME); |
| 656 |
r = plfxlc_chip_set_rate(chip, 8); |
| 657 |
if (r < 0) { |
| 658 |
dev_dbg(&intf->dev, "chip_set_rate failed (%d)\n", r); |
| 659 |
goto error_unreg_hw; |
| 660 |
} |
| 661 |
|
| 662 |
msleep(PLF_MSLEEP_TIME); |
| 663 |
r = plfxlc_usb_wreq(usb->ez_usb, |
| 664 |
hw_address, ETH_ALEN, USB_REQ_MAC_WR); |
| 665 |
if (r < 0) { |
| 666 |
dev_dbg(&intf->dev, "MAC_WR failure (%d)\n", r); |
| 667 |
goto error_unreg_hw; |
| 668 |
} |
| 669 |
|
| 670 |
plfxlc_chip_enable_rxtx(chip); |
| 671 |
|
| 672 |
/* Initialise the data plane Tx queue */ |
| 673 |
for (i = 0; i < MAX_STA_NUM; i++) { |
| 674 |
skb_queue_head_init(&tx->station[i].data_list); |
| 675 |
tx->station[i].flag = 0; |
| 676 |
} |
| 677 |
|
| 678 |
tx->station[STA_BROADCAST_INDEX].flag |= STATION_CONNECTED_FLAG; |
| 679 |
for (i = 0; i < ETH_ALEN; i++) |
| 680 |
tx->station[STA_BROADCAST_INDEX].mac[i] = 0xFF; |
| 681 |
|
| 682 |
timer_setup(&tx->tx_retry_timer, slif_data_plane_sap_timer_callb, 0); |
| 683 |
tx->tx_retry_timer.expires = jiffies + TX_RETRY_BACKOFF_JIFF; |
| 684 |
add_timer(&tx->tx_retry_timer); |
| 685 |
|
| 686 |
timer_setup(&usb->sta_queue_cleanup, |
| 687 |
sta_queue_cleanup_timer_callb, 0); |
| 688 |
usb->sta_queue_cleanup.expires = jiffies + STA_QUEUE_CLEANUP_JIFF; |
| 689 |
add_timer(&usb->sta_queue_cleanup); |
| 690 |
|
| 691 |
plfxlc_mac_init_hw(hw); |
| 692 |
usb->initialized = true; |
| 693 |
return 0; |
| 694 |
|
| 695 |
error_unreg_hw: |
| 696 |
ieee80211_unregister_hw(hw); |
| 697 |
error_free_hw: |
| 698 |
plfxlc_mac_release_hw(hw); |
| 699 |
error: |
| 700 |
dev_err(&intf->dev, "pureLifi:Device error"); |
| 701 |
return r; |
| 702 |
} |
| 703 |
|
| 704 |
static void disconnect(struct usb_interface *intf) |
| 705 |
{ |
| 706 |
struct ieee80211_hw *hw = plfxlc_intf_to_hw(intf); |
| 707 |
struct plfxlc_mac *mac; |
| 708 |
struct plfxlc_usb *usb; |
| 709 |
|
| 710 |
/* Either something really bad happened, or |
| 711 |
* we're just dealing with a DEVICE_INSTALLER. |
| 712 |
*/ |
| 713 |
if (!hw) |
| 714 |
return; |
| 715 |
|
| 716 |
mac = plfxlc_hw_mac(hw); |
| 717 |
usb = &mac->chip.usb; |
| 718 |
|
| 719 |
timer_delete_sync(&usb->tx.tx_retry_timer); |
| 720 |
timer_delete_sync(&usb->sta_queue_cleanup); |
| 721 |
|
| 722 |
ieee80211_unregister_hw(hw); |
| 723 |
|
| 724 |
plfxlc_chip_disable_rxtx(&mac->chip); |
| 725 |
|
| 726 |
/* If the disconnect has been caused by a removal of the |
| 727 |
* driver module, the reset allows reloading of the driver. If the |
| 728 |
* reset will not be executed here, the upload of the firmware in the |
| 729 |
* probe function caused by the reloading of the driver will fail. |
| 730 |
*/ |
| 731 |
usb_reset_device(interface_to_usbdev(intf)); |
| 732 |
|
| 733 |
plfxlc_mac_release_hw(hw); |
| 734 |
} |
| 735 |
|
| 736 |
static void plfxlc_usb_resume(struct plfxlc_usb *usb) |
| 737 |
{ |
| 738 |
struct plfxlc_mac *mac = plfxlc_usb_to_mac(usb); |
| 739 |
int r; |
| 740 |
|
| 741 |
r = plfxlc_op_start(plfxlc_usb_to_hw(usb)); |
| 742 |
if (r < 0) { |
| 743 |
dev_warn(plfxlc_usb_dev(usb), |
| 744 |
"Device resume failed (%d)\n", r); |
| 745 |
|
| 746 |
if (usb->was_running) |
| 747 |
set_bit(PURELIFI_DEVICE_RUNNING, &mac->flags); |
| 748 |
|
| 749 |
usb_queue_reset_device(usb->intf); |
| 750 |
return; |
| 751 |
} |
| 752 |
|
| 753 |
if (mac->type != NL80211_IFTYPE_UNSPECIFIED) { |
| 754 |
r = plfxlc_restore_settings(mac); |
| 755 |
if (r < 0) { |
| 756 |
dev_dbg(plfxlc_usb_dev(usb), |
| 757 |
"Restore failed (%d)\n", r); |
| 758 |
return; |
| 759 |
} |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
static void plfxlc_usb_stop(struct plfxlc_usb *usb) |
| 764 |
{ |
| 765 |
plfxlc_op_stop(plfxlc_usb_to_hw(usb), false); |
| 766 |
plfxlc_usb_disable_tx(usb); |
| 767 |
plfxlc_usb_disable_rx(usb); |
| 768 |
|
| 769 |
usb->initialized = false; |
| 770 |
} |
| 771 |
|
| 772 |
static int pre_reset(struct usb_interface *intf) |
| 773 |
{ |
| 774 |
struct ieee80211_hw *hw = usb_get_intfdata(intf); |
| 775 |
struct plfxlc_mac *mac; |
| 776 |
struct plfxlc_usb *usb; |
| 777 |
|
| 778 |
if (!hw || intf->condition != USB_INTERFACE_BOUND) |
| 779 |
return 0; |
| 780 |
|
| 781 |
mac = plfxlc_hw_mac(hw); |
| 782 |
usb = &mac->chip.usb; |
| 783 |
|
| 784 |
usb->was_running = test_bit(PURELIFI_DEVICE_RUNNING, &mac->flags); |
| 785 |
|
| 786 |
plfxlc_usb_stop(usb); |
| 787 |
|
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
static int post_reset(struct usb_interface *intf) |
| 792 |
{ |
| 793 |
struct ieee80211_hw *hw = usb_get_intfdata(intf); |
| 794 |
struct plfxlc_mac *mac; |
| 795 |
struct plfxlc_usb *usb; |
| 796 |
|
| 797 |
if (!hw || intf->condition != USB_INTERFACE_BOUND) |
| 798 |
return 0; |
| 799 |
|
| 800 |
mac = plfxlc_hw_mac(hw); |
| 801 |
usb = &mac->chip.usb; |
| 802 |
|
| 803 |
if (usb->was_running) |
| 804 |
plfxlc_usb_resume(usb); |
| 805 |
|
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
#ifdef CONFIG_PM |
| 810 |
|
| 811 |
static struct plfxlc_usb *get_plfxlc_usb(struct usb_interface *intf) |
| 812 |
{ |
| 813 |
struct ieee80211_hw *hw = plfxlc_intf_to_hw(intf); |
| 814 |
struct plfxlc_mac *mac; |
| 815 |
|
| 816 |
/* Either something really bad happened, or |
| 817 |
* we're just dealing with a DEVICE_INSTALLER. |
| 818 |
*/ |
| 819 |
if (!hw) |
| 820 |
return NULL; |
| 821 |
|
| 822 |
mac = plfxlc_hw_mac(hw); |
| 823 |
return &mac->chip.usb; |
| 824 |
} |
| 825 |
|
| 826 |
static int suspend(struct usb_interface *interface, |
| 827 |
pm_message_t message) |
| 828 |
{ |
| 829 |
struct plfxlc_usb *pl = get_plfxlc_usb(interface); |
| 830 |
struct plfxlc_mac *mac = plfxlc_usb_to_mac(pl); |
| 831 |
|
| 832 |
if (!pl) |
| 833 |
return -ENODEV; |
| 834 |
if (pl->initialized == 0) |
| 835 |
return 0; |
| 836 |
pl->was_running = test_bit(PURELIFI_DEVICE_RUNNING, &mac->flags); |
| 837 |
plfxlc_usb_stop(pl); |
| 838 |
return 0; |
| 839 |
} |
| 840 |
|
| 841 |
static int resume(struct usb_interface *interface) |
| 842 |
{ |
| 843 |
struct plfxlc_usb *pl = get_plfxlc_usb(interface); |
| 844 |
|
| 845 |
if (!pl) |
| 846 |
return -ENODEV; |
| 847 |
if (pl->was_running) |
| 848 |
plfxlc_usb_resume(pl); |
| 849 |
return 0; |
| 850 |
} |
| 851 |
|
| 852 |
#endif |
| 853 |
|
| 854 |
static struct usb_driver driver = { |
| 855 |
.name = KBUILD_MODNAME, |
| 856 |
.id_table = usb_ids, |
| 857 |
.probe = probe, |
| 858 |
.disconnect = disconnect, |
| 859 |
.pre_reset = pre_reset, |
| 860 |
.post_reset = post_reset, |
| 861 |
#ifdef CONFIG_PM |
| 862 |
.suspend = suspend, |
| 863 |
.resume = resume, |
| 864 |
#endif |
| 865 |
.disable_hub_initiated_lpm = 1, |
| 866 |
}; |
| 867 |
|
| 868 |
static int __init usb_init(void) |
| 869 |
{ |
| 870 |
int r; |
| 871 |
|
| 872 |
r = usb_register(&driver); |
| 873 |
if (r) { |
| 874 |
pr_err("%s usb_register() failed %d\n", driver.name, r); |
| 875 |
return r; |
| 876 |
} |
| 877 |
|
| 878 |
pr_debug("Driver initialized :%s\n", driver.name); |
| 879 |
return 0; |
| 880 |
} |
| 881 |
|
| 882 |
static void __exit usb_exit(void) |
| 883 |
{ |
| 884 |
usb_deregister(&driver); |
| 885 |
pr_debug("%s %s\n", driver.name, __func__); |
| 886 |
} |
| 887 |
|
| 888 |
MODULE_LICENSE("GPL"); |
| 889 |
MODULE_DESCRIPTION("USB driver for pureLiFi devices"); |
| 890 |
MODULE_AUTHOR("pureLiFi"); |
| 891 |
MODULE_VERSION("1.0"); |
| 892 |
MODULE_FIRMWARE("plfxlc/lifi-x.bin"); |
| 893 |
MODULE_DEVICE_TABLE(usb, usb_ids); |
| 894 |
|
| 895 |
module_init(usb_init); |
| 896 |
module_exit(usb_exit); |