stk1160-core.c 10 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. 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 const 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. mutex_destroy(&dev->v4l_lock);
  142. mutex_destroy(&dev->vb_queue_lock);
  143. kfree(dev->alt_max_pkt_size);
  144. kfree(dev);
  145. }
  146. /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
  147. #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  148. /*
  149. * Scan usb interface and populate max_pkt_size array
  150. * with information on each alternate setting.
  151. * The array should be allocated by the caller.
  152. */
  153. static int stk1160_scan_usb(struct usb_interface *intf, struct usb_device *udev,
  154. unsigned int *max_pkt_size)
  155. {
  156. int i, e, sizedescr, size, ifnum;
  157. const struct usb_endpoint_descriptor *desc;
  158. bool has_video = false, has_audio = false;
  159. const char *speed;
  160. ifnum = intf->altsetting[0].desc.bInterfaceNumber;
  161. /* Get endpoints */
  162. for (i = 0; i < intf->num_altsetting; i++) {
  163. for (e = 0; e < intf->altsetting[i].desc.bNumEndpoints; e++) {
  164. /* This isn't clear enough, at least to me */
  165. desc = &intf->altsetting[i].endpoint[e].desc;
  166. sizedescr = le16_to_cpu(desc->wMaxPacketSize);
  167. size = sizedescr & 0x7ff;
  168. if (udev->speed == USB_SPEED_HIGH)
  169. size = size * hb_mult(sizedescr);
  170. if (usb_endpoint_xfer_isoc(desc) &&
  171. usb_endpoint_dir_in(desc)) {
  172. switch (desc->bEndpointAddress) {
  173. case STK1160_EP_AUDIO:
  174. has_audio = true;
  175. break;
  176. case STK1160_EP_VIDEO:
  177. has_video = true;
  178. max_pkt_size[i] = size;
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. /* Is this even possible? */
  185. if (!(has_audio || has_video)) {
  186. dev_err(&udev->dev, "no audio or video endpoints found\n");
  187. return -ENODEV;
  188. }
  189. switch (udev->speed) {
  190. case USB_SPEED_LOW:
  191. speed = "1.5";
  192. break;
  193. case USB_SPEED_FULL:
  194. speed = "12";
  195. break;
  196. case USB_SPEED_HIGH:
  197. speed = "480";
  198. break;
  199. default:
  200. speed = "unknown";
  201. }
  202. dev_info(&udev->dev, "New device %s %s @ %s Mbps (%04x:%04x, interface %d, class %d)\n",
  203. udev->manufacturer ? udev->manufacturer : "",
  204. udev->product ? udev->product : "",
  205. speed,
  206. le16_to_cpu(udev->descriptor.idVendor),
  207. le16_to_cpu(udev->descriptor.idProduct),
  208. ifnum,
  209. intf->altsetting->desc.bInterfaceNumber);
  210. /* This should never happen, since we rejected audio interfaces */
  211. if (has_audio)
  212. dev_warn(&udev->dev, "audio interface %d found.\n\
  213. This is not implemented by this driver,\
  214. you should use snd-usb-audio instead\n", ifnum);
  215. if (has_video)
  216. dev_info(&udev->dev, "video interface %d found\n",
  217. ifnum);
  218. /*
  219. * Make sure we have 480 Mbps of bandwidth, otherwise things like
  220. * video stream wouldn't likely work, since 12 Mbps is generally
  221. * not enough even for most streams.
  222. */
  223. if (udev->speed != USB_SPEED_HIGH)
  224. dev_warn(&udev->dev, "must be connected to a high-speed USB 2.0 port\n\
  225. You may not be able to stream video smoothly\n");
  226. return 0;
  227. }
  228. static int stk1160_probe(struct usb_interface *interface,
  229. const struct usb_device_id *id)
  230. {
  231. int rc = 0;
  232. unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
  233. struct usb_device *udev;
  234. struct stk1160 *dev;
  235. udev = interface_to_usbdev(interface);
  236. /*
  237. * Since usb audio class is supported by snd-usb-audio,
  238. * we reject audio interface.
  239. */
  240. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
  241. return -ENODEV;
  242. /* Alloc an array for all possible max_pkt_size */
  243. alt_max_pkt_size = kmalloc_array(interface->num_altsetting,
  244. sizeof(alt_max_pkt_size[0]),
  245. GFP_KERNEL);
  246. if (alt_max_pkt_size == NULL)
  247. return -ENOMEM;
  248. /*
  249. * Scan usb posibilities and populate alt_max_pkt_size array.
  250. * Also, check if device speed is fast enough.
  251. */
  252. rc = stk1160_scan_usb(interface, udev, alt_max_pkt_size);
  253. if (rc < 0) {
  254. kfree(alt_max_pkt_size);
  255. return rc;
  256. }
  257. dev = kzalloc(sizeof(struct stk1160), GFP_KERNEL);
  258. if (dev == NULL) {
  259. kfree(alt_max_pkt_size);
  260. return -ENOMEM;
  261. }
  262. dev->alt_max_pkt_size = alt_max_pkt_size;
  263. dev->udev = udev;
  264. dev->num_alt = interface->num_altsetting;
  265. dev->ctl_input = input;
  266. /* We save struct device for debug purposes only */
  267. dev->dev = &interface->dev;
  268. usb_set_intfdata(interface, dev);
  269. /* initialize videobuf2 stuff */
  270. rc = stk1160_vb2_setup(dev);
  271. if (rc < 0)
  272. goto free_err;
  273. /*
  274. * There is no need to take any locks here in probe
  275. * because we register the device node as the *last* thing.
  276. */
  277. spin_lock_init(&dev->buf_lock);
  278. mutex_init(&dev->v4l_lock);
  279. mutex_init(&dev->vb_queue_lock);
  280. rc = v4l2_ctrl_handler_init(&dev->ctrl_handler, 0);
  281. if (rc) {
  282. stk1160_err("v4l2_ctrl_handler_init failed (%d)\n", rc);
  283. goto free_err;
  284. }
  285. /*
  286. * We obtain a v4l2_dev but defer
  287. * registration of video device node as the last thing.
  288. * There is no need to set the name if we give a device struct
  289. */
  290. dev->v4l2_dev.release = stk1160_release;
  291. dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
  292. rc = v4l2_device_register(dev->dev, &dev->v4l2_dev);
  293. if (rc) {
  294. stk1160_err("v4l2_device_register failed (%d)\n", rc);
  295. goto free_ctrl;
  296. }
  297. rc = stk1160_i2c_register(dev);
  298. if (rc < 0)
  299. goto unreg_v4l2;
  300. /*
  301. * To the best of my knowledge stk1160 boards only have
  302. * saa7113, but it doesn't hurt to support them all.
  303. */
  304. dev->sd_saa7115 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
  305. "saa7115_auto", 0, saa7113_addrs);
  306. /* i2c reset saa711x */
  307. v4l2_device_call_all(&dev->v4l2_dev, 0, core, reset, 0);
  308. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
  309. /* reset stk1160 to default values */
  310. stk1160_reg_reset(dev);
  311. /* select default input */
  312. stk1160_select_input(dev);
  313. stk1160_ac97_setup(dev);
  314. rc = stk1160_video_register(dev);
  315. if (rc < 0)
  316. goto unreg_i2c;
  317. return 0;
  318. unreg_i2c:
  319. stk1160_i2c_unregister(dev);
  320. unreg_v4l2:
  321. v4l2_device_unregister(&dev->v4l2_dev);
  322. free_ctrl:
  323. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  324. free_err:
  325. kfree(alt_max_pkt_size);
  326. kfree(dev);
  327. return rc;
  328. }
  329. static void stk1160_disconnect(struct usb_interface *interface)
  330. {
  331. struct stk1160 *dev;
  332. dev = usb_get_intfdata(interface);
  333. usb_set_intfdata(interface, NULL);
  334. /*
  335. * Wait until all current v4l2 operation are finished
  336. * then deallocate resources
  337. */
  338. mutex_lock(&dev->vb_queue_lock);
  339. mutex_lock(&dev->v4l_lock);
  340. /* Here is the only place where isoc get released */
  341. stk1160_uninit_isoc(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. * Otherwise, 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);