zr364xx.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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. struct timeval ts;
  433. const char *tmpbuf;
  434. char *vbuf = videobuf_to_vmalloc(&buf->vb);
  435. unsigned long last_frame;
  436. if (!vbuf)
  437. return;
  438. last_frame = cam->last_frame;
  439. if (last_frame != -1) {
  440. tmpbuf = (const char *)cam->buffer.frame[last_frame].lpvbits;
  441. switch (buf->fmt->fourcc) {
  442. case V4L2_PIX_FMT_JPEG:
  443. buf->vb.size = jpgsize;
  444. memcpy(vbuf, tmpbuf, buf->vb.size);
  445. break;
  446. default:
  447. printk(KERN_DEBUG KBUILD_MODNAME ": unknown format?\n");
  448. }
  449. cam->last_frame = -1;
  450. } else {
  451. printk(KERN_ERR KBUILD_MODNAME ": =======no frame\n");
  452. return;
  453. }
  454. DBG("%s: Buffer 0x%08lx size= %d\n", __func__,
  455. (unsigned long)vbuf, pos);
  456. /* tell v4l buffer was filled */
  457. buf->vb.field_count = cam->frame_count * 2;
  458. do_gettimeofday(&ts);
  459. buf->vb.ts = ts;
  460. buf->vb.state = VIDEOBUF_DONE;
  461. }
  462. static int zr364xx_got_frame(struct zr364xx_camera *cam, int jpgsize)
  463. {
  464. struct zr364xx_dmaqueue *dma_q = &cam->vidq;
  465. struct zr364xx_buffer *buf;
  466. unsigned long flags = 0;
  467. int rc = 0;
  468. DBG("wakeup: %p\n", &dma_q);
  469. spin_lock_irqsave(&cam->slock, flags);
  470. if (list_empty(&dma_q->active)) {
  471. DBG("No active queue to serve\n");
  472. rc = -1;
  473. goto unlock;
  474. }
  475. buf = list_entry(dma_q->active.next,
  476. struct zr364xx_buffer, vb.queue);
  477. if (!waitqueue_active(&buf->vb.done)) {
  478. /* no one active */
  479. rc = -1;
  480. goto unlock;
  481. }
  482. list_del(&buf->vb.queue);
  483. do_gettimeofday(&buf->vb.ts);
  484. DBG("[%p/%d] wakeup\n", buf, buf->vb.i);
  485. zr364xx_fillbuff(cam, buf, jpgsize);
  486. wake_up(&buf->vb.done);
  487. DBG("wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
  488. unlock:
  489. spin_unlock_irqrestore(&cam->slock, flags);
  490. return rc;
  491. }
  492. /* this function moves the usb stream read pipe data
  493. * into the system buffers.
  494. * returns 0 on success, EAGAIN if more data to process (call this
  495. * function again).
  496. */
  497. static int zr364xx_read_video_callback(struct zr364xx_camera *cam,
  498. struct zr364xx_pipeinfo *pipe_info,
  499. struct urb *purb)
  500. {
  501. unsigned char *pdest;
  502. unsigned char *psrc;
  503. s32 idx = -1;
  504. struct zr364xx_framei *frm;
  505. int i = 0;
  506. unsigned char *ptr = NULL;
  507. _DBG("buffer to user\n");
  508. idx = cam->cur_frame;
  509. frm = &cam->buffer.frame[idx];
  510. /* swap bytes if camera needs it */
  511. if (cam->method == METHOD0) {
  512. u16 *buf = (u16 *)pipe_info->transfer_buffer;
  513. for (i = 0; i < purb->actual_length/2; i++)
  514. swab16s(buf + i);
  515. }
  516. /* search done. now find out if should be acquiring */
  517. if (!cam->b_acquire) {
  518. /* we found a frame, but this channel is turned off */
  519. frm->ulState = ZR364XX_READ_IDLE;
  520. return -EINVAL;
  521. }
  522. psrc = (u8 *)pipe_info->transfer_buffer;
  523. ptr = pdest = frm->lpvbits;
  524. if (frm->ulState == ZR364XX_READ_IDLE) {
  525. frm->ulState = ZR364XX_READ_FRAME;
  526. frm->cur_size = 0;
  527. _DBG("jpeg header, ");
  528. memcpy(ptr, header1, sizeof(header1));
  529. ptr += sizeof(header1);
  530. header3 = 0;
  531. memcpy(ptr, &header3, 1);
  532. ptr++;
  533. memcpy(ptr, psrc, 64);
  534. ptr += 64;
  535. header3 = 1;
  536. memcpy(ptr, &header3, 1);
  537. ptr++;
  538. memcpy(ptr, psrc + 64, 64);
  539. ptr += 64;
  540. memcpy(ptr, header2, sizeof(header2));
  541. ptr += sizeof(header2);
  542. memcpy(ptr, psrc + 128,
  543. purb->actual_length - 128);
  544. ptr += purb->actual_length - 128;
  545. _DBG("header : %d %d %d %d %d %d %d %d %d\n",
  546. psrc[0], psrc[1], psrc[2],
  547. psrc[3], psrc[4], psrc[5],
  548. psrc[6], psrc[7], psrc[8]);
  549. frm->cur_size = ptr - pdest;
  550. } else {
  551. if (frm->cur_size + purb->actual_length > MAX_FRAME_SIZE) {
  552. dev_info(&cam->udev->dev,
  553. "%s: buffer (%d bytes) too small to hold "
  554. "frame data. Discarding frame data.\n",
  555. __func__, MAX_FRAME_SIZE);
  556. } else {
  557. pdest += frm->cur_size;
  558. memcpy(pdest, psrc, purb->actual_length);
  559. frm->cur_size += purb->actual_length;
  560. }
  561. }
  562. /*_DBG("cur_size %lu urb size %d\n", frm->cur_size,
  563. purb->actual_length);*/
  564. if (purb->actual_length < pipe_info->transfer_size) {
  565. _DBG("****************Buffer[%d]full*************\n", idx);
  566. cam->last_frame = cam->cur_frame;
  567. cam->cur_frame++;
  568. /* end of system frame ring buffer, start at zero */
  569. if (cam->cur_frame == cam->buffer.dwFrames)
  570. cam->cur_frame = 0;
  571. /* frame ready */
  572. /* go back to find the JPEG EOI marker */
  573. ptr = pdest = frm->lpvbits;
  574. ptr += frm->cur_size - 2;
  575. while (ptr > pdest) {
  576. if (*ptr == 0xFF && *(ptr + 1) == 0xD9
  577. && *(ptr + 2) == 0xFF)
  578. break;
  579. ptr--;
  580. }
  581. if (ptr == pdest)
  582. DBG("No EOI marker\n");
  583. /* Sometimes there is junk data in the middle of the picture,
  584. * we want to skip this bogus frames */
  585. while (ptr > pdest) {
  586. if (*ptr == 0xFF && *(ptr + 1) == 0xFF
  587. && *(ptr + 2) == 0xFF)
  588. break;
  589. ptr--;
  590. }
  591. if (ptr != pdest) {
  592. DBG("Bogus frame ? %d\n", ++(cam->nb));
  593. } else if (cam->b_acquire) {
  594. /* we skip the 2 first frames which are usually buggy */
  595. if (cam->skip)
  596. cam->skip--;
  597. else {
  598. _DBG("jpeg(%lu): %d %d %d %d %d %d %d %d\n",
  599. frm->cur_size,
  600. pdest[0], pdest[1], pdest[2], pdest[3],
  601. pdest[4], pdest[5], pdest[6], pdest[7]);
  602. zr364xx_got_frame(cam, frm->cur_size);
  603. }
  604. }
  605. cam->frame_count++;
  606. frm->ulState = ZR364XX_READ_IDLE;
  607. frm->cur_size = 0;
  608. }
  609. /* done successfully */
  610. return 0;
  611. }
  612. static int zr364xx_vidioc_querycap(struct file *file, void *priv,
  613. struct v4l2_capability *cap)
  614. {
  615. struct zr364xx_camera *cam = video_drvdata(file);
  616. strlcpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
  617. strlcpy(cap->card, cam->udev->product, sizeof(cap->card));
  618. strlcpy(cap->bus_info, dev_name(&cam->udev->dev),
  619. sizeof(cap->bus_info));
  620. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  621. V4L2_CAP_READWRITE |
  622. V4L2_CAP_STREAMING;
  623. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  624. return 0;
  625. }
  626. static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
  627. struct v4l2_input *i)
  628. {
  629. if (i->index != 0)
  630. return -EINVAL;
  631. strcpy(i->name, DRIVER_DESC " Camera");
  632. i->type = V4L2_INPUT_TYPE_CAMERA;
  633. return 0;
  634. }
  635. static int zr364xx_vidioc_g_input(struct file *file, void *priv,
  636. unsigned int *i)
  637. {
  638. *i = 0;
  639. return 0;
  640. }
  641. static int zr364xx_vidioc_s_input(struct file *file, void *priv,
  642. unsigned int i)
  643. {
  644. if (i != 0)
  645. return -EINVAL;
  646. return 0;
  647. }
  648. static int zr364xx_s_ctrl(struct v4l2_ctrl *ctrl)
  649. {
  650. struct zr364xx_camera *cam =
  651. container_of(ctrl->handler, struct zr364xx_camera, ctrl_handler);
  652. int temp;
  653. switch (ctrl->id) {
  654. case V4L2_CID_BRIGHTNESS:
  655. /* hardware brightness */
  656. send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
  657. temp = (0x60 << 8) + 127 - ctrl->val;
  658. send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
  659. break;
  660. default:
  661. return -EINVAL;
  662. }
  663. return 0;
  664. }
  665. static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
  666. void *priv, struct v4l2_fmtdesc *f)
  667. {
  668. if (f->index > 0)
  669. return -EINVAL;
  670. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  671. strcpy(f->description, formats[0].name);
  672. f->pixelformat = formats[0].fourcc;
  673. return 0;
  674. }
  675. static char *decode_fourcc(__u32 pixelformat, char *buf)
  676. {
  677. buf[0] = pixelformat & 0xff;
  678. buf[1] = (pixelformat >> 8) & 0xff;
  679. buf[2] = (pixelformat >> 16) & 0xff;
  680. buf[3] = (pixelformat >> 24) & 0xff;
  681. buf[4] = '\0';
  682. return buf;
  683. }
  684. static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  685. struct v4l2_format *f)
  686. {
  687. struct zr364xx_camera *cam = video_drvdata(file);
  688. char pixelformat_name[5];
  689. if (cam == NULL)
  690. return -ENODEV;
  691. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) {
  692. DBG("%s: unsupported pixelformat V4L2_PIX_FMT_%s\n", __func__,
  693. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name));
  694. return -EINVAL;
  695. }
  696. if (!(f->fmt.pix.width == 160 && f->fmt.pix.height == 120) &&
  697. !(f->fmt.pix.width == 640 && f->fmt.pix.height == 480)) {
  698. f->fmt.pix.width = 320;
  699. f->fmt.pix.height = 240;
  700. }
  701. f->fmt.pix.field = V4L2_FIELD_NONE;
  702. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  703. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  704. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  705. f->fmt.pix.priv = 0;
  706. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  707. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  708. f->fmt.pix.field);
  709. return 0;
  710. }
  711. static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  712. struct v4l2_format *f)
  713. {
  714. struct zr364xx_camera *cam;
  715. if (file == NULL)
  716. return -ENODEV;
  717. cam = video_drvdata(file);
  718. f->fmt.pix.pixelformat = formats[0].fourcc;
  719. f->fmt.pix.field = V4L2_FIELD_NONE;
  720. f->fmt.pix.width = cam->width;
  721. f->fmt.pix.height = cam->height;
  722. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  723. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  724. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  725. f->fmt.pix.priv = 0;
  726. return 0;
  727. }
  728. static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  729. struct v4l2_format *f)
  730. {
  731. struct zr364xx_camera *cam = video_drvdata(file);
  732. struct videobuf_queue *q = &cam->vb_vidq;
  733. char pixelformat_name[5];
  734. int ret = zr364xx_vidioc_try_fmt_vid_cap(file, cam, f);
  735. int i;
  736. if (ret < 0)
  737. return ret;
  738. mutex_lock(&q->vb_lock);
  739. if (videobuf_queue_is_busy(&cam->vb_vidq)) {
  740. DBG("%s queue busy\n", __func__);
  741. ret = -EBUSY;
  742. goto out;
  743. }
  744. if (cam->owner) {
  745. DBG("%s can't change format after started\n", __func__);
  746. ret = -EBUSY;
  747. goto out;
  748. }
  749. cam->width = f->fmt.pix.width;
  750. cam->height = f->fmt.pix.height;
  751. DBG("%s: %dx%d mode selected\n", __func__,
  752. cam->width, cam->height);
  753. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  754. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  755. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  756. f->fmt.pix.priv = 0;
  757. cam->vb_vidq.field = f->fmt.pix.field;
  758. if (f->fmt.pix.width == 160 && f->fmt.pix.height == 120)
  759. mode = 1;
  760. else if (f->fmt.pix.width == 640 && f->fmt.pix.height == 480)
  761. mode = 2;
  762. else
  763. mode = 0;
  764. m0d1[0] = mode;
  765. m1[2].value = 0xf000 + mode;
  766. m2[1].value = 0xf000 + mode;
  767. /* special case for METHOD3, the modes are different */
  768. if (cam->method == METHOD3) {
  769. switch (mode) {
  770. case 1:
  771. m2[1].value = 0xf000 + 4;
  772. break;
  773. case 2:
  774. m2[1].value = 0xf000 + 0;
  775. break;
  776. default:
  777. m2[1].value = 0xf000 + 1;
  778. break;
  779. }
  780. }
  781. header2[437] = cam->height / 256;
  782. header2[438] = cam->height % 256;
  783. header2[439] = cam->width / 256;
  784. header2[440] = cam->width % 256;
  785. for (i = 0; init[cam->method][i].size != -1; i++) {
  786. ret =
  787. send_control_msg(cam->udev, 1, init[cam->method][i].value,
  788. 0, init[cam->method][i].bytes,
  789. init[cam->method][i].size);
  790. if (ret < 0) {
  791. dev_err(&cam->udev->dev,
  792. "error during resolution change sequence: %d\n", i);
  793. goto out;
  794. }
  795. }
  796. /* Added some delay here, since opening/closing the camera quickly,
  797. * like Ekiga does during its startup, can crash the webcam
  798. */
  799. mdelay(100);
  800. cam->skip = 2;
  801. ret = 0;
  802. out:
  803. mutex_unlock(&q->vb_lock);
  804. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  805. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  806. f->fmt.pix.field);
  807. return ret;
  808. }
  809. static int zr364xx_vidioc_reqbufs(struct file *file, void *priv,
  810. struct v4l2_requestbuffers *p)
  811. {
  812. struct zr364xx_camera *cam = video_drvdata(file);
  813. if (cam->owner && cam->owner != priv)
  814. return -EBUSY;
  815. return videobuf_reqbufs(&cam->vb_vidq, p);
  816. }
  817. static int zr364xx_vidioc_querybuf(struct file *file,
  818. void *priv,
  819. struct v4l2_buffer *p)
  820. {
  821. int rc;
  822. struct zr364xx_camera *cam = video_drvdata(file);
  823. rc = videobuf_querybuf(&cam->vb_vidq, p);
  824. return rc;
  825. }
  826. static int zr364xx_vidioc_qbuf(struct file *file,
  827. void *priv,
  828. struct v4l2_buffer *p)
  829. {
  830. int rc;
  831. struct zr364xx_camera *cam = video_drvdata(file);
  832. _DBG("%s\n", __func__);
  833. if (cam->owner && cam->owner != priv)
  834. return -EBUSY;
  835. rc = videobuf_qbuf(&cam->vb_vidq, p);
  836. return rc;
  837. }
  838. static int zr364xx_vidioc_dqbuf(struct file *file,
  839. void *priv,
  840. struct v4l2_buffer *p)
  841. {
  842. int rc;
  843. struct zr364xx_camera *cam = video_drvdata(file);
  844. _DBG("%s\n", __func__);
  845. if (cam->owner && cam->owner != priv)
  846. return -EBUSY;
  847. rc = videobuf_dqbuf(&cam->vb_vidq, p, file->f_flags & O_NONBLOCK);
  848. return rc;
  849. }
  850. static void read_pipe_completion(struct urb *purb)
  851. {
  852. struct zr364xx_pipeinfo *pipe_info;
  853. struct zr364xx_camera *cam;
  854. int pipe;
  855. pipe_info = purb->context;
  856. _DBG("%s %p, status %d\n", __func__, purb, purb->status);
  857. if (pipe_info == NULL) {
  858. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  859. return;
  860. }
  861. cam = pipe_info->cam;
  862. if (cam == NULL) {
  863. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  864. return;
  865. }
  866. /* if shutting down, do not resubmit, exit immediately */
  867. if (purb->status == -ESHUTDOWN) {
  868. DBG("%s, err shutdown\n", __func__);
  869. pipe_info->err_count++;
  870. return;
  871. }
  872. if (pipe_info->state == 0) {
  873. DBG("exiting USB pipe\n");
  874. return;
  875. }
  876. if (purb->actual_length < 0 ||
  877. purb->actual_length > pipe_info->transfer_size) {
  878. dev_err(&cam->udev->dev, "wrong number of bytes\n");
  879. return;
  880. }
  881. if (purb->status == 0)
  882. zr364xx_read_video_callback(cam, pipe_info, purb);
  883. else {
  884. pipe_info->err_count++;
  885. DBG("%s: failed URB %d\n", __func__, purb->status);
  886. }
  887. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  888. /* reuse urb */
  889. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  890. pipe,
  891. pipe_info->transfer_buffer,
  892. pipe_info->transfer_size,
  893. read_pipe_completion, pipe_info);
  894. if (pipe_info->state != 0) {
  895. purb->status = usb_submit_urb(pipe_info->stream_urb,
  896. GFP_ATOMIC);
  897. if (purb->status)
  898. dev_err(&cam->udev->dev,
  899. "error submitting urb (error=%i)\n",
  900. purb->status);
  901. } else
  902. DBG("read pipe complete state 0\n");
  903. }
  904. static int zr364xx_start_readpipe(struct zr364xx_camera *cam)
  905. {
  906. int pipe;
  907. int retval;
  908. struct zr364xx_pipeinfo *pipe_info = cam->pipe;
  909. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  910. DBG("%s: start pipe IN x%x\n", __func__, cam->read_endpoint);
  911. pipe_info->state = 1;
  912. pipe_info->err_count = 0;
  913. pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
  914. if (!pipe_info->stream_urb) {
  915. dev_err(&cam->udev->dev, "ReadStream: Unable to alloc URB\n");
  916. return -ENOMEM;
  917. }
  918. /* transfer buffer allocated in board_init */
  919. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  920. pipe,
  921. pipe_info->transfer_buffer,
  922. pipe_info->transfer_size,
  923. read_pipe_completion, pipe_info);
  924. DBG("submitting URB %p\n", pipe_info->stream_urb);
  925. retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
  926. if (retval) {
  927. printk(KERN_ERR KBUILD_MODNAME ": start read pipe failed\n");
  928. return retval;
  929. }
  930. return 0;
  931. }
  932. static void zr364xx_stop_readpipe(struct zr364xx_camera *cam)
  933. {
  934. struct zr364xx_pipeinfo *pipe_info;
  935. if (cam == NULL) {
  936. printk(KERN_ERR KBUILD_MODNAME ": invalid device\n");
  937. return;
  938. }
  939. DBG("stop read pipe\n");
  940. pipe_info = cam->pipe;
  941. if (pipe_info) {
  942. if (pipe_info->state != 0)
  943. pipe_info->state = 0;
  944. if (pipe_info->stream_urb) {
  945. /* cancel urb */
  946. usb_kill_urb(pipe_info->stream_urb);
  947. usb_free_urb(pipe_info->stream_urb);
  948. pipe_info->stream_urb = NULL;
  949. }
  950. }
  951. return;
  952. }
  953. /* starts acquisition process */
  954. static int zr364xx_start_acquire(struct zr364xx_camera *cam)
  955. {
  956. int j;
  957. DBG("start acquire\n");
  958. cam->last_frame = -1;
  959. cam->cur_frame = 0;
  960. for (j = 0; j < FRAMES; j++) {
  961. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  962. cam->buffer.frame[j].cur_size = 0;
  963. }
  964. cam->b_acquire = 1;
  965. return 0;
  966. }
  967. static inline int zr364xx_stop_acquire(struct zr364xx_camera *cam)
  968. {
  969. cam->b_acquire = 0;
  970. return 0;
  971. }
  972. static int zr364xx_prepare(struct zr364xx_camera *cam)
  973. {
  974. int res;
  975. int i, j;
  976. for (i = 0; init[cam->method][i].size != -1; i++) {
  977. res = send_control_msg(cam->udev, 1, init[cam->method][i].value,
  978. 0, init[cam->method][i].bytes,
  979. init[cam->method][i].size);
  980. if (res < 0) {
  981. dev_err(&cam->udev->dev,
  982. "error during open sequence: %d\n", i);
  983. return res;
  984. }
  985. }
  986. cam->skip = 2;
  987. cam->last_frame = -1;
  988. cam->cur_frame = 0;
  989. cam->frame_count = 0;
  990. for (j = 0; j < FRAMES; j++) {
  991. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  992. cam->buffer.frame[j].cur_size = 0;
  993. }
  994. v4l2_ctrl_handler_setup(&cam->ctrl_handler);
  995. return 0;
  996. }
  997. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  998. enum v4l2_buf_type type)
  999. {
  1000. struct zr364xx_camera *cam = video_drvdata(file);
  1001. int res;
  1002. DBG("%s\n", __func__);
  1003. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1004. return -EINVAL;
  1005. if (cam->owner && cam->owner != priv)
  1006. return -EBUSY;
  1007. res = zr364xx_prepare(cam);
  1008. if (res)
  1009. return res;
  1010. res = videobuf_streamon(&cam->vb_vidq);
  1011. if (res == 0) {
  1012. zr364xx_start_acquire(cam);
  1013. cam->owner = file->private_data;
  1014. }
  1015. return res;
  1016. }
  1017. static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
  1018. enum v4l2_buf_type type)
  1019. {
  1020. struct zr364xx_camera *cam = video_drvdata(file);
  1021. DBG("%s\n", __func__);
  1022. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1023. return -EINVAL;
  1024. if (cam->owner && cam->owner != priv)
  1025. return -EBUSY;
  1026. zr364xx_stop_acquire(cam);
  1027. return videobuf_streamoff(&cam->vb_vidq);
  1028. }
  1029. /* open the camera */
  1030. static int zr364xx_open(struct file *file)
  1031. {
  1032. struct zr364xx_camera *cam = video_drvdata(file);
  1033. int err;
  1034. DBG("%s\n", __func__);
  1035. if (mutex_lock_interruptible(&cam->lock))
  1036. return -ERESTARTSYS;
  1037. err = v4l2_fh_open(file);
  1038. if (err)
  1039. goto out;
  1040. /* Added some delay here, since opening/closing the camera quickly,
  1041. * like Ekiga does during its startup, can crash the webcam
  1042. */
  1043. mdelay(100);
  1044. err = 0;
  1045. out:
  1046. mutex_unlock(&cam->lock);
  1047. DBG("%s: %d\n", __func__, err);
  1048. return err;
  1049. }
  1050. static void zr364xx_release(struct v4l2_device *v4l2_dev)
  1051. {
  1052. struct zr364xx_camera *cam =
  1053. container_of(v4l2_dev, struct zr364xx_camera, v4l2_dev);
  1054. unsigned long i;
  1055. v4l2_device_unregister(&cam->v4l2_dev);
  1056. videobuf_mmap_free(&cam->vb_vidq);
  1057. /* release sys buffers */
  1058. for (i = 0; i < FRAMES; i++) {
  1059. if (cam->buffer.frame[i].lpvbits) {
  1060. DBG("vfree %p\n", cam->buffer.frame[i].lpvbits);
  1061. vfree(cam->buffer.frame[i].lpvbits);
  1062. }
  1063. cam->buffer.frame[i].lpvbits = NULL;
  1064. }
  1065. v4l2_ctrl_handler_free(&cam->ctrl_handler);
  1066. /* release transfer buffer */
  1067. kfree(cam->pipe->transfer_buffer);
  1068. kfree(cam);
  1069. }
  1070. /* release the camera */
  1071. static int zr364xx_close(struct file *file)
  1072. {
  1073. struct zr364xx_camera *cam;
  1074. struct usb_device *udev;
  1075. int i;
  1076. DBG("%s\n", __func__);
  1077. cam = video_drvdata(file);
  1078. mutex_lock(&cam->lock);
  1079. udev = cam->udev;
  1080. if (file->private_data == cam->owner) {
  1081. /* turn off stream */
  1082. if (cam->b_acquire)
  1083. zr364xx_stop_acquire(cam);
  1084. videobuf_streamoff(&cam->vb_vidq);
  1085. for (i = 0; i < 2; i++) {
  1086. send_control_msg(udev, 1, init[cam->method][i].value,
  1087. 0, init[cam->method][i].bytes,
  1088. init[cam->method][i].size);
  1089. }
  1090. cam->owner = NULL;
  1091. }
  1092. /* Added some delay here, since opening/closing the camera quickly,
  1093. * like Ekiga does during its startup, can crash the webcam
  1094. */
  1095. mdelay(100);
  1096. mutex_unlock(&cam->lock);
  1097. return v4l2_fh_release(file);
  1098. }
  1099. static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
  1100. {
  1101. struct zr364xx_camera *cam = video_drvdata(file);
  1102. int ret;
  1103. if (cam == NULL) {
  1104. DBG("%s: cam == NULL\n", __func__);
  1105. return -ENODEV;
  1106. }
  1107. DBG("mmap called, vma=0x%08lx\n", (unsigned long)vma);
  1108. ret = videobuf_mmap_mapper(&cam->vb_vidq, vma);
  1109. DBG("vma start=0x%08lx, size=%ld, ret=%d\n",
  1110. (unsigned long)vma->vm_start,
  1111. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
  1112. return ret;
  1113. }
  1114. static unsigned int zr364xx_poll(struct file *file,
  1115. struct poll_table_struct *wait)
  1116. {
  1117. struct zr364xx_camera *cam = video_drvdata(file);
  1118. struct videobuf_queue *q = &cam->vb_vidq;
  1119. unsigned res = v4l2_ctrl_poll(file, wait);
  1120. _DBG("%s\n", __func__);
  1121. return res | videobuf_poll_stream(file, q, wait);
  1122. }
  1123. static const struct v4l2_ctrl_ops zr364xx_ctrl_ops = {
  1124. .s_ctrl = zr364xx_s_ctrl,
  1125. };
  1126. static const struct v4l2_file_operations zr364xx_fops = {
  1127. .owner = THIS_MODULE,
  1128. .open = zr364xx_open,
  1129. .release = zr364xx_close,
  1130. .read = zr364xx_read,
  1131. .mmap = zr364xx_mmap,
  1132. .unlocked_ioctl = video_ioctl2,
  1133. .poll = zr364xx_poll,
  1134. };
  1135. static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
  1136. .vidioc_querycap = zr364xx_vidioc_querycap,
  1137. .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
  1138. .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
  1139. .vidioc_s_fmt_vid_cap = zr364xx_vidioc_s_fmt_vid_cap,
  1140. .vidioc_g_fmt_vid_cap = zr364xx_vidioc_g_fmt_vid_cap,
  1141. .vidioc_enum_input = zr364xx_vidioc_enum_input,
  1142. .vidioc_g_input = zr364xx_vidioc_g_input,
  1143. .vidioc_s_input = zr364xx_vidioc_s_input,
  1144. .vidioc_streamon = zr364xx_vidioc_streamon,
  1145. .vidioc_streamoff = zr364xx_vidioc_streamoff,
  1146. .vidioc_reqbufs = zr364xx_vidioc_reqbufs,
  1147. .vidioc_querybuf = zr364xx_vidioc_querybuf,
  1148. .vidioc_qbuf = zr364xx_vidioc_qbuf,
  1149. .vidioc_dqbuf = zr364xx_vidioc_dqbuf,
  1150. .vidioc_log_status = v4l2_ctrl_log_status,
  1151. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1152. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1153. };
  1154. static struct video_device zr364xx_template = {
  1155. .name = DRIVER_DESC,
  1156. .fops = &zr364xx_fops,
  1157. .ioctl_ops = &zr364xx_ioctl_ops,
  1158. .release = video_device_release_empty,
  1159. };
  1160. /*******************/
  1161. /* USB integration */
  1162. /*******************/
  1163. static int zr364xx_board_init(struct zr364xx_camera *cam)
  1164. {
  1165. struct zr364xx_pipeinfo *pipe = cam->pipe;
  1166. unsigned long i;
  1167. DBG("board init: %p\n", cam);
  1168. memset(pipe, 0, sizeof(*pipe));
  1169. pipe->cam = cam;
  1170. pipe->transfer_size = BUFFER_SIZE;
  1171. pipe->transfer_buffer = kzalloc(pipe->transfer_size,
  1172. GFP_KERNEL);
  1173. if (pipe->transfer_buffer == NULL) {
  1174. DBG("out of memory!\n");
  1175. return -ENOMEM;
  1176. }
  1177. cam->b_acquire = 0;
  1178. cam->frame_count = 0;
  1179. /*** start create system buffers ***/
  1180. for (i = 0; i < FRAMES; i++) {
  1181. /* always allocate maximum size for system buffers */
  1182. cam->buffer.frame[i].lpvbits = vmalloc(MAX_FRAME_SIZE);
  1183. DBG("valloc %p, idx %lu, pdata %p\n",
  1184. &cam->buffer.frame[i], i,
  1185. cam->buffer.frame[i].lpvbits);
  1186. if (cam->buffer.frame[i].lpvbits == NULL) {
  1187. printk(KERN_INFO KBUILD_MODNAME ": out of memory. "
  1188. "Using less frames\n");
  1189. break;
  1190. }
  1191. }
  1192. if (i == 0) {
  1193. printk(KERN_INFO KBUILD_MODNAME ": out of memory. Aborting\n");
  1194. kfree(cam->pipe->transfer_buffer);
  1195. cam->pipe->transfer_buffer = NULL;
  1196. return -ENOMEM;
  1197. } else
  1198. cam->buffer.dwFrames = i;
  1199. /* make sure internal states are set */
  1200. for (i = 0; i < FRAMES; i++) {
  1201. cam->buffer.frame[i].ulState = ZR364XX_READ_IDLE;
  1202. cam->buffer.frame[i].cur_size = 0;
  1203. }
  1204. cam->cur_frame = 0;
  1205. cam->last_frame = -1;
  1206. /*** end create system buffers ***/
  1207. /* start read pipe */
  1208. zr364xx_start_readpipe(cam);
  1209. DBG(": board initialized\n");
  1210. return 0;
  1211. }
  1212. static int zr364xx_probe(struct usb_interface *intf,
  1213. const struct usb_device_id *id)
  1214. {
  1215. struct usb_device *udev = interface_to_usbdev(intf);
  1216. struct zr364xx_camera *cam = NULL;
  1217. struct usb_host_interface *iface_desc;
  1218. struct usb_endpoint_descriptor *endpoint;
  1219. struct v4l2_ctrl_handler *hdl;
  1220. int err;
  1221. int i;
  1222. DBG("probing...\n");
  1223. dev_info(&intf->dev, DRIVER_DESC " compatible webcam plugged\n");
  1224. dev_info(&intf->dev, "model %04x:%04x detected\n",
  1225. le16_to_cpu(udev->descriptor.idVendor),
  1226. le16_to_cpu(udev->descriptor.idProduct));
  1227. cam = kzalloc(sizeof(struct zr364xx_camera), GFP_KERNEL);
  1228. if (cam == NULL) {
  1229. dev_err(&udev->dev, "cam: out of memory !\n");
  1230. return -ENOMEM;
  1231. }
  1232. cam->v4l2_dev.release = zr364xx_release;
  1233. err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
  1234. if (err < 0) {
  1235. dev_err(&udev->dev, "couldn't register v4l2_device\n");
  1236. kfree(cam);
  1237. return err;
  1238. }
  1239. hdl = &cam->ctrl_handler;
  1240. v4l2_ctrl_handler_init(hdl, 1);
  1241. v4l2_ctrl_new_std(hdl, &zr364xx_ctrl_ops,
  1242. V4L2_CID_BRIGHTNESS, 0, 127, 1, 64);
  1243. if (hdl->error) {
  1244. err = hdl->error;
  1245. dev_err(&udev->dev, "couldn't register control\n");
  1246. goto fail;
  1247. }
  1248. /* save the init method used by this camera */
  1249. cam->method = id->driver_info;
  1250. mutex_init(&cam->lock);
  1251. cam->vdev = zr364xx_template;
  1252. cam->vdev.lock = &cam->lock;
  1253. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1254. cam->vdev.ctrl_handler = &cam->ctrl_handler;
  1255. set_bit(V4L2_FL_USE_FH_PRIO, &cam->vdev.flags);
  1256. video_set_drvdata(&cam->vdev, cam);
  1257. if (debug)
  1258. cam->vdev.debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
  1259. cam->udev = udev;
  1260. switch (mode) {
  1261. case 1:
  1262. dev_info(&udev->dev, "160x120 mode selected\n");
  1263. cam->width = 160;
  1264. cam->height = 120;
  1265. break;
  1266. case 2:
  1267. dev_info(&udev->dev, "640x480 mode selected\n");
  1268. cam->width = 640;
  1269. cam->height = 480;
  1270. break;
  1271. default:
  1272. dev_info(&udev->dev, "320x240 mode selected\n");
  1273. cam->width = 320;
  1274. cam->height = 240;
  1275. break;
  1276. }
  1277. m0d1[0] = mode;
  1278. m1[2].value = 0xf000 + mode;
  1279. m2[1].value = 0xf000 + mode;
  1280. /* special case for METHOD3, the modes are different */
  1281. if (cam->method == METHOD3) {
  1282. switch (mode) {
  1283. case 1:
  1284. m2[1].value = 0xf000 + 4;
  1285. break;
  1286. case 2:
  1287. m2[1].value = 0xf000 + 0;
  1288. break;
  1289. default:
  1290. m2[1].value = 0xf000 + 1;
  1291. break;
  1292. }
  1293. }
  1294. header2[437] = cam->height / 256;
  1295. header2[438] = cam->height % 256;
  1296. header2[439] = cam->width / 256;
  1297. header2[440] = cam->width % 256;
  1298. cam->nb = 0;
  1299. DBG("dev: %p, udev %p interface %p\n", cam, cam->udev, intf);
  1300. /* set up the endpoint information */
  1301. iface_desc = intf->cur_altsetting;
  1302. DBG("num endpoints %d\n", iface_desc->desc.bNumEndpoints);
  1303. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1304. endpoint = &iface_desc->endpoint[i].desc;
  1305. if (!cam->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1306. /* we found the bulk in endpoint */
  1307. cam->read_endpoint = endpoint->bEndpointAddress;
  1308. }
  1309. }
  1310. if (!cam->read_endpoint) {
  1311. err = -ENOMEM;
  1312. dev_err(&intf->dev, "Could not find bulk-in endpoint\n");
  1313. goto fail;
  1314. }
  1315. /* v4l */
  1316. INIT_LIST_HEAD(&cam->vidq.active);
  1317. cam->vidq.cam = cam;
  1318. usb_set_intfdata(intf, cam);
  1319. /* load zr364xx board specific */
  1320. err = zr364xx_board_init(cam);
  1321. if (!err)
  1322. err = v4l2_ctrl_handler_setup(hdl);
  1323. if (err)
  1324. goto fail;
  1325. spin_lock_init(&cam->slock);
  1326. cam->fmt = formats;
  1327. videobuf_queue_vmalloc_init(&cam->vb_vidq, &zr364xx_video_qops,
  1328. NULL, &cam->slock,
  1329. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1330. V4L2_FIELD_NONE,
  1331. sizeof(struct zr364xx_buffer), cam, &cam->lock);
  1332. err = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1333. if (err) {
  1334. dev_err(&udev->dev, "video_register_device failed\n");
  1335. goto fail;
  1336. }
  1337. dev_info(&udev->dev, DRIVER_DESC " controlling device %s\n",
  1338. video_device_node_name(&cam->vdev));
  1339. return 0;
  1340. fail:
  1341. v4l2_ctrl_handler_free(hdl);
  1342. v4l2_device_unregister(&cam->v4l2_dev);
  1343. kfree(cam);
  1344. return err;
  1345. }
  1346. static void zr364xx_disconnect(struct usb_interface *intf)
  1347. {
  1348. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1349. mutex_lock(&cam->lock);
  1350. usb_set_intfdata(intf, NULL);
  1351. dev_info(&intf->dev, DRIVER_DESC " webcam unplugged\n");
  1352. video_unregister_device(&cam->vdev);
  1353. v4l2_device_disconnect(&cam->v4l2_dev);
  1354. /* stops the read pipe if it is running */
  1355. if (cam->b_acquire)
  1356. zr364xx_stop_acquire(cam);
  1357. zr364xx_stop_readpipe(cam);
  1358. mutex_unlock(&cam->lock);
  1359. v4l2_device_put(&cam->v4l2_dev);
  1360. }
  1361. #ifdef CONFIG_PM
  1362. static int zr364xx_suspend(struct usb_interface *intf, pm_message_t message)
  1363. {
  1364. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1365. cam->was_streaming = cam->b_acquire;
  1366. if (!cam->was_streaming)
  1367. return 0;
  1368. zr364xx_stop_acquire(cam);
  1369. zr364xx_stop_readpipe(cam);
  1370. return 0;
  1371. }
  1372. static int zr364xx_resume(struct usb_interface *intf)
  1373. {
  1374. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1375. int res;
  1376. if (!cam->was_streaming)
  1377. return 0;
  1378. zr364xx_start_readpipe(cam);
  1379. res = zr364xx_prepare(cam);
  1380. if (!res)
  1381. zr364xx_start_acquire(cam);
  1382. return res;
  1383. }
  1384. #endif
  1385. /**********************/
  1386. /* Module integration */
  1387. /**********************/
  1388. static struct usb_driver zr364xx_driver = {
  1389. .name = "zr364xx",
  1390. .probe = zr364xx_probe,
  1391. .disconnect = zr364xx_disconnect,
  1392. #ifdef CONFIG_PM
  1393. .suspend = zr364xx_suspend,
  1394. .resume = zr364xx_resume,
  1395. .reset_resume = zr364xx_resume,
  1396. #endif
  1397. .id_table = device_table
  1398. };
  1399. module_usb_driver(zr364xx_driver);
  1400. MODULE_AUTHOR(DRIVER_AUTHOR);
  1401. MODULE_DESCRIPTION(DRIVER_DESC);
  1402. MODULE_LICENSE("GPL");
  1403. MODULE_VERSION(DRIVER_VERSION);