1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates.
4 * All rights reserved.
5 */
6
7 /* kernel includes */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/usb.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/input.h>
14 #include <linux/mutex.h>
15 #include <linux/i2c.h>
16 /* V4l includes */
17 #include <linux/videodev2.h>
18 #include <media/v4l2-common.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-event.h>
22 #include <linux/platform_data/media/si4713.h>
23
24 #include "si4713.h"
25
26 /* driver and module definitions */
27 MODULE_AUTHOR("Dinesh Ram <dinesh.ram@cern.ch>");
28 MODULE_DESCRIPTION("Si4713 FM Transmitter USB driver");
29 MODULE_LICENSE("GPL v2");
30
31 /* The Device announces itself as Cygnal Integrated Products, Inc. */
32 #define USB_SI4713_VENDOR 0x10c4
33 #define USB_SI4713_PRODUCT 0x8244
34
35 #define BUFFER_LENGTH 64
36 #define USB_TIMEOUT 1000
37 #define USB_RESP_TIMEOUT 50000
38
39 /* USB Device ID List */
40 static const struct usb_device_id usb_si4713_usb_device_table[] = {
41 {USB_DEVICE_AND_INTERFACE_INFO(USB_SI4713_VENDOR, USB_SI4713_PRODUCT,
42 USB_CLASS_HID, 0, 0) },
43 { } /* Terminating entry */
44 };
45
46 MODULE_DEVICE_TABLE(usb, usb_si4713_usb_device_table);
47
48 struct si4713_usb_device {
49 struct usb_device *usbdev;
50 struct usb_interface *intf;
51 struct video_device vdev;
52 struct v4l2_device v4l2_dev;
53 struct v4l2_subdev *v4l2_subdev;
54 struct mutex lock;
55 struct i2c_adapter i2c_adapter;
56
57 u8 *buffer;
58 };
59
to_si4713_dev(struct v4l2_device * v4l2_dev)60 static inline struct si4713_usb_device *to_si4713_dev(struct v4l2_device *v4l2_dev)
61 {
62 return container_of(v4l2_dev, struct si4713_usb_device, v4l2_dev);
63 }
64
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * v)65 static int vidioc_querycap(struct file *file, void *priv,
66 struct v4l2_capability *v)
67 {
68 struct si4713_usb_device *radio = video_drvdata(file);
69
70 strlcpy(v->driver, "radio-usb-si4713", sizeof(v->driver));
71 strlcpy(v->card, "Si4713 FM Transmitter", sizeof(v->card));
72 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
73 v->device_caps = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
74 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
75
76 return 0;
77 }
78
vidioc_g_modulator(struct file * file,void * priv,struct v4l2_modulator * vm)79 static int vidioc_g_modulator(struct file *file, void *priv,
80 struct v4l2_modulator *vm)
81 {
82 struct si4713_usb_device *radio = video_drvdata(file);
83
84 return v4l2_subdev_call(radio->v4l2_subdev, tuner, g_modulator, vm);
85 }
86
vidioc_s_modulator(struct file * file,void * priv,const struct v4l2_modulator * vm)87 static int vidioc_s_modulator(struct file *file, void *priv,
88 const struct v4l2_modulator *vm)
89 {
90 struct si4713_usb_device *radio = video_drvdata(file);
91
92 return v4l2_subdev_call(radio->v4l2_subdev, tuner, s_modulator, vm);
93 }
94
vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * vf)95 static int vidioc_s_frequency(struct file *file, void *priv,
96 const struct v4l2_frequency *vf)
97 {
98 struct si4713_usb_device *radio = video_drvdata(file);
99
100 return v4l2_subdev_call(radio->v4l2_subdev, tuner, s_frequency, vf);
101 }
102
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * vf)103 static int vidioc_g_frequency(struct file *file, void *priv,
104 struct v4l2_frequency *vf)
105 {
106 struct si4713_usb_device *radio = video_drvdata(file);
107
108 return v4l2_subdev_call(radio->v4l2_subdev, tuner, g_frequency, vf);
109 }
110
111 static const struct v4l2_ioctl_ops usb_si4713_ioctl_ops = {
112 .vidioc_querycap = vidioc_querycap,
113 .vidioc_g_modulator = vidioc_g_modulator,
114 .vidioc_s_modulator = vidioc_s_modulator,
115 .vidioc_g_frequency = vidioc_g_frequency,
116 .vidioc_s_frequency = vidioc_s_frequency,
117 .vidioc_log_status = v4l2_ctrl_log_status,
118 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
119 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
120 };
121
122 /* File system interface */
123 static const struct v4l2_file_operations usb_si4713_fops = {
124 .owner = THIS_MODULE,
125 .open = v4l2_fh_open,
126 .release = v4l2_fh_release,
127 .poll = v4l2_ctrl_poll,
128 .unlocked_ioctl = video_ioctl2,
129 };
130
usb_si4713_video_device_release(struct v4l2_device * v4l2_dev)131 static void usb_si4713_video_device_release(struct v4l2_device *v4l2_dev)
132 {
133 struct si4713_usb_device *radio = to_si4713_dev(v4l2_dev);
134 struct i2c_adapter *adapter = &radio->i2c_adapter;
135
136 i2c_del_adapter(adapter);
137 v4l2_device_unregister(&radio->v4l2_dev);
138 kfree(radio->buffer);
139 kfree(radio);
140 }
141
142 /*
143 * This command sequence emulates the behaviour of the Windows driver.
144 * The structure of these commands was determined by sniffing the
145 * usb traffic of the device during startup.
146 * Most likely, these commands make some queries to the device.
147 * Commands are sent to enquire parameters like the bus mode,
148 * component revision, boot mode, the device serial number etc.
149 *
150 * These commands are necessary to be sent in this order during startup.
151 * The device fails to powerup if these commands are not sent.
152 *
153 * The complete list of startup commands is given in the start_seq table below.
154 */
si4713_send_startup_command(struct si4713_usb_device * radio)155 static int si4713_send_startup_command(struct si4713_usb_device *radio)
156 {
157 unsigned long until_jiffies = jiffies + usecs_to_jiffies(USB_RESP_TIMEOUT) + 1;
158 u8 *buffer = radio->buffer;
159 int retval;
160
161 /* send the command */
162 retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
163 0x09, 0x21, 0x033f, 0, radio->buffer,
164 BUFFER_LENGTH, USB_TIMEOUT);
165 if (retval < 0)
166 return retval;
167
168 for (;;) {
169 /* receive the response */
170 retval = usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
171 0x01, 0xa1, 0x033f, 0, radio->buffer,
172 BUFFER_LENGTH, USB_TIMEOUT);
173 if (retval < 0)
174 return retval;
175 if (!radio->buffer[1]) {
176 /* USB traffic sniffing showed that some commands require
177 * additional checks. */
178 switch (buffer[1]) {
179 case 0x32:
180 if (radio->buffer[2] == 0)
181 return 0;
182 break;
183 case 0x14:
184 case 0x12:
185 if (radio->buffer[2] & SI4713_CTS)
186 return 0;
187 break;
188 case 0x06:
189 if ((radio->buffer[2] & SI4713_CTS) && radio->buffer[9] == 0x08)
190 return 0;
191 break;
192 default:
193 return 0;
194 }
195 }
196 if (time_is_before_jiffies(until_jiffies))
197 return -EIO;
198 msleep(3);
199 }
200
201 return retval;
202 }
203
204 struct si4713_start_seq_table {
205 int len;
206 u8 payload[8];
207 };
208
209 /*
210 * Some of the startup commands that could be recognized are :
211 * (0x03): Get serial number of the board (Response : CB000-00-00)
212 * (0x06, 0x03, 0x03, 0x08, 0x01, 0x0f) : Get Component revision
213 */
214 static const struct si4713_start_seq_table start_seq[] = {
215
216 { 1, { 0x03 } },
217 { 2, { 0x32, 0x7f } },
218 { 6, { 0x06, 0x03, 0x03, 0x08, 0x01, 0x0f } },
219 { 2, { 0x14, 0x02 } },
220 { 2, { 0x09, 0x90 } },
221 { 3, { 0x08, 0x90, 0xfa } },
222 { 2, { 0x36, 0x01 } },
223 { 2, { 0x05, 0x03 } },
224 { 7, { 0x06, 0x00, 0x06, 0x0e, 0x01, 0x0f, 0x05 } },
225 { 1, { 0x12 } },
226 /* Commands that are sent after pressing the 'Initialize'
227 button in the windows application */
228 { 1, { 0x03 } },
229 { 1, { 0x01 } },
230 { 2, { 0x09, 0x90 } },
231 { 3, { 0x08, 0x90, 0xfa } },
232 { 1, { 0x34 } },
233 { 2, { 0x35, 0x01 } },
234 { 2, { 0x36, 0x01 } },
235 { 2, { 0x30, 0x09 } },
236 { 4, { 0x30, 0x06, 0x00, 0xe2 } },
237 { 3, { 0x31, 0x01, 0x30 } },
238 { 3, { 0x31, 0x04, 0x09 } },
239 { 2, { 0x05, 0x02 } },
240 { 6, { 0x06, 0x03, 0x03, 0x08, 0x01, 0x0f } },
241 };
242
si4713_start_seq(struct si4713_usb_device * radio)243 static int si4713_start_seq(struct si4713_usb_device *radio)
244 {
245 int retval = 0;
246 int i;
247
248 radio->buffer[0] = 0x3f;
249
250 for (i = 0; i < ARRAY_SIZE(start_seq); i++) {
251 int len = start_seq[i].len;
252 const u8 *payload = start_seq[i].payload;
253
254 memcpy(radio->buffer + 1, payload, len);
255 memset(radio->buffer + len + 1, 0, BUFFER_LENGTH - 1 - len);
256 retval = si4713_send_startup_command(radio);
257 }
258
259 return retval;
260 }
261
262 static struct i2c_board_info si4713_board_info = {
263 I2C_BOARD_INFO("si4713", SI4713_I2C_ADDR_BUSEN_HIGH),
264 };
265
266 struct si4713_command_table {
267 int command_id;
268 u8 payload[8];
269 };
270
271 /*
272 * Structure of a command :
273 * Byte 1 : 0x3f (always)
274 * Byte 2 : 0x06 (send a command)
275 * Byte 3 : Unknown
276 * Byte 4 : Number of arguments + 1 (for the command byte)
277 * Byte 5 : Number of response bytes
278 */
279 static struct si4713_command_table command_table[] = {
280
281 { SI4713_CMD_POWER_UP, { 0x00, SI4713_PWUP_NARGS + 1, SI4713_PWUP_NRESP} },
282 { SI4713_CMD_GET_REV, { 0x03, 0x01, SI4713_GETREV_NRESP } },
283 { SI4713_CMD_POWER_DOWN, { 0x00, 0x01, SI4713_PWDN_NRESP} },
284 { SI4713_CMD_SET_PROPERTY, { 0x00, SI4713_SET_PROP_NARGS + 1, SI4713_SET_PROP_NRESP } },
285 { SI4713_CMD_GET_PROPERTY, { 0x00, SI4713_GET_PROP_NARGS + 1, SI4713_GET_PROP_NRESP } },
286 { SI4713_CMD_TX_TUNE_FREQ, { 0x03, SI4713_TXFREQ_NARGS + 1, SI4713_TXFREQ_NRESP } },
287 { SI4713_CMD_TX_TUNE_POWER, { 0x03, SI4713_TXPWR_NARGS + 1, SI4713_TXPWR_NRESP } },
288 { SI4713_CMD_TX_TUNE_MEASURE, { 0x03, SI4713_TXMEA_NARGS + 1, SI4713_TXMEA_NRESP } },
289 { SI4713_CMD_TX_TUNE_STATUS, { 0x00, SI4713_TXSTATUS_NARGS + 1, SI4713_TXSTATUS_NRESP } },
290 { SI4713_CMD_TX_ASQ_STATUS, { 0x03, SI4713_ASQSTATUS_NARGS + 1, SI4713_ASQSTATUS_NRESP } },
291 { SI4713_CMD_GET_INT_STATUS, { 0x03, 0x01, SI4713_GET_STATUS_NRESP } },
292 { SI4713_CMD_TX_RDS_BUFF, { 0x03, SI4713_RDSBUFF_NARGS + 1, SI4713_RDSBUFF_NRESP } },
293 { SI4713_CMD_TX_RDS_PS, { 0x00, SI4713_RDSPS_NARGS + 1, SI4713_RDSPS_NRESP } },
294 };
295
send_command(struct si4713_usb_device * radio,u8 * payload,char * data,int len)296 static int send_command(struct si4713_usb_device *radio, u8 *payload, char *data, int len)
297 {
298 int retval;
299
300 radio->buffer[0] = 0x3f;
301 radio->buffer[1] = 0x06;
302
303 memcpy(radio->buffer + 2, payload, 3);
304 memcpy(radio->buffer + 5, data, len);
305 memset(radio->buffer + 5 + len, 0, BUFFER_LENGTH - 5 - len);
306
307 /* send the command */
308 retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
309 0x09, 0x21, 0x033f, 0, radio->buffer,
310 BUFFER_LENGTH, USB_TIMEOUT);
311
312 return retval < 0 ? retval : 0;
313 }
314
si4713_i2c_read(struct si4713_usb_device * radio,char * data,int len)315 static int si4713_i2c_read(struct si4713_usb_device *radio, char *data, int len)
316 {
317 unsigned long until_jiffies = jiffies + usecs_to_jiffies(USB_RESP_TIMEOUT) + 1;
318 int retval;
319
320 /* receive the response */
321 for (;;) {
322 retval = usb_control_msg(radio->usbdev,
323 usb_rcvctrlpipe(radio->usbdev, 0),
324 0x01, 0xa1, 0x033f, 0, radio->buffer,
325 BUFFER_LENGTH, USB_TIMEOUT);
326 if (retval < 0)
327 return retval;
328
329 /*
330 * Check that we get a valid reply back (buffer[1] == 0) and
331 * that CTS is set before returning, otherwise we wait and try
332 * again. The i2c driver also does the CTS check, but the timeouts
333 * used there are much too small for this USB driver, so we wait
334 * for it here.
335 */
336 if (radio->buffer[1] == 0 && (radio->buffer[2] & SI4713_CTS)) {
337 memcpy(data, radio->buffer + 2, len);
338 return 0;
339 }
340 if (time_is_before_jiffies(until_jiffies)) {
341 /* Zero the status value, ensuring CTS isn't set */
342 data[0] = 0;
343 return 0;
344 }
345 msleep(3);
346 }
347 }
348
si4713_i2c_write(struct si4713_usb_device * radio,char * data,int len)349 static int si4713_i2c_write(struct si4713_usb_device *radio, char *data, int len)
350 {
351 int retval = -EINVAL;
352 int i;
353
354 if (len > BUFFER_LENGTH - 5)
355 return -EINVAL;
356
357 for (i = 0; i < ARRAY_SIZE(command_table); i++) {
358 if (data[0] == command_table[i].command_id)
359 retval = send_command(radio, command_table[i].payload,
360 data, len);
361 }
362
363 return retval < 0 ? retval : 0;
364 }
365
si4713_transfer(struct i2c_adapter * i2c_adapter,struct i2c_msg * msgs,int num)366 static int si4713_transfer(struct i2c_adapter *i2c_adapter,
367 struct i2c_msg *msgs, int num)
368 {
369 struct si4713_usb_device *radio = i2c_get_adapdata(i2c_adapter);
370 int retval = -EINVAL;
371 int i;
372
373 for (i = 0; i < num; i++) {
374 if (msgs[i].flags & I2C_M_RD)
375 retval = si4713_i2c_read(radio, msgs[i].buf, msgs[i].len);
376 else
377 retval = si4713_i2c_write(radio, msgs[i].buf, msgs[i].len);
378 if (retval)
379 break;
380 }
381
382 return retval ? retval : num;
383 }
384
si4713_functionality(struct i2c_adapter * adapter)385 static u32 si4713_functionality(struct i2c_adapter *adapter)
386 {
387 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
388 }
389
390 static const struct i2c_algorithm si4713_algo = {
391 .master_xfer = si4713_transfer,
392 .functionality = si4713_functionality,
393 };
394
395 /* This name value shows up in the sysfs filename associated
396 with this I2C adapter */
397 static const struct i2c_adapter si4713_i2c_adapter_template = {
398 .name = "si4713-i2c",
399 .owner = THIS_MODULE,
400 .algo = &si4713_algo,
401 };
402
si4713_register_i2c_adapter(struct si4713_usb_device * radio)403 static int si4713_register_i2c_adapter(struct si4713_usb_device *radio)
404 {
405 radio->i2c_adapter = si4713_i2c_adapter_template;
406 /* set up sysfs linkage to our parent device */
407 radio->i2c_adapter.dev.parent = &radio->usbdev->dev;
408 i2c_set_adapdata(&radio->i2c_adapter, radio);
409
410 return i2c_add_adapter(&radio->i2c_adapter);
411 }
412
413 /* check if the device is present and register with v4l and usb if it is */
usb_si4713_probe(struct usb_interface * intf,const struct usb_device_id * id)414 static int usb_si4713_probe(struct usb_interface *intf,
415 const struct usb_device_id *id)
416 {
417 struct si4713_usb_device *radio;
418 struct i2c_adapter *adapter;
419 struct v4l2_subdev *sd;
420 int retval = -ENOMEM;
421
422 dev_info(&intf->dev, "Si4713 development board discovered: (%04X:%04X)\n",
423 id->idVendor, id->idProduct);
424
425 /* Initialize local device structure */
426 radio = kzalloc(sizeof(struct si4713_usb_device), GFP_KERNEL);
427 if (radio)
428 radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
429
430 if (!radio || !radio->buffer) {
431 dev_err(&intf->dev, "kmalloc for si4713_usb_device failed\n");
432 kfree(radio);
433 return -ENOMEM;
434 }
435
436 mutex_init(&radio->lock);
437
438 radio->usbdev = interface_to_usbdev(intf);
439 radio->intf = intf;
440 usb_set_intfdata(intf, &radio->v4l2_dev);
441
442 retval = si4713_start_seq(radio);
443 if (retval < 0)
444 goto err_v4l2;
445
446 retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
447 if (retval < 0) {
448 dev_err(&intf->dev, "couldn't register v4l2_device\n");
449 goto err_v4l2;
450 }
451
452 retval = si4713_register_i2c_adapter(radio);
453 if (retval < 0) {
454 dev_err(&intf->dev, "could not register i2c device\n");
455 goto err_i2cdev;
456 }
457
458 adapter = &radio->i2c_adapter;
459 sd = v4l2_i2c_new_subdev_board(&radio->v4l2_dev, adapter,
460 &si4713_board_info, NULL);
461 radio->v4l2_subdev = sd;
462 if (!sd) {
463 dev_err(&intf->dev, "cannot get v4l2 subdevice\n");
464 retval = -ENODEV;
465 goto del_adapter;
466 }
467
468 radio->vdev.ctrl_handler = sd->ctrl_handler;
469 radio->v4l2_dev.release = usb_si4713_video_device_release;
470 strlcpy(radio->vdev.name, radio->v4l2_dev.name,
471 sizeof(radio->vdev.name));
472 radio->vdev.v4l2_dev = &radio->v4l2_dev;
473 radio->vdev.fops = &usb_si4713_fops;
474 radio->vdev.ioctl_ops = &usb_si4713_ioctl_ops;
475 radio->vdev.lock = &radio->lock;
476 radio->vdev.release = video_device_release_empty;
477 radio->vdev.vfl_dir = VFL_DIR_TX;
478
479 video_set_drvdata(&radio->vdev, radio);
480
481 retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
482 if (retval < 0) {
483 dev_err(&intf->dev, "could not register video device\n");
484 goto del_adapter;
485 }
486
487 dev_info(&intf->dev, "V4L2 device registered as %s\n",
488 video_device_node_name(&radio->vdev));
489
490 return 0;
491
492 del_adapter:
493 i2c_del_adapter(adapter);
494 err_i2cdev:
495 v4l2_device_unregister(&radio->v4l2_dev);
496 err_v4l2:
497 kfree(radio->buffer);
498 kfree(radio);
499 return retval;
500 }
501
usb_si4713_disconnect(struct usb_interface * intf)502 static void usb_si4713_disconnect(struct usb_interface *intf)
503 {
504 struct si4713_usb_device *radio = to_si4713_dev(usb_get_intfdata(intf));
505
506 dev_info(&intf->dev, "Si4713 development board now disconnected\n");
507
508 mutex_lock(&radio->lock);
509 usb_set_intfdata(intf, NULL);
510 video_unregister_device(&radio->vdev);
511 v4l2_device_disconnect(&radio->v4l2_dev);
512 mutex_unlock(&radio->lock);
513 v4l2_device_put(&radio->v4l2_dev);
514 }
515
516 /* USB subsystem interface */
517 static struct usb_driver usb_si4713_driver = {
518 .name = "radio-usb-si4713",
519 .probe = usb_si4713_probe,
520 .disconnect = usb_si4713_disconnect,
521 .id_table = usb_si4713_usb_device_table,
522 };
523
524 module_usb_driver(usb_si4713_driver);
525