hdpvr-video.c 29 KB

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