whiteheat.c 44 KB

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