0001
0002
0003
0004
0005
0006 #include <linux/kernel.h>
0007 #include <linux/init.h>
0008 #include <linux/device.h>
0009 #include <linux/errno.h>
0010 #include <linux/slab.h>
0011 #include <linux/skbuff.h>
0012 #include <linux/usb.h>
0013 #include <linux/workqueue.h>
0014 #include <linux/proc_fs.h>
0015 #include <linux/fs.h>
0016 #include <linux/string.h>
0017 #include <linux/module.h>
0018 #include <net/mac80211.h>
0019 #include <asm/unaligned.h>
0020 #include <linux/sysfs.h>
0021
0022 #include "mac.h"
0023 #include "usb.h"
0024 #include "chip.h"
0025
0026 static const struct usb_device_id usb_ids[] = {
0027 { USB_DEVICE(PURELIFI_X_VENDOR_ID_0, PURELIFI_X_PRODUCT_ID_0),
0028 .driver_info = DEVICE_LIFI_X },
0029 { USB_DEVICE(PURELIFI_XC_VENDOR_ID_0, PURELIFI_XC_PRODUCT_ID_0),
0030 .driver_info = DEVICE_LIFI_XC },
0031 { USB_DEVICE(PURELIFI_XL_VENDOR_ID_0, PURELIFI_XL_PRODUCT_ID_0),
0032 .driver_info = DEVICE_LIFI_XL },
0033 {}
0034 };
0035
0036 void plfxlc_send_packet_from_data_queue(struct plfxlc_usb *usb)
0037 {
0038 struct plfxlc_usb_tx *tx = &usb->tx;
0039 struct sk_buff *skb = NULL;
0040 unsigned long flags;
0041 u8 last_served_sidx;
0042
0043 spin_lock_irqsave(&tx->lock, flags);
0044 last_served_sidx = usb->sidx;
0045 do {
0046 usb->sidx = (usb->sidx + 1) % MAX_STA_NUM;
0047 if (!(tx->station[usb->sidx].flag & STATION_CONNECTED_FLAG))
0048 continue;
0049 if (!(tx->station[usb->sidx].flag & STATION_FIFO_FULL_FLAG))
0050 skb = skb_peek(&tx->station[usb->sidx].data_list);
0051 } while ((usb->sidx != last_served_sidx) && (!skb));
0052
0053 if (skb) {
0054 skb = skb_dequeue(&tx->station[usb->sidx].data_list);
0055 plfxlc_usb_wreq_async(usb, skb->data, skb->len, USB_REQ_DATA_TX,
0056 plfxlc_tx_urb_complete, skb);
0057 if (skb_queue_len(&tx->station[usb->sidx].data_list) <= 60)
0058 ieee80211_wake_queues(plfxlc_usb_to_hw(usb));
0059 }
0060 spin_unlock_irqrestore(&tx->lock, flags);
0061 }
0062
0063 static void handle_rx_packet(struct plfxlc_usb *usb, const u8 *buffer,
0064 unsigned int length)
0065 {
0066 plfxlc_mac_rx(plfxlc_usb_to_hw(usb), buffer, length);
0067 }
0068
0069 static void rx_urb_complete(struct urb *urb)
0070 {
0071 struct plfxlc_usb_tx *tx;
0072 struct plfxlc_usb *usb;
0073 unsigned int length;
0074 const u8 *buffer;
0075 u16 status;
0076 u8 sidx;
0077 int r;
0078
0079 if (!urb) {
0080 pr_err("urb is NULL\n");
0081 return;
0082 }
0083 if (!urb->context) {
0084 pr_err("urb ctx is NULL\n");
0085 return;
0086 }
0087 usb = urb->context;
0088
0089 if (usb->initialized != 1) {
0090 pr_err("usb is not initialized\n");
0091 return;
0092 }
0093
0094 tx = &usb->tx;
0095 switch (urb->status) {
0096 case 0:
0097 break;
0098 case -ESHUTDOWN:
0099 case -EINVAL:
0100 case -ENODEV:
0101 case -ENOENT:
0102 case -ECONNRESET:
0103 case -EPIPE:
0104 dev_dbg(plfxlc_urb_dev(urb), "urb %p error %d\n", urb, urb->status);
0105 return;
0106 default:
0107 dev_dbg(plfxlc_urb_dev(urb), "urb %p error %d\n", urb, urb->status);
0108 if (tx->submitted_urbs++ < PURELIFI_URB_RETRY_MAX) {
0109 dev_dbg(plfxlc_urb_dev(urb), "urb %p resubmit %d", urb,
0110 tx->submitted_urbs++);
0111 goto resubmit;
0112 } else {
0113 dev_dbg(plfxlc_urb_dev(urb), "urb %p max resubmits reached", urb);
0114 tx->submitted_urbs = 0;
0115 return;
0116 }
0117 }
0118
0119 buffer = urb->transfer_buffer;
0120 length = le32_to_cpu(*(__le32 *)(buffer + sizeof(struct rx_status)))
0121 + sizeof(u32);
0122
0123 if (urb->actual_length != (PLF_MSG_STATUS_OFFSET + 1)) {
0124 if (usb->initialized && usb->link_up)
0125 handle_rx_packet(usb, buffer, length);
0126 goto resubmit;
0127 }
0128
0129 status = buffer[PLF_MSG_STATUS_OFFSET];
0130
0131 switch (status) {
0132 case STATION_FIFO_ALMOST_FULL_NOT_MESSAGE:
0133 dev_dbg(&usb->intf->dev,
0134 "FIFO full not packet receipt\n");
0135 tx->mac_fifo_full = 1;
0136 for (sidx = 0; sidx < MAX_STA_NUM; sidx++)
0137 tx->station[sidx].flag |= STATION_FIFO_FULL_FLAG;
0138 break;
0139 case STATION_FIFO_ALMOST_FULL_MESSAGE:
0140 dev_dbg(&usb->intf->dev, "FIFO full packet receipt\n");
0141
0142 for (sidx = 0; sidx < MAX_STA_NUM; sidx++)
0143 tx->station[sidx].flag &= STATION_ACTIVE_FLAG;
0144
0145 plfxlc_send_packet_from_data_queue(usb);
0146 break;
0147 case STATION_CONNECT_MESSAGE:
0148 usb->link_up = 1;
0149 dev_dbg(&usb->intf->dev, "ST_CONNECT_MSG packet receipt\n");
0150 break;
0151 case STATION_DISCONNECT_MESSAGE:
0152 usb->link_up = 0;
0153 dev_dbg(&usb->intf->dev, "ST_DISCONN_MSG packet receipt\n");
0154 break;
0155 default:
0156 dev_dbg(&usb->intf->dev, "Unknown packet receipt\n");
0157 break;
0158 }
0159
0160 resubmit:
0161 r = usb_submit_urb(urb, GFP_ATOMIC);
0162 if (r)
0163 dev_dbg(plfxlc_urb_dev(urb), "urb %p resubmit fail (%d)\n", urb, r);
0164 }
0165
0166 static struct urb *alloc_rx_urb(struct plfxlc_usb *usb)
0167 {
0168 struct usb_device *udev = plfxlc_usb_to_usbdev(usb);
0169 struct urb *urb;
0170 void *buffer;
0171
0172 urb = usb_alloc_urb(0, GFP_KERNEL);
0173 if (!urb)
0174 return NULL;
0175
0176 buffer = usb_alloc_coherent(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
0177 &urb->transfer_dma);
0178 if (!buffer) {
0179 usb_free_urb(urb);
0180 return NULL;
0181 }
0182
0183 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
0184 buffer, USB_MAX_RX_SIZE,
0185 rx_urb_complete, usb);
0186 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0187
0188 return urb;
0189 }
0190
0191 static void free_rx_urb(struct urb *urb)
0192 {
0193 if (!urb)
0194 return;
0195 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
0196 urb->transfer_buffer, urb->transfer_dma);
0197 usb_free_urb(urb);
0198 }
0199
0200 static int __lf_x_usb_enable_rx(struct plfxlc_usb *usb)
0201 {
0202 struct plfxlc_usb_rx *rx = &usb->rx;
0203 struct urb **urbs;
0204 int i, r;
0205
0206 r = -ENOMEM;
0207 urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
0208 if (!urbs)
0209 goto error;
0210
0211 for (i = 0; i < RX_URBS_COUNT; i++) {
0212 urbs[i] = alloc_rx_urb(usb);
0213 if (!urbs[i])
0214 goto error;
0215 }
0216
0217 spin_lock_irq(&rx->lock);
0218
0219 dev_dbg(plfxlc_usb_dev(usb), "irq_disabled %d\n", irqs_disabled());
0220
0221 if (rx->urbs) {
0222 spin_unlock_irq(&rx->lock);
0223 r = 0;
0224 goto error;
0225 }
0226 rx->urbs = urbs;
0227 rx->urbs_count = RX_URBS_COUNT;
0228 spin_unlock_irq(&rx->lock);
0229
0230 for (i = 0; i < RX_URBS_COUNT; i++) {
0231 r = usb_submit_urb(urbs[i], GFP_KERNEL);
0232 if (r)
0233 goto error_submit;
0234 }
0235
0236 return 0;
0237
0238 error_submit:
0239 for (i = 0; i < RX_URBS_COUNT; i++)
0240 usb_kill_urb(urbs[i]);
0241 spin_lock_irq(&rx->lock);
0242 rx->urbs = NULL;
0243 rx->urbs_count = 0;
0244 spin_unlock_irq(&rx->lock);
0245 error:
0246 if (urbs) {
0247 for (i = 0; i < RX_URBS_COUNT; i++)
0248 free_rx_urb(urbs[i]);
0249 }
0250 return r;
0251 }
0252
0253 int plfxlc_usb_enable_rx(struct plfxlc_usb *usb)
0254 {
0255 struct plfxlc_usb_rx *rx = &usb->rx;
0256 int r;
0257
0258 mutex_lock(&rx->setup_mutex);
0259 r = __lf_x_usb_enable_rx(usb);
0260 if (!r)
0261 usb->rx_usb_enabled = 1;
0262
0263 mutex_unlock(&rx->setup_mutex);
0264
0265 return r;
0266 }
0267
0268 static void __lf_x_usb_disable_rx(struct plfxlc_usb *usb)
0269 {
0270 struct plfxlc_usb_rx *rx = &usb->rx;
0271 unsigned long flags;
0272 unsigned int count;
0273 struct urb **urbs;
0274 int i;
0275
0276 spin_lock_irqsave(&rx->lock, flags);
0277 urbs = rx->urbs;
0278 count = rx->urbs_count;
0279 spin_unlock_irqrestore(&rx->lock, flags);
0280
0281 if (!urbs)
0282 return;
0283
0284 for (i = 0; i < count; i++) {
0285 usb_kill_urb(urbs[i]);
0286 free_rx_urb(urbs[i]);
0287 }
0288 kfree(urbs);
0289 rx->urbs = NULL;
0290 rx->urbs_count = 0;
0291 }
0292
0293 void plfxlc_usb_disable_rx(struct plfxlc_usb *usb)
0294 {
0295 struct plfxlc_usb_rx *rx = &usb->rx;
0296
0297 mutex_lock(&rx->setup_mutex);
0298 __lf_x_usb_disable_rx(usb);
0299 usb->rx_usb_enabled = 0;
0300 mutex_unlock(&rx->setup_mutex);
0301 }
0302
0303 void plfxlc_usb_disable_tx(struct plfxlc_usb *usb)
0304 {
0305 struct plfxlc_usb_tx *tx = &usb->tx;
0306 unsigned long flags;
0307
0308 clear_bit(PLF_BIT_ENABLED, &tx->enabled);
0309
0310
0311 usb_kill_anchored_urbs(&tx->submitted);
0312
0313 spin_lock_irqsave(&tx->lock, flags);
0314 WARN_ON(!skb_queue_empty(&tx->submitted_skbs));
0315 WARN_ON(tx->submitted_urbs != 0);
0316 tx->submitted_urbs = 0;
0317 spin_unlock_irqrestore(&tx->lock, flags);
0318
0319
0320
0321
0322 }
0323
0324 void plfxlc_usb_enable_tx(struct plfxlc_usb *usb)
0325 {
0326 struct plfxlc_usb_tx *tx = &usb->tx;
0327 unsigned long flags;
0328
0329 spin_lock_irqsave(&tx->lock, flags);
0330 set_bit(PLF_BIT_ENABLED, &tx->enabled);
0331 tx->submitted_urbs = 0;
0332 ieee80211_wake_queues(plfxlc_usb_to_hw(usb));
0333 tx->stopped = 0;
0334 spin_unlock_irqrestore(&tx->lock, flags);
0335 }
0336
0337 void plfxlc_tx_urb_complete(struct urb *urb)
0338 {
0339 struct ieee80211_tx_info *info;
0340 struct plfxlc_usb *usb;
0341 struct sk_buff *skb;
0342
0343 skb = urb->context;
0344 info = IEEE80211_SKB_CB(skb);
0345
0346
0347
0348 usb = &plfxlc_hw_mac(info->rate_driver_data[0])->chip.usb;
0349
0350 switch (urb->status) {
0351 case 0:
0352 break;
0353 case -ESHUTDOWN:
0354 case -EINVAL:
0355 case -ENODEV:
0356 case -ENOENT:
0357 case -ECONNRESET:
0358 case -EPIPE:
0359 dev_dbg(plfxlc_urb_dev(urb), "urb %p error %d\n", urb, urb->status);
0360 break;
0361 default:
0362 dev_dbg(plfxlc_urb_dev(urb), "urb %p error %d\n", urb, urb->status);
0363 return;
0364 }
0365
0366 plfxlc_mac_tx_to_dev(skb, urb->status);
0367 plfxlc_send_packet_from_data_queue(usb);
0368 usb_free_urb(urb);
0369 }
0370
0371 static inline void init_usb_rx(struct plfxlc_usb *usb)
0372 {
0373 struct plfxlc_usb_rx *rx = &usb->rx;
0374
0375 spin_lock_init(&rx->lock);
0376 mutex_init(&rx->setup_mutex);
0377
0378 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH)
0379 rx->usb_packet_size = 512;
0380 else
0381 rx->usb_packet_size = 64;
0382
0383 if (rx->fragment_length != 0)
0384 dev_dbg(plfxlc_usb_dev(usb), "fragment_length error\n");
0385 }
0386
0387 static inline void init_usb_tx(struct plfxlc_usb *usb)
0388 {
0389 struct plfxlc_usb_tx *tx = &usb->tx;
0390
0391 spin_lock_init(&tx->lock);
0392 clear_bit(PLF_BIT_ENABLED, &tx->enabled);
0393 tx->stopped = 0;
0394 skb_queue_head_init(&tx->submitted_skbs);
0395 init_usb_anchor(&tx->submitted);
0396 }
0397
0398 void plfxlc_usb_init(struct plfxlc_usb *usb, struct ieee80211_hw *hw,
0399 struct usb_interface *intf)
0400 {
0401 memset(usb, 0, sizeof(*usb));
0402 usb->intf = usb_get_intf(intf);
0403 usb_set_intfdata(usb->intf, hw);
0404 init_usb_tx(usb);
0405 init_usb_rx(usb);
0406 }
0407
0408 void plfxlc_usb_release(struct plfxlc_usb *usb)
0409 {
0410 plfxlc_op_stop(plfxlc_usb_to_hw(usb));
0411 plfxlc_usb_disable_tx(usb);
0412 plfxlc_usb_disable_rx(usb);
0413 usb_set_intfdata(usb->intf, NULL);
0414 usb_put_intf(usb->intf);
0415 }
0416
0417 const char *plfxlc_speed(enum usb_device_speed speed)
0418 {
0419 switch (speed) {
0420 case USB_SPEED_LOW:
0421 return "low";
0422 case USB_SPEED_FULL:
0423 return "full";
0424 case USB_SPEED_HIGH:
0425 return "high";
0426 default:
0427 return "unknown";
0428 }
0429 }
0430
0431 int plfxlc_usb_init_hw(struct plfxlc_usb *usb)
0432 {
0433 int r;
0434
0435 r = usb_reset_configuration(plfxlc_usb_to_usbdev(usb));
0436 if (r) {
0437 dev_err(plfxlc_usb_dev(usb), "cfg reset failed (%d)\n", r);
0438 return r;
0439 }
0440 return 0;
0441 }
0442
0443 static void get_usb_req(struct usb_device *udev, void *buffer,
0444 u32 buffer_len, enum plf_usb_req_enum usb_req_id,
0445 struct plf_usb_req *usb_req)
0446 {
0447 __be32 payload_len_nw = cpu_to_be32(buffer_len + FCS_LEN);
0448 const u8 *buffer_src_p = buffer;
0449 u8 *buffer_dst = usb_req->buf;
0450 u32 temp_usb_len = 0;
0451
0452 usb_req->id = cpu_to_be32(usb_req_id);
0453 usb_req->len = cpu_to_be32(0);
0454
0455
0456
0457
0458 if (usb_req->id == cpu_to_be32(USB_REQ_BEACON_WR)) {
0459 memcpy(buffer_dst, &payload_len_nw, sizeof(payload_len_nw));
0460 buffer_dst += sizeof(payload_len_nw);
0461 temp_usb_len += sizeof(payload_len_nw);
0462 }
0463
0464 memcpy(buffer_dst, buffer_src_p, buffer_len);
0465 buffer_dst += buffer_len;
0466 buffer_src_p += buffer_len;
0467 temp_usb_len += buffer_len;
0468
0469
0470 memset(buffer_dst, 0, FCS_LEN);
0471 buffer_dst += FCS_LEN;
0472 temp_usb_len += FCS_LEN;
0473
0474
0475 if (temp_usb_len % PURELIFI_BYTE_NUM_ALIGNMENT) {
0476 memset(buffer_dst, 0, PURELIFI_BYTE_NUM_ALIGNMENT -
0477 (temp_usb_len %
0478 PURELIFI_BYTE_NUM_ALIGNMENT));
0479 buffer_dst += PURELIFI_BYTE_NUM_ALIGNMENT -
0480 (temp_usb_len %
0481 PURELIFI_BYTE_NUM_ALIGNMENT);
0482 temp_usb_len += PURELIFI_BYTE_NUM_ALIGNMENT -
0483 (temp_usb_len % PURELIFI_BYTE_NUM_ALIGNMENT);
0484 }
0485
0486 usb_req->len = cpu_to_be32(temp_usb_len);
0487 }
0488
0489 int plfxlc_usb_wreq_async(struct plfxlc_usb *usb, const u8 *buffer,
0490 int buffer_len, enum plf_usb_req_enum usb_req_id,
0491 usb_complete_t complete_fn,
0492 void *context)
0493 {
0494 struct usb_device *udev = interface_to_usbdev(usb->ez_usb);
0495 struct urb *urb = usb_alloc_urb(0, GFP_ATOMIC);
0496 int r;
0497
0498 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
0499 (void *)buffer, buffer_len, complete_fn, context);
0500
0501 r = usb_submit_urb(urb, GFP_ATOMIC);
0502 if (r)
0503 dev_err(&udev->dev, "Async write submit failed (%d)\n", r);
0504
0505 return r;
0506 }
0507
0508 int plfxlc_usb_wreq(struct usb_interface *ez_usb, void *buffer, int buffer_len,
0509 enum plf_usb_req_enum usb_req_id)
0510 {
0511 struct usb_device *udev = interface_to_usbdev(ez_usb);
0512 unsigned char *dma_buffer = NULL;
0513 struct plf_usb_req usb_req;
0514 int usb_bulk_msg_len;
0515 int actual_length;
0516 int r;
0517
0518 get_usb_req(udev, buffer, buffer_len, usb_req_id, &usb_req);
0519 usb_bulk_msg_len = sizeof(__le32) + sizeof(__le32) +
0520 be32_to_cpu(usb_req.len);
0521
0522 dma_buffer = kmemdup(&usb_req, usb_bulk_msg_len, GFP_KERNEL);
0523
0524 if (!dma_buffer) {
0525 r = -ENOMEM;
0526 goto error;
0527 }
0528
0529 r = usb_bulk_msg(udev,
0530 usb_sndbulkpipe(udev, EP_DATA_OUT),
0531 dma_buffer, usb_bulk_msg_len,
0532 &actual_length, USB_BULK_MSG_TIMEOUT_MS);
0533 kfree(dma_buffer);
0534 error:
0535 if (r) {
0536 r = -ENOMEM;
0537 dev_err(&udev->dev, "usb_bulk_msg failed (%d)\n", r);
0538 }
0539
0540 return r;
0541 }
0542
0543 static void slif_data_plane_sap_timer_callb(struct timer_list *t)
0544 {
0545 struct plfxlc_usb *usb = from_timer(usb, t, tx.tx_retry_timer);
0546
0547 plfxlc_send_packet_from_data_queue(usb);
0548 timer_setup(&usb->tx.tx_retry_timer,
0549 slif_data_plane_sap_timer_callb, 0);
0550 mod_timer(&usb->tx.tx_retry_timer, jiffies + TX_RETRY_BACKOFF_JIFF);
0551 }
0552
0553 static void sta_queue_cleanup_timer_callb(struct timer_list *t)
0554 {
0555 struct plfxlc_usb *usb = from_timer(usb, t, sta_queue_cleanup);
0556 struct plfxlc_usb_tx *tx = &usb->tx;
0557 int sidx;
0558
0559 for (sidx = 0; sidx < MAX_STA_NUM - 1; sidx++) {
0560 if (!(tx->station[sidx].flag & STATION_CONNECTED_FLAG))
0561 continue;
0562 if (tx->station[sidx].flag & STATION_HEARTBEAT_FLAG) {
0563 tx->station[sidx].flag ^= STATION_HEARTBEAT_FLAG;
0564 } else {
0565 eth_zero_addr(tx->station[sidx].mac);
0566 tx->station[sidx].flag = 0;
0567 }
0568 }
0569 timer_setup(&usb->sta_queue_cleanup,
0570 sta_queue_cleanup_timer_callb, 0);
0571 mod_timer(&usb->sta_queue_cleanup, jiffies + STA_QUEUE_CLEANUP_JIFF);
0572 }
0573
0574 static int probe(struct usb_interface *intf,
0575 const struct usb_device_id *id)
0576 {
0577 u8 serial_number[PURELIFI_SERIAL_LEN];
0578 struct ieee80211_hw *hw = NULL;
0579 struct plfxlc_usb_tx *tx;
0580 struct plfxlc_chip *chip;
0581 struct plfxlc_usb *usb;
0582 u8 hw_address[ETH_ALEN];
0583 unsigned int i;
0584 int r = 0;
0585
0586 hw = plfxlc_mac_alloc_hw(intf);
0587
0588 if (!hw) {
0589 r = -ENOMEM;
0590 goto error;
0591 }
0592
0593 chip = &plfxlc_hw_mac(hw)->chip;
0594 usb = &chip->usb;
0595 usb->ez_usb = intf;
0596 tx = &usb->tx;
0597
0598 r = plfxlc_upload_mac_and_serial(intf, hw_address, serial_number);
0599 if (r) {
0600 dev_err(&intf->dev, "MAC and Serial upload failed (%d)\n", r);
0601 goto error;
0602 }
0603
0604 chip->unit_type = STA;
0605 dev_err(&intf->dev, "Unit type is station");
0606
0607 r = plfxlc_mac_preinit_hw(hw, hw_address);
0608 if (r) {
0609 dev_err(&intf->dev, "Init mac failed (%d)\n", r);
0610 goto error;
0611 }
0612
0613 r = ieee80211_register_hw(hw);
0614 if (r) {
0615 dev_err(&intf->dev, "Register device failed (%d)\n", r);
0616 goto error;
0617 }
0618
0619 if ((le16_to_cpu(interface_to_usbdev(intf)->descriptor.idVendor) ==
0620 PURELIFI_XL_VENDOR_ID_0) &&
0621 (le16_to_cpu(interface_to_usbdev(intf)->descriptor.idProduct) ==
0622 PURELIFI_XL_PRODUCT_ID_0)) {
0623 r = plfxlc_download_xl_firmware(intf);
0624 } else {
0625 r = plfxlc_download_fpga(intf);
0626 }
0627 if (r != 0) {
0628 dev_err(&intf->dev, "FPGA download failed (%d)\n", r);
0629 goto error;
0630 }
0631
0632 tx->mac_fifo_full = 0;
0633 spin_lock_init(&tx->lock);
0634
0635 msleep(PLF_MSLEEP_TIME);
0636 r = plfxlc_usb_init_hw(usb);
0637 if (r < 0) {
0638 dev_err(&intf->dev, "usb_init_hw failed (%d)\n", r);
0639 goto error;
0640 }
0641
0642 msleep(PLF_MSLEEP_TIME);
0643 r = plfxlc_chip_switch_radio(chip, PLFXLC_RADIO_ON);
0644 if (r < 0) {
0645 dev_dbg(&intf->dev, "chip_switch_radio_on failed (%d)\n", r);
0646 goto error;
0647 }
0648
0649 msleep(PLF_MSLEEP_TIME);
0650 r = plfxlc_chip_set_rate(chip, 8);
0651 if (r < 0) {
0652 dev_dbg(&intf->dev, "chip_set_rate failed (%d)\n", r);
0653 goto error;
0654 }
0655
0656 msleep(PLF_MSLEEP_TIME);
0657 r = plfxlc_usb_wreq(usb->ez_usb,
0658 hw_address, ETH_ALEN, USB_REQ_MAC_WR);
0659 if (r < 0) {
0660 dev_dbg(&intf->dev, "MAC_WR failure (%d)\n", r);
0661 goto error;
0662 }
0663
0664 plfxlc_chip_enable_rxtx(chip);
0665
0666
0667 for (i = 0; i < MAX_STA_NUM; i++) {
0668 skb_queue_head_init(&tx->station[i].data_list);
0669 tx->station[i].flag = 0;
0670 }
0671
0672 tx->station[STA_BROADCAST_INDEX].flag |= STATION_CONNECTED_FLAG;
0673 for (i = 0; i < ETH_ALEN; i++)
0674 tx->station[STA_BROADCAST_INDEX].mac[i] = 0xFF;
0675
0676 timer_setup(&tx->tx_retry_timer, slif_data_plane_sap_timer_callb, 0);
0677 tx->tx_retry_timer.expires = jiffies + TX_RETRY_BACKOFF_JIFF;
0678 add_timer(&tx->tx_retry_timer);
0679
0680 timer_setup(&usb->sta_queue_cleanup,
0681 sta_queue_cleanup_timer_callb, 0);
0682 usb->sta_queue_cleanup.expires = jiffies + STA_QUEUE_CLEANUP_JIFF;
0683 add_timer(&usb->sta_queue_cleanup);
0684
0685 plfxlc_mac_init_hw(hw);
0686 usb->initialized = true;
0687 return 0;
0688 error:
0689 if (hw) {
0690 plfxlc_mac_release(plfxlc_hw_mac(hw));
0691 ieee80211_unregister_hw(hw);
0692 ieee80211_free_hw(hw);
0693 }
0694 dev_err(&intf->dev, "pureLifi:Device error");
0695 return r;
0696 }
0697
0698 static void disconnect(struct usb_interface *intf)
0699 {
0700 struct ieee80211_hw *hw = plfxlc_intf_to_hw(intf);
0701 struct plfxlc_mac *mac;
0702 struct plfxlc_usb *usb;
0703
0704
0705
0706
0707 if (!hw)
0708 return;
0709
0710 mac = plfxlc_hw_mac(hw);
0711 usb = &mac->chip.usb;
0712
0713 del_timer_sync(&usb->tx.tx_retry_timer);
0714 del_timer_sync(&usb->sta_queue_cleanup);
0715
0716 ieee80211_unregister_hw(hw);
0717
0718 plfxlc_chip_disable_rxtx(&mac->chip);
0719
0720
0721
0722
0723
0724
0725 usb_reset_device(interface_to_usbdev(intf));
0726
0727 plfxlc_mac_release(mac);
0728 ieee80211_free_hw(hw);
0729 }
0730
0731 static void plfxlc_usb_resume(struct plfxlc_usb *usb)
0732 {
0733 struct plfxlc_mac *mac = plfxlc_usb_to_mac(usb);
0734 int r;
0735
0736 r = plfxlc_op_start(plfxlc_usb_to_hw(usb));
0737 if (r < 0) {
0738 dev_warn(plfxlc_usb_dev(usb),
0739 "Device resume failed (%d)\n", r);
0740
0741 if (usb->was_running)
0742 set_bit(PURELIFI_DEVICE_RUNNING, &mac->flags);
0743
0744 usb_queue_reset_device(usb->intf);
0745 return;
0746 }
0747
0748 if (mac->type != NL80211_IFTYPE_UNSPECIFIED) {
0749 r = plfxlc_restore_settings(mac);
0750 if (r < 0) {
0751 dev_dbg(plfxlc_usb_dev(usb),
0752 "Restore failed (%d)\n", r);
0753 return;
0754 }
0755 }
0756 }
0757
0758 static void plfxlc_usb_stop(struct plfxlc_usb *usb)
0759 {
0760 plfxlc_op_stop(plfxlc_usb_to_hw(usb));
0761 plfxlc_usb_disable_tx(usb);
0762 plfxlc_usb_disable_rx(usb);
0763
0764 usb->initialized = false;
0765 }
0766
0767 static int pre_reset(struct usb_interface *intf)
0768 {
0769 struct ieee80211_hw *hw = usb_get_intfdata(intf);
0770 struct plfxlc_mac *mac;
0771 struct plfxlc_usb *usb;
0772
0773 if (!hw || intf->condition != USB_INTERFACE_BOUND)
0774 return 0;
0775
0776 mac = plfxlc_hw_mac(hw);
0777 usb = &mac->chip.usb;
0778
0779 usb->was_running = test_bit(PURELIFI_DEVICE_RUNNING, &mac->flags);
0780
0781 plfxlc_usb_stop(usb);
0782
0783 return 0;
0784 }
0785
0786 static int post_reset(struct usb_interface *intf)
0787 {
0788 struct ieee80211_hw *hw = usb_get_intfdata(intf);
0789 struct plfxlc_mac *mac;
0790 struct plfxlc_usb *usb;
0791
0792 if (!hw || intf->condition != USB_INTERFACE_BOUND)
0793 return 0;
0794
0795 mac = plfxlc_hw_mac(hw);
0796 usb = &mac->chip.usb;
0797
0798 if (usb->was_running)
0799 plfxlc_usb_resume(usb);
0800
0801 return 0;
0802 }
0803
0804 #ifdef CONFIG_PM
0805
0806 static struct plfxlc_usb *get_plfxlc_usb(struct usb_interface *intf)
0807 {
0808 struct ieee80211_hw *hw = plfxlc_intf_to_hw(intf);
0809 struct plfxlc_mac *mac;
0810
0811
0812
0813
0814 if (!hw)
0815 return NULL;
0816
0817 mac = plfxlc_hw_mac(hw);
0818 return &mac->chip.usb;
0819 }
0820
0821 static int suspend(struct usb_interface *interface,
0822 pm_message_t message)
0823 {
0824 struct plfxlc_usb *pl = get_plfxlc_usb(interface);
0825 struct plfxlc_mac *mac = plfxlc_usb_to_mac(pl);
0826
0827 if (!pl)
0828 return -ENODEV;
0829 if (pl->initialized == 0)
0830 return 0;
0831 pl->was_running = test_bit(PURELIFI_DEVICE_RUNNING, &mac->flags);
0832 plfxlc_usb_stop(pl);
0833 return 0;
0834 }
0835
0836 static int resume(struct usb_interface *interface)
0837 {
0838 struct plfxlc_usb *pl = get_plfxlc_usb(interface);
0839
0840 if (!pl)
0841 return -ENODEV;
0842 if (pl->was_running)
0843 plfxlc_usb_resume(pl);
0844 return 0;
0845 }
0846
0847 #endif
0848
0849 static struct usb_driver driver = {
0850 .name = KBUILD_MODNAME,
0851 .id_table = usb_ids,
0852 .probe = probe,
0853 .disconnect = disconnect,
0854 .pre_reset = pre_reset,
0855 .post_reset = post_reset,
0856 #ifdef CONFIG_PM
0857 .suspend = suspend,
0858 .resume = resume,
0859 #endif
0860 .disable_hub_initiated_lpm = 1,
0861 };
0862
0863 static int __init usb_init(void)
0864 {
0865 int r;
0866
0867 r = usb_register(&driver);
0868 if (r) {
0869 pr_err("%s usb_register() failed %d\n", driver.name, r);
0870 return r;
0871 }
0872
0873 pr_debug("Driver initialized :%s\n", driver.name);
0874 return 0;
0875 }
0876
0877 static void __exit usb_exit(void)
0878 {
0879 usb_deregister(&driver);
0880 pr_debug("%s %s\n", driver.name, __func__);
0881 }
0882
0883 MODULE_LICENSE("GPL");
0884 MODULE_DESCRIPTION("USB driver for pureLiFi devices");
0885 MODULE_AUTHOR("pureLiFi");
0886 MODULE_VERSION("1.0");
0887 MODULE_FIRMWARE("plfxlc/lifi-x.bin");
0888 MODULE_DEVICE_TABLE(usb, usb_ids);
0889
0890 module_init(usb_init);
0891 module_exit(usb_exit);