2 * Benq DC E300 subdriver
4 * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
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
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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #define MODULE_NAME "benq"
23 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
24 MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
25 MODULE_LICENSE("GPL");
27 /* specific webcam descriptor */
29 struct gspca_dev gspca_dev; /* !! must be the first item */
32 static const struct v4l2_pix_format vga_mode[] = {
33 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
35 .sizeimage = 320 * 240 * 3 / 8 + 590,
36 .colorspace = V4L2_COLORSPACE_JPEG},
39 static void sd_isoc_irq(struct urb *urb);
41 /* -- write a register -- */
42 static void reg_w(struct gspca_dev *gspca_dev,
45 struct usb_device *dev = gspca_dev->dev;
48 if (gspca_dev->usb_err < 0)
50 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
52 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
59 pr_err("reg_w err %d\n", ret);
60 gspca_dev->usb_err = ret;
64 /* this function is called at probe time */
65 static int sd_config(struct gspca_dev *gspca_dev,
66 const struct usb_device_id *id)
68 gspca_dev->cam.cam_mode = vga_mode;
69 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
70 gspca_dev->cam.no_urb_create = 1;
74 /* this function is called at probe and resume time */
75 static int sd_init(struct gspca_dev *gspca_dev)
80 /* -- start the camera -- */
81 static int sd_start(struct gspca_dev *gspca_dev)
86 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
88 #error "Not enough URBs in the gspca table"
92 for (n = 0; n < 4; n++) {
93 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
96 gspca_dev->urb[n] = urb;
97 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
102 if (urb->transfer_buffer == NULL) {
103 pr_err("usb_alloc_coherent failed\n");
106 urb->dev = gspca_dev->dev;
107 urb->context = gspca_dev;
108 urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT;
109 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
110 n & 1 ? 0x82 : 0x83);
111 urb->transfer_flags = URB_ISO_ASAP
112 | URB_NO_TRANSFER_DMA_MAP;
114 urb->complete = sd_isoc_irq;
115 urb->number_of_packets = SD_NPKT;
116 for (i = 0; i < SD_NPKT; i++) {
117 urb->iso_frame_desc[i].length = SD_PKT_SZ;
118 urb->iso_frame_desc[i].offset = SD_PKT_SZ * i;
122 return gspca_dev->usb_err;
125 static void sd_stopN(struct gspca_dev *gspca_dev)
127 struct usb_interface *intf;
129 reg_w(gspca_dev, 0x003c, 0x0003);
130 reg_w(gspca_dev, 0x003c, 0x0004);
131 reg_w(gspca_dev, 0x003c, 0x0005);
132 reg_w(gspca_dev, 0x003c, 0x0006);
133 reg_w(gspca_dev, 0x003c, 0x0007);
135 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
136 usb_set_interface(gspca_dev->dev, gspca_dev->iface,
137 intf->num_altsetting - 1);
140 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
141 u8 *data, /* isoc packet */
142 int len) /* iso packet length */
147 /* reception of an URB */
148 static void sd_isoc_irq(struct urb *urb)
150 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
155 PDEBUG(D_PACK, "sd isoc irq");
156 if (!gspca_dev->streaming)
158 if (urb->status != 0) {
159 if (urb->status == -ESHUTDOWN)
160 return; /* disconnection */
162 if (gspca_dev->frozen)
165 pr_err("urb status: %d\n", urb->status);
169 /* if this is a control URN (ep 0x83), wait */
170 if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
173 /* scan both received URBs */
174 if (urb == gspca_dev->urb[1])
175 urb0 = gspca_dev->urb[0];
177 urb0 = gspca_dev->urb[2];
178 for (i = 0; i < urb->number_of_packets; i++) {
180 /* check the packet status and length */
181 if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
182 || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
183 PERR("ISOC bad lengths %d / %d",
184 urb0->iso_frame_desc[i].actual_length,
185 urb->iso_frame_desc[i].actual_length);
186 gspca_dev->last_packet_type = DISCARD_PACKET;
189 st = urb0->iso_frame_desc[i].status;
191 st = urb->iso_frame_desc[i].status;
193 pr_err("ISOC data error: [%d] status=%d\n",
195 gspca_dev->last_packet_type = DISCARD_PACKET;
200 * The images are received in URBs of different endpoints
202 * Image pieces in URBs of ep 0x83 are continuated in URBs of
203 * ep 0x82 of the same index.
204 * The packets in the URBs of endpoint 0x83 start with:
205 * - 80 ba/bb 00 00 = start of image followed by 'ff d8'
206 * - 04 ba/bb oo oo = image piece
207 * where 'oo oo' is the image offset
209 * - (other -> bad frame)
210 * The images are JPEG encoded with full header and
212 * The end of image ('ff d9') may occur in any URB.
215 data = (u8 *) urb0->transfer_buffer
216 + urb0->iso_frame_desc[i].offset;
217 if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
220 gspca_frame_add(gspca_dev, LAST_PACKET,
222 gspca_frame_add(gspca_dev, FIRST_PACKET,
223 data + 4, SD_PKT_SZ - 4);
224 } else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) {
225 gspca_frame_add(gspca_dev, INTER_PACKET,
226 data + 4, SD_PKT_SZ - 4);
228 gspca_dev->last_packet_type = DISCARD_PACKET;
231 data = (u8 *) urb->transfer_buffer
232 + urb->iso_frame_desc[i].offset;
233 gspca_frame_add(gspca_dev, INTER_PACKET,
237 /* resubmit the URBs */
238 st = usb_submit_urb(urb0, GFP_ATOMIC);
240 pr_err("usb_submit_urb(0) ret %d\n", st);
241 st = usb_submit_urb(urb, GFP_ATOMIC);
243 pr_err("usb_submit_urb() ret %d\n", st);
246 /* sub-driver description */
247 static const struct sd_desc sd_desc = {
253 .pkt_scan = sd_pkt_scan,
256 /* -- module initialisation -- */
257 static const struct usb_device_id device_table[] = {
258 {USB_DEVICE(0x04a5, 0x3035)},
261 MODULE_DEVICE_TABLE(usb, device_table);
263 /* -- device connect -- */
264 static int sd_probe(struct usb_interface *intf,
265 const struct usb_device_id *id)
267 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
271 static struct usb_driver sd_driver = {
273 .id_table = device_table,
275 .disconnect = gspca_disconnect,
277 .suspend = gspca_suspend,
278 .resume = gspca_resume,
279 .reset_resume = gspca_resume,
283 module_usb_driver(sd_driver);