hif_usb.c 25 KB

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