hif_usb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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_bulk_urb(urb, hif_dev->udev,
  81. usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
  82. skb->data, skb->len,
  83. hif_usb_regout_cb, cmd);
  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_rcvbulkpipe(hif_dev->udev,
  443. USB_REG_IN_PIPE),
  444. nskb->data, MAX_REG_IN_BUF_SIZE,
  445. ath9k_hif_usb_reg_in_cb, nskb, 1);
  446. ret = usb_submit_urb(urb, GFP_ATOMIC);
  447. if (ret) {
  448. kfree_skb(nskb);
  449. urb->context = NULL;
  450. }
  451. return;
  452. }
  453. resubmit:
  454. skb_reset_tail_pointer(skb);
  455. skb_trim(skb, 0);
  456. ret = usb_submit_urb(urb, GFP_ATOMIC);
  457. if (ret)
  458. goto free;
  459. return;
  460. free:
  461. kfree_skb(skb);
  462. urb->context = NULL;
  463. }
  464. static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
  465. {
  466. struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
  467. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  468. &hif_dev->tx.tx_buf, list) {
  469. usb_kill_urb(tx_buf->urb);
  470. list_del(&tx_buf->list);
  471. usb_free_urb(tx_buf->urb);
  472. kfree(tx_buf->buf);
  473. kfree(tx_buf);
  474. }
  475. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  476. &hif_dev->tx.tx_pending, list) {
  477. usb_kill_urb(tx_buf->urb);
  478. list_del(&tx_buf->list);
  479. usb_free_urb(tx_buf->urb);
  480. kfree(tx_buf->buf);
  481. kfree(tx_buf);
  482. }
  483. }
  484. static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
  485. {
  486. struct tx_buf *tx_buf;
  487. int i;
  488. INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
  489. INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
  490. spin_lock_init(&hif_dev->tx.tx_lock);
  491. __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
  492. for (i = 0; i < MAX_TX_URB_NUM; i++) {
  493. tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
  494. if (!tx_buf)
  495. goto err;
  496. tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
  497. if (!tx_buf->buf)
  498. goto err;
  499. tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
  500. if (!tx_buf->urb)
  501. goto err;
  502. tx_buf->hif_dev = hif_dev;
  503. __skb_queue_head_init(&tx_buf->skb_queue);
  504. list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
  505. }
  506. hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
  507. return 0;
  508. err:
  509. if (tx_buf) {
  510. kfree(tx_buf->buf);
  511. kfree(tx_buf);
  512. }
  513. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  514. return -ENOMEM;
  515. }
  516. static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
  517. {
  518. usb_kill_anchored_urbs(&hif_dev->rx_submitted);
  519. }
  520. static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
  521. {
  522. struct urb *urb = NULL;
  523. struct sk_buff *skb = NULL;
  524. int i, ret;
  525. init_usb_anchor(&hif_dev->rx_submitted);
  526. spin_lock_init(&hif_dev->rx_lock);
  527. for (i = 0; i < MAX_RX_URB_NUM; i++) {
  528. /* Allocate URB */
  529. urb = usb_alloc_urb(0, GFP_KERNEL);
  530. if (urb == NULL) {
  531. ret = -ENOMEM;
  532. goto err_urb;
  533. }
  534. /* Allocate buffer */
  535. skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
  536. if (!skb) {
  537. ret = -ENOMEM;
  538. goto err_skb;
  539. }
  540. usb_fill_bulk_urb(urb, hif_dev->udev,
  541. usb_rcvbulkpipe(hif_dev->udev,
  542. USB_WLAN_RX_PIPE),
  543. skb->data, MAX_RX_BUF_SIZE,
  544. ath9k_hif_usb_rx_cb, skb);
  545. /* Anchor URB */
  546. usb_anchor_urb(urb, &hif_dev->rx_submitted);
  547. /* Submit URB */
  548. ret = usb_submit_urb(urb, GFP_KERNEL);
  549. if (ret) {
  550. usb_unanchor_urb(urb);
  551. goto err_submit;
  552. }
  553. /*
  554. * Drop reference count.
  555. * This ensures that the URB is freed when killing them.
  556. */
  557. usb_free_urb(urb);
  558. }
  559. return 0;
  560. err_submit:
  561. kfree_skb(skb);
  562. err_skb:
  563. usb_free_urb(urb);
  564. err_urb:
  565. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  566. return ret;
  567. }
  568. static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
  569. {
  570. if (hif_dev->reg_in_urb) {
  571. usb_kill_urb(hif_dev->reg_in_urb);
  572. if (hif_dev->reg_in_urb->context)
  573. kfree_skb((void *)hif_dev->reg_in_urb->context);
  574. usb_free_urb(hif_dev->reg_in_urb);
  575. hif_dev->reg_in_urb = NULL;
  576. }
  577. }
  578. static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
  579. {
  580. struct sk_buff *skb;
  581. hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  582. if (hif_dev->reg_in_urb == NULL)
  583. return -ENOMEM;
  584. skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
  585. if (!skb)
  586. goto err;
  587. usb_fill_int_urb(hif_dev->reg_in_urb, hif_dev->udev,
  588. usb_rcvbulkpipe(hif_dev->udev,
  589. USB_REG_IN_PIPE),
  590. skb->data, MAX_REG_IN_BUF_SIZE,
  591. ath9k_hif_usb_reg_in_cb, skb, 1);
  592. if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
  593. goto err;
  594. return 0;
  595. err:
  596. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  597. return -ENOMEM;
  598. }
  599. static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
  600. {
  601. /* Register Write */
  602. init_usb_anchor(&hif_dev->regout_submitted);
  603. /* TX */
  604. if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
  605. goto err;
  606. /* RX */
  607. if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
  608. goto err_rx;
  609. /* Register Read */
  610. if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
  611. goto err_reg;
  612. return 0;
  613. err_reg:
  614. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  615. err_rx:
  616. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  617. err:
  618. return -ENOMEM;
  619. }
  620. static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
  621. {
  622. usb_kill_anchored_urbs(&hif_dev->regout_submitted);
  623. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  624. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  625. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  626. }
  627. static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
  628. {
  629. int transfer, err;
  630. const void *data = hif_dev->firmware->data;
  631. size_t len = hif_dev->firmware->size;
  632. u32 addr = AR9271_FIRMWARE;
  633. u8 *buf = kzalloc(4096, GFP_KERNEL);
  634. u32 firm_offset;
  635. if (!buf)
  636. return -ENOMEM;
  637. while (len) {
  638. transfer = min_t(int, len, 4096);
  639. memcpy(buf, data, transfer);
  640. err = usb_control_msg(hif_dev->udev,
  641. usb_sndctrlpipe(hif_dev->udev, 0),
  642. FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
  643. addr >> 8, 0, buf, transfer, HZ);
  644. if (err < 0) {
  645. kfree(buf);
  646. return err;
  647. }
  648. len -= transfer;
  649. data += transfer;
  650. addr += transfer;
  651. }
  652. kfree(buf);
  653. if ((hif_dev->device_id == 0x7010) || (hif_dev->device_id == 0x7015))
  654. firm_offset = AR7010_FIRMWARE_TEXT;
  655. else
  656. firm_offset = AR9271_FIRMWARE_TEXT;
  657. /*
  658. * Issue FW download complete command to firmware.
  659. */
  660. err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
  661. FIRMWARE_DOWNLOAD_COMP,
  662. 0x40 | USB_DIR_OUT,
  663. firm_offset >> 8, 0, NULL, 0, HZ);
  664. if (err)
  665. return -EIO;
  666. dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
  667. hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
  668. return 0;
  669. }
  670. static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
  671. {
  672. int ret, idx;
  673. struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
  674. struct usb_endpoint_descriptor *endp;
  675. /* Request firmware */
  676. ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
  677. &hif_dev->udev->dev);
  678. if (ret) {
  679. dev_err(&hif_dev->udev->dev,
  680. "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
  681. goto err_fw_req;
  682. }
  683. /* Alloc URBs */
  684. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  685. if (ret) {
  686. dev_err(&hif_dev->udev->dev,
  687. "ath9k_htc: Unable to allocate URBs\n");
  688. goto err_urb;
  689. }
  690. /* Download firmware */
  691. ret = ath9k_hif_usb_download_fw(hif_dev);
  692. if (ret) {
  693. dev_err(&hif_dev->udev->dev,
  694. "ath9k_htc: Firmware - %s download failed\n",
  695. hif_dev->fw_name);
  696. goto err_fw_download;
  697. }
  698. /* On downloading the firmware to the target, the USB descriptor of EP4
  699. * is 'patched' to change the type of the endpoint to Bulk. This will
  700. * bring down CPU usage during the scan period.
  701. */
  702. for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
  703. endp = &alt->endpoint[idx].desc;
  704. if (((endp->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)
  705. == 0x04) &&
  706. ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  707. == USB_ENDPOINT_XFER_INT)) {
  708. endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
  709. endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
  710. endp->bInterval = 0;
  711. }
  712. }
  713. return 0;
  714. err_fw_download:
  715. ath9k_hif_usb_dealloc_urbs(hif_dev);
  716. err_urb:
  717. release_firmware(hif_dev->firmware);
  718. err_fw_req:
  719. hif_dev->firmware = NULL;
  720. return ret;
  721. }
  722. static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
  723. {
  724. ath9k_hif_usb_dealloc_urbs(hif_dev);
  725. if (hif_dev->firmware)
  726. release_firmware(hif_dev->firmware);
  727. }
  728. static int ath9k_hif_usb_probe(struct usb_interface *interface,
  729. const struct usb_device_id *id)
  730. {
  731. struct usb_device *udev = interface_to_usbdev(interface);
  732. struct hif_device_usb *hif_dev;
  733. int ret = 0;
  734. hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
  735. if (!hif_dev) {
  736. ret = -ENOMEM;
  737. goto err_alloc;
  738. }
  739. usb_get_dev(udev);
  740. hif_dev->udev = udev;
  741. hif_dev->interface = interface;
  742. hif_dev->device_id = id->idProduct;
  743. #ifdef CONFIG_PM
  744. udev->reset_resume = 1;
  745. #endif
  746. usb_set_intfdata(interface, hif_dev);
  747. hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
  748. &hif_dev->udev->dev);
  749. if (hif_dev->htc_handle == NULL) {
  750. ret = -ENOMEM;
  751. goto err_htc_hw_alloc;
  752. }
  753. /* Find out which firmware to load */
  754. switch(hif_dev->device_id) {
  755. case 0x7010:
  756. case 0x7015:
  757. case 0x9018:
  758. if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
  759. hif_dev->fw_name = FIRMWARE_AR7010_1_1;
  760. else
  761. hif_dev->fw_name = FIRMWARE_AR7010;
  762. break;
  763. default:
  764. hif_dev->fw_name = FIRMWARE_AR9271;
  765. break;
  766. }
  767. ret = ath9k_hif_usb_dev_init(hif_dev);
  768. if (ret) {
  769. ret = -EINVAL;
  770. goto err_hif_init_usb;
  771. }
  772. ret = ath9k_htc_hw_init(hif_dev->htc_handle,
  773. &hif_dev->udev->dev, hif_dev->device_id,
  774. hif_dev->udev->product);
  775. if (ret) {
  776. ret = -EINVAL;
  777. goto err_htc_hw_init;
  778. }
  779. dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
  780. return 0;
  781. err_htc_hw_init:
  782. ath9k_hif_usb_dev_deinit(hif_dev);
  783. err_hif_init_usb:
  784. ath9k_htc_hw_free(hif_dev->htc_handle);
  785. err_htc_hw_alloc:
  786. usb_set_intfdata(interface, NULL);
  787. kfree(hif_dev);
  788. usb_put_dev(udev);
  789. err_alloc:
  790. return ret;
  791. }
  792. static void ath9k_hif_usb_reboot(struct usb_device *udev)
  793. {
  794. u32 reboot_cmd = 0xffffffff;
  795. void *buf;
  796. int ret;
  797. buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
  798. if (!buf)
  799. return;
  800. ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
  801. buf, 4, NULL, HZ);
  802. if (ret)
  803. dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
  804. kfree(buf);
  805. }
  806. static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
  807. {
  808. struct usb_device *udev = interface_to_usbdev(interface);
  809. struct hif_device_usb *hif_dev =
  810. (struct hif_device_usb *) usb_get_intfdata(interface);
  811. if (hif_dev) {
  812. ath9k_htc_hw_deinit(hif_dev->htc_handle,
  813. (udev->state == USB_STATE_NOTATTACHED) ? true : false);
  814. ath9k_htc_hw_free(hif_dev->htc_handle);
  815. ath9k_hif_usb_dev_deinit(hif_dev);
  816. usb_set_intfdata(interface, NULL);
  817. }
  818. if (hif_dev->flags & HIF_USB_START)
  819. ath9k_hif_usb_reboot(udev);
  820. kfree(hif_dev);
  821. dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
  822. usb_put_dev(udev);
  823. }
  824. #ifdef CONFIG_PM
  825. static int ath9k_hif_usb_suspend(struct usb_interface *interface,
  826. pm_message_t message)
  827. {
  828. struct hif_device_usb *hif_dev =
  829. (struct hif_device_usb *) usb_get_intfdata(interface);
  830. ath9k_hif_usb_dealloc_urbs(hif_dev);
  831. return 0;
  832. }
  833. static int ath9k_hif_usb_resume(struct usb_interface *interface)
  834. {
  835. struct hif_device_usb *hif_dev =
  836. (struct hif_device_usb *) usb_get_intfdata(interface);
  837. int ret;
  838. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  839. if (ret)
  840. return ret;
  841. if (hif_dev->firmware) {
  842. ret = ath9k_hif_usb_download_fw(hif_dev);
  843. if (ret)
  844. goto fail_resume;
  845. } else {
  846. ath9k_hif_usb_dealloc_urbs(hif_dev);
  847. return -EIO;
  848. }
  849. mdelay(100);
  850. ret = ath9k_htc_resume(hif_dev->htc_handle);
  851. if (ret)
  852. goto fail_resume;
  853. return 0;
  854. fail_resume:
  855. ath9k_hif_usb_dealloc_urbs(hif_dev);
  856. return ret;
  857. }
  858. #endif
  859. static struct usb_driver ath9k_hif_usb_driver = {
  860. .name = "ath9k_hif_usb",
  861. .probe = ath9k_hif_usb_probe,
  862. .disconnect = ath9k_hif_usb_disconnect,
  863. #ifdef CONFIG_PM
  864. .suspend = ath9k_hif_usb_suspend,
  865. .resume = ath9k_hif_usb_resume,
  866. .reset_resume = ath9k_hif_usb_resume,
  867. #endif
  868. .id_table = ath9k_hif_usb_ids,
  869. .soft_unbind = 1,
  870. };
  871. int ath9k_hif_usb_init(void)
  872. {
  873. return usb_register(&ath9k_hif_usb_driver);
  874. }
  875. void ath9k_hif_usb_exit(void)
  876. {
  877. usb_deregister(&ath9k_hif_usb_driver);
  878. }