hdpvr-video.c 31 KB

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