hif_usb.c 28 KB

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