gspca.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  1. /*
  2. * Main USB camera driver
  3. *
  4. * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
  5. *
  6. * Camera button input handling by Márton Németh
  7. * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #define GSPCA_VERSION "2.14.0"
  25. #include <linux/init.h>
  26. #include <linux/fs.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/mm.h>
  31. #include <linux/string.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/io.h>
  34. #include <asm/page.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/ktime.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/v4l2-ctrls.h>
  39. #include <media/v4l2-fh.h>
  40. #include <media/v4l2-event.h>
  41. #include "gspca.h"
  42. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  43. #include <linux/input.h>
  44. #include <linux/usb/input.h>
  45. #endif
  46. /* global values */
  47. #define DEF_NURBS 3 /* default number of URBs */
  48. #if DEF_NURBS > MAX_NURBS
  49. #error "DEF_NURBS too big"
  50. #endif
  51. MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
  52. MODULE_DESCRIPTION("GSPCA USB Camera Driver");
  53. MODULE_LICENSE("GPL");
  54. MODULE_VERSION(GSPCA_VERSION);
  55. #ifdef GSPCA_DEBUG
  56. int gspca_debug = D_ERR | D_PROBE;
  57. EXPORT_SYMBOL(gspca_debug);
  58. static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
  59. {
  60. if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
  61. PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
  62. txt,
  63. pixfmt & 0xff,
  64. (pixfmt >> 8) & 0xff,
  65. (pixfmt >> 16) & 0xff,
  66. pixfmt >> 24,
  67. w, h);
  68. } else {
  69. PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
  70. txt,
  71. pixfmt,
  72. w, h);
  73. }
  74. }
  75. #else
  76. #define PDEBUG_MODE(txt, pixfmt, w, h)
  77. #endif
  78. /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
  79. #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
  80. #define GSPCA_MEMORY_READ 7
  81. #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
  82. /*
  83. * VMA operations.
  84. */
  85. static void gspca_vm_open(struct vm_area_struct *vma)
  86. {
  87. struct gspca_frame *frame = vma->vm_private_data;
  88. frame->vma_use_count++;
  89. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
  90. }
  91. static void gspca_vm_close(struct vm_area_struct *vma)
  92. {
  93. struct gspca_frame *frame = vma->vm_private_data;
  94. if (--frame->vma_use_count <= 0)
  95. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  96. }
  97. static const struct vm_operations_struct gspca_vm_ops = {
  98. .open = gspca_vm_open,
  99. .close = gspca_vm_close,
  100. };
  101. /*
  102. * Input and interrupt endpoint handling functions
  103. */
  104. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  105. static void int_irq(struct urb *urb)
  106. {
  107. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  108. int ret;
  109. ret = urb->status;
  110. switch (ret) {
  111. case 0:
  112. if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
  113. urb->transfer_buffer, urb->actual_length) < 0) {
  114. PDEBUG(D_ERR, "Unknown packet received");
  115. }
  116. break;
  117. case -ENOENT:
  118. case -ECONNRESET:
  119. case -ENODEV:
  120. case -ESHUTDOWN:
  121. /* Stop is requested either by software or hardware is gone,
  122. * keep the ret value non-zero and don't resubmit later.
  123. */
  124. break;
  125. default:
  126. PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
  127. urb->status = 0;
  128. ret = 0;
  129. }
  130. if (ret == 0) {
  131. ret = usb_submit_urb(urb, GFP_ATOMIC);
  132. if (ret < 0)
  133. pr_err("Resubmit URB failed with error %i\n", ret);
  134. }
  135. }
  136. static int gspca_input_connect(struct gspca_dev *dev)
  137. {
  138. struct input_dev *input_dev;
  139. int err = 0;
  140. dev->input_dev = NULL;
  141. if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
  142. input_dev = input_allocate_device();
  143. if (!input_dev)
  144. return -ENOMEM;
  145. usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
  146. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  147. input_dev->name = dev->sd_desc->name;
  148. input_dev->phys = dev->phys;
  149. usb_to_input_id(dev->dev, &input_dev->id);
  150. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  151. input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
  152. input_dev->dev.parent = &dev->dev->dev;
  153. err = input_register_device(input_dev);
  154. if (err) {
  155. pr_err("Input device registration failed with error %i\n",
  156. err);
  157. input_dev->dev.parent = NULL;
  158. input_free_device(input_dev);
  159. } else {
  160. dev->input_dev = input_dev;
  161. }
  162. }
  163. return err;
  164. }
  165. static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
  166. struct usb_endpoint_descriptor *ep)
  167. {
  168. unsigned int buffer_len;
  169. int interval;
  170. struct urb *urb;
  171. struct usb_device *dev;
  172. void *buffer = NULL;
  173. int ret = -EINVAL;
  174. buffer_len = le16_to_cpu(ep->wMaxPacketSize);
  175. interval = ep->bInterval;
  176. PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
  177. "buffer_len=%u, interval=%u",
  178. ep->bEndpointAddress, buffer_len, interval);
  179. dev = gspca_dev->dev;
  180. urb = usb_alloc_urb(0, GFP_KERNEL);
  181. if (!urb) {
  182. ret = -ENOMEM;
  183. goto error;
  184. }
  185. buffer = usb_alloc_coherent(dev, buffer_len,
  186. GFP_KERNEL, &urb->transfer_dma);
  187. if (!buffer) {
  188. ret = -ENOMEM;
  189. goto error_buffer;
  190. }
  191. usb_fill_int_urb(urb, dev,
  192. usb_rcvintpipe(dev, ep->bEndpointAddress),
  193. buffer, buffer_len,
  194. int_irq, (void *)gspca_dev, interval);
  195. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  196. ret = usb_submit_urb(urb, GFP_KERNEL);
  197. if (ret < 0) {
  198. PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
  199. goto error_submit;
  200. }
  201. gspca_dev->int_urb = urb;
  202. return ret;
  203. error_submit:
  204. usb_free_coherent(dev,
  205. urb->transfer_buffer_length,
  206. urb->transfer_buffer,
  207. urb->transfer_dma);
  208. error_buffer:
  209. usb_free_urb(urb);
  210. error:
  211. return ret;
  212. }
  213. static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
  214. {
  215. struct usb_interface *intf;
  216. struct usb_host_interface *intf_desc;
  217. struct usb_endpoint_descriptor *ep;
  218. int i;
  219. if (gspca_dev->sd_desc->int_pkt_scan) {
  220. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  221. intf_desc = intf->cur_altsetting;
  222. for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
  223. ep = &intf_desc->endpoint[i].desc;
  224. if (usb_endpoint_dir_in(ep) &&
  225. usb_endpoint_xfer_int(ep)) {
  226. alloc_and_submit_int_urb(gspca_dev, ep);
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
  233. {
  234. struct urb *urb;
  235. urb = gspca_dev->int_urb;
  236. if (urb) {
  237. gspca_dev->int_urb = NULL;
  238. usb_kill_urb(urb);
  239. usb_free_coherent(gspca_dev->dev,
  240. urb->transfer_buffer_length,
  241. urb->transfer_buffer,
  242. urb->transfer_dma);
  243. usb_free_urb(urb);
  244. }
  245. }
  246. #else
  247. static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
  248. {
  249. }
  250. static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
  251. {
  252. }
  253. static inline int gspca_input_connect(struct gspca_dev *dev)
  254. {
  255. return 0;
  256. }
  257. #endif
  258. /*
  259. * fill a video frame from an URB and resubmit
  260. */
  261. static void fill_frame(struct gspca_dev *gspca_dev,
  262. struct urb *urb)
  263. {
  264. u8 *data; /* address of data in the iso message */
  265. int i, len, st;
  266. cam_pkt_op pkt_scan;
  267. if (urb->status != 0) {
  268. if (urb->status == -ESHUTDOWN)
  269. return; /* disconnection */
  270. #ifdef CONFIG_PM
  271. if (gspca_dev->frozen)
  272. return;
  273. #endif
  274. PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
  275. urb->status = 0;
  276. goto resubmit;
  277. }
  278. pkt_scan = gspca_dev->sd_desc->pkt_scan;
  279. for (i = 0; i < urb->number_of_packets; i++) {
  280. len = urb->iso_frame_desc[i].actual_length;
  281. /* check the packet status and length */
  282. st = urb->iso_frame_desc[i].status;
  283. if (st) {
  284. pr_err("ISOC data error: [%d] len=%d, status=%d\n",
  285. i, len, st);
  286. gspca_dev->last_packet_type = DISCARD_PACKET;
  287. continue;
  288. }
  289. if (len == 0) {
  290. if (gspca_dev->empty_packet == 0)
  291. gspca_dev->empty_packet = 1;
  292. continue;
  293. }
  294. /* let the packet be analyzed by the subdriver */
  295. PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
  296. i, urb->iso_frame_desc[i].offset, len);
  297. data = (u8 *) urb->transfer_buffer
  298. + urb->iso_frame_desc[i].offset;
  299. pkt_scan(gspca_dev, data, len);
  300. }
  301. resubmit:
  302. /* resubmit the URB */
  303. st = usb_submit_urb(urb, GFP_ATOMIC);
  304. if (st < 0)
  305. pr_err("usb_submit_urb() ret %d\n", st);
  306. }
  307. /*
  308. * ISOC message interrupt from the USB device
  309. *
  310. * Analyse each packet and call the subdriver for copy to the frame buffer.
  311. */
  312. static void isoc_irq(struct urb *urb)
  313. {
  314. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  315. PDEBUG(D_PACK, "isoc irq");
  316. if (!gspca_dev->streaming)
  317. return;
  318. fill_frame(gspca_dev, urb);
  319. }
  320. /*
  321. * bulk message interrupt from the USB device
  322. */
  323. static void bulk_irq(struct urb *urb)
  324. {
  325. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  326. int st;
  327. PDEBUG(D_PACK, "bulk irq");
  328. if (!gspca_dev->streaming)
  329. return;
  330. switch (urb->status) {
  331. case 0:
  332. break;
  333. case -ESHUTDOWN:
  334. return; /* disconnection */
  335. default:
  336. #ifdef CONFIG_PM
  337. if (gspca_dev->frozen)
  338. return;
  339. #endif
  340. PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
  341. urb->status = 0;
  342. goto resubmit;
  343. }
  344. PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
  345. gspca_dev->sd_desc->pkt_scan(gspca_dev,
  346. urb->transfer_buffer,
  347. urb->actual_length);
  348. resubmit:
  349. /* resubmit the URB */
  350. if (gspca_dev->cam.bulk_nurbs != 0) {
  351. st = usb_submit_urb(urb, GFP_ATOMIC);
  352. if (st < 0)
  353. pr_err("usb_submit_urb() ret %d\n", st);
  354. }
  355. }
  356. /*
  357. * add data to the current frame
  358. *
  359. * This function is called by the subdrivers at interrupt level.
  360. *
  361. * To build a frame, these ones must add
  362. * - one FIRST_PACKET
  363. * - 0 or many INTER_PACKETs
  364. * - one LAST_PACKET
  365. * DISCARD_PACKET invalidates the whole frame.
  366. */
  367. void gspca_frame_add(struct gspca_dev *gspca_dev,
  368. enum gspca_packet_type packet_type,
  369. const u8 *data,
  370. int len)
  371. {
  372. struct gspca_frame *frame;
  373. int i, j;
  374. PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
  375. if (packet_type == FIRST_PACKET) {
  376. i = atomic_read(&gspca_dev->fr_i);
  377. /* if there are no queued buffer, discard the whole frame */
  378. if (i == atomic_read(&gspca_dev->fr_q)) {
  379. gspca_dev->last_packet_type = DISCARD_PACKET;
  380. gspca_dev->sequence++;
  381. return;
  382. }
  383. j = gspca_dev->fr_queue[i];
  384. frame = &gspca_dev->frame[j];
  385. frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
  386. frame->v4l2_buf.sequence = gspca_dev->sequence++;
  387. gspca_dev->image = frame->data;
  388. gspca_dev->image_len = 0;
  389. } else {
  390. switch (gspca_dev->last_packet_type) {
  391. case DISCARD_PACKET:
  392. if (packet_type == LAST_PACKET) {
  393. gspca_dev->last_packet_type = packet_type;
  394. gspca_dev->image = NULL;
  395. gspca_dev->image_len = 0;
  396. }
  397. return;
  398. case LAST_PACKET:
  399. return;
  400. }
  401. }
  402. /* append the packet to the frame buffer */
  403. if (len > 0) {
  404. if (gspca_dev->image_len + len > gspca_dev->frsz) {
  405. PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
  406. gspca_dev->image_len + len,
  407. gspca_dev->frsz);
  408. packet_type = DISCARD_PACKET;
  409. } else {
  410. /* !! image is NULL only when last pkt is LAST or DISCARD
  411. if (gspca_dev->image == NULL) {
  412. pr_err("gspca_frame_add() image == NULL\n");
  413. return;
  414. }
  415. */
  416. memcpy(gspca_dev->image + gspca_dev->image_len,
  417. data, len);
  418. gspca_dev->image_len += len;
  419. }
  420. }
  421. gspca_dev->last_packet_type = packet_type;
  422. /* if last packet, invalidate packet concatenation until
  423. * next first packet, wake up the application and advance
  424. * in the queue */
  425. if (packet_type == LAST_PACKET) {
  426. i = atomic_read(&gspca_dev->fr_i);
  427. j = gspca_dev->fr_queue[i];
  428. frame = &gspca_dev->frame[j];
  429. frame->v4l2_buf.bytesused = gspca_dev->image_len;
  430. frame->v4l2_buf.flags = (frame->v4l2_buf.flags
  431. | V4L2_BUF_FLAG_DONE)
  432. & ~V4L2_BUF_FLAG_QUEUED;
  433. i = (i + 1) % GSPCA_MAX_FRAMES;
  434. atomic_set(&gspca_dev->fr_i, i);
  435. wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
  436. PDEBUG(D_FRAM, "frame complete len:%d",
  437. frame->v4l2_buf.bytesused);
  438. gspca_dev->image = NULL;
  439. gspca_dev->image_len = 0;
  440. }
  441. }
  442. EXPORT_SYMBOL(gspca_frame_add);
  443. static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
  444. enum v4l2_memory memory, unsigned int count)
  445. {
  446. struct gspca_frame *frame;
  447. unsigned int frsz;
  448. int i;
  449. i = gspca_dev->curr_mode;
  450. frsz = gspca_dev->cam.cam_mode[i].sizeimage;
  451. PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
  452. frsz = PAGE_ALIGN(frsz);
  453. if (count >= GSPCA_MAX_FRAMES)
  454. count = GSPCA_MAX_FRAMES - 1;
  455. gspca_dev->frbuf = vmalloc_32(frsz * count);
  456. if (!gspca_dev->frbuf) {
  457. pr_err("frame alloc failed\n");
  458. return -ENOMEM;
  459. }
  460. gspca_dev->capt_file = file;
  461. gspca_dev->memory = memory;
  462. gspca_dev->frsz = frsz;
  463. gspca_dev->nframes = count;
  464. for (i = 0; i < count; i++) {
  465. frame = &gspca_dev->frame[i];
  466. frame->v4l2_buf.index = i;
  467. frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  468. frame->v4l2_buf.flags = 0;
  469. frame->v4l2_buf.field = V4L2_FIELD_NONE;
  470. frame->v4l2_buf.length = frsz;
  471. frame->v4l2_buf.memory = memory;
  472. frame->v4l2_buf.sequence = 0;
  473. frame->data = gspca_dev->frbuf + i * frsz;
  474. frame->v4l2_buf.m.offset = i * frsz;
  475. }
  476. atomic_set(&gspca_dev->fr_q, 0);
  477. atomic_set(&gspca_dev->fr_i, 0);
  478. gspca_dev->fr_o = 0;
  479. return 0;
  480. }
  481. static void frame_free(struct gspca_dev *gspca_dev)
  482. {
  483. int i;
  484. PDEBUG(D_STREAM, "frame free");
  485. if (gspca_dev->frbuf != NULL) {
  486. vfree(gspca_dev->frbuf);
  487. gspca_dev->frbuf = NULL;
  488. for (i = 0; i < gspca_dev->nframes; i++)
  489. gspca_dev->frame[i].data = NULL;
  490. }
  491. gspca_dev->nframes = 0;
  492. gspca_dev->frsz = 0;
  493. gspca_dev->capt_file = NULL;
  494. gspca_dev->memory = GSPCA_MEMORY_NO;
  495. }
  496. static void destroy_urbs(struct gspca_dev *gspca_dev)
  497. {
  498. struct urb *urb;
  499. unsigned int i;
  500. PDEBUG(D_STREAM, "kill transfer");
  501. for (i = 0; i < MAX_NURBS; i++) {
  502. urb = gspca_dev->urb[i];
  503. if (urb == NULL)
  504. break;
  505. gspca_dev->urb[i] = NULL;
  506. usb_kill_urb(urb);
  507. if (urb->transfer_buffer != NULL)
  508. usb_free_coherent(gspca_dev->dev,
  509. urb->transfer_buffer_length,
  510. urb->transfer_buffer,
  511. urb->transfer_dma);
  512. usb_free_urb(urb);
  513. }
  514. }
  515. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  516. {
  517. int ret;
  518. if (gspca_dev->alt == 0)
  519. return 0;
  520. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  521. if (ret < 0)
  522. pr_err("set alt 0 err %d\n", ret);
  523. return ret;
  524. }
  525. /* Note: both the queue and the usb locks should be held when calling this */
  526. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  527. {
  528. gspca_dev->streaming = 0;
  529. gspca_dev->usb_err = 0;
  530. if (gspca_dev->sd_desc->stopN)
  531. gspca_dev->sd_desc->stopN(gspca_dev);
  532. destroy_urbs(gspca_dev);
  533. gspca_input_destroy_urb(gspca_dev);
  534. gspca_set_alt0(gspca_dev);
  535. gspca_input_create_urb(gspca_dev);
  536. if (gspca_dev->sd_desc->stop0)
  537. gspca_dev->sd_desc->stop0(gspca_dev);
  538. PDEBUG(D_STREAM, "stream off OK");
  539. }
  540. /*
  541. * look for an input transfer endpoint in an alternate setting
  542. */
  543. static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
  544. int xfer)
  545. {
  546. struct usb_host_endpoint *ep;
  547. int i, attr;
  548. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  549. ep = &alt->endpoint[i];
  550. attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  551. if (attr == xfer
  552. && ep->desc.wMaxPacketSize != 0
  553. && usb_endpoint_dir_in(&ep->desc))
  554. return ep;
  555. }
  556. return NULL;
  557. }
  558. /* compute the minimum bandwidth for the current transfer */
  559. static u32 which_bandwidth(struct gspca_dev *gspca_dev)
  560. {
  561. u32 bandwidth;
  562. int i;
  563. /* get the (max) image size */
  564. i = gspca_dev->curr_mode;
  565. bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
  566. /* if the image is compressed, estimate its mean size */
  567. if (!gspca_dev->cam.needs_full_bandwidth &&
  568. bandwidth < gspca_dev->cam.cam_mode[i].width *
  569. gspca_dev->cam.cam_mode[i].height)
  570. bandwidth = bandwidth * 3 / 8; /* 0.375 */
  571. /* estimate the frame rate */
  572. if (gspca_dev->sd_desc->get_streamparm) {
  573. struct v4l2_streamparm parm;
  574. gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
  575. bandwidth *= parm.parm.capture.timeperframe.denominator;
  576. bandwidth /= parm.parm.capture.timeperframe.numerator;
  577. } else {
  578. /* don't hope more than 15 fps with USB 1.1 and
  579. * image resolution >= 640x480 */
  580. if (gspca_dev->width >= 640
  581. && gspca_dev->dev->speed == USB_SPEED_FULL)
  582. bandwidth *= 15; /* 15 fps */
  583. else
  584. bandwidth *= 30; /* 30 fps */
  585. }
  586. PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
  587. return bandwidth;
  588. }
  589. /* endpoint table */
  590. #define MAX_ALT 16
  591. struct ep_tb_s {
  592. u32 alt;
  593. u32 bandwidth;
  594. };
  595. /*
  596. * build the table of the endpoints
  597. * and compute the minimum bandwidth for the image transfer
  598. */
  599. static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
  600. struct usb_interface *intf,
  601. struct ep_tb_s *ep_tb)
  602. {
  603. struct usb_host_endpoint *ep;
  604. int i, j, nbalt, psize, found;
  605. u32 bandwidth, last_bw;
  606. nbalt = intf->num_altsetting;
  607. if (nbalt > MAX_ALT)
  608. nbalt = MAX_ALT; /* fixme: should warn */
  609. /* build the endpoint table */
  610. i = 0;
  611. last_bw = 0;
  612. for (;;) {
  613. ep_tb->bandwidth = 2000 * 2000 * 120;
  614. found = 0;
  615. for (j = 0; j < nbalt; j++) {
  616. ep = alt_xfer(&intf->altsetting[j],
  617. USB_ENDPOINT_XFER_ISOC);
  618. if (ep == NULL)
  619. continue;
  620. if (ep->desc.bInterval == 0) {
  621. pr_err("alt %d iso endp with 0 interval\n", j);
  622. continue;
  623. }
  624. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  625. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  626. bandwidth = psize * 1000;
  627. if (gspca_dev->dev->speed == USB_SPEED_HIGH
  628. || gspca_dev->dev->speed == USB_SPEED_SUPER)
  629. bandwidth *= 8;
  630. bandwidth /= 1 << (ep->desc.bInterval - 1);
  631. if (bandwidth <= last_bw)
  632. continue;
  633. if (bandwidth < ep_tb->bandwidth) {
  634. ep_tb->bandwidth = bandwidth;
  635. ep_tb->alt = j;
  636. found = 1;
  637. }
  638. }
  639. if (!found)
  640. break;
  641. PDEBUG(D_STREAM, "alt %d bandwidth %d",
  642. ep_tb->alt, ep_tb->bandwidth);
  643. last_bw = ep_tb->bandwidth;
  644. i++;
  645. ep_tb++;
  646. }
  647. /*
  648. * If the camera:
  649. * has a usb audio class interface (a built in usb mic); and
  650. * is a usb 1 full speed device; and
  651. * uses the max full speed iso bandwidth; and
  652. * and has more than 1 alt setting
  653. * then skip the highest alt setting to spare bandwidth for the mic
  654. */
  655. if (gspca_dev->audio &&
  656. gspca_dev->dev->speed == USB_SPEED_FULL &&
  657. last_bw >= 1000000 &&
  658. i > 1) {
  659. PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
  660. i--;
  661. ep_tb--;
  662. }
  663. /* get the requested bandwidth and start at the highest atlsetting */
  664. bandwidth = which_bandwidth(gspca_dev);
  665. ep_tb--;
  666. while (i > 1) {
  667. ep_tb--;
  668. if (ep_tb->bandwidth < bandwidth)
  669. break;
  670. i--;
  671. }
  672. return i;
  673. }
  674. /*
  675. * create the URBs for image transfer
  676. */
  677. static int create_urbs(struct gspca_dev *gspca_dev,
  678. struct usb_host_endpoint *ep)
  679. {
  680. struct urb *urb;
  681. int n, nurbs, i, psize, npkt, bsize;
  682. /* calculate the packet size and the number of packets */
  683. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  684. if (!gspca_dev->cam.bulk) { /* isoc */
  685. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  686. if (gspca_dev->pkt_size == 0)
  687. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  688. else
  689. psize = gspca_dev->pkt_size;
  690. npkt = gspca_dev->cam.npkt;
  691. if (npkt == 0)
  692. npkt = 32; /* default value */
  693. bsize = psize * npkt;
  694. PDEBUG(D_STREAM,
  695. "isoc %d pkts size %d = bsize:%d",
  696. npkt, psize, bsize);
  697. nurbs = DEF_NURBS;
  698. } else { /* bulk */
  699. npkt = 0;
  700. bsize = gspca_dev->cam.bulk_size;
  701. if (bsize == 0)
  702. bsize = psize;
  703. PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
  704. if (gspca_dev->cam.bulk_nurbs != 0)
  705. nurbs = gspca_dev->cam.bulk_nurbs;
  706. else
  707. nurbs = 1;
  708. }
  709. for (n = 0; n < nurbs; n++) {
  710. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  711. if (!urb) {
  712. pr_err("usb_alloc_urb failed\n");
  713. return -ENOMEM;
  714. }
  715. gspca_dev->urb[n] = urb;
  716. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  717. bsize,
  718. GFP_KERNEL,
  719. &urb->transfer_dma);
  720. if (urb->transfer_buffer == NULL) {
  721. pr_err("usb_alloc_coherent failed\n");
  722. return -ENOMEM;
  723. }
  724. urb->dev = gspca_dev->dev;
  725. urb->context = gspca_dev;
  726. urb->transfer_buffer_length = bsize;
  727. if (npkt != 0) { /* ISOC */
  728. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  729. ep->desc.bEndpointAddress);
  730. urb->transfer_flags = URB_ISO_ASAP
  731. | URB_NO_TRANSFER_DMA_MAP;
  732. urb->interval = 1 << (ep->desc.bInterval - 1);
  733. urb->complete = isoc_irq;
  734. urb->number_of_packets = npkt;
  735. for (i = 0; i < npkt; i++) {
  736. urb->iso_frame_desc[i].length = psize;
  737. urb->iso_frame_desc[i].offset = psize * i;
  738. }
  739. } else { /* bulk */
  740. urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
  741. ep->desc.bEndpointAddress);
  742. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  743. urb->complete = bulk_irq;
  744. }
  745. }
  746. return 0;
  747. }
  748. /*
  749. * start the USB transfer
  750. */
  751. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  752. {
  753. struct usb_interface *intf;
  754. struct usb_host_endpoint *ep;
  755. struct urb *urb;
  756. struct ep_tb_s ep_tb[MAX_ALT];
  757. int n, ret, xfer, alt, alt_idx;
  758. /* reset the streaming variables */
  759. gspca_dev->image = NULL;
  760. gspca_dev->image_len = 0;
  761. gspca_dev->last_packet_type = DISCARD_PACKET;
  762. gspca_dev->sequence = 0;
  763. gspca_dev->usb_err = 0;
  764. /* do the specific subdriver stuff before endpoint selection */
  765. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  766. gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
  767. if (gspca_dev->sd_desc->isoc_init) {
  768. ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
  769. if (ret < 0)
  770. return ret;
  771. }
  772. xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
  773. : USB_ENDPOINT_XFER_ISOC;
  774. /* if bulk or the subdriver forced an altsetting, get the endpoint */
  775. if (gspca_dev->alt != 0) {
  776. gspca_dev->alt--; /* (previous version compatibility) */
  777. ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
  778. if (ep == NULL) {
  779. pr_err("bad altsetting %d\n", gspca_dev->alt);
  780. return -EIO;
  781. }
  782. ep_tb[0].alt = gspca_dev->alt;
  783. alt_idx = 1;
  784. } else {
  785. /* else, compute the minimum bandwidth
  786. * and build the endpoint table */
  787. alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
  788. if (alt_idx <= 0) {
  789. pr_err("no transfer endpoint found\n");
  790. return -EIO;
  791. }
  792. }
  793. /* set the highest alternate setting and
  794. * loop until urb submit succeeds */
  795. gspca_input_destroy_urb(gspca_dev);
  796. gspca_dev->alt = ep_tb[--alt_idx].alt;
  797. alt = -1;
  798. for (;;) {
  799. if (alt != gspca_dev->alt) {
  800. alt = gspca_dev->alt;
  801. if (intf->num_altsetting > 1) {
  802. ret = usb_set_interface(gspca_dev->dev,
  803. gspca_dev->iface,
  804. alt);
  805. if (ret < 0) {
  806. if (ret == -ENOSPC)
  807. goto retry; /*fixme: ugly*/
  808. pr_err("set alt %d err %d\n", alt, ret);
  809. goto out;
  810. }
  811. }
  812. }
  813. if (!gspca_dev->cam.no_urb_create) {
  814. PDEBUG(D_STREAM, "init transfer alt %d", alt);
  815. ret = create_urbs(gspca_dev,
  816. alt_xfer(&intf->altsetting[alt], xfer));
  817. if (ret < 0) {
  818. destroy_urbs(gspca_dev);
  819. goto out;
  820. }
  821. }
  822. /* clear the bulk endpoint */
  823. if (gspca_dev->cam.bulk)
  824. usb_clear_halt(gspca_dev->dev,
  825. gspca_dev->urb[0]->pipe);
  826. /* start the cam */
  827. ret = gspca_dev->sd_desc->start(gspca_dev);
  828. if (ret < 0) {
  829. destroy_urbs(gspca_dev);
  830. goto out;
  831. }
  832. gspca_dev->streaming = 1;
  833. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  834. /* some bulk transfers are started by the subdriver */
  835. if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
  836. break;
  837. /* submit the URBs */
  838. for (n = 0; n < MAX_NURBS; n++) {
  839. urb = gspca_dev->urb[n];
  840. if (urb == NULL)
  841. break;
  842. ret = usb_submit_urb(urb, GFP_KERNEL);
  843. if (ret < 0)
  844. break;
  845. }
  846. if (ret >= 0)
  847. break; /* transfer is started */
  848. /* something when wrong
  849. * stop the webcam and free the transfer resources */
  850. gspca_stream_off(gspca_dev);
  851. if (ret != -ENOSPC) {
  852. pr_err("usb_submit_urb alt %d err %d\n",
  853. gspca_dev->alt, ret);
  854. goto out;
  855. }
  856. /* the bandwidth is not wide enough
  857. * negotiate or try a lower alternate setting */
  858. retry:
  859. PDEBUG(D_ERR|D_STREAM,
  860. "alt %d - bandwidth not wide enough - trying again",
  861. alt);
  862. msleep(20); /* wait for kill complete */
  863. if (gspca_dev->sd_desc->isoc_nego) {
  864. ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
  865. if (ret < 0)
  866. goto out;
  867. } else {
  868. if (alt_idx <= 0) {
  869. pr_err("no transfer endpoint found\n");
  870. ret = -EIO;
  871. goto out;
  872. }
  873. gspca_dev->alt = ep_tb[--alt_idx].alt;
  874. }
  875. }
  876. out:
  877. gspca_input_create_urb(gspca_dev);
  878. return ret;
  879. }
  880. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  881. {
  882. struct gspca_ctrl *ctrl;
  883. int i;
  884. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  885. gspca_dev->curr_mode = i;
  886. gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
  887. gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
  888. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
  889. /* set the current control values to their default values
  890. * which may have changed in sd_init() */
  891. /* does nothing if ctrl_handler == NULL */
  892. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  893. ctrl = gspca_dev->cam.ctrls;
  894. if (ctrl != NULL) {
  895. for (i = 0;
  896. i < gspca_dev->sd_desc->nctrls;
  897. i++, ctrl++)
  898. ctrl->val = ctrl->def;
  899. }
  900. }
  901. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  902. int width, int height)
  903. {
  904. int i;
  905. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  906. if (width >= gspca_dev->cam.cam_mode[i].width
  907. && height >= gspca_dev->cam.cam_mode[i].height)
  908. break;
  909. }
  910. return i;
  911. }
  912. /*
  913. * search a mode with the right pixel format
  914. */
  915. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  916. int mode,
  917. int pixfmt)
  918. {
  919. int modeU, modeD;
  920. modeU = modeD = mode;
  921. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  922. if (--modeD >= 0) {
  923. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  924. == pixfmt)
  925. return modeD;
  926. }
  927. if (++modeU < gspca_dev->cam.nmodes) {
  928. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  929. == pixfmt)
  930. return modeU;
  931. }
  932. }
  933. return -EINVAL;
  934. }
  935. #ifdef CONFIG_VIDEO_ADV_DEBUG
  936. static int vidioc_g_register(struct file *file, void *priv,
  937. struct v4l2_dbg_register *reg)
  938. {
  939. struct gspca_dev *gspca_dev = video_drvdata(file);
  940. gspca_dev->usb_err = 0;
  941. return gspca_dev->sd_desc->get_register(gspca_dev, reg);
  942. }
  943. static int vidioc_s_register(struct file *file, void *priv,
  944. struct v4l2_dbg_register *reg)
  945. {
  946. struct gspca_dev *gspca_dev = video_drvdata(file);
  947. gspca_dev->usb_err = 0;
  948. return gspca_dev->sd_desc->set_register(gspca_dev, reg);
  949. }
  950. #endif
  951. static int vidioc_g_chip_ident(struct file *file, void *priv,
  952. struct v4l2_dbg_chip_ident *chip)
  953. {
  954. struct gspca_dev *gspca_dev = video_drvdata(file);
  955. gspca_dev->usb_err = 0;
  956. return gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
  957. }
  958. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  959. struct v4l2_fmtdesc *fmtdesc)
  960. {
  961. struct gspca_dev *gspca_dev = video_drvdata(file);
  962. int i, j, index;
  963. __u32 fmt_tb[8];
  964. /* give an index to each format */
  965. index = 0;
  966. j = 0;
  967. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  968. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  969. j = 0;
  970. for (;;) {
  971. if (fmt_tb[j] == fmt_tb[index])
  972. break;
  973. j++;
  974. }
  975. if (j == index) {
  976. if (fmtdesc->index == index)
  977. break; /* new format */
  978. index++;
  979. if (index >= ARRAY_SIZE(fmt_tb))
  980. return -EINVAL;
  981. }
  982. }
  983. if (i < 0)
  984. return -EINVAL; /* no more format */
  985. fmtdesc->pixelformat = fmt_tb[index];
  986. if (gspca_dev->cam.cam_mode[i].sizeimage <
  987. gspca_dev->cam.cam_mode[i].width *
  988. gspca_dev->cam.cam_mode[i].height)
  989. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  990. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  991. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  992. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  993. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  994. fmtdesc->description[4] = '\0';
  995. return 0;
  996. }
  997. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  998. struct v4l2_format *fmt)
  999. {
  1000. struct gspca_dev *gspca_dev = video_drvdata(file);
  1001. int mode;
  1002. mode = gspca_dev->curr_mode;
  1003. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  1004. /* some drivers use priv internally, zero it before giving it to
  1005. userspace */
  1006. fmt->fmt.pix.priv = 0;
  1007. return 0;
  1008. }
  1009. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  1010. struct v4l2_format *fmt)
  1011. {
  1012. int w, h, mode, mode2;
  1013. w = fmt->fmt.pix.width;
  1014. h = fmt->fmt.pix.height;
  1015. #ifdef GSPCA_DEBUG
  1016. if (gspca_debug & D_CONF)
  1017. PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
  1018. #endif
  1019. /* search the closest mode for width and height */
  1020. mode = wxh_to_mode(gspca_dev, w, h);
  1021. /* OK if right palette */
  1022. if (gspca_dev->cam.cam_mode[mode].pixelformat
  1023. != fmt->fmt.pix.pixelformat) {
  1024. /* else, search the closest mode with the same pixel format */
  1025. mode2 = gspca_get_mode(gspca_dev, mode,
  1026. fmt->fmt.pix.pixelformat);
  1027. if (mode2 >= 0)
  1028. mode = mode2;
  1029. /* else
  1030. ; * no chance, return this mode */
  1031. }
  1032. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  1033. /* some drivers use priv internally, zero it before giving it to
  1034. userspace */
  1035. fmt->fmt.pix.priv = 0;
  1036. return mode; /* used when s_fmt */
  1037. }
  1038. static int vidioc_try_fmt_vid_cap(struct file *file,
  1039. void *priv,
  1040. struct v4l2_format *fmt)
  1041. {
  1042. struct gspca_dev *gspca_dev = video_drvdata(file);
  1043. int ret;
  1044. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1045. if (ret < 0)
  1046. return ret;
  1047. return 0;
  1048. }
  1049. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1050. struct v4l2_format *fmt)
  1051. {
  1052. struct gspca_dev *gspca_dev = video_drvdata(file);
  1053. int ret;
  1054. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1055. return -ERESTARTSYS;
  1056. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1057. if (ret < 0)
  1058. goto out;
  1059. if (gspca_dev->nframes != 0
  1060. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  1061. ret = -EINVAL;
  1062. goto out;
  1063. }
  1064. if (ret == gspca_dev->curr_mode) {
  1065. ret = 0;
  1066. goto out; /* same mode */
  1067. }
  1068. if (gspca_dev->streaming) {
  1069. ret = -EBUSY;
  1070. goto out;
  1071. }
  1072. gspca_dev->width = fmt->fmt.pix.width;
  1073. gspca_dev->height = fmt->fmt.pix.height;
  1074. gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
  1075. gspca_dev->curr_mode = ret;
  1076. ret = 0;
  1077. out:
  1078. mutex_unlock(&gspca_dev->queue_lock);
  1079. return ret;
  1080. }
  1081. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1082. struct v4l2_frmsizeenum *fsize)
  1083. {
  1084. struct gspca_dev *gspca_dev = video_drvdata(file);
  1085. int i;
  1086. __u32 index = 0;
  1087. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  1088. if (fsize->pixel_format !=
  1089. gspca_dev->cam.cam_mode[i].pixelformat)
  1090. continue;
  1091. if (fsize->index == index) {
  1092. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1093. fsize->discrete.width =
  1094. gspca_dev->cam.cam_mode[i].width;
  1095. fsize->discrete.height =
  1096. gspca_dev->cam.cam_mode[i].height;
  1097. return 0;
  1098. }
  1099. index++;
  1100. }
  1101. return -EINVAL;
  1102. }
  1103. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  1104. struct v4l2_frmivalenum *fival)
  1105. {
  1106. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1107. int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
  1108. __u32 i;
  1109. if (gspca_dev->cam.mode_framerates == NULL ||
  1110. gspca_dev->cam.mode_framerates[mode].nrates == 0)
  1111. return -EINVAL;
  1112. if (fival->pixel_format !=
  1113. gspca_dev->cam.cam_mode[mode].pixelformat)
  1114. return -EINVAL;
  1115. for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
  1116. if (fival->index == i) {
  1117. fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1118. fival->discrete.numerator = 1;
  1119. fival->discrete.denominator =
  1120. gspca_dev->cam.mode_framerates[mode].rates[i];
  1121. return 0;
  1122. }
  1123. }
  1124. return -EINVAL;
  1125. }
  1126. static void gspca_release(struct v4l2_device *v4l2_device)
  1127. {
  1128. struct gspca_dev *gspca_dev =
  1129. container_of(v4l2_device, struct gspca_dev, v4l2_dev);
  1130. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1131. v4l2_device_unregister(&gspca_dev->v4l2_dev);
  1132. kfree(gspca_dev->usb_buf);
  1133. kfree(gspca_dev);
  1134. }
  1135. static int dev_open(struct file *file)
  1136. {
  1137. struct gspca_dev *gspca_dev = video_drvdata(file);
  1138. PDEBUG(D_STREAM, "[%s] open", current->comm);
  1139. /* protect the subdriver against rmmod */
  1140. if (!try_module_get(gspca_dev->module))
  1141. return -ENODEV;
  1142. #ifdef GSPCA_DEBUG
  1143. /* activate the v4l2 debug */
  1144. if (gspca_debug & D_V4L2)
  1145. gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
  1146. | V4L2_DEBUG_IOCTL_ARG;
  1147. else
  1148. gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
  1149. | V4L2_DEBUG_IOCTL_ARG);
  1150. #endif
  1151. return v4l2_fh_open(file);
  1152. }
  1153. static int dev_close(struct file *file)
  1154. {
  1155. struct gspca_dev *gspca_dev = video_drvdata(file);
  1156. PDEBUG(D_STREAM, "[%s] close", current->comm);
  1157. /* Needed for gspca_stream_off, always lock before queue_lock! */
  1158. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1159. return -ERESTARTSYS;
  1160. if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
  1161. mutex_unlock(&gspca_dev->usb_lock);
  1162. return -ERESTARTSYS;
  1163. }
  1164. /* if the file did the capture, free the streaming resources */
  1165. if (gspca_dev->capt_file == file) {
  1166. if (gspca_dev->streaming)
  1167. gspca_stream_off(gspca_dev);
  1168. frame_free(gspca_dev);
  1169. }
  1170. module_put(gspca_dev->module);
  1171. mutex_unlock(&gspca_dev->queue_lock);
  1172. mutex_unlock(&gspca_dev->usb_lock);
  1173. PDEBUG(D_STREAM, "close done");
  1174. return v4l2_fh_release(file);
  1175. }
  1176. static int vidioc_querycap(struct file *file, void *priv,
  1177. struct v4l2_capability *cap)
  1178. {
  1179. struct gspca_dev *gspca_dev = video_drvdata(file);
  1180. strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
  1181. sizeof cap->driver);
  1182. if (gspca_dev->dev->product != NULL) {
  1183. strlcpy((char *) cap->card, gspca_dev->dev->product,
  1184. sizeof cap->card);
  1185. } else {
  1186. snprintf((char *) cap->card, sizeof cap->card,
  1187. "USB Camera (%04x:%04x)",
  1188. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  1189. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  1190. }
  1191. usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
  1192. sizeof(cap->bus_info));
  1193. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  1194. | V4L2_CAP_STREAMING
  1195. | V4L2_CAP_READWRITE;
  1196. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1197. return 0;
  1198. }
  1199. static int get_ctrl(struct gspca_dev *gspca_dev,
  1200. int id)
  1201. {
  1202. const struct ctrl *ctrls;
  1203. int i;
  1204. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  1205. i < gspca_dev->sd_desc->nctrls;
  1206. i++, ctrls++) {
  1207. if (gspca_dev->ctrl_dis & (1 << i))
  1208. continue;
  1209. if (id == ctrls->qctrl.id)
  1210. return i;
  1211. }
  1212. return -1;
  1213. }
  1214. static int vidioc_queryctrl(struct file *file, void *priv,
  1215. struct v4l2_queryctrl *q_ctrl)
  1216. {
  1217. struct gspca_dev *gspca_dev = video_drvdata(file);
  1218. const struct ctrl *ctrls;
  1219. struct gspca_ctrl *gspca_ctrl;
  1220. int i, idx;
  1221. u32 id;
  1222. id = q_ctrl->id;
  1223. if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  1224. id &= V4L2_CTRL_ID_MASK;
  1225. id++;
  1226. idx = -1;
  1227. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  1228. if (gspca_dev->ctrl_dis & (1 << i))
  1229. continue;
  1230. if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
  1231. continue;
  1232. if (idx >= 0
  1233. && gspca_dev->sd_desc->ctrls[i].qctrl.id
  1234. > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
  1235. continue;
  1236. idx = i;
  1237. }
  1238. } else {
  1239. idx = get_ctrl(gspca_dev, id);
  1240. }
  1241. if (idx < 0)
  1242. return -EINVAL;
  1243. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1244. memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
  1245. if (gspca_dev->cam.ctrls != NULL) {
  1246. gspca_ctrl = &gspca_dev->cam.ctrls[idx];
  1247. q_ctrl->default_value = gspca_ctrl->def;
  1248. q_ctrl->minimum = gspca_ctrl->min;
  1249. q_ctrl->maximum = gspca_ctrl->max;
  1250. }
  1251. if (gspca_dev->ctrl_inac & (1 << idx))
  1252. q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
  1253. return 0;
  1254. }
  1255. static int vidioc_s_ctrl(struct file *file, void *priv,
  1256. struct v4l2_control *ctrl)
  1257. {
  1258. struct gspca_dev *gspca_dev = video_drvdata(file);
  1259. const struct ctrl *ctrls;
  1260. struct gspca_ctrl *gspca_ctrl;
  1261. int idx;
  1262. idx = get_ctrl(gspca_dev, ctrl->id);
  1263. if (idx < 0)
  1264. return -EINVAL;
  1265. if (gspca_dev->ctrl_inac & (1 << idx))
  1266. return -EINVAL;
  1267. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1268. if (gspca_dev->cam.ctrls != NULL) {
  1269. gspca_ctrl = &gspca_dev->cam.ctrls[idx];
  1270. if (ctrl->value < gspca_ctrl->min
  1271. || ctrl->value > gspca_ctrl->max)
  1272. return -ERANGE;
  1273. } else {
  1274. gspca_ctrl = NULL;
  1275. if (ctrl->value < ctrls->qctrl.minimum
  1276. || ctrl->value > ctrls->qctrl.maximum)
  1277. return -ERANGE;
  1278. }
  1279. PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
  1280. gspca_dev->usb_err = 0;
  1281. if (ctrls->set != NULL)
  1282. return ctrls->set(gspca_dev, ctrl->value);
  1283. if (gspca_ctrl != NULL) {
  1284. gspca_ctrl->val = ctrl->value;
  1285. if (ctrls->set_control != NULL
  1286. && gspca_dev->streaming)
  1287. ctrls->set_control(gspca_dev);
  1288. }
  1289. return gspca_dev->usb_err;
  1290. }
  1291. static int vidioc_g_ctrl(struct file *file, void *priv,
  1292. struct v4l2_control *ctrl)
  1293. {
  1294. struct gspca_dev *gspca_dev = video_drvdata(file);
  1295. const struct ctrl *ctrls;
  1296. int idx;
  1297. idx = get_ctrl(gspca_dev, ctrl->id);
  1298. if (idx < 0)
  1299. return -EINVAL;
  1300. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1301. gspca_dev->usb_err = 0;
  1302. if (ctrls->get != NULL)
  1303. return ctrls->get(gspca_dev, &ctrl->value);
  1304. if (gspca_dev->cam.ctrls != NULL)
  1305. ctrl->value = gspca_dev->cam.ctrls[idx].val;
  1306. return 0;
  1307. }
  1308. static int vidioc_querymenu(struct file *file, void *priv,
  1309. struct v4l2_querymenu *qmenu)
  1310. {
  1311. struct gspca_dev *gspca_dev = video_drvdata(file);
  1312. if (!gspca_dev->sd_desc->querymenu)
  1313. return -ENOTTY;
  1314. return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
  1315. }
  1316. static int vidioc_enum_input(struct file *file, void *priv,
  1317. struct v4l2_input *input)
  1318. {
  1319. struct gspca_dev *gspca_dev = video_drvdata(file);
  1320. if (input->index != 0)
  1321. return -EINVAL;
  1322. input->type = V4L2_INPUT_TYPE_CAMERA;
  1323. input->status = gspca_dev->cam.input_flags;
  1324. strlcpy(input->name, gspca_dev->sd_desc->name,
  1325. sizeof input->name);
  1326. return 0;
  1327. }
  1328. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1329. {
  1330. *i = 0;
  1331. return 0;
  1332. }
  1333. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1334. {
  1335. if (i > 0)
  1336. return -EINVAL;
  1337. return (0);
  1338. }
  1339. static int vidioc_reqbufs(struct file *file, void *priv,
  1340. struct v4l2_requestbuffers *rb)
  1341. {
  1342. struct gspca_dev *gspca_dev = video_drvdata(file);
  1343. int i, ret = 0, streaming;
  1344. i = rb->memory; /* (avoid compilation warning) */
  1345. switch (i) {
  1346. case GSPCA_MEMORY_READ: /* (internal call) */
  1347. case V4L2_MEMORY_MMAP:
  1348. case V4L2_MEMORY_USERPTR:
  1349. break;
  1350. default:
  1351. return -EINVAL;
  1352. }
  1353. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1354. return -ERESTARTSYS;
  1355. if (gspca_dev->memory != GSPCA_MEMORY_NO
  1356. && gspca_dev->memory != GSPCA_MEMORY_READ
  1357. && gspca_dev->memory != rb->memory) {
  1358. ret = -EBUSY;
  1359. goto out;
  1360. }
  1361. /* only one file may do the capture */
  1362. if (gspca_dev->capt_file != NULL
  1363. && gspca_dev->capt_file != file) {
  1364. ret = -EBUSY;
  1365. goto out;
  1366. }
  1367. /* if allocated, the buffers must not be mapped */
  1368. for (i = 0; i < gspca_dev->nframes; i++) {
  1369. if (gspca_dev->frame[i].vma_use_count) {
  1370. ret = -EBUSY;
  1371. goto out;
  1372. }
  1373. }
  1374. /* stop streaming */
  1375. streaming = gspca_dev->streaming;
  1376. if (streaming) {
  1377. gspca_stream_off(gspca_dev);
  1378. /* Don't restart the stream when switching from read
  1379. * to mmap mode */
  1380. if (gspca_dev->memory == GSPCA_MEMORY_READ)
  1381. streaming = 0;
  1382. }
  1383. /* free the previous allocated buffers, if any */
  1384. if (gspca_dev->nframes != 0)
  1385. frame_free(gspca_dev);
  1386. if (rb->count == 0) /* unrequest */
  1387. goto out;
  1388. ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
  1389. if (ret == 0) {
  1390. rb->count = gspca_dev->nframes;
  1391. if (streaming)
  1392. ret = gspca_init_transfer(gspca_dev);
  1393. }
  1394. out:
  1395. mutex_unlock(&gspca_dev->queue_lock);
  1396. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1397. return ret;
  1398. }
  1399. static int vidioc_querybuf(struct file *file, void *priv,
  1400. struct v4l2_buffer *v4l2_buf)
  1401. {
  1402. struct gspca_dev *gspca_dev = video_drvdata(file);
  1403. struct gspca_frame *frame;
  1404. if (v4l2_buf->index < 0
  1405. || v4l2_buf->index >= gspca_dev->nframes)
  1406. return -EINVAL;
  1407. frame = &gspca_dev->frame[v4l2_buf->index];
  1408. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1409. return 0;
  1410. }
  1411. static int vidioc_streamon(struct file *file, void *priv,
  1412. enum v4l2_buf_type buf_type)
  1413. {
  1414. struct gspca_dev *gspca_dev = video_drvdata(file);
  1415. int ret;
  1416. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1417. return -EINVAL;
  1418. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1419. return -ERESTARTSYS;
  1420. /* check the capture file */
  1421. if (gspca_dev->capt_file != file) {
  1422. ret = -EBUSY;
  1423. goto out;
  1424. }
  1425. if (gspca_dev->nframes == 0
  1426. || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
  1427. ret = -EINVAL;
  1428. goto out;
  1429. }
  1430. if (!gspca_dev->streaming) {
  1431. ret = gspca_init_transfer(gspca_dev);
  1432. if (ret < 0)
  1433. goto out;
  1434. }
  1435. #ifdef GSPCA_DEBUG
  1436. if (gspca_debug & D_STREAM) {
  1437. PDEBUG_MODE("stream on OK",
  1438. gspca_dev->pixfmt,
  1439. gspca_dev->width,
  1440. gspca_dev->height);
  1441. }
  1442. #endif
  1443. ret = 0;
  1444. out:
  1445. mutex_unlock(&gspca_dev->queue_lock);
  1446. return ret;
  1447. }
  1448. static int vidioc_streamoff(struct file *file, void *priv,
  1449. enum v4l2_buf_type buf_type)
  1450. {
  1451. struct gspca_dev *gspca_dev = video_drvdata(file);
  1452. int i, ret;
  1453. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1454. return -EINVAL;
  1455. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1456. return -ERESTARTSYS;
  1457. if (!gspca_dev->streaming) {
  1458. ret = 0;
  1459. goto out;
  1460. }
  1461. /* check the capture file */
  1462. if (gspca_dev->capt_file != file) {
  1463. ret = -EBUSY;
  1464. goto out;
  1465. }
  1466. /* stop streaming */
  1467. gspca_stream_off(gspca_dev);
  1468. /* In case another thread is waiting in dqbuf */
  1469. wake_up_interruptible(&gspca_dev->wq);
  1470. /* empty the transfer queues */
  1471. for (i = 0; i < gspca_dev->nframes; i++)
  1472. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1473. atomic_set(&gspca_dev->fr_q, 0);
  1474. atomic_set(&gspca_dev->fr_i, 0);
  1475. gspca_dev->fr_o = 0;
  1476. ret = 0;
  1477. out:
  1478. mutex_unlock(&gspca_dev->queue_lock);
  1479. return ret;
  1480. }
  1481. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1482. struct v4l2_jpegcompression *jpegcomp)
  1483. {
  1484. struct gspca_dev *gspca_dev = video_drvdata(file);
  1485. gspca_dev->usb_err = 0;
  1486. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1487. }
  1488. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1489. struct v4l2_jpegcompression *jpegcomp)
  1490. {
  1491. struct gspca_dev *gspca_dev = video_drvdata(file);
  1492. gspca_dev->usb_err = 0;
  1493. return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1494. }
  1495. static int vidioc_g_parm(struct file *filp, void *priv,
  1496. struct v4l2_streamparm *parm)
  1497. {
  1498. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1499. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1500. if (gspca_dev->sd_desc->get_streamparm) {
  1501. gspca_dev->usb_err = 0;
  1502. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1503. return gspca_dev->usb_err;
  1504. }
  1505. return 0;
  1506. }
  1507. static int vidioc_s_parm(struct file *filp, void *priv,
  1508. struct v4l2_streamparm *parm)
  1509. {
  1510. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1511. int n;
  1512. n = parm->parm.capture.readbuffers;
  1513. if (n == 0 || n >= GSPCA_MAX_FRAMES)
  1514. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1515. else
  1516. gspca_dev->nbufread = n;
  1517. if (gspca_dev->sd_desc->set_streamparm) {
  1518. gspca_dev->usb_err = 0;
  1519. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1520. return gspca_dev->usb_err;
  1521. }
  1522. return 0;
  1523. }
  1524. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1525. {
  1526. struct gspca_dev *gspca_dev = video_drvdata(file);
  1527. struct gspca_frame *frame;
  1528. struct page *page;
  1529. unsigned long addr, start, size;
  1530. int i, ret;
  1531. start = vma->vm_start;
  1532. size = vma->vm_end - vma->vm_start;
  1533. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1534. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1535. return -ERESTARTSYS;
  1536. if (gspca_dev->capt_file != file) {
  1537. ret = -EINVAL;
  1538. goto out;
  1539. }
  1540. frame = NULL;
  1541. for (i = 0; i < gspca_dev->nframes; ++i) {
  1542. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1543. PDEBUG(D_STREAM, "mmap bad memory type");
  1544. break;
  1545. }
  1546. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1547. == vma->vm_pgoff) {
  1548. frame = &gspca_dev->frame[i];
  1549. break;
  1550. }
  1551. }
  1552. if (frame == NULL) {
  1553. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1554. ret = -EINVAL;
  1555. goto out;
  1556. }
  1557. if (size != frame->v4l2_buf.length) {
  1558. PDEBUG(D_STREAM, "mmap bad size");
  1559. ret = -EINVAL;
  1560. goto out;
  1561. }
  1562. /*
  1563. * - VM_IO marks the area as being a mmaped region for I/O to a
  1564. * device. It also prevents the region from being core dumped.
  1565. */
  1566. vma->vm_flags |= VM_IO;
  1567. addr = (unsigned long) frame->data;
  1568. while (size > 0) {
  1569. page = vmalloc_to_page((void *) addr);
  1570. ret = vm_insert_page(vma, start, page);
  1571. if (ret < 0)
  1572. goto out;
  1573. start += PAGE_SIZE;
  1574. addr += PAGE_SIZE;
  1575. size -= PAGE_SIZE;
  1576. }
  1577. vma->vm_ops = &gspca_vm_ops;
  1578. vma->vm_private_data = frame;
  1579. gspca_vm_open(vma);
  1580. ret = 0;
  1581. out:
  1582. mutex_unlock(&gspca_dev->queue_lock);
  1583. return ret;
  1584. }
  1585. static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
  1586. enum v4l2_memory memory)
  1587. {
  1588. if (!gspca_dev->present)
  1589. return -ENODEV;
  1590. if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
  1591. !gspca_dev->streaming)
  1592. return -EINVAL;
  1593. /* check if a frame is ready */
  1594. return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
  1595. }
  1596. static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
  1597. enum v4l2_memory memory)
  1598. {
  1599. int ret;
  1600. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1601. return -ERESTARTSYS;
  1602. ret = frame_ready_nolock(gspca_dev, file, memory);
  1603. mutex_unlock(&gspca_dev->queue_lock);
  1604. return ret;
  1605. }
  1606. /*
  1607. * dequeue a video buffer
  1608. *
  1609. * If nonblock_ing is false, block until a buffer is available.
  1610. */
  1611. static int vidioc_dqbuf(struct file *file, void *priv,
  1612. struct v4l2_buffer *v4l2_buf)
  1613. {
  1614. struct gspca_dev *gspca_dev = video_drvdata(file);
  1615. struct gspca_frame *frame;
  1616. int i, j, ret;
  1617. PDEBUG(D_FRAM, "dqbuf");
  1618. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1619. return -ERESTARTSYS;
  1620. for (;;) {
  1621. ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
  1622. if (ret < 0)
  1623. goto out;
  1624. if (ret > 0)
  1625. break;
  1626. mutex_unlock(&gspca_dev->queue_lock);
  1627. if (file->f_flags & O_NONBLOCK)
  1628. return -EAGAIN;
  1629. /* wait till a frame is ready */
  1630. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1631. frame_ready(gspca_dev, file, v4l2_buf->memory),
  1632. msecs_to_jiffies(3000));
  1633. if (ret < 0)
  1634. return ret;
  1635. if (ret == 0)
  1636. return -EIO;
  1637. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1638. return -ERESTARTSYS;
  1639. }
  1640. i = gspca_dev->fr_o;
  1641. j = gspca_dev->fr_queue[i];
  1642. frame = &gspca_dev->frame[j];
  1643. gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
  1644. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1645. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1646. PDEBUG(D_FRAM, "dqbuf %d", j);
  1647. ret = 0;
  1648. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1649. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1650. frame->data,
  1651. frame->v4l2_buf.bytesused)) {
  1652. PDEBUG(D_ERR|D_STREAM,
  1653. "dqbuf cp to user failed");
  1654. ret = -EFAULT;
  1655. }
  1656. }
  1657. out:
  1658. mutex_unlock(&gspca_dev->queue_lock);
  1659. if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
  1660. mutex_lock(&gspca_dev->usb_lock);
  1661. gspca_dev->usb_err = 0;
  1662. if (gspca_dev->present)
  1663. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1664. mutex_unlock(&gspca_dev->usb_lock);
  1665. }
  1666. return ret;
  1667. }
  1668. /*
  1669. * queue a video buffer
  1670. *
  1671. * Attempting to queue a buffer that has already been
  1672. * queued will return -EINVAL.
  1673. */
  1674. static int vidioc_qbuf(struct file *file, void *priv,
  1675. struct v4l2_buffer *v4l2_buf)
  1676. {
  1677. struct gspca_dev *gspca_dev = video_drvdata(file);
  1678. struct gspca_frame *frame;
  1679. int i, index, ret;
  1680. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1681. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1682. return -ERESTARTSYS;
  1683. index = v4l2_buf->index;
  1684. if ((unsigned) index >= gspca_dev->nframes) {
  1685. PDEBUG(D_FRAM,
  1686. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1687. ret = -EINVAL;
  1688. goto out;
  1689. }
  1690. if (v4l2_buf->memory != gspca_dev->memory) {
  1691. PDEBUG(D_FRAM, "qbuf bad memory type");
  1692. ret = -EINVAL;
  1693. goto out;
  1694. }
  1695. frame = &gspca_dev->frame[index];
  1696. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1697. PDEBUG(D_FRAM, "qbuf bad state");
  1698. ret = -EINVAL;
  1699. goto out;
  1700. }
  1701. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1702. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1703. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1704. frame->v4l2_buf.length = v4l2_buf->length;
  1705. }
  1706. /* put the buffer in the 'queued' queue */
  1707. i = atomic_read(&gspca_dev->fr_q);
  1708. gspca_dev->fr_queue[i] = index;
  1709. atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
  1710. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1711. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1712. ret = 0;
  1713. out:
  1714. mutex_unlock(&gspca_dev->queue_lock);
  1715. return ret;
  1716. }
  1717. /*
  1718. * allocate the resources for read()
  1719. */
  1720. static int read_alloc(struct gspca_dev *gspca_dev,
  1721. struct file *file)
  1722. {
  1723. struct v4l2_buffer v4l2_buf;
  1724. int i, ret;
  1725. PDEBUG(D_STREAM, "read alloc");
  1726. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1727. return -ERESTARTSYS;
  1728. if (gspca_dev->nframes == 0) {
  1729. struct v4l2_requestbuffers rb;
  1730. memset(&rb, 0, sizeof rb);
  1731. rb.count = gspca_dev->nbufread;
  1732. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1733. rb.memory = GSPCA_MEMORY_READ;
  1734. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1735. if (ret != 0) {
  1736. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1737. goto out;
  1738. }
  1739. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1740. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1741. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1742. for (i = 0; i < gspca_dev->nbufread; i++) {
  1743. v4l2_buf.index = i;
  1744. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1745. if (ret != 0) {
  1746. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1747. goto out;
  1748. }
  1749. }
  1750. }
  1751. /* start streaming */
  1752. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1753. if (ret != 0)
  1754. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1755. out:
  1756. mutex_unlock(&gspca_dev->usb_lock);
  1757. return ret;
  1758. }
  1759. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1760. {
  1761. struct gspca_dev *gspca_dev = video_drvdata(file);
  1762. unsigned long req_events = poll_requested_events(wait);
  1763. int ret = 0;
  1764. PDEBUG(D_FRAM, "poll");
  1765. if (req_events & POLLPRI)
  1766. ret |= v4l2_ctrl_poll(file, wait);
  1767. if (req_events & (POLLIN | POLLRDNORM)) {
  1768. /* if reqbufs is not done, the user would use read() */
  1769. if (gspca_dev->memory == GSPCA_MEMORY_NO) {
  1770. if (read_alloc(gspca_dev, file) != 0) {
  1771. ret |= POLLERR;
  1772. goto out;
  1773. }
  1774. }
  1775. poll_wait(file, &gspca_dev->wq, wait);
  1776. /* check if an image has been received */
  1777. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
  1778. ret |= POLLERR;
  1779. goto out;
  1780. }
  1781. if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
  1782. ret |= POLLIN | POLLRDNORM;
  1783. mutex_unlock(&gspca_dev->queue_lock);
  1784. }
  1785. out:
  1786. if (!gspca_dev->present)
  1787. ret |= POLLHUP;
  1788. return ret;
  1789. }
  1790. static ssize_t dev_read(struct file *file, char __user *data,
  1791. size_t count, loff_t *ppos)
  1792. {
  1793. struct gspca_dev *gspca_dev = video_drvdata(file);
  1794. struct gspca_frame *frame;
  1795. struct v4l2_buffer v4l2_buf;
  1796. struct timeval timestamp;
  1797. int n, ret, ret2;
  1798. PDEBUG(D_FRAM, "read (%zd)", count);
  1799. if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
  1800. ret = read_alloc(gspca_dev, file);
  1801. if (ret != 0)
  1802. return ret;
  1803. }
  1804. /* get a frame */
  1805. timestamp = ktime_to_timeval(ktime_get());
  1806. timestamp.tv_sec--;
  1807. n = 2;
  1808. for (;;) {
  1809. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1810. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1811. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1812. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1813. if (ret != 0) {
  1814. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1815. return ret;
  1816. }
  1817. /* if the process slept for more than 1 second,
  1818. * get a newer frame */
  1819. frame = &gspca_dev->frame[v4l2_buf.index];
  1820. if (--n < 0)
  1821. break; /* avoid infinite loop */
  1822. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1823. break;
  1824. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1825. if (ret != 0) {
  1826. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1827. return ret;
  1828. }
  1829. }
  1830. /* copy the frame */
  1831. if (count > frame->v4l2_buf.bytesused)
  1832. count = frame->v4l2_buf.bytesused;
  1833. ret = copy_to_user(data, frame->data, count);
  1834. if (ret != 0) {
  1835. PDEBUG(D_ERR|D_STREAM,
  1836. "read cp to user lack %d / %zd", ret, count);
  1837. ret = -EFAULT;
  1838. goto out;
  1839. }
  1840. ret = count;
  1841. out:
  1842. /* in each case, requeue the buffer */
  1843. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1844. if (ret2 != 0)
  1845. return ret2;
  1846. return ret;
  1847. }
  1848. static struct v4l2_file_operations dev_fops = {
  1849. .owner = THIS_MODULE,
  1850. .open = dev_open,
  1851. .release = dev_close,
  1852. .read = dev_read,
  1853. .mmap = dev_mmap,
  1854. .unlocked_ioctl = video_ioctl2,
  1855. .poll = dev_poll,
  1856. };
  1857. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1858. .vidioc_querycap = vidioc_querycap,
  1859. .vidioc_dqbuf = vidioc_dqbuf,
  1860. .vidioc_qbuf = vidioc_qbuf,
  1861. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1862. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1863. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1864. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1865. .vidioc_streamon = vidioc_streamon,
  1866. .vidioc_queryctrl = vidioc_queryctrl,
  1867. .vidioc_g_ctrl = vidioc_g_ctrl,
  1868. .vidioc_s_ctrl = vidioc_s_ctrl,
  1869. .vidioc_querymenu = vidioc_querymenu,
  1870. .vidioc_enum_input = vidioc_enum_input,
  1871. .vidioc_g_input = vidioc_g_input,
  1872. .vidioc_s_input = vidioc_s_input,
  1873. .vidioc_reqbufs = vidioc_reqbufs,
  1874. .vidioc_querybuf = vidioc_querybuf,
  1875. .vidioc_streamoff = vidioc_streamoff,
  1876. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1877. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1878. .vidioc_g_parm = vidioc_g_parm,
  1879. .vidioc_s_parm = vidioc_s_parm,
  1880. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1881. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1882. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1883. .vidioc_g_register = vidioc_g_register,
  1884. .vidioc_s_register = vidioc_s_register,
  1885. #endif
  1886. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1887. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1888. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1889. };
  1890. static const struct video_device gspca_template = {
  1891. .name = "gspca main driver",
  1892. .fops = &dev_fops,
  1893. .ioctl_ops = &dev_ioctl_ops,
  1894. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1895. };
  1896. /* initialize the controls */
  1897. static void ctrls_init(struct gspca_dev *gspca_dev)
  1898. {
  1899. struct gspca_ctrl *ctrl;
  1900. int i;
  1901. for (i = 0, ctrl = gspca_dev->cam.ctrls;
  1902. i < gspca_dev->sd_desc->nctrls;
  1903. i++, ctrl++) {
  1904. ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
  1905. ctrl->val = ctrl->def;
  1906. ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
  1907. ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
  1908. }
  1909. }
  1910. /*
  1911. * probe and create a new gspca device
  1912. *
  1913. * This function must be called by the sub-driver when it is
  1914. * called for probing a new device.
  1915. */
  1916. int gspca_dev_probe2(struct usb_interface *intf,
  1917. const struct usb_device_id *id,
  1918. const struct sd_desc *sd_desc,
  1919. int dev_size,
  1920. struct module *module)
  1921. {
  1922. struct gspca_dev *gspca_dev;
  1923. struct usb_device *dev = interface_to_usbdev(intf);
  1924. int ret;
  1925. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1926. sd_desc->name, id->idVendor, id->idProduct);
  1927. /* create the device */
  1928. if (dev_size < sizeof *gspca_dev)
  1929. dev_size = sizeof *gspca_dev;
  1930. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1931. if (!gspca_dev) {
  1932. pr_err("couldn't kzalloc gspca struct\n");
  1933. return -ENOMEM;
  1934. }
  1935. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1936. if (!gspca_dev->usb_buf) {
  1937. pr_err("out of memory\n");
  1938. ret = -ENOMEM;
  1939. goto out;
  1940. }
  1941. gspca_dev->dev = dev;
  1942. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1943. /* check if any audio device */
  1944. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1945. int i;
  1946. struct usb_interface *intf2;
  1947. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1948. intf2 = dev->actconfig->interface[i];
  1949. if (intf2 != NULL
  1950. && intf2->altsetting != NULL
  1951. && intf2->altsetting->desc.bInterfaceClass ==
  1952. USB_CLASS_AUDIO) {
  1953. gspca_dev->audio = 1;
  1954. break;
  1955. }
  1956. }
  1957. }
  1958. gspca_dev->v4l2_dev.release = gspca_release;
  1959. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1960. if (ret)
  1961. goto out;
  1962. gspca_dev->sd_desc = sd_desc;
  1963. gspca_dev->nbufread = 2;
  1964. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1965. gspca_dev->vdev = gspca_template;
  1966. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1967. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1968. set_bit(V4L2_FL_USE_FH_PRIO, &gspca_dev->vdev.flags);
  1969. gspca_dev->module = module;
  1970. gspca_dev->present = 1;
  1971. mutex_init(&gspca_dev->usb_lock);
  1972. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1973. mutex_init(&gspca_dev->queue_lock);
  1974. init_waitqueue_head(&gspca_dev->wq);
  1975. /* configure the subdriver and initialize the USB device */
  1976. ret = sd_desc->config(gspca_dev, id);
  1977. if (ret < 0)
  1978. goto out;
  1979. if (gspca_dev->cam.ctrls != NULL)
  1980. ctrls_init(gspca_dev);
  1981. ret = sd_desc->init(gspca_dev);
  1982. if (ret < 0)
  1983. goto out;
  1984. if (sd_desc->init_controls)
  1985. ret = sd_desc->init_controls(gspca_dev);
  1986. if (ret < 0)
  1987. goto out;
  1988. gspca_set_default_mode(gspca_dev);
  1989. ret = gspca_input_connect(gspca_dev);
  1990. if (ret)
  1991. goto out;
  1992. /*
  1993. * Don't take usb_lock for these ioctls. This improves latency if
  1994. * usb_lock is taken for a long time, e.g. when changing a control
  1995. * value, and a new frame is ready to be dequeued.
  1996. */
  1997. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
  1998. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
  1999. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
  2000. if (!gspca_dev->sd_desc->get_chip_ident)
  2001. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_CHIP_IDENT);
  2002. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2003. if (!gspca_dev->sd_desc->get_chip_ident ||
  2004. !gspca_dev->sd_desc->get_register)
  2005. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
  2006. if (!gspca_dev->sd_desc->get_chip_ident ||
  2007. !gspca_dev->sd_desc->set_register)
  2008. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
  2009. #endif
  2010. if (!gspca_dev->sd_desc->get_jcomp)
  2011. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
  2012. if (!gspca_dev->sd_desc->set_jcomp)
  2013. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
  2014. /* init video stuff */
  2015. ret = video_register_device(&gspca_dev->vdev,
  2016. VFL_TYPE_GRABBER,
  2017. -1);
  2018. if (ret < 0) {
  2019. pr_err("video_register_device err %d\n", ret);
  2020. goto out;
  2021. }
  2022. usb_set_intfdata(intf, gspca_dev);
  2023. PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
  2024. gspca_input_create_urb(gspca_dev);
  2025. return 0;
  2026. out:
  2027. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2028. if (gspca_dev->input_dev)
  2029. input_unregister_device(gspca_dev->input_dev);
  2030. #endif
  2031. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  2032. kfree(gspca_dev->usb_buf);
  2033. kfree(gspca_dev);
  2034. return ret;
  2035. }
  2036. EXPORT_SYMBOL(gspca_dev_probe2);
  2037. /* same function as the previous one, but check the interface */
  2038. int gspca_dev_probe(struct usb_interface *intf,
  2039. const struct usb_device_id *id,
  2040. const struct sd_desc *sd_desc,
  2041. int dev_size,
  2042. struct module *module)
  2043. {
  2044. struct usb_device *dev = interface_to_usbdev(intf);
  2045. /* we don't handle multi-config cameras */
  2046. if (dev->descriptor.bNumConfigurations != 1) {
  2047. pr_err("%04x:%04x too many config\n",
  2048. id->idVendor, id->idProduct);
  2049. return -ENODEV;
  2050. }
  2051. /* the USB video interface must be the first one */
  2052. if (dev->actconfig->desc.bNumInterfaces != 1
  2053. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  2054. return -ENODEV;
  2055. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  2056. }
  2057. EXPORT_SYMBOL(gspca_dev_probe);
  2058. /*
  2059. * USB disconnection
  2060. *
  2061. * This function must be called by the sub-driver
  2062. * when the device disconnects, after the specific resources are freed.
  2063. */
  2064. void gspca_disconnect(struct usb_interface *intf)
  2065. {
  2066. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2067. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2068. struct input_dev *input_dev;
  2069. #endif
  2070. PDEBUG(D_PROBE, "%s disconnect",
  2071. video_device_node_name(&gspca_dev->vdev));
  2072. mutex_lock(&gspca_dev->usb_lock);
  2073. gspca_dev->present = 0;
  2074. destroy_urbs(gspca_dev);
  2075. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2076. gspca_input_destroy_urb(gspca_dev);
  2077. input_dev = gspca_dev->input_dev;
  2078. if (input_dev) {
  2079. gspca_dev->input_dev = NULL;
  2080. input_unregister_device(input_dev);
  2081. }
  2082. #endif
  2083. /* Free subdriver's streaming resources / stop sd workqueue(s) */
  2084. if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
  2085. gspca_dev->sd_desc->stop0(gspca_dev);
  2086. gspca_dev->streaming = 0;
  2087. gspca_dev->dev = NULL;
  2088. wake_up_interruptible(&gspca_dev->wq);
  2089. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  2090. video_unregister_device(&gspca_dev->vdev);
  2091. mutex_unlock(&gspca_dev->usb_lock);
  2092. /* (this will call gspca_release() immediately or on last close) */
  2093. v4l2_device_put(&gspca_dev->v4l2_dev);
  2094. }
  2095. EXPORT_SYMBOL(gspca_disconnect);
  2096. #ifdef CONFIG_PM
  2097. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  2098. {
  2099. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2100. gspca_input_destroy_urb(gspca_dev);
  2101. if (!gspca_dev->streaming)
  2102. return 0;
  2103. mutex_lock(&gspca_dev->usb_lock);
  2104. gspca_dev->frozen = 1; /* avoid urb error messages */
  2105. gspca_dev->usb_err = 0;
  2106. if (gspca_dev->sd_desc->stopN)
  2107. gspca_dev->sd_desc->stopN(gspca_dev);
  2108. destroy_urbs(gspca_dev);
  2109. gspca_set_alt0(gspca_dev);
  2110. if (gspca_dev->sd_desc->stop0)
  2111. gspca_dev->sd_desc->stop0(gspca_dev);
  2112. mutex_unlock(&gspca_dev->usb_lock);
  2113. return 0;
  2114. }
  2115. EXPORT_SYMBOL(gspca_suspend);
  2116. int gspca_resume(struct usb_interface *intf)
  2117. {
  2118. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2119. int streaming, ret = 0;
  2120. mutex_lock(&gspca_dev->usb_lock);
  2121. gspca_dev->frozen = 0;
  2122. gspca_dev->usb_err = 0;
  2123. gspca_dev->sd_desc->init(gspca_dev);
  2124. /*
  2125. * Most subdrivers send all ctrl values on sd_start and thus
  2126. * only write to the device registers on s_ctrl when streaming ->
  2127. * Clear streaming to avoid setting all ctrls twice.
  2128. */
  2129. streaming = gspca_dev->streaming;
  2130. gspca_dev->streaming = 0;
  2131. if (streaming)
  2132. ret = gspca_init_transfer(gspca_dev);
  2133. else
  2134. gspca_input_create_urb(gspca_dev);
  2135. mutex_unlock(&gspca_dev->usb_lock);
  2136. return ret;
  2137. }
  2138. EXPORT_SYMBOL(gspca_resume);
  2139. #endif
  2140. /* -- module insert / remove -- */
  2141. static int __init gspca_init(void)
  2142. {
  2143. pr_info("v" GSPCA_VERSION " registered\n");
  2144. return 0;
  2145. }
  2146. static void __exit gspca_exit(void)
  2147. {
  2148. }
  2149. module_init(gspca_init);
  2150. module_exit(gspca_exit);
  2151. #ifdef GSPCA_DEBUG
  2152. module_param_named(debug, gspca_debug, int, 0644);
  2153. MODULE_PARM_DESC(debug,
  2154. "Debug (bit) 0x01:error 0x02:probe 0x04:config"
  2155. " 0x08:stream 0x10:frame 0x20:packet"
  2156. " 0x0100: v4l2");
  2157. #endif