gspca.c 50 KB

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