hdpvr-video.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. fh->dev = dev;
  310. /* save our object in the file's private structure */
  311. file->private_data = fh;
  312. retval = 0;
  313. err:
  314. mutex_unlock(&dev->io_mutex);
  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. return -EIO;
  425. if (dev->status == STATUS_IDLE) {
  426. if (hdpvr_start_streaming(dev)) {
  427. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  428. "start_streaming failed\n");
  429. dev->status = STATUS_IDLE;
  430. }
  431. print_buffer_status();
  432. }
  433. mutex_unlock(&dev->io_mutex);
  434. buf = hdpvr_get_next_buffer(dev);
  435. /* only wait if no data is available */
  436. if (!buf || buf->status != BUFSTAT_READY) {
  437. poll_wait(filp, &dev->wait_data, wait);
  438. buf = hdpvr_get_next_buffer(dev);
  439. }
  440. if (buf && buf->status == BUFSTAT_READY)
  441. mask |= POLLIN | POLLRDNORM;
  442. return mask;
  443. }
  444. static const struct v4l2_file_operations hdpvr_fops = {
  445. .owner = THIS_MODULE,
  446. .open = hdpvr_open,
  447. .release = hdpvr_release,
  448. .read = hdpvr_read,
  449. .poll = hdpvr_poll,
  450. .unlocked_ioctl = video_ioctl2,
  451. };
  452. /*=======================================================================*/
  453. /*
  454. * V4L2 ioctl handling
  455. */
  456. static int vidioc_querycap(struct file *file, void *priv,
  457. struct v4l2_capability *cap)
  458. {
  459. struct hdpvr_device *dev = video_drvdata(file);
  460. strcpy(cap->driver, "hdpvr");
  461. strcpy(cap->card, "Haupauge HD PVR");
  462. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  463. cap->version = HDPVR_VERSION;
  464. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  465. V4L2_CAP_AUDIO |
  466. V4L2_CAP_READWRITE;
  467. return 0;
  468. }
  469. static int vidioc_s_std(struct file *file, void *private_data,
  470. v4l2_std_id *std)
  471. {
  472. struct hdpvr_fh *fh = file->private_data;
  473. struct hdpvr_device *dev = fh->dev;
  474. u8 std_type = 1;
  475. if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
  476. std_type = 0;
  477. return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
  478. }
  479. static const char *iname[] = {
  480. [HDPVR_COMPONENT] = "Component",
  481. [HDPVR_SVIDEO] = "S-Video",
  482. [HDPVR_COMPOSITE] = "Composite",
  483. };
  484. static int vidioc_enum_input(struct file *file, void *priv,
  485. struct v4l2_input *i)
  486. {
  487. struct hdpvr_fh *fh = file->private_data;
  488. struct hdpvr_device *dev = fh->dev;
  489. unsigned int n;
  490. n = i->index;
  491. if (n >= HDPVR_VIDEO_INPUTS)
  492. return -EINVAL;
  493. i->type = V4L2_INPUT_TYPE_CAMERA;
  494. strncpy(i->name, iname[n], sizeof(i->name) - 1);
  495. i->name[sizeof(i->name) - 1] = '\0';
  496. i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
  497. i->std = dev->video_dev->tvnorms;
  498. return 0;
  499. }
  500. static int vidioc_s_input(struct file *file, void *private_data,
  501. unsigned int index)
  502. {
  503. struct hdpvr_fh *fh = file->private_data;
  504. struct hdpvr_device *dev = fh->dev;
  505. int retval;
  506. if (index >= HDPVR_VIDEO_INPUTS)
  507. return -EINVAL;
  508. if (dev->status != STATUS_IDLE)
  509. return -EAGAIN;
  510. retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
  511. if (!retval)
  512. dev->options.video_input = index;
  513. return retval;
  514. }
  515. static int vidioc_g_input(struct file *file, void *private_data,
  516. unsigned int *index)
  517. {
  518. struct hdpvr_fh *fh = file->private_data;
  519. struct hdpvr_device *dev = fh->dev;
  520. *index = dev->options.video_input;
  521. return 0;
  522. }
  523. static const char *audio_iname[] = {
  524. [HDPVR_RCA_FRONT] = "RCA front",
  525. [HDPVR_RCA_BACK] = "RCA back",
  526. [HDPVR_SPDIF] = "SPDIF",
  527. };
  528. static int vidioc_enumaudio(struct file *file, void *priv,
  529. struct v4l2_audio *audio)
  530. {
  531. unsigned int n;
  532. n = audio->index;
  533. if (n >= HDPVR_AUDIO_INPUTS)
  534. return -EINVAL;
  535. audio->capability = V4L2_AUDCAP_STEREO;
  536. strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
  537. audio->name[sizeof(audio->name) - 1] = '\0';
  538. return 0;
  539. }
  540. static int vidioc_s_audio(struct file *file, void *private_data,
  541. struct v4l2_audio *audio)
  542. {
  543. struct hdpvr_fh *fh = file->private_data;
  544. struct hdpvr_device *dev = fh->dev;
  545. int retval;
  546. if (audio->index >= HDPVR_AUDIO_INPUTS)
  547. return -EINVAL;
  548. if (dev->status != STATUS_IDLE)
  549. return -EAGAIN;
  550. retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
  551. if (!retval)
  552. dev->options.audio_input = audio->index;
  553. return retval;
  554. }
  555. static int vidioc_g_audio(struct file *file, void *private_data,
  556. struct v4l2_audio *audio)
  557. {
  558. struct hdpvr_fh *fh = file->private_data;
  559. struct hdpvr_device *dev = fh->dev;
  560. audio->index = dev->options.audio_input;
  561. audio->capability = V4L2_AUDCAP_STEREO;
  562. strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
  563. audio->name[sizeof(audio->name) - 1] = '\0';
  564. return 0;
  565. }
  566. static const s32 supported_v4l2_ctrls[] = {
  567. V4L2_CID_BRIGHTNESS,
  568. V4L2_CID_CONTRAST,
  569. V4L2_CID_SATURATION,
  570. V4L2_CID_HUE,
  571. V4L2_CID_SHARPNESS,
  572. V4L2_CID_MPEG_AUDIO_ENCODING,
  573. V4L2_CID_MPEG_VIDEO_ENCODING,
  574. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  575. V4L2_CID_MPEG_VIDEO_BITRATE,
  576. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  577. };
  578. static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
  579. int ac3)
  580. {
  581. int err;
  582. switch (qc->id) {
  583. case V4L2_CID_BRIGHTNESS:
  584. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
  585. case V4L2_CID_CONTRAST:
  586. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  587. case V4L2_CID_SATURATION:
  588. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  589. case V4L2_CID_HUE:
  590. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  591. case V4L2_CID_SHARPNESS:
  592. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  593. case V4L2_CID_MPEG_AUDIO_ENCODING:
  594. return v4l2_ctrl_query_fill(
  595. qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
  596. ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
  597. : V4L2_MPEG_AUDIO_ENCODING_AAC,
  598. 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
  599. case V4L2_CID_MPEG_VIDEO_ENCODING:
  600. return v4l2_ctrl_query_fill(
  601. qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
  602. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
  603. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  604. /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
  605. /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
  606. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  607. return v4l2_ctrl_query_fill(
  608. qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  609. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
  610. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  611. case V4L2_CID_MPEG_VIDEO_BITRATE:
  612. return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
  613. 6500000);
  614. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  615. err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
  616. 9000000);
  617. if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
  618. qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
  619. return err;
  620. default:
  621. return -EINVAL;
  622. }
  623. }
  624. static int vidioc_queryctrl(struct file *file, void *private_data,
  625. struct v4l2_queryctrl *qc)
  626. {
  627. struct hdpvr_fh *fh = file->private_data;
  628. struct hdpvr_device *dev = fh->dev;
  629. int i, next;
  630. u32 id = qc->id;
  631. memset(qc, 0, sizeof(*qc));
  632. next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
  633. qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
  634. for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
  635. if (next) {
  636. if (qc->id < supported_v4l2_ctrls[i])
  637. qc->id = supported_v4l2_ctrls[i];
  638. else
  639. continue;
  640. }
  641. if (qc->id == supported_v4l2_ctrls[i])
  642. return fill_queryctrl(&dev->options, qc,
  643. dev->flags & HDPVR_FLAG_AC3_CAP);
  644. if (qc->id < supported_v4l2_ctrls[i])
  645. break;
  646. }
  647. return -EINVAL;
  648. }
  649. static int vidioc_g_ctrl(struct file *file, void *private_data,
  650. struct v4l2_control *ctrl)
  651. {
  652. struct hdpvr_fh *fh = file->private_data;
  653. struct hdpvr_device *dev = fh->dev;
  654. switch (ctrl->id) {
  655. case V4L2_CID_BRIGHTNESS:
  656. ctrl->value = dev->options.brightness;
  657. break;
  658. case V4L2_CID_CONTRAST:
  659. ctrl->value = dev->options.contrast;
  660. break;
  661. case V4L2_CID_SATURATION:
  662. ctrl->value = dev->options.saturation;
  663. break;
  664. case V4L2_CID_HUE:
  665. ctrl->value = dev->options.hue;
  666. break;
  667. case V4L2_CID_SHARPNESS:
  668. ctrl->value = dev->options.sharpness;
  669. break;
  670. default:
  671. return -EINVAL;
  672. }
  673. return 0;
  674. }
  675. static int vidioc_s_ctrl(struct file *file, void *private_data,
  676. struct v4l2_control *ctrl)
  677. {
  678. struct hdpvr_fh *fh = file->private_data;
  679. struct hdpvr_device *dev = fh->dev;
  680. int retval;
  681. switch (ctrl->id) {
  682. case V4L2_CID_BRIGHTNESS:
  683. retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
  684. if (!retval)
  685. dev->options.brightness = ctrl->value;
  686. break;
  687. case V4L2_CID_CONTRAST:
  688. retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
  689. if (!retval)
  690. dev->options.contrast = ctrl->value;
  691. break;
  692. case V4L2_CID_SATURATION:
  693. retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
  694. if (!retval)
  695. dev->options.saturation = ctrl->value;
  696. break;
  697. case V4L2_CID_HUE:
  698. retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
  699. if (!retval)
  700. dev->options.hue = ctrl->value;
  701. break;
  702. case V4L2_CID_SHARPNESS:
  703. retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
  704. if (!retval)
  705. dev->options.sharpness = ctrl->value;
  706. break;
  707. default:
  708. return -EINVAL;
  709. }
  710. return retval;
  711. }
  712. static int hdpvr_get_ctrl(struct hdpvr_options *opt,
  713. struct v4l2_ext_control *ctrl)
  714. {
  715. switch (ctrl->id) {
  716. case V4L2_CID_MPEG_AUDIO_ENCODING:
  717. ctrl->value = opt->audio_codec;
  718. break;
  719. case V4L2_CID_MPEG_VIDEO_ENCODING:
  720. ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
  721. break;
  722. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  723. /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
  724. /* break; */
  725. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  726. ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
  727. ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
  728. : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
  729. break;
  730. case V4L2_CID_MPEG_VIDEO_BITRATE:
  731. ctrl->value = opt->bitrate * 100000;
  732. break;
  733. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  734. ctrl->value = opt->peak_bitrate * 100000;
  735. break;
  736. case V4L2_CID_MPEG_STREAM_TYPE:
  737. ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
  738. break;
  739. default:
  740. return -EINVAL;
  741. }
  742. return 0;
  743. }
  744. static int vidioc_g_ext_ctrls(struct file *file, void *priv,
  745. struct v4l2_ext_controls *ctrls)
  746. {
  747. struct hdpvr_fh *fh = file->private_data;
  748. struct hdpvr_device *dev = fh->dev;
  749. int i, err = 0;
  750. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  751. for (i = 0; i < ctrls->count; i++) {
  752. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  753. err = hdpvr_get_ctrl(&dev->options, ctrl);
  754. if (err) {
  755. ctrls->error_idx = i;
  756. break;
  757. }
  758. }
  759. return err;
  760. }
  761. return -EINVAL;
  762. }
  763. static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
  764. {
  765. int ret = -EINVAL;
  766. switch (ctrl->id) {
  767. case V4L2_CID_MPEG_AUDIO_ENCODING:
  768. if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
  769. (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
  770. ret = 0;
  771. break;
  772. case V4L2_CID_MPEG_VIDEO_ENCODING:
  773. if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
  774. ret = 0;
  775. break;
  776. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  777. /* if (ctrl->value == 0 || ctrl->value == 128) */
  778. /* ret = 0; */
  779. /* break; */
  780. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  781. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
  782. ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
  783. ret = 0;
  784. break;
  785. case V4L2_CID_MPEG_VIDEO_BITRATE:
  786. {
  787. uint bitrate = ctrl->value / 100000;
  788. if (bitrate >= 10 && bitrate <= 135)
  789. ret = 0;
  790. break;
  791. }
  792. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  793. {
  794. uint peak_bitrate = ctrl->value / 100000;
  795. if (peak_bitrate >= 10 && peak_bitrate <= 202)
  796. ret = 0;
  797. break;
  798. }
  799. case V4L2_CID_MPEG_STREAM_TYPE:
  800. if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
  801. ret = 0;
  802. break;
  803. default:
  804. return -EINVAL;
  805. }
  806. return 0;
  807. }
  808. static int vidioc_try_ext_ctrls(struct file *file, void *priv,
  809. struct v4l2_ext_controls *ctrls)
  810. {
  811. struct hdpvr_fh *fh = file->private_data;
  812. struct hdpvr_device *dev = fh->dev;
  813. int i, err = 0;
  814. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  815. for (i = 0; i < ctrls->count; i++) {
  816. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  817. err = hdpvr_try_ctrl(ctrl,
  818. dev->flags & HDPVR_FLAG_AC3_CAP);
  819. if (err) {
  820. ctrls->error_idx = i;
  821. break;
  822. }
  823. }
  824. return err;
  825. }
  826. return -EINVAL;
  827. }
  828. static int hdpvr_set_ctrl(struct hdpvr_device *dev,
  829. struct v4l2_ext_control *ctrl)
  830. {
  831. struct hdpvr_options *opt = &dev->options;
  832. int ret = 0;
  833. switch (ctrl->id) {
  834. case V4L2_CID_MPEG_AUDIO_ENCODING:
  835. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  836. opt->audio_codec = ctrl->value;
  837. ret = hdpvr_set_audio(dev, opt->audio_input,
  838. opt->audio_codec);
  839. }
  840. break;
  841. case V4L2_CID_MPEG_VIDEO_ENCODING:
  842. break;
  843. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  844. /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
  845. /* opt->gop_mode |= 0x2; */
  846. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  847. /* opt->gop_mode); */
  848. /* } */
  849. /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
  850. /* opt->gop_mode &= ~0x2; */
  851. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  852. /* opt->gop_mode); */
  853. /* } */
  854. /* break; */
  855. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  856. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
  857. opt->bitrate_mode != HDPVR_CONSTANT) {
  858. opt->bitrate_mode = HDPVR_CONSTANT;
  859. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  860. opt->bitrate_mode);
  861. }
  862. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  863. opt->bitrate_mode == HDPVR_CONSTANT) {
  864. opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
  865. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  866. opt->bitrate_mode);
  867. }
  868. break;
  869. case V4L2_CID_MPEG_VIDEO_BITRATE: {
  870. uint bitrate = ctrl->value / 100000;
  871. opt->bitrate = bitrate;
  872. if (bitrate >= opt->peak_bitrate)
  873. opt->peak_bitrate = bitrate+1;
  874. hdpvr_set_bitrate(dev);
  875. break;
  876. }
  877. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
  878. uint peak_bitrate = ctrl->value / 100000;
  879. if (opt->bitrate_mode == HDPVR_CONSTANT)
  880. break;
  881. if (opt->bitrate < peak_bitrate) {
  882. opt->peak_bitrate = peak_bitrate;
  883. hdpvr_set_bitrate(dev);
  884. } else
  885. ret = -EINVAL;
  886. break;
  887. }
  888. case V4L2_CID_MPEG_STREAM_TYPE:
  889. break;
  890. default:
  891. return -EINVAL;
  892. }
  893. return ret;
  894. }
  895. static int vidioc_s_ext_ctrls(struct file *file, void *priv,
  896. struct v4l2_ext_controls *ctrls)
  897. {
  898. struct hdpvr_fh *fh = file->private_data;
  899. struct hdpvr_device *dev = fh->dev;
  900. int i, err = 0;
  901. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  902. for (i = 0; i < ctrls->count; i++) {
  903. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  904. err = hdpvr_try_ctrl(ctrl,
  905. dev->flags & HDPVR_FLAG_AC3_CAP);
  906. if (err) {
  907. ctrls->error_idx = i;
  908. break;
  909. }
  910. err = hdpvr_set_ctrl(dev, ctrl);
  911. if (err) {
  912. ctrls->error_idx = i;
  913. break;
  914. }
  915. }
  916. return err;
  917. }
  918. return -EINVAL;
  919. }
  920. static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
  921. struct v4l2_fmtdesc *f)
  922. {
  923. if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  924. return -EINVAL;
  925. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  926. strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
  927. f->pixelformat = V4L2_PIX_FMT_MPEG;
  928. return 0;
  929. }
  930. static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
  931. struct v4l2_format *f)
  932. {
  933. struct hdpvr_fh *fh = file->private_data;
  934. struct hdpvr_device *dev = fh->dev;
  935. struct hdpvr_video_info *vid_info;
  936. if (!dev)
  937. return -ENODEV;
  938. vid_info = get_video_info(dev);
  939. if (!vid_info)
  940. return -EFAULT;
  941. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  942. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  943. f->fmt.pix.width = vid_info->width;
  944. f->fmt.pix.height = vid_info->height;
  945. f->fmt.pix.sizeimage = dev->bulk_in_size;
  946. f->fmt.pix.colorspace = 0;
  947. f->fmt.pix.bytesperline = 0;
  948. f->fmt.pix.field = V4L2_FIELD_ANY;
  949. kfree(vid_info);
  950. return 0;
  951. }
  952. static int vidioc_encoder_cmd(struct file *filp, void *priv,
  953. struct v4l2_encoder_cmd *a)
  954. {
  955. struct hdpvr_fh *fh = filp->private_data;
  956. struct hdpvr_device *dev = fh->dev;
  957. int res;
  958. mutex_lock(&dev->io_mutex);
  959. memset(&a->raw, 0, sizeof(a->raw));
  960. switch (a->cmd) {
  961. case V4L2_ENC_CMD_START:
  962. a->flags = 0;
  963. res = hdpvr_start_streaming(dev);
  964. break;
  965. case V4L2_ENC_CMD_STOP:
  966. res = hdpvr_stop_streaming(dev);
  967. break;
  968. default:
  969. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  970. "Unsupported encoder cmd %d\n", a->cmd);
  971. res = -EINVAL;
  972. }
  973. mutex_unlock(&dev->io_mutex);
  974. return res;
  975. }
  976. static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
  977. struct v4l2_encoder_cmd *a)
  978. {
  979. switch (a->cmd) {
  980. case V4L2_ENC_CMD_START:
  981. case V4L2_ENC_CMD_STOP:
  982. return 0;
  983. default:
  984. return -EINVAL;
  985. }
  986. }
  987. static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
  988. .vidioc_querycap = vidioc_querycap,
  989. .vidioc_s_std = vidioc_s_std,
  990. .vidioc_enum_input = vidioc_enum_input,
  991. .vidioc_g_input = vidioc_g_input,
  992. .vidioc_s_input = vidioc_s_input,
  993. .vidioc_enumaudio = vidioc_enumaudio,
  994. .vidioc_g_audio = vidioc_g_audio,
  995. .vidioc_s_audio = vidioc_s_audio,
  996. .vidioc_queryctrl = vidioc_queryctrl,
  997. .vidioc_g_ctrl = vidioc_g_ctrl,
  998. .vidioc_s_ctrl = vidioc_s_ctrl,
  999. .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
  1000. .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
  1001. .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
  1002. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1003. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1004. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  1005. .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
  1006. };
  1007. static void hdpvr_device_release(struct video_device *vdev)
  1008. {
  1009. struct hdpvr_device *dev = video_get_drvdata(vdev);
  1010. hdpvr_delete(dev);
  1011. }
  1012. static const struct video_device hdpvr_video_template = {
  1013. /* .type = VFL_TYPE_GRABBER, */
  1014. /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
  1015. .fops = &hdpvr_fops,
  1016. .release = hdpvr_device_release,
  1017. .ioctl_ops = &hdpvr_ioctl_ops,
  1018. .tvnorms =
  1019. V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
  1020. V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
  1021. V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
  1022. V4L2_STD_PAL_60,
  1023. .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |
  1024. V4L2_STD_PAL_60,
  1025. };
  1026. int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
  1027. int devnum)
  1028. {
  1029. /* setup and register video device */
  1030. dev->video_dev = video_device_alloc();
  1031. if (!dev->video_dev) {
  1032. v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
  1033. goto error;
  1034. }
  1035. *(dev->video_dev) = hdpvr_video_template;
  1036. strcpy(dev->video_dev->name, "Hauppauge HD PVR");
  1037. dev->video_dev->parent = parent;
  1038. video_set_drvdata(dev->video_dev, dev);
  1039. if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
  1040. v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
  1041. goto error;
  1042. }
  1043. return 0;
  1044. error:
  1045. return -ENOMEM;
  1046. }