whiteheat.c 23 KB

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