stk1160-core.c 10 KB

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