stk1160-core.c 10 KB

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