hdpvr-video.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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 *private_data,
  470. v4l2_std_id std)
  471. {
  472. struct hdpvr_device *dev = video_drvdata(file);
  473. u8 std_type = 1;
  474. if (dev->options.video_input == HDPVR_COMPONENT)
  475. return -ENODATA;
  476. if (dev->status != STATUS_IDLE)
  477. return -EBUSY;
  478. if (std & V4L2_STD_525_60)
  479. std_type = 0;
  480. dev->cur_std = std;
  481. dev->width = 720;
  482. dev->height = std_type ? 576 : 480;
  483. return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
  484. }
  485. static int vidioc_g_std(struct file *file, void *private_data,
  486. v4l2_std_id *std)
  487. {
  488. struct hdpvr_device *dev = video_drvdata(file);
  489. if (dev->options.video_input == HDPVR_COMPONENT)
  490. return -ENODATA;
  491. *std = dev->cur_std;
  492. return 0;
  493. }
  494. static int vidioc_querystd(struct file *file, void *fh, v4l2_std_id *a)
  495. {
  496. struct hdpvr_device *dev = video_drvdata(file);
  497. struct hdpvr_video_info *vid_info;
  498. if (dev->options.video_input == HDPVR_COMPONENT)
  499. return -ENODATA;
  500. *a = V4L2_STD_ALL;
  501. vid_info = get_video_info(dev);
  502. if (vid_info == NULL)
  503. return 0;
  504. if (vid_info->width == 720 &&
  505. (vid_info->height == 480 || vid_info->height == 576)) {
  506. *a = (vid_info->height == 480) ?
  507. V4L2_STD_525_60 : V4L2_STD_625_50;
  508. }
  509. kfree(vid_info);
  510. return 0;
  511. }
  512. static int vidioc_s_dv_timings(struct file *file, void *_fh,
  513. struct v4l2_dv_timings *timings)
  514. {
  515. struct hdpvr_device *dev = video_drvdata(file);
  516. struct hdpvr_fh *fh = _fh;
  517. int i;
  518. fh->legacy_mode = false;
  519. if (dev->options.video_input)
  520. return -ENODATA;
  521. if (dev->status != STATUS_IDLE)
  522. return -EBUSY;
  523. for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++)
  524. if (v4l_match_dv_timings(timings, hdpvr_dv_timings + i, 0))
  525. break;
  526. if (i == ARRAY_SIZE(hdpvr_dv_timings))
  527. return -EINVAL;
  528. dev->cur_dv_timings = hdpvr_dv_timings[i];
  529. dev->width = hdpvr_dv_timings[i].bt.width;
  530. dev->height = hdpvr_dv_timings[i].bt.height;
  531. return 0;
  532. }
  533. static int vidioc_g_dv_timings(struct file *file, void *_fh,
  534. struct v4l2_dv_timings *timings)
  535. {
  536. struct hdpvr_device *dev = video_drvdata(file);
  537. struct hdpvr_fh *fh = _fh;
  538. fh->legacy_mode = false;
  539. if (dev->options.video_input)
  540. return -ENODATA;
  541. *timings = dev->cur_dv_timings;
  542. return 0;
  543. }
  544. static int vidioc_query_dv_timings(struct file *file, void *_fh,
  545. struct v4l2_dv_timings *timings)
  546. {
  547. struct hdpvr_device *dev = video_drvdata(file);
  548. struct hdpvr_fh *fh = _fh;
  549. struct hdpvr_video_info *vid_info;
  550. bool interlaced;
  551. int ret = 0;
  552. int i;
  553. fh->legacy_mode = false;
  554. if (dev->options.video_input)
  555. return -ENODATA;
  556. vid_info = get_video_info(dev);
  557. if (vid_info == NULL)
  558. return -ENOLCK;
  559. interlaced = vid_info->fps <= 30;
  560. for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) {
  561. const struct v4l2_bt_timings *bt = &hdpvr_dv_timings[i].bt;
  562. unsigned hsize;
  563. unsigned vsize;
  564. unsigned fps;
  565. hsize = bt->hfrontporch + bt->hsync + bt->hbackporch + bt->width;
  566. vsize = bt->vfrontporch + bt->vsync + bt->vbackporch +
  567. bt->il_vfrontporch + bt->il_vsync + bt->il_vbackporch +
  568. bt->height;
  569. fps = (unsigned)bt->pixelclock / (hsize * vsize);
  570. if (bt->width != vid_info->width ||
  571. bt->height != vid_info->height ||
  572. bt->interlaced != interlaced ||
  573. (fps != vid_info->fps && fps + 1 != vid_info->fps))
  574. continue;
  575. *timings = hdpvr_dv_timings[i];
  576. break;
  577. }
  578. if (i == ARRAY_SIZE(hdpvr_dv_timings))
  579. ret = -ERANGE;
  580. kfree(vid_info);
  581. return ret;
  582. }
  583. static int vidioc_enum_dv_timings(struct file *file, void *_fh,
  584. struct v4l2_enum_dv_timings *timings)
  585. {
  586. struct hdpvr_device *dev = video_drvdata(file);
  587. struct hdpvr_fh *fh = _fh;
  588. fh->legacy_mode = false;
  589. memset(timings->reserved, 0, sizeof(timings->reserved));
  590. if (dev->options.video_input)
  591. return -ENODATA;
  592. if (timings->index >= ARRAY_SIZE(hdpvr_dv_timings))
  593. return -EINVAL;
  594. timings->timings = hdpvr_dv_timings[timings->index];
  595. return 0;
  596. }
  597. static int vidioc_dv_timings_cap(struct file *file, void *_fh,
  598. struct v4l2_dv_timings_cap *cap)
  599. {
  600. struct hdpvr_device *dev = video_drvdata(file);
  601. struct hdpvr_fh *fh = _fh;
  602. fh->legacy_mode = false;
  603. if (dev->options.video_input)
  604. return -ENODATA;
  605. cap->type = V4L2_DV_BT_656_1120;
  606. cap->bt.min_width = 720;
  607. cap->bt.max_width = 1920;
  608. cap->bt.min_height = 480;
  609. cap->bt.max_height = 1080;
  610. cap->bt.min_pixelclock = 27000000;
  611. cap->bt.max_pixelclock = 74250000;
  612. cap->bt.standards = V4L2_DV_BT_STD_CEA861;
  613. cap->bt.capabilities = V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE;
  614. return 0;
  615. }
  616. static const char *iname[] = {
  617. [HDPVR_COMPONENT] = "Component",
  618. [HDPVR_SVIDEO] = "S-Video",
  619. [HDPVR_COMPOSITE] = "Composite",
  620. };
  621. static int vidioc_enum_input(struct file *file, void *priv,
  622. struct v4l2_input *i)
  623. {
  624. unsigned int n;
  625. n = i->index;
  626. if (n >= HDPVR_VIDEO_INPUTS)
  627. return -EINVAL;
  628. i->type = V4L2_INPUT_TYPE_CAMERA;
  629. strncpy(i->name, iname[n], sizeof(i->name) - 1);
  630. i->name[sizeof(i->name) - 1] = '\0';
  631. i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
  632. i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
  633. i->std = n ? V4L2_STD_ALL : 0;
  634. return 0;
  635. }
  636. static int vidioc_s_input(struct file *file, void *private_data,
  637. unsigned int index)
  638. {
  639. struct hdpvr_device *dev = video_drvdata(file);
  640. int retval;
  641. if (index >= HDPVR_VIDEO_INPUTS)
  642. return -EINVAL;
  643. if (dev->status != STATUS_IDLE)
  644. return -EBUSY;
  645. retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
  646. if (!retval) {
  647. dev->options.video_input = index;
  648. dev->video_dev->tvnorms =
  649. index != HDPVR_COMPONENT ? V4L2_STD_ALL : 0;
  650. }
  651. return retval;
  652. }
  653. static int vidioc_g_input(struct file *file, void *private_data,
  654. unsigned int *index)
  655. {
  656. struct hdpvr_device *dev = video_drvdata(file);
  657. *index = dev->options.video_input;
  658. return 0;
  659. }
  660. static const char *audio_iname[] = {
  661. [HDPVR_RCA_FRONT] = "RCA front",
  662. [HDPVR_RCA_BACK] = "RCA back",
  663. [HDPVR_SPDIF] = "SPDIF",
  664. };
  665. static int vidioc_enumaudio(struct file *file, void *priv,
  666. struct v4l2_audio *audio)
  667. {
  668. unsigned int n;
  669. n = audio->index;
  670. if (n >= HDPVR_AUDIO_INPUTS)
  671. return -EINVAL;
  672. audio->capability = V4L2_AUDCAP_STEREO;
  673. strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
  674. audio->name[sizeof(audio->name) - 1] = '\0';
  675. return 0;
  676. }
  677. static int vidioc_s_audio(struct file *file, void *private_data,
  678. const struct v4l2_audio *audio)
  679. {
  680. struct hdpvr_device *dev = video_drvdata(file);
  681. int retval;
  682. if (audio->index >= HDPVR_AUDIO_INPUTS)
  683. return -EINVAL;
  684. if (dev->status != STATUS_IDLE)
  685. return -EBUSY;
  686. retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
  687. if (!retval)
  688. dev->options.audio_input = audio->index;
  689. return retval;
  690. }
  691. static int vidioc_g_audio(struct file *file, void *private_data,
  692. struct v4l2_audio *audio)
  693. {
  694. struct hdpvr_device *dev = video_drvdata(file);
  695. audio->index = dev->options.audio_input;
  696. audio->capability = V4L2_AUDCAP_STEREO;
  697. strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
  698. audio->name[sizeof(audio->name) - 1] = '\0';
  699. return 0;
  700. }
  701. static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
  702. {
  703. struct hdpvr_device *dev =
  704. container_of(ctrl->handler, struct hdpvr_device, hdl);
  705. switch (ctrl->id) {
  706. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  707. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  708. dev->video_bitrate->val >= dev->video_bitrate_peak->val)
  709. dev->video_bitrate_peak->val =
  710. dev->video_bitrate->val + 100000;
  711. break;
  712. }
  713. return 0;
  714. }
  715. static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
  716. {
  717. struct hdpvr_device *dev =
  718. container_of(ctrl->handler, struct hdpvr_device, hdl);
  719. struct hdpvr_options *opt = &dev->options;
  720. int ret = -EINVAL;
  721. switch (ctrl->id) {
  722. case V4L2_CID_BRIGHTNESS:
  723. ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
  724. if (ret)
  725. break;
  726. dev->options.brightness = ctrl->val;
  727. return 0;
  728. case V4L2_CID_CONTRAST:
  729. ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
  730. if (ret)
  731. break;
  732. dev->options.contrast = ctrl->val;
  733. return 0;
  734. case V4L2_CID_SATURATION:
  735. ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
  736. if (ret)
  737. break;
  738. dev->options.saturation = ctrl->val;
  739. return 0;
  740. case V4L2_CID_HUE:
  741. ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
  742. if (ret)
  743. break;
  744. dev->options.hue = ctrl->val;
  745. return 0;
  746. case V4L2_CID_SHARPNESS:
  747. ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
  748. if (ret)
  749. break;
  750. dev->options.sharpness = ctrl->val;
  751. return 0;
  752. case V4L2_CID_MPEG_AUDIO_ENCODING:
  753. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  754. opt->audio_codec = ctrl->val;
  755. return hdpvr_set_audio(dev, opt->audio_input,
  756. opt->audio_codec);
  757. }
  758. return 0;
  759. case V4L2_CID_MPEG_VIDEO_ENCODING:
  760. return 0;
  761. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  762. /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
  763. /* opt->gop_mode |= 0x2; */
  764. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  765. /* opt->gop_mode); */
  766. /* } */
  767. /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
  768. /* opt->gop_mode &= ~0x2; */
  769. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  770. /* opt->gop_mode); */
  771. /* } */
  772. /* break; */
  773. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
  774. uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
  775. uint bitrate = dev->video_bitrate->val / 100000;
  776. if (ctrl->is_new) {
  777. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
  778. opt->bitrate_mode = HDPVR_CONSTANT;
  779. else
  780. opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
  781. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  782. opt->bitrate_mode);
  783. v4l2_ctrl_activate(dev->video_bitrate_peak,
  784. ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  785. }
  786. if (dev->video_bitrate_peak->is_new ||
  787. dev->video_bitrate->is_new) {
  788. opt->bitrate = bitrate;
  789. opt->peak_bitrate = peak_bitrate;
  790. hdpvr_set_bitrate(dev);
  791. }
  792. return 0;
  793. }
  794. case V4L2_CID_MPEG_STREAM_TYPE:
  795. return 0;
  796. default:
  797. break;
  798. }
  799. return ret;
  800. }
  801. static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
  802. struct v4l2_fmtdesc *f)
  803. {
  804. if (f->index != 0)
  805. return -EINVAL;
  806. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  807. strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
  808. f->pixelformat = V4L2_PIX_FMT_MPEG;
  809. return 0;
  810. }
  811. static int vidioc_g_fmt_vid_cap(struct file *file, void *_fh,
  812. struct v4l2_format *f)
  813. {
  814. struct hdpvr_device *dev = video_drvdata(file);
  815. struct hdpvr_fh *fh = _fh;
  816. /*
  817. * The original driver would always returns the current detected
  818. * resolution as the format (and EFAULT if it couldn't be detected).
  819. * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
  820. * better way of doing this, but to stay compatible with existing
  821. * applications we assume legacy mode every time an application opens
  822. * the device. Only if one of the new DV_TIMINGS ioctls is called
  823. * will the filehandle go into 'normal' mode where g_fmt returns the
  824. * last set format.
  825. */
  826. if (fh->legacy_mode) {
  827. struct hdpvr_video_info *vid_info;
  828. vid_info = get_video_info(dev);
  829. if (!vid_info)
  830. return -EFAULT;
  831. f->fmt.pix.width = vid_info->width;
  832. f->fmt.pix.height = vid_info->height;
  833. kfree(vid_info);
  834. } else {
  835. f->fmt.pix.width = dev->width;
  836. f->fmt.pix.height = dev->height;
  837. }
  838. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  839. f->fmt.pix.sizeimage = dev->bulk_in_size;
  840. f->fmt.pix.bytesperline = 0;
  841. f->fmt.pix.priv = 0;
  842. if (f->fmt.pix.width == 720) {
  843. /* SDTV formats */
  844. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  845. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  846. } else {
  847. /* HDTV formats */
  848. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE240M;
  849. f->fmt.pix.field = V4L2_FIELD_NONE;
  850. }
  851. return 0;
  852. }
  853. static int vidioc_encoder_cmd(struct file *filp, void *priv,
  854. struct v4l2_encoder_cmd *a)
  855. {
  856. struct hdpvr_device *dev = video_drvdata(filp);
  857. int res = 0;
  858. mutex_lock(&dev->io_mutex);
  859. a->flags = 0;
  860. switch (a->cmd) {
  861. case V4L2_ENC_CMD_START:
  862. if (dev->owner && filp->private_data != dev->owner) {
  863. res = -EBUSY;
  864. break;
  865. }
  866. if (dev->status == STATUS_STREAMING)
  867. break;
  868. res = hdpvr_start_streaming(dev);
  869. if (!res)
  870. dev->owner = filp->private_data;
  871. else
  872. dev->status = STATUS_IDLE;
  873. break;
  874. case V4L2_ENC_CMD_STOP:
  875. if (dev->owner && filp->private_data != dev->owner) {
  876. res = -EBUSY;
  877. break;
  878. }
  879. if (dev->status == STATUS_IDLE)
  880. break;
  881. res = hdpvr_stop_streaming(dev);
  882. if (!res)
  883. dev->owner = NULL;
  884. break;
  885. default:
  886. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  887. "Unsupported encoder cmd %d\n", a->cmd);
  888. res = -EINVAL;
  889. break;
  890. }
  891. mutex_unlock(&dev->io_mutex);
  892. return res;
  893. }
  894. static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
  895. struct v4l2_encoder_cmd *a)
  896. {
  897. a->flags = 0;
  898. switch (a->cmd) {
  899. case V4L2_ENC_CMD_START:
  900. case V4L2_ENC_CMD_STOP:
  901. return 0;
  902. default:
  903. return -EINVAL;
  904. }
  905. }
  906. static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
  907. .vidioc_querycap = vidioc_querycap,
  908. .vidioc_s_std = vidioc_s_std,
  909. .vidioc_g_std = vidioc_g_std,
  910. .vidioc_querystd = vidioc_querystd,
  911. .vidioc_s_dv_timings = vidioc_s_dv_timings,
  912. .vidioc_g_dv_timings = vidioc_g_dv_timings,
  913. .vidioc_query_dv_timings= vidioc_query_dv_timings,
  914. .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
  915. .vidioc_dv_timings_cap = vidioc_dv_timings_cap,
  916. .vidioc_enum_input = vidioc_enum_input,
  917. .vidioc_g_input = vidioc_g_input,
  918. .vidioc_s_input = vidioc_s_input,
  919. .vidioc_enumaudio = vidioc_enumaudio,
  920. .vidioc_g_audio = vidioc_g_audio,
  921. .vidioc_s_audio = vidioc_s_audio,
  922. .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
  923. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  924. .vidioc_s_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  925. .vidioc_try_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  926. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  927. .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
  928. .vidioc_log_status = v4l2_ctrl_log_status,
  929. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  930. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  931. };
  932. static void hdpvr_device_release(struct video_device *vdev)
  933. {
  934. struct hdpvr_device *dev = video_get_drvdata(vdev);
  935. hdpvr_delete(dev);
  936. mutex_lock(&dev->io_mutex);
  937. destroy_workqueue(dev->workqueue);
  938. mutex_unlock(&dev->io_mutex);
  939. v4l2_device_unregister(&dev->v4l2_dev);
  940. v4l2_ctrl_handler_free(&dev->hdl);
  941. /* deregister I2C adapter */
  942. #if IS_ENABLED(CONFIG_I2C)
  943. mutex_lock(&dev->i2c_mutex);
  944. i2c_del_adapter(&dev->i2c_adapter);
  945. mutex_unlock(&dev->i2c_mutex);
  946. #endif /* CONFIG_I2C */
  947. kfree(dev->usbc_buf);
  948. kfree(dev);
  949. }
  950. static const struct video_device hdpvr_video_template = {
  951. .fops = &hdpvr_fops,
  952. .release = hdpvr_device_release,
  953. .ioctl_ops = &hdpvr_ioctl_ops,
  954. };
  955. static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
  956. .try_ctrl = hdpvr_try_ctrl,
  957. .s_ctrl = hdpvr_s_ctrl,
  958. };
  959. int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
  960. int devnum)
  961. {
  962. struct v4l2_ctrl_handler *hdl = &dev->hdl;
  963. bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
  964. int res;
  965. dev->cur_std = V4L2_STD_525_60;
  966. dev->width = 720;
  967. dev->height = 480;
  968. dev->cur_dv_timings = hdpvr_dv_timings[HDPVR_DEF_DV_TIMINGS_IDX];
  969. v4l2_ctrl_handler_init(hdl, 11);
  970. if (dev->fw_ver > 0x15) {
  971. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  972. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
  973. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  974. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
  975. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  976. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
  977. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  978. V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
  979. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  980. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  981. } else {
  982. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  983. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
  984. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  985. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
  986. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  987. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
  988. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  989. V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
  990. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  991. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  992. }
  993. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  994. V4L2_CID_MPEG_STREAM_TYPE,
  995. V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
  996. 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
  997. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  998. V4L2_CID_MPEG_AUDIO_ENCODING,
  999. ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
  1000. 0x7, V4L2_MPEG_AUDIO_ENCODING_AAC);
  1001. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  1002. V4L2_CID_MPEG_VIDEO_ENCODING,
  1003. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
  1004. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  1005. dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  1006. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  1007. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
  1008. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  1009. dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1010. V4L2_CID_MPEG_VIDEO_BITRATE,
  1011. 1000000, 13500000, 100000, 6500000);
  1012. dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1013. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  1014. 1100000, 20200000, 100000, 9000000);
  1015. dev->v4l2_dev.ctrl_handler = hdl;
  1016. if (hdl->error) {
  1017. res = hdl->error;
  1018. v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
  1019. goto error;
  1020. }
  1021. v4l2_ctrl_cluster(3, &dev->video_mode);
  1022. res = v4l2_ctrl_handler_setup(hdl);
  1023. if (res < 0) {
  1024. v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
  1025. goto error;
  1026. }
  1027. /* setup and register video device */
  1028. dev->video_dev = video_device_alloc();
  1029. if (!dev->video_dev) {
  1030. v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
  1031. res = -ENOMEM;
  1032. goto error;
  1033. }
  1034. *dev->video_dev = hdpvr_video_template;
  1035. strcpy(dev->video_dev->name, "Hauppauge HD PVR");
  1036. dev->video_dev->v4l2_dev = &dev->v4l2_dev;
  1037. video_set_drvdata(dev->video_dev, dev);
  1038. set_bit(V4L2_FL_USE_FH_PRIO, &dev->video_dev->flags);
  1039. res = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum);
  1040. if (res < 0) {
  1041. v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
  1042. goto error;
  1043. }
  1044. return 0;
  1045. error:
  1046. v4l2_ctrl_handler_free(hdl);
  1047. return res;
  1048. }