Home | History | Annotate | Line # | Download | only in usb
ukyopon.c revision 1.22
      1 /*	$NetBSD: ukyopon.c,v 1.22 2019/05/04 23:52:18 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2005 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 (at) augustsson.net) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * This code is derived from software contributed to The NetBSD Foundation
     12  * by ITOH Yasufumi.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: ukyopon.c,v 1.22 2019/05/04 23:52:18 mrg Exp $");
     38 
     39 #ifdef _KERNEL_OPT
     40 #include "opt_usb.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/conf.h>
     48 #include <sys/tty.h>
     49 #include <sys/file.h>
     50 #include <sys/select.h>
     51 #include <sys/proc.h>
     52 #include <sys/vnode.h>
     53 #include <sys/device.h>
     54 #include <sys/poll.h>
     55 
     56 #include <sys/bus.h>
     57 
     58 #include <dev/usb/usb.h>
     59 #include <dev/usb/usbcdc.h>
     60 
     61 #include <dev/usb/usbdi.h>
     62 #include <dev/usb/usbdi_util.h>
     63 #include <dev/usb/usbdivar.h>
     64 #include <dev/usb/usbdevs.h>
     65 #include <dev/usb/usb_quirks.h>
     66 
     67 #include <dev/usb/ucomvar.h>
     68 #include <dev/usb/umodemvar.h>
     69 #include <dev/usb/ukyopon.h>
     70 
     71 #ifdef UKYOPON_DEBUG
     72 #define DPRINTFN(n, x)	if (ukyopondebug > (n)) printf x
     73 int	ukyopondebug = 0;
     74 #else
     75 #define DPRINTFN(n, x)
     76 #endif
     77 #define DPRINTF(x) DPRINTFN(0, x)
     78 
     79 struct ukyopon_softc {
     80 	/* generic umodem device */
     81 	struct umodem_softc	sc_umodem;
     82 
     83 	/* ukyopon addition */
     84 };
     85 
     86 #define UKYOPON_MODEM_IFACE_INDEX	0
     87 #define UKYOPON_DATA_IFACE_INDEX	3
     88 
     89 static void	ukyopon_get_status(void *, int, u_char *, u_char *);
     90 static int	ukyopon_ioctl(void *, int, u_long, void *, int, proc_t *);
     91 static void	ukyopon_set(void *, int, int, int);
     92 static int	ukyopon_param(void *, int, struct termios *);
     93 static int	ukyopon_open(void *, int);
     94 static void	ukyopon_close(void *, int);
     95 
     96 static struct ucom_methods ukyopon_methods = {
     97 	.ucom_get_status = ukyopon_get_status,
     98 	.ucom_set = ukyopon_set,
     99 	.ucom_param = ukyopon_param,
    100 	.ucom_ioctl = ukyopon_ioctl,
    101 	.ucom_open = ukyopon_open,
    102 	.ucom_close = ukyopon_close,
    103 };
    104 
    105 static int	ukyopon_match(device_t, cfdata_t, void *);
    106 static void	ukyopon_attach(device_t, device_t, void *);
    107 static int	ukyopon_detach(device_t, int);
    108 static int	ukyopon_activate(device_t, enum devact);
    109 extern struct cfdriver ukyopon_cd;
    110 CFATTACH_DECL_NEW(ukyopon, sizeof(struct ukyopon_softc), ukyopon_match,
    111     ukyopon_attach, ukyopon_detach, ukyopon_activate);
    112 
    113 static int
    114 ukyopon_match(device_t parent, cfdata_t match, void *aux)
    115 {
    116 	struct usbif_attach_arg *uiaa = aux;
    117 
    118 	if (uiaa->uiaa_vendor == USB_VENDOR_KYOCERA &&
    119 	    uiaa->uiaa_product == USB_PRODUCT_KYOCERA_AHK3001V &&
    120 	    (uiaa->uiaa_ifaceno == UKYOPON_MODEM_IFACE_INDEX ||
    121 	     uiaa->uiaa_ifaceno == UKYOPON_DATA_IFACE_INDEX))
    122 		return UMATCH_VENDOR_PRODUCT;
    123 
    124 	return UMATCH_NONE;
    125 }
    126 
    127 static void
    128 ukyopon_attach(device_t parent, device_t self, void *aux)
    129 {
    130 	struct ukyopon_softc *sc = device_private(self);
    131 	struct usbif_attach_arg *uiaa = aux;
    132 	struct ucom_attach_args ucaa;
    133 
    134 	ucaa.ucaa_portno = (uiaa->uiaa_ifaceno == UKYOPON_MODEM_IFACE_INDEX) ?
    135 		UKYOPON_PORT_MODEM : UKYOPON_PORT_DATA;
    136 	ucaa.ucaa_methods = &ukyopon_methods;
    137 	ucaa.ucaa_info = (uiaa->uiaa_ifaceno == UKYOPON_MODEM_IFACE_INDEX) ?
    138 	    "modem port" : "data transfer port";
    139 
    140 	if (umodem_common_attach(self, &sc->sc_umodem, uiaa, &ucaa))
    141 		return;
    142 	return;
    143 }
    144 
    145 static void
    146 ukyopon_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    147 {
    148 	struct ukyopon_softc *sc = addr;
    149 
    150 	/*
    151 	 * The device doesn't set DCD (Data Carrier Detect) bit properly.
    152 	 * Assume DCD is always present.
    153 	 */
    154 	if ((sc->sc_umodem.sc_msr & UMSR_DCD) == 0)
    155 		sc->sc_umodem.sc_msr |= UMSR_DCD;
    156 
    157 	umodem_get_status(addr, portno, lsr, msr);
    158 }
    159 
    160 static void
    161 ukyopon_set(void *addr, int portno, int reg, int onoff)
    162 {
    163 	struct ukyopon_softc *sc = addr;
    164 
    165 	umodem_set(&sc->sc_umodem, portno, reg, onoff);
    166 }
    167 
    168 static int
    169 ukyopon_param(void *addr, int portno, struct termios *t)
    170 {
    171 	struct ukyopon_softc *sc = addr;
    172 
    173 	return umodem_param(&sc->sc_umodem, portno, t);
    174 }
    175 
    176 static int
    177 ukyopon_open(void *addr, int portno)
    178 {
    179 	struct ukyopon_softc *sc = addr;
    180 
    181 	return umodem_open(&sc->sc_umodem, portno);
    182 }
    183 
    184 static void
    185 ukyopon_close(void *addr, int portno)
    186 {
    187 	struct ukyopon_softc *sc = addr;
    188 
    189 	umodem_close(&sc->sc_umodem, portno);
    190 }
    191 
    192 
    193 static int
    194 ukyopon_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
    195 	      proc_t *p)
    196 {
    197 	struct ukyopon_softc *sc = addr;
    198 	struct ukyopon_identify *arg_id = (void*)data;
    199 	int error = 0;
    200 
    201 	switch (cmd) {
    202 	case UKYOPON_IDENTIFY:
    203 		strncpy(arg_id->ui_name, UKYOPON_NAME, sizeof(arg_id->ui_name));
    204 		arg_id->ui_busno =
    205 		    device_unit(sc->sc_umodem.sc_udev->ud_bus->ub_usbctl);
    206 		arg_id->ui_address = sc->sc_umodem.sc_udev->ud_addr;
    207 		arg_id->ui_model = UKYOPON_MODEL_UNKNOWN;
    208 		arg_id->ui_porttype = portno;
    209 		break;
    210 
    211 	default:
    212 		error = umodem_ioctl(addr, portno, cmd, data, flag, p);
    213 		break;
    214 	}
    215 
    216 	return error;
    217 }
    218 
    219 int
    220 ukyopon_activate(device_t self, enum devact act)
    221 {
    222 	struct ukyopon_softc *sc = device_private(self);
    223 
    224 	return umodem_common_activate(&sc->sc_umodem, act);
    225 }
    226 
    227 int
    228 ukyopon_detach(device_t self, int flags)
    229 {
    230 	struct ukyopon_softc *sc = device_private(self);
    231 
    232 	return umodem_common_detach(&sc->sc_umodem, flags);
    233 }
    234