Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * USB ConnectTech WhiteHEAT driver | |
3 | * | |
4 | * Copyright (C) 2002 | |
5 | * Connect Tech Inc. | |
6 | * | |
7 | * Copyright (C) 1999 - 2001 | |
8 | * Greg Kroah-Hartman (greg@kroah.com) | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
80359a9c AC |
15 | * See Documentation/usb/usb-serial.txt for more information on using this |
16 | * driver | |
1da177e4 LT |
17 | * |
18 | * (10/09/2002) Stuart MacDonald (stuartm@connecttech.com) | |
19 | * Upgrade to full working driver | |
20 | * | |
21 | * (05/30/2001) gkh | |
80359a9c AC |
22 | * switched from using spinlock to a semaphore, which fixes lots of |
23 | * problems. | |
1da177e4 LT |
24 | * |
25 | * (04/08/2001) gb | |
26 | * Identify version on module load. | |
80359a9c | 27 | * |
1da177e4 | 28 | * 2001_Mar_19 gkh |
80359a9c | 29 | * Fixed MOD_INC and MOD_DEC logic, the ability to open a port more |
1da177e4 LT |
30 | * than once, and the got the proper usb_device_id table entries so |
31 | * the driver works again. | |
32 | * | |
33 | * (11/01/2000) Adam J. Richter | |
34 | * usb_device_id table support | |
80359a9c | 35 | * |
1da177e4 LT |
36 | * (10/05/2000) gkh |
37 | * Fixed bug with urb->dev not being set properly, now that the usb | |
38 | * core needs it. | |
80359a9c | 39 | * |
1da177e4 LT |
40 | * (10/03/2000) smd |
41 | * firmware is improved to guard against crap sent to device | |
42 | * firmware now replies CMD_FAILURE on bad things | |
43 | * read_callback fix you provided for private info struct | |
44 | * command_finished now indicates success or fail | |
45 | * setup_port struct now packed to avoid gcc padding | |
46 | * firmware uses 1 based port numbering, driver now handles that | |
47 | * | |
48 | * (09/11/2000) gkh | |
49 | * Removed DEBUG #ifdefs with call to usb_serial_debug_data | |
50 | * | |
51 | * (07/19/2000) gkh | |
52 | * Added module_init and module_exit functions to handle the fact that this | |
53 | * driver is a loadable module now. | |
54 | * Fixed bug with port->minor that was found by Al Borchers | |
55 | * | |
56 | * (07/04/2000) gkh | |
80359a9c AC |
57 | * Added support for port settings. Baud rate can now be changed. Line |
58 | * signals are not transferred to and from the tty layer yet, but things | |
59 | * seem to be working well now. | |
1da177e4 LT |
60 | * |
61 | * (05/04/2000) gkh | |
62 | * First cut at open and close commands. Data can flow through the ports at | |
63 | * default speeds now. | |
64 | * | |
65 | * (03/26/2000) gkh | |
66 | * Split driver up into device specific pieces. | |
80359a9c | 67 | * |
1da177e4 LT |
68 | */ |
69 | ||
1da177e4 LT |
70 | #include <linux/kernel.h> |
71 | #include <linux/errno.h> | |
72 | #include <linux/init.h> | |
73 | #include <linux/slab.h> | |
74 | #include <linux/tty.h> | |
75 | #include <linux/tty_driver.h> | |
76 | #include <linux/tty_flip.h> | |
77 | #include <linux/module.h> | |
78 | #include <linux/spinlock.h> | |
08a2b3b6 | 79 | #include <linux/mutex.h> |
80359a9c | 80 | #include <linux/uaccess.h> |
1da177e4 LT |
81 | #include <asm/termbits.h> |
82 | #include <linux/usb.h> | |
83 | #include <linux/serial_reg.h> | |
84 | #include <linux/serial.h> | |
a969888c | 85 | #include <linux/usb/serial.h> |
ec6752f5 DW |
86 | #include <linux/firmware.h> |
87 | #include <linux/ihex.h> | |
1da177e4 LT |
88 | #include "whiteheat.h" /* WhiteHEAT specific commands */ |
89 | ||
90 | static int debug; | |
91 | ||
92 | #ifndef CMSPAR | |
93 | #define CMSPAR 0 | |
94 | #endif | |
95 | ||
96 | /* | |
97 | * Version Information | |
98 | */ | |
99 | #define DRIVER_VERSION "v2.0" | |
100 | #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>" | |
101 | #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver" | |
102 | ||
103 | #define CONNECT_TECH_VENDOR_ID 0x0710 | |
104 | #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001 | |
105 | #define CONNECT_TECH_WHITE_HEAT_ID 0x8001 | |
106 | ||
107 | /* | |
108 | ID tables for whiteheat are unusual, because we want to different | |
109 | things for different versions of the device. Eventually, this | |
110 | will be doable from a single table. But, for now, we define two | |
111 | separate ID tables, and then a third table that combines them | |
112 | just for the purpose of exporting the autoloading information. | |
113 | */ | |
114 | static struct usb_device_id id_table_std [] = { | |
115 | { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) }, | |
116 | { } /* Terminating entry */ | |
117 | }; | |
118 | ||
119 | static struct usb_device_id id_table_prerenumeration [] = { | |
120 | { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) }, | |
121 | { } /* Terminating entry */ | |
122 | }; | |
123 | ||
124 | static struct usb_device_id id_table_combined [] = { | |
125 | { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) }, | |
126 | { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) }, | |
127 | { } /* Terminating entry */ | |
128 | }; | |
129 | ||
80359a9c | 130 | MODULE_DEVICE_TABLE(usb, id_table_combined); |
1da177e4 LT |
131 | |
132 | static struct usb_driver whiteheat_driver = { | |
1da177e4 LT |
133 | .name = "whiteheat", |
134 | .probe = usb_serial_probe, | |
135 | .disconnect = usb_serial_disconnect, | |
136 | .id_table = id_table_combined, | |
ba9dc657 | 137 | .no_dynamic_id = 1, |
1da177e4 LT |
138 | }; |
139 | ||
140 | /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */ | |
80359a9c AC |
141 | static int whiteheat_firmware_download(struct usb_serial *serial, |
142 | const struct usb_device_id *id); | |
143 | static int whiteheat_firmware_attach(struct usb_serial *serial); | |
1da177e4 LT |
144 | |
145 | /* function prototypes for the Connect Tech WhiteHEAT serial converter */ | |
80359a9c AC |
146 | static int whiteheat_attach(struct usb_serial *serial); |
147 | static void whiteheat_shutdown(struct usb_serial *serial); | |
148 | static int whiteheat_open(struct tty_struct *tty, | |
149 | struct usb_serial_port *port, struct file *filp); | |
150 | static void whiteheat_close(struct tty_struct *tty, | |
151 | struct usb_serial_port *port, struct file *filp); | |
152 | static int whiteheat_write(struct tty_struct *tty, | |
153 | struct usb_serial_port *port, | |
154 | const unsigned char *buf, int count); | |
155 | static int whiteheat_write_room(struct tty_struct *tty); | |
156 | static int whiteheat_ioctl(struct tty_struct *tty, struct file *file, | |
157 | unsigned int cmd, unsigned long arg); | |
158 | static void whiteheat_set_termios(struct tty_struct *tty, | |
159 | struct usb_serial_port *port, struct ktermios *old); | |
160 | static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file); | |
161 | static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file, | |
162 | unsigned int set, unsigned int clear); | |
163 | static void whiteheat_break_ctl(struct tty_struct *tty, int break_state); | |
164 | static int whiteheat_chars_in_buffer(struct tty_struct *tty); | |
165 | static void whiteheat_throttle(struct tty_struct *tty); | |
166 | static void whiteheat_unthrottle(struct tty_struct *tty); | |
167 | static void whiteheat_read_callback(struct urb *urb); | |
168 | static void whiteheat_write_callback(struct urb *urb); | |
1da177e4 | 169 | |
ea65370d | 170 | static struct usb_serial_driver whiteheat_fake_device = { |
18fcac35 GKH |
171 | .driver = { |
172 | .owner = THIS_MODULE, | |
269bda1c | 173 | .name = "whiteheatnofirm", |
18fcac35 | 174 | }, |
269bda1c | 175 | .description = "Connect Tech - WhiteHEAT - (prerenumeration)", |
d9b1b787 | 176 | .usb_driver = &whiteheat_driver, |
1da177e4 | 177 | .id_table = id_table_prerenumeration, |
1da177e4 LT |
178 | .num_ports = 1, |
179 | .probe = whiteheat_firmware_download, | |
180 | .attach = whiteheat_firmware_attach, | |
181 | }; | |
182 | ||
ea65370d | 183 | static struct usb_serial_driver whiteheat_device = { |
18fcac35 GKH |
184 | .driver = { |
185 | .owner = THIS_MODULE, | |
269bda1c | 186 | .name = "whiteheat", |
18fcac35 | 187 | }, |
269bda1c | 188 | .description = "Connect Tech - WhiteHEAT", |
d9b1b787 | 189 | .usb_driver = &whiteheat_driver, |
1da177e4 | 190 | .id_table = id_table_std, |
1da177e4 LT |
191 | .num_ports = 4, |
192 | .attach = whiteheat_attach, | |
193 | .shutdown = whiteheat_shutdown, | |
194 | .open = whiteheat_open, | |
195 | .close = whiteheat_close, | |
196 | .write = whiteheat_write, | |
197 | .write_room = whiteheat_write_room, | |
198 | .ioctl = whiteheat_ioctl, | |
199 | .set_termios = whiteheat_set_termios, | |
200 | .break_ctl = whiteheat_break_ctl, | |
201 | .tiocmget = whiteheat_tiocmget, | |
202 | .tiocmset = whiteheat_tiocmset, | |
203 | .chars_in_buffer = whiteheat_chars_in_buffer, | |
204 | .throttle = whiteheat_throttle, | |
205 | .unthrottle = whiteheat_unthrottle, | |
206 | .read_bulk_callback = whiteheat_read_callback, | |
207 | .write_bulk_callback = whiteheat_write_callback, | |
208 | }; | |
209 | ||
210 | ||
211 | struct whiteheat_command_private { | |
08a2b3b6 | 212 | struct mutex mutex; |
1da177e4 LT |
213 | __u8 port_running; |
214 | __u8 command_finished; | |
80359a9c AC |
215 | wait_queue_head_t wait_command; /* for handling sleeping whilst |
216 | waiting for a command to | |
217 | finish */ | |
1da177e4 LT |
218 | __u8 result_buffer[64]; |
219 | }; | |
220 | ||
221 | ||
222 | #define THROTTLED 0x01 | |
223 | #define ACTUALLY_THROTTLED 0x02 | |
224 | ||
225 | static int urb_pool_size = 8; | |
226 | ||
227 | struct whiteheat_urb_wrap { | |
228 | struct list_head list; | |
229 | struct urb *urb; | |
230 | }; | |
231 | ||
232 | struct whiteheat_private { | |
233 | spinlock_t lock; | |
234 | __u8 flags; | |
a5b6f60c | 235 | __u8 mcr; /* FIXME: no locking on mcr */ |
1da177e4 LT |
236 | struct list_head rx_urbs_free; |
237 | struct list_head rx_urbs_submitted; | |
238 | struct list_head rx_urb_q; | |
239 | struct work_struct rx_work; | |
c4028958 | 240 | struct usb_serial_port *port; |
1da177e4 LT |
241 | struct list_head tx_urbs_free; |
242 | struct list_head tx_urbs_submitted; | |
08a2b3b6 | 243 | struct mutex deathwarrant; |
1da177e4 LT |
244 | }; |
245 | ||
246 | ||
247 | /* local function prototypes */ | |
248 | static int start_command_port(struct usb_serial *serial); | |
249 | static void stop_command_port(struct usb_serial *serial); | |
7d12e780 DH |
250 | static void command_port_write_callback(struct urb *urb); |
251 | static void command_port_read_callback(struct urb *urb); | |
1da177e4 LT |
252 | |
253 | static int start_port_read(struct usb_serial_port *port); | |
80359a9c AC |
254 | static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb, |
255 | struct list_head *head); | |
1da177e4 | 256 | static struct list_head *list_first(struct list_head *head); |
c4028958 | 257 | static void rx_data_softint(struct work_struct *work); |
1da177e4 | 258 | |
80359a9c AC |
259 | static int firm_send_command(struct usb_serial_port *port, __u8 command, |
260 | __u8 *data, __u8 datasize); | |
1da177e4 LT |
261 | static int firm_open(struct usb_serial_port *port); |
262 | static int firm_close(struct usb_serial_port *port); | |
95da310e | 263 | static int firm_setup_port(struct tty_struct *tty); |
1da177e4 LT |
264 | static int firm_set_rts(struct usb_serial_port *port, __u8 onoff); |
265 | static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff); | |
266 | static int firm_set_break(struct usb_serial_port *port, __u8 onoff); | |
267 | static int firm_purge(struct usb_serial_port *port, __u8 rxtx); | |
268 | static int firm_get_dtr_rts(struct usb_serial_port *port); | |
269 | static int firm_report_tx_done(struct usb_serial_port *port); | |
270 | ||
271 | ||
272 | #define COMMAND_PORT 4 | |
273 | #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */ | |
274 | #define COMMAND_TIMEOUT_MS 2000 | |
275 | #define CLOSING_DELAY (30 * HZ) | |
276 | ||
277 | ||
278 | /***************************************************************************** | |
279 | * Connect Tech's White Heat prerenumeration driver functions | |
280 | *****************************************************************************/ | |
281 | ||
282 | /* steps to download the firmware to the WhiteHEAT device: | |
283 | - hold the reset (by writing to the reset bit of the CPUCS register) | |
284 | - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD | |
285 | - release the reset (by writing to the CPUCS register) | |
286 | - download the WH.HEX file for all addresses greater than 0x1b3f using | |
287 | VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD | |
288 | - hold the reset | |
289 | - download the WH.HEX file for all addresses less than 0x1b40 using | |
290 | VENDOR_REQUEST_ANCHOR_LOAD | |
291 | - release the reset | |
292 | - device renumerated itself and comes up as new device id with all | |
293 | firmware download completed. | |
294 | */ | |
80359a9c AC |
295 | static int whiteheat_firmware_download(struct usb_serial *serial, |
296 | const struct usb_device_id *id) | |
1da177e4 | 297 | { |
ec6752f5 DW |
298 | int response, ret = -ENOENT; |
299 | const struct firmware *loader_fw = NULL, *firmware_fw = NULL; | |
300 | const struct ihex_binrec *record; | |
301 | ||
441b62c1 | 302 | dbg("%s", __func__); |
ec6752f5 DW |
303 | |
304 | if (request_ihex_firmware(&firmware_fw, "whiteheat.fw", | |
305 | &serial->dev->dev)) { | |
194343d9 GKH |
306 | dev_err(&serial->dev->dev, |
307 | "%s - request \"whiteheat.fw\" failed\n", __func__); | |
ec6752f5 DW |
308 | goto out; |
309 | } | |
310 | if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw", | |
311 | &serial->dev->dev)) { | |
194343d9 GKH |
312 | dev_err(&serial->dev->dev, |
313 | "%s - request \"whiteheat_loader.fw\" failed\n", | |
314 | __func__); | |
ec6752f5 DW |
315 | goto out; |
316 | } | |
317 | ret = 0; | |
1da177e4 LT |
318 | response = ezusb_set_reset (serial, 1); |
319 | ||
ec6752f5 DW |
320 | record = (const struct ihex_binrec *)loader_fw->data; |
321 | while (record) { | |
322 | response = ezusb_writememory (serial, be32_to_cpu(record->addr), | |
323 | (unsigned char *)record->data, | |
324 | be16_to_cpu(record->len), 0xa0); | |
1da177e4 | 325 | if (response < 0) { |
194343d9 GKH |
326 | dev_err(&serial->dev->dev, "%s - ezusb_writememory " |
327 | "failed for loader (%d %04X %p %d)\n", | |
328 | __func__, response, be32_to_cpu(record->addr), | |
329 | record->data, be16_to_cpu(record->len)); | |
1da177e4 LT |
330 | break; |
331 | } | |
ec6752f5 | 332 | record = ihex_next_binrec(record); |
1da177e4 LT |
333 | } |
334 | ||
80359a9c | 335 | response = ezusb_set_reset(serial, 0); |
1da177e4 | 336 | |
ec6752f5 DW |
337 | record = (const struct ihex_binrec *)firmware_fw->data; |
338 | while (record && be32_to_cpu(record->addr) < 0x1b40) | |
339 | record = ihex_next_binrec(record); | |
340 | while (record) { | |
341 | response = ezusb_writememory (serial, be32_to_cpu(record->addr), | |
342 | (unsigned char *)record->data, | |
343 | be16_to_cpu(record->len), 0xa3); | |
1da177e4 | 344 | if (response < 0) { |
194343d9 GKH |
345 | dev_err(&serial->dev->dev, "%s - ezusb_writememory " |
346 | "failed for first firmware step " | |
347 | "(%d %04X %p %d)\n", __func__, response, | |
348 | be32_to_cpu(record->addr), record->data, | |
349 | be16_to_cpu(record->len)); | |
1da177e4 LT |
350 | break; |
351 | } | |
352 | ++record; | |
353 | } | |
80359a9c AC |
354 | |
355 | response = ezusb_set_reset(serial, 1); | |
1da177e4 | 356 | |
ec6752f5 DW |
357 | record = (const struct ihex_binrec *)firmware_fw->data; |
358 | while (record && be32_to_cpu(record->addr) < 0x1b40) { | |
359 | response = ezusb_writememory (serial, be32_to_cpu(record->addr), | |
360 | (unsigned char *)record->data, | |
361 | be16_to_cpu(record->len), 0xa0); | |
1da177e4 | 362 | if (response < 0) { |
194343d9 GKH |
363 | dev_err(&serial->dev->dev, "%s - ezusb_writememory " |
364 | "failed for second firmware step " | |
365 | "(%d %04X %p %d)\n", __func__, response, | |
366 | be32_to_cpu(record->addr), record->data, | |
367 | be16_to_cpu(record->len)); | |
1da177e4 LT |
368 | break; |
369 | } | |
370 | ++record; | |
371 | } | |
ec6752f5 | 372 | ret = 0; |
1da177e4 | 373 | response = ezusb_set_reset (serial, 0); |
ec6752f5 DW |
374 | out: |
375 | release_firmware(loader_fw); | |
376 | release_firmware(firmware_fw); | |
377 | return ret; | |
1da177e4 LT |
378 | } |
379 | ||
380 | ||
80359a9c | 381 | static int whiteheat_firmware_attach(struct usb_serial *serial) |
1da177e4 LT |
382 | { |
383 | /* We want this device to fail to have a driver assigned to it */ | |
384 | return 1; | |
385 | } | |
386 | ||
387 | ||
388 | /***************************************************************************** | |
389 | * Connect Tech's White Heat serial driver functions | |
390 | *****************************************************************************/ | |
80359a9c | 391 | static int whiteheat_attach(struct usb_serial *serial) |
1da177e4 LT |
392 | { |
393 | struct usb_serial_port *command_port; | |
394 | struct whiteheat_command_private *command_info; | |
395 | struct usb_serial_port *port; | |
396 | struct whiteheat_private *info; | |
397 | struct whiteheat_hw_info *hw_info; | |
398 | int pipe; | |
399 | int ret; | |
400 | int alen; | |
401 | __u8 *command; | |
402 | __u8 *result; | |
403 | int i; | |
404 | int j; | |
405 | struct urb *urb; | |
406 | int buf_size; | |
407 | struct whiteheat_urb_wrap *wrap; | |
408 | struct list_head *tmp; | |
409 | ||
410 | command_port = serial->port[COMMAND_PORT]; | |
411 | ||
80359a9c AC |
412 | pipe = usb_sndbulkpipe(serial->dev, |
413 | command_port->bulk_out_endpointAddress); | |
1da177e4 LT |
414 | command = kmalloc(2, GFP_KERNEL); |
415 | if (!command) | |
416 | goto no_command_buffer; | |
417 | command[0] = WHITEHEAT_GET_HW_INFO; | |
418 | command[1] = 0; | |
80359a9c | 419 | |
1da177e4 LT |
420 | result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL); |
421 | if (!result) | |
422 | goto no_result_buffer; | |
423 | /* | |
424 | * When the module is reloaded the firmware is still there and | |
425 | * the endpoints are still in the usb core unchanged. This is the | |
80359a9c AC |
426 | * unlinking bug in disguise. Same for the call below. |
427 | */ | |
1da177e4 | 428 | usb_clear_halt(serial->dev, pipe); |
80359a9c AC |
429 | ret = usb_bulk_msg(serial->dev, pipe, command, 2, |
430 | &alen, COMMAND_TIMEOUT_MS); | |
1da177e4 | 431 | if (ret) { |
194343d9 GKH |
432 | dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n", |
433 | serial->type->description, ret); | |
1da177e4 | 434 | goto no_firmware; |
09fd6bc8 | 435 | } else if (alen != 2) { |
194343d9 GKH |
436 | dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n", |
437 | serial->type->description, alen); | |
1da177e4 LT |
438 | goto no_firmware; |
439 | } | |
440 | ||
80359a9c AC |
441 | pipe = usb_rcvbulkpipe(serial->dev, |
442 | command_port->bulk_in_endpointAddress); | |
1da177e4 LT |
443 | /* See the comment on the usb_clear_halt() above */ |
444 | usb_clear_halt(serial->dev, pipe); | |
80359a9c AC |
445 | ret = usb_bulk_msg(serial->dev, pipe, result, |
446 | sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS); | |
1da177e4 | 447 | if (ret) { |
194343d9 GKH |
448 | dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n", |
449 | serial->type->description, ret); | |
1da177e4 | 450 | goto no_firmware; |
09fd6bc8 | 451 | } else if (alen != sizeof(*hw_info) + 1) { |
194343d9 GKH |
452 | dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n", |
453 | serial->type->description, alen); | |
1da177e4 LT |
454 | goto no_firmware; |
455 | } else if (result[0] != command[0]) { | |
194343d9 GKH |
456 | dev_err(&serial->dev->dev, "%s: Command failed [%d]\n", |
457 | serial->type->description, result[0]); | |
1da177e4 LT |
458 | goto no_firmware; |
459 | } | |
460 | ||
461 | hw_info = (struct whiteheat_hw_info *)&result[1]; | |
462 | ||
c197a8db GKH |
463 | dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n", |
464 | serial->type->description, DRIVER_VERSION, | |
465 | hw_info->sw_major_rev, hw_info->sw_minor_rev); | |
1da177e4 LT |
466 | |
467 | for (i = 0; i < serial->num_ports; i++) { | |
468 | port = serial->port[i]; | |
469 | ||
5cbded58 | 470 | info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL); |
1da177e4 | 471 | if (info == NULL) { |
194343d9 GKH |
472 | dev_err(&port->dev, |
473 | "%s: Out of memory for port structures\n", | |
474 | serial->type->description); | |
1da177e4 LT |
475 | goto no_private; |
476 | } | |
477 | ||
478 | spin_lock_init(&info->lock); | |
08a2b3b6 | 479 | mutex_init(&info->deathwarrant); |
1da177e4 LT |
480 | info->flags = 0; |
481 | info->mcr = 0; | |
c4028958 DH |
482 | INIT_WORK(&info->rx_work, rx_data_softint); |
483 | info->port = port; | |
1da177e4 LT |
484 | |
485 | INIT_LIST_HEAD(&info->rx_urbs_free); | |
486 | INIT_LIST_HEAD(&info->rx_urbs_submitted); | |
487 | INIT_LIST_HEAD(&info->rx_urb_q); | |
488 | INIT_LIST_HEAD(&info->tx_urbs_free); | |
489 | INIT_LIST_HEAD(&info->tx_urbs_submitted); | |
490 | ||
491 | for (j = 0; j < urb_pool_size; j++) { | |
492 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
493 | if (!urb) { | |
194343d9 | 494 | dev_err(&port->dev, "No free urbs available\n"); |
1da177e4 LT |
495 | goto no_rx_urb; |
496 | } | |
497 | buf_size = port->read_urb->transfer_buffer_length; | |
498 | urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL); | |
499 | if (!urb->transfer_buffer) { | |
194343d9 GKH |
500 | dev_err(&port->dev, |
501 | "Couldn't allocate urb buffer\n"); | |
1da177e4 LT |
502 | goto no_rx_buf; |
503 | } | |
504 | wrap = kmalloc(sizeof(*wrap), GFP_KERNEL); | |
505 | if (!wrap) { | |
194343d9 GKH |
506 | dev_err(&port->dev, |
507 | "Couldn't allocate urb wrapper\n"); | |
1da177e4 LT |
508 | goto no_rx_wrap; |
509 | } | |
510 | usb_fill_bulk_urb(urb, serial->dev, | |
511 | usb_rcvbulkpipe(serial->dev, | |
512 | port->bulk_in_endpointAddress), | |
513 | urb->transfer_buffer, buf_size, | |
514 | whiteheat_read_callback, port); | |
515 | wrap->urb = urb; | |
516 | list_add(&wrap->list, &info->rx_urbs_free); | |
517 | ||
518 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
519 | if (!urb) { | |
194343d9 | 520 | dev_err(&port->dev, "No free urbs available\n"); |
1da177e4 LT |
521 | goto no_tx_urb; |
522 | } | |
523 | buf_size = port->write_urb->transfer_buffer_length; | |
524 | urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL); | |
525 | if (!urb->transfer_buffer) { | |
194343d9 GKH |
526 | dev_err(&port->dev, |
527 | "Couldn't allocate urb buffer\n"); | |
1da177e4 LT |
528 | goto no_tx_buf; |
529 | } | |
530 | wrap = kmalloc(sizeof(*wrap), GFP_KERNEL); | |
531 | if (!wrap) { | |
194343d9 GKH |
532 | dev_err(&port->dev, |
533 | "Couldn't allocate urb wrapper\n"); | |
1da177e4 LT |
534 | goto no_tx_wrap; |
535 | } | |
536 | usb_fill_bulk_urb(urb, serial->dev, | |
537 | usb_sndbulkpipe(serial->dev, | |
538 | port->bulk_out_endpointAddress), | |
539 | urb->transfer_buffer, buf_size, | |
540 | whiteheat_write_callback, port); | |
541 | wrap->urb = urb; | |
542 | list_add(&wrap->list, &info->tx_urbs_free); | |
543 | } | |
544 | ||
545 | usb_set_serial_port_data(port, info); | |
546 | } | |
547 | ||
80359a9c AC |
548 | command_info = kmalloc(sizeof(struct whiteheat_command_private), |
549 | GFP_KERNEL); | |
1da177e4 | 550 | if (command_info == NULL) { |
194343d9 GKH |
551 | dev_err(&serial->dev->dev, |
552 | "%s: Out of memory for port structures\n", | |
553 | serial->type->description); | |
1da177e4 LT |
554 | goto no_command_private; |
555 | } | |
556 | ||
08a2b3b6 | 557 | mutex_init(&command_info->mutex); |
1da177e4 LT |
558 | command_info->port_running = 0; |
559 | init_waitqueue_head(&command_info->wait_command); | |
560 | usb_set_serial_port_data(command_port, command_info); | |
561 | command_port->write_urb->complete = command_port_write_callback; | |
562 | command_port->read_urb->complete = command_port_read_callback; | |
563 | kfree(result); | |
564 | kfree(command); | |
565 | ||
566 | return 0; | |
567 | ||
568 | no_firmware: | |
569 | /* Firmware likely not running */ | |
194343d9 GKH |
570 | dev_err(&serial->dev->dev, |
571 | "%s: Unable to retrieve firmware version, try replugging\n", | |
572 | serial->type->description); | |
573 | dev_err(&serial->dev->dev, | |
574 | "%s: If the firmware is not running (status led not blinking)\n", | |
575 | serial->type->description); | |
576 | dev_err(&serial->dev->dev, | |
577 | "%s: please contact support@connecttech.com\n", | |
578 | serial->type->description); | |
67ca0284 | 579 | kfree(result); |
1da177e4 LT |
580 | return -ENODEV; |
581 | ||
582 | no_command_private: | |
583 | for (i = serial->num_ports - 1; i >= 0; i--) { | |
584 | port = serial->port[i]; | |
585 | info = usb_get_serial_port_data(port); | |
586 | for (j = urb_pool_size - 1; j >= 0; j--) { | |
587 | tmp = list_first(&info->tx_urbs_free); | |
588 | list_del(tmp); | |
589 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
590 | urb = wrap->urb; | |
591 | kfree(wrap); | |
592 | no_tx_wrap: | |
593 | kfree(urb->transfer_buffer); | |
594 | no_tx_buf: | |
595 | usb_free_urb(urb); | |
596 | no_tx_urb: | |
597 | tmp = list_first(&info->rx_urbs_free); | |
598 | list_del(tmp); | |
599 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
600 | urb = wrap->urb; | |
601 | kfree(wrap); | |
602 | no_rx_wrap: | |
603 | kfree(urb->transfer_buffer); | |
604 | no_rx_buf: | |
605 | usb_free_urb(urb); | |
606 | no_rx_urb: | |
607 | ; | |
608 | } | |
609 | kfree(info); | |
610 | no_private: | |
611 | ; | |
612 | } | |
613 | kfree(result); | |
614 | no_result_buffer: | |
615 | kfree(command); | |
616 | no_command_buffer: | |
617 | return -ENOMEM; | |
618 | } | |
619 | ||
620 | ||
80359a9c | 621 | static void whiteheat_shutdown(struct usb_serial *serial) |
1da177e4 LT |
622 | { |
623 | struct usb_serial_port *command_port; | |
624 | struct usb_serial_port *port; | |
625 | struct whiteheat_private *info; | |
626 | struct whiteheat_urb_wrap *wrap; | |
627 | struct urb *urb; | |
628 | struct list_head *tmp; | |
629 | struct list_head *tmp2; | |
630 | int i; | |
631 | ||
441b62c1 | 632 | dbg("%s", __func__); |
1da177e4 LT |
633 | |
634 | /* free up our private data for our command port */ | |
635 | command_port = serial->port[COMMAND_PORT]; | |
80359a9c | 636 | kfree(usb_get_serial_port_data(command_port)); |
1da177e4 LT |
637 | |
638 | for (i = 0; i < serial->num_ports; i++) { | |
639 | port = serial->port[i]; | |
640 | info = usb_get_serial_port_data(port); | |
641 | list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) { | |
642 | list_del(tmp); | |
643 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
644 | urb = wrap->urb; | |
645 | kfree(wrap); | |
646 | kfree(urb->transfer_buffer); | |
647 | usb_free_urb(urb); | |
648 | } | |
649 | list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) { | |
650 | list_del(tmp); | |
651 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
652 | urb = wrap->urb; | |
653 | kfree(wrap); | |
654 | kfree(urb->transfer_buffer); | |
655 | usb_free_urb(urb); | |
656 | } | |
657 | kfree(info); | |
658 | } | |
659 | ||
660 | return; | |
661 | } | |
662 | ||
80359a9c | 663 | static int whiteheat_open(struct tty_struct *tty, |
95da310e | 664 | struct usb_serial_port *port, struct file *filp) |
1da177e4 LT |
665 | { |
666 | int retval = 0; | |
1da177e4 | 667 | |
441b62c1 | 668 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
669 | |
670 | retval = start_command_port(port->serial); | |
671 | if (retval) | |
672 | goto exit; | |
673 | ||
95da310e AC |
674 | if (tty) |
675 | tty->low_latency = 1; | |
1da177e4 LT |
676 | |
677 | /* send an open port command */ | |
678 | retval = firm_open(port); | |
679 | if (retval) { | |
680 | stop_command_port(port->serial); | |
681 | goto exit; | |
682 | } | |
683 | ||
684 | retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX); | |
685 | if (retval) { | |
686 | firm_close(port); | |
687 | stop_command_port(port->serial); | |
688 | goto exit; | |
689 | } | |
690 | ||
72e27412 AC |
691 | if (tty) |
692 | firm_setup_port(tty); | |
1da177e4 LT |
693 | |
694 | /* Work around HCD bugs */ | |
695 | usb_clear_halt(port->serial->dev, port->read_urb->pipe); | |
696 | usb_clear_halt(port->serial->dev, port->write_urb->pipe); | |
697 | ||
698 | /* Start reading from the device */ | |
699 | retval = start_port_read(port); | |
700 | if (retval) { | |
194343d9 GKH |
701 | dev_err(&port->dev, |
702 | "%s - failed submitting read urb, error %d\n", | |
703 | __func__, retval); | |
1da177e4 LT |
704 | firm_close(port); |
705 | stop_command_port(port->serial); | |
706 | goto exit; | |
707 | } | |
708 | ||
709 | exit: | |
441b62c1 | 710 | dbg("%s - exit, retval = %d", __func__, retval); |
1da177e4 LT |
711 | return retval; |
712 | } | |
713 | ||
714 | ||
95da310e | 715 | static void whiteheat_close(struct tty_struct *tty, |
80359a9c | 716 | struct usb_serial_port *port, struct file *filp) |
1da177e4 LT |
717 | { |
718 | struct whiteheat_private *info = usb_get_serial_port_data(port); | |
719 | struct whiteheat_urb_wrap *wrap; | |
720 | struct urb *urb; | |
721 | struct list_head *tmp; | |
722 | struct list_head *tmp2; | |
1da177e4 | 723 | |
441b62c1 | 724 | dbg("%s - port %d", __func__, port->number); |
e33fe4d8 ON |
725 | |
726 | mutex_lock(&port->serial->disc_mutex); | |
1da177e4 | 727 | /* filp is NULL when called from usb_serial_disconnect */ |
e33fe4d8 ON |
728 | if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) { |
729 | mutex_unlock(&port->serial->disc_mutex); | |
1da177e4 LT |
730 | return; |
731 | } | |
e33fe4d8 | 732 | mutex_unlock(&port->serial->disc_mutex); |
1da177e4 | 733 | |
95da310e | 734 | tty->closing = 1; |
1da177e4 LT |
735 | |
736 | /* | |
737 | * Not currently in use; tty_wait_until_sent() calls | |
738 | * serial_chars_in_buffer() which deadlocks on the second semaphore | |
739 | * acquisition. This should be fixed at some point. Greg's been | |
740 | * notified. | |
741 | if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) { | |
95da310e | 742 | tty_wait_until_sent(tty, CLOSING_DELAY); |
1da177e4 LT |
743 | } |
744 | */ | |
745 | ||
95da310e AC |
746 | tty_driver_flush_buffer(tty); |
747 | tty_ldisc_flush(tty); | |
1da177e4 LT |
748 | |
749 | firm_report_tx_done(port); | |
750 | ||
751 | firm_close(port); | |
752 | ||
753 | /* shutdown our bulk reads and writes */ | |
08a2b3b6 ON |
754 | mutex_lock(&info->deathwarrant); |
755 | spin_lock_irq(&info->lock); | |
1da177e4 LT |
756 | list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) { |
757 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
758 | urb = wrap->urb; | |
08a2b3b6 ON |
759 | list_del(tmp); |
760 | spin_unlock_irq(&info->lock); | |
1da177e4 | 761 | usb_kill_urb(urb); |
08a2b3b6 ON |
762 | spin_lock_irq(&info->lock); |
763 | list_add(tmp, &info->rx_urbs_free); | |
1da177e4 | 764 | } |
179e0917 AM |
765 | list_for_each_safe(tmp, tmp2, &info->rx_urb_q) |
766 | list_move(tmp, &info->rx_urbs_free); | |
1da177e4 LT |
767 | list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) { |
768 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
769 | urb = wrap->urb; | |
08a2b3b6 ON |
770 | list_del(tmp); |
771 | spin_unlock_irq(&info->lock); | |
1da177e4 | 772 | usb_kill_urb(urb); |
08a2b3b6 ON |
773 | spin_lock_irq(&info->lock); |
774 | list_add(tmp, &info->tx_urbs_free); | |
1da177e4 | 775 | } |
08a2b3b6 ON |
776 | spin_unlock_irq(&info->lock); |
777 | mutex_unlock(&info->deathwarrant); | |
1da177e4 LT |
778 | |
779 | stop_command_port(port->serial); | |
780 | ||
95da310e | 781 | tty->closing = 0; |
1da177e4 LT |
782 | } |
783 | ||
784 | ||
95da310e AC |
785 | static int whiteheat_write(struct tty_struct *tty, |
786 | struct usb_serial_port *port, const unsigned char *buf, int count) | |
1da177e4 LT |
787 | { |
788 | struct usb_serial *serial = port->serial; | |
789 | struct whiteheat_private *info = usb_get_serial_port_data(port); | |
790 | struct whiteheat_urb_wrap *wrap; | |
791 | struct urb *urb; | |
792 | int result; | |
793 | int bytes; | |
794 | int sent = 0; | |
795 | unsigned long flags; | |
796 | struct list_head *tmp; | |
797 | ||
441b62c1 | 798 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
799 | |
800 | if (count == 0) { | |
441b62c1 | 801 | dbg("%s - write request of 0 bytes", __func__); |
1da177e4 LT |
802 | return (0); |
803 | } | |
804 | ||
805 | while (count) { | |
806 | spin_lock_irqsave(&info->lock, flags); | |
807 | if (list_empty(&info->tx_urbs_free)) { | |
808 | spin_unlock_irqrestore(&info->lock, flags); | |
809 | break; | |
810 | } | |
811 | tmp = list_first(&info->tx_urbs_free); | |
812 | list_del(tmp); | |
813 | spin_unlock_irqrestore(&info->lock, flags); | |
814 | ||
815 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
816 | urb = wrap->urb; | |
80359a9c AC |
817 | bytes = (count > port->bulk_out_size) ? |
818 | port->bulk_out_size : count; | |
819 | memcpy(urb->transfer_buffer, buf + sent, bytes); | |
1da177e4 | 820 | |
80359a9c AC |
821 | usb_serial_debug_data(debug, &port->dev, |
822 | __func__, bytes, urb->transfer_buffer); | |
1da177e4 LT |
823 | |
824 | urb->dev = serial->dev; | |
825 | urb->transfer_buffer_length = bytes; | |
826 | result = usb_submit_urb(urb, GFP_ATOMIC); | |
827 | if (result) { | |
194343d9 GKH |
828 | dev_err(&port->dev, |
829 | "%s - failed submitting write urb, error %d\n", | |
830 | __func__, result); | |
1da177e4 LT |
831 | sent = result; |
832 | spin_lock_irqsave(&info->lock, flags); | |
833 | list_add(tmp, &info->tx_urbs_free); | |
834 | spin_unlock_irqrestore(&info->lock, flags); | |
835 | break; | |
836 | } else { | |
837 | sent += bytes; | |
838 | count -= bytes; | |
839 | spin_lock_irqsave(&info->lock, flags); | |
840 | list_add(tmp, &info->tx_urbs_submitted); | |
841 | spin_unlock_irqrestore(&info->lock, flags); | |
842 | } | |
843 | } | |
844 | ||
845 | return sent; | |
846 | } | |
847 | ||
95da310e | 848 | static int whiteheat_write_room(struct tty_struct *tty) |
1da177e4 | 849 | { |
95da310e | 850 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
851 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
852 | struct list_head *tmp; | |
853 | int room = 0; | |
854 | unsigned long flags; | |
855 | ||
441b62c1 | 856 | dbg("%s - port %d", __func__, port->number); |
80359a9c | 857 | |
1da177e4 LT |
858 | spin_lock_irqsave(&info->lock, flags); |
859 | list_for_each(tmp, &info->tx_urbs_free) | |
860 | room++; | |
861 | spin_unlock_irqrestore(&info->lock, flags); | |
862 | room *= port->bulk_out_size; | |
863 | ||
441b62c1 | 864 | dbg("%s - returns %d", __func__, room); |
1da177e4 LT |
865 | return (room); |
866 | } | |
867 | ||
80359a9c | 868 | static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file) |
1da177e4 | 869 | { |
95da310e | 870 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
871 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
872 | unsigned int modem_signals = 0; | |
873 | ||
441b62c1 | 874 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
875 | |
876 | firm_get_dtr_rts(port); | |
877 | if (info->mcr & UART_MCR_DTR) | |
878 | modem_signals |= TIOCM_DTR; | |
879 | if (info->mcr & UART_MCR_RTS) | |
880 | modem_signals |= TIOCM_RTS; | |
881 | ||
882 | return modem_signals; | |
883 | } | |
884 | ||
80359a9c | 885 | static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file, |
1da177e4 LT |
886 | unsigned int set, unsigned int clear) |
887 | { | |
95da310e | 888 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
889 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
890 | ||
441b62c1 | 891 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
892 | |
893 | if (set & TIOCM_RTS) | |
894 | info->mcr |= UART_MCR_RTS; | |
895 | if (set & TIOCM_DTR) | |
896 | info->mcr |= UART_MCR_DTR; | |
897 | ||
898 | if (clear & TIOCM_RTS) | |
899 | info->mcr &= ~UART_MCR_RTS; | |
900 | if (clear & TIOCM_DTR) | |
901 | info->mcr &= ~UART_MCR_DTR; | |
902 | ||
903 | firm_set_dtr(port, info->mcr & UART_MCR_DTR); | |
904 | firm_set_rts(port, info->mcr & UART_MCR_RTS); | |
905 | return 0; | |
906 | } | |
907 | ||
908 | ||
80359a9c AC |
909 | static int whiteheat_ioctl(struct tty_struct *tty, struct file *file, |
910 | unsigned int cmd, unsigned long arg) | |
1da177e4 | 911 | { |
95da310e | 912 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
913 | struct serial_struct serstruct; |
914 | void __user *user_arg = (void __user *)arg; | |
915 | ||
441b62c1 | 916 | dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd); |
1da177e4 LT |
917 | |
918 | switch (cmd) { | |
80359a9c AC |
919 | case TIOCGSERIAL: |
920 | memset(&serstruct, 0, sizeof(serstruct)); | |
921 | serstruct.type = PORT_16654; | |
922 | serstruct.line = port->serial->minor; | |
923 | serstruct.port = port->number; | |
924 | serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; | |
925 | serstruct.xmit_fifo_size = port->bulk_out_size; | |
926 | serstruct.custom_divisor = 0; | |
927 | serstruct.baud_base = 460800; | |
928 | serstruct.close_delay = CLOSING_DELAY; | |
929 | serstruct.closing_wait = CLOSING_DELAY; | |
930 | ||
931 | if (copy_to_user(user_arg, &serstruct, sizeof(serstruct))) | |
932 | return -EFAULT; | |
933 | break; | |
934 | default: | |
935 | break; | |
1da177e4 LT |
936 | } |
937 | ||
938 | return -ENOIOCTLCMD; | |
939 | } | |
940 | ||
941 | ||
80359a9c AC |
942 | static void whiteheat_set_termios(struct tty_struct *tty, |
943 | struct usb_serial_port *port, struct ktermios *old_termios) | |
1da177e4 | 944 | { |
95da310e | 945 | firm_setup_port(tty); |
1da177e4 LT |
946 | } |
947 | ||
80359a9c AC |
948 | static void whiteheat_break_ctl(struct tty_struct *tty, int break_state) |
949 | { | |
95da310e | 950 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
951 | firm_set_break(port, break_state); |
952 | } | |
953 | ||
954 | ||
95da310e | 955 | static int whiteheat_chars_in_buffer(struct tty_struct *tty) |
1da177e4 | 956 | { |
95da310e | 957 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
958 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
959 | struct list_head *tmp; | |
960 | struct whiteheat_urb_wrap *wrap; | |
961 | int chars = 0; | |
962 | unsigned long flags; | |
963 | ||
441b62c1 | 964 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
965 | |
966 | spin_lock_irqsave(&info->lock, flags); | |
967 | list_for_each(tmp, &info->tx_urbs_submitted) { | |
968 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
969 | chars += wrap->urb->transfer_buffer_length; | |
970 | } | |
971 | spin_unlock_irqrestore(&info->lock, flags); | |
972 | ||
80359a9c | 973 | dbg("%s - returns %d", __func__, chars); |
08a2b3b6 | 974 | return chars; |
1da177e4 LT |
975 | } |
976 | ||
977 | ||
80359a9c | 978 | static void whiteheat_throttle(struct tty_struct *tty) |
1da177e4 | 979 | { |
95da310e | 980 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
981 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
982 | unsigned long flags; | |
983 | ||
441b62c1 | 984 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
985 | |
986 | spin_lock_irqsave(&info->lock, flags); | |
987 | info->flags |= THROTTLED; | |
988 | spin_unlock_irqrestore(&info->lock, flags); | |
989 | ||
990 | return; | |
991 | } | |
992 | ||
993 | ||
80359a9c | 994 | static void whiteheat_unthrottle(struct tty_struct *tty) |
1da177e4 | 995 | { |
95da310e | 996 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
997 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
998 | int actually_throttled; | |
999 | unsigned long flags; | |
1000 | ||
441b62c1 | 1001 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
1002 | |
1003 | spin_lock_irqsave(&info->lock, flags); | |
1004 | actually_throttled = info->flags & ACTUALLY_THROTTLED; | |
1005 | info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED); | |
1006 | spin_unlock_irqrestore(&info->lock, flags); | |
1007 | ||
1008 | if (actually_throttled) | |
c4028958 | 1009 | rx_data_softint(&info->rx_work); |
1da177e4 LT |
1010 | |
1011 | return; | |
1012 | } | |
1013 | ||
1014 | ||
1015 | /***************************************************************************** | |
1016 | * Connect Tech's White Heat callback routines | |
1017 | *****************************************************************************/ | |
08a2b3b6 | 1018 | static void command_port_write_callback(struct urb *urb) |
1da177e4 | 1019 | { |
05400013 GKH |
1020 | int status = urb->status; |
1021 | ||
441b62c1 | 1022 | dbg("%s", __func__); |
1da177e4 | 1023 | |
05400013 GKH |
1024 | if (status) { |
1025 | dbg("nonzero urb status: %d", status); | |
1da177e4 LT |
1026 | return; |
1027 | } | |
1028 | } | |
1029 | ||
1030 | ||
08a2b3b6 | 1031 | static void command_port_read_callback(struct urb *urb) |
1da177e4 | 1032 | { |
cdc97792 | 1033 | struct usb_serial_port *command_port = urb->context; |
1da177e4 | 1034 | struct whiteheat_command_private *command_info; |
05400013 | 1035 | int status = urb->status; |
1da177e4 LT |
1036 | unsigned char *data = urb->transfer_buffer; |
1037 | int result; | |
1da177e4 | 1038 | |
441b62c1 | 1039 | dbg("%s", __func__); |
1da177e4 | 1040 | |
08a2b3b6 ON |
1041 | command_info = usb_get_serial_port_data(command_port); |
1042 | if (!command_info) { | |
80359a9c | 1043 | dbg("%s - command_info is NULL, exiting.", __func__); |
08a2b3b6 ON |
1044 | return; |
1045 | } | |
05400013 | 1046 | if (status) { |
441b62c1 | 1047 | dbg("%s - nonzero urb status: %d", __func__, status); |
05400013 | 1048 | if (status != -ENOENT) |
08a2b3b6 ON |
1049 | command_info->command_finished = WHITEHEAT_CMD_FAILURE; |
1050 | wake_up(&command_info->wait_command); | |
1da177e4 LT |
1051 | return; |
1052 | } | |
1053 | ||
80359a9c AC |
1054 | usb_serial_debug_data(debug, &command_port->dev, |
1055 | __func__, urb->actual_length, data); | |
1da177e4 | 1056 | |
1da177e4 LT |
1057 | if (data[0] == WHITEHEAT_CMD_COMPLETE) { |
1058 | command_info->command_finished = WHITEHEAT_CMD_COMPLETE; | |
08a2b3b6 | 1059 | wake_up(&command_info->wait_command); |
1da177e4 LT |
1060 | } else if (data[0] == WHITEHEAT_CMD_FAILURE) { |
1061 | command_info->command_finished = WHITEHEAT_CMD_FAILURE; | |
08a2b3b6 | 1062 | wake_up(&command_info->wait_command); |
1da177e4 | 1063 | } else if (data[0] == WHITEHEAT_EVENT) { |
80359a9c AC |
1064 | /* These are unsolicited reports from the firmware, hence no |
1065 | waiting command to wakeup */ | |
441b62c1 | 1066 | dbg("%s - event received", __func__); |
1da177e4 | 1067 | } else if (data[0] == WHITEHEAT_GET_DTR_RTS) { |
80359a9c AC |
1068 | memcpy(command_info->result_buffer, &data[1], |
1069 | urb->actual_length - 1); | |
1da177e4 | 1070 | command_info->command_finished = WHITEHEAT_CMD_COMPLETE; |
08a2b3b6 | 1071 | wake_up(&command_info->wait_command); |
80359a9c | 1072 | } else |
441b62c1 | 1073 | dbg("%s - bad reply from firmware", __func__); |
80359a9c | 1074 | |
1da177e4 LT |
1075 | /* Continue trying to always read */ |
1076 | command_port->read_urb->dev = command_port->serial->dev; | |
1077 | result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC); | |
1da177e4 | 1078 | if (result) |
80359a9c AC |
1079 | dbg("%s - failed resubmitting read urb, error %d", |
1080 | __func__, result); | |
1da177e4 LT |
1081 | } |
1082 | ||
1083 | ||
7d12e780 | 1084 | static void whiteheat_read_callback(struct urb *urb) |
1da177e4 | 1085 | { |
cdc97792 | 1086 | struct usb_serial_port *port = urb->context; |
1da177e4 LT |
1087 | struct whiteheat_urb_wrap *wrap; |
1088 | unsigned char *data = urb->transfer_buffer; | |
1089 | struct whiteheat_private *info = usb_get_serial_port_data(port); | |
05400013 | 1090 | int status = urb->status; |
1da177e4 | 1091 | |
441b62c1 | 1092 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
1093 | |
1094 | spin_lock(&info->lock); | |
1095 | wrap = urb_to_wrap(urb, &info->rx_urbs_submitted); | |
1096 | if (!wrap) { | |
1097 | spin_unlock(&info->lock); | |
194343d9 | 1098 | dev_err(&port->dev, "%s - Not my urb!\n", __func__); |
1da177e4 LT |
1099 | return; |
1100 | } | |
1101 | list_del(&wrap->list); | |
1102 | spin_unlock(&info->lock); | |
1103 | ||
05400013 GKH |
1104 | if (status) { |
1105 | dbg("%s - nonzero read bulk status received: %d", | |
441b62c1 | 1106 | __func__, status); |
1da177e4 LT |
1107 | spin_lock(&info->lock); |
1108 | list_add(&wrap->list, &info->rx_urbs_free); | |
1109 | spin_unlock(&info->lock); | |
1110 | return; | |
1111 | } | |
1112 | ||
80359a9c AC |
1113 | usb_serial_debug_data(debug, &port->dev, |
1114 | __func__, urb->actual_length, data); | |
1da177e4 LT |
1115 | |
1116 | spin_lock(&info->lock); | |
1117 | list_add_tail(&wrap->list, &info->rx_urb_q); | |
1118 | if (info->flags & THROTTLED) { | |
1119 | info->flags |= ACTUALLY_THROTTLED; | |
1120 | spin_unlock(&info->lock); | |
1121 | return; | |
1122 | } | |
1123 | spin_unlock(&info->lock); | |
1124 | ||
1125 | schedule_work(&info->rx_work); | |
1126 | } | |
1127 | ||
1128 | ||
7d12e780 | 1129 | static void whiteheat_write_callback(struct urb *urb) |
1da177e4 | 1130 | { |
cdc97792 | 1131 | struct usb_serial_port *port = urb->context; |
1da177e4 LT |
1132 | struct whiteheat_private *info = usb_get_serial_port_data(port); |
1133 | struct whiteheat_urb_wrap *wrap; | |
05400013 | 1134 | int status = urb->status; |
1da177e4 | 1135 | |
441b62c1 | 1136 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
1137 | |
1138 | spin_lock(&info->lock); | |
1139 | wrap = urb_to_wrap(urb, &info->tx_urbs_submitted); | |
1140 | if (!wrap) { | |
1141 | spin_unlock(&info->lock); | |
194343d9 | 1142 | dev_err(&port->dev, "%s - Not my urb!\n", __func__); |
1da177e4 LT |
1143 | return; |
1144 | } | |
179e0917 | 1145 | list_move(&wrap->list, &info->tx_urbs_free); |
1da177e4 LT |
1146 | spin_unlock(&info->lock); |
1147 | ||
05400013 GKH |
1148 | if (status) { |
1149 | dbg("%s - nonzero write bulk status received: %d", | |
441b62c1 | 1150 | __func__, status); |
1da177e4 LT |
1151 | return; |
1152 | } | |
1153 | ||
cf2c7481 | 1154 | usb_serial_port_softint(port); |
1da177e4 LT |
1155 | } |
1156 | ||
1157 | ||
1158 | /***************************************************************************** | |
1159 | * Connect Tech's White Heat firmware interface | |
1160 | *****************************************************************************/ | |
80359a9c AC |
1161 | static int firm_send_command(struct usb_serial_port *port, __u8 command, |
1162 | __u8 *data, __u8 datasize) | |
1da177e4 LT |
1163 | { |
1164 | struct usb_serial_port *command_port; | |
1165 | struct whiteheat_command_private *command_info; | |
1166 | struct whiteheat_private *info; | |
1167 | __u8 *transfer_buffer; | |
1168 | int retval = 0; | |
08a2b3b6 | 1169 | int t; |
1da177e4 | 1170 | |
441b62c1 | 1171 | dbg("%s - command %d", __func__, command); |
1da177e4 LT |
1172 | |
1173 | command_port = port->serial->port[COMMAND_PORT]; | |
1174 | command_info = usb_get_serial_port_data(command_port); | |
08a2b3b6 | 1175 | mutex_lock(&command_info->mutex); |
38c3cb5b | 1176 | command_info->command_finished = false; |
80359a9c | 1177 | |
1da177e4 LT |
1178 | transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer; |
1179 | transfer_buffer[0] = command; | |
80359a9c | 1180 | memcpy(&transfer_buffer[1], data, datasize); |
1da177e4 LT |
1181 | command_port->write_urb->transfer_buffer_length = datasize + 1; |
1182 | command_port->write_urb->dev = port->serial->dev; | |
80359a9c | 1183 | retval = usb_submit_urb(command_port->write_urb, GFP_NOIO); |
1da177e4 | 1184 | if (retval) { |
441b62c1 | 1185 | dbg("%s - submit urb failed", __func__); |
1da177e4 LT |
1186 | goto exit; |
1187 | } | |
1da177e4 LT |
1188 | |
1189 | /* wait for the command to complete */ | |
08a2b3b6 | 1190 | t = wait_event_timeout(command_info->wait_command, |
38c3cb5b | 1191 | (bool)command_info->command_finished, COMMAND_TIMEOUT); |
08a2b3b6 ON |
1192 | if (!t) |
1193 | usb_kill_urb(command_port->write_urb); | |
1da177e4 | 1194 | |
38c3cb5b | 1195 | if (command_info->command_finished == false) { |
441b62c1 | 1196 | dbg("%s - command timed out.", __func__); |
1da177e4 LT |
1197 | retval = -ETIMEDOUT; |
1198 | goto exit; | |
1199 | } | |
1200 | ||
1201 | if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) { | |
441b62c1 | 1202 | dbg("%s - command failed.", __func__); |
1da177e4 LT |
1203 | retval = -EIO; |
1204 | goto exit; | |
1205 | } | |
1206 | ||
1207 | if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) { | |
441b62c1 | 1208 | dbg("%s - command completed.", __func__); |
1da177e4 | 1209 | switch (command) { |
80359a9c AC |
1210 | case WHITEHEAT_GET_DTR_RTS: |
1211 | info = usb_get_serial_port_data(port); | |
1212 | memcpy(&info->mcr, command_info->result_buffer, | |
1213 | sizeof(struct whiteheat_dr_info)); | |
1da177e4 LT |
1214 | break; |
1215 | } | |
1216 | } | |
1da177e4 | 1217 | exit: |
08a2b3b6 | 1218 | mutex_unlock(&command_info->mutex); |
1da177e4 LT |
1219 | return retval; |
1220 | } | |
1221 | ||
1222 | ||
80359a9c AC |
1223 | static int firm_open(struct usb_serial_port *port) |
1224 | { | |
1da177e4 LT |
1225 | struct whiteheat_simple open_command; |
1226 | ||
1227 | open_command.port = port->number - port->serial->minor + 1; | |
80359a9c AC |
1228 | return firm_send_command(port, WHITEHEAT_OPEN, |
1229 | (__u8 *)&open_command, sizeof(open_command)); | |
1da177e4 LT |
1230 | } |
1231 | ||
1232 | ||
80359a9c AC |
1233 | static int firm_close(struct usb_serial_port *port) |
1234 | { | |
1da177e4 LT |
1235 | struct whiteheat_simple close_command; |
1236 | ||
1237 | close_command.port = port->number - port->serial->minor + 1; | |
80359a9c AC |
1238 | return firm_send_command(port, WHITEHEAT_CLOSE, |
1239 | (__u8 *)&close_command, sizeof(close_command)); | |
1da177e4 LT |
1240 | } |
1241 | ||
1242 | ||
80359a9c AC |
1243 | static int firm_setup_port(struct tty_struct *tty) |
1244 | { | |
95da310e | 1245 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 | 1246 | struct whiteheat_port_settings port_settings; |
95da310e | 1247 | unsigned int cflag = tty->termios->c_cflag; |
1da177e4 LT |
1248 | |
1249 | port_settings.port = port->number + 1; | |
1250 | ||
1251 | /* get the byte size */ | |
1252 | switch (cflag & CSIZE) { | |
80359a9c AC |
1253 | case CS5: port_settings.bits = 5; break; |
1254 | case CS6: port_settings.bits = 6; break; | |
1255 | case CS7: port_settings.bits = 7; break; | |
1256 | default: | |
1257 | case CS8: port_settings.bits = 8; break; | |
1da177e4 | 1258 | } |
441b62c1 | 1259 | dbg("%s - data bits = %d", __func__, port_settings.bits); |
80359a9c | 1260 | |
1da177e4 LT |
1261 | /* determine the parity */ |
1262 | if (cflag & PARENB) | |
1263 | if (cflag & CMSPAR) | |
1264 | if (cflag & PARODD) | |
1265 | port_settings.parity = WHITEHEAT_PAR_MARK; | |
1266 | else | |
1267 | port_settings.parity = WHITEHEAT_PAR_SPACE; | |
1268 | else | |
1269 | if (cflag & PARODD) | |
1270 | port_settings.parity = WHITEHEAT_PAR_ODD; | |
1271 | else | |
1272 | port_settings.parity = WHITEHEAT_PAR_EVEN; | |
1273 | else | |
1274 | port_settings.parity = WHITEHEAT_PAR_NONE; | |
441b62c1 | 1275 | dbg("%s - parity = %c", __func__, port_settings.parity); |
1da177e4 LT |
1276 | |
1277 | /* figure out the stop bits requested */ | |
1278 | if (cflag & CSTOPB) | |
1279 | port_settings.stop = 2; | |
1280 | else | |
1281 | port_settings.stop = 1; | |
441b62c1 | 1282 | dbg("%s - stop bits = %d", __func__, port_settings.stop); |
1da177e4 LT |
1283 | |
1284 | /* figure out the flow control settings */ | |
1285 | if (cflag & CRTSCTS) | |
80359a9c AC |
1286 | port_settings.hflow = (WHITEHEAT_HFLOW_CTS | |
1287 | WHITEHEAT_HFLOW_RTS); | |
1da177e4 LT |
1288 | else |
1289 | port_settings.hflow = WHITEHEAT_HFLOW_NONE; | |
441b62c1 | 1290 | dbg("%s - hardware flow control = %s %s %s %s", __func__, |
1da177e4 LT |
1291 | (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "", |
1292 | (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "", | |
1293 | (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "", | |
1294 | (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : ""); | |
80359a9c | 1295 | |
1da177e4 | 1296 | /* determine software flow control */ |
95da310e | 1297 | if (I_IXOFF(tty)) |
1da177e4 LT |
1298 | port_settings.sflow = WHITEHEAT_SFLOW_RXTX; |
1299 | else | |
1300 | port_settings.sflow = WHITEHEAT_SFLOW_NONE; | |
441b62c1 | 1301 | dbg("%s - software flow control = %c", __func__, port_settings.sflow); |
80359a9c | 1302 | |
95da310e AC |
1303 | port_settings.xon = START_CHAR(tty); |
1304 | port_settings.xoff = STOP_CHAR(tty); | |
80359a9c AC |
1305 | dbg("%s - XON = %2x, XOFF = %2x", |
1306 | __func__, port_settings.xon, port_settings.xoff); | |
1da177e4 LT |
1307 | |
1308 | /* get the baud rate wanted */ | |
95da310e | 1309 | port_settings.baud = tty_get_baud_rate(tty); |
441b62c1 | 1310 | dbg("%s - baud rate = %d", __func__, port_settings.baud); |
1da177e4 | 1311 | |
01d1df29 | 1312 | /* fixme: should set validated settings */ |
95da310e | 1313 | tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud); |
1da177e4 LT |
1314 | /* handle any settings that aren't specified in the tty structure */ |
1315 | port_settings.lloop = 0; | |
80359a9c | 1316 | |
1da177e4 | 1317 | /* now send the message to the device */ |
80359a9c AC |
1318 | return firm_send_command(port, WHITEHEAT_SETUP_PORT, |
1319 | (__u8 *)&port_settings, sizeof(port_settings)); | |
1da177e4 LT |
1320 | } |
1321 | ||
1322 | ||
80359a9c AC |
1323 | static int firm_set_rts(struct usb_serial_port *port, __u8 onoff) |
1324 | { | |
1da177e4 LT |
1325 | struct whiteheat_set_rdb rts_command; |
1326 | ||
1327 | rts_command.port = port->number - port->serial->minor + 1; | |
1328 | rts_command.state = onoff; | |
80359a9c AC |
1329 | return firm_send_command(port, WHITEHEAT_SET_RTS, |
1330 | (__u8 *)&rts_command, sizeof(rts_command)); | |
1da177e4 LT |
1331 | } |
1332 | ||
1333 | ||
80359a9c AC |
1334 | static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff) |
1335 | { | |
1da177e4 LT |
1336 | struct whiteheat_set_rdb dtr_command; |
1337 | ||
1338 | dtr_command.port = port->number - port->serial->minor + 1; | |
1339 | dtr_command.state = onoff; | |
72e27412 | 1340 | return firm_send_command(port, WHITEHEAT_SET_DTR, |
80359a9c | 1341 | (__u8 *)&dtr_command, sizeof(dtr_command)); |
1da177e4 LT |
1342 | } |
1343 | ||
1344 | ||
80359a9c AC |
1345 | static int firm_set_break(struct usb_serial_port *port, __u8 onoff) |
1346 | { | |
1da177e4 LT |
1347 | struct whiteheat_set_rdb break_command; |
1348 | ||
1349 | break_command.port = port->number - port->serial->minor + 1; | |
1350 | break_command.state = onoff; | |
72e27412 | 1351 | return firm_send_command(port, WHITEHEAT_SET_BREAK, |
80359a9c | 1352 | (__u8 *)&break_command, sizeof(break_command)); |
1da177e4 LT |
1353 | } |
1354 | ||
1355 | ||
80359a9c AC |
1356 | static int firm_purge(struct usb_serial_port *port, __u8 rxtx) |
1357 | { | |
1da177e4 LT |
1358 | struct whiteheat_purge purge_command; |
1359 | ||
1360 | purge_command.port = port->number - port->serial->minor + 1; | |
1361 | purge_command.what = rxtx; | |
80359a9c AC |
1362 | return firm_send_command(port, WHITEHEAT_PURGE, |
1363 | (__u8 *)&purge_command, sizeof(purge_command)); | |
1da177e4 LT |
1364 | } |
1365 | ||
1366 | ||
80359a9c AC |
1367 | static int firm_get_dtr_rts(struct usb_serial_port *port) |
1368 | { | |
1da177e4 LT |
1369 | struct whiteheat_simple get_dr_command; |
1370 | ||
1371 | get_dr_command.port = port->number - port->serial->minor + 1; | |
80359a9c AC |
1372 | return firm_send_command(port, WHITEHEAT_GET_DTR_RTS, |
1373 | (__u8 *)&get_dr_command, sizeof(get_dr_command)); | |
1da177e4 LT |
1374 | } |
1375 | ||
1376 | ||
80359a9c AC |
1377 | static int firm_report_tx_done(struct usb_serial_port *port) |
1378 | { | |
1da177e4 LT |
1379 | struct whiteheat_simple close_command; |
1380 | ||
1381 | close_command.port = port->number - port->serial->minor + 1; | |
80359a9c AC |
1382 | return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE, |
1383 | (__u8 *)&close_command, sizeof(close_command)); | |
1da177e4 LT |
1384 | } |
1385 | ||
1386 | ||
1387 | /***************************************************************************** | |
1388 | * Connect Tech's White Heat utility functions | |
1389 | *****************************************************************************/ | |
1390 | static int start_command_port(struct usb_serial *serial) | |
1391 | { | |
1392 | struct usb_serial_port *command_port; | |
1393 | struct whiteheat_command_private *command_info; | |
1da177e4 | 1394 | int retval = 0; |
80359a9c | 1395 | |
1da177e4 LT |
1396 | command_port = serial->port[COMMAND_PORT]; |
1397 | command_info = usb_get_serial_port_data(command_port); | |
08a2b3b6 | 1398 | mutex_lock(&command_info->mutex); |
1da177e4 LT |
1399 | if (!command_info->port_running) { |
1400 | /* Work around HCD bugs */ | |
1401 | usb_clear_halt(serial->dev, command_port->read_urb->pipe); | |
1402 | ||
1403 | command_port->read_urb->dev = serial->dev; | |
1404 | retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL); | |
1405 | if (retval) { | |
194343d9 GKH |
1406 | dev_err(&serial->dev->dev, |
1407 | "%s - failed submitting read urb, error %d\n", | |
1408 | __func__, retval); | |
1da177e4 LT |
1409 | goto exit; |
1410 | } | |
1411 | } | |
1412 | command_info->port_running++; | |
1413 | ||
1414 | exit: | |
08a2b3b6 | 1415 | mutex_unlock(&command_info->mutex); |
1da177e4 LT |
1416 | return retval; |
1417 | } | |
1418 | ||
1419 | ||
1420 | static void stop_command_port(struct usb_serial *serial) | |
1421 | { | |
1422 | struct usb_serial_port *command_port; | |
1423 | struct whiteheat_command_private *command_info; | |
1da177e4 LT |
1424 | |
1425 | command_port = serial->port[COMMAND_PORT]; | |
1426 | command_info = usb_get_serial_port_data(command_port); | |
08a2b3b6 | 1427 | mutex_lock(&command_info->mutex); |
1da177e4 LT |
1428 | command_info->port_running--; |
1429 | if (!command_info->port_running) | |
1430 | usb_kill_urb(command_port->read_urb); | |
08a2b3b6 | 1431 | mutex_unlock(&command_info->mutex); |
1da177e4 LT |
1432 | } |
1433 | ||
1434 | ||
1435 | static int start_port_read(struct usb_serial_port *port) | |
1436 | { | |
1437 | struct whiteheat_private *info = usb_get_serial_port_data(port); | |
1438 | struct whiteheat_urb_wrap *wrap; | |
1439 | struct urb *urb; | |
1440 | int retval = 0; | |
1441 | unsigned long flags; | |
1442 | struct list_head *tmp; | |
1443 | struct list_head *tmp2; | |
1444 | ||
1445 | spin_lock_irqsave(&info->lock, flags); | |
1446 | ||
1447 | list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) { | |
1448 | list_del(tmp); | |
1449 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
1450 | urb = wrap->urb; | |
1451 | urb->dev = port->serial->dev; | |
08a2b3b6 | 1452 | spin_unlock_irqrestore(&info->lock, flags); |
1da177e4 LT |
1453 | retval = usb_submit_urb(urb, GFP_KERNEL); |
1454 | if (retval) { | |
08a2b3b6 | 1455 | spin_lock_irqsave(&info->lock, flags); |
1da177e4 LT |
1456 | list_add(tmp, &info->rx_urbs_free); |
1457 | list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) { | |
1458 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
1459 | urb = wrap->urb; | |
08a2b3b6 ON |
1460 | list_del(tmp); |
1461 | spin_unlock_irqrestore(&info->lock, flags); | |
1da177e4 | 1462 | usb_kill_urb(urb); |
08a2b3b6 ON |
1463 | spin_lock_irqsave(&info->lock, flags); |
1464 | list_add(tmp, &info->rx_urbs_free); | |
1da177e4 LT |
1465 | } |
1466 | break; | |
1467 | } | |
08a2b3b6 | 1468 | spin_lock_irqsave(&info->lock, flags); |
1da177e4 LT |
1469 | list_add(tmp, &info->rx_urbs_submitted); |
1470 | } | |
1471 | ||
1472 | spin_unlock_irqrestore(&info->lock, flags); | |
1473 | ||
1474 | return retval; | |
1475 | } | |
1476 | ||
1477 | ||
80359a9c AC |
1478 | static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb, |
1479 | struct list_head *head) | |
1da177e4 LT |
1480 | { |
1481 | struct whiteheat_urb_wrap *wrap; | |
1482 | struct list_head *tmp; | |
1483 | ||
1484 | list_for_each(tmp, head) { | |
1485 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
1486 | if (wrap->urb == urb) | |
1487 | return wrap; | |
1488 | } | |
1489 | ||
1490 | return NULL; | |
1491 | } | |
1492 | ||
1493 | ||
1494 | static struct list_head *list_first(struct list_head *head) | |
1495 | { | |
1496 | return head->next; | |
1497 | } | |
1498 | ||
1499 | ||
c4028958 | 1500 | static void rx_data_softint(struct work_struct *work) |
1da177e4 | 1501 | { |
c4028958 DH |
1502 | struct whiteheat_private *info = |
1503 | container_of(work, struct whiteheat_private, rx_work); | |
1504 | struct usb_serial_port *port = info->port; | |
4a90f09b | 1505 | struct tty_struct *tty = tty_port_tty_get(&port->port); |
1da177e4 LT |
1506 | struct whiteheat_urb_wrap *wrap; |
1507 | struct urb *urb; | |
1508 | unsigned long flags; | |
1509 | struct list_head *tmp; | |
1510 | struct list_head *tmp2; | |
1511 | int result; | |
1512 | int sent = 0; | |
1513 | ||
1514 | spin_lock_irqsave(&info->lock, flags); | |
1515 | if (info->flags & THROTTLED) { | |
1516 | spin_unlock_irqrestore(&info->lock, flags); | |
4a90f09b | 1517 | goto out; |
1da177e4 LT |
1518 | } |
1519 | ||
1520 | list_for_each_safe(tmp, tmp2, &info->rx_urb_q) { | |
1521 | list_del(tmp); | |
1522 | spin_unlock_irqrestore(&info->lock, flags); | |
1523 | ||
1524 | wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); | |
1525 | urb = wrap->urb; | |
1526 | ||
1527 | if (tty && urb->actual_length) { | |
80359a9c AC |
1528 | int len = tty_buffer_request_room(tty, |
1529 | urb->actual_length); | |
33f0f88f AC |
1530 | /* This stuff can go away now I suspect */ |
1531 | if (unlikely(len < urb->actual_length)) { | |
1da177e4 LT |
1532 | spin_lock_irqsave(&info->lock, flags); |
1533 | list_add(tmp, &info->rx_urb_q); | |
1534 | spin_unlock_irqrestore(&info->lock, flags); | |
1535 | tty_flip_buffer_push(tty); | |
1536 | schedule_work(&info->rx_work); | |
4a90f09b | 1537 | goto out; |
1da177e4 | 1538 | } |
33f0f88f AC |
1539 | tty_insert_flip_string(tty, urb->transfer_buffer, len); |
1540 | sent += len; | |
1da177e4 LT |
1541 | } |
1542 | ||
1543 | urb->dev = port->serial->dev; | |
1544 | result = usb_submit_urb(urb, GFP_ATOMIC); | |
1545 | if (result) { | |
194343d9 GKH |
1546 | dev_err(&port->dev, |
1547 | "%s - failed resubmitting read urb, error %d\n", | |
80359a9c | 1548 | __func__, result); |
1da177e4 LT |
1549 | spin_lock_irqsave(&info->lock, flags); |
1550 | list_add(tmp, &info->rx_urbs_free); | |
1551 | continue; | |
1552 | } | |
1553 | ||
1554 | spin_lock_irqsave(&info->lock, flags); | |
1555 | list_add(tmp, &info->rx_urbs_submitted); | |
1556 | } | |
1557 | spin_unlock_irqrestore(&info->lock, flags); | |
1558 | ||
1559 | if (sent) | |
1560 | tty_flip_buffer_push(tty); | |
4a90f09b AC |
1561 | out: |
1562 | tty_kref_put(tty); | |
1da177e4 LT |
1563 | } |
1564 | ||
1565 | ||
1566 | /***************************************************************************** | |
1567 | * Connect Tech's White Heat module functions | |
1568 | *****************************************************************************/ | |
80359a9c | 1569 | static int __init whiteheat_init(void) |
1da177e4 LT |
1570 | { |
1571 | int retval; | |
1572 | retval = usb_serial_register(&whiteheat_fake_device); | |
1573 | if (retval) | |
1574 | goto failed_fake_register; | |
1575 | retval = usb_serial_register(&whiteheat_device); | |
1576 | if (retval) | |
1577 | goto failed_device_register; | |
1578 | retval = usb_register(&whiteheat_driver); | |
1579 | if (retval) | |
1580 | goto failed_usb_register; | |
c197a8db GKH |
1581 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" |
1582 | DRIVER_DESC "\n"); | |
1da177e4 LT |
1583 | return 0; |
1584 | failed_usb_register: | |
1585 | usb_serial_deregister(&whiteheat_device); | |
1586 | failed_device_register: | |
1587 | usb_serial_deregister(&whiteheat_fake_device); | |
1588 | failed_fake_register: | |
1589 | return retval; | |
1590 | } | |
1591 | ||
1592 | ||
80359a9c | 1593 | static void __exit whiteheat_exit(void) |
1da177e4 | 1594 | { |
80359a9c AC |
1595 | usb_deregister(&whiteheat_driver); |
1596 | usb_serial_deregister(&whiteheat_fake_device); | |
1597 | usb_serial_deregister(&whiteheat_device); | |
1da177e4 LT |
1598 | } |
1599 | ||
1600 | ||
1601 | module_init(whiteheat_init); | |
1602 | module_exit(whiteheat_exit); | |
1603 | ||
80359a9c AC |
1604 | MODULE_AUTHOR(DRIVER_AUTHOR); |
1605 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1da177e4 LT |
1606 | MODULE_LICENSE("GPL"); |
1607 | ||
ec6752f5 DW |
1608 | MODULE_FIRMWARE("whiteheat.fw"); |
1609 | MODULE_FIRMWARE("whiteheat_loader.fw"); | |
1610 | ||
1da177e4 LT |
1611 | module_param(urb_pool_size, int, 0); |
1612 | MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering"); | |
1613 | ||
1614 | module_param(debug, bool, S_IRUGO | S_IWUSR); | |
1615 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |