em28xx-video.c 46 KB

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