em28xx-video.c 44 KB

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