hif_usb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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;
  607. /* Register Read */
  608. if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
  609. goto err;
  610. return 0;
  611. err:
  612. return -ENOMEM;
  613. }
  614. static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
  615. {
  616. usb_kill_anchored_urbs(&hif_dev->regout_submitted);
  617. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  618. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  619. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  620. }
  621. static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
  622. {
  623. int transfer, err;
  624. const void *data = hif_dev->firmware->data;
  625. size_t len = hif_dev->firmware->size;
  626. u32 addr = AR9271_FIRMWARE;
  627. u8 *buf = kzalloc(4096, GFP_KERNEL);
  628. u32 firm_offset;
  629. if (!buf)
  630. return -ENOMEM;
  631. while (len) {
  632. transfer = min_t(int, len, 4096);
  633. memcpy(buf, data, transfer);
  634. err = usb_control_msg(hif_dev->udev,
  635. usb_sndctrlpipe(hif_dev->udev, 0),
  636. FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
  637. addr >> 8, 0, buf, transfer, HZ);
  638. if (err < 0) {
  639. kfree(buf);
  640. return err;
  641. }
  642. len -= transfer;
  643. data += transfer;
  644. addr += transfer;
  645. }
  646. kfree(buf);
  647. if (hif_dev->device_id == 0x7010)
  648. firm_offset = AR7010_FIRMWARE_TEXT;
  649. else
  650. firm_offset = AR9271_FIRMWARE_TEXT;
  651. /*
  652. * Issue FW download complete command to firmware.
  653. */
  654. err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
  655. FIRMWARE_DOWNLOAD_COMP,
  656. 0x40 | USB_DIR_OUT,
  657. firm_offset >> 8, 0, NULL, 0, HZ);
  658. if (err)
  659. return -EIO;
  660. dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
  661. hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
  662. return 0;
  663. }
  664. static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
  665. {
  666. int ret;
  667. /* Request firmware */
  668. ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
  669. &hif_dev->udev->dev);
  670. if (ret) {
  671. dev_err(&hif_dev->udev->dev,
  672. "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
  673. goto err_fw_req;
  674. }
  675. /* Alloc URBs */
  676. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  677. if (ret) {
  678. dev_err(&hif_dev->udev->dev,
  679. "ath9k_htc: Unable to allocate URBs\n");
  680. goto err_urb;
  681. }
  682. /* Download firmware */
  683. ret = ath9k_hif_usb_download_fw(hif_dev);
  684. if (ret) {
  685. dev_err(&hif_dev->udev->dev,
  686. "ath9k_htc: Firmware - %s download failed\n",
  687. hif_dev->fw_name);
  688. goto err_fw_download;
  689. }
  690. return 0;
  691. err_fw_download:
  692. ath9k_hif_usb_dealloc_urbs(hif_dev);
  693. err_urb:
  694. release_firmware(hif_dev->firmware);
  695. err_fw_req:
  696. hif_dev->firmware = NULL;
  697. return ret;
  698. }
  699. static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
  700. {
  701. ath9k_hif_usb_dealloc_urbs(hif_dev);
  702. if (hif_dev->firmware)
  703. release_firmware(hif_dev->firmware);
  704. }
  705. static int ath9k_hif_usb_probe(struct usb_interface *interface,
  706. const struct usb_device_id *id)
  707. {
  708. struct usb_device *udev = interface_to_usbdev(interface);
  709. struct hif_device_usb *hif_dev;
  710. int ret = 0;
  711. hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
  712. if (!hif_dev) {
  713. ret = -ENOMEM;
  714. goto err_alloc;
  715. }
  716. usb_get_dev(udev);
  717. hif_dev->udev = udev;
  718. hif_dev->interface = interface;
  719. hif_dev->device_id = id->idProduct;
  720. #ifdef CONFIG_PM
  721. udev->reset_resume = 1;
  722. #endif
  723. usb_set_intfdata(interface, hif_dev);
  724. hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
  725. &hif_dev->udev->dev);
  726. if (hif_dev->htc_handle == NULL) {
  727. ret = -ENOMEM;
  728. goto err_htc_hw_alloc;
  729. }
  730. /* Find out which firmware to load */
  731. switch(hif_dev->device_id) {
  732. case 0x7010:
  733. case 0x9018:
  734. if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
  735. hif_dev->fw_name = FIRMWARE_AR7010_1_1;
  736. else
  737. hif_dev->fw_name = FIRMWARE_AR7010;
  738. break;
  739. default:
  740. hif_dev->fw_name = FIRMWARE_AR9271;
  741. break;
  742. }
  743. if (!hif_dev->fw_name) {
  744. dev_err(&udev->dev, "Can't determine firmware !\n");
  745. goto err_htc_hw_alloc;
  746. }
  747. ret = ath9k_hif_usb_dev_init(hif_dev);
  748. if (ret) {
  749. ret = -EINVAL;
  750. goto err_hif_init_usb;
  751. }
  752. ret = ath9k_htc_hw_init(hif_dev->htc_handle,
  753. &hif_dev->udev->dev, hif_dev->device_id);
  754. if (ret) {
  755. ret = -EINVAL;
  756. goto err_htc_hw_init;
  757. }
  758. dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
  759. return 0;
  760. err_htc_hw_init:
  761. ath9k_hif_usb_dev_deinit(hif_dev);
  762. err_hif_init_usb:
  763. ath9k_htc_hw_free(hif_dev->htc_handle);
  764. err_htc_hw_alloc:
  765. usb_set_intfdata(interface, NULL);
  766. kfree(hif_dev);
  767. usb_put_dev(udev);
  768. err_alloc:
  769. return ret;
  770. }
  771. static void ath9k_hif_usb_reboot(struct usb_device *udev)
  772. {
  773. u32 reboot_cmd = 0xffffffff;
  774. void *buf;
  775. int ret;
  776. buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
  777. if (!buf)
  778. return;
  779. ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
  780. buf, 4, NULL, HZ);
  781. if (ret)
  782. dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
  783. kfree(buf);
  784. }
  785. static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
  786. {
  787. struct usb_device *udev = interface_to_usbdev(interface);
  788. struct hif_device_usb *hif_dev =
  789. (struct hif_device_usb *) usb_get_intfdata(interface);
  790. if (hif_dev) {
  791. ath9k_htc_hw_deinit(hif_dev->htc_handle,
  792. (udev->state == USB_STATE_NOTATTACHED) ? true : false);
  793. ath9k_htc_hw_free(hif_dev->htc_handle);
  794. ath9k_hif_usb_dev_deinit(hif_dev);
  795. usb_set_intfdata(interface, NULL);
  796. }
  797. if (hif_dev->flags & HIF_USB_START)
  798. ath9k_hif_usb_reboot(udev);
  799. kfree(hif_dev);
  800. dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
  801. usb_put_dev(udev);
  802. }
  803. #ifdef CONFIG_PM
  804. static int ath9k_hif_usb_suspend(struct usb_interface *interface,
  805. pm_message_t message)
  806. {
  807. struct hif_device_usb *hif_dev =
  808. (struct hif_device_usb *) usb_get_intfdata(interface);
  809. ath9k_hif_usb_dealloc_urbs(hif_dev);
  810. return 0;
  811. }
  812. static int ath9k_hif_usb_resume(struct usb_interface *interface)
  813. {
  814. struct hif_device_usb *hif_dev =
  815. (struct hif_device_usb *) usb_get_intfdata(interface);
  816. int ret;
  817. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  818. if (ret)
  819. return ret;
  820. if (hif_dev->firmware) {
  821. ret = ath9k_hif_usb_download_fw(hif_dev);
  822. if (ret)
  823. goto fail_resume;
  824. } else {
  825. ath9k_hif_usb_dealloc_urbs(hif_dev);
  826. return -EIO;
  827. }
  828. mdelay(100);
  829. ret = ath9k_htc_resume(hif_dev->htc_handle);
  830. if (ret)
  831. goto fail_resume;
  832. return 0;
  833. fail_resume:
  834. ath9k_hif_usb_dealloc_urbs(hif_dev);
  835. return ret;
  836. }
  837. #endif
  838. static struct usb_driver ath9k_hif_usb_driver = {
  839. .name = "ath9k_hif_usb",
  840. .probe = ath9k_hif_usb_probe,
  841. .disconnect = ath9k_hif_usb_disconnect,
  842. #ifdef CONFIG_PM
  843. .suspend = ath9k_hif_usb_suspend,
  844. .resume = ath9k_hif_usb_resume,
  845. .reset_resume = ath9k_hif_usb_resume,
  846. #endif
  847. .id_table = ath9k_hif_usb_ids,
  848. .soft_unbind = 1,
  849. };
  850. int ath9k_hif_usb_init(void)
  851. {
  852. return usb_register(&ath9k_hif_usb_driver);
  853. }
  854. void ath9k_hif_usb_exit(void)
  855. {
  856. usb_deregister(&ath9k_hif_usb_driver);
  857. }