hif_usb.c 24 KB

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