gspca.c 51 KB

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