whiteheat.c 44 KB

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