summaryrefslogtreecommitdiff
path: root/drivers/net/can/usb/peak_usb/pcan_usb_core.h
blob: abab00930b9dbcc3f316b349e75a70ec8ced113f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
146
147
148
149
150
151
152
153
154
155
156
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * CAN driver for PEAK System USB adapters
 * Derived from the PCAN project file driver/src/pcan_usb_core.c
 *
 * Copyright (C) 2003-2010 PEAK System-Technik GmbH
 * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com>
 *
 * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
 */
#ifndef PCAN_USB_CORE_H
#define PCAN_USB_CORE_H

/* PEAK-System vendor id. */
#define PCAN_USB_VENDOR_ID		0x0c72

/* supported device ids. */
#define PCAN_USB_PRODUCT_ID		0x000c
#define PCAN_USBPRO_PRODUCT_ID		0x000d
#define PCAN_USBPROFD_PRODUCT_ID	0x0011
#define PCAN_USBFD_PRODUCT_ID		0x0012
#define PCAN_USBCHIP_PRODUCT_ID		0x0013
#define PCAN_USBX6_PRODUCT_ID		0x0014

#define PCAN_USB_DRIVER_NAME		"peak_usb"

/* number of urbs that are submitted for rx/tx per channel */
#define PCAN_USB_MAX_RX_URBS		4
#define PCAN_USB_MAX_TX_URBS		10

/* usb adapters maximum channels per usb interface */
#define PCAN_USB_MAX_CHANNEL		2

/* maximum length of the usb commands sent to/received from the devices */
#define PCAN_USB_MAX_CMD_LEN		32

struct peak_usb_device;

/* PEAK-System USB adapter descriptor */
struct peak_usb_adapter {
	char *name;
	u32 device_id;
	u32 ctrlmode_supported;
	struct can_clock clock;
	const struct can_bittiming_const * const bittiming_const;
	const struct can_bittiming_const * const data_bittiming_const;
	unsigned int ctrl_count;

	const struct ethtool_ops *ethtool_ops;

	int (*intf_probe)(struct usb_interface *intf);

	int (*dev_init)(struct peak_usb_device *dev);
	void (*dev_exit)(struct peak_usb_device *dev);
	void (*dev_free)(struct peak_usb_device *dev);
	int (*dev_open)(struct peak_usb_device *dev);
	int (*dev_close)(struct peak_usb_device *dev);
	int (*dev_set_bittiming)(struct peak_usb_device *dev,
					struct can_bittiming *bt);
	int (*dev_set_data_bittiming)(struct peak_usb_device *dev,
				      struct can_bittiming *bt);
	int (*dev_set_bus)(struct peak_usb_device *dev, u8 onoff);
	int (*dev_get_can_channel_id)(struct peak_usb_device *dev, u32 *can_ch_id);
	int (*dev_set_can_channel_id)(struct peak_usb_device *dev, u32 can_ch_id);
	int (*dev_decode_buf)(struct peak_usb_device *dev, struct urb *urb);
	int (*dev_encode_msg)(struct peak_usb_device *dev, struct sk_buff *skb,
					u8 *obuf, size_t *size);
	int (*dev_start)(struct peak_usb_device *dev);
	int (*dev_stop)(struct peak_usb_device *dev);
	int (*dev_restart_async)(struct peak_usb_device *dev, struct urb *urb,
					u8 *buf);
	int (*do_get_berr_counter)(const struct net_device *netdev,
				   struct can_berr_counter *bec);
	u8 ep_msg_in;
	u8 ep_msg_out[PCAN_USB_MAX_CHANNEL];
	u8 ts_used_bits;
	u8 us_per_ts_shift;
	u32 us_per_ts_scale;

	int rx_buffer_size;
	int tx_buffer_size;
	int sizeof_dev_private;
};

extern const struct peak_usb_adapter pcan_usb;
extern const struct peak_usb_adapter pcan_usb_pro;
extern const struct peak_usb_adapter pcan_usb_fd;
extern const struct peak_usb_adapter pcan_usb_chip;
extern const struct peak_usb_adapter pcan_usb_pro_fd;
extern const struct peak_usb_adapter pcan_usb_x6;

struct peak_time_ref {
	ktime_t tv_host_0, tv_host;
	u32 ts_dev_1, ts_dev_2;
	u64 ts_total;
	u32 tick_count;
	const struct peak_usb_adapter *adapter;
};

struct peak_tx_urb_context {
	struct peak_usb_device *dev;
	u32 echo_index;
	struct urb *urb;
};

#define PCAN_USB_STATE_CONNECTED	0x00000001
#define PCAN_USB_STATE_STARTED		0x00000002

/* PEAK-System USB device */
struct peak_usb_device {
	struct can_priv can;
	const struct peak_usb_adapter *adapter;
	unsigned int ctrl_idx;
	u32 state;

	struct usb_device *udev;
	struct net_device *netdev;

	atomic_t active_tx_urbs;
	struct usb_anchor tx_submitted;
	struct peak_tx_urb_context tx_contexts[PCAN_USB_MAX_TX_URBS];

	u8 *cmd_buf;
	struct usb_anchor rx_submitted;

	/* equivalent to the device ID in the Windows API */
	u32 can_channel_id;
	u8 device_rev;

	u8 ep_msg_in;
	u8 ep_msg_out;

	struct peak_usb_device *prev_siblings;
	struct peak_usb_device *next_siblings;
};

void pcan_dump_mem(const char *prompt, const void *p, int l);

/* common timestamp management */
void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
			    const struct peak_usb_adapter *adapter);
void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *tv);
int peak_usb_netif_rx_64(struct sk_buff *skb, u32 ts_low, u32 ts_high);
void peak_usb_async_complete(struct urb *urb);
void peak_usb_restart_complete(struct peak_usb_device *dev);
int pcan_get_ts_info(struct net_device *dev, struct kernel_ethtool_ts_info *info);

/* common 32-bit CAN channel ID ethtool management */
int peak_usb_get_eeprom_len(struct net_device *netdev);
int peak_usb_get_eeprom(struct net_device *netdev,
			struct ethtool_eeprom *eeprom, u8 *data);
int peak_usb_set_eeprom(struct net_device *netdev,
			struct ethtool_eeprom *eeprom, u8 *data);
#endif