1
2
3
4
5
6
7
8
9
10
11
12
13
14 #ifndef KVASER_USB_H
15 #define KVASER_USB_H
16
17
18
19
20
21
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
35 #define KVASER_USB_RX_BUFFER_SIZE 3072
36 #define KVASER_USB_MAX_NET_DEVICES 5
37
38
39 #define KVASER_USB_HAS_SILENT_MODE BIT(0)
40 #define KVASER_USB_HAS_TXRX_ERRORS BIT(1)
41
42
43 #define KVASER_USB_CAP_BERR_CAP 0x01
44 #define KVASER_USB_CAP_EXT_CAP 0x02
45 #define KVASER_USB_HYDRA_CAP_EXT_CMD 0x04
46
47 struct kvaser_usb_dev_cfg;
48
49 enum kvaser_usb_leaf_family {
50 KVASER_LEAF,
51 KVASER_USBCAN,
52 };
53
54 #define KVASER_USB_HYDRA_MAX_CMD_LEN 128
55 struct kvaser_usb_dev_card_data_hydra {
56 u8 channel_to_he[KVASER_USB_MAX_NET_DEVICES];
57 u8 sysdbg_he;
58 spinlock_t transid_lock;
59 u16 transid;
60
61 spinlock_t usb_rx_leftover_lock;
62 u8 usb_rx_leftover[KVASER_USB_HYDRA_MAX_CMD_LEN];
63 u8 usb_rx_leftover_len;
64 };
65 struct kvaser_usb_dev_card_data {
66 u32 ctrlmode_supported;
67 u32 capabilities;
68 union {
69 struct {
70 enum kvaser_usb_leaf_family family;
71 } leaf;
72 struct kvaser_usb_dev_card_data_hydra hydra;
73 };
74 };
75
76
77 struct kvaser_usb_tx_urb_context {
78 struct kvaser_usb_net_priv *priv;
79 u32 echo_index;
80 int dlc;
81 };
82
83 struct kvaser_usb {
84 struct usb_device *udev;
85 struct usb_interface *intf;
86 struct kvaser_usb_net_priv *nets[KVASER_USB_MAX_NET_DEVICES];
87 const struct kvaser_usb_dev_ops *ops;
88 const struct kvaser_usb_dev_cfg *cfg;
89
90 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
91 struct usb_anchor rx_submitted;
92
93
94
95
96
97 u32 fw_version;
98 unsigned int nchannels;
99 unsigned int max_tx_urbs;
100 struct kvaser_usb_dev_card_data card_data;
101
102 bool rxinitdone;
103 void *rxbuf[KVASER_USB_MAX_RX_URBS];
104 dma_addr_t rxbuf_dma[KVASER_USB_MAX_RX_URBS];
105 };
106
107 struct kvaser_usb_net_priv {
108 struct can_priv can;
109 struct can_berr_counter bec;
110
111 struct kvaser_usb *dev;
112 struct net_device *netdev;
113 int channel;
114
115 struct completion start_comp, stop_comp, flush_comp;
116 struct usb_anchor tx_submitted;
117
118 spinlock_t tx_contexts_lock;
119 int active_tx_contexts;
120 struct kvaser_usb_tx_urb_context tx_contexts[];
121 };
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 struct kvaser_usb_dev_ops {
146 int (*dev_set_mode)(struct net_device *netdev, enum can_mode mode);
147 int (*dev_set_bittiming)(struct net_device *netdev);
148 int (*dev_set_data_bittiming)(struct net_device *netdev);
149 int (*dev_get_berr_counter)(const struct net_device *netdev,
150 struct can_berr_counter *bec);
151 int (*dev_setup_endpoints)(struct kvaser_usb *dev);
152 int (*dev_init_card)(struct kvaser_usb *dev);
153 int (*dev_get_software_info)(struct kvaser_usb *dev);
154 int (*dev_get_software_details)(struct kvaser_usb *dev);
155 int (*dev_get_card_info)(struct kvaser_usb *dev);
156 int (*dev_get_capabilities)(struct kvaser_usb *dev);
157 int (*dev_set_opt_mode)(const struct kvaser_usb_net_priv *priv);
158 int (*dev_start_chip)(struct kvaser_usb_net_priv *priv);
159 int (*dev_stop_chip)(struct kvaser_usb_net_priv *priv);
160 int (*dev_reset_chip)(struct kvaser_usb *dev, int channel);
161 int (*dev_flush_queue)(struct kvaser_usb_net_priv *priv);
162 void (*dev_read_bulk_callback)(struct kvaser_usb *dev, void *buf,
163 int len);
164 void *(*dev_frame_to_cmd)(const struct kvaser_usb_net_priv *priv,
165 const struct sk_buff *skb, int *frame_len,
166 int *cmd_len, u16 transid);
167 };
168
169 struct kvaser_usb_dev_cfg {
170 const struct can_clock clock;
171 const unsigned int timestamp_freq;
172 const struct can_bittiming_const * const bittiming_const;
173 const struct can_bittiming_const * const data_bittiming_const;
174 };
175
176 extern const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops;
177 extern const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops;
178
179 int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len,
180 int *actual_len);
181
182 int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len);
183
184 int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd,
185 int len);
186
187 int kvaser_usb_can_rx_over_error(struct net_device *netdev);
188 #endif