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.
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
21 #define MODULE_NAME "benq"
25 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
26 MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
27 MODULE_LICENSE("GPL");
29 /* specific webcam descriptor */
31 struct gspca_dev gspca_dev
; /* !! must be the first item */
34 /* V4L2 controls supported by the driver */
35 static const struct ctrl sd_ctrls
[] = {
38 static const struct v4l2_pix_format vga_mode
[] = {
39 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
41 .sizeimage
= 320 * 240 * 3 / 8 + 590,
42 .colorspace
= V4L2_COLORSPACE_JPEG
},
45 static void sd_isoc_irq(struct urb
*urb
);
47 /* -- write a register -- */
48 static void reg_w(struct gspca_dev
*gspca_dev
,
51 struct usb_device
*dev
= gspca_dev
->dev
;
54 if (gspca_dev
->usb_err
< 0)
56 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
58 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
65 err("reg_w err %d", ret
);
66 gspca_dev
->usb_err
= ret
;
70 /* this function is called at probe time */
71 static int sd_config(struct gspca_dev
*gspca_dev
,
72 const struct usb_device_id
*id
)
74 gspca_dev
->cam
.cam_mode
= vga_mode
;
75 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(vga_mode
);
76 gspca_dev
->cam
.no_urb_create
= 1;
77 gspca_dev
->cam
.reverse_alts
= 1;
81 /* this function is called at probe and resume time */
82 static int sd_init(struct gspca_dev
*gspca_dev
)
87 static int sd_isoc_init(struct gspca_dev
*gspca_dev
)
91 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
,
92 gspca_dev
->nbalt
- 1);
94 err("usb_set_interface failed");
97 /* reg_w(gspca_dev, 0x0003, 0x0002); */
101 /* -- start the camera -- */
102 static int sd_start(struct gspca_dev
*gspca_dev
)
107 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
109 #error "Not enough URBs in the gspca table"
113 for (n
= 0; n
< 4; n
++) {
114 urb
= usb_alloc_urb(SD_NPKT
, GFP_KERNEL
);
116 err("usb_alloc_urb failed");
119 gspca_dev
->urb
[n
] = urb
;
120 urb
->transfer_buffer
= usb_alloc_coherent(gspca_dev
->dev
,
125 if (urb
->transfer_buffer
== NULL
) {
126 err("usb_alloc_coherent failed");
129 urb
->dev
= gspca_dev
->dev
;
130 urb
->context
= gspca_dev
;
131 urb
->transfer_buffer_length
= SD_PKT_SZ
* SD_NPKT
;
132 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
133 n
& 1 ? 0x82 : 0x83);
134 urb
->transfer_flags
= URB_ISO_ASAP
135 | URB_NO_TRANSFER_DMA_MAP
;
137 urb
->complete
= sd_isoc_irq
;
138 urb
->number_of_packets
= SD_NPKT
;
139 for (i
= 0; i
< SD_NPKT
; i
++) {
140 urb
->iso_frame_desc
[i
].length
= SD_PKT_SZ
;
141 urb
->iso_frame_desc
[i
].offset
= SD_PKT_SZ
* i
;
145 return gspca_dev
->usb_err
;
148 static void sd_stopN(struct gspca_dev
*gspca_dev
)
150 reg_w(gspca_dev
, 0x003c, 0x0003);
151 reg_w(gspca_dev
, 0x003c, 0x0004);
152 reg_w(gspca_dev
, 0x003c, 0x0005);
153 reg_w(gspca_dev
, 0x003c, 0x0006);
154 reg_w(gspca_dev
, 0x003c, 0x0007);
155 usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
,
156 gspca_dev
->nbalt
- 1);
159 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
160 u8
*data
, /* isoc packet */
161 int len
) /* iso packet length */
166 /* reception of an URB */
167 static void sd_isoc_irq(struct urb
*urb
)
169 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
174 PDEBUG(D_PACK
, "sd isoc irq");
175 if (!gspca_dev
->streaming
)
177 if (urb
->status
!= 0) {
178 if (urb
->status
== -ESHUTDOWN
)
179 return; /* disconnection */
181 if (gspca_dev
->frozen
)
184 err("urb status: %d", urb
->status
);
188 /* if this is a control URN (ep 0x83), wait */
189 if (urb
== gspca_dev
->urb
[0] || urb
== gspca_dev
->urb
[2])
192 /* scan both received URBs */
193 if (urb
== gspca_dev
->urb
[1])
194 urb0
= gspca_dev
->urb
[0];
196 urb0
= gspca_dev
->urb
[2];
197 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
199 /* check the packet status and length */
200 if (urb0
->iso_frame_desc
[i
].actual_length
!= SD_PKT_SZ
201 || urb
->iso_frame_desc
[i
].actual_length
!= SD_PKT_SZ
) {
202 PDEBUG(D_ERR
, "ISOC bad lengths %d / %d",
203 urb0
->iso_frame_desc
[i
].actual_length
,
204 urb
->iso_frame_desc
[i
].actual_length
);
205 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
208 st
= urb0
->iso_frame_desc
[i
].status
;
210 st
= urb
->iso_frame_desc
[i
].status
;
212 err("ISOC data error: [%d] status=%d",
214 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
219 * The images are received in URBs of different endpoints
221 * Image pieces in URBs of ep 0x83 are continuated in URBs of
222 * ep 0x82 of the same index.
223 * The packets in the URBs of endpoint 0x83 start with:
224 * - 80 ba/bb 00 00 = start of image followed by 'ff d8'
225 * - 04 ba/bb oo oo = image piece
226 * where 'oo oo' is the image offset
228 * - (other -> bad frame)
229 * The images are JPEG encoded with full header and
231 * The end of image ('ff d9') may occur in any URB.
234 data
= (u8
*) urb0
->transfer_buffer
235 + urb0
->iso_frame_desc
[i
].offset
;
236 if (data
[0] == 0x80 && (data
[1] & 0xfe) == 0xba) {
239 gspca_frame_add(gspca_dev
, LAST_PACKET
,
241 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
242 data
+ 4, SD_PKT_SZ
- 4);
243 } else if (data
[0] == 0x04 && (data
[1] & 0xfe) == 0xba) {
244 gspca_frame_add(gspca_dev
, INTER_PACKET
,
245 data
+ 4, SD_PKT_SZ
- 4);
247 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
250 data
= (u8
*) urb
->transfer_buffer
251 + urb
->iso_frame_desc
[i
].offset
;
252 gspca_frame_add(gspca_dev
, INTER_PACKET
,
256 /* resubmit the URBs */
257 st
= usb_submit_urb(urb0
, GFP_ATOMIC
);
259 err("usb_submit_urb(0) ret %d", st
);
260 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
262 err("usb_submit_urb() ret %d", st
);
265 /* sub-driver description */
266 static const struct sd_desc sd_desc
= {
269 .nctrls
= ARRAY_SIZE(sd_ctrls
),
272 .isoc_init
= sd_isoc_init
,
275 .pkt_scan
= sd_pkt_scan
,
278 /* -- module initialisation -- */
279 static const struct usb_device_id device_table
[] = {
280 {USB_DEVICE(0x04a5, 0x3035)},
283 MODULE_DEVICE_TABLE(usb
, device_table
);
285 /* -- device connect -- */
286 static int sd_probe(struct usb_interface
*intf
,
287 const struct usb_device_id
*id
)
289 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
293 static struct usb_driver sd_driver
= {
295 .id_table
= device_table
,
297 .disconnect
= gspca_disconnect
,
299 .suspend
= gspca_suspend
,
300 .resume
= gspca_resume
,
304 /* -- module insert / remove -- */
305 static int __init
sd_mod_init(void)
307 return usb_register(&sd_driver
);
309 static void __exit
sd_mod_exit(void)
311 usb_deregister(&sd_driver
);
314 module_init(sd_mod_init
);
315 module_exit(sd_mod_exit
);