em28xx-video.c 46 KB

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