hdpvr-video.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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 const struct v4l2_file_operations hdpvr_fops = {
  423. .owner = THIS_MODULE,
  424. .open = hdpvr_open,
  425. .release = hdpvr_release,
  426. .read = hdpvr_read,
  427. .poll = hdpvr_poll,
  428. .unlocked_ioctl = video_ioctl2,
  429. };
  430. /*=======================================================================*/
  431. /*
  432. * V4L2 ioctl handling
  433. */
  434. static int vidioc_querycap(struct file *file, void *priv,
  435. struct v4l2_capability *cap)
  436. {
  437. struct hdpvr_device *dev = video_drvdata(file);
  438. strcpy(cap->driver, "hdpvr");
  439. strcpy(cap->card, "Haupauge HD PVR");
  440. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  441. cap->version = HDPVR_VERSION;
  442. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  443. V4L2_CAP_AUDIO |
  444. V4L2_CAP_READWRITE;
  445. return 0;
  446. }
  447. static int vidioc_s_std(struct file *file, void *private_data,
  448. v4l2_std_id *std)
  449. {
  450. struct hdpvr_fh *fh = file->private_data;
  451. struct hdpvr_device *dev = fh->dev;
  452. u8 std_type = 1;
  453. if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
  454. std_type = 0;
  455. return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
  456. }
  457. static const char *iname[] = {
  458. [HDPVR_COMPONENT] = "Component",
  459. [HDPVR_SVIDEO] = "S-Video",
  460. [HDPVR_COMPOSITE] = "Composite",
  461. };
  462. static int vidioc_enum_input(struct file *file, void *priv,
  463. struct v4l2_input *i)
  464. {
  465. struct hdpvr_fh *fh = file->private_data;
  466. struct hdpvr_device *dev = fh->dev;
  467. unsigned int n;
  468. n = i->index;
  469. if (n >= HDPVR_VIDEO_INPUTS)
  470. return -EINVAL;
  471. i->type = V4L2_INPUT_TYPE_CAMERA;
  472. strncpy(i->name, iname[n], sizeof(i->name) - 1);
  473. i->name[sizeof(i->name) - 1] = '\0';
  474. i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
  475. i->std = dev->video_dev->tvnorms;
  476. return 0;
  477. }
  478. static int vidioc_s_input(struct file *file, void *private_data,
  479. unsigned int index)
  480. {
  481. struct hdpvr_fh *fh = file->private_data;
  482. struct hdpvr_device *dev = fh->dev;
  483. int retval;
  484. if (index >= HDPVR_VIDEO_INPUTS)
  485. return -EINVAL;
  486. if (dev->status != STATUS_IDLE)
  487. return -EAGAIN;
  488. retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
  489. if (!retval)
  490. dev->options.video_input = index;
  491. return retval;
  492. }
  493. static int vidioc_g_input(struct file *file, void *private_data,
  494. unsigned int *index)
  495. {
  496. struct hdpvr_fh *fh = file->private_data;
  497. struct hdpvr_device *dev = fh->dev;
  498. *index = dev->options.video_input;
  499. return 0;
  500. }
  501. static const char *audio_iname[] = {
  502. [HDPVR_RCA_FRONT] = "RCA front",
  503. [HDPVR_RCA_BACK] = "RCA back",
  504. [HDPVR_SPDIF] = "SPDIF",
  505. };
  506. static int vidioc_enumaudio(struct file *file, void *priv,
  507. struct v4l2_audio *audio)
  508. {
  509. unsigned int n;
  510. n = audio->index;
  511. if (n >= HDPVR_AUDIO_INPUTS)
  512. return -EINVAL;
  513. audio->capability = V4L2_AUDCAP_STEREO;
  514. strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
  515. audio->name[sizeof(audio->name) - 1] = '\0';
  516. return 0;
  517. }
  518. static int vidioc_s_audio(struct file *file, void *private_data,
  519. struct v4l2_audio *audio)
  520. {
  521. struct hdpvr_fh *fh = file->private_data;
  522. struct hdpvr_device *dev = fh->dev;
  523. int retval;
  524. if (audio->index >= HDPVR_AUDIO_INPUTS)
  525. return -EINVAL;
  526. if (dev->status != STATUS_IDLE)
  527. return -EAGAIN;
  528. retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
  529. if (!retval)
  530. dev->options.audio_input = audio->index;
  531. return retval;
  532. }
  533. static int vidioc_g_audio(struct file *file, void *private_data,
  534. struct v4l2_audio *audio)
  535. {
  536. struct hdpvr_fh *fh = file->private_data;
  537. struct hdpvr_device *dev = fh->dev;
  538. audio->index = dev->options.audio_input;
  539. audio->capability = V4L2_AUDCAP_STEREO;
  540. strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
  541. audio->name[sizeof(audio->name) - 1] = '\0';
  542. return 0;
  543. }
  544. static const s32 supported_v4l2_ctrls[] = {
  545. V4L2_CID_BRIGHTNESS,
  546. V4L2_CID_CONTRAST,
  547. V4L2_CID_SATURATION,
  548. V4L2_CID_HUE,
  549. V4L2_CID_SHARPNESS,
  550. V4L2_CID_MPEG_AUDIO_ENCODING,
  551. V4L2_CID_MPEG_VIDEO_ENCODING,
  552. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  553. V4L2_CID_MPEG_VIDEO_BITRATE,
  554. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  555. };
  556. static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
  557. int ac3)
  558. {
  559. int err;
  560. switch (qc->id) {
  561. case V4L2_CID_BRIGHTNESS:
  562. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
  563. case V4L2_CID_CONTRAST:
  564. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  565. case V4L2_CID_SATURATION:
  566. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  567. case V4L2_CID_HUE:
  568. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  569. case V4L2_CID_SHARPNESS:
  570. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  571. case V4L2_CID_MPEG_AUDIO_ENCODING:
  572. return v4l2_ctrl_query_fill(
  573. qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
  574. ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
  575. : V4L2_MPEG_AUDIO_ENCODING_AAC,
  576. 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
  577. case V4L2_CID_MPEG_VIDEO_ENCODING:
  578. return v4l2_ctrl_query_fill(
  579. qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
  580. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
  581. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  582. /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
  583. /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
  584. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  585. return v4l2_ctrl_query_fill(
  586. qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  587. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
  588. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  589. case V4L2_CID_MPEG_VIDEO_BITRATE:
  590. return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
  591. 6500000);
  592. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  593. err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
  594. 9000000);
  595. if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
  596. qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
  597. return err;
  598. default:
  599. return -EINVAL;
  600. }
  601. }
  602. static int vidioc_queryctrl(struct file *file, void *private_data,
  603. struct v4l2_queryctrl *qc)
  604. {
  605. struct hdpvr_fh *fh = file->private_data;
  606. struct hdpvr_device *dev = fh->dev;
  607. int i, next;
  608. u32 id = qc->id;
  609. memset(qc, 0, sizeof(*qc));
  610. next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
  611. qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
  612. for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
  613. if (next) {
  614. if (qc->id < supported_v4l2_ctrls[i])
  615. qc->id = supported_v4l2_ctrls[i];
  616. else
  617. continue;
  618. }
  619. if (qc->id == supported_v4l2_ctrls[i])
  620. return fill_queryctrl(&dev->options, qc,
  621. dev->flags & HDPVR_FLAG_AC3_CAP);
  622. if (qc->id < supported_v4l2_ctrls[i])
  623. break;
  624. }
  625. return -EINVAL;
  626. }
  627. static int vidioc_g_ctrl(struct file *file, void *private_data,
  628. struct v4l2_control *ctrl)
  629. {
  630. struct hdpvr_fh *fh = file->private_data;
  631. struct hdpvr_device *dev = fh->dev;
  632. switch (ctrl->id) {
  633. case V4L2_CID_BRIGHTNESS:
  634. ctrl->value = dev->options.brightness;
  635. break;
  636. case V4L2_CID_CONTRAST:
  637. ctrl->value = dev->options.contrast;
  638. break;
  639. case V4L2_CID_SATURATION:
  640. ctrl->value = dev->options.saturation;
  641. break;
  642. case V4L2_CID_HUE:
  643. ctrl->value = dev->options.hue;
  644. break;
  645. case V4L2_CID_SHARPNESS:
  646. ctrl->value = dev->options.sharpness;
  647. break;
  648. default:
  649. return -EINVAL;
  650. }
  651. return 0;
  652. }
  653. static int vidioc_s_ctrl(struct file *file, void *private_data,
  654. struct v4l2_control *ctrl)
  655. {
  656. struct hdpvr_fh *fh = file->private_data;
  657. struct hdpvr_device *dev = fh->dev;
  658. int retval;
  659. switch (ctrl->id) {
  660. case V4L2_CID_BRIGHTNESS:
  661. retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
  662. if (!retval)
  663. dev->options.brightness = ctrl->value;
  664. break;
  665. case V4L2_CID_CONTRAST:
  666. retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
  667. if (!retval)
  668. dev->options.contrast = ctrl->value;
  669. break;
  670. case V4L2_CID_SATURATION:
  671. retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
  672. if (!retval)
  673. dev->options.saturation = ctrl->value;
  674. break;
  675. case V4L2_CID_HUE:
  676. retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
  677. if (!retval)
  678. dev->options.hue = ctrl->value;
  679. break;
  680. case V4L2_CID_SHARPNESS:
  681. retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
  682. if (!retval)
  683. dev->options.sharpness = ctrl->value;
  684. break;
  685. default:
  686. return -EINVAL;
  687. }
  688. return retval;
  689. }
  690. static int hdpvr_get_ctrl(struct hdpvr_options *opt,
  691. struct v4l2_ext_control *ctrl)
  692. {
  693. switch (ctrl->id) {
  694. case V4L2_CID_MPEG_AUDIO_ENCODING:
  695. ctrl->value = opt->audio_codec;
  696. break;
  697. case V4L2_CID_MPEG_VIDEO_ENCODING:
  698. ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
  699. break;
  700. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  701. /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
  702. /* break; */
  703. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  704. ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
  705. ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
  706. : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
  707. break;
  708. case V4L2_CID_MPEG_VIDEO_BITRATE:
  709. ctrl->value = opt->bitrate * 100000;
  710. break;
  711. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  712. ctrl->value = opt->peak_bitrate * 100000;
  713. break;
  714. case V4L2_CID_MPEG_STREAM_TYPE:
  715. ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
  716. break;
  717. default:
  718. return -EINVAL;
  719. }
  720. return 0;
  721. }
  722. static int vidioc_g_ext_ctrls(struct file *file, void *priv,
  723. struct v4l2_ext_controls *ctrls)
  724. {
  725. struct hdpvr_fh *fh = file->private_data;
  726. struct hdpvr_device *dev = fh->dev;
  727. int i, err = 0;
  728. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  729. for (i = 0; i < ctrls->count; i++) {
  730. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  731. err = hdpvr_get_ctrl(&dev->options, ctrl);
  732. if (err) {
  733. ctrls->error_idx = i;
  734. break;
  735. }
  736. }
  737. return err;
  738. }
  739. return -EINVAL;
  740. }
  741. static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
  742. {
  743. int ret = -EINVAL;
  744. switch (ctrl->id) {
  745. case V4L2_CID_MPEG_AUDIO_ENCODING:
  746. if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
  747. (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
  748. ret = 0;
  749. break;
  750. case V4L2_CID_MPEG_VIDEO_ENCODING:
  751. if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
  752. ret = 0;
  753. break;
  754. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  755. /* if (ctrl->value == 0 || ctrl->value == 128) */
  756. /* ret = 0; */
  757. /* break; */
  758. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  759. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
  760. ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
  761. ret = 0;
  762. break;
  763. case V4L2_CID_MPEG_VIDEO_BITRATE:
  764. {
  765. uint bitrate = ctrl->value / 100000;
  766. if (bitrate >= 10 && bitrate <= 135)
  767. ret = 0;
  768. break;
  769. }
  770. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  771. {
  772. uint peak_bitrate = ctrl->value / 100000;
  773. if (peak_bitrate >= 10 && peak_bitrate <= 202)
  774. ret = 0;
  775. break;
  776. }
  777. case V4L2_CID_MPEG_STREAM_TYPE:
  778. if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
  779. ret = 0;
  780. break;
  781. default:
  782. return -EINVAL;
  783. }
  784. return 0;
  785. }
  786. static int vidioc_try_ext_ctrls(struct file *file, void *priv,
  787. struct v4l2_ext_controls *ctrls)
  788. {
  789. struct hdpvr_fh *fh = file->private_data;
  790. struct hdpvr_device *dev = fh->dev;
  791. int i, err = 0;
  792. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  793. for (i = 0; i < ctrls->count; i++) {
  794. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  795. err = hdpvr_try_ctrl(ctrl,
  796. dev->flags & HDPVR_FLAG_AC3_CAP);
  797. if (err) {
  798. ctrls->error_idx = i;
  799. break;
  800. }
  801. }
  802. return err;
  803. }
  804. return -EINVAL;
  805. }
  806. static int hdpvr_set_ctrl(struct hdpvr_device *dev,
  807. struct v4l2_ext_control *ctrl)
  808. {
  809. struct hdpvr_options *opt = &dev->options;
  810. int ret = 0;
  811. switch (ctrl->id) {
  812. case V4L2_CID_MPEG_AUDIO_ENCODING:
  813. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  814. opt->audio_codec = ctrl->value;
  815. ret = hdpvr_set_audio(dev, opt->audio_input,
  816. opt->audio_codec);
  817. }
  818. break;
  819. case V4L2_CID_MPEG_VIDEO_ENCODING:
  820. break;
  821. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  822. /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
  823. /* opt->gop_mode |= 0x2; */
  824. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  825. /* opt->gop_mode); */
  826. /* } */
  827. /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
  828. /* opt->gop_mode &= ~0x2; */
  829. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  830. /* opt->gop_mode); */
  831. /* } */
  832. /* break; */
  833. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  834. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
  835. opt->bitrate_mode != HDPVR_CONSTANT) {
  836. opt->bitrate_mode = HDPVR_CONSTANT;
  837. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  838. opt->bitrate_mode);
  839. }
  840. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  841. opt->bitrate_mode == HDPVR_CONSTANT) {
  842. opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
  843. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  844. opt->bitrate_mode);
  845. }
  846. break;
  847. case V4L2_CID_MPEG_VIDEO_BITRATE: {
  848. uint bitrate = ctrl->value / 100000;
  849. opt->bitrate = bitrate;
  850. if (bitrate >= opt->peak_bitrate)
  851. opt->peak_bitrate = bitrate+1;
  852. hdpvr_set_bitrate(dev);
  853. break;
  854. }
  855. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
  856. uint peak_bitrate = ctrl->value / 100000;
  857. if (opt->bitrate_mode == HDPVR_CONSTANT)
  858. break;
  859. if (opt->bitrate < peak_bitrate) {
  860. opt->peak_bitrate = peak_bitrate;
  861. hdpvr_set_bitrate(dev);
  862. } else
  863. ret = -EINVAL;
  864. break;
  865. }
  866. case V4L2_CID_MPEG_STREAM_TYPE:
  867. break;
  868. default:
  869. return -EINVAL;
  870. }
  871. return ret;
  872. }
  873. static int vidioc_s_ext_ctrls(struct file *file, void *priv,
  874. struct v4l2_ext_controls *ctrls)
  875. {
  876. struct hdpvr_fh *fh = file->private_data;
  877. struct hdpvr_device *dev = fh->dev;
  878. int i, err = 0;
  879. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  880. for (i = 0; i < ctrls->count; i++) {
  881. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  882. err = hdpvr_try_ctrl(ctrl,
  883. dev->flags & HDPVR_FLAG_AC3_CAP);
  884. if (err) {
  885. ctrls->error_idx = i;
  886. break;
  887. }
  888. err = hdpvr_set_ctrl(dev, ctrl);
  889. if (err) {
  890. ctrls->error_idx = i;
  891. break;
  892. }
  893. }
  894. return err;
  895. }
  896. return -EINVAL;
  897. }
  898. static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
  899. struct v4l2_fmtdesc *f)
  900. {
  901. if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  902. return -EINVAL;
  903. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  904. strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
  905. f->pixelformat = V4L2_PIX_FMT_MPEG;
  906. return 0;
  907. }
  908. static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
  909. struct v4l2_format *f)
  910. {
  911. struct hdpvr_fh *fh = file->private_data;
  912. struct hdpvr_device *dev = fh->dev;
  913. struct hdpvr_video_info *vid_info;
  914. if (!dev)
  915. return -ENODEV;
  916. vid_info = get_video_info(dev);
  917. if (!vid_info)
  918. return -EFAULT;
  919. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  920. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  921. f->fmt.pix.width = vid_info->width;
  922. f->fmt.pix.height = vid_info->height;
  923. f->fmt.pix.sizeimage = dev->bulk_in_size;
  924. f->fmt.pix.colorspace = 0;
  925. f->fmt.pix.bytesperline = 0;
  926. f->fmt.pix.field = V4L2_FIELD_ANY;
  927. kfree(vid_info);
  928. return 0;
  929. }
  930. static int vidioc_encoder_cmd(struct file *filp, void *priv,
  931. struct v4l2_encoder_cmd *a)
  932. {
  933. struct hdpvr_fh *fh = filp->private_data;
  934. struct hdpvr_device *dev = fh->dev;
  935. int res;
  936. mutex_lock(&dev->io_mutex);
  937. memset(&a->raw, 0, sizeof(a->raw));
  938. switch (a->cmd) {
  939. case V4L2_ENC_CMD_START:
  940. a->flags = 0;
  941. res = hdpvr_start_streaming(dev);
  942. break;
  943. case V4L2_ENC_CMD_STOP:
  944. res = hdpvr_stop_streaming(dev);
  945. break;
  946. default:
  947. v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
  948. "Unsupported encoder cmd %d\n", a->cmd);
  949. return -EINVAL;
  950. }
  951. mutex_unlock(&dev->io_mutex);
  952. return res;
  953. }
  954. static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
  955. struct v4l2_encoder_cmd *a)
  956. {
  957. switch (a->cmd) {
  958. case V4L2_ENC_CMD_START:
  959. case V4L2_ENC_CMD_STOP:
  960. return 0;
  961. default:
  962. return -EINVAL;
  963. }
  964. }
  965. static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
  966. .vidioc_querycap = vidioc_querycap,
  967. .vidioc_s_std = vidioc_s_std,
  968. .vidioc_enum_input = vidioc_enum_input,
  969. .vidioc_g_input = vidioc_g_input,
  970. .vidioc_s_input = vidioc_s_input,
  971. .vidioc_enumaudio = vidioc_enumaudio,
  972. .vidioc_g_audio = vidioc_g_audio,
  973. .vidioc_s_audio = vidioc_s_audio,
  974. .vidioc_queryctrl = vidioc_queryctrl,
  975. .vidioc_g_ctrl = vidioc_g_ctrl,
  976. .vidioc_s_ctrl = vidioc_s_ctrl,
  977. .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
  978. .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
  979. .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
  980. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  981. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  982. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  983. .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
  984. };
  985. static void hdpvr_device_release(struct video_device *vdev)
  986. {
  987. struct hdpvr_device *dev = video_get_drvdata(vdev);
  988. hdpvr_delete(dev);
  989. }
  990. static const struct video_device hdpvr_video_template = {
  991. /* .type = VFL_TYPE_GRABBER, */
  992. /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
  993. .fops = &hdpvr_fops,
  994. .release = hdpvr_device_release,
  995. .ioctl_ops = &hdpvr_ioctl_ops,
  996. .tvnorms =
  997. V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
  998. V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
  999. V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
  1000. V4L2_STD_PAL_60,
  1001. };
  1002. int hdpvr_register_videodev(struct hdpvr_device *dev, int devnum)
  1003. {
  1004. /* setup and register video device */
  1005. dev->video_dev = video_device_alloc();
  1006. if (!dev->video_dev) {
  1007. err("video_device_alloc() failed");
  1008. goto error;
  1009. }
  1010. *(dev->video_dev) = hdpvr_video_template;
  1011. strcpy(dev->video_dev->name, "Hauppauge HD PVR");
  1012. dev->video_dev->parent = &dev->udev->dev;
  1013. video_set_drvdata(dev->video_dev, dev);
  1014. if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
  1015. err("V4L2 device registration failed");
  1016. goto error;
  1017. }
  1018. return 0;
  1019. error:
  1020. return -ENOMEM;
  1021. }