Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Opticon USB barcode to serial driver
  4 *
  5 * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com>
  6 * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
  7 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
  8 * Copyright (C) 2008 - 2009 Novell Inc.
  9 */
 10
 11#include <linux/kernel.h>
 12#include <linux/tty.h>
 13#include <linux/tty_driver.h>
 14#include <linux/slab.h>
 15#include <linux/tty_flip.h>
 16#include <linux/serial.h>
 17#include <linux/module.h>
 18#include <linux/usb.h>
 19#include <linux/usb/serial.h>
 20#include <linux/uaccess.h>
 21
 22#define CONTROL_RTS			0x02
 23#define RESEND_CTS_STATE	0x03
 24
 25/* max number of write urbs in flight */
 26#define URB_UPPER_LIMIT	8
 27
 28/* This driver works for the Opticon 1D barcode reader
 29 * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
 30#define DRIVER_DESC	"Opticon USB barcode to serial driver (1D)"
 31
 32static const struct usb_device_id id_table[] = {
 33	{ USB_DEVICE(0x065a, 0x0009) },
 34	{ },
 35};
 36MODULE_DEVICE_TABLE(usb, id_table);
 37
 38/* This structure holds all of the individual device information */
 39struct opticon_private {
 40	spinlock_t lock;	/* protects the following flags */
 41	bool rts;
 42	bool cts;
 43	int outstanding_urbs;
 44	int outstanding_bytes;
 45
 46	struct usb_anchor anchor;
 47};
 48
 49
 50static void opticon_process_data_packet(struct usb_serial_port *port,
 51					const unsigned char *buf, size_t len)
 52{
 53	tty_insert_flip_string(&port->port, buf, len);
 54	tty_flip_buffer_push(&port->port);
 55}
 56
 57static void opticon_process_status_packet(struct usb_serial_port *port,
 58					const unsigned char *buf, size_t len)
 59{
 60	struct opticon_private *priv = usb_get_serial_port_data(port);
 61	unsigned long flags;
 62
 63	spin_lock_irqsave(&priv->lock, flags);
 64	if (buf[0] == 0x00)
 65		priv->cts = false;
 66	else
 67		priv->cts = true;
 68	spin_unlock_irqrestore(&priv->lock, flags);
 69}
 70
 71static void opticon_process_read_urb(struct urb *urb)
 72{
 73	struct usb_serial_port *port = urb->context;
 74	const unsigned char *hdr = urb->transfer_buffer;
 75	const unsigned char *data = hdr + 2;
 76	size_t data_len = urb->actual_length - 2;
 77
 78	if (urb->actual_length <= 2) {
 79		dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
 80							urb->actual_length);
 81		return;
 82	}
 83	/*
 84	 * Data from the device comes with a 2 byte header:
 85	 *
 86	 * <0x00><0x00>data...
 87	 *      This is real data to be sent to the tty layer
 88	 * <0x00><0x01>level
 89	 *      This is a CTS level change, the third byte is the CTS
 90	 *      value (0 for low, 1 for high).
 91	 */
 92	if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
 93		opticon_process_data_packet(port, data, data_len);
 94	} else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
 95		opticon_process_status_packet(port, data, data_len);
 96	} else {
 97		dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
 98							hdr[0], hdr[1]);
 99	}
