xref: /linux/drivers/net/can/usb/kvaser_usb/kvaser_usb.h (revision 9410645520e9b820069761f3450ef6661418e279)
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/ktime.h>
26 #include <linux/math64.h>
27 #include <linux/spinlock.h>
28 #include <linux/types.h>
29 #include <linux/usb.h>
30 
31 #include <linux/can.h>
32 #include <linux/can/dev.h>
33 
34 #define KVASER_USB_MAX_RX_URBS			4
35 #define KVASER_USB_MAX_TX_URBS			128
36 #define KVASER_USB_TIMEOUT			1000 /* msecs */
37 #define KVASER_USB_RX_BUFFER_SIZE		3072
38 #define KVASER_USB_MAX_NET_DEVICES		5
39 
40 /* Kvaser USB device quirks */
41 #define KVASER_USB_QUIRK_HAS_SILENT_MODE	BIT(0)
42 #define KVASER_USB_QUIRK_HAS_TXRX_ERRORS	BIT(1)
43 #define KVASER_USB_QUIRK_IGNORE_CLK_FREQ	BIT(2)
44 
45 /* Device capabilities */
46 #define KVASER_USB_CAP_BERR_CAP			0x01
47 #define KVASER_USB_CAP_EXT_CAP			0x02
48 #define KVASER_USB_HYDRA_CAP_EXT_CMD		0x04
49 
50 struct kvaser_usb_dev_cfg;
51 
52 enum kvaser_usb_leaf_family {
53 	KVASER_LEAF,
54 	KVASER_USBCAN,
55 };
56 
57 #define KVASER_USB_HYDRA_MAX_CMD_LEN		128
58 struct kvaser_usb_dev_card_data_hydra {
59 	u8 channel_to_he[KVASER_USB_MAX_NET_DEVICES];
60 	u8 sysdbg_he;
61 	spinlock_t transid_lock; /* lock for transid */
62 	u16 transid;
63 	/* lock for usb_rx_leftover and usb_rx_leftover_len */
64 	spinlock_t usb_rx_leftover_lock;
65 	u8 usb_rx_leftover[KVASER_USB_HYDRA_MAX_CMD_LEN];
66 	u8 usb_rx_leftover_len;
67 };
68 struct kvaser_usb_dev_card_data {
69 	u32 ctrlmode_supported;
70 	u32 capabilities;
71 	struct kvaser_usb_dev_card_data_hydra hydra;
72 	u32 usbcan_timestamp_msb;
73 };
74 
75 /* Context for an outstanding, not yet ACKed, transmission */
76 struct kvaser_usb_tx_urb_context {
77 	struct kvaser_usb_net_priv *priv;
78 	u32 echo_index;
79 };
80 
81 struct kvaser_usb_busparams {
82 	__le32 bitrate;
83 	u8 tseg1;
84 	u8 tseg2;
85 	u8 sjw;
86 	u8 nsamples;
87 } __packed;
88 
89 struct kvaser_usb {
90 	struct usb_device *udev;
91 	struct usb_interface *intf;
92 	struct kvaser_usb_net_priv *nets[KVASER_USB_MAX_NET_DEVICES];
93 	const struct kvaser_usb_driver_info *driver_info;
94 	const struct kvaser_usb_dev_cfg *cfg;
95 
96 	struct usb_endpoint_descriptor *bulk_in, *bulk_out;
97 	struct usb_anchor rx_submitted;
98 
99 	/* @max_tx_urbs: Firmware-reported maximum number of outstanding,
100 	 * not yet ACKed, transmissions on this device. This value is
101 	 * also used as a sentinel for marking free tx contexts.
102 	 */
103 	u32 fw_version;
104 	unsigned int nchannels;
105 	unsigned int max_tx_urbs;
106 	struct kvaser_usb_dev_card_data card_data;
107 
108 	bool rxinitdone;
109 	void *rxbuf[KVASER_USB_MAX_RX_URBS];
110 	dma_addr_t rxbuf_dma[KVASER_USB_MAX_RX_URBS];
111 };
112 
113 struct kvaser_usb_net_priv {
114 	struct can_priv can;
115 	struct can_berr_counter bec;
116 
117 	/* subdriver-specific data */
118 	void *sub_priv;
119 
120 	struct kvaser_usb *dev;
121 	struct net_device *netdev;
122 	int channel;
123 
124 	struct completion start_comp, stop_comp, flush_comp,
125 			  get_busparams_comp;
126 	struct usb_anchor tx_submitted;
127 
128 	struct kvaser_usb_busparams busparams_nominal, busparams_data;
129 
130 	spinlock_t tx_contexts_lock; /* lock for active_tx_contexts */
131 	int active_tx_contexts;
132 	struct kvaser_usb_tx_urb_context tx_contexts[];
133 };
134 
135 /**
136  * struct kvaser_usb_dev_ops - Device specific functions
137  * @dev_set_mode:		used for can.do_set_mode
138  * @dev_set_bittiming:		used for can.do_set_bittiming
139  * @dev_get_busparams:		readback arbitration busparams
140  * @dev_set_data_bittiming:	used for can.do_set_data_bittiming
141  * @dev_get_data_busparams:	readback data busparams
142  * @dev_get_berr_counter:	used for can.do_get_berr_counter
143  *
144  * @dev_setup_endpoints:	setup USB in and out endpoints
145  * @dev_init_card:		initialize card
146  * @dev_init_channel:		initialize channel
147  * @dev_remove_channel:		uninitialize channel
148  * @dev_get_software_info:	get software info
149  * @dev_get_software_details:	get software details
150  * @dev_get_card_info:		get card info
151  * @dev_get_capabilities:	discover device capabilities
152  *
153  * @dev_set_opt_mode:		set ctrlmod
154  * @dev_start_chip:		start the CAN controller
155  * @dev_stop_chip:		stop the CAN controller
156  * @dev_reset_chip:		reset the CAN controller
157  * @dev_flush_queue:		flush outstanding CAN messages
158  * @dev_read_bulk_callback:	handle incoming commands
159  * @dev_frame_to_cmd:		translate struct can_frame into device command
160  */
161 struct kvaser_usb_dev_ops {
162 	int (*dev_set_mode)(struct net_device *netdev, enum can_mode mode);
163 	int (*dev_set_bittiming)(const struct net_device *netdev,
164 				 const struct kvaser_usb_busparams *busparams);
165 	int (*dev_get_busparams)(struct kvaser_usb_net_priv *priv);
166 	int (*dev_set_data_bittiming)(const struct net_device *netdev,
167 				      const struct kvaser_usb_busparams *busparams);
168 	int (*dev_get_data_busparams)(struct kvaser_usb_net_priv *priv);
169 	int (*dev_get_berr_counter)(const struct net_device *netdev,
170 				    struct can_berr_counter *bec);
171 	int (*dev_setup_endpoints)(struct kvaser_usb *dev);
172 	int (*dev_init_card)(struct kvaser_usb *dev);
173 	int (*dev_init_channel)(struct kvaser_usb_net_priv *priv);
174 	void (*dev_remove_channel)(struct kvaser_usb_net_priv *priv);
175 	int (*dev_get_software_info)(struct kvaser_usb *dev);
176 	int (*dev_get_software_details)(struct kvaser_usb *dev);
177 	int (*dev_get_card_info)(struct kvaser_usb *dev);
178 	int (*dev_get_capabilities)(struct kvaser_usb *dev);
179 	int (*dev_set_opt_mode)(const struct kvaser_usb_net_priv *priv);
180 	int (*dev_start_chip)(struct kvaser_usb_net_priv *priv);
181 	int (*dev_stop_chip)(struct kvaser_usb_net_priv *priv);
182 	int (*dev_reset_chip)(struct kvaser_usb *dev, int channel);
183 	int (*dev_flush_queue)(struct kvaser_usb_net_priv *priv);
184 	void (*dev_read_bulk_callback)(struct kvaser_usb *dev, void *buf,
185 				       int len);
186 	void *(*dev_frame_to_cmd)(const struct kvaser_usb_net_priv *priv,
187 				  const struct sk_buff *skb, int *cmd_len,
188 				  u16 transid);
189 };
190 
191 struct kvaser_usb_driver_info {
192 	u32 quirks;
193 	enum kvaser_usb_leaf_family family;
194 	const struct kvaser_usb_dev_ops *ops;
195 };
196 
197 struct kvaser_usb_dev_cfg {
198 	const struct can_clock clock;
199 	const unsigned int timestamp_freq;
200 	const struct can_bittiming_const * const bittiming_const;
201 	const struct can_bittiming_const * const data_bittiming_const;
202 };
203 
204 extern const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops;
205 extern const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops;
206 
207 void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv);
208 
209 int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len,
210 			int *actual_len);
211 
212 int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len);
213 
214 int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd,
215 			      int len);
216 
217 int kvaser_usb_can_rx_over_error(struct net_device *netdev);
218 
219 extern const struct can_bittiming_const kvaser_usb_flexc_bittiming_const;
220 
kvaser_usb_ticks_to_ktime(const struct kvaser_usb_dev_cfg * cfg,u64 ticks)221 static inline ktime_t kvaser_usb_ticks_to_ktime(const struct kvaser_usb_dev_cfg *cfg,
222 						u64 ticks)
223 {
224 	return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
225 }
226 
kvaser_usb_timestamp48_to_ktime(const struct kvaser_usb_dev_cfg * cfg,const __le16 * timestamp)227 static inline ktime_t kvaser_usb_timestamp48_to_ktime(const struct kvaser_usb_dev_cfg *cfg,
228 						      const __le16 *timestamp)
229 {
230 	u64 ticks = le16_to_cpu(timestamp[0]) |
231 		    (u64)(le16_to_cpu(timestamp[1])) << 16 |
232 		    (u64)(le16_to_cpu(timestamp[2])) << 32;
233 
234 	return kvaser_usb_ticks_to_ktime(cfg, ticks);
235 }
236 
kvaser_usb_timestamp64_to_ktime(const struct kvaser_usb_dev_cfg * cfg,__le64 timestamp)237 static inline ktime_t kvaser_usb_timestamp64_to_ktime(const struct kvaser_usb_dev_cfg *cfg,
238 						      __le64 timestamp)
239 {
240 	return kvaser_usb_ticks_to_ktime(cfg, le64_to_cpu(timestamp));
241 }
242 
243 #endif /* KVASER_USB_H */
244