hdpvr-video.c 28 KB

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