em28xx-video.c 49 KB

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