gspca.c 49 KB

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