em28xx-video.c 48 KB

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