hif_usb.c 23 KB

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