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
|
/*
* ZyDAS ZD1301 driver (USB interface)
*
* Copyright (C) 2015 Antti Palosaari <crope@iki.fi>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "dvb_usb.h"
#include "zd1301_demod.h"
#include "mt2060.h"
#include <linux/i2c.h>
#include <linux/platform_device.h>
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
struct zd1301_dev {
#define BUF_LEN 8
u8 buf[BUF_LEN]; /* bulk USB control message */
struct zd1301_demod_platform_data demod_pdata;
struct mt2060_platform_data mt2060_pdata;
struct platform_device *platform_device_demod;
struct i2c_client *i2c_client_tuner;
};
static int zd1301_ctrl_msg(struct dvb_usb_device *d, const u8 *wbuf,
unsigned int wlen, u8 *rbuf, unsigned int rlen)
{
struct zd1301_dev *dev = d_to_priv(d);
struct usb_interface *intf = d->intf;
int ret, actual_length;
mutex_lock(&d->usb_mutex);
memcpy(&dev->buf, wbuf, wlen);
dev_dbg(&intf->dev, ">>> %*ph\n", wlen, dev->buf);
ret = usb_bulk_msg(d->udev, usb_sndbulkpipe(d->udev, 0x04), dev->buf,
wlen, &actual_length, 1000);
if (ret) {
dev_err(&intf->dev, "1st usb_bulk_msg() failed %d\n", ret);
goto err_mutex_unlock;
}
if (rlen) {
ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev, 0x83),
dev->buf, rlen, &actual_length, 1000);
if (ret) {
dev_err(&intf->dev,
"2nd usb_bulk_msg() failed %d\n", ret);
goto err_mutex_unlock;
}
dev_dbg(&intf->dev, "<<< %*ph\n", actual_length, dev->buf);
if (actual_length != rlen) {
/*
* Chip replies often with 3 byte len stub. On that case
* we have to query new reply.
*/
dev_dbg(&intf->dev, "repeating reply message\n");
ret = usb_bulk_msg(d->udev,
usb_rcvbulkpipe(d->udev, 0x83),
dev->buf, rlen, &actual_length,
1000);
if (ret) {
dev_err(&intf->dev,
"3rd usb_bulk_msg() failed %d\n", ret);
goto err_mutex_unlock;
}
dev_dbg(&intf->dev,
"<<< %*ph\n", actual_length, dev->buf);
}
memcpy(rbuf, dev->buf, rlen);
}
err_mutex_unlock:
mutex_unlock(&d->usb_mutex);
return ret;
}
static int zd1301_demod_wreg(void *reg_priv, u16 reg, u8 val)
{
struct dvb_usb_device *d = reg_priv;
struct usb_interface *intf = d->intf;
int ret;
u8 buf[7] = {0x07, 0x00, 0x03, 0x01,
(reg >> 0) & 0xff, (reg >> 8) & 0xff, val};
ret = zd1301_ctrl_msg(d, buf, 7, NULL, 0);
if (ret)
goto err;
return 0;
err:
dev_dbg(&intf->dev, "failed=%d\n", ret);
return ret;
}
static int zd1301_demod_rreg(void *reg_priv, u16 reg, u8 *val)
{
struct dvb_usb_device *d = reg_priv;
struct usb_interface *intf = d->intf;
int ret;
u8 buf[7] = {0x07, 0x00, 0x04, 0x01,
(reg >> 0) & 0xff, (reg >> 8) & 0xff, 0};
ret = zd1301_ctrl_msg(d, buf, 7, buf, 7);
if (ret)
goto err;
*val = buf[6];
return 0;
err:
dev_dbg(&intf->dev, "failed=%d\n", ret);
return ret;
}
static int zd1301_frontend_attach(struct dvb_usb_adapter *adap)
{
struct dvb_usb_device *d = adap_to_d(adap);
struct zd1301_dev *dev = adap_to_priv(adap);
struct usb_interface *intf = d->intf;
struct platform_device *pdev;
struct i2c_client *client;
struct i2c_board_info board_info;
struct i2c_adapter *adapter;
struct dvb_frontend *frontend;
int ret;
dev_dbg(&intf->dev, "\n");
/* Add platform demod */
dev->demod_pdata.reg_priv = d;
dev->demod_pdata.reg_read = zd1301_demod_rreg;
dev->demod_pdata.reg_write = zd1301_demod_wreg;
request_module("%s", "zd1301_demod");
pdev = platform_device_register_data(&intf->dev,
"zd1301_demod",
PLATFORM_DEVID_AUTO,
&dev->demod_pdata,
sizeof(dev->demod_pdata));
if (IS_ERR(pdev)) {
ret = PTR_ERR(pdev);
goto err;
}
if (!pdev->dev.driver) {
ret = -ENODEV;
goto err;
}
if (!try_module_get(pdev->dev.driver->owner)) {
ret = -ENODEV;
goto err_platform_device_unregister;
}
adapter = zd1301_demod_get_i2c_adapter(pdev);
frontend = zd1301_demod_get_dvb_frontend(pdev);
if (!adapter || !frontend) {
ret = -ENODEV;
goto err_module_put_demod;
}
/* Add I2C tuner */
dev->mt2060_pdata.i2c_write_max = 9;
dev->mt2060_pdata.dvb_frontend = frontend;
memset(&board_info, 0, sizeof(board_info));
strlcpy(board_info.type, "mt2060", I2C_NAME_SIZE);
board_info.addr = 0x60;
board_info.platform_data = &dev->mt2060_pdata;
request_module("%s", "mt2060");
client = i2c_new_device(adapter, &board_info);
if (!client || !client->dev.driver) {
ret = -ENODEV;
goto err_module_put_demod;
}
if (!try_module_get(client->dev.driver->owner)) {
ret = -ENODEV;
goto err_i2c_unregister_device;
}
dev->platform_device_demod = pdev;
dev->i2c_client_tuner = client;
adap->fe[0] = frontend;
return 0;
err_i2c_unregister_device:
i2c_unregister_device(client);
err_module_put_demod:
module_put(pdev->dev.driver->owner);
err_platform_device_unregister:
platform_device_unregister(pdev);
err:
dev_dbg(&intf->dev, "failed=%d\n", ret);
return ret;
}
static int zd1301_frontend_detach(struct dvb_usb_adapter *adap)
{
struct dvb_usb_device *d = adap_to_d(adap);
struct zd1301_dev *dev = d_to_priv(d);
struct usb_interface *intf = d->intf;
struct platform_device *pdev;
struct i2c_client *client;
dev_dbg(&intf->dev, "\n");
client = dev->i2c_client_tuner;
pdev = dev->platform_device_demod;
/* Remove I2C tuner */
if (client) {
module_put(client->dev.driver->owner);
i2c_unregister_device(client);
}
/* Remove platform demod */
if (pdev) {
module_put(pdev->dev.driver->owner);
platform_device_unregister(pdev);
}
return 0;
}
static int zd1301_streaming_ctrl(struct dvb_frontend *fe, int onoff)
{
struct dvb_usb_device *d = fe_to_d(fe);
struct usb_interface *intf = d->intf;
int ret;
u8 buf[3] = {0x03, 0x00, onoff ? 0x07 : 0x08};
dev_dbg(&intf->dev, "onoff=%d\n", onoff);
ret = zd1301_ctrl_msg(d, buf, 3, NULL, 0);
if (ret)
goto err;
return 0;
err:
dev_dbg(&intf->dev, "failed=%d\n", ret);
return ret;
}
static const struct dvb_usb_device_properties zd1301_props = {
.driver_name = KBUILD_MODNAME,
.owner = THIS_MODULE,
.adapter_nr = adapter_nr,
.size_of_priv = sizeof(struct zd1301_dev),
.frontend_attach = zd1301_frontend_attach,
.frontend_detach = zd1301_frontend_detach,
.streaming_ctrl = zd1301_streaming_ctrl,
.num_adapters = 1,
.adapter = {
{
.stream = DVB_USB_STREAM_BULK(0x81, 6, 21 * 188),
},
},
};
static const struct usb_device_id zd1301_id_table[] = {
{DVB_USB_DEVICE(USB_VID_ZYDAS, 0x13a1, &zd1301_props,
"ZyDAS ZD1301 reference design", NULL)},
{}
};
MODULE_DEVICE_TABLE(usb, zd1301_id_table);
/* Usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver zd1301_usb_driver = {
.name = KBUILD_MODNAME,
.id_table = zd1301_id_table,
.probe = dvb_usbv2_probe,
.disconnect = dvb_usbv2_disconnect,
.suspend = dvb_usbv2_suspend,
.resume = dvb_usbv2_resume,
.reset_resume = dvb_usbv2_reset_resume,
.no_dynamic_id = 1,
.soft_unbind = 1,
};
module_usb_driver(zd1301_usb_driver);
MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("ZyDAS ZD1301 driver");
MODULE_LICENSE("GPL");
|