em28xx-video.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  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); audio register */
  198. /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
  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. mutex_init(&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. mutex_lock(&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. /* device needs to be initialized before isoc transfer */
  327. video_mux(dev, 0);
  328. /* start the transfer */
  329. errCode = em28xx_init_isoc(dev);
  330. if (errCode)
  331. goto err;
  332. }
  333. dev->users++;
  334. filp->private_data = dev;
  335. dev->io = IO_NONE;
  336. dev->stream = STREAM_OFF;
  337. dev->num_frames = 0;
  338. /* prepare queues */
  339. em28xx_empty_framequeues(dev);
  340. dev->state |= DEV_INITIALIZED;
  341. err:
  342. mutex_unlock(&dev->lock);
  343. up_read(&em28xx_disconnect);
  344. return errCode;
  345. }
  346. /*
  347. * em28xx_realease_resources()
  348. * unregisters the v4l2,i2c and usb devices
  349. * called when the device gets disconected or at module unload
  350. */
  351. static void em28xx_release_resources(struct em28xx *dev)
  352. {
  353. mutex_lock(&em28xx_sysfs_lock);
  354. /*FIXME: I2C IR should be disconnected */
  355. em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
  356. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
  357. dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
  358. list_del(&dev->devlist);
  359. video_unregister_device(dev->vdev);
  360. video_unregister_device(dev->vbi_dev);
  361. em28xx_i2c_unregister(dev);
  362. usb_put_dev(dev->udev);
  363. mutex_unlock(&em28xx_sysfs_lock);
  364. /* Mark device as unused */
  365. em28xx_devused&=~(1<<dev->devno);
  366. }
  367. /*
  368. * em28xx_v4l2_close()
  369. * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
  370. */
  371. static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
  372. {
  373. int errCode;
  374. struct em28xx *dev=filp->private_data;
  375. em28xx_videodbg("users=%d\n", dev->users);
  376. mutex_lock(&dev->lock);
  377. em28xx_uninit_isoc(dev);
  378. em28xx_release_buffers(dev);
  379. /* the device is already disconnect, free the remaining resources */
  380. if (dev->state & DEV_DISCONNECTED) {
  381. em28xx_release_resources(dev);
  382. mutex_unlock(&dev->lock);
  383. kfree(dev);
  384. return 0;
  385. }
  386. /* set alternate 0 */
  387. dev->alt = 0;
  388. em28xx_videodbg("setting alternate 0\n");
  389. errCode = usb_set_interface(dev->udev, 0, 0);
  390. if (errCode < 0) {
  391. em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
  392. errCode);
  393. }
  394. dev->users--;
  395. wake_up_interruptible_nr(&dev->open, 1);
  396. mutex_unlock(&dev->lock);
  397. return 0;
  398. }
  399. /*
  400. * em28xx_v4l2_read()
  401. * will allocate buffers when called for the first time
  402. */
  403. static ssize_t
  404. em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
  405. loff_t * f_pos)
  406. {
  407. struct em28xx_frame_t *f, *i;
  408. unsigned long lock_flags;
  409. int ret = 0;
  410. struct em28xx *dev = filp->private_data;
  411. if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  412. em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
  413. }
  414. if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  415. em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
  416. em28xx_videodbg("not supported yet! ...\n");
  417. if (copy_to_user(buf, "", 1)) {
  418. mutex_unlock(&dev->fileop_lock);
  419. return -EFAULT;
  420. }
  421. return (1);
  422. }
  423. if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
  424. em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
  425. em28xx_videodbg("not supported yet! ...\n");
  426. if (copy_to_user(buf, "", 1)) {
  427. mutex_unlock(&dev->fileop_lock);
  428. return -EFAULT;
  429. }
  430. return (1);
  431. }
  432. if (mutex_lock_interruptible(&dev->fileop_lock))
  433. return -ERESTARTSYS;
  434. if (dev->state & DEV_DISCONNECTED) {
  435. em28xx_videodbg("device not present\n");
  436. mutex_unlock(&dev->fileop_lock);
  437. return -ENODEV;
  438. }
  439. if (dev->state & DEV_MISCONFIGURED) {
  440. em28xx_videodbg("device misconfigured; close and open it again\n");
  441. mutex_unlock(&dev->fileop_lock);
  442. return -EIO;
  443. }
  444. if (dev->io == IO_MMAP) {
  445. em28xx_videodbg ("IO method is set to mmap; close and open"
  446. " the device again to choose the read method\n");
  447. mutex_unlock(&dev->fileop_lock);
  448. return -EINVAL;
  449. }
  450. if (dev->io == IO_NONE) {
  451. if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
  452. em28xx_errdev("read failed, not enough memory\n");
  453. mutex_unlock(&dev->fileop_lock);
  454. return -ENOMEM;
  455. }
  456. dev->io = IO_READ;
  457. dev->stream = STREAM_ON;
  458. em28xx_queue_unusedframes(dev);
  459. }
  460. if (!count) {
  461. mutex_unlock(&dev->fileop_lock);
  462. return 0;
  463. }
  464. if (list_empty(&dev->outqueue)) {
  465. if (filp->f_flags & O_NONBLOCK) {
  466. mutex_unlock(&dev->fileop_lock);
  467. return -EAGAIN;
  468. }
  469. ret = wait_event_interruptible
  470. (dev->wait_frame,
  471. (!list_empty(&dev->outqueue)) ||
  472. (dev->state & DEV_DISCONNECTED));
  473. if (ret) {
  474. mutex_unlock(&dev->fileop_lock);
  475. return ret;
  476. }
  477. if (dev->state & DEV_DISCONNECTED) {
  478. mutex_unlock(&dev->fileop_lock);
  479. return -ENODEV;
  480. }
  481. }
  482. f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
  483. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  484. list_for_each_entry(i, &dev->outqueue, frame)
  485. i->state = F_UNUSED;
  486. INIT_LIST_HEAD(&dev->outqueue);
  487. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  488. em28xx_queue_unusedframes(dev);
  489. if (count > f->buf.length)
  490. count = f->buf.length;
  491. if (copy_to_user(buf, f->bufmem, count)) {
  492. mutex_unlock(&dev->fileop_lock);
  493. return -EFAULT;
  494. }
  495. *f_pos += count;
  496. mutex_unlock(&dev->fileop_lock);
  497. return count;
  498. }
  499. /*
  500. * em28xx_v4l2_poll()
  501. * will allocate buffers when called for the first time
  502. */
  503. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
  504. {
  505. unsigned int mask = 0;
  506. struct em28xx *dev = filp->private_data;
  507. if (mutex_lock_interruptible(&dev->fileop_lock))
  508. return POLLERR;
  509. if (dev->state & DEV_DISCONNECTED) {
  510. em28xx_videodbg("device not present\n");
  511. } else if (dev->state & DEV_MISCONFIGURED) {
  512. em28xx_videodbg("device is misconfigured; close and open it again\n");
  513. } else {
  514. if (dev->io == IO_NONE) {
  515. if (!em28xx_request_buffers
  516. (dev, EM28XX_NUM_READ_FRAMES)) {
  517. em28xx_warn
  518. ("poll() failed, not enough memory\n");
  519. } else {
  520. dev->io = IO_READ;
  521. dev->stream = STREAM_ON;
  522. }
  523. }
  524. if (dev->io == IO_READ) {
  525. em28xx_queue_unusedframes(dev);
  526. poll_wait(filp, &dev->wait_frame, wait);
  527. if (!list_empty(&dev->outqueue))
  528. mask |= POLLIN | POLLRDNORM;
  529. mutex_unlock(&dev->fileop_lock);
  530. return mask;
  531. }
  532. }
  533. mutex_unlock(&dev->fileop_lock);
  534. return POLLERR;
  535. }
  536. /*
  537. * em28xx_vm_open()
  538. */
  539. static void em28xx_vm_open(struct vm_area_struct *vma)
  540. {
  541. struct em28xx_frame_t *f = vma->vm_private_data;
  542. f->vma_use_count++;
  543. }
  544. /*
  545. * em28xx_vm_close()
  546. */
  547. static void em28xx_vm_close(struct vm_area_struct *vma)
  548. {
  549. /* NOTE: buffers are not freed here */
  550. struct em28xx_frame_t *f = vma->vm_private_data;
  551. f->vma_use_count--;
  552. }
  553. static struct vm_operations_struct em28xx_vm_ops = {
  554. .open = em28xx_vm_open,
  555. .close = em28xx_vm_close,
  556. };
  557. /*
  558. * em28xx_v4l2_mmap()
  559. */
  560. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  561. {
  562. unsigned long size = vma->vm_end - vma->vm_start,
  563. start = vma->vm_start;
  564. void *pos;
  565. u32 i;
  566. struct em28xx *dev = filp->private_data;
  567. if (mutex_lock_interruptible(&dev->fileop_lock))
  568. return -ERESTARTSYS;
  569. if (dev->state & DEV_DISCONNECTED) {
  570. em28xx_videodbg("mmap: device not present\n");
  571. mutex_unlock(&dev->fileop_lock);
  572. return -ENODEV;
  573. }
  574. if (dev->state & DEV_MISCONFIGURED) {
  575. em28xx_videodbg ("mmap: Device is misconfigured; close and "
  576. "open it again\n");
  577. mutex_unlock(&dev->fileop_lock);
  578. return -EIO;
  579. }
  580. if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
  581. size != PAGE_ALIGN(dev->frame[0].buf.length)) {
  582. mutex_unlock(&dev->fileop_lock);
  583. return -EINVAL;
  584. }
  585. for (i = 0; i < dev->num_frames; i++) {
  586. if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
  587. break;
  588. }
  589. if (i == dev->num_frames) {
  590. em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
  591. mutex_unlock(&dev->fileop_lock);
  592. return -EINVAL;
  593. }
  594. /* VM_IO is eventually going to replace PageReserved altogether */
  595. vma->vm_flags |= VM_IO;
  596. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  597. pos = dev->frame[i].bufmem;
  598. while (size > 0) { /* size is page-aligned */
  599. if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
  600. em28xx_videodbg("mmap: vm_insert_page failed\n");
  601. mutex_unlock(&dev->fileop_lock);
  602. return -EAGAIN;
  603. }
  604. start += PAGE_SIZE;
  605. pos += PAGE_SIZE;
  606. size -= PAGE_SIZE;
  607. }
  608. vma->vm_ops = &em28xx_vm_ops;
  609. vma->vm_private_data = &dev->frame[i];
  610. em28xx_vm_open(vma);
  611. mutex_unlock(&dev->fileop_lock);
  612. return 0;
  613. }
  614. /*
  615. * em28xx_get_ctrl()
  616. * return the current saturation, brightness or contrast, mute state
  617. */
  618. static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  619. {
  620. switch (ctrl->id) {
  621. case V4L2_CID_AUDIO_MUTE:
  622. ctrl->value = dev->mute;
  623. return 0;
  624. case V4L2_CID_AUDIO_VOLUME:
  625. ctrl->value = dev->volume;
  626. return 0;
  627. default:
  628. return -EINVAL;
  629. }
  630. }
  631. /*FIXME: should be moved to saa711x */
  632. static int saa711x_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  633. {
  634. s32 tmp;
  635. switch (ctrl->id) {
  636. case V4L2_CID_BRIGHTNESS:
  637. if ((tmp = em28xx_brightness_get(dev)) < 0)
  638. return -EIO;
  639. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  640. return 0;
  641. case V4L2_CID_CONTRAST:
  642. if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
  643. return -EIO;
  644. return 0;
  645. case V4L2_CID_SATURATION:
  646. if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
  647. return -EIO;
  648. return 0;
  649. case V4L2_CID_RED_BALANCE:
  650. if ((tmp = em28xx_v_balance_get(dev)) < 0)
  651. return -EIO;
  652. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  653. return 0;
  654. case V4L2_CID_BLUE_BALANCE:
  655. if ((tmp = em28xx_u_balance_get(dev)) < 0)
  656. return -EIO;
  657. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  658. return 0;
  659. case V4L2_CID_GAMMA:
  660. if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
  661. return -EIO;
  662. return 0;
  663. default:
  664. return -EINVAL;
  665. }
  666. }
  667. /*
  668. * em28xx_set_ctrl()
  669. * mute or set new saturation, brightness or contrast
  670. */
  671. static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  672. {
  673. switch (ctrl->id) {
  674. case V4L2_CID_AUDIO_MUTE:
  675. if (ctrl->value != dev->mute) {
  676. dev->mute = ctrl->value;
  677. em28xx_audio_usb_mute(dev, ctrl->value);
  678. return em28xx_audio_analog_set(dev);
  679. }
  680. return 0;
  681. case V4L2_CID_AUDIO_VOLUME:
  682. dev->volume = ctrl->value;
  683. return em28xx_audio_analog_set(dev);
  684. default:
  685. return -EINVAL;
  686. }
  687. }
  688. /*FIXME: should be moved to saa711x */
  689. static int saa711x_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  690. {
  691. switch (ctrl->id) {
  692. case V4L2_CID_BRIGHTNESS:
  693. return em28xx_brightness_set(dev, ctrl->value);
  694. case V4L2_CID_CONTRAST:
  695. return em28xx_contrast_set(dev, ctrl->value);
  696. case V4L2_CID_SATURATION:
  697. return em28xx_saturation_set(dev, ctrl->value);
  698. case V4L2_CID_RED_BALANCE:
  699. return em28xx_v_balance_set(dev, ctrl->value);
  700. case V4L2_CID_BLUE_BALANCE:
  701. return em28xx_u_balance_set(dev, ctrl->value);
  702. case V4L2_CID_GAMMA:
  703. return em28xx_gamma_set(dev, ctrl->value);
  704. default:
  705. return -EINVAL;
  706. }
  707. }
  708. /*
  709. * em28xx_stream_interrupt()
  710. * stops streaming
  711. */
  712. static int em28xx_stream_interrupt(struct em28xx *dev)
  713. {
  714. int ret = 0;
  715. /* stop reading from the device */
  716. dev->stream = STREAM_INTERRUPT;
  717. ret = wait_event_timeout(dev->wait_stream,
  718. (dev->stream == STREAM_OFF) ||
  719. (dev->state & DEV_DISCONNECTED),
  720. EM28XX_URB_TIMEOUT);
  721. if (dev->state & DEV_DISCONNECTED)
  722. return -ENODEV;
  723. else if (ret) {
  724. dev->state |= DEV_MISCONFIGURED;
  725. em28xx_videodbg("device is misconfigured; close and "
  726. "open /dev/video%d again\n",
  727. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
  728. return ret;
  729. }
  730. return 0;
  731. }
  732. static int em28xx_set_norm(struct em28xx *dev, int width, int height)
  733. {
  734. unsigned int hscale, vscale;
  735. unsigned int maxh, maxw;
  736. maxw = norm_maxw(dev);
  737. maxh = norm_maxh(dev);
  738. /* width must even because of the YUYV format */
  739. /* height must be even because of interlacing */
  740. height &= 0xfffe;
  741. width &= 0xfffe;
  742. if (height < 32)
  743. height = 32;
  744. if (height > maxh)
  745. height = maxh;
  746. if (width < 48)
  747. width = 48;
  748. if (width > maxw)
  749. width = maxw;
  750. if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
  751. hscale = 0x3fff;
  752. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  753. if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
  754. vscale = 0x3fff;
  755. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  756. /* set new image size */
  757. dev->width = width;
  758. dev->height = height;
  759. dev->frame_size = dev->width * dev->height * 2;
  760. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  761. dev->bytesperline = dev->width * 2;
  762. dev->hscale = hscale;
  763. dev->vscale = vscale;
  764. em28xx_resolution_set(dev);
  765. return 0;
  766. }
  767. static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format)
  768. {
  769. em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
  770. (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
  771. "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
  772. (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ?
  773. "V4L2_BUF_TYPE_VBI_CAPTURE" :
  774. (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ?
  775. "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
  776. "not supported");
  777. switch (format->type) {
  778. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  779. {
  780. format->fmt.pix.width = dev->width;
  781. format->fmt.pix.height = dev->height;
  782. format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  783. format->fmt.pix.bytesperline = dev->bytesperline;
  784. format->fmt.pix.sizeimage = dev->frame_size;
  785. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  786. format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  787. em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
  788. dev->height);
  789. break;
  790. }
  791. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  792. {
  793. format->fmt.sliced.service_set=0;
  794. em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
  795. if (format->fmt.sliced.service_set==0)
  796. return -EINVAL;
  797. break;
  798. }
  799. default:
  800. return -EINVAL;
  801. }
  802. return (0);
  803. }
  804. static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format)
  805. {
  806. u32 i;
  807. int ret = 0;
  808. int width = format->fmt.pix.width;
  809. int height = format->fmt.pix.height;
  810. unsigned int hscale, vscale;
  811. unsigned int maxh, maxw;
  812. maxw = norm_maxw(dev);
  813. maxh = norm_maxh(dev);
  814. em28xx_videodbg("%s: type=%s\n",
  815. cmd == VIDIOC_TRY_FMT ?
  816. "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
  817. format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ?
  818. "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
  819. format->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
  820. "V4L2_BUF_TYPE_VBI_CAPTURE " :
  821. "not supported");
  822. if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
  823. em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
  824. if (format->fmt.sliced.service_set==0)
  825. return -EINVAL;
  826. return 0;
  827. }
  828. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  829. return -EINVAL;
  830. em28xx_videodbg("%s: requested %dx%d\n",
  831. cmd == VIDIOC_TRY_FMT ?
  832. "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
  833. format->fmt.pix.width, format->fmt.pix.height);
  834. /* FIXME: Move some code away from here */
  835. /* width must even because of the YUYV format */
  836. /* height must be even because of interlacing */
  837. height &= 0xfffe;
  838. width &= 0xfffe;
  839. if (height < 32)
  840. height = 32;
  841. if (height > maxh)
  842. height = maxh;
  843. if (width < 48)
  844. width = 48;
  845. if (width > maxw)
  846. width = maxw;
  847. if(dev->is_em2800){
  848. /* the em2800 can only scale down to 50% */
  849. if(height % (maxh / 2))
  850. height=maxh;
  851. if(width % (maxw / 2))
  852. width=maxw;
  853. /* according to empiatech support */
  854. /* the MaxPacketSize is to small to support */
  855. /* framesizes larger than 640x480 @ 30 fps */
  856. /* or 640x576 @ 25 fps. As this would cut */
  857. /* of a part of the image we prefer */
  858. /* 360x576 or 360x480 for now */
  859. if(width == maxw && height == maxh)
  860. width /= 2;
  861. }
  862. if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
  863. hscale = 0x3fff;
  864. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  865. if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
  866. vscale = 0x3fff;
  867. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  868. format->fmt.pix.width = width;
  869. format->fmt.pix.height = height;
  870. format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  871. format->fmt.pix.bytesperline = width * 2;
  872. format->fmt.pix.sizeimage = width * 2 * height;
  873. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  874. format->fmt.pix.field = V4L2_FIELD_INTERLACED;
  875. em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
  876. cmd == VIDIOC_TRY_FMT ?
  877. "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
  878. format->fmt.pix.width, format->fmt.pix.height, hscale, vscale);
  879. if (cmd == VIDIOC_TRY_FMT)
  880. return 0;
  881. for (i = 0; i < dev->num_frames; i++)
  882. if (dev->frame[i].vma_use_count) {
  883. em28xx_videodbg("VIDIOC_S_FMT failed. "
  884. "Unmap the buffers first.\n");
  885. return -EINVAL;
  886. }
  887. /* stop io in case it is already in progress */
  888. if (dev->stream == STREAM_ON) {
  889. em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
  890. if ((ret = em28xx_stream_interrupt(dev)))
  891. return ret;
  892. }
  893. em28xx_release_buffers(dev);
  894. dev->io = IO_NONE;
  895. /* set new image size */
  896. dev->width = width;
  897. dev->height = height;
  898. dev->frame_size = dev->width * dev->height * 2;
  899. dev->field_size = dev->frame_size >> 1;
  900. dev->bytesperline = dev->width * 2;
  901. dev->hscale = hscale;
  902. dev->vscale = vscale;
  903. em28xx_uninit_isoc(dev);
  904. em28xx_set_alternate(dev);
  905. em28xx_capture_start(dev, 1);
  906. em28xx_resolution_set(dev);
  907. em28xx_init_isoc(dev);
  908. return 0;
  909. }
  910. /*
  911. * em28xx_v4l2_do_ioctl()
  912. * This function is _not_ called directly, but from
  913. * em28xx_v4l2_ioctl. Userspace
  914. * copying is done already, arg is a kernel pointer.
  915. */
  916. static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
  917. struct em28xx *dev, unsigned int cmd, void *arg,
  918. v4l2_kioctl driver_ioctl)
  919. {
  920. int ret;
  921. switch (cmd) {
  922. /* ---------- tv norms ---------- */
  923. case VIDIOC_ENUMSTD:
  924. {
  925. struct v4l2_standard *e = arg;
  926. unsigned int i;
  927. i = e->index;
  928. if (i >= TVNORMS)
  929. return -EINVAL;
  930. ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
  931. tvnorms[e->index].name);
  932. e->index = i;
  933. if (ret < 0)
  934. return ret;
  935. return 0;
  936. }
  937. case VIDIOC_G_STD:
  938. {
  939. v4l2_std_id *id = arg;
  940. *id = dev->tvnorm->id;
  941. return 0;
  942. }
  943. case VIDIOC_S_STD:
  944. {
  945. v4l2_std_id *id = arg;
  946. unsigned int i;
  947. for (i = 0; i < TVNORMS; i++)
  948. if (*id == tvnorms[i].id)
  949. break;
  950. if (i == TVNORMS)
  951. for (i = 0; i < TVNORMS; i++)
  952. if (*id & tvnorms[i].id)
  953. break;
  954. if (i == TVNORMS)
  955. return -EINVAL;
  956. mutex_lock(&dev->lock);
  957. dev->tvnorm = &tvnorms[i];
  958. em28xx_set_norm(dev, dev->width, dev->height);
  959. em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
  960. &tvnorms[i].mode);
  961. em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
  962. &dev->tvnorm->id);
  963. mutex_unlock(&dev->lock);
  964. return 0;
  965. }
  966. /* ------ input switching ---------- */
  967. case VIDIOC_ENUMINPUT:
  968. {
  969. struct v4l2_input *i = arg;
  970. unsigned int n;
  971. static const char *iname[] = {
  972. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  973. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  974. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  975. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  976. [EM28XX_VMUX_SVIDEO] = "S-Video",
  977. [EM28XX_VMUX_TELEVISION] = "Television",
  978. [EM28XX_VMUX_CABLE] = "Cable TV",
  979. [EM28XX_VMUX_DVB] = "DVB",
  980. [EM28XX_VMUX_DEBUG] = "for debug only",
  981. };
  982. n = i->index;
  983. if (n >= MAX_EM28XX_INPUT)
  984. return -EINVAL;
  985. if (0 == INPUT(n)->type)
  986. return -EINVAL;
  987. memset(i, 0, sizeof(*i));
  988. i->index = n;
  989. i->type = V4L2_INPUT_TYPE_CAMERA;
  990. strcpy(i->name, iname[INPUT(n)->type]);
  991. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  992. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  993. i->type = V4L2_INPUT_TYPE_TUNER;
  994. for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
  995. i->std |= tvnorms[n].id;
  996. return 0;
  997. }
  998. case VIDIOC_G_INPUT:
  999. {
  1000. int *i = arg;
  1001. *i = dev->ctl_input;
  1002. return 0;
  1003. }
  1004. case VIDIOC_S_INPUT:
  1005. {
  1006. int *index = arg;
  1007. if (*index >= MAX_EM28XX_INPUT)
  1008. return -EINVAL;
  1009. if (0 == INPUT(*index)->type)
  1010. return -EINVAL;
  1011. mutex_lock(&dev->lock);
  1012. video_mux(dev, *index);
  1013. mutex_unlock(&dev->lock);
  1014. return 0;
  1015. }
  1016. case VIDIOC_G_AUDIO:
  1017. {
  1018. struct v4l2_audio *a = arg;
  1019. unsigned int index = a->index;
  1020. if (a->index > 1)
  1021. return -EINVAL;
  1022. memset(a, 0, sizeof(*a));
  1023. index = dev->ctl_ainput;
  1024. if (index == 0) {
  1025. strcpy(a->name, "Television");
  1026. } else {
  1027. strcpy(a->name, "Line In");
  1028. }
  1029. a->capability = V4L2_AUDCAP_STEREO;
  1030. a->index = index;
  1031. return 0;
  1032. }
  1033. case VIDIOC_S_AUDIO:
  1034. {
  1035. struct v4l2_audio *a = arg;
  1036. if (a->index != dev->ctl_ainput)
  1037. return -EINVAL;
  1038. return 0;
  1039. }
  1040. /* --- controls ---------------------------------------------- */
  1041. case VIDIOC_QUERYCTRL:
  1042. {
  1043. struct v4l2_queryctrl *qc = arg;
  1044. int i, id=qc->id;
  1045. memset(qc,0,sizeof(*qc));
  1046. qc->id=id;
  1047. if (!dev->has_msp34xx) {
  1048. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1049. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1050. memcpy(qc, &(em28xx_qctrl[i]),
  1051. sizeof(*qc));
  1052. return 0;
  1053. }
  1054. }
  1055. }
  1056. if (dev->decoder == EM28XX_TVP5150) {
  1057. em28xx_i2c_call_clients(dev,cmd,qc);
  1058. if (qc->type)
  1059. return 0;
  1060. else
  1061. return -EINVAL;
  1062. }
  1063. for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
  1064. if (qc->id && qc->id == saa711x_qctrl[i].id) {
  1065. memcpy(qc, &(saa711x_qctrl[i]),
  1066. sizeof(*qc));
  1067. return 0;
  1068. }
  1069. }
  1070. return -EINVAL;
  1071. }
  1072. case VIDIOC_G_CTRL:
  1073. {
  1074. struct v4l2_control *ctrl = arg;
  1075. int retval=-EINVAL;
  1076. if (!dev->has_msp34xx)
  1077. retval=em28xx_get_ctrl(dev, ctrl);
  1078. if (retval==-EINVAL) {
  1079. if (dev->decoder == EM28XX_TVP5150) {
  1080. em28xx_i2c_call_clients(dev,cmd,arg);
  1081. return 0;
  1082. }
  1083. return saa711x_get_ctrl(dev, ctrl);
  1084. } else return retval;
  1085. }
  1086. case VIDIOC_S_CTRL:
  1087. {
  1088. struct v4l2_control *ctrl = arg;
  1089. u8 i;
  1090. if (!dev->has_msp34xx){
  1091. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1092. if (ctrl->id == em28xx_qctrl[i].id) {
  1093. if (ctrl->value <
  1094. em28xx_qctrl[i].minimum
  1095. || ctrl->value >
  1096. em28xx_qctrl[i].maximum)
  1097. return -ERANGE;
  1098. return em28xx_set_ctrl(dev, ctrl);
  1099. }
  1100. }
  1101. }
  1102. if (dev->decoder == EM28XX_TVP5150) {
  1103. em28xx_i2c_call_clients(dev,cmd,arg);
  1104. return 0;
  1105. } else if (!dev->has_msp34xx) {
  1106. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1107. if (ctrl->id == em28xx_qctrl[i].id) {
  1108. if (ctrl->value <
  1109. em28xx_qctrl[i].minimum
  1110. || ctrl->value >
  1111. em28xx_qctrl[i].maximum)
  1112. return -ERANGE;
  1113. return em28xx_set_ctrl(dev, ctrl);
  1114. }
  1115. }
  1116. for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
  1117. if (ctrl->id == saa711x_qctrl[i].id) {
  1118. if (ctrl->value <
  1119. saa711x_qctrl[i].minimum
  1120. || ctrl->value >
  1121. saa711x_qctrl[i].maximum)
  1122. return -ERANGE;
  1123. return saa711x_set_ctrl(dev, ctrl);
  1124. }
  1125. }
  1126. }
  1127. return -EINVAL;
  1128. }
  1129. /* --- tuner ioctls ------------------------------------------ */
  1130. case VIDIOC_G_TUNER:
  1131. {
  1132. struct v4l2_tuner *t = arg;
  1133. int status = 0;
  1134. if (0 != t->index)
  1135. return -EINVAL;
  1136. memset(t, 0, sizeof(*t));
  1137. strcpy(t->name, "Tuner");
  1138. t->type = V4L2_TUNER_ANALOG_TV;
  1139. t->capability = V4L2_TUNER_CAP_NORM;
  1140. t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
  1141. /* t->signal = 0xffff;*/
  1142. /* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
  1143. /* No way to get signal strength? */
  1144. mutex_lock(&dev->lock);
  1145. em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
  1146. &status);
  1147. mutex_unlock(&dev->lock);
  1148. t->signal =
  1149. (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
  1150. em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
  1151. t->afc);
  1152. return 0;
  1153. }
  1154. case VIDIOC_S_TUNER:
  1155. {
  1156. struct v4l2_tuner *t = arg;
  1157. int status = 0;
  1158. if (0 != t->index)
  1159. return -EINVAL;
  1160. memset(t, 0, sizeof(*t));
  1161. strcpy(t->name, "Tuner");
  1162. t->type = V4L2_TUNER_ANALOG_TV;
  1163. t->capability = V4L2_TUNER_CAP_NORM;
  1164. t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
  1165. /* t->signal = 0xffff; */
  1166. /* No way to get signal strength? */
  1167. mutex_lock(&dev->lock);
  1168. em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
  1169. &status);
  1170. mutex_unlock(&dev->lock);
  1171. t->signal =
  1172. (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
  1173. em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
  1174. t->signal, t->afc);
  1175. return 0;
  1176. }
  1177. case VIDIOC_G_FREQUENCY:
  1178. {
  1179. struct v4l2_frequency *f = arg;
  1180. memset(f, 0, sizeof(*f));
  1181. f->type = V4L2_TUNER_ANALOG_TV;
  1182. f->frequency = dev->ctl_freq;
  1183. return 0;
  1184. }
  1185. case VIDIOC_S_FREQUENCY:
  1186. {
  1187. struct v4l2_frequency *f = arg;
  1188. if (0 != f->tuner)
  1189. return -EINVAL;
  1190. if (V4L2_TUNER_ANALOG_TV != f->type)
  1191. return -EINVAL;
  1192. mutex_lock(&dev->lock);
  1193. dev->ctl_freq = f->frequency;
  1194. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  1195. mutex_unlock(&dev->lock);
  1196. return 0;
  1197. }
  1198. case VIDIOC_CROPCAP:
  1199. {
  1200. struct v4l2_cropcap *cc = arg;
  1201. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1202. return -EINVAL;
  1203. cc->bounds.left = 0;
  1204. cc->bounds.top = 0;
  1205. cc->bounds.width = dev->width;
  1206. cc->bounds.height = dev->height;
  1207. cc->defrect = cc->bounds;
  1208. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1209. cc->pixelaspect.denominator = 59;
  1210. return 0;
  1211. }
  1212. case VIDIOC_STREAMON:
  1213. {
  1214. int *type = arg;
  1215. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1216. || dev->io != IO_MMAP)
  1217. return -EINVAL;
  1218. if (list_empty(&dev->inqueue))
  1219. return -EINVAL;
  1220. dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
  1221. em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
  1222. return 0;
  1223. }
  1224. case VIDIOC_STREAMOFF:
  1225. {
  1226. int *type = arg;
  1227. int ret;
  1228. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1229. || dev->io != IO_MMAP)
  1230. return -EINVAL;
  1231. if (dev->stream == STREAM_ON) {
  1232. em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
  1233. if ((ret = em28xx_stream_interrupt(dev)))
  1234. return ret;
  1235. }
  1236. em28xx_empty_framequeues(dev);
  1237. return 0;
  1238. }
  1239. default:
  1240. return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
  1241. driver_ioctl);
  1242. }
  1243. return 0;
  1244. }
  1245. /*
  1246. * em28xx_v4l2_do_ioctl()
  1247. * This function is _not_ called directly, but from
  1248. * em28xx_v4l2_ioctl. Userspace
  1249. * copying is done already, arg is a kernel pointer.
  1250. */
  1251. static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
  1252. unsigned int cmd, void *arg)
  1253. {
  1254. struct em28xx *dev = filp->private_data;
  1255. if (!dev)
  1256. return -ENODEV;
  1257. if (video_debug > 1)
  1258. v4l_print_ioctl(dev->name,cmd);
  1259. switch (cmd) {
  1260. /* --- capabilities ------------------------------------------ */
  1261. case VIDIOC_QUERYCAP:
  1262. {
  1263. struct v4l2_capability *cap = arg;
  1264. memset(cap, 0, sizeof(*cap));
  1265. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1266. strlcpy(cap->card, em28xx_boards[dev->model].name,
  1267. sizeof(cap->card));
  1268. strlcpy(cap->bus_info, dev->udev->dev.bus_id,
  1269. sizeof(cap->bus_info));
  1270. cap->version = EM28XX_VERSION_CODE;
  1271. cap->capabilities =
  1272. V4L2_CAP_SLICED_VBI_CAPTURE |
  1273. V4L2_CAP_VIDEO_CAPTURE |
  1274. V4L2_CAP_AUDIO |
  1275. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1276. if (dev->has_tuner)
  1277. cap->capabilities |= V4L2_CAP_TUNER;
  1278. return 0;
  1279. }
  1280. /* --- capture ioctls ---------------------------------------- */
  1281. case VIDIOC_ENUM_FMT:
  1282. {
  1283. struct v4l2_fmtdesc *fmtd = arg;
  1284. if (fmtd->index != 0)
  1285. return -EINVAL;
  1286. memset(fmtd, 0, sizeof(*fmtd));
  1287. fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1288. strcpy(fmtd->description, "Packed YUY2");
  1289. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  1290. memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
  1291. return 0;
  1292. }
  1293. case VIDIOC_G_FMT:
  1294. return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
  1295. case VIDIOC_TRY_FMT:
  1296. case VIDIOC_S_FMT:
  1297. return em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
  1298. case VIDIOC_REQBUFS:
  1299. {
  1300. struct v4l2_requestbuffers *rb = arg;
  1301. u32 i;
  1302. int ret;
  1303. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1304. rb->memory != V4L2_MEMORY_MMAP)
  1305. return -EINVAL;
  1306. if (dev->io == IO_READ) {
  1307. em28xx_videodbg ("method is set to read;"
  1308. " close and open the device again to"
  1309. " choose the mmap I/O method\n");
  1310. return -EINVAL;
  1311. }
  1312. for (i = 0; i < dev->num_frames; i++)
  1313. if (dev->frame[i].vma_use_count) {
  1314. em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
  1315. return -EINVAL;
  1316. }
  1317. if (dev->stream == STREAM_ON) {
  1318. em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
  1319. if ((ret = em28xx_stream_interrupt(dev)))
  1320. return ret;
  1321. }
  1322. em28xx_empty_framequeues(dev);
  1323. em28xx_release_buffers(dev);
  1324. if (rb->count)
  1325. rb->count =
  1326. em28xx_request_buffers(dev, rb->count);
  1327. dev->frame_current = NULL;
  1328. em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
  1329. rb->count);
  1330. dev->io = rb->count ? IO_MMAP : IO_NONE;
  1331. return 0;
  1332. }
  1333. case VIDIOC_QUERYBUF:
  1334. {
  1335. struct v4l2_buffer *b = arg;
  1336. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1337. b->index >= dev->num_frames || dev->io != IO_MMAP)
  1338. return -EINVAL;
  1339. memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
  1340. if (dev->frame[b->index].vma_use_count) {
  1341. b->flags |= V4L2_BUF_FLAG_MAPPED;
  1342. }
  1343. if (dev->frame[b->index].state == F_DONE)
  1344. b->flags |= V4L2_BUF_FLAG_DONE;
  1345. else if (dev->frame[b->index].state != F_UNUSED)
  1346. b->flags |= V4L2_BUF_FLAG_QUEUED;
  1347. return 0;
  1348. }
  1349. case VIDIOC_QBUF:
  1350. {
  1351. struct v4l2_buffer *b = arg;
  1352. unsigned long lock_flags;
  1353. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1354. b->index >= dev->num_frames || dev->io != IO_MMAP) {
  1355. return -EINVAL;
  1356. }
  1357. if (dev->frame[b->index].state != F_UNUSED) {
  1358. return -EAGAIN;
  1359. }
  1360. dev->frame[b->index].state = F_QUEUED;
  1361. /* add frame to fifo */
  1362. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  1363. list_add_tail(&dev->frame[b->index].frame,
  1364. &dev->inqueue);
  1365. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  1366. return 0;
  1367. }
  1368. case VIDIOC_DQBUF:
  1369. {
  1370. struct v4l2_buffer *b = arg;
  1371. struct em28xx_frame_t *f;
  1372. unsigned long lock_flags;
  1373. int ret = 0;
  1374. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1375. || dev->io != IO_MMAP)
  1376. return -EINVAL;
  1377. if (list_empty(&dev->outqueue)) {
  1378. if (dev->stream == STREAM_OFF)
  1379. return -EINVAL;
  1380. if (filp->f_flags & O_NONBLOCK)
  1381. return -EAGAIN;
  1382. ret = wait_event_interruptible
  1383. (dev->wait_frame,
  1384. (!list_empty(&dev->outqueue)) ||
  1385. (dev->state & DEV_DISCONNECTED));
  1386. if (ret)
  1387. return ret;
  1388. if (dev->state & DEV_DISCONNECTED)
  1389. return -ENODEV;
  1390. }
  1391. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  1392. f = list_entry(dev->outqueue.next,
  1393. struct em28xx_frame_t, frame);
  1394. list_del(dev->outqueue.next);
  1395. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  1396. f->state = F_UNUSED;
  1397. memcpy(b, &f->buf, sizeof(*b));
  1398. if (f->vma_use_count)
  1399. b->flags |= V4L2_BUF_FLAG_MAPPED;
  1400. return 0;
  1401. }
  1402. default:
  1403. return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
  1404. em28xx_video_do_ioctl);
  1405. }
  1406. return 0;
  1407. }
  1408. /*
  1409. * em28xx_v4l2_ioctl()
  1410. * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
  1411. */
  1412. static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
  1413. unsigned int cmd, unsigned long arg)
  1414. {
  1415. int ret = 0;
  1416. struct em28xx *dev = filp->private_data;
  1417. if (mutex_lock_interruptible(&dev->fileop_lock))
  1418. return -ERESTARTSYS;
  1419. if (dev->state & DEV_DISCONNECTED) {
  1420. em28xx_errdev("v4l2 ioctl: device not present\n");
  1421. mutex_unlock(&dev->fileop_lock);
  1422. return -ENODEV;
  1423. }
  1424. if (dev->state & DEV_MISCONFIGURED) {
  1425. em28xx_errdev
  1426. ("v4l2 ioctl: device is misconfigured; close and open it again\n");
  1427. mutex_unlock(&dev->fileop_lock);
  1428. return -EIO;
  1429. }
  1430. ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
  1431. mutex_unlock(&dev->fileop_lock);
  1432. return ret;
  1433. }
  1434. static struct file_operations em28xx_v4l_fops = {
  1435. .owner = THIS_MODULE,
  1436. .open = em28xx_v4l2_open,
  1437. .release = em28xx_v4l2_close,
  1438. .ioctl = em28xx_v4l2_ioctl,
  1439. .read = em28xx_v4l2_read,
  1440. .poll = em28xx_v4l2_poll,
  1441. .mmap = em28xx_v4l2_mmap,
  1442. .llseek = no_llseek,
  1443. .compat_ioctl = v4l_compat_ioctl32,
  1444. };
  1445. /******************************** usb interface *****************************************/
  1446. /*
  1447. * em28xx_init_dev()
  1448. * allocates and inits the device structs, registers i2c bus and v4l device
  1449. */
  1450. static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
  1451. int minor, int model)
  1452. {
  1453. struct em28xx *dev = *devhandle;
  1454. int retval = -ENOMEM;
  1455. int errCode, i;
  1456. unsigned int maxh, maxw;
  1457. dev->udev = udev;
  1458. dev->model = model;
  1459. mutex_init(&dev->lock);
  1460. init_waitqueue_head(&dev->open);
  1461. dev->em28xx_write_regs = em28xx_write_regs;
  1462. dev->em28xx_read_reg = em28xx_read_reg;
  1463. dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
  1464. dev->em28xx_write_regs_req = em28xx_write_regs_req;
  1465. dev->em28xx_read_reg_req = em28xx_read_reg_req;
  1466. dev->is_em2800 = em28xx_boards[model].is_em2800;
  1467. dev->has_tuner = em28xx_boards[model].has_tuner;
  1468. dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
  1469. dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
  1470. dev->decoder = em28xx_boards[model].decoder;
  1471. if (tuner >= 0)
  1472. dev->tuner_type = tuner;
  1473. else
  1474. dev->tuner_type = em28xx_boards[model].tuner_type;
  1475. dev->video_inputs = em28xx_boards[model].vchannels;
  1476. for (i = 0; i < TVNORMS; i++)
  1477. if (em28xx_boards[model].norm == tvnorms[i].mode)
  1478. break;
  1479. if (i == TVNORMS)
  1480. i = 0;
  1481. dev->tvnorm = &tvnorms[i]; /* set default norm */
  1482. em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
  1483. maxw = norm_maxw(dev);
  1484. maxh = norm_maxh(dev);
  1485. /* set default image size */
  1486. dev->width = maxw;
  1487. dev->height = maxh;
  1488. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1489. dev->field_size = dev->width * dev->height;
  1490. dev->frame_size =
  1491. dev->interlaced ? dev->field_size << 1 : dev->field_size;
  1492. dev->bytesperline = dev->width * 2;
  1493. dev->hscale = 0;
  1494. dev->vscale = 0;
  1495. dev->ctl_input = 2;
  1496. /* setup video picture settings for saa7113h */
  1497. memset(&dev->vpic, 0, sizeof(dev->vpic));
  1498. dev->vpic.colour = 128 << 8;
  1499. dev->vpic.hue = 128 << 8;
  1500. dev->vpic.brightness = 128 << 8;
  1501. dev->vpic.contrast = 192 << 8;
  1502. dev->vpic.whiteness = 128 << 8; /* This one isn't used */
  1503. dev->vpic.depth = 16;
  1504. dev->vpic.palette = VIDEO_PALETTE_YUV422;
  1505. em28xx_pre_card_setup(dev);
  1506. #ifdef CONFIG_MODULES
  1507. /* request some modules */
  1508. if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
  1509. request_module("saa711x");
  1510. if (dev->decoder == EM28XX_TVP5150)
  1511. request_module("tvp5150");
  1512. if (dev->has_tuner)
  1513. request_module("tuner");
  1514. if (dev->tda9887_conf)
  1515. request_module("tda9887");
  1516. #endif
  1517. errCode = em28xx_config(dev);
  1518. if (errCode) {
  1519. em28xx_errdev("error configuring device\n");
  1520. kfree(dev);
  1521. em28xx_devused&=~(1<<dev->devno);
  1522. return -ENOMEM;
  1523. }
  1524. mutex_lock(&dev->lock);
  1525. /* register i2c bus */
  1526. em28xx_i2c_register(dev);
  1527. /* Do board specific init and eeprom reading */
  1528. em28xx_card_setup(dev);
  1529. /* configure the device */
  1530. em28xx_config_i2c(dev);
  1531. mutex_unlock(&dev->lock);
  1532. errCode = em28xx_config(dev);
  1533. #ifdef CONFIG_MODULES
  1534. if (dev->has_msp34xx)
  1535. request_module("msp3400");
  1536. #endif
  1537. /* allocate and fill v4l2 device struct */
  1538. dev->vdev = video_device_alloc();
  1539. if (NULL == dev->vdev) {
  1540. em28xx_errdev("cannot allocate video_device.\n");
  1541. kfree(dev);
  1542. em28xx_devused&=~(1<<dev->devno);
  1543. return -ENOMEM;
  1544. }
  1545. dev->vbi_dev = video_device_alloc();
  1546. if (NULL == dev->vbi_dev) {
  1547. em28xx_errdev("cannot allocate video_device.\n");
  1548. kfree(dev->vdev);
  1549. kfree(dev);
  1550. em28xx_devused&=~(1<<dev->devno);
  1551. return -ENOMEM;
  1552. }
  1553. /* Fills VBI device info */
  1554. dev->vbi_dev->type = VFL_TYPE_VBI;
  1555. dev->vbi_dev->hardware = 0;
  1556. dev->vbi_dev->fops = &em28xx_v4l_fops;
  1557. dev->vbi_dev->minor = -1;
  1558. dev->vbi_dev->dev = &dev->udev->dev;
  1559. dev->vbi_dev->release = video_device_release;
  1560. snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
  1561. "em28xx",dev->devno,"vbi");
  1562. /* Fills CAPTURE device info */
  1563. dev->vdev->type = VID_TYPE_CAPTURE;
  1564. if (dev->has_tuner)
  1565. dev->vdev->type |= VID_TYPE_TUNER;
  1566. dev->vdev->hardware = 0;
  1567. dev->vdev->fops = &em28xx_v4l_fops;
  1568. dev->vdev->minor = -1;
  1569. dev->vdev->dev = &dev->udev->dev;
  1570. dev->vdev->release = video_device_release;
  1571. snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
  1572. "em28xx",dev->devno,"video");
  1573. list_add_tail(&dev->devlist,&em28xx_devlist);
  1574. /* register v4l2 device */
  1575. mutex_lock(&dev->lock);
  1576. if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1577. video_nr[dev->devno]))) {
  1578. em28xx_errdev("unable to register video device (error=%i).\n",
  1579. retval);
  1580. mutex_unlock(&dev->lock);
  1581. list_del(&dev->devlist);
  1582. video_device_release(dev->vdev);
  1583. kfree(dev);
  1584. em28xx_devused&=~(1<<dev->devno);
  1585. return -ENODEV;
  1586. }
  1587. if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1588. vbi_nr[dev->devno]) < 0) {
  1589. printk("unable to register vbi device\n");
  1590. mutex_unlock(&dev->lock);
  1591. list_del(&dev->devlist);
  1592. video_device_release(dev->vbi_dev);
  1593. video_device_release(dev->vdev);
  1594. kfree(dev);
  1595. em28xx_devused&=~(1<<dev->devno);
  1596. return -ENODEV;
  1597. } else {
  1598. printk("registered VBI\n");
  1599. }
  1600. if (dev->has_msp34xx) {
  1601. /* Send a reset to other chips via gpio */
  1602. em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
  1603. udelay(2500);
  1604. em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
  1605. udelay(2500);
  1606. }
  1607. video_mux(dev, 0);
  1608. mutex_unlock(&dev->lock);
  1609. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1610. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
  1611. dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
  1612. return 0;
  1613. }
  1614. /*
  1615. * em28xx_usb_probe()
  1616. * checks for supported devices
  1617. */
  1618. static int em28xx_usb_probe(struct usb_interface *interface,
  1619. const struct usb_device_id *id)
  1620. {
  1621. const struct usb_endpoint_descriptor *endpoint;
  1622. struct usb_device *udev;
  1623. struct usb_interface *uif;
  1624. struct em28xx *dev = NULL;
  1625. int retval = -ENODEV;
  1626. int model,i,nr,ifnum;
  1627. udev = usb_get_dev(interface_to_usbdev(interface));
  1628. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  1629. /* Check to see next free device and mark as used */
  1630. nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
  1631. em28xx_devused|=1<<nr;
  1632. /* Don't register audio interfaces */
  1633. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1634. em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
  1635. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1636. ifnum,
  1637. interface->altsetting[0].desc.bInterfaceClass);
  1638. em28xx_devused&=~(1<<nr);
  1639. return -ENODEV;
  1640. }
  1641. em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
  1642. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1643. ifnum,
  1644. interface->altsetting[0].desc.bInterfaceClass);
  1645. endpoint = &interface->cur_altsetting->endpoint[1].desc;
  1646. /* check if the the device has the iso in endpoint at the correct place */
  1647. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1648. USB_ENDPOINT_XFER_ISOC) {
  1649. em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
  1650. em28xx_devused&=~(1<<nr);
  1651. return -ENODEV;
  1652. }
  1653. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1654. em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
  1655. em28xx_devused&=~(1<<nr);
  1656. return -ENODEV;
  1657. }
  1658. model=id->driver_info;
  1659. if (nr >= EM28XX_MAXBOARDS) {
  1660. printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
  1661. em28xx_devused&=~(1<<nr);
  1662. return -ENOMEM;
  1663. }
  1664. /* allocate memory for our device state and initialize it */
  1665. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1666. if (dev == NULL) {
  1667. em28xx_err(DRIVER_NAME ": out of memory!\n");
  1668. em28xx_devused&=~(1<<nr);
  1669. return -ENOMEM;
  1670. }
  1671. snprintf(dev->name, 29, "em28xx #%d", nr);
  1672. dev->devno=nr;
  1673. /* compute alternate max packet sizes */
  1674. uif = udev->actconfig->interface[0];
  1675. dev->num_alt=uif->num_altsetting;
  1676. em28xx_info("Alternate settings: %i\n",dev->num_alt);
  1677. // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
  1678. dev->alt_max_pkt_size = kmalloc(32*
  1679. dev->num_alt,GFP_KERNEL);
  1680. if (dev->alt_max_pkt_size == NULL) {
  1681. em28xx_errdev("out of memory!\n");
  1682. em28xx_devused&=~(1<<nr);
  1683. return -ENOMEM;
  1684. }
  1685. for (i = 0; i < dev->num_alt ; i++) {
  1686. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1687. wMaxPacketSize);
  1688. dev->alt_max_pkt_size[i] =
  1689. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1690. em28xx_info("Alternate setting %i, max size= %i\n",i,
  1691. dev->alt_max_pkt_size[i]);
  1692. }
  1693. if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
  1694. model=card[nr];
  1695. if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
  1696. em28xx_errdev( "Your board has no eeprom inside it and thus can't\n"
  1697. "%s: be autodetected. Please pass card=<n> insmod option to\n"
  1698. "%s: workaround that. Redirect complaints to the vendor of\n"
  1699. "%s: the TV card. Generic type will be used."
  1700. "%s: Best regards,\n"
  1701. "%s: -- tux\n",
  1702. dev->name,dev->name,dev->name,dev->name,dev->name);
  1703. em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
  1704. dev->name);
  1705. for (i = 0; i < em28xx_bcount; i++) {
  1706. em28xx_errdev(" card=%d -> %s\n", i,
  1707. em28xx_boards[i].name);
  1708. }
  1709. }
  1710. /* allocate device struct */
  1711. retval = em28xx_init_dev(&dev, udev, nr, model);
  1712. if (retval)
  1713. return retval;
  1714. em28xx_info("Found %s\n", em28xx_boards[model].name);
  1715. /* save our data pointer in this interface device */
  1716. usb_set_intfdata(interface, dev);
  1717. return 0;
  1718. }
  1719. /*
  1720. * em28xx_usb_disconnect()
  1721. * called when the device gets diconencted
  1722. * video device will be unregistered on v4l2_close in case it is still open
  1723. */
  1724. static void em28xx_usb_disconnect(struct usb_interface *interface)
  1725. {
  1726. struct em28xx *dev = usb_get_intfdata(interface);
  1727. usb_set_intfdata(interface, NULL);
  1728. if (!dev)
  1729. return;
  1730. down_write(&em28xx_disconnect);
  1731. mutex_lock(&dev->lock);
  1732. em28xx_info("disconnecting %s\n", dev->vdev->name);
  1733. wake_up_interruptible_all(&dev->open);
  1734. if (dev->users) {
  1735. em28xx_warn
  1736. ("device /dev/video%d is open! Deregistration and memory "
  1737. "deallocation are deferred on close.\n",
  1738. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
  1739. dev->state |= DEV_MISCONFIGURED;
  1740. em28xx_uninit_isoc(dev);
  1741. dev->state |= DEV_DISCONNECTED;
  1742. wake_up_interruptible(&dev->wait_frame);
  1743. wake_up_interruptible(&dev->wait_stream);
  1744. } else {
  1745. dev->state |= DEV_DISCONNECTED;
  1746. em28xx_release_resources(dev);
  1747. }
  1748. mutex_unlock(&dev->lock);
  1749. if (!dev->users) {
  1750. kfree(dev->alt_max_pkt_size);
  1751. kfree(dev);
  1752. }
  1753. up_write(&em28xx_disconnect);
  1754. }
  1755. static struct usb_driver em28xx_usb_driver = {
  1756. .name = "em28xx",
  1757. .probe = em28xx_usb_probe,
  1758. .disconnect = em28xx_usb_disconnect,
  1759. .id_table = em28xx_id_table,
  1760. };
  1761. static int __init em28xx_module_init(void)
  1762. {
  1763. int result;
  1764. printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
  1765. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1766. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1767. #ifdef SNAPSHOT
  1768. printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
  1769. SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
  1770. #endif
  1771. /* register this driver with the USB subsystem */
  1772. result = usb_register(&em28xx_usb_driver);
  1773. if (result)
  1774. em28xx_err(DRIVER_NAME
  1775. " usb_register failed. Error number %d.\n", result);
  1776. return result;
  1777. }
  1778. static void __exit em28xx_module_exit(void)
  1779. {
  1780. /* deregister this driver with the USB subsystem */
  1781. usb_deregister(&em28xx_usb_driver);
  1782. }
  1783. module_init(em28xx_module_init);
  1784. module_exit(em28xx_module_exit);