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