usb.c 37 KB

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