radio-usb-si4713.c 15 KB

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