usb.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2021 pureLiFi
  4. */
  5. #ifndef PLFXLC_USB_H
  6. #define PLFXLC_USB_H
  7. #include <linux/completion.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/usb.h>
  12. #include "intf.h"
  13. #define USB_BULK_MSG_TIMEOUT_MS 2000
  14. #define PURELIFI_X_VENDOR_ID_0 0x16C1
  15. #define PURELIFI_X_PRODUCT_ID_0 0x1CDE
  16. #define PURELIFI_XC_VENDOR_ID_0 0x2EF5
  17. #define PURELIFI_XC_PRODUCT_ID_0 0x0008
  18. #define PURELIFI_XL_VENDOR_ID_0 0x2EF5
  19. #define PURELIFI_XL_PRODUCT_ID_0 0x000A /* Station */
  20. #define PLF_FPGA_STATUS_LEN 2
  21. #define PLF_FPGA_STATE_LEN 9
  22. #define PLF_BULK_TLEN 16384
  23. #define PLF_FPGA_MG 6 /* Magic check */
  24. #define PLF_XL_BUF_LEN 64
  25. #define PLF_MSG_STATUS_OFFSET 7
  26. #define PLF_USB_TIMEOUT 1000
  27. #define PLF_MSLEEP_TIME 200
  28. #define PURELIFI_URB_RETRY_MAX 5
  29. #define plfxlc_usb_dev(usb) (&(usb)->intf->dev)
  30. /* Tx retry backoff timer (in milliseconds) */
  31. #define TX_RETRY_BACKOFF_MS 10
  32. #define STA_QUEUE_CLEANUP_MS 5000
  33. /* Tx retry backoff timer (in jiffies) */
  34. #define TX_RETRY_BACKOFF_JIFF ((TX_RETRY_BACKOFF_MS * HZ) / 1000)
  35. #define STA_QUEUE_CLEANUP_JIFF ((STA_QUEUE_CLEANUP_MS * HZ) / 1000)
  36. /* Ensures that MAX_TRANSFER_SIZE is even. */
  37. #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
  38. #define plfxlc_urb_dev(urb) (&(urb)->dev->dev)
  39. #define STATION_FIFO_ALMOST_FULL_MESSAGE 0
  40. #define STATION_FIFO_ALMOST_FULL_NOT_MESSAGE 1
  41. #define STATION_CONNECT_MESSAGE 2
  42. #define STATION_DISCONNECT_MESSAGE 3
  43. int plfxlc_usb_wreq(struct usb_interface *ez_usb, void *buffer, int buffer_len,
  44. enum plf_usb_req_enum usb_req_id);
  45. void plfxlc_tx_urb_complete(struct urb *urb);
  46. enum {
  47. USB_MAX_RX_SIZE = 4800,
  48. USB_MAX_EP_INT_BUFFER = 64,
  49. };
  50. struct plfxlc_usb_interrupt {
  51. spinlock_t lock; /* spin lock for usb interrupt buffer */
  52. struct urb *urb;
  53. void *buffer;
  54. int interval;
  55. };
  56. #define RX_URBS_COUNT 5
  57. struct plfxlc_usb_rx {
  58. spinlock_t lock; /* spin lock for rx urb */
  59. struct mutex setup_mutex; /* mutex lockt for rx urb */
  60. u8 fragment[2 * USB_MAX_RX_SIZE];
  61. unsigned int fragment_length;
  62. unsigned int usb_packet_size;
  63. struct urb **urbs;
  64. int urbs_count;
  65. };
  66. struct plf_station {
  67. /* 7...3 | 2 | 1 | 0 |
  68. * Reserved | Heartbeat | FIFO full | Connected |
  69. */
  70. unsigned char flag;
  71. unsigned char mac[ETH_ALEN];
  72. struct sk_buff_head data_list;
  73. };
  74. struct plfxlc_firmware_file {
  75. u32 total_files;
  76. u32 total_size;
  77. u32 size;
  78. u32 start_addr;
  79. u32 control_packets;
  80. } __packed;
  81. #define STATION_CONNECTED_FLAG 0x1
  82. #define STATION_FIFO_FULL_FLAG 0x2
  83. #define STATION_HEARTBEAT_FLAG 0x4
  84. #define STATION_ACTIVE_FLAG 0xFD
  85. #define PURELIFI_SERIAL_LEN 256
  86. #define STA_BROADCAST_INDEX (AP_USER_LIMIT)
  87. #define MAX_STA_NUM (AP_USER_LIMIT + 1)
  88. struct plfxlc_usb_tx {
  89. unsigned long enabled;
  90. spinlock_t lock; /* spinlock for USB tx */
  91. u8 mac_fifo_full;
  92. struct sk_buff_head submitted_skbs;
  93. struct usb_anchor submitted;
  94. int submitted_urbs;
  95. bool stopped;
  96. struct timer_list tx_retry_timer;
  97. struct plf_station station[MAX_STA_NUM];
  98. };
  99. /* Contains the usb parts. The structure doesn't require a lock because intf
  100. * will not be changed after initialization.
  101. */
  102. struct plfxlc_usb {
  103. struct timer_list sta_queue_cleanup;
  104. struct plfxlc_usb_rx rx;
  105. struct plfxlc_usb_tx tx;
  106. struct usb_interface *intf;
  107. struct usb_interface *ez_usb;
  108. u8 req_buf[64]; /* plfxlc_usb_iowrite16v needs 62 bytes */
  109. u8 sidx; /* store last served */
  110. bool rx_usb_enabled;
  111. bool initialized;
  112. bool was_running;
  113. bool link_up;
  114. };
  115. enum endpoints {
  116. EP_DATA_IN = 2,
  117. EP_DATA_OUT = 8,
  118. };
  119. enum devicetype {
  120. DEVICE_LIFI_X = 0,
  121. DEVICE_LIFI_XC = 1,
  122. DEVICE_LIFI_XL = 1,
  123. };
  124. enum {
  125. PLF_BIT_ENABLED = 1,
  126. PLF_BIT_MAX = 2,
  127. };
  128. int plfxlc_usb_wreq_async(struct plfxlc_usb *usb, const u8 *buffer,
  129. int buffer_len, enum plf_usb_req_enum usb_req_id,
  130. usb_complete_t complete_fn, void *context);
  131. static inline struct usb_device *
  132. plfxlc_usb_to_usbdev(struct plfxlc_usb *usb)
  133. {
  134. return interface_to_usbdev(usb->intf);
  135. }
  136. static inline struct ieee80211_hw *
  137. plfxlc_intf_to_hw(struct usb_interface *intf)
  138. {
  139. return usb_get_intfdata(intf);
  140. }
  141. static inline struct ieee80211_hw *
  142. plfxlc_usb_to_hw(struct plfxlc_usb *usb)
  143. {
  144. return plfxlc_intf_to_hw(usb->intf);
  145. }
  146. void plfxlc_usb_init(struct plfxlc_usb *usb, struct ieee80211_hw *hw,
  147. struct usb_interface *intf);
  148. void plfxlc_send_packet_from_data_queue(struct plfxlc_usb *usb);
  149. void plfxlc_usb_release(struct plfxlc_usb *usb);
  150. void plfxlc_usb_disable_rx(struct plfxlc_usb *usb);
  151. void plfxlc_usb_enable_tx(struct plfxlc_usb *usb);
  152. void plfxlc_usb_disable_tx(struct plfxlc_usb *usb);
  153. int plfxlc_usb_tx(struct plfxlc_usb *usb, struct sk_buff *skb);
  154. int plfxlc_usb_enable_rx(struct plfxlc_usb *usb);
  155. int plfxlc_usb_init_hw(struct plfxlc_usb *usb);
  156. const char *plfxlc_speed(enum usb_device_speed speed);
  157. /* Firmware declarations */
  158. int plfxlc_download_xl_firmware(struct usb_interface *intf);
  159. int plfxlc_download_fpga(struct usb_interface *intf);
  160. int plfxlc_upload_mac_and_serial(struct usb_interface *intf,
  161. unsigned char *hw_address,
  162. unsigned char *serial_number);
  163. #endif /* PLFXLC_USB_H */