100}
101
102static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
103				u8 val)
104{
105	struct usb_serial *serial = port->serial;
106	int retval;
107	u8 *buffer;
108
109	buffer = kzalloc(1, GFP_KERNEL);
110	if (!buffer)
111		return -ENOMEM;
112
113	buffer[0] = val;
114	/* Send the message to the vendor control endpoint
115	 * of the connected device */
116	retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
117				requesttype,
118				USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
119				0, 0, buffer, 1, USB_CTRL_SET_TIMEOUT);
120	kfree(buffer);
121
122	if (retval < 0)
123		return retval;
124
125	return 0;
126}
127
128static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
129{
130	struct opticon_private *priv = usb_get_serial_port_data(port);
131	unsigned long flags;
132	int res;
133
134	spin_lock_irqsave(&priv->lock, flags);
135	priv->rts = false;
136	spin_unlock_irqrestore(&priv->lock, flags);
137
138	/* Clear RTS line */
139	send_control_msg(port, CONTROL_RTS, 0);
140
141	/* clear the halt status of the endpoint */
142	usb_clear_halt(port->serial->dev, port->read_urb->pipe);
143
144	res = usb_serial_generic_open(tty, port);
145	if (res)
146		return res;
147
148	/* Request CTS line state, sometimes during opening the current
149	 * CTS state can be missed. */
150	send_control_msg(port, RESEND_CTS_STATE, 1);
151
152	return res;
153}
154
155static void opticon_close(struct usb_serial_port *port)
156{
157	struct opticon_private *priv = usb_get_serial_port_data(port);
158
159	usb_kill_anchored_urbs(&priv->anchor);
160
161	usb_serial_generic_close(port);
162}
163
164static void opticon_write_control_callback(struct urb *urb)
165{
166	struct usb_serial_port *port = urb->context;
167	struct opticon_private *priv = usb_get_serial_port_data(port);
168	int status = urb->status;
169	unsigned long flags;
170
171	/* free up the transfer buffer, as usb_free_urb() does not do this */
172	kfree(urb->transfer_buffer);
173
174	/* setup packet may be set if we're using it for writing */
175	kfree(urb->setup_packet);
176
177	if (status)
178		dev_dbg(&port->dev,
179			"%s - non-zero urb status received: %d\n",
180			__func__, status);
181
182	spin_lock_irqsave(&priv->lock, flags);
183	--priv->outstanding_urbs;
184	priv->outstanding_bytes -= urb->transfer_buffer_length;
185	spin_unlock_irqrestore(&priv->lock, flags);
186
187	usb_serial_port_softint(port);
188}
189
190static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
191			 const unsigned char *buf, int count)
192{
193	struct opticon_private *priv = usb_get_serial_port_data(port);
194	struct usb_serial *serial = port->serial;
195	struct urb *urb;
196	unsigned char *buffer;
197	unsigned long flags;
 
198	struct usb_ctrlrequest *dr;
199	int ret = -ENOMEM;
200
201	spin_lock_irqsave(&priv->lock, flags);
202	if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
203		spin_unlock_irqrestore(&priv->lock, flags);
204		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
205		return 0;
206	}
207	priv->outstanding_urbs++;
208	priv->outstanding_bytes += count;
209	spin_unlock_irqrestore(&priv->lock, flags);
210
211	buffer = kmemdup(buf, count, GFP_ATOMIC);
212	if (!buffer)
 
213		goto error_no_buffer;
 
214
215	urb = usb_alloc_urb(0, GFP_ATOMIC);
216	if (!urb)
 
217		goto error_no_urb;
 
 
 
218
219	usb_serial_debug_data(&port->dev, __func__, count, buffer);
220
221	/* The connected devices do not have a bulk write endpoint,
222	 * to transmit data to de barcode device the control endpoint is used */
223	dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
224	if (!dr)
 
225		goto error_no_dr;
 
