whiteheat.c 43 KB

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