hif_usb.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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, len = skb->len;
  303. int rx_remain_len, rx_pkt_len;
  304. u16 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. u16 pkt_len;
  328. u16 pkt_tag;
  329. u16 pad_len;
  330. int chk_idx;
  331. ptr = (u8 *) skb->data;
  332. pkt_len = ptr[index] + (ptr[index+1] << 8);
  333. pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
  334. if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
  335. RX_STAT_INC(skb_dropped);
  336. return;
  337. }
  338. pad_len = 4 - (pkt_len & 0x3);
  339. if (pad_len == 4)
  340. pad_len = 0;
  341. chk_idx = index;
  342. index = index + 4 + pkt_len + pad_len;
  343. if (index > MAX_RX_BUF_SIZE) {
  344. spin_lock(&hif_dev->rx_lock);
  345. hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
  346. hif_dev->rx_transfer_len =
  347. MAX_RX_BUF_SIZE - chk_idx - 4;
  348. hif_dev->rx_pad_len = pad_len;
  349. nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
  350. if (!nskb) {
  351. dev_err(&hif_dev->udev->dev,
  352. "ath9k_htc: RX memory allocation error\n");
  353. spin_unlock(&hif_dev->rx_lock);
  354. goto err;
  355. }
  356. skb_reserve(nskb, 32);
  357. RX_STAT_INC(skb_allocated);
  358. memcpy(nskb->data, &(skb->data[chk_idx+4]),
  359. hif_dev->rx_transfer_len);
  360. /* Record the buffer pointer */
  361. hif_dev->remain_skb = nskb;
  362. spin_unlock(&hif_dev->rx_lock);
  363. } else {
  364. nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
  365. if (!nskb) {
  366. dev_err(&hif_dev->udev->dev,
  367. "ath9k_htc: RX memory allocation error\n");
  368. goto err;
  369. }
  370. skb_reserve(nskb, 32);
  371. RX_STAT_INC(skb_allocated);
  372. memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
  373. skb_put(nskb, pkt_len);
  374. skb_pool[pool_index++] = nskb;
  375. }
  376. }
  377. err:
  378. for (i = 0; i < pool_index; i++) {
  379. ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
  380. skb_pool[i]->len, USB_WLAN_RX_PIPE);
  381. RX_STAT_INC(skb_completed);
  382. }
  383. }
  384. static void ath9k_hif_usb_rx_cb(struct urb *urb)
  385. {
  386. struct sk_buff *skb = (struct sk_buff *) urb->context;
  387. struct hif_device_usb *hif_dev =
  388. usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  389. int ret;
  390. if (!skb)
  391. return;
  392. if (!hif_dev)
  393. goto free;
  394. switch (urb->status) {
  395. case 0:
  396. break;
  397. case -ENOENT:
  398. case -ECONNRESET:
  399. case -ENODEV:
  400. case -ESHUTDOWN:
  401. goto free;
  402. default:
  403. goto resubmit;
  404. }
  405. if (likely(urb->actual_length != 0)) {
  406. skb_put(skb, urb->actual_length);
  407. ath9k_hif_usb_rx_stream(hif_dev, skb);
  408. }
  409. resubmit:
  410. skb_reset_tail_pointer(skb);
  411. skb_trim(skb, 0);
  412. usb_anchor_urb(urb, &hif_dev->rx_submitted);
  413. ret = usb_submit_urb(urb, GFP_ATOMIC);
  414. if (ret) {
  415. usb_unanchor_urb(urb);
  416. goto free;
  417. }
  418. return;
  419. free:
  420. kfree_skb(skb);
  421. }
  422. static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
  423. {
  424. struct sk_buff *skb = (struct sk_buff *) urb->context;
  425. struct sk_buff *nskb;
  426. struct hif_device_usb *hif_dev =
  427. usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  428. int ret;
  429. if (!skb)
  430. return;
  431. if (!hif_dev)
  432. goto free;
  433. switch (urb->status) {
  434. case 0:
  435. break;
  436. case -ENOENT:
  437. case -ECONNRESET:
  438. case -ENODEV:
  439. case -ESHUTDOWN:
  440. goto free;
  441. default:
  442. goto resubmit;
  443. }
  444. if (likely(urb->actual_length != 0)) {
  445. skb_put(skb, urb->actual_length);
  446. /* Process the command first */
  447. ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
  448. skb->len, USB_REG_IN_PIPE);
  449. nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
  450. if (!nskb) {
  451. dev_err(&hif_dev->udev->dev,
  452. "ath9k_htc: REG_IN memory allocation failure\n");
  453. urb->context = NULL;
  454. return;
  455. }
  456. usb_fill_bulk_urb(urb, hif_dev->udev,
  457. usb_rcvbulkpipe(hif_dev->udev,
  458. USB_REG_IN_PIPE),
  459. nskb->data, MAX_REG_IN_BUF_SIZE,
  460. ath9k_hif_usb_reg_in_cb, nskb);
  461. ret = usb_submit_urb(urb, GFP_ATOMIC);
  462. if (ret) {
  463. kfree_skb(nskb);
  464. urb->context = NULL;
  465. }
  466. return;
  467. }
  468. resubmit:
  469. skb_reset_tail_pointer(skb);
  470. skb_trim(skb, 0);
  471. ret = usb_submit_urb(urb, GFP_ATOMIC);
  472. if (ret)
  473. goto free;
  474. return;
  475. free:
  476. kfree_skb(skb);
  477. urb->context = NULL;
  478. }
  479. static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
  480. {
  481. struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
  482. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  483. &hif_dev->tx.tx_buf, list) {
  484. usb_kill_urb(tx_buf->urb);
  485. list_del(&tx_buf->list);
  486. usb_free_urb(tx_buf->urb);
  487. kfree(tx_buf->buf);
  488. kfree(tx_buf);
  489. }
  490. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  491. &hif_dev->tx.tx_pending, list) {
  492. usb_kill_urb(tx_buf->urb);
  493. list_del(&tx_buf->list);
  494. usb_free_urb(tx_buf->urb);
  495. kfree(tx_buf->buf);
  496. kfree(tx_buf);
  497. }
  498. }
  499. static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
  500. {
  501. struct tx_buf *tx_buf;
  502. int i;
  503. INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
  504. INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
  505. spin_lock_init(&hif_dev->tx.tx_lock);
  506. __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
  507. for (i = 0; i < MAX_TX_URB_NUM; i++) {
  508. tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
  509. if (!tx_buf)
  510. goto err;
  511. tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
  512. if (!tx_buf->buf)
  513. goto err;
  514. tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
  515. if (!tx_buf->urb)
  516. goto err;
  517. tx_buf->hif_dev = hif_dev;
  518. __skb_queue_head_init(&tx_buf->skb_queue);
  519. list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
  520. }
  521. hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
  522. return 0;
  523. err:
  524. if (tx_buf) {
  525. kfree(tx_buf->buf);
  526. kfree(tx_buf);
  527. }
  528. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  529. return -ENOMEM;
  530. }
  531. static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
  532. {
  533. usb_kill_anchored_urbs(&hif_dev->rx_submitted);
  534. }
  535. static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
  536. {
  537. struct urb *urb = NULL;
  538. struct sk_buff *skb = NULL;
  539. int i, ret;
  540. init_usb_anchor(&hif_dev->rx_submitted);
  541. spin_lock_init(&hif_dev->rx_lock);
  542. for (i = 0; i < MAX_RX_URB_NUM; i++) {
  543. /* Allocate URB */
  544. urb = usb_alloc_urb(0, GFP_KERNEL);
  545. if (urb == NULL) {
  546. ret = -ENOMEM;
  547. goto err_urb;
  548. }
  549. /* Allocate buffer */
  550. skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
  551. if (!skb) {
  552. ret = -ENOMEM;
  553. goto err_skb;
  554. }
  555. usb_fill_bulk_urb(urb, hif_dev->udev,
  556. usb_rcvbulkpipe(hif_dev->udev,
  557. USB_WLAN_RX_PIPE),
  558. skb->data, MAX_RX_BUF_SIZE,
  559. ath9k_hif_usb_rx_cb, skb);
  560. /* Anchor URB */
  561. usb_anchor_urb(urb, &hif_dev->rx_submitted);
  562. /* Submit URB */
  563. ret = usb_submit_urb(urb, GFP_KERNEL);
  564. if (ret) {
  565. usb_unanchor_urb(urb);
  566. goto err_submit;
  567. }
  568. /*
  569. * Drop reference count.
  570. * This ensures that the URB is freed when killing them.
  571. */
  572. usb_free_urb(urb);
  573. }
  574. return 0;
  575. err_submit:
  576. kfree_skb(skb);
  577. err_skb:
  578. usb_free_urb(urb);
  579. err_urb:
  580. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  581. return ret;
  582. }
  583. static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
  584. {
  585. if (hif_dev->reg_in_urb) {
  586. usb_kill_urb(hif_dev->reg_in_urb);
  587. if (hif_dev->reg_in_urb->context)
  588. kfree_skb((void *)hif_dev->reg_in_urb->context);
  589. usb_free_urb(hif_dev->reg_in_urb);
  590. hif_dev->reg_in_urb = NULL;
  591. }
  592. }
  593. static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
  594. {
  595. struct sk_buff *skb;
  596. hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  597. if (hif_dev->reg_in_urb == NULL)
  598. return -ENOMEM;
  599. skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
  600. if (!skb)
  601. goto err;
  602. usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev,
  603. usb_rcvbulkpipe(hif_dev->udev,
  604. USB_REG_IN_PIPE),
  605. skb->data, MAX_REG_IN_BUF_SIZE,
  606. ath9k_hif_usb_reg_in_cb, skb);
  607. if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
  608. goto err;
  609. return 0;
  610. err:
  611. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  612. return -ENOMEM;
  613. }
  614. static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
  615. {
  616. /* Register Write */
  617. init_usb_anchor(&hif_dev->regout_submitted);
  618. /* TX */
  619. if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
  620. goto err;
  621. /* RX */
  622. if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
  623. goto err_rx;
  624. /* Register Read */
  625. if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
  626. goto err_reg;
  627. return 0;
  628. err_reg:
  629. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  630. err_rx:
  631. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  632. err:
  633. return -ENOMEM;
  634. }
  635. static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
  636. {
  637. usb_kill_anchored_urbs(&hif_dev->regout_submitted);
  638. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  639. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  640. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  641. }
  642. static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
  643. u32 drv_info)
  644. {
  645. int transfer, err;
  646. const void *data = hif_dev->firmware->data;
  647. size_t len = hif_dev->firmware->size;
  648. u32 addr = AR9271_FIRMWARE;
  649. u8 *buf = kzalloc(4096, GFP_KERNEL);
  650. u32 firm_offset;
  651. if (!buf)
  652. return -ENOMEM;
  653. while (len) {
  654. transfer = min_t(int, len, 4096);
  655. memcpy(buf, data, transfer);
  656. err = usb_control_msg(hif_dev->udev,
  657. usb_sndctrlpipe(hif_dev->udev, 0),
  658. FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
  659. addr >> 8, 0, buf, transfer, HZ);
  660. if (err < 0) {
  661. kfree(buf);
  662. return err;
  663. }
  664. len -= transfer;
  665. data += transfer;
  666. addr += transfer;
  667. }
  668. kfree(buf);
  669. if (drv_info & AR7010_DEVICE)
  670. firm_offset = AR7010_FIRMWARE_TEXT;
  671. else
  672. firm_offset = AR9271_FIRMWARE_TEXT;
  673. /*
  674. * Issue FW download complete command to firmware.
  675. */
  676. err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
  677. FIRMWARE_DOWNLOAD_COMP,
  678. 0x40 | USB_DIR_OUT,
  679. firm_offset >> 8, 0, NULL, 0, HZ);
  680. if (err)
  681. return -EIO;
  682. dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
  683. hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
  684. return 0;
  685. }
  686. static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
  687. {
  688. int ret, idx;
  689. struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
  690. struct usb_endpoint_descriptor *endp;
  691. /* Request firmware */
  692. ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
  693. &hif_dev->udev->dev);
  694. if (ret) {
  695. dev_err(&hif_dev->udev->dev,
  696. "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
  697. goto err_fw_req;
  698. }
  699. /* Download firmware */
  700. ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
  701. if (ret) {
  702. dev_err(&hif_dev->udev->dev,
  703. "ath9k_htc: Firmware - %s download failed\n",
  704. hif_dev->fw_name);
  705. goto err_fw_download;
  706. }
  707. /* On downloading the firmware to the target, the USB descriptor of EP4
  708. * is 'patched' to change the type of the endpoint to Bulk. This will
  709. * bring down CPU usage during the scan period.
  710. */
  711. for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
  712. endp = &alt->endpoint[idx].desc;
  713. if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  714. == USB_ENDPOINT_XFER_INT) {
  715. endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
  716. endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
  717. endp->bInterval = 0;
  718. }
  719. }
  720. /* Alloc URBs */
  721. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  722. if (ret) {
  723. dev_err(&hif_dev->udev->dev,
  724. "ath9k_htc: Unable to allocate URBs\n");
  725. goto err_urb;
  726. }
  727. return 0;
  728. err_fw_download:
  729. ath9k_hif_usb_dealloc_urbs(hif_dev);
  730. err_urb:
  731. release_firmware(hif_dev->firmware);
  732. err_fw_req:
  733. hif_dev->firmware = NULL;
  734. return ret;
  735. }
  736. static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
  737. {
  738. ath9k_hif_usb_dealloc_urbs(hif_dev);
  739. if (hif_dev->firmware)
  740. release_firmware(hif_dev->firmware);
  741. }
  742. static int ath9k_hif_usb_probe(struct usb_interface *interface,
  743. const struct usb_device_id *id)
  744. {
  745. struct usb_device *udev = interface_to_usbdev(interface);
  746. struct hif_device_usb *hif_dev;
  747. int ret = 0;
  748. hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
  749. if (!hif_dev) {
  750. ret = -ENOMEM;
  751. goto err_alloc;
  752. }
  753. usb_get_dev(udev);
  754. hif_dev->udev = udev;
  755. hif_dev->interface = interface;
  756. hif_dev->device_id = id->idProduct;
  757. #ifdef CONFIG_PM
  758. udev->reset_resume = 1;
  759. #endif
  760. usb_set_intfdata(interface, hif_dev);
  761. hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
  762. &hif_dev->udev->dev);
  763. if (hif_dev->htc_handle == NULL) {
  764. ret = -ENOMEM;
  765. goto err_htc_hw_alloc;
  766. }
  767. /* Find out which firmware to load */
  768. if (id->driver_info & AR7010_DEVICE)
  769. if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
  770. hif_dev->fw_name = FIRMWARE_AR7010_1_1;
  771. else
  772. hif_dev->fw_name = FIRMWARE_AR7010;
  773. else
  774. hif_dev->fw_name = FIRMWARE_AR9271;
  775. ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
  776. if (ret) {
  777. ret = -EINVAL;
  778. goto err_hif_init_usb;
  779. }
  780. ret = ath9k_htc_hw_init(hif_dev->htc_handle,
  781. &hif_dev->udev->dev, hif_dev->device_id,
  782. hif_dev->udev->product, id->driver_info);
  783. if (ret) {
  784. ret = -EINVAL;
  785. goto err_htc_hw_init;
  786. }
  787. dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
  788. return 0;
  789. err_htc_hw_init:
  790. ath9k_hif_usb_dev_deinit(hif_dev);
  791. err_hif_init_usb:
  792. ath9k_htc_hw_free(hif_dev->htc_handle);
  793. err_htc_hw_alloc:
  794. usb_set_intfdata(interface, NULL);
  795. kfree(hif_dev);
  796. usb_put_dev(udev);
  797. err_alloc:
  798. return ret;
  799. }
  800. static void ath9k_hif_usb_reboot(struct usb_device *udev)
  801. {
  802. u32 reboot_cmd = 0xffffffff;
  803. void *buf;
  804. int ret;
  805. buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
  806. if (!buf)
  807. return;
  808. ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
  809. buf, 4, NULL, HZ);
  810. if (ret)
  811. dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
  812. kfree(buf);
  813. }
  814. static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
  815. {
  816. struct usb_device *udev = interface_to_usbdev(interface);
  817. struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
  818. if (hif_dev) {
  819. ath9k_htc_hw_deinit(hif_dev->htc_handle,
  820. (udev->state == USB_STATE_NOTATTACHED) ? true : false);
  821. ath9k_htc_hw_free(hif_dev->htc_handle);
  822. ath9k_hif_usb_dev_deinit(hif_dev);
  823. usb_set_intfdata(interface, NULL);
  824. }
  825. if (hif_dev->flags & HIF_USB_START)
  826. ath9k_hif_usb_reboot(udev);
  827. kfree(hif_dev);
  828. dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
  829. usb_put_dev(udev);
  830. }
  831. #ifdef CONFIG_PM
  832. static int ath9k_hif_usb_suspend(struct usb_interface *interface,
  833. pm_message_t message)
  834. {
  835. struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
  836. ath9k_hif_usb_dealloc_urbs(hif_dev);
  837. return 0;
  838. }
  839. static int ath9k_hif_usb_resume(struct usb_interface *interface)
  840. {
  841. struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
  842. struct htc_target *htc_handle = hif_dev->htc_handle;
  843. int ret;
  844. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  845. if (ret)
  846. return ret;
  847. if (hif_dev->firmware) {
  848. ret = ath9k_hif_usb_download_fw(hif_dev,
  849. htc_handle->drv_priv->ah->common.driver_info);
  850. if (ret)
  851. goto fail_resume;
  852. } else {
  853. ath9k_hif_usb_dealloc_urbs(hif_dev);
  854. return -EIO;
  855. }
  856. mdelay(100);
  857. ret = ath9k_htc_resume(htc_handle);
  858. if (ret)
  859. goto fail_resume;
  860. return 0;
  861. fail_resume:
  862. ath9k_hif_usb_dealloc_urbs(hif_dev);
  863. return ret;
  864. }
  865. #endif
  866. static struct usb_driver ath9k_hif_usb_driver = {
  867. .name = "ath9k_hif_usb",
  868. .probe = ath9k_hif_usb_probe,
  869. .disconnect = ath9k_hif_usb_disconnect,
  870. #ifdef CONFIG_PM
  871. .suspend = ath9k_hif_usb_suspend,
  872. .resume = ath9k_hif_usb_resume,
  873. .reset_resume = ath9k_hif_usb_resume,
  874. #endif
  875. .id_table = ath9k_hif_usb_ids,
  876. .soft_unbind = 1,
  877. };
  878. int ath9k_hif_usb_init(void)
  879. {
  880. return usb_register(&ath9k_hif_usb_driver);
  881. }
  882. void ath9k_hif_usb_exit(void)
  883. {
  884. usb_deregister(&ath9k_hif_usb_driver);
  885. }