[S390] Remove error checking from copy_oldmem_page()
[linux/fpc-iii.git] / drivers / usb / gadget / dbgp.c
blobf855ecf7a637f7153711e770f8088e0d90eb4acf
1 /*
2 * dbgp.c -- EHCI Debug Port device gadget
4 * Copyright (C) 2010 Stephane Duverger
6 * Released under the GPLv2.
7 */
9 /* verbose messages */
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/usb/ch9.h>
13 #include <linux/usb/gadget.h>
15 /* See comments in "zero.c" */
16 #include "epautoconf.c"
18 #ifdef CONFIG_USB_G_DBGP_SERIAL
19 #include "u_serial.c"
20 #endif
22 #define DRIVER_VENDOR_ID 0x0525 /* NetChip */
23 #define DRIVER_PRODUCT_ID 0xc0de /* undefined */
25 #define USB_DEBUG_MAX_PACKET_SIZE 8
26 #define DBGP_REQ_EP0_LEN 128
27 #define DBGP_REQ_LEN 512
29 static struct dbgp {
30 struct usb_gadget *gadget;
31 struct usb_request *req;
32 struct usb_ep *i_ep;
33 struct usb_ep *o_ep;
34 #ifdef CONFIG_USB_G_DBGP_SERIAL
35 struct gserial *serial;
36 #endif
37 } dbgp;
39 static struct usb_device_descriptor device_desc = {
40 .bLength = sizeof device_desc,
41 .bDescriptorType = USB_DT_DEVICE,
42 .bcdUSB = __constant_cpu_to_le16(0x0200),
43 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
44 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_ID),
45 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_ID),
46 .bNumConfigurations = 1,
49 static struct usb_debug_descriptor dbg_desc = {
50 .bLength = sizeof dbg_desc,
51 .bDescriptorType = USB_DT_DEBUG,
54 static struct usb_endpoint_descriptor i_desc = {
55 .bLength = USB_DT_ENDPOINT_SIZE,
56 .bDescriptorType = USB_DT_ENDPOINT,
57 .bmAttributes = USB_ENDPOINT_XFER_BULK,
58 .bEndpointAddress = USB_DIR_IN,
61 static struct usb_endpoint_descriptor o_desc = {
62 .bLength = USB_DT_ENDPOINT_SIZE,
63 .bDescriptorType = USB_DT_ENDPOINT,
64 .bmAttributes = USB_ENDPOINT_XFER_BULK,
65 .bEndpointAddress = USB_DIR_OUT,
68 #ifdef CONFIG_USB_G_DBGP_PRINTK
69 static int dbgp_consume(char *buf, unsigned len)
71 char c;
73 if (!len)
74 return 0;
76 c = buf[len-1];
77 if (c != 0)
78 buf[len-1] = 0;
80 printk(KERN_NOTICE "%s%c", buf, c);
81 return 0;
84 static void __disable_ep(struct usb_ep *ep)
86 if (ep && ep->driver_data == dbgp.gadget) {
87 usb_ep_disable(ep);
88 ep->driver_data = NULL;
92 static void dbgp_disable_ep(void)
94 __disable_ep(dbgp.i_ep);
95 __disable_ep(dbgp.o_ep);
98 static void dbgp_complete(struct usb_ep *ep, struct usb_request *req)
100 int stp;
101 int err = 0;
102 int status = req->status;
104 if (ep == dbgp.i_ep) {
105 stp = 1;
106 goto fail;
109 if (status != 0) {
110 stp = 2;
111 goto release_req;
114 dbgp_consume(req->buf, req->actual);
116 req->length = DBGP_REQ_LEN;
117 err = usb_ep_queue(ep, req, GFP_ATOMIC);
118 if (err < 0) {
119 stp = 3;
120 goto release_req;
123 return;
125 release_req:
126 kfree(req->buf);
127 usb_ep_free_request(dbgp.o_ep, req);
128 dbgp_disable_ep();
129 fail:
130 dev_dbg(&dbgp.gadget->dev,
131 "complete: failure (%d:%d) ==> %d\n", stp, err, status);
134 static int dbgp_enable_ep_req(struct usb_ep *ep)
136 int err, stp;
137 struct usb_request *req;
139 req = usb_ep_alloc_request(ep, GFP_KERNEL);
140 if (!req) {
141 err = -ENOMEM;
142 stp = 1;
143 goto fail_1;
146 req->buf = kmalloc(DBGP_REQ_LEN, GFP_KERNEL);
147 if (!req->buf) {
148 err = -ENOMEM;
149 stp = 2;
150 goto fail_2;
153 req->complete = dbgp_complete;
154 req->length = DBGP_REQ_LEN;
155 err = usb_ep_queue(ep, req, GFP_ATOMIC);
156 if (err < 0) {
157 stp = 3;
158 goto fail_3;
161 return 0;
163 fail_3:
164 kfree(req->buf);
165 fail_2:
166 usb_ep_free_request(dbgp.o_ep, req);
167 fail_1:
168 dev_dbg(&dbgp.gadget->dev,
169 "enable ep req: failure (%d:%d)\n", stp, err);
170 return err;
173 static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc)
175 int err;
176 ep->desc = desc;
177 err = usb_ep_enable(ep);
178 ep->driver_data = dbgp.gadget;
179 return err;
182 static int dbgp_enable_ep(void)
184 int err, stp;
186 err = __enable_ep(dbgp.i_ep, &i_desc);
187 if (err < 0) {
188 stp = 1;
189 goto fail_1;
192 err = __enable_ep(dbgp.o_ep, &o_desc);
193 if (err < 0) {
194 stp = 2;
195 goto fail_2;
198 err = dbgp_enable_ep_req(dbgp.o_ep);
199 if (err < 0) {
200 stp = 3;
201 goto fail_3;
204 return 0;
206 fail_3:
207 __disable_ep(dbgp.o_ep);
208 fail_2:
209 __disable_ep(dbgp.i_ep);
210 fail_1:
211 dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err);
212 return err;
214 #endif
216 static void dbgp_disconnect(struct usb_gadget *gadget)
218 #ifdef CONFIG_USB_G_DBGP_PRINTK
219 dbgp_disable_ep();
220 #else
221 gserial_disconnect(dbgp.serial);
222 #endif
225 static void dbgp_unbind(struct usb_gadget *gadget)
227 #ifdef CONFIG_USB_G_DBGP_SERIAL
228 kfree(dbgp.serial);
229 #endif
230 if (dbgp.req) {
231 kfree(dbgp.req->buf);
232 usb_ep_free_request(gadget->ep0, dbgp.req);
235 gadget->ep0->driver_data = NULL;
238 static int __init dbgp_configure_endpoints(struct usb_gadget *gadget)
240 int stp;
242 usb_ep_autoconfig_reset(gadget);
244 dbgp.i_ep = usb_ep_autoconfig(gadget, &i_desc);
245 if (!dbgp.i_ep) {
246 stp = 1;
247 goto fail_1;
250 dbgp.i_ep->driver_data = gadget;
251 i_desc.wMaxPacketSize =
252 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
254 dbgp.o_ep = usb_ep_autoconfig(gadget, &o_desc);
255 if (!dbgp.o_ep) {
256 dbgp.i_ep->driver_data = NULL;
257 stp = 2;
258 goto fail_2;
261 dbgp.o_ep->driver_data = gadget;
262 o_desc.wMaxPacketSize =
263 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
265 dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress;
266 dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress;
268 #ifdef CONFIG_USB_G_DBGP_SERIAL
269 dbgp.serial->in = dbgp.i_ep;
270 dbgp.serial->out = dbgp.o_ep;
272 dbgp.serial->in->desc = &i_desc;
273 dbgp.serial->out->desc = &o_desc;
275 if (gserial_setup(gadget, 1) < 0) {
276 stp = 3;
277 goto fail_3;
280 return 0;
282 fail_3:
283 dbgp.o_ep->driver_data = NULL;
284 #else
285 return 0;
286 #endif
287 fail_2:
288 dbgp.i_ep->driver_data = NULL;
289 fail_1:
290 dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp);
291 return -ENODEV;
294 static int __init dbgp_bind(struct usb_gadget *gadget)
296 int err, stp;
298 dbgp.gadget = gadget;
300 dbgp.req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
301 if (!dbgp.req) {
302 err = -ENOMEM;
303 stp = 1;
304 goto fail;
307 dbgp.req->buf = kmalloc(DBGP_REQ_EP0_LEN, GFP_KERNEL);
308 if (!dbgp.req->buf) {
309 err = -ENOMEM;
310 stp = 2;
311 goto fail;
314 dbgp.req->length = DBGP_REQ_EP0_LEN;
315 gadget->ep0->driver_data = gadget;
317 #ifdef CONFIG_USB_G_DBGP_SERIAL
318 dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL);
319 if (!dbgp.serial) {
320 stp = 3;
321 err = -ENOMEM;
322 goto fail;
324 #endif
325 err = dbgp_configure_endpoints(gadget);
326 if (err < 0) {
327 stp = 4;
328 goto fail;
331 dev_dbg(&dbgp.gadget->dev, "bind: success\n");
332 return 0;
334 fail:
335 dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err);
336 dbgp_unbind(gadget);
337 return err;
340 static void dbgp_setup_complete(struct usb_ep *ep,
341 struct usb_request *req)
343 dev_dbg(&dbgp.gadget->dev, "setup complete: %d, %d/%d\n",
344 req->status, req->actual, req->length);
347 static int dbgp_setup(struct usb_gadget *gadget,
348 const struct usb_ctrlrequest *ctrl)
350 struct usb_request *req = dbgp.req;
351 u8 request = ctrl->bRequest;
352 u16 value = le16_to_cpu(ctrl->wValue);
353 u16 length = le16_to_cpu(ctrl->wLength);
354 int err = -EOPNOTSUPP;
355 void *data = NULL;
356 u16 len = 0;
358 gadget->ep0->driver_data = gadget;
360 if (request == USB_REQ_GET_DESCRIPTOR) {
361 switch (value>>8) {
362 case USB_DT_DEVICE:
363 dev_dbg(&dbgp.gadget->dev, "setup: desc device\n");
364 len = sizeof device_desc;
365 data = &device_desc;
366 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
367 break;
368 case USB_DT_DEBUG:
369 dev_dbg(&dbgp.gadget->dev, "setup: desc debug\n");
370 len = sizeof dbg_desc;
371 data = &dbg_desc;
372 break;
373 default:
374 goto fail;
376 err = 0;
377 } else if (request == USB_REQ_SET_FEATURE &&
378 value == USB_DEVICE_DEBUG_MODE) {
379 dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n");
380 #ifdef CONFIG_USB_G_DBGP_PRINTK
381 err = dbgp_enable_ep();
382 #else
383 err = gserial_connect(dbgp.serial, 0);
384 #endif
385 if (err < 0)
386 goto fail;
387 } else
388 goto fail;
390 req->length = min(length, len);
391 req->zero = len < req->length;
392 if (data && req->length)
393 memcpy(req->buf, data, req->length);
395 req->complete = dbgp_setup_complete;
396 return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
398 fail:
399 dev_dbg(&dbgp.gadget->dev,
400 "setup: failure req %x v %x\n", request, value);
401 return err;
404 static struct usb_gadget_driver dbgp_driver = {
405 .function = "dbgp",
406 .speed = USB_SPEED_HIGH,
407 .unbind = dbgp_unbind,
408 .setup = dbgp_setup,
409 .disconnect = dbgp_disconnect,
410 .driver = {
411 .owner = THIS_MODULE,
412 .name = "dbgp"
416 static int __init dbgp_init(void)
418 return usb_gadget_probe_driver(&dbgp_driver, dbgp_bind);
421 static void __exit dbgp_exit(void)
423 usb_gadget_unregister_driver(&dbgp_driver);
424 #ifdef CONFIG_USB_G_DBGP_SERIAL
425 gserial_cleanup();
426 #endif
429 MODULE_AUTHOR("Stephane Duverger");
430 MODULE_LICENSE("GPL");
431 module_init(dbgp_init);
432 module_exit(dbgp_exit);