if_udav.c revision 1.68 1 1.68 mrg /* $NetBSD: if_udav.c,v 1.68 2019/08/15 05:52:23 mrg Exp $ */
2 1.1 itojun /* $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $ */
3 1.37 mrg
4 1.1 itojun /*
5 1.1 itojun * Copyright (c) 2003
6 1.1 itojun * Shingo WATANABE <nabe (at) nabechan.org>. All rights reserved.
7 1.1 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun * 1. Redistributions of source code must retain the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer.
13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer in the
15 1.1 itojun * documentation and/or other materials provided with the distribution.
16 1.2 tsutsui * 3. Neither the name of the author nor the names of any co-contributors
17 1.1 itojun * may be used to endorse or promote products derived from this software
18 1.1 itojun * without specific prior written permission.
19 1.1 itojun *
20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 itojun * SUCH DAMAGE.
31 1.1 itojun *
32 1.1 itojun */
33 1.1 itojun
34 1.1 itojun /*
35 1.1 itojun * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
36 1.1 itojun * The spec can be found at the following url.
37 1.1 itojun * http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-F01-062202s.pdf
38 1.1 itojun */
39 1.1 itojun
40 1.1 itojun /*
41 1.1 itojun * TODO:
42 1.1 itojun * Interrupt Endpoint support
43 1.1 itojun * External PHYs
44 1.1 itojun * powerhook() support?
45 1.1 itojun */
46 1.1 itojun
47 1.1 itojun #include <sys/cdefs.h>
48 1.68 mrg __KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.68 2019/08/15 05:52:23 mrg Exp $");
49 1.1 itojun
50 1.40 christos #ifdef _KERNEL_OPT
51 1.49 skrll #include "opt_usb.h"
52 1.40 christos #endif
53 1.1 itojun
54 1.1 itojun #include <sys/param.h>
55 1.1 itojun
56 1.61 skrll #include <dev/usb/usbnet.h>
57 1.1 itojun #include <dev/usb/if_udavreg.h>
58 1.1 itojun
59 1.1 itojun /* Function declarations */
60 1.48 msaitoh int udav_match(device_t, cfdata_t, void *);
61 1.48 msaitoh void udav_attach(device_t, device_t, void *);
62 1.57 mrg
63 1.64 mrg CFATTACH_DECL_NEW(udav, sizeof(struct usbnet), udav_match, udav_attach,
64 1.61 skrll usbnet_detach, usbnet_activate);
65 1.61 skrll
66 1.61 skrll static void udav_chip_init(struct usbnet *);
67 1.1 itojun
68 1.61 skrll static unsigned udav_tx_prepare(struct usbnet *, struct mbuf *,
69 1.61 skrll struct usbnet_chain *);
70 1.68 mrg static void udav_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
71 1.61 skrll static void udav_stop_cb(struct ifnet *, int);
72 1.61 skrll static int udav_ioctl_cb(struct ifnet *, u_long, void *);
73 1.61 skrll static usbd_status udav_mii_read_reg(struct usbnet *, int, int, uint16_t *);
74 1.61 skrll static usbd_status udav_mii_write_reg(struct usbnet *, int, int, uint16_t);
75 1.61 skrll static void udav_mii_statchg(struct ifnet *);
76 1.61 skrll static int udav_init(struct ifnet *);
77 1.61 skrll static void udav_setiff(struct usbnet *);
78 1.61 skrll static void udav_setiff_locked(struct usbnet *);
79 1.61 skrll static void udav_reset(struct usbnet *);
80 1.61 skrll
81 1.64 mrg static int udav_csr_read(struct usbnet *, int, void *, int);
82 1.64 mrg static int udav_csr_write(struct usbnet *, int, void *, int);
83 1.64 mrg static int udav_csr_read1(struct usbnet *, int);
84 1.64 mrg static int udav_csr_write1(struct usbnet *, int, unsigned char);
85 1.1 itojun
86 1.1 itojun #if 0
87 1.64 mrg static int udav_mem_read(struct usbnet *, int, void *, int);
88 1.64 mrg static int udav_mem_write(struct usbnet *, int, void *, int);
89 1.64 mrg static int udav_mem_write1(struct usbnet *, int, unsigned char);
90 1.1 itojun #endif
91 1.1 itojun
92 1.1 itojun /* Macros */
93 1.1 itojun #ifdef UDAV_DEBUG
94 1.31 dyoung #define DPRINTF(x) if (udavdebug) printf x
95 1.58 msaitoh #define DPRINTFN(n, x) if (udavdebug >= (n)) printf x
96 1.61 skrll int udavdebug = 10;
97 1.1 itojun #else
98 1.1 itojun #define DPRINTF(x)
99 1.58 msaitoh #define DPRINTFN(n, x)
100 1.1 itojun #endif
101 1.1 itojun
102 1.64 mrg #define UDAV_SETBIT(un, reg, x) \
103 1.64 mrg udav_csr_write1(un, reg, udav_csr_read1(un, reg) | (x))
104 1.1 itojun
105 1.64 mrg #define UDAV_CLRBIT(un, reg, x) \
106 1.64 mrg udav_csr_write1(un, reg, udav_csr_read1(un, reg) & ~(x))
107 1.1 itojun
108 1.1 itojun static const struct udav_type {
109 1.1 itojun struct usb_devno udav_dev;
110 1.46 skrll uint16_t udav_flags;
111 1.1 itojun #define UDAV_EXT_PHY 0x0001
112 1.42 jakllsch #define UDAV_NO_PHY 0x0002
113 1.1 itojun } udav_devs [] = {
114 1.1 itojun /* Corega USB-TXC */
115 1.1 itojun {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
116 1.18 xtraeme /* ShanTou ST268 USB NIC */
117 1.18 xtraeme {{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268_USB_NIC }, 0},
118 1.22 drochner /* ShanTou ADM8515 */
119 1.22 drochner {{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515 }, 0},
120 1.30 jmcneill /* SUNRISING SR9600 */
121 1.30 jmcneill {{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_SR9600 }, 0 },
122 1.42 jakllsch /* SUNRISING QF9700 */
123 1.42 jakllsch {{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_QF9700 }, UDAV_NO_PHY },
124 1.35 mbalmer /* QUAN DM9601 */
125 1.35 mbalmer {{USB_VENDOR_QUAN, USB_PRODUCT_QUAN_DM9601 }, 0},
126 1.1 itojun #if 0
127 1.1 itojun /* DAVICOM DM9601 Generic? */
128 1.1 itojun /* XXX: The following ids was obtained from the data sheet. */
129 1.1 itojun {{ 0x0a46, 0x9601 }, 0},
130 1.1 itojun #endif
131 1.1 itojun };
132 1.7 christos #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p))
133 1.1 itojun
134 1.63 mrg static struct usbnet_ops udav_ops = {
135 1.63 mrg .uno_stop = udav_stop_cb,
136 1.63 mrg .uno_ioctl = udav_ioctl_cb,
137 1.63 mrg .uno_read_reg = udav_mii_read_reg,
138 1.63 mrg .uno_write_reg = udav_mii_write_reg,
139 1.63 mrg .uno_statchg = udav_mii_statchg,
140 1.63 mrg .uno_tx_prepare = udav_tx_prepare,
141 1.68 mrg .uno_rx_loop = udav_rx_loop,
142 1.63 mrg .uno_init = udav_init,
143 1.63 mrg };
144 1.1 itojun
145 1.1 itojun /* Probe */
146 1.40 christos int
147 1.31 dyoung udav_match(device_t parent, cfdata_t match, void *aux)
148 1.1 itojun {
149 1.31 dyoung struct usb_attach_arg *uaa = aux;
150 1.1 itojun
151 1.46 skrll return udav_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
152 1.46 skrll UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
153 1.1 itojun }
154 1.1 itojun
155 1.1 itojun /* Attach */
156 1.40 christos void
157 1.31 dyoung udav_attach(device_t parent, device_t self, void *aux)
158 1.1 itojun {
159 1.64 mrg struct usbnet * const un = device_private(self);
160 1.31 dyoung struct usb_attach_arg *uaa = aux;
161 1.46 skrll struct usbd_device *dev = uaa->uaa_device;
162 1.46 skrll struct usbd_interface *iface;
163 1.1 itojun usbd_status err;
164 1.1 itojun usb_interface_descriptor_t *id;
165 1.1 itojun usb_endpoint_descriptor_t *ed;
166 1.6 augustss char *devinfop;
167 1.61 skrll int i;
168 1.1 itojun
169 1.26 plunky aprint_naive("\n");
170 1.26 plunky aprint_normal("\n");
171 1.6 augustss devinfop = usbd_devinfo_alloc(dev, 0);
172 1.24 cube aprint_normal_dev(self, "%s\n", devinfop);
173 1.6 augustss usbd_devinfo_free(devinfop);
174 1.1 itojun
175 1.61 skrll un->un_dev = self;
176 1.61 skrll un->un_udev = dev;
177 1.64 mrg un->un_sc = un;
178 1.63 mrg un->un_ops = &udav_ops;
179 1.65 mrg un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
180 1.65 mrg un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
181 1.65 mrg un->un_rx_list_cnt = UDAV_RX_LIST_CNT;
182 1.65 mrg un->un_tx_list_cnt = UDAV_TX_LIST_CNT;
183 1.65 mrg un->un_rx_bufsz = UDAV_BUFSZ;
184 1.65 mrg un->un_tx_bufsz = UDAV_BUFSZ;
185 1.61 skrll
186 1.1 itojun /* Move the device into the configured state. */
187 1.22 drochner err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */
188 1.1 itojun if (err) {
189 1.39 skrll aprint_error_dev(self, "failed to set configuration"
190 1.39 skrll ", err=%s\n", usbd_errstr(err));
191 1.61 skrll return;
192 1.1 itojun }
193 1.1 itojun
194 1.1 itojun /* get control interface */
195 1.1 itojun err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
196 1.1 itojun if (err) {
197 1.24 cube aprint_error_dev(self, "failed to get interface, err=%s\n",
198 1.1 itojun usbd_errstr(err));
199 1.61 skrll return;
200 1.1 itojun }
201 1.1 itojun
202 1.61 skrll un->un_iface = iface;
203 1.64 mrg un->un_flags = udav_lookup(uaa->uaa_vendor,
204 1.48 msaitoh uaa->uaa_product)->udav_flags;
205 1.1 itojun
206 1.1 itojun /* get interface descriptor */
207 1.61 skrll id = usbd_get_interface_descriptor(un->un_iface);
208 1.1 itojun
209 1.1 itojun /* find endpoints */
210 1.61 skrll un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] =
211 1.61 skrll un->un_ed[USBNET_ENDPT_INTR] = -1;
212 1.1 itojun for (i = 0; i < id->bNumEndpoints; i++) {
213 1.61 skrll ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
214 1.1 itojun if (ed == NULL) {
215 1.24 cube aprint_error_dev(self, "couldn't get endpoint %d\n", i);
216 1.61 skrll return;
217 1.1 itojun }
218 1.1 itojun if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
219 1.1 itojun UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
220 1.61 skrll un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
221 1.1 itojun else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
222 1.1 itojun UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
223 1.61 skrll un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
224 1.1 itojun else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
225 1.1 itojun UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
226 1.61 skrll un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
227 1.1 itojun }
228 1.1 itojun
229 1.62 skrll if (un->un_ed[USBNET_ENDPT_RX] == 0 ||
230 1.62 skrll un->un_ed[USBNET_ENDPT_TX] == 0 ||
231 1.62 skrll un->un_ed[USBNET_ENDPT_INTR] == 0) {
232 1.24 cube aprint_error_dev(self, "missing endpoint\n");
233 1.61 skrll return;
234 1.1 itojun }
235 1.1 itojun
236 1.61 skrll /* Not supported yet. */
237 1.61 skrll un->un_ed[USBNET_ENDPT_INTR] = 0;
238 1.61 skrll
239 1.61 skrll // /* reset the adapter */
240 1.61 skrll // udav_reset(un);
241 1.1 itojun
242 1.65 mrg usbnet_attach(un, "udavdet");
243 1.1 itojun
244 1.1 itojun /* Get Ethernet Address */
245 1.61 skrll usbnet_lock_mii(un);
246 1.64 mrg err = udav_csr_read(un, UDAV_PAR, un->un_eaddr, ETHER_ADDR_LEN);
247 1.61 skrll usbnet_unlock_mii(un);
248 1.1 itojun if (err) {
249 1.24 cube aprint_error_dev(self, "read MAC address failed\n");
250 1.61 skrll return;
251 1.1 itojun }
252 1.1 itojun
253 1.64 mrg bool have_mii = !ISSET(un->un_flags, UDAV_NO_PHY);
254 1.1 itojun
255 1.9 wiz /* initialize interface information */
256 1.61 skrll usbnet_attach_ifp(un, have_mii,
257 1.61 skrll IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, 0, 0);
258 1.42 jakllsch
259 1.31 dyoung return;
260 1.1 itojun }
261 1.1 itojun
262 1.1 itojun #if 0
263 1.1 itojun /* read memory */
264 1.61 skrll static int
265 1.64 mrg udav_mem_read(struct usbnet *un, int offset, void *buf, int len)
266 1.1 itojun {
267 1.1 itojun usb_device_request_t req;
268 1.1 itojun usbd_status err;
269 1.1 itojun
270 1.1 itojun DPRINTFN(0x200,
271 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
272 1.1 itojun
273 1.63 mrg if (usbnet_isdying(un))
274 1.46 skrll return 0;
275 1.1 itojun
276 1.1 itojun offset &= 0xffff;
277 1.1 itojun len &= 0xff;
278 1.1 itojun
279 1.1 itojun req.bmRequestType = UT_READ_VENDOR_DEVICE;
280 1.1 itojun req.bRequest = UDAV_REQ_MEM_READ;
281 1.1 itojun USETW(req.wValue, 0x0000);
282 1.1 itojun USETW(req.wIndex, offset);
283 1.1 itojun USETW(req.wLength, len);
284 1.1 itojun
285 1.61 skrll err = usbd_do_request(un->un_udev, &req, buf);
286 1.1 itojun if (err) {
287 1.1 itojun DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
288 1.61 skrll device_xname(un->un_dev), __func__, offset, err));
289 1.1 itojun }
290 1.1 itojun
291 1.46 skrll return err;
292 1.1 itojun }
293 1.1 itojun
294 1.1 itojun /* write memory */
295 1.61 skrll static int
296 1.64 mrg udav_mem_write(struct usbnet *un, int offset, void *buf, int len)
297 1.1 itojun {
298 1.1 itojun usb_device_request_t req;
299 1.1 itojun usbd_status err;
300 1.1 itojun
301 1.1 itojun DPRINTFN(0x200,
302 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
303 1.1 itojun
304 1.63 mrg if (usbnet_isdying(un))
305 1.46 skrll return 0;
306 1.1 itojun
307 1.1 itojun offset &= 0xffff;
308 1.1 itojun len &= 0xff;
309 1.1 itojun
310 1.1 itojun req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
311 1.1 itojun req.bRequest = UDAV_REQ_MEM_WRITE;
312 1.1 itojun USETW(req.wValue, 0x0000);
313 1.1 itojun USETW(req.wIndex, offset);
314 1.1 itojun USETW(req.wLength, len);
315 1.1 itojun
316 1.61 skrll err = usbd_do_request(un->un_udev, &req, buf);
317 1.1 itojun if (err) {
318 1.1 itojun DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
319 1.61 skrll device_xname(un->un_dev), __func__, offset, err));
320 1.1 itojun }
321 1.1 itojun
322 1.46 skrll return err;
323 1.1 itojun }
324 1.1 itojun
325 1.1 itojun /* write memory */
326 1.61 skrll static int
327 1.64 mrg udav_mem_write1(struct usbnet *un, int offset, unsigned char ch)
328 1.1 itojun {
329 1.1 itojun usb_device_request_t req;
330 1.1 itojun usbd_status err;
331 1.1 itojun
332 1.1 itojun DPRINTFN(0x200,
333 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
334 1.1 itojun
335 1.63 mrg if (usbnet_isdying(un))
336 1.46 skrll return 0;
337 1.1 itojun
338 1.1 itojun offset &= 0xffff;
339 1.1 itojun
340 1.1 itojun req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
341 1.1 itojun req.bRequest = UDAV_REQ_MEM_WRITE1;
342 1.1 itojun USETW(req.wValue, ch);
343 1.1 itojun USETW(req.wIndex, offset);
344 1.1 itojun USETW(req.wLength, 0x0000);
345 1.1 itojun
346 1.61 skrll err = usbd_do_request(un->un_udev, &req, NULL);
347 1.1 itojun if (err) {
348 1.1 itojun DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
349 1.61 skrll device_xname(un->un_dev), __func__, offset, err));
350 1.1 itojun }
351 1.1 itojun
352 1.46 skrll return err;
353 1.1 itojun }
354 1.1 itojun #endif
355 1.1 itojun
356 1.1 itojun /* read register(s) */
357 1.61 skrll static int
358 1.64 mrg udav_csr_read(struct usbnet *un, int offset, void *buf, int len)
359 1.1 itojun {
360 1.1 itojun usb_device_request_t req;
361 1.1 itojun usbd_status err;
362 1.1 itojun
363 1.61 skrll usbnet_isowned_mii(un);
364 1.63 mrg KASSERT(!usbnet_isdying(un));
365 1.1 itojun
366 1.1 itojun DPRINTFN(0x200,
367 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
368 1.1 itojun
369 1.1 itojun offset &= 0xff;
370 1.1 itojun len &= 0xff;
371 1.1 itojun
372 1.1 itojun req.bmRequestType = UT_READ_VENDOR_DEVICE;
373 1.1 itojun req.bRequest = UDAV_REQ_REG_READ;
374 1.1 itojun USETW(req.wValue, 0x0000);
375 1.1 itojun USETW(req.wIndex, offset);
376 1.1 itojun USETW(req.wLength, len);
377 1.1 itojun
378 1.61 skrll err = usbd_do_request(un->un_udev, &req, buf);
379 1.1 itojun if (err) {
380 1.1 itojun DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
381 1.61 skrll device_xname(un->un_dev), __func__, offset, err));
382 1.1 itojun }
383 1.1 itojun
384 1.46 skrll return err;
385 1.1 itojun }
386 1.1 itojun
387 1.1 itojun /* write register(s) */
388 1.61 skrll static int
389 1.64 mrg udav_csr_write(struct usbnet *un, int offset, void *buf, int len)
390 1.1 itojun {
391 1.1 itojun usb_device_request_t req;
392 1.1 itojun usbd_status err;
393 1.1 itojun
394 1.61 skrll usbnet_isowned_mii(un);
395 1.63 mrg KASSERT(!usbnet_isdying(un));
396 1.1 itojun
397 1.1 itojun DPRINTFN(0x200,
398 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
399 1.1 itojun
400 1.1 itojun offset &= 0xff;
401 1.1 itojun len &= 0xff;
402 1.1 itojun
403 1.1 itojun req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
404 1.1 itojun req.bRequest = UDAV_REQ_REG_WRITE;
405 1.1 itojun USETW(req.wValue, 0x0000);
406 1.1 itojun USETW(req.wIndex, offset);
407 1.1 itojun USETW(req.wLength, len);
408 1.1 itojun
409 1.61 skrll err = usbd_do_request(un->un_udev, &req, buf);
410 1.1 itojun if (err) {
411 1.1 itojun DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
412 1.61 skrll device_xname(un->un_dev), __func__, offset, err));
413 1.1 itojun }
414 1.1 itojun
415 1.46 skrll return err;
416 1.1 itojun }
417 1.1 itojun
418 1.61 skrll static int
419 1.64 mrg udav_csr_read1(struct usbnet *un, int offset)
420 1.1 itojun {
421 1.46 skrll uint8_t val = 0;
422 1.5 perry
423 1.61 skrll usbnet_isowned_mii(un);
424 1.1 itojun
425 1.1 itojun DPRINTFN(0x200,
426 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
427 1.1 itojun
428 1.63 mrg if (usbnet_isdying(un))
429 1.46 skrll return 0;
430 1.1 itojun
431 1.64 mrg return udav_csr_read(un, offset, &val, 1) ? 0 : val;
432 1.1 itojun }
433 1.1 itojun
434 1.1 itojun /* write a register */
435 1.61 skrll static int
436 1.64 mrg udav_csr_write1(struct usbnet *un, int offset, unsigned char ch)
437 1.1 itojun {
438 1.1 itojun usb_device_request_t req;
439 1.1 itojun usbd_status err;
440 1.1 itojun
441 1.61 skrll usbnet_isowned_mii(un);
442 1.63 mrg KASSERT(!usbnet_isdying(un));
443 1.1 itojun
444 1.1 itojun DPRINTFN(0x200,
445 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
446 1.1 itojun
447 1.1 itojun offset &= 0xff;
448 1.1 itojun
449 1.1 itojun req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
450 1.1 itojun req.bRequest = UDAV_REQ_REG_WRITE1;
451 1.1 itojun USETW(req.wValue, ch);
452 1.1 itojun USETW(req.wIndex, offset);
453 1.1 itojun USETW(req.wLength, 0x0000);
454 1.1 itojun
455 1.61 skrll err = usbd_do_request(un->un_udev, &req, NULL);
456 1.1 itojun if (err) {
457 1.1 itojun DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
458 1.61 skrll device_xname(un->un_dev), __func__, offset, err));
459 1.1 itojun }
460 1.1 itojun
461 1.46 skrll return err;
462 1.1 itojun }
463 1.1 itojun
464 1.61 skrll static int
465 1.67 mrg udav_init(struct ifnet *ifp)
466 1.1 itojun {
467 1.61 skrll struct usbnet * const un = ifp->if_softc;
468 1.61 skrll struct mii_data * const mii = usbnet_mii(un);
469 1.20 dyoung uint8_t eaddr[ETHER_ADDR_LEN];
470 1.67 mrg int rc = 0;
471 1.61 skrll
472 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
473 1.1 itojun
474 1.67 mrg usbnet_lock(un);
475 1.1 itojun
476 1.67 mrg if (usbnet_isdying(un)) {
477 1.67 mrg usbnet_unlock(un);
478 1.46 skrll return EIO;
479 1.67 mrg }
480 1.1 itojun
481 1.61 skrll /* Cancel pending I/O and free all TX/RX buffers */
482 1.61 skrll if (ifp->if_flags & IFF_RUNNING)
483 1.61 skrll usbnet_stop(un, ifp, 1);
484 1.1 itojun
485 1.61 skrll usbnet_lock_mii_un_locked(un);
486 1.1 itojun
487 1.20 dyoung memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
488 1.64 mrg udav_csr_write(un, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
489 1.1 itojun
490 1.1 itojun /* Initialize network control register */
491 1.1 itojun /* Disable loopback */
492 1.64 mrg UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
493 1.1 itojun
494 1.1 itojun /* Initialize RX control register */
495 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
496 1.1 itojun
497 1.1 itojun /* If we want promiscuous mode, accept all physical frames. */
498 1.1 itojun if (ifp->if_flags & IFF_PROMISC)
499 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC);
500 1.1 itojun else
501 1.64 mrg UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC);
502 1.1 itojun
503 1.1 itojun /* Load the multicast filter */
504 1.61 skrll udav_setiff_locked(un);
505 1.1 itojun
506 1.1 itojun /* Enable RX */
507 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_RXEN);
508 1.1 itojun
509 1.1 itojun /* clear POWER_DOWN state of internal PHY */
510 1.64 mrg UDAV_SETBIT(un, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
511 1.64 mrg UDAV_CLRBIT(un, UDAV_GPR, UDAV_GPR_GEPIO0);
512 1.1 itojun
513 1.61 skrll usbnet_unlock_mii_un_locked(un);
514 1.67 mrg usbnet_unlock(un);
515 1.1 itojun
516 1.67 mrg if (mii && (rc = mii_mediachg(mii)) == ENXIO)
517 1.67 mrg rc = 0;
518 1.46 skrll
519 1.61 skrll if (rc != 0)
520 1.61 skrll return rc;
521 1.46 skrll
522 1.61 skrll usbnet_lock(un);
523 1.67 mrg if (usbnet_isdying(un))
524 1.67 mrg rc = EIO;
525 1.67 mrg else
526 1.67 mrg rc = usbnet_init_rx_tx(un);
527 1.61 skrll usbnet_unlock(un);
528 1.1 itojun
529 1.67 mrg return rc;
530 1.1 itojun }
531 1.1 itojun
532 1.61 skrll static void
533 1.61 skrll udav_reset(struct usbnet *un)
534 1.1 itojun {
535 1.61 skrll usbnet_isowned(un);
536 1.61 skrll
537 1.63 mrg if (usbnet_isdying(un))
538 1.61 skrll return;
539 1.61 skrll
540 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
541 1.61 skrll
542 1.61 skrll udav_chip_init(un);
543 1.61 skrll }
544 1.1 itojun
545 1.61 skrll static void
546 1.61 skrll udav_chip_init(struct usbnet *un)
547 1.61 skrll {
548 1.61 skrll usbnet_lock_mii_un_locked(un);
549 1.1 itojun
550 1.1 itojun /* Select PHY */
551 1.1 itojun #if 1
552 1.1 itojun /*
553 1.1 itojun * XXX: force select internal phy.
554 1.1 itojun * external phy routines are not tested.
555 1.1 itojun */
556 1.64 mrg UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY);
557 1.1 itojun #else
558 1.64 mrg if (un->un_flags & UDAV_EXT_PHY) {
559 1.64 mrg UDAV_SETBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY);
560 1.1 itojun } else {
561 1.64 mrg UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY);
562 1.1 itojun }
563 1.1 itojun #endif
564 1.1 itojun
565 1.64 mrg UDAV_SETBIT(un, UDAV_NCR, UDAV_NCR_RST);
566 1.1 itojun
567 1.61 skrll for (int i = 0; i < UDAV_TX_TIMEOUT; i++) {
568 1.64 mrg if (!(udav_csr_read1(un, UDAV_NCR) & UDAV_NCR_RST))
569 1.1 itojun break;
570 1.1 itojun delay(10); /* XXX */
571 1.1 itojun }
572 1.1 itojun delay(10000); /* XXX */
573 1.1 itojun
574 1.61 skrll usbnet_unlock_mii_un_locked(un);
575 1.1 itojun }
576 1.1 itojun
577 1.1 itojun #define UDAV_BITS 6
578 1.1 itojun
579 1.1 itojun #define UDAV_CALCHASH(addr) \
580 1.1 itojun (ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
581 1.1 itojun
582 1.61 skrll static void
583 1.61 skrll udav_setiff_locked(struct usbnet *un)
584 1.1 itojun {
585 1.61 skrll struct ethercom *ec = usbnet_ec(un);
586 1.61 skrll struct ifnet * const ifp = usbnet_ifp(un);
587 1.1 itojun struct ether_multi *enm;
588 1.1 itojun struct ether_multistep step;
589 1.46 skrll uint8_t hashes[8];
590 1.1 itojun int h = 0;
591 1.1 itojun
592 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
593 1.61 skrll
594 1.61 skrll usbnet_isowned_mii(un);
595 1.1 itojun
596 1.63 mrg if (usbnet_isdying(un))
597 1.1 itojun return;
598 1.1 itojun
599 1.64 mrg if (ISSET(un->un_flags, UDAV_NO_PHY)) {
600 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL);
601 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_PRMSC);
602 1.42 jakllsch return;
603 1.42 jakllsch }
604 1.42 jakllsch
605 1.1 itojun if (ifp->if_flags & IFF_PROMISC) {
606 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC);
607 1.1 itojun return;
608 1.1 itojun } else if (ifp->if_flags & IFF_ALLMULTI) {
609 1.59 msaitoh allmulti:
610 1.1 itojun ifp->if_flags |= IFF_ALLMULTI;
611 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL);
612 1.64 mrg UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_PRMSC);
613 1.1 itojun return;
614 1.1 itojun }
615 1.1 itojun
616 1.1 itojun /* first, zot all the existing hash bits */
617 1.1 itojun memset(hashes, 0x00, sizeof(hashes));
618 1.1 itojun hashes[7] |= 0x80; /* broadcast address */
619 1.64 mrg udav_csr_write(un, UDAV_MAR, hashes, sizeof(hashes));
620 1.1 itojun
621 1.1 itojun /* now program new ones */
622 1.59 msaitoh ETHER_LOCK(ec);
623 1.59 msaitoh ETHER_FIRST_MULTI(step, ec, enm);
624 1.1 itojun while (enm != NULL) {
625 1.1 itojun if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
626 1.59 msaitoh ETHER_ADDR_LEN) != 0) {
627 1.59 msaitoh ETHER_UNLOCK(ec);
628 1.1 itojun goto allmulti;
629 1.59 msaitoh }
630 1.1 itojun
631 1.1 itojun h = UDAV_CALCHASH(enm->enm_addrlo);
632 1.1 itojun hashes[h>>3] |= 1 << (h & 0x7);
633 1.1 itojun ETHER_NEXT_MULTI(step, enm);
634 1.1 itojun }
635 1.59 msaitoh ETHER_UNLOCK(ec);
636 1.1 itojun
637 1.1 itojun /* disable all multicast */
638 1.1 itojun ifp->if_flags &= ~IFF_ALLMULTI;
639 1.64 mrg UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_ALL);
640 1.1 itojun
641 1.1 itojun /* write hash value to the register */
642 1.64 mrg udav_csr_write(un, UDAV_MAR, hashes, sizeof(hashes));
643 1.1 itojun }
644 1.1 itojun
645 1.61 skrll static void
646 1.61 skrll udav_setiff(struct usbnet *un)
647 1.1 itojun {
648 1.61 skrll usbnet_lock_mii(un);
649 1.61 skrll udav_setiff_locked(un);
650 1.61 skrll usbnet_unlock_mii(un);
651 1.1 itojun }
652 1.1 itojun
653 1.1 itojun
654 1.61 skrll static unsigned
655 1.61 skrll udav_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
656 1.1 itojun {
657 1.61 skrll int total_len;
658 1.61 skrll uint8_t *buf = c->unc_buf;
659 1.1 itojun
660 1.61 skrll usbnet_isowned_tx(un);
661 1.1 itojun
662 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
663 1.1 itojun
664 1.66 skrll if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - 2)
665 1.61 skrll return 0;
666 1.1 itojun
667 1.1 itojun /* Copy the mbuf data into a contiguous buffer */
668 1.61 skrll m_copydata(m, 0, m->m_pkthdr.len, buf + 2);
669 1.1 itojun total_len = m->m_pkthdr.len;
670 1.1 itojun if (total_len < UDAV_MIN_FRAME_LEN) {
671 1.61 skrll memset(buf + 2 + total_len, 0,
672 1.1 itojun UDAV_MIN_FRAME_LEN - total_len);
673 1.1 itojun total_len = UDAV_MIN_FRAME_LEN;
674 1.1 itojun }
675 1.1 itojun
676 1.1 itojun /* Frame length is specified in the first 2bytes of the buffer */
677 1.61 skrll buf[0] = (uint8_t)total_len;
678 1.61 skrll buf[1] = (uint8_t)(total_len >> 8);
679 1.1 itojun total_len += 2;
680 1.1 itojun
681 1.61 skrll DPRINTF(("%s: %s: send %d bytes\n", device_xname(un->un_dev),
682 1.61 skrll __func__, total_len));
683 1.1 itojun
684 1.61 skrll return total_len;
685 1.1 itojun }
686 1.1 itojun
687 1.61 skrll static void
688 1.68 mrg udav_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
689 1.1 itojun {
690 1.61 skrll struct ifnet *ifp = usbnet_ifp(un);
691 1.61 skrll uint8_t *buf = c->unc_buf;
692 1.61 skrll uint16_t pkt_len;
693 1.61 skrll uint8_t pktstat;
694 1.1 itojun
695 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
696 1.1 itojun
697 1.61 skrll /* first byte in received data */
698 1.61 skrll pktstat = *buf;
699 1.61 skrll total_len -= sizeof(pktstat);
700 1.61 skrll buf += sizeof(pktstat);
701 1.1 itojun
702 1.61 skrll DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(un->un_dev), pktstat));
703 1.1 itojun
704 1.61 skrll pkt_len = UGETW(buf);
705 1.61 skrll total_len -= sizeof(pkt_len);
706 1.61 skrll buf += sizeof(pkt_len);
707 1.1 itojun
708 1.61 skrll DPRINTF(("%s: RX Length: 0x%02x\n", device_xname(un->un_dev), pkt_len));
709 1.1 itojun
710 1.61 skrll if (pktstat & UDAV_RSR_LCS) {
711 1.1 itojun ifp->if_collisions++;
712 1.61 skrll return;
713 1.1 itojun }
714 1.1 itojun
715 1.61 skrll if (pkt_len < sizeof(struct ether_header) ||
716 1.61 skrll pkt_len > total_len ||
717 1.61 skrll (pktstat & UDAV_RSR_ERR)) {
718 1.1 itojun ifp->if_ierrors++;
719 1.61 skrll return;
720 1.1 itojun }
721 1.1 itojun
722 1.61 skrll pkt_len -= ETHER_CRC_LEN;
723 1.1 itojun
724 1.61 skrll DPRINTF(("%s: Rx deliver: 0x%02x\n", device_xname(un->un_dev), pkt_len));
725 1.1 itojun
726 1.61 skrll usbnet_enqueue(un, buf, pkt_len, 0, 0, 0);
727 1.1 itojun }
728 1.1 itojun
729 1.61 skrll static int
730 1.61 skrll udav_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data)
731 1.1 itojun {
732 1.61 skrll struct usbnet * const un = ifp->if_softc;
733 1.1 itojun
734 1.61 skrll switch (cmd) {
735 1.61 skrll case SIOCADDMULTI:
736 1.61 skrll case SIOCDELMULTI:
737 1.61 skrll udav_setiff(un);
738 1.61 skrll break;
739 1.61 skrll default:
740 1.61 skrll break;
741 1.1 itojun }
742 1.1 itojun
743 1.1 itojun
744 1.61 skrll return 0;
745 1.1 itojun }
746 1.1 itojun
747 1.1 itojun /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
748 1.61 skrll static void
749 1.61 skrll udav_stop_cb(struct ifnet *ifp, int disable)
750 1.1 itojun {
751 1.61 skrll struct usbnet * const un = ifp->if_softc;
752 1.1 itojun
753 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
754 1.1 itojun
755 1.61 skrll udav_reset(un);
756 1.1 itojun }
757 1.1 itojun
758 1.61 skrll static usbd_status
759 1.61 skrll udav_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
760 1.1 itojun {
761 1.56 msaitoh uint8_t data[2];
762 1.1 itojun
763 1.61 skrll usbnet_isowned_mii(un);
764 1.1 itojun
765 1.1 itojun DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
766 1.61 skrll device_xname(un->un_dev), __func__, phy, reg));
767 1.1 itojun
768 1.63 mrg if (usbnet_isdying(un)) {
769 1.1 itojun #ifdef DIAGNOSTIC
770 1.61 skrll printf("%s: %s: dying\n", device_xname(un->un_dev),
771 1.1 itojun __func__);
772 1.1 itojun #endif
773 1.61 skrll return USBD_INVAL;
774 1.1 itojun }
775 1.1 itojun
776 1.1 itojun /* XXX: one PHY only for the internal PHY */
777 1.1 itojun if (phy != 0) {
778 1.1 itojun DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
779 1.61 skrll device_xname(un->un_dev), __func__, phy));
780 1.61 skrll return USBD_INVAL;
781 1.1 itojun }
782 1.1 itojun
783 1.1 itojun /* select internal PHY and set PHY register address */
784 1.64 mrg udav_csr_write1(un, UDAV_EPAR,
785 1.1 itojun UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
786 1.1 itojun
787 1.1 itojun /* select PHY operation and start read command */
788 1.64 mrg udav_csr_write1(un, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
789 1.1 itojun
790 1.1 itojun /* XXX: should be wait? */
791 1.1 itojun
792 1.1 itojun /* end read command */
793 1.64 mrg UDAV_CLRBIT(un, UDAV_EPCR, UDAV_EPCR_ERPRR);
794 1.1 itojun
795 1.1 itojun /* retrieve the result from data registers */
796 1.64 mrg udav_csr_read(un, UDAV_EPDRL, data, 2);
797 1.1 itojun
798 1.56 msaitoh *val = data[0] | (data[1] << 8);
799 1.1 itojun
800 1.56 msaitoh DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n",
801 1.61 skrll device_xname(un->un_dev), __func__, phy, reg, *val));
802 1.1 itojun
803 1.61 skrll return USBD_NORMAL_COMPLETION;
804 1.1 itojun }
805 1.1 itojun
806 1.61 skrll static usbd_status
807 1.61 skrll udav_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
808 1.1 itojun {
809 1.56 msaitoh uint8_t data[2];
810 1.1 itojun
811 1.61 skrll usbnet_isowned_mii(un);
812 1.1 itojun
813 1.56 msaitoh DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n",
814 1.61 skrll device_xname(un->un_dev), __func__, phy, reg, val));
815 1.1 itojun
816 1.63 mrg if (usbnet_isdying(un)) {
817 1.1 itojun #ifdef DIAGNOSTIC
818 1.61 skrll printf("%s: %s: dying\n", device_xname(un->un_dev),
819 1.1 itojun __func__);
820 1.1 itojun #endif
821 1.56 msaitoh return -1;
822 1.1 itojun }
823 1.1 itojun
824 1.1 itojun /* XXX: one PHY only for the internal PHY */
825 1.1 itojun if (phy != 0) {
826 1.1 itojun DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
827 1.61 skrll device_xname(un->un_dev), __func__, phy));
828 1.56 msaitoh return -1;
829 1.1 itojun }
830 1.1 itojun
831 1.1 itojun /* select internal PHY and set PHY register address */
832 1.64 mrg udav_csr_write1(un, UDAV_EPAR,
833 1.1 itojun UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
834 1.1 itojun
835 1.1 itojun /* put the value to the data registers */
836 1.56 msaitoh data[0] = val & 0xff;
837 1.56 msaitoh data[1] = (val >> 8) & 0xff;
838 1.64 mrg udav_csr_write(un, UDAV_EPDRL, data, 2);
839 1.1 itojun
840 1.1 itojun /* select PHY operation and start write command */
841 1.64 mrg udav_csr_write1(un, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
842 1.1 itojun
843 1.1 itojun /* XXX: should be wait? */
844 1.1 itojun
845 1.1 itojun /* end write command */
846 1.64 mrg UDAV_CLRBIT(un, UDAV_EPCR, UDAV_EPCR_ERPRW);
847 1.1 itojun
848 1.61 skrll return USBD_NORMAL_COMPLETION;
849 1.1 itojun }
850 1.1 itojun
851 1.61 skrll static void
852 1.61 skrll udav_mii_statchg(struct ifnet *ifp)
853 1.1 itojun {
854 1.61 skrll struct usbnet * const un = ifp->if_softc;
855 1.61 skrll struct mii_data * const mii = usbnet_mii(un);
856 1.61 skrll
857 1.61 skrll DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
858 1.1 itojun
859 1.63 mrg if (usbnet_isdying(un))
860 1.1 itojun return;
861 1.1 itojun
862 1.61 skrll if (mii->mii_media_status & IFM_ACTIVE &&
863 1.61 skrll IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
864 1.61 skrll DPRINTF(("%s: %s: got link\n",
865 1.61 skrll device_xname(un->un_dev), __func__));
866 1.65 mrg usbnet_set_link(un, true);
867 1.61 skrll }
868 1.61 skrll }
869 1.61 skrll
870 1.61 skrll #ifdef _MODULE
871 1.61 skrll #include "ioconf.c"
872 1.61 skrll #endif
873 1.61 skrll
874 1.67 mrg USBNET_MODULE(udav)
875