gspca.c 48 KB

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