saa7134-empress.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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_vid_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_vid_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_vid_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 saa7134_i2c_call_saa6752(struct saa7134_dev *dev,
  227. unsigned int cmd, void *arg)
  228. {
  229. if (dev->mpeg_i2c_client == NULL)
  230. return -EINVAL;
  231. return dev->mpeg_i2c_client->driver->command(dev->mpeg_i2c_client,
  232. cmd, arg);
  233. }
  234. static int empress_s_ext_ctrls(struct file *file, void *priv,
  235. struct v4l2_ext_controls *ctrls)
  236. {
  237. struct saa7134_dev *dev = file->private_data;
  238. int err;
  239. /* count == 0 is abused in saa6752hs.c, so that special
  240. case is handled here explicitly. */
  241. if (ctrls->count == 0)
  242. return 0;
  243. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  244. return -EINVAL;
  245. err = saa7134_i2c_call_saa6752(dev, VIDIOC_S_EXT_CTRLS, ctrls);
  246. ts_init_encoder(dev);
  247. return err;
  248. }
  249. static int empress_g_ext_ctrls(struct file *file, void *priv,
  250. struct v4l2_ext_controls *ctrls)
  251. {
  252. struct saa7134_dev *dev = file->private_data;
  253. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  254. return -EINVAL;
  255. return saa7134_i2c_call_saa6752(dev, VIDIOC_G_EXT_CTRLS, ctrls);
  256. }
  257. static int empress_queryctrl(struct file *file, void *priv,
  258. struct v4l2_queryctrl *c)
  259. {
  260. static const u32 user_ctrls[] = {
  261. V4L2_CID_USER_CLASS,
  262. V4L2_CID_BRIGHTNESS,
  263. V4L2_CID_CONTRAST,
  264. V4L2_CID_SATURATION,
  265. V4L2_CID_HUE,
  266. V4L2_CID_AUDIO_VOLUME,
  267. V4L2_CID_AUDIO_MUTE,
  268. V4L2_CID_HFLIP,
  269. 0
  270. };
  271. static const u32 mpeg_ctrls[] = {
  272. V4L2_CID_MPEG_CLASS,
  273. V4L2_CID_MPEG_STREAM_TYPE,
  274. V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
  275. V4L2_CID_MPEG_AUDIO_ENCODING,
  276. V4L2_CID_MPEG_AUDIO_L2_BITRATE,
  277. V4L2_CID_MPEG_VIDEO_ENCODING,
  278. V4L2_CID_MPEG_VIDEO_ASPECT,
  279. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  280. V4L2_CID_MPEG_VIDEO_BITRATE,
  281. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  282. 0
  283. };
  284. static const u32 *ctrl_classes[] = {
  285. user_ctrls,
  286. mpeg_ctrls,
  287. NULL
  288. };
  289. struct saa7134_dev *dev = file->private_data;
  290. c->id = v4l2_ctrl_next(ctrl_classes, c->id);
  291. if (c->id == 0)
  292. return -EINVAL;
  293. if (c->id == V4L2_CID_USER_CLASS || c->id == V4L2_CID_MPEG_CLASS)
  294. return v4l2_ctrl_query_fill_std(c);
  295. if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
  296. return saa7134_queryctrl(file, priv, c);
  297. return saa7134_i2c_call_saa6752(dev, VIDIOC_QUERYCTRL, c);
  298. }
  299. static int empress_querymenu(struct file *file, void *priv,
  300. struct v4l2_querymenu *c)
  301. {
  302. struct saa7134_dev *dev = file->private_data;
  303. if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
  304. return -EINVAL;
  305. return saa7134_i2c_call_saa6752(dev, VIDIOC_QUERYMENU, c);
  306. }
  307. static const struct file_operations ts_fops =
  308. {
  309. .owner = THIS_MODULE,
  310. .open = ts_open,
  311. .release = ts_release,
  312. .read = ts_read,
  313. .poll = ts_poll,
  314. .mmap = ts_mmap,
  315. .ioctl = video_ioctl2,
  316. .llseek = no_llseek,
  317. };
  318. /* ----------------------------------------------------------- */
  319. static struct video_device saa7134_empress_template =
  320. {
  321. .name = "saa7134-empress",
  322. .type = 0 /* FIXME */,
  323. .type2 = 0 /* FIXME */,
  324. .fops = &ts_fops,
  325. .minor = -1,
  326. .vidioc_querycap = empress_querycap,
  327. .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap,
  328. .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap,
  329. .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap,
  330. .vidioc_reqbufs = empress_reqbufs,
  331. .vidioc_querybuf = empress_querybuf,
  332. .vidioc_qbuf = empress_qbuf,
  333. .vidioc_dqbuf = empress_dqbuf,
  334. .vidioc_streamon = empress_streamon,
  335. .vidioc_streamoff = empress_streamoff,
  336. .vidioc_s_ext_ctrls = empress_s_ext_ctrls,
  337. .vidioc_g_ext_ctrls = empress_g_ext_ctrls,
  338. .vidioc_enum_input = empress_enum_input,
  339. .vidioc_g_input = empress_g_input,
  340. .vidioc_s_input = empress_s_input,
  341. .vidioc_queryctrl = empress_queryctrl,
  342. .vidioc_querymenu = empress_querymenu,
  343. .vidioc_g_ctrl = saa7134_g_ctrl,
  344. .vidioc_s_ctrl = saa7134_s_ctrl,
  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 (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->dev = &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. */