PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/media/video/hdpvr/hdpvr-core.c

https://github.com/dutchmaster/cm-kernel
C | 465 lines | 360 code | 70 blank | 35 comment | 38 complexity | 6d4d36f490a3cc4bfd9b8c6583d8bf0d MD5 | raw file
  1. /*
  2. * Hauppauge HD PVR USB driver
  3. *
  4. * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  5. * Copyright (C) 2008 Janne Grunau (j@jannau.net)
  6. * Copyright (C) 2008 John Poet
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, version 2.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/uaccess.h>
  19. #include <asm/atomic.h>
  20. #include <linux/usb.h>
  21. #include <linux/mutex.h>
  22. #include <linux/i2c.h>
  23. #include <linux/videodev2.h>
  24. #include <media/v4l2-dev.h>
  25. #include <media/v4l2-common.h>
  26. #include "hdpvr.h"
  27. static int video_nr[HDPVR_MAX] = {[0 ... (HDPVR_MAX - 1)] = UNSET};
  28. module_param_array(video_nr, int, NULL, 0);
  29. MODULE_PARM_DESC(video_nr, "video device number (-1=Auto)");
  30. /* holds the number of currently registered devices */
  31. static atomic_t dev_nr = ATOMIC_INIT(-1);
  32. int hdpvr_debug;
  33. module_param(hdpvr_debug, int, S_IRUGO|S_IWUSR);
  34. MODULE_PARM_DESC(hdpvr_debug, "enable debugging output");
  35. static uint default_video_input = HDPVR_VIDEO_INPUTS;
  36. module_param(default_video_input, uint, S_IRUGO|S_IWUSR);
  37. MODULE_PARM_DESC(default_video_input, "default video input: 0=Component / "
  38. "1=S-Video / 2=Composite");
  39. static uint default_audio_input = HDPVR_AUDIO_INPUTS;
  40. module_param(default_audio_input, uint, S_IRUGO|S_IWUSR);
  41. MODULE_PARM_DESC(default_audio_input, "default audio input: 0=RCA back / "
  42. "1=RCA front / 2=S/PDIF");
  43. static int boost_audio;
  44. module_param(boost_audio, bool, S_IRUGO|S_IWUSR);
  45. MODULE_PARM_DESC(boost_audio, "boost the audio signal");
  46. /* table of devices that work with this driver */
  47. static struct usb_device_id hdpvr_table[] = {
  48. { USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID) },
  49. { USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID1) },
  50. { USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID2) },
  51. { USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID3) },
  52. { } /* Terminating entry */
  53. };
  54. MODULE_DEVICE_TABLE(usb, hdpvr_table);
  55. void hdpvr_delete(struct hdpvr_device *dev)
  56. {
  57. hdpvr_free_buffers(dev);
  58. if (dev->video_dev)
  59. video_device_release(dev->video_dev);
  60. usb_put_dev(dev->udev);
  61. }
  62. static void challenge(u8 *bytes)
  63. {
  64. u64 *i64P, tmp64;
  65. uint i, idx;
  66. for (idx = 0; idx < 32; ++idx) {
  67. if (idx & 0x3)
  68. bytes[(idx >> 3) + 3] = bytes[(idx >> 2) & 0x3];
  69. switch (idx & 0x3) {
  70. case 0x3:
  71. bytes[2] += bytes[3] * 4 + bytes[4] + bytes[5];
  72. bytes[4] += bytes[(idx & 0x1) * 2] * 9 + 9;
  73. break;
  74. case 0x1:
  75. bytes[0] *= 8;
  76. bytes[0] += 7*idx + 4;
  77. bytes[6] += bytes[3] * 3;
  78. break;
  79. case 0x0:
  80. bytes[3 - (idx >> 3)] = bytes[idx >> 2];
  81. bytes[5] += bytes[6] * 3;
  82. for (i = 0; i < 3; i++)
  83. bytes[3] *= bytes[3] + 1;
  84. break;
  85. case 0x2:
  86. for (i = 0; i < 3; i++)
  87. bytes[1] *= bytes[6] + 1;
  88. for (i = 0; i < 3; i++) {
  89. i64P = (u64 *)bytes;
  90. tmp64 = le64_to_cpup(i64P);
  91. tmp64 <<= bytes[7] & 0x0f;
  92. *i64P += cpu_to_le64(tmp64);
  93. }
  94. break;
  95. }
  96. }
  97. }
  98. /* try to init the device like the windows driver */
  99. static int device_authorization(struct hdpvr_device *dev)
  100. {
  101. int ret, retval = -ENOMEM;
  102. char request_type = 0x38, rcv_request = 0x81;
  103. char *response;
  104. #ifdef HDPVR_DEBUG
  105. size_t buf_size = 46;
  106. char *print_buf = kzalloc(5*buf_size+1, GFP_KERNEL);
  107. if (!print_buf) {
  108. v4l2_err(&dev->v4l2_dev, "Out of memory\n");
  109. return retval;
  110. }
  111. #endif
  112. mutex_lock(&dev->usbc_mutex);
  113. ret = usb_control_msg(dev->udev,
  114. usb_rcvctrlpipe(dev->udev, 0),
  115. rcv_request, 0x80 | request_type,
  116. 0x0400, 0x0003,
  117. dev->usbc_buf, 46,
  118. 10000);
  119. if (ret != 46) {
  120. v4l2_err(&dev->v4l2_dev,
  121. "unexpected answer of status request, len %d\n", ret);
  122. goto unlock;
  123. }
  124. #ifdef HDPVR_DEBUG
  125. else {
  126. hex_dump_to_buffer(dev->usbc_buf, 46, 16, 1, print_buf,
  127. 5*buf_size+1, 0);
  128. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  129. "Status request returned, len %d: %s\n",
  130. ret, print_buf);
  131. }
  132. #endif
  133. if (dev->usbc_buf[1] == HDPVR_FIRMWARE_VERSION) {
  134. dev->flags &= ~HDPVR_FLAG_AC3_CAP;
  135. } else if (dev->usbc_buf[1] == HDPVR_FIRMWARE_VERSION_AC3) {
  136. dev->flags |= HDPVR_FLAG_AC3_CAP;
  137. } else if (dev->usbc_buf[1] > HDPVR_FIRMWARE_VERSION_AC3) {
  138. v4l2_info(&dev->v4l2_dev, "untested firmware version 0x%x, "
  139. "the driver might not work\n", dev->usbc_buf[1]);
  140. dev->flags |= HDPVR_FLAG_AC3_CAP;
  141. } else {
  142. v4l2_err(&dev->v4l2_dev, "unknown firmware version 0x%x\n",
  143. dev->usbc_buf[1]);
  144. ret = -EINVAL;
  145. goto unlock;
  146. }
  147. response = dev->usbc_buf+38;
  148. #ifdef HDPVR_DEBUG
  149. hex_dump_to_buffer(response, 8, 16, 1, print_buf, 5*buf_size+1, 0);
  150. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, "challenge: %s\n",
  151. print_buf);
  152. #endif
  153. challenge(response);
  154. #ifdef HDPVR_DEBUG
  155. hex_dump_to_buffer(response, 8, 16, 1, print_buf, 5*buf_size+1, 0);
  156. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, " response: %s\n",
  157. print_buf);
  158. #endif
  159. msleep(100);
  160. ret = usb_control_msg(dev->udev,
  161. usb_sndctrlpipe(dev->udev, 0),
  162. 0xd1, 0x00 | request_type,
  163. 0x0000, 0x0000,
  164. response, 8,
  165. 10000);
  166. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  167. "magic request returned %d\n", ret);
  168. retval = ret != 8;
  169. unlock:
  170. mutex_unlock(&dev->usbc_mutex);
  171. return retval;
  172. }
  173. static int hdpvr_device_init(struct hdpvr_device *dev)
  174. {
  175. int ret;
  176. u8 *buf;
  177. struct hdpvr_video_info *vidinf;
  178. if (device_authorization(dev))
  179. return -EACCES;
  180. /* default options for init */
  181. hdpvr_set_options(dev);
  182. /* set filter options */
  183. mutex_lock(&dev->usbc_mutex);
  184. buf = dev->usbc_buf;
  185. buf[0] = 0x03; buf[1] = 0x03; buf[2] = 0x00; buf[3] = 0x00;
  186. ret = usb_control_msg(dev->udev,
  187. usb_sndctrlpipe(dev->udev, 0),
  188. 0x01, 0x38,
  189. CTRL_LOW_PASS_FILTER_VALUE, CTRL_DEFAULT_INDEX,
  190. buf, 4,
  191. 1000);
  192. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  193. "control request returned %d\n", ret);
  194. mutex_unlock(&dev->usbc_mutex);
  195. vidinf = get_video_info(dev);
  196. if (!vidinf)
  197. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  198. "no valid video signal or device init failed\n");
  199. else
  200. kfree(vidinf);
  201. /* enable fan and bling leds */
  202. mutex_lock(&dev->usbc_mutex);
  203. buf[0] = 0x1;
  204. ret = usb_control_msg(dev->udev,
  205. usb_sndctrlpipe(dev->udev, 0),
  206. 0xd4, 0x38, 0, 0, buf, 1,
  207. 1000);
  208. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  209. "control request returned %d\n", ret);
  210. /* boost analog audio */
  211. buf[0] = boost_audio;
  212. ret = usb_control_msg(dev->udev,
  213. usb_sndctrlpipe(dev->udev, 0),
  214. 0xd5, 0x38, 0, 0, buf, 1,
  215. 1000);
  216. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  217. "control request returned %d\n", ret);
  218. mutex_unlock(&dev->usbc_mutex);
  219. dev->status = STATUS_IDLE;
  220. return 0;
  221. }
  222. static const struct hdpvr_options hdpvr_default_options = {
  223. .video_std = HDPVR_60HZ,
  224. .video_input = HDPVR_COMPONENT,
  225. .audio_input = HDPVR_RCA_BACK,
  226. .bitrate = 65, /* 6 mbps */
  227. .peak_bitrate = 90, /* 9 mbps */
  228. .bitrate_mode = HDPVR_CONSTANT,
  229. .gop_mode = HDPVR_SIMPLE_IDR_GOP,
  230. .audio_codec = V4L2_MPEG_AUDIO_ENCODING_AAC,
  231. .brightness = 0x86,
  232. .contrast = 0x80,
  233. .hue = 0x80,
  234. .saturation = 0x80,
  235. .sharpness = 0x80,
  236. };
  237. static int hdpvr_probe(struct usb_interface *interface,
  238. const struct usb_device_id *id)
  239. {
  240. struct hdpvr_device *dev;
  241. struct usb_host_interface *iface_desc;
  242. struct usb_endpoint_descriptor *endpoint;
  243. size_t buffer_size;
  244. int i;
  245. int retval = -ENOMEM;
  246. /* allocate memory for our device state and initialize it */
  247. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  248. if (!dev) {
  249. err("Out of memory");
  250. goto error;
  251. }
  252. /* register v4l2_device early so it can be used for printks */
  253. if (v4l2_device_register(&interface->dev, &dev->v4l2_dev)) {
  254. err("v4l2_device_register failed");
  255. goto error;
  256. }
  257. mutex_init(&dev->io_mutex);
  258. mutex_init(&dev->i2c_mutex);
  259. mutex_init(&dev->usbc_mutex);
  260. dev->usbc_buf = kmalloc(64, GFP_KERNEL);
  261. if (!dev->usbc_buf) {
  262. v4l2_err(&dev->v4l2_dev, "Out of memory\n");
  263. goto error;
  264. }
  265. init_waitqueue_head(&dev->wait_buffer);
  266. init_waitqueue_head(&dev->wait_data);
  267. dev->workqueue = create_singlethread_workqueue("hdpvr_buffer");
  268. if (!dev->workqueue)
  269. goto error;
  270. /* init video transfer queues */
  271. INIT_LIST_HEAD(&dev->free_buff_list);
  272. INIT_LIST_HEAD(&dev->rec_buff_list);
  273. dev->options = hdpvr_default_options;
  274. if (default_video_input < HDPVR_VIDEO_INPUTS)
  275. dev->options.video_input = default_video_input;
  276. if (default_audio_input < HDPVR_AUDIO_INPUTS)
  277. dev->options.audio_input = default_audio_input;
  278. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  279. /* set up the endpoint information */
  280. /* use only the first bulk-in and bulk-out endpoints */
  281. iface_desc = interface->cur_altsetting;
  282. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  283. endpoint = &iface_desc->endpoint[i].desc;
  284. if (!dev->bulk_in_endpointAddr &&
  285. usb_endpoint_is_bulk_in(endpoint)) {
  286. /* USB interface description is buggy, reported max
  287. * packet size is 512 bytes, windows driver uses 8192 */
  288. buffer_size = 8192;
  289. dev->bulk_in_size = buffer_size;
  290. dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
  291. }
  292. }
  293. if (!dev->bulk_in_endpointAddr) {
  294. v4l2_err(&dev->v4l2_dev, "Could not find bulk-in endpoint\n");
  295. goto error;
  296. }
  297. /* init the device */
  298. if (hdpvr_device_init(dev)) {
  299. v4l2_err(&dev->v4l2_dev, "device init failed\n");
  300. goto error;
  301. }
  302. mutex_lock(&dev->io_mutex);
  303. if (hdpvr_alloc_buffers(dev, NUM_BUFFERS)) {
  304. mutex_unlock(&dev->io_mutex);
  305. v4l2_err(&dev->v4l2_dev,
  306. "allocating transfer buffers failed\n");
  307. goto error;
  308. }
  309. mutex_unlock(&dev->io_mutex);
  310. if (hdpvr_register_videodev(dev, &interface->dev,
  311. video_nr[atomic_inc_return(&dev_nr)])) {
  312. v4l2_err(&dev->v4l2_dev, "registering videodev failed\n");
  313. goto error;
  314. }
  315. #ifdef CONFIG_I2C
  316. /* until i2c is working properly */
  317. retval = 0; /* hdpvr_register_i2c_adapter(dev); */
  318. if (retval < 0) {
  319. v4l2_err(&dev->v4l2_dev, "registering i2c adapter failed\n");
  320. goto error;
  321. }
  322. #endif /* CONFIG_I2C */
  323. /* save our data pointer in this interface device */
  324. usb_set_intfdata(interface, dev);
  325. /* let the user know what node this device is now attached to */
  326. v4l2_info(&dev->v4l2_dev, "device now attached to %s\n",
  327. video_device_node_name(dev->video_dev));
  328. return 0;
  329. error:
  330. if (dev) {
  331. /* this frees allocated memory */
  332. hdpvr_delete(dev);
  333. }
  334. return retval;
  335. }
  336. static void hdpvr_disconnect(struct usb_interface *interface)
  337. {
  338. struct hdpvr_device *dev;
  339. dev = usb_get_intfdata(interface);
  340. usb_set_intfdata(interface, NULL);
  341. /* prevent more I/O from starting and stop any ongoing */
  342. mutex_lock(&dev->io_mutex);
  343. dev->status = STATUS_DISCONNECTED;
  344. v4l2_device_disconnect(&dev->v4l2_dev);
  345. video_unregister_device(dev->video_dev);
  346. wake_up_interruptible(&dev->wait_data);
  347. wake_up_interruptible(&dev->wait_buffer);
  348. mutex_unlock(&dev->io_mutex);
  349. msleep(100);
  350. flush_workqueue(dev->workqueue);
  351. mutex_lock(&dev->io_mutex);
  352. hdpvr_cancel_queue(dev);
  353. destroy_workqueue(dev->workqueue);
  354. mutex_unlock(&dev->io_mutex);
  355. /* deregister I2C adapter */
  356. #ifdef CONFIG_I2C
  357. mutex_lock(&dev->i2c_mutex);
  358. if (dev->i2c_adapter)
  359. i2c_del_adapter(dev->i2c_adapter);
  360. kfree(dev->i2c_adapter);
  361. dev->i2c_adapter = NULL;
  362. mutex_unlock(&dev->i2c_mutex);
  363. #endif /* CONFIG_I2C */
  364. atomic_dec(&dev_nr);
  365. v4l2_info(&dev->v4l2_dev, "device %s disconnected\n",
  366. video_device_node_name(dev->video_dev));
  367. v4l2_device_unregister(&dev->v4l2_dev);
  368. kfree(dev->usbc_buf);
  369. kfree(dev);
  370. }
  371. static struct usb_driver hdpvr_usb_driver = {
  372. .name = "hdpvr",
  373. .probe = hdpvr_probe,
  374. .disconnect = hdpvr_disconnect,
  375. .id_table = hdpvr_table,
  376. };
  377. static int __init hdpvr_init(void)
  378. {
  379. int result;
  380. /* register this driver with the USB subsystem */
  381. result = usb_register(&hdpvr_usb_driver);
  382. if (result)
  383. err("usb_register failed. Error number %d", result);
  384. return result;
  385. }
  386. static void __exit hdpvr_exit(void)
  387. {
  388. /* deregister this driver with the USB subsystem */
  389. usb_deregister(&hdpvr_usb_driver);
  390. }
  391. module_init(hdpvr_init);
  392. module_exit(hdpvr_exit);
  393. MODULE_LICENSE("GPL");
  394. MODULE_AUTHOR("Janne Grunau");
  395. MODULE_DESCRIPTION("Hauppauge HD PVR driver");