1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * CAN driver for PEAK System USB adapters
4  * Derived from the PCAN project file driver/src/pcan_usb_core.c
5  *
6  * Copyright (C) 2003-2010 PEAK System-Technik GmbH
7  * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com>
8  *
9  * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
10  */
11 #ifndef PCAN_USB_CORE_H
12 #define PCAN_USB_CORE_H
13 
14 /* PEAK-System vendor id. */
15 #define PCAN_USB_VENDOR_ID		0x0c72
16 
17 /* supported device ids. */
18 #define PCAN_USB_PRODUCT_ID		0x000c
19 #define PCAN_USBPRO_PRODUCT_ID		0x000d
20 #define PCAN_USBPROFD_PRODUCT_ID	0x0011
21 #define PCAN_USBFD_PRODUCT_ID		0x0012
22 #define PCAN_USBCHIP_PRODUCT_ID		0x0013
23 #define PCAN_USBX6_PRODUCT_ID		0x0014
24 
25 #define PCAN_USB_DRIVER_NAME		"peak_usb"
26 
27 /* number of urbs that are submitted for rx/tx per channel */
28 #define PCAN_USB_MAX_RX_URBS		4
29 #define PCAN_USB_MAX_TX_URBS		10
30 
31 /* usb adapters maximum channels per usb interface */
32 #define PCAN_USB_MAX_CHANNEL		2
33 
34 /* maximum length of the usb commands sent to/received from the devices */
35 #define PCAN_USB_MAX_CMD_LEN		32
36 
37 struct peak_usb_device;
38 
39 /* PEAK-System USB adapter descriptor */
40 struct peak_usb_adapter {
41 	char *name;
42 	u32 device_id;
43 	u32 ctrlmode_supported;
44 	struct can_clock clock;
45 	const struct can_bittiming_const * const bittiming_const;
46 	const struct can_bittiming_const * const data_bittiming_const;
47 	unsigned int ctrl_count;
48 
49 	const struct ethtool_ops *ethtool_ops;
50 
51 	int (*intf_probe)(struct usb_interface *intf);
52 
53 	int (*dev_init)(struct peak_usb_device *dev);
54 	void (*dev_exit)(struct peak_usb_device *dev);
55 	void (*dev_free)(struct peak_usb_device *dev);
56 	int (*dev_open)(struct peak_usb_device *dev);
57 	int (*dev_close)(struct peak_usb_device *dev);
58 	int (*dev_set_bittiming)(struct peak_usb_device *dev,
59 					struct can_bittiming *bt);
60 	int (*dev_set_data_bittiming)(struct peak_usb_device *dev,
61 				      struct can_bittiming *bt);
62 	int (*dev_set_bus)(struct peak_usb_device *dev, u8 onoff);
63 	int (*dev_get_can_channel_id)(struct peak_usb_device *dev, u32 *can_ch_id);
64 	int (*dev_set_can_channel_id)(struct peak_usb_device *dev, u32 can_ch_id);
65 	int (*dev_decode_buf)(struct peak_usb_device *dev, struct urb *urb);
66 	int (*dev_encode_msg)(struct peak_usb_device *dev, struct sk_buff *skb,
67 					u8 *obuf, size_t *size);
68 	int (*dev_start)(struct peak_usb_device *dev);
69 	int (*dev_stop)(struct peak_usb_device *dev);
70 	int (*dev_restart_async)(struct peak_usb_device *dev, struct urb *urb,
71 					u8 *buf);
72 	int (*do_get_berr_counter)(const struct net_device *netdev,
73 				   struct can_berr_counter *bec);
74 	u8 ep_msg_in;
75 	u8 ep_msg_out[PCAN_USB_MAX_CHANNEL];
76 	u8 ts_used_bits;
77 	u8 us_per_ts_shift;
78 	u32 us_per_ts_scale;
79 
80 	int rx_buffer_size;
81 	int tx_buffer_size;
82 	int sizeof_dev_private;
83 };
84 
85 extern const struct peak_usb_adapter pcan_usb;
86 extern const struct peak_usb_adapter pcan_usb_pro;
87 extern const struct peak_usb_adapter pcan_usb_fd;
88 extern const struct peak_usb_adapter pcan_usb_chip;
89 extern const struct peak_usb_adapter pcan_usb_pro_fd;
90 extern const struct peak_usb_adapter pcan_usb_x6;
91 
92 struct peak_time_ref {
93 	ktime_t tv_host_0, tv_host;
94 	u32 ts_dev_1, ts_dev_2;
95 	u64 ts_total;
96 	u32 tick_count;
97 	const struct peak_usb_adapter *adapter;
98 };
99 
100 struct peak_tx_urb_context {
101 	struct peak_usb_device *dev;
102 	u32 echo_index;
103 	struct urb *urb;
104 };
105 
106 #define PCAN_USB_STATE_CONNECTED	0x00000001
107 #define PCAN_USB_STATE_STARTED		0x00000002
108 
109 /* PEAK-System USB device */
110 struct peak_usb_device {
111 	struct can_priv can;
112 	const struct peak_usb_adapter *adapter;
113 	unsigned int ctrl_idx;
114 	u32 state;
115 
116 	struct usb_device *udev;
117 	struct net_device *netdev;
118 
119 	atomic_t active_tx_urbs;
120 	struct usb_anchor tx_submitted;
121 	struct peak_tx_urb_context tx_contexts[PCAN_USB_MAX_TX_URBS];
122 
123 	u8 *cmd_buf;
124 	struct usb_anchor rx_submitted;
125 
126 	/* equivalent to the device ID in the Windows API */
127 	u32 can_channel_id;
128 	u8 device_rev;
129 
130 	u8 ep_msg_in;
131 	u8 ep_msg_out;
132 
133 	struct peak_usb_device *prev_siblings;
134 	struct peak_usb_device *next_siblings;
135 };
136 
137 void pcan_dump_mem(const char *prompt, const void *p, int l);
138 
139 /* common timestamp management */
140 void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
141 			    const struct peak_usb_adapter *adapter);
142 void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
143 void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
144 void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *tv);
145 int peak_usb_netif_rx(struct sk_buff *skb,
146 		      struct peak_time_ref *time_ref, u32 ts_low);
147 int peak_usb_netif_rx_64(struct sk_buff *skb, u32 ts_low, u32 ts_high);
148 void peak_usb_async_complete(struct urb *urb);
149 void peak_usb_restart_complete(struct peak_usb_device *dev);
150 int pcan_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info);
151 
152 /* common 32-bit CAN channel ID ethtool management */
153 int peak_usb_get_eeprom_len(struct net_device *netdev);
154 int peak_usb_get_eeprom(struct net_device *netdev,
155 			struct ethtool_eeprom *eeprom, u8 *data);
156 int peak_usb_set_eeprom(struct net_device *netdev,
157 			struct ethtool_eeprom *eeprom, u8 *data);
158 #endif
159