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 return r; 251 } 252 253 int plfxlc_usb_enable_rx(struct plfxlc_usb *usb) 254 { 255 struct plfxlc_usb_rx *rx = &usb->rx; 256 int r; 257 258 mutex_lock(&rx->setup_mutex); 259 r = __lf_x_usb_enable_rx(usb); 260 if (!r) 261 usb->rx_usb_enabled = 1; 262 263 mutex_unlock(&rx->setup_mutex); 264 265 return r; 266 } 267 268 static void __lf_x_usb_disable_rx(struct plfxlc_usb *usb) 269 { 270 struct plfxlc_usb_rx *rx = &usb->rx; 271 unsigned long flags; 272 unsigned int count; 273 struct urb **urbs; 274 int i; 275 276 spin_lock_irqsave(&rx->lock, flags); 277 urbs = rx->urbs; 278 count = rx->urbs_count; 279 spin_unlock_irqrestore(&rx->lock, flags); 280 281 if (!urbs) 282 return; 283 284 for (i = 0; i < count; i++) { 285 usb_kill_urb(urbs[i]); 286 free_rx_urb(urbs[i]); 287 } 288 kfree(urbs); 289 rx->urbs = NULL; 290 rx->urbs_count = 0; 291 } 292 293 void plfxlc_usb_disable_rx(struct plfxlc_usb *usb) 294 { 295 struct plfxlc_usb_rx *rx = &usb->rx; 296 297 mutex_lock(&rx->setup_mutex); 298 __lf_x_usb_disable_rx(usb); 299 usb->rx_usb_enabled = 0; 300 mutex_unlock(&rx->setup_mutex); 301 } 302 303 void plfxlc_usb_disable_tx(struct plfxlc_usb *usb) 304 { 305 struct plfxlc_usb_tx *tx = &usb->tx; 306 unsigned long flags; 307 308 clear_bit(PLF_BIT_ENABLED, &tx->enabled); 309 310 /* kill all submitted tx-urbs */ 311 usb_kill_anchored_urbs(&tx->submitted); 312 313 spin_lock_irqsave(&tx->lock, flags); 314 WARN_ON(!skb_queue_empty(&tx->submitted_skbs)); 315 WARN_ON(tx->submitted_urbs != 0); 316 tx->submitted_urbs = 0; 317 spin_unlock_irqrestore(&tx->lock, flags); 318 319 /* The stopped state is ignored, relying on ieee80211_wake_queues() 320 * in a potentionally following plfxlc_usb_enable_tx(). 321 */ 322 } 323 324 void plfxlc_usb_enable_tx(struct plfxlc_usb *usb) 325 { 326 struct plfxlc_usb_tx *tx = &usb->tx; 327 unsigned long flags; 328 329 spin_lock_irqsave(&tx->lock, flags); 330 set_bit(PLF_BIT_ENABLED, &tx->enabled); 331 tx->submitted_urbs = 0; 332 ieee80211_wake_queues(plfxlc_usb_to_hw(usb)); 333 tx->stopped = 0; 334 spin_unlock_irqrestore(&tx->lock, flags); 335 } 336 337 void plfxlc_tx_urb_complete(struct urb *urb) 338 { 339 struct ieee80211_tx_info *info; 340 struct plfxlc_usb *usb; 341 struct sk_buff *skb; 342 343 skb = urb->context; 344 info = IEEE80211_SKB_CB(skb); 345 /* grab 'usb' pointer before handing off the skb (since 346 * it might be freed by plfxlc_mac_tx_to_dev or mac80211) 347 */ 348 usb = &plfxlc_hw_mac(info->rate_driver_data[0])->chip.usb; 349 350 switch (urb->status) { 351 case 0: 352 break; 353 case -ESHUTDOWN: 354 case -EINVAL: 355 case -ENODEV: 356 case -ENOENT: 357 case -ECONNRESET: 358 case -EPIPE: 359 dev_dbg(plfxlc_urb_dev(urb), "urb %p error %d\n", urb, urb->status); 360 break; 361 default: 362 dev_dbg(plfxlc_urb_dev(urb), "urb %p error %d\n", urb, urb->status); 363 return; 364 } 365 366 plfxlc_mac_tx_to_dev(skb, urb->status); 367 plfxlc_send_packet_from_data_queue(usb); 368 usb_free_urb(urb); 369 } 370 371 static inline void init_usb_rx(struct plfxlc_usb *usb) 372 { 373 struct plfxlc_usb_rx *rx = &usb->rx; 374 375 spin_lock_init(&rx->lock); 376 mutex_init(&rx->setup_mutex); 377 378 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) 379 rx->usb_packet_size = 512; 380 else 381 rx->usb_packet_size = 64; 382 383 if (rx->fragment_length != 0) 384 dev_dbg(plfxlc_usb_dev(usb), "fragment_length error\n"); 385 } 386 387 static inline void init_usb_tx(struct plfxlc_usb *usb) 388 { 389 struct plfxlc_usb_tx *tx = &usb->tx; 390 391 spin_lock_init(&tx->lock); 392 clear_bit(PLF_BIT_ENABLED, &tx->enabled); 393 tx->stopped = 0; 394 skb_queue_head_init(&tx->submitted_skbs); 395 init_usb_anchor(&tx->submitted); 396 } 397 398 void plfxlc_usb_init(struct plfxlc_usb *usb, struct ieee80211_hw *hw, 399 struct usb_interface *intf) 400 { 401 memset(usb, 0, sizeof(*usb)); 402 usb->intf = usb_get_intf(intf); 403 usb_set_intfdata(usb->intf, hw); 404 init_usb_tx(usb); 405 init_usb_rx(usb); 406 } 407 408 void plfxlc_usb_release(struct plfxlc_usb *usb) 409 { 410 plfxlc_op_stop(plfxlc_usb_to_hw(usb)); 411 plfxlc_usb_disable_tx(usb); 412 plfxlc_usb_disable_rx(usb); 413 usb_set_intfdata(usb->intf, NULL); 414 usb_put_intf(usb->intf); 415 } 416 417 const char *plfxlc_speed(enum usb_device_speed speed) 418 { 419 switch (speed) { 420 case USB_SPEED_LOW: 421 return "low"; 422 case USB_SPEED_FULL: 423 return "full"; 424 case USB_SPEED_HIGH: 425 return "high"; 426 default: 427 return "unknown"; 428 } 429 } 430 431 int plfxlc_usb_init_hw(struct plfxlc_usb *usb) 432 { 433 int r; 434 435 r = usb_reset_configuration(plfxlc_usb_to_usbdev(usb)); 436 if (r) { 437 dev_err(plfxlc_usb_dev(usb), "cfg reset failed (%d)\n", r); 438 return r; 439 } 440 return 0; 441 } 442 443 static void get_usb_req(struct usb_device *udev, void *buffer, 444 u32 buffer_len, enum plf_usb_req_enum usb_req_id, 445 struct plf_usb_req *usb_req) 446 { 447 __be32 payload_len_nw = cpu_to_be32(buffer_len + FCS_LEN); 448 const u8 *buffer_src_p = buffer; 449 u8 *buffer_dst = usb_req->buf; 450 u32 temp_usb_len = 0; 451 452 usb_req->id = cpu_to_be32(usb_req_id); 453 usb_req->len = cpu_to_be32(0); 454 455 /* Copy buffer length into the transmitted buffer, as it is important 456 * for the Rx MAC to know its exact length. 457 */ 458 if (usb_req->id == cpu_to_be32(USB_REQ_BEACON_WR)) { 459 memcpy(buffer_dst, &payload_len_nw, sizeof(payload_len_nw)); 460 buffer_dst += sizeof(payload_len_nw); 461 temp_usb_len += sizeof(payload_len_nw); 462 } 463 464 memcpy(buffer_dst, buffer_src_p, buffer_len); 465 buffer_dst += buffer_len; 466 buffer_src_p += buffer_len; 467 temp_usb_len += buffer_len; 468 469 /* Set the FCS_LEN (4) bytes as 0 for CRC checking. */ 470 memset(buffer_dst, 0, FCS_LEN); 471 buffer_dst += FCS_LEN; 472 temp_usb_len += FCS_LEN; 473 474 /* Round the packet to be transmitted to 4 bytes. */ 475 if (temp_usb_len % PURELIFI_BYTE_NUM_ALIGNMENT) { 476 memset(buffer_dst, 0, PURELIFI_BYTE_NUM_ALIGNMENT - 477 (temp_usb_len % 478 PURELIFI_BYTE_NUM_ALIGNMENT)); 479 buffer_dst += PURELIFI_BYTE_NUM_ALIGNMENT - 480 (temp_usb_len % 481 PURELIFI_BYTE_NUM_ALIGNMENT); 482 temp_usb_len += PURELIFI_BYTE_NUM_ALIGNMENT - 483 (temp_usb_len % PURELIFI_BYTE_NUM_ALIGNMENT); 484 } 485 486 usb_req->len = cpu_to_be32(temp_usb_len); 487 } 488 489 int plfxlc_usb_wreq_async(struct plfxlc_usb *usb, const u8 *buffer, 490 int buffer_len, enum plf_usb_req_enum usb_req_id, 491 usb_complete_t complete_fn, 492 void *context) 493 { 494 struct usb_device *udev = interface_to_usbdev(usb->ez_usb); 495 struct urb *urb = usb_alloc_urb(0, GFP_ATOMIC); 496 int r; 497 498 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT), 499 (void *)buffer, buffer_len, complete_fn, context); 500 501 r = usb_submit_urb(urb, GFP_ATOMIC); 502 if (r) 503 dev_err(&udev->dev, "Async write submit failed (%d)\n", r); 504 505 return r; 506 } 507 508 int plfxlc_usb_wreq(struct usb_interface *ez_usb, void *buffer, int buffer_len, 509 enum plf_usb_req_enum usb_req_id) 510 { 511 struct usb_device *udev = interface_to_usbdev(ez_usb); 512 unsigned char *dma_buffer = NULL; 513 struct plf_usb_req usb_req; 514 int usb_bulk_msg_len; 515 int actual_length; 516 int r; 517 518 get_usb_req(udev, buffer, buffer_len, usb_req_id, &usb_req); 519 usb_bulk_msg_len = sizeof(__le32) + sizeof(__le32) + 520 be32_to_cpu(usb_req.len); 521 522 dma_buffer = kmemdup(&usb_req, usb_bulk_msg_len, GFP_KERNEL); 523 524 if (!dma_buffer) { 525 r = -ENOMEM; 526 goto error; 527 } 528 529 r = usb_bulk_msg(udev, 530 usb_sndbulkpipe(udev, EP_DATA_OUT), 531 dma_buffer, usb_bulk_msg_len, 532 &actual_length, USB_BULK_MSG_TIMEOUT_MS); 533 kfree(dma_buffer); 534 error: 535 if (r) { 536 r = -ENOMEM; 537 dev_err(&udev->dev, "usb_bulk_msg failed (%d)\n", r); 538 } 539 540 return r; 541 } 542 543 static void slif_data_plane_sap_timer_callb(struct timer_list *t) 544 { 545 struct plfxlc_usb *usb = from_timer(usb, t, tx.tx_retry_timer); 546 547 plfxlc_send_packet_from_data_queue(usb); 548 timer_setup(&usb->tx.tx_retry_timer, 549 slif_data_plane_sap_timer_callb, 0); 550 mod_timer(&usb->tx.tx_retry_timer, jiffies + TX_RETRY_BACKOFF_JIFF); 551 } 552 553 static void sta_queue_cleanup_timer_callb(struct timer_list *t) 554 { 555 struct plfxlc_usb *usb = from_timer(usb, t, sta_queue_cleanup); 556 struct plfxlc_usb_tx *tx = &usb->tx; 557 int sidx; 558 559 for (sidx = 0; sidx < MAX_STA_NUM - 1; sidx++) { 560 if (!(tx->station[sidx].flag & STATION_CONNECTED_FLAG)) 561 continue; 562 if (tx->station[sidx].flag & STATION_HEARTBEAT_FLAG) { 563 tx->station[sidx].flag ^= STATION_HEARTBEAT_FLAG; 564 } else { 565 eth_zero_addr(tx->station[sidx].mac); 566 tx->station[sidx].flag = 0; 567 } 568 } 569 timer_setup(&usb->sta_queue_cleanup, 570 sta_queue_cleanup_timer_callb, 0); 571 mod_timer(&usb->sta_queue_cleanup, jiffies + STA_QUEUE_CLEANUP_JIFF); 572 } 573 574 static int probe(struct usb_interface *intf, 575 const struct usb_device_id *id) 576 { 577 u8 serial_number[PURELIFI_SERIAL_LEN]; 578 struct ieee80211_hw *hw = NULL; 579 struct plfxlc_usb_tx *tx; 580 struct plfxlc_chip *chip; 581 struct plfxlc_usb *usb; 582 u8 hw_address[ETH_ALEN]; 583 unsigned int i; 584 int r = 0; 585 586 hw = plfxlc_mac_alloc_hw(intf); 587 588 if (!hw) { 589 r = -ENOMEM; 590 goto error; 591 } 592 593 chip = &plfxlc_hw_mac(hw)->chip; 594 usb = &chip->usb; 595 usb->ez_usb = intf; 596 tx = &usb->tx; 597 598 r = plfxlc_upload_mac_and_serial(intf, hw_address, serial_number); 599 if (r) { 600 dev_err(&intf->dev, "MAC and Serial upload failed (%d)\n", r); 601 goto error; 602 } 603 604 chip->unit_type = STA; 605 dev_err(&intf->dev, "Unit type is station"); 606 607 r = plfxlc_mac_preinit_hw(hw, hw_address); 608 if (r) { 609 dev_err(&intf->dev, "Init mac failed (%d)\n", r); 610 goto error; 611 } 612 613 r = ieee80211_register_hw(hw); 614 if (r) { 615 dev_err(&intf->dev, "Register device failed (%d)\n", r); 616 goto error; 617 } 618 619 if ((le16_to_cpu(interface_to_usbdev(intf)->descriptor.idVendor) == 620 PURELIFI_XL_VENDOR_ID_0) && 621 (le16_to_cpu(interface_to_usbdev(intf)->descriptor.idProduct) == 622 PURELIFI_XL_PRODUCT_ID_0)) { 623 r = plfxlc_download_xl_firmware(intf); 624 } else { 625 r = plfxlc_download_fpga(intf); 626 } 627 if (r != 0) { 628 dev_err(&intf->dev, "FPGA download failed (%d)\n", r); 629 goto error; 630 } 631 632 tx->mac_fifo_full = 0; 633 spin_lock_init(&tx->lock); 634 635 msleep(PLF_MSLEEP_TIME); 636 r = plfxlc_usb_init_hw(usb); 637 if (r < 0) { 638 dev_err(&intf->dev, "usb_init_hw failed (%d)\n", r); 639 goto error; 640 } 641 642 msleep(PLF_MSLEEP_TIME); 643 r = plfxlc_chip_switch_radio(chip, PLFXLC_RADIO_ON); 644 if (r < 0) { 645 dev_dbg(&intf->dev, "chip_switch_radio_on failed (%d)\n", r); 646 goto error; 647 } 648 649 msleep(PLF_MSLEEP_TIME); 650 r = plfxlc_chip_set_rate(chip, 8); 651 if (r < 0) { 652 dev_dbg(&intf->dev, "chip_set_rate failed (%d)\n", r); 653 goto error; 654 } 655 656 msleep(PLF_MSLEEP_TIME); 657 r = plfxlc_usb_wreq(usb->ez_usb, 658 hw_address, ETH_ALEN, USB_REQ_MAC_WR); 659 if (r < 0) { 660 dev_dbg(&intf->dev, "MAC_WR failure (%d)\n", r); 661 goto error; 662 } 663 664 plfxlc_chip_enable_rxtx(chip); 665 666 /* Initialise the data plane Tx queue */ 667 for (i = 0; i < MAX_STA_NUM; i++) { 668 skb_queue_head_init(&tx->station[i].data_list); 669 tx->station[i].flag = 0; 670 } 671 672 tx->station[STA_BROADCAST_INDEX].flag |= STATION_CONNECTED_FLAG; 673 for (i = 0; i < ETH_ALEN; i++) 674 tx->station[STA_BROADCAST_INDEX].mac[i] = 0xFF; 675 676 timer_setup(&tx->tx_retry_timer, slif_data_plane_sap_timer_callb, 0); 677 tx->tx_retry_timer.expires = jiffies + TX_RETRY_BACKOFF_JIFF; 678 add_timer(&tx->tx_retry_timer); 679 680 timer_setup(&usb->sta_queue_cleanup, 681 sta_queue_cleanup_timer_callb, 0); 682 usb->sta_queue_cleanup.expires = jiffies + STA_QUEUE_CLEANUP_JIFF; 683 add_timer(&usb->sta_queue_cleanup); 684 685 plfxlc_mac_init_hw(hw); 686 usb->initialized = true; 687 return 0; 688 error: 689 if (hw) { 690 plfxlc_mac_release(plfxlc_hw_mac(hw)); 691 ieee80211_unregister_hw(hw); 692 ieee80211_free_hw(hw); 693 } 694 dev_err(&intf->dev, "pureLifi:Device error"); 695 return r; 696 } 697 698 static void disconnect(struct usb_interface *intf) 699 { 700 struct ieee80211_hw *hw = plfxlc_intf_to_hw(intf); 701 struct plfxlc_mac *mac; 702 struct plfxlc_usb *usb; 703 704 /* Either something really bad happened, or 705 * we're just dealing with a DEVICE_INSTALLER. 706 */ 707 if (!hw) 708 return; 709 710 mac = plfxlc_hw_mac(hw); 711 usb = &mac->chip.usb; 712 713 del_timer_sync(&usb->tx.tx_retry_timer); 714 del_timer_sync(&usb->sta_queue_cleanup); 715 716 ieee80211_unregister_hw(hw); 717 718 plfxlc_chip_disable_rxtx(&mac->chip); 719 720 /* If the disconnect has been caused by a removal of the 721 * driver module, the reset allows reloading of the driver. If the 722 * reset will not be executed here, the upload of the firmware in the 723 * probe function caused by the reloading of the driver will fail. 724 */ 725 usb_reset_device(interface_to_usbdev(intf)); 726 727 plfxlc_mac_release(mac); 728 ieee80211_free_hw(hw); 729 } 730 731 static void plfxlc_usb_resume(struct plfxlc_usb *usb) 732 { 733 struct plfxlc_mac *mac = plfxlc_usb_to_mac(usb); 734 int r; 735 736 r = plfxlc_op_start(plfxlc_usb_to_hw(usb)); 737 if (r < 0) { 738 dev_warn(plfxlc_usb_dev(usb), 739 "Device resume failed (%d)\n", r); 740 741 if (usb->was_running) 742 set_bit(PURELIFI_DEVICE_RUNNING, &mac->flags); 743 744 usb_queue_reset_device(usb->intf); 745 return; 746 } 747 748 if (mac->type != NL80211_IFTYPE_UNSPECIFIED) { 749 r = plfxlc_restore_settings(mac); 750 if (r < 0) { 751 dev_dbg(plfxlc_usb_dev(usb), 752 "Restore failed (%d)\n", r); 753 return; 754 } 755 } 756 } 757 758 static void plfxlc_usb_stop(struct plfxlc_usb *usb) 759 { 760 plfxlc_op_stop(plfxlc_usb_to_hw(usb)); 761 plfxlc_usb_disable_tx(usb); 762 plfxlc_usb_disable_rx(usb); 763 764 usb->initialized = false; 765 } 766 767 static int pre_reset(struct usb_interface *intf) 768 { 769 struct ieee80211_hw *hw = usb_get_intfdata(intf); 770 struct plfxlc_mac *mac; 771 struct plfxlc_usb *usb; 772 773 if (!hw || intf->condition != USB_INTERFACE_BOUND) 774 return 0; 775 776 mac = plfxlc_hw_mac(hw); 777 usb = &mac->chip.usb; 778 779 usb->was_running = test_bit(PURELIFI_DEVICE_RUNNING, &mac->flags); 780 781 plfxlc_usb_stop(usb); 782 783 return 0; 784 } 785 786 static int post_reset(struct usb_interface *intf) 787 { 788 struct ieee80211_hw *hw = usb_get_intfdata(intf); 789 struct plfxlc_mac *mac; 790 struct plfxlc_usb *usb; 791 792 if (!hw || intf->condition != USB_INTERFACE_BOUND) 793 return 0; 794 795 mac = plfxlc_hw_mac(hw); 796 usb = &mac->chip.usb; 797 798 if (usb->was_running) 799 plfxlc_usb_resume(usb); 800 801 return 0; 802 } 803 804 #ifdef CONFIG_PM 805 806 static struct plfxlc_usb *get_plfxlc_usb(struct usb_interface *intf) 807 { 808 struct ieee80211_hw *hw = plfxlc_intf_to_hw(intf); 809 struct plfxlc_mac *mac; 810 811 /* Either something really bad happened, or 812 * we're just dealing with a DEVICE_INSTALLER. 813 */ 814 if (!hw) 815 return NULL; 816 817 mac = plfxlc_hw_mac(hw); 818 return &mac->chip.usb; 819 } 820 821 static int suspend(struct usb_interface *interface, 822 pm_message_t message) 823 { 824 struct plfxlc_usb *pl = get_plfxlc_usb(interface); 825 struct plfxlc_mac *mac = plfxlc_usb_to_mac(pl); 826 827 if (!pl) 828 return -ENODEV; 829 if (pl->initialized == 0) 830 return 0; 831 pl->was_running = test_bit(PURELIFI_DEVICE_RUNNING, &mac->flags); 832 plfxlc_usb_stop(pl); 833 return 0; 834 } 835 836 static int resume(struct usb_interface *interface) 837 { 838 struct plfxlc_usb *pl = get_plfxlc_usb(interface); 839 840 if (!pl) 841 return -ENODEV; 842 if (pl->was_running) 843 plfxlc_usb_resume(pl); 844 return 0; 845 } 846 847 #endif 848 849 static struct usb_driver driver = { 850 .name = KBUILD_MODNAME, 851 .id_table = usb_ids, 852 .probe = probe, 853 .disconnect = disconnect, 854 .pre_reset = pre_reset, 855 .post_reset = post_reset, 856 #ifdef CONFIG_PM 857 .suspend = suspend, 858 .resume = resume, 859 #endif 860 .disable_hub_initiated_lpm = 1, 861 }; 862 863 static int __init usb_init(void) 864 { 865 int r; 866 867 r = usb_register(&driver); 868 if (r) { 869 pr_err("%s usb_register() failed %d\n", driver.name, r); 870 return r; 871 } 872 873 pr_debug("Driver initialized :%s\n", driver.name); 874 return 0; 875 } 876 877 static void __exit usb_exit(void) 878 { 879 usb_deregister(&driver); 880 pr_debug("%s %s\n", driver.name, __func__); 881 } 882 883 MODULE_LICENSE("GPL"); 884 MODULE_DESCRIPTION("USB driver for pureLiFi devices"); 885 MODULE_AUTHOR("pureLiFi"); 886 MODULE_VERSION("1.0"); 887 MODULE_FIRMWARE("plfxlc/lifi-x.bin"); 888 MODULE_DEVICE_TABLE(usb, usb_ids); 889 890 module_init(usb_init); 891 module_exit(usb_exit); 892