em28xx-video.c 47 KB

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