rsi_91x_usb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. u16 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] = (value & 0x00ff);
  201. usb_reg_buf[1] = (value & 0xff00) >> 8;
  202. usb_reg_buf[2] = 0x0;
  203. usb_reg_buf[3] = 0x0;
  204. status = usb_control_msg(usbdev,
  205. usb_sndctrlpipe(usbdev, 0),
  206. USB_VENDOR_REGISTER_WRITE,
  207. RSI_USB_REQ_OUT,
  208. ((reg & 0xffff0000) >> 16),
  209. (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 recieve 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. goto out;
  235. if (urb->actual_length <= 0 ||
  236. urb->actual_length > rx_cb->rx_skb->len) {
  237. rsi_dbg(INFO_ZONE, "%s: Invalid packet length = %d\n",
  238. __func__, urb->actual_length);
  239. goto out;
  240. }
  241. if (skb_queue_len(&dev->rx_q) >= RSI_MAX_RX_PKTS) {
  242. rsi_dbg(INFO_ZONE, "Max RX packets reached\n");
  243. goto out;
  244. }
  245. skb_trim(rx_cb->rx_skb, urb->actual_length);
  246. skb_queue_tail(&dev->rx_q, rx_cb->rx_skb);
  247. rsi_set_event(&dev->rx_thread.event);
  248. status = 0;
  249. out:
  250. if (rsi_rx_urb_submit(dev->priv, rx_cb->ep_num, GFP_ATOMIC))
  251. rsi_dbg(ERR_ZONE, "%s: Failed in urb submission", __func__);
  252. if (status)
  253. dev_kfree_skb(rx_cb->rx_skb);
  254. }
  255. static void rsi_rx_urb_kill(struct rsi_hw *adapter, u8 ep_num)
  256. {
  257. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  258. struct rx_usb_ctrl_block *rx_cb = &dev->rx_cb[ep_num - 1];
  259. struct urb *urb = rx_cb->rx_urb;
  260. usb_kill_urb(urb);
  261. }
  262. /**
  263. * rsi_rx_urb_submit() - This function submits the given URB to the USB stack.
  264. * @adapter: Pointer to the adapter structure.
  265. *
  266. * Return: 0 on success, a negative error code on failure.
  267. */
  268. static int rsi_rx_urb_submit(struct rsi_hw *adapter, u8 ep_num, gfp_t mem_flags)
  269. {
  270. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  271. struct rx_usb_ctrl_block *rx_cb = &dev->rx_cb[ep_num - 1];
  272. struct urb *urb = rx_cb->rx_urb;
  273. int status;
  274. struct sk_buff *skb;
  275. u8 dword_align_bytes = 0;
  276. #define RSI_MAX_RX_USB_PKT_SIZE 3000
  277. skb = dev_alloc_skb(RSI_MAX_RX_USB_PKT_SIZE);
  278. if (!skb)
  279. return -ENOMEM;
  280. skb_reserve(skb, MAX_DWORD_ALIGN_BYTES);
  281. skb_put(skb, RSI_MAX_RX_USB_PKT_SIZE - MAX_DWORD_ALIGN_BYTES);
  282. dword_align_bytes = (unsigned long)skb->data & 0x3f;
  283. if (dword_align_bytes > 0)
  284. skb_push(skb, dword_align_bytes);
  285. urb->transfer_buffer = skb->data;
  286. rx_cb->rx_skb = skb;
  287. usb_fill_bulk_urb(urb,
  288. dev->usbdev,
  289. usb_rcvbulkpipe(dev->usbdev,
  290. dev->bulkin_endpoint_addr[ep_num - 1]),
  291. urb->transfer_buffer,
  292. skb->len,
  293. rsi_rx_done_handler,
  294. rx_cb);
  295. status = usb_submit_urb(urb, mem_flags);
  296. if (status) {
  297. rsi_dbg(ERR_ZONE, "%s: Failed in urb submission\n", __func__);
  298. dev_kfree_skb(skb);
  299. }
  300. return status;
  301. }
  302. static int rsi_usb_read_register_multiple(struct rsi_hw *adapter, u32 addr,
  303. u8 *data, u16 count)
  304. {
  305. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  306. u8 *buf;
  307. u16 transfer;
  308. int status;
  309. if (!addr)
  310. return -EINVAL;
  311. buf = kzalloc(RSI_USB_BUF_SIZE, GFP_KERNEL);
  312. if (!buf)
  313. return -ENOMEM;
  314. while (count) {
  315. transfer = min_t(u16, count, RSI_USB_BUF_SIZE);
  316. status = usb_control_msg(dev->usbdev,
  317. usb_rcvctrlpipe(dev->usbdev, 0),
  318. USB_VENDOR_REGISTER_READ,
  319. RSI_USB_REQ_IN,
  320. ((addr & 0xffff0000) >> 16),
  321. (addr & 0xffff), (void *)buf,
  322. transfer, USB_CTRL_GET_TIMEOUT);
  323. if (status < 0) {
  324. rsi_dbg(ERR_ZONE,
  325. "Reg read failed with error code :%d\n",
  326. status);
  327. kfree(buf);
  328. return status;
  329. }
  330. memcpy(data, buf, transfer);
  331. count -= transfer;
  332. data += transfer;
  333. addr += transfer;
  334. }
  335. kfree(buf);
  336. return 0;
  337. }
  338. /**
  339. * rsi_usb_write_register_multiple() - This function writes multiple bytes of
  340. * information to multiple registers.
  341. * @adapter: Pointer to the adapter structure.
  342. * @addr: Address of the register.
  343. * @data: Pointer to the data that has to be written.
  344. * @count: Number of multiple bytes to be written on to the registers.
  345. *
  346. * Return: status: 0 on success, a negative error code on failure.
  347. */
  348. static int rsi_usb_write_register_multiple(struct rsi_hw *adapter, u32 addr,
  349. u8 *data, u16 count)
  350. {
  351. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  352. u8 *buf;
  353. u16 transfer;
  354. int status = 0;
  355. buf = kzalloc(RSI_USB_BUF_SIZE, GFP_KERNEL);
  356. if (!buf)
  357. return -ENOMEM;
  358. while (count) {
  359. transfer = min_t(u16, count, RSI_USB_BUF_SIZE);
  360. memcpy(buf, data, transfer);
  361. status = usb_control_msg(dev->usbdev,
  362. usb_sndctrlpipe(dev->usbdev, 0),
  363. USB_VENDOR_REGISTER_WRITE,
  364. RSI_USB_REQ_OUT,
  365. ((addr & 0xffff0000) >> 16),
  366. (addr & 0xffff),
  367. (void *)buf,
  368. transfer,
  369. USB_CTRL_SET_TIMEOUT);
  370. if (status < 0) {
  371. rsi_dbg(ERR_ZONE,
  372. "Reg write failed with error code :%d\n",
  373. status);
  374. kfree(buf);
  375. return status;
  376. }
  377. count -= transfer;
  378. data += transfer;
  379. addr += transfer;
  380. }
  381. kfree(buf);
  382. return 0;
  383. }
  384. /**
  385. *rsi_usb_host_intf_write_pkt() - This function writes the packet to the
  386. * USB card.
  387. * @adapter: Pointer to the adapter structure.
  388. * @pkt: Pointer to the data to be written on to the card.
  389. * @len: Length of the data to be written on to the card.
  390. *
  391. * Return: 0 on success, a negative error code on failure.
  392. */
  393. static int rsi_usb_host_intf_write_pkt(struct rsi_hw *adapter,
  394. u8 *pkt,
  395. u32 len)
  396. {
  397. u32 queueno = ((pkt[1] >> 4) & 0x7);
  398. u8 endpoint;
  399. endpoint = ((queueno == RSI_WIFI_MGMT_Q || queueno == RSI_WIFI_DATA_Q ||
  400. queueno == RSI_COEX_Q) ? WLAN_EP : BT_EP);
  401. return rsi_write_multiple(adapter,
  402. endpoint,
  403. (u8 *)pkt,
  404. len);
  405. }
  406. static int rsi_usb_master_reg_read(struct rsi_hw *adapter, u32 reg,
  407. u32 *value, u16 len)
  408. {
  409. struct usb_device *usbdev =
  410. ((struct rsi_91x_usbdev *)adapter->rsi_dev)->usbdev;
  411. u16 temp;
  412. int ret;
  413. ret = rsi_usb_reg_read(usbdev, reg, &temp, len);
  414. if (ret < 0)
  415. return ret;
  416. *value = temp;
  417. return 0;
  418. }
  419. static int rsi_usb_master_reg_write(struct rsi_hw *adapter,
  420. unsigned long reg,
  421. unsigned long value, u16 len)
  422. {
  423. struct usb_device *usbdev =
  424. ((struct rsi_91x_usbdev *)adapter->rsi_dev)->usbdev;
  425. return rsi_usb_reg_write(usbdev, reg, value, len);
  426. }
  427. static int rsi_usb_load_data_master_write(struct rsi_hw *adapter,
  428. u32 base_address,
  429. u32 instructions_sz, u16 block_size,
  430. u8 *ta_firmware)
  431. {
  432. u16 num_blocks;
  433. u32 cur_indx, i;
  434. u8 temp_buf[256];
  435. int status;
  436. num_blocks = instructions_sz / block_size;
  437. rsi_dbg(INFO_ZONE, "num_blocks: %d\n", num_blocks);
  438. for (cur_indx = 0, i = 0; i < num_blocks; i++, cur_indx += block_size) {
  439. memcpy(temp_buf, ta_firmware + cur_indx, block_size);
  440. status = rsi_usb_write_register_multiple(adapter, base_address,
  441. (u8 *)(temp_buf),
  442. block_size);
  443. if (status < 0)
  444. return status;
  445. rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
  446. base_address += block_size;
  447. }
  448. if (instructions_sz % block_size) {
  449. memset(temp_buf, 0, block_size);
  450. memcpy(temp_buf, ta_firmware + cur_indx,
  451. instructions_sz % block_size);
  452. status = rsi_usb_write_register_multiple
  453. (adapter, base_address,
  454. (u8 *)temp_buf,
  455. instructions_sz % block_size);
  456. if (status < 0)
  457. return status;
  458. rsi_dbg(INFO_ZONE,
  459. "Written Last Block in Address 0x%x Successfully\n",
  460. cur_indx);
  461. }
  462. return 0;
  463. }
  464. static struct rsi_host_intf_ops usb_host_intf_ops = {
  465. .write_pkt = rsi_usb_host_intf_write_pkt,
  466. .read_reg_multiple = rsi_usb_read_register_multiple,
  467. .write_reg_multiple = rsi_usb_write_register_multiple,
  468. .master_reg_read = rsi_usb_master_reg_read,
  469. .master_reg_write = rsi_usb_master_reg_write,
  470. .load_data_master_write = rsi_usb_load_data_master_write,
  471. };
  472. /**
  473. * rsi_deinit_usb_interface() - This function deinitializes the usb interface.
  474. * @adapter: Pointer to the adapter structure.
  475. *
  476. * Return: None.
  477. */
  478. static void rsi_deinit_usb_interface(struct rsi_hw *adapter)
  479. {
  480. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  481. rsi_kill_thread(&dev->rx_thread);
  482. usb_free_urb(dev->rx_cb[0].rx_urb);
  483. if (adapter->priv->coex_mode > 1)
  484. usb_free_urb(dev->rx_cb[1].rx_urb);
  485. kfree(dev->tx_buffer);
  486. }
  487. static int rsi_usb_init_rx(struct rsi_hw *adapter)
  488. {
  489. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  490. struct rx_usb_ctrl_block *rx_cb;
  491. u8 idx, num_rx_cb;
  492. num_rx_cb = (adapter->priv->coex_mode > 1 ? 2 : 1);
  493. for (idx = 0; idx < num_rx_cb; idx++) {
  494. rx_cb = &dev->rx_cb[idx];
  495. rx_cb->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  496. if (!rx_cb->rx_urb) {
  497. rsi_dbg(ERR_ZONE, "Failed alloc rx urb[%d]\n", idx);
  498. goto err;
  499. }
  500. rx_cb->ep_num = idx + 1;
  501. rx_cb->data = (void *)dev;
  502. }
  503. skb_queue_head_init(&dev->rx_q);
  504. rsi_init_event(&dev->rx_thread.event);
  505. if (rsi_create_kthread(adapter->priv, &dev->rx_thread,
  506. rsi_usb_rx_thread, "RX-Thread")) {
  507. rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
  508. goto err;
  509. }
  510. return 0;
  511. err:
  512. usb_free_urb(dev->rx_cb[0].rx_urb);
  513. if (adapter->priv->coex_mode > 1)
  514. usb_free_urb(dev->rx_cb[1].rx_urb);
  515. return -1;
  516. }
  517. /**
  518. * rsi_init_usb_interface() - This function initializes the usb interface.
  519. * @adapter: Pointer to the adapter structure.
  520. * @pfunction: Pointer to USB interface structure.
  521. *
  522. * Return: 0 on success, a negative error code on failure.
  523. */
  524. static int rsi_init_usb_interface(struct rsi_hw *adapter,
  525. struct usb_interface *pfunction)
  526. {
  527. struct rsi_91x_usbdev *rsi_dev;
  528. int status;
  529. rsi_dev = kzalloc(sizeof(*rsi_dev), GFP_KERNEL);
  530. if (!rsi_dev)
  531. return -ENOMEM;
  532. adapter->rsi_dev = rsi_dev;
  533. rsi_dev->usbdev = interface_to_usbdev(pfunction);
  534. rsi_dev->priv = (void *)adapter;
  535. if (rsi_find_bulk_in_and_out_endpoints(pfunction, adapter)) {
  536. status = -EINVAL;
  537. goto fail_eps;
  538. }
  539. adapter->device = &pfunction->dev;
  540. usb_set_intfdata(pfunction, adapter);
  541. rsi_dev->tx_buffer = kmalloc(2048, GFP_KERNEL);
  542. if (!rsi_dev->tx_buffer) {
  543. status = -ENOMEM;
  544. goto fail_eps;
  545. }
  546. if (rsi_usb_init_rx(adapter)) {
  547. rsi_dbg(ERR_ZONE, "Failed to init RX handle\n");
  548. status = -ENOMEM;
  549. goto fail_rx;
  550. }
  551. rsi_dev->tx_blk_size = 252;
  552. adapter->block_size = rsi_dev->tx_blk_size;
  553. /* Initializing function callbacks */
  554. adapter->check_hw_queue_status = rsi_usb_check_queue_status;
  555. adapter->determine_event_timeout = rsi_usb_event_timeout;
  556. adapter->rsi_host_intf = RSI_HOST_INTF_USB;
  557. adapter->host_intf_ops = &usb_host_intf_ops;
  558. #ifdef CONFIG_RSI_DEBUGFS
  559. /* In USB, one less than the MAX_DEBUGFS_ENTRIES entries is required */
  560. adapter->num_debugfs_entries = (MAX_DEBUGFS_ENTRIES - 1);
  561. #endif
  562. rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
  563. return 0;
  564. fail_rx:
  565. kfree(rsi_dev->tx_buffer);
  566. fail_eps:
  567. return status;
  568. }
  569. static int usb_ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
  570. u16 len_in_bits)
  571. {
  572. int ret;
  573. ret = rsi_usb_master_reg_write
  574. (adapter, RSI_GSPI_DATA_REG1,
  575. ((addr << 6) | ((data >> 16) & 0xffff)), 2);
  576. if (ret < 0)
  577. return ret;
  578. ret = rsi_usb_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
  579. (data & 0xffff), 2);
  580. if (ret < 0)
  581. return ret;
  582. /* Initializing GSPI for ULP read/writes */
  583. rsi_usb_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
  584. RSI_GSPI_CTRL_REG0_VALUE, 2);
  585. ret = rsi_usb_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
  586. ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
  587. if (ret < 0)
  588. return ret;
  589. msleep(20);
  590. return 0;
  591. }
  592. static int rsi_reset_card(struct rsi_hw *adapter)
  593. {
  594. int ret;
  595. rsi_dbg(INFO_ZONE, "Resetting Card...\n");
  596. rsi_usb_master_reg_write(adapter, RSI_TA_HOLD_REG, 0xE, 4);
  597. /* This msleep will ensure Thread-Arch processor to go to hold
  598. * and any pending dma transfers to rf in device to finish.
  599. */
  600. msleep(100);
  601. ret = rsi_usb_master_reg_write(adapter, SWBL_REGOUT,
  602. RSI_FW_WDT_DISABLE_REQ,
  603. RSI_COMMON_REG_SIZE);
  604. if (ret < 0) {
  605. rsi_dbg(ERR_ZONE, "Disabling firmware watchdog timer failed\n");
  606. goto fail;
  607. }
  608. ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_1,
  609. RSI_ULP_WRITE_2, 32);
  610. if (ret < 0)
  611. goto fail;
  612. ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2,
  613. RSI_ULP_WRITE_0, 32);
  614. if (ret < 0)
  615. goto fail;
  616. ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1,
  617. RSI_ULP_WRITE_50, 32);
  618. if (ret < 0)
  619. goto fail;
  620. ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2,
  621. RSI_ULP_WRITE_0, 32);
  622. if (ret < 0)
  623. goto fail;
  624. ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
  625. RSI_ULP_TIMER_ENABLE, 32);
  626. if (ret < 0)
  627. goto fail;
  628. rsi_dbg(INFO_ZONE, "Reset card done\n");
  629. return ret;
  630. fail:
  631. rsi_dbg(ERR_ZONE, "Reset card failed\n");
  632. return ret;
  633. }
  634. /**
  635. * rsi_probe() - This function is called by kernel when the driver provided
  636. * Vendor and device IDs are matched. All the initialization
  637. * work is done here.
  638. * @pfunction: Pointer to the USB interface structure.
  639. * @id: Pointer to the usb_device_id structure.
  640. *
  641. * Return: 0 on success, a negative error code on failure.
  642. */
  643. static int rsi_probe(struct usb_interface *pfunction,
  644. const struct usb_device_id *id)
  645. {
  646. struct rsi_hw *adapter;
  647. struct rsi_91x_usbdev *dev;
  648. u16 fw_status;
  649. int status;
  650. rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
  651. adapter = rsi_91x_init(dev_oper_mode);
  652. if (!adapter) {
  653. rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
  654. __func__);
  655. return -ENOMEM;
  656. }
  657. adapter->rsi_host_intf = RSI_HOST_INTF_USB;
  658. status = rsi_init_usb_interface(adapter, pfunction);
  659. if (status) {
  660. rsi_dbg(ERR_ZONE, "%s: Failed to init usb interface\n",
  661. __func__);
  662. goto err;
  663. }
  664. rsi_dbg(ERR_ZONE, "%s: Initialized os intf ops\n", __func__);
  665. dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  666. status = rsi_usb_reg_read(dev->usbdev, FW_STATUS_REG, &fw_status, 2);
  667. if (status < 0)
  668. goto err1;
  669. else
  670. fw_status &= 1;
  671. if (!fw_status) {
  672. rsi_dbg(INIT_ZONE, "Loading firmware...\n");
  673. status = rsi_hal_device_init(adapter);
  674. if (status) {
  675. rsi_dbg(ERR_ZONE, "%s: Failed in device init\n",
  676. __func__);
  677. goto err1;
  678. }
  679. rsi_dbg(INIT_ZONE, "%s: Device Init Done\n", __func__);
  680. }
  681. status = rsi_rx_urb_submit(adapter, WLAN_EP, GFP_KERNEL);
  682. if (status)
  683. goto err1;
  684. if (adapter->priv->coex_mode > 1) {
  685. status = rsi_rx_urb_submit(adapter, BT_EP, GFP_KERNEL);
  686. if (status)
  687. goto err_kill_wlan_urb;
  688. }
  689. return 0;
  690. err_kill_wlan_urb:
  691. rsi_rx_urb_kill(adapter, WLAN_EP);
  692. err1:
  693. rsi_deinit_usb_interface(adapter);
  694. err:
  695. rsi_91x_deinit(adapter);
  696. rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
  697. return status;
  698. }
  699. /**
  700. * rsi_disconnect() - This function performs the reverse of the probe function,
  701. * it deinitialize the driver structure.
  702. * @pfunction: Pointer to the USB interface structure.
  703. *
  704. * Return: None.
  705. */
  706. static void rsi_disconnect(struct usb_interface *pfunction)
  707. {
  708. struct rsi_hw *adapter = usb_get_intfdata(pfunction);
  709. if (!adapter)
  710. return;
  711. rsi_mac80211_detach(adapter);
  712. if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
  713. adapter->priv->bt_adapter) {
  714. rsi_bt_ops.detach(adapter->priv->bt_adapter);
  715. adapter->priv->bt_adapter = NULL;
  716. }
  717. if (adapter->priv->coex_mode > 1)
  718. rsi_rx_urb_kill(adapter, BT_EP);
  719. rsi_rx_urb_kill(adapter, WLAN_EP);
  720. rsi_reset_card(adapter);
  721. rsi_deinit_usb_interface(adapter);
  722. rsi_91x_deinit(adapter);
  723. rsi_dbg(INFO_ZONE, "%s: Deinitialization completed\n", __func__);
  724. }
  725. #ifdef CONFIG_PM
  726. static int rsi_suspend(struct usb_interface *intf, pm_message_t message)
  727. {
  728. /* Not yet implemented */
  729. return -ENOSYS;
  730. }
  731. static int rsi_resume(struct usb_interface *intf)
  732. {
  733. /* Not yet implemented */
  734. return -ENOSYS;
  735. }
  736. #endif
  737. static const struct usb_device_id rsi_dev_table[] = {
  738. { USB_DEVICE(RSI_USB_VID_9113, RSI_USB_PID_9113) },
  739. { /* Blank */},
  740. };
  741. static struct usb_driver rsi_driver = {
  742. .name = "RSI-USB WLAN",
  743. .probe = rsi_probe,
  744. .disconnect = rsi_disconnect,
  745. .id_table = rsi_dev_table,
  746. #ifdef CONFIG_PM
  747. .suspend = rsi_suspend,
  748. .resume = rsi_resume,
  749. #endif
  750. };
  751. module_usb_driver(rsi_driver);
  752. MODULE_AUTHOR("Redpine Signals Inc");
  753. MODULE_DESCRIPTION("Common USB layer for RSI drivers");
  754. MODULE_SUPPORTED_DEVICE("RSI-91x");
  755. MODULE_DEVICE_TABLE(usb, rsi_dev_table);
  756. MODULE_FIRMWARE(FIRMWARE_RSI9113);
  757. MODULE_VERSION("0.1");
  758. MODULE_LICENSE("Dual BSD/GPL");