hdpvr-video.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * Hauppauge HD PVR USB driver - video 4 linux 2 interface
  3. *
  4. * Copyright (C) 2008 Janne Grunau (j@jannau.net)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/kconfig.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/usb.h>
  19. #include <linux/mutex.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/videodev2.h>
  22. #include <linux/v4l2-dv-timings.h>
  23. #include <media/v4l2-dev.h>
  24. #include <media/v4l2-common.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/v4l2-event.h>
  27. #include "hdpvr.h"
  28. #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
  29. #define print_buffer_status() { \
  30. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
  31. "%s:%d buffer stat: %d free, %d proc\n", \
  32. __func__, __LINE__, \
  33. list_size(&dev->free_buff_list), \
  34. list_size(&dev->rec_buff_list)); }
  35. static const struct v4l2_dv_timings hdpvr_dv_timings[] = {
  36. V4L2_DV_BT_CEA_720X480I59_94,
  37. V4L2_DV_BT_CEA_720X576I50,
  38. V4L2_DV_BT_CEA_720X480P59_94,
  39. V4L2_DV_BT_CEA_720X576P50,
  40. V4L2_DV_BT_CEA_1280X720P50,
  41. V4L2_DV_BT_CEA_1280X720P60,
  42. V4L2_DV_BT_CEA_1920X1080I50,
  43. V4L2_DV_BT_CEA_1920X1080I60,
  44. };
  45. /* Use 480i59 as the default timings */
  46. #define HDPVR_DEF_DV_TIMINGS_IDX (0)
  47. struct hdpvr_fh {
  48. struct v4l2_fh fh;
  49. bool legacy_mode;
  50. };
  51. static uint list_size(struct list_head *list)
  52. {
  53. struct list_head *tmp;
  54. uint count = 0;
  55. list_for_each(tmp, list) {
  56. count++;
  57. }
  58. return count;
  59. }
  60. /*=========================================================================*/
  61. /* urb callback */
  62. static void hdpvr_read_bulk_callback(struct urb *urb)
  63. {
  64. struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
  65. struct hdpvr_device *dev = buf->dev;
  66. /* marking buffer as received and wake waiting */
  67. buf->status = BUFSTAT_READY;
  68. wake_up_interruptible(&dev->wait_data);
  69. }
  70. /*=========================================================================*/
  71. /* bufffer bits */
  72. /* function expects dev->io_mutex to be hold by caller */
  73. int hdpvr_cancel_queue(struct hdpvr_device *dev)
  74. {
  75. struct hdpvr_buffer *buf;
  76. list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
  77. usb_kill_urb(buf->urb);
  78. buf->status = BUFSTAT_AVAILABLE;
  79. }
  80. list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
  81. return 0;
  82. }
  83. static int hdpvr_free_queue(struct list_head *q)
  84. {
  85. struct list_head *tmp;
  86. struct list_head *p;
  87. struct hdpvr_buffer *buf;
  88. struct urb *urb;
  89. for (p = q->next; p != q;) {
  90. buf = list_entry(p, struct hdpvr_buffer, buff_list);
  91. urb = buf->urb;
  92. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  93. urb->transfer_buffer, urb->transfer_dma);
  94. usb_free_urb(urb);
  95. tmp = p->next;
  96. list_del(p);
  97. kfree(buf);
  98. p = tmp;
  99. }
  100. return 0;
  101. }
  102. /* function expects dev->io_mutex to be hold by caller */
  103. int hdpvr_free_buffers(struct hdpvr_device *dev)
  104. {
  105. hdpvr_cancel_queue(dev);
  106. hdpvr_free_queue(&dev->free_buff_list);
  107. hdpvr_free_queue(&dev->rec_buff_list);
  108. return 0;
  109. }
  110. /* function expects dev->io_mutex to be hold by caller */
  111. int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
  112. {
  113. uint i;
  114. int retval = -ENOMEM;
  115. u8 *mem;
  116. struct hdpvr_buffer *buf;
  117. struct urb *urb;
  118. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  119. "allocating %u buffers\n", count);
  120. for (i = 0; i < count; i++) {
  121. buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
  122. if (!buf) {
  123. v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
  124. goto exit;
  125. }
  126. buf->dev = dev;
  127. urb = usb_alloc_urb(0, GFP_KERNEL);
  128. if (!urb) {
  129. v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
  130. goto exit_urb;
  131. }
  132. buf->urb = urb;
  133. mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
  134. &urb->transfer_dma);
  135. if (!mem) {
  136. v4l2_err(&dev->v4l2_dev,
  137. "cannot allocate usb transfer buffer\n");
  138. goto exit_urb_buffer;
  139. }
  140. usb_fill_bulk_urb(buf->urb, dev->udev,
  141. usb_rcvbulkpipe(dev->udev,
  142. dev->bulk_in_endpointAddr),
  143. mem, dev->bulk_in_size,
  144. hdpvr_read_bulk_callback, buf);
  145. buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  146. buf->status = BUFSTAT_AVAILABLE;
  147. list_add_tail(&buf->buff_list, &dev->free_buff_list);
  148. }
  149. return 0;
  150. exit_urb_buffer:
  151. usb_free_urb(urb);
  152. exit_urb:
  153. kfree(buf);
  154. exit:
  155. hdpvr_free_buffers(dev);
  156. return retval;
  157. }
  158. static int hdpvr_submit_buffers(struct hdpvr_device *dev)
  159. {
  160. struct hdpvr_buffer *buf;
  161. struct urb *urb;
  162. int ret = 0, err_count = 0;
  163. mutex_lock(&dev->io_mutex);
  164. while (dev->status == STATUS_STREAMING &&
  165. !list_empty(&dev->free_buff_list)) {
  166. buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
  167. buff_list);
  168. if (buf->status != BUFSTAT_AVAILABLE) {
  169. v4l2_err(&dev->v4l2_dev,
  170. "buffer not marked as available\n");
  171. ret = -EFAULT;
  172. goto err;
  173. }
  174. urb = buf->urb;
  175. urb->status = 0;
  176. urb->actual_length = 0;
  177. ret = usb_submit_urb(urb, GFP_KERNEL);
  178. if (ret) {
  179. v4l2_err(&dev->v4l2_dev,
  180. "usb_submit_urb in %s returned %d\n",
  181. __func__, ret);
  182. if (++err_count > 2)
  183. break;
  184. continue;
  185. }
  186. buf->status = BUFSTAT_INPROGRESS;
  187. list_move_tail(&buf->buff_list, &dev->rec_buff_list);
  188. }
  189. err:
  190. print_buffer_status();
  191. mutex_unlock(&dev->io_mutex);
  192. return ret;
  193. }
  194. static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
  195. {
  196. struct hdpvr_buffer *buf;
  197. mutex_lock(&dev->io_mutex);
  198. if (list_empty(&dev->rec_buff_list)) {
  199. mutex_unlock(&dev->io_mutex);
  200. return NULL;
  201. }
  202. buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
  203. buff_list);
  204. mutex_unlock(&dev->io_mutex);
  205. return buf;
  206. }
  207. static void hdpvr_transmit_buffers(struct work_struct *work)
  208. {
  209. struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
  210. worker);
  211. while (dev->status == STATUS_STREAMING) {
  212. if (hdpvr_submit_buffers(dev)) {
  213. v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
  214. goto error;
  215. }
  216. if (wait_event_interruptible(dev->wait_buffer,
  217. !list_empty(&dev->free_buff_list) ||
  218. dev->status != STATUS_STREAMING))
  219. goto error;
  220. }
  221. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  222. "transmit worker exited\n");
  223. return;
  224. error:
  225. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  226. "transmit buffers errored\n");
  227. dev->status = STATUS_ERROR;
  228. }
  229. /* function expects dev->io_mutex to be hold by caller */
  230. static int hdpvr_start_streaming(struct hdpvr_device *dev)
  231. {
  232. int ret;
  233. struct hdpvr_video_info vidinf;
  234. if (dev->status == STATUS_STREAMING)
  235. return 0;
  236. else if (dev->status != STATUS_IDLE)
  237. return -EAGAIN;
  238. ret = get_video_info(dev, &vidinf);
  239. if (!ret) {
  240. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  241. "video signal: %dx%d@%dhz\n", vidinf.width,
  242. vidinf.height, vidinf.fps);
  243. /* start streaming 2 request */
  244. ret = usb_control_msg(dev->udev,
  245. usb_sndctrlpipe(dev->udev, 0),
  246. 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
  247. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  248. "encoder start control request returned %d\n", ret);
  249. if (ret < 0)
  250. return ret;
  251. ret = hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
  252. if (ret)
  253. return ret;
  254. dev->status = STATUS_STREAMING;
  255. INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
  256. queue_work(dev->workqueue, &dev->worker);
  257. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  258. "streaming started\n");
  259. return 0;
  260. }
  261. msleep(250);
  262. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  263. "no video signal at input %d\n", dev->options.video_input);
  264. return -EAGAIN;
  265. }
  266. /* function expects dev->io_mutex to be hold by caller */
  267. static int hdpvr_stop_streaming(struct hdpvr_device *dev)
  268. {
  269. int actual_length;
  270. uint c = 0;
  271. u8 *buf;
  272. if (dev->status == STATUS_IDLE)
  273. return 0;
  274. else if (dev->status != STATUS_STREAMING)
  275. return -EAGAIN;
  276. buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
  277. if (!buf)
  278. v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
  279. "for emptying the internal device buffer. "
  280. "Next capture start will be slow\n");
  281. dev->status = STATUS_SHUTTING_DOWN;
  282. hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
  283. mutex_unlock(&dev->io_mutex);
  284. wake_up_interruptible(&dev->wait_buffer);
  285. msleep(50);
  286. flush_workqueue(dev->workqueue);
  287. mutex_lock(&dev->io_mutex);
  288. /* kill the still outstanding urbs */
  289. hdpvr_cancel_queue(dev);
  290. /* emptying the device buffer beforeshutting it down */
  291. while (buf && ++c < 500 &&
  292. !usb_bulk_msg(dev->udev,
  293. usb_rcvbulkpipe(dev->udev,
  294. dev->bulk_in_endpointAddr),
  295. buf, dev->bulk_in_size, &actual_length,
  296. BULK_URB_TIMEOUT)) {
  297. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  298. "%2d: got %d bytes\n", c, actual_length);
  299. }
  300. kfree(buf);
  301. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  302. "used %d urbs to empty device buffers\n", c-1);
  303. msleep(10);
  304. dev->status = STATUS_IDLE;
  305. return 0;
  306. }
  307. /*=======================================================================*/
  308. /*
  309. * video 4 linux 2 file operations
  310. */
  311. static int hdpvr_open(struct file *file)
  312. {
  313. struct hdpvr_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  314. if (fh == NULL)
  315. return -ENOMEM;
  316. fh->legacy_mode = true;
  317. v4l2_fh_init(&fh->fh, video_devdata(file));
  318. v4l2_fh_add(&fh->fh);
  319. file->private_data = fh;
  320. return 0;
  321. }
  322. static int hdpvr_release(struct file *file)
  323. {
  324. struct hdpvr_device *dev = video_drvdata(file);
  325. mutex_lock(&dev->io_mutex);
  326. if (file->private_data == dev->owner) {
  327. hdpvr_stop_streaming(dev);
  328. dev->owner = NULL;
  329. }
  330. mutex_unlock(&dev->io_mutex);
  331. return v4l2_fh_release(file);
  332. }
  333. /*
  334. * hdpvr_v4l2_read()
  335. * will allocate buffers when called for the first time
  336. */
  337. static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
  338. loff_t *pos)
  339. {
  340. struct hdpvr_device *dev = video_drvdata(file);
  341. struct hdpvr_buffer *buf = NULL;
  342. struct urb *urb;
  343. unsigned int ret = 0;
  344. int rem, cnt;
  345. if (*pos)
  346. return -ESPIPE;
  347. mutex_lock(&dev->io_mutex);
  348. if (dev->status == STATUS_IDLE) {
  349. if (hdpvr_start_streaming(dev)) {
  350. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  351. "start_streaming failed\n");
  352. ret = -EIO;
  353. msleep(200);
  354. dev->status = STATUS_IDLE;
  355. mutex_unlock(&dev->io_mutex);
  356. goto err;
  357. }
  358. dev->owner = file->private_data;
  359. print_buffer_status();
  360. }
  361. mutex_unlock(&dev->io_mutex);
  362. /* wait for the first buffer */
  363. if (!(file->f_flags & O_NONBLOCK)) {
  364. if (wait_event_interruptible(dev->wait_data,
  365. hdpvr_get_next_buffer(dev)))
  366. return -ERESTARTSYS;
  367. }
  368. buf = hdpvr_get_next_buffer(dev);
  369. while (count > 0 && buf) {
  370. if (buf->status != BUFSTAT_READY &&
  371. dev->status != STATUS_DISCONNECTED) {
  372. /* return nonblocking */
  373. if (file->f_flags & O_NONBLOCK) {
  374. if (!ret)
  375. ret = -EAGAIN;
  376. goto err;
  377. }
  378. if (wait_event_interruptible(dev->wait_data,
  379. buf->status == BUFSTAT_READY)) {
  380. ret = -ERESTARTSYS;
  381. goto err;
  382. }
  383. }
  384. if (buf->status != BUFSTAT_READY)
  385. break;
  386. /* set remaining bytes to copy */
  387. urb = buf->urb;
  388. rem = urb->actual_length - buf->pos;
  389. cnt = rem > count ? count : rem;
  390. if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
  391. cnt)) {
  392. v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
  393. if (!ret)
  394. ret = -EFAULT;
  395. goto err;
  396. }
  397. buf->pos += cnt;
  398. count -= cnt;
  399. buffer += cnt;
  400. ret += cnt;
  401. /* finished, take next buffer */
  402. if (buf->pos == urb->actual_length) {
  403. mutex_lock(&dev->io_mutex);
  404. buf->pos = 0;
  405. buf->status = BUFSTAT_AVAILABLE;
  406. list_move_tail(&buf->buff_list, &dev->free_buff_list);
  407. print_buffer_status();
  408. mutex_unlock(&dev->io_mutex);
  409. wake_up_interruptible(&dev->wait_buffer);
  410. buf = hdpvr_get_next_buffer(dev);
  411. }
  412. }
  413. err:
  414. if (!ret && !buf)
  415. ret = -EAGAIN;
  416. return ret;
  417. }
  418. static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
  419. {
  420. unsigned long req_events = poll_requested_events(wait);
  421. struct hdpvr_buffer *buf = NULL;
  422. struct hdpvr_device *dev = video_drvdata(filp);
  423. unsigned int mask = v4l2_ctrl_poll(filp, wait);
  424. if (!(req_events & (POLLIN | POLLRDNORM)))
  425. return mask;
  426. mutex_lock(&dev->io_mutex);
  427. if (dev->status == STATUS_IDLE) {
  428. if (hdpvr_start_streaming(dev)) {
  429. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  430. "start_streaming failed\n");
  431. dev->status = STATUS_IDLE;
  432. } else {
  433. dev->owner = filp->private_data;
  434. }
  435. print_buffer_status();
  436. }
  437. mutex_unlock(&dev->io_mutex);
  438. buf = hdpvr_get_next_buffer(dev);
  439. /* only wait if no data is available */
  440. if (!buf || buf->status != BUFSTAT_READY) {
  441. poll_wait(filp, &dev->wait_data, wait);
  442. buf = hdpvr_get_next_buffer(dev);
  443. }
  444. if (buf && buf->status == BUFSTAT_READY)
  445. mask |= POLLIN | POLLRDNORM;
  446. return mask;
  447. }
  448. static const struct v4l2_file_operations hdpvr_fops = {
  449. .owner = THIS_MODULE,
  450. .open = hdpvr_open,
  451. .release = hdpvr_release,
  452. .read = hdpvr_read,
  453. .poll = hdpvr_poll,
  454. .unlocked_ioctl = video_ioctl2,
  455. };
  456. /*=======================================================================*/
  457. /*
  458. * V4L2 ioctl handling
  459. */
  460. static int vidioc_querycap(struct file *file, void *priv,
  461. struct v4l2_capability *cap)
  462. {
  463. struct hdpvr_device *dev = video_drvdata(file);
  464. strcpy(cap->driver, "hdpvr");
  465. strcpy(cap->card, "Hauppauge HD PVR");
  466. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  467. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO |
  468. V4L2_CAP_READWRITE;
  469. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  470. return 0;
  471. }
  472. static int vidioc_s_std(struct file *file, void *_fh,
  473. v4l2_std_id std)
  474. {
  475. struct hdpvr_device *dev = video_drvdata(file);
  476. struct hdpvr_fh *fh = _fh;
  477. u8 std_type = 1;
  478. if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
  479. return -ENODATA;
  480. if (dev->status != STATUS_IDLE)
  481. return -EBUSY;
  482. if (std & V4L2_STD_525_60)
  483. std_type = 0;
  484. dev->cur_std = std;
  485. dev->width = 720;
  486. dev->height = std_type ? 576 : 480;
  487. return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
  488. }
  489. static int vidioc_g_std(struct file *file, void *_fh,
  490. v4l2_std_id *std)
  491. {
  492. struct hdpvr_device *dev = video_drvdata(file);
  493. struct hdpvr_fh *fh = _fh;
  494. if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
  495. return -ENODATA;
  496. *std = dev->cur_std;
  497. return 0;
  498. }
  499. static int vidioc_querystd(struct file *file, void *_fh, v4l2_std_id *a)
  500. {
  501. struct hdpvr_device *dev = video_drvdata(file);
  502. struct hdpvr_video_info vid_info;
  503. struct hdpvr_fh *fh = _fh;
  504. int ret;
  505. *a = V4L2_STD_ALL;
  506. if (dev->options.video_input == HDPVR_COMPONENT)
  507. return fh->legacy_mode ? 0 : -ENODATA;
  508. ret = get_video_info(dev, &vid_info);
  509. if (ret)
  510. return 0;
  511. if (vid_info.width == 720 &&
  512. (vid_info.height == 480 || vid_info.height == 576)) {
  513. *a = (vid_info.height == 480) ?
  514. V4L2_STD_525_60 : V4L2_STD_625_50;
  515. }
  516. return 0;
  517. }
  518. static int vidioc_s_dv_timings(struct file *file, void *_fh,
  519. struct v4l2_dv_timings *timings)
  520. {
  521. struct hdpvr_device *dev = video_drvdata(file);
  522. struct hdpvr_fh *fh = _fh;
  523. int i;
  524. fh->legacy_mode = false;
  525. if (dev->options.video_input)
  526. return -ENODATA;
  527. if (dev->status != STATUS_IDLE)
  528. return -EBUSY;
  529. for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++)
  530. if (v4l_match_dv_timings(timings, hdpvr_dv_timings + i, 0))
  531. break;
  532. if (i == ARRAY_SIZE(hdpvr_dv_timings))
  533. return -EINVAL;
  534. dev->cur_dv_timings = hdpvr_dv_timings[i];
  535. dev->width = hdpvr_dv_timings[i].bt.width;
  536. dev->height = hdpvr_dv_timings[i].bt.height;
  537. return 0;
  538. }
  539. static int vidioc_g_dv_timings(struct file *file, void *_fh,
  540. struct v4l2_dv_timings *timings)
  541. {
  542. struct hdpvr_device *dev = video_drvdata(file);
  543. struct hdpvr_fh *fh = _fh;
  544. fh->legacy_mode = false;
  545. if (dev->options.video_input)
  546. return -ENODATA;
  547. *timings = dev->cur_dv_timings;
  548. return 0;
  549. }
  550. static int vidioc_query_dv_timings(struct file *file, void *_fh,
  551. struct v4l2_dv_timings *timings)
  552. {
  553. struct hdpvr_device *dev = video_drvdata(file);
  554. struct hdpvr_fh *fh = _fh;
  555. struct hdpvr_video_info vid_info;
  556. bool interlaced;
  557. int ret = 0;
  558. int i;
  559. fh->legacy_mode = false;
  560. if (dev->options.video_input)
  561. return -ENODATA;
  562. ret = get_video_info(dev, &vid_info);
  563. if (ret)
  564. return -ENOLCK;
  565. interlaced = vid_info.fps <= 30;
  566. for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) {
  567. const struct v4l2_bt_timings *bt = &hdpvr_dv_timings[i].bt;
  568. unsigned hsize;
  569. unsigned vsize;
  570. unsigned fps;
  571. hsize = bt->hfrontporch + bt->hsync + bt->hbackporch + bt->width;
  572. vsize = bt->vfrontporch + bt->vsync + bt->vbackporch +
  573. bt->il_vfrontporch + bt->il_vsync + bt->il_vbackporch +
  574. bt->height;
  575. fps = (unsigned)bt->pixelclock / (hsize * vsize);
  576. if (bt->width != vid_info.width ||
  577. bt->height != vid_info.height ||
  578. bt->interlaced != interlaced ||
  579. (fps != vid_info.fps && fps + 1 != vid_info.fps))
  580. continue;
  581. *timings = hdpvr_dv_timings[i];
  582. break;
  583. }
  584. if (i == ARRAY_SIZE(hdpvr_dv_timings))
  585. ret = -ERANGE;
  586. return ret;
  587. }
  588. static int vidioc_enum_dv_timings(struct file *file, void *_fh,
  589. struct v4l2_enum_dv_timings *timings)
  590. {
  591. struct hdpvr_device *dev = video_drvdata(file);
  592. struct hdpvr_fh *fh = _fh;
  593. fh->legacy_mode = false;
  594. memset(timings->reserved, 0, sizeof(timings->reserved));
  595. if (dev->options.video_input)
  596. return -ENODATA;
  597. if (timings->index >= ARRAY_SIZE(hdpvr_dv_timings))
  598. return -EINVAL;
  599. timings->timings = hdpvr_dv_timings[timings->index];
  600. return 0;
  601. }
  602. static int vidioc_dv_timings_cap(struct file *file, void *_fh,
  603. struct v4l2_dv_timings_cap *cap)
  604. {
  605. struct hdpvr_device *dev = video_drvdata(file);
  606. struct hdpvr_fh *fh = _fh;
  607. fh->legacy_mode = false;
  608. if (dev->options.video_input)
  609. return -ENODATA;
  610. cap->type = V4L2_DV_BT_656_1120;
  611. cap->bt.min_width = 720;
  612. cap->bt.max_width = 1920;
  613. cap->bt.min_height = 480;
  614. cap->bt.max_height = 1080;
  615. cap->bt.min_pixelclock = 27000000;
  616. cap->bt.max_pixelclock = 74250000;
  617. cap->bt.standards = V4L2_DV_BT_STD_CEA861;
  618. cap->bt.capabilities = V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE;
  619. return 0;
  620. }
  621. static const char *iname[] = {
  622. [HDPVR_COMPONENT] = "Component",
  623. [HDPVR_SVIDEO] = "S-Video",
  624. [HDPVR_COMPOSITE] = "Composite",
  625. };
  626. static int vidioc_enum_input(struct file *file, void *_fh, struct v4l2_input *i)
  627. {
  628. unsigned int n;
  629. n = i->index;
  630. if (n >= HDPVR_VIDEO_INPUTS)
  631. return -EINVAL;
  632. i->type = V4L2_INPUT_TYPE_CAMERA;
  633. strncpy(i->name, iname[n], sizeof(i->name) - 1);
  634. i->name[sizeof(i->name) - 1] = '\0';
  635. i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
  636. i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
  637. i->std = n ? V4L2_STD_ALL : 0;
  638. return 0;
  639. }
  640. static int vidioc_s_input(struct file *file, void *_fh,
  641. unsigned int index)
  642. {
  643. struct hdpvr_device *dev = video_drvdata(file);
  644. int retval;
  645. if (index >= HDPVR_VIDEO_INPUTS)
  646. return -EINVAL;
  647. if (dev->status != STATUS_IDLE)
  648. return -EBUSY;
  649. retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
  650. if (!retval) {
  651. dev->options.video_input = index;
  652. /*
  653. * Unfortunately gstreamer calls ENUMSTD and bails out if it
  654. * won't find any formats, even though component input is
  655. * selected. This means that we have to leave tvnorms at
  656. * V4L2_STD_ALL. We cannot use the 'legacy' trick since
  657. * tvnorms is set at the device node level and not at the
  658. * filehandle level.
  659. *
  660. * Comment this out for now, but if the legacy mode can be
  661. * removed in the future, then this code should be enabled
  662. * again.
  663. dev->video_dev->tvnorms =
  664. (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
  665. */
  666. }
  667. return retval;
  668. }
  669. static int vidioc_g_input(struct file *file, void *private_data,
  670. unsigned int *index)
  671. {
  672. struct hdpvr_device *dev = video_drvdata(file);
  673. *index = dev->options.video_input;
  674. return 0;
  675. }
  676. static const char *audio_iname[] = {
  677. [HDPVR_RCA_FRONT] = "RCA front",
  678. [HDPVR_RCA_BACK] = "RCA back",
  679. [HDPVR_SPDIF] = "SPDIF",
  680. };
  681. static int vidioc_enumaudio(struct file *file, void *priv,
  682. struct v4l2_audio *audio)
  683. {
  684. unsigned int n;
  685. n = audio->index;
  686. if (n >= HDPVR_AUDIO_INPUTS)
  687. return -EINVAL;
  688. audio->capability = V4L2_AUDCAP_STEREO;
  689. strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
  690. audio->name[sizeof(audio->name) - 1] = '\0';
  691. return 0;
  692. }
  693. static int vidioc_s_audio(struct file *file, void *private_data,
  694. const struct v4l2_audio *audio)
  695. {
  696. struct hdpvr_device *dev = video_drvdata(file);
  697. int retval;
  698. if (audio->index >= HDPVR_AUDIO_INPUTS)
  699. return -EINVAL;
  700. if (dev->status != STATUS_IDLE)
  701. return -EBUSY;
  702. retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
  703. if (!retval)
  704. dev->options.audio_input = audio->index;
  705. return retval;
  706. }
  707. static int vidioc_g_audio(struct file *file, void *private_data,
  708. struct v4l2_audio *audio)
  709. {
  710. struct hdpvr_device *dev = video_drvdata(file);
  711. audio->index = dev->options.audio_input;
  712. audio->capability = V4L2_AUDCAP_STEREO;
  713. strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
  714. audio->name[sizeof(audio->name) - 1] = '\0';
  715. return 0;
  716. }
  717. static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
  718. {
  719. struct hdpvr_device *dev =
  720. container_of(ctrl->handler, struct hdpvr_device, hdl);
  721. switch (ctrl->id) {
  722. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  723. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  724. dev->video_bitrate->val >= dev->video_bitrate_peak->val)
  725. dev->video_bitrate_peak->val =
  726. dev->video_bitrate->val + 100000;
  727. break;
  728. }
  729. return 0;
  730. }
  731. static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
  732. {
  733. struct hdpvr_device *dev =
  734. container_of(ctrl->handler, struct hdpvr_device, hdl);
  735. struct hdpvr_options *opt = &dev->options;
  736. int ret = -EINVAL;
  737. switch (ctrl->id) {
  738. case V4L2_CID_BRIGHTNESS:
  739. ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
  740. if (ret)
  741. break;
  742. dev->options.brightness = ctrl->val;
  743. return 0;
  744. case V4L2_CID_CONTRAST:
  745. ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
  746. if (ret)
  747. break;
  748. dev->options.contrast = ctrl->val;
  749. return 0;
  750. case V4L2_CID_SATURATION:
  751. ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
  752. if (ret)
  753. break;
  754. dev->options.saturation = ctrl->val;
  755. return 0;
  756. case V4L2_CID_HUE:
  757. ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
  758. if (ret)
  759. break;
  760. dev->options.hue = ctrl->val;
  761. return 0;
  762. case V4L2_CID_SHARPNESS:
  763. ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
  764. if (ret)
  765. break;
  766. dev->options.sharpness = ctrl->val;
  767. return 0;
  768. case V4L2_CID_MPEG_AUDIO_ENCODING:
  769. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  770. opt->audio_codec = ctrl->val;
  771. return hdpvr_set_audio(dev, opt->audio_input,
  772. opt->audio_codec);
  773. }
  774. return 0;
  775. case V4L2_CID_MPEG_VIDEO_ENCODING:
  776. return 0;
  777. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  778. /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
  779. /* opt->gop_mode |= 0x2; */
  780. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  781. /* opt->gop_mode); */
  782. /* } */
  783. /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
  784. /* opt->gop_mode &= ~0x2; */
  785. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  786. /* opt->gop_mode); */
  787. /* } */
  788. /* break; */
  789. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
  790. uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
  791. uint bitrate = dev->video_bitrate->val / 100000;
  792. if (ctrl->is_new) {
  793. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
  794. opt->bitrate_mode = HDPVR_CONSTANT;
  795. else
  796. opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
  797. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  798. opt->bitrate_mode);
  799. v4l2_ctrl_activate(dev->video_bitrate_peak,
  800. ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  801. }
  802. if (dev->video_bitrate_peak->is_new ||
  803. dev->video_bitrate->is_new) {
  804. opt->bitrate = bitrate;
  805. opt->peak_bitrate = peak_bitrate;
  806. hdpvr_set_bitrate(dev);
  807. }
  808. return 0;
  809. }
  810. case V4L2_CID_MPEG_STREAM_TYPE:
  811. return 0;
  812. default:
  813. break;
  814. }
  815. return ret;
  816. }
  817. static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
  818. struct v4l2_fmtdesc *f)
  819. {
  820. if (f->index != 0)
  821. return -EINVAL;
  822. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  823. strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
  824. f->pixelformat = V4L2_PIX_FMT_MPEG;
  825. return 0;
  826. }
  827. static int vidioc_g_fmt_vid_cap(struct file *file, void *_fh,
  828. struct v4l2_format *f)
  829. {
  830. struct hdpvr_device *dev = video_drvdata(file);
  831. struct hdpvr_fh *fh = _fh;
  832. int ret;
  833. /*
  834. * The original driver would always returns the current detected
  835. * resolution as the format (and EFAULT if it couldn't be detected).
  836. * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
  837. * better way of doing this, but to stay compatible with existing
  838. * applications we assume legacy mode every time an application opens
  839. * the device. Only if one of the new DV_TIMINGS ioctls is called
  840. * will the filehandle go into 'normal' mode where g_fmt returns the
  841. * last set format.
  842. */
  843. if (fh->legacy_mode) {
  844. struct hdpvr_video_info vid_info;
  845. ret = get_video_info(dev, &vid_info);
  846. if (ret)
  847. return -EFAULT;
  848. f->fmt.pix.width = vid_info.width;
  849. f->fmt.pix.height = vid_info.height;
  850. } else {
  851. f->fmt.pix.width = dev->width;
  852. f->fmt.pix.height = dev->height;
  853. }
  854. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  855. f->fmt.pix.sizeimage = dev->bulk_in_size;
  856. f->fmt.pix.bytesperline = 0;
  857. f->fmt.pix.priv = 0;
  858. if (f->fmt.pix.width == 720) {
  859. /* SDTV formats */
  860. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  861. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  862. } else {
  863. /* HDTV formats */
  864. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE240M;
  865. f->fmt.pix.field = V4L2_FIELD_NONE;
  866. }
  867. return 0;
  868. }
  869. static int vidioc_encoder_cmd(struct file *filp, void *priv,
  870. struct v4l2_encoder_cmd *a)
  871. {
  872. struct hdpvr_device *dev = video_drvdata(filp);
  873. int res = 0;
  874. mutex_lock(&dev->io_mutex);
  875. a->flags = 0;
  876. switch (a->cmd) {
  877. case V4L2_ENC_CMD_START:
  878. if (dev->owner && filp->private_data != dev->owner) {
  879. res = -EBUSY;
  880. break;
  881. }
  882. if (dev->status == STATUS_STREAMING)
  883. break;
  884. res = hdpvr_start_streaming(dev);
  885. if (!res)
  886. dev->owner = filp->private_data;
  887. else
  888. dev->status = STATUS_IDLE;
  889. break;
  890. case V4L2_ENC_CMD_STOP:
  891. if (dev->owner && filp->private_data != dev->owner) {
  892. res = -EBUSY;
  893. break;
  894. }
  895. if (dev->status == STATUS_IDLE)
  896. break;
  897. res = hdpvr_stop_streaming(dev);
  898. if (!res)
  899. dev->owner = NULL;
  900. break;
  901. default:
  902. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  903. "Unsupported encoder cmd %d\n", a->cmd);
  904. res = -EINVAL;
  905. break;
  906. }
  907. mutex_unlock(&dev->io_mutex);
  908. return res;
  909. }
  910. static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
  911. struct v4l2_encoder_cmd *a)
  912. {
  913. a->flags = 0;
  914. switch (a->cmd) {
  915. case V4L2_ENC_CMD_START:
  916. case V4L2_ENC_CMD_STOP:
  917. return 0;
  918. default:
  919. return -EINVAL;
  920. }
  921. }
  922. static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
  923. .vidioc_querycap = vidioc_querycap,
  924. .vidioc_s_std = vidioc_s_std,
  925. .vidioc_g_std = vidioc_g_std,
  926. .vidioc_querystd = vidioc_querystd,
  927. .vidioc_s_dv_timings = vidioc_s_dv_timings,
  928. .vidioc_g_dv_timings = vidioc_g_dv_timings,
  929. .vidioc_query_dv_timings= vidioc_query_dv_timings,
  930. .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
  931. .vidioc_dv_timings_cap = vidioc_dv_timings_cap,
  932. .vidioc_enum_input = vidioc_enum_input,
  933. .vidioc_g_input = vidioc_g_input,
  934. .vidioc_s_input = vidioc_s_input,
  935. .vidioc_enumaudio = vidioc_enumaudio,
  936. .vidioc_g_audio = vidioc_g_audio,
  937. .vidioc_s_audio = vidioc_s_audio,
  938. .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
  939. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  940. .vidioc_s_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  941. .vidioc_try_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  942. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  943. .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
  944. .vidioc_log_status = v4l2_ctrl_log_status,
  945. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  946. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  947. };
  948. static void hdpvr_device_release(struct video_device *vdev)
  949. {
  950. struct hdpvr_device *dev = video_get_drvdata(vdev);
  951. hdpvr_delete(dev);
  952. mutex_lock(&dev->io_mutex);
  953. destroy_workqueue(dev->workqueue);
  954. mutex_unlock(&dev->io_mutex);
  955. v4l2_device_unregister(&dev->v4l2_dev);
  956. v4l2_ctrl_handler_free(&dev->hdl);
  957. /* deregister I2C adapter */
  958. #if IS_ENABLED(CONFIG_I2C)
  959. mutex_lock(&dev->i2c_mutex);
  960. i2c_del_adapter(&dev->i2c_adapter);
  961. mutex_unlock(&dev->i2c_mutex);
  962. #endif /* CONFIG_I2C */
  963. kfree(dev->usbc_buf);
  964. kfree(dev);
  965. }
  966. static const struct video_device hdpvr_video_template = {
  967. .fops = &hdpvr_fops,
  968. .release = hdpvr_device_release,
  969. .ioctl_ops = &hdpvr_ioctl_ops,
  970. .tvnorms = V4L2_STD_ALL,
  971. };
  972. static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
  973. .try_ctrl = hdpvr_try_ctrl,
  974. .s_ctrl = hdpvr_s_ctrl,
  975. };
  976. int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
  977. int devnum)
  978. {
  979. struct v4l2_ctrl_handler *hdl = &dev->hdl;
  980. bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
  981. int res;
  982. dev->cur_std = V4L2_STD_525_60;
  983. dev->width = 720;
  984. dev->height = 480;
  985. dev->cur_dv_timings = hdpvr_dv_timings[HDPVR_DEF_DV_TIMINGS_IDX];
  986. v4l2_ctrl_handler_init(hdl, 11);
  987. if (dev->fw_ver > 0x15) {
  988. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  989. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
  990. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  991. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
  992. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  993. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
  994. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  995. V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
  996. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  997. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  998. } else {
  999. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1000. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
  1001. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1002. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
  1003. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1004. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
  1005. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1006. V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
  1007. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1008. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  1009. }
  1010. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  1011. V4L2_CID_MPEG_STREAM_TYPE,
  1012. V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
  1013. 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
  1014. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  1015. V4L2_CID_MPEG_AUDIO_ENCODING,
  1016. ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
  1017. 0x7, V4L2_MPEG_AUDIO_ENCODING_AAC);
  1018. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  1019. V4L2_CID_MPEG_VIDEO_ENCODING,
  1020. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
  1021. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  1022. dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  1023. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  1024. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
  1025. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  1026. dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1027. V4L2_CID_MPEG_VIDEO_BITRATE,
  1028. 1000000, 13500000, 100000, 6500000);
  1029. dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1030. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  1031. 1100000, 20200000, 100000, 9000000);
  1032. dev->v4l2_dev.ctrl_handler = hdl;
  1033. if (hdl->error) {
  1034. res = hdl->error;
  1035. v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
  1036. goto error;
  1037. }
  1038. v4l2_ctrl_cluster(3, &dev->video_mode);
  1039. res = v4l2_ctrl_handler_setup(hdl);
  1040. if (res < 0) {
  1041. v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
  1042. goto error;
  1043. }
  1044. /* setup and register video device */
  1045. dev->video_dev = video_device_alloc();
  1046. if (!dev->video_dev) {
  1047. v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
  1048. res = -ENOMEM;
  1049. goto error;
  1050. }
  1051. *dev->video_dev = hdpvr_video_template;
  1052. strcpy(dev->video_dev->name, "Hauppauge HD PVR");
  1053. dev->video_dev->v4l2_dev = &dev->v4l2_dev;
  1054. video_set_drvdata(dev->video_dev, dev);
  1055. set_bit(V4L2_FL_USE_FH_PRIO, &dev->video_dev->flags);
  1056. res = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum);
  1057. if (res < 0) {
  1058. v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
  1059. goto error;
  1060. }
  1061. return 0;
  1062. error:
  1063. v4l2_ctrl_handler_free(hdl);
  1064. return res;
  1065. }