gspca.c 51 KB

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