cypress_m8.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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. spin_lock_irq(&priv->lock);
  602. if (on == 0)
  603. priv->line_control = 0;
  604. else
  605. priv->line_control = CONTROL_DTR | CONTROL_RTS;
  606. priv->cmd_ctrl = 1;
  607. spin_unlock_irq(&priv->lock);
  608. cypress_write(NULL, port, NULL, 0);
  609. }
  610. static void cypress_close(struct usb_serial_port *port)
  611. {
  612. struct cypress_private *priv = usb_get_serial_port_data(port);
  613. dbg("%s - port %d", __func__, port->number);
  614. /* writing is potentially harmful, lock must be taken */
  615. mutex_lock(&port->serial->disc_mutex);
  616. if (port->serial->disconnected) {
  617. mutex_unlock(&port->serial->disc_mutex);
  618. return;
  619. }
  620. cypress_buf_clear(priv->buf);
  621. dbg("%s - stopping urbs", __func__);
  622. usb_kill_urb(port->interrupt_in_urb);
  623. usb_kill_urb(port->interrupt_out_urb);
  624. if (stats)
  625. dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
  626. priv->bytes_in, priv->bytes_out, priv->cmd_count);
  627. mutex_unlock(&port->serial->disc_mutex);
  628. } /* cypress_close */
  629. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  630. const unsigned char *buf, int count)
  631. {
  632. struct cypress_private *priv = usb_get_serial_port_data(port);
  633. unsigned long flags;
  634. dbg("%s - port %d, %d bytes", __func__, port->number, count);
  635. /* line control commands, which need to be executed immediately,
  636. are not put into the buffer for obvious reasons.
  637. */
  638. if (priv->cmd_ctrl) {
  639. count = 0;
  640. goto finish;
  641. }
  642. if (!count)
  643. return count;
  644. spin_lock_irqsave(&priv->lock, flags);
  645. count = cypress_buf_put(priv->buf, buf, count);
  646. spin_unlock_irqrestore(&priv->lock, flags);
  647. finish:
  648. cypress_send(port);
  649. return count;
  650. } /* cypress_write */
  651. static void cypress_send(struct usb_serial_port *port)
  652. {
  653. int count = 0, result, offset, actual_size;
  654. struct cypress_private *priv = usb_get_serial_port_data(port);
  655. unsigned long flags;
  656. if (!priv->comm_is_ok)
  657. return;
  658. dbg("%s - port %d", __func__, port->number);
  659. dbg("%s - interrupt out size is %d", __func__,
  660. port->interrupt_out_size);
  661. spin_lock_irqsave(&priv->lock, flags);
  662. if (priv->write_urb_in_use) {
  663. dbg("%s - can't write, urb in use", __func__);
  664. spin_unlock_irqrestore(&priv->lock, flags);
  665. return;
  666. }
  667. spin_unlock_irqrestore(&priv->lock, flags);
  668. /* clear buffer */
  669. memset(port->interrupt_out_urb->transfer_buffer, 0,
  670. port->interrupt_out_size);
  671. spin_lock_irqsave(&priv->lock, flags);
  672. switch (priv->pkt_fmt) {
  673. default:
  674. case packet_format_1:
  675. /* this is for the CY7C64013... */
  676. offset = 2;
  677. port->interrupt_out_buffer[0] = priv->line_control;
  678. break;
  679. case packet_format_2:
  680. /* this is for the CY7C63743... */
  681. offset = 1;
  682. port->interrupt_out_buffer[0] = priv->line_control;
  683. break;
  684. }
  685. if (priv->line_control & CONTROL_RESET)
  686. priv->line_control &= ~CONTROL_RESET;
  687. if (priv->cmd_ctrl) {
  688. priv->cmd_count++;
  689. dbg("%s - line control command being issued", __func__);
  690. spin_unlock_irqrestore(&priv->lock, flags);
  691. goto send;
  692. } else
  693. spin_unlock_irqrestore(&priv->lock, flags);
  694. count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
  695. port->interrupt_out_size-offset);
  696. if (count == 0)
  697. return;
  698. switch (priv->pkt_fmt) {
  699. default:
  700. case packet_format_1:
  701. port->interrupt_out_buffer[1] = count;
  702. break;
  703. case packet_format_2:
  704. port->interrupt_out_buffer[0] |= count;
  705. }
  706. dbg("%s - count is %d", __func__, count);
  707. send:
  708. spin_lock_irqsave(&priv->lock, flags);
  709. priv->write_urb_in_use = 1;
  710. spin_unlock_irqrestore(&priv->lock, flags);
  711. if (priv->cmd_ctrl)
  712. actual_size = 1;
  713. else
  714. actual_size = count +
  715. (priv->pkt_fmt == packet_format_1 ? 2 : 1);
  716. usb_serial_debug_data(debug, &port->dev, __func__,
  717. port->interrupt_out_size,
  718. port->interrupt_out_urb->transfer_buffer);
  719. usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
  720. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  721. port->interrupt_out_buffer, port->interrupt_out_size,
  722. cypress_write_int_callback, port, priv->write_urb_interval);
  723. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  724. if (result) {
  725. dev_err(&port->dev,
  726. "%s - failed submitting write urb, error %d\n",
  727. __func__, result);
  728. priv->write_urb_in_use = 0;
  729. cypress_set_dead(port);
  730. }
  731. spin_lock_irqsave(&priv->lock, flags);
  732. if (priv->cmd_ctrl)
  733. priv->cmd_ctrl = 0;
  734. /* do not count the line control and size bytes */
  735. priv->bytes_out += count;
  736. spin_unlock_irqrestore(&priv->lock, flags);
  737. usb_serial_port_softint(port);
  738. } /* cypress_send */
  739. /* returns how much space is available in the soft buffer */
  740. static int cypress_write_room(struct tty_struct *tty)
  741. {
  742. struct usb_serial_port *port = tty->driver_data;
  743. struct cypress_private *priv = usb_get_serial_port_data(port);
  744. int room = 0;
  745. unsigned long flags;
  746. dbg("%s - port %d", __func__, port->number);
  747. spin_lock_irqsave(&priv->lock, flags);
  748. room = cypress_buf_space_avail(priv->buf);
  749. spin_unlock_irqrestore(&priv->lock, flags);
  750. dbg("%s - returns %d", __func__, room);
  751. return room;
  752. }
  753. static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
  754. {
  755. struct usb_serial_port *port = tty->driver_data;
  756. struct cypress_private *priv = usb_get_serial_port_data(port);
  757. __u8 status, control;
  758. unsigned int result = 0;
  759. unsigned long flags;
  760. dbg("%s - port %d", __func__, port->number);
  761. spin_lock_irqsave(&priv->lock, flags);
  762. control = priv->line_control;
  763. status = priv->current_status;
  764. spin_unlock_irqrestore(&priv->lock, flags);
  765. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  766. | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  767. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  768. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  769. | ((status & UART_RI) ? TIOCM_RI : 0)
  770. | ((status & UART_CD) ? TIOCM_CD : 0);
  771. dbg("%s - result = %x", __func__, result);
  772. return result;
  773. }
  774. static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
  775. unsigned int set, unsigned int clear)
  776. {
  777. struct usb_serial_port *port = tty->driver_data;
  778. struct cypress_private *priv = usb_get_serial_port_data(port);
  779. unsigned long flags;
  780. dbg("%s - port %d", __func__, port->number);
  781. spin_lock_irqsave(&priv->lock, flags);
  782. if (set & TIOCM_RTS)
  783. priv->line_control |= CONTROL_RTS;
  784. if (set & TIOCM_DTR)
  785. priv->line_control |= CONTROL_DTR;
  786. if (clear & TIOCM_RTS)
  787. priv->line_control &= ~CONTROL_RTS;
  788. if (clear & TIOCM_DTR)
  789. priv->line_control &= ~CONTROL_DTR;
  790. priv->cmd_ctrl = 1;
  791. spin_unlock_irqrestore(&priv->lock, flags);
  792. return cypress_write(tty, port, NULL, 0);
  793. }
  794. static int cypress_ioctl(struct tty_struct *tty, struct file *file,
  795. unsigned int cmd, unsigned long arg)
  796. {
  797. struct usb_serial_port *port = tty->driver_data;
  798. struct cypress_private *priv = usb_get_serial_port_data(port);
  799. dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
  800. switch (cmd) {
  801. /* This code comes from drivers/char/serial.c and ftdi_sio.c */
  802. case TIOCMIWAIT:
  803. while (priv != NULL) {
  804. interruptible_sleep_on(&priv->delta_msr_wait);
  805. /* see if a signal did it */
  806. if (signal_pending(current))
  807. return -ERESTARTSYS;
  808. else {
  809. char diff = priv->diff_status;
  810. if (diff == 0)
  811. return -EIO; /* no change => error */
  812. /* consume all events */
  813. priv->diff_status = 0;
  814. /* return 0 if caller wanted to know about
  815. these bits */
  816. if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
  817. ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
  818. ((arg & TIOCM_CD) && (diff & UART_CD)) ||
  819. ((arg & TIOCM_CTS) && (diff & UART_CTS)))
  820. return 0;
  821. /* otherwise caller can't care less about what
  822. * happened, and so we continue to wait for
  823. * more events.
  824. */
  825. }
  826. }
  827. return 0;
  828. default:
  829. break;
  830. }
  831. dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
  832. return -ENOIOCTLCMD;
  833. } /* cypress_ioctl */
  834. static void cypress_set_termios(struct tty_struct *tty,
  835. struct usb_serial_port *port, struct ktermios *old_termios)
  836. {
  837. struct cypress_private *priv = usb_get_serial_port_data(port);
  838. int data_bits, stop_bits, parity_type, parity_enable;
  839. unsigned cflag, iflag;
  840. unsigned long flags;
  841. __u8 oldlines;
  842. int linechange = 0;
  843. dbg("%s - port %d", __func__, port->number);
  844. spin_lock_irqsave(&priv->lock, flags);
  845. /* We can't clean this one up as we don't know the device type
  846. early enough */
  847. if (!priv->termios_initialized) {
  848. if (priv->chiptype == CT_EARTHMATE) {
  849. *(tty->termios) = tty_std_termios;
  850. tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
  851. CLOCAL;
  852. tty->termios->c_ispeed = 4800;
  853. tty->termios->c_ospeed = 4800;
  854. } else if (priv->chiptype == CT_CYPHIDCOM) {
  855. *(tty->termios) = tty_std_termios;
  856. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  857. CLOCAL;
  858. tty->termios->c_ispeed = 9600;
  859. tty->termios->c_ospeed = 9600;
  860. } else if (priv->chiptype == CT_CA42V2) {
  861. *(tty->termios) = tty_std_termios;
  862. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  863. CLOCAL;
  864. tty->termios->c_ispeed = 9600;
  865. tty->termios->c_ospeed = 9600;
  866. }
  867. priv->termios_initialized = 1;
  868. }
  869. spin_unlock_irqrestore(&priv->lock, flags);
  870. /* Unsupported features need clearing */
  871. tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
  872. cflag = tty->termios->c_cflag;
  873. iflag = tty->termios->c_iflag;
  874. /* check if there are new settings */
  875. if (old_termios) {
  876. spin_lock_irqsave(&priv->lock, flags);
  877. priv->tmp_termios = *(tty->termios);
  878. spin_unlock_irqrestore(&priv->lock, flags);
  879. }
  880. /* set number of data bits, parity, stop bits */
  881. /* when parity is disabled the parity type bit is ignored */
  882. /* 1 means 2 stop bits, 0 means 1 stop bit */
  883. stop_bits = cflag & CSTOPB ? 1 : 0;
  884. if (cflag & PARENB) {
  885. parity_enable = 1;
  886. /* 1 means odd parity, 0 means even parity */
  887. parity_type = cflag & PARODD ? 1 : 0;
  888. } else
  889. parity_enable = parity_type = 0;
  890. switch (cflag & CSIZE) {
  891. case CS5:
  892. data_bits = 0;
  893. break;
  894. case CS6:
  895. data_bits = 1;
  896. break;
  897. case CS7:
  898. data_bits = 2;
  899. break;
  900. case CS8:
  901. data_bits = 3;
  902. break;
  903. default:
  904. dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
  905. __func__);
  906. data_bits = 3;
  907. }
  908. spin_lock_irqsave(&priv->lock, flags);
  909. oldlines = priv->line_control;
  910. if ((cflag & CBAUD) == B0) {
  911. /* drop dtr and rts */
  912. dbg("%s - dropping the lines, baud rate 0bps", __func__);
  913. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  914. } else
  915. priv->line_control = (CONTROL_DTR | CONTROL_RTS);
  916. spin_unlock_irqrestore(&priv->lock, flags);
  917. dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
  918. "%d data_bits (+5)", __func__, stop_bits,
  919. parity_enable, parity_type, data_bits);
  920. cypress_serial_control(tty, port, tty_get_baud_rate(tty),
  921. data_bits, stop_bits,
  922. parity_enable, parity_type,
  923. 0, CYPRESS_SET_CONFIG);
  924. /* we perform a CYPRESS_GET_CONFIG so that the current settings are
  925. * filled into the private structure this should confirm that all is
  926. * working if it returns what we just set */
  927. cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
  928. /* Here we can define custom tty settings for devices; the main tty
  929. * termios flag base comes from empeg.c */
  930. spin_lock_irqsave(&priv->lock, flags);
  931. if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
  932. dbg("Using custom termios settings for a baud rate of "
  933. "4800bps.");
  934. /* define custom termios settings for NMEA protocol */
  935. tty->termios->c_iflag /* input modes - */
  936. &= ~(IGNBRK /* disable ignore break */
  937. | BRKINT /* disable break causes interrupt */
  938. | PARMRK /* disable mark parity errors */
  939. | ISTRIP /* disable clear high bit of input char */
  940. | INLCR /* disable translate NL to CR */
  941. | IGNCR /* disable ignore CR */
  942. | ICRNL /* disable translate CR to NL */
  943. | IXON); /* disable enable XON/XOFF flow control */
  944. tty->termios->c_oflag /* output modes */
  945. &= ~OPOST; /* disable postprocess output char */
  946. tty->termios->c_lflag /* line discipline modes */
  947. &= ~(ECHO /* disable echo input characters */
  948. | ECHONL /* disable echo new line */
  949. | ICANON /* disable erase, kill, werase, and rprnt
  950. special characters */
  951. | ISIG /* disable interrupt, quit, and suspend
  952. special characters */
  953. | IEXTEN); /* disable non-POSIX special characters */
  954. } /* CT_CYPHIDCOM: Application should handle this for device */
  955. linechange = (priv->line_control != oldlines);
  956. spin_unlock_irqrestore(&priv->lock, flags);
  957. /* if necessary, set lines */
  958. if (linechange) {
  959. priv->cmd_ctrl = 1;
  960. cypress_write(tty, port, NULL, 0);
  961. }
  962. } /* cypress_set_termios */
  963. /* returns amount of data still left in soft buffer */
  964. static int cypress_chars_in_buffer(struct tty_struct *tty)
  965. {
  966. struct usb_serial_port *port = tty->driver_data;
  967. struct cypress_private *priv = usb_get_serial_port_data(port);
  968. int chars = 0;
  969. unsigned long flags;
  970. dbg("%s - port %d", __func__, port->number);
  971. spin_lock_irqsave(&priv->lock, flags);
  972. chars = cypress_buf_data_avail(priv->buf);
  973. spin_unlock_irqrestore(&priv->lock, flags);
  974. dbg("%s - returns %d", __func__, chars);
  975. return chars;
  976. }
  977. static void cypress_throttle(struct tty_struct *tty)
  978. {
  979. struct usb_serial_port *port = tty->driver_data;
  980. struct cypress_private *priv = usb_get_serial_port_data(port);
  981. dbg("%s - port %d", __func__, port->number);
  982. spin_lock_irq(&priv->lock);
  983. priv->rx_flags = THROTTLED;
  984. spin_unlock_irq(&priv->lock);
  985. }
  986. static void cypress_unthrottle(struct tty_struct *tty)
  987. {
  988. struct usb_serial_port *port = tty->driver_data;
  989. struct cypress_private *priv = usb_get_serial_port_data(port);
  990. int actually_throttled, result;
  991. dbg("%s - port %d", __func__, port->number);
  992. spin_lock_irq(&priv->lock);
  993. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  994. priv->rx_flags = 0;
  995. spin_unlock_irq(&priv->lock);
  996. if (!priv->comm_is_ok)
  997. return;
  998. if (actually_throttled) {
  999. port->interrupt_in_urb->dev = port->serial->dev;
  1000. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  1001. if (result) {
  1002. dev_err(&port->dev, "%s - failed submitting read urb, "
  1003. "error %d\n", __func__, result);
  1004. cypress_set_dead(port);
  1005. }
  1006. }
  1007. }
  1008. static void cypress_read_int_callback(struct urb *urb)
  1009. {
  1010. struct usb_serial_port *port = urb->context;
  1011. struct cypress_private *priv = usb_get_serial_port_data(port);
  1012. struct tty_struct *tty;
  1013. unsigned char *data = urb->transfer_buffer;
  1014. unsigned long flags;
  1015. char tty_flag = TTY_NORMAL;
  1016. int havedata = 0;
  1017. int bytes = 0;
  1018. int result;
  1019. int i = 0;
  1020. int status = urb->status;
  1021. dbg("%s - port %d", __func__, port->number);
  1022. switch (status) {
  1023. case 0: /* success */
  1024. break;
  1025. case -ECONNRESET:
  1026. case -ENOENT:
  1027. case -ESHUTDOWN:
  1028. /* precursor to disconnect so just go away */
  1029. return;
  1030. case -EPIPE:
  1031. /* Can't call usb_clear_halt while in_interrupt */
  1032. /* FALLS THROUGH */
  1033. default:
  1034. /* something ugly is going on... */
  1035. dev_err(&urb->dev->dev,
  1036. "%s - unexpected nonzero read status received: %d\n",
  1037. __func__, status);
  1038. cypress_set_dead(port);
  1039. return;
  1040. }
  1041. spin_lock_irqsave(&priv->lock, flags);
  1042. if (priv->rx_flags & THROTTLED) {
  1043. dbg("%s - now throttling", __func__);
  1044. priv->rx_flags |= ACTUALLY_THROTTLED;
  1045. spin_unlock_irqrestore(&priv->lock, flags);
  1046. return;
  1047. }
  1048. spin_unlock_irqrestore(&priv->lock, flags);
  1049. tty = tty_port_tty_get(&port->port);
  1050. if (!tty) {
  1051. dbg("%s - bad tty pointer - exiting", __func__);
  1052. return;
  1053. }
  1054. spin_lock_irqsave(&priv->lock, flags);
  1055. result = urb->actual_length;
  1056. switch (priv->pkt_fmt) {
  1057. default:
  1058. case packet_format_1:
  1059. /* This is for the CY7C64013... */
  1060. priv->current_status = data[0] & 0xF8;
  1061. bytes = data[1] + 2;
  1062. i = 2;
  1063. if (bytes > 2)
  1064. havedata = 1;
  1065. break;
  1066. case packet_format_2:
  1067. /* This is for the CY7C63743... */
  1068. priv->current_status = data[0] & 0xF8;
  1069. bytes = (data[0] & 0x07) + 1;
  1070. i = 1;
  1071. if (bytes > 1)
  1072. havedata = 1;
  1073. break;
  1074. }
  1075. spin_unlock_irqrestore(&priv->lock, flags);
  1076. if (result < bytes) {
  1077. dbg("%s - wrong packet size - received %d bytes but packet "
  1078. "said %d bytes", __func__, result, bytes);
  1079. goto continue_read;
  1080. }
  1081. usb_serial_debug_data(debug, &port->dev, __func__,
  1082. urb->actual_length, data);
  1083. spin_lock_irqsave(&priv->lock, flags);
  1084. /* check to see if status has changed */
  1085. if (priv->current_status != priv->prev_status) {
  1086. priv->diff_status |= priv->current_status ^
  1087. priv->prev_status;
  1088. wake_up_interruptible(&priv->delta_msr_wait);
  1089. priv->prev_status = priv->current_status;
  1090. }
  1091. spin_unlock_irqrestore(&priv->lock, flags);
  1092. /* hangup, as defined in acm.c... this might be a bad place for it
  1093. * though */
  1094. if (tty && !(tty->termios->c_cflag & CLOCAL) &&
  1095. !(priv->current_status & UART_CD)) {
  1096. dbg("%s - calling hangup", __func__);
  1097. tty_hangup(tty);
  1098. goto continue_read;
  1099. }
  1100. /* There is one error bit... I'm assuming it is a parity error
  1101. * indicator as the generic firmware will set this bit to 1 if a
  1102. * parity error occurs.
  1103. * I can not find reference to any other error events. */
  1104. spin_lock_irqsave(&priv->lock, flags);
  1105. if (priv->current_status & CYP_ERROR) {
  1106. spin_unlock_irqrestore(&priv->lock, flags);
  1107. tty_flag = TTY_PARITY;
  1108. dbg("%s - Parity Error detected", __func__);
  1109. } else
  1110. spin_unlock_irqrestore(&priv->lock, flags);
  1111. /* process read if there is data other than line status */
  1112. if (tty && bytes > i) {
  1113. tty_insert_flip_string_fixed_flag(tty, data + i,
  1114. bytes - i, tty_flag);
  1115. tty_flip_buffer_push(tty);
  1116. }
  1117. spin_lock_irqsave(&priv->lock, flags);
  1118. /* control and status byte(s) are also counted */
  1119. priv->bytes_in += bytes;
  1120. spin_unlock_irqrestore(&priv->lock, flags);
  1121. continue_read:
  1122. tty_kref_put(tty);
  1123. /* Continue trying to always read */
  1124. if (priv->comm_is_ok) {
  1125. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  1126. usb_rcvintpipe(port->serial->dev,
  1127. port->interrupt_in_endpointAddress),
  1128. port->interrupt_in_urb->transfer_buffer,
  1129. port->interrupt_in_urb->transfer_buffer_length,
  1130. cypress_read_int_callback, port,
  1131. priv->read_urb_interval);
  1132. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  1133. if (result && result != -EPERM) {
  1134. dev_err(&urb->dev->dev, "%s - failed resubmitting "
  1135. "read urb, error %d\n", __func__,
  1136. result);
  1137. cypress_set_dead(port);
  1138. }
  1139. }
  1140. return;
  1141. } /* cypress_read_int_callback */
  1142. static void cypress_write_int_callback(struct urb *urb)
  1143. {
  1144. struct usb_serial_port *port = urb->context;
  1145. struct cypress_private *priv = usb_get_serial_port_data(port);
  1146. int result;
  1147. int status = urb->status;
  1148. dbg("%s - port %d", __func__, port->number);
  1149. switch (status) {
  1150. case 0:
  1151. /* success */
  1152. break;
  1153. case -ECONNRESET:
  1154. case -ENOENT:
  1155. case -ESHUTDOWN:
  1156. /* this urb is terminated, clean up */
  1157. dbg("%s - urb shutting down with status: %d",
  1158. __func__, status);
  1159. priv->write_urb_in_use = 0;
  1160. return;
  1161. case -EPIPE: /* no break needed; clear halt and resubmit */
  1162. if (!priv->comm_is_ok)
  1163. break;
  1164. usb_clear_halt(port->serial->dev, 0x02);
  1165. /* error in the urb, so we have to resubmit it */
  1166. dbg("%s - nonzero write bulk status received: %d",
  1167. __func__, status);
  1168. port->interrupt_out_urb->transfer_buffer_length = 1;
  1169. port->interrupt_out_urb->dev = port->serial->dev;
  1170. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  1171. if (!result)
  1172. return;
  1173. dev_err(&urb->dev->dev,
  1174. "%s - failed resubmitting write urb, error %d\n",
  1175. __func__, result);
  1176. cypress_set_dead(port);
  1177. break;
  1178. default:
  1179. dev_err(&urb->dev->dev,
  1180. "%s - unexpected nonzero write status received: %d\n",
  1181. __func__, status);
  1182. cypress_set_dead(port);
  1183. break;
  1184. }
  1185. priv->write_urb_in_use = 0;
  1186. /* send any buffered data */
  1187. cypress_send(port);
  1188. }
  1189. /*****************************************************************************
  1190. * Write buffer functions - buffering code from pl2303 used
  1191. *****************************************************************************/
  1192. /*
  1193. * cypress_buf_alloc
  1194. *
  1195. * Allocate a circular buffer and all associated memory.
  1196. */
  1197. static struct cypress_buf *cypress_buf_alloc(unsigned int size)
  1198. {
  1199. struct cypress_buf *cb;
  1200. if (size == 0)
  1201. return NULL;
  1202. cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
  1203. if (cb == NULL)
  1204. return NULL;
  1205. cb->buf_buf = kmalloc(size, GFP_KERNEL);
  1206. if (cb->buf_buf == NULL) {
  1207. kfree(cb);
  1208. return NULL;
  1209. }
  1210. cb->buf_size = size;
  1211. cb->buf_get = cb->buf_put = cb->buf_buf;
  1212. return cb;
  1213. }
  1214. /*
  1215. * cypress_buf_free
  1216. *
  1217. * Free the buffer and all associated memory.
  1218. */
  1219. static void cypress_buf_free(struct cypress_buf *cb)
  1220. {
  1221. if (cb) {
  1222. kfree(cb->buf_buf);
  1223. kfree(cb);
  1224. }
  1225. }
  1226. /*
  1227. * cypress_buf_clear
  1228. *
  1229. * Clear out all data in the circular buffer.
  1230. */
  1231. static void cypress_buf_clear(struct cypress_buf *cb)
  1232. {
  1233. if (cb != NULL)
  1234. cb->buf_get = cb->buf_put;
  1235. /* equivalent to a get of all data available */
  1236. }
  1237. /*
  1238. * cypress_buf_data_avail
  1239. *
  1240. * Return the number of bytes of data available in the circular
  1241. * buffer.
  1242. */
  1243. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
  1244. {
  1245. if (cb != NULL)
  1246. return (cb->buf_size + cb->buf_put - cb->buf_get)
  1247. % cb->buf_size;
  1248. else
  1249. return 0;
  1250. }
  1251. /*
  1252. * cypress_buf_space_avail
  1253. *
  1254. * Return the number of bytes of space available in the circular
  1255. * buffer.
  1256. */
  1257. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
  1258. {
  1259. if (cb != NULL)
  1260. return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
  1261. % cb->buf_size;
  1262. else
  1263. return 0;
  1264. }
  1265. /*
  1266. * cypress_buf_put
  1267. *
  1268. * Copy data data from a user buffer and put it into the circular buffer.
  1269. * Restrict to the amount of space available.
  1270. *
  1271. * Return the number of bytes copied.
  1272. */
  1273. static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
  1274. unsigned int count)
  1275. {
  1276. unsigned int len;
  1277. if (cb == NULL)
  1278. return 0;
  1279. len = cypress_buf_space_avail(cb);
  1280. if (count > len)
  1281. count = len;
  1282. if (count == 0)
  1283. return 0;
  1284. len = cb->buf_buf + cb->buf_size - cb->buf_put;
  1285. if (count > len) {
  1286. memcpy(cb->buf_put, buf, len);
  1287. memcpy(cb->buf_buf, buf+len, count - len);
  1288. cb->buf_put = cb->buf_buf + count - len;
  1289. } else {
  1290. memcpy(cb->buf_put, buf, count);
  1291. if (count < len)
  1292. cb->buf_put += count;
  1293. else /* count == len */
  1294. cb->buf_put = cb->buf_buf;
  1295. }
  1296. return count;
  1297. }
  1298. /*
  1299. * cypress_buf_get
  1300. *
  1301. * Get data from the circular buffer and copy to the given buffer.
  1302. * Restrict to the amount of data available.
  1303. *
  1304. * Return the number of bytes copied.
  1305. */
  1306. static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
  1307. unsigned int count)
  1308. {
  1309. unsigned int len;
  1310. if (cb == NULL)
  1311. return 0;
  1312. len = cypress_buf_data_avail(cb);
  1313. if (count > len)
  1314. count = len;
  1315. if (count == 0)
  1316. return 0;
  1317. len = cb->buf_buf + cb->buf_size - cb->buf_get;
  1318. if (count > len) {
  1319. memcpy(buf, cb->buf_get, len);
  1320. memcpy(buf+len, cb->buf_buf, count - len);
  1321. cb->buf_get = cb->buf_buf + count - len;
  1322. } else {
  1323. memcpy(buf, cb->buf_get, count);
  1324. if (count < len)
  1325. cb->buf_get += count;
  1326. else /* count == len */
  1327. cb->buf_get = cb->buf_buf;
  1328. }
  1329. return count;
  1330. }
  1331. /*****************************************************************************
  1332. * Module functions
  1333. *****************************************************************************/
  1334. static int __init cypress_init(void)
  1335. {
  1336. int retval;
  1337. dbg("%s", __func__);
  1338. retval = usb_serial_register(&cypress_earthmate_device);
  1339. if (retval)
  1340. goto failed_em_register;
  1341. retval = usb_serial_register(&cypress_hidcom_device);
  1342. if (retval)
  1343. goto failed_hidcom_register;
  1344. retval = usb_serial_register(&cypress_ca42v2_device);
  1345. if (retval)
  1346. goto failed_ca42v2_register;
  1347. retval = usb_register(&cypress_driver);
  1348. if (retval)
  1349. goto failed_usb_register;
  1350. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1351. DRIVER_DESC "\n");
  1352. return 0;
  1353. failed_usb_register:
  1354. usb_serial_deregister(&cypress_ca42v2_device);
  1355. failed_ca42v2_register:
  1356. usb_serial_deregister(&cypress_hidcom_device);
  1357. failed_hidcom_register:
  1358. usb_serial_deregister(&cypress_earthmate_device);
  1359. failed_em_register:
  1360. return retval;
  1361. }
  1362. static void __exit cypress_exit(void)
  1363. {
  1364. dbg("%s", __func__);
  1365. usb_deregister(&cypress_driver);
  1366. usb_serial_deregister(&cypress_earthmate_device);
  1367. usb_serial_deregister(&cypress_hidcom_device);
  1368. usb_serial_deregister(&cypress_ca42v2_device);
  1369. }
  1370. module_init(cypress_init);
  1371. module_exit(cypress_exit);
  1372. MODULE_AUTHOR(DRIVER_AUTHOR);
  1373. MODULE_DESCRIPTION(DRIVER_DESC);
  1374. MODULE_VERSION(DRIVER_VERSION);
  1375. MODULE_LICENSE("GPL");
  1376. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1377. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1378. module_param(stats, bool, S_IRUGO | S_IWUSR);
  1379. MODULE_PARM_DESC(stats, "Enable statistics or not");
  1380. module_param(interval, int, S_IRUGO | S_IWUSR);
  1381. MODULE_PARM_DESC(interval, "Overrides interrupt interval");
  1382. module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
  1383. MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");