hdpvr-video.c 30 KB

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