saa7134-empress.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 (atomic_read(&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. atomic_inc(&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. videobuf_stop(&dev->empress_tsq);
  91. videobuf_mmap_free(&dev->empress_tsq);
  92. /* stop the encoder */
  93. ts_reset_encoder(dev);
  94. /* Mute audio */
  95. saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
  96. saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
  97. atomic_dec(&dev->empress_users);
  98. return 0;
  99. }
  100. static ssize_t
  101. ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  102. {
  103. struct saa7134_dev *dev = file->private_data;
  104. if (!dev->empress_started)
  105. ts_init_encoder(dev);
  106. return videobuf_read_stream(&dev->empress_tsq,
  107. data, count, ppos, 0,
  108. file->f_flags & O_NONBLOCK);
  109. }
  110. static unsigned int
  111. ts_poll(struct file *file, struct poll_table_struct *wait)
  112. {
  113. struct saa7134_dev *dev = file->private_data;
  114. return videobuf_poll_stream(file, &dev->empress_tsq, wait);
  115. }
  116. static int
  117. ts_mmap(struct file *file, struct vm_area_struct * vma)
  118. {
  119. struct saa7134_dev *dev = file->private_data;
  120. return videobuf_mmap_mapper(&dev->empress_tsq, vma);
  121. }
  122. /*
  123. * This function is _not_ called directly, but from
  124. * video_generic_ioctl (and maybe others). userspace
  125. * copying is done already, arg is a kernel pointer.
  126. */
  127. static int empress_querycap(struct file *file, void *priv,
  128. struct v4l2_capability *cap)
  129. {
  130. struct saa7134_dev *dev = file->private_data;
  131. strcpy(cap->driver, "saa7134");
  132. strlcpy(cap->card, saa7134_boards[dev->board].name,
  133. sizeof(cap->card));
  134. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  135. cap->version = SAA7134_VERSION_CODE;
  136. cap->capabilities =
  137. V4L2_CAP_VIDEO_CAPTURE |
  138. V4L2_CAP_READWRITE |
  139. V4L2_CAP_STREAMING;
  140. return 0;
  141. }
  142. static int empress_enum_input(struct file *file, void *priv,
  143. struct v4l2_input *i)
  144. {
  145. if (i->index != 0)
  146. return -EINVAL;
  147. i->type = V4L2_INPUT_TYPE_CAMERA;
  148. strcpy(i->name, "CCIR656");
  149. return 0;
  150. }
  151. static int empress_g_input(struct file *file, void *priv, unsigned int *i)
  152. {
  153. *i = 0;
  154. return 0;
  155. }
  156. static int empress_s_input(struct file *file, void *priv, unsigned int i)
  157. {
  158. if (i != 0)
  159. return -EINVAL;
  160. return 0;
  161. }
  162. static int empress_enum_fmt_vid_cap(struct file *file, void *priv,
  163. struct v4l2_fmtdesc *f)
  164. {
  165. if (f->index != 0)
  166. return -EINVAL;
  167. strlcpy(f->description, "MPEG TS", sizeof(f->description));
  168. f->pixelformat = V4L2_PIX_FMT_MPEG;
  169. return 0;
  170. }
  171. static int empress_g_fmt_vid_cap(struct file *file, void *priv,
  172. struct v4l2_format *f)
  173. {
  174. struct saa7134_dev *dev = file->private_data;
  175. saa7134_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  176. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  177. f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
  178. return 0;
  179. }
  180. static int empress_s_fmt_vid_cap(struct file *file, void *priv,
  181. struct v4l2_format *f)
  182. {
  183. struct saa7134_dev *dev = file->private_data;
  184. saa7134_i2c_call_clients(dev, VIDIOC_S_FMT, f);
  185. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  186. f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
  187. return 0;
  188. }
  189. static int empress_reqbufs(struct file *file, void *priv,
  190. struct v4l2_requestbuffers *p)
  191. {
  192. struct saa7134_dev *dev = file->private_data;
  193. return videobuf_reqbufs(&dev->empress_tsq, p);
  194. }
  195. static int empress_querybuf(struct file *file, void *priv,
  196. struct v4l2_buffer *b)
  197. {
  198. struct saa7134_dev *dev = file->private_data;
  199. return videobuf_querybuf(&dev->empress_tsq, b);
  200. }
  201. static int empress_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  202. {
  203. struct saa7134_dev *dev = file->private_data;
  204. return videobuf_qbuf(&dev->empress_tsq, b);
  205. }
  206. static int empress_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  207. {
  208. struct saa7134_dev *dev = file->private_data;
  209. return videobuf_dqbuf(&dev->empress_tsq, b,
  210. file->f_flags & O_NONBLOCK);
  211. }
  212. static int empress_streamon(struct file *file, void *priv,
  213. enum v4l2_buf_type type)
  214. {
  215. struct saa7134_dev *dev = file->private_data;
  216. return videobuf_streamon(&dev->empress_tsq);
  217. }
  218. static int empress_streamoff(struct file *file, void *priv,
  219. enum v4l2_buf_type type)
  220. {
  221. struct saa7134_dev *dev = file->private_data;
  222. return videobuf_streamoff(&dev->empress_tsq);
  223. }
  224. static int saa7134_i2c_call_saa6752(struct saa7134_dev *dev,
  225. unsigned int cmd, void *arg)
  226. {
  227. if (dev->mpeg_i2c_client == NULL)
  228. return -EINVAL;
  229. return dev->mpeg_i2c_client->driver->command(dev->mpeg_i2c_client,
  230. cmd, arg);
  231. }
  232. static int empress_s_ext_ctrls(struct file *file, void *priv,
  233. struct v4l2_ext_controls *ctrls)
  234. {
  235. struct saa7134_dev *dev = file->private_data;
  236. int err;
  237. /* count == 0 is abused in saa6752hs.c, so that special
  238. case is handled here explicitly. */
  239. if (ctrls->count == 0)
  240. return 0;
  241. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  242. return -EINVAL;
  243. err = saa7134_i2c_call_saa6752(dev, VIDIOC_S_EXT_CTRLS, ctrls);
  244. ts_init_encoder(dev);
  245. return err;
  246. }
  247. static int empress_g_ext_ctrls(struct file *file, void *priv,
  248. struct v4l2_ext_controls *ctrls)
  249. {
  250. struct saa7134_dev *dev = file->private_data;
  251. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  252. return -EINVAL;
  253. return saa7134_i2c_call_saa6752(dev, VIDIOC_G_EXT_CTRLS, ctrls);
  254. }
  255. static int empress_queryctrl(struct file *file, void *priv,
  256. struct v4l2_queryctrl *c)
  257. {
  258. static const u32 user_ctrls[] = {
  259. V4L2_CID_USER_CLASS,
  260. V4L2_CID_BRIGHTNESS,
  261. V4L2_CID_CONTRAST,
  262. V4L2_CID_SATURATION,
  263. V4L2_CID_HUE,
  264. V4L2_CID_AUDIO_VOLUME,
  265. V4L2_CID_AUDIO_MUTE,
  266. V4L2_CID_HFLIP,
  267. 0
  268. };
  269. static const u32 mpeg_ctrls[] = {
  270. V4L2_CID_MPEG_CLASS,
  271. V4L2_CID_MPEG_STREAM_TYPE,
  272. V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
  273. V4L2_CID_MPEG_AUDIO_ENCODING,
  274. V4L2_CID_MPEG_AUDIO_L2_BITRATE,
  275. V4L2_CID_MPEG_VIDEO_ENCODING,
  276. V4L2_CID_MPEG_VIDEO_ASPECT,
  277. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  278. V4L2_CID_MPEG_VIDEO_BITRATE,
  279. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  280. 0
  281. };
  282. static const u32 *ctrl_classes[] = {
  283. user_ctrls,
  284. mpeg_ctrls,
  285. NULL
  286. };
  287. struct saa7134_dev *dev = file->private_data;
  288. c->id = v4l2_ctrl_next(ctrl_classes, c->id);
  289. if (c->id == 0)
  290. return -EINVAL;
  291. if (c->id == V4L2_CID_USER_CLASS || c->id == V4L2_CID_MPEG_CLASS)
  292. return v4l2_ctrl_query_fill_std(c);
  293. if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
  294. return saa7134_queryctrl(file, priv, c);
  295. return saa7134_i2c_call_saa6752(dev, VIDIOC_QUERYCTRL, c);
  296. }
  297. static int empress_querymenu(struct file *file, void *priv,
  298. struct v4l2_querymenu *c)
  299. {
  300. struct saa7134_dev *dev = file->private_data;
  301. if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
  302. return -EINVAL;
  303. return saa7134_i2c_call_saa6752(dev, VIDIOC_QUERYMENU, c);
  304. }
  305. static const struct file_operations ts_fops =
  306. {
  307. .owner = THIS_MODULE,
  308. .open = ts_open,
  309. .release = ts_release,
  310. .read = ts_read,
  311. .poll = ts_poll,
  312. .mmap = ts_mmap,
  313. .ioctl = video_ioctl2,
  314. .llseek = no_llseek,
  315. };
  316. static const struct v4l2_ioctl_ops ts_ioctl_ops = {
  317. .vidioc_querycap = empress_querycap,
  318. .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap,
  319. .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap,
  320. .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap,
  321. .vidioc_reqbufs = empress_reqbufs,
  322. .vidioc_querybuf = empress_querybuf,
  323. .vidioc_qbuf = empress_qbuf,
  324. .vidioc_dqbuf = empress_dqbuf,
  325. .vidioc_streamon = empress_streamon,
  326. .vidioc_streamoff = empress_streamoff,
  327. .vidioc_s_ext_ctrls = empress_s_ext_ctrls,
  328. .vidioc_g_ext_ctrls = empress_g_ext_ctrls,
  329. .vidioc_enum_input = empress_enum_input,
  330. .vidioc_g_input = empress_g_input,
  331. .vidioc_s_input = empress_s_input,
  332. .vidioc_queryctrl = empress_queryctrl,
  333. .vidioc_querymenu = empress_querymenu,
  334. .vidioc_g_ctrl = saa7134_g_ctrl,
  335. .vidioc_s_ctrl = saa7134_s_ctrl,
  336. };
  337. /* ----------------------------------------------------------- */
  338. static struct video_device saa7134_empress_template = {
  339. .name = "saa7134-empress",
  340. .type = 0 /* FIXME */,
  341. .type2 = 0 /* FIXME */,
  342. .fops = &ts_fops,
  343. .minor = -1,
  344. .ioctl_ops = &ts_ioctl_ops,
  345. .tvnorms = SAA7134_NORMS,
  346. .current_norm = V4L2_STD_PAL,
  347. };
  348. static void empress_signal_update(struct work_struct *work)
  349. {
  350. struct saa7134_dev* dev =
  351. container_of(work, struct saa7134_dev, empress_workqueue);
  352. if (dev->nosignal) {
  353. dprintk("no video signal\n");
  354. ts_reset_encoder(dev);
  355. } else {
  356. dprintk("video signal acquired\n");
  357. if (atomic_read(&dev->empress_users))
  358. ts_init_encoder(dev);
  359. }
  360. }
  361. static void empress_signal_change(struct saa7134_dev *dev)
  362. {
  363. schedule_work(&dev->empress_workqueue);
  364. }
  365. static int empress_init(struct saa7134_dev *dev)
  366. {
  367. int err;
  368. dprintk("%s: %s\n",dev->name,__func__);
  369. dev->empress_dev = video_device_alloc();
  370. if (NULL == dev->empress_dev)
  371. return -ENOMEM;
  372. *(dev->empress_dev) = saa7134_empress_template;
  373. dev->empress_dev->parent = &dev->pci->dev;
  374. dev->empress_dev->release = video_device_release;
  375. snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
  376. "%s empress (%s)", dev->name,
  377. saa7134_boards[dev->board].name);
  378. INIT_WORK(&dev->empress_workqueue, empress_signal_update);
  379. err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
  380. empress_nr[dev->nr]);
  381. if (err < 0) {
  382. printk(KERN_INFO "%s: can't register video device\n",
  383. dev->name);
  384. video_device_release(dev->empress_dev);
  385. dev->empress_dev = NULL;
  386. return err;
  387. }
  388. printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
  389. dev->name,dev->empress_dev->minor & 0x1f);
  390. videobuf_queue_sg_init(&dev->empress_tsq, &saa7134_ts_qops,
  391. &dev->pci->dev, &dev->slock,
  392. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  393. V4L2_FIELD_ALTERNATE,
  394. sizeof(struct saa7134_buf),
  395. dev);
  396. empress_signal_update(&dev->empress_workqueue);
  397. return 0;
  398. }
  399. static int empress_fini(struct saa7134_dev *dev)
  400. {
  401. dprintk("%s: %s\n",dev->name,__func__);
  402. if (NULL == dev->empress_dev)
  403. return 0;
  404. flush_scheduled_work();
  405. video_unregister_device(dev->empress_dev);
  406. dev->empress_dev = NULL;
  407. return 0;
  408. }
  409. static struct saa7134_mpeg_ops empress_ops = {
  410. .type = SAA7134_MPEG_EMPRESS,
  411. .init = empress_init,
  412. .fini = empress_fini,
  413. .signal_change = empress_signal_change,
  414. };
  415. static int __init empress_register(void)
  416. {
  417. return saa7134_ts_register(&empress_ops);
  418. }
  419. static void __exit empress_unregister(void)
  420. {
  421. saa7134_ts_unregister(&empress_ops);
  422. }
  423. module_init(empress_register);
  424. module_exit(empress_unregister);
  425. /* ----------------------------------------------------------- */
  426. /*
  427. * Local variables:
  428. * c-basic-offset: 8
  429. * End:
  430. */