visor.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * USB HandSpring Visor, Palm m50x, and Sony Clie driver
  3. * (supports all of the Palm OS USB devices)
  4. *
  5. * Copyright (C) 1999 - 2004
  6. * Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * See Documentation/usb/usb-serial.txt for more information on using this
  13. * driver
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/serial.h>
  29. #include "visor.h"
  30. /*
  31. * Version Information
  32. */
  33. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  34. #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
  35. /* function prototypes for a handspring visor */
  36. static int visor_open(struct tty_struct *tty, struct usb_serial_port *port);
  37. static void visor_close(struct usb_serial_port *port);
  38. static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
  39. const unsigned char *buf, int count);
  40. static int visor_write_room(struct tty_struct *tty);
  41. static void visor_throttle(struct tty_struct *tty);
  42. static void visor_unthrottle(struct tty_struct *tty);
  43. static int visor_probe(struct usb_serial *serial,
  44. const struct usb_device_id *id);
  45. static int visor_calc_num_ports(struct usb_serial *serial);
  46. static void visor_release(struct usb_serial *serial);
  47. static void visor_write_bulk_callback(struct urb *urb);
  48. static void visor_read_bulk_callback(struct urb *urb);
  49. static void visor_read_int_callback(struct urb *urb);
  50. static int clie_3_5_startup(struct usb_serial *serial);
  51. static int treo_attach(struct usb_serial *serial);
  52. static int clie_5_attach(struct usb_serial *serial);
  53. static int palm_os_3_probe(struct usb_serial *serial,
  54. const struct usb_device_id *id);
  55. static int palm_os_4_probe(struct usb_serial *serial,
  56. const struct usb_device_id *id);
  57. /* Parameters that may be passed into the module. */
  58. static int debug;
  59. static __u16 vendor;
  60. static __u16 product;
  61. static struct usb_device_id id_table [] = {
  62. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
  63. .driver_info = (kernel_ulong_t)&palm_os_3_probe },
  64. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
  65. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  66. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
  67. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  68. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID),
  69. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  70. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
  71. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  72. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
  73. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  74. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
  75. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  76. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
  77. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  78. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
  79. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  80. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
  81. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  82. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
  83. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  84. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
  85. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  86. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650),
  87. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  88. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
  89. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  90. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
  91. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  92. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
  93. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  94. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
  95. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  96. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
  97. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  98. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
  99. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  100. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
  101. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  102. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
  103. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  104. { USB_DEVICE(ACER_VENDOR_ID, ACER_S10_ID),
  105. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  106. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID),
  107. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  108. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID),
  109. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  110. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID),
  111. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  112. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID),
  113. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  114. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
  115. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  116. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
  117. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  118. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID),
  119. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  120. { }, /* optional parameter entry */
  121. { } /* Terminating entry */
  122. };
  123. static struct usb_device_id clie_id_5_table [] = {
  124. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
  125. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  126. { }, /* optional parameter entry */
  127. { } /* Terminating entry */
  128. };
  129. static struct usb_device_id clie_id_3_5_table [] = {
  130. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  131. { } /* Terminating entry */
  132. };
  133. static struct usb_device_id id_table_combined [] = {
  134. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
  135. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
  136. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
  137. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID) },
  138. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
  139. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
  140. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
  141. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
  142. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
  143. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
  144. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
  145. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
  146. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650) },
  147. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
  148. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
  149. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  150. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
  151. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
  152. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
  153. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
  154. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
  155. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
  156. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
  157. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
  158. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
  159. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID) },
  160. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
  161. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
  162. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
  163. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID) },
  164. { }, /* optional parameter entry */
  165. { } /* Terminating entry */
  166. };
  167. MODULE_DEVICE_TABLE(usb, id_table_combined);
  168. static struct usb_driver visor_driver = {
  169. .name = "visor",
  170. .probe = usb_serial_probe,
  171. .disconnect = usb_serial_disconnect,
  172. .id_table = id_table_combined,
  173. .no_dynamic_id = 1,
  174. };
  175. /* All of the device info needed for the Handspring Visor,
  176. and Palm 4.0 devices */
  177. static struct usb_serial_driver handspring_device = {
  178. .driver = {
  179. .owner = THIS_MODULE,
  180. .name = "visor",
  181. },
  182. .description = "Handspring Visor / Palm OS",
  183. .usb_driver = &visor_driver,
  184. .id_table = id_table,
  185. .num_ports = 2,
  186. .open = visor_open,
  187. .close = visor_close,
  188. .throttle = visor_throttle,
  189. .unthrottle = visor_unthrottle,
  190. .attach = treo_attach,
  191. .probe = visor_probe,
  192. .calc_num_ports = visor_calc_num_ports,
  193. .release = visor_release,
  194. .write = visor_write,
  195. .write_room = visor_write_room,
  196. .write_bulk_callback = visor_write_bulk_callback,
  197. .read_bulk_callback = visor_read_bulk_callback,
  198. .read_int_callback = visor_read_int_callback,
  199. };
  200. /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
  201. static struct usb_serial_driver clie_5_device = {
  202. .driver = {
  203. .owner = THIS_MODULE,
  204. .name = "clie_5",
  205. },
  206. .description = "Sony Clie 5.0",
  207. .usb_driver = &visor_driver,
  208. .id_table = clie_id_5_table,
  209. .num_ports = 2,
  210. .open = visor_open,
  211. .close = visor_close,
  212. .throttle = visor_throttle,
  213. .unthrottle = visor_unthrottle,
  214. .attach = clie_5_attach,
  215. .probe = visor_probe,
  216. .calc_num_ports = visor_calc_num_ports,
  217. .release = visor_release,
  218. .write = visor_write,
  219. .write_room = visor_write_room,
  220. .write_bulk_callback = visor_write_bulk_callback,
  221. .read_bulk_callback = visor_read_bulk_callback,
  222. .read_int_callback = visor_read_int_callback,
  223. };
  224. /* device info for the Sony Clie OS version 3.5 */
  225. static struct usb_serial_driver clie_3_5_device = {
  226. .driver = {
  227. .owner = THIS_MODULE,
  228. .name = "clie_3.5",
  229. },
  230. .description = "Sony Clie 3.5",
  231. .usb_driver = &visor_driver,
  232. .id_table = clie_id_3_5_table,
  233. .num_ports = 1,
  234. .open = visor_open,
  235. .close = visor_close,
  236. .throttle = visor_throttle,
  237. .unthrottle = visor_unthrottle,
  238. .attach = clie_3_5_startup,
  239. .write = visor_write,
  240. .write_room = visor_write_room,
  241. .write_bulk_callback = visor_write_bulk_callback,
  242. .read_bulk_callback = visor_read_bulk_callback,
  243. };
  244. struct visor_private {
  245. spinlock_t lock;
  246. int bytes_in;
  247. int bytes_out;
  248. int outstanding_urbs;
  249. unsigned char throttled;
  250. unsigned char actually_throttled;
  251. };
  252. /* number of outstanding urbs to prevent userspace DoS from happening */
  253. #define URB_UPPER_LIMIT 42
  254. static int stats;
  255. /******************************************************************************
  256. * Handspring Visor specific driver functions
  257. ******************************************************************************/
  258. static int visor_open(struct tty_struct *tty, struct usb_serial_port *port)
  259. {
  260. struct usb_serial *serial = port->serial;
  261. struct visor_private *priv = usb_get_serial_port_data(port);
  262. unsigned long flags;
  263. int result = 0;
  264. dbg("%s - port %d", __func__, port->number);
  265. if (!port->read_urb) {
  266. /* this is needed for some brain dead Sony devices */
  267. dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
  268. return -ENODEV;
  269. }
  270. spin_lock_irqsave(&priv->lock, flags);
  271. priv->bytes_in = 0;
  272. priv->bytes_out = 0;
  273. priv->throttled = 0;
  274. spin_unlock_irqrestore(&priv->lock, flags);
  275. /* Start reading from the device */
  276. usb_fill_bulk_urb(port->read_urb, serial->dev,
  277. usb_rcvbulkpipe(serial->dev,
  278. port->bulk_in_endpointAddress),
  279. port->read_urb->transfer_buffer,
  280. port->read_urb->transfer_buffer_length,
  281. visor_read_bulk_callback, port);
  282. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  283. if (result) {
  284. dev_err(&port->dev,
  285. "%s - failed submitting read urb, error %d\n",
  286. __func__, result);
  287. goto exit;
  288. }
  289. if (port->interrupt_in_urb) {
  290. dbg("%s - adding interrupt input for treo", __func__);
  291. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  292. if (result)
  293. dev_err(&port->dev,
  294. "%s - failed submitting interrupt urb, error %d\n",
  295. __func__, result);
  296. }
  297. exit:
  298. return result;
  299. }
  300. static void visor_close(struct usb_serial_port *port)
  301. {
  302. struct visor_private *priv = usb_get_serial_port_data(port);
  303. unsigned char *transfer_buffer;
  304. dbg("%s - port %d", __func__, port->number);
  305. /* shutdown our urbs */
  306. usb_kill_urb(port->read_urb);
  307. usb_kill_urb(port->interrupt_in_urb);
  308. mutex_lock(&port->serial->disc_mutex);
  309. if (!port->serial->disconnected) {
  310. /* Try to send shutdown message, unless the device is gone */
  311. transfer_buffer = kmalloc(0x12, GFP_KERNEL);
  312. if (transfer_buffer) {
  313. usb_control_msg(port->serial->dev,
  314. usb_rcvctrlpipe(port->serial->dev, 0),
  315. VISOR_CLOSE_NOTIFICATION, 0xc2,
  316. 0x0000, 0x0000,
  317. transfer_buffer, 0x12, 300);
  318. kfree(transfer_buffer);
  319. }
  320. }
  321. mutex_unlock(&port->serial->disc_mutex);
  322. if (stats)
  323. dev_info(&port->dev, "Bytes In = %d Bytes Out = %d\n",
  324. priv->bytes_in, priv->bytes_out);
  325. }
  326. static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
  327. const unsigned char *buf, int count)
  328. {
  329. struct visor_private *priv = usb_get_serial_port_data(port);
  330. struct usb_serial *serial = port->serial;
  331. struct urb *urb;
  332. unsigned char *buffer;
  333. unsigned long flags;
  334. int status;
  335. dbg("%s - port %d", __func__, port->number);
  336. spin_lock_irqsave(&priv->lock, flags);
  337. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  338. spin_unlock_irqrestore(&priv->lock, flags);
  339. dbg("%s - write limit hit", __func__);
  340. return 0;
  341. }
  342. priv->outstanding_urbs++;
  343. spin_unlock_irqrestore(&priv->lock, flags);
  344. buffer = kmalloc(count, GFP_ATOMIC);
  345. if (!buffer) {
  346. dev_err(&port->dev, "out of memory\n");
  347. count = -ENOMEM;
  348. goto error_no_buffer;
  349. }
  350. urb = usb_alloc_urb(0, GFP_ATOMIC);
  351. if (!urb) {
  352. dev_err(&port->dev, "no more free urbs\n");
  353. count = -ENOMEM;
  354. goto error_no_urb;
  355. }
  356. memcpy(buffer, buf, count);
  357. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  358. usb_fill_bulk_urb(urb, serial->dev,
  359. usb_sndbulkpipe(serial->dev,
  360. port->bulk_out_endpointAddress),
  361. buffer, count,
  362. visor_write_bulk_callback, port);
  363. /* send it down the pipe */
  364. status = usb_submit_urb(urb, GFP_ATOMIC);
  365. if (status) {
  366. dev_err(&port->dev,
  367. "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  368. __func__, status);
  369. count = status;
  370. goto error;
  371. } else {
  372. spin_lock_irqsave(&priv->lock, flags);
  373. priv->bytes_out += count;
  374. spin_unlock_irqrestore(&priv->lock, flags);
  375. }
  376. /* we are done with this urb, so let the host driver
  377. * really free it when it is finished with it */
  378. usb_free_urb(urb);
  379. return count;
  380. error:
  381. usb_free_urb(urb);
  382. error_no_urb:
  383. kfree(buffer);
  384. error_no_buffer:
  385. spin_lock_irqsave(&priv->lock, flags);
  386. --priv->outstanding_urbs;
  387. spin_unlock_irqrestore(&priv->lock, flags);
  388. return count;
  389. }
  390. static int visor_write_room(struct tty_struct *tty)
  391. {
  392. struct usb_serial_port *port = tty->driver_data;
  393. struct visor_private *priv = usb_get_serial_port_data(port);
  394. unsigned long flags;
  395. dbg("%s - port %d", __func__, port->number);
  396. /*
  397. * We really can take anything the user throws at us
  398. * but let's pick a nice big number to tell the tty
  399. * layer that we have lots of free space, unless we don't.
  400. */
  401. spin_lock_irqsave(&priv->lock, flags);
  402. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  403. spin_unlock_irqrestore(&priv->lock, flags);
  404. dbg("%s - write limit hit", __func__);
  405. return 0;
  406. }
  407. spin_unlock_irqrestore(&priv->lock, flags);
  408. return 2048;
  409. }
  410. static void visor_write_bulk_callback(struct urb *urb)
  411. {
  412. struct usb_serial_port *port = urb->context;
  413. struct visor_private *priv = usb_get_serial_port_data(port);
  414. int status = urb->status;
  415. unsigned long flags;
  416. /* free up the transfer buffer, as usb_free_urb() does not do this */
  417. kfree(urb->transfer_buffer);
  418. dbg("%s - port %d", __func__, port->number);
  419. if (status)
  420. dbg("%s - nonzero write bulk status received: %d",
  421. __func__, status);
  422. spin_lock_irqsave(&priv->lock, flags);
  423. --priv->outstanding_urbs;
  424. spin_unlock_irqrestore(&priv->lock, flags);
  425. usb_serial_port_softint(port);
  426. }
  427. static void visor_read_bulk_callback(struct urb *urb)
  428. {
  429. struct usb_serial_port *port = urb->context;
  430. struct visor_private *priv = usb_get_serial_port_data(port);
  431. unsigned char *data = urb->transfer_buffer;
  432. int status = urb->status;
  433. struct tty_struct *tty;
  434. int result;
  435. int available_room = 0;
  436. dbg("%s - port %d", __func__, port->number);
  437. if (status) {
  438. dbg("%s - nonzero read bulk status received: %d",
  439. __func__, status);
  440. return;
  441. }
  442. usb_serial_debug_data(debug, &port->dev, __func__,
  443. urb->actual_length, data);
  444. if (urb->actual_length) {
  445. tty = tty_port_tty_get(&port->port);
  446. if (tty) {
  447. tty_insert_flip_string(tty, data,
  448. urb->actual_length);
  449. tty_flip_buffer_push(tty);
  450. tty_kref_put(tty);
  451. }
  452. spin_lock(&priv->lock);
  453. if (tty)
  454. priv->bytes_in += available_room;
  455. } else {
  456. spin_lock(&priv->lock);
  457. }
  458. /* Continue trying to always read if we should */
  459. if (!priv->throttled) {
  460. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  461. usb_rcvbulkpipe(port->serial->dev,
  462. port->bulk_in_endpointAddress),
  463. port->read_urb->transfer_buffer,
  464. port->read_urb->transfer_buffer_length,
  465. visor_read_bulk_callback, port);
  466. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  467. if (result)
  468. dev_err(&port->dev,
  469. "%s - failed resubmitting read urb, error %d\n",
  470. __func__, result);
  471. } else
  472. priv->actually_throttled = 1;
  473. spin_unlock(&priv->lock);
  474. }
  475. static void visor_read_int_callback(struct urb *urb)
  476. {
  477. struct usb_serial_port *port = urb->context;
  478. int status = urb->status;
  479. int result;
  480. switch (status) {
  481. case 0:
  482. /* success */
  483. break;
  484. case -ECONNRESET:
  485. case -ENOENT:
  486. case -ESHUTDOWN:
  487. /* this urb is terminated, clean up */
  488. dbg("%s - urb shutting down with status: %d",
  489. __func__, status);
  490. return;
  491. default:
  492. dbg("%s - nonzero urb status received: %d",
  493. __func__, status);
  494. goto exit;
  495. }
  496. /*
  497. * This information is still unknown what it can be used for.
  498. * If anyone has an idea, please let the author know...
  499. *
  500. * Rumor has it this endpoint is used to notify when data
  501. * is ready to be read from the bulk ones.
  502. */
  503. usb_serial_debug_data(debug, &port->dev, __func__,
  504. urb->actual_length, urb->transfer_buffer);
  505. exit:
  506. result = usb_submit_urb(urb, GFP_ATOMIC);
  507. if (result)
  508. dev_err(&urb->dev->dev,
  509. "%s - Error %d submitting interrupt urb\n",
  510. __func__, result);
  511. }
  512. static void visor_throttle(struct tty_struct *tty)
  513. {
  514. struct usb_serial_port *port = tty->driver_data;
  515. struct visor_private *priv = usb_get_serial_port_data(port);
  516. dbg("%s - port %d", __func__, port->number);
  517. spin_lock_irq(&priv->lock);
  518. priv->throttled = 1;
  519. spin_unlock_irq(&priv->lock);
  520. }
  521. static void visor_unthrottle(struct tty_struct *tty)
  522. {
  523. struct usb_serial_port *port = tty->driver_data;
  524. struct visor_private *priv = usb_get_serial_port_data(port);
  525. int result, was_throttled;
  526. dbg("%s - port %d", __func__, port->number);
  527. spin_lock_irq(&priv->lock);
  528. priv->throttled = 0;
  529. was_throttled = priv->actually_throttled;
  530. priv->actually_throttled = 0;
  531. spin_unlock_irq(&priv->lock);
  532. if (was_throttled) {
  533. port->read_urb->dev = port->serial->dev;
  534. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  535. if (result)
  536. dev_err(&port->dev,
  537. "%s - failed submitting read urb, error %d\n",
  538. __func__, result);
  539. }
  540. }
  541. static int palm_os_3_probe(struct usb_serial *serial,
  542. const struct usb_device_id *id)
  543. {
  544. struct device *dev = &serial->dev->dev;
  545. struct visor_connection_info *connection_info;
  546. unsigned char *transfer_buffer;
  547. char *string;
  548. int retval = 0;
  549. int i;
  550. int num_ports = 0;
  551. dbg("%s", __func__);
  552. transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
  553. if (!transfer_buffer) {
  554. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  555. sizeof(*connection_info));
  556. return -ENOMEM;
  557. }
  558. /* send a get connection info request */
  559. retval = usb_control_msg(serial->dev,
  560. usb_rcvctrlpipe(serial->dev, 0),
  561. VISOR_GET_CONNECTION_INFORMATION,
  562. 0xc2, 0x0000, 0x0000, transfer_buffer,
  563. sizeof(*connection_info), 300);
  564. if (retval < 0) {
  565. dev_err(dev, "%s - error %d getting connection information\n",
  566. __func__, retval);
  567. goto exit;
  568. }
  569. if (retval == sizeof(*connection_info)) {
  570. connection_info = (struct visor_connection_info *)
  571. transfer_buffer;
  572. num_ports = le16_to_cpu(connection_info->num_ports);
  573. for (i = 0; i < num_ports; ++i) {
  574. switch (
  575. connection_info->connections[i].port_function_id) {
  576. case VISOR_FUNCTION_GENERIC:
  577. string = "Generic";
  578. break;
  579. case VISOR_FUNCTION_DEBUGGER:
  580. string = "Debugger";
  581. break;
  582. case VISOR_FUNCTION_HOTSYNC:
  583. string = "HotSync";
  584. break;
  585. case VISOR_FUNCTION_CONSOLE:
  586. string = "Console";
  587. break;
  588. case VISOR_FUNCTION_REMOTE_FILE_SYS:
  589. string = "Remote File System";
  590. break;
  591. default:
  592. string = "unknown";
  593. break;
  594. }
  595. dev_info(dev, "%s: port %d, is for %s use\n",
  596. serial->type->description,
  597. connection_info->connections[i].port, string);
  598. }
  599. }
  600. /*
  601. * Handle devices that report invalid stuff here.
  602. */
  603. if (num_ports == 0 || num_ports > 2) {
  604. dev_warn(dev, "%s: No valid connect info available\n",
  605. serial->type->description);
  606. num_ports = 2;
  607. }
  608. dev_info(dev, "%s: Number of ports: %d\n", serial->type->description,
  609. num_ports);
  610. /*
  611. * save off our num_ports info so that we can use it in the
  612. * calc_num_ports callback
  613. */
  614. usb_set_serial_data(serial, (void *)(long)num_ports);
  615. /* ask for the number of bytes available, but ignore the
  616. response as it is broken */
  617. retval = usb_control_msg(serial->dev,
  618. usb_rcvctrlpipe(serial->dev, 0),
  619. VISOR_REQUEST_BYTES_AVAILABLE,
  620. 0xc2, 0x0000, 0x0005, transfer_buffer,
  621. 0x02, 300);
  622. if (retval < 0)
  623. dev_err(dev, "%s - error %d getting bytes available request\n",
  624. __func__, retval);
  625. retval = 0;
  626. exit:
  627. kfree(transfer_buffer);
  628. return retval;
  629. }
  630. static int palm_os_4_probe(struct usb_serial *serial,
  631. const struct usb_device_id *id)
  632. {
  633. struct device *dev = &serial->dev->dev;
  634. struct palm_ext_connection_info *connection_info;
  635. unsigned char *transfer_buffer;
  636. int retval;
  637. dbg("%s", __func__);
  638. transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
  639. if (!transfer_buffer) {
  640. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  641. sizeof(*connection_info));
  642. return -ENOMEM;
  643. }
  644. retval = usb_control_msg(serial->dev,
  645. usb_rcvctrlpipe(serial->dev, 0),
  646. PALM_GET_EXT_CONNECTION_INFORMATION,
  647. 0xc2, 0x0000, 0x0000, transfer_buffer,
  648. sizeof(*connection_info), 300);
  649. if (retval < 0)
  650. dev_err(dev, "%s - error %d getting connection info\n",
  651. __func__, retval);
  652. else
  653. usb_serial_debug_data(debug, &serial->dev->dev, __func__,
  654. retval, transfer_buffer);
  655. kfree(transfer_buffer);
  656. return 0;
  657. }
  658. static int visor_probe(struct usb_serial *serial,
  659. const struct usb_device_id *id)
  660. {
  661. int retval = 0;
  662. int (*startup)(struct usb_serial *serial,
  663. const struct usb_device_id *id);
  664. dbg("%s", __func__);
  665. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  666. dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
  667. serial->dev->actconfig->desc.bConfigurationValue);
  668. return -ENODEV;
  669. }
  670. if (id->driver_info) {
  671. startup = (void *)id->driver_info;
  672. retval = startup(serial, id);
  673. }
  674. return retval;
  675. }
  676. static int visor_calc_num_ports(struct usb_serial *serial)
  677. {
  678. int num_ports = (int)(long)(usb_get_serial_data(serial));
  679. if (num_ports)
  680. usb_set_serial_data(serial, NULL);
  681. return num_ports;
  682. }
  683. static int generic_startup(struct usb_serial *serial)
  684. {
  685. struct usb_serial_port **ports = serial->port;
  686. struct visor_private *priv;
  687. int i;
  688. for (i = 0; i < serial->num_ports; ++i) {
  689. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  690. if (!priv) {
  691. while (i-- != 0) {
  692. priv = usb_get_serial_port_data(ports[i]);
  693. usb_set_serial_port_data(ports[i], NULL);
  694. kfree(priv);
  695. }
  696. return -ENOMEM;
  697. }
  698. spin_lock_init(&priv->lock);
  699. usb_set_serial_port_data(ports[i], priv);
  700. }
  701. return 0;
  702. }
  703. static int clie_3_5_startup(struct usb_serial *serial)
  704. {
  705. struct device *dev = &serial->dev->dev;
  706. int result;
  707. u8 *data;
  708. dbg("%s", __func__);
  709. data = kmalloc(1, GFP_KERNEL);
  710. if (!data)
  711. return -ENOMEM;
  712. /*
  713. * Note that PEG-300 series devices expect the following two calls.
  714. */
  715. /* get the config number */
  716. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  717. USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
  718. 0, 0, data, 1, 3000);
  719. if (result < 0) {
  720. dev_err(dev, "%s: get config number failed: %d\n",
  721. __func__, result);
  722. goto out;
  723. }
  724. if (result != 1) {
  725. dev_err(dev, "%s: get config number bad return length: %d\n",
  726. __func__, result);
  727. result = -EIO;
  728. goto out;
  729. }
  730. /* get the interface number */
  731. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  732. USB_REQ_GET_INTERFACE,
  733. USB_DIR_IN | USB_RECIP_INTERFACE,
  734. 0, 0, data, 1, 3000);
  735. if (result < 0) {
  736. dev_err(dev, "%s: get interface number failed: %d\n",
  737. __func__, result);
  738. goto out;
  739. }
  740. if (result != 1) {
  741. dev_err(dev,
  742. "%s: get interface number bad return length: %d\n",
  743. __func__, result);
  744. result = -EIO;
  745. goto out;
  746. }
  747. result = generic_startup(serial);
  748. out:
  749. kfree(data);
  750. return result;
  751. }
  752. static int treo_attach(struct usb_serial *serial)
  753. {
  754. struct usb_serial_port *swap_port;
  755. /* Only do this endpoint hack for the Handspring devices with
  756. * interrupt in endpoints, which for now are the Treo devices. */
  757. if (!((le16_to_cpu(serial->dev->descriptor.idVendor)
  758. == HANDSPRING_VENDOR_ID) ||
  759. (le16_to_cpu(serial->dev->descriptor.idVendor)
  760. == KYOCERA_VENDOR_ID)) ||
  761. (serial->num_interrupt_in == 0))
  762. goto generic_startup;
  763. dbg("%s", __func__);
  764. /*
  765. * It appears that Treos and Kyoceras want to use the
  766. * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint,
  767. * so let's swap the 1st and 2nd bulk in and interrupt endpoints.
  768. * Note that swapping the bulk out endpoints would break lots of
  769. * apps that want to communicate on the second port.
  770. */
  771. #define COPY_PORT(dest, src) \
  772. do { \
  773. dest->read_urb = src->read_urb; \
  774. dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress;\
  775. dest->bulk_in_buffer = src->bulk_in_buffer; \
  776. dest->interrupt_in_urb = src->interrupt_in_urb; \
  777. dest->interrupt_in_endpointAddress = \
  778. src->interrupt_in_endpointAddress;\
  779. dest->interrupt_in_buffer = src->interrupt_in_buffer; \
  780. } while (0);
  781. swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
  782. if (!swap_port)
  783. return -ENOMEM;
  784. COPY_PORT(swap_port, serial->port[0]);
  785. COPY_PORT(serial->port[0], serial->port[1]);
  786. COPY_PORT(serial->port[1], swap_port);
  787. kfree(swap_port);
  788. generic_startup:
  789. return generic_startup(serial);
  790. }
  791. static int clie_5_attach(struct usb_serial *serial)
  792. {
  793. dbg("%s", __func__);
  794. /* TH55 registers 2 ports.
  795. Communication in from the UX50/TH55 uses bulk_in_endpointAddress
  796. from port 0. Communication out to the UX50/TH55 uses
  797. bulk_out_endpointAddress from port 1
  798. Lets do a quick and dirty mapping
  799. */
  800. /* some sanity check */
  801. if (serial->num_ports < 2)
  802. return -1;
  803. /* port 0 now uses the modified endpoint Address */
  804. serial->port[0]->bulk_out_endpointAddress =
  805. serial->port[1]->bulk_out_endpointAddress;
  806. return generic_startup(serial);
  807. }
  808. static void visor_release(struct usb_serial *serial)
  809. {
  810. struct visor_private *priv;
  811. int i;
  812. dbg("%s", __func__);
  813. for (i = 0; i < serial->num_ports; i++) {
  814. priv = usb_get_serial_port_data(serial->port[i]);
  815. kfree(priv);
  816. }
  817. }
  818. static int __init visor_init(void)
  819. {
  820. int i, retval;
  821. /* Only if parameters were passed to us */
  822. if (vendor > 0 && product > 0) {
  823. struct usb_device_id usb_dev_temp[] = {
  824. {
  825. USB_DEVICE(vendor, product),
  826. .driver_info =
  827. (kernel_ulong_t) &palm_os_4_probe
  828. }
  829. };
  830. /* Find the last entry in id_table */
  831. for (i = 0;; i++) {
  832. if (id_table[i].idVendor == 0) {
  833. id_table[i] = usb_dev_temp[0];
  834. break;
  835. }
  836. }
  837. /* Find the last entry in id_table_combined */
  838. for (i = 0;; i++) {
  839. if (id_table_combined[i].idVendor == 0) {
  840. id_table_combined[i] = usb_dev_temp[0];
  841. break;
  842. }
  843. }
  844. printk(KERN_INFO KBUILD_MODNAME
  845. ": Untested USB device specified at time of module insertion\n");
  846. printk(KERN_INFO KBUILD_MODNAME
  847. ": Warning: This is not guaranteed to work\n");
  848. printk(KERN_INFO KBUILD_MODNAME
  849. ": Using a newer kernel is preferred to this method\n");
  850. printk(KERN_INFO KBUILD_MODNAME
  851. ": Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x\n",
  852. vendor, product);
  853. }
  854. retval = usb_serial_register(&handspring_device);
  855. if (retval)
  856. goto failed_handspring_register;
  857. retval = usb_serial_register(&clie_3_5_device);
  858. if (retval)
  859. goto failed_clie_3_5_register;
  860. retval = usb_serial_register(&clie_5_device);
  861. if (retval)
  862. goto failed_clie_5_register;
  863. retval = usb_register(&visor_driver);
  864. if (retval)
  865. goto failed_usb_register;
  866. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  867. return 0;
  868. failed_usb_register:
  869. usb_serial_deregister(&clie_5_device);
  870. failed_clie_5_register:
  871. usb_serial_deregister(&clie_3_5_device);
  872. failed_clie_3_5_register:
  873. usb_serial_deregister(&handspring_device);
  874. failed_handspring_register:
  875. return retval;
  876. }
  877. static void __exit visor_exit (void)
  878. {
  879. usb_deregister(&visor_driver);
  880. usb_serial_deregister(&handspring_device);
  881. usb_serial_deregister(&clie_3_5_device);
  882. usb_serial_deregister(&clie_5_device);
  883. }
  884. module_init(visor_init);
  885. module_exit(visor_exit);
  886. MODULE_AUTHOR(DRIVER_AUTHOR);
  887. MODULE_DESCRIPTION(DRIVER_DESC);
  888. MODULE_LICENSE("GPL");
  889. module_param(debug, bool, S_IRUGO | S_IWUSR);
  890. MODULE_PARM_DESC(debug, "Debug enabled or not");
  891. module_param(stats, bool, S_IRUGO | S_IWUSR);
  892. MODULE_PARM_DESC(stats, "Enables statistics or not");
  893. module_param(vendor, ushort, 0);
  894. MODULE_PARM_DESC(vendor, "User specified vendor ID");
  895. module_param(product, ushort, 0);
  896. MODULE_PARM_DESC(product, "User specified product ID");