hdpvr-video.c 31 KB

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