usb.c 40 KB

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