gspca.c 48 KB

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