usb.c 40 KB

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