]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * USB Cypress M8 driver | |
3 | * | |
4 | * Copyright (C) 2004 | |
813a224f | 5 | * Lonnie Mendez (dignome@gmail.com) |
1da177e4 LT |
6 | * Copyright (C) 2003,2004 |
7 | * Neil Whelchel (koyama@firstlight.net) | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
813a224f AC |
14 | * See Documentation/usb/usb-serial.txt for more information on using this |
15 | * driver | |
1da177e4 LT |
16 | * |
17 | * See http://geocities.com/i0xox0i for information on this driver and the | |
18 | * earthmate usb device. | |
19 | * | |
3cb4a4f7 LM |
20 | * Lonnie Mendez <dignome@gmail.com> |
21 | * 4-29-2005 | |
813a224f AC |
22 | * Fixed problem where setting or retreiving the serial config would fail |
23 | * with EPIPE. Removed CRTS toggling so the driver behaves more like | |
24 | * other usbserial adapters. Issued new interval of 1ms instead of the | |
25 | * default 10ms. As a result, transfer speed has been substantially | |
26 | * increased from avg. 850bps to avg. 3300bps. initial termios has also | |
27 | * been modified. Cleaned up code and formatting issues so it is more | |
28 | * readable. Replaced the C++ style comments. | |
1da177e4 LT |
29 | * |
30 | * Lonnie Mendez <dignome@gmail.com> | |
31 | * 12-15-2004 | |
32 | * Incorporated write buffering from pl2303 driver. Fixed bug with line | |
33 | * handling so both lines are raised in cypress_open. (was dropping rts) | |
34 | * Various code cleanups made as well along with other misc bug fixes. | |
35 | * | |
36 | * Lonnie Mendez <dignome@gmail.com> | |
37 | * 04-10-2004 | |
38 | * Driver modified to support dynamic line settings. Various improvments | |
39 | * and features. | |
40 | * | |
41 | * Neil Whelchel | |
42 | * 10-2003 | |
43 | * Driver first released. | |
44 | * | |
1da177e4 LT |
45 | */ |
46 | ||
813a224f AC |
47 | /* Thanks to Neil Whelchel for writing the first cypress m8 implementation |
48 | for linux. */ | |
1da177e4 LT |
49 | /* Thanks to cypress for providing references for the hid reports. */ |
50 | /* Thanks to Jiang Zhang for providing links and for general help. */ | |
813a224f | 51 | /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/ |
1da177e4 LT |
52 | |
53 | ||
1da177e4 LT |
54 | #include <linux/kernel.h> |
55 | #include <linux/errno.h> | |
56 | #include <linux/init.h> | |
57 | #include <linux/slab.h> | |
58 | #include <linux/tty.h> | |
59 | #include <linux/tty_driver.h> | |
60 | #include <linux/tty_flip.h> | |
61 | #include <linux/module.h> | |
62 | #include <linux/moduleparam.h> | |
63 | #include <linux/spinlock.h> | |
64 | #include <linux/usb.h> | |
a969888c | 65 | #include <linux/usb/serial.h> |
1da177e4 LT |
66 | #include <linux/serial.h> |
67 | #include <linux/delay.h> | |
813a224f | 68 | #include <linux/uaccess.h> |
0f2c2d7b | 69 | #include <asm/unaligned.h> |
1da177e4 | 70 | |
1da177e4 LT |
71 | #include "cypress_m8.h" |
72 | ||
73 | ||
64319dd7 | 74 | static int debug; |
1da177e4 | 75 | static int stats; |
3cb4a4f7 | 76 | static int interval; |
c312659c | 77 | static int unstable_bauds; |
1da177e4 LT |
78 | |
79 | /* | |
80 | * Version Information | |
81 | */ | |
3cb4a4f7 | 82 | #define DRIVER_VERSION "v1.09" |
1da177e4 LT |
83 | #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>" |
84 | #define DRIVER_DESC "Cypress USB to Serial Driver" | |
85 | ||
86 | /* write buffer size defines */ | |
87 | #define CYPRESS_BUF_SIZE 1024 | |
88 | #define CYPRESS_CLOSING_WAIT (30*HZ) | |
89 | ||
7d40d7e8 | 90 | static const struct usb_device_id id_table_earthmate[] = { |
1da177e4 | 91 | { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) }, |
25b6f08e | 92 | { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) }, |
1da177e4 LT |
93 | { } /* Terminating entry */ |
94 | }; | |
95 | ||
7d40d7e8 | 96 | static const struct usb_device_id id_table_cyphidcomrs232[] = { |
1da177e4 | 97 | { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) }, |
6f6f06ee | 98 | { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) }, |
1da177e4 LT |
99 | { } /* Terminating entry */ |
100 | }; | |
101 | ||
7d40d7e8 | 102 | static const struct usb_device_id id_table_nokiaca42v2[] = { |
a5c44e29 LM |
103 | { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) }, |
104 | { } /* Terminating entry */ | |
105 | }; | |
106 | ||
7d40d7e8 | 107 | static const struct usb_device_id id_table_combined[] = { |
1da177e4 | 108 | { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) }, |
25b6f08e | 109 | { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) }, |
1da177e4 | 110 | { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) }, |
6f6f06ee | 111 | { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) }, |
a5c44e29 | 112 | { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) }, |
1da177e4 LT |
113 | { } /* Terminating entry */ |
114 | }; | |
115 | ||
813a224f | 116 | MODULE_DEVICE_TABLE(usb, id_table_combined); |
1da177e4 LT |
117 | |
118 | static struct usb_driver cypress_driver = { | |
119 | .name = "cypress", | |
120 | .probe = usb_serial_probe, | |
121 | .disconnect = usb_serial_disconnect, | |
122 | .id_table = id_table_combined, | |
ba9dc657 | 123 | .no_dynamic_id = 1, |
1da177e4 LT |
124 | }; |
125 | ||
3416eaa1 MI |
126 | enum packet_format { |
127 | packet_format_1, /* b0:status, b1:payload count */ | |
128 | packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */ | |
129 | }; | |
130 | ||
1da177e4 LT |
131 | struct cypress_private { |
132 | spinlock_t lock; /* private lock */ | |
133 | int chiptype; /* identifier of device, for quirks/etc */ | |
134 | int bytes_in; /* used for statistics */ | |
135 | int bytes_out; /* used for statistics */ | |
136 | int cmd_count; /* used for statistics */ | |
137 | int cmd_ctrl; /* always set this to 1 before issuing a command */ | |
138 | struct cypress_buf *buf; /* write buffer */ | |
139 | int write_urb_in_use; /* write urb in use indicator */ | |
0257fa9f MI |
140 | int write_urb_interval; /* interval to use for write urb */ |
141 | int read_urb_interval; /* interval to use for read urb */ | |
78aef519 | 142 | int comm_is_ok; /* true if communication is (still) ok */ |
1da177e4 LT |
143 | int termios_initialized; |
144 | __u8 line_control; /* holds dtr / rts value */ | |
145 | __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */ | |
146 | __u8 current_config; /* stores the current configuration byte */ | |
147 | __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */ | |
3416eaa1 | 148 | enum packet_format pkt_fmt; /* format to use for packet send / receive */ |
3d6aa320 | 149 | int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */ |
813a224f AC |
150 | int baud_rate; /* stores current baud rate in |
151 | integer form */ | |
1da177e4 LT |
152 | int isthrottled; /* if throttled, discard reads */ |
153 | wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */ | |
154 | char prev_status, diff_status; /* used for TIOCMIWAIT */ | |
813a224f AC |
155 | /* we pass a pointer to this as the arguement sent to |
156 | cypress_set_termios old_termios */ | |
606d099c | 157 | struct ktermios tmp_termios; /* stores the old termios settings */ |
1da177e4 LT |
158 | }; |
159 | ||
160 | /* write buffer structure */ | |
161 | struct cypress_buf { | |
162 | unsigned int buf_size; | |
163 | char *buf_buf; | |
164 | char *buf_get; | |
165 | char *buf_put; | |
166 | }; | |
167 | ||
168 | /* function prototypes for the Cypress USB to serial device */ | |
813a224f AC |
169 | static int cypress_earthmate_startup(struct usb_serial *serial); |
170 | static int cypress_hidcom_startup(struct usb_serial *serial); | |
171 | static int cypress_ca42v2_startup(struct usb_serial *serial); | |
f9c99bb8 | 172 | static void cypress_release(struct usb_serial *serial); |
a509a7e4 | 173 | static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port); |
335f8514 AC |
174 | static void cypress_close(struct usb_serial_port *port); |
175 | static void cypress_dtr_rts(struct usb_serial_port *port, int on); | |
813a224f AC |
176 | static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port, |
177 | const unsigned char *buf, int count); | |
178 | static void cypress_send(struct usb_serial_port *port); | |
179 | static int cypress_write_room(struct tty_struct *tty); | |
180 | static int cypress_ioctl(struct tty_struct *tty, struct file *file, | |
181 | unsigned int cmd, unsigned long arg); | |
182 | static void cypress_set_termios(struct tty_struct *tty, | |
183 | struct usb_serial_port *port, struct ktermios *old); | |
184 | static int cypress_tiocmget(struct tty_struct *tty, struct file *file); | |
185 | static int cypress_tiocmset(struct tty_struct *tty, struct file *file, | |
186 | unsigned int set, unsigned int clear); | |
187 | static int cypress_chars_in_buffer(struct tty_struct *tty); | |
188 | static void cypress_throttle(struct tty_struct *tty); | |
189 | static void cypress_unthrottle(struct tty_struct *tty); | |
190 | static void cypress_set_dead(struct usb_serial_port *port); | |
191 | static void cypress_read_int_callback(struct urb *urb); | |
192 | static void cypress_write_int_callback(struct urb *urb); | |
1da177e4 LT |
193 | /* write buffer functions */ |
194 | static struct cypress_buf *cypress_buf_alloc(unsigned int size); | |
813a224f AC |
195 | static void cypress_buf_free(struct cypress_buf *cb); |
196 | static void cypress_buf_clear(struct cypress_buf *cb); | |
197 | static unsigned int cypress_buf_data_avail(struct cypress_buf *cb); | |
198 | static unsigned int cypress_buf_space_avail(struct cypress_buf *cb); | |
199 | static unsigned int cypress_buf_put(struct cypress_buf *cb, | |
200 | const char *buf, unsigned int count); | |
201 | static unsigned int cypress_buf_get(struct cypress_buf *cb, | |
202 | char *buf, unsigned int count); | |
1da177e4 LT |
203 | |
204 | ||
ea65370d | 205 | static struct usb_serial_driver cypress_earthmate_device = { |
18fcac35 GKH |
206 | .driver = { |
207 | .owner = THIS_MODULE, | |
269bda1c | 208 | .name = "earthmate", |
18fcac35 | 209 | }, |
269bda1c | 210 | .description = "DeLorme Earthmate USB", |
d9b1b787 | 211 | .usb_driver = &cypress_driver, |
1da177e4 | 212 | .id_table = id_table_earthmate, |
1da177e4 LT |
213 | .num_ports = 1, |
214 | .attach = cypress_earthmate_startup, | |
f9c99bb8 | 215 | .release = cypress_release, |
1da177e4 LT |
216 | .open = cypress_open, |
217 | .close = cypress_close, | |
335f8514 | 218 | .dtr_rts = cypress_dtr_rts, |
1da177e4 LT |
219 | .write = cypress_write, |
220 | .write_room = cypress_write_room, | |
221 | .ioctl = cypress_ioctl, | |
222 | .set_termios = cypress_set_termios, | |
223 | .tiocmget = cypress_tiocmget, | |
224 | .tiocmset = cypress_tiocmset, | |
225 | .chars_in_buffer = cypress_chars_in_buffer, | |
226 | .throttle = cypress_throttle, | |
227 | .unthrottle = cypress_unthrottle, | |
228 | .read_int_callback = cypress_read_int_callback, | |
229 | .write_int_callback = cypress_write_int_callback, | |
230 | }; | |
231 | ||
ea65370d | 232 | static struct usb_serial_driver cypress_hidcom_device = { |
18fcac35 GKH |
233 | .driver = { |
234 | .owner = THIS_MODULE, | |
269bda1c | 235 | .name = "cyphidcom", |
18fcac35 | 236 | }, |
269bda1c | 237 | .description = "HID->COM RS232 Adapter", |
d9b1b787 | 238 | .usb_driver = &cypress_driver, |
1da177e4 | 239 | .id_table = id_table_cyphidcomrs232, |
1da177e4 LT |
240 | .num_ports = 1, |
241 | .attach = cypress_hidcom_startup, | |
f9c99bb8 | 242 | .release = cypress_release, |
1da177e4 LT |
243 | .open = cypress_open, |
244 | .close = cypress_close, | |
335f8514 | 245 | .dtr_rts = cypress_dtr_rts, |
1da177e4 LT |
246 | .write = cypress_write, |
247 | .write_room = cypress_write_room, | |
248 | .ioctl = cypress_ioctl, | |
249 | .set_termios = cypress_set_termios, | |
250 | .tiocmget = cypress_tiocmget, | |
251 | .tiocmset = cypress_tiocmset, | |
252 | .chars_in_buffer = cypress_chars_in_buffer, | |
253 | .throttle = cypress_throttle, | |
254 | .unthrottle = cypress_unthrottle, | |
255 | .read_int_callback = cypress_read_int_callback, | |
256 | .write_int_callback = cypress_write_int_callback, | |
257 | }; | |
258 | ||
a5c44e29 LM |
259 | static struct usb_serial_driver cypress_ca42v2_device = { |
260 | .driver = { | |
261 | .owner = THIS_MODULE, | |
813a224f | 262 | .name = "nokiaca42v2", |
a5c44e29 LM |
263 | }, |
264 | .description = "Nokia CA-42 V2 Adapter", | |
d9b1b787 | 265 | .usb_driver = &cypress_driver, |
a5c44e29 | 266 | .id_table = id_table_nokiaca42v2, |
a5c44e29 LM |
267 | .num_ports = 1, |
268 | .attach = cypress_ca42v2_startup, | |
f9c99bb8 | 269 | .release = cypress_release, |
a5c44e29 LM |
270 | .open = cypress_open, |
271 | .close = cypress_close, | |
335f8514 | 272 | .dtr_rts = cypress_dtr_rts, |
a5c44e29 LM |
273 | .write = cypress_write, |
274 | .write_room = cypress_write_room, | |
275 | .ioctl = cypress_ioctl, | |
276 | .set_termios = cypress_set_termios, | |
277 | .tiocmget = cypress_tiocmget, | |
278 | .tiocmset = cypress_tiocmset, | |
279 | .chars_in_buffer = cypress_chars_in_buffer, | |
280 | .throttle = cypress_throttle, | |
281 | .unthrottle = cypress_unthrottle, | |
282 | .read_int_callback = cypress_read_int_callback, | |
283 | .write_int_callback = cypress_write_int_callback, | |
284 | }; | |
1da177e4 LT |
285 | |
286 | /***************************************************************************** | |
287 | * Cypress serial helper functions | |
288 | *****************************************************************************/ | |
289 | ||
290 | ||
8873aaa6 | 291 | static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate) |
92983c21 | 292 | { |
92983c21 MI |
293 | struct cypress_private *priv; |
294 | priv = usb_get_serial_port_data(port); | |
295 | ||
c312659c MF |
296 | if (unstable_bauds) |
297 | return new_rate; | |
298 | ||
92983c21 MI |
299 | /* |
300 | * The general purpose firmware for the Cypress M8 allows for | |
301 | * a maximum speed of 57600bps (I have no idea whether DeLorme | |
302 | * chose to use the general purpose firmware or not), if you | |
303 | * need to modify this speed setting for your own project | |
304 | * please add your own chiptype and modify the code likewise. | |
305 | * The Cypress HID->COM device will work successfully up to | |
306 | * 115200bps (but the actual throughput is around 3kBps). | |
307 | */ | |
92983c21 MI |
308 | if (port->serial->dev->speed == USB_SPEED_LOW) { |
309 | /* | |
310 | * Mike Isely <isely@pobox.com> 2-Feb-2008: The | |
311 | * Cypress app note that describes this mechanism | |
312 | * states the the low-speed part can't handle more | |
313 | * than 800 bytes/sec, in which case 4800 baud is the | |
314 | * safest speed for a part like that. | |
315 | */ | |
316 | if (new_rate > 4800) { | |
317 | dbg("%s - failed setting baud rate, device incapable " | |
318 | "speed %d", __func__, new_rate); | |
319 | return -1; | |
320 | } | |
321 | } | |
322 | switch (priv->chiptype) { | |
323 | case CT_EARTHMATE: | |
324 | if (new_rate <= 600) { | |
325 | /* 300 and 600 baud rates are supported under | |
326 | * the generic firmware, but are not used with | |
327 | * NMEA and SiRF protocols */ | |
328 | dbg("%s - failed setting baud rate, unsupported speed " | |
329 | "of %d on Earthmate GPS", __func__, new_rate); | |
330 | return -1; | |
331 | } | |
332 | break; | |
333 | default: | |
334 | break; | |
335 | } | |
336 | return new_rate; | |
337 | } | |
338 | ||
339 | ||
093cf723 | 340 | /* This function can either set or retrieve the current serial line settings */ |
813a224f | 341 | static int cypress_serial_control(struct tty_struct *tty, |
95da310e AC |
342 | struct usb_serial_port *port, speed_t baud_rate, int data_bits, |
343 | int stop_bits, int parity_enable, int parity_type, int reset, | |
344 | int cypress_request_type) | |
1da177e4 | 345 | { |
3cb4a4f7 | 346 | int new_baudrate = 0, retval = 0, tries = 0; |
1da177e4 | 347 | struct cypress_private *priv; |
0954644b JH |
348 | u8 *feature_buffer; |
349 | const unsigned int feature_len = 5; | |
1da177e4 LT |
350 | unsigned long flags; |
351 | ||
441b62c1 | 352 | dbg("%s", __func__); |
813a224f | 353 | |
1da177e4 LT |
354 | priv = usb_get_serial_port_data(port); |
355 | ||
78aef519 MI |
356 | if (!priv->comm_is_ok) |
357 | return -ENODEV; | |
358 | ||
0954644b JH |
359 | feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL); |
360 | if (!feature_buffer) | |
361 | return -ENOMEM; | |
362 | ||
813a224f AC |
363 | switch (cypress_request_type) { |
364 | case CYPRESS_SET_CONFIG: | |
813a224f | 365 | /* 0 means 'Hang up' so doesn't change the true bit rate */ |
2805eb13 MF |
366 | new_baudrate = priv->baud_rate; |
367 | if (baud_rate && baud_rate != priv->baud_rate) { | |
813a224f AC |
368 | dbg("%s - baud rate is changing", __func__); |
369 | retval = analyze_baud_rate(port, baud_rate); | |
2805eb13 | 370 | if (retval >= 0) { |
813a224f AC |
371 | new_baudrate = retval; |
372 | dbg("%s - New baud rate set to %d", | |
373 | __func__, new_baudrate); | |
1da177e4 | 374 | } |
813a224f AC |
375 | } |
376 | dbg("%s - baud rate is being sent as %d", | |
377 | __func__, new_baudrate); | |
378 | ||
813a224f | 379 | /* fill the feature_buffer with new configuration */ |
0f2c2d7b | 380 | put_unaligned_le32(new_baudrate, feature_buffer); |
813a224f AC |
381 | feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */ |
382 | /* 1 bit gap */ | |
383 | feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */ | |
384 | feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */ | |
385 | feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */ | |
386 | /* 1 bit gap */ | |
387 | feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */ | |
388 | ||
389 | dbg("%s - device is being sent this feature report:", | |
390 | __func__); | |
391 | dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__, | |
392 | feature_buffer[0], feature_buffer[1], | |
393 | feature_buffer[2], feature_buffer[3], | |
394 | feature_buffer[4]); | |
395 | ||
396 | do { | |
397 | retval = usb_control_msg(port->serial->dev, | |
398 | usb_sndctrlpipe(port->serial->dev, 0), | |
399 | HID_REQ_SET_REPORT, | |
400 | USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS, | |
401 | 0x0300, 0, feature_buffer, | |
0954644b | 402 | feature_len, 500); |
813a224f AC |
403 | |
404 | if (tries++ >= 3) | |
405 | break; | |
406 | ||
0954644b | 407 | } while (retval != feature_len && |
813a224f AC |
408 | retval != -ENODEV); |
409 | ||
0954644b | 410 | if (retval != feature_len) { |
194343d9 GKH |
411 | dev_err(&port->dev, "%s - failed sending serial " |
412 | "line settings - %d\n", __func__, retval); | |
813a224f AC |
413 | cypress_set_dead(port); |
414 | } else { | |
415 | spin_lock_irqsave(&priv->lock, flags); | |
416 | priv->baud_rate = new_baudrate; | |
417 | priv->current_config = feature_buffer[4]; | |
418 | spin_unlock_irqrestore(&priv->lock, flags); | |
419 | /* If we asked for a speed change encode it */ | |
420 | if (baud_rate) | |
421 | tty_encode_baud_rate(tty, | |
422 | new_baudrate, new_baudrate); | |
423 | } | |
424 | break; | |
425 | case CYPRESS_GET_CONFIG: | |
426 | if (priv->get_cfg_unsafe) { | |
427 | /* Not implemented for this device, | |
428 | and if we try to do it we're likely | |
429 | to crash the hardware. */ | |
0954644b JH |
430 | retval = -ENOTTY; |
431 | goto out; | |
813a224f AC |
432 | } |
433 | dbg("%s - retreiving serial line settings", __func__); | |
813a224f AC |
434 | do { |
435 | retval = usb_control_msg(port->serial->dev, | |
436 | usb_rcvctrlpipe(port->serial->dev, 0), | |
437 | HID_REQ_GET_REPORT, | |
438 | USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS, | |
439 | 0x0300, 0, feature_buffer, | |
0954644b | 440 | feature_len, 500); |
813a224f AC |
441 | |
442 | if (tries++ >= 3) | |
443 | break; | |
0954644b | 444 | } while (retval != feature_len |
813a224f AC |
445 | && retval != -ENODEV); |
446 | ||
0954644b | 447 | if (retval != feature_len) { |
194343d9 GKH |
448 | dev_err(&port->dev, "%s - failed to retrieve serial " |
449 | "line settings - %d\n", __func__, retval); | |
813a224f | 450 | cypress_set_dead(port); |
0954644b | 451 | goto out; |
813a224f AC |
452 | } else { |
453 | spin_lock_irqsave(&priv->lock, flags); | |
454 | /* store the config in one byte, and later | |
455 | use bit masks to check values */ | |
456 | priv->current_config = feature_buffer[4]; | |
0f2c2d7b | 457 | priv->baud_rate = get_unaligned_le32(feature_buffer); |
813a224f AC |
458 | spin_unlock_irqrestore(&priv->lock, flags); |
459 | } | |
1da177e4 | 460 | } |
3cb4a4f7 LM |
461 | spin_lock_irqsave(&priv->lock, flags); |
462 | ++priv->cmd_count; | |
463 | spin_unlock_irqrestore(&priv->lock, flags); | |
0954644b JH |
464 | out: |
465 | kfree(feature_buffer); | |
1da177e4 LT |
466 | return retval; |
467 | } /* cypress_serial_control */ | |
468 | ||
469 | ||
78aef519 MI |
470 | static void cypress_set_dead(struct usb_serial_port *port) |
471 | { | |
472 | struct cypress_private *priv = usb_get_serial_port_data(port); | |
473 | unsigned long flags; | |
474 | ||
475 | spin_lock_irqsave(&priv->lock, flags); | |
476 | if (!priv->comm_is_ok) { | |
477 | spin_unlock_irqrestore(&priv->lock, flags); | |
478 | return; | |
479 | } | |
480 | priv->comm_is_ok = 0; | |
481 | spin_unlock_irqrestore(&priv->lock, flags); | |
482 | ||
194343d9 GKH |
483 | dev_err(&port->dev, "cypress_m8 suspending failing port %d - " |
484 | "interval might be too short\n", port->number); | |
78aef519 MI |
485 | } |
486 | ||
487 | ||
1da177e4 LT |
488 | /***************************************************************************** |
489 | * Cypress serial driver functions | |
490 | *****************************************************************************/ | |
491 | ||
492 | ||
813a224f | 493 | static int generic_startup(struct usb_serial *serial) |
1da177e4 LT |
494 | { |
495 | struct cypress_private *priv; | |
0257fa9f | 496 | struct usb_serial_port *port = serial->port[0]; |
1da177e4 | 497 | |
441b62c1 | 498 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 499 | |
813a224f | 500 | priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL); |
1da177e4 LT |
501 | if (!priv) |
502 | return -ENOMEM; | |
503 | ||
78aef519 | 504 | priv->comm_is_ok = !0; |
1da177e4 LT |
505 | spin_lock_init(&priv->lock); |
506 | priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE); | |
507 | if (priv->buf == NULL) { | |
508 | kfree(priv); | |
509 | return -ENOMEM; | |
510 | } | |
511 | init_waitqueue_head(&priv->delta_msr_wait); | |
813a224f AC |
512 | |
513 | usb_reset_configuration(serial->dev); | |
514 | ||
1da177e4 LT |
515 | priv->cmd_ctrl = 0; |
516 | priv->line_control = 0; | |
517 | priv->termios_initialized = 0; | |
1da177e4 | 518 | priv->rx_flags = 0; |
3416eaa1 MI |
519 | /* Default packet format setting is determined by packet size. |
520 | Anything with a size larger then 9 must have a separate | |
521 | count field since the 3 bit count field is otherwise too | |
522 | small. Otherwise we can use the slightly more compact | |
523 | format. This is in accordance with the cypress_m8 serial | |
524 | converter app note. */ | |
813a224f | 525 | if (port->interrupt_out_size > 9) |
3416eaa1 | 526 | priv->pkt_fmt = packet_format_1; |
813a224f | 527 | else |
3416eaa1 | 528 | priv->pkt_fmt = packet_format_2; |
813a224f | 529 | |
0257fa9f MI |
530 | if (interval > 0) { |
531 | priv->write_urb_interval = interval; | |
532 | priv->read_urb_interval = interval; | |
533 | dbg("%s - port %d read & write intervals forced to %d", | |
813a224f | 534 | __func__, port->number, interval); |
0257fa9f MI |
535 | } else { |
536 | priv->write_urb_interval = port->interrupt_out_urb->interval; | |
537 | priv->read_urb_interval = port->interrupt_in_urb->interval; | |
538 | dbg("%s - port %d intervals: read=%d write=%d", | |
813a224f AC |
539 | __func__, port->number, |
540 | priv->read_urb_interval, priv->write_urb_interval); | |
0257fa9f MI |
541 | } |
542 | usb_set_serial_port_data(port, priv); | |
813a224f | 543 | |
b9db07fb LM |
544 | return 0; |
545 | } | |
1da177e4 LT |
546 | |
547 | ||
813a224f | 548 | static int cypress_earthmate_startup(struct usb_serial *serial) |
1da177e4 LT |
549 | { |
550 | struct cypress_private *priv; | |
3d6aa320 | 551 | struct usb_serial_port *port = serial->port[0]; |
1da177e4 | 552 | |
441b62c1 | 553 | dbg("%s", __func__); |
1da177e4 LT |
554 | |
555 | if (generic_startup(serial)) { | |
441b62c1 | 556 | dbg("%s - Failed setting up port %d", __func__, |
3d6aa320 | 557 | port->number); |
1da177e4 LT |
558 | return 1; |
559 | } | |
560 | ||
3d6aa320 | 561 | priv = usb_get_serial_port_data(port); |
1da177e4 | 562 | priv->chiptype = CT_EARTHMATE; |
3416eaa1 MI |
563 | /* All Earthmate devices use the separated-count packet |
564 | format! Idiotic. */ | |
565 | priv->pkt_fmt = packet_format_1; | |
813a224f AC |
566 | if (serial->dev->descriptor.idProduct != |
567 | cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) { | |
3d6aa320 MI |
568 | /* The old original USB Earthmate seemed able to |
569 | handle GET_CONFIG requests; everything they've | |
570 | produced since that time crashes if this command is | |
571 | attempted :-( */ | |
572 | dbg("%s - Marking this device as unsafe for GET_CONFIG " | |
573 | "commands", __func__); | |
574 | priv->get_cfg_unsafe = !0; | |
575 | } | |
b9db07fb LM |
576 | |
577 | return 0; | |
1da177e4 LT |
578 | } /* cypress_earthmate_startup */ |
579 | ||
580 | ||
813a224f | 581 | static int cypress_hidcom_startup(struct usb_serial *serial) |
1da177e4 LT |
582 | { |
583 | struct cypress_private *priv; | |
584 | ||
441b62c1 | 585 | dbg("%s", __func__); |
1da177e4 LT |
586 | |
587 | if (generic_startup(serial)) { | |
441b62c1 | 588 | dbg("%s - Failed setting up port %d", __func__, |
b9db07fb | 589 | serial->port[0]->number); |
1da177e4 LT |
590 | return 1; |
591 | } | |
592 | ||
593 | priv = usb_get_serial_port_data(serial->port[0]); | |
594 | priv->chiptype = CT_CYPHIDCOM; | |
813a224f | 595 | |
b9db07fb | 596 | return 0; |
1da177e4 LT |
597 | } /* cypress_hidcom_startup */ |
598 | ||
599 | ||
813a224f | 600 | static int cypress_ca42v2_startup(struct usb_serial *serial) |
a5c44e29 LM |
601 | { |
602 | struct cypress_private *priv; | |
603 | ||
441b62c1 | 604 | dbg("%s", __func__); |
a5c44e29 LM |
605 | |
606 | if (generic_startup(serial)) { | |
441b62c1 | 607 | dbg("%s - Failed setting up port %d", __func__, |
a5c44e29 LM |
608 | serial->port[0]->number); |
609 | return 1; | |
610 | } | |
611 | ||
612 | priv = usb_get_serial_port_data(serial->port[0]); | |
613 | priv->chiptype = CT_CA42V2; | |
614 | ||
615 | return 0; | |
616 | } /* cypress_ca42v2_startup */ | |
617 | ||
618 | ||
f9c99bb8 | 619 | static void cypress_release(struct usb_serial *serial) |
1da177e4 LT |
620 | { |
621 | struct cypress_private *priv; | |
622 | ||
813a224f | 623 | dbg("%s - port %d", __func__, serial->port[0]->number); |
1da177e4 LT |
624 | |
625 | /* all open ports are closed at this point */ | |
626 | ||
627 | priv = usb_get_serial_port_data(serial->port[0]); | |
628 | ||
629 | if (priv) { | |
630 | cypress_buf_free(priv->buf); | |
631 | kfree(priv); | |
1da177e4 LT |
632 | } |
633 | } | |
634 | ||
635 | ||
a509a7e4 | 636 | static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port) |
1da177e4 LT |
637 | { |
638 | struct cypress_private *priv = usb_get_serial_port_data(port); | |
639 | struct usb_serial *serial = port->serial; | |
640 | unsigned long flags; | |
641 | int result = 0; | |
642 | ||
441b62c1 | 643 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 644 | |
78aef519 MI |
645 | if (!priv->comm_is_ok) |
646 | return -EIO; | |
647 | ||
1da177e4 | 648 | /* clear halts before open */ |
1da177e4 LT |
649 | usb_clear_halt(serial->dev, 0x81); |
650 | usb_clear_halt(serial->dev, 0x02); | |
651 | ||
652 | spin_lock_irqsave(&priv->lock, flags); | |
653 | /* reset read/write statistics */ | |
654 | priv->bytes_in = 0; | |
655 | priv->bytes_out = 0; | |
656 | priv->cmd_count = 0; | |
657 | priv->rx_flags = 0; | |
658 | spin_unlock_irqrestore(&priv->lock, flags); | |
659 | ||
335f8514 | 660 | /* Set termios */ |
fe1ae7fd | 661 | cypress_send(port); |
1da177e4 | 662 | |
95da310e AC |
663 | if (tty) |
664 | cypress_set_termios(tty, port, &priv->tmp_termios); | |
1da177e4 LT |
665 | |
666 | /* setup the port and start reading from the device */ | |
813a224f | 667 | if (!port->interrupt_in_urb) { |
194343d9 GKH |
668 | dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n", |
669 | __func__); | |
813a224f | 670 | return -1; |
1da177e4 LT |
671 | } |
672 | ||
673 | usb_fill_int_urb(port->interrupt_in_urb, serial->dev, | |
674 | usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress), | |
813a224f AC |
675 | port->interrupt_in_urb->transfer_buffer, |
676 | port->interrupt_in_urb->transfer_buffer_length, | |
0257fa9f | 677 | cypress_read_int_callback, port, priv->read_urb_interval); |
1da177e4 LT |
678 | result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
679 | ||
813a224f AC |
680 | if (result) { |
681 | dev_err(&port->dev, | |
682 | "%s - failed submitting read urb, error %d\n", | |
683 | __func__, result); | |
78aef519 | 684 | cypress_set_dead(port); |
1da177e4 | 685 | } |
335f8514 | 686 | port->port.drain_delay = 256; |
1da177e4 LT |
687 | return result; |
688 | } /* cypress_open */ | |
689 | ||
335f8514 AC |
690 | static void cypress_dtr_rts(struct usb_serial_port *port, int on) |
691 | { | |
692 | struct cypress_private *priv = usb_get_serial_port_data(port); | |
693 | /* drop dtr and rts */ | |
335f8514 AC |
694 | spin_lock_irq(&priv->lock); |
695 | if (on == 0) | |
696 | priv->line_control = 0; | |
697 | else | |
698 | priv->line_control = CONTROL_DTR | CONTROL_RTS; | |
699 | priv->cmd_ctrl = 1; | |
700 | spin_unlock_irq(&priv->lock); | |
701 | cypress_write(NULL, port, NULL, 0); | |
702 | } | |
1da177e4 | 703 | |
335f8514 | 704 | static void cypress_close(struct usb_serial_port *port) |
1da177e4 LT |
705 | { |
706 | struct cypress_private *priv = usb_get_serial_port_data(port); | |
1da177e4 | 707 | |
441b62c1 | 708 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 709 | |
9e3b1d8e ON |
710 | /* writing is potentially harmful, lock must be taken */ |
711 | mutex_lock(&port->serial->disc_mutex); | |
712 | if (port->serial->disconnected) { | |
713 | mutex_unlock(&port->serial->disc_mutex); | |
714 | return; | |
715 | } | |
335f8514 | 716 | cypress_buf_clear(priv->buf); |
441b62c1 | 717 | dbg("%s - stopping urbs", __func__); |
813a224f AC |
718 | usb_kill_urb(port->interrupt_in_urb); |
719 | usb_kill_urb(port->interrupt_out_urb); | |
1da177e4 | 720 | |
1da177e4 LT |
721 | |
722 | if (stats) | |
813a224f AC |
723 | dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n", |
724 | priv->bytes_in, priv->bytes_out, priv->cmd_count); | |
9e3b1d8e | 725 | mutex_unlock(&port->serial->disc_mutex); |
1da177e4 LT |
726 | } /* cypress_close */ |
727 | ||
728 | ||
95da310e AC |
729 | static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port, |
730 | const unsigned char *buf, int count) | |
1da177e4 LT |
731 | { |
732 | struct cypress_private *priv = usb_get_serial_port_data(port); | |
733 | unsigned long flags; | |
813a224f | 734 | |
441b62c1 | 735 | dbg("%s - port %d, %d bytes", __func__, port->number, count); |
1da177e4 LT |
736 | |
737 | /* line control commands, which need to be executed immediately, | |
738 | are not put into the buffer for obvious reasons. | |
739 | */ | |
740 | if (priv->cmd_ctrl) { | |
741 | count = 0; | |
742 | goto finish; | |
743 | } | |
813a224f | 744 | |
1da177e4 LT |
745 | if (!count) |
746 | return count; | |
813a224f | 747 | |
1da177e4 LT |
748 | spin_lock_irqsave(&priv->lock, flags); |
749 | count = cypress_buf_put(priv->buf, buf, count); | |
750 | spin_unlock_irqrestore(&priv->lock, flags); | |
751 | ||
752 | finish: | |
753 | cypress_send(port); | |
754 | ||
755 | return count; | |
756 | } /* cypress_write */ | |
757 | ||
758 | ||
759 | static void cypress_send(struct usb_serial_port *port) | |
760 | { | |
761 | int count = 0, result, offset, actual_size; | |
762 | struct cypress_private *priv = usb_get_serial_port_data(port); | |
763 | unsigned long flags; | |
813a224f | 764 | |
78aef519 MI |
765 | if (!priv->comm_is_ok) |
766 | return; | |
767 | ||
441b62c1 | 768 | dbg("%s - port %d", __func__, port->number); |
813a224f AC |
769 | dbg("%s - interrupt out size is %d", __func__, |
770 | port->interrupt_out_size); | |
771 | ||
1da177e4 LT |
772 | spin_lock_irqsave(&priv->lock, flags); |
773 | if (priv->write_urb_in_use) { | |
441b62c1 | 774 | dbg("%s - can't write, urb in use", __func__); |
1da177e4 LT |
775 | spin_unlock_irqrestore(&priv->lock, flags); |
776 | return; | |
777 | } | |
778 | spin_unlock_irqrestore(&priv->lock, flags); | |
779 | ||
780 | /* clear buffer */ | |
813a224f AC |
781 | memset(port->interrupt_out_urb->transfer_buffer, 0, |
782 | port->interrupt_out_size); | |
1da177e4 LT |
783 | |
784 | spin_lock_irqsave(&priv->lock, flags); | |
3416eaa1 MI |
785 | switch (priv->pkt_fmt) { |
786 | default: | |
787 | case packet_format_1: | |
788 | /* this is for the CY7C64013... */ | |
789 | offset = 2; | |
790 | port->interrupt_out_buffer[0] = priv->line_control; | |
791 | break; | |
792 | case packet_format_2: | |
793 | /* this is for the CY7C63743... */ | |
794 | offset = 1; | |
795 | port->interrupt_out_buffer[0] = priv->line_control; | |
796 | break; | |
1da177e4 LT |
797 | } |
798 | ||
799 | if (priv->line_control & CONTROL_RESET) | |
800 | priv->line_control &= ~CONTROL_RESET; | |
801 | ||
802 | if (priv->cmd_ctrl) { | |
803 | priv->cmd_count++; | |
441b62c1 | 804 | dbg("%s - line control command being issued", __func__); |
1da177e4 LT |
805 | spin_unlock_irqrestore(&priv->lock, flags); |
806 | goto send; | |
807 | } else | |
808 | spin_unlock_irqrestore(&priv->lock, flags); | |
809 | ||
810 | count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset], | |
811 | port->interrupt_out_size-offset); | |
812 | ||
813a224f | 813 | if (count == 0) |
1da177e4 | 814 | return; |
1da177e4 | 815 | |
3416eaa1 MI |
816 | switch (priv->pkt_fmt) { |
817 | default: | |
818 | case packet_format_1: | |
819 | port->interrupt_out_buffer[1] = count; | |
820 | break; | |
821 | case packet_format_2: | |
822 | port->interrupt_out_buffer[0] |= count; | |
1da177e4 LT |
823 | } |
824 | ||
441b62c1 | 825 | dbg("%s - count is %d", __func__, count); |
1da177e4 LT |
826 | |
827 | send: | |
828 | spin_lock_irqsave(&priv->lock, flags); | |
829 | priv->write_urb_in_use = 1; | |
830 | spin_unlock_irqrestore(&priv->lock, flags); | |
831 | ||
832 | if (priv->cmd_ctrl) | |
833 | actual_size = 1; | |
834 | else | |
3416eaa1 MI |
835 | actual_size = count + |
836 | (priv->pkt_fmt == packet_format_1 ? 2 : 1); | |
837 | ||
813a224f AC |
838 | usb_serial_debug_data(debug, &port->dev, __func__, |
839 | port->interrupt_out_size, | |
840 | port->interrupt_out_urb->transfer_buffer); | |
1da177e4 | 841 | |
9aa8dae7 MI |
842 | usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev, |
843 | usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress), | |
844 | port->interrupt_out_buffer, port->interrupt_out_size, | |
845 | cypress_write_int_callback, port, priv->write_urb_interval); | |
813a224f | 846 | result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC); |
1da177e4 | 847 | if (result) { |
813a224f AC |
848 | dev_err(&port->dev, |
849 | "%s - failed submitting write urb, error %d\n", | |
850 | __func__, result); | |
1da177e4 | 851 | priv->write_urb_in_use = 0; |
78aef519 | 852 | cypress_set_dead(port); |
1da177e4 LT |
853 | } |
854 | ||
855 | spin_lock_irqsave(&priv->lock, flags); | |
813a224f | 856 | if (priv->cmd_ctrl) |
1da177e4 | 857 | priv->cmd_ctrl = 0; |
813a224f AC |
858 | |
859 | /* do not count the line control and size bytes */ | |
860 | priv->bytes_out += count; | |
1da177e4 LT |
861 | spin_unlock_irqrestore(&priv->lock, flags); |
862 | ||
cf2c7481 | 863 | usb_serial_port_softint(port); |
1da177e4 LT |
864 | } /* cypress_send */ |
865 | ||
866 | ||
867 | /* returns how much space is available in the soft buffer */ | |
95da310e | 868 | static int cypress_write_room(struct tty_struct *tty) |
1da177e4 | 869 | { |
95da310e | 870 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
871 | struct cypress_private *priv = usb_get_serial_port_data(port); |
872 | int room = 0; | |
873 | unsigned long flags; | |
874 | ||
441b62c1 | 875 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
876 | |
877 | spin_lock_irqsave(&priv->lock, flags); | |
878 | room = cypress_buf_space_avail(priv->buf); | |
879 | spin_unlock_irqrestore(&priv->lock, flags); | |
880 | ||
441b62c1 | 881 | dbg("%s - returns %d", __func__, room); |
1da177e4 LT |
882 | return room; |
883 | } | |
884 | ||
885 | ||
95da310e | 886 | static int cypress_tiocmget(struct tty_struct *tty, struct file *file) |
1da177e4 | 887 | { |
95da310e | 888 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
889 | struct cypress_private *priv = usb_get_serial_port_data(port); |
890 | __u8 status, control; | |
891 | unsigned int result = 0; | |
892 | unsigned long flags; | |
813a224f | 893 | |
441b62c1 | 894 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
895 | |
896 | spin_lock_irqsave(&priv->lock, flags); | |
897 | control = priv->line_control; | |
898 | status = priv->current_status; | |
899 | spin_unlock_irqrestore(&priv->lock, flags); | |
900 | ||
901 | result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0) | |
902 | | ((control & CONTROL_RTS) ? TIOCM_RTS : 0) | |
903 | | ((status & UART_CTS) ? TIOCM_CTS : 0) | |
904 | | ((status & UART_DSR) ? TIOCM_DSR : 0) | |
905 | | ((status & UART_RI) ? TIOCM_RI : 0) | |
906 | | ((status & UART_CD) ? TIOCM_CD : 0); | |
907 | ||
441b62c1 | 908 | dbg("%s - result = %x", __func__, result); |
1da177e4 LT |
909 | |
910 | return result; | |
911 | } | |
912 | ||
913 | ||
95da310e | 914 | static int cypress_tiocmset(struct tty_struct *tty, struct file *file, |
1da177e4 LT |
915 | unsigned int set, unsigned int clear) |
916 | { | |
95da310e | 917 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
918 | struct cypress_private *priv = usb_get_serial_port_data(port); |
919 | unsigned long flags; | |
813a224f | 920 | |
441b62c1 | 921 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
922 | |
923 | spin_lock_irqsave(&priv->lock, flags); | |
924 | if (set & TIOCM_RTS) | |
925 | priv->line_control |= CONTROL_RTS; | |
926 | if (set & TIOCM_DTR) | |
927 | priv->line_control |= CONTROL_DTR; | |
928 | if (clear & TIOCM_RTS) | |
929 | priv->line_control &= ~CONTROL_RTS; | |
930 | if (clear & TIOCM_DTR) | |
931 | priv->line_control &= ~CONTROL_DTR; | |
8873aaa6 | 932 | priv->cmd_ctrl = 1; |
1da177e4 LT |
933 | spin_unlock_irqrestore(&priv->lock, flags); |
934 | ||
95da310e | 935 | return cypress_write(tty, port, NULL, 0); |
1da177e4 LT |
936 | } |
937 | ||
938 | ||
813a224f | 939 | static int cypress_ioctl(struct tty_struct *tty, struct file *file, |
95da310e | 940 | unsigned int cmd, unsigned long arg) |
1da177e4 | 941 | { |
95da310e | 942 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
943 | struct cypress_private *priv = usb_get_serial_port_data(port); |
944 | ||
441b62c1 | 945 | dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd); |
1da177e4 LT |
946 | |
947 | switch (cmd) { | |
813a224f AC |
948 | /* This code comes from drivers/char/serial.c and ftdi_sio.c */ |
949 | case TIOCMIWAIT: | |
950 | while (priv != NULL) { | |
951 | interruptible_sleep_on(&priv->delta_msr_wait); | |
952 | /* see if a signal did it */ | |
953 | if (signal_pending(current)) | |
954 | return -ERESTARTSYS; | |
955 | else { | |
956 | char diff = priv->diff_status; | |
957 | if (diff == 0) | |
958 | return -EIO; /* no change => error */ | |
959 | ||
960 | /* consume all events */ | |
961 | priv->diff_status = 0; | |
962 | ||
963 | /* return 0 if caller wanted to know about | |
964 | these bits */ | |
965 | if (((arg & TIOCM_RNG) && (diff & UART_RI)) || | |
966 | ((arg & TIOCM_DSR) && (diff & UART_DSR)) || | |
967 | ((arg & TIOCM_CD) && (diff & UART_CD)) || | |
968 | ((arg & TIOCM_CTS) && (diff & UART_CTS))) | |
969 | return 0; | |
970 | /* otherwise caller can't care less about what | |
971 | * happened, and so we continue to wait for | |
972 | * more events. | |
973 | */ | |
1da177e4 | 974 | } |
813a224f AC |
975 | } |
976 | return 0; | |
977 | default: | |
978 | break; | |
1da177e4 | 979 | } |
441b62c1 | 980 | dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd); |
1da177e4 LT |
981 | return -ENOIOCTLCMD; |
982 | } /* cypress_ioctl */ | |
983 | ||
984 | ||
95da310e AC |
985 | static void cypress_set_termios(struct tty_struct *tty, |
986 | struct usb_serial_port *port, struct ktermios *old_termios) | |
1da177e4 LT |
987 | { |
988 | struct cypress_private *priv = usb_get_serial_port_data(port); | |
1da177e4 | 989 | int data_bits, stop_bits, parity_type, parity_enable; |
8873aaa6 | 990 | unsigned cflag, iflag; |
1da177e4 LT |
991 | unsigned long flags; |
992 | __u8 oldlines; | |
3cb4a4f7 | 993 | int linechange = 0; |
b9db07fb | 994 | |
441b62c1 | 995 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 996 | |
1da177e4 | 997 | spin_lock_irqsave(&priv->lock, flags); |
fe1ae7fd AC |
998 | /* We can't clean this one up as we don't know the device type |
999 | early enough */ | |
1da177e4 LT |
1000 | if (!priv->termios_initialized) { |
1001 | if (priv->chiptype == CT_EARTHMATE) { | |
1002 | *(tty->termios) = tty_std_termios; | |
b9db07fb LM |
1003 | tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL | |
1004 | CLOCAL; | |
8873aaa6 AC |
1005 | tty->termios->c_ispeed = 4800; |
1006 | tty->termios->c_ospeed = 4800; | |
1da177e4 LT |
1007 | } else if (priv->chiptype == CT_CYPHIDCOM) { |
1008 | *(tty->termios) = tty_std_termios; | |
b9db07fb LM |
1009 | tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | |
1010 | CLOCAL; | |
8873aaa6 AC |
1011 | tty->termios->c_ispeed = 9600; |
1012 | tty->termios->c_ospeed = 9600; | |
a5c44e29 LM |
1013 | } else if (priv->chiptype == CT_CA42V2) { |
1014 | *(tty->termios) = tty_std_termios; | |
1015 | tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | | |
1016 | CLOCAL; | |
8873aaa6 AC |
1017 | tty->termios->c_ispeed = 9600; |
1018 | tty->termios->c_ospeed = 9600; | |
1da177e4 LT |
1019 | } |
1020 | priv->termios_initialized = 1; | |
1021 | } | |
1022 | spin_unlock_irqrestore(&priv->lock, flags); | |
1023 | ||
8873aaa6 AC |
1024 | /* Unsupported features need clearing */ |
1025 | tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS); | |
1026 | ||
1da177e4 LT |
1027 | cflag = tty->termios->c_cflag; |
1028 | iflag = tty->termios->c_iflag; | |
1029 | ||
1030 | /* check if there are new settings */ | |
1031 | if (old_termios) { | |
8873aaa6 AC |
1032 | spin_lock_irqsave(&priv->lock, flags); |
1033 | priv->tmp_termios = *(tty->termios); | |
1034 | spin_unlock_irqrestore(&priv->lock, flags); | |
1035 | } | |
1da177e4 LT |
1036 | |
1037 | /* set number of data bits, parity, stop bits */ | |
1038 | /* when parity is disabled the parity type bit is ignored */ | |
1039 | ||
b9db07fb LM |
1040 | /* 1 means 2 stop bits, 0 means 1 stop bit */ |
1041 | stop_bits = cflag & CSTOPB ? 1 : 0; | |
1042 | ||
1da177e4 LT |
1043 | if (cflag & PARENB) { |
1044 | parity_enable = 1; | |
b9db07fb LM |
1045 | /* 1 means odd parity, 0 means even parity */ |
1046 | parity_type = cflag & PARODD ? 1 : 0; | |
1da177e4 LT |
1047 | } else |
1048 | parity_enable = parity_type = 0; | |
1049 | ||
77336828 AC |
1050 | switch (cflag & CSIZE) { |
1051 | case CS5: | |
1052 | data_bits = 0; | |
1053 | break; | |
1054 | case CS6: | |
1055 | data_bits = 1; | |
1056 | break; | |
1057 | case CS7: | |
1058 | data_bits = 2; | |
1059 | break; | |
1060 | case CS8: | |
1da177e4 | 1061 | data_bits = 3; |
77336828 AC |
1062 | break; |
1063 | default: | |
194343d9 GKH |
1064 | dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n", |
1065 | __func__); | |
77336828 AC |
1066 | data_bits = 3; |
1067 | } | |
1da177e4 LT |
1068 | spin_lock_irqsave(&priv->lock, flags); |
1069 | oldlines = priv->line_control; | |
1070 | if ((cflag & CBAUD) == B0) { | |
1071 | /* drop dtr and rts */ | |
441b62c1 | 1072 | dbg("%s - dropping the lines, baud rate 0bps", __func__); |
1da177e4 | 1073 | priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); |
8873aaa6 | 1074 | } else |
3cb4a4f7 | 1075 | priv->line_control = (CONTROL_DTR | CONTROL_RTS); |
1da177e4 | 1076 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 | 1077 | |
b9db07fb | 1078 | dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, " |
441b62c1 | 1079 | "%d data_bits (+5)", __func__, stop_bits, |
b9db07fb LM |
1080 | parity_enable, parity_type, data_bits); |
1081 | ||
813a224f AC |
1082 | cypress_serial_control(tty, port, tty_get_baud_rate(tty), |
1083 | data_bits, stop_bits, | |
1084 | parity_enable, parity_type, | |
1085 | 0, CYPRESS_SET_CONFIG); | |
1da177e4 | 1086 | |
b9db07fb LM |
1087 | /* we perform a CYPRESS_GET_CONFIG so that the current settings are |
1088 | * filled into the private structure this should confirm that all is | |
1089 | * working if it returns what we just set */ | |
95da310e | 1090 | cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG); |
1da177e4 | 1091 | |
b9db07fb LM |
1092 | /* Here we can define custom tty settings for devices; the main tty |
1093 | * termios flag base comes from empeg.c */ | |
1da177e4 | 1094 | |
b9db07fb | 1095 | spin_lock_irqsave(&priv->lock, flags); |
813a224f | 1096 | if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) { |
b9db07fb LM |
1097 | dbg("Using custom termios settings for a baud rate of " |
1098 | "4800bps."); | |
1da177e4 LT |
1099 | /* define custom termios settings for NMEA protocol */ |
1100 | ||
1da177e4 | 1101 | tty->termios->c_iflag /* input modes - */ |
b9db07fb LM |
1102 | &= ~(IGNBRK /* disable ignore break */ |
1103 | | BRKINT /* disable break causes interrupt */ | |
1104 | | PARMRK /* disable mark parity errors */ | |
1105 | | ISTRIP /* disable clear high bit of input char */ | |
1106 | | INLCR /* disable translate NL to CR */ | |
1107 | | IGNCR /* disable ignore CR */ | |
1108 | | ICRNL /* disable translate CR to NL */ | |
1109 | | IXON); /* disable enable XON/XOFF flow control */ | |
1110 | ||
1da177e4 | 1111 | tty->termios->c_oflag /* output modes */ |
b9db07fb | 1112 | &= ~OPOST; /* disable postprocess output char */ |
1da177e4 | 1113 | |
b9db07fb LM |
1114 | tty->termios->c_lflag /* line discipline modes */ |
1115 | &= ~(ECHO /* disable echo input characters */ | |
1116 | | ECHONL /* disable echo new line */ | |
1117 | | ICANON /* disable erase, kill, werase, and rprnt | |
1118 | special characters */ | |
1119 | | ISIG /* disable interrupt, quit, and suspend | |
1120 | special characters */ | |
1121 | | IEXTEN); /* disable non-POSIX special characters */ | |
3cb4a4f7 | 1122 | } /* CT_CYPHIDCOM: Application should handle this for device */ |
1da177e4 | 1123 | |
1da177e4 LT |
1124 | linechange = (priv->line_control != oldlines); |
1125 | spin_unlock_irqrestore(&priv->lock, flags); | |
1126 | ||
1127 | /* if necessary, set lines */ | |
3cb4a4f7 | 1128 | if (linechange) { |
1da177e4 | 1129 | priv->cmd_ctrl = 1; |
95da310e | 1130 | cypress_write(tty, port, NULL, 0); |
1da177e4 | 1131 | } |
1da177e4 LT |
1132 | } /* cypress_set_termios */ |
1133 | ||
b9db07fb | 1134 | |
1da177e4 | 1135 | /* returns amount of data still left in soft buffer */ |
95da310e | 1136 | static int cypress_chars_in_buffer(struct tty_struct *tty) |
1da177e4 | 1137 | { |
95da310e | 1138 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
1139 | struct cypress_private *priv = usb_get_serial_port_data(port); |
1140 | int chars = 0; | |
1141 | unsigned long flags; | |
1142 | ||
441b62c1 | 1143 | dbg("%s - port %d", __func__, port->number); |
813a224f | 1144 | |
1da177e4 LT |
1145 | spin_lock_irqsave(&priv->lock, flags); |
1146 | chars = cypress_buf_data_avail(priv->buf); | |
1147 | spin_unlock_irqrestore(&priv->lock, flags); | |
1148 | ||
441b62c1 | 1149 | dbg("%s - returns %d", __func__, chars); |
1da177e4 LT |
1150 | return chars; |
1151 | } | |
1152 | ||
1153 | ||
95da310e | 1154 | static void cypress_throttle(struct tty_struct *tty) |
1da177e4 | 1155 | { |
95da310e | 1156 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 | 1157 | struct cypress_private *priv = usb_get_serial_port_data(port); |
1da177e4 | 1158 | |
441b62c1 | 1159 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 1160 | |
63832515 | 1161 | spin_lock_irq(&priv->lock); |
1da177e4 | 1162 | priv->rx_flags = THROTTLED; |
63832515 | 1163 | spin_unlock_irq(&priv->lock); |
1da177e4 LT |
1164 | } |
1165 | ||
1166 | ||
95da310e | 1167 | static void cypress_unthrottle(struct tty_struct *tty) |
1da177e4 | 1168 | { |
95da310e | 1169 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
1170 | struct cypress_private *priv = usb_get_serial_port_data(port); |
1171 | int actually_throttled, result; | |
1da177e4 | 1172 | |
441b62c1 | 1173 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 1174 | |
63832515 | 1175 | spin_lock_irq(&priv->lock); |
1da177e4 LT |
1176 | actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED; |
1177 | priv->rx_flags = 0; | |
63832515 | 1178 | spin_unlock_irq(&priv->lock); |
1da177e4 | 1179 | |
78aef519 MI |
1180 | if (!priv->comm_is_ok) |
1181 | return; | |
1182 | ||
1da177e4 LT |
1183 | if (actually_throttled) { |
1184 | port->interrupt_in_urb->dev = port->serial->dev; | |
1185 | ||
63832515 | 1186 | result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
78aef519 | 1187 | if (result) { |
b9db07fb | 1188 | dev_err(&port->dev, "%s - failed submitting read urb, " |
441b62c1 | 1189 | "error %d\n", __func__, result); |
78aef519 MI |
1190 | cypress_set_dead(port); |
1191 | } | |
1da177e4 LT |
1192 | } |
1193 | } | |
1194 | ||
1195 | ||
7d12e780 | 1196 | static void cypress_read_int_callback(struct urb *urb) |
1da177e4 | 1197 | { |
cdc97792 | 1198 | struct usb_serial_port *port = urb->context; |
1da177e4 LT |
1199 | struct cypress_private *priv = usb_get_serial_port_data(port); |
1200 | struct tty_struct *tty; | |
1201 | unsigned char *data = urb->transfer_buffer; | |
1202 | unsigned long flags; | |
b9db07fb | 1203 | char tty_flag = TTY_NORMAL; |
1da177e4 LT |
1204 | int havedata = 0; |
1205 | int bytes = 0; | |
1206 | int result; | |
1207 | int i = 0; | |
8d7bc55e | 1208 | int status = urb->status; |
1da177e4 | 1209 | |
441b62c1 | 1210 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 1211 | |
8d7bc55e | 1212 | switch (status) { |
78aef519 MI |
1213 | case 0: /* success */ |
1214 | break; | |
1215 | case -ECONNRESET: | |
1216 | case -ENOENT: | |
1217 | case -ESHUTDOWN: | |
1218 | /* precursor to disconnect so just go away */ | |
1219 | return; | |
1220 | case -EPIPE: | |
4d2fae8b AS |
1221 | /* Can't call usb_clear_halt while in_interrupt */ |
1222 | /* FALLS THROUGH */ | |
78aef519 MI |
1223 | default: |
1224 | /* something ugly is going on... */ | |
813a224f AC |
1225 | dev_err(&urb->dev->dev, |
1226 | "%s - unexpected nonzero read status received: %d\n", | |
1227 | __func__, status); | |
78aef519 | 1228 | cypress_set_dead(port); |
1da177e4 LT |
1229 | return; |
1230 | } | |
1231 | ||
1232 | spin_lock_irqsave(&priv->lock, flags); | |
1233 | if (priv->rx_flags & THROTTLED) { | |
441b62c1 | 1234 | dbg("%s - now throttling", __func__); |
1da177e4 LT |
1235 | priv->rx_flags |= ACTUALLY_THROTTLED; |
1236 | spin_unlock_irqrestore(&priv->lock, flags); | |
1237 | return; | |
1238 | } | |
1239 | spin_unlock_irqrestore(&priv->lock, flags); | |
1240 | ||
4a90f09b | 1241 | tty = tty_port_tty_get(&port->port); |
1da177e4 | 1242 | if (!tty) { |
441b62c1 | 1243 | dbg("%s - bad tty pointer - exiting", __func__); |
1da177e4 LT |
1244 | return; |
1245 | } | |
1246 | ||
1247 | spin_lock_irqsave(&priv->lock, flags); | |
3416eaa1 MI |
1248 | result = urb->actual_length; |
1249 | switch (priv->pkt_fmt) { | |
1250 | default: | |
1251 | case packet_format_1: | |
1252 | /* This is for the CY7C64013... */ | |
1253 | priv->current_status = data[0] & 0xF8; | |
1254 | bytes = data[1] + 2; | |
1255 | i = 2; | |
1256 | if (bytes > 2) | |
1257 | havedata = 1; | |
1258 | break; | |
1259 | case packet_format_2: | |
1260 | /* This is for the CY7C63743... */ | |
1261 | priv->current_status = data[0] & 0xF8; | |
1262 | bytes = (data[0] & 0x07) + 1; | |
1263 | i = 1; | |
1264 | if (bytes > 1) | |
1265 | havedata = 1; | |
1266 | break; | |
1da177e4 LT |
1267 | } |
1268 | spin_unlock_irqrestore(&priv->lock, flags); | |
3416eaa1 MI |
1269 | if (result < bytes) { |
1270 | dbg("%s - wrong packet size - received %d bytes but packet " | |
1271 | "said %d bytes", __func__, result, bytes); | |
1272 | goto continue_read; | |
1273 | } | |
1da177e4 | 1274 | |
813a224f AC |
1275 | usb_serial_debug_data(debug, &port->dev, __func__, |
1276 | urb->actual_length, data); | |
1da177e4 LT |
1277 | |
1278 | spin_lock_irqsave(&priv->lock, flags); | |
1279 | /* check to see if status has changed */ | |
6768306c MI |
1280 | if (priv->current_status != priv->prev_status) { |
1281 | priv->diff_status |= priv->current_status ^ | |
1282 | priv->prev_status; | |
1283 | wake_up_interruptible(&priv->delta_msr_wait); | |
1284 | priv->prev_status = priv->current_status; | |
1da177e4 | 1285 | } |
b9db07fb | 1286 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 | 1287 | |
b9db07fb LM |
1288 | /* hangup, as defined in acm.c... this might be a bad place for it |
1289 | * though */ | |
1290 | if (tty && !(tty->termios->c_cflag & CLOCAL) && | |
1291 | !(priv->current_status & UART_CD)) { | |
441b62c1 | 1292 | dbg("%s - calling hangup", __func__); |
1da177e4 LT |
1293 | tty_hangup(tty); |
1294 | goto continue_read; | |
1295 | } | |
1296 | ||
b9db07fb LM |
1297 | /* There is one error bit... I'm assuming it is a parity error |
1298 | * indicator as the generic firmware will set this bit to 1 if a | |
1299 | * parity error occurs. | |
1300 | * I can not find reference to any other error events. */ | |
1da177e4 LT |
1301 | spin_lock_irqsave(&priv->lock, flags); |
1302 | if (priv->current_status & CYP_ERROR) { | |
1303 | spin_unlock_irqrestore(&priv->lock, flags); | |
1304 | tty_flag = TTY_PARITY; | |
441b62c1 | 1305 | dbg("%s - Parity Error detected", __func__); |
1da177e4 LT |
1306 | } else |
1307 | spin_unlock_irqrestore(&priv->lock, flags); | |
1308 | ||
1309 | /* process read if there is data other than line status */ | |
a108bfcb AC |
1310 | if (tty && bytes > i) { |
1311 | tty_insert_flip_string_fixed_flag(tty, data + i, | |
1312 | bytes - i, tty_flag); | |
4a90f09b | 1313 | tty_flip_buffer_push(tty); |
1da177e4 LT |
1314 | } |
1315 | ||
1316 | spin_lock_irqsave(&priv->lock, flags); | |
b9db07fb LM |
1317 | /* control and status byte(s) are also counted */ |
1318 | priv->bytes_in += bytes; | |
1da177e4 LT |
1319 | spin_unlock_irqrestore(&priv->lock, flags); |
1320 | ||
1321 | continue_read: | |
4a90f09b | 1322 | tty_kref_put(tty); |
b9db07fb LM |
1323 | |
1324 | /* Continue trying to always read... unless the port has closed. */ | |
1da177e4 | 1325 | |
95da310e | 1326 | if (port->port.count > 0 && priv->comm_is_ok) { |
b9db07fb LM |
1327 | usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev, |
1328 | usb_rcvintpipe(port->serial->dev, | |
1329 | port->interrupt_in_endpointAddress), | |
1330 | port->interrupt_in_urb->transfer_buffer, | |
1331 | port->interrupt_in_urb->transfer_buffer_length, | |
813a224f AC |
1332 | cypress_read_int_callback, port, |
1333 | priv->read_urb_interval); | |
b9db07fb | 1334 | result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); |
78aef519 | 1335 | if (result) { |
b9db07fb | 1336 | dev_err(&urb->dev->dev, "%s - failed resubmitting " |
441b62c1 | 1337 | "read urb, error %d\n", __func__, |
b9db07fb | 1338 | result); |
78aef519 MI |
1339 | cypress_set_dead(port); |
1340 | } | |
1da177e4 | 1341 | } |
b9db07fb | 1342 | |
1da177e4 LT |
1343 | return; |
1344 | } /* cypress_read_int_callback */ | |
1345 | ||
1346 | ||
7d12e780 | 1347 | static void cypress_write_int_callback(struct urb *urb) |
1da177e4 | 1348 | { |
cdc97792 | 1349 | struct usb_serial_port *port = urb->context; |
1da177e4 LT |
1350 | struct cypress_private *priv = usb_get_serial_port_data(port); |
1351 | int result; | |
8d7bc55e | 1352 | int status = urb->status; |
1da177e4 | 1353 | |
441b62c1 | 1354 | dbg("%s - port %d", __func__, port->number); |
8d7bc55e GKH |
1355 | |
1356 | switch (status) { | |
813a224f AC |
1357 | case 0: |
1358 | /* success */ | |
1359 | break; | |
1360 | case -ECONNRESET: | |
1361 | case -ENOENT: | |
1362 | case -ESHUTDOWN: | |
1363 | /* this urb is terminated, clean up */ | |
1364 | dbg("%s - urb shutting down with status: %d", | |
1365 | __func__, status); | |
1366 | priv->write_urb_in_use = 0; | |
1367 | return; | |
1368 | case -EPIPE: /* no break needed; clear halt and resubmit */ | |
1369 | if (!priv->comm_is_ok) | |
1da177e4 | 1370 | break; |
813a224f AC |
1371 | usb_clear_halt(port->serial->dev, 0x02); |
1372 | /* error in the urb, so we have to resubmit it */ | |
1373 | dbg("%s - nonzero write bulk status received: %d", | |
1374 | __func__, status); | |
1375 | port->interrupt_out_urb->transfer_buffer_length = 1; | |
1376 | port->interrupt_out_urb->dev = port->serial->dev; | |
1377 | result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC); | |
1378 | if (!result) | |
1da177e4 | 1379 | return; |
813a224f AC |
1380 | dev_err(&urb->dev->dev, |
1381 | "%s - failed resubmitting write urb, error %d\n", | |
1382 | __func__, result); | |
1383 | cypress_set_dead(port); | |
1384 | break; | |
1385 | default: | |
1386 | dev_err(&urb->dev->dev, | |
1387 | "%s - unexpected nonzero write status received: %d\n", | |
1388 | __func__, status); | |
1389 | cypress_set_dead(port); | |
1390 | break; | |
1da177e4 | 1391 | } |
1da177e4 | 1392 | priv->write_urb_in_use = 0; |
813a224f | 1393 | |
1da177e4 LT |
1394 | /* send any buffered data */ |
1395 | cypress_send(port); | |
1396 | } | |
1397 | ||
1398 | ||
1399 | /***************************************************************************** | |
1400 | * Write buffer functions - buffering code from pl2303 used | |
1401 | *****************************************************************************/ | |
1402 | ||
1403 | /* | |
1404 | * cypress_buf_alloc | |
1405 | * | |
1406 | * Allocate a circular buffer and all associated memory. | |
1407 | */ | |
1408 | ||
1409 | static struct cypress_buf *cypress_buf_alloc(unsigned int size) | |
1410 | { | |
1411 | ||
1412 | struct cypress_buf *cb; | |
1413 | ||
1414 | ||
1415 | if (size == 0) | |
1416 | return NULL; | |
1417 | ||
5cbded58 | 1418 | cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL); |
1da177e4 LT |
1419 | if (cb == NULL) |
1420 | return NULL; | |
1421 | ||
1422 | cb->buf_buf = kmalloc(size, GFP_KERNEL); | |
1423 | if (cb->buf_buf == NULL) { | |
1424 | kfree(cb); | |
1425 | return NULL; | |
1426 | } | |
1427 | ||
1428 | cb->buf_size = size; | |
1429 | cb->buf_get = cb->buf_put = cb->buf_buf; | |
1430 | ||
1431 | return cb; | |
1432 | ||
1433 | } | |
1434 | ||
1435 | ||
1436 | /* | |
1437 | * cypress_buf_free | |
1438 | * | |
1439 | * Free the buffer and all associated memory. | |
1440 | */ | |
1441 | ||
1442 | static void cypress_buf_free(struct cypress_buf *cb) | |
1443 | { | |
1bc3c9e1 JJ |
1444 | if (cb) { |
1445 | kfree(cb->buf_buf); | |
1da177e4 LT |
1446 | kfree(cb); |
1447 | } | |
1448 | } | |
1449 | ||
1450 | ||
1451 | /* | |
1452 | * cypress_buf_clear | |
1453 | * | |
1454 | * Clear out all data in the circular buffer. | |
1455 | */ | |
1456 | ||
1457 | static void cypress_buf_clear(struct cypress_buf *cb) | |
1458 | { | |
1459 | if (cb != NULL) | |
1460 | cb->buf_get = cb->buf_put; | |
1461 | /* equivalent to a get of all data available */ | |
1462 | } | |
1463 | ||
1464 | ||
1465 | /* | |
1466 | * cypress_buf_data_avail | |
1467 | * | |
1468 | * Return the number of bytes of data available in the circular | |
1469 | * buffer. | |
1470 | */ | |
1471 | ||
1472 | static unsigned int cypress_buf_data_avail(struct cypress_buf *cb) | |
1473 | { | |
1474 | if (cb != NULL) | |
813a224f AC |
1475 | return (cb->buf_size + cb->buf_put - cb->buf_get) |
1476 | % cb->buf_size; | |
1da177e4 LT |
1477 | else |
1478 | return 0; | |
1479 | } | |
1480 | ||
1481 | ||
1482 | /* | |
1483 | * cypress_buf_space_avail | |
1484 | * | |
1485 | * Return the number of bytes of space available in the circular | |
1486 | * buffer. | |
1487 | */ | |
1488 | ||
1489 | static unsigned int cypress_buf_space_avail(struct cypress_buf *cb) | |
1490 | { | |
1491 | if (cb != NULL) | |
813a224f AC |
1492 | return (cb->buf_size + cb->buf_get - cb->buf_put - 1) |
1493 | % cb->buf_size; | |
1da177e4 LT |
1494 | else |
1495 | return 0; | |
1496 | } | |
1497 | ||
1498 | ||
1499 | /* | |
1500 | * cypress_buf_put | |
1501 | * | |
1502 | * Copy data data from a user buffer and put it into the circular buffer. | |
1503 | * Restrict to the amount of space available. | |
1504 | * | |
1505 | * Return the number of bytes copied. | |
1506 | */ | |
1507 | ||
1508 | static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, | |
1509 | unsigned int count) | |
1510 | { | |
1511 | ||
1512 | unsigned int len; | |
1513 | ||
1514 | ||
1515 | if (cb == NULL) | |
1516 | return 0; | |
1517 | ||
1518 | len = cypress_buf_space_avail(cb); | |
1519 | if (count > len) | |
1520 | count = len; | |
1521 | ||
1522 | if (count == 0) | |
1523 | return 0; | |
1524 | ||
1525 | len = cb->buf_buf + cb->buf_size - cb->buf_put; | |
1526 | if (count > len) { | |
1527 | memcpy(cb->buf_put, buf, len); | |
1528 | memcpy(cb->buf_buf, buf+len, count - len); | |
1529 | cb->buf_put = cb->buf_buf + count - len; | |
1530 | } else { | |
1531 | memcpy(cb->buf_put, buf, count); | |
1532 | if (count < len) | |
1533 | cb->buf_put += count; | |
1534 | else /* count == len */ | |
1535 | cb->buf_put = cb->buf_buf; | |
1536 | } | |
1537 | ||
1538 | return count; | |
1539 | ||
1540 | } | |
1541 | ||
1542 | ||
1543 | /* | |
1544 | * cypress_buf_get | |
1545 | * | |
1546 | * Get data from the circular buffer and copy to the given buffer. | |
1547 | * Restrict to the amount of data available. | |
1548 | * | |
1549 | * Return the number of bytes copied. | |
1550 | */ | |
1551 | ||
1552 | static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, | |
1553 | unsigned int count) | |
1554 | { | |
1555 | ||
1556 | unsigned int len; | |
1557 | ||
1558 | ||
1559 | if (cb == NULL) | |
1560 | return 0; | |
1561 | ||
1562 | len = cypress_buf_data_avail(cb); | |
1563 | if (count > len) | |
1564 | count = len; | |
1565 | ||
1566 | if (count == 0) | |
1567 | return 0; | |
1568 | ||
1569 | len = cb->buf_buf + cb->buf_size - cb->buf_get; | |
1570 | if (count > len) { | |
1571 | memcpy(buf, cb->buf_get, len); | |
1572 | memcpy(buf+len, cb->buf_buf, count - len); | |
1573 | cb->buf_get = cb->buf_buf + count - len; | |
1574 | } else { | |
1575 | memcpy(buf, cb->buf_get, count); | |
1576 | if (count < len) | |
1577 | cb->buf_get += count; | |
1578 | else /* count == len */ | |
1579 | cb->buf_get = cb->buf_buf; | |
1580 | } | |
1581 | ||
1582 | return count; | |
1583 | ||
1584 | } | |
1585 | ||
1586 | /***************************************************************************** | |
1587 | * Module functions | |
1588 | *****************************************************************************/ | |
1589 | ||
1590 | static int __init cypress_init(void) | |
1591 | { | |
1592 | int retval; | |
813a224f | 1593 | |
441b62c1 | 1594 | dbg("%s", __func__); |
813a224f | 1595 | |
1da177e4 LT |
1596 | retval = usb_serial_register(&cypress_earthmate_device); |
1597 | if (retval) | |
1598 | goto failed_em_register; | |
1599 | retval = usb_serial_register(&cypress_hidcom_device); | |
1600 | if (retval) | |
1601 | goto failed_hidcom_register; | |
a5c44e29 LM |
1602 | retval = usb_serial_register(&cypress_ca42v2_device); |
1603 | if (retval) | |
1604 | goto failed_ca42v2_register; | |
1da177e4 LT |
1605 | retval = usb_register(&cypress_driver); |
1606 | if (retval) | |
1607 | goto failed_usb_register; | |
1608 | ||
c197a8db GKH |
1609 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" |
1610 | DRIVER_DESC "\n"); | |
1da177e4 | 1611 | return 0; |
2e46b748 | 1612 | |
1da177e4 | 1613 | failed_usb_register: |
a5c44e29 | 1614 | usb_serial_deregister(&cypress_ca42v2_device); |
2e46b748 | 1615 | failed_ca42v2_register: |
1da177e4 | 1616 | usb_serial_deregister(&cypress_hidcom_device); |
2e46b748 | 1617 | failed_hidcom_register: |
1da177e4 | 1618 | usb_serial_deregister(&cypress_earthmate_device); |
2e46b748 | 1619 | failed_em_register: |
1da177e4 LT |
1620 | return retval; |
1621 | } | |
1622 | ||
1623 | ||
813a224f | 1624 | static void __exit cypress_exit(void) |
1da177e4 | 1625 | { |
441b62c1 | 1626 | dbg("%s", __func__); |
1da177e4 | 1627 | |
813a224f AC |
1628 | usb_deregister(&cypress_driver); |
1629 | usb_serial_deregister(&cypress_earthmate_device); | |
1630 | usb_serial_deregister(&cypress_hidcom_device); | |
1631 | usb_serial_deregister(&cypress_ca42v2_device); | |
1da177e4 LT |
1632 | } |
1633 | ||
1634 | ||
1635 | module_init(cypress_init); | |
1636 | module_exit(cypress_exit); | |
1637 | ||
813a224f AC |
1638 | MODULE_AUTHOR(DRIVER_AUTHOR); |
1639 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1640 | MODULE_VERSION(DRIVER_VERSION); | |
1da177e4 LT |
1641 | MODULE_LICENSE("GPL"); |
1642 | ||
1643 | module_param(debug, bool, S_IRUGO | S_IWUSR); | |
1644 | MODULE_PARM_DESC(debug, "Debug enabled or not"); | |
1645 | module_param(stats, bool, S_IRUGO | S_IWUSR); | |
1646 | MODULE_PARM_DESC(stats, "Enable statistics or not"); | |
3cb4a4f7 LM |
1647 | module_param(interval, int, S_IRUGO | S_IWUSR); |
1648 | MODULE_PARM_DESC(interval, "Overrides interrupt interval"); | |
c312659c MF |
1649 | module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR); |
1650 | MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates"); |