em28xx-video.c 49 KB

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