zr364xx.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. /*
  2. * Zoran 364xx based USB webcam module version 0.73
  3. *
  4. * Allows you to use your USB webcam with V4L2 applications
  5. * This is still in heavy developpement !
  6. *
  7. * Copyright (C) 2004 Antoine Jacquet <royale@zerezo.com>
  8. * http://royale.zerezo.com/zr364xx/
  9. *
  10. * Heavily inspired by usb-skeleton.c, vicam.c, cpia.c and spca50x.c drivers
  11. * V4L2 version inspired by meye.c driver
  12. *
  13. * Some video buffer code by Lamarque based on s2255drv.c and vivi.c drivers.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/usb.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/slab.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/highmem.h>
  36. #include <media/v4l2-common.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-ctrls.h>
  40. #include <media/v4l2-fh.h>
  41. #include <media/v4l2-event.h>
  42. #include <media/videobuf-vmalloc.h>
  43. /* Version Information */
  44. #define DRIVER_VERSION "0.7.4"
  45. #define DRIVER_AUTHOR "Antoine Jacquet, http://royale.zerezo.com/"
  46. #define DRIVER_DESC "Zoran 364xx"
  47. /* Camera */
  48. #define FRAMES 1
  49. #define MAX_FRAME_SIZE 200000
  50. #define BUFFER_SIZE 0x1000
  51. #define CTRL_TIMEOUT 500
  52. #define ZR364XX_DEF_BUFS 4
  53. #define ZR364XX_READ_IDLE 0
  54. #define ZR364XX_READ_FRAME 1
  55. /* Debug macro */
  56. #define DBG(fmt, args...) \
  57. do { \
  58. if (debug) { \
  59. printk(KERN_INFO KBUILD_MODNAME " " fmt, ##args); \
  60. } \
  61. } while (0)
  62. /*#define FULL_DEBUG 1*/
  63. #ifdef FULL_DEBUG
  64. #define _DBG DBG
  65. #else
  66. #define _DBG(fmt, args...)
  67. #endif
  68. /* Init methods, need to find nicer names for these
  69. * the exact names of the chipsets would be the best if someone finds it */
  70. #define METHOD0 0
  71. #define METHOD1 1
  72. #define METHOD2 2
  73. #define METHOD3 3
  74. /* Module parameters */
  75. static int debug;
  76. static int mode;
  77. /* Module parameters interface */
  78. module_param(debug, int, 0644);
  79. MODULE_PARM_DESC(debug, "Debug level");
  80. module_param(mode, int, 0644);
  81. MODULE_PARM_DESC(mode, "0 = 320x240, 1 = 160x120, 2 = 640x480");
  82. /* Devices supported by this driver
  83. * .driver_info contains the init method used by the camera */
  84. static struct usb_device_id device_table[] = {
  85. {USB_DEVICE(0x08ca, 0x0109), .driver_info = METHOD0 },
  86. {USB_DEVICE(0x041e, 0x4024), .driver_info = METHOD0 },
  87. {USB_DEVICE(0x0d64, 0x0108), .driver_info = METHOD0 },
  88. {USB_DEVICE(0x0546, 0x3187), .driver_info = METHOD0 },
  89. {USB_DEVICE(0x0d64, 0x3108), .driver_info = METHOD0 },
  90. {USB_DEVICE(0x0595, 0x4343), .driver_info = METHOD0 },
  91. {USB_DEVICE(0x0bb0, 0x500d), .driver_info = METHOD0 },
  92. {USB_DEVICE(0x0feb, 0x2004), .driver_info = METHOD0 },
  93. {USB_DEVICE(0x055f, 0xb500), .driver_info = METHOD0 },
  94. {USB_DEVICE(0x08ca, 0x2062), .driver_info = METHOD2 },
  95. {USB_DEVICE(0x052b, 0x1a18), .driver_info = METHOD1 },
  96. {USB_DEVICE(0x04c8, 0x0729), .driver_info = METHOD0 },
  97. {USB_DEVICE(0x04f2, 0xa208), .driver_info = METHOD0 },
  98. {USB_DEVICE(0x0784, 0x0040), .driver_info = METHOD1 },
  99. {USB_DEVICE(0x06d6, 0x0034), .driver_info = METHOD0 },
  100. {USB_DEVICE(0x0a17, 0x0062), .driver_info = METHOD2 },
  101. {USB_DEVICE(0x06d6, 0x003b), .driver_info = METHOD0 },
  102. {USB_DEVICE(0x0a17, 0x004e), .driver_info = METHOD2 },
  103. {USB_DEVICE(0x041e, 0x405d), .driver_info = METHOD2 },
  104. {USB_DEVICE(0x08ca, 0x2102), .driver_info = METHOD3 },
  105. {USB_DEVICE(0x06d6, 0x003d), .driver_info = METHOD0 },
  106. {} /* Terminating entry */
  107. };
  108. MODULE_DEVICE_TABLE(usb, device_table);
  109. /* frame structure */
  110. struct zr364xx_framei {
  111. unsigned long ulState; /* ulState:ZR364XX_READ_IDLE,
  112. ZR364XX_READ_FRAME */
  113. void *lpvbits; /* image data */
  114. unsigned long cur_size; /* current data copied to it */
  115. };
  116. /* image buffer structure */
  117. struct zr364xx_bufferi {
  118. unsigned long dwFrames; /* number of frames in buffer */
  119. struct zr364xx_framei frame[FRAMES]; /* array of FRAME structures */
  120. };
  121. struct zr364xx_dmaqueue {
  122. struct list_head active;
  123. struct zr364xx_camera *cam;
  124. };
  125. struct zr364xx_pipeinfo {
  126. u32 transfer_size;
  127. u8 *transfer_buffer;
  128. u32 state;
  129. void *stream_urb;
  130. void *cam; /* back pointer to zr364xx_camera struct */
  131. u32 err_count;
  132. u32 idx;
  133. };
  134. struct zr364xx_fmt {
  135. char *name;
  136. u32 fourcc;
  137. int depth;
  138. };
  139. /* image formats. */
  140. static const struct zr364xx_fmt formats[] = {
  141. {
  142. .name = "JPG",
  143. .fourcc = V4L2_PIX_FMT_JPEG,
  144. .depth = 24
  145. }
  146. };
  147. /* Camera stuff */
  148. struct zr364xx_camera {
  149. struct usb_device *udev; /* save off the usb device pointer */
  150. struct usb_interface *interface;/* the interface for this device */
  151. struct v4l2_device v4l2_dev;
  152. struct v4l2_ctrl_handler ctrl_handler;
  153. struct video_device vdev; /* v4l video device */
  154. struct v4l2_fh *owner; /* owns the streaming */
  155. int nb;
  156. struct zr364xx_bufferi buffer;
  157. int skip;
  158. int width;
  159. int height;
  160. int method;
  161. struct mutex lock;
  162. spinlock_t slock;
  163. struct zr364xx_dmaqueue vidq;
  164. int last_frame;
  165. int cur_frame;
  166. unsigned long frame_count;
  167. int b_acquire;
  168. struct zr364xx_pipeinfo pipe[1];
  169. u8 read_endpoint;
  170. const struct zr364xx_fmt *fmt;
  171. struct videobuf_queue vb_vidq;
  172. bool was_streaming;
  173. };
  174. /* buffer for one video frame */
  175. struct zr364xx_buffer {
  176. /* common v4l buffer stuff -- must be first */
  177. struct videobuf_buffer vb;
  178. const struct zr364xx_fmt *fmt;
  179. };
  180. /* function used to send initialisation commands to the camera */
  181. static int send_control_msg(struct usb_device *udev, u8 request, u16 value,
  182. u16 index, unsigned char *cp, u16 size)
  183. {
  184. int status;
  185. unsigned char *transfer_buffer = kmalloc(size, GFP_KERNEL);
  186. if (!transfer_buffer) {
  187. dev_err(&udev->dev, "kmalloc(%d) failed\n", size);
  188. return -ENOMEM;
  189. }
  190. memcpy(transfer_buffer, cp, size);
  191. status = usb_control_msg(udev,
  192. usb_sndctrlpipe(udev, 0),
  193. request,
  194. USB_DIR_OUT | USB_TYPE_VENDOR |
  195. USB_RECIP_DEVICE, value, index,
  196. transfer_buffer, size, CTRL_TIMEOUT);
  197. kfree(transfer_buffer);
  198. return status;
  199. }
  200. /* Control messages sent to the camera to initialize it
  201. * and launch the capture */
  202. typedef struct {
  203. unsigned int value;
  204. unsigned int size;
  205. unsigned char *bytes;
  206. } message;
  207. /* method 0 */
  208. static unsigned char m0d1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  209. static unsigned char m0d2[] = { 0, 0, 0, 0, 0, 0 };
  210. static unsigned char m0d3[] = { 0, 0 };
  211. static message m0[] = {
  212. {0x1f30, 0, NULL},
  213. {0xd000, 0, NULL},
  214. {0x3370, sizeof(m0d1), m0d1},
  215. {0x2000, 0, NULL},
  216. {0x2f0f, 0, NULL},
  217. {0x2610, sizeof(m0d2), m0d2},
  218. {0xe107, 0, NULL},
  219. {0x2502, 0, NULL},
  220. {0x1f70, 0, NULL},
  221. {0xd000, 0, NULL},
  222. {0x9a01, sizeof(m0d3), m0d3},
  223. {-1, -1, NULL}
  224. };
  225. /* method 1 */
  226. static unsigned char m1d1[] = { 0xff, 0xff };
  227. static unsigned char m1d2[] = { 0x00, 0x00 };
  228. static message m1[] = {
  229. {0x1f30, 0, NULL},
  230. {0xd000, 0, NULL},
  231. {0xf000, 0, NULL},
  232. {0x2000, 0, NULL},
  233. {0x2f0f, 0, NULL},
  234. {0x2650, 0, NULL},
  235. {0xe107, 0, NULL},
  236. {0x2502, sizeof(m1d1), m1d1},
  237. {0x1f70, 0, NULL},
  238. {0xd000, 0, NULL},
  239. {0xd000, 0, NULL},
  240. {0xd000, 0, NULL},
  241. {0x9a01, sizeof(m1d2), m1d2},
  242. {-1, -1, NULL}
  243. };
  244. /* method 2 */
  245. static unsigned char m2d1[] = { 0xff, 0xff };
  246. static message m2[] = {
  247. {0x1f30, 0, NULL},
  248. {0xf000, 0, NULL},
  249. {0x2000, 0, NULL},
  250. {0x2f0f, 0, NULL},
  251. {0x2650, 0, NULL},
  252. {0xe107, 0, NULL},
  253. {0x2502, sizeof(m2d1), m2d1},
  254. {0x1f70, 0, NULL},
  255. {-1, -1, NULL}
  256. };
  257. /* init table */
  258. static message *init[4] = { m0, m1, m2, m2 };
  259. /* JPEG static data in header (Huffman table, etc) */
  260. static unsigned char header1[] = {
  261. 0xFF, 0xD8,
  262. /*
  263. 0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F',
  264. 0x00, 0x01, 0x01, 0x00, 0x33, 0x8A, 0x00, 0x00, 0x33, 0x88,
  265. */
  266. 0xFF, 0xDB, 0x00, 0x84
  267. };
  268. static unsigned char header2[] = {
  269. 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01,
  270. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  271. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  272. 0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
  273. 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
  274. 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
  275. 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
  276. 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33,
  277. 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25,
  278. 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  279. 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,
  280. 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
  281. 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
  282. 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
  283. 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
  284. 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
  285. 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA,
  286. 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
  287. 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
  288. 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F,
  289. 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  290. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  291. 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5,
  292. 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
  293. 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
  294. 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  295. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1,
  296. 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
  297. 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27,
  298. 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
  299. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
  300. 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
  301. 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84,
  302. 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
  303. 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
  304. 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
  305. 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3,
  306. 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,
  307. 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  308. 0xF8, 0xF9, 0xFA, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0xF0, 0x01,
  309. 0x40, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01,
  310. 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11,
  311. 0x00, 0x3F, 0x00
  312. };
  313. static unsigned char header3;
  314. /* ------------------------------------------------------------------
  315. Videobuf operations
  316. ------------------------------------------------------------------*/
  317. static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
  318. unsigned int *size)
  319. {
  320. struct zr364xx_camera *cam = vq->priv_data;
  321. *size = cam->width * cam->height * (cam->fmt->depth >> 3);
  322. if (*count == 0)
  323. *count = ZR364XX_DEF_BUFS;
  324. if (*size * *count > ZR364XX_DEF_BUFS * 1024 * 1024)
  325. *count = (ZR364XX_DEF_BUFS * 1024 * 1024) / *size;
  326. return 0;
  327. }
  328. static void free_buffer(struct videobuf_queue *vq, struct zr364xx_buffer *buf)
  329. {
  330. _DBG("%s\n", __func__);
  331. if (in_interrupt())
  332. BUG();
  333. videobuf_vmalloc_free(&buf->vb);
  334. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  335. }
  336. static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  337. enum v4l2_field field)
  338. {
  339. struct zr364xx_camera *cam = vq->priv_data;
  340. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  341. vb);
  342. int rc;
  343. DBG("%s, field=%d, fmt name = %s\n", __func__, field, cam->fmt != NULL ?
  344. cam->fmt->name : "");
  345. if (cam->fmt == NULL)
  346. return -EINVAL;
  347. buf->vb.size = cam->width * cam->height * (cam->fmt->depth >> 3);
  348. if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size) {
  349. DBG("invalid buffer prepare\n");
  350. return -EINVAL;
  351. }
  352. buf->fmt = cam->fmt;
  353. buf->vb.width = cam->width;
  354. buf->vb.height = cam->height;
  355. buf->vb.field = field;
  356. if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
  357. rc = videobuf_iolock(vq, &buf->vb, NULL);
  358. if (rc < 0)
  359. goto fail;
  360. }
  361. buf->vb.state = VIDEOBUF_PREPARED;
  362. return 0;
  363. fail:
  364. free_buffer(vq, buf);
  365. return rc;
  366. }
  367. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  368. {
  369. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  370. vb);
  371. struct zr364xx_camera *cam = vq->priv_data;
  372. _DBG("%s\n", __func__);
  373. buf->vb.state = VIDEOBUF_QUEUED;
  374. list_add_tail(&buf->vb.queue, &cam->vidq.active);
  375. }
  376. static void buffer_release(struct videobuf_queue *vq,
  377. struct videobuf_buffer *vb)
  378. {
  379. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  380. vb);
  381. _DBG("%s\n", __func__);
  382. free_buffer(vq, buf);
  383. }
  384. static struct videobuf_queue_ops zr364xx_video_qops = {
  385. .buf_setup = buffer_setup,
  386. .buf_prepare = buffer_prepare,
  387. .buf_queue = buffer_queue,
  388. .buf_release = buffer_release,
  389. };
  390. /********************/
  391. /* V4L2 integration */
  392. /********************/
  393. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  394. enum v4l2_buf_type type);
  395. static ssize_t zr364xx_read(struct file *file, char __user *buf, size_t count,
  396. loff_t * ppos)
  397. {
  398. struct zr364xx_camera *cam = video_drvdata(file);
  399. int err = 0;
  400. _DBG("%s\n", __func__);
  401. if (!buf)
  402. return -EINVAL;
  403. if (!count)
  404. return -EINVAL;
  405. if (mutex_lock_interruptible(&cam->lock))
  406. return -ERESTARTSYS;
  407. err = zr364xx_vidioc_streamon(file, file->private_data,
  408. V4L2_BUF_TYPE_VIDEO_CAPTURE);
  409. if (err == 0) {
  410. DBG("%s: reading %d bytes at pos %d.\n", __func__,
  411. (int) count, (int) *ppos);
  412. /* NoMan Sux ! */
  413. err = videobuf_read_one(&cam->vb_vidq, buf, count, ppos,
  414. file->f_flags & O_NONBLOCK);
  415. }
  416. mutex_unlock(&cam->lock);
  417. return err;
  418. }
  419. /* video buffer vmalloc implementation based partly on VIVI driver which is
  420. * Copyright (c) 2006 by
  421. * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
  422. * Ted Walther <ted--a.t--enumera.com>
  423. * John Sokol <sokol--a.t--videotechnology.com>
  424. * http://v4l.videotechnology.com/
  425. *
  426. */
  427. static void zr364xx_fillbuff(struct zr364xx_camera *cam,
  428. struct zr364xx_buffer *buf,
  429. int jpgsize)
  430. {
  431. int pos = 0;
  432. const char *tmpbuf;
  433. char *vbuf = videobuf_to_vmalloc(&buf->vb);
  434. unsigned long last_frame;
  435. if (!vbuf)
  436. return;
  437. last_frame = cam->last_frame;
  438. if (last_frame != -1) {
  439. tmpbuf = (const char *)cam->buffer.frame[last_frame].lpvbits;
  440. switch (buf->fmt->fourcc) {
  441. case V4L2_PIX_FMT_JPEG:
  442. buf->vb.size = jpgsize;
  443. memcpy(vbuf, tmpbuf, buf->vb.size);
  444. break;
  445. default:
  446. printk(KERN_DEBUG KBUILD_MODNAME ": unknown format?\n");
  447. }
  448. cam->last_frame = -1;
  449. } else {
  450. printk(KERN_ERR KBUILD_MODNAME ": =======no frame\n");
  451. return;
  452. }
  453. DBG("%s: Buffer 0x%08lx size= %d\n", __func__,
  454. (unsigned long)vbuf, pos);
  455. /* tell v4l buffer was filled */
  456. buf->vb.field_count = cam->frame_count * 2;
  457. v4l2_get_timestamp(&buf->vb.ts);
  458. buf->vb.state = VIDEOBUF_DONE;
  459. }
  460. static int zr364xx_got_frame(struct zr364xx_camera *cam, int jpgsize)
  461. {
  462. struct zr364xx_dmaqueue *dma_q = &cam->vidq;
  463. struct zr364xx_buffer *buf;
  464. unsigned long flags = 0;
  465. int rc = 0;
  466. DBG("wakeup: %p\n", &dma_q);
  467. spin_lock_irqsave(&cam->slock, flags);
  468. if (list_empty(&dma_q->active)) {
  469. DBG("No active queue to serve\n");
  470. rc = -1;
  471. goto unlock;
  472. }
  473. buf = list_entry(dma_q->active.next,
  474. struct zr364xx_buffer, vb.queue);
  475. if (!waitqueue_active(&buf->vb.done)) {
  476. /* no one active */
  477. rc = -1;
  478. goto unlock;
  479. }
  480. list_del(&buf->vb.queue);
  481. v4l2_get_timestamp(&buf->vb.ts);
  482. DBG("[%p/%d] wakeup\n", buf, buf->vb.i);
  483. zr364xx_fillbuff(cam, buf, jpgsize);
  484. wake_up(&buf->vb.done);
  485. DBG("wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
  486. unlock:
  487. spin_unlock_irqrestore(&cam->slock, flags);
  488. return rc;
  489. }
  490. /* this function moves the usb stream read pipe data
  491. * into the system buffers.
  492. * returns 0 on success, EAGAIN if more data to process (call this
  493. * function again).
  494. */
  495. static int zr364xx_read_video_callback(struct zr364xx_camera *cam,
  496. struct zr364xx_pipeinfo *pipe_info,
  497. struct urb *purb)
  498. {
  499. unsigned char *pdest;
  500. unsigned char *psrc;
  501. s32 idx = -1;
  502. struct zr364xx_framei *frm;
  503. int i = 0;
  504. unsigned char *ptr = NULL;
  505. _DBG("buffer to user\n");
  506. idx = cam->cur_frame;
  507. frm = &cam->buffer.frame[idx];
  508. /* swap bytes if camera needs it */
  509. if (cam->method == METHOD0) {
  510. u16 *buf = (u16 *)pipe_info->transfer_buffer;
  511. for (i = 0; i < purb->actual_length/2; i++)
  512. swab16s(buf + i);
  513. }
  514. /* search done. now find out if should be acquiring */
  515. if (!cam->b_acquire) {
  516. /* we found a frame, but this channel is turned off */
  517. frm->ulState = ZR364XX_READ_IDLE;
  518. return -EINVAL;
  519. }
  520. psrc = (u8 *)pipe_info->transfer_buffer;
  521. ptr = pdest = frm->lpvbits;
  522. if (frm->ulState == ZR364XX_READ_IDLE) {
  523. frm->ulState = ZR364XX_READ_FRAME;
  524. frm->cur_size = 0;
  525. _DBG("jpeg header, ");
  526. memcpy(ptr, header1, sizeof(header1));
  527. ptr += sizeof(header1);
  528. header3 = 0;
  529. memcpy(ptr, &header3, 1);
  530. ptr++;
  531. memcpy(ptr, psrc, 64);
  532. ptr += 64;
  533. header3 = 1;
  534. memcpy(ptr, &header3, 1);
  535. ptr++;
  536. memcpy(ptr, psrc + 64, 64);
  537. ptr += 64;
  538. memcpy(ptr, header2, sizeof(header2));
  539. ptr += sizeof(header2);
  540. memcpy(ptr, psrc + 128,
  541. purb->actual_length - 128);
  542. ptr += purb->actual_length - 128;
  543. _DBG("header : %d %d %d %d %d %d %d %d %d\n",
  544. psrc[0], psrc[1], psrc[2],
  545. psrc[3], psrc[4], psrc[5],
  546. psrc[6], psrc[7], psrc[8]);
  547. frm->cur_size = ptr - pdest;
  548. } else {
  549. if (frm->cur_size + purb->actual_length > MAX_FRAME_SIZE) {
  550. dev_info(&cam->udev->dev,
  551. "%s: buffer (%d bytes) too small to hold "
  552. "frame data. Discarding frame data.\n",
  553. __func__, MAX_FRAME_SIZE);
  554. } else {
  555. pdest += frm->cur_size;
  556. memcpy(pdest, psrc, purb->actual_length);
  557. frm->cur_size += purb->actual_length;
  558. }
  559. }
  560. /*_DBG("cur_size %lu urb size %d\n", frm->cur_size,
  561. purb->actual_length);*/
  562. if (purb->actual_length < pipe_info->transfer_size) {
  563. _DBG("****************Buffer[%d]full*************\n", idx);
  564. cam->last_frame = cam->cur_frame;
  565. cam->cur_frame++;
  566. /* end of system frame ring buffer, start at zero */
  567. if (cam->cur_frame == cam->buffer.dwFrames)
  568. cam->cur_frame = 0;
  569. /* frame ready */
  570. /* go back to find the JPEG EOI marker */
  571. ptr = pdest = frm->lpvbits;
  572. ptr += frm->cur_size - 2;
  573. while (ptr > pdest) {
  574. if (*ptr == 0xFF && *(ptr + 1) == 0xD9
  575. && *(ptr + 2) == 0xFF)
  576. break;
  577. ptr--;
  578. }
  579. if (ptr == pdest)
  580. DBG("No EOI marker\n");
  581. /* Sometimes there is junk data in the middle of the picture,
  582. * we want to skip this bogus frames */
  583. while (ptr > pdest) {
  584. if (*ptr == 0xFF && *(ptr + 1) == 0xFF
  585. && *(ptr + 2) == 0xFF)
  586. break;
  587. ptr--;
  588. }
  589. if (ptr != pdest) {
  590. DBG("Bogus frame ? %d\n", ++(cam->nb));
  591. } else if (cam->b_acquire) {
  592. /* we skip the 2 first frames which are usually buggy */
  593. if (cam->skip)
  594. cam->skip--;
  595. else {
  596. _DBG("jpeg(%lu): %d %d %d %d %d %d %d %d\n",
  597. frm->cur_size,
  598. pdest[0], pdest[1], pdest[2], pdest[3],
  599. pdest[4], pdest[5], pdest[6], pdest[7]);
  600. zr364xx_got_frame(cam, frm->cur_size);
  601. }
  602. }
  603. cam->frame_count++;
  604. frm->ulState = ZR364XX_READ_IDLE;
  605. frm->cur_size = 0;
  606. }
  607. /* done successfully */
  608. return 0;
  609. }
  610. static int zr364xx_vidioc_querycap(struct file *file, void *priv,
  611. struct v4l2_capability *cap)
  612. {
  613. struct zr364xx_camera *cam = video_drvdata(file);
  614. strlcpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
  615. strlcpy(cap->card, cam->udev->product, sizeof(cap->card));
  616. strlcpy(cap->bus_info, dev_name(&cam->udev->dev),
  617. sizeof(cap->bus_info));
  618. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  619. V4L2_CAP_READWRITE |
  620. V4L2_CAP_STREAMING;
  621. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  622. return 0;
  623. }
  624. static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
  625. struct v4l2_input *i)
  626. {
  627. if (i->index != 0)
  628. return -EINVAL;
  629. strcpy(i->name, DRIVER_DESC " Camera");
  630. i->type = V4L2_INPUT_TYPE_CAMERA;
  631. return 0;
  632. }
  633. static int zr364xx_vidioc_g_input(struct file *file, void *priv,
  634. unsigned int *i)
  635. {
  636. *i = 0;
  637. return 0;
  638. }
  639. static int zr364xx_vidioc_s_input(struct file *file, void *priv,
  640. unsigned int i)
  641. {
  642. if (i != 0)
  643. return -EINVAL;
  644. return 0;
  645. }
  646. static int zr364xx_s_ctrl(struct v4l2_ctrl *ctrl)
  647. {
  648. struct zr364xx_camera *cam =
  649. container_of(ctrl->handler, struct zr364xx_camera, ctrl_handler);
  650. int temp;
  651. switch (ctrl->id) {
  652. case V4L2_CID_BRIGHTNESS:
  653. /* hardware brightness */
  654. send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
  655. temp = (0x60 << 8) + 127 - ctrl->val;
  656. send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
  657. break;
  658. default:
  659. return -EINVAL;
  660. }
  661. return 0;
  662. }
  663. static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
  664. void *priv, struct v4l2_fmtdesc *f)
  665. {
  666. if (f->index > 0)
  667. return -EINVAL;
  668. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  669. strcpy(f->description, formats[0].name);
  670. f->pixelformat = formats[0].fourcc;
  671. return 0;
  672. }
  673. static char *decode_fourcc(__u32 pixelformat, char *buf)
  674. {
  675. buf[0] = pixelformat & 0xff;
  676. buf[1] = (pixelformat >> 8) & 0xff;
  677. buf[2] = (pixelformat >> 16) & 0xff;
  678. buf[3] = (pixelformat >> 24) & 0xff;
  679. buf[4] = '\0';
  680. return buf;
  681. }
  682. static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  683. struct v4l2_format *f)
  684. {
  685. struct zr364xx_camera *cam = video_drvdata(file);
  686. char pixelformat_name[5];
  687. if (cam == NULL)
  688. return -ENODEV;
  689. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) {
  690. DBG("%s: unsupported pixelformat V4L2_PIX_FMT_%s\n", __func__,
  691. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name));
  692. return -EINVAL;
  693. }
  694. if (!(f->fmt.pix.width == 160 && f->fmt.pix.height == 120) &&
  695. !(f->fmt.pix.width == 640 && f->fmt.pix.height == 480)) {
  696. f->fmt.pix.width = 320;
  697. f->fmt.pix.height = 240;
  698. }
  699. f->fmt.pix.field = V4L2_FIELD_NONE;
  700. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  701. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  702. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  703. f->fmt.pix.priv = 0;
  704. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  705. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  706. f->fmt.pix.field);
  707. return 0;
  708. }
  709. static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  710. struct v4l2_format *f)
  711. {
  712. struct zr364xx_camera *cam;
  713. if (file == NULL)
  714. return -ENODEV;
  715. cam = video_drvdata(file);
  716. f->fmt.pix.pixelformat = formats[0].fourcc;
  717. f->fmt.pix.field = V4L2_FIELD_NONE;
  718. f->fmt.pix.width = cam->width;
  719. f->fmt.pix.height = cam->height;
  720. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  721. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  722. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  723. f->fmt.pix.priv = 0;
  724. return 0;
  725. }
  726. static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  727. struct v4l2_format *f)
  728. {
  729. struct zr364xx_camera *cam = video_drvdata(file);
  730. struct videobuf_queue *q = &cam->vb_vidq;
  731. char pixelformat_name[5];
  732. int ret = zr364xx_vidioc_try_fmt_vid_cap(file, cam, f);
  733. int i;
  734. if (ret < 0)
  735. return ret;
  736. mutex_lock(&q->vb_lock);
  737. if (videobuf_queue_is_busy(&cam->vb_vidq)) {
  738. DBG("%s queue busy\n", __func__);
  739. ret = -EBUSY;
  740. goto out;
  741. }
  742. if (cam->owner) {
  743. DBG("%s can't change format after started\n", __func__);
  744. ret = -EBUSY;
  745. goto out;
  746. }
  747. cam->width = f->fmt.pix.width;
  748. cam->height = f->fmt.pix.height;
  749. DBG("%s: %dx%d mode selected\n", __func__,
  750. cam->width, cam->height);
  751. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  752. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  753. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  754. f->fmt.pix.priv = 0;
  755. cam->vb_vidq.field = f->fmt.pix.field;
  756. if (f->fmt.pix.width == 160 && f->fmt.pix.height == 120)
  757. mode = 1;
  758. else if (f->fmt.pix.width == 640 && f->fmt.pix.height == 480)
  759. mode = 2;
  760. else
  761. mode = 0;
  762. m0d1[0] = mode;
  763. m1[2].value = 0xf000 + mode;
  764. m2[1].value = 0xf000 + mode;
  765. /* special case for METHOD3, the modes are different */
  766. if (cam->method == METHOD3) {
  767. switch (mode) {
  768. case 1:
  769. m2[1].value = 0xf000 + 4;
  770. break;
  771. case 2:
  772. m2[1].value = 0xf000 + 0;
  773. break;
  774. default:
  775. m2[1].value = 0xf000 + 1;
  776. break;
  777. }
  778. }
  779. header2[437] = cam->height / 256;
  780. header2[438] = cam->height % 256;
  781. header2[439] = cam->width / 256;
  782. header2[440] = cam->width % 256;
  783. for (i = 0; init[cam->method][i].size != -1; i++) {
  784. ret =
  785. send_control_msg(cam->udev, 1, init[cam->method][i].value,
  786. 0, init[cam->method][i].bytes,
  787. init[cam->method][i].size);
  788. if (ret < 0) {
  789. dev_err(&cam->udev->dev,
  790. "error during resolution change sequence: %d\n", i);
  791. goto out;
  792. }
  793. }
  794. /* Added some delay here, since opening/closing the camera quickly,
  795. * like Ekiga does during its startup, can crash the webcam
  796. */
  797. mdelay(100);
  798. cam->skip = 2;
  799. ret = 0;
  800. out:
  801. mutex_unlock(&q->vb_lock);
  802. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  803. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  804. f->fmt.pix.field);
  805. return ret;
  806. }
  807. static int zr364xx_vidioc_reqbufs(struct file *file, void *priv,
  808. struct v4l2_requestbuffers *p)
  809. {
  810. struct zr364xx_camera *cam = video_drvdata(file);
  811. if (cam->owner && cam->owner != priv)
  812. return -EBUSY;
  813. return videobuf_reqbufs(&cam->vb_vidq, p);
  814. }
  815. static int zr364xx_vidioc_querybuf(struct file *file,
  816. void *priv,
  817. struct v4l2_buffer *p)
  818. {
  819. int rc;
  820. struct zr364xx_camera *cam = video_drvdata(file);
  821. rc = videobuf_querybuf(&cam->vb_vidq, p);
  822. return rc;
  823. }
  824. static int zr364xx_vidioc_qbuf(struct file *file,
  825. void *priv,
  826. struct v4l2_buffer *p)
  827. {
  828. int rc;
  829. struct zr364xx_camera *cam = video_drvdata(file);
  830. _DBG("%s\n", __func__);
  831. if (cam->owner && cam->owner != priv)
  832. return -EBUSY;
  833. rc = videobuf_qbuf(&cam->vb_vidq, p);
  834. return rc;
  835. }
  836. static int zr364xx_vidioc_dqbuf(struct file *file,
  837. void *priv,
  838. struct v4l2_buffer *p)
  839. {
  840. int rc;
  841. struct zr364xx_camera *cam = video_drvdata(file);
  842. _DBG("%s\n", __func__);
  843. if (cam->owner && cam->owner != priv)
  844. return -EBUSY;
  845. rc = videobuf_dqbuf(&cam->vb_vidq, p, file->f_flags & O_NONBLOCK);
  846. return rc;
  847. }
  848. static void read_pipe_completion(struct urb *purb)
  849. {
  850. struct zr364xx_pipeinfo *pipe_info;
  851. struct zr364xx_camera *cam;
  852. int pipe;
  853. pipe_info = purb->context;
  854. _DBG("%s %p, status %d\n", __func__, purb, purb->status);
  855. if (pipe_info == NULL) {
  856. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  857. return;
  858. }
  859. cam = pipe_info->cam;
  860. if (cam == NULL) {
  861. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  862. return;
  863. }
  864. /* if shutting down, do not resubmit, exit immediately */
  865. if (purb->status == -ESHUTDOWN) {
  866. DBG("%s, err shutdown\n", __func__);
  867. pipe_info->err_count++;
  868. return;
  869. }
  870. if (pipe_info->state == 0) {
  871. DBG("exiting USB pipe\n");
  872. return;
  873. }
  874. if (purb->actual_length > pipe_info->transfer_size) {
  875. dev_err(&cam->udev->dev, "wrong number of bytes\n");
  876. return;
  877. }
  878. if (purb->status == 0)
  879. zr364xx_read_video_callback(cam, pipe_info, purb);
  880. else {
  881. pipe_info->err_count++;
  882. DBG("%s: failed URB %d\n", __func__, purb->status);
  883. }
  884. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  885. /* reuse urb */
  886. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  887. pipe,
  888. pipe_info->transfer_buffer,
  889. pipe_info->transfer_size,
  890. read_pipe_completion, pipe_info);
  891. if (pipe_info->state != 0) {
  892. purb->status = usb_submit_urb(pipe_info->stream_urb,
  893. GFP_ATOMIC);
  894. if (purb->status)
  895. dev_err(&cam->udev->dev,
  896. "error submitting urb (error=%i)\n",
  897. purb->status);
  898. } else
  899. DBG("read pipe complete state 0\n");
  900. }
  901. static int zr364xx_start_readpipe(struct zr364xx_camera *cam)
  902. {
  903. int pipe;
  904. int retval;
  905. struct zr364xx_pipeinfo *pipe_info = cam->pipe;
  906. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  907. DBG("%s: start pipe IN x%x\n", __func__, cam->read_endpoint);
  908. pipe_info->state = 1;
  909. pipe_info->err_count = 0;
  910. pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
  911. if (!pipe_info->stream_urb) {
  912. dev_err(&cam->udev->dev, "ReadStream: Unable to alloc URB\n");
  913. return -ENOMEM;
  914. }
  915. /* transfer buffer allocated in board_init */
  916. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  917. pipe,
  918. pipe_info->transfer_buffer,
  919. pipe_info->transfer_size,
  920. read_pipe_completion, pipe_info);
  921. DBG("submitting URB %p\n", pipe_info->stream_urb);
  922. retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
  923. if (retval) {
  924. printk(KERN_ERR KBUILD_MODNAME ": start read pipe failed\n");
  925. return retval;
  926. }
  927. return 0;
  928. }
  929. static void zr364xx_stop_readpipe(struct zr364xx_camera *cam)
  930. {
  931. struct zr364xx_pipeinfo *pipe_info;
  932. if (cam == NULL) {
  933. printk(KERN_ERR KBUILD_MODNAME ": invalid device\n");
  934. return;
  935. }
  936. DBG("stop read pipe\n");
  937. pipe_info = cam->pipe;
  938. if (pipe_info) {
  939. if (pipe_info->state != 0)
  940. pipe_info->state = 0;
  941. if (pipe_info->stream_urb) {
  942. /* cancel urb */
  943. usb_kill_urb(pipe_info->stream_urb);
  944. usb_free_urb(pipe_info->stream_urb);
  945. pipe_info->stream_urb = NULL;
  946. }
  947. }
  948. return;
  949. }
  950. /* starts acquisition process */
  951. static int zr364xx_start_acquire(struct zr364xx_camera *cam)
  952. {
  953. int j;
  954. DBG("start acquire\n");
  955. cam->last_frame = -1;
  956. cam->cur_frame = 0;
  957. for (j = 0; j < FRAMES; j++) {
  958. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  959. cam->buffer.frame[j].cur_size = 0;
  960. }
  961. cam->b_acquire = 1;
  962. return 0;
  963. }
  964. static inline int zr364xx_stop_acquire(struct zr364xx_camera *cam)
  965. {
  966. cam->b_acquire = 0;
  967. return 0;
  968. }
  969. static int zr364xx_prepare(struct zr364xx_camera *cam)
  970. {
  971. int res;
  972. int i, j;
  973. for (i = 0; init[cam->method][i].size != -1; i++) {
  974. res = send_control_msg(cam->udev, 1, init[cam->method][i].value,
  975. 0, init[cam->method][i].bytes,
  976. init[cam->method][i].size);
  977. if (res < 0) {
  978. dev_err(&cam->udev->dev,
  979. "error during open sequence: %d\n", i);
  980. return res;
  981. }
  982. }
  983. cam->skip = 2;
  984. cam->last_frame = -1;
  985. cam->cur_frame = 0;
  986. cam->frame_count = 0;
  987. for (j = 0; j < FRAMES; j++) {
  988. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  989. cam->buffer.frame[j].cur_size = 0;
  990. }
  991. v4l2_ctrl_handler_setup(&cam->ctrl_handler);
  992. return 0;
  993. }
  994. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  995. enum v4l2_buf_type type)
  996. {
  997. struct zr364xx_camera *cam = video_drvdata(file);
  998. int res;
  999. DBG("%s\n", __func__);
  1000. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1001. return -EINVAL;
  1002. if (cam->owner && cam->owner != priv)
  1003. return -EBUSY;
  1004. res = zr364xx_prepare(cam);
  1005. if (res)
  1006. return res;
  1007. res = videobuf_streamon(&cam->vb_vidq);
  1008. if (res == 0) {
  1009. zr364xx_start_acquire(cam);
  1010. cam->owner = file->private_data;
  1011. }
  1012. return res;
  1013. }
  1014. static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
  1015. enum v4l2_buf_type type)
  1016. {
  1017. struct zr364xx_camera *cam = video_drvdata(file);
  1018. DBG("%s\n", __func__);
  1019. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1020. return -EINVAL;
  1021. if (cam->owner && cam->owner != priv)
  1022. return -EBUSY;
  1023. zr364xx_stop_acquire(cam);
  1024. return videobuf_streamoff(&cam->vb_vidq);
  1025. }
  1026. /* open the camera */
  1027. static int zr364xx_open(struct file *file)
  1028. {
  1029. struct zr364xx_camera *cam = video_drvdata(file);
  1030. int err;
  1031. DBG("%s\n", __func__);
  1032. if (mutex_lock_interruptible(&cam->lock))
  1033. return -ERESTARTSYS;
  1034. err = v4l2_fh_open(file);
  1035. if (err)
  1036. goto out;
  1037. /* Added some delay here, since opening/closing the camera quickly,
  1038. * like Ekiga does during its startup, can crash the webcam
  1039. */
  1040. mdelay(100);
  1041. err = 0;
  1042. out:
  1043. mutex_unlock(&cam->lock);
  1044. DBG("%s: %d\n", __func__, err);
  1045. return err;
  1046. }
  1047. static void zr364xx_release(struct v4l2_device *v4l2_dev)
  1048. {
  1049. struct zr364xx_camera *cam =
  1050. container_of(v4l2_dev, struct zr364xx_camera, v4l2_dev);
  1051. unsigned long i;
  1052. v4l2_device_unregister(&cam->v4l2_dev);
  1053. videobuf_mmap_free(&cam->vb_vidq);
  1054. /* release sys buffers */
  1055. for (i = 0; i < FRAMES; i++) {
  1056. if (cam->buffer.frame[i].lpvbits) {
  1057. DBG("vfree %p\n", cam->buffer.frame[i].lpvbits);
  1058. vfree(cam->buffer.frame[i].lpvbits);
  1059. }
  1060. cam->buffer.frame[i].lpvbits = NULL;
  1061. }
  1062. v4l2_ctrl_handler_free(&cam->ctrl_handler);
  1063. /* release transfer buffer */
  1064. kfree(cam->pipe->transfer_buffer);
  1065. kfree(cam);
  1066. }
  1067. /* release the camera */
  1068. static int zr364xx_close(struct file *file)
  1069. {
  1070. struct zr364xx_camera *cam;
  1071. struct usb_device *udev;
  1072. int i;
  1073. DBG("%s\n", __func__);
  1074. cam = video_drvdata(file);
  1075. mutex_lock(&cam->lock);
  1076. udev = cam->udev;
  1077. if (file->private_data == cam->owner) {
  1078. /* turn off stream */
  1079. if (cam->b_acquire)
  1080. zr364xx_stop_acquire(cam);
  1081. videobuf_streamoff(&cam->vb_vidq);
  1082. for (i = 0; i < 2; i++) {
  1083. send_control_msg(udev, 1, init[cam->method][i].value,
  1084. 0, init[cam->method][i].bytes,
  1085. init[cam->method][i].size);
  1086. }
  1087. cam->owner = NULL;
  1088. }
  1089. /* Added some delay here, since opening/closing the camera quickly,
  1090. * like Ekiga does during its startup, can crash the webcam
  1091. */
  1092. mdelay(100);
  1093. mutex_unlock(&cam->lock);
  1094. return v4l2_fh_release(file);
  1095. }
  1096. static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
  1097. {
  1098. struct zr364xx_camera *cam = video_drvdata(file);
  1099. int ret;
  1100. if (cam == NULL) {
  1101. DBG("%s: cam == NULL\n", __func__);
  1102. return -ENODEV;
  1103. }
  1104. DBG("mmap called, vma=0x%08lx\n", (unsigned long)vma);
  1105. ret = videobuf_mmap_mapper(&cam->vb_vidq, vma);
  1106. DBG("vma start=0x%08lx, size=%ld, ret=%d\n",
  1107. (unsigned long)vma->vm_start,
  1108. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
  1109. return ret;
  1110. }
  1111. static unsigned int zr364xx_poll(struct file *file,
  1112. struct poll_table_struct *wait)
  1113. {
  1114. struct zr364xx_camera *cam = video_drvdata(file);
  1115. struct videobuf_queue *q = &cam->vb_vidq;
  1116. unsigned res = v4l2_ctrl_poll(file, wait);
  1117. _DBG("%s\n", __func__);
  1118. return res | videobuf_poll_stream(file, q, wait);
  1119. }
  1120. static const struct v4l2_ctrl_ops zr364xx_ctrl_ops = {
  1121. .s_ctrl = zr364xx_s_ctrl,
  1122. };
  1123. static const struct v4l2_file_operations zr364xx_fops = {
  1124. .owner = THIS_MODULE,
  1125. .open = zr364xx_open,
  1126. .release = zr364xx_close,
  1127. .read = zr364xx_read,
  1128. .mmap = zr364xx_mmap,
  1129. .unlocked_ioctl = video_ioctl2,
  1130. .poll = zr364xx_poll,
  1131. };
  1132. static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
  1133. .vidioc_querycap = zr364xx_vidioc_querycap,
  1134. .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
  1135. .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
  1136. .vidioc_s_fmt_vid_cap = zr364xx_vidioc_s_fmt_vid_cap,
  1137. .vidioc_g_fmt_vid_cap = zr364xx_vidioc_g_fmt_vid_cap,
  1138. .vidioc_enum_input = zr364xx_vidioc_enum_input,
  1139. .vidioc_g_input = zr364xx_vidioc_g_input,
  1140. .vidioc_s_input = zr364xx_vidioc_s_input,
  1141. .vidioc_streamon = zr364xx_vidioc_streamon,
  1142. .vidioc_streamoff = zr364xx_vidioc_streamoff,
  1143. .vidioc_reqbufs = zr364xx_vidioc_reqbufs,
  1144. .vidioc_querybuf = zr364xx_vidioc_querybuf,
  1145. .vidioc_qbuf = zr364xx_vidioc_qbuf,
  1146. .vidioc_dqbuf = zr364xx_vidioc_dqbuf,
  1147. .vidioc_log_status = v4l2_ctrl_log_status,
  1148. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1149. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1150. };
  1151. static struct video_device zr364xx_template = {
  1152. .name = DRIVER_DESC,
  1153. .fops = &zr364xx_fops,
  1154. .ioctl_ops = &zr364xx_ioctl_ops,
  1155. .release = video_device_release_empty,
  1156. };
  1157. /*******************/
  1158. /* USB integration */
  1159. /*******************/
  1160. static int zr364xx_board_init(struct zr364xx_camera *cam)
  1161. {
  1162. struct zr364xx_pipeinfo *pipe = cam->pipe;
  1163. unsigned long i;
  1164. DBG("board init: %p\n", cam);
  1165. memset(pipe, 0, sizeof(*pipe));
  1166. pipe->cam = cam;
  1167. pipe->transfer_size = BUFFER_SIZE;
  1168. pipe->transfer_buffer = kzalloc(pipe->transfer_size,
  1169. GFP_KERNEL);
  1170. if (pipe->transfer_buffer == NULL) {
  1171. DBG("out of memory!\n");
  1172. return -ENOMEM;
  1173. }
  1174. cam->b_acquire = 0;
  1175. cam->frame_count = 0;
  1176. /*** start create system buffers ***/
  1177. for (i = 0; i < FRAMES; i++) {
  1178. /* always allocate maximum size for system buffers */
  1179. cam->buffer.frame[i].lpvbits = vmalloc(MAX_FRAME_SIZE);
  1180. DBG("valloc %p, idx %lu, pdata %p\n",
  1181. &cam->buffer.frame[i], i,
  1182. cam->buffer.frame[i].lpvbits);
  1183. if (cam->buffer.frame[i].lpvbits == NULL) {
  1184. printk(KERN_INFO KBUILD_MODNAME ": out of memory. "
  1185. "Using less frames\n");
  1186. break;
  1187. }
  1188. }
  1189. if (i == 0) {
  1190. printk(KERN_INFO KBUILD_MODNAME ": out of memory. Aborting\n");
  1191. kfree(cam->pipe->transfer_buffer);
  1192. cam->pipe->transfer_buffer = NULL;
  1193. return -ENOMEM;
  1194. } else
  1195. cam->buffer.dwFrames = i;
  1196. /* make sure internal states are set */
  1197. for (i = 0; i < FRAMES; i++) {
  1198. cam->buffer.frame[i].ulState = ZR364XX_READ_IDLE;
  1199. cam->buffer.frame[i].cur_size = 0;
  1200. }
  1201. cam->cur_frame = 0;
  1202. cam->last_frame = -1;
  1203. /*** end create system buffers ***/
  1204. /* start read pipe */
  1205. zr364xx_start_readpipe(cam);
  1206. DBG(": board initialized\n");
  1207. return 0;
  1208. }
  1209. static int zr364xx_probe(struct usb_interface *intf,
  1210. const struct usb_device_id *id)
  1211. {
  1212. struct usb_device *udev = interface_to_usbdev(intf);
  1213. struct zr364xx_camera *cam = NULL;
  1214. struct usb_host_interface *iface_desc;
  1215. struct usb_endpoint_descriptor *endpoint;
  1216. struct v4l2_ctrl_handler *hdl;
  1217. int err;
  1218. int i;
  1219. DBG("probing...\n");
  1220. dev_info(&intf->dev, DRIVER_DESC " compatible webcam plugged\n");
  1221. dev_info(&intf->dev, "model %04x:%04x detected\n",
  1222. le16_to_cpu(udev->descriptor.idVendor),
  1223. le16_to_cpu(udev->descriptor.idProduct));
  1224. cam = kzalloc(sizeof(struct zr364xx_camera), GFP_KERNEL);
  1225. if (cam == NULL) {
  1226. dev_err(&udev->dev, "cam: out of memory !\n");
  1227. return -ENOMEM;
  1228. }
  1229. cam->v4l2_dev.release = zr364xx_release;
  1230. err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
  1231. if (err < 0) {
  1232. dev_err(&udev->dev, "couldn't register v4l2_device\n");
  1233. kfree(cam);
  1234. return err;
  1235. }
  1236. hdl = &cam->ctrl_handler;
  1237. v4l2_ctrl_handler_init(hdl, 1);
  1238. v4l2_ctrl_new_std(hdl, &zr364xx_ctrl_ops,
  1239. V4L2_CID_BRIGHTNESS, 0, 127, 1, 64);
  1240. if (hdl->error) {
  1241. err = hdl->error;
  1242. dev_err(&udev->dev, "couldn't register control\n");
  1243. goto fail;
  1244. }
  1245. /* save the init method used by this camera */
  1246. cam->method = id->driver_info;
  1247. mutex_init(&cam->lock);
  1248. cam->vdev = zr364xx_template;
  1249. cam->vdev.lock = &cam->lock;
  1250. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1251. cam->vdev.ctrl_handler = &cam->ctrl_handler;
  1252. set_bit(V4L2_FL_USE_FH_PRIO, &cam->vdev.flags);
  1253. video_set_drvdata(&cam->vdev, cam);
  1254. if (debug)
  1255. cam->vdev.debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
  1256. cam->udev = udev;
  1257. switch (mode) {
  1258. case 1:
  1259. dev_info(&udev->dev, "160x120 mode selected\n");
  1260. cam->width = 160;
  1261. cam->height = 120;
  1262. break;
  1263. case 2:
  1264. dev_info(&udev->dev, "640x480 mode selected\n");
  1265. cam->width = 640;
  1266. cam->height = 480;
  1267. break;
  1268. default:
  1269. dev_info(&udev->dev, "320x240 mode selected\n");
  1270. cam->width = 320;
  1271. cam->height = 240;
  1272. break;
  1273. }
  1274. m0d1[0] = mode;
  1275. m1[2].value = 0xf000 + mode;
  1276. m2[1].value = 0xf000 + mode;
  1277. /* special case for METHOD3, the modes are different */
  1278. if (cam->method == METHOD3) {
  1279. switch (mode) {
  1280. case 1:
  1281. m2[1].value = 0xf000 + 4;
  1282. break;
  1283. case 2:
  1284. m2[1].value = 0xf000 + 0;
  1285. break;
  1286. default:
  1287. m2[1].value = 0xf000 + 1;
  1288. break;
  1289. }
  1290. }
  1291. header2[437] = cam->height / 256;
  1292. header2[438] = cam->height % 256;
  1293. header2[439] = cam->width / 256;
  1294. header2[440] = cam->width % 256;
  1295. cam->nb = 0;
  1296. DBG("dev: %p, udev %p interface %p\n", cam, cam->udev, intf);
  1297. /* set up the endpoint information */
  1298. iface_desc = intf->cur_altsetting;
  1299. DBG("num endpoints %d\n", iface_desc->desc.bNumEndpoints);
  1300. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1301. endpoint = &iface_desc->endpoint[i].desc;
  1302. if (!cam->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1303. /* we found the bulk in endpoint */
  1304. cam->read_endpoint = endpoint->bEndpointAddress;
  1305. }
  1306. }
  1307. if (!cam->read_endpoint) {
  1308. err = -ENOMEM;
  1309. dev_err(&intf->dev, "Could not find bulk-in endpoint\n");
  1310. goto fail;
  1311. }
  1312. /* v4l */
  1313. INIT_LIST_HEAD(&cam->vidq.active);
  1314. cam->vidq.cam = cam;
  1315. usb_set_intfdata(intf, cam);
  1316. /* load zr364xx board specific */
  1317. err = zr364xx_board_init(cam);
  1318. if (!err)
  1319. err = v4l2_ctrl_handler_setup(hdl);
  1320. if (err)
  1321. goto fail;
  1322. spin_lock_init(&cam->slock);
  1323. cam->fmt = formats;
  1324. videobuf_queue_vmalloc_init(&cam->vb_vidq, &zr364xx_video_qops,
  1325. NULL, &cam->slock,
  1326. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1327. V4L2_FIELD_NONE,
  1328. sizeof(struct zr364xx_buffer), cam, &cam->lock);
  1329. err = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1330. if (err) {
  1331. dev_err(&udev->dev, "video_register_device failed\n");
  1332. goto fail;
  1333. }
  1334. dev_info(&udev->dev, DRIVER_DESC " controlling device %s\n",
  1335. video_device_node_name(&cam->vdev));
  1336. return 0;
  1337. fail:
  1338. v4l2_ctrl_handler_free(hdl);
  1339. v4l2_device_unregister(&cam->v4l2_dev);
  1340. kfree(cam);
  1341. return err;
  1342. }
  1343. static void zr364xx_disconnect(struct usb_interface *intf)
  1344. {
  1345. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1346. mutex_lock(&cam->lock);
  1347. usb_set_intfdata(intf, NULL);
  1348. dev_info(&intf->dev, DRIVER_DESC " webcam unplugged\n");
  1349. video_unregister_device(&cam->vdev);
  1350. v4l2_device_disconnect(&cam->v4l2_dev);
  1351. /* stops the read pipe if it is running */
  1352. if (cam->b_acquire)
  1353. zr364xx_stop_acquire(cam);
  1354. zr364xx_stop_readpipe(cam);
  1355. mutex_unlock(&cam->lock);
  1356. v4l2_device_put(&cam->v4l2_dev);
  1357. }
  1358. #ifdef CONFIG_PM
  1359. static int zr364xx_suspend(struct usb_interface *intf, pm_message_t message)
  1360. {
  1361. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1362. cam->was_streaming = cam->b_acquire;
  1363. if (!cam->was_streaming)
  1364. return 0;
  1365. zr364xx_stop_acquire(cam);
  1366. zr364xx_stop_readpipe(cam);
  1367. return 0;
  1368. }
  1369. static int zr364xx_resume(struct usb_interface *intf)
  1370. {
  1371. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1372. int res;
  1373. if (!cam->was_streaming)
  1374. return 0;
  1375. zr364xx_start_readpipe(cam);
  1376. res = zr364xx_prepare(cam);
  1377. if (!res)
  1378. zr364xx_start_acquire(cam);
  1379. return res;
  1380. }
  1381. #endif
  1382. /**********************/
  1383. /* Module integration */
  1384. /**********************/
  1385. static struct usb_driver zr364xx_driver = {
  1386. .name = "zr364xx",
  1387. .probe = zr364xx_probe,
  1388. .disconnect = zr364xx_disconnect,
  1389. #ifdef CONFIG_PM
  1390. .suspend = zr364xx_suspend,
  1391. .resume = zr364xx_resume,
  1392. .reset_resume = zr364xx_resume,
  1393. #endif
  1394. .id_table = device_table
  1395. };
  1396. module_usb_driver(zr364xx_driver);
  1397. MODULE_AUTHOR(DRIVER_AUTHOR);
  1398. MODULE_DESCRIPTION(DRIVER_DESC);
  1399. MODULE_LICENSE("GPL");
  1400. MODULE_VERSION(DRIVER_VERSION);