whiteheat.c 24 KB

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