radio-usb-si4713.c 15 KB

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