usb.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. /*
  2. * Copyright (c) 2011 Broadcom Corporation
  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 ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/firmware.h>
  19. #include <linux/usb.h>
  20. #include <linux/vmalloc.h>
  21. #include <brcmu_utils.h>
  22. #include <brcmu_wifi.h>
  23. #include <dhd_bus.h>
  24. #include <dhd_dbg.h>
  25. #include "usb_rdl.h"
  26. #include "usb.h"
  27. #define IOCTL_RESP_TIMEOUT 2000
  28. #define BRCMF_USB_RESET_GETVER_SPINWAIT 100 /* in unit of ms */
  29. #define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
  30. #define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle
  31. has boot up */
  32. #define BRCMF_USB_NRXQ 50
  33. #define BRCMF_USB_NTXQ 50
  34. #define CONFIGDESC(usb) (&((usb)->actconfig)->desc)
  35. #define IFPTR(usb, idx) ((usb)->actconfig->interface[(idx)])
  36. #define IFALTS(usb, idx) (IFPTR((usb), (idx))->altsetting[0])
  37. #define IFDESC(usb, idx) IFALTS((usb), (idx)).desc
  38. #define IFEPDESC(usb, idx, ep) (IFALTS((usb), (idx)).endpoint[(ep)]).desc
  39. #define CONTROL_IF 0
  40. #define BULK_IF 0
  41. #define BRCMF_USB_CBCTL_WRITE 0
  42. #define BRCMF_USB_CBCTL_READ 1
  43. #define BRCMF_USB_MAX_PKT_SIZE 1600
  44. #define BRCMF_USB_43143_FW_NAME "brcm/brcmfmac43143.bin"
  45. #define BRCMF_USB_43236_FW_NAME "brcm/brcmfmac43236b.bin"
  46. #define BRCMF_USB_43242_FW_NAME "brcm/brcmfmac43242a.bin"
  47. struct brcmf_usb_image {
  48. struct list_head list;
  49. s8 *fwname;
  50. u8 *image;
  51. int image_len;
  52. };
  53. static struct list_head fw_image_list;
  54. struct intr_transfer_buf {
  55. u32 notification;
  56. u32 reserved;
  57. };
  58. struct brcmf_usbdev_info {
  59. struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
  60. spinlock_t qlock;
  61. struct list_head rx_freeq;
  62. struct list_head rx_postq;
  63. struct list_head tx_freeq;
  64. struct list_head tx_postq;
  65. uint rx_pipe, tx_pipe, intr_pipe, rx_pipe2;
  66. int rx_low_watermark;
  67. int tx_low_watermark;
  68. int tx_high_watermark;
  69. int tx_freecount;
  70. bool tx_flowblock;
  71. struct brcmf_usbreq *tx_reqs;
  72. struct brcmf_usbreq *rx_reqs;
  73. u8 *image; /* buffer for combine fw and nvram */
  74. int image_len;
  75. struct usb_device *usbdev;
  76. struct device *dev;
  77. int ctl_in_pipe, ctl_out_pipe;
  78. struct urb *ctl_urb; /* URB for control endpoint */
  79. struct usb_ctrlrequest ctl_write;
  80. struct usb_ctrlrequest ctl_read;
  81. u32 ctl_urb_actual_length;
  82. int ctl_urb_status;
  83. int ctl_completed;
  84. wait_queue_head_t ioctl_resp_wait;
  85. ulong ctl_op;
  86. struct urb *bulk_urb; /* used for FW download */
  87. struct urb *intr_urb; /* URB for interrupt endpoint */
  88. int intr_size; /* Size of interrupt message */
  89. int interval; /* Interrupt polling interval */
  90. struct intr_transfer_buf intr; /* Data buffer for interrupt endpoint */
  91. };
  92. static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
  93. struct brcmf_usbreq *req);
  94. MODULE_AUTHOR("Broadcom Corporation");
  95. MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac usb driver.");
  96. MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac usb cards");
  97. MODULE_LICENSE("Dual BSD/GPL");
  98. static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
  99. {
  100. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  101. return bus_if->bus_priv.usb;
  102. }
  103. static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
  104. {
  105. return brcmf_usb_get_buspub(dev)->devinfo;
  106. }
  107. static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
  108. {
  109. return wait_event_timeout(devinfo->ioctl_resp_wait,
  110. devinfo->ctl_completed,
  111. msecs_to_jiffies(IOCTL_RESP_TIMEOUT));
  112. }
  113. static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
  114. {
  115. if (waitqueue_active(&devinfo->ioctl_resp_wait))
  116. wake_up(&devinfo->ioctl_resp_wait);
  117. }
  118. static void
  119. brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
  120. {
  121. brcmf_dbg(USB, "Enter, status=%d\n", status);
  122. if (unlikely(devinfo == NULL))
  123. return;
  124. if (type == BRCMF_USB_CBCTL_READ) {
  125. if (status == 0)
  126. devinfo->bus_pub.stats.rx_ctlpkts++;
  127. else
  128. devinfo->bus_pub.stats.rx_ctlerrs++;
  129. } else if (type == BRCMF_USB_CBCTL_WRITE) {
  130. if (status == 0)
  131. devinfo->bus_pub.stats.tx_ctlpkts++;
  132. else
  133. devinfo->bus_pub.stats.tx_ctlerrs++;
  134. }
  135. devinfo->ctl_urb_status = status;
  136. devinfo->ctl_completed = true;
  137. brcmf_usb_ioctl_resp_wake(devinfo);
  138. }
  139. static void
  140. brcmf_usb_ctlread_complete(struct urb *urb)
  141. {
  142. struct brcmf_usbdev_info *devinfo =
  143. (struct brcmf_usbdev_info *)urb->context;
  144. brcmf_dbg(USB, "Enter\n");
  145. devinfo->ctl_urb_actual_length = urb->actual_length;
  146. brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
  147. urb->status);
  148. }
  149. static void
  150. brcmf_usb_ctlwrite_complete(struct urb *urb)
  151. {
  152. struct brcmf_usbdev_info *devinfo =
  153. (struct brcmf_usbdev_info *)urb->context;
  154. brcmf_dbg(USB, "Enter\n");
  155. brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
  156. urb->status);
  157. }
  158. static int
  159. brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
  160. {
  161. int ret;
  162. u16 size;
  163. brcmf_dbg(USB, "Enter\n");
  164. if (devinfo == NULL || buf == NULL ||
  165. len == 0 || devinfo->ctl_urb == NULL)
  166. return -EINVAL;
  167. size = len;
  168. devinfo->ctl_write.wLength = cpu_to_le16p(&size);
  169. devinfo->ctl_urb->transfer_buffer_length = size;
  170. devinfo->ctl_urb_status = 0;
  171. devinfo->ctl_urb_actual_length = 0;
  172. usb_fill_control_urb(devinfo->ctl_urb,
  173. devinfo->usbdev,
  174. devinfo->ctl_out_pipe,
  175. (unsigned char *) &devinfo->ctl_write,
  176. buf, size,
  177. (usb_complete_t)brcmf_usb_ctlwrite_complete,
  178. devinfo);
  179. ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
  180. if (ret < 0)
  181. brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
  182. return ret;
  183. }
  184. static int
  185. brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
  186. {
  187. int ret;
  188. u16 size;
  189. brcmf_dbg(USB, "Enter\n");
  190. if ((devinfo == NULL) || (buf == NULL) || (len == 0)
  191. || (devinfo->ctl_urb == NULL))
  192. return -EINVAL;
  193. size = len;
  194. devinfo->ctl_read.wLength = cpu_to_le16p(&size);
  195. devinfo->ctl_urb->transfer_buffer_length = size;
  196. devinfo->ctl_read.bRequestType = USB_DIR_IN
  197. | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  198. devinfo->ctl_read.bRequest = 1;
  199. usb_fill_control_urb(devinfo->ctl_urb,
  200. devinfo->usbdev,
  201. devinfo->ctl_in_pipe,
  202. (unsigned char *) &devinfo->ctl_read,
  203. buf, size,
  204. (usb_complete_t)brcmf_usb_ctlread_complete,
  205. devinfo);
  206. ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
  207. if (ret < 0)
  208. brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
  209. return ret;
  210. }
  211. static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
  212. {
  213. int err = 0;
  214. int timeout = 0;
  215. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  216. brcmf_dbg(USB, "Enter\n");
  217. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
  218. return -EIO;
  219. if (test_and_set_bit(0, &devinfo->ctl_op))
  220. return -EIO;
  221. devinfo->ctl_completed = false;
  222. err = brcmf_usb_send_ctl(devinfo, buf, len);
  223. if (err) {
  224. brcmf_dbg(ERROR, "fail %d bytes: %d\n", err, len);
  225. clear_bit(0, &devinfo->ctl_op);
  226. return err;
  227. }
  228. timeout = brcmf_usb_ioctl_resp_wait(devinfo);
  229. clear_bit(0, &devinfo->ctl_op);
  230. if (!timeout) {
  231. brcmf_dbg(ERROR, "Txctl wait timed out\n");
  232. err = -EIO;
  233. }
  234. return err;
  235. }
  236. static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
  237. {
  238. int err = 0;
  239. int timeout = 0;
  240. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  241. brcmf_dbg(USB, "Enter\n");
  242. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
  243. return -EIO;
  244. if (test_and_set_bit(0, &devinfo->ctl_op))
  245. return -EIO;
  246. devinfo->ctl_completed = false;
  247. err = brcmf_usb_recv_ctl(devinfo, buf, len);
  248. if (err) {
  249. brcmf_dbg(ERROR, "fail %d bytes: %d\n", err, len);
  250. clear_bit(0, &devinfo->ctl_op);
  251. return err;
  252. }
  253. timeout = brcmf_usb_ioctl_resp_wait(devinfo);
  254. err = devinfo->ctl_urb_status;
  255. clear_bit(0, &devinfo->ctl_op);
  256. if (!timeout) {
  257. brcmf_dbg(ERROR, "rxctl wait timed out\n");
  258. err = -EIO;
  259. }
  260. if (!err)
  261. return devinfo->ctl_urb_actual_length;
  262. else
  263. return err;
  264. }
  265. static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
  266. struct list_head *q, int *counter)
  267. {
  268. unsigned long flags;
  269. struct brcmf_usbreq *req;
  270. spin_lock_irqsave(&devinfo->qlock, flags);
  271. if (list_empty(q)) {
  272. spin_unlock_irqrestore(&devinfo->qlock, flags);
  273. return NULL;
  274. }
  275. req = list_entry(q->next, struct brcmf_usbreq, list);
  276. list_del_init(q->next);
  277. if (counter)
  278. (*counter)--;
  279. spin_unlock_irqrestore(&devinfo->qlock, flags);
  280. return req;
  281. }
  282. static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
  283. struct list_head *q, struct brcmf_usbreq *req,
  284. int *counter)
  285. {
  286. unsigned long flags;
  287. spin_lock_irqsave(&devinfo->qlock, flags);
  288. list_add_tail(&req->list, q);
  289. if (counter)
  290. (*counter)++;
  291. spin_unlock_irqrestore(&devinfo->qlock, flags);
  292. }
  293. static struct brcmf_usbreq *
  294. brcmf_usbdev_qinit(struct list_head *q, int qsize)
  295. {
  296. int i;
  297. struct brcmf_usbreq *req, *reqs;
  298. reqs = kzalloc(sizeof(struct brcmf_usbreq) * qsize, GFP_ATOMIC);
  299. if (reqs == NULL) {
  300. brcmf_dbg(ERROR, "fail to allocate memory!\n");
  301. return NULL;
  302. }
  303. req = reqs;
  304. for (i = 0; i < qsize; i++) {
  305. req->urb = usb_alloc_urb(0, GFP_ATOMIC);
  306. if (!req->urb)
  307. goto fail;
  308. INIT_LIST_HEAD(&req->list);
  309. list_add_tail(&req->list, q);
  310. req++;
  311. }
  312. return reqs;
  313. fail:
  314. brcmf_dbg(ERROR, "fail!\n");
  315. while (!list_empty(q)) {
  316. req = list_entry(q->next, struct brcmf_usbreq, list);
  317. if (req && req->urb)
  318. usb_free_urb(req->urb);
  319. list_del(q->next);
  320. }
  321. return NULL;
  322. }
  323. static void brcmf_usb_free_q(struct list_head *q, bool pending)
  324. {
  325. struct brcmf_usbreq *req, *next;
  326. int i = 0;
  327. list_for_each_entry_safe(req, next, q, list) {
  328. if (!req->urb) {
  329. brcmf_dbg(ERROR, "bad req\n");
  330. break;
  331. }
  332. i++;
  333. if (pending) {
  334. usb_kill_urb(req->urb);
  335. } else {
  336. usb_free_urb(req->urb);
  337. list_del_init(&req->list);
  338. }
  339. }
  340. }
  341. static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
  342. struct brcmf_usbreq *req)
  343. {
  344. unsigned long flags;
  345. spin_lock_irqsave(&devinfo->qlock, flags);
  346. list_del_init(&req->list);
  347. spin_unlock_irqrestore(&devinfo->qlock, flags);
  348. }
  349. static void brcmf_usb_tx_complete(struct urb *urb)
  350. {
  351. struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
  352. struct brcmf_usbdev_info *devinfo = req->devinfo;
  353. brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
  354. req->skb);
  355. brcmf_usb_del_fromq(devinfo, req);
  356. if (urb->status == 0)
  357. devinfo->bus_pub.bus->dstats.tx_packets++;
  358. else
  359. devinfo->bus_pub.bus->dstats.tx_errors++;
  360. brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
  361. brcmu_pkt_buf_free_skb(req->skb);
  362. req->skb = NULL;
  363. brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
  364. if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
  365. devinfo->tx_flowblock) {
  366. brcmf_txflowblock(devinfo->dev, false);
  367. devinfo->tx_flowblock = false;
  368. }
  369. }
  370. static void brcmf_usb_rx_complete(struct urb *urb)
  371. {
  372. struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
  373. struct brcmf_usbdev_info *devinfo = req->devinfo;
  374. struct sk_buff *skb;
  375. int ifidx = 0;
  376. brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
  377. brcmf_usb_del_fromq(devinfo, req);
  378. skb = req->skb;
  379. req->skb = NULL;
  380. if (urb->status == 0) {
  381. devinfo->bus_pub.bus->dstats.rx_packets++;
  382. } else {
  383. devinfo->bus_pub.bus->dstats.rx_errors++;
  384. brcmu_pkt_buf_free_skb(skb);
  385. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  386. return;
  387. }
  388. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
  389. skb_put(skb, urb->actual_length);
  390. if (brcmf_proto_hdrpull(devinfo->dev, &ifidx, skb) != 0) {
  391. brcmf_dbg(ERROR, "rx protocol error\n");
  392. brcmu_pkt_buf_free_skb(skb);
  393. devinfo->bus_pub.bus->dstats.rx_errors++;
  394. } else
  395. brcmf_rx_packet(devinfo->dev, ifidx, skb);
  396. brcmf_usb_rx_refill(devinfo, req);
  397. } else {
  398. brcmu_pkt_buf_free_skb(skb);
  399. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  400. }
  401. return;
  402. }
  403. static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
  404. struct brcmf_usbreq *req)
  405. {
  406. struct sk_buff *skb;
  407. int ret;
  408. if (!req || !devinfo)
  409. return;
  410. skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
  411. if (!skb) {
  412. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  413. return;
  414. }
  415. req->skb = skb;
  416. usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
  417. skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
  418. req);
  419. req->devinfo = devinfo;
  420. brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
  421. ret = usb_submit_urb(req->urb, GFP_ATOMIC);
  422. if (ret) {
  423. brcmf_usb_del_fromq(devinfo, req);
  424. brcmu_pkt_buf_free_skb(req->skb);
  425. req->skb = NULL;
  426. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  427. }
  428. return;
  429. }
  430. static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
  431. {
  432. struct brcmf_usbreq *req;
  433. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
  434. brcmf_dbg(ERROR, "bus is not up=%d\n", devinfo->bus_pub.state);
  435. return;
  436. }
  437. while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
  438. brcmf_usb_rx_refill(devinfo, req);
  439. }
  440. static void
  441. brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
  442. {
  443. struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
  444. int old_state;
  445. brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
  446. devinfo->bus_pub.state, state);
  447. if (devinfo->bus_pub.state == state)
  448. return;
  449. old_state = devinfo->bus_pub.state;
  450. devinfo->bus_pub.state = state;
  451. /* update state of upper layer */
  452. if (state == BRCMFMAC_USB_STATE_DOWN) {
  453. brcmf_dbg(USB, "DBUS is down\n");
  454. bcmf_bus->state = BRCMF_BUS_DOWN;
  455. } else if (state == BRCMFMAC_USB_STATE_UP) {
  456. brcmf_dbg(USB, "DBUS is up\n");
  457. bcmf_bus->state = BRCMF_BUS_DATA;
  458. } else {
  459. brcmf_dbg(USB, "DBUS current state=%d\n", state);
  460. }
  461. }
  462. static void
  463. brcmf_usb_intr_complete(struct urb *urb)
  464. {
  465. struct brcmf_usbdev_info *devinfo =
  466. (struct brcmf_usbdev_info *)urb->context;
  467. int err;
  468. brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
  469. if (devinfo == NULL)
  470. return;
  471. if (unlikely(urb->status)) {
  472. if (urb->status == -ENOENT ||
  473. urb->status == -ESHUTDOWN ||
  474. urb->status == -ENODEV) {
  475. brcmf_usb_state_change(devinfo,
  476. BRCMFMAC_USB_STATE_DOWN);
  477. }
  478. }
  479. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN) {
  480. brcmf_dbg(ERROR, "intr cb when DBUS down, ignoring\n");
  481. return;
  482. }
  483. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
  484. err = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
  485. if (err)
  486. brcmf_dbg(ERROR, "usb_submit_urb, err=%d\n", err);
  487. }
  488. }
  489. static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
  490. {
  491. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  492. struct brcmf_usbreq *req;
  493. int ret;
  494. brcmf_dbg(USB, "Enter, skb=%p\n", skb);
  495. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
  496. return -EIO;
  497. req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
  498. &devinfo->tx_freecount);
  499. if (!req) {
  500. brcmu_pkt_buf_free_skb(skb);
  501. brcmf_dbg(ERROR, "no req to send\n");
  502. return -ENOMEM;
  503. }
  504. req->skb = skb;
  505. req->devinfo = devinfo;
  506. usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
  507. skb->data, skb->len, brcmf_usb_tx_complete, req);
  508. req->urb->transfer_flags |= URB_ZERO_PACKET;
  509. brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
  510. ret = usb_submit_urb(req->urb, GFP_ATOMIC);
  511. if (ret) {
  512. brcmf_dbg(ERROR, "brcmf_usb_tx usb_submit_urb FAILED\n");
  513. brcmf_usb_del_fromq(devinfo, req);
  514. brcmu_pkt_buf_free_skb(req->skb);
  515. req->skb = NULL;
  516. brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
  517. &devinfo->tx_freecount);
  518. } else {
  519. if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
  520. !devinfo->tx_flowblock) {
  521. brcmf_txflowblock(dev, true);
  522. devinfo->tx_flowblock = true;
  523. }
  524. }
  525. return ret;
  526. }
  527. static int brcmf_usb_up(struct device *dev)
  528. {
  529. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  530. u16 ifnum;
  531. int ret;
  532. brcmf_dbg(USB, "Enter\n");
  533. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
  534. return 0;
  535. /* Success, indicate devinfo is fully up */
  536. brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
  537. if (devinfo->intr_urb) {
  538. usb_fill_int_urb(devinfo->intr_urb, devinfo->usbdev,
  539. devinfo->intr_pipe,
  540. &devinfo->intr,
  541. devinfo->intr_size,
  542. (usb_complete_t)brcmf_usb_intr_complete,
  543. devinfo,
  544. devinfo->interval);
  545. ret = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
  546. if (ret) {
  547. brcmf_dbg(ERROR, "USB_SUBMIT_URB failed with status %d\n",
  548. ret);
  549. return -EINVAL;
  550. }
  551. }
  552. if (devinfo->ctl_urb) {
  553. devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
  554. devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
  555. ifnum = IFDESC(devinfo->usbdev, CONTROL_IF).bInterfaceNumber;
  556. /* CTL Write */
  557. devinfo->ctl_write.bRequestType =
  558. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  559. devinfo->ctl_write.bRequest = 0;
  560. devinfo->ctl_write.wValue = cpu_to_le16(0);
  561. devinfo->ctl_write.wIndex = cpu_to_le16p(&ifnum);
  562. /* CTL Read */
  563. devinfo->ctl_read.bRequestType =
  564. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  565. devinfo->ctl_read.bRequest = 1;
  566. devinfo->ctl_read.wValue = cpu_to_le16(0);
  567. devinfo->ctl_read.wIndex = cpu_to_le16p(&ifnum);
  568. }
  569. brcmf_usb_rx_fill_all(devinfo);
  570. return 0;
  571. }
  572. static void brcmf_usb_down(struct device *dev)
  573. {
  574. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  575. brcmf_dbg(USB, "Enter\n");
  576. if (devinfo == NULL)
  577. return;
  578. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
  579. return;
  580. brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
  581. if (devinfo->intr_urb)
  582. usb_kill_urb(devinfo->intr_urb);
  583. if (devinfo->ctl_urb)
  584. usb_kill_urb(devinfo->ctl_urb);
  585. if (devinfo->bulk_urb)
  586. usb_kill_urb(devinfo->bulk_urb);
  587. brcmf_usb_free_q(&devinfo->tx_postq, true);
  588. brcmf_usb_free_q(&devinfo->rx_postq, true);
  589. }
  590. static void
  591. brcmf_usb_sync_complete(struct urb *urb)
  592. {
  593. struct brcmf_usbdev_info *devinfo =
  594. (struct brcmf_usbdev_info *)urb->context;
  595. devinfo->ctl_completed = true;
  596. brcmf_usb_ioctl_resp_wake(devinfo);
  597. }
  598. static bool brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
  599. void *buffer, int buflen)
  600. {
  601. int ret = 0;
  602. char *tmpbuf;
  603. u16 size;
  604. if ((!devinfo) || (devinfo->ctl_urb == NULL))
  605. return false;
  606. tmpbuf = kmalloc(buflen, GFP_ATOMIC);
  607. if (!tmpbuf)
  608. return false;
  609. size = buflen;
  610. devinfo->ctl_urb->transfer_buffer_length = size;
  611. devinfo->ctl_read.wLength = cpu_to_le16p(&size);
  612. devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
  613. USB_RECIP_INTERFACE;
  614. devinfo->ctl_read.bRequest = cmd;
  615. usb_fill_control_urb(devinfo->ctl_urb,
  616. devinfo->usbdev,
  617. usb_rcvctrlpipe(devinfo->usbdev, 0),
  618. (unsigned char *) &devinfo->ctl_read,
  619. (void *) tmpbuf, size,
  620. (usb_complete_t)brcmf_usb_sync_complete, devinfo);
  621. devinfo->ctl_completed = false;
  622. ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
  623. if (ret < 0) {
  624. brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
  625. kfree(tmpbuf);
  626. return false;
  627. }
  628. ret = brcmf_usb_ioctl_resp_wait(devinfo);
  629. memcpy(buffer, tmpbuf, buflen);
  630. kfree(tmpbuf);
  631. return ret;
  632. }
  633. static bool
  634. brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
  635. {
  636. struct bootrom_id_le id;
  637. u32 chipid, chiprev;
  638. brcmf_dbg(USB, "Enter\n");
  639. if (devinfo == NULL)
  640. return false;
  641. /* Check if firmware downloaded already by querying runtime ID */
  642. id.chip = cpu_to_le32(0xDEAD);
  643. brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
  644. chipid = le32_to_cpu(id.chip);
  645. chiprev = le32_to_cpu(id.chiprev);
  646. if ((chipid & 0x4300) == 0x4300)
  647. brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
  648. else
  649. brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
  650. if (chipid == BRCMF_POSTBOOT_ID) {
  651. brcmf_dbg(USB, "firmware already downloaded\n");
  652. brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
  653. return false;
  654. } else {
  655. devinfo->bus_pub.devid = chipid;
  656. devinfo->bus_pub.chiprev = chiprev;
  657. }
  658. return true;
  659. }
  660. static int
  661. brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
  662. {
  663. struct bootrom_id_le id;
  664. u32 loop_cnt;
  665. brcmf_dbg(USB, "Enter\n");
  666. loop_cnt = 0;
  667. do {
  668. mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
  669. loop_cnt++;
  670. id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
  671. brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
  672. if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
  673. break;
  674. } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
  675. if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
  676. brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
  677. le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
  678. brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
  679. return 0;
  680. } else {
  681. brcmf_dbg(ERROR, "Cannot talk to Dongle. Firmware is not UP, %d ms\n",
  682. BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
  683. return -EINVAL;
  684. }
  685. }
  686. static int
  687. brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
  688. {
  689. int ret;
  690. if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
  691. return -EINVAL;
  692. /* Prepare the URB */
  693. usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
  694. devinfo->tx_pipe, buffer, len,
  695. (usb_complete_t)brcmf_usb_sync_complete, devinfo);
  696. devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
  697. devinfo->ctl_completed = false;
  698. ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
  699. if (ret) {
  700. brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
  701. return ret;
  702. }
  703. ret = brcmf_usb_ioctl_resp_wait(devinfo);
  704. return (ret == 0);
  705. }
  706. static int
  707. brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
  708. {
  709. unsigned int sendlen, sent, dllen;
  710. char *bulkchunk = NULL, *dlpos;
  711. struct rdl_state_le state;
  712. u32 rdlstate, rdlbytes;
  713. int err = 0;
  714. brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
  715. bulkchunk = kmalloc(RDL_CHUNK, GFP_ATOMIC);
  716. if (bulkchunk == NULL) {
  717. err = -ENOMEM;
  718. goto fail;
  719. }
  720. /* 1) Prepare USB boot loader for runtime image */
  721. brcmf_usb_dl_cmd(devinfo, DL_START, &state,
  722. sizeof(struct rdl_state_le));
  723. rdlstate = le32_to_cpu(state.state);
  724. rdlbytes = le32_to_cpu(state.bytes);
  725. /* 2) Check we are in the Waiting state */
  726. if (rdlstate != DL_WAITING) {
  727. brcmf_dbg(ERROR, "Failed to DL_START\n");
  728. err = -EINVAL;
  729. goto fail;
  730. }
  731. sent = 0;
  732. dlpos = fw;
  733. dllen = fwlen;
  734. /* Get chip id and rev */
  735. while (rdlbytes != dllen) {
  736. /* Wait until the usb device reports it received all
  737. * the bytes we sent */
  738. if ((rdlbytes == sent) && (rdlbytes != dllen)) {
  739. if ((dllen-sent) < RDL_CHUNK)
  740. sendlen = dllen-sent;
  741. else
  742. sendlen = RDL_CHUNK;
  743. /* simply avoid having to send a ZLP by ensuring we
  744. * never have an even
  745. * multiple of 64
  746. */
  747. if (!(sendlen % 64))
  748. sendlen -= 4;
  749. /* send data */
  750. memcpy(bulkchunk, dlpos, sendlen);
  751. if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
  752. sendlen)) {
  753. brcmf_dbg(ERROR, "send_bulk failed\n");
  754. err = -EINVAL;
  755. goto fail;
  756. }
  757. dlpos += sendlen;
  758. sent += sendlen;
  759. }
  760. if (!brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
  761. sizeof(struct rdl_state_le))) {
  762. brcmf_dbg(ERROR, "DL_GETSTATE Failed xxxx\n");
  763. err = -EINVAL;
  764. goto fail;
  765. }
  766. rdlstate = le32_to_cpu(state.state);
  767. rdlbytes = le32_to_cpu(state.bytes);
  768. /* restart if an error is reported */
  769. if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
  770. brcmf_dbg(ERROR, "Bad Hdr or Bad CRC state %d\n",
  771. rdlstate);
  772. err = -EINVAL;
  773. goto fail;
  774. }
  775. }
  776. fail:
  777. kfree(bulkchunk);
  778. brcmf_dbg(USB, "Exit, err=%d\n", err);
  779. return err;
  780. }
  781. static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
  782. {
  783. int err;
  784. brcmf_dbg(USB, "Enter\n");
  785. if (devinfo == NULL)
  786. return -EINVAL;
  787. if (devinfo->bus_pub.devid == 0xDEAD)
  788. return -EINVAL;
  789. err = brcmf_usb_dl_writeimage(devinfo, fw, len);
  790. if (err == 0)
  791. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
  792. else
  793. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
  794. brcmf_dbg(USB, "Exit, err=%d\n", err);
  795. return err;
  796. }
  797. static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
  798. {
  799. struct rdl_state_le state;
  800. brcmf_dbg(USB, "Enter\n");
  801. if (!devinfo)
  802. return -EINVAL;
  803. if (devinfo->bus_pub.devid == 0xDEAD)
  804. return -EINVAL;
  805. /* Check we are runnable */
  806. brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
  807. sizeof(struct rdl_state_le));
  808. /* Start the image */
  809. if (state.state == cpu_to_le32(DL_RUNNABLE)) {
  810. if (!brcmf_usb_dl_cmd(devinfo, DL_GO, &state,
  811. sizeof(struct rdl_state_le)))
  812. return -ENODEV;
  813. if (brcmf_usb_resetcfg(devinfo))
  814. return -ENODEV;
  815. /* The Dongle may go for re-enumeration. */
  816. } else {
  817. brcmf_dbg(ERROR, "Dongle not runnable\n");
  818. return -EINVAL;
  819. }
  820. brcmf_dbg(USB, "Exit\n");
  821. return 0;
  822. }
  823. static bool brcmf_usb_chip_support(int chipid, int chiprev)
  824. {
  825. switch(chipid) {
  826. case 43143:
  827. return true;
  828. case 43235:
  829. case 43236:
  830. case 43238:
  831. return (chiprev == 3);
  832. case 43242:
  833. return true;
  834. default:
  835. break;
  836. }
  837. return false;
  838. }
  839. static int
  840. brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
  841. {
  842. int devid, chiprev;
  843. int err;
  844. brcmf_dbg(USB, "Enter\n");
  845. if (devinfo == NULL)
  846. return -ENODEV;
  847. devid = devinfo->bus_pub.devid;
  848. chiprev = devinfo->bus_pub.chiprev;
  849. if (!brcmf_usb_chip_support(devid, chiprev)) {
  850. brcmf_dbg(ERROR, "unsupported chip %d rev %d\n",
  851. devid, chiprev);
  852. return -EINVAL;
  853. }
  854. if (!devinfo->image) {
  855. brcmf_dbg(ERROR, "No firmware!\n");
  856. return -ENOENT;
  857. }
  858. err = brcmf_usb_dlstart(devinfo,
  859. devinfo->image, devinfo->image_len);
  860. if (err == 0)
  861. err = brcmf_usb_dlrun(devinfo);
  862. return err;
  863. }
  864. static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
  865. {
  866. brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
  867. /* free the URBS */
  868. brcmf_usb_free_q(&devinfo->rx_freeq, false);
  869. brcmf_usb_free_q(&devinfo->tx_freeq, false);
  870. usb_free_urb(devinfo->intr_urb);
  871. usb_free_urb(devinfo->ctl_urb);
  872. usb_free_urb(devinfo->bulk_urb);
  873. kfree(devinfo->tx_reqs);
  874. kfree(devinfo->rx_reqs);
  875. }
  876. #define TRX_MAGIC 0x30524448 /* "HDR0" */
  877. #define TRX_VERSION 1 /* Version 1 */
  878. #define TRX_MAX_LEN 0x3B0000 /* Max length */
  879. #define TRX_NO_HEADER 1 /* Do not write TRX header */
  880. #define TRX_MAX_OFFSET 3 /* Max number of individual files */
  881. #define TRX_UNCOMP_IMAGE 0x20 /* Trx contains uncompressed image */
  882. struct trx_header_le {
  883. __le32 magic; /* "HDR0" */
  884. __le32 len; /* Length of file including header */
  885. __le32 crc32; /* CRC from flag_version to end of file */
  886. __le32 flag_version; /* 0:15 flags, 16:31 version */
  887. __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
  888. * header */
  889. };
  890. static int check_file(const u8 *headers)
  891. {
  892. struct trx_header_le *trx;
  893. int actual_len = -1;
  894. brcmf_dbg(USB, "Enter\n");
  895. /* Extract trx header */
  896. trx = (struct trx_header_le *) headers;
  897. if (trx->magic != cpu_to_le32(TRX_MAGIC))
  898. return -1;
  899. headers += sizeof(struct trx_header_le);
  900. if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
  901. actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
  902. return actual_len + sizeof(struct trx_header_le);
  903. }
  904. return -1;
  905. }
  906. static int brcmf_usb_get_fw(struct brcmf_usbdev_info *devinfo)
  907. {
  908. s8 *fwname;
  909. const struct firmware *fw;
  910. struct brcmf_usb_image *fw_image;
  911. int err;
  912. brcmf_dbg(USB, "Enter\n");
  913. switch (devinfo->bus_pub.devid) {
  914. case 43143:
  915. fwname = BRCMF_USB_43143_FW_NAME;
  916. break;
  917. case 43235:
  918. case 43236:
  919. case 43238:
  920. fwname = BRCMF_USB_43236_FW_NAME;
  921. break;
  922. case 43242:
  923. fwname = BRCMF_USB_43242_FW_NAME;
  924. break;
  925. default:
  926. return -EINVAL;
  927. break;
  928. }
  929. brcmf_dbg(USB, "Loading FW %s\n", fwname);
  930. list_for_each_entry(fw_image, &fw_image_list, list) {
  931. if (fw_image->fwname == fwname) {
  932. devinfo->image = fw_image->image;
  933. devinfo->image_len = fw_image->image_len;
  934. return 0;
  935. }
  936. }
  937. /* fw image not yet loaded. Load it now and add to list */
  938. err = request_firmware(&fw, fwname, devinfo->dev);
  939. if (!fw) {
  940. brcmf_dbg(ERROR, "fail to request firmware %s\n", fwname);
  941. return err;
  942. }
  943. if (check_file(fw->data) < 0) {
  944. brcmf_dbg(ERROR, "invalid firmware %s\n", fwname);
  945. return -EINVAL;
  946. }
  947. fw_image = kzalloc(sizeof(*fw_image), GFP_ATOMIC);
  948. if (!fw_image)
  949. return -ENOMEM;
  950. INIT_LIST_HEAD(&fw_image->list);
  951. list_add_tail(&fw_image->list, &fw_image_list);
  952. fw_image->fwname = fwname;
  953. fw_image->image = vmalloc(fw->size);
  954. if (!fw_image->image)
  955. return -ENOMEM;
  956. memcpy(fw_image->image, fw->data, fw->size);
  957. fw_image->image_len = fw->size;
  958. release_firmware(fw);
  959. devinfo->image = fw_image->image;
  960. devinfo->image_len = fw_image->image_len;
  961. return 0;
  962. }
  963. static
  964. struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
  965. int nrxq, int ntxq)
  966. {
  967. brcmf_dbg(USB, "Enter\n");
  968. devinfo->bus_pub.nrxq = nrxq;
  969. devinfo->rx_low_watermark = nrxq / 2;
  970. devinfo->bus_pub.devinfo = devinfo;
  971. devinfo->bus_pub.ntxq = ntxq;
  972. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
  973. /* flow control when too many tx urbs posted */
  974. devinfo->tx_low_watermark = ntxq / 4;
  975. devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
  976. devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
  977. /* Initialize other structure content */
  978. init_waitqueue_head(&devinfo->ioctl_resp_wait);
  979. /* Initialize the spinlocks */
  980. spin_lock_init(&devinfo->qlock);
  981. INIT_LIST_HEAD(&devinfo->rx_freeq);
  982. INIT_LIST_HEAD(&devinfo->rx_postq);
  983. INIT_LIST_HEAD(&devinfo->tx_freeq);
  984. INIT_LIST_HEAD(&devinfo->tx_postq);
  985. devinfo->tx_flowblock = false;
  986. devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
  987. if (!devinfo->rx_reqs)
  988. goto error;
  989. devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
  990. if (!devinfo->tx_reqs)
  991. goto error;
  992. devinfo->tx_freecount = ntxq;
  993. devinfo->intr_urb = usb_alloc_urb(0, GFP_ATOMIC);
  994. if (!devinfo->intr_urb) {
  995. brcmf_dbg(ERROR, "usb_alloc_urb (intr) failed\n");
  996. goto error;
  997. }
  998. devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
  999. if (!devinfo->ctl_urb) {
  1000. brcmf_dbg(ERROR, "usb_alloc_urb (ctl) failed\n");
  1001. goto error;
  1002. }
  1003. devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
  1004. if (!devinfo->bulk_urb) {
  1005. brcmf_dbg(ERROR, "usb_alloc_urb (bulk) failed\n");
  1006. goto error;
  1007. }
  1008. if (!brcmf_usb_dlneeded(devinfo))
  1009. return &devinfo->bus_pub;
  1010. brcmf_dbg(USB, "Start fw downloading\n");
  1011. if (brcmf_usb_get_fw(devinfo))
  1012. goto error;
  1013. if (brcmf_usb_fw_download(devinfo))
  1014. goto error;
  1015. return &devinfo->bus_pub;
  1016. error:
  1017. brcmf_dbg(ERROR, "failed!\n");
  1018. brcmf_usb_detach(devinfo);
  1019. return NULL;
  1020. }
  1021. static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
  1022. {
  1023. struct brcmf_bus *bus = NULL;
  1024. struct brcmf_usbdev *bus_pub = NULL;
  1025. int ret;
  1026. struct device *dev = devinfo->dev;
  1027. brcmf_dbg(USB, "Enter\n");
  1028. bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
  1029. if (!bus_pub)
  1030. return -ENODEV;
  1031. bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
  1032. if (!bus) {
  1033. ret = -ENOMEM;
  1034. goto fail;
  1035. }
  1036. bus_pub->bus = bus;
  1037. bus->brcmf_bus_txdata = brcmf_usb_tx;
  1038. bus->brcmf_bus_init = brcmf_usb_up;
  1039. bus->brcmf_bus_stop = brcmf_usb_down;
  1040. bus->brcmf_bus_txctl = brcmf_usb_tx_ctlpkt;
  1041. bus->brcmf_bus_rxctl = brcmf_usb_rx_ctlpkt;
  1042. bus->bus_priv.usb = bus_pub;
  1043. dev_set_drvdata(dev, bus);
  1044. /* Attach to the common driver interface */
  1045. ret = brcmf_attach(0, dev);
  1046. if (ret) {
  1047. brcmf_dbg(ERROR, "brcmf_attach failed\n");
  1048. goto fail;
  1049. }
  1050. ret = brcmf_bus_start(dev);
  1051. if (ret) {
  1052. brcmf_dbg(ERROR, "dongle is not responding\n");
  1053. brcmf_detach(dev);
  1054. goto fail;
  1055. }
  1056. return 0;
  1057. fail:
  1058. /* Release resources in reverse order */
  1059. kfree(bus);
  1060. brcmf_usb_detach(devinfo);
  1061. return ret;
  1062. }
  1063. static void
  1064. brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
  1065. {
  1066. if (!devinfo)
  1067. return;
  1068. brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
  1069. brcmf_detach(devinfo->dev);
  1070. kfree(devinfo->bus_pub.bus);
  1071. brcmf_usb_detach(devinfo);
  1072. }
  1073. static int
  1074. brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1075. {
  1076. int ep;
  1077. struct usb_endpoint_descriptor *endpoint;
  1078. int ret = 0;
  1079. struct usb_device *usb = interface_to_usbdev(intf);
  1080. int num_of_eps;
  1081. u8 endpoint_num;
  1082. struct brcmf_usbdev_info *devinfo;
  1083. brcmf_dbg(USB, "Enter\n");
  1084. devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
  1085. if (devinfo == NULL)
  1086. return -ENOMEM;
  1087. devinfo->usbdev = usb;
  1088. devinfo->dev = &usb->dev;
  1089. usb_set_intfdata(intf, devinfo);
  1090. /* Check that the device supports only one configuration */
  1091. if (usb->descriptor.bNumConfigurations != 1) {
  1092. ret = -1;
  1093. goto fail;
  1094. }
  1095. if (usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
  1096. ret = -1;
  1097. goto fail;
  1098. }
  1099. /*
  1100. * Only the BDC interface configuration is supported:
  1101. * Device class: USB_CLASS_VENDOR_SPEC
  1102. * if0 class: USB_CLASS_VENDOR_SPEC
  1103. * if0/ep0: control
  1104. * if0/ep1: bulk in
  1105. * if0/ep2: bulk out (ok if swapped with bulk in)
  1106. */
  1107. if (CONFIGDESC(usb)->bNumInterfaces != 1) {
  1108. ret = -1;
  1109. goto fail;
  1110. }
  1111. /* Check interface */
  1112. if (IFDESC(usb, CONTROL_IF).bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
  1113. IFDESC(usb, CONTROL_IF).bInterfaceSubClass != 2 ||
  1114. IFDESC(usb, CONTROL_IF).bInterfaceProtocol != 0xff) {
  1115. brcmf_dbg(ERROR, "invalid control interface: class %d, subclass %d, proto %d\n",
  1116. IFDESC(usb, CONTROL_IF).bInterfaceClass,
  1117. IFDESC(usb, CONTROL_IF).bInterfaceSubClass,
  1118. IFDESC(usb, CONTROL_IF).bInterfaceProtocol);
  1119. ret = -1;
  1120. goto fail;
  1121. }
  1122. /* Check control endpoint */
  1123. endpoint = &IFEPDESC(usb, CONTROL_IF, 0);
  1124. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1125. != USB_ENDPOINT_XFER_INT) {
  1126. brcmf_dbg(ERROR, "invalid control endpoint %d\n",
  1127. endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
  1128. ret = -1;
  1129. goto fail;
  1130. }
  1131. endpoint_num = endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1132. devinfo->intr_pipe = usb_rcvintpipe(usb, endpoint_num);
  1133. devinfo->rx_pipe = 0;
  1134. devinfo->rx_pipe2 = 0;
  1135. devinfo->tx_pipe = 0;
  1136. num_of_eps = IFDESC(usb, BULK_IF).bNumEndpoints - 1;
  1137. /* Check data endpoints and get pipes */
  1138. for (ep = 1; ep <= num_of_eps; ep++) {
  1139. endpoint = &IFEPDESC(usb, BULK_IF, ep);
  1140. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1141. USB_ENDPOINT_XFER_BULK) {
  1142. brcmf_dbg(ERROR, "invalid data endpoint %d\n", ep);
  1143. ret = -1;
  1144. goto fail;
  1145. }
  1146. endpoint_num = endpoint->bEndpointAddress &
  1147. USB_ENDPOINT_NUMBER_MASK;
  1148. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1149. == USB_DIR_IN) {
  1150. if (!devinfo->rx_pipe) {
  1151. devinfo->rx_pipe =
  1152. usb_rcvbulkpipe(usb, endpoint_num);
  1153. } else {
  1154. devinfo->rx_pipe2 =
  1155. usb_rcvbulkpipe(usb, endpoint_num);
  1156. }
  1157. } else {
  1158. devinfo->tx_pipe = usb_sndbulkpipe(usb, endpoint_num);
  1159. }
  1160. }
  1161. /* Allocate interrupt URB and data buffer */
  1162. /* RNDIS says 8-byte intr, our old drivers used 4-byte */
  1163. if (IFEPDESC(usb, CONTROL_IF, 0).wMaxPacketSize == cpu_to_le16(16))
  1164. devinfo->intr_size = 8;
  1165. else
  1166. devinfo->intr_size = 4;
  1167. devinfo->interval = IFEPDESC(usb, CONTROL_IF, 0).bInterval;
  1168. if (usb->speed == USB_SPEED_HIGH)
  1169. brcmf_dbg(USB, "Broadcom high speed USB wireless device detected\n");
  1170. else
  1171. brcmf_dbg(USB, "Broadcom full speed USB wireless device detected\n");
  1172. ret = brcmf_usb_probe_cb(devinfo);
  1173. if (ret)
  1174. goto fail;
  1175. /* Success */
  1176. return 0;
  1177. fail:
  1178. brcmf_dbg(ERROR, "failed with errno %d\n", ret);
  1179. kfree(devinfo);
  1180. usb_set_intfdata(intf, NULL);
  1181. return ret;
  1182. }
  1183. static void
  1184. brcmf_usb_disconnect(struct usb_interface *intf)
  1185. {
  1186. struct brcmf_usbdev_info *devinfo;
  1187. brcmf_dbg(USB, "Enter\n");
  1188. devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
  1189. brcmf_usb_disconnect_cb(devinfo);
  1190. kfree(devinfo);
  1191. brcmf_dbg(USB, "Exit\n");
  1192. }
  1193. /*
  1194. * only need to signal the bus being down and update the state.
  1195. */
  1196. static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
  1197. {
  1198. struct usb_device *usb = interface_to_usbdev(intf);
  1199. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1200. brcmf_dbg(USB, "Enter\n");
  1201. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
  1202. brcmf_detach(&usb->dev);
  1203. return 0;
  1204. }
  1205. /*
  1206. * (re-) start the bus.
  1207. */
  1208. static int brcmf_usb_resume(struct usb_interface *intf)
  1209. {
  1210. struct usb_device *usb = interface_to_usbdev(intf);
  1211. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1212. brcmf_dbg(USB, "Enter\n");
  1213. if (!brcmf_attach(0, devinfo->dev))
  1214. return brcmf_bus_start(&usb->dev);
  1215. return 0;
  1216. }
  1217. static int brcmf_usb_reset_resume(struct usb_interface *intf)
  1218. {
  1219. struct usb_device *usb = interface_to_usbdev(intf);
  1220. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1221. brcmf_dbg(USB, "Enter\n");
  1222. if (!brcmf_usb_fw_download(devinfo))
  1223. return brcmf_usb_resume(intf);
  1224. return -EIO;
  1225. }
  1226. #define BRCMF_USB_VENDOR_ID_BROADCOM 0x0a5c
  1227. #define BRCMF_USB_DEVICE_ID_43143 0xbd1e
  1228. #define BRCMF_USB_DEVICE_ID_43236 0xbd17
  1229. #define BRCMF_USB_DEVICE_ID_43242 0xbd1f
  1230. #define BRCMF_USB_DEVICE_ID_BCMFW 0x0bdc
  1231. static struct usb_device_id brcmf_usb_devid_table[] = {
  1232. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43143) },
  1233. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43236) },
  1234. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43242) },
  1235. /* special entry for device with firmware loaded and running */
  1236. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_BCMFW) },
  1237. { }
  1238. };
  1239. MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
  1240. MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME);
  1241. MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME);
  1242. MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME);
  1243. static struct usb_driver brcmf_usbdrvr = {
  1244. .name = KBUILD_MODNAME,
  1245. .probe = brcmf_usb_probe,
  1246. .disconnect = brcmf_usb_disconnect,
  1247. .id_table = brcmf_usb_devid_table,
  1248. .suspend = brcmf_usb_suspend,
  1249. .resume = brcmf_usb_resume,
  1250. .reset_resume = brcmf_usb_reset_resume,
  1251. .supports_autosuspend = 1,
  1252. .disable_hub_initiated_lpm = 1,
  1253. };
  1254. static void brcmf_release_fw(struct list_head *q)
  1255. {
  1256. struct brcmf_usb_image *fw_image, *next;
  1257. list_for_each_entry_safe(fw_image, next, q, list) {
  1258. vfree(fw_image->image);
  1259. list_del_init(&fw_image->list);
  1260. }
  1261. }
  1262. void brcmf_usb_exit(void)
  1263. {
  1264. brcmf_dbg(USB, "Enter\n");
  1265. usb_deregister(&brcmf_usbdrvr);
  1266. brcmf_release_fw(&fw_image_list);
  1267. }
  1268. void brcmf_usb_init(void)
  1269. {
  1270. brcmf_dbg(USB, "Enter\n");
  1271. INIT_LIST_HEAD(&fw_image_list);
  1272. usb_register(&brcmf_usbdrvr);
  1273. }