em28xx-video.c 46 KB

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