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