em28xx-video.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  1. /*
  2. em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
  3. Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  4. Markus Rechberger <mrechberger@gmail.com>
  5. Mauro Carvalho Chehab <mchehab@brturbo.com.br>
  6. Sascha Sommer <saschasommer@freenet.de>
  7. Some parts based on SN9C10x PC Camera Controllers GPL driver made
  8. by Luca Risolia <luca.risolia@studio.unibo.it>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/bitmap.h>
  26. #include <linux/usb.h>
  27. #include <linux/i2c.h>
  28. #include <linux/version.h>
  29. #include <linux/video_decoder.h>
  30. #include <linux/mutex.h>
  31. #include "em28xx.h"
  32. #include <media/tuner.h>
  33. #include <media/v4l2-common.h>
  34. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  35. "Markus Rechberger <mrechberger@gmail.com>, " \
  36. "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \
  37. "Sascha Sommer <saschasommer@freenet.de>"
  38. #define DRIVER_NAME "em28xx"
  39. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  40. #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
  41. #define em28xx_videodbg(fmt, arg...) do {\
  42. if (video_debug) \
  43. printk(KERN_INFO "%s %s :"fmt, \
  44. dev->name, __FUNCTION__ , ##arg); } while (0)
  45. MODULE_AUTHOR(DRIVER_AUTHOR);
  46. MODULE_DESCRIPTION(DRIVER_DESC);
  47. MODULE_LICENSE("GPL");
  48. static LIST_HEAD(em28xx_devlist);
  49. static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  50. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  51. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  52. module_param_array(card, int, NULL, 0444);
  53. module_param_array(video_nr, int, NULL, 0444);
  54. module_param_array(vbi_nr, int, NULL, 0444);
  55. MODULE_PARM_DESC(card,"card type");
  56. MODULE_PARM_DESC(video_nr,"video device numbers");
  57. MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
  58. static int tuner = -1;
  59. module_param(tuner, int, 0444);
  60. MODULE_PARM_DESC(tuner, "tuner type");
  61. static unsigned int video_debug = 0;
  62. module_param(video_debug,int,0644);
  63. MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
  64. /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
  65. static unsigned long em28xx_devused;
  66. /* supported tv norms */
  67. static struct em28xx_tvnorm tvnorms[] = {
  68. {
  69. .name = "PAL",
  70. .id = V4L2_STD_PAL,
  71. .mode = VIDEO_MODE_PAL,
  72. }, {
  73. .name = "NTSC",
  74. .id = V4L2_STD_NTSC,
  75. .mode = VIDEO_MODE_NTSC,
  76. }, {
  77. .name = "SECAM",
  78. .id = V4L2_STD_SECAM,
  79. .mode = VIDEO_MODE_SECAM,
  80. }, {
  81. .name = "PAL-M",
  82. .id = V4L2_STD_PAL_M,
  83. .mode = VIDEO_MODE_PAL,
  84. }
  85. };
  86. static const unsigned char saa7114_i2c_init[] = {
  87. 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0,
  88. 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a,
  89. 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42,
  90. 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff,
  91. 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff,
  92. 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff,
  93. 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00,
  94. 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00,
  95. 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0,
  96. 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06,
  97. 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67,
  98. 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd,
  99. 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00,
  100. 0xbe,0x00,0xbf,0x00
  101. };
  102. #define TVNORMS ARRAY_SIZE(tvnorms)
  103. /* supported controls */
  104. /* Common to all boards */
  105. static struct v4l2_queryctrl em28xx_qctrl[] = {
  106. {
  107. .id = V4L2_CID_AUDIO_VOLUME,
  108. .type = V4L2_CTRL_TYPE_INTEGER,
  109. .name = "Volume",
  110. .minimum = 0x0,
  111. .maximum = 0x1f,
  112. .step = 0x1,
  113. .default_value = 0x1f,
  114. .flags = 0,
  115. },{
  116. .id = V4L2_CID_AUDIO_MUTE,
  117. .type = V4L2_CTRL_TYPE_BOOLEAN,
  118. .name = "Mute",
  119. .minimum = 0,
  120. .maximum = 1,
  121. .step = 1,
  122. .default_value = 1,
  123. .flags = 0,
  124. }
  125. };
  126. /* FIXME: These are specific to saa711x - should be moved to its code */
  127. static struct v4l2_queryctrl saa711x_qctrl[] = {
  128. {
  129. .id = V4L2_CID_BRIGHTNESS,
  130. .type = V4L2_CTRL_TYPE_INTEGER,
  131. .name = "Brightness",
  132. .minimum = -128,
  133. .maximum = 127,
  134. .step = 1,
  135. .default_value = 0,
  136. .flags = 0,
  137. },{
  138. .id = V4L2_CID_CONTRAST,
  139. .type = V4L2_CTRL_TYPE_INTEGER,
  140. .name = "Contrast",
  141. .minimum = 0x0,
  142. .maximum = 0x1f,
  143. .step = 0x1,
  144. .default_value = 0x10,
  145. .flags = 0,
  146. },{
  147. .id = V4L2_CID_SATURATION,
  148. .type = V4L2_CTRL_TYPE_INTEGER,
  149. .name = "Saturation",
  150. .minimum = 0x0,
  151. .maximum = 0x1f,
  152. .step = 0x1,
  153. .default_value = 0x10,
  154. .flags = 0,
  155. },{
  156. .id = V4L2_CID_RED_BALANCE,
  157. .type = V4L2_CTRL_TYPE_INTEGER,
  158. .name = "Red chroma balance",
  159. .minimum = -128,
  160. .maximum = 127,
  161. .step = 1,
  162. .default_value = 0,
  163. .flags = 0,
  164. },{
  165. .id = V4L2_CID_BLUE_BALANCE,
  166. .type = V4L2_CTRL_TYPE_INTEGER,
  167. .name = "Blue chroma balance",
  168. .minimum = -128,
  169. .maximum = 127,
  170. .step = 1,
  171. .default_value = 0,
  172. .flags = 0,
  173. },{
  174. .id = V4L2_CID_GAMMA,
  175. .type = V4L2_CTRL_TYPE_INTEGER,
  176. .name = "Gamma",
  177. .minimum = 0x0,
  178. .maximum = 0x3f,
  179. .step = 0x1,
  180. .default_value = 0x20,
  181. .flags = 0,
  182. }
  183. };
  184. static struct usb_driver em28xx_usb_driver;
  185. static DEFINE_MUTEX(em28xx_sysfs_lock);
  186. static DECLARE_RWSEM(em28xx_disconnect);
  187. /********************* v4l2 interface ******************************************/
  188. /*
  189. * em28xx_config()
  190. * inits registers with sane defaults
  191. */
  192. static int em28xx_config(struct em28xx *dev)
  193. {
  194. /* Sets I2C speed to 100 KHz */
  195. em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
  196. /* enable vbi capturing */
  197. em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1);
  198. em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1);
  199. em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
  200. em28xx_audio_usb_mute(dev, 1);
  201. dev->mute = 1; /* maybe not the right place... */
  202. dev->volume = 0x1f;
  203. em28xx_audio_analog_set(dev);
  204. em28xx_audio_analog_setup(dev);
  205. em28xx_outfmt_set_yuv422(dev);
  206. em28xx_colorlevels_set_default(dev);
  207. em28xx_compression_disable(dev);
  208. return 0;
  209. }
  210. /*
  211. * em28xx_config_i2c()
  212. * configure i2c attached devices
  213. */
  214. static void em28xx_config_i2c(struct em28xx *dev)
  215. {
  216. struct v4l2_frequency f;
  217. struct video_decoder_init em28xx_vdi = {.data = NULL };
  218. /* configure decoder */
  219. if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){
  220. em28xx_vdi.data=saa7114_i2c_init;
  221. em28xx_vdi.len=sizeof(saa7114_i2c_init);
  222. }
  223. em28xx_i2c_call_clients(dev, DECODER_INIT, &em28xx_vdi);
  224. em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
  225. /* em28xx_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
  226. /* em28xx_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
  227. /* em28xx_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
  228. /* em28xx_i2c_call_clients(dev,DECODER_DUMP, NULL); */
  229. /* configure tuner */
  230. f.tuner = 0;
  231. f.type = V4L2_TUNER_ANALOG_TV;
  232. f.frequency = 9076; /* FIXME:remove magic number */
  233. dev->ctl_freq = f.frequency;
  234. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
  235. /* configure tda9887 */
  236. /* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
  237. }
  238. /*
  239. * em28xx_empty_framequeues()
  240. * prepare queues for incoming and outgoing frames
  241. */
  242. static void em28xx_empty_framequeues(struct em28xx *dev)
  243. {
  244. u32 i;
  245. INIT_LIST_HEAD(&dev->inqueue);
  246. INIT_LIST_HEAD(&dev->outqueue);
  247. for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
  248. dev->frame[i].state = F_UNUSED;
  249. dev->frame[i].buf.bytesused = 0;
  250. }
  251. }
  252. static void video_mux(struct em28xx *dev, int index)
  253. {
  254. int input, ainput;
  255. input = INPUT(index)->vmux;
  256. dev->ctl_input = index;
  257. dev->ctl_ainput = INPUT(index)->amux;
  258. em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
  259. em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,input,dev->ctl_ainput);
  260. if (dev->has_msp34xx) {
  261. if (dev->i2s_speed)
  262. em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
  263. em28xx_i2c_call_clients(dev, VIDIOC_S_AUDIO, &dev->ctl_ainput);
  264. ainput = EM28XX_AUDIO_SRC_TUNER;
  265. em28xx_audio_source(dev, ainput);
  266. } else {
  267. switch (dev->ctl_ainput) {
  268. case 0:
  269. ainput = EM28XX_AUDIO_SRC_TUNER;
  270. break;
  271. default:
  272. ainput = EM28XX_AUDIO_SRC_LINE;
  273. }
  274. em28xx_audio_source(dev, ainput);
  275. }
  276. }
  277. /*
  278. * em28xx_v4l2_open()
  279. * inits the device and starts isoc transfer
  280. */
  281. static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
  282. {
  283. int minor = iminor(inode);
  284. int errCode = 0;
  285. struct em28xx *h,*dev = NULL;
  286. struct list_head *list;
  287. list_for_each(list,&em28xx_devlist) {
  288. h = list_entry(list, struct em28xx, devlist);
  289. if (h->vdev->minor == minor) {
  290. dev = h;
  291. dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  292. }
  293. if (h->vbi_dev->minor == minor) {
  294. dev = h;
  295. dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  296. }
  297. }
  298. if (NULL == dev)
  299. return -ENODEV;
  300. filp->private_data=dev;
  301. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  302. minor,v4l2_type_names[dev->type],dev->users);
  303. if (!down_read_trylock(&em28xx_disconnect))
  304. return -ERESTARTSYS;
  305. if (dev->users) {
  306. em28xx_warn("this driver can be opened only once\n");
  307. up_read(&em28xx_disconnect);
  308. return -EBUSY;
  309. }
  310. init_MUTEX(&dev->fileop_lock); /* to 1 == available */
  311. spin_lock_init(&dev->queue_lock);
  312. init_waitqueue_head(&dev->wait_frame);
  313. init_waitqueue_head(&dev->wait_stream);
  314. down(&dev->lock);
  315. if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  316. em28xx_set_alternate(dev);
  317. dev->width = norm_maxw(dev);
  318. dev->height = norm_maxh(dev);
  319. dev->frame_size = dev->width * dev->height * 2;
  320. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  321. dev->bytesperline = dev->width * 2;
  322. dev->hscale = 0;
  323. dev->vscale = 0;
  324. em28xx_capture_start(dev, 1);
  325. em28xx_resolution_set(dev);
  326. /* start the transfer */
  327. errCode = em28xx_init_isoc(dev);
  328. if (errCode)
  329. goto err;
  330. video_mux(dev, 0);
  331. }
  332. dev->users++;
  333. filp->private_data = dev;
  334. dev->io = IO_NONE;
  335. dev->stream = STREAM_OFF;
  336. dev->num_frames = 0;
  337. /* prepare queues */
  338. em28xx_empty_framequeues(dev);
  339. dev->state |= DEV_INITIALIZED;
  340. err:
  341. up(&dev->lock);
  342. up_read(&em28xx_disconnect);
  343. return errCode;
  344. }
  345. /*
  346. * em28xx_realease_resources()
  347. * unregisters the v4l2,i2c and usb devices
  348. * called when the device gets disconected or at module unload
  349. */
  350. static void em28xx_release_resources(struct em28xx *dev)
  351. {
  352. mutex_lock(&em28xx_sysfs_lock);
  353. /*FIXME: I2C IR should be disconnected */
  354. em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
  355. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
  356. dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
  357. list_del(&dev->devlist);
  358. video_unregister_device(dev->vdev);
  359. video_unregister_device(dev->vbi_dev);
  360. em28xx_i2c_unregister(dev);
  361. usb_put_dev(dev->udev);
  362. mutex_unlock(&em28xx_sysfs_lock);
  363. /* Mark device as unused */
  364. em28xx_devused&=~(1<<dev->devno);
  365. }
  366. /*
  367. * em28xx_v4l2_close()
  368. * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
  369. */
  370. static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
  371. {
  372. int errCode;
  373. struct em28xx *dev=filp->private_data;
  374. em28xx_videodbg("users=%d\n", dev->users);
  375. down(&dev->lock);
  376. em28xx_uninit_isoc(dev);
  377. em28xx_release_buffers(dev);
  378. /* the device is already disconnect, free the remaining resources */
  379. if (dev->state & DEV_DISCONNECTED) {
  380. em28xx_release_resources(dev);
  381. up(&dev->lock);
  382. kfree(dev);
  383. return 0;
  384. }
  385. /* set alternate 0 */
  386. dev->alt = 0;
  387. em28xx_videodbg("setting alternate 0\n");
  388. errCode = usb_set_interface(dev->udev, 0, 0);
  389. if (errCode < 0) {
  390. em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
  391. errCode);
  392. }
  393. dev->users--;
  394. wake_up_interruptible_nr(&dev->open, 1);
  395. up(&dev->lock);
  396. return 0;
  397. }
  398. /*
  399. * em28xx_v4l2_read()
  400. * will allocate buffers when called for the first time
  401. */
  402. static ssize_t
  403. em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
  404. loff_t * f_pos)
  405. {
  406. struct em28xx_frame_t *f, *i;
  407. unsigned long lock_flags;
  408. int ret = 0;
  409. struct em28xx *dev = filp->private_data;
  410. if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  411. em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
  412. }
  413. if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  414. em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
  415. em28xx_videodbg("not supported yet! ...\n");
  416. if (copy_to_user(buf, "", 1)) {
  417. up(&dev->fileop_lock);
  418. return -EFAULT;
  419. }
  420. return (1);
  421. }
  422. if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
  423. em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
  424. em28xx_videodbg("not supported yet! ...\n");
  425. if (copy_to_user(buf, "", 1)) {
  426. up(&dev->fileop_lock);
  427. return -EFAULT;
  428. }
  429. return (1);
  430. }
  431. if (down_interruptible(&dev->fileop_lock))
  432. return -ERESTARTSYS;
  433. if (dev->state & DEV_DISCONNECTED) {
  434. em28xx_videodbg("device not present\n");
  435. up(&dev->fileop_lock);
  436. return -ENODEV;
  437. }
  438. if (dev->state & DEV_MISCONFIGURED) {
  439. em28xx_videodbg("device misconfigured; close and open it again\n");
  440. up(&dev->fileop_lock);
  441. return -EIO;
  442. }
  443. if (dev->io == IO_MMAP) {
  444. em28xx_videodbg ("IO method is set to mmap; close and open"
  445. " the device again to choose the read method\n");
  446. up(&dev->fileop_lock);
  447. return -EINVAL;
  448. }
  449. if (dev->io == IO_NONE) {
  450. if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
  451. em28xx_errdev("read failed, not enough memory\n");
  452. up(&dev->fileop_lock);
  453. return -ENOMEM;
  454. }
  455. dev->io = IO_READ;
  456. dev->stream = STREAM_ON;
  457. em28xx_queue_unusedframes(dev);
  458. }
  459. if (!count) {
  460. up(&dev->fileop_lock);
  461. return 0;
  462. }
  463. if (list_empty(&dev->outqueue)) {
  464. if (filp->f_flags & O_NONBLOCK) {
  465. up(&dev->fileop_lock);
  466. return -EAGAIN;
  467. }
  468. ret = wait_event_interruptible
  469. (dev->wait_frame,
  470. (!list_empty(&dev->outqueue)) ||
  471. (dev->state & DEV_DISCONNECTED));
  472. if (ret) {
  473. up(&dev->fileop_lock);
  474. return ret;
  475. }
  476. if (dev->state & DEV_DISCONNECTED) {
  477. up(&dev->fileop_lock);
  478. return -ENODEV;
  479. }
  480. }
  481. f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
  482. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  483. list_for_each_entry(i, &dev->outqueue, frame)
  484. i->state = F_UNUSED;
  485. INIT_LIST_HEAD(&dev->outqueue);
  486. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  487. em28xx_queue_unusedframes(dev);
  488. if (count > f->buf.length)
  489. count = f->buf.length;
  490. if (copy_to_user(buf, f->bufmem, count)) {
  491. up(&dev->fileop_lock);
  492. return -EFAULT;
  493. }
  494. *f_pos += count;
  495. up(&dev->fileop_lock);
  496. return count;
  497. }
  498. /*
  499. * em28xx_v4l2_poll()
  500. * will allocate buffers when called for the first time
  501. */
  502. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
  503. {
  504. unsigned int mask = 0;
  505. struct em28xx *dev = filp->private_data;
  506. if (down_interruptible(&dev->fileop_lock))
  507. return POLLERR;
  508. if (dev->state & DEV_DISCONNECTED) {
  509. em28xx_videodbg("device not present\n");
  510. } else if (dev->state & DEV_MISCONFIGURED) {
  511. em28xx_videodbg("device is misconfigured; close and open it again\n");
  512. } else {
  513. if (dev->io == IO_NONE) {
  514. if (!em28xx_request_buffers
  515. (dev, EM28XX_NUM_READ_FRAMES)) {
  516. em28xx_warn
  517. ("poll() failed, not enough memory\n");
  518. } else {
  519. dev->io = IO_READ;
  520. dev->stream = STREAM_ON;
  521. }
  522. }
  523. if (dev->io == IO_READ) {
  524. em28xx_queue_unusedframes(dev);
  525. poll_wait(filp, &dev->wait_frame, wait);
  526. if (!list_empty(&dev->outqueue))
  527. mask |= POLLIN | POLLRDNORM;
  528. up(&dev->fileop_lock);
  529. return mask;
  530. }
  531. }
  532. up(&dev->fileop_lock);
  533. return POLLERR;
  534. }
  535. /*
  536. * em28xx_vm_open()
  537. */
  538. static void em28xx_vm_open(struct vm_area_struct *vma)
  539. {
  540. struct em28xx_frame_t *f = vma->vm_private_data;
  541. f->vma_use_count++;
  542. }
  543. /*
  544. * em28xx_vm_close()
  545. */
  546. static void em28xx_vm_close(struct vm_area_struct *vma)
  547. {
  548. /* NOTE: buffers are not freed here */
  549. struct em28xx_frame_t *f = vma->vm_private_data;
  550. f->vma_use_count--;
  551. }
  552. static struct vm_operations_struct em28xx_vm_ops = {
  553. .open = em28xx_vm_open,
  554. .close = em28xx_vm_close,
  555. };
  556. /*
  557. * em28xx_v4l2_mmap()
  558. */
  559. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  560. {
  561. unsigned long size = vma->vm_end - vma->vm_start,
  562. start = vma->vm_start;
  563. void *pos;
  564. u32 i;
  565. struct em28xx *dev = filp->private_data;
  566. if (down_interruptible(&dev->fileop_lock))
  567. return -ERESTARTSYS;
  568. if (dev->state & DEV_DISCONNECTED) {
  569. em28xx_videodbg("mmap: device not present\n");
  570. up(&dev->fileop_lock);
  571. return -ENODEV;
  572. }
  573. if (dev->state & DEV_MISCONFIGURED) {
  574. em28xx_videodbg ("mmap: Device is misconfigured; close and "
  575. "open it again\n");
  576. up(&dev->fileop_lock);
  577. return -EIO;
  578. }
  579. if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
  580. size != PAGE_ALIGN(dev->frame[0].buf.length)) {
  581. up(&dev->fileop_lock);
  582. return -EINVAL;
  583. }
  584. for (i = 0; i < dev->num_frames; i++) {
  585. if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
  586. break;
  587. }
  588. if (i == dev->num_frames) {
  589. em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
  590. up(&dev->fileop_lock);
  591. return -EINVAL;
  592. }
  593. /* VM_IO is eventually going to replace PageReserved altogether */
  594. vma->vm_flags |= VM_IO;
  595. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  596. pos = dev->frame[i].bufmem;
  597. while (size > 0) { /* size is page-aligned */
  598. if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
  599. em28xx_videodbg("mmap: vm_insert_page failed\n");
  600. up(&dev->fileop_lock);
  601. return -EAGAIN;
  602. }
  603. start += PAGE_SIZE;
  604. pos += PAGE_SIZE;
  605. size -= PAGE_SIZE;
  606. }
  607. vma->vm_ops = &em28xx_vm_ops;
  608. vma->vm_private_data = &dev->frame[i];
  609. em28xx_vm_open(vma);
  610. up(&dev->fileop_lock);
  611. return 0;
  612. }
  613. /*
  614. * em28xx_get_ctrl()
  615. * return the current saturation, brightness or contrast, mute state
  616. */
  617. static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  618. {
  619. switch (ctrl->id) {
  620. case V4L2_CID_AUDIO_MUTE:
  621. ctrl->value = dev->mute;
  622. return 0;
  623. case V4L2_CID_AUDIO_VOLUME:
  624. ctrl->value = dev->volume;
  625. return 0;
  626. default:
  627. return -EINVAL;
  628. }
  629. }
  630. /*FIXME: should be moved to saa711x */
  631. static int saa711x_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  632. {
  633. s32 tmp;
  634. switch (ctrl->id) {
  635. case V4L2_CID_BRIGHTNESS:
  636. if ((tmp = em28xx_brightness_get(dev)) < 0)
  637. return -EIO;
  638. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  639. return 0;
  640. case V4L2_CID_CONTRAST:
  641. if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
  642. return -EIO;
  643. return 0;
  644. case V4L2_CID_SATURATION:
  645. if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
  646. return -EIO;
  647. return 0;
  648. case V4L2_CID_RED_BALANCE:
  649. if ((tmp = em28xx_v_balance_get(dev)) < 0)
  650. return -EIO;
  651. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  652. return 0;
  653. case V4L2_CID_BLUE_BALANCE:
  654. if ((tmp = em28xx_u_balance_get(dev)) < 0)
  655. return -EIO;
  656. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  657. return 0;
  658. case V4L2_CID_GAMMA:
  659. if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
  660. return -EIO;
  661. return 0;
  662. default:
  663. return -EINVAL;
  664. }
  665. }
  666. /*
  667. * em28xx_set_ctrl()
  668. * mute or set new saturation, brightness or contrast
  669. */
  670. static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  671. {
  672. switch (ctrl->id) {
  673. case V4L2_CID_AUDIO_MUTE:
  674. if (ctrl->value != dev->mute) {
  675. dev->mute = ctrl->value;
  676. em28xx_audio_usb_mute(dev, ctrl->value);
  677. return em28xx_audio_analog_set(dev);
  678. }
  679. return 0;
  680. case V4L2_CID_AUDIO_VOLUME:
  681. dev->volume = ctrl->value;
  682. return em28xx_audio_analog_set(dev);
  683. default:
  684. return -EINVAL;
  685. }
  686. }
  687. /*FIXME: should be moved to saa711x */
  688. static int saa711x_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  689. {
  690. switch (ctrl->id) {
  691. case V4L2_CID_BRIGHTNESS:
  692. return em28xx_brightness_set(dev, ctrl->value);
  693. case V4L2_CID_CONTRAST:
  694. return em28xx_contrast_set(dev, ctrl->value);
  695. case V4L2_CID_SATURATION:
  696. return em28xx_saturation_set(dev, ctrl->value);
  697. case V4L2_CID_RED_BALANCE:
  698. return em28xx_v_balance_set(dev, ctrl->value);
  699. case V4L2_CID_BLUE_BALANCE:
  700. return em28xx_u_balance_set(dev, ctrl->value);
  701. case V4L2_CID_GAMMA:
  702. return em28xx_gamma_set(dev, ctrl->value);
  703. default:
  704. return -EINVAL;
  705. }
  706. }
  707. /*
  708. * em28xx_stream_interrupt()
  709. * stops streaming
  710. */
  711. static int em28xx_stream_interrupt(struct em28xx *dev)
  712. {
  713. int ret = 0;
  714. /* stop reading from the device */
  715. dev->stream = STREAM_INTERRUPT;
  716. ret = wait_event_timeout(dev->wait_stream,
  717. (dev->stream == STREAM_OFF) ||
  718. (dev->state & DEV_DISCONNECTED),
  719. EM28XX_URB_TIMEOUT);
  720. if (dev->state & DEV_DISCONNECTED)
  721. return -ENODEV;
  722. else if (ret) {
  723. dev->state |= DEV_MISCONFIGURED;
  724. em28xx_videodbg("device is misconfigured; close and "
  725. "open /dev/video%d again\n",
  726. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
  727. return ret;
  728. }
  729. return 0;
  730. }
  731. static int em28xx_set_norm(struct em28xx *dev, int width, int height)
  732. {
  733. unsigned int hscale, vscale;
  734. unsigned int maxh, maxw;
  735. maxw = norm_maxw(dev);
  736. maxh = norm_maxh(dev);
  737. /* width must even because of the YUYV format */
  738. /* height must be even because of interlacing */
  739. height &= 0xfffe;
  740. width &= 0xfffe;
  741. if (height < 32)
  742. height = 32;
  743. if (height > maxh)
  744. height = maxh;
  745. if (width < 48)
  746. width = 48;
  747. if (width > maxw)
  748. width = maxw;
  749. if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
  750. hscale = 0x3fff;
  751. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  752. if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
  753. vscale = 0x3fff;
  754. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  755. /* set new image size */
  756. dev->width = width;
  757. dev->height = height;
  758. dev->frame_size = dev->width * dev->height * 2;
  759. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  760. dev->bytesperline = dev->width * 2;
  761. dev->hscale = hscale;
  762. dev->vscale = vscale;
  763. em28xx_resolution_set(dev);
  764. return 0;
  765. }
  766. static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format)
  767. {
  768. em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
  769. (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
  770. "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
  771. (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ?
  772. "V4L2_BUF_TYPE_VBI_CAPTURE" :
  773. (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ?
  774. "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
  775. "not supported");
  776. switch (format->type) {
  777. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  778. {
  779. format->fmt.pix.width = dev->width;
  780. format->fmt.pix.height = dev->height;
  781. format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  782. format->fmt.pix.bytesperline = dev->bytesperline;
  783. format->fmt.pix.sizeimage = dev->frame_size;
  784. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  785. format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  786. em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
  787. dev->height);
  788. break;
  789. }
  790. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  791. {
  792. format->fmt.sliced.service_set=0;
  793. em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
  794. if (format->fmt.sliced.service_set==0)
  795. return -EINVAL;
  796. break;
  797. }
  798. default:
  799. return -EINVAL;
  800. }
  801. return (0);
  802. }
  803. static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format)
  804. {
  805. u32 i;
  806. int ret = 0;
  807. int width = format->fmt.pix.width;
  808. int height = format->fmt.pix.height;
  809. unsigned int hscale, vscale;
  810. unsigned int maxh, maxw;
  811. maxw = norm_maxw(dev);
  812. maxh = norm_maxh(dev);
  813. em28xx_videodbg("%s: type=%s\n",
  814. cmd == VIDIOC_TRY_FMT ?
  815. "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
  816. format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ?
  817. "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
  818. format->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
  819. "V4L2_BUF_TYPE_VBI_CAPTURE " :
  820. "not supported");
  821. if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
  822. em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
  823. if (format->fmt.sliced.service_set==0)
  824. return -EINVAL;
  825. return 0;
  826. }
  827. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  828. return -EINVAL;
  829. em28xx_videodbg("%s: requested %dx%d\n",
  830. cmd == VIDIOC_TRY_FMT ?
  831. "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
  832. format->fmt.pix.width, format->fmt.pix.height);
  833. /* FIXME: Move some code away from here */
  834. /* width must even because of the YUYV format */
  835. /* height must be even because of interlacing */
  836. height &= 0xfffe;
  837. width &= 0xfffe;
  838. if (height < 32)
  839. height = 32;
  840. if (height > maxh)
  841. height = maxh;
  842. if (width < 48)
  843. width = 48;
  844. if (width > maxw)
  845. width = maxw;
  846. if(dev->is_em2800){
  847. /* the em2800 can only scale down to 50% */
  848. if(height % (maxh / 2))
  849. height=maxh;
  850. if(width % (maxw / 2))
  851. width=maxw;
  852. /* according to empiatech support */
  853. /* the MaxPacketSize is to small to support */
  854. /* framesizes larger than 640x480 @ 30 fps */
  855. /* or 640x576 @ 25 fps. As this would cut */
  856. /* of a part of the image we prefer */
  857. /* 360x576 or 360x480 for now */
  858. if(width == maxw && height == maxh)
  859. width /= 2;
  860. }
  861. if ((hscale =
  862. (((unsigned long)maxw) << 12) / width - 4096L) >=
  863. 0x4000)
  864. hscale = 0x3fff;
  865. width =
  866. (((unsigned long)maxw) << 12) / (hscale + 4096L);
  867. if ((vscale =
  868. (((unsigned long)maxh) << 12) / height - 4096L) >=
  869. 0x4000)
  870. vscale = 0x3fff;
  871. height =
  872. (((unsigned long)maxh) << 12) / (vscale + 4096L);
  873. format->fmt.pix.width = width;
  874. format->fmt.pix.height = height;
  875. format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  876. format->fmt.pix.bytesperline = width * 2;
  877. format->fmt.pix.sizeimage = width * 2 * height;
  878. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  879. format->fmt.pix.field = V4L2_FIELD_INTERLACED;
  880. em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
  881. cmd ==
  882. VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
  883. "VIDIOC_S_FMT", format->fmt.pix.width,
  884. format->fmt.pix.height, hscale, vscale);
  885. if (cmd == VIDIOC_TRY_FMT)
  886. return 0;
  887. for (i = 0; i < dev->num_frames; i++)
  888. if (dev->frame[i].vma_use_count) {
  889. em28xx_videodbg("VIDIOC_S_FMT failed. "
  890. "Unmap the buffers first.\n");
  891. return -EINVAL;
  892. }
  893. /* stop io in case it is already in progress */
  894. if (dev->stream == STREAM_ON) {
  895. em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
  896. if ((ret = em28xx_stream_interrupt(dev)))
  897. return ret;
  898. }
  899. em28xx_release_buffers(dev);
  900. dev->io = IO_NONE;
  901. /* set new image size */
  902. dev->width = width;
  903. dev->height = height;
  904. dev->frame_size = dev->width * dev->height * 2;
  905. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  906. dev->bytesperline = dev->width * 2;
  907. dev->hscale = hscale;
  908. dev->vscale = vscale;
  909. em28xx_uninit_isoc(dev);
  910. em28xx_set_alternate(dev);
  911. em28xx_capture_start(dev, 1);
  912. em28xx_resolution_set(dev);
  913. em28xx_init_isoc(dev);
  914. return 0;
  915. }
  916. /*
  917. * em28xx_v4l2_do_ioctl()
  918. * This function is _not_ called directly, but from
  919. * em28xx_v4l2_ioctl. Userspace
  920. * copying is done already, arg is a kernel pointer.
  921. */
  922. static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
  923. struct em28xx *dev, unsigned int cmd, void *arg,
  924. v4l2_kioctl driver_ioctl)
  925. {
  926. int ret;
  927. switch (cmd) {
  928. /* ---------- tv norms ---------- */
  929. case VIDIOC_ENUMSTD:
  930. {
  931. struct v4l2_standard *e = arg;
  932. unsigned int i;
  933. i = e->index;
  934. if (i >= TVNORMS)
  935. return -EINVAL;
  936. ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
  937. tvnorms[e->index].name);
  938. e->index = i;
  939. if (ret < 0)
  940. return ret;
  941. return 0;
  942. }
  943. case VIDIOC_G_STD:
  944. {
  945. v4l2_std_id *id = arg;
  946. *id = dev->tvnorm->id;
  947. return 0;
  948. }
  949. case VIDIOC_S_STD:
  950. {
  951. v4l2_std_id *id = arg;
  952. unsigned int i;
  953. for (i = 0; i < TVNORMS; i++)
  954. if (*id == tvnorms[i].id)
  955. break;
  956. if (i == TVNORMS)
  957. for (i = 0; i < TVNORMS; i++)
  958. if (*id & tvnorms[i].id)
  959. break;
  960. if (i == TVNORMS)
  961. return -EINVAL;
  962. down(&dev->lock);
  963. dev->tvnorm = &tvnorms[i];
  964. em28xx_set_norm(dev, dev->width, dev->height);
  965. em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
  966. &tvnorms[i].mode);
  967. em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
  968. &dev->tvnorm->id);
  969. up(&dev->lock);
  970. return 0;
  971. }
  972. /* ------ input switching ---------- */
  973. case VIDIOC_ENUMINPUT:
  974. {
  975. struct v4l2_input *i = arg;
  976. unsigned int n;
  977. static const char *iname[] = {
  978. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  979. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  980. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  981. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  982. [EM28XX_VMUX_SVIDEO] = "S-Video",
  983. [EM28XX_VMUX_TELEVISION] = "Television",
  984. [EM28XX_VMUX_CABLE] = "Cable TV",
  985. [EM28XX_VMUX_DVB] = "DVB",
  986. [EM28XX_VMUX_DEBUG] = "for debug only",
  987. };
  988. n = i->index;
  989. if (n >= MAX_EM28XX_INPUT)
  990. return -EINVAL;
  991. if (0 == INPUT(n)->type)
  992. return -EINVAL;
  993. memset(i, 0, sizeof(*i));
  994. i->index = n;
  995. i->type = V4L2_INPUT_TYPE_CAMERA;
  996. strcpy(i->name, iname[INPUT(n)->type]);
  997. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  998. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  999. i->type = V4L2_INPUT_TYPE_TUNER;
  1000. for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
  1001. i->std |= tvnorms[n].id;
  1002. return 0;
  1003. }
  1004. case VIDIOC_G_INPUT:
  1005. {
  1006. int *i = arg;
  1007. *i = dev->ctl_input;
  1008. return 0;
  1009. }
  1010. case VIDIOC_S_INPUT:
  1011. {
  1012. int *index = arg;
  1013. if (*index >= MAX_EM28XX_INPUT)
  1014. return -EINVAL;
  1015. if (0 == INPUT(*index)->type)
  1016. return -EINVAL;
  1017. down(&dev->lock);
  1018. video_mux(dev, *index);
  1019. up(&dev->lock);
  1020. return 0;
  1021. }
  1022. case VIDIOC_G_AUDIO:
  1023. {
  1024. struct v4l2_audio *a = arg;
  1025. unsigned int index = a->index;
  1026. if (a->index > 1)
  1027. return -EINVAL;
  1028. memset(a, 0, sizeof(*a));
  1029. index = dev->ctl_ainput;
  1030. if (index == 0) {
  1031. strcpy(a->name, "Television");
  1032. } else {
  1033. strcpy(a->name, "Line In");
  1034. }
  1035. a->capability = V4L2_AUDCAP_STEREO;
  1036. a->index = index;
  1037. return 0;
  1038. }
  1039. case VIDIOC_S_AUDIO:
  1040. {
  1041. struct v4l2_audio *a = arg;
  1042. if (a->index != dev->ctl_ainput)
  1043. return -EINVAL;
  1044. return 0;
  1045. }
  1046. /* --- controls ---------------------------------------------- */
  1047. case VIDIOC_QUERYCTRL:
  1048. {
  1049. struct v4l2_queryctrl *qc = arg;
  1050. int i, id=qc->id;
  1051. memset(qc,0,sizeof(*qc));
  1052. qc->id=id;
  1053. if (!dev->has_msp34xx) {
  1054. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1055. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1056. memcpy(qc, &(em28xx_qctrl[i]),
  1057. sizeof(*qc));
  1058. return 0;
  1059. }
  1060. }
  1061. }
  1062. if (dev->decoder == EM28XX_TVP5150) {
  1063. em28xx_i2c_call_clients(dev,cmd,qc);
  1064. if (qc->type)
  1065. return 0;
  1066. else
  1067. return -EINVAL;
  1068. }
  1069. for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
  1070. if (qc->id && qc->id == saa711x_qctrl[i].id) {
  1071. memcpy(qc, &(saa711x_qctrl[i]),
  1072. sizeof(*qc));
  1073. return 0;
  1074. }
  1075. }
  1076. return -EINVAL;
  1077. }
  1078. case VIDIOC_G_CTRL:
  1079. {
  1080. struct v4l2_control *ctrl = arg;
  1081. int retval=-EINVAL;
  1082. if (!dev->has_msp34xx)
  1083. retval=em28xx_get_ctrl(dev, ctrl);
  1084. if (retval==-EINVAL) {
  1085. if (dev->decoder == EM28XX_TVP5150) {
  1086. em28xx_i2c_call_clients(dev,cmd,arg);
  1087. return 0;
  1088. }
  1089. return saa711x_get_ctrl(dev, ctrl);
  1090. } else return retval;
  1091. }
  1092. case VIDIOC_S_CTRL:
  1093. {
  1094. struct v4l2_control *ctrl = arg;
  1095. u8 i;
  1096. if (!dev->has_msp34xx){
  1097. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1098. if (ctrl->id == em28xx_qctrl[i].id) {
  1099. if (ctrl->value <
  1100. em28xx_qctrl[i].minimum
  1101. || ctrl->value >
  1102. em28xx_qctrl[i].maximum)
  1103. return -ERANGE;
  1104. return em28xx_set_ctrl(dev, ctrl);
  1105. }
  1106. }
  1107. }
  1108. if (dev->decoder == EM28XX_TVP5150) {
  1109. em28xx_i2c_call_clients(dev,cmd,arg);
  1110. return 0;
  1111. } else if (!dev->has_msp34xx) {
  1112. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1113. if (ctrl->id == em28xx_qctrl[i].id) {
  1114. if (ctrl->value <
  1115. em28xx_qctrl[i].minimum
  1116. || ctrl->value >
  1117. em28xx_qctrl[i].maximum)
  1118. return -ERANGE;
  1119. return em28xx_set_ctrl(dev, ctrl);
  1120. }
  1121. }
  1122. for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
  1123. if (ctrl->id == saa711x_qctrl[i].id) {
  1124. if (ctrl->value <
  1125. saa711x_qctrl[i].minimum
  1126. || ctrl->value >
  1127. saa711x_qctrl[i].maximum)
  1128. return -ERANGE;
  1129. return saa711x_set_ctrl(dev, ctrl);
  1130. }
  1131. }
  1132. }
  1133. return -EINVAL;
  1134. }
  1135. /* --- tuner ioctls ------------------------------------------ */
  1136. case VIDIOC_G_TUNER:
  1137. {
  1138. struct v4l2_tuner *t = arg;
  1139. int status = 0;
  1140. if (0 != t->index)
  1141. return -EINVAL;
  1142. memset(t, 0, sizeof(*t));
  1143. strcpy(t->name, "Tuner");
  1144. t->type = V4L2_TUNER_ANALOG_TV;
  1145. t->capability = V4L2_TUNER_CAP_NORM;
  1146. t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
  1147. /* t->signal = 0xffff;*/
  1148. /* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
  1149. /* No way to get signal strength? */
  1150. down(&dev->lock);
  1151. em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
  1152. &status);
  1153. up(&dev->lock);
  1154. t->signal =
  1155. (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
  1156. em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
  1157. t->afc);
  1158. return 0;
  1159. }
  1160. case VIDIOC_S_TUNER:
  1161. {
  1162. struct v4l2_tuner *t = arg;
  1163. int status = 0;
  1164. if (0 != t->index)
  1165. return -EINVAL;
  1166. memset(t, 0, sizeof(*t));
  1167. strcpy(t->name, "Tuner");
  1168. t->type = V4L2_TUNER_ANALOG_TV;
  1169. t->capability = V4L2_TUNER_CAP_NORM;
  1170. t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
  1171. /* t->signal = 0xffff; */
  1172. /* No way to get signal strength? */
  1173. down(&dev->lock);
  1174. em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
  1175. &status);
  1176. up(&dev->lock);
  1177. t->signal =
  1178. (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
  1179. em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
  1180. t->signal, t->afc);
  1181. return 0;
  1182. }
  1183. case VIDIOC_G_FREQUENCY:
  1184. {
  1185. struct v4l2_frequency *f = arg;
  1186. memset(f, 0, sizeof(*f));
  1187. f->type = V4L2_TUNER_ANALOG_TV;
  1188. f->frequency = dev->ctl_freq;
  1189. return 0;
  1190. }
  1191. case VIDIOC_S_FREQUENCY:
  1192. {
  1193. struct v4l2_frequency *f = arg;
  1194. if (0 != f->tuner)
  1195. return -EINVAL;
  1196. if (V4L2_TUNER_ANALOG_TV != f->type)
  1197. return -EINVAL;
  1198. down(&dev->lock);
  1199. dev->ctl_freq = f->frequency;
  1200. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  1201. up(&dev->lock);
  1202. return 0;
  1203. }
  1204. case VIDIOC_CROPCAP:
  1205. {
  1206. struct v4l2_cropcap *cc = arg;
  1207. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1208. return -EINVAL;
  1209. cc->bounds.left = 0;
  1210. cc->bounds.top = 0;
  1211. cc->bounds.width = dev->width;
  1212. cc->bounds.height = dev->height;
  1213. cc->defrect = cc->bounds;
  1214. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1215. cc->pixelaspect.denominator = 59;
  1216. return 0;
  1217. }
  1218. case VIDIOC_STREAMON:
  1219. {
  1220. int *type = arg;
  1221. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1222. || dev->io != IO_MMAP)
  1223. return -EINVAL;
  1224. if (list_empty(&dev->inqueue))
  1225. return -EINVAL;
  1226. dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
  1227. em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
  1228. return 0;
  1229. }
  1230. case VIDIOC_STREAMOFF:
  1231. {
  1232. int *type = arg;
  1233. int ret;
  1234. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1235. || dev->io != IO_MMAP)
  1236. return -EINVAL;
  1237. if (dev->stream == STREAM_ON) {
  1238. em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
  1239. if ((ret = em28xx_stream_interrupt(dev)))
  1240. return ret;
  1241. }
  1242. em28xx_empty_framequeues(dev);
  1243. return 0;
  1244. }
  1245. default:
  1246. return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
  1247. driver_ioctl);
  1248. }
  1249. return 0;
  1250. }
  1251. /*
  1252. * em28xx_v4l2_do_ioctl()
  1253. * This function is _not_ called directly, but from
  1254. * em28xx_v4l2_ioctl. Userspace
  1255. * copying is done already, arg is a kernel pointer.
  1256. */
  1257. static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
  1258. unsigned int cmd, void *arg)
  1259. {
  1260. struct em28xx *dev = filp->private_data;
  1261. if (!dev)
  1262. return -ENODEV;
  1263. if (video_debug > 1)
  1264. v4l_print_ioctl(dev->name,cmd);
  1265. switch (cmd) {
  1266. /* --- capabilities ------------------------------------------ */
  1267. case VIDIOC_QUERYCAP:
  1268. {
  1269. struct v4l2_capability *cap = arg;
  1270. memset(cap, 0, sizeof(*cap));
  1271. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1272. strlcpy(cap->card, em28xx_boards[dev->model].name,
  1273. sizeof(cap->card));
  1274. strlcpy(cap->bus_info, dev->udev->dev.bus_id,
  1275. sizeof(cap->bus_info));
  1276. cap->version = EM28XX_VERSION_CODE;
  1277. cap->capabilities =
  1278. V4L2_CAP_SLICED_VBI_CAPTURE |
  1279. V4L2_CAP_VIDEO_CAPTURE |
  1280. V4L2_CAP_AUDIO |
  1281. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1282. if (dev->has_tuner)
  1283. cap->capabilities |= V4L2_CAP_TUNER;
  1284. return 0;
  1285. }
  1286. /* --- capture ioctls ---------------------------------------- */
  1287. case VIDIOC_ENUM_FMT:
  1288. {
  1289. struct v4l2_fmtdesc *fmtd = arg;
  1290. if (fmtd->index != 0)
  1291. return -EINVAL;
  1292. memset(fmtd, 0, sizeof(*fmtd));
  1293. fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1294. strcpy(fmtd->description, "Packed YUY2");
  1295. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  1296. memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
  1297. return 0;
  1298. }
  1299. case VIDIOC_G_FMT:
  1300. return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
  1301. case VIDIOC_TRY_FMT:
  1302. case VIDIOC_S_FMT:
  1303. return em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
  1304. case VIDIOC_REQBUFS:
  1305. {
  1306. struct v4l2_requestbuffers *rb = arg;
  1307. u32 i;
  1308. int ret;
  1309. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1310. rb->memory != V4L2_MEMORY_MMAP)
  1311. return -EINVAL;
  1312. if (dev->io == IO_READ) {
  1313. em28xx_videodbg ("method is set to read;"
  1314. " close and open the device again to"
  1315. " choose the mmap I/O method\n");
  1316. return -EINVAL;
  1317. }
  1318. for (i = 0; i < dev->num_frames; i++)
  1319. if (dev->frame[i].vma_use_count) {
  1320. em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
  1321. return -EINVAL;
  1322. }
  1323. if (dev->stream == STREAM_ON) {
  1324. em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
  1325. if ((ret = em28xx_stream_interrupt(dev)))
  1326. return ret;
  1327. }
  1328. em28xx_empty_framequeues(dev);
  1329. em28xx_release_buffers(dev);
  1330. if (rb->count)
  1331. rb->count =
  1332. em28xx_request_buffers(dev, rb->count);
  1333. dev->frame_current = NULL;
  1334. em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
  1335. rb->count);
  1336. dev->io = rb->count ? IO_MMAP : IO_NONE;
  1337. return 0;
  1338. }
  1339. case VIDIOC_QUERYBUF:
  1340. {
  1341. struct v4l2_buffer *b = arg;
  1342. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1343. b->index >= dev->num_frames || dev->io != IO_MMAP)
  1344. return -EINVAL;
  1345. memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
  1346. if (dev->frame[b->index].vma_use_count) {
  1347. b->flags |= V4L2_BUF_FLAG_MAPPED;
  1348. }
  1349. if (dev->frame[b->index].state == F_DONE)
  1350. b->flags |= V4L2_BUF_FLAG_DONE;
  1351. else if (dev->frame[b->index].state != F_UNUSED)
  1352. b->flags |= V4L2_BUF_FLAG_QUEUED;
  1353. return 0;
  1354. }
  1355. case VIDIOC_QBUF:
  1356. {
  1357. struct v4l2_buffer *b = arg;
  1358. unsigned long lock_flags;
  1359. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1360. b->index >= dev->num_frames || dev->io != IO_MMAP) {
  1361. return -EINVAL;
  1362. }
  1363. if (dev->frame[b->index].state != F_UNUSED) {
  1364. return -EAGAIN;
  1365. }
  1366. dev->frame[b->index].state = F_QUEUED;
  1367. /* add frame to fifo */
  1368. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  1369. list_add_tail(&dev->frame[b->index].frame,
  1370. &dev->inqueue);
  1371. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  1372. return 0;
  1373. }
  1374. case VIDIOC_DQBUF:
  1375. {
  1376. struct v4l2_buffer *b = arg;
  1377. struct em28xx_frame_t *f;
  1378. unsigned long lock_flags;
  1379. int ret = 0;
  1380. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1381. || dev->io != IO_MMAP)
  1382. return -EINVAL;
  1383. if (list_empty(&dev->outqueue)) {
  1384. if (dev->stream == STREAM_OFF)
  1385. return -EINVAL;
  1386. if (filp->f_flags & O_NONBLOCK)
  1387. return -EAGAIN;
  1388. ret = wait_event_interruptible
  1389. (dev->wait_frame,
  1390. (!list_empty(&dev->outqueue)) ||
  1391. (dev->state & DEV_DISCONNECTED));
  1392. if (ret)
  1393. return ret;
  1394. if (dev->state & DEV_DISCONNECTED)
  1395. return -ENODEV;
  1396. }
  1397. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  1398. f = list_entry(dev->outqueue.next,
  1399. struct em28xx_frame_t, frame);
  1400. list_del(dev->outqueue.next);
  1401. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  1402. f->state = F_UNUSED;
  1403. memcpy(b, &f->buf, sizeof(*b));
  1404. if (f->vma_use_count)
  1405. b->flags |= V4L2_BUF_FLAG_MAPPED;
  1406. return 0;
  1407. }
  1408. default:
  1409. return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
  1410. em28xx_video_do_ioctl);
  1411. }
  1412. return 0;
  1413. }
  1414. /*
  1415. * em28xx_v4l2_ioctl()
  1416. * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
  1417. */
  1418. static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
  1419. unsigned int cmd, unsigned long arg)
  1420. {
  1421. int ret = 0;
  1422. struct em28xx *dev = filp->private_data;
  1423. if (down_interruptible(&dev->fileop_lock))
  1424. return -ERESTARTSYS;
  1425. if (dev->state & DEV_DISCONNECTED) {
  1426. em28xx_errdev("v4l2 ioctl: device not present\n");
  1427. up(&dev->fileop_lock);
  1428. return -ENODEV;
  1429. }
  1430. if (dev->state & DEV_MISCONFIGURED) {
  1431. em28xx_errdev
  1432. ("v4l2 ioctl: device is misconfigured; close and open it again\n");
  1433. up(&dev->fileop_lock);
  1434. return -EIO;
  1435. }
  1436. ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
  1437. up(&dev->fileop_lock);
  1438. return ret;
  1439. }
  1440. static struct file_operations em28xx_v4l_fops = {
  1441. .owner = THIS_MODULE,
  1442. .open = em28xx_v4l2_open,
  1443. .release = em28xx_v4l2_close,
  1444. .ioctl = em28xx_v4l2_ioctl,
  1445. .read = em28xx_v4l2_read,
  1446. .poll = em28xx_v4l2_poll,
  1447. .mmap = em28xx_v4l2_mmap,
  1448. .llseek = no_llseek,
  1449. .compat_ioctl = v4l_compat_ioctl32,
  1450. };
  1451. /******************************** usb interface *****************************************/
  1452. /*
  1453. * em28xx_init_dev()
  1454. * allocates and inits the device structs, registers i2c bus and v4l device
  1455. */
  1456. static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
  1457. int minor, int model)
  1458. {
  1459. struct em28xx *dev = *devhandle;
  1460. int retval = -ENOMEM;
  1461. int errCode, i;
  1462. unsigned int maxh, maxw;
  1463. dev->udev = udev;
  1464. dev->model = model;
  1465. init_MUTEX(&dev->lock);
  1466. init_waitqueue_head(&dev->open);
  1467. dev->em28xx_write_regs = em28xx_write_regs;
  1468. dev->em28xx_read_reg = em28xx_read_reg;
  1469. dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
  1470. dev->em28xx_write_regs_req = em28xx_write_regs_req;
  1471. dev->em28xx_read_reg_req = em28xx_read_reg_req;
  1472. dev->is_em2800 = em28xx_boards[model].is_em2800;
  1473. dev->has_tuner = em28xx_boards[model].has_tuner;
  1474. dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
  1475. dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
  1476. dev->decoder = em28xx_boards[model].decoder;
  1477. if (tuner >= 0)
  1478. dev->tuner_type = tuner;
  1479. else
  1480. dev->tuner_type = em28xx_boards[model].tuner_type;
  1481. dev->video_inputs = em28xx_boards[model].vchannels;
  1482. for (i = 0; i < TVNORMS; i++)
  1483. if (em28xx_boards[model].norm == tvnorms[i].mode)
  1484. break;
  1485. if (i == TVNORMS)
  1486. i = 0;
  1487. dev->tvnorm = &tvnorms[i]; /* set default norm */
  1488. em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
  1489. maxw = norm_maxw(dev);
  1490. maxh = norm_maxh(dev);
  1491. /* set default image size */
  1492. dev->width = maxw;
  1493. dev->height = maxh;
  1494. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1495. dev->field_size = dev->width * dev->height;
  1496. dev->frame_size =
  1497. dev->interlaced ? dev->field_size << 1 : dev->field_size;
  1498. dev->bytesperline = dev->width * 2;
  1499. dev->hscale = 0;
  1500. dev->vscale = 0;
  1501. dev->ctl_input = 2;
  1502. /* setup video picture settings for saa7113h */
  1503. memset(&dev->vpic, 0, sizeof(dev->vpic));
  1504. dev->vpic.colour = 128 << 8;
  1505. dev->vpic.hue = 128 << 8;
  1506. dev->vpic.brightness = 128 << 8;
  1507. dev->vpic.contrast = 192 << 8;
  1508. dev->vpic.whiteness = 128 << 8; /* This one isn't used */
  1509. dev->vpic.depth = 16;
  1510. dev->vpic.palette = VIDEO_PALETTE_YUV422;
  1511. #ifdef CONFIG_MODULES
  1512. /* request some modules */
  1513. if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
  1514. request_module("saa711x");
  1515. if (dev->decoder == EM28XX_TVP5150)
  1516. request_module("tvp5150");
  1517. if (dev->has_tuner)
  1518. request_module("tuner");
  1519. if (dev->tda9887_conf)
  1520. request_module("tda9887");
  1521. #endif
  1522. errCode = em28xx_config(dev);
  1523. if (errCode) {
  1524. em28xx_errdev("error configuring device\n");
  1525. kfree(dev);
  1526. em28xx_devused&=~(1<<dev->devno);
  1527. return -ENOMEM;
  1528. }
  1529. down(&dev->lock);
  1530. /* register i2c bus */
  1531. em28xx_i2c_register(dev);
  1532. /* Do board specific init and eeprom reading */
  1533. em28xx_card_setup(dev);
  1534. /* configure the device */
  1535. em28xx_config_i2c(dev);
  1536. up(&dev->lock);
  1537. errCode = em28xx_config(dev);
  1538. #ifdef CONFIG_MODULES
  1539. if (dev->has_msp34xx)
  1540. request_module("msp3400");
  1541. #endif
  1542. /* allocate and fill v4l2 device struct */
  1543. dev->vdev = video_device_alloc();
  1544. if (NULL == dev->vdev) {
  1545. em28xx_errdev("cannot allocate video_device.\n");
  1546. kfree(dev);
  1547. em28xx_devused&=~(1<<dev->devno);
  1548. return -ENOMEM;
  1549. }
  1550. dev->vbi_dev = video_device_alloc();
  1551. if (NULL == dev->vbi_dev) {
  1552. em28xx_errdev("cannot allocate video_device.\n");
  1553. kfree(dev->vdev);
  1554. kfree(dev);
  1555. em28xx_devused&=~(1<<dev->devno);
  1556. return -ENOMEM;
  1557. }
  1558. /* Fills VBI device info */
  1559. dev->vbi_dev->type = VFL_TYPE_VBI;
  1560. dev->vbi_dev->hardware = 0;
  1561. dev->vbi_dev->fops = &em28xx_v4l_fops;
  1562. dev->vbi_dev->minor = -1;
  1563. dev->vbi_dev->dev = &dev->udev->dev;
  1564. dev->vbi_dev->release = video_device_release;
  1565. snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
  1566. "em28xx",dev->devno,"vbi");
  1567. /* Fills CAPTURE device info */
  1568. dev->vdev->type = VID_TYPE_CAPTURE;
  1569. if (dev->has_tuner)
  1570. dev->vdev->type |= VID_TYPE_TUNER;
  1571. dev->vdev->hardware = 0;
  1572. dev->vdev->fops = &em28xx_v4l_fops;
  1573. dev->vdev->minor = -1;
  1574. dev->vdev->dev = &dev->udev->dev;
  1575. dev->vdev->release = video_device_release;
  1576. snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
  1577. "em28xx",dev->devno,"video");
  1578. list_add_tail(&dev->devlist,&em28xx_devlist);
  1579. /* register v4l2 device */
  1580. down(&dev->lock);
  1581. if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1582. video_nr[dev->devno]))) {
  1583. em28xx_errdev("unable to register video device (error=%i).\n",
  1584. retval);
  1585. up(&dev->lock);
  1586. list_del(&dev->devlist);
  1587. video_device_release(dev->vdev);
  1588. kfree(dev);
  1589. em28xx_devused&=~(1<<dev->devno);
  1590. return -ENODEV;
  1591. }
  1592. if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1593. vbi_nr[dev->devno]) < 0) {
  1594. printk("unable to register vbi device\n");
  1595. up(&dev->lock);
  1596. list_del(&dev->devlist);
  1597. video_device_release(dev->vbi_dev);
  1598. video_device_release(dev->vdev);
  1599. kfree(dev);
  1600. em28xx_devused&=~(1<<dev->devno);
  1601. return -ENODEV;
  1602. } else {
  1603. printk("registered VBI\n");
  1604. }
  1605. if (dev->has_msp34xx) {
  1606. /* Send a reset to other chips via gpio */
  1607. em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
  1608. udelay(2500);
  1609. em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
  1610. udelay(2500);
  1611. }
  1612. video_mux(dev, 0);
  1613. up(&dev->lock);
  1614. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1615. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
  1616. dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
  1617. return 0;
  1618. }
  1619. /*
  1620. * em28xx_usb_probe()
  1621. * checks for supported devices
  1622. */
  1623. static int em28xx_usb_probe(struct usb_interface *interface,
  1624. const struct usb_device_id *id)
  1625. {
  1626. const struct usb_endpoint_descriptor *endpoint;
  1627. struct usb_device *udev;
  1628. struct usb_interface *uif;
  1629. struct em28xx *dev = NULL;
  1630. int retval = -ENODEV;
  1631. int model,i,nr,ifnum;
  1632. udev = usb_get_dev(interface_to_usbdev(interface));
  1633. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  1634. /* Check to see next free device and mark as used */
  1635. nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
  1636. em28xx_devused|=1<<nr;
  1637. /* Don't register audio interfaces */
  1638. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1639. em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
  1640. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1641. ifnum,
  1642. interface->altsetting[0].desc.bInterfaceClass);
  1643. em28xx_devused&=~(1<<nr);
  1644. return -ENODEV;
  1645. }
  1646. em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
  1647. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1648. ifnum,
  1649. interface->altsetting[0].desc.bInterfaceClass);
  1650. endpoint = &interface->cur_altsetting->endpoint[1].desc;
  1651. /* check if the the device has the iso in endpoint at the correct place */
  1652. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1653. USB_ENDPOINT_XFER_ISOC) {
  1654. em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
  1655. em28xx_devused&=~(1<<nr);
  1656. return -ENODEV;
  1657. }
  1658. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1659. em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
  1660. em28xx_devused&=~(1<<nr);
  1661. return -ENODEV;
  1662. }
  1663. model=id->driver_info;
  1664. if (nr > EM28XX_MAXBOARDS) {
  1665. printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
  1666. em28xx_devused&=~(1<<nr);
  1667. return -ENOMEM;
  1668. }
  1669. /* allocate memory for our device state and initialize it */
  1670. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1671. if (dev == NULL) {
  1672. em28xx_err(DRIVER_NAME ": out of memory!\n");
  1673. em28xx_devused&=~(1<<nr);
  1674. return -ENOMEM;
  1675. }
  1676. snprintf(dev->name, 29, "em28xx #%d", nr);
  1677. dev->devno=nr;
  1678. /* compute alternate max packet sizes */
  1679. uif = udev->actconfig->interface[0];
  1680. dev->num_alt=uif->num_altsetting;
  1681. em28xx_info("Alternate settings: %i\n",dev->num_alt);
  1682. // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
  1683. dev->alt_max_pkt_size = kmalloc(32*
  1684. dev->num_alt,GFP_KERNEL);
  1685. if (dev->alt_max_pkt_size == NULL) {
  1686. em28xx_errdev("out of memory!\n");
  1687. em28xx_devused&=~(1<<nr);
  1688. return -ENOMEM;
  1689. }
  1690. for (i = 0; i < dev->num_alt ; i++) {
  1691. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1692. wMaxPacketSize);
  1693. dev->alt_max_pkt_size[i] =
  1694. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1695. em28xx_info("Alternate setting %i, max size= %i\n",i,
  1696. dev->alt_max_pkt_size[i]);
  1697. }
  1698. if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
  1699. model=card[nr];
  1700. if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
  1701. em28xx_errdev( "Your board has no eeprom inside it and thus can't\n"
  1702. "%s: be autodetected. Please pass card=<n> insmod option to\n"
  1703. "%s: workaround that. Redirect complaints to the vendor of\n"
  1704. "%s: the TV card. Generic type will be used."
  1705. "%s: Best regards,\n"
  1706. "%s: -- tux\n",
  1707. dev->name,dev->name,dev->name,dev->name,dev->name);
  1708. em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
  1709. dev->name);
  1710. for (i = 0; i < em28xx_bcount; i++) {
  1711. em28xx_errdev(" card=%d -> %s\n", i,
  1712. em28xx_boards[i].name);
  1713. }
  1714. }
  1715. /* allocate device struct */
  1716. retval = em28xx_init_dev(&dev, udev, nr, model);
  1717. if (retval)
  1718. return retval;
  1719. em28xx_info("Found %s\n", em28xx_boards[model].name);
  1720. /* save our data pointer in this interface device */
  1721. usb_set_intfdata(interface, dev);
  1722. return 0;
  1723. }
  1724. /*
  1725. * em28xx_usb_disconnect()
  1726. * called when the device gets diconencted
  1727. * video device will be unregistered on v4l2_close in case it is still open
  1728. */
  1729. static void em28xx_usb_disconnect(struct usb_interface *interface)
  1730. {
  1731. struct em28xx *dev = usb_get_intfdata(interface);
  1732. usb_set_intfdata(interface, NULL);
  1733. if (!dev)
  1734. return;
  1735. down_write(&em28xx_disconnect);
  1736. down(&dev->lock);
  1737. em28xx_info("disconnecting %s\n", dev->vdev->name);
  1738. wake_up_interruptible_all(&dev->open);
  1739. if (dev->users) {
  1740. em28xx_warn
  1741. ("device /dev/video%d is open! Deregistration and memory "
  1742. "deallocation are deferred on close.\n",
  1743. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
  1744. dev->state |= DEV_MISCONFIGURED;
  1745. em28xx_uninit_isoc(dev);
  1746. dev->state |= DEV_DISCONNECTED;
  1747. wake_up_interruptible(&dev->wait_frame);
  1748. wake_up_interruptible(&dev->wait_stream);
  1749. } else {
  1750. dev->state |= DEV_DISCONNECTED;
  1751. em28xx_release_resources(dev);
  1752. }
  1753. up(&dev->lock);
  1754. if (!dev->users) {
  1755. kfree(dev->alt_max_pkt_size);
  1756. kfree(dev);
  1757. }
  1758. up_write(&em28xx_disconnect);
  1759. }
  1760. static struct usb_driver em28xx_usb_driver = {
  1761. .name = "em28xx",
  1762. .probe = em28xx_usb_probe,
  1763. .disconnect = em28xx_usb_disconnect,
  1764. .id_table = em28xx_id_table,
  1765. };
  1766. static int __init em28xx_module_init(void)
  1767. {
  1768. int result;
  1769. printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
  1770. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1771. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1772. #ifdef SNAPSHOT
  1773. printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
  1774. SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
  1775. #endif
  1776. /* register this driver with the USB subsystem */
  1777. result = usb_register(&em28xx_usb_driver);
  1778. if (result)
  1779. em28xx_err(DRIVER_NAME
  1780. " usb_register failed. Error number %d.\n", result);
  1781. return result;
  1782. }
  1783. static void __exit em28xx_module_exit(void)
  1784. {
  1785. /* deregister this driver with the USB subsystem */
  1786. usb_deregister(&em28xx_usb_driver);
  1787. }
  1788. module_init(em28xx_module_init);
  1789. module_exit(em28xx_module_exit);