hdpvr-video.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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 <linux/v4l2-dv-timings.h>
  22. #include <media/v4l2-dev.h>
  23. #include <media/v4l2-common.h>
  24. #include <media/v4l2-dv-timings.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/v4l2-event.h>
  27. #include "hdpvr.h"
  28. #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
  29. #define print_buffer_status() { \
  30. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
  31. "%s:%d buffer stat: %d free, %d proc\n", \
  32. __func__, __LINE__, \
  33. list_size(&dev->free_buff_list), \
  34. list_size(&dev->rec_buff_list)); }
  35. static const struct v4l2_dv_timings hdpvr_dv_timings[] = {
  36. V4L2_DV_BT_CEA_720X480I59_94,
  37. V4L2_DV_BT_CEA_720X576I50,
  38. V4L2_DV_BT_CEA_720X480P59_94,
  39. V4L2_DV_BT_CEA_720X576P50,
  40. V4L2_DV_BT_CEA_1280X720P50,
  41. V4L2_DV_BT_CEA_1280X720P60,
  42. V4L2_DV_BT_CEA_1920X1080I50,
  43. V4L2_DV_BT_CEA_1920X1080I60,
  44. };
  45. /* Use 480i59 as the default timings */
  46. #define HDPVR_DEF_DV_TIMINGS_IDX (0)
  47. struct hdpvr_fh {
  48. struct v4l2_fh fh;
  49. bool legacy_mode;
  50. };
  51. static uint list_size(struct list_head *list)
  52. {
  53. struct list_head *tmp;
  54. uint count = 0;
  55. list_for_each(tmp, list) {
  56. count++;
  57. }
  58. return count;
  59. }
  60. /*=========================================================================*/
  61. /* urb callback */
  62. static void hdpvr_read_bulk_callback(struct urb *urb)
  63. {
  64. struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
  65. struct hdpvr_device *dev = buf->dev;
  66. /* marking buffer as received and wake waiting */
  67. buf->status = BUFSTAT_READY;
  68. wake_up_interruptible(&dev->wait_data);
  69. }
  70. /*=========================================================================*/
  71. /* buffer bits */
  72. /* function expects dev->io_mutex to be hold by caller */
  73. int hdpvr_cancel_queue(struct hdpvr_device *dev)
  74. {
  75. struct hdpvr_buffer *buf;
  76. list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
  77. usb_kill_urb(buf->urb);
  78. buf->status = BUFSTAT_AVAILABLE;
  79. }
  80. list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
  81. return 0;
  82. }
  83. static int hdpvr_free_queue(struct list_head *q)
  84. {
  85. struct list_head *tmp;
  86. struct list_head *p;
  87. struct hdpvr_buffer *buf;
  88. struct urb *urb;
  89. for (p = q->next; p != q;) {
  90. buf = list_entry(p, struct hdpvr_buffer, buff_list);
  91. urb = buf->urb;
  92. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  93. urb->transfer_buffer, urb->transfer_dma);
  94. usb_free_urb(urb);
  95. tmp = p->next;
  96. list_del(p);
  97. kfree(buf);
  98. p = tmp;
  99. }
  100. return 0;
  101. }
  102. /* function expects dev->io_mutex to be hold by caller */
  103. int hdpvr_free_buffers(struct hdpvr_device *dev)
  104. {
  105. hdpvr_cancel_queue(dev);
  106. hdpvr_free_queue(&dev->free_buff_list);
  107. hdpvr_free_queue(&dev->rec_buff_list);
  108. return 0;
  109. }
  110. /* function expects dev->io_mutex to be hold by caller */
  111. int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
  112. {
  113. uint i;
  114. int retval = -ENOMEM;
  115. u8 *mem;
  116. struct hdpvr_buffer *buf;
  117. struct urb *urb;
  118. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  119. "allocating %u buffers\n", count);
  120. for (i = 0; i < count; i++) {
  121. buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
  122. if (!buf) {
  123. v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
  124. goto exit;
  125. }
  126. buf->dev = dev;
  127. urb = usb_alloc_urb(0, GFP_KERNEL);
  128. if (!urb)
  129. goto exit_urb;
  130. buf->urb = urb;
  131. mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
  132. &urb->transfer_dma);
  133. if (!mem) {
  134. v4l2_err(&dev->v4l2_dev,
  135. "cannot allocate usb transfer buffer\n");
  136. goto exit_urb_buffer;
  137. }
  138. usb_fill_bulk_urb(buf->urb, dev->udev,
  139. usb_rcvbulkpipe(dev->udev,
  140. dev->bulk_in_endpointAddr),
  141. mem, dev->bulk_in_size,
  142. hdpvr_read_bulk_callback, buf);
  143. buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  144. buf->status = BUFSTAT_AVAILABLE;
  145. list_add_tail(&buf->buff_list, &dev->free_buff_list);
  146. }
  147. return 0;
  148. exit_urb_buffer:
  149. usb_free_urb(urb);
  150. exit_urb:
  151. kfree(buf);
  152. exit:
  153. hdpvr_free_buffers(dev);
  154. return retval;
  155. }
  156. static int hdpvr_submit_buffers(struct hdpvr_device *dev)
  157. {
  158. struct hdpvr_buffer *buf;
  159. struct urb *urb;
  160. int ret = 0, err_count = 0;
  161. mutex_lock(&dev->io_mutex);
  162. while (dev->status == STATUS_STREAMING &&
  163. !list_empty(&dev->free_buff_list)) {
  164. buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
  165. buff_list);
  166. if (buf->status != BUFSTAT_AVAILABLE) {
  167. v4l2_err(&dev->v4l2_dev,
  168. "buffer not marked as available\n");
  169. ret = -EFAULT;
  170. goto err;
  171. }
  172. urb = buf->urb;
  173. urb->status = 0;
  174. urb->actual_length = 0;
  175. ret = usb_submit_urb(urb, GFP_KERNEL);
  176. if (ret) {
  177. v4l2_err(&dev->v4l2_dev,
  178. "usb_submit_urb in %s returned %d\n",
  179. __func__, ret);
  180. if (++err_count > 2)
  181. break;
  182. continue;
  183. }
  184. buf->status = BUFSTAT_INPROGRESS;
  185. list_move_tail(&buf->buff_list, &dev->rec_buff_list);
  186. }
  187. err:
  188. print_buffer_status();
  189. mutex_unlock(&dev->io_mutex);
  190. return ret;
  191. }
  192. static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
  193. {
  194. struct hdpvr_buffer *buf;
  195. mutex_lock(&dev->io_mutex);
  196. if (list_empty(&dev->rec_buff_list)) {
  197. mutex_unlock(&dev->io_mutex);
  198. return NULL;
  199. }
  200. buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
  201. buff_list);
  202. mutex_unlock(&dev->io_mutex);
  203. return buf;
  204. }
  205. static void hdpvr_transmit_buffers(struct work_struct *work)
  206. {
  207. struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
  208. worker);
  209. while (dev->status == STATUS_STREAMING) {
  210. if (hdpvr_submit_buffers(dev)) {
  211. v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
  212. goto error;
  213. }
  214. if (wait_event_interruptible(dev->wait_buffer,
  215. !list_empty(&dev->free_buff_list) ||
  216. dev->status != STATUS_STREAMING))
  217. goto error;
  218. }
  219. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  220. "transmit worker exited\n");
  221. return;
  222. error:
  223. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  224. "transmit buffers errored\n");
  225. dev->status = STATUS_ERROR;
  226. }
  227. /* function expects dev->io_mutex to be hold by caller */
  228. static int hdpvr_start_streaming(struct hdpvr_device *dev)
  229. {
  230. int ret;
  231. struct hdpvr_video_info vidinf;
  232. if (dev->status == STATUS_STREAMING)
  233. return 0;
  234. if (dev->status != STATUS_IDLE)
  235. return -EAGAIN;
  236. ret = get_video_info(dev, &vidinf);
  237. if (ret < 0)
  238. return ret;
  239. if (!vidinf.valid) {
  240. msleep(250);
  241. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  242. "no video signal at input %d\n", dev->options.video_input);
  243. return -EAGAIN;
  244. }
  245. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  246. "video signal: %dx%d@%dhz\n", vidinf.width,
  247. vidinf.height, vidinf.fps);
  248. /* start streaming 2 request */
  249. ret = usb_control_msg(dev->udev,
  250. usb_sndctrlpipe(dev->udev, 0),
  251. 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
  252. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  253. "encoder start control request returned %d\n", ret);
  254. if (ret < 0)
  255. return ret;
  256. ret = hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
  257. if (ret)
  258. return ret;
  259. dev->status = STATUS_STREAMING;
  260. INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
  261. schedule_work(&dev->worker);
  262. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  263. "streaming started\n");
  264. return 0;
  265. }
  266. /* function expects dev->io_mutex to be hold by caller */
  267. static int hdpvr_stop_streaming(struct hdpvr_device *dev)
  268. {
  269. int actual_length;
  270. uint c = 0;
  271. u8 *buf;
  272. if (dev->status == STATUS_IDLE)
  273. return 0;
  274. else if (dev->status != STATUS_STREAMING)
  275. return -EAGAIN;
  276. buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
  277. if (!buf)
  278. v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
  279. "for emptying the internal device buffer. "
  280. "Next capture start will be slow\n");
  281. dev->status = STATUS_SHUTTING_DOWN;
  282. hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
  283. mutex_unlock(&dev->io_mutex);
  284. wake_up_interruptible(&dev->wait_buffer);
  285. msleep(50);
  286. flush_work(&dev->worker);
  287. mutex_lock(&dev->io_mutex);
  288. /* kill the still outstanding urbs */
  289. hdpvr_cancel_queue(dev);
  290. /* emptying the device buffer beforeshutting it down */
  291. while (buf && ++c < 500 &&
  292. !usb_bulk_msg(dev->udev,
  293. usb_rcvbulkpipe(dev->udev,
  294. dev->bulk_in_endpointAddr),
  295. buf, dev->bulk_in_size, &actual_length,
  296. BULK_URB_TIMEOUT)) {
  297. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  298. "%2d: got %d bytes\n", c, actual_length);
  299. }
  300. kfree(buf);
  301. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  302. "used %d urbs to empty device buffers\n", c-1);
  303. msleep(10);
  304. dev->status = STATUS_IDLE;
  305. return 0;
  306. }
  307. /*=======================================================================*/
  308. /*
  309. * video 4 linux 2 file operations
  310. */
  311. static int hdpvr_open(struct file *file)
  312. {
  313. struct hdpvr_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  314. if (fh == NULL)
  315. return -ENOMEM;
  316. fh->legacy_mode = true;
  317. v4l2_fh_init(&fh->fh, video_devdata(file));
  318. v4l2_fh_add(&fh->fh);
  319. file->private_data = fh;
  320. return 0;
  321. }
  322. static int hdpvr_release(struct file *file)
  323. {
  324. struct hdpvr_device *dev = video_drvdata(file);
  325. mutex_lock(&dev->io_mutex);
  326. if (file->private_data == dev->owner) {
  327. hdpvr_stop_streaming(dev);
  328. dev->owner = NULL;
  329. }
  330. mutex_unlock(&dev->io_mutex);
  331. return v4l2_fh_release(file);
  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_device *dev = video_drvdata(file);
  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. mutex_lock(&dev->io_mutex);
  348. if (dev->status == STATUS_IDLE) {
  349. if (hdpvr_start_streaming(dev)) {
  350. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  351. "start_streaming failed\n");
  352. ret = -EIO;
  353. msleep(200);
  354. dev->status = STATUS_IDLE;
  355. mutex_unlock(&dev->io_mutex);
  356. goto err;
  357. }
  358. dev->owner = file->private_data;
  359. print_buffer_status();
  360. }
  361. mutex_unlock(&dev->io_mutex);
  362. /* wait for the first buffer */
  363. if (!(file->f_flags & O_NONBLOCK)) {
  364. if (wait_event_interruptible(dev->wait_data,
  365. hdpvr_get_next_buffer(dev)))
  366. return -ERESTARTSYS;
  367. }
  368. buf = hdpvr_get_next_buffer(dev);
  369. while (count > 0 && buf) {
  370. if (buf->status != BUFSTAT_READY &&
  371. dev->status != STATUS_DISCONNECTED) {
  372. /* return nonblocking */
  373. if (file->f_flags & O_NONBLOCK) {
  374. if (!ret)
  375. ret = -EAGAIN;
  376. goto err;
  377. }
  378. if (wait_event_interruptible(dev->wait_data,
  379. buf->status == BUFSTAT_READY))
  380. return -ERESTARTSYS;
  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. unsigned long req_events = poll_requested_events(wait);
  419. struct hdpvr_buffer *buf = NULL;
  420. struct hdpvr_device *dev = video_drvdata(filp);
  421. unsigned int mask = v4l2_ctrl_poll(filp, wait);
  422. if (!(req_events & (POLLIN | POLLRDNORM)))
  423. return mask;
  424. mutex_lock(&dev->io_mutex);
  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. } else {
  431. dev->owner = filp->private_data;
  432. }
  433. print_buffer_status();
  434. }
  435. mutex_unlock(&dev->io_mutex);
  436. buf = hdpvr_get_next_buffer(dev);
  437. /* only wait if no data is available */
  438. if (!buf || buf->status != BUFSTAT_READY) {
  439. poll_wait(filp, &dev->wait_data, wait);
  440. buf = hdpvr_get_next_buffer(dev);
  441. }
  442. if (buf && buf->status == BUFSTAT_READY)
  443. mask |= POLLIN | POLLRDNORM;
  444. return mask;
  445. }
  446. static const struct v4l2_file_operations hdpvr_fops = {
  447. .owner = THIS_MODULE,
  448. .open = hdpvr_open,
  449. .release = hdpvr_release,
  450. .read = hdpvr_read,
  451. .poll = hdpvr_poll,
  452. .unlocked_ioctl = video_ioctl2,
  453. };
  454. /*=======================================================================*/
  455. /*
  456. * V4L2 ioctl handling
  457. */
  458. static int vidioc_querycap(struct file *file, void *priv,
  459. struct v4l2_capability *cap)
  460. {
  461. struct hdpvr_device *dev = video_drvdata(file);
  462. strcpy(cap->driver, "hdpvr");
  463. strcpy(cap->card, "Hauppauge HD PVR");
  464. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  465. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO |
  466. V4L2_CAP_READWRITE;
  467. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  468. return 0;
  469. }
  470. static int vidioc_s_std(struct file *file, void *_fh,
  471. v4l2_std_id std)
  472. {
  473. struct hdpvr_device *dev = video_drvdata(file);
  474. struct hdpvr_fh *fh = _fh;
  475. u8 std_type = 1;
  476. if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
  477. return -ENODATA;
  478. if (dev->status != STATUS_IDLE)
  479. return -EBUSY;
  480. if (std & V4L2_STD_525_60)
  481. std_type = 0;
  482. dev->cur_std = std;
  483. dev->width = 720;
  484. dev->height = std_type ? 576 : 480;
  485. return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
  486. }
  487. static int vidioc_g_std(struct file *file, void *_fh,
  488. v4l2_std_id *std)
  489. {
  490. struct hdpvr_device *dev = video_drvdata(file);
  491. struct hdpvr_fh *fh = _fh;
  492. if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
  493. return -ENODATA;
  494. *std = dev->cur_std;
  495. return 0;
  496. }
  497. static int vidioc_querystd(struct file *file, void *_fh, v4l2_std_id *a)
  498. {
  499. struct hdpvr_device *dev = video_drvdata(file);
  500. struct hdpvr_video_info vid_info;
  501. struct hdpvr_fh *fh = _fh;
  502. int ret;
  503. *a = V4L2_STD_UNKNOWN;
  504. if (dev->options.video_input == HDPVR_COMPONENT)
  505. return fh->legacy_mode ? 0 : -ENODATA;
  506. ret = get_video_info(dev, &vid_info);
  507. if (vid_info.valid && vid_info.width == 720 &&
  508. (vid_info.height == 480 || vid_info.height == 576)) {
  509. *a = (vid_info.height == 480) ?
  510. V4L2_STD_525_60 : V4L2_STD_625_50;
  511. }
  512. return ret;
  513. }
  514. static int vidioc_s_dv_timings(struct file *file, void *_fh,
  515. struct v4l2_dv_timings *timings)
  516. {
  517. struct hdpvr_device *dev = video_drvdata(file);
  518. struct hdpvr_fh *fh = _fh;
  519. int i;
  520. fh->legacy_mode = false;
  521. if (dev->options.video_input)
  522. return -ENODATA;
  523. if (dev->status != STATUS_IDLE)
  524. return -EBUSY;
  525. for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++)
  526. if (v4l2_match_dv_timings(timings, hdpvr_dv_timings + i, 0, false))
  527. break;
  528. if (i == ARRAY_SIZE(hdpvr_dv_timings))
  529. return -EINVAL;
  530. dev->cur_dv_timings = hdpvr_dv_timings[i];
  531. dev->width = hdpvr_dv_timings[i].bt.width;
  532. dev->height = hdpvr_dv_timings[i].bt.height;
  533. return 0;
  534. }
  535. static int vidioc_g_dv_timings(struct file *file, void *_fh,
  536. struct v4l2_dv_timings *timings)
  537. {
  538. struct hdpvr_device *dev = video_drvdata(file);
  539. struct hdpvr_fh *fh = _fh;
  540. fh->legacy_mode = false;
  541. if (dev->options.video_input)
  542. return -ENODATA;
  543. *timings = dev->cur_dv_timings;
  544. return 0;
  545. }
  546. static int vidioc_query_dv_timings(struct file *file, void *_fh,
  547. struct v4l2_dv_timings *timings)
  548. {
  549. struct hdpvr_device *dev = video_drvdata(file);
  550. struct hdpvr_fh *fh = _fh;
  551. struct hdpvr_video_info vid_info;
  552. bool interlaced;
  553. int ret = 0;
  554. int i;
  555. fh->legacy_mode = false;
  556. if (dev->options.video_input)
  557. return -ENODATA;
  558. ret = get_video_info(dev, &vid_info);
  559. if (ret)
  560. return ret;
  561. if (!vid_info.valid)
  562. return -ENOLCK;
  563. interlaced = vid_info.fps <= 30;
  564. for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) {
  565. const struct v4l2_bt_timings *bt = &hdpvr_dv_timings[i].bt;
  566. unsigned hsize;
  567. unsigned vsize;
  568. unsigned fps;
  569. hsize = V4L2_DV_BT_FRAME_WIDTH(bt);
  570. vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
  571. fps = (unsigned)bt->pixelclock / (hsize * vsize);
  572. if (bt->width != vid_info.width ||
  573. bt->height != vid_info.height ||
  574. bt->interlaced != interlaced ||
  575. (fps != vid_info.fps && fps + 1 != vid_info.fps))
  576. continue;
  577. *timings = hdpvr_dv_timings[i];
  578. break;
  579. }
  580. if (i == ARRAY_SIZE(hdpvr_dv_timings))
  581. ret = -ERANGE;
  582. return ret;
  583. }
  584. static int vidioc_enum_dv_timings(struct file *file, void *_fh,
  585. struct v4l2_enum_dv_timings *timings)
  586. {
  587. struct hdpvr_device *dev = video_drvdata(file);
  588. struct hdpvr_fh *fh = _fh;
  589. fh->legacy_mode = false;
  590. memset(timings->reserved, 0, sizeof(timings->reserved));
  591. if (dev->options.video_input)
  592. return -ENODATA;
  593. if (timings->index >= ARRAY_SIZE(hdpvr_dv_timings))
  594. return -EINVAL;
  595. timings->timings = hdpvr_dv_timings[timings->index];
  596. return 0;
  597. }
  598. static int vidioc_dv_timings_cap(struct file *file, void *_fh,
  599. struct v4l2_dv_timings_cap *cap)
  600. {
  601. struct hdpvr_device *dev = video_drvdata(file);
  602. struct hdpvr_fh *fh = _fh;
  603. fh->legacy_mode = false;
  604. if (dev->options.video_input)
  605. return -ENODATA;
  606. cap->type = V4L2_DV_BT_656_1120;
  607. cap->bt.min_width = 720;
  608. cap->bt.max_width = 1920;
  609. cap->bt.min_height = 480;
  610. cap->bt.max_height = 1080;
  611. cap->bt.min_pixelclock = 27000000;
  612. cap->bt.max_pixelclock = 74250000;
  613. cap->bt.standards = V4L2_DV_BT_STD_CEA861;
  614. cap->bt.capabilities = V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE;
  615. return 0;
  616. }
  617. static const char *iname[] = {
  618. [HDPVR_COMPONENT] = "Component",
  619. [HDPVR_SVIDEO] = "S-Video",
  620. [HDPVR_COMPOSITE] = "Composite",
  621. };
  622. static int vidioc_enum_input(struct file *file, void *_fh, struct v4l2_input *i)
  623. {
  624. unsigned int n;
  625. n = i->index;
  626. if (n >= HDPVR_VIDEO_INPUTS)
  627. return -EINVAL;
  628. i->type = V4L2_INPUT_TYPE_CAMERA;
  629. strncpy(i->name, iname[n], sizeof(i->name) - 1);
  630. i->name[sizeof(i->name) - 1] = '\0';
  631. i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
  632. i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
  633. i->std = n ? V4L2_STD_ALL : 0;
  634. return 0;
  635. }
  636. static int vidioc_s_input(struct file *file, void *_fh,
  637. unsigned int index)
  638. {
  639. struct hdpvr_device *dev = video_drvdata(file);
  640. int retval;
  641. if (index >= HDPVR_VIDEO_INPUTS)
  642. return -EINVAL;
  643. if (dev->status != STATUS_IDLE)
  644. return -EBUSY;
  645. retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
  646. if (!retval) {
  647. dev->options.video_input = index;
  648. /*
  649. * Unfortunately gstreamer calls ENUMSTD and bails out if it
  650. * won't find any formats, even though component input is
  651. * selected. This means that we have to leave tvnorms at
  652. * V4L2_STD_ALL. We cannot use the 'legacy' trick since
  653. * tvnorms is set at the device node level and not at the
  654. * filehandle level.
  655. *
  656. * Comment this out for now, but if the legacy mode can be
  657. * removed in the future, then this code should be enabled
  658. * again.
  659. dev->video_dev.tvnorms =
  660. (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
  661. */
  662. }
  663. return retval;
  664. }
  665. static int vidioc_g_input(struct file *file, void *private_data,
  666. unsigned int *index)
  667. {
  668. struct hdpvr_device *dev = video_drvdata(file);
  669. *index = dev->options.video_input;
  670. return 0;
  671. }
  672. static const char *audio_iname[] = {
  673. [HDPVR_RCA_FRONT] = "RCA front",
  674. [HDPVR_RCA_BACK] = "RCA back",
  675. [HDPVR_SPDIF] = "SPDIF",
  676. };
  677. static int vidioc_enumaudio(struct file *file, void *priv,
  678. struct v4l2_audio *audio)
  679. {
  680. unsigned int n;
  681. n = audio->index;
  682. if (n >= HDPVR_AUDIO_INPUTS)
  683. return -EINVAL;
  684. audio->capability = V4L2_AUDCAP_STEREO;
  685. strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
  686. audio->name[sizeof(audio->name) - 1] = '\0';
  687. return 0;
  688. }
  689. static int vidioc_s_audio(struct file *file, void *private_data,
  690. const struct v4l2_audio *audio)
  691. {
  692. struct hdpvr_device *dev = video_drvdata(file);
  693. int retval;
  694. if (audio->index >= HDPVR_AUDIO_INPUTS)
  695. return -EINVAL;
  696. if (dev->status != STATUS_IDLE)
  697. return -EBUSY;
  698. retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
  699. if (!retval)
  700. dev->options.audio_input = audio->index;
  701. return retval;
  702. }
  703. static int vidioc_g_audio(struct file *file, void *private_data,
  704. struct v4l2_audio *audio)
  705. {
  706. struct hdpvr_device *dev = video_drvdata(file);
  707. audio->index = dev->options.audio_input;
  708. audio->capability = V4L2_AUDCAP_STEREO;
  709. strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
  710. audio->name[sizeof(audio->name) - 1] = '\0';
  711. return 0;
  712. }
  713. static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
  714. {
  715. struct hdpvr_device *dev =
  716. container_of(ctrl->handler, struct hdpvr_device, hdl);
  717. switch (ctrl->id) {
  718. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  719. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  720. dev->video_bitrate->val >= dev->video_bitrate_peak->val)
  721. dev->video_bitrate_peak->val =
  722. dev->video_bitrate->val + 100000;
  723. break;
  724. }
  725. return 0;
  726. }
  727. static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
  728. {
  729. struct hdpvr_device *dev =
  730. container_of(ctrl->handler, struct hdpvr_device, hdl);
  731. struct hdpvr_options *opt = &dev->options;
  732. int ret = -EINVAL;
  733. switch (ctrl->id) {
  734. case V4L2_CID_BRIGHTNESS:
  735. ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
  736. if (ret)
  737. break;
  738. dev->options.brightness = ctrl->val;
  739. return 0;
  740. case V4L2_CID_CONTRAST:
  741. ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
  742. if (ret)
  743. break;
  744. dev->options.contrast = ctrl->val;
  745. return 0;
  746. case V4L2_CID_SATURATION:
  747. ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
  748. if (ret)
  749. break;
  750. dev->options.saturation = ctrl->val;
  751. return 0;
  752. case V4L2_CID_HUE:
  753. ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
  754. if (ret)
  755. break;
  756. dev->options.hue = ctrl->val;
  757. return 0;
  758. case V4L2_CID_SHARPNESS:
  759. ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
  760. if (ret)
  761. break;
  762. dev->options.sharpness = ctrl->val;
  763. return 0;
  764. case V4L2_CID_MPEG_AUDIO_ENCODING:
  765. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  766. opt->audio_codec = ctrl->val;
  767. return hdpvr_set_audio(dev, opt->audio_input + 1,
  768. opt->audio_codec);
  769. }
  770. return 0;
  771. case V4L2_CID_MPEG_VIDEO_ENCODING:
  772. return 0;
  773. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  774. /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
  775. /* opt->gop_mode |= 0x2; */
  776. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  777. /* opt->gop_mode); */
  778. /* } */
  779. /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
  780. /* opt->gop_mode &= ~0x2; */
  781. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  782. /* opt->gop_mode); */
  783. /* } */
  784. /* break; */
  785. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
  786. uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
  787. uint bitrate = dev->video_bitrate->val / 100000;
  788. if (ctrl->is_new) {
  789. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
  790. opt->bitrate_mode = HDPVR_CONSTANT;
  791. else
  792. opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
  793. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  794. opt->bitrate_mode);
  795. v4l2_ctrl_activate(dev->video_bitrate_peak,
  796. ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  797. }
  798. if (dev->video_bitrate_peak->is_new ||
  799. dev->video_bitrate->is_new) {
  800. opt->bitrate = bitrate;
  801. opt->peak_bitrate = peak_bitrate;
  802. hdpvr_set_bitrate(dev);
  803. }
  804. return 0;
  805. }
  806. case V4L2_CID_MPEG_STREAM_TYPE:
  807. return 0;
  808. default:
  809. break;
  810. }
  811. return ret;
  812. }
  813. static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
  814. struct v4l2_fmtdesc *f)
  815. {
  816. if (f->index != 0)
  817. return -EINVAL;
  818. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  819. strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
  820. f->pixelformat = V4L2_PIX_FMT_MPEG;
  821. return 0;
  822. }
  823. static int vidioc_g_fmt_vid_cap(struct file *file, void *_fh,
  824. struct v4l2_format *f)
  825. {
  826. struct hdpvr_device *dev = video_drvdata(file);
  827. struct hdpvr_fh *fh = _fh;
  828. int ret;
  829. /*
  830. * The original driver would always returns the current detected
  831. * resolution as the format (and EFAULT if it couldn't be detected).
  832. * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
  833. * better way of doing this, but to stay compatible with existing
  834. * applications we assume legacy mode every time an application opens
  835. * the device. Only if one of the new DV_TIMINGS ioctls is called
  836. * will the filehandle go into 'normal' mode where g_fmt returns the
  837. * last set format.
  838. */
  839. if (fh->legacy_mode) {
  840. struct hdpvr_video_info vid_info;
  841. ret = get_video_info(dev, &vid_info);
  842. if (ret < 0)
  843. return ret;
  844. if (!vid_info.valid)
  845. return -EFAULT;
  846. f->fmt.pix.width = vid_info.width;
  847. f->fmt.pix.height = vid_info.height;
  848. } else {
  849. f->fmt.pix.width = dev->width;
  850. f->fmt.pix.height = dev->height;
  851. }
  852. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  853. f->fmt.pix.sizeimage = dev->bulk_in_size;
  854. f->fmt.pix.bytesperline = 0;
  855. if (f->fmt.pix.width == 720) {
  856. /* SDTV formats */
  857. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  858. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  859. } else {
  860. /* HDTV formats */
  861. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  862. f->fmt.pix.field = V4L2_FIELD_NONE;
  863. }
  864. return 0;
  865. }
  866. static int vidioc_encoder_cmd(struct file *filp, void *priv,
  867. struct v4l2_encoder_cmd *a)
  868. {
  869. struct hdpvr_device *dev = video_drvdata(filp);
  870. int res = 0;
  871. mutex_lock(&dev->io_mutex);
  872. a->flags = 0;
  873. switch (a->cmd) {
  874. case V4L2_ENC_CMD_START:
  875. if (dev->owner && filp->private_data != dev->owner) {
  876. res = -EBUSY;
  877. break;
  878. }
  879. if (dev->status == STATUS_STREAMING)
  880. break;
  881. res = hdpvr_start_streaming(dev);
  882. if (!res)
  883. dev->owner = filp->private_data;
  884. else
  885. dev->status = STATUS_IDLE;
  886. break;
  887. case V4L2_ENC_CMD_STOP:
  888. if (dev->owner && filp->private_data != dev->owner) {
  889. res = -EBUSY;
  890. break;
  891. }
  892. if (dev->status == STATUS_IDLE)
  893. break;
  894. res = hdpvr_stop_streaming(dev);
  895. if (!res)
  896. dev->owner = NULL;
  897. break;
  898. default:
  899. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  900. "Unsupported encoder cmd %d\n", a->cmd);
  901. res = -EINVAL;
  902. break;
  903. }
  904. mutex_unlock(&dev->io_mutex);
  905. return res;
  906. }
  907. static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
  908. struct v4l2_encoder_cmd *a)
  909. {
  910. a->flags = 0;
  911. switch (a->cmd) {
  912. case V4L2_ENC_CMD_START:
  913. case V4L2_ENC_CMD_STOP:
  914. return 0;
  915. default:
  916. return -EINVAL;
  917. }
  918. }
  919. static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
  920. .vidioc_querycap = vidioc_querycap,
  921. .vidioc_s_std = vidioc_s_std,
  922. .vidioc_g_std = vidioc_g_std,
  923. .vidioc_querystd = vidioc_querystd,
  924. .vidioc_s_dv_timings = vidioc_s_dv_timings,
  925. .vidioc_g_dv_timings = vidioc_g_dv_timings,
  926. .vidioc_query_dv_timings= vidioc_query_dv_timings,
  927. .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
  928. .vidioc_dv_timings_cap = vidioc_dv_timings_cap,
  929. .vidioc_enum_input = vidioc_enum_input,
  930. .vidioc_g_input = vidioc_g_input,
  931. .vidioc_s_input = vidioc_s_input,
  932. .vidioc_enumaudio = vidioc_enumaudio,
  933. .vidioc_g_audio = vidioc_g_audio,
  934. .vidioc_s_audio = vidioc_s_audio,
  935. .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
  936. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  937. .vidioc_s_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  938. .vidioc_try_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  939. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  940. .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
  941. .vidioc_log_status = v4l2_ctrl_log_status,
  942. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  943. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  944. };
  945. static void hdpvr_device_release(struct video_device *vdev)
  946. {
  947. struct hdpvr_device *dev = video_get_drvdata(vdev);
  948. hdpvr_delete(dev);
  949. mutex_lock(&dev->io_mutex);
  950. flush_work(&dev->worker);
  951. mutex_unlock(&dev->io_mutex);
  952. v4l2_device_unregister(&dev->v4l2_dev);
  953. v4l2_ctrl_handler_free(&dev->hdl);
  954. /* deregister I2C adapter */
  955. #if IS_ENABLED(CONFIG_I2C)
  956. mutex_lock(&dev->i2c_mutex);
  957. i2c_del_adapter(&dev->i2c_adapter);
  958. mutex_unlock(&dev->i2c_mutex);
  959. #endif /* CONFIG_I2C */
  960. kfree(dev->usbc_buf);
  961. kfree(dev);
  962. }
  963. static const struct video_device hdpvr_video_template = {
  964. .fops = &hdpvr_fops,
  965. .release = hdpvr_device_release,
  966. .ioctl_ops = &hdpvr_ioctl_ops,
  967. .tvnorms = V4L2_STD_ALL,
  968. };
  969. static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
  970. .try_ctrl = hdpvr_try_ctrl,
  971. .s_ctrl = hdpvr_s_ctrl,
  972. };
  973. int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
  974. int devnum)
  975. {
  976. struct v4l2_ctrl_handler *hdl = &dev->hdl;
  977. bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
  978. int res;
  979. dev->cur_std = V4L2_STD_525_60;
  980. dev->width = 720;
  981. dev->height = 480;
  982. dev->cur_dv_timings = hdpvr_dv_timings[HDPVR_DEF_DV_TIMINGS_IDX];
  983. v4l2_ctrl_handler_init(hdl, 11);
  984. if (dev->fw_ver > 0x15) {
  985. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  986. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
  987. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  988. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
  989. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  990. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
  991. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  992. V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
  993. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  994. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  995. } else {
  996. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  997. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
  998. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  999. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
  1000. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1001. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
  1002. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1003. V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
  1004. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1005. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  1006. }
  1007. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  1008. V4L2_CID_MPEG_STREAM_TYPE,
  1009. V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
  1010. 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
  1011. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  1012. V4L2_CID_MPEG_AUDIO_ENCODING,
  1013. ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
  1014. 0x7, ac3 ? dev->options.audio_codec : V4L2_MPEG_AUDIO_ENCODING_AAC);
  1015. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  1016. V4L2_CID_MPEG_VIDEO_ENCODING,
  1017. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
  1018. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  1019. dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  1020. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  1021. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
  1022. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  1023. dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1024. V4L2_CID_MPEG_VIDEO_BITRATE,
  1025. 1000000, 13500000, 100000, 6500000);
  1026. dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  1027. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  1028. 1100000, 20200000, 100000, 9000000);
  1029. dev->v4l2_dev.ctrl_handler = hdl;
  1030. if (hdl->error) {
  1031. res = hdl->error;
  1032. v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
  1033. goto error;
  1034. }
  1035. v4l2_ctrl_cluster(3, &dev->video_mode);
  1036. res = v4l2_ctrl_handler_setup(hdl);
  1037. if (res < 0) {
  1038. v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
  1039. goto error;
  1040. }
  1041. /* setup and register video device */
  1042. dev->video_dev = hdpvr_video_template;
  1043. strcpy(dev->video_dev.name, "Hauppauge HD PVR");
  1044. dev->video_dev.v4l2_dev = &dev->v4l2_dev;
  1045. video_set_drvdata(&dev->video_dev, dev);
  1046. res = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER, devnum);
  1047. if (res < 0) {
  1048. v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
  1049. goto error;
  1050. }
  1051. return 0;
  1052. error:
  1053. v4l2_ctrl_handler_free(hdl);
  1054. return res;
  1055. }