hdpvr-video.c 30 KB

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