hdpvr-video.c 31 KB

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