PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/media/video/em28xx/em28xx-video.c

https://bitbucket.org/abioy/linux
C | 2579 lines | 1927 code | 455 blank | 197 comment | 371 complexity | b28f8b86652de0801c8e5b89e6d309ce MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
  3. video capture devices
  4. Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  5. Markus Rechberger <mrechberger@gmail.com>
  6. Mauro Carvalho Chehab <mchehab@infradead.org>
  7. Sascha Sommer <saschasommer@freenet.de>
  8. Some parts based on SN9C10x PC Camera Controllers GPL driver made
  9. by Luca Risolia <luca.risolia@studio.unibo.it>
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/bitmap.h>
  27. #include <linux/usb.h>
  28. #include <linux/i2c.h>
  29. #include <linux/version.h>
  30. #include <linux/mm.h>
  31. #include <linux/mutex.h>
  32. #include <linux/slab.h>
  33. #include "em28xx.h"
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-chip-ident.h>
  37. #include <media/msp3400.h>
  38. #include <media/tuner.h>
  39. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  40. "Markus Rechberger <mrechberger@gmail.com>, " \
  41. "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
  42. "Sascha Sommer <saschasommer@freenet.de>"
  43. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  44. #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
  45. #define em28xx_videodbg(fmt, arg...) do {\
  46. if (video_debug) \
  47. printk(KERN_INFO "%s %s :"fmt, \
  48. dev->name, __func__ , ##arg); } while (0)
  49. static unsigned int isoc_debug;
  50. module_param(isoc_debug, int, 0644);
  51. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  52. #define em28xx_isocdbg(fmt, arg...) \
  53. do {\
  54. if (isoc_debug) { \
  55. printk(KERN_INFO "%s %s :"fmt, \
  56. dev->name, __func__ , ##arg); \
  57. } \
  58. } while (0)
  59. MODULE_AUTHOR(DRIVER_AUTHOR);
  60. MODULE_DESCRIPTION(DRIVER_DESC);
  61. MODULE_LICENSE("GPL");
  62. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  63. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  64. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  65. module_param_array(video_nr, int, NULL, 0444);
  66. module_param_array(vbi_nr, int, NULL, 0444);
  67. module_param_array(radio_nr, int, NULL, 0444);
  68. MODULE_PARM_DESC(video_nr, "video device numbers");
  69. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  70. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  71. static unsigned int video_debug;
  72. module_param(video_debug, int, 0644);
  73. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  74. /* supported video standards */
  75. static struct em28xx_fmt format[] = {
  76. {
  77. .name = "16 bpp YUY2, 4:2:2, packed",
  78. .fourcc = V4L2_PIX_FMT_YUYV,
  79. .depth = 16,
  80. .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
  81. }, {
  82. .name = "16 bpp RGB 565, LE",
  83. .fourcc = V4L2_PIX_FMT_RGB565,
  84. .depth = 16,
  85. .reg = EM28XX_OUTFMT_RGB_16_656,
  86. }, {
  87. .name = "8 bpp Bayer BGBG..GRGR",
  88. .fourcc = V4L2_PIX_FMT_SBGGR8,
  89. .depth = 8,
  90. .reg = EM28XX_OUTFMT_RGB_8_BGBG,
  91. }, {
  92. .name = "8 bpp Bayer GRGR..BGBG",
  93. .fourcc = V4L2_PIX_FMT_SGRBG8,
  94. .depth = 8,
  95. .reg = EM28XX_OUTFMT_RGB_8_GRGR,
  96. }, {
  97. .name = "8 bpp Bayer GBGB..RGRG",
  98. .fourcc = V4L2_PIX_FMT_SGBRG8,
  99. .depth = 8,
  100. .reg = EM28XX_OUTFMT_RGB_8_GBGB,
  101. }, {
  102. .name = "12 bpp YUV411",
  103. .fourcc = V4L2_PIX_FMT_YUV411P,
  104. .depth = 12,
  105. .reg = EM28XX_OUTFMT_YUV411,
  106. },
  107. };
  108. /* supported controls */
  109. /* Common to all boards */
  110. static struct v4l2_queryctrl ac97_qctrl[] = {
  111. {
  112. .id = V4L2_CID_AUDIO_VOLUME,
  113. .type = V4L2_CTRL_TYPE_INTEGER,
  114. .name = "Volume",
  115. .minimum = 0x0,
  116. .maximum = 0x1f,
  117. .step = 0x1,
  118. .default_value = 0x1f,
  119. .flags = V4L2_CTRL_FLAG_SLIDER,
  120. }, {
  121. .id = V4L2_CID_AUDIO_MUTE,
  122. .type = V4L2_CTRL_TYPE_BOOLEAN,
  123. .name = "Mute",
  124. .minimum = 0,
  125. .maximum = 1,
  126. .step = 1,
  127. .default_value = 1,
  128. .flags = 0,
  129. }
  130. };
  131. /* ------------------------------------------------------------------
  132. DMA and thread functions
  133. ------------------------------------------------------------------*/
  134. /*
  135. * Announces that a buffer were filled and request the next
  136. */
  137. static inline void buffer_filled(struct em28xx *dev,
  138. struct em28xx_dmaqueue *dma_q,
  139. struct em28xx_buffer *buf)
  140. {
  141. /* Advice that buffer was filled */
  142. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  143. buf->vb.state = VIDEOBUF_DONE;
  144. buf->vb.field_count++;
  145. do_gettimeofday(&buf->vb.ts);
  146. dev->isoc_ctl.vid_buf = NULL;
  147. list_del(&buf->vb.queue);
  148. wake_up(&buf->vb.done);
  149. }
  150. static inline void vbi_buffer_filled(struct em28xx *dev,
  151. struct em28xx_dmaqueue *dma_q,
  152. struct em28xx_buffer *buf)
  153. {
  154. /* Advice that buffer was filled */
  155. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  156. buf->vb.state = VIDEOBUF_DONE;
  157. buf->vb.field_count++;
  158. do_gettimeofday(&buf->vb.ts);
  159. dev->isoc_ctl.vbi_buf = NULL;
  160. list_del(&buf->vb.queue);
  161. wake_up(&buf->vb.done);
  162. }
  163. /*
  164. * Identify the buffer header type and properly handles
  165. */
  166. static void em28xx_copy_video(struct em28xx *dev,
  167. struct em28xx_dmaqueue *dma_q,
  168. struct em28xx_buffer *buf,
  169. unsigned char *p,
  170. unsigned char *outp, unsigned long len)
  171. {
  172. void *fieldstart, *startwrite, *startread;
  173. int linesdone, currlinedone, offset, lencopy, remain;
  174. int bytesperline = dev->width << 1;
  175. if (dma_q->pos + len > buf->vb.size)
  176. len = buf->vb.size - dma_q->pos;
  177. if (p[0] != 0x88 && p[0] != 0x22) {
  178. em28xx_isocdbg("frame is not complete\n");
  179. len += 4;
  180. } else
  181. p += 4;
  182. startread = p;
  183. remain = len;
  184. if (dev->progressive)
  185. fieldstart = outp;
  186. else {
  187. /* Interlaces two half frames */
  188. if (buf->top_field)
  189. fieldstart = outp;
  190. else
  191. fieldstart = outp + bytesperline;
  192. }
  193. linesdone = dma_q->pos / bytesperline;
  194. currlinedone = dma_q->pos % bytesperline;
  195. if (dev->progressive)
  196. offset = linesdone * bytesperline + currlinedone;
  197. else
  198. offset = linesdone * bytesperline * 2 + currlinedone;
  199. startwrite = fieldstart + offset;
  200. lencopy = bytesperline - currlinedone;
  201. lencopy = lencopy > remain ? remain : lencopy;
  202. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  203. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  204. ((char *)startwrite + lencopy) -
  205. ((char *)outp + buf->vb.size));
  206. remain = (char *)outp + buf->vb.size - (char *)startwrite;
  207. lencopy = remain;
  208. }
  209. if (lencopy <= 0)
  210. return;
  211. memcpy(startwrite, startread, lencopy);
  212. remain -= lencopy;
  213. while (remain > 0) {
  214. startwrite += lencopy + bytesperline;
  215. startread += lencopy;
  216. if (bytesperline > remain)
  217. lencopy = remain;
  218. else
  219. lencopy = bytesperline;
  220. if ((char *)startwrite + lencopy > (char *)outp +
  221. buf->vb.size) {
  222. em28xx_isocdbg("Overflow of %zi bytes past buffer end"
  223. "(2)\n",
  224. ((char *)startwrite + lencopy) -
  225. ((char *)outp + buf->vb.size));
  226. lencopy = remain = (char *)outp + buf->vb.size -
  227. (char *)startwrite;
  228. }
  229. if (lencopy <= 0)
  230. break;
  231. memcpy(startwrite, startread, lencopy);
  232. remain -= lencopy;
  233. }
  234. dma_q->pos += len;
  235. }
  236. static void em28xx_copy_vbi(struct em28xx *dev,
  237. struct em28xx_dmaqueue *dma_q,
  238. struct em28xx_buffer *buf,
  239. unsigned char *p,
  240. unsigned char *outp, unsigned long len)
  241. {
  242. void *startwrite, *startread;
  243. int offset;
  244. int bytesperline = dev->vbi_width;
  245. if (dev == NULL) {
  246. em28xx_isocdbg("dev is null\n");
  247. return;
  248. }
  249. if (dma_q == NULL) {
  250. em28xx_isocdbg("dma_q is null\n");
  251. return;
  252. }
  253. if (buf == NULL) {
  254. return;
  255. }
  256. if (p == NULL) {
  257. em28xx_isocdbg("p is null\n");
  258. return;
  259. }
  260. if (outp == NULL) {
  261. em28xx_isocdbg("outp is null\n");
  262. return;
  263. }
  264. if (dma_q->pos + len > buf->vb.size)
  265. len = buf->vb.size - dma_q->pos;
  266. if ((p[0] == 0x33 && p[1] == 0x95) ||
  267. (p[0] == 0x88 && p[1] == 0x88)) {
  268. /* Header field, advance past it */
  269. p += 4;
  270. } else {
  271. len += 4;
  272. }
  273. startread = p;
  274. startwrite = outp + dma_q->pos;
  275. offset = dma_q->pos;
  276. /* Make sure the bottom field populates the second half of the frame */
  277. if (buf->top_field == 0) {
  278. startwrite += bytesperline * dev->vbi_height;
  279. offset += bytesperline * dev->vbi_height;
  280. }
  281. memcpy(startwrite, startread, len);
  282. dma_q->pos += len;
  283. }
  284. static inline void print_err_status(struct em28xx *dev,
  285. int packet, int status)
  286. {
  287. char *errmsg = "Unknown";
  288. switch (status) {
  289. case -ENOENT:
  290. errmsg = "unlinked synchronuously";
  291. break;
  292. case -ECONNRESET:
  293. errmsg = "unlinked asynchronuously";
  294. break;
  295. case -ENOSR:
  296. errmsg = "Buffer error (overrun)";
  297. break;
  298. case -EPIPE:
  299. errmsg = "Stalled (device not responding)";
  300. break;
  301. case -EOVERFLOW:
  302. errmsg = "Babble (bad cable?)";
  303. break;
  304. case -EPROTO:
  305. errmsg = "Bit-stuff error (bad cable?)";
  306. break;
  307. case -EILSEQ:
  308. errmsg = "CRC/Timeout (could be anything)";
  309. break;
  310. case -ETIME:
  311. errmsg = "Device does not respond";
  312. break;
  313. }
  314. if (packet < 0) {
  315. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  316. } else {
  317. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  318. packet, status, errmsg);
  319. }
  320. }
  321. /*
  322. * video-buf generic routine to get the next available buffer
  323. */
  324. static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
  325. struct em28xx_buffer **buf)
  326. {
  327. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  328. char *outp;
  329. if (list_empty(&dma_q->active)) {
  330. em28xx_isocdbg("No active queue to serve\n");
  331. dev->isoc_ctl.vid_buf = NULL;
  332. *buf = NULL;
  333. return;
  334. }
  335. /* Get the next buffer */
  336. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  337. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  338. outp = videobuf_to_vmalloc(&(*buf)->vb);
  339. memset(outp, 0, (*buf)->vb.size);
  340. dev->isoc_ctl.vid_buf = *buf;
  341. return;
  342. }
  343. /*
  344. * video-buf generic routine to get the next available VBI buffer
  345. */
  346. static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
  347. struct em28xx_buffer **buf)
  348. {
  349. struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
  350. char *outp;
  351. if (list_empty(&dma_q->active)) {
  352. em28xx_isocdbg("No active queue to serve\n");
  353. dev->isoc_ctl.vbi_buf = NULL;
  354. *buf = NULL;
  355. return;
  356. }
  357. /* Get the next buffer */
  358. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  359. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  360. outp = videobuf_to_vmalloc(&(*buf)->vb);
  361. memset(outp, 0x00, (*buf)->vb.size);
  362. dev->isoc_ctl.vbi_buf = *buf;
  363. return;
  364. }
  365. /*
  366. * Controls the isoc copy of each urb packet
  367. */
  368. static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
  369. {
  370. struct em28xx_buffer *buf;
  371. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  372. unsigned char *outp = NULL;
  373. int i, len = 0, rc = 1;
  374. unsigned char *p;
  375. if (!dev)
  376. return 0;
  377. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  378. return 0;
  379. if (urb->status < 0) {
  380. print_err_status(dev, -1, urb->status);
  381. if (urb->status == -ENOENT)
  382. return 0;
  383. }
  384. buf = dev->isoc_ctl.vid_buf;
  385. if (buf != NULL)
  386. outp = videobuf_to_vmalloc(&buf->vb);
  387. for (i = 0; i < urb->number_of_packets; i++) {
  388. int status = urb->iso_frame_desc[i].status;
  389. if (status < 0) {
  390. print_err_status(dev, i, status);
  391. if (urb->iso_frame_desc[i].status != -EPROTO)
  392. continue;
  393. }
  394. len = urb->iso_frame_desc[i].actual_length - 4;
  395. if (urb->iso_frame_desc[i].actual_length <= 0) {
  396. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  397. continue;
  398. }
  399. if (urb->iso_frame_desc[i].actual_length >
  400. dev->max_pkt_size) {
  401. em28xx_isocdbg("packet bigger than packet size");
  402. continue;
  403. }
  404. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  405. /* FIXME: incomplete buffer checks where removed to make
  406. logic simpler. Impacts of those changes should be evaluated
  407. */
  408. if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
  409. em28xx_isocdbg("VBI HEADER!!!\n");
  410. /* FIXME: Should add vbi copy */
  411. continue;
  412. }
  413. if (p[0] == 0x22 && p[1] == 0x5a) {
  414. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  415. len, (p[2] & 1) ? "odd" : "even");
  416. if (dev->progressive || !(p[2] & 1)) {
  417. if (buf != NULL)
  418. buffer_filled(dev, dma_q, buf);
  419. get_next_buf(dma_q, &buf);
  420. if (buf == NULL)
  421. outp = NULL;
  422. else
  423. outp = videobuf_to_vmalloc(&buf->vb);
  424. }
  425. if (buf != NULL) {
  426. if (p[2] & 1)
  427. buf->top_field = 0;
  428. else
  429. buf->top_field = 1;
  430. }
  431. dma_q->pos = 0;
  432. }
  433. if (buf != NULL)
  434. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  435. }
  436. return rc;
  437. }
  438. /* Version of isoc handler that takes into account a mixture of video and
  439. VBI data */
  440. static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
  441. {
  442. struct em28xx_buffer *buf, *vbi_buf;
  443. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  444. struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
  445. unsigned char *outp = NULL;
  446. unsigned char *vbioutp = NULL;
  447. int i, len = 0, rc = 1;
  448. unsigned char *p;
  449. int vbi_size;
  450. if (!dev)
  451. return 0;
  452. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  453. return 0;
  454. if (urb->status < 0) {
  455. print_err_status(dev, -1, urb->status);
  456. if (urb->status == -ENOENT)
  457. return 0;
  458. }
  459. buf = dev->isoc_ctl.vid_buf;
  460. if (buf != NULL)
  461. outp = videobuf_to_vmalloc(&buf->vb);
  462. vbi_buf = dev->isoc_ctl.vbi_buf;
  463. if (vbi_buf != NULL)
  464. vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
  465. for (i = 0; i < urb->number_of_packets; i++) {
  466. int status = urb->iso_frame_desc[i].status;
  467. if (status < 0) {
  468. print_err_status(dev, i, status);
  469. if (urb->iso_frame_desc[i].status != -EPROTO)
  470. continue;
  471. }
  472. len = urb->iso_frame_desc[i].actual_length - 4;
  473. if (urb->iso_frame_desc[i].actual_length <= 0) {
  474. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  475. continue;
  476. }
  477. if (urb->iso_frame_desc[i].actual_length >
  478. dev->max_pkt_size) {
  479. em28xx_isocdbg("packet bigger than packet size");
  480. continue;
  481. }
  482. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  483. /* capture type 0 = vbi start
  484. capture type 1 = video start
  485. capture type 2 = video in progress */
  486. if (p[0] == 0x33 && p[1] == 0x95) {
  487. dev->capture_type = 0;
  488. dev->vbi_read = 0;
  489. em28xx_isocdbg("VBI START HEADER!!!\n");
  490. dev->cur_field = p[2];
  491. }
  492. vbi_size = dev->vbi_width * dev->vbi_height;
  493. if (dev->capture_type == 0) {
  494. if (dev->vbi_read >= vbi_size) {
  495. /* We've already read all the VBI data, so
  496. treat the rest as video */
  497. em28xx_isocdbg("dev->vbi_read > vbi_size\n");
  498. } else if ((dev->vbi_read + len) < vbi_size) {
  499. /* This entire frame is VBI data */
  500. if (dev->vbi_read == 0 &&
  501. (!(dev->cur_field & 1))) {
  502. /* Brand new frame */
  503. if (vbi_buf != NULL)
  504. vbi_buffer_filled(dev,
  505. vbi_dma_q,
  506. vbi_buf);
  507. vbi_get_next_buf(vbi_dma_q, &vbi_buf);
  508. if (vbi_buf == NULL)
  509. vbioutp = NULL;
  510. else
  511. vbioutp = videobuf_to_vmalloc(
  512. &vbi_buf->vb);
  513. }
  514. if (dev->vbi_read == 0) {
  515. vbi_dma_q->pos = 0;
  516. if (vbi_buf != NULL) {
  517. if (dev->cur_field & 1)
  518. vbi_buf->top_field = 0;
  519. else
  520. vbi_buf->top_field = 1;
  521. }
  522. }
  523. dev->vbi_read += len;
  524. em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
  525. vbioutp, len);
  526. } else {
  527. /* Some of this frame is VBI data and some is
  528. video data */
  529. int vbi_data_len = vbi_size - dev->vbi_read;
  530. dev->vbi_read += vbi_data_len;
  531. em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
  532. vbioutp, vbi_data_len);
  533. dev->capture_type = 1;
  534. p += vbi_data_len;
  535. len -= vbi_data_len;
  536. }
  537. }
  538. if (dev->capture_type == 1) {
  539. dev->capture_type = 2;
  540. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  541. len, (p[2] & 1) ? "odd" : "even");
  542. if (dev->progressive || !(dev->cur_field & 1)) {
  543. if (buf != NULL)
  544. buffer_filled(dev, dma_q, buf);
  545. get_next_buf(dma_q, &buf);
  546. if (buf == NULL)
  547. outp = NULL;
  548. else
  549. outp = videobuf_to_vmalloc(&buf->vb);
  550. }
  551. if (buf != NULL) {
  552. if (dev->cur_field & 1)
  553. buf->top_field = 0;
  554. else
  555. buf->top_field = 1;
  556. }
  557. dma_q->pos = 0;
  558. }
  559. if (buf != NULL && dev->capture_type == 2)
  560. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  561. }
  562. return rc;
  563. }
  564. /* ------------------------------------------------------------------
  565. Videobuf operations
  566. ------------------------------------------------------------------*/
  567. static int
  568. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  569. {
  570. struct em28xx_fh *fh = vq->priv_data;
  571. struct em28xx *dev = fh->dev;
  572. struct v4l2_frequency f;
  573. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
  574. >> 3;
  575. if (0 == *count)
  576. *count = EM28XX_DEF_BUF;
  577. if (*count < EM28XX_MIN_BUF)
  578. *count = EM28XX_MIN_BUF;
  579. /* Ask tuner to go to analog or radio mode */
  580. memset(&f, 0, sizeof(f));
  581. f.frequency = dev->ctl_freq;
  582. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  583. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  584. return 0;
  585. }
  586. /* This is called *without* dev->slock held; please keep it that way */
  587. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  588. {
  589. struct em28xx_fh *fh = vq->priv_data;
  590. struct em28xx *dev = fh->dev;
  591. unsigned long flags = 0;
  592. if (in_interrupt())
  593. BUG();
  594. /* We used to wait for the buffer to finish here, but this didn't work
  595. because, as we were keeping the state as VIDEOBUF_QUEUED,
  596. videobuf_queue_cancel marked it as finished for us.
  597. (Also, it could wedge forever if the hardware was misconfigured.)
  598. This should be safe; by the time we get here, the buffer isn't
  599. queued anymore. If we ever start marking the buffers as
  600. VIDEOBUF_ACTIVE, it won't be, though.
  601. */
  602. spin_lock_irqsave(&dev->slock, flags);
  603. if (dev->isoc_ctl.vid_buf == buf)
  604. dev->isoc_ctl.vid_buf = NULL;
  605. spin_unlock_irqrestore(&dev->slock, flags);
  606. videobuf_vmalloc_free(&buf->vb);
  607. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  608. }
  609. static int
  610. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  611. enum v4l2_field field)
  612. {
  613. struct em28xx_fh *fh = vq->priv_data;
  614. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  615. struct em28xx *dev = fh->dev;
  616. int rc = 0, urb_init = 0;
  617. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
  618. + 7) >> 3;
  619. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  620. return -EINVAL;
  621. buf->vb.width = dev->width;
  622. buf->vb.height = dev->height;
  623. buf->vb.field = field;
  624. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  625. rc = videobuf_iolock(vq, &buf->vb, NULL);
  626. if (rc < 0)
  627. goto fail;
  628. }
  629. if (!dev->isoc_ctl.num_bufs)
  630. urb_init = 1;
  631. if (urb_init) {
  632. if (em28xx_vbi_supported(dev) == 1)
  633. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  634. EM28XX_NUM_BUFS,
  635. dev->max_pkt_size,
  636. em28xx_isoc_copy_vbi);
  637. else
  638. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  639. EM28XX_NUM_BUFS,
  640. dev->max_pkt_size,
  641. em28xx_isoc_copy);
  642. if (rc < 0)
  643. goto fail;
  644. }
  645. buf->vb.state = VIDEOBUF_PREPARED;
  646. return 0;
  647. fail:
  648. free_buffer(vq, buf);
  649. return rc;
  650. }
  651. static void
  652. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  653. {
  654. struct em28xx_buffer *buf = container_of(vb,
  655. struct em28xx_buffer,
  656. vb);
  657. struct em28xx_fh *fh = vq->priv_data;
  658. struct em28xx *dev = fh->dev;
  659. struct em28xx_dmaqueue *vidq = &dev->vidq;
  660. buf->vb.state = VIDEOBUF_QUEUED;
  661. list_add_tail(&buf->vb.queue, &vidq->active);
  662. }
  663. static void buffer_release(struct videobuf_queue *vq,
  664. struct videobuf_buffer *vb)
  665. {
  666. struct em28xx_buffer *buf = container_of(vb,
  667. struct em28xx_buffer,
  668. vb);
  669. struct em28xx_fh *fh = vq->priv_data;
  670. struct em28xx *dev = (struct em28xx *)fh->dev;
  671. em28xx_isocdbg("em28xx: called buffer_release\n");
  672. free_buffer(vq, buf);
  673. }
  674. static struct videobuf_queue_ops em28xx_video_qops = {
  675. .buf_setup = buffer_setup,
  676. .buf_prepare = buffer_prepare,
  677. .buf_queue = buffer_queue,
  678. .buf_release = buffer_release,
  679. };
  680. /********************* v4l2 interface **************************************/
  681. static void video_mux(struct em28xx *dev, int index)
  682. {
  683. dev->ctl_input = index;
  684. dev->ctl_ainput = INPUT(index)->amux;
  685. dev->ctl_aoutput = INPUT(index)->aout;
  686. if (!dev->ctl_aoutput)
  687. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  688. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  689. INPUT(index)->vmux, 0, 0);
  690. if (dev->board.has_msp34xx) {
  691. if (dev->i2s_speed) {
  692. v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
  693. s_i2s_clock_freq, dev->i2s_speed);
  694. }
  695. /* Note: this is msp3400 specific */
  696. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  697. dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  698. }
  699. if (dev->board.adecoder != EM28XX_NOADECODER) {
  700. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  701. dev->ctl_ainput, dev->ctl_aoutput, 0);
  702. }
  703. em28xx_audio_analog_set(dev);
  704. }
  705. /* Usage lock check functions */
  706. static int res_get(struct em28xx_fh *fh, unsigned int bit)
  707. {
  708. struct em28xx *dev = fh->dev;
  709. if (fh->resources & bit)
  710. /* have it already allocated */
  711. return 1;
  712. /* is it free? */
  713. mutex_lock(&dev->lock);
  714. if (dev->resources & bit) {
  715. /* no, someone else uses it */
  716. mutex_unlock(&dev->lock);
  717. return 0;
  718. }
  719. /* it's free, grab it */
  720. fh->resources |= bit;
  721. dev->resources |= bit;
  722. em28xx_videodbg("res: get %d\n", bit);
  723. mutex_unlock(&dev->lock);
  724. return 1;
  725. }
  726. static int res_check(struct em28xx_fh *fh, unsigned int bit)
  727. {
  728. return fh->resources & bit;
  729. }
  730. static int res_locked(struct em28xx *dev, unsigned int bit)
  731. {
  732. return dev->resources & bit;
  733. }
  734. static void res_free(struct em28xx_fh *fh, unsigned int bits)
  735. {
  736. struct em28xx *dev = fh->dev;
  737. BUG_ON((fh->resources & bits) != bits);
  738. mutex_lock(&dev->lock);
  739. fh->resources &= ~bits;
  740. dev->resources &= ~bits;
  741. em28xx_videodbg("res: put %d\n", bits);
  742. mutex_unlock(&dev->lock);
  743. }
  744. static int get_ressource(struct em28xx_fh *fh)
  745. {
  746. switch (fh->type) {
  747. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  748. return EM28XX_RESOURCE_VIDEO;
  749. case V4L2_BUF_TYPE_VBI_CAPTURE:
  750. return EM28XX_RESOURCE_VBI;
  751. default:
  752. BUG();
  753. return 0;
  754. }
  755. }
  756. /*
  757. * ac97_queryctrl()
  758. * return the ac97 supported controls
  759. */
  760. static int ac97_queryctrl(struct v4l2_queryctrl *qc)
  761. {
  762. int i;
  763. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  764. if (qc->id && qc->id == ac97_qctrl[i].id) {
  765. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  766. return 0;
  767. }
  768. }
  769. /* Control is not ac97 related */
  770. return 1;
  771. }
  772. /*
  773. * ac97_get_ctrl()
  774. * return the current values for ac97 mute and volume
  775. */
  776. static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  777. {
  778. switch (ctrl->id) {
  779. case V4L2_CID_AUDIO_MUTE:
  780. ctrl->value = dev->mute;
  781. return 0;
  782. case V4L2_CID_AUDIO_VOLUME:
  783. ctrl->value = dev->volume;
  784. return 0;
  785. default:
  786. /* Control is not ac97 related */
  787. return 1;
  788. }
  789. }
  790. /*
  791. * ac97_set_ctrl()
  792. * set values for ac97 mute and volume
  793. */
  794. static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  795. {
  796. int i;
  797. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
  798. if (ctrl->id == ac97_qctrl[i].id)
  799. goto handle;
  800. /* Announce that hasn't handle it */
  801. return 1;
  802. handle:
  803. if (ctrl->value < ac97_qctrl[i].minimum ||
  804. ctrl->value > ac97_qctrl[i].maximum)
  805. return -ERANGE;
  806. switch (ctrl->id) {
  807. case V4L2_CID_AUDIO_MUTE:
  808. dev->mute = ctrl->value;
  809. break;
  810. case V4L2_CID_AUDIO_VOLUME:
  811. dev->volume = ctrl->value;
  812. break;
  813. }
  814. return em28xx_audio_analog_set(dev);
  815. }
  816. static int check_dev(struct em28xx *dev)
  817. {
  818. if (dev->state & DEV_DISCONNECTED) {
  819. em28xx_errdev("v4l2 ioctl: device not present\n");
  820. return -ENODEV;
  821. }
  822. if (dev->state & DEV_MISCONFIGURED) {
  823. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  824. "close and open it again\n");
  825. return -EIO;
  826. }
  827. return 0;
  828. }
  829. static void get_scale(struct em28xx *dev,
  830. unsigned int width, unsigned int height,
  831. unsigned int *hscale, unsigned int *vscale)
  832. {
  833. unsigned int maxw = norm_maxw(dev);
  834. unsigned int maxh = norm_maxh(dev);
  835. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  836. if (*hscale >= 0x4000)
  837. *hscale = 0x3fff;
  838. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  839. if (*vscale >= 0x4000)
  840. *vscale = 0x3fff;
  841. }
  842. /* ------------------------------------------------------------------
  843. IOCTL vidioc handling
  844. ------------------------------------------------------------------*/
  845. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  846. struct v4l2_format *f)
  847. {
  848. struct em28xx_fh *fh = priv;
  849. struct em28xx *dev = fh->dev;
  850. mutex_lock(&dev->lock);
  851. f->fmt.pix.width = dev->width;
  852. f->fmt.pix.height = dev->height;
  853. f->fmt.pix.pixelformat = dev->format->fourcc;
  854. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  855. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  856. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  857. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  858. if (dev->progressive)
  859. f->fmt.pix.field = V4L2_FIELD_NONE;
  860. else
  861. f->fmt.pix.field = dev->interlaced ?
  862. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  863. mutex_unlock(&dev->lock);
  864. return 0;
  865. }
  866. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  867. {
  868. unsigned int i;
  869. for (i = 0; i < ARRAY_SIZE(format); i++)
  870. if (format[i].fourcc == fourcc)
  871. return &format[i];
  872. return NULL;
  873. }
  874. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  875. struct v4l2_format *f)
  876. {
  877. struct em28xx_fh *fh = priv;
  878. struct em28xx *dev = fh->dev;
  879. unsigned int width = f->fmt.pix.width;
  880. unsigned int height = f->fmt.pix.height;
  881. unsigned int maxw = norm_maxw(dev);
  882. unsigned int maxh = norm_maxh(dev);
  883. unsigned int hscale, vscale;
  884. struct em28xx_fmt *fmt;
  885. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  886. if (!fmt) {
  887. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  888. f->fmt.pix.pixelformat);
  889. return -EINVAL;
  890. }
  891. if (dev->board.is_em2800) {
  892. /* the em2800 can only scale down to 50% */
  893. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  894. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  895. } else {
  896. /* width must even because of the YUYV format
  897. height must be even because of interlacing */
  898. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  899. 1, 0);
  900. }
  901. get_scale(dev, width, height, &hscale, &vscale);
  902. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  903. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  904. f->fmt.pix.width = width;
  905. f->fmt.pix.height = height;
  906. f->fmt.pix.pixelformat = fmt->fourcc;
  907. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  908. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  909. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  910. if (dev->progressive)
  911. f->fmt.pix.field = V4L2_FIELD_NONE;
  912. else
  913. f->fmt.pix.field = dev->interlaced ?
  914. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  915. return 0;
  916. }
  917. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  918. unsigned width, unsigned height)
  919. {
  920. struct em28xx_fmt *fmt;
  921. fmt = format_by_fourcc(fourcc);
  922. if (!fmt)
  923. return -EINVAL;
  924. dev->format = fmt;
  925. dev->width = width;
  926. dev->height = height;
  927. /* set new image size */
  928. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  929. em28xx_set_alternate(dev);
  930. em28xx_resolution_set(dev);
  931. return 0;
  932. }
  933. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  934. struct v4l2_format *f)
  935. {
  936. struct em28xx_fh *fh = priv;
  937. struct em28xx *dev = fh->dev;
  938. int rc;
  939. rc = check_dev(dev);
  940. if (rc < 0)
  941. return rc;
  942. mutex_lock(&dev->lock);
  943. vidioc_try_fmt_vid_cap(file, priv, f);
  944. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  945. em28xx_errdev("%s queue busy\n", __func__);
  946. rc = -EBUSY;
  947. goto out;
  948. }
  949. rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  950. f->fmt.pix.width, f->fmt.pix.height);
  951. out:
  952. mutex_unlock(&dev->lock);
  953. return rc;
  954. }
  955. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  956. {
  957. struct em28xx_fh *fh = priv;
  958. struct em28xx *dev = fh->dev;
  959. int rc;
  960. rc = check_dev(dev);
  961. if (rc < 0)
  962. return rc;
  963. *norm = dev->norm;
  964. return 0;
  965. }
  966. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  967. {
  968. struct em28xx_fh *fh = priv;
  969. struct em28xx *dev = fh->dev;
  970. struct v4l2_format f;
  971. int rc;
  972. rc = check_dev(dev);
  973. if (rc < 0)
  974. return rc;
  975. mutex_lock(&dev->lock);
  976. dev->norm = *norm;
  977. /* Adjusts width/height, if needed */
  978. f.fmt.pix.width = dev->width;
  979. f.fmt.pix.height = dev->height;
  980. vidioc_try_fmt_vid_cap(file, priv, &f);
  981. /* set new image size */
  982. dev->width = f.fmt.pix.width;
  983. dev->height = f.fmt.pix.height;
  984. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  985. em28xx_resolution_set(dev);
  986. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  987. mutex_unlock(&dev->lock);
  988. return 0;
  989. }
  990. static int vidioc_g_parm(struct file *file, void *priv,
  991. struct v4l2_streamparm *p)
  992. {
  993. struct em28xx_fh *fh = priv;
  994. struct em28xx *dev = fh->dev;
  995. int rc = 0;
  996. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  997. return -EINVAL;
  998. if (dev->board.is_webcam)
  999. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  1000. video, g_parm, p);
  1001. else
  1002. v4l2_video_std_frame_period(dev->norm,
  1003. &p->parm.capture.timeperframe);
  1004. return rc;
  1005. }
  1006. static int vidioc_s_parm(struct file *file, void *priv,
  1007. struct v4l2_streamparm *p)
  1008. {
  1009. struct em28xx_fh *fh = priv;
  1010. struct em28xx *dev = fh->dev;
  1011. if (!dev->board.is_webcam)
  1012. return -EINVAL;
  1013. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1014. return -EINVAL;
  1015. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  1016. }
  1017. static const char *iname[] = {
  1018. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  1019. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  1020. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  1021. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  1022. [EM28XX_VMUX_SVIDEO] = "S-Video",
  1023. [EM28XX_VMUX_TELEVISION] = "Television",
  1024. [EM28XX_VMUX_CABLE] = "Cable TV",
  1025. [EM28XX_VMUX_DVB] = "DVB",
  1026. [EM28XX_VMUX_DEBUG] = "for debug only",
  1027. };
  1028. static int vidioc_enum_input(struct file *file, void *priv,
  1029. struct v4l2_input *i)
  1030. {
  1031. struct em28xx_fh *fh = priv;
  1032. struct em28xx *dev = fh->dev;
  1033. unsigned int n;
  1034. n = i->index;
  1035. if (n >= MAX_EM28XX_INPUT)
  1036. return -EINVAL;
  1037. if (0 == INPUT(n)->type)
  1038. return -EINVAL;
  1039. i->index = n;
  1040. i->type = V4L2_INPUT_TYPE_CAMERA;
  1041. strcpy(i->name, iname[INPUT(n)->type]);
  1042. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  1043. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  1044. i->type = V4L2_INPUT_TYPE_TUNER;
  1045. i->std = dev->vdev->tvnorms;
  1046. return 0;
  1047. }
  1048. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1049. {
  1050. struct em28xx_fh *fh = priv;
  1051. struct em28xx *dev = fh->dev;
  1052. *i = dev->ctl_input;
  1053. return 0;
  1054. }
  1055. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1056. {
  1057. struct em28xx_fh *fh = priv;
  1058. struct em28xx *dev = fh->dev;
  1059. int rc;
  1060. rc = check_dev(dev);
  1061. if (rc < 0)
  1062. return rc;
  1063. if (i >= MAX_EM28XX_INPUT)
  1064. return -EINVAL;
  1065. if (0 == INPUT(i)->type)
  1066. return -EINVAL;
  1067. dev->ctl_input = i;
  1068. mutex_lock(&dev->lock);
  1069. video_mux(dev, dev->ctl_input);
  1070. mutex_unlock(&dev->lock);
  1071. return 0;
  1072. }
  1073. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1074. {
  1075. struct em28xx_fh *fh = priv;
  1076. struct em28xx *dev = fh->dev;
  1077. if (!dev->audio_mode.has_audio)
  1078. return -EINVAL;
  1079. switch (a->index) {
  1080. case EM28XX_AMUX_VIDEO:
  1081. strcpy(a->name, "Television");
  1082. break;
  1083. case EM28XX_AMUX_LINE_IN:
  1084. strcpy(a->name, "Line In");
  1085. break;
  1086. case EM28XX_AMUX_VIDEO2:
  1087. strcpy(a->name, "Television alt");
  1088. break;
  1089. case EM28XX_AMUX_PHONE:
  1090. strcpy(a->name, "Phone");
  1091. break;
  1092. case EM28XX_AMUX_MIC:
  1093. strcpy(a->name, "Mic");
  1094. break;
  1095. case EM28XX_AMUX_CD:
  1096. strcpy(a->name, "CD");
  1097. break;
  1098. case EM28XX_AMUX_AUX:
  1099. strcpy(a->name, "Aux");
  1100. break;
  1101. case EM28XX_AMUX_PCM_OUT:
  1102. strcpy(a->name, "PCM");
  1103. break;
  1104. default:
  1105. return -EINVAL;
  1106. }
  1107. a->index = dev->ctl_ainput;
  1108. a->capability = V4L2_AUDCAP_STEREO;
  1109. return 0;
  1110. }
  1111. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1112. {
  1113. struct em28xx_fh *fh = priv;
  1114. struct em28xx *dev = fh->dev;
  1115. if (!dev->audio_mode.has_audio)
  1116. return -EINVAL;
  1117. if (a->index >= MAX_EM28XX_INPUT)
  1118. return -EINVAL;
  1119. if (0 == INPUT(a->index)->type)
  1120. return -EINVAL;
  1121. mutex_lock(&dev->lock);
  1122. dev->ctl_ainput = INPUT(a->index)->amux;
  1123. dev->ctl_aoutput = INPUT(a->index)->aout;
  1124. if (!dev->ctl_aoutput)
  1125. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1126. mutex_unlock(&dev->lock);
  1127. return 0;
  1128. }
  1129. static int vidioc_queryctrl(struct file *file, void *priv,
  1130. struct v4l2_queryctrl *qc)
  1131. {
  1132. struct em28xx_fh *fh = priv;
  1133. struct em28xx *dev = fh->dev;
  1134. int id = qc->id;
  1135. int rc;
  1136. rc = check_dev(dev);
  1137. if (rc < 0)
  1138. return rc;
  1139. memset(qc, 0, sizeof(*qc));
  1140. qc->id = id;
  1141. /* enumberate AC97 controls */
  1142. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  1143. rc = ac97_queryctrl(qc);
  1144. if (!rc)
  1145. return 0;
  1146. }
  1147. /* enumberate V4L2 device controls */
  1148. mutex_lock(&dev->lock);
  1149. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
  1150. mutex_unlock(&dev->lock);
  1151. if (qc->type)
  1152. return 0;
  1153. else
  1154. return -EINVAL;
  1155. }
  1156. static int vidioc_g_ctrl(struct file *file, void *priv,
  1157. struct v4l2_control *ctrl)
  1158. {
  1159. struct em28xx_fh *fh = priv;
  1160. struct em28xx *dev = fh->dev;
  1161. int rc;
  1162. rc = check_dev(dev);
  1163. if (rc < 0)
  1164. return rc;
  1165. rc = 0;
  1166. mutex_lock(&dev->lock);
  1167. /* Set an AC97 control */
  1168. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1169. rc = ac97_get_ctrl(dev, ctrl);
  1170. else
  1171. rc = 1;
  1172. /* It were not an AC97 control. Sends it to the v4l2 dev interface */
  1173. if (rc == 1) {
  1174. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  1175. rc = 0;
  1176. }
  1177. mutex_unlock(&dev->lock);
  1178. return rc;
  1179. }
  1180. static int vidioc_s_ctrl(struct file *file, void *priv,
  1181. struct v4l2_control *ctrl)
  1182. {
  1183. struct em28xx_fh *fh = priv;
  1184. struct em28xx *dev = fh->dev;
  1185. int rc;
  1186. rc = check_dev(dev);
  1187. if (rc < 0)
  1188. return rc;
  1189. mutex_lock(&dev->lock);
  1190. /* Set an AC97 control */
  1191. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1192. rc = ac97_set_ctrl(dev, ctrl);
  1193. else
  1194. rc = 1;
  1195. /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
  1196. if (rc == 1) {
  1197. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
  1198. /*
  1199. * In the case of non-AC97 volume controls, we still need
  1200. * to do some setups at em28xx, in order to mute/unmute
  1201. * and to adjust audio volume. However, the value ranges
  1202. * should be checked by the corresponding V4L subdriver.
  1203. */
  1204. switch (ctrl->id) {
  1205. case V4L2_CID_AUDIO_MUTE:
  1206. dev->mute = ctrl->value;
  1207. rc = em28xx_audio_analog_set(dev);
  1208. break;
  1209. case V4L2_CID_AUDIO_VOLUME:
  1210. dev->volume = ctrl->value;
  1211. rc = em28xx_audio_analog_set(dev);
  1212. }
  1213. }
  1214. mutex_unlock(&dev->lock);
  1215. return rc;
  1216. }
  1217. static int vidioc_g_tuner(struct file *file, void *priv,
  1218. struct v4l2_tuner *t)
  1219. {
  1220. struct em28xx_fh *fh = priv;
  1221. struct em28xx *dev = fh->dev;
  1222. int rc;
  1223. rc = check_dev(dev);
  1224. if (rc < 0)
  1225. return rc;
  1226. if (0 != t->index)
  1227. return -EINVAL;
  1228. strcpy(t->name, "Tuner");
  1229. mutex_lock(&dev->lock);
  1230. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1231. mutex_unlock(&dev->lock);
  1232. return 0;
  1233. }
  1234. static int vidioc_s_tuner(struct file *file, void *priv,
  1235. struct v4l2_tuner *t)
  1236. {
  1237. struct em28xx_fh *fh = priv;
  1238. struct em28xx *dev = fh->dev;
  1239. int rc;
  1240. rc = check_dev(dev);
  1241. if (rc < 0)
  1242. return rc;
  1243. if (0 != t->index)
  1244. return -EINVAL;
  1245. mutex_lock(&dev->lock);
  1246. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1247. mutex_unlock(&dev->lock);
  1248. return 0;
  1249. }
  1250. static int vidioc_g_frequency(struct file *file, void *priv,
  1251. struct v4l2_frequency *f)
  1252. {
  1253. struct em28xx_fh *fh = priv;
  1254. struct em28xx *dev = fh->dev;
  1255. mutex_lock(&dev->lock);
  1256. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1257. f->frequency = dev->ctl_freq;
  1258. mutex_unlock(&dev->lock);
  1259. return 0;
  1260. }
  1261. static int vidioc_s_frequency(struct file *file, void *priv,
  1262. struct v4l2_frequency *f)
  1263. {
  1264. struct em28xx_fh *fh = priv;
  1265. struct em28xx *dev = fh->dev;
  1266. int rc;
  1267. rc = check_dev(dev);
  1268. if (rc < 0)
  1269. return rc;
  1270. if (0 != f->tuner)
  1271. return -EINVAL;
  1272. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1273. return -EINVAL;
  1274. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1275. return -EINVAL;
  1276. mutex_lock(&dev->lock);
  1277. dev->ctl_freq = f->frequency;
  1278. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1279. mutex_unlock(&dev->lock);
  1280. return 0;
  1281. }
  1282. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1283. static int em28xx_reg_len(int reg)
  1284. {
  1285. switch (reg) {
  1286. case EM28XX_R40_AC97LSB:
  1287. case EM28XX_R30_HSCALELOW:
  1288. case EM28XX_R32_VSCALELOW:
  1289. return 2;
  1290. default:
  1291. return 1;
  1292. }
  1293. }
  1294. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1295. struct v4l2_dbg_chip_ident *chip)
  1296. {
  1297. struct em28xx_fh *fh = priv;
  1298. struct em28xx *dev = fh->dev;
  1299. chip->ident = V4L2_IDENT_NONE;
  1300. chip->revision = 0;
  1301. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1302. return 0;
  1303. }
  1304. static int vidioc_g_register(struct file *file, void *priv,
  1305. struct v4l2_dbg_register *reg)
  1306. {
  1307. struct em28xx_fh *fh = priv;
  1308. struct em28xx *dev = fh->dev;
  1309. int ret;
  1310. switch (reg->match.type) {
  1311. case V4L2_CHIP_MATCH_AC97:
  1312. mutex_lock(&dev->lock);
  1313. ret = em28xx_read_ac97(dev, reg->reg);
  1314. mutex_unlock(&dev->lock);
  1315. if (ret < 0)
  1316. return ret;
  1317. reg->val = ret;
  1318. reg->size = 1;
  1319. return 0;
  1320. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1321. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1322. return 0;
  1323. case V4L2_CHIP_MATCH_I2C_ADDR:
  1324. /* TODO: is this correct? */
  1325. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1326. return 0;
  1327. default:
  1328. if (!v4l2_chip_match_host(&reg->match))
  1329. return -EINVAL;
  1330. }
  1331. /* Match host */
  1332. reg->size = em28xx_reg_len(reg->reg);
  1333. if (reg->size == 1) {
  1334. mutex_lock(&dev->lock);
  1335. ret = em28xx_read_reg(dev, reg->reg);
  1336. mutex_unlock(&dev->lock);
  1337. if (ret < 0)
  1338. return ret;
  1339. reg->val = ret;
  1340. } else {
  1341. __le16 val = 0;
  1342. mutex_lock(&dev->lock);
  1343. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1344. reg->reg, (char *)&val, 2);
  1345. mutex_unlock(&dev->lock);
  1346. if (ret < 0)
  1347. return ret;
  1348. reg->val = le16_to_cpu(val);
  1349. }
  1350. return 0;
  1351. }
  1352. static int vidioc_s_register(struct file *file, void *priv,
  1353. struct v4l2_dbg_register *reg)
  1354. {
  1355. struct em28xx_fh *fh = priv;
  1356. struct em28xx *dev = fh->dev;
  1357. __le16 buf;
  1358. int rc;
  1359. switch (reg->match.type) {
  1360. case V4L2_CHIP_MATCH_AC97:
  1361. mutex_lock(&dev->lock);
  1362. rc = em28xx_write_ac97(dev, reg->reg, reg->val);
  1363. mutex_unlock(&dev->lock);
  1364. return rc;
  1365. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1366. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1367. return 0;
  1368. case V4L2_CHIP_MATCH_I2C_ADDR:
  1369. /* TODO: is this correct? */
  1370. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1371. return 0;
  1372. default:
  1373. if (!v4l2_chip_match_host(&reg->match))
  1374. return -EINVAL;
  1375. }
  1376. /* Match host */
  1377. buf = cpu_to_le16(reg->val);
  1378. mutex_lock(&dev->lock);
  1379. rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1380. em28xx_reg_len(reg->reg));
  1381. mutex_unlock(&dev->lock);
  1382. return rc;
  1383. }
  1384. #endif
  1385. static int vidioc_cropcap(struct file *file, void *priv,
  1386. struct v4l2_cropcap *cc)
  1387. {
  1388. struct em28xx_fh *fh = priv;
  1389. struct em28xx *dev = fh->dev;
  1390. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1391. return -EINVAL;
  1392. cc->bounds.left = 0;
  1393. cc->bounds.top = 0;
  1394. cc->bounds.width = dev->width;
  1395. cc->bounds.height = dev->height;
  1396. cc->defrect = cc->bounds;
  1397. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1398. cc->pixelaspect.denominator = 59;
  1399. return 0;
  1400. }
  1401. static int vidioc_streamon(struct file *file, void *priv,
  1402. enum v4l2_buf_type type)
  1403. {
  1404. struct em28xx_fh *fh = priv;
  1405. struct em28xx *dev = fh->dev;
  1406. int rc = -EINVAL;
  1407. rc = check_dev(dev);
  1408. if (rc < 0)
  1409. return rc;
  1410. if (unlikely(type != fh->type))
  1411. return -EINVAL;
  1412. em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
  1413. fh, type, fh->resources, dev->resources);
  1414. if (unlikely(!res_get(fh, get_ressource(fh))))
  1415. return -EBUSY;
  1416. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1417. rc = videobuf_streamon(&fh->vb_vidq);
  1418. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1419. rc = videobuf_streamon(&fh->vb_vbiq);
  1420. return rc;
  1421. }
  1422. static int vidioc_streamoff(struct file *file, void *priv,
  1423. enum v4l2_buf_type type)
  1424. {
  1425. struct em28xx_fh *fh = priv;
  1426. struct em28xx *dev = fh->dev;
  1427. int rc;
  1428. rc = check_dev(dev);
  1429. if (rc < 0)
  1430. return rc;
  1431. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1432. fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
  1433. return -EINVAL;
  1434. if (type != fh->type)
  1435. return -EINVAL;
  1436. em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
  1437. fh, type, fh->resources, dev->resources);
  1438. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1439. videobuf_streamoff(&fh->vb_vidq);
  1440. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1441. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1442. videobuf_streamoff(&fh->vb_vbiq);
  1443. res_free(fh, EM28XX_RESOURCE_VBI);
  1444. }
  1445. return 0;
  1446. }
  1447. static int vidioc_querycap(struct file *file, void *priv,
  1448. struct v4l2_capability *cap)
  1449. {
  1450. struct em28xx_fh *fh = priv;
  1451. struct em28xx *dev = fh->dev;
  1452. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1453. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1454. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1455. cap->version = EM28XX_VERSION_CODE;
  1456. cap->capabilities =
  1457. V4L2_CAP_SLICED_VBI_CAPTURE |
  1458. V4L2_CAP_VIDEO_CAPTURE |
  1459. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1460. if (dev->vbi_dev)
  1461. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  1462. if (dev->audio_mode.has_audio)
  1463. cap->capabilities |= V4L2_CAP_AUDIO;
  1464. if (dev->tuner_type != TUNER_ABSENT)
  1465. cap->capabilities |= V4L2_CAP_TUNER;
  1466. return 0;
  1467. }
  1468. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1469. struct v4l2_fmtdesc *f)
  1470. {
  1471. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1472. return -EINVAL;
  1473. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1474. f->pixelformat = format[f->index].fourcc;
  1475. return 0;
  1476. }
  1477. /* Sliced VBI ioctls */
  1478. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1479. struct v4l2_format *f)
  1480. {
  1481. struct em28xx_fh *fh = priv;
  1482. struct em28xx *dev = fh->dev;
  1483. int rc;
  1484. rc = check_dev(dev);
  1485. if (rc < 0)
  1486. return rc;
  1487. mutex_lock(&dev->lock);
  1488. f->fmt.sliced.service_set = 0;
  1489. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1490. if (f->fmt.sliced.service_set == 0)
  1491. rc = -EINVAL;
  1492. mutex_unlock(&dev->lock);
  1493. return rc;
  1494. }
  1495. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1496. struct v4l2_format *f)
  1497. {
  1498. struct em28xx_fh *fh = priv;
  1499. struct em28xx *dev = fh->dev;
  1500. int rc;
  1501. rc = check_dev(dev);
  1502. if (rc < 0)
  1503. return rc;
  1504. mutex_lock(&dev->lock);
  1505. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1506. mutex_unlock(&dev->lock);
  1507. if (f->fmt.sliced.service_set == 0)
  1508. return -EINVAL;
  1509. return 0;
  1510. }
  1511. /* RAW VBI ioctls */
  1512. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1513. struct v4l2_format *format)
  1514. {
  1515. struct em28xx_fh *fh = priv;
  1516. struct em28xx *dev = fh->dev;
  1517. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1518. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1519. format->fmt.vbi.offset = 0;
  1520. format->fmt.vbi.flags = 0;
  1521. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1522. format->fmt.vbi.count[0] = dev->vbi_height;
  1523. format->fmt.vbi.count[1] = dev->vbi_height;
  1524. /* Varies by video standard (NTSC, PAL, etc.) */
  1525. if (dev->norm & V4L2_STD_525_60) {
  1526. /* NTSC */
  1527. format->fmt.vbi.start[0] = 10;
  1528. format->fmt.vbi.start[1] = 273;
  1529. } else if (dev->norm & V4L2_STD_625_50) {
  1530. /* PAL */
  1531. format->fmt.vbi.start[0] = 6;
  1532. format->fmt.vbi.start[1] = 318;
  1533. }
  1534. return 0;
  1535. }
  1536. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1537. struct v4l2_format *format)
  1538. {
  1539. struct em28xx_fh *fh = priv;
  1540. struct em28xx *dev = fh->dev;
  1541. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1542. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1543. format->fmt.vbi.offset = 0;
  1544. format->fmt.vbi.flags = 0;
  1545. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1546. format->fmt.vbi.count[0] = dev->vbi_height;
  1547. format->fmt.vbi.count[1] = dev->vbi_height;
  1548. /* Varies by video standard (NTSC, PAL, etc.) */
  1549. if (dev->norm & V4L2_STD_525_60) {
  1550. /* NTSC */
  1551. format->fmt.vbi.start[0] = 10;
  1552. format->fmt.vbi.start[1] = 273;
  1553. } else if (dev->norm & V4L2_STD_625_50) {
  1554. /* PAL */
  1555. format->fmt.vbi.start[0] = 6;
  1556. format->fmt.vbi.start[1] = 318;
  1557. }
  1558. return 0;
  1559. }
  1560. static int vidioc_reqbufs(struct file *file, void *priv,
  1561. struct v4l2_requestbuffers *rb)
  1562. {
  1563. struct em28xx_fh *fh = priv;
  1564. struct em28xx *dev = fh->dev;
  1565. int rc;
  1566. rc = check_dev(dev);
  1567. if (rc < 0)
  1568. return rc;
  1569. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1570. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1571. else
  1572. return videobuf_reqbufs(&fh->vb_vbiq, rb);
  1573. }
  1574. static int vidioc_querybuf(struct file *file, void *priv,
  1575. struct v4l2_buffer *b)
  1576. {
  1577. struct em28xx_fh *fh = priv;
  1578. struct em28xx *dev = fh->dev;
  1579. int rc;
  1580. rc = check_dev(dev);
  1581. if (rc < 0)
  1582. return rc;
  1583. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1584. return videobuf_querybuf(&fh->vb_vidq, b);
  1585. else {
  1586. /* FIXME: I'm not sure yet whether this is a bug in zvbi or
  1587. the videobuf framework, but we probably shouldn't be
  1588. returning a buffer larger than that which was asked for.
  1589. At a minimum, it causes a crash in zvbi since it does
  1590. a memcpy based on the source buffer length */
  1591. int result = videobuf_querybuf(&fh->vb_vbiq, b);
  1592. b->length = dev->vbi_width * dev->vbi_height * 2;
  1593. return result;
  1594. }
  1595. }
  1596. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1597. {
  1598. struct em28xx_fh *fh = priv;
  1599. struct em28xx *dev = fh->dev;
  1600. int rc;
  1601. rc = check_dev(dev);
  1602. if (rc < 0)
  1603. return rc;
  1604. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1605. return videobuf_qbuf(&fh->vb_vidq, b);
  1606. else
  1607. return videobuf_qbuf(&fh->vb_vbiq, b);
  1608. }
  1609. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1610. {
  1611. struct em28xx_fh *fh = priv;
  1612. struct em28xx *dev = fh->dev;
  1613. int rc;
  1614. rc = check_dev(dev);
  1615. if (rc < 0)
  1616. return rc;
  1617. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1618. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
  1619. O_NONBLOCK);
  1620. else
  1621. return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
  1622. O_NONBLOCK);
  1623. }
  1624. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1625. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1626. {
  1627. struct em28xx_fh *fh = priv;
  1628. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1629. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1630. else
  1631. return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
  1632. }
  1633. #endif
  1634. /* ----------------------------------------------------------- */
  1635. /* RADIO ESPECIFIC IOCTLS */
  1636. /* ----------------------------------------------------------- */
  1637. static int radio_querycap(struct file *file, void *priv,
  1638. struct v4l2_capability *cap)
  1639. {
  1640. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1641. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1642. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1643. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1644. cap->version = EM28XX_VERSION_CODE;
  1645. cap->capabilities = V4L2_CAP_TUNER;
  1646. return 0;
  1647. }
  1648. static int radio_g_tuner(struct file *file, void *priv,
  1649. struct v4l2_tuner *t)
  1650. {
  1651. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1652. if (unlikely(t->index > 0))
  1653. return -EINVAL;
  1654. strcpy(t->name, "Radio");
  1655. t->type = V4L2_TUNER_RADIO;
  1656. mutex_lock(&dev->lock);
  1657. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1658. mutex_unlock(&dev->lock);
  1659. return 0;
  1660. }
  1661. static int radio_enum_input(struct file *file, void *priv,
  1662. struct v4l2_input *i)
  1663. {
  1664. if (i->index != 0)
  1665. return -EINVAL;
  1666. strcpy(i->name, "Radio");
  1667. i->type = V4L2_INPUT_TYPE_TUNER;
  1668. return 0;
  1669. }
  1670. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1671. {
  1672. if (unlikely(a->index))
  1673. return -EINVAL;
  1674. strcpy(a->name, "Radio");
  1675. return 0;
  1676. }
  1677. static int radio_s_tuner(struct file *file, void *priv,
  1678. struct v4l2_tuner *t)
  1679. {
  1680. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1681. if (0 != t->index)
  1682. return -EINVAL;
  1683. mutex_lock(&dev->lock);
  1684. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1685. mutex_unlock(&dev->lock);
  1686. return 0;
  1687. }
  1688. static int radio_s_audio(struct file *file, void *fh,
  1689. struct v4l2_audio *a)
  1690. {
  1691. return 0;
  1692. }
  1693. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1694. {
  1695. return 0;
  1696. }
  1697. static int radio_queryctrl(struct file *file, void *priv,
  1698. struct v4l2_queryctrl *qc)
  1699. {
  1700. int i;
  1701. if (qc->id < V4L2_CID_BASE ||
  1702. qc->id >= V4L2_CID_LASTP1)
  1703. return -EINVAL;
  1704. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  1705. if (qc->id && qc->id == ac97_qctrl[i].id) {
  1706. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  1707. return 0;
  1708. }
  1709. }
  1710. return -EINVAL;
  1711. }
  1712. /*
  1713. * em28xx_v4l2_open()
  1714. * inits the device and starts isoc transfer
  1715. */
  1716. static int em28xx_v4l2_open(struct file *filp)
  1717. {
  1718. int errCode = 0, radio = 0;
  1719. struct video_device *vdev = video_devdata(filp);
  1720. struct em28xx *dev = video_drvdata(filp);
  1721. enum v4l2_buf_type fh_type = 0;
  1722. struct em28xx_fh *fh;
  1723. enum v4l2_field field;
  1724. switch (vdev->vfl_type) {
  1725. case VFL_TYPE_GRABBER:
  1726. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1727. break;
  1728. case VFL_TYPE_VBI:
  1729. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1730. break;
  1731. case VFL_TYPE_RADIO:
  1732. radio = 1;
  1733. break;
  1734. }
  1735. mutex_lock(&dev->lock);
  1736. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1737. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1738. dev->users);
  1739. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1740. if (!fh) {
  1741. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1742. mutex_unlock(&dev->lock);
  1743. return -ENOMEM;
  1744. }
  1745. fh->dev = dev;
  1746. fh->radio = radio;
  1747. fh->type = fh_type;
  1748. filp->private_data = fh;
  1749. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1750. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1751. em28xx_set_alternate(dev);
  1752. em28xx_resolution_set(dev);
  1753. /* Needed, since GPIO might have disabled power of
  1754. some i2c device
  1755. */
  1756. em28xx_wake_i2c(dev);
  1757. }
  1758. if (fh->radio) {
  1759. em28xx_videodbg("video_open: setting radio device\n");
  1760. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1761. }
  1762. dev->users++;
  1763. if (dev->progressive)
  1764. field = V4L2_FIELD_NONE;
  1765. else
  1766. field = V4L2_FIELD_INTERLACED;
  1767. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1768. NULL, &dev->slock,
  1769. V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
  1770. sizeof(struct em28xx_buffer), fh);
  1771. videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
  1772. NULL, &dev->slock,
  1773. V4L2_BUF_TYPE_VBI_CAPTURE,
  1774. V4L2_FIELD_SEQ_TB,
  1775. sizeof(struct em28xx_buffer), fh);
  1776. mutex_unlock(&dev->lock);
  1777. return errCode;
  1778. }
  1779. /*
  1780. * em28xx_realease_resources()
  1781. * unregisters the v4l2,i2c and usb devices
  1782. * called when the device gets disconected or at module unload
  1783. */
  1784. void em28xx_release_analog_resources(struct em28xx *dev)
  1785. {
  1786. /*FIXME: I2C IR should be disconnected */
  1787. if (dev->radio_dev) {
  1788. if (video_is_registered(dev->radio_dev))
  1789. video_unregister_device(dev->radio_dev);
  1790. else
  1791. video_device_release(dev->radio_dev);
  1792. dev->radio_dev = NULL;
  1793. }
  1794. if (dev->vbi_dev) {
  1795. em28xx_info("V4L2 device %s deregistered\n",
  1796. video_device_node_name(dev->vbi_dev));
  1797. if (video_is_registered(dev->vbi_dev))
  1798. video_unregister_device(dev->vbi_dev);
  1799. else
  1800. video_device_release(dev->vbi_dev);
  1801. dev->vbi_dev = NULL;
  1802. }
  1803. if (dev->vdev) {
  1804. em28xx_info("V4L2 device %s deregistered\n",
  1805. video_device_node_name(dev->vdev));
  1806. if (video_is_registered(dev->vdev))
  1807. video_unregister_device(dev->vdev);
  1808. else
  1809. video_device_release(dev->vdev);
  1810. dev->vdev = NULL;
  1811. }
  1812. }
  1813. /*
  1814. * em28xx_v4l2_close()
  1815. * stops streaming and deallocates all resources allocated by the v4l2
  1816. * calls and ioctls
  1817. */
  1818. static int em28xx_v4l2_close(struct file *filp)
  1819. {
  1820. struct em28xx_fh *fh = filp->private_data;
  1821. struct em28xx *dev = fh->dev;
  1822. int errCode;
  1823. em28xx_videodbg("users=%d\n", dev->users);
  1824. if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
  1825. videobuf_stop(&fh->vb_vidq);
  1826. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1827. }
  1828. if (res_check(fh, EM28XX_RESOURCE_VBI)) {
  1829. videobuf_stop(&fh->vb_vbiq);
  1830. res_free(fh, EM28XX_RESOURCE_VBI);
  1831. }
  1832. if (dev->users == 1) {
  1833. /* the device is already disconnect,
  1834. free the remaining resources */
  1835. if (dev->state & DEV_DISCONNECTED) {
  1836. em28xx_release_resources(dev);
  1837. kfree(dev);
  1838. return 0;
  1839. }
  1840. /* Save some power by putting tuner to sleep */
  1841. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
  1842. /* do this before setting alternate! */
  1843. em28xx_uninit_isoc(dev);
  1844. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1845. /* set alternate 0 */
  1846. dev->alt = 0;
  1847. em28xx_videodbg("setting alternate 0\n");
  1848. errCode = usb_set_interface(dev->udev, 0, 0);
  1849. if (errCode < 0) {
  1850. em28xx_errdev("cannot change alternate number to "
  1851. "0 (error=%i)\n", errCode);
  1852. }
  1853. }
  1854. videobuf_mmap_free(&fh->vb_vidq);
  1855. videobuf_mmap_free(&fh->vb_vbiq);
  1856. kfree(fh);
  1857. dev->users--;
  1858. wake_up_interruptible_nr(&dev->open, 1);
  1859. return 0;
  1860. }
  1861. /*
  1862. * em28xx_v4l2_read()
  1863. * will allocate buffers when called for the first time
  1864. */
  1865. static ssize_t
  1866. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1867. loff_t *pos)
  1868. {
  1869. struct em28xx_fh *fh = filp->private_data;
  1870. struct em28xx *dev = fh->dev;
  1871. int rc;
  1872. rc = check_dev(dev);
  1873. if (rc < 0)
  1874. return rc;
  1875. /* FIXME: read() is not prepared to allow changing the video
  1876. resolution while streaming. Seems a bug at em28xx_set_fmt
  1877. */
  1878. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1879. if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
  1880. return -EBUSY;
  1881. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1882. filp->f_flags & O_NONBLOCK);
  1883. }
  1884. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1885. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1886. return -EBUSY;
  1887. return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
  1888. filp->f_flags & O_NONBLOCK);
  1889. }
  1890. return 0;
  1891. }
  1892. /*
  1893. * em28xx_v4l2_poll()
  1894. * will allocate buffers when called for the first time
  1895. */
  1896. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
  1897. {
  1898. struct em28xx_fh *fh = filp->private_data;
  1899. struct em28xx *dev = fh->dev;
  1900. int rc;
  1901. rc = check_dev(dev);
  1902. if (rc < 0)
  1903. return rc;
  1904. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1905. if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
  1906. return POLLERR;
  1907. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1908. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1909. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1910. return POLLERR;
  1911. return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
  1912. } else {
  1913. return POLLERR;
  1914. }
  1915. }
  1916. /*
  1917. * em28xx_v4l2_mmap()
  1918. */
  1919. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1920. {
  1921. struct em28xx_fh *fh = filp->private_data;
  1922. struct em28xx *dev = fh->dev;
  1923. int rc;
  1924. rc = check_dev(dev);
  1925. if (rc < 0)
  1926. return rc;
  1927. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1928. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1929. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1930. rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
  1931. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1932. (unsigned long)vma->vm_start,
  1933. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1934. rc);
  1935. return rc;
  1936. }
  1937. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1938. .owner = THIS_MODULE,
  1939. .open = em28xx_v4l2_open,
  1940. .release = em28xx_v4l2_close,
  1941. .read = em28xx_v4l2_read,
  1942. .poll = em28xx_v4l2_poll,
  1943. .mmap = em28xx_v4l2_mmap,
  1944. .ioctl = video_ioctl2,
  1945. };
  1946. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1947. .vidioc_querycap = vidioc_querycap,
  1948. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1949. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1950. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1951. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1952. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1953. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  1954. .vidioc_g_audio = vidioc_g_audio,
  1955. .vidioc_s_audio = vidioc_s_audio,
  1956. .vidioc_cropcap = vidioc_cropcap,
  1957. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1958. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1959. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1960. .vidioc_reqbufs = vidioc_reqbufs,
  1961. .vidioc_querybuf = vidioc_querybuf,
  1962. .vidioc_qbuf = vidioc_qbuf,
  1963. .vidioc_dqbuf = vidioc_dqbuf,
  1964. .vidioc_g_std = vidioc_g_std,
  1965. .vidioc_s_std = vidioc_s_std,
  1966. .vidioc_g_parm = vidioc_g_parm,
  1967. .vidioc_s_parm = vidioc_s_parm,
  1968. .vidioc_enum_input = vidioc_enum_input,
  1969. .vidioc_g_input = vidioc_g_input,
  1970. .vidioc_s_input = vidioc_s_input,
  1971. .vidioc_queryctrl = vidioc_queryctrl,
  1972. .vidioc_g_ctrl = vidioc_g_ctrl,
  1973. .vidioc_s_ctrl = vidioc_s_ctrl,
  1974. .vidioc_streamon = vidioc_streamon,
  1975. .vidioc_streamoff = vidioc_streamoff,
  1976. .vidioc_g_tuner = vidioc_g_tuner,
  1977. .vidioc_s_tuner = vidioc_s_tuner,
  1978. .vidioc_g_frequency = vidioc_g_frequency,
  1979. .vidioc_s_frequency = vidioc_s_frequency,
  1980. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1981. .vidioc_g_register = vidioc_g_register,
  1982. .vidioc_s_register = vidioc_s_register,
  1983. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1984. #endif
  1985. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1986. .vidiocgmbuf = vidiocgmbuf,
  1987. #endif
  1988. };
  1989. static const struct video_device em28xx_video_template = {
  1990. .fops = &em28xx_v4l_fops,
  1991. .release = video_device_release,
  1992. .ioctl_ops = &video_ioctl_ops,
  1993. .tvnorms = V4L2_STD_ALL,
  1994. .current_norm = V4L2_STD_PAL,
  1995. };
  1996. static const struct v4l2_file_operations radio_fops = {
  1997. .owner = THIS_MODULE,
  1998. .open = em28xx_v4l2_open,
  1999. .release = em28xx_v4l2_close,
  2000. .ioctl = video_ioctl2,
  2001. };
  2002. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2003. .vidioc_querycap = radio_querycap,
  2004. .vidioc_g_tuner = radio_g_tuner,
  2005. .vidioc_enum_input = radio_enum_input,
  2006. .vidioc_g_audio = radio_g_audio,
  2007. .vidioc_s_tuner = radio_s_tuner,
  2008. .vidioc_s_audio = radio_s_audio,
  2009. .vidioc_s_input = radio_s_input,
  2010. .vidioc_queryctrl = radio_queryctrl,
  2011. .vidioc_g_ctrl = vidioc_g_ctrl,
  2012. .vidioc_s_ctrl = vidioc_s_ctrl,
  2013. .vidioc_g_frequency = vidioc_g_frequency,
  2014. .vidioc_s_frequency = vidioc_s_frequency,
  2015. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2016. .vidioc_g_register = vidioc_g_register,
  2017. .vidioc_s_register = vidioc_s_register,
  2018. #endif
  2019. };
  2020. static struct video_device em28xx_radio_template = {
  2021. .name = "em28xx-radio",
  2022. .fops = &radio_fops,
  2023. .ioctl_ops = &radio_ioctl_ops,
  2024. };
  2025. /******************************** usb interface ******************************/
  2026. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  2027. const struct video_device *template,
  2028. const char *type_name)
  2029. {
  2030. struct video_device *vfd;
  2031. vfd = video_device_alloc();
  2032. if (NULL == vfd)
  2033. return NULL;
  2034. *vfd = *template;
  2035. vfd->v4l2_dev = &dev->v4l2_dev;
  2036. vfd->release = video_device_release;
  2037. vfd->debug = video_debug;
  2038. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  2039. dev->name, type_name);
  2040. video_set_drvdata(vfd, dev);
  2041. return vfd;
  2042. }
  2043. int em28xx_register_analog_devices(struct em28xx *dev)
  2044. {
  2045. u8 val;
  2046. int ret;
  2047. printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
  2048. dev->name,
  2049. (EM28XX_VERSION_CODE >> 16) & 0xff,
  2050. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  2051. /* set default norm */
  2052. dev->norm = em28xx_video_template.current_norm;
  2053. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  2054. dev->ctl_input = 0;
  2055. /* Analog specific initialization */
  2056. dev->format = &format[0];
  2057. em28xx_set_video_format(dev, format[0].fourcc,
  2058. norm_maxw(dev), norm_maxh(dev));
  2059. video_mux(dev, dev->ctl_input);
  2060. /* Audio defaults */
  2061. dev->mute = 1;
  2062. dev->volume = 0x1f;
  2063. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  2064. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  2065. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  2066. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  2067. em28xx_set_outfmt(dev);
  2068. em28xx_colorlevels_set_default(dev);
  2069. em28xx_compression_disable(dev);
  2070. /* allocate and fill video video_device struct */
  2071. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  2072. if (!dev->vdev) {
  2073. em28xx_errdev("cannot allocate video_device.\n");
  2074. return -ENODEV;
  2075. }
  2076. /* register v4l2 video video_device */
  2077. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  2078. video_nr[dev->devno]);
  2079. if (ret) {
  2080. em28xx_errdev("unable to register video device (error=%i).\n",
  2081. ret);
  2082. return ret;
  2083. }
  2084. /* Allocate and fill vbi video_device struct */
  2085. if (em28xx_vbi_supported(dev) == 1) {
  2086. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  2087. "vbi");
  2088. /* register v4l2 vbi video_device */
  2089. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  2090. vbi_nr[dev->devno]);
  2091. if (ret < 0) {
  2092. em28xx_errdev("unable to register vbi device\n");
  2093. return ret;
  2094. }
  2095. }
  2096. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  2097. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  2098. "radio");
  2099. if (!dev->radio_dev) {
  2100. em28xx_errdev("cannot allocate video_device.\n");
  2101. return -ENODEV;
  2102. }
  2103. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  2104. radio_nr[dev->devno]);
  2105. if (ret < 0) {
  2106. em28xx_errdev("can't register radio device\n");
  2107. return ret;
  2108. }
  2109. em28xx_info("Registered radio device as %s\n",
  2110. video_device_node_name(dev->radio_dev));
  2111. }
  2112. em28xx_info("V4L2 video device registered as %s\n",
  2113. video_device_node_name(dev->vdev));
  2114. if (dev->vbi_dev)
  2115. em28xx_info("V4L2 VBI device registered as %s\n",
  2116. video_device_node_name(dev->vbi_dev));
  2117. return 0;
  2118. }