hdpvr-core.c 12 KB

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