em28xx-video.c 49 KB

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