em28xx-video.c 46 KB

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