hdpvr-video.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. * Hauppauge HD PVR USB driver - video 4 linux 2 interface
  3. *
  4. * Copyright (C) 2008 Janne Grunau (j@jannau.net)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/kconfig.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/usb.h>
  19. #include <linux/mutex.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/videodev2.h>
  22. #include <media/v4l2-dev.h>
  23. #include <media/v4l2-common.h>
  24. #include <media/v4l2-ioctl.h>
  25. #include <media/v4l2-event.h>
  26. #include "hdpvr.h"
  27. #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
  28. #define print_buffer_status() { \
  29. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
  30. "%s:%d buffer stat: %d free, %d proc\n", \
  31. __func__, __LINE__, \
  32. list_size(&dev->free_buff_list), \
  33. list_size(&dev->rec_buff_list)); }
  34. static uint list_size(struct list_head *list)
  35. {
  36. struct list_head *tmp;
  37. uint count = 0;
  38. list_for_each(tmp, list) {
  39. count++;
  40. }
  41. return count;
  42. }
  43. /*=========================================================================*/
  44. /* urb callback */
  45. static void hdpvr_read_bulk_callback(struct urb *urb)
  46. {
  47. struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
  48. struct hdpvr_device *dev = buf->dev;
  49. /* marking buffer as received and wake waiting */
  50. buf->status = BUFSTAT_READY;
  51. wake_up_interruptible(&dev->wait_data);
  52. }
  53. /*=========================================================================*/
  54. /* bufffer bits */
  55. /* function expects dev->io_mutex to be hold by caller */
  56. int hdpvr_cancel_queue(struct hdpvr_device *dev)
  57. {
  58. struct hdpvr_buffer *buf;
  59. list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
  60. usb_kill_urb(buf->urb);
  61. buf->status = BUFSTAT_AVAILABLE;
  62. }
  63. list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
  64. return 0;
  65. }
  66. static int hdpvr_free_queue(struct list_head *q)
  67. {
  68. struct list_head *tmp;
  69. struct list_head *p;
  70. struct hdpvr_buffer *buf;
  71. struct urb *urb;
  72. for (p = q->next; p != q;) {
  73. buf = list_entry(p, struct hdpvr_buffer, buff_list);
  74. urb = buf->urb;
  75. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  76. urb->transfer_buffer, urb->transfer_dma);
  77. usb_free_urb(urb);
  78. tmp = p->next;
  79. list_del(p);
  80. kfree(buf);
  81. p = tmp;
  82. }
  83. return 0;
  84. }
  85. /* function expects dev->io_mutex to be hold by caller */
  86. int hdpvr_free_buffers(struct hdpvr_device *dev)
  87. {
  88. hdpvr_cancel_queue(dev);
  89. hdpvr_free_queue(&dev->free_buff_list);
  90. hdpvr_free_queue(&dev->rec_buff_list);
  91. return 0;
  92. }
  93. /* function expects dev->io_mutex to be hold by caller */
  94. int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
  95. {
  96. uint i;
  97. int retval = -ENOMEM;
  98. u8 *mem;
  99. struct hdpvr_buffer *buf;
  100. struct urb *urb;
  101. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  102. "allocating %u buffers\n", count);
  103. for (i = 0; i < count; i++) {
  104. buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
  105. if (!buf) {
  106. v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
  107. goto exit;
  108. }
  109. buf->dev = dev;
  110. urb = usb_alloc_urb(0, GFP_KERNEL);
  111. if (!urb) {
  112. v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
  113. goto exit_urb;
  114. }
  115. buf->urb = urb;
  116. mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
  117. &urb->transfer_dma);
  118. if (!mem) {
  119. v4l2_err(&dev->v4l2_dev,
  120. "cannot allocate usb transfer buffer\n");
  121. goto exit_urb_buffer;
  122. }
  123. usb_fill_bulk_urb(buf->urb, dev->udev,
  124. usb_rcvbulkpipe(dev->udev,
  125. dev->bulk_in_endpointAddr),
  126. mem, dev->bulk_in_size,
  127. hdpvr_read_bulk_callback, buf);
  128. buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  129. buf->status = BUFSTAT_AVAILABLE;
  130. list_add_tail(&buf->buff_list, &dev->free_buff_list);
  131. }
  132. return 0;
  133. exit_urb_buffer:
  134. usb_free_urb(urb);
  135. exit_urb:
  136. kfree(buf);
  137. exit:
  138. hdpvr_free_buffers(dev);
  139. return retval;
  140. }
  141. static int hdpvr_submit_buffers(struct hdpvr_device *dev)
  142. {
  143. struct hdpvr_buffer *buf;
  144. struct urb *urb;
  145. int ret = 0, err_count = 0;
  146. mutex_lock(&dev->io_mutex);
  147. while (dev->status == STATUS_STREAMING &&
  148. !list_empty(&dev->free_buff_list)) {
  149. buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
  150. buff_list);
  151. if (buf->status != BUFSTAT_AVAILABLE) {
  152. v4l2_err(&dev->v4l2_dev,
  153. "buffer not marked as available\n");
  154. ret = -EFAULT;
  155. goto err;
  156. }
  157. urb = buf->urb;
  158. urb->status = 0;
  159. urb->actual_length = 0;
  160. ret = usb_submit_urb(urb, GFP_KERNEL);
  161. if (ret) {
  162. v4l2_err(&dev->v4l2_dev,
  163. "usb_submit_urb in %s returned %d\n",
  164. __func__, ret);
  165. if (++err_count > 2)
  166. break;
  167. continue;
  168. }
  169. buf->status = BUFSTAT_INPROGRESS;
  170. list_move_tail(&buf->buff_list, &dev->rec_buff_list);
  171. }
  172. err:
  173. print_buffer_status();
  174. mutex_unlock(&dev->io_mutex);
  175. return ret;
  176. }
  177. static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
  178. {
  179. struct hdpvr_buffer *buf;
  180. mutex_lock(&dev->io_mutex);
  181. if (list_empty(&dev->rec_buff_list)) {
  182. mutex_unlock(&dev->io_mutex);
  183. return NULL;
  184. }
  185. buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
  186. buff_list);
  187. mutex_unlock(&dev->io_mutex);
  188. return buf;
  189. }
  190. static void hdpvr_transmit_buffers(struct work_struct *work)
  191. {
  192. struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
  193. worker);
  194. while (dev->status == STATUS_STREAMING) {
  195. if (hdpvr_submit_buffers(dev)) {
  196. v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
  197. goto error;
  198. }
  199. if (wait_event_interruptible(dev->wait_buffer,
  200. !list_empty(&dev->free_buff_list) ||
  201. dev->status != STATUS_STREAMING))
  202. goto error;
  203. }
  204. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  205. "transmit worker exited\n");
  206. return;
  207. error:
  208. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  209. "transmit buffers errored\n");
  210. dev->status = STATUS_ERROR;
  211. }
  212. /* function expects dev->io_mutex to be hold by caller */
  213. static int hdpvr_start_streaming(struct hdpvr_device *dev)
  214. {
  215. int ret;
  216. struct hdpvr_video_info *vidinf;
  217. if (dev->status == STATUS_STREAMING)
  218. return 0;
  219. else if (dev->status != STATUS_IDLE)
  220. return -EAGAIN;
  221. vidinf = get_video_info(dev);
  222. if (vidinf) {
  223. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  224. "video signal: %dx%d@%dhz\n", vidinf->width,
  225. vidinf->height, vidinf->fps);
  226. kfree(vidinf);
  227. /* start streaming 2 request */
  228. ret = usb_control_msg(dev->udev,
  229. usb_sndctrlpipe(dev->udev, 0),
  230. 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
  231. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  232. "encoder start control request returned %d\n", ret);
  233. hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
  234. dev->status = STATUS_STREAMING;
  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. return 0;
  240. }
  241. msleep(250);
  242. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  243. "no video signal at input %d\n", dev->options.video_input);
  244. return -EAGAIN;
  245. }
  246. /* function expects dev->io_mutex to be hold by caller */
  247. static int hdpvr_stop_streaming(struct hdpvr_device *dev)
  248. {
  249. int actual_length;
  250. uint c = 0;
  251. u8 *buf;
  252. if (dev->status == STATUS_IDLE)
  253. return 0;
  254. else if (dev->status != STATUS_STREAMING)
  255. return -EAGAIN;
  256. buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
  257. if (!buf)
  258. v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
  259. "for emptying the internal device buffer. "
  260. "Next capture start will be slow\n");
  261. dev->status = STATUS_SHUTTING_DOWN;
  262. hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
  263. mutex_unlock(&dev->io_mutex);
  264. wake_up_interruptible(&dev->wait_buffer);
  265. msleep(50);
  266. flush_workqueue(dev->workqueue);
  267. mutex_lock(&dev->io_mutex);
  268. /* kill the still outstanding urbs */
  269. hdpvr_cancel_queue(dev);
  270. /* emptying the device buffer beforeshutting it down */
  271. while (buf && ++c < 500 &&
  272. !usb_bulk_msg(dev->udev,
  273. usb_rcvbulkpipe(dev->udev,
  274. dev->bulk_in_endpointAddr),
  275. buf, dev->bulk_in_size, &actual_length,
  276. BULK_URB_TIMEOUT)) {
  277. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  278. "%2d: got %d bytes\n", c, actual_length);
  279. }
  280. kfree(buf);
  281. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  282. "used %d urbs to empty device buffers\n", c-1);
  283. msleep(10);
  284. dev->status = STATUS_IDLE;
  285. return 0;
  286. }
  287. /*=======================================================================*/
  288. /*
  289. * video 4 linux 2 file operations
  290. */
  291. static int hdpvr_release(struct file *file)
  292. {
  293. struct hdpvr_device *dev = video_drvdata(file);
  294. if (!dev)
  295. return -ENODEV;
  296. mutex_lock(&dev->io_mutex);
  297. if (file->private_data == dev->owner) {
  298. hdpvr_stop_streaming(dev);
  299. dev->owner = NULL;
  300. }
  301. mutex_unlock(&dev->io_mutex);
  302. return v4l2_fh_release(file);
  303. }
  304. /*
  305. * hdpvr_v4l2_read()
  306. * will allocate buffers when called for the first time
  307. */
  308. static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
  309. loff_t *pos)
  310. {
  311. struct hdpvr_device *dev = video_drvdata(file);
  312. struct hdpvr_buffer *buf = NULL;
  313. struct urb *urb;
  314. unsigned int ret = 0;
  315. int rem, cnt;
  316. if (*pos)
  317. return -ESPIPE;
  318. if (!dev)
  319. return -ENODEV;
  320. mutex_lock(&dev->io_mutex);
  321. if (dev->status == STATUS_IDLE) {
  322. if (hdpvr_start_streaming(dev)) {
  323. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  324. "start_streaming failed\n");
  325. ret = -EIO;
  326. msleep(200);
  327. dev->status = STATUS_IDLE;
  328. mutex_unlock(&dev->io_mutex);
  329. goto err;
  330. }
  331. dev->owner = file->private_data;
  332. print_buffer_status();
  333. }
  334. mutex_unlock(&dev->io_mutex);
  335. /* wait for the first buffer */
  336. if (!(file->f_flags & O_NONBLOCK)) {
  337. if (wait_event_interruptible(dev->wait_data,
  338. hdpvr_get_next_buffer(dev)))
  339. return -ERESTARTSYS;
  340. }
  341. buf = hdpvr_get_next_buffer(dev);
  342. while (count > 0 && buf) {
  343. if (buf->status != BUFSTAT_READY &&
  344. dev->status != STATUS_DISCONNECTED) {
  345. /* return nonblocking */
  346. if (file->f_flags & O_NONBLOCK) {
  347. if (!ret)
  348. ret = -EAGAIN;
  349. goto err;
  350. }
  351. if (wait_event_interruptible(dev->wait_data,
  352. buf->status == BUFSTAT_READY)) {
  353. ret = -ERESTARTSYS;
  354. goto err;
  355. }
  356. }
  357. if (buf->status != BUFSTAT_READY)
  358. break;
  359. /* set remaining bytes to copy */
  360. urb = buf->urb;
  361. rem = urb->actual_length - buf->pos;
  362. cnt = rem > count ? count : rem;
  363. if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
  364. cnt)) {
  365. v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
  366. if (!ret)
  367. ret = -EFAULT;
  368. goto err;
  369. }
  370. buf->pos += cnt;
  371. count -= cnt;
  372. buffer += cnt;
  373. ret += cnt;
  374. /* finished, take next buffer */
  375. if (buf->pos == urb->actual_length) {
  376. mutex_lock(&dev->io_mutex);
  377. buf->pos = 0;
  378. buf->status = BUFSTAT_AVAILABLE;
  379. list_move_tail(&buf->buff_list, &dev->free_buff_list);
  380. print_buffer_status();
  381. mutex_unlock(&dev->io_mutex);
  382. wake_up_interruptible(&dev->wait_buffer);
  383. buf = hdpvr_get_next_buffer(dev);
  384. }
  385. }
  386. err:
  387. if (!ret && !buf)
  388. ret = -EAGAIN;
  389. return ret;
  390. }
  391. static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
  392. {
  393. unsigned long req_events = poll_requested_events(wait);
  394. struct hdpvr_buffer *buf = NULL;
  395. struct hdpvr_device *dev = video_drvdata(filp);
  396. unsigned int mask = v4l2_ctrl_poll(filp, wait);
  397. if (!(req_events & (POLLIN | POLLRDNORM)))
  398. return mask;
  399. mutex_lock(&dev->io_mutex);
  400. if (dev->status == STATUS_IDLE) {
  401. if (hdpvr_start_streaming(dev)) {
  402. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  403. "start_streaming failed\n");
  404. dev->status = STATUS_IDLE;
  405. } else {
  406. dev->owner = filp->private_data;
  407. }
  408. print_buffer_status();
  409. }
  410. mutex_unlock(&dev->io_mutex);
  411. buf = hdpvr_get_next_buffer(dev);
  412. /* only wait if no data is available */
  413. if (!buf || buf->status != BUFSTAT_READY) {
  414. poll_wait(filp, &dev->wait_data, wait);
  415. buf = hdpvr_get_next_buffer(dev);
  416. }
  417. if (buf && buf->status == BUFSTAT_READY)
  418. mask |= POLLIN | POLLRDNORM;
  419. return mask;
  420. }
  421. static const struct v4l2_file_operations hdpvr_fops = {
  422. .owner = THIS_MODULE,
  423. .open = v4l2_fh_open,
  424. .release = hdpvr_release,
  425. .read = hdpvr_read,
  426. .poll = hdpvr_poll,
  427. .unlocked_ioctl = video_ioctl2,
  428. };
  429. /*=======================================================================*/
  430. /*
  431. * V4L2 ioctl handling
  432. */
  433. static int vidioc_querycap(struct file *file, void *priv,
  434. struct v4l2_capability *cap)
  435. {
  436. struct hdpvr_device *dev = video_drvdata(file);
  437. strcpy(cap->driver, "hdpvr");
  438. strcpy(cap->card, "Hauppauge HD PVR");
  439. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  440. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO |
  441. V4L2_CAP_READWRITE;
  442. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  443. return 0;
  444. }
  445. static int vidioc_s_std(struct file *file, void *private_data,
  446. v4l2_std_id std)
  447. {
  448. struct hdpvr_device *dev = video_drvdata(file);
  449. u8 std_type = 1;
  450. if (std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
  451. std_type = 0;
  452. return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
  453. }
  454. static const char *iname[] = {
  455. [HDPVR_COMPONENT] = "Component",
  456. [HDPVR_SVIDEO] = "S-Video",
  457. [HDPVR_COMPOSITE] = "Composite",
  458. };
  459. static int vidioc_enum_input(struct file *file, void *priv,
  460. struct v4l2_input *i)
  461. {
  462. struct hdpvr_device *dev = video_drvdata(file);
  463. unsigned int n;
  464. n = i->index;
  465. if (n >= HDPVR_VIDEO_INPUTS)
  466. return -EINVAL;
  467. i->type = V4L2_INPUT_TYPE_CAMERA;
  468. strncpy(i->name, iname[n], sizeof(i->name) - 1);
  469. i->name[sizeof(i->name) - 1] = '\0';
  470. i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
  471. i->std = dev->video_dev->tvnorms;
  472. return 0;
  473. }
  474. static int vidioc_s_input(struct file *file, void *private_data,
  475. unsigned int index)
  476. {
  477. struct hdpvr_device *dev = video_drvdata(file);
  478. int retval;
  479. if (index >= HDPVR_VIDEO_INPUTS)
  480. return -EINVAL;
  481. if (dev->status != STATUS_IDLE)
  482. return -EBUSY;
  483. retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
  484. if (!retval)
  485. dev->options.video_input = index;
  486. return retval;
  487. }
  488. static int vidioc_g_input(struct file *file, void *private_data,
  489. unsigned int *index)
  490. {
  491. struct hdpvr_device *dev = video_drvdata(file);
  492. *index = dev->options.video_input;
  493. return 0;
  494. }
  495. static const char *audio_iname[] = {
  496. [HDPVR_RCA_FRONT] = "RCA front",
  497. [HDPVR_RCA_BACK] = "RCA back",
  498. [HDPVR_SPDIF] = "SPDIF",
  499. };
  500. static int vidioc_enumaudio(struct file *file, void *priv,
  501. struct v4l2_audio *audio)
  502. {
  503. unsigned int n;
  504. n = audio->index;
  505. if (n >= HDPVR_AUDIO_INPUTS)
  506. return -EINVAL;
  507. audio->capability = V4L2_AUDCAP_STEREO;
  508. strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
  509. audio->name[sizeof(audio->name) - 1] = '\0';
  510. return 0;
  511. }
  512. static int vidioc_s_audio(struct file *file, void *private_data,
  513. const struct v4l2_audio *audio)
  514. {
  515. struct hdpvr_device *dev = video_drvdata(file);
  516. int retval;
  517. if (audio->index >= HDPVR_AUDIO_INPUTS)
  518. return -EINVAL;
  519. if (dev->status != STATUS_IDLE)
  520. return -EBUSY;
  521. retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
  522. if (!retval)
  523. dev->options.audio_input = audio->index;
  524. return retval;
  525. }
  526. static int vidioc_g_audio(struct file *file, void *private_data,
  527. struct v4l2_audio *audio)
  528. {
  529. struct hdpvr_device *dev = video_drvdata(file);
  530. audio->index = dev->options.audio_input;
  531. audio->capability = V4L2_AUDCAP_STEREO;
  532. strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
  533. audio->name[sizeof(audio->name) - 1] = '\0';
  534. return 0;
  535. }
  536. static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
  537. {
  538. struct hdpvr_device *dev =
  539. container_of(ctrl->handler, struct hdpvr_device, hdl);
  540. switch (ctrl->id) {
  541. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  542. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  543. dev->video_bitrate->val >= dev->video_bitrate_peak->val)
  544. dev->video_bitrate_peak->val =
  545. dev->video_bitrate->val + 100000;
  546. break;
  547. }
  548. return 0;
  549. }
  550. static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
  551. {
  552. struct hdpvr_device *dev =
  553. container_of(ctrl->handler, struct hdpvr_device, hdl);
  554. struct hdpvr_options *opt = &dev->options;
  555. int ret = -EINVAL;
  556. switch (ctrl->id) {
  557. case V4L2_CID_BRIGHTNESS:
  558. ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
  559. if (ret)
  560. break;
  561. dev->options.brightness = ctrl->val;
  562. return 0;
  563. case V4L2_CID_CONTRAST:
  564. ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
  565. if (ret)
  566. break;
  567. dev->options.contrast = ctrl->val;
  568. return 0;
  569. case V4L2_CID_SATURATION:
  570. ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
  571. if (ret)
  572. break;
  573. dev->options.saturation = ctrl->val;
  574. return 0;
  575. case V4L2_CID_HUE:
  576. ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
  577. if (ret)
  578. break;
  579. dev->options.hue = ctrl->val;
  580. return 0;
  581. case V4L2_CID_SHARPNESS:
  582. ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
  583. if (ret)
  584. break;
  585. dev->options.sharpness = ctrl->val;
  586. return 0;
  587. case V4L2_CID_MPEG_AUDIO_ENCODING:
  588. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  589. opt->audio_codec = ctrl->val;
  590. return hdpvr_set_audio(dev, opt->audio_input,
  591. opt->audio_codec);
  592. }
  593. return 0;
  594. case V4L2_CID_MPEG_VIDEO_ENCODING:
  595. return 0;
  596. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  597. /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
  598. /* opt->gop_mode |= 0x2; */
  599. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  600. /* opt->gop_mode); */
  601. /* } */
  602. /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
  603. /* opt->gop_mode &= ~0x2; */
  604. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  605. /* opt->gop_mode); */
  606. /* } */
  607. /* break; */
  608. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
  609. uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
  610. uint bitrate = dev->video_bitrate->val / 100000;
  611. if (ctrl->is_new) {
  612. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
  613. opt->bitrate_mode = HDPVR_CONSTANT;
  614. else
  615. opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
  616. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  617. opt->bitrate_mode);
  618. v4l2_ctrl_activate(dev->video_bitrate_peak,
  619. ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  620. }
  621. if (dev->video_bitrate_peak->is_new ||
  622. dev->video_bitrate->is_new) {
  623. opt->bitrate = bitrate;
  624. opt->peak_bitrate = peak_bitrate;
  625. hdpvr_set_bitrate(dev);
  626. }
  627. return 0;
  628. }
  629. case V4L2_CID_MPEG_STREAM_TYPE:
  630. return 0;
  631. default:
  632. break;
  633. }
  634. return ret;
  635. }
  636. static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
  637. struct v4l2_fmtdesc *f)
  638. {
  639. if (f->index != 0)
  640. return -EINVAL;
  641. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  642. strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
  643. f->pixelformat = V4L2_PIX_FMT_MPEG;
  644. return 0;
  645. }
  646. static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
  647. struct v4l2_format *f)
  648. {
  649. struct hdpvr_device *dev = video_drvdata(file);
  650. struct hdpvr_video_info *vid_info;
  651. if (!dev)
  652. return -ENODEV;
  653. vid_info = get_video_info(dev);
  654. if (!vid_info)
  655. return -EFAULT;
  656. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  657. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  658. f->fmt.pix.width = vid_info->width;
  659. f->fmt.pix.height = vid_info->height;
  660. f->fmt.pix.sizeimage = dev->bulk_in_size;
  661. f->fmt.pix.colorspace = 0;
  662. f->fmt.pix.bytesperline = 0;
  663. f->fmt.pix.field = V4L2_FIELD_ANY;
  664. kfree(vid_info);
  665. return 0;
  666. }
  667. static int vidioc_encoder_cmd(struct file *filp, void *priv,
  668. struct v4l2_encoder_cmd *a)
  669. {
  670. struct hdpvr_device *dev = video_drvdata(filp);
  671. int res = 0;
  672. mutex_lock(&dev->io_mutex);
  673. a->flags = 0;
  674. switch (a->cmd) {
  675. case V4L2_ENC_CMD_START:
  676. if (dev->owner && filp->private_data != dev->owner) {
  677. res = -EBUSY;
  678. break;
  679. }
  680. if (dev->status == STATUS_STREAMING)
  681. break;
  682. res = hdpvr_start_streaming(dev);
  683. if (!res)
  684. dev->owner = filp->private_data;
  685. else
  686. dev->status = STATUS_IDLE;
  687. break;
  688. case V4L2_ENC_CMD_STOP:
  689. if (dev->owner && filp->private_data != dev->owner) {
  690. res = -EBUSY;
  691. break;
  692. }
  693. if (dev->status == STATUS_IDLE)
  694. break;
  695. res = hdpvr_stop_streaming(dev);
  696. if (!res)
  697. dev->owner = NULL;
  698. break;
  699. default:
  700. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  701. "Unsupported encoder cmd %d\n", a->cmd);
  702. res = -EINVAL;
  703. break;
  704. }
  705. mutex_unlock(&dev->io_mutex);
  706. return res;
  707. }
  708. static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
  709. struct v4l2_encoder_cmd *a)
  710. {
  711. a->flags = 0;
  712. switch (a->cmd) {
  713. case V4L2_ENC_CMD_START:
  714. case V4L2_ENC_CMD_STOP:
  715. return 0;
  716. default:
  717. return -EINVAL;
  718. }
  719. }
  720. static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
  721. .vidioc_querycap = vidioc_querycap,
  722. .vidioc_s_std = vidioc_s_std,
  723. .vidioc_enum_input = vidioc_enum_input,
  724. .vidioc_g_input = vidioc_g_input,
  725. .vidioc_s_input = vidioc_s_input,
  726. .vidioc_enumaudio = vidioc_enumaudio,
  727. .vidioc_g_audio = vidioc_g_audio,
  728. .vidioc_s_audio = vidioc_s_audio,
  729. .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
  730. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  731. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  732. .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
  733. .vidioc_log_status = v4l2_ctrl_log_status,
  734. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  735. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  736. };
  737. static void hdpvr_device_release(struct video_device *vdev)
  738. {
  739. struct hdpvr_device *dev = video_get_drvdata(vdev);
  740. hdpvr_delete(dev);
  741. mutex_lock(&dev->io_mutex);
  742. destroy_workqueue(dev->workqueue);
  743. mutex_unlock(&dev->io_mutex);
  744. v4l2_device_unregister(&dev->v4l2_dev);
  745. v4l2_ctrl_handler_free(&dev->hdl);
  746. /* deregister I2C adapter */
  747. #if IS_ENABLED(CONFIG_I2C)
  748. mutex_lock(&dev->i2c_mutex);
  749. i2c_del_adapter(&dev->i2c_adapter);
  750. mutex_unlock(&dev->i2c_mutex);
  751. #endif /* CONFIG_I2C */
  752. kfree(dev->usbc_buf);
  753. kfree(dev);
  754. }
  755. static const struct video_device hdpvr_video_template = {
  756. .fops = &hdpvr_fops,
  757. .release = hdpvr_device_release,
  758. .ioctl_ops = &hdpvr_ioctl_ops,
  759. .tvnorms =
  760. V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
  761. V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
  762. V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
  763. V4L2_STD_PAL_60,
  764. .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |
  765. V4L2_STD_PAL_60,
  766. };
  767. static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
  768. .try_ctrl = hdpvr_try_ctrl,
  769. .s_ctrl = hdpvr_s_ctrl,
  770. };
  771. int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
  772. int devnum)
  773. {
  774. struct v4l2_ctrl_handler *hdl = &dev->hdl;
  775. bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
  776. int res;
  777. v4l2_ctrl_handler_init(hdl, 11);
  778. if (dev->fw_ver > 0x15) {
  779. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  780. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
  781. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  782. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
  783. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  784. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
  785. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  786. V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
  787. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  788. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  789. } else {
  790. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  791. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
  792. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  793. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
  794. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  795. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
  796. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  797. V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
  798. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  799. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  800. }
  801. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  802. V4L2_CID_MPEG_STREAM_TYPE,
  803. V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
  804. 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
  805. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  806. V4L2_CID_MPEG_AUDIO_ENCODING,
  807. ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
  808. 0x7, V4L2_MPEG_AUDIO_ENCODING_AAC);
  809. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  810. V4L2_CID_MPEG_VIDEO_ENCODING,
  811. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
  812. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  813. dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  814. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  815. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
  816. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  817. dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  818. V4L2_CID_MPEG_VIDEO_BITRATE,
  819. 1000000, 13500000, 100000, 6500000);
  820. dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  821. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  822. 1100000, 20200000, 100000, 9000000);
  823. dev->v4l2_dev.ctrl_handler = hdl;
  824. if (hdl->error) {
  825. res = hdl->error;
  826. v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
  827. goto error;
  828. }
  829. v4l2_ctrl_cluster(3, &dev->video_mode);
  830. res = v4l2_ctrl_handler_setup(hdl);
  831. if (res < 0) {
  832. v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
  833. goto error;
  834. }
  835. /* setup and register video device */
  836. dev->video_dev = video_device_alloc();
  837. if (!dev->video_dev) {
  838. v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
  839. res = -ENOMEM;
  840. goto error;
  841. }
  842. *dev->video_dev = hdpvr_video_template;
  843. strcpy(dev->video_dev->name, "Hauppauge HD PVR");
  844. dev->video_dev->v4l2_dev = &dev->v4l2_dev;
  845. video_set_drvdata(dev->video_dev, dev);
  846. set_bit(V4L2_FL_USE_FH_PRIO, &dev->video_dev->flags);
  847. res = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum);
  848. if (res < 0) {
  849. v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
  850. goto error;
  851. }
  852. return 0;
  853. error:
  854. v4l2_ctrl_handler_free(hdl);
  855. return res;
  856. }