whiteheat.c 23 KB

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