226
227	dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
228	dr->bRequest = 0x01;
229	dr->wValue = 0;
230	dr->wIndex = 0;
231	dr->wLength = cpu_to_le16(count);
232
233	usb_fill_control_urb(urb, serial->dev,
234		usb_sndctrlpipe(serial->dev, 0),
235		(unsigned char *)dr, buffer, count,
236		opticon_write_control_callback, port);
237
238	usb_anchor_urb(urb, &priv->anchor);
239
240	/* send it down the pipe */
241	ret = usb_submit_urb(urb, GFP_ATOMIC);
242	if (ret) {
243		dev_err(&port->dev, "failed to submit write urb: %d\n", ret);
244		usb_unanchor_urb(urb);
 
 
245		goto error;
246	}
247
248	/* we are done with this urb, so let the host driver
249	 * really free it when it is finished with it */
250	usb_free_urb(urb);
251
252	return count;
253error:
254	kfree(dr);
255error_no_dr:
256	usb_free_urb(urb);
257error_no_urb:
258	kfree(buffer);
259error_no_buffer:
260	spin_lock_irqsave(&priv->lock, flags);
261	--priv->outstanding_urbs;
262	priv->outstanding_bytes -= count;
263	spin_unlock_irqrestore(&priv->lock, flags);
264
265	return ret;
266}
267
268static unsigned int opticon_write_room(struct tty_struct *tty)
269{
270	struct usb_serial_port *port = tty->driver_data;
271	struct opticon_private *priv = usb_get_serial_port_data(port);
272	unsigned long flags;
273
274	/*
275	 * We really can take almost anything the user throws at us
276	 * but let's pick a nice big number to tell the tty
277	 * layer that we have lots of free space, unless we don't.
278	 */
279	spin_lock_irqsave(&priv->lock, flags);
280	if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
281		spin_unlock_irqrestore(&priv->lock, flags);
282		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
283		return 0;
284	}
285	spin_unlock_irqrestore(&priv->lock, flags);
286
287	return 2048;
288}
289
290static unsigned int opticon_chars_in_buffer(struct tty_struct *tty)
291{
292	struct usb_serial_port *port = tty->driver_data;
293	struct opticon_private *priv = usb_get_serial_port_data(port);
294	unsigned long flags;
295	unsigned int count;
296
297	spin_lock_irqsave(&priv->lock, flags);
298	count = priv->outstanding_bytes;
299	spin_unlock_irqrestore(&priv->lock, flags);
300
301	return count;
302}
303
304static int opticon_tiocmget(struct tty_struct *tty)
305{
306	struct usb_serial_port *port = tty->driver_data;
307	struct opticon_private *priv = usb_get_serial_port_data(port);
308	unsigned long flags;
309	int result = 0;
310
311	spin_lock_irqsave(&priv->lock, flags);
312	if (priv->rts)
313		result |= TIOCM_RTS;
314	if (priv->cts)
315		result |= TIOCM_CTS;
316	spin_unlock_irqrestore(&priv->lock, flags);
317
318	dev_dbg(&port->dev, "%s - %x\n", __func__, result);
319	return result;
320}
321
322static int opticon_tiocmset(struct tty_struct *tty,
323			   unsigned int set, unsigned int clear)
324{
325	struct usb_serial_port *port = tty->driver_data;
326	struct opticon_private *priv = usb_get_serial_port_data(port);
327	unsigned long flags;
328	bool rts;
329	bool changed = false;
330	int ret;
331
332	/* We only support RTS so we only handle that */
333	spin_lock_irqsave(&priv->lock, flags);
334
335	rts = priv->rts;
336	if (set & TIOCM_RTS)
337		priv->rts = true;
338	if (clear & TIOCM_RTS)
339		priv->rts = false;
340	changed = rts ^ priv->rts;
341	spin_unlock_irqrestore(&priv->lock, flags);
342
343	if (!changed)
344		return 0;
345
346	ret = send_control_msg(port, CONTROL_RTS, !rts);
347	if (ret)
348		return usb_translate_errors(ret);
349
350	return 0;
351}
352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353static int opticon_port_probe(struct usb_serial_port *port)
354{
355	struct opticon_private *priv;
356
357	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
358	if (!priv)
359		return -ENOMEM;
360
361	spin_lock_init(&priv->lock);
362	init_usb_anchor(&priv->anchor);
363
364	usb_set_serial_port_data(port, priv);
365
366	return 0;
367}
368
369static void opticon_port_remove(struct usb_serial_port *port)
370{
371	struct opticon_private *priv = usb_get_serial_port_data(port);
372
373	kfree(priv);
 
 
374}
375
376static struct usb_serial_driver opticon_device = {
377	.driver = {
378		.owner =	THIS_MODULE,
379		.name =		"opticon",
380	},
381	.id_table =		id_table,
382	.num_ports =		1,
383	.num_bulk_in =		1,
384	.bulk_in_size =		256,
385	.port_probe =		opticon_port_probe,
386	.port_remove =		opticon_port_remove,
387	.open =			opticon_open,
388	.close =		opticon_close,
389	.write =		opticon_write,
390	.write_room = 		opticon_write_room,
391	.chars_in_buffer =	opticon_chars_in_buffer,
392	.throttle =		usb_serial_generic_throttle,
393	.unthrottle =		usb_serial_generic_unthrottle,
 
394	.tiocmget =		opticon_tiocmget,
395	.tiocmset =		opticon_tiocmset,
396	.process_read_urb =	opticon_process_read_urb,
397};
398
399static struct usb_serial_driver * const serial_drivers[] = {
400	&opticon_device, NULL
401};
402
403module_usb_serial_driver(serial_drivers, id_table);
404
405MODULE_DESCRIPTION(DRIVER_DESC);
406MODULE_LICENSE("GPL v2");
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Opticon USB barcode to serial driver
  4 *
  5 * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com>
  6 * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
  7 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
  8 * Copyright (C) 2008 - 2009 Novell Inc.
  9 */
 10
 11#include <linux/kernel.h>
 12#include <linux/tty.h>
 13#include <linux/tty_driver.h>
 14#include <linux/slab.h>
 15#include <linux/tty_flip.h>
 16#include <linux/serial.h>
 17#include <linux/module.h>
 18#include <linux/usb.h>
 19#include <linux/usb/serial.h>
 20#include <linux/uaccess.h>
 21
 22#define CONTROL_RTS			0x02
 23#define RESEND_CTS_STATE	0x03
 24
 25/* max number of write urbs in flight */
 26#define URB_UPPER_LIMIT	8
 27
 28/* This driver works for the Opticon 1D barcode reader
 29 * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
 30#define DRIVER_DESC	"Opticon USB barcode to serial driver (1D)"
 31
 32static const struct usb_device_id id_table[] = {
 33	{ USB_DEVICE(0x065a, 0x0009) },
 34	{ },
 35};
 36MODULE_DEVICE_TABLE(usb, id_table);
 37
 38/* This structure holds all of the individual device information */
 39struct opticon_private {
 40	spinlock_t lock;	/* protects the following flags */
 41	bool rts;
 42	bool cts;
 43	int outstanding_urbs;
 
 
 
 44};
 45
 46
 47static void opticon_process_data_packet(struct usb_serial_port *port,
 48					const unsigned char *buf, size_t len)
 49{
 50	tty_insert_flip_string(&port->port, buf, len);
 51	tty_flip_buffer_push(&port->port);
 52}
 53
 54static void opticon_process_status_packet(struct usb_serial_port *port,
 55					const unsigned char *buf, size_t len)
 56{
 57	struct opticon_private *priv = usb_get_serial_port_data(port);
 58	unsigned long flags;
 59
 60	spin_lock_irqsave(&priv->lock, flags);
 61	if (buf[0] == 0x00)
 62		priv->cts = false;
 63	else
 64		priv->cts = true;
 65	spin_unlock_irqrestore(&priv->lock, flags);
 66}
 67
 68static void opticon_process_read_urb(struct urb *urb)
 69{
 70	struct usb_serial_port *port = urb->context;
 71	const unsigned char *hdr = urb->transfer_buffer;
 72	const unsigned char *data = hdr + 2;
 73	size_t data_len = urb->actual_length - 2;
 74
 75	if (urb->actual_length <= 2) {
 76		dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
 77							urb->actual_length);
 78		return;
 79	}
 80	/*
 81	 * Data from the device comes with a 2 byte header:
 82	 *
 83	 * <0x00><0x00>data...
 84	 *      This is real data to be sent to the tty layer
 85	 * <0x00><0x01>level
 86	 *      This is a CTS level change, the third byte is the CTS
 87	 *      value (0 for low, 1 for high).
 88	 */
 89	if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
 90		opticon_process_data_packet(port, data, data_len);
 91	} else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
 92		opticon_process_status_packet(port, data, data_len);
 93	} else {
 94		dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
 95							hdr[0], hdr[1]);
 96	}
 97}
 98
 99static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
