hif_usb.c 25 KB

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