Home | History | Annotate | Line # | Download | only in usb
      1 /*	$NetBSD: umcpmio.h,v 1.3 2025/11/29 18:39:15 brad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2024 Brad Spencer <brad (at) anduin.eldar.org>
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  */
     18 
     19 #ifndef _UMCPMIO_H_
     20 #define _UMCPMIO_H_
     21 
     22 #include <sys/param.h>
     23 #include <sys/types.h>
     24 
     25 #include <sys/condvar.h>
     26 #include <sys/conf.h>
     27 #include <sys/device.h>
     28 #include <sys/file.h>
     29 #include <sys/gpio.h>
     30 #include <sys/kauth.h>
     31 #include <sys/kernel.h>
     32 #include <sys/kmem.h>
     33 #include <sys/lwp.h>
     34 #include <sys/mutex.h>
     35 #include <sys/sysctl.h>
     36 #include <sys/systm.h>
     37 #include <sys/tty.h>
     38 #include <sys/vnode.h>
     39 
     40 #include <dev/gpio/gpiovar.h>
     41 
     42 #include <dev/hid/hid.h>
     43 
     44 #include <dev/i2c/i2cvar.h>
     45 #include <dev/spi/spivar.h>
     46 
     47 #include <dev/usb/uhidev.h>
     48 #include <dev/usb/usbdevs.h>
     49 #include <dev/usb/usbdi.h>
     50 #include <dev/usb/usbdi_util.h>
     51 
     52 #include <dev/usb/umcpmio_info.h>
     53 
     54 #define MCP2221_VREF_NAME 7
     55 #define MCP2221_CD_NAME 7
     56 #define MCP2221_DC_NAME 4
     57 
     58 #define MCP2210_NPINS 9
     59 #define MCP2221_NPINS 4
     60 #define UMCPMIO_NUM_DEVS 5
     61 #define MCP2210_MAX_SPI_BYTES 60
     62 #define MCP2210_SPI_SLAVES 8
     63 
     64 enum umcpmio_minor_devs {
     65 	CONTROL_DEV = 0,
     66 	GP1_DEV = 1,
     67 	GP2_DEV = 2,
     68 	GP3_DEV = 3,
     69 	EEPROM_DEV = 4,
     70 };
     71 
     72 struct umcpmio_spi_received {
     73 	uint8_t	num_receive_bytes;
     74 	uint8_t receive_bytes[MCP2210_MAX_SPI_BYTES];
     75 	SIMPLEQ_ENTRY(umcpmio_spi_received) umcpmio_spi_received_q;
     76 };
     77 
     78 struct mcp2210_slave_config {
     79 	uint32_t	bit_rate;
     80 	uint8_t		mode;
     81 };
     82 
     83 struct umcpmio_softc {
     84 	device_t		sc_dev;
     85 	struct uhidev		*sc_hdev;
     86 	struct usbd_device	*sc_udev;
     87 	const struct umcpmio_chip_info *sc_chipinfo;
     88 	uint16_t		sc_product;
     89 
     90 	struct sysctllog	*sc_umcpmiolog;
     91 	bool			sc_dumpbuffer;
     92 
     93 	int			sc_cv_wait;
     94 	int			sc_response_errcnt;
     95 	int			sc_busy_delay;
     96 	int			sc_retry_busy_read;
     97 	int			sc_retry_busy_write;
     98 
     99 	kmutex_t		sc_action_mutex;
    100 
    101 	kcondvar_t		sc_res_cv;
    102 	kmutex_t		sc_res_mutex;
    103 	bool			sc_res_ready;
    104 	uint8_t			*sc_res_buffer;
    105 
    106 	device_t		sc_gpio_dev;
    107 	struct gpio_chipset_tag	sc_gpio_gc;
    108 	gpio_pin_t		sc_gpio_pins[MCP2210_NPINS];
    109 	int			sc_adcdac_pin_flags[MCP2221_NPINS];
    110 
    111 	bool			sc_spi_verbose;
    112 	kmutex_t		sc_spi_mutex;
    113 	bool			sc_running;
    114 	struct spi_controller	sc_spi;
    115 	SIMPLEQ_HEAD(,spi_transfer) sc_q;
    116 	struct spi_transfer	*sc_transfer;
    117 	struct spi_chunk	*sc_rchunk, *sc_wchunk;
    118 	SIMPLEQ_HEAD(,umcpmio_spi_received) sc_received[MCP2210_SPI_SLAVES];
    119 	struct mcp2210_slave_config sc_slave_configs[MCP2210_SPI_SLAVES];
    120 
    121 	struct i2c_controller	sc_i2c_tag;
    122 	device_t		sc_i2c_dev;
    123 	bool			sc_reportreadnostop;
    124 
    125 	bool			sc_dev_open[UMCPMIO_NUM_DEVS];
    126 
    127 	bool			sc_dying;
    128 };
    129 
    130 struct umcpmio_sysctl_name {
    131 	const char	*text;
    132 };
    133 
    134 #endif	/* _UMCPMIO_H_ */
    135