100				u8 val)
101{
102	struct usb_serial *serial = port->serial;
103	int retval;
104	u8 *buffer;
105
106	buffer = kzalloc(1, GFP_KERNEL);
107	if (!buffer)
108		return -ENOMEM;
109
110	buffer[0] = val;
111	/* Send the message to the vendor control endpoint
112	 * of the connected device */
113	retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
114				requesttype,
115				USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
116				0, 0, buffer, 1, 0);
117	kfree(buffer);
118
119	if (retval < 0)
120		return retval;
121
122	return 0;
123}
124
125static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
126{
127	struct opticon_private *priv = usb_get_serial_port_data(port);
128	unsigned long flags;
129	int res;
130
131	spin_lock_irqsave(&priv->lock, flags);
132	priv->rts = false;
133	spin_unlock_irqrestore(&priv->lock, flags);
134
135	/* Clear RTS line */
136	send_control_msg(port, CONTROL_RTS, 0);
137
138	/* clear the halt status of the endpoint */
139	usb_clear_halt(port->serial->dev, port->read_urb->pipe);
140
141	res = usb_serial_generic_open(tty, port);
142	if (res)
143		return res;
144
145	/* Request CTS line state, sometimes during opening the current
146	 * CTS state can be missed. */
147	send_control_msg(port, RESEND_CTS_STATE, 1);
148
149	return res;
150}
151
 
 
 
 
 
 
 
 
 
