hif_usb.c 25 KB

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