em28xx-video.c 46 KB

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