em28xx-video.c 49 KB

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