/drivers/media/video/hdpvr/hdpvr-video.c

https://bitbucket.org/abioy/linux · C · 1257 lines · 985 code · 204 blank · 68 comment · 153 complexity · ca4cb733cf4b7fb4cb596e0d9eaf8c87 MD5 · raw file

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