xref: /linux/drivers/usb/serial/whiteheat.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * USB ConnectTech WhiteHEAT driver
4  *
5  *	Copyright (C) 2002
6  *	    Connect Tech Inc.
7  *
8  *	Copyright (C) 1999 - 2001
9  *	    Greg Kroah-Hartman (greg@kroah.com)
10  *
11  * See Documentation/usb/usb-serial.rst for more information on using this
12  * driver
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/tty.h>
19 #include <linux/tty_driver.h>
20 #include <linux/tty_flip.h>
21 #include <linux/module.h>
22 #include <linux/spinlock.h>
23 #include <linux/mutex.h>
24 #include <linux/uaccess.h>
25 #include <asm/termbits.h>
26 #include <linux/usb.h>
27 #include <linux/serial_reg.h>
28 #include <linux/serial.h>
29 #include <linux/usb/serial.h>
30 #include <linux/usb/ezusb.h>
31 #include "whiteheat.h"			/* WhiteHEAT specific commands */
32 
33 /*
34  * Version Information
35  */
36 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
37 #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
38 
39 #define CONNECT_TECH_VENDOR_ID		0x0710
40 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID	0x0001
41 #define CONNECT_TECH_WHITE_HEAT_ID	0x8001
42 
43 /*
44    ID tables for whiteheat are unusual, because we want to different
45    things for different versions of the device.  Eventually, this
46    will be doable from a single table.  But, for now, we define two
47    separate ID tables, and then a third table that combines them
48    just for the purpose of exporting the autoloading information.
49 */
50 static const struct usb_device_id id_table_std[] = {
51 	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
52 	{ }						/* Terminating entry */
53 };
54 
55 static const struct usb_device_id id_table_prerenumeration[] = {
56 	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
57 	{ }						/* Terminating entry */
58 };
59 
60 static const struct usb_device_id id_table_combined[] = {
61 	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
62 	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
63 	{ }						/* Terminating entry */
64 };
65 
66 MODULE_DEVICE_TABLE(usb, id_table_combined);
67 
68 
69 /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
70 static int  whiteheat_firmware_download(struct usb_serial *serial,
71 					const struct usb_device_id *id);
72 static int  whiteheat_firmware_attach(struct usb_serial *serial);
73 
74 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
75 static int  whiteheat_attach(struct usb_serial *serial);
76 static void whiteheat_release(struct usb_serial *serial);
77 static int  whiteheat_port_probe(struct usb_serial_port *port);
78 static void whiteheat_port_remove(struct usb_serial_port *port);
79 static int  whiteheat_open(struct tty_struct *tty,
80 			struct usb_serial_port *port);
81 static void whiteheat_close(struct usb_serial_port *port);
82 static void whiteheat_get_serial(struct tty_struct *tty,
83 			struct serial_struct *ss);
84 static void whiteheat_set_termios(struct tty_struct *tty,
85 				  struct usb_serial_port *port,
86 				  const struct ktermios *old_termios);
87 static int  whiteheat_tiocmget(struct tty_struct *tty);
88 static int  whiteheat_tiocmset(struct tty_struct *tty,
89 			unsigned int set, unsigned int clear);
90 static int whiteheat_break_ctl(struct tty_struct *tty, int break_state);
91 
92 static struct usb_serial_driver whiteheat_fake_device = {
93 	.driver = {
94 		.name =		"whiteheatnofirm",
95 	},
96 	.description =		"Connect Tech - WhiteHEAT - (prerenumeration)",
97 	.id_table =		id_table_prerenumeration,
98 	.num_ports =		1,
99 	.probe =		whiteheat_firmware_download,
100 	.attach =		whiteheat_firmware_attach,
101 };
102 
103 static struct usb_serial_driver whiteheat_device = {
104 	.driver = {
105 		.name =		"whiteheat",
106 	},
107 	.description =		"Connect Tech - WhiteHEAT",
108 	.id_table =		id_table_std,
109 	.num_ports =		4,
110 	.num_bulk_in =		5,
111 	.num_bulk_out =		5,
112 	.attach =		whiteheat_attach,
113 	.release =		whiteheat_release,
114 	.port_probe =		whiteheat_port_probe,
115 	.port_remove =		whiteheat_port_remove,
116 	.open =			whiteheat_open,
117 	.close =		whiteheat_close,
118 	.get_serial =		whiteheat_get_serial,
119 	.set_termios =		whiteheat_set_termios,
120 	.break_ctl =		whiteheat_break_ctl,
121 	.tiocmget =		whiteheat_tiocmget,
122 	.tiocmset =		whiteheat_tiocmset,
123 	.throttle =		usb_serial_generic_throttle,
124 	.unthrottle =		usb_serial_generic_unthrottle,
125 };
126 
127 static struct usb_serial_driver * const serial_drivers[] = {
128 	&whiteheat_fake_device, &whiteheat_device, NULL
129 };
130 
131 struct whiteheat_command_private {
132 	struct mutex		mutex;
133 	__u8			port_running;
134 	__u8			command_finished;
135 	wait_queue_head_t	wait_command; /* for handling sleeping whilst
136 						 waiting for a command to
137 						 finish */
138 	__u8			result_buffer[64];
139 };
140 
141 struct whiteheat_private {
142 	__u8			mcr;		/* FIXME: no locking on mcr */
143 };
144 
145 
146 /* local function prototypes */
147 static int start_command_port(struct usb_serial *serial);
148 static void stop_command_port(struct usb_serial *serial);
149 static void command_port_write_callback(struct urb *urb);
150 static void command_port_read_callback(struct urb *urb);
151 
152 static int firm_send_command(struct usb_serial_port *port, __u8 command,
153 						__u8 *data, __u8 datasize);
154 static int firm_open(struct usb_serial_port *port);
155 static int firm_close(struct usb_serial_port *port);
156 static void firm_setup_port(struct tty_struct *tty);
157 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
158 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
159 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
160 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
161 static int firm_get_dtr_rts(struct usb_serial_port *port);
162 static int firm_report_tx_done(struct usb_serial_port *port);
163 
164 
165 #define COMMAND_PORT		4
166 #define COMMAND_TIMEOUT		(2*HZ)	/* 2 second timeout for a command */
167 #define	COMMAND_TIMEOUT_MS	2000
168 
169 
170 /*****************************************************************************
171  * Connect Tech's White Heat prerenumeration driver functions
172  *****************************************************************************/
173 
174 /* steps to download the firmware to the WhiteHEAT device:
175  - hold the reset (by writing to the reset bit of the CPUCS register)
176  - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
177  - release the reset (by writing to the CPUCS register)
178  - download the WH.HEX file for all addresses greater than 0x1b3f using
179    VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
180  - hold the reset
181  - download the WH.HEX file for all addresses less than 0x1b40 using
182    VENDOR_REQUEST_ANCHOR_LOAD
183  - release the reset
184  - device renumerated itself and comes up as new device id with all
185    firmware download completed.
186 */
whiteheat_firmware_download(struct usb_serial * serial,const struct usb_device_id * id)187 static int whiteheat_firmware_download(struct usb_serial *serial,
188 					const struct usb_device_id *id)
189 {
190 	int response;
191 
192 	response = ezusb_fx1_ihex_firmware_download(serial->dev, "whiteheat_loader.fw");
193 	if (response >= 0) {
194 		response = ezusb_fx1_ihex_firmware_download(serial->dev, "whiteheat.fw");
195 		if (response >= 0)
196 			return 0;
197 	}
198 	return -ENOENT;
199 }
200 
201 
whiteheat_firmware_attach(struct usb_serial * serial)202 static int whiteheat_firmware_attach(struct usb_serial *serial)
203 {
204 	/* We want this device to fail to have a driver assigned to it */
205 	return 1;
206 }
207 
208 
209 /*****************************************************************************
210  * Connect Tech's White Heat serial driver functions
211  *****************************************************************************/
212 
whiteheat_attach(struct usb_serial * serial)213 static int whiteheat_attach(struct usb_serial *serial)
214 {
215 	struct usb_serial_port *command_port;
216 	struct whiteheat_command_private *command_info;
217 	struct whiteheat_hw_info *hw_info;
218 	int pipe;
219 	int ret;
220 	int alen;
221 	__u8 *command;
222 	__u8 *result;
223 
224 	command_port = serial->port[COMMAND_PORT];
225 
226 	pipe = usb_sndbulkpipe(serial->dev,
227 			command_port->bulk_out_endpointAddress);
228 	command = kmalloc(2, GFP_KERNEL);
229 	if (!command)
230 		goto no_command_buffer;
231 	command[0] = WHITEHEAT_GET_HW_INFO;
232 	command[1] = 0;
233 
234 	result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
235 	if (!result)
236 		goto no_result_buffer;
237 	/*
238 	 * When the module is reloaded the firmware is still there and
239 	 * the endpoints are still in the usb core unchanged. This is the
240 	 * unlinking bug in disguise. Same for the call below.
241 	 */
242 	usb_clear_halt(serial->dev, pipe);
243 	ret = usb_bulk_msg(serial->dev, pipe, command, 2,
244 						&alen, COMMAND_TIMEOUT_MS);
245 	if (ret) {
246 		dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
247 			serial->type->description, ret);
248 		goto no_firmware;
249 	} else if (alen != 2) {
250 		dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
251 			serial->type->description, alen);
252 		goto no_firmware;
253 	}
254 
255 	pipe = usb_rcvbulkpipe(serial->dev,
256 				command_port->bulk_in_endpointAddress);
257 	/* See the comment on the usb_clear_halt() above */
258 	usb_clear_halt(serial->dev, pipe);
259 	ret = usb_bulk_msg(serial->dev, pipe, result,
260 			sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
261 	if (ret) {
262 		dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
263 			serial->type->description, ret);
264 		goto no_firmware;
265 	} else if (alen != sizeof(*hw_info) + 1) {
266 		dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
267 			serial->type->description, alen);
268 		goto no_firmware;
269 	} else if (result[0] != command[0]) {
270 		dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
271 			serial->type->description, result[0]);
272 		goto no_firmware;
273 	}
274 
275 	hw_info = (struct whiteheat_hw_info *)&result[1];
276 
277 	dev_info(&serial->dev->dev, "%s: Firmware v%d.%02d\n",
278 		 serial->type->description,
279 		 hw_info->sw_major_rev, hw_info->sw_minor_rev);
280 
281 	command_info = kmalloc_obj(struct whiteheat_command_private);
282 	if (!command_info)
283 		goto no_command_private;
284 
285 	mutex_init(&command_info->mutex);
286 	command_info->port_running = 0;
287 	init_waitqueue_head(&command_info->wait_command);
288 	usb_set_serial_port_data(command_port, command_info);
289 	command_port->write_urb->complete = command_port_write_callback;
290 	command_port->read_urb->complete = command_port_read_callback;
291 	kfree(result);
292 	kfree(command);
293 
294 	return 0;
295 
296 no_firmware:
297 	/* Firmware likely not running */
298 	dev_err(&serial->dev->dev,
299 		"%s: Unable to retrieve firmware version, try replugging\n",
300 		serial->type->description);
301 	dev_err(&serial->dev->dev,
302 		"%s: If the firmware is not running (status led not blinking)\n",
303 		serial->type->description);
304 	dev_err(&serial->dev->dev,
305 		"%s: please contact support@connecttech.com\n",
306 		serial->type->description);
307 	kfree(result);
308 	kfree(command);
309 	return -ENODEV;
310 
311 no_command_private:
312 	kfree(result);
313 no_result_buffer:
314 	kfree(command);
315 no_command_buffer:
316 	return -ENOMEM;
317 }
318 
whiteheat_release(struct usb_serial * serial)319 static void whiteheat_release(struct usb_serial *serial)
320 {
321 	struct usb_serial_port *command_port;
322 
323 	/* free up our private data for our command port */
324 	command_port = serial->port[COMMAND_PORT];
325 	kfree(usb_get_serial_port_data(command_port));
326 }
327 
whiteheat_port_probe(struct usb_serial_port * port)328 static int whiteheat_port_probe(struct usb_serial_port *port)
329 {
330 	struct whiteheat_private *info;
331 
332 	info = kzalloc_obj(*info);
333 	if (!info)
334 		return -ENOMEM;
335 
336 	usb_set_serial_port_data(port, info);
337 
338 	return 0;
339 }
340 
whiteheat_port_remove(struct usb_serial_port * port)341 static void whiteheat_port_remove(struct usb_serial_port *port)
342 {
343 	struct whiteheat_private *info;
344 
345 	info = usb_get_serial_port_data(port);
346 	kfree(info);
347 }
348 
whiteheat_open(struct tty_struct * tty,struct usb_serial_port * port)349 static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
350 {
351 	int retval;
352 
353 	retval = start_command_port(port->serial);
354 	if (retval)
355 		goto exit;
356 
357 	/* send an open port command */
358 	retval = firm_open(port);
359 	if (retval) {
360 		stop_command_port(port->serial);
361 		goto exit;
362 	}
363 
364 	retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
365 	if (retval) {
366 		firm_close(port);
367 		stop_command_port(port->serial);
368 		goto exit;
369 	}
370 
371 	if (tty)
372 		firm_setup_port(tty);
373 
374 	/* Work around HCD bugs */
375 	usb_clear_halt(port->serial->dev, port->read_urb->pipe);
376 	usb_clear_halt(port->serial->dev, port->write_urb->pipe);
377 
378 	retval = usb_serial_generic_open(tty, port);
379 	if (retval) {
380 		firm_close(port);
381 		stop_command_port(port->serial);
382 		goto exit;
383 	}
384 exit:
385 	return retval;
386 }
387 
388 
whiteheat_close(struct usb_serial_port * port)389 static void whiteheat_close(struct usb_serial_port *port)
390 {
391 	firm_report_tx_done(port);
392 	firm_close(port);
393 
394 	usb_serial_generic_close(port);
395 
396 	stop_command_port(port->serial);
397 }
398 
whiteheat_tiocmget(struct tty_struct * tty)399 static int whiteheat_tiocmget(struct tty_struct *tty)
400 {
401 	struct usb_serial_port *port = tty->driver_data;
402 	struct whiteheat_private *info = usb_get_serial_port_data(port);
403 	unsigned int modem_signals = 0;
404 
405 	firm_get_dtr_rts(port);
406 	if (info->mcr & UART_MCR_DTR)
407 		modem_signals |= TIOCM_DTR;
408 	if (info->mcr & UART_MCR_RTS)
409 		modem_signals |= TIOCM_RTS;
410 
411 	return modem_signals;
412 }
413 
whiteheat_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)414 static int whiteheat_tiocmset(struct tty_struct *tty,
415 			       unsigned int set, unsigned int clear)
416 {
417 	struct usb_serial_port *port = tty->driver_data;
418 	struct whiteheat_private *info = usb_get_serial_port_data(port);
419 
420 	if (set & TIOCM_RTS)
421 		info->mcr |= UART_MCR_RTS;
422 	if (set & TIOCM_DTR)
423 		info->mcr |= UART_MCR_DTR;
424 
425 	if (clear & TIOCM_RTS)
426 		info->mcr &= ~UART_MCR_RTS;
427 	if (clear & TIOCM_DTR)
428 		info->mcr &= ~UART_MCR_DTR;
429 
430 	firm_set_dtr(port, info->mcr & UART_MCR_DTR);
431 	firm_set_rts(port, info->mcr & UART_MCR_RTS);
432 	return 0;
433 }
434 
435 
whiteheat_get_serial(struct tty_struct * tty,struct serial_struct * ss)436 static void whiteheat_get_serial(struct tty_struct *tty, struct serial_struct *ss)
437 {
438 	ss->baud_base = 460800;
439 }
440 
441 
whiteheat_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)442 static void whiteheat_set_termios(struct tty_struct *tty,
443 				  struct usb_serial_port *port,
444 				  const struct ktermios *old_termios)
445 {
446 	firm_setup_port(tty);
447 }
448 
whiteheat_break_ctl(struct tty_struct * tty,int break_state)449 static int whiteheat_break_ctl(struct tty_struct *tty, int break_state)
450 {
451 	struct usb_serial_port *port = tty->driver_data;
452 
453 	return firm_set_break(port, break_state);
454 }
455 
456 
457 /*****************************************************************************
458  * Connect Tech's White Heat callback routines
459  *****************************************************************************/
command_port_write_callback(struct urb * urb)460 static void command_port_write_callback(struct urb *urb)
461 {
462 	int status = urb->status;
463 
464 	if (status) {
465 		dev_dbg(&urb->dev->dev, "nonzero urb status: %d\n", status);
466 		return;
467 	}
468 }
469 
470 
command_port_read_callback(struct urb * urb)471 static void command_port_read_callback(struct urb *urb)
472 {
473 	struct usb_serial_port *command_port = urb->context;
474 	struct whiteheat_command_private *command_info;
475 	int status = urb->status;
476 	unsigned char *data = urb->transfer_buffer;
477 	int result;
478 
479 	command_info = usb_get_serial_port_data(command_port);
480 	if (!command_info) {
481 		dev_dbg(&urb->dev->dev, "%s - command_info is NULL, exiting.\n", __func__);
482 		return;
483 	}
484 	if (!urb->actual_length) {
485 		dev_dbg(&urb->dev->dev, "%s - empty response, exiting.\n", __func__);
486 		return;
487 	}
488 	if (status) {
489 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", __func__, status);
490 		if (status != -ENOENT)
491 			command_info->command_finished = WHITEHEAT_CMD_FAILURE;
492 		wake_up(&command_info->wait_command);
493 		return;
494 	}
495 
496 	usb_serial_debug_data(&command_port->dev, __func__, urb->actual_length, data);
497 
498 	if (data[0] == WHITEHEAT_CMD_COMPLETE) {
499 		command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
500 		wake_up(&command_info->wait_command);
501 	} else if (data[0] == WHITEHEAT_CMD_FAILURE) {
502 		command_info->command_finished = WHITEHEAT_CMD_FAILURE;
503 		wake_up(&command_info->wait_command);
504 	} else if (data[0] == WHITEHEAT_EVENT) {
505 		/* These are unsolicited reports from the firmware, hence no
506 		   waiting command to wakeup */
507 		dev_dbg(&urb->dev->dev, "%s - event received\n", __func__);
508 	} else if ((data[0] == WHITEHEAT_GET_DTR_RTS) &&
509 		(urb->actual_length - 1 <= sizeof(command_info->result_buffer))) {
510 		memcpy(command_info->result_buffer, &data[1],
511 						urb->actual_length - 1);
512 		command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
513 		wake_up(&command_info->wait_command);
514 	} else
515 		dev_dbg(&urb->dev->dev, "%s - bad reply from firmware\n", __func__);
516 
517 	/* Continue trying to always read */
518 	result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
519 	if (result)
520 		dev_dbg(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n",
521 			__func__, result);
522 }
523 
524 
525 /*****************************************************************************
526  * Connect Tech's White Heat firmware interface
527  *****************************************************************************/
firm_send_command(struct usb_serial_port * port,__u8 command,__u8 * data,__u8 datasize)528 static int firm_send_command(struct usb_serial_port *port, __u8 command,
529 						__u8 *data, __u8 datasize)
530 {
531 	struct usb_serial_port *command_port;
532 	struct whiteheat_command_private *command_info;
533 	struct whiteheat_private *info;
534 	struct device *dev = &port->dev;
535 	__u8 *transfer_buffer;
536 	int retval = 0;
537 	int t;
538 
539 	dev_dbg(dev, "%s - command %d\n", __func__, command);
540 
541 	command_port = port->serial->port[COMMAND_PORT];
542 	command_info = usb_get_serial_port_data(command_port);
543 
544 	if (command_port->bulk_out_size < datasize + 1)
545 		return -EIO;
546 
547 	mutex_lock(&command_info->mutex);
548 	command_info->command_finished = false;
549 
550 	transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
551 	transfer_buffer[0] = command;
552 	memcpy(&transfer_buffer[1], data, datasize);
553 	command_port->write_urb->transfer_buffer_length = datasize + 1;
554 	retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
555 	if (retval) {
556 		dev_dbg(dev, "%s - submit urb failed\n", __func__);
557 		goto exit;
558 	}
559 
560 	/* wait for the command to complete */
561 	t = wait_event_timeout(command_info->wait_command,
562 		(bool)command_info->command_finished, COMMAND_TIMEOUT);
563 	if (!t)
564 		usb_kill_urb(command_port->write_urb);
565 
566 	if (command_info->command_finished == false) {
567 		dev_dbg(dev, "%s - command timed out.\n", __func__);
568 		retval = -ETIMEDOUT;
569 		goto exit;
570 	}
571 
572 	if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
573 		dev_dbg(dev, "%s - command failed.\n", __func__);
574 		retval = -EIO;
575 		goto exit;
576 	}
577 
578 	if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
579 		dev_dbg(dev, "%s - command completed.\n", __func__);
580 		switch (command) {
581 		case WHITEHEAT_GET_DTR_RTS:
582 			info = usb_get_serial_port_data(port);
583 			info->mcr = command_info->result_buffer[0];
584 			break;
585 		}
586 	}
587 exit:
588 	mutex_unlock(&command_info->mutex);
589 	return retval;
590 }
591 
592 
firm_open(struct usb_serial_port * port)593 static int firm_open(struct usb_serial_port *port)
594 {
595 	struct whiteheat_simple open_command;
596 
597 	open_command.port = port->port_number + 1;
598 	return firm_send_command(port, WHITEHEAT_OPEN,
599 		(__u8 *)&open_command, sizeof(open_command));
600 }
601 
602 
firm_close(struct usb_serial_port * port)603 static int firm_close(struct usb_serial_port *port)
604 {
605 	struct whiteheat_simple close_command;
606 
607 	close_command.port = port->port_number + 1;
608 	return firm_send_command(port, WHITEHEAT_CLOSE,
609 			(__u8 *)&close_command, sizeof(close_command));
610 }
611 
612 
firm_setup_port(struct tty_struct * tty)613 static void firm_setup_port(struct tty_struct *tty)
614 {
615 	struct usb_serial_port *port = tty->driver_data;
616 	struct device *dev = &port->dev;
617 	struct whiteheat_port_settings port_settings;
618 	unsigned int cflag = tty->termios.c_cflag;
619 	speed_t baud;
620 
621 	port_settings.port = port->port_number + 1;
622 
623 	port_settings.bits = tty_get_char_size(cflag);
624 	dev_dbg(dev, "%s - data bits = %d\n", __func__, port_settings.bits);
625 
626 	/* determine the parity */
627 	if (cflag & PARENB)
628 		if (cflag & CMSPAR)
629 			if (cflag & PARODD)
630 				port_settings.parity = WHITEHEAT_PAR_MARK;
631 			else
632 				port_settings.parity = WHITEHEAT_PAR_SPACE;
633 		else
634 			if (cflag & PARODD)
635 				port_settings.parity = WHITEHEAT_PAR_ODD;
636 			else
637 				port_settings.parity = WHITEHEAT_PAR_EVEN;
638 	else
639 		port_settings.parity = WHITEHEAT_PAR_NONE;
640 	dev_dbg(dev, "%s - parity = %c\n", __func__, port_settings.parity);
641 
642 	/* figure out the stop bits requested */
643 	if (cflag & CSTOPB)
644 		port_settings.stop = 2;
645 	else
646 		port_settings.stop = 1;
647 	dev_dbg(dev, "%s - stop bits = %d\n", __func__, port_settings.stop);
648 
649 	/* figure out the flow control settings */
650 	if (cflag & CRTSCTS)
651 		port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
652 						WHITEHEAT_HFLOW_RTS);
653 	else
654 		port_settings.hflow = WHITEHEAT_HFLOW_NONE;
655 	dev_dbg(dev, "%s - hardware flow control = %s %s %s %s\n", __func__,
656 	    (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
657 	    (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
658 	    (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
659 	    (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
660 
661 	/* determine software flow control */
662 	if (I_IXOFF(tty))
663 		port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
664 	else
665 		port_settings.sflow = WHITEHEAT_SFLOW_NONE;
666 	dev_dbg(dev, "%s - software flow control = %c\n", __func__, port_settings.sflow);
667 
668 	port_settings.xon = START_CHAR(tty);
669 	port_settings.xoff = STOP_CHAR(tty);
670 	dev_dbg(dev, "%s - XON = %2x, XOFF = %2x\n", __func__, port_settings.xon, port_settings.xoff);
671 
672 	/* get the baud rate wanted */
673 	baud = tty_get_baud_rate(tty);
674 	port_settings.baud = cpu_to_le32(baud);
675 	dev_dbg(dev, "%s - baud rate = %u\n", __func__, baud);
676 
677 	/* fixme: should set validated settings */
678 	tty_encode_baud_rate(tty, baud, baud);
679 
680 	/* handle any settings that aren't specified in the tty structure */
681 	port_settings.lloop = 0;
682 
683 	/* now send the message to the device */
684 	firm_send_command(port, WHITEHEAT_SETUP_PORT,
685 			(__u8 *)&port_settings, sizeof(port_settings));
686 }
687 
688 
firm_set_rts(struct usb_serial_port * port,__u8 onoff)689 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
690 {
691 	struct whiteheat_set_rdb rts_command;
692 
693 	rts_command.port = port->port_number + 1;
694 	rts_command.state = onoff;
695 	return firm_send_command(port, WHITEHEAT_SET_RTS,
696 			(__u8 *)&rts_command, sizeof(rts_command));
697 }
698 
699 
firm_set_dtr(struct usb_serial_port * port,__u8 onoff)700 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
701 {
702 	struct whiteheat_set_rdb dtr_command;
703 
704 	dtr_command.port = port->port_number + 1;
705 	dtr_command.state = onoff;
706 	return firm_send_command(port, WHITEHEAT_SET_DTR,
707 			(__u8 *)&dtr_command, sizeof(dtr_command));
708 }
709 
710 
firm_set_break(struct usb_serial_port * port,__u8 onoff)711 static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
712 {
713 	struct whiteheat_set_rdb break_command;
714 
715 	break_command.port = port->port_number + 1;
716 	break_command.state = onoff;
717 	return firm_send_command(port, WHITEHEAT_SET_BREAK,
718 			(__u8 *)&break_command, sizeof(break_command));
719 }
720 
721 
firm_purge(struct usb_serial_port * port,__u8 rxtx)722 static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
723 {
724 	struct whiteheat_purge purge_command;
725 
726 	purge_command.port = port->port_number + 1;
727 	purge_command.what = rxtx;
728 	return firm_send_command(port, WHITEHEAT_PURGE,
729 			(__u8 *)&purge_command, sizeof(purge_command));
730 }
731 
732 
firm_get_dtr_rts(struct usb_serial_port * port)733 static int firm_get_dtr_rts(struct usb_serial_port *port)
734 {
735 	struct whiteheat_simple get_dr_command;
736 
737 	get_dr_command.port = port->port_number + 1;
738 	return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
739 			(__u8 *)&get_dr_command, sizeof(get_dr_command));
740 }
741 
742 
firm_report_tx_done(struct usb_serial_port * port)743 static int firm_report_tx_done(struct usb_serial_port *port)
744 {
745 	struct whiteheat_simple close_command;
746 
747 	close_command.port = port->port_number + 1;
748 	return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
749 			(__u8 *)&close_command, sizeof(close_command));
750 }
751 
752 
753 /*****************************************************************************
754  * Connect Tech's White Heat utility functions
755  *****************************************************************************/
start_command_port(struct usb_serial * serial)756 static int start_command_port(struct usb_serial *serial)
757 {
758 	struct usb_serial_port *command_port;
759 	struct whiteheat_command_private *command_info;
760 	int retval = 0;
761 
762 	command_port = serial->port[COMMAND_PORT];
763 	command_info = usb_get_serial_port_data(command_port);
764 	mutex_lock(&command_info->mutex);
765 	if (!command_info->port_running) {
766 		/* Work around HCD bugs */
767 		usb_clear_halt(serial->dev, command_port->read_urb->pipe);
768 
769 		retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
770 		if (retval) {
771 			dev_err(&serial->dev->dev,
772 				"%s - failed submitting read urb, error %d\n",
773 				__func__, retval);
774 			goto exit;
775 		}
776 	}
777 	command_info->port_running++;
778 
779 exit:
780 	mutex_unlock(&command_info->mutex);
781 	return retval;
782 }
783 
784 
stop_command_port(struct usb_serial * serial)785 static void stop_command_port(struct usb_serial *serial)
786 {
787 	struct usb_serial_port *command_port;
788 	struct whiteheat_command_private *command_info;
789 
790 	command_port = serial->port[COMMAND_PORT];
791 	command_info = usb_get_serial_port_data(command_port);
792 	mutex_lock(&command_info->mutex);
793 	command_info->port_running--;
794 	if (!command_info->port_running)
795 		usb_kill_urb(command_port->read_urb);
796 	mutex_unlock(&command_info->mutex);
797 }
798 
799 module_usb_serial_driver(serial_drivers, id_table_combined);
800 
801 MODULE_AUTHOR(DRIVER_AUTHOR);
802 MODULE_DESCRIPTION(DRIVER_DESC);
803 MODULE_LICENSE("GPL");
804 
805 MODULE_FIRMWARE("whiteheat.fw");
806 MODULE_FIRMWARE("whiteheat_loader.fw");
807