em28xx-video.c 47 KB

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