xref: /openbmc/linux/drivers/usb/gadget/legacy/dbgp.c (revision 1c2dd16a)
1 /*
2  * dbgp.c -- EHCI Debug Port device gadget
3  *
4  * Copyright (C) 2010 Stephane Duverger
5  *
6  * Released under the GPLv2.
7  */
8 
9 /* verbose messages */
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/usb/ch9.h>
14 #include <linux/usb/gadget.h>
15 
16 #include "u_serial.h"
17 
18 #define DRIVER_VENDOR_ID	0x0525 /* NetChip */
19 #define DRIVER_PRODUCT_ID	0xc0de /* undefined */
20 
21 #define USB_DEBUG_MAX_PACKET_SIZE     8
22 #define DBGP_REQ_EP0_LEN              128
23 #define DBGP_REQ_LEN                  512
24 
25 static struct dbgp {
26 	struct usb_gadget  *gadget;
27 	struct usb_request *req;
28 	struct usb_ep      *i_ep;
29 	struct usb_ep      *o_ep;
30 #ifdef CONFIG_USB_G_DBGP_SERIAL
31 	struct gserial     *serial;
32 #endif
33 } dbgp;
34 
35 static struct usb_device_descriptor device_desc = {
36 	.bLength = sizeof device_desc,
37 	.bDescriptorType = USB_DT_DEVICE,
38 	.bcdUSB = cpu_to_le16(0x0200),
39 	.bDeviceClass =	USB_CLASS_VENDOR_SPEC,
40 	.idVendor = cpu_to_le16(DRIVER_VENDOR_ID),
41 	.idProduct = cpu_to_le16(DRIVER_PRODUCT_ID),
42 	.bNumConfigurations = 1,
43 };
44 
45 static struct usb_debug_descriptor dbg_desc = {
46 	.bLength = sizeof dbg_desc,
47 	.bDescriptorType = USB_DT_DEBUG,
48 };
49 
50 static struct usb_endpoint_descriptor i_desc = {
51 	.bLength = USB_DT_ENDPOINT_SIZE,
52 	.bDescriptorType = USB_DT_ENDPOINT,
53 	.bmAttributes = USB_ENDPOINT_XFER_BULK,
54 	.bEndpointAddress = USB_DIR_IN,
55 };
56 
57 static struct usb_endpoint_descriptor o_desc = {
58 	.bLength = USB_DT_ENDPOINT_SIZE,
59 	.bDescriptorType = USB_DT_ENDPOINT,
60 	.bmAttributes = USB_ENDPOINT_XFER_BULK,
61 	.bEndpointAddress = USB_DIR_OUT,
62 };
63 
64 #ifdef CONFIG_USB_G_DBGP_PRINTK
65 static int dbgp_consume(char *buf, unsigned len)
66 {
67 	char c;
68 
69 	if (!len)
70 		return 0;
71 
72 	c = buf[len-1];
73 	if (c != 0)
74 		buf[len-1] = 0;
75 
76 	printk(KERN_NOTICE "%s%c", buf, c);
77 	return 0;
78 }
79 
80 static void __disable_ep(struct usb_ep *ep)
81 {
82 	usb_ep_disable(ep);
83 }
84 
85 static void dbgp_disable_ep(void)
86 {
87 	__disable_ep(dbgp.i_ep);
88 	__disable_ep(dbgp.o_ep);
89 }
90 
91 static void dbgp_complete(struct usb_ep *ep, struct usb_request *req)
92 {
93 	int stp;
94 	int err = 0;
95 	int status = req->status;
96 
97 	if (ep == dbgp.i_ep) {
98 		stp = 1;
99 		goto fail;
100 	}
101 
102 	if (status != 0) {
103 		stp = 2;
104 		goto release_req;
105 	}
106 
107 	dbgp_consume(req->buf, req->actual);
108 
109 	req->length = DBGP_REQ_LEN;
110 	err = usb_ep_queue(ep, req, GFP_ATOMIC);
111 	if (err < 0) {
112 		stp = 3;
113 		goto release_req;
114 	}
115 
116 	return;
117 
118 release_req:
119 	kfree(req->buf);
120 	usb_ep_free_request(dbgp.o_ep, req);
121 	dbgp_disable_ep();
122 fail:
123 	dev_dbg(&dbgp.gadget->dev,
124 		"complete: failure (%d:%d) ==> %d\n", stp, err, status);
125 }
126 
127 static int dbgp_enable_ep_req(struct usb_ep *ep)
128 {
129 	int err, stp;
130 	struct usb_request *req;
131 
132 	req = usb_ep_alloc_request(ep, GFP_KERNEL);
133 	if (!req) {
134 		err = -ENOMEM;
135 		stp = 1;
136 		goto fail_1;
137 	}
138 
139 	req->buf = kmalloc(DBGP_REQ_LEN, GFP_KERNEL);
140 	if (!req->buf) {
141 		err = -ENOMEM;
142 		stp = 2;
143 		goto fail_2;
144 	}
145 
146 	req->complete = dbgp_complete;
147 	req->length = DBGP_REQ_LEN;
148 	err = usb_ep_queue(ep, req, GFP_ATOMIC);
149 	if (err < 0) {
150 		stp = 3;
151 		goto fail_3;
152 	}
153 
154 	return 0;
155 
156 fail_3:
157 	kfree(req->buf);
158 fail_2:
159 	usb_ep_free_request(dbgp.o_ep, req);
160 fail_1:
161 	dev_dbg(&dbgp.gadget->dev,
162 		"enable ep req: failure (%d:%d)\n", stp, err);
163 	return err;
164 }
165 
166 static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc)
167 {
168 	int err;
169 	ep->desc = desc;
170 	err = usb_ep_enable(ep);
171 	return err;
172 }
173 
174 static int dbgp_enable_ep(void)
175 {
176 	int err, stp;
177 
178 	err = __enable_ep(dbgp.i_ep, &i_desc);
179 	if (err < 0) {
180 		stp = 1;
181 		goto fail_1;
182 	}
183 
184 	err = __enable_ep(dbgp.o_ep, &o_desc);
185 	if (err < 0) {
186 		stp = 2;
187 		goto fail_2;
188 	}
189 
190 	err = dbgp_enable_ep_req(dbgp.o_ep);
191 	if (err < 0) {
192 		stp = 3;
193 		goto fail_3;
194 	}
195 
196 	return 0;
197 
198 fail_3:
199 	__disable_ep(dbgp.o_ep);
200 fail_2:
201 	__disable_ep(dbgp.i_ep);
202 fail_1:
203 	dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err);
204 	return err;
205 }
206 #endif
207 
208 static void dbgp_disconnect(struct usb_gadget *gadget)
209 {
210 #ifdef CONFIG_USB_G_DBGP_PRINTK
211 	dbgp_disable_ep();
212 #else
213 	gserial_disconnect(dbgp.serial);
214 #endif
215 }
216 
217 static void dbgp_unbind(struct usb_gadget *gadget)
218 {
219 #ifdef CONFIG_USB_G_DBGP_SERIAL
220 	kfree(dbgp.serial);
221 	dbgp.serial = NULL;
222 #endif
223 	if (dbgp.req) {
224 		kfree(dbgp.req->buf);
225 		usb_ep_free_request(gadget->ep0, dbgp.req);
226 		dbgp.req = NULL;
227 	}
228 }
229 
230 #ifdef CONFIG_USB_G_DBGP_SERIAL
231 static unsigned char tty_line;
232 #endif
233 
234 static int dbgp_configure_endpoints(struct usb_gadget *gadget)
235 {
236 	int stp;
237 
238 	usb_ep_autoconfig_reset(gadget);
239 
240 	dbgp.i_ep = usb_ep_autoconfig(gadget, &i_desc);
241 	if (!dbgp.i_ep) {
242 		stp = 1;
243 		goto fail_1;
244 	}
245 
246 	i_desc.wMaxPacketSize =
247 		cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
248 
249 	dbgp.o_ep = usb_ep_autoconfig(gadget, &o_desc);
250 	if (!dbgp.o_ep) {
251 		stp = 2;
252 		goto fail_1;
253 	}
254 
255 	o_desc.wMaxPacketSize =
256 		cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
257 
258 	dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress;
259 	dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress;
260 
261 #ifdef CONFIG_USB_G_DBGP_SERIAL
262 	dbgp.serial->in = dbgp.i_ep;
263 	dbgp.serial->out = dbgp.o_ep;
264 
265 	dbgp.serial->in->desc = &i_desc;
266 	dbgp.serial->out->desc = &o_desc;
267 #endif
268 
269 	return 0;
270 
271 fail_1:
272 	dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp);
273 	return -ENODEV;
274 }
275 
276 static int dbgp_bind(struct usb_gadget *gadget,
277 		struct usb_gadget_driver *driver)
278 {
279 	int err, stp;
280 
281 	dbgp.gadget = gadget;
282 
283 	dbgp.req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
284 	if (!dbgp.req) {
285 		err = -ENOMEM;
286 		stp = 1;
287 		goto fail;
288 	}
289 
290 	dbgp.req->buf = kmalloc(DBGP_REQ_EP0_LEN, GFP_KERNEL);
291 	if (!dbgp.req->buf) {
292 		err = -ENOMEM;
293 		stp = 2;
294 		goto fail;
295 	}
296 
297 	dbgp.req->length = DBGP_REQ_EP0_LEN;
298 
299 #ifdef CONFIG_USB_G_DBGP_SERIAL
300 	dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL);
301 	if (!dbgp.serial) {
302 		stp = 3;
303 		err = -ENOMEM;
304 		goto fail;
305 	}
306 
307 	if (gserial_alloc_line(&tty_line)) {
308 		stp = 4;
309 		err = -ENODEV;
310 		goto fail;
311 	}
312 #endif
313 
314 	err = dbgp_configure_endpoints(gadget);
315 	if (err < 0) {
316 		stp = 5;
317 		goto fail;
318 	}
319 
320 	dev_dbg(&dbgp.gadget->dev, "bind: success\n");
321 	return 0;
322 
323 fail:
324 	dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err);
325 	dbgp_unbind(gadget);
326 	return err;
327 }
328 
329 static void dbgp_setup_complete(struct usb_ep *ep,
330 				struct usb_request *req)
331 {
332 	dev_dbg(&dbgp.gadget->dev, "setup complete: %d, %d/%d\n",
333 		req->status, req->actual, req->length);
334 }
335 
336 static int dbgp_setup(struct usb_gadget *gadget,
337 		      const struct usb_ctrlrequest *ctrl)
338 {
339 	struct usb_request *req = dbgp.req;
340 	u8 request = ctrl->bRequest;
341 	u16 value = le16_to_cpu(ctrl->wValue);
342 	u16 length = le16_to_cpu(ctrl->wLength);
343 	int err = -EOPNOTSUPP;
344 	void *data = NULL;
345 	u16 len = 0;
346 
347 	if (request == USB_REQ_GET_DESCRIPTOR) {
348 		switch (value>>8) {
349 		case USB_DT_DEVICE:
350 			dev_dbg(&dbgp.gadget->dev, "setup: desc device\n");
351 			len = sizeof device_desc;
352 			data = &device_desc;
353 			device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
354 			break;
355 		case USB_DT_DEBUG:
356 			dev_dbg(&dbgp.gadget->dev, "setup: desc debug\n");
357 			len = sizeof dbg_desc;
358 			data = &dbg_desc;
359 			break;
360 		default:
361 			goto fail;
362 		}
363 		err = 0;
364 	} else if (request == USB_REQ_SET_FEATURE &&
365 		   value == USB_DEVICE_DEBUG_MODE) {
366 		dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n");
367 #ifdef CONFIG_USB_G_DBGP_PRINTK
368 		err = dbgp_enable_ep();
369 #else
370 		err = dbgp_configure_endpoints(gadget);
371 		if (err < 0) {
372 			goto fail;
373 		}
374 		err = gserial_connect(dbgp.serial, tty_line);
375 #endif
376 		if (err < 0)
377 			goto fail;
378 	} else
379 		goto fail;
380 
381 	req->length = min(length, len);
382 	req->zero = len < req->length;
383 	if (data && req->length)
384 		memcpy(req->buf, data, req->length);
385 
386 	req->complete = dbgp_setup_complete;
387 	return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
388 
389 fail:
390 	dev_dbg(&dbgp.gadget->dev,
391 		"setup: failure req %x v %x\n", request, value);
392 	return err;
393 }
394 
395 static struct usb_gadget_driver dbgp_driver = {
396 	.function = "dbgp",
397 	.max_speed = USB_SPEED_HIGH,
398 	.bind = dbgp_bind,
399 	.unbind = dbgp_unbind,
400 	.setup = dbgp_setup,
401 	.reset = dbgp_disconnect,
402 	.disconnect = dbgp_disconnect,
403 	.driver	= {
404 		.owner = THIS_MODULE,
405 		.name = "dbgp"
406 	},
407 };
408 
409 static int __init dbgp_init(void)
410 {
411 	return usb_gadget_probe_driver(&dbgp_driver);
412 }
413 
414 static void __exit dbgp_exit(void)
415 {
416 	usb_gadget_unregister_driver(&dbgp_driver);
417 #ifdef CONFIG_USB_G_DBGP_SERIAL
418 	gserial_free_line(tty_line);
419 #endif
420 }
421 
422 MODULE_AUTHOR("Stephane Duverger");
423 MODULE_LICENSE("GPL");
424 module_init(dbgp_init);
425 module_exit(dbgp_exit);
426