saa7134-empress.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. *
  3. * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #include "saa7134-reg.h"
  26. #include "saa7134.h"
  27. #include <media/saa6752hs.h>
  28. #include <media/v4l2-common.h>
  29. /* ------------------------------------------------------------------ */
  30. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  31. MODULE_LICENSE("GPL");
  32. static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
  33. module_param_array(empress_nr, int, NULL, 0444);
  34. MODULE_PARM_DESC(empress_nr,"ts device number");
  35. static unsigned int debug;
  36. module_param(debug, int, 0644);
  37. MODULE_PARM_DESC(debug,"enable debug messages");
  38. #define dprintk(fmt, arg...) if (debug) \
  39. printk(KERN_DEBUG "%s/empress: " fmt, dev->name , ## arg)
  40. /* ------------------------------------------------------------------ */
  41. static void ts_reset_encoder(struct saa7134_dev* dev)
  42. {
  43. if (!dev->empress_started)
  44. return;
  45. saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
  46. msleep(10);
  47. saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
  48. msleep(100);
  49. dev->empress_started = 0;
  50. }
  51. static int ts_init_encoder(struct saa7134_dev* dev)
  52. {
  53. struct v4l2_ext_controls ctrls = { V4L2_CTRL_CLASS_MPEG, 0 };
  54. ts_reset_encoder(dev);
  55. saa7134_i2c_call_clients(dev, VIDIOC_S_EXT_CTRLS, &ctrls);
  56. dev->empress_started = 1;
  57. return 0;
  58. }
  59. /* ------------------------------------------------------------------ */
  60. static int ts_open(struct inode *inode, struct file *file)
  61. {
  62. int minor = iminor(inode);
  63. struct saa7134_dev *dev;
  64. int err;
  65. list_for_each_entry(dev, &saa7134_devlist, devlist)
  66. if (dev->empress_dev && dev->empress_dev->minor == minor)
  67. goto found;
  68. return -ENODEV;
  69. found:
  70. dprintk("open minor=%d\n",minor);
  71. err = -EBUSY;
  72. if (!mutex_trylock(&dev->empress_tsq.vb_lock))
  73. goto done;
  74. if (dev->empress_users)
  75. goto done_up;
  76. /* Unmute audio */
  77. saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
  78. saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
  79. dev->empress_users++;
  80. file->private_data = dev;
  81. err = 0;
  82. done_up:
  83. mutex_unlock(&dev->empress_tsq.vb_lock);
  84. done:
  85. return err;
  86. }
  87. static int ts_release(struct inode *inode, struct file *file)
  88. {
  89. struct saa7134_dev *dev = file->private_data;
  90. mutex_lock(&dev->empress_tsq.vb_lock);
  91. videobuf_stop(&dev->empress_tsq);
  92. videobuf_mmap_free(&dev->empress_tsq);
  93. /* stop the encoder */
  94. ts_reset_encoder(dev);
  95. /* Mute audio */
  96. saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
  97. saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
  98. dev->empress_users--;
  99. mutex_unlock(&dev->empress_tsq.vb_lock);
  100. return 0;
  101. }
  102. static ssize_t
  103. ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  104. {
  105. struct saa7134_dev *dev = file->private_data;
  106. if (!dev->empress_started)
  107. ts_init_encoder(dev);
  108. return videobuf_read_stream(&dev->empress_tsq,
  109. data, count, ppos, 0,
  110. file->f_flags & O_NONBLOCK);
  111. }
  112. static unsigned int
  113. ts_poll(struct file *file, struct poll_table_struct *wait)
  114. {
  115. struct saa7134_dev *dev = file->private_data;
  116. return videobuf_poll_stream(file, &dev->empress_tsq, wait);
  117. }
  118. static int
  119. ts_mmap(struct file *file, struct vm_area_struct * vma)
  120. {
  121. struct saa7134_dev *dev = file->private_data;
  122. return videobuf_mmap_mapper(&dev->empress_tsq, vma);
  123. }
  124. /*
  125. * This function is _not_ called directly, but from
  126. * video_generic_ioctl (and maybe others). userspace
  127. * copying is done already, arg is a kernel pointer.
  128. */
  129. static int empress_querycap(struct file *file, void *priv,
  130. struct v4l2_capability *cap)
  131. {
  132. struct saa7134_dev *dev = file->private_data;
  133. strcpy(cap->driver, "saa7134");
  134. strlcpy(cap->card, saa7134_boards[dev->board].name,
  135. sizeof(cap->card));
  136. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  137. cap->version = SAA7134_VERSION_CODE;
  138. cap->capabilities =
  139. V4L2_CAP_VIDEO_CAPTURE |
  140. V4L2_CAP_READWRITE |
  141. V4L2_CAP_STREAMING;
  142. return 0;
  143. }
  144. static int empress_enum_input(struct file *file, void *priv,
  145. struct v4l2_input *i)
  146. {
  147. if (i->index != 0)
  148. return -EINVAL;
  149. i->type = V4L2_INPUT_TYPE_CAMERA;
  150. strcpy(i->name, "CCIR656");
  151. return 0;
  152. }
  153. static int empress_g_input(struct file *file, void *priv, unsigned int *i)
  154. {
  155. *i = 0;
  156. return 0;
  157. }
  158. static int empress_s_input(struct file *file, void *priv, unsigned int i)
  159. {
  160. if (i != 0)
  161. return -EINVAL;
  162. return 0;
  163. }
  164. static int empress_enum_fmt_cap(struct file *file, void *priv,
  165. struct v4l2_fmtdesc *f)
  166. {
  167. if (f->index != 0)
  168. return -EINVAL;
  169. strlcpy(f->description, "MPEG TS", sizeof(f->description));
  170. f->pixelformat = V4L2_PIX_FMT_MPEG;
  171. return 0;
  172. }
  173. static int empress_g_fmt_cap(struct file *file, void *priv,
  174. struct v4l2_format *f)
  175. {
  176. struct saa7134_dev *dev = file->private_data;
  177. saa7134_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  178. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  179. f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
  180. return 0;
  181. }
  182. static int empress_s_fmt_cap(struct file *file, void *priv,
  183. struct v4l2_format *f)
  184. {
  185. struct saa7134_dev *dev = file->private_data;
  186. saa7134_i2c_call_clients(dev, VIDIOC_S_FMT, f);
  187. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  188. f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
  189. return 0;
  190. }
  191. static int empress_reqbufs(struct file *file, void *priv,
  192. struct v4l2_requestbuffers *p)
  193. {
  194. struct saa7134_dev *dev = file->private_data;
  195. return videobuf_reqbufs(&dev->empress_tsq, p);
  196. }
  197. static int empress_querybuf(struct file *file, void *priv,
  198. struct v4l2_buffer *b)
  199. {
  200. struct saa7134_dev *dev = file->private_data;
  201. return videobuf_querybuf(&dev->empress_tsq, b);
  202. }
  203. static int empress_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  204. {
  205. struct saa7134_dev *dev = file->private_data;
  206. return videobuf_qbuf(&dev->empress_tsq, b);
  207. }
  208. static int empress_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  209. {
  210. struct saa7134_dev *dev = file->private_data;
  211. return videobuf_dqbuf(&dev->empress_tsq, b,
  212. file->f_flags & O_NONBLOCK);
  213. }
  214. static int empress_streamon(struct file *file, void *priv,
  215. enum v4l2_buf_type type)
  216. {
  217. struct saa7134_dev *dev = file->private_data;
  218. return videobuf_streamon(&dev->empress_tsq);
  219. }
  220. static int empress_streamoff(struct file *file, void *priv,
  221. enum v4l2_buf_type type)
  222. {
  223. struct saa7134_dev *dev = file->private_data;
  224. return videobuf_streamoff(&dev->empress_tsq);
  225. }
  226. static int empress_s_ext_ctrls(struct file *file, void *priv,
  227. struct v4l2_ext_controls *ctrls)
  228. {
  229. struct saa7134_dev *dev = file->private_data;
  230. /* count == 0 is abused in saa6752hs.c, so that special
  231. case is handled here explicitly. */
  232. if (ctrls->count == 0)
  233. return 0;
  234. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  235. return -EINVAL;
  236. saa7134_i2c_call_clients(dev, VIDIOC_S_EXT_CTRLS, ctrls);
  237. ts_init_encoder(dev);
  238. return 0;
  239. }
  240. static int empress_g_ext_ctrls(struct file *file, void *priv,
  241. struct v4l2_ext_controls *ctrls)
  242. {
  243. struct saa7134_dev *dev = file->private_data;
  244. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  245. return -EINVAL;
  246. saa7134_i2c_call_clients(dev, VIDIOC_G_EXT_CTRLS, ctrls);
  247. return 0;
  248. }
  249. static const struct file_operations ts_fops =
  250. {
  251. .owner = THIS_MODULE,
  252. .open = ts_open,
  253. .release = ts_release,
  254. .read = ts_read,
  255. .poll = ts_poll,
  256. .mmap = ts_mmap,
  257. .ioctl = video_ioctl2,
  258. .llseek = no_llseek,
  259. };
  260. /* ----------------------------------------------------------- */
  261. static struct video_device saa7134_empress_template =
  262. {
  263. .name = "saa7134-empress",
  264. .type = 0 /* FIXME */,
  265. .type2 = 0 /* FIXME */,
  266. .fops = &ts_fops,
  267. .minor = -1,
  268. .vidioc_querycap = empress_querycap,
  269. .vidioc_enum_fmt_cap = empress_enum_fmt_cap,
  270. .vidioc_s_fmt_cap = empress_s_fmt_cap,
  271. .vidioc_g_fmt_cap = empress_g_fmt_cap,
  272. .vidioc_reqbufs = empress_reqbufs,
  273. .vidioc_querybuf = empress_querybuf,
  274. .vidioc_qbuf = empress_qbuf,
  275. .vidioc_dqbuf = empress_dqbuf,
  276. .vidioc_streamon = empress_streamon,
  277. .vidioc_streamoff = empress_streamoff,
  278. .vidioc_s_ext_ctrls = empress_s_ext_ctrls,
  279. .vidioc_g_ext_ctrls = empress_g_ext_ctrls,
  280. .vidioc_enum_input = empress_enum_input,
  281. .vidioc_g_input = empress_g_input,
  282. .vidioc_s_input = empress_s_input,
  283. .vidioc_queryctrl = saa7134_queryctrl,
  284. .vidioc_g_ctrl = saa7134_g_ctrl,
  285. .vidioc_s_ctrl = saa7134_s_ctrl,
  286. .tvnorms = SAA7134_NORMS,
  287. .current_norm = V4L2_STD_PAL,
  288. };
  289. static void empress_signal_update(struct work_struct *work)
  290. {
  291. struct saa7134_dev* dev =
  292. container_of(work, struct saa7134_dev, empress_workqueue);
  293. if (dev->nosignal) {
  294. dprintk("no video signal\n");
  295. ts_reset_encoder(dev);
  296. } else {
  297. dprintk("video signal acquired\n");
  298. if (dev->empress_users)
  299. ts_init_encoder(dev);
  300. }
  301. }
  302. static void empress_signal_change(struct saa7134_dev *dev)
  303. {
  304. schedule_work(&dev->empress_workqueue);
  305. }
  306. static int empress_init(struct saa7134_dev *dev)
  307. {
  308. int err;
  309. dprintk("%s: %s\n",dev->name,__func__);
  310. dev->empress_dev = video_device_alloc();
  311. if (NULL == dev->empress_dev)
  312. return -ENOMEM;
  313. *(dev->empress_dev) = saa7134_empress_template;
  314. dev->empress_dev->dev = &dev->pci->dev;
  315. dev->empress_dev->release = video_device_release;
  316. snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
  317. "%s empress (%s)", dev->name,
  318. saa7134_boards[dev->board].name);
  319. INIT_WORK(&dev->empress_workqueue, empress_signal_update);
  320. err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
  321. empress_nr[dev->nr]);
  322. if (err < 0) {
  323. printk(KERN_INFO "%s: can't register video device\n",
  324. dev->name);
  325. video_device_release(dev->empress_dev);
  326. dev->empress_dev = NULL;
  327. return err;
  328. }
  329. printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
  330. dev->name,dev->empress_dev->minor & 0x1f);
  331. videobuf_queue_sg_init(&dev->empress_tsq, &saa7134_ts_qops,
  332. &dev->pci->dev, &dev->slock,
  333. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  334. V4L2_FIELD_ALTERNATE,
  335. sizeof(struct saa7134_buf),
  336. dev);
  337. empress_signal_update(&dev->empress_workqueue);
  338. return 0;
  339. }
  340. static int empress_fini(struct saa7134_dev *dev)
  341. {
  342. dprintk("%s: %s\n",dev->name,__func__);
  343. if (NULL == dev->empress_dev)
  344. return 0;
  345. flush_scheduled_work();
  346. video_unregister_device(dev->empress_dev);
  347. dev->empress_dev = NULL;
  348. return 0;
  349. }
  350. static struct saa7134_mpeg_ops empress_ops = {
  351. .type = SAA7134_MPEG_EMPRESS,
  352. .init = empress_init,
  353. .fini = empress_fini,
  354. .signal_change = empress_signal_change,
  355. };
  356. static int __init empress_register(void)
  357. {
  358. return saa7134_ts_register(&empress_ops);
  359. }
  360. static void __exit empress_unregister(void)
  361. {
  362. saa7134_ts_unregister(&empress_ops);
  363. }
  364. module_init(empress_register);
  365. module_exit(empress_unregister);
  366. /* ----------------------------------------------------------- */
  367. /*
  368. * Local variables:
  369. * c-basic-offset: 8
  370. * End:
  371. */