hdpvr-core.c 11 KB

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