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