hif_usb.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * Copyright (c) 2010 Atheros Communications 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. #include "htc.h"
  17. static struct usb_device_id ath9k_hif_usb_ids[] = {
  18. { USB_DEVICE(0x0cf3, 0x9271) },
  19. { USB_DEVICE(0x0cf3, 0x1006) },
  20. { },
  21. };
  22. MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
  23. static int __hif_usb_tx(struct hif_device_usb *hif_dev);
  24. static void hif_usb_regout_cb(struct urb *urb)
  25. {
  26. struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
  27. switch (urb->status) {
  28. case 0:
  29. break;
  30. case -ENOENT:
  31. case -ECONNRESET:
  32. case -ENODEV:
  33. case -ESHUTDOWN:
  34. goto free;
  35. default:
  36. break;
  37. }
  38. if (cmd) {
  39. ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
  40. cmd->skb, 1);
  41. kfree(cmd);
  42. }
  43. return;
  44. free:
  45. kfree_skb(cmd->skb);
  46. kfree(cmd);
  47. }
  48. static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
  49. struct sk_buff *skb)
  50. {
  51. struct urb *urb;
  52. struct cmd_buf *cmd;
  53. int ret = 0;
  54. urb = usb_alloc_urb(0, GFP_KERNEL);
  55. if (urb == NULL)
  56. return -ENOMEM;
  57. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  58. if (cmd == NULL) {
  59. usb_free_urb(urb);
  60. return -ENOMEM;
  61. }
  62. cmd->skb = skb;
  63. cmd->hif_dev = hif_dev;
  64. usb_fill_int_urb(urb, hif_dev->udev,
  65. usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
  66. skb->data, skb->len,
  67. hif_usb_regout_cb, cmd, 1);
  68. usb_anchor_urb(urb, &hif_dev->regout_submitted);
  69. ret = usb_submit_urb(urb, GFP_KERNEL);
  70. if (ret) {
  71. usb_unanchor_urb(urb);
  72. kfree(cmd);
  73. }
  74. usb_free_urb(urb);
  75. return ret;
  76. }
  77. static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
  78. struct sk_buff_head *list)
  79. {
  80. struct sk_buff *skb;
  81. while ((skb = __skb_dequeue(list)) != NULL) {
  82. dev_kfree_skb_any(skb);
  83. TX_STAT_INC(skb_dropped);
  84. }
  85. }
  86. static void hif_usb_tx_cb(struct urb *urb)
  87. {
  88. struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
  89. struct hif_device_usb *hif_dev;
  90. struct sk_buff *skb;
  91. if (!tx_buf || !tx_buf->hif_dev)
  92. return;
  93. hif_dev = tx_buf->hif_dev;
  94. switch (urb->status) {
  95. case 0:
  96. break;
  97. case -ENOENT:
  98. case -ECONNRESET:
  99. case -ENODEV:
  100. case -ESHUTDOWN:
  101. /*
  102. * The URB has been killed, free the SKBs
  103. * and return.
  104. */
  105. ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
  106. return;
  107. default:
  108. break;
  109. }
  110. /* Check if TX has been stopped */
  111. spin_lock(&hif_dev->tx.tx_lock);
  112. if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
  113. spin_unlock(&hif_dev->tx.tx_lock);
  114. ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
  115. goto add_free;
  116. }
  117. spin_unlock(&hif_dev->tx.tx_lock);
  118. /* Complete the queued SKBs. */
  119. while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
  120. ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
  121. skb, 1);
  122. TX_STAT_INC(skb_completed);
  123. }
  124. add_free:
  125. /* Re-initialize the SKB queue */
  126. tx_buf->len = tx_buf->offset = 0;
  127. __skb_queue_head_init(&tx_buf->skb_queue);
  128. /* Add this TX buffer to the free list */
  129. spin_lock(&hif_dev->tx.tx_lock);
  130. list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
  131. hif_dev->tx.tx_buf_cnt++;
  132. if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
  133. __hif_usb_tx(hif_dev); /* Check for pending SKBs */
  134. TX_STAT_INC(buf_completed);
  135. spin_unlock(&hif_dev->tx.tx_lock);
  136. }
  137. /* TX lock has to be taken */
  138. static int __hif_usb_tx(struct hif_device_usb *hif_dev)
  139. {
  140. struct tx_buf *tx_buf = NULL;
  141. struct sk_buff *nskb = NULL;
  142. int ret = 0, i;
  143. u16 *hdr, tx_skb_cnt = 0;
  144. u8 *buf;
  145. if (hif_dev->tx.tx_skb_cnt == 0)
  146. return 0;
  147. /* Check if a free TX buffer is available */
  148. if (list_empty(&hif_dev->tx.tx_buf))
  149. return 0;
  150. tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
  151. list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
  152. hif_dev->tx.tx_buf_cnt--;
  153. tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
  154. for (i = 0; i < tx_skb_cnt; i++) {
  155. nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
  156. /* Should never be NULL */
  157. BUG_ON(!nskb);
  158. hif_dev->tx.tx_skb_cnt--;
  159. buf = tx_buf->buf;
  160. buf += tx_buf->offset;
  161. hdr = (u16 *)buf;
  162. *hdr++ = nskb->len;
  163. *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
  164. buf += 4;
  165. memcpy(buf, nskb->data, nskb->len);
  166. tx_buf->len = nskb->len + 4;
  167. if (i < (tx_skb_cnt - 1))
  168. tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
  169. if (i == (tx_skb_cnt - 1))
  170. tx_buf->len += tx_buf->offset;
  171. __skb_queue_tail(&tx_buf->skb_queue, nskb);
  172. TX_STAT_INC(skb_queued);
  173. }
  174. usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
  175. usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
  176. tx_buf->buf, tx_buf->len,
  177. hif_usb_tx_cb, tx_buf);
  178. ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
  179. if (ret) {
  180. tx_buf->len = tx_buf->offset = 0;
  181. ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
  182. __skb_queue_head_init(&tx_buf->skb_queue);
  183. list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
  184. hif_dev->tx.tx_buf_cnt++;
  185. }
  186. if (!ret)
  187. TX_STAT_INC(buf_queued);
  188. return ret;
  189. }
  190. static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
  191. struct ath9k_htc_tx_ctl *tx_ctl)
  192. {
  193. unsigned long flags;
  194. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  195. if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
  196. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  197. return -ENODEV;
  198. }
  199. /* Check if the max queue count has been reached */
  200. if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
  201. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  202. return -ENOMEM;
  203. }
  204. __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
  205. hif_dev->tx.tx_skb_cnt++;
  206. /* Send normal frames immediately */
  207. if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
  208. __hif_usb_tx(hif_dev);
  209. /* Check if AMPDUs have to be sent immediately */
  210. if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
  211. (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
  212. (hif_dev->tx.tx_skb_cnt < 2)) {
  213. __hif_usb_tx(hif_dev);
  214. }
  215. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  216. return 0;
  217. }
  218. static void hif_usb_start(void *hif_handle, u8 pipe_id)
  219. {
  220. struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
  221. unsigned long flags;
  222. hif_dev->flags |= HIF_USB_START;
  223. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  224. hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
  225. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  226. }
  227. static void hif_usb_stop(void *hif_handle, u8 pipe_id)
  228. {
  229. struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
  230. unsigned long flags;
  231. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  232. ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
  233. hif_dev->tx.tx_skb_cnt = 0;
  234. hif_dev->tx.flags |= HIF_USB_TX_STOP;
  235. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  236. }
  237. static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
  238. struct ath9k_htc_tx_ctl *tx_ctl)
  239. {
  240. struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
  241. int ret = 0;
  242. switch (pipe_id) {
  243. case USB_WLAN_TX_PIPE:
  244. ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
  245. break;
  246. case USB_REG_OUT_PIPE:
  247. ret = hif_usb_send_regout(hif_dev, skb);
  248. break;
  249. default:
  250. dev_err(&hif_dev->udev->dev,
  251. "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
  252. ret = -EINVAL;
  253. break;
  254. }
  255. return ret;
  256. }
  257. static struct ath9k_htc_hif hif_usb = {
  258. .transport = ATH9K_HIF_USB,
  259. .name = "ath9k_hif_usb",
  260. .control_ul_pipe = USB_REG_OUT_PIPE,
  261. .control_dl_pipe = USB_REG_IN_PIPE,
  262. .start = hif_usb_start,
  263. .stop = hif_usb_stop,
  264. .send = hif_usb_send,
  265. };
  266. static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
  267. struct sk_buff *skb)
  268. {
  269. struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
  270. int index = 0, i = 0, chk_idx, len = skb->len;
  271. int rx_remain_len = 0, rx_pkt_len = 0;
  272. u16 pkt_len, pkt_tag, pool_index = 0;
  273. u8 *ptr;
  274. spin_lock(&hif_dev->rx_lock);
  275. rx_remain_len = hif_dev->rx_remain_len;
  276. rx_pkt_len = hif_dev->rx_transfer_len;
  277. if (rx_remain_len != 0) {
  278. struct sk_buff *remain_skb = hif_dev->remain_skb;
  279. if (remain_skb) {
  280. ptr = (u8 *) remain_skb->data;
  281. index = rx_remain_len;
  282. rx_remain_len -= hif_dev->rx_pad_len;
  283. ptr += rx_pkt_len;
  284. memcpy(ptr, skb->data, rx_remain_len);
  285. rx_pkt_len += rx_remain_len;
  286. hif_dev->rx_remain_len = 0;
  287. skb_put(remain_skb, rx_pkt_len);
  288. skb_pool[pool_index++] = remain_skb;
  289. } else {
  290. index = rx_remain_len;
  291. }
  292. }
  293. spin_unlock(&hif_dev->rx_lock);
  294. while (index < len) {
  295. ptr = (u8 *) skb->data;
  296. pkt_len = ptr[index] + (ptr[index+1] << 8);
  297. pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
  298. if (pkt_tag == ATH_USB_RX_STREAM_MODE_TAG) {
  299. u16 pad_len;
  300. pad_len = 4 - (pkt_len & 0x3);
  301. if (pad_len == 4)
  302. pad_len = 0;
  303. chk_idx = index;
  304. index = index + 4 + pkt_len + pad_len;
  305. if (index > MAX_RX_BUF_SIZE) {
  306. spin_lock(&hif_dev->rx_lock);
  307. hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
  308. hif_dev->rx_transfer_len =
  309. MAX_RX_BUF_SIZE - chk_idx - 4;
  310. hif_dev->rx_pad_len = pad_len;
  311. nskb = __dev_alloc_skb(pkt_len + 32,
  312. GFP_ATOMIC);
  313. if (!nskb) {
  314. dev_err(&hif_dev->udev->dev,
  315. "ath9k_htc: RX memory allocation"
  316. " error\n");
  317. spin_unlock(&hif_dev->rx_lock);
  318. goto err;
  319. }
  320. skb_reserve(nskb, 32);
  321. RX_STAT_INC(skb_allocated);
  322. memcpy(nskb->data, &(skb->data[chk_idx+4]),
  323. hif_dev->rx_transfer_len);
  324. /* Record the buffer pointer */
  325. hif_dev->remain_skb = nskb;
  326. spin_unlock(&hif_dev->rx_lock);
  327. } else {
  328. nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
  329. if (!nskb) {
  330. dev_err(&hif_dev->udev->dev,
  331. "ath9k_htc: RX memory allocation"
  332. " error\n");
  333. goto err;
  334. }
  335. skb_reserve(nskb, 32);
  336. RX_STAT_INC(skb_allocated);
  337. memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
  338. skb_put(nskb, pkt_len);
  339. skb_pool[pool_index++] = nskb;
  340. }
  341. } else {
  342. RX_STAT_INC(skb_dropped);
  343. return;
  344. }
  345. }
  346. err:
  347. for (i = 0; i < pool_index; i++) {
  348. ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
  349. skb_pool[i]->len, USB_WLAN_RX_PIPE);
  350. RX_STAT_INC(skb_completed);
  351. }
  352. }
  353. static void ath9k_hif_usb_rx_cb(struct urb *urb)
  354. {
  355. struct sk_buff *skb = (struct sk_buff *) urb->context;
  356. struct hif_device_usb *hif_dev = (struct hif_device_usb *)
  357. usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  358. int ret;
  359. if (!skb)
  360. return;
  361. if (!hif_dev)
  362. goto free;
  363. switch (urb->status) {
  364. case 0:
  365. break;
  366. case -ENOENT:
  367. case -ECONNRESET:
  368. case -ENODEV:
  369. case -ESHUTDOWN:
  370. goto free;
  371. default:
  372. goto resubmit;
  373. }
  374. if (likely(urb->actual_length != 0)) {
  375. skb_put(skb, urb->actual_length);
  376. ath9k_hif_usb_rx_stream(hif_dev, skb);
  377. }
  378. resubmit:
  379. skb_reset_tail_pointer(skb);
  380. skb_trim(skb, 0);
  381. usb_anchor_urb(urb, &hif_dev->rx_submitted);
  382. ret = usb_submit_urb(urb, GFP_ATOMIC);
  383. if (ret) {
  384. usb_unanchor_urb(urb);
  385. goto free;
  386. }
  387. return;
  388. free:
  389. kfree_skb(skb);
  390. }
  391. static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
  392. {
  393. struct sk_buff *skb = (struct sk_buff *) urb->context;
  394. struct sk_buff *nskb;
  395. struct hif_device_usb *hif_dev = (struct hif_device_usb *)
  396. usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  397. int ret;
  398. if (!skb)
  399. return;
  400. if (!hif_dev)
  401. goto free;
  402. switch (urb->status) {
  403. case 0:
  404. break;
  405. case -ENOENT:
  406. case -ECONNRESET:
  407. case -ENODEV:
  408. case -ESHUTDOWN:
  409. goto free;
  410. default:
  411. goto resubmit;
  412. }
  413. if (likely(urb->actual_length != 0)) {
  414. skb_put(skb, urb->actual_length);
  415. /* Process the command first */
  416. ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
  417. skb->len, USB_REG_IN_PIPE);
  418. nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
  419. if (!nskb) {
  420. dev_err(&hif_dev->udev->dev,
  421. "ath9k_htc: REG_IN memory allocation failure\n");
  422. urb->context = NULL;
  423. return;
  424. }
  425. usb_fill_int_urb(urb, hif_dev->udev,
  426. usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
  427. nskb->data, MAX_REG_IN_BUF_SIZE,
  428. ath9k_hif_usb_reg_in_cb, nskb, 1);
  429. ret = usb_submit_urb(urb, GFP_ATOMIC);
  430. if (ret) {
  431. kfree_skb(nskb);
  432. urb->context = NULL;
  433. }
  434. return;
  435. }
  436. resubmit:
  437. skb_reset_tail_pointer(skb);
  438. skb_trim(skb, 0);
  439. ret = usb_submit_urb(urb, GFP_ATOMIC);
  440. if (ret)
  441. goto free;
  442. return;
  443. free:
  444. kfree_skb(skb);
  445. urb->context = NULL;
  446. }
  447. static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
  448. {
  449. struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
  450. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  451. &hif_dev->tx.tx_buf, list) {
  452. usb_kill_urb(tx_buf->urb);
  453. list_del(&tx_buf->list);
  454. usb_free_urb(tx_buf->urb);
  455. kfree(tx_buf->buf);
  456. kfree(tx_buf);
  457. }
  458. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  459. &hif_dev->tx.tx_pending, list) {
  460. usb_kill_urb(tx_buf->urb);
  461. list_del(&tx_buf->list);
  462. usb_free_urb(tx_buf->urb);
  463. kfree(tx_buf->buf);
  464. kfree(tx_buf);
  465. }
  466. }
  467. static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
  468. {
  469. struct tx_buf *tx_buf;
  470. int i;
  471. INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
  472. INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
  473. spin_lock_init(&hif_dev->tx.tx_lock);
  474. __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
  475. for (i = 0; i < MAX_TX_URB_NUM; i++) {
  476. tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
  477. if (!tx_buf)
  478. goto err;
  479. tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
  480. if (!tx_buf->buf)
  481. goto err;
  482. tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
  483. if (!tx_buf->urb)
  484. goto err;
  485. tx_buf->hif_dev = hif_dev;
  486. __skb_queue_head_init(&tx_buf->skb_queue);
  487. list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
  488. }
  489. hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
  490. return 0;
  491. err:
  492. if (tx_buf) {
  493. kfree(tx_buf->buf);
  494. kfree(tx_buf);
  495. }
  496. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  497. return -ENOMEM;
  498. }
  499. static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
  500. {
  501. usb_kill_anchored_urbs(&hif_dev->rx_submitted);
  502. }
  503. static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
  504. {
  505. struct urb *urb = NULL;
  506. struct sk_buff *skb = NULL;
  507. int i, ret;
  508. init_usb_anchor(&hif_dev->rx_submitted);
  509. spin_lock_init(&hif_dev->rx_lock);
  510. for (i = 0; i < MAX_RX_URB_NUM; i++) {
  511. /* Allocate URB */
  512. urb = usb_alloc_urb(0, GFP_KERNEL);
  513. if (urb == NULL) {
  514. ret = -ENOMEM;
  515. goto err_urb;
  516. }
  517. /* Allocate buffer */
  518. skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
  519. if (!skb) {
  520. ret = -ENOMEM;
  521. goto err_skb;
  522. }
  523. usb_fill_bulk_urb(urb, hif_dev->udev,
  524. usb_rcvbulkpipe(hif_dev->udev,
  525. USB_WLAN_RX_PIPE),
  526. skb->data, MAX_RX_BUF_SIZE,
  527. ath9k_hif_usb_rx_cb, skb);
  528. /* Anchor URB */
  529. usb_anchor_urb(urb, &hif_dev->rx_submitted);
  530. /* Submit URB */
  531. ret = usb_submit_urb(urb, GFP_KERNEL);
  532. if (ret) {
  533. usb_unanchor_urb(urb);
  534. goto err_submit;
  535. }
  536. /*
  537. * Drop reference count.
  538. * This ensures that the URB is freed when killing them.
  539. */
  540. usb_free_urb(urb);
  541. }
  542. return 0;
  543. err_submit:
  544. kfree_skb(skb);
  545. err_skb:
  546. usb_free_urb(urb);
  547. err_urb:
  548. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  549. return ret;
  550. }
  551. static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
  552. {
  553. if (hif_dev->reg_in_urb) {
  554. usb_kill_urb(hif_dev->reg_in_urb);
  555. if (hif_dev->reg_in_urb->context)
  556. kfree_skb((void *)hif_dev->reg_in_urb->context);
  557. usb_free_urb(hif_dev->reg_in_urb);
  558. hif_dev->reg_in_urb = NULL;
  559. }
  560. }
  561. static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
  562. {
  563. struct sk_buff *skb;
  564. hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  565. if (hif_dev->reg_in_urb == NULL)
  566. return -ENOMEM;
  567. skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
  568. if (!skb)
  569. goto err;
  570. usb_fill_int_urb(hif_dev->reg_in_urb, hif_dev->udev,
  571. usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
  572. skb->data, MAX_REG_IN_BUF_SIZE,
  573. ath9k_hif_usb_reg_in_cb, skb, 1);
  574. if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
  575. goto err;
  576. return 0;
  577. err:
  578. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  579. return -ENOMEM;
  580. }
  581. static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
  582. {
  583. /* Register Write */
  584. init_usb_anchor(&hif_dev->regout_submitted);
  585. /* TX */
  586. if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
  587. goto err;
  588. /* RX */
  589. if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
  590. goto err;
  591. /* Register Read */
  592. if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
  593. goto err;
  594. return 0;
  595. err:
  596. return -ENOMEM;
  597. }
  598. static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
  599. {
  600. usb_kill_anchored_urbs(&hif_dev->regout_submitted);
  601. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  602. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  603. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  604. }
  605. static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
  606. {
  607. int transfer, err;
  608. const void *data = hif_dev->firmware->data;
  609. size_t len = hif_dev->firmware->size;
  610. u32 addr = AR9271_FIRMWARE;
  611. u8 *buf = kzalloc(4096, GFP_KERNEL);
  612. if (!buf)
  613. return -ENOMEM;
  614. while (len) {
  615. transfer = min_t(int, len, 4096);
  616. memcpy(buf, data, transfer);
  617. err = usb_control_msg(hif_dev->udev,
  618. usb_sndctrlpipe(hif_dev->udev, 0),
  619. FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
  620. addr >> 8, 0, buf, transfer, HZ);
  621. if (err < 0) {
  622. kfree(buf);
  623. return err;
  624. }
  625. len -= transfer;
  626. data += transfer;
  627. addr += transfer;
  628. }
  629. kfree(buf);
  630. /*
  631. * Issue FW download complete command to firmware.
  632. */
  633. err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
  634. FIRMWARE_DOWNLOAD_COMP,
  635. 0x40 | USB_DIR_OUT,
  636. AR9271_FIRMWARE_TEXT >> 8, 0, NULL, 0, HZ);
  637. if (err)
  638. return -EIO;
  639. dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
  640. hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
  641. return 0;
  642. }
  643. static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
  644. {
  645. int ret;
  646. /* Request firmware */
  647. ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
  648. &hif_dev->udev->dev);
  649. if (ret) {
  650. dev_err(&hif_dev->udev->dev,
  651. "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
  652. goto err_fw_req;
  653. }
  654. /* Alloc URBs */
  655. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  656. if (ret) {
  657. dev_err(&hif_dev->udev->dev,
  658. "ath9k_htc: Unable to allocate URBs\n");
  659. goto err_urb;
  660. }
  661. /* Download firmware */
  662. ret = ath9k_hif_usb_download_fw(hif_dev);
  663. if (ret) {
  664. dev_err(&hif_dev->udev->dev,
  665. "ath9k_htc: Firmware - %s download failed\n",
  666. hif_dev->fw_name);
  667. goto err_fw_download;
  668. }
  669. return 0;
  670. err_fw_download:
  671. ath9k_hif_usb_dealloc_urbs(hif_dev);
  672. err_urb:
  673. release_firmware(hif_dev->firmware);
  674. err_fw_req:
  675. hif_dev->firmware = NULL;
  676. return ret;
  677. }
  678. static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
  679. {
  680. ath9k_hif_usb_dealloc_urbs(hif_dev);
  681. if (hif_dev->firmware)
  682. release_firmware(hif_dev->firmware);
  683. }
  684. static int ath9k_hif_usb_probe(struct usb_interface *interface,
  685. const struct usb_device_id *id)
  686. {
  687. struct usb_device *udev = interface_to_usbdev(interface);
  688. struct hif_device_usb *hif_dev;
  689. int ret = 0;
  690. hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
  691. if (!hif_dev) {
  692. ret = -ENOMEM;
  693. goto err_alloc;
  694. }
  695. usb_get_dev(udev);
  696. hif_dev->udev = udev;
  697. hif_dev->interface = interface;
  698. hif_dev->device_id = id->idProduct;
  699. #ifdef CONFIG_PM
  700. udev->reset_resume = 1;
  701. #endif
  702. usb_set_intfdata(interface, hif_dev);
  703. hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
  704. &hif_dev->udev->dev);
  705. if (hif_dev->htc_handle == NULL) {
  706. ret = -ENOMEM;
  707. goto err_htc_hw_alloc;
  708. }
  709. /* Find out which firmware to load */
  710. switch(hif_dev->device_id) {
  711. case 0x9271:
  712. case 0x1006:
  713. hif_dev->fw_name = "ar9271.fw";
  714. break;
  715. default:
  716. break;
  717. }
  718. if (!hif_dev->fw_name) {
  719. dev_err(&udev->dev, "Can't determine firmware !\n");
  720. goto err_htc_hw_alloc;
  721. }
  722. ret = ath9k_hif_usb_dev_init(hif_dev);
  723. if (ret) {
  724. ret = -EINVAL;
  725. goto err_hif_init_usb;
  726. }
  727. ret = ath9k_htc_hw_init(hif_dev->htc_handle,
  728. &hif_dev->udev->dev, hif_dev->device_id);
  729. if (ret) {
  730. ret = -EINVAL;
  731. goto err_htc_hw_init;
  732. }
  733. dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
  734. return 0;
  735. err_htc_hw_init:
  736. ath9k_hif_usb_dev_deinit(hif_dev);
  737. err_hif_init_usb:
  738. ath9k_htc_hw_free(hif_dev->htc_handle);
  739. err_htc_hw_alloc:
  740. usb_set_intfdata(interface, NULL);
  741. kfree(hif_dev);
  742. usb_put_dev(udev);
  743. err_alloc:
  744. return ret;
  745. }
  746. static void ath9k_hif_usb_reboot(struct usb_device *udev)
  747. {
  748. u32 reboot_cmd = 0xffffffff;
  749. void *buf;
  750. int ret;
  751. buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
  752. if (!buf)
  753. return;
  754. ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
  755. buf, 4, NULL, HZ);
  756. if (ret)
  757. dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
  758. kfree(buf);
  759. }
  760. static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
  761. {
  762. struct usb_device *udev = interface_to_usbdev(interface);
  763. struct hif_device_usb *hif_dev =
  764. (struct hif_device_usb *) usb_get_intfdata(interface);
  765. if (hif_dev) {
  766. ath9k_htc_hw_deinit(hif_dev->htc_handle,
  767. (udev->state == USB_STATE_NOTATTACHED) ? true : false);
  768. ath9k_htc_hw_free(hif_dev->htc_handle);
  769. ath9k_hif_usb_dev_deinit(hif_dev);
  770. usb_set_intfdata(interface, NULL);
  771. }
  772. if (hif_dev->flags & HIF_USB_START)
  773. ath9k_hif_usb_reboot(udev);
  774. kfree(hif_dev);
  775. dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
  776. usb_put_dev(udev);
  777. }
  778. #ifdef CONFIG_PM
  779. static int ath9k_hif_usb_suspend(struct usb_interface *interface,
  780. pm_message_t message)
  781. {
  782. struct hif_device_usb *hif_dev =
  783. (struct hif_device_usb *) usb_get_intfdata(interface);
  784. ath9k_hif_usb_dealloc_urbs(hif_dev);
  785. return 0;
  786. }
  787. static int ath9k_hif_usb_resume(struct usb_interface *interface)
  788. {
  789. struct hif_device_usb *hif_dev =
  790. (struct hif_device_usb *) usb_get_intfdata(interface);
  791. int ret;
  792. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  793. if (ret)
  794. return ret;
  795. if (hif_dev->firmware) {
  796. ret = ath9k_hif_usb_download_fw(hif_dev);
  797. if (ret)
  798. goto fail_resume;
  799. } else {
  800. ath9k_hif_usb_dealloc_urbs(hif_dev);
  801. return -EIO;
  802. }
  803. mdelay(100);
  804. ret = ath9k_htc_resume(hif_dev->htc_handle);
  805. if (ret)
  806. goto fail_resume;
  807. return 0;
  808. fail_resume:
  809. ath9k_hif_usb_dealloc_urbs(hif_dev);
  810. return ret;
  811. }
  812. #endif
  813. static struct usb_driver ath9k_hif_usb_driver = {
  814. .name = "ath9k_hif_usb",
  815. .probe = ath9k_hif_usb_probe,
  816. .disconnect = ath9k_hif_usb_disconnect,
  817. #ifdef CONFIG_PM
  818. .suspend = ath9k_hif_usb_suspend,
  819. .resume = ath9k_hif_usb_resume,
  820. .reset_resume = ath9k_hif_usb_resume,
  821. #endif
  822. .id_table = ath9k_hif_usb_ids,
  823. .soft_unbind = 1,
  824. };
  825. int ath9k_hif_usb_init(void)
  826. {
  827. return usb_register(&ath9k_hif_usb_driver);
  828. }
  829. void ath9k_hif_usb_exit(void)
  830. {
  831. usb_deregister(&ath9k_hif_usb_driver);
  832. }