1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Edgeport USB Serial Converter driver
4 *
5 * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
6 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
7 *
8 * Supports the following devices:
9 * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
10 *
11 * For questions or problems with this driver, contact Inside Out
12 * Networks technical support, or Peter Berger <pberger@brimson.com>,
13 * or Al Borchers <alborchers@steinerpoint.com>.
14 */
15
16 #include <linux/kernel.h>
17 #include <linux/jiffies.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/tty.h>
21 #include <linux/tty_flip.h>
22 #include <linux/module.h>
23 #include <linux/spinlock.h>
24 #include <linux/mutex.h>
25 #include <linux/serial.h>
26 #include <linux/swab.h>
27 #include <linux/kfifo.h>
28 #include <linux/ioctl.h>
29 #include <linux/firmware.h>
30 #include <linux/usb.h>
31 #include <linux/usb/serial.h>
32
33 #include "io_16654.h"
34 #include "io_usbvend.h"
35 #include "io_ti.h"
36
37 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
38 #define DRIVER_DESC "Edgeport USB Serial Driver"
39
40 #define EPROM_PAGE_SIZE 64
41
42
43 /* different hardware types */
44 #define HARDWARE_TYPE_930 0
45 #define HARDWARE_TYPE_TIUMP 1
46
47 /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
48 #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
49 #define TI_MODE_BOOT 1 /* Staying in boot mode */
50 #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
51 #define TI_MODE_TRANSITIONING 3 /*
52 * Currently in boot mode but
53 * transitioning to download mode
54 */
55
56 /* read urb state */
57 #define EDGE_READ_URB_RUNNING 0
58 #define EDGE_READ_URB_STOPPING 1
59 #define EDGE_READ_URB_STOPPED 2
60
61
62 /* Product information read from the Edgeport */
63 struct product_info {
64 int TiMode; /* Current TI Mode */
65 u8 hardware_type; /* Type of hardware */
66 } __packed;
67
68 /*
69 * Edgeport firmware header
70 *
71 * "build_number" has been set to 0 in all three of the images I have
72 * seen, and Digi Tech Support suggests that it is safe to ignore it.
73 *
74 * "length" is the number of bytes of actual data following the header.
75 *
76 * "checksum" is the low order byte resulting from adding the values of
77 * all the data bytes.
78 */
79 struct edgeport_fw_hdr {
80 u8 major_version;
81 u8 minor_version;
82 __le16 build_number;
83 __le16 length;
84 u8 checksum;
85 } __packed;
86
87 struct edgeport_port {
88 u16 uart_base;
89 u16 dma_address;
90 u8 shadow_msr;
91 u8 shadow_mcr;
92 u8 shadow_lsr;
93 u8 lsr_mask;
94 u32 ump_read_timeout; /*
95 * Number of milliseconds the UMP will
96 * wait without data before completing
97 * a read short
98 */
99 int baud_rate;
100 int close_pending;
101 int lsr_event;
102
103 struct edgeport_serial *edge_serial;
104 struct usb_serial_port *port;
105 u8 bUartMode; /* Port type, 0: RS232, etc. */
106 spinlock_t ep_lock;
107 int ep_read_urb_state;
108 int ep_write_urb_in_use;
109 };
110
111 struct edgeport_serial {
112 struct product_info product_info;
113 u8 TI_I2C_Type; /* Type of I2C in UMP */
114 u8 TiReadI2C; /*
115 * Set to TRUE if we have read the
116 * I2c in Boot Mode
117 */
118 struct mutex es_lock;
119 int num_ports_open;
120 struct usb_serial *serial;
121 struct delayed_work heartbeat_work;
122 int fw_version;
123 bool use_heartbeat;
124 };
125
126
127 /* Devices that this driver supports */
128 static const struct usb_device_id edgeport_1port_id_table[] = {
129 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
130 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
131 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
132 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
133 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
134 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
135 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
136 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
137 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
145 { }
146 };
147
148 static const struct usb_device_id edgeport_2port_id_table[] = {
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
156 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
157 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
161 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_E5805A) },
168 { }
169 };
170
171 /* Devices that this driver supports */
172 static const struct usb_device_id id_table_combined[] = {
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
179 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
205 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
206 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_E5805A) },
207 { }
208 };
209
210 MODULE_DEVICE_TABLE(usb, id_table_combined);
211
212 static bool ignore_cpu_rev;
213 static int default_uart_mode; /* RS232 */
214
215 static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
216 int length);
217
218 static void stop_read(struct edgeport_port *edge_port);
219 static int restart_read(struct edgeport_port *edge_port);
220
221 static void edge_set_termios(struct tty_struct *tty,
222 struct usb_serial_port *port,
223 const struct ktermios *old_termios);
224 static void edge_send(struct usb_serial_port *port, struct tty_struct *tty);
225
226 static int do_download_mode(struct edgeport_serial *serial,
227 const struct firmware *fw);
228 static int do_boot_mode(struct edgeport_serial *serial,
229 const struct firmware *fw);
230
231 /* sysfs attributes */
232 static int edge_create_sysfs_attrs(struct usb_serial_port *port);
233 static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
234
235 /*
236 * Some release of Edgeport firmware "down3.bin" after version 4.80
237 * introduced code to automatically disconnect idle devices on some
238 * Edgeport models after periods of inactivity, typically ~60 seconds.
239 * This occurs without regard to whether ports on the device are open
240 * or not. Digi International Tech Support suggested:
241 *
242 * 1. Adding driver "heartbeat" code to reset the firmware timer by
243 * requesting a descriptor record every 15 seconds, which should be
244 * effective with newer firmware versions that require it, and benign
245 * with older versions that do not. In practice 40 seconds seems often
246 * enough.
247 * 2. The heartbeat code is currently required only on Edgeport/416 models.
248 */
249 #define FW_HEARTBEAT_VERSION_CUTOFF ((4 << 8) + 80)
250 #define FW_HEARTBEAT_SECS 40
251
252 /* Timeouts in msecs: firmware downloads take longer */
253 #define TI_VSEND_TIMEOUT_DEFAULT 1000
254 #define TI_VSEND_TIMEOUT_FW_DOWNLOAD 10000
255
ti_vread_sync(struct usb_device * dev,u8 request,u16 value,u16 index,void * data,int size)256 static int ti_vread_sync(struct usb_device *dev, u8 request, u16 value,
257 u16 index, void *data, int size)
258 {
259 int status;
260
261 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
262 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
263 value, index, data, size, 1000);
264 if (status < 0)
265 return status;
266 if (status != size) {
267 dev_dbg(&dev->dev, "%s - wanted to read %d, but only read %d\n",
268 __func__, size, status);
269 return -ECOMM;
270 }
271 return 0;
272 }
273
ti_vsend_sync(struct usb_device * dev,u8 request,u16 value,u16 index,void * data,int size,int timeout)274 static int ti_vsend_sync(struct usb_device *dev, u8 request, u16 value,
275 u16 index, void *data, int size, int timeout)
276 {
277 int status;
278
279 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
280 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
281 value, index, data, size, timeout);
282 if (status < 0)
283 return status;
284
285 return 0;
286 }
287
read_port_cmd(struct usb_serial_port * port,u8 command,u16 value,void * data,int size)288 static int read_port_cmd(struct usb_serial_port *port, u8 command, u16 value,
289 void *data, int size)
290 {
291 return ti_vread_sync(port->serial->dev, command, value,
292 UMPM_UART1_PORT + port->port_number,
293 data, size);
294 }
295
send_port_cmd(struct usb_serial_port * port,u8 command,u16 value,void * data,int size)296 static int send_port_cmd(struct usb_serial_port *port, u8 command, u16 value,
297 void *data, int size)
298 {
299 return ti_vsend_sync(port->serial->dev, command, value,
300 UMPM_UART1_PORT + port->port_number,
301 data, size, TI_VSEND_TIMEOUT_DEFAULT);
302 }
303
304 /* clear tx/rx buffers and fifo in TI UMP */
purge_port(struct usb_serial_port * port,u16 mask)305 static int purge_port(struct usb_serial_port *port, u16 mask)
306 {
307 int port_number = port->port_number;
308
309 dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
310
311 return send_port_cmd(port, UMPC_PURGE_PORT, mask, NULL, 0);
312 }
313
314 /**
315 * read_download_mem - Read edgeport memory from TI chip
316 * @dev: usb device pointer
317 * @start_address: Device CPU address at which to read
318 * @length: Length of above data
319 * @address_type: Can read both XDATA and I2C
320 * @buffer: pointer to input data buffer
321 */
read_download_mem(struct usb_device * dev,int start_address,int length,u8 address_type,u8 * buffer)322 static int read_download_mem(struct usb_device *dev, int start_address,
323 int length, u8 address_type, u8 *buffer)
324 {
325 int status = 0;
326 u8 read_length;
327 u16 be_start_address;
328
329 dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
330
331 /*
332 * Read in blocks of 64 bytes
333 * (TI firmware can't handle more than 64 byte reads)
334 */
335 while (length) {
336 if (length > 64)
337 read_length = 64;
338 else
339 read_length = (u8)length;
340
341 if (read_length > 1) {
342 dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
343 }
344 /*
345 * NOTE: Must use swab as wIndex is sent in little-endian
346 * byte order regardless of host byte order.
347 */
348 be_start_address = swab16((u16)start_address);
349 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
350 (u16)address_type,
351 be_start_address,
352 buffer, read_length);
353
354 if (status) {
355 dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
356 return status;
357 }
358
359 if (read_length > 1)
360 usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
361
362 /* Update pointers/length */
363 start_address += read_length;
364 buffer += read_length;
365 length -= read_length;
366 }
367
368 return status;
369 }
370
read_ram(struct usb_device * dev,int start_address,int length,u8 * buffer)371 static int read_ram(struct usb_device *dev, int start_address,
372 int length, u8 *buffer)
373 {
374 return read_download_mem(dev, start_address, length,
375 DTK_ADDR_SPACE_XDATA, buffer);
376 }
377
378 /* Read edgeport memory to a given block */
read_boot_mem(struct edgeport_serial * serial,int start_address,int length,u8 * buffer)379 static int read_boot_mem(struct edgeport_serial *serial,
380 int start_address, int length, u8 *buffer)
381 {
382 int status = 0;
383 int i;
384
385 for (i = 0; i < length; i++) {
386 status = ti_vread_sync(serial->serial->dev,
387 UMPC_MEMORY_READ, serial->TI_I2C_Type,
388 (u16)(start_address+i), &buffer[i], 0x01);
389 if (status) {
390 dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
391 return status;
392 }
393 }
394
395 dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
396 __func__, start_address, length);
397 usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
398
399 serial->TiReadI2C = 1;
400
401 return status;
402 }
403
404 /* Write given block to TI EPROM memory */
write_boot_mem(struct edgeport_serial * serial,int start_address,int length,u8 * buffer)405 static int write_boot_mem(struct edgeport_serial *serial,
406 int start_address, int length, u8 *buffer)
407 {
408 int status = 0;
409 int i;
410 u8 *temp;
411
412 /* Must do a read before write */
413 if (!serial->TiReadI2C) {
414 temp = kmalloc(1, GFP_KERNEL);
415 if (!temp)
416 return -ENOMEM;
417
418 status = read_boot_mem(serial, 0, 1, temp);
419 kfree(temp);
420 if (status)
421 return status;
422 }
423
424 for (i = 0; i < length; ++i) {
425 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
426 buffer[i], (u16)(i + start_address), NULL,
427 0, TI_VSEND_TIMEOUT_DEFAULT);
428 if (status)
429 return status;
430 }
431
432 dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
433 usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
434
435 return status;
436 }
437
438 /* Write edgeport I2C memory to TI chip */
write_i2c_mem(struct edgeport_serial * serial,int start_address,int length,u8 address_type,u8 * buffer)439 static int write_i2c_mem(struct edgeport_serial *serial,
440 int start_address, int length, u8 address_type, u8 *buffer)
441 {
442 struct device *dev = &serial->serial->dev->dev;
443 int status = 0;
444 int write_length;
445 u16 be_start_address;
446
447 /* We can only send a maximum of 1 aligned byte page at a time */
448
449 /* calculate the number of bytes left in the first page */
450 write_length = EPROM_PAGE_SIZE -
451 (start_address & (EPROM_PAGE_SIZE - 1));
452
453 if (write_length > length)
454 write_length = length;
455
456 dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
457 __func__, start_address, write_length);
458 usb_serial_debug_data(dev, __func__, write_length, buffer);
459
460 /*
461 * Write first page.
462 *
463 * NOTE: Must use swab as wIndex is sent in little-endian byte order
464 * regardless of host byte order.
465 */
466 be_start_address = swab16((u16)start_address);
467 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
468 (u16)address_type, be_start_address,
469 buffer, write_length, TI_VSEND_TIMEOUT_DEFAULT);
470 if (status) {
471 dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
472 return status;
473 }
474
475 length -= write_length;
476 start_address += write_length;
477 buffer += write_length;
478
479 /*
480 * We should be aligned now -- can write max page size bytes at a
481 * time.
482 */
483 while (length) {
484 if (length > EPROM_PAGE_SIZE)
485 write_length = EPROM_PAGE_SIZE;
486 else
487 write_length = length;
488
489 dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
490 __func__, start_address, write_length);
491 usb_serial_debug_data(dev, __func__, write_length, buffer);
492
493 /*
494 * Write next page.
495 *
496 * NOTE: Must use swab as wIndex is sent in little-endian byte
497 * order regardless of host byte order.
498 */
499 be_start_address = swab16((u16)start_address);
500 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
501 (u16)address_type, be_start_address, buffer,
502 write_length, TI_VSEND_TIMEOUT_DEFAULT);
503 if (status) {
504 dev_err(dev, "%s - ERROR %d\n", __func__, status);
505 return status;
506 }
507
508 length -= write_length;
509 start_address += write_length;
510 buffer += write_length;
511 }
512 return status;
513 }
514
515 /*
516 * Examine the UMP DMA registers and LSR
517 *
518 * Check the MSBit of the X and Y DMA byte count registers.
519 * A zero in this bit indicates that the TX DMA buffers are empty
520 * then check the TX Empty bit in the UART.
521 */
tx_active(struct edgeport_port * port)522 static int tx_active(struct edgeport_port *port)
523 {
524 int status;
525 struct out_endpoint_desc_block *oedb;
526 u8 *lsr;
527 int bytes_left = 0;
528
529 oedb = kmalloc_obj(*oedb);
530 if (!oedb)
531 return -ENOMEM;
532
533 /*
534 * Sigh, that's right, just one byte, as not all platforms can
535 * do DMA from stack
536 */
537 lsr = kmalloc(1, GFP_KERNEL);
538 if (!lsr) {
539 kfree(oedb);
540 return -ENOMEM;
541 }
542 /* Read the DMA Count Registers */
543 status = read_ram(port->port->serial->dev, port->dma_address,
544 sizeof(*oedb), (void *)oedb);
545 if (status)
546 goto exit_is_tx_active;
547
548 dev_dbg(&port->port->dev, "%s - XByteCount 0x%X\n", __func__, oedb->XByteCount);
549
550 /* and the LSR */
551 status = read_ram(port->port->serial->dev,
552 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
553
554 if (status)
555 goto exit_is_tx_active;
556 dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
557
558 /* If either buffer has data or we are transmitting then return TRUE */
559 if ((oedb->XByteCount & 0x80) != 0)
560 bytes_left += 64;
561
562 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
563 bytes_left += 1;
564
565 /* We return Not Active if we get any kind of error */
566 exit_is_tx_active:
567 dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
568
569 kfree(lsr);
570 kfree(oedb);
571 return bytes_left;
572 }
573
choose_config(struct usb_device * dev)574 static int choose_config(struct usb_device *dev)
575 {
576 /*
577 * There may be multiple configurations on this device, in which case
578 * we would need to read and parse all of them to find out which one
579 * we want. However, we just support one config at this point,
580 * configuration # 1, which is Config Descriptor 0.
581 */
582
583 dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
584 __func__, dev->config->desc.bNumInterfaces);
585 dev_dbg(&dev->dev, "%s - MAX Power = %d\n",
586 __func__, dev->config->desc.bMaxPower * 2);
587
588 if (dev->config->desc.bNumInterfaces != 1) {
589 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
590 return -ENODEV;
591 }
592
593 return 0;
594 }
595
read_rom(struct edgeport_serial * serial,int start_address,int length,u8 * buffer)596 static int read_rom(struct edgeport_serial *serial,
597 int start_address, int length, u8 *buffer)
598 {
599 int status;
600
601 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
602 status = read_download_mem(serial->serial->dev,
603 start_address,
604 length,
605 serial->TI_I2C_Type,
606 buffer);
607 } else {
608 status = read_boot_mem(serial, start_address, length,
609 buffer);
610 }
611 return status;
612 }
613
write_rom(struct edgeport_serial * serial,int start_address,int length,u8 * buffer)614 static int write_rom(struct edgeport_serial *serial, int start_address,
615 int length, u8 *buffer)
616 {
617 if (serial->product_info.TiMode == TI_MODE_BOOT)
618 return write_boot_mem(serial, start_address, length,
619 buffer);
620
621 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
622 return write_i2c_mem(serial, start_address, length,
623 serial->TI_I2C_Type, buffer);
624 return -EINVAL;
625 }
626
627 /* Read a descriptor header from I2C based on type */
get_descriptor_addr(struct edgeport_serial * serial,int desc_type,struct ti_i2c_desc * rom_desc)628 static int get_descriptor_addr(struct edgeport_serial *serial,
629 int desc_type, struct ti_i2c_desc *rom_desc)
630 {
631 int start_address;
632 int status;
633
634 /* Search for requested descriptor in I2C */
635 start_address = 2;
636 do {
637 status = read_rom(serial,
638 start_address,
639 sizeof(struct ti_i2c_desc),
640 (u8 *)rom_desc);
641 if (status)
642 return 0;
643
644 if (rom_desc->Type == desc_type)
645 return start_address;
646
647 start_address = start_address + sizeof(struct ti_i2c_desc) +
648 le16_to_cpu(rom_desc->Size);
649
650 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
651
652 return 0;
653 }
654
655 /* Validate descriptor checksum */
valid_csum(struct ti_i2c_desc * rom_desc,u8 * buffer)656 static int valid_csum(struct ti_i2c_desc *rom_desc, u8 *buffer)
657 {
658 u16 i;
659 u8 cs = 0;
660
661 for (i = 0; i < le16_to_cpu(rom_desc->Size); i++)
662 cs = (u8)(cs + buffer[i]);
663
664 if (cs != rom_desc->CheckSum) {
665 pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
666 return -EINVAL;
667 }
668 return 0;
669 }
670
671 /* Make sure that the I2C image is good */
check_i2c_image(struct edgeport_serial * serial)672 static int check_i2c_image(struct edgeport_serial *serial)
673 {
674 struct device *dev = &serial->serial->dev->dev;
675 int status = 0;
676 struct ti_i2c_desc *rom_desc;
677 int start_address = 2;
678 u8 *buffer;
679 u16 ttype;
680
681 rom_desc = kmalloc_obj(*rom_desc);
682 if (!rom_desc)
683 return -ENOMEM;
684
685 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
686 if (!buffer) {
687 kfree(rom_desc);
688 return -ENOMEM;
689 }
690
691 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
692 status = read_rom(serial, 0, 1, buffer);
693 if (status)
694 goto out;
695
696 if (*buffer != UMP5152 && *buffer != UMP3410) {
697 dev_err(dev, "%s - invalid buffer signature\n", __func__);
698 status = -ENODEV;
699 goto out;
700 }
701
702 do {
703 /* Validate the I2C */
704 status = read_rom(serial,
705 start_address,
706 sizeof(struct ti_i2c_desc),
707 (u8 *)rom_desc);
708 if (status)
709 break;
710
711 if ((start_address + sizeof(struct ti_i2c_desc) +
712 le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) {
713 status = -ENODEV;
714 dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
715 break;
716 }
717
718 dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
719
720 /* Skip type 2 record */
721 ttype = rom_desc->Type & 0x0f;
722 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
723 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
724 /* Read the descriptor data */
725 status = read_rom(serial, start_address +
726 sizeof(struct ti_i2c_desc),
727 le16_to_cpu(rom_desc->Size),
728 buffer);
729 if (status)
730 break;
731
732 status = valid_csum(rom_desc, buffer);
733 if (status)
734 break;
735 }
736 start_address = start_address + sizeof(struct ti_i2c_desc) +
737 le16_to_cpu(rom_desc->Size);
738
739 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
740 (start_address < TI_MAX_I2C_SIZE));
741
742 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
743 (start_address > TI_MAX_I2C_SIZE))
744 status = -ENODEV;
745
746 out:
747 kfree(buffer);
748 kfree(rom_desc);
749 return status;
750 }
751
get_manuf_info(struct edgeport_serial * serial,u8 * buffer)752 static int get_manuf_info(struct edgeport_serial *serial, u8 *buffer)
753 {
754 int status;
755 int start_address;
756 struct ti_i2c_desc *rom_desc;
757 struct edge_ti_manuf_descriptor *desc;
758 struct device *dev = &serial->serial->dev->dev;
759
760 rom_desc = kmalloc_obj(*rom_desc);
761 if (!rom_desc)
762 return -ENOMEM;
763
764 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
765 rom_desc);
766
767 if (!start_address) {
768 dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
769 status = -ENODEV;
770 goto exit;
771 }
772
773 /* Read the descriptor data */
774 if (le16_to_cpu(rom_desc->Size) != sizeof(struct edge_ti_manuf_descriptor)) {
775 dev_err(dev, "unexpected Edge descriptor length: %u\n",
776 le16_to_cpu(rom_desc->Size));
777 status = -EINVAL;
778 goto exit;
779 }
780 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
781 le16_to_cpu(rom_desc->Size), buffer);
782 if (status)
783 goto exit;
784
785 status = valid_csum(rom_desc, buffer);
786
787 desc = (struct edge_ti_manuf_descriptor *)buffer;
788 dev_dbg(dev, "%s - IonConfig 0x%x\n", __func__, desc->IonConfig);
789 dev_dbg(dev, "%s - Version %d\n", __func__, desc->Version);
790 dev_dbg(dev, "%s - Cpu/Board 0x%x\n", __func__, desc->CpuRev_BoardRev);
791 dev_dbg(dev, "%s - NumPorts %d\n", __func__, desc->NumPorts);
792 dev_dbg(dev, "%s - NumVirtualPorts %d\n", __func__, desc->NumVirtualPorts);
793 dev_dbg(dev, "%s - TotalPorts %d\n", __func__, desc->TotalPorts);
794
795 exit:
796 kfree(rom_desc);
797 return status;
798 }
799
800 /* Build firmware header used for firmware update */
build_i2c_fw_hdr(u8 * header,const struct firmware * fw)801 static int build_i2c_fw_hdr(u8 *header, const struct firmware *fw)
802 {
803 u8 *buffer;
804 int buffer_size;
805 int i;
806 u8 cs = 0;
807 struct ti_i2c_desc *i2c_header;
808 struct ti_i2c_image_header *img_header;
809 struct ti_i2c_firmware_rec *firmware_rec;
810 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
811
812 /*
813 * In order to update the I2C firmware we must change the type 2 record
814 * to type 0xF2. This will force the UMP to come up in Boot Mode.
815 * Then while in boot mode, the driver will download the latest
816 * firmware (padded to 15.5k) into the UMP ram. And finally when the
817 * device comes back up in download mode the driver will cause the new
818 * firmware to be copied from the UMP Ram to I2C and the firmware will
819 * update the record type from 0xf2 to 0x02.
820 */
821
822 /*
823 * Allocate a 15.5k buffer + 2 bytes for version number (Firmware
824 * Record)
825 */
826 buffer_size = (((1024 * 16) - 512 ) +
827 sizeof(struct ti_i2c_firmware_rec));
828
829 buffer = kmalloc(buffer_size, GFP_KERNEL);
830 if (!buffer)
831 return -ENOMEM;
832
833 /* Set entire image of 0xffs */
834 memset(buffer, 0xff, buffer_size);
835
836 /* Copy version number into firmware record */
837 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
838
839 firmware_rec->Ver_Major = fw_hdr->major_version;
840 firmware_rec->Ver_Minor = fw_hdr->minor_version;
841
842 /* Pointer to fw_down memory image */
843 img_header = (struct ti_i2c_image_header *)&fw->data[4];
844
845 if (le16_to_cpu(img_header->Length) >
846 buffer_size - sizeof(struct ti_i2c_firmware_rec)) {
847 kfree(buffer);
848 return -EINVAL;
849 }
850 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
851 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
852 le16_to_cpu(img_header->Length));
853
854 for (i=0; i < buffer_size; i++) {
855 cs = (u8)(cs + buffer[i]);
856 }
857
858 kfree(buffer);
859
860 /* Build new header */
861 i2c_header = (struct ti_i2c_desc *)header;
862 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
863
864 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
865 i2c_header->Size = cpu_to_le16(buffer_size);
866 i2c_header->CheckSum = cs;
867 firmware_rec->Ver_Major = fw_hdr->major_version;
868 firmware_rec->Ver_Minor = fw_hdr->minor_version;
869
870 return 0;
871 }
872
873 /* Try to figure out what type of I2c we have */
i2c_type_bootmode(struct edgeport_serial * serial)874 static int i2c_type_bootmode(struct edgeport_serial *serial)
875 {
876 struct device *dev = &serial->serial->dev->dev;
877 int status;
878 u8 *data;
879
880 data = kmalloc(1, GFP_KERNEL);
881 if (!data)
882 return -ENOMEM;
883
884 /* Try to read type 2 */
885 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
886 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
887 if (status)
888 dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
889 else
890 dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
891 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
892 dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
893 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
894 goto out;
895 }
896
897 /* Try to read type 3 */
898 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
899 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
900 if (status)
901 dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
902 else
903 dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
904 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
905 dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
906 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
907 goto out;
908 }
909
910 dev_dbg(dev, "%s - Unknown\n", __func__);
911 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
912 status = -ENODEV;
913 out:
914 kfree(data);
915 return status;
916 }
917
bulk_xfer(struct usb_serial * serial,void * buffer,int length,int * num_sent)918 static int bulk_xfer(struct usb_serial *serial, void *buffer,
919 int length, int *num_sent)
920 {
921 int status;
922
923 status = usb_bulk_msg(serial->dev,
924 usb_sndbulkpipe(serial->dev,
925 serial->port[0]->bulk_out_endpointAddress),
926 buffer, length, num_sent, 1000);
927 return status;
928 }
929
930 /* Download given firmware image to the device (IN BOOT MODE) */
download_code(struct edgeport_serial * serial,u8 * image,int image_length)931 static int download_code(struct edgeport_serial *serial, u8 *image,
932 int image_length)
933 {
934 int status = 0;
935 int pos;
936 int transfer;
937 int done;
938
939 /* Transfer firmware image */
940 for (pos = 0; pos < image_length; ) {
941 /* Read the next buffer from file */
942 transfer = image_length - pos;
943 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
944 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
945
946 /* Transfer data */
947 status = bulk_xfer(serial->serial, &image[pos],
948 transfer, &done);
949 if (status)
950 break;
951 /* Advance buffer pointer */
952 pos += done;
953 }
954
955 return status;
956 }
957
958 /* FIXME!!! */
config_boot_dev(struct usb_device * dev)959 static int config_boot_dev(struct usb_device *dev)
960 {
961 return 0;
962 }
963
ti_cpu_rev(struct edge_ti_manuf_descriptor * desc)964 static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
965 {
966 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
967 }
968
check_fw_sanity(struct edgeport_serial * serial,const struct firmware * fw)969 static int check_fw_sanity(struct edgeport_serial *serial,
970 const struct firmware *fw)
971 {
972 u16 length_total;
973 u8 checksum = 0;
974 int pos;
975 struct device *dev = &serial->serial->interface->dev;
976 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
977
978 if (fw->size < sizeof(struct edgeport_fw_hdr)) {
979 dev_err(dev, "incomplete fw header\n");
980 return -EINVAL;
981 }
982
983 length_total = le16_to_cpu(fw_hdr->length) +
984 sizeof(struct edgeport_fw_hdr);
985
986 if (fw->size != length_total) {
987 dev_err(dev, "bad fw size (expected: %u, got: %zu)\n",
988 length_total, fw->size);
989 return -EINVAL;
990 }
991
992 for (pos = sizeof(struct edgeport_fw_hdr); pos < fw->size; ++pos)
993 checksum += fw->data[pos];
994
995 if (checksum != fw_hdr->checksum) {
996 dev_err(dev, "bad fw checksum (expected: 0x%x, got: 0x%x)\n",
997 fw_hdr->checksum, checksum);
998 return -EINVAL;
999 }
1000
1001 return 0;
1002 }
1003
1004 /*
1005 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
1006 *
1007 * This routine downloads the main operating code into the TI5052, using the
1008 * boot code already burned into E2PROM or ROM.
1009 */
download_fw(struct edgeport_serial * serial)1010 static int download_fw(struct edgeport_serial *serial)
1011 {
1012 struct device *dev = &serial->serial->interface->dev;
1013 int status = 0;
1014 struct usb_interface_descriptor *interface;
1015 const struct firmware *fw;
1016 const char *fw_name = "edgeport/down3.bin";
1017 struct edgeport_fw_hdr *fw_hdr;
1018
1019 status = request_firmware(&fw, fw_name, dev);
1020 if (status) {
1021 dev_err(dev, "Failed to load image \"%s\" err %d\n",
1022 fw_name, status);
1023 return status;
1024 }
1025
1026 if (check_fw_sanity(serial, fw)) {
1027 status = -EINVAL;
1028 goto out;
1029 }
1030
1031 fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1032
1033 /* If on-board version is newer, "fw_version" will be updated later. */
1034 serial->fw_version = (fw_hdr->major_version << 8) +
1035 fw_hdr->minor_version;
1036
1037 /*
1038 * This routine is entered by both the BOOT mode and the Download mode
1039 * We can determine which code is running by the reading the config
1040 * descriptor and if we have only one bulk pipe it is in boot mode
1041 */
1042 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1043
1044 /* Default to type 2 i2c */
1045 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1046
1047 status = choose_config(serial->serial->dev);
1048 if (status)
1049 goto out;
1050
1051 interface = &serial->serial->interface->cur_altsetting->desc;
1052 if (!interface) {
1053 dev_err(dev, "%s - no interface set, error!\n", __func__);
1054 status = -ENODEV;
1055 goto out;
1056 }
1057
1058 /*
1059 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1060 * if we have more than one endpoint we are definitely in download
1061 * mode
1062 */
1063 if (interface->bNumEndpoints > 1) {
1064 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1065 status = do_download_mode(serial, fw);
1066 } else {
1067 /* Otherwise we will remain in configuring mode */
1068 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1069 status = do_boot_mode(serial, fw);
1070 }
1071
1072 out:
1073 release_firmware(fw);
1074 return status;
1075 }
1076
do_download_mode(struct edgeport_serial * serial,const struct firmware * fw)1077 static int do_download_mode(struct edgeport_serial *serial,
1078 const struct firmware *fw)
1079 {
1080 struct device *dev = &serial->serial->interface->dev;
1081 int status = 0;
1082 int start_address;
1083 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1084 int download_cur_ver;
1085 int download_new_ver;
1086 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1087 struct ti_i2c_desc *rom_desc;
1088
1089 dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
1090
1091 status = check_i2c_image(serial);
1092 if (status) {
1093 dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
1094 return status;
1095 }
1096
1097 /*
1098 * Validate Hardware version number
1099 * Read Manufacturing Descriptor from TI Based Edgeport
1100 */
1101 ti_manuf_desc = kmalloc_obj(*ti_manuf_desc);
1102 if (!ti_manuf_desc)
1103 return -ENOMEM;
1104
1105 status = get_manuf_info(serial, (u8 *)ti_manuf_desc);
1106 if (status) {
1107 kfree(ti_manuf_desc);
1108 return status;
1109 }
1110
1111 /* Check version number of ION descriptor */
1112 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1113 dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
1114 __func__, ti_cpu_rev(ti_manuf_desc));
1115 kfree(ti_manuf_desc);
1116 return -EINVAL;
1117 }
1118
1119 rom_desc = kmalloc_obj(*rom_desc);
1120 if (!rom_desc) {
1121 kfree(ti_manuf_desc);
1122 return -ENOMEM;
1123 }
1124
1125 /* Search for type 2 record (firmware record) */
1126 start_address = get_descriptor_addr(serial,
1127 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1128 if (start_address != 0) {
1129 struct ti_i2c_firmware_rec *firmware_version;
1130 u8 *record;
1131
1132 dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n",
1133 __func__);
1134
1135 firmware_version = kmalloc_obj(*firmware_version);
1136 if (!firmware_version) {
1137 kfree(rom_desc);
1138 kfree(ti_manuf_desc);
1139 return -ENOMEM;
1140 }
1141
1142 /*
1143 * Validate version number
1144 * Read the descriptor data
1145 */
1146 status = read_rom(serial, start_address +
1147 sizeof(struct ti_i2c_desc),
1148 sizeof(struct ti_i2c_firmware_rec),
1149 (u8 *)firmware_version);
1150 if (status) {
1151 kfree(firmware_version);
1152 kfree(rom_desc);
1153 kfree(ti_manuf_desc);
1154 return status;
1155 }
1156
1157 /*
1158 * Check version number of download with current
1159 * version in I2c
1160 */
1161 download_cur_ver = (firmware_version->Ver_Major << 8) +
1162 (firmware_version->Ver_Minor);
1163 download_new_ver = (fw_hdr->major_version << 8) +
1164 (fw_hdr->minor_version);
1165
1166 dev_dbg(dev, "%s - >> FW Versions Device %d.%d Driver %d.%d\n",
1167 __func__, firmware_version->Ver_Major,
1168 firmware_version->Ver_Minor,
1169 fw_hdr->major_version, fw_hdr->minor_version);
1170
1171 /*
1172 * Check if we have an old version in the I2C and
1173 * update if necessary
1174 */
1175 if (download_cur_ver < download_new_ver) {
1176 dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
1177 __func__,
1178 firmware_version->Ver_Major,
1179 firmware_version->Ver_Minor,
1180 fw_hdr->major_version,
1181 fw_hdr->minor_version);
1182
1183 record = kmalloc(1, GFP_KERNEL);
1184 if (!record) {
1185 kfree(firmware_version);
1186 kfree(rom_desc);
1187 kfree(ti_manuf_desc);
1188 return -ENOMEM;
1189 }
1190 /*
1191 * In order to update the I2C firmware we must
1192 * change the type 2 record to type 0xF2. This
1193 * will force the UMP to come up in Boot Mode.
1194 * Then while in boot mode, the driver will
1195 * download the latest firmware (padded to
1196 * 15.5k) into the UMP ram. Finally when the
1197 * device comes back up in download mode the
1198 * driver will cause the new firmware to be
1199 * copied from the UMP Ram to I2C and the
1200 * firmware will update the record type from
1201 * 0xf2 to 0x02.
1202 */
1203 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1204
1205 /*
1206 * Change the I2C Firmware record type to
1207 * 0xf2 to trigger an update
1208 */
1209 status = write_rom(serial, start_address,
1210 sizeof(*record), record);
1211 if (status) {
1212 kfree(record);
1213 kfree(firmware_version);
1214 kfree(rom_desc);
1215 kfree(ti_manuf_desc);
1216 return status;
1217 }
1218
1219 /*
1220 * verify the write -- must do this in order
1221 * for write to complete before we do the
1222 * hardware reset
1223 */
1224 status = read_rom(serial,
1225 start_address,
1226 sizeof(*record),
1227 record);
1228 if (status) {
1229 kfree(record);
1230 kfree(firmware_version);
1231 kfree(rom_desc);
1232 kfree(ti_manuf_desc);
1233 return status;
1234 }
1235
1236 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1237 dev_err(dev, "%s - error resetting device\n",
1238 __func__);
1239 kfree(record);
1240 kfree(firmware_version);
1241 kfree(rom_desc);
1242 kfree(ti_manuf_desc);
1243 return -ENODEV;
1244 }
1245
1246 dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
1247
1248 /* Reset UMP -- Back to BOOT MODE */
1249 status = ti_vsend_sync(serial->serial->dev,
1250 UMPC_HARDWARE_RESET,
1251 0, 0, NULL, 0,
1252 TI_VSEND_TIMEOUT_DEFAULT);
1253
1254 dev_dbg(dev, "%s - HARDWARE RESET return %d\n",
1255 __func__, status);
1256
1257 /* return an error on purpose. */
1258 kfree(record);
1259 kfree(firmware_version);
1260 kfree(rom_desc);
1261 kfree(ti_manuf_desc);
1262 return -ENODEV;
1263 }
1264 /* Same or newer fw version is already loaded */
1265 serial->fw_version = download_cur_ver;
1266 kfree(firmware_version);
1267 }
1268 /* Search for type 0xF2 record (firmware blank record) */
1269 else {
1270 start_address = get_descriptor_addr(serial,
1271 I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc);
1272 if (start_address != 0) {
1273 #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1274 sizeof(struct ti_i2c_firmware_rec))
1275 u8 *header;
1276 u8 *vheader;
1277
1278 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
1279 if (!header) {
1280 kfree(rom_desc);
1281 kfree(ti_manuf_desc);
1282 return -ENOMEM;
1283 }
1284
1285 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1286 if (!vheader) {
1287 kfree(header);
1288 kfree(rom_desc);
1289 kfree(ti_manuf_desc);
1290 return -ENOMEM;
1291 }
1292
1293 dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n",
1294 __func__);
1295
1296 /*
1297 * In order to update the I2C firmware we must change
1298 * the type 2 record to type 0xF2. This will force the
1299 * UMP to come up in Boot Mode. Then while in boot
1300 * mode, the driver will download the latest firmware
1301 * (padded to 15.5k) into the UMP ram. Finally when the
1302 * device comes back up in download mode the driver
1303 * will cause the new firmware to be copied from the
1304 * UMP Ram to I2C and the firmware will update the
1305 * record type from 0xf2 to 0x02.
1306 */
1307 status = build_i2c_fw_hdr(header, fw);
1308 if (status) {
1309 kfree(vheader);
1310 kfree(header);
1311 kfree(rom_desc);
1312 kfree(ti_manuf_desc);
1313 return -EINVAL;
1314 }
1315
1316 /*
1317 * Update I2C with type 0xf2 record with correct
1318 * size and checksum
1319 */
1320 status = write_rom(serial,
1321 start_address,
1322 HEADER_SIZE,
1323 header);
1324 if (status) {
1325 kfree(vheader);
1326 kfree(header);
1327 kfree(rom_desc);
1328 kfree(ti_manuf_desc);
1329 return -EINVAL;
1330 }
1331
1332 /*
1333 * verify the write -- must do this in order for
1334 * write to complete before we do the hardware reset
1335 */
1336 status = read_rom(serial, start_address,
1337 HEADER_SIZE, vheader);
1338
1339 if (status) {
1340 dev_dbg(dev, "%s - can't read header back\n",
1341 __func__);
1342 kfree(vheader);
1343 kfree(header);
1344 kfree(rom_desc);
1345 kfree(ti_manuf_desc);
1346 return status;
1347 }
1348 if (memcmp(vheader, header, HEADER_SIZE)) {
1349 dev_dbg(dev, "%s - write download record failed\n",
1350 __func__);
1351 kfree(vheader);
1352 kfree(header);
1353 kfree(rom_desc);
1354 kfree(ti_manuf_desc);
1355 return -EINVAL;
1356 }
1357
1358 kfree(vheader);
1359 kfree(header);
1360
1361 dev_dbg(dev, "%s - Start firmware update\n", __func__);
1362
1363 /* Tell firmware to copy download image into I2C */
1364 status = ti_vsend_sync(serial->serial->dev,
1365 UMPC_COPY_DNLD_TO_I2C,
1366 0, 0, NULL, 0,
1367 TI_VSEND_TIMEOUT_FW_DOWNLOAD);
1368
1369 dev_dbg(dev, "%s - Update complete 0x%x\n", __func__,
1370 status);
1371 if (status) {
1372 dev_err(dev,
1373 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1374 __func__);
1375 kfree(rom_desc);
1376 kfree(ti_manuf_desc);
1377 return status;
1378 }
1379 }
1380 }
1381
1382 /* The device is running the download code */
1383 kfree(rom_desc);
1384 kfree(ti_manuf_desc);
1385 return 0;
1386 }
1387
do_boot_mode(struct edgeport_serial * serial,const struct firmware * fw)1388 static int do_boot_mode(struct edgeport_serial *serial,
1389 const struct firmware *fw)
1390 {
1391 struct device *dev = &serial->serial->interface->dev;
1392 int status = 0;
1393 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1394 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1395
1396 dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
1397
1398 /* Configure the TI device so we can use the BULK pipes for download */
1399 status = config_boot_dev(serial->serial->dev);
1400 if (status)
1401 return status;
1402
1403 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1404 != USB_VENDOR_ID_ION) {
1405 dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
1406 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1407 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1408 goto stayinbootmode;
1409 }
1410
1411 /*
1412 * We have an ION device (I2c Must be programmed)
1413 * Determine I2C image type
1414 */
1415 if (i2c_type_bootmode(serial))
1416 goto stayinbootmode;
1417
1418 /* Check for ION Vendor ID and that the I2C is valid */
1419 if (!check_i2c_image(serial)) {
1420 struct ti_i2c_image_header *header;
1421 int i;
1422 u8 cs = 0;
1423 u8 *buffer;
1424 int buffer_size;
1425
1426 /*
1427 * Validate Hardware version number
1428 * Read Manufacturing Descriptor from TI Based Edgeport
1429 */
1430 ti_manuf_desc = kmalloc_obj(*ti_manuf_desc);
1431 if (!ti_manuf_desc)
1432 return -ENOMEM;
1433
1434 status = get_manuf_info(serial, (u8 *)ti_manuf_desc);
1435 if (status) {
1436 kfree(ti_manuf_desc);
1437 goto stayinbootmode;
1438 }
1439
1440 /* Check for version 2 */
1441 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1442 dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
1443 __func__, ti_cpu_rev(ti_manuf_desc));
1444 kfree(ti_manuf_desc);
1445 goto stayinbootmode;
1446 }
1447
1448 kfree(ti_manuf_desc);
1449
1450 /*
1451 * In order to update the I2C firmware we must change the type
1452 * 2 record to type 0xF2. This will force the UMP to come up
1453 * in Boot Mode. Then while in boot mode, the driver will
1454 * download the latest firmware (padded to 15.5k) into the
1455 * UMP ram. Finally when the device comes back up in download
1456 * mode the driver will cause the new firmware to be copied
1457 * from the UMP Ram to I2C and the firmware will update the
1458 * record type from 0xf2 to 0x02.
1459 *
1460 * Do we really have to copy the whole firmware image,
1461 * or could we do this in place!
1462 */
1463
1464 /* Allocate a 15.5k buffer + 3 byte header */
1465 buffer_size = (((1024 * 16) - 512) +
1466 sizeof(struct ti_i2c_image_header));
1467 if (fw->size - 4 > buffer_size) {
1468 dev_err(dev, "%s - firmware image is too large\n",
1469 __func__);
1470 return -EINVAL;
1471 }
1472
1473 buffer = kmalloc(buffer_size, GFP_KERNEL);
1474 if (!buffer)
1475 return -ENOMEM;
1476
1477 /* Initialize the buffer to 0xff (pad the buffer) */
1478 memset(buffer, 0xff, buffer_size);
1479 memcpy(buffer, &fw->data[4], fw->size - 4);
1480
1481 for (i = sizeof(struct ti_i2c_image_header);
1482 i < buffer_size; i++) {
1483 cs = (u8)(cs + buffer[i]);
1484 }
1485
1486 header = (struct ti_i2c_image_header *)buffer;
1487
1488 /* update length and checksum after padding */
1489 header->Length = cpu_to_le16((u16)(buffer_size -
1490 sizeof(struct ti_i2c_image_header)));
1491 header->CheckSum = cs;
1492
1493 /* Download the operational code */
1494 dev_dbg(dev, "%s - Downloading operational code image version %d.%d (TI UMP)\n",
1495 __func__,
1496 fw_hdr->major_version, fw_hdr->minor_version);
1497 status = download_code(serial, buffer, buffer_size);
1498
1499 kfree(buffer);
1500
1501 if (status) {
1502 dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
1503 return status;
1504 }
1505
1506 /* Device will reboot */
1507 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1508
1509 dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
1510
1511 return 1;
1512 }
1513
1514 stayinbootmode:
1515 /* Eprom is invalid or blank stay in boot mode */
1516 dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
1517 serial->product_info.TiMode = TI_MODE_BOOT;
1518
1519 return 1;
1520 }
1521
ti_do_config(struct edgeport_port * port,int feature,int on)1522 static int ti_do_config(struct edgeport_port *port, int feature, int on)
1523 {
1524 on = !!on; /* 1 or 0 not bitmask */
1525
1526 return send_port_cmd(port->port, feature, on, NULL, 0);
1527 }
1528
restore_mcr(struct edgeport_port * port,u8 mcr)1529 static int restore_mcr(struct edgeport_port *port, u8 mcr)
1530 {
1531 int status = 0;
1532
1533 dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
1534
1535 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
1536 if (status)
1537 return status;
1538 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
1539 if (status)
1540 return status;
1541 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
1542 }
1543
1544 /* Convert TI LSR to standard UART flags */
map_line_status(u8 ti_lsr)1545 static u8 map_line_status(u8 ti_lsr)
1546 {
1547 u8 lsr = 0;
1548
1549 #define MAP_FLAG(flagUmp, flagUart) \
1550 if (ti_lsr & flagUmp) \
1551 lsr |= flagUart;
1552
1553 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1554 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1555 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1556 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
1557 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1558 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
1559
1560 #undef MAP_FLAG
1561
1562 return lsr;
1563 }
1564
handle_new_msr(struct edgeport_port * edge_port,u8 msr)1565 static void handle_new_msr(struct edgeport_port *edge_port, u8 msr)
1566 {
1567 struct async_icount *icount;
1568 struct tty_struct *tty;
1569
1570 dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
1571
1572 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1573 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1574 icount = &edge_port->port->icount;
1575
1576 /* update input line counters */
1577 if (msr & EDGEPORT_MSR_DELTA_CTS)
1578 icount->cts++;
1579 if (msr & EDGEPORT_MSR_DELTA_DSR)
1580 icount->dsr++;
1581 if (msr & EDGEPORT_MSR_DELTA_CD)
1582 icount->dcd++;
1583 if (msr & EDGEPORT_MSR_DELTA_RI)
1584 icount->rng++;
1585 wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
1586 }
1587
1588 /* Save the new modem status */
1589 edge_port->shadow_msr = msr & 0xf0;
1590
1591 tty = tty_port_tty_get(&edge_port->port->port);
1592 /* handle CTS flow control */
1593 if (tty && C_CRTSCTS(tty)) {
1594 if (msr & EDGEPORT_MSR_CTS)
1595 tty_wakeup(tty);
1596 }
1597 tty_kref_put(tty);
1598 }
1599
handle_new_lsr(struct edgeport_port * edge_port,int lsr_data,u8 lsr,u8 data)1600 static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1601 u8 lsr, u8 data)
1602 {
1603 struct async_icount *icount;
1604 u8 new_lsr = (u8)(lsr & (u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1605 LSR_FRM_ERR | LSR_BREAK));
1606
1607 dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
1608
1609 edge_port->shadow_lsr = lsr;
1610
1611 if (new_lsr & LSR_BREAK)
1612 /*
1613 * Parity and Framing errors only count if they
1614 * occur exclusive of a break being received.
1615 */
1616 new_lsr &= (u8)(LSR_OVER_ERR | LSR_BREAK);
1617
1618 /* Place LSR data byte into Rx buffer */
1619 if (lsr_data)
1620 edge_tty_recv(edge_port->port, &data, 1);
1621
1622 /* update input line counters */
1623 icount = &edge_port->port->icount;
1624 if (new_lsr & LSR_BREAK)
1625 icount->brk++;
1626 if (new_lsr & LSR_OVER_ERR)
1627 icount->overrun++;
1628 if (new_lsr & LSR_PAR_ERR)
1629 icount->parity++;
1630 if (new_lsr & LSR_FRM_ERR)
1631 icount->frame++;
1632 }
1633
edge_interrupt_callback(struct urb * urb)1634 static void edge_interrupt_callback(struct urb *urb)
1635 {
1636 struct edgeport_serial *edge_serial = urb->context;
1637 struct usb_serial_port *port;
1638 struct edgeport_port *edge_port;
1639 struct device *dev;
1640 unsigned char *data = urb->transfer_buffer;
1641 int length = urb->actual_length;
1642 int port_number;
1643 int function;
1644 int retval;
1645 u8 lsr;
1646 u8 msr;
1647 int status = urb->status;
1648
1649 switch (status) {
1650 case 0:
1651 /* success */
1652 break;
1653 case -ECONNRESET:
1654 case -ENOENT:
1655 case -ESHUTDOWN:
1656 /* this urb is terminated, clean up */
1657 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1658 __func__, status);
1659 return;
1660 default:
1661 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1662 "%d\n", __func__, status);
1663 goto exit;
1664 }
1665
1666 if (!length) {
1667 dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
1668 goto exit;
1669 }
1670
1671 dev = &edge_serial->serial->dev->dev;
1672 usb_serial_debug_data(dev, __func__, length, data);
1673
1674 if (length != 2) {
1675 dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
1676 goto exit;
1677 }
1678
1679 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1680 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1681 dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
1682 port_number, function, data[1]);
1683
1684 if (port_number >= edge_serial->serial->num_ports) {
1685 dev_err(dev, "bad port number %d\n", port_number);
1686 goto exit;
1687 }
1688
1689 port = edge_serial->serial->port[port_number];
1690 edge_port = usb_get_serial_port_data(port);
1691 if (!edge_port) {
1692 dev_dbg(dev, "%s - edge_port not found\n", __func__);
1693 return;
1694 }
1695 switch (function) {
1696 case TIUMP_INTERRUPT_CODE_LSR:
1697 lsr = map_line_status(data[1]);
1698 if (lsr & UMP_UART_LSR_DATA_MASK) {
1699 /*
1700 * Save the LSR event for bulk read completion routine
1701 */
1702 dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
1703 __func__, port_number, lsr);
1704 edge_port->lsr_event = 1;
1705 edge_port->lsr_mask = lsr;
1706 } else {
1707 dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
1708 __func__, port_number, lsr);
1709 handle_new_lsr(edge_port, 0, lsr, 0);
1710 }
1711 break;
1712
1713 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
1714 /* Copy MSR from UMP */
1715 msr = data[1];
1716 dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
1717 __func__, port_number, msr);
1718 handle_new_msr(edge_port, msr);
1719 break;
1720
1721 default:
1722 dev_err(&urb->dev->dev,
1723 "%s - Unknown Interrupt code from UMP %x\n",
1724 __func__, data[1]);
1725 break;
1726
1727 }
1728
1729 exit:
1730 retval = usb_submit_urb(urb, GFP_ATOMIC);
1731 if (retval)
1732 dev_err(&urb->dev->dev,
1733 "%s - usb_submit_urb failed with result %d\n",
1734 __func__, retval);
1735 }
1736
edge_bulk_in_callback(struct urb * urb)1737 static void edge_bulk_in_callback(struct urb *urb)
1738 {
1739 struct edgeport_port *edge_port = urb->context;
1740 struct device *dev = &edge_port->port->dev;
1741 unsigned char *data = urb->transfer_buffer;
1742 unsigned long flags;
1743 int retval = 0;
1744 int port_number;
1745 int status = urb->status;
1746
1747 switch (status) {
1748 case 0:
1749 /* success */
1750 break;
1751 case -ECONNRESET:
1752 case -ENOENT:
1753 case -ESHUTDOWN:
1754 /* this urb is terminated, clean up */
1755 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
1756 return;
1757 default:
1758 dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
1759 }
1760
1761 if (status == -EPIPE)
1762 goto exit;
1763
1764 if (status) {
1765 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
1766 return;
1767 }
1768
1769 port_number = edge_port->port->port_number;
1770
1771 if (urb->actual_length > 0 && edge_port->lsr_event) {
1772 edge_port->lsr_event = 0;
1773 dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
1774 __func__, port_number, edge_port->lsr_mask, *data);
1775 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
1776 /* Adjust buffer length/pointer */
1777 --urb->actual_length;
1778 ++data;
1779 }
1780
1781 if (urb->actual_length) {
1782 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
1783 if (edge_port->close_pending)
1784 dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
1785 __func__);
1786 else
1787 edge_tty_recv(edge_port->port, data,
1788 urb->actual_length);
1789 edge_port->port->icount.rx += urb->actual_length;
1790 }
1791
1792 exit:
1793 /* continue read unless stopped */
1794 spin_lock_irqsave(&edge_port->ep_lock, flags);
1795 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
1796 retval = usb_submit_urb(urb, GFP_ATOMIC);
1797 else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
1798 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1799
1800 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1801 if (retval)
1802 dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
1803 }
1804
edge_tty_recv(struct usb_serial_port * port,unsigned char * data,int length)1805 static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1806 int length)
1807 {
1808 int queued;
1809
1810 queued = tty_insert_flip_string(&port->port, data, length);
1811 if (queued < length)
1812 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1813 __func__, length - queued);
1814 tty_flip_buffer_push(&port->port);
1815 }
1816
edge_bulk_out_callback(struct urb * urb)1817 static void edge_bulk_out_callback(struct urb *urb)
1818 {
1819 struct usb_serial_port *port = urb->context;
1820 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1821 int status = urb->status;
1822 struct tty_struct *tty;
1823
1824 edge_port->ep_write_urb_in_use = 0;
1825
1826 switch (status) {
1827 case 0:
1828 /* success */
1829 break;
1830 case -ECONNRESET:
1831 case -ENOENT:
1832 case -ESHUTDOWN:
1833 /* this urb is terminated, clean up */
1834 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1835 __func__, status);
1836 return;
1837 default:
1838 dev_err_console(port, "%s - nonzero write bulk status "
1839 "received: %d\n", __func__, status);
1840 }
1841
1842 /* send any buffered data */
1843 tty = tty_port_tty_get(&port->port);
1844 edge_send(port, tty);
1845 tty_kref_put(tty);
1846 }
1847
edge_open(struct tty_struct * tty,struct usb_serial_port * port)1848 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
1849 {
1850 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1851 struct edgeport_serial *edge_serial;
1852 struct usb_device *dev;
1853 struct urb *urb;
1854 int status;
1855 u16 open_settings;
1856 u8 transaction_timeout;
1857
1858 if (edge_port == NULL)
1859 return -ENODEV;
1860
1861 dev = port->serial->dev;
1862
1863 /* turn off loopback */
1864 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
1865 if (status) {
1866 dev_err(&port->dev,
1867 "%s - cannot send clear loopback command, %d\n",
1868 __func__, status);
1869 return status;
1870 }
1871
1872 /* set up the port settings */
1873 if (tty)
1874 edge_set_termios(tty, port, &tty->termios);
1875
1876 /* open up the port */
1877
1878 /* milliseconds to timeout for DMA transfer */
1879 transaction_timeout = 2;
1880
1881 edge_port->ump_read_timeout =
1882 max(20, ((transaction_timeout * 3) / 2));
1883
1884 /* milliseconds to timeout for DMA transfer */
1885 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1886 UMP_PIPE_TRANS_TIMEOUT_ENA |
1887 (transaction_timeout << 2));
1888
1889 dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
1890
1891 /* Tell TI to open and start the port */
1892 status = send_port_cmd(port, UMPC_OPEN_PORT, open_settings, NULL, 0);
1893 if (status) {
1894 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1895 __func__, status);
1896 return status;
1897 }
1898
1899 /* Start the DMA? */
1900 status = send_port_cmd(port, UMPC_START_PORT, 0, NULL, 0);
1901 if (status) {
1902 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1903 __func__, status);
1904 return status;
1905 }
1906
1907 /* Clear TX and RX buffers in UMP */
1908 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
1909 if (status) {
1910 dev_err(&port->dev,
1911 "%s - cannot send clear buffers command, %d\n",
1912 __func__, status);
1913 return status;
1914 }
1915
1916 /* Read Initial MSR */
1917 status = read_port_cmd(port, UMPC_READ_MSR, 0, &edge_port->shadow_msr, 1);
1918 if (status) {
1919 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1920 __func__, status);
1921 return status;
1922 }
1923
1924 dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
1925
1926 /* Set Initial MCR */
1927 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
1928 dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
1929
1930 edge_serial = edge_port->edge_serial;
1931 if (mutex_lock_interruptible(&edge_serial->es_lock))
1932 return -ERESTARTSYS;
1933 if (edge_serial->num_ports_open == 0) {
1934 /* we are the first port to open, post the interrupt urb */
1935 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1936 urb->context = edge_serial;
1937 status = usb_submit_urb(urb, GFP_KERNEL);
1938 if (status) {
1939 dev_err(&port->dev,
1940 "%s - usb_submit_urb failed with value %d\n",
1941 __func__, status);
1942 goto release_es_lock;
1943 }
1944 }
1945
1946 /*
1947 * reset the data toggle on the bulk endpoints to work around bug in
1948 * host controllers where things get out of sync some times
1949 */
1950 usb_clear_halt(dev, port->write_urb->pipe);
1951 usb_clear_halt(dev, port->read_urb->pipe);
1952
1953 /* start up our bulk read urb */
1954 urb = port->read_urb;
1955 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
1956 urb->context = edge_port;
1957 status = usb_submit_urb(urb, GFP_KERNEL);
1958 if (status) {
1959 dev_err(&port->dev,
1960 "%s - read bulk usb_submit_urb failed with value %d\n",
1961 __func__, status);
1962 goto unlink_int_urb;
1963 }
1964
1965 ++edge_serial->num_ports_open;
1966
1967 goto release_es_lock;
1968
1969 unlink_int_urb:
1970 if (edge_port->edge_serial->num_ports_open == 0)
1971 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1972 release_es_lock:
1973 mutex_unlock(&edge_serial->es_lock);
1974 return status;
1975 }
1976
edge_close(struct usb_serial_port * port)1977 static void edge_close(struct usb_serial_port *port)
1978 {
1979 struct edgeport_serial *edge_serial;
1980 struct edgeport_port *edge_port;
1981 unsigned long flags;
1982
1983 edge_serial = usb_get_serial_data(port->serial);
1984 edge_port = usb_get_serial_port_data(port);
1985 if (edge_serial == NULL || edge_port == NULL)
1986 return;
1987
1988 /*
1989 * The bulkreadcompletion routine will check
1990 * this flag and dump add read data
1991 */
1992 edge_port->close_pending = 1;
1993
1994 usb_kill_urb(port->read_urb);
1995 usb_kill_urb(port->write_urb);
1996 edge_port->ep_write_urb_in_use = 0;
1997 spin_lock_irqsave(&edge_port->ep_lock, flags);
1998 kfifo_reset_out(&port->write_fifo);
1999 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2000
2001 dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
2002 send_port_cmd(port, UMPC_CLOSE_PORT, 0, NULL, 0);
2003
2004 mutex_lock(&edge_serial->es_lock);
2005 --edge_port->edge_serial->num_ports_open;
2006 if (edge_port->edge_serial->num_ports_open <= 0) {
2007 /* last port is now closed, let's shut down our interrupt urb */
2008 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2009 edge_port->edge_serial->num_ports_open = 0;
2010 }
2011 mutex_unlock(&edge_serial->es_lock);
2012 edge_port->close_pending = 0;
2013 }
2014
edge_write(struct tty_struct * tty,struct usb_serial_port * port,const unsigned char * data,int count)2015 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2016 const unsigned char *data, int count)
2017 {
2018 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2019
2020 if (count == 0) {
2021 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
2022 return 0;
2023 }
2024
2025 if (edge_port == NULL)
2026 return -ENODEV;
2027 if (edge_port->close_pending == 1)
2028 return -ENODEV;
2029
2030 count = kfifo_in_locked(&port->write_fifo, data, count,
2031 &edge_port->ep_lock);
2032 edge_send(port, tty);
2033
2034 return count;
2035 }
2036
edge_send(struct usb_serial_port * port,struct tty_struct * tty)2037 static void edge_send(struct usb_serial_port *port, struct tty_struct *tty)
2038 {
2039 int count, result;
2040 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2041 unsigned long flags;
2042
2043 spin_lock_irqsave(&edge_port->ep_lock, flags);
2044
2045 if (edge_port->ep_write_urb_in_use) {
2046 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2047 return;
2048 }
2049
2050 count = kfifo_out(&port->write_fifo,
2051 port->write_urb->transfer_buffer,
2052 port->bulk_out_size);
2053
2054 if (count == 0) {
2055 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2056 return;
2057 }
2058
2059 edge_port->ep_write_urb_in_use = 1;
2060
2061 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2062
2063 usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
2064
2065 /* set up our urb */
2066 port->write_urb->transfer_buffer_length = count;
2067
2068 /* send the data out the bulk port */
2069 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2070 if (result) {
2071 dev_err_console(port,
2072 "%s - failed submitting write urb, error %d\n",
2073 __func__, result);
2074 edge_port->ep_write_urb_in_use = 0;
2075 /* TODO: reschedule edge_send */
2076 } else
2077 edge_port->port->icount.tx += count;
2078
2079 /*
2080 * wakeup any process waiting for writes to complete
2081 * there is now more room in the buffer for new writes
2082 */
2083 if (tty)
2084 tty_wakeup(tty);
2085 }
2086
edge_write_room(struct tty_struct * tty)2087 static unsigned int edge_write_room(struct tty_struct *tty)
2088 {
2089 struct usb_serial_port *port = tty->driver_data;
2090 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2091 unsigned int room;
2092 unsigned long flags;
2093
2094 if (edge_port == NULL)
2095 return 0;
2096 if (edge_port->close_pending == 1)
2097 return 0;
2098
2099 spin_lock_irqsave(&edge_port->ep_lock, flags);
2100 room = kfifo_avail(&port->write_fifo);
2101 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2102
2103 dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
2104 return room;
2105 }
2106
edge_chars_in_buffer(struct tty_struct * tty)2107 static unsigned int edge_chars_in_buffer(struct tty_struct *tty)
2108 {
2109 struct usb_serial_port *port = tty->driver_data;
2110 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2111 unsigned int chars;
2112 unsigned long flags;
2113 if (edge_port == NULL)
2114 return 0;
2115
2116 spin_lock_irqsave(&edge_port->ep_lock, flags);
2117 chars = kfifo_len(&port->write_fifo);
2118 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2119
2120 dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
2121 return chars;
2122 }
2123
edge_tx_empty(struct usb_serial_port * port)2124 static bool edge_tx_empty(struct usb_serial_port *port)
2125 {
2126 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2127 int ret;
2128
2129 ret = tx_active(edge_port);
2130 if (ret > 0)
2131 return false;
2132
2133 return true;
2134 }
2135
edge_throttle(struct tty_struct * tty)2136 static void edge_throttle(struct tty_struct *tty)
2137 {
2138 struct usb_serial_port *port = tty->driver_data;
2139 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2140 int status;
2141
2142 if (edge_port == NULL)
2143 return;
2144
2145 /* if we are implementing XON/XOFF, send the stop character */
2146 if (I_IXOFF(tty)) {
2147 unsigned char stop_char = STOP_CHAR(tty);
2148 status = edge_write(tty, port, &stop_char, 1);
2149 if (status <= 0) {
2150 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2151 }
2152 }
2153
2154 /*
2155 * if we are implementing RTS/CTS, stop reads
2156 * and the Edgeport will clear the RTS line
2157 */
2158 if (C_CRTSCTS(tty))
2159 stop_read(edge_port);
2160
2161 }
2162
edge_unthrottle(struct tty_struct * tty)2163 static void edge_unthrottle(struct tty_struct *tty)
2164 {
2165 struct usb_serial_port *port = tty->driver_data;
2166 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2167 int status;
2168
2169 if (edge_port == NULL)
2170 return;
2171
2172 /* if we are implementing XON/XOFF, send the start character */
2173 if (I_IXOFF(tty)) {
2174 unsigned char start_char = START_CHAR(tty);
2175 status = edge_write(tty, port, &start_char, 1);
2176 if (status <= 0) {
2177 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2178 }
2179 }
2180 /*
2181 * if we are implementing RTS/CTS, restart reads
2182 * are the Edgeport will assert the RTS line
2183 */
2184 if (C_CRTSCTS(tty)) {
2185 status = restart_read(edge_port);
2186 if (status)
2187 dev_err(&port->dev,
2188 "%s - read bulk usb_submit_urb failed: %d\n",
2189 __func__, status);
2190 }
2191
2192 }
2193
stop_read(struct edgeport_port * edge_port)2194 static void stop_read(struct edgeport_port *edge_port)
2195 {
2196 unsigned long flags;
2197
2198 spin_lock_irqsave(&edge_port->ep_lock, flags);
2199
2200 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2201 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2202 edge_port->shadow_mcr &= ~MCR_RTS;
2203
2204 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2205 }
2206
restart_read(struct edgeport_port * edge_port)2207 static int restart_read(struct edgeport_port *edge_port)
2208 {
2209 struct urb *urb;
2210 int status = 0;
2211 unsigned long flags;
2212
2213 spin_lock_irqsave(&edge_port->ep_lock, flags);
2214
2215 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2216 urb = edge_port->port->read_urb;
2217 status = usb_submit_urb(urb, GFP_ATOMIC);
2218 }
2219 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2220 edge_port->shadow_mcr |= MCR_RTS;
2221
2222 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2223
2224 return status;
2225 }
2226
change_port_settings(struct tty_struct * tty,struct edgeport_port * edge_port,const struct ktermios * old_termios)2227 static void change_port_settings(struct tty_struct *tty,
2228 struct edgeport_port *edge_port, const struct ktermios *old_termios)
2229 {
2230 struct device *dev = &edge_port->port->dev;
2231 struct ump_uart_config *config;
2232 int baud;
2233 unsigned cflag;
2234 int status;
2235
2236 config = kmalloc_obj(*config);
2237 if (!config) {
2238 tty->termios = *old_termios;
2239 return;
2240 }
2241
2242 cflag = tty->termios.c_cflag;
2243
2244 config->wFlags = 0;
2245
2246 /* These flags must be set */
2247 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2248 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2249 config->bUartMode = (u8)(edge_port->bUartMode);
2250
2251 switch (cflag & CSIZE) {
2252 case CS5:
2253 config->bDataBits = UMP_UART_CHAR5BITS;
2254 dev_dbg(dev, "%s - data bits = 5\n", __func__);
2255 break;
2256 case CS6:
2257 config->bDataBits = UMP_UART_CHAR6BITS;
2258 dev_dbg(dev, "%s - data bits = 6\n", __func__);
2259 break;
2260 case CS7:
2261 config->bDataBits = UMP_UART_CHAR7BITS;
2262 dev_dbg(dev, "%s - data bits = 7\n", __func__);
2263 break;
2264 default:
2265 case CS8:
2266 config->bDataBits = UMP_UART_CHAR8BITS;
2267 dev_dbg(dev, "%s - data bits = 8\n", __func__);
2268 break;
2269 }
2270
2271 if (cflag & PARENB) {
2272 if (cflag & PARODD) {
2273 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2274 config->bParity = UMP_UART_ODDPARITY;
2275 dev_dbg(dev, "%s - parity = odd\n", __func__);
2276 } else {
2277 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2278 config->bParity = UMP_UART_EVENPARITY;
2279 dev_dbg(dev, "%s - parity = even\n", __func__);
2280 }
2281 } else {
2282 config->bParity = UMP_UART_NOPARITY;
2283 dev_dbg(dev, "%s - parity = none\n", __func__);
2284 }
2285
2286 if (cflag & CSTOPB) {
2287 config->bStopBits = UMP_UART_STOPBIT2;
2288 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
2289 } else {
2290 config->bStopBits = UMP_UART_STOPBIT1;
2291 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
2292 }
2293
2294 /* figure out the flow control settings */
2295 if (cflag & CRTSCTS) {
2296 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2297 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2298 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
2299 } else {
2300 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
2301 restart_read(edge_port);
2302 }
2303
2304 /*
2305 * if we are implementing XON/XOFF, set the start and stop
2306 * character in the device
2307 */
2308 config->cXon = START_CHAR(tty);
2309 config->cXoff = STOP_CHAR(tty);
2310
2311 /* if we are implementing INBOUND XON/XOFF */
2312 if (I_IXOFF(tty)) {
2313 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2314 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2315 __func__, config->cXon, config->cXoff);
2316 } else
2317 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
2318
2319 /* if we are implementing OUTBOUND XON/XOFF */
2320 if (I_IXON(tty)) {
2321 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2322 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2323 __func__, config->cXon, config->cXoff);
2324 } else
2325 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
2326
2327 tty->termios.c_cflag &= ~CMSPAR;
2328
2329 /* Round the baud rate */
2330 baud = tty_get_baud_rate(tty);
2331 if (!baud) {
2332 /* pick a default, any default... */
2333 baud = 9600;
2334 } else {
2335 /* Avoid a zero divisor. */
2336 baud = min(baud, 461550);
2337 tty_encode_baud_rate(tty, baud, baud);
2338 }
2339
2340 edge_port->baud_rate = baud;
2341 config->wBaudRate = (u16)((461550L + baud/2) / baud);
2342
2343 /* FIXME: Recompute actual baud from divisor here */
2344
2345 dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
2346
2347 dev_dbg(dev, "wBaudRate: %d\n", (int)(461550L / config->wBaudRate));
2348 dev_dbg(dev, "wFlags: 0x%x\n", config->wFlags);
2349 dev_dbg(dev, "bDataBits: %d\n", config->bDataBits);
2350 dev_dbg(dev, "bParity: %d\n", config->bParity);
2351 dev_dbg(dev, "bStopBits: %d\n", config->bStopBits);
2352 dev_dbg(dev, "cXon: %d\n", config->cXon);
2353 dev_dbg(dev, "cXoff: %d\n", config->cXoff);
2354 dev_dbg(dev, "bUartMode: %d\n", config->bUartMode);
2355
2356 /* move the word values into big endian mode */
2357 cpu_to_be16s(&config->wFlags);
2358 cpu_to_be16s(&config->wBaudRate);
2359
2360 status = send_port_cmd(edge_port->port, UMPC_SET_CONFIG, 0, config,
2361 sizeof(*config));
2362 if (status)
2363 dev_dbg(dev, "%s - error %d when trying to write config to device\n",
2364 __func__, status);
2365 kfree(config);
2366 }
2367
edge_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)2368 static void edge_set_termios(struct tty_struct *tty,
2369 struct usb_serial_port *port,
2370 const struct ktermios *old_termios)
2371 {
2372 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2373
2374 if (edge_port == NULL)
2375 return;
2376 /* change the port settings to the new ones specified */
2377 change_port_settings(tty, edge_port, old_termios);
2378 }
2379
edge_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)2380 static int edge_tiocmset(struct tty_struct *tty,
2381 unsigned int set, unsigned int clear)
2382 {
2383 struct usb_serial_port *port = tty->driver_data;
2384 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2385 unsigned int mcr;
2386 unsigned long flags;
2387
2388 spin_lock_irqsave(&edge_port->ep_lock, flags);
2389 mcr = edge_port->shadow_mcr;
2390 if (set & TIOCM_RTS)
2391 mcr |= MCR_RTS;
2392 if (set & TIOCM_DTR)
2393 mcr |= MCR_DTR;
2394 if (set & TIOCM_LOOP)
2395 mcr |= MCR_LOOPBACK;
2396
2397 if (clear & TIOCM_RTS)
2398 mcr &= ~MCR_RTS;
2399 if (clear & TIOCM_DTR)
2400 mcr &= ~MCR_DTR;
2401 if (clear & TIOCM_LOOP)
2402 mcr &= ~MCR_LOOPBACK;
2403
2404 edge_port->shadow_mcr = mcr;
2405 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2406
2407 restore_mcr(edge_port, mcr);
2408 return 0;
2409 }
2410
edge_tiocmget(struct tty_struct * tty)2411 static int edge_tiocmget(struct tty_struct *tty)
2412 {
2413 struct usb_serial_port *port = tty->driver_data;
2414 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2415 unsigned int result = 0;
2416 unsigned int msr;
2417 unsigned int mcr;
2418 unsigned long flags;
2419
2420 spin_lock_irqsave(&edge_port->ep_lock, flags);
2421
2422 msr = edge_port->shadow_msr;
2423 mcr = edge_port->shadow_mcr;
2424 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2425 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2426 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2427 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2428 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2429 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2430
2431
2432 dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
2433 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2434
2435 return result;
2436 }
2437
edge_break(struct tty_struct * tty,int break_state)2438 static int edge_break(struct tty_struct *tty, int break_state)
2439 {
2440 struct usb_serial_port *port = tty->driver_data;
2441 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2442 int status;
2443 int bv = 0; /* Off */
2444
2445 if (break_state == -1)
2446 bv = 1; /* On */
2447
2448 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2449 if (status) {
2450 dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
2451 __func__, status);
2452 return status;
2453 }
2454
2455 return 0;
2456 }
2457
edge_heartbeat_schedule(struct edgeport_serial * edge_serial)2458 static void edge_heartbeat_schedule(struct edgeport_serial *edge_serial)
2459 {
2460 if (!edge_serial->use_heartbeat)
2461 return;
2462
2463 schedule_delayed_work(&edge_serial->heartbeat_work,
2464 FW_HEARTBEAT_SECS * HZ);
2465 }
2466
edge_heartbeat_work(struct work_struct * work)2467 static void edge_heartbeat_work(struct work_struct *work)
2468 {
2469 struct edgeport_serial *serial;
2470 struct ti_i2c_desc *rom_desc;
2471
2472 serial = container_of(work, struct edgeport_serial,
2473 heartbeat_work.work);
2474
2475 rom_desc = kmalloc_obj(*rom_desc);
2476
2477 /* Descriptor address request is enough to reset the firmware timer */
2478 if (!rom_desc || !get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
2479 rom_desc)) {
2480 dev_err(&serial->serial->interface->dev,
2481 "%s - Incomplete heartbeat\n", __func__);
2482 }
2483 kfree(rom_desc);
2484
2485 edge_heartbeat_schedule(serial);
2486 }
2487
edge_calc_num_ports(struct usb_serial * serial,struct usb_serial_endpoints * epds)2488 static int edge_calc_num_ports(struct usb_serial *serial,
2489 struct usb_serial_endpoints *epds)
2490 {
2491 struct device *dev = &serial->interface->dev;
2492 unsigned char num_ports = serial->type->num_ports;
2493
2494 /* Make sure we have the required endpoints when in download mode. */
2495 if (serial->interface->cur_altsetting->desc.bNumEndpoints > 1) {
2496 if (epds->num_bulk_in < num_ports ||
2497 epds->num_bulk_out < num_ports ||
2498 epds->num_interrupt_in < 1) {
2499 dev_err(dev, "required endpoints missing\n");
2500 return -ENODEV;
2501 }
2502 }
2503
2504 return num_ports;
2505 }
2506
edge_startup(struct usb_serial * serial)2507 static int edge_startup(struct usb_serial *serial)
2508 {
2509 struct edgeport_serial *edge_serial;
2510 int status;
2511 u16 product_id;
2512
2513 /* create our private serial structure */
2514 edge_serial = kzalloc_obj(struct edgeport_serial);
2515 if (!edge_serial)
2516 return -ENOMEM;
2517
2518 mutex_init(&edge_serial->es_lock);
2519 edge_serial->serial = serial;
2520 INIT_DELAYED_WORK(&edge_serial->heartbeat_work, edge_heartbeat_work);
2521 usb_set_serial_data(serial, edge_serial);
2522
2523 status = download_fw(edge_serial);
2524 if (status < 0) {
2525 kfree(edge_serial);
2526 return status;
2527 }
2528
2529 if (status > 0)
2530 return 1; /* bind but do not register any ports */
2531
2532 product_id = le16_to_cpu(
2533 edge_serial->serial->dev->descriptor.idProduct);
2534
2535 /* Currently only the EP/416 models require heartbeat support */
2536 if (edge_serial->fw_version > FW_HEARTBEAT_VERSION_CUTOFF) {
2537 if (product_id == ION_DEVICE_ID_TI_EDGEPORT_416 ||
2538 product_id == ION_DEVICE_ID_TI_EDGEPORT_416B) {
2539 edge_serial->use_heartbeat = true;
2540 }
2541 }
2542
2543 edge_heartbeat_schedule(edge_serial);
2544
2545 return 0;
2546 }
2547
edge_disconnect(struct usb_serial * serial)2548 static void edge_disconnect(struct usb_serial *serial)
2549 {
2550 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2551
2552 cancel_delayed_work_sync(&edge_serial->heartbeat_work);
2553 }
2554
edge_release(struct usb_serial * serial)2555 static void edge_release(struct usb_serial *serial)
2556 {
2557 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2558
2559 cancel_delayed_work_sync(&edge_serial->heartbeat_work);
2560 kfree(edge_serial);
2561 }
2562
edge_port_probe(struct usb_serial_port * port)2563 static int edge_port_probe(struct usb_serial_port *port)
2564 {
2565 struct edgeport_port *edge_port;
2566 int ret;
2567
2568 edge_port = kzalloc_obj(*edge_port);
2569 if (!edge_port)
2570 return -ENOMEM;
2571
2572 spin_lock_init(&edge_port->ep_lock);
2573 edge_port->port = port;
2574 edge_port->edge_serial = usb_get_serial_data(port->serial);
2575 edge_port->bUartMode = default_uart_mode;
2576
2577 switch (port->port_number) {
2578 case 0:
2579 edge_port->uart_base = UMPMEM_BASE_UART1;
2580 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
2581 break;
2582 case 1:
2583 edge_port->uart_base = UMPMEM_BASE_UART2;
2584 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
2585 break;
2586 default:
2587 dev_err(&port->dev, "unknown port number\n");
2588 ret = -ENODEV;
2589 goto err;
2590 }
2591
2592 dev_dbg(&port->dev,
2593 "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
2594 __func__, port->port_number, edge_port->uart_base,
2595 edge_port->dma_address);
2596
2597 usb_set_serial_port_data(port, edge_port);
2598
2599 ret = edge_create_sysfs_attrs(port);
2600 if (ret)
2601 goto err;
2602
2603 /*
2604 * The LSR does not tell when the transmitter shift register has
2605 * emptied so add a one-character drain delay.
2606 */
2607 port->port.drain_delay = 1;
2608
2609 return 0;
2610 err:
2611 kfree(edge_port);
2612
2613 return ret;
2614 }
2615
edge_port_remove(struct usb_serial_port * port)2616 static void edge_port_remove(struct usb_serial_port *port)
2617 {
2618 struct edgeport_port *edge_port;
2619
2620 edge_port = usb_get_serial_port_data(port);
2621 edge_remove_sysfs_attrs(port);
2622 kfree(edge_port);
2623 }
2624
2625 /* Sysfs Attributes */
2626
uart_mode_show(struct device * dev,struct device_attribute * attr,char * buf)2627 static ssize_t uart_mode_show(struct device *dev,
2628 struct device_attribute *attr, char *buf)
2629 {
2630 struct usb_serial_port *port = to_usb_serial_port(dev);
2631 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2632
2633 return sprintf(buf, "%d\n", edge_port->bUartMode);
2634 }
2635
uart_mode_store(struct device * dev,struct device_attribute * attr,const char * valbuf,size_t count)2636 static ssize_t uart_mode_store(struct device *dev,
2637 struct device_attribute *attr, const char *valbuf, size_t count)
2638 {
2639 struct usb_serial_port *port = to_usb_serial_port(dev);
2640 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2641 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2642
2643 dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
2644
2645 if (v < 256)
2646 edge_port->bUartMode = v;
2647 else
2648 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2649
2650 return count;
2651 }
2652 static DEVICE_ATTR_RW(uart_mode);
2653
edge_create_sysfs_attrs(struct usb_serial_port * port)2654 static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2655 {
2656 return device_create_file(&port->dev, &dev_attr_uart_mode);
2657 }
2658
edge_remove_sysfs_attrs(struct usb_serial_port * port)2659 static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2660 {
2661 device_remove_file(&port->dev, &dev_attr_uart_mode);
2662 return 0;
2663 }
2664
2665 #ifdef CONFIG_PM
edge_suspend(struct usb_serial * serial,pm_message_t message)2666 static int edge_suspend(struct usb_serial *serial, pm_message_t message)
2667 {
2668 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2669
2670 cancel_delayed_work_sync(&edge_serial->heartbeat_work);
2671
2672 return 0;
2673 }
2674
edge_resume(struct usb_serial * serial)2675 static int edge_resume(struct usb_serial *serial)
2676 {
2677 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2678
2679 edge_heartbeat_schedule(edge_serial);
2680
2681 return 0;
2682 }
2683 #endif
2684
2685 static struct usb_serial_driver edgeport_1port_device = {
2686 .driver = {
2687 .name = "edgeport_ti_1",
2688 },
2689 .description = "Edgeport TI 1 port adapter",
2690 .id_table = edgeport_1port_id_table,
2691 .num_ports = 1,
2692 .num_bulk_out = 1,
2693 .open = edge_open,
2694 .close = edge_close,
2695 .throttle = edge_throttle,
2696 .unthrottle = edge_unthrottle,
2697 .attach = edge_startup,
2698 .calc_num_ports = edge_calc_num_ports,
2699 .disconnect = edge_disconnect,
2700 .release = edge_release,
2701 .port_probe = edge_port_probe,
2702 .port_remove = edge_port_remove,
2703 .set_termios = edge_set_termios,
2704 .tiocmget = edge_tiocmget,
2705 .tiocmset = edge_tiocmset,
2706 .tiocmiwait = usb_serial_generic_tiocmiwait,
2707 .get_icount = usb_serial_generic_get_icount,
2708 .write = edge_write,
2709 .write_room = edge_write_room,
2710 .chars_in_buffer = edge_chars_in_buffer,
2711 .tx_empty = edge_tx_empty,
2712 .break_ctl = edge_break,
2713 .read_int_callback = edge_interrupt_callback,
2714 .read_bulk_callback = edge_bulk_in_callback,
2715 .write_bulk_callback = edge_bulk_out_callback,
2716 #ifdef CONFIG_PM
2717 .suspend = edge_suspend,
2718 .resume = edge_resume,
2719 #endif
2720 };
2721
2722 static struct usb_serial_driver edgeport_2port_device = {
2723 .driver = {
2724 .name = "edgeport_ti_2",
2725 },
2726 .description = "Edgeport TI 2 port adapter",
2727 .id_table = edgeport_2port_id_table,
2728 .num_ports = 2,
2729 .num_bulk_out = 1,
2730 .open = edge_open,
2731 .close = edge_close,
2732 .throttle = edge_throttle,
2733 .unthrottle = edge_unthrottle,
2734 .attach = edge_startup,
2735 .calc_num_ports = edge_calc_num_ports,
2736 .disconnect = edge_disconnect,
2737 .release = edge_release,
2738 .port_probe = edge_port_probe,
2739 .port_remove = edge_port_remove,
2740 .set_termios = edge_set_termios,
2741 .tiocmget = edge_tiocmget,
2742 .tiocmset = edge_tiocmset,
2743 .tiocmiwait = usb_serial_generic_tiocmiwait,
2744 .get_icount = usb_serial_generic_get_icount,
2745 .write = edge_write,
2746 .write_room = edge_write_room,
2747 .chars_in_buffer = edge_chars_in_buffer,
2748 .tx_empty = edge_tx_empty,
2749 .break_ctl = edge_break,
2750 .read_int_callback = edge_interrupt_callback,
2751 .read_bulk_callback = edge_bulk_in_callback,
2752 .write_bulk_callback = edge_bulk_out_callback,
2753 #ifdef CONFIG_PM
2754 .suspend = edge_suspend,
2755 .resume = edge_resume,
2756 #endif
2757 };
2758
2759 static struct usb_serial_driver * const serial_drivers[] = {
2760 &edgeport_1port_device, &edgeport_2port_device, NULL
2761 };
2762
2763 module_usb_serial_driver(serial_drivers, id_table_combined);
2764
2765 MODULE_AUTHOR(DRIVER_AUTHOR);
2766 MODULE_DESCRIPTION(DRIVER_DESC);
2767 MODULE_LICENSE("GPL");
2768 MODULE_FIRMWARE("edgeport/down3.bin");
2769
2770 module_param(ignore_cpu_rev, bool, 0644);
2771 MODULE_PARM_DESC(ignore_cpu_rev,
2772 "Ignore the cpu revision when connecting to a device");
2773
2774 module_param(default_uart_mode, int, 0644);
2775 MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");
2776