hdpvr-video.c 31 KB

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