em28xx-video.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  1. /*
  2. em2820-video.c - driver for Empia EM2820/2840 USB video capture devices
  3. Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
  4. Ludovico Cavedon <cavedon@sssup.it>
  5. Mauro Carvalho Chehab <mchehab@brturbo.com.br>
  6. Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  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/usb.h>
  24. #include <linux/i2c.h>
  25. #include <media/tuner.h>
  26. #include <linux/video_decoder.h>
  27. #include "em2820.h"
  28. #define DRIVER_AUTHOR "Markus Rechberger <mrechberger@gmail.com>, " \
  29. "Ludovico Cavedon <cavedon@sssup.it>, " \
  30. "Mauro Carvalho Chehab <mchehab@brturbo.com.br>"
  31. #define DRIVER_NAME "em2820"
  32. #define DRIVER_DESC "Empia em2820 based USB video device driver"
  33. #define EM2820_VERSION_CODE KERNEL_VERSION(0, 0, 1)
  34. #define em2820_videodbg(fmt, arg...) do {\
  35. if (video_debug) \
  36. printk(KERN_INFO "%s %s :"fmt, \
  37. dev->name, __FUNCTION__ , ##arg); } while (0)
  38. MODULE_AUTHOR(DRIVER_AUTHOR);
  39. MODULE_DESCRIPTION(DRIVER_DESC);
  40. MODULE_LICENSE("GPL");
  41. static int tuner = -1;
  42. module_param(tuner, int, 0444);
  43. MODULE_PARM_DESC(tuner, "tuner type");
  44. static unsigned int video_debug = 0;
  45. module_param(video_debug,int,0644);
  46. MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
  47. /* supported tv norms */
  48. static struct em2820_tvnorm tvnorms[] = {
  49. {
  50. .name = "PAL",
  51. .id = V4L2_STD_PAL,
  52. .mode = VIDEO_MODE_PAL,
  53. }, {
  54. .name = "NTSC",
  55. .id = V4L2_STD_NTSC,
  56. .mode = VIDEO_MODE_NTSC,
  57. }, {
  58. .name = "SECAM",
  59. .id = V4L2_STD_SECAM,
  60. .mode = VIDEO_MODE_SECAM,
  61. }, {
  62. .name = "PAL-M",
  63. .id = V4L2_STD_PAL_M,
  64. .mode = VIDEO_MODE_PAL,
  65. }
  66. };
  67. #define TVNORMS ARRAY_SIZE(tvnorms)
  68. /* supported controls */
  69. static struct v4l2_queryctrl em2820_qctrl[] = {
  70. {
  71. .id = V4L2_CID_BRIGHTNESS,
  72. .type = V4L2_CTRL_TYPE_INTEGER,
  73. .name = "Brightness",
  74. .minimum = -128,
  75. .maximum = 127,
  76. .step = 1,
  77. .default_value = 0,
  78. .flags = 0,
  79. },{
  80. .id = V4L2_CID_CONTRAST,
  81. .type = V4L2_CTRL_TYPE_INTEGER,
  82. .name = "Contrast",
  83. .minimum = 0x0,
  84. .maximum = 0x1f,
  85. .step = 0x1,
  86. .default_value = 0x10,
  87. .flags = 0,
  88. },{
  89. .id = V4L2_CID_SATURATION,
  90. .type = V4L2_CTRL_TYPE_INTEGER,
  91. .name = "Saturation",
  92. .minimum = 0x0,
  93. .maximum = 0x1f,
  94. .step = 0x1,
  95. .default_value = 0x10,
  96. .flags = 0,
  97. },{
  98. .id = V4L2_CID_AUDIO_VOLUME,
  99. .type = V4L2_CTRL_TYPE_INTEGER,
  100. .name = "Volume",
  101. .minimum = 0x0,
  102. .maximum = 0x1f,
  103. .step = 0x1,
  104. .default_value = 0x1f,
  105. .flags = 0,
  106. },{
  107. .id = V4L2_CID_AUDIO_MUTE,
  108. .type = V4L2_CTRL_TYPE_BOOLEAN,
  109. .name = "Mute",
  110. .minimum = 0,
  111. .maximum = 1,
  112. .step = 1,
  113. .default_value = 1,
  114. .flags = 0,
  115. },{
  116. .id = V4L2_CID_RED_BALANCE,
  117. .type = V4L2_CTRL_TYPE_INTEGER,
  118. .name = "Red chroma balance",
  119. .minimum = -128,
  120. .maximum = 127,
  121. .step = 1,
  122. .default_value = 0,
  123. .flags = 0,
  124. },{
  125. .id = V4L2_CID_BLUE_BALANCE,
  126. .type = V4L2_CTRL_TYPE_INTEGER,
  127. .name = "Blue chroma balance",
  128. .minimum = -128,
  129. .maximum = 127,
  130. .step = 1,
  131. .default_value = 0,
  132. .flags = 0,
  133. },{
  134. .id = V4L2_CID_GAMMA,
  135. .type = V4L2_CTRL_TYPE_INTEGER,
  136. .name = "Gamma",
  137. .minimum = 0x0,
  138. .maximum = 0x3f,
  139. .step = 0x1,
  140. .default_value = 0x20,
  141. .flags = 0,
  142. }
  143. };
  144. static struct usb_driver em2820_usb_driver;
  145. static DECLARE_MUTEX(em2820_sysfs_lock);
  146. static DECLARE_RWSEM(em2820_disconnect);
  147. /********************* v4l2 interface ******************************************/
  148. static inline unsigned long kvirt_to_pa(unsigned long adr)
  149. {
  150. unsigned long kva, ret;
  151. kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
  152. kva |= adr & (PAGE_SIZE - 1);
  153. ret = __pa(kva);
  154. return ret;
  155. }
  156. /*
  157. * em2820_config()
  158. * inits registers with sane defaults
  159. */
  160. static int em2820_config(struct em2820 *dev)
  161. {
  162. /* Sets I2C speed to 100 KHz */
  163. em2820_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
  164. /* enable vbi capturing */
  165. em2820_audio_usb_mute(dev, 1);
  166. dev->mute = 1; /* maybe not the right place... */
  167. dev->volume = 0x1f;
  168. em2820_audio_analog_set(dev);
  169. em2820_audio_analog_setup(dev);
  170. em2820_outfmt_set_yuv422(dev);
  171. em2820_colorlevels_set_default(dev);
  172. em2820_compression_disable(dev);
  173. return 0;
  174. }
  175. /*
  176. * em2820_config_i2c()
  177. * configure i2c attached devices
  178. */
  179. void em2820_config_i2c(struct em2820 *dev)
  180. {
  181. struct v4l2_frequency f;
  182. struct video_decoder_init em2820_vdi = {.data = NULL };
  183. /* configure decoder */
  184. em2820_i2c_call_clients(dev, DECODER_INIT, &em2820_vdi);
  185. em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
  186. /* em2820_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
  187. /* em2820_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
  188. /* em2820_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
  189. /* em2820_i2c_call_clients(dev,DECODER_DUMP, NULL); */
  190. /* configure tuner */
  191. f.tuner = 0;
  192. f.type = V4L2_TUNER_ANALOG_TV;
  193. f.frequency = 9076; /* FIXME:remove magic number */
  194. dev->ctl_freq = f.frequency;
  195. em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
  196. /* configure tda9887 */
  197. /* em2820_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
  198. }
  199. /*
  200. * em2820_empty_framequeues()
  201. * prepare queues for incoming and outgoing frames
  202. */
  203. static void em2820_empty_framequeues(struct em2820 *dev)
  204. {
  205. u32 i;
  206. INIT_LIST_HEAD(&dev->inqueue);
  207. INIT_LIST_HEAD(&dev->outqueue);
  208. for (i = 0; i < EM2820_NUM_FRAMES; i++) {
  209. dev->frame[i].state = F_UNUSED;
  210. dev->frame[i].buf.bytesused = 0;
  211. }
  212. }
  213. /*
  214. * em2820_v4l2_open()
  215. * inits the device and starts isoc transfer
  216. */
  217. static int em2820_v4l2_open(struct inode *inode, struct file *filp)
  218. {
  219. struct video_device *vdev = video_devdata(filp);
  220. int minor = iminor(inode);
  221. struct em2820 *dev = (struct em2820 *)video_get_drvdata(vdev);
  222. int errCode = 0;
  223. em2820_videodbg("users=%d", dev->users);
  224. if (!down_read_trylock(&em2820_disconnect))
  225. return -ERESTARTSYS;
  226. if (dev->users) {
  227. em2820_warn("this driver can be opened only once\n");
  228. up_read(&em2820_disconnect);
  229. return -EBUSY;
  230. }
  231. /* if(dev->vbi_dev->minor == minor){
  232. dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
  233. }*/
  234. if (dev->vdev->minor == minor) {
  235. dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  236. }
  237. init_MUTEX(&dev->fileop_lock); /* to 1 == available */
  238. spin_lock_init(&dev->queue_lock);
  239. init_waitqueue_head(&dev->wait_frame);
  240. init_waitqueue_head(&dev->wait_stream);
  241. down(&dev->lock);
  242. em2820_set_alternate(dev);
  243. dev->width = norm_maxw(dev);
  244. dev->height = norm_maxh(dev);
  245. dev->frame_size = dev->width * dev->height * 2;
  246. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  247. dev->bytesperline = dev->width * 2;
  248. dev->hscale = 0;
  249. dev->vscale = 0;
  250. em2820_capture_start(dev, 1);
  251. em2820_resolution_set(dev);
  252. /* start the transfer */
  253. errCode = em2820_init_isoc(dev);
  254. if (errCode)
  255. goto err;
  256. dev->users++;
  257. filp->private_data = dev;
  258. dev->io = IO_NONE;
  259. dev->stream = STREAM_OFF;
  260. dev->num_frames = 0;
  261. /* prepare queues */
  262. em2820_empty_framequeues(dev);
  263. dev->state |= DEV_INITIALIZED;
  264. err:
  265. up(&dev->lock);
  266. up_read(&em2820_disconnect);
  267. return errCode;
  268. }
  269. /*
  270. * em2820_realease_resources()
  271. * unregisters the v4l2,i2c and usb devices
  272. * called when the device gets disconected or at module unload
  273. */
  274. static void em2820_release_resources(struct em2820 *dev)
  275. {
  276. down(&em2820_sysfs_lock);
  277. em2820_info("V4L2 device /dev/video%d deregistered\n",
  278. dev->vdev->minor);
  279. video_set_drvdata(dev->vdev, NULL);
  280. video_unregister_device(dev->vdev);
  281. /* video_unregister_device(dev->vbi_dev); */
  282. em2820_i2c_unregister(dev);
  283. usb_put_dev(dev->udev);
  284. up(&em2820_sysfs_lock);
  285. }
  286. /*
  287. * em2820_v4l2_close()
  288. * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
  289. */
  290. static int em2820_v4l2_close(struct inode *inode, struct file *file)
  291. {
  292. struct video_device *vdev = video_devdata(file);
  293. struct em2820 *dev = (struct em2820 *)video_get_drvdata(vdev);
  294. int errCode;
  295. em2820_videodbg("users=%d", dev->users);
  296. down(&dev->lock);
  297. em2820_uninit_isoc(dev);
  298. em2820_release_buffers(dev);
  299. /* the device is already disconnect, free the remaining resources */
  300. if (dev->state & DEV_DISCONNECTED) {
  301. em2820_release_resources(dev);
  302. up(&dev->lock);
  303. kfree(dev);
  304. return 0;
  305. }
  306. /* set alternate 0 */
  307. dev->alt = 0;
  308. em2820_videodbg("setting alternate 0");
  309. errCode = usb_set_interface(dev->udev, 0, 0);
  310. if (errCode < 0) {
  311. em2820_errdev ("cannot change alternate number to 0 (error=%i)\n",
  312. errCode);
  313. }
  314. dev->users--;
  315. wake_up_interruptible_nr(&dev->open, 1);
  316. up(&dev->lock);
  317. return 0;
  318. }
  319. /*
  320. * em2820_v4l2_read()
  321. * will allocate buffers when called for the first time
  322. */
  323. static ssize_t
  324. em2820_v4l2_read(struct file *filp, char __user * buf, size_t count,
  325. loff_t * f_pos)
  326. {
  327. struct em2820 *dev = video_get_drvdata(video_devdata(filp));
  328. struct em2820_frame_t *f, *i;
  329. unsigned long lock_flags;
  330. int ret = 0;
  331. if (down_interruptible(&dev->fileop_lock))
  332. return -ERESTARTSYS;
  333. if (dev->state & DEV_DISCONNECTED) {
  334. em2820_videodbg("device not present");
  335. up(&dev->fileop_lock);
  336. return -ENODEV;
  337. }
  338. if (dev->state & DEV_MISCONFIGURED) {
  339. em2820_videodbg("device misconfigured; close and open it again");
  340. up(&dev->fileop_lock);
  341. return -EIO;
  342. }
  343. if (dev->io == IO_MMAP) {
  344. em2820_videodbg ("IO method is set to mmap; close and open"
  345. " the device again to choose the read method");
  346. up(&dev->fileop_lock);
  347. return -EINVAL;
  348. }
  349. if (dev->io == IO_NONE) {
  350. if (!em2820_request_buffers(dev, EM2820_NUM_READ_FRAMES)) {
  351. em2820_errdev("read failed, not enough memory\n");
  352. up(&dev->fileop_lock);
  353. return -ENOMEM;
  354. }
  355. dev->io = IO_READ;
  356. dev->stream = STREAM_ON;
  357. em2820_queue_unusedframes(dev);
  358. }
  359. if (!count) {
  360. up(&dev->fileop_lock);
  361. return 0;
  362. }
  363. if (list_empty(&dev->outqueue)) {
  364. if (filp->f_flags & O_NONBLOCK) {
  365. up(&dev->fileop_lock);
  366. return -EAGAIN;
  367. }
  368. ret = wait_event_interruptible
  369. (dev->wait_frame,
  370. (!list_empty(&dev->outqueue)) ||
  371. (dev->state & DEV_DISCONNECTED));
  372. if (ret) {
  373. up(&dev->fileop_lock);
  374. return ret;
  375. }
  376. if (dev->state & DEV_DISCONNECTED) {
  377. up(&dev->fileop_lock);
  378. return -ENODEV;
  379. }
  380. }
  381. f = list_entry(dev->outqueue.prev, struct em2820_frame_t, frame);
  382. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  383. list_for_each_entry(i, &dev->outqueue, frame)
  384. i->state = F_UNUSED;
  385. INIT_LIST_HEAD(&dev->outqueue);
  386. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  387. em2820_queue_unusedframes(dev);
  388. if (count > f->buf.length)
  389. count = f->buf.length;
  390. if (copy_to_user(buf, f->bufmem, count)) {
  391. up(&dev->fileop_lock);
  392. return -EFAULT;
  393. }
  394. *f_pos += count;
  395. up(&dev->fileop_lock);
  396. return count;
  397. }
  398. /*
  399. * em2820_v4l2_poll()
  400. * will allocate buffers when called for the first time
  401. */
  402. static unsigned int em2820_v4l2_poll(struct file *filp, poll_table * wait)
  403. {
  404. struct em2820 *dev = video_get_drvdata(video_devdata(filp));
  405. unsigned int mask = 0;
  406. if (down_interruptible(&dev->fileop_lock))
  407. return POLLERR;
  408. if (dev->state & DEV_DISCONNECTED) {
  409. em2820_videodbg("device not present");
  410. } else if (dev->state & DEV_MISCONFIGURED) {
  411. em2820_videodbg("device is misconfigured; close and open it again");
  412. } else {
  413. if (dev->io == IO_NONE) {
  414. if (!em2820_request_buffers
  415. (dev, EM2820_NUM_READ_FRAMES)) {
  416. em2820_warn
  417. ("poll() failed, not enough memory\n");
  418. } else {
  419. dev->io = IO_READ;
  420. dev->stream = STREAM_ON;
  421. }
  422. }
  423. if (dev->io == IO_READ) {
  424. em2820_queue_unusedframes(dev);
  425. poll_wait(filp, &dev->wait_frame, wait);
  426. if (!list_empty(&dev->outqueue))
  427. mask |= POLLIN | POLLRDNORM;
  428. up(&dev->fileop_lock);
  429. return mask;
  430. }
  431. }
  432. up(&dev->fileop_lock);
  433. return POLLERR;
  434. }
  435. /*
  436. * em2820_vm_open()
  437. */
  438. static void em2820_vm_open(struct vm_area_struct *vma)
  439. {
  440. struct em2820_frame_t *f = vma->vm_private_data;
  441. f->vma_use_count++;
  442. }
  443. /*
  444. * em2820_vm_close()
  445. */
  446. static void em2820_vm_close(struct vm_area_struct *vma)
  447. {
  448. /* NOTE: buffers are not freed here */
  449. struct em2820_frame_t *f = vma->vm_private_data;
  450. f->vma_use_count--;
  451. }
  452. static struct vm_operations_struct em2820_vm_ops = {
  453. .open = em2820_vm_open,
  454. .close = em2820_vm_close,
  455. };
  456. /*
  457. * em2820_v4l2_mmap()
  458. */
  459. static int em2820_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  460. {
  461. struct em2820 *dev = video_get_drvdata(video_devdata(filp));
  462. unsigned long size = vma->vm_end - vma->vm_start,
  463. start = vma->vm_start, pos, page;
  464. u32 i;
  465. if (down_interruptible(&dev->fileop_lock))
  466. return -ERESTARTSYS;
  467. if (dev->state & DEV_DISCONNECTED) {
  468. em2820_videodbg("mmap: device not present");
  469. up(&dev->fileop_lock);
  470. return -ENODEV;
  471. }
  472. if (dev->state & DEV_MISCONFIGURED) {
  473. em2820_videodbg ("mmap: Device is misconfigured; close and "
  474. "open it again");
  475. up(&dev->fileop_lock);
  476. return -EIO;
  477. }
  478. if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
  479. size != PAGE_ALIGN(dev->frame[0].buf.length)) {
  480. up(&dev->fileop_lock);
  481. return -EINVAL;
  482. }
  483. for (i = 0; i < dev->num_frames; i++) {
  484. if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
  485. break;
  486. }
  487. if (i == dev->num_frames) {
  488. em2820_videodbg("mmap: user supplied mapping address is out of range");
  489. up(&dev->fileop_lock);
  490. return -EINVAL;
  491. }
  492. /* VM_IO is eventually going to replace PageReserved altogether */
  493. vma->vm_flags |= VM_IO;
  494. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  495. pos = (unsigned long)dev->frame[i].bufmem;
  496. while (size > 0) { /* size is page-aligned */
  497. page = vmalloc_to_pfn((void *)pos);
  498. if (remap_pfn_range(vma, start, page, PAGE_SIZE,
  499. vma->vm_page_prot)) {
  500. em2820_videodbg("mmap: rename page map failed");
  501. up(&dev->fileop_lock);
  502. return -EAGAIN;
  503. }
  504. start += PAGE_SIZE;
  505. pos += PAGE_SIZE;
  506. size -= PAGE_SIZE;
  507. }
  508. vma->vm_ops = &em2820_vm_ops;
  509. vma->vm_private_data = &dev->frame[i];
  510. em2820_vm_open(vma);
  511. up(&dev->fileop_lock);
  512. return 0;
  513. }
  514. /*
  515. * em2820_get_ctrl()
  516. * return the current saturation, brightness or contrast, mute state
  517. */
  518. static int em2820_get_ctrl(struct em2820 *dev, struct v4l2_control *ctrl)
  519. {
  520. s32 tmp;
  521. switch (ctrl->id) {
  522. case V4L2_CID_AUDIO_MUTE:
  523. ctrl->value = dev->mute;
  524. return 0;
  525. case V4L2_CID_AUDIO_VOLUME:
  526. ctrl->value = dev->volume;
  527. return 0;
  528. case V4L2_CID_BRIGHTNESS:
  529. if ((tmp = em2820_brightness_get(dev)) < 0)
  530. return -EIO;
  531. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  532. return 0;
  533. case V4L2_CID_CONTRAST:
  534. if ((ctrl->value = em2820_contrast_get(dev)) < 0)
  535. return -EIO;
  536. return 0;
  537. case V4L2_CID_SATURATION:
  538. if ((ctrl->value = em2820_saturation_get(dev)) < 0)
  539. return -EIO;
  540. return 0;
  541. case V4L2_CID_RED_BALANCE:
  542. if ((tmp = em2820_v_balance_get(dev)) < 0)
  543. return -EIO;
  544. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  545. return 0;
  546. case V4L2_CID_BLUE_BALANCE:
  547. if ((tmp = em2820_u_balance_get(dev)) < 0)
  548. return -EIO;
  549. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  550. return 0;
  551. case V4L2_CID_GAMMA:
  552. if ((ctrl->value = em2820_gamma_get(dev)) < 0)
  553. return -EIO;
  554. return 0;
  555. default:
  556. return -EINVAL;
  557. }
  558. }
  559. /*
  560. * em2820_set_ctrl()
  561. * mute or set new saturation, brightness or contrast
  562. */
  563. static int em2820_set_ctrl(struct em2820 *dev, const struct v4l2_control *ctrl)
  564. {
  565. switch (ctrl->id) {
  566. case V4L2_CID_AUDIO_MUTE:
  567. if (ctrl->value != dev->mute) {
  568. dev->mute = ctrl->value;
  569. em2820_audio_usb_mute(dev, ctrl->value);
  570. return em2820_audio_analog_set(dev);
  571. }
  572. return 0;
  573. case V4L2_CID_AUDIO_VOLUME:
  574. dev->volume = ctrl->value;
  575. return em2820_audio_analog_set(dev);
  576. case V4L2_CID_BRIGHTNESS:
  577. return em2820_brightness_set(dev, ctrl->value);
  578. case V4L2_CID_CONTRAST:
  579. return em2820_contrast_set(dev, ctrl->value);
  580. case V4L2_CID_SATURATION:
  581. return em2820_saturation_set(dev, ctrl->value);
  582. case V4L2_CID_RED_BALANCE:
  583. return em2820_v_balance_set(dev, ctrl->value);
  584. case V4L2_CID_BLUE_BALANCE:
  585. return em2820_u_balance_set(dev, ctrl->value);
  586. case V4L2_CID_GAMMA:
  587. return em2820_gamma_set(dev, ctrl->value);
  588. default:
  589. return -EINVAL;
  590. }
  591. }
  592. /*
  593. * em2820_stream_interrupt()
  594. * stops streaming
  595. */
  596. static int em2820_stream_interrupt(struct em2820 *dev)
  597. {
  598. int ret = 0;
  599. /* stop reading from the device */
  600. dev->stream = STREAM_INTERRUPT;
  601. ret = wait_event_timeout(dev->wait_stream,
  602. (dev->stream == STREAM_OFF) ||
  603. (dev->state & DEV_DISCONNECTED),
  604. EM2820_URB_TIMEOUT);
  605. if (dev->state & DEV_DISCONNECTED)
  606. return -ENODEV;
  607. else if (ret) {
  608. dev->state |= DEV_MISCONFIGURED;
  609. em2820_videodbg("device is misconfigured; close and "
  610. "open /dev/video%d again", dev->vdev->minor);
  611. return ret;
  612. }
  613. return 0;
  614. }
  615. static int em2820_set_norm(struct em2820 *dev, int width, int height)
  616. {
  617. unsigned int hscale, vscale;
  618. unsigned int maxh, maxw;
  619. maxw = norm_maxw(dev);
  620. maxh = norm_maxh(dev);
  621. /* width must even because of the YUYV format */
  622. /* height must be even because of interlacing */
  623. height &= 0xfffe;
  624. width &= 0xfffe;
  625. if (height < 32)
  626. height = 32;
  627. if (height > maxh)
  628. height = maxh;
  629. if (width < 48)
  630. width = 48;
  631. if (width > maxw)
  632. width = maxw;
  633. if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
  634. hscale = 0x3fff;
  635. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  636. if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
  637. vscale = 0x3fff;
  638. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  639. /* set new image size */
  640. dev->width = width;
  641. dev->height = height;
  642. dev->frame_size = dev->width * dev->height * 2;
  643. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  644. dev->bytesperline = dev->width * 2;
  645. dev->hscale = hscale;
  646. dev->vscale = vscale;
  647. em2820_resolution_set(dev);
  648. return 0;
  649. }
  650. static void video_mux(struct em2820 *dev, int index)
  651. {
  652. int input, ainput;
  653. input = INPUT(index)->vmux;
  654. dev->ctl_input = index;
  655. em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
  656. dev->ctl_ainput = INPUT(index)->amux;
  657. switch (dev->ctl_ainput) {
  658. case 0:
  659. ainput = EM2820_AUDIO_SRC_TUNER;
  660. break;
  661. default:
  662. ainput = EM2820_AUDIO_SRC_LINE;
  663. }
  664. em2820_audio_source(dev, ainput);
  665. }
  666. /*
  667. * em2820_v4l2_do_ioctl()
  668. * This function is _not_ called directly, but from
  669. * em2820_v4l2_ioctl. Userspace
  670. * copying is done already, arg is a kernel pointer.
  671. */
  672. static int em2820_do_ioctl(struct inode *inode, struct file *filp,
  673. struct em2820 *dev, unsigned int cmd, void *arg,
  674. v4l2_kioctl driver_ioctl)
  675. {
  676. int ret;
  677. switch (cmd) {
  678. /* ---------- tv norms ---------- */
  679. case VIDIOC_ENUMSTD:
  680. {
  681. struct v4l2_standard *e = arg;
  682. unsigned int i;
  683. i = e->index;
  684. if (i >= TVNORMS)
  685. return -EINVAL;
  686. ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
  687. tvnorms[e->index].name);
  688. e->index = i;
  689. if (ret < 0)
  690. return ret;
  691. return 0;
  692. }
  693. case VIDIOC_G_STD:
  694. {
  695. v4l2_std_id *id = arg;
  696. *id = dev->tvnorm->id;
  697. return 0;
  698. }
  699. case VIDIOC_S_STD:
  700. {
  701. v4l2_std_id *id = arg;
  702. unsigned int i;
  703. for (i = 0; i < TVNORMS; i++)
  704. if (*id == tvnorms[i].id)
  705. break;
  706. if (i == TVNORMS)
  707. for (i = 0; i < TVNORMS; i++)
  708. if (*id & tvnorms[i].id)
  709. break;
  710. if (i == TVNORMS)
  711. return -EINVAL;
  712. down(&dev->lock);
  713. dev->tvnorm = &tvnorms[i];
  714. em2820_set_norm(dev, dev->width, dev->height);
  715. /*
  716. dev->width=norm_maxw(dev);
  717. dev->height=norm_maxh(dev);
  718. dev->frame_size=dev->width*dev->height*2;
  719. dev->field_size=dev->frame_size>>1;
  720. dev->bytesperline=dev->width*2;
  721. dev->hscale=0;
  722. dev->vscale=0;
  723. em2820_resolution_set(dev);
  724. */
  725. /*
  726. em2820_uninit_isoc(dev);
  727. em2820_set_alternate(dev);
  728. em2820_capture_start(dev, 1);
  729. em2820_resolution_set(dev);
  730. em2820_init_isoc(dev);
  731. */
  732. em2820_i2c_call_clients(dev, DECODER_SET_NORM,
  733. &tvnorms[i].mode);
  734. em2820_i2c_call_clients(dev, VIDIOC_S_STD,
  735. &dev->tvnorm->id);
  736. up(&dev->lock);
  737. return 0;
  738. }
  739. /* ------ input switching ---------- */
  740. case VIDIOC_ENUMINPUT:
  741. {
  742. struct v4l2_input *i = arg;
  743. unsigned int n;
  744. static const char *iname[] = {
  745. [EM2820_VMUX_COMPOSITE1] = "Composite1",
  746. [EM2820_VMUX_COMPOSITE2] = "Composite2",
  747. [EM2820_VMUX_COMPOSITE3] = "Composite3",
  748. [EM2820_VMUX_COMPOSITE4] = "Composite4",
  749. [EM2820_VMUX_SVIDEO] = "S-Video",
  750. [EM2820_VMUX_TELEVISION] = "Television",
  751. [EM2820_VMUX_CABLE] = "Cable TV",
  752. [EM2820_VMUX_DVB] = "DVB",
  753. [EM2820_VMUX_DEBUG] = "for debug only",
  754. };
  755. n = i->index;
  756. if (n >= MAX_EM2820_INPUT)
  757. return -EINVAL;
  758. if (0 == INPUT(n)->type)
  759. return -EINVAL;
  760. memset(i, 0, sizeof(*i));
  761. i->index = n;
  762. i->type = V4L2_INPUT_TYPE_CAMERA;
  763. strcpy(i->name, iname[INPUT(n)->type]);
  764. if ((EM2820_VMUX_TELEVISION == INPUT(n)->type) ||
  765. (EM2820_VMUX_CABLE == INPUT(n)->type))
  766. i->type = V4L2_INPUT_TYPE_TUNER;
  767. for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
  768. i->std |= tvnorms[n].id;
  769. return 0;
  770. }
  771. case VIDIOC_G_INPUT:
  772. {
  773. int *i = arg;
  774. *i = dev->ctl_input;
  775. return 0;
  776. }
  777. case VIDIOC_S_INPUT:
  778. {
  779. int *index = arg;
  780. if (*index >= MAX_EM2820_INPUT)
  781. return -EINVAL;
  782. if (0 == INPUT(*index)->type)
  783. return -EINVAL;
  784. down(&dev->lock);
  785. video_mux(dev, *index);
  786. up(&dev->lock);
  787. return 0;
  788. }
  789. case VIDIOC_G_AUDIO:
  790. {
  791. struct v4l2_audio *a = arg;
  792. unsigned int index = a->index;
  793. if (a->index > 1)
  794. return -EINVAL;
  795. memset(a, 0, sizeof(*a));
  796. index = dev->ctl_ainput;
  797. if (index == 0) {
  798. strcpy(a->name, "Television");
  799. } else {
  800. strcpy(a->name, "Line In");
  801. }
  802. a->capability = V4L2_AUDCAP_STEREO;
  803. a->index = index;
  804. return 0;
  805. }
  806. case VIDIOC_S_AUDIO:
  807. {
  808. struct v4l2_audio *a = arg;
  809. if (a->index != dev->ctl_ainput)
  810. return -EINVAL;
  811. return 0;
  812. }
  813. /* --- controls ---------------------------------------------- */
  814. case VIDIOC_QUERYCTRL:
  815. {
  816. struct v4l2_queryctrl *qc = arg;
  817. u8 i, n;
  818. n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
  819. for (i = 0; i < n; i++)
  820. if (qc->id && qc->id == em2820_qctrl[i].id) {
  821. memcpy(qc, &(em2820_qctrl[i]),
  822. sizeof(*qc));
  823. return 0;
  824. }
  825. return -EINVAL;
  826. }
  827. case VIDIOC_G_CTRL:
  828. {
  829. struct v4l2_control *ctrl = arg;
  830. return em2820_get_ctrl(dev, ctrl);
  831. }
  832. case VIDIOC_S_CTRL_OLD: /* ??? */
  833. case VIDIOC_S_CTRL:
  834. {
  835. struct v4l2_control *ctrl = arg;
  836. u8 i, n;
  837. n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
  838. for (i = 0; i < n; i++)
  839. if (ctrl->id == em2820_qctrl[i].id) {
  840. if (ctrl->value <
  841. em2820_qctrl[i].minimum
  842. || ctrl->value >
  843. em2820_qctrl[i].maximum)
  844. return -ERANGE;
  845. return em2820_set_ctrl(dev, ctrl);
  846. }
  847. return -EINVAL;
  848. }
  849. /* --- tuner ioctls ------------------------------------------ */
  850. case VIDIOC_G_TUNER:
  851. {
  852. struct v4l2_tuner *t = arg;
  853. int status = 0;
  854. if (0 != t->index)
  855. return -EINVAL;
  856. memset(t, 0, sizeof(*t));
  857. strcpy(t->name, "Tuner");
  858. t->type = V4L2_TUNER_ANALOG_TV;
  859. t->capability = V4L2_TUNER_CAP_NORM;
  860. t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
  861. /* t->signal = 0xffff;*/
  862. /* em2820_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
  863. /* No way to get signal strength? */
  864. down(&dev->lock);
  865. em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
  866. &status);
  867. up(&dev->lock);
  868. t->signal =
  869. (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
  870. em2820_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal,
  871. t->afc);
  872. return 0;
  873. }
  874. case VIDIOC_S_TUNER:
  875. {
  876. struct v4l2_tuner *t = arg;
  877. int status = 0;
  878. if (0 != t->index)
  879. return -EINVAL;
  880. memset(t, 0, sizeof(*t));
  881. strcpy(t->name, "Tuner");
  882. t->type = V4L2_TUNER_ANALOG_TV;
  883. t->capability = V4L2_TUNER_CAP_NORM;
  884. t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
  885. /* t->signal = 0xffff; */
  886. /* No way to get signal strength? */
  887. down(&dev->lock);
  888. em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
  889. &status);
  890. up(&dev->lock);
  891. t->signal =
  892. (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
  893. em2820_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
  894. t->signal, t->afc);
  895. return 0;
  896. }
  897. case VIDIOC_G_FREQUENCY:
  898. {
  899. struct v4l2_frequency *f = arg;
  900. memset(f, 0, sizeof(*f));
  901. f->type = V4L2_TUNER_ANALOG_TV;
  902. f->frequency = dev->ctl_freq;
  903. return 0;
  904. }
  905. case VIDIOC_S_FREQUENCY:
  906. {
  907. struct v4l2_frequency *f = arg;
  908. if (0 != f->tuner)
  909. return -EINVAL;
  910. if (V4L2_TUNER_ANALOG_TV != f->type)
  911. return -EINVAL;
  912. down(&dev->lock);
  913. dev->ctl_freq = f->frequency;
  914. em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  915. up(&dev->lock);
  916. return 0;
  917. }
  918. case VIDIOC_CROPCAP:
  919. {
  920. struct v4l2_cropcap *cc = arg;
  921. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  922. return EINVAL;
  923. cc->bounds.left = 0;
  924. cc->bounds.top = 0;
  925. cc->bounds.width = dev->width;
  926. cc->bounds.height = dev->height;
  927. cc->defrect = cc->bounds;
  928. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  929. cc->pixelaspect.denominator = 59;
  930. return 0;
  931. }
  932. case VIDIOC_STREAMON:
  933. {
  934. int *type = arg;
  935. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  936. || dev->io != IO_MMAP)
  937. return -EINVAL;
  938. if (list_empty(&dev->inqueue))
  939. return -EINVAL;
  940. dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
  941. em2820_videodbg("VIDIOC_STREAMON: starting stream");
  942. return 0;
  943. }
  944. case VIDIOC_STREAMOFF:
  945. {
  946. int *type = arg;
  947. int ret;
  948. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  949. || dev->io != IO_MMAP)
  950. return -EINVAL;
  951. if (dev->stream == STREAM_ON) {
  952. em2820_videodbg ("VIDIOC_STREAMOFF: interrupting stream");
  953. if ((ret = em2820_stream_interrupt(dev)))
  954. return ret;
  955. }
  956. em2820_empty_framequeues(dev);
  957. return 0;
  958. }
  959. default:
  960. return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
  961. driver_ioctl);
  962. }
  963. return 0;
  964. }
  965. /*
  966. * em2820_v4l2_do_ioctl()
  967. * This function is _not_ called directly, but from
  968. * em2820_v4l2_ioctl. Userspace
  969. * copying is done already, arg is a kernel pointer.
  970. */
  971. static int em2820_video_do_ioctl(struct inode *inode, struct file *filp,
  972. unsigned int cmd, void *arg)
  973. {
  974. struct em2820 *dev = filp->private_data;
  975. if (!dev)
  976. return -ENODEV;
  977. if (video_debug > 1)
  978. em2820_print_ioctl(dev->name,cmd);
  979. switch (cmd) {
  980. /* --- capabilities ------------------------------------------ */
  981. case VIDIOC_QUERYCAP:
  982. {
  983. struct v4l2_capability *cap = arg;
  984. memset(cap, 0, sizeof(*cap));
  985. strlcpy(cap->driver, "em2820", sizeof(cap->driver));
  986. strlcpy(cap->card, em2820_boards[dev->model].name,
  987. sizeof(cap->card));
  988. strlcpy(cap->bus_info, dev->udev->dev.bus_id,
  989. sizeof(cap->bus_info));
  990. cap->version = EM2820_VERSION_CODE;
  991. cap->capabilities =
  992. V4L2_CAP_VIDEO_CAPTURE |
  993. V4L2_CAP_AUDIO |
  994. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  995. if (dev->has_tuner)
  996. cap->capabilities |= V4L2_CAP_TUNER;
  997. return 0;
  998. }
  999. /* --- capture ioctls ---------------------------------------- */
  1000. case VIDIOC_ENUM_FMT:
  1001. {
  1002. struct v4l2_fmtdesc *fmtd = arg;
  1003. if (fmtd->index != 0)
  1004. return -EINVAL;
  1005. memset(fmtd, 0, sizeof(*fmtd));
  1006. fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1007. strcpy(fmtd->description, "Packed YUY2");
  1008. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  1009. memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
  1010. return 0;
  1011. }
  1012. case VIDIOC_G_FMT:
  1013. {
  1014. struct v4l2_format *format = arg;
  1015. em2820_videodbg("VIDIOC_G_FMT: type=%s",
  1016. format->type ==
  1017. V4L2_BUF_TYPE_VIDEO_CAPTURE ?
  1018. "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
  1019. V4L2_BUF_TYPE_VBI_CAPTURE ?
  1020. "V4L2_BUF_TYPE_VBI_CAPTURE " :
  1021. "not supported");
  1022. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1023. return -EINVAL;
  1024. format->fmt.pix.width = dev->width;
  1025. format->fmt.pix.height = dev->height;
  1026. format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  1027. format->fmt.pix.bytesperline = dev->bytesperline;
  1028. format->fmt.pix.sizeimage = dev->frame_size;
  1029. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1030. format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  1031. em2820_videodbg("VIDIOC_G_FMT: %dx%d", dev->width,
  1032. dev->height);
  1033. return 0;
  1034. }
  1035. case VIDIOC_TRY_FMT:
  1036. case VIDIOC_S_FMT:
  1037. {
  1038. struct v4l2_format *format = arg;
  1039. u32 i;
  1040. int ret = 0;
  1041. int width = format->fmt.pix.width;
  1042. int height = format->fmt.pix.height;
  1043. unsigned int hscale, vscale;
  1044. unsigned int maxh, maxw;
  1045. maxw = norm_maxw(dev);
  1046. maxh = norm_maxh(dev);
  1047. /* int both_fields; */
  1048. em2820_videodbg("%s: type=%s",
  1049. cmd ==
  1050. VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
  1051. "VIDIOC_S_FMT",
  1052. format->type ==
  1053. V4L2_BUF_TYPE_VIDEO_CAPTURE ?
  1054. "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
  1055. V4L2_BUF_TYPE_VBI_CAPTURE ?
  1056. "V4L2_BUF_TYPE_VBI_CAPTURE " :
  1057. "not supported");
  1058. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1059. return -EINVAL;
  1060. em2820_videodbg("%s: requested %dx%d",
  1061. cmd ==
  1062. VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
  1063. "VIDIOC_S_FMT", format->fmt.pix.width,
  1064. format->fmt.pix.height);
  1065. /* FIXME: Move some code away from here */
  1066. /* width must even because of the YUYV format */
  1067. /* height must be even because of interlacing */
  1068. height &= 0xfffe;
  1069. width &= 0xfffe;
  1070. if (height < 32)
  1071. height = 32;
  1072. if (height > maxh)
  1073. height = maxh;
  1074. if (width < 48)
  1075. width = 48;
  1076. if (width > maxw)
  1077. width = maxw;
  1078. if ((hscale =
  1079. (((unsigned long)maxw) << 12) / width - 4096L) >=
  1080. 0x4000)
  1081. hscale = 0x3fff;
  1082. width =
  1083. (((unsigned long)maxw) << 12) / (hscale + 4096L);
  1084. if ((vscale =
  1085. (((unsigned long)maxh) << 12) / height - 4096L) >=
  1086. 0x4000)
  1087. vscale = 0x3fff;
  1088. height =
  1089. (((unsigned long)maxh) << 12) / (vscale + 4096L);
  1090. format->fmt.pix.width = width;
  1091. format->fmt.pix.height = height;
  1092. format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  1093. format->fmt.pix.bytesperline = width * 2;
  1094. format->fmt.pix.sizeimage = width * 2 * height;
  1095. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1096. format->fmt.pix.field = V4L2_FIELD_INTERLACED;
  1097. em2820_videodbg("%s: returned %dx%d (%d, %d)",
  1098. cmd ==
  1099. VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
  1100. "VIDIOC_S_FMT", format->fmt.pix.width,
  1101. format->fmt.pix.height, hscale, vscale);
  1102. if (cmd == VIDIOC_TRY_FMT)
  1103. return 0;
  1104. for (i = 0; i < dev->num_frames; i++)
  1105. if (dev->frame[i].vma_use_count) {
  1106. em2820_videodbg("VIDIOC_S_FMT failed. "
  1107. "Unmap the buffers first.");
  1108. return -EINVAL;
  1109. }
  1110. /* stop io in case it is already in progress */
  1111. if (dev->stream == STREAM_ON) {
  1112. em2820_videodbg("VIDIOC_SET_FMT: interupting stream");
  1113. if ((ret = em2820_stream_interrupt(dev)))
  1114. return ret;
  1115. }
  1116. em2820_release_buffers(dev);
  1117. dev->io = IO_NONE;
  1118. /* set new image size */
  1119. dev->width = width;
  1120. dev->height = height;
  1121. dev->frame_size = dev->width * dev->height * 2;
  1122. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  1123. dev->bytesperline = dev->width * 2;
  1124. dev->hscale = hscale;
  1125. dev->vscale = vscale;
  1126. /* dev->both_fileds = both_fileds; */
  1127. em2820_uninit_isoc(dev);
  1128. em2820_set_alternate(dev);
  1129. em2820_capture_start(dev, 1);
  1130. em2820_resolution_set(dev);
  1131. em2820_init_isoc(dev);
  1132. return 0;
  1133. }
  1134. /* --- streaming capture ------------------------------------- */
  1135. case VIDIOC_REQBUFS:
  1136. {
  1137. struct v4l2_requestbuffers *rb = arg;
  1138. u32 i;
  1139. int ret;
  1140. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1141. rb->memory != V4L2_MEMORY_MMAP)
  1142. return -EINVAL;
  1143. if (dev->io == IO_READ) {
  1144. em2820_videodbg ("method is set to read;"
  1145. " close and open the device again to"
  1146. " choose the mmap I/O method");
  1147. return -EINVAL;
  1148. }
  1149. for (i = 0; i < dev->num_frames; i++)
  1150. if (dev->frame[i].vma_use_count) {
  1151. em2820_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped");
  1152. return -EINVAL;
  1153. }
  1154. if (dev->stream == STREAM_ON) {
  1155. em2820_videodbg("VIDIOC_REQBUFS: interrupting stream");
  1156. if ((ret = em2820_stream_interrupt(dev)))
  1157. return ret;
  1158. }
  1159. em2820_empty_framequeues(dev);
  1160. em2820_release_buffers(dev);
  1161. if (rb->count)
  1162. rb->count =
  1163. em2820_request_buffers(dev, rb->count);
  1164. dev->frame_current = NULL;
  1165. em2820_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i",
  1166. rb->count);
  1167. dev->io = rb->count ? IO_MMAP : IO_NONE;
  1168. return 0;
  1169. }
  1170. case VIDIOC_QUERYBUF:
  1171. {
  1172. struct v4l2_buffer *b = arg;
  1173. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1174. b->index >= dev->num_frames || dev->io != IO_MMAP)
  1175. return -EINVAL;
  1176. memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
  1177. if (dev->frame[b->index].vma_use_count) {
  1178. b->flags |= V4L2_BUF_FLAG_MAPPED;
  1179. }
  1180. if (dev->frame[b->index].state == F_DONE)
  1181. b->flags |= V4L2_BUF_FLAG_DONE;
  1182. else if (dev->frame[b->index].state != F_UNUSED)
  1183. b->flags |= V4L2_BUF_FLAG_QUEUED;
  1184. return 0;
  1185. }
  1186. case VIDIOC_QBUF:
  1187. {
  1188. struct v4l2_buffer *b = arg;
  1189. unsigned long lock_flags;
  1190. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1191. b->index >= dev->num_frames || dev->io != IO_MMAP) {
  1192. return -EINVAL;
  1193. }
  1194. if (dev->frame[b->index].state != F_UNUSED) {
  1195. return -EAGAIN;
  1196. }
  1197. dev->frame[b->index].state = F_QUEUED;
  1198. /* add frame to fifo */
  1199. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  1200. list_add_tail(&dev->frame[b->index].frame,
  1201. &dev->inqueue);
  1202. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  1203. return 0;
  1204. }
  1205. case VIDIOC_DQBUF:
  1206. {
  1207. struct v4l2_buffer *b = arg;
  1208. struct em2820_frame_t *f;
  1209. unsigned long lock_flags;
  1210. int ret = 0;
  1211. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1212. || dev->io != IO_MMAP)
  1213. return -EINVAL;
  1214. if (list_empty(&dev->outqueue)) {
  1215. if (dev->stream == STREAM_OFF)
  1216. return -EINVAL;
  1217. if (filp->f_flags & O_NONBLOCK)
  1218. return -EAGAIN;
  1219. ret = wait_event_interruptible
  1220. (dev->wait_frame,
  1221. (!list_empty(&dev->outqueue)) ||
  1222. (dev->state & DEV_DISCONNECTED));
  1223. if (ret)
  1224. return ret;
  1225. if (dev->state & DEV_DISCONNECTED)
  1226. return -ENODEV;
  1227. }
  1228. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  1229. f = list_entry(dev->outqueue.next,
  1230. struct em2820_frame_t, frame);
  1231. list_del(dev->outqueue.next);
  1232. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  1233. f->state = F_UNUSED;
  1234. memcpy(b, &f->buf, sizeof(*b));
  1235. if (f->vma_use_count)
  1236. b->flags |= V4L2_BUF_FLAG_MAPPED;
  1237. return 0;
  1238. }
  1239. default:
  1240. return em2820_do_ioctl(inode, filp, dev, cmd, arg,
  1241. em2820_video_do_ioctl);
  1242. }
  1243. return 0;
  1244. }
  1245. /*
  1246. * em2820_v4l2_ioctl()
  1247. * handle v4l2 ioctl the main action happens in em2820_v4l2_do_ioctl()
  1248. */
  1249. static int em2820_v4l2_ioctl(struct inode *inode, struct file *filp,
  1250. unsigned int cmd, unsigned long arg)
  1251. {
  1252. struct em2820 *dev = video_get_drvdata(video_devdata(filp));
  1253. int ret = 0;
  1254. if (down_interruptible(&dev->fileop_lock))
  1255. return -ERESTARTSYS;
  1256. if (dev->state & DEV_DISCONNECTED) {
  1257. em2820_errdev("v4l2 ioctl: device not present\n");
  1258. up(&dev->fileop_lock);
  1259. return -ENODEV;
  1260. }
  1261. if (dev->state & DEV_MISCONFIGURED) {
  1262. em2820_errdev
  1263. ("v4l2 ioctl: device is misconfigured; close and open it again\n");
  1264. up(&dev->fileop_lock);
  1265. return -EIO;
  1266. }
  1267. ret = video_usercopy(inode, filp, cmd, arg, em2820_video_do_ioctl);
  1268. up(&dev->fileop_lock);
  1269. return ret;
  1270. }
  1271. static struct file_operations em2820_v4l_fops = {
  1272. .owner = THIS_MODULE,
  1273. .open = em2820_v4l2_open,
  1274. .release = em2820_v4l2_close,
  1275. .ioctl = em2820_v4l2_ioctl,
  1276. .read = em2820_v4l2_read,
  1277. .poll = em2820_v4l2_poll,
  1278. .mmap = em2820_v4l2_mmap,
  1279. .llseek = no_llseek,
  1280. };
  1281. /******************************** usb interface *****************************************/
  1282. /*
  1283. * em2820_init_dev()
  1284. * allocates and inits the device structs, registers i2c bus and v4l device
  1285. */
  1286. static int em2820_init_dev(struct em2820 **devhandle, struct usb_device *udev,
  1287. int minor, int model)
  1288. {
  1289. struct em2820 *dev;
  1290. int retval = -ENOMEM;
  1291. int errCode, i;
  1292. unsigned int maxh, maxw;
  1293. struct usb_interface *uif;
  1294. /* allocate memory for our device state and initialize it */
  1295. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  1296. if (dev == NULL) {
  1297. em2820_err(DRIVER_NAME ": out of memory!\n");
  1298. return -ENOMEM;
  1299. }
  1300. memset(dev, 0x00, sizeof(*dev));
  1301. snprintf(dev->name, 29, "em2820 #%d", minor);
  1302. dev->udev = udev;
  1303. dev->model = model;
  1304. init_MUTEX(&dev->lock);
  1305. init_waitqueue_head(&dev->open);
  1306. dev->em2820_write_regs = em2820_write_regs;
  1307. dev->em2820_read_reg = em2820_read_reg;
  1308. dev->em2820_read_reg_req_len = em2820_read_reg_req_len;
  1309. dev->em2820_write_regs_req = em2820_write_regs_req;
  1310. dev->em2820_read_reg_req = em2820_read_reg_req;
  1311. dev->has_tuner = em2820_boards[model].has_tuner;
  1312. dev->has_msp34xx = em2820_boards[model].has_msp34xx;
  1313. dev->tda9887_conf = em2820_boards[model].tda9887_conf;
  1314. dev->decoder = em2820_boards[model].decoder;
  1315. if (tuner >= 0)
  1316. dev->tuner_type = tuner;
  1317. else
  1318. dev->tuner_type = em2820_boards[model].tuner_type;
  1319. dev->video_inputs = em2820_boards[model].vchannels;
  1320. for (i = 0; i < TVNORMS; i++)
  1321. if (em2820_boards[model].norm == tvnorms[i].mode)
  1322. break;
  1323. if (i == TVNORMS)
  1324. i = 0;
  1325. dev->tvnorm = &tvnorms[i]; /* set default norm */
  1326. em2820_videodbg("tvnorm=%s\n", dev->tvnorm->name);
  1327. maxw = norm_maxw(dev);
  1328. maxh = norm_maxh(dev);
  1329. /* set default image size */
  1330. dev->width = maxw;
  1331. dev->height = maxh;
  1332. dev->interlaced = EM2820_INTERLACED_DEFAULT;
  1333. dev->field_size = dev->width * dev->height;
  1334. dev->frame_size =
  1335. dev->interlaced ? dev->field_size << 1 : dev->field_size;
  1336. dev->bytesperline = dev->width * 2;
  1337. dev->hscale = 0;
  1338. dev->vscale = 0;
  1339. dev->ctl_input = 2;
  1340. /* setup video picture settings for saa7113h */
  1341. memset(&dev->vpic, 0, sizeof(dev->vpic));
  1342. dev->vpic.colour = 128 << 8;
  1343. dev->vpic.hue = 128 << 8;
  1344. dev->vpic.brightness = 128 << 8;
  1345. dev->vpic.contrast = 192 << 8;
  1346. dev->vpic.whiteness = 128 << 8; /* This one isn't used */
  1347. dev->vpic.depth = 16;
  1348. dev->vpic.palette = VIDEO_PALETTE_YUV422;
  1349. /* compute alternate max packet sizes */
  1350. uif = dev->udev->actconfig->interface[0];
  1351. dev->alt_max_pkt_size[0] = 0;
  1352. for (i = 1; i <= EM2820_MAX_ALT; i++) {
  1353. u16 tmp =
  1354. le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1355. wMaxPacketSize);
  1356. dev->alt_max_pkt_size[i] =
  1357. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1358. }
  1359. #ifdef CONFIG_MODULES
  1360. /* request some modules */
  1361. if (dev->decoder == EM2820_SAA7113)
  1362. request_module("saa7113");
  1363. if (dev->decoder == EM2820_SAA7114)
  1364. request_module("saa7114");
  1365. if (dev->decoder == EM2820_TVP5150)
  1366. request_module("tvp5150");
  1367. if (dev->has_tuner)
  1368. request_module("tuner");
  1369. if (dev->tda9887_conf)
  1370. request_module("tda9887");
  1371. #endif
  1372. errCode = em2820_config(dev);
  1373. if (errCode) {
  1374. em2820_errdev("error configuring device\n");
  1375. kfree(dev);
  1376. return -ENOMEM;
  1377. }
  1378. down(&dev->lock);
  1379. /* register i2c bus */
  1380. em2820_i2c_register(dev);
  1381. /* Do board specific init and eeprom reading */
  1382. em2820_card_setup(dev);
  1383. /* configure the device */
  1384. em2820_config_i2c(dev);
  1385. up(&dev->lock);
  1386. errCode = em2820_config(dev);
  1387. #ifdef CONFIG_MODULES
  1388. if (dev->has_msp34xx)
  1389. request_module("msp3400");
  1390. #endif
  1391. /* allocate and fill v4l2 device struct */
  1392. dev->vdev = video_device_alloc();
  1393. if (NULL == dev->vdev) {
  1394. em2820_errdev("cannot allocate video_device.\n");
  1395. kfree(dev);
  1396. return -ENOMEM;
  1397. }
  1398. dev->vdev->owner = THIS_MODULE;
  1399. dev->vdev->type = VID_TYPE_CAPTURE;
  1400. if (dev->has_tuner)
  1401. dev->vdev->type |= VID_TYPE_TUNER;
  1402. dev->vdev->hardware = 0;
  1403. dev->vdev->fops = &em2820_v4l_fops;
  1404. dev->vdev->minor = -1;
  1405. dev->vdev->dev = &dev->udev->dev;
  1406. dev->vdev->release = video_device_release;
  1407. snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
  1408. "em2820 video");
  1409. video_set_drvdata(dev->vdev, dev);
  1410. /* register v4l2 device */
  1411. down(&dev->lock);
  1412. if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
  1413. em2820_errdev("unable to register video device (error=%i).\n",
  1414. retval);
  1415. up(&dev->lock);
  1416. video_set_drvdata(dev->vdev, NULL);
  1417. video_device_release(dev->vdev);
  1418. kfree(dev);
  1419. return -ENODEV;
  1420. }
  1421. if (dev->has_msp34xx) {
  1422. /* Send a reset to other chips via gpio */
  1423. em2820_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
  1424. udelay(2500);
  1425. em2820_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
  1426. udelay(2500);
  1427. }
  1428. video_mux(dev, 0);
  1429. up(&dev->lock);
  1430. em2820_info("V4L2 device registered as /dev/video%d\n",
  1431. dev->vdev->minor);
  1432. *devhandle = dev;
  1433. return 0;
  1434. }
  1435. /*
  1436. * em2820_usb_probe()
  1437. * checks for supported devices
  1438. */
  1439. static int em2820_usb_probe(struct usb_interface *interface,
  1440. const struct usb_device_id *id)
  1441. {
  1442. const struct usb_endpoint_descriptor *endpoint;
  1443. struct usb_device *udev;
  1444. struct em2820 *dev = NULL;
  1445. int retval = -ENODEV;
  1446. udev = usb_get_dev(interface_to_usbdev(interface));
  1447. endpoint = &interface->cur_altsetting->endpoint[1].desc;
  1448. /* check if the the device has the iso in endpoint at the correct place */
  1449. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1450. USB_ENDPOINT_XFER_ISOC) {
  1451. /* em2820_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n"); */
  1452. return -ENODEV;
  1453. }
  1454. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1455. /* em2820_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n"); */
  1456. return -ENODEV;
  1457. }
  1458. /* allocate device struct */
  1459. retval = em2820_init_dev(&dev, udev, interface->minor, id->driver_info);
  1460. if (retval)
  1461. return retval;
  1462. em2820_info("Found %s\n", em2820_boards[id->driver_info].name);
  1463. /* save our data pointer in this interface device */
  1464. usb_set_intfdata(interface, dev);
  1465. return 0;
  1466. }
  1467. /*
  1468. * em2820_usb_disconnect()
  1469. * called when the device gets diconencted
  1470. * video device will be unregistered on v4l2_close in case it is still open
  1471. */
  1472. static void em2820_usb_disconnect(struct usb_interface *interface)
  1473. {
  1474. struct em2820 *dev = usb_get_intfdata(interface);
  1475. usb_set_intfdata(interface, NULL);
  1476. if (!dev)
  1477. return;
  1478. down_write(&em2820_disconnect);
  1479. down(&dev->lock);
  1480. em2820_info("disconnecting %s\n", dev->vdev->name);
  1481. wake_up_interruptible_all(&dev->open);
  1482. if (dev->users) {
  1483. em2820_warn
  1484. ("device /dev/video%d is open! Deregistration and memory "
  1485. "deallocation are deferred on close.\n", dev->vdev->minor);
  1486. dev->state |= DEV_MISCONFIGURED;
  1487. em2820_uninit_isoc(dev);
  1488. dev->state |= DEV_DISCONNECTED;
  1489. wake_up_interruptible(&dev->wait_frame);
  1490. wake_up_interruptible(&dev->wait_stream);
  1491. } else {
  1492. dev->state |= DEV_DISCONNECTED;
  1493. em2820_release_resources(dev);
  1494. }
  1495. up(&dev->lock);
  1496. if (!dev->users)
  1497. kfree(dev);
  1498. up_write(&em2820_disconnect);
  1499. }
  1500. static struct usb_driver em2820_usb_driver = {
  1501. .owner = THIS_MODULE,
  1502. .name = "em2820",
  1503. .probe = em2820_usb_probe,
  1504. .disconnect = em2820_usb_disconnect,
  1505. .id_table = em2820_id_table,
  1506. };
  1507. static int __init em2820_module_init(void)
  1508. {
  1509. int result;
  1510. printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
  1511. (EM2820_VERSION_CODE >> 16) & 0xff,
  1512. (EM2820_VERSION_CODE >> 8) & 0xff, EM2820_VERSION_CODE & 0xff);
  1513. #ifdef SNAPSHOT
  1514. printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
  1515. SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
  1516. #endif
  1517. /* register this driver with the USB subsystem */
  1518. result = usb_register(&em2820_usb_driver);
  1519. if (result)
  1520. em2820_err(DRIVER_NAME
  1521. " usb_register failed. Error number %d.\n", result);
  1522. return result;
  1523. }
  1524. static void __exit em2820_module_exit(void)
  1525. {
  1526. /* deregister this driver with the USB subsystem */
  1527. usb_deregister(&em2820_usb_driver);
  1528. }
  1529. module_init(em2820_module_init);
  1530. module_exit(em2820_module_exit);