hdpvr-video.c 30 KB

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