gspca.c 47 KB

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