xref: /openbmc/linux/drivers/usb/serial/generic.c (revision 78c99ba1)
1 /*
2  * USB Serial Converter Generic functions
3  *
4  * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License version
8  *	2 as published by the Free Software Foundation.
9  *
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
21 #include <linux/uaccess.h>
22 
23 
24 static int debug;
25 
26 #ifdef CONFIG_USB_SERIAL_GENERIC
27 
28 static int generic_probe(struct usb_interface *interface,
29 			 const struct usb_device_id *id);
30 
31 static __u16 vendor  = 0x05f9;
32 static __u16 product = 0xffff;
33 
34 module_param(vendor, ushort, 0);
35 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
36 
37 module_param(product, ushort, 0);
38 MODULE_PARM_DESC(product, "User specified USB idProduct");
39 
40 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
41 
42 /* we want to look at all devices, as the vendor/product id can change
43  * depending on the command line argument */
44 static struct usb_device_id generic_serial_ids[] = {
45 	{.driver_info = 42},
46 	{}
47 };
48 
49 static struct usb_driver generic_driver = {
50 	.name =		"usbserial_generic",
51 	.probe =	generic_probe,
52 	.disconnect =	usb_serial_disconnect,
53 	.id_table =	generic_serial_ids,
54 	.no_dynamic_id =	1,
55 };
56 
57 /* All of the device info needed for the Generic Serial Converter */
58 struct usb_serial_driver usb_serial_generic_device = {
59 	.driver = {
60 		.owner =	THIS_MODULE,
61 		.name =		"generic",
62 	},
63 	.id_table =		generic_device_ids,
64 	.usb_driver = 		&generic_driver,
65 	.num_ports =		1,
66 	.shutdown =		usb_serial_generic_shutdown,
67 	.throttle =		usb_serial_generic_throttle,
68 	.unthrottle =		usb_serial_generic_unthrottle,
69 	.resume =		usb_serial_generic_resume,
70 };
71 
72 static int generic_probe(struct usb_interface *interface,
73 			       const struct usb_device_id *id)
74 {
75 	const struct usb_device_id *id_pattern;
76 
77 	id_pattern = usb_match_id(interface, generic_device_ids);
78 	if (id_pattern != NULL)
79 		return usb_serial_probe(interface, id);
80 	return -ENODEV;
81 }
82 #endif
83 
84 int usb_serial_generic_register(int _debug)
85 {
86 	int retval = 0;
87 
88 	debug = _debug;
89 #ifdef CONFIG_USB_SERIAL_GENERIC
90 	generic_device_ids[0].idVendor = vendor;
91 	generic_device_ids[0].idProduct = product;
92 	generic_device_ids[0].match_flags =
93 		USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
94 
95 	/* register our generic driver with ourselves */
96 	retval = usb_serial_register(&usb_serial_generic_device);
97 	if (retval)
98 		goto exit;
99 	retval = usb_register(&generic_driver);
100 	if (retval)
101 		usb_serial_deregister(&usb_serial_generic_device);
102 exit:
103 #endif
104 	return retval;
105 }
106 
107 void usb_serial_generic_deregister(void)
108 {
109 #ifdef CONFIG_USB_SERIAL_GENERIC
110 	/* remove our generic driver */
111 	usb_deregister(&generic_driver);
112 	usb_serial_deregister(&usb_serial_generic_device);
113 #endif
114 }
115 
116 int usb_serial_generic_open(struct tty_struct *tty,
117 			struct usb_serial_port *port, struct file *filp)
118 {
119 	struct usb_serial *serial = port->serial;
120 	int result = 0;
121 	unsigned long flags;
122 
123 	dbg("%s - port %d", __func__, port->number);
124 
125 	/* clear the throttle flags */
126 	spin_lock_irqsave(&port->lock, flags);
127 	port->throttled = 0;
128 	port->throttle_req = 0;
129 	spin_unlock_irqrestore(&port->lock, flags);
130 
131 	/* if we have a bulk endpoint, start reading from it */
132 	if (serial->num_bulk_in) {
133 		/* Start reading from the device */
134 		usb_fill_bulk_urb(port->read_urb, serial->dev,
135 				   usb_rcvbulkpipe(serial->dev,
136 						port->bulk_in_endpointAddress),
137 				   port->read_urb->transfer_buffer,
138 				   port->read_urb->transfer_buffer_length,
139 				   ((serial->type->read_bulk_callback) ?
140 				     serial->type->read_bulk_callback :
141 				     usb_serial_generic_read_bulk_callback),
142 				   port);
143 		result = usb_submit_urb(port->read_urb, GFP_KERNEL);
144 		if (result)
145 			dev_err(&port->dev,
146 			    "%s - failed resubmitting read urb, error %d\n",
147 							__func__, result);
148 	}
149 
150 	return result;
151 }
152 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
153 
154 static void generic_cleanup(struct usb_serial_port *port)
155 {
156 	struct usb_serial *serial = port->serial;
157 
158 	dbg("%s - port %d", __func__, port->number);
159 
160 	if (serial->dev) {
161 		/* shutdown any bulk reads that might be going on */
162 		if (serial->num_bulk_out)
163 			usb_kill_urb(port->write_urb);
164 		if (serial->num_bulk_in)
165 			usb_kill_urb(port->read_urb);
166 	}
167 }
168 
169 int usb_serial_generic_resume(struct usb_serial *serial)
170 {
171 	struct usb_serial_port *port;
172 	int i, c = 0, r;
173 
174 	for (i = 0; i < serial->num_ports; i++) {
175 		port = serial->port[i];
176 		if (port->port.count && port->read_urb) {
177 			r = usb_submit_urb(port->read_urb, GFP_NOIO);
178 			if (r < 0)
179 				c++;
180 		}
181 	}
182 
183 	return c ? -EIO : 0;
184 }
185 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
186 
187 void usb_serial_generic_close(struct usb_serial_port *port)
188 {
189 	dbg("%s - port %d", __func__, port->number);
190 	generic_cleanup(port);
191 }
192 
193 int usb_serial_generic_write(struct tty_struct *tty,
194 	struct usb_serial_port *port, const unsigned char *buf, int count)
195 {
196 	struct usb_serial *serial = port->serial;
197 	int result;
198 	unsigned char *data;
199 
200 	dbg("%s - port %d", __func__, port->number);
201 
202 	if (count == 0) {
203 		dbg("%s - write request of 0 bytes", __func__);
204 		return 0;
205 	}
206 
207 	/* only do something if we have a bulk out endpoint */
208 	if (serial->num_bulk_out) {
209 		unsigned long flags;
210 		spin_lock_irqsave(&port->lock, flags);
211 		if (port->write_urb_busy) {
212 			spin_unlock_irqrestore(&port->lock, flags);
213 			dbg("%s - already writing", __func__);
214 			return 0;
215 		}
216 		port->write_urb_busy = 1;
217 		spin_unlock_irqrestore(&port->lock, flags);
218 
219 		count = (count > port->bulk_out_size) ?
220 					port->bulk_out_size : count;
221 
222 		memcpy(port->write_urb->transfer_buffer, buf, count);
223 		data = port->write_urb->transfer_buffer;
224 		usb_serial_debug_data(debug, &port->dev, __func__, count, data);
225 
226 		/* set up our urb */
227 		usb_fill_bulk_urb(port->write_urb, serial->dev,
228 				   usb_sndbulkpipe(serial->dev,
229 					port->bulk_out_endpointAddress),
230 				   port->write_urb->transfer_buffer, count,
231 				   ((serial->type->write_bulk_callback) ?
232 				     serial->type->write_bulk_callback :
233 				     usb_serial_generic_write_bulk_callback),
234 				   port);
235 
236 		/* send the data out the bulk port */
237 		port->write_urb_busy = 1;
238 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
239 		if (result) {
240 			dev_err(&port->dev,
241 				"%s - failed submitting write urb, error %d\n",
242 							__func__, result);
243 			/* don't have to grab the lock here, as we will
244 			   retry if != 0 */
245 			port->write_urb_busy = 0;
246 		} else
247 			result = count;
248 
249 		return result;
250 	}
251 
252 	/* no bulk out, so return 0 bytes written */
253 	return 0;
254 }
255 
256 int usb_serial_generic_write_room(struct tty_struct *tty)
257 {
258 	struct usb_serial_port *port = tty->driver_data;
259 	struct usb_serial *serial = port->serial;
260 	int room = 0;
261 
262 	dbg("%s - port %d", __func__, port->number);
263 
264 	/* FIXME: Locking */
265 	if (serial->num_bulk_out) {
266 		if (!(port->write_urb_busy))
267 			room = port->bulk_out_size;
268 	}
269 
270 	dbg("%s - returns %d", __func__, room);
271 	return room;
272 }
273 
274 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
275 {
276 	struct usb_serial_port *port = tty->driver_data;
277 	struct usb_serial *serial = port->serial;
278 	int chars = 0;
279 
280 	dbg("%s - port %d", __func__, port->number);
281 
282 	/* FIXME: Locking */
283 	if (serial->num_bulk_out) {
284 		if (port->write_urb_busy)
285 			chars = port->write_urb->transfer_buffer_length;
286 	}
287 
288 	dbg("%s - returns %d", __func__, chars);
289 	return chars;
290 }
291 
292 
293 static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
294 {
295 	struct urb *urb = port->read_urb;
296 	struct usb_serial *serial = port->serial;
297 	int result;
298 
299 	/* Continue reading from device */
300 	usb_fill_bulk_urb(urb, serial->dev,
301 			   usb_rcvbulkpipe(serial->dev,
302 					port->bulk_in_endpointAddress),
303 			   urb->transfer_buffer,
304 			   urb->transfer_buffer_length,
305 			   ((serial->type->read_bulk_callback) ?
306 			     serial->type->read_bulk_callback :
307 			     usb_serial_generic_read_bulk_callback), port);
308 	result = usb_submit_urb(urb, mem_flags);
309 	if (result)
310 		dev_err(&port->dev,
311 			"%s - failed resubmitting read urb, error %d\n",
312 							__func__, result);
313 }
314 
315 /* Push data to tty layer and resubmit the bulk read URB */
316 static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
317 {
318 	struct urb *urb = port->read_urb;
319 	struct tty_struct *tty = tty_port_tty_get(&port->port);
320 	int room;
321 
322 	/* Push data to tty */
323 	if (tty && urb->actual_length) {
324 		room = tty_buffer_request_room(tty, urb->actual_length);
325 		if (room) {
326 			tty_insert_flip_string(tty, urb->transfer_buffer, room);
327 			tty_flip_buffer_push(tty);
328 		}
329 	}
330 	tty_kref_put(tty);
331 
332 	resubmit_read_urb(port, GFP_ATOMIC);
333 }
334 
335 void usb_serial_generic_read_bulk_callback(struct urb *urb)
336 {
337 	struct usb_serial_port *port = urb->context;
338 	unsigned char *data = urb->transfer_buffer;
339 	int status = urb->status;
340 	unsigned long flags;
341 
342 	dbg("%s - port %d", __func__, port->number);
343 
344 	if (unlikely(status != 0)) {
345 		dbg("%s - nonzero read bulk status received: %d",
346 		    __func__, status);
347 		return;
348 	}
349 
350 	usb_serial_debug_data(debug, &port->dev, __func__,
351 						urb->actual_length, data);
352 
353 	/* Throttle the device if requested by tty */
354 	spin_lock_irqsave(&port->lock, flags);
355 	port->throttled = port->throttle_req;
356 	if (!port->throttled) {
357 		spin_unlock_irqrestore(&port->lock, flags);
358 		flush_and_resubmit_read_urb(port);
359 	} else
360 		spin_unlock_irqrestore(&port->lock, flags);
361 }
362 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
363 
364 void usb_serial_generic_write_bulk_callback(struct urb *urb)
365 {
366 	struct usb_serial_port *port = urb->context;
367 	int status = urb->status;
368 
369 	dbg("%s - port %d", __func__, port->number);
370 
371 	port->write_urb_busy = 0;
372 	if (status) {
373 		dbg("%s - nonzero write bulk status received: %d",
374 		    __func__, status);
375 		return;
376 	}
377 	usb_serial_port_softint(port);
378 }
379 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
380 
381 void usb_serial_generic_throttle(struct tty_struct *tty)
382 {
383 	struct usb_serial_port *port = tty->driver_data;
384 	unsigned long flags;
385 
386 	dbg("%s - port %d", __func__, port->number);
387 
388 	/* Set the throttle request flag. It will be picked up
389 	 * by usb_serial_generic_read_bulk_callback(). */
390 	spin_lock_irqsave(&port->lock, flags);
391 	port->throttle_req = 1;
392 	spin_unlock_irqrestore(&port->lock, flags);
393 }
394 
395 void usb_serial_generic_unthrottle(struct tty_struct *tty)
396 {
397 	struct usb_serial_port *port = tty->driver_data;
398 	int was_throttled;
399 	unsigned long flags;
400 
401 	dbg("%s - port %d", __func__, port->number);
402 
403 	/* Clear the throttle flags */
404 	spin_lock_irqsave(&port->lock, flags);
405 	was_throttled = port->throttled;
406 	port->throttled = port->throttle_req = 0;
407 	spin_unlock_irqrestore(&port->lock, flags);
408 
409 	if (was_throttled) {
410 		/* Resume reading from device */
411 		resubmit_read_urb(port, GFP_KERNEL);
412 	}
413 }
414 
415 void usb_serial_generic_shutdown(struct usb_serial *serial)
416 {
417 	int i;
418 
419 	dbg("%s", __func__);
420 
421 	/* stop reads and writes on all ports */
422 	for (i = 0; i < serial->num_ports; ++i)
423 		generic_cleanup(serial->port[i]);
424 }
425 
426