gspca.c 46 KB

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