em28xx-video.c 47 KB

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