hdpvr-video.c 31 KB

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