if_url.c revision 1.2.2.2 1 1.2.2.2 nathanw /* $NetBSD: if_url.c,v 1.2.2.2 2002/04/01 07:47:30 nathanw Exp $ */
2 1.2.2.2 nathanw /*
3 1.2.2.2 nathanw * Copyright (c) 2001, 2002
4 1.2.2.2 nathanw * Shingo WATANABE <nabe (at) nabechan.org>. All rights reserved.
5 1.2.2.2 nathanw *
6 1.2.2.2 nathanw * Redistribution and use in source and binary forms, with or without
7 1.2.2.2 nathanw * modification, are permitted provided that the following conditions
8 1.2.2.2 nathanw * are met:
9 1.2.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
10 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer.
11 1.2.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
12 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
13 1.2.2.2 nathanw * documentation and/or other materials provided with the distribution.
14 1.2.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
15 1.2.2.2 nathanw * must display the following acknowledgement:
16 1.2.2.2 nathanw * This product includes software developed by Shingo WATANABE.
17 1.2.2.2 nathanw * 4. Neither the name of the author nor the names of any co-contributors
18 1.2.2.2 nathanw * may be used to endorse or promote products derived from this software
19 1.2.2.2 nathanw * without specific prior written permission.
20 1.2.2.2 nathanw *
21 1.2.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 1.2.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.2.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.2.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 1.2.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.2.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.2.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.2.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.2.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.2.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.2.2.2 nathanw * SUCH DAMAGE.
32 1.2.2.2 nathanw *
33 1.2.2.2 nathanw */
34 1.2.2.2 nathanw
35 1.2.2.2 nathanw /*
36 1.2.2.2 nathanw * The RTL8150L(Realtek USB to fast ethernet controller) spec can be found at
37 1.2.2.2 nathanw * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/8150v14.pdf
38 1.2.2.2 nathanw * ftp://152.104.125.40/lancard/data_sheet/8150/8150v14.pdf
39 1.2.2.2 nathanw */
40 1.2.2.2 nathanw
41 1.2.2.2 nathanw /*
42 1.2.2.2 nathanw * TODO:
43 1.2.2.2 nathanw * Interrupt Endpoint support
44 1.2.2.2 nathanw * External PHYs
45 1.2.2.2 nathanw * powerhook() support?
46 1.2.2.2 nathanw */
47 1.2.2.2 nathanw
48 1.2.2.2 nathanw #include <sys/cdefs.h>
49 1.2.2.2 nathanw __KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1.2.2.2 2002/04/01 07:47:30 nathanw Exp $");
50 1.2.2.2 nathanw
51 1.2.2.2 nathanw #include "opt_inet.h"
52 1.2.2.2 nathanw #include "opt_ns.h"
53 1.2.2.2 nathanw #include "bpfilter.h"
54 1.2.2.2 nathanw #include "rnd.h"
55 1.2.2.2 nathanw
56 1.2.2.2 nathanw #include <sys/param.h>
57 1.2.2.2 nathanw #include <sys/systm.h>
58 1.2.2.2 nathanw #include <sys/lock.h>
59 1.2.2.2 nathanw #include <sys/mbuf.h>
60 1.2.2.2 nathanw #include <sys/kernel.h>
61 1.2.2.2 nathanw #include <sys/socket.h>
62 1.2.2.2 nathanw
63 1.2.2.2 nathanw #include <sys/device.h>
64 1.2.2.2 nathanw #if NRND > 0
65 1.2.2.2 nathanw #include <sys/rnd.h>
66 1.2.2.2 nathanw #endif
67 1.2.2.2 nathanw
68 1.2.2.2 nathanw #include <net/if.h>
69 1.2.2.2 nathanw #include <net/if_arp.h>
70 1.2.2.2 nathanw #include <net/if_dl.h>
71 1.2.2.2 nathanw #include <net/if_media.h>
72 1.2.2.2 nathanw
73 1.2.2.2 nathanw #if NBPFILTER > 0
74 1.2.2.2 nathanw #include <net/bpf.h>
75 1.2.2.2 nathanw #endif
76 1.2.2.2 nathanw #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
77 1.2.2.2 nathanw
78 1.2.2.2 nathanw #include <net/if_ether.h>
79 1.2.2.2 nathanw #ifdef INET
80 1.2.2.2 nathanw #include <netinet/in.h>
81 1.2.2.2 nathanw #include <netinet/if_inarp.h>
82 1.2.2.2 nathanw #endif
83 1.2.2.2 nathanw #ifdef NS
84 1.2.2.2 nathanw #include <netns/ns.h>
85 1.2.2.2 nathanw #include <netns/ns_if.h>
86 1.2.2.2 nathanw #endif
87 1.2.2.2 nathanw
88 1.2.2.2 nathanw #include <dev/mii/mii.h>
89 1.2.2.2 nathanw #include <dev/mii/miivar.h>
90 1.2.2.2 nathanw #include <dev/mii/urlphyreg.h>
91 1.2.2.2 nathanw
92 1.2.2.2 nathanw #include <dev/usb/usb.h>
93 1.2.2.2 nathanw #include <dev/usb/usbdi.h>
94 1.2.2.2 nathanw #include <dev/usb/usbdi_util.h>
95 1.2.2.2 nathanw #include <dev/usb/usbdevs.h>
96 1.2.2.2 nathanw
97 1.2.2.2 nathanw #include <dev/usb/if_urlreg.h>
98 1.2.2.2 nathanw
99 1.2.2.2 nathanw
100 1.2.2.2 nathanw /* Function declarations */
101 1.2.2.2 nathanw USB_DECLARE_DRIVER(url);
102 1.2.2.2 nathanw
103 1.2.2.2 nathanw Static int url_openpipes(struct url_softc *);
104 1.2.2.2 nathanw Static int url_rx_list_init(struct url_softc *);
105 1.2.2.2 nathanw Static int url_tx_list_init(struct url_softc *);
106 1.2.2.2 nathanw Static int url_newbuf(struct url_softc *, struct url_chain *, struct mbuf *);
107 1.2.2.2 nathanw Static void url_start(struct ifnet *);
108 1.2.2.2 nathanw Static int url_send(struct url_softc *, struct mbuf *, int);
109 1.2.2.2 nathanw Static void url_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
110 1.2.2.2 nathanw Static void url_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
111 1.2.2.2 nathanw Static void url_tick(void *);
112 1.2.2.2 nathanw Static void url_tick_task(void *);
113 1.2.2.2 nathanw Static int url_ioctl(struct ifnet *, u_long, caddr_t);
114 1.2.2.2 nathanw Static void url_stop_task(struct url_softc *);
115 1.2.2.2 nathanw Static void url_stop(struct ifnet *, int);
116 1.2.2.2 nathanw Static void url_watchdog(struct ifnet *);
117 1.2.2.2 nathanw Static int url_ifmedia_change(struct ifnet *);
118 1.2.2.2 nathanw Static void url_ifmedia_status(struct ifnet *, struct ifmediareq *);
119 1.2.2.2 nathanw Static void url_lock_mii(struct url_softc *);
120 1.2.2.2 nathanw Static void url_unlock_mii(struct url_softc *);
121 1.2.2.2 nathanw Static int url_int_miibus_readreg(device_ptr_t, int, int);
122 1.2.2.2 nathanw Static void url_int_miibus_writereg(device_ptr_t, int, int, int);
123 1.2.2.2 nathanw Static void url_miibus_statchg(device_ptr_t);
124 1.2.2.2 nathanw Static int url_init(struct ifnet *);
125 1.2.2.2 nathanw Static void url_setmulti(struct url_softc *);
126 1.2.2.2 nathanw Static void url_reset(struct url_softc *);
127 1.2.2.2 nathanw
128 1.2.2.2 nathanw Static int url_csr_read_1(struct url_softc *, int);
129 1.2.2.2 nathanw Static int url_csr_read_2(struct url_softc *, int);
130 1.2.2.2 nathanw Static int url_csr_write_1(struct url_softc *, int, int);
131 1.2.2.2 nathanw Static int url_csr_write_2(struct url_softc *, int, int);
132 1.2.2.2 nathanw Static int url_csr_write_4(struct url_softc *, int, int);
133 1.2.2.2 nathanw Static int url_mem(struct url_softc *, int, int, void *, int);
134 1.2.2.2 nathanw
135 1.2.2.2 nathanw /* Macros */
136 1.2.2.2 nathanw #ifdef URL_DEBUG
137 1.2.2.2 nathanw #define DPRINTF(x) if (urldebug) logprintf x
138 1.2.2.2 nathanw #define DPRINTFN(n,x) if (urldebug >= (n)) logprintf x
139 1.2.2.2 nathanw int urldebug = 0;
140 1.2.2.2 nathanw #else
141 1.2.2.2 nathanw #define DPRINTF(x)
142 1.2.2.2 nathanw #define DPRINTFN(n,x)
143 1.2.2.2 nathanw #endif
144 1.2.2.2 nathanw
145 1.2.2.2 nathanw #define URL_SETBIT(sc, reg, x) \
146 1.2.2.2 nathanw url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) | (x))
147 1.2.2.2 nathanw
148 1.2.2.2 nathanw #define URL_SETBIT2(sc, reg, x) \
149 1.2.2.2 nathanw url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) | (x))
150 1.2.2.2 nathanw
151 1.2.2.2 nathanw #define URL_CLRBIT(sc, reg, x) \
152 1.2.2.2 nathanw url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) & ~(x))
153 1.2.2.2 nathanw
154 1.2.2.2 nathanw #define URL_CLRBIT2(sc, reg, x) \
155 1.2.2.2 nathanw url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) & ~(x))
156 1.2.2.2 nathanw
157 1.2.2.2 nathanw static const struct url_type {
158 1.2.2.2 nathanw struct usb_devno url_dev;
159 1.2.2.2 nathanw u_int16_t url_flags;
160 1.2.2.2 nathanw #define URL_EXT_PHY 0x0001
161 1.2.2.2 nathanw } url_devs [] = {
162 1.2.2.2 nathanw /* MELCO LUA-KTX */
163 1.2.2.2 nathanw {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0},
164 1.2.2.2 nathanw /* GREEN HOUSE USBKR100 */
165 1.2.2.2 nathanw {{ USB_VENDOR_GREENHOUSE2, USB_PRODUCT_GREENHOUSE2_USBKR100}, 0}
166 1.2.2.2 nathanw };
167 1.2.2.2 nathanw #define url_lookup(v, p) ((struct url_type *)usb_lookup(url_devs, v, p))
168 1.2.2.2 nathanw
169 1.2.2.2 nathanw
170 1.2.2.2 nathanw /* Probe */
171 1.2.2.2 nathanw USB_MATCH(url)
172 1.2.2.2 nathanw {
173 1.2.2.2 nathanw USB_MATCH_START(url, uaa);
174 1.2.2.2 nathanw
175 1.2.2.2 nathanw if (uaa->iface != NULL)
176 1.2.2.2 nathanw return (UMATCH_NONE);
177 1.2.2.2 nathanw
178 1.2.2.2 nathanw return (url_lookup(uaa->vendor, uaa->product) != NULL ?
179 1.2.2.2 nathanw UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
180 1.2.2.2 nathanw }
181 1.2.2.2 nathanw /* Attach */
182 1.2.2.2 nathanw USB_ATTACH(url)
183 1.2.2.2 nathanw {
184 1.2.2.2 nathanw USB_ATTACH_START(url, sc, uaa);
185 1.2.2.2 nathanw usbd_device_handle dev = uaa->device;
186 1.2.2.2 nathanw usbd_interface_handle iface;
187 1.2.2.2 nathanw usbd_status err;
188 1.2.2.2 nathanw usb_interface_descriptor_t *id;
189 1.2.2.2 nathanw usb_endpoint_descriptor_t *ed;
190 1.2.2.2 nathanw char devinfo[1024];
191 1.2.2.2 nathanw char *devname = USBDEVNAME(sc->sc_dev);
192 1.2.2.2 nathanw struct ifnet *ifp;
193 1.2.2.2 nathanw struct mii_data *mii;
194 1.2.2.2 nathanw u_char eaddr[ETHER_ADDR_LEN];
195 1.2.2.2 nathanw int i, s;
196 1.2.2.2 nathanw
197 1.2.2.2 nathanw usbd_devinfo(dev, 0, devinfo);
198 1.2.2.2 nathanw USB_ATTACH_SETUP;
199 1.2.2.2 nathanw printf("%s: %s\n", devname, devinfo);
200 1.2.2.2 nathanw
201 1.2.2.2 nathanw /* Move the device into the configured state. */
202 1.2.2.2 nathanw err = usbd_set_config_no(dev, URL_CONFIG_NO, 1);
203 1.2.2.2 nathanw if (err) {
204 1.2.2.2 nathanw printf("%s: setting config no failed\n", devname);
205 1.2.2.2 nathanw goto bad;
206 1.2.2.2 nathanw }
207 1.2.2.2 nathanw
208 1.2.2.2 nathanw usb_init_task(&sc->sc_tick_task, url_tick_task, sc);
209 1.2.2.2 nathanw lockinit(&sc->sc_mii_lock, PZERO, "urlmii", 0, 0);
210 1.2.2.2 nathanw usb_init_task(&sc->sc_stop_task, (void (*)(void *)) url_stop_task, sc);
211 1.2.2.2 nathanw
212 1.2.2.2 nathanw /* get control interface */
213 1.2.2.2 nathanw err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface);
214 1.2.2.2 nathanw if (err) {
215 1.2.2.2 nathanw printf("%s: failed to get interface, err=%s\n", devname,
216 1.2.2.2 nathanw usbd_errstr(err));
217 1.2.2.2 nathanw goto bad;
218 1.2.2.2 nathanw }
219 1.2.2.2 nathanw
220 1.2.2.2 nathanw sc->sc_udev = dev;
221 1.2.2.2 nathanw sc->sc_ctl_iface = iface;
222 1.2.2.2 nathanw sc->sc_flags = url_lookup(uaa->vendor, uaa->product)->url_flags;
223 1.2.2.2 nathanw
224 1.2.2.2 nathanw /* get interface descriptor */
225 1.2.2.2 nathanw id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
226 1.2.2.2 nathanw
227 1.2.2.2 nathanw /* find endpoints */
228 1.2.2.2 nathanw sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
229 1.2.2.2 nathanw for (i = 0; i < id->bNumEndpoints; i++) {
230 1.2.2.2 nathanw ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
231 1.2.2.2 nathanw if (ed == NULL) {
232 1.2.2.2 nathanw printf("%s: couldn't get endpoint %d\n", devname, i);
233 1.2.2.2 nathanw goto bad;
234 1.2.2.2 nathanw }
235 1.2.2.2 nathanw if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
236 1.2.2.2 nathanw UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
237 1.2.2.2 nathanw sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
238 1.2.2.2 nathanw else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
239 1.2.2.2 nathanw UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
240 1.2.2.2 nathanw sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
241 1.2.2.2 nathanw else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
242 1.2.2.2 nathanw UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
243 1.2.2.2 nathanw sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
244 1.2.2.2 nathanw }
245 1.2.2.2 nathanw
246 1.2.2.2 nathanw if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
247 1.2.2.2 nathanw sc->sc_intrin_no == -1) {
248 1.2.2.2 nathanw printf("%s: missing endpoint\n", devname);
249 1.2.2.2 nathanw goto bad;
250 1.2.2.2 nathanw }
251 1.2.2.2 nathanw
252 1.2.2.2 nathanw s = splnet();
253 1.2.2.2 nathanw
254 1.2.2.2 nathanw /* reset the adapter */
255 1.2.2.2 nathanw url_reset(sc);
256 1.2.2.2 nathanw
257 1.2.2.2 nathanw /* Get Ethernet Address */
258 1.2.2.2 nathanw err = url_mem(sc, URL_CMD_READMEM, URL_IDR0, (void *)eaddr,
259 1.2.2.2 nathanw ETHER_ADDR_LEN);
260 1.2.2.2 nathanw if (err) {
261 1.2.2.2 nathanw printf("%s: read MAC address faild\n", devname);
262 1.2.2.2 nathanw splx(s);
263 1.2.2.2 nathanw goto bad;
264 1.2.2.2 nathanw }
265 1.2.2.2 nathanw
266 1.2.2.2 nathanw /* Print Ethernet Address */
267 1.2.2.2 nathanw printf("%s: Ethernet address %s\n", devname, ether_sprintf(eaddr));
268 1.2.2.2 nathanw
269 1.2.2.2 nathanw /* initialize interface infomation */
270 1.2.2.2 nathanw ifp = GET_IFP(sc);
271 1.2.2.2 nathanw ifp->if_softc = sc;
272 1.2.2.2 nathanw strncpy(ifp->if_xname, devname, IFNAMSIZ);
273 1.2.2.2 nathanw ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
274 1.2.2.2 nathanw ifp->if_start = url_start;
275 1.2.2.2 nathanw ifp->if_ioctl = url_ioctl;
276 1.2.2.2 nathanw ifp->if_watchdog = url_watchdog;
277 1.2.2.2 nathanw ifp->if_init = url_init;
278 1.2.2.2 nathanw ifp->if_stop = url_stop;
279 1.2.2.2 nathanw
280 1.2.2.2 nathanw IFQ_SET_READY(&ifp->if_snd);
281 1.2.2.2 nathanw
282 1.2.2.2 nathanw /*
283 1.2.2.2 nathanw * Do ifmedia setup.
284 1.2.2.2 nathanw */
285 1.2.2.2 nathanw mii = &sc->sc_mii;
286 1.2.2.2 nathanw mii->mii_ifp = ifp;
287 1.2.2.2 nathanw mii->mii_readreg = url_int_miibus_readreg;
288 1.2.2.2 nathanw mii->mii_writereg = url_int_miibus_writereg;
289 1.2.2.2 nathanw #if 0
290 1.2.2.2 nathanw if (sc->sc_flags & URL_EXT_PHY) {
291 1.2.2.2 nathanw mii->mii_readreg = url_ext_miibus_readreg;
292 1.2.2.2 nathanw mii->mii_writereg = url_ext_miibus_writereg;
293 1.2.2.2 nathanw }
294 1.2.2.2 nathanw #endif
295 1.2.2.2 nathanw mii->mii_statchg = url_miibus_statchg;
296 1.2.2.2 nathanw mii->mii_flags = MIIF_AUTOTSLEEP;
297 1.2.2.2 nathanw ifmedia_init(&mii->mii_media, 0,
298 1.2.2.2 nathanw url_ifmedia_change, url_ifmedia_status);
299 1.2.2.2 nathanw mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
300 1.2.2.2 nathanw if (LIST_FIRST(&mii->mii_phys) == NULL) {
301 1.2.2.2 nathanw ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
302 1.2.2.2 nathanw ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
303 1.2.2.2 nathanw } else
304 1.2.2.2 nathanw ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
305 1.2.2.2 nathanw
306 1.2.2.2 nathanw /* attach the interface */
307 1.2.2.2 nathanw if_attach(ifp);
308 1.2.2.2 nathanw Ether_ifattach(ifp, eaddr);
309 1.2.2.2 nathanw
310 1.2.2.2 nathanw #if NRND > 0
311 1.2.2.2 nathanw rnd_attach_source(&sc->rnd_source, devname, RND_TYPE_NET, 0);
312 1.2.2.2 nathanw #endif
313 1.2.2.2 nathanw
314 1.2.2.2 nathanw usb_callout_init(sc->sc_stat_ch);
315 1.2.2.2 nathanw sc->sc_attached = 1;
316 1.2.2.2 nathanw splx(s);
317 1.2.2.2 nathanw
318 1.2.2.2 nathanw usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
319 1.2.2.2 nathanw
320 1.2.2.2 nathanw USB_ATTACH_SUCCESS_RETURN;
321 1.2.2.2 nathanw
322 1.2.2.2 nathanw bad:
323 1.2.2.2 nathanw sc->sc_dying = 1;
324 1.2.2.2 nathanw USB_ATTACH_ERROR_RETURN;
325 1.2.2.2 nathanw }
326 1.2.2.2 nathanw
327 1.2.2.2 nathanw /* detach */
328 1.2.2.2 nathanw USB_DETACH(url)
329 1.2.2.2 nathanw {
330 1.2.2.2 nathanw USB_DETACH_START(url, sc);
331 1.2.2.2 nathanw struct ifnet *ifp = GET_IFP(sc);
332 1.2.2.2 nathanw int s;
333 1.2.2.2 nathanw
334 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
335 1.2.2.2 nathanw
336 1.2.2.2 nathanw /* Detached before attached finished */
337 1.2.2.2 nathanw if (!sc->sc_attached)
338 1.2.2.2 nathanw return (0);
339 1.2.2.2 nathanw
340 1.2.2.2 nathanw usb_uncallout(sc->sc_stat_ch, url_tick, sc);
341 1.2.2.2 nathanw
342 1.2.2.2 nathanw /* Remove any pending tasks */
343 1.2.2.2 nathanw usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
344 1.2.2.2 nathanw usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
345 1.2.2.2 nathanw
346 1.2.2.2 nathanw s = splusb();
347 1.2.2.2 nathanw
348 1.2.2.2 nathanw if (--sc->sc_refcnt >= 0) {
349 1.2.2.2 nathanw /* Wait for processes to go away */
350 1.2.2.2 nathanw usb_detach_wait(USBDEV(sc->sc_dev));
351 1.2.2.2 nathanw }
352 1.2.2.2 nathanw
353 1.2.2.2 nathanw if (ifp->if_flags & IFF_RUNNING)
354 1.2.2.2 nathanw url_stop(GET_IFP(sc), 1);
355 1.2.2.2 nathanw
356 1.2.2.2 nathanw #if NRND > 0
357 1.2.2.2 nathanw rnd_detach_source(&sc->rnd_source);
358 1.2.2.2 nathanw #endif
359 1.2.2.2 nathanw mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
360 1.2.2.2 nathanw ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
361 1.2.2.2 nathanw ether_ifdetach(ifp);
362 1.2.2.2 nathanw if_detach(ifp);
363 1.2.2.2 nathanw
364 1.2.2.2 nathanw #ifdef DIAGNOSTIC
365 1.2.2.2 nathanw if (sc->sc_pipe_tx != NULL)
366 1.2.2.2 nathanw printf("%s: detach has active tx endpoint.\n",
367 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev));
368 1.2.2.2 nathanw if (sc->sc_pipe_rx != NULL)
369 1.2.2.2 nathanw printf("%s: detach has active rx endpoint.\n",
370 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev));
371 1.2.2.2 nathanw if (sc->sc_pipe_intr != NULL)
372 1.2.2.2 nathanw printf("%s: detach has active intr endpoint.\n",
373 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev));
374 1.2.2.2 nathanw #endif
375 1.2.2.2 nathanw
376 1.2.2.2 nathanw sc->sc_attached = 0;
377 1.2.2.2 nathanw
378 1.2.2.2 nathanw splx(s);
379 1.2.2.2 nathanw
380 1.2.2.2 nathanw usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
381 1.2.2.2 nathanw USBDEV(sc->sc_dev));
382 1.2.2.2 nathanw
383 1.2.2.2 nathanw return (0);
384 1.2.2.2 nathanw }
385 1.2.2.2 nathanw
386 1.2.2.2 nathanw /* read/write memory */
387 1.2.2.2 nathanw Static int
388 1.2.2.2 nathanw url_mem(struct url_softc *sc, int cmd, int offset, void *buf, int len)
389 1.2.2.2 nathanw {
390 1.2.2.2 nathanw usb_device_request_t req;
391 1.2.2.2 nathanw usbd_status err;
392 1.2.2.2 nathanw
393 1.2.2.2 nathanw if (sc == NULL)
394 1.2.2.2 nathanw return (0);
395 1.2.2.2 nathanw
396 1.2.2.2 nathanw DPRINTFN(0x200,
397 1.2.2.2 nathanw ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
398 1.2.2.2 nathanw
399 1.2.2.2 nathanw if (sc->sc_dying)
400 1.2.2.2 nathanw return (0);
401 1.2.2.2 nathanw
402 1.2.2.2 nathanw if (cmd == URL_CMD_READMEM)
403 1.2.2.2 nathanw req.bmRequestType = UT_READ_VENDOR_DEVICE;
404 1.2.2.2 nathanw else
405 1.2.2.2 nathanw req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
406 1.2.2.2 nathanw req.bRequest = URL_REQ_MEM;
407 1.2.2.2 nathanw USETW(req.wValue, offset);
408 1.2.2.2 nathanw USETW(req.wIndex, 0x0000);
409 1.2.2.2 nathanw USETW(req.wLength, len);
410 1.2.2.2 nathanw
411 1.2.2.2 nathanw sc->sc_refcnt++;
412 1.2.2.2 nathanw err = usbd_do_request(sc->sc_udev, &req, buf);
413 1.2.2.2 nathanw if (--sc->sc_refcnt < 0)
414 1.2.2.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
415 1.2.2.2 nathanw if (err) {
416 1.2.2.2 nathanw DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n",
417 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev),
418 1.2.2.2 nathanw cmd == URL_CMD_READMEM ? "read" : "write",
419 1.2.2.2 nathanw offset, err));
420 1.2.2.2 nathanw }
421 1.2.2.2 nathanw
422 1.2.2.2 nathanw return (err);
423 1.2.2.2 nathanw }
424 1.2.2.2 nathanw
425 1.2.2.2 nathanw /* read 1byte from register */
426 1.2.2.2 nathanw Static int
427 1.2.2.2 nathanw url_csr_read_1(struct url_softc *sc, int reg)
428 1.2.2.2 nathanw {
429 1.2.2.2 nathanw u_int8_t val = 0;
430 1.2.2.2 nathanw
431 1.2.2.2 nathanw DPRINTFN(0x100,
432 1.2.2.2 nathanw ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
433 1.2.2.2 nathanw
434 1.2.2.2 nathanw if (sc->sc_dying)
435 1.2.2.2 nathanw return (0);
436 1.2.2.2 nathanw
437 1.2.2.2 nathanw return (url_mem(sc, URL_CMD_READMEM, reg, &val, 1) ? 0 : val);
438 1.2.2.2 nathanw }
439 1.2.2.2 nathanw
440 1.2.2.2 nathanw /* read 2bytes from register */
441 1.2.2.2 nathanw Static int
442 1.2.2.2 nathanw url_csr_read_2(struct url_softc *sc, int reg)
443 1.2.2.2 nathanw {
444 1.2.2.2 nathanw uWord val;
445 1.2.2.2 nathanw
446 1.2.2.2 nathanw DPRINTFN(0x100,
447 1.2.2.2 nathanw ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
448 1.2.2.2 nathanw
449 1.2.2.2 nathanw if (sc->sc_dying)
450 1.2.2.2 nathanw return (0);
451 1.2.2.2 nathanw
452 1.2.2.2 nathanw USETW(val, 0);
453 1.2.2.2 nathanw return (url_mem(sc, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val));
454 1.2.2.2 nathanw }
455 1.2.2.2 nathanw
456 1.2.2.2 nathanw /* write 1byte to register */
457 1.2.2.2 nathanw Static int
458 1.2.2.2 nathanw url_csr_write_1(struct url_softc *sc, int reg, int aval)
459 1.2.2.2 nathanw {
460 1.2.2.2 nathanw u_int8_t val = aval;
461 1.2.2.2 nathanw
462 1.2.2.2 nathanw DPRINTFN(0x100,
463 1.2.2.2 nathanw ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
464 1.2.2.2 nathanw
465 1.2.2.2 nathanw if (sc->sc_dying)
466 1.2.2.2 nathanw return (0);
467 1.2.2.2 nathanw
468 1.2.2.2 nathanw return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0);
469 1.2.2.2 nathanw }
470 1.2.2.2 nathanw
471 1.2.2.2 nathanw /* write 2bytes to register */
472 1.2.2.2 nathanw Static int
473 1.2.2.2 nathanw url_csr_write_2(struct url_softc *sc, int reg, int aval)
474 1.2.2.2 nathanw {
475 1.2.2.2 nathanw uWord val;
476 1.2.2.2 nathanw
477 1.2.2.2 nathanw DPRINTFN(0x100,
478 1.2.2.2 nathanw ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
479 1.2.2.2 nathanw
480 1.2.2.2 nathanw USETW(val, aval);
481 1.2.2.2 nathanw
482 1.2.2.2 nathanw if (sc->sc_dying)
483 1.2.2.2 nathanw return (0);
484 1.2.2.2 nathanw
485 1.2.2.2 nathanw return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0);
486 1.2.2.2 nathanw }
487 1.2.2.2 nathanw
488 1.2.2.2 nathanw /* write 4bytes to register */
489 1.2.2.2 nathanw Static int
490 1.2.2.2 nathanw url_csr_write_4(struct url_softc *sc, int reg, int aval)
491 1.2.2.2 nathanw {
492 1.2.2.2 nathanw uDWord val;
493 1.2.2.2 nathanw
494 1.2.2.2 nathanw DPRINTFN(0x100,
495 1.2.2.2 nathanw ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
496 1.2.2.2 nathanw
497 1.2.2.2 nathanw USETDW(val, aval);
498 1.2.2.2 nathanw
499 1.2.2.2 nathanw if (sc->sc_dying)
500 1.2.2.2 nathanw return (0);
501 1.2.2.2 nathanw
502 1.2.2.2 nathanw return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0);
503 1.2.2.2 nathanw }
504 1.2.2.2 nathanw
505 1.2.2.2 nathanw Static int
506 1.2.2.2 nathanw url_init(struct ifnet *ifp)
507 1.2.2.2 nathanw {
508 1.2.2.2 nathanw struct url_softc *sc = ifp->if_softc;
509 1.2.2.2 nathanw struct mii_data *mii = GET_MII(sc);
510 1.2.2.2 nathanw u_char *eaddr;
511 1.2.2.2 nathanw int i, s;
512 1.2.2.2 nathanw
513 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
514 1.2.2.2 nathanw
515 1.2.2.2 nathanw if (sc->sc_dying)
516 1.2.2.2 nathanw return (EIO);
517 1.2.2.2 nathanw
518 1.2.2.2 nathanw s = splnet();
519 1.2.2.2 nathanw
520 1.2.2.2 nathanw /* Cancel pending I/O and free all TX/RX buffers */
521 1.2.2.2 nathanw url_stop(ifp, 1);
522 1.2.2.2 nathanw
523 1.2.2.2 nathanw eaddr = LLADDR(ifp->if_sadl);
524 1.2.2.2 nathanw for (i = 0; i < ETHER_ADDR_LEN; i++)
525 1.2.2.2 nathanw url_csr_write_1(sc, URL_IDR0 + i, eaddr[i]);
526 1.2.2.2 nathanw
527 1.2.2.2 nathanw /* Init transmission control register */
528 1.2.2.2 nathanw URL_CLRBIT(sc, URL_TCR,
529 1.2.2.2 nathanw URL_TCR_TXRR1 | URL_TCR_TXRR0 |
530 1.2.2.2 nathanw URL_TCR_IFG1 | URL_TCR_IFG0 |
531 1.2.2.2 nathanw URL_TCR_NOCRC);
532 1.2.2.2 nathanw
533 1.2.2.2 nathanw /* Init receive control register */
534 1.2.2.2 nathanw URL_SETBIT2(sc, URL_RCR, URL_RCR_TAIL | URL_RCR_AD);
535 1.2.2.2 nathanw if (ifp->if_flags & IFF_BROADCAST)
536 1.2.2.2 nathanw URL_SETBIT2(sc, URL_RCR, URL_RCR_AB);
537 1.2.2.2 nathanw else
538 1.2.2.2 nathanw URL_CLRBIT2(sc, URL_RCR, URL_RCR_AB);
539 1.2.2.2 nathanw
540 1.2.2.2 nathanw /* If we want promiscuous mode, accept all physical frames. */
541 1.2.2.2 nathanw if (ifp->if_flags & IFF_PROMISC)
542 1.2.2.2 nathanw URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
543 1.2.2.2 nathanw else
544 1.2.2.2 nathanw URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
545 1.2.2.2 nathanw
546 1.2.2.2 nathanw
547 1.2.2.2 nathanw /* Initialize transmit ring */
548 1.2.2.2 nathanw if (url_tx_list_init(sc) == ENOBUFS) {
549 1.2.2.2 nathanw printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev));
550 1.2.2.2 nathanw splx(s);
551 1.2.2.2 nathanw return (EIO);
552 1.2.2.2 nathanw }
553 1.2.2.2 nathanw
554 1.2.2.2 nathanw /* Initialize receive ring */
555 1.2.2.2 nathanw if (url_rx_list_init(sc) == ENOBUFS) {
556 1.2.2.2 nathanw printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev));
557 1.2.2.2 nathanw splx(s);
558 1.2.2.2 nathanw return (EIO);
559 1.2.2.2 nathanw }
560 1.2.2.2 nathanw
561 1.2.2.2 nathanw /* Load the multicast filter */
562 1.2.2.2 nathanw url_setmulti(sc);
563 1.2.2.2 nathanw
564 1.2.2.2 nathanw /* Enable RX and TX */
565 1.2.2.2 nathanw URL_SETBIT(sc, URL_CR, URL_CR_TE | URL_CR_RE);
566 1.2.2.2 nathanw
567 1.2.2.2 nathanw mii_mediachg(mii);
568 1.2.2.2 nathanw
569 1.2.2.2 nathanw if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
570 1.2.2.2 nathanw if (url_openpipes(sc)) {
571 1.2.2.2 nathanw splx(s);
572 1.2.2.2 nathanw return (EIO);
573 1.2.2.2 nathanw }
574 1.2.2.2 nathanw }
575 1.2.2.2 nathanw
576 1.2.2.2 nathanw ifp->if_flags |= IFF_RUNNING;
577 1.2.2.2 nathanw ifp->if_flags &= ~IFF_OACTIVE;
578 1.2.2.2 nathanw
579 1.2.2.2 nathanw splx(s);
580 1.2.2.2 nathanw
581 1.2.2.2 nathanw usb_callout(sc->sc_stat_ch, hz, url_tick, sc);
582 1.2.2.2 nathanw
583 1.2.2.2 nathanw return (0);
584 1.2.2.2 nathanw }
585 1.2.2.2 nathanw
586 1.2.2.2 nathanw Static void
587 1.2.2.2 nathanw url_reset(struct url_softc *sc)
588 1.2.2.2 nathanw {
589 1.2.2.2 nathanw int i;
590 1.2.2.2 nathanw
591 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
592 1.2.2.2 nathanw
593 1.2.2.2 nathanw if (sc->sc_dying)
594 1.2.2.2 nathanw return;
595 1.2.2.2 nathanw
596 1.2.2.2 nathanw URL_SETBIT(sc, URL_CR, URL_CR_SOFT_RST);
597 1.2.2.2 nathanw
598 1.2.2.2 nathanw for (i = 0; i < URL_TX_TIMEOUT; i++) {
599 1.2.2.2 nathanw if (!(url_csr_read_1(sc, URL_CR) & URL_CR_SOFT_RST))
600 1.2.2.2 nathanw break;
601 1.2.2.2 nathanw delay(10); /* XXX */
602 1.2.2.2 nathanw }
603 1.2.2.2 nathanw
604 1.2.2.2 nathanw delay(10000); /* XXX */
605 1.2.2.2 nathanw }
606 1.2.2.2 nathanw
607 1.2.2.2 nathanw int
608 1.2.2.2 nathanw url_activate(device_ptr_t self, enum devact act)
609 1.2.2.2 nathanw {
610 1.2.2.2 nathanw struct url_softc *sc = (struct url_softc *)self;
611 1.2.2.2 nathanw
612 1.2.2.2 nathanw DPRINTF(("%s: %s: enter, act=%d\n", USBDEVNAME(sc->sc_dev),
613 1.2.2.2 nathanw __FUNCTION__, act));
614 1.2.2.2 nathanw
615 1.2.2.2 nathanw switch (act) {
616 1.2.2.2 nathanw case DVACT_ACTIVATE:
617 1.2.2.2 nathanw return (EOPNOTSUPP);
618 1.2.2.2 nathanw break;
619 1.2.2.2 nathanw
620 1.2.2.2 nathanw case DVACT_DEACTIVATE:
621 1.2.2.2 nathanw if_deactivate(&sc->sc_ec.ec_if);
622 1.2.2.2 nathanw sc->sc_dying = 1;
623 1.2.2.2 nathanw break;
624 1.2.2.2 nathanw }
625 1.2.2.2 nathanw
626 1.2.2.2 nathanw return (0);
627 1.2.2.2 nathanw }
628 1.2.2.2 nathanw
629 1.2.2.2 nathanw #define url_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
630 1.2.2.2 nathanw
631 1.2.2.2 nathanw
632 1.2.2.2 nathanw Static void
633 1.2.2.2 nathanw url_setmulti(struct url_softc *sc)
634 1.2.2.2 nathanw {
635 1.2.2.2 nathanw struct ifnet *ifp;
636 1.2.2.2 nathanw struct ether_multi *enm;
637 1.2.2.2 nathanw struct ether_multistep step;
638 1.2.2.2 nathanw u_int32_t hashes[2] = { 0, 0 };
639 1.2.2.2 nathanw int h = 0;
640 1.2.2.2 nathanw int mcnt = 0;
641 1.2.2.2 nathanw
642 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
643 1.2.2.2 nathanw
644 1.2.2.2 nathanw if (sc->sc_dying)
645 1.2.2.2 nathanw return;
646 1.2.2.2 nathanw
647 1.2.2.2 nathanw ifp = GET_IFP(sc);
648 1.2.2.2 nathanw
649 1.2.2.2 nathanw if (ifp->if_flags & IFF_PROMISC) {
650 1.2.2.2 nathanw URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
651 1.2.2.2 nathanw return;
652 1.2.2.2 nathanw } else if (ifp->if_flags & IFF_ALLMULTI) {
653 1.2.2.2 nathanw allmulti:
654 1.2.2.2 nathanw ifp->if_flags |= IFF_ALLMULTI;
655 1.2.2.2 nathanw URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM);
656 1.2.2.2 nathanw URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAP);
657 1.2.2.2 nathanw return;
658 1.2.2.2 nathanw }
659 1.2.2.2 nathanw
660 1.2.2.2 nathanw /* first, zot all the existing hash bits */
661 1.2.2.2 nathanw url_csr_write_4(sc, URL_MAR0, 0);
662 1.2.2.2 nathanw url_csr_write_4(sc, URL_MAR4, 0);
663 1.2.2.2 nathanw
664 1.2.2.2 nathanw /* now program new ones */
665 1.2.2.2 nathanw ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
666 1.2.2.2 nathanw while (enm != NULL) {
667 1.2.2.2 nathanw if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
668 1.2.2.2 nathanw ETHER_ADDR_LEN) != 0)
669 1.2.2.2 nathanw goto allmulti;
670 1.2.2.2 nathanw
671 1.2.2.2 nathanw h = url_calchash(enm->enm_addrlo);
672 1.2.2.2 nathanw if (h < 32)
673 1.2.2.2 nathanw hashes[0] |= (1 << h);
674 1.2.2.2 nathanw else
675 1.2.2.2 nathanw hashes[1] |= (1 << (h -32));
676 1.2.2.2 nathanw mcnt++;
677 1.2.2.2 nathanw ETHER_NEXT_MULTI(step, enm);
678 1.2.2.2 nathanw }
679 1.2.2.2 nathanw
680 1.2.2.2 nathanw ifp->if_flags &= ~IFF_ALLMULTI;
681 1.2.2.2 nathanw
682 1.2.2.2 nathanw URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
683 1.2.2.2 nathanw
684 1.2.2.2 nathanw if (mcnt){
685 1.2.2.2 nathanw URL_SETBIT2(sc, URL_RCR, URL_RCR_AM);
686 1.2.2.2 nathanw } else {
687 1.2.2.2 nathanw URL_CLRBIT2(sc, URL_RCR, URL_RCR_AM);
688 1.2.2.2 nathanw }
689 1.2.2.2 nathanw url_csr_write_4(sc, URL_MAR0, hashes[0]);
690 1.2.2.2 nathanw url_csr_write_4(sc, URL_MAR4, hashes[1]);
691 1.2.2.2 nathanw }
692 1.2.2.2 nathanw
693 1.2.2.2 nathanw Static int
694 1.2.2.2 nathanw url_openpipes(struct url_softc *sc)
695 1.2.2.2 nathanw {
696 1.2.2.2 nathanw struct url_chain *c;
697 1.2.2.2 nathanw usbd_status err;
698 1.2.2.2 nathanw int i;
699 1.2.2.2 nathanw int error = 0;
700 1.2.2.2 nathanw
701 1.2.2.2 nathanw if (sc->sc_dying)
702 1.2.2.2 nathanw return (EIO);
703 1.2.2.2 nathanw
704 1.2.2.2 nathanw sc->sc_refcnt++;
705 1.2.2.2 nathanw
706 1.2.2.2 nathanw /* Open RX pipe */
707 1.2.2.2 nathanw err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
708 1.2.2.2 nathanw USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
709 1.2.2.2 nathanw if (err) {
710 1.2.2.2 nathanw printf("%s: open rx pipe failed: %s\n",
711 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), usbd_errstr(err));
712 1.2.2.2 nathanw error = EIO;
713 1.2.2.2 nathanw goto done;
714 1.2.2.2 nathanw }
715 1.2.2.2 nathanw
716 1.2.2.2 nathanw /* Open TX pipe */
717 1.2.2.2 nathanw err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
718 1.2.2.2 nathanw USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
719 1.2.2.2 nathanw if (err) {
720 1.2.2.2 nathanw printf("%s: open tx pipe failed: %s\n",
721 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), usbd_errstr(err));
722 1.2.2.2 nathanw error = EIO;
723 1.2.2.2 nathanw goto done;
724 1.2.2.2 nathanw }
725 1.2.2.2 nathanw
726 1.2.2.2 nathanw #if 0
727 1.2.2.2 nathanw /* XXX: interrupt endpoint is not yet supported */
728 1.2.2.2 nathanw /* Open Interrupt pipe */
729 1.2.2.2 nathanw err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
730 1.2.2.2 nathanw USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
731 1.2.2.2 nathanw &sc->sc_cdata.url_ibuf, URL_INTR_PKGLEN,
732 1.2.2.2 nathanw url_intr, URL_INTR_INTERVAL);
733 1.2.2.2 nathanw if (err) {
734 1.2.2.2 nathanw printf("%s: open intr pipe failed: %s\n",
735 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), usbd_errstr(err));
736 1.2.2.2 nathanw error = EIO;
737 1.2.2.2 nathanw goto done;
738 1.2.2.2 nathanw }
739 1.2.2.2 nathanw #endif
740 1.2.2.2 nathanw
741 1.2.2.2 nathanw
742 1.2.2.2 nathanw /* Start up the receive pipe. */
743 1.2.2.2 nathanw for (i = 0; i < URL_RX_LIST_CNT; i++) {
744 1.2.2.2 nathanw c = &sc->sc_cdata.url_rx_chain[i];
745 1.2.2.2 nathanw usbd_setup_xfer(c->url_xfer, sc->sc_pipe_rx,
746 1.2.2.2 nathanw c, c->url_buf, URL_BUFSZ,
747 1.2.2.2 nathanw USBD_SHORT_XFER_OK | USBD_NO_COPY,
748 1.2.2.2 nathanw USBD_NO_TIMEOUT, url_rxeof);
749 1.2.2.2 nathanw (void)usbd_transfer(c->url_xfer);
750 1.2.2.2 nathanw DPRINTF(("%s: %s: start read\n", USBDEVNAME(sc->sc_dev),
751 1.2.2.2 nathanw __FUNCTION__));
752 1.2.2.2 nathanw }
753 1.2.2.2 nathanw
754 1.2.2.2 nathanw done:
755 1.2.2.2 nathanw if (--sc->sc_refcnt < 0)
756 1.2.2.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
757 1.2.2.2 nathanw
758 1.2.2.2 nathanw return (error);
759 1.2.2.2 nathanw }
760 1.2.2.2 nathanw
761 1.2.2.2 nathanw Static int
762 1.2.2.2 nathanw url_newbuf(struct url_softc *sc, struct url_chain *c, struct mbuf *m)
763 1.2.2.2 nathanw {
764 1.2.2.2 nathanw struct mbuf *m_new = NULL;
765 1.2.2.2 nathanw
766 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
767 1.2.2.2 nathanw
768 1.2.2.2 nathanw if (m == NULL) {
769 1.2.2.2 nathanw MGETHDR(m_new, M_DONTWAIT, MT_DATA);
770 1.2.2.2 nathanw if (m_new == NULL) {
771 1.2.2.2 nathanw printf("%s: no memory for rx list "
772 1.2.2.2 nathanw "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
773 1.2.2.2 nathanw return (ENOBUFS);
774 1.2.2.2 nathanw }
775 1.2.2.2 nathanw MCLGET(m_new, M_DONTWAIT);
776 1.2.2.2 nathanw if (!(m_new->m_flags & M_EXT)) {
777 1.2.2.2 nathanw printf("%s: no memory for rx list "
778 1.2.2.2 nathanw "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
779 1.2.2.2 nathanw m_freem(m_new);
780 1.2.2.2 nathanw return (ENOBUFS);
781 1.2.2.2 nathanw }
782 1.2.2.2 nathanw m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
783 1.2.2.2 nathanw } else {
784 1.2.2.2 nathanw m_new = m;
785 1.2.2.2 nathanw m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
786 1.2.2.2 nathanw m_new->m_data = m_new->m_ext.ext_buf;
787 1.2.2.2 nathanw }
788 1.2.2.2 nathanw
789 1.2.2.2 nathanw m_adj(m_new, ETHER_ALIGN);
790 1.2.2.2 nathanw c->url_mbuf = m_new;
791 1.2.2.2 nathanw
792 1.2.2.2 nathanw return (0);
793 1.2.2.2 nathanw }
794 1.2.2.2 nathanw
795 1.2.2.2 nathanw
796 1.2.2.2 nathanw Static int
797 1.2.2.2 nathanw url_rx_list_init(struct url_softc *sc)
798 1.2.2.2 nathanw {
799 1.2.2.2 nathanw struct url_cdata *cd;
800 1.2.2.2 nathanw struct url_chain *c;
801 1.2.2.2 nathanw int i;
802 1.2.2.2 nathanw
803 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
804 1.2.2.2 nathanw
805 1.2.2.2 nathanw cd = &sc->sc_cdata;
806 1.2.2.2 nathanw for (i = 0; i < URL_RX_LIST_CNT; i++) {
807 1.2.2.2 nathanw c = &cd->url_rx_chain[i];
808 1.2.2.2 nathanw c->url_sc = sc;
809 1.2.2.2 nathanw c->url_idx = i;
810 1.2.2.2 nathanw if (url_newbuf(sc, c, NULL) == ENOBUFS)
811 1.2.2.2 nathanw return (ENOBUFS);
812 1.2.2.2 nathanw if (c->url_xfer == NULL) {
813 1.2.2.2 nathanw c->url_xfer = usbd_alloc_xfer(sc->sc_udev);
814 1.2.2.2 nathanw if (c->url_xfer == NULL)
815 1.2.2.2 nathanw return (ENOBUFS);
816 1.2.2.2 nathanw c->url_buf = usbd_alloc_buffer(c->url_xfer, URL_BUFSZ);
817 1.2.2.2 nathanw if (c->url_buf == NULL) {
818 1.2.2.2 nathanw usbd_free_xfer(c->url_xfer);
819 1.2.2.2 nathanw return (ENOBUFS);
820 1.2.2.2 nathanw }
821 1.2.2.2 nathanw }
822 1.2.2.2 nathanw }
823 1.2.2.2 nathanw
824 1.2.2.2 nathanw return (0);
825 1.2.2.2 nathanw }
826 1.2.2.2 nathanw
827 1.2.2.2 nathanw Static int
828 1.2.2.2 nathanw url_tx_list_init(struct url_softc *sc)
829 1.2.2.2 nathanw {
830 1.2.2.2 nathanw struct url_cdata *cd;
831 1.2.2.2 nathanw struct url_chain *c;
832 1.2.2.2 nathanw int i;
833 1.2.2.2 nathanw
834 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
835 1.2.2.2 nathanw
836 1.2.2.2 nathanw cd = &sc->sc_cdata;
837 1.2.2.2 nathanw for (i = 0; i < URL_TX_LIST_CNT; i++) {
838 1.2.2.2 nathanw c = &cd->url_tx_chain[i];
839 1.2.2.2 nathanw c->url_sc = sc;
840 1.2.2.2 nathanw c->url_idx = i;
841 1.2.2.2 nathanw c->url_mbuf = NULL;
842 1.2.2.2 nathanw if (c->url_xfer == NULL) {
843 1.2.2.2 nathanw c->url_xfer = usbd_alloc_xfer(sc->sc_udev);
844 1.2.2.2 nathanw if (c->url_xfer == NULL)
845 1.2.2.2 nathanw return (ENOBUFS);
846 1.2.2.2 nathanw c->url_buf = usbd_alloc_buffer(c->url_xfer, URL_BUFSZ);
847 1.2.2.2 nathanw if (c->url_buf == NULL) {
848 1.2.2.2 nathanw usbd_free_xfer(c->url_xfer);
849 1.2.2.2 nathanw return (ENOBUFS);
850 1.2.2.2 nathanw }
851 1.2.2.2 nathanw }
852 1.2.2.2 nathanw }
853 1.2.2.2 nathanw
854 1.2.2.2 nathanw return (0);
855 1.2.2.2 nathanw }
856 1.2.2.2 nathanw
857 1.2.2.2 nathanw Static void
858 1.2.2.2 nathanw url_start(struct ifnet *ifp)
859 1.2.2.2 nathanw {
860 1.2.2.2 nathanw struct url_softc *sc = ifp->if_softc;
861 1.2.2.2 nathanw struct mbuf *m_head = NULL;
862 1.2.2.2 nathanw
863 1.2.2.2 nathanw DPRINTF(("%s: %s: enter, link=%d\n", USBDEVNAME(sc->sc_dev),
864 1.2.2.2 nathanw __FUNCTION__, sc->sc_link));
865 1.2.2.2 nathanw
866 1.2.2.2 nathanw if (sc->sc_dying)
867 1.2.2.2 nathanw return;
868 1.2.2.2 nathanw
869 1.2.2.2 nathanw if (!sc->sc_link)
870 1.2.2.2 nathanw return;
871 1.2.2.2 nathanw
872 1.2.2.2 nathanw if (ifp->if_flags & IFF_OACTIVE)
873 1.2.2.2 nathanw return;
874 1.2.2.2 nathanw
875 1.2.2.2 nathanw IFQ_POLL(&ifp->if_snd, m_head);
876 1.2.2.2 nathanw if (m_head == NULL)
877 1.2.2.2 nathanw return;
878 1.2.2.2 nathanw
879 1.2.2.2 nathanw if (url_send(sc, m_head, 0)) {
880 1.2.2.2 nathanw ifp->if_flags |= IFF_OACTIVE;
881 1.2.2.2 nathanw return;
882 1.2.2.2 nathanw }
883 1.2.2.2 nathanw
884 1.2.2.2 nathanw IFQ_DEQUEUE(&ifp->if_snd, m_head);
885 1.2.2.2 nathanw
886 1.2.2.2 nathanw #if NBPFILTER > 0
887 1.2.2.2 nathanw if (ifp->if_bpf)
888 1.2.2.2 nathanw bpf_mtap(ifp->if_bpf, m_head);
889 1.2.2.2 nathanw #endif
890 1.2.2.2 nathanw
891 1.2.2.2 nathanw ifp->if_flags |= IFF_OACTIVE;
892 1.2.2.2 nathanw
893 1.2.2.2 nathanw /* Set a timeout in case the chip goes out to lunch. */
894 1.2.2.2 nathanw ifp->if_timer = 5;
895 1.2.2.2 nathanw }
896 1.2.2.2 nathanw
897 1.2.2.2 nathanw Static int
898 1.2.2.2 nathanw url_send(struct url_softc *sc, struct mbuf *m, int idx)
899 1.2.2.2 nathanw {
900 1.2.2.2 nathanw int total_len;
901 1.2.2.2 nathanw struct url_chain *c;
902 1.2.2.2 nathanw usbd_status err;
903 1.2.2.2 nathanw
904 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
905 1.2.2.2 nathanw
906 1.2.2.2 nathanw c = &sc->sc_cdata.url_tx_chain[idx];
907 1.2.2.2 nathanw
908 1.2.2.2 nathanw /* Copy the mbuf data into a contiguous buffer */
909 1.2.2.2 nathanw m_copydata(m, 0, m->m_pkthdr.len, c->url_buf);
910 1.2.2.2 nathanw c->url_mbuf = m;
911 1.2.2.2 nathanw total_len = m->m_pkthdr.len;
912 1.2.2.2 nathanw
913 1.2.2.2 nathanw if (total_len < URL_MIN_FRAME_LEN)
914 1.2.2.2 nathanw total_len = URL_MIN_FRAME_LEN;
915 1.2.2.2 nathanw usbd_setup_xfer(c->url_xfer, sc->sc_pipe_tx, c, c->url_buf, total_len,
916 1.2.2.2 nathanw USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
917 1.2.2.2 nathanw URL_TX_TIMEOUT, url_txeof);
918 1.2.2.2 nathanw
919 1.2.2.2 nathanw /* Transmit */
920 1.2.2.2 nathanw sc->sc_refcnt++;
921 1.2.2.2 nathanw err = usbd_transfer(c->url_xfer);
922 1.2.2.2 nathanw if (--sc->sc_refcnt < 0)
923 1.2.2.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
924 1.2.2.2 nathanw if (err != USBD_IN_PROGRESS) {
925 1.2.2.2 nathanw printf("%s: url_send error=%s\n", USBDEVNAME(sc->sc_dev),
926 1.2.2.2 nathanw usbd_errstr(err));
927 1.2.2.2 nathanw /* Stop the interface */
928 1.2.2.2 nathanw usb_add_task(sc->sc_udev, &sc->sc_stop_task);
929 1.2.2.2 nathanw return (EIO);
930 1.2.2.2 nathanw }
931 1.2.2.2 nathanw
932 1.2.2.2 nathanw DPRINTF(("%s: %s: send %d bytes\n", USBDEVNAME(sc->sc_dev),
933 1.2.2.2 nathanw __FUNCTION__, total_len));
934 1.2.2.2 nathanw
935 1.2.2.2 nathanw sc->sc_cdata.url_tx_cnt++;
936 1.2.2.2 nathanw
937 1.2.2.2 nathanw return (0);
938 1.2.2.2 nathanw }
939 1.2.2.2 nathanw
940 1.2.2.2 nathanw Static void
941 1.2.2.2 nathanw url_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
942 1.2.2.2 nathanw {
943 1.2.2.2 nathanw struct url_chain *c = priv;
944 1.2.2.2 nathanw struct url_softc *sc = c->url_sc;
945 1.2.2.2 nathanw struct ifnet *ifp = GET_IFP(sc);
946 1.2.2.2 nathanw int s;
947 1.2.2.2 nathanw
948 1.2.2.2 nathanw if (sc->sc_dying)
949 1.2.2.2 nathanw return;
950 1.2.2.2 nathanw
951 1.2.2.2 nathanw s = splnet();
952 1.2.2.2 nathanw
953 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
954 1.2.2.2 nathanw
955 1.2.2.2 nathanw ifp->if_timer = 0;
956 1.2.2.2 nathanw ifp->if_flags &= ~IFF_OACTIVE;
957 1.2.2.2 nathanw
958 1.2.2.2 nathanw if (status != USBD_NORMAL_COMPLETION) {
959 1.2.2.2 nathanw if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
960 1.2.2.2 nathanw splx(s);
961 1.2.2.2 nathanw return;
962 1.2.2.2 nathanw }
963 1.2.2.2 nathanw ifp->if_oerrors++;
964 1.2.2.2 nathanw printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
965 1.2.2.2 nathanw usbd_errstr(status));
966 1.2.2.2 nathanw if (status == USBD_STALLED) {
967 1.2.2.2 nathanw sc->sc_refcnt++;
968 1.2.2.2 nathanw usbd_clear_endpoint_stall(sc->sc_pipe_tx);
969 1.2.2.2 nathanw if (--sc->sc_refcnt < 0)
970 1.2.2.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
971 1.2.2.2 nathanw }
972 1.2.2.2 nathanw splx(s);
973 1.2.2.2 nathanw return;
974 1.2.2.2 nathanw }
975 1.2.2.2 nathanw
976 1.2.2.2 nathanw ifp->if_opackets++;
977 1.2.2.2 nathanw
978 1.2.2.2 nathanw m_free(c->url_mbuf);
979 1.2.2.2 nathanw c->url_mbuf = NULL;
980 1.2.2.2 nathanw
981 1.2.2.2 nathanw if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
982 1.2.2.2 nathanw url_start(ifp);
983 1.2.2.2 nathanw
984 1.2.2.2 nathanw splx(s);
985 1.2.2.2 nathanw }
986 1.2.2.2 nathanw
987 1.2.2.2 nathanw Static void
988 1.2.2.2 nathanw url_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
989 1.2.2.2 nathanw {
990 1.2.2.2 nathanw struct url_chain *c = priv;
991 1.2.2.2 nathanw struct url_softc *sc = c->url_sc;
992 1.2.2.2 nathanw struct ifnet *ifp = GET_IFP(sc);
993 1.2.2.2 nathanw struct mbuf *m;
994 1.2.2.2 nathanw u_int32_t total_len;
995 1.2.2.2 nathanw url_rxhdr_t rxhdr;
996 1.2.2.2 nathanw int s;
997 1.2.2.2 nathanw
998 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
999 1.2.2.2 nathanw
1000 1.2.2.2 nathanw if (sc->sc_dying)
1001 1.2.2.2 nathanw return;
1002 1.2.2.2 nathanw
1003 1.2.2.2 nathanw if (status != USBD_NORMAL_COMPLETION) {
1004 1.2.2.2 nathanw if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1005 1.2.2.2 nathanw return;
1006 1.2.2.2 nathanw sc->sc_rx_errs++;
1007 1.2.2.2 nathanw if (usbd_ratecheck(&sc->sc_rx_notice)) {
1008 1.2.2.2 nathanw printf("%s: %u usb errors on rx: %s\n",
1009 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
1010 1.2.2.2 nathanw usbd_errstr(status));
1011 1.2.2.2 nathanw sc->sc_rx_errs = 0;
1012 1.2.2.2 nathanw }
1013 1.2.2.2 nathanw if (status == USBD_STALLED) {
1014 1.2.2.2 nathanw sc->sc_refcnt++;
1015 1.2.2.2 nathanw usbd_clear_endpoint_stall(sc->sc_pipe_rx);
1016 1.2.2.2 nathanw if (--sc->sc_refcnt < 0)
1017 1.2.2.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
1018 1.2.2.2 nathanw }
1019 1.2.2.2 nathanw goto done;
1020 1.2.2.2 nathanw }
1021 1.2.2.2 nathanw
1022 1.2.2.2 nathanw usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1023 1.2.2.2 nathanw
1024 1.2.2.2 nathanw memcpy(mtod(c->url_mbuf, char *), c->url_buf, total_len);
1025 1.2.2.2 nathanw
1026 1.2.2.2 nathanw if (total_len <= ETHER_CRC_LEN) {
1027 1.2.2.2 nathanw ifp->if_ierrors++;
1028 1.2.2.2 nathanw goto done;
1029 1.2.2.2 nathanw }
1030 1.2.2.2 nathanw
1031 1.2.2.2 nathanw memcpy(&rxhdr, c->url_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr));
1032 1.2.2.2 nathanw
1033 1.2.2.2 nathanw DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n",
1034 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev),
1035 1.2.2.2 nathanw UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK,
1036 1.2.2.2 nathanw UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "",
1037 1.2.2.2 nathanw UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "",
1038 1.2.2.2 nathanw UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "",
1039 1.2.2.2 nathanw UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : ""));
1040 1.2.2.2 nathanw
1041 1.2.2.2 nathanw if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) {
1042 1.2.2.2 nathanw ifp->if_ierrors++;
1043 1.2.2.2 nathanw goto done;
1044 1.2.2.2 nathanw }
1045 1.2.2.2 nathanw
1046 1.2.2.2 nathanw ifp->if_ipackets++;
1047 1.2.2.2 nathanw total_len -= ETHER_CRC_LEN;
1048 1.2.2.2 nathanw
1049 1.2.2.2 nathanw m = c->url_mbuf;
1050 1.2.2.2 nathanw m->m_pkthdr.len = m->m_len = total_len;
1051 1.2.2.2 nathanw m->m_pkthdr.rcvif = ifp;
1052 1.2.2.2 nathanw
1053 1.2.2.2 nathanw s = splnet();
1054 1.2.2.2 nathanw
1055 1.2.2.2 nathanw if (url_newbuf(sc, c, NULL) == ENOBUFS) {
1056 1.2.2.2 nathanw ifp->if_ierrors++;
1057 1.2.2.2 nathanw goto done1;
1058 1.2.2.2 nathanw }
1059 1.2.2.2 nathanw
1060 1.2.2.2 nathanw #if NBPFILTER > 0
1061 1.2.2.2 nathanw if (ifp->if_bpf)
1062 1.2.2.2 nathanw BPF_MTAP(ifp, m);
1063 1.2.2.2 nathanw #endif
1064 1.2.2.2 nathanw
1065 1.2.2.2 nathanw DPRINTF(("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
1066 1.2.2.2 nathanw __FUNCTION__, m->m_len));
1067 1.2.2.2 nathanw IF_INPUT(ifp, m);
1068 1.2.2.2 nathanw
1069 1.2.2.2 nathanw done1:
1070 1.2.2.2 nathanw splx(s);
1071 1.2.2.2 nathanw
1072 1.2.2.2 nathanw done:
1073 1.2.2.2 nathanw /* Setup new transfer */
1074 1.2.2.2 nathanw usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->url_buf, URL_BUFSZ,
1075 1.2.2.2 nathanw USBD_SHORT_XFER_OK | USBD_NO_COPY,
1076 1.2.2.2 nathanw USBD_NO_TIMEOUT, url_rxeof);
1077 1.2.2.2 nathanw sc->sc_refcnt++;
1078 1.2.2.2 nathanw usbd_transfer(xfer);
1079 1.2.2.2 nathanw if (--sc->sc_refcnt < 0)
1080 1.2.2.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
1081 1.2.2.2 nathanw
1082 1.2.2.2 nathanw DPRINTF(("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1083 1.2.2.2 nathanw }
1084 1.2.2.2 nathanw
1085 1.2.2.2 nathanw #if 0
1086 1.2.2.2 nathanw Static void url_intr()
1087 1.2.2.2 nathanw {
1088 1.2.2.2 nathanw }
1089 1.2.2.2 nathanw #endif
1090 1.2.2.2 nathanw
1091 1.2.2.2 nathanw Static int
1092 1.2.2.2 nathanw url_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1093 1.2.2.2 nathanw {
1094 1.2.2.2 nathanw struct url_softc *sc = ifp->if_softc;
1095 1.2.2.2 nathanw struct ifreq *ifr = (struct ifreq *)data;
1096 1.2.2.2 nathanw struct mii_data *mii;
1097 1.2.2.2 nathanw int s, error = 0;
1098 1.2.2.2 nathanw
1099 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1100 1.2.2.2 nathanw
1101 1.2.2.2 nathanw if (sc->sc_dying)
1102 1.2.2.2 nathanw return (EIO);
1103 1.2.2.2 nathanw
1104 1.2.2.2 nathanw s = splnet();
1105 1.2.2.2 nathanw
1106 1.2.2.2 nathanw switch (cmd) {
1107 1.2.2.2 nathanw case SIOCGIFMEDIA:
1108 1.2.2.2 nathanw case SIOCSIFMEDIA:
1109 1.2.2.2 nathanw mii = GET_MII(sc);
1110 1.2.2.2 nathanw error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1111 1.2.2.2 nathanw break;
1112 1.2.2.2 nathanw
1113 1.2.2.2 nathanw default:
1114 1.2.2.2 nathanw error = ether_ioctl(ifp, cmd, data);
1115 1.2.2.2 nathanw if (error == ENETRESET) {
1116 1.2.2.2 nathanw url_setmulti(sc);
1117 1.2.2.2 nathanw error = 0;
1118 1.2.2.2 nathanw }
1119 1.2.2.2 nathanw break;
1120 1.2.2.2 nathanw }
1121 1.2.2.2 nathanw
1122 1.2.2.2 nathanw splx(s);
1123 1.2.2.2 nathanw
1124 1.2.2.2 nathanw return (error);
1125 1.2.2.2 nathanw }
1126 1.2.2.2 nathanw
1127 1.2.2.2 nathanw Static void
1128 1.2.2.2 nathanw url_watchdog(struct ifnet *ifp)
1129 1.2.2.2 nathanw {
1130 1.2.2.2 nathanw struct url_softc *sc = ifp->if_softc;
1131 1.2.2.2 nathanw struct url_chain *c;
1132 1.2.2.2 nathanw usbd_status stat;
1133 1.2.2.2 nathanw int s;
1134 1.2.2.2 nathanw
1135 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1136 1.2.2.2 nathanw
1137 1.2.2.2 nathanw ifp->if_oerrors++;
1138 1.2.2.2 nathanw printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
1139 1.2.2.2 nathanw
1140 1.2.2.2 nathanw s = splusb();
1141 1.2.2.2 nathanw c = &sc->sc_cdata.url_tx_chain[0];
1142 1.2.2.2 nathanw usbd_get_xfer_status(c->url_xfer, NULL, NULL, NULL, &stat);
1143 1.2.2.2 nathanw url_txeof(c->url_xfer, c, stat);
1144 1.2.2.2 nathanw
1145 1.2.2.2 nathanw if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1146 1.2.2.2 nathanw url_start(ifp);
1147 1.2.2.2 nathanw splx(s);
1148 1.2.2.2 nathanw }
1149 1.2.2.2 nathanw
1150 1.2.2.2 nathanw Static void
1151 1.2.2.2 nathanw url_stop_task(struct url_softc *sc)
1152 1.2.2.2 nathanw {
1153 1.2.2.2 nathanw url_stop(GET_IFP(sc), 1);
1154 1.2.2.2 nathanw }
1155 1.2.2.2 nathanw
1156 1.2.2.2 nathanw /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1157 1.2.2.2 nathanw Static void
1158 1.2.2.2 nathanw url_stop(struct ifnet *ifp, int disable)
1159 1.2.2.2 nathanw {
1160 1.2.2.2 nathanw struct url_softc *sc = ifp->if_softc;
1161 1.2.2.2 nathanw usbd_status err;
1162 1.2.2.2 nathanw int i;
1163 1.2.2.2 nathanw
1164 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1165 1.2.2.2 nathanw
1166 1.2.2.2 nathanw ifp->if_timer = 0;
1167 1.2.2.2 nathanw
1168 1.2.2.2 nathanw url_reset(sc);
1169 1.2.2.2 nathanw
1170 1.2.2.2 nathanw usb_uncallout(sc->sc_stat_ch, url_tick, sc);
1171 1.2.2.2 nathanw
1172 1.2.2.2 nathanw /* Stop transfers */
1173 1.2.2.2 nathanw /* RX endpoint */
1174 1.2.2.2 nathanw if (sc->sc_pipe_rx != NULL) {
1175 1.2.2.2 nathanw err = usbd_abort_pipe(sc->sc_pipe_rx);
1176 1.2.2.2 nathanw if (err)
1177 1.2.2.2 nathanw printf("%s: abort rx pipe failed: %s\n",
1178 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1179 1.2.2.2 nathanw err = usbd_close_pipe(sc->sc_pipe_rx);
1180 1.2.2.2 nathanw if (err)
1181 1.2.2.2 nathanw printf("%s: close rx pipe failed: %s\n",
1182 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1183 1.2.2.2 nathanw sc->sc_pipe_rx = NULL;
1184 1.2.2.2 nathanw }
1185 1.2.2.2 nathanw
1186 1.2.2.2 nathanw /* TX endpoint */
1187 1.2.2.2 nathanw if (sc->sc_pipe_tx != NULL) {
1188 1.2.2.2 nathanw err = usbd_abort_pipe(sc->sc_pipe_tx);
1189 1.2.2.2 nathanw if (err)
1190 1.2.2.2 nathanw printf("%s: abort tx pipe failed: %s\n",
1191 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1192 1.2.2.2 nathanw err = usbd_close_pipe(sc->sc_pipe_tx);
1193 1.2.2.2 nathanw if (err)
1194 1.2.2.2 nathanw printf("%s: close tx pipe failed: %s\n",
1195 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1196 1.2.2.2 nathanw sc->sc_pipe_tx = NULL;
1197 1.2.2.2 nathanw }
1198 1.2.2.2 nathanw
1199 1.2.2.2 nathanw #if 0
1200 1.2.2.2 nathanw /* XXX: Interrupt endpoint is not yet supported!! */
1201 1.2.2.2 nathanw /* Interrupt endpoint */
1202 1.2.2.2 nathanw if (sc->sc_pipe_intr != NULL) {
1203 1.2.2.2 nathanw err = usbd_abort_pipe(sc->sc_pipe_intr);
1204 1.2.2.2 nathanw if (err)
1205 1.2.2.2 nathanw printf("%s: abort intr pipe failed: %s\n",
1206 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1207 1.2.2.2 nathanw err = usbd_close_pipe(sc->sc_pipe_intr);
1208 1.2.2.2 nathanw if (err)
1209 1.2.2.2 nathanw printf("%s: close intr pipe failed: %s\n",
1210 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1211 1.2.2.2 nathanw sc->sc_pipe_intr = NULL;
1212 1.2.2.2 nathanw }
1213 1.2.2.2 nathanw #endif
1214 1.2.2.2 nathanw
1215 1.2.2.2 nathanw /* Free RX resources. */
1216 1.2.2.2 nathanw for (i = 0; i < URL_RX_LIST_CNT; i++) {
1217 1.2.2.2 nathanw if (sc->sc_cdata.url_rx_chain[i].url_mbuf != NULL) {
1218 1.2.2.2 nathanw m_freem(sc->sc_cdata.url_rx_chain[i].url_mbuf);
1219 1.2.2.2 nathanw sc->sc_cdata.url_rx_chain[i].url_mbuf = NULL;
1220 1.2.2.2 nathanw }
1221 1.2.2.2 nathanw if (sc->sc_cdata.url_rx_chain[i].url_xfer != NULL) {
1222 1.2.2.2 nathanw usbd_free_xfer(sc->sc_cdata.url_rx_chain[i].url_xfer);
1223 1.2.2.2 nathanw sc->sc_cdata.url_rx_chain[i].url_xfer = NULL;
1224 1.2.2.2 nathanw }
1225 1.2.2.2 nathanw }
1226 1.2.2.2 nathanw
1227 1.2.2.2 nathanw /* Free TX resources. */
1228 1.2.2.2 nathanw for (i = 0; i < URL_TX_LIST_CNT; i++) {
1229 1.2.2.2 nathanw if (sc->sc_cdata.url_tx_chain[i].url_mbuf != NULL) {
1230 1.2.2.2 nathanw m_freem(sc->sc_cdata.url_tx_chain[i].url_mbuf);
1231 1.2.2.2 nathanw sc->sc_cdata.url_tx_chain[i].url_mbuf = NULL;
1232 1.2.2.2 nathanw }
1233 1.2.2.2 nathanw if (sc->sc_cdata.url_tx_chain[i].url_xfer != NULL) {
1234 1.2.2.2 nathanw usbd_free_xfer(sc->sc_cdata.url_tx_chain[i].url_xfer);
1235 1.2.2.2 nathanw sc->sc_cdata.url_tx_chain[i].url_xfer = NULL;
1236 1.2.2.2 nathanw }
1237 1.2.2.2 nathanw }
1238 1.2.2.2 nathanw
1239 1.2.2.2 nathanw sc->sc_link = 0;
1240 1.2.2.2 nathanw ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1241 1.2.2.2 nathanw }
1242 1.2.2.2 nathanw
1243 1.2.2.2 nathanw /* Set media options */
1244 1.2.2.2 nathanw Static int
1245 1.2.2.2 nathanw url_ifmedia_change(struct ifnet *ifp)
1246 1.2.2.2 nathanw {
1247 1.2.2.2 nathanw struct url_softc *sc = ifp->if_softc;
1248 1.2.2.2 nathanw struct mii_data *mii = GET_MII(sc);
1249 1.2.2.2 nathanw
1250 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1251 1.2.2.2 nathanw
1252 1.2.2.2 nathanw if (sc->sc_dying)
1253 1.2.2.2 nathanw return (0);
1254 1.2.2.2 nathanw
1255 1.2.2.2 nathanw sc->sc_link = 0;
1256 1.2.2.2 nathanw if (mii->mii_instance) {
1257 1.2.2.2 nathanw struct mii_softc *miisc;
1258 1.2.2.2 nathanw for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1259 1.2.2.2 nathanw miisc = LIST_NEXT(miisc, mii_list))
1260 1.2.2.2 nathanw mii_phy_reset(miisc);
1261 1.2.2.2 nathanw }
1262 1.2.2.2 nathanw
1263 1.2.2.2 nathanw return (mii_mediachg(mii));
1264 1.2.2.2 nathanw }
1265 1.2.2.2 nathanw
1266 1.2.2.2 nathanw /* Report current media status. */
1267 1.2.2.2 nathanw Static void
1268 1.2.2.2 nathanw url_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1269 1.2.2.2 nathanw {
1270 1.2.2.2 nathanw struct url_softc *sc = ifp->if_softc;
1271 1.2.2.2 nathanw struct mii_data *mii = GET_MII(sc);
1272 1.2.2.2 nathanw
1273 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1274 1.2.2.2 nathanw
1275 1.2.2.2 nathanw if (sc->sc_dying)
1276 1.2.2.2 nathanw return;
1277 1.2.2.2 nathanw
1278 1.2.2.2 nathanw if ((ifp->if_flags & IFF_RUNNING) == 0) {
1279 1.2.2.2 nathanw ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1280 1.2.2.2 nathanw ifmr->ifm_status = 0;
1281 1.2.2.2 nathanw return;
1282 1.2.2.2 nathanw }
1283 1.2.2.2 nathanw
1284 1.2.2.2 nathanw mii_pollstat(mii);
1285 1.2.2.2 nathanw ifmr->ifm_active = mii->mii_media_active;
1286 1.2.2.2 nathanw ifmr->ifm_status = mii->mii_media_status;
1287 1.2.2.2 nathanw }
1288 1.2.2.2 nathanw
1289 1.2.2.2 nathanw Static void
1290 1.2.2.2 nathanw url_tick(void *xsc)
1291 1.2.2.2 nathanw {
1292 1.2.2.2 nathanw struct url_softc *sc = xsc;
1293 1.2.2.2 nathanw
1294 1.2.2.2 nathanw if (sc == NULL)
1295 1.2.2.2 nathanw return;
1296 1.2.2.2 nathanw
1297 1.2.2.2 nathanw DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1298 1.2.2.2 nathanw __FUNCTION__));
1299 1.2.2.2 nathanw
1300 1.2.2.2 nathanw if (sc->sc_dying)
1301 1.2.2.2 nathanw return;
1302 1.2.2.2 nathanw
1303 1.2.2.2 nathanw /* Perform periodic stuff in process context */
1304 1.2.2.2 nathanw usb_add_task(sc->sc_udev, &sc->sc_tick_task);
1305 1.2.2.2 nathanw }
1306 1.2.2.2 nathanw
1307 1.2.2.2 nathanw Static void
1308 1.2.2.2 nathanw url_tick_task(void *xsc)
1309 1.2.2.2 nathanw {
1310 1.2.2.2 nathanw struct url_softc *sc = xsc;
1311 1.2.2.2 nathanw struct ifnet *ifp;
1312 1.2.2.2 nathanw struct mii_data *mii;
1313 1.2.2.2 nathanw int s;
1314 1.2.2.2 nathanw
1315 1.2.2.2 nathanw if (sc == NULL)
1316 1.2.2.2 nathanw return;
1317 1.2.2.2 nathanw
1318 1.2.2.2 nathanw DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1319 1.2.2.2 nathanw __FUNCTION__));
1320 1.2.2.2 nathanw
1321 1.2.2.2 nathanw if (sc->sc_dying)
1322 1.2.2.2 nathanw return;
1323 1.2.2.2 nathanw
1324 1.2.2.2 nathanw ifp = GET_IFP(sc);
1325 1.2.2.2 nathanw mii = GET_MII(sc);
1326 1.2.2.2 nathanw
1327 1.2.2.2 nathanw if (mii == NULL)
1328 1.2.2.2 nathanw return;
1329 1.2.2.2 nathanw
1330 1.2.2.2 nathanw s = splnet();
1331 1.2.2.2 nathanw
1332 1.2.2.2 nathanw mii_tick(mii);
1333 1.2.2.2 nathanw if (!sc->sc_link) {
1334 1.2.2.2 nathanw mii_pollstat(mii);
1335 1.2.2.2 nathanw if (mii->mii_media_status & IFM_ACTIVE &&
1336 1.2.2.2 nathanw IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1337 1.2.2.2 nathanw DPRINTF(("%s: %s: got link\n",
1338 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__));
1339 1.2.2.2 nathanw sc->sc_link++;
1340 1.2.2.2 nathanw if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1341 1.2.2.2 nathanw url_start(ifp);
1342 1.2.2.2 nathanw }
1343 1.2.2.2 nathanw }
1344 1.2.2.2 nathanw
1345 1.2.2.2 nathanw usb_callout(sc->sc_stat_ch, hz, url_tick, sc);
1346 1.2.2.2 nathanw
1347 1.2.2.2 nathanw splx(s);
1348 1.2.2.2 nathanw }
1349 1.2.2.2 nathanw
1350 1.2.2.2 nathanw /* Get exclusive access to the MII registers */
1351 1.2.2.2 nathanw Static void
1352 1.2.2.2 nathanw url_lock_mii(struct url_softc *sc)
1353 1.2.2.2 nathanw {
1354 1.2.2.2 nathanw DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1355 1.2.2.2 nathanw __FUNCTION__));
1356 1.2.2.2 nathanw
1357 1.2.2.2 nathanw sc->sc_refcnt++;
1358 1.2.2.2 nathanw lockmgr(&sc->sc_mii_lock, LK_EXCLUSIVE, NULL);
1359 1.2.2.2 nathanw }
1360 1.2.2.2 nathanw
1361 1.2.2.2 nathanw Static void
1362 1.2.2.2 nathanw url_unlock_mii(struct url_softc *sc)
1363 1.2.2.2 nathanw {
1364 1.2.2.2 nathanw DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1365 1.2.2.2 nathanw __FUNCTION__));
1366 1.2.2.2 nathanw
1367 1.2.2.2 nathanw lockmgr(&sc->sc_mii_lock, LK_RELEASE, NULL);
1368 1.2.2.2 nathanw if (--sc->sc_refcnt < 0)
1369 1.2.2.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
1370 1.2.2.2 nathanw }
1371 1.2.2.2 nathanw
1372 1.2.2.2 nathanw Static int
1373 1.2.2.2 nathanw url_int_miibus_readreg(device_ptr_t dev, int phy, int reg)
1374 1.2.2.2 nathanw {
1375 1.2.2.2 nathanw struct url_softc *sc;
1376 1.2.2.2 nathanw u_int16_t val;
1377 1.2.2.2 nathanw
1378 1.2.2.2 nathanw if (dev == NULL)
1379 1.2.2.2 nathanw return (0);
1380 1.2.2.2 nathanw
1381 1.2.2.2 nathanw sc = USBGETSOFTC(dev);
1382 1.2.2.2 nathanw
1383 1.2.2.2 nathanw DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1384 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg));
1385 1.2.2.2 nathanw
1386 1.2.2.2 nathanw if (sc->sc_dying) {
1387 1.2.2.2 nathanw #ifdef DIAGNOSTIC
1388 1.2.2.2 nathanw printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1389 1.2.2.2 nathanw __FUNCTION__);
1390 1.2.2.2 nathanw #endif
1391 1.2.2.2 nathanw return (0);
1392 1.2.2.2 nathanw }
1393 1.2.2.2 nathanw
1394 1.2.2.2 nathanw /* XXX: one PHY only for the RTL8150 internal PHY */
1395 1.2.2.2 nathanw if (phy != 0) {
1396 1.2.2.2 nathanw DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1397 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__, phy));
1398 1.2.2.2 nathanw return (0);
1399 1.2.2.2 nathanw }
1400 1.2.2.2 nathanw
1401 1.2.2.2 nathanw url_lock_mii(sc);
1402 1.2.2.2 nathanw
1403 1.2.2.2 nathanw switch (reg) {
1404 1.2.2.2 nathanw case MII_BMCR: /* Control Register */
1405 1.2.2.2 nathanw reg = URL_BMCR;
1406 1.2.2.2 nathanw break;
1407 1.2.2.2 nathanw case MII_BMSR: /* Status Register */
1408 1.2.2.2 nathanw reg = URL_BMSR;
1409 1.2.2.2 nathanw break;
1410 1.2.2.2 nathanw case MII_PHYIDR1:
1411 1.2.2.2 nathanw case MII_PHYIDR2:
1412 1.2.2.2 nathanw val = 0;
1413 1.2.2.2 nathanw goto R_DONE;
1414 1.2.2.2 nathanw break;
1415 1.2.2.2 nathanw case MII_ANAR: /* Autonegotiation advertisement */
1416 1.2.2.2 nathanw reg = URL_ANAR;
1417 1.2.2.2 nathanw break;
1418 1.2.2.2 nathanw case MII_ANLPAR: /* Autonegotiation link partner abilities */
1419 1.2.2.2 nathanw reg = URL_ANLP;
1420 1.2.2.2 nathanw break;
1421 1.2.2.2 nathanw case URLPHY_MSR: /* Media Status Register */
1422 1.2.2.2 nathanw reg = URL_MSR;
1423 1.2.2.2 nathanw break;
1424 1.2.2.2 nathanw default:
1425 1.2.2.2 nathanw printf("%s: %s: bad register %04x\n",
1426 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__, reg);
1427 1.2.2.2 nathanw val = 0;
1428 1.2.2.2 nathanw goto R_DONE;
1429 1.2.2.2 nathanw break;
1430 1.2.2.2 nathanw }
1431 1.2.2.2 nathanw
1432 1.2.2.2 nathanw if (reg == URL_MSR)
1433 1.2.2.2 nathanw val = url_csr_read_1(sc, reg);
1434 1.2.2.2 nathanw else
1435 1.2.2.2 nathanw val = url_csr_read_2(sc, reg);
1436 1.2.2.2 nathanw
1437 1.2.2.2 nathanw R_DONE:
1438 1.2.2.2 nathanw DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1439 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg, val));
1440 1.2.2.2 nathanw
1441 1.2.2.2 nathanw url_unlock_mii(sc);
1442 1.2.2.2 nathanw return (val);
1443 1.2.2.2 nathanw }
1444 1.2.2.2 nathanw
1445 1.2.2.2 nathanw Static void
1446 1.2.2.2 nathanw url_int_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
1447 1.2.2.2 nathanw {
1448 1.2.2.2 nathanw struct url_softc *sc;
1449 1.2.2.2 nathanw
1450 1.2.2.2 nathanw if (dev == NULL)
1451 1.2.2.2 nathanw return;
1452 1.2.2.2 nathanw
1453 1.2.2.2 nathanw sc = USBGETSOFTC(dev);
1454 1.2.2.2 nathanw
1455 1.2.2.2 nathanw DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1456 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg, data));
1457 1.2.2.2 nathanw
1458 1.2.2.2 nathanw if (sc->sc_dying) {
1459 1.2.2.2 nathanw #ifdef DIAGNOSTIC
1460 1.2.2.2 nathanw printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1461 1.2.2.2 nathanw __FUNCTION__);
1462 1.2.2.2 nathanw #endif
1463 1.2.2.2 nathanw return;
1464 1.2.2.2 nathanw }
1465 1.2.2.2 nathanw
1466 1.2.2.2 nathanw /* XXX: one PHY only for the RTL8150 internal PHY */
1467 1.2.2.2 nathanw if (phy != 0) {
1468 1.2.2.2 nathanw DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1469 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__, phy));
1470 1.2.2.2 nathanw return;
1471 1.2.2.2 nathanw }
1472 1.2.2.2 nathanw
1473 1.2.2.2 nathanw url_lock_mii(sc);
1474 1.2.2.2 nathanw
1475 1.2.2.2 nathanw switch (reg) {
1476 1.2.2.2 nathanw case MII_BMCR: /* Control Register */
1477 1.2.2.2 nathanw reg = URL_BMCR;
1478 1.2.2.2 nathanw break;
1479 1.2.2.2 nathanw case MII_BMSR: /* Status Register */
1480 1.2.2.2 nathanw reg = URL_BMSR;
1481 1.2.2.2 nathanw break;
1482 1.2.2.2 nathanw case MII_PHYIDR1:
1483 1.2.2.2 nathanw case MII_PHYIDR2:
1484 1.2.2.2 nathanw goto W_DONE;
1485 1.2.2.2 nathanw break;
1486 1.2.2.2 nathanw case MII_ANAR: /* Autonegotiation advertisement */
1487 1.2.2.2 nathanw reg = URL_ANAR;
1488 1.2.2.2 nathanw break;
1489 1.2.2.2 nathanw case MII_ANLPAR: /* Autonegotiation link partner abilities */
1490 1.2.2.2 nathanw reg = URL_ANLP;
1491 1.2.2.2 nathanw break;
1492 1.2.2.2 nathanw case URLPHY_MSR: /* Media Status Register */
1493 1.2.2.2 nathanw reg = URL_MSR;
1494 1.2.2.2 nathanw break;
1495 1.2.2.2 nathanw default:
1496 1.2.2.2 nathanw printf("%s: %s: bad register %04x\n",
1497 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__, reg);
1498 1.2.2.2 nathanw goto W_DONE;
1499 1.2.2.2 nathanw break;
1500 1.2.2.2 nathanw }
1501 1.2.2.2 nathanw
1502 1.2.2.2 nathanw if (reg == URL_MSR)
1503 1.2.2.2 nathanw url_csr_write_1(sc, reg, data);
1504 1.2.2.2 nathanw else
1505 1.2.2.2 nathanw url_csr_write_2(sc, reg, data);
1506 1.2.2.2 nathanw W_DONE:
1507 1.2.2.2 nathanw
1508 1.2.2.2 nathanw url_unlock_mii(sc);
1509 1.2.2.2 nathanw return;
1510 1.2.2.2 nathanw }
1511 1.2.2.2 nathanw
1512 1.2.2.2 nathanw Static void
1513 1.2.2.2 nathanw url_miibus_statchg(device_ptr_t dev)
1514 1.2.2.2 nathanw {
1515 1.2.2.2 nathanw #ifdef URL_DEBUG
1516 1.2.2.2 nathanw struct url_softc *sc;
1517 1.2.2.2 nathanw
1518 1.2.2.2 nathanw if (dev == NULL)
1519 1.2.2.2 nathanw return;
1520 1.2.2.2 nathanw
1521 1.2.2.2 nathanw sc = USBGETSOFTC(dev);
1522 1.2.2.2 nathanw DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1523 1.2.2.2 nathanw #endif
1524 1.2.2.2 nathanw /* Nothing to do */
1525 1.2.2.2 nathanw }
1526 1.2.2.2 nathanw
1527 1.2.2.2 nathanw #if 0
1528 1.2.2.2 nathanw /*
1529 1.2.2.2 nathanw * external PHYs support, but not test.
1530 1.2.2.2 nathanw */
1531 1.2.2.2 nathanw Static int
1532 1.2.2.2 nathanw url_ext_miibus_redreg(device_ptr_t dev, int phy, int reg)
1533 1.2.2.2 nathanw {
1534 1.2.2.2 nathanw struct url_softc *sc = USBGETSOFTC(dev);
1535 1.2.2.2 nathanw u_int16_t val;
1536 1.2.2.2 nathanw
1537 1.2.2.2 nathanw DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n",
1538 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg));
1539 1.2.2.2 nathanw
1540 1.2.2.2 nathanw if (sc->sc_dying) {
1541 1.2.2.2 nathanw #ifdef DIAGNOSTIC
1542 1.2.2.2 nathanw printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1543 1.2.2.2 nathanw __FUNCTION__);
1544 1.2.2.2 nathanw #endif
1545 1.2.2.2 nathanw return (0);
1546 1.2.2.2 nathanw }
1547 1.2.2.2 nathanw
1548 1.2.2.2 nathanw url_lock_mii(sc);
1549 1.2.2.2 nathanw
1550 1.2.2.2 nathanw url_csr_write_1(sc, URL_PHYADD, phy & URL_PHYADD_MASK);
1551 1.2.2.2 nathanw /*
1552 1.2.2.2 nathanw * RTL8150L will initiate a MII management data transaction
1553 1.2.2.2 nathanw * if PHYCNT_OWN bit is set 1 by software. After transaction,
1554 1.2.2.2 nathanw * this bit is auto cleared by TRL8150L.
1555 1.2.2.2 nathanw */
1556 1.2.2.2 nathanw url_csr_write_1(sc, URL_PHYCNT,
1557 1.2.2.2 nathanw (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR);
1558 1.2.2.2 nathanw for (i = 0; i < URL_TIMEOUT; i++) {
1559 1.2.2.2 nathanw if ((url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0)
1560 1.2.2.2 nathanw break;
1561 1.2.2.2 nathanw }
1562 1.2.2.2 nathanw if (i == URL_TIMEOUT) {
1563 1.2.2.2 nathanw printf("%s: MII read timed out\n", USBDEVNAME(sc->sc_dev));
1564 1.2.2.2 nathanw }
1565 1.2.2.2 nathanw
1566 1.2.2.2 nathanw val = url_csr_read_2(sc, URL_PHYDAT);
1567 1.2.2.2 nathanw
1568 1.2.2.2 nathanw DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1569 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg, val));
1570 1.2.2.2 nathanw
1571 1.2.2.2 nathanw url_unlock_mii(sc);
1572 1.2.2.2 nathanw return (val);
1573 1.2.2.2 nathanw }
1574 1.2.2.2 nathanw
1575 1.2.2.2 nathanw Static void
1576 1.2.2.2 nathanw url_ext_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
1577 1.2.2.2 nathanw {
1578 1.2.2.2 nathanw struct url_softc *sc = USBGETSOFTC(dev);
1579 1.2.2.2 nathanw
1580 1.2.2.2 nathanw DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1581 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg, data));
1582 1.2.2.2 nathanw
1583 1.2.2.2 nathanw if (sc->sc_dying) {
1584 1.2.2.2 nathanw #ifdef DIAGNOSTIC
1585 1.2.2.2 nathanw printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1586 1.2.2.2 nathanw __FUNCTION__);
1587 1.2.2.2 nathanw #endif
1588 1.2.2.2 nathanw return;
1589 1.2.2.2 nathanw }
1590 1.2.2.2 nathanw
1591 1.2.2.2 nathanw url_lock_mii(sc);
1592 1.2.2.2 nathanw
1593 1.2.2.2 nathanw url_csr_write_2(sc, URL_PHYDAT, data);
1594 1.2.2.2 nathanw url_csr_write_1(sc, URL_PHYADD, phy);
1595 1.2.2.2 nathanw url_csr_write_1(sc, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */
1596 1.2.2.2 nathanw
1597 1.2.2.2 nathanw for (i=0; i < URL_TIMEOUT; i++) {
1598 1.2.2.2 nathanw if (url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN)
1599 1.2.2.2 nathanw break;
1600 1.2.2.2 nathanw }
1601 1.2.2.2 nathanw
1602 1.2.2.2 nathanw if (i == URL_TIMEOUT) {
1603 1.2.2.2 nathanw printf("%s: MII write timed out\n",
1604 1.2.2.2 nathanw USBDEVNAME(sc->sc_dev));
1605 1.2.2.2 nathanw }
1606 1.2.2.2 nathanw
1607 1.2.2.2 nathanw url_unlock_mii(sc);
1608 1.2.2.2 nathanw return;
1609 1.2.2.2 nathanw }
1610 1.2.2.2 nathanw #endif
1611 1.2.2.2 nathanw
1612