em28xx-video.c 46 KB

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