gspca.c 50 KB

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