gspca.c 50 KB

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