File Index
Files and directories
Sign in
FILE

kobil_sct.c

File preview with syntax highlighting.

linux-6.19.14/drivers/usb/serial/kobil_sct.c C
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 *  KOBIL USB Smart Card Terminal Driver
  4 *
  5 *  Copyright (C) 2002  KOBIL Systems GmbH
  6 *  Author: Thomas Wahrenbruch
  7 *
  8 *  Contact: linuxusb@kobil.de
  9 *
 10 *  This program is largely derived from work by the linux-usb group
 11 *  and associated source files.  Please see the usb/serial files for
 12 *  individual credits and copyrights.
 13 *
 14 *  Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
 15 *  patience.
 16 *
 17 * Supported readers: USB TWIN, KAAN Standard Plus and SecOVID Reader Plus
 18 * (Adapter K), B1 Professional and KAAN Professional (Adapter B)
 19 */
 20
 21
 22#include <linux/kernel.h>
 23#include <linux/errno.h>
 24#include <linux/slab.h>
 25#include <linux/tty.h>
 26#include <linux/tty_driver.h>
 27#include <linux/tty_flip.h>
 28#include <linux/module.h>
 29#include <linux/spinlock.h>
 30#include <linux/uaccess.h>
 31#include <linux/usb.h>
 32#include <linux/usb/serial.h>
 33#include <linux/ioctl.h>
 34#include "kobil_sct.h"
 35
 36#define DRIVER_AUTHOR "KOBIL Systems GmbH - http://www.kobil.com"
 37#define DRIVER_DESC "KOBIL USB Smart Card Terminal Driver (experimental)"
 38
 39#define KOBIL_VENDOR_ID			0x0D46
 40#define KOBIL_ADAPTER_B_PRODUCT_ID	0x2011
 41#define KOBIL_ADAPTER_K_PRODUCT_ID	0x2012
 42#define KOBIL_USBTWIN_PRODUCT_ID	0x0078
 43#define KOBIL_KAAN_SIM_PRODUCT_ID       0x0081
 44
 45#define KOBIL_TIMEOUT		500
 46#define KOBIL_BUF_LENGTH	300
 47
 48
 49/* Function prototypes */
 50static int kobil_port_probe(struct usb_serial_port *probe);
 51static void kobil_port_remove(struct usb_serial_port *probe);
 52static int  kobil_open(struct tty_struct *tty, struct usb_serial_port *port);
 53static void kobil_close(struct usb_serial_port *port);
 54static int  kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
 55			 const unsigned char *buf, int count);
 56static unsigned int kobil_write_room(struct tty_struct *tty);
 57static int  kobil_ioctl(struct tty_struct *tty,
 58			unsigned int cmd, unsigned long arg);
 59static int  kobil_tiocmget(struct tty_struct *tty);
 60static int  kobil_tiocmset(struct tty_struct *tty,
 61			   unsigned int set, unsigned int clear);
 62static void kobil_read_int_callback(struct urb *urb);
 63static void kobil_write_int_callback(struct urb *urb);
 64static void kobil_set_termios(struct tty_struct *tty,
 65			      struct usb_serial_port *port,
 66			      const struct ktermios *old);
 67static void kobil_init_termios(struct tty_struct *tty);
 68
 69static const struct usb_device_id id_table[] = {
 70	{ USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
 71	{ USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
 72	{ USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
 73	{ USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
 74	{ }			/* Terminating entry */
 75};
 76MODULE_DEVICE_TABLE(usb, id_table);
 77
 78static struct usb_serial_driver kobil_device = {
 79	.driver = {
 80		.name =		"kobil",
 81	},
 82	.description =		"KOBIL USB smart card terminal",
 83	.id_table =		id_table,
 84	.num_ports =		1,
 85	.num_interrupt_out =	1,
 86	.port_probe =		kobil_port_probe,
 87	.port_remove =		kobil_port_remove,
 88	.ioctl =		kobil_ioctl,
 89	.set_termios =		kobil_set_termios,
 90	.init_termios =		kobil_init_termios,
 91	.tiocmget =		kobil_tiocmget,
 92	.tiocmset =		kobil_tiocmset,
 93	.open =			kobil_open,
 94	.close =		kobil_close,
 95	.write =		kobil_write,
 96	.write_room =		kobil_write_room,
 97	.read_int_callback =	kobil_read_int_callback,
 98	.write_int_callback =	kobil_write_int_callback,
 99};
100
101static struct usb_serial_driver * const serial_drivers[] = {
102	&kobil_device, NULL
103};
104
105struct kobil_private {
106	unsigned char buf[KOBIL_BUF_LENGTH]; /* buffer for the APDU to send */
107	int filled;  /* index of the last char in buf */
108	int cur_pos; /* index of the next char to send in buf */
109	__u16 device_type;
110};
111
112static int kobil_ctrl_send(struct usb_serial_port *port, u8 req, u16 val)
113{
114	return usb_control_msg(port->serial->dev,
115			usb_sndctrlpipe(port->serial->dev, 0),
116			req, USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
117			val, 0, NULL, 0, KOBIL_TIMEOUT);
118}
119
120static int kobil_ctrl_recv(struct usb_serial_port *port, u8 req, u16 val, void *buf, u16 size)
121{
122	return usb_control_msg(port->serial->dev,
123			usb_rcvctrlpipe(port->serial->dev, 0),
124			req, USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
125			val, 0, buf, size, KOBIL_TIMEOUT);
126}
127
128static int kobil_port_probe(struct usb_serial_port *port)
129{
130	struct usb_serial *serial = port->serial;
131	struct kobil_private *priv;
132
133	priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
134	if (!priv)
135		return -ENOMEM;
136
137	priv->filled = 0;
138	priv->cur_pos = 0;
139	priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
140
141	switch (priv->device_type) {
142	case KOBIL_ADAPTER_B_PRODUCT_ID:
143		dev_dbg(&serial->dev->dev, "KOBIL B1 PRO / KAAN PRO detected\n");
144		break;
145	case KOBIL_ADAPTER_K_PRODUCT_ID:
146		dev_dbg(&serial->dev->dev, "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
147		break;
148	case KOBIL_USBTWIN_PRODUCT_ID:
149		dev_dbg(&serial->dev->dev, "KOBIL USBTWIN detected\n");
150		break;
151	case KOBIL_KAAN_SIM_PRODUCT_ID:
152		dev_dbg(&serial->dev->dev, "KOBIL KAAN SIM detected\n");
153		break;
154	}
155	usb_set_serial_port_data(port, priv);
156
157	return 0;
158}
159
160
161static void kobil_port_remove(struct usb_serial_port *port)
162{
163	struct kobil_private *priv;
164
165	priv = usb_get_serial_port_data(port);
166	kfree(priv);
167}
168
169static void kobil_init_termios(struct tty_struct *tty)
170{
171	/* Default to echo off and other sane device settings */
172	tty->termios.c_lflag = 0;
173	tty->termios.c_iflag = IGNBRK | IGNPAR | IXOFF;
174	/* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */
175	tty->termios.c_oflag &= ~ONLCR;
176}
177
178static int kobil_open(struct tty_struct *tty, struct usb_serial_port *port)
179{
180	struct device *dev = &port->dev;
181	struct kobil_private *priv;
182	unsigned char *transfer_buffer;
183	int transfer_buffer_length = 8;
184	int result;
185
186	priv = usb_get_serial_port_data(port);
187
188	/* allocate memory for transfer buffer */
189	transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
190	if (!transfer_buffer)
191		return -ENOMEM;
192
193	/* get hardware version */
194	result = kobil_ctrl_recv(port, SUSBCRequest_GetMisc, SUSBCR_MSC_GetHWVersion,
195			transfer_buffer, transfer_buffer_length);
196	dev_dbg(dev, "%s - Send get_HW_version URB returns: %i\n", __func__, result);
197	if (result >= 3) {
198		dev_dbg(dev, "Hardware version: %i.%i.%i\n", transfer_buffer[0],
199				transfer_buffer[1], transfer_buffer[2]);
200	}
201
202	/* get firmware version */
203	result = kobil_ctrl_recv(port, SUSBCRequest_GetMisc, SUSBCR_MSC_GetFWVersion,
204			transfer_buffer, transfer_buffer_length);
205	dev_dbg(dev, "%s - Send get_FW_version URB returns: %i\n", __func__, result);
206	if (result >= 3) {
207		dev_dbg(dev, "Firmware version: %i.%i.%i\n", transfer_buffer[0],
208				transfer_buffer[1], transfer_buffer[2]);
209	}
210
211	if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
212			priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
213		/* Setting Baudrate, Parity and Stopbits */
214		result = kobil_ctrl_send(port, SUSBCRequest_SetBaudRateParityAndStopBits,
215				SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit);
216		dev_dbg(dev, "%s - Send set_baudrate URB returns: %i\n", __func__, result);
217
218		/* reset all queues */
219		result = kobil_ctrl_send(port, SUSBCRequest_Misc, SUSBCR_MSC_ResetAllQueues);
220		dev_dbg(dev, "%s - Send reset_all_queues URB returns: %i\n", __func__, result);
221	}
222	if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
223			priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
224			priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
225		/* start reading (Adapter B 'cause PNP string) */
226		result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
227		dev_dbg(dev, "%s - Send read URB returns: %i\n", __func__, result);
228	}
229
230	kfree(transfer_buffer);
231	return 0;
232}
233
234
235static void kobil_close(struct usb_serial_port *port)
236{
237	/* FIXME: Add rts/dtr methods */
238	usb_kill_urb(port->interrupt_out_urb);
239	usb_kill_urb(port->interrupt_in_urb);
240}
241
242
243static void kobil_read_int_callback(struct urb *urb)
244{
245	int result;
246	struct usb_serial_port *port = urb->context;
247	unsigned char *data = urb->transfer_buffer;
248	int status = urb->status;
249
250	if (status) {
251		dev_dbg(&port->dev, "%s - Read int status not zero: %d\n", __func__, status);
252		return;
253	}
254
255	if (urb->actual_length) {
256		usb_serial_debug_data(&port->dev, __func__, urb->actual_length,
257									data);
258		tty_insert_flip_string(&port->port, data, urb->actual_length);
259		tty_flip_buffer_push(&port->port);
260	}
261
262	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
263	dev_dbg(&port->dev, "%s - Send read URB returns: %i\n", __func__, result);
264}
265
266
267static void kobil_write_int_callback(struct urb *urb)
268{
269}
270
271
272static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
273			const unsigned char *buf, int count)
274{
275	struct kobil_private *priv;
276	int length, todo, result;
277
278	if (count == 0) {
279		dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
280		return 0;
281	}
282
283	priv = usb_get_serial_port_data(port);
284
285	if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
286		dev_dbg(&port->dev, "%s - Error: write request bigger than buffer size\n", __func__);
287		return -ENOMEM;
288	}
289
290	/* Copy data to buffer */
291	memcpy(priv->buf + priv->filled, buf, count);
292	usb_serial_debug_data(&port->dev, __func__, count, priv->buf + priv->filled);
293	priv->filled = priv->filled + count;
294
295	/* only send complete block. TWIN, KAAN SIM and adapter K
296	   use the same protocol. */
297	if (((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
298	     ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4)))) {
299		/* stop reading (except TWIN and KAAN SIM) */
300		if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
301				priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
302			usb_kill_urb(port->interrupt_in_urb);
303		}
304
305		todo = priv->filled - priv->cur_pos;
306
307		while (todo > 0) {
308			/* max 8 byte in one urb (endpoint size) */
309			length = min(todo, port->interrupt_out_size);
310			/* copy data to transfer buffer */
311			memcpy(port->interrupt_out_buffer,
312					priv->buf + priv->cur_pos, length);
313			port->interrupt_out_urb->transfer_buffer_length = length;
314
315			priv->cur_pos = priv->cur_pos + length;
316			result = usb_submit_urb(port->interrupt_out_urb,
317					GFP_ATOMIC);
318			dev_dbg(&port->dev, "%s - Send write URB returns: %i\n", __func__, result);
319			todo = priv->filled - priv->cur_pos;
320
321			if (todo > 0)
322				msleep(24);
323		}
324
325		priv->filled = 0;
326		priv->cur_pos = 0;
327
328		/* start reading (except TWIN and KAAN SIM) */
329		if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
330				priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
331			result = usb_submit_urb(port->interrupt_in_urb,
332					GFP_ATOMIC);
333			dev_dbg(&port->dev, "%s - Send read URB returns: %i\n", __func__, result);
334		}
335	}
336	return count;
337}
338
339
340static unsigned int kobil_write_room(struct tty_struct *tty)
341{
342	/* FIXME */
343	return 8;
344}
345
346
347static int kobil_tiocmget(struct tty_struct *tty)
348{
349	struct usb_serial_port *port = tty->driver_data;
350	struct kobil_private *priv;
351	int result;
352	unsigned char *transfer_buffer;
353	int transfer_buffer_length = 8;
354
355	priv = usb_get_serial_port_data(port);
356	if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
357			priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
358		/* This device doesn't support ioctl calls */
359		return -EINVAL;
360	}
361
362	/* allocate memory for transfer buffer */
363	transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
364	if (!transfer_buffer)
365		return -ENOMEM;
366
367	result = kobil_ctrl_recv(port, SUSBCRequest_GetStatusLineState, 0,
368			transfer_buffer, transfer_buffer_length);
369	dev_dbg(&port->dev, "Send get_status_line_state URB returns: %i\n",
370			result);
371	if (result < 1) {
372		if (result >= 0)
373			result = -EIO;
374		goto out_free;
375	}
376
377	dev_dbg(&port->dev, "Statusline: %02x\n", transfer_buffer[0]);
378
379	result = 0;
380	if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
381		result = TIOCM_DSR;
382out_free:
383	kfree(transfer_buffer);
384	return result;
385}
386
387static int kobil_tiocmset(struct tty_struct *tty,
388			   unsigned int set, unsigned int clear)
389{
390	struct usb_serial_port *port = tty->driver_data;
391	struct device *dev = &port->dev;
392	struct kobil_private *priv;
393	int dtr, rts;
394	int result;
395	u16 val = 0;
396
397	priv = usb_get_serial_port_data(port);
398	if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
399			priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
400		/* This device doesn't support ioctl calls */
401		return -EINVAL;
402	}
403
404	dtr = (set | clear) & TIOCM_DTR;
405	rts = (set | clear) & TIOCM_RTS;
406
407	if (dtr && priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
408		if (set & TIOCM_DTR)
409			val = SUSBCR_SSL_SETDTR;
410		else
411			val = SUSBCR_SSL_CLRDTR;
412	} else if (rts) {
413		if (set & TIOCM_RTS)
414			val = SUSBCR_SSL_SETRTS;
415		else
416			val = SUSBCR_SSL_CLRRTS;
417	}
418
419	if (val) {
420		result = kobil_ctrl_send(port, SUSBCRequest_SetStatusLinesOrQueues, val);
421		if (result < 0) {
422			dev_err(dev, "failed to set status lines: %d\n", result);
423			return result;
424		}
425	}
426
427	return 0;
428}
429
430static void kobil_set_termios(struct tty_struct *tty,
431			      struct usb_serial_port *port,
432			      const struct ktermios *old)
433{
434	struct kobil_private *priv;
435	int result;
436	int c_cflag = tty->termios.c_cflag;
437	speed_t speed;
438	u16 val;
439
440	priv = usb_get_serial_port_data(port);
441	if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
442			priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
443		/* This device doesn't support ioctl calls */
444		tty_termios_copy_hw(&tty->termios, old);
445		return;
446	}
447
448	speed = tty_get_baud_rate(tty);
449	switch (speed) {
450	case 1200:
451		val = SUSBCR_SBR_1200;
452		break;
453	default:
454		speed = 9600;
455		fallthrough;
456	case 9600:
457		val = SUSBCR_SBR_9600;
458		break;
459	}
460
461	if (c_cflag & CSTOPB)
462		val |= SUSBCR_SPASB_2StopBits;
463	else
464		val |= SUSBCR_SPASB_1StopBit;
465
466	if (c_cflag & PARENB) {
467		if  (c_cflag & PARODD)
468			val |= SUSBCR_SPASB_OddParity;
469		else
470			val |= SUSBCR_SPASB_EvenParity;
471	} else {
472		val |= SUSBCR_SPASB_NoParity;
473	}
474
475	tty->termios.c_cflag &= ~CMSPAR;
476	tty_encode_baud_rate(tty, speed, speed);
477
478	result = kobil_ctrl_send(port, SUSBCRequest_SetBaudRateParityAndStopBits, val);
479	if (result) {
480		dev_err(&port->dev, "failed to update line settings: %d\n",
481				result);
482	}
483}
484
485static int kobil_ioctl(struct tty_struct *tty,
486					unsigned int cmd, unsigned long arg)
487{
488	struct usb_serial_port *port = tty->driver_data;
489	struct kobil_private *priv = usb_get_serial_port_data(port);
490	int result;
491
492	if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
493			priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
494		/* This device doesn't support ioctl calls */
495		return -ENOIOCTLCMD;
496
497	switch (cmd) {
498	case TCFLSH:
499		result = kobil_ctrl_send(port, SUSBCRequest_Misc, SUSBCR_MSC_ResetAllQueues);
500		dev_dbg(&port->dev,
501			"%s - Send reset_all_queues (FLUSH) URB returns: %i\n",
502			__func__, result);
503		return (result < 0) ? -EIO: 0;
504	default:
505		return -ENOIOCTLCMD;
506	}
507}
508
509module_usb_serial_driver(serial_drivers, id_table);
510
511MODULE_AUTHOR(DRIVER_AUTHOR);
512MODULE_DESCRIPTION(DRIVER_DESC);
513MODULE_LICENSE("GPL");
~/ Read-only file preview.