whiteheat.c 27 KB

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