hdpvr-video.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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 const s32 supported_v4l2_ctrls[] = {
  572. V4L2_CID_BRIGHTNESS,
  573. V4L2_CID_CONTRAST,
  574. V4L2_CID_SATURATION,
  575. V4L2_CID_HUE,
  576. V4L2_CID_SHARPNESS,
  577. V4L2_CID_MPEG_AUDIO_ENCODING,
  578. V4L2_CID_MPEG_VIDEO_ENCODING,
  579. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  580. V4L2_CID_MPEG_VIDEO_BITRATE,
  581. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  582. };
  583. static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
  584. int ac3, int fw_ver)
  585. {
  586. int err;
  587. if (fw_ver > 0x15) {
  588. switch (qc->id) {
  589. case V4L2_CID_BRIGHTNESS:
  590. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  591. case V4L2_CID_CONTRAST:
  592. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x40);
  593. case V4L2_CID_SATURATION:
  594. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x40);
  595. case V4L2_CID_HUE:
  596. return v4l2_ctrl_query_fill(qc, 0x0, 0x1e, 1, 0xf);
  597. case V4L2_CID_SHARPNESS:
  598. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  599. }
  600. } else {
  601. switch (qc->id) {
  602. case V4L2_CID_BRIGHTNESS:
  603. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
  604. case V4L2_CID_CONTRAST:
  605. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  606. case V4L2_CID_SATURATION:
  607. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  608. case V4L2_CID_HUE:
  609. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  610. case V4L2_CID_SHARPNESS:
  611. return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
  612. }
  613. }
  614. switch (qc->id) {
  615. case V4L2_CID_MPEG_AUDIO_ENCODING:
  616. return v4l2_ctrl_query_fill(
  617. qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
  618. ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
  619. : V4L2_MPEG_AUDIO_ENCODING_AAC,
  620. 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
  621. case V4L2_CID_MPEG_VIDEO_ENCODING:
  622. return v4l2_ctrl_query_fill(
  623. qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
  624. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
  625. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  626. /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
  627. /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
  628. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  629. return v4l2_ctrl_query_fill(
  630. qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  631. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
  632. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  633. case V4L2_CID_MPEG_VIDEO_BITRATE:
  634. return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
  635. 6500000);
  636. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  637. err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
  638. 9000000);
  639. if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
  640. qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
  641. return err;
  642. default:
  643. return -EINVAL;
  644. }
  645. }
  646. static int vidioc_queryctrl(struct file *file, void *private_data,
  647. struct v4l2_queryctrl *qc)
  648. {
  649. struct hdpvr_fh *fh = file->private_data;
  650. struct hdpvr_device *dev = fh->dev;
  651. int i, next;
  652. u32 id = qc->id;
  653. memset(qc, 0, sizeof(*qc));
  654. next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
  655. qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
  656. for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
  657. if (next) {
  658. if (qc->id < supported_v4l2_ctrls[i])
  659. qc->id = supported_v4l2_ctrls[i];
  660. else
  661. continue;
  662. }
  663. if (qc->id == supported_v4l2_ctrls[i])
  664. return fill_queryctrl(&dev->options, qc,
  665. dev->flags & HDPVR_FLAG_AC3_CAP,
  666. dev->fw_ver);
  667. if (qc->id < supported_v4l2_ctrls[i])
  668. break;
  669. }
  670. return -EINVAL;
  671. }
  672. static int vidioc_g_ctrl(struct file *file, void *private_data,
  673. struct v4l2_control *ctrl)
  674. {
  675. struct hdpvr_fh *fh = file->private_data;
  676. struct hdpvr_device *dev = fh->dev;
  677. switch (ctrl->id) {
  678. case V4L2_CID_BRIGHTNESS:
  679. ctrl->value = dev->options.brightness;
  680. break;
  681. case V4L2_CID_CONTRAST:
  682. ctrl->value = dev->options.contrast;
  683. break;
  684. case V4L2_CID_SATURATION:
  685. ctrl->value = dev->options.saturation;
  686. break;
  687. case V4L2_CID_HUE:
  688. ctrl->value = dev->options.hue;
  689. break;
  690. case V4L2_CID_SHARPNESS:
  691. ctrl->value = dev->options.sharpness;
  692. break;
  693. default:
  694. return -EINVAL;
  695. }
  696. return 0;
  697. }
  698. static int vidioc_s_ctrl(struct file *file, void *private_data,
  699. struct v4l2_control *ctrl)
  700. {
  701. struct hdpvr_fh *fh = file->private_data;
  702. struct hdpvr_device *dev = fh->dev;
  703. int retval;
  704. switch (ctrl->id) {
  705. case V4L2_CID_BRIGHTNESS:
  706. retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
  707. if (!retval)
  708. dev->options.brightness = ctrl->value;
  709. break;
  710. case V4L2_CID_CONTRAST:
  711. retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
  712. if (!retval)
  713. dev->options.contrast = ctrl->value;
  714. break;
  715. case V4L2_CID_SATURATION:
  716. retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
  717. if (!retval)
  718. dev->options.saturation = ctrl->value;
  719. break;
  720. case V4L2_CID_HUE:
  721. retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
  722. if (!retval)
  723. dev->options.hue = ctrl->value;
  724. break;
  725. case V4L2_CID_SHARPNESS:
  726. retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
  727. if (!retval)
  728. dev->options.sharpness = ctrl->value;
  729. break;
  730. default:
  731. return -EINVAL;
  732. }
  733. return retval;
  734. }
  735. static int hdpvr_get_ctrl(struct hdpvr_options *opt,
  736. struct v4l2_ext_control *ctrl)
  737. {
  738. switch (ctrl->id) {
  739. case V4L2_CID_MPEG_AUDIO_ENCODING:
  740. ctrl->value = opt->audio_codec;
  741. break;
  742. case V4L2_CID_MPEG_VIDEO_ENCODING:
  743. ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
  744. break;
  745. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  746. /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
  747. /* break; */
  748. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  749. ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
  750. ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
  751. : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
  752. break;
  753. case V4L2_CID_MPEG_VIDEO_BITRATE:
  754. ctrl->value = opt->bitrate * 100000;
  755. break;
  756. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  757. ctrl->value = opt->peak_bitrate * 100000;
  758. break;
  759. case V4L2_CID_MPEG_STREAM_TYPE:
  760. ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
  761. break;
  762. default:
  763. return -EINVAL;
  764. }
  765. return 0;
  766. }
  767. static int vidioc_g_ext_ctrls(struct file *file, void *priv,
  768. struct v4l2_ext_controls *ctrls)
  769. {
  770. struct hdpvr_fh *fh = file->private_data;
  771. struct hdpvr_device *dev = fh->dev;
  772. int i, err = 0;
  773. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  774. for (i = 0; i < ctrls->count; i++) {
  775. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  776. err = hdpvr_get_ctrl(&dev->options, ctrl);
  777. if (err) {
  778. ctrls->error_idx = i;
  779. break;
  780. }
  781. }
  782. return err;
  783. }
  784. return -EINVAL;
  785. }
  786. static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
  787. {
  788. int ret = -EINVAL;
  789. switch (ctrl->id) {
  790. case V4L2_CID_MPEG_AUDIO_ENCODING:
  791. if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
  792. (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
  793. ret = 0;
  794. break;
  795. case V4L2_CID_MPEG_VIDEO_ENCODING:
  796. if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
  797. ret = 0;
  798. break;
  799. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  800. /* if (ctrl->value == 0 || ctrl->value == 128) */
  801. /* ret = 0; */
  802. /* break; */
  803. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  804. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
  805. ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
  806. ret = 0;
  807. break;
  808. case V4L2_CID_MPEG_VIDEO_BITRATE:
  809. {
  810. uint bitrate = ctrl->value / 100000;
  811. if (bitrate >= 10 && bitrate <= 135)
  812. ret = 0;
  813. break;
  814. }
  815. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  816. {
  817. uint peak_bitrate = ctrl->value / 100000;
  818. if (peak_bitrate >= 10 && peak_bitrate <= 202)
  819. ret = 0;
  820. break;
  821. }
  822. case V4L2_CID_MPEG_STREAM_TYPE:
  823. if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
  824. ret = 0;
  825. break;
  826. default:
  827. return -EINVAL;
  828. }
  829. return ret;
  830. }
  831. static int vidioc_try_ext_ctrls(struct file *file, void *priv,
  832. struct v4l2_ext_controls *ctrls)
  833. {
  834. struct hdpvr_fh *fh = file->private_data;
  835. struct hdpvr_device *dev = fh->dev;
  836. int i, err = 0;
  837. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  838. for (i = 0; i < ctrls->count; i++) {
  839. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  840. err = hdpvr_try_ctrl(ctrl,
  841. dev->flags & HDPVR_FLAG_AC3_CAP);
  842. if (err) {
  843. ctrls->error_idx = i;
  844. break;
  845. }
  846. }
  847. return err;
  848. }
  849. return -EINVAL;
  850. }
  851. static int hdpvr_set_ctrl(struct hdpvr_device *dev,
  852. struct v4l2_ext_control *ctrl)
  853. {
  854. struct hdpvr_options *opt = &dev->options;
  855. int ret = 0;
  856. switch (ctrl->id) {
  857. case V4L2_CID_MPEG_AUDIO_ENCODING:
  858. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  859. opt->audio_codec = ctrl->value;
  860. ret = hdpvr_set_audio(dev, opt->audio_input,
  861. opt->audio_codec);
  862. }
  863. break;
  864. case V4L2_CID_MPEG_VIDEO_ENCODING:
  865. break;
  866. /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
  867. /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
  868. /* opt->gop_mode |= 0x2; */
  869. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  870. /* opt->gop_mode); */
  871. /* } */
  872. /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
  873. /* opt->gop_mode &= ~0x2; */
  874. /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
  875. /* opt->gop_mode); */
  876. /* } */
  877. /* break; */
  878. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  879. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
  880. opt->bitrate_mode != HDPVR_CONSTANT) {
  881. opt->bitrate_mode = HDPVR_CONSTANT;
  882. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  883. opt->bitrate_mode);
  884. }
  885. if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  886. opt->bitrate_mode == HDPVR_CONSTANT) {
  887. opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
  888. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  889. opt->bitrate_mode);
  890. }
  891. break;
  892. case V4L2_CID_MPEG_VIDEO_BITRATE: {
  893. uint bitrate = ctrl->value / 100000;
  894. opt->bitrate = bitrate;
  895. if (bitrate >= opt->peak_bitrate)
  896. opt->peak_bitrate = bitrate+1;
  897. hdpvr_set_bitrate(dev);
  898. break;
  899. }
  900. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
  901. uint peak_bitrate = ctrl->value / 100000;
  902. if (opt->bitrate_mode == HDPVR_CONSTANT)
  903. break;
  904. if (opt->bitrate < peak_bitrate) {
  905. opt->peak_bitrate = peak_bitrate;
  906. hdpvr_set_bitrate(dev);
  907. } else
  908. ret = -EINVAL;
  909. break;
  910. }
  911. case V4L2_CID_MPEG_STREAM_TYPE:
  912. break;
  913. default:
  914. return -EINVAL;
  915. }
  916. return ret;
  917. }
  918. static int vidioc_s_ext_ctrls(struct file *file, void *priv,
  919. struct v4l2_ext_controls *ctrls)
  920. {
  921. struct hdpvr_fh *fh = file->private_data;
  922. struct hdpvr_device *dev = fh->dev;
  923. int i, err = 0;
  924. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  925. for (i = 0; i < ctrls->count; i++) {
  926. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  927. err = hdpvr_try_ctrl(ctrl,
  928. dev->flags & HDPVR_FLAG_AC3_CAP);
  929. if (err) {
  930. ctrls->error_idx = i;
  931. break;
  932. }
  933. err = hdpvr_set_ctrl(dev, ctrl);
  934. if (err) {
  935. ctrls->error_idx = i;
  936. break;
  937. }
  938. }
  939. return err;
  940. }
  941. return -EINVAL;
  942. }
  943. static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
  944. struct v4l2_fmtdesc *f)
  945. {
  946. if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  947. return -EINVAL;
  948. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  949. strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
  950. f->pixelformat = V4L2_PIX_FMT_MPEG;
  951. return 0;
  952. }
  953. static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
  954. struct v4l2_format *f)
  955. {
  956. struct hdpvr_fh *fh = file->private_data;
  957. struct hdpvr_device *dev = fh->dev;
  958. struct hdpvr_video_info *vid_info;
  959. if (!dev)
  960. return -ENODEV;
  961. vid_info = get_video_info(dev);
  962. if (!vid_info)
  963. return -EFAULT;
  964. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  965. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  966. f->fmt.pix.width = vid_info->width;
  967. f->fmt.pix.height = vid_info->height;
  968. f->fmt.pix.sizeimage = dev->bulk_in_size;
  969. f->fmt.pix.colorspace = 0;
  970. f->fmt.pix.bytesperline = 0;
  971. f->fmt.pix.field = V4L2_FIELD_ANY;
  972. kfree(vid_info);
  973. return 0;
  974. }
  975. static int vidioc_encoder_cmd(struct file *filp, void *priv,
  976. struct v4l2_encoder_cmd *a)
  977. {
  978. struct hdpvr_fh *fh = filp->private_data;
  979. struct hdpvr_device *dev = fh->dev;
  980. int res;
  981. mutex_lock(&dev->io_mutex);
  982. memset(&a->raw, 0, sizeof(a->raw));
  983. switch (a->cmd) {
  984. case V4L2_ENC_CMD_START:
  985. a->flags = 0;
  986. res = hdpvr_start_streaming(dev);
  987. break;
  988. case V4L2_ENC_CMD_STOP:
  989. res = hdpvr_stop_streaming(dev);
  990. break;
  991. default:
  992. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  993. "Unsupported encoder cmd %d\n", a->cmd);
  994. res = -EINVAL;
  995. }
  996. mutex_unlock(&dev->io_mutex);
  997. return res;
  998. }
  999. static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
  1000. struct v4l2_encoder_cmd *a)
  1001. {
  1002. switch (a->cmd) {
  1003. case V4L2_ENC_CMD_START:
  1004. case V4L2_ENC_CMD_STOP:
  1005. return 0;
  1006. default:
  1007. return -EINVAL;
  1008. }
  1009. }
  1010. static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
  1011. .vidioc_querycap = vidioc_querycap,
  1012. .vidioc_s_std = vidioc_s_std,
  1013. .vidioc_enum_input = vidioc_enum_input,
  1014. .vidioc_g_input = vidioc_g_input,
  1015. .vidioc_s_input = vidioc_s_input,
  1016. .vidioc_enumaudio = vidioc_enumaudio,
  1017. .vidioc_g_audio = vidioc_g_audio,
  1018. .vidioc_s_audio = vidioc_s_audio,
  1019. .vidioc_queryctrl = vidioc_queryctrl,
  1020. .vidioc_g_ctrl = vidioc_g_ctrl,
  1021. .vidioc_s_ctrl = vidioc_s_ctrl,
  1022. .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
  1023. .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
  1024. .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
  1025. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1026. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1027. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  1028. .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
  1029. };
  1030. static void hdpvr_device_release(struct video_device *vdev)
  1031. {
  1032. struct hdpvr_device *dev = video_get_drvdata(vdev);
  1033. hdpvr_delete(dev);
  1034. mutex_lock(&dev->io_mutex);
  1035. destroy_workqueue(dev->workqueue);
  1036. mutex_unlock(&dev->io_mutex);
  1037. v4l2_device_unregister(&dev->v4l2_dev);
  1038. /* deregister I2C adapter */
  1039. #if IS_ENABLED(CONFIG_I2C)
  1040. mutex_lock(&dev->i2c_mutex);
  1041. i2c_del_adapter(&dev->i2c_adapter);
  1042. mutex_unlock(&dev->i2c_mutex);
  1043. #endif /* CONFIG_I2C */
  1044. kfree(dev->usbc_buf);
  1045. kfree(dev);
  1046. }
  1047. static const struct video_device hdpvr_video_template = {
  1048. /* .type = VFL_TYPE_GRABBER, */
  1049. /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
  1050. .fops = &hdpvr_fops,
  1051. .release = hdpvr_device_release,
  1052. .ioctl_ops = &hdpvr_ioctl_ops,
  1053. .tvnorms =
  1054. V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
  1055. V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
  1056. V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
  1057. V4L2_STD_PAL_60,
  1058. .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |
  1059. V4L2_STD_PAL_60,
  1060. };
  1061. int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
  1062. int devnum)
  1063. {
  1064. /* setup and register video device */
  1065. dev->video_dev = video_device_alloc();
  1066. if (!dev->video_dev) {
  1067. v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
  1068. goto error;
  1069. }
  1070. *(dev->video_dev) = hdpvr_video_template;
  1071. strcpy(dev->video_dev->name, "Hauppauge HD PVR");
  1072. dev->video_dev->parent = parent;
  1073. video_set_drvdata(dev->video_dev, dev);
  1074. if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
  1075. v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
  1076. goto error;
  1077. }
  1078. return 0;
  1079. error:
  1080. return -ENOMEM;
  1081. }