hdpvr-video.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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 (dev->options.video_input == HDPVR_COMPONENT)
  451. return -ENODATA;
  452. if (dev->status != STATUS_IDLE)
  453. return -EBUSY;
  454. if (std & V4L2_STD_525_60)
  455. std_type = 0;
  456. dev->cur_std = std;
  457. dev->width = 720;
  458. dev->height = std_type ? 576 : 480;
  459. return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
  460. }
  461. static int vidioc_g_std(struct file *file, void *private_data,
  462. v4l2_std_id *std)
  463. {
  464. struct hdpvr_device *dev = video_drvdata(file);
  465. if (dev->options.video_input == HDPVR_COMPONENT)
  466. return -ENODATA;
  467. *std = dev->cur_std;
  468. return 0;
  469. }
  470. static int vidioc_querystd(struct file *file, void *fh, v4l2_std_id *a)
  471. {
  472. struct hdpvr_device *dev = video_drvdata(file);
  473. struct hdpvr_video_info *vid_info;
  474. if (dev->options.video_input == HDPVR_COMPONENT)
  475. return -ENODATA;
  476. *a = V4L2_STD_ALL;
  477. vid_info = get_video_info(dev);
  478. if (vid_info == NULL)
  479. return 0;
  480. if (vid_info->width == 720 &&
  481. (vid_info->height == 480 || vid_info->height == 576)) {
  482. *a = (vid_info->height == 480) ?
  483. V4L2_STD_525_60 : V4L2_STD_625_50;
  484. }
  485. kfree(vid_info);
  486. return 0;
  487. }
  488. static const char *iname[] = {
  489. [HDPVR_COMPONENT] = "Component",
  490. [HDPVR_SVIDEO] = "S-Video",
  491. [HDPVR_COMPOSITE] = "Composite",
  492. };
  493. static int vidioc_enum_input(struct file *file, void *priv,
  494. struct v4l2_input *i)
  495. {
  496. unsigned int n;
  497. n = i->index;
  498. if (n >= HDPVR_VIDEO_INPUTS)
  499. return -EINVAL;
  500. i->type = V4L2_INPUT_TYPE_CAMERA;
  501. strncpy(i->name, iname[n], sizeof(i->name) - 1);
  502. i->name[sizeof(i->name) - 1] = '\0';
  503. i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
  504. i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
  505. i->std = n ? V4L2_STD_ALL : 0;
  506. return 0;
  507. }
  508. static int vidioc_s_input(struct file *file, void *private_data,
  509. unsigned int index)
  510. {
  511. struct hdpvr_device *dev = video_drvdata(file);
  512. int retval;
  513. if (index >= HDPVR_VIDEO_INPUTS)
  514. return -EINVAL;
  515. if (dev->status != STATUS_IDLE)
  516. return -EBUSY;
  517. retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
  518. if (!retval) {
  519. dev->options.video_input = index;
  520. dev->video_dev->tvnorms =
  521. index != HDPVR_COMPONENT ? V4L2_STD_ALL : 0;
  522. }
  523. return retval;
  524. }
  525. static int vidioc_g_input(struct file *file, void *private_data,
  526. unsigned int *index)
  527. {
  528. struct hdpvr_device *dev = video_drvdata(file);
  529. *index = dev->options.video_input;
  530. return 0;
  531. }
  532. static const char *audio_iname[] = {
  533. [HDPVR_RCA_FRONT] = "RCA front",
  534. [HDPVR_RCA_BACK] = "RCA back",
  535. [HDPVR_SPDIF] = "SPDIF",
  536. };
  537. static int vidioc_enumaudio(struct file *file, void *priv,
  538. struct v4l2_audio *audio)
  539. {
  540. unsigned int n;
  541. n = audio->index;
  542. if (n >= HDPVR_AUDIO_INPUTS)
  543. return -EINVAL;
  544. audio->capability = V4L2_AUDCAP_STEREO;
  545. strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
  546. audio->name[sizeof(audio->name) - 1] = '\0';
  547. return 0;
  548. }
  549. static int vidioc_s_audio(struct file *file, void *private_data,
  550. const struct v4l2_audio *audio)
  551. {
  552. struct hdpvr_device *dev = video_drvdata(file);
  553. int retval;
  554. if (audio->index >= HDPVR_AUDIO_INPUTS)
  555. return -EINVAL;
  556. if (dev->status != STATUS_IDLE)
  557. return -EBUSY;
  558. retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
  559. if (!retval)
  560. dev->options.audio_input = audio->index;
  561. return retval;
  562. }
  563. static int vidioc_g_audio(struct file *file, void *private_data,
  564. struct v4l2_audio *audio)
  565. {
  566. struct hdpvr_device *dev = video_drvdata(file);
  567. audio->index = dev->options.audio_input;
  568. audio->capability = V4L2_AUDCAP_STEREO;
  569. strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
  570. audio->name[sizeof(audio->name) - 1] = '\0';
  571. return 0;
  572. }
  573. static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
  574. {
  575. struct hdpvr_device *dev =
  576. container_of(ctrl->handler, struct hdpvr_device, hdl);
  577. switch (ctrl->id) {
  578. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  579. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  580. dev->video_bitrate->val >= dev->video_bitrate_peak->val)
  581. dev->video_bitrate_peak->val =
  582. dev->video_bitrate->val + 100000;
  583. break;
  584. }
  585. return 0;
  586. }
  587. static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
  588. {
  589. struct hdpvr_device *dev =
  590. container_of(ctrl->handler, struct hdpvr_device, hdl);
  591. struct hdpvr_options *opt = &dev->options;
  592. int ret = -EINVAL;
  593. switch (ctrl->id) {
  594. case V4L2_CID_BRIGHTNESS:
  595. ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
  596. if (ret)
  597. break;
  598. dev->options.brightness = ctrl->val;
  599. return 0;
  600. case V4L2_CID_CONTRAST:
  601. ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
  602. if (ret)
  603. break;
  604. dev->options.contrast = ctrl->val;
  605. return 0;
  606. case V4L2_CID_SATURATION:
  607. ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
  608. if (ret)
  609. break;
  610. dev->options.saturation = ctrl->val;
  611. return 0;
  612. case V4L2_CID_HUE:
  613. ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
  614. if (ret)
  615. break;
  616. dev->options.hue = ctrl->val;
  617. return 0;
  618. case V4L2_CID_SHARPNESS:
  619. ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
  620. if (ret)
  621. break;
  622. dev->options.sharpness = ctrl->val;
  623. return 0;
  624. case V4L2_CID_MPEG_AUDIO_ENCODING:
  625. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  626. opt->audio_codec = ctrl->val;
  627. return hdpvr_set_audio(dev, opt->audio_input,
  628. opt->audio_codec);
  629. }
  630. return 0;
  631. case V4L2_CID_MPEG_VIDEO_ENCODING:
  632. return 0;
  633. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  634. /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
  635. /* opt->gop_mode |= 0x2; */
  636. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  637. /* opt->gop_mode); */
  638. /* } */
  639. /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
  640. /* opt->gop_mode &= ~0x2; */
  641. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  642. /* opt->gop_mode); */
  643. /* } */
  644. /* break; */
  645. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
  646. uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
  647. uint bitrate = dev->video_bitrate->val / 100000;
  648. if (ctrl->is_new) {
  649. if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
  650. opt->bitrate_mode = HDPVR_CONSTANT;
  651. else
  652. opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
  653. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  654. opt->bitrate_mode);
  655. v4l2_ctrl_activate(dev->video_bitrate_peak,
  656. ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  657. }
  658. if (dev->video_bitrate_peak->is_new ||
  659. dev->video_bitrate->is_new) {
  660. opt->bitrate = bitrate;
  661. opt->peak_bitrate = peak_bitrate;
  662. hdpvr_set_bitrate(dev);
  663. }
  664. return 0;
  665. }
  666. case V4L2_CID_MPEG_STREAM_TYPE:
  667. return 0;
  668. default:
  669. break;
  670. }
  671. return ret;
  672. }
  673. static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
  674. struct v4l2_fmtdesc *f)
  675. {
  676. if (f->index != 0)
  677. return -EINVAL;
  678. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  679. strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
  680. f->pixelformat = V4L2_PIX_FMT_MPEG;
  681. return 0;
  682. }
  683. static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
  684. struct v4l2_format *f)
  685. {
  686. struct hdpvr_device *dev = video_drvdata(file);
  687. struct hdpvr_video_info *vid_info;
  688. if (!dev)
  689. return -ENODEV;
  690. vid_info = get_video_info(dev);
  691. if (!vid_info)
  692. return -EFAULT;
  693. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  694. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  695. f->fmt.pix.width = vid_info->width;
  696. f->fmt.pix.height = vid_info->height;
  697. f->fmt.pix.sizeimage = dev->bulk_in_size;
  698. f->fmt.pix.colorspace = 0;
  699. f->fmt.pix.bytesperline = 0;
  700. f->fmt.pix.field = V4L2_FIELD_ANY;
  701. kfree(vid_info);
  702. return 0;
  703. }
  704. static int vidioc_encoder_cmd(struct file *filp, void *priv,
  705. struct v4l2_encoder_cmd *a)
  706. {
  707. struct hdpvr_device *dev = video_drvdata(filp);
  708. int res = 0;
  709. mutex_lock(&dev->io_mutex);
  710. a->flags = 0;
  711. switch (a->cmd) {
  712. case V4L2_ENC_CMD_START:
  713. if (dev->owner && filp->private_data != dev->owner) {
  714. res = -EBUSY;
  715. break;
  716. }
  717. if (dev->status == STATUS_STREAMING)
  718. break;
  719. res = hdpvr_start_streaming(dev);
  720. if (!res)
  721. dev->owner = filp->private_data;
  722. else
  723. dev->status = STATUS_IDLE;
  724. break;
  725. case V4L2_ENC_CMD_STOP:
  726. if (dev->owner && filp->private_data != dev->owner) {
  727. res = -EBUSY;
  728. break;
  729. }
  730. if (dev->status == STATUS_IDLE)
  731. break;
  732. res = hdpvr_stop_streaming(dev);
  733. if (!res)
  734. dev->owner = NULL;
  735. break;
  736. default:
  737. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  738. "Unsupported encoder cmd %d\n", a->cmd);
  739. res = -EINVAL;
  740. break;
  741. }
  742. mutex_unlock(&dev->io_mutex);
  743. return res;
  744. }
  745. static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
  746. struct v4l2_encoder_cmd *a)
  747. {
  748. a->flags = 0;
  749. switch (a->cmd) {
  750. case V4L2_ENC_CMD_START:
  751. case V4L2_ENC_CMD_STOP:
  752. return 0;
  753. default:
  754. return -EINVAL;
  755. }
  756. }
  757. static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
  758. .vidioc_querycap = vidioc_querycap,
  759. .vidioc_s_std = vidioc_s_std,
  760. .vidioc_g_std = vidioc_g_std,
  761. .vidioc_querystd = vidioc_querystd,
  762. .vidioc_enum_input = vidioc_enum_input,
  763. .vidioc_g_input = vidioc_g_input,
  764. .vidioc_s_input = vidioc_s_input,
  765. .vidioc_enumaudio = vidioc_enumaudio,
  766. .vidioc_g_audio = vidioc_g_audio,
  767. .vidioc_s_audio = vidioc_s_audio,
  768. .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
  769. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  770. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  771. .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
  772. .vidioc_log_status = v4l2_ctrl_log_status,
  773. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  774. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  775. };
  776. static void hdpvr_device_release(struct video_device *vdev)
  777. {
  778. struct hdpvr_device *dev = video_get_drvdata(vdev);
  779. hdpvr_delete(dev);
  780. mutex_lock(&dev->io_mutex);
  781. destroy_workqueue(dev->workqueue);
  782. mutex_unlock(&dev->io_mutex);
  783. v4l2_device_unregister(&dev->v4l2_dev);
  784. v4l2_ctrl_handler_free(&dev->hdl);
  785. /* deregister I2C adapter */
  786. #if IS_ENABLED(CONFIG_I2C)
  787. mutex_lock(&dev->i2c_mutex);
  788. i2c_del_adapter(&dev->i2c_adapter);
  789. mutex_unlock(&dev->i2c_mutex);
  790. #endif /* CONFIG_I2C */
  791. kfree(dev->usbc_buf);
  792. kfree(dev);
  793. }
  794. static const struct video_device hdpvr_video_template = {
  795. .fops = &hdpvr_fops,
  796. .release = hdpvr_device_release,
  797. .ioctl_ops = &hdpvr_ioctl_ops,
  798. };
  799. static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
  800. .try_ctrl = hdpvr_try_ctrl,
  801. .s_ctrl = hdpvr_s_ctrl,
  802. };
  803. int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
  804. int devnum)
  805. {
  806. struct v4l2_ctrl_handler *hdl = &dev->hdl;
  807. bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
  808. int res;
  809. dev->cur_std = V4L2_STD_525_60;
  810. dev->width = 720;
  811. dev->height = 480;
  812. v4l2_ctrl_handler_init(hdl, 11);
  813. if (dev->fw_ver > 0x15) {
  814. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  815. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
  816. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  817. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
  818. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  819. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
  820. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  821. V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
  822. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  823. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  824. } else {
  825. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  826. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
  827. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  828. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
  829. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  830. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
  831. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  832. V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
  833. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  834. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  835. }
  836. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  837. V4L2_CID_MPEG_STREAM_TYPE,
  838. V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
  839. 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
  840. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  841. V4L2_CID_MPEG_AUDIO_ENCODING,
  842. ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
  843. 0x7, V4L2_MPEG_AUDIO_ENCODING_AAC);
  844. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  845. V4L2_CID_MPEG_VIDEO_ENCODING,
  846. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
  847. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  848. dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  849. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  850. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
  851. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  852. dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  853. V4L2_CID_MPEG_VIDEO_BITRATE,
  854. 1000000, 13500000, 100000, 6500000);
  855. dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  856. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  857. 1100000, 20200000, 100000, 9000000);
  858. dev->v4l2_dev.ctrl_handler = hdl;
  859. if (hdl->error) {
  860. res = hdl->error;
  861. v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
  862. goto error;
  863. }
  864. v4l2_ctrl_cluster(3, &dev->video_mode);
  865. res = v4l2_ctrl_handler_setup(hdl);
  866. if (res < 0) {
  867. v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
  868. goto error;
  869. }
  870. /* setup and register video device */
  871. dev->video_dev = video_device_alloc();
  872. if (!dev->video_dev) {
  873. v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
  874. res = -ENOMEM;
  875. goto error;
  876. }
  877. *dev->video_dev = hdpvr_video_template;
  878. strcpy(dev->video_dev->name, "Hauppauge HD PVR");
  879. dev->video_dev->v4l2_dev = &dev->v4l2_dev;
  880. video_set_drvdata(dev->video_dev, dev);
  881. set_bit(V4L2_FL_USE_FH_PRIO, &dev->video_dev->flags);
  882. res = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum);
  883. if (res < 0) {
  884. v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
  885. goto error;
  886. }
  887. return 0;
  888. error:
  889. v4l2_ctrl_handler_free(hdl);
  890. return res;
  891. }