PageRenderTime 24ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

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