hdpvr-video.c 28 KB

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