usb.c 39 KB

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