stk1160-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * STK1160 driver
  3. *
  4. * Copyright (C) 2012 Ezequiel Garcia
  5. * <elezegarcia--a.t--gmail.com>
  6. *
  7. * Based on Easycap driver by R.M. Thomas
  8. * Copyright (C) 2010 R.M. Thomas
  9. * <rmthomas--a.t--sciolus.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * TODO:
  22. *
  23. * 1. (Try to) detect if we must register ac97 mixer
  24. * 2. Support stream at lower speed: lower frame rate or lower frame size.
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/kernel.h>
  30. #include <linux/errno.h>
  31. #include <linux/slab.h>
  32. #include <linux/usb.h>
  33. #include <linux/mm.h>
  34. #include <linux/vmalloc.h>
  35. #include <media/saa7115.h>
  36. #include "stk1160.h"
  37. #include "stk1160-reg.h"
  38. static unsigned int input;
  39. module_param(input, int, 0644);
  40. MODULE_PARM_DESC(input, "Set default input");
  41. MODULE_LICENSE("GPL");
  42. MODULE_AUTHOR("Ezequiel Garcia");
  43. MODULE_DESCRIPTION("STK1160 driver");
  44. /* Devices supported by this driver */
  45. static struct usb_device_id stk1160_id_table[] = {
  46. { USB_DEVICE(0x05e1, 0x0408) },
  47. { }
  48. };
  49. MODULE_DEVICE_TABLE(usb, stk1160_id_table);
  50. /* saa7113 I2C address */
  51. static unsigned short saa7113_addrs[] = {
  52. 0x4a >> 1,
  53. I2C_CLIENT_END
  54. };
  55. /*
  56. * Read/Write stk registers
  57. */
  58. int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value)
  59. {
  60. int ret;
  61. int pipe = usb_rcvctrlpipe(dev->udev, 0);
  62. u8 *buf;
  63. *value = 0;
  64. buf = kmalloc(sizeof(u8), GFP_KERNEL);
  65. if (!buf)
  66. return -ENOMEM;
  67. ret = usb_control_msg(dev->udev, pipe, 0x00,
  68. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  69. 0x00, reg, buf, sizeof(u8), HZ);
  70. if (ret < 0) {
  71. stk1160_err("read failed on reg 0x%x (%d)\n",
  72. reg, ret);
  73. kfree(buf);
  74. return ret;
  75. }
  76. *value = *buf;
  77. kfree(buf);
  78. return 0;
  79. }
  80. int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value)
  81. {
  82. int ret;
  83. int pipe = usb_sndctrlpipe(dev->udev, 0);
  84. ret = usb_control_msg(dev->udev, pipe, 0x01,
  85. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  86. value, reg, NULL, 0, HZ);
  87. if (ret < 0) {
  88. stk1160_err("write failed on reg 0x%x (%d)\n",
  89. reg, ret);
  90. return ret;
  91. }
  92. return 0;
  93. }
  94. void stk1160_select_input(struct stk1160 *dev)
  95. {
  96. int route;
  97. static const u8 gctrl[] = {
  98. 0x98, 0x90, 0x88, 0x80, 0x98
  99. };
  100. if (dev->ctl_input == STK1160_SVIDEO_INPUT)
  101. route = SAA7115_SVIDEO3;
  102. else
  103. route = SAA7115_COMPOSITE0;
  104. if (dev->ctl_input < ARRAY_SIZE(gctrl)) {
  105. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  106. route, 0, 0);
  107. stk1160_write_reg(dev, STK1160_GCTRL, gctrl[dev->ctl_input]);
  108. }
  109. }
  110. /* TODO: We should break this into pieces */
  111. static void stk1160_reg_reset(struct stk1160 *dev)
  112. {
  113. int i;
  114. static const struct regval ctl[] = {
  115. {STK1160_GCTRL+2, 0x0078},
  116. {STK1160_RMCTL+1, 0x0000},
  117. {STK1160_RMCTL+3, 0x0002},
  118. {STK1160_PLLSO, 0x0010},
  119. {STK1160_PLLSO+1, 0x0000},
  120. {STK1160_PLLSO+2, 0x0014},
  121. {STK1160_PLLSO+3, 0x000E},
  122. {STK1160_PLLFD, 0x0046},
  123. /* Timing generator setup */
  124. {STK1160_TIGEN, 0x0012},
  125. {STK1160_TICTL, 0x002D},
  126. {STK1160_TICTL+1, 0x0001},
  127. {STK1160_TICTL+2, 0x0000},
  128. {STK1160_TICTL+3, 0x0000},
  129. {STK1160_TIGEN, 0x0080},
  130. {0xffff, 0xffff}
  131. };
  132. for (i = 0; ctl[i].reg != 0xffff; i++)
  133. stk1160_write_reg(dev, ctl[i].reg, ctl[i].val);
  134. }
  135. static void stk1160_release(struct v4l2_device *v4l2_dev)
  136. {
  137. struct stk1160 *dev = container_of(v4l2_dev, struct stk1160, v4l2_dev);
  138. stk1160_dbg("releasing all resources\n");
  139. stk1160_i2c_unregister(dev);
  140. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  141. v4l2_device_unregister(&dev->v4l2_dev);
  142. kfree(dev->alt_max_pkt_size);
  143. kfree(dev);
  144. }
  145. /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
  146. #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  147. /*
  148. * Scan usb interface and populate max_pkt_size array
  149. * with information on each alternate setting.
  150. * The array should be allocated by the caller.
  151. */
  152. static int stk1160_scan_usb(struct usb_interface *intf, struct usb_device *udev,
  153. unsigned int *max_pkt_size)
  154. {
  155. int i, e, sizedescr, size, ifnum;
  156. const struct usb_endpoint_descriptor *desc;
  157. bool has_video = false, has_audio = false;
  158. const char *speed;
  159. ifnum = intf->altsetting[0].desc.bInterfaceNumber;
  160. /* Get endpoints */
  161. for (i = 0; i < intf->num_altsetting; i++) {
  162. for (e = 0; e < intf->altsetting[i].desc.bNumEndpoints; e++) {
  163. /* This isn't clear enough, at least to me */
  164. desc = &intf->altsetting[i].endpoint[e].desc;
  165. sizedescr = le16_to_cpu(desc->wMaxPacketSize);
  166. size = sizedescr & 0x7ff;
  167. if (udev->speed == USB_SPEED_HIGH)
  168. size = size * hb_mult(sizedescr);
  169. if (usb_endpoint_xfer_isoc(desc) &&
  170. usb_endpoint_dir_in(desc)) {
  171. switch (desc->bEndpointAddress) {
  172. case STK1160_EP_AUDIO:
  173. has_audio = true;
  174. break;
  175. case STK1160_EP_VIDEO:
  176. has_video = true;
  177. max_pkt_size[i] = size;
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. /* Is this even possible? */
  184. if (!(has_audio || has_video)) {
  185. dev_err(&udev->dev, "no audio or video endpoints found\n");
  186. return -ENODEV;
  187. }
  188. switch (udev->speed) {
  189. case USB_SPEED_LOW:
  190. speed = "1.5";
  191. break;
  192. case USB_SPEED_FULL:
  193. speed = "12";
  194. break;
  195. case USB_SPEED_HIGH:
  196. speed = "480";
  197. break;
  198. default:
  199. speed = "unknown";
  200. }
  201. dev_info(&udev->dev, "New device %s %s @ %s Mbps (%04x:%04x, interface %d, class %d)\n",
  202. udev->manufacturer ? udev->manufacturer : "",
  203. udev->product ? udev->product : "",
  204. speed,
  205. le16_to_cpu(udev->descriptor.idVendor),
  206. le16_to_cpu(udev->descriptor.idProduct),
  207. ifnum,
  208. intf->altsetting->desc.bInterfaceNumber);
  209. /* This should never happen, since we rejected audio interfaces */
  210. if (has_audio)
  211. dev_warn(&udev->dev, "audio interface %d found.\n\
  212. This is not implemented by this driver,\
  213. you should use snd-usb-audio instead\n", ifnum);
  214. if (has_video)
  215. dev_info(&udev->dev, "video interface %d found\n",
  216. ifnum);
  217. /*
  218. * Make sure we have 480 Mbps of bandwidth, otherwise things like
  219. * video stream wouldn't likely work, since 12 Mbps is generally
  220. * not enough even for most streams.
  221. */
  222. if (udev->speed != USB_SPEED_HIGH)
  223. dev_warn(&udev->dev, "must be connected to a high-speed USB 2.0 port\n\
  224. You may not be able to stream video smoothly\n");
  225. return 0;
  226. }
  227. static int stk1160_probe(struct usb_interface *interface,
  228. const struct usb_device_id *id)
  229. {
  230. int rc = 0;
  231. unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
  232. struct usb_device *udev;
  233. struct stk1160 *dev;
  234. udev = interface_to_usbdev(interface);
  235. /*
  236. * Since usb audio class is supported by snd-usb-audio,
  237. * we reject audio interface.
  238. */
  239. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
  240. return -ENODEV;
  241. /* Alloc an array for all possible max_pkt_size */
  242. alt_max_pkt_size = kmalloc(sizeof(alt_max_pkt_size[0]) *
  243. interface->num_altsetting, GFP_KERNEL);
  244. if (alt_max_pkt_size == NULL)
  245. return -ENOMEM;
  246. /*
  247. * Scan usb posibilities and populate alt_max_pkt_size array.
  248. * Also, check if device speed is fast enough.
  249. */
  250. rc = stk1160_scan_usb(interface, udev, alt_max_pkt_size);
  251. if (rc < 0) {
  252. kfree(alt_max_pkt_size);
  253. return rc;
  254. }
  255. dev = kzalloc(sizeof(struct stk1160), GFP_KERNEL);
  256. if (dev == NULL) {
  257. kfree(alt_max_pkt_size);
  258. return -ENOMEM;
  259. }
  260. dev->alt_max_pkt_size = alt_max_pkt_size;
  261. dev->udev = udev;
  262. dev->num_alt = interface->num_altsetting;
  263. dev->ctl_input = input;
  264. /* We save struct device for debug purposes only */
  265. dev->dev = &interface->dev;
  266. usb_set_intfdata(interface, dev);
  267. /* initialize videobuf2 stuff */
  268. rc = stk1160_vb2_setup(dev);
  269. if (rc < 0)
  270. goto free_err;
  271. /*
  272. * There is no need to take any locks here in probe
  273. * because we register the device node as the *last* thing.
  274. */
  275. spin_lock_init(&dev->buf_lock);
  276. mutex_init(&dev->v4l_lock);
  277. mutex_init(&dev->vb_queue_lock);
  278. rc = v4l2_ctrl_handler_init(&dev->ctrl_handler, 0);
  279. if (rc) {
  280. stk1160_err("v4l2_ctrl_handler_init failed (%d)\n", rc);
  281. goto free_err;
  282. }
  283. /*
  284. * We obtain a v4l2_dev but defer
  285. * registration of video device node as the last thing.
  286. * There is no need to set the name if we give a device struct
  287. */
  288. dev->v4l2_dev.release = stk1160_release;
  289. dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
  290. rc = v4l2_device_register(dev->dev, &dev->v4l2_dev);
  291. if (rc) {
  292. stk1160_err("v4l2_device_register failed (%d)\n", rc);
  293. goto free_ctrl;
  294. }
  295. rc = stk1160_i2c_register(dev);
  296. if (rc < 0)
  297. goto unreg_v4l2;
  298. /*
  299. * To the best of my knowledge stk1160 boards only have
  300. * saa7113, but it doesn't hurt to support them all.
  301. */
  302. dev->sd_saa7115 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
  303. "saa7115_auto", 0, saa7113_addrs);
  304. /* i2c reset saa711x */
  305. v4l2_device_call_all(&dev->v4l2_dev, 0, core, reset, 0);
  306. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
  307. /* reset stk1160 to default values */
  308. stk1160_reg_reset(dev);
  309. /* select default input */
  310. stk1160_select_input(dev);
  311. stk1160_ac97_register(dev);
  312. rc = stk1160_video_register(dev);
  313. if (rc < 0)
  314. goto unreg_i2c;
  315. return 0;
  316. unreg_i2c:
  317. stk1160_i2c_unregister(dev);
  318. unreg_v4l2:
  319. v4l2_device_unregister(&dev->v4l2_dev);
  320. free_ctrl:
  321. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  322. free_err:
  323. kfree(alt_max_pkt_size);
  324. kfree(dev);
  325. return rc;
  326. }
  327. static void stk1160_disconnect(struct usb_interface *interface)
  328. {
  329. struct stk1160 *dev;
  330. dev = usb_get_intfdata(interface);
  331. usb_set_intfdata(interface, NULL);
  332. /*
  333. * Wait until all current v4l2 operation are finished
  334. * then deallocate resources
  335. */
  336. mutex_lock(&dev->vb_queue_lock);
  337. mutex_lock(&dev->v4l_lock);
  338. /* Here is the only place where isoc get released */
  339. stk1160_uninit_isoc(dev);
  340. /* ac97 unregister needs to be done before usb_device is cleared */
  341. stk1160_ac97_unregister(dev);
  342. stk1160_clear_queue(dev);
  343. video_unregister_device(&dev->vdev);
  344. v4l2_device_disconnect(&dev->v4l2_dev);
  345. /* This way current users can detect device is gone */
  346. dev->udev = NULL;
  347. mutex_unlock(&dev->v4l_lock);
  348. mutex_unlock(&dev->vb_queue_lock);
  349. /*
  350. * This calls stk1160_release if it's the last reference.
  351. * therwise, release is posponed until there are no users left.
  352. */
  353. v4l2_device_put(&dev->v4l2_dev);
  354. }
  355. static struct usb_driver stk1160_usb_driver = {
  356. .name = "stk1160",
  357. .id_table = stk1160_id_table,
  358. .probe = stk1160_probe,
  359. .disconnect = stk1160_disconnect,
  360. };
  361. module_usb_driver(stk1160_usb_driver);