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. vidinf = get_video_info(dev);
  239. if (vidinf) {
  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. kfree(vidinf);
  244. /* start streaming 2 request */
  245. ret = usb_control_msg(dev->udev,
  246. usb_sndctrlpipe(dev->udev, 0),
  247. 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
  248. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  249. "encoder start control request returned %d\n", ret);
  250. if (ret < 0)
  251. return ret;
  252. ret = hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
  253. if (ret)
  254. return ret;
  255. dev->status = STATUS_STREAMING;
  256. INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
  257. queue_work(dev->workqueue, &dev->worker);
  258. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  259. "streaming started\n");
  260. return 0;
  261. }
  262. msleep(250);
  263. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  264. "no video signal at input %d\n", dev->options.video_input);
  265. return -EAGAIN;
  266. }
  267. /* function expects dev->io_mutex to be hold by caller */
  268. static int hdpvr_stop_streaming(struct hdpvr_device *dev)
  269. {
  270. int actual_length;
  271. uint c = 0;
  272. u8 *buf;
  273. if (dev->status == STATUS_IDLE)
  274. return 0;
  275. else if (dev->status != STATUS_STREAMING)
  276. return -EAGAIN;
  277. buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
  278. if (!buf)
  279. v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
  280. "for emptying the internal device buffer. "
  281. "Next capture start will be slow\n");
  282. dev->status = STATUS_SHUTTING_DOWN;
  283. hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
  284. mutex_unlock(&dev->io_mutex);
  285. wake_up_interruptible(&dev->wait_buffer);
  286. msleep(50);
  287. flush_workqueue(dev->workqueue);
  288. mutex_lock(&dev->io_mutex);
  289. /* kill the still outstanding urbs */
  290. hdpvr_cancel_queue(dev);
  291. /* emptying the device buffer beforeshutting it down */
  292. while (buf && ++c < 500 &&
  293. !usb_bulk_msg(dev->udev,
  294. usb_rcvbulkpipe(dev->udev,
  295. dev->bulk_in_endpointAddr),
  296. buf, dev->bulk_in_size, &actual_length,
  297. BULK_URB_TIMEOUT)) {
  298. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  299. "%2d: got %d bytes\n", c, actual_length);
  300. }
  301. kfree(buf);
  302. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  303. "used %d urbs to empty device buffers\n", c-1);
  304. msleep(10);
  305. dev->status = STATUS_IDLE;
  306. return 0;
  307. }
  308. /*=======================================================================*/
  309. /*
  310. * video 4 linux 2 file operations
  311. */
  312. static int hdpvr_open(struct file *file)
  313. {
  314. struct hdpvr_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  315. if (fh == NULL)
  316. return -ENOMEM;
  317. fh->legacy_mode = true;
  318. v4l2_fh_init(&fh->fh, video_devdata(file));
  319. v4l2_fh_add(&fh->fh);
  320. file->private_data = fh;
  321. return 0;
  322. }
  323. static int hdpvr_release(struct file *file)
  324. {
  325. struct hdpvr_device *dev = video_drvdata(file);
  326. mutex_lock(&dev->io_mutex);
  327. if (file->private_data == dev->owner) {
  328. hdpvr_stop_streaming(dev);
  329. dev->owner = NULL;
  330. }
  331. mutex_unlock(&dev->io_mutex);
  332. return v4l2_fh_release(file);
  333. }
  334. /*
  335. * hdpvr_v4l2_read()
  336. * will allocate buffers when called for the first time
  337. */
  338. static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
  339. loff_t *pos)
  340. {
  341. struct hdpvr_device *dev = video_drvdata(file);
  342. struct hdpvr_buffer *buf = NULL;
  343. struct urb *urb;
  344. unsigned int ret = 0;
  345. int rem, cnt;
  346. if (*pos)
  347. return -ESPIPE;
  348. mutex_lock(&dev->io_mutex);
  349. if (dev->status == STATUS_IDLE) {
  350. if (hdpvr_start_streaming(dev)) {
  351. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  352. "start_streaming failed\n");
  353. ret = -EIO;
  354. msleep(200);
  355. dev->status = STATUS_IDLE;
  356. mutex_unlock(&dev->io_mutex);
  357. goto err;
  358. }
  359. dev->owner = file->private_data;
  360. print_buffer_status();
  361. }
  362. mutex_unlock(&dev->io_mutex);
  363. /* wait for the first buffer */
  364. if (!(file->f_flags & O_NONBLOCK)) {
  365. if (wait_event_interruptible(dev->wait_data,
  366. hdpvr_get_next_buffer(dev)))
  367. return -ERESTARTSYS;
  368. }
  369. buf = hdpvr_get_next_buffer(dev);
  370. while (count > 0 && buf) {
  371. if (buf->status != BUFSTAT_READY &&
  372. dev->status != STATUS_DISCONNECTED) {
  373. /* return nonblocking */
  374. if (file->f_flags & O_NONBLOCK) {
  375. if (!ret)
  376. ret = -EAGAIN;
  377. goto err;
  378. }
  379. if (wait_event_interruptible(dev->wait_data,
  380. buf->status == BUFSTAT_READY)) {
  381. ret = -ERESTARTSYS;
  382. goto err;
  383. }
  384. }
  385. if (buf->status != BUFSTAT_READY)
  386. break;
  387. /* set remaining bytes to copy */
  388. urb = buf->urb;
  389. rem = urb->actual_length - buf->pos;
  390. cnt = rem > count ? count : rem;
  391. if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
  392. cnt)) {
  393. v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
  394. if (!ret)
  395. ret = -EFAULT;
  396. goto err;
  397. }
  398. buf->pos += cnt;
  399. count -= cnt;
  400. buffer += cnt;
  401. ret += cnt;
  402. /* finished, take next buffer */
  403. if (buf->pos == urb->actual_length) {
  404. mutex_lock(&dev->io_mutex);
  405. buf->pos = 0;
  406. buf->status = BUFSTAT_AVAILABLE;
  407. list_move_tail(&buf->buff_list, &dev->free_buff_list);
  408. print_buffer_status();
  409. mutex_unlock(&dev->io_mutex);
  410. wake_up_interruptible(&dev->wait_buffer);
  411. buf = hdpvr_get_next_buffer(dev);
  412. }
  413. }
  414. err:
  415. if (!ret && !buf)
  416. ret = -EAGAIN;
  417. return ret;
  418. }
  419. static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
  420. {
  421. unsigned long req_events = poll_requested_events(wait);
  422. struct hdpvr_buffer *buf = NULL;
  423. struct hdpvr_device *dev = video_drvdata(filp);
  424. unsigned int mask = v4l2_ctrl_poll(filp, wait);
  425. if (!(req_events & (POLLIN | POLLRDNORM)))
  426. return mask;
  427. mutex_lock(&dev->io_mutex);
  428. if (dev->status == STATUS_IDLE) {
  429. if (hdpvr_start_streaming(dev)) {
  430. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  431. "start_streaming failed\n");
  432. dev->status = STATUS_IDLE;
  433. } else {
  434. dev->owner = filp->private_data;
  435. }
  436. print_buffer_status();
  437. }
  438. mutex_unlock(&dev->io_mutex);
  439. buf = hdpvr_get_next_buffer(dev);
  440. /* only wait if no data is available */
  441. if (!buf || buf->status != BUFSTAT_READY) {
  442. poll_wait(filp, &dev->wait_data, wait);
  443. buf = hdpvr_get_next_buffer(dev);
  444. }
  445. if (buf && buf->status == BUFSTAT_READY)
  446. mask |= POLLIN | POLLRDNORM;
  447. return mask;
  448. }
  449. static const struct v4l2_file_operations hdpvr_fops = {
  450. .owner = THIS_MODULE,
  451. .open = hdpvr_open,
  452. .release = hdpvr_release,
  453. .read = hdpvr_read,
  454. .poll = hdpvr_poll,
  455. .unlocked_ioctl = video_ioctl2,
  456. };
  457. /*=======================================================================*/
  458. /*
  459. * V4L2 ioctl handling
  460. */
  461. static int vidioc_querycap(struct file *file, void *priv,
  462. struct v4l2_capability *cap)
  463. {
  464. struct hdpvr_device *dev = video_drvdata(file);
  465. strcpy(cap->driver, "hdpvr");
  466. strcpy(cap->card, "Hauppauge HD PVR");
  467. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  468. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO |
  469. V4L2_CAP_READWRITE;
  470. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  471. return 0;
  472. }
  473. static int vidioc_s_std(struct file *file, void *_fh,
  474. v4l2_std_id std)
  475. {
  476. struct hdpvr_device *dev = video_drvdata(file);
  477. struct hdpvr_fh *fh = _fh;
  478. u8 std_type = 1;
  479. if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
  480. return -ENODATA;
  481. if (dev->status != STATUS_IDLE)
  482. return -EBUSY;
  483. if (std & V4L2_STD_525_60)
  484. std_type = 0;
  485. dev->cur_std = std;
  486. dev->width = 720;
  487. dev->height = std_type ? 576 : 480;
  488. return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
  489. }
  490. static int vidioc_g_std(struct file *file, void *_fh,
  491. v4l2_std_id *std)
  492. {
  493. struct hdpvr_device *dev = video_drvdata(file);
  494. struct hdpvr_fh *fh = _fh;
  495. if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
  496. return -ENODATA;
  497. *std = dev->cur_std;
  498. return 0;
  499. }
  500. static int vidioc_querystd(struct file *file, void *_fh, v4l2_std_id *a)
  501. {
  502. struct hdpvr_device *dev = video_drvdata(file);
  503. struct hdpvr_video_info *vid_info;
  504. struct hdpvr_fh *fh = _fh;
  505. *a = V4L2_STD_ALL;
  506. if (dev->options.video_input == HDPVR_COMPONENT)
  507. return fh->legacy_mode ? 0 : -ENODATA;
  508. vid_info = get_video_info(dev);
  509. if (vid_info == NULL)
  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. kfree(vid_info);
  517. return 0;
  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 (v4l_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. vid_info = get_video_info(dev);
  564. if (vid_info == NULL)
  565. return -ENOLCK;
  566. interlaced = vid_info->fps <= 30;
  567. for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) {
  568. const struct v4l2_bt_timings *bt = &hdpvr_dv_timings[i].bt;
  569. unsigned hsize;
  570. unsigned vsize;
  571. unsigned fps;
  572. hsize = bt->hfrontporch + bt->hsync + bt->hbackporch + bt->width;
  573. vsize = bt->vfrontporch + bt->vsync + bt->vbackporch +
  574. bt->il_vfrontporch + bt->il_vsync + bt->il_vbackporch +
  575. bt->height;
  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. kfree(vid_info);
  588. return ret;
  589. }
  590. static int vidioc_enum_dv_timings(struct file *file, void *_fh,
  591. struct v4l2_enum_dv_timings *timings)
  592. {
  593. struct hdpvr_device *dev = video_drvdata(file);
  594. struct hdpvr_fh *fh = _fh;
  595. fh->legacy_mode = false;
  596. memset(timings->reserved, 0, sizeof(timings->reserved));
  597. if (dev->options.video_input)
  598. return -ENODATA;
  599. if (timings->index >= ARRAY_SIZE(hdpvr_dv_timings))
  600. return -EINVAL;
  601. timings->timings = hdpvr_dv_timings[timings->index];
  602. return 0;
  603. }
  604. static int vidioc_dv_timings_cap(struct file *file, void *_fh,
  605. struct v4l2_dv_timings_cap *cap)
  606. {
  607. struct hdpvr_device *dev = video_drvdata(file);
  608. struct hdpvr_fh *fh = _fh;
  609. fh->legacy_mode = false;
  610. if (dev->options.video_input)
  611. return -ENODATA;
  612. cap->type = V4L2_DV_BT_656_1120;
  613. cap->bt.min_width = 720;
  614. cap->bt.max_width = 1920;
  615. cap->bt.min_height = 480;
  616. cap->bt.max_height = 1080;
  617. cap->bt.min_pixelclock = 27000000;
  618. cap->bt.max_pixelclock = 74250000;
  619. cap->bt.standards = V4L2_DV_BT_STD_CEA861;
  620. cap->bt.capabilities = V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE;
  621. return 0;
  622. }
  623. static const char *iname[] = {
  624. [HDPVR_COMPONENT] = "Component",
  625. [HDPVR_SVIDEO] = "S-Video",
  626. [HDPVR_COMPOSITE] = "Composite",
  627. };
  628. static int vidioc_enum_input(struct file *file, void *_fh, struct v4l2_input *i)
  629. {
  630. unsigned int n;
  631. n = i->index;
  632. if (n >= HDPVR_VIDEO_INPUTS)
  633. return -EINVAL;
  634. i->type = V4L2_INPUT_TYPE_CAMERA;
  635. strncpy(i->name, iname[n], sizeof(i->name) - 1);
  636. i->name[sizeof(i->name) - 1] = '\0';
  637. i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
  638. i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
  639. i->std = n ? V4L2_STD_ALL : 0;
  640. return 0;
  641. }
  642. static int vidioc_s_input(struct file *file, void *_fh,
  643. unsigned int index)
  644. {
  645. struct hdpvr_device *dev = video_drvdata(file);
  646. int retval;
  647. if (index >= HDPVR_VIDEO_INPUTS)
  648. return -EINVAL;
  649. if (dev->status != STATUS_IDLE)
  650. return -EBUSY;
  651. retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
  652. if (!retval) {
  653. dev->options.video_input = index;
  654. /*
  655. * Unfortunately gstreamer calls ENUMSTD and bails out if it
  656. * won't find any formats, even though component input is
  657. * selected. This means that we have to leave tvnorms at
  658. * V4L2_STD_ALL. We cannot use the 'legacy' trick since
  659. * tvnorms is set at the device node level and not at the
  660. * filehandle level.
  661. *
  662. * Comment this out for now, but if the legacy mode can be
  663. * removed in the future, then this code should be enabled
  664. * again.
  665. dev->video_dev->tvnorms =
  666. (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
  667. */
  668. }
  669. return retval;
  670. }
  671. static int vidioc_g_input(struct file *file, void *private_data,
  672. unsigned int *index)
  673. {
  674. struct hdpvr_device *dev = video_drvdata(file);
  675. *index = dev->options.video_input;
  676. return 0;
  677. }
  678. static const char *audio_iname[] = {
  679. [HDPVR_RCA_FRONT] = "RCA front",
  680. [HDPVR_RCA_BACK] = "RCA back",
  681. [HDPVR_SPDIF] = "SPDIF",
  682. };
  683. static int vidioc_enumaudio(struct file *file, void *priv,
  684. struct v4l2_audio *audio)
  685. {
  686. unsigned int n;
  687. n = audio->index;
  688. if (n >= HDPVR_AUDIO_INPUTS)
  689. return -EINVAL;
  690. audio->capability = V4L2_AUDCAP_STEREO;
  691. strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
  692. audio->name[sizeof(audio->name) - 1] = '\0';
  693. return 0;
  694. }
  695. static int vidioc_s_audio(struct file *file, void *private_data,
  696. const struct v4l2_audio *audio)
  697. {
  698. struct hdpvr_device *dev = video_drvdata(file);
  699. int retval;
  700. if (audio->index >= HDPVR_AUDIO_INPUTS)
  701. return -EINVAL;
  702. if (dev->status != STATUS_IDLE)
  703. return -EBUSY;
  704. retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
  705. if (!retval)
  706. dev->options.audio_input = audio->index;
  707. return retval;
  708. }
  709. static int vidioc_g_audio(struct file *file, void *private_data,
  710. struct v4l2_audio *audio)
  711. {
  712. struct hdpvr_device *dev = video_drvdata(file);
  713. audio->index = dev->options.audio_input;
  714. audio->capability = V4L2_AUDCAP_STEREO;
  715. strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
  716. audio->name[sizeof(audio->name) - 1] = '\0';
  717. return 0;
  718. }
  719. static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
  720. {
  721. struct hdpvr_device *dev =
  722. container_of(ctrl->handler, struct hdpvr_device, hdl);
  723. switch (ctrl->id) {
  724. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  725. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  726. dev->video_bitrate->val >= dev->video_bitrate_peak->val)
  727. dev->video_bitrate_peak->val =
  728. dev->video_bitrate->val + 100000;
  729. break;
  730. }
  731. return 0;
  732. }
  733. static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
  734. {
  735. struct hdpvr_device *dev =
  736. container_of(ctrl->handler, struct hdpvr_device, hdl);
  737. struct hdpvr_options *opt = &dev->options;
  738. int ret = -EINVAL;
  739. switch (ctrl->id) {
  740. case V4L2_CID_BRIGHTNESS:
  741. ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
  742. if (ret)
  743. break;
  744. dev->options.brightness = ctrl->val;
  745. return 0;
  746. case V4L2_CID_CONTRAST:
  747. ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
  748. if (ret)
  749. break;
  750. dev->options.contrast = ctrl->val;
  751. return 0;
  752. case V4L2_CID_SATURATION:
  753. ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
  754. if (ret)
  755. break;
  756. dev->options.saturation = ctrl->val;
  757. return 0;
  758. case V4L2_CID_HUE:
  759. ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
  760. if (ret)
  761. break;
  762. dev->options.hue = ctrl->val;
  763. return 0;
  764. case V4L2_CID_SHARPNESS:
  765. ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
  766. if (ret)
  767. break;
  768. dev->options.sharpness = ctrl->val;
  769. return 0;
  770. case V4L2_CID_MPEG_AUDIO_ENCODING:
  771. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  772. opt->audio_codec = ctrl->val;
  773. return hdpvr_set_audio(dev, opt->audio_input,
  774. opt->audio_codec);
  775. }
  776. return 0;
  777. case V4L2_CID_MPEG_VIDEO_ENCODING:
  778. return 0;
  779. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  780. /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
  781. /* opt->gop_mode |= 0x2; */
  782. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  783. /* opt->gop_mode); */
  784. /* } */
  785. /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
  786. /* opt->gop_mode &= ~0x2; */
  787. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  788. /* opt->gop_mode); */
  789. /* } */
  790. /* break; */
  791. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
  792. uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
  793. uint bitrate = dev->video_bitrate->val / 100000;
  794. if (ctrl->is_new) {
  795. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
  796. opt->bitrate_mode = HDPVR_CONSTANT;
  797. else
  798. opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
  799. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  800. opt->bitrate_mode);
  801. v4l2_ctrl_activate(dev->video_bitrate_peak,
  802. ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  803. }
  804. if (dev->video_bitrate_peak->is_new ||
  805. dev->video_bitrate->is_new) {
  806. opt->bitrate = bitrate;
  807. opt->peak_bitrate = peak_bitrate;
  808. hdpvr_set_bitrate(dev);
  809. }
  810. return 0;
  811. }
  812. case V4L2_CID_MPEG_STREAM_TYPE:
  813. return 0;
  814. default:
  815. break;
  816. }
  817. return ret;
  818. }
  819. static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
  820. struct v4l2_fmtdesc *f)
  821. {
  822. if (f->index != 0)
  823. return -EINVAL;
  824. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  825. strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
  826. f->pixelformat = V4L2_PIX_FMT_MPEG;
  827. return 0;
  828. }
  829. static int vidioc_g_fmt_vid_cap(struct file *file, void *_fh,
  830. struct v4l2_format *f)
  831. {
  832. struct hdpvr_device *dev = video_drvdata(file);
  833. struct hdpvr_fh *fh = _fh;
  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. vid_info = get_video_info(dev);
  847. if (!vid_info)
  848. return -EFAULT;
  849. f->fmt.pix.width = vid_info->width;
  850. f->fmt.pix.height = vid_info->height;
  851. kfree(vid_info);
  852. } else {
  853. f->fmt.pix.width = dev->width;
  854. f->fmt.pix.height = dev->height;
  855. }
  856. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  857. f->fmt.pix.sizeimage = dev->bulk_in_size;
  858. f->fmt.pix.bytesperline = 0;
  859. f->fmt.pix.priv = 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_SMPTE240M;
  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, 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 = video_device_alloc();
  1048. if (!dev->video_dev) {
  1049. v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
  1050. res = -ENOMEM;
  1051. goto error;
  1052. }
  1053. *dev->video_dev = hdpvr_video_template;
  1054. strcpy(dev->video_dev->name, "Hauppauge HD PVR");
  1055. dev->video_dev->v4l2_dev = &dev->v4l2_dev;
  1056. video_set_drvdata(dev->video_dev, dev);
  1057. set_bit(V4L2_FL_USE_FH_PRIO, &dev->video_dev->flags);
  1058. res = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum);
  1059. if (res < 0) {
  1060. v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
  1061. goto error;
  1062. }
  1063. return 0;
  1064. error:
  1065. v4l2_ctrl_handler_free(hdl);
  1066. return res;
  1067. }