whiteheat.c 44 KB

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