Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3) /* Driver for Theobroma Systems UCAN devices, Protocol Version 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2018 Theobroma Systems Design und Consulting GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * General Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * The USB Device uses three Endpoints:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *   CONTROL Endpoint: Is used the setup the device (start, stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *   info, configure).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *   IN Endpoint: The device sends CAN Frame Messages and Device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *   Information using the IN endpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *   OUT Endpoint: The driver sends configuration requests, and CAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *   Frames on the out endpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * Error Handling:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  *   If error reporting is turned on the device encodes error into CAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  *   error frames (see uapi/linux/can/error.h) and sends it using the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  *   IN Endpoint. The driver updates statistics and forward it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/can.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/can/dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/can/error.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define UCAN_DRIVER_NAME "ucan"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define UCAN_MAX_RX_URBS 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) /* the CAN controller needs a while to enable/disable the bus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #define UCAN_USB_CTL_PIPE_TIMEOUT 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) /* this driver currently supports protocol version 3 only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define UCAN_PROTOCOL_VERSION_MIN 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define UCAN_PROTOCOL_VERSION_MAX 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) /* UCAN Message Definitions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * ------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  *  ucan_message_out_t and ucan_message_in_t define the messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  *  transmitted on the OUT and IN endpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  *  Multibyte fields are transmitted with little endianness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  *  INTR Endpoint: a single uint32_t storing the current space in the fifo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  *  OUT Endpoint: single message of type ucan_message_out_t is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  *    transmitted on the out endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  *  IN Endpoint: multiple messages ucan_message_in_t concateted in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  *    the following way:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  *	m[n].len <=> the length if message n(including the header in bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  *	m[n] is is aligned to a 4 byte boundary, hence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  *	  offset(m[0])	 := 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  *	  offset(m[n+1]) := offset(m[n]) + (m[n].len + 3) & 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  *	this implies that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  *	  offset(m[n]) % 4 <=> 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) /* Device Global Commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	UCAN_DEVICE_GET_FW_STRING = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) /* UCAN Commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	/* start the can transceiver - val defines the operation mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	UCAN_COMMAND_START = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	/* cancel pending transmissions and stop the can transceiver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	UCAN_COMMAND_STOP = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	/* send can transceiver into low-power sleep mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	UCAN_COMMAND_SLEEP = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	/* wake up can transceiver from low-power sleep mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	UCAN_COMMAND_WAKEUP = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	/* reset the can transceiver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	UCAN_COMMAND_RESET = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	/* get piece of info from the can transceiver - subcmd defines what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	 * piece
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	UCAN_COMMAND_GET = 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	/* clear or disable hardware filter - subcmd defines which of the two */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	UCAN_COMMAND_FILTER = 6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	/* Setup bittiming */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	UCAN_COMMAND_SET_BITTIMING = 7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	/* recover from bus-off state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	UCAN_COMMAND_RESTART = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) /* UCAN_COMMAND_START and UCAN_COMMAND_GET_INFO operation modes (bitmap).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * Undefined bits must be set to 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	UCAN_MODE_LOOPBACK = BIT(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	UCAN_MODE_SILENT = BIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	UCAN_MODE_3_SAMPLES = BIT(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	UCAN_MODE_ONE_SHOT = BIT(3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	UCAN_MODE_BERR_REPORT = BIT(4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) /* UCAN_COMMAND_GET subcommands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	UCAN_COMMAND_GET_INFO = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	UCAN_COMMAND_GET_PROTOCOL_VERSION = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) /* UCAN_COMMAND_FILTER subcommands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	UCAN_FILTER_CLEAR = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	UCAN_FILTER_DISABLE = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	UCAN_FILTER_ENABLE = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) /* OUT endpoint message types */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	UCAN_OUT_TX = 2,     /* transmit a CAN frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) /* IN endpoint message types */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	UCAN_IN_TX_COMPLETE = 1,  /* CAN frame transmission completed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	UCAN_IN_RX = 2,           /* CAN frame received */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) struct ucan_ctl_cmd_start {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	__le16 mode;         /* OR-ing any of UCAN_MODE_* */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) struct ucan_ctl_cmd_set_bittiming {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	__le32 tq;           /* Time quanta (TQ) in nanoseconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	__le16 brp;          /* TQ Prescaler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	__le16 sample_point; /* Samplepoint on tenth percent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	u8 prop_seg;         /* Propagation segment in TQs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	u8 phase_seg1;       /* Phase buffer segment 1 in TQs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	u8 phase_seg2;       /* Phase buffer segment 2 in TQs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	u8 sjw;              /* Synchronisation jump width in TQs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) struct ucan_ctl_cmd_device_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	__le32 freq;         /* Clock Frequency for tq generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	u8 tx_fifo;          /* Size of the transmission fifo */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	u8 sjw_max;          /* can_bittiming fields... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	u8 tseg1_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	u8 tseg1_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	u8 tseg2_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	u8 tseg2_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	__le16 brp_inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	__le32 brp_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	__le32 brp_max;      /* ...can_bittiming fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	__le16 ctrlmodes;    /* supported control modes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	__le16 hwfilter;     /* Number of HW filter banks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	__le16 rxmboxes;     /* Number of receive Mailboxes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) struct ucan_ctl_cmd_get_protocol_version {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	__le32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) union ucan_ctl_payload {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	/* Setup Bittiming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	 * bmRequest == UCAN_COMMAND_START
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	struct ucan_ctl_cmd_start cmd_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	/* Setup Bittiming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	 * bmRequest == UCAN_COMMAND_SET_BITTIMING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	struct ucan_ctl_cmd_set_bittiming cmd_set_bittiming;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	/* Get Device Information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	 * bmRequest == UCAN_COMMAND_GET; wValue = UCAN_COMMAND_GET_INFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	struct ucan_ctl_cmd_device_info cmd_get_device_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	/* Get Protocol Version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	 * bmRequest == UCAN_COMMAND_GET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	 * wValue = UCAN_COMMAND_GET_PROTOCOL_VERSION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	struct ucan_ctl_cmd_get_protocol_version cmd_get_protocol_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	u8 raw[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	UCAN_TX_COMPLETE_SUCCESS = BIT(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) /* Transmission Complete within ucan_message_in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) struct ucan_tx_complete_entry_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	u8 echo_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	u8 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) } __packed __aligned(0x2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) /* CAN Data message format within ucan_message_in/out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) struct ucan_can_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	/* note DLC is computed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	 *    msg.len - sizeof (msg.len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	 *            - sizeof (msg.type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	 *            - sizeof (msg.can_msg.id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	__le32 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		u8 data[CAN_MAX_DLEN];  /* Data of CAN frames */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		u8 dlc;                 /* RTR dlc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) /* OUT Endpoint, outbound messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) struct ucan_message_out {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	__le16 len; /* Length of the content include header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	u8 type;    /* UCAN_OUT_TX and friends */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	u8 subtype; /* command sub type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		/* Transmit CAN frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		 * (type == UCAN_TX) && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		 * subtype stores the echo id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		struct ucan_can_msg can_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	} msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) } __packed __aligned(0x4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) /* IN Endpoint, inbound messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) struct ucan_message_in {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	__le16 len; /* Length of the content include header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	u8 type;    /* UCAN_IN_RX and friends */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	u8 subtype; /* command sub type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		/* CAN Frame received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		 * (type == UCAN_IN_RX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		 * && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		struct ucan_can_msg can_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		/* CAN transmission complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		 * (type == UCAN_IN_TX_COMPLETE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		struct ucan_tx_complete_entry_t can_tx_complete_msg[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	} __aligned(0x4) msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) /* Macros to calculate message lengths */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) #define UCAN_OUT_HDR_SIZE offsetof(struct ucan_message_out, msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) #define UCAN_IN_HDR_SIZE offsetof(struct ucan_message_in, msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) #define UCAN_IN_LEN(member) (UCAN_OUT_HDR_SIZE + sizeof(member))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) struct ucan_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) /* Context Information for transmission URBs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) struct ucan_urb_context {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	struct ucan_priv *up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	u8 dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	bool allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) /* Information reported by the USB device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) struct ucan_device_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	struct can_bittiming_const bittiming_const;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	u8 tx_fifo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) /* Driver private data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) struct ucan_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	/* must be the first member */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	struct can_priv can;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	/* linux USB device structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	struct usb_interface *intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	struct net_device *netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	/* lock for can->echo_skb (used around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	 * can_put/get/free_echo_skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	spinlock_t echo_skb_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	/* usb device information information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	u8 intf_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	u8 in_ep_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	u8 out_ep_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	u16 in_ep_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	/* transmission and reception buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	struct usb_anchor rx_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	struct usb_anchor tx_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	union ucan_ctl_payload *ctl_msg_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	struct ucan_device_info device_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	/* transmission control information and locks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	spinlock_t context_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	unsigned int available_tx_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	struct ucan_urb_context *context_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) static u8 ucan_get_can_dlc(struct ucan_can_msg *msg, u16 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	if (le32_to_cpu(msg->id) & CAN_RTR_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		return get_can_dlc(msg->dlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		return get_can_dlc(len - (UCAN_IN_HDR_SIZE + sizeof(msg->id)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) static void ucan_release_context_array(struct ucan_priv *up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	if (!up->context_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	/* lock is not needed because, driver is currently opening or closing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	up->available_tx_urbs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	kfree(up->context_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	up->context_array = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) static int ucan_alloc_context_array(struct ucan_priv *up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	/* release contexts if any */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	ucan_release_context_array(up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	up->context_array = kcalloc(up->device_info.tx_fifo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 				    sizeof(*up->context_array),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 				    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	if (!up->context_array) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 			   "Not enough memory to allocate tx contexts\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	for (i = 0; i < up->device_info.tx_fifo; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		up->context_array[i].allocated = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		up->context_array[i].up = up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	/* lock is not needed because, driver is currently opening */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	up->available_tx_urbs = up->device_info.tx_fifo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) static struct ucan_urb_context *ucan_alloc_context(struct ucan_priv *up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	struct ucan_urb_context *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	if (WARN_ON_ONCE(!up->context_array))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	/* execute context operation atomically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	spin_lock_irqsave(&up->context_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	for (i = 0; i < up->device_info.tx_fifo; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		if (!up->context_array[i].allocated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			/* update context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			ret = &up->context_array[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 			up->context_array[i].allocated = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 			/* stop queue if necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 			up->available_tx_urbs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			if (!up->available_tx_urbs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 				netif_stop_queue(up->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	spin_unlock_irqrestore(&up->context_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) static bool ucan_release_context(struct ucan_priv *up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 				 struct ucan_urb_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	if (WARN_ON_ONCE(!up->context_array))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	/* execute context operation atomically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	spin_lock_irqsave(&up->context_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	/* context was not allocated, maybe the device sent garbage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	if (ctx->allocated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		ctx->allocated = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		/* check if the queue needs to be woken */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		if (!up->available_tx_urbs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 			netif_wake_queue(up->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		up->available_tx_urbs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	spin_unlock_irqrestore(&up->context_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) static int ucan_ctrl_command_out(struct ucan_priv *up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 				 u8 cmd, u16 subcmd, u16 datalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	return usb_control_msg(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 			       usb_sndctrlpipe(up->udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			       cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 			       USB_DIR_OUT | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 						USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 			       subcmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 			       up->intf_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 			       up->ctl_msg_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 			       datalen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 			       UCAN_USB_CTL_PIPE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) static int ucan_device_request_in(struct ucan_priv *up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 				  u8 cmd, u16 subcmd, u16 datalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	return usb_control_msg(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 			       usb_rcvctrlpipe(up->udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 			       cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			       USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			       subcmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			       0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 			       up->ctl_msg_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			       datalen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 			       UCAN_USB_CTL_PIPE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) /* Parse the device information structure reported by the device and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442)  * setup private variables accordingly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) static void ucan_parse_device_info(struct ucan_priv *up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 				   struct ucan_ctl_cmd_device_info *device_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	struct can_bittiming_const *bittiming =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		&up->device_info.bittiming_const;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	u16 ctrlmodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	/* store the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	up->can.clock.freq = le32_to_cpu(device_info->freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	up->device_info.tx_fifo = device_info->tx_fifo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	strcpy(bittiming->name, "ucan");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	bittiming->tseg1_min = device_info->tseg1_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	bittiming->tseg1_max = device_info->tseg1_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	bittiming->tseg2_min = device_info->tseg2_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	bittiming->tseg2_max = device_info->tseg2_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	bittiming->sjw_max = device_info->sjw_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	bittiming->brp_min = le32_to_cpu(device_info->brp_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	bittiming->brp_max = le32_to_cpu(device_info->brp_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	bittiming->brp_inc = le16_to_cpu(device_info->brp_inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	ctrlmodes = le16_to_cpu(device_info->ctrlmodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	up->can.ctrlmode_supported = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	if (ctrlmodes & UCAN_MODE_LOOPBACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		up->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	if (ctrlmodes & UCAN_MODE_SILENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		up->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	if (ctrlmodes & UCAN_MODE_3_SAMPLES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		up->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	if (ctrlmodes & UCAN_MODE_ONE_SHOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		up->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	if (ctrlmodes & UCAN_MODE_BERR_REPORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		up->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) /* Handle a CAN error frame that we have received from the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481)  * Returns true if the can state has changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) static bool ucan_handle_error_frame(struct ucan_priv *up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 				    struct ucan_message_in *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 				    canid_t canid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	enum can_state new_state = up->can.state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	struct net_device_stats *net_stats = &up->netdev->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	struct can_device_stats *can_stats = &up->can.can_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	if (canid & CAN_ERR_LOSTARB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		can_stats->arbitration_lost++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	if (canid & CAN_ERR_BUSERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		can_stats->bus_error++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	if (canid & CAN_ERR_ACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		net_stats->tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (canid & CAN_ERR_BUSOFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		new_state = CAN_STATE_BUS_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	/* controller problems, details in data[1] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	if (canid & CAN_ERR_CRTL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		u8 d1 = m->msg.can_msg.data[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		if (d1 & CAN_ERR_CRTL_RX_OVERFLOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 			net_stats->rx_over_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		/* controller state bits: if multiple are set the worst wins */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		if (d1 & CAN_ERR_CRTL_ACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 			new_state = CAN_STATE_ERROR_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		if (d1 & (CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 			new_state = CAN_STATE_ERROR_WARNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		if (d1 & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 			new_state = CAN_STATE_ERROR_PASSIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	/* protocol error, details in data[2] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	if (canid & CAN_ERR_PROT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		u8 d2 = m->msg.can_msg.data[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		if (d2 & CAN_ERR_PROT_TX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			net_stats->tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 			net_stats->rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	/* no state change - we are done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (up->can.state == new_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	/* we switched into a better state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (up->can.state > new_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		up->can.state = new_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	/* we switched into a worse state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	up->can.state = new_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	switch (new_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	case CAN_STATE_BUS_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		can_stats->bus_off++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		can_bus_off(up->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	case CAN_STATE_ERROR_PASSIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		can_stats->error_passive++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	case CAN_STATE_ERROR_WARNING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		can_stats->error_warning++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) /* Callback on reception of a can frame via the IN endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562)  * This function allocates an skb and transferres it to the Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563)  * network stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) static void ucan_rx_can_msg(struct ucan_priv *up, struct ucan_message_in *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	canid_t canid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	struct can_frame *cf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	struct net_device_stats *stats = &up->netdev->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	/* get the contents of the length field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	len = le16_to_cpu(m->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	/* check sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	if (len < UCAN_IN_HDR_SIZE + sizeof(m->msg.can_msg.id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		netdev_warn(up->netdev, "invalid input message len: %d\n", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	/* handle error frames */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	canid = le32_to_cpu(m->msg.can_msg.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	if (canid & CAN_ERR_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		bool busstate_changed = ucan_handle_error_frame(up, m, canid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		/* if berr-reporting is off only state changes get through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		if (!(up->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		    !busstate_changed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		canid_t canid_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		/* compute the mask for canid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		canid_mask = CAN_RTR_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		if (canid & CAN_EFF_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 			canid_mask |= CAN_EFF_MASK | CAN_EFF_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 			canid_mask |= CAN_SFF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		if (canid & ~canid_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 			netdev_warn(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 				    "unexpected bits set (canid %x, mask %x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 				    canid, canid_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		canid &= canid_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	/* allocate skb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	skb = alloc_can_skb(up->netdev, &cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	/* fill the can frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	cf->can_id = canid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	/* compute DLC taking RTR_FLAG into account */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	cf->can_dlc = ucan_get_can_dlc(&m->msg.can_msg, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	/* copy the payload of non RTR frames */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	if (!(cf->can_id & CAN_RTR_FLAG) || (cf->can_id & CAN_ERR_FLAG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		memcpy(cf->data, m->msg.can_msg.data, cf->can_dlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	/* don't count error frames as real packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	stats->rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	stats->rx_bytes += cf->can_dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	/* pass it to Linux */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) /* callback indicating completed transmission */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) static void ucan_tx_complete_msg(struct ucan_priv *up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 				 struct ucan_message_in *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	u16 count, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	u8 echo_index, dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	u16 len = le16_to_cpu(m->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	struct ucan_urb_context *context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (len < UCAN_IN_HDR_SIZE || (len % 2 != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		netdev_err(up->netdev, "invalid tx complete length\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	count = (len - UCAN_IN_HDR_SIZE) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		/* we did not submit such echo ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		echo_index = m->msg.can_tx_complete_msg[i].echo_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		if (echo_index >= up->device_info.tx_fifo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 			up->netdev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 			netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 				   "invalid echo_index %d received\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 				   echo_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		/* gather information from the context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		context = &up->context_array[echo_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		dlc = READ_ONCE(context->dlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		/* Release context and restart queue if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		 * Also check if the context was allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		if (!ucan_release_context(up, context))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		spin_lock_irqsave(&up->echo_skb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		if (m->msg.can_tx_complete_msg[i].flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		    UCAN_TX_COMPLETE_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 			/* update statistics */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 			up->netdev->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			up->netdev->stats.tx_bytes += dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 			can_get_echo_skb(up->netdev, echo_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 			up->netdev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 			can_free_echo_skb(up->netdev, echo_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		spin_unlock_irqrestore(&up->echo_skb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) /* callback on reception of a USB message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) static void ucan_read_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	struct ucan_priv *up = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	struct net_device *netdev = up->netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	struct ucan_message_in *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	/* the device is not up and the driver should not receive any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	 * data on the bulk in pipe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	if (WARN_ON(!up->context_array)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		usb_free_coherent(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 				  up->in_ep_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 				  urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 				  urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	/* check URB status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	switch (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	case -EPIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	case -EPROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	case -ETIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		/* urb is not resubmitted -> free dma data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		usb_free_coherent(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 				  up->in_ep_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 				  urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 				  urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		netdev_dbg(up->netdev, "not resubmitting urb; status: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 			   urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		goto resubmit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	/* sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	if (!netif_device_present(netdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	/* iterate over input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	while (pos < urb->actual_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		/* check sanity (length of header) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			netdev_warn(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 				    "invalid message (short; no hdr; l:%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 				    urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 			goto resubmit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		/* setup the message address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		m = (struct ucan_message_in *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 			((u8 *)urb->transfer_buffer + pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		len = le16_to_cpu(m->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		/* check sanity (length of content) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		if (urb->actual_length - pos < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			netdev_warn(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 				    "invalid message (short; no data; l:%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 				    urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			print_hex_dump(KERN_WARNING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 				       "raw data: ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 				       DUMP_PREFIX_ADDRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 				       16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 				       1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 				       urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 				       urb->actual_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 				       true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 			goto resubmit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		switch (m->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		case UCAN_IN_RX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 			ucan_rx_can_msg(up, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		case UCAN_IN_TX_COMPLETE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 			ucan_tx_complete_msg(up, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 			netdev_warn(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 				    "invalid message (type; t:%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 				    m->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		/* proceed to next message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		pos += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		/* align to 4 byte boundary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		pos = round_up(pos, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) resubmit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	/* resubmit urb when done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	usb_fill_bulk_urb(urb, up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			  usb_rcvbulkpipe(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 					  up->in_ep_addr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			  urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 			  up->in_ep_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 			  ucan_read_bulk_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			  up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	usb_anchor_urb(urb, &up->rx_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	ret = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 			   "failed resubmitting read bulk urb: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 			   ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		usb_free_coherent(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 				  up->in_ep_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 				  urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 				  urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		if (ret == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 			netif_device_detach(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) /* callback after transmission of a USB message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) static void ucan_write_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	struct ucan_priv *up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	struct ucan_urb_context *context = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	/* get the urb context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	if (WARN_ON_ONCE(!context))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	/* free up our allocated buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	usb_free_coherent(urb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			  sizeof(struct ucan_message_out),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			  urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 			  urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	up = context->up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	if (WARN_ON_ONCE(!up))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	/* sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	if (!netif_device_present(up->netdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	/* transmission failed (USB - the device will not send a TX complete) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	if (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		netdev_warn(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 			    "failed to transmit USB message to device: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			     urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		/* update counters an cleanup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		spin_lock_irqsave(&up->echo_skb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		can_free_echo_skb(up->netdev, context - up->context_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		spin_unlock_irqrestore(&up->echo_skb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		up->netdev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		/* release context and restart the queue if necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		if (!ucan_release_context(up, context))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 			netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 				   "urb failed, failed to release context\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) static void ucan_cleanup_rx_urbs(struct ucan_priv *up, struct urb **urbs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		if (urbs[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			usb_unanchor_urb(urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			usb_free_coherent(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 					  up->in_ep_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 					  urbs[i]->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 					  urbs[i]->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			usb_free_urb(urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) static int ucan_prepare_and_anchor_rx_urbs(struct ucan_priv *up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 					   struct urb **urbs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		if (!urbs[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		buf = usb_alloc_coherent(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 					 up->in_ep_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 					 GFP_KERNEL, &urbs[i]->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			/* cleanup this urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			usb_free_urb(urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			urbs[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		usb_fill_bulk_urb(urbs[i], up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 				  usb_rcvbulkpipe(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 						  up->in_ep_addr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 				  buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 				  up->in_ep_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 				  ucan_read_bulk_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 				  up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		urbs[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		usb_anchor_urb(urbs[i], &up->rx_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	/* cleanup other unsubmitted urbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	ucan_cleanup_rx_urbs(up, urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) /* Submits rx urbs with the semantic: Either submit all, or cleanup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921)  * everything. I case of errors submitted urbs are killed and all urbs in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922)  * the array are freed. I case of no errors every entry in the urb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923)  * array is set to NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	/* Iterate over all urbs to submit. On success remove the urb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	 * from the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		ret = usb_submit_urb(urbs[i], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 				   "could not submit urb; code: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 				   ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		/* Anchor URB and drop reference, USB core will take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		 * care of freeing it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		usb_free_urb(urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		urbs[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	/* Cleanup unsubmitted urbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	ucan_cleanup_rx_urbs(up, urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	/* Kill urbs that are already submitted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	usb_kill_anchored_urbs(&up->rx_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) /* Open the network device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) static int ucan_open(struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	int ret, ret_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	u16 ctrlmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	struct urb *urbs[UCAN_MAX_RX_URBS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	struct ucan_priv *up = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	ret = ucan_alloc_context_array(up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	/* Allocate and prepare IN URBS - allocated and anchored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	 * urbs are stored in urbs[] for clean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	ret = ucan_prepare_and_anchor_rx_urbs(up, urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		goto err_contexts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	/* Check the control mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	ctrlmode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	if (up->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		ctrlmode |= UCAN_MODE_LOOPBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (up->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		ctrlmode |= UCAN_MODE_SILENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	if (up->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		ctrlmode |= UCAN_MODE_3_SAMPLES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	if (up->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		ctrlmode |= UCAN_MODE_ONE_SHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	/* Enable this in any case - filtering is down within the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	 * receive path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	ctrlmode |= UCAN_MODE_BERR_REPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	up->ctl_msg_buffer->cmd_start.mode = cpu_to_le16(ctrlmode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	/* Driver is ready to receive data - start the USB device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			   "could not start device, code: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			   ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		goto err_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	/* Call CAN layer open */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	ret = open_candev(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		goto err_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	/* Driver is ready to receive data. Submit RX URBS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	ret = ucan_submit_rx_urbs(up, urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		goto err_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	up->can.state = CAN_STATE_ERROR_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	/* Start the network queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	netif_start_queue(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) err_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	/* The device have started already stop it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	if (ret_cleanup < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			   "could not stop device, code: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 			   ret_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) err_reset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	/* The device might have received data, reset it for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	 * consistent state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	if (ret_cleanup < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			   "could not reset device, code: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 			   ret_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	/* clean up unsubmitted urbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	ucan_cleanup_rx_urbs(up, urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) err_contexts:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	ucan_release_context_array(up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) static struct urb *ucan_prepare_tx_urb(struct ucan_priv *up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 				       struct ucan_urb_context *context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 				       struct can_frame *cf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 				       u8 echo_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	int mlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	struct ucan_message_out *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	/* create a URB, and a buffer for it, and copy the data to the URB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	urb = usb_alloc_urb(0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		netdev_err(up->netdev, "no memory left for URBs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	m = usb_alloc_coherent(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			       sizeof(struct ucan_message_out),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			       GFP_ATOMIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 			       &urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	if (!m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		netdev_err(up->netdev, "no memory left for USB buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	/* build the USB message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	m->type = UCAN_OUT_TX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	m->msg.can_msg.id = cpu_to_le32(cf->can_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	if (cf->can_id & CAN_RTR_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		mlen = UCAN_OUT_HDR_SIZE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			offsetof(struct ucan_can_msg, dlc) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			sizeof(m->msg.can_msg.dlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		m->msg.can_msg.dlc = cf->can_dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		mlen = UCAN_OUT_HDR_SIZE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 			sizeof(m->msg.can_msg.id) + cf->can_dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		memcpy(m->msg.can_msg.data, cf->data, cf->can_dlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	m->len = cpu_to_le16(mlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	context->dlc = cf->can_dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	m->subtype = echo_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	/* build the urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	usb_fill_bulk_urb(urb, up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			  usb_sndbulkpipe(up->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 					  up->out_ep_addr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 			  m, mlen, ucan_write_bulk_callback, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	return urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) static void ucan_clean_up_tx_urb(struct ucan_priv *up, struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	usb_free_coherent(up->udev, sizeof(struct ucan_message_out),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			  urb->transfer_buffer, urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) /* callback when Linux needs to send a can frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) static netdev_tx_t ucan_start_xmit(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 				   struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	u8 echo_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	struct ucan_urb_context *context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	struct ucan_priv *up = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	struct can_frame *cf = (struct can_frame *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	/* check skb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	if (can_dropped_invalid_skb(netdev, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	/* allocate a context and slow down tx path, if fifo state is low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	context = ucan_alloc_context(up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	echo_index = context - up->context_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	if (WARN_ON_ONCE(!context))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		return NETDEV_TX_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	/* prepare urb for transmission */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	urb = ucan_prepare_tx_urb(up, context, cf, echo_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	/* put the skb on can loopback stack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	spin_lock_irqsave(&up->echo_skb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	can_put_echo_skb(skb, up->netdev, echo_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	spin_unlock_irqrestore(&up->echo_skb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	/* transmit it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	usb_anchor_urb(urb, &up->tx_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	ret = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	/* cleanup urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		/* on error, clean up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		ucan_clean_up_tx_urb(up, urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		if (!ucan_release_context(up, context))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 				   "xmit err: failed to release context\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		/* remove the skb from the echo stack - this also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		 * frees the skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		spin_lock_irqsave(&up->echo_skb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		can_free_echo_skb(up->netdev, echo_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		spin_unlock_irqrestore(&up->echo_skb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		if (ret == -ENODEV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 			netif_device_detach(up->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 			netdev_warn(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 				    "xmit err: failed to submit urb %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 				    ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 			up->netdev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	netif_trans_update(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	/* release ref, as we do not need the urb anymore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	if (!ucan_release_context(up, context))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 			   "xmit drop: failed to release context\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	up->netdev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) /* Device goes down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)  * Clean up used resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) static int ucan_close(struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	struct ucan_priv *up = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	up->can.state = CAN_STATE_STOPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	/* stop sending data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	usb_kill_anchored_urbs(&up->tx_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	/* stop receiving data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	usb_kill_anchored_urbs(&up->rx_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	/* stop and reset can device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 			   "could not stop device, code: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 			   ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		netdev_err(up->netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			   "could not reset device, code: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 			   ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	netif_stop_queue(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	ucan_release_context_array(up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	close_candev(up->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) /* CAN driver callbacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) static const struct net_device_ops ucan_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	.ndo_open = ucan_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	.ndo_stop = ucan_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	.ndo_start_xmit = ucan_start_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	.ndo_change_mtu = can_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) /* Request to set bittiming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)  * This function generates an USB set bittiming message and transmits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)  * it to the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) static int ucan_set_bittiming(struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	struct ucan_priv *up = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	struct ucan_ctl_cmd_set_bittiming *cmd_set_bittiming;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	cmd_set_bittiming = &up->ctl_msg_buffer->cmd_set_bittiming;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	cmd_set_bittiming->tq = cpu_to_le32(up->can.bittiming.tq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	cmd_set_bittiming->brp = cpu_to_le16(up->can.bittiming.brp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	cmd_set_bittiming->sample_point =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	    cpu_to_le16(up->can.bittiming.sample_point);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	cmd_set_bittiming->prop_seg = up->can.bittiming.prop_seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	cmd_set_bittiming->phase_seg1 = up->can.bittiming.phase_seg1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	cmd_set_bittiming->phase_seg2 = up->can.bittiming.phase_seg2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	cmd_set_bittiming->sjw = up->can.bittiming.sjw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 				    sizeof(*cmd_set_bittiming));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	return (ret < 0) ? ret : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) /* Restart the device to get it out of BUS-OFF state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)  * Called when the user runs "ip link set can1 type can restart".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) static int ucan_set_mode(struct net_device *netdev, enum can_mode mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	struct ucan_priv *up = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	switch (mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	case CAN_MODE_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		netdev_dbg(up->netdev, "restarting device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		up->can.state = CAN_STATE_ERROR_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		/* check if queue can be restarted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		 * up->available_tx_urbs must be protected by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		 * lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		spin_lock_irqsave(&up->context_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		if (up->available_tx_urbs > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 			netif_wake_queue(up->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		spin_unlock_irqrestore(&up->context_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) /* Probe the device, reset it and gather general device information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) static int ucan_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		      const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	u32 protocol_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	struct net_device *netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	struct usb_host_interface *iface_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	struct ucan_priv *up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	struct usb_endpoint_descriptor *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	u16 in_ep_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	u16 out_ep_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	u8 in_ep_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	u8 out_ep_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	union ucan_ctl_payload *ctl_msg_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	char firmware_str[sizeof(union ucan_ctl_payload) + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	udev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	/* Stage 1 - Interface Parsing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	 * ---------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	 * Identifie the device USB interface descriptor and its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	 * endpoints. Probing is aborted on errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	/* check if the interface is sane */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	iface_desc = intf->cur_altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	if (!iface_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	dev_info(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		 "%s: probing device on interface #%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		 UCAN_DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		 iface_desc->desc.bInterfaceNumber);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	/* interface sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	if (iface_desc->desc.bNumEndpoints != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		dev_err(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 			"%s: invalid EP count (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			UCAN_DRIVER_NAME, iface_desc->desc.bNumEndpoints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		goto err_firmware_needs_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	/* check interface endpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	in_ep_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	out_ep_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	in_ep_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	out_ep_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		ep = &iface_desc->endpoint[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		    ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		     USB_ENDPOINT_XFER_BULK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			/* In Endpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 			in_ep_addr = ep->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 			in_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			in_ep_size = le16_to_cpu(ep->wMaxPacketSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		} else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 			    0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 			   ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 			    USB_ENDPOINT_XFER_BULK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 			/* Out Endpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 			out_ep_addr = ep->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 			out_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			out_ep_size = le16_to_cpu(ep->wMaxPacketSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	/* check if interface is sane */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	if (!in_ep_addr || !out_ep_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		dev_err(&udev->dev, "%s: invalid endpoint configuration\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 			UCAN_DRIVER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		goto err_firmware_needs_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	if (in_ep_size < sizeof(struct ucan_message_in)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		dev_err(&udev->dev, "%s: invalid in_ep MaxPacketSize\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			UCAN_DRIVER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		goto err_firmware_needs_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	if (out_ep_size < sizeof(struct ucan_message_out)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		dev_err(&udev->dev, "%s: invalid out_ep MaxPacketSize\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 			UCAN_DRIVER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		goto err_firmware_needs_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	/* Stage 2 - Device Identification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	 * -------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	 * The device interface seems to be a ucan device. Do further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	 * compatibility checks. On error probing is aborted, on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	 * success this stage leaves the ctl_msg_buffer with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	 * reported contents of a GET_INFO command (supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	 * bittimings, tx_fifo depth). This information is used in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	 * Stage 3 for the final driver initialisation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	/* Prepare Memory for control transferes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	ctl_msg_buffer = devm_kzalloc(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 				      sizeof(union ucan_ctl_payload),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 				      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	if (!ctl_msg_buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		dev_err(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			"%s: failed to allocate control pipe memory\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 			UCAN_DRIVER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	/* get protocol version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	 * note: ucan_ctrl_command_* wrappers cannot be used yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	 * because `up` is initialised in Stage 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	ret = usb_control_msg(udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 			      usb_rcvctrlpipe(udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 			      UCAN_COMMAND_GET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 			      USB_DIR_IN | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 					USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 			      UCAN_COMMAND_GET_PROTOCOL_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 			      iface_desc->desc.bInterfaceNumber,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 			      ctl_msg_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 			      sizeof(union ucan_ctl_payload),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 			      UCAN_USB_CTL_PIPE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	/* older firmware version do not support this command - those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	 * are not supported by this drive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	if (ret != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		dev_err(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 			"%s: could not read protocol version, ret=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			UCAN_DRIVER_NAME, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		goto err_firmware_needs_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	/* this driver currently supports protocol version 3 only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	protocol_version =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		le32_to_cpu(ctl_msg_buffer->cmd_get_protocol_version.version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	if (protocol_version < UCAN_PROTOCOL_VERSION_MIN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	    protocol_version > UCAN_PROTOCOL_VERSION_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		dev_err(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 			"%s: device protocol version %d is not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 			UCAN_DRIVER_NAME, protocol_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		goto err_firmware_needs_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	/* request the device information and store it in ctl_msg_buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	 * note: ucan_ctrl_command_* wrappers cannot be used yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	 * because `up` is initialised in Stage 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	ret = usb_control_msg(udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 			      usb_rcvctrlpipe(udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 			      UCAN_COMMAND_GET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 			      USB_DIR_IN | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 					USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 			      UCAN_COMMAND_GET_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 			      iface_desc->desc.bInterfaceNumber,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 			      ctl_msg_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 			      sizeof(ctl_msg_buffer->cmd_get_device_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 			      UCAN_USB_CTL_PIPE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		dev_err(&udev->dev, "%s: failed to retrieve device info\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 			UCAN_DRIVER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		goto err_firmware_needs_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	if (ret < sizeof(ctl_msg_buffer->cmd_get_device_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		dev_err(&udev->dev, "%s: device reported invalid device info\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 			UCAN_DRIVER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		goto err_firmware_needs_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	if (ctl_msg_buffer->cmd_get_device_info.tx_fifo == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		dev_err(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 			"%s: device reported invalid tx-fifo size\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 			UCAN_DRIVER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		goto err_firmware_needs_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	/* Stage 3 - Driver Initialisation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	 * -------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	 * Register device to Linux, prepare private structures and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	 * reset the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	/* allocate driver resources */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	netdev = alloc_candev(sizeof(struct ucan_priv),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 			      ctl_msg_buffer->cmd_get_device_info.tx_fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	if (!netdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		dev_err(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 			"%s: cannot allocate candev\n", UCAN_DRIVER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	up = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	/* initialize data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	up->udev = udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	up->intf = intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	up->netdev = netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	up->intf_index = iface_desc->desc.bInterfaceNumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	up->in_ep_addr = in_ep_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	up->out_ep_addr = out_ep_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	up->in_ep_size = in_ep_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	up->ctl_msg_buffer = ctl_msg_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	up->context_array = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	up->available_tx_urbs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	up->can.state = CAN_STATE_STOPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	up->can.bittiming_const = &up->device_info.bittiming_const;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	up->can.do_set_bittiming = ucan_set_bittiming;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	up->can.do_set_mode = &ucan_set_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	spin_lock_init(&up->context_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	spin_lock_init(&up->echo_skb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	netdev->netdev_ops = &ucan_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	usb_set_intfdata(intf, up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	SET_NETDEV_DEV(netdev, &intf->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	/* parse device information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	 * the data retrieved in Stage 2 is still available in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	 * up->ctl_msg_buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	/* just print some device information - if available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 				     sizeof(union ucan_ctl_payload));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		/* copy string while ensuring zero terminiation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		strncpy(firmware_str, up->ctl_msg_buffer->raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 			sizeof(union ucan_ctl_payload));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		firmware_str[sizeof(union ucan_ctl_payload)] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		strcpy(firmware_str, "unknown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	/* device is compatible, reset it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		goto err_free_candev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	init_usb_anchor(&up->rx_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	init_usb_anchor(&up->tx_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	up->can.state = CAN_STATE_STOPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	/* register the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	ret = register_candev(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		goto err_free_candev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	/* initialisation complete, log device info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	netdev_info(up->netdev, "registered device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	netdev_info(up->netdev, "firmware string: %s\n", firmware_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	/* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) err_free_candev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	free_candev(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) err_firmware_needs_update:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	dev_err(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		"%s: probe failed; try to update the device firmware\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		UCAN_DRIVER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) /* disconnect the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) static void ucan_disconnect(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	struct ucan_priv *up = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	usb_set_intfdata(intf, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	if (up) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		unregister_netdev(up->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		free_candev(up->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) static struct usb_device_id ucan_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	/* Mule (soldered onto compute modules) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	{USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425a, 0)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	/* Seal (standalone USB stick) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	{USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425b, 0)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	{} /* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) MODULE_DEVICE_TABLE(usb, ucan_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) /* driver callbacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) static struct usb_driver ucan_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	.name = UCAN_DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	.probe = ucan_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	.disconnect = ucan_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	.id_table = ucan_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) module_usb_driver(ucan_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) MODULE_AUTHOR("Martin Elshuber <martin.elshuber@theobroma-systems.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) MODULE_AUTHOR("Jakob Unterwurzacher <jakob.unterwurzacher@theobroma-systems.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) MODULE_DESCRIPTION("Driver for Theobroma Systems UCAN devices");