rsi_91x_usb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /**
  2. * Copyright (c) 2014 Redpine Signals Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <net/rsi_91x.h>
  20. #include "rsi_usb.h"
  21. #include "rsi_hal.h"
  22. #include "rsi_coex.h"
  23. /* Default operating mode is wlan STA + BT */
  24. static u16 dev_oper_mode = DEV_OPMODE_STA_BT_DUAL;
  25. module_param(dev_oper_mode, ushort, 0444);
  26. MODULE_PARM_DESC(dev_oper_mode,
  27. "1[Wi-Fi], 4[BT], 8[BT LE], 5[Wi-Fi STA + BT classic]\n"
  28. "9[Wi-Fi STA + BT LE], 13[Wi-Fi STA + BT classic + BT LE]\n"
  29. "6[AP + BT classic], 14[AP + BT classic + BT LE]");
  30. static int rsi_rx_urb_submit(struct rsi_hw *adapter, u8 ep_num, gfp_t flags);
  31. /**
  32. * rsi_usb_card_write() - This function writes to the USB Card.
  33. * @adapter: Pointer to the adapter structure.
  34. * @buf: Pointer to the buffer from where the data has to be taken.
  35. * @len: Length to be written.
  36. * @endpoint: Type of endpoint.
  37. *
  38. * Return: status: 0 on success, a negative error code on failure.
  39. */
  40. static int rsi_usb_card_write(struct rsi_hw *adapter,
  41. u8 *buf,
  42. u16 len,
  43. u8 endpoint)
  44. {
  45. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  46. int status;
  47. u8 *seg = dev->tx_buffer;
  48. int transfer;
  49. int ep = dev->bulkout_endpoint_addr[endpoint - 1];
  50. memset(seg, 0, len + RSI_USB_TX_HEAD_ROOM);
  51. memcpy(seg + RSI_USB_TX_HEAD_ROOM, buf, len);
  52. len += RSI_USB_TX_HEAD_ROOM;
  53. transfer = len;
  54. status = usb_bulk_msg(dev->usbdev,
  55. usb_sndbulkpipe(dev->usbdev, ep),
  56. (void *)seg,
  57. (int)len,
  58. &transfer,
  59. HZ * 5);
  60. if (status < 0) {
  61. rsi_dbg(ERR_ZONE,
  62. "Card write failed with error code :%10d\n", status);
  63. dev->write_fail = 1;
  64. }
  65. return status;
  66. }
  67. /**
  68. * rsi_write_multiple() - This function writes multiple bytes of information
  69. * to the USB card.
  70. * @adapter: Pointer to the adapter structure.
  71. * @addr: Address of the register.
  72. * @data: Pointer to the data that has to be written.
  73. * @count: Number of multiple bytes to be written.
  74. *
  75. * Return: 0 on success, a negative error code on failure.
  76. */
  77. static int rsi_write_multiple(struct rsi_hw *adapter,
  78. u8 endpoint,
  79. u8 *data,
  80. u32 count)
  81. {
  82. struct rsi_91x_usbdev *dev;
  83. if (!adapter)
  84. return -ENODEV;
  85. if (endpoint == 0)
  86. return -EINVAL;
  87. dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  88. if (dev->write_fail)
  89. return -ENETDOWN;
  90. return rsi_usb_card_write(adapter, data, count, endpoint);
  91. }
  92. /**
  93. * rsi_find_bulk_in_and_out_endpoints() - This function initializes the bulk
  94. * endpoints to the device.
  95. * @interface: Pointer to the USB interface structure.
  96. * @adapter: Pointer to the adapter structure.
  97. *
  98. * Return: ret_val: 0 on success, -ENOMEM on failure.
  99. */
  100. static int rsi_find_bulk_in_and_out_endpoints(struct usb_interface *interface,
  101. struct rsi_hw *adapter)
  102. {
  103. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  104. struct usb_host_interface *iface_desc;
  105. struct usb_endpoint_descriptor *endpoint;
  106. __le16 buffer_size;
  107. int ii, bin_found = 0, bout_found = 0;
  108. iface_desc = interface->cur_altsetting;
  109. for (ii = 0; ii < iface_desc->desc.bNumEndpoints; ++ii) {
  110. endpoint = &(iface_desc->endpoint[ii].desc);
  111. if (!dev->bulkin_endpoint_addr[bin_found] &&
  112. (endpoint->bEndpointAddress & USB_DIR_IN) &&
  113. ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  114. USB_ENDPOINT_XFER_BULK)) {
  115. buffer_size = endpoint->wMaxPacketSize;
  116. dev->bulkin_size[bin_found] = buffer_size;
  117. dev->bulkin_endpoint_addr[bin_found] =
  118. endpoint->bEndpointAddress;
  119. bin_found++;
  120. }
  121. if (!dev->bulkout_endpoint_addr[bout_found] &&
  122. !(endpoint->bEndpointAddress & USB_DIR_IN) &&
  123. ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  124. USB_ENDPOINT_XFER_BULK)) {
  125. buffer_size = endpoint->wMaxPacketSize;
  126. dev->bulkout_endpoint_addr[bout_found] =
  127. endpoint->bEndpointAddress;
  128. dev->bulkout_size[bout_found] = buffer_size;
  129. bout_found++;
  130. }
  131. if (bin_found >= MAX_BULK_EP || bout_found >= MAX_BULK_EP)
  132. break;
  133. }
  134. if (!(dev->bulkin_endpoint_addr[0]) &&
  135. dev->bulkout_endpoint_addr[0])
  136. return -EINVAL;
  137. return 0;
  138. }
  139. #define RSI_USB_REQ_OUT (USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE)
  140. #define RSI_USB_REQ_IN (USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE)
  141. /* rsi_usb_reg_read() - This function reads data from given register address.
  142. * @usbdev: Pointer to the usb_device structure.
  143. * @reg: Address of the register to be read.
  144. * @value: Value to be read.
  145. * @len: length of data to be read.
  146. *
  147. * Return: status: 0 on success, a negative error code on failure.
  148. */
  149. static int rsi_usb_reg_read(struct usb_device *usbdev,
  150. u32 reg,
  151. u16 *value,
  152. u16 len)
  153. {
  154. u8 *buf;
  155. int status = -ENOMEM;
  156. if (len > RSI_USB_CTRL_BUF_SIZE)
  157. return -EINVAL;
  158. buf = kmalloc(RSI_USB_CTRL_BUF_SIZE, GFP_KERNEL);
  159. if (!buf)
  160. return status;
  161. status = usb_control_msg(usbdev,
  162. usb_rcvctrlpipe(usbdev, 0),
  163. USB_VENDOR_REGISTER_READ,
  164. RSI_USB_REQ_IN,
  165. ((reg & 0xffff0000) >> 16), (reg & 0xffff),
  166. (void *)buf,
  167. len,
  168. USB_CTRL_GET_TIMEOUT);
  169. *value = (buf[0] | (buf[1] << 8));
  170. if (status < 0) {
  171. rsi_dbg(ERR_ZONE,
  172. "%s: Reg read failed with error code :%d\n",
  173. __func__, status);
  174. }
  175. kfree(buf);
  176. return status;
  177. }
  178. /**
  179. * rsi_usb_reg_write() - This function writes the given data into the given
  180. * register address.
  181. * @usbdev: Pointer to the usb_device structure.
  182. * @reg: Address of the register.
  183. * @value: Value to write.
  184. * @len: Length of data to be written.
  185. *
  186. * Return: status: 0 on success, a negative error code on failure.
  187. */
  188. static int rsi_usb_reg_write(struct usb_device *usbdev,
  189. u32 reg,
  190. u32 value,
  191. u16 len)
  192. {
  193. u8 *usb_reg_buf;
  194. int status = -ENOMEM;
  195. if (len > RSI_USB_CTRL_BUF_SIZE)
  196. return -EINVAL;
  197. usb_reg_buf = kmalloc(RSI_USB_CTRL_BUF_SIZE, GFP_KERNEL);
  198. if (!usb_reg_buf)
  199. return status;
  200. usb_reg_buf[0] = (cpu_to_le32(value) & 0x00ff);
  201. usb_reg_buf[1] = (cpu_to_le32(value) & 0xff00) >> 8;
  202. usb_reg_buf[2] = (cpu_to_le32(value) & 0x00ff0000) >> 16;
  203. usb_reg_buf[3] = (cpu_to_le32(value) & 0xff000000) >> 24;
  204. status = usb_control_msg(usbdev,
  205. usb_sndctrlpipe(usbdev, 0),
  206. USB_VENDOR_REGISTER_WRITE,
  207. RSI_USB_REQ_OUT,
  208. ((cpu_to_le32(reg) & 0xffff0000) >> 16),
  209. (cpu_to_le32(reg) & 0xffff),
  210. (void *)usb_reg_buf,
  211. len,
  212. USB_CTRL_SET_TIMEOUT);
  213. if (status < 0) {
  214. rsi_dbg(ERR_ZONE,
  215. "%s: Reg write failed with error code :%d\n",
  216. __func__, status);
  217. }
  218. kfree(usb_reg_buf);
  219. return status;
  220. }
  221. /**
  222. * rsi_rx_done_handler() - This function is called when a packet is received
  223. * from USB stack. This is callback to receive done.
  224. * @urb: Received URB.
  225. *
  226. * Return: None.
  227. */
  228. static void rsi_rx_done_handler(struct urb *urb)
  229. {
  230. struct rx_usb_ctrl_block *rx_cb = urb->context;
  231. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)rx_cb->data;
  232. int status = -EINVAL;
  233. if (urb->status) {
  234. dev_kfree_skb(rx_cb->rx_skb);
  235. return;
  236. }
  237. if (urb->actual_length <= 0 ||
  238. urb->actual_length > rx_cb->rx_skb->len) {
  239. rsi_dbg(INFO_ZONE, "%s: Invalid packet length = %d\n",
  240. __func__, urb->actual_length);
  241. goto out;
  242. }
  243. if (skb_queue_len(&dev->rx_q) >= RSI_MAX_RX_PKTS) {
  244. rsi_dbg(INFO_ZONE, "Max RX packets reached\n");
  245. goto out;
  246. }
  247. skb_trim(rx_cb->rx_skb, urb->actual_length);
  248. skb_queue_tail(&dev->rx_q, rx_cb->rx_skb);
  249. rsi_set_event(&dev->rx_thread.event);
  250. status = 0;
  251. out:
  252. if (rsi_rx_urb_submit(dev->priv, rx_cb->ep_num, GFP_ATOMIC))
  253. rsi_dbg(ERR_ZONE, "%s: Failed in urb submission", __func__);
  254. if (status)
  255. dev_kfree_skb(rx_cb->rx_skb);
  256. }
  257. static void rsi_rx_urb_kill(struct rsi_hw *adapter, u8 ep_num)
  258. {
  259. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  260. struct rx_usb_ctrl_block *rx_cb = &dev->rx_cb[ep_num - 1];
  261. struct urb *urb = rx_cb->rx_urb;
  262. usb_kill_urb(urb);
  263. }
  264. /**
  265. * rsi_rx_urb_submit() - This function submits the given URB to the USB stack.
  266. * @adapter: Pointer to the adapter structure.
  267. *
  268. * Return: 0 on success, a negative error code on failure.
  269. */
  270. static int rsi_rx_urb_submit(struct rsi_hw *adapter, u8 ep_num, gfp_t mem_flags)
  271. {
  272. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  273. struct rx_usb_ctrl_block *rx_cb = &dev->rx_cb[ep_num - 1];
  274. struct urb *urb = rx_cb->rx_urb;
  275. int status;
  276. struct sk_buff *skb;
  277. u8 dword_align_bytes = 0;
  278. #define RSI_MAX_RX_USB_PKT_SIZE 3000
  279. skb = dev_alloc_skb(RSI_MAX_RX_USB_PKT_SIZE);
  280. if (!skb)
  281. return -ENOMEM;
  282. skb_reserve(skb, MAX_DWORD_ALIGN_BYTES);
  283. skb_put(skb, RSI_MAX_RX_USB_PKT_SIZE - MAX_DWORD_ALIGN_BYTES);
  284. dword_align_bytes = (unsigned long)skb->data & 0x3f;
  285. if (dword_align_bytes > 0)
  286. skb_push(skb, dword_align_bytes);
  287. urb->transfer_buffer = skb->data;
  288. rx_cb->rx_skb = skb;
  289. usb_fill_bulk_urb(urb,
  290. dev->usbdev,
  291. usb_rcvbulkpipe(dev->usbdev,
  292. dev->bulkin_endpoint_addr[ep_num - 1]),
  293. urb->transfer_buffer,
  294. skb->len,
  295. rsi_rx_done_handler,
  296. rx_cb);
  297. status = usb_submit_urb(urb, mem_flags);
  298. if (status) {
  299. rsi_dbg(ERR_ZONE, "%s: Failed in urb submission\n", __func__);
  300. dev_kfree_skb(skb);
  301. }
  302. return status;
  303. }
  304. static int rsi_usb_read_register_multiple(struct rsi_hw *adapter, u32 addr,
  305. u8 *data, u16 count)
  306. {
  307. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  308. u8 *buf;
  309. u16 transfer;
  310. int status;
  311. if (!addr)
  312. return -EINVAL;
  313. buf = kzalloc(RSI_USB_BUF_SIZE, GFP_KERNEL);
  314. if (!buf)
  315. return -ENOMEM;
  316. while (count) {
  317. transfer = min_t(u16, count, RSI_USB_BUF_SIZE);
  318. status = usb_control_msg(dev->usbdev,
  319. usb_rcvctrlpipe(dev->usbdev, 0),
  320. USB_VENDOR_REGISTER_READ,
  321. RSI_USB_REQ_IN,
  322. ((addr & 0xffff0000) >> 16),
  323. (addr & 0xffff), (void *)buf,
  324. transfer, USB_CTRL_GET_TIMEOUT);
  325. if (status < 0) {
  326. rsi_dbg(ERR_ZONE,
  327. "Reg read failed with error code :%d\n",
  328. status);
  329. kfree(buf);
  330. return status;
  331. }
  332. memcpy(data, buf, transfer);
  333. count -= transfer;
  334. data += transfer;
  335. addr += transfer;
  336. }
  337. kfree(buf);
  338. return 0;
  339. }
  340. /**
  341. * rsi_usb_write_register_multiple() - This function writes multiple bytes of
  342. * information to multiple registers.
  343. * @adapter: Pointer to the adapter structure.
  344. * @addr: Address of the register.
  345. * @data: Pointer to the data that has to be written.
  346. * @count: Number of multiple bytes to be written on to the registers.
  347. *
  348. * Return: status: 0 on success, a negative error code on failure.
  349. */
  350. static int rsi_usb_write_register_multiple(struct rsi_hw *adapter, u32 addr,
  351. u8 *data, u16 count)
  352. {
  353. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  354. u8 *buf;
  355. u16 transfer;
  356. int status = 0;
  357. buf = kzalloc(RSI_USB_BUF_SIZE, GFP_KERNEL);
  358. if (!buf)
  359. return -ENOMEM;
  360. while (count) {
  361. transfer = min_t(u16, count, RSI_USB_BUF_SIZE);
  362. memcpy(buf, data, transfer);
  363. status = usb_control_msg(dev->usbdev,
  364. usb_sndctrlpipe(dev->usbdev, 0),
  365. USB_VENDOR_REGISTER_WRITE,
  366. RSI_USB_REQ_OUT,
  367. ((addr & 0xffff0000) >> 16),
  368. (addr & 0xffff),
  369. (void *)buf,
  370. transfer,
  371. USB_CTRL_SET_TIMEOUT);
  372. if (status < 0) {
  373. rsi_dbg(ERR_ZONE,
  374. "Reg write failed with error code :%d\n",
  375. status);
  376. kfree(buf);
  377. return status;
  378. }
  379. count -= transfer;
  380. data += transfer;
  381. addr += transfer;
  382. }
  383. kfree(buf);
  384. return 0;
  385. }
  386. /**
  387. *rsi_usb_host_intf_write_pkt() - This function writes the packet to the
  388. * USB card.
  389. * @adapter: Pointer to the adapter structure.
  390. * @pkt: Pointer to the data to be written on to the card.
  391. * @len: Length of the data to be written on to the card.
  392. *
  393. * Return: 0 on success, a negative error code on failure.
  394. */
  395. static int rsi_usb_host_intf_write_pkt(struct rsi_hw *adapter,
  396. u8 *pkt,
  397. u32 len)
  398. {
  399. u32 queueno = ((pkt[1] >> 4) & 0x7);
  400. u8 endpoint;
  401. endpoint = ((queueno == RSI_WIFI_MGMT_Q || queueno == RSI_WIFI_DATA_Q ||
  402. queueno == RSI_COEX_Q) ? WLAN_EP : BT_EP);
  403. return rsi_write_multiple(adapter,
  404. endpoint,
  405. (u8 *)pkt,
  406. len);
  407. }
  408. static int rsi_usb_master_reg_read(struct rsi_hw *adapter, u32 reg,
  409. u32 *value, u16 len)
  410. {
  411. struct usb_device *usbdev =
  412. ((struct rsi_91x_usbdev *)adapter->rsi_dev)->usbdev;
  413. u16 temp;
  414. int ret;
  415. ret = rsi_usb_reg_read(usbdev, reg, &temp, len);
  416. if (ret < 0)
  417. return ret;
  418. *value = temp;
  419. return 0;
  420. }
  421. static int rsi_usb_master_reg_write(struct rsi_hw *adapter,
  422. unsigned long reg,
  423. unsigned long value, u16 len)
  424. {
  425. struct usb_device *usbdev =
  426. ((struct rsi_91x_usbdev *)adapter->rsi_dev)->usbdev;
  427. return rsi_usb_reg_write(usbdev, reg, value, len);
  428. }
  429. static int rsi_usb_load_data_master_write(struct rsi_hw *adapter,
  430. u32 base_address,
  431. u32 instructions_sz, u16 block_size,
  432. u8 *ta_firmware)
  433. {
  434. u16 num_blocks;
  435. u32 cur_indx, i;
  436. u8 temp_buf[256];
  437. int status;
  438. num_blocks = instructions_sz / block_size;
  439. rsi_dbg(INFO_ZONE, "num_blocks: %d\n", num_blocks);
  440. for (cur_indx = 0, i = 0; i < num_blocks; i++, cur_indx += block_size) {
  441. memcpy(temp_buf, ta_firmware + cur_indx, block_size);
  442. status = rsi_usb_write_register_multiple(adapter, base_address,
  443. (u8 *)(temp_buf),
  444. block_size);
  445. if (status < 0)
  446. return status;
  447. rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
  448. base_address += block_size;
  449. }
  450. if (instructions_sz % block_size) {
  451. memset(temp_buf, 0, block_size);
  452. memcpy(temp_buf, ta_firmware + cur_indx,
  453. instructions_sz % block_size);
  454. status = rsi_usb_write_register_multiple
  455. (adapter, base_address,
  456. (u8 *)temp_buf,
  457. instructions_sz % block_size);
  458. if (status < 0)
  459. return status;
  460. rsi_dbg(INFO_ZONE,
  461. "Written Last Block in Address 0x%x Successfully\n",
  462. cur_indx);
  463. }
  464. return 0;
  465. }
  466. static struct rsi_host_intf_ops usb_host_intf_ops = {
  467. .write_pkt = rsi_usb_host_intf_write_pkt,
  468. .read_reg_multiple = rsi_usb_read_register_multiple,
  469. .write_reg_multiple = rsi_usb_write_register_multiple,
  470. .master_reg_read = rsi_usb_master_reg_read,
  471. .master_reg_write = rsi_usb_master_reg_write,
  472. .load_data_master_write = rsi_usb_load_data_master_write,
  473. };
  474. /**
  475. * rsi_deinit_usb_interface() - This function deinitializes the usb interface.
  476. * @adapter: Pointer to the adapter structure.
  477. *
  478. * Return: None.
  479. */
  480. static void rsi_deinit_usb_interface(struct rsi_hw *adapter)
  481. {
  482. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  483. rsi_kill_thread(&dev->rx_thread);
  484. usb_free_urb(dev->rx_cb[0].rx_urb);
  485. if (adapter->priv->coex_mode > 1)
  486. usb_free_urb(dev->rx_cb[1].rx_urb);
  487. kfree(dev->tx_buffer);
  488. }
  489. static int rsi_usb_init_rx(struct rsi_hw *adapter)
  490. {
  491. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  492. struct rx_usb_ctrl_block *rx_cb;
  493. u8 idx, num_rx_cb;
  494. num_rx_cb = (adapter->priv->coex_mode > 1 ? 2 : 1);
  495. for (idx = 0; idx < num_rx_cb; idx++) {
  496. rx_cb = &dev->rx_cb[idx];
  497. rx_cb->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  498. if (!rx_cb->rx_urb) {
  499. rsi_dbg(ERR_ZONE, "Failed alloc rx urb[%d]\n", idx);
  500. goto err;
  501. }
  502. rx_cb->ep_num = idx + 1;
  503. rx_cb->data = (void *)dev;
  504. }
  505. skb_queue_head_init(&dev->rx_q);
  506. rsi_init_event(&dev->rx_thread.event);
  507. if (rsi_create_kthread(adapter->priv, &dev->rx_thread,
  508. rsi_usb_rx_thread, "RX-Thread")) {
  509. rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
  510. goto err;
  511. }
  512. return 0;
  513. err:
  514. usb_free_urb(dev->rx_cb[0].rx_urb);
  515. if (adapter->priv->coex_mode > 1)
  516. usb_free_urb(dev->rx_cb[1].rx_urb);
  517. return -1;
  518. }
  519. /**
  520. * rsi_init_usb_interface() - This function initializes the usb interface.
  521. * @adapter: Pointer to the adapter structure.
  522. * @pfunction: Pointer to USB interface structure.
  523. *
  524. * Return: 0 on success, a negative error code on failure.
  525. */
  526. static int rsi_init_usb_interface(struct rsi_hw *adapter,
  527. struct usb_interface *pfunction)
  528. {
  529. struct rsi_91x_usbdev *rsi_dev;
  530. int status;
  531. rsi_dev = kzalloc(sizeof(*rsi_dev), GFP_KERNEL);
  532. if (!rsi_dev)
  533. return -ENOMEM;
  534. adapter->rsi_dev = rsi_dev;
  535. rsi_dev->usbdev = interface_to_usbdev(pfunction);
  536. rsi_dev->priv = (void *)adapter;
  537. if (rsi_find_bulk_in_and_out_endpoints(pfunction, adapter)) {
  538. status = -EINVAL;
  539. goto fail_eps;
  540. }
  541. adapter->device = &pfunction->dev;
  542. usb_set_intfdata(pfunction, adapter);
  543. rsi_dev->tx_buffer = kmalloc(2048, GFP_KERNEL);
  544. if (!rsi_dev->tx_buffer) {
  545. status = -ENOMEM;
  546. goto fail_eps;
  547. }
  548. if (rsi_usb_init_rx(adapter)) {
  549. rsi_dbg(ERR_ZONE, "Failed to init RX handle\n");
  550. status = -ENOMEM;
  551. goto fail_rx;
  552. }
  553. rsi_dev->tx_blk_size = 252;
  554. adapter->block_size = rsi_dev->tx_blk_size;
  555. /* Initializing function callbacks */
  556. adapter->check_hw_queue_status = rsi_usb_check_queue_status;
  557. adapter->determine_event_timeout = rsi_usb_event_timeout;
  558. adapter->rsi_host_intf = RSI_HOST_INTF_USB;
  559. adapter->host_intf_ops = &usb_host_intf_ops;
  560. #ifdef CONFIG_RSI_DEBUGFS
  561. /* In USB, one less than the MAX_DEBUGFS_ENTRIES entries is required */
  562. adapter->num_debugfs_entries = (MAX_DEBUGFS_ENTRIES - 1);
  563. #endif
  564. rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
  565. return 0;
  566. fail_rx:
  567. kfree(rsi_dev->tx_buffer);
  568. fail_eps:
  569. return status;
  570. }
  571. static int usb_ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
  572. u16 len_in_bits)
  573. {
  574. int ret;
  575. ret = rsi_usb_master_reg_write
  576. (adapter, RSI_GSPI_DATA_REG1,
  577. ((addr << 6) | ((data >> 16) & 0xffff)), 2);
  578. if (ret < 0)
  579. return ret;
  580. ret = rsi_usb_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
  581. (data & 0xffff), 2);
  582. if (ret < 0)
  583. return ret;
  584. /* Initializing GSPI for ULP read/writes */
  585. rsi_usb_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
  586. RSI_GSPI_CTRL_REG0_VALUE, 2);
  587. ret = rsi_usb_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
  588. ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
  589. if (ret < 0)
  590. return ret;
  591. msleep(20);
  592. return 0;
  593. }
  594. static int rsi_reset_card(struct rsi_hw *adapter)
  595. {
  596. int ret;
  597. rsi_dbg(INFO_ZONE, "Resetting Card...\n");
  598. rsi_usb_master_reg_write(adapter, RSI_TA_HOLD_REG, 0xE, 4);
  599. /* This msleep will ensure Thread-Arch processor to go to hold
  600. * and any pending dma transfers to rf in device to finish.
  601. */
  602. msleep(100);
  603. ret = rsi_usb_master_reg_write(adapter, SWBL_REGOUT,
  604. RSI_FW_WDT_DISABLE_REQ,
  605. RSI_COMMON_REG_SIZE);
  606. if (ret < 0) {
  607. rsi_dbg(ERR_ZONE, "Disabling firmware watchdog timer failed\n");
  608. goto fail;
  609. }
  610. if (adapter->device_model != RSI_DEV_9116) {
  611. ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_1,
  612. RSI_ULP_WRITE_2, 32);
  613. if (ret < 0)
  614. goto fail;
  615. ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2,
  616. RSI_ULP_WRITE_0, 32);
  617. if (ret < 0)
  618. goto fail;
  619. ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1,
  620. RSI_ULP_WRITE_50, 32);
  621. if (ret < 0)
  622. goto fail;
  623. ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2,
  624. RSI_ULP_WRITE_0, 32);
  625. if (ret < 0)
  626. goto fail;
  627. ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
  628. RSI_ULP_TIMER_ENABLE, 32);
  629. if (ret < 0)
  630. goto fail;
  631. } else {
  632. ret = rsi_usb_master_reg_write(adapter,
  633. NWP_WWD_INTERRUPT_TIMER,
  634. NWP_WWD_INT_TIMER_CLKS,
  635. RSI_9116_REG_SIZE);
  636. if (ret < 0)
  637. goto fail;
  638. ret = rsi_usb_master_reg_write(adapter,
  639. NWP_WWD_SYSTEM_RESET_TIMER,
  640. NWP_WWD_SYS_RESET_TIMER_CLKS,
  641. RSI_9116_REG_SIZE);
  642. if (ret < 0)
  643. goto fail;
  644. ret = rsi_usb_master_reg_write(adapter,
  645. NWP_WWD_MODE_AND_RSTART,
  646. NWP_WWD_TIMER_DISABLE,
  647. RSI_9116_REG_SIZE);
  648. if (ret < 0)
  649. goto fail;
  650. }
  651. rsi_dbg(INFO_ZONE, "Reset card done\n");
  652. return ret;
  653. fail:
  654. rsi_dbg(ERR_ZONE, "Reset card failed\n");
  655. return ret;
  656. }
  657. /**
  658. * rsi_probe() - This function is called by kernel when the driver provided
  659. * Vendor and device IDs are matched. All the initialization
  660. * work is done here.
  661. * @pfunction: Pointer to the USB interface structure.
  662. * @id: Pointer to the usb_device_id structure.
  663. *
  664. * Return: 0 on success, a negative error code on failure.
  665. */
  666. static int rsi_probe(struct usb_interface *pfunction,
  667. const struct usb_device_id *id)
  668. {
  669. struct rsi_hw *adapter;
  670. struct rsi_91x_usbdev *dev;
  671. u16 fw_status;
  672. int status;
  673. rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
  674. adapter = rsi_91x_init(dev_oper_mode);
  675. if (!adapter) {
  676. rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
  677. __func__);
  678. return -ENOMEM;
  679. }
  680. adapter->rsi_host_intf = RSI_HOST_INTF_USB;
  681. status = rsi_init_usb_interface(adapter, pfunction);
  682. if (status) {
  683. rsi_dbg(ERR_ZONE, "%s: Failed to init usb interface\n",
  684. __func__);
  685. goto err;
  686. }
  687. rsi_dbg(ERR_ZONE, "%s: Initialized os intf ops\n", __func__);
  688. if (id && id->idProduct == RSI_USB_PID_9113) {
  689. rsi_dbg(INIT_ZONE, "%s: 9113 module detected\n", __func__);
  690. adapter->device_model = RSI_DEV_9113;
  691. } else if (id && id->idProduct == RSI_USB_PID_9116) {
  692. rsi_dbg(INIT_ZONE, "%s: 9116 module detected\n", __func__);
  693. adapter->device_model = RSI_DEV_9116;
  694. } else {
  695. rsi_dbg(ERR_ZONE, "%s: Unsupported RSI device id 0x%x\n",
  696. __func__, id->idProduct);
  697. status = -ENODEV;
  698. goto err1;
  699. }
  700. dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  701. status = rsi_usb_reg_read(dev->usbdev, FW_STATUS_REG, &fw_status, 2);
  702. if (status < 0)
  703. goto err1;
  704. else
  705. fw_status &= 1;
  706. if (!fw_status) {
  707. rsi_dbg(INIT_ZONE, "Loading firmware...\n");
  708. status = rsi_hal_device_init(adapter);
  709. if (status) {
  710. rsi_dbg(ERR_ZONE, "%s: Failed in device init\n",
  711. __func__);
  712. goto err1;
  713. }
  714. rsi_dbg(INIT_ZONE, "%s: Device Init Done\n", __func__);
  715. }
  716. status = rsi_rx_urb_submit(adapter, WLAN_EP, GFP_KERNEL);
  717. if (status)
  718. goto err1;
  719. if (adapter->priv->coex_mode > 1) {
  720. status = rsi_rx_urb_submit(adapter, BT_EP, GFP_KERNEL);
  721. if (status)
  722. goto err_kill_wlan_urb;
  723. }
  724. return 0;
  725. err_kill_wlan_urb:
  726. rsi_rx_urb_kill(adapter, WLAN_EP);
  727. err1:
  728. rsi_deinit_usb_interface(adapter);
  729. err:
  730. rsi_91x_deinit(adapter);
  731. rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
  732. return status;
  733. }
  734. /**
  735. * rsi_disconnect() - This function performs the reverse of the probe function,
  736. * it deinitialize the driver structure.
  737. * @pfunction: Pointer to the USB interface structure.
  738. *
  739. * Return: None.
  740. */
  741. static void rsi_disconnect(struct usb_interface *pfunction)
  742. {
  743. struct rsi_hw *adapter = usb_get_intfdata(pfunction);
  744. if (!adapter)
  745. return;
  746. rsi_mac80211_detach(adapter);
  747. if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
  748. adapter->priv->bt_adapter) {
  749. rsi_bt_ops.detach(adapter->priv->bt_adapter);
  750. adapter->priv->bt_adapter = NULL;
  751. }
  752. if (adapter->priv->coex_mode > 1)
  753. rsi_rx_urb_kill(adapter, BT_EP);
  754. rsi_rx_urb_kill(adapter, WLAN_EP);
  755. rsi_reset_card(adapter);
  756. rsi_deinit_usb_interface(adapter);
  757. rsi_91x_deinit(adapter);
  758. rsi_dbg(INFO_ZONE, "%s: Deinitialization completed\n", __func__);
  759. }
  760. #ifdef CONFIG_PM
  761. static int rsi_suspend(struct usb_interface *intf, pm_message_t message)
  762. {
  763. /* Not yet implemented */
  764. return -ENOSYS;
  765. }
  766. static int rsi_resume(struct usb_interface *intf)
  767. {
  768. /* Not yet implemented */
  769. return -ENOSYS;
  770. }
  771. #endif
  772. static const struct usb_device_id rsi_dev_table[] = {
  773. { USB_DEVICE(RSI_USB_VENDOR_ID, RSI_USB_PID_9113) },
  774. { USB_DEVICE(RSI_USB_VENDOR_ID, RSI_USB_PID_9116) },
  775. { /* Blank */},
  776. };
  777. static struct usb_driver rsi_driver = {
  778. .name = "RSI-USB WLAN",
  779. .probe = rsi_probe,
  780. .disconnect = rsi_disconnect,
  781. .id_table = rsi_dev_table,
  782. #ifdef CONFIG_PM
  783. .suspend = rsi_suspend,
  784. .resume = rsi_resume,
  785. #endif
  786. };
  787. module_usb_driver(rsi_driver);
  788. MODULE_AUTHOR("Redpine Signals Inc");
  789. MODULE_DESCRIPTION("Common USB layer for RSI drivers");
  790. MODULE_SUPPORTED_DEVICE("RSI-91x");
  791. MODULE_DEVICE_TABLE(usb, rsi_dev_table);
  792. MODULE_FIRMWARE(FIRMWARE_RSI9113);
  793. MODULE_VERSION("0.1");
  794. MODULE_LICENSE("Dual BSD/GPL");