gspca.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  1. /*
  2. * Main USB camera driver
  3. *
  4. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #define MODULE_NAME "gspca"
  21. #include <linux/init.h>
  22. #include <linux/fs.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/string.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/io.h>
  30. #include <asm/page.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/jiffies.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include "gspca.h"
  35. /* global values */
  36. #define DEF_NURBS 2 /* default number of URBs */
  37. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  38. MODULE_DESCRIPTION("GSPCA USB Camera Driver");
  39. MODULE_LICENSE("GPL");
  40. #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 1, 7)
  41. static const char version[] = "2.1.7";
  42. static int video_nr = -1;
  43. #ifdef CONFIG_VIDEO_ADV_DEBUG
  44. int gspca_debug = D_ERR | D_PROBE;
  45. EXPORT_SYMBOL(gspca_debug);
  46. static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
  47. {
  48. if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
  49. PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
  50. txt,
  51. pixfmt & 0xff,
  52. (pixfmt >> 8) & 0xff,
  53. (pixfmt >> 16) & 0xff,
  54. pixfmt >> 24,
  55. w, h);
  56. } else {
  57. PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
  58. txt,
  59. pixfmt,
  60. w, h);
  61. }
  62. }
  63. #else
  64. #define PDEBUG_MODE(txt, pixfmt, w, h)
  65. #endif
  66. /* specific memory types - !! should different from V4L2_MEMORY_xxx */
  67. #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
  68. #define GSPCA_MEMORY_READ 7
  69. #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
  70. /*
  71. * VMA operations.
  72. */
  73. static void gspca_vm_open(struct vm_area_struct *vma)
  74. {
  75. struct gspca_frame *frame = vma->vm_private_data;
  76. frame->vma_use_count++;
  77. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
  78. }
  79. static void gspca_vm_close(struct vm_area_struct *vma)
  80. {
  81. struct gspca_frame *frame = vma->vm_private_data;
  82. if (--frame->vma_use_count <= 0)
  83. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  84. }
  85. static struct vm_operations_struct gspca_vm_ops = {
  86. .open = gspca_vm_open,
  87. .close = gspca_vm_close,
  88. };
  89. /*
  90. * fill a video frame from an URB and resubmit
  91. */
  92. static void fill_frame(struct gspca_dev *gspca_dev,
  93. struct urb *urb)
  94. {
  95. struct gspca_frame *frame;
  96. __u8 *data; /* address of data in the iso message */
  97. int i, j, len, st;
  98. cam_pkt_op pkt_scan;
  99. if (urb->status != 0) {
  100. PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
  101. return; /* disconnection ? */
  102. }
  103. pkt_scan = gspca_dev->sd_desc->pkt_scan;
  104. for (i = 0; i < urb->number_of_packets; i++) {
  105. /* check the availability of the frame buffer */
  106. j = gspca_dev->fr_i;
  107. j = gspca_dev->fr_queue[j];
  108. frame = &gspca_dev->frame[j];
  109. if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
  110. != V4L2_BUF_FLAG_QUEUED) {
  111. gspca_dev->last_packet_type = DISCARD_PACKET;
  112. break;
  113. }
  114. /* check the packet status and length */
  115. len = urb->iso_frame_desc[i].actual_length;
  116. if (len == 0)
  117. continue;
  118. st = urb->iso_frame_desc[i].status;
  119. if (st) {
  120. PDEBUG(D_ERR,
  121. "ISOC data error: [%d] len=%d, status=%d",
  122. i, len, st);
  123. gspca_dev->last_packet_type = DISCARD_PACKET;
  124. continue;
  125. }
  126. /* let the packet be analyzed by the subdriver */
  127. PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
  128. i, urb->iso_frame_desc[i].offset, len);
  129. data = (__u8 *) urb->transfer_buffer
  130. + urb->iso_frame_desc[i].offset;
  131. pkt_scan(gspca_dev, frame, data, len);
  132. }
  133. /* resubmit the URB */
  134. urb->status = 0;
  135. st = usb_submit_urb(urb, GFP_ATOMIC);
  136. if (st < 0)
  137. PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
  138. }
  139. /*
  140. * ISOC message interrupt from the USB device
  141. *
  142. * Analyse each packet and call the subdriver for copy to the frame buffer.
  143. */
  144. static void isoc_irq(struct urb *urb
  145. )
  146. {
  147. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  148. PDEBUG(D_PACK, "isoc irq");
  149. if (!gspca_dev->streaming)
  150. return;
  151. fill_frame(gspca_dev, urb);
  152. }
  153. /*
  154. * add data to the current frame
  155. *
  156. * This function is called by the subdrivers at interrupt level.
  157. *
  158. * To build a frame, these ones must add
  159. * - one FIRST_PACKET
  160. * - 0 or many INTER_PACKETs
  161. * - one LAST_PACKET
  162. * DISCARD_PACKET invalidates the whole frame.
  163. * On LAST_PACKET, a new frame is returned.
  164. */
  165. struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
  166. int packet_type,
  167. struct gspca_frame *frame,
  168. const __u8 *data,
  169. int len)
  170. {
  171. int i, j;
  172. PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
  173. /* when start of a new frame, if the current frame buffer
  174. * is not queued, discard the whole frame */
  175. if (packet_type == FIRST_PACKET) {
  176. if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
  177. != V4L2_BUF_FLAG_QUEUED) {
  178. gspca_dev->last_packet_type = DISCARD_PACKET;
  179. return frame;
  180. }
  181. frame->data_end = frame->data;
  182. jiffies_to_timeval(get_jiffies_64(),
  183. &frame->v4l2_buf.timestamp);
  184. frame->v4l2_buf.sequence = ++gspca_dev->sequence;
  185. } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
  186. return frame;
  187. }
  188. /* append the packet to the frame buffer */
  189. if (len > 0) {
  190. if (frame->data_end - frame->data + len
  191. > frame->v4l2_buf.length) {
  192. PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
  193. frame->data_end - frame->data + len,
  194. frame->v4l2_buf.length);
  195. packet_type = DISCARD_PACKET;
  196. } else {
  197. memcpy(frame->data_end, data, len);
  198. frame->data_end += len;
  199. }
  200. }
  201. gspca_dev->last_packet_type = packet_type;
  202. /* if last packet, wake the application and advance in the queue */
  203. if (packet_type == LAST_PACKET) {
  204. frame->v4l2_buf.bytesused = frame->data_end - frame->data;
  205. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  206. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
  207. atomic_inc(&gspca_dev->nevent);
  208. wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
  209. i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
  210. gspca_dev->fr_i = i;
  211. PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
  212. frame->v4l2_buf.bytesused,
  213. gspca_dev->fr_q,
  214. i,
  215. gspca_dev->fr_o);
  216. j = gspca_dev->fr_queue[i];
  217. frame = &gspca_dev->frame[j];
  218. }
  219. return frame;
  220. }
  221. EXPORT_SYMBOL(gspca_frame_add);
  222. static int gspca_is_compressed(__u32 format)
  223. {
  224. switch (format) {
  225. case V4L2_PIX_FMT_MJPEG:
  226. case V4L2_PIX_FMT_JPEG:
  227. case V4L2_PIX_FMT_SPCA561:
  228. case V4L2_PIX_FMT_PAC207:
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. static void *rvmalloc(unsigned long size)
  234. {
  235. void *mem;
  236. unsigned long adr;
  237. /* size = PAGE_ALIGN(size); (already done) */
  238. mem = vmalloc_32(size);
  239. if (mem != NULL) {
  240. adr = (unsigned long) mem;
  241. while ((long) size > 0) {
  242. SetPageReserved(vmalloc_to_page((void *) adr));
  243. adr += PAGE_SIZE;
  244. size -= PAGE_SIZE;
  245. }
  246. }
  247. return mem;
  248. }
  249. static void rvfree(void *mem, long size)
  250. {
  251. unsigned long adr;
  252. adr = (unsigned long) mem;
  253. while (size > 0) {
  254. ClearPageReserved(vmalloc_to_page((void *) adr));
  255. adr += PAGE_SIZE;
  256. size -= PAGE_SIZE;
  257. }
  258. vfree(mem);
  259. }
  260. static int frame_alloc(struct gspca_dev *gspca_dev,
  261. unsigned int count)
  262. {
  263. struct gspca_frame *frame;
  264. unsigned int frsz;
  265. int i;
  266. i = gspca_dev->curr_mode;
  267. frsz = gspca_dev->cam.cam_mode[i].sizeimage;
  268. PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
  269. frsz = PAGE_ALIGN(frsz);
  270. gspca_dev->frsz = frsz;
  271. if (count > GSPCA_MAX_FRAMES)
  272. count = GSPCA_MAX_FRAMES;
  273. gspca_dev->frbuf = rvmalloc(frsz * count);
  274. if (!gspca_dev->frbuf) {
  275. err("frame alloc failed");
  276. return -ENOMEM;
  277. }
  278. gspca_dev->nframes = count;
  279. for (i = 0; i < count; i++) {
  280. frame = &gspca_dev->frame[i];
  281. frame->v4l2_buf.index = i;
  282. frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  283. frame->v4l2_buf.flags = 0;
  284. frame->v4l2_buf.field = V4L2_FIELD_NONE;
  285. frame->v4l2_buf.length = frsz;
  286. frame->v4l2_buf.memory = gspca_dev->memory;
  287. frame->v4l2_buf.sequence = 0;
  288. frame->data = frame->data_end =
  289. gspca_dev->frbuf + i * frsz;
  290. frame->v4l2_buf.m.offset = i * frsz;
  291. }
  292. gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
  293. gspca_dev->last_packet_type = DISCARD_PACKET;
  294. gspca_dev->sequence = 0;
  295. atomic_set(&gspca_dev->nevent, 0);
  296. return 0;
  297. }
  298. static void frame_free(struct gspca_dev *gspca_dev)
  299. {
  300. int i;
  301. PDEBUG(D_STREAM, "frame free");
  302. if (gspca_dev->frbuf != NULL) {
  303. rvfree(gspca_dev->frbuf,
  304. gspca_dev->nframes * gspca_dev->frsz);
  305. gspca_dev->frbuf = NULL;
  306. for (i = 0; i < gspca_dev->nframes; i++)
  307. gspca_dev->frame[i].data = NULL;
  308. }
  309. gspca_dev->nframes = 0;
  310. }
  311. static void destroy_urbs(struct gspca_dev *gspca_dev)
  312. {
  313. struct urb *urb;
  314. unsigned int i;
  315. PDEBUG(D_STREAM, "kill transfer");
  316. for (i = 0; i < MAX_NURBS; ++i) {
  317. urb = gspca_dev->urb[i];
  318. if (urb == NULL)
  319. break;
  320. gspca_dev->urb[i] = NULL;
  321. usb_kill_urb(urb);
  322. if (urb->transfer_buffer != NULL)
  323. usb_buffer_free(gspca_dev->dev,
  324. urb->transfer_buffer_length,
  325. urb->transfer_buffer,
  326. urb->transfer_dma);
  327. usb_free_urb(urb);
  328. }
  329. }
  330. /*
  331. * search an input isochronous endpoint in an alternate setting
  332. */
  333. static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
  334. __u8 epaddr)
  335. {
  336. struct usb_host_endpoint *ep;
  337. int i, attr;
  338. epaddr |= USB_DIR_IN;
  339. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  340. ep = &alt->endpoint[i];
  341. if (ep->desc.bEndpointAddress == epaddr) {
  342. attr = ep->desc.bmAttributes
  343. & USB_ENDPOINT_XFERTYPE_MASK;
  344. if (attr == USB_ENDPOINT_XFER_ISOC)
  345. return ep;
  346. break;
  347. }
  348. }
  349. return NULL;
  350. }
  351. /*
  352. * search an input isochronous endpoint
  353. *
  354. * The endpoint is defined by the subdriver.
  355. * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
  356. * This routine may be called many times when the bandwidth is too small
  357. * (the bandwidth is checked on urb submit).
  358. */
  359. struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
  360. {
  361. struct usb_interface *intf;
  362. struct usb_host_endpoint *ep;
  363. int i, ret;
  364. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  365. ep = NULL;
  366. i = gspca_dev->alt; /* previous alt setting */
  367. while (--i > 0) { /* alt 0 is unusable */
  368. ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
  369. if (ep)
  370. break;
  371. }
  372. if (ep == NULL) {
  373. err("no ISOC endpoint found");
  374. return NULL;
  375. }
  376. PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
  377. i, ep->desc.bEndpointAddress);
  378. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
  379. if (ret < 0) {
  380. err("set interface err %d", ret);
  381. return NULL;
  382. }
  383. gspca_dev->alt = i; /* memorize the current alt setting */
  384. return ep;
  385. }
  386. /*
  387. * create the isochronous URBs
  388. */
  389. static int create_urbs(struct gspca_dev *gspca_dev,
  390. struct usb_host_endpoint *ep)
  391. {
  392. struct urb *urb;
  393. int n, nurbs, i, psize, npkt, bsize;
  394. /* calculate the packet size and the number of packets */
  395. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  396. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  397. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  398. npkt = ISO_MAX_SIZE / psize;
  399. if (npkt > ISO_MAX_PKT)
  400. npkt = ISO_MAX_PKT;
  401. bsize = psize * npkt;
  402. PDEBUG(D_STREAM,
  403. "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
  404. nurbs = DEF_NURBS;
  405. gspca_dev->nurbs = nurbs;
  406. for (n = 0; n < nurbs; n++) {
  407. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  408. if (!urb) {
  409. err("usb_alloc_urb failed");
  410. return -ENOMEM;
  411. }
  412. urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
  413. bsize,
  414. GFP_KERNEL,
  415. &urb->transfer_dma);
  416. if (urb->transfer_buffer == NULL) {
  417. usb_free_urb(urb);
  418. destroy_urbs(gspca_dev);
  419. err("usb_buffer_urb failed");
  420. return -ENOMEM;
  421. }
  422. gspca_dev->urb[n] = urb;
  423. urb->dev = gspca_dev->dev;
  424. urb->context = gspca_dev;
  425. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  426. ep->desc.bEndpointAddress);
  427. urb->transfer_flags = URB_ISO_ASAP
  428. | URB_NO_TRANSFER_DMA_MAP;
  429. urb->interval = ep->desc.bInterval;
  430. urb->complete = isoc_irq;
  431. urb->number_of_packets = npkt;
  432. urb->transfer_buffer_length = bsize;
  433. for (i = 0; i < npkt; i++) {
  434. urb->iso_frame_desc[i].length = psize;
  435. urb->iso_frame_desc[i].offset = psize * i;
  436. }
  437. }
  438. return 0;
  439. }
  440. /*
  441. * start the USB transfer
  442. */
  443. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  444. {
  445. struct usb_host_endpoint *ep;
  446. int n, ret;
  447. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  448. return -ERESTARTSYS;
  449. /* set the higher alternate setting and
  450. * loop until urb submit succeeds */
  451. gspca_dev->alt = gspca_dev->nbalt;
  452. for (;;) {
  453. PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
  454. ep = get_isoc_ep(gspca_dev);
  455. if (ep == NULL) {
  456. ret = -EIO;
  457. goto out;
  458. }
  459. ret = create_urbs(gspca_dev, ep);
  460. if (ret < 0)
  461. goto out;
  462. /* start the cam */
  463. gspca_dev->sd_desc->start(gspca_dev);
  464. gspca_dev->streaming = 1;
  465. atomic_set(&gspca_dev->nevent, 0);
  466. /* submit the URBs */
  467. for (n = 0; n < gspca_dev->nurbs; n++) {
  468. ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
  469. if (ret < 0) {
  470. PDEBUG(D_ERR|D_STREAM,
  471. "usb_submit_urb [%d] err %d", n, ret);
  472. gspca_dev->streaming = 0;
  473. destroy_urbs(gspca_dev);
  474. if (ret == -ENOSPC)
  475. break; /* try the previous alt */
  476. goto out;
  477. }
  478. }
  479. if (ret >= 0)
  480. break;
  481. }
  482. out:
  483. mutex_unlock(&gspca_dev->usb_lock);
  484. return ret;
  485. }
  486. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  487. {
  488. int ret;
  489. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  490. if (ret < 0)
  491. PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
  492. return ret;
  493. }
  494. /* Note both the queue and the usb lock should be hold when calling this */
  495. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  496. {
  497. gspca_dev->streaming = 0;
  498. atomic_set(&gspca_dev->nevent, 0);
  499. if (gspca_dev->present) {
  500. gspca_dev->sd_desc->stopN(gspca_dev);
  501. destroy_urbs(gspca_dev);
  502. gspca_set_alt0(gspca_dev);
  503. gspca_dev->sd_desc->stop0(gspca_dev);
  504. PDEBUG(D_STREAM, "stream off OK");
  505. }
  506. }
  507. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  508. {
  509. int i;
  510. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  511. gspca_dev->curr_mode = i;
  512. gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
  513. gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
  514. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
  515. }
  516. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  517. int width, int height)
  518. {
  519. int i;
  520. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  521. if (width >= gspca_dev->cam.cam_mode[i].width
  522. && height >= gspca_dev->cam.cam_mode[i].height)
  523. break;
  524. }
  525. return i;
  526. }
  527. /*
  528. * search a mode with the right pixel format
  529. */
  530. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  531. int mode,
  532. int pixfmt)
  533. {
  534. int modeU, modeD;
  535. modeU = modeD = mode;
  536. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  537. if (--modeD >= 0) {
  538. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  539. == pixfmt)
  540. return modeD;
  541. }
  542. if (++modeU < gspca_dev->cam.nmodes) {
  543. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  544. == pixfmt)
  545. return modeU;
  546. }
  547. }
  548. return -EINVAL;
  549. }
  550. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  551. struct v4l2_fmtdesc *fmtdesc)
  552. {
  553. struct gspca_dev *gspca_dev = priv;
  554. int i, j, index;
  555. __u32 fmt_tb[8];
  556. /* give an index to each format */
  557. index = 0;
  558. j = 0;
  559. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  560. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  561. j = 0;
  562. for (;;) {
  563. if (fmt_tb[j] == fmt_tb[index])
  564. break;
  565. j++;
  566. }
  567. if (j == index) {
  568. if (fmtdesc->index == index)
  569. break; /* new format */
  570. index++;
  571. if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
  572. return -EINVAL;
  573. }
  574. }
  575. if (i < 0)
  576. return -EINVAL; /* no more format */
  577. fmtdesc->pixelformat = fmt_tb[index];
  578. if (gspca_is_compressed(fmt_tb[index]))
  579. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  580. fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  581. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  582. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  583. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  584. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  585. fmtdesc->description[4] = '\0';
  586. return 0;
  587. }
  588. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  589. struct v4l2_format *fmt)
  590. {
  591. struct gspca_dev *gspca_dev = priv;
  592. int mode;
  593. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  594. return -EINVAL;
  595. mode = gspca_dev->curr_mode;
  596. memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
  597. sizeof fmt->fmt.pix);
  598. return 0;
  599. }
  600. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  601. struct v4l2_format *fmt)
  602. {
  603. int w, h, mode, mode2;
  604. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  605. return -EINVAL;
  606. w = fmt->fmt.pix.width;
  607. h = fmt->fmt.pix.height;
  608. #ifdef CONFIG_VIDEO_ADV_DEBUG
  609. if (gspca_debug & D_CONF)
  610. PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
  611. #endif
  612. /* search the closest mode for width and height */
  613. mode = wxh_to_mode(gspca_dev, w, h);
  614. /* OK if right palette */
  615. if (gspca_dev->cam.cam_mode[mode].pixelformat
  616. != fmt->fmt.pix.pixelformat) {
  617. /* else, search the closest mode with the same pixel format */
  618. mode2 = gspca_get_mode(gspca_dev, mode,
  619. fmt->fmt.pix.pixelformat);
  620. if (mode2 >= 0)
  621. mode = mode2;
  622. /* else
  623. ; * no chance, return this mode */
  624. }
  625. memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
  626. sizeof fmt->fmt.pix);
  627. return mode; /* used when s_fmt */
  628. }
  629. static int vidioc_try_fmt_vid_cap(struct file *file,
  630. void *priv,
  631. struct v4l2_format *fmt)
  632. {
  633. struct gspca_dev *gspca_dev = priv;
  634. int ret;
  635. ret = try_fmt_vid_cap(gspca_dev, fmt);
  636. if (ret < 0)
  637. return ret;
  638. return 0;
  639. }
  640. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  641. struct v4l2_format *fmt)
  642. {
  643. struct gspca_dev *gspca_dev = priv;
  644. int ret;
  645. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  646. return -ERESTARTSYS;
  647. ret = try_fmt_vid_cap(gspca_dev, fmt);
  648. if (ret < 0)
  649. goto out;
  650. if (gspca_dev->nframes != 0
  651. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  652. ret = -EINVAL;
  653. goto out;
  654. }
  655. if (ret == gspca_dev->curr_mode) {
  656. ret = 0;
  657. goto out; /* same mode */
  658. }
  659. if (gspca_dev->streaming) {
  660. ret = -EBUSY;
  661. goto out;
  662. }
  663. gspca_dev->width = fmt->fmt.pix.width;
  664. gspca_dev->height = fmt->fmt.pix.height;
  665. gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
  666. gspca_dev->curr_mode = ret;
  667. ret = 0;
  668. out:
  669. mutex_unlock(&gspca_dev->queue_lock);
  670. return ret;
  671. }
  672. static int dev_open(struct inode *inode, struct file *file)
  673. {
  674. struct gspca_dev *gspca_dev;
  675. int ret;
  676. PDEBUG(D_STREAM, "%s open", current->comm);
  677. gspca_dev = (struct gspca_dev *) video_devdata(file);
  678. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  679. return -ERESTARTSYS;
  680. if (!gspca_dev->present) {
  681. ret = -ENODEV;
  682. goto out;
  683. }
  684. /* if not done yet, initialize the sensor */
  685. if (gspca_dev->users == 0) {
  686. if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
  687. ret = -ERESTARTSYS;
  688. goto out;
  689. }
  690. ret = gspca_dev->sd_desc->open(gspca_dev);
  691. mutex_unlock(&gspca_dev->usb_lock);
  692. if (ret != 0) {
  693. PDEBUG(D_ERR|D_CONF, "init device failed %d", ret);
  694. goto out;
  695. }
  696. } else if (gspca_dev->users > 4) { /* (arbitrary value) */
  697. ret = -EBUSY;
  698. goto out;
  699. }
  700. gspca_dev->users++;
  701. file->private_data = gspca_dev;
  702. #ifdef CONFIG_VIDEO_ADV_DEBUG
  703. /* activate the v4l2 debug */
  704. if (gspca_debug & D_V4L2)
  705. gspca_dev->vdev.debug |= 3;
  706. else
  707. gspca_dev->vdev.debug &= ~3;
  708. #endif
  709. out:
  710. mutex_unlock(&gspca_dev->queue_lock);
  711. if (ret != 0)
  712. PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
  713. else
  714. PDEBUG(D_STREAM, "open done");
  715. return ret;
  716. }
  717. static int dev_close(struct inode *inode, struct file *file)
  718. {
  719. struct gspca_dev *gspca_dev = file->private_data;
  720. PDEBUG(D_STREAM, "%s close", current->comm);
  721. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  722. return -ERESTARTSYS;
  723. gspca_dev->users--;
  724. /* if the file did the capture, free the streaming resources */
  725. if (gspca_dev->capt_file == file) {
  726. mutex_lock(&gspca_dev->usb_lock);
  727. if (gspca_dev->streaming)
  728. gspca_stream_off(gspca_dev);
  729. gspca_dev->sd_desc->close(gspca_dev);
  730. mutex_unlock(&gspca_dev->usb_lock);
  731. frame_free(gspca_dev);
  732. gspca_dev->capt_file = NULL;
  733. gspca_dev->memory = GSPCA_MEMORY_NO;
  734. }
  735. file->private_data = NULL;
  736. mutex_unlock(&gspca_dev->queue_lock);
  737. PDEBUG(D_STREAM, "close done");
  738. return 0;
  739. }
  740. static int vidioc_querycap(struct file *file, void *priv,
  741. struct v4l2_capability *cap)
  742. {
  743. struct gspca_dev *gspca_dev = priv;
  744. memset(cap, 0, sizeof *cap);
  745. strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
  746. strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card);
  747. strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
  748. sizeof cap->bus_info);
  749. cap->version = DRIVER_VERSION_NUMBER;
  750. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  751. | V4L2_CAP_STREAMING
  752. | V4L2_CAP_READWRITE;
  753. return 0;
  754. }
  755. /* the use of V4L2_CTRL_FLAG_NEXT_CTRL asks for the controls to be sorted */
  756. static int vidioc_queryctrl(struct file *file, void *priv,
  757. struct v4l2_queryctrl *q_ctrl)
  758. {
  759. struct gspca_dev *gspca_dev = priv;
  760. int i;
  761. u32 id;
  762. id = q_ctrl->id;
  763. if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  764. id &= V4L2_CTRL_ID_MASK;
  765. id++;
  766. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  767. if (id >= gspca_dev->sd_desc->ctrls[i].qctrl.id) {
  768. memcpy(q_ctrl,
  769. &gspca_dev->sd_desc->ctrls[i].qctrl,
  770. sizeof *q_ctrl);
  771. return 0;
  772. }
  773. }
  774. return -EINVAL;
  775. }
  776. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  777. if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
  778. memcpy(q_ctrl,
  779. &gspca_dev->sd_desc->ctrls[i].qctrl,
  780. sizeof *q_ctrl);
  781. return 0;
  782. }
  783. }
  784. if (id >= V4L2_CID_BASE
  785. && id <= V4L2_CID_LASTP1) {
  786. q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  787. return 0;
  788. }
  789. return -EINVAL;
  790. }
  791. static int vidioc_s_ctrl(struct file *file, void *priv,
  792. struct v4l2_control *ctrl)
  793. {
  794. struct gspca_dev *gspca_dev = priv;
  795. const struct ctrl *ctrls;
  796. int i, ret;
  797. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  798. i < gspca_dev->sd_desc->nctrls;
  799. i++, ctrls++) {
  800. if (ctrl->id != ctrls->qctrl.id)
  801. continue;
  802. if (ctrl->value < ctrls->qctrl.minimum
  803. && ctrl->value > ctrls->qctrl.maximum)
  804. return -ERANGE;
  805. PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
  806. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  807. return -ERESTARTSYS;
  808. ret = ctrls->set(gspca_dev, ctrl->value);
  809. mutex_unlock(&gspca_dev->usb_lock);
  810. return ret;
  811. }
  812. return -EINVAL;
  813. }
  814. static int vidioc_g_ctrl(struct file *file, void *priv,
  815. struct v4l2_control *ctrl)
  816. {
  817. struct gspca_dev *gspca_dev = priv;
  818. const struct ctrl *ctrls;
  819. int i, ret;
  820. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  821. i < gspca_dev->sd_desc->nctrls;
  822. i++, ctrls++) {
  823. if (ctrl->id != ctrls->qctrl.id)
  824. continue;
  825. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  826. return -ERESTARTSYS;
  827. ret = ctrls->get(gspca_dev, &ctrl->value);
  828. mutex_unlock(&gspca_dev->usb_lock);
  829. return ret;
  830. }
  831. return -EINVAL;
  832. }
  833. static int vidioc_querymenu(struct file *file, void *priv,
  834. struct v4l2_querymenu *qmenu)
  835. {
  836. struct gspca_dev *gspca_dev = priv;
  837. if (!gspca_dev->sd_desc->querymenu)
  838. return -EINVAL;
  839. return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
  840. }
  841. static int vidioc_enum_input(struct file *file, void *priv,
  842. struct v4l2_input *input)
  843. {
  844. struct gspca_dev *gspca_dev = priv;
  845. if (input->index != 0)
  846. return -EINVAL;
  847. memset(input, 0, sizeof *input);
  848. input->type = V4L2_INPUT_TYPE_CAMERA;
  849. strncpy(input->name, gspca_dev->sd_desc->name,
  850. sizeof input->name);
  851. return 0;
  852. }
  853. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  854. {
  855. *i = 0;
  856. return 0;
  857. }
  858. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  859. {
  860. if (i > 0)
  861. return -EINVAL;
  862. return (0);
  863. }
  864. static int vidioc_reqbufs(struct file *file, void *priv,
  865. struct v4l2_requestbuffers *rb)
  866. {
  867. struct gspca_dev *gspca_dev = priv;
  868. int i, ret = 0;
  869. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  870. return -EINVAL;
  871. switch (rb->memory) {
  872. case GSPCA_MEMORY_READ: /* (internal call) */
  873. case V4L2_MEMORY_MMAP:
  874. case V4L2_MEMORY_USERPTR:
  875. break;
  876. default:
  877. return -EINVAL;
  878. }
  879. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  880. return -ERESTARTSYS;
  881. if (gspca_dev->memory != GSPCA_MEMORY_NO
  882. && gspca_dev->memory != rb->memory) {
  883. ret = -EBUSY;
  884. goto out;
  885. }
  886. /* only one file may do the capture */
  887. if (gspca_dev->capt_file != NULL
  888. && gspca_dev->capt_file != file) {
  889. ret = -EBUSY;
  890. goto out;
  891. }
  892. /* if allocated, the buffers must not be mapped */
  893. for (i = 0; i < gspca_dev->nframes; i++) {
  894. if (gspca_dev->frame[i].vma_use_count) {
  895. ret = -EBUSY;
  896. goto out;
  897. }
  898. }
  899. /* stop streaming */
  900. if (gspca_dev->streaming) {
  901. mutex_lock(&gspca_dev->usb_lock);
  902. gspca_stream_off(gspca_dev);
  903. mutex_unlock(&gspca_dev->usb_lock);
  904. }
  905. /* free the previous allocated buffers, if any */
  906. if (gspca_dev->nframes != 0) {
  907. frame_free(gspca_dev);
  908. gspca_dev->capt_file = NULL;
  909. }
  910. if (rb->count == 0) /* unrequest */
  911. goto out;
  912. gspca_dev->memory = rb->memory;
  913. ret = frame_alloc(gspca_dev, rb->count);
  914. if (ret == 0) {
  915. rb->count = gspca_dev->nframes;
  916. gspca_dev->capt_file = file;
  917. }
  918. out:
  919. mutex_unlock(&gspca_dev->queue_lock);
  920. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  921. return ret;
  922. }
  923. static int vidioc_querybuf(struct file *file, void *priv,
  924. struct v4l2_buffer *v4l2_buf)
  925. {
  926. struct gspca_dev *gspca_dev = priv;
  927. struct gspca_frame *frame;
  928. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  929. || v4l2_buf->index < 0
  930. || v4l2_buf->index >= gspca_dev->nframes)
  931. return -EINVAL;
  932. frame = &gspca_dev->frame[v4l2_buf->index];
  933. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  934. return 0;
  935. }
  936. static int vidioc_streamon(struct file *file, void *priv,
  937. enum v4l2_buf_type buf_type)
  938. {
  939. struct gspca_dev *gspca_dev = priv;
  940. int ret;
  941. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  942. return -EINVAL;
  943. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  944. return -ERESTARTSYS;
  945. if (!gspca_dev->present) {
  946. ret = -ENODEV;
  947. goto out;
  948. }
  949. if (gspca_dev->nframes == 0) {
  950. ret = -EINVAL;
  951. goto out;
  952. }
  953. if (!gspca_dev->streaming) {
  954. ret = gspca_init_transfer(gspca_dev);
  955. if (ret < 0)
  956. goto out;
  957. }
  958. #ifdef CONFIG_VIDEO_ADV_DEBUG
  959. if (gspca_debug & D_STREAM) {
  960. PDEBUG_MODE("stream on OK",
  961. gspca_dev->pixfmt,
  962. gspca_dev->width,
  963. gspca_dev->height);
  964. }
  965. #endif
  966. ret = 0;
  967. out:
  968. mutex_unlock(&gspca_dev->queue_lock);
  969. return ret;
  970. }
  971. static int vidioc_streamoff(struct file *file, void *priv,
  972. enum v4l2_buf_type buf_type)
  973. {
  974. struct gspca_dev *gspca_dev = priv;
  975. int i, ret;
  976. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  977. return -EINVAL;
  978. if (!gspca_dev->streaming)
  979. return 0;
  980. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  981. return -ERESTARTSYS;
  982. /* stop streaming */
  983. if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
  984. ret = -ERESTARTSYS;
  985. goto out;
  986. }
  987. gspca_stream_off(gspca_dev);
  988. mutex_unlock(&gspca_dev->usb_lock);
  989. /* empty the application queues */
  990. for (i = 0; i < gspca_dev->nframes; i++)
  991. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  992. gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
  993. gspca_dev->last_packet_type = DISCARD_PACKET;
  994. gspca_dev->sequence = 0;
  995. atomic_set(&gspca_dev->nevent, 0);
  996. ret = 0;
  997. out:
  998. mutex_unlock(&gspca_dev->queue_lock);
  999. return ret;
  1000. }
  1001. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1002. struct v4l2_jpegcompression *jpegcomp)
  1003. {
  1004. struct gspca_dev *gspca_dev = priv;
  1005. int ret;
  1006. if (!gspca_dev->sd_desc->get_jcomp)
  1007. return -EINVAL;
  1008. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1009. return -ERESTARTSYS;
  1010. ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1011. mutex_unlock(&gspca_dev->usb_lock);
  1012. return ret;
  1013. }
  1014. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1015. struct v4l2_jpegcompression *jpegcomp)
  1016. {
  1017. struct gspca_dev *gspca_dev = priv;
  1018. int ret;
  1019. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1020. return -ERESTARTSYS;
  1021. if (!gspca_dev->sd_desc->set_jcomp)
  1022. return -EINVAL;
  1023. ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1024. mutex_unlock(&gspca_dev->usb_lock);
  1025. return ret;
  1026. }
  1027. static int vidioc_g_parm(struct file *filp, void *priv,
  1028. struct v4l2_streamparm *parm)
  1029. {
  1030. struct gspca_dev *gspca_dev = priv;
  1031. memset(parm, 0, sizeof *parm);
  1032. parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1033. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1034. return 0;
  1035. }
  1036. static int vidioc_s_parm(struct file *filp, void *priv,
  1037. struct v4l2_streamparm *parm)
  1038. {
  1039. struct gspca_dev *gspca_dev = priv;
  1040. int n;
  1041. n = parm->parm.capture.readbuffers;
  1042. if (n == 0 || n > GSPCA_MAX_FRAMES)
  1043. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1044. else
  1045. gspca_dev->nbufread = n;
  1046. return 0;
  1047. }
  1048. static int vidioc_s_std(struct file *filp, void *priv,
  1049. v4l2_std_id *parm)
  1050. {
  1051. return 0;
  1052. }
  1053. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1054. static int vidiocgmbuf(struct file *file, void *priv,
  1055. struct video_mbuf *mbuf)
  1056. {
  1057. struct gspca_dev *gspca_dev = file->private_data;
  1058. int i;
  1059. PDEBUG(D_STREAM, "cgmbuf");
  1060. if (gspca_dev->nframes == 0) {
  1061. int ret;
  1062. {
  1063. struct v4l2_format fmt;
  1064. memset(&fmt, 0, sizeof fmt);
  1065. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1066. i = gspca_dev->cam.nmodes - 1; /* highest mode */
  1067. fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
  1068. fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
  1069. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
  1070. ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
  1071. if (ret != 0)
  1072. return ret;
  1073. }
  1074. {
  1075. struct v4l2_requestbuffers rb;
  1076. memset(&rb, 0, sizeof rb);
  1077. rb.count = 4;
  1078. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1079. rb.memory = V4L2_MEMORY_MMAP;
  1080. ret = vidioc_reqbufs(file, priv, &rb);
  1081. if (ret != 0)
  1082. return ret;
  1083. }
  1084. }
  1085. mbuf->frames = gspca_dev->nframes;
  1086. mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
  1087. for (i = 0; i < mbuf->frames; i++)
  1088. mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
  1089. return 0;
  1090. }
  1091. #endif
  1092. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1093. {
  1094. struct gspca_dev *gspca_dev = file->private_data;
  1095. struct gspca_frame *frame;
  1096. struct page *page;
  1097. unsigned long addr, start, size;
  1098. int i, ret;
  1099. start = vma->vm_start;
  1100. size = vma->vm_end - vma->vm_start;
  1101. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1102. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1103. return -ERESTARTSYS;
  1104. if (!gspca_dev->present) {
  1105. ret = -ENODEV;
  1106. goto out;
  1107. }
  1108. if (gspca_dev->capt_file != file) {
  1109. ret = -EINVAL;
  1110. goto out;
  1111. }
  1112. frame = NULL;
  1113. for (i = 0; i < gspca_dev->nframes; ++i) {
  1114. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1115. PDEBUG(D_STREAM, "mmap bad memory type");
  1116. break;
  1117. }
  1118. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1119. == vma->vm_pgoff) {
  1120. frame = &gspca_dev->frame[i];
  1121. break;
  1122. }
  1123. }
  1124. if (frame == NULL) {
  1125. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1126. ret = -EINVAL;
  1127. goto out;
  1128. }
  1129. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1130. /* v4l1 maps all the buffers */
  1131. if (i != 0
  1132. || size != frame->v4l2_buf.length * gspca_dev->nframes)
  1133. #endif
  1134. if (size != frame->v4l2_buf.length) {
  1135. PDEBUG(D_STREAM, "mmap bad size");
  1136. ret = -EINVAL;
  1137. goto out;
  1138. }
  1139. /*
  1140. * - VM_IO marks the area as being a mmaped region for I/O to a
  1141. * device. It also prevents the region from being core dumped.
  1142. */
  1143. vma->vm_flags |= VM_IO;
  1144. addr = (unsigned long) frame->data;
  1145. while (size > 0) {
  1146. page = vmalloc_to_page((void *) addr);
  1147. ret = vm_insert_page(vma, start, page);
  1148. if (ret < 0)
  1149. goto out;
  1150. start += PAGE_SIZE;
  1151. addr += PAGE_SIZE;
  1152. size -= PAGE_SIZE;
  1153. }
  1154. vma->vm_ops = &gspca_vm_ops;
  1155. vma->vm_private_data = frame;
  1156. gspca_vm_open(vma);
  1157. ret = 0;
  1158. out:
  1159. mutex_unlock(&gspca_dev->queue_lock);
  1160. return ret;
  1161. }
  1162. /*
  1163. * wait for a video frame
  1164. *
  1165. * If a frame is ready, its index is returned.
  1166. */
  1167. static int frame_wait(struct gspca_dev *gspca_dev,
  1168. int nonblock_ing)
  1169. {
  1170. struct gspca_frame *frame;
  1171. int i, j, ret;
  1172. /* check if a frame is ready */
  1173. i = gspca_dev->fr_o;
  1174. j = gspca_dev->fr_queue[i];
  1175. frame = &gspca_dev->frame[j];
  1176. if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
  1177. atomic_dec(&gspca_dev->nevent);
  1178. goto ok;
  1179. }
  1180. if (nonblock_ing) /* no frame yet */
  1181. return -EAGAIN;
  1182. /* wait till a frame is ready */
  1183. for (;;) {
  1184. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1185. atomic_read(&gspca_dev->nevent) > 0,
  1186. msecs_to_jiffies(3000));
  1187. if (ret <= 0) {
  1188. if (ret < 0)
  1189. return ret; /* interrupt */
  1190. return -EIO; /* timeout */
  1191. }
  1192. atomic_dec(&gspca_dev->nevent);
  1193. if (!gspca_dev->streaming || !gspca_dev->present)
  1194. return -EIO;
  1195. i = gspca_dev->fr_o;
  1196. j = gspca_dev->fr_queue[i];
  1197. frame = &gspca_dev->frame[j];
  1198. if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
  1199. break;
  1200. }
  1201. ok:
  1202. gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
  1203. PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
  1204. gspca_dev->fr_q,
  1205. gspca_dev->fr_i,
  1206. gspca_dev->fr_o);
  1207. if (gspca_dev->sd_desc->dq_callback) {
  1208. mutex_lock(&gspca_dev->usb_lock);
  1209. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1210. mutex_unlock(&gspca_dev->usb_lock);
  1211. }
  1212. return j;
  1213. }
  1214. /*
  1215. * dequeue a video buffer
  1216. *
  1217. * If nonblock_ing is false, block until a buffer is available.
  1218. */
  1219. static int vidioc_dqbuf(struct file *file, void *priv,
  1220. struct v4l2_buffer *v4l2_buf)
  1221. {
  1222. struct gspca_dev *gspca_dev = priv;
  1223. struct gspca_frame *frame;
  1224. int i, ret;
  1225. PDEBUG(D_FRAM, "dqbuf");
  1226. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1227. return -EINVAL;
  1228. if (v4l2_buf->memory != gspca_dev->memory)
  1229. return -EINVAL;
  1230. /* if not streaming, be sure the application will not loop forever */
  1231. if (!(file->f_flags & O_NONBLOCK)
  1232. && !gspca_dev->streaming && gspca_dev->users == 1)
  1233. return -EINVAL;
  1234. /* only the capturing file may dequeue */
  1235. if (gspca_dev->capt_file != file)
  1236. return -EINVAL;
  1237. /* only one dequeue / read at a time */
  1238. if (mutex_lock_interruptible(&gspca_dev->read_lock))
  1239. return -ERESTARTSYS;
  1240. ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
  1241. if (ret < 0)
  1242. goto out;
  1243. i = ret; /* frame index */
  1244. frame = &gspca_dev->frame[i];
  1245. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1246. if (copy_to_user((__u8 *) frame->v4l2_buf.m.userptr,
  1247. frame->data,
  1248. frame->v4l2_buf.bytesused)) {
  1249. PDEBUG(D_ERR|D_STREAM,
  1250. "dqbuf cp to user failed");
  1251. ret = -EFAULT;
  1252. goto out;
  1253. }
  1254. }
  1255. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1256. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1257. PDEBUG(D_FRAM, "dqbuf %d", i);
  1258. ret = 0;
  1259. out:
  1260. mutex_unlock(&gspca_dev->read_lock);
  1261. return ret;
  1262. }
  1263. /*
  1264. * queue a video buffer
  1265. *
  1266. * Attempting to queue a buffer that has already been
  1267. * queued will return -EINVAL.
  1268. */
  1269. static int vidioc_qbuf(struct file *file, void *priv,
  1270. struct v4l2_buffer *v4l2_buf)
  1271. {
  1272. struct gspca_dev *gspca_dev = priv;
  1273. struct gspca_frame *frame;
  1274. int i, index, ret;
  1275. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1276. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1277. return -EINVAL;
  1278. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1279. return -ERESTARTSYS;
  1280. index = v4l2_buf->index;
  1281. if ((unsigned) index >= gspca_dev->nframes) {
  1282. PDEBUG(D_FRAM,
  1283. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1284. ret = -EINVAL;
  1285. goto out;
  1286. }
  1287. if (v4l2_buf->memory != gspca_dev->memory) {
  1288. PDEBUG(D_FRAM, "qbuf bad memory type");
  1289. ret = -EINVAL;
  1290. goto out;
  1291. }
  1292. frame = &gspca_dev->frame[index];
  1293. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1294. PDEBUG(D_FRAM, "qbuf bad state");
  1295. ret = -EINVAL;
  1296. goto out;
  1297. }
  1298. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1299. /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
  1300. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1301. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1302. frame->v4l2_buf.length = v4l2_buf->length;
  1303. }
  1304. /* put the buffer in the 'queued' queue */
  1305. i = gspca_dev->fr_q;
  1306. gspca_dev->fr_queue[i] = index;
  1307. gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
  1308. PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
  1309. gspca_dev->fr_q,
  1310. gspca_dev->fr_i,
  1311. gspca_dev->fr_o);
  1312. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1313. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1314. ret = 0;
  1315. out:
  1316. mutex_unlock(&gspca_dev->queue_lock);
  1317. return ret;
  1318. }
  1319. /*
  1320. * allocate the resources for read()
  1321. */
  1322. static int read_alloc(struct gspca_dev *gspca_dev,
  1323. struct file *file)
  1324. {
  1325. struct v4l2_buffer v4l2_buf;
  1326. int i, ret;
  1327. PDEBUG(D_STREAM, "read alloc");
  1328. if (gspca_dev->nframes == 0) {
  1329. struct v4l2_requestbuffers rb;
  1330. memset(&rb, 0, sizeof rb);
  1331. rb.count = gspca_dev->nbufread;
  1332. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1333. rb.memory = GSPCA_MEMORY_READ;
  1334. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1335. if (ret != 0) {
  1336. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1337. return ret;
  1338. }
  1339. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1340. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1341. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1342. for (i = 0; i < gspca_dev->nbufread; i++) {
  1343. v4l2_buf.index = i;
  1344. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1345. if (ret != 0) {
  1346. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1347. return ret;
  1348. }
  1349. }
  1350. gspca_dev->memory = GSPCA_MEMORY_READ;
  1351. }
  1352. /* start streaming */
  1353. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1354. if (ret != 0)
  1355. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1356. return ret;
  1357. }
  1358. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1359. {
  1360. struct gspca_dev *gspca_dev = file->private_data;
  1361. int i, ret;
  1362. PDEBUG(D_FRAM, "poll");
  1363. poll_wait(file, &gspca_dev->wq, wait);
  1364. if (!gspca_dev->present)
  1365. return POLLERR;
  1366. /* if reqbufs is not done, the user would use read() */
  1367. if (gspca_dev->nframes == 0) {
  1368. if (gspca_dev->memory != GSPCA_MEMORY_NO)
  1369. return POLLERR; /* not the 1st time */
  1370. ret = read_alloc(gspca_dev, file);
  1371. if (ret != 0)
  1372. return POLLERR;
  1373. }
  1374. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
  1375. return POLLERR;
  1376. if (!gspca_dev->present) {
  1377. ret = POLLERR;
  1378. goto out;
  1379. }
  1380. /* check the next incoming buffer */
  1381. i = gspca_dev->fr_o;
  1382. i = gspca_dev->fr_queue[i];
  1383. if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
  1384. ret = POLLIN | POLLRDNORM; /* something to read */
  1385. else
  1386. ret = 0;
  1387. out:
  1388. mutex_unlock(&gspca_dev->queue_lock);
  1389. return ret;
  1390. }
  1391. static ssize_t dev_read(struct file *file, char __user *data,
  1392. size_t count, loff_t *ppos)
  1393. {
  1394. struct gspca_dev *gspca_dev = file->private_data;
  1395. struct gspca_frame *frame;
  1396. struct v4l2_buffer v4l2_buf;
  1397. struct timeval timestamp;
  1398. int n, ret, ret2;
  1399. PDEBUG(D_FRAM, "read (%zd)", count);
  1400. if (!gspca_dev->present)
  1401. return -ENODEV;
  1402. switch (gspca_dev->memory) {
  1403. case GSPCA_MEMORY_NO: /* first time */
  1404. ret = read_alloc(gspca_dev, file);
  1405. if (ret != 0)
  1406. return ret;
  1407. break;
  1408. case GSPCA_MEMORY_READ:
  1409. if (gspca_dev->capt_file == file)
  1410. break;
  1411. /* fall thru */
  1412. default:
  1413. return -EINVAL;
  1414. }
  1415. /* get a frame */
  1416. jiffies_to_timeval(get_jiffies_64(), &timestamp);
  1417. timestamp.tv_sec--;
  1418. n = 2;
  1419. for (;;) {
  1420. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1421. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1422. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1423. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1424. if (ret != 0) {
  1425. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1426. return ret;
  1427. }
  1428. /* if the process slept for more than 1 second,
  1429. * get anewer frame */
  1430. frame = &gspca_dev->frame[v4l2_buf.index];
  1431. if (--n < 0)
  1432. break; /* avoid infinite loop */
  1433. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1434. break;
  1435. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1436. if (ret != 0) {
  1437. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1438. return ret;
  1439. }
  1440. }
  1441. /* copy the frame */
  1442. if (count > frame->v4l2_buf.bytesused)
  1443. count = frame->v4l2_buf.bytesused;
  1444. ret = copy_to_user(data, frame->data, count);
  1445. if (ret != 0) {
  1446. PDEBUG(D_ERR|D_STREAM,
  1447. "read cp to user lack %d / %zd", ret, count);
  1448. ret = -EFAULT;
  1449. goto out;
  1450. }
  1451. ret = count;
  1452. out:
  1453. /* in each case, requeue the buffer */
  1454. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1455. if (ret2 != 0)
  1456. return ret2;
  1457. return ret;
  1458. }
  1459. static void dev_release(struct video_device *vfd)
  1460. {
  1461. /* nothing */
  1462. }
  1463. static struct file_operations dev_fops = {
  1464. .owner = THIS_MODULE,
  1465. .open = dev_open,
  1466. .release = dev_close,
  1467. .read = dev_read,
  1468. .mmap = dev_mmap,
  1469. .ioctl = video_ioctl2,
  1470. #ifdef CONFIG_COMPAT
  1471. .compat_ioctl = v4l_compat_ioctl32,
  1472. #endif
  1473. .llseek = no_llseek,
  1474. .poll = dev_poll,
  1475. };
  1476. static struct video_device gspca_template = {
  1477. .name = "gspca main driver",
  1478. .type = VID_TYPE_CAPTURE,
  1479. .fops = &dev_fops,
  1480. .release = dev_release, /* mandatory */
  1481. .minor = -1,
  1482. .vidioc_querycap = vidioc_querycap,
  1483. .vidioc_dqbuf = vidioc_dqbuf,
  1484. .vidioc_qbuf = vidioc_qbuf,
  1485. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1486. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1487. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1488. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1489. .vidioc_streamon = vidioc_streamon,
  1490. .vidioc_queryctrl = vidioc_queryctrl,
  1491. .vidioc_g_ctrl = vidioc_g_ctrl,
  1492. .vidioc_s_ctrl = vidioc_s_ctrl,
  1493. .vidioc_querymenu = vidioc_querymenu,
  1494. .vidioc_enum_input = vidioc_enum_input,
  1495. .vidioc_g_input = vidioc_g_input,
  1496. .vidioc_s_input = vidioc_s_input,
  1497. .vidioc_reqbufs = vidioc_reqbufs,
  1498. .vidioc_querybuf = vidioc_querybuf,
  1499. .vidioc_streamoff = vidioc_streamoff,
  1500. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1501. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1502. .vidioc_g_parm = vidioc_g_parm,
  1503. .vidioc_s_parm = vidioc_s_parm,
  1504. .vidioc_s_std = vidioc_s_std,
  1505. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1506. .vidiocgmbuf = vidiocgmbuf,
  1507. #endif
  1508. };
  1509. /*
  1510. * probe and create a new gspca device
  1511. *
  1512. * This function must be called by the sub-driver when it is
  1513. * called for probing a new device.
  1514. */
  1515. int gspca_dev_probe(struct usb_interface *intf,
  1516. const struct usb_device_id *id,
  1517. const struct sd_desc *sd_desc,
  1518. int dev_size,
  1519. struct module *module)
  1520. {
  1521. struct usb_interface_descriptor *interface;
  1522. struct gspca_dev *gspca_dev;
  1523. struct usb_device *dev = interface_to_usbdev(intf);
  1524. int ret;
  1525. PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
  1526. /* we don't handle multi-config cameras */
  1527. if (dev->descriptor.bNumConfigurations != 1)
  1528. return -ENODEV;
  1529. interface = &intf->cur_altsetting->desc;
  1530. if (interface->bInterfaceNumber > 0)
  1531. return -ENODEV;
  1532. /* create the device */
  1533. if (dev_size < sizeof *gspca_dev)
  1534. dev_size = sizeof *gspca_dev;
  1535. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1536. if (gspca_dev == NULL) {
  1537. err("couldn't kzalloc gspca struct");
  1538. return -EIO;
  1539. }
  1540. gspca_dev->dev = dev;
  1541. gspca_dev->iface = interface->bInterfaceNumber;
  1542. gspca_dev->nbalt = intf->num_altsetting;
  1543. gspca_dev->sd_desc = sd_desc;
  1544. /* gspca_dev->users = 0; (done by kzalloc) */
  1545. gspca_dev->nbufread = 2;
  1546. /* configure the subdriver */
  1547. ret = gspca_dev->sd_desc->config(gspca_dev, id);
  1548. if (ret < 0)
  1549. goto out;
  1550. ret = gspca_set_alt0(gspca_dev);
  1551. if (ret < 0)
  1552. goto out;
  1553. gspca_set_default_mode(gspca_dev);
  1554. mutex_init(&gspca_dev->usb_lock);
  1555. mutex_init(&gspca_dev->read_lock);
  1556. mutex_init(&gspca_dev->queue_lock);
  1557. init_waitqueue_head(&gspca_dev->wq);
  1558. /* init video stuff */
  1559. memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
  1560. gspca_dev->vdev.parent = &dev->dev;
  1561. memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
  1562. gspca_dev->vdev.fops = &gspca_dev->fops;
  1563. gspca_dev->fops.owner = module; /* module protection */
  1564. ret = video_register_device(&gspca_dev->vdev,
  1565. VFL_TYPE_GRABBER,
  1566. video_nr);
  1567. if (ret < 0) {
  1568. err("video_register_device err %d", ret);
  1569. goto out;
  1570. }
  1571. gspca_dev->present = 1;
  1572. usb_set_intfdata(intf, gspca_dev);
  1573. PDEBUG(D_PROBE, "probe ok");
  1574. return 0;
  1575. out:
  1576. kfree(gspca_dev);
  1577. return ret;
  1578. }
  1579. EXPORT_SYMBOL(gspca_dev_probe);
  1580. /*
  1581. * USB disconnection
  1582. *
  1583. * This function must be called by the sub-driver
  1584. * when the device disconnects, after the specific resources are freed.
  1585. */
  1586. void gspca_disconnect(struct usb_interface *intf)
  1587. {
  1588. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1589. if (!gspca_dev)
  1590. return;
  1591. gspca_dev->present = 0;
  1592. mutex_lock(&gspca_dev->queue_lock);
  1593. mutex_lock(&gspca_dev->usb_lock);
  1594. gspca_dev->streaming = 0;
  1595. destroy_urbs(gspca_dev);
  1596. mutex_unlock(&gspca_dev->usb_lock);
  1597. mutex_unlock(&gspca_dev->queue_lock);
  1598. while (gspca_dev->users != 0) { /* wait until fully closed */
  1599. atomic_inc(&gspca_dev->nevent);
  1600. wake_up_interruptible(&gspca_dev->wq); /* wake processes */
  1601. schedule();
  1602. }
  1603. /* We don't want people trying to open up the device */
  1604. video_unregister_device(&gspca_dev->vdev);
  1605. /* Free the memory */
  1606. kfree(gspca_dev);
  1607. PDEBUG(D_PROBE, "disconnect complete");
  1608. }
  1609. EXPORT_SYMBOL(gspca_disconnect);
  1610. /* -- cam driver utility functions -- */
  1611. /* auto gain and exposure algorithm based on the knee algorithm described here:
  1612. http://ytse.tricolour.net/docs/LowLightOptimization.html
  1613. Returns 0 if no changes were made, 1 if the gain and or exposure settings
  1614. where changed. */
  1615. int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
  1616. int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
  1617. {
  1618. int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
  1619. const struct ctrl *gain_ctrl = NULL;
  1620. const struct ctrl *exposure_ctrl = NULL;
  1621. const struct ctrl *autogain_ctrl = NULL;
  1622. int retval = 0;
  1623. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  1624. if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
  1625. gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
  1626. if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
  1627. exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
  1628. if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
  1629. autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
  1630. }
  1631. if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
  1632. PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
  1633. "on cam without (auto)gain/exposure");
  1634. return 0;
  1635. }
  1636. if (gain_ctrl->get(gspca_dev, &gain) ||
  1637. exposure_ctrl->get(gspca_dev, &exposure) ||
  1638. autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
  1639. return 0;
  1640. orig_gain = gain;
  1641. orig_exposure = exposure;
  1642. /* If we are of a multiple of deadzone, do multiple steps to reach the
  1643. desired lumination fast (with the risc of a slight overshoot) */
  1644. steps = abs(desired_avg_lum - avg_lum) / deadzone;
  1645. PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
  1646. avg_lum, desired_avg_lum, steps);
  1647. for (i = 0; i < steps; i++) {
  1648. if (avg_lum > desired_avg_lum) {
  1649. if (gain > gain_knee)
  1650. gain--;
  1651. else if (exposure > exposure_knee)
  1652. exposure--;
  1653. else if (gain > gain_ctrl->qctrl.default_value)
  1654. gain--;
  1655. else if (exposure > exposure_ctrl->qctrl.minimum)
  1656. exposure--;
  1657. else if (gain > gain_ctrl->qctrl.minimum)
  1658. gain--;
  1659. else
  1660. break;
  1661. } else {
  1662. if (gain < gain_ctrl->qctrl.default_value)
  1663. gain++;
  1664. else if (exposure < exposure_knee)
  1665. exposure++;
  1666. else if (gain < gain_knee)
  1667. gain++;
  1668. else if (exposure < exposure_ctrl->qctrl.maximum)
  1669. exposure++;
  1670. else if (gain < gain_ctrl->qctrl.maximum)
  1671. gain++;
  1672. else
  1673. break;
  1674. }
  1675. }
  1676. if (gain != orig_gain) {
  1677. gain_ctrl->set(gspca_dev, gain);
  1678. retval = 1;
  1679. }
  1680. if (exposure != orig_exposure) {
  1681. exposure_ctrl->set(gspca_dev, exposure);
  1682. retval = 1;
  1683. }
  1684. return retval;
  1685. }
  1686. EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
  1687. /* -- module insert / remove -- */
  1688. static int __init gspca_init(void)
  1689. {
  1690. info("main v%s registered", version);
  1691. return 0;
  1692. }
  1693. static void __exit gspca_exit(void)
  1694. {
  1695. info("main deregistered");
  1696. }
  1697. module_init(gspca_init);
  1698. module_exit(gspca_exit);
  1699. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1700. module_param_named(debug, gspca_debug, int, 0644);
  1701. MODULE_PARM_DESC(debug,
  1702. "Debug (bit) 0x01:error 0x02:probe 0x04:config"
  1703. " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
  1704. " 0x0100: v4l2");
  1705. #endif