gspca.c 51 KB

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