gspca.c 51 KB

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