152static void opticon_write_control_callback(struct urb *urb)
153{
154	struct usb_serial_port *port = urb->context;
155	struct opticon_private *priv = usb_get_serial_port_data(port);
156	int status = urb->status;
157	unsigned long flags;
158
159	/* free up the transfer buffer, as usb_free_urb() does not do this */
160	kfree(urb->transfer_buffer);
161
162	/* setup packet may be set if we're using it for writing */
163	kfree(urb->setup_packet);
164
165	if (status)
166		dev_dbg(&port->dev,
167			"%s - non-zero urb status received: %d\n",
168			__func__, status);
169
170	spin_lock_irqsave(&priv->lock, flags);
171	--priv->outstanding_urbs;
 
172	spin_unlock_irqrestore(&priv->lock, flags);
173
174	usb_serial_port_softint(port);
175}
176
177static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
178			 const unsigned char *buf, int count)
179{
180	struct opticon_private *priv = usb_get_serial_port_data(port);
181	struct usb_serial *serial = port->serial;
182	struct urb *urb;
183	unsigned char *buffer;
184	unsigned long flags;
185	int status;
186	struct usb_ctrlrequest *dr;
 
187
188	spin_lock_irqsave(&priv->lock, flags);
189	if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
190		spin_unlock_irqrestore(&priv->lock, flags);
191		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
192		return 0;
193	}
194	priv->outstanding_urbs++;
 
195	spin_unlock_irqrestore(&priv->lock, flags);
196
197	buffer = kmalloc(count, GFP_ATOMIC);
198	if (!buffer) {
199		count = -ENOMEM;
200		goto error_no_buffer;
201	}
202
203	urb = usb_alloc_urb(0, GFP_ATOMIC);
204	if (!urb) {
205		count = -ENOMEM;
206		goto error_no_urb;
207	}
208
209	memcpy(buffer, buf, count);
210
211	usb_serial_debug_data(&port->dev, __func__, count, buffer);
212
213	/* The connected devices do not have a bulk write endpoint,
214	 * to transmit data to de barcode device the control endpoint is used */
215	dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
216	if (!dr) {
217		count = -ENOMEM;
218		goto error_no_dr;
219	}
220
221	dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
222	dr->bRequest = 0x01;
223	dr->wValue = 0;
224	dr->wIndex = 0;
225	dr->wLength = cpu_to_le16(count);
226
227	usb_fill_control_urb(urb, serial->dev,
228		usb_sndctrlpipe(serial->dev, 0),
229		(unsigned char *)dr, buffer, count,
230		opticon_write_control_callback, port);
231
 
 
232	/* send it down the pipe */
233	status = usb_submit_urb(urb, GFP_ATOMIC);
234	if (status) {
235		dev_err(&port->dev,
236		"%s - usb_submit_urb(write endpoint) failed status = %d\n",
237							__func__, status);
238		count = status;
239		goto error;
240	}
241
242	/* we are done with this urb, so let the host driver
243	 * really free it when it is finished with it */
244	usb_free_urb(urb);
245
246	return count;
247error:
248	kfree(dr);
249error_no_dr:
250	usb_free_urb(urb);
251error_no_urb:
252	kfree(buffer);
253error_no_buffer:
254	spin_lock_irqsave(&priv->lock, flags);
255	--priv->outstanding_urbs;
 
256	spin_unlock_irqrestore(&priv->lock, flags);
257	return count;
 
