whiteheat.c 44 KB

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