usb.c 39 KB

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