1 /* $NetBSD: if_upl.c,v 1.77 2022/03/03 05:56:28 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Prolific PL2301/PL2302 driver
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1.77 2022/03/03 05:56:28 riastradh Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_inet.h"
42 #include "opt_usb.h"
43 #endif
44
45 #include <sys/param.h>
46
47 #include <dev/usb/usbnet.h>
48
49 #include <net/if_types.h>
50
51 #ifdef INET
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/if_inarp.h>
55 #endif
56
57 /*
58 * 7 6 5 4 3 2 1 0
59 * tx rx 1 0
60 * 1110 0000 rxdata
61 * 1010 0000 idle
62 * 0010 0000 tx over
63 * 0110 tx over + rxd
64 */
65
66 #define UPL_RXDATA 0x40
67 #define UPL_TXOK 0x80
68
69 #define UPL_CONFIG_NO 1
70 #define UPL_IFACE_IDX 0
71
72 /***/
73
74 #define UPL_INTR_INTERVAL 20
75
76 #define UPL_BUFSZ 1024
77
78 #define UPL_RX_LIST_CNT 1
79 #define UPL_TX_LIST_CNT 1
80
81 #ifdef UPL_DEBUG
82 #define DPRINTF(x) if (upldebug) printf x
83 #define DPRINTFN(n,x) if (upldebug >= (n)) printf x
84 int upldebug = 0;
85 #else
86 #define DPRINTF(x)
87 #define DPRINTFN(n,x)
88 #endif
89
90 /*
91 * Various supported device vendors/products.
92 */
93 static const struct usb_devno sc_devs[] = {
94 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301 },
95 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302 },
96 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL25A1 },
97 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U258 },
98 { USB_VENDOR_NI, USB_PRODUCT_NI_HTOH_7825 }
99 };
100
101 static int upl_match(device_t, cfdata_t, void *);
102 static void upl_attach(device_t, device_t, void *);
103
104 CFATTACH_DECL_NEW(upl, sizeof(struct usbnet), upl_match, upl_attach,
105 usbnet_detach, usbnet_activate);
106
107 #if 0
108 static void upl_uno_intr(struct usbnet *, usbd_status);
109 #endif
110 static void upl_uno_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
111 static unsigned upl_uno_tx_prepare(struct usbnet *, struct mbuf *,
112 struct usbnet_chain *);
113 static int upl_uno_ioctl(struct ifnet *, u_long, void *);
114
115 static const struct usbnet_ops upl_ops = {
116 .uno_tx_prepare = upl_uno_tx_prepare,
117 .uno_rx_loop = upl_uno_rx_loop,
118 .uno_ioctl = upl_uno_ioctl,
119 #if 0
120 .uno_intr = upl_uno_intr,
121 #endif
122 };
123
124 static int upl_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
125 const struct rtentry *);
126 static void upl_input(struct ifnet *, struct mbuf *);
127
128 /*
129 * Probe for a Prolific chip.
130 */
131 static int
upl_match(device_t parent,cfdata_t match,void * aux)132 upl_match(device_t parent, cfdata_t match, void *aux)
133 {
134 struct usb_attach_arg *uaa = aux;
135
136 return usb_lookup(sc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL ?
137 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
138 }
139
140 static void
upl_attach(device_t parent,device_t self,void * aux)141 upl_attach(device_t parent, device_t self, void *aux)
142 {
143 struct usbnet * const un = device_private(self);
144 struct usb_attach_arg *uaa = aux;
145 char *devinfop;
146 struct usbd_device * dev = uaa->uaa_device;
147 usbd_status err;
148 usb_interface_descriptor_t *id;
149 usb_endpoint_descriptor_t *ed;
150 int i;
151
152 DPRINTFN(5,(" : upl_attach: un=%p, dev=%p", un, dev));
153
154 aprint_naive("\n");
155 aprint_normal("\n");
156 devinfop = usbd_devinfo_alloc(dev, 0);
157 aprint_normal_dev(self, "%s\n", devinfop);
158 usbd_devinfo_free(devinfop);
159
160 un->un_dev = self;
161 un->un_udev = dev;
162 un->un_sc = un;
163 un->un_ops = &upl_ops;
164 un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
165 un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
166 un->un_rx_list_cnt = UPL_RX_LIST_CNT;
167 un->un_tx_list_cnt = UPL_TX_LIST_CNT;
168 un->un_rx_bufsz = UPL_BUFSZ;
169 un->un_tx_bufsz = UPL_BUFSZ;
170
171 err = usbd_set_config_no(dev, UPL_CONFIG_NO, 1);
172 if (err) {
173 aprint_error_dev(self, "failed to set configuration"
174 ", err=%s\n", usbd_errstr(err));
175 return;
176 }
177
178 err = usbd_device2interface_handle(dev, UPL_IFACE_IDX, &un->un_iface);
179 if (err) {
180 aprint_error_dev(self, "getting interface handle failed\n");
181 return;
182 }
183
184 id = usbd_get_interface_descriptor(un->un_iface);
185
186 /* Find endpoints. */
187 for (i = 0; i < id->bNumEndpoints; i++) {
188 ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
189 if (ed == NULL) {
190 aprint_error_dev(self, "couldn't get ep %d\n", i);
191 return;
192 }
193 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
194 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
195 un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
196 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
197 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
198 un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
199 }
200 }
201
202 if (un->un_ed[USBNET_ENDPT_RX] == 0 || un->un_ed[USBNET_ENDPT_TX] == 0 /*||
203 un->un_ed[USBNET_ENDPT_INTR] == 0*/) {
204 aprint_error_dev(self, "missing endpoint\n");
205 return;
206 }
207
208 usbnet_attach(un);
209
210 /* Initialize interface info.*/
211 struct ifnet *ifp = usbnet_ifp(un);
212 ifp->if_mtu = UPL_BUFSZ;
213 ifp->if_type = IFT_OTHER;
214 ifp->if_addrlen = 0;
215 ifp->if_hdrlen = 0;
216 ifp->if_output = upl_output;
217 ifp->_if_input = upl_input;
218 ifp->if_baudrate = 12000000;
219 ifp->if_dlt = DLT_RAW;
220
221 usbnet_attach_ifp(un, IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX,
222 0, NULL);
223 }
224
225 static void
upl_uno_rx_loop(struct usbnet * un,struct usbnet_chain * c,uint32_t total_len)226 upl_uno_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len)
227 {
228
229 DPRINTFN(9,("%s: %s: enter length=%d\n",
230 device_xname(un->un_dev), __func__, total_len));
231
232 usbnet_input(un, c->unc_buf, total_len);
233 }
234
235 static unsigned
upl_uno_tx_prepare(struct usbnet * un,struct mbuf * m,struct usbnet_chain * c)236 upl_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
237 {
238 int total_len;
239
240 if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz)
241 return 0;
242
243 m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
244 total_len = m->m_pkthdr.len;
245
246 DPRINTFN(10,("%s: %s: total_len=%d\n",
247 device_xname(un->un_dev), __func__, total_len));
248
249 return total_len;
250 }
251
252 static int
upl_uno_ioctl(struct ifnet * ifp,u_long cmd,void * data)253 upl_uno_ioctl(struct ifnet *ifp, u_long cmd, void *data)
254 {
255 if (cmd == SIOCSIFMTU) {
256 struct ifreq *ifr = data;
257
258 if (ifr->ifr_mtu > UPL_BUFSZ)
259 return EINVAL;
260 }
261 return 0;
262 }
263
264 static int
upl_output(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,const struct rtentry * rt0)265 upl_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
266 const struct rtentry *rt0)
267 {
268 struct usbnet * const un __unused = ifp->if_softc;
269
270 DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev), __func__));
271
272 /* If the queueing discipline needs packet classification, do it now. */
273 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
274
275 /*
276 * Queue message on interface, and start output if interface
277 * not yet active.
278 */
279 return if_transmit_lock(ifp, m);
280 }
281
282 static void
upl_input(struct ifnet * ifp,struct mbuf * m)283 upl_input(struct ifnet *ifp, struct mbuf *m)
284 {
285 #ifdef INET
286 size_t pktlen = m->m_len;
287 int s;
288
289 s = splnet();
290 if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
291 if_statinc(ifp, if_iqdrops);
292 m_freem(m);
293 } else {
294 if_statadd2(ifp, if_ipackets, 1, if_ibytes, pktlen);
295 }
296 splx(s);
297 #endif
298 }
299
300 #ifdef _MODULE
301 #include "ioconf.c"
302 #endif
303
304 USBNET_MODULE(upl)
305