1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates.
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>
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>
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");
31 /* The Device announces itself as Cygnal Integrated Products, Inc. */
32 #define USB_SI4713_VENDOR 0x10c4
33 #define USB_SI4713_PRODUCT 0x8244
35 #define BUFFER_LENGTH 64
36 #define USB_TIMEOUT 1000
37 #define USB_RESP_TIMEOUT 50000
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 */
46 MODULE_DEVICE_TABLE(usb
, usb_si4713_usb_device_table
);
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
;
55 struct i2c_adapter i2c_adapter
;
60 static inline struct si4713_usb_device
*to_si4713_dev(struct v4l2_device
*v4l2_dev
)
62 return container_of(v4l2_dev
, struct si4713_usb_device
, v4l2_dev
);
65 static int vidioc_querycap(struct file
*file
, void *priv
,
66 struct v4l2_capability
*v
)
68 struct si4713_usb_device
*radio
= video_drvdata(file
);
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
;
79 static int vidioc_g_modulator(struct file
*file
, void *priv
,
80 struct v4l2_modulator
*vm
)
82 struct si4713_usb_device
*radio
= video_drvdata(file
);
84 return v4l2_subdev_call(radio
->v4l2_subdev
, tuner
, g_modulator
, vm
);
87 static int vidioc_s_modulator(struct file
*file
, void *priv
,
88 const struct v4l2_modulator
*vm
)
90 struct si4713_usb_device
*radio
= video_drvdata(file
);
92 return v4l2_subdev_call(radio
->v4l2_subdev
, tuner
, s_modulator
, vm
);
95 static int vidioc_s_frequency(struct file
*file
, void *priv
,
96 const struct v4l2_frequency
*vf
)
98 struct si4713_usb_device
*radio
= video_drvdata(file
);
100 return v4l2_subdev_call(radio
->v4l2_subdev
, tuner
, s_frequency
, vf
);
103 static int vidioc_g_frequency(struct file
*file
, void *priv
,
104 struct v4l2_frequency
*vf
)
106 struct si4713_usb_device
*radio
= video_drvdata(file
);
108 return v4l2_subdev_call(radio
->v4l2_subdev
, tuner
, g_frequency
, vf
);
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
,
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
,
131 static void usb_si4713_video_device_release(struct v4l2_device
*v4l2_dev
)
133 struct si4713_usb_device
*radio
= to_si4713_dev(v4l2_dev
);
134 struct i2c_adapter
*adapter
= &radio
->i2c_adapter
;
136 i2c_del_adapter(adapter
);
137 v4l2_device_unregister(&radio
->v4l2_dev
);
138 kfree(radio
->buffer
);
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.
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.
153 * The complete list of startup commands is given in the start_seq table below.
155 static int si4713_send_startup_command(struct si4713_usb_device
*radio
)
157 unsigned long until_jiffies
= jiffies
+ usecs_to_jiffies(USB_RESP_TIMEOUT
) + 1;
158 u8
*buffer
= radio
->buffer
;
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
);
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
);
175 if (!radio
->buffer
[1]) {
176 /* USB traffic sniffing showed that some commands require
177 * additional checks. */
180 if (radio
->buffer
[2] == 0)
185 if (radio
->buffer
[2] & SI4713_CTS
)
189 if ((radio
->buffer
[2] & SI4713_CTS
) && radio
->buffer
[9] == 0x08)
196 if (time_is_before_jiffies(until_jiffies
))
204 struct si4713_start_seq_table
{
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
214 static const struct si4713_start_seq_table start_seq
[] = {
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 } },
226 /* Commands that are sent after pressing the 'Initialize'
227 button in the windows application */
230 { 2, { 0x09, 0x90 } },
231 { 3, { 0x08, 0x90, 0xfa } },
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 } },
243 static int si4713_start_seq(struct si4713_usb_device
*radio
)
248 radio
->buffer
[0] = 0x3f;
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
;
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
);
262 static struct i2c_board_info si4713_board_info
= {
263 I2C_BOARD_INFO("si4713", SI4713_I2C_ADDR_BUSEN_HIGH
),
266 struct si4713_command_table
{
272 * Structure of a command :
273 * Byte 1 : 0x3f (always)
274 * Byte 2 : 0x06 (send a command)
276 * Byte 4 : Number of arguments + 1 (for the command byte)
277 * Byte 5 : Number of response bytes
279 static struct si4713_command_table command_table
[] = {
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
} },
296 static int send_command(struct si4713_usb_device
*radio
, u8
*payload
, char *data
, int len
)
300 radio
->buffer
[0] = 0x3f;
301 radio
->buffer
[1] = 0x06;
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
);
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
);
312 return retval
< 0 ? retval
: 0;
315 static int si4713_i2c_read(struct si4713_usb_device
*radio
, char *data
, int len
)
317 unsigned long until_jiffies
= jiffies
+ usecs_to_jiffies(USB_RESP_TIMEOUT
) + 1;
320 /* receive the response */
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
);
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
336 if (radio
->buffer
[1] == 0 && (radio
->buffer
[2] & SI4713_CTS
)) {
337 memcpy(data
, radio
->buffer
+ 2, len
);
340 if (time_is_before_jiffies(until_jiffies
)) {
341 /* Zero the status value, ensuring CTS isn't set */
349 static int si4713_i2c_write(struct si4713_usb_device
*radio
, char *data
, int len
)
351 int retval
= -EINVAL
;
354 if (len
> BUFFER_LENGTH
- 5)
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
,
363 return retval
< 0 ? retval
: 0;
366 static int si4713_transfer(struct i2c_adapter
*i2c_adapter
,
367 struct i2c_msg
*msgs
, int num
)
369 struct si4713_usb_device
*radio
= i2c_get_adapdata(i2c_adapter
);
370 int retval
= -EINVAL
;
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
);
377 retval
= si4713_i2c_write(radio
, msgs
[i
].buf
, msgs
[i
].len
);
382 return retval
? retval
: num
;
385 static u32
si4713_functionality(struct i2c_adapter
*adapter
)
387 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
390 static const struct i2c_algorithm si4713_algo
= {
391 .master_xfer
= si4713_transfer
,
392 .functionality
= si4713_functionality
,
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
,
403 static int si4713_register_i2c_adapter(struct si4713_usb_device
*radio
)
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
);
410 return i2c_add_adapter(&radio
->i2c_adapter
);
413 /* check if the device is present and register with v4l and usb if it is */
414 static int usb_si4713_probe(struct usb_interface
*intf
,
415 const struct usb_device_id
*id
)
417 struct si4713_usb_device
*radio
;
418 struct i2c_adapter
*adapter
;
419 struct v4l2_subdev
*sd
;
420 int retval
= -ENOMEM
;
422 dev_info(&intf
->dev
, "Si4713 development board discovered: (%04X:%04X)\n",
423 id
->idVendor
, id
->idProduct
);
425 /* Initialize local device structure */
426 radio
= kzalloc(sizeof(struct si4713_usb_device
), GFP_KERNEL
);
428 radio
->buffer
= kmalloc(BUFFER_LENGTH
, GFP_KERNEL
);
430 if (!radio
|| !radio
->buffer
) {
431 dev_err(&intf
->dev
, "kmalloc for si4713_usb_device failed\n");
436 mutex_init(&radio
->lock
);
438 radio
->usbdev
= interface_to_usbdev(intf
);
440 usb_set_intfdata(intf
, &radio
->v4l2_dev
);
442 retval
= si4713_start_seq(radio
);
446 retval
= v4l2_device_register(&intf
->dev
, &radio
->v4l2_dev
);
448 dev_err(&intf
->dev
, "couldn't register v4l2_device\n");
452 retval
= si4713_register_i2c_adapter(radio
);
454 dev_err(&intf
->dev
, "could not register i2c device\n");
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
;
463 dev_err(&intf
->dev
, "cannot get v4l2 subdevice\n");
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
;
479 video_set_drvdata(&radio
->vdev
, radio
);
481 retval
= video_register_device(&radio
->vdev
, VFL_TYPE_RADIO
, -1);
483 dev_err(&intf
->dev
, "could not register video device\n");
487 dev_info(&intf
->dev
, "V4L2 device registered as %s\n",
488 video_device_node_name(&radio
->vdev
));
493 i2c_del_adapter(adapter
);
495 v4l2_device_unregister(&radio
->v4l2_dev
);
497 kfree(radio
->buffer
);
502 static void usb_si4713_disconnect(struct usb_interface
*intf
)
504 struct si4713_usb_device
*radio
= to_si4713_dev(usb_get_intfdata(intf
));
506 dev_info(&intf
->dev
, "Si4713 development board now disconnected\n");
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
);
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
,
524 module_usb_driver(usb_si4713_driver
);