PageRenderTime 6550ms CodeModel.GetById 21ms RepoModel.GetById 44ms app.codeStats 8ms

/drivers/staging/tm6000/tm6000-video.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 1812 lines | 1370 code | 306 blank | 136 comment | 204 complexity | 2eae5d81cc277187f5c3267cd9aafc78 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
  3. *
  4. * Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
  5. *
  6. * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
  7. * - Fixed module load/unload
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation version 2
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  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/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/fs.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/ioport.h>
  30. #include <linux/init.h>
  31. #include <linux/sched.h>
  32. #include <linux/random.h>
  33. #include <linux/version.h>
  34. #include <linux/usb.h>
  35. #include <linux/videodev2.h>
  36. #include <media/v4l2-ioctl.h>
  37. #include <media/tuner.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/kthread.h>
  40. #include <linux/highmem.h>
  41. #include <linux/freezer.h>
  42. #include "tm6000-regs.h"
  43. #include "tm6000.h"
  44. #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
  45. /* Limits minimum and default number of buffers */
  46. #define TM6000_MIN_BUF 4
  47. #define TM6000_DEF_BUF 8
  48. #define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
  49. /* Declare static vars that will be used as parameters */
  50. static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
  51. static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
  52. static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */
  53. /* Debug level */
  54. int tm6000_debug;
  55. EXPORT_SYMBOL_GPL(tm6000_debug);
  56. static const struct v4l2_queryctrl no_ctrl = {
  57. .name = "42",
  58. .flags = V4L2_CTRL_FLAG_DISABLED,
  59. };
  60. /* supported controls */
  61. static struct v4l2_queryctrl tm6000_qctrl[] = {
  62. {
  63. .id = V4L2_CID_BRIGHTNESS,
  64. .type = V4L2_CTRL_TYPE_INTEGER,
  65. .name = "Brightness",
  66. .minimum = 0,
  67. .maximum = 255,
  68. .step = 1,
  69. .default_value = 54,
  70. .flags = 0,
  71. }, {
  72. .id = V4L2_CID_CONTRAST,
  73. .type = V4L2_CTRL_TYPE_INTEGER,
  74. .name = "Contrast",
  75. .minimum = 0,
  76. .maximum = 255,
  77. .step = 0x1,
  78. .default_value = 119,
  79. .flags = 0,
  80. }, {
  81. .id = V4L2_CID_SATURATION,
  82. .type = V4L2_CTRL_TYPE_INTEGER,
  83. .name = "Saturation",
  84. .minimum = 0,
  85. .maximum = 255,
  86. .step = 0x1,
  87. .default_value = 112,
  88. .flags = 0,
  89. }, {
  90. .id = V4L2_CID_HUE,
  91. .type = V4L2_CTRL_TYPE_INTEGER,
  92. .name = "Hue",
  93. .minimum = -128,
  94. .maximum = 127,
  95. .step = 0x1,
  96. .default_value = 0,
  97. .flags = 0,
  98. },
  99. /* --- audio --- */
  100. {
  101. .id = V4L2_CID_AUDIO_MUTE,
  102. .name = "Mute",
  103. .minimum = 0,
  104. .maximum = 1,
  105. .type = V4L2_CTRL_TYPE_BOOLEAN,
  106. }, {
  107. .id = V4L2_CID_AUDIO_VOLUME,
  108. .name = "Volume",
  109. .minimum = -15,
  110. .maximum = 15,
  111. .step = 1,
  112. .default_value = 0,
  113. .type = V4L2_CTRL_TYPE_INTEGER,
  114. }
  115. };
  116. static const unsigned int CTRLS = ARRAY_SIZE(tm6000_qctrl);
  117. static int qctl_regs[ARRAY_SIZE(tm6000_qctrl)];
  118. static struct tm6000_fmt format[] = {
  119. {
  120. .name = "4:2:2, packed, YVY2",
  121. .fourcc = V4L2_PIX_FMT_YUYV,
  122. .depth = 16,
  123. }, {
  124. .name = "4:2:2, packed, UYVY",
  125. .fourcc = V4L2_PIX_FMT_UYVY,
  126. .depth = 16,
  127. }, {
  128. .name = "A/V + VBI mux packet",
  129. .fourcc = V4L2_PIX_FMT_TM6000,
  130. .depth = 16,
  131. }
  132. };
  133. static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
  134. {
  135. unsigned int i;
  136. for (i = 0; i < CTRLS; i++)
  137. if (tm6000_qctrl[i].id == id)
  138. return tm6000_qctrl+i;
  139. return NULL;
  140. }
  141. /* ------------------------------------------------------------------
  142. * DMA and thread functions
  143. * ------------------------------------------------------------------
  144. */
  145. #define norm_maxw(a) 720
  146. #define norm_maxh(a) 576
  147. #define norm_minw(a) norm_maxw(a)
  148. #define norm_minh(a) norm_maxh(a)
  149. /*
  150. * video-buf generic routine to get the next available buffer
  151. */
  152. static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
  153. struct tm6000_buffer **buf)
  154. {
  155. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  156. char *outp;
  157. if (list_empty(&dma_q->active)) {
  158. dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
  159. *buf = NULL;
  160. return;
  161. }
  162. *buf = list_entry(dma_q->active.next,
  163. struct tm6000_buffer, vb.queue);
  164. if (!buf)
  165. return;
  166. /* Cleans up buffer - Useful for testing for frame/URB loss */
  167. outp = videobuf_to_vmalloc(&(*buf)->vb);
  168. return;
  169. }
  170. /*
  171. * Announces that a buffer were filled and request the next
  172. */
  173. static inline void buffer_filled(struct tm6000_core *dev,
  174. struct tm6000_dmaqueue *dma_q,
  175. struct tm6000_buffer *buf)
  176. {
  177. /* Advice that buffer was filled */
  178. dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
  179. buf->vb.state = VIDEOBUF_DONE;
  180. buf->vb.field_count++;
  181. do_gettimeofday(&buf->vb.ts);
  182. list_del(&buf->vb.queue);
  183. wake_up(&buf->vb.done);
  184. }
  185. const char *tm6000_msg_type[] = {
  186. "unknown(0)", /* 0 */
  187. "video", /* 1 */
  188. "audio", /* 2 */
  189. "vbi", /* 3 */
  190. "pts", /* 4 */
  191. "err", /* 5 */
  192. "unknown(6)", /* 6 */
  193. "unknown(7)", /* 7 */
  194. };
  195. /*
  196. * Identify the tm5600/6000 buffer header type and properly handles
  197. */
  198. static int copy_streams(u8 *data, unsigned long len,
  199. struct urb *urb)
  200. {
  201. struct tm6000_dmaqueue *dma_q = urb->context;
  202. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  203. u8 *ptr = data, *endp = data+len, c;
  204. unsigned long header = 0;
  205. int rc = 0;
  206. unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
  207. struct tm6000_buffer *vbuf = NULL;
  208. char *voutp = NULL;
  209. unsigned int linewidth;
  210. if (!dev->radio) {
  211. /* get video buffer */
  212. get_next_buf(dma_q, &vbuf);
  213. if (!vbuf)
  214. return rc;
  215. voutp = videobuf_to_vmalloc(&vbuf->vb);
  216. if (!voutp)
  217. return 0;
  218. }
  219. for (ptr = data; ptr < endp;) {
  220. if (!dev->isoc_ctl.cmd) {
  221. /* Header */
  222. if (dev->isoc_ctl.tmp_buf_len > 0) {
  223. /* from last urb or packet */
  224. header = dev->isoc_ctl.tmp_buf;
  225. if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
  226. memcpy((u8 *)&header +
  227. dev->isoc_ctl.tmp_buf_len,
  228. ptr,
  229. 4 - dev->isoc_ctl.tmp_buf_len);
  230. ptr += 4 - dev->isoc_ctl.tmp_buf_len;
  231. }
  232. dev->isoc_ctl.tmp_buf_len = 0;
  233. } else {
  234. if (ptr + 3 >= endp) {
  235. /* have incomplete header */
  236. dev->isoc_ctl.tmp_buf_len = endp - ptr;
  237. memcpy(&dev->isoc_ctl.tmp_buf, ptr,
  238. dev->isoc_ctl.tmp_buf_len);
  239. return rc;
  240. }
  241. /* Seek for sync */
  242. for (; ptr < endp - 3; ptr++) {
  243. if (*(ptr + 3) == 0x47)
  244. break;
  245. }
  246. /* Get message header */
  247. header = *(unsigned long *)ptr;
  248. ptr += 4;
  249. }
  250. /* split the header fields */
  251. c = (header >> 24) & 0xff;
  252. size = ((header & 0x7e) << 1);
  253. if (size > 0)
  254. size -= 4;
  255. block = (header >> 7) & 0xf;
  256. field = (header >> 11) & 0x1;
  257. line = (header >> 12) & 0x1ff;
  258. cmd = (header >> 21) & 0x7;
  259. /* Validates haeder fields */
  260. if (size > TM6000_URB_MSG_LEN)
  261. size = TM6000_URB_MSG_LEN;
  262. pktsize = TM6000_URB_MSG_LEN;
  263. /* calculate position in buffer
  264. * and change the buffer
  265. */
  266. switch (cmd) {
  267. case TM6000_URB_MSG_VIDEO:
  268. if (!dev->radio) {
  269. if ((dev->isoc_ctl.vfield != field) &&
  270. (field == 1)) {
  271. /* Announces that a new buffer
  272. * were filled
  273. */
  274. buffer_filled(dev, dma_q, vbuf);
  275. dprintk(dev, V4L2_DEBUG_ISOC,
  276. "new buffer filled\n");
  277. get_next_buf(dma_q, &vbuf);
  278. if (!vbuf)
  279. return rc;
  280. voutp = videobuf_to_vmalloc(&vbuf->vb);
  281. if (!voutp)
  282. return rc;
  283. memset(voutp, 0, vbuf->vb.size);
  284. }
  285. linewidth = vbuf->vb.width << 1;
  286. pos = ((line << 1) - field - 1) *
  287. linewidth + block * TM6000_URB_MSG_LEN;
  288. /* Don't allow to write out of the buffer */
  289. if (pos + size > vbuf->vb.size)
  290. cmd = TM6000_URB_MSG_ERR;
  291. dev->isoc_ctl.vfield = field;
  292. }
  293. break;
  294. case TM6000_URB_MSG_VBI:
  295. break;
  296. case TM6000_URB_MSG_AUDIO:
  297. case TM6000_URB_MSG_PTS:
  298. size = pktsize; /* Size is always 180 bytes */
  299. break;
  300. }
  301. } else {
  302. /* Continue the last copy */
  303. cmd = dev->isoc_ctl.cmd;
  304. size = dev->isoc_ctl.size;
  305. pos = dev->isoc_ctl.pos;
  306. pktsize = dev->isoc_ctl.pktsize;
  307. field = dev->isoc_ctl.field;
  308. }
  309. cpysize = (endp - ptr > size) ? size : endp - ptr;
  310. if (cpysize) {
  311. /* copy data in different buffers */
  312. switch (cmd) {
  313. case TM6000_URB_MSG_VIDEO:
  314. /* Fills video buffer */
  315. if (vbuf)
  316. memcpy(&voutp[pos], ptr, cpysize);
  317. break;
  318. case TM6000_URB_MSG_AUDIO: {
  319. int i;
  320. for (i = 0; i < cpysize; i += 2)
  321. swab16s((u16 *)(ptr + i));
  322. tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
  323. break;
  324. }
  325. case TM6000_URB_MSG_VBI:
  326. /* Need some code to copy vbi buffer */
  327. break;
  328. case TM6000_URB_MSG_PTS: {
  329. /* Need some code to copy pts */
  330. u32 pts;
  331. pts = *(u32 *)ptr;
  332. dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
  333. field, pts);
  334. break;
  335. }
  336. }
  337. }
  338. if (ptr + pktsize > endp) {
  339. /* End of URB packet, but cmd processing is not
  340. * complete. Preserve the state for a next packet
  341. */
  342. dev->isoc_ctl.pos = pos + cpysize;
  343. dev->isoc_ctl.size = size - cpysize;
  344. dev->isoc_ctl.cmd = cmd;
  345. dev->isoc_ctl.field = field;
  346. dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
  347. ptr += endp - ptr;
  348. } else {
  349. dev->isoc_ctl.cmd = 0;
  350. ptr += pktsize;
  351. }
  352. }
  353. return 0;
  354. }
  355. /*
  356. * Identify the tm5600/6000 buffer header type and properly handles
  357. */
  358. static int copy_multiplexed(u8 *ptr, unsigned long len,
  359. struct urb *urb)
  360. {
  361. struct tm6000_dmaqueue *dma_q = urb->context;
  362. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  363. unsigned int pos = dev->isoc_ctl.pos, cpysize;
  364. int rc = 1;
  365. struct tm6000_buffer *buf;
  366. char *outp = NULL;
  367. get_next_buf(dma_q, &buf);
  368. if (buf)
  369. outp = videobuf_to_vmalloc(&buf->vb);
  370. if (!outp)
  371. return 0;
  372. while (len > 0) {
  373. cpysize = min(len, buf->vb.size-pos);
  374. memcpy(&outp[pos], ptr, cpysize);
  375. pos += cpysize;
  376. ptr += cpysize;
  377. len -= cpysize;
  378. if (pos >= buf->vb.size) {
  379. pos = 0;
  380. /* Announces that a new buffer were filled */
  381. buffer_filled(dev, dma_q, buf);
  382. dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
  383. get_next_buf(dma_q, &buf);
  384. if (!buf)
  385. break;
  386. outp = videobuf_to_vmalloc(&(buf->vb));
  387. if (!outp)
  388. return rc;
  389. pos = 0;
  390. }
  391. }
  392. dev->isoc_ctl.pos = pos;
  393. return rc;
  394. }
  395. static inline void print_err_status(struct tm6000_core *dev,
  396. int packet, int status)
  397. {
  398. char *errmsg = "Unknown";
  399. switch (status) {
  400. case -ENOENT:
  401. errmsg = "unlinked synchronuously";
  402. break;
  403. case -ECONNRESET:
  404. errmsg = "unlinked asynchronuously";
  405. break;
  406. case -ENOSR:
  407. errmsg = "Buffer error (overrun)";
  408. break;
  409. case -EPIPE:
  410. errmsg = "Stalled (device not responding)";
  411. break;
  412. case -EOVERFLOW:
  413. errmsg = "Babble (bad cable?)";
  414. break;
  415. case -EPROTO:
  416. errmsg = "Bit-stuff error (bad cable?)";
  417. break;
  418. case -EILSEQ:
  419. errmsg = "CRC/Timeout (could be anything)";
  420. break;
  421. case -ETIME:
  422. errmsg = "Device does not respond";
  423. break;
  424. }
  425. if (packet < 0) {
  426. dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
  427. status, errmsg);
  428. } else {
  429. dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
  430. packet, status, errmsg);
  431. }
  432. }
  433. /*
  434. * Controls the isoc copy of each urb packet
  435. */
  436. static inline int tm6000_isoc_copy(struct urb *urb)
  437. {
  438. struct tm6000_dmaqueue *dma_q = urb->context;
  439. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  440. int i, len = 0, rc = 1, status;
  441. char *p;
  442. if (urb->status < 0) {
  443. print_err_status(dev, -1, urb->status);
  444. return 0;
  445. }
  446. for (i = 0; i < urb->number_of_packets; i++) {
  447. status = urb->iso_frame_desc[i].status;
  448. if (status < 0) {
  449. print_err_status(dev, i, status);
  450. continue;
  451. }
  452. len = urb->iso_frame_desc[i].actual_length;
  453. if (len > 0) {
  454. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  455. if (!urb->iso_frame_desc[i].status) {
  456. if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
  457. rc = copy_multiplexed(p, len, urb);
  458. if (rc <= 0)
  459. return rc;
  460. } else {
  461. copy_streams(p, len, urb);
  462. }
  463. }
  464. }
  465. }
  466. return rc;
  467. }
  468. /* ------------------------------------------------------------------
  469. * URB control
  470. * ------------------------------------------------------------------
  471. */
  472. /*
  473. * IRQ callback, called by URB callback
  474. */
  475. static void tm6000_irq_callback(struct urb *urb)
  476. {
  477. struct tm6000_dmaqueue *dma_q = urb->context;
  478. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  479. int i;
  480. if (!dev)
  481. return;
  482. spin_lock(&dev->slock);
  483. tm6000_isoc_copy(urb);
  484. spin_unlock(&dev->slock);
  485. /* Reset urb buffers */
  486. for (i = 0; i < urb->number_of_packets; i++) {
  487. urb->iso_frame_desc[i].status = 0;
  488. urb->iso_frame_desc[i].actual_length = 0;
  489. }
  490. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  491. if (urb->status)
  492. tm6000_err("urb resubmit failed (error=%i)\n",
  493. urb->status);
  494. }
  495. /*
  496. * Stop and Deallocate URBs
  497. */
  498. static void tm6000_uninit_isoc(struct tm6000_core *dev)
  499. {
  500. struct urb *urb;
  501. int i;
  502. dev->isoc_ctl.buf = NULL;
  503. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  504. urb = dev->isoc_ctl.urb[i];
  505. if (urb) {
  506. usb_kill_urb(urb);
  507. usb_unlink_urb(urb);
  508. if (dev->isoc_ctl.transfer_buffer[i]) {
  509. usb_free_coherent(dev->udev,
  510. urb->transfer_buffer_length,
  511. dev->isoc_ctl.transfer_buffer[i],
  512. urb->transfer_dma);
  513. }
  514. usb_free_urb(urb);
  515. dev->isoc_ctl.urb[i] = NULL;
  516. }
  517. dev->isoc_ctl.transfer_buffer[i] = NULL;
  518. }
  519. kfree(dev->isoc_ctl.urb);
  520. kfree(dev->isoc_ctl.transfer_buffer);
  521. dev->isoc_ctl.urb = NULL;
  522. dev->isoc_ctl.transfer_buffer = NULL;
  523. dev->isoc_ctl.num_bufs = 0;
  524. }
  525. /*
  526. * Allocate URBs and start IRQ
  527. */
  528. static int tm6000_prepare_isoc(struct tm6000_core *dev)
  529. {
  530. struct tm6000_dmaqueue *dma_q = &dev->vidq;
  531. int i, j, sb_size, pipe, size, max_packets, num_bufs = 8;
  532. struct urb *urb;
  533. /* De-allocates all pending stuff */
  534. tm6000_uninit_isoc(dev);
  535. /* Stop interrupt USB pipe */
  536. tm6000_ir_int_stop(dev);
  537. usb_set_interface(dev->udev,
  538. dev->isoc_in.bInterfaceNumber,
  539. dev->isoc_in.bAlternateSetting);
  540. /* Start interrupt USB pipe */
  541. tm6000_ir_int_start(dev);
  542. pipe = usb_rcvisocpipe(dev->udev,
  543. dev->isoc_in.endp->desc.bEndpointAddress &
  544. USB_ENDPOINT_NUMBER_MASK);
  545. size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
  546. if (size > dev->isoc_in.maxsize)
  547. size = dev->isoc_in.maxsize;
  548. dev->isoc_ctl.max_pkt_size = size;
  549. max_packets = TM6000_MAX_ISO_PACKETS;
  550. sb_size = max_packets * size;
  551. dev->isoc_ctl.num_bufs = num_bufs;
  552. dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
  553. if (!dev->isoc_ctl.urb) {
  554. tm6000_err("cannot alloc memory for usb buffers\n");
  555. return -ENOMEM;
  556. }
  557. dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
  558. GFP_KERNEL);
  559. if (!dev->isoc_ctl.transfer_buffer) {
  560. tm6000_err("cannot allocate memory for usbtransfer\n");
  561. kfree(dev->isoc_ctl.urb);
  562. return -ENOMEM;
  563. }
  564. dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
  565. " (%d bytes) of %d bytes each to handle %u size\n",
  566. max_packets, num_bufs, sb_size,
  567. dev->isoc_in.maxsize, size);
  568. /* allocate urbs and transfer buffers */
  569. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  570. urb = usb_alloc_urb(max_packets, GFP_KERNEL);
  571. if (!urb) {
  572. tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
  573. tm6000_uninit_isoc(dev);
  574. usb_free_urb(urb);
  575. return -ENOMEM;
  576. }
  577. dev->isoc_ctl.urb[i] = urb;
  578. dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
  579. sb_size, GFP_KERNEL, &urb->transfer_dma);
  580. if (!dev->isoc_ctl.transfer_buffer[i]) {
  581. tm6000_err("unable to allocate %i bytes for transfer"
  582. " buffer %i%s\n",
  583. sb_size, i,
  584. in_interrupt() ? " while in int" : "");
  585. tm6000_uninit_isoc(dev);
  586. return -ENOMEM;
  587. }
  588. memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
  589. usb_fill_bulk_urb(urb, dev->udev, pipe,
  590. dev->isoc_ctl.transfer_buffer[i], sb_size,
  591. tm6000_irq_callback, dma_q);
  592. urb->interval = dev->isoc_in.endp->desc.bInterval;
  593. urb->number_of_packets = max_packets;
  594. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  595. for (j = 0; j < max_packets; j++) {
  596. urb->iso_frame_desc[j].offset = size * j;
  597. urb->iso_frame_desc[j].length = size;
  598. }
  599. }
  600. return 0;
  601. }
  602. static int tm6000_start_thread(struct tm6000_core *dev)
  603. {
  604. struct tm6000_dmaqueue *dma_q = &dev->vidq;
  605. int i;
  606. dma_q->frame = 0;
  607. dma_q->ini_jiffies = jiffies;
  608. init_waitqueue_head(&dma_q->wq);
  609. /* submit urbs and enables IRQ */
  610. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  611. int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
  612. if (rc) {
  613. tm6000_err("submit of urb %i failed (error=%i)\n", i,
  614. rc);
  615. tm6000_uninit_isoc(dev);
  616. return rc;
  617. }
  618. }
  619. return 0;
  620. }
  621. /* ------------------------------------------------------------------
  622. * Videobuf operations
  623. * ------------------------------------------------------------------
  624. */
  625. static int
  626. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  627. {
  628. struct tm6000_fh *fh = vq->priv_data;
  629. *size = fh->fmt->depth * fh->width * fh->height >> 3;
  630. if (0 == *count)
  631. *count = TM6000_DEF_BUF;
  632. if (*count < TM6000_MIN_BUF)
  633. *count = TM6000_MIN_BUF;
  634. while (*size * *count > vid_limit * 1024 * 1024)
  635. (*count)--;
  636. return 0;
  637. }
  638. static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
  639. {
  640. struct tm6000_fh *fh = vq->priv_data;
  641. struct tm6000_core *dev = fh->dev;
  642. unsigned long flags;
  643. if (in_interrupt())
  644. BUG();
  645. /* We used to wait for the buffer to finish here, but this didn't work
  646. because, as we were keeping the state as VIDEOBUF_QUEUED,
  647. videobuf_queue_cancel marked it as finished for us.
  648. (Also, it could wedge forever if the hardware was misconfigured.)
  649. This should be safe; by the time we get here, the buffer isn't
  650. queued anymore. If we ever start marking the buffers as
  651. VIDEOBUF_ACTIVE, it won't be, though.
  652. */
  653. spin_lock_irqsave(&dev->slock, flags);
  654. if (dev->isoc_ctl.buf == buf)
  655. dev->isoc_ctl.buf = NULL;
  656. spin_unlock_irqrestore(&dev->slock, flags);
  657. videobuf_vmalloc_free(&buf->vb);
  658. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  659. }
  660. static int
  661. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  662. enum v4l2_field field)
  663. {
  664. struct tm6000_fh *fh = vq->priv_data;
  665. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  666. struct tm6000_core *dev = fh->dev;
  667. int rc = 0, urb_init = 0;
  668. BUG_ON(NULL == fh->fmt);
  669. /* FIXME: It assumes depth=2 */
  670. /* The only currently supported format is 16 bits/pixel */
  671. buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
  672. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  673. return -EINVAL;
  674. if (buf->fmt != fh->fmt ||
  675. buf->vb.width != fh->width ||
  676. buf->vb.height != fh->height ||
  677. buf->vb.field != field) {
  678. buf->fmt = fh->fmt;
  679. buf->vb.width = fh->width;
  680. buf->vb.height = fh->height;
  681. buf->vb.field = field;
  682. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  683. }
  684. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  685. if (0 != (rc = videobuf_iolock(vq, &buf->vb, NULL)))
  686. goto fail;
  687. urb_init = 1;
  688. }
  689. if (!dev->isoc_ctl.num_bufs)
  690. urb_init = 1;
  691. if (urb_init) {
  692. rc = tm6000_prepare_isoc(dev);
  693. if (rc < 0)
  694. goto fail;
  695. rc = tm6000_start_thread(dev);
  696. if (rc < 0)
  697. goto fail;
  698. }
  699. buf->vb.state = VIDEOBUF_PREPARED;
  700. return 0;
  701. fail:
  702. free_buffer(vq, buf);
  703. return rc;
  704. }
  705. static void
  706. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  707. {
  708. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  709. struct tm6000_fh *fh = vq->priv_data;
  710. struct tm6000_core *dev = fh->dev;
  711. struct tm6000_dmaqueue *vidq = &dev->vidq;
  712. buf->vb.state = VIDEOBUF_QUEUED;
  713. list_add_tail(&buf->vb.queue, &vidq->active);
  714. }
  715. static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  716. {
  717. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  718. free_buffer(vq, buf);
  719. }
  720. static struct videobuf_queue_ops tm6000_video_qops = {
  721. .buf_setup = buffer_setup,
  722. .buf_prepare = buffer_prepare,
  723. .buf_queue = buffer_queue,
  724. .buf_release = buffer_release,
  725. };
  726. /* ------------------------------------------------------------------
  727. * IOCTL handling
  728. * ------------------------------------------------------------------
  729. */
  730. static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
  731. {
  732. /* Is the current fh handling it? if so, that's OK */
  733. if (dev->resources == fh && dev->is_res_read)
  734. return true;
  735. return false;
  736. }
  737. static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
  738. {
  739. /* Is the current fh handling it? if so, that's OK */
  740. if (dev->resources == fh)
  741. return true;
  742. return false;
  743. }
  744. static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
  745. bool is_res_read)
  746. {
  747. /* Is the current fh handling it? if so, that's OK */
  748. if (dev->resources == fh && dev->is_res_read == is_res_read)
  749. return true;
  750. /* is it free? */
  751. if (dev->resources)
  752. return false;
  753. /* grab it */
  754. dev->resources = fh;
  755. dev->is_res_read = is_res_read;
  756. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
  757. return true;
  758. }
  759. static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
  760. {
  761. /* Is the current fh handling it? if so, that's OK */
  762. if (dev->resources != fh)
  763. return;
  764. dev->resources = NULL;
  765. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
  766. }
  767. /* ------------------------------------------------------------------
  768. * IOCTL vidioc handling
  769. * ------------------------------------------------------------------
  770. */
  771. static int vidioc_querycap(struct file *file, void *priv,
  772. struct v4l2_capability *cap)
  773. {
  774. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  775. strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
  776. strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
  777. cap->version = TM6000_VERSION;
  778. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  779. V4L2_CAP_STREAMING |
  780. V4L2_CAP_AUDIO |
  781. V4L2_CAP_READWRITE;
  782. if (dev->tuner_type != TUNER_ABSENT)
  783. cap->capabilities |= V4L2_CAP_TUNER;
  784. return 0;
  785. }
  786. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  787. struct v4l2_fmtdesc *f)
  788. {
  789. if (unlikely(f->index >= ARRAY_SIZE(format)))
  790. return -EINVAL;
  791. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  792. f->pixelformat = format[f->index].fourcc;
  793. return 0;
  794. }
  795. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  796. struct v4l2_format *f)
  797. {
  798. struct tm6000_fh *fh = priv;
  799. f->fmt.pix.width = fh->width;
  800. f->fmt.pix.height = fh->height;
  801. f->fmt.pix.field = fh->vb_vidq.field;
  802. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  803. f->fmt.pix.bytesperline =
  804. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  805. f->fmt.pix.sizeimage =
  806. f->fmt.pix.height * f->fmt.pix.bytesperline;
  807. return 0;
  808. }
  809. static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
  810. {
  811. unsigned int i;
  812. for (i = 0; i < ARRAY_SIZE(format); i++)
  813. if (format[i].fourcc == fourcc)
  814. return format+i;
  815. return NULL;
  816. }
  817. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  818. struct v4l2_format *f)
  819. {
  820. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  821. struct tm6000_fmt *fmt;
  822. enum v4l2_field field;
  823. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  824. if (NULL == fmt) {
  825. dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
  826. " invalid.\n", f->fmt.pix.pixelformat);
  827. return -EINVAL;
  828. }
  829. field = f->fmt.pix.field;
  830. if (field == V4L2_FIELD_ANY)
  831. field = V4L2_FIELD_SEQ_TB;
  832. else if (V4L2_FIELD_INTERLACED != field) {
  833. dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
  834. return -EINVAL;
  835. }
  836. tm6000_get_std_res(dev);
  837. f->fmt.pix.width = dev->width;
  838. f->fmt.pix.height = dev->height;
  839. f->fmt.pix.width &= ~0x01;
  840. f->fmt.pix.field = field;
  841. f->fmt.pix.bytesperline =
  842. (f->fmt.pix.width * fmt->depth) >> 3;
  843. f->fmt.pix.sizeimage =
  844. f->fmt.pix.height * f->fmt.pix.bytesperline;
  845. return 0;
  846. }
  847. /*FIXME: This seems to be generic enough to be at videodev2 */
  848. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  849. struct v4l2_format *f)
  850. {
  851. struct tm6000_fh *fh = priv;
  852. struct tm6000_core *dev = fh->dev;
  853. int ret = vidioc_try_fmt_vid_cap(file, fh, f);
  854. if (ret < 0)
  855. return ret;
  856. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  857. fh->width = f->fmt.pix.width;
  858. fh->height = f->fmt.pix.height;
  859. fh->vb_vidq.field = f->fmt.pix.field;
  860. fh->type = f->type;
  861. dev->fourcc = f->fmt.pix.pixelformat;
  862. tm6000_set_fourcc_format(dev);
  863. return 0;
  864. }
  865. static int vidioc_reqbufs(struct file *file, void *priv,
  866. struct v4l2_requestbuffers *p)
  867. {
  868. struct tm6000_fh *fh = priv;
  869. return videobuf_reqbufs(&fh->vb_vidq, p);
  870. }
  871. static int vidioc_querybuf(struct file *file, void *priv,
  872. struct v4l2_buffer *p)
  873. {
  874. struct tm6000_fh *fh = priv;
  875. return videobuf_querybuf(&fh->vb_vidq, p);
  876. }
  877. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  878. {
  879. struct tm6000_fh *fh = priv;
  880. return videobuf_qbuf(&fh->vb_vidq, p);
  881. }
  882. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  883. {
  884. struct tm6000_fh *fh = priv;
  885. return videobuf_dqbuf(&fh->vb_vidq, p,
  886. file->f_flags & O_NONBLOCK);
  887. }
  888. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  889. {
  890. struct tm6000_fh *fh = priv;
  891. struct tm6000_core *dev = fh->dev;
  892. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  893. return -EINVAL;
  894. if (i != fh->type)
  895. return -EINVAL;
  896. if (!res_get(dev, fh, false))
  897. return -EBUSY;
  898. return (videobuf_streamon(&fh->vb_vidq));
  899. }
  900. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  901. {
  902. struct tm6000_fh *fh=priv;
  903. struct tm6000_core *dev = fh->dev;
  904. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  905. return -EINVAL;
  906. if (i != fh->type)
  907. return -EINVAL;
  908. videobuf_streamoff(&fh->vb_vidq);
  909. res_free(dev,fh);
  910. return (0);
  911. }
  912. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
  913. {
  914. int rc=0;
  915. struct tm6000_fh *fh=priv;
  916. struct tm6000_core *dev = fh->dev;
  917. dev->norm = *norm;
  918. rc = tm6000_init_analog_mode(dev);
  919. fh->width = dev->width;
  920. fh->height = dev->height;
  921. if (rc<0)
  922. return rc;
  923. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  924. return 0;
  925. }
  926. static const char *iname [] = {
  927. [TM6000_INPUT_TV] = "Television",
  928. [TM6000_INPUT_COMPOSITE1] = "Composite 1",
  929. [TM6000_INPUT_COMPOSITE2] = "Composite 2",
  930. [TM6000_INPUT_SVIDEO] = "S-Video",
  931. };
  932. static int vidioc_enum_input(struct file *file, void *priv,
  933. struct v4l2_input *i)
  934. {
  935. struct tm6000_fh *fh = priv;
  936. struct tm6000_core *dev = fh->dev;
  937. unsigned int n;
  938. n = i->index;
  939. if (n >= 3)
  940. return -EINVAL;
  941. if (!dev->vinput[n].type)
  942. return -EINVAL;
  943. i->index = n;
  944. if (dev->vinput[n].type == TM6000_INPUT_TV)
  945. i->type = V4L2_INPUT_TYPE_TUNER;
  946. else
  947. i->type = V4L2_INPUT_TYPE_CAMERA;
  948. strcpy(i->name, iname[dev->vinput[n].type]);
  949. i->std = TM6000_STD;
  950. return 0;
  951. }
  952. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  953. {
  954. struct tm6000_fh *fh = priv;
  955. struct tm6000_core *dev = fh->dev;
  956. *i = dev->input;
  957. return 0;
  958. }
  959. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  960. {
  961. struct tm6000_fh *fh = priv;
  962. struct tm6000_core *dev = fh->dev;
  963. int rc = 0;
  964. if (i >= 3)
  965. return -EINVAL;
  966. if (!dev->vinput[i].type)
  967. return -EINVAL;
  968. dev->input = i;
  969. rc = vidioc_s_std(file, priv, &dev->vfd->current_norm);
  970. return rc;
  971. }
  972. /* --- controls ---------------------------------------------- */
  973. static int vidioc_queryctrl(struct file *file, void *priv,
  974. struct v4l2_queryctrl *qc)
  975. {
  976. int i;
  977. for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
  978. if (qc->id && qc->id == tm6000_qctrl[i].id) {
  979. memcpy(qc, &(tm6000_qctrl[i]),
  980. sizeof(*qc));
  981. return 0;
  982. }
  983. return -EINVAL;
  984. }
  985. static int vidioc_g_ctrl(struct file *file, void *priv,
  986. struct v4l2_control *ctrl)
  987. {
  988. struct tm6000_fh *fh = priv;
  989. struct tm6000_core *dev = fh->dev;
  990. int val;
  991. /* FIXME: Probably, those won't work! Maybe we need shadow regs */
  992. switch (ctrl->id) {
  993. case V4L2_CID_CONTRAST:
  994. val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
  995. break;
  996. case V4L2_CID_BRIGHTNESS:
  997. val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
  998. return 0;
  999. case V4L2_CID_SATURATION:
  1000. val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
  1001. return 0;
  1002. case V4L2_CID_HUE:
  1003. val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
  1004. return 0;
  1005. case V4L2_CID_AUDIO_MUTE:
  1006. val = dev->ctl_mute;
  1007. return 0;
  1008. case V4L2_CID_AUDIO_VOLUME:
  1009. val = dev->ctl_volume;
  1010. return 0;
  1011. default:
  1012. return -EINVAL;
  1013. }
  1014. if (val < 0)
  1015. return val;
  1016. ctrl->value = val;
  1017. return 0;
  1018. }
  1019. static int vidioc_s_ctrl(struct file *file, void *priv,
  1020. struct v4l2_control *ctrl)
  1021. {
  1022. struct tm6000_fh *fh = priv;
  1023. struct tm6000_core *dev = fh->dev;
  1024. u8 val = ctrl->value;
  1025. switch (ctrl->id) {
  1026. case V4L2_CID_CONTRAST:
  1027. tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
  1028. return 0;
  1029. case V4L2_CID_BRIGHTNESS:
  1030. tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
  1031. return 0;
  1032. case V4L2_CID_SATURATION:
  1033. tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
  1034. return 0;
  1035. case V4L2_CID_HUE:
  1036. tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
  1037. return 0;
  1038. case V4L2_CID_AUDIO_MUTE:
  1039. dev->ctl_mute = val;
  1040. tm6000_tvaudio_set_mute(dev, val);
  1041. return 0;
  1042. case V4L2_CID_AUDIO_VOLUME:
  1043. dev->ctl_volume = val;
  1044. tm6000_set_volume(dev, val);
  1045. return 0;
  1046. }
  1047. return -EINVAL;
  1048. }
  1049. static int vidioc_g_tuner(struct file *file, void *priv,
  1050. struct v4l2_tuner *t)
  1051. {
  1052. struct tm6000_fh *fh = priv;
  1053. struct tm6000_core *dev = fh->dev;
  1054. if (unlikely(UNSET == dev->tuner_type))
  1055. return -EINVAL;
  1056. if (0 != t->index)
  1057. return -EINVAL;
  1058. strcpy(t->name, "Television");
  1059. t->type = V4L2_TUNER_ANALOG_TV;
  1060. t->capability = V4L2_TUNER_CAP_NORM;
  1061. t->rangehigh = 0xffffffffUL;
  1062. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1063. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1064. t->audmode = dev->amode;
  1065. return 0;
  1066. }
  1067. static int vidioc_s_tuner(struct file *file, void *priv,
  1068. struct v4l2_tuner *t)
  1069. {
  1070. struct tm6000_fh *fh = priv;
  1071. struct tm6000_core *dev = fh->dev;
  1072. if (UNSET == dev->tuner_type)
  1073. return -EINVAL;
  1074. if (0 != t->index)
  1075. return -EINVAL;
  1076. dev->amode = t->audmode;
  1077. dprintk(dev, 3, "audio mode: %x\n", t->audmode);
  1078. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1079. return 0;
  1080. }
  1081. static int vidioc_g_frequency(struct file *file, void *priv,
  1082. struct v4l2_frequency *f)
  1083. {
  1084. struct tm6000_fh *fh = priv;
  1085. struct tm6000_core *dev = fh->dev;
  1086. if (unlikely(UNSET == dev->tuner_type))
  1087. return -EINVAL;
  1088. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1089. f->frequency = dev->freq;
  1090. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
  1091. return 0;
  1092. }
  1093. static int vidioc_s_frequency(struct file *file, void *priv,
  1094. struct v4l2_frequency *f)
  1095. {
  1096. struct tm6000_fh *fh = priv;
  1097. struct tm6000_core *dev = fh->dev;
  1098. if (unlikely(UNSET == dev->tuner_type))
  1099. return -EINVAL;
  1100. if (unlikely(f->tuner != 0))
  1101. return -EINVAL;
  1102. if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
  1103. return -EINVAL;
  1104. if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
  1105. return -EINVAL;
  1106. dev->freq = f->frequency;
  1107. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1108. return 0;
  1109. }
  1110. static int radio_querycap(struct file *file, void *priv,
  1111. struct v4l2_capability *cap)
  1112. {
  1113. struct tm6000_fh *fh = file->private_data;
  1114. struct tm6000_core *dev = fh->dev;
  1115. strcpy(cap->driver, "tm6000");
  1116. strlcpy(cap->card, dev->name, sizeof(dev->name));
  1117. sprintf(cap->bus_info, "USB%04x:%04x",
  1118. le16_to_cpu(dev->udev->descriptor.idVendor),
  1119. le16_to_cpu(dev->udev->descriptor.idProduct));
  1120. cap->version = dev->dev_type;
  1121. cap->capabilities = V4L2_CAP_TUNER |
  1122. V4L2_CAP_AUDIO |
  1123. V4L2_CAP_RADIO |
  1124. V4L2_CAP_READWRITE |
  1125. V4L2_CAP_STREAMING;
  1126. return 0;
  1127. }
  1128. static int radio_g_tuner(struct file *file, void *priv,
  1129. struct v4l2_tuner *t)
  1130. {
  1131. struct tm6000_fh *fh = file->private_data;
  1132. struct tm6000_core *dev = fh->dev;
  1133. if (0 != t->index)
  1134. return -EINVAL;
  1135. memset(t, 0, sizeof(*t));
  1136. strcpy(t->name, "Radio");
  1137. t->type = V4L2_TUNER_RADIO;
  1138. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1139. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1140. return 0;
  1141. }
  1142. static int radio_s_tuner(struct file *file, void *priv,
  1143. struct v4l2_tuner *t)
  1144. {
  1145. struct tm6000_fh *fh = file->private_data;
  1146. struct tm6000_core *dev = fh->dev;
  1147. if (0 != t->index)
  1148. return -EINVAL;
  1149. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1150. return 0;
  1151. }
  1152. static int radio_enum_input(struct file *file, void *priv,
  1153. struct v4l2_input *i)
  1154. {
  1155. struct tm6000_fh *fh = priv;
  1156. struct tm6000_core *dev = fh->dev;
  1157. if (i->index != 0)
  1158. return -EINVAL;
  1159. if (!dev->rinput.type)
  1160. return -EINVAL;
  1161. strcpy(i->name, "Radio");
  1162. i->type = V4L2_INPUT_TYPE_TUNER;
  1163. return 0;
  1164. }
  1165. static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
  1166. {
  1167. struct tm6000_fh *fh = priv;
  1168. struct tm6000_core *dev = fh->dev;
  1169. if (dev->input !=5)
  1170. return -EINVAL;
  1171. *i = dev->input -5;
  1172. return 0;
  1173. }
  1174. static int radio_g_audio(struct file *file, void *priv,
  1175. struct v4l2_audio *a)
  1176. {
  1177. memset(a, 0, sizeof(*a));
  1178. strcpy(a->name, "Radio");
  1179. return 0;
  1180. }
  1181. static int radio_s_audio(struct file *file, void *priv,
  1182. struct v4l2_audio *a)
  1183. {
  1184. return 0;
  1185. }
  1186. static int radio_s_input(struct file *filp, void *priv, unsigned int i)
  1187. {
  1188. struct tm6000_fh *fh = priv;
  1189. struct tm6000_core *dev = fh->dev;
  1190. if (i)
  1191. return -EINVAL;
  1192. if (!dev->rinput.type)
  1193. return -EINVAL;
  1194. dev->input = i + 5;
  1195. return 0;
  1196. }
  1197. static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
  1198. {
  1199. return 0;
  1200. }
  1201. static int radio_queryctrl(struct file *file, void *priv,
  1202. struct v4l2_queryctrl *c)
  1203. {
  1204. const struct v4l2_queryctrl *ctrl;
  1205. if (c->id < V4L2_CID_BASE ||
  1206. c->id >= V4L2_CID_LASTP1)
  1207. return -EINVAL;
  1208. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1209. ctrl = ctrl_by_id(c->id);
  1210. *c = *ctrl;
  1211. } else
  1212. *c = no_ctrl;
  1213. return 0;
  1214. }
  1215. /* ------------------------------------------------------------------
  1216. File operations for the device
  1217. ------------------------------------------------------------------*/
  1218. static int tm6000_open(struct file *file)
  1219. {
  1220. struct video_device *vdev = video_devdata(file);
  1221. struct tm6000_core *dev = video_drvdata(file);
  1222. struct tm6000_fh *fh;
  1223. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1224. int i, rc;
  1225. int radio = 0;
  1226. printk(KERN_INFO "tm6000: open called (dev=%s)\n",
  1227. video_device_node_name(vdev));
  1228. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
  1229. video_device_node_name(vdev));
  1230. switch (vdev->vfl_type) {
  1231. case VFL_TYPE_GRABBER:
  1232. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1233. break;
  1234. case VFL_TYPE_VBI:
  1235. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1236. break;
  1237. case VFL_TYPE_RADIO:
  1238. radio = 1;
  1239. break;
  1240. }
  1241. /* If more than one user, mutex should be added */
  1242. dev->users++;
  1243. dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
  1244. video_device_node_name(vdev), v4l2_type_names[type],
  1245. dev->users);
  1246. /* allocate + initialize per filehandle data */
  1247. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1248. if (NULL == fh) {
  1249. dev->users--;
  1250. return -ENOMEM;
  1251. }
  1252. file->private_data = fh;
  1253. fh->dev = dev;
  1254. fh->radio = radio;
  1255. dev->radio = radio;
  1256. fh->type = type;
  1257. dev->fourcc = format[0].fourcc;
  1258. fh->fmt = format_by_fourcc(dev->fourcc);
  1259. tm6000_get_std_res (dev);
  1260. fh->width = dev->width;
  1261. fh->height = dev->height;
  1262. dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
  1263. "dev->vidq=0x%08lx\n",
  1264. (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
  1265. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
  1266. "queued=%d\n",list_empty(&dev->vidq.queued));
  1267. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
  1268. "active=%d\n",list_empty(&dev->vidq.active));
  1269. /* initialize hardware on analog mode */
  1270. rc = tm6000_init_analog_mode(dev);
  1271. if (rc < 0)
  1272. return rc;
  1273. if (dev->mode != TM6000_MODE_ANALOG) {
  1274. /* Put all controls at a sane state */
  1275. for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
  1276. qctl_regs[i] = tm6000_qctrl[i].default_value;
  1277. dev->mode = TM6000_MODE_ANALOG;
  1278. }
  1279. videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
  1280. NULL, &dev->slock,
  1281. fh->type,
  1282. V4L2_FIELD_INTERLACED,
  1283. sizeof(struct tm6000_buffer), fh, &dev->lock);
  1284. if (fh->radio) {
  1285. dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
  1286. dev->input = 5;
  1287. tm6000_set_audio_rinput(dev);
  1288. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1289. tm6000_prepare_isoc(dev);
  1290. tm6000_start_thread(dev);
  1291. }
  1292. return 0;
  1293. }
  1294. static ssize_t
  1295. tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
  1296. {
  1297. struct tm6000_fh *fh = file->private_data;
  1298. if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1299. if (!res_get(fh->dev, fh, true))
  1300. return -EBUSY;
  1301. return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
  1302. file->f_flags & O_NONBLOCK);
  1303. }
  1304. return 0;
  1305. }
  1306. static unsigned int
  1307. tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1308. {
  1309. struct tm6000_fh *fh = file->private_data;
  1310. struct tm6000_buffer *buf;
  1311. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1312. return POLLERR;
  1313. if (!!is_res_streaming(fh->dev, fh))
  1314. return POLLERR;
  1315. if (!is_res_read(fh->dev, fh)) {
  1316. /* streaming capture */
  1317. if (list_empty(&fh->vb_vidq.stream))
  1318. return POLLERR;
  1319. buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
  1320. } else {
  1321. /* read() capture */
  1322. return videobuf_poll_stream(file, &fh->vb_vidq,
  1323. wait);
  1324. }
  1325. poll_wait(file, &buf->vb.done, wait);
  1326. if (buf->vb.state == VIDEOBUF_DONE ||
  1327. buf->vb.state == VIDEOBUF_ERROR)
  1328. return POLLIN | POLLRDNORM;
  1329. return 0;
  1330. }
  1331. static int tm6000_release(struct file *file)
  1332. {
  1333. struct tm6000_fh *fh = file->private_data;
  1334. struct tm6000_core *dev = fh->dev;
  1335. struct video_device *vdev = video_devdata(file);
  1336. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
  1337. video_device_node_name(vdev), dev->users);
  1338. dev->users--;
  1339. res_free(dev, fh);
  1340. if (!dev->users) {
  1341. tm6000_uninit_isoc(dev);
  1342. videobuf_mmap_free(&fh->vb_vidq);
  1343. }
  1344. kfree(fh);
  1345. return 0;
  1346. }
  1347. static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
  1348. {
  1349. struct tm6000_fh *fh = file->private_data;
  1350. int ret;
  1351. ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1352. return ret;
  1353. }
  1354. static struct v4l2_file_operations tm6000_fops = {
  1355. .owner = THIS_MODULE,
  1356. .open = tm6000_open,
  1357. .release = tm6000_release,
  1358. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1359. .read = tm6000_read,
  1360. .poll = tm6000_poll,
  1361. .mmap = tm6000_mmap,
  1362. };
  1363. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1364. .vidioc_querycap = vidioc_querycap,
  1365. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1366. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1367. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1368. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1369. .vidioc_s_std = vidioc_s_std,
  1370. .vidioc_enum_input = vidioc_enum_input,
  1371. .vidioc_g_input = vidioc_g_input,
  1372. .vidioc_s_input = vidioc_s_input,
  1373. .vidioc_queryctrl = vidioc_queryctrl,
  1374. .vidioc_g_ctrl = vidioc_g_ctrl,
  1375. .vidioc_s_ctrl = vidioc_s_ctrl,
  1376. .vidioc_g_tuner = vidioc_g_tuner,
  1377. .vidioc_s_tuner = vidioc_s_tuner,
  1378. .vidioc_g_frequency = vidioc_g_frequency,
  1379. .vidioc_s_frequency = vidioc_s_frequency,
  1380. .vidioc_streamon = vidioc_streamon,
  1381. .vidioc_streamoff = vidioc_streamoff,
  1382. .vidioc_reqbufs = vidioc_reqbufs,
  1383. .vidioc_querybuf = vidioc_querybuf,
  1384. .vidioc_qbuf = vidioc_qbuf,
  1385. .vidioc_dqbuf = vidioc_dqbuf,
  1386. };
  1387. static struct video_device tm6000_template = {
  1388. .name = "tm6000",
  1389. .fops = &tm6000_fops,
  1390. .ioctl_ops = &video_ioctl_ops,
  1391. .release = video_device_release,
  1392. .tvnorms = TM6000_STD,
  1393. .current_norm = V4L2_STD_NTSC_M,
  1394. };
  1395. static const struct v4l2_file_operations radio_fops = {
  1396. .owner = THIS_MODULE,
  1397. .open = tm6000_open,
  1398. .release = tm6000_release,
  1399. .unlocked_ioctl = video_ioctl2,
  1400. };
  1401. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1402. .vidioc_querycap = radio_querycap,
  1403. .vidioc_g_tuner = radio_g_tuner,
  1404. .vidioc_enum_input = radio_enum_input,
  1405. .vidioc_g_audio = radio_g_audio,
  1406. .vidioc_s_tuner = radio_s_tuner,
  1407. .vidioc_s_audio = radio_s_audio,
  1408. .vidioc_s_input = radio_s_input,
  1409. .vidioc_s_std = radio_s_std,
  1410. .vidioc_queryctrl = radio_queryctrl,
  1411. .vidioc_g_input = radio_g_input,
  1412. .vidioc_g_ctrl = vidioc_g_ctrl,
  1413. .vidioc_s_ctrl = vidioc_s_ctrl,
  1414. .vidioc_g_frequency = vidioc_g_frequency,
  1415. .vidioc_s_frequency = vidioc_s_frequency,
  1416. };
  1417. struct video_device tm6000_radio_template = {
  1418. .name = "tm6000",
  1419. .fops = &radio_fops,
  1420. .ioctl_ops = &radio_ioctl_ops,
  1421. };
  1422. /* -----------------------------------------------------------------
  1423. * Initialization and module stuff
  1424. * ------------------------------------------------------------------
  1425. */
  1426. static struct video_device *vdev_init(struct tm6000_core *dev,
  1427. const struct video_device
  1428. *template, const char *type_name)
  1429. {
  1430. struct video_device *vfd;
  1431. vfd = video_device_alloc();
  1432. if (NULL == vfd)
  1433. return NULL;
  1434. *vfd = *template;
  1435. vfd->v4l2_dev = &dev->v4l2_dev;
  1436. vfd->release = video_device_release;
  1437. vfd->debug = tm6000_debug;
  1438. vfd->lock = &dev->lock;
  1439. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1440. video_set_drvdata(vfd, dev);
  1441. return vfd;
  1442. }
  1443. int tm6000_v4l2_register(struct tm6000_core *dev)
  1444. {
  1445. int ret = -1;
  1446. dev->vfd = vdev_init(dev, &tm6000_template, "video");
  1447. if (!dev->vfd) {
  1448. printk(KERN_INFO "%s: can't register video device\n",
  1449. dev->name);
  1450. return -ENOMEM;
  1451. }
  1452. /* init video dma queues */
  1453. INIT_LIST_HEAD(&dev->vidq.active);
  1454. INIT_LIST_HEAD(&dev->vidq.queued);
  1455. ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
  1456. if (ret < 0) {
  1457. printk(KERN_INFO "%s: can't register video device\n",
  1458. dev->name);
  1459. return ret;
  1460. }
  1461. printk(KERN_INFO "%s: registered device %s\n",
  1462. dev->name, video_device_node_name(dev->vfd));
  1463. if (dev->caps.has_radio) {
  1464. dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
  1465. "radio");
  1466. if (!dev->radio_dev) {
  1467. printk(KERN_INFO "%s: can't register radio device\n",
  1468. dev->name);
  1469. return ret; /* FIXME release resource */
  1470. }
  1471. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1472. radio_nr);
  1473. if (ret < 0) {
  1474. printk(KERN_INFO "%s: can't register radio device\n",
  1475. dev->name);
  1476. return ret; /* FIXME release resource */
  1477. }
  1478. printk(KERN_INFO "%s: registered device %s\n",
  1479. dev->name, video_device_node_name(dev->radio_dev));
  1480. }
  1481. printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
  1482. return ret;
  1483. }
  1484. int tm6000_v4l2_unregister(struct tm6000_core *dev)
  1485. {
  1486. video_unregister_device(dev->vfd);
  1487. if (dev->radio_dev) {
  1488. if (video_is_registered(dev->radio_dev))
  1489. video_unregister_device(dev->radio_dev);
  1490. else
  1491. video_device_release(dev->radio_dev);
  1492. dev->radio_dev = NULL;
  1493. }
  1494. return 0;
  1495. }
  1496. int tm6000_v4l2_exit(void)
  1497. {
  1498. return 0;
  1499. }
  1500. module_param(video_nr, int, 0);
  1501. MODULE_PARM_DESC(video_nr, "Allow changing video device number");
  1502. module_param_named(debug, tm6000_debug, int, 0444);
  1503. MODULE_PARM_DESC(debug, "activates debug info");
  1504. module_param(vid_limit, int, 0644);
  1505. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");