-
Notifications
You must be signed in to change notification settings - Fork 59
/
i2c-ch341-usb.c
330 lines (261 loc) · 8.57 KB
/
i2c-ch341-usb.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Driver for the CH341 USB-I2C adapter
*
* Copyright (c) 2016 Tse Lun Bien
*
* Derived from:
* i2c-ch341.c
* Copyright (c) 2014 Marco Gittler
*
* i2c-tiny-usb.c
* Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, version 2.
*/
#include <linux/errno.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/usb.h>
#define DRV_VERSION "1.1"
#define CH341_I2C_LOW_SPEED 0 // low speed - 20kHz
#define CH341_I2C_STANDARD_SPEED 1 // standard speed - 100kHz
#define CH341_I2C_FAST_SPEED 2 // fast speed - 400kHz
#define CH341_I2C_HIGH_SPEED 3 // high speed - 750kHz
#define CH341_CMD_I2C_STREAM 0xAA
#define CH341_CMD_I2C_STM_STA 0x74
#define CH341_CMD_I2C_STM_STO 0x75
#define CH341_CMD_I2C_STM_OUT 0x80
#define CH341_CMD_I2C_STM_IN 0xC0
#define CH341_CMD_I2C_STM_SET 0x60
#define CH341_CMD_I2C_STM_END 0x00
/* Structure to hold all of our device specific stuff */
struct i2c_ch341_usb {
struct usb_device *usb_dev; /* the usb device for this device */
struct usb_interface *iface; /* the interface for this device */
struct i2c_adapter adapter; /* i2c related things */
int ep_in;
int ep_out;
u8 in_buf[32];
u8 out_buf[32];
};
static int ch341_xfer(struct i2c_ch341_usb *dev, int out_len, int in_len);
/* ----- begin of i2c layer ---------------------------------------------- */
static int ch341_i2c_check_dev(struct i2c_ch341_usb *dev, u8 addr)
{
int retval;
dev->out_buf[0] = CH341_CMD_I2C_STREAM;
dev->out_buf[1] = CH341_CMD_I2C_STM_STA;
dev->out_buf[2] =
CH341_CMD_I2C_STM_OUT; /* NOTE: must be zero length otherwise it
messes up the device */
dev->out_buf[3] = (addr << 1) | 0x1;
dev->out_buf[4] =
CH341_CMD_I2C_STM_IN; /* NOTE: zero length here as well */
dev->out_buf[5] = CH341_CMD_I2C_STM_STO;
dev->out_buf[6] = CH341_CMD_I2C_STM_END;
retval = ch341_xfer(dev, 6, 1);
if (retval < 0)
return retval;
if (dev->in_buf[0] & 0x80)
return -ETIMEDOUT;
return 0;
}
static int ch341_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
int num)
{
struct i2c_ch341_usb *dev = (struct i2c_ch341_usb *)adapter->algo_data;
int retval;
int i;
int l;
dev_dbg(&adapter->dev, "master xfer %d messages\n", num);
retval = ch341_i2c_check_dev(dev, msgs[0].addr);
if (retval < 0)
return retval;
if (num == 1) {
/* size larger than endpoint max transfer size */
if ((msgs[0].len + 5) > 32)
return -EIO;
if (msgs[0].flags & I2C_M_RD) {
dev->out_buf[0] = CH341_CMD_I2C_STREAM;
dev->out_buf[1] = CH341_CMD_I2C_STM_STA;
dev->out_buf[2] = CH341_CMD_I2C_STM_OUT | 0x1;
dev->out_buf[3] = (msgs[0].addr << 1) | 0x1;
if (msgs[0].len) {
for (i = 0, l = msgs[0].len; l > 1; l--, i++)
dev->out_buf[i + 4] =
CH341_CMD_I2C_STM_IN | 1;
dev->out_buf[msgs[0].len + 3] =
CH341_CMD_I2C_STM_IN;
}
dev->out_buf[msgs[0].len + 4] = CH341_CMD_I2C_STM_STO;
dev->out_buf[msgs[0].len + 5] = CH341_CMD_I2C_STM_END;
retval = ch341_xfer(dev, msgs[0].len + 5, msgs[0].len);
if (retval < 0)
return retval;
memcpy(msgs[0].buf, dev->in_buf, msgs[0].len);
} else {
dev->out_buf[0] = CH341_CMD_I2C_STREAM;
dev->out_buf[1] = CH341_CMD_I2C_STM_STA;
dev->out_buf[2] =
CH341_CMD_I2C_STM_OUT | (msgs[0].len + 1);
dev->out_buf[3] = msgs[0].addr << 1;
memcpy(&dev->out_buf[4], msgs[0].buf, msgs[0].len);
dev->out_buf[msgs[0].len + 4] = CH341_CMD_I2C_STM_STO;
dev->out_buf[msgs[0].len + 5] = CH341_CMD_I2C_STM_END;
retval = ch341_xfer(dev, msgs[0].len + 5, 0);
if (retval < 0)
return retval;
}
} else if (num == 2) {
if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
/* size larger than endpoint max transfer size */
if (((msgs[0].len + 3) > 32)
|| ((msgs[1].len + 5) > 32))
return -EIO;
/* write data phase */
dev->out_buf[0] = CH341_CMD_I2C_STREAM;
dev->out_buf[1] = CH341_CMD_I2C_STM_STA;
dev->out_buf[2] =
CH341_CMD_I2C_STM_OUT | (msgs[0].len + 1);
dev->out_buf[3] = msgs[0].addr << 1;
memcpy(&dev->out_buf[4], msgs[0].buf, msgs[0].len);
retval = ch341_xfer(dev, msgs[0].len + 4, 0);
if (retval < 0)
return retval;
/* read data phase */
dev->out_buf[0] = CH341_CMD_I2C_STREAM;
dev->out_buf[1] = CH341_CMD_I2C_STM_STA;
dev->out_buf[2] = CH341_CMD_I2C_STM_OUT | 0x1;
dev->out_buf[3] = (msgs[1].addr << 1) | 0x1;
if (msgs[1].len) {
for (i = 0, l = msgs[1].len; l > 1; l--, i++)
dev->out_buf[i + 4] =
CH341_CMD_I2C_STM_IN | 1;
dev->out_buf[msgs[1].len + 3] =
CH341_CMD_I2C_STM_IN;
}
dev->out_buf[msgs[1].len + 4] = CH341_CMD_I2C_STM_STO;
dev->out_buf[msgs[1].len + 5] = CH341_CMD_I2C_STM_END;
retval = ch341_xfer(dev, msgs[1].len + 5, msgs[1].len);
if (retval < 0)
return retval;
memcpy(msgs[1].buf, dev->in_buf, msgs[1].len);
} else {
return -EIO;
}
} else {
dev_err(&adapter->dev,
"This case(num > 2) has not been support now\n");
return -EIO;
}
return num;
}
static u32 ch341_i2c_func(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}
/* ----- end of i2c layer ------------------------------------------------ */
/* ----- begin of usb layer ---------------------------------------------- */
static const struct i2c_algorithm ch341_i2c_algorithm = {
.master_xfer = ch341_i2c_xfer,
.functionality = ch341_i2c_func,
};
static const struct usb_device_id i2c_ch341_usb_table[] = {
{USB_DEVICE(0x1a86, 0x5512)},
{}};
MODULE_DEVICE_TABLE(usb, i2c_ch341_usb_table);
static int ch341_xfer(struct i2c_ch341_usb *dev, int out_len, int in_len)
{
int retval;
int actual;
dev_dbg(&dev->adapter.dev, "bulk_out %d bytes, bulk_in %d bytes\n",
out_len, (in_len == 0) ? 0 : 32);
retval = usb_bulk_msg(dev->usb_dev,
usb_sndbulkpipe(dev->usb_dev, dev->ep_out),
dev->out_buf, out_len, &actual, 2000);
if (retval < 0)
return retval;
if (in_len == 0)
return actual;
memset(dev->in_buf, 0, sizeof(dev->in_buf));
retval = usb_bulk_msg(dev->usb_dev,
usb_rcvbulkpipe(dev->usb_dev, dev->ep_in),
dev->in_buf, 32, &actual, 2000);
if (retval < 0)
return retval;
return actual;
}
static void i2c_ch341_usb_free(struct i2c_ch341_usb *dev)
{
usb_put_dev(dev->usb_dev);
kfree(dev);
}
static int i2c_ch341_usb_probe(struct usb_interface *iface,
const struct usb_device_id *id)
{
struct i2c_ch341_usb *dev;
int retval = -ENOMEM;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (dev == NULL) {
dev_err(&iface->dev, "Out of memory\n");
goto error;
}
dev->usb_dev = usb_get_dev(interface_to_usbdev(iface));
dev->iface = iface;
dev->ep_out = iface->cur_altsetting->endpoint[1].desc.bEndpointAddress;
dev->ep_in = iface->cur_altsetting->endpoint[0].desc.bEndpointAddress;
/* save our data pointer in this interface device */
usb_set_intfdata(iface, dev);
/* setup i2c adapter description */
dev->adapter.owner = THIS_MODULE;
dev->adapter.class = I2C_CLASS_HWMON;
dev->adapter.algo = &ch341_i2c_algorithm;
dev->adapter.algo_data = dev;
snprintf(dev->adapter.name, sizeof(dev->adapter.name),
"i2c-ch341-usb at bus %03d device %03d",
dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
dev->adapter.dev.parent = &dev->iface->dev;
/* and finally attach to i2c layer */
i2c_add_adapter(&dev->adapter);
/* set ch341 i2c speed */
dev->out_buf[0] = CH341_CMD_I2C_STREAM;
dev->out_buf[1] = CH341_CMD_I2C_STM_SET | CH341_I2C_STANDARD_SPEED;
dev->out_buf[2] = CH341_CMD_I2C_STM_END;
retval = ch341_xfer(dev, 3, 0);
if (retval < 0) {
dev_err(&dev->adapter.dev, "failure setting speed\n");
retval = -EIO;
goto error;
}
dev_info(&dev->adapter.dev, "connected i2c-ch341-usb device\n");
return 0;
error:
if (dev)
i2c_ch341_usb_free(dev);
return retval;
}
static void i2c_ch341_usb_disconnect(struct usb_interface *iface)
{
struct i2c_ch341_usb *dev = usb_get_intfdata(iface);
i2c_del_adapter(&dev->adapter);
usb_set_intfdata(iface, NULL);
i2c_ch341_usb_free(dev);
dev_dbg(&iface->dev, "disconnected\n");
}
static struct usb_driver i2c_ch341_usb_driver = {
.name = "i2c-ch341-usb",
.probe = i2c_ch341_usb_probe,
.disconnect = i2c_ch341_usb_disconnect,
.id_table = i2c_ch341_usb_table,
};
module_usb_driver(i2c_ch341_usb_driver);
/* ----- end of usb layer ------------------------------------------------ */
MODULE_AUTHOR("Tse Lun Bien <allanbian@gmail.com>");
MODULE_DESCRIPTION("i2c-ch341-usb driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_VERSION);