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. /* some bulk transfers are started by the subdriver */
  834. if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
  835. break;
  836. /* submit the URBs */
  837. for (n = 0; n < MAX_NURBS; n++) {
  838. urb = gspca_dev->urb[n];
  839. if (urb == NULL)
  840. break;
  841. ret = usb_submit_urb(urb, GFP_KERNEL);
  842. if (ret < 0)
  843. break;
  844. }
  845. if (ret >= 0)
  846. break; /* transfer is started */
  847. /* something when wrong
  848. * stop the webcam and free the transfer resources */
  849. gspca_stream_off(gspca_dev);
  850. if (ret != -ENOSPC) {
  851. pr_err("usb_submit_urb alt %d err %d\n",
  852. gspca_dev->alt, ret);
  853. goto out;
  854. }
  855. /* the bandwidth is not wide enough
  856. * negotiate or try a lower alternate setting */
  857. retry:
  858. PDEBUG(D_ERR|D_STREAM,
  859. "alt %d - bandwidth not wide enough - trying again",
  860. alt);
  861. msleep(20); /* wait for kill complete */
  862. if (gspca_dev->sd_desc->isoc_nego) {
  863. ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
  864. if (ret < 0)
  865. goto out;
  866. } else {
  867. if (alt_idx <= 0) {
  868. pr_err("no transfer endpoint found\n");
  869. ret = -EIO;
  870. goto out;
  871. }
  872. gspca_dev->alt = ep_tb[--alt_idx].alt;
  873. }
  874. }
  875. out:
  876. gspca_input_create_urb(gspca_dev);
  877. return ret;
  878. }
  879. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  880. {
  881. struct gspca_ctrl *ctrl;
  882. int i;
  883. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  884. gspca_dev->curr_mode = i;
  885. gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
  886. gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
  887. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
  888. /* set the current control values to their default values
  889. * which may have changed in sd_init() */
  890. /* does nothing if ctrl_handler == NULL */
  891. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  892. ctrl = gspca_dev->cam.ctrls;
  893. if (ctrl != NULL) {
  894. for (i = 0;
  895. i < gspca_dev->sd_desc->nctrls;
  896. i++, ctrl++)
  897. ctrl->val = ctrl->def;
  898. }
  899. }
  900. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  901. int width, int height)
  902. {
  903. int i;
  904. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  905. if (width >= gspca_dev->cam.cam_mode[i].width
  906. && height >= gspca_dev->cam.cam_mode[i].height)
  907. break;
  908. }
  909. return i;
  910. }
  911. /*
  912. * search a mode with the right pixel format
  913. */
  914. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  915. int mode,
  916. int pixfmt)
  917. {
  918. int modeU, modeD;
  919. modeU = modeD = mode;
  920. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  921. if (--modeD >= 0) {
  922. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  923. == pixfmt)
  924. return modeD;
  925. }
  926. if (++modeU < gspca_dev->cam.nmodes) {
  927. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  928. == pixfmt)
  929. return modeU;
  930. }
  931. }
  932. return -EINVAL;
  933. }
  934. #ifdef CONFIG_VIDEO_ADV_DEBUG
  935. static int vidioc_g_register(struct file *file, void *priv,
  936. struct v4l2_dbg_register *reg)
  937. {
  938. struct gspca_dev *gspca_dev = video_drvdata(file);
  939. if (!gspca_dev->sd_desc->get_chip_ident)
  940. return -ENOTTY;
  941. if (!gspca_dev->sd_desc->get_register)
  942. return -ENOTTY;
  943. gspca_dev->usb_err = 0;
  944. return gspca_dev->sd_desc->get_register(gspca_dev, reg);
  945. }
  946. static int vidioc_s_register(struct file *file, void *priv,
  947. struct v4l2_dbg_register *reg)
  948. {
  949. struct gspca_dev *gspca_dev = video_drvdata(file);
  950. if (!gspca_dev->sd_desc->get_chip_ident)
  951. return -ENOTTY;
  952. if (!gspca_dev->sd_desc->set_register)
  953. return -ENOTTY;
  954. gspca_dev->usb_err = 0;
  955. return gspca_dev->sd_desc->set_register(gspca_dev, reg);
  956. }
  957. #endif
  958. static int vidioc_g_chip_ident(struct file *file, void *priv,
  959. struct v4l2_dbg_chip_ident *chip)
  960. {
  961. struct gspca_dev *gspca_dev = video_drvdata(file);
  962. if (!gspca_dev->sd_desc->get_chip_ident)
  963. return -ENOTTY;
  964. gspca_dev->usb_err = 0;
  965. return gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
  966. }
  967. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  968. struct v4l2_fmtdesc *fmtdesc)
  969. {
  970. struct gspca_dev *gspca_dev = video_drvdata(file);
  971. int i, j, index;
  972. __u32 fmt_tb[8];
  973. /* give an index to each format */
  974. index = 0;
  975. j = 0;
  976. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  977. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  978. j = 0;
  979. for (;;) {
  980. if (fmt_tb[j] == fmt_tb[index])
  981. break;
  982. j++;
  983. }
  984. if (j == index) {
  985. if (fmtdesc->index == index)
  986. break; /* new format */
  987. index++;
  988. if (index >= ARRAY_SIZE(fmt_tb))
  989. return -EINVAL;
  990. }
  991. }
  992. if (i < 0)
  993. return -EINVAL; /* no more format */
  994. fmtdesc->pixelformat = fmt_tb[index];
  995. if (gspca_dev->cam.cam_mode[i].sizeimage <
  996. gspca_dev->cam.cam_mode[i].width *
  997. gspca_dev->cam.cam_mode[i].height)
  998. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  999. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  1000. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  1001. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  1002. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  1003. fmtdesc->description[4] = '\0';
  1004. return 0;
  1005. }
  1006. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  1007. struct v4l2_format *fmt)
  1008. {
  1009. struct gspca_dev *gspca_dev = video_drvdata(file);
  1010. int mode;
  1011. mode = gspca_dev->curr_mode;
  1012. memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
  1013. sizeof fmt->fmt.pix);
  1014. return 0;
  1015. }
  1016. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  1017. struct v4l2_format *fmt)
  1018. {
  1019. int w, h, mode, mode2;
  1020. w = fmt->fmt.pix.width;
  1021. h = fmt->fmt.pix.height;
  1022. #ifdef GSPCA_DEBUG
  1023. if (gspca_debug & D_CONF)
  1024. PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
  1025. #endif
  1026. /* search the closest mode for width and height */
  1027. mode = wxh_to_mode(gspca_dev, w, h);
  1028. /* OK if right palette */
  1029. if (gspca_dev->cam.cam_mode[mode].pixelformat
  1030. != fmt->fmt.pix.pixelformat) {
  1031. /* else, search the closest mode with the same pixel format */
  1032. mode2 = gspca_get_mode(gspca_dev, mode,
  1033. fmt->fmt.pix.pixelformat);
  1034. if (mode2 >= 0)
  1035. mode = mode2;
  1036. /* else
  1037. ; * no chance, return this mode */
  1038. }
  1039. memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
  1040. sizeof fmt->fmt.pix);
  1041. return mode; /* used when s_fmt */
  1042. }
  1043. static int vidioc_try_fmt_vid_cap(struct file *file,
  1044. void *priv,
  1045. struct v4l2_format *fmt)
  1046. {
  1047. struct gspca_dev *gspca_dev = video_drvdata(file);
  1048. int ret;
  1049. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1050. if (ret < 0)
  1051. return ret;
  1052. return 0;
  1053. }
  1054. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1055. struct v4l2_format *fmt)
  1056. {
  1057. struct gspca_dev *gspca_dev = video_drvdata(file);
  1058. int ret;
  1059. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1060. return -ERESTARTSYS;
  1061. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1062. if (ret < 0)
  1063. goto out;
  1064. if (gspca_dev->nframes != 0
  1065. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  1066. ret = -EINVAL;
  1067. goto out;
  1068. }
  1069. if (ret == gspca_dev->curr_mode) {
  1070. ret = 0;
  1071. goto out; /* same mode */
  1072. }
  1073. if (gspca_dev->streaming) {
  1074. ret = -EBUSY;
  1075. goto out;
  1076. }
  1077. gspca_dev->width = fmt->fmt.pix.width;
  1078. gspca_dev->height = fmt->fmt.pix.height;
  1079. gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
  1080. gspca_dev->curr_mode = ret;
  1081. ret = 0;
  1082. out:
  1083. mutex_unlock(&gspca_dev->queue_lock);
  1084. return ret;
  1085. }
  1086. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1087. struct v4l2_frmsizeenum *fsize)
  1088. {
  1089. struct gspca_dev *gspca_dev = video_drvdata(file);
  1090. int i;
  1091. __u32 index = 0;
  1092. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  1093. if (fsize->pixel_format !=
  1094. gspca_dev->cam.cam_mode[i].pixelformat)
  1095. continue;
  1096. if (fsize->index == index) {
  1097. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1098. fsize->discrete.width =
  1099. gspca_dev->cam.cam_mode[i].width;
  1100. fsize->discrete.height =
  1101. gspca_dev->cam.cam_mode[i].height;
  1102. return 0;
  1103. }
  1104. index++;
  1105. }
  1106. return -EINVAL;
  1107. }
  1108. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  1109. struct v4l2_frmivalenum *fival)
  1110. {
  1111. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1112. int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
  1113. __u32 i;
  1114. if (gspca_dev->cam.mode_framerates == NULL ||
  1115. gspca_dev->cam.mode_framerates[mode].nrates == 0)
  1116. return -EINVAL;
  1117. if (fival->pixel_format !=
  1118. gspca_dev->cam.cam_mode[mode].pixelformat)
  1119. return -EINVAL;
  1120. for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
  1121. if (fival->index == i) {
  1122. fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1123. fival->discrete.numerator = 1;
  1124. fival->discrete.denominator =
  1125. gspca_dev->cam.mode_framerates[mode].rates[i];
  1126. return 0;
  1127. }
  1128. }
  1129. return -EINVAL;
  1130. }
  1131. static void gspca_release(struct v4l2_device *v4l2_device)
  1132. {
  1133. struct gspca_dev *gspca_dev =
  1134. container_of(v4l2_device, struct gspca_dev, v4l2_dev);
  1135. PDEBUG(D_PROBE, "%s released",
  1136. video_device_node_name(&gspca_dev->vdev));
  1137. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1138. v4l2_device_unregister(&gspca_dev->v4l2_dev);
  1139. kfree(gspca_dev->usb_buf);
  1140. kfree(gspca_dev);
  1141. }
  1142. static int dev_open(struct file *file)
  1143. {
  1144. struct gspca_dev *gspca_dev = video_drvdata(file);
  1145. PDEBUG(D_STREAM, "[%s] open", current->comm);
  1146. /* protect the subdriver against rmmod */
  1147. if (!try_module_get(gspca_dev->module))
  1148. return -ENODEV;
  1149. #ifdef GSPCA_DEBUG
  1150. /* activate the v4l2 debug */
  1151. if (gspca_debug & D_V4L2)
  1152. gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
  1153. | V4L2_DEBUG_IOCTL_ARG;
  1154. else
  1155. gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
  1156. | V4L2_DEBUG_IOCTL_ARG);
  1157. #endif
  1158. return v4l2_fh_open(file);
  1159. }
  1160. static int dev_close(struct file *file)
  1161. {
  1162. struct gspca_dev *gspca_dev = video_drvdata(file);
  1163. PDEBUG(D_STREAM, "[%s] close", current->comm);
  1164. /* Needed for gspca_stream_off, always lock before queue_lock! */
  1165. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1166. return -ERESTARTSYS;
  1167. if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
  1168. mutex_unlock(&gspca_dev->usb_lock);
  1169. return -ERESTARTSYS;
  1170. }
  1171. /* if the file did the capture, free the streaming resources */
  1172. if (gspca_dev->capt_file == file) {
  1173. if (gspca_dev->streaming)
  1174. gspca_stream_off(gspca_dev);
  1175. frame_free(gspca_dev);
  1176. }
  1177. module_put(gspca_dev->module);
  1178. mutex_unlock(&gspca_dev->queue_lock);
  1179. mutex_unlock(&gspca_dev->usb_lock);
  1180. PDEBUG(D_STREAM, "close done");
  1181. return v4l2_fh_release(file);
  1182. }
  1183. static int vidioc_querycap(struct file *file, void *priv,
  1184. struct v4l2_capability *cap)
  1185. {
  1186. struct gspca_dev *gspca_dev = video_drvdata(file);
  1187. strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
  1188. sizeof cap->driver);
  1189. if (gspca_dev->dev->product != NULL) {
  1190. strlcpy((char *) cap->card, gspca_dev->dev->product,
  1191. sizeof cap->card);
  1192. } else {
  1193. snprintf((char *) cap->card, sizeof cap->card,
  1194. "USB Camera (%04x:%04x)",
  1195. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  1196. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  1197. }
  1198. usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
  1199. sizeof(cap->bus_info));
  1200. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  1201. | V4L2_CAP_STREAMING
  1202. | V4L2_CAP_READWRITE;
  1203. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1204. return 0;
  1205. }
  1206. static int get_ctrl(struct gspca_dev *gspca_dev,
  1207. int id)
  1208. {
  1209. const struct ctrl *ctrls;
  1210. int i;
  1211. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  1212. i < gspca_dev->sd_desc->nctrls;
  1213. i++, ctrls++) {
  1214. if (gspca_dev->ctrl_dis & (1 << i))
  1215. continue;
  1216. if (id == ctrls->qctrl.id)
  1217. return i;
  1218. }
  1219. return -1;
  1220. }
  1221. static int vidioc_queryctrl(struct file *file, void *priv,
  1222. struct v4l2_queryctrl *q_ctrl)
  1223. {
  1224. struct gspca_dev *gspca_dev = video_drvdata(file);
  1225. const struct ctrl *ctrls;
  1226. struct gspca_ctrl *gspca_ctrl;
  1227. int i, idx;
  1228. u32 id;
  1229. id = q_ctrl->id;
  1230. if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  1231. id &= V4L2_CTRL_ID_MASK;
  1232. id++;
  1233. idx = -1;
  1234. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  1235. if (gspca_dev->ctrl_dis & (1 << i))
  1236. continue;
  1237. if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
  1238. continue;
  1239. if (idx >= 0
  1240. && gspca_dev->sd_desc->ctrls[i].qctrl.id
  1241. > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
  1242. continue;
  1243. idx = i;
  1244. }
  1245. } else {
  1246. idx = get_ctrl(gspca_dev, id);
  1247. }
  1248. if (idx < 0)
  1249. return -EINVAL;
  1250. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1251. memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
  1252. if (gspca_dev->cam.ctrls != NULL) {
  1253. gspca_ctrl = &gspca_dev->cam.ctrls[idx];
  1254. q_ctrl->default_value = gspca_ctrl->def;
  1255. q_ctrl->minimum = gspca_ctrl->min;
  1256. q_ctrl->maximum = gspca_ctrl->max;
  1257. }
  1258. if (gspca_dev->ctrl_inac & (1 << idx))
  1259. q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
  1260. return 0;
  1261. }
  1262. static int vidioc_s_ctrl(struct file *file, void *priv,
  1263. struct v4l2_control *ctrl)
  1264. {
  1265. struct gspca_dev *gspca_dev = video_drvdata(file);
  1266. const struct ctrl *ctrls;
  1267. struct gspca_ctrl *gspca_ctrl;
  1268. int idx;
  1269. idx = get_ctrl(gspca_dev, ctrl->id);
  1270. if (idx < 0)
  1271. return -EINVAL;
  1272. if (gspca_dev->ctrl_inac & (1 << idx))
  1273. return -EINVAL;
  1274. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1275. if (gspca_dev->cam.ctrls != NULL) {
  1276. gspca_ctrl = &gspca_dev->cam.ctrls[idx];
  1277. if (ctrl->value < gspca_ctrl->min
  1278. || ctrl->value > gspca_ctrl->max)
  1279. return -ERANGE;
  1280. } else {
  1281. gspca_ctrl = NULL;
  1282. if (ctrl->value < ctrls->qctrl.minimum
  1283. || ctrl->value > ctrls->qctrl.maximum)
  1284. return -ERANGE;
  1285. }
  1286. PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
  1287. gspca_dev->usb_err = 0;
  1288. if (ctrls->set != NULL)
  1289. return ctrls->set(gspca_dev, ctrl->value);
  1290. if (gspca_ctrl != NULL) {
  1291. gspca_ctrl->val = ctrl->value;
  1292. if (ctrls->set_control != NULL
  1293. && gspca_dev->streaming)
  1294. ctrls->set_control(gspca_dev);
  1295. }
  1296. return gspca_dev->usb_err;
  1297. }
  1298. static int vidioc_g_ctrl(struct file *file, void *priv,
  1299. struct v4l2_control *ctrl)
  1300. {
  1301. struct gspca_dev *gspca_dev = video_drvdata(file);
  1302. const struct ctrl *ctrls;
  1303. int idx;
  1304. idx = get_ctrl(gspca_dev, ctrl->id);
  1305. if (idx < 0)
  1306. return -EINVAL;
  1307. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1308. gspca_dev->usb_err = 0;
  1309. if (ctrls->get != NULL)
  1310. return ctrls->get(gspca_dev, &ctrl->value);
  1311. if (gspca_dev->cam.ctrls != NULL)
  1312. ctrl->value = gspca_dev->cam.ctrls[idx].val;
  1313. return 0;
  1314. }
  1315. static int vidioc_querymenu(struct file *file, void *priv,
  1316. struct v4l2_querymenu *qmenu)
  1317. {
  1318. struct gspca_dev *gspca_dev = video_drvdata(file);
  1319. if (!gspca_dev->sd_desc->querymenu)
  1320. return -ENOTTY;
  1321. return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
  1322. }
  1323. static int vidioc_enum_input(struct file *file, void *priv,
  1324. struct v4l2_input *input)
  1325. {
  1326. struct gspca_dev *gspca_dev = video_drvdata(file);
  1327. if (input->index != 0)
  1328. return -EINVAL;
  1329. input->type = V4L2_INPUT_TYPE_CAMERA;
  1330. input->status = gspca_dev->cam.input_flags;
  1331. strlcpy(input->name, gspca_dev->sd_desc->name,
  1332. sizeof input->name);
  1333. return 0;
  1334. }
  1335. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1336. {
  1337. *i = 0;
  1338. return 0;
  1339. }
  1340. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1341. {
  1342. if (i > 0)
  1343. return -EINVAL;
  1344. return (0);
  1345. }
  1346. static int vidioc_reqbufs(struct file *file, void *priv,
  1347. struct v4l2_requestbuffers *rb)
  1348. {
  1349. struct gspca_dev *gspca_dev = video_drvdata(file);
  1350. int i, ret = 0, streaming;
  1351. i = rb->memory; /* (avoid compilation warning) */
  1352. switch (i) {
  1353. case GSPCA_MEMORY_READ: /* (internal call) */
  1354. case V4L2_MEMORY_MMAP:
  1355. case V4L2_MEMORY_USERPTR:
  1356. break;
  1357. default:
  1358. return -EINVAL;
  1359. }
  1360. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1361. return -ERESTARTSYS;
  1362. if (gspca_dev->memory != GSPCA_MEMORY_NO
  1363. && gspca_dev->memory != GSPCA_MEMORY_READ
  1364. && gspca_dev->memory != rb->memory) {
  1365. ret = -EBUSY;
  1366. goto out;
  1367. }
  1368. /* only one file may do the capture */
  1369. if (gspca_dev->capt_file != NULL
  1370. && gspca_dev->capt_file != file) {
  1371. ret = -EBUSY;
  1372. goto out;
  1373. }
  1374. /* if allocated, the buffers must not be mapped */
  1375. for (i = 0; i < gspca_dev->nframes; i++) {
  1376. if (gspca_dev->frame[i].vma_use_count) {
  1377. ret = -EBUSY;
  1378. goto out;
  1379. }
  1380. }
  1381. /* stop streaming */
  1382. streaming = gspca_dev->streaming;
  1383. if (streaming) {
  1384. gspca_stream_off(gspca_dev);
  1385. /* Don't restart the stream when switching from read
  1386. * to mmap mode */
  1387. if (gspca_dev->memory == GSPCA_MEMORY_READ)
  1388. streaming = 0;
  1389. }
  1390. /* free the previous allocated buffers, if any */
  1391. if (gspca_dev->nframes != 0)
  1392. frame_free(gspca_dev);
  1393. if (rb->count == 0) /* unrequest */
  1394. goto out;
  1395. ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
  1396. if (ret == 0) {
  1397. rb->count = gspca_dev->nframes;
  1398. if (streaming)
  1399. ret = gspca_init_transfer(gspca_dev);
  1400. }
  1401. out:
  1402. mutex_unlock(&gspca_dev->queue_lock);
  1403. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1404. return ret;
  1405. }
  1406. static int vidioc_querybuf(struct file *file, void *priv,
  1407. struct v4l2_buffer *v4l2_buf)
  1408. {
  1409. struct gspca_dev *gspca_dev = video_drvdata(file);
  1410. struct gspca_frame *frame;
  1411. if (v4l2_buf->index < 0
  1412. || v4l2_buf->index >= gspca_dev->nframes)
  1413. return -EINVAL;
  1414. frame = &gspca_dev->frame[v4l2_buf->index];
  1415. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1416. return 0;
  1417. }
  1418. static int vidioc_streamon(struct file *file, void *priv,
  1419. enum v4l2_buf_type buf_type)
  1420. {
  1421. struct gspca_dev *gspca_dev = video_drvdata(file);
  1422. int ret;
  1423. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1424. return -EINVAL;
  1425. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1426. return -ERESTARTSYS;
  1427. /* check the capture file */
  1428. if (gspca_dev->capt_file != file) {
  1429. ret = -EBUSY;
  1430. goto out;
  1431. }
  1432. if (gspca_dev->nframes == 0
  1433. || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
  1434. ret = -EINVAL;
  1435. goto out;
  1436. }
  1437. if (!gspca_dev->streaming) {
  1438. ret = gspca_init_transfer(gspca_dev);
  1439. if (ret < 0)
  1440. goto out;
  1441. }
  1442. #ifdef GSPCA_DEBUG
  1443. if (gspca_debug & D_STREAM) {
  1444. PDEBUG_MODE("stream on OK",
  1445. gspca_dev->pixfmt,
  1446. gspca_dev->width,
  1447. gspca_dev->height);
  1448. }
  1449. #endif
  1450. ret = 0;
  1451. out:
  1452. mutex_unlock(&gspca_dev->queue_lock);
  1453. return ret;
  1454. }
  1455. static int vidioc_streamoff(struct file *file, void *priv,
  1456. enum v4l2_buf_type buf_type)
  1457. {
  1458. struct gspca_dev *gspca_dev = video_drvdata(file);
  1459. int i, ret;
  1460. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1461. return -EINVAL;
  1462. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1463. return -ERESTARTSYS;
  1464. if (!gspca_dev->streaming) {
  1465. ret = 0;
  1466. goto out;
  1467. }
  1468. /* check the capture file */
  1469. if (gspca_dev->capt_file != file) {
  1470. ret = -EBUSY;
  1471. goto out;
  1472. }
  1473. /* stop streaming */
  1474. gspca_stream_off(gspca_dev);
  1475. /* In case another thread is waiting in dqbuf */
  1476. wake_up_interruptible(&gspca_dev->wq);
  1477. /* empty the transfer queues */
  1478. for (i = 0; i < gspca_dev->nframes; i++)
  1479. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1480. atomic_set(&gspca_dev->fr_q, 0);
  1481. atomic_set(&gspca_dev->fr_i, 0);
  1482. gspca_dev->fr_o = 0;
  1483. ret = 0;
  1484. out:
  1485. mutex_unlock(&gspca_dev->queue_lock);
  1486. return ret;
  1487. }
  1488. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1489. struct v4l2_jpegcompression *jpegcomp)
  1490. {
  1491. struct gspca_dev *gspca_dev = video_drvdata(file);
  1492. if (!gspca_dev->sd_desc->get_jcomp)
  1493. return -ENOTTY;
  1494. gspca_dev->usb_err = 0;
  1495. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1496. }
  1497. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1498. struct v4l2_jpegcompression *jpegcomp)
  1499. {
  1500. struct gspca_dev *gspca_dev = video_drvdata(file);
  1501. if (!gspca_dev->sd_desc->set_jcomp)
  1502. return -ENOTTY;
  1503. gspca_dev->usb_err = 0;
  1504. return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1505. }
  1506. static int vidioc_g_parm(struct file *filp, void *priv,
  1507. struct v4l2_streamparm *parm)
  1508. {
  1509. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1510. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1511. if (gspca_dev->sd_desc->get_streamparm) {
  1512. gspca_dev->usb_err = 0;
  1513. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1514. return gspca_dev->usb_err;
  1515. }
  1516. return 0;
  1517. }
  1518. static int vidioc_s_parm(struct file *filp, void *priv,
  1519. struct v4l2_streamparm *parm)
  1520. {
  1521. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1522. int n;
  1523. n = parm->parm.capture.readbuffers;
  1524. if (n == 0 || n >= GSPCA_MAX_FRAMES)
  1525. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1526. else
  1527. gspca_dev->nbufread = n;
  1528. if (gspca_dev->sd_desc->set_streamparm) {
  1529. gspca_dev->usb_err = 0;
  1530. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1531. return gspca_dev->usb_err;
  1532. }
  1533. return 0;
  1534. }
  1535. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1536. {
  1537. struct gspca_dev *gspca_dev = video_drvdata(file);
  1538. struct gspca_frame *frame;
  1539. struct page *page;
  1540. unsigned long addr, start, size;
  1541. int i, ret;
  1542. start = vma->vm_start;
  1543. size = vma->vm_end - vma->vm_start;
  1544. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1545. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1546. return -ERESTARTSYS;
  1547. if (gspca_dev->capt_file != file) {
  1548. ret = -EINVAL;
  1549. goto out;
  1550. }
  1551. frame = NULL;
  1552. for (i = 0; i < gspca_dev->nframes; ++i) {
  1553. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1554. PDEBUG(D_STREAM, "mmap bad memory type");
  1555. break;
  1556. }
  1557. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1558. == vma->vm_pgoff) {
  1559. frame = &gspca_dev->frame[i];
  1560. break;
  1561. }
  1562. }
  1563. if (frame == NULL) {
  1564. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1565. ret = -EINVAL;
  1566. goto out;
  1567. }
  1568. if (size != frame->v4l2_buf.length) {
  1569. PDEBUG(D_STREAM, "mmap bad size");
  1570. ret = -EINVAL;
  1571. goto out;
  1572. }
  1573. /*
  1574. * - VM_IO marks the area as being a mmaped region for I/O to a
  1575. * device. It also prevents the region from being core dumped.
  1576. */
  1577. vma->vm_flags |= VM_IO;
  1578. addr = (unsigned long) frame->data;
  1579. while (size > 0) {
  1580. page = vmalloc_to_page((void *) addr);
  1581. ret = vm_insert_page(vma, start, page);
  1582. if (ret < 0)
  1583. goto out;
  1584. start += PAGE_SIZE;
  1585. addr += PAGE_SIZE;
  1586. size -= PAGE_SIZE;
  1587. }
  1588. vma->vm_ops = &gspca_vm_ops;
  1589. vma->vm_private_data = frame;
  1590. gspca_vm_open(vma);
  1591. ret = 0;
  1592. out:
  1593. mutex_unlock(&gspca_dev->queue_lock);
  1594. return ret;
  1595. }
  1596. static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
  1597. enum v4l2_memory memory)
  1598. {
  1599. if (!gspca_dev->present)
  1600. return -ENODEV;
  1601. if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
  1602. !gspca_dev->streaming)
  1603. return -EINVAL;
  1604. /* check if a frame is ready */
  1605. return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
  1606. }
  1607. static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
  1608. enum v4l2_memory memory)
  1609. {
  1610. int ret;
  1611. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1612. return -ERESTARTSYS;
  1613. ret = frame_ready_nolock(gspca_dev, file, memory);
  1614. mutex_unlock(&gspca_dev->queue_lock);
  1615. return ret;
  1616. }
  1617. /*
  1618. * dequeue a video buffer
  1619. *
  1620. * If nonblock_ing is false, block until a buffer is available.
  1621. */
  1622. static int vidioc_dqbuf(struct file *file, void *priv,
  1623. struct v4l2_buffer *v4l2_buf)
  1624. {
  1625. struct gspca_dev *gspca_dev = video_drvdata(file);
  1626. struct gspca_frame *frame;
  1627. int i, j, ret;
  1628. PDEBUG(D_FRAM, "dqbuf");
  1629. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1630. return -ERESTARTSYS;
  1631. for (;;) {
  1632. ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
  1633. if (ret < 0)
  1634. goto out;
  1635. if (ret > 0)
  1636. break;
  1637. mutex_unlock(&gspca_dev->queue_lock);
  1638. if (file->f_flags & O_NONBLOCK)
  1639. return -EAGAIN;
  1640. /* wait till a frame is ready */
  1641. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1642. frame_ready(gspca_dev, file, v4l2_buf->memory),
  1643. msecs_to_jiffies(3000));
  1644. if (ret < 0)
  1645. return ret;
  1646. if (ret == 0)
  1647. return -EIO;
  1648. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1649. return -ERESTARTSYS;
  1650. }
  1651. i = gspca_dev->fr_o;
  1652. j = gspca_dev->fr_queue[i];
  1653. frame = &gspca_dev->frame[j];
  1654. gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
  1655. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1656. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1657. PDEBUG(D_FRAM, "dqbuf %d", j);
  1658. ret = 0;
  1659. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1660. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1661. frame->data,
  1662. frame->v4l2_buf.bytesused)) {
  1663. PDEBUG(D_ERR|D_STREAM,
  1664. "dqbuf cp to user failed");
  1665. ret = -EFAULT;
  1666. }
  1667. }
  1668. out:
  1669. mutex_unlock(&gspca_dev->queue_lock);
  1670. if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
  1671. mutex_lock(&gspca_dev->usb_lock);
  1672. gspca_dev->usb_err = 0;
  1673. if (gspca_dev->present)
  1674. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1675. mutex_unlock(&gspca_dev->usb_lock);
  1676. }
  1677. return ret;
  1678. }
  1679. /*
  1680. * queue a video buffer
  1681. *
  1682. * Attempting to queue a buffer that has already been
  1683. * queued will return -EINVAL.
  1684. */
  1685. static int vidioc_qbuf(struct file *file, void *priv,
  1686. struct v4l2_buffer *v4l2_buf)
  1687. {
  1688. struct gspca_dev *gspca_dev = video_drvdata(file);
  1689. struct gspca_frame *frame;
  1690. int i, index, ret;
  1691. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1692. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1693. return -ERESTARTSYS;
  1694. index = v4l2_buf->index;
  1695. if ((unsigned) index >= gspca_dev->nframes) {
  1696. PDEBUG(D_FRAM,
  1697. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1698. ret = -EINVAL;
  1699. goto out;
  1700. }
  1701. if (v4l2_buf->memory != gspca_dev->memory) {
  1702. PDEBUG(D_FRAM, "qbuf bad memory type");
  1703. ret = -EINVAL;
  1704. goto out;
  1705. }
  1706. frame = &gspca_dev->frame[index];
  1707. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1708. PDEBUG(D_FRAM, "qbuf bad state");
  1709. ret = -EINVAL;
  1710. goto out;
  1711. }
  1712. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1713. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1714. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1715. frame->v4l2_buf.length = v4l2_buf->length;
  1716. }
  1717. /* put the buffer in the 'queued' queue */
  1718. i = atomic_read(&gspca_dev->fr_q);
  1719. gspca_dev->fr_queue[i] = index;
  1720. atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
  1721. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1722. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1723. ret = 0;
  1724. out:
  1725. mutex_unlock(&gspca_dev->queue_lock);
  1726. return ret;
  1727. }
  1728. /*
  1729. * allocate the resources for read()
  1730. */
  1731. static int read_alloc(struct gspca_dev *gspca_dev,
  1732. struct file *file)
  1733. {
  1734. struct v4l2_buffer v4l2_buf;
  1735. int i, ret;
  1736. PDEBUG(D_STREAM, "read alloc");
  1737. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1738. return -ERESTARTSYS;
  1739. if (gspca_dev->nframes == 0) {
  1740. struct v4l2_requestbuffers rb;
  1741. memset(&rb, 0, sizeof rb);
  1742. rb.count = gspca_dev->nbufread;
  1743. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1744. rb.memory = GSPCA_MEMORY_READ;
  1745. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1746. if (ret != 0) {
  1747. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1748. goto out;
  1749. }
  1750. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1751. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1752. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1753. for (i = 0; i < gspca_dev->nbufread; i++) {
  1754. v4l2_buf.index = i;
  1755. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1756. if (ret != 0) {
  1757. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1758. goto out;
  1759. }
  1760. }
  1761. }
  1762. /* start streaming */
  1763. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1764. if (ret != 0)
  1765. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1766. out:
  1767. mutex_unlock(&gspca_dev->usb_lock);
  1768. return ret;
  1769. }
  1770. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1771. {
  1772. struct gspca_dev *gspca_dev = video_drvdata(file);
  1773. unsigned long req_events = poll_requested_events(wait);
  1774. int ret = 0;
  1775. PDEBUG(D_FRAM, "poll");
  1776. if (req_events & POLLPRI)
  1777. ret |= v4l2_ctrl_poll(file, wait);
  1778. if (req_events & (POLLIN | POLLRDNORM)) {
  1779. /* if reqbufs is not done, the user would use read() */
  1780. if (gspca_dev->memory == GSPCA_MEMORY_NO) {
  1781. if (read_alloc(gspca_dev, file) != 0) {
  1782. ret |= POLLERR;
  1783. goto out;
  1784. }
  1785. }
  1786. poll_wait(file, &gspca_dev->wq, wait);
  1787. /* check if an image has been received */
  1788. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
  1789. ret |= POLLERR;
  1790. goto out;
  1791. }
  1792. if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
  1793. ret |= POLLIN | POLLRDNORM;
  1794. mutex_unlock(&gspca_dev->queue_lock);
  1795. }
  1796. out:
  1797. if (!gspca_dev->present)
  1798. ret |= POLLHUP;
  1799. return ret;
  1800. }
  1801. static ssize_t dev_read(struct file *file, char __user *data,
  1802. size_t count, loff_t *ppos)
  1803. {
  1804. struct gspca_dev *gspca_dev = video_drvdata(file);
  1805. struct gspca_frame *frame;
  1806. struct v4l2_buffer v4l2_buf;
  1807. struct timeval timestamp;
  1808. int n, ret, ret2;
  1809. PDEBUG(D_FRAM, "read (%zd)", count);
  1810. if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
  1811. ret = read_alloc(gspca_dev, file);
  1812. if (ret != 0)
  1813. return ret;
  1814. }
  1815. /* get a frame */
  1816. timestamp = ktime_to_timeval(ktime_get());
  1817. timestamp.tv_sec--;
  1818. n = 2;
  1819. for (;;) {
  1820. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1821. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1822. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1823. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1824. if (ret != 0) {
  1825. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1826. return ret;
  1827. }
  1828. /* if the process slept for more than 1 second,
  1829. * get a newer frame */
  1830. frame = &gspca_dev->frame[v4l2_buf.index];
  1831. if (--n < 0)
  1832. break; /* avoid infinite loop */
  1833. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1834. break;
  1835. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1836. if (ret != 0) {
  1837. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1838. return ret;
  1839. }
  1840. }
  1841. /* copy the frame */
  1842. if (count > frame->v4l2_buf.bytesused)
  1843. count = frame->v4l2_buf.bytesused;
  1844. ret = copy_to_user(data, frame->data, count);
  1845. if (ret != 0) {
  1846. PDEBUG(D_ERR|D_STREAM,
  1847. "read cp to user lack %d / %zd", ret, count);
  1848. ret = -EFAULT;
  1849. goto out;
  1850. }
  1851. ret = count;
  1852. out:
  1853. /* in each case, requeue the buffer */
  1854. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1855. if (ret2 != 0)
  1856. return ret2;
  1857. return ret;
  1858. }
  1859. static struct v4l2_file_operations dev_fops = {
  1860. .owner = THIS_MODULE,
  1861. .open = dev_open,
  1862. .release = dev_close,
  1863. .read = dev_read,
  1864. .mmap = dev_mmap,
  1865. .unlocked_ioctl = video_ioctl2,
  1866. .poll = dev_poll,
  1867. };
  1868. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1869. .vidioc_querycap = vidioc_querycap,
  1870. .vidioc_dqbuf = vidioc_dqbuf,
  1871. .vidioc_qbuf = vidioc_qbuf,
  1872. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1873. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1874. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1875. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1876. .vidioc_streamon = vidioc_streamon,
  1877. .vidioc_queryctrl = vidioc_queryctrl,
  1878. .vidioc_g_ctrl = vidioc_g_ctrl,
  1879. .vidioc_s_ctrl = vidioc_s_ctrl,
  1880. .vidioc_querymenu = vidioc_querymenu,
  1881. .vidioc_enum_input = vidioc_enum_input,
  1882. .vidioc_g_input = vidioc_g_input,
  1883. .vidioc_s_input = vidioc_s_input,
  1884. .vidioc_reqbufs = vidioc_reqbufs,
  1885. .vidioc_querybuf = vidioc_querybuf,
  1886. .vidioc_streamoff = vidioc_streamoff,
  1887. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1888. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1889. .vidioc_g_parm = vidioc_g_parm,
  1890. .vidioc_s_parm = vidioc_s_parm,
  1891. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1892. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1893. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1894. .vidioc_g_register = vidioc_g_register,
  1895. .vidioc_s_register = vidioc_s_register,
  1896. #endif
  1897. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1898. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1899. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1900. };
  1901. static const struct video_device gspca_template = {
  1902. .name = "gspca main driver",
  1903. .fops = &dev_fops,
  1904. .ioctl_ops = &dev_ioctl_ops,
  1905. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1906. };
  1907. /* initialize the controls */
  1908. static void ctrls_init(struct gspca_dev *gspca_dev)
  1909. {
  1910. struct gspca_ctrl *ctrl;
  1911. int i;
  1912. for (i = 0, ctrl = gspca_dev->cam.ctrls;
  1913. i < gspca_dev->sd_desc->nctrls;
  1914. i++, ctrl++) {
  1915. ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
  1916. ctrl->val = ctrl->def;
  1917. ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
  1918. ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
  1919. }
  1920. }
  1921. /*
  1922. * probe and create a new gspca device
  1923. *
  1924. * This function must be called by the sub-driver when it is
  1925. * called for probing a new device.
  1926. */
  1927. int gspca_dev_probe2(struct usb_interface *intf,
  1928. const struct usb_device_id *id,
  1929. const struct sd_desc *sd_desc,
  1930. int dev_size,
  1931. struct module *module)
  1932. {
  1933. struct gspca_dev *gspca_dev;
  1934. struct usb_device *dev = interface_to_usbdev(intf);
  1935. int ret;
  1936. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1937. sd_desc->name, id->idVendor, id->idProduct);
  1938. /* create the device */
  1939. if (dev_size < sizeof *gspca_dev)
  1940. dev_size = sizeof *gspca_dev;
  1941. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1942. if (!gspca_dev) {
  1943. pr_err("couldn't kzalloc gspca struct\n");
  1944. return -ENOMEM;
  1945. }
  1946. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1947. if (!gspca_dev->usb_buf) {
  1948. pr_err("out of memory\n");
  1949. ret = -ENOMEM;
  1950. goto out;
  1951. }
  1952. gspca_dev->dev = dev;
  1953. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1954. /* check if any audio device */
  1955. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1956. int i;
  1957. struct usb_interface *intf2;
  1958. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1959. intf2 = dev->actconfig->interface[i];
  1960. if (intf2 != NULL
  1961. && intf2->altsetting != NULL
  1962. && intf2->altsetting->desc.bInterfaceClass ==
  1963. USB_CLASS_AUDIO) {
  1964. gspca_dev->audio = 1;
  1965. break;
  1966. }
  1967. }
  1968. }
  1969. gspca_dev->v4l2_dev.release = gspca_release;
  1970. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1971. if (ret)
  1972. goto out;
  1973. gspca_dev->sd_desc = sd_desc;
  1974. gspca_dev->nbufread = 2;
  1975. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1976. gspca_dev->vdev = gspca_template;
  1977. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1978. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1979. set_bit(V4L2_FL_USE_FH_PRIO, &gspca_dev->vdev.flags);
  1980. gspca_dev->module = module;
  1981. gspca_dev->present = 1;
  1982. mutex_init(&gspca_dev->usb_lock);
  1983. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1984. mutex_init(&gspca_dev->queue_lock);
  1985. init_waitqueue_head(&gspca_dev->wq);
  1986. /* configure the subdriver and initialize the USB device */
  1987. ret = sd_desc->config(gspca_dev, id);
  1988. if (ret < 0)
  1989. goto out;
  1990. if (gspca_dev->cam.ctrls != NULL)
  1991. ctrls_init(gspca_dev);
  1992. ret = sd_desc->init(gspca_dev);
  1993. if (ret < 0)
  1994. goto out;
  1995. if (sd_desc->init_controls)
  1996. ret = sd_desc->init_controls(gspca_dev);
  1997. if (ret < 0)
  1998. goto out;
  1999. gspca_set_default_mode(gspca_dev);
  2000. ret = gspca_input_connect(gspca_dev);
  2001. if (ret)
  2002. goto out;
  2003. /*
  2004. * Don't take usb_lock for these ioctls. This improves latency if
  2005. * usb_lock is taken for a long time, e.g. when changing a control
  2006. * value, and a new frame is ready to be dequeued.
  2007. */
  2008. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
  2009. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
  2010. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
  2011. /* init video stuff */
  2012. ret = video_register_device(&gspca_dev->vdev,
  2013. VFL_TYPE_GRABBER,
  2014. -1);
  2015. if (ret < 0) {
  2016. pr_err("video_register_device err %d\n", ret);
  2017. goto out;
  2018. }
  2019. usb_set_intfdata(intf, gspca_dev);
  2020. PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
  2021. gspca_input_create_urb(gspca_dev);
  2022. return 0;
  2023. out:
  2024. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2025. if (gspca_dev->input_dev)
  2026. input_unregister_device(gspca_dev->input_dev);
  2027. #endif
  2028. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  2029. kfree(gspca_dev->usb_buf);
  2030. kfree(gspca_dev);
  2031. return ret;
  2032. }
  2033. EXPORT_SYMBOL(gspca_dev_probe2);
  2034. /* same function as the previous one, but check the interface */
  2035. int gspca_dev_probe(struct usb_interface *intf,
  2036. const struct usb_device_id *id,
  2037. const struct sd_desc *sd_desc,
  2038. int dev_size,
  2039. struct module *module)
  2040. {
  2041. struct usb_device *dev = interface_to_usbdev(intf);
  2042. /* we don't handle multi-config cameras */
  2043. if (dev->descriptor.bNumConfigurations != 1) {
  2044. pr_err("%04x:%04x too many config\n",
  2045. id->idVendor, id->idProduct);
  2046. return -ENODEV;
  2047. }
  2048. /* the USB video interface must be the first one */
  2049. if (dev->actconfig->desc.bNumInterfaces != 1
  2050. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  2051. return -ENODEV;
  2052. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  2053. }
  2054. EXPORT_SYMBOL(gspca_dev_probe);
  2055. /*
  2056. * USB disconnection
  2057. *
  2058. * This function must be called by the sub-driver
  2059. * when the device disconnects, after the specific resources are freed.
  2060. */
  2061. void gspca_disconnect(struct usb_interface *intf)
  2062. {
  2063. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2064. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2065. struct input_dev *input_dev;
  2066. #endif
  2067. PDEBUG(D_PROBE, "%s disconnect",
  2068. video_device_node_name(&gspca_dev->vdev));
  2069. mutex_lock(&gspca_dev->usb_lock);
  2070. usb_set_intfdata(intf, NULL);
  2071. gspca_dev->dev = NULL;
  2072. gspca_dev->present = 0;
  2073. destroy_urbs(gspca_dev);
  2074. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2075. gspca_input_destroy_urb(gspca_dev);
  2076. input_dev = gspca_dev->input_dev;
  2077. if (input_dev) {
  2078. gspca_dev->input_dev = NULL;
  2079. input_unregister_device(input_dev);
  2080. }
  2081. #endif
  2082. /* Free subdriver's streaming resources / stop sd workqueue(s) */
  2083. if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
  2084. gspca_dev->sd_desc->stop0(gspca_dev);
  2085. gspca_dev->streaming = 0;
  2086. wake_up_interruptible(&gspca_dev->wq);
  2087. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  2088. video_unregister_device(&gspca_dev->vdev);
  2089. mutex_unlock(&gspca_dev->usb_lock);
  2090. /* (this will call gspca_release() immediately or on last close) */
  2091. v4l2_device_put(&gspca_dev->v4l2_dev);
  2092. }
  2093. EXPORT_SYMBOL(gspca_disconnect);
  2094. #ifdef CONFIG_PM
  2095. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  2096. {
  2097. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2098. if (!gspca_dev->streaming)
  2099. return 0;
  2100. mutex_lock(&gspca_dev->usb_lock);
  2101. gspca_dev->frozen = 1; /* avoid urb error messages */
  2102. gspca_dev->usb_err = 0;
  2103. if (gspca_dev->sd_desc->stopN)
  2104. gspca_dev->sd_desc->stopN(gspca_dev);
  2105. destroy_urbs(gspca_dev);
  2106. gspca_input_destroy_urb(gspca_dev);
  2107. gspca_set_alt0(gspca_dev);
  2108. if (gspca_dev->sd_desc->stop0)
  2109. gspca_dev->sd_desc->stop0(gspca_dev);
  2110. mutex_unlock(&gspca_dev->usb_lock);
  2111. return 0;
  2112. }
  2113. EXPORT_SYMBOL(gspca_suspend);
  2114. int gspca_resume(struct usb_interface *intf)
  2115. {
  2116. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2117. int streaming, ret = 0;
  2118. mutex_lock(&gspca_dev->usb_lock);
  2119. gspca_dev->frozen = 0;
  2120. gspca_dev->usb_err = 0;
  2121. gspca_dev->sd_desc->init(gspca_dev);
  2122. gspca_input_create_urb(gspca_dev);
  2123. /*
  2124. * Most subdrivers send all ctrl values on sd_start and thus
  2125. * only write to the device registers on s_ctrl when streaming ->
  2126. * Clear streaming to avoid setting all ctrls twice.
  2127. */
  2128. streaming = gspca_dev->streaming;
  2129. gspca_dev->streaming = 0;
  2130. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  2131. if (streaming)
  2132. ret = gspca_init_transfer(gspca_dev);
  2133. mutex_unlock(&gspca_dev->usb_lock);
  2134. return ret;
  2135. }
  2136. EXPORT_SYMBOL(gspca_resume);
  2137. #endif
  2138. /* -- module insert / remove -- */
  2139. static int __init gspca_init(void)
  2140. {
  2141. pr_info("v" GSPCA_VERSION " registered\n");
  2142. return 0;
  2143. }
  2144. static void __exit gspca_exit(void)
  2145. {
  2146. }
  2147. module_init(gspca_init);
  2148. module_exit(gspca_exit);
  2149. #ifdef GSPCA_DEBUG
  2150. module_param_named(debug, gspca_debug, int, 0644);
  2151. MODULE_PARM_DESC(debug,
  2152. "Debug (bit) 0x01:error 0x02:probe 0x04:config"
  2153. " 0x08:stream 0x10:frame 0x20:packet"
  2154. " 0x0100: v4l2");
  2155. #endif