gspca.c 48 KB

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