saa7134-empress.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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/smp_lock.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. #include <media/v4l2-chip-ident.h>
  30. /* ------------------------------------------------------------------ */
  31. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  32. MODULE_LICENSE("GPL");
  33. static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
  34. module_param_array(empress_nr, int, NULL, 0444);
  35. MODULE_PARM_DESC(empress_nr,"ts device number");
  36. static unsigned int debug;
  37. module_param(debug, int, 0644);
  38. MODULE_PARM_DESC(debug,"enable debug messages");
  39. #define dprintk(fmt, arg...) if (debug) \
  40. printk(KERN_DEBUG "%s/empress: " fmt, dev->name , ## arg)
  41. /* ------------------------------------------------------------------ */
  42. static void ts_reset_encoder(struct saa7134_dev* dev)
  43. {
  44. if (!dev->empress_started)
  45. return;
  46. saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
  47. msleep(10);
  48. saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
  49. msleep(100);
  50. dev->empress_started = 0;
  51. }
  52. static int ts_init_encoder(struct saa7134_dev* dev)
  53. {
  54. u32 leading_null_bytes = 0;
  55. /* If more cards start to need this, then this
  56. should probably be added to the card definitions. */
  57. switch (dev->board) {
  58. case SAA7134_BOARD_BEHOLD_M6:
  59. case SAA7134_BOARD_BEHOLD_M63:
  60. case SAA7134_BOARD_BEHOLD_M6_EXTRA:
  61. leading_null_bytes = 1;
  62. break;
  63. }
  64. ts_reset_encoder(dev);
  65. saa_call_all(dev, core, init, leading_null_bytes);
  66. dev->empress_started = 1;
  67. return 0;
  68. }
  69. /* ------------------------------------------------------------------ */
  70. static int ts_open(struct file *file)
  71. {
  72. struct video_device *vdev = video_devdata(file);
  73. struct saa7134_dev *dev = video_drvdata(file);
  74. int err;
  75. dprintk("open dev=%s\n", video_device_node_name(vdev));
  76. err = -EBUSY;
  77. if (!mutex_trylock(&dev->empress_tsq.vb_lock))
  78. return err;
  79. if (atomic_read(&dev->empress_users))
  80. goto done;
  81. /* Unmute audio */
  82. saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
  83. saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
  84. atomic_inc(&dev->empress_users);
  85. file->private_data = dev;
  86. err = 0;
  87. done:
  88. mutex_unlock(&dev->empress_tsq.vb_lock);
  89. return err;
  90. }
  91. static int ts_release(struct file *file)
  92. {
  93. struct saa7134_dev *dev = file->private_data;
  94. videobuf_stop(&dev->empress_tsq);
  95. videobuf_mmap_free(&dev->empress_tsq);
  96. /* stop the encoder */
  97. ts_reset_encoder(dev);
  98. /* Mute audio */
  99. saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
  100. saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
  101. atomic_dec(&dev->empress_users);
  102. return 0;
  103. }
  104. static ssize_t
  105. ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  106. {
  107. struct saa7134_dev *dev = file->private_data;
  108. if (!dev->empress_started)
  109. ts_init_encoder(dev);
  110. return videobuf_read_stream(&dev->empress_tsq,
  111. data, count, ppos, 0,
  112. file->f_flags & O_NONBLOCK);
  113. }
  114. static unsigned int
  115. ts_poll(struct file *file, struct poll_table_struct *wait)
  116. {
  117. struct saa7134_dev *dev = file->private_data;
  118. return videobuf_poll_stream(file, &dev->empress_tsq, wait);
  119. }
  120. static int
  121. ts_mmap(struct file *file, struct vm_area_struct * vma)
  122. {
  123. struct saa7134_dev *dev = file->private_data;
  124. return videobuf_mmap_mapper(&dev->empress_tsq, vma);
  125. }
  126. /*
  127. * This function is _not_ called directly, but from
  128. * video_generic_ioctl (and maybe others). userspace
  129. * copying is done already, arg is a kernel pointer.
  130. */
  131. static int empress_querycap(struct file *file, void *priv,
  132. struct v4l2_capability *cap)
  133. {
  134. struct saa7134_dev *dev = file->private_data;
  135. strcpy(cap->driver, "saa7134");
  136. strlcpy(cap->card, saa7134_boards[dev->board].name,
  137. sizeof(cap->card));
  138. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  139. cap->version = SAA7134_VERSION_CODE;
  140. cap->capabilities =
  141. V4L2_CAP_VIDEO_CAPTURE |
  142. V4L2_CAP_READWRITE |
  143. V4L2_CAP_STREAMING;
  144. return 0;
  145. }
  146. static int empress_enum_input(struct file *file, void *priv,
  147. struct v4l2_input *i)
  148. {
  149. if (i->index != 0)
  150. return -EINVAL;
  151. i->type = V4L2_INPUT_TYPE_CAMERA;
  152. strcpy(i->name, "CCIR656");
  153. return 0;
  154. }
  155. static int empress_g_input(struct file *file, void *priv, unsigned int *i)
  156. {
  157. *i = 0;
  158. return 0;
  159. }
  160. static int empress_s_input(struct file *file, void *priv, unsigned int i)
  161. {
  162. if (i != 0)
  163. return -EINVAL;
  164. return 0;
  165. }
  166. static int empress_enum_fmt_vid_cap(struct file *file, void *priv,
  167. struct v4l2_fmtdesc *f)
  168. {
  169. if (f->index != 0)
  170. return -EINVAL;
  171. strlcpy(f->description, "MPEG TS", sizeof(f->description));
  172. f->pixelformat = V4L2_PIX_FMT_MPEG;
  173. return 0;
  174. }
  175. static int empress_g_fmt_vid_cap(struct file *file, void *priv,
  176. struct v4l2_format *f)
  177. {
  178. struct saa7134_dev *dev = file->private_data;
  179. struct v4l2_mbus_framefmt mbus_fmt;
  180. saa_call_all(dev, video, g_mbus_fmt, &mbus_fmt);
  181. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  182. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  183. f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
  184. return 0;
  185. }
  186. static int empress_s_fmt_vid_cap(struct file *file, void *priv,
  187. struct v4l2_format *f)
  188. {
  189. struct saa7134_dev *dev = file->private_data;
  190. struct v4l2_mbus_framefmt mbus_fmt;
  191. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  192. saa_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  193. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  194. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  195. f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
  196. return 0;
  197. }
  198. static int empress_try_fmt_vid_cap(struct file *file, void *priv,
  199. struct v4l2_format *f)
  200. {
  201. struct saa7134_dev *dev = file->private_data;
  202. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  203. f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
  204. return 0;
  205. }
  206. static int empress_reqbufs(struct file *file, void *priv,
  207. struct v4l2_requestbuffers *p)
  208. {
  209. struct saa7134_dev *dev = file->private_data;
  210. return videobuf_reqbufs(&dev->empress_tsq, p);
  211. }
  212. static int empress_querybuf(struct file *file, void *priv,
  213. struct v4l2_buffer *b)
  214. {
  215. struct saa7134_dev *dev = file->private_data;
  216. return videobuf_querybuf(&dev->empress_tsq, b);
  217. }
  218. static int empress_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  219. {
  220. struct saa7134_dev *dev = file->private_data;
  221. return videobuf_qbuf(&dev->empress_tsq, b);
  222. }
  223. static int empress_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  224. {
  225. struct saa7134_dev *dev = file->private_data;
  226. return videobuf_dqbuf(&dev->empress_tsq, b,
  227. file->f_flags & O_NONBLOCK);
  228. }
  229. static int empress_streamon(struct file *file, void *priv,
  230. enum v4l2_buf_type type)
  231. {
  232. struct saa7134_dev *dev = file->private_data;
  233. return videobuf_streamon(&dev->empress_tsq);
  234. }
  235. static int empress_streamoff(struct file *file, void *priv,
  236. enum v4l2_buf_type type)
  237. {
  238. struct saa7134_dev *dev = file->private_data;
  239. return videobuf_streamoff(&dev->empress_tsq);
  240. }
  241. static int empress_s_ext_ctrls(struct file *file, void *priv,
  242. struct v4l2_ext_controls *ctrls)
  243. {
  244. struct saa7134_dev *dev = file->private_data;
  245. int err;
  246. /* count == 0 is abused in saa6752hs.c, so that special
  247. case is handled here explicitly. */
  248. if (ctrls->count == 0)
  249. return 0;
  250. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  251. return -EINVAL;
  252. err = saa_call_empress(dev, core, s_ext_ctrls, ctrls);
  253. ts_init_encoder(dev);
  254. return err;
  255. }
  256. static int empress_g_ext_ctrls(struct file *file, void *priv,
  257. struct v4l2_ext_controls *ctrls)
  258. {
  259. struct saa7134_dev *dev = file->private_data;
  260. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  261. return -EINVAL;
  262. return saa_call_empress(dev, core, g_ext_ctrls, ctrls);
  263. }
  264. static int empress_g_ctrl(struct file *file, void *priv,
  265. struct v4l2_control *c)
  266. {
  267. struct saa7134_dev *dev = file->private_data;
  268. return saa7134_g_ctrl_internal(dev, NULL, c);
  269. }
  270. static int empress_s_ctrl(struct file *file, void *priv,
  271. struct v4l2_control *c)
  272. {
  273. struct saa7134_dev *dev = file->private_data;
  274. return saa7134_s_ctrl_internal(dev, NULL, c);
  275. }
  276. static int empress_queryctrl(struct file *file, void *priv,
  277. struct v4l2_queryctrl *c)
  278. {
  279. /* Must be sorted from low to high control ID! */
  280. static const u32 user_ctrls[] = {
  281. V4L2_CID_USER_CLASS,
  282. V4L2_CID_BRIGHTNESS,
  283. V4L2_CID_CONTRAST,
  284. V4L2_CID_SATURATION,
  285. V4L2_CID_HUE,
  286. V4L2_CID_AUDIO_VOLUME,
  287. V4L2_CID_AUDIO_MUTE,
  288. V4L2_CID_HFLIP,
  289. 0
  290. };
  291. /* Must be sorted from low to high control ID! */
  292. static const u32 mpeg_ctrls[] = {
  293. V4L2_CID_MPEG_CLASS,
  294. V4L2_CID_MPEG_STREAM_TYPE,
  295. V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
  296. V4L2_CID_MPEG_AUDIO_ENCODING,
  297. V4L2_CID_MPEG_AUDIO_L2_BITRATE,
  298. V4L2_CID_MPEG_VIDEO_ENCODING,
  299. V4L2_CID_MPEG_VIDEO_ASPECT,
  300. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  301. V4L2_CID_MPEG_VIDEO_BITRATE,
  302. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  303. 0
  304. };
  305. static const u32 *ctrl_classes[] = {
  306. user_ctrls,
  307. mpeg_ctrls,
  308. NULL
  309. };
  310. struct saa7134_dev *dev = file->private_data;
  311. c->id = v4l2_ctrl_next(ctrl_classes, c->id);
  312. if (c->id == 0)
  313. return -EINVAL;
  314. if (c->id == V4L2_CID_USER_CLASS || c->id == V4L2_CID_MPEG_CLASS)
  315. return v4l2_ctrl_query_fill(c, 0, 0, 0, 0);
  316. if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
  317. return saa7134_queryctrl(file, priv, c);
  318. return saa_call_empress(dev, core, queryctrl, c);
  319. }
  320. static int empress_querymenu(struct file *file, void *priv,
  321. struct v4l2_querymenu *c)
  322. {
  323. struct saa7134_dev *dev = file->private_data;
  324. if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
  325. return -EINVAL;
  326. return saa_call_empress(dev, core, querymenu, c);
  327. }
  328. static int empress_g_chip_ident(struct file *file, void *fh,
  329. struct v4l2_dbg_chip_ident *chip)
  330. {
  331. struct saa7134_dev *dev = file->private_data;
  332. chip->ident = V4L2_IDENT_NONE;
  333. chip->revision = 0;
  334. if (chip->match.type == V4L2_CHIP_MATCH_I2C_DRIVER &&
  335. !strcmp(chip->match.name, "saa6752hs"))
  336. return saa_call_empress(dev, core, g_chip_ident, chip);
  337. if (chip->match.type == V4L2_CHIP_MATCH_I2C_ADDR)
  338. return saa_call_empress(dev, core, g_chip_ident, chip);
  339. return -EINVAL;
  340. }
  341. static int empress_s_std(struct file *file, void *priv, v4l2_std_id *id)
  342. {
  343. struct saa7134_dev *dev = file->private_data;
  344. return saa7134_s_std_internal(dev, NULL, id);
  345. }
  346. static int empress_g_std(struct file *file, void *priv, v4l2_std_id *id)
  347. {
  348. struct saa7134_dev *dev = file->private_data;
  349. *id = dev->tvnorm->id;
  350. return 0;
  351. }
  352. static const struct v4l2_file_operations ts_fops =
  353. {
  354. .owner = THIS_MODULE,
  355. .open = ts_open,
  356. .release = ts_release,
  357. .read = ts_read,
  358. .poll = ts_poll,
  359. .mmap = ts_mmap,
  360. .ioctl = video_ioctl2,
  361. };
  362. static const struct v4l2_ioctl_ops ts_ioctl_ops = {
  363. .vidioc_querycap = empress_querycap,
  364. .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap,
  365. .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap,
  366. .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap,
  367. .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap,
  368. .vidioc_reqbufs = empress_reqbufs,
  369. .vidioc_querybuf = empress_querybuf,
  370. .vidioc_qbuf = empress_qbuf,
  371. .vidioc_dqbuf = empress_dqbuf,
  372. .vidioc_streamon = empress_streamon,
  373. .vidioc_streamoff = empress_streamoff,
  374. .vidioc_s_ext_ctrls = empress_s_ext_ctrls,
  375. .vidioc_g_ext_ctrls = empress_g_ext_ctrls,
  376. .vidioc_enum_input = empress_enum_input,
  377. .vidioc_g_input = empress_g_input,
  378. .vidioc_s_input = empress_s_input,
  379. .vidioc_queryctrl = empress_queryctrl,
  380. .vidioc_querymenu = empress_querymenu,
  381. .vidioc_g_ctrl = empress_g_ctrl,
  382. .vidioc_s_ctrl = empress_s_ctrl,
  383. .vidioc_g_chip_ident = empress_g_chip_ident,
  384. .vidioc_s_std = empress_s_std,
  385. .vidioc_g_std = empress_g_std,
  386. };
  387. /* ----------------------------------------------------------- */
  388. static struct video_device saa7134_empress_template = {
  389. .name = "saa7134-empress",
  390. .fops = &ts_fops,
  391. .ioctl_ops = &ts_ioctl_ops,
  392. .tvnorms = SAA7134_NORMS,
  393. .current_norm = V4L2_STD_PAL,
  394. };
  395. static void empress_signal_update(struct work_struct *work)
  396. {
  397. struct saa7134_dev* dev =
  398. container_of(work, struct saa7134_dev, empress_workqueue);
  399. if (dev->nosignal) {
  400. dprintk("no video signal\n");
  401. } else {
  402. dprintk("video signal acquired\n");
  403. }
  404. }
  405. static void empress_signal_change(struct saa7134_dev *dev)
  406. {
  407. schedule_work(&dev->empress_workqueue);
  408. }
  409. static int empress_init(struct saa7134_dev *dev)
  410. {
  411. int err;
  412. dprintk("%s: %s\n",dev->name,__func__);
  413. dev->empress_dev = video_device_alloc();
  414. if (NULL == dev->empress_dev)
  415. return -ENOMEM;
  416. *(dev->empress_dev) = saa7134_empress_template;
  417. dev->empress_dev->parent = &dev->pci->dev;
  418. dev->empress_dev->release = video_device_release;
  419. snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
  420. "%s empress (%s)", dev->name,
  421. saa7134_boards[dev->board].name);
  422. INIT_WORK(&dev->empress_workqueue, empress_signal_update);
  423. video_set_drvdata(dev->empress_dev, dev);
  424. err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
  425. empress_nr[dev->nr]);
  426. if (err < 0) {
  427. printk(KERN_INFO "%s: can't register video device\n",
  428. dev->name);
  429. video_device_release(dev->empress_dev);
  430. dev->empress_dev = NULL;
  431. return err;
  432. }
  433. printk(KERN_INFO "%s: registered device %s [mpeg]\n",
  434. dev->name, video_device_node_name(dev->empress_dev));
  435. videobuf_queue_sg_init(&dev->empress_tsq, &saa7134_ts_qops,
  436. &dev->pci->dev, &dev->slock,
  437. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  438. V4L2_FIELD_ALTERNATE,
  439. sizeof(struct saa7134_buf),
  440. dev);
  441. empress_signal_update(&dev->empress_workqueue);
  442. return 0;
  443. }
  444. static int empress_fini(struct saa7134_dev *dev)
  445. {
  446. dprintk("%s: %s\n",dev->name,__func__);
  447. if (NULL == dev->empress_dev)
  448. return 0;
  449. flush_scheduled_work();
  450. video_unregister_device(dev->empress_dev);
  451. dev->empress_dev = NULL;
  452. return 0;
  453. }
  454. static struct saa7134_mpeg_ops empress_ops = {
  455. .type = SAA7134_MPEG_EMPRESS,
  456. .init = empress_init,
  457. .fini = empress_fini,
  458. .signal_change = empress_signal_change,
  459. };
  460. static int __init empress_register(void)
  461. {
  462. return saa7134_ts_register(&empress_ops);
  463. }
  464. static void __exit empress_unregister(void)
  465. {
  466. saa7134_ts_unregister(&empress_ops);
  467. }
  468. module_init(empress_register);
  469. module_exit(empress_unregister);
  470. /* ----------------------------------------------------------- */
  471. /*
  472. * Local variables:
  473. * c-basic-offset: 8
  474. * End:
  475. */