whiteheat.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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. if (port->tty)
  542. port->tty->low_latency = 1;
  543. /* send an open port command */
  544. retval = firm_open(port);
  545. if (retval) {
  546. stop_command_port(port->serial);
  547. goto exit;
  548. }
  549. retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
  550. if (retval) {
  551. firm_close(port);
  552. stop_command_port(port->serial);
  553. goto exit;
  554. }
  555. old_term.c_cflag = ~port->tty->termios->c_cflag;
  556. old_term.c_iflag = ~port->tty->termios->c_iflag;
  557. whiteheat_set_termios(port, &old_term);
  558. /* Work around HCD bugs */
  559. usb_clear_halt(port->serial->dev, port->read_urb->pipe);
  560. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  561. /* Start reading from the device */
  562. retval = start_port_read(port);
  563. if (retval) {
  564. err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
  565. firm_close(port);
  566. stop_command_port(port->serial);
  567. goto exit;
  568. }
  569. exit:
  570. dbg("%s - exit, retval = %d", __FUNCTION__, retval);
  571. return retval;
  572. }
  573. static void whiteheat_close(struct usb_serial_port *port, struct file * filp)
  574. {
  575. struct whiteheat_private *info = usb_get_serial_port_data(port);
  576. struct whiteheat_urb_wrap *wrap;
  577. struct urb *urb;
  578. struct list_head *tmp;
  579. struct list_head *tmp2;
  580. dbg("%s - port %d", __FUNCTION__, port->number);
  581. /* filp is NULL when called from usb_serial_disconnect */
  582. if (filp && (tty_hung_up_p(filp))) {
  583. return;
  584. }
  585. port->tty->closing = 1;
  586. /*
  587. * Not currently in use; tty_wait_until_sent() calls
  588. * serial_chars_in_buffer() which deadlocks on the second semaphore
  589. * acquisition. This should be fixed at some point. Greg's been
  590. * notified.
  591. if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
  592. tty_wait_until_sent(port->tty, CLOSING_DELAY);
  593. }
  594. */
  595. if (port->tty->driver->flush_buffer)
  596. port->tty->driver->flush_buffer(port->tty);
  597. tty_ldisc_flush(port->tty);
  598. firm_report_tx_done(port);
  599. firm_close(port);
  600. printk(KERN_ERR"Before processing rx_urbs_submitted.\n");
  601. /* shutdown our bulk reads and writes */
  602. mutex_lock(&info->deathwarrant);
  603. spin_lock_irq(&info->lock);
  604. list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
  605. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  606. urb = wrap->urb;
  607. list_del(tmp);
  608. spin_unlock_irq(&info->lock);
  609. usb_kill_urb(urb);
  610. spin_lock_irq(&info->lock);
  611. list_add(tmp, &info->rx_urbs_free);
  612. }
  613. list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
  614. list_move(tmp, &info->rx_urbs_free);
  615. list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
  616. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  617. urb = wrap->urb;
  618. list_del(tmp);
  619. spin_unlock_irq(&info->lock);
  620. usb_kill_urb(urb);
  621. spin_lock_irq(&info->lock);
  622. list_add(tmp, &info->tx_urbs_free);
  623. }
  624. spin_unlock_irq(&info->lock);
  625. mutex_unlock(&info->deathwarrant);
  626. stop_command_port(port->serial);
  627. port->tty->closing = 0;
  628. }
  629. static int whiteheat_write(struct usb_serial_port *port, const unsigned char *buf, int count)
  630. {
  631. struct usb_serial *serial = port->serial;
  632. struct whiteheat_private *info = usb_get_serial_port_data(port);
  633. struct whiteheat_urb_wrap *wrap;
  634. struct urb *urb;
  635. int result;
  636. int bytes;
  637. int sent = 0;
  638. unsigned long flags;
  639. struct list_head *tmp;
  640. dbg("%s - port %d", __FUNCTION__, port->number);
  641. if (count == 0) {
  642. dbg("%s - write request of 0 bytes", __FUNCTION__);
  643. return (0);
  644. }
  645. while (count) {
  646. spin_lock_irqsave(&info->lock, flags);
  647. if (list_empty(&info->tx_urbs_free)) {
  648. spin_unlock_irqrestore(&info->lock, flags);
  649. break;
  650. }
  651. tmp = list_first(&info->tx_urbs_free);
  652. list_del(tmp);
  653. spin_unlock_irqrestore(&info->lock, flags);
  654. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  655. urb = wrap->urb;
  656. bytes = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  657. memcpy (urb->transfer_buffer, buf + sent, bytes);
  658. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, bytes, urb->transfer_buffer);
  659. urb->dev = serial->dev;
  660. urb->transfer_buffer_length = bytes;
  661. result = usb_submit_urb(urb, GFP_ATOMIC);
  662. if (result) {
  663. err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
  664. sent = result;
  665. spin_lock_irqsave(&info->lock, flags);
  666. list_add(tmp, &info->tx_urbs_free);
  667. spin_unlock_irqrestore(&info->lock, flags);
  668. break;
  669. } else {
  670. sent += bytes;
  671. count -= bytes;
  672. spin_lock_irqsave(&info->lock, flags);
  673. list_add(tmp, &info->tx_urbs_submitted);
  674. spin_unlock_irqrestore(&info->lock, flags);
  675. }
  676. }
  677. return sent;
  678. }
  679. static int whiteheat_write_room(struct usb_serial_port *port)
  680. {
  681. struct whiteheat_private *info = usb_get_serial_port_data(port);
  682. struct list_head *tmp;
  683. int room = 0;
  684. unsigned long flags;
  685. dbg("%s - port %d", __FUNCTION__, port->number);
  686. spin_lock_irqsave(&info->lock, flags);
  687. list_for_each(tmp, &info->tx_urbs_free)
  688. room++;
  689. spin_unlock_irqrestore(&info->lock, flags);
  690. room *= port->bulk_out_size;
  691. dbg("%s - returns %d", __FUNCTION__, room);
  692. return (room);
  693. }
  694. static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file)
  695. {
  696. struct whiteheat_private *info = usb_get_serial_port_data(port);
  697. unsigned int modem_signals = 0;
  698. dbg("%s - port %d", __FUNCTION__, port->number);
  699. firm_get_dtr_rts(port);
  700. if (info->mcr & UART_MCR_DTR)
  701. modem_signals |= TIOCM_DTR;
  702. if (info->mcr & UART_MCR_RTS)
  703. modem_signals |= TIOCM_RTS;
  704. return modem_signals;
  705. }
  706. static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file,
  707. unsigned int set, unsigned int clear)
  708. {
  709. struct whiteheat_private *info = usb_get_serial_port_data(port);
  710. dbg("%s - port %d", __FUNCTION__, port->number);
  711. if (set & TIOCM_RTS)
  712. info->mcr |= UART_MCR_RTS;
  713. if (set & TIOCM_DTR)
  714. info->mcr |= UART_MCR_DTR;
  715. if (clear & TIOCM_RTS)
  716. info->mcr &= ~UART_MCR_RTS;
  717. if (clear & TIOCM_DTR)
  718. info->mcr &= ~UART_MCR_DTR;
  719. firm_set_dtr(port, info->mcr & UART_MCR_DTR);
  720. firm_set_rts(port, info->mcr & UART_MCR_RTS);
  721. return 0;
  722. }
  723. static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
  724. {
  725. struct serial_struct serstruct;
  726. void __user *user_arg = (void __user *)arg;
  727. dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
  728. switch (cmd) {
  729. case TIOCGSERIAL:
  730. memset(&serstruct, 0, sizeof(serstruct));
  731. serstruct.type = PORT_16654;
  732. serstruct.line = port->serial->minor;
  733. serstruct.port = port->number;
  734. serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  735. serstruct.xmit_fifo_size = port->bulk_out_size;
  736. serstruct.custom_divisor = 0;
  737. serstruct.baud_base = 460800;
  738. serstruct.close_delay = CLOSING_DELAY;
  739. serstruct.closing_wait = CLOSING_DELAY;
  740. if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
  741. return -EFAULT;
  742. break;
  743. case TIOCSSERIAL:
  744. if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
  745. return -EFAULT;
  746. /*
  747. * For now this is ignored. dip sets the ASYNC_[V]HI flags
  748. * but this isn't used by us at all. Maybe someone somewhere
  749. * will need the custom_divisor setting.
  750. */
  751. break;
  752. default:
  753. break;
  754. }
  755. return -ENOIOCTLCMD;
  756. }
  757. static void whiteheat_set_termios(struct usb_serial_port *port, struct ktermios *old_termios)
  758. {
  759. dbg("%s -port %d", __FUNCTION__, port->number);
  760. if ((!port->tty) || (!port->tty->termios)) {
  761. dbg("%s - no tty structures", __FUNCTION__);
  762. goto exit;
  763. }
  764. /* check that they really want us to change something */
  765. if (old_termios) {
  766. if ((port->tty->termios->c_cflag == old_termios->c_cflag) &&
  767. (port->tty->termios->c_iflag == old_termios->c_iflag)) {
  768. dbg("%s - nothing to change...", __FUNCTION__);
  769. goto exit;
  770. }
  771. }
  772. firm_setup_port(port);
  773. exit:
  774. return;
  775. }
  776. static void whiteheat_break_ctl(struct usb_serial_port *port, int break_state) {
  777. firm_set_break(port, break_state);
  778. }
  779. static int whiteheat_chars_in_buffer(struct usb_serial_port *port)
  780. {
  781. struct whiteheat_private *info = usb_get_serial_port_data(port);
  782. struct list_head *tmp;
  783. struct whiteheat_urb_wrap *wrap;
  784. int chars = 0;
  785. unsigned long flags;
  786. dbg("%s - port %d", __FUNCTION__, port->number);
  787. spin_lock_irqsave(&info->lock, flags);
  788. list_for_each(tmp, &info->tx_urbs_submitted) {
  789. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  790. chars += wrap->urb->transfer_buffer_length;
  791. }
  792. spin_unlock_irqrestore(&info->lock, flags);
  793. dbg ("%s - returns %d", __FUNCTION__, chars);
  794. return chars;
  795. }
  796. static void whiteheat_throttle (struct usb_serial_port *port)
  797. {
  798. struct whiteheat_private *info = usb_get_serial_port_data(port);
  799. unsigned long flags;
  800. dbg("%s - port %d", __FUNCTION__, port->number);
  801. spin_lock_irqsave(&info->lock, flags);
  802. info->flags |= THROTTLED;
  803. spin_unlock_irqrestore(&info->lock, flags);
  804. return;
  805. }
  806. static void whiteheat_unthrottle (struct usb_serial_port *port)
  807. {
  808. struct whiteheat_private *info = usb_get_serial_port_data(port);
  809. int actually_throttled;
  810. unsigned long flags;
  811. dbg("%s - port %d", __FUNCTION__, port->number);
  812. spin_lock_irqsave(&info->lock, flags);
  813. actually_throttled = info->flags & ACTUALLY_THROTTLED;
  814. info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  815. spin_unlock_irqrestore(&info->lock, flags);
  816. if (actually_throttled)
  817. rx_data_softint(&info->rx_work);
  818. return;
  819. }
  820. /*****************************************************************************
  821. * Connect Tech's White Heat callback routines
  822. *****************************************************************************/
  823. static void command_port_write_callback(struct urb *urb)
  824. {
  825. dbg("%s", __FUNCTION__);
  826. if (urb->status) {
  827. dbg ("nonzero urb status: %d", urb->status);
  828. return;
  829. }
  830. }
  831. static void command_port_read_callback(struct urb *urb)
  832. {
  833. struct usb_serial_port *command_port = (struct usb_serial_port *)urb->context;
  834. struct whiteheat_command_private *command_info;
  835. unsigned char *data = urb->transfer_buffer;
  836. int result;
  837. dbg("%s", __FUNCTION__);
  838. command_info = usb_get_serial_port_data(command_port);
  839. if (!command_info) {
  840. dbg ("%s - command_info is NULL, exiting.", __FUNCTION__);
  841. return;
  842. }
  843. if (urb->status) {
  844. dbg("%s - nonzero urb status: %d", __FUNCTION__, urb->status);
  845. if (urb->status != -ENOENT)
  846. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  847. wake_up(&command_info->wait_command);
  848. return;
  849. }
  850. usb_serial_debug_data(debug, &command_port->dev, __FUNCTION__, urb->actual_length, data);
  851. if (data[0] == WHITEHEAT_CMD_COMPLETE) {
  852. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  853. wake_up(&command_info->wait_command);
  854. } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
  855. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  856. wake_up(&command_info->wait_command);
  857. } else if (data[0] == WHITEHEAT_EVENT) {
  858. /* These are unsolicited reports from the firmware, hence no waiting command to wakeup */
  859. dbg("%s - event received", __FUNCTION__);
  860. } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
  861. memcpy(command_info->result_buffer, &data[1], urb->actual_length - 1);
  862. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  863. wake_up(&command_info->wait_command);
  864. } else {
  865. dbg("%s - bad reply from firmware", __FUNCTION__);
  866. }
  867. /* Continue trying to always read */
  868. command_port->read_urb->dev = command_port->serial->dev;
  869. result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
  870. if (result)
  871. dbg("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  872. }
  873. static void whiteheat_read_callback(struct urb *urb)
  874. {
  875. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  876. struct whiteheat_urb_wrap *wrap;
  877. unsigned char *data = urb->transfer_buffer;
  878. struct whiteheat_private *info = usb_get_serial_port_data(port);
  879. dbg("%s - port %d", __FUNCTION__, port->number);
  880. spin_lock(&info->lock);
  881. wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
  882. if (!wrap) {
  883. spin_unlock(&info->lock);
  884. err("%s - Not my urb!", __FUNCTION__);
  885. return;
  886. }
  887. list_del(&wrap->list);
  888. spin_unlock(&info->lock);
  889. if (urb->status) {
  890. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  891. spin_lock(&info->lock);
  892. list_add(&wrap->list, &info->rx_urbs_free);
  893. spin_unlock(&info->lock);
  894. return;
  895. }
  896. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  897. spin_lock(&info->lock);
  898. list_add_tail(&wrap->list, &info->rx_urb_q);
  899. if (info->flags & THROTTLED) {
  900. info->flags |= ACTUALLY_THROTTLED;
  901. spin_unlock(&info->lock);
  902. return;
  903. }
  904. spin_unlock(&info->lock);
  905. schedule_work(&info->rx_work);
  906. }
  907. static void whiteheat_write_callback(struct urb *urb)
  908. {
  909. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  910. struct whiteheat_private *info = usb_get_serial_port_data(port);
  911. struct whiteheat_urb_wrap *wrap;
  912. dbg("%s - port %d", __FUNCTION__, port->number);
  913. spin_lock(&info->lock);
  914. wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
  915. if (!wrap) {
  916. spin_unlock(&info->lock);
  917. err("%s - Not my urb!", __FUNCTION__);
  918. return;
  919. }
  920. list_move(&wrap->list, &info->tx_urbs_free);
  921. spin_unlock(&info->lock);
  922. if (urb->status) {
  923. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  924. return;
  925. }
  926. usb_serial_port_softint(port);
  927. }
  928. /*****************************************************************************
  929. * Connect Tech's White Heat firmware interface
  930. *****************************************************************************/
  931. static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize)
  932. {
  933. struct usb_serial_port *command_port;
  934. struct whiteheat_command_private *command_info;
  935. struct whiteheat_private *info;
  936. __u8 *transfer_buffer;
  937. int retval = 0;
  938. int t;
  939. dbg("%s - command %d", __FUNCTION__, command);
  940. command_port = port->serial->port[COMMAND_PORT];
  941. command_info = usb_get_serial_port_data(command_port);
  942. mutex_lock(&command_info->mutex);
  943. command_info->command_finished = false;
  944. transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
  945. transfer_buffer[0] = command;
  946. memcpy (&transfer_buffer[1], data, datasize);
  947. command_port->write_urb->transfer_buffer_length = datasize + 1;
  948. command_port->write_urb->dev = port->serial->dev;
  949. retval = usb_submit_urb (command_port->write_urb, GFP_NOIO);
  950. if (retval) {
  951. dbg("%s - submit urb failed", __FUNCTION__);
  952. goto exit;
  953. }
  954. /* wait for the command to complete */
  955. t = wait_event_timeout(command_info->wait_command,
  956. (bool)command_info->command_finished, COMMAND_TIMEOUT);
  957. if (!t)
  958. usb_kill_urb(command_port->write_urb);
  959. if (command_info->command_finished == false) {
  960. dbg("%s - command timed out.", __FUNCTION__);
  961. retval = -ETIMEDOUT;
  962. goto exit;
  963. }
  964. if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
  965. dbg("%s - command failed.", __FUNCTION__);
  966. retval = -EIO;
  967. goto exit;
  968. }
  969. if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
  970. dbg("%s - command completed.", __FUNCTION__);
  971. switch (command) {
  972. case WHITEHEAT_GET_DTR_RTS:
  973. info = usb_get_serial_port_data(port);
  974. memcpy(&info->mcr, command_info->result_buffer, sizeof(struct whiteheat_dr_info));
  975. break;
  976. }
  977. }
  978. exit:
  979. mutex_unlock(&command_info->mutex);
  980. return retval;
  981. }
  982. static int firm_open(struct usb_serial_port *port) {
  983. struct whiteheat_simple open_command;
  984. open_command.port = port->number - port->serial->minor + 1;
  985. return firm_send_command(port, WHITEHEAT_OPEN, (__u8 *)&open_command, sizeof(open_command));
  986. }
  987. static int firm_close(struct usb_serial_port *port) {
  988. struct whiteheat_simple close_command;
  989. close_command.port = port->number - port->serial->minor + 1;
  990. return firm_send_command(port, WHITEHEAT_CLOSE, (__u8 *)&close_command, sizeof(close_command));
  991. }
  992. static int firm_setup_port(struct usb_serial_port *port) {
  993. struct whiteheat_port_settings port_settings;
  994. unsigned int cflag = port->tty->termios->c_cflag;
  995. port_settings.port = port->number + 1;
  996. /* get the byte size */
  997. switch (cflag & CSIZE) {
  998. case CS5: port_settings.bits = 5; break;
  999. case CS6: port_settings.bits = 6; break;
  1000. case CS7: port_settings.bits = 7; break;
  1001. default:
  1002. case CS8: port_settings.bits = 8; break;
  1003. }
  1004. dbg("%s - data bits = %d", __FUNCTION__, port_settings.bits);
  1005. /* determine the parity */
  1006. if (cflag & PARENB)
  1007. if (cflag & CMSPAR)
  1008. if (cflag & PARODD)
  1009. port_settings.parity = WHITEHEAT_PAR_MARK;
  1010. else
  1011. port_settings.parity = WHITEHEAT_PAR_SPACE;
  1012. else
  1013. if (cflag & PARODD)
  1014. port_settings.parity = WHITEHEAT_PAR_ODD;
  1015. else
  1016. port_settings.parity = WHITEHEAT_PAR_EVEN;
  1017. else
  1018. port_settings.parity = WHITEHEAT_PAR_NONE;
  1019. dbg("%s - parity = %c", __FUNCTION__, port_settings.parity);
  1020. /* figure out the stop bits requested */
  1021. if (cflag & CSTOPB)
  1022. port_settings.stop = 2;
  1023. else
  1024. port_settings.stop = 1;
  1025. dbg("%s - stop bits = %d", __FUNCTION__, port_settings.stop);
  1026. /* figure out the flow control settings */
  1027. if (cflag & CRTSCTS)
  1028. port_settings.hflow = (WHITEHEAT_HFLOW_CTS | WHITEHEAT_HFLOW_RTS);
  1029. else
  1030. port_settings.hflow = WHITEHEAT_HFLOW_NONE;
  1031. dbg("%s - hardware flow control = %s %s %s %s", __FUNCTION__,
  1032. (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
  1033. (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
  1034. (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
  1035. (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
  1036. /* determine software flow control */
  1037. if (I_IXOFF(port->tty))
  1038. port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
  1039. else
  1040. port_settings.sflow = WHITEHEAT_SFLOW_NONE;
  1041. dbg("%s - software flow control = %c", __FUNCTION__, port_settings.sflow);
  1042. port_settings.xon = START_CHAR(port->tty);
  1043. port_settings.xoff = STOP_CHAR(port->tty);
  1044. dbg("%s - XON = %2x, XOFF = %2x", __FUNCTION__, port_settings.xon, port_settings.xoff);
  1045. /* get the baud rate wanted */
  1046. port_settings.baud = tty_get_baud_rate(port->tty);
  1047. dbg("%s - baud rate = %d", __FUNCTION__, port_settings.baud);
  1048. /* handle any settings that aren't specified in the tty structure */
  1049. port_settings.lloop = 0;
  1050. /* now send the message to the device */
  1051. return firm_send_command(port, WHITEHEAT_SETUP_PORT, (__u8 *)&port_settings, sizeof(port_settings));
  1052. }
  1053. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff) {
  1054. struct whiteheat_set_rdb rts_command;
  1055. rts_command.port = port->number - port->serial->minor + 1;
  1056. rts_command.state = onoff;
  1057. return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&rts_command, sizeof(rts_command));
  1058. }
  1059. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff) {
  1060. struct whiteheat_set_rdb dtr_command;
  1061. dtr_command.port = port->number - port->serial->minor + 1;
  1062. dtr_command.state = onoff;
  1063. return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&dtr_command, sizeof(dtr_command));
  1064. }
  1065. static int firm_set_break(struct usb_serial_port *port, __u8 onoff) {
  1066. struct whiteheat_set_rdb break_command;
  1067. break_command.port = port->number - port->serial->minor + 1;
  1068. break_command.state = onoff;
  1069. return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&break_command, sizeof(break_command));
  1070. }
  1071. static int firm_purge(struct usb_serial_port *port, __u8 rxtx) {
  1072. struct whiteheat_purge purge_command;
  1073. purge_command.port = port->number - port->serial->minor + 1;
  1074. purge_command.what = rxtx;
  1075. return firm_send_command(port, WHITEHEAT_PURGE, (__u8 *)&purge_command, sizeof(purge_command));
  1076. }
  1077. static int firm_get_dtr_rts(struct usb_serial_port *port) {
  1078. struct whiteheat_simple get_dr_command;
  1079. get_dr_command.port = port->number - port->serial->minor + 1;
  1080. return firm_send_command(port, WHITEHEAT_GET_DTR_RTS, (__u8 *)&get_dr_command, sizeof(get_dr_command));
  1081. }
  1082. static int firm_report_tx_done(struct usb_serial_port *port) {
  1083. struct whiteheat_simple close_command;
  1084. close_command.port = port->number - port->serial->minor + 1;
  1085. return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE, (__u8 *)&close_command, sizeof(close_command));
  1086. }
  1087. /*****************************************************************************
  1088. * Connect Tech's White Heat utility functions
  1089. *****************************************************************************/
  1090. static int start_command_port(struct usb_serial *serial)
  1091. {
  1092. struct usb_serial_port *command_port;
  1093. struct whiteheat_command_private *command_info;
  1094. int retval = 0;
  1095. command_port = serial->port[COMMAND_PORT];
  1096. command_info = usb_get_serial_port_data(command_port);
  1097. mutex_lock(&command_info->mutex);
  1098. if (!command_info->port_running) {
  1099. /* Work around HCD bugs */
  1100. usb_clear_halt(serial->dev, command_port->read_urb->pipe);
  1101. command_port->read_urb->dev = serial->dev;
  1102. retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
  1103. if (retval) {
  1104. err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
  1105. goto exit;
  1106. }
  1107. }
  1108. command_info->port_running++;
  1109. exit:
  1110. mutex_unlock(&command_info->mutex);
  1111. return retval;
  1112. }
  1113. static void stop_command_port(struct usb_serial *serial)
  1114. {
  1115. struct usb_serial_port *command_port;
  1116. struct whiteheat_command_private *command_info;
  1117. command_port = serial->port[COMMAND_PORT];
  1118. command_info = usb_get_serial_port_data(command_port);
  1119. mutex_lock(&command_info->mutex);
  1120. command_info->port_running--;
  1121. if (!command_info->port_running)
  1122. usb_kill_urb(command_port->read_urb);
  1123. mutex_unlock(&command_info->mutex);
  1124. }
  1125. static int start_port_read(struct usb_serial_port *port)
  1126. {
  1127. struct whiteheat_private *info = usb_get_serial_port_data(port);
  1128. struct whiteheat_urb_wrap *wrap;
  1129. struct urb *urb;
  1130. int retval = 0;
  1131. unsigned long flags;
  1132. struct list_head *tmp;
  1133. struct list_head *tmp2;
  1134. spin_lock_irqsave(&info->lock, flags);
  1135. list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
  1136. list_del(tmp);
  1137. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1138. urb = wrap->urb;
  1139. urb->dev = port->serial->dev;
  1140. spin_unlock_irqrestore(&info->lock, flags);
  1141. retval = usb_submit_urb(urb, GFP_KERNEL);
  1142. if (retval) {
  1143. spin_lock_irqsave(&info->lock, flags);
  1144. list_add(tmp, &info->rx_urbs_free);
  1145. list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
  1146. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1147. urb = wrap->urb;
  1148. list_del(tmp);
  1149. spin_unlock_irqrestore(&info->lock, flags);
  1150. usb_kill_urb(urb);
  1151. spin_lock_irqsave(&info->lock, flags);
  1152. list_add(tmp, &info->rx_urbs_free);
  1153. }
  1154. break;
  1155. }
  1156. spin_lock_irqsave(&info->lock, flags);
  1157. list_add(tmp, &info->rx_urbs_submitted);
  1158. }
  1159. spin_unlock_irqrestore(&info->lock, flags);
  1160. return retval;
  1161. }
  1162. static struct whiteheat_urb_wrap *urb_to_wrap(struct urb* urb, struct list_head *head)
  1163. {
  1164. struct whiteheat_urb_wrap *wrap;
  1165. struct list_head *tmp;
  1166. list_for_each(tmp, head) {
  1167. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1168. if (wrap->urb == urb)
  1169. return wrap;
  1170. }
  1171. return NULL;
  1172. }
  1173. static struct list_head *list_first(struct list_head *head)
  1174. {
  1175. return head->next;
  1176. }
  1177. static void rx_data_softint(struct work_struct *work)
  1178. {
  1179. struct whiteheat_private *info =
  1180. container_of(work, struct whiteheat_private, rx_work);
  1181. struct usb_serial_port *port = info->port;
  1182. struct tty_struct *tty = port->tty;
  1183. struct whiteheat_urb_wrap *wrap;
  1184. struct urb *urb;
  1185. unsigned long flags;
  1186. struct list_head *tmp;
  1187. struct list_head *tmp2;
  1188. int result;
  1189. int sent = 0;
  1190. spin_lock_irqsave(&info->lock, flags);
  1191. if (info->flags & THROTTLED) {
  1192. spin_unlock_irqrestore(&info->lock, flags);
  1193. return;
  1194. }
  1195. list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
  1196. list_del(tmp);
  1197. spin_unlock_irqrestore(&info->lock, flags);
  1198. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1199. urb = wrap->urb;
  1200. if (tty && urb->actual_length) {
  1201. int len = tty_buffer_request_room(tty, urb->actual_length);
  1202. /* This stuff can go away now I suspect */
  1203. if (unlikely(len < urb->actual_length)) {
  1204. spin_lock_irqsave(&info->lock, flags);
  1205. list_add(tmp, &info->rx_urb_q);
  1206. spin_unlock_irqrestore(&info->lock, flags);
  1207. tty_flip_buffer_push(tty);
  1208. schedule_work(&info->rx_work);
  1209. return;
  1210. }
  1211. tty_insert_flip_string(tty, urb->transfer_buffer, len);
  1212. sent += len;
  1213. }
  1214. urb->dev = port->serial->dev;
  1215. result = usb_submit_urb(urb, GFP_ATOMIC);
  1216. if (result) {
  1217. err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  1218. spin_lock_irqsave(&info->lock, flags);
  1219. list_add(tmp, &info->rx_urbs_free);
  1220. continue;
  1221. }
  1222. spin_lock_irqsave(&info->lock, flags);
  1223. list_add(tmp, &info->rx_urbs_submitted);
  1224. }
  1225. spin_unlock_irqrestore(&info->lock, flags);
  1226. if (sent)
  1227. tty_flip_buffer_push(tty);
  1228. }
  1229. /*****************************************************************************
  1230. * Connect Tech's White Heat module functions
  1231. *****************************************************************************/
  1232. static int __init whiteheat_init (void)
  1233. {
  1234. int retval;
  1235. retval = usb_serial_register(&whiteheat_fake_device);
  1236. if (retval)
  1237. goto failed_fake_register;
  1238. retval = usb_serial_register(&whiteheat_device);
  1239. if (retval)
  1240. goto failed_device_register;
  1241. retval = usb_register(&whiteheat_driver);
  1242. if (retval)
  1243. goto failed_usb_register;
  1244. info(DRIVER_DESC " " DRIVER_VERSION);
  1245. return 0;
  1246. failed_usb_register:
  1247. usb_serial_deregister(&whiteheat_device);
  1248. failed_device_register:
  1249. usb_serial_deregister(&whiteheat_fake_device);
  1250. failed_fake_register:
  1251. return retval;
  1252. }
  1253. static void __exit whiteheat_exit (void)
  1254. {
  1255. usb_deregister (&whiteheat_driver);
  1256. usb_serial_deregister (&whiteheat_fake_device);
  1257. usb_serial_deregister (&whiteheat_device);
  1258. }
  1259. module_init(whiteheat_init);
  1260. module_exit(whiteheat_exit);
  1261. MODULE_AUTHOR( DRIVER_AUTHOR );
  1262. MODULE_DESCRIPTION( DRIVER_DESC );
  1263. MODULE_LICENSE("GPL");
  1264. module_param(urb_pool_size, int, 0);
  1265. MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
  1266. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1267. MODULE_PARM_DESC(debug, "Debug enabled or not");