em28xx-video.c 45 KB

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