hif_usb.c 27 KB

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