hif_usb.c 25 KB

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