cypress_m8.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. /*
  2. * USB Cypress M8 driver
  3. *
  4. * Copyright (C) 2004
  5. * Lonnie Mendez (dignome@gmail.com)
  6. * Copyright (C) 2003,2004
  7. * Neil Whelchel (koyama@firstlight.net)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * See Documentation/usb/usb-serial.txt for more information on using this
  15. * driver
  16. *
  17. * See http://geocities.com/i0xox0i for information on this driver and the
  18. * earthmate usb device.
  19. *
  20. * Lonnie Mendez <dignome@gmail.com>
  21. * 4-29-2005
  22. * Fixed problem where setting or retreiving the serial config would fail
  23. * with EPIPE. Removed CRTS toggling so the driver behaves more like
  24. * other usbserial adapters. Issued new interval of 1ms instead of the
  25. * default 10ms. As a result, transfer speed has been substantially
  26. * increased from avg. 850bps to avg. 3300bps. initial termios has also
  27. * been modified. Cleaned up code and formatting issues so it is more
  28. * readable. Replaced the C++ style comments.
  29. *
  30. * Lonnie Mendez <dignome@gmail.com>
  31. * 12-15-2004
  32. * Incorporated write buffering from pl2303 driver. Fixed bug with line
  33. * handling so both lines are raised in cypress_open. (was dropping rts)
  34. * Various code cleanups made as well along with other misc bug fixes.
  35. *
  36. * Lonnie Mendez <dignome@gmail.com>
  37. * 04-10-2004
  38. * Driver modified to support dynamic line settings. Various improvments
  39. * and features.
  40. *
  41. * Neil Whelchel
  42. * 10-2003
  43. * Driver first released.
  44. *
  45. */
  46. /* Thanks to Neil Whelchel for writing the first cypress m8 implementation
  47. for linux. */
  48. /* Thanks to cypress for providing references for the hid reports. */
  49. /* Thanks to Jiang Zhang for providing links and for general help. */
  50. /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
  51. #include <linux/kernel.h>
  52. #include <linux/errno.h>
  53. #include <linux/init.h>
  54. #include <linux/slab.h>
  55. #include <linux/tty.h>
  56. #include <linux/tty_driver.h>
  57. #include <linux/tty_flip.h>
  58. #include <linux/module.h>
  59. #include <linux/moduleparam.h>
  60. #include <linux/spinlock.h>
  61. #include <linux/usb.h>
  62. #include <linux/usb/serial.h>
  63. #include <linux/serial.h>
  64. #include <linux/delay.h>
  65. #include <linux/uaccess.h>
  66. #include <asm/unaligned.h>
  67. #include "cypress_m8.h"
  68. static int debug;
  69. static int stats;
  70. static int interval;
  71. static int unstable_bauds;
  72. /*
  73. * Version Information
  74. */
  75. #define DRIVER_VERSION "v1.09"
  76. #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
  77. #define DRIVER_DESC "Cypress USB to Serial Driver"
  78. /* write buffer size defines */
  79. #define CYPRESS_BUF_SIZE 1024
  80. #define CYPRESS_CLOSING_WAIT (30*HZ)
  81. static const struct usb_device_id id_table_earthmate[] = {
  82. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  83. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  84. { } /* Terminating entry */
  85. };
  86. static const struct usb_device_id id_table_cyphidcomrs232[] = {
  87. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  88. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  89. { } /* Terminating entry */
  90. };
  91. static const struct usb_device_id id_table_nokiaca42v2[] = {
  92. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  93. { } /* Terminating entry */
  94. };
  95. static const struct usb_device_id id_table_combined[] = {
  96. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  97. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  98. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  99. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  100. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  101. { } /* Terminating entry */
  102. };
  103. MODULE_DEVICE_TABLE(usb, id_table_combined);
  104. static struct usb_driver cypress_driver = {
  105. .name = "cypress",
  106. .probe = usb_serial_probe,
  107. .disconnect = usb_serial_disconnect,
  108. .id_table = id_table_combined,
  109. .no_dynamic_id = 1,
  110. };
  111. enum packet_format {
  112. packet_format_1, /* b0:status, b1:payload count */
  113. packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
  114. };
  115. struct cypress_private {
  116. spinlock_t lock; /* private lock */
  117. int chiptype; /* identifier of device, for quirks/etc */
  118. int bytes_in; /* used for statistics */
  119. int bytes_out; /* used for statistics */
  120. int cmd_count; /* used for statistics */
  121. int cmd_ctrl; /* always set this to 1 before issuing a command */
  122. struct cypress_buf *buf; /* write buffer */
  123. int write_urb_in_use; /* write urb in use indicator */
  124. int write_urb_interval; /* interval to use for write urb */
  125. int read_urb_interval; /* interval to use for read urb */
  126. int comm_is_ok; /* true if communication is (still) ok */
  127. int termios_initialized;
  128. __u8 line_control; /* holds dtr / rts value */
  129. __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
  130. __u8 current_config; /* stores the current configuration byte */
  131. __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
  132. enum packet_format pkt_fmt; /* format to use for packet send / receive */
  133. int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
  134. int baud_rate; /* stores current baud rate in
  135. integer form */
  136. int isthrottled; /* if throttled, discard reads */
  137. wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
  138. char prev_status, diff_status; /* used for TIOCMIWAIT */
  139. /* we pass a pointer to this as the arguement sent to
  140. cypress_set_termios old_termios */
  141. struct ktermios tmp_termios; /* stores the old termios settings */
  142. };
  143. /* write buffer structure */
  144. struct cypress_buf {
  145. unsigned int buf_size;
  146. char *buf_buf;
  147. char *buf_get;
  148. char *buf_put;
  149. };
  150. /* function prototypes for the Cypress USB to serial device */
  151. static int cypress_earthmate_startup(struct usb_serial *serial);
  152. static int cypress_hidcom_startup(struct usb_serial *serial);
  153. static int cypress_ca42v2_startup(struct usb_serial *serial);
  154. static void cypress_release(struct usb_serial *serial);
  155. static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
  156. static void cypress_close(struct usb_serial_port *port);
  157. static void cypress_dtr_rts(struct usb_serial_port *port, int on);
  158. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  159. const unsigned char *buf, int count);
  160. static void cypress_send(struct usb_serial_port *port);
  161. static int cypress_write_room(struct tty_struct *tty);
  162. static int cypress_ioctl(struct tty_struct *tty, struct file *file,
  163. unsigned int cmd, unsigned long arg);
  164. static void cypress_set_termios(struct tty_struct *tty,
  165. struct usb_serial_port *port, struct ktermios *old);
  166. static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
  167. static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
  168. unsigned int set, unsigned int clear);
  169. static int cypress_chars_in_buffer(struct tty_struct *tty);
  170. static void cypress_throttle(struct tty_struct *tty);
  171. static void cypress_unthrottle(struct tty_struct *tty);
  172. static void cypress_set_dead(struct usb_serial_port *port);
  173. static void cypress_read_int_callback(struct urb *urb);
  174. static void cypress_write_int_callback(struct urb *urb);
  175. /* write buffer functions */
  176. static struct cypress_buf *cypress_buf_alloc(unsigned int size);
  177. static void cypress_buf_free(struct cypress_buf *cb);
  178. static void cypress_buf_clear(struct cypress_buf *cb);
  179. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
  180. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
  181. static unsigned int cypress_buf_put(struct cypress_buf *cb,
  182. const char *buf, unsigned int count);
  183. static unsigned int cypress_buf_get(struct cypress_buf *cb,
  184. char *buf, unsigned int count);
  185. static struct usb_serial_driver cypress_earthmate_device = {
  186. .driver = {
  187. .owner = THIS_MODULE,
  188. .name = "earthmate",
  189. },
  190. .description = "DeLorme Earthmate USB",
  191. .usb_driver = &cypress_driver,
  192. .id_table = id_table_earthmate,
  193. .num_ports = 1,
  194. .attach = cypress_earthmate_startup,
  195. .release = cypress_release,
  196. .open = cypress_open,
  197. .close = cypress_close,
  198. .dtr_rts = cypress_dtr_rts,
  199. .write = cypress_write,
  200. .write_room = cypress_write_room,
  201. .ioctl = cypress_ioctl,
  202. .set_termios = cypress_set_termios,
  203. .tiocmget = cypress_tiocmget,
  204. .tiocmset = cypress_tiocmset,
  205. .chars_in_buffer = cypress_chars_in_buffer,
  206. .throttle = cypress_throttle,
  207. .unthrottle = cypress_unthrottle,
  208. .read_int_callback = cypress_read_int_callback,
  209. .write_int_callback = cypress_write_int_callback,
  210. };
  211. static struct usb_serial_driver cypress_hidcom_device = {
  212. .driver = {
  213. .owner = THIS_MODULE,
  214. .name = "cyphidcom",
  215. },
  216. .description = "HID->COM RS232 Adapter",
  217. .usb_driver = &cypress_driver,
  218. .id_table = id_table_cyphidcomrs232,
  219. .num_ports = 1,
  220. .attach = cypress_hidcom_startup,
  221. .release = cypress_release,
  222. .open = cypress_open,
  223. .close = cypress_close,
  224. .dtr_rts = cypress_dtr_rts,
  225. .write = cypress_write,
  226. .write_room = cypress_write_room,
  227. .ioctl = cypress_ioctl,
  228. .set_termios = cypress_set_termios,
  229. .tiocmget = cypress_tiocmget,
  230. .tiocmset = cypress_tiocmset,
  231. .chars_in_buffer = cypress_chars_in_buffer,
  232. .throttle = cypress_throttle,
  233. .unthrottle = cypress_unthrottle,
  234. .read_int_callback = cypress_read_int_callback,
  235. .write_int_callback = cypress_write_int_callback,
  236. };
  237. static struct usb_serial_driver cypress_ca42v2_device = {
  238. .driver = {
  239. .owner = THIS_MODULE,
  240. .name = "nokiaca42v2",
  241. },
  242. .description = "Nokia CA-42 V2 Adapter",
  243. .usb_driver = &cypress_driver,
  244. .id_table = id_table_nokiaca42v2,
  245. .num_ports = 1,
  246. .attach = cypress_ca42v2_startup,
  247. .release = cypress_release,
  248. .open = cypress_open,
  249. .close = cypress_close,
  250. .dtr_rts = cypress_dtr_rts,
  251. .write = cypress_write,
  252. .write_room = cypress_write_room,
  253. .ioctl = cypress_ioctl,
  254. .set_termios = cypress_set_termios,
  255. .tiocmget = cypress_tiocmget,
  256. .tiocmset = cypress_tiocmset,
  257. .chars_in_buffer = cypress_chars_in_buffer,
  258. .throttle = cypress_throttle,
  259. .unthrottle = cypress_unthrottle,
  260. .read_int_callback = cypress_read_int_callback,
  261. .write_int_callback = cypress_write_int_callback,
  262. };
  263. /*****************************************************************************
  264. * Cypress serial helper functions
  265. *****************************************************************************/
  266. static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
  267. {
  268. struct cypress_private *priv;
  269. priv = usb_get_serial_port_data(port);
  270. if (unstable_bauds)
  271. return new_rate;
  272. /*
  273. * The general purpose firmware for the Cypress M8 allows for
  274. * a maximum speed of 57600bps (I have no idea whether DeLorme
  275. * chose to use the general purpose firmware or not), if you
  276. * need to modify this speed setting for your own project
  277. * please add your own chiptype and modify the code likewise.
  278. * The Cypress HID->COM device will work successfully up to
  279. * 115200bps (but the actual throughput is around 3kBps).
  280. */
  281. if (port->serial->dev->speed == USB_SPEED_LOW) {
  282. /*
  283. * Mike Isely <isely@pobox.com> 2-Feb-2008: The
  284. * Cypress app note that describes this mechanism
  285. * states the the low-speed part can't handle more
  286. * than 800 bytes/sec, in which case 4800 baud is the
  287. * safest speed for a part like that.
  288. */
  289. if (new_rate > 4800) {
  290. dbg("%s - failed setting baud rate, device incapable "
  291. "speed %d", __func__, new_rate);
  292. return -1;
  293. }
  294. }
  295. switch (priv->chiptype) {
  296. case CT_EARTHMATE:
  297. if (new_rate <= 600) {
  298. /* 300 and 600 baud rates are supported under
  299. * the generic firmware, but are not used with
  300. * NMEA and SiRF protocols */
  301. dbg("%s - failed setting baud rate, unsupported speed "
  302. "of %d on Earthmate GPS", __func__, new_rate);
  303. return -1;
  304. }
  305. break;
  306. default:
  307. break;
  308. }
  309. return new_rate;
  310. }
  311. /* This function can either set or retrieve the current serial line settings */
  312. static int cypress_serial_control(struct tty_struct *tty,
  313. struct usb_serial_port *port, speed_t baud_rate, int data_bits,
  314. int stop_bits, int parity_enable, int parity_type, int reset,
  315. int cypress_request_type)
  316. {
  317. int new_baudrate = 0, retval = 0, tries = 0;
  318. struct cypress_private *priv;
  319. u8 *feature_buffer;
  320. const unsigned int feature_len = 5;
  321. unsigned long flags;
  322. dbg("%s", __func__);
  323. priv = usb_get_serial_port_data(port);
  324. if (!priv->comm_is_ok)
  325. return -ENODEV;
  326. feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
  327. if (!feature_buffer)
  328. return -ENOMEM;
  329. switch (cypress_request_type) {
  330. case CYPRESS_SET_CONFIG:
  331. /* 0 means 'Hang up' so doesn't change the true bit rate */
  332. new_baudrate = priv->baud_rate;
  333. if (baud_rate && baud_rate != priv->baud_rate) {
  334. dbg("%s - baud rate is changing", __func__);
  335. retval = analyze_baud_rate(port, baud_rate);
  336. if (retval >= 0) {
  337. new_baudrate = retval;
  338. dbg("%s - New baud rate set to %d",
  339. __func__, new_baudrate);
  340. }
  341. }
  342. dbg("%s - baud rate is being sent as %d",
  343. __func__, new_baudrate);
  344. /* fill the feature_buffer with new configuration */
  345. put_unaligned_le32(new_baudrate, feature_buffer);
  346. feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
  347. /* 1 bit gap */
  348. feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
  349. feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
  350. feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
  351. /* 1 bit gap */
  352. feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
  353. dbg("%s - device is being sent this feature report:",
  354. __func__);
  355. dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
  356. feature_buffer[0], feature_buffer[1],
  357. feature_buffer[2], feature_buffer[3],
  358. feature_buffer[4]);
  359. do {
  360. retval = usb_control_msg(port->serial->dev,
  361. usb_sndctrlpipe(port->serial->dev, 0),
  362. HID_REQ_SET_REPORT,
  363. USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  364. 0x0300, 0, feature_buffer,
  365. feature_len, 500);
  366. if (tries++ >= 3)
  367. break;
  368. } while (retval != feature_len &&
  369. retval != -ENODEV);
  370. if (retval != feature_len) {
  371. dev_err(&port->dev, "%s - failed sending serial "
  372. "line settings - %d\n", __func__, retval);
  373. cypress_set_dead(port);
  374. } else {
  375. spin_lock_irqsave(&priv->lock, flags);
  376. priv->baud_rate = new_baudrate;
  377. priv->current_config = feature_buffer[4];
  378. spin_unlock_irqrestore(&priv->lock, flags);
  379. /* If we asked for a speed change encode it */
  380. if (baud_rate)
  381. tty_encode_baud_rate(tty,
  382. new_baudrate, new_baudrate);
  383. }
  384. break;
  385. case CYPRESS_GET_CONFIG:
  386. if (priv->get_cfg_unsafe) {
  387. /* Not implemented for this device,
  388. and if we try to do it we're likely
  389. to crash the hardware. */
  390. retval = -ENOTTY;
  391. goto out;
  392. }
  393. dbg("%s - retreiving serial line settings", __func__);
  394. do {
  395. retval = usb_control_msg(port->serial->dev,
  396. usb_rcvctrlpipe(port->serial->dev, 0),
  397. HID_REQ_GET_REPORT,
  398. USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  399. 0x0300, 0, feature_buffer,
  400. feature_len, 500);
  401. if (tries++ >= 3)
  402. break;
  403. } while (retval != feature_len
  404. && retval != -ENODEV);
  405. if (retval != feature_len) {
  406. dev_err(&port->dev, "%s - failed to retrieve serial "
  407. "line settings - %d\n", __func__, retval);
  408. cypress_set_dead(port);
  409. goto out;
  410. } else {
  411. spin_lock_irqsave(&priv->lock, flags);
  412. /* store the config in one byte, and later
  413. use bit masks to check values */
  414. priv->current_config = feature_buffer[4];
  415. priv->baud_rate = get_unaligned_le32(feature_buffer);
  416. spin_unlock_irqrestore(&priv->lock, flags);
  417. }
  418. }
  419. spin_lock_irqsave(&priv->lock, flags);
  420. ++priv->cmd_count;
  421. spin_unlock_irqrestore(&priv->lock, flags);
  422. out:
  423. kfree(feature_buffer);
  424. return retval;
  425. } /* cypress_serial_control */
  426. static void cypress_set_dead(struct usb_serial_port *port)
  427. {
  428. struct cypress_private *priv = usb_get_serial_port_data(port);
  429. unsigned long flags;
  430. spin_lock_irqsave(&priv->lock, flags);
  431. if (!priv->comm_is_ok) {
  432. spin_unlock_irqrestore(&priv->lock, flags);
  433. return;
  434. }
  435. priv->comm_is_ok = 0;
  436. spin_unlock_irqrestore(&priv->lock, flags);
  437. dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
  438. "interval might be too short\n", port->number);
  439. }
  440. /*****************************************************************************
  441. * Cypress serial driver functions
  442. *****************************************************************************/
  443. static int generic_startup(struct usb_serial *serial)
  444. {
  445. struct cypress_private *priv;
  446. struct usb_serial_port *port = serial->port[0];
  447. dbg("%s - port %d", __func__, port->number);
  448. priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
  449. if (!priv)
  450. return -ENOMEM;
  451. priv->comm_is_ok = !0;
  452. spin_lock_init(&priv->lock);
  453. priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
  454. if (priv->buf == NULL) {
  455. kfree(priv);
  456. return -ENOMEM;
  457. }
  458. init_waitqueue_head(&priv->delta_msr_wait);
  459. usb_reset_configuration(serial->dev);
  460. priv->cmd_ctrl = 0;
  461. priv->line_control = 0;
  462. priv->termios_initialized = 0;
  463. priv->rx_flags = 0;
  464. /* Default packet format setting is determined by packet size.
  465. Anything with a size larger then 9 must have a separate
  466. count field since the 3 bit count field is otherwise too
  467. small. Otherwise we can use the slightly more compact
  468. format. This is in accordance with the cypress_m8 serial
  469. converter app note. */
  470. if (port->interrupt_out_size > 9)
  471. priv->pkt_fmt = packet_format_1;
  472. else
  473. priv->pkt_fmt = packet_format_2;
  474. if (interval > 0) {
  475. priv->write_urb_interval = interval;
  476. priv->read_urb_interval = interval;
  477. dbg("%s - port %d read & write intervals forced to %d",
  478. __func__, port->number, interval);
  479. } else {
  480. priv->write_urb_interval = port->interrupt_out_urb->interval;
  481. priv->read_urb_interval = port->interrupt_in_urb->interval;
  482. dbg("%s - port %d intervals: read=%d write=%d",
  483. __func__, port->number,
  484. priv->read_urb_interval, priv->write_urb_interval);
  485. }
  486. usb_set_serial_port_data(port, priv);
  487. return 0;
  488. }
  489. static int cypress_earthmate_startup(struct usb_serial *serial)
  490. {
  491. struct cypress_private *priv;
  492. struct usb_serial_port *port = serial->port[0];
  493. dbg("%s", __func__);
  494. if (generic_startup(serial)) {
  495. dbg("%s - Failed setting up port %d", __func__,
  496. port->number);
  497. return 1;
  498. }
  499. priv = usb_get_serial_port_data(port);
  500. priv->chiptype = CT_EARTHMATE;
  501. /* All Earthmate devices use the separated-count packet
  502. format! Idiotic. */
  503. priv->pkt_fmt = packet_format_1;
  504. if (serial->dev->descriptor.idProduct !=
  505. cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
  506. /* The old original USB Earthmate seemed able to
  507. handle GET_CONFIG requests; everything they've
  508. produced since that time crashes if this command is
  509. attempted :-( */
  510. dbg("%s - Marking this device as unsafe for GET_CONFIG "
  511. "commands", __func__);
  512. priv->get_cfg_unsafe = !0;
  513. }
  514. return 0;
  515. } /* cypress_earthmate_startup */
  516. static int cypress_hidcom_startup(struct usb_serial *serial)
  517. {
  518. struct cypress_private *priv;
  519. dbg("%s", __func__);
  520. if (generic_startup(serial)) {
  521. dbg("%s - Failed setting up port %d", __func__,
  522. serial->port[0]->number);
  523. return 1;
  524. }
  525. priv = usb_get_serial_port_data(serial->port[0]);
  526. priv->chiptype = CT_CYPHIDCOM;
  527. return 0;
  528. } /* cypress_hidcom_startup */
  529. static int cypress_ca42v2_startup(struct usb_serial *serial)
  530. {
  531. struct cypress_private *priv;
  532. dbg("%s", __func__);
  533. if (generic_startup(serial)) {
  534. dbg("%s - Failed setting up port %d", __func__,
  535. serial->port[0]->number);
  536. return 1;
  537. }
  538. priv = usb_get_serial_port_data(serial->port[0]);
  539. priv->chiptype = CT_CA42V2;
  540. return 0;
  541. } /* cypress_ca42v2_startup */
  542. static void cypress_release(struct usb_serial *serial)
  543. {
  544. struct cypress_private *priv;
  545. dbg("%s - port %d", __func__, serial->port[0]->number);
  546. /* all open ports are closed at this point */
  547. priv = usb_get_serial_port_data(serial->port[0]);
  548. if (priv) {
  549. cypress_buf_free(priv->buf);
  550. kfree(priv);
  551. }
  552. }
  553. static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
  554. {
  555. struct cypress_private *priv = usb_get_serial_port_data(port);
  556. struct usb_serial *serial = port->serial;
  557. unsigned long flags;
  558. int result = 0;
  559. dbg("%s - port %d", __func__, port->number);
  560. if (!priv->comm_is_ok)
  561. return -EIO;
  562. /* clear halts before open */
  563. usb_clear_halt(serial->dev, 0x81);
  564. usb_clear_halt(serial->dev, 0x02);
  565. spin_lock_irqsave(&priv->lock, flags);
  566. /* reset read/write statistics */
  567. priv->bytes_in = 0;
  568. priv->bytes_out = 0;
  569. priv->cmd_count = 0;
  570. priv->rx_flags = 0;
  571. spin_unlock_irqrestore(&priv->lock, flags);
  572. /* Set termios */
  573. cypress_send(port);
  574. if (tty)
  575. cypress_set_termios(tty, port, &priv->tmp_termios);
  576. /* setup the port and start reading from the device */
  577. if (!port->interrupt_in_urb) {
  578. dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
  579. __func__);
  580. return -1;
  581. }
  582. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  583. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  584. port->interrupt_in_urb->transfer_buffer,
  585. port->interrupt_in_urb->transfer_buffer_length,
  586. cypress_read_int_callback, port, priv->read_urb_interval);
  587. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  588. if (result) {
  589. dev_err(&port->dev,
  590. "%s - failed submitting read urb, error %d\n",
  591. __func__, result);
  592. cypress_set_dead(port);
  593. }
  594. port->port.drain_delay = 256;
  595. return result;
  596. } /* cypress_open */
  597. static void cypress_dtr_rts(struct usb_serial_port *port, int on)
  598. {
  599. struct cypress_private *priv = usb_get_serial_port_data(port);
  600. /* drop dtr and rts */
  601. priv = usb_get_serial_port_data(port);
  602. spin_lock_irq(&priv->lock);
  603. if (on == 0)
  604. priv->line_control = 0;
  605. else
  606. priv->line_control = CONTROL_DTR | CONTROL_RTS;
  607. priv->cmd_ctrl = 1;
  608. spin_unlock_irq(&priv->lock);
  609. cypress_write(NULL, port, NULL, 0);
  610. }
  611. static void cypress_close(struct usb_serial_port *port)
  612. {
  613. struct cypress_private *priv = usb_get_serial_port_data(port);
  614. dbg("%s - port %d", __func__, port->number);
  615. /* writing is potentially harmful, lock must be taken */
  616. mutex_lock(&port->serial->disc_mutex);
  617. if (port->serial->disconnected) {
  618. mutex_unlock(&port->serial->disc_mutex);
  619. return;
  620. }
  621. cypress_buf_clear(priv->buf);
  622. dbg("%s - stopping urbs", __func__);
  623. usb_kill_urb(port->interrupt_in_urb);
  624. usb_kill_urb(port->interrupt_out_urb);
  625. if (stats)
  626. dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
  627. priv->bytes_in, priv->bytes_out, priv->cmd_count);
  628. mutex_unlock(&port->serial->disc_mutex);
  629. } /* cypress_close */
  630. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  631. const unsigned char *buf, int count)
  632. {
  633. struct cypress_private *priv = usb_get_serial_port_data(port);
  634. unsigned long flags;
  635. dbg("%s - port %d, %d bytes", __func__, port->number, count);
  636. /* line control commands, which need to be executed immediately,
  637. are not put into the buffer for obvious reasons.
  638. */
  639. if (priv->cmd_ctrl) {
  640. count = 0;
  641. goto finish;
  642. }
  643. if (!count)
  644. return count;
  645. spin_lock_irqsave(&priv->lock, flags);
  646. count = cypress_buf_put(priv->buf, buf, count);
  647. spin_unlock_irqrestore(&priv->lock, flags);
  648. finish:
  649. cypress_send(port);
  650. return count;
  651. } /* cypress_write */
  652. static void cypress_send(struct usb_serial_port *port)
  653. {
  654. int count = 0, result, offset, actual_size;
  655. struct cypress_private *priv = usb_get_serial_port_data(port);
  656. unsigned long flags;
  657. if (!priv->comm_is_ok)
  658. return;
  659. dbg("%s - port %d", __func__, port->number);
  660. dbg("%s - interrupt out size is %d", __func__,
  661. port->interrupt_out_size);
  662. spin_lock_irqsave(&priv->lock, flags);
  663. if (priv->write_urb_in_use) {
  664. dbg("%s - can't write, urb in use", __func__);
  665. spin_unlock_irqrestore(&priv->lock, flags);
  666. return;
  667. }
  668. spin_unlock_irqrestore(&priv->lock, flags);
  669. /* clear buffer */
  670. memset(port->interrupt_out_urb->transfer_buffer, 0,
  671. port->interrupt_out_size);
  672. spin_lock_irqsave(&priv->lock, flags);
  673. switch (priv->pkt_fmt) {
  674. default:
  675. case packet_format_1:
  676. /* this is for the CY7C64013... */
  677. offset = 2;
  678. port->interrupt_out_buffer[0] = priv->line_control;
  679. break;
  680. case packet_format_2:
  681. /* this is for the CY7C63743... */
  682. offset = 1;
  683. port->interrupt_out_buffer[0] = priv->line_control;
  684. break;
  685. }
  686. if (priv->line_control & CONTROL_RESET)
  687. priv->line_control &= ~CONTROL_RESET;
  688. if (priv->cmd_ctrl) {
  689. priv->cmd_count++;
  690. dbg("%s - line control command being issued", __func__);
  691. spin_unlock_irqrestore(&priv->lock, flags);
  692. goto send;
  693. } else
  694. spin_unlock_irqrestore(&priv->lock, flags);
  695. count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
  696. port->interrupt_out_size-offset);
  697. if (count == 0)
  698. return;
  699. switch (priv->pkt_fmt) {
  700. default:
  701. case packet_format_1:
  702. port->interrupt_out_buffer[1] = count;
  703. break;
  704. case packet_format_2:
  705. port->interrupt_out_buffer[0] |= count;
  706. }
  707. dbg("%s - count is %d", __func__, count);
  708. send:
  709. spin_lock_irqsave(&priv->lock, flags);
  710. priv->write_urb_in_use = 1;
  711. spin_unlock_irqrestore(&priv->lock, flags);
  712. if (priv->cmd_ctrl)
  713. actual_size = 1;
  714. else
  715. actual_size = count +
  716. (priv->pkt_fmt == packet_format_1 ? 2 : 1);
  717. usb_serial_debug_data(debug, &port->dev, __func__,
  718. port->interrupt_out_size,
  719. port->interrupt_out_urb->transfer_buffer);
  720. usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
  721. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  722. port->interrupt_out_buffer, port->interrupt_out_size,
  723. cypress_write_int_callback, port, priv->write_urb_interval);
  724. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  725. if (result) {
  726. dev_err(&port->dev,
  727. "%s - failed submitting write urb, error %d\n",
  728. __func__, result);
  729. priv->write_urb_in_use = 0;
  730. cypress_set_dead(port);
  731. }
  732. spin_lock_irqsave(&priv->lock, flags);
  733. if (priv->cmd_ctrl)
  734. priv->cmd_ctrl = 0;
  735. /* do not count the line control and size bytes */
  736. priv->bytes_out += count;
  737. spin_unlock_irqrestore(&priv->lock, flags);
  738. usb_serial_port_softint(port);
  739. } /* cypress_send */
  740. /* returns how much space is available in the soft buffer */
  741. static int cypress_write_room(struct tty_struct *tty)
  742. {
  743. struct usb_serial_port *port = tty->driver_data;
  744. struct cypress_private *priv = usb_get_serial_port_data(port);
  745. int room = 0;
  746. unsigned long flags;
  747. dbg("%s - port %d", __func__, port->number);
  748. spin_lock_irqsave(&priv->lock, flags);
  749. room = cypress_buf_space_avail(priv->buf);
  750. spin_unlock_irqrestore(&priv->lock, flags);
  751. dbg("%s - returns %d", __func__, room);
  752. return room;
  753. }
  754. static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
  755. {
  756. struct usb_serial_port *port = tty->driver_data;
  757. struct cypress_private *priv = usb_get_serial_port_data(port);
  758. __u8 status, control;
  759. unsigned int result = 0;
  760. unsigned long flags;
  761. dbg("%s - port %d", __func__, port->number);
  762. spin_lock_irqsave(&priv->lock, flags);
  763. control = priv->line_control;
  764. status = priv->current_status;
  765. spin_unlock_irqrestore(&priv->lock, flags);
  766. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  767. | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  768. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  769. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  770. | ((status & UART_RI) ? TIOCM_RI : 0)
  771. | ((status & UART_CD) ? TIOCM_CD : 0);
  772. dbg("%s - result = %x", __func__, result);
  773. return result;
  774. }
  775. static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
  776. unsigned int set, unsigned int clear)
  777. {
  778. struct usb_serial_port *port = tty->driver_data;
  779. struct cypress_private *priv = usb_get_serial_port_data(port);
  780. unsigned long flags;
  781. dbg("%s - port %d", __func__, port->number);
  782. spin_lock_irqsave(&priv->lock, flags);
  783. if (set & TIOCM_RTS)
  784. priv->line_control |= CONTROL_RTS;
  785. if (set & TIOCM_DTR)
  786. priv->line_control |= CONTROL_DTR;
  787. if (clear & TIOCM_RTS)
  788. priv->line_control &= ~CONTROL_RTS;
  789. if (clear & TIOCM_DTR)
  790. priv->line_control &= ~CONTROL_DTR;
  791. priv->cmd_ctrl = 1;
  792. spin_unlock_irqrestore(&priv->lock, flags);
  793. return cypress_write(tty, port, NULL, 0);
  794. }
  795. static int cypress_ioctl(struct tty_struct *tty, struct file *file,
  796. unsigned int cmd, unsigned long arg)
  797. {
  798. struct usb_serial_port *port = tty->driver_data;
  799. struct cypress_private *priv = usb_get_serial_port_data(port);
  800. dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
  801. switch (cmd) {
  802. /* This code comes from drivers/char/serial.c and ftdi_sio.c */
  803. case TIOCMIWAIT:
  804. while (priv != NULL) {
  805. interruptible_sleep_on(&priv->delta_msr_wait);
  806. /* see if a signal did it */
  807. if (signal_pending(current))
  808. return -ERESTARTSYS;
  809. else {
  810. char diff = priv->diff_status;
  811. if (diff == 0)
  812. return -EIO; /* no change => error */
  813. /* consume all events */
  814. priv->diff_status = 0;
  815. /* return 0 if caller wanted to know about
  816. these bits */
  817. if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
  818. ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
  819. ((arg & TIOCM_CD) && (diff & UART_CD)) ||
  820. ((arg & TIOCM_CTS) && (diff & UART_CTS)))
  821. return 0;
  822. /* otherwise caller can't care less about what
  823. * happened, and so we continue to wait for
  824. * more events.
  825. */
  826. }
  827. }
  828. return 0;
  829. default:
  830. break;
  831. }
  832. dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
  833. return -ENOIOCTLCMD;
  834. } /* cypress_ioctl */
  835. static void cypress_set_termios(struct tty_struct *tty,
  836. struct usb_serial_port *port, struct ktermios *old_termios)
  837. {
  838. struct cypress_private *priv = usb_get_serial_port_data(port);
  839. int data_bits, stop_bits, parity_type, parity_enable;
  840. unsigned cflag, iflag;
  841. unsigned long flags;
  842. __u8 oldlines;
  843. int linechange = 0;
  844. dbg("%s - port %d", __func__, port->number);
  845. spin_lock_irqsave(&priv->lock, flags);
  846. /* We can't clean this one up as we don't know the device type
  847. early enough */
  848. if (!priv->termios_initialized) {
  849. if (priv->chiptype == CT_EARTHMATE) {
  850. *(tty->termios) = tty_std_termios;
  851. tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
  852. CLOCAL;
  853. tty->termios->c_ispeed = 4800;
  854. tty->termios->c_ospeed = 4800;
  855. } else if (priv->chiptype == CT_CYPHIDCOM) {
  856. *(tty->termios) = tty_std_termios;
  857. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  858. CLOCAL;
  859. tty->termios->c_ispeed = 9600;
  860. tty->termios->c_ospeed = 9600;
  861. } else if (priv->chiptype == CT_CA42V2) {
  862. *(tty->termios) = tty_std_termios;
  863. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  864. CLOCAL;
  865. tty->termios->c_ispeed = 9600;
  866. tty->termios->c_ospeed = 9600;
  867. }
  868. priv->termios_initialized = 1;
  869. }
  870. spin_unlock_irqrestore(&priv->lock, flags);
  871. /* Unsupported features need clearing */
  872. tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
  873. cflag = tty->termios->c_cflag;
  874. iflag = tty->termios->c_iflag;
  875. /* check if there are new settings */
  876. if (old_termios) {
  877. spin_lock_irqsave(&priv->lock, flags);
  878. priv->tmp_termios = *(tty->termios);
  879. spin_unlock_irqrestore(&priv->lock, flags);
  880. }
  881. /* set number of data bits, parity, stop bits */
  882. /* when parity is disabled the parity type bit is ignored */
  883. /* 1 means 2 stop bits, 0 means 1 stop bit */
  884. stop_bits = cflag & CSTOPB ? 1 : 0;
  885. if (cflag & PARENB) {
  886. parity_enable = 1;
  887. /* 1 means odd parity, 0 means even parity */
  888. parity_type = cflag & PARODD ? 1 : 0;
  889. } else
  890. parity_enable = parity_type = 0;
  891. switch (cflag & CSIZE) {
  892. case CS5:
  893. data_bits = 0;
  894. break;
  895. case CS6:
  896. data_bits = 1;
  897. break;
  898. case CS7:
  899. data_bits = 2;
  900. break;
  901. case CS8:
  902. data_bits = 3;
  903. break;
  904. default:
  905. dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
  906. __func__);
  907. data_bits = 3;
  908. }
  909. spin_lock_irqsave(&priv->lock, flags);
  910. oldlines = priv->line_control;
  911. if ((cflag & CBAUD) == B0) {
  912. /* drop dtr and rts */
  913. dbg("%s - dropping the lines, baud rate 0bps", __func__);
  914. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  915. } else
  916. priv->line_control = (CONTROL_DTR | CONTROL_RTS);
  917. spin_unlock_irqrestore(&priv->lock, flags);
  918. dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
  919. "%d data_bits (+5)", __func__, stop_bits,
  920. parity_enable, parity_type, data_bits);
  921. cypress_serial_control(tty, port, tty_get_baud_rate(tty),
  922. data_bits, stop_bits,
  923. parity_enable, parity_type,
  924. 0, CYPRESS_SET_CONFIG);
  925. /* we perform a CYPRESS_GET_CONFIG so that the current settings are
  926. * filled into the private structure this should confirm that all is
  927. * working if it returns what we just set */
  928. cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
  929. /* Here we can define custom tty settings for devices; the main tty
  930. * termios flag base comes from empeg.c */
  931. spin_lock_irqsave(&priv->lock, flags);
  932. if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
  933. dbg("Using custom termios settings for a baud rate of "
  934. "4800bps.");
  935. /* define custom termios settings for NMEA protocol */
  936. tty->termios->c_iflag /* input modes - */
  937. &= ~(IGNBRK /* disable ignore break */
  938. | BRKINT /* disable break causes interrupt */
  939. | PARMRK /* disable mark parity errors */
  940. | ISTRIP /* disable clear high bit of input char */
  941. | INLCR /* disable translate NL to CR */
  942. | IGNCR /* disable ignore CR */
  943. | ICRNL /* disable translate CR to NL */
  944. | IXON); /* disable enable XON/XOFF flow control */
  945. tty->termios->c_oflag /* output modes */
  946. &= ~OPOST; /* disable postprocess output char */
  947. tty->termios->c_lflag /* line discipline modes */
  948. &= ~(ECHO /* disable echo input characters */
  949. | ECHONL /* disable echo new line */
  950. | ICANON /* disable erase, kill, werase, and rprnt
  951. special characters */
  952. | ISIG /* disable interrupt, quit, and suspend
  953. special characters */
  954. | IEXTEN); /* disable non-POSIX special characters */
  955. } /* CT_CYPHIDCOM: Application should handle this for device */
  956. linechange = (priv->line_control != oldlines);
  957. spin_unlock_irqrestore(&priv->lock, flags);
  958. /* if necessary, set lines */
  959. if (linechange) {
  960. priv->cmd_ctrl = 1;
  961. cypress_write(tty, port, NULL, 0);
  962. }
  963. } /* cypress_set_termios */
  964. /* returns amount of data still left in soft buffer */
  965. static int cypress_chars_in_buffer(struct tty_struct *tty)
  966. {
  967. struct usb_serial_port *port = tty->driver_data;
  968. struct cypress_private *priv = usb_get_serial_port_data(port);
  969. int chars = 0;
  970. unsigned long flags;
  971. dbg("%s - port %d", __func__, port->number);
  972. spin_lock_irqsave(&priv->lock, flags);
  973. chars = cypress_buf_data_avail(priv->buf);
  974. spin_unlock_irqrestore(&priv->lock, flags);
  975. dbg("%s - returns %d", __func__, chars);
  976. return chars;
  977. }
  978. static void cypress_throttle(struct tty_struct *tty)
  979. {
  980. struct usb_serial_port *port = tty->driver_data;
  981. struct cypress_private *priv = usb_get_serial_port_data(port);
  982. dbg("%s - port %d", __func__, port->number);
  983. spin_lock_irq(&priv->lock);
  984. priv->rx_flags = THROTTLED;
  985. spin_unlock_irq(&priv->lock);
  986. }
  987. static void cypress_unthrottle(struct tty_struct *tty)
  988. {
  989. struct usb_serial_port *port = tty->driver_data;
  990. struct cypress_private *priv = usb_get_serial_port_data(port);
  991. int actually_throttled, result;
  992. dbg("%s - port %d", __func__, port->number);
  993. spin_lock_irq(&priv->lock);
  994. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  995. priv->rx_flags = 0;
  996. spin_unlock_irq(&priv->lock);
  997. if (!priv->comm_is_ok)
  998. return;
  999. if (actually_throttled) {
  1000. port->interrupt_in_urb->dev = port->serial->dev;
  1001. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  1002. if (result) {
  1003. dev_err(&port->dev, "%s - failed submitting read urb, "
  1004. "error %d\n", __func__, result);
  1005. cypress_set_dead(port);
  1006. }
  1007. }
  1008. }
  1009. static void cypress_read_int_callback(struct urb *urb)
  1010. {
  1011. struct usb_serial_port *port = urb->context;
  1012. struct cypress_private *priv = usb_get_serial_port_data(port);
  1013. struct tty_struct *tty;
  1014. unsigned char *data = urb->transfer_buffer;
  1015. unsigned long flags;
  1016. char tty_flag = TTY_NORMAL;
  1017. int havedata = 0;
  1018. int bytes = 0;
  1019. int result;
  1020. int i = 0;
  1021. int status = urb->status;
  1022. dbg("%s - port %d", __func__, port->number);
  1023. switch (status) {
  1024. case 0: /* success */
  1025. break;
  1026. case -ECONNRESET:
  1027. case -ENOENT:
  1028. case -ESHUTDOWN:
  1029. /* precursor to disconnect so just go away */
  1030. return;
  1031. case -EPIPE:
  1032. /* Can't call usb_clear_halt while in_interrupt */
  1033. /* FALLS THROUGH */
  1034. default:
  1035. /* something ugly is going on... */
  1036. dev_err(&urb->dev->dev,
  1037. "%s - unexpected nonzero read status received: %d\n",
  1038. __func__, status);
  1039. cypress_set_dead(port);
  1040. return;
  1041. }
  1042. spin_lock_irqsave(&priv->lock, flags);
  1043. if (priv->rx_flags & THROTTLED) {
  1044. dbg("%s - now throttling", __func__);
  1045. priv->rx_flags |= ACTUALLY_THROTTLED;
  1046. spin_unlock_irqrestore(&priv->lock, flags);
  1047. return;
  1048. }
  1049. spin_unlock_irqrestore(&priv->lock, flags);
  1050. tty = tty_port_tty_get(&port->port);
  1051. if (!tty) {
  1052. dbg("%s - bad tty pointer - exiting", __func__);
  1053. return;
  1054. }
  1055. spin_lock_irqsave(&priv->lock, flags);
  1056. result = urb->actual_length;
  1057. switch (priv->pkt_fmt) {
  1058. default:
  1059. case packet_format_1:
  1060. /* This is for the CY7C64013... */
  1061. priv->current_status = data[0] & 0xF8;
  1062. bytes = data[1] + 2;
  1063. i = 2;
  1064. if (bytes > 2)
  1065. havedata = 1;
  1066. break;
  1067. case packet_format_2:
  1068. /* This is for the CY7C63743... */
  1069. priv->current_status = data[0] & 0xF8;
  1070. bytes = (data[0] & 0x07) + 1;
  1071. i = 1;
  1072. if (bytes > 1)
  1073. havedata = 1;
  1074. break;
  1075. }
  1076. spin_unlock_irqrestore(&priv->lock, flags);
  1077. if (result < bytes) {
  1078. dbg("%s - wrong packet size - received %d bytes but packet "
  1079. "said %d bytes", __func__, result, bytes);
  1080. goto continue_read;
  1081. }
  1082. usb_serial_debug_data(debug, &port->dev, __func__,
  1083. urb->actual_length, data);
  1084. spin_lock_irqsave(&priv->lock, flags);
  1085. /* check to see if status has changed */
  1086. if (priv->current_status != priv->prev_status) {
  1087. priv->diff_status |= priv->current_status ^
  1088. priv->prev_status;
  1089. wake_up_interruptible(&priv->delta_msr_wait);
  1090. priv->prev_status = priv->current_status;
  1091. }
  1092. spin_unlock_irqrestore(&priv->lock, flags);
  1093. /* hangup, as defined in acm.c... this might be a bad place for it
  1094. * though */
  1095. if (tty && !(tty->termios->c_cflag & CLOCAL) &&
  1096. !(priv->current_status & UART_CD)) {
  1097. dbg("%s - calling hangup", __func__);
  1098. tty_hangup(tty);
  1099. goto continue_read;
  1100. }
  1101. /* There is one error bit... I'm assuming it is a parity error
  1102. * indicator as the generic firmware will set this bit to 1 if a
  1103. * parity error occurs.
  1104. * I can not find reference to any other error events. */
  1105. spin_lock_irqsave(&priv->lock, flags);
  1106. if (priv->current_status & CYP_ERROR) {
  1107. spin_unlock_irqrestore(&priv->lock, flags);
  1108. tty_flag = TTY_PARITY;
  1109. dbg("%s - Parity Error detected", __func__);
  1110. } else
  1111. spin_unlock_irqrestore(&priv->lock, flags);
  1112. /* process read if there is data other than line status */
  1113. if (tty && (bytes > i)) {
  1114. bytes = tty_buffer_request_room(tty, bytes);
  1115. for (; i < bytes ; ++i) {
  1116. dbg("pushing byte number %d - %d - %c", i, data[i],
  1117. data[i]);
  1118. tty_insert_flip_char(tty, data[i], tty_flag);
  1119. }
  1120. tty_flip_buffer_push(tty);
  1121. }
  1122. spin_lock_irqsave(&priv->lock, flags);
  1123. /* control and status byte(s) are also counted */
  1124. priv->bytes_in += bytes;
  1125. spin_unlock_irqrestore(&priv->lock, flags);
  1126. continue_read:
  1127. tty_kref_put(tty);
  1128. /* Continue trying to always read... unless the port has closed. */
  1129. if (port->port.count > 0 && priv->comm_is_ok) {
  1130. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  1131. usb_rcvintpipe(port->serial->dev,
  1132. port->interrupt_in_endpointAddress),
  1133. port->interrupt_in_urb->transfer_buffer,
  1134. port->interrupt_in_urb->transfer_buffer_length,
  1135. cypress_read_int_callback, port,
  1136. priv->read_urb_interval);
  1137. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  1138. if (result) {
  1139. dev_err(&urb->dev->dev, "%s - failed resubmitting "
  1140. "read urb, error %d\n", __func__,
  1141. result);
  1142. cypress_set_dead(port);
  1143. }
  1144. }
  1145. return;
  1146. } /* cypress_read_int_callback */
  1147. static void cypress_write_int_callback(struct urb *urb)
  1148. {
  1149. struct usb_serial_port *port = urb->context;
  1150. struct cypress_private *priv = usb_get_serial_port_data(port);
  1151. int result;
  1152. int status = urb->status;
  1153. dbg("%s - port %d", __func__, port->number);
  1154. switch (status) {
  1155. case 0:
  1156. /* success */
  1157. break;
  1158. case -ECONNRESET:
  1159. case -ENOENT:
  1160. case -ESHUTDOWN:
  1161. /* this urb is terminated, clean up */
  1162. dbg("%s - urb shutting down with status: %d",
  1163. __func__, status);
  1164. priv->write_urb_in_use = 0;
  1165. return;
  1166. case -EPIPE: /* no break needed; clear halt and resubmit */
  1167. if (!priv->comm_is_ok)
  1168. break;
  1169. usb_clear_halt(port->serial->dev, 0x02);
  1170. /* error in the urb, so we have to resubmit it */
  1171. dbg("%s - nonzero write bulk status received: %d",
  1172. __func__, status);
  1173. port->interrupt_out_urb->transfer_buffer_length = 1;
  1174. port->interrupt_out_urb->dev = port->serial->dev;
  1175. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  1176. if (!result)
  1177. return;
  1178. dev_err(&urb->dev->dev,
  1179. "%s - failed resubmitting write urb, error %d\n",
  1180. __func__, result);
  1181. cypress_set_dead(port);
  1182. break;
  1183. default:
  1184. dev_err(&urb->dev->dev,
  1185. "%s - unexpected nonzero write status received: %d\n",
  1186. __func__, status);
  1187. cypress_set_dead(port);
  1188. break;
  1189. }
  1190. priv->write_urb_in_use = 0;
  1191. /* send any buffered data */
  1192. cypress_send(port);
  1193. }
  1194. /*****************************************************************************
  1195. * Write buffer functions - buffering code from pl2303 used
  1196. *****************************************************************************/
  1197. /*
  1198. * cypress_buf_alloc
  1199. *
  1200. * Allocate a circular buffer and all associated memory.
  1201. */
  1202. static struct cypress_buf *cypress_buf_alloc(unsigned int size)
  1203. {
  1204. struct cypress_buf *cb;
  1205. if (size == 0)
  1206. return NULL;
  1207. cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
  1208. if (cb == NULL)
  1209. return NULL;
  1210. cb->buf_buf = kmalloc(size, GFP_KERNEL);
  1211. if (cb->buf_buf == NULL) {
  1212. kfree(cb);
  1213. return NULL;
  1214. }
  1215. cb->buf_size = size;
  1216. cb->buf_get = cb->buf_put = cb->buf_buf;
  1217. return cb;
  1218. }
  1219. /*
  1220. * cypress_buf_free
  1221. *
  1222. * Free the buffer and all associated memory.
  1223. */
  1224. static void cypress_buf_free(struct cypress_buf *cb)
  1225. {
  1226. if (cb) {
  1227. kfree(cb->buf_buf);
  1228. kfree(cb);
  1229. }
  1230. }
  1231. /*
  1232. * cypress_buf_clear
  1233. *
  1234. * Clear out all data in the circular buffer.
  1235. */
  1236. static void cypress_buf_clear(struct cypress_buf *cb)
  1237. {
  1238. if (cb != NULL)
  1239. cb->buf_get = cb->buf_put;
  1240. /* equivalent to a get of all data available */
  1241. }
  1242. /*
  1243. * cypress_buf_data_avail
  1244. *
  1245. * Return the number of bytes of data available in the circular
  1246. * buffer.
  1247. */
  1248. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
  1249. {
  1250. if (cb != NULL)
  1251. return (cb->buf_size + cb->buf_put - cb->buf_get)
  1252. % cb->buf_size;
  1253. else
  1254. return 0;
  1255. }
  1256. /*
  1257. * cypress_buf_space_avail
  1258. *
  1259. * Return the number of bytes of space available in the circular
  1260. * buffer.
  1261. */
  1262. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
  1263. {
  1264. if (cb != NULL)
  1265. return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
  1266. % cb->buf_size;
  1267. else
  1268. return 0;
  1269. }
  1270. /*
  1271. * cypress_buf_put
  1272. *
  1273. * Copy data data from a user buffer and put it into the circular buffer.
  1274. * Restrict to the amount of space available.
  1275. *
  1276. * Return the number of bytes copied.
  1277. */
  1278. static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
  1279. unsigned int count)
  1280. {
  1281. unsigned int len;
  1282. if (cb == NULL)
  1283. return 0;
  1284. len = cypress_buf_space_avail(cb);
  1285. if (count > len)
  1286. count = len;
  1287. if (count == 0)
  1288. return 0;
  1289. len = cb->buf_buf + cb->buf_size - cb->buf_put;
  1290. if (count > len) {
  1291. memcpy(cb->buf_put, buf, len);
  1292. memcpy(cb->buf_buf, buf+len, count - len);
  1293. cb->buf_put = cb->buf_buf + count - len;
  1294. } else {
  1295. memcpy(cb->buf_put, buf, count);
  1296. if (count < len)
  1297. cb->buf_put += count;
  1298. else /* count == len */
  1299. cb->buf_put = cb->buf_buf;
  1300. }
  1301. return count;
  1302. }
  1303. /*
  1304. * cypress_buf_get
  1305. *
  1306. * Get data from the circular buffer and copy to the given buffer.
  1307. * Restrict to the amount of data available.
  1308. *
  1309. * Return the number of bytes copied.
  1310. */
  1311. static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
  1312. unsigned int count)
  1313. {
  1314. unsigned int len;
  1315. if (cb == NULL)
  1316. return 0;
  1317. len = cypress_buf_data_avail(cb);
  1318. if (count > len)
  1319. count = len;
  1320. if (count == 0)
  1321. return 0;
  1322. len = cb->buf_buf + cb->buf_size - cb->buf_get;
  1323. if (count > len) {
  1324. memcpy(buf, cb->buf_get, len);
  1325. memcpy(buf+len, cb->buf_buf, count - len);
  1326. cb->buf_get = cb->buf_buf + count - len;
  1327. } else {
  1328. memcpy(buf, cb->buf_get, count);
  1329. if (count < len)
  1330. cb->buf_get += count;
  1331. else /* count == len */
  1332. cb->buf_get = cb->buf_buf;
  1333. }
  1334. return count;
  1335. }
  1336. /*****************************************************************************
  1337. * Module functions
  1338. *****************************************************************************/
  1339. static int __init cypress_init(void)
  1340. {
  1341. int retval;
  1342. dbg("%s", __func__);
  1343. retval = usb_serial_register(&cypress_earthmate_device);
  1344. if (retval)
  1345. goto failed_em_register;
  1346. retval = usb_serial_register(&cypress_hidcom_device);
  1347. if (retval)
  1348. goto failed_hidcom_register;
  1349. retval = usb_serial_register(&cypress_ca42v2_device);
  1350. if (retval)
  1351. goto failed_ca42v2_register;
  1352. retval = usb_register(&cypress_driver);
  1353. if (retval)
  1354. goto failed_usb_register;
  1355. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1356. DRIVER_DESC "\n");
  1357. return 0;
  1358. failed_usb_register:
  1359. usb_serial_deregister(&cypress_ca42v2_device);
  1360. failed_ca42v2_register:
  1361. usb_serial_deregister(&cypress_hidcom_device);
  1362. failed_hidcom_register:
  1363. usb_serial_deregister(&cypress_earthmate_device);
  1364. failed_em_register:
  1365. return retval;
  1366. }
  1367. static void __exit cypress_exit(void)
  1368. {
  1369. dbg("%s", __func__);
  1370. usb_deregister(&cypress_driver);
  1371. usb_serial_deregister(&cypress_earthmate_device);
  1372. usb_serial_deregister(&cypress_hidcom_device);
  1373. usb_serial_deregister(&cypress_ca42v2_device);
  1374. }
  1375. module_init(cypress_init);
  1376. module_exit(cypress_exit);
  1377. MODULE_AUTHOR(DRIVER_AUTHOR);
  1378. MODULE_DESCRIPTION(DRIVER_DESC);
  1379. MODULE_VERSION(DRIVER_VERSION);
  1380. MODULE_LICENSE("GPL");
  1381. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1382. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1383. module_param(stats, bool, S_IRUGO | S_IWUSR);
  1384. MODULE_PARM_DESC(stats, "Enable statistics or not");
  1385. module_param(interval, int, S_IRUGO | S_IWUSR);
  1386. MODULE_PARM_DESC(interval, "Overrides interrupt interval");
  1387. module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
  1388. MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");