]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - drivers/net/wireless/brcm80211/brcmfmac/dhd_cdc.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[can-eth-gw-linux.git] / drivers / net / wireless / brcm80211 / brcmfmac / dhd_cdc.c
1 /*
2  * Copyright (c) 2010 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
17 /*******************************************************************************
18  * Communicates with the dongle by using dcmd codes.
19  * For certain dcmd codes, the dongle interprets string data from the host.
20  ******************************************************************************/
21
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24 #include <linux/types.h>
25 #include <linux/netdevice.h>
26
27 #include <brcmu_utils.h>
28 #include <brcmu_wifi.h>
29
30 #include "dhd.h"
31 #include "dhd_proto.h"
32 #include "dhd_bus.h"
33 #include "dhd_dbg.h"
34
35 struct brcmf_proto_cdc_dcmd {
36         __le32 cmd;     /* dongle command value */
37         __le32 len;     /* lower 16: output buflen;
38                          * upper 16: input buflen (excludes header) */
39         __le32 flags;   /* flag defns given below */
40         __le32 status;  /* status code returned from the device */
41 };
42
43 /* Max valid buffer size that can be sent to the dongle */
44 #define CDC_MAX_MSG_SIZE        (ETH_FRAME_LEN+ETH_FCS_LEN)
45
46 /* CDC flag definitions */
47 #define CDC_DCMD_ERROR          0x01    /* 1=cmd failed */
48 #define CDC_DCMD_SET            0x02    /* 0=get, 1=set cmd */
49 #define CDC_DCMD_IF_MASK        0xF000          /* I/F index */
50 #define CDC_DCMD_IF_SHIFT       12
51 #define CDC_DCMD_ID_MASK        0xFFFF0000      /* id an cmd pairing */
52 #define CDC_DCMD_ID_SHIFT       16              /* ID Mask shift bits */
53 #define CDC_DCMD_ID(flags)      \
54         (((flags) & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT)
55
56 /*
57  * BDC header - Broadcom specific extension of CDC.
58  * Used on data packets to convey priority across USB.
59  */
60 #define BDC_HEADER_LEN          4
61 #define BDC_PROTO_VER           2       /* Protocol version */
62 #define BDC_FLAG_VER_MASK       0xf0    /* Protocol version mask */
63 #define BDC_FLAG_VER_SHIFT      4       /* Protocol version shift */
64 #define BDC_FLAG_SUM_GOOD       0x04    /* Good RX checksums */
65 #define BDC_FLAG_SUM_NEEDED     0x08    /* Dongle needs to do TX checksums */
66 #define BDC_PRIORITY_MASK       0x7
67 #define BDC_FLAG2_IF_MASK       0x0f    /* packet rx interface in APSTA */
68 #define BDC_FLAG2_IF_SHIFT      0
69
70 #define BDC_GET_IF_IDX(hdr) \
71         ((int)((((hdr)->flags2) & BDC_FLAG2_IF_MASK) >> BDC_FLAG2_IF_SHIFT))
72 #define BDC_SET_IF_IDX(hdr, idx) \
73         ((hdr)->flags2 = (((hdr)->flags2 & ~BDC_FLAG2_IF_MASK) | \
74         ((idx) << BDC_FLAG2_IF_SHIFT)))
75
76 struct brcmf_proto_bdc_header {
77         u8 flags;
78         u8 priority;    /* 802.1d Priority, 4:7 flow control info for usb */
79         u8 flags2;
80         u8 data_offset;
81 };
82
83
84 #define RETRIES 2 /* # of retries to retrieve matching dcmd response */
85 #define BUS_HEADER_LEN  (16+64)         /* Must be atleast SDPCM_RESERVE
86                                          * (amount of header tha might be added)
87                                          * plus any space that might be needed
88                                          * for bus alignment padding.
89                                          */
90 #define ROUND_UP_MARGIN 2048    /* Biggest bus block size possible for
91                                  * round off at the end of buffer
92                                  * Currently is SDIO
93                                  */
94
95 struct brcmf_proto {
96         u16 reqid;
97         u8 pending;
98         u32 lastcmd;
99         u8 bus_header[BUS_HEADER_LEN];
100         struct brcmf_proto_cdc_dcmd msg;
101         unsigned char buf[BRCMF_DCMD_MAXLEN + ROUND_UP_MARGIN];
102 };
103
104 static int brcmf_proto_cdc_msg(struct brcmf_pub *drvr)
105 {
106         struct brcmf_proto *prot = drvr->prot;
107         int len = le32_to_cpu(prot->msg.len) +
108                         sizeof(struct brcmf_proto_cdc_dcmd);
109
110         brcmf_dbg(TRACE, "Enter\n");
111
112         /* NOTE : cdc->msg.len holds the desired length of the buffer to be
113          *        returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
114          *        is actually sent to the dongle
115          */
116         if (len > CDC_MAX_MSG_SIZE)
117                 len = CDC_MAX_MSG_SIZE;
118
119         /* Send request */
120         return brcmf_bus_txctl(drvr->bus_if, (unsigned char *)&prot->msg, len);
121 }
122
123 static int brcmf_proto_cdc_cmplt(struct brcmf_pub *drvr, u32 id, u32 len)
124 {
125         int ret;
126         struct brcmf_proto *prot = drvr->prot;
127
128         brcmf_dbg(TRACE, "Enter\n");
129         len += sizeof(struct brcmf_proto_cdc_dcmd);
130         do {
131                 ret = brcmf_bus_rxctl(drvr->bus_if, (unsigned char *)&prot->msg,
132                                       len);
133                 if (ret < 0)
134                         break;
135         } while (CDC_DCMD_ID(le32_to_cpu(prot->msg.flags)) != id);
136
137         return ret;
138 }
139
140 int
141 brcmf_proto_cdc_query_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
142                                void *buf, uint len)
143 {
144         struct brcmf_proto *prot = drvr->prot;
145         struct brcmf_proto_cdc_dcmd *msg = &prot->msg;
146         void *info;
147         int ret = 0, retries = 0;
148         u32 id, flags;
149
150         brcmf_dbg(TRACE, "Enter\n");
151         brcmf_dbg(CTL, "cmd %d len %d\n", cmd, len);
152
153         /* Respond "bcmerror" and "bcmerrorstr" with local cache */
154         if (cmd == BRCMF_C_GET_VAR && buf) {
155                 if (!strcmp((char *)buf, "bcmerrorstr")) {
156                         strncpy((char *)buf, "bcm_error",
157                                 BCME_STRLEN);
158                         goto done;
159                 } else if (!strcmp((char *)buf, "bcmerror")) {
160                         *(int *)buf = drvr->dongle_error;
161                         goto done;
162                 }
163         }
164
165         memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd));
166
167         msg->cmd = cpu_to_le32(cmd);
168         msg->len = cpu_to_le32(len);
169         flags = (++prot->reqid << CDC_DCMD_ID_SHIFT);
170         flags = (flags & ~CDC_DCMD_IF_MASK) |
171                 (ifidx << CDC_DCMD_IF_SHIFT);
172         msg->flags = cpu_to_le32(flags);
173
174         if (buf)
175                 memcpy(prot->buf, buf, len);
176
177         ret = brcmf_proto_cdc_msg(drvr);
178         if (ret < 0) {
179                 brcmf_err("brcmf_proto_cdc_msg failed w/status %d\n",
180                           ret);
181                 goto done;
182         }
183
184 retry:
185         /* wait for interrupt and get first fragment */
186         ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
187         if (ret < 0)
188                 goto done;
189
190         flags = le32_to_cpu(msg->flags);
191         id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT;
192
193         if ((id < prot->reqid) && (++retries < RETRIES))
194                 goto retry;
195         if (id != prot->reqid) {
196                 brcmf_err("%s: unexpected request id %d (expected %d)\n",
197                           brcmf_ifname(drvr, ifidx), id, prot->reqid);
198                 ret = -EINVAL;
199                 goto done;
200         }
201
202         /* Check info buffer */
203         info = (void *)&msg[1];
204
205         /* Copy info buffer */
206         if (buf) {
207                 if (ret < (int)len)
208                         len = ret;
209                 memcpy(buf, info, len);
210         }
211
212         /* Check the ERROR flag */
213         if (flags & CDC_DCMD_ERROR) {
214                 ret = le32_to_cpu(msg->status);
215                 /* Cache error from dongle */
216                 drvr->dongle_error = ret;
217         }
218
219 done:
220         return ret;
221 }
222
223 int brcmf_proto_cdc_set_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
224                                  void *buf, uint len)
225 {
226         struct brcmf_proto *prot = drvr->prot;
227         struct brcmf_proto_cdc_dcmd *msg = &prot->msg;
228         int ret = 0;
229         u32 flags, id;
230
231         brcmf_dbg(TRACE, "Enter\n");
232         brcmf_dbg(CTL, "cmd %d len %d\n", cmd, len);
233
234         memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd));
235
236         msg->cmd = cpu_to_le32(cmd);
237         msg->len = cpu_to_le32(len);
238         flags = (++prot->reqid << CDC_DCMD_ID_SHIFT) | CDC_DCMD_SET;
239         flags = (flags & ~CDC_DCMD_IF_MASK) |
240                 (ifidx << CDC_DCMD_IF_SHIFT);
241         msg->flags = cpu_to_le32(flags);
242
243         if (buf)
244                 memcpy(prot->buf, buf, len);
245
246         ret = brcmf_proto_cdc_msg(drvr);
247         if (ret < 0)
248                 goto done;
249
250         ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
251         if (ret < 0)
252                 goto done;
253
254         flags = le32_to_cpu(msg->flags);
255         id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT;
256
257         if (id != prot->reqid) {
258                 brcmf_err("%s: unexpected request id %d (expected %d)\n",
259                           brcmf_ifname(drvr, ifidx), id, prot->reqid);
260                 ret = -EINVAL;
261                 goto done;
262         }
263
264         /* Check the ERROR flag */
265         if (flags & CDC_DCMD_ERROR) {
266                 ret = le32_to_cpu(msg->status);
267                 /* Cache error from dongle */
268                 drvr->dongle_error = ret;
269         }
270
271 done:
272         return ret;
273 }
274
275 static bool pkt_sum_needed(struct sk_buff *skb)
276 {
277         return skb->ip_summed == CHECKSUM_PARTIAL;
278 }
279
280 static void pkt_set_sum_good(struct sk_buff *skb, bool x)
281 {
282         skb->ip_summed = (x ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
283 }
284
285 void brcmf_proto_hdrpush(struct brcmf_pub *drvr, int ifidx,
286                          struct sk_buff *pktbuf)
287 {
288         struct brcmf_proto_bdc_header *h;
289
290         brcmf_dbg(TRACE, "Enter\n");
291
292         /* Push BDC header used to convey priority for buses that don't */
293
294         skb_push(pktbuf, BDC_HEADER_LEN);
295
296         h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
297
298         h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
299         if (pkt_sum_needed(pktbuf))
300                 h->flags |= BDC_FLAG_SUM_NEEDED;
301
302         h->priority = (pktbuf->priority & BDC_PRIORITY_MASK);
303         h->flags2 = 0;
304         h->data_offset = 0;
305         BDC_SET_IF_IDX(h, ifidx);
306 }
307
308 int brcmf_proto_hdrpull(struct device *dev, int *ifidx,
309                         struct sk_buff *pktbuf)
310 {
311         struct brcmf_proto_bdc_header *h;
312         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
313         struct brcmf_pub *drvr = bus_if->drvr;
314
315         brcmf_dbg(TRACE, "Enter\n");
316
317         /* Pop BDC header used to convey priority for buses that don't */
318
319         if (pktbuf->len < BDC_HEADER_LEN) {
320                 brcmf_err("rx data too short (%d < %d)\n",
321                           pktbuf->len, BDC_HEADER_LEN);
322                 return -EBADE;
323         }
324
325         h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
326
327         *ifidx = BDC_GET_IF_IDX(h);
328         if (*ifidx >= BRCMF_MAX_IFS) {
329                 brcmf_err("rx data ifnum out of range (%d)\n", *ifidx);
330                 return -EBADE;
331         }
332
333         if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
334             BDC_PROTO_VER) {
335                 brcmf_err("%s: non-BDC packet received, flags 0x%x\n",
336                           brcmf_ifname(drvr, *ifidx), h->flags);
337                 return -EBADE;
338         }
339
340         if (h->flags & BDC_FLAG_SUM_GOOD) {
341                 brcmf_dbg(INFO, "%s: BDC packet received with good rx-csum, flags 0x%x\n",
342                           brcmf_ifname(drvr, *ifidx), h->flags);
343                 pkt_set_sum_good(pktbuf, true);
344         }
345
346         pktbuf->priority = h->priority & BDC_PRIORITY_MASK;
347
348         skb_pull(pktbuf, BDC_HEADER_LEN);
349         skb_pull(pktbuf, h->data_offset << 2);
350
351         return 0;
352 }
353
354 int brcmf_proto_attach(struct brcmf_pub *drvr)
355 {
356         struct brcmf_proto *cdc;
357
358         cdc = kzalloc(sizeof(struct brcmf_proto), GFP_ATOMIC);
359         if (!cdc)
360                 goto fail;
361
362         /* ensure that the msg buf directly follows the cdc msg struct */
363         if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) {
364                 brcmf_err("struct brcmf_proto is not correctly defined\n");
365                 goto fail;
366         }
367
368         drvr->prot = cdc;
369         drvr->hdrlen += BDC_HEADER_LEN;
370         drvr->bus_if->maxctl = BRCMF_DCMD_MAXLEN +
371                         sizeof(struct brcmf_proto_cdc_dcmd) + ROUND_UP_MARGIN;
372         return 0;
373
374 fail:
375         kfree(cdc);
376         return -ENOMEM;
377 }
378
379 /* ~NOTE~ What if another thread is waiting on the semaphore?  Holding it? */
380 void brcmf_proto_detach(struct brcmf_pub *drvr)
381 {
382         kfree(drvr->prot);
383         drvr->prot = NULL;
384 }
385
386 void brcmf_proto_stop(struct brcmf_pub *drvr)
387 {
388         /* Nothing to do for CDC */
389 }