usb.c 39 KB

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