benq.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Benq DC E300 subdriver
  3. *
  4. * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #define MODULE_NAME "benq"
  22. #include "gspca.h"
  23. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  24. MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
  25. MODULE_LICENSE("GPL");
  26. /* specific webcam descriptor */
  27. struct sd {
  28. struct gspca_dev gspca_dev; /* !! must be the first item */
  29. };
  30. /* V4L2 controls supported by the driver */
  31. static const struct ctrl sd_ctrls[] = {
  32. };
  33. static const struct v4l2_pix_format vga_mode[] = {
  34. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  35. .bytesperline = 320,
  36. .sizeimage = 320 * 240 * 3 / 8 + 590,
  37. .colorspace = V4L2_COLORSPACE_JPEG},
  38. };
  39. static void sd_isoc_irq(struct urb *urb);
  40. /* -- write a register -- */
  41. static void reg_w(struct gspca_dev *gspca_dev,
  42. u16 value, u16 index)
  43. {
  44. struct usb_device *dev = gspca_dev->dev;
  45. int ret;
  46. if (gspca_dev->usb_err < 0)
  47. return;
  48. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  49. 0x02,
  50. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  51. value,
  52. index,
  53. NULL,
  54. 0,
  55. 500);
  56. if (ret < 0) {
  57. pr_err("reg_w err %d\n", ret);
  58. gspca_dev->usb_err = ret;
  59. }
  60. }
  61. /* this function is called at probe time */
  62. static int sd_config(struct gspca_dev *gspca_dev,
  63. const struct usb_device_id *id)
  64. {
  65. gspca_dev->cam.cam_mode = vga_mode;
  66. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  67. gspca_dev->cam.no_urb_create = 1;
  68. gspca_dev->cam.reverse_alts = 1;
  69. return 0;
  70. }
  71. /* this function is called at probe and resume time */
  72. static int sd_init(struct gspca_dev *gspca_dev)
  73. {
  74. return 0;
  75. }
  76. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  77. {
  78. int ret;
  79. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface,
  80. gspca_dev->nbalt - 1);
  81. if (ret < 0) {
  82. pr_err("usb_set_interface failed\n");
  83. return ret;
  84. }
  85. /* reg_w(gspca_dev, 0x0003, 0x0002); */
  86. return 0;
  87. }
  88. /* -- start the camera -- */
  89. static int sd_start(struct gspca_dev *gspca_dev)
  90. {
  91. struct urb *urb;
  92. int i, n;
  93. /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
  94. #if MAX_NURBS < 4
  95. #error "Not enough URBs in the gspca table"
  96. #endif
  97. #define SD_PKT_SZ 64
  98. #define SD_NPKT 32
  99. for (n = 0; n < 4; n++) {
  100. urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
  101. if (!urb) {
  102. pr_err("usb_alloc_urb failed\n");
  103. return -ENOMEM;
  104. }
  105. gspca_dev->urb[n] = urb;
  106. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  107. SD_PKT_SZ * SD_NPKT,
  108. GFP_KERNEL,
  109. &urb->transfer_dma);
  110. if (urb->transfer_buffer == NULL) {
  111. pr_err("usb_alloc_coherent failed\n");
  112. return -ENOMEM;
  113. }
  114. urb->dev = gspca_dev->dev;
  115. urb->context = gspca_dev;
  116. urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT;
  117. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  118. n & 1 ? 0x82 : 0x83);
  119. urb->transfer_flags = URB_ISO_ASAP
  120. | URB_NO_TRANSFER_DMA_MAP;
  121. urb->interval = 1;
  122. urb->complete = sd_isoc_irq;
  123. urb->number_of_packets = SD_NPKT;
  124. for (i = 0; i < SD_NPKT; i++) {
  125. urb->iso_frame_desc[i].length = SD_PKT_SZ;
  126. urb->iso_frame_desc[i].offset = SD_PKT_SZ * i;
  127. }
  128. }
  129. return gspca_dev->usb_err;
  130. }
  131. static void sd_stopN(struct gspca_dev *gspca_dev)
  132. {
  133. reg_w(gspca_dev, 0x003c, 0x0003);
  134. reg_w(gspca_dev, 0x003c, 0x0004);
  135. reg_w(gspca_dev, 0x003c, 0x0005);
  136. reg_w(gspca_dev, 0x003c, 0x0006);
  137. reg_w(gspca_dev, 0x003c, 0x0007);
  138. usb_set_interface(gspca_dev->dev, gspca_dev->iface,
  139. gspca_dev->nbalt - 1);
  140. }
  141. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  142. u8 *data, /* isoc packet */
  143. int len) /* iso packet length */
  144. {
  145. /* unused */
  146. }
  147. /* reception of an URB */
  148. static void sd_isoc_irq(struct urb *urb)
  149. {
  150. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  151. struct urb *urb0;
  152. u8 *data;
  153. int i, st;
  154. PDEBUG(D_PACK, "sd isoc irq");
  155. if (!gspca_dev->streaming)
  156. return;
  157. if (urb->status != 0) {
  158. if (urb->status == -ESHUTDOWN)
  159. return; /* disconnection */
  160. #ifdef CONFIG_PM
  161. if (gspca_dev->frozen)
  162. return;
  163. #endif
  164. pr_err("urb status: %d\n", urb->status);
  165. return;
  166. }
  167. /* if this is a control URN (ep 0x83), wait */
  168. if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
  169. return;
  170. /* scan both received URBs */
  171. if (urb == gspca_dev->urb[1])
  172. urb0 = gspca_dev->urb[0];
  173. else
  174. urb0 = gspca_dev->urb[2];
  175. for (i = 0; i < urb->number_of_packets; i++) {
  176. /* check the packet status and length */
  177. if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
  178. || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
  179. PDEBUG(D_ERR, "ISOC bad lengths %d / %d",
  180. urb0->iso_frame_desc[i].actual_length,
  181. urb->iso_frame_desc[i].actual_length);
  182. gspca_dev->last_packet_type = DISCARD_PACKET;
  183. continue;
  184. }
  185. st = urb0->iso_frame_desc[i].status;
  186. if (st == 0)
  187. st = urb->iso_frame_desc[i].status;
  188. if (st) {
  189. pr_err("ISOC data error: [%d] status=%d\n",
  190. i, st);
  191. gspca_dev->last_packet_type = DISCARD_PACKET;
  192. continue;
  193. }
  194. /*
  195. * The images are received in URBs of different endpoints
  196. * (0x83 and 0x82).
  197. * Image pieces in URBs of ep 0x83 are continuated in URBs of
  198. * ep 0x82 of the same index.
  199. * The packets in the URBs of endpoint 0x83 start with:
  200. * - 80 ba/bb 00 00 = start of image followed by 'ff d8'
  201. * - 04 ba/bb oo oo = image piece
  202. * where 'oo oo' is the image offset
  203. (not cheked)
  204. * - (other -> bad frame)
  205. * The images are JPEG encoded with full header and
  206. * normal ff escape.
  207. * The end of image ('ff d9') may occur in any URB.
  208. * (not cheked)
  209. */
  210. data = (u8 *) urb0->transfer_buffer
  211. + urb0->iso_frame_desc[i].offset;
  212. if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
  213. /* new image */
  214. gspca_frame_add(gspca_dev, LAST_PACKET,
  215. NULL, 0);
  216. gspca_frame_add(gspca_dev, FIRST_PACKET,
  217. data + 4, SD_PKT_SZ - 4);
  218. } else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) {
  219. gspca_frame_add(gspca_dev, INTER_PACKET,
  220. data + 4, SD_PKT_SZ - 4);
  221. } else {
  222. gspca_dev->last_packet_type = DISCARD_PACKET;
  223. continue;
  224. }
  225. data = (u8 *) urb->transfer_buffer
  226. + urb->iso_frame_desc[i].offset;
  227. gspca_frame_add(gspca_dev, INTER_PACKET,
  228. data, SD_PKT_SZ);
  229. }
  230. /* resubmit the URBs */
  231. st = usb_submit_urb(urb0, GFP_ATOMIC);
  232. if (st < 0)
  233. pr_err("usb_submit_urb(0) ret %d\n", st);
  234. st = usb_submit_urb(urb, GFP_ATOMIC);
  235. if (st < 0)
  236. pr_err("usb_submit_urb() ret %d\n", st);
  237. }
  238. /* sub-driver description */
  239. static const struct sd_desc sd_desc = {
  240. .name = MODULE_NAME,
  241. .ctrls = sd_ctrls,
  242. .nctrls = ARRAY_SIZE(sd_ctrls),
  243. .config = sd_config,
  244. .init = sd_init,
  245. .isoc_init = sd_isoc_init,
  246. .start = sd_start,
  247. .stopN = sd_stopN,
  248. .pkt_scan = sd_pkt_scan,
  249. };
  250. /* -- module initialisation -- */
  251. static const struct usb_device_id device_table[] = {
  252. {USB_DEVICE(0x04a5, 0x3035)},
  253. {}
  254. };
  255. MODULE_DEVICE_TABLE(usb, device_table);
  256. /* -- device connect -- */
  257. static int sd_probe(struct usb_interface *intf,
  258. const struct usb_device_id *id)
  259. {
  260. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  261. THIS_MODULE);
  262. }
  263. static struct usb_driver sd_driver = {
  264. .name = MODULE_NAME,
  265. .id_table = device_table,
  266. .probe = sd_probe,
  267. .disconnect = gspca_disconnect,
  268. #ifdef CONFIG_PM
  269. .suspend = gspca_suspend,
  270. .resume = gspca_resume,
  271. #endif
  272. };
  273. /* -- module insert / remove -- */
  274. static int __init sd_mod_init(void)
  275. {
  276. return usb_register(&sd_driver);
  277. }
  278. static void __exit sd_mod_exit(void)
  279. {
  280. usb_deregister(&sd_driver);
  281. }
  282. module_init(sd_mod_init);
  283. module_exit(sd_mod_exit);