em28xx-video.c 46 KB

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