radio-usb-si4713.c 15 KB

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