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 <asm/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 = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL); 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)); 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 = usb_alloc_urb(0, GFP_ATOMIC); 497 int r; 498 499 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT), 500 (void *)buffer, buffer_len, complete_fn, context); 501 502 r = usb_submit_urb(urb, GFP_ATOMIC); 503 if (r) 504 dev_err(&udev->dev, "Async write submit failed (%d)\n", r); 505 506 return r; 507 } 508 509 int plfxlc_usb_wreq(struct usb_interface *ez_usb, void *buffer, int buffer_len, 510 enum plf_usb_req_enum usb_req_id) 511 { 512 struct usb_device *udev = interface_to_usbdev(ez_usb); 513 unsigned char *dma_buffer = NULL; 514 struct plf_usb_req usb_req; 515 int usb_bulk_msg_len; 516 int actual_length; 517 int r; 518 519 get_usb_req(udev, buffer, buffer_len, usb_req_id, &usb_req); 520 usb_bulk_msg_len = sizeof(__le32) + sizeof(__le32) + 521 be32_to_cpu(usb_req.len); 522 523 dma_buffer = kmemdup(&usb_req, usb_bulk_msg_len, GFP_KERNEL); 524 525 if (!dma_buffer) { 526 r = -ENOMEM; 527 goto error; 528 } 529 530 r = usb_bulk_msg(udev, 531 usb_sndbulkpipe(udev, EP_DATA_OUT), 532 dma_buffer, usb_bulk_msg_len, 533 &actual_length, USB_BULK_MSG_TIMEOUT_MS); 534 kfree(dma_buffer); 535 error: 536 if (r) { 537 r = -ENOMEM; 538 dev_err(&udev->dev, "usb_bulk_msg failed (%d)\n", r); 539 } 540 541 return r; 542 } 543 544 static void slif_data_plane_sap_timer_callb(struct timer_list *t) 545 { 546 struct plfxlc_usb *usb = from_timer(usb, t, tx.tx_retry_timer); 547 548 plfxlc_send_packet_from_data_queue(usb); 549 timer_setup(&usb->tx.tx_retry_timer, 550 slif_data_plane_sap_timer_callb, 0); 551 mod_timer(&usb->tx.tx_retry_timer, jiffies + TX_RETRY_BACKOFF_JIFF); 552 } 553 554 static void sta_queue_cleanup_timer_callb(struct timer_list *t) 555 { 556 struct plfxlc_usb *usb = from_timer(usb, t, sta_queue_cleanup); 557 struct plfxlc_usb_tx *tx = &usb->tx; 558 int sidx; 559 560 for (sidx = 0; sidx < MAX_STA_NUM - 1; sidx++) { 561 if (!(tx->station[sidx].flag & STATION_CONNECTED_FLAG)) 562 continue; 563 if (tx->station[sidx].flag & STATION_HEARTBEAT_FLAG) { 564 tx->station[sidx].flag ^= STATION_HEARTBEAT_FLAG; 565 } else { 566 eth_zero_addr(tx->station[sidx].mac); 567 tx->station[sidx].flag = 0; 568 } 569 } 570 timer_setup(&usb->sta_queue_cleanup, 571 sta_queue_cleanup_timer_callb, 0); 572 mod_timer(&usb->sta_queue_cleanup, jiffies + STA_QUEUE_CLEANUP_JIFF); 573 } 574 575 static int probe(struct usb_interface *intf, 576 const struct usb_device_id *id) 577 { 578 u8 serial_number[PURELIFI_SERIAL_LEN]; 579 struct ieee80211_hw *hw = NULL; 580 struct plfxlc_usb_tx *tx; 581 struct plfxlc_chip *chip; 582 struct plfxlc_usb *usb; 583 u8 hw_address[ETH_ALEN]; 584 unsigned int i; 585 int r = 0; 586 587 hw = plfxlc_mac_alloc_hw(intf); 588 589 if (!hw) { 590 r = -ENOMEM; 591 goto error; 592 } 593 594 chip = &plfxlc_hw_mac(hw)->chip; 595 usb = &chip->usb; 596 usb->ez_usb = intf; 597 tx = &usb->tx; 598 599 r = plfxlc_upload_mac_and_serial(intf, hw_address, serial_number); 600 if (r) { 601 dev_err(&intf->dev, "MAC and Serial upload failed (%d)\n", r); 602 goto error; 603 } 604 605 chip->unit_type = STA; 606 dev_err(&intf->dev, "Unit type is station"); 607 608 r = plfxlc_mac_preinit_hw(hw, hw_address); 609 if (r) { 610 dev_err(&intf->dev, "Init mac failed (%d)\n", r); 611 goto error; 612 } 613 614 r = ieee80211_register_hw(hw); 615 if (r) { 616 dev_err(&intf->dev, "Register device failed (%d)\n", r); 617 goto error; 618 } 619 620 if ((le16_to_cpu(interface_to_usbdev(intf)->descriptor.idVendor) == 621 PURELIFI_XL_VENDOR_ID_0) && 622 (le16_to_cpu(interface_to_usbdev(intf)->descriptor.idProduct) == 623 PURELIFI_XL_PRODUCT_ID_0)) { 624 r = plfxlc_download_xl_firmware(intf); 625 } else { 626 r = plfxlc_download_fpga(intf); 627 } 628 if (r != 0) { 629 dev_err(&intf->dev, "FPGA download failed (%d)\n", r); 630 goto error; 631 } 632 633 tx->mac_fifo_full = 0; 634 spin_lock_init(&tx->lock); 635 636 msleep(PLF_MSLEEP_TIME); 637 r = plfxlc_usb_init_hw(usb); 638 if (r < 0) { 639 dev_err(&intf->dev, "usb_init_hw failed (%d)\n", r); 640 goto error; 641 } 642 643 msleep(PLF_MSLEEP_TIME); 644 r = plfxlc_chip_switch_radio(chip, PLFXLC_RADIO_ON); 645 if (r < 0) { 646 dev_dbg(&intf->dev, "chip_switch_radio_on failed (%d)\n", r); 647 goto error; 648 } 649 650 msleep(PLF_MSLEEP_TIME); 651 r = plfxlc_chip_set_rate(chip, 8); 652 if (r < 0) { 653 dev_dbg(&intf->dev, "chip_set_rate failed (%d)\n", r); 654 goto error; 655 } 656 657 msleep(PLF_MSLEEP_TIME); 658 r = plfxlc_usb_wreq(usb->ez_usb, 659 hw_address, ETH_ALEN, USB_REQ_MAC_WR); 660 if (r < 0) { 661 dev_dbg(&intf->dev, "MAC_WR failure (%d)\n", r); 662 goto error; 663 } 664 665 plfxlc_chip_enable_rxtx(chip); 666 667 /* Initialise the data plane Tx queue */ 668 for (i = 0; i < MAX_STA_NUM; i++) { 669 skb_queue_head_init(&tx->station[i].data_list); 670 tx->station[i].flag = 0; 671 } 672 673 tx->station[STA_BROADCAST_INDEX].flag |= STATION_CONNECTED_FLAG; 674 for (i = 0; i < ETH_ALEN; i++) 675 tx->station[STA_BROADCAST_INDEX].mac[i] = 0xFF; 676 677 timer_setup(&tx->tx_retry_timer, slif_data_plane_sap_timer_callb, 0); 678 tx->tx_retry_timer.expires = jiffies + TX_RETRY_BACKOFF_JIFF; 679 add_timer(&tx->tx_retry_timer); 680 681 timer_setup(&usb->sta_queue_cleanup, 682 sta_queue_cleanup_timer_callb, 0); 683 usb->sta_queue_cleanup.expires = jiffies + STA_QUEUE_CLEANUP_JIFF; 684 add_timer(&usb->sta_queue_cleanup); 685 686 plfxlc_mac_init_hw(hw); 687 usb->initialized = true; 688 return 0; 689 error: 690 if (hw) { 691 plfxlc_mac_release(plfxlc_hw_mac(hw)); 692 ieee80211_unregister_hw(hw); 693 ieee80211_free_hw(hw); 694 } 695 dev_err(&intf->dev, "pureLifi:Device error"); 696 return r; 697 } 698 699 static void disconnect(struct usb_interface *intf) 700 { 701 struct ieee80211_hw *hw = plfxlc_intf_to_hw(intf); 702 struct plfxlc_mac *mac; 703 struct plfxlc_usb *usb; 704 705 /* Either something really bad happened, or 706 * we're just dealing with a DEVICE_INSTALLER. 707 */ 708 if (!hw) 709 return; 710 711 mac = plfxlc_hw_mac(hw); 712 usb = &mac->chip.usb; 713 714 del_timer_sync(&usb->tx.tx_retry_timer); 715 del_timer_sync(&usb->sta_queue_cleanup); 716 717 ieee80211_unregister_hw(hw); 718 719 plfxlc_chip_disable_rxtx(&mac->chip); 720 721 /* If the disconnect has been caused by a removal of the 722 * driver module, the reset allows reloading of the driver. If the 723 * reset will not be executed here, the upload of the firmware in the 724 * probe function caused by the reloading of the driver will fail. 725 */ 726 usb_reset_device(interface_to_usbdev(intf)); 727 728 plfxlc_mac_release(mac); 729 ieee80211_free_hw(hw); 730 } 731 732 static void plfxlc_usb_resume(struct plfxlc_usb *usb) 733 { 734 struct plfxlc_mac *mac = plfxlc_usb_to_mac(usb); 735 int r; 736 737 r = plfxlc_op_start(plfxlc_usb_to_hw(usb)); 738 if (r < 0) { 739 dev_warn(plfxlc_usb_dev(usb), 740 "Device resume failed (%d)\n", r); 741 742 if (usb->was_running) 743 set_bit(PURELIFI_DEVICE_RUNNING, &mac->flags); 744 745 usb_queue_reset_device(usb->intf); 746 return; 747 } 748 749 if (mac->type != NL80211_IFTYPE_UNSPECIFIED) { 750 r = plfxlc_restore_settings(mac); 751 if (r < 0) { 752 dev_dbg(plfxlc_usb_dev(usb), 753 "Restore failed (%d)\n", r); 754 return; 755 } 756 } 757 } 758 759 static void plfxlc_usb_stop(struct plfxlc_usb *usb) 760 { 761 plfxlc_op_stop(plfxlc_usb_to_hw(usb)); 762 plfxlc_usb_disable_tx(usb); 763 plfxlc_usb_disable_rx(usb); 764 765 usb->initialized = false; 766 } 767 768 static int pre_reset(struct usb_interface *intf) 769 { 770 struct ieee80211_hw *hw = usb_get_intfdata(intf); 771 struct plfxlc_mac *mac; 772 struct plfxlc_usb *usb; 773 774 if (!hw || intf->condition != USB_INTERFACE_BOUND) 775 return 0; 776 777 mac = plfxlc_hw_mac(hw); 778 usb = &mac->chip.usb; 779 780 usb->was_running = test_bit(PURELIFI_DEVICE_RUNNING, &mac->flags); 781 782 plfxlc_usb_stop(usb); 783 784 return 0; 785 } 786 787 static int post_reset(struct usb_interface *intf) 788 { 789 struct ieee80211_hw *hw = usb_get_intfdata(intf); 790 struct plfxlc_mac *mac; 791 struct plfxlc_usb *usb; 792 793 if (!hw || intf->condition != USB_INTERFACE_BOUND) 794 return 0; 795 796 mac = plfxlc_hw_mac(hw); 797 usb = &mac->chip.usb; 798 799 if (usb->was_running) 800 plfxlc_usb_resume(usb); 801 802 return 0; 803 } 804 805 #ifdef CONFIG_PM 806 807 static struct plfxlc_usb *get_plfxlc_usb(struct usb_interface *intf) 808 { 809 struct ieee80211_hw *hw = plfxlc_intf_to_hw(intf); 810 struct plfxlc_mac *mac; 811 812 /* Either something really bad happened, or 813 * we're just dealing with a DEVICE_INSTALLER. 814 */ 815 if (!hw) 816 return NULL; 817 818 mac = plfxlc_hw_mac(hw); 819 return &mac->chip.usb; 820 } 821 822 static int suspend(struct usb_interface *interface, 823 pm_message_t message) 824 { 825 struct plfxlc_usb *pl = get_plfxlc_usb(interface); 826 struct plfxlc_mac *mac = plfxlc_usb_to_mac(pl); 827 828 if (!pl) 829 return -ENODEV; 830 if (pl->initialized == 0) 831 return 0; 832 pl->was_running = test_bit(PURELIFI_DEVICE_RUNNING, &mac->flags); 833 plfxlc_usb_stop(pl); 834 return 0; 835 } 836 837 static int resume(struct usb_interface *interface) 838 { 839 struct plfxlc_usb *pl = get_plfxlc_usb(interface); 840 841 if (!pl) 842 return -ENODEV; 843 if (pl->was_running) 844 plfxlc_usb_resume(pl); 845 return 0; 846 } 847 848 #endif 849 850 static struct usb_driver driver = { 851 .name = KBUILD_MODNAME, 852 .id_table = usb_ids, 853 .probe = probe, 854 .disconnect = disconnect, 855 .pre_reset = pre_reset, 856 .post_reset = post_reset, 857 #ifdef CONFIG_PM 858 .suspend = suspend, 859 .resume = resume, 860 #endif 861 .disable_hub_initiated_lpm = 1, 862 }; 863 864 static int __init usb_init(void) 865 { 866 int r; 867 868 r = usb_register(&driver); 869 if (r) { 870 pr_err("%s usb_register() failed %d\n", driver.name, r); 871 return r; 872 } 873 874 pr_debug("Driver initialized :%s\n", driver.name); 875 return 0; 876 } 877 878 static void __exit usb_exit(void) 879 { 880 usb_deregister(&driver); 881 pr_debug("%s %s\n", driver.name, __func__); 882 } 883 884 MODULE_LICENSE("GPL"); 885 MODULE_DESCRIPTION("USB driver for pureLiFi devices"); 886 MODULE_AUTHOR("pureLiFi"); 887 MODULE_VERSION("1.0"); 888 MODULE_FIRMWARE("plfxlc/lifi-x.bin"); 889 MODULE_DEVICE_TABLE(usb, usb_ids); 890 891 module_init(usb_init); 892 module_exit(usb_exit); 893