1 /*
2  * CAN driver for PEAK System USB adapters
3  * Derived from the PCAN project file driver/src/pcan_usb_core.c
4  *
5  * Copyright (C) 2003-2010 PEAK System-Technik GmbH
6  * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com>
7  *
8  * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published
12  * by the Free Software Foundation; version 2 of the License.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  */
19 #ifndef PCAN_USB_CORE_H
20 #define PCAN_USB_CORE_H
21 
22 /* PEAK-System vendor id. */
23 #define PCAN_USB_VENDOR_ID		0x0c72
24 
25 /* supported device ids. */
26 #define PCAN_USB_PRODUCT_ID		0x000c
27 #define PCAN_USBPRO_PRODUCT_ID		0x000d
28 
29 #define PCAN_USB_DRIVER_NAME		"peak_usb"
30 
31 /* number of urbs that are submitted for rx/tx per channel */
32 #define PCAN_USB_MAX_RX_URBS		4
33 #define PCAN_USB_MAX_TX_URBS		10
34 
35 /* usb adapters maximum channels per usb interface */
36 #define PCAN_USB_MAX_CHANNEL		2
37 
38 /* maximum length of the usb commands sent to/received from  the devices */
39 #define PCAN_USB_MAX_CMD_LEN		32
40 
41 struct peak_usb_device;
42 
43 /* PEAK-System USB adapter descriptor */
44 struct peak_usb_adapter {
45 	char *name;
46 	u32 device_id;
47 	struct can_clock clock;
48 	struct can_bittiming_const bittiming_const;
49 	unsigned int ctrl_count;
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_bus)(struct peak_usb_device *dev, u8 onoff);
61 	int (*dev_get_device_id)(struct peak_usb_device *dev, u32 *device_id);
62 	int (*dev_decode_buf)(struct peak_usb_device *dev, struct urb *urb);
63 	int (*dev_encode_msg)(struct peak_usb_device *dev, struct sk_buff *skb,
64 					u8 *obuf, size_t *size);
65 	int (*dev_start)(struct peak_usb_device *dev);
66 	int (*dev_stop)(struct peak_usb_device *dev);
67 	int (*dev_restart_async)(struct peak_usb_device *dev, struct urb *urb,
68 					u8 *buf);
69 	u8 ep_msg_in;
70 	u8 ep_msg_out[PCAN_USB_MAX_CHANNEL];
71 	u8 ts_used_bits;
72 	u32 ts_period;
73 	u8 us_per_ts_shift;
74 	u32 us_per_ts_scale;
75 
76 	int rx_buffer_size;
77 	int tx_buffer_size;
78 	int sizeof_dev_private;
79 };
80 
81 extern struct peak_usb_adapter pcan_usb;
82 extern struct peak_usb_adapter pcan_usb_pro;
83 
84 struct peak_time_ref {
85 	struct timeval tv_host_0, tv_host;
86 	u32 ts_dev_1, ts_dev_2;
87 	u64 ts_total;
88 	u32 tick_count;
89 	struct peak_usb_adapter *adapter;
90 };
91 
92 struct peak_tx_urb_context {
93 	struct peak_usb_device *dev;
94 	u32 echo_index;
95 	u8 dlc;
96 	struct urb *urb;
97 };
98 
99 #define PCAN_USB_STATE_CONNECTED	0x00000001
100 #define PCAN_USB_STATE_STARTED		0x00000002
101 
102 /* PEAK-System USB device */
103 struct peak_usb_device {
104 	struct can_priv can;
105 	struct peak_usb_adapter *adapter;
106 	unsigned int ctrl_idx;
107 	int open_time;
108 	u32 state;
109 
110 	struct sk_buff *echo_skb[PCAN_USB_MAX_TX_URBS];
111 
112 	struct usb_device *udev;
113 	struct net_device *netdev;
114 
115 	atomic_t active_tx_urbs;
116 	struct usb_anchor tx_submitted;
117 	struct peak_tx_urb_context tx_contexts[PCAN_USB_MAX_TX_URBS];
118 
119 	u8 *cmd_buf;
120 	struct usb_anchor rx_submitted;
121 
122 	u32 device_number;
123 	u8 device_rev;
124 
125 	u8 ep_msg_in;
126 	u8 ep_msg_out;
127 
128 	u16 bus_load;
129 
130 	struct peak_usb_device *prev_siblings;
131 	struct peak_usb_device *next_siblings;
132 };
133 
134 void dump_mem(char *prompt, void *p, int l);
135 
136 /* common timestamp management */
137 void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
138 			    struct peak_usb_adapter *adapter);
139 void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
140 void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
141 void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts,
142 			struct timeval *tv);
143 
144 void peak_usb_async_complete(struct urb *urb);
145 void peak_usb_restart_complete(struct peak_usb_device *dev);
146 #endif
147