hdpvr-video.c 31 KB

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