1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Benq DC E300 subdriver
5 * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #define MODULE_NAME "benq"
14 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
15 MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
16 MODULE_LICENSE("GPL");
18 /* specific webcam descriptor */
20 struct gspca_dev gspca_dev
; /* !! must be the first item */
23 static const struct v4l2_pix_format vga_mode
[] = {
24 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
26 .sizeimage
= 320 * 240 * 3 / 8 + 590,
27 .colorspace
= V4L2_COLORSPACE_JPEG
},
30 static void sd_isoc_irq(struct urb
*urb
);
32 /* -- write a register -- */
33 static void reg_w(struct gspca_dev
*gspca_dev
,
36 struct usb_device
*dev
= gspca_dev
->dev
;
39 if (gspca_dev
->usb_err
< 0)
41 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
43 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
50 pr_err("reg_w err %d\n", ret
);
51 gspca_dev
->usb_err
= ret
;
55 /* this function is called at probe time */
56 static int sd_config(struct gspca_dev
*gspca_dev
,
57 const struct usb_device_id
*id
)
59 gspca_dev
->cam
.cam_mode
= vga_mode
;
60 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(vga_mode
);
61 gspca_dev
->cam
.no_urb_create
= 1;
65 /* this function is called at probe and resume time */
66 static int sd_init(struct gspca_dev
*gspca_dev
)
71 /* -- start the camera -- */
72 static int sd_start(struct gspca_dev
*gspca_dev
)
77 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
79 #error "Not enough URBs in the gspca table"
83 for (n
= 0; n
< 4; n
++) {
84 urb
= usb_alloc_urb(SD_NPKT
, GFP_KERNEL
);
87 gspca_dev
->urb
[n
] = urb
;
88 urb
->transfer_buffer
= usb_alloc_coherent(gspca_dev
->dev
,
93 if (urb
->transfer_buffer
== NULL
) {
94 pr_err("usb_alloc_coherent failed\n");
97 urb
->dev
= gspca_dev
->dev
;
98 urb
->context
= gspca_dev
;
99 urb
->transfer_buffer_length
= SD_PKT_SZ
* SD_NPKT
;
100 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
101 n
& 1 ? 0x82 : 0x83);
102 urb
->transfer_flags
= URB_ISO_ASAP
103 | URB_NO_TRANSFER_DMA_MAP
;
105 urb
->complete
= sd_isoc_irq
;
106 urb
->number_of_packets
= SD_NPKT
;
107 for (i
= 0; i
< SD_NPKT
; i
++) {
108 urb
->iso_frame_desc
[i
].length
= SD_PKT_SZ
;
109 urb
->iso_frame_desc
[i
].offset
= SD_PKT_SZ
* i
;
113 return gspca_dev
->usb_err
;
116 static void sd_stopN(struct gspca_dev
*gspca_dev
)
118 struct usb_interface
*intf
;
120 reg_w(gspca_dev
, 0x003c, 0x0003);
121 reg_w(gspca_dev
, 0x003c, 0x0004);
122 reg_w(gspca_dev
, 0x003c, 0x0005);
123 reg_w(gspca_dev
, 0x003c, 0x0006);
124 reg_w(gspca_dev
, 0x003c, 0x0007);
126 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
127 usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
,
128 intf
->num_altsetting
- 1);
131 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
132 u8
*data
, /* isoc packet */
133 int len
) /* iso packet length */
138 /* reception of an URB */
139 static void sd_isoc_irq(struct urb
*urb
)
141 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
146 gspca_dbg(gspca_dev
, D_PACK
, "sd isoc irq\n");
147 if (!gspca_dev
->streaming
)
149 if (urb
->status
!= 0) {
150 if (urb
->status
== -ESHUTDOWN
)
151 return; /* disconnection */
153 if (gspca_dev
->frozen
)
156 pr_err("urb status: %d\n", urb
->status
);
160 /* if this is a control URN (ep 0x83), wait */
161 if (urb
== gspca_dev
->urb
[0] || urb
== gspca_dev
->urb
[2])
164 /* scan both received URBs */
165 if (urb
== gspca_dev
->urb
[1])
166 urb0
= gspca_dev
->urb
[0];
168 urb0
= gspca_dev
->urb
[2];
169 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
171 /* check the packet status and length */
172 if (urb0
->iso_frame_desc
[i
].actual_length
!= SD_PKT_SZ
173 || urb
->iso_frame_desc
[i
].actual_length
!= SD_PKT_SZ
) {
174 gspca_err(gspca_dev
, "ISOC bad lengths %d / %d\n",
175 urb0
->iso_frame_desc
[i
].actual_length
,
176 urb
->iso_frame_desc
[i
].actual_length
);
177 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
180 st
= urb0
->iso_frame_desc
[i
].status
;
182 st
= urb
->iso_frame_desc
[i
].status
;
184 pr_err("ISOC data error: [%d] status=%d\n",
186 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
191 * The images are received in URBs of different endpoints
193 * Image pieces in URBs of ep 0x83 are continuated in URBs of
194 * ep 0x82 of the same index.
195 * The packets in the URBs of endpoint 0x83 start with:
196 * - 80 ba/bb 00 00 = start of image followed by 'ff d8'
197 * - 04 ba/bb oo oo = image piece
198 * where 'oo oo' is the image offset
200 * - (other -> bad frame)
201 * The images are JPEG encoded with full header and
203 * The end of image ('ff d9') may occur in any URB.
206 data
= (u8
*) urb0
->transfer_buffer
207 + urb0
->iso_frame_desc
[i
].offset
;
208 if (data
[0] == 0x80 && (data
[1] & 0xfe) == 0xba) {
211 gspca_frame_add(gspca_dev
, LAST_PACKET
,
213 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
214 data
+ 4, SD_PKT_SZ
- 4);
215 } else if (data
[0] == 0x04 && (data
[1] & 0xfe) == 0xba) {
216 gspca_frame_add(gspca_dev
, INTER_PACKET
,
217 data
+ 4, SD_PKT_SZ
- 4);
219 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
222 data
= (u8
*) urb
->transfer_buffer
223 + urb
->iso_frame_desc
[i
].offset
;
224 gspca_frame_add(gspca_dev
, INTER_PACKET
,
228 /* resubmit the URBs */
229 st
= usb_submit_urb(urb0
, GFP_ATOMIC
);
231 pr_err("usb_submit_urb(0) ret %d\n", st
);
232 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
234 pr_err("usb_submit_urb() ret %d\n", st
);
237 /* sub-driver description */
238 static const struct sd_desc sd_desc
= {
244 .pkt_scan
= sd_pkt_scan
,
247 /* -- module initialisation -- */
248 static const struct usb_device_id device_table
[] = {
249 {USB_DEVICE(0x04a5, 0x3035)},
252 MODULE_DEVICE_TABLE(usb
, device_table
);
254 /* -- device connect -- */
255 static int sd_probe(struct usb_interface
*intf
,
256 const struct usb_device_id
*id
)
258 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
262 static struct usb_driver sd_driver
= {
264 .id_table
= device_table
,
266 .disconnect
= gspca_disconnect
,
268 .suspend
= gspca_suspend
,
269 .resume
= gspca_resume
,
270 .reset_resume
= gspca_resume
,
274 module_usb_driver(sd_driver
);