258}
259
260static int opticon_write_room(struct tty_struct *tty)
261{
262	struct usb_serial_port *port = tty->driver_data;
263	struct opticon_private *priv = usb_get_serial_port_data(port);
264	unsigned long flags;
265
266	/*
267	 * We really can take almost anything the user throws at us
268	 * but let's pick a nice big number to tell the tty
269	 * layer that we have lots of free space, unless we don't.
270	 */
271	spin_lock_irqsave(&priv->lock, flags);
272	if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
273		spin_unlock_irqrestore(&priv->lock, flags);
274		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
275		return 0;
276	}
277	spin_unlock_irqrestore(&priv->lock, flags);
278
279	return 2048;
280}
281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282static int opticon_tiocmget(struct tty_struct *tty)
283{
284	struct usb_serial_port *port = tty->driver_data;
285	struct opticon_private *priv = usb_get_serial_port_data(port);
286	unsigned long flags;
287	int result = 0;
288
289	spin_lock_irqsave(&priv->lock, flags);
290	if (priv->rts)
291		result |= TIOCM_RTS;
292	if (priv->cts)
293		result |= TIOCM_CTS;
294	spin_unlock_irqrestore(&priv->lock, flags);
295
296	dev_dbg(&port->dev, "%s - %x\n", __func__, result);
297	return result;
298}
299
300static int opticon_tiocmset(struct tty_struct *tty,
301			   unsigned int set, unsigned int clear)
302{
303	struct usb_serial_port *port = tty->driver_data;
304	struct opticon_private *priv = usb_get_serial_port_data(port);
305	unsigned long flags;
306	bool rts;
307	bool changed = false;
308	int ret;
309
310	/* We only support RTS so we only handle that */
311	spin_lock_irqsave(&priv->lock, flags);
312
313	rts = priv->rts;
314	if (set & TIOCM_RTS)
315		priv->rts = true;
316	if (clear & TIOCM_RTS)
317		priv->rts = false;
318	changed = rts ^ priv->rts;
319	spin_unlock_irqrestore(&priv->lock, flags);
320
321	if (!changed)
322		return 0;
323
324	ret = send_control_msg(port, CONTROL_RTS, !rts);
325	if (ret)
326		return usb_translate_errors(ret);
327
328	return 0;
329}
330
331static int get_serial_info(struct usb_serial_port *port,
332			   struct serial_struct __user *serial)
333{
334	struct serial_struct tmp;
335
336	memset(&tmp, 0x00, sizeof(tmp));
337
338	/* fake emulate a 16550 uart to make userspace code happy */
339	tmp.type		= PORT_16550A;
340	tmp.line		= port->minor;
341	tmp.port		= 0;
342	tmp.irq			= 0;
343	tmp.xmit_fifo_size	= 1024;
344	tmp.baud_base		= 9600;
345	tmp.close_delay		= 5*HZ;
346	tmp.closing_wait	= 30*HZ;
347
348	if (copy_to_user(serial, &tmp, sizeof(*serial)))
349		return -EFAULT;
350	return 0;
351}
352
353static int opticon_ioctl(struct tty_struct *tty,
354			 unsigned int cmd, unsigned long arg)
355{
356	struct usb_serial_port *port = tty->driver_data;
357
358	switch (cmd) {
359	case TIOCGSERIAL:
360		return get_serial_info(port,
361				       (struct serial_struct __user *)arg);
362	}
363
364	return -ENOIOCTLCMD;
365}
366
367static int opticon_port_probe(struct usb_serial_port *port)
368{
369	struct opticon_private *priv;
370
371	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
372	if (!priv)
373		return -ENOMEM;
374
375	spin_lock_init(&priv->lock);
 
376
377	usb_set_serial_port_data(port, priv);
378
379	return 0;
380}
381
382static int opticon_port_remove(struct usb_serial_port *port)
383{
384	struct opticon_private *priv = usb_get_serial_port_data(port);
385
386	kfree(priv);
387
388	return 0;
389}
390
391static struct usb_serial_driver opticon_device = {
392	.driver = {
393		.owner =	THIS_MODULE,
394		.name =		"opticon",
395	},
396	.id_table =		id_table,
397	.num_ports =		1,
398	.num_bulk_in =		1,
399	.bulk_in_size =		256,
400	.port_probe =		opticon_port_probe,
401	.port_remove =		opticon_port_remove,
402	.open =			opticon_open,
 
403	.write =		opticon_write,
404	.write_room = 		opticon_write_room,
 
405	.throttle =		usb_serial_generic_throttle,
406	.unthrottle =		usb_serial_generic_unthrottle,
407	.ioctl =		opticon_ioctl,
408	.tiocmget =		opticon_tiocmget,
409	.tiocmset =		opticon_tiocmset,
410	.process_read_urb =	opticon_process_read_urb,
411};
412
413static struct usb_serial_driver * const serial_drivers[] = {
414	&opticon_device, NULL
415};
416
417module_usb_serial_driver(serial_drivers, id_table);
418
419MODULE_DESCRIPTION(DRIVER_DESC);
420MODULE_LICENSE("GPL v2");