1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Parts of this driver are based on the following:
3  *  - Kvaser linux leaf driver (version 4.78)
4  *  - CAN driver for esd CAN-USB/2
5  *  - Kvaser linux usbcanII driver (version 5.3)
6  *  - Kvaser linux mhydra driver (version 5.24)
7  *
8  * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved.
9  * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
10  * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
11  * Copyright (C) 2015 Valeo S.A.
12  */
13 
14 #ifndef KVASER_USB_H
15 #define KVASER_USB_H
16 
17 /* Kvaser USB CAN dongles are divided into three major platforms:
18  * - Hydra: Running firmware labeled as 'mhydra'
19  * - Leaf: Based on Renesas M32C or Freescale i.MX28, running firmware labeled
20  *         as 'filo'
21  * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios'
22  */
23 
24 #include <linux/completion.h>
25 #include <linux/spinlock.h>
26 #include <linux/types.h>
27 #include <linux/usb.h>
28 
29 #include <linux/can.h>
30 #include <linux/can/dev.h>
31 
32 #define KVASER_USB_MAX_RX_URBS			4
33 #define KVASER_USB_MAX_TX_URBS			128
34 #define KVASER_USB_TIMEOUT			1000 /* msecs */
35 #define KVASER_USB_RX_BUFFER_SIZE		3072
36 #define KVASER_USB_MAX_NET_DEVICES		5
37 
38 /* Kvaser USB device quirks */
39 #define KVASER_USB_QUIRK_HAS_SILENT_MODE	BIT(0)
40 #define KVASER_USB_QUIRK_HAS_TXRX_ERRORS	BIT(1)
41 #define KVASER_USB_QUIRK_IGNORE_CLK_FREQ	BIT(2)
42 #define KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP	BIT(3)
43 
44 /* Device capabilities */
45 #define KVASER_USB_CAP_BERR_CAP			0x01
46 #define KVASER_USB_CAP_EXT_CAP			0x02
47 #define KVASER_USB_HYDRA_CAP_EXT_CMD		0x04
48 
49 struct kvaser_usb_dev_cfg;
50 
51 enum kvaser_usb_leaf_family {
52 	KVASER_LEAF,
53 	KVASER_USBCAN,
54 };
55 
56 #define KVASER_USB_HYDRA_MAX_CMD_LEN		128
57 struct kvaser_usb_dev_card_data_hydra {
58 	u8 channel_to_he[KVASER_USB_MAX_NET_DEVICES];
59 	u8 sysdbg_he;
60 	spinlock_t transid_lock; /* lock for transid */
61 	u16 transid;
62 	/* lock for usb_rx_leftover and usb_rx_leftover_len */
63 	spinlock_t usb_rx_leftover_lock;
64 	u8 usb_rx_leftover[KVASER_USB_HYDRA_MAX_CMD_LEN];
65 	u8 usb_rx_leftover_len;
66 };
67 struct kvaser_usb_dev_card_data {
68 	u32 ctrlmode_supported;
69 	u32 capabilities;
70 	struct kvaser_usb_dev_card_data_hydra hydra;
71 };
72 
73 /* Context for an outstanding, not yet ACKed, transmission */
74 struct kvaser_usb_tx_urb_context {
75 	struct kvaser_usb_net_priv *priv;
76 	u32 echo_index;
77 };
78 
79 struct kvaser_usb_busparams {
80 	__le32 bitrate;
81 	u8 tseg1;
82 	u8 tseg2;
83 	u8 sjw;
84 	u8 nsamples;
85 } __packed;
86 
87 struct kvaser_usb {
88 	struct usb_device *udev;
89 	struct usb_interface *intf;
90 	struct kvaser_usb_net_priv *nets[KVASER_USB_MAX_NET_DEVICES];
91 	const struct kvaser_usb_driver_info *driver_info;
92 	const struct kvaser_usb_dev_cfg *cfg;
93 
94 	struct usb_endpoint_descriptor *bulk_in, *bulk_out;
95 	struct usb_anchor rx_submitted;
96 
97 	/* @max_tx_urbs: Firmware-reported maximum number of outstanding,
98 	 * not yet ACKed, transmissions on this device. This value is
99 	 * also used as a sentinel for marking free tx contexts.
100 	 */
101 	u32 fw_version;
102 	unsigned int nchannels;
103 	unsigned int max_tx_urbs;
104 	struct kvaser_usb_dev_card_data card_data;
105 
106 	bool rxinitdone;
107 	void *rxbuf[KVASER_USB_MAX_RX_URBS];
108 	dma_addr_t rxbuf_dma[KVASER_USB_MAX_RX_URBS];
109 };
110 
111 struct kvaser_usb_net_priv {
112 	struct can_priv can;
113 	struct can_berr_counter bec;
114 
115 	/* subdriver-specific data */
116 	void *sub_priv;
117 
118 	struct kvaser_usb *dev;
119 	struct net_device *netdev;
120 	int channel;
121 
122 	struct completion start_comp, stop_comp, flush_comp,
123 			  get_busparams_comp;
124 	struct usb_anchor tx_submitted;
125 
126 	struct kvaser_usb_busparams busparams_nominal, busparams_data;
127 
128 	spinlock_t tx_contexts_lock; /* lock for active_tx_contexts */
129 	int active_tx_contexts;
130 	struct kvaser_usb_tx_urb_context tx_contexts[];
131 };
132 
133 /**
134  * struct kvaser_usb_dev_ops - Device specific functions
135  * @dev_set_mode:		used for can.do_set_mode
136  * @dev_set_bittiming:		used for can.do_set_bittiming
137  * @dev_get_busparams:		readback arbitration busparams
138  * @dev_set_data_bittiming:	used for can.do_set_data_bittiming
139  * @dev_get_data_busparams:	readback data busparams
140  * @dev_get_berr_counter:	used for can.do_get_berr_counter
141  *
142  * @dev_setup_endpoints:	setup USB in and out endpoints
143  * @dev_init_card:		initialize card
144  * @dev_init_channel:		initialize channel
145  * @dev_remove_channel:		uninitialize channel
146  * @dev_get_software_info:	get software info
147  * @dev_get_software_details:	get software details
148  * @dev_get_card_info:		get card info
149  * @dev_get_capabilities:	discover device capabilities
150  *
151  * @dev_set_opt_mode:		set ctrlmod
152  * @dev_start_chip:		start the CAN controller
153  * @dev_stop_chip:		stop the CAN controller
154  * @dev_reset_chip:		reset the CAN controller
155  * @dev_flush_queue:		flush outstanding CAN messages
156  * @dev_read_bulk_callback:	handle incoming commands
157  * @dev_frame_to_cmd:		translate struct can_frame into device command
158  */
159 struct kvaser_usb_dev_ops {
160 	int (*dev_set_mode)(struct net_device *netdev, enum can_mode mode);
161 	int (*dev_set_bittiming)(const struct net_device *netdev,
162 				 const struct kvaser_usb_busparams *busparams);
163 	int (*dev_get_busparams)(struct kvaser_usb_net_priv *priv);
164 	int (*dev_set_data_bittiming)(const struct net_device *netdev,
165 				      const struct kvaser_usb_busparams *busparams);
166 	int (*dev_get_data_busparams)(struct kvaser_usb_net_priv *priv);
167 	int (*dev_get_berr_counter)(const struct net_device *netdev,
168 				    struct can_berr_counter *bec);
169 	int (*dev_setup_endpoints)(struct kvaser_usb *dev);
170 	int (*dev_init_card)(struct kvaser_usb *dev);
171 	int (*dev_init_channel)(struct kvaser_usb_net_priv *priv);
172 	void (*dev_remove_channel)(struct kvaser_usb_net_priv *priv);
173 	int (*dev_get_software_info)(struct kvaser_usb *dev);
174 	int (*dev_get_software_details)(struct kvaser_usb *dev);
175 	int (*dev_get_card_info)(struct kvaser_usb *dev);
176 	int (*dev_get_capabilities)(struct kvaser_usb *dev);
177 	int (*dev_set_opt_mode)(const struct kvaser_usb_net_priv *priv);
178 	int (*dev_start_chip)(struct kvaser_usb_net_priv *priv);
179 	int (*dev_stop_chip)(struct kvaser_usb_net_priv *priv);
180 	int (*dev_reset_chip)(struct kvaser_usb *dev, int channel);
181 	int (*dev_flush_queue)(struct kvaser_usb_net_priv *priv);
182 	void (*dev_read_bulk_callback)(struct kvaser_usb *dev, void *buf,
183 				       int len);
184 	void *(*dev_frame_to_cmd)(const struct kvaser_usb_net_priv *priv,
185 				  const struct sk_buff *skb, int *cmd_len,
186 				  u16 transid);
187 };
188 
189 struct kvaser_usb_driver_info {
190 	u32 quirks;
191 	enum kvaser_usb_leaf_family family;
192 	const struct kvaser_usb_dev_ops *ops;
193 };
194 
195 struct kvaser_usb_dev_cfg {
196 	const struct can_clock clock;
197 	const unsigned int timestamp_freq;
198 	const struct can_bittiming_const * const bittiming_const;
199 	const struct can_bittiming_const * const data_bittiming_const;
200 };
201 
202 extern const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops;
203 extern const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops;
204 
205 void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv);
206 
207 int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len,
208 			int *actual_len);
209 
210 int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len);
211 
212 int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd,
213 			      int len);
214 
215 int kvaser_usb_can_rx_over_error(struct net_device *netdev);
216 
217 extern const struct can_bittiming_const kvaser_usb_flexc_bittiming_const;
218 
219 #endif /* KVASER_USB_H */
220