hdpvr-video.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * Hauppauge HD PVR USB driver - video 4 linux 2 interface
  3. *
  4. * Copyright (C) 2008 Janne Grunau (j@jannau.net)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/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/version.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/videodev2.h>
  22. #include <media/v4l2-dev.h>
  23. #include <media/v4l2-common.h>
  24. #include <media/v4l2-ioctl.h>
  25. #include "hdpvr.h"
  26. #define BULK_URB_TIMEOUT 1250 /* 1.25 seconds */
  27. #define print_buffer_status() { \
  28. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
  29. "%s:%d buffer stat: %d free, %d proc\n", \
  30. __func__, __LINE__, \
  31. list_size(&dev->free_buff_list), \
  32. list_size(&dev->rec_buff_list)); }
  33. struct hdpvr_fh {
  34. struct hdpvr_device *dev;
  35. };
  36. static uint list_size(struct list_head *list)
  37. {
  38. struct list_head *tmp;
  39. uint count = 0;
  40. list_for_each(tmp, list) {
  41. count++;
  42. }
  43. return count;
  44. }
  45. /*=========================================================================*/
  46. /* urb callback */
  47. static void hdpvr_read_bulk_callback(struct urb *urb)
  48. {
  49. struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
  50. struct hdpvr_device *dev = buf->dev;
  51. /* marking buffer as received and wake waiting */
  52. buf->status = BUFSTAT_READY;
  53. wake_up_interruptible(&dev->wait_data);
  54. }
  55. /*=========================================================================*/
  56. /* bufffer bits */
  57. /* function expects dev->io_mutex to be hold by caller */
  58. int hdpvr_cancel_queue(struct hdpvr_device *dev)
  59. {
  60. struct hdpvr_buffer *buf;
  61. list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
  62. usb_kill_urb(buf->urb);
  63. buf->status = BUFSTAT_AVAILABLE;
  64. }
  65. list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
  66. return 0;
  67. }
  68. static int hdpvr_free_queue(struct list_head *q)
  69. {
  70. struct list_head *tmp;
  71. struct list_head *p;
  72. struct hdpvr_buffer *buf;
  73. struct urb *urb;
  74. for (p = q->next; p != q;) {
  75. buf = list_entry(p, struct hdpvr_buffer, buff_list);
  76. urb = buf->urb;
  77. usb_buffer_free(urb->dev, urb->transfer_buffer_length,
  78. urb->transfer_buffer, urb->transfer_dma);
  79. usb_free_urb(urb);
  80. tmp = p->next;
  81. list_del(p);
  82. kfree(buf);
  83. p = tmp;
  84. }
  85. return 0;
  86. }
  87. /* function expects dev->io_mutex to be hold by caller */
  88. int hdpvr_free_buffers(struct hdpvr_device *dev)
  89. {
  90. hdpvr_cancel_queue(dev);
  91. hdpvr_free_queue(&dev->free_buff_list);
  92. hdpvr_free_queue(&dev->rec_buff_list);
  93. return 0;
  94. }
  95. /* function expects dev->io_mutex to be hold by caller */
  96. int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
  97. {
  98. uint i;
  99. int retval = -ENOMEM;
  100. u8 *mem;
  101. struct hdpvr_buffer *buf;
  102. struct urb *urb;
  103. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  104. "allocating %u buffers\n", count);
  105. for (i = 0; i < count; i++) {
  106. buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
  107. if (!buf) {
  108. v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
  109. goto exit;
  110. }
  111. buf->dev = dev;
  112. urb = usb_alloc_urb(0, GFP_KERNEL);
  113. if (!urb) {
  114. v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
  115. goto exit;
  116. }
  117. buf->urb = urb;
  118. mem = usb_buffer_alloc(dev->udev, dev->bulk_in_size, GFP_KERNEL,
  119. &urb->transfer_dma);
  120. if (!mem) {
  121. v4l2_err(&dev->v4l2_dev,
  122. "cannot allocate usb transfer buffer\n");
  123. goto exit;
  124. }
  125. usb_fill_bulk_urb(buf->urb, dev->udev,
  126. usb_rcvbulkpipe(dev->udev,
  127. dev->bulk_in_endpointAddr),
  128. mem, dev->bulk_in_size,
  129. hdpvr_read_bulk_callback, buf);
  130. buf->status = BUFSTAT_AVAILABLE;
  131. list_add_tail(&buf->buff_list, &dev->free_buff_list);
  132. }
  133. return 0;
  134. exit:
  135. hdpvr_free_buffers(dev);
  136. return retval;
  137. }
  138. static int hdpvr_submit_buffers(struct hdpvr_device *dev)
  139. {
  140. struct hdpvr_buffer *buf;
  141. struct urb *urb;
  142. int ret = 0, err_count = 0;
  143. mutex_lock(&dev->io_mutex);
  144. while (dev->status == STATUS_STREAMING &&
  145. !list_empty(&dev->free_buff_list)) {
  146. buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
  147. buff_list);
  148. if (buf->status != BUFSTAT_AVAILABLE) {
  149. v4l2_err(&dev->v4l2_dev,
  150. "buffer not marked as available\n");
  151. ret = -EFAULT;
  152. goto err;
  153. }
  154. urb = buf->urb;
  155. urb->status = 0;
  156. urb->actual_length = 0;
  157. ret = usb_submit_urb(urb, GFP_KERNEL);
  158. if (ret) {
  159. v4l2_err(&dev->v4l2_dev,
  160. "usb_submit_urb in %s returned %d\n",
  161. __func__, ret);
  162. if (++err_count > 2)
  163. break;
  164. continue;
  165. }
  166. buf->status = BUFSTAT_INPROGRESS;
  167. list_move_tail(&buf->buff_list, &dev->rec_buff_list);
  168. }
  169. err:
  170. print_buffer_status();
  171. mutex_unlock(&dev->io_mutex);
  172. return ret;
  173. }
  174. static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
  175. {
  176. struct hdpvr_buffer *buf;
  177. mutex_lock(&dev->io_mutex);
  178. if (list_empty(&dev->rec_buff_list)) {
  179. mutex_unlock(&dev->io_mutex);
  180. return NULL;
  181. }
  182. buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
  183. buff_list);
  184. mutex_unlock(&dev->io_mutex);
  185. return buf;
  186. }
  187. static void hdpvr_transmit_buffers(struct work_struct *work)
  188. {
  189. struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
  190. worker);
  191. while (dev->status == STATUS_STREAMING) {
  192. if (hdpvr_submit_buffers(dev)) {
  193. v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
  194. goto error;
  195. }
  196. if (wait_event_interruptible(dev->wait_buffer,
  197. !list_empty(&dev->free_buff_list) ||
  198. dev->status != STATUS_STREAMING))
  199. goto error;
  200. }
  201. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  202. "transmit worker exited\n");
  203. return;
  204. error:
  205. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  206. "transmit buffers errored\n");
  207. dev->status = STATUS_ERROR;
  208. }
  209. /* function expects dev->io_mutex to be hold by caller */
  210. static int hdpvr_start_streaming(struct hdpvr_device *dev)
  211. {
  212. int ret;
  213. struct hdpvr_video_info *vidinf;
  214. if (dev->status == STATUS_STREAMING)
  215. return 0;
  216. else if (dev->status != STATUS_IDLE)
  217. return -EAGAIN;
  218. vidinf = get_video_info(dev);
  219. if (vidinf) {
  220. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  221. "video signal: %dx%d@%dhz\n", vidinf->width,
  222. vidinf->height, vidinf->fps);
  223. kfree(vidinf);
  224. /* start streaming 2 request */
  225. ret = usb_control_msg(dev->udev,
  226. usb_sndctrlpipe(dev->udev, 0),
  227. 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
  228. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  229. "encoder start control request returned %d\n", ret);
  230. hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
  231. INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
  232. queue_work(dev->workqueue, &dev->worker);
  233. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  234. "streaming started\n");
  235. dev->status = STATUS_STREAMING;
  236. return 0;
  237. }
  238. msleep(250);
  239. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  240. "no video signal at input %d\n", dev->options.video_input);
  241. return -EAGAIN;
  242. }
  243. /* function expects dev->io_mutex to be hold by caller */
  244. static int hdpvr_stop_streaming(struct hdpvr_device *dev)
  245. {
  246. uint actual_length, c = 0;
  247. u8 *buf;
  248. if (dev->status == STATUS_IDLE)
  249. return 0;
  250. else if (dev->status != STATUS_STREAMING)
  251. return -EAGAIN;
  252. buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
  253. if (!buf)
  254. v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
  255. "for emptying the internal device buffer. "
  256. "Next capture start will be slow\n");
  257. dev->status = STATUS_SHUTTING_DOWN;
  258. hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
  259. mutex_unlock(&dev->io_mutex);
  260. wake_up_interruptible(&dev->wait_buffer);
  261. msleep(50);
  262. flush_workqueue(dev->workqueue);
  263. mutex_lock(&dev->io_mutex);
  264. /* kill the still outstanding urbs */
  265. hdpvr_cancel_queue(dev);
  266. /* emptying the device buffer beforeshutting it down */
  267. while (buf && ++c < 500 &&
  268. !usb_bulk_msg(dev->udev,
  269. usb_rcvbulkpipe(dev->udev,
  270. dev->bulk_in_endpointAddr),
  271. buf, dev->bulk_in_size, &actual_length,
  272. BULK_URB_TIMEOUT)) {
  273. /* wait */
  274. msleep(5);
  275. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  276. "%2d: got %d bytes\n", c, actual_length);
  277. }
  278. kfree(buf);
  279. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  280. "used %d urbs to empty device buffers\n", c-1);
  281. msleep(10);
  282. dev->status = STATUS_IDLE;
  283. return 0;
  284. }
  285. /*=======================================================================*/
  286. /*
  287. * video 4 linux 2 file operations
  288. */
  289. static int hdpvr_open(struct file *file)
  290. {
  291. struct hdpvr_device *dev;
  292. struct hdpvr_fh *fh;
  293. int retval = -ENOMEM;
  294. dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
  295. if (!dev) {
  296. v4l2_err(&dev->v4l2_dev, "open failing with with ENODEV\n");
  297. retval = -ENODEV;
  298. goto err;
  299. }
  300. fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
  301. if (!fh) {
  302. v4l2_err(&dev->v4l2_dev, "Out of memory\n");
  303. goto err;
  304. }
  305. /* lock the device to allow correctly handling errors
  306. * in resumption */
  307. mutex_lock(&dev->io_mutex);
  308. dev->open_count++;
  309. mutex_unlock(&dev->io_mutex);
  310. fh->dev = dev;
  311. /* save our object in the file's private structure */
  312. file->private_data = fh;
  313. retval = 0;
  314. err:
  315. return retval;
  316. }
  317. static int hdpvr_release(struct file *file)
  318. {
  319. struct hdpvr_fh *fh = (struct hdpvr_fh *)file->private_data;
  320. struct hdpvr_device *dev = fh->dev;
  321. if (!dev)
  322. return -ENODEV;
  323. mutex_lock(&dev->io_mutex);
  324. if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
  325. hdpvr_stop_streaming(dev);
  326. mutex_unlock(&dev->io_mutex);
  327. return 0;
  328. }
  329. /*
  330. * hdpvr_v4l2_read()
  331. * will allocate buffers when called for the first time
  332. */
  333. static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
  334. loff_t *pos)
  335. {
  336. struct hdpvr_fh *fh = file->private_data;
  337. struct hdpvr_device *dev = fh->dev;
  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. if (!dev)
  345. return -ENODEV;
  346. mutex_lock(&dev->io_mutex);
  347. if (dev->status == STATUS_IDLE) {
  348. if (hdpvr_start_streaming(dev)) {
  349. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  350. "start_streaming failed\n");
  351. ret = -EIO;
  352. msleep(200);
  353. dev->status = STATUS_IDLE;
  354. mutex_unlock(&dev->io_mutex);
  355. goto err;
  356. }
  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. hdpvr_get_next_buffer(dev)))
  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. /* return nonblocking */
  371. if (file->f_flags & O_NONBLOCK) {
  372. if (!ret)
  373. ret = -EAGAIN;
  374. goto err;
  375. }
  376. if (wait_event_interruptible(dev->wait_data,
  377. buf->status == BUFSTAT_READY)) {
  378. ret = -ERESTARTSYS;
  379. goto err;
  380. }
  381. }
  382. if (buf->status != BUFSTAT_READY)
  383. break;
  384. /* set remaining bytes to copy */
  385. urb = buf->urb;
  386. rem = urb->actual_length - buf->pos;
  387. cnt = rem > count ? count : rem;
  388. if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
  389. cnt)) {
  390. v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
  391. if (!ret)
  392. ret = -EFAULT;
  393. goto err;
  394. }
  395. buf->pos += cnt;
  396. count -= cnt;
  397. buffer += cnt;
  398. ret += cnt;
  399. /* finished, take next buffer */
  400. if (buf->pos == urb->actual_length) {
  401. mutex_lock(&dev->io_mutex);
  402. buf->pos = 0;
  403. buf->status = BUFSTAT_AVAILABLE;
  404. list_move_tail(&buf->buff_list, &dev->free_buff_list);
  405. print_buffer_status();
  406. mutex_unlock(&dev->io_mutex);
  407. wake_up_interruptible(&dev->wait_buffer);
  408. buf = hdpvr_get_next_buffer(dev);
  409. }
  410. }
  411. err:
  412. if (!ret && !buf)
  413. ret = -EAGAIN;
  414. return ret;
  415. }
  416. static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
  417. {
  418. struct hdpvr_buffer *buf = NULL;
  419. struct hdpvr_fh *fh = (struct hdpvr_fh *)filp->private_data;
  420. struct hdpvr_device *dev = fh->dev;
  421. unsigned int mask = 0;
  422. mutex_lock(&dev->io_mutex);
  423. if (video_is_unregistered(dev->video_dev)) {
  424. mutex_unlock(&dev->io_mutex);
  425. return -EIO;
  426. }
  427. if (dev->status == STATUS_IDLE) {
  428. if (hdpvr_start_streaming(dev)) {
  429. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  430. "start_streaming failed\n");
  431. dev->status = STATUS_IDLE;
  432. }
  433. print_buffer_status();
  434. }
  435. mutex_unlock(&dev->io_mutex);
  436. buf = hdpvr_get_next_buffer(dev);
  437. /* only wait if no data is available */
  438. if (!buf || buf->status != BUFSTAT_READY) {
  439. poll_wait(filp, &dev->wait_data, wait);
  440. buf = hdpvr_get_next_buffer(dev);
  441. }
  442. if (buf && buf->status == BUFSTAT_READY)
  443. mask |= POLLIN | POLLRDNORM;
  444. return mask;
  445. }
  446. static const struct v4l2_file_operations hdpvr_fops = {
  447. .owner = THIS_MODULE,
  448. .open = hdpvr_open,
  449. .release = hdpvr_release,
  450. .read = hdpvr_read,
  451. .poll = hdpvr_poll,
  452. .unlocked_ioctl = video_ioctl2,
  453. };
  454. /*=======================================================================*/
  455. /*
  456. * V4L2 ioctl handling
  457. */
  458. static int vidioc_querycap(struct file *file, void *priv,
  459. struct v4l2_capability *cap)
  460. {
  461. struct hdpvr_device *dev = video_drvdata(file);
  462. strcpy(cap->driver, "hdpvr");
  463. strcpy(cap->card, "Haupauge HD PVR");
  464. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  465. cap->version = HDPVR_VERSION;
  466. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  467. V4L2_CAP_AUDIO |
  468. V4L2_CAP_READWRITE;
  469. return 0;
  470. }
  471. static int vidioc_s_std(struct file *file, void *private_data,
  472. v4l2_std_id *std)
  473. {
  474. struct hdpvr_fh *fh = file->private_data;
  475. struct hdpvr_device *dev = fh->dev;
  476. u8 std_type = 1;
  477. if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
  478. std_type = 0;
  479. return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
  480. }
  481. static const char *iname[] = {
  482. [HDPVR_COMPONENT] = "Component",
  483. [HDPVR_SVIDEO] = "S-Video",
  484. [HDPVR_COMPOSITE] = "Composite",
  485. };
  486. static int vidioc_enum_input(struct file *file, void *priv,
  487. struct v4l2_input *i)
  488. {
  489. struct hdpvr_fh *fh = file->private_data;
  490. struct hdpvr_device *dev = fh->dev;
  491. unsigned int n;
  492. n = i->index;
  493. if (n >= HDPVR_VIDEO_INPUTS)
  494. return -EINVAL;
  495. i->type = V4L2_INPUT_TYPE_CAMERA;
  496. strncpy(i->name, iname[n], sizeof(i->name) - 1);
  497. i->name[sizeof(i->name) - 1] = '\0';
  498. i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
  499. i->std = dev->video_dev->tvnorms;
  500. return 0;
  501. }
  502. static int vidioc_s_input(struct file *file, void *private_data,
  503. unsigned int index)
  504. {
  505. struct hdpvr_fh *fh = file->private_data;
  506. struct hdpvr_device *dev = fh->dev;
  507. int retval;
  508. if (index >= HDPVR_VIDEO_INPUTS)
  509. return -EINVAL;
  510. if (dev->status != STATUS_IDLE)
  511. return -EAGAIN;
  512. retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
  513. if (!retval)
  514. dev->options.video_input = index;
  515. return retval;
  516. }
  517. static int vidioc_g_input(struct file *file, void *private_data,
  518. unsigned int *index)
  519. {
  520. struct hdpvr_fh *fh = file->private_data;
  521. struct hdpvr_device *dev = fh->dev;
  522. *index = dev->options.video_input;
  523. return 0;
  524. }
  525. static const char *audio_iname[] = {
  526. [HDPVR_RCA_FRONT] = "RCA front",
  527. [HDPVR_RCA_BACK] = "RCA back",
  528. [HDPVR_SPDIF] = "SPDIF",
  529. };
  530. static int vidioc_enumaudio(struct file *file, void *priv,
  531. struct v4l2_audio *audio)
  532. {
  533. unsigned int n;
  534. n = audio->index;
  535. if (n >= HDPVR_AUDIO_INPUTS)
  536. return -EINVAL;
  537. audio->capability = V4L2_AUDCAP_STEREO;
  538. strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
  539. audio->name[sizeof(audio->name) - 1] = '\0';
  540. return 0;
  541. }
  542. static int vidioc_s_audio(struct file *file, void *private_data,
  543. struct v4l2_audio *audio)
  544. {
  545. struct hdpvr_fh *fh = file->private_data;
  546. struct hdpvr_device *dev = fh->dev;
  547. int retval;
  548. if (audio->index >= HDPVR_AUDIO_INPUTS)
  549. return -EINVAL;
  550. if (dev->status != STATUS_IDLE)
  551. return -EAGAIN;
  552. retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
  553. if (!retval)
  554. dev->options.audio_input = audio->index;
  555. return retval;
  556. }
  557. static int vidioc_g_audio(struct file *file, void *private_data,
  558. struct v4l2_audio *audio)
  559. {
  560. struct hdpvr_fh *fh = file->private_data;
  561. struct hdpvr_device *dev = fh->dev;
  562. audio->index = dev->options.audio_input;
  563. audio->capability = V4L2_AUDCAP_STEREO;
  564. strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
  565. audio->name[sizeof(audio->name) - 1] = '\0';
  566. return 0;
  567. }
  568. static const s32 supported_v4l2_ctrls[] = {
  569. V4L2_CID_BRIGHTNESS,
  570. V4L2_CID_CONTRAST,
  571. V4L2_CID_SATURATION,
  572. V4L2_CID_HUE,
  573. V4L2_CID_SHARPNESS,
  574. V4L2_CID_MPEG_AUDIO_ENCODING,
  575. V4L2_CID_MPEG_VIDEO_ENCODING,
  576. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  577. V4L2_CID_MPEG_VIDEO_BITRATE,
  578. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  579. };
  580. static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
  581. int ac3)
  582. {
  583. int err;
  584. switch (qc->id) {
  585. case V4L2_CID_BRIGHTNESS:
  586. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
  587. case V4L2_CID_CONTRAST:
  588. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  589. case V4L2_CID_SATURATION:
  590. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  591. case V4L2_CID_HUE:
  592. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  593. case V4L2_CID_SHARPNESS:
  594. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  595. case V4L2_CID_MPEG_AUDIO_ENCODING:
  596. return v4l2_ctrl_query_fill(
  597. qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
  598. ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
  599. : V4L2_MPEG_AUDIO_ENCODING_AAC,
  600. 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
  601. case V4L2_CID_MPEG_VIDEO_ENCODING:
  602. return v4l2_ctrl_query_fill(
  603. qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
  604. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
  605. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  606. /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
  607. /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
  608. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  609. return v4l2_ctrl_query_fill(
  610. qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  611. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
  612. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  613. case V4L2_CID_MPEG_VIDEO_BITRATE:
  614. return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
  615. 6500000);
  616. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  617. err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
  618. 9000000);
  619. if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
  620. qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
  621. return err;
  622. default:
  623. return -EINVAL;
  624. }
  625. }
  626. static int vidioc_queryctrl(struct file *file, void *private_data,
  627. struct v4l2_queryctrl *qc)
  628. {
  629. struct hdpvr_fh *fh = file->private_data;
  630. struct hdpvr_device *dev = fh->dev;
  631. int i, next;
  632. u32 id = qc->id;
  633. memset(qc, 0, sizeof(*qc));
  634. next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
  635. qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
  636. for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
  637. if (next) {
  638. if (qc->id < supported_v4l2_ctrls[i])
  639. qc->id = supported_v4l2_ctrls[i];
  640. else
  641. continue;
  642. }
  643. if (qc->id == supported_v4l2_ctrls[i])
  644. return fill_queryctrl(&dev->options, qc,
  645. dev->flags & HDPVR_FLAG_AC3_CAP);
  646. if (qc->id < supported_v4l2_ctrls[i])
  647. break;
  648. }
  649. return -EINVAL;
  650. }
  651. static int vidioc_g_ctrl(struct file *file, void *private_data,
  652. struct v4l2_control *ctrl)
  653. {
  654. struct hdpvr_fh *fh = file->private_data;
  655. struct hdpvr_device *dev = fh->dev;
  656. switch (ctrl->id) {
  657. case V4L2_CID_BRIGHTNESS:
  658. ctrl->value = dev->options.brightness;
  659. break;
  660. case V4L2_CID_CONTRAST:
  661. ctrl->value = dev->options.contrast;
  662. break;
  663. case V4L2_CID_SATURATION:
  664. ctrl->value = dev->options.saturation;
  665. break;
  666. case V4L2_CID_HUE:
  667. ctrl->value = dev->options.hue;
  668. break;
  669. case V4L2_CID_SHARPNESS:
  670. ctrl->value = dev->options.sharpness;
  671. break;
  672. default:
  673. return -EINVAL;
  674. }
  675. return 0;
  676. }
  677. static int vidioc_s_ctrl(struct file *file, void *private_data,
  678. struct v4l2_control *ctrl)
  679. {
  680. struct hdpvr_fh *fh = file->private_data;
  681. struct hdpvr_device *dev = fh->dev;
  682. int retval;
  683. switch (ctrl->id) {
  684. case V4L2_CID_BRIGHTNESS:
  685. retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
  686. if (!retval)
  687. dev->options.brightness = ctrl->value;
  688. break;
  689. case V4L2_CID_CONTRAST:
  690. retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
  691. if (!retval)
  692. dev->options.contrast = ctrl->value;
  693. break;
  694. case V4L2_CID_SATURATION:
  695. retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
  696. if (!retval)
  697. dev->options.saturation = ctrl->value;
  698. break;
  699. case V4L2_CID_HUE:
  700. retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
  701. if (!retval)
  702. dev->options.hue = ctrl->value;
  703. break;
  704. case V4L2_CID_SHARPNESS:
  705. retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
  706. if (!retval)
  707. dev->options.sharpness = ctrl->value;
  708. break;
  709. default:
  710. return -EINVAL;
  711. }
  712. return retval;
  713. }
  714. static int hdpvr_get_ctrl(struct hdpvr_options *opt,
  715. struct v4l2_ext_control *ctrl)
  716. {
  717. switch (ctrl->id) {
  718. case V4L2_CID_MPEG_AUDIO_ENCODING:
  719. ctrl->value = opt->audio_codec;
  720. break;
  721. case V4L2_CID_MPEG_VIDEO_ENCODING:
  722. ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
  723. break;
  724. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  725. /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
  726. /* break; */
  727. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  728. ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
  729. ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
  730. : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
  731. break;
  732. case V4L2_CID_MPEG_VIDEO_BITRATE:
  733. ctrl->value = opt->bitrate * 100000;
  734. break;
  735. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  736. ctrl->value = opt->peak_bitrate * 100000;
  737. break;
  738. case V4L2_CID_MPEG_STREAM_TYPE:
  739. ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
  740. break;
  741. default:
  742. return -EINVAL;
  743. }
  744. return 0;
  745. }
  746. static int vidioc_g_ext_ctrls(struct file *file, void *priv,
  747. struct v4l2_ext_controls *ctrls)
  748. {
  749. struct hdpvr_fh *fh = file->private_data;
  750. struct hdpvr_device *dev = fh->dev;
  751. int i, err = 0;
  752. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  753. for (i = 0; i < ctrls->count; i++) {
  754. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  755. err = hdpvr_get_ctrl(&dev->options, ctrl);
  756. if (err) {
  757. ctrls->error_idx = i;
  758. break;
  759. }
  760. }
  761. return err;
  762. }
  763. return -EINVAL;
  764. }
  765. static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
  766. {
  767. int ret = -EINVAL;
  768. switch (ctrl->id) {
  769. case V4L2_CID_MPEG_AUDIO_ENCODING:
  770. if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
  771. (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
  772. ret = 0;
  773. break;
  774. case V4L2_CID_MPEG_VIDEO_ENCODING:
  775. if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
  776. ret = 0;
  777. break;
  778. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  779. /* if (ctrl->value == 0 || ctrl->value == 128) */
  780. /* ret = 0; */
  781. /* break; */
  782. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  783. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
  784. ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
  785. ret = 0;
  786. break;
  787. case V4L2_CID_MPEG_VIDEO_BITRATE:
  788. {
  789. uint bitrate = ctrl->value / 100000;
  790. if (bitrate >= 10 && bitrate <= 135)
  791. ret = 0;
  792. break;
  793. }
  794. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  795. {
  796. uint peak_bitrate = ctrl->value / 100000;
  797. if (peak_bitrate >= 10 && peak_bitrate <= 202)
  798. ret = 0;
  799. break;
  800. }
  801. case V4L2_CID_MPEG_STREAM_TYPE:
  802. if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
  803. ret = 0;
  804. break;
  805. default:
  806. return -EINVAL;
  807. }
  808. return 0;
  809. }
  810. static int vidioc_try_ext_ctrls(struct file *file, void *priv,
  811. struct v4l2_ext_controls *ctrls)
  812. {
  813. struct hdpvr_fh *fh = file->private_data;
  814. struct hdpvr_device *dev = fh->dev;
  815. int i, err = 0;
  816. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  817. for (i = 0; i < ctrls->count; i++) {
  818. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  819. err = hdpvr_try_ctrl(ctrl,
  820. dev->flags & HDPVR_FLAG_AC3_CAP);
  821. if (err) {
  822. ctrls->error_idx = i;
  823. break;
  824. }
  825. }
  826. return err;
  827. }
  828. return -EINVAL;
  829. }
  830. static int hdpvr_set_ctrl(struct hdpvr_device *dev,
  831. struct v4l2_ext_control *ctrl)
  832. {
  833. struct hdpvr_options *opt = &dev->options;
  834. int ret = 0;
  835. switch (ctrl->id) {
  836. case V4L2_CID_MPEG_AUDIO_ENCODING:
  837. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  838. opt->audio_codec = ctrl->value;
  839. ret = hdpvr_set_audio(dev, opt->audio_input,
  840. opt->audio_codec);
  841. }
  842. break;
  843. case V4L2_CID_MPEG_VIDEO_ENCODING:
  844. break;
  845. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  846. /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
  847. /* opt->gop_mode |= 0x2; */
  848. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  849. /* opt->gop_mode); */
  850. /* } */
  851. /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
  852. /* opt->gop_mode &= ~0x2; */
  853. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  854. /* opt->gop_mode); */
  855. /* } */
  856. /* break; */
  857. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  858. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
  859. opt->bitrate_mode != HDPVR_CONSTANT) {
  860. opt->bitrate_mode = HDPVR_CONSTANT;
  861. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  862. opt->bitrate_mode);
  863. }
  864. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  865. opt->bitrate_mode == HDPVR_CONSTANT) {
  866. opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
  867. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  868. opt->bitrate_mode);
  869. }
  870. break;
  871. case V4L2_CID_MPEG_VIDEO_BITRATE: {
  872. uint bitrate = ctrl->value / 100000;
  873. opt->bitrate = bitrate;
  874. if (bitrate >= opt->peak_bitrate)
  875. opt->peak_bitrate = bitrate+1;
  876. hdpvr_set_bitrate(dev);
  877. break;
  878. }
  879. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
  880. uint peak_bitrate = ctrl->value / 100000;
  881. if (opt->bitrate_mode == HDPVR_CONSTANT)
  882. break;
  883. if (opt->bitrate < peak_bitrate) {
  884. opt->peak_bitrate = peak_bitrate;
  885. hdpvr_set_bitrate(dev);
  886. } else
  887. ret = -EINVAL;
  888. break;
  889. }
  890. case V4L2_CID_MPEG_STREAM_TYPE:
  891. break;
  892. default:
  893. return -EINVAL;
  894. }
  895. return ret;
  896. }
  897. static int vidioc_s_ext_ctrls(struct file *file, void *priv,
  898. struct v4l2_ext_controls *ctrls)
  899. {
  900. struct hdpvr_fh *fh = file->private_data;
  901. struct hdpvr_device *dev = fh->dev;
  902. int i, err = 0;
  903. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  904. for (i = 0; i < ctrls->count; i++) {
  905. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  906. err = hdpvr_try_ctrl(ctrl,
  907. dev->flags & HDPVR_FLAG_AC3_CAP);
  908. if (err) {
  909. ctrls->error_idx = i;
  910. break;
  911. }
  912. err = hdpvr_set_ctrl(dev, ctrl);
  913. if (err) {
  914. ctrls->error_idx = i;
  915. break;
  916. }
  917. }
  918. return err;
  919. }
  920. return -EINVAL;
  921. }
  922. static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
  923. struct v4l2_fmtdesc *f)
  924. {
  925. if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  926. return -EINVAL;
  927. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  928. strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
  929. f->pixelformat = V4L2_PIX_FMT_MPEG;
  930. return 0;
  931. }
  932. static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
  933. struct v4l2_format *f)
  934. {
  935. struct hdpvr_fh *fh = file->private_data;
  936. struct hdpvr_device *dev = fh->dev;
  937. struct hdpvr_video_info *vid_info;
  938. if (!dev)
  939. return -ENODEV;
  940. vid_info = get_video_info(dev);
  941. if (!vid_info)
  942. return -EFAULT;
  943. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  944. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  945. f->fmt.pix.width = vid_info->width;
  946. f->fmt.pix.height = vid_info->height;
  947. f->fmt.pix.sizeimage = dev->bulk_in_size;
  948. f->fmt.pix.colorspace = 0;
  949. f->fmt.pix.bytesperline = 0;
  950. f->fmt.pix.field = V4L2_FIELD_ANY;
  951. kfree(vid_info);
  952. return 0;
  953. }
  954. static int vidioc_encoder_cmd(struct file *filp, void *priv,
  955. struct v4l2_encoder_cmd *a)
  956. {
  957. struct hdpvr_fh *fh = filp->private_data;
  958. struct hdpvr_device *dev = fh->dev;
  959. int res;
  960. mutex_lock(&dev->io_mutex);
  961. memset(&a->raw, 0, sizeof(a->raw));
  962. switch (a->cmd) {
  963. case V4L2_ENC_CMD_START:
  964. a->flags = 0;
  965. res = hdpvr_start_streaming(dev);
  966. break;
  967. case V4L2_ENC_CMD_STOP:
  968. res = hdpvr_stop_streaming(dev);
  969. break;
  970. default:
  971. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  972. "Unsupported encoder cmd %d\n", a->cmd);
  973. res = -EINVAL;
  974. }
  975. mutex_unlock(&dev->io_mutex);
  976. return res;
  977. }
  978. static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
  979. struct v4l2_encoder_cmd *a)
  980. {
  981. switch (a->cmd) {
  982. case V4L2_ENC_CMD_START:
  983. case V4L2_ENC_CMD_STOP:
  984. return 0;
  985. default:
  986. return -EINVAL;
  987. }
  988. }
  989. static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
  990. .vidioc_querycap = vidioc_querycap,
  991. .vidioc_s_std = vidioc_s_std,
  992. .vidioc_enum_input = vidioc_enum_input,
  993. .vidioc_g_input = vidioc_g_input,
  994. .vidioc_s_input = vidioc_s_input,
  995. .vidioc_enumaudio = vidioc_enumaudio,
  996. .vidioc_g_audio = vidioc_g_audio,
  997. .vidioc_s_audio = vidioc_s_audio,
  998. .vidioc_queryctrl = vidioc_queryctrl,
  999. .vidioc_g_ctrl = vidioc_g_ctrl,
  1000. .vidioc_s_ctrl = vidioc_s_ctrl,
  1001. .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
  1002. .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
  1003. .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
  1004. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1005. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1006. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  1007. .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
  1008. };
  1009. static void hdpvr_device_release(struct video_device *vdev)
  1010. {
  1011. struct hdpvr_device *dev = video_get_drvdata(vdev);
  1012. hdpvr_delete(dev);
  1013. }
  1014. static const struct video_device hdpvr_video_template = {
  1015. /* .type = VFL_TYPE_GRABBER, */
  1016. /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
  1017. .fops = &hdpvr_fops,
  1018. .release = hdpvr_device_release,
  1019. .ioctl_ops = &hdpvr_ioctl_ops,
  1020. .tvnorms =
  1021. V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
  1022. V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
  1023. V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
  1024. V4L2_STD_PAL_60,
  1025. .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |
  1026. V4L2_STD_PAL_60,
  1027. };
  1028. int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
  1029. int devnum)
  1030. {
  1031. /* setup and register video device */
  1032. dev->video_dev = video_device_alloc();
  1033. if (!dev->video_dev) {
  1034. v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
  1035. goto error;
  1036. }
  1037. *(dev->video_dev) = hdpvr_video_template;
  1038. strcpy(dev->video_dev->name, "Hauppauge HD PVR");
  1039. dev->video_dev->parent = parent;
  1040. video_set_drvdata(dev->video_dev, dev);
  1041. if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
  1042. v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
  1043. goto error;
  1044. }
  1045. return 0;
  1046. error:
  1047. return -ENOMEM;
  1048. }