stk-webcam.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. /*
  2. * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
  3. *
  4. * Copyright (C) 2006 Nicolas VIVIEN
  5. * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
  6. *
  7. * Some parts are inspired from cafe_ccic.c
  8. * Copyright 2006-2007 Jonathan Corbet
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/slab.h>
  29. #include <linux/dmi.h>
  30. #include <linux/usb.h>
  31. #include <linux/mm.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/videodev2.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include "stk-webcam.h"
  37. static int hflip = -1;
  38. module_param(hflip, int, 0444);
  39. MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 0");
  40. static int vflip = -1;
  41. module_param(vflip, int, 0444);
  42. MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 0");
  43. static int debug;
  44. module_param(debug, int, 0444);
  45. MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
  46. MODULE_LICENSE("GPL");
  47. MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
  48. MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
  49. /* Some cameras have audio interfaces, we aren't interested in those */
  50. static struct usb_device_id stkwebcam_table[] = {
  51. { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
  52. { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
  53. { }
  54. };
  55. MODULE_DEVICE_TABLE(usb, stkwebcam_table);
  56. /*
  57. * The stk webcam laptop module is mounted upside down in some laptops :(
  58. *
  59. * Some background information (thanks to Hans de Goede for providing this):
  60. *
  61. * 1) Once upon a time the stkwebcam driver was written
  62. *
  63. * 2) The webcam in question was used mostly in Asus laptop models, including
  64. * the laptop of the original author of the driver, and in these models, in
  65. * typical Asus fashion (see the long long list for uvc cams inside v4l-utils),
  66. * they mounted the webcam-module the wrong way up. So the hflip and vflip
  67. * module options were given a default value of 1 (the correct value for
  68. * upside down mounted models)
  69. *
  70. * 3) Years later I got a bug report from a user with a laptop with stkwebcam,
  71. * where the module was actually mounted the right way up, and thus showed
  72. * upside down under Linux. So now I was facing the choice of 2 options:
  73. *
  74. * a) Add a not-upside-down list to stkwebcam, which overrules the default.
  75. *
  76. * b) Do it like all the other drivers do, and make the default right for
  77. * cams mounted the proper way and add an upside-down model list, with
  78. * models where we need to flip-by-default.
  79. *
  80. * Despite knowing that going b) would cause a period of pain where we were
  81. * building the table I opted to go for option b), since a) is just too ugly,
  82. * and worse different from how every other driver does it leading to
  83. * confusion in the long run. This change was made in kernel 3.6.
  84. *
  85. * So for any user report about upside-down images since kernel 3.6 ask them
  86. * to provide the output of 'sudo dmidecode' so the laptop can be added in
  87. * the table below.
  88. */
  89. static const struct dmi_system_id stk_upside_down_dmi_table[] = {
  90. {
  91. .ident = "ASUS G1",
  92. .matches = {
  93. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  94. DMI_MATCH(DMI_PRODUCT_NAME, "G1")
  95. }
  96. }, {
  97. .ident = "ASUS F3JC",
  98. .matches = {
  99. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  100. DMI_MATCH(DMI_PRODUCT_NAME, "F3JC")
  101. }
  102. },
  103. {}
  104. };
  105. /*
  106. * Basic stuff
  107. */
  108. int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
  109. {
  110. struct usb_device *udev = dev->udev;
  111. int ret;
  112. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  113. 0x01,
  114. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  115. value,
  116. index,
  117. NULL,
  118. 0,
  119. 500);
  120. if (ret < 0)
  121. return ret;
  122. else
  123. return 0;
  124. }
  125. int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
  126. {
  127. struct usb_device *udev = dev->udev;
  128. int ret;
  129. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  130. 0x00,
  131. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  132. 0x00,
  133. index,
  134. (u8 *) value,
  135. sizeof(u8),
  136. 500);
  137. if (ret < 0)
  138. return ret;
  139. else
  140. return 0;
  141. }
  142. static int stk_start_stream(struct stk_camera *dev)
  143. {
  144. int value;
  145. int i, ret;
  146. int value_116, value_117;
  147. if (!is_present(dev))
  148. return -ENODEV;
  149. if (!is_memallocd(dev) || !is_initialised(dev)) {
  150. STK_ERROR("FIXME: Buffers are not allocated\n");
  151. return -EFAULT;
  152. }
  153. ret = usb_set_interface(dev->udev, 0, 5);
  154. if (ret < 0)
  155. STK_ERROR("usb_set_interface failed !\n");
  156. if (stk_sensor_wakeup(dev))
  157. STK_ERROR("error awaking the sensor\n");
  158. stk_camera_read_reg(dev, 0x0116, &value_116);
  159. stk_camera_read_reg(dev, 0x0117, &value_117);
  160. stk_camera_write_reg(dev, 0x0116, 0x0000);
  161. stk_camera_write_reg(dev, 0x0117, 0x0000);
  162. stk_camera_read_reg(dev, 0x0100, &value);
  163. stk_camera_write_reg(dev, 0x0100, value | 0x80);
  164. stk_camera_write_reg(dev, 0x0116, value_116);
  165. stk_camera_write_reg(dev, 0x0117, value_117);
  166. for (i = 0; i < MAX_ISO_BUFS; i++) {
  167. if (dev->isobufs[i].urb) {
  168. ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
  169. atomic_inc(&dev->urbs_used);
  170. if (ret)
  171. return ret;
  172. }
  173. }
  174. set_streaming(dev);
  175. return 0;
  176. }
  177. static int stk_stop_stream(struct stk_camera *dev)
  178. {
  179. int value;
  180. int i;
  181. if (is_present(dev)) {
  182. stk_camera_read_reg(dev, 0x0100, &value);
  183. stk_camera_write_reg(dev, 0x0100, value & ~0x80);
  184. if (dev->isobufs != NULL) {
  185. for (i = 0; i < MAX_ISO_BUFS; i++) {
  186. if (dev->isobufs[i].urb)
  187. usb_kill_urb(dev->isobufs[i].urb);
  188. }
  189. }
  190. unset_streaming(dev);
  191. if (usb_set_interface(dev->udev, 0, 0))
  192. STK_ERROR("usb_set_interface failed !\n");
  193. if (stk_sensor_sleep(dev))
  194. STK_ERROR("error suspending the sensor\n");
  195. }
  196. return 0;
  197. }
  198. /*
  199. * This seems to be the shortest init sequence we
  200. * must do in order to find the sensor
  201. * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
  202. * is also reset. Maybe powers down it?
  203. * Rest of values don't make a difference
  204. */
  205. static struct regval stk1125_initvals[] = {
  206. /*TODO: What means this sequence? */
  207. {0x0000, 0x24},
  208. {0x0100, 0x21},
  209. {0x0002, 0x68},
  210. {0x0003, 0x80},
  211. {0x0005, 0x00},
  212. {0x0007, 0x03},
  213. {0x000d, 0x00},
  214. {0x000f, 0x02},
  215. {0x0300, 0x12},
  216. {0x0350, 0x41},
  217. {0x0351, 0x00},
  218. {0x0352, 0x00},
  219. {0x0353, 0x00},
  220. {0x0018, 0x10},
  221. {0x0019, 0x00},
  222. {0x001b, 0x0e},
  223. {0x001c, 0x46},
  224. {0x0300, 0x80},
  225. {0x001a, 0x04},
  226. {0x0110, 0x00},
  227. {0x0111, 0x00},
  228. {0x0112, 0x00},
  229. {0x0113, 0x00},
  230. {0xffff, 0xff},
  231. };
  232. static int stk_initialise(struct stk_camera *dev)
  233. {
  234. struct regval *rv;
  235. int ret;
  236. if (!is_present(dev))
  237. return -ENODEV;
  238. if (is_initialised(dev))
  239. return 0;
  240. rv = stk1125_initvals;
  241. while (rv->reg != 0xffff) {
  242. ret = stk_camera_write_reg(dev, rv->reg, rv->val);
  243. if (ret)
  244. return ret;
  245. rv++;
  246. }
  247. if (stk_sensor_init(dev) == 0) {
  248. set_initialised(dev);
  249. return 0;
  250. } else
  251. return -1;
  252. }
  253. /* *********************************************** */
  254. /*
  255. * This function is called as an URB transfert is complete (Isochronous pipe).
  256. * So, the traitement is done in interrupt time, so it has be fast, not crash,
  257. * and not stall. Neat.
  258. */
  259. static void stk_isoc_handler(struct urb *urb)
  260. {
  261. int i;
  262. int ret;
  263. int framelen;
  264. unsigned long flags;
  265. unsigned char *fill = NULL;
  266. unsigned char *iso_buf = NULL;
  267. struct stk_camera *dev;
  268. struct stk_sio_buffer *fb;
  269. dev = (struct stk_camera *) urb->context;
  270. if (dev == NULL) {
  271. STK_ERROR("isoc_handler called with NULL device !\n");
  272. return;
  273. }
  274. if (urb->status == -ENOENT || urb->status == -ECONNRESET
  275. || urb->status == -ESHUTDOWN) {
  276. atomic_dec(&dev->urbs_used);
  277. return;
  278. }
  279. spin_lock_irqsave(&dev->spinlock, flags);
  280. if (urb->status != -EINPROGRESS && urb->status != 0) {
  281. STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
  282. goto resubmit;
  283. }
  284. if (list_empty(&dev->sio_avail)) {
  285. /*FIXME Stop streaming after a while */
  286. (void) (printk_ratelimit() &&
  287. STK_ERROR("isoc_handler without available buffer!\n"));
  288. goto resubmit;
  289. }
  290. fb = list_first_entry(&dev->sio_avail,
  291. struct stk_sio_buffer, list);
  292. fill = fb->buffer + fb->v4lbuf.bytesused;
  293. for (i = 0; i < urb->number_of_packets; i++) {
  294. if (urb->iso_frame_desc[i].status != 0) {
  295. if (urb->iso_frame_desc[i].status != -EXDEV)
  296. STK_ERROR("Frame %d has error %d\n", i,
  297. urb->iso_frame_desc[i].status);
  298. continue;
  299. }
  300. framelen = urb->iso_frame_desc[i].actual_length;
  301. iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  302. if (framelen <= 4)
  303. continue; /* no data */
  304. /*
  305. * we found something informational from there
  306. * the isoc frames have to type of headers
  307. * type1: 00 xx 00 00 or 20 xx 00 00
  308. * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
  309. * xx is a sequencer which has never been seen over 0x3f
  310. * imho data written down looks like bayer, i see similarities
  311. * after every 640 bytes
  312. */
  313. if (*iso_buf & 0x80) {
  314. framelen -= 8;
  315. iso_buf += 8;
  316. /* This marks a new frame */
  317. if (fb->v4lbuf.bytesused != 0
  318. && fb->v4lbuf.bytesused != dev->frame_size) {
  319. (void) (printk_ratelimit() &&
  320. STK_ERROR("frame %d, "
  321. "bytesused=%d, skipping\n",
  322. i, fb->v4lbuf.bytesused));
  323. fb->v4lbuf.bytesused = 0;
  324. fill = fb->buffer;
  325. } else if (fb->v4lbuf.bytesused == dev->frame_size) {
  326. if (list_is_singular(&dev->sio_avail)) {
  327. /* Always reuse the last buffer */
  328. fb->v4lbuf.bytesused = 0;
  329. fill = fb->buffer;
  330. } else {
  331. list_move_tail(dev->sio_avail.next,
  332. &dev->sio_full);
  333. wake_up(&dev->wait_frame);
  334. fb = list_first_entry(&dev->sio_avail,
  335. struct stk_sio_buffer, list);
  336. fb->v4lbuf.bytesused = 0;
  337. fill = fb->buffer;
  338. }
  339. }
  340. } else {
  341. framelen -= 4;
  342. iso_buf += 4;
  343. }
  344. /* Our buffer is full !!! */
  345. if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
  346. (void) (printk_ratelimit() &&
  347. STK_ERROR("Frame buffer overflow, lost sync\n"));
  348. /*FIXME Do something here? */
  349. continue;
  350. }
  351. spin_unlock_irqrestore(&dev->spinlock, flags);
  352. memcpy(fill, iso_buf, framelen);
  353. spin_lock_irqsave(&dev->spinlock, flags);
  354. fill += framelen;
  355. /* New size of our buffer */
  356. fb->v4lbuf.bytesused += framelen;
  357. }
  358. resubmit:
  359. spin_unlock_irqrestore(&dev->spinlock, flags);
  360. urb->dev = dev->udev;
  361. ret = usb_submit_urb(urb, GFP_ATOMIC);
  362. if (ret != 0) {
  363. STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
  364. ret);
  365. }
  366. }
  367. /* -------------------------------------------- */
  368. static int stk_prepare_iso(struct stk_camera *dev)
  369. {
  370. void *kbuf;
  371. int i, j;
  372. struct urb *urb;
  373. struct usb_device *udev;
  374. if (dev == NULL)
  375. return -ENXIO;
  376. udev = dev->udev;
  377. if (dev->isobufs)
  378. STK_ERROR("isobufs already allocated. Bad\n");
  379. else
  380. dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
  381. GFP_KERNEL);
  382. if (dev->isobufs == NULL) {
  383. STK_ERROR("Unable to allocate iso buffers\n");
  384. return -ENOMEM;
  385. }
  386. for (i = 0; i < MAX_ISO_BUFS; i++) {
  387. if (dev->isobufs[i].data == NULL) {
  388. kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  389. if (kbuf == NULL) {
  390. STK_ERROR("Failed to allocate iso buffer %d\n",
  391. i);
  392. goto isobufs_out;
  393. }
  394. dev->isobufs[i].data = kbuf;
  395. } else
  396. STK_ERROR("isobuf data already allocated\n");
  397. if (dev->isobufs[i].urb == NULL) {
  398. urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
  399. if (urb == NULL) {
  400. STK_ERROR("Failed to allocate URB %d\n", i);
  401. goto isobufs_out;
  402. }
  403. dev->isobufs[i].urb = urb;
  404. } else {
  405. STK_ERROR("Killing URB\n");
  406. usb_kill_urb(dev->isobufs[i].urb);
  407. urb = dev->isobufs[i].urb;
  408. }
  409. urb->interval = 1;
  410. urb->dev = udev;
  411. urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
  412. urb->transfer_flags = URB_ISO_ASAP;
  413. urb->transfer_buffer = dev->isobufs[i].data;
  414. urb->transfer_buffer_length = ISO_BUFFER_SIZE;
  415. urb->complete = stk_isoc_handler;
  416. urb->context = dev;
  417. urb->start_frame = 0;
  418. urb->number_of_packets = ISO_FRAMES_PER_DESC;
  419. for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
  420. urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
  421. urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
  422. }
  423. }
  424. set_memallocd(dev);
  425. return 0;
  426. isobufs_out:
  427. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
  428. kfree(dev->isobufs[i].data);
  429. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
  430. usb_free_urb(dev->isobufs[i].urb);
  431. kfree(dev->isobufs);
  432. dev->isobufs = NULL;
  433. return -ENOMEM;
  434. }
  435. static void stk_clean_iso(struct stk_camera *dev)
  436. {
  437. int i;
  438. if (dev == NULL || dev->isobufs == NULL)
  439. return;
  440. for (i = 0; i < MAX_ISO_BUFS; i++) {
  441. struct urb *urb;
  442. urb = dev->isobufs[i].urb;
  443. if (urb) {
  444. if (atomic_read(&dev->urbs_used) && is_present(dev))
  445. usb_kill_urb(urb);
  446. usb_free_urb(urb);
  447. }
  448. kfree(dev->isobufs[i].data);
  449. }
  450. kfree(dev->isobufs);
  451. dev->isobufs = NULL;
  452. unset_memallocd(dev);
  453. }
  454. static int stk_setup_siobuf(struct stk_camera *dev, int index)
  455. {
  456. struct stk_sio_buffer *buf = dev->sio_bufs + index;
  457. INIT_LIST_HEAD(&buf->list);
  458. buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
  459. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  460. if (buf->buffer == NULL)
  461. return -ENOMEM;
  462. buf->mapcount = 0;
  463. buf->dev = dev;
  464. buf->v4lbuf.index = index;
  465. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  466. buf->v4lbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  467. buf->v4lbuf.field = V4L2_FIELD_NONE;
  468. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  469. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  470. return 0;
  471. }
  472. static int stk_free_sio_buffers(struct stk_camera *dev)
  473. {
  474. int i;
  475. int nbufs;
  476. unsigned long flags;
  477. if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
  478. return 0;
  479. /*
  480. * If any buffers are mapped, we cannot free them at all.
  481. */
  482. for (i = 0; i < dev->n_sbufs; i++) {
  483. if (dev->sio_bufs[i].mapcount > 0)
  484. return -EBUSY;
  485. }
  486. /*
  487. * OK, let's do it.
  488. */
  489. spin_lock_irqsave(&dev->spinlock, flags);
  490. INIT_LIST_HEAD(&dev->sio_avail);
  491. INIT_LIST_HEAD(&dev->sio_full);
  492. nbufs = dev->n_sbufs;
  493. dev->n_sbufs = 0;
  494. spin_unlock_irqrestore(&dev->spinlock, flags);
  495. for (i = 0; i < nbufs; i++) {
  496. if (dev->sio_bufs[i].buffer != NULL)
  497. vfree(dev->sio_bufs[i].buffer);
  498. }
  499. kfree(dev->sio_bufs);
  500. dev->sio_bufs = NULL;
  501. return 0;
  502. }
  503. static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
  504. {
  505. int i;
  506. if (dev->sio_bufs != NULL)
  507. STK_ERROR("sio_bufs already allocated\n");
  508. else {
  509. dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
  510. GFP_KERNEL);
  511. if (dev->sio_bufs == NULL)
  512. return -ENOMEM;
  513. for (i = 0; i < n_sbufs; i++) {
  514. if (stk_setup_siobuf(dev, i))
  515. return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
  516. dev->n_sbufs = i+1;
  517. }
  518. }
  519. return 0;
  520. }
  521. static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
  522. {
  523. int err;
  524. err = stk_prepare_iso(dev);
  525. if (err) {
  526. stk_clean_iso(dev);
  527. return err;
  528. }
  529. err = stk_prepare_sio_buffers(dev, n_sbufs);
  530. if (err) {
  531. stk_free_sio_buffers(dev);
  532. return err;
  533. }
  534. return 0;
  535. }
  536. static void stk_free_buffers(struct stk_camera *dev)
  537. {
  538. stk_clean_iso(dev);
  539. stk_free_sio_buffers(dev);
  540. }
  541. /* -------------------------------------------- */
  542. /* v4l file operations */
  543. static int v4l_stk_open(struct file *fp)
  544. {
  545. static int first_init = 1; /* webcam LED management */
  546. struct stk_camera *dev = video_drvdata(fp);
  547. if (dev == NULL || !is_present(dev))
  548. return -ENXIO;
  549. if (!first_init)
  550. stk_camera_write_reg(dev, 0x0, 0x24);
  551. else
  552. first_init = 0;
  553. usb_autopm_get_interface(dev->interface);
  554. return 0;
  555. }
  556. static int v4l_stk_release(struct file *fp)
  557. {
  558. struct stk_camera *dev = video_drvdata(fp);
  559. if (dev->owner == fp) {
  560. stk_stop_stream(dev);
  561. stk_free_buffers(dev);
  562. stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
  563. unset_initialised(dev);
  564. dev->owner = NULL;
  565. }
  566. if (is_present(dev))
  567. usb_autopm_put_interface(dev->interface);
  568. return 0;
  569. }
  570. static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
  571. size_t count, loff_t *f_pos)
  572. {
  573. int i;
  574. int ret;
  575. unsigned long flags;
  576. struct stk_sio_buffer *sbuf;
  577. struct stk_camera *dev = video_drvdata(fp);
  578. if (!is_present(dev))
  579. return -EIO;
  580. if (dev->owner && dev->owner != fp)
  581. return -EBUSY;
  582. dev->owner = fp;
  583. if (!is_streaming(dev)) {
  584. if (stk_initialise(dev)
  585. || stk_allocate_buffers(dev, 3)
  586. || stk_start_stream(dev))
  587. return -ENOMEM;
  588. spin_lock_irqsave(&dev->spinlock, flags);
  589. for (i = 0; i < dev->n_sbufs; i++) {
  590. list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
  591. dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
  592. }
  593. spin_unlock_irqrestore(&dev->spinlock, flags);
  594. }
  595. if (*f_pos == 0) {
  596. if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  597. return -EWOULDBLOCK;
  598. ret = wait_event_interruptible(dev->wait_frame,
  599. !list_empty(&dev->sio_full) || !is_present(dev));
  600. if (ret)
  601. return ret;
  602. if (!is_present(dev))
  603. return -EIO;
  604. }
  605. if (count + *f_pos > dev->frame_size)
  606. count = dev->frame_size - *f_pos;
  607. spin_lock_irqsave(&dev->spinlock, flags);
  608. if (list_empty(&dev->sio_full)) {
  609. spin_unlock_irqrestore(&dev->spinlock, flags);
  610. STK_ERROR("BUG: No siobufs ready\n");
  611. return 0;
  612. }
  613. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  614. spin_unlock_irqrestore(&dev->spinlock, flags);
  615. if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
  616. return -EFAULT;
  617. *f_pos += count;
  618. if (*f_pos >= dev->frame_size) {
  619. *f_pos = 0;
  620. spin_lock_irqsave(&dev->spinlock, flags);
  621. list_move_tail(&sbuf->list, &dev->sio_avail);
  622. spin_unlock_irqrestore(&dev->spinlock, flags);
  623. }
  624. return count;
  625. }
  626. static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
  627. {
  628. struct stk_camera *dev = video_drvdata(fp);
  629. poll_wait(fp, &dev->wait_frame, wait);
  630. if (!is_present(dev))
  631. return POLLERR;
  632. if (!list_empty(&dev->sio_full))
  633. return POLLIN | POLLRDNORM;
  634. return 0;
  635. }
  636. static void stk_v4l_vm_open(struct vm_area_struct *vma)
  637. {
  638. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  639. sbuf->mapcount++;
  640. }
  641. static void stk_v4l_vm_close(struct vm_area_struct *vma)
  642. {
  643. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  644. sbuf->mapcount--;
  645. if (sbuf->mapcount == 0)
  646. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  647. }
  648. static const struct vm_operations_struct stk_v4l_vm_ops = {
  649. .open = stk_v4l_vm_open,
  650. .close = stk_v4l_vm_close
  651. };
  652. static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
  653. {
  654. unsigned int i;
  655. int ret;
  656. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  657. struct stk_camera *dev = video_drvdata(fp);
  658. struct stk_sio_buffer *sbuf = NULL;
  659. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
  660. return -EINVAL;
  661. for (i = 0; i < dev->n_sbufs; i++) {
  662. if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
  663. sbuf = dev->sio_bufs + i;
  664. break;
  665. }
  666. }
  667. if (sbuf == NULL)
  668. return -EINVAL;
  669. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  670. if (ret)
  671. return ret;
  672. vma->vm_flags |= VM_DONTEXPAND;
  673. vma->vm_private_data = sbuf;
  674. vma->vm_ops = &stk_v4l_vm_ops;
  675. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  676. stk_v4l_vm_open(vma);
  677. return 0;
  678. }
  679. /* v4l ioctl handlers */
  680. static int stk_vidioc_querycap(struct file *filp,
  681. void *priv, struct v4l2_capability *cap)
  682. {
  683. strcpy(cap->driver, "stk");
  684. strcpy(cap->card, "stk");
  685. cap->version = DRIVER_VERSION_NUM;
  686. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  687. | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  688. return 0;
  689. }
  690. static int stk_vidioc_enum_input(struct file *filp,
  691. void *priv, struct v4l2_input *input)
  692. {
  693. if (input->index != 0)
  694. return -EINVAL;
  695. strcpy(input->name, "Syntek USB Camera");
  696. input->type = V4L2_INPUT_TYPE_CAMERA;
  697. return 0;
  698. }
  699. static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  700. {
  701. *i = 0;
  702. return 0;
  703. }
  704. static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  705. {
  706. if (i != 0)
  707. return -EINVAL;
  708. else
  709. return 0;
  710. }
  711. static int stk_s_ctrl(struct v4l2_ctrl *ctrl)
  712. {
  713. struct stk_camera *dev =
  714. container_of(ctrl->handler, struct stk_camera, hdl);
  715. switch (ctrl->id) {
  716. case V4L2_CID_BRIGHTNESS:
  717. return stk_sensor_set_brightness(dev, ctrl->val);
  718. case V4L2_CID_HFLIP:
  719. if (dmi_check_system(stk_upside_down_dmi_table))
  720. dev->vsettings.hflip = !ctrl->val;
  721. else
  722. dev->vsettings.hflip = ctrl->val;
  723. return 0;
  724. case V4L2_CID_VFLIP:
  725. if (dmi_check_system(stk_upside_down_dmi_table))
  726. dev->vsettings.vflip = !ctrl->val;
  727. else
  728. dev->vsettings.vflip = ctrl->val;
  729. return 0;
  730. default:
  731. return -EINVAL;
  732. }
  733. return 0;
  734. }
  735. static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
  736. void *priv, struct v4l2_fmtdesc *fmtd)
  737. {
  738. switch (fmtd->index) {
  739. case 0:
  740. fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
  741. strcpy(fmtd->description, "r5g6b5");
  742. break;
  743. case 1:
  744. fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
  745. strcpy(fmtd->description, "r5g6b5BE");
  746. break;
  747. case 2:
  748. fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
  749. strcpy(fmtd->description, "yuv4:2:2");
  750. break;
  751. case 3:
  752. fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
  753. strcpy(fmtd->description, "Raw bayer");
  754. break;
  755. case 4:
  756. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  757. strcpy(fmtd->description, "yuv4:2:2");
  758. break;
  759. default:
  760. return -EINVAL;
  761. }
  762. return 0;
  763. }
  764. static struct stk_size {
  765. unsigned w;
  766. unsigned h;
  767. enum stk_mode m;
  768. } stk_sizes[] = {
  769. { .w = 1280, .h = 1024, .m = MODE_SXGA, },
  770. { .w = 640, .h = 480, .m = MODE_VGA, },
  771. { .w = 352, .h = 288, .m = MODE_CIF, },
  772. { .w = 320, .h = 240, .m = MODE_QVGA, },
  773. { .w = 176, .h = 144, .m = MODE_QCIF, },
  774. };
  775. static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
  776. void *priv, struct v4l2_format *f)
  777. {
  778. struct v4l2_pix_format *pix_format = &f->fmt.pix;
  779. struct stk_camera *dev = video_drvdata(filp);
  780. int i;
  781. for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
  782. stk_sizes[i].m != dev->vsettings.mode; i++)
  783. ;
  784. if (i == ARRAY_SIZE(stk_sizes)) {
  785. STK_ERROR("ERROR: mode invalid\n");
  786. return -EINVAL;
  787. }
  788. pix_format->width = stk_sizes[i].w;
  789. pix_format->height = stk_sizes[i].h;
  790. pix_format->field = V4L2_FIELD_NONE;
  791. pix_format->colorspace = V4L2_COLORSPACE_SRGB;
  792. pix_format->pixelformat = dev->vsettings.palette;
  793. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  794. pix_format->bytesperline = pix_format->width;
  795. else
  796. pix_format->bytesperline = 2 * pix_format->width;
  797. pix_format->sizeimage = pix_format->bytesperline
  798. * pix_format->height;
  799. return 0;
  800. }
  801. static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
  802. void *priv, struct v4l2_format *fmtd)
  803. {
  804. int i;
  805. switch (fmtd->fmt.pix.pixelformat) {
  806. case V4L2_PIX_FMT_RGB565:
  807. case V4L2_PIX_FMT_RGB565X:
  808. case V4L2_PIX_FMT_UYVY:
  809. case V4L2_PIX_FMT_YUYV:
  810. case V4L2_PIX_FMT_SBGGR8:
  811. break;
  812. default:
  813. return -EINVAL;
  814. }
  815. for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
  816. if (fmtd->fmt.pix.width > stk_sizes[i].w)
  817. break;
  818. }
  819. if (i == ARRAY_SIZE(stk_sizes)
  820. || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
  821. < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
  822. fmtd->fmt.pix.height = stk_sizes[i-1].h;
  823. fmtd->fmt.pix.width = stk_sizes[i-1].w;
  824. fmtd->fmt.pix.priv = i - 1;
  825. } else {
  826. fmtd->fmt.pix.height = stk_sizes[i].h;
  827. fmtd->fmt.pix.width = stk_sizes[i].w;
  828. fmtd->fmt.pix.priv = i;
  829. }
  830. fmtd->fmt.pix.field = V4L2_FIELD_NONE;
  831. fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  832. if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
  833. fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
  834. else
  835. fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
  836. fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
  837. * fmtd->fmt.pix.height;
  838. return 0;
  839. }
  840. static int stk_setup_format(struct stk_camera *dev)
  841. {
  842. int i = 0;
  843. int depth;
  844. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  845. depth = 1;
  846. else
  847. depth = 2;
  848. while (i < ARRAY_SIZE(stk_sizes) &&
  849. stk_sizes[i].m != dev->vsettings.mode)
  850. i++;
  851. if (i == ARRAY_SIZE(stk_sizes)) {
  852. STK_ERROR("Something is broken in %s\n", __func__);
  853. return -EFAULT;
  854. }
  855. /* This registers controls some timings, not sure of what. */
  856. stk_camera_write_reg(dev, 0x001b, 0x0e);
  857. if (dev->vsettings.mode == MODE_SXGA)
  858. stk_camera_write_reg(dev, 0x001c, 0x0e);
  859. else
  860. stk_camera_write_reg(dev, 0x001c, 0x46);
  861. /*
  862. * Registers 0x0115 0x0114 are the size of each line (bytes),
  863. * regs 0x0117 0x0116 are the heigth of the image.
  864. */
  865. stk_camera_write_reg(dev, 0x0115,
  866. ((stk_sizes[i].w * depth) >> 8) & 0xff);
  867. stk_camera_write_reg(dev, 0x0114,
  868. (stk_sizes[i].w * depth) & 0xff);
  869. stk_camera_write_reg(dev, 0x0117,
  870. (stk_sizes[i].h >> 8) & 0xff);
  871. stk_camera_write_reg(dev, 0x0116,
  872. stk_sizes[i].h & 0xff);
  873. return stk_sensor_configure(dev);
  874. }
  875. static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
  876. void *priv, struct v4l2_format *fmtd)
  877. {
  878. int ret;
  879. struct stk_camera *dev = video_drvdata(filp);
  880. if (dev == NULL)
  881. return -ENODEV;
  882. if (!is_present(dev))
  883. return -ENODEV;
  884. if (is_streaming(dev))
  885. return -EBUSY;
  886. if (dev->owner && dev->owner != filp)
  887. return -EBUSY;
  888. ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
  889. if (ret)
  890. return ret;
  891. dev->owner = filp;
  892. dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
  893. stk_free_buffers(dev);
  894. dev->frame_size = fmtd->fmt.pix.sizeimage;
  895. dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
  896. stk_initialise(dev);
  897. return stk_setup_format(dev);
  898. }
  899. static int stk_vidioc_reqbufs(struct file *filp,
  900. void *priv, struct v4l2_requestbuffers *rb)
  901. {
  902. struct stk_camera *dev = video_drvdata(filp);
  903. if (dev == NULL)
  904. return -ENODEV;
  905. if (rb->memory != V4L2_MEMORY_MMAP)
  906. return -EINVAL;
  907. if (is_streaming(dev)
  908. || (dev->owner && dev->owner != filp))
  909. return -EBUSY;
  910. dev->owner = filp;
  911. /*FIXME If they ask for zero, we must stop streaming and free */
  912. if (rb->count < 3)
  913. rb->count = 3;
  914. /* Arbitrary limit */
  915. else if (rb->count > 5)
  916. rb->count = 5;
  917. stk_allocate_buffers(dev, rb->count);
  918. rb->count = dev->n_sbufs;
  919. return 0;
  920. }
  921. static int stk_vidioc_querybuf(struct file *filp,
  922. void *priv, struct v4l2_buffer *buf)
  923. {
  924. struct stk_camera *dev = video_drvdata(filp);
  925. struct stk_sio_buffer *sbuf;
  926. if (buf->index >= dev->n_sbufs)
  927. return -EINVAL;
  928. sbuf = dev->sio_bufs + buf->index;
  929. *buf = sbuf->v4lbuf;
  930. return 0;
  931. }
  932. static int stk_vidioc_qbuf(struct file *filp,
  933. void *priv, struct v4l2_buffer *buf)
  934. {
  935. struct stk_camera *dev = video_drvdata(filp);
  936. struct stk_sio_buffer *sbuf;
  937. unsigned long flags;
  938. if (buf->memory != V4L2_MEMORY_MMAP)
  939. return -EINVAL;
  940. if (buf->index >= dev->n_sbufs)
  941. return -EINVAL;
  942. sbuf = dev->sio_bufs + buf->index;
  943. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
  944. return 0;
  945. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  946. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  947. spin_lock_irqsave(&dev->spinlock, flags);
  948. list_add_tail(&sbuf->list, &dev->sio_avail);
  949. *buf = sbuf->v4lbuf;
  950. spin_unlock_irqrestore(&dev->spinlock, flags);
  951. return 0;
  952. }
  953. static int stk_vidioc_dqbuf(struct file *filp,
  954. void *priv, struct v4l2_buffer *buf)
  955. {
  956. struct stk_camera *dev = video_drvdata(filp);
  957. struct stk_sio_buffer *sbuf;
  958. unsigned long flags;
  959. int ret;
  960. if (!is_streaming(dev))
  961. return -EINVAL;
  962. if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  963. return -EWOULDBLOCK;
  964. ret = wait_event_interruptible(dev->wait_frame,
  965. !list_empty(&dev->sio_full) || !is_present(dev));
  966. if (ret)
  967. return ret;
  968. if (!is_present(dev))
  969. return -EIO;
  970. spin_lock_irqsave(&dev->spinlock, flags);
  971. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  972. list_del_init(&sbuf->list);
  973. spin_unlock_irqrestore(&dev->spinlock, flags);
  974. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  975. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  976. sbuf->v4lbuf.sequence = ++dev->sequence;
  977. v4l2_get_timestamp(&sbuf->v4lbuf.timestamp);
  978. *buf = sbuf->v4lbuf;
  979. return 0;
  980. }
  981. static int stk_vidioc_streamon(struct file *filp,
  982. void *priv, enum v4l2_buf_type type)
  983. {
  984. struct stk_camera *dev = video_drvdata(filp);
  985. if (is_streaming(dev))
  986. return 0;
  987. if (dev->sio_bufs == NULL)
  988. return -EINVAL;
  989. dev->sequence = 0;
  990. return stk_start_stream(dev);
  991. }
  992. static int stk_vidioc_streamoff(struct file *filp,
  993. void *priv, enum v4l2_buf_type type)
  994. {
  995. struct stk_camera *dev = video_drvdata(filp);
  996. unsigned long flags;
  997. int i;
  998. stk_stop_stream(dev);
  999. spin_lock_irqsave(&dev->spinlock, flags);
  1000. INIT_LIST_HEAD(&dev->sio_avail);
  1001. INIT_LIST_HEAD(&dev->sio_full);
  1002. for (i = 0; i < dev->n_sbufs; i++) {
  1003. INIT_LIST_HEAD(&dev->sio_bufs[i].list);
  1004. dev->sio_bufs[i].v4lbuf.flags = 0;
  1005. }
  1006. spin_unlock_irqrestore(&dev->spinlock, flags);
  1007. return 0;
  1008. }
  1009. static int stk_vidioc_g_parm(struct file *filp,
  1010. void *priv, struct v4l2_streamparm *sp)
  1011. {
  1012. /*FIXME This is not correct */
  1013. sp->parm.capture.timeperframe.numerator = 1;
  1014. sp->parm.capture.timeperframe.denominator = 30;
  1015. sp->parm.capture.readbuffers = 2;
  1016. return 0;
  1017. }
  1018. static int stk_vidioc_enum_framesizes(struct file *filp,
  1019. void *priv, struct v4l2_frmsizeenum *frms)
  1020. {
  1021. if (frms->index >= ARRAY_SIZE(stk_sizes))
  1022. return -EINVAL;
  1023. switch (frms->pixel_format) {
  1024. case V4L2_PIX_FMT_RGB565:
  1025. case V4L2_PIX_FMT_RGB565X:
  1026. case V4L2_PIX_FMT_UYVY:
  1027. case V4L2_PIX_FMT_YUYV:
  1028. case V4L2_PIX_FMT_SBGGR8:
  1029. frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1030. frms->discrete.width = stk_sizes[frms->index].w;
  1031. frms->discrete.height = stk_sizes[frms->index].h;
  1032. return 0;
  1033. default: return -EINVAL;
  1034. }
  1035. }
  1036. static const struct v4l2_ctrl_ops stk_ctrl_ops = {
  1037. .s_ctrl = stk_s_ctrl,
  1038. };
  1039. static struct v4l2_file_operations v4l_stk_fops = {
  1040. .owner = THIS_MODULE,
  1041. .open = v4l_stk_open,
  1042. .release = v4l_stk_release,
  1043. .read = v4l_stk_read,
  1044. .poll = v4l_stk_poll,
  1045. .mmap = v4l_stk_mmap,
  1046. .ioctl = video_ioctl2,
  1047. };
  1048. static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
  1049. .vidioc_querycap = stk_vidioc_querycap,
  1050. .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
  1051. .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
  1052. .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
  1053. .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
  1054. .vidioc_enum_input = stk_vidioc_enum_input,
  1055. .vidioc_s_input = stk_vidioc_s_input,
  1056. .vidioc_g_input = stk_vidioc_g_input,
  1057. .vidioc_reqbufs = stk_vidioc_reqbufs,
  1058. .vidioc_querybuf = stk_vidioc_querybuf,
  1059. .vidioc_qbuf = stk_vidioc_qbuf,
  1060. .vidioc_dqbuf = stk_vidioc_dqbuf,
  1061. .vidioc_streamon = stk_vidioc_streamon,
  1062. .vidioc_streamoff = stk_vidioc_streamoff,
  1063. .vidioc_g_parm = stk_vidioc_g_parm,
  1064. .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
  1065. };
  1066. static void stk_v4l_dev_release(struct video_device *vd)
  1067. {
  1068. struct stk_camera *dev = vdev_to_camera(vd);
  1069. if (dev->sio_bufs != NULL || dev->isobufs != NULL)
  1070. STK_ERROR("We are leaking memory\n");
  1071. usb_put_intf(dev->interface);
  1072. kfree(dev);
  1073. }
  1074. static struct video_device stk_v4l_data = {
  1075. .name = "stkwebcam",
  1076. .fops = &v4l_stk_fops,
  1077. .ioctl_ops = &v4l_stk_ioctl_ops,
  1078. .release = stk_v4l_dev_release,
  1079. };
  1080. static int stk_register_video_device(struct stk_camera *dev)
  1081. {
  1082. int err;
  1083. dev->vdev = stk_v4l_data;
  1084. dev->vdev.debug = debug;
  1085. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  1086. video_set_drvdata(&dev->vdev, dev);
  1087. err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  1088. if (err)
  1089. STK_ERROR("v4l registration failed\n");
  1090. else
  1091. STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
  1092. video_device_node_name(&dev->vdev));
  1093. return err;
  1094. }
  1095. /* USB Stuff */
  1096. static int stk_camera_probe(struct usb_interface *interface,
  1097. const struct usb_device_id *id)
  1098. {
  1099. struct v4l2_ctrl_handler *hdl;
  1100. int err = 0;
  1101. int i;
  1102. struct stk_camera *dev = NULL;
  1103. struct usb_device *udev = interface_to_usbdev(interface);
  1104. struct usb_host_interface *iface_desc;
  1105. struct usb_endpoint_descriptor *endpoint;
  1106. dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
  1107. if (dev == NULL) {
  1108. STK_ERROR("Out of memory !\n");
  1109. return -ENOMEM;
  1110. }
  1111. err = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
  1112. if (err < 0) {
  1113. dev_err(&udev->dev, "couldn't register v4l2_device\n");
  1114. kfree(dev);
  1115. return err;
  1116. }
  1117. hdl = &dev->hdl;
  1118. v4l2_ctrl_handler_init(hdl, 3);
  1119. v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
  1120. V4L2_CID_BRIGHTNESS, 0, 0xff, 0x1, 0x60);
  1121. v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
  1122. V4L2_CID_HFLIP, 0, 1, 1, 1);
  1123. v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
  1124. V4L2_CID_VFLIP, 0, 1, 1, 1);
  1125. if (hdl->error) {
  1126. err = hdl->error;
  1127. dev_err(&udev->dev, "couldn't register control\n");
  1128. goto error;
  1129. }
  1130. dev->v4l2_dev.ctrl_handler = hdl;
  1131. spin_lock_init(&dev->spinlock);
  1132. init_waitqueue_head(&dev->wait_frame);
  1133. dev->udev = udev;
  1134. dev->interface = interface;
  1135. usb_get_intf(interface);
  1136. if (hflip != -1)
  1137. dev->vsettings.hflip = hflip;
  1138. else if (dmi_check_system(stk_upside_down_dmi_table))
  1139. dev->vsettings.hflip = 1;
  1140. else
  1141. dev->vsettings.hflip = 0;
  1142. if (vflip != -1)
  1143. dev->vsettings.vflip = vflip;
  1144. else if (dmi_check_system(stk_upside_down_dmi_table))
  1145. dev->vsettings.vflip = 1;
  1146. else
  1147. dev->vsettings.vflip = 0;
  1148. dev->n_sbufs = 0;
  1149. set_present(dev);
  1150. /* Set up the endpoint information
  1151. * use only the first isoc-in endpoint
  1152. * for the current alternate setting */
  1153. iface_desc = interface->cur_altsetting;
  1154. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1155. endpoint = &iface_desc->endpoint[i].desc;
  1156. if (!dev->isoc_ep
  1157. && usb_endpoint_is_isoc_in(endpoint)) {
  1158. /* we found an isoc in endpoint */
  1159. dev->isoc_ep = usb_endpoint_num(endpoint);
  1160. break;
  1161. }
  1162. }
  1163. if (!dev->isoc_ep) {
  1164. STK_ERROR("Could not find isoc-in endpoint");
  1165. err = -ENODEV;
  1166. goto error;
  1167. }
  1168. dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
  1169. dev->vsettings.mode = MODE_VGA;
  1170. dev->frame_size = 640 * 480 * 2;
  1171. INIT_LIST_HEAD(&dev->sio_avail);
  1172. INIT_LIST_HEAD(&dev->sio_full);
  1173. usb_set_intfdata(interface, dev);
  1174. err = stk_register_video_device(dev);
  1175. if (err)
  1176. goto error;
  1177. return 0;
  1178. error:
  1179. v4l2_ctrl_handler_free(hdl);
  1180. v4l2_device_unregister(&dev->v4l2_dev);
  1181. kfree(dev);
  1182. return err;
  1183. }
  1184. static void stk_camera_disconnect(struct usb_interface *interface)
  1185. {
  1186. struct stk_camera *dev = usb_get_intfdata(interface);
  1187. usb_set_intfdata(interface, NULL);
  1188. unset_present(dev);
  1189. wake_up_interruptible(&dev->wait_frame);
  1190. STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
  1191. video_device_node_name(&dev->vdev));
  1192. video_unregister_device(&dev->vdev);
  1193. v4l2_ctrl_handler_free(&dev->hdl);
  1194. v4l2_device_unregister(&dev->v4l2_dev);
  1195. }
  1196. #ifdef CONFIG_PM
  1197. static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
  1198. {
  1199. struct stk_camera *dev = usb_get_intfdata(intf);
  1200. if (is_streaming(dev)) {
  1201. stk_stop_stream(dev);
  1202. /* yes, this is ugly */
  1203. set_streaming(dev);
  1204. }
  1205. return 0;
  1206. }
  1207. static int stk_camera_resume(struct usb_interface *intf)
  1208. {
  1209. struct stk_camera *dev = usb_get_intfdata(intf);
  1210. if (!is_initialised(dev))
  1211. return 0;
  1212. unset_initialised(dev);
  1213. stk_initialise(dev);
  1214. stk_camera_write_reg(dev, 0x0, 0x49);
  1215. stk_setup_format(dev);
  1216. if (is_streaming(dev))
  1217. stk_start_stream(dev);
  1218. return 0;
  1219. }
  1220. #endif
  1221. static struct usb_driver stk_camera_driver = {
  1222. .name = "stkwebcam",
  1223. .probe = stk_camera_probe,
  1224. .disconnect = stk_camera_disconnect,
  1225. .id_table = stkwebcam_table,
  1226. #ifdef CONFIG_PM
  1227. .suspend = stk_camera_suspend,
  1228. .resume = stk_camera_resume,
  1229. #endif
  1230. };
  1231. module_usb_driver(stk_camera_driver);