Deleted Added
full compact
if_cue.c (207077) if_cue.c (212122)
1/*-
2 * Copyright (c) 1997, 1998, 1999, 2000
3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1997, 1998, 1999, 2000
3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/usb/net/if_cue.c 207077 2010-04-22 21:31:34Z thompsa $");
34__FBSDID("$FreeBSD: head/sys/dev/usb/net/if_cue.c 212122 2010-09-01 23:47:53Z thompsa $");
35
36/*
37 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
38 * adapters and others.
39 *
40 * Written by Bill Paul <wpaul@ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45/*
46 * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
47 * RX filter uses a 512-bit multicast hash table, single perfect entry
48 * for the station address, and promiscuous mode. Unlike the ADMtek
49 * and KLSI chips, the CATC ASIC supports read and write combining
50 * mode where multiple packets can be transfered using a single bulk
51 * transaction, which helps performance a great deal.
52 */
53
54#include <sys/stdint.h>
55#include <sys/stddef.h>
56#include <sys/param.h>
57#include <sys/queue.h>
58#include <sys/types.h>
59#include <sys/systm.h>
60#include <sys/kernel.h>
61#include <sys/bus.h>
62#include <sys/linker_set.h>
63#include <sys/module.h>
64#include <sys/lock.h>
65#include <sys/mutex.h>
66#include <sys/condvar.h>
67#include <sys/sysctl.h>
68#include <sys/sx.h>
69#include <sys/unistd.h>
70#include <sys/callout.h>
71#include <sys/malloc.h>
72#include <sys/priv.h>
73
74#include <dev/usb/usb.h>
75#include <dev/usb/usbdi.h>
76#include <dev/usb/usbdi_util.h>
77#include "usbdevs.h"
78
79#define USB_DEBUG_VAR cue_debug
80#include <dev/usb/usb_debug.h>
81#include <dev/usb/usb_process.h>
82
83#include <dev/usb/net/usb_ethernet.h>
84#include <dev/usb/net/if_cuereg.h>
85
86/*
87 * Various supported device vendors/products.
88 */
89
90/* Belkin F5U111 adapter covered by NETMATE entry */
91
92static const struct usb_device_id cue_devs[] = {
93#define CUE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
94 CUE_DEV(CATC, NETMATE),
95 CUE_DEV(CATC, NETMATE2),
96 CUE_DEV(SMARTBRIDGES, SMARTLINK),
97#undef CUE_DEV
98};
99
100/* prototypes */
101
102static device_probe_t cue_probe;
103static device_attach_t cue_attach;
104static device_detach_t cue_detach;
105
106static usb_callback_t cue_bulk_read_callback;
107static usb_callback_t cue_bulk_write_callback;
108
109static uether_fn_t cue_attach_post;
110static uether_fn_t cue_init;
111static uether_fn_t cue_stop;
112static uether_fn_t cue_start;
113static uether_fn_t cue_tick;
114static uether_fn_t cue_setmulti;
115static uether_fn_t cue_setpromisc;
116
117static uint8_t cue_csr_read_1(struct cue_softc *, uint16_t);
118static uint16_t cue_csr_read_2(struct cue_softc *, uint8_t);
119static int cue_csr_write_1(struct cue_softc *, uint16_t, uint16_t);
120static int cue_mem(struct cue_softc *, uint8_t, uint16_t, void *, int);
121static int cue_getmac(struct cue_softc *, void *);
122static uint32_t cue_mchash(const uint8_t *);
123static void cue_reset(struct cue_softc *);
124
125#ifdef USB_DEBUG
126static int cue_debug = 0;
127
128SYSCTL_NODE(_hw_usb, OID_AUTO, cue, CTLFLAG_RW, 0, "USB cue");
129SYSCTL_INT(_hw_usb_cue, OID_AUTO, debug, CTLFLAG_RW, &cue_debug, 0,
130 "Debug level");
131#endif
132
133static const struct usb_config cue_config[CUE_N_TRANSFER] = {
134
135 [CUE_BULK_DT_WR] = {
136 .type = UE_BULK,
137 .endpoint = UE_ADDR_ANY,
138 .direction = UE_DIR_OUT,
139 .bufsize = (MCLBYTES + 2),
140 .flags = {.pipe_bof = 1,},
141 .callback = cue_bulk_write_callback,
142 .timeout = 10000, /* 10 seconds */
143 },
144
145 [CUE_BULK_DT_RD] = {
146 .type = UE_BULK,
147 .endpoint = UE_ADDR_ANY,
148 .direction = UE_DIR_IN,
149 .bufsize = (MCLBYTES + 2),
150 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
151 .callback = cue_bulk_read_callback,
152 },
153};
154
155static device_method_t cue_methods[] = {
156 /* Device interface */
157 DEVMETHOD(device_probe, cue_probe),
158 DEVMETHOD(device_attach, cue_attach),
159 DEVMETHOD(device_detach, cue_detach),
160
161 {0, 0}
162};
163
164static driver_t cue_driver = {
165 .name = "cue",
166 .methods = cue_methods,
167 .size = sizeof(struct cue_softc),
168};
169
170static devclass_t cue_devclass;
171
172DRIVER_MODULE(cue, uhub, cue_driver, cue_devclass, NULL, 0);
173MODULE_DEPEND(cue, uether, 1, 1, 1);
174MODULE_DEPEND(cue, usb, 1, 1, 1);
175MODULE_DEPEND(cue, ether, 1, 1, 1);
35
36/*
37 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
38 * adapters and others.
39 *
40 * Written by Bill Paul <wpaul@ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45/*
46 * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
47 * RX filter uses a 512-bit multicast hash table, single perfect entry
48 * for the station address, and promiscuous mode. Unlike the ADMtek
49 * and KLSI chips, the CATC ASIC supports read and write combining
50 * mode where multiple packets can be transfered using a single bulk
51 * transaction, which helps performance a great deal.
52 */
53
54#include <sys/stdint.h>
55#include <sys/stddef.h>
56#include <sys/param.h>
57#include <sys/queue.h>
58#include <sys/types.h>
59#include <sys/systm.h>
60#include <sys/kernel.h>
61#include <sys/bus.h>
62#include <sys/linker_set.h>
63#include <sys/module.h>
64#include <sys/lock.h>
65#include <sys/mutex.h>
66#include <sys/condvar.h>
67#include <sys/sysctl.h>
68#include <sys/sx.h>
69#include <sys/unistd.h>
70#include <sys/callout.h>
71#include <sys/malloc.h>
72#include <sys/priv.h>
73
74#include <dev/usb/usb.h>
75#include <dev/usb/usbdi.h>
76#include <dev/usb/usbdi_util.h>
77#include "usbdevs.h"
78
79#define USB_DEBUG_VAR cue_debug
80#include <dev/usb/usb_debug.h>
81#include <dev/usb/usb_process.h>
82
83#include <dev/usb/net/usb_ethernet.h>
84#include <dev/usb/net/if_cuereg.h>
85
86/*
87 * Various supported device vendors/products.
88 */
89
90/* Belkin F5U111 adapter covered by NETMATE entry */
91
92static const struct usb_device_id cue_devs[] = {
93#define CUE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
94 CUE_DEV(CATC, NETMATE),
95 CUE_DEV(CATC, NETMATE2),
96 CUE_DEV(SMARTBRIDGES, SMARTLINK),
97#undef CUE_DEV
98};
99
100/* prototypes */
101
102static device_probe_t cue_probe;
103static device_attach_t cue_attach;
104static device_detach_t cue_detach;
105
106static usb_callback_t cue_bulk_read_callback;
107static usb_callback_t cue_bulk_write_callback;
108
109static uether_fn_t cue_attach_post;
110static uether_fn_t cue_init;
111static uether_fn_t cue_stop;
112static uether_fn_t cue_start;
113static uether_fn_t cue_tick;
114static uether_fn_t cue_setmulti;
115static uether_fn_t cue_setpromisc;
116
117static uint8_t cue_csr_read_1(struct cue_softc *, uint16_t);
118static uint16_t cue_csr_read_2(struct cue_softc *, uint8_t);
119static int cue_csr_write_1(struct cue_softc *, uint16_t, uint16_t);
120static int cue_mem(struct cue_softc *, uint8_t, uint16_t, void *, int);
121static int cue_getmac(struct cue_softc *, void *);
122static uint32_t cue_mchash(const uint8_t *);
123static void cue_reset(struct cue_softc *);
124
125#ifdef USB_DEBUG
126static int cue_debug = 0;
127
128SYSCTL_NODE(_hw_usb, OID_AUTO, cue, CTLFLAG_RW, 0, "USB cue");
129SYSCTL_INT(_hw_usb_cue, OID_AUTO, debug, CTLFLAG_RW, &cue_debug, 0,
130 "Debug level");
131#endif
132
133static const struct usb_config cue_config[CUE_N_TRANSFER] = {
134
135 [CUE_BULK_DT_WR] = {
136 .type = UE_BULK,
137 .endpoint = UE_ADDR_ANY,
138 .direction = UE_DIR_OUT,
139 .bufsize = (MCLBYTES + 2),
140 .flags = {.pipe_bof = 1,},
141 .callback = cue_bulk_write_callback,
142 .timeout = 10000, /* 10 seconds */
143 },
144
145 [CUE_BULK_DT_RD] = {
146 .type = UE_BULK,
147 .endpoint = UE_ADDR_ANY,
148 .direction = UE_DIR_IN,
149 .bufsize = (MCLBYTES + 2),
150 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
151 .callback = cue_bulk_read_callback,
152 },
153};
154
155static device_method_t cue_methods[] = {
156 /* Device interface */
157 DEVMETHOD(device_probe, cue_probe),
158 DEVMETHOD(device_attach, cue_attach),
159 DEVMETHOD(device_detach, cue_detach),
160
161 {0, 0}
162};
163
164static driver_t cue_driver = {
165 .name = "cue",
166 .methods = cue_methods,
167 .size = sizeof(struct cue_softc),
168};
169
170static devclass_t cue_devclass;
171
172DRIVER_MODULE(cue, uhub, cue_driver, cue_devclass, NULL, 0);
173MODULE_DEPEND(cue, uether, 1, 1, 1);
174MODULE_DEPEND(cue, usb, 1, 1, 1);
175MODULE_DEPEND(cue, ether, 1, 1, 1);
176MODULE_VERSION(cue, 1);
176
177static const struct usb_ether_methods cue_ue_methods = {
178 .ue_attach_post = cue_attach_post,
179 .ue_start = cue_start,
180 .ue_init = cue_init,
181 .ue_stop = cue_stop,
182 .ue_tick = cue_tick,
183 .ue_setmulti = cue_setmulti,
184 .ue_setpromisc = cue_setpromisc,
185};
186
187#define CUE_SETBIT(sc, reg, x) \
188 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
189
190#define CUE_CLRBIT(sc, reg, x) \
191 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
192
193static uint8_t
194cue_csr_read_1(struct cue_softc *sc, uint16_t reg)
195{
196 struct usb_device_request req;
197 uint8_t val;
198
199 req.bmRequestType = UT_READ_VENDOR_DEVICE;
200 req.bRequest = CUE_CMD_READREG;
201 USETW(req.wValue, 0);
202 USETW(req.wIndex, reg);
203 USETW(req.wLength, 1);
204
205 if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
206 /* ignore any errors */
207 }
208 return (val);
209}
210
211static uint16_t
212cue_csr_read_2(struct cue_softc *sc, uint8_t reg)
213{
214 struct usb_device_request req;
215 uint16_t val;
216
217 req.bmRequestType = UT_READ_VENDOR_DEVICE;
218 req.bRequest = CUE_CMD_READREG;
219 USETW(req.wValue, 0);
220 USETW(req.wIndex, reg);
221 USETW(req.wLength, 2);
222
223 (void)uether_do_request(&sc->sc_ue, &req, &val, 1000);
224 return (le16toh(val));
225}
226
227static int
228cue_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val)
229{
230 struct usb_device_request req;
231
232 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
233 req.bRequest = CUE_CMD_WRITEREG;
234 USETW(req.wValue, val);
235 USETW(req.wIndex, reg);
236 USETW(req.wLength, 0);
237
238 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
239}
240
241static int
242cue_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr, void *buf, int len)
243{
244 struct usb_device_request req;
245
246 if (cmd == CUE_CMD_READSRAM)
247 req.bmRequestType = UT_READ_VENDOR_DEVICE;
248 else
249 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
250 req.bRequest = cmd;
251 USETW(req.wValue, 0);
252 USETW(req.wIndex, addr);
253 USETW(req.wLength, len);
254
255 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
256}
257
258static int
259cue_getmac(struct cue_softc *sc, void *buf)
260{
261 struct usb_device_request req;
262
263 req.bmRequestType = UT_READ_VENDOR_DEVICE;
264 req.bRequest = CUE_CMD_GET_MACADDR;
265 USETW(req.wValue, 0);
266 USETW(req.wIndex, 0);
267 USETW(req.wLength, ETHER_ADDR_LEN);
268
269 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
270}
271
272#define CUE_BITS 9
273
274static uint32_t
275cue_mchash(const uint8_t *addr)
276{
277 uint32_t crc;
278
279 /* Compute CRC for the address value. */
280 crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
281
282 return (crc & ((1 << CUE_BITS) - 1));
283}
284
285static void
286cue_setpromisc(struct usb_ether *ue)
287{
288 struct cue_softc *sc = uether_getsc(ue);
289 struct ifnet *ifp = uether_getifp(ue);
290
291 CUE_LOCK_ASSERT(sc, MA_OWNED);
292
293 /* if we want promiscuous mode, set the allframes bit */
294 if (ifp->if_flags & IFF_PROMISC)
295 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
296 else
297 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
298
299 /* write multicast hash-bits */
300 cue_setmulti(ue);
301}
302
303static void
304cue_setmulti(struct usb_ether *ue)
305{
306 struct cue_softc *sc = uether_getsc(ue);
307 struct ifnet *ifp = uether_getifp(ue);
308 struct ifmultiaddr *ifma;
309 uint32_t h = 0, i;
310 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
311
312 CUE_LOCK_ASSERT(sc, MA_OWNED);
313
314 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
315 for (i = 0; i < 8; i++)
316 hashtbl[i] = 0xff;
317 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
318 &hashtbl, 8);
319 return;
320 }
321
322 /* now program new ones */
323 if_maddr_rlock(ifp);
324 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
325 {
326 if (ifma->ifma_addr->sa_family != AF_LINK)
327 continue;
328 h = cue_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
329 hashtbl[h >> 3] |= 1 << (h & 0x7);
330 }
331 if_maddr_runlock(ifp);
332
333 /*
334 * Also include the broadcast address in the filter
335 * so we can receive broadcast frames.
336 */
337 if (ifp->if_flags & IFF_BROADCAST) {
338 h = cue_mchash(ifp->if_broadcastaddr);
339 hashtbl[h >> 3] |= 1 << (h & 0x7);
340 }
341
342 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &hashtbl, 8);
343}
344
345static void
346cue_reset(struct cue_softc *sc)
347{
348 struct usb_device_request req;
349
350 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
351 req.bRequest = CUE_CMD_RESET;
352 USETW(req.wValue, 0);
353 USETW(req.wIndex, 0);
354 USETW(req.wLength, 0);
355
356 if (uether_do_request(&sc->sc_ue, &req, NULL, 1000)) {
357 /* ignore any errors */
358 }
359
360 /*
361 * wait a little while for the chip to get its brains in order:
362 */
363 uether_pause(&sc->sc_ue, hz / 100);
364}
365
366static void
367cue_attach_post(struct usb_ether *ue)
368{
369 struct cue_softc *sc = uether_getsc(ue);
370
371 cue_getmac(sc, ue->ue_eaddr);
372}
373
374static int
375cue_probe(device_t dev)
376{
377 struct usb_attach_arg *uaa = device_get_ivars(dev);
378
379 if (uaa->usb_mode != USB_MODE_HOST)
380 return (ENXIO);
381 if (uaa->info.bConfigIndex != CUE_CONFIG_IDX)
382 return (ENXIO);
383 if (uaa->info.bIfaceIndex != CUE_IFACE_IDX)
384 return (ENXIO);
385
386 return (usbd_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa));
387}
388
389/*
390 * Attach the interface. Allocate softc structures, do ifmedia
391 * setup and ethernet/BPF attach.
392 */
393static int
394cue_attach(device_t dev)
395{
396 struct usb_attach_arg *uaa = device_get_ivars(dev);
397 struct cue_softc *sc = device_get_softc(dev);
398 struct usb_ether *ue = &sc->sc_ue;
399 uint8_t iface_index;
400 int error;
401
402 device_set_usb_desc(dev);
403 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
404
405 iface_index = CUE_IFACE_IDX;
406 error = usbd_transfer_setup(uaa->device, &iface_index,
407 sc->sc_xfer, cue_config, CUE_N_TRANSFER, sc, &sc->sc_mtx);
408 if (error) {
409 device_printf(dev, "allocating USB transfers failed\n");
410 goto detach;
411 }
412
413 ue->ue_sc = sc;
414 ue->ue_dev = dev;
415 ue->ue_udev = uaa->device;
416 ue->ue_mtx = &sc->sc_mtx;
417 ue->ue_methods = &cue_ue_methods;
418
419 error = uether_ifattach(ue);
420 if (error) {
421 device_printf(dev, "could not attach interface\n");
422 goto detach;
423 }
424 return (0); /* success */
425
426detach:
427 cue_detach(dev);
428 return (ENXIO); /* failure */
429}
430
431static int
432cue_detach(device_t dev)
433{
434 struct cue_softc *sc = device_get_softc(dev);
435 struct usb_ether *ue = &sc->sc_ue;
436
437 usbd_transfer_unsetup(sc->sc_xfer, CUE_N_TRANSFER);
438 uether_ifdetach(ue);
439 mtx_destroy(&sc->sc_mtx);
440
441 return (0);
442}
443
444static void
445cue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
446{
447 struct cue_softc *sc = usbd_xfer_softc(xfer);
448 struct usb_ether *ue = &sc->sc_ue;
449 struct ifnet *ifp = uether_getifp(ue);
450 struct usb_page_cache *pc;
451 uint8_t buf[2];
452 int len;
453 int actlen;
454
455 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
456
457 switch (USB_GET_STATE(xfer)) {
458 case USB_ST_TRANSFERRED:
459
460 if (actlen <= (2 + sizeof(struct ether_header))) {
461 ifp->if_ierrors++;
462 goto tr_setup;
463 }
464 pc = usbd_xfer_get_frame(xfer, 0);
465 usbd_copy_out(pc, 0, buf, 2);
466 actlen -= 2;
467 len = buf[0] | (buf[1] << 8);
468 len = min(actlen, len);
469
470 uether_rxbuf(ue, pc, 2, len);
471 /* FALLTHROUGH */
472 case USB_ST_SETUP:
473tr_setup:
474 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
475 usbd_transfer_submit(xfer);
476 uether_rxflush(ue);
477 return;
478
479 default: /* Error */
480 DPRINTF("bulk read error, %s\n",
481 usbd_errstr(error));
482
483 if (error != USB_ERR_CANCELLED) {
484 /* try to clear stall first */
485 usbd_xfer_set_stall(xfer);
486 goto tr_setup;
487 }
488 return;
489
490 }
491}
492
493static void
494cue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
495{
496 struct cue_softc *sc = usbd_xfer_softc(xfer);
497 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
498 struct usb_page_cache *pc;
499 struct mbuf *m;
500 uint8_t buf[2];
501
502 switch (USB_GET_STATE(xfer)) {
503 case USB_ST_TRANSFERRED:
504 DPRINTFN(11, "transfer complete\n");
505 ifp->if_opackets++;
506
507 /* FALLTHROUGH */
508 case USB_ST_SETUP:
509tr_setup:
510 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
511
512 if (m == NULL)
513 return;
514 if (m->m_pkthdr.len > MCLBYTES)
515 m->m_pkthdr.len = MCLBYTES;
516 usbd_xfer_set_frame_len(xfer, 0, (m->m_pkthdr.len + 2));
517
518 /* the first two bytes are the frame length */
519
520 buf[0] = (uint8_t)(m->m_pkthdr.len);
521 buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
522
523 pc = usbd_xfer_get_frame(xfer, 0);
524 usbd_copy_in(pc, 0, buf, 2);
525 usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
526
527 /*
528 * If there's a BPF listener, bounce a copy of this frame
529 * to him.
530 */
531 BPF_MTAP(ifp, m);
532
533 m_freem(m);
534
535 usbd_transfer_submit(xfer);
536
537 return;
538
539 default: /* Error */
540 DPRINTFN(11, "transfer error, %s\n",
541 usbd_errstr(error));
542
543 ifp->if_oerrors++;
544
545 if (error != USB_ERR_CANCELLED) {
546 /* try to clear stall first */
547 usbd_xfer_set_stall(xfer);
548 goto tr_setup;
549 }
550 return;
551 }
552}
553
554static void
555cue_tick(struct usb_ether *ue)
556{
557 struct cue_softc *sc = uether_getsc(ue);
558 struct ifnet *ifp = uether_getifp(ue);
559
560 CUE_LOCK_ASSERT(sc, MA_OWNED);
561
562 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
563 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
564 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
565
566 if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
567 ifp->if_ierrors++;
568}
569
570static void
571cue_start(struct usb_ether *ue)
572{
573 struct cue_softc *sc = uether_getsc(ue);
574
575 /*
576 * start the USB transfers, if not already started:
577 */
578 usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_RD]);
579 usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_WR]);
580}
581
582static void
583cue_init(struct usb_ether *ue)
584{
585 struct cue_softc *sc = uether_getsc(ue);
586 struct ifnet *ifp = uether_getifp(ue);
587 int i;
588
589 CUE_LOCK_ASSERT(sc, MA_OWNED);
590
591 /*
592 * Cancel pending I/O and free all RX/TX buffers.
593 */
594 cue_stop(ue);
595#if 0
596 cue_reset(sc);
597#endif
598 /* Set MAC address */
599 for (i = 0; i < ETHER_ADDR_LEN; i++)
600 cue_csr_write_1(sc, CUE_PAR0 - i, IF_LLADDR(ifp)[i]);
601
602 /* Enable RX logic. */
603 cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON);
604
605 /* Load the multicast filter */
606 cue_setpromisc(ue);
607
608 /*
609 * Set the number of RX and TX buffers that we want
610 * to reserve inside the ASIC.
611 */
612 cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
613 cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
614
615 /* Set advanced operation modes. */
616 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
617 CUE_AOP_EMBED_RXLEN | 0x01);/* 1 wait state */
618
619 /* Program the LED operation. */
620 cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
621
622 usbd_xfer_set_stall(sc->sc_xfer[CUE_BULK_DT_WR]);
623
624 ifp->if_drv_flags |= IFF_DRV_RUNNING;
625 cue_start(ue);
626}
627
628/*
629 * Stop the adapter and free any mbufs allocated to the
630 * RX and TX lists.
631 */
632static void
633cue_stop(struct usb_ether *ue)
634{
635 struct cue_softc *sc = uether_getsc(ue);
636 struct ifnet *ifp = uether_getifp(ue);
637
638 CUE_LOCK_ASSERT(sc, MA_OWNED);
639
640 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
641
642 /*
643 * stop all the transfers, if not already stopped:
644 */
645 usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_WR]);
646 usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_RD]);
647
648 cue_csr_write_1(sc, CUE_ETHCTL, 0);
649 cue_reset(sc);
650}
177
178static const struct usb_ether_methods cue_ue_methods = {
179 .ue_attach_post = cue_attach_post,
180 .ue_start = cue_start,
181 .ue_init = cue_init,
182 .ue_stop = cue_stop,
183 .ue_tick = cue_tick,
184 .ue_setmulti = cue_setmulti,
185 .ue_setpromisc = cue_setpromisc,
186};
187
188#define CUE_SETBIT(sc, reg, x) \
189 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
190
191#define CUE_CLRBIT(sc, reg, x) \
192 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
193
194static uint8_t
195cue_csr_read_1(struct cue_softc *sc, uint16_t reg)
196{
197 struct usb_device_request req;
198 uint8_t val;
199
200 req.bmRequestType = UT_READ_VENDOR_DEVICE;
201 req.bRequest = CUE_CMD_READREG;
202 USETW(req.wValue, 0);
203 USETW(req.wIndex, reg);
204 USETW(req.wLength, 1);
205
206 if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
207 /* ignore any errors */
208 }
209 return (val);
210}
211
212static uint16_t
213cue_csr_read_2(struct cue_softc *sc, uint8_t reg)
214{
215 struct usb_device_request req;
216 uint16_t val;
217
218 req.bmRequestType = UT_READ_VENDOR_DEVICE;
219 req.bRequest = CUE_CMD_READREG;
220 USETW(req.wValue, 0);
221 USETW(req.wIndex, reg);
222 USETW(req.wLength, 2);
223
224 (void)uether_do_request(&sc->sc_ue, &req, &val, 1000);
225 return (le16toh(val));
226}
227
228static int
229cue_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val)
230{
231 struct usb_device_request req;
232
233 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
234 req.bRequest = CUE_CMD_WRITEREG;
235 USETW(req.wValue, val);
236 USETW(req.wIndex, reg);
237 USETW(req.wLength, 0);
238
239 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
240}
241
242static int
243cue_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr, void *buf, int len)
244{
245 struct usb_device_request req;
246
247 if (cmd == CUE_CMD_READSRAM)
248 req.bmRequestType = UT_READ_VENDOR_DEVICE;
249 else
250 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
251 req.bRequest = cmd;
252 USETW(req.wValue, 0);
253 USETW(req.wIndex, addr);
254 USETW(req.wLength, len);
255
256 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
257}
258
259static int
260cue_getmac(struct cue_softc *sc, void *buf)
261{
262 struct usb_device_request req;
263
264 req.bmRequestType = UT_READ_VENDOR_DEVICE;
265 req.bRequest = CUE_CMD_GET_MACADDR;
266 USETW(req.wValue, 0);
267 USETW(req.wIndex, 0);
268 USETW(req.wLength, ETHER_ADDR_LEN);
269
270 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
271}
272
273#define CUE_BITS 9
274
275static uint32_t
276cue_mchash(const uint8_t *addr)
277{
278 uint32_t crc;
279
280 /* Compute CRC for the address value. */
281 crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
282
283 return (crc & ((1 << CUE_BITS) - 1));
284}
285
286static void
287cue_setpromisc(struct usb_ether *ue)
288{
289 struct cue_softc *sc = uether_getsc(ue);
290 struct ifnet *ifp = uether_getifp(ue);
291
292 CUE_LOCK_ASSERT(sc, MA_OWNED);
293
294 /* if we want promiscuous mode, set the allframes bit */
295 if (ifp->if_flags & IFF_PROMISC)
296 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
297 else
298 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
299
300 /* write multicast hash-bits */
301 cue_setmulti(ue);
302}
303
304static void
305cue_setmulti(struct usb_ether *ue)
306{
307 struct cue_softc *sc = uether_getsc(ue);
308 struct ifnet *ifp = uether_getifp(ue);
309 struct ifmultiaddr *ifma;
310 uint32_t h = 0, i;
311 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
312
313 CUE_LOCK_ASSERT(sc, MA_OWNED);
314
315 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
316 for (i = 0; i < 8; i++)
317 hashtbl[i] = 0xff;
318 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
319 &hashtbl, 8);
320 return;
321 }
322
323 /* now program new ones */
324 if_maddr_rlock(ifp);
325 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
326 {
327 if (ifma->ifma_addr->sa_family != AF_LINK)
328 continue;
329 h = cue_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
330 hashtbl[h >> 3] |= 1 << (h & 0x7);
331 }
332 if_maddr_runlock(ifp);
333
334 /*
335 * Also include the broadcast address in the filter
336 * so we can receive broadcast frames.
337 */
338 if (ifp->if_flags & IFF_BROADCAST) {
339 h = cue_mchash(ifp->if_broadcastaddr);
340 hashtbl[h >> 3] |= 1 << (h & 0x7);
341 }
342
343 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &hashtbl, 8);
344}
345
346static void
347cue_reset(struct cue_softc *sc)
348{
349 struct usb_device_request req;
350
351 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
352 req.bRequest = CUE_CMD_RESET;
353 USETW(req.wValue, 0);
354 USETW(req.wIndex, 0);
355 USETW(req.wLength, 0);
356
357 if (uether_do_request(&sc->sc_ue, &req, NULL, 1000)) {
358 /* ignore any errors */
359 }
360
361 /*
362 * wait a little while for the chip to get its brains in order:
363 */
364 uether_pause(&sc->sc_ue, hz / 100);
365}
366
367static void
368cue_attach_post(struct usb_ether *ue)
369{
370 struct cue_softc *sc = uether_getsc(ue);
371
372 cue_getmac(sc, ue->ue_eaddr);
373}
374
375static int
376cue_probe(device_t dev)
377{
378 struct usb_attach_arg *uaa = device_get_ivars(dev);
379
380 if (uaa->usb_mode != USB_MODE_HOST)
381 return (ENXIO);
382 if (uaa->info.bConfigIndex != CUE_CONFIG_IDX)
383 return (ENXIO);
384 if (uaa->info.bIfaceIndex != CUE_IFACE_IDX)
385 return (ENXIO);
386
387 return (usbd_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa));
388}
389
390/*
391 * Attach the interface. Allocate softc structures, do ifmedia
392 * setup and ethernet/BPF attach.
393 */
394static int
395cue_attach(device_t dev)
396{
397 struct usb_attach_arg *uaa = device_get_ivars(dev);
398 struct cue_softc *sc = device_get_softc(dev);
399 struct usb_ether *ue = &sc->sc_ue;
400 uint8_t iface_index;
401 int error;
402
403 device_set_usb_desc(dev);
404 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
405
406 iface_index = CUE_IFACE_IDX;
407 error = usbd_transfer_setup(uaa->device, &iface_index,
408 sc->sc_xfer, cue_config, CUE_N_TRANSFER, sc, &sc->sc_mtx);
409 if (error) {
410 device_printf(dev, "allocating USB transfers failed\n");
411 goto detach;
412 }
413
414 ue->ue_sc = sc;
415 ue->ue_dev = dev;
416 ue->ue_udev = uaa->device;
417 ue->ue_mtx = &sc->sc_mtx;
418 ue->ue_methods = &cue_ue_methods;
419
420 error = uether_ifattach(ue);
421 if (error) {
422 device_printf(dev, "could not attach interface\n");
423 goto detach;
424 }
425 return (0); /* success */
426
427detach:
428 cue_detach(dev);
429 return (ENXIO); /* failure */
430}
431
432static int
433cue_detach(device_t dev)
434{
435 struct cue_softc *sc = device_get_softc(dev);
436 struct usb_ether *ue = &sc->sc_ue;
437
438 usbd_transfer_unsetup(sc->sc_xfer, CUE_N_TRANSFER);
439 uether_ifdetach(ue);
440 mtx_destroy(&sc->sc_mtx);
441
442 return (0);
443}
444
445static void
446cue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
447{
448 struct cue_softc *sc = usbd_xfer_softc(xfer);
449 struct usb_ether *ue = &sc->sc_ue;
450 struct ifnet *ifp = uether_getifp(ue);
451 struct usb_page_cache *pc;
452 uint8_t buf[2];
453 int len;
454 int actlen;
455
456 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
457
458 switch (USB_GET_STATE(xfer)) {
459 case USB_ST_TRANSFERRED:
460
461 if (actlen <= (2 + sizeof(struct ether_header))) {
462 ifp->if_ierrors++;
463 goto tr_setup;
464 }
465 pc = usbd_xfer_get_frame(xfer, 0);
466 usbd_copy_out(pc, 0, buf, 2);
467 actlen -= 2;
468 len = buf[0] | (buf[1] << 8);
469 len = min(actlen, len);
470
471 uether_rxbuf(ue, pc, 2, len);
472 /* FALLTHROUGH */
473 case USB_ST_SETUP:
474tr_setup:
475 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
476 usbd_transfer_submit(xfer);
477 uether_rxflush(ue);
478 return;
479
480 default: /* Error */
481 DPRINTF("bulk read error, %s\n",
482 usbd_errstr(error));
483
484 if (error != USB_ERR_CANCELLED) {
485 /* try to clear stall first */
486 usbd_xfer_set_stall(xfer);
487 goto tr_setup;
488 }
489 return;
490
491 }
492}
493
494static void
495cue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
496{
497 struct cue_softc *sc = usbd_xfer_softc(xfer);
498 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
499 struct usb_page_cache *pc;
500 struct mbuf *m;
501 uint8_t buf[2];
502
503 switch (USB_GET_STATE(xfer)) {
504 case USB_ST_TRANSFERRED:
505 DPRINTFN(11, "transfer complete\n");
506 ifp->if_opackets++;
507
508 /* FALLTHROUGH */
509 case USB_ST_SETUP:
510tr_setup:
511 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
512
513 if (m == NULL)
514 return;
515 if (m->m_pkthdr.len > MCLBYTES)
516 m->m_pkthdr.len = MCLBYTES;
517 usbd_xfer_set_frame_len(xfer, 0, (m->m_pkthdr.len + 2));
518
519 /* the first two bytes are the frame length */
520
521 buf[0] = (uint8_t)(m->m_pkthdr.len);
522 buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
523
524 pc = usbd_xfer_get_frame(xfer, 0);
525 usbd_copy_in(pc, 0, buf, 2);
526 usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
527
528 /*
529 * If there's a BPF listener, bounce a copy of this frame
530 * to him.
531 */
532 BPF_MTAP(ifp, m);
533
534 m_freem(m);
535
536 usbd_transfer_submit(xfer);
537
538 return;
539
540 default: /* Error */
541 DPRINTFN(11, "transfer error, %s\n",
542 usbd_errstr(error));
543
544 ifp->if_oerrors++;
545
546 if (error != USB_ERR_CANCELLED) {
547 /* try to clear stall first */
548 usbd_xfer_set_stall(xfer);
549 goto tr_setup;
550 }
551 return;
552 }
553}
554
555static void
556cue_tick(struct usb_ether *ue)
557{
558 struct cue_softc *sc = uether_getsc(ue);
559 struct ifnet *ifp = uether_getifp(ue);
560
561 CUE_LOCK_ASSERT(sc, MA_OWNED);
562
563 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
564 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
565 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
566
567 if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
568 ifp->if_ierrors++;
569}
570
571static void
572cue_start(struct usb_ether *ue)
573{
574 struct cue_softc *sc = uether_getsc(ue);
575
576 /*
577 * start the USB transfers, if not already started:
578 */
579 usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_RD]);
580 usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_WR]);
581}
582
583static void
584cue_init(struct usb_ether *ue)
585{
586 struct cue_softc *sc = uether_getsc(ue);
587 struct ifnet *ifp = uether_getifp(ue);
588 int i;
589
590 CUE_LOCK_ASSERT(sc, MA_OWNED);
591
592 /*
593 * Cancel pending I/O and free all RX/TX buffers.
594 */
595 cue_stop(ue);
596#if 0
597 cue_reset(sc);
598#endif
599 /* Set MAC address */
600 for (i = 0; i < ETHER_ADDR_LEN; i++)
601 cue_csr_write_1(sc, CUE_PAR0 - i, IF_LLADDR(ifp)[i]);
602
603 /* Enable RX logic. */
604 cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON);
605
606 /* Load the multicast filter */
607 cue_setpromisc(ue);
608
609 /*
610 * Set the number of RX and TX buffers that we want
611 * to reserve inside the ASIC.
612 */
613 cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
614 cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
615
616 /* Set advanced operation modes. */
617 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
618 CUE_AOP_EMBED_RXLEN | 0x01);/* 1 wait state */
619
620 /* Program the LED operation. */
621 cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
622
623 usbd_xfer_set_stall(sc->sc_xfer[CUE_BULK_DT_WR]);
624
625 ifp->if_drv_flags |= IFF_DRV_RUNNING;
626 cue_start(ue);
627}
628
629/*
630 * Stop the adapter and free any mbufs allocated to the
631 * RX and TX lists.
632 */
633static void
634cue_stop(struct usb_ether *ue)
635{
636 struct cue_softc *sc = uether_getsc(ue);
637 struct ifnet *ifp = uether_getifp(ue);
638
639 CUE_LOCK_ASSERT(sc, MA_OWNED);
640
641 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
642
643 /*
644 * stop all the transfers, if not already stopped:
645 */
646 usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_WR]);
647 usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_RD]);
648
649 cue_csr_write_1(sc, CUE_ETHCTL, 0);
650 cue_reset(sc);
651}