hdpvr-video.c 31 KB

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