hdpvr-video.c 25 KB

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