gspca.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  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 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. atomic_set(&gspca_dev->fr_q, 0);
  1479. atomic_set(&gspca_dev->fr_i, 0);
  1480. gspca_dev->fr_o = 0;
  1481. ret = 0;
  1482. out:
  1483. mutex_unlock(&gspca_dev->queue_lock);
  1484. return ret;
  1485. }
  1486. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1487. struct v4l2_jpegcompression *jpegcomp)
  1488. {
  1489. struct gspca_dev *gspca_dev = video_drvdata(file);
  1490. if (!gspca_dev->sd_desc->get_jcomp)
  1491. return -ENOTTY;
  1492. gspca_dev->usb_err = 0;
  1493. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1494. }
  1495. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1496. struct v4l2_jpegcompression *jpegcomp)
  1497. {
  1498. struct gspca_dev *gspca_dev = video_drvdata(file);
  1499. if (!gspca_dev->sd_desc->set_jcomp)
  1500. return -ENOTTY;
  1501. gspca_dev->usb_err = 0;
  1502. return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1503. }
  1504. static int vidioc_g_parm(struct file *filp, void *priv,
  1505. struct v4l2_streamparm *parm)
  1506. {
  1507. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1508. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1509. if (gspca_dev->sd_desc->get_streamparm) {
  1510. gspca_dev->usb_err = 0;
  1511. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1512. return gspca_dev->usb_err;
  1513. }
  1514. return 0;
  1515. }
  1516. static int vidioc_s_parm(struct file *filp, void *priv,
  1517. struct v4l2_streamparm *parm)
  1518. {
  1519. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1520. int n;
  1521. n = parm->parm.capture.readbuffers;
  1522. if (n == 0 || n >= GSPCA_MAX_FRAMES)
  1523. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1524. else
  1525. gspca_dev->nbufread = n;
  1526. if (gspca_dev->sd_desc->set_streamparm) {
  1527. gspca_dev->usb_err = 0;
  1528. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1529. return gspca_dev->usb_err;
  1530. }
  1531. return 0;
  1532. }
  1533. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1534. {
  1535. struct gspca_dev *gspca_dev = video_drvdata(file);
  1536. struct gspca_frame *frame;
  1537. struct page *page;
  1538. unsigned long addr, start, size;
  1539. int i, ret;
  1540. start = vma->vm_start;
  1541. size = vma->vm_end - vma->vm_start;
  1542. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1543. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1544. return -ERESTARTSYS;
  1545. if (gspca_dev->capt_file != file) {
  1546. ret = -EINVAL;
  1547. goto out;
  1548. }
  1549. frame = NULL;
  1550. for (i = 0; i < gspca_dev->nframes; ++i) {
  1551. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1552. PDEBUG(D_STREAM, "mmap bad memory type");
  1553. break;
  1554. }
  1555. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1556. == vma->vm_pgoff) {
  1557. frame = &gspca_dev->frame[i];
  1558. break;
  1559. }
  1560. }
  1561. if (frame == NULL) {
  1562. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1563. ret = -EINVAL;
  1564. goto out;
  1565. }
  1566. if (size != frame->v4l2_buf.length) {
  1567. PDEBUG(D_STREAM, "mmap bad size");
  1568. ret = -EINVAL;
  1569. goto out;
  1570. }
  1571. /*
  1572. * - VM_IO marks the area as being a mmaped region for I/O to a
  1573. * device. It also prevents the region from being core dumped.
  1574. */
  1575. vma->vm_flags |= VM_IO;
  1576. addr = (unsigned long) frame->data;
  1577. while (size > 0) {
  1578. page = vmalloc_to_page((void *) addr);
  1579. ret = vm_insert_page(vma, start, page);
  1580. if (ret < 0)
  1581. goto out;
  1582. start += PAGE_SIZE;
  1583. addr += PAGE_SIZE;
  1584. size -= PAGE_SIZE;
  1585. }
  1586. vma->vm_ops = &gspca_vm_ops;
  1587. vma->vm_private_data = frame;
  1588. gspca_vm_open(vma);
  1589. ret = 0;
  1590. out:
  1591. mutex_unlock(&gspca_dev->queue_lock);
  1592. return ret;
  1593. }
  1594. static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
  1595. enum v4l2_memory memory)
  1596. {
  1597. if (!gspca_dev->present)
  1598. return -ENODEV;
  1599. if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
  1600. !gspca_dev->streaming)
  1601. return -EINVAL;
  1602. /* check if a frame is ready */
  1603. return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
  1604. }
  1605. static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
  1606. enum v4l2_memory memory)
  1607. {
  1608. int ret;
  1609. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1610. return -ERESTARTSYS;
  1611. ret = frame_ready_nolock(gspca_dev, file, memory);
  1612. mutex_unlock(&gspca_dev->queue_lock);
  1613. return ret;
  1614. }
  1615. /*
  1616. * dequeue a video buffer
  1617. *
  1618. * If nonblock_ing is false, block until a buffer is available.
  1619. */
  1620. static int vidioc_dqbuf(struct file *file, void *priv,
  1621. struct v4l2_buffer *v4l2_buf)
  1622. {
  1623. struct gspca_dev *gspca_dev = video_drvdata(file);
  1624. struct gspca_frame *frame;
  1625. int i, j, ret;
  1626. PDEBUG(D_FRAM, "dqbuf");
  1627. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1628. return -ERESTARTSYS;
  1629. for (;;) {
  1630. ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
  1631. if (ret < 0)
  1632. goto out;
  1633. if (ret > 0)
  1634. break;
  1635. mutex_unlock(&gspca_dev->queue_lock);
  1636. if (file->f_flags & O_NONBLOCK)
  1637. return -EAGAIN;
  1638. /* wait till a frame is ready */
  1639. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1640. frame_ready(gspca_dev, file, v4l2_buf->memory),
  1641. msecs_to_jiffies(3000));
  1642. if (ret < 0)
  1643. return ret;
  1644. if (ret == 0)
  1645. return -EIO;
  1646. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1647. return -ERESTARTSYS;
  1648. }
  1649. i = gspca_dev->fr_o;
  1650. j = gspca_dev->fr_queue[i];
  1651. frame = &gspca_dev->frame[j];
  1652. gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
  1653. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1654. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1655. PDEBUG(D_FRAM, "dqbuf %d", j);
  1656. ret = 0;
  1657. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1658. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1659. frame->data,
  1660. frame->v4l2_buf.bytesused)) {
  1661. PDEBUG(D_ERR|D_STREAM,
  1662. "dqbuf cp to user failed");
  1663. ret = -EFAULT;
  1664. }
  1665. }
  1666. out:
  1667. mutex_unlock(&gspca_dev->queue_lock);
  1668. if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
  1669. mutex_lock(&gspca_dev->usb_lock);
  1670. gspca_dev->usb_err = 0;
  1671. if (gspca_dev->present)
  1672. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1673. mutex_unlock(&gspca_dev->usb_lock);
  1674. }
  1675. return ret;
  1676. }
  1677. /*
  1678. * queue a video buffer
  1679. *
  1680. * Attempting to queue a buffer that has already been
  1681. * queued will return -EINVAL.
  1682. */
  1683. static int vidioc_qbuf(struct file *file, void *priv,
  1684. struct v4l2_buffer *v4l2_buf)
  1685. {
  1686. struct gspca_dev *gspca_dev = video_drvdata(file);
  1687. struct gspca_frame *frame;
  1688. int i, index, ret;
  1689. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1690. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1691. return -ERESTARTSYS;
  1692. index = v4l2_buf->index;
  1693. if ((unsigned) index >= gspca_dev->nframes) {
  1694. PDEBUG(D_FRAM,
  1695. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1696. ret = -EINVAL;
  1697. goto out;
  1698. }
  1699. if (v4l2_buf->memory != gspca_dev->memory) {
  1700. PDEBUG(D_FRAM, "qbuf bad memory type");
  1701. ret = -EINVAL;
  1702. goto out;
  1703. }
  1704. frame = &gspca_dev->frame[index];
  1705. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1706. PDEBUG(D_FRAM, "qbuf bad state");
  1707. ret = -EINVAL;
  1708. goto out;
  1709. }
  1710. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1711. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1712. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1713. frame->v4l2_buf.length = v4l2_buf->length;
  1714. }
  1715. /* put the buffer in the 'queued' queue */
  1716. i = atomic_read(&gspca_dev->fr_q);
  1717. gspca_dev->fr_queue[i] = index;
  1718. atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
  1719. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1720. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1721. ret = 0;
  1722. out:
  1723. mutex_unlock(&gspca_dev->queue_lock);
  1724. return ret;
  1725. }
  1726. /*
  1727. * allocate the resources for read()
  1728. */
  1729. static int read_alloc(struct gspca_dev *gspca_dev,
  1730. struct file *file)
  1731. {
  1732. struct v4l2_buffer v4l2_buf;
  1733. int i, ret;
  1734. PDEBUG(D_STREAM, "read alloc");
  1735. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1736. return -ERESTARTSYS;
  1737. if (gspca_dev->nframes == 0) {
  1738. struct v4l2_requestbuffers rb;
  1739. memset(&rb, 0, sizeof rb);
  1740. rb.count = gspca_dev->nbufread;
  1741. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1742. rb.memory = GSPCA_MEMORY_READ;
  1743. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1744. if (ret != 0) {
  1745. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1746. goto out;
  1747. }
  1748. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1749. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1750. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1751. for (i = 0; i < gspca_dev->nbufread; i++) {
  1752. v4l2_buf.index = i;
  1753. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1754. if (ret != 0) {
  1755. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1756. goto out;
  1757. }
  1758. }
  1759. }
  1760. /* start streaming */
  1761. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1762. if (ret != 0)
  1763. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1764. out:
  1765. mutex_unlock(&gspca_dev->usb_lock);
  1766. return ret;
  1767. }
  1768. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1769. {
  1770. struct gspca_dev *gspca_dev = video_drvdata(file);
  1771. unsigned long req_events = poll_requested_events(wait);
  1772. int ret = 0;
  1773. PDEBUG(D_FRAM, "poll");
  1774. if (req_events & POLLPRI)
  1775. ret |= v4l2_ctrl_poll(file, wait);
  1776. if (req_events & (POLLIN | POLLRDNORM)) {
  1777. /* if reqbufs is not done, the user would use read() */
  1778. if (gspca_dev->memory == GSPCA_MEMORY_NO) {
  1779. if (read_alloc(gspca_dev, file) != 0) {
  1780. ret |= POLLERR;
  1781. goto out;
  1782. }
  1783. }
  1784. poll_wait(file, &gspca_dev->wq, wait);
  1785. /* check if an image has been received */
  1786. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
  1787. ret |= POLLERR;
  1788. goto out;
  1789. }
  1790. if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
  1791. ret |= POLLIN | POLLRDNORM;
  1792. mutex_unlock(&gspca_dev->queue_lock);
  1793. }
  1794. out:
  1795. if (!gspca_dev->present)
  1796. ret |= POLLHUP;
  1797. return ret;
  1798. }
  1799. static ssize_t dev_read(struct file *file, char __user *data,
  1800. size_t count, loff_t *ppos)
  1801. {
  1802. struct gspca_dev *gspca_dev = video_drvdata(file);
  1803. struct gspca_frame *frame;
  1804. struct v4l2_buffer v4l2_buf;
  1805. struct timeval timestamp;
  1806. int n, ret, ret2;
  1807. PDEBUG(D_FRAM, "read (%zd)", count);
  1808. if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
  1809. ret = read_alloc(gspca_dev, file);
  1810. if (ret != 0)
  1811. return ret;
  1812. }
  1813. /* get a frame */
  1814. timestamp = ktime_to_timeval(ktime_get());
  1815. timestamp.tv_sec--;
  1816. n = 2;
  1817. for (;;) {
  1818. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1819. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1820. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1821. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1822. if (ret != 0) {
  1823. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1824. return ret;
  1825. }
  1826. /* if the process slept for more than 1 second,
  1827. * get a newer frame */
  1828. frame = &gspca_dev->frame[v4l2_buf.index];
  1829. if (--n < 0)
  1830. break; /* avoid infinite loop */
  1831. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1832. break;
  1833. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1834. if (ret != 0) {
  1835. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1836. return ret;
  1837. }
  1838. }
  1839. /* copy the frame */
  1840. if (count > frame->v4l2_buf.bytesused)
  1841. count = frame->v4l2_buf.bytesused;
  1842. ret = copy_to_user(data, frame->data, count);
  1843. if (ret != 0) {
  1844. PDEBUG(D_ERR|D_STREAM,
  1845. "read cp to user lack %d / %zd", ret, count);
  1846. ret = -EFAULT;
  1847. goto out;
  1848. }
  1849. ret = count;
  1850. out:
  1851. /* in each case, requeue the buffer */
  1852. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1853. if (ret2 != 0)
  1854. return ret2;
  1855. return ret;
  1856. }
  1857. static struct v4l2_file_operations dev_fops = {
  1858. .owner = THIS_MODULE,
  1859. .open = dev_open,
  1860. .release = dev_close,
  1861. .read = dev_read,
  1862. .mmap = dev_mmap,
  1863. .unlocked_ioctl = video_ioctl2,
  1864. .poll = dev_poll,
  1865. };
  1866. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1867. .vidioc_querycap = vidioc_querycap,
  1868. .vidioc_dqbuf = vidioc_dqbuf,
  1869. .vidioc_qbuf = vidioc_qbuf,
  1870. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1871. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1872. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1873. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1874. .vidioc_streamon = vidioc_streamon,
  1875. .vidioc_queryctrl = vidioc_queryctrl,
  1876. .vidioc_g_ctrl = vidioc_g_ctrl,
  1877. .vidioc_s_ctrl = vidioc_s_ctrl,
  1878. .vidioc_querymenu = vidioc_querymenu,
  1879. .vidioc_enum_input = vidioc_enum_input,
  1880. .vidioc_g_input = vidioc_g_input,
  1881. .vidioc_s_input = vidioc_s_input,
  1882. .vidioc_reqbufs = vidioc_reqbufs,
  1883. .vidioc_querybuf = vidioc_querybuf,
  1884. .vidioc_streamoff = vidioc_streamoff,
  1885. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1886. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1887. .vidioc_g_parm = vidioc_g_parm,
  1888. .vidioc_s_parm = vidioc_s_parm,
  1889. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1890. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1891. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1892. .vidioc_g_register = vidioc_g_register,
  1893. .vidioc_s_register = vidioc_s_register,
  1894. #endif
  1895. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1896. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1897. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1898. };
  1899. static const struct video_device gspca_template = {
  1900. .name = "gspca main driver",
  1901. .fops = &dev_fops,
  1902. .ioctl_ops = &dev_ioctl_ops,
  1903. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1904. };
  1905. /* initialize the controls */
  1906. static void ctrls_init(struct gspca_dev *gspca_dev)
  1907. {
  1908. struct gspca_ctrl *ctrl;
  1909. int i;
  1910. for (i = 0, ctrl = gspca_dev->cam.ctrls;
  1911. i < gspca_dev->sd_desc->nctrls;
  1912. i++, ctrl++) {
  1913. ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
  1914. ctrl->val = ctrl->def;
  1915. ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
  1916. ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
  1917. }
  1918. }
  1919. /*
  1920. * probe and create a new gspca device
  1921. *
  1922. * This function must be called by the sub-driver when it is
  1923. * called for probing a new device.
  1924. */
  1925. int gspca_dev_probe2(struct usb_interface *intf,
  1926. const struct usb_device_id *id,
  1927. const struct sd_desc *sd_desc,
  1928. int dev_size,
  1929. struct module *module)
  1930. {
  1931. struct gspca_dev *gspca_dev;
  1932. struct usb_device *dev = interface_to_usbdev(intf);
  1933. int ret;
  1934. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1935. sd_desc->name, id->idVendor, id->idProduct);
  1936. /* create the device */
  1937. if (dev_size < sizeof *gspca_dev)
  1938. dev_size = sizeof *gspca_dev;
  1939. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1940. if (!gspca_dev) {
  1941. pr_err("couldn't kzalloc gspca struct\n");
  1942. return -ENOMEM;
  1943. }
  1944. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1945. if (!gspca_dev->usb_buf) {
  1946. pr_err("out of memory\n");
  1947. ret = -ENOMEM;
  1948. goto out;
  1949. }
  1950. gspca_dev->dev = dev;
  1951. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1952. /* check if any audio device */
  1953. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1954. int i;
  1955. struct usb_interface *intf2;
  1956. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1957. intf2 = dev->actconfig->interface[i];
  1958. if (intf2 != NULL
  1959. && intf2->altsetting != NULL
  1960. && intf2->altsetting->desc.bInterfaceClass ==
  1961. USB_CLASS_AUDIO) {
  1962. gspca_dev->audio = 1;
  1963. break;
  1964. }
  1965. }
  1966. }
  1967. gspca_dev->v4l2_dev.release = gspca_release;
  1968. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1969. if (ret)
  1970. goto out;
  1971. gspca_dev->sd_desc = sd_desc;
  1972. gspca_dev->nbufread = 2;
  1973. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1974. gspca_dev->vdev = gspca_template;
  1975. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1976. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1977. set_bit(V4L2_FL_USE_FH_PRIO, &gspca_dev->vdev.flags);
  1978. gspca_dev->module = module;
  1979. gspca_dev->present = 1;
  1980. mutex_init(&gspca_dev->usb_lock);
  1981. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1982. mutex_init(&gspca_dev->queue_lock);
  1983. init_waitqueue_head(&gspca_dev->wq);
  1984. /* configure the subdriver and initialize the USB device */
  1985. ret = sd_desc->config(gspca_dev, id);
  1986. if (ret < 0)
  1987. goto out;
  1988. if (gspca_dev->cam.ctrls != NULL)
  1989. ctrls_init(gspca_dev);
  1990. ret = sd_desc->init(gspca_dev);
  1991. if (ret < 0)
  1992. goto out;
  1993. if (sd_desc->init_controls)
  1994. ret = sd_desc->init_controls(gspca_dev);
  1995. if (ret < 0)
  1996. goto out;
  1997. gspca_set_default_mode(gspca_dev);
  1998. ret = gspca_input_connect(gspca_dev);
  1999. if (ret)
  2000. goto out;
  2001. /*
  2002. * Don't take usb_lock for these ioctls. This improves latency if
  2003. * usb_lock is taken for a long time, e.g. when changing a control
  2004. * value, and a new frame is ready to be dequeued.
  2005. */
  2006. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
  2007. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
  2008. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
  2009. /* init video stuff */
  2010. ret = video_register_device(&gspca_dev->vdev,
  2011. VFL_TYPE_GRABBER,
  2012. -1);
  2013. if (ret < 0) {
  2014. pr_err("video_register_device err %d\n", ret);
  2015. goto out;
  2016. }
  2017. usb_set_intfdata(intf, gspca_dev);
  2018. PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
  2019. gspca_input_create_urb(gspca_dev);
  2020. return 0;
  2021. out:
  2022. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2023. if (gspca_dev->input_dev)
  2024. input_unregister_device(gspca_dev->input_dev);
  2025. #endif
  2026. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  2027. kfree(gspca_dev->usb_buf);
  2028. kfree(gspca_dev);
  2029. return ret;
  2030. }
  2031. EXPORT_SYMBOL(gspca_dev_probe2);
  2032. /* same function as the previous one, but check the interface */
  2033. int gspca_dev_probe(struct usb_interface *intf,
  2034. const struct usb_device_id *id,
  2035. const struct sd_desc *sd_desc,
  2036. int dev_size,
  2037. struct module *module)
  2038. {
  2039. struct usb_device *dev = interface_to_usbdev(intf);
  2040. /* we don't handle multi-config cameras */
  2041. if (dev->descriptor.bNumConfigurations != 1) {
  2042. pr_err("%04x:%04x too many config\n",
  2043. id->idVendor, id->idProduct);
  2044. return -ENODEV;
  2045. }
  2046. /* the USB video interface must be the first one */
  2047. if (dev->actconfig->desc.bNumInterfaces != 1
  2048. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  2049. return -ENODEV;
  2050. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  2051. }
  2052. EXPORT_SYMBOL(gspca_dev_probe);
  2053. /*
  2054. * USB disconnection
  2055. *
  2056. * This function must be called by the sub-driver
  2057. * when the device disconnects, after the specific resources are freed.
  2058. */
  2059. void gspca_disconnect(struct usb_interface *intf)
  2060. {
  2061. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2062. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2063. struct input_dev *input_dev;
  2064. #endif
  2065. PDEBUG(D_PROBE, "%s disconnect",
  2066. video_device_node_name(&gspca_dev->vdev));
  2067. mutex_lock(&gspca_dev->usb_lock);
  2068. usb_set_intfdata(intf, NULL);
  2069. gspca_dev->dev = NULL;
  2070. gspca_dev->present = 0;
  2071. destroy_urbs(gspca_dev);
  2072. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2073. gspca_input_destroy_urb(gspca_dev);
  2074. input_dev = gspca_dev->input_dev;
  2075. if (input_dev) {
  2076. gspca_dev->input_dev = NULL;
  2077. input_unregister_device(input_dev);
  2078. }
  2079. #endif
  2080. /* Free subdriver's streaming resources / stop sd workqueue(s) */
  2081. if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
  2082. gspca_dev->sd_desc->stop0(gspca_dev);
  2083. gspca_dev->streaming = 0;
  2084. wake_up_interruptible(&gspca_dev->wq);
  2085. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  2086. video_unregister_device(&gspca_dev->vdev);
  2087. mutex_unlock(&gspca_dev->usb_lock);
  2088. /* (this will call gspca_release() immediately or on last close) */
  2089. v4l2_device_put(&gspca_dev->v4l2_dev);
  2090. }
  2091. EXPORT_SYMBOL(gspca_disconnect);
  2092. #ifdef CONFIG_PM
  2093. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  2094. {
  2095. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2096. if (!gspca_dev->streaming)
  2097. return 0;
  2098. mutex_lock(&gspca_dev->usb_lock);
  2099. gspca_dev->frozen = 1; /* avoid urb error messages */
  2100. gspca_dev->usb_err = 0;
  2101. if (gspca_dev->sd_desc->stopN)
  2102. gspca_dev->sd_desc->stopN(gspca_dev);
  2103. destroy_urbs(gspca_dev);
  2104. gspca_input_destroy_urb(gspca_dev);
  2105. gspca_set_alt0(gspca_dev);
  2106. if (gspca_dev->sd_desc->stop0)
  2107. gspca_dev->sd_desc->stop0(gspca_dev);
  2108. mutex_unlock(&gspca_dev->usb_lock);
  2109. return 0;
  2110. }
  2111. EXPORT_SYMBOL(gspca_suspend);
  2112. int gspca_resume(struct usb_interface *intf)
  2113. {
  2114. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2115. int streaming, ret = 0;
  2116. mutex_lock(&gspca_dev->usb_lock);
  2117. gspca_dev->frozen = 0;
  2118. gspca_dev->usb_err = 0;
  2119. gspca_dev->sd_desc->init(gspca_dev);
  2120. gspca_input_create_urb(gspca_dev);
  2121. /*
  2122. * Most subdrivers send all ctrl values on sd_start and thus
  2123. * only write to the device registers on s_ctrl when streaming ->
  2124. * Clear streaming to avoid setting all ctrls twice.
  2125. */
  2126. streaming = gspca_dev->streaming;
  2127. gspca_dev->streaming = 0;
  2128. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  2129. if (streaming)
  2130. ret = gspca_init_transfer(gspca_dev);
  2131. mutex_unlock(&gspca_dev->usb_lock);
  2132. return ret;
  2133. }
  2134. EXPORT_SYMBOL(gspca_resume);
  2135. #endif
  2136. /* -- module insert / remove -- */
  2137. static int __init gspca_init(void)
  2138. {
  2139. pr_info("v" GSPCA_VERSION " registered\n");
  2140. return 0;
  2141. }
  2142. static void __exit gspca_exit(void)
  2143. {
  2144. }
  2145. module_init(gspca_init);
  2146. module_exit(gspca_exit);
  2147. #ifdef GSPCA_DEBUG
  2148. module_param_named(debug, gspca_debug, int, 0644);
  2149. MODULE_PARM_DESC(debug,
  2150. "Debug (bit) 0x01:error 0x02:probe 0x04:config"
  2151. " 0x08:stream 0x10:frame 0x20:packet"
  2152. " 0x0100: v4l2");
  2153. #endif