hdpvr-video.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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 "hdpvr.h"
  26. #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
  27. #define print_buffer_status() { \
  28. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
  29. "%s:%d buffer stat: %d free, %d proc\n", \
  30. __func__, __LINE__, \
  31. list_size(&dev->free_buff_list), \
  32. list_size(&dev->rec_buff_list)); }
  33. static uint list_size(struct list_head *list)
  34. {
  35. struct list_head *tmp;
  36. uint count = 0;
  37. list_for_each(tmp, list) {
  38. count++;
  39. }
  40. return count;
  41. }
  42. /*=========================================================================*/
  43. /* urb callback */
  44. static void hdpvr_read_bulk_callback(struct urb *urb)
  45. {
  46. struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
  47. struct hdpvr_device *dev = buf->dev;
  48. /* marking buffer as received and wake waiting */
  49. buf->status = BUFSTAT_READY;
  50. wake_up_interruptible(&dev->wait_data);
  51. }
  52. /*=========================================================================*/
  53. /* bufffer bits */
  54. /* function expects dev->io_mutex to be hold by caller */
  55. int hdpvr_cancel_queue(struct hdpvr_device *dev)
  56. {
  57. struct hdpvr_buffer *buf;
  58. list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
  59. usb_kill_urb(buf->urb);
  60. buf->status = BUFSTAT_AVAILABLE;
  61. }
  62. list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
  63. return 0;
  64. }
  65. static int hdpvr_free_queue(struct list_head *q)
  66. {
  67. struct list_head *tmp;
  68. struct list_head *p;
  69. struct hdpvr_buffer *buf;
  70. struct urb *urb;
  71. for (p = q->next; p != q;) {
  72. buf = list_entry(p, struct hdpvr_buffer, buff_list);
  73. urb = buf->urb;
  74. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  75. urb->transfer_buffer, urb->transfer_dma);
  76. usb_free_urb(urb);
  77. tmp = p->next;
  78. list_del(p);
  79. kfree(buf);
  80. p = tmp;
  81. }
  82. return 0;
  83. }
  84. /* function expects dev->io_mutex to be hold by caller */
  85. int hdpvr_free_buffers(struct hdpvr_device *dev)
  86. {
  87. hdpvr_cancel_queue(dev);
  88. hdpvr_free_queue(&dev->free_buff_list);
  89. hdpvr_free_queue(&dev->rec_buff_list);
  90. return 0;
  91. }
  92. /* function expects dev->io_mutex to be hold by caller */
  93. int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
  94. {
  95. uint i;
  96. int retval = -ENOMEM;
  97. u8 *mem;
  98. struct hdpvr_buffer *buf;
  99. struct urb *urb;
  100. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  101. "allocating %u buffers\n", count);
  102. for (i = 0; i < count; i++) {
  103. buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
  104. if (!buf) {
  105. v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
  106. goto exit;
  107. }
  108. buf->dev = dev;
  109. urb = usb_alloc_urb(0, GFP_KERNEL);
  110. if (!urb) {
  111. v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
  112. goto exit_urb;
  113. }
  114. buf->urb = urb;
  115. mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
  116. &urb->transfer_dma);
  117. if (!mem) {
  118. v4l2_err(&dev->v4l2_dev,
  119. "cannot allocate usb transfer buffer\n");
  120. goto exit_urb_buffer;
  121. }
  122. usb_fill_bulk_urb(buf->urb, dev->udev,
  123. usb_rcvbulkpipe(dev->udev,
  124. dev->bulk_in_endpointAddr),
  125. mem, dev->bulk_in_size,
  126. hdpvr_read_bulk_callback, buf);
  127. buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  128. buf->status = BUFSTAT_AVAILABLE;
  129. list_add_tail(&buf->buff_list, &dev->free_buff_list);
  130. }
  131. return 0;
  132. exit_urb_buffer:
  133. usb_free_urb(urb);
  134. exit_urb:
  135. kfree(buf);
  136. exit:
  137. hdpvr_free_buffers(dev);
  138. return retval;
  139. }
  140. static int hdpvr_submit_buffers(struct hdpvr_device *dev)
  141. {
  142. struct hdpvr_buffer *buf;
  143. struct urb *urb;
  144. int ret = 0, err_count = 0;
  145. mutex_lock(&dev->io_mutex);
  146. while (dev->status == STATUS_STREAMING &&
  147. !list_empty(&dev->free_buff_list)) {
  148. buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
  149. buff_list);
  150. if (buf->status != BUFSTAT_AVAILABLE) {
  151. v4l2_err(&dev->v4l2_dev,
  152. "buffer not marked as available\n");
  153. ret = -EFAULT;
  154. goto err;
  155. }
  156. urb = buf->urb;
  157. urb->status = 0;
  158. urb->actual_length = 0;
  159. ret = usb_submit_urb(urb, GFP_KERNEL);
  160. if (ret) {
  161. v4l2_err(&dev->v4l2_dev,
  162. "usb_submit_urb in %s returned %d\n",
  163. __func__, ret);
  164. if (++err_count > 2)
  165. break;
  166. continue;
  167. }
  168. buf->status = BUFSTAT_INPROGRESS;
  169. list_move_tail(&buf->buff_list, &dev->rec_buff_list);
  170. }
  171. err:
  172. print_buffer_status();
  173. mutex_unlock(&dev->io_mutex);
  174. return ret;
  175. }
  176. static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
  177. {
  178. struct hdpvr_buffer *buf;
  179. mutex_lock(&dev->io_mutex);
  180. if (list_empty(&dev->rec_buff_list)) {
  181. mutex_unlock(&dev->io_mutex);
  182. return NULL;
  183. }
  184. buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
  185. buff_list);
  186. mutex_unlock(&dev->io_mutex);
  187. return buf;
  188. }
  189. static void hdpvr_transmit_buffers(struct work_struct *work)
  190. {
  191. struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
  192. worker);
  193. while (dev->status == STATUS_STREAMING) {
  194. if (hdpvr_submit_buffers(dev)) {
  195. v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
  196. goto error;
  197. }
  198. if (wait_event_interruptible(dev->wait_buffer,
  199. !list_empty(&dev->free_buff_list) ||
  200. dev->status != STATUS_STREAMING))
  201. goto error;
  202. }
  203. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  204. "transmit worker exited\n");
  205. return;
  206. error:
  207. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  208. "transmit buffers errored\n");
  209. dev->status = STATUS_ERROR;
  210. }
  211. /* function expects dev->io_mutex to be hold by caller */
  212. static int hdpvr_start_streaming(struct hdpvr_device *dev)
  213. {
  214. int ret;
  215. struct hdpvr_video_info *vidinf;
  216. if (dev->status == STATUS_STREAMING)
  217. return 0;
  218. else if (dev->status != STATUS_IDLE)
  219. return -EAGAIN;
  220. vidinf = get_video_info(dev);
  221. if (vidinf) {
  222. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  223. "video signal: %dx%d@%dhz\n", vidinf->width,
  224. vidinf->height, vidinf->fps);
  225. kfree(vidinf);
  226. /* start streaming 2 request */
  227. ret = usb_control_msg(dev->udev,
  228. usb_sndctrlpipe(dev->udev, 0),
  229. 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
  230. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  231. "encoder start control request returned %d\n", ret);
  232. hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
  233. dev->status = STATUS_STREAMING;
  234. INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
  235. queue_work(dev->workqueue, &dev->worker);
  236. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  237. "streaming started\n");
  238. return 0;
  239. }
  240. msleep(250);
  241. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  242. "no video signal at input %d\n", dev->options.video_input);
  243. return -EAGAIN;
  244. }
  245. /* function expects dev->io_mutex to be hold by caller */
  246. static int hdpvr_stop_streaming(struct hdpvr_device *dev)
  247. {
  248. int actual_length;
  249. uint c = 0;
  250. u8 *buf;
  251. if (dev->status == STATUS_IDLE)
  252. return 0;
  253. else if (dev->status != STATUS_STREAMING)
  254. return -EAGAIN;
  255. buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
  256. if (!buf)
  257. v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
  258. "for emptying the internal device buffer. "
  259. "Next capture start will be slow\n");
  260. dev->status = STATUS_SHUTTING_DOWN;
  261. hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
  262. mutex_unlock(&dev->io_mutex);
  263. wake_up_interruptible(&dev->wait_buffer);
  264. msleep(50);
  265. flush_workqueue(dev->workqueue);
  266. mutex_lock(&dev->io_mutex);
  267. /* kill the still outstanding urbs */
  268. hdpvr_cancel_queue(dev);
  269. /* emptying the device buffer beforeshutting it down */
  270. while (buf && ++c < 500 &&
  271. !usb_bulk_msg(dev->udev,
  272. usb_rcvbulkpipe(dev->udev,
  273. dev->bulk_in_endpointAddr),
  274. buf, dev->bulk_in_size, &actual_length,
  275. BULK_URB_TIMEOUT)) {
  276. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  277. "%2d: got %d bytes\n", c, actual_length);
  278. }
  279. kfree(buf);
  280. v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
  281. "used %d urbs to empty device buffers\n", c-1);
  282. msleep(10);
  283. dev->status = STATUS_IDLE;
  284. return 0;
  285. }
  286. /*=======================================================================*/
  287. /*
  288. * video 4 linux 2 file operations
  289. */
  290. static int hdpvr_release(struct file *file)
  291. {
  292. struct hdpvr_device *dev = video_drvdata(file);
  293. if (!dev)
  294. return -ENODEV;
  295. mutex_lock(&dev->io_mutex);
  296. if (file->private_data == dev->owner) {
  297. hdpvr_stop_streaming(dev);
  298. dev->owner = NULL;
  299. }
  300. mutex_unlock(&dev->io_mutex);
  301. return v4l2_fh_release(file);
  302. }
  303. /*
  304. * hdpvr_v4l2_read()
  305. * will allocate buffers when called for the first time
  306. */
  307. static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
  308. loff_t *pos)
  309. {
  310. struct hdpvr_device *dev = video_drvdata(file);
  311. struct hdpvr_buffer *buf = NULL;
  312. struct urb *urb;
  313. unsigned int ret = 0;
  314. int rem, cnt;
  315. if (*pos)
  316. return -ESPIPE;
  317. if (!dev)
  318. return -ENODEV;
  319. mutex_lock(&dev->io_mutex);
  320. if (dev->status == STATUS_IDLE) {
  321. if (hdpvr_start_streaming(dev)) {
  322. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  323. "start_streaming failed\n");
  324. ret = -EIO;
  325. msleep(200);
  326. dev->status = STATUS_IDLE;
  327. mutex_unlock(&dev->io_mutex);
  328. goto err;
  329. }
  330. dev->owner = file->private_data;
  331. print_buffer_status();
  332. }
  333. mutex_unlock(&dev->io_mutex);
  334. /* wait for the first buffer */
  335. if (!(file->f_flags & O_NONBLOCK)) {
  336. if (wait_event_interruptible(dev->wait_data,
  337. hdpvr_get_next_buffer(dev)))
  338. return -ERESTARTSYS;
  339. }
  340. buf = hdpvr_get_next_buffer(dev);
  341. while (count > 0 && buf) {
  342. if (buf->status != BUFSTAT_READY &&
  343. dev->status != STATUS_DISCONNECTED) {
  344. /* return nonblocking */
  345. if (file->f_flags & O_NONBLOCK) {
  346. if (!ret)
  347. ret = -EAGAIN;
  348. goto err;
  349. }
  350. if (wait_event_interruptible(dev->wait_data,
  351. buf->status == BUFSTAT_READY)) {
  352. ret = -ERESTARTSYS;
  353. goto err;
  354. }
  355. }
  356. if (buf->status != BUFSTAT_READY)
  357. break;
  358. /* set remaining bytes to copy */
  359. urb = buf->urb;
  360. rem = urb->actual_length - buf->pos;
  361. cnt = rem > count ? count : rem;
  362. if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
  363. cnt)) {
  364. v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
  365. if (!ret)
  366. ret = -EFAULT;
  367. goto err;
  368. }
  369. buf->pos += cnt;
  370. count -= cnt;
  371. buffer += cnt;
  372. ret += cnt;
  373. /* finished, take next buffer */
  374. if (buf->pos == urb->actual_length) {
  375. mutex_lock(&dev->io_mutex);
  376. buf->pos = 0;
  377. buf->status = BUFSTAT_AVAILABLE;
  378. list_move_tail(&buf->buff_list, &dev->free_buff_list);
  379. print_buffer_status();
  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_buffer *buf = NULL;
  393. struct hdpvr_device *dev = video_drvdata(filp);
  394. unsigned int mask = 0;
  395. mutex_lock(&dev->io_mutex);
  396. if (!video_is_registered(dev->video_dev)) {
  397. mutex_unlock(&dev->io_mutex);
  398. return -EIO;
  399. }
  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->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  441. V4L2_CAP_AUDIO |
  442. V4L2_CAP_READWRITE;
  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 -EAGAIN;
  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 -EAGAIN;
  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 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  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. };
  734. static void hdpvr_device_release(struct video_device *vdev)
  735. {
  736. struct hdpvr_device *dev = video_get_drvdata(vdev);
  737. hdpvr_delete(dev);
  738. mutex_lock(&dev->io_mutex);
  739. destroy_workqueue(dev->workqueue);
  740. mutex_unlock(&dev->io_mutex);
  741. v4l2_device_unregister(&dev->v4l2_dev);
  742. v4l2_ctrl_handler_free(&dev->hdl);
  743. /* deregister I2C adapter */
  744. #if IS_ENABLED(CONFIG_I2C)
  745. mutex_lock(&dev->i2c_mutex);
  746. i2c_del_adapter(&dev->i2c_adapter);
  747. mutex_unlock(&dev->i2c_mutex);
  748. #endif /* CONFIG_I2C */
  749. kfree(dev->usbc_buf);
  750. kfree(dev);
  751. }
  752. static const struct video_device hdpvr_video_template = {
  753. .fops = &hdpvr_fops,
  754. .release = hdpvr_device_release,
  755. .ioctl_ops = &hdpvr_ioctl_ops,
  756. .tvnorms =
  757. V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
  758. V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
  759. V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
  760. V4L2_STD_PAL_60,
  761. .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |
  762. V4L2_STD_PAL_60,
  763. };
  764. static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
  765. .try_ctrl = hdpvr_try_ctrl,
  766. .s_ctrl = hdpvr_s_ctrl,
  767. };
  768. int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
  769. int devnum)
  770. {
  771. struct v4l2_ctrl_handler *hdl = &dev->hdl;
  772. bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
  773. int res;
  774. v4l2_ctrl_handler_init(hdl, 11);
  775. if (dev->fw_ver > 0x15) {
  776. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  777. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
  778. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  779. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
  780. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  781. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
  782. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  783. V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
  784. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  785. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  786. } else {
  787. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  788. V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
  789. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  790. V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
  791. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  792. V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
  793. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  794. V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
  795. v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  796. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
  797. }
  798. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  799. V4L2_CID_MPEG_STREAM_TYPE,
  800. V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
  801. 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
  802. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  803. V4L2_CID_MPEG_AUDIO_ENCODING,
  804. ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
  805. 0x7, V4L2_MPEG_AUDIO_ENCODING_AAC);
  806. v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  807. V4L2_CID_MPEG_VIDEO_ENCODING,
  808. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
  809. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  810. dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
  811. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  812. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
  813. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  814. dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  815. V4L2_CID_MPEG_VIDEO_BITRATE,
  816. 1000000, 13500000, 100000, 6500000);
  817. dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
  818. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  819. 1100000, 20200000, 100000, 9000000);
  820. dev->v4l2_dev.ctrl_handler = hdl;
  821. if (hdl->error) {
  822. res = hdl->error;
  823. v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
  824. goto error;
  825. }
  826. v4l2_ctrl_cluster(3, &dev->video_mode);
  827. res = v4l2_ctrl_handler_setup(hdl);
  828. if (res < 0) {
  829. v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
  830. goto error;
  831. }
  832. /* setup and register video device */
  833. dev->video_dev = video_device_alloc();
  834. if (!dev->video_dev) {
  835. v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
  836. res = -ENOMEM;
  837. goto error;
  838. }
  839. *dev->video_dev = hdpvr_video_template;
  840. strcpy(dev->video_dev->name, "Hauppauge HD PVR");
  841. dev->video_dev->v4l2_dev = &dev->v4l2_dev;
  842. video_set_drvdata(dev->video_dev, dev);
  843. res = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum);
  844. if (res < 0) {
  845. v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
  846. goto error;
  847. }
  848. return 0;
  849. error:
  850. v4l2_ctrl_handler_free(hdl);
  851. return res;